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