[vm] Cleanup unused SourceLabel and NameReference

TEST=ci

Change-Id: Iffe90eca5b84f76ec2a1d22a31939b51f726909b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/235862
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Alexander Markov 2022-03-08 18:06:14 +00:00 committed by Commit Bot
parent c9b322c2cf
commit dc8f487bae
4 changed files with 1 additions and 263 deletions

View file

@ -35,19 +35,10 @@ class Isolate;
class LocalScope;
class LocalVariable;
struct RegExpCompileData;
class SourceLabel;
template <typename T>
class GrowableArray;
class Parser;
struct CatchParamDesc;
class ClassDesc;
struct MemberDesc;
struct ParamList;
struct QualIdent;
class TopLevel;
class RecursionChecker;
// The class ParsedFunction holds the result of parsing a function.
class ParsedFunction : public ZoneAllocated {
public:

View file

@ -21,11 +21,6 @@ DEFINE_FLAG(bool,
"enclosing scope (up to innermost loop) and spare the allocation "
"of a local context.");
int SourceLabel::FunctionLevel() const {
ASSERT(owner() != NULL);
return owner()->function_level();
}
LocalScope::LocalScope(LocalScope* parent, int function_level, int loop_level)
: parent_(parent),
child_(NULL),
@ -36,11 +31,9 @@ LocalScope::LocalScope(LocalScope* parent, int function_level, int loop_level)
begin_token_pos_(TokenPosition::kNoSource),
end_token_pos_(TokenPosition::kNoSource),
variables_(),
labels_(),
context_variables_(),
context_slots_(new (Thread::Current()->zone())
ZoneGrowableArray<const Slot*>()),
referenced_() {
ZoneGrowableArray<const Slot*>()) {
// Hook this node into the children of the parent, unless the parent has a
// different function_level, since the local scope of a nested function can
// be discarded after it has been parsed.
@ -87,66 +80,6 @@ bool LocalScope::InsertParameterAt(intptr_t pos, LocalVariable* parameter) {
return true;
}
bool LocalScope::AddLabel(SourceLabel* label) {
if (LocalLookupLabel(label->name()) != NULL) {
return false;
}
labels_.Add(label);
if (label->owner() == NULL) {
// Labels must be added to their owner scope first. Subsequent calls
// to 'add' treat the label as an alias.
label->set_owner(this);
}
return true;
}
void LocalScope::MoveLabel(SourceLabel* label) {
ASSERT(LocalLookupLabel(label->name()) == NULL);
ASSERT(label->kind() == SourceLabel::kForward);
labels_.Add(label);
label->set_owner(this);
}
NameReference* LocalScope::FindReference(const String& name) const {
ASSERT(name.IsSymbol());
intptr_t num_references = referenced_.length();
for (intptr_t i = 0; i < num_references; i++) {
if (name.ptr() == referenced_[i]->name().ptr()) {
return referenced_[i];
}
}
return NULL;
}
void LocalScope::AddReferencedName(TokenPosition token_pos,
const String& name) {
if (LocalLookupVariable(name) != NULL) {
return;
}
NameReference* ref = FindReference(name);
if (ref != NULL) {
ref->set_token_pos(token_pos);
return;
}
ref = new NameReference(token_pos, name);
referenced_.Add(ref);
// Add name reference in innermost enclosing scopes that do not
// define a local variable with this name.
LocalScope* scope = this->parent();
while (scope != NULL && (scope->LocalLookupVariable(name) == NULL)) {
scope->referenced_.Add(ref);
scope = scope->parent();
}
}
TokenPosition LocalScope::PreviousReferencePos(const String& name) const {
NameReference* ref = FindReference(name);
if (ref != NULL) {
return ref->token_pos();
}
return TokenPosition::kNoSource;
}
void LocalScope::AllocateContextVariable(LocalVariable* variable,
LocalScope** context_owner) {
ASSERT(variable->is_captured());
@ -464,17 +397,6 @@ void LocalScope::CollectLocalVariables(LocalVarDescriptorsBuilder* vars,
}
}
SourceLabel* LocalScope::LocalLookupLabel(const String& name) const {
ASSERT(name.IsSymbol());
for (intptr_t i = 0; i < labels_.length(); i++) {
SourceLabel* label = labels_[i];
if (label->name().ptr() == name.ptr()) {
return label;
}
}
return NULL;
}
LocalVariable* LocalScope::LocalLookupVariable(const String& name) const {
ASSERT(name.IsSymbol());
for (intptr_t i = 0; i < variables_.length(); i++) {
@ -529,71 +451,6 @@ void LocalScope::CaptureVariable(LocalVariable* variable) {
}
}
SourceLabel* LocalScope::LookupLabel(const String& name) {
LocalScope* current_scope = this;
while (current_scope != NULL) {
SourceLabel* label = current_scope->LocalLookupLabel(name);
if (label != NULL) {
return label;
}
current_scope = current_scope->parent();
}
return NULL;
}
SourceLabel* LocalScope::LookupInnermostLabel(Token::Kind jump_kind) {
ASSERT((jump_kind == Token::kCONTINUE) || (jump_kind == Token::kBREAK));
LocalScope* current_scope = this;
while (current_scope != NULL) {
for (intptr_t i = 0; i < current_scope->labels_.length(); i++) {
SourceLabel* label = current_scope->labels_[i];
if ((label->kind() == SourceLabel::kWhile) ||
(label->kind() == SourceLabel::kFor) ||
(label->kind() == SourceLabel::kDoWhile) ||
((jump_kind == Token::kBREAK) &&
(label->kind() == SourceLabel::kSwitch))) {
return label;
}
}
current_scope = current_scope->parent();
}
return NULL;
}
LocalScope* LocalScope::LookupSwitchScope() {
LocalScope* current_scope = this->parent();
int this_level = this->function_level();
while (current_scope != NULL &&
current_scope->function_level() == this_level) {
for (int i = 0; i < current_scope->labels_.length(); i++) {
SourceLabel* label = current_scope->labels_[i];
if (label->kind() == SourceLabel::kSwitch) {
// This scope contains a label that is bound to a switch statement,
// so it is the scope of the a statement body.
return current_scope;
}
}
current_scope = current_scope->parent();
}
// We did not find a switch statement scope at the same function level.
return NULL;
}
SourceLabel* LocalScope::CheckUnresolvedLabels() {
for (int i = 0; i < this->labels_.length(); i++) {
SourceLabel* label = this->labels_[i];
if (label->kind() == SourceLabel::kForward) {
LocalScope* outer_switch = LookupSwitchScope();
if (outer_switch == NULL) {
return label;
} else {
outer_switch->MoveLabel(label);
}
}
}
return NULL;
}
int LocalScope::NumCapturedVariables() const {
// It is not necessary to traverse parent scopes, since we are only interested
// in the captured variables referenced in this scope. If this scope is the

View file

@ -276,71 +276,6 @@ class LocalVarDescriptorsBuilder : public ValueObject {
GrowableArray<VarDesc> vars_;
};
class NameReference : public ZoneAllocated {
public:
NameReference(TokenPosition token_pos, const String& name)
: token_pos_(token_pos), name_(name) {
ASSERT(name.IsSymbol());
}
const String& name() const { return name_; }
TokenPosition token_pos() const { return token_pos_; }
void set_token_pos(TokenPosition value) { token_pos_ = value; }
private:
TokenPosition token_pos_;
const String& name_;
};
class SourceLabel : public ZoneAllocated {
public:
enum Kind {
kFor,
kWhile,
kDoWhile,
kSwitch,
kCase,
kTry,
kCatch,
kForward,
kStatement // Any statement other than the above
};
SourceLabel(TokenPosition token_pos, const String& name, Kind kind)
: token_pos_(token_pos), name_(name), owner_(NULL), kind_(kind) {
ASSERT(name.IsSymbol());
}
static SourceLabel* New(TokenPosition token_pos, String* name, Kind kind) {
if (name != NULL) {
return new SourceLabel(token_pos, *name, kind);
} else {
return new SourceLabel(token_pos, Symbols::DefaultLabel(), kind);
}
}
TokenPosition token_pos() const { return token_pos_; }
const String& name() const { return name_; }
LocalScope* owner() const { return owner_; }
void set_owner(LocalScope* owner) { owner_ = owner; }
Kind kind() const { return kind_; }
// Returns the function level of the scope in which the label is defined.
int FunctionLevel() const;
bool IsUnresolved() { return kind_ == kForward; }
void ResolveForwardReference() { kind_ = kCase; }
private:
const TokenPosition token_pos_;
const String& name_;
LocalScope* owner_; // Local scope declaring this label.
Kind kind_;
DISALLOW_COPY_AND_ASSIGN(SourceLabel);
};
class LocalScope : public ZoneAllocated {
public:
LocalScope(LocalScope* parent, int function_level, int loop_level);
@ -403,19 +338,9 @@ class LocalScope : public ZoneAllocated {
// Returns false if a variable with the same name is already present.
bool InsertParameterAt(intptr_t pos, LocalVariable* parameter);
// Add a label to the scope. Returns false if a label with the same name
// is already present.
bool AddLabel(SourceLabel* label);
// Move an unresolved label of a switch case label to an outer switch.
void MoveLabel(SourceLabel* label);
// Lookup a variable in this scope only.
LocalVariable* LocalLookupVariable(const String& name) const;
// Lookup a label in this scope only.
SourceLabel* LocalLookupLabel(const String& name) const;
// Lookup a variable in this scope and its parents. If the variable
// is found in a parent scope and 'test_only' is not true, we insert
// aliases of the variable in the current and intermediate scopes up to
@ -423,26 +348,9 @@ class LocalScope : public ZoneAllocated {
// We mark a variable as 'captured' when applicable.
LocalVariable* LookupVariable(const String& name, bool test_only);
// Lookup a label in this scope and its parents.
SourceLabel* LookupLabel(const String& name);
// Lookup the "innermost" label that labels a for, while, do, or switch
// statement.
SourceLabel* LookupInnermostLabel(Token::Kind jump_kind);
// Lookup scope of outer switch statement at same function level.
// Returns NULL if this scope is not embedded in a switch.
LocalScope* LookupSwitchScope();
// Mark this variable as captured by this scope.
void CaptureVariable(LocalVariable* variable);
// Look for unresolved forward references to labels in this scope.
// If there are any, propagate the forward reference to the next
// outer scope of a switch statement. If there is no outer switch
// statement, return the first unresolved label found.
SourceLabel* CheckUnresolvedLabels();
// Accessing the variables in the scope.
intptr_t num_variables() const { return variables_.length(); }
LocalVariable* VariableAt(intptr_t index) const {
@ -454,12 +362,6 @@ class LocalScope : public ZoneAllocated {
// this local scope.
int NumCapturedVariables() const;
// Add a reference to the given name into this scope and the enclosing
// scopes that do not have a local variable declaration for this name
// already.
void AddReferencedName(TokenPosition token_pos, const String& name);
TokenPosition PreviousReferencePos(const String& name) const;
// Allocate both captured and non-captured variables declared in this scope
// and in its children scopes of the same function level. Allocating means
// assigning a frame slot index or a context slot index.
@ -512,8 +414,6 @@ class LocalScope : public ZoneAllocated {
void CollectLocalVariables(LocalVarDescriptorsBuilder* vars,
int16_t* scope_id);
NameReference* FindReference(const String& name) const;
static const int kUninitializedContextLevel = INT_MIN;
LocalScope* parent_;
LocalScope* child_;
@ -524,17 +424,12 @@ class LocalScope : public ZoneAllocated {
TokenPosition begin_token_pos_; // Token index of beginning of scope.
TokenPosition end_token_pos_; // Token index of end of scope.
GrowableArray<LocalVariable*> variables_;
GrowableArray<SourceLabel*> labels_;
// List of variables allocated into the context which is owned by this scope,
// and their corresponding Slots.
GrowableArray<LocalVariable*> context_variables_;
ZoneGrowableArray<const Slot*>* context_slots_;
// List of names referenced in this scope and its children that
// are not resolved to local variables.
GrowableArray<NameReference*> referenced_;
DISALLOW_COPY_AND_ASSIGN(LocalScope);
};

View file

@ -22,9 +22,6 @@ ISOLATE_UNIT_TEST_CASE(LocalScope) {
const String& c = String::ZoneHandle(Symbols::New(thread, "c"));
LocalVariable* var_c = new LocalVariable(
TokenPosition::kNoSource, TokenPosition::kNoSource, c, dynamic_type);
const String& L = String::ZoneHandle(Symbols::New(thread, "L"));
SourceLabel* label_L =
new SourceLabel(TokenPosition::kNoSource, L, SourceLabel::kFor);
LocalScope* outer_scope = new LocalScope(NULL, 0, 0);
LocalScope* inner_scope1 = new LocalScope(outer_scope, 0, 0);
@ -51,13 +48,11 @@ ISOLATE_UNIT_TEST_CASE(LocalScope) {
EXPECT(outer_scope->AddVariable(var_a));
EXPECT(inner_scope1->AddVariable(var_b));
EXPECT(inner_scope2->AddVariable(var_c));
EXPECT(inner_scope2->AddLabel(label_L));
EXPECT(!outer_scope->AddVariable(var_a));
// Check the simple layout above.
EXPECT_EQ(var_a, outer_scope->LocalLookupVariable(a));
EXPECT_EQ(var_a, inner_scope1->LookupVariable(a, true));
EXPECT_EQ(label_L, inner_scope2->LookupLabel(L));
EXPECT(outer_scope->LocalLookupVariable(b) == NULL);
EXPECT(inner_scope1->LocalLookupVariable(c) == NULL);