From 3c0833860151b739cf5bfbe8811055845b097d74 Mon Sep 17 00:00:00 2001 From: Justin King Date: Fri, 8 Apr 2022 13:13:07 -0700 Subject: [PATCH] [C++] Remove more dynamic_cast usage --- runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp | 3 ++- runtime/Cpp/runtime/src/Parser.cpp | 13 ++++++------- runtime/Cpp/runtime/src/ParserInterpreter.cpp | 9 +++++---- runtime/Cpp/runtime/src/ParserRuleContext.cpp | 6 ++++-- runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp | 3 ++- runtime/Cpp/runtime/src/dfa/DFA.cpp | 5 +++-- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp b/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp index 2b12cab7711..54d35ee3b17 100755 --- a/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp +++ b/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp @@ -13,6 +13,7 @@ #include "atn/ATN.h" #include "atn/ATNState.h" #include "support/StringUtils.h" +#include "support/Casts.h" #include "Parser.h" #include "CommonToken.h" #include "Vocabulary.h" @@ -308,7 +309,7 @@ misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) while (ctx->invokingState != ATNState::INVALID_STATE_NUMBER) { // compute what follows who invoked us atn::ATNState *invokingState = atn.states[ctx->invokingState]; - const atn::RuleTransition *rt = dynamic_cast(invokingState->transitions[0].get()); + const atn::RuleTransition *rt = downCast(invokingState->transitions[0].get()); misc::IntervalSet follow = atn.nextTokens(rt->followState); recoverSet.addAll(follow); diff --git a/runtime/Cpp/runtime/src/Parser.cpp b/runtime/Cpp/runtime/src/Parser.cpp index ec74ca89e2f..35293491973 100755 --- a/runtime/Cpp/runtime/src/Parser.cpp +++ b/runtime/Cpp/runtime/src/Parser.cpp @@ -348,7 +348,6 @@ void Parser::addContextToParseTree() { if (_ctx->parent == nullptr) return; - ParserRuleContext *parent = dynamic_cast(_ctx->parent); parent->addChild(_ctx); } @@ -377,7 +376,7 @@ void Parser::exitRule() { triggerExitRuleEvent(); } setState(_ctx->invokingState); - _ctx = dynamic_cast(_ctx->parent); + _ctx = downCast(_ctx->parent); } void Parser::enterOuterAlt(ParserRuleContext *localctx, size_t altNum) { @@ -387,7 +386,7 @@ void Parser::enterOuterAlt(ParserRuleContext *localctx, size_t altNum) { // that is previous child of parse tree if (_buildParseTrees && _ctx != localctx) { if (_ctx->parent != nullptr) { - ParserRuleContext *parent = dynamic_cast(_ctx->parent); + ParserRuleContext *parent = downCast(_ctx->parent); parent->removeLastChild(); parent->addChild(localctx); } @@ -443,7 +442,7 @@ void Parser::unrollRecursionContexts(ParserRuleContext *parentctx) { if (_parseListeners.size() > 0) { while (_ctx != parentctx) { triggerExitRuleEvent(); - _ctx = dynamic_cast(_ctx->parent); + _ctx = downCast(_ctx->parent); } } else { _ctx = parentctx; @@ -466,7 +465,7 @@ ParserRuleContext* Parser::getInvokingContext(size_t ruleIndex) { } if (p->parent == nullptr) break; - p = dynamic_cast(p->parent); + p = downCast(p->parent); } return nullptr; } @@ -510,7 +509,7 @@ bool Parser::isExpectedToken(size_t symbol) { return true; } - ctx = dynamic_cast(ctx->parent); + ctx = downCast(ctx->parent); } if (following.contains(Token::EPSILON) && symbol == EOF) { @@ -565,7 +564,7 @@ std::vector Parser::getRuleInvocationStack(RuleContext *p) { } if (p->parent == nullptr) break; - run = dynamic_cast(run->parent); + run = run->parent; } return stack; } diff --git a/runtime/Cpp/runtime/src/ParserInterpreter.cpp b/runtime/Cpp/runtime/src/ParserInterpreter.cpp index 8b7c8054456..e3be2d2c612 100755 --- a/runtime/Cpp/runtime/src/ParserInterpreter.cpp +++ b/runtime/Cpp/runtime/src/ParserInterpreter.cpp @@ -26,6 +26,7 @@ #include "tree/ErrorNode.h" #include "support/CPPUtils.h" +#include "support/Casts.h" #include "ParserInterpreter.h" @@ -148,16 +149,16 @@ atn::ATNState* ParserInterpreter::getATNState() { void ParserInterpreter::visitState(atn::ATNState *p) { size_t predictedAlt = 1; - if (is(p)) { - predictedAlt = visitDecisionState(dynamic_cast(p)); + if (DecisionState::is(p)) { + predictedAlt = visitDecisionState(downCast(p)); } const atn::Transition *transition = p->transitions[predictedAlt - 1].get(); switch (transition->getTransitionType()) { case atn::TransitionType::EPSILON: if (p->getStateType() == ATNStateType::STAR_LOOP_ENTRY && - (dynamic_cast(p))->isPrecedenceDecision && - !is(transition->target)) { + (downCast(p))->isPrecedenceDecision && + !LoopEndState::is(transition->target)) { // We are at the start of a left recursive rule's (...)* loop // and we're not taking the exit branch of loop. InterpreterRuleContext *localctx = createInterpreterRuleContext(_parentContextStack.top().first, diff --git a/runtime/Cpp/runtime/src/ParserRuleContext.cpp b/runtime/Cpp/runtime/src/ParserRuleContext.cpp index e57eda6e43a..184929ef88f 100755 --- a/runtime/Cpp/runtime/src/ParserRuleContext.cpp +++ b/runtime/Cpp/runtime/src/ParserRuleContext.cpp @@ -40,10 +40,12 @@ void ParserRuleContext::copyFrom(ParserRuleContext *ctx) { // copy any error nodes to alt label node if (!ctx->children.empty()) { for (auto *child : ctx->children) { + if (ErrorNode::is(child)) { + child->setParent(this); + children.push_back(child); + } auto *errorNode = dynamic_cast(child); if (errorNode != nullptr) { - errorNode->setParent(this); - children.push_back(errorNode); } } diff --git a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp index d352012ea68..8a828144581 100755 --- a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp @@ -31,6 +31,7 @@ #include "Vocabulary.h" #include "support/Arrays.h" +#include "support/Casts.h" #include "atn/ParserATNSimulator.h" @@ -913,7 +914,7 @@ void ParserATNSimulator::closure_(Ref const& config, ATNConfigSet *co closureBusy.insert(c); if (_dfa != nullptr && _dfa->isPrecedenceDfa()) { - size_t outermostPrecedenceReturn = dynamic_cast(t)->outermostPrecedenceReturn(); + size_t outermostPrecedenceReturn = downCast(t)->outermostPrecedenceReturn(); if (outermostPrecedenceReturn == _dfa->atnStartState->ruleIndex) { c->setPrecedenceFilterSuppressed(true); } diff --git a/runtime/Cpp/runtime/src/dfa/DFA.cpp b/runtime/Cpp/runtime/src/dfa/DFA.cpp index 5c11b14bdf5..ef5b07c5ea4 100755 --- a/runtime/Cpp/runtime/src/dfa/DFA.cpp +++ b/runtime/Cpp/runtime/src/dfa/DFA.cpp @@ -8,6 +8,7 @@ #include "support/CPPUtils.h" #include "atn/StarLoopEntryState.h" #include "atn/ATNConfigSet.h" +#include "support/Casts.h" #include "dfa/DFA.h" @@ -22,8 +23,8 @@ DFA::DFA(atn::DecisionState *atnStartState, size_t decision) : atnStartState(atnStartState), s0(nullptr), decision(decision) { _precedenceDfa = false; - if (is(atnStartState)) { - if (static_cast(atnStartState)->isPrecedenceDecision) { + if (atn::StarLoopEntryState::is(atnStartState)) { + if (downCast(atnStartState)->isPrecedenceDecision) { _precedenceDfa = true; s0 = new DFAState(std::unique_ptr(new atn::ATNConfigSet())); s0->isAcceptState = false;