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 25931 : AstNode(Kind kind, SourcePosition pos) : kind(kind), pos(pos) {}
101 25931 : 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 14068 : struct Expression : AstNode {
138 14068 : Expression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
139 : DEFINE_AST_NODE_INNER_BOILERPLATE(Expression)
140 : };
141 :
142 9308 : struct LocationExpression : Expression {
143 9308 : LocationExpression(Kind kind, SourcePosition pos) : Expression(kind, pos) {}
144 : DEFINE_AST_NODE_INNER_BOILERPLATE(LocationExpression)
145 : };
146 :
147 4773 : struct TypeExpression : AstNode {
148 4773 : TypeExpression(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
149 : DEFINE_AST_NODE_INNER_BOILERPLATE(TypeExpression)
150 : };
151 :
152 1199 : struct Declaration : AstNode {
153 1199 : Declaration(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
154 : DEFINE_AST_NODE_INNER_BOILERPLATE(Declaration)
155 : };
156 :
157 4896 : struct Statement : AstNode {
158 4896 : Statement(Kind kind, SourcePosition pos) : AstNode(kind, pos) {}
159 : DEFINE_AST_NODE_INNER_BOILERPLATE(Statement)
160 : };
161 :
162 : class Namespace;
163 :
164 78 : struct NamespaceDeclaration : Declaration {
165 26 : DEFINE_AST_NODE_LEAF_BOILERPLATE(NamespaceDeclaration)
166 26 : NamespaceDeclaration(SourcePosition pos, std::string name,
167 : std::vector<Declaration*> declarations)
168 : : Declaration(kKind, pos),
169 : declarations(std::move(declarations)),
170 52 : 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 25931 : T* AddNode(std::unique_ptr<T> node) {
185 : T* result = node.get();
186 51862 : nodes_.push_back(std::move(node));
187 25931 : 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 33852 : struct IdentifierExpression : LocationExpression {
198 7085 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IdentifierExpression)
199 8463 : 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 16926 : generic_arguments(std::move(args)) {}
206 1295 : IdentifierExpression(SourcePosition pos, std::string name,
207 : std::vector<TypeExpression*> args = {})
208 3885 : : IdentifierExpression(pos, {}, std::move(name), std::move(args)) {}
209 984 : 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 116 : struct LoadObjectFieldExpression : Expression {
216 58 : 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 58 : field_name(std::move(field_name)) {}
222 : Expression* base;
223 : std::string field_name;
224 : };
225 :
226 116 : struct StoreObjectFieldExpression : Expression {
227 58 : 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 116 : value(std::move(value)) {}
234 : Expression* base;
235 : std::string field_name;
236 : Expression* value;
237 : size_t offset;
238 : };
239 :
240 138 : struct IntrinsicCallExpression : Expression {
241 78 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicCallExpression)
242 46 : 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 92 : arguments(std::move(arguments)) {}
249 : std::string name;
250 : std::vector<TypeExpression*> generic_arguments;
251 : std::vector<Expression*> arguments;
252 : };
253 :
254 189 : struct CallMethodExpression : Expression {
255 155 : 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 63 : labels(std::move(labels)) {}
265 : Expression* target;
266 : IdentifierExpression* method;
267 : std::vector<Expression*> arguments;
268 : std::vector<std::string> labels;
269 : };
270 :
271 7512 : struct CallExpression : Expression {
272 2792 : 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 2504 : labels(std::move(labels)) {}
280 : IdentifierExpression* callee;
281 : std::vector<Expression*> arguments;
282 : std::vector<std::string> labels;
283 : };
284 :
285 84 : struct StructExpression : Expression {
286 31 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StructExpression)
287 21 : 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 42 : expressions(std::move(expressions)) {}
294 : std::vector<std::string> namespace_qualification;
295 : std::string name;
296 : std::vector<Expression*> expressions;
297 : };
298 :
299 48 : struct LogicalOrExpression : Expression {
300 29 : DEFINE_AST_NODE_LEAF_BOILERPLATE(LogicalOrExpression)
301 : LogicalOrExpression(SourcePosition pos, Expression* left, Expression* right)
302 24 : : Expression(kKind, pos), left(left), right(right) {}
303 : Expression* left;
304 : Expression* right;
305 : };
306 :
307 92 : struct LogicalAndExpression : Expression {
308 28 : 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 176 : struct ConditionalExpression : Expression {
316 96 : 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 88 : if_false(if_false) {}
323 : Expression* condition;
324 : Expression* if_true;
325 : Expression* if_false;
326 : };
327 :
328 90 : struct StringLiteralExpression : Expression {
329 46 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StringLiteralExpression)
330 : StringLiteralExpression(SourcePosition pos, std::string literal)
331 45 : : Expression(kKind, pos), literal(std::move(literal)) {}
332 : std::string literal;
333 : };
334 :
335 1688 : struct NumberLiteralExpression : Expression {
336 829 : DEFINE_AST_NODE_LEAF_BOILERPLATE(NumberLiteralExpression)
337 : NumberLiteralExpression(SourcePosition pos, std::string name)
338 844 : : Expression(kKind, pos), number(std::move(name)) {}
339 : std::string number;
340 : };
341 :
342 418 : struct ElementAccessExpression : LocationExpression {
343 165 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ElementAccessExpression)
344 : ElementAccessExpression(SourcePosition pos, Expression* array,
345 : Expression* index)
346 209 : : LocationExpression(kKind, pos), array(array), index(index) {}
347 : Expression* array;
348 : Expression* index;
349 : };
350 :
351 1272 : struct FieldAccessExpression : LocationExpression {
352 520 : DEFINE_AST_NODE_LEAF_BOILERPLATE(FieldAccessExpression)
353 : FieldAccessExpression(SourcePosition pos, Expression* object,
354 : std::string field)
355 : : LocationExpression(kKind, pos),
356 : object(object),
357 636 : field(std::move(field)) {}
358 : Expression* object;
359 : std::string field;
360 : };
361 :
362 1380 : struct AssignmentExpression : Expression {
363 792 : 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 920 : value(value) {}
370 : LocationExpression* location;
371 : base::Optional<std::string> op;
372 : Expression* value;
373 : };
374 :
375 : enum class IncrementDecrementOperator { kIncrement, kDecrement };
376 :
377 234 : struct IncrementDecrementExpression : Expression {
378 142 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IncrementDecrementExpression)
379 : IncrementDecrementExpression(SourcePosition pos, LocationExpression* location,
380 : IncrementDecrementOperator op, bool postfix)
381 117 : : 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 68 : struct AssumeTypeImpossibleExpression : Expression {
392 48 : 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 34 : expression(expression) {}
399 : TypeExpression* excluded_type;
400 : Expression* expression;
401 : };
402 :
403 16 : struct NewExpression : Expression {
404 12 : DEFINE_AST_NODE_LEAF_BOILERPLATE(NewExpression)
405 : NewExpression(SourcePosition pos, TypeExpression* type,
406 : std::vector<Expression*> parameters)
407 8 : : Expression(kKind, pos), type(type), parameters(parameters) {}
408 : TypeExpression* type;
409 : std::vector<Expression*> parameters;
410 : };
411 :
412 41812 : 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 124 : 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 18808 : struct BasicTypeExpression : TypeExpression {
431 12689 : 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 9404 : 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 120 : struct UnionTypeExpression : TypeExpression {
457 65 : DEFINE_AST_NODE_LEAF_BOILERPLATE(UnionTypeExpression)
458 : UnionTypeExpression(SourcePosition pos, TypeExpression* a, TypeExpression* b)
459 60 : : TypeExpression(kKind, pos), a(a), b(b) {}
460 : TypeExpression* a;
461 : TypeExpression* b;
462 : };
463 :
464 2406 : struct ExpressionStatement : Statement {
465 1758 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExpressionStatement)
466 : ExpressionStatement(SourcePosition pos, Expression* expression)
467 1203 : : Statement(kKind, pos), expression(expression) {}
468 : Expression* expression;
469 : };
470 :
471 832 : struct IfStatement : Statement {
472 586 : 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 416 : if_false(if_false) {}
480 : Expression* condition;
481 : bool is_constexpr;
482 : Statement* if_true;
483 : base::Optional<Statement*> if_false;
484 : };
485 :
486 72 : struct WhileStatement : Statement {
487 40 : DEFINE_AST_NODE_LEAF_BOILERPLATE(WhileStatement)
488 : WhileStatement(SourcePosition pos, Expression* condition, Statement* body)
489 36 : : Statement(kKind, pos), condition(condition), body(body) {}
490 : Expression* condition;
491 : Statement* body;
492 : };
493 :
494 1096 : struct ReturnStatement : Statement {
495 691 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ReturnStatement)
496 : ReturnStatement(SourcePosition pos, base::Optional<Expression*> value)
497 548 : : Statement(kKind, pos), value(value) {}
498 : base::Optional<Expression*> value;
499 : };
500 :
501 150 : struct DebugStatement : Statement {
502 77 : 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 75 : never_continues(never_continues) {}
508 : std::string reason;
509 : bool never_continues;
510 : };
511 :
512 436 : struct AssertStatement : Statement {
513 259 : 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 218 : 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 1912 : struct VarDeclarationStatement : Statement {
533 4006 : DEFINE_AST_NODE_LEAF_BOILERPLATE(VarDeclarationStatement)
534 956 : 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 1912 : initializer(initializer) {}
543 : bool const_qualified;
544 : std::string name;
545 : base::Optional<TypeExpression*> type;
546 : base::Optional<Expression*> initializer;
547 : };
548 :
549 34 : struct BreakStatement : Statement {
550 17 : DEFINE_AST_NODE_LEAF_BOILERPLATE(BreakStatement)
551 17 : explicit BreakStatement(SourcePosition pos) : Statement(kKind, pos) {}
552 : };
553 :
554 20 : struct ContinueStatement : Statement {
555 15 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ContinueStatement)
556 10 : explicit ContinueStatement(SourcePosition pos) : Statement(kKind, pos) {}
557 : };
558 :
559 465 : struct GotoStatement : Statement {
560 217 : 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 310 : arguments(std::move(arguments)) {}
566 : std::string label;
567 : std::vector<Expression*> arguments;
568 : };
569 :
570 76 : struct ForLoopStatement : Statement {
571 49 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ForLoopStatement)
572 76 : 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 76 : body(std::move(body)) {
580 38 : if (declaration)
581 25 : var_declaration = VarDeclarationStatement::cast(*declaration);
582 38 : }
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 651 : 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 434 : body(std::move(body)) {}
622 : std::string label;
623 : ParameterList parameters;
624 : Statement* body;
625 : };
626 :
627 254 : struct StatementExpression : Expression {
628 154 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StatementExpression)
629 : StatementExpression(SourcePosition pos, Statement* statement)
630 127 : : Expression(kKind, pos), statement(statement) {}
631 : Statement* statement;
632 : };
633 :
634 434 : struct TryLabelExpression : Expression {
635 263 : 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 217 : label_block(label_block) {}
642 : bool catch_exceptions;
643 : Expression* try_expression;
644 : LabelBlock* label_block;
645 : };
646 :
647 2440 : struct BlockStatement : Statement {
648 5982 : 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 1220 : statements(std::move(statements)) {}
654 : bool deferred;
655 : std::vector<Statement*> statements;
656 : };
657 :
658 261 : struct TypeDeclaration : Declaration {
659 87 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeDeclaration)
660 87 : 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 261 : 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 36 : struct TypeAliasDeclaration : Declaration {
678 18 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TypeAliasDeclaration)
679 : TypeAliasDeclaration(SourcePosition pos, std::string name,
680 : TypeExpression* type)
681 36 : : Declaration(kKind, pos), name(std::move(name)), type(type) {}
682 : std::string name;
683 : TypeExpression* type;
684 : };
685 :
686 6280 : struct NameAndTypeExpression {
687 : std::string name;
688 : TypeExpression* type;
689 : };
690 :
691 : struct StructFieldExpression {
692 : NameAndTypeExpression name_and_type;
693 : };
694 :
695 1320 : struct ClassFieldExpression {
696 : NameAndTypeExpression name_and_type;
697 : base::Optional<std::string> index;
698 : bool weak;
699 : };
700 :
701 2356 : struct LabelAndTypes {
702 : std::string name;
703 : std::vector<TypeExpression*> types;
704 : };
705 :
706 : typedef std::vector<LabelAndTypes> LabelAndTypesVector;
707 :
708 916 : struct CallableNodeSignature {
709 : ParameterList parameters;
710 : TypeExpression* return_type;
711 : LabelAndTypesVector labels;
712 : };
713 :
714 1556 : struct CallableNode : AstNode {
715 778 : CallableNode(AstNode::Kind kind, SourcePosition pos, bool transitioning,
716 : std::string name, ParameterList parameters,
717 : TypeExpression* return_type, const LabelAndTypesVector& labels)
718 : : AstNode(kind, pos),
719 : transitioning(transitioning),
720 : name(std::move(name)),
721 2334 : signature(new CallableNodeSignature{parameters, return_type, labels}) {}
722 : DEFINE_AST_NODE_INNER_BOILERPLATE(CallableNode)
723 : bool transitioning;
724 : std::string name;
725 : std::unique_ptr<CallableNodeSignature> signature;
726 : };
727 :
728 1308 : struct MacroDeclaration : CallableNode {
729 326 : DEFINE_AST_NODE_INNER_BOILERPLATE(MacroDeclaration)
730 654 : MacroDeclaration(AstNode::Kind kind, SourcePosition pos, bool transitioning,
731 : std::string name, base::Optional<std::string> op,
732 : ParameterList parameters, TypeExpression* return_type,
733 : const LabelAndTypesVector& labels)
734 : : CallableNode(kind, pos, transitioning, std::move(name),
735 : std::move(parameters), return_type, labels),
736 2616 : op(std::move(op)) {}
737 : base::Optional<std::string> op;
738 : };
739 :
740 1179 : struct ExternalMacroDeclaration : MacroDeclaration {
741 393 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalMacroDeclaration)
742 393 : ExternalMacroDeclaration(SourcePosition pos, bool transitioning,
743 : std::string external_assembler_name,
744 : std::string name, base::Optional<std::string> op,
745 : ParameterList parameters,
746 : TypeExpression* return_type,
747 : const LabelAndTypesVector& labels)
748 : : MacroDeclaration(kKind, pos, transitioning, std::move(name),
749 : std::move(op), std::move(parameters), return_type,
750 : labels),
751 1965 : external_assembler_name(std::move(external_assembler_name)) {}
752 : std::string external_assembler_name;
753 : };
754 :
755 6 : struct IntrinsicDeclaration : CallableNode {
756 186 : DEFINE_AST_NODE_LEAF_BOILERPLATE(IntrinsicDeclaration)
757 6 : IntrinsicDeclaration(SourcePosition pos, std::string name,
758 : ParameterList parameters, TypeExpression* return_type)
759 : : CallableNode(kKind, pos, false, std::move(name), std::move(parameters),
760 18 : return_type, {}) {}
761 : };
762 :
763 261 : struct TorqueMacroDeclaration : MacroDeclaration {
764 225 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueMacroDeclaration)
765 261 : TorqueMacroDeclaration(SourcePosition pos, bool transitioning,
766 : std::string name, base::Optional<std::string> op,
767 : ParameterList parameters, TypeExpression* return_type,
768 : const LabelAndTypesVector& labels)
769 : : MacroDeclaration(kKind, pos, transitioning, std::move(name),
770 : std::move(op), std::move(parameters), return_type,
771 1044 : labels) {}
772 : };
773 :
774 104 : struct BuiltinDeclaration : CallableNode {
775 : DEFINE_AST_NODE_INNER_BOILERPLATE(BuiltinDeclaration)
776 104 : BuiltinDeclaration(AstNode::Kind kind, SourcePosition pos,
777 : bool javascript_linkage, bool transitioning,
778 : std::string name, ParameterList parameters,
779 : TypeExpression* return_type)
780 : : CallableNode(kind, pos, transitioning, std::move(name),
781 : std::move(parameters), return_type, {}),
782 312 : javascript_linkage(javascript_linkage) {}
783 : bool javascript_linkage;
784 : };
785 :
786 36 : struct ExternalBuiltinDeclaration : BuiltinDeclaration {
787 18 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalBuiltinDeclaration)
788 18 : ExternalBuiltinDeclaration(SourcePosition pos, bool transitioning,
789 : bool javascript_linkage, std::string name,
790 : ParameterList parameters,
791 : TypeExpression* return_type)
792 : : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning,
793 : std::move(name), std::move(parameters),
794 54 : return_type) {}
795 : };
796 :
797 172 : struct TorqueBuiltinDeclaration : BuiltinDeclaration {
798 78 : DEFINE_AST_NODE_LEAF_BOILERPLATE(TorqueBuiltinDeclaration)
799 86 : TorqueBuiltinDeclaration(SourcePosition pos, bool transitioning,
800 : bool javascript_linkage, std::string name,
801 : ParameterList parameters,
802 : TypeExpression* return_type)
803 : : BuiltinDeclaration(kKind, pos, javascript_linkage, transitioning,
804 : std::move(name), std::move(parameters),
805 258 : return_type) {}
806 : };
807 :
808 14 : struct ExternalRuntimeDeclaration : CallableNode {
809 14 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternalRuntimeDeclaration)
810 14 : ExternalRuntimeDeclaration(SourcePosition pos, bool transitioning,
811 : std::string name, ParameterList parameters,
812 : TypeExpression* return_type)
813 : : CallableNode(kKind, pos, transitioning, name, parameters, return_type,
814 28 : {}) {}
815 : };
816 :
817 48 : struct ConstDeclaration : Declaration {
818 24 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ConstDeclaration)
819 : ConstDeclaration(SourcePosition pos, std::string name, TypeExpression* type,
820 : Expression* expression)
821 : : Declaration(kKind, pos),
822 : name(std::move(name)),
823 : type(type),
824 48 : expression(expression) {}
825 : std::string name;
826 : TypeExpression* type;
827 : Expression* expression;
828 : };
829 :
830 1456 : struct StandardDeclaration : Declaration {
831 728 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StandardDeclaration)
832 : StandardDeclaration(SourcePosition pos, CallableNode* callable,
833 : base::Optional<Statement*> body)
834 728 : : Declaration(kKind, pos), callable(callable), body(body) {}
835 : CallableNode* callable;
836 : base::Optional<Statement*> body;
837 : };
838 :
839 100 : struct GenericDeclaration : Declaration {
840 50 : DEFINE_AST_NODE_LEAF_BOILERPLATE(GenericDeclaration)
841 : GenericDeclaration(SourcePosition pos, CallableNode* callable,
842 : std::vector<std::string> generic_parameters,
843 : base::Optional<Statement*> body = base::nullopt)
844 : : Declaration(kKind, pos),
845 : callable(callable),
846 : generic_parameters(std::move(generic_parameters)),
847 50 : body(body) {}
848 : CallableNode* callable;
849 : std::vector<std::string> generic_parameters;
850 : base::Optional<Statement*> body;
851 : };
852 :
853 414 : struct SpecializationDeclaration : Declaration {
854 138 : DEFINE_AST_NODE_LEAF_BOILERPLATE(SpecializationDeclaration)
855 138 : SpecializationDeclaration(SourcePosition pos, std::string name,
856 : std::vector<TypeExpression*> generic_parameters,
857 : ParameterList parameters,
858 : TypeExpression* return_type,
859 : LabelAndTypesVector labels, Statement* b)
860 : : Declaration(kKind, pos),
861 : name(std::move(name)),
862 : external(false),
863 : generic_parameters(std::move(generic_parameters)),
864 : signature(new CallableNodeSignature{std::move(parameters), return_type,
865 : std::move(labels)}),
866 552 : body(b) {}
867 : std::string name;
868 : bool external;
869 : std::vector<TypeExpression*> generic_parameters;
870 : std::unique_ptr<CallableNodeSignature> signature;
871 : Statement* body;
872 : };
873 :
874 243 : struct ExternConstDeclaration : Declaration {
875 81 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ExternConstDeclaration)
876 : ExternConstDeclaration(SourcePosition pos, std::string name,
877 : TypeExpression* type, std::string literal)
878 : : Declaration(kKind, pos),
879 : name(std::move(name)),
880 : type(type),
881 162 : literal(std::move(literal)) {}
882 : std::string name;
883 : TypeExpression* type;
884 : std::string literal;
885 : };
886 :
887 39 : struct StructDeclaration : Declaration {
888 13 : DEFINE_AST_NODE_LEAF_BOILERPLATE(StructDeclaration)
889 13 : StructDeclaration(SourcePosition pos, std::string name,
890 : std::vector<Declaration*> methods,
891 : std::vector<StructFieldExpression> fields)
892 : : Declaration(kKind, pos),
893 : name(std::move(name)),
894 : methods(std::move(methods)),
895 26 : fields(std::move(fields)) {}
896 : std::string name;
897 : std::vector<Declaration*> methods;
898 : std::vector<StructFieldExpression> fields;
899 : };
900 :
901 57 : struct ClassDeclaration : Declaration {
902 19 : DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration)
903 19 : ClassDeclaration(SourcePosition pos, std::string name, bool is_extern,
904 : bool transient, base::Optional<std::string> super,
905 : base::Optional<std::string> generates,
906 : std::vector<Declaration*> methods,
907 : std::vector<ClassFieldExpression> fields)
908 : : Declaration(kKind, pos),
909 : name(std::move(name)),
910 : is_extern(is_extern),
911 : transient(transient),
912 : super(std::move(super)),
913 : generates(std::move(generates)),
914 : methods(std::move(methods)),
915 57 : fields(std::move(fields)) {}
916 : std::string name;
917 : bool is_extern;
918 : bool transient;
919 : base::Optional<std::string> super;
920 : base::Optional<std::string> generates;
921 : std::vector<Declaration*> methods;
922 : std::vector<ClassFieldExpression> fields;
923 : };
924 :
925 30 : struct CppIncludeDeclaration : Declaration {
926 15 : DEFINE_AST_NODE_LEAF_BOILERPLATE(CppIncludeDeclaration)
927 : CppIncludeDeclaration(SourcePosition pos, std::string include_path)
928 15 : : Declaration(kKind, pos), include_path(std::move(include_path)) {}
929 : std::string include_path;
930 : };
931 :
932 : #define ENUM_ITEM(name) \
933 : case AstNode::Kind::k##name: \
934 : return std::is_base_of<T, name>::value; \
935 : break;
936 :
937 : template <class T>
938 22117 : bool AstNodeClassCheck::IsInstanceOf(AstNode* node) {
939 22117 : switch (node->kind) {
940 15607 : AST_NODE_KIND_LIST(ENUM_ITEM)
941 : default:
942 0 : UNIMPLEMENTED();
943 : }
944 : return true;
945 : }
946 :
947 : #undef ENUM_ITEM
948 :
949 833 : inline bool IsDeferred(Statement* stmt) {
950 833 : if (auto* block = BlockStatement::DynamicCast(stmt)) {
951 485 : return block->deferred;
952 : }
953 : return false;
954 : }
955 :
956 : DECLARE_CONTEXTUAL_VARIABLE(CurrentAst, Ast);
957 :
958 : template <class T, class... Args>
959 25931 : T* MakeNode(Args... args) {
960 : return CurrentAst::Get().AddNode(std::unique_ptr<T>(
961 129764 : new T(CurrentSourcePosition::Get(), std::move(args)...)));
962 : }
963 :
964 : } // namespace torque
965 : } // namespace internal
966 : } // namespace v8
967 :
968 : #endif // V8_TORQUE_AST_H_
|