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 : #include <cctype>
6 :
7 : #include "src/torque/earley-parser.h"
8 : #include "src/torque/torque-parser.h"
9 : #include "src/torque/utils.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace torque {
14 :
15 16690 : DEFINE_CONTEXTUAL_VARIABLE(CurrentAst)
16 :
17 : using TypeList = std::vector<TypeExpression*>;
18 : using GenericParameters = std::vector<Identifier*>;
19 :
20 1589 : struct ExpressionWithSource {
21 : Expression* expression;
22 : std::string source;
23 : };
24 :
25 830 : struct TypeswitchCase {
26 : SourcePosition pos;
27 : base::Optional<std::string> name;
28 : TypeExpression* type;
29 : Statement* block;
30 : };
31 :
32 : template <>
33 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<std::string>::id =
34 : ParseResultTypeId::kStdString;
35 : template <>
36 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<bool>::id =
37 : ParseResultTypeId::kBool;
38 : template <>
39 : V8_EXPORT_PRIVATE const ParseResultTypeId
40 : ParseResultHolder<std::vector<std::string>>::id =
41 : ParseResultTypeId::kStdVectorOfString;
42 : template <>
43 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<Declaration*>::id =
44 : ParseResultTypeId::kDeclarationPtr;
45 : template <>
46 : V8_EXPORT_PRIVATE const ParseResultTypeId
47 : ParseResultHolder<TypeExpression*>::id =
48 : ParseResultTypeId::kTypeExpressionPtr;
49 : template <>
50 : V8_EXPORT_PRIVATE const ParseResultTypeId
51 : ParseResultHolder<base::Optional<TypeExpression*>>::id =
52 : ParseResultTypeId::kOptionalTypeExpressionPtr;
53 : template <>
54 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<LabelBlock*>::id =
55 : ParseResultTypeId::kLabelBlockPtr;
56 : template <>
57 : V8_EXPORT_PRIVATE const ParseResultTypeId
58 : ParseResultHolder<base::Optional<LabelBlock*>>::id =
59 : ParseResultTypeId::kOptionalLabelBlockPtr;
60 : template <>
61 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<Expression*>::id =
62 : ParseResultTypeId::kExpressionPtr;
63 : template <>
64 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<Identifier*>::id =
65 : ParseResultTypeId::kIdentifierPtr;
66 : template <>
67 : V8_EXPORT_PRIVATE const ParseResultTypeId
68 : ParseResultHolder<base::Optional<Identifier*>>::id =
69 : ParseResultTypeId::kOptionalIdentifierPtr;
70 : template <>
71 : V8_EXPORT_PRIVATE const ParseResultTypeId
72 : ParseResultHolder<LocationExpression*>::id =
73 : ParseResultTypeId::kLocationExpressionPtr;
74 : template <>
75 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<Statement*>::id =
76 : ParseResultTypeId::kStatementPtr;
77 : template <>
78 : V8_EXPORT_PRIVATE const ParseResultTypeId
79 : ParseResultHolder<NameAndTypeExpression>::id =
80 : ParseResultTypeId::kNameAndTypeExpression;
81 : template <>
82 : V8_EXPORT_PRIVATE const ParseResultTypeId
83 : ParseResultHolder<ClassFieldExpression>::id =
84 : ParseResultTypeId::kClassFieldExpression;
85 : template <>
86 : V8_EXPORT_PRIVATE const ParseResultTypeId
87 : ParseResultHolder<StructFieldExpression>::id =
88 : ParseResultTypeId::kStructFieldExpression;
89 : template <>
90 : V8_EXPORT_PRIVATE const ParseResultTypeId
91 : ParseResultHolder<std::vector<NameAndTypeExpression>>::id =
92 : ParseResultTypeId::kStdVectorOfNameAndTypeExpression;
93 : template <>
94 : V8_EXPORT_PRIVATE const ParseResultTypeId
95 : ParseResultHolder<std::vector<ClassFieldExpression>>::id =
96 : ParseResultTypeId::kStdVectorOfClassFieldExpression;
97 : template <>
98 : V8_EXPORT_PRIVATE const ParseResultTypeId
99 : ParseResultHolder<std::vector<StructFieldExpression>>::id =
100 : ParseResultTypeId::kStdVectorOfStructFieldExpression;
101 : template <>
102 : V8_EXPORT_PRIVATE const ParseResultTypeId
103 : ParseResultHolder<IncrementDecrementOperator>::id =
104 : ParseResultTypeId::kIncrementDecrementOperator;
105 : template <>
106 : V8_EXPORT_PRIVATE const ParseResultTypeId
107 : ParseResultHolder<base::Optional<std::string>>::id =
108 : ParseResultTypeId::kOptionalStdString;
109 : template <>
110 : V8_EXPORT_PRIVATE const ParseResultTypeId
111 : ParseResultHolder<std::vector<Statement*>>::id =
112 : ParseResultTypeId::kStdVectorOfStatementPtr;
113 : template <>
114 : V8_EXPORT_PRIVATE const ParseResultTypeId
115 : ParseResultHolder<std::vector<Declaration*>>::id =
116 : ParseResultTypeId::kStdVectorOfDeclarationPtr;
117 : template <>
118 : V8_EXPORT_PRIVATE const ParseResultTypeId
119 : ParseResultHolder<std::vector<Expression*>>::id =
120 : ParseResultTypeId::kStdVectorOfExpressionPtr;
121 : template <>
122 : V8_EXPORT_PRIVATE const ParseResultTypeId
123 : ParseResultHolder<ExpressionWithSource>::id =
124 : ParseResultTypeId::kExpressionWithSource;
125 : template <>
126 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<ParameterList>::id =
127 : ParseResultTypeId::kParameterList;
128 : template <>
129 : V8_EXPORT_PRIVATE const ParseResultTypeId
130 : ParseResultHolder<RangeExpression>::id =
131 : ParseResultTypeId::kRangeExpression;
132 : template <>
133 : V8_EXPORT_PRIVATE const ParseResultTypeId
134 : ParseResultHolder<base::Optional<RangeExpression>>::id =
135 : ParseResultTypeId::kOptionalRangeExpression;
136 : template <>
137 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<TypeList>::id =
138 : ParseResultTypeId::kTypeList;
139 : template <>
140 : V8_EXPORT_PRIVATE const ParseResultTypeId
141 : ParseResultHolder<base::Optional<TypeList>>::id =
142 : ParseResultTypeId::kOptionalTypeList;
143 : template <>
144 : V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder<LabelAndTypes>::id =
145 : ParseResultTypeId::kLabelAndTypes;
146 : template <>
147 : V8_EXPORT_PRIVATE const ParseResultTypeId
148 : ParseResultHolder<std::vector<LabelAndTypes>>::id =
149 : ParseResultTypeId::kStdVectorOfLabelAndTypes;
150 : template <>
151 : V8_EXPORT_PRIVATE const ParseResultTypeId
152 : ParseResultHolder<std::vector<LabelBlock*>>::id =
153 : ParseResultTypeId::kStdVectorOfLabelBlockPtr;
154 : template <>
155 : V8_EXPORT_PRIVATE const ParseResultTypeId
156 : ParseResultHolder<base::Optional<Statement*>>::id =
157 : ParseResultTypeId::kOptionalStatementPtr;
158 : template <>
159 : V8_EXPORT_PRIVATE const ParseResultTypeId
160 : ParseResultHolder<base::Optional<Expression*>>::id =
161 : ParseResultTypeId::kOptionalExpressionPtr;
162 : template <>
163 : V8_EXPORT_PRIVATE const ParseResultTypeId
164 : ParseResultHolder<TypeswitchCase>::id = ParseResultTypeId::kTypeswitchCase;
165 : template <>
166 : V8_EXPORT_PRIVATE const ParseResultTypeId
167 : ParseResultHolder<std::vector<TypeswitchCase>>::id =
168 : ParseResultTypeId::kStdVectorOfTypeswitchCase;
169 : template <>
170 : V8_EXPORT_PRIVATE const ParseResultTypeId
171 : ParseResultHolder<std::vector<Identifier*>>::id =
172 : ParseResultTypeId::kStdVectorOfIdentifierPtr;
173 :
174 : namespace {
175 :
176 897 : base::Optional<ParseResult> AddGlobalDeclaration(
177 : ParseResultIterator* child_results) {
178 897 : auto declaration = child_results->NextAs<Declaration*>();
179 897 : CurrentAst::Get().declarations().push_back(declaration);
180 897 : return base::nullopt;
181 : }
182 :
183 834 : void LintGenericParameters(const GenericParameters& parameters) {
184 891 : for (const Identifier* parameter : parameters) {
185 57 : if (!IsUpperCamelCase(parameter->value)) {
186 0 : NamingConventionError("Generic parameter", parameter->value,
187 0 : "UpperCamelCase");
188 : }
189 : }
190 834 : }
191 :
192 3770 : void CheckNotDeferredStatement(Statement* statement) {
193 3770 : CurrentSourcePosition::Scope source_position(statement->pos);
194 3770 : if (BlockStatement* block = BlockStatement::DynamicCast(statement)) {
195 374 : if (block->deferred) {
196 0 : LintError(
197 : "cannot use deferred with a statement block here, it will have no "
198 0 : "effect");
199 : }
200 : }
201 3770 : }
202 :
203 3058 : Expression* MakeCall(IdentifierExpression* callee,
204 : base::Optional<Expression*> target,
205 : std::vector<Expression*> arguments,
206 : const std::vector<Statement*>& otherwise) {
207 3058 : std::vector<std::string> labels;
208 :
209 : // All IdentifierExpressions are treated as label names and can be directly
210 : // used as labels identifiers. All other statements in a call's otherwise
211 : // must create intermediate Labels for the otherwise's statement code.
212 : size_t label_id = 0;
213 : std::vector<LabelBlock*> temp_labels;
214 3447 : for (auto* statement : otherwise) {
215 389 : if (auto* e = ExpressionStatement::DynamicCast(statement)) {
216 474 : if (auto* id = IdentifierExpression::DynamicCast(e->expression)) {
217 228 : if (id->generic_arguments.size() != 0) {
218 0 : ReportError("An otherwise label cannot have generic parameters");
219 : }
220 228 : labels.push_back(id->name->value);
221 228 : continue;
222 : }
223 : }
224 644 : auto label_name = std::string("_label") + std::to_string(label_id++);
225 161 : labels.push_back(label_name);
226 : auto* label_block =
227 322 : MakeNode<LabelBlock>(label_name, ParameterList::Empty(), statement);
228 161 : temp_labels.push_back(label_block);
229 : }
230 :
231 : // Create nested try-label expression for all of the temporary Labels that
232 : // were created.
233 : Expression* result = nullptr;
234 3058 : if (target) {
235 234 : result = MakeNode<CallMethodExpression>(*target, callee, arguments, labels);
236 : } else {
237 5882 : result = MakeNode<CallExpression>(callee, arguments, labels);
238 : }
239 :
240 3219 : for (auto* label : temp_labels) {
241 161 : result = MakeNode<TryLabelExpression>(false, result, label);
242 : }
243 3058 : return result;
244 : }
245 :
246 1224 : Expression* MakeCall(const std::string& callee,
247 : const std::vector<TypeExpression*>& generic_arguments,
248 : const std::vector<Expression*>& arguments,
249 : const std::vector<Statement*>& otherwise) {
250 7344 : return MakeCall(MakeNode<IdentifierExpression>(MakeNode<Identifier>(callee),
251 : generic_arguments),
252 2448 : base::nullopt, arguments, otherwise);
253 : }
254 :
255 1717 : base::Optional<ParseResult> MakeCall(ParseResultIterator* child_results) {
256 1717 : auto callee = child_results->NextAs<LocationExpression*>();
257 1717 : auto args = child_results->NextAs<std::vector<Expression*>>();
258 1717 : auto otherwise = child_results->NextAs<std::vector<Statement*>>();
259 : IdentifierExpression* target = IdentifierExpression::cast(callee);
260 8585 : return ParseResult{MakeCall(target, base::nullopt, args, otherwise)};
261 : }
262 :
263 117 : base::Optional<ParseResult> MakeMethodCall(ParseResultIterator* child_results) {
264 117 : auto this_arg = child_results->NextAs<Expression*>();
265 117 : auto callee = child_results->NextAs<std::string>();
266 117 : auto args = child_results->NextAs<std::vector<Expression*>>();
267 117 : auto otherwise = child_results->NextAs<std::vector<Statement*>>();
268 : return ParseResult{
269 : MakeCall(MakeNode<IdentifierExpression>(MakeNode<Identifier>(callee)),
270 585 : this_arg, args, otherwise)};
271 : }
272 :
273 7 : base::Optional<ParseResult> MakeNew(ParseResultIterator* child_results) {
274 7 : TypeExpression* type = child_results->NextAs<TypeExpression*>();
275 7 : auto args = child_results->NextAs<std::vector<Expression*>>();
276 14 : Expression* result = MakeNode<NewExpression>(type, args);
277 7 : return ParseResult{result};
278 : }
279 :
280 1139 : base::Optional<ParseResult> MakeBinaryOperator(
281 : ParseResultIterator* child_results) {
282 1139 : auto left = child_results->NextAs<Expression*>();
283 1139 : auto op = child_results->NextAs<std::string>();
284 1139 : auto right = child_results->NextAs<Expression*>();
285 : return ParseResult{MakeCall(op, TypeList{},
286 : std::vector<Expression*>{left, right},
287 6834 : std::vector<Statement*>{})};
288 : }
289 :
290 46 : base::Optional<ParseResult> MakeIntrinsicCallExpression(
291 : ParseResultIterator* child_results) {
292 46 : auto callee = child_results->NextAs<std::string>();
293 : auto generic_arguments =
294 46 : child_results->NextAs<std::vector<TypeExpression*>>();
295 46 : auto args = child_results->NextAs<std::vector<Expression*>>();
296 : Expression* result =
297 230 : MakeNode<IntrinsicCallExpression>(callee, generic_arguments, args);
298 46 : return ParseResult{result};
299 : }
300 :
301 51 : base::Optional<ParseResult> MakeUnaryOperator(
302 : ParseResultIterator* child_results) {
303 51 : auto op = child_results->NextAs<std::string>();
304 51 : auto e = child_results->NextAs<Expression*>();
305 : return ParseResult{MakeCall(op, TypeList{}, std::vector<Expression*>{e},
306 306 : std::vector<Statement*>{})};
307 : }
308 :
309 : template <bool has_varargs>
310 445 : base::Optional<ParseResult> MakeParameterListFromTypes(
311 : ParseResultIterator* child_results) {
312 : auto implicit_params =
313 445 : child_results->NextAs<std::vector<NameAndTypeExpression>>();
314 445 : auto explicit_types = child_results->NextAs<TypeList>();
315 445 : ParameterList result;
316 445 : result.has_varargs = has_varargs;
317 445 : result.implicit_count = implicit_params.size();
318 497 : for (NameAndTypeExpression& implicit_param : implicit_params) {
319 52 : if (!IsLowerCamelCase(implicit_param.name->value)) {
320 0 : NamingConventionError("Parameter", implicit_param.name->value,
321 : "lowerCamelCase");
322 : }
323 52 : result.names.push_back(implicit_param.name);
324 52 : result.types.push_back(implicit_param.type);
325 : }
326 1235 : for (auto* explicit_type : explicit_types) {
327 790 : result.types.push_back(explicit_type);
328 : }
329 1335 : return ParseResult{std::move(result)};
330 : }
331 :
332 : template <bool has_varargs>
333 597 : base::Optional<ParseResult> MakeParameterListFromNameAndTypeList(
334 : ParseResultIterator* child_results) {
335 : auto implicit_params =
336 597 : child_results->NextAs<std::vector<NameAndTypeExpression>>();
337 : auto explicit_params =
338 597 : child_results->NextAs<std::vector<NameAndTypeExpression>>();
339 597 : std::string arguments_variable = "";
340 597 : if (child_results->HasNext()) {
341 152 : arguments_variable = child_results->NextAs<std::string>();
342 : }
343 597 : ParameterList result;
344 825 : for (NameAndTypeExpression& pair : implicit_params) {
345 228 : if (!IsLowerCamelCase(pair.name->value)) {
346 0 : NamingConventionError("Parameter", pair.name->value, "lowerCamelCase");
347 : }
348 :
349 228 : result.names.push_back(std::move(pair.name));
350 228 : result.types.push_back(pair.type);
351 : }
352 1815 : for (NameAndTypeExpression& pair : explicit_params) {
353 1218 : if (!IsLowerCamelCase(pair.name->value)) {
354 0 : NamingConventionError("Parameter", pair.name->value, "lowerCamelCase");
355 : }
356 :
357 1218 : result.names.push_back(pair.name);
358 1218 : result.types.push_back(pair.type);
359 : }
360 597 : result.implicit_count = implicit_params.size();
361 597 : result.has_varargs = has_varargs;
362 : result.arguments_variable = arguments_variable;
363 1791 : return ParseResult{std::move(result)};
364 : }
365 :
366 227 : base::Optional<ParseResult> MakeAssertStatement(
367 : ParseResultIterator* child_results) {
368 227 : auto kind = child_results->NextAs<std::string>();
369 227 : auto expr_with_source = child_results->NextAs<ExpressionWithSource>();
370 : DCHECK(kind == "assert" || kind == "check");
371 681 : Statement* result = MakeNode<AssertStatement>(
372 227 : kind == "assert", expr_with_source.expression, expr_with_source.source);
373 227 : return ParseResult{result};
374 : }
375 :
376 123 : base::Optional<ParseResult> MakeDebugStatement(
377 : ParseResultIterator* child_results) {
378 123 : auto kind = child_results->NextAs<std::string>();
379 : DCHECK(kind == "unreachable" || kind == "debug");
380 369 : Statement* result = MakeNode<DebugStatement>(kind, kind == "unreachable");
381 123 : return ParseResult{result};
382 : }
383 :
384 100 : base::Optional<ParseResult> MakeVoidType(ParseResultIterator* child_results) {
385 : TypeExpression* result =
386 100 : MakeNode<BasicTypeExpression>(std::vector<std::string>{}, false, "void");
387 100 : return ParseResult{result};
388 : }
389 :
390 412 : base::Optional<ParseResult> MakeExternalMacro(
391 : ParseResultIterator* child_results) {
392 412 : auto transitioning = child_results->NextAs<bool>();
393 412 : auto operator_name = child_results->NextAs<base::Optional<std::string>>();
394 : auto external_assembler_name =
395 412 : child_results->NextAs<base::Optional<std::string>>();
396 412 : auto name = child_results->NextAs<std::string>();
397 412 : auto generic_parameters = child_results->NextAs<GenericParameters>();
398 412 : LintGenericParameters(generic_parameters);
399 :
400 824 : auto args = child_results->NextAs<ParameterList>();
401 412 : auto return_type = child_results->NextAs<TypeExpression*>();
402 824 : auto labels = child_results->NextAs<LabelAndTypesVector>();
403 2440 : MacroDeclaration* macro = MakeNode<ExternalMacroDeclaration>(
404 : transitioning,
405 : external_assembler_name ? *external_assembler_name : "CodeStubAssembler",
406 412 : name, operator_name, args, return_type, labels);
407 : Declaration* result;
408 412 : if (generic_parameters.empty()) {
409 412 : result = MakeNode<StandardDeclaration>(macro, base::nullopt);
410 : } else {
411 0 : result = MakeNode<GenericDeclaration>(macro, generic_parameters);
412 : }
413 412 : return ParseResult{result};
414 : }
415 :
416 6 : base::Optional<ParseResult> MakeIntrinsicDeclaration(
417 : ParseResultIterator* child_results) {
418 6 : auto name = child_results->NextAs<std::string>();
419 6 : auto generic_parameters = child_results->NextAs<GenericParameters>();
420 6 : LintGenericParameters(generic_parameters);
421 :
422 12 : auto args = child_results->NextAs<ParameterList>();
423 6 : auto return_type = child_results->NextAs<TypeExpression*>();
424 : IntrinsicDeclaration* macro =
425 18 : MakeNode<IntrinsicDeclaration>(name, args, return_type);
426 : Declaration* result;
427 6 : if (generic_parameters.empty()) {
428 0 : result = MakeNode<StandardDeclaration>(macro, base::nullopt);
429 : } else {
430 12 : result = MakeNode<GenericDeclaration>(macro, generic_parameters);
431 : }
432 6 : return ParseResult{result};
433 : }
434 :
435 266 : base::Optional<ParseResult> MakeTorqueMacroDeclaration(
436 : ParseResultIterator* child_results) {
437 266 : auto transitioning = child_results->NextAs<bool>();
438 266 : auto operator_name = child_results->NextAs<base::Optional<std::string>>();
439 266 : auto name = child_results->NextAs<std::string>();
440 266 : if (!IsUpperCamelCase(name)) {
441 0 : NamingConventionError("Macro", name, "UpperCamelCase");
442 : }
443 :
444 266 : auto generic_parameters = child_results->NextAs<GenericParameters>();
445 266 : LintGenericParameters(generic_parameters);
446 :
447 532 : auto args = child_results->NextAs<ParameterList>();
448 266 : auto return_type = child_results->NextAs<TypeExpression*>();
449 532 : auto labels = child_results->NextAs<LabelAndTypesVector>();
450 266 : auto body = child_results->NextAs<base::Optional<Statement*>>();
451 1064 : MacroDeclaration* macro = MakeNode<TorqueMacroDeclaration>(
452 266 : transitioning, name, operator_name, args, return_type, labels);
453 : Declaration* result;
454 266 : if (generic_parameters.empty()) {
455 235 : if (!body) ReportError("A non-generic declaration needs a body.");
456 235 : result = MakeNode<StandardDeclaration>(macro, *body);
457 : } else {
458 62 : result = MakeNode<GenericDeclaration>(macro, generic_parameters, body);
459 : }
460 266 : return ParseResult{result};
461 : }
462 :
463 133 : base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
464 : ParseResultIterator* child_results) {
465 133 : auto transitioning = child_results->NextAs<bool>();
466 133 : auto javascript_linkage = child_results->NextAs<bool>();
467 133 : auto name = child_results->NextAs<std::string>();
468 133 : if (!IsUpperCamelCase(name)) {
469 0 : NamingConventionError("Builtin", name, "UpperCamelCase");
470 : }
471 :
472 133 : auto generic_parameters = child_results->NextAs<GenericParameters>();
473 133 : LintGenericParameters(generic_parameters);
474 :
475 266 : auto args = child_results->NextAs<ParameterList>();
476 133 : auto return_type = child_results->NextAs<TypeExpression*>();
477 133 : auto body = child_results->NextAs<base::Optional<Statement*>>();
478 399 : BuiltinDeclaration* builtin = MakeNode<TorqueBuiltinDeclaration>(
479 133 : transitioning, javascript_linkage, name, args, return_type);
480 : Declaration* result;
481 133 : if (generic_parameters.empty()) {
482 125 : if (!body) ReportError("A non-generic declaration needs a body.");
483 125 : result = MakeNode<StandardDeclaration>(builtin, *body);
484 : } else {
485 16 : result = MakeNode<GenericDeclaration>(builtin, generic_parameters, body);
486 : }
487 133 : return ParseResult{result};
488 : }
489 :
490 31 : base::Optional<ParseResult> MakeConstDeclaration(
491 : ParseResultIterator* child_results) {
492 31 : auto name = child_results->NextAs<Identifier*>();
493 31 : if (!IsValidNamespaceConstName(name->value)) {
494 0 : NamingConventionError("Constant", name->value, "kUpperCamelCase");
495 : }
496 :
497 31 : auto type = child_results->NextAs<TypeExpression*>();
498 31 : auto expression = child_results->NextAs<Expression*>();
499 31 : Declaration* result = MakeNode<ConstDeclaration>(name, type, expression);
500 31 : return ParseResult{result};
501 : }
502 :
503 89 : base::Optional<ParseResult> MakeExternConstDeclaration(
504 : ParseResultIterator* child_results) {
505 89 : auto name = child_results->NextAs<Identifier*>();
506 89 : auto type = child_results->NextAs<TypeExpression*>();
507 89 : auto literal = child_results->NextAs<std::string>();
508 : Declaration* result =
509 178 : MakeNode<ExternConstDeclaration>(name, type, std::move(literal));
510 89 : return ParseResult{result};
511 : }
512 :
513 18 : base::Optional<ParseResult> MakeTypeAliasDeclaration(
514 : ParseResultIterator* child_results) {
515 18 : auto name = child_results->NextAs<Identifier*>();
516 18 : auto type = child_results->NextAs<TypeExpression*>();
517 18 : Declaration* result = MakeNode<TypeAliasDeclaration>(name, type);
518 18 : return ParseResult{result};
519 : }
520 :
521 96 : base::Optional<ParseResult> MakeTypeDeclaration(
522 : ParseResultIterator* child_results) {
523 96 : auto transient = child_results->NextAs<bool>();
524 96 : auto name = child_results->NextAs<Identifier*>();
525 96 : if (!IsValidTypeName(name->value)) {
526 0 : NamingConventionError("Type", name->value, "UpperCamelCase");
527 : }
528 96 : auto extends = child_results->NextAs<base::Optional<Identifier*>>();
529 96 : auto generates = child_results->NextAs<base::Optional<std::string>>();
530 : auto constexpr_generates =
531 96 : child_results->NextAs<base::Optional<std::string>>();
532 : Declaration* result =
533 288 : MakeNode<TypeDeclaration>(name, transient, extends, std::move(generates),
534 96 : std::move(constexpr_generates));
535 96 : return ParseResult{result};
536 : }
537 :
538 32 : base::Optional<ParseResult> MakeMethodDeclaration(
539 : ParseResultIterator* child_results) {
540 32 : auto transitioning = child_results->NextAs<bool>();
541 32 : auto operator_name = child_results->NextAs<base::Optional<std::string>>();
542 32 : auto name = child_results->NextAs<std::string>();
543 32 : if (!IsUpperCamelCase(name)) {
544 0 : NamingConventionError("Method", name, "UpperCamelCase");
545 : }
546 :
547 64 : auto args = child_results->NextAs<ParameterList>();
548 32 : auto return_type = child_results->NextAs<TypeExpression*>();
549 64 : auto labels = child_results->NextAs<LabelAndTypesVector>();
550 32 : auto body = child_results->NextAs<Statement*>();
551 128 : MacroDeclaration* macro = MakeNode<TorqueMacroDeclaration>(
552 32 : transitioning, name, operator_name, args, return_type, labels);
553 32 : Declaration* result = MakeNode<StandardDeclaration>(macro, body);
554 32 : return ParseResult{result};
555 : }
556 :
557 89 : base::Optional<ParseResult> MakeClassDeclaration(
558 : ParseResultIterator* child_results) {
559 89 : auto is_extern = child_results->NextAs<bool>();
560 89 : auto transient = child_results->NextAs<bool>();
561 89 : auto name = child_results->NextAs<Identifier*>();
562 89 : if (!IsValidTypeName(name->value)) {
563 0 : NamingConventionError("Type", name->value, "UpperCamelCase");
564 : }
565 89 : auto extends = child_results->NextAs<base::Optional<std::string>>();
566 89 : auto generates = child_results->NextAs<base::Optional<std::string>>();
567 89 : auto methods = child_results->NextAs<std::vector<Declaration*>>();
568 178 : auto fields = child_results->NextAs<std::vector<ClassFieldExpression>>();
569 445 : Declaration* result = MakeNode<ClassDeclaration>(
570 : name, is_extern, transient, std::move(extends), std::move(generates),
571 89 : std::move(methods), fields);
572 89 : return ParseResult{result};
573 : }
574 :
575 45 : base::Optional<ParseResult> MakeNamespaceDeclaration(
576 : ParseResultIterator* child_results) {
577 45 : auto name = child_results->NextAs<std::string>();
578 45 : if (!IsSnakeCase(name)) {
579 0 : NamingConventionError("Namespace", name, "snake_case");
580 : }
581 45 : auto declarations = child_results->NextAs<std::vector<Declaration*>>();
582 : Declaration* result =
583 135 : MakeNode<NamespaceDeclaration>(std::move(name), std::move(declarations));
584 45 : return ParseResult{result};
585 : }
586 :
587 139 : base::Optional<ParseResult> MakeSpecializationDeclaration(
588 : ParseResultIterator* child_results) {
589 139 : auto name = child_results->NextAs<std::string>();
590 : auto generic_parameters =
591 139 : child_results->NextAs<std::vector<TypeExpression*>>();
592 278 : auto parameters = child_results->NextAs<ParameterList>();
593 139 : auto return_type = child_results->NextAs<TypeExpression*>();
594 278 : auto labels = child_results->NextAs<LabelAndTypesVector>();
595 139 : auto body = child_results->NextAs<Statement*>();
596 139 : CheckNotDeferredStatement(body);
597 417 : Declaration* result = MakeNode<SpecializationDeclaration>(
598 : std::move(name), std::move(generic_parameters), std::move(parameters),
599 139 : return_type, std::move(labels), body);
600 139 : return ParseResult{result};
601 : }
602 :
603 14 : base::Optional<ParseResult> MakeStructDeclaration(
604 : ParseResultIterator* child_results) {
605 14 : auto name = child_results->NextAs<Identifier*>();
606 14 : auto methods = child_results->NextAs<std::vector<Declaration*>>();
607 14 : auto fields = child_results->NextAs<std::vector<StructFieldExpression>>();
608 : Declaration* result =
609 42 : MakeNode<StructDeclaration>(name, std::move(methods), std::move(fields));
610 14 : return ParseResult{result};
611 : }
612 :
613 29 : base::Optional<ParseResult> MakeCppIncludeDeclaration(
614 : ParseResultIterator* child_results) {
615 29 : auto include_path = child_results->NextAs<std::string>();
616 : Declaration* result =
617 58 : MakeNode<CppIncludeDeclaration>(std::move(include_path));
618 29 : return ParseResult{result};
619 : }
620 :
621 17 : base::Optional<ParseResult> MakeExternalBuiltin(
622 : ParseResultIterator* child_results) {
623 17 : auto transitioning = child_results->NextAs<bool>();
624 17 : auto js_linkage = child_results->NextAs<bool>();
625 17 : auto name = child_results->NextAs<std::string>();
626 17 : auto generic_parameters = child_results->NextAs<GenericParameters>();
627 17 : LintGenericParameters(generic_parameters);
628 :
629 34 : auto args = child_results->NextAs<ParameterList>();
630 17 : auto return_type = child_results->NextAs<TypeExpression*>();
631 51 : BuiltinDeclaration* builtin = MakeNode<ExternalBuiltinDeclaration>(
632 17 : transitioning, js_linkage, name, args, return_type);
633 : Declaration* result;
634 17 : if (generic_parameters.empty()) {
635 17 : result = MakeNode<StandardDeclaration>(builtin, base::nullopt);
636 : } else {
637 0 : result = MakeNode<GenericDeclaration>(builtin, generic_parameters);
638 : }
639 17 : return ParseResult{result};
640 : }
641 :
642 16 : base::Optional<ParseResult> MakeExternalRuntime(
643 : ParseResultIterator* child_results) {
644 16 : auto transitioning = child_results->NextAs<bool>();
645 16 : auto name = child_results->NextAs<std::string>();
646 32 : auto args = child_results->NextAs<ParameterList>();
647 16 : auto return_type = child_results->NextAs<TypeExpression*>();
648 48 : ExternalRuntimeDeclaration* runtime = MakeNode<ExternalRuntimeDeclaration>(
649 16 : transitioning, name, args, return_type);
650 16 : Declaration* result = MakeNode<StandardDeclaration>(runtime, base::nullopt);
651 16 : return ParseResult{result};
652 : }
653 :
654 339 : base::Optional<ParseResult> StringLiteralUnquoteAction(
655 : ParseResultIterator* child_results) {
656 : return ParseResult{
657 1356 : StringLiteralUnquote(child_results->NextAs<std::string>())};
658 : }
659 :
660 5427 : base::Optional<ParseResult> MakeBasicTypeExpression(
661 : ParseResultIterator* child_results) {
662 : auto namespace_qualification =
663 10854 : child_results->NextAs<std::vector<std::string>>();
664 5427 : auto is_constexpr = child_results->NextAs<bool>();
665 5427 : auto name = child_results->NextAs<std::string>();
666 16281 : TypeExpression* result = MakeNode<BasicTypeExpression>(
667 5427 : std::move(namespace_qualification), is_constexpr, std::move(name));
668 5427 : return ParseResult{result};
669 : }
670 :
671 11 : base::Optional<ParseResult> MakeFunctionTypeExpression(
672 : ParseResultIterator* child_results) {
673 11 : auto parameters = child_results->NextAs<std::vector<TypeExpression*>>();
674 11 : auto return_type = child_results->NextAs<TypeExpression*>();
675 : TypeExpression* result =
676 22 : MakeNode<FunctionTypeExpression>(std::move(parameters), return_type);
677 11 : return ParseResult{result};
678 : }
679 :
680 73 : base::Optional<ParseResult> MakeUnionTypeExpression(
681 : ParseResultIterator* child_results) {
682 73 : auto a = child_results->NextAs<TypeExpression*>();
683 73 : auto b = child_results->NextAs<TypeExpression*>();
684 73 : TypeExpression* result = MakeNode<UnionTypeExpression>(a, b);
685 73 : return ParseResult{result};
686 : }
687 :
688 993 : base::Optional<ParseResult> MakeExpressionStatement(
689 : ParseResultIterator* child_results) {
690 993 : auto expression = child_results->NextAs<Expression*>();
691 993 : Statement* result = MakeNode<ExpressionStatement>(expression);
692 993 : return ParseResult{result};
693 : }
694 :
695 487 : base::Optional<ParseResult> MakeIfStatement(
696 : ParseResultIterator* child_results) {
697 487 : auto is_constexpr = child_results->NextAs<bool>();
698 487 : auto condition = child_results->NextAs<Expression*>();
699 487 : auto if_true = child_results->NextAs<Statement*>();
700 487 : auto if_false = child_results->NextAs<base::Optional<Statement*>>();
701 :
702 1144 : if (if_false && !(BlockStatement::DynamicCast(if_true) &&
703 85 : (BlockStatement::DynamicCast(*if_false) ||
704 170 : IfStatement::DynamicCast(*if_false)))) {
705 0 : ReportError("if-else statements require curly braces");
706 : }
707 :
708 487 : if (is_constexpr) {
709 52 : CheckNotDeferredStatement(if_true);
710 52 : if (if_false) CheckNotDeferredStatement(*if_false);
711 : }
712 :
713 : Statement* result =
714 487 : MakeNode<IfStatement>(is_constexpr, condition, if_true, if_false);
715 487 : return ParseResult{result};
716 : }
717 :
718 19 : base::Optional<ParseResult> MakeTypeswitchStatement(
719 : ParseResultIterator* child_results) {
720 19 : auto expression = child_results->NextAs<Expression*>();
721 38 : auto cases = child_results->NextAs<std::vector<TypeswitchCase>>();
722 : CurrentSourcePosition::Scope current_source_position(
723 19 : child_results->matched_input().pos);
724 :
725 : // typeswitch (expression) case (x1 : T1) {
726 : // ...b1
727 : // } case (x2 : T2) {
728 : // ...b2
729 : // } case (x3 : T3) {
730 : // ...b3
731 : // }
732 : //
733 : // desugars to
734 : //
735 : // {
736 : // const _value = expression;
737 : // try {
738 : // const x1 : T1 = cast<T1>(_value) otherwise _NextCase;
739 : // ...b1
740 : // } label _NextCase {
741 : // try {
742 : // const x2 : T2 = cast<T2>(%assume_impossible<T1>(_value));
743 : // ...b2
744 : // } label _NextCase {
745 : // const x3 : T3 = %assume_impossible<T1|T2>(_value);
746 : // ...b3
747 : // }
748 : // }
749 : // }
750 :
751 19 : BlockStatement* current_block = MakeNode<BlockStatement>();
752 : Statement* result = current_block;
753 : {
754 19 : CurrentSourcePosition::Scope current_source_position(expression->pos);
755 38 : current_block->statements.push_back(MakeNode<VarDeclarationStatement>(
756 : true, MakeNode<Identifier>("_value"), base::nullopt, expression));
757 : }
758 :
759 : TypeExpression* accumulated_types;
760 125 : for (size_t i = 0; i < cases.size(); ++i) {
761 53 : CurrentSourcePosition::Scope current_source_position(cases[i].pos);
762 : Expression* value =
763 53 : MakeNode<IdentifierExpression>(MakeNode<Identifier>("_value"));
764 53 : if (i >= 1) {
765 : value =
766 34 : MakeNode<AssumeTypeImpossibleExpression>(accumulated_types, value);
767 : }
768 : BlockStatement* case_block;
769 53 : if (i < cases.size() - 1) {
770 272 : value = MakeCall("Cast", std::vector<TypeExpression*>{cases[i].type},
771 : std::vector<Expression*>{value},
772 34 : std::vector<Statement*>{MakeNode<ExpressionStatement>(
773 : MakeNode<IdentifierExpression>(
774 34 : MakeNode<Identifier>("_NextCase")))});
775 34 : case_block = MakeNode<BlockStatement>();
776 : } else {
777 : case_block = current_block;
778 : }
779 53 : std::string name = "_case_value";
780 : if (cases[i].name) name = *cases[i].name;
781 212 : case_block->statements.push_back(MakeNode<VarDeclarationStatement>(
782 : true, MakeNode<Identifier>(name), cases[i].type, value));
783 53 : case_block->statements.push_back(cases[i].block);
784 53 : if (i < cases.size() - 1) {
785 34 : BlockStatement* next_block = MakeNode<BlockStatement>();
786 68 : current_block->statements.push_back(
787 34 : MakeNode<ExpressionStatement>(MakeNode<TryLabelExpression>(
788 : false, MakeNode<StatementExpression>(case_block),
789 34 : MakeNode<LabelBlock>("_NextCase", ParameterList::Empty(),
790 : next_block))));
791 : current_block = next_block;
792 : }
793 : accumulated_types =
794 34 : i > 0 ? MakeNode<UnionTypeExpression>(accumulated_types, cases[i].type)
795 106 : : cases[i].type;
796 : }
797 19 : return ParseResult{result};
798 : }
799 :
800 53 : base::Optional<ParseResult> MakeTypeswitchCase(
801 : ParseResultIterator* child_results) {
802 53 : auto name = child_results->NextAs<base::Optional<std::string>>();
803 53 : auto type = child_results->NextAs<TypeExpression*>();
804 53 : auto block = child_results->NextAs<Statement*>();
805 : return ParseResult{TypeswitchCase{child_results->matched_input().pos,
806 265 : std::move(name), type, block}};
807 : }
808 :
809 38 : base::Optional<ParseResult> MakeWhileStatement(
810 : ParseResultIterator* child_results) {
811 38 : auto condition = child_results->NextAs<Expression*>();
812 38 : auto body = child_results->NextAs<Statement*>();
813 38 : Statement* result = MakeNode<WhileStatement>(condition, body);
814 38 : CheckNotDeferredStatement(result);
815 38 : return ParseResult{result};
816 : }
817 :
818 598 : base::Optional<ParseResult> MakeReturnStatement(
819 : ParseResultIterator* child_results) {
820 598 : auto value = child_results->NextAs<base::Optional<Expression*>>();
821 598 : Statement* result = MakeNode<ReturnStatement>(value);
822 598 : return ParseResult{result};
823 : }
824 :
825 2 : base::Optional<ParseResult> MakeTailCallStatement(
826 : ParseResultIterator* child_results) {
827 2 : auto value = child_results->NextAs<Expression*>();
828 2 : Statement* result = MakeNode<TailCallStatement>(CallExpression::cast(value));
829 2 : return ParseResult{result};
830 : }
831 :
832 1115 : base::Optional<ParseResult> MakeVarDeclarationStatement(
833 : ParseResultIterator* child_results) {
834 1115 : auto kind = child_results->NextAs<std::string>();
835 : bool const_qualified = kind == "const";
836 : if (!const_qualified) DCHECK_EQ("let", kind);
837 1115 : auto name = child_results->NextAs<Identifier*>();
838 1115 : if (!IsLowerCamelCase(name->value)) {
839 0 : NamingConventionError("Variable", name->value, "lowerCamelCase");
840 : }
841 :
842 1115 : auto type = child_results->NextAs<base::Optional<TypeExpression*>>();
843 1115 : base::Optional<Expression*> initializer;
844 1115 : if (child_results->HasNext())
845 1086 : initializer = child_results->NextAs<Expression*>();
846 1115 : if (!initializer && !type) {
847 0 : ReportError("Declaration is missing a type.");
848 : }
849 1115 : Statement* result = MakeNode<VarDeclarationStatement>(const_qualified, name,
850 1115 : type, initializer);
851 1115 : return ParseResult{result};
852 : }
853 :
854 25 : base::Optional<ParseResult> MakeBreakStatement(
855 : ParseResultIterator* child_results) {
856 25 : Statement* result = MakeNode<BreakStatement>();
857 25 : return ParseResult{result};
858 : }
859 :
860 14 : base::Optional<ParseResult> MakeContinueStatement(
861 : ParseResultIterator* child_results) {
862 14 : Statement* result = MakeNode<ContinueStatement>();
863 14 : return ParseResult{result};
864 : }
865 :
866 185 : base::Optional<ParseResult> MakeGotoStatement(
867 : ParseResultIterator* child_results) {
868 185 : auto label = child_results->NextAs<std::string>();
869 185 : auto arguments = child_results->NextAs<std::vector<Expression*>>();
870 : Statement* result =
871 555 : MakeNode<GotoStatement>(std::move(label), std::move(arguments));
872 185 : return ParseResult{result};
873 : }
874 :
875 1350 : base::Optional<ParseResult> MakeBlockStatement(
876 : ParseResultIterator* child_results) {
877 1350 : auto deferred = child_results->NextAs<bool>();
878 1350 : auto statements = child_results->NextAs<std::vector<Statement*>>();
879 4688 : for (Statement* statement : statements) {
880 3338 : CheckNotDeferredStatement(statement);
881 : }
882 2700 : Statement* result = MakeNode<BlockStatement>(deferred, std::move(statements));
883 1350 : return ParseResult{result};
884 : }
885 :
886 95 : base::Optional<ParseResult> MakeTryLabelExpression(
887 : ParseResultIterator* child_results) {
888 95 : auto try_block = child_results->NextAs<Statement*>();
889 95 : CheckNotDeferredStatement(try_block);
890 : Statement* result = try_block;
891 95 : auto label_blocks = child_results->NextAs<std::vector<LabelBlock*>>();
892 95 : auto catch_block = child_results->NextAs<base::Optional<LabelBlock*>>();
893 224 : for (auto block : label_blocks) {
894 129 : result = MakeNode<ExpressionStatement>(MakeNode<TryLabelExpression>(
895 129 : false, MakeNode<StatementExpression>(result), block));
896 : }
897 95 : if (catch_block) {
898 5 : result = MakeNode<ExpressionStatement>(MakeNode<TryLabelExpression>(
899 5 : true, MakeNode<StatementExpression>(result), *catch_block));
900 : }
901 95 : return ParseResult{result};
902 : }
903 :
904 3 : base::Optional<ParseResult> MakeForOfLoopStatement(
905 : ParseResultIterator* child_results) {
906 3 : auto var_decl = child_results->NextAs<Statement*>();
907 3 : CheckNotDeferredStatement(var_decl);
908 3 : auto iterable = child_results->NextAs<Expression*>();
909 3 : auto range = child_results->NextAs<base::Optional<RangeExpression>>();
910 3 : auto body = child_results->NextAs<Statement*>();
911 3 : CheckNotDeferredStatement(body);
912 : Statement* result =
913 3 : MakeNode<ForOfLoopStatement>(var_decl, iterable, range, body);
914 3 : return ParseResult{result};
915 : }
916 :
917 54 : base::Optional<ParseResult> MakeForLoopStatement(
918 : ParseResultIterator* child_results) {
919 54 : auto var_decl = child_results->NextAs<base::Optional<Statement*>>();
920 54 : auto test = child_results->NextAs<base::Optional<Expression*>>();
921 54 : auto action = child_results->NextAs<base::Optional<Expression*>>();
922 54 : base::Optional<Statement*> action_stmt;
923 54 : if (action) action_stmt = MakeNode<ExpressionStatement>(*action);
924 54 : auto body = child_results->NextAs<Statement*>();
925 54 : CheckNotDeferredStatement(body);
926 : Statement* result =
927 54 : MakeNode<ForLoopStatement>(var_decl, test, action_stmt, body);
928 54 : return ParseResult{result};
929 : }
930 :
931 129 : base::Optional<ParseResult> MakeLabelBlock(ParseResultIterator* child_results) {
932 129 : auto label = child_results->NextAs<std::string>();
933 129 : if (!IsUpperCamelCase(label)) {
934 0 : NamingConventionError("Label", label, "UpperCamelCase");
935 : }
936 258 : auto parameters = child_results->NextAs<ParameterList>();
937 129 : auto body = child_results->NextAs<Statement*>();
938 : LabelBlock* result =
939 387 : MakeNode<LabelBlock>(std::move(label), std::move(parameters), body);
940 129 : return ParseResult{result};
941 : }
942 :
943 5 : base::Optional<ParseResult> MakeCatchBlock(ParseResultIterator* child_results) {
944 5 : auto variable = child_results->NextAs<std::string>();
945 5 : auto body = child_results->NextAs<Statement*>();
946 5 : if (!IsLowerCamelCase(variable)) {
947 0 : NamingConventionError("Exception", variable, "lowerCamelCase");
948 : }
949 5 : ParameterList parameters;
950 15 : parameters.names.push_back(MakeNode<Identifier>(variable));
951 10 : parameters.types.push_back(MakeNode<BasicTypeExpression>(
952 : std::vector<std::string>{}, false, "Object"));
953 5 : parameters.has_varargs = false;
954 : LabelBlock* result =
955 5 : MakeNode<LabelBlock>("_catch", std::move(parameters), body);
956 5 : return ParseResult{result};
957 : }
958 :
959 3 : base::Optional<ParseResult> MakeRangeExpression(
960 : ParseResultIterator* child_results) {
961 3 : auto begin = child_results->NextAs<base::Optional<Expression*>>();
962 3 : auto end = child_results->NextAs<base::Optional<Expression*>>();
963 3 : RangeExpression result = {begin, end};
964 6 : return ParseResult{result};
965 : }
966 :
967 227 : base::Optional<ParseResult> MakeExpressionWithSource(
968 : ParseResultIterator* child_results) {
969 227 : auto e = child_results->NextAs<Expression*>();
970 : return ParseResult{
971 908 : ExpressionWithSource{e, child_results->matched_input().ToString()}};
972 : }
973 :
974 12181 : base::Optional<ParseResult> MakeIdentifier(ParseResultIterator* child_results) {
975 12181 : auto name = child_results->NextAs<std::string>();
976 24362 : Identifier* result = MakeNode<Identifier>(std::move(name));
977 12181 : return ParseResult{result};
978 : }
979 :
980 8207 : base::Optional<ParseResult> MakeIdentifierExpression(
981 : ParseResultIterator* child_results) {
982 : auto namespace_qualification =
983 16414 : child_results->NextAs<std::vector<std::string>>();
984 8207 : auto name = child_results->NextAs<Identifier*>();
985 : auto generic_arguments =
986 8207 : child_results->NextAs<std::vector<TypeExpression*>>();
987 16414 : LocationExpression* result = MakeNode<IdentifierExpression>(
988 8207 : std::move(namespace_qualification), name, std::move(generic_arguments));
989 8207 : return ParseResult{result};
990 : }
991 :
992 621 : base::Optional<ParseResult> MakeFieldAccessExpression(
993 : ParseResultIterator* child_results) {
994 621 : auto object = child_results->NextAs<Expression*>();
995 621 : auto field = child_results->NextAs<Identifier*>();
996 621 : LocationExpression* result = MakeNode<FieldAccessExpression>(object, field);
997 621 : return ParseResult{result};
998 : }
999 :
1000 261 : base::Optional<ParseResult> MakeElementAccessExpression(
1001 : ParseResultIterator* child_results) {
1002 261 : auto object = child_results->NextAs<Expression*>();
1003 261 : auto field = child_results->NextAs<Expression*>();
1004 261 : LocationExpression* result = MakeNode<ElementAccessExpression>(object, field);
1005 261 : return ParseResult{result};
1006 : }
1007 :
1008 17 : base::Optional<ParseResult> MakeStructExpression(
1009 : ParseResultIterator* child_results) {
1010 : auto namespace_qualification =
1011 34 : child_results->NextAs<std::vector<std::string>>();
1012 17 : auto name = child_results->NextAs<std::string>();
1013 17 : auto expressions = child_results->NextAs<std::vector<Expression*>>();
1014 : Expression* result =
1015 51 : MakeNode<StructExpression>(std::move(namespace_qualification),
1016 17 : std::move(name), std::move(expressions));
1017 17 : return ParseResult{result};
1018 : }
1019 :
1020 398 : base::Optional<ParseResult> MakeAssignmentExpression(
1021 : ParseResultIterator* child_results) {
1022 398 : auto location = child_results->NextAs<LocationExpression*>();
1023 398 : auto op = child_results->NextAs<base::Optional<std::string>>();
1024 398 : auto value = child_results->NextAs<Expression*>();
1025 : Expression* result =
1026 796 : MakeNode<AssignmentExpression>(location, std::move(op), value);
1027 398 : return ParseResult{result};
1028 : }
1029 :
1030 938 : base::Optional<ParseResult> MakeNumberLiteralExpression(
1031 : ParseResultIterator* child_results) {
1032 938 : auto number = child_results->NextAs<std::string>();
1033 1876 : Expression* result = MakeNode<NumberLiteralExpression>(std::move(number));
1034 938 : return ParseResult{result};
1035 : }
1036 :
1037 100 : base::Optional<ParseResult> MakeStringLiteralExpression(
1038 : ParseResultIterator* child_results) {
1039 100 : auto literal = child_results->NextAs<std::string>();
1040 200 : Expression* result = MakeNode<StringLiteralExpression>(std::move(literal));
1041 100 : return ParseResult{result};
1042 : }
1043 :
1044 83 : base::Optional<ParseResult> MakeIncrementDecrementExpressionPostfix(
1045 : ParseResultIterator* child_results) {
1046 83 : auto location = child_results->NextAs<LocationExpression*>();
1047 83 : auto op = child_results->NextAs<IncrementDecrementOperator>();
1048 : Expression* result =
1049 83 : MakeNode<IncrementDecrementExpression>(location, op, true);
1050 83 : return ParseResult{result};
1051 : }
1052 :
1053 52 : base::Optional<ParseResult> MakeIncrementDecrementExpressionPrefix(
1054 : ParseResultIterator* child_results) {
1055 52 : auto op = child_results->NextAs<IncrementDecrementOperator>();
1056 52 : auto location = child_results->NextAs<LocationExpression*>();
1057 : Expression* result =
1058 52 : MakeNode<IncrementDecrementExpression>(location, op, false);
1059 52 : return ParseResult{result};
1060 : }
1061 :
1062 28 : base::Optional<ParseResult> MakeLogicalOrExpression(
1063 : ParseResultIterator* child_results) {
1064 28 : auto left = child_results->NextAs<Expression*>();
1065 28 : auto right = child_results->NextAs<Expression*>();
1066 28 : Expression* result = MakeNode<LogicalOrExpression>(left, right);
1067 28 : return ParseResult{result};
1068 : }
1069 :
1070 46 : base::Optional<ParseResult> MakeLogicalAndExpression(
1071 : ParseResultIterator* child_results) {
1072 46 : auto left = child_results->NextAs<Expression*>();
1073 46 : auto right = child_results->NextAs<Expression*>();
1074 46 : Expression* result = MakeNode<LogicalAndExpression>(left, right);
1075 46 : return ParseResult{result};
1076 : }
1077 :
1078 98 : base::Optional<ParseResult> MakeConditionalExpression(
1079 : ParseResultIterator* child_results) {
1080 98 : auto condition = child_results->NextAs<Expression*>();
1081 98 : auto if_true = child_results->NextAs<Expression*>();
1082 98 : auto if_false = child_results->NextAs<Expression*>();
1083 : Expression* result =
1084 98 : MakeNode<ConditionalExpression>(condition, if_true, if_false);
1085 98 : return ParseResult{result};
1086 : }
1087 :
1088 164 : base::Optional<ParseResult> MakeLabelAndTypes(
1089 : ParseResultIterator* child_results) {
1090 164 : auto name = child_results->NextAs<std::string>();
1091 164 : if (!IsUpperCamelCase(name)) {
1092 0 : NamingConventionError("Label", name, "UpperCamelCase");
1093 : }
1094 164 : auto types = child_results->NextAs<std::vector<TypeExpression*>>();
1095 492 : return ParseResult{LabelAndTypes{std::move(name), std::move(types)}};
1096 : }
1097 :
1098 1498 : base::Optional<ParseResult> MakeNameAndType(
1099 : ParseResultIterator* child_results) {
1100 1498 : auto name = child_results->NextAs<Identifier*>();
1101 1498 : auto type = child_results->NextAs<TypeExpression*>();
1102 1498 : return ParseResult{NameAndTypeExpression{name, type}};
1103 : }
1104 :
1105 248 : base::Optional<ParseResult> MakeClassField(ParseResultIterator* child_results) {
1106 248 : auto weak = child_results->NextAs<bool>();
1107 248 : auto name = child_results->NextAs<Identifier*>();
1108 248 : auto index = child_results->NextAs<base::Optional<std::string>>();
1109 248 : auto type = child_results->NextAs<TypeExpression*>();
1110 1240 : return ParseResult{ClassFieldExpression{{name, type}, index, weak}};
1111 : }
1112 :
1113 42 : base::Optional<ParseResult> MakeStructField(
1114 : ParseResultIterator* child_results) {
1115 42 : auto name = child_results->NextAs<Identifier*>();
1116 42 : auto type = child_results->NextAs<TypeExpression*>();
1117 42 : return ParseResult{StructFieldExpression{{name, type}}};
1118 : }
1119 :
1120 11 : base::Optional<ParseResult> ExtractAssignmentOperator(
1121 : ParseResultIterator* child_results) {
1122 11 : auto op = child_results->NextAs<std::string>();
1123 11 : base::Optional<std::string> result = std::string(op.begin(), op.end() - 1);
1124 44 : return ParseResult(std::move(result));
1125 : }
1126 :
1127 98 : struct TorqueGrammar : Grammar {
1128 56899 : static bool MatchWhitespace(InputPosition* pos) {
1129 : while (true) {
1130 132112 : if (MatchChar(std::isspace, pos)) continue;
1131 58486 : if (MatchString("//", pos)) {
1132 153078 : while (MatchChar([](char c) { return c != '\n'; }, pos)) {
1133 : }
1134 : continue;
1135 : }
1136 56899 : return true;
1137 : }
1138 : }
1139 :
1140 56850 : static bool MatchIdentifier(InputPosition* pos) {
1141 56850 : if (!MatchChar(std::isalpha, pos)) return false;
1142 206149 : while (MatchChar(std::isalnum, pos) || MatchString("_", pos)) {
1143 : }
1144 : return true;
1145 : }
1146 :
1147 56850 : static bool MatchIntrinsicName(InputPosition* pos) {
1148 56850 : InputPosition current = *pos;
1149 56850 : if (!MatchString("%", ¤t)) return false;
1150 52 : if (!MatchChar(std::isalpha, ¤t)) return false;
1151 631 : while (MatchChar(std::isalnum, ¤t) || MatchString("_", pos)) {
1152 : }
1153 52 : *pos = current;
1154 52 : return true;
1155 : }
1156 :
1157 56850 : static bool MatchStringLiteral(InputPosition* pos) {
1158 56850 : InputPosition current = *pos;
1159 56850 : if (MatchString("\"", ¤t)) {
1160 0 : while (
1161 0 : (MatchString("\\", ¤t) && MatchAnyChar(¤t)) ||
1162 0 : MatchChar([](char c) { return c != '"' && c != '\n'; }, ¤t)) {
1163 : }
1164 0 : if (MatchString("\"", ¤t)) {
1165 0 : *pos = current;
1166 0 : return true;
1167 : }
1168 : }
1169 56850 : current = *pos;
1170 56850 : if (MatchString("'", ¤t)) {
1171 7288 : while (
1172 14573 : (MatchString("\\", ¤t) && MatchAnyChar(¤t)) ||
1173 14570 : MatchChar([](char c) { return c != '\'' && c != '\n'; }, ¤t)) {
1174 : }
1175 439 : if (MatchString("'", ¤t)) {
1176 439 : *pos = current;
1177 439 : return true;
1178 : }
1179 : }
1180 : return false;
1181 : }
1182 :
1183 56850 : static bool MatchHexLiteral(InputPosition* pos) {
1184 56850 : InputPosition current = *pos;
1185 56850 : MatchString("-", ¤t);
1186 56850 : if (MatchString("0x", ¤t) && MatchChar(std::isxdigit, ¤t)) {
1187 57 : while (MatchChar(std::isxdigit, ¤t)) {
1188 : }
1189 18 : *pos = current;
1190 18 : return true;
1191 : }
1192 : return false;
1193 : }
1194 :
1195 56850 : static bool MatchDecimalLiteral(InputPosition* pos) {
1196 56850 : InputPosition current = *pos;
1197 : bool found_digit = false;
1198 56850 : MatchString("-", ¤t);
1199 57861 : while (MatchChar(std::isdigit, ¤t)) found_digit = true;
1200 56850 : MatchString(".", ¤t);
1201 56855 : while (MatchChar(std::isdigit, ¤t)) found_digit = true;
1202 56850 : if (!found_digit) return false;
1203 938 : *pos = current;
1204 2814 : if ((MatchString("e", ¤t) || MatchString("E", ¤t)) &&
1205 938 : (MatchString("+", ¤t) || MatchString("-", ¤t) || true) &&
1206 0 : MatchChar(std::isdigit, ¤t)) {
1207 0 : while (MatchChar(std::isdigit, ¤t)) {
1208 : }
1209 0 : *pos = current;
1210 0 : return true;
1211 : }
1212 : return true;
1213 : }
1214 :
1215 31115 : TorqueGrammar() : Grammar(&file) { SetWhitespace(MatchWhitespace); }
1216 :
1217 : // Result: std::string
1218 : Symbol identifier = {Rule({Pattern(MatchIdentifier)}, YieldMatchedInput)};
1219 :
1220 : // Result: Identifier*
1221 : Symbol name = {Rule({&identifier}, MakeIdentifier)};
1222 :
1223 : // Result: std::string
1224 : Symbol intrinsicName = {
1225 : Rule({Pattern(MatchIntrinsicName)}, YieldMatchedInput)};
1226 :
1227 : // Result: std::string
1228 : Symbol stringLiteral = {
1229 : Rule({Pattern(MatchStringLiteral)}, YieldMatchedInput)};
1230 :
1231 : // Result: std::string
1232 : Symbol externalString = {Rule({&stringLiteral}, StringLiteralUnquoteAction)};
1233 :
1234 : // Result: std::string
1235 : Symbol decimalLiteral = {
1236 : Rule({Pattern(MatchDecimalLiteral)}, YieldMatchedInput),
1237 : Rule({Pattern(MatchHexLiteral)}, YieldMatchedInput)};
1238 :
1239 : // Result: TypeList
1240 147 : Symbol* typeList = List<TypeExpression*>(&type, Token(","));
1241 :
1242 : // Result: TypeExpression*
1243 : Symbol simpleType = {
1244 196 : Rule({Token("("), &type, Token(")")}),
1245 245 : Rule({List<std::string>(Sequence({&identifier, Token("::")})),
1246 147 : CheckIf(Token("constexpr")), &identifier},
1247 : MakeBasicTypeExpression),
1248 441 : Rule({Token("builtin"), Token("("), typeList, Token(")"), Token("=>"),
1249 : &simpleType},
1250 : MakeFunctionTypeExpression)};
1251 :
1252 : // Result: TypeExpression*
1253 98 : Symbol type = {Rule({&simpleType}), Rule({&type, Token("|"), &simpleType},
1254 : MakeUnionTypeExpression)};
1255 :
1256 : // Result: GenericParameters
1257 : Symbol genericParameters = {
1258 98 : Rule({Token("<"),
1259 343 : List<Identifier*>(Sequence({&name, Token(":"), Token("type")}),
1260 98 : Token(",")),
1261 98 : Token(">")})};
1262 :
1263 : // Result: TypeList
1264 : Symbol genericSpecializationTypeList = {
1265 245 : Rule({Token("<"), typeList, Token(">")})};
1266 :
1267 : // Result: base::Optional<TypeList>
1268 : Symbol* optionalGenericParameters = Optional<TypeList>(&genericParameters);
1269 :
1270 : Symbol* optionalImplicitParameterList{
1271 294 : TryOrDefault<std::vector<NameAndTypeExpression>>(
1272 196 : Sequence({Token("("), Token("implicit"),
1273 147 : List<NameAndTypeExpression>(&nameAndType, Token(",")),
1274 98 : Token(")")}))};
1275 :
1276 : // Result: ParameterList
1277 : Symbol typeListMaybeVarArgs = {
1278 147 : Rule({optionalImplicitParameterList, Token("("),
1279 343 : List<TypeExpression*>(Sequence({&type, Token(",")})), Token("..."),
1280 98 : Token(")")},
1281 : MakeParameterListFromTypes<true>),
1282 294 : Rule({optionalImplicitParameterList, Token("("), typeList, Token(")")},
1283 : MakeParameterListFromTypes<false>)};
1284 :
1285 : // Result: LabelAndTypes
1286 : Symbol labelParameter = {Rule(
1287 : {&identifier,
1288 343 : TryOrDefault<TypeList>(Sequence({Token("("), typeList, Token(")")}))},
1289 : MakeLabelAndTypes)};
1290 :
1291 : // Result: TypeExpression*
1292 98 : Symbol optionalReturnType = {Rule({Token(":"), &type}),
1293 : Rule({}, MakeVoidType)};
1294 :
1295 : // Result: LabelAndTypesVector
1296 196 : Symbol* optionalLabelList{TryOrDefault<LabelAndTypesVector>(
1297 98 : Sequence({Token("labels"),
1298 196 : NonemptyList<LabelAndTypes>(&labelParameter, Token(","))}))};
1299 :
1300 : // Result: std::vector<Statement*>
1301 196 : Symbol* optionalOtherwise{TryOrDefault<std::vector<Statement*>>(
1302 98 : Sequence({Token("otherwise"),
1303 196 : NonemptyList<Statement*>(&atomarStatement, Token(","))}))};
1304 :
1305 : // Result: NameAndTypeExpression
1306 98 : Symbol nameAndType = {Rule({&name, Token(":"), &type}, MakeNameAndType)};
1307 :
1308 : Symbol* optionalArraySpecifier = {
1309 343 : Optional<std::string>(Sequence({Token("["), &identifier, Token("]")}))};
1310 :
1311 : Symbol classField = {
1312 294 : Rule({CheckIf(Token("weak")), &name, optionalArraySpecifier, Token(":"),
1313 98 : &type, Token(";")},
1314 : MakeClassField)};
1315 :
1316 : Symbol structField = {
1317 196 : Rule({&name, Token(":"), &type, Token(";")}, MakeStructField)};
1318 :
1319 : // Result: ParameterList
1320 : Symbol parameterListNoVararg = {
1321 147 : Rule({optionalImplicitParameterList, Token("("),
1322 196 : List<NameAndTypeExpression>(&nameAndType, Token(",")), Token(")")},
1323 : MakeParameterListFromNameAndTypeList<false>)};
1324 :
1325 : // Result: ParameterList
1326 : Symbol parameterListAllowVararg = {
1327 : Rule({¶meterListNoVararg}),
1328 147 : Rule({optionalImplicitParameterList, Token("("),
1329 196 : NonemptyList<NameAndTypeExpression>(&nameAndType, Token(",")),
1330 294 : Token(","), Token("..."), &identifier, Token(")")},
1331 : MakeParameterListFromNameAndTypeList<true>)};
1332 :
1333 : // Result: std::string
1334 588 : Symbol* OneOf(const std::vector<std::string>& alternatives) {
1335 588 : Symbol* result = NewSymbol();
1336 2499 : for (const std::string& s : alternatives) {
1337 7644 : result->AddRule(Rule({Token(s)}, YieldMatchedInput));
1338 : }
1339 588 : return result;
1340 : }
1341 :
1342 : // Result: Expression*
1343 245 : Symbol* BinaryOperator(Symbol* nextLevel, Symbol* op) {
1344 245 : Symbol* result = NewSymbol();
1345 : *result = {Rule({nextLevel}),
1346 1470 : Rule({result, op, nextLevel}, MakeBinaryOperator)};
1347 245 : return result;
1348 : }
1349 :
1350 : // Result: Expression*
1351 : Symbol* expression = &assignmentExpression;
1352 :
1353 : // Result: IncrementDecrementOperator
1354 : Symbol incrementDecrementOperator = {
1355 98 : Rule({Token("++")},
1356 : YieldIntegralConstant<IncrementDecrementOperator,
1357 : IncrementDecrementOperator::kIncrement>),
1358 98 : Rule({Token("--")},
1359 : YieldIntegralConstant<IncrementDecrementOperator,
1360 : IncrementDecrementOperator::kDecrement>)};
1361 :
1362 : // Result: LocationExpression*
1363 : Symbol identifierExpression = {
1364 245 : Rule({List<std::string>(Sequence({&identifier, Token("::")})), &name,
1365 49 : TryOrDefault<TypeList>(&genericSpecializationTypeList)},
1366 : MakeIdentifierExpression),
1367 : };
1368 :
1369 : // Result: LocationExpression*
1370 : Symbol locationExpression = {
1371 : Rule({&identifierExpression}),
1372 98 : Rule({&primaryExpression, Token("."), &name}, MakeFieldAccessExpression),
1373 245 : Rule({&primaryExpression, Token("["), expression, Token("]")},
1374 : MakeElementAccessExpression)};
1375 :
1376 : // Result: std::vector<Expression*>
1377 : Symbol argumentList = {Rule(
1378 343 : {Token("("), List<Expression*>(expression, Token(",")), Token(")")})};
1379 :
1380 : // Result: Expression*
1381 : Symbol callExpression = {Rule(
1382 49 : {&identifierExpression, &argumentList, optionalOtherwise}, MakeCall)};
1383 :
1384 : Symbol callMethodExpression = {
1385 98 : Rule({&primaryExpression, Token("."), &identifier, &argumentList,
1386 49 : optionalOtherwise},
1387 : MakeMethodCall)};
1388 :
1389 : Symbol initializerList = {Rule(
1390 343 : {Token("{"), List<Expression*>(expression, Token(",")), Token("}")})};
1391 :
1392 : Symbol newExpression = {
1393 98 : Rule({Token("new"), &type, &initializerList}, MakeNew)};
1394 :
1395 : // Result: Expression*
1396 : Symbol intrinsicCallExpression = {Rule(
1397 49 : {&intrinsicName, TryOrDefault<TypeList>(&genericSpecializationTypeList),
1398 : &argumentList},
1399 : MakeIntrinsicCallExpression)};
1400 :
1401 : // Result: Expression*
1402 : Symbol primaryExpression = {
1403 : Rule({&newExpression}),
1404 : Rule({&callExpression}),
1405 : Rule({&callMethodExpression}),
1406 : Rule({&intrinsicCallExpression}),
1407 : Rule({&locationExpression},
1408 : CastParseResult<LocationExpression*, Expression*>),
1409 : Rule({&decimalLiteral}, MakeNumberLiteralExpression),
1410 : Rule({&stringLiteral}, MakeStringLiteralExpression),
1411 : Rule(
1412 245 : {List<std::string>(Sequence({&identifier, Token("::")})), &identifier,
1413 343 : Token("{"), List<Expression*>(expression, Token(",")), Token("}")},
1414 : MakeStructExpression),
1415 245 : Rule({Token("("), expression, Token(")")})};
1416 :
1417 : // Result: Expression*
1418 : Symbol unaryExpression = {
1419 : Rule({&primaryExpression}),
1420 196 : Rule({OneOf({"+", "-", "!", "~"}), &unaryExpression}, MakeUnaryOperator),
1421 : Rule({&incrementDecrementOperator, &locationExpression},
1422 : MakeIncrementDecrementExpressionPrefix),
1423 : Rule({&locationExpression, &incrementDecrementOperator},
1424 : MakeIncrementDecrementExpressionPostfix)};
1425 :
1426 : // Result: Expression*
1427 : Symbol* multiplicativeExpression =
1428 196 : BinaryOperator(&unaryExpression, OneOf({"*", "/", "%"}));
1429 :
1430 : // Result: Expression*
1431 : Symbol* additiveExpression =
1432 196 : BinaryOperator(multiplicativeExpression, OneOf({"+", "-"}));
1433 :
1434 : // Result: Expression*
1435 : Symbol* shiftExpression =
1436 196 : BinaryOperator(additiveExpression, OneOf({"<<", ">>", ">>>"}));
1437 :
1438 : // Do not allow expressions like a < b > c because this is never
1439 : // useful and ambiguous with template parameters.
1440 : // Result: Expression*
1441 : Symbol relationalExpression = {
1442 49 : Rule({shiftExpression}),
1443 294 : Rule({shiftExpression, OneOf({"<", ">", "<=", ">="}), shiftExpression},
1444 : MakeBinaryOperator)};
1445 :
1446 : // Result: Expression*
1447 : Symbol* equalityExpression =
1448 196 : BinaryOperator(&relationalExpression, OneOf({"==", "!="}));
1449 :
1450 : // Result: Expression*
1451 : Symbol* bitwiseExpression =
1452 196 : BinaryOperator(equalityExpression, OneOf({"&", "|"}));
1453 :
1454 : // Result: Expression*
1455 : Symbol logicalAndExpression = {
1456 49 : Rule({bitwiseExpression}),
1457 147 : Rule({&logicalAndExpression, Token("&&"), bitwiseExpression},
1458 : MakeLogicalAndExpression)};
1459 :
1460 : // Result: Expression*
1461 : Symbol logicalOrExpression = {
1462 : Rule({&logicalAndExpression}),
1463 98 : Rule({&logicalOrExpression, Token("||"), &logicalAndExpression},
1464 : MakeLogicalOrExpression)};
1465 :
1466 : // Result: Expression*
1467 : Symbol conditionalExpression = {
1468 : Rule({&logicalOrExpression}),
1469 245 : Rule({&logicalOrExpression, Token("?"), expression, Token(":"),
1470 : &conditionalExpression},
1471 : MakeConditionalExpression)};
1472 :
1473 : // Result: base::Optional<std::string>
1474 : Symbol assignmentOperator = {
1475 98 : Rule({Token("=")}, YieldDefaultValue<base::Optional<std::string>>),
1476 196 : Rule({OneOf({"*=", "/=", "%=", "+=", "-=", "<<=", ">>=", ">>>=", "&=",
1477 : "^=", "|="})},
1478 : ExtractAssignmentOperator)};
1479 :
1480 : // Result: Expression*
1481 : Symbol assignmentExpression = {
1482 : Rule({&conditionalExpression}),
1483 : Rule({&locationExpression, &assignmentOperator, &assignmentExpression},
1484 : MakeAssignmentExpression)};
1485 :
1486 : // Result: Statement*
1487 245 : Symbol block = {Rule({CheckIf(Token("deferred")), Token("{"),
1488 147 : List<Statement*>(&statement), Token("}")},
1489 : MakeBlockStatement)};
1490 :
1491 : // Result: LabelBlock*
1492 : Symbol labelBlock = {
1493 98 : Rule({Token("label"), &identifier,
1494 49 : TryOrDefault<ParameterList>(¶meterListNoVararg), &block},
1495 : MakeLabelBlock)};
1496 :
1497 : Symbol catchBlock = {
1498 294 : Rule({Token("catch"), Token("("), &identifier, Token(")"), &block},
1499 : MakeCatchBlock)};
1500 :
1501 : // Result: ExpressionWithSource
1502 49 : Symbol expressionWithSource = {Rule({expression}, MakeExpressionWithSource)};
1503 :
1504 : // Result: RangeExpression
1505 : Symbol rangeSpecifier = {
1506 245 : Rule({Token("["), Optional<Expression*>(expression), Token(":"),
1507 147 : Optional<Expression*>(expression), Token("]")},
1508 : MakeRangeExpression)};
1509 :
1510 : Symbol* optionalTypeSpecifier =
1511 245 : Optional<TypeExpression*>(Sequence({Token(":"), &type}));
1512 :
1513 : // Result: Statement*
1514 : Symbol varDeclaration = {
1515 245 : Rule({OneOf({"let", "const"}), &name, optionalTypeSpecifier},
1516 : MakeVarDeclarationStatement)};
1517 :
1518 : // Result: Statement*
1519 : Symbol varDeclarationWithInitialization = {
1520 294 : Rule({OneOf({"let", "const"}), &name, optionalTypeSpecifier, Token("="),
1521 49 : expression},
1522 : MakeVarDeclarationStatement)};
1523 :
1524 : // Result: Statement*
1525 : Symbol atomarStatement = {
1526 49 : Rule({expression}, MakeExpressionStatement),
1527 147 : Rule({Token("return"), Optional<Expression*>(expression)},
1528 : MakeReturnStatement),
1529 98 : Rule({Token("tail"), &callExpression}, MakeTailCallStatement),
1530 98 : Rule({Token("break")}, MakeBreakStatement),
1531 98 : Rule({Token("continue")}, MakeContinueStatement),
1532 98 : Rule({Token("goto"), &identifier,
1533 49 : TryOrDefault<std::vector<Expression*>>(&argumentList)},
1534 : MakeGotoStatement),
1535 196 : Rule({OneOf({"debug", "unreachable"})}, MakeDebugStatement)};
1536 :
1537 : // Result: Statement*
1538 : Symbol statement = {
1539 : Rule({&block}),
1540 98 : Rule({&atomarStatement, Token(";")}),
1541 98 : Rule({&varDeclaration, Token(";")}),
1542 98 : Rule({&varDeclarationWithInitialization, Token(";")}),
1543 392 : Rule({Token("if"), CheckIf(Token("constexpr")), Token("("), expression,
1544 98 : Token(")"), &statement,
1545 245 : Optional<Statement*>(Sequence({Token("else"), &statement}))},
1546 : MakeIfStatement),
1547 : Rule(
1548 : {
1549 343 : Token("typeswitch"), Token("("), expression, Token(")"),
1550 196 : Token("{"), NonemptyList<TypeswitchCase>(&typeswitchCase),
1551 98 : Token("}"),
1552 : },
1553 : MakeTypeswitchStatement),
1554 196 : Rule({Token("try"), &block, List<LabelBlock*>(&labelBlock),
1555 : Optional<LabelBlock*>(&catchBlock)},
1556 : MakeTryLabelExpression),
1557 245 : Rule({OneOf({"assert", "check"}), Token("("), &expressionWithSource,
1558 196 : Token(")"), Token(";")},
1559 : MakeAssertStatement),
1560 343 : Rule({Token("while"), Token("("), expression, Token(")"), &statement},
1561 : MakeWhileStatement),
1562 343 : Rule({Token("for"), Token("("), &varDeclaration, Token("of"), expression,
1563 98 : Optional<RangeExpression>(&rangeSpecifier), Token(")"), &statement},
1564 : MakeForOfLoopStatement),
1565 196 : Rule({Token("for"), Token("("),
1566 98 : Optional<Statement*>(&varDeclarationWithInitialization), Token(";"),
1567 147 : Optional<Expression*>(expression), Token(";"),
1568 147 : Optional<Expression*>(expression), Token(")"), &statement},
1569 : MakeForLoopStatement)};
1570 :
1571 : // Result: TypeswitchCase
1572 : Symbol typeswitchCase = {
1573 196 : Rule({Token("case"), Token("("),
1574 245 : Optional<std::string>(Sequence({&identifier, Token(":")})), &type,
1575 196 : Token(")"), Token(":"), &block},
1576 : MakeTypeswitchCase)};
1577 :
1578 : // Result: base::Optional<Statement*>
1579 : Symbol optionalBody = {
1580 : Rule({&block}, CastParseResult<Statement*, base::Optional<Statement*>>),
1581 98 : Rule({Token(";")}, YieldDefaultValue<base::Optional<Statement*>>)};
1582 :
1583 : // Result: Declaration*
1584 : Symbol method = {Rule(
1585 147 : {CheckIf(Token("transitioning")),
1586 245 : Optional<std::string>(Sequence({Token("operator"), &externalString})),
1587 : &identifier, ¶meterListNoVararg, &optionalReturnType,
1588 49 : optionalLabelList, &block},
1589 : MakeMethodDeclaration)};
1590 :
1591 : // Result: Declaration*
1592 : Symbol declaration = {
1593 343 : Rule({Token("const"), &name, Token(":"), &type, Token("="), expression,
1594 98 : Token(";")},
1595 : MakeConstDeclaration),
1596 294 : Rule({Token("const"), &name, Token(":"), &type, Token("generates"),
1597 98 : &externalString, Token(";")},
1598 : MakeExternConstDeclaration),
1599 294 : Rule({CheckIf(Token("extern")), CheckIf(Token("transient")),
1600 98 : Token("class"), &name,
1601 245 : Optional<std::string>(Sequence({Token("extends"), &identifier})),
1602 147 : Optional<std::string>(
1603 98 : Sequence({Token("generates"), &externalString})),
1604 196 : Token("{"), List<Declaration*>(&method),
1605 147 : List<ClassFieldExpression>(&classField), Token("}")},
1606 : MakeClassDeclaration),
1607 294 : Rule({Token("struct"), &name, Token("{"), List<Declaration*>(&method),
1608 147 : List<StructFieldExpression>(&structField), Token("}")},
1609 : MakeStructDeclaration),
1610 245 : Rule({CheckIf(Token("transient")), Token("type"), &name,
1611 245 : Optional<Identifier*>(Sequence({Token("extends"), &name})),
1612 147 : Optional<std::string>(
1613 98 : Sequence({Token("generates"), &externalString})),
1614 147 : Optional<std::string>(
1615 98 : Sequence({Token("constexpr"), &externalString})),
1616 98 : Token(";")},
1617 : MakeTypeDeclaration),
1618 294 : Rule({Token("type"), &name, Token("="), &type, Token(";")},
1619 : MakeTypeAliasDeclaration),
1620 98 : Rule({Token("intrinsic"), &intrinsicName,
1621 49 : TryOrDefault<GenericParameters>(&genericParameters),
1622 98 : ¶meterListNoVararg, &optionalReturnType, Token(";")},
1623 : MakeIntrinsicDeclaration),
1624 245 : Rule({Token("extern"), CheckIf(Token("transitioning")),
1625 147 : Optional<std::string>(
1626 98 : Sequence({Token("operator"), &externalString})),
1627 98 : Token("macro"),
1628 245 : Optional<std::string>(Sequence({&identifier, Token("::")})),
1629 49 : &identifier, TryOrDefault<GenericParameters>(&genericParameters),
1630 49 : &typeListMaybeVarArgs, &optionalReturnType, optionalLabelList,
1631 98 : Token(";")},
1632 : MakeExternalMacro),
1633 245 : Rule({Token("extern"), CheckIf(Token("transitioning")),
1634 245 : CheckIf(Token("javascript")), Token("builtin"), &identifier,
1635 49 : TryOrDefault<GenericParameters>(&genericParameters),
1636 98 : &typeListMaybeVarArgs, &optionalReturnType, Token(";")},
1637 : MakeExternalBuiltin),
1638 : Rule(
1639 343 : {Token("extern"), CheckIf(Token("transitioning")), Token("runtime"),
1640 98 : &identifier, &typeListMaybeVarArgs, &optionalReturnType, Token(";")},
1641 : MakeExternalRuntime),
1642 147 : Rule({CheckIf(Token("transitioning")),
1643 147 : Optional<std::string>(
1644 98 : Sequence({Token("operator"), &externalString})),
1645 98 : Token("macro"), &identifier,
1646 49 : TryOrDefault<GenericParameters>(&genericParameters),
1647 49 : ¶meterListNoVararg, &optionalReturnType, optionalLabelList,
1648 : &optionalBody},
1649 : MakeTorqueMacroDeclaration),
1650 294 : Rule({CheckIf(Token("transitioning")), CheckIf(Token("javascript")),
1651 98 : Token("builtin"), &identifier,
1652 49 : TryOrDefault<GenericParameters>(&genericParameters),
1653 : ¶meterListAllowVararg, &optionalReturnType, &optionalBody},
1654 : MakeTorqueBuiltinDeclaration),
1655 : Rule({&identifier, &genericSpecializationTypeList,
1656 49 : ¶meterListAllowVararg, &optionalReturnType, optionalLabelList,
1657 : &block},
1658 : MakeSpecializationDeclaration),
1659 98 : Rule({Token("#include"), &externalString}, MakeCppIncludeDeclaration)};
1660 :
1661 : // Result: Declaration*
1662 : Symbol namespaceDeclaration = {
1663 196 : Rule({Token("namespace"), &identifier, Token("{"),
1664 147 : List<Declaration*>(&declaration), Token("}")},
1665 : MakeNamespaceDeclaration)};
1666 :
1667 : Symbol file = {Rule({&file, &namespaceDeclaration}, AddGlobalDeclaration),
1668 : Rule({&file, &declaration}, AddGlobalDeclaration), Rule({})};
1669 : };
1670 :
1671 : } // namespace
1672 :
1673 98 : void ParseTorque(const std::string& input) { TorqueGrammar().Parse(input); }
1674 :
1675 : } // namespace torque
1676 : } // namespace internal
1677 6150 : } // namespace v8
|