Skip to content

Commit

Permalink
[C++] Remove more dynamic_cast usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jcking committed Apr 8, 2022
1 parent 2095446 commit d0c6f55
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
3 changes: 2 additions & 1 deletion runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<const atn::RuleTransition*>(invokingState->transitions[0].get());
const atn::RuleTransition *rt = downCast<const atn::RuleTransition*>(invokingState->transitions[0].get());
misc::IntervalSet follow = atn.nextTokens(rt->followState);
recoverSet.addAll(follow);

Expand Down
18 changes: 9 additions & 9 deletions runtime/Cpp/runtime/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ void Parser::addContextToParseTree() {
if (_ctx->parent == nullptr)
return;

ParserRuleContext *parent = dynamic_cast<ParserRuleContext *>(_ctx->parent);
parent->addChild(_ctx);
downCast<ParserRuleContext*>(_ctx->parent)->addChild(_ctx);
}

void Parser::enterRule(ParserRuleContext *localctx, size_t state, size_t /*ruleIndex*/) {
Expand Down Expand Up @@ -377,7 +376,7 @@ void Parser::exitRule() {
triggerExitRuleEvent();
}
setState(_ctx->invokingState);
_ctx = dynamic_cast<ParserRuleContext *>(_ctx->parent);
_ctx = downCast<ParserRuleContext*>(_ctx->parent);
}

void Parser::enterOuterAlt(ParserRuleContext *localctx, size_t altNum) {
Expand All @@ -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<ParserRuleContext *>(_ctx->parent);
ParserRuleContext *parent = downCast<ParserRuleContext*>(_ctx->parent);
parent->removeLastChild();
parent->addChild(localctx);
}
Expand Down Expand Up @@ -443,7 +442,7 @@ void Parser::unrollRecursionContexts(ParserRuleContext *parentctx) {
if (_parseListeners.size() > 0) {
while (_ctx != parentctx) {
triggerExitRuleEvent();
_ctx = dynamic_cast<ParserRuleContext *>(_ctx->parent);
_ctx = downCast<ParserRuleContext*>(_ctx->parent);
}
} else {
_ctx = parentctx;
Expand All @@ -466,7 +465,7 @@ ParserRuleContext* Parser::getInvokingContext(size_t ruleIndex) {
}
if (p->parent == nullptr)
break;
p = dynamic_cast<ParserRuleContext *>(p->parent);
p = downCast<ParserRuleContext*>(p->parent);
}
return nullptr;
}
Expand Down Expand Up @@ -510,7 +509,7 @@ bool Parser::isExpectedToken(size_t symbol) {
return true;
}

ctx = dynamic_cast<ParserRuleContext *>(ctx->parent);
ctx = downCast<ParserRuleContext*>(ctx->parent);
}

if (following.contains(Token::EPSILON) && symbol == EOF) {
Expand Down Expand Up @@ -563,9 +562,10 @@ std::vector<std::string> Parser::getRuleInvocationStack(RuleContext *p) {
} else {
stack.push_back(ruleNames[ruleIndex]);
}
if (p->parent == nullptr)
if (!RuleContext::is(run->parent)) {
break;
run = dynamic_cast<RuleContext *>(run->parent);
}
run = downCast<RuleContext*>(run->parent);
}
return stack;
}
Expand Down
9 changes: 5 additions & 4 deletions runtime/Cpp/runtime/src/ParserInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "tree/ErrorNode.h"

#include "support/CPPUtils.h"
#include "support/Casts.h"

#include "ParserInterpreter.h"

Expand Down Expand Up @@ -148,16 +149,16 @@ atn::ATNState* ParserInterpreter::getATNState() {

void ParserInterpreter::visitState(atn::ATNState *p) {
size_t predictedAlt = 1;
if (is<DecisionState *>(p)) {
predictedAlt = visitDecisionState(dynamic_cast<DecisionState *>(p));
if (DecisionState::is(p)) {
predictedAlt = visitDecisionState(downCast<DecisionState*>(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<StarLoopEntryState *>(p))->isPrecedenceDecision &&
!is<LoopEndState *>(transition->target)) {
(downCast<StarLoopEntryState *>(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,
Expand Down
7 changes: 3 additions & 4 deletions runtime/Cpp/runtime/src/ParserRuleContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ void ParserRuleContext::copyFrom(ParserRuleContext *ctx) {
// copy any error nodes to alt label node
if (!ctx->children.empty()) {
for (auto *child : ctx->children) {
auto *errorNode = dynamic_cast<ErrorNode *>(child);
if (errorNode != nullptr) {
errorNode->setParent(this);
children.push_back(errorNode);
if (ErrorNode::is(child)) {
downCast<ErrorNode*>(child)->setParent(this);
children.push_back(child);
}
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "Vocabulary.h"
#include "support/Arrays.h"
#include "support/Casts.h"

#include "atn/ParserATNSimulator.h"

Expand Down Expand Up @@ -913,7 +914,7 @@ void ParserATNSimulator::closure_(Ref<ATNConfig> const& config, ATNConfigSet *co
closureBusy.insert(c);

if (_dfa != nullptr && _dfa->isPrecedenceDfa()) {
size_t outermostPrecedenceReturn = dynamic_cast<const EpsilonTransition *>(t)->outermostPrecedenceReturn();
size_t outermostPrecedenceReturn = downCast<const EpsilonTransition *>(t)->outermostPrecedenceReturn();
if (outermostPrecedenceReturn == _dfa->atnStartState->ruleIndex) {
c->setPrecedenceFilterSuppressed(true);
}
Expand Down
5 changes: 3 additions & 2 deletions runtime/Cpp/runtime/src/dfa/DFA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "support/CPPUtils.h"
#include "atn/StarLoopEntryState.h"
#include "atn/ATNConfigSet.h"
#include "support/Casts.h"

#include "dfa/DFA.h"

Expand All @@ -22,8 +23,8 @@ DFA::DFA(atn::DecisionState *atnStartState, size_t decision)
: atnStartState(atnStartState), s0(nullptr), decision(decision) {

_precedenceDfa = false;
if (is<atn::StarLoopEntryState *>(atnStartState)) {
if (static_cast<atn::StarLoopEntryState *>(atnStartState)->isPrecedenceDecision) {
if (atn::StarLoopEntryState::is(atnStartState)) {
if (downCast<atn::StarLoopEntryState*>(atnStartState)->isPrecedenceDecision) {
_precedenceDfa = true;
s0 = new DFAState(std::unique_ptr<atn::ATNConfigSet>(new atn::ATNConfigSet()));
s0->isAcceptState = false;
Expand Down

0 comments on commit d0c6f55

Please sign in to comment.