Skip to content

Commit 185eaec

Browse files
tebbiCommit Bot
authored andcommitted
[torque] declare local value and label bindings in the ImplementationVisitor
While this is mostly a mechanical change to enable re-visiting macros for inlining, it has a few user-facing effects: - Labels and (variables, parameters, local constants) are handled separately, so they do not shadow each other. - A local variable or constant is not bound in its initializer. This allows code like: const x = 5; { const x = x + 1; } Bug: v8:7793 Change-Id: I968e1f93d92689737362c803342a797d312e95cd Reviewed-on: https://chromium-review.googlesource.com/c/1276628 Commit-Queue: Tobias Tebbi <tebbi@chromium.org> Reviewed-by: Daniel Clifford <danno@chromium.org> Cr-Commit-Position: refs/heads/master@{#56649}
1 parent fa3b5d7 commit 185eaec

16 files changed

Lines changed: 601 additions & 824 deletions

src/torque/ast.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace torque {
5353
V(AssertStatement) \
5454
V(TailCallStatement) \
5555
V(VarDeclarationStatement) \
56-
V(GotoStatement) \
56+
V(GotoStatement)
5757

5858
#define AST_DECLARATION_NODE_KIND_LIST(V) \
5959
V(TypeDeclaration) \
@@ -510,7 +510,7 @@ struct ForLoopStatement : Statement {
510510
DEFINE_AST_NODE_LEAF_BOILERPLATE(ForLoopStatement)
511511
ForLoopStatement(SourcePosition pos, base::Optional<Statement*> declaration,
512512
base::Optional<Expression*> test,
513-
base::Optional<Expression*> action, Statement* body)
513+
base::Optional<Statement*> action, Statement* body)
514514
: Statement(kKind, pos),
515515
var_declaration(),
516516
test(std::move(test)),
@@ -521,7 +521,7 @@ struct ForLoopStatement : Statement {
521521
}
522522
base::Optional<VarDeclarationStatement*> var_declaration;
523523
base::Optional<Expression*> test;
524-
base::Optional<Expression*> action;
524+
base::Optional<Statement*> action;
525525
Statement* body;
526526
};
527527

@@ -815,6 +815,13 @@ bool AstNodeClassCheck::IsInstanceOf(AstNode* node) {
815815

816816
#undef ENUM_ITEM
817817

818+
inline bool IsDeferred(Statement* stmt) {
819+
if (auto* block = BlockStatement::DynamicCast(stmt)) {
820+
return block->deferred;
821+
}
822+
return false;
823+
}
824+
818825
} // namespace torque
819826
} // namespace internal
820827
} // namespace v8

src/torque/declarable.cc

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ std::ostream& operator<<(std::ostream& os, const Callable& m) {
3232
return os;
3333
}
3434

35-
std::ostream& operator<<(std::ostream& os, const Variable& v) {
36-
os << "variable " << v.name() << ": " << *v.type();
37-
return os;
38-
}
39-
4035
std::ostream& operator<<(std::ostream& os, const Builtin& b) {
4136
os << "builtin " << *b.signature().return_type << " " << b.name()
4237
<< b.signature().parameter_types;
@@ -49,33 +44,6 @@ std::ostream& operator<<(std::ostream& os, const RuntimeFunction& b) {
4944
return os;
5045
}
5146

52-
void PrintLabel(std::ostream& os, const Label& l, bool with_names) {
53-
os << l.name();
54-
if (l.GetParameterCount() != 0) {
55-
os << "(";
56-
if (with_names) {
57-
PrintCommaSeparatedList(os, l.GetParameters(),
58-
[](Variable* v) -> std::string {
59-
std::stringstream stream;
60-
stream << v->name();
61-
stream << ": ";
62-
stream << *(v->type());
63-
return stream.str();
64-
});
65-
} else {
66-
PrintCommaSeparatedList(
67-
os, l.GetParameters(),
68-
[](Variable* v) -> const Type& { return *(v->type()); });
69-
}
70-
os << ")";
71-
}
72-
}
73-
74-
std::ostream& operator<<(std::ostream& os, const Label& l) {
75-
PrintLabel(os, l, true);
76-
return os;
77-
}
78-
7947
std::ostream& operator<<(std::ostream& os, const Generic& g) {
8048
os << "generic " << g.name() << "<";
8149
PrintCommaSeparatedList(os, g.declaration()->generic_parameters);
@@ -84,8 +52,6 @@ std::ostream& operator<<(std::ostream& os, const Generic& g) {
8452
return os;
8553
}
8654

87-
size_t Label::next_id_ = 0;
88-
8955
} // namespace torque
9056
} // namespace internal
9157
} // namespace v8

src/torque/declarable.h

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ class Declarable {
2727
public:
2828
virtual ~Declarable() = default;
2929
enum Kind {
30-
kVariable,
31-
kParameter,
3230
kMacro,
3331
kMacroList,
3432
kBuiltin,
3533
kRuntimeFunction,
3634
kGeneric,
3735
kGenericList,
3836
kTypeAlias,
39-
kLabel,
4037
kExternConstant,
4138
kModuleConstant
4239
};
@@ -46,17 +43,11 @@ class Declarable {
4643
bool IsRuntimeFunction() const { return kind() == kRuntimeFunction; }
4744
bool IsGeneric() const { return kind() == kGeneric; }
4845
bool IsTypeAlias() const { return kind() == kTypeAlias; }
49-
bool IsParameter() const { return kind() == kParameter; }
50-
bool IsLabel() const { return kind() == kLabel; }
51-
bool IsVariable() const { return kind() == kVariable; }
5246
bool IsMacroList() const { return kind() == kMacroList; }
5347
bool IsGenericList() const { return kind() == kGenericList; }
5448
bool IsExternConstant() const { return kind() == kExternConstant; }
5549
bool IsModuleConstant() const { return kind() == kModuleConstant; }
56-
bool IsValue() const {
57-
return IsVariable() || IsExternConstant() || IsParameter() ||
58-
IsModuleConstant();
59-
}
50+
bool IsValue() const { return IsExternConstant() || IsModuleConstant(); }
6051
virtual const char* type_name() const { return "<<unknown>>"; }
6152

6253
protected:
@@ -110,22 +101,6 @@ class Value : public Declarable {
110101
base::Optional<VisitResult> value_;
111102
};
112103

113-
class Parameter : public Value {
114-
public:
115-
DECLARE_DECLARABLE_BOILERPLATE(Parameter, parameter);
116-
117-
const std::string& external_name() const { return external_name_; }
118-
119-
private:
120-
friend class Declarations;
121-
Parameter(const std::string& name, std::string external_name,
122-
const Type* type)
123-
: Value(Declarable::kParameter, type, name),
124-
external_name_(external_name) {}
125-
126-
std::string external_name_;
127-
};
128-
129104
class ModuleConstant : public Value {
130105
public:
131106
DECLARE_DECLARABLE_BOILERPLATE(ModuleConstant, constant);
@@ -141,75 +116,6 @@ class ModuleConstant : public Value {
141116
std::string constant_name_;
142117
};
143118

144-
class Variable : public Value {
145-
public:
146-
DECLARE_DECLARABLE_BOILERPLATE(Variable, variable);
147-
bool IsConst() const override { return const_; }
148-
void Define() {
149-
if (defined_ && IsConst()) {
150-
ReportError("Cannot re-define a const-bound variable.");
151-
}
152-
defined_ = true;
153-
}
154-
bool IsDefined() const { return defined_; }
155-
156-
private:
157-
friend class Declarations;
158-
Variable(std::string name, const Type* type, bool is_const)
159-
: Value(Declarable::kVariable, type, name),
160-
defined_(false),
161-
const_(is_const) {
162-
DCHECK_IMPLIES(type->IsConstexpr(), IsConst());
163-
}
164-
165-
std::string value_;
166-
bool defined_;
167-
bool const_;
168-
};
169-
170-
class Label : public Declarable {
171-
public:
172-
void AddVariable(Variable* var) { parameters_.push_back(var); }
173-
Block* block() const { return *block_; }
174-
void set_block(Block* block) {
175-
DCHECK(!block_);
176-
block_ = block;
177-
}
178-
const std::string& external_label_name() const {
179-
return *external_label_name_;
180-
}
181-
const std::string& name() const { return name_; }
182-
void set_external_label_name(std::string external_label_name) {
183-
DCHECK(!block_);
184-
DCHECK(!external_label_name_);
185-
external_label_name_ = std::move(external_label_name);
186-
}
187-
Variable* GetParameter(size_t i) const { return parameters_[i]; }
188-
size_t GetParameterCount() const { return parameters_.size(); }
189-
const std::vector<Variable*>& GetParameters() const { return parameters_; }
190-
191-
DECLARE_DECLARABLE_BOILERPLATE(Label, label);
192-
void MarkUsed() { used_ = true; }
193-
bool IsUsed() const { return used_; }
194-
bool IsDeferred() const { return deferred_; }
195-
196-
private:
197-
friend class Declarations;
198-
explicit Label(std::string name, bool deferred = false)
199-
: Declarable(Declarable::kLabel),
200-
name_(std::move(name)),
201-
used_(false),
202-
deferred_(deferred) {}
203-
204-
std::string name_;
205-
base::Optional<Block*> block_;
206-
base::Optional<std::string> external_label_name_;
207-
std::vector<Variable*> parameters_;
208-
static size_t next_id_;
209-
bool used_;
210-
bool deferred_;
211-
};
212-
213119
class ExternConstant : public Value {
214120
public:
215121
DECLARE_DECLARABLE_BOILERPLATE(ExternConstant, constant);
@@ -380,12 +286,8 @@ class TypeAlias : public Declarable {
380286
const Type* type_;
381287
};
382288

383-
void PrintLabel(std::ostream& os, const Label& l, bool with_names);
384-
385289
std::ostream& operator<<(std::ostream& os, const Callable& m);
386-
std::ostream& operator<<(std::ostream& os, const Variable& v);
387290
std::ostream& operator<<(std::ostream& os, const Builtin& b);
388-
std::ostream& operator<<(std::ostream& os, const Label& l);
389291
std::ostream& operator<<(std::ostream& os, const RuntimeFunction& b);
390292
std::ostream& operator<<(std::ostream& os, const Generic& g);
391293

0 commit comments

Comments
 (0)