Skip to content

Commit

Permalink
[C++] Optimizations and cleanups and const correctness, oh my
Browse files Browse the repository at this point in the history
  • Loading branch information
jcking authored and parrt committed Jan 31, 2022
1 parent ac55068 commit c41663a
Show file tree
Hide file tree
Showing 78 changed files with 233 additions and 324 deletions.
8 changes: 0 additions & 8 deletions runtime/Cpp/runtime/src/Recognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ bool Recognizer::precpred(RuleContext * /*localctx*/, int /*precedence*/) {
void Recognizer::action(RuleContext * /*localctx*/, size_t /*ruleIndex*/, size_t /*actionIndex*/) {
}

size_t Recognizer::getState() const {
return _stateNumber;
}

void Recognizer::setState(size_t atnState) {
_stateNumber = atnState;
}

void Recognizer::InitializeInstanceFields() {
_stateNumber = ATNState::INVALID_STATE_NUMBER;
_interpreter = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions runtime/Cpp/runtime/src/Recognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace antlr4 {

virtual void action(RuleContext *localctx, size_t ruleIndex, size_t actionIndex);

virtual size_t getState() const ;
size_t getState() const { return _stateNumber; }

// Get the ATN used by the recognizer for prediction.
virtual const atn::ATN& getATN() const = 0;
Expand All @@ -126,7 +126,7 @@ namespace antlr4 {
/// invoking rules. Combine this and we have complete ATN
/// configuration information.
/// </summary>
void setState(size_t atnState);
void setState(size_t atnState) { _stateNumber = atnState; }

virtual IntStream* getInputStream() = 0;

Expand Down
39 changes: 18 additions & 21 deletions runtime/Cpp/runtime/src/atn/ATNConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

using namespace antlr4::atn;

ATNConfig::ATNConfig(ATNState *state_, size_t alt_, Ref<PredictionContext> const& context_)
: ATNConfig(state_, alt_, context_, SemanticContext::NONE) {
ATNConfig::ATNConfig(ATNState *state_, size_t alt, Ref<PredictionContext> context)
: ATNConfig(state_, alt, std::move(context), SemanticContext::NONE) {
}

ATNConfig::ATNConfig(ATNState *state_, size_t alt_, Ref<PredictionContext> const& context_, Ref<SemanticContext> const& semanticContext_)
: state(state_), alt(alt_), context(context_), semanticContext(semanticContext_) {
ATNConfig::ATNConfig(ATNState *state, size_t alt, Ref<PredictionContext> context, Ref<SemanticContext> semanticContext)
: state(state), alt(alt), context(std::move(context)), semanticContext(std::move(semanticContext)) {
reachesIntoOuterContext = 0;
}

Expand All @@ -26,25 +26,22 @@ ATNConfig::ATNConfig(Ref<ATNConfig> const& c) : ATNConfig(c, c->state, c->contex
ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state_) : ATNConfig(c, state_, c->context, c->semanticContext) {
}

ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<SemanticContext> const& semanticContext)
: ATNConfig(c, state, c->context, semanticContext) {
ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<SemanticContext> semanticContext)
: ATNConfig(c, state, c->context, std::move(semanticContext)) {
}

ATNConfig::ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> const& semanticContext)
: ATNConfig(c, c->state, c->context, semanticContext) {
ATNConfig::ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> semanticContext)
: ATNConfig(c, c->state, c->context, std::move(semanticContext)) {
}

ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context)
: ATNConfig(c, state, context, c->semanticContext) {
ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> context)
: ATNConfig(c, state, std::move(context), c->semanticContext) {
}

ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context,
Ref<SemanticContext> const& semanticContext)
: state(state), alt(c->alt), context(context), reachesIntoOuterContext(c->reachesIntoOuterContext),
semanticContext(semanticContext) {
}

ATNConfig::~ATNConfig() {
ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> context,
Ref<SemanticContext> semanticContext)
: state(state), alt(c->alt), context(std::move(context)), reachesIntoOuterContext(c->reachesIntoOuterContext),
semanticContext(std::move(semanticContext)) {
}

size_t ATNConfig::hashCode() const {
Expand Down Expand Up @@ -73,22 +70,22 @@ void ATNConfig::setPrecedenceFilterSuppressed(bool value) {
}
}

bool ATNConfig::operator == (const ATNConfig &other) const {
bool ATNConfig::operator==(const ATNConfig &other) const {
return state->stateNumber == other.state->stateNumber && alt == other.alt &&
((context == other.context) || (*context == *other.context)) &&
*semanticContext == *other.semanticContext &&
isPrecedenceFilterSuppressed() == other.isPrecedenceFilterSuppressed();
}

bool ATNConfig::operator != (const ATNConfig &other) const {
bool ATNConfig::operator!=(const ATNConfig &other) const {
return !operator==(other);
}

std::string ATNConfig::toString() {
std::string ATNConfig::toString() const {
return toString(true);
}

std::string ATNConfig::toString(bool showAlt) {
std::string ATNConfig::toString(bool showAlt) const {
std::stringstream ss;
ss << "(";

Expand Down
25 changes: 13 additions & 12 deletions runtime/Cpp/runtime/src/atn/ATNConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,19 @@ namespace atn {
/// Can be shared between multiple ATNConfig instances.
Ref<SemanticContext> semanticContext;

ATNConfig(ATNState *state, size_t alt, Ref<PredictionContext> const& context);
ATNConfig(ATNState *state, size_t alt, Ref<PredictionContext> const& context, Ref<SemanticContext> const& semanticContext);
ATNConfig(ATNState *state, size_t alt, Ref<PredictionContext> context);
ATNConfig(ATNState *state, size_t alt, Ref<PredictionContext> context, Ref<SemanticContext> semanticContext);

ATNConfig(Ref<ATNConfig> const& c); // dup
ATNConfig(Ref<ATNConfig> const& c, ATNState *state);
ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<SemanticContext> const& semanticContext);
ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> const& semanticContext);
ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context);
ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context, Ref<SemanticContext> const& semanticContext);
ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<SemanticContext> semanticContext);
ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> semanticContext);
ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> context);
ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> context, Ref<SemanticContext> semanticContext);

ATNConfig(ATNConfig const&) = default;
virtual ~ATNConfig();

virtual ~ATNConfig() = default;

virtual size_t hashCode() const;

Expand All @@ -97,18 +98,18 @@ namespace atn {
* as it existed prior to the introduction of the
* {@link #isPrecedenceFilterSuppressed} method.
*/
size_t getOuterContextDepth() const ;
size_t getOuterContextDepth() const;
bool isPrecedenceFilterSuppressed() const;
void setPrecedenceFilterSuppressed(bool value);

/// An ATN configuration is equal to another if both have
/// the same state, they predict the same alternative, and
/// syntactic/semantic contexts are the same.
bool operator == (const ATNConfig &other) const;
bool operator != (const ATNConfig &other) const;
bool operator==(const ATNConfig &other) const;
bool operator!=(const ATNConfig &other) const;

virtual std::string toString();
std::string toString(bool showAlt);
virtual std::string toString() const;
std::string toString(bool showAlt) const;

private:
/**
Expand Down
40 changes: 21 additions & 19 deletions runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using namespace antlr4::atn;
using namespace antlrcpp;

ATNConfigSet::ATNConfigSet() : ATNConfigSet(true) {}

ATNConfigSet::ATNConfigSet(bool fullCtx) : fullCtx(fullCtx) {
InitializeInstanceFields();
}
Expand All @@ -27,9 +29,6 @@ ATNConfigSet::ATNConfigSet(const Ref<ATNConfigSet> &old) : ATNConfigSet(old->ful
dipsIntoOuterContext = old->dipsIntoOuterContext;
}

ATNConfigSet::~ATNConfigSet() {
}

bool ATNConfigSet::add(const Ref<ATNConfig> &config) {
return add(config, nullptr);
}
Expand Down Expand Up @@ -68,7 +67,7 @@ bool ATNConfigSet::add(const Ref<ATNConfig> &config, PredictionContextMergeCache
existing->setPrecedenceFilterSuppressed(true);
}

existing->context = merged; // replace context; no need to alt mapping
existing->context = std::move(merged); // replace context; no need to alt mapping

return true;
}
Expand All @@ -80,8 +79,9 @@ bool ATNConfigSet::addAll(const Ref<ATNConfigSet> &other) {
return false;
}

std::vector<ATNState*> ATNConfigSet::getStates() {
std::vector<ATNState*> ATNConfigSet::getStates() const {
std::vector<ATNState*> states;
states.reserve(configs.size());
for (const auto &c : configs) {
states.push_back(c->state);
}
Expand All @@ -97,16 +97,17 @@ std::vector<ATNState*> ATNConfigSet::getStates() {
* @since 4.3
*/

BitSet ATNConfigSet::getAlts() {
BitSet ATNConfigSet::getAlts() const {
BitSet alts;
for (const auto &config : configs) {
alts.set(config->alt);
}
return alts;
}

std::vector<Ref<SemanticContext>> ATNConfigSet::getPredicates() {
std::vector<Ref<SemanticContext>> ATNConfigSet::getPredicates() const {
std::vector<Ref<SemanticContext>> preds;
preds.reserve(configs.size());
for (const auto &c : configs) {
if (c->semanticContext != SemanticContext::NONE) {
preds.push_back(c->semanticContext);
Expand All @@ -131,7 +132,7 @@ void ATNConfigSet::optimizeConfigs(ATNSimulator *interpreter) {
}
}

bool ATNConfigSet::operator == (const ATNConfigSet &other) {
bool ATNConfigSet::operator==(const ATNConfigSet &other) const {
if (&other == this) {
return true;
}
Expand All @@ -147,22 +148,23 @@ bool ATNConfigSet::operator == (const ATNConfigSet &other) {
return Arrays::equals(configs, other.configs);
}

size_t ATNConfigSet::hashCode() {
if (!isReadonly() || _cachedHashCode == 0) {
_cachedHashCode = 1;
size_t ATNConfigSet::hashCode() const {
size_t cachedHashCode = _cachedHashCode.load(std::memory_order_relaxed);
if (!isReadonly() || cachedHashCode == 0) {
cachedHashCode = 1;
for (const auto &i : configs) {
_cachedHashCode = 31 * _cachedHashCode + i->hashCode(); // Same as Java's list hashCode impl.
cachedHashCode = 31 * cachedHashCode + i->hashCode(); // Same as Java's list hashCode impl.
}
_cachedHashCode.store(cachedHashCode, std::memory_order_relaxed);
}

return _cachedHashCode;
return cachedHashCode;
}

size_t ATNConfigSet::size() {
size_t ATNConfigSet::size() const {
return configs.size();
}

bool ATNConfigSet::isEmpty() {
bool ATNConfigSet::isEmpty() const {
return configs.empty();
}

Expand All @@ -175,7 +177,7 @@ void ATNConfigSet::clear() {
_configLookup.clear();
}

bool ATNConfigSet::isReadonly() {
bool ATNConfigSet::isReadonly() const {
return _readonly;
}

Expand All @@ -184,7 +186,7 @@ void ATNConfigSet::setReadonly(bool readonly) {
_configLookup.clear();
}

std::string ATNConfigSet::toString() {
std::string ATNConfigSet::toString() const {
std::stringstream ss;
ss << "[";
for (size_t i = 0; i < configs.size(); i++) {
Expand All @@ -210,7 +212,7 @@ std::string ATNConfigSet::toString() {
return ss.str();
}

size_t ATNConfigSet::getHash(ATNConfig *c) {
size_t ATNConfigSet::getHash(const ATNConfig *c) const {
size_t hashCode = 7;
hashCode = 31 * hashCode + c->state->stateNumber;
hashCode = 31 * hashCode + c->alt;
Expand Down
31 changes: 17 additions & 14 deletions runtime/Cpp/runtime/src/atn/ATNConfigSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ namespace atn {
/// it's a wildcard whereas it is not for LL context merge.
const bool fullCtx;

ATNConfigSet(bool fullCtx = true);
ATNConfigSet(const Ref<ATNConfigSet> &old);
ATNConfigSet();

virtual ~ATNConfigSet();
explicit ATNConfigSet(bool fullCtx);

explicit ATNConfigSet(const Ref<ATNConfigSet> &old);

virtual ~ATNConfigSet() = default;

virtual bool add(const Ref<ATNConfig> &config);

Expand All @@ -58,7 +61,7 @@ namespace atn {
/// </summary>
virtual bool add(const Ref<ATNConfig> &config, PredictionContextMergeCache *mergeCache);

virtual std::vector<ATNState *> getStates();
virtual std::vector<ATNState *> getStates() const;

/**
* Gets the complete set of represented alternatives for the configuration
Expand All @@ -68,23 +71,23 @@ namespace atn {
*
* @since 4.3
*/
antlrcpp::BitSet getAlts();
virtual std::vector<Ref<SemanticContext>> getPredicates();
antlrcpp::BitSet getAlts() const;
virtual std::vector<Ref<SemanticContext>> getPredicates() const;

virtual Ref<ATNConfig> get(size_t i) const;

virtual void optimizeConfigs(ATNSimulator *interpreter);

bool addAll(const Ref<ATNConfigSet> &other);

bool operator == (const ATNConfigSet &other);
virtual size_t hashCode();
virtual size_t size();
virtual bool isEmpty();
bool operator==(const ATNConfigSet &other) const;
virtual size_t hashCode() const;
virtual size_t size() const;
virtual bool isEmpty() const;
virtual void clear();
virtual bool isReadonly();
virtual bool isReadonly() const;
virtual void setReadonly(bool readonly);
virtual std::string toString();
virtual std::string toString() const;

protected:
/// Indicates that the set of configurations is read-only. Do not
Expand All @@ -94,10 +97,10 @@ namespace atn {
/// we've made this readonly.
bool _readonly;

virtual size_t getHash(ATNConfig *c); // Hash differs depending on set type.
virtual size_t getHash(const ATNConfig *c) const; // Hash differs depending on set type.

private:
size_t _cachedHashCode;
mutable std::atomic<size_t> _cachedHashCode;

/// All configs but hashed by (s, i, _, pi) not including context. Wiped out
/// when we go readonly as this set becomes a DFA state.
Expand Down
9 changes: 3 additions & 6 deletions runtime/Cpp/runtime/src/atn/ATNState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
using namespace antlr4::atn;
using namespace antlrcpp;

ATNState::ATNState() {
}

ATNState::~ATNState() {
for (auto *transition : transitions) {
delete transition;
Expand All @@ -28,15 +25,15 @@ const std::vector<std::string> ATNState::serializationNames = {
"BLOCK_END", "STAR_LOOP_BACK", "STAR_LOOP_ENTRY", "PLUS_LOOP_BACK", "LOOP_END"
};

size_t ATNState::hashCode() {
size_t ATNState::hashCode() const {
return stateNumber;
}

bool ATNState::operator == (const ATNState &other) {
bool ATNState::operator==(const ATNState &other) const {
return stateNumber == other.stateNumber;
}

bool ATNState::isNonGreedyExitState() {
bool ATNState::isNonGreedyExitState() const {
return false;
}

Expand Down
Loading

0 comments on commit c41663a

Please sign in to comment.