diff --git a/runtime/Cpp/demo/TParser.g4 b/runtime/Cpp/demo/TParser.g4 index 9da3054a9a..9f25be93d3 100644 --- a/runtime/Cpp/demo/TParser.g4 +++ b/runtime/Cpp/demo/TParser.g4 @@ -103,8 +103,8 @@ expr: expr Star expr | expr QuestionMark expr Colon expr | expr Equal expr | identifier = id - | flowControl - | INT + | flowControl + | INT | String ; diff --git a/runtime/Cpp/runtime/src/Parser.cpp b/runtime/Cpp/runtime/src/Parser.cpp index e33859bbe0..736354c568 100755 --- a/runtime/Cpp/runtime/src/Parser.cpp +++ b/runtime/Cpp/runtime/src/Parser.cpp @@ -238,7 +238,7 @@ const atn::ATN& Parser::getATNWithBypassAlts() { throw UnsupportedOperationException("The current parser does not support an ATN with bypass alternatives."); } - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); // XXX: using the entire serialized ATN as key into the map is a big resource waste. // How large can that thing become? @@ -570,7 +570,7 @@ std::vector Parser::getRuleInvocationStack(RuleContext *p) { std::vector Parser::getDFAStrings() { atn::ParserATNSimulator *simulator = getInterpreter(); if (!simulator->decisionToDFA.empty()) { - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); std::vector s; for (size_t d = 0; d < simulator->decisionToDFA.size(); d++) { @@ -585,7 +585,7 @@ std::vector Parser::getDFAStrings() { void Parser::dumpDFA() { atn::ParserATNSimulator *simulator = getInterpreter(); if (!simulator->decisionToDFA.empty()) { - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); bool seenOne = false; for (size_t d = 0; d < simulator->decisionToDFA.size(); d++) { dfa::DFA &dfa = simulator->decisionToDFA[d]; diff --git a/runtime/Cpp/runtime/src/Parser.h b/runtime/Cpp/runtime/src/Parser.h index a5b454db3b..a3a9ca5e22 100755 --- a/runtime/Cpp/runtime/src/Parser.h +++ b/runtime/Cpp/runtime/src/Parser.h @@ -421,9 +421,6 @@ namespace antlr4 { std::vector _precedenceStack; - // Mutex to manage synchronized access for multithreading in the parser - std::recursive_mutex mtx; - /// /// Specifies whether or not the parser should construct a parse tree during /// the parsing process. The default value is {@code true}. diff --git a/runtime/Cpp/runtime/src/Recognizer.cpp b/runtime/Cpp/runtime/src/Recognizer.cpp index 8af14e3193..7ea856682c 100755 --- a/runtime/Cpp/runtime/src/Recognizer.cpp +++ b/runtime/Cpp/runtime/src/Recognizer.cpp @@ -61,7 +61,7 @@ dfa::Vocabulary const& Recognizer::getVocabulary() const { std::map Recognizer::getTokenTypeMap() { const dfa::Vocabulary& vocabulary = getVocabulary(); - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); std::map result; auto iterator = _tokenTypeMapCache.find(&vocabulary); if (iterator != _tokenTypeMapCache.end()) { @@ -91,7 +91,7 @@ std::map Recognizer::getRuleIndexMap() { throw "The current recognizer does not provide a list of rule names."; } - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); std::map result; auto iterator = _ruleIndexMapCache.find(ruleNames); if (iterator != _ruleIndexMapCache.end()) { diff --git a/runtime/Cpp/runtime/src/Recognizer.h b/runtime/Cpp/runtime/src/Recognizer.h index 661cbffa90..6015fd8f79 100755 --- a/runtime/Cpp/runtime/src/Recognizer.h +++ b/runtime/Cpp/runtime/src/Recognizer.h @@ -167,15 +167,15 @@ namespace antlr4 { protected: atn::ATNSimulator *_interpreter; // Set and deleted in descendants (or the profiler). + // Mutex to manage synchronized access for multithreading. + std::recursive_mutex _mutex; + private: static std::map> _tokenTypeMapCache; static std::map, std::map> _ruleIndexMapCache; ProxyErrorListener _proxListener; // Manages a collection of listeners. - // Mutex to manage synchronized access for multithreading. - std::recursive_mutex mtx; - size_t _stateNumber; void InitializeInstanceFields(); diff --git a/runtime/Cpp/runtime/src/atn/ATN.cpp b/runtime/Cpp/runtime/src/atn/ATN.cpp index f3916724aa..d1e2ad9f5b 100755 --- a/runtime/Cpp/runtime/src/atn/ATN.cpp +++ b/runtime/Cpp/runtime/src/atn/ATN.cpp @@ -50,6 +50,7 @@ ATN::ATN() : ATN(ATNType::LEXER, 0) { } ATN::ATN(ATN &&other) { + // All source vectors are implicitly cleared by the moves. states = std::move(other.states); decisionToState = std::move(other.decisionToState); ruleToStartState = std::move(other.ruleToStartState); @@ -92,6 +93,7 @@ ATN& ATN::operator = (ATN &other) NOEXCEPT { * operators it seems the copy operator is preferred causing trouble when releasing the allocated ATNState instances. */ ATN& ATN::operator = (ATN &&other) NOEXCEPT { + // All source vectors are implicitly cleared by the moves. states = std::move(other.states); decisionToState = std::move(other.decisionToState); ruleToStartState = std::move(other.ruleToStartState); diff --git a/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp index f537215317..2d928efb37 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp @@ -56,7 +56,7 @@ PredictionContextCache& ATNSimulator::getSharedContextCache() { } Ref ATNSimulator::getCachedContext(Ref const& context) { - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); std::map, Ref> visited; return PredictionContext::getCachedContext(context, _sharedContextCache, visited); } diff --git a/runtime/Cpp/runtime/src/atn/ATNSimulator.h b/runtime/Cpp/runtime/src/atn/ATNSimulator.h index 964a7f26b0..edc0a56738 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSimulator.h +++ b/runtime/Cpp/runtime/src/atn/ATNSimulator.h @@ -81,8 +81,8 @@ namespace atn { static ATNState *stateFactory(int type, int ruleIndex); protected: - // Mutex to manage synchronized access for multithreading - std::recursive_mutex mtx; + // Mutex to manage synchronized access for multithreading. + std::recursive_mutex _mutex; /// /// The context cache maps all PredictionContext objects that are equals() @@ -106,7 +106,6 @@ namespace atn { /// so it's not worth the complexity. /// PredictionContextCache &_sharedContextCache; - }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/ATNState.h b/runtime/Cpp/runtime/src/atn/ATNState.h index cc2095277e..f7c9b50b37 100755 --- a/runtime/Cpp/runtime/src/atn/ATNState.h +++ b/runtime/Cpp/runtime/src/atn/ATNState.h @@ -123,7 +123,6 @@ namespace atn { static const std::vector serializationNames; - /// Which ATN are we in? size_t stateNumber = INVALID_STATE_NUMBER; size_t ruleIndex = 0; // at runtime, we don't have Rule objects bool epsilonOnlyTransitions = false; @@ -143,7 +142,7 @@ namespace atn { virtual void addTransition(Transition *e); virtual void addTransition(size_t index, Transition *e); virtual Transition* removeTransition(size_t index); - virtual int getStateType() = 0; + virtual size_t getStateType() = 0; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/BasicBlockStartState.cpp b/runtime/Cpp/runtime/src/atn/BasicBlockStartState.cpp index 7ea889fa16..a9942f2950 100755 --- a/runtime/Cpp/runtime/src/atn/BasicBlockStartState.cpp +++ b/runtime/Cpp/runtime/src/atn/BasicBlockStartState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int BasicBlockStartState::getStateType() { +size_t BasicBlockStartState::getStateType() { return BLOCK_START; } diff --git a/runtime/Cpp/runtime/src/atn/BasicBlockStartState.h b/runtime/Cpp/runtime/src/atn/BasicBlockStartState.h index d3ec91bb6b..ad6c5750d3 100755 --- a/runtime/Cpp/runtime/src/atn/BasicBlockStartState.h +++ b/runtime/Cpp/runtime/src/atn/BasicBlockStartState.h @@ -40,7 +40,7 @@ namespace atn { class ANTLR4CPP_PUBLIC BasicBlockStartState final : public BlockStartState { public: - virtual int getStateType() override; + virtual size_t getStateType() override; }; diff --git a/runtime/Cpp/runtime/src/atn/BasicState.cpp b/runtime/Cpp/runtime/src/atn/BasicState.cpp index b387a42fe9..815515d64a 100755 --- a/runtime/Cpp/runtime/src/atn/BasicState.cpp +++ b/runtime/Cpp/runtime/src/atn/BasicState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int BasicState::getStateType() { +size_t BasicState::getStateType() { return BASIC; } diff --git a/runtime/Cpp/runtime/src/atn/BasicState.h b/runtime/Cpp/runtime/src/atn/BasicState.h index c85dacfa1f..e4d18ba1b1 100755 --- a/runtime/Cpp/runtime/src/atn/BasicState.h +++ b/runtime/Cpp/runtime/src/atn/BasicState.h @@ -39,7 +39,7 @@ namespace atn { class ANTLR4CPP_PUBLIC BasicState final : public ATNState { public: - virtual int getStateType() override; + virtual size_t getStateType() override; }; diff --git a/runtime/Cpp/runtime/src/atn/BlockEndState.cpp b/runtime/Cpp/runtime/src/atn/BlockEndState.cpp index f4b8199b32..3b8e3d79a6 100755 --- a/runtime/Cpp/runtime/src/atn/BlockEndState.cpp +++ b/runtime/Cpp/runtime/src/atn/BlockEndState.cpp @@ -36,6 +36,6 @@ using namespace antlr4::atn; BlockEndState::BlockEndState() : startState(nullptr) { } -int BlockEndState::getStateType() { +size_t BlockEndState::getStateType() { return BLOCK_END; } diff --git a/runtime/Cpp/runtime/src/atn/BlockEndState.h b/runtime/Cpp/runtime/src/atn/BlockEndState.h index 0f2d30b4bb..014cf3742b 100755 --- a/runtime/Cpp/runtime/src/atn/BlockEndState.h +++ b/runtime/Cpp/runtime/src/atn/BlockEndState.h @@ -36,15 +36,14 @@ namespace antlr4 { namespace atn { - /// - /// Terminal node of a simple {@code (a|b|c)} block. + /// Terminal node of a simple {@code (a|b|c)} block. class ANTLR4CPP_PUBLIC BlockEndState final : public ATNState { public: - BlockStartState *startState; + BlockStartState *startState = nullptr; BlockEndState(); - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/BlockStartState.h b/runtime/Cpp/runtime/src/atn/BlockStartState.h index e53f6ea398..339b37f292 100755 --- a/runtime/Cpp/runtime/src/atn/BlockStartState.h +++ b/runtime/Cpp/runtime/src/atn/BlockStartState.h @@ -36,11 +36,10 @@ namespace antlr4 { namespace atn { - /// - /// The start of a regular {@code (...)} block. + /// The start of a regular {@code (...)} block. class ANTLR4CPP_PUBLIC BlockStartState : public DecisionState { public: - BlockEndState *endState; + BlockEndState *endState = nullptr; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/EpsilonTransition.h b/runtime/Cpp/runtime/src/atn/EpsilonTransition.h index 4320a50a77..252792981f 100755 --- a/runtime/Cpp/runtime/src/atn/EpsilonTransition.h +++ b/runtime/Cpp/runtime/src/atn/EpsilonTransition.h @@ -43,7 +43,7 @@ namespace atn { /** * @return the rule index of a precedence rule for which this transition is - * returning from, where the precedence value is 0; otherwise, -1. + * returning from, where the precedence value is 0; otherwise, INVALID_INDEX. * * @see ATNConfig#isPrecedenceFilterSuppressed() * @see ParserATNSimulator#applyPrecedenceFilter(ATNConfigSet) diff --git a/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp b/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp index b496676b1a..149ef85856 100755 --- a/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp +++ b/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp @@ -102,7 +102,8 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref lookBusy.insert(c); - if (stopState != nullptr && s == stopState) { + // ml: s can never be null, hence no need to check if stopState is != null. + if (s == stopState) { if (ctx == nullptr) { look.add(Token::EPSILON); return; @@ -112,7 +113,7 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref } } - if (is(s)) { + if (s->getStateType() == ATNState::RULE_STOP) { if (ctx == nullptr) { look.add(Token::EPSILON); return; @@ -144,7 +145,7 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref for (size_t i = 0; i < n; i++) { Transition *t = s->transitions[i]; - if (is(t)) { + if (t->getSerializationType() == Transition::RULE) { if (calledRuleStack[(static_cast(t))->target->ruleIndex]) { continue; } @@ -165,13 +166,13 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref } } else if (t->isEpsilon()) { _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); - } else if (is(t)) { - look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (int)_atn.maxTokenType)); + } else if (t->getSerializationType() == Transition::WILDCARD) { + look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (ssize_t)_atn.maxTokenType)); } else { misc::IntervalSet set = t->label(); if (!set.isEmpty()) { if (is(t)) { - set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (int)_atn.maxTokenType)); + set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (ssize_t)_atn.maxTokenType)); } look.addAll(set); } diff --git a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp index e89c46e913..cb1c88a980 100755 --- a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp @@ -551,7 +551,7 @@ void LexerATNSimulator::addDFAEdge(dfa::DFAState *p, size_t t, dfa::DFAState *q) return; } - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); p->edges[t - MIN_DFA_EDGE] = q; // connect } @@ -579,7 +579,7 @@ dfa::DFAState *LexerATNSimulator::addDFAState(ATNConfigSet *configs) { dfa::DFA &dfa = _decisionToDFA[_mode]; { - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); if (!dfa.states.empty()) { auto iterator = dfa.states.find(proposed); diff --git a/runtime/Cpp/runtime/src/atn/LoopEndState.cpp b/runtime/Cpp/runtime/src/atn/LoopEndState.cpp index 76c84c7b14..70f99855a3 100755 --- a/runtime/Cpp/runtime/src/atn/LoopEndState.cpp +++ b/runtime/Cpp/runtime/src/atn/LoopEndState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int LoopEndState::getStateType() { +size_t LoopEndState::getStateType() { return LOOP_END; } diff --git a/runtime/Cpp/runtime/src/atn/LoopEndState.h b/runtime/Cpp/runtime/src/atn/LoopEndState.h index 3de98af060..740c2626cc 100755 --- a/runtime/Cpp/runtime/src/atn/LoopEndState.h +++ b/runtime/Cpp/runtime/src/atn/LoopEndState.h @@ -36,13 +36,12 @@ namespace antlr4 { namespace atn { - /// - /// Mark the end of a * or + loop. + /// Mark the end of a * or + loop. class ANTLR4CPP_PUBLIC LoopEndState final : public ATNState { public: - ATNState *loopBackState; + ATNState *loopBackState = nullptr; - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp index 3f8f8e691f..d49e8466f0 100755 --- a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp @@ -137,7 +137,7 @@ size_t ParserATNSimulator::adaptivePredict(TokenStream *input, size_t decision, dfa.s0->configs = std::move(s0_closure); // not used for prediction but useful to know start configs anyway dfa::DFAState *newState = new dfa::DFAState(applyPrecedenceFilter(dfa.s0->configs.get())); /* mem-check: managed by the DFA or deleted below */ s0 = addDFAState(dfa, newState); - dfa.setPrecedenceStartState(parser->getPrecedence(), s0); + dfa.setPrecedenceStartState(parser->getPrecedence(), s0, _mutex); if (s0 != newState) { delete newState; // If there was already a state with this config set we don't need the new one. } @@ -1182,7 +1182,7 @@ dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA &dfa, dfa::DFAState *from } { - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); from->edges[t] = to; // connect } @@ -1205,7 +1205,7 @@ dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA &dfa, dfa::DFAState *D) } { - std::lock_guard lck(mtx); + std::lock_guard lck(_mutex); auto existing = dfa.states.find(D); if (existing != dfa.states.end()) { diff --git a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h index 6a560ec48d..b999f7d791 100755 --- a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h +++ b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h @@ -280,9 +280,6 @@ namespace atn { private: PredictionMode mode; - // Mutex to manage synchronized access for multithreading in the parser atn simulator. - std::recursive_mutex _lock; - /// /// Each prediction operation uses a cache for merge of prediction contexts. /// Don't keep around as it wastes huge amounts of memory. The merge cache diff --git a/runtime/Cpp/runtime/src/atn/PlusBlockStartState.cpp b/runtime/Cpp/runtime/src/atn/PlusBlockStartState.cpp index be586f5635..f6144d14bf 100755 --- a/runtime/Cpp/runtime/src/atn/PlusBlockStartState.cpp +++ b/runtime/Cpp/runtime/src/atn/PlusBlockStartState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int PlusBlockStartState::getStateType() { +size_t PlusBlockStartState::getStateType() { return PLUS_BLOCK_START; } diff --git a/runtime/Cpp/runtime/src/atn/PlusBlockStartState.h b/runtime/Cpp/runtime/src/atn/PlusBlockStartState.h index 397dd542c0..569d0f470e 100755 --- a/runtime/Cpp/runtime/src/atn/PlusBlockStartState.h +++ b/runtime/Cpp/runtime/src/atn/PlusBlockStartState.h @@ -36,17 +36,15 @@ namespace antlr4 { namespace atn { - /// /// Start of {@code (A|B|...)+} loop. Technically a decision state, but - /// we don't use for code generation; somebody might need it, so I'm defining - /// it for completeness. In reality, the node is the - /// real decision-making note for {@code A+}. - /// + /// we don't use for code generation; somebody might need it, so I'm defining + /// it for completeness. In reality, the node is the + /// real decision-making note for {@code A+}. class ANTLR4CPP_PUBLIC PlusBlockStartState final : public BlockStartState { public: - PlusLoopbackState *loopBackState; + PlusLoopbackState *loopBackState = nullptr; - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/PlusLoopbackState.cpp b/runtime/Cpp/runtime/src/atn/PlusLoopbackState.cpp index b9472b7bb9..c844b6407f 100755 --- a/runtime/Cpp/runtime/src/atn/PlusLoopbackState.cpp +++ b/runtime/Cpp/runtime/src/atn/PlusLoopbackState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int PlusLoopbackState::getStateType() { +size_t PlusLoopbackState::getStateType() { return PLUS_LOOP_BACK; } diff --git a/runtime/Cpp/runtime/src/atn/PlusLoopbackState.h b/runtime/Cpp/runtime/src/atn/PlusLoopbackState.h index b5ea2c37bf..6549514a2b 100755 --- a/runtime/Cpp/runtime/src/atn/PlusLoopbackState.h +++ b/runtime/Cpp/runtime/src/atn/PlusLoopbackState.h @@ -36,14 +36,12 @@ namespace antlr4 { namespace atn { - /// - /// Decision state for {@code A+} and {@code (A|B)+}. It has two transitions: - /// one to the loop back to start of the block and one to exit. - /// + /// Decision state for {@code A+} and {@code (A|B)+}. It has two transitions: + /// one to the loop back to start of the block and one to exit. class ANTLR4CPP_PUBLIC PlusLoopbackState final : public DecisionState { public: - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/RuleStartState.cpp b/runtime/Cpp/runtime/src/atn/RuleStartState.cpp index 8545a95c63..e3a7281a04 100755 --- a/runtime/Cpp/runtime/src/atn/RuleStartState.cpp +++ b/runtime/Cpp/runtime/src/atn/RuleStartState.cpp @@ -37,6 +37,6 @@ RuleStartState::RuleStartState() { isLeftRecursiveRule = false; } -int RuleStartState::getStateType() { +size_t RuleStartState::getStateType() { return RULE_START; } diff --git a/runtime/Cpp/runtime/src/atn/RuleStartState.h b/runtime/Cpp/runtime/src/atn/RuleStartState.h index 690492f410..bed014f92c 100755 --- a/runtime/Cpp/runtime/src/atn/RuleStartState.h +++ b/runtime/Cpp/runtime/src/atn/RuleStartState.h @@ -40,10 +40,10 @@ namespace atn { public: RuleStartState(); - RuleStopState *stopState; - bool isLeftRecursiveRule; + RuleStopState *stopState = nullptr; + bool isLeftRecursiveRule = false; - virtual int getStateType(); + virtual size_t getStateType() override; }; diff --git a/runtime/Cpp/runtime/src/atn/RuleStopState.cpp b/runtime/Cpp/runtime/src/atn/RuleStopState.cpp index 1b9a195212..367aaf7d04 100755 --- a/runtime/Cpp/runtime/src/atn/RuleStopState.cpp +++ b/runtime/Cpp/runtime/src/atn/RuleStopState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int RuleStopState::getStateType() { +size_t RuleStopState::getStateType() { return RULE_STOP; } diff --git a/runtime/Cpp/runtime/src/atn/RuleStopState.h b/runtime/Cpp/runtime/src/atn/RuleStopState.h index 56b2a2dfc8..50909683e5 100755 --- a/runtime/Cpp/runtime/src/atn/RuleStopState.h +++ b/runtime/Cpp/runtime/src/atn/RuleStopState.h @@ -36,16 +36,14 @@ namespace antlr4 { namespace atn { - /// /// The last node in the ATN for a rule, unless that rule is the start symbol. - /// In that case, there is one transition to EOF. Later, we might encode - /// references to all calls to this rule to compute FOLLOW sets for - /// error handling. - /// + /// In that case, there is one transition to EOF. Later, we might encode + /// references to all calls to this rule to compute FOLLOW sets for + /// error handling. class ANTLR4CPP_PUBLIC RuleStopState final : public ATNState { public: - virtual int getStateType() override; + virtual size_t getStateType() override; }; diff --git a/runtime/Cpp/runtime/src/atn/RuleTransition.h b/runtime/Cpp/runtime/src/atn/RuleTransition.h index 572b480918..33d60e06b2 100755 --- a/runtime/Cpp/runtime/src/atn/RuleTransition.h +++ b/runtime/Cpp/runtime/src/atn/RuleTransition.h @@ -37,15 +37,13 @@ namespace antlr4 { namespace atn { class ANTLR4CPP_PUBLIC RuleTransition : public Transition { - /// - /// Ptr to the rule definition object for this rule ref public: + /// Ptr to the rule definition object for this rule ref. const size_t ruleIndex; // no Rule object at runtime const int precedence; - /// - /// What node to begin computations following ref to rule + /// What node to begin computations following ref to rule. ATNState *followState; /// @deprecated Use diff --git a/runtime/Cpp/runtime/src/atn/StarBlockStartState.cpp b/runtime/Cpp/runtime/src/atn/StarBlockStartState.cpp index 9c74fc6cce..1377cc4772 100755 --- a/runtime/Cpp/runtime/src/atn/StarBlockStartState.cpp +++ b/runtime/Cpp/runtime/src/atn/StarBlockStartState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int StarBlockStartState::getStateType() { +size_t StarBlockStartState::getStateType() { return STAR_BLOCK_START; } diff --git a/runtime/Cpp/runtime/src/atn/StarBlockStartState.h b/runtime/Cpp/runtime/src/atn/StarBlockStartState.h index 972cddf7b4..76e141960f 100755 --- a/runtime/Cpp/runtime/src/atn/StarBlockStartState.h +++ b/runtime/Cpp/runtime/src/atn/StarBlockStartState.h @@ -36,12 +36,11 @@ namespace antlr4 { namespace atn { - /// - /// The block that begins a closure loop. + /// The block that begins a closure loop. class ANTLR4CPP_PUBLIC StarBlockStartState final : public BlockStartState { public: - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/StarLoopEntryState.cpp b/runtime/Cpp/runtime/src/atn/StarLoopEntryState.cpp index 60604ef8cb..62dfeb67c0 100755 --- a/runtime/Cpp/runtime/src/atn/StarLoopEntryState.cpp +++ b/runtime/Cpp/runtime/src/atn/StarLoopEntryState.cpp @@ -36,6 +36,6 @@ using namespace antlr4::atn; StarLoopEntryState::StarLoopEntryState() : DecisionState(), isPrecedenceDecision(false) { } -int StarLoopEntryState::getStateType() { +size_t StarLoopEntryState::getStateType() { return STAR_LOOP_ENTRY; } diff --git a/runtime/Cpp/runtime/src/atn/StarLoopEntryState.h b/runtime/Cpp/runtime/src/atn/StarLoopEntryState.h index 9248ea3f7f..05b8fdfca6 100755 --- a/runtime/Cpp/runtime/src/atn/StarLoopEntryState.h +++ b/runtime/Cpp/runtime/src/atn/StarLoopEntryState.h @@ -50,11 +50,11 @@ namespace atn { * * @see DFA#isPrecedenceDfa() */ - bool isPrecedenceDecision; + bool isPrecedenceDecision = false; - StarLoopbackState *loopBackState; + StarLoopbackState *loopBackState = nullptr; - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/StarLoopbackState.cpp b/runtime/Cpp/runtime/src/atn/StarLoopbackState.cpp index 78529b6055..d69b681d9e 100755 --- a/runtime/Cpp/runtime/src/atn/StarLoopbackState.cpp +++ b/runtime/Cpp/runtime/src/atn/StarLoopbackState.cpp @@ -37,9 +37,9 @@ using namespace antlr4::atn; StarLoopEntryState *StarLoopbackState::getLoopEntryState() { - return static_cast(transitions[0]->target); + return dynamic_cast(transitions[0]->target); } -int StarLoopbackState::getStateType() { +size_t StarLoopbackState::getStateType() { return STAR_LOOP_BACK; } diff --git a/runtime/Cpp/runtime/src/atn/StarLoopbackState.h b/runtime/Cpp/runtime/src/atn/StarLoopbackState.h index 614331f713..defdd522ec 100755 --- a/runtime/Cpp/runtime/src/atn/StarLoopbackState.h +++ b/runtime/Cpp/runtime/src/atn/StarLoopbackState.h @@ -40,7 +40,7 @@ namespace atn { public: StarLoopEntryState *getLoopEntryState(); - virtual int getStateType() override; + virtual size_t getStateType() override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/TokensStartState.cpp b/runtime/Cpp/runtime/src/atn/TokensStartState.cpp index ec753fa4b2..b161cfd852 100755 --- a/runtime/Cpp/runtime/src/atn/TokensStartState.cpp +++ b/runtime/Cpp/runtime/src/atn/TokensStartState.cpp @@ -33,6 +33,6 @@ using namespace antlr4::atn; -int TokensStartState::getStateType() { +size_t TokensStartState::getStateType() { return TOKEN_START; } diff --git a/runtime/Cpp/runtime/src/atn/TokensStartState.h b/runtime/Cpp/runtime/src/atn/TokensStartState.h index 7975d21adc..31de545e4c 100755 --- a/runtime/Cpp/runtime/src/atn/TokensStartState.h +++ b/runtime/Cpp/runtime/src/atn/TokensStartState.h @@ -36,12 +36,11 @@ namespace antlr4 { namespace atn { - /// - /// The Tokens rule start state linking to each lexer rule start state + /// The Tokens rule start state linking to each lexer rule start state. class ANTLR4CPP_PUBLIC TokensStartState final : public DecisionState { public: - virtual int getStateType(); + virtual size_t getStateType(); }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/dfa/DFA.cpp b/runtime/Cpp/runtime/src/dfa/DFA.cpp index 845db50cea..e81d262813 100755 --- a/runtime/Cpp/runtime/src/dfa/DFA.cpp +++ b/runtime/Cpp/runtime/src/dfa/DFA.cpp @@ -60,9 +60,16 @@ DFA::DFA(atn::DecisionState *atnStartState, size_t decision) } DFA::DFA(DFA &&other) : atnStartState(std::move(other.atnStartState)), decision(std::move(other.decision)) { + // Source states are implicitly cleared by the move. states = std::move(other.states); - s0 = std::move(other.s0); - _precedenceDfa = std::move(other._precedenceDfa); + + // Manually move s0 pointers. + s0 = other.s0; + other.s0 = nullptr; + _s0Shadow = other._s0Shadow; + other._s0Shadow = nullptr; + + _precedenceDfa = other._precedenceDfa; } DFA::~DFA() { @@ -90,7 +97,7 @@ DFAState* DFA::getPrecedenceStartState(int precedence) const { return iterator->second; } -void DFA::setPrecedenceStartState(int precedence, DFAState *startState) { +void DFA::setPrecedenceStartState(int precedence, DFAState *startState, std::recursive_mutex &mutex) { if (!isPrecedenceDfa()) { throw IllegalStateException("Only precedence DFAs may contain a precedence start state."); } @@ -100,7 +107,7 @@ void DFA::setPrecedenceStartState(int precedence, DFAState *startState) { } { - std::unique_lock lock(_lock); + std::unique_lock lock(mutex); s0->edges[precedence] = startState; } } diff --git a/runtime/Cpp/runtime/src/dfa/DFA.h b/runtime/Cpp/runtime/src/dfa/DFA.h index 3916ee7325..7e7363c0af 100755 --- a/runtime/Cpp/runtime/src/dfa/DFA.h +++ b/runtime/Cpp/runtime/src/dfa/DFA.h @@ -88,7 +88,7 @@ namespace dfa { * @throws IllegalStateException if this is not a precedence DFA. * @see #isPrecedenceDfa() */ - void setPrecedenceStartState(int precedence, DFAState *startState); + void setPrecedenceStartState(int precedence, DFAState *startState, std::recursive_mutex &mutex); /// Return a list of all states in this DFA, ordered by state number. virtual std::vector getStates() const; @@ -108,8 +108,6 @@ namespace dfa { */ bool _precedenceDfa; DFAState *_s0Shadow = nullptr; // ml: assigned when we created s0 ourselves. - - std::recursive_mutex _lock; // To synchronize access to s0. }; } // namespace atn diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg index 1a4ed68a99..4773fe4707 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg @@ -924,7 +924,7 @@ RulePropertyRef_ctxHeader(r) ::= "" RulePropertyRef_ctx(r) ::= "->" ThisRulePropertyRef_start(r) ::= "ThisRulePropertyRef_start(r) _localctx->start" -ThisRulePropertyRef_stop(r) ::= "ThisRulePropertyRef_stop(r) _localctx->stop" +ThisRulePropertyRef_stop(r) ::= "ThisRulePropertyRef_stop(r) _localctx->stop" ThisRulePropertyRef_textHeader(r) ::= "" ThisRulePropertyRef_text(r) ::= "_input->getText(_localctx->start, _input->LT(-1))" @@ -955,7 +955,7 @@ TokenTypeDecl(t) ::= "size_t = 0;" TokenListDeclHeader(t) ::= "std::vector\ ;" TokenListDecl(t) ::= "" -RuleContextDeclHeader(r) ::= ":: *;" +RuleContextDeclHeader(r) ::= ":: * = nullptr;" RuleContextDecl(r) ::= "" RuleContextListDeclHeader(rdecl) ::= "std::vector\< *> ;"