1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
9 #include "misc/IntervalSet.h"
10 #include "support/CPPUtils.h"
11 #include "atn/PredictionContext.h"
16 class ANTLR4CPP_PUBLIC ATNSimulator {
18 /// Must distinguish between missing edge and edge we know leads nowhere.
19 static const Ref<dfa::DFAState> ERROR;
22 ATNSimulator(const ATN &atn, PredictionContextCache &sharedContextCache);
23 virtual ~ATNSimulator();
25 virtual void reset() = 0;
28 * Clear the DFA cache used by the current instance. Since the DFA cache may
29 * be shared by multiple ATN simulators, this method may affect the
30 * performance (but not accuracy) of other parsers which are being used
33 * @throws UnsupportedOperationException if the current instance does not
34 * support clearing the DFA.
38 virtual void clearDFA();
39 virtual PredictionContextCache& getSharedContextCache();
40 virtual Ref<PredictionContext> getCachedContext(Ref<PredictionContext> const& context);
42 /// @deprecated Use <seealso cref="ATNDeserializer#deserialize"/> instead.
43 static ATN deserialize(const std::vector<uint16_t> &data);
45 /// @deprecated Use <seealso cref="ATNDeserializer#checkCondition(boolean)"/> instead.
46 static void checkCondition(bool condition);
48 /// @deprecated Use <seealso cref="ATNDeserializer#checkCondition(boolean, String)"/> instead.
49 static void checkCondition(bool condition, const std::string &message);
51 /// @deprecated Use <seealso cref="ATNDeserializer#edgeFactory"/> instead.
52 static Transition *edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3,
53 const std::vector<misc::IntervalSet> &sets);
55 /// @deprecated Use <seealso cref="ATNDeserializer#stateFactory"/> instead.
56 static ATNState *stateFactory(int type, int ruleIndex);
59 static antlrcpp::SingleWriteMultipleReadLock _stateLock; // Lock for DFA states.
60 static antlrcpp::SingleWriteMultipleReadLock _edgeLock; // Lock for the sparse edge map in DFA states.
63 /// The context cache maps all PredictionContext objects that are equals()
64 /// to a single cached copy. This cache is shared across all contexts
65 /// in all ATNConfigs in all DFA states. We rebuild each ATNConfigSet
66 /// to use only cached nodes/graphs in addDFAState(). We don't want to
67 /// fill this during closure() since there are lots of contexts that
68 /// pop up but are not used ever again. It also greatly slows down closure().
70 /// This cache makes a huge difference in memory and a little bit in speed.
71 /// For the Java grammar on java.*, it dropped the memory requirements
72 /// at the end from 25M to 16M. We don't store any of the full context
73 /// graphs in the DFA because they are limited to local context only,
74 /// but apparently there's a lot of repetition there as well. We optimize
75 /// the config contexts before storing the config set in the DFA states
76 /// by literally rebuilding them with cached subgraphs only.
78 /// I tried a cache for use during closure operations, that was
79 /// whacked after each adaptivePredict(). It cost a little bit
80 /// more time I think and doesn't save on the overall footprint
81 /// so it's not worth the complexity.
83 PredictionContextCache &_sharedContextCache;