Line data Source code
1 : // Copyright 2018 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_TORQUE_AST_H_
6 : #define V8_TORQUE_AST_H_
7 :
8 : #include <iostream>
9 : #include <memory>
10 : #include <string>
11 : #include <vector>
12 :
13 : #include "src/base/optional.h"
14 : #include "src/torque/source-positions.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 : namespace torque {
19 :
20 : #define AST_EXPRESSION_NODE_KIND_LIST(V) \
21 : V(CallExpression) \
22 : V(CallMethodExpression) \
23 : V(IntrinsicCallExpression) \
24 : V(StructExpression) \
25 : V(LogicalOrExpression) \
26 : V(LogicalAndExpression) \
27 : V(SpreadExpression) \
28 : V(ConditionalExpression) \
29 : V(IdentifierExpression) \
30 : V(StringLiteralExpression) \
31 : V(NumberLiteralExpression) \
32 : V(FieldAccessExpression) \
33 : V(ElementAccessExpression) \
34 : V(DereferenceExpression) \
35 : V(AssignmentExpression) \
36 : V(IncrementDecrementExpression) \
37 : V(NewExpression) \
38 : V(AssumeTypeImpossibleExpression) \
39 : V(StatementExpression) \
40 : V(TryLabelExpression)
41 :
42 : #define AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
43 : V(BasicTypeExpression) \
44 : V(FunctionTypeExpression) \
45 : V(UnionTypeExpression) \
46 : V(ReferenceTypeExpression)
47 :
48 : #define AST_STATEMENT_NODE_KIND_LIST(V) \
49 : V(BlockStatement) \
50 : V(ExpressionStatement) \
51 : V(IfStatement) \
52 : V(WhileStatement) \
53 : V(ForLoopStatement) \
54 : V(ForOfLoopStatement) \
55 : V(BreakStatement) \
56 : V(ContinueStatement) \
57 : V(ReturnStatement) \
58 : V(DebugStatement) \
59 : V(AssertStatement) \
60 : V(TailCallStatement) \
61 : V(VarDeclarationStatement) \
62 : V(GotoStatement)
63 :
64 : #define AST_DECLARATION_NODE_KIND_LIST(V) \
65 : V(TypeDeclaration) \
66 : V(TypeAliasDeclaration) \
67 : V(StandardDeclaration) \
68 : V(GenericDeclaration) \
69 : V(SpecializationDeclaration) \
70 : V(ExternConstDeclaration) \
71 : V(ClassDeclaration) \
72 : V(StructDeclaration) \
73 : V(NamespaceDeclaration) \
74 : V(ConstDeclaration) \
75 : V(CppIncludeDeclaration)
76 :
77 : #define AST_CALLABLE_NODE_KIND_LIST(V) \
78 : V(TorqueMacroDeclaration) \
79 : V(TorqueBuiltinDeclaration) \
80 : V(ExternalMacroDeclaration) \
81 : V(ExternalBuiltinDeclaration) \
82 : V(ExternalRuntimeDeclaration) \
83 : V(IntrinsicDeclaration)
84 :
85 : #define AST_NODE_KIND_LIST(V) \
86 : AST_EXPRESSION_NODE_KIND_LIST(V) \
87 : AST_TYPE_EXPRESSION_NODE_KIND_LIST(V) \
88 : AST_STATEMENT_NODE_KIND_LIST(V) \
89 : AST_DECLARATION_NODE_KIND_LIST(V) \
90 : AST_CALLABLE_NODE_KIND_LIST(V) \
91 : V(Identifier) \
92 : V(LabelBlock)
93 :
94 : struct AstNode {
95 : public:
96 : enum class Kind {
97 : #define ENUM_ITEM(name) k##name,
98 : AST_NODE_KIND_LIST(ENUM_ITEM)
99 : #undef ENUM_ITEM
100 : };
101 :
102 49867 : AstNode(Kind kind, SourcePosition pos) : kind(kind), pos(pos) {}
103 49867 : virtual ~AstNode() = default;
104 :
105 : const Kind kind;
106 : SourcePosition pos;
107 : };
108 :
109 : struct AstNodeClassCheck {
110 : template <class T>
111 : static bool IsInstanceOf(AstNode* node);
112 : };
113 :
114 : // Boilerplate for most derived classes.
115 : #define DEFINE_AST_NODE_LEAF_BOILERPLATE(T) \
116 : static const Kind kKind = Kind::k##T; \
117 : static T* cast(AstNode* node) { \
118 : if (node->kind != kKind) return nullptr; \
119 : return static_cast<T*>(node); \
120 : } \
121 : static T* DynamicCast(AstNode* node) { \
122 : if (!node) return nullptr; \
123 : if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
124 : return static_cast<T*>(node); \
125 : }
126 :
127 : // Boilerplate for classes with subclasses.
128 : #define DEFINE_AST_NODE_INNER_BOILERPLATE(T) \
129 : static T* cast(AstNode* node) { \
130 : DCHECK(AstNodeClassCheck::IsInstanceOf<T>(node)); \
131 : return static_cast<T*>(node); \
132 : } \
133 : static T* DynamicCast(AstNode* node) { \
134 : if (!node) return nullptr; \
135 : if (!AstNodeClassCheck::IsInstanceOf<T>(node)) return nullptr; \
136 : return static_cast<T*>(node); \
137 : }
138 :
139 36024 : struct Expression : AstNode {
140 18012 : Expression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
141 : DEFINE_AST_NODE_INNER_BOILERPLATE(Expression)
142 : };
143 :
144 24224 : struct LocationExpression : Expression {
145 12112 : LocationExpression(Kind kind, SourcePosition pos) : Expression(kind, pos) {}
146 3 : DEFINE_AST_NODE_INNER_BOILERPLATE(LocationExpression)
147 : };
148 :
149 11934 : struct TypeExpression : AstNode {
150 5967 : TypeExpression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
151 : DEFINE_AST_NODE_INNER_BOILERPLATE(TypeExpression)
152 : };
153 :
154 3070 : struct Declaration : AstNode {
155 1535 : Declaration(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
156 : DEFINE_AST_NODE_INNER_BOILERPLATE(Declaration)
157 : };
158 :
159 12944 : struct Statement : AstNode {
160 6472 : Statement(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
161 : DEFINE_AST_NODE_INNER_BOILERPLATE(Statement)
162 : };
163 :
164 : class Namespace;
165 :
166 150 : struct NamespaceDeclaration : Declaration {
167 50 : DEFINE_AST_NODE_LEAF_BOILERPLATE(NamespaceDeclaration)
168 50 : NamespaceDeclaration(SourcePosition pos, std::string name,
169 : std::vector<Declaration*> declarations)
170 : : Declaration(kKind, pos),
171 : declarations(std::move(declarations)),
172 150 : name(name) {}
173 : std::vector<Declaration*> declarations;
174 : std::string name;
175 : };
176 :
177 18 : class Ast {
178 : public:
179 : Ast() {}
180 :
181 966 : std::vector<Declaration*>& declarations() { return declarations_; }
182 : const std::vector<Declaration*>& declarations() const {
183 : return declarations_;
184 : }
185 : template <class T>
186 49867 : T* AddNode(std::unique_ptr<T> node) {
187 : T* result = node.get();
188 99734 : nodes_.push_back(std::move(node));
189 49867 : return result;
190 : }
191 :
192 : private:
193 : std::vector<Declaration*> declarations_;
194 : std::vector<std::unique_ptr<AstNode>> nodes_;
195 : };
196 :
197 : static const char* const kThisParameterName = "this";
198 :
199 : // A Identifier is a string with a SourcePosition attached.
200 33224 : struct Identifier : AstNode {
201 : DEFINE_AST_NODE_LEAF_BOILERPLATE(Identifier)
202 : Identifier(SourcePosition pos, std::string identifier)
203 16612 : : AstNode(kKind, pos), value(std::move(identifier)) {}
204 : std::string value;
205 : };
206 :
207 42220 : struct IdentifierExpression : LocationExpression {
208 8052 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IdentifierExpression)
209 : IdentifierExpression(SourcePosition pos,
210 : std::vector<std::string> namespace_qualification,
211 : Identifier* name, std::vector<TypeExpression*> args = {})
212 : : LocationExpression(kKind, pos),
213 : namespace_qualification(std::move(namespace_qualification)),
214 : name(name),
215 21110 : generic_arguments(std::move(args)) {}
216 1784 : IdentifierExpression(SourcePosition pos, Identifier* name,
217 : std::vector<TypeExpression*> args = {})
218 1784 : : IdentifierExpression(pos, {}, name, std::move(args)) {}
219 1103 : bool IsThis() const { return name->value == kThisParameterName; }
220 : std::vector<std::string> namespace_qualification;
221 : Identifier* name;
222 : std::vector<TypeExpression*> generic_arguments;
223 : };
224 :
225 147 : struct IntrinsicCallExpression : Expression {
226 84 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicCallExpression)
227 49 : IntrinsicCallExpression(SourcePosition pos, std::string name,
228 : std::vector<TypeExpression*> generic_arguments,
229 : std::vector<Expression*> arguments)
230 : : Expression(kKind, pos),
231 : name(std::move(name)),
232 : generic_arguments(std::move(generic_arguments)),
233 98 : arguments(std::move(arguments)) {}
234 : std::string name;
235 : std::vector<TypeExpression*> generic_arguments;
236 : std::vector<Expression*> arguments;
237 : };
238 :
239 360 : struct CallMethodExpression : Expression {
240 129 : DEFINE_AST_NODE_LEAF_BOILERPLATE(CallMethodExpression)
241 : CallMethodExpression(SourcePosition pos, Expression* target,
242 : IdentifierExpression* method,
243 : std::vector<Expression*> arguments,
244 : std::vector<std::string> labels)
245 : : Expression(kKind, pos),
246 : target(target),
247 : method(method),
248 : arguments(std::move(arguments)),
249 120 : labels(std::move(labels)) {}
250 : Expression* target;
251 : IdentifierExpression* method;
252 : std::vector<Expression*> arguments;
253 : std::vector<std::string> labels;
254 : };
255 :
256 9081 : struct CallExpression : Expression {
257 3289 : DEFINE_AST_NODE_LEAF_BOILERPLATE(CallExpression)
258 : CallExpression(SourcePosition pos, IdentifierExpression* callee,
259 : std::vector<Expression*> arguments,
260 : std::vector<std::string> labels)
261 : : Expression(kKind, pos),
262 : callee(callee),
263 : arguments(std::move(arguments)),
264 3027 : labels(std::move(labels)) {}
265 : IdentifierExpression* callee;
266 : std::vector<Expression*> arguments;
267 : std::vector<std::string> labels;
268 : };
269 :
270 : struct NameAndExpression {
271 : Identifier* name;
272 : Expression* expression;
273 : };
274 :
275 42 : struct StructExpression : Expression {
276 21 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StructExpression)
277 : StructExpression(SourcePosition pos, TypeExpression* type,
278 : std::vector<NameAndExpression> initializers)
279 : : Expression(kKind, pos),
280 : type(type),
281 21 : initializers(std::move(initializers)) {}
282 : TypeExpression* type;
283 : std::vector<NameAndExpression> initializers;
284 : };
285 :
286 60 : struct LogicalOrExpression : Expression {
287 33 : DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalOrExpression)
288 : LogicalOrExpression(SourcePosition pos, Expression* left, Expression* right)
289 30 : : Expression(kKind, pos), left(left), right(right) {}
290 : Expression* left;
291 : Expression* right;
292 : };
293 :
294 92 : struct LogicalAndExpression : Expression {
295 24 : DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalAndExpression)
296 : LogicalAndExpression(SourcePosition pos, Expression* left, Expression* right)
297 46 : : Expression(kKind, pos), left(left), right(right) {}
298 : Expression* left;
299 : Expression* right;
300 : };
301 :
302 4 : struct SpreadExpression : Expression {
303 106 : DEFINE_AST_NODE_LEAF_BOILERPLATE(SpreadExpression)
304 : SpreadExpression(SourcePosition pos, Expression* spreadee)
305 2 : : Expression(kKind, pos), spreadee(spreadee) {}
306 : Expression* spreadee;
307 : };
308 :
309 198 : struct ConditionalExpression : Expression {
310 102 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ConditionalExpression)
311 : ConditionalExpression(SourcePosition pos, Expression* condition,
312 : Expression* if_true, Expression* if_false)
313 : : Expression(kKind, pos),
314 : condition(condition),
315 : if_true(if_true),
316 99 : if_false(if_false) {}
317 : Expression* condition;
318 : Expression* if_true;
319 : Expression* if_false;
320 : };
321 :
322 212 : struct StringLiteralExpression : Expression {
323 107 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StringLiteralExpression)
324 : StringLiteralExpression(SourcePosition pos, std::string literal)
325 106 : : Expression(kKind, pos), literal(std::move(literal)) {}
326 : std::string literal;
327 : };
328 :
329 1930 : struct NumberLiteralExpression : Expression {
330 939 : DEFINE_AST_NODE_LEAF_BOILERPLATE(NumberLiteralExpression)
331 : NumberLiteralExpression(SourcePosition pos, std::string name)
332 965 : : Expression(kKind, pos), number(std::move(name)) {}
333 : std::string number;
334 : };
335 :
336 538 : struct ElementAccessExpression : LocationExpression {
337 220 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ElementAccessExpression)
338 : ElementAccessExpression(SourcePosition pos, Expression* array,
339 : Expression* index)
340 269 : : LocationExpression(kKind, pos), array(array), index(index) {}
341 : Expression* array;
342 : Expression* index;
343 : };
344 :
345 2562 : struct FieldAccessExpression : LocationExpression {
346 967 : DEFINE_AST_NODE_LEAF_BOILERPLATE(FieldAccessExpression)
347 : FieldAccessExpression(SourcePosition pos, Expression* object,
348 : Identifier* field)
349 1281 : : LocationExpression(kKind, pos), object(object), field(field) {}
350 : Expression* object;
351 : Identifier* field;
352 : };
353 :
354 14 : struct DereferenceExpression : LocationExpression {
355 3 : DEFINE_AST_NODE_LEAF_BOILERPLATE(DereferenceExpression)
356 : DereferenceExpression(SourcePosition pos, Expression* reference)
357 7 : : LocationExpression(kKind, pos), reference(reference) {}
358 : Expression* reference;
359 : };
360 :
361 1452 : struct AssignmentExpression : Expression {
362 807 : DEFINE_AST_NODE_LEAF_BOILERPLATE(AssignmentExpression)
363 : AssignmentExpression(SourcePosition pos, Expression* location,
364 : Expression* value)
365 : : AssignmentExpression(pos, location, base::nullopt, value) {}
366 : AssignmentExpression(SourcePosition pos, Expression* location,
367 : base::Optional<std::string> op, Expression* value)
368 : : Expression(kKind, pos),
369 : location(location),
370 : op(std::move(op)),
371 1452 : value(value) {}
372 : Expression* location;
373 : base::Optional<std::string> op;
374 : Expression* value;
375 : };
376 :
377 : enum class IncrementDecrementOperator { kIncrement, kDecrement };
378 :
379 280 : struct IncrementDecrementExpression : Expression {
380 160 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IncrementDecrementExpression)
381 : IncrementDecrementExpression(SourcePosition pos, Expression* location,
382 : IncrementDecrementOperator op, bool postfix)
383 140 : : Expression(kKind, pos), location(location), op(op), postfix(postfix) {}
384 : Expression* location;
385 : IncrementDecrementOperator op;
386 : bool postfix;
387 : };
388 :
389 : // This expression is only used in the desugaring of typeswitch, and it allows
390 : // to bake in the static information that certain types are impossible at a
391 : // certain position in the control flow.
392 : // The result type is the type of {expression} minus the provided type.
393 72 : struct AssumeTypeImpossibleExpression : Expression {
394 42 : DEFINE_AST_NODE_LEAF_BOILERPLATE(AssumeTypeImpossibleExpression)
395 : AssumeTypeImpossibleExpression(SourcePosition pos,
396 : TypeExpression* excluded_type,
397 : Expression* expression)
398 : : Expression(kKind, pos),
399 : excluded_type(excluded_type),
400 36 : expression(expression) {}
401 : TypeExpression* excluded_type;
402 : Expression* expression;
403 : };
404 :
405 24 : struct NewExpression : Expression {
406 12 : DEFINE_AST_NODE_LEAF_BOILERPLATE(NewExpression)
407 : NewExpression(SourcePosition pos, TypeExpression* type,
408 : std::vector<NameAndExpression> initializers)
409 : : Expression(kKind, pos),
410 : type(type),
411 12 : initializers(std::move(initializers)) {}
412 : TypeExpression* type;
413 : std::vector<NameAndExpression> initializers;
414 : };
415 :
416 52202 : struct ParameterList {
417 : std::vector<Identifier*> names;
418 : std::vector<TypeExpression*> types;
419 : size_t implicit_count;
420 : bool has_varargs;
421 : std::string arguments_variable;
422 :
423 201 : static ParameterList Empty() { return ParameterList{{}, {}, 0, false, ""}; }
424 : std::vector<TypeExpression*> GetImplicitTypes() {
425 : return std::vector<TypeExpression*>(types.begin(),
426 : types.begin() + implicit_count);
427 : }
428 : std::vector<TypeExpression*> GetExplicitTypes() {
429 : return std::vector<TypeExpression*>(types.begin() + implicit_count,
430 : types.end());
431 : }
432 : };
433 :
434 23348 : struct BasicTypeExpression : TypeExpression {
435 14732 : DEFINE_AST_NODE_LEAF_BOILERPLATE(BasicTypeExpression)
436 : BasicTypeExpression(SourcePosition pos,
437 : std::vector<std::string> namespace_qualification,
438 : bool is_constexpr, std::string name)
439 : : TypeExpression(kKind, pos),
440 : namespace_qualification(std::move(namespace_qualification)),
441 : is_constexpr(is_constexpr),
442 11674 : name(std::move(name)) {}
443 : std::vector<std::string> namespace_qualification;
444 : bool is_constexpr;
445 : std::string name;
446 : };
447 :
448 22 : struct FunctionTypeExpression : TypeExpression {
449 11 : DEFINE_AST_NODE_LEAF_BOILERPLATE(FunctionTypeExpression)
450 : FunctionTypeExpression(SourcePosition pos,
451 : std::vector<TypeExpression*> parameters,
452 : TypeExpression* return_type)
453 : : TypeExpression(kKind, pos),
454 : parameters(std::move(parameters)),
455 22 : return_type(return_type) {}
456 : std::vector<TypeExpression*> parameters;
457 : TypeExpression* return_type;
458 : };
459 :
460 230 : struct UnionTypeExpression : TypeExpression {
461 120 : DEFINE_AST_NODE_LEAF_BOILERPLATE(UnionTypeExpression)
462 : UnionTypeExpression(SourcePosition pos, TypeExpression* a, TypeExpression* b)
463 115 : : TypeExpression(kKind, pos), a(a), b(b) {}
464 : TypeExpression* a;
465 : TypeExpression* b;
466 : };
467 :
468 8 : struct ReferenceTypeExpression : TypeExpression {
469 83 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ReferenceTypeExpression)
470 : ReferenceTypeExpression(SourcePosition pos, TypeExpression* referenced_type)
471 4 : : TypeExpression(kKind, pos), referenced_type(referenced_type) {}
472 : TypeExpression* referenced_type;
473 : };
474 :
475 3202 : struct ExpressionStatement : Statement {
476 1915 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExpressionStatement)
477 : ExpressionStatement(SourcePosition pos, Expression* expression)
478 1601 : : Statement(kKind, pos), expression(expression) {}
479 : Expression* expression;
480 : };
481 :
482 1010 : struct IfStatement : Statement {
483 683 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IfStatement)
484 : IfStatement(SourcePosition pos, bool is_constexpr, Expression* condition,
485 : Statement* if_true, base::Optional<Statement*> if_false)
486 : : Statement(kKind, pos),
487 : condition(condition),
488 : is_constexpr(is_constexpr),
489 : if_true(if_true),
490 505 : if_false(if_false) {}
491 : Expression* condition;
492 : bool is_constexpr;
493 : Statement* if_true;
494 : base::Optional<Statement*> if_false;
495 : };
496 :
497 78 : struct WhileStatement : Statement {
498 43 : DEFINE_AST_NODE_LEAF_BOILERPLATE(WhileStatement)
499 : WhileStatement(SourcePosition pos, Expression* condition, Statement* body)
500 39 : : Statement(kKind, pos), condition(condition), body(body) {}
501 : Expression* condition;
502 : Statement* body;
503 : };
504 :
505 1886 : struct ReturnStatement : Statement {
506 1116 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ReturnStatement)
507 : ReturnStatement(SourcePosition pos, base::Optional<Expression*> value)
508 943 : : Statement(kKind, pos), value(value) {}
509 : base::Optional<Expression*> value;
510 : };
511 :
512 246 : struct DebugStatement : Statement {
513 125 : DEFINE_AST_NODE_LEAF_BOILERPLATE(DebugStatement)
514 123 : DebugStatement(SourcePosition pos, const std::string& reason,
515 : bool never_continues)
516 : : Statement(kKind, pos),
517 : reason(reason),
518 246 : never_continues(never_continues) {}
519 : std::string reason;
520 : bool never_continues;
521 : };
522 :
523 462 : struct AssertStatement : Statement {
524 291 : DEFINE_AST_NODE_LEAF_BOILERPLATE(AssertStatement)
525 : AssertStatement(SourcePosition pos, bool debug_only, Expression* expression,
526 : std::string source)
527 : : Statement(kKind, pos),
528 : debug_only(debug_only),
529 : expression(expression),
530 231 : source(std::move(source)) {}
531 : bool debug_only;
532 : Expression* expression;
533 : std::string source;
534 : };
535 :
536 4 : struct TailCallStatement : Statement {
537 2 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TailCallStatement)
538 : TailCallStatement(SourcePosition pos, CallExpression* call)
539 2 : : Statement(kKind, pos), call(call) {}
540 : CallExpression* call;
541 : };
542 :
543 2474 : struct VarDeclarationStatement : Statement {
544 4289 : DEFINE_AST_NODE_LEAF_BOILERPLATE(VarDeclarationStatement)
545 : VarDeclarationStatement(
546 : SourcePosition pos, bool const_qualified, Identifier* name,
547 : base::Optional<TypeExpression*> type,
548 : base::Optional<Expression*> initializer = base::nullopt)
549 : : Statement(kKind, pos),
550 : const_qualified(const_qualified),
551 : name(name),
552 : type(type),
553 1237 : initializer(initializer) {}
554 : bool const_qualified;
555 : Identifier* name;
556 : base::Optional<TypeExpression*> type;
557 : base::Optional<Expression*> initializer;
558 : };
559 :
560 54 : struct BreakStatement : Statement {
561 27 : DEFINE_AST_NODE_LEAF_BOILERPLATE(BreakStatement)
562 27 : explicit BreakStatement(SourcePosition pos) : Statement(kKind, pos) {}
563 : };
564 :
565 28 : struct ContinueStatement : Statement {
566 17 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ContinueStatement)
567 14 : explicit ContinueStatement(SourcePosition pos) : Statement(kKind, pos) {}
568 : };
569 :
570 597 : struct GotoStatement : Statement {
571 237 : DEFINE_AST_NODE_LEAF_BOILERPLATE(GotoStatement)
572 199 : GotoStatement(SourcePosition pos, std::string label,
573 : const std::vector<Expression*>& arguments)
574 : : Statement(kKind, pos),
575 : label(std::move(label)),
576 398 : arguments(std::move(arguments)) {}
577 : std::string label;
578 : std::vector<Expression*> arguments;
579 : };
580 :
581 114 : struct ForLoopStatement : Statement {
582 61 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ForLoopStatement)
583 : ForLoopStatement(SourcePosition pos, base::Optional<Statement*> declaration,
584 : base::Optional<Expression*> test,
585 : base::Optional<Statement*> action, Statement* body)
586 : : Statement(kKind, pos),
587 : var_declaration(),
588 : test(std::move(test)),
589 : action(std::move(action)),
590 114 : body(std::move(body)) {
591 57 : if (declaration)
592 : var_declaration = VarDeclarationStatement::cast(*declaration);
593 : }
594 : base::Optional<VarDeclarationStatement*> var_declaration;
595 : base::Optional<Expression*> test;
596 : base::Optional<Statement*> action;
597 : Statement* body;
598 : };
599 :
600 : struct RangeExpression {
601 : base::Optional<Expression*> begin;
602 : base::Optional<Expression*> end;
603 : };
604 :
605 0 : struct ForOfLoopStatement : Statement {
606 0 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ForOfLoopStatement)
607 : ForOfLoopStatement(SourcePosition pos, Statement* decl, Expression* iterable,
608 : base::Optional<RangeExpression> range, Statement* body)
609 : : Statement(kKind, pos),
610 : var_declaration(VarDeclarationStatement::cast(decl)),
611 : iterable(iterable),
612 0 : body(body) {
613 0 : if (range) {
614 : begin = range->begin;
615 : end = range->end;
616 : }
617 : }
618 : VarDeclarationStatement* var_declaration;
619 : Expression* iterable;
620 : base::Optional<Expression*> begin;
621 : base::Optional<Expression*> end;
622 : Statement* body;
623 : };
624 :
625 1029 : struct LabelBlock : AstNode {
626 : DEFINE_AST_NODE_LEAF_BOILERPLATE(LabelBlock)
627 343 : LabelBlock(SourcePosition pos, std::string label,
628 : const ParameterList& parameters, Statement* body)
629 : : AstNode(kKind, pos),
630 : label(std::move(label)),
631 : parameters(parameters),
632 686 : body(std::move(body)) {}
633 : std::string label;
634 : ParameterList parameters;
635 : Statement* body;
636 : };
637 :
638 356 : struct StatementExpression : Expression {
639 189 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StatementExpression)
640 : StatementExpression(SourcePosition pos, Statement* statement)
641 178 : : Expression(kKind, pos), statement(statement) {}
642 : Statement* statement;
643 : };
644 :
645 686 : struct TryLabelExpression : Expression {
646 356 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TryLabelExpression)
647 : TryLabelExpression(SourcePosition pos, bool catch_exceptions,
648 : Expression* try_expression, LabelBlock* label_block)
649 : : Expression(kKind, pos),
650 : catch_exceptions(catch_exceptions),
651 : try_expression(try_expression),
652 343 : label_block(label_block) {}
653 : bool catch_exceptions;
654 : Expression* try_expression;
655 : LabelBlock* label_block;
656 : };
657 :
658 2988 : struct BlockStatement : Statement {
659 7088 : DEFINE_AST_NODE_LEAF_BOILERPLATE(BlockStatement)
660 : explicit BlockStatement(SourcePosition pos, bool deferred = false,
661 : std::vector<Statement*> statements = {})
662 : : Statement(kKind, pos),
663 : deferred(deferred),
664 1494 : statements(std::move(statements)) {}
665 : bool deferred;
666 : std::vector<Statement*> statements;
667 : };
668 :
669 282 : struct TypeDeclaration : Declaration {
670 94 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeDeclaration)
671 94 : TypeDeclaration(SourcePosition pos, Identifier* name, bool transient,
672 : base::Optional<Identifier*> extends,
673 : base::Optional<std::string> generates,
674 : base::Optional<std::string> constexpr_generates)
675 : : Declaration(kKind, pos),
676 : name(name),
677 : transient(transient),
678 : extends(extends),
679 : generates(std::move(generates)),
680 188 : constexpr_generates(std::move(constexpr_generates)) {}
681 : Identifier* name;
682 : bool transient;
683 : base::Optional<Identifier*> extends;
684 : base::Optional<std::string> generates;
685 : base::Optional<std::string> constexpr_generates;
686 : };
687 :
688 38 : struct TypeAliasDeclaration : Declaration {
689 19 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeAliasDeclaration)
690 : TypeAliasDeclaration(SourcePosition pos, Identifier* name,
691 : TypeExpression* type)
692 19 : : Declaration(kKind, pos), name(name), type(type) {}
693 : Identifier* name;
694 : TypeExpression* type;
695 : };
696 :
697 : struct NameAndTypeExpression {
698 : Identifier* name;
699 : TypeExpression* type;
700 : };
701 :
702 : struct StructFieldExpression {
703 : NameAndTypeExpression name_and_type;
704 : bool const_qualified;
705 : };
706 :
707 5959 : struct ClassFieldExpression {
708 : NameAndTypeExpression name_and_type;
709 : base::Optional<std::string> index;
710 : bool weak;
711 : bool const_qualified;
712 : };
713 :
714 2762 : struct LabelAndTypes {
715 : std::string name;
716 : std::vector<TypeExpression*> types;
717 : };
718 :
719 : typedef std::vector<LabelAndTypes> LabelAndTypesVector;
720 :
721 1067 : struct CallableNodeSignature {
722 : ParameterList parameters;
723 : TypeExpression* return_type;
724 : LabelAndTypesVector labels;
725 : };
726 :
727 1852 : struct CallableNode : AstNode {
728 926 : CallableNode(AstNode::Kind kind, SourcePosition pos, bool transitioning,
729 : std::string name, ParameterList parameters,
730 : TypeExpression* return_type, const LabelAndTypesVector& labels)
731 : : AstNode(kind, pos),
732 : transitioning(transitioning),
733 : name(std::move(name)),
734 2778 : signature(new CallableNodeSignature{parameters, return_type, labels}) {}
735 : DEFINE_AST_NODE_INNER_BOILERPLATE(CallableNode)
736 : bool transitioning;
737 : std::string name;
738 : std::unique_ptr<CallableNodeSignature> signature;
739 : };
740 :
741 1490 : struct MacroDeclaration : CallableNode {
742 328 : DEFINE_AST_NODE_INNER_BOILERPLATE(MacroDeclaration)
743 745 : MacroDeclaration(AstNode::Kind kind, SourcePosition pos, bool transitioning,
744 : std::string name, base::Optional<std::string> op,
745 : ParameterList parameters, TypeExpression* return_type,
746 : const LabelAndTypesVector& labels)
747 : : CallableNode(kind, pos, transitioning, std::move(name),
748 : std::move(parameters), return_type, labels),
749 2980 : op(std::move(op)) {}
750 : base::Optional<std::string> op;
751 : };
752 :
753 1284 : struct ExternalMacroDeclaration : MacroDeclaration {
754 428 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalMacroDeclaration)
755 428 : ExternalMacroDeclaration(SourcePosition pos, bool transitioning,
756 : std::string external_assembler_name,
757 : std::string name, base::Optional<std::string> op,
758 : ParameterList parameters,
759 : TypeExpression* return_type,
760 : const LabelAndTypesVector& labels)
761 : : MacroDeclaration(kKind, pos, transitioning, std::move(name),
762 : std::move(op), std::move(parameters), return_type,
763 : labels),
764 2140 : external_assembler_name(std::move(external_assembler_name)) {}
765 : std::string external_assembler_name;
766 : };
767 :
768 6 : struct IntrinsicDeclaration : CallableNode {
769 200 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicDeclaration)
770 6 : IntrinsicDeclaration(SourcePosition pos, std::string name,
771 : ParameterList parameters, TypeExpression* return_type)
772 : : CallableNode(kKind, pos, false, std::move(name), std::move(parameters),
773 18 : return_type, {}) {}
774 : };
775 :
776 317 : struct TorqueMacroDeclaration : MacroDeclaration {
777 285 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueMacroDeclaration)
778 317 : TorqueMacroDeclaration(SourcePosition pos, bool transitioning,
779 : std::string name, base::Optional<std::string> op,
780 : ParameterList parameters, TypeExpression* return_type,
781 : const LabelAndTypesVector& labels)
782 : : MacroDeclaration(kKind, pos, transitioning, std::move(name),
783 : std::move(op), std::move(parameters), return_type,
784 1268 : labels) {}
785 : };
786 :
787 157 : struct BuiltinDeclaration : CallableNode {
788 : DEFINE_AST_NODE_INNER_BOILERPLATE(BuiltinDeclaration)
789 157 : BuiltinDeclaration(AstNode::Kind kind, SourcePosition pos,
790 : bool javascript_linkage, bool transitioning,
791 : std::string name, ParameterList parameters,
792 : TypeExpression* return_type)
793 : : CallableNode(kind, pos, transitioning, std::move(name),
794 : std::move(parameters), return_type, {}),
795 471 : javascript_linkage(javascript_linkage) {}
796 : bool javascript_linkage;
797 : };
798 :
799 38 : struct ExternalBuiltinDeclaration : BuiltinDeclaration {
800 19 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalBuiltinDeclaration)
801 19 : ExternalBuiltinDeclaration(SourcePosition pos, bool transitioning,
802 : bool javascript_linkage, std::string name,
803 : ParameterList parameters,
804 : TypeExpression* return_type)
805 : : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning,
806 : std::move(name), std::move(parameters),
807 57 : return_type) {}
808 : };
809 :
810 276 : struct TorqueBuiltinDeclaration : BuiltinDeclaration {
811 130 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueBuiltinDeclaration)
812 138 : TorqueBuiltinDeclaration(SourcePosition pos, bool transitioning,
813 : bool javascript_linkage, std::string name,
814 : ParameterList parameters,
815 : TypeExpression* return_type)
816 : : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning,
817 : std::move(name), std::move(parameters),
818 414 : return_type) {}
819 : };
820 :
821 18 : struct ExternalRuntimeDeclaration : CallableNode {
822 18 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalRuntimeDeclaration)
823 18 : ExternalRuntimeDeclaration(SourcePosition pos, bool transitioning,
824 : std::string name, ParameterList parameters,
825 : TypeExpression* return_type)
826 : : CallableNode(kKind, pos, transitioning, name, parameters, return_type,
827 54 : {}) {}
828 : };
829 :
830 66 : struct ConstDeclaration : Declaration {
831 33 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ConstDeclaration)
832 : ConstDeclaration(SourcePosition pos, Identifier* name, TypeExpression* type,
833 : Expression* expression)
834 : : Declaration(kKind, pos),
835 : name(name),
836 : type(type),
837 33 : expression(expression) {}
838 : Identifier* name;
839 : TypeExpression* type;
840 : Expression* expression;
841 : };
842 :
843 1760 : struct StandardDeclaration : Declaration {
844 880 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StandardDeclaration)
845 : StandardDeclaration(SourcePosition pos, CallableNode* callable,
846 : base::Optional<Statement*> body)
847 880 : : Declaration(kKind, pos), callable(callable), body(body) {}
848 : CallableNode* callable;
849 : base::Optional<Statement*> body;
850 : };
851 :
852 92 : struct GenericDeclaration : Declaration {
853 46 : DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericDeclaration)
854 : GenericDeclaration(SourcePosition pos, CallableNode* callable,
855 : std::vector<Identifier*> generic_parameters,
856 : base::Optional<Statement*> body = base::nullopt)
857 : : Declaration(kKind, pos),
858 : callable(callable),
859 : generic_parameters(std::move(generic_parameters)),
860 92 : body(body) {}
861 : CallableNode* callable;
862 : std::vector<Identifier*> generic_parameters;
863 : base::Optional<Statement*> body;
864 : };
865 :
866 423 : struct SpecializationDeclaration : Declaration {
867 141 : DEFINE_AST_NODE_LEAF_BOILERPLATE(SpecializationDeclaration)
868 141 : SpecializationDeclaration(SourcePosition pos, std::string name,
869 : std::vector<TypeExpression*> generic_parameters,
870 : ParameterList parameters,
871 : TypeExpression* return_type,
872 : LabelAndTypesVector labels, Statement* b)
873 : : Declaration(kKind, pos),
874 : name(std::move(name)),
875 : external(false),
876 : generic_parameters(std::move(generic_parameters)),
877 : signature(new CallableNodeSignature{std::move(parameters), return_type,
878 : std::move(labels)}),
879 564 : body(b) {}
880 : std::string name;
881 : bool external;
882 : std::vector<TypeExpression*> generic_parameters;
883 : std::unique_ptr<CallableNodeSignature> signature;
884 : Statement* body;
885 : };
886 :
887 194 : struct ExternConstDeclaration : Declaration {
888 97 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternConstDeclaration)
889 : ExternConstDeclaration(SourcePosition pos, Identifier* name,
890 : TypeExpression* type, std::string literal)
891 : : Declaration(kKind, pos),
892 : name(name),
893 : type(type),
894 97 : literal(std::move(literal)) {}
895 : Identifier* name;
896 : TypeExpression* type;
897 : std::string literal;
898 : };
899 :
900 54 : struct StructDeclaration : Declaration {
901 18 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StructDeclaration)
902 : StructDeclaration(SourcePosition pos, Identifier* name,
903 : std::vector<Declaration*> methods,
904 : std::vector<StructFieldExpression> fields)
905 : : Declaration(kKind, pos),
906 : name(name),
907 : methods(std::move(methods)),
908 18 : fields(std::move(fields)) {}
909 : Identifier* name;
910 : std::vector<Declaration*> methods;
911 : std::vector<StructFieldExpression> fields;
912 : };
913 :
914 363 : struct ClassDeclaration : Declaration {
915 121 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration)
916 121 : ClassDeclaration(SourcePosition pos, Identifier* name, bool is_extern,
917 : bool generate_print, bool transient,
918 : base::Optional<std::string> super,
919 : base::Optional<std::string> generates,
920 : std::vector<Declaration*> methods,
921 : std::vector<ClassFieldExpression> fields)
922 : : Declaration(kKind, pos),
923 : name(name),
924 : is_extern(is_extern),
925 : generate_print(generate_print),
926 : transient(transient),
927 : super(std::move(super)),
928 : generates(std::move(generates)),
929 : methods(std::move(methods)),
930 242 : fields(std::move(fields)) {}
931 : Identifier* name;
932 : bool is_extern;
933 : bool generate_print;
934 : bool transient;
935 : base::Optional<std::string> super;
936 : base::Optional<std::string> generates;
937 : std::vector<Declaration*> methods;
938 : std::vector<ClassFieldExpression> fields;
939 : };
940 :
941 72 : struct CppIncludeDeclaration : Declaration {
942 36 : DEFINE_AST_NODE_LEAF_BOILERPLATE(CppIncludeDeclaration)
943 : CppIncludeDeclaration(SourcePosition pos, std::string include_path)
944 36 : : Declaration(kKind, pos), include_path(std::move(include_path)) {}
945 : std::string include_path;
946 : };
947 :
948 : #define ENUM_ITEM(name) \
949 : case AstNode::Kind::k##name: \
950 : return std::is_base_of<T, name>::value; \
951 : break;
952 :
953 : template <class T>
954 25932 : bool AstNodeClassCheck::IsInstanceOf(AstNode* node) {
955 25932 : switch (node->kind) {
956 18310 : AST_NODE_KIND_LIST(ENUM_ITEM)
957 : default:
958 0 : UNIMPLEMENTED();
959 : }
960 : return true;
961 : }
962 :
963 : #undef ENUM_ITEM
964 :
965 : inline bool IsDeferred(Statement* stmt) {
966 1047 : if (auto* block = BlockStatement::DynamicCast(stmt)) {
967 617 : return block->deferred;
968 : }
969 : return false;
970 : }
971 :
972 : DECLARE_CONTEXTUAL_VARIABLE(CurrentAst, Ast);
973 :
974 : template <class T, class... Args>
975 49867 : T* MakeNode(Args... args) {
976 : return CurrentAst::Get().AddNode(std::unique_ptr<T>(
977 219822 : new T(CurrentSourcePosition::Get(), std::move(args)...)));
978 : }
979 :
980 : } // namespace torque
981 : } // namespace internal
982 : } // namespace v8
983 :
984 : #endif // V8_TORQUE_AST_H_
|