HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Android 10
|
10.0.0_r6
下载
查看原文件
收藏
根目录
external
v8
src
torque
torque-parser.cc
// Copyright 2018 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include
#include "src/torque/earley-parser.h" #include "src/torque/torque-parser.h" #include "src/torque/utils.h" namespace v8 { namespace internal { namespace torque { DEFINE_CONTEXTUAL_VARIABLE(CurrentAst); using TypeList = std::vector
; using GenericParameters = std::vector
; struct ExpressionWithSource { Expression* expression; std::string source; }; struct TypeswitchCase { SourcePosition pos; base::Optional
name; TypeExpression* type; Statement* block; }; enum class ParseResultHolderBase::TypeId { kStdString, kBool, kStdVectorOfString, kExpressionPtr, kLocationExpressionPtr, kStatementPtr, kDeclarationPtr, kTypeExpressionPtr, kLabelBlockPtr, kNameAndTypeExpression, kStdVectorOfNameAndTypeExpression, kIncrementDecrementOperator, kOptionalStdString, kStdVectorOfStatementPtr, kStdVectorOfDeclarationPtr, kStdVectorOfExpressionPtr, kExpressionWithSource, kParameterList, kRangeExpression, kOptionalRangeExpression, kTypeList, kOptionalTypeList, kLabelAndTypes, kStdVectorOfLabelAndTypes, kStdVectorOfLabelBlockPtr, kOptionalStatementPtr, kOptionalExpressionPtr, kTypeswitchCase, kStdVectorOfTypeswitchCase }; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kStdString; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kBool; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfString; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kDeclarationPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kTypeExpressionPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kLabelBlockPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kExpressionPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kLocationExpressionPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kStatementPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kNameAndTypeExpression; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfNameAndTypeExpression; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kIncrementDecrementOperator; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kOptionalStdString; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfStatementPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfDeclarationPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfExpressionPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kExpressionWithSource; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kParameterList; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kRangeExpression; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kOptionalRangeExpression; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kTypeList; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kOptionalTypeList; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kLabelAndTypes; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfLabelAndTypes; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfLabelBlockPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kOptionalStatementPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kOptionalExpressionPtr; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
::id = ParseResultTypeId::kTypeswitchCase; template <> V8_EXPORT_PRIVATE const ParseResultTypeId ParseResultHolder
>::id = ParseResultTypeId::kStdVectorOfTypeswitchCase; namespace { base::Optional
AddGlobalDeclaration( ParseResultIterator* child_results) { auto declaration = child_results->NextAs
(); CurrentAst::Get().declarations().push_back(declaration); return base::nullopt; } template
T* MakeNode(Args... args) { return CurrentAst::Get().AddNode(std::unique_ptr
( new T(CurrentSourcePosition::Get(), std::move(args)...))); } base::Optional
MakeCall(ParseResultIterator* child_results) { auto callee = child_results->NextAs
(); auto generic_args = child_results->NextAs
(); auto args = child_results->NextAs
>(); auto labels = child_results->NextAs
>(); Expression* result = MakeNode
(callee, false, generic_args, args, labels); return ParseResult{result}; } base::Optional
MakeBinaryOperator( ParseResultIterator* child_results) { auto left = child_results->NextAs
(); auto op = child_results->NextAs
(); auto right = child_results->NextAs
(); Expression* result = MakeNode
( op, true, TypeList{}, std::vector
{left, right}, std::vector
{}); return ParseResult{result}; } base::Optional
MakeUnaryOperator( ParseResultIterator* child_results) { auto op = child_results->NextAs
(); auto e = child_results->NextAs
(); Expression* result = MakeNode
(op, true, TypeList{}, std::vector
{e}, std::vector
{}); return ParseResult{result}; } template
base::Optional
MakeParameterListFromTypes( ParseResultIterator* child_results) { auto types = child_results->NextAs
(); ParameterList result; result.types = std::move(types); result.has_varargs = has_varargs; return ParseResult{std::move(result)}; } template
base::Optional
MakeParameterListFromNameAndTypeList( ParseResultIterator* child_results) { auto params = child_results->NextAs
>(); std::string arguments_variable = ""; if (child_results->HasNext()) { arguments_variable = child_results->NextAs
(); } ParameterList result; for (NameAndTypeExpression& pair : params) { result.names.push_back(std::move(pair.name)); result.types.push_back(pair.type); } result.has_varargs = has_varargs; result.arguments_variable = arguments_variable; return ParseResult{std::move(result)}; } base::Optional
MakeAssertStatement( ParseResultIterator* child_results) { auto kind = child_results->NextAs
(); auto expr_with_source = child_results->NextAs
(); DCHECK(kind == "assert" || kind == "check"); Statement* result = MakeNode
( kind == "assert", expr_with_source.expression, expr_with_source.source); return ParseResult{result}; } base::Optional
MakeDebugStatement( ParseResultIterator* child_results) { auto kind = child_results->NextAs
(); DCHECK(kind == "unreachable" || kind == "debug"); Statement* result = MakeNode
(kind, kind == "unreachable"); return ParseResult{result}; } base::Optional
MakeVoidType(ParseResultIterator* child_results) { TypeExpression* result = MakeNode
(false, "void"); return ParseResult{result}; } base::Optional
MakeExternalMacro( ParseResultIterator* child_results) { auto operator_name = child_results->NextAs
>(); auto name = child_results->NextAs
(); auto generic_parameters = child_results->NextAs
(); auto args = child_results->NextAs
(); auto return_type = child_results->NextAs
(); auto labels = child_results->NextAs
(); MacroDeclaration* macro = MakeNode
( name, operator_name, args, return_type, labels); Declaration* result; if (generic_parameters.empty()) { result = MakeNode
(macro, nullptr); } else { result = MakeNode
(macro, generic_parameters); } return ParseResult{result}; } base::Optional
MakeTorqueMacroDeclaration( ParseResultIterator* child_results) { auto operator_name = child_results->NextAs
>(); auto name = child_results->NextAs
(); auto generic_parameters = child_results->NextAs
(); auto args = child_results->NextAs
(); auto return_type = child_results->NextAs
(); auto labels = child_results->NextAs
(); auto body = child_results->NextAs
>(); MacroDeclaration* macro = MakeNode
( name, operator_name, args, return_type, labels); Declaration* result; if (generic_parameters.empty()) { if (!body) ReportError("A non-generic declaration needs a body."); result = MakeNode
(macro, *body); } else { result = MakeNode
(macro, generic_parameters, body); } return ParseResult{result}; } base::Optional
MakeTorqueBuiltinDeclaration( ParseResultIterator* child_results) { auto javascript_linkage = child_results->NextAs
(); auto name = child_results->NextAs
(); auto generic_parameters = child_results->NextAs
(); auto args = child_results->NextAs
(); auto return_type = child_results->NextAs
(); auto body = child_results->NextAs
>(); BuiltinDeclaration* builtin = MakeNode
( javascript_linkage, name, args, return_type); Declaration* result; if (generic_parameters.empty()) { if (!body) ReportError("A non-generic declaration needs a body."); result = MakeNode
(builtin, *body); } else { result = MakeNode
(builtin, generic_parameters, body); } return ParseResult{result}; } base::Optional
MakeConstDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto type = child_results->NextAs
(); auto expression = child_results->NextAs
(); Declaration* result = MakeNode
(std::move(name), type, expression); return ParseResult{result}; } base::Optional
MakeExternConstDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto type = child_results->NextAs
(); auto literal = child_results->NextAs
(); Declaration* result = MakeNode
(std::move(name), type, std::move(literal)); return ParseResult{result}; } base::Optional
MakeTypeAliasDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto type = child_results->NextAs
(); Declaration* result = MakeNode
(std::move(name), type); return ParseResult{result}; } base::Optional
MakeTypeDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto extends = child_results->NextAs
>(); auto generates = child_results->NextAs
>(); auto constexpr_generates = child_results->NextAs
>(); Declaration* result = MakeNode
( std::move(name), std::move(extends), std::move(generates), std::move(constexpr_generates)); return ParseResult{result}; } base::Optional
MakeExplicitModuleDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto declarations = child_results->NextAs
>(); Declaration* result = MakeNode
( std::move(name), std::move(declarations)); return ParseResult{result}; } base::Optional
MakeSpecializationDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto generic_parameters = child_results->NextAs
>(); auto parameters = child_results->NextAs
(); auto return_type = child_results->NextAs
(); auto labels = child_results->NextAs
(); auto body = child_results->NextAs
(); Declaration* result = MakeNode
( std::move(name), std::move(generic_parameters), std::move(parameters), return_type, std::move(labels), body); return ParseResult{result}; } base::Optional
MakeStructDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto fields = child_results->NextAs
>(); Declaration* result = MakeNode
(std::move(name), std::move(fields)); return ParseResult{result}; } base::Optional
MakeExternalBuiltin( ParseResultIterator* child_results) { auto js_linkage = child_results->NextAs
(); auto name = child_results->NextAs
(); auto generic_parameters = child_results->NextAs
(); auto args = child_results->NextAs
(); auto return_type = child_results->NextAs
(); BuiltinDeclaration* builtin = MakeNode
(js_linkage, name, args, return_type); Declaration* result; if (generic_parameters.empty()) { result = MakeNode
(builtin, nullptr); } else { result = MakeNode
(builtin, generic_parameters); } return ParseResult{result}; } base::Optional
MakeExternalRuntime( ParseResultIterator* child_results) { auto name = child_results->NextAs
(); auto args = child_results->NextAs
(); auto return_type = child_results->NextAs
(); ExternalRuntimeDeclaration* runtime = MakeNode
(name, args, return_type); Declaration* result = MakeNode
(runtime, nullptr); return ParseResult{result}; } base::Optional
StringLiteralUnquoteAction( ParseResultIterator* child_results) { return ParseResult{ StringLiteralUnquote(child_results->NextAs
())}; } base::Optional
MakeBasicTypeExpression( ParseResultIterator* child_results) { auto is_constexpr = child_results->NextAs
(); auto name = child_results->NextAs
(); TypeExpression* result = MakeNode
(is_constexpr, std::move(name)); return ParseResult{result}; } base::Optional
MakeFunctionTypeExpression( ParseResultIterator* child_results) { auto parameters = child_results->NextAs
>(); auto return_type = child_results->NextAs
(); TypeExpression* result = MakeNode
(std::move(parameters), return_type); return ParseResult{result}; } base::Optional
MakeUnionTypeExpression( ParseResultIterator* child_results) { auto a = child_results->NextAs
(); auto b = child_results->NextAs
(); TypeExpression* result = MakeNode
(a, b); return ParseResult{result}; } base::Optional
MakeExpressionStatement( ParseResultIterator* child_results) { auto expression = child_results->NextAs
(); Statement* result = MakeNode
(expression); return ParseResult{result}; } base::Optional
MakeIfStatement( ParseResultIterator* child_results) { auto is_constexpr = child_results->NextAs
(); auto condition = child_results->NextAs
(); auto if_true = child_results->NextAs
(); auto if_false = child_results->NextAs
>(); if (if_false && !(BlockStatement::DynamicCast(if_true) && (BlockStatement::DynamicCast(*if_false) || IfStatement::DynamicCast(*if_false)))) { ReportError("if-else statements require curly braces"); } Statement* result = MakeNode
(is_constexpr, condition, if_true, if_false); return ParseResult{result}; } base::Optional
MakeTypeswitchStatement( ParseResultIterator* child_results) { auto expression = child_results->NextAs
(); auto cases = child_results->NextAs
>(); CurrentSourcePosition::Scope current_source_position( child_results->matched_input().pos); // typeswitch (expression) case (x1 : T1) { // ...b1 // } case (x2 : T2) { // ...b2 // } case (x3 : T3) { // ...b3 // } // // desugars to // // { // const _value = expression; // try { // const x1 : T1 = cast
(_value) otherwise _NextCase; // ...b1 // } label _NextCase { // try { // const x2 : T2 = cast
(%assume_impossible
(_value)); // ...b2 // } label _NextCase { // const x3 : T3 = %assume_impossible
(_value); // ...b3 // } // } // } BlockStatement* current_block = MakeNode
(); Statement* result = current_block; { CurrentSourcePosition::Scope current_source_position(expression->pos); current_block->statements.push_back(MakeNode
( true, "_value", base::nullopt, expression)); } TypeExpression* accumulated_types; for (size_t i = 0; i < cases.size(); ++i) { CurrentSourcePosition::Scope current_source_position(cases[i].pos); Expression* value = MakeNode
("_value"); if (i >= 1) { value = MakeNode
(accumulated_types, value); } BlockStatement* case_block; if (i < cases.size() - 1) { value = MakeNode
( "cast", false, std::vector
{cases[i].type}, std::vector
{value}, std::vector
{"_NextCase"}); case_block = MakeNode
(); } else { case_block = current_block; } std::string name = "_case_value"; if (cases[i].name) name = *cases[i].name; case_block->statements.push_back( MakeNode
(true, name, cases[i].type, value)); case_block->statements.push_back(cases[i].block); if (i < cases.size() - 1) { BlockStatement* next_block = MakeNode
(); current_block->statements.push_back(MakeNode
( case_block, std::vector
{MakeNode
( "_NextCase", ParameterList::Empty(), next_block)})); current_block = next_block; } accumulated_types = i > 0 ? MakeNode
(accumulated_types, cases[i].type) : cases[i].type; } return ParseResult{result}; } base::Optional
MakeTypeswitchCase( ParseResultIterator* child_results) { auto name = child_results->NextAs
>(); auto type = child_results->NextAs
(); auto block = child_results->NextAs
(); return ParseResult{TypeswitchCase{child_results->matched_input().pos, std::move(name), type, block}}; } base::Optional
MakeWhileStatement( ParseResultIterator* child_results) { auto condition = child_results->NextAs
(); auto body = child_results->NextAs
(); Statement* result = MakeNode
(condition, body); return ParseResult{result}; } base::Optional
MakeReturnStatement( ParseResultIterator* child_results) { auto value = child_results->NextAs
>(); Statement* result = MakeNode
(value); return ParseResult{result}; } base::Optional
MakeTailCallStatement( ParseResultIterator* child_results) { auto value = child_results->NextAs
(); Statement* result = MakeNode
(CallExpression::cast(value)); return ParseResult{result}; } base::Optional
MakeVarDeclarationStatement( ParseResultIterator* child_results) { auto kind = child_results->NextAs
(); bool const_qualified = kind == "const"; if (!const_qualified) DCHECK_EQ("let", kind); auto name = child_results->NextAs
(); auto type = child_results->NextAs
(); base::Optional
initializer; if (child_results->HasNext()) initializer = child_results->NextAs
(); Statement* result = MakeNode
( const_qualified, std::move(name), type, initializer); return ParseResult{result}; } base::Optional
MakeBreakStatement( ParseResultIterator* child_results) { Statement* result = MakeNode
(); return ParseResult{result}; } base::Optional
MakeContinueStatement( ParseResultIterator* child_results) { Statement* result = MakeNode
(); return ParseResult{result}; } base::Optional
MakeGotoStatement( ParseResultIterator* child_results) { auto label = child_results->NextAs
(); auto arguments = child_results->NextAs
>(); Statement* result = MakeNode
(std::move(label), std::move(arguments)); return ParseResult{result}; } base::Optional
MakeBlockStatement( ParseResultIterator* child_results) { auto deferred = child_results->NextAs
(); auto statements = child_results->NextAs
>(); Statement* result = MakeNode
(deferred, std::move(statements)); return ParseResult{result}; } base::Optional
MakeTryLabelStatement( ParseResultIterator* child_results) { auto try_block = child_results->NextAs
(); auto label_blocks = child_results->NextAs
>(); Statement* result = MakeNode
(try_block, std::move(label_blocks)); return ParseResult{result}; } base::Optional
MakeForOfLoopStatement( ParseResultIterator* child_results) { auto var_decl = child_results->NextAs
(); auto iterable = child_results->NextAs
(); auto range = child_results->NextAs
>(); auto body = child_results->NextAs
(); Statement* result = MakeNode
(var_decl, iterable, range, body); return ParseResult{result}; } base::Optional
MakeForLoopStatement( ParseResultIterator* child_results) { auto var_decl = child_results->NextAs
>(); auto test = child_results->NextAs
>(); auto action = child_results->NextAs
>(); auto body = child_results->NextAs
(); Statement* result = MakeNode
(var_decl, test, action, body); return ParseResult{result}; } base::Optional
MakeLabelBlock(ParseResultIterator* child_results) { auto label = child_results->NextAs
(); auto parameters = child_results->NextAs
(); auto body = child_results->NextAs
(); LabelBlock* result = MakeNode
(std::move(label), std::move(parameters), body); return ParseResult{result}; } base::Optional