diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index 991b26a77f9d0382c7cc7770ea83c5d739af7bc9..bef7e6af5f2435c0ec8489e277e09e11c6aba42f 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -83,6 +83,7 @@ libes2panda_sources = [ "checker/types/ets/etsObjectType.cpp", "checker/types/ets/etsStringType.cpp", "checker/types/ets/etsTypeParameter.cpp", + "checker/types/ets/etsUnionType.cpp", "checker/types/ets/etsVoidType.cpp", "checker/types/ets/floatType.cpp", "checker/types/ets/intType.cpp", @@ -138,6 +139,7 @@ libes2panda_sources = [ "compiler/core/JSCompiler.cpp", "compiler/core/JSemitter.cpp", "compiler/core/codeGen.cpp", + "compiler/core/ASTVerifier.cpp", "compiler/core/compileJob.cpp", "compiler/core/compileQueue.cpp", "compiler/core/compilerContext.cpp", @@ -164,6 +166,7 @@ libes2panda_sources = [ "compiler/lowering/checkerPhase.cpp", "compiler/lowering/ets/generateDeclarations.cpp", "compiler/lowering/ets/opAssignment.cpp", + "compiler/lowering/ets/unionLowering.cpp", "compiler/lowering/phase.cpp", "compiler/lowering/util.cpp", "es2panda.cpp", @@ -201,6 +204,7 @@ libes2panda_sources = [ "ir/ets/etsStructDeclaration.cpp", "ir/ets/etsTypeReference.cpp", "ir/ets/etsTypeReferencePart.cpp", + "ir/ets/etsUnionType.cpp", "ir/ets/etsWildcardType.cpp", "ir/expression.cpp", "ir/expressions/arrayExpression.cpp", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index e49880048b39a7862c1959de208a79d92c2719a7..215c2e874ae52028372aba47f5b28bb74221d2c4 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -102,6 +102,7 @@ set(ES2PANDA_LIB_SRC compiler/base/literals.cpp compiler/base/lreference.cpp compiler/base/optionalChain.cpp + compiler/core/ASTVerifier.cpp compiler/core/codeGen.cpp compiler/core/compileJob.cpp compiler/core/compileQueue.cpp @@ -137,6 +138,7 @@ set(ES2PANDA_LIB_SRC compiler/lowering/util.cpp compiler/lowering/ets/generateDeclarations.cpp compiler/lowering/ets/opAssignment.cpp + compiler/lowering/ets/unionLowering.cpp ir/astDump.cpp ir/astNode.cpp ir/irnode.cpp @@ -240,6 +242,7 @@ set(ES2PANDA_LIB_SRC ir/ets/etsScript.cpp ir/ets/etsTypeReference.cpp ir/ets/etsTypeReferencePart.cpp + ir/ets/etsUnionType.cpp ir/ets/etsWildcardType.cpp ir/ets/etsImportSource.cpp ir/ts/tsAnyKeyword.cpp @@ -368,6 +371,7 @@ set(ES2PANDA_LIB_SRC checker/types/ets/etsObjectType.cpp checker/types/ets/etsStringType.cpp checker/types/ets/etsTypeParameter.cpp + checker/types/ets/etsUnionType.cpp checker/types/ets/etsVoidType.cpp checker/types/ets/wildcardType.cpp checker/types/ets/etsAsyncFuncReturnType.cpp diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 2e05e528f30d1b428c49aef664379507c47a488b..1256a36fc581c0dd97422c945681b2efaa545c79 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -16,6 +16,8 @@ #include "binder/binder.h" #include "binder/ETSBinder.h" +#include "checker/ETSchecker.h" +#include "checker/ets/castingContext.h" #include "checker/ets/typeRelationContext.h" #include "ir/base/catchClause.h" #include "ir/base/classProperty.h" @@ -233,37 +235,61 @@ checker::Type *ETSAnalyzer::Check(ir::ETSPackageDeclaration *st) const checker::Type *ETSAnalyzer::Check(ir::ETSParameterExpression *expr) const { - (void)expr; - UNREACHABLE(); + ETSChecker *checker = GetETSChecker(); + if (expr->TsType() == nullptr) { + checker::Type *param_type; + + if (expr->Ident()->TsType() != nullptr) { + param_type = expr->Ident()->TsType(); + } else { + param_type = !expr->IsRestParameter() ? expr->Ident()->Check(checker) : expr->spread_->Check(checker); + if (expr->IsDefault()) { + [[maybe_unused]] auto *const init_type = expr->Initializer()->Check(checker); + // TODO(ttamas) : fix this aftet nullable fix + // const checker::AssignmentContext ctx(checker->Relation(), initializer_, init_type, name_type, + // initializer_->Start(), + // {"Initializers type is not assignable to the target type"}); + } + } + + expr->SetTsType(param_type); + } + + return expr->TsType(); } -checker::Type *ETSAnalyzer::Check(ir::ETSPrimitiveType *node) const +checker::Type *ETSAnalyzer::Check([[maybe_unused]] ir::ETSPrimitiveType *node) const { - (void)node; - UNREACHABLE(); + return nullptr; } checker::Type *ETSAnalyzer::Check(ir::ETSStructDeclaration *node) const { - (void)node; - UNREACHABLE(); + ETSChecker *checker = GetETSChecker(); + node->Definition()->Check(checker); + return nullptr; } checker::Type *ETSAnalyzer::Check(ir::ETSTypeReference *node) const { - (void)node; - UNREACHABLE(); + ETSChecker *checker = GetETSChecker(); + return node->GetType(checker); } checker::Type *ETSAnalyzer::Check(ir::ETSTypeReferencePart *node) const +{ + ETSChecker *checker = GetETSChecker(); + return node->GetType(checker); +} + +checker::Type *ETSAnalyzer::Check(ir::ETSUnionType *node) const { (void)node; UNREACHABLE(); } -checker::Type *ETSAnalyzer::Check(ir::ETSWildcardType *node) const +checker::Type *ETSAnalyzer::Check([[maybe_unused]] ir::ETSWildcardType *node) const { - (void)node; UNREACHABLE(); } // compile methods for EXPRESSIONS in alphabetical order @@ -275,8 +301,70 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const { - (void)expr; - UNREACHABLE(); + ETSChecker *checker = GetETSChecker(); + if (expr->TsType() != nullptr) { + return expr->TsType(); + } + + auto *func_type = checker->BuildFunctionSignature(expr->Function(), false); + + if (expr->Function()->IsAsyncFunc()) { + auto *ret_type = static_cast(expr->Function()->Signature()->ReturnType()); + if (ret_type->AssemblerName() != checker->GlobalBuiltinPromiseType()->AssemblerName()) { + checker->ThrowTypeError("Return type of async lambda must be 'Promise'", expr->Function()->Start()); + } + } + + checker::ScopeContext scope_ctx(checker, expr->Function()->Scope()); + + if (checker->HasStatus(checker::CheckerStatus::IN_INSTANCE_EXTENSION_METHOD)) { + /* + example code: + ``` + class A { + prop:number + } + function A.method() { + let a = () => { + console.println(this.prop) + } + } + ``` + here the enclosing class of arrow function should be Class A + */ + checker->Context().SetContainingClass( + checker->Scope()->Find(binder::Binder::MANDATORY_PARAM_THIS).variable->TsType()->AsETSObjectType()); + } + + checker::SavedCheckerContext saved_context(checker, checker->Context().Status(), + checker->Context().ContainingClass()); + checker->AddStatus(checker::CheckerStatus::IN_LAMBDA); + checker->Context().SetContainingSignature(func_type->CallSignatures()[0]); + + auto *body_type = expr->Function()->Body()->Check(checker); + + if (expr->Function()->Body()->IsExpression()) { + if (expr->Function()->ReturnTypeAnnotation() == nullptr) { + func_type->CallSignatures()[0]->SetReturnType(body_type); + } + + checker::AssignmentContext( + checker->Relation(), expr->Function()->Body()->AsExpression(), body_type, + func_type->CallSignatures()[0]->ReturnType(), expr->Function()->Start(), + {"Return statements return type is not compatible with the containing functions return type"}, + checker::TypeRelationFlag::DIRECT_RETURN); + } + + checker->Context().SetContainingSignature(nullptr); + checker->CheckCapturedVariables(); + + for (auto [var, _] : checker->Context().CapturedVars()) { + (void)_; + expr->CapturedVars().push_back(var); + } + + expr->SetTsType(func_type); + return expr->TsType(); } checker::Type *ETSAnalyzer::Check(ir::AssignmentExpression *expr) const diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 1cdd1f1694880378644a32a14612852ed30af37d..3fe77f6af7ce3b0e892598ce1ac32118886e33a9 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -227,6 +227,7 @@ public: CharType *CreateCharType(char16_t value); ETSStringType *CreateETSStringLiteralType(util::StringView value); ETSArrayType *CreateETSArrayType(Type *element_type); + Type *CreateETSUnionType(ArenaVector &&constituent_types); ETSFunctionType *CreateETSFunctionType(Signature *signature); ETSFunctionType *CreateETSFunctionType(Signature *signature, util::StringView name); ETSFunctionType *CreateETSFunctionType(ir::ScriptFunction *func, Signature *signature, util::StringView name); @@ -250,7 +251,7 @@ public: // Arithmetic Type *NegateNumericType(Type *type, ir::Expression *node); Type *BitwiseNegateIntegralType(Type *type, ir::Expression *node); - std::tuple CheckBinaryOperator(ir::Expression *left, ir::Expression *right, + std::tuple CheckBinaryOperator(ir::Expression *left, ir::Expression *right, ir::Expression *expr, lexer::TokenType operation_type, lexer::SourcePosition pos, bool force_promotion = false); Type *HandleArithmeticOperationOnTypes(Type *left, Type *right, lexer::TokenType operation_type); @@ -399,14 +400,17 @@ public: Type *GetTypeFromEnumReference(binder::Variable *var); Type *GetTypeFromTypeParameterReference(binder::LocalVariable *var, const lexer::SourcePosition &pos); Type *GetNonConstantTypeFromPrimitiveType(Type *type); + bool IsNullOrVoidExpression(const ir::Expression *expr) const; bool IsConstantExpression(ir::Expression *expr, Type *type); void ValidateUnaryOperatorOperand(binder::Variable *variable); std::tuple ApplyBinaryOperatorPromotion(Type *left, Type *right, TypeFlag test, bool do_promotion = true); checker::Type *ApplyConditionalOperatorPromotion(checker::ETSChecker *checker, checker::Type *unboxed_l, checker::Type *unboxed_r); - Type *ApplyUnaryOperatorPromotion(Type *type, bool create_const = true, bool do_promotion = true); + Type *ApplyUnaryOperatorPromotion(Type *type, bool create_const = true, bool do_promotion = true, + bool is_cond_expr = false); Type *HandleBooleanLogicalOperators(Type *left_type, Type *right_type, lexer::TokenType token_type); + Type *HandleBooleanLogicalOperatorsExtended(Type *left_type, Type *right_type, ir::BinaryExpression *expr); checker::Type *CheckVariableDeclaration(ir::Identifier *ident, ir::TypeNode *type_annotation, ir::Expression *init, ir::ModifierFlags flags); void CheckTruthinessOfType(ir::Expression *expr); @@ -428,6 +432,7 @@ public: binder::VariableFlags GetAccessFlagFromNode(const ir::AstNode *node); void CheckSwitchDiscriminant(ir::Expression *discriminant); Type *ETSBuiltinTypeAsPrimitiveType(Type *object_type); + Type *ETSBuiltinTypeAsConditionalType(Type *object_type); Type *PrimitiveTypeAsETSBuiltinType(Type *object_type); void AddBoxingUnboxingFlagToNode(ir::AstNode *node, Type *boxing_unboxing_type); ir::BoxingUnboxingFlags GetBoxingFlag(Type *boxing_type); diff --git a/ets2panda/checker/SemanticAnalyzer.h b/ets2panda/checker/SemanticAnalyzer.h index 72cfe3735de6e7438ae231ba39f65cf98146e39e..8cc27451f53332c643d0c2cbf5c7139e84b51f76 100644 --- a/ets2panda/checker/SemanticAnalyzer.h +++ b/ets2panda/checker/SemanticAnalyzer.h @@ -17,6 +17,152 @@ #define ES2PANDA_CHECKER_SEMANTICANALYZER_H #include "compiler/core/dynamicContext.h" +#include "ir/opaqueTypeNode.h" +#include "ir/as/namedType.h" +#include "ir/as/prefixAssertionExpression.h" +#include "ir/base/catchClause.h" +#include "ir/base/classDefinition.h" +#include "ir/base/classProperty.h" +#include "ir/base/classStaticBlock.h" +#include "ir/base/decorator.h" +#include "ir/base/metaProperty.h" +#include "ir/base/methodDefinition.h" +#include "ir/base/property.h" +#include "ir/base/scriptFunction.h" +#include "ir/base/spreadElement.h" +#include "ir/base/templateElement.h" +#include "ir/base/tsIndexSignature.h" +#include "ir/base/tsMethodSignature.h" +#include "ir/base/tsPropertySignature.h" +#include "ir/base/tsSignatureDeclaration.h" +#include "ir/ets/etsClassLiteral.h" +#include "ir/ets/etsFunctionType.h" +#include "ir/ets/etsImportDeclaration.h" +#include "ir/ets/etsLaunchExpression.h" +#include "ir/ets/etsNewArrayInstanceExpression.h" +#include "ir/ets/etsNewClassInstanceExpression.h" +#include "ir/ets/etsNewMultiDimArrayInstanceExpression.h" +#include "ir/ets/etsPackageDeclaration.h" +#include "ir/ets/etsParameterExpression.h" +#include "ir/ets/etsPrimitiveType.h" +#include "ir/ets/etsScript.h" +#include "ir/ets/etsStructDeclaration.h" +#include "ir/ets/etsTypeReference.h" +#include "ir/ets/etsTypeReferencePart.h" +#include "ir/ets/etsWildcardType.h" +#include "ir/expressions/arrayExpression.h" +#include "ir/expressions/arrowFunctionExpression.h" +#include "ir/expressions/assignmentExpression.h" +#include "ir/expressions/awaitExpression.h" +#include "ir/expressions/binaryExpression.h" +#include "ir/expressions/callExpression.h" +#include "ir/expressions/chainExpression.h" +#include "ir/expressions/classExpression.h" +#include "ir/expressions/conditionalExpression.h" +#include "ir/expressions/directEvalExpression.h" +#include "ir/expressions/functionExpression.h" +#include "ir/expressions/identifier.h" +#include "ir/expressions/importExpression.h" +#include "ir/expressions/memberExpression.h" +#include "ir/expressions/newExpression.h" +#include "ir/expressions/objectExpression.h" +#include "ir/expressions/omittedExpression.h" +#include "ir/expressions/sequenceExpression.h" +#include "ir/expressions/superExpression.h" +#include "ir/expressions/taggedTemplateExpression.h" +#include "ir/expressions/templateLiteral.h" +#include "ir/expressions/thisExpression.h" +#include "ir/expressions/unaryExpression.h" +#include "ir/expressions/updateExpression.h" +#include "ir/expressions/yieldExpression.h" +#include "ir/expressions/literals/bigIntLiteral.h" +#include "ir/expressions/literals/booleanLiteral.h" +#include "ir/expressions/literals/charLiteral.h" +#include "ir/expressions/literals/nullLiteral.h" +#include "ir/expressions/literals/numberLiteral.h" +#include "ir/expressions/literals/regExpLiteral.h" +#include "ir/expressions/literals/stringLiteral.h" +#include "ir/module/exportAllDeclaration.h" +#include "ir/module/exportDefaultDeclaration.h" +#include "ir/module/exportNamedDeclaration.h" +#include "ir/module/exportSpecifier.h" +#include "ir/module/importDeclaration.h" +#include "ir/module/importDefaultSpecifier.h" +#include "ir/module/importNamespaceSpecifier.h" +#include "ir/module/importSpecifier.h" +#include "ir/statements/assertStatement.h" +#include "ir/statements/blockStatement.h" +#include "ir/statements/breakStatement.h" +#include "ir/statements/classDeclaration.h" +#include "ir/statements/continueStatement.h" +#include "ir/statements/debuggerStatement.h" +#include "ir/statements/doWhileStatement.h" +#include "ir/statements/emptyStatement.h" +#include "ir/statements/expressionStatement.h" +#include "ir/statements/forInStatement.h" +#include "ir/statements/forOfStatement.h" +#include "ir/statements/forUpdateStatement.h" +#include "ir/statements/functionDeclaration.h" +#include "ir/statements/ifStatement.h" +#include "ir/statements/labelledStatement.h" +#include "ir/statements/returnStatement.h" +#include "ir/statements/switchCaseStatement.h" +#include "ir/statements/switchStatement.h" +#include "ir/statements/throwStatement.h" +#include "ir/statements/tryStatement.h" +#include "ir/statements/variableDeclaration.h" +#include "ir/statements/variableDeclarator.h" +#include "ir/statements/whileStatement.h" +#include "ir/ts/tsAnyKeyword.h" +#include "ir/ts/tsArrayType.h" +#include "ir/ts/tsAsExpression.h" +#include "ir/ts/tsBigintKeyword.h" +#include "ir/ts/tsBooleanKeyword.h" +#include "ir/ts/tsClassImplements.h" +#include "ir/ts/tsConditionalType.h" +#include "ir/ts/tsConstructorType.h" +#include "ir/ts/tsEnumDeclaration.h" +#include "ir/ts/tsEnumMember.h" +#include "ir/ts/tsExternalModuleReference.h" +#include "ir/ts/tsFunctionType.h" +#include "ir/ts/tsImportEqualsDeclaration.h" +#include "ir/ts/tsImportType.h" +#include "ir/ts/tsIndexedAccessType.h" +#include "ir/ts/tsInferType.h" +#include "ir/ts/tsInterfaceBody.h" +#include "ir/ts/tsInterfaceDeclaration.h" +#include "ir/ts/tsInterfaceHeritage.h" +#include "ir/ts/tsIntersectionType.h" +#include "ir/ts/tsLiteralType.h" +#include "ir/ts/tsMappedType.h" +#include "ir/ts/tsModuleBlock.h" +#include "ir/ts/tsModuleDeclaration.h" +#include "ir/ts/tsNamedTupleMember.h" +#include "ir/ts/tsNeverKeyword.h" +#include "ir/ts/tsNonNullExpression.h" +#include "ir/ts/tsNullKeyword.h" +#include "ir/ts/tsNumberKeyword.h" +#include "ir/ts/tsObjectKeyword.h" +#include "ir/ts/tsParameterProperty.h" +#include "ir/ts/tsParenthesizedType.h" +#include "ir/ts/tsQualifiedName.h" +#include "ir/ts/tsStringKeyword.h" +#include "ir/ts/tsThisType.h" +#include "ir/ts/tsTupleType.h" +#include "ir/ts/tsTypeAliasDeclaration.h" +#include "ir/ts/tsTypeAssertion.h" +#include "ir/ts/tsTypeLiteral.h" +#include "ir/ts/tsTypeOperator.h" +#include "ir/ts/tsTypeParameterDeclaration.h" +#include "ir/ts/tsTypeParameter.h" +#include "ir/ts/tsTypeParameterInstantiation.h" +#include "ir/ts/tsTypePredicate.h" +#include "ir/ts/tsTypeQuery.h" +#include "ir/ts/tsTypeReference.h" +#include "ir/ts/tsUndefinedKeyword.h" +#include "ir/ts/tsUnionType.h" +#include "ir/ts/tsUnknownKeyword.h" +#include "ir/ts/tsVoidKeyword.h" namespace panda::es2panda::checker { class Checker; diff --git a/ets2panda/checker/TSAnalyzer.cpp b/ets2panda/checker/TSAnalyzer.cpp index f5bfa79b2f493c500b2a5eb57e720362351182ea..26b808b82e930d9c432f09ca003b47382a15cc4a 100644 --- a/ets2panda/checker/TSAnalyzer.cpp +++ b/ets2panda/checker/TSAnalyzer.cpp @@ -16,12 +16,7 @@ #include "TSAnalyzer.h" #include "checker/TSchecker.h" -#include "ir/base/catchClause.h" -#include "ir/base/methodDefinition.h" -#include "ir/base/scriptFunction.h" -#include "ir/statements/blockStatement.h" -#include "ir/statements/returnStatement.h" -#include "ir/typeNode.h" +#include "checker/ts/destructuringContext.h" #include "util/helpers.h" namespace panda::es2panda::checker { @@ -191,41 +186,41 @@ checker::Type *TSAnalyzer::Check(ir::ETSPackageDeclaration *st) const UNREACHABLE(); } -checker::Type *TSAnalyzer::Check(ir::ETSParameterExpression *expr) const +checker::Type *TSAnalyzer::Check([[maybe_unused]] ir::ETSParameterExpression *expr) const { - (void)expr; UNREACHABLE(); } -checker::Type *TSAnalyzer::Check(ir::ETSPrimitiveType *node) const +checker::Type *TSAnalyzer::Check([[maybe_unused]] ir::ETSPrimitiveType *node) const { - (void)node; UNREACHABLE(); } -checker::Type *TSAnalyzer::Check(ir::ETSStructDeclaration *node) const +checker::Type *TSAnalyzer::Check([[maybe_unused]] ir::ETSStructDeclaration *node) const { - (void)node; UNREACHABLE(); } -checker::Type *TSAnalyzer::Check(ir::ETSTypeReference *node) const +checker::Type *TSAnalyzer::Check([[maybe_unused]] ir::ETSTypeReference *node) const { - (void)node; UNREACHABLE(); } -checker::Type *TSAnalyzer::Check(ir::ETSTypeReferencePart *node) const +checker::Type *TSAnalyzer::Check([[maybe_unused]] ir::ETSTypeReferencePart *node) const { - (void)node; UNREACHABLE(); } -checker::Type *TSAnalyzer::Check(ir::ETSWildcardType *node) const +checker::Type *TSAnalyzer::Check(ir::ETSUnionType *node) const { (void)node; UNREACHABLE(); } + +checker::Type *TSAnalyzer::Check([[maybe_unused]] ir::ETSWildcardType *node) const +{ + UNREACHABLE(); +} // compile methods for EXPRESSIONS in alphabetical order checker::Type *TSAnalyzer::Check(ir::ArrayExpression *expr) const { @@ -235,8 +230,35 @@ checker::Type *TSAnalyzer::Check(ir::ArrayExpression *expr) const checker::Type *TSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const { - (void)expr; - UNREACHABLE(); + TSChecker *checker = GetTSChecker(); + binder::Variable *func_var = nullptr; + + if (expr->Function()->Parent()->Parent() != nullptr && + expr->Function()->Parent()->Parent()->IsVariableDeclarator() && + expr->Function()->Parent()->Parent()->AsVariableDeclarator()->Id()->IsIdentifier()) { + func_var = expr->Function()->Parent()->Parent()->AsVariableDeclarator()->Id()->AsIdentifier()->Variable(); + } + + checker::ScopeContext scope_ctx(checker, expr->Function()->Scope()); + + auto *signature_info = checker->Allocator()->New(checker->Allocator()); + checker->CheckFunctionParameterDeclarations(expr->Function()->Params(), signature_info); + + auto *signature = checker->Allocator()->New( + signature_info, checker->GlobalResolvingReturnType(), expr->Function()); + checker::Type *func_type = checker->CreateFunctionTypeWithSignature(signature); + + if (func_var != nullptr && func_var->TsType() == nullptr) { + func_var->SetTsType(func_type); + } + + signature->SetReturnType(checker->HandleFunctionReturn(expr->Function())); + + if (!expr->Function()->Body()->IsExpression()) { + expr->Function()->Body()->Check(checker); + } + + return func_type; } checker::Type *TSAnalyzer::Check(ir::AssignmentExpression *expr) const diff --git a/ets2panda/checker/ets/aliveAnalyzer.cpp b/ets2panda/checker/ets/aliveAnalyzer.cpp index c47f5915e9b4cbc7eed077c56d4da589b7478631..e0e22a14ab9b439c3424c8b03b7e1c6b58b7949e 100644 --- a/ets2panda/checker/ets/aliveAnalyzer.cpp +++ b/ets2panda/checker/ets/aliveAnalyzer.cpp @@ -300,10 +300,9 @@ void AliveAnalyzer::AnalyzeDoLoop(const ir::DoWhileStatement *do_while) AnalyzeStat(do_while->Body()); status_ = Or(status_, ResolveContinues(do_while)); AnalyzeNode(do_while->Test()); - ASSERT(do_while->Test()->TsType() && do_while->Test()->TsType()->IsETSBooleanType()); - auto *cond_type = do_while->Test()->TsType()->AsETSBooleanType(); - status_ = And(status_, - static_cast(!(cond_type->HasTypeFlag(TypeFlag::CONSTANT) && cond_type->GetValue()))); + ASSERT(do_while->Test()->TsType() && do_while->Test()->TsType()->IsConditionalExprType()); + const auto expr_res = do_while->Test()->TsType()->ResolveConditionExpr(); + status_ = And(status_, static_cast(!std::get<0>(expr_res) || !std::get<1>(expr_res))); status_ = Or(status_, ResolveBreaks(do_while)); } @@ -311,26 +310,28 @@ void AliveAnalyzer::AnalyzeWhileLoop(const ir::WhileStatement *while_stmt) { SetOldPendingExits(PendingExits()); AnalyzeNode(while_stmt->Test()); - ASSERT(while_stmt->Test()->TsType() && while_stmt->Test()->TsType()->IsETSBooleanType()); - auto *cond_type = while_stmt->Test()->TsType()->AsETSBooleanType(); - status_ = And(status_, - static_cast(!(cond_type->HasTypeFlag(TypeFlag::CONSTANT) && !cond_type->GetValue()))); + ASSERT(while_stmt->Test()->TsType() && while_stmt->Test()->TsType()->IsConditionalExprType()); + const auto expr_res = while_stmt->Test()->TsType()->ResolveConditionExpr(); + status_ = And(status_, static_cast(!std::get<0>(expr_res) || std::get<1>(expr_res))); AnalyzeStat(while_stmt->Body()); status_ = Or(status_, ResolveContinues(while_stmt)); - status_ = - Or(ResolveBreaks(while_stmt), From(!(cond_type->HasTypeFlag(TypeFlag::CONSTANT) && cond_type->GetValue()))); + status_ = Or(ResolveBreaks(while_stmt), From(!std::get<0>(expr_res) || !std::get<1>(expr_res))); } void AliveAnalyzer::AnalyzeForLoop(const ir::ForUpdateStatement *for_stmt) { AnalyzeNode(for_stmt->Init()); SetOldPendingExits(PendingExits()); - const ETSBooleanType *cond_type {}; + const Type *cond_type {}; + bool resolve_type = false; + bool res = false; + if (for_stmt->Test() != nullptr) { AnalyzeNode(for_stmt->Test()); - ASSERT(for_stmt->Test()->TsType() && for_stmt->Test()->TsType()->IsETSBooleanType()); - cond_type = for_stmt->Test()->TsType()->AsETSBooleanType(); - status_ = From(!(cond_type->HasTypeFlag(TypeFlag::CONSTANT) && !cond_type->GetValue())); + ASSERT(for_stmt->Test()->TsType() && for_stmt->Test()->TsType()->IsConditionalExprType()); + cond_type = for_stmt->Test()->TsType(); + std::tie(resolve_type, res) = for_stmt->Test()->TsType()->ResolveConditionExpr(); + status_ = From(!resolve_type || res); } else { status_ = LivenessStatus::ALIVE; } @@ -338,8 +339,7 @@ void AliveAnalyzer::AnalyzeForLoop(const ir::ForUpdateStatement *for_stmt) AnalyzeStat(for_stmt->Body()); status_ = Or(status_, ResolveContinues(for_stmt)); AnalyzeNode(for_stmt->Update()); - status_ = Or(ResolveBreaks(for_stmt), - From(cond_type != nullptr && !(cond_type->HasTypeFlag(TypeFlag::CONSTANT) && cond_type->GetValue()))); + status_ = Or(ResolveBreaks(for_stmt), From(cond_type != nullptr && (!resolve_type || !res))); } void AliveAnalyzer::AnalyzeForOfLoop(const ir::ForOfStatement *for_of_stmt) diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index d5fd4872f8932d998a04c6fc478461cb6f19cc47..8f05886a059014eb0676c13f42f9c531a8739197 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -128,13 +128,18 @@ Type *ETSChecker::HandleRelationOperationOnTypes(Type *left, Type *right, lexer: // NOLINTNEXTLINE(readability-function-size) std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, ir::Expression *right, - lexer::TokenType operation_type, lexer::SourcePosition pos, - bool force_promotion) + ir::Expression *expr, lexer::TokenType operation_type, + lexer::SourcePosition pos, bool force_promotion) { checker::Type *const left_type = left->Check(this); checker::Type *const right_type = right->Check(this); - Type *unboxed_l = ETSBuiltinTypeAsPrimitiveType(left_type); - Type *unboxed_r = ETSBuiltinTypeAsPrimitiveType(right_type); + const bool is_logical_extended_operator = (operation_type == lexer::TokenType::PUNCTUATOR_LOGICAL_AND) || + (operation_type == lexer::TokenType::PUNCTUATOR_LOGICAL_OR); + Type *unboxed_l = is_logical_extended_operator ? ETSBuiltinTypeAsConditionalType(left_type) + : ETSBuiltinTypeAsPrimitiveType(left_type); + Type *unboxed_r = is_logical_extended_operator ? ETSBuiltinTypeAsConditionalType(right_type) + : ETSBuiltinTypeAsPrimitiveType(right_type); + checker::Type *ts_type {}; bool is_equal_op = (operation_type > lexer::TokenType::PUNCTUATOR_SUBSTITUTION && operation_type < lexer::TokenType::PUNCTUATOR_ARROW) && @@ -283,15 +288,24 @@ std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, } case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: case lexer::TokenType::PUNCTUATOR_LOGICAL_OR: { - if (unboxed_l == nullptr || !unboxed_l->HasTypeFlag(checker::TypeFlag::ETS_BOOLEAN) || - unboxed_r == nullptr || !unboxed_r->HasTypeFlag(checker::TypeFlag::ETS_BOOLEAN)) { - ThrowTypeError("Bad operand type, the types of the operands must be boolean type.", pos); + if (unboxed_l == nullptr || !unboxed_l->IsConditionalExprType() || unboxed_r == nullptr || + !unboxed_r->IsConditionalExprType()) { + ThrowTypeError("Bad operand type, the types of the operands must be of possible condition type.", pos); } - FlagExpressionWithUnboxing(left_type, unboxed_l, left); - FlagExpressionWithUnboxing(right_type, unboxed_r, right); + if (unboxed_l->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { + FlagExpressionWithUnboxing(left_type, unboxed_l, left); + } + + if (unboxed_r->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { + FlagExpressionWithUnboxing(right_type, unboxed_r, right); + } - ts_type = HandleBooleanLogicalOperators(unboxed_l, unboxed_r, operation_type); + if (expr->IsBinaryExpression()) { + ts_type = HandleBooleanLogicalOperatorsExtended(unboxed_l, unboxed_r, expr->AsBinaryExpression()); + } else { + UNREACHABLE(); + } break; } case lexer::TokenType::PUNCTUATOR_STRICT_EQUAL: @@ -366,6 +380,16 @@ std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, FlagExpressionWithUnboxing(left_type, unboxed_l, left); FlagExpressionWithUnboxing(right_type, unboxed_r, right); + if (left_type->IsETSUnionType()) { + ts_type = GlobalETSBooleanType(); + return {ts_type, left_type->AsETSUnionType()}; + } + + if (right_type->IsETSUnionType()) { + ts_type = GlobalETSBooleanType(); + return {ts_type, right_type->AsETSUnionType()}; + } + if (promotedType == nullptr && !bothConst) { ThrowTypeError("Bad operand type, the types of the operands must be numeric type.", pos); } @@ -380,8 +404,8 @@ std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, return {ts_type, op_type}; } case lexer::TokenType::KEYW_INSTANCEOF: { - if (!left_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT) || - !right_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (!left_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION) || + !right_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { ThrowTypeError("Bad operand type, the types of the operands must be same type.", pos); } diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index d8417228b46d5ae5a1133b84f46a5fd42e16d848..efc7865227e80b8c9bf9b302470f0b8849defb86 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -72,13 +72,15 @@ namespace panda::es2panda::checker { void ETSChecker::CheckTruthinessOfType(ir::Expression *expr) { checker::Type *type = expr->Check(this); - auto *unboxed_type = ETSBuiltinTypeAsPrimitiveType(type); + auto *unboxed_type = ETSBuiltinTypeAsConditionalType(type); - if (unboxed_type == nullptr || !unboxed_type->IsETSBooleanType()) { - ThrowTypeError("Condition must be of type boolean", expr->Start()); + if (unboxed_type != nullptr && !unboxed_type->IsConditionalExprType()) { + ThrowTypeError("Condition must be of possible condition type", expr->Start()); } - FlagExpressionWithUnboxing(type, unboxed_type, expr); + if (unboxed_type != nullptr && unboxed_type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { + FlagExpressionWithUnboxing(type, unboxed_type, expr); + } expr->SetTsType(unboxed_type); } @@ -344,7 +346,8 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident, binder: } if (!resolved_type->IsETSObjectType() && !resolved_type->IsETSArrayType() && - !resolved_type->IsETSEnumType() && !resolved_type->IsETSStringEnumType()) { + !resolved_type->IsETSEnumType() && !resolved_type->IsETSStringEnumType() && + !resolved_type->IsETSUnionType()) { throw_error(); } @@ -626,9 +629,10 @@ checker::Type *ETSChecker::ApplyConditionalOperatorPromotion(checker::ETSChecker UNREACHABLE(); } -Type *ETSChecker::ApplyUnaryOperatorPromotion(Type *type, bool create_const, bool do_promotion) +Type *ETSChecker::ApplyUnaryOperatorPromotion(Type *type, const bool create_const, const bool do_promotion, + const bool is_cond_expr) { - Type *unboxed_type = ETSBuiltinTypeAsPrimitiveType(type); + Type *unboxed_type = is_cond_expr ? ETSBuiltinTypeAsConditionalType(type) : ETSBuiltinTypeAsPrimitiveType(type); if (unboxed_type == nullptr) { return nullptr; @@ -652,6 +656,58 @@ Type *ETSChecker::ApplyUnaryOperatorPromotion(Type *type, bool create_const, boo return unboxed_type; } +bool ETSChecker::IsNullOrVoidExpression(const ir::Expression *expr) const +{ + return (expr->IsLiteral() && expr->AsLiteral()->IsNullLiteral()) || + (expr->IsCallExpression() && + (expr->AsCallExpression()->Signature()->ReturnType() == GlobalBuiltinVoidType())); +} + +Type *ETSChecker::HandleBooleanLogicalOperatorsExtended(Type *left_type, Type *right_type, ir::BinaryExpression *expr) +{ + ASSERT(left_type->IsConditionalExprType() && right_type->IsConditionalExprType()); + + bool resolve_left = false; + bool left_value = false; + bool resolve_right = false; + bool right_value = false; + std::tie(resolve_left, left_value) = + IsNullOrVoidExpression(expr->Left()) ? std::make_tuple(true, false) : left_type->ResolveConditionExpr(); + std::tie(resolve_right, right_value) = + IsNullOrVoidExpression(expr->Right()) ? std::make_tuple(true, false) : right_type->ResolveConditionExpr(); + + if (!resolve_left) { + // return the UNION type when it is implemented + return IsTypeIdenticalTo(left_type, right_type) ? left_type : GlobalETSBooleanType(); + } + + switch (expr->OperatorType()) { + case lexer::TokenType::PUNCTUATOR_LOGICAL_OR: { + if (left_value) { + expr->SetResult(expr->Left()); + return left_type->IsETSBooleanType() ? CreateETSBooleanType(true) : left_type; + } + + expr->SetResult(expr->Right()); + return right_type->IsETSBooleanType() && resolve_right ? CreateETSBooleanType(right_value) : right_type; + } + case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: { + if (left_value) { + expr->SetResult(expr->Right()); + return right_type->IsETSBooleanType() && resolve_right ? CreateETSBooleanType(right_value) : right_type; + } + + expr->SetResult(expr->Left()); + return left_type->IsETSBooleanType() ? CreateETSBooleanType(false) : left_type; + } + default: { + break; + } + } + + UNREACHABLE(); +} + Type *ETSChecker::HandleBooleanLogicalOperators(Type *left_type, Type *right_type, lexer::TokenType token_type) { using UType = typename ETSBooleanType::UType; @@ -1309,6 +1365,27 @@ Type *ETSChecker::ETSBuiltinTypeAsPrimitiveType(Type *object_type) return converter.Result(); } +Type *ETSChecker::ETSBuiltinTypeAsConditionalType(Type *object_type) +{ + if ((object_type == nullptr) || !object_type->IsConditionalExprType()) { + return nullptr; + } + + if (object_type->IsETSObjectType()) { + if (!object_type->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::UNBOXABLE_TYPE)) { + return object_type; + } + auto saved_result = Relation()->IsTrue(); + Relation()->Result(false); + + UnboxingConverter converter = UnboxingConverter(AsETSChecker(), Relation(), object_type, object_type); + Relation()->Result(saved_result); + return converter.Result(); + } + + return object_type; +} + Type *ETSChecker::PrimitiveTypeAsETSBuiltinType(Type *object_type) { if (object_type == nullptr) { diff --git a/ets2panda/checker/ets/typeCreation.cpp b/ets2panda/checker/ets/typeCreation.cpp index f4314b693181b0198cdb3b6ea0b98aa1e4a02387..571ea3d2f943b26bead67c9945a4197207e9e3f4 100644 --- a/ets2panda/checker/ets/typeCreation.cpp +++ b/ets2panda/checker/ets/typeCreation.cpp @@ -114,6 +114,35 @@ ETSArrayType *ETSChecker::CreateETSArrayType(Type *element_type) return array_type; } +Type *ETSChecker::CreateETSUnionType(ArenaVector &&constituent_types) +{ + if (constituent_types.empty()) { + return nullptr; + } + + ArenaVector new_constituent_types(Allocator()->Adapter()); + + for (auto *it : constituent_types) { + if (it->IsUnionType()) { + for (auto *type : it->AsETSUnionType()->ConstituentTypes()) { + new_constituent_types.push_back(type); + } + + continue; + } + + new_constituent_types.push_back(it); + } + + if (new_constituent_types.size() == 1) { + return new_constituent_types[0]; + } + + auto *new_union_type = Allocator()->New(std::move(new_constituent_types)); + + return ETSUnionType::HandleUnionType(new_union_type); +} + ETSFunctionType *ETSChecker::CreateETSFunctionType(ArenaVector &signatures) { auto *func_type = Allocator()->New(signatures[0]->Function()->Id()->Name(), Allocator()); diff --git a/ets2panda/checker/ets/typeRelationContext.cpp b/ets2panda/checker/ets/typeRelationContext.cpp index 8ba09ba64645597e2665275b0d1bb908613cbe8d..ba6fdbc113a252a1a764f77a539510b9618d5e65 100644 --- a/ets2panda/checker/ets/typeRelationContext.cpp +++ b/ets2panda/checker/ets/typeRelationContext.cpp @@ -69,33 +69,48 @@ bool InstantiationContext::ValidateTypeArguments(ETSObjectType *type, ir::TSType continue; } - auto *constraint_type = type_param_constraint->GetType(checker_)->AsETSObjectType(); - auto *arg_ref_type = param_type->AsETSObjectType(); - - if (const auto *const found = checker_->AsETSChecker()->Scope()->FindLocal( - constraint_type->Name(), binder::ResolveBindingOptions::TYPE_ALIASES); - found != nullptr) { - arg_ref_type = found->TsType()->AsETSObjectType(); - } - - auto assignable = checker_->Relation()->IsAssignableTo(arg_ref_type, constraint_type); - if (constraint_type->HasObjectFlag(ETSObjectFlags::INTERFACE)) { - for (const auto *const interface : arg_ref_type->Interfaces()) { - // TODO(mmartin): make correct check later for multiple bounds - assignable = (interface == constraint_type) || assignable; - } + bool assignable = false; + auto *constraint_type = type_param_constraint->GetType(checker_); + if (constraint_type->IsETSObjectType() && param_type->IsETSObjectType()) { + assignable = ValidateTypeArg(constraint_type->AsETSObjectType(), param_type->AsETSObjectType()); + } else if (param_type->IsETSUnionType() && !constraint_type->IsETSUnionType()) { + auto constituent_types = param_type->AsETSUnionType()->ConstituentTypes(); + assignable = + std::all_of(constituent_types.begin(), constituent_types.end(), [this, constraint_type](Type *c_type) { + return c_type->IsETSObjectType() && + ValidateTypeArg(constraint_type->AsETSObjectType(), c_type->AsETSObjectType()); + }); } if (!assignable) { - checker_->ThrowTypeError( - {"Type '", arg_ref_type, "' is not assignable to constraint type '", constraint_type, "'."}, - type_args->Params().at(type_param_iter)->Start()); + checker_->ThrowTypeError({"Type '", param_type->AsETSObjectType(), + "' is not assignable to constraint type '", constraint_type, "'."}, + type_args->Params().at(type_param_iter)->Start()); } } return false; } +bool InstantiationContext::ValidateTypeArg(ETSObjectType *constraint_type, ETSObjectType *arg_ref_type) +{ + if (const auto *const found = checker_->AsETSChecker()->Scope()->FindLocal( + constraint_type->Name(), binder::ResolveBindingOptions::TYPE_ALIASES); + found != nullptr) { + arg_ref_type = found->TsType()->AsETSObjectType(); + } + + auto assignable = checker_->Relation()->IsAssignableTo(arg_ref_type, constraint_type); + if (constraint_type->HasObjectFlag(ETSObjectFlags::INTERFACE)) { + for (const auto *const interface : arg_ref_type->Interfaces()) { + // TODO(mmartin): make correct check later for multiple bounds + assignable = (interface == constraint_type) || assignable; + } + } + + return assignable; +} + void InstantiationContext::InstantiateType(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args) { ArenaVector type_arg_types(checker_->Allocator()->Adapter()); @@ -132,7 +147,18 @@ void InstantiationContext::InstantiateType(ETSObjectType *type, ArenaVectorNewSubstitution(); for (size_t ix = 0; ix < type_params.size(); ix++) { - if (!checker_->IsCompatibleTypeArgument(type_params[ix], type_arg_types[ix])) { + auto *type_param = type_params[ix]; + bool is_compatible_type_arg; + if (type_arg_types[ix]->IsETSUnionType()) { + auto union_constituent_types = type_arg_types[ix]->AsETSUnionType()->ConstituentTypes(); + is_compatible_type_arg = std::all_of(union_constituent_types.begin(), union_constituent_types.end(), + [this, type_param](Type *type_arg) { + return checker_->IsCompatibleTypeArgument(type_param, type_arg); + }); + } else { + is_compatible_type_arg = checker_->IsCompatibleTypeArgument(type_param, type_arg_types[ix]); + } + if (!is_compatible_type_arg) { checker_->ThrowTypeError( {"Type ", type_arg_types[ix], " is not assignable to", " type parameter ", type_params[ix]}, pos); } diff --git a/ets2panda/checker/ets/typeRelationContext.h b/ets2panda/checker/ets/typeRelationContext.h index c2d48bf63d70027b1397f0d7f8b48370dba13494..fe034ad67fc67f765f749380c79ebd9becb696a7 100644 --- a/ets2panda/checker/ets/typeRelationContext.h +++ b/ets2panda/checker/ets/typeRelationContext.h @@ -173,6 +173,9 @@ public: private: bool ValidateTypeArguments(ETSObjectType *type, ir::TSTypeParameterDeclaration *type_param_decl, ir::TSTypeParameterInstantiation *type_args, const lexer::SourcePosition &pos); + + bool ValidateTypeArg(ETSObjectType *constraint_type, ETSObjectType *arg_ref_type); + void InstantiateType(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args); void InstantiateType(ETSObjectType *type, ArenaVector &type_arg_types, const lexer::SourcePosition &pos); diff --git a/ets2panda/checker/types/ets/byteType.cpp b/ets2panda/checker/types/ets/byteType.cpp index 5c05455108965532d6f2c915fac5811e362b543a..0adece6593fff74e47f6f82bd711067c830f6809 100644 --- a/ets2panda/checker/types/ets/byteType.cpp +++ b/ets2panda/checker/types/ets/byteType.cpp @@ -82,6 +82,11 @@ void ByteType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/byteType.h b/ets2panda/checker/types/ets/byteType.h index ecbf47d8850307a8d1fd941769787baae269fdca..1a1ea05d11fb29120f2ac0fd8163bf6eb2a2a803 100644 --- a/ets2panda/checker/types/ets/byteType.h +++ b/ets2panda/checker/types/ets/byteType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_BYTE; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != 0}; + } + private: UType value_ {0}; }; diff --git a/ets2panda/checker/types/ets/charType.cpp b/ets2panda/checker/types/ets/charType.cpp index aba253d3397dea3f635ecd99b118f12310192977..f43a20563cf6604bf0868e4855d4c7cd0e5fc407 100644 --- a/ets2panda/checker/types/ets/charType.cpp +++ b/ets2panda/checker/types/ets/charType.cpp @@ -82,6 +82,11 @@ void CharType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/charType.h b/ets2panda/checker/types/ets/charType.h index 6f7dc11b828ee2b971499876bb838aa3063b6a8b..763a43354204710774ac7228e192dcb6865618fd 100644 --- a/ets2panda/checker/types/ets/charType.h +++ b/ets2panda/checker/types/ets/charType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_CHAR; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != '\0'}; + } + private: UType value_ {'\0'}; }; diff --git a/ets2panda/checker/types/ets/doubleType.cpp b/ets2panda/checker/types/ets/doubleType.cpp index 99e3d5e0d11c66b58e56619e1820c31a31191b57..f474e0b5f761f053cee8de9d21afbd8ccdb5a367 100644 --- a/ets2panda/checker/types/ets/doubleType.cpp +++ b/ets2panda/checker/types/ets/doubleType.cpp @@ -77,6 +77,11 @@ void DoubleType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/doubleType.h b/ets2panda/checker/types/ets/doubleType.h index 07365fa67043ffd611697251a14b99b0e3fb2d9a..ec7ebce230a57d3e2f595002a904fac24e03a1be 100644 --- a/ets2panda/checker/types/ets/doubleType.h +++ b/ets2panda/checker/types/ets/doubleType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_DOUBLE; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != 0}; + } + private: UType value_ {0.0}; }; diff --git a/ets2panda/checker/types/ets/etsArrayType.h b/ets2panda/checker/types/ets/etsArrayType.h index 323f3df4006d9a62f55d4735c5cc1cd439accf51..e19ceccd59c78935ac4e2421f27fb298d9577b87 100644 --- a/ets2panda/checker/types/ets/etsArrayType.h +++ b/ets2panda/checker/types/ets/etsArrayType.h @@ -33,6 +33,11 @@ public: return element_; } + std::tuple ResolveConditionExpr() const override + { + return {false, false}; + } + void ToString(std::stringstream &ss) const override; void ToAssemblerType(std::stringstream &ss) const override; void ToAssemblerTypeWithRank(std::stringstream &ss) const override; diff --git a/ets2panda/checker/types/ets/etsBooleanType.cpp b/ets2panda/checker/types/ets/etsBooleanType.cpp index 52dbbfdad44d6ff9bd7664fd628e694dc9998589..35d92816262a7b3198077cf05616a1717b1fb967 100644 --- a/ets2panda/checker/types/ets/etsBooleanType.cpp +++ b/ets2panda/checker/types/ets/etsBooleanType.cpp @@ -64,6 +64,11 @@ void ETSBooleanType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/etsBooleanType.h b/ets2panda/checker/types/ets/etsBooleanType.h index f842f762fed55effd52d8be4e1b41c6b0edb1969..2d35d71ef714f1a89d731144f3b7658ea908145a 100644 --- a/ets2panda/checker/types/ets/etsBooleanType.h +++ b/ets2panda/checker/types/ets/etsBooleanType.h @@ -51,6 +51,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_BOOLEAN; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_}; + } + private: UType value_ {false}; }; diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index c04684c5be39fde5c97a8c5cbc9e53008fbb2c96..66ccff4e3a6ceb670603d72ba24170d3cac028ba 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -623,6 +623,11 @@ void ETSObjectType::Cast(TypeRelation *const relation, Type *const target) } } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } @@ -639,7 +644,8 @@ void ETSObjectType::IsSupertypeOf(TypeRelation *relation, Type *source) } if (!source->IsETSObjectType() || - !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::CLASS | ETSObjectFlags::INTERFACE)) { + !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::CLASS | ETSObjectFlags::INTERFACE | + ETSObjectFlags::NULL_TYPE)) { return; } diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 45232d5c8fcdd9bae7b0364f03e730a91698e600..4821d6b00c1f09dc24189c396dd526d1c88e90eb 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -508,6 +508,15 @@ public: return allocator_; } + std::tuple ResolveConditionExpr() const override + { + if (IsNullableType() || IsETSStringType()) { + return {false, false}; + } + + return {true, true}; + } + protected: virtual ETSFunctionType *CreateETSFunctionType(const util::StringView &name) const; diff --git a/ets2panda/checker/types/ets/etsStringType.h b/ets2panda/checker/types/ets/etsStringType.h index 4a516db40d6a9328261d3aaaaaa64d78953247b3..1cb059c48abfcb1551b830316d2b37a4fc604c76 100644 --- a/ets2panda/checker/types/ets/etsStringType.h +++ b/ets2panda/checker/types/ets/etsStringType.h @@ -55,6 +55,15 @@ public: return value_; } + std::tuple ResolveConditionExpr() const override + { + if (IsNullableType()) { + return {false, false}; + } + + return {IsConstantType(), IsConstantType() ? (GetValue().Length() != 0) : false}; + } + private: util::StringView value_ {}; }; diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0addc81fd54c38f60c346cb3057321476d39d3c --- /dev/null +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -0,0 +1,240 @@ +/** + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "etsUnionType.h" +#include "checker/ets/conversion.h" +#include "checker/types/globalTypesHolder.h" +#include "checker/ETSchecker.h" + +namespace panda::es2panda::checker { +void ETSUnionType::ToString(std::stringstream &ss) const +{ + for (auto it = constituent_types_.begin(); it != constituent_types_.end(); it++) { + (*it)->ToString(ss); + if (std::next(it) != constituent_types_.end()) { + ss << " | "; + } + } +} + +bool ETSUnionType::EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target) +{ + return std::all_of(source->constituent_types_.begin(), source->constituent_types_.end(), + [relation, target](auto *s) { return TypeRelatedToSomeType(relation, s, target); }); +} + +bool ETSUnionType::TypeRelatedToSomeType(TypeRelation *relation, Type *source, ETSUnionType *target) +{ + return std::any_of(target->constituent_types_.begin(), target->constituent_types_.end(), + [relation, source](auto *t) { return relation->IsIdenticalTo(source, t); }); +} + +Type *ETSUnionType::GetLeastUpperBoundType(ETSChecker *checker) +{ + ASSERT(constituent_types_.size() > 1); + if (lub_type_ == nullptr) { + lub_type_ = constituent_types_.front(); + for (auto *t : constituent_types_) { + if (!t->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT)) { + lub_type_ = checker->GetGlobalTypesHolder()->GlobalETSObjectType(); + return lub_type_; + } + lub_type_ = checker->FindLeastUpperBound(lub_type_, t); + } + } + return lub_type_; +} + +void ETSUnionType::Identical(TypeRelation *relation, Type *other) +{ + if (other->IsETSUnionType()) { + if (EachTypeRelatedToSomeType(relation, this, other->AsETSUnionType()) && + EachTypeRelatedToSomeType(relation, other->AsETSUnionType(), this)) { + relation->Result(true); + return; + } + } + + relation->Result(false); +} + +bool ETSUnionType::AssignmentSource(TypeRelation *relation, Type *target) +{ + for (auto *it : constituent_types_) { + if (!relation->IsAssignableTo(it, target)) { + return false; + } + } + + relation->Result(true); + return true; +} + +void ETSUnionType::AssignmentTarget(TypeRelation *relation, Type *source) +{ + // For an unsorted constituent_types_, a less suitable type may first come across than it could be + // if the entire array of constituent types was analyzed. + for (auto *it : constituent_types_) { + if (!source->IsETSObjectType() && (source->HasTypeFlag(it->TypeFlags()) || it == source)) { + relation->IsAssignableTo(source, it); + return; + } + } + for (auto *it : constituent_types_) { + if (relation->IsAssignableTo(source, it)) { + return; + } + } +} + +Type *ETSUnionType::HandleUnionType(ETSUnionType *union_type) +{ + if (union_type->ConstituentTypes().size() == 1) { + return union_type->ConstituentTypes()[0]; + } + + return union_type; +} + +Type *ETSUnionType::Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *global_types) +{ + ArenaVector copied_constituents(constituent_types_.size(), allocator->Adapter()); + + for (auto *it : constituent_types_) { + copied_constituents.push_back(it->Instantiate(allocator, relation, global_types)); + } + + if (copied_constituents.size() == 1) { + return copied_constituents[0]; + } + + Type *new_union_type = allocator->New(std::move(copied_constituents)); + + lub_type_ = global_types->GlobalETSObjectType(); + return HandleUnionType(new_union_type->AsETSUnionType()); +} + +void ETSUnionType::Cast(TypeRelation *relation, Type *target) +{ + bool is_cast_to_obj = target->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT); + for (auto *source : constituent_types_) { + relation->IsCastableTo(source, target); + if (relation->IsTrue()) { + if (is_cast_to_obj && source->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT)) { + GetLeastUpperBoundType(relation->GetChecker()->AsETSChecker())->Cast(relation, target); + return; + } + if (!is_cast_to_obj) { + source->Cast(relation, target); + return; + } + } + } + + conversion::Forbidden(relation); +} + +void ETSUnionType::CastToThis(TypeRelation *relation, Type *source) +{ + Type *target_type = FindTypeIsCastableToThis(relation->GetNode(), relation, source); + if (target_type != nullptr) { + source->Cast(relation, target_type); + return; + } + + conversion::Forbidden(relation); +} + +Type *ETSUnionType::FindTypeIsCastableToThis(ir::Expression *node, TypeRelation *relation, Type *source) const +{ + ASSERT(node); + bool node_was_set = false; + if (relation->GetNode() == nullptr) { + node_was_set = true; + relation->SetNode(node); + } + // Prioritize object to object conversion + auto it = std::find_if(constituent_types_.begin(), constituent_types_.end(), [relation, source](Type *target) { + relation->IsCastableTo(source, target); + return relation->IsTrue() && source->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT) && + target->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT); + }); + if (it != constituent_types_.end()) { + if (node_was_set) { + relation->SetNode(nullptr); + } + return *it; + } + it = std::find_if(constituent_types_.begin(), constituent_types_.end(), [relation, source](Type *target) { + relation->IsCastableTo(source, target); + return relation->IsTrue(); + }); + if (node_was_set) { + relation->SetNode(nullptr); + } + if (it != constituent_types_.end()) { + return *it; + } + return nullptr; +} + +Type *ETSUnionType::FindTypeIsCastableToSomeType(ir::Expression *node, TypeRelation *relation, Type *target) const +{ + ASSERT(node); + bool node_was_set = false; + if (relation->GetNode() == nullptr) { + node_was_set = true; + relation->SetNode(node); + relation->SetFlags(TypeRelationFlag::CASTING_CONTEXT); + } + auto is_castable_pred = [](TypeRelation *r, Type *source_type, Type *target_type) { + if (target_type->IsETSUnionType()) { + auto *found_target_type = + target_type->AsETSUnionType()->FindTypeIsCastableToThis(r->GetNode(), r, source_type); + r->Result(found_target_type != nullptr); + } else { + r->IsCastableTo(source_type, target_type); + } + return r->IsTrue(); + }; + // Prioritize object to object conversion + auto it = std::find_if( + constituent_types_.begin(), constituent_types_.end(), [relation, target, &is_castable_pred](Type *source) { + return is_castable_pred(relation, source, target) && source->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT) && + target->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT); + }); + if (it != constituent_types_.end()) { + if (node_was_set) { + relation->SetNode(nullptr); + relation->RemoveFlags(TypeRelationFlag::CASTING_CONTEXT); + } + return *it; + } + it = std::find_if( + constituent_types_.begin(), constituent_types_.end(), + [relation, target, &is_castable_pred](Type *source) { return is_castable_pred(relation, source, target); }); + if (node_was_set) { + relation->SetNode(nullptr); + relation->RemoveFlags(TypeRelationFlag::CASTING_CONTEXT); + } + if (it != constituent_types_.end()) { + return *it; + } + return nullptr; +} + +} // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/ets/etsUnionType.h b/ets2panda/checker/types/ets/etsUnionType.h new file mode 100644 index 0000000000000000000000000000000000000000..26760592334537e7e0d073ad612bcb6f01181ca7 --- /dev/null +++ b/ets2panda/checker/types/ets/etsUnionType.h @@ -0,0 +1,91 @@ +/** + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_COMPILER_CHECKER_TYPES_ETS_UNION_TYPE_H +#define ES2PANDA_COMPILER_CHECKER_TYPES_ETS_UNION_TYPE_H + +#include "checker/types/type.h" + +namespace panda::es2panda::checker { +class GlobalTypesHolder; + +class ETSUnionType : public Type { +public: + explicit ETSUnionType(ArenaAllocator *allocator) + : Type(TypeFlag::ETS_UNION), constituent_types_(allocator->Adapter()) + { + } + + explicit ETSUnionType(ArenaVector &&constituent_types) + : Type(TypeFlag::ETS_UNION), constituent_types_(std::move(constituent_types)) + { + } + + explicit ETSUnionType(ArenaVector &constituent_types) + : Type(TypeFlag::ETS_UNION), constituent_types_(constituent_types) + { + } + + const ArenaVector &ConstituentTypes() const + { + return constituent_types_; + } + + ArenaVector &ConstituentTypes() + { + return constituent_types_; + } + + void AddConstituentType(Type *type, TypeRelation *relation) + { + for (auto *it : constituent_types_) { + if (relation->IsIdenticalTo(it, type)) { + return; + } + } + + constituent_types_.push_back(type); + } + + void ToString(std::stringstream &ss) const override; + void Identical(TypeRelation *relation, Type *other) override; + void AssignmentTarget(TypeRelation *relation, Type *source) override; + bool AssignmentSource(TypeRelation *relation, Type *target) override; + Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *global_types) override; + void Cast(TypeRelation *relation, Type *target) override; + void CastToThis(TypeRelation *relation, Type *source); + Type *FindTypeIsCastableToThis(ir::Expression *node, TypeRelation *relation, Type *source) const; + Type *FindTypeIsCastableToSomeType(ir::Expression *node, TypeRelation *relation, Type *target) const; + + Type *GetLeastUpperBoundType(ETSChecker *checker); + + Type *GetLeastUpperBoundType() const + { + ASSERT(lub_type_ != nullptr); + return lub_type_; + } + + static Type *HandleUnionType(ETSUnionType *union_type); + +private: + static bool EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target); + static bool TypeRelatedToSomeType(TypeRelation *relation, Type *source, ETSUnionType *target); + + ArenaVector constituent_types_; + Type *lub_type_ {nullptr}; +}; +} // namespace panda::es2panda::checker + +#endif /* ETS_TYPES_ETS_UNION_TYPE_H */ diff --git a/ets2panda/checker/types/ets/floatType.cpp b/ets2panda/checker/types/ets/floatType.cpp index d40efa8f09c0f687af01c1abe96895c4a7753755..094609f9b1bdc3b9bd321aae748c5f6cc8e2ad19 100644 --- a/ets2panda/checker/types/ets/floatType.cpp +++ b/ets2panda/checker/types/ets/floatType.cpp @@ -82,6 +82,11 @@ void FloatType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/floatType.h b/ets2panda/checker/types/ets/floatType.h index 513c9b325da92a3c0a48c7596b43a9ae83c302b6..a3b8ffb5adeb54c4bff5b951faf7cb88a778f7d8 100644 --- a/ets2panda/checker/types/ets/floatType.h +++ b/ets2panda/checker/types/ets/floatType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_FLOAT; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != 0}; + } + private: UType value_ {0.0}; }; diff --git a/ets2panda/checker/types/ets/intType.cpp b/ets2panda/checker/types/ets/intType.cpp index 2f4cac76c3f375a99438dcfa1f9a85bf699b7668..c0009aada31721659444015637a1a3b6dfc545eb 100644 --- a/ets2panda/checker/types/ets/intType.cpp +++ b/ets2panda/checker/types/ets/intType.cpp @@ -43,7 +43,7 @@ bool IntType::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_ } } - if (relation->ApplyBoxing() && target->IsETSObjectType()) { + if (relation->ApplyBoxing() && (target->IsETSObjectType())) { relation->GetChecker()->AsETSChecker()->CheckBoxedSourceTypeAssignable(relation, this, target); } @@ -87,6 +87,11 @@ void IntType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/intType.h b/ets2panda/checker/types/ets/intType.h index df8f6af99a43d79d9a9db910540848d94f7e03ed..ec411394f8f2ab4cf2cece1f4140a47a5de7d8e4 100644 --- a/ets2panda/checker/types/ets/intType.h +++ b/ets2panda/checker/types/ets/intType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_INT; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != 0}; + } + private: UType value_ {0}; }; diff --git a/ets2panda/checker/types/ets/longType.cpp b/ets2panda/checker/types/ets/longType.cpp index 4246b89ccd8132492d15022b85945a346b7b0e20..becec8867e5c9d93c1a4d1e617855ca3c040e887 100644 --- a/ets2panda/checker/types/ets/longType.cpp +++ b/ets2panda/checker/types/ets/longType.cpp @@ -82,6 +82,11 @@ void LongType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/longType.h b/ets2panda/checker/types/ets/longType.h index 27a2deca699ce7dc662970de1a834dc77bb3e77f..8d89b3be8cb2af62707b41932bbd8205b18797f3 100644 --- a/ets2panda/checker/types/ets/longType.h +++ b/ets2panda/checker/types/ets/longType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_LONG; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != 0}; + } + private: UType value_ {0}; }; diff --git a/ets2panda/checker/types/ets/shortType.cpp b/ets2panda/checker/types/ets/shortType.cpp index 8cd1be7202dde5d5a64436f3263c3b7618c838b1..02c91218c3f68c00c05bcc5b24684a1f230281da 100644 --- a/ets2panda/checker/types/ets/shortType.cpp +++ b/ets2panda/checker/types/ets/shortType.cpp @@ -82,6 +82,11 @@ void ShortType::Cast(TypeRelation *const relation, Type *const target) return; } + if (target->IsETSUnionType()) { + target->AsETSUnionType()->CastToThis(relation, this); + return; + } + conversion::Forbidden(relation); } diff --git a/ets2panda/checker/types/ets/shortType.h b/ets2panda/checker/types/ets/shortType.h index 287c0424247afe07b9a2e6a1acdc916202044808..e0848cc725fe8923413a9f046c0c21c70f000eda 100644 --- a/ets2panda/checker/types/ets/shortType.h +++ b/ets2panda/checker/types/ets/shortType.h @@ -52,6 +52,11 @@ public: ss << compiler::Signatures::TYPE_DESCRIPTOR_SHORT; } + std::tuple ResolveConditionExpr() const override + { + return {IsConstantType(), value_ != 0}; + } + private: UType value_ {0}; }; diff --git a/ets2panda/checker/types/ets/types.h b/ets2panda/checker/types/ets/types.h index 24bd24e69a5c5be2033028e4c37926b357f957e5..1f798ee532f0073398218b8c14b9eb951315b8fc 100644 --- a/ets2panda/checker/types/ets/types.h +++ b/ets2panda/checker/types/ets/types.h @@ -27,6 +27,7 @@ #include "etsEnumType.h" #include "etsExtensionFuncHelperType.h" #include "etsFunctionType.h" +#include "etsUnionType.h" #include "etsVoidType.h" #include "etsStringType.h" #include "etsObjectType.h" diff --git a/ets2panda/checker/types/type.h b/ets2panda/checker/types/type.h index 2c6d11a786a3deb0f9be83dfdd7ef7fae5b8feb8..77b74baec3248743cdc33cdcfe3ec024dc40d317 100644 --- a/ets2panda/checker/types/type.h +++ b/ets2panda/checker/types/type.h @@ -145,6 +145,16 @@ public: return reinterpret_cast(this); } + bool IsConditionalExprType() const + { + return HasTypeFlag(TypeFlag::CONDITION_EXPRESSION_TYPE); + } + + bool IsConstantType() const + { + return HasTypeFlag(checker::TypeFlag::CONSTANT); + } + TypeFlag TypeFlags() const { return type_flags_; @@ -208,6 +218,11 @@ public: return 0; } + virtual std::tuple ResolveConditionExpr() const + { + UNREACHABLE(); + }; + virtual void Identical(TypeRelation *relation, Type *other); virtual void AssignmentTarget(TypeRelation *relation, Type *source) = 0; virtual bool AssignmentSource(TypeRelation *relation, Type *target); diff --git a/ets2panda/checker/types/typeFlag.h b/ets2panda/checker/types/typeFlag.h index 5bc36dad5cc0f7d65d7230392a56ebf5d5df97a8..bf8c29c6aa6d3073b73a40775f212be4b32d61fb 100644 --- a/ets2panda/checker/types/typeFlag.h +++ b/ets2panda/checker/types/typeFlag.h @@ -79,6 +79,7 @@ enum class TypeFlag : uint64_t { GETTER = 1ULL << 54ULL, // ETS Getter SETTER = 1ULL << 55ULL, // ETS Setter ETS_EXTENSION_FUNC_HELPER = 1ULL << 56ULL, // ETS Extension Function Helper + ETS_UNION = 1ULL << 57ULL, // ETS union ETS_DYNAMIC_TYPE = ETS_OBJECT | ETS_DYNAMIC_FLAG, ETS_DYNAMIC_FUNCTION_TYPE = FUNCTION | ETS_DYNAMIC_FLAG, ETS_TYPE = BYTE | SHORT | INT | LONG | FLOAT | DOUBLE | CHAR | ETS_BOOLEAN | ETS_VOID | ETS_OBJECT | ETS_ARRAY | @@ -127,7 +128,9 @@ enum class TypeFlag : uint64_t { POSSIBLY_FALSY = DEFINITELY_FALSY | STRING | NUMBER | BOOLEAN | BIGINT, VALID_ARITHMETIC_TYPE = ANY | NUMBER_LIKE | BIGINT_LIKE | ENUM, UNIT = LITERAL | UNIQUE_SYMBOL | NULLABLE, - GETTER_SETTER = GETTER | SETTER + GETTER_SETTER = GETTER | SETTER, + CONDITION_EXPRESSION_TYPE = + NULLABLE | CONSTANT | ETS_OBJECT | BYTE | SHORT | INT | LONG | FLOAT | DOUBLE | ETS_BOOLEAN | ETS_ARRAY }; DEFINE_BITOPS(TypeFlag) diff --git a/ets2panda/checker/types/typeMapping.h b/ets2panda/checker/types/typeMapping.h index 6fe7873d22fb286f648e7b0f6d635bc5fb03e4b3..3d6d406d80b40dfd1b2daba10cc8576180a702cf 100644 --- a/ets2panda/checker/types/typeMapping.h +++ b/ets2panda/checker/types/typeMapping.h @@ -53,6 +53,7 @@ _(TypeFlag::FUNCTION, ETSFunctionType) \ _(TypeFlag::ETS_OBJECT, ETSObjectType) \ _(TypeFlag::ETS_ARRAY, ETSArrayType) \ + _(TypeFlag::ETS_UNION, ETSUnionType) \ _(TypeFlag::NON_PRIMITIVE, NonPrimitiveType) \ _(TypeFlag::WILDCARD, WildcardType) \ _(TypeFlag::ETS_TYPE_PARAMETER, ETSTypeParameter) \ diff --git a/ets2panda/compiler/base/condition.cpp b/ets2panda/compiler/base/condition.cpp index 7ebaf1929e5691f8312b8b56436f0ed46b7b5f46..a90b5f02022d2093998b8981d1014babcf93509e 100644 --- a/ets2panda/compiler/base/condition.cpp +++ b/ets2panda/compiler/base/condition.cpp @@ -17,7 +17,9 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" +#include "ir/expressions/assignmentExpression.h" #include "ir/expressions/binaryExpression.h" +#include "ir/expressions/callExpression.h" #include "ir/expressions/unaryExpression.h" namespace panda::es2panda::compiler { @@ -91,11 +93,29 @@ void Condition::Compile(PandaGen *pg, const ir::Expression *expr, Label *false_l pg->BranchIfFalse(expr, false_label); } -Condition::Result Condition::CheckConstantExpr(const ir::Expression *expr) +Condition::Result Condition::CheckConstantExpr(ETSGen *etsg, const ir::Expression *expr) { - if (expr->TsType()->HasTypeFlag(checker::TypeFlag::CONSTANT)) { - auto res = expr->TsType()->AsETSBooleanType()->GetValue(); - return res ? Result::CONST_TRUE : Result::CONST_FALSE; + const auto resulting_expression = [](const ir::Expression *e) { + if (e->IsBinaryExpression() && e->AsBinaryExpression()->IsLogicalExtended()) { + return e->AsBinaryExpression()->Result(); + } + if (e->IsAssignmentExpression() && e->AsAssignmentExpression()->IsLogicalExtended()) { + return e->AsAssignmentExpression()->Result(); + } + return e; + }(expr); + + if (resulting_expression == nullptr) { + return Result::UNKNOWN; + } + + if (etsg->Checker()->IsNullOrVoidExpression(resulting_expression)) { + return Result::CONST_FALSE; + } + + auto expr_res = resulting_expression->TsType()->ResolveConditionExpr(); + if (std::get<0>(expr_res)) { + return std::get<1>(expr_res) ? Result::CONST_TRUE : Result::CONST_FALSE; } return Result::UNKNOWN; @@ -129,10 +149,12 @@ void Condition::Compile(ETSGen *etsg, const ir::Expression *expr, Label *false_l case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: { bin_expr->Left()->Compile(etsg); etsg->ApplyConversion(bin_expr->Left(), bin_expr->OperationType()); + etsg->ResolveConditionalResultIfFalse(bin_expr->Left(), false_label); etsg->BranchIfFalse(bin_expr, false_label); bin_expr->Right()->Compile(etsg); etsg->ApplyConversion(bin_expr->Right(), bin_expr->OperationType()); + etsg->ResolveConditionalResultIfFalse(bin_expr->Right(), false_label); etsg->BranchIfFalse(bin_expr, false_label); return; } @@ -141,10 +163,12 @@ void Condition::Compile(ETSGen *etsg, const ir::Expression *expr, Label *false_l bin_expr->Left()->Compile(etsg); etsg->ApplyConversion(bin_expr->Left(), bin_expr->OperationType()); + etsg->ResolveConditionalResultIfTrue(bin_expr->Left(), end_label); etsg->BranchIfTrue(bin_expr, end_label); bin_expr->Right()->Compile(etsg); etsg->ApplyConversion(bin_expr->Right(), bin_expr->OperationType()); + etsg->ResolveConditionalResultIfFalse(bin_expr->Right(), false_label); etsg->BranchIfFalse(bin_expr, false_label); etsg->SetLabel(bin_expr, end_label); return; @@ -157,17 +181,14 @@ void Condition::Compile(ETSGen *etsg, const ir::Expression *expr, Label *false_l expr->AsUnaryExpression()->OperatorType() == lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK) { expr->AsUnaryExpression()->Argument()->Compile(etsg); etsg->ApplyConversion(expr->AsUnaryExpression()->Argument(), etsg->Checker()->GlobalETSBooleanType()); + etsg->ResolveConditionalResultIfTrue(expr, false_label); etsg->BranchIfTrue(expr, false_label); return; } - - // TODO(user): Handle implicit bool conversion: not zero int == true, not null obj ref == true, otherwise false - ASSERT(expr->TsType()->IsETSBooleanType() || - (expr->TsType()->IsETSObjectType() && - expr->TsType()->AsETSObjectType()->HasObjectFlag( - checker::ETSObjectFlags::BUILTIN_BOOLEAN))); // already checked by checker::CheckTruthinessOfType() + ASSERT(expr->TsType()->IsConditionalExprType()); expr->Compile(etsg); etsg->ApplyConversion(expr, etsg->Checker()->GlobalETSBooleanType()); + etsg->ResolveConditionalResultIfFalse(expr, false_label); etsg->BranchIfFalse(expr, false_label); } } // namespace panda::es2panda::compiler diff --git a/ets2panda/compiler/base/condition.h b/ets2panda/compiler/base/condition.h index 85071b3a40643b8ddf78e2f63e2b126ace3156ae..0fe3e98036601f210fad8e45067d1c30d9ca308e 100644 --- a/ets2panda/compiler/base/condition.h +++ b/ets2panda/compiler/base/condition.h @@ -35,7 +35,7 @@ public: static void Compile(PandaGen *pg, const ir::Expression *expr, Label *false_label); static void Compile(ETSGen *etsg, const ir::Expression *expr, Label *false_label); - static Result CheckConstantExpr(const ir::Expression *expr); + static Result CheckConstantExpr(ETSGen *etsg, const ir::Expression *expr); }; } // namespace panda::es2panda::compiler diff --git a/ets2panda/compiler/base/lreference.cpp b/ets2panda/compiler/base/lreference.cpp index 1202ef83796f520c4fd6a7d0ae55edaabec352bd..beb7f4d11a1d33953147aa2bbe2e2f82febb66d5 100644 --- a/ets2panda/compiler/base/lreference.cpp +++ b/ets2panda/compiler/base/lreference.cpp @@ -21,6 +21,7 @@ #include "compiler/core/function.h" #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" +#include "checker/types/ets/etsUnionType.h" #include "ir/astNode.h" #include "ir/base/spreadElement.h" #include "ir/base/classProperty.h" @@ -319,8 +320,13 @@ void ETSLReference::SetValue() const if (static_obj_ref_->IsETSDynamicType()) { auto lang = static_obj_ref_->AsETSDynamicType()->Language(); etsg_->StorePropertyDynamic(Node(), member_expr->TsType(), base_reg_, prop_name, lang); + } else if (static_obj_ref_->IsETSUnionType()) { + etsg_->StoreUnionProperty(Node(), base_reg_, prop_name); } else { auto type = etsg_->Checker()->MaybeBoxedType(member_expr->PropVar(), etsg_->Allocator()); + if (type->IsETSUnionType()) { + type = type->AsETSUnionType()->GetLeastUpperBoundType(); + } etsg_->StoreProperty(Node(), type, base_reg_, prop_name); } break; diff --git a/ets2panda/compiler/core/ASTCompiler.h b/ets2panda/compiler/core/ASTCompiler.h index b5bd6661794f22acfc3e000542ef9b1282941eda..03fdde033adf00ef433610758d7e7dbcbdccb6c0 100644 --- a/ets2panda/compiler/core/ASTCompiler.h +++ b/ets2panda/compiler/core/ASTCompiler.h @@ -17,6 +17,152 @@ #define ES2PANDA_COMPILER_CORE_ASTCOMPILER_H #include "compiler/core/dynamicContext.h" +#include "ir/opaqueTypeNode.h" +#include "ir/as/namedType.h" +#include "ir/as/prefixAssertionExpression.h" +#include "ir/base/catchClause.h" +#include "ir/base/classDefinition.h" +#include "ir/base/classProperty.h" +#include "ir/base/classStaticBlock.h" +#include "ir/base/decorator.h" +#include "ir/base/metaProperty.h" +#include "ir/base/methodDefinition.h" +#include "ir/base/property.h" +#include "ir/base/scriptFunction.h" +#include "ir/base/spreadElement.h" +#include "ir/base/templateElement.h" +#include "ir/base/tsIndexSignature.h" +#include "ir/base/tsMethodSignature.h" +#include "ir/base/tsPropertySignature.h" +#include "ir/base/tsSignatureDeclaration.h" +#include "ir/ets/etsClassLiteral.h" +#include "ir/ets/etsFunctionType.h" +#include "ir/ets/etsImportDeclaration.h" +#include "ir/ets/etsLaunchExpression.h" +#include "ir/ets/etsNewArrayInstanceExpression.h" +#include "ir/ets/etsNewClassInstanceExpression.h" +#include "ir/ets/etsNewMultiDimArrayInstanceExpression.h" +#include "ir/ets/etsPackageDeclaration.h" +#include "ir/ets/etsParameterExpression.h" +#include "ir/ets/etsPrimitiveType.h" +#include "ir/ets/etsScript.h" +#include "ir/ets/etsStructDeclaration.h" +#include "ir/ets/etsTypeReference.h" +#include "ir/ets/etsTypeReferencePart.h" +#include "ir/ets/etsWildcardType.h" +#include "ir/expressions/arrayExpression.h" +#include "ir/expressions/arrowFunctionExpression.h" +#include "ir/expressions/assignmentExpression.h" +#include "ir/expressions/awaitExpression.h" +#include "ir/expressions/binaryExpression.h" +#include "ir/expressions/callExpression.h" +#include "ir/expressions/chainExpression.h" +#include "ir/expressions/classExpression.h" +#include "ir/expressions/conditionalExpression.h" +#include "ir/expressions/directEvalExpression.h" +#include "ir/expressions/functionExpression.h" +#include "ir/expressions/identifier.h" +#include "ir/expressions/importExpression.h" +#include "ir/expressions/memberExpression.h" +#include "ir/expressions/newExpression.h" +#include "ir/expressions/objectExpression.h" +#include "ir/expressions/omittedExpression.h" +#include "ir/expressions/sequenceExpression.h" +#include "ir/expressions/superExpression.h" +#include "ir/expressions/taggedTemplateExpression.h" +#include "ir/expressions/templateLiteral.h" +#include "ir/expressions/thisExpression.h" +#include "ir/expressions/unaryExpression.h" +#include "ir/expressions/updateExpression.h" +#include "ir/expressions/yieldExpression.h" +#include "ir/expressions/literals/bigIntLiteral.h" +#include "ir/expressions/literals/booleanLiteral.h" +#include "ir/expressions/literals/charLiteral.h" +#include "ir/expressions/literals/nullLiteral.h" +#include "ir/expressions/literals/numberLiteral.h" +#include "ir/expressions/literals/regExpLiteral.h" +#include "ir/expressions/literals/stringLiteral.h" +#include "ir/module/exportAllDeclaration.h" +#include "ir/module/exportDefaultDeclaration.h" +#include "ir/module/exportNamedDeclaration.h" +#include "ir/module/exportSpecifier.h" +#include "ir/module/importDeclaration.h" +#include "ir/module/importDefaultSpecifier.h" +#include "ir/module/importNamespaceSpecifier.h" +#include "ir/module/importSpecifier.h" +#include "ir/statements/assertStatement.h" +#include "ir/statements/blockStatement.h" +#include "ir/statements/breakStatement.h" +#include "ir/statements/classDeclaration.h" +#include "ir/statements/continueStatement.h" +#include "ir/statements/debuggerStatement.h" +#include "ir/statements/doWhileStatement.h" +#include "ir/statements/emptyStatement.h" +#include "ir/statements/expressionStatement.h" +#include "ir/statements/forInStatement.h" +#include "ir/statements/forOfStatement.h" +#include "ir/statements/forUpdateStatement.h" +#include "ir/statements/functionDeclaration.h" +#include "ir/statements/ifStatement.h" +#include "ir/statements/labelledStatement.h" +#include "ir/statements/returnStatement.h" +#include "ir/statements/switchCaseStatement.h" +#include "ir/statements/switchStatement.h" +#include "ir/statements/throwStatement.h" +#include "ir/statements/tryStatement.h" +#include "ir/statements/variableDeclaration.h" +#include "ir/statements/variableDeclarator.h" +#include "ir/statements/whileStatement.h" +#include "ir/ts/tsAnyKeyword.h" +#include "ir/ts/tsArrayType.h" +#include "ir/ts/tsAsExpression.h" +#include "ir/ts/tsBigintKeyword.h" +#include "ir/ts/tsBooleanKeyword.h" +#include "ir/ts/tsClassImplements.h" +#include "ir/ts/tsConditionalType.h" +#include "ir/ts/tsConstructorType.h" +#include "ir/ts/tsEnumDeclaration.h" +#include "ir/ts/tsEnumMember.h" +#include "ir/ts/tsExternalModuleReference.h" +#include "ir/ts/tsFunctionType.h" +#include "ir/ts/tsImportEqualsDeclaration.h" +#include "ir/ts/tsImportType.h" +#include "ir/ts/tsIndexedAccessType.h" +#include "ir/ts/tsInferType.h" +#include "ir/ts/tsInterfaceBody.h" +#include "ir/ts/tsInterfaceDeclaration.h" +#include "ir/ts/tsInterfaceHeritage.h" +#include "ir/ts/tsIntersectionType.h" +#include "ir/ts/tsLiteralType.h" +#include "ir/ts/tsMappedType.h" +#include "ir/ts/tsModuleBlock.h" +#include "ir/ts/tsModuleDeclaration.h" +#include "ir/ts/tsNamedTupleMember.h" +#include "ir/ts/tsNeverKeyword.h" +#include "ir/ts/tsNonNullExpression.h" +#include "ir/ts/tsNullKeyword.h" +#include "ir/ts/tsNumberKeyword.h" +#include "ir/ts/tsObjectKeyword.h" +#include "ir/ts/tsParameterProperty.h" +#include "ir/ts/tsParenthesizedType.h" +#include "ir/ts/tsQualifiedName.h" +#include "ir/ts/tsStringKeyword.h" +#include "ir/ts/tsThisType.h" +#include "ir/ts/tsTupleType.h" +#include "ir/ts/tsTypeAliasDeclaration.h" +#include "ir/ts/tsTypeAssertion.h" +#include "ir/ts/tsTypeLiteral.h" +#include "ir/ts/tsTypeOperator.h" +#include "ir/ts/tsTypeParameterDeclaration.h" +#include "ir/ts/tsTypeParameter.h" +#include "ir/ts/tsTypeParameterInstantiation.h" +#include "ir/ts/tsTypePredicate.h" +#include "ir/ts/tsTypeQuery.h" +#include "ir/ts/tsTypeReference.h" +#include "ir/ts/tsUndefinedKeyword.h" +#include "ir/ts/tsUnionType.h" +#include "ir/ts/tsUnknownKeyword.h" +#include "ir/ts/tsVoidKeyword.h" namespace panda::es2panda::compiler { class CodeGen; diff --git a/ets2panda/compiler/core/ASTVerifier.cpp b/ets2panda/compiler/core/ASTVerifier.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f59438c301e16963727eee4a563f37305bd0c933 --- /dev/null +++ b/ets2panda/compiler/core/ASTVerifier.cpp @@ -0,0 +1,409 @@ +/** + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ASTVerifier.h" + +#include "es2panda.h" +#include "binder/variableFlags.h" +#include "binder/scope.h" +#include "ir/astNode.h" +#include "ir/base/catchClause.h" +#include "ir/base/classDefinition.h" +#include "ir/base/classStaticBlock.h" +#include "ir/base/methodDefinition.h" +#include "ir/base/scriptFunction.h" +#include "ir/ets/etsFunctionType.h" +#include "ir/ets/etsNewClassInstanceExpression.h" +#include "ir/ets/etsPackageDeclaration.h" +#include "ir/ets/etsParameterExpression.h" +#include "ir/ets/etsTypeReference.h" +#include "ir/ets/etsTypeReferencePart.h" +#include "ir/expressions/callExpression.h" +#include "ir/expressions/functionExpression.h" +#include "ir/expressions/identifier.h" +#include "ir/expressions/memberExpression.h" +#include "ir/expressions/literals/numberLiteral.h" +#include "ir/expressions/literals/stringLiteral.h" +#include "ir/statements/blockStatement.h" +#include "ir/statements/classDeclaration.h" +#include "ir/statements/expressionStatement.h" +#include "ir/statements/throwStatement.h" +#include "ir/statements/tryStatement.h" +#include "ir/statements/variableDeclaration.h" +#include "ir/statements/variableDeclarator.h" +#include "ir/ts/tsClassImplements.h" +#include "ir/ts/tsTypeParameter.h" +#include "ir/ts/tsTypeParameterDeclaration.h" +#include "ir/ts/tsTypeParameterInstantiation.h" + +namespace panda::es2panda::compiler { + +bool ASTVerifier::IsCorrectProgram(const parser::Program *program) +{ + bool is_correct = true; + error_messages_.clear(); + + for (auto *statement : program->Ast()->Statements()) { + is_correct &= HaveParents(statement); + } + is_correct &= HaveParents(program->GlobalClass()); + + for (auto *statement : program->Ast()->Statements()) { + is_correct &= HaveTypes(statement); + } + is_correct &= HaveTypes(program->GlobalClass()); + + for (auto *statement : program->Ast()->Statements()) { + is_correct &= HaveVariables(statement); + } + is_correct &= HaveVariables(program->GlobalClass()); + + for (auto *statement : program->Ast()->Statements()) { + is_correct &= HaveScopes(statement); + } + is_correct &= HaveScopes(program->GlobalClass()); + +#ifndef NDEBUG + std::for_each(error_messages_.begin(), error_messages_.end(), [](auto const msg) { LOG(INFO, COMMON) << msg; }); +#endif // NDEBUG + return is_correct; +} + +std::string ToStringHelper(const binder::ScopeType type) +{ + switch (type) { + case binder::ScopeType::CATCH: { + return "CATCH"; + } + case binder::ScopeType::CATCH_PARAM: { + return "CATCH_PARAM"; + } + case binder::ScopeType::CLASS: { + return "CLASS"; + } + case binder::ScopeType::FUNCTION: { + return "FUNCTION"; + } + case binder::ScopeType::FUNCTION_PARAM: { + return "FUNCTION_PARAM"; + } + case binder::ScopeType::GLOBAL: { + return "GLOBAL"; + } + case binder::ScopeType::LOCAL: { + return "LOCAL"; + } + case binder::ScopeType::LOOP: { + return "LOOP"; + } + case binder::ScopeType::LOOP_DECL: { + return "LOOP_DECL"; + } + case binder::ScopeType::MODULE: { + return "MODULE"; + } + case binder::ScopeType::PARAM: { + return "PARAM"; + } + default: { + return "MUST BE UNREACHABLE"; + } + } +} + +std::string ToStringHelper(const util::StringView &name) +{ + return name == nullptr ? "" : name.Mutf8(); +} + +std::string ToStringHelper(const binder::Scope *scope) +{ + if (scope == nullptr) { + return ""; + } + + switch (scope->Type()) { + case binder::ScopeType::FUNCTION: { + return "FUNC_SCOPE " + ToStringHelper(scope->AsFunctionScope()->Name()); + } + case binder::ScopeType::LOCAL: { + return "LOCAL_SCOPE "; + } + case binder::ScopeType::CATCH: { + return "CATCH_SCOPE "; + } + default: { + return "MUST BE UNREACHABLE"; + } + } +} + +std::string ToStringHelper(const binder::Variable *var) +{ + if (var == nullptr) { + return ""; + } + + switch (var->Type()) { + case binder::VariableType::LOCAL: { + return "LOCAL_VAR " + ToStringHelper(var->Name()); + } + case binder::VariableType::MODULE: { + return "MODULE_VAR " + ToStringHelper(var->Name()); + } + case binder::VariableType::GLOBAL: { + return "GLOBAL_VAR " + ToStringHelper(var->Name()); + } + case binder::VariableType::ENUM: { + return "ENUM_VAR " + ToStringHelper(var->Name()); + } + default: { + return "MUST BE UNREACHABLE"; + } + } +} + +template +std::string ToStringParamsHelper(const ir::AstNode *parent, const ArenaVector ¶ms) +{ + std::string name; + if (parent != nullptr) { + name = ToStringHelper(parent) + " "; + } + + name += "("; + for (auto const *param : params) { + name += ToStringHelper(param); + } + + return name + ")"; +} + +std::string ToStringHelper(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return ""; + } + + switch (ast->Type()) { + case ir::AstNodeType::IDENTIFIER: { + return "ID " + ToStringHelper(ast->AsIdentifier()->Name()); + } + case ir::AstNodeType::CLASS_DEFINITION: { + return "CLS_DEF " + ToStringHelper(ast->AsClassDefinition()->Ident()); + } + case ir::AstNodeType::CLASS_DECLARATION: { + return "CLS_DECL " + ToStringHelper(ast->AsClassDeclaration()->Definition()); + } + case ir::AstNodeType::BLOCK_STATEMENT: { + return "BLOCK " + ToStringHelper(ast->AsBlockStatement()->Scope()); + } + case ir::AstNodeType::SCRIPT_FUNCTION: { + auto const *sf = ast->AsScriptFunction(); + return "SCRIPT_FUN " + ToStringHelper(sf->Scope()) + "::" + ToStringHelper(sf->Id()); + } + case ir::AstNodeType::FUNCTION_EXPRESSION: { + return "FUN_EXPR " + ToStringHelper(ast->AsFunctionExpression()->Function()); + } + case ir::AstNodeType::METHOD_DEFINITION: { + return "METHOD_DEF " + ToStringHelper(ast->AsMethodDefinition()->Value()); + } + case ir::AstNodeType::ETS_TYPE_REFERENCE_PART: { + return "TYPE_REF_PART " + ToStringHelper(ast->AsETSTypeReferencePart()->Name()); + } + case ir::AstNodeType::ETS_TYPE_REFERENCE: { + return "TYPE_REF " + ToStringHelper(ast->AsETSTypeReference()->Part()); + } + case ir::AstNodeType::VARIABLE_DECLARATOR: { + return "VAR_DECLARATOR " + ToStringHelper(ast->AsVariableDeclarator()->Id()); + } + case ir::AstNodeType::VARIABLE_DECLARATION: { + if (ast->AsVariableDeclaration()->Declarators().empty()) { + return "VAR_DECLARATION "; + } + return "VAR_DECLARATION " + ToStringHelper(ast->AsVariableDeclaration()->Declarators().at(0)); + } + case ir::AstNodeType::CALL_EXPRESSION: { + return "CALL_EXPR " + ToStringHelper(ast->AsCallExpression()->Callee()) + "(...)"; + } + case ir::AstNodeType::EXPRESSION_STATEMENT: { + return "EXPR_STMT " + ToStringHelper(ast->AsExpressionStatement()->GetExpression()); + } + case ir::AstNodeType::MEMBER_EXPRESSION: { + auto const *me = ast->AsMemberExpression(); + return "MEMBER_EXPR " + ToStringHelper(me->Object()) + "." + ToStringHelper(me->Property()); + } + case ir::AstNodeType::CLASS_STATIC_BLOCK: { + return "CLS_STATIC_BLOCK " + ToStringHelper(ast->AsClassStaticBlock()->Function()); + } + case ir::AstNodeType::ETS_PACKAGE_DECLARATION: { + return "PKG_DECL "; + } + case ir::AstNodeType::TS_TYPE_PARAMETER_DECLARATION: { + return "PARAM_DECL " + ToStringParamsHelper( + ast->Parent(), ast->AsTSTypeParameterDeclaration()->Params()); + } + case ir::AstNodeType::TS_TYPE_PARAMETER: { + return "TYPE_PARAM " + ToStringHelper(ast->AsTSTypeParameter()->Name()); + } + case ir::AstNodeType::TS_TYPE_PARAMETER_INSTANTIATION: { + return "PARAM_INSTANTIATION " + + ToStringParamsHelper(ast->Parent(), ast->AsTSTypeParameterInstantiation()->Params()); + } + case ir::AstNodeType::THROW_STATEMENT: { + return "THROW_STMT " + ToStringHelper(ast->AsThrowStatement()->Argument()); + } + case ir::AstNodeType::ETS_NEW_CLASS_INSTANCE_EXPRESSION: { + return "NEW_CLS_INSTANCE " + ToStringHelper(ast->AsETSNewClassInstanceExpression()->GetTypeRef()); + } + case ir::AstNodeType::STRING_LITERAL: { + return "STR_LITERAL " + ToStringHelper(ast->AsStringLiteral()->Str()); + } + case ir::AstNodeType::TRY_STATEMENT: { + return "TRY_STMT " + ToStringHelper(ast->AsTryStatement()->Block()); + } + case ir::AstNodeType::CATCH_CLAUSE: { + return "CATCH_CLAUSE "; + } + case ir::AstNodeType::NUMBER_LITERAL: { + return "NUMBER_LITERAL " + ToStringHelper(ast->AsNumberLiteral()->Str()); + } + case ir::AstNodeType::ETS_PARAMETER_EXPRESSION: { + return "ETS_PARAM_EXPR " + ToStringHelper(ast->AsETSParameterExpression()->Ident()); + } + case ir::AstNodeType::TS_INTERFACE_DECLARATION: { + return "TS_INTERFACE_DECL " + ToStringHelper(ast->AsTSInterfaceDeclaration()->Id()); + } + case ir::AstNodeType::TS_INTERFACE_BODY: { + return "TS_INTERFACE_BODY "; + } + case ir::AstNodeType::ETS_FUNCTION_TYPE: { + return "ETS_FUNC_TYPE " + + ToStringParamsHelper(ast->Parent(), ast->AsETSFunctionType()->Params()); + } + case ir::AstNodeType::TS_CLASS_IMPLEMENTS: { + return "TS_CLASS_IMPL " + ToStringHelper(ast->AsTSClassImplements()->Expr()); + } + default: { + return "MUST BE UNREACHABLE"; + } + } +} + +bool ASTVerifier::HasParent(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + if (ast->Parent() == nullptr) { + error_messages_.push_back("NULL_PARENT: " + ToStringHelper(ast)); + return false; + } + + return true; +} + +bool ASTVerifier::HaveParents(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + bool has_parent = HasParent(ast); + ast->IterateRecursively([this, &has_parent](ir::AstNode *child) { has_parent &= HasParent(child); }); + return has_parent; +} + +bool ASTVerifier::HasType(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + if (ast->IsTyped() && static_cast(ast)->TsType() == nullptr) { + error_messages_.push_back("NULL_TS_TYPE: " + ToStringHelper(ast)); + return false; + } + return true; +} + +bool ASTVerifier::HaveTypes(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + bool has_type = HasType(ast); + ast->IterateRecursively([this, &has_type](ir::AstNode *child) { has_type &= HasType(child); }); + return has_type; +} + +bool ASTVerifier::HasVariable(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + if (!ast->IsIdentifier() || ast->AsIdentifier()->Variable() != nullptr) { + return true; + } + + error_messages_.push_back("NULL_VARIABLE: " + ToStringHelper(ast->AsIdentifier())); + return false; +} + +bool ASTVerifier::HaveVariables(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + bool has_variable = HasVariable(ast); + ast->IterateRecursively([this, &has_variable](ir::AstNode *child) { has_variable &= HasVariable(child); }); + return has_variable; +} + +bool ASTVerifier::HasScope(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + if (!ast->IsIdentifier()) { + return true; // we will check only Identifier + } + // we will check only local variables of identifiers + if (HasVariable(ast) && ast->AsIdentifier()->Variable()->IsLocalVariable() && + ast->AsIdentifier()->Variable()->AsLocalVariable()->GetScope() == nullptr) { + error_messages_.push_back("NULL_SCOPE_LOCAL_VAR: " + ToStringHelper(ast)); + return false; + } + // TODO(tatiana): Add check that the scope enclose this identifier + return true; +} + +bool ASTVerifier::HaveScopes(const ir::AstNode *ast) +{ + if (ast == nullptr) { + return false; + } + + bool has_scope = HasScope(ast); + ast->IterateRecursively([this, &has_scope](ir::AstNode *child) { has_scope &= HasScope(child); }); + return has_scope; +} + +} // namespace panda::es2panda::compiler diff --git a/ets2panda/compiler/core/ASTVerifier.h b/ets2panda/compiler/core/ASTVerifier.h new file mode 100644 index 0000000000000000000000000000000000000000..d9bdba1180982ab20a5050b1b263dfe8f9db2cda --- /dev/null +++ b/ets2panda/compiler/core/ASTVerifier.h @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_COMPILER_CORE_ASTVERIFIER_H +#define ES2PANDA_COMPILER_CORE_ASTVERIFIER_H + +#include "parser/program/program.h" + +namespace panda::es2panda::compiler { + +class ASTVerifier { +public: + using ErrorMessages = std::vector; + NO_COPY_SEMANTIC(ASTVerifier); + NO_MOVE_SEMANTIC(ASTVerifier); + + ASTVerifier() = default; + ~ASTVerifier() = default; + + bool IsCorrectProgram(const parser::Program *program); + bool HaveParents(const ir::AstNode *ast); + bool HasParent(const ir::AstNode *ast); + bool HaveTypes(const ir::AstNode *ast); + bool HasType(const ir::AstNode *ast); + bool HaveVariables(const ir::AstNode *ast); + bool HasVariable(const ir::AstNode *ast); + bool HasScope(const ir::AstNode *ast); + bool HaveScopes(const ir::AstNode *ast); + + ErrorMessages GetErrorMessages() + { + return error_messages_; + } + +private: + ErrorMessages error_messages_; +}; + +std::string ToStringHelper(const ir::AstNode *ast); + +} // namespace panda::es2panda::compiler + +#endif // ES2PANDA_COMPILER_CORE_ASTVERIFIER_H diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index c7f91c0d7962a2e2b8797f073f71b64e0e9c642f..82fa834957ec8d1e1064dd095772782b3c8558ef 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -15,13 +15,11 @@ #include "ETSCompiler.h" +#include "checker/types/ets/etsDynamicFunctionType.h" +#include "compiler/base/condition.h" #include "compiler/base/lreference.h" #include "compiler/core/ETSGen.h" -#include "ir/base/catchClause.h" -#include "ir/base/classProperty.h" -#include "ir/expressions/identifier.h" -#include "ir/statements/blockStatement.h" -#include "ir/statements/returnStatement.h" +#include "compiler/function/functionBuilder.h" namespace panda::es2panda::compiler { @@ -198,39 +196,43 @@ void ETSCompiler::Compile(const ir::ETSPackageDeclaration *st) const void ETSCompiler::Compile(const ir::ETSParameterExpression *expr) const { - (void)expr; - UNREACHABLE(); + ETSGen *etsg = GetETSGen(); + expr->Ident()->Identifier::Compile(etsg); } -void ETSCompiler::Compile(const ir::ETSPrimitiveType *node) const +void ETSCompiler::Compile([[maybe_unused]] const ir::ETSPrimitiveType *node) const { - (void)node; UNREACHABLE(); } -void ETSCompiler::Compile(const ir::ETSStructDeclaration *node) const +void ETSCompiler::Compile([[maybe_unused]] const ir::ETSStructDeclaration *node) const { - (void)node; UNREACHABLE(); } void ETSCompiler::Compile(const ir::ETSTypeReference *node) const { - (void)node; - UNREACHABLE(); + ETSGen *etsg = GetETSGen(); + node->Part()->Compile(etsg); } void ETSCompiler::Compile(const ir::ETSTypeReferencePart *node) const { - (void)node; - UNREACHABLE(); + ETSGen *etsg = GetETSGen(); + node->Name()->Compile(etsg); } -void ETSCompiler::Compile(const ir::ETSWildcardType *node) const +void ETSCompiler::Compile(const ir::ETSUnionType *node) const { (void)node; UNREACHABLE(); } + +void ETSCompiler::Compile([[maybe_unused]] const ir::ETSWildcardType *node) const +{ + ETSGen *etsg = GetETSGen(); + etsg->Unimplemented(); +} // compile methods for EXPRESSIONS in alphabetical order void ETSCompiler::Compile(const ir::ArrayExpression *expr) const { @@ -240,8 +242,23 @@ void ETSCompiler::Compile(const ir::ArrayExpression *expr) const void ETSCompiler::Compile(const ir::ArrowFunctionExpression *expr) const { - (void)expr; - UNREACHABLE(); + ETSGen *etsg = GetETSGen(); + ASSERT(expr->ResolvedLambda() != nullptr); + auto *ctor = expr->ResolvedLambda()->TsType()->AsETSObjectType()->ConstructSignatures()[0]; + std::vector arguments; + + for (auto *it : expr->CapturedVars()) { + if (it->HasFlag(binder::VariableFlags::LOCAL)) { + arguments.push_back(it->AsLocalVariable()->Vreg()); + } + } + + if (expr->propagate_this_) { + arguments.push_back(etsg->GetThisReg()); + } + + etsg->InitLambdaObject(expr, ctor, arguments); + etsg->SetAccumulatorType(expr->resolved_lambda_->TsType()); } void ETSCompiler::Compile(const ir::AssignmentExpression *expr) const diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 04fa62fea3b7c5b54a51eb49c30cce01c565ac03..ba7490684ceb13ab0010f16c36444e557ecca929 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -21,6 +21,7 @@ #include "ir/expressions/assignmentExpression.h" #include "ir/expressions/identifier.h" #include "ir/expressions/binaryExpression.h" +#include "ir/expressions/callExpression.h" #include "ir/expressions/memberExpression.h" #include "ir/expressions/templateLiteral.h" #include "ir/statements/breakStatement.h" @@ -113,7 +114,7 @@ void ETSGen::StoreAccumulator(const ir::AstNode *const node, const VReg vreg) { const auto *const acc_type = GetAccumulatorType(); - if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Ra().Emit(node, vreg); } else if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, vreg); @@ -128,7 +129,7 @@ void ETSGen::LoadAccumulator(const ir::AstNode *node, VReg vreg) { const auto *const vreg_type = GetVRegType(vreg); - if (vreg_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (vreg_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Ra().Emit(node, vreg); } else if (vreg_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, vreg); @@ -144,7 +145,7 @@ IRNode *ETSGen::AllocMov(const ir::AstNode *const node, const VReg vd, const VRe const auto *const source_type = GetVRegType(vs); auto *const mov = [this, source_type, node, vd, vs]() -> IRNode * { - if (source_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (source_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { return Allocator()->New(node, vd, vs); } if (source_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { @@ -182,7 +183,7 @@ void ETSGen::MoveVreg(const ir::AstNode *const node, const VReg vd, const VReg v { const auto *const source_type = GetVRegType(vs); - if (source_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (source_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Ra().Emit(node, vd, vs); } else if (source_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, vd, vs); @@ -368,7 +369,7 @@ void ETSGen::StoreStaticOwnProperty(const ir::AstNode *node, const checker::Type void ETSGen::StoreStaticProperty(const ir::AstNode *const node, const checker::Type *prop_type, const util::StringView &full_name) { - if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Sa().Emit(node, full_name); } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Sa().Emit(node, full_name); @@ -380,7 +381,7 @@ void ETSGen::StoreStaticProperty(const ir::AstNode *const node, const checker::T void ETSGen::LoadStaticProperty(const ir::AstNode *const node, const checker::Type *prop_type, const util::StringView &full_name) { - if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Sa().Emit(node, full_name); } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Sa().Emit(node, full_name); @@ -414,7 +415,7 @@ void ETSGen::LoadProperty(const ir::AstNode *const node, const checker::Type *pr if (node->IsIdentifier() && node->AsIdentifier()->Variable()->HasFlag(binder::VariableFlags::BOXED)) { prop_type = Checker()->GlobalBuiltinBoxType(prop_type); } - if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Ra().Emit(node, obj_reg, full_name); } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, obj_reg, full_name); @@ -425,6 +426,28 @@ void ETSGen::LoadProperty(const ir::AstNode *const node, const checker::Type *pr SetAccumulatorType(prop_type); } +void ETSGen::StoreUnionProperty([[maybe_unused]] const ir::AstNode *node, [[maybe_unused]] VReg obj_reg, + [[maybe_unused]] const util::StringView &prop_name) +{ +#ifdef PANDA_WITH_ETS + Ra().Emit(node, obj_reg, prop_name); +#else + UNREACHABLE(); +#endif // PANDA_WITH_ETS +} + +void ETSGen::LoadUnionProperty([[maybe_unused]] const ir::AstNode *const node, + [[maybe_unused]] const checker::Type *prop_type, [[maybe_unused]] const VReg obj_reg, + [[maybe_unused]] const util::StringView &prop_name) +{ +#ifdef PANDA_WITH_ETS + Ra().Emit(node, obj_reg, prop_name); + SetAccumulatorType(prop_type); +#else + UNREACHABLE(); +#endif // PANDA_WITH_ETS +} + void ETSGen::StorePropertyDynamic(const ir::AstNode *node, const checker::Type *prop_type, VReg obj_reg, const util::StringView &prop_name, Language lang) { @@ -657,6 +680,9 @@ VReg ETSGen::GetThisReg() const void ETSGen::LoadDefaultValue([[maybe_unused]] const ir::AstNode *node, [[maybe_unused]] const checker::Type *type) { + if (type->IsETSUnionType()) { + type = Checker()->GetGlobalTypesHolder()->GlobalETSObjectType(); + } if (type->IsETSObjectType() || type->IsETSArrayType()) { LoadAccumulatorNull(node, type); } else if (type->IsETSBooleanType()) { @@ -682,7 +708,7 @@ void ETSGen::ReturnAcc(const ir::AstNode *node) { const auto *const acc_type = GetAccumulatorType(); - if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { Sa().Emit(node); } else if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Sa().Emit(node); @@ -1423,7 +1449,7 @@ void ETSGen::CastToInt(const ir::AstNode *node) void ETSGen::CastToArrayOrObject(const ir::AstNode *const node, const checker::Type *const target_type, const bool unchecked) { - ASSERT(GetAccumulatorType()->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)); + ASSERT(GetAccumulatorType()->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)); const auto *const source_type = GetAccumulatorType(); if (source_type->IsETSDynamicType() && target_type->IsETSDynamicType()) { @@ -1653,7 +1679,7 @@ void ETSGen::CastDynamicTo(const ir::AstNode *node, enum checker::TypeFlag type_ void ETSGen::EmitCheckedNarrowingReferenceConversion(const ir::AstNode *const node, const checker::Type *const target_type) { - ASSERT(target_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)); + ASSERT(target_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)); if (target_type->IsETSObjectType()) { Sa().Emit(node, target_type->AsETSObjectType()->AssemblerName()); @@ -1878,7 +1904,8 @@ void ETSGen::Negate(const ir::AstNode *node) void ETSGen::LogicalNot(const ir::AstNode *node) { - ASSERT(GetAccumulatorType()->IsETSBooleanType()); + ASSERT(GetAccumulatorType()->IsConditionalExprType()); + ResolveConditionalResultIfFalse(node); Sa().Emit(node, 1); } @@ -2237,6 +2264,18 @@ void ETSGen::LoadStringLength(const ir::AstNode *node) SetAccumulatorType(Checker()->GlobalIntType()); } +void ETSGen::FloatIsNaN(const ir::AstNode *node) +{ + Ra().Emit(node, Signatures::BUILTIN_FLOAT_IS_NAN, dummy_reg_, 0); + SetAccumulatorType(Checker()->GlobalETSBooleanType()); +} + +void ETSGen::DoubleIsNaN(const ir::AstNode *node) +{ + Ra().Emit(node, Signatures::BUILTIN_DOUBLE_IS_NAN, dummy_reg_, 0); + SetAccumulatorType(Checker()->GlobalETSBooleanType()); +} + void ETSGen::LoadStringChar(const ir::AstNode *node, const VReg string_obj, const VReg char_index) { Ra().Emit(node, Signatures::BUILTIN_STRING_CHAR_AT, string_obj, char_index); diff --git a/ets2panda/compiler/core/ETSGen.h b/ets2panda/compiler/core/ETSGen.h index c70e92e3786f1a7531de00d7a2c9fd9371268cc5..358a1ea1dd913c97a24887f7a3ee9448718dd754 100644 --- a/ets2panda/compiler/core/ETSGen.h +++ b/ets2panda/compiler/core/ETSGen.h @@ -77,6 +77,10 @@ public: void StoreElementDynamic(const ir::AstNode *node, VReg object_reg, VReg index, Language lang); void LoadElementDynamic(const ir::AstNode *node, VReg object_reg, Language lang); + void StoreUnionProperty(const ir::AstNode *node, VReg obj_reg, const util::StringView &name); + void LoadUnionProperty(const ir::AstNode *node, const checker::Type *prop_type, VReg obj_reg, + const util::StringView &prop_name); + void LoadUndefinedDynamic(const ir::AstNode *node, Language lang); void LoadThis(const ir::AstNode *node); @@ -96,6 +100,132 @@ public: bool TryLoadConstantExpression(const ir::Expression *node); void Condition(const ir::AstNode *node, lexer::TokenType op, VReg lhs, Label *if_false); + template + void ResolveConditionalResult(const ir::AstNode *node, [[maybe_unused]] Label *if_false) + { + auto type = node->IsExpression() ? node->AsExpression()->TsType() : GetAccumulatorType(); + if (type->IsETSObjectType() && + type->AsETSObjectType()->HasObjectFlag(panda::es2panda::checker::ETSObjectFlags::UNBOXABLE_TYPE)) { + type = GetAccumulatorType(); + } + if (type->IsETSBooleanType()) { + return; + } + + if (node->IsExpression()) { + auto expr_node = node->AsExpression(); + if (Checker()->IsNullOrVoidExpression(expr_node)) { + if constexpr (USE_FALSE_LABEL) { + Branch(node, if_false); + } else { + Sa().Emit(node, 0); + } + return; + } + } + Label *if_nullable {nullptr}; + Label *end {nullptr}; + if (type->IsNullableType()) { + if constexpr (USE_FALSE_LABEL) { + BranchIfNull(node, if_false); + } else { + if_nullable = AllocLabel(); + end = AllocLabel(); + BranchIfNull(node, if_nullable); + } + } + if (type->IsETSArrayType()) { + compiler::VReg obj_reg = AllocReg(); + StoreAccumulator(node, obj_reg); + LoadArrayLength(node, obj_reg); + } else if (type->IsETSObjectType()) { + if (type->IsETSStringType()) { + LoadStringLength(node); + if constexpr (BEFORE_LOGICAL_NOT) { + Label *zero_lenth = AllocLabel(); + BranchIfFalse(node, zero_lenth); + ToBinaryResult(node, zero_lenth); + } + } else { + Sa().Emit(node, 1); + } + } else { + switch (type->TypeFlags()) { + case checker::TypeFlag::LONG: { + CastToInt(node); + [[fallthrough]]; + } + case checker::TypeFlag::BYTE: + case checker::TypeFlag::CHAR: + case checker::TypeFlag::SHORT: + case checker::TypeFlag::INT: { + if constexpr (BEFORE_LOGICAL_NOT) { + Label *zero_primitive = AllocLabel(); + BranchIfFalse(node, zero_primitive); + ToBinaryResult(node, zero_primitive); + } + break; + } + case checker::TypeFlag::DOUBLE: + case checker::TypeFlag::FLOAT: { + VReg tmp_reg = AllocReg(); + StoreAccumulator(node, tmp_reg); + if (type->IsFloatType()) { + FloatIsNaN(node); + } else { + DoubleIsNaN(node); + } + Sa().Emit(node, 1); + + auto real_end_label = [](Label *end_label, Label *if_false_label, ETSGen *etsgn, + bool use_false_label) { + if (use_false_label) { + return if_false_label; + } + if (end_label == nullptr) { + end_label = etsgn->AllocLabel(); + } + return end_label; + }(end, if_false, this, USE_FALSE_LABEL); + BranchIfFalse(node, real_end_label); + + LoadAccumulator(node, tmp_reg); + VReg zero_reg = AllocReg(); + if (type->IsFloatType()) { + MoveImmediateToRegister(node, zero_reg, checker::TypeFlag::FLOAT, 0); + BinaryNumberComparison(node, zero_reg, real_end_label); + } else { + MoveImmediateToRegister(node, zero_reg, checker::TypeFlag::DOUBLE, 0); + BinaryNumberComparison(node, zero_reg, real_end_label); + } + break; + } + default: + break; + } + } + if (if_nullable != nullptr) { + Branch(node, end); + SetLabel(node, if_nullable); + Sa().Emit(node, 0); + } + if (end != nullptr) { + SetLabel(node, end); + } + } + + template + void ResolveConditionalResultIfFalse(const ir::AstNode *node, Label *if_false = nullptr) + { + ResolveConditionalResult(node, if_false); + } + + template + void ResolveConditionalResultIfTrue(const ir::AstNode *node, Label *if_false = nullptr) + { + ResolveConditionalResult(node, if_false); + } + void BranchIfFalse(const ir::AstNode *node, Label *if_false) { Sa().Emit(node, if_false); @@ -305,6 +435,9 @@ public: void LoadStringLength(const ir::AstNode *node); void LoadStringChar(const ir::AstNode *node, VReg string_obj, VReg char_index); + void FloatIsNaN(const ir::AstNode *node); + void DoubleIsNaN(const ir::AstNode *node); + void CompileStatements(const ArenaVector &statements); // Cast @@ -500,9 +633,12 @@ private: template void ConditionalBranching(const ir::AstNode *node, Label *if_false) { - SetAccumulatorType(Checker()->GlobalETSBooleanType()); if constexpr (std::is_same_v) { - LogicalNot(node); + ResolveConditionalResultIfTrue(node, if_false); + Sa().Emit(node); + Sa().Emit(node, 1); + } else { + ResolveConditionalResultIfFalse(node, if_false); } BranchIfFalse(node, if_false); } @@ -519,6 +655,7 @@ private: } else { CallThisVirtual1(node, lhs, Signatures::BUILTIN_OBJECT_EQUALS, arg0); } + SetAccumulatorType(Checker()->GlobalETSBooleanType()); ConditionalBranching(node, if_false); JumpTo(node, if_true); @@ -923,8 +1060,9 @@ private: template void ETSGen::LoadAccumulatorNumber(const ir::AstNode *node, T number, checker::TypeFlag target_type) { - auto type_kind = - target_type_ && !target_type_->IsETSObjectType() ? checker::ETSChecker::TypeKind(target_type_) : target_type; + auto type_kind = target_type_ && (!target_type_->IsETSObjectType() && !target_type_->IsETSUnionType()) + ? checker::ETSChecker::TypeKind(target_type_) + : target_type; switch (type_kind) { case checker::TypeFlag::ETS_BOOLEAN: diff --git a/ets2panda/compiler/core/JSCompiler.cpp b/ets2panda/compiler/core/JSCompiler.cpp index a0b4815892d893a6432ee139e5fcea744c7a4b0c..02dc8226aba51a33faa49625781feba84580a63c 100644 --- a/ets2panda/compiler/core/JSCompiler.cpp +++ b/ets2panda/compiler/core/JSCompiler.cpp @@ -15,18 +15,10 @@ #include "JSCompiler.h" +#include "compiler/base/condition.h" #include "compiler/base/lreference.h" #include "compiler/core/pandagen.h" -#include "ir/base/catchClause.h" -#include "ir/base/classDefinition.h" -#include "ir/base/classProperty.h" -#include "ir/base/classStaticBlock.h" -#include "ir/base/methodDefinition.h" -#include "ir/base/scriptFunction.h" -#include "ir/expressions/functionExpression.h" -#include "ir/expressions/identifier.h" -#include "ir/statements/blockStatement.h" -#include "ir/statements/returnStatement.h" +#include "compiler/function/functionBuilder.h" #include "util/helpers.h" namespace panda::es2panda::compiler { @@ -523,39 +515,39 @@ void JSCompiler::Compile(const ir::ETSPackageDeclaration *expr) const UNREACHABLE(); } -void JSCompiler::Compile(const ir::ETSParameterExpression *expr) const +void JSCompiler::Compile([[maybe_unused]] const ir::ETSParameterExpression *expr) const { - (void)expr; UNREACHABLE(); } -void JSCompiler::Compile(const ir::ETSPrimitiveType *expr) const +void JSCompiler::Compile([[maybe_unused]] const ir::ETSPrimitiveType *expr) const { - (void)expr; UNREACHABLE(); } -void JSCompiler::Compile(const ir::ETSStructDeclaration *node) const +void JSCompiler::Compile([[maybe_unused]] const ir::ETSStructDeclaration *node) const { - (void)node; UNREACHABLE(); } -void JSCompiler::Compile(const ir::ETSTypeReference *expr) const +void JSCompiler::Compile([[maybe_unused]] const ir::ETSTypeReference *expr) const { - (void)expr; UNREACHABLE(); } -void JSCompiler::Compile(const ir::ETSTypeReferencePart *expr) const +void JSCompiler::Compile([[maybe_unused]] const ir::ETSTypeReferencePart *expr) const { - (void)expr; UNREACHABLE(); } -void JSCompiler::Compile(const ir::ETSWildcardType *expr) const +void JSCompiler::Compile(const ir::ETSUnionType *node) const +{ + (void)node; + UNREACHABLE(); +} + +void JSCompiler::Compile([[maybe_unused]] const ir::ETSWildcardType *expr) const { - (void)expr; UNREACHABLE(); } @@ -568,8 +560,8 @@ void JSCompiler::Compile(const ir::ArrayExpression *expr) const void JSCompiler::Compile(const ir::ArrowFunctionExpression *expr) const { - (void)expr; - UNREACHABLE(); + PandaGen *pg = GetPandaGen(); + pg->DefineFunction(expr->Function(), expr->Function(), expr->Function()->Scope()->InternalName()); } void JSCompiler::Compile(const ir::AssignmentExpression *expr) const diff --git a/ets2panda/compiler/lowering/checkerPhase.h b/ets2panda/compiler/lowering/checkerPhase.h index 1fac2594bdedb9b13810871d0fd33ada56a722da..e20a4a7f930f1bc36d9dbf1501cff9982cc7d572 100644 --- a/ets2panda/compiler/lowering/checkerPhase.h +++ b/ets2panda/compiler/lowering/checkerPhase.h @@ -20,9 +20,10 @@ namespace panda::es2panda::compiler { class CheckerPhase : public Phase { - util::StringView Name() override + std::string const &Name() override { - return "checker"; + static std::string const NAME = "checker"; + return NAME; } bool Perform(CompilerContext *ctx, parser::Program *program) override; diff --git a/ets2panda/compiler/lowering/ets/generateDeclarations.h b/ets2panda/compiler/lowering/ets/generateDeclarations.h index a3fd6bdebabd1ee74547de7b34fc951288d74f78..ababf09c9af78f8fdbcc0c46baedf472ac933684 100644 --- a/ets2panda/compiler/lowering/ets/generateDeclarations.h +++ b/ets2panda/compiler/lowering/ets/generateDeclarations.h @@ -22,9 +22,10 @@ namespace panda::es2panda::compiler { class GenerateTsDeclarationsPhase : public Phase { public: - util::StringView Name() override + std::string const &Name() override { - return "generate-ts-declarations"; + static std::string const NAME = "generate-ts-declarations"; + return NAME; } bool Perform(CompilerContext *ctx, parser::Program *program) override; }; diff --git a/ets2panda/compiler/lowering/ets/opAssignment.cpp b/ets2panda/compiler/lowering/ets/opAssignment.cpp index 31913c1a395f74a6ed6f065381c2876bdd962dcd..61859393893227e01e3c5cf6bde31c997adc582e 100644 --- a/ets2panda/compiler/lowering/ets/opAssignment.cpp +++ b/ets2panda/compiler/lowering/ets/opAssignment.cpp @@ -44,9 +44,10 @@ namespace panda::es2panda::compiler { -util::StringView OpAssignmentLowering::Name() +std::string const &OpAssignmentLowering::Name() { - return "op-assignment"; + static std::string const NAME = "op-assignment"; + return NAME; } struct Conversion { @@ -147,7 +148,7 @@ void AdjustBoxingUnboxingFlags(ir::Expression *new_expr, const ir::Expression *o ir::Expression *HandleOpAssignment(checker::ETSChecker *checker, ir::AssignmentExpression *assignment) { - if (assignment->TsType() == nullptr) { // hasn't been throiugh checker + if (assignment->TsType() == nullptr) { // hasn't been through checker return assignment; } diff --git a/ets2panda/compiler/lowering/ets/opAssignment.h b/ets2panda/compiler/lowering/ets/opAssignment.h index f85facad8559bcdd92720fa0b337ce700bff768d..077d18ad755fd1b7084566655eea20c07585e409 100644 --- a/ets2panda/compiler/lowering/ets/opAssignment.h +++ b/ets2panda/compiler/lowering/ets/opAssignment.h @@ -22,7 +22,7 @@ namespace panda::es2panda::compiler { class OpAssignmentLowering : public Phase { public: - util::StringView Name() override; + std::string const &Name() override; bool Perform(CompilerContext *ctx, parser::Program *program) override; bool Postcondition(CompilerContext *ctx, const parser::Program *program) override; }; diff --git a/ets2panda/compiler/lowering/ets/unionLowering.cpp b/ets2panda/compiler/lowering/ets/unionLowering.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7664b76457f60535a425735aa5ea4edbd7bc9094 --- /dev/null +++ b/ets2panda/compiler/lowering/ets/unionLowering.cpp @@ -0,0 +1,219 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "unionLowering.h" +#include "binder/variableFlags.h" +#include "binder/ETSBinder.h" +#include "checker/ETSchecker.h" +#include "compiler/core/compilerContext.h" +#include "ir/base/classDefinition.h" +#include "ir/base/classProperty.h" +#include "ir/astNode.h" +#include "ir/expression.h" +#include "ir/opaqueTypeNode.h" +#include "ir/ets/etsParameterExpression.h" +#include "ir/expressions/assignmentExpression.h" +#include "ir/expressions/binaryExpression.h" +#include "ir/expressions/identifier.h" +#include "ir/expressions/functionExpression.h" +#include "ir/expressions/memberExpression.h" +#include "ir/expressions/sequenceExpression.h" +#include "ir/statements/blockStatement.h" +#include "ir/statements/classDeclaration.h" +#include "ir/ts/tsAsExpression.h" +#include "type_helper.h" + +namespace panda::es2panda::compiler { + +std::string const &UnionLowering::Name() +{ + static std::string const NAME = "union-property-access"; + return NAME; +} + +ir::ClassDefinition *CreateUnionFieldClass(checker::ETSChecker *checker, binder::Binder *binder) +{ + // Create the name for the synthetic class node + util::UString union_field_class_name(util::StringView(panda_file::GetDummyClassName()), checker->Allocator()); + binder::Variable *found_var = nullptr; + if ((found_var = checker->Scope()->FindLocal(union_field_class_name.View())) != nullptr) { + return found_var->Declaration()->Node()->AsClassDeclaration()->Definition(); + } + auto *ident = checker->AllocNode(union_field_class_name.View(), checker->Allocator()); + auto [decl, var] = binder->NewVarDecl(ident->Start(), ident->Name()); + ident->SetVariable(var); + + auto class_ctx = binder::LexicalScope(binder); + auto *class_def = checker->AllocNode(checker->Allocator(), class_ctx.GetScope(), ident, + ir::ClassDefinitionModifiers::GLOBAL, + ir::ModifierFlags::NONE, Language(Language::Id::ETS)); + + auto *class_decl = checker->AllocNode(class_def, checker->Allocator()); + class_def->Scope()->BindNode(class_decl); + class_def->SetTsType(checker->GlobalETSObjectType()); + decl->BindNode(class_decl); + var->SetScope(class_def->Scope()); + + binder->AsETSBinder()->BuildClassDefinition(class_def); + return class_def; +} + +void CreateUnionFieldClassProperty(ArenaAllocator *allocator, binder::Binder *binder, ir::ClassDefinition *class_def, + checker::Type *field_type, const util::StringView &prop_name) +{ + auto *class_scope = class_def->Scope()->AsClassScope(); + // Enter the union filed class instance field scope + auto field_ctx = binder::LexicalScope::Enter(binder, class_scope->InstanceFieldScope()); + + if (class_scope->FindLocal(prop_name, binder::ResolveBindingOptions::VARIABLES) != nullptr) { + return; + } + + // Create field name for synthetic class + auto *field_ident = allocator->New(prop_name, allocator); + + // Create the synthetic class property node + auto *field = + allocator->New(field_ident, nullptr, nullptr, ir::ModifierFlags::NONE, allocator, false); + + // Add the declaration to the scope + auto [decl, var] = binder->NewVarDecl(field_ident->Start(), field_ident->Name()); + var->AddFlag(binder::VariableFlags::PROPERTY); + var->SetTsType(field_type); + field_ident->SetVariable(var); + field->SetTsType(field_type); + decl->BindNode(field); + + ArenaVector field_decl {allocator->Adapter()}; + field_decl.push_back(field); + class_def->AddProperties(std::move(field_decl)); +} + +ir::Expression *HandleUnionPropertyAccess(checker::ETSChecker *checker, binder::Binder *binder, + ir::MemberExpression *expr) +{ + auto *class_def = CreateUnionFieldClass(checker, binder); + CreateUnionFieldClassProperty(checker->Allocator(), binder, class_def, expr->PropVar()->TsType(), + expr->Property()->AsIdentifier()->Name()); + if (expr->Object()->IsIdentifier()) { + auto *new_ts_type = expr->Object()->TsType()->AsETSUnionType()->GetLeastUpperBoundType(checker); + expr->Object()->AsIdentifier()->Variable()->SetTsType(new_ts_type); + } + return expr; +} + +ir::Expression *HandleUnionFunctionParameter(checker::ETSChecker *checker, ir::ETSParameterExpression *param) +{ + auto *union_type = param->Ident()->Variable()->TsType()->AsETSUnionType(); + param->Ident()->Variable()->SetTsType(union_type->GetLeastUpperBoundType(checker)); + return param; +} + +ir::Expression *HandleBinaryExpressionWithUnion(checker::ETSChecker *checker, ir::BinaryExpression *expr) +{ + auto *union_type = expr->OperationType()->AsETSUnionType(); + ir::Expression *union_node; + ir::Expression *other_union_node = nullptr; + checker::Type *other_node_type; + if (expr->Left()->TsType()->IsETSUnionType()) { + union_node = expr->Left(); + other_node_type = expr->Right()->TsType(); + if (other_node_type->IsETSUnionType()) { + other_union_node = expr->Right(); + } + } else { + union_node = expr->Right(); + other_node_type = expr->Left()->TsType(); + } + auto *source_type = + union_type->AsETSUnionType()->FindTypeIsCastableToSomeType(union_node, checker->Relation(), other_node_type); + if (source_type == nullptr) { + checker->ThrowTypeError("Bad operand type, some type of the union must be the same type as other expression.", + expr->Start()); + } + if ((union_node->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG) != 0U && + source_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + union_node->SetBoxingUnboxingFlags(ir::BoxingUnboxingFlags::NONE); + } + auto *union_type_node = checker->AllocNode(source_type); + auto *as_expression = checker->AllocNode(union_node, union_type_node, false); + as_expression->SetParent(expr); + expr->SetOperationType(source_type); + if (other_union_node != nullptr) { + auto *other_union_type_node = checker->AllocNode(other_node_type); + auto *other_as_expression = + checker->AllocNode(other_union_node, other_union_type_node, false); + other_as_expression->SetParent(expr); + } + expr->SetTsType(checker->GlobalETSBooleanType()); + return expr; +} + +bool UnionLowering::Perform(CompilerContext *ctx, parser::Program *program) +{ + for (auto &[_, ext_programs] : program->ExternalSources()) { + (void)_; + for (auto *ext_prog : ext_programs) { + Perform(ctx, ext_prog); + } + } + + checker::ETSChecker *checker = ctx->Checker()->AsETSChecker(); + + program->Ast()->TransformChildrenRecursively([checker, ctx](ir::AstNode *ast) -> ir::AstNode * { + if (ast->IsMemberExpression() && ast->AsMemberExpression()->Object()->TsType() != nullptr && + ast->AsMemberExpression()->Object()->TsType()->IsETSUnionType()) { + return HandleUnionPropertyAccess(checker, ctx->Binder(), ast->AsMemberExpression()); + } + + if (ast->IsETSParameterExpression() && + ast->AsETSParameterExpression()->Ident()->Variable()->TsType() != nullptr && + ast->AsETSParameterExpression()->Ident()->Variable()->TsType()->IsETSUnionType()) { + return HandleUnionFunctionParameter(checker, ast->AsETSParameterExpression()); + } + + if (ast->IsBinaryExpression() && ast->AsBinaryExpression()->OperationType() != nullptr && + ast->AsBinaryExpression()->OperationType()->IsETSUnionType()) { + return HandleBinaryExpressionWithUnion(checker, ast->AsBinaryExpression()); + } + + return ast; + }); + + return true; +} + +bool UnionLowering::Postcondition(CompilerContext *ctx, const parser::Program *program) +{ + if (ctx->Options()->compilation_mode == CompilationMode::GEN_STD_LIB) { + for (auto &[_, ext_programs] : program->ExternalSources()) { + (void)_; + for (auto *ext_prog : ext_programs) { + if (!Postcondition(ctx, ext_prog)) { + return false; + } + } + } + } + + return !program->Ast()->IsAnyChild([](const ir::AstNode *ast) { + return ast->IsMemberExpression() && ast->AsMemberExpression()->Object()->TsType() != nullptr && + ast->IsMemberExpression() && ast->AsMemberExpression()->Object()->TsType()->IsETSUnionType() && + ast->AsMemberExpression()->Object()->IsIdentifier() && + ast->AsMemberExpression()->Object()->AsIdentifier()->Variable()->TsType()->IsETSUnionType(); + }); +} + +} // namespace panda::es2panda::compiler diff --git a/ets2panda/compiler/lowering/ets/unionLowering.h b/ets2panda/compiler/lowering/ets/unionLowering.h new file mode 100644 index 0000000000000000000000000000000000000000..beddab61fefe5071cf775359b7414cfd7ea96f9c --- /dev/null +++ b/ets2panda/compiler/lowering/ets/unionLowering.h @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_COMPILER_LOWERING_UNION_LOWERING_H +#define ES2PANDA_COMPILER_LOWERING_UNION_LOWERING_H + +#include "compiler/lowering/phase.h" + +namespace panda::es2panda::compiler { + +class UnionLowering : public Phase { +public: + std::string const &Name() override; + bool Perform(CompilerContext *ctx, parser::Program *program) override; + bool Postcondition(CompilerContext *ctx, const parser::Program *program) override; +}; + +} // namespace panda::es2panda::compiler + +#endif diff --git a/ets2panda/compiler/lowering/phase.cpp b/ets2panda/compiler/lowering/phase.cpp index 5877c6e4907ef23c263a0290514724a6c3c3becb..9da2c19beddd1d5d01645eabee576700cba8c18d 100644 --- a/ets2panda/compiler/lowering/phase.cpp +++ b/ets2panda/compiler/lowering/phase.cpp @@ -15,11 +15,13 @@ #include "phase.h" #include "checker/checker.h" +#include "compiler/core/ASTVerifier.h" #include "compiler/core/compilerContext.h" #include "lexer/token/sourceLocation.h" #include "compiler/lowering/checkerPhase.h" #include "compiler/lowering/ets/generateDeclarations.h" #include "compiler/lowering/ets/opAssignment.h" +#include "compiler/lowering/ets/unionLowering.h" namespace panda::es2panda::compiler { @@ -34,6 +36,7 @@ std::vector GetTrivialPhaseList() static GenerateTsDeclarationsPhase GENERATE_TS_DECLARATIONS_PHASE; static OpAssignmentLowering OP_ASSIGNMENT_LOWERING; +static UnionLowering UNION_LOWERING; std::vector GetETSPhaseList() { @@ -41,6 +44,7 @@ std::vector GetETSPhaseList() &CHECKER_PHASE, &GENERATE_TS_DECLARATIONS_PHASE, &OP_ASSIGNMENT_LOWERING, + &UNION_LOWERING, }; } @@ -57,8 +61,13 @@ bool Phase::Apply(CompilerContext *ctx, parser::Program *program) } #ifndef NDEBUG + ASTVerifier ast_before; + if (!ast_before.IsCorrectProgram(program)) { + // TODO(tatiana): Add some error processing + } if (!Precondition(ctx, program)) { - ctx->Checker()->ThrowTypeError({"Precondition check failed for ", Name()}, lexer::SourcePosition {}); + ctx->Checker()->ThrowTypeError({"Precondition check failed for ", util::StringView {Name()}}, + lexer::SourcePosition {}); } #endif @@ -72,8 +81,13 @@ bool Phase::Apply(CompilerContext *ctx, parser::Program *program) } #ifndef NDEBUG + ASTVerifier ast_after; + if (!ast_after.IsCorrectProgram(program)) { + // TODO(tatiana): Add some error processing + } if (!Postcondition(ctx, program)) { - ctx->Checker()->ThrowTypeError({"Postcondition check failed for ", Name()}, lexer::SourcePosition {}); + ctx->Checker()->ThrowTypeError({"Postcondition check failed for ", util::StringView {Name()}}, + lexer::SourcePosition {}); } #endif diff --git a/ets2panda/compiler/lowering/phase.h b/ets2panda/compiler/lowering/phase.h index 07a304620e38b37dfc8f5dea29289bde36d0b8ba..25ea0226835352976e5e165596c7d1c5dc8a0f90 100644 --- a/ets2panda/compiler/lowering/phase.h +++ b/ets2panda/compiler/lowering/phase.h @@ -25,7 +25,7 @@ public: /* If Apply returns false, processing is stopped. */ bool Apply(CompilerContext *ctx, parser::Program *program); - virtual util::StringView Name() = 0; + virtual std::string const &Name() = 0; virtual bool Precondition([[maybe_unused]] CompilerContext *ctx, [[maybe_unused]] const parser::Program *program) { diff --git a/ets2panda/compiler/scripts/signatures.yaml b/ets2panda/compiler/scripts/signatures.yaml index 35f30fef05a85fdd091786419c521dc594dc8bde..0f2a2e8dc1a580211a2867ac346d91bd59545991 100644 --- a/ets2panda/compiler/scripts/signatures.yaml +++ b/ets2panda/compiler/scripts/signatures.yaml @@ -451,6 +451,18 @@ signatures: return_type: BUILTIN_DOUBLE ref: BUILTIN_DOUBLE_VALUE_OF + - callee: BUILTIN_FLOAT + method_name: isNaN + params: [PRIMITIVE_FLOAT] + return_type: PRIMITIVE_BOOLEAN + ref: BUILTIN_FLOAT_IS_NAN + + - callee: BUILTIN_DOUBLE + method_name: isNaN + params: [PRIMITIVE_DOUBLE] + return_type: PRIMITIVE_BOOLEAN + ref: BUILTIN_DOUBLE_IS_NAN + - callee: BUILTIN_BOOLEAN method_name: unboxed params: [] diff --git a/ets2panda/es2panda.h b/ets2panda/es2panda.h index e0bfd92142537c09b70e3357831e2dd3d15d7260..f0a135c0e1fff43a26af958e4b076b2f80af18ee 100644 --- a/ets2panda/es2panda.h +++ b/ets2panda/es2panda.h @@ -99,9 +99,9 @@ struct CompilerOptions { bool parse_only {}; std::string std_lib {}; std::string ts_decl_out {}; - std::unordered_set skip_phases {}; - std::unordered_set dump_before_phases {}; - std::unordered_set dump_after_phases {}; + std::unordered_set skip_phases {}; + std::unordered_set dump_before_phases {}; + std::unordered_set dump_after_phases {}; std::shared_ptr arkts_config {}; CompilationMode compilation_mode {}; // NOLINTEND(misc-non-private-member-variables-in-classes) diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index b9558663a3a25fd3e987cc93a64866b1fa0e4b12..5accd1e728953732abeb88275b0252004feebff8 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -227,6 +227,11 @@ public: return false; } + virtual bool IsTyped() const + { + return false; + } + // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define DECLARE_AS_CASTS(nodeType, className) \ className *As##className() \ @@ -587,6 +592,11 @@ public: ts_type_ = ts_type; } + bool IsTyped() const override + { + return true; + } + protected: explicit Typed(AstNodeType const type) : T(type) {} explicit Typed(AstNodeType const type, ModifierFlags const flags) : T(type, flags) {} diff --git a/ets2panda/ir/astNodeMapping.h b/ets2panda/ir/astNodeMapping.h index 245b7b86369526bbe12cb3b35558e5ef4b6e99de..55cddab5a51684b51edbf436334db68014c77ac2 100644 --- a/ets2panda/ir/astNodeMapping.h +++ b/ets2panda/ir/astNodeMapping.h @@ -82,6 +82,7 @@ _(ETS_CLASS_LITERAL, ETSClassLiteral) \ _(ETS_TYPE_REFERENCE, ETSTypeReference) \ _(ETS_TYPE_REFERENCE_PART, ETSTypeReferencePart) \ + _(ETS_UNION_TYPE, ETSUnionType) \ _(ETS_LAUNCH_EXPRESSION, ETSLaunchExpression) \ _(ETS_NEW_ARRAY_INSTANCE_EXPRESSION, ETSNewArrayInstanceExpression) \ _(ETS_NEW_MULTI_DIM_ARRAY_INSTANCE_EXPRESSION, ETSNewMultiDimArrayInstanceExpression) \ diff --git a/ets2panda/ir/ets/etsParameterExpression.cpp b/ets2panda/ir/ets/etsParameterExpression.cpp index c862ffe69700f0e0eb8df259431497a42cf888d3..30fee78aa4f556aff9461fd5fa689f1cc5183758 100644 --- a/ets2panda/ir/ets/etsParameterExpression.cpp +++ b/ets2panda/ir/ets/etsParameterExpression.cpp @@ -15,9 +15,11 @@ #include "etsParameterExpression.h" -#include "compiler/core/pandagen.h" #include "checker/ETSchecker.h" #include "checker/ets/typeRelationContext.h" +#include "checker/TSchecker.h" +#include "compiler/core/ETSGen.h" +#include "compiler/core/pandagen.h" #include "ir/astDump.h" #include "ir/typeNode.h" #include "ir/expressions/identifier.h" @@ -140,43 +142,24 @@ void ETSParameterExpression::Dump(ir::AstDumper *const dumper) const } } -void ETSParameterExpression::Compile([[maybe_unused]] compiler::PandaGen *const pg) const +void ETSParameterExpression::Compile(compiler::PandaGen *const pg) const { - UNREACHABLE(); + pg->GetAstCompiler()->Compile(this); } -void ETSParameterExpression::Compile([[maybe_unused]] compiler::ETSGen *const etsg) const +void ETSParameterExpression::Compile(compiler::ETSGen *const etsg) const { - ident_->Identifier::Compile(etsg); + etsg->GetAstCompiler()->Compile(this); } -checker::Type *ETSParameterExpression::Check([[maybe_unused]] checker::TSChecker *const checker) +checker::Type *ETSParameterExpression::Check(checker::TSChecker *const checker) { - UNREACHABLE(); + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSParameterExpression::Check(checker::ETSChecker *const checker) { - if (TsType() == nullptr) { - checker::Type *param_type; - - if (ident_->TsType() != nullptr) { - param_type = ident_->TsType(); - } else { - param_type = !IsRestParameter() ? ident_->Check(checker) : spread_->Check(checker); - if (IsDefault()) { - [[maybe_unused]] auto *const init_type = initializer_->Check(checker); - // TODO(ttamas) : fix this aftet nullable fix - // const checker::AssignmentContext ctx(checker->Relation(), initializer_, init_type, name_type, - // initializer_->Start(), - // {"Initializers type is not assignable to the target type"}); - } - } - - SetTsType(param_type); - } - - return TsType(); + return checker->GetAnalyzer()->Check(this); } // NOLINTNEXTLINE(google-default-arguments) diff --git a/ets2panda/ir/ets/etsParameterExpression.h b/ets2panda/ir/ets/etsParameterExpression.h index d63a3dd887cd921d18cacf3f9e98f1a327a0fdb3..1160289e8857c7061ed70be9d20f93637d05f9b3 100644 --- a/ets2panda/ir/ets/etsParameterExpression.h +++ b/ets2panda/ir/ets/etsParameterExpression.h @@ -18,6 +18,10 @@ #include "ir/expression.h" +namespace panda::es2panda::checker { +class ETSAnalyzer; +} // namespace panda::es2panda::checker + namespace panda::es2panda::ir { class ETSParameterExpression final : public Expression { public: @@ -29,6 +33,9 @@ public: explicit ETSParameterExpression(AnnotatedExpression *ident_or_spread, Expression *initializer); + // TODO (csabahurton): friend relationship can be removed once there are getters for private fields + friend class checker::ETSAnalyzer; + [[nodiscard]] const Identifier *Ident() const noexcept; [[nodiscard]] Identifier *Ident() noexcept; diff --git a/ets2panda/ir/ets/etsPrimitiveType.cpp b/ets2panda/ir/ets/etsPrimitiveType.cpp index 820b6fb5448dc8d09b2a45b200d35fd229f549cb..7e722d78c6c25cb8134f8a32d09d8adbfe49b296 100644 --- a/ets2panda/ir/ets/etsPrimitiveType.cpp +++ b/ets2panda/ir/ets/etsPrimitiveType.cpp @@ -15,10 +15,11 @@ #include "etsPrimitiveType.h" -#include "ir/astDump.h" #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "compiler/core/ETSGen.h" +#include "compiler/core/pandagen.h" +#include "ir/astDump.h" namespace panda::es2panda::ir { void ETSPrimitiveType::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -29,16 +30,19 @@ void ETSPrimitiveType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSPrimitiveType"}}); } -void ETSPrimitiveType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} +void ETSPrimitiveType::Compile(compiler::PandaGen *pg) const +{ + pg->GetAstCompiler()->Compile(this); +} -void ETSPrimitiveType::Compile([[maybe_unused]] compiler::ETSGen *etsg) const +void ETSPrimitiveType::Compile(compiler::ETSGen *etsg) const { - UNREACHABLE(); + etsg->GetAstCompiler()->Compile(this); } -checker::Type *ETSPrimitiveType::Check([[maybe_unused]] checker::TSChecker *checker) +checker::Type *ETSPrimitiveType::Check(checker::TSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSPrimitiveType::GetType([[maybe_unused]] checker::TSChecker *checker) @@ -46,9 +50,9 @@ checker::Type *ETSPrimitiveType::GetType([[maybe_unused]] checker::TSChecker *ch return checker->GlobalAnyType(); } -checker::Type *ETSPrimitiveType::Check([[maybe_unused]] checker::ETSChecker *checker) +checker::Type *ETSPrimitiveType::Check(checker::ETSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSPrimitiveType::GetType([[maybe_unused]] checker::ETSChecker *checker) diff --git a/ets2panda/ir/ets/etsPrimitiveType.h b/ets2panda/ir/ets/etsPrimitiveType.h index c8ae085f4585c41133ece704a672f1730826cc76..c394b0019617a7cee0117fe9a666514feda59b01 100644 --- a/ets2panda/ir/ets/etsPrimitiveType.h +++ b/ets2panda/ir/ets/etsPrimitiveType.h @@ -33,11 +33,11 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; - void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; - checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; + void Compile(compiler::PandaGen *pg) const override; + void Compile(compiler::ETSGen *etsg) const override; + checker::Type *Check(checker::TSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Type *Check(checker::ETSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; private: diff --git a/ets2panda/ir/ets/etsStructDeclaration.cpp b/ets2panda/ir/ets/etsStructDeclaration.cpp index 62f8b74cdf5b9d4c5b577779dc33efa932f0d523..3b05f2717a58e9f06fd49b95e29ce09bdb8838d4 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.cpp +++ b/ets2panda/ir/ets/etsStructDeclaration.cpp @@ -15,6 +15,7 @@ #include "etsStructDeclaration.h" +#include "checker/TSchecker.h" #include "compiler/base/lreference.h" #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" @@ -48,24 +49,23 @@ void ETSStructDeclaration::Dump(ir::AstDumper *dumper) const {{"type", "ETSStructDeclaration"}, {"definition", def_}, {"decorators", AstDumper::Optional(decorators_)}}); } -void ETSStructDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const +void ETSStructDeclaration::Compile(compiler::PandaGen *pg) const { - UNREACHABLE(); + pg->GetAstCompiler()->Compile(this); } -void ETSStructDeclaration::Compile([[maybe_unused]] compiler::ETSGen *etsg) const +void ETSStructDeclaration::Compile(compiler::ETSGen *etsg) const { - UNREACHABLE(); + etsg->GetAstCompiler()->Compile(this); } -checker::Type *ETSStructDeclaration::Check([[maybe_unused]] checker::TSChecker *checker) +checker::Type *ETSStructDeclaration::Check(checker::TSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSStructDeclaration::Check(checker::ETSChecker *checker) { - def_->Check(checker); - return nullptr; + return checker->GetAnalyzer()->Check(this); } } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsStructDeclaration.h b/ets2panda/ir/ets/etsStructDeclaration.h index 7a52e9b804388b7722ac9993ddced626184610ec..10a2201ab66054c5439654f7c01363b52520d218 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.h +++ b/ets2panda/ir/ets/etsStructDeclaration.h @@ -54,11 +54,11 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; - void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; + void Compile(compiler::PandaGen *pg) const override; + void Compile(compiler::ETSGen *etsg) const override; - checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Type *Check(checker::TSChecker *checker) override; + checker::Type *Check(checker::ETSChecker *checker) override; private: ClassDefinition *def_; diff --git a/ets2panda/ir/ets/etsTypeReference.cpp b/ets2panda/ir/ets/etsTypeReference.cpp index 849f5234630df198d5d699fb7da1081ed8f802a4..7ac21bd519609eefdad4dd3b0d8ad0d0a1438b1f 100644 --- a/ets2panda/ir/ets/etsTypeReference.cpp +++ b/ets2panda/ir/ets/etsTypeReference.cpp @@ -15,11 +15,13 @@ #include "etsTypeReference.h" +#include "checker/ETSchecker.h" +#include "checker/TSchecker.h" +#include "compiler/core/ETSGen.h" +#include "compiler/core/pandagen.h" #include "ir/astDump.h" #include "ir/ts/tsQualifiedName.h" #include "ir/ets/etsTypeReferencePart.h" -#include "checker/ETSchecker.h" -#include "compiler/core/ETSGen.h" namespace panda::es2panda::ir { void ETSTypeReference::TransformChildren(const NodeTransformer &cb) @@ -60,20 +62,23 @@ void ETSTypeReference::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSTypeReference"}, {"part", part_}}); } -void ETSTypeReference::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} -void ETSTypeReference::Compile([[maybe_unused]] compiler::ETSGen *etsg) const +void ETSTypeReference::Compile(compiler::PandaGen *pg) const +{ + pg->GetAstCompiler()->Compile(this); +} +void ETSTypeReference::Compile(compiler::ETSGen *etsg) const { - part_->Compile(etsg); + etsg->GetAstCompiler()->Compile(this); } -checker::Type *ETSTypeReference::Check([[maybe_unused]] checker::TSChecker *checker) +checker::Type *ETSTypeReference::Check(checker::TSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSTypeReference::Check(checker::ETSChecker *checker) { - return GetType(checker); + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSTypeReference::GetType(checker::ETSChecker *checker) diff --git a/ets2panda/ir/ets/etsTypeReference.h b/ets2panda/ir/ets/etsTypeReference.h index 17b8ef6ce1c30595a325a66579d06005fa28314c..d771dcceb0b8cdfb80668aa6d9861e7ec4d51f04 100644 --- a/ets2panda/ir/ets/etsTypeReference.h +++ b/ets2panda/ir/ets/etsTypeReference.h @@ -41,10 +41,10 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; - void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; - checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + void Compile(compiler::PandaGen *pg) const override; + void Compile(compiler::ETSGen *etsg) const override; + checker::Type *Check(checker::TSChecker *checker) override; + checker::Type *Check(checker::ETSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; private: diff --git a/ets2panda/ir/ets/etsTypeReferencePart.cpp b/ets2panda/ir/ets/etsTypeReferencePart.cpp index 4d86e09056694abe5f068f41ab938457160d3c9e..79a2b4e97a74dcaa63f37e1151bd8c658e2331cd 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.cpp +++ b/ets2panda/ir/ets/etsTypeReferencePart.cpp @@ -15,13 +15,12 @@ #include "etsTypeReferencePart.h" -#include "ir/astDump.h" -#include "ir/expressions/identifier.h" -#include "ir/ts/tsTypeParameterInstantiation.h" -#include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "checker/ets/typeRelationContext.h" +#include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" +#include "compiler/core/pandagen.h" +#include "ir/astDump.h" namespace panda::es2panda::ir { void ETSTypeReferencePart::TransformChildren(const NodeTransformer &cb) @@ -58,20 +57,23 @@ void ETSTypeReferencePart::Dump(ir::AstDumper *dumper) const {"previous", AstDumper::Optional(prev_)}}); } -void ETSTypeReferencePart::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} -void ETSTypeReferencePart::Compile([[maybe_unused]] compiler::ETSGen *etsg) const +void ETSTypeReferencePart::Compile(compiler::PandaGen *pg) const +{ + pg->GetAstCompiler()->Compile(this); +} +void ETSTypeReferencePart::Compile(compiler::ETSGen *etsg) const { - name_->Compile(etsg); + etsg->GetAstCompiler()->Compile(this); } -checker::Type *ETSTypeReferencePart::Check([[maybe_unused]] checker::TSChecker *checker) +checker::Type *ETSTypeReferencePart::Check(checker::TSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSTypeReferencePart::Check(checker::ETSChecker *checker) { - return GetType(checker); + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSTypeReferencePart::GetType(checker::ETSChecker *checker) diff --git a/ets2panda/ir/ets/etsTypeReferencePart.h b/ets2panda/ir/ets/etsTypeReferencePart.h index f8d82bcf2934f5df7b119ff7dd3952b6b5eca2a2..f3e4b8aafa150d7615ff64c1cbaf4c5b84146efd 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.h +++ b/ets2panda/ir/ets/etsTypeReferencePart.h @@ -58,10 +58,10 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; - void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; - checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + void Compile(compiler::PandaGen *pg) const override; + void Compile(compiler::ETSGen *etsg) const override; + checker::Type *Check(checker::TSChecker *checker) override; + checker::Type *Check(checker::ETSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; private: diff --git a/ets2panda/ir/ets/etsUnionType.cpp b/ets2panda/ir/ets/etsUnionType.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e811f5a37d816364392c73030c8f1174b4ce992e --- /dev/null +++ b/ets2panda/ir/ets/etsUnionType.cpp @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "etsUnionType.h" + +#include "checker/ETSchecker.h" +#include "ir/astDump.h" + +namespace panda::es2panda::ir { +void ETSUnionType::TransformChildren(const NodeTransformer &cb) +{ + for (auto *&it : types_) { + it = static_cast(cb(it)); + } +} + +void ETSUnionType::Iterate(const NodeTraverser &cb) const +{ + for (auto *it : types_) { + cb(it); + } +} + +void ETSUnionType::Dump(ir::AstDumper *dumper) const +{ + dumper->Add({{"type", "ETSUnionType"}, {"types", types_}}); +} + +void ETSUnionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} + +checker::Type *ETSUnionType::Check([[maybe_unused]] checker::TSChecker *checker) +{ + return nullptr; +} + +checker::Type *ETSUnionType::Check([[maybe_unused]] checker::ETSChecker *checker) +{ + for (auto *it : types_) { + it->Check(checker); + } + + return GetType(checker); +} + +checker::Type *ETSUnionType::GetType([[maybe_unused]] checker::ETSChecker *checker) +{ + if (TsType() != nullptr) { + return TsType(); + } + + ArenaVector types(checker->Allocator()->Adapter()); + + for (auto *it : types_) { + types.push_back(it->GetType(checker)); + } + + SetTsType(checker->CreateETSUnionType(std::move(types))); + return TsType(); +} +} // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsUnionType.h b/ets2panda/ir/ets/etsUnionType.h new file mode 100644 index 0000000000000000000000000000000000000000..c7f146794c0071eedc11af9c0cbd2535ee17229a --- /dev/null +++ b/ets2panda/ir/ets/etsUnionType.h @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_IR_ETS_UNION_TYPE_H +#define ES2PANDA_IR_ETS_UNION_TYPE_H + +#include "ir/typeNode.h" + +namespace panda::es2panda::ir { +class ETSUnionType : public TypeNode { +public: + explicit ETSUnionType(ArenaVector &&types) + : TypeNode(AstNodeType::ETS_UNION_TYPE), types_(std::move(types)) + { + } + + const ArenaVector &Types() const + { + return types_; + } + + void TransformChildren(const NodeTransformer &cb) override; + void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; + checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; + checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; + +private: + ArenaVector types_; +}; +} // namespace panda::es2panda::ir + +#endif diff --git a/ets2panda/ir/ets/etsWildcardType.cpp b/ets2panda/ir/ets/etsWildcardType.cpp index 07db87b9c6c5e99b1846269df3f178c6b0c3ee33..76c5ea1cc3fa4cc74d2e5b7de0644e290332dc51 100644 --- a/ets2panda/ir/ets/etsWildcardType.cpp +++ b/ets2panda/ir/ets/etsWildcardType.cpp @@ -15,11 +15,12 @@ #include "etsWildcardType.h" -#include "ir/astDump.h" -#include "ir/ets/etsTypeReference.h" -#include "checker/TSchecker.h" #include "checker/ETSchecker.h" +#include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" +#include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/ets/etsTypeReference.h" namespace panda::es2panda::ir { void ETSWildcardType::TransformChildren(const NodeTransformer &cb) @@ -44,16 +45,19 @@ void ETSWildcardType::Dump(ir::AstDumper *dumper) const {"out", AstDumper::Optional(IsOut())}}); } -void ETSWildcardType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} +void ETSWildcardType::Compile(compiler::PandaGen *pg) const +{ + pg->GetAstCompiler()->Compile(this); +} -void ETSWildcardType::Compile([[maybe_unused]] compiler::ETSGen *etsg) const +void ETSWildcardType::Compile(compiler::ETSGen *etsg) const { - etsg->Unimplemented(); + etsg->GetAstCompiler()->Compile(this); } -checker::Type *ETSWildcardType::Check([[maybe_unused]] checker::TSChecker *checker) +checker::Type *ETSWildcardType::Check(checker::TSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSWildcardType::GetType([[maybe_unused]] checker::TSChecker *checker) @@ -61,9 +65,9 @@ checker::Type *ETSWildcardType::GetType([[maybe_unused]] checker::TSChecker *che return nullptr; } -checker::Type *ETSWildcardType::Check([[maybe_unused]] checker::ETSChecker *checker) +checker::Type *ETSWildcardType::Check(checker::ETSChecker *checker) { - return nullptr; + return checker->GetAnalyzer()->Check(this); } checker::Type *ETSWildcardType::GetType([[maybe_unused]] checker::ETSChecker *checker) diff --git a/ets2panda/ir/ets/etsWildcardType.h b/ets2panda/ir/ets/etsWildcardType.h index 4ce8ce2adbed61728aa477c22a1d7fdec22f3a5c..ca14342e0d1e44b64cab6164cb81721b8ba5bfbf 100644 --- a/ets2panda/ir/ets/etsWildcardType.h +++ b/ets2panda/ir/ets/etsWildcardType.h @@ -36,11 +36,11 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; - void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; - checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; + void Compile(compiler::PandaGen *pg) const override; + void Compile(compiler::ETSGen *etsg) const override; + checker::Type *Check(checker::TSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Type *Check(checker::ETSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; private: diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.cpp b/ets2panda/ir/expressions/arrowFunctionExpression.cpp index a7ceaf07c98cedf1e0434fd224eafc22f089b02c..0cb1b3b65fda36d2382bd984c471d1ff3110e718 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.cpp +++ b/ets2panda/ir/expressions/arrowFunctionExpression.cpp @@ -44,124 +44,21 @@ void ArrowFunctionExpression::Dump(ir::AstDumper *dumper) const void ArrowFunctionExpression::Compile(compiler::PandaGen *pg) const { - pg->DefineFunction(func_, func_, func_->Scope()->InternalName()); + pg->GetAstCompiler()->Compile(this); } void ArrowFunctionExpression::Compile(compiler::ETSGen *etsg) const { - ASSERT(resolved_lambda_ != nullptr); - auto *ctor = resolved_lambda_->TsType()->AsETSObjectType()->ConstructSignatures()[0]; - std::vector arguments; - - for (auto *it : captured_vars_) { - if (it->HasFlag(binder::VariableFlags::LOCAL)) { - arguments.push_back(it->AsLocalVariable()->Vreg()); - } - } - - if (propagate_this_) { - arguments.push_back(etsg->GetThisReg()); - } - - etsg->InitLambdaObject(this, ctor, arguments); - etsg->SetAccumulatorType(resolved_lambda_->TsType()); + etsg->GetAstCompiler()->Compile(this); } checker::Type *ArrowFunctionExpression::Check(checker::TSChecker *checker) { - binder::Variable *func_var = nullptr; - - if (func_->Parent()->Parent() != nullptr && func_->Parent()->Parent()->IsVariableDeclarator() && - func_->Parent()->Parent()->AsVariableDeclarator()->Id()->IsIdentifier()) { - func_var = func_->Parent()->Parent()->AsVariableDeclarator()->Id()->AsIdentifier()->Variable(); - } - - checker::ScopeContext scope_ctx(checker, func_->Scope()); - - auto *signature_info = checker->Allocator()->New(checker->Allocator()); - checker->CheckFunctionParameterDeclarations(func_->Params(), signature_info); - - auto *signature = - checker->Allocator()->New(signature_info, checker->GlobalResolvingReturnType(), func_); - checker::Type *func_type = checker->CreateFunctionTypeWithSignature(signature); - - if (func_var != nullptr && func_var->TsType() == nullptr) { - func_var->SetTsType(func_type); - } - - signature->SetReturnType(checker->HandleFunctionReturn(func_)); - - if (!func_->Body()->IsExpression()) { - func_->Body()->Check(checker); - } - - return func_type; + return checker->GetAnalyzer()->Check(this); } checker::Type *ArrowFunctionExpression::Check(checker::ETSChecker *checker) { - if (TsType() != nullptr) { - return TsType(); - } - - auto *func_type = checker->BuildFunctionSignature(func_, false); - - if (Function()->IsAsyncFunc()) { - auto *ret_type = static_cast(Function()->Signature()->ReturnType()); - if (ret_type->AssemblerName() != checker->GlobalBuiltinPromiseType()->AssemblerName()) { - checker->ThrowTypeError("Return type of async lambda must be 'Promise'", Function()->Start()); - } - } - - checker::ScopeContext scope_ctx(checker, func_->Scope()); - - if (checker->HasStatus(checker::CheckerStatus::IN_INSTANCE_EXTENSION_METHOD)) { - /* - example code: - ``` - class A { - prop:number - } - function A.method() { - let a = () => { - console.println(this.prop) - } - } - ``` - here the enclosing class of arrow function should be Class A - */ - checker->Context().SetContainingClass( - checker->Scope()->Find(binder::Binder::MANDATORY_PARAM_THIS).variable->TsType()->AsETSObjectType()); - } - - checker::SavedCheckerContext saved_context(checker, checker->Context().Status(), - checker->Context().ContainingClass()); - checker->AddStatus(checker::CheckerStatus::IN_LAMBDA); - checker->Context().SetContainingSignature(func_type->CallSignatures()[0]); - - auto *body_type = func_->Body()->Check(checker); - - if (func_->Body()->IsExpression()) { - if (func_->ReturnTypeAnnotation() == nullptr) { - func_type->CallSignatures()[0]->SetReturnType(body_type); - } - - checker::AssignmentContext( - checker->Relation(), func_->Body()->AsExpression(), body_type, func_type->CallSignatures()[0]->ReturnType(), - func_->Start(), - {"Return statements return type is not compatible with the containing functions return type"}, - checker::TypeRelationFlag::DIRECT_RETURN); - } - - checker->Context().SetContainingSignature(nullptr); - checker->CheckCapturedVariables(); - - for (auto [var, _] : checker->Context().CapturedVars()) { - (void)_; - captured_vars_.push_back(var); - } - - SetTsType(func_type); - return TsType(); + return checker->GetAnalyzer()->Check(this); } } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.h b/ets2panda/ir/expressions/arrowFunctionExpression.h index 5bcefdb81bafc68f1657f7ef856caeb730c6276c..204ce84ac5bb4e47207b3e8e6ed0344e793d852b 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.h +++ b/ets2panda/ir/expressions/arrowFunctionExpression.h @@ -18,6 +18,10 @@ #include "ir/expression.h" +namespace panda::es2panda::compiler { +class ETSCompiler; +} // namespace panda::es2panda::compiler + namespace panda::es2panda::ir { class ScriptFunction; @@ -27,6 +31,8 @@ public: : Expression(AstNodeType::ARROW_FUNCTION_EXPRESSION), func_(func), captured_vars_(allocator->Adapter()) { } + // TODO (csabahurton): friend relationship can be removed once there are getters for private fields + friend class compiler::ETSCompiler; const ScriptFunction *Function() const { @@ -71,10 +77,10 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; + void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; - checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Type *Check(checker::TSChecker *checker) override; + checker::Type *Check(checker::ETSChecker *checker) override; private: ScriptFunction *func_; diff --git a/ets2panda/ir/expressions/assignmentExpression.cpp b/ets2panda/ir/expressions/assignmentExpression.cpp index bd1d436bc0ca418aae52bf3af3809b9e506475f9..b1e58362d162ec18d73f82e53e7eb916a0329242 100644 --- a/ets2panda/ir/expressions/assignmentExpression.cpp +++ b/ets2panda/ir/expressions/assignmentExpression.cpp @@ -274,7 +274,7 @@ checker::Type *AssignmentExpression::Check([[maybe_unused]] checker::ETSChecker case lexer::TokenType::PUNCTUATOR_BITWISE_OR_EQUAL: case lexer::TokenType::PUNCTUATOR_PLUS_EQUAL: { std::tie(std::ignore, operation_type_) = - checker->CheckBinaryOperator(left_, right_, operator_, Start(), true); + checker->CheckBinaryOperator(left_, right_, this, operator_, Start(), true); auto unboxed_left = checker->ETSBuiltinTypeAsPrimitiveType(left_type); source_type = unboxed_left == nullptr ? left_type : unboxed_left; diff --git a/ets2panda/ir/expressions/assignmentExpression.h b/ets2panda/ir/expressions/assignmentExpression.h index c16b3780ca2c7a53b29ac486ec018c1a47f7dab0..0bbef758cad9ea5d0429f06cfffc6273a7c3a642 100644 --- a/ets2panda/ir/expressions/assignmentExpression.h +++ b/ets2panda/ir/expressions/assignmentExpression.h @@ -65,6 +65,16 @@ public: return right_; } + [[nodiscard]] const Expression *Result() const noexcept + { + return result_; + } + + [[nodiscard]] Expression *Result() noexcept + { + return result_; + } + [[nodiscard]] lexer::TokenType OperatorType() const noexcept { return operator_; @@ -75,6 +85,18 @@ public: return operator_ = token_type; } + void SetResult(Expression *expr) noexcept + { + left_ = expr; + SetStart(left_->Start()); + } + + [[nodiscard]] bool IsLogicalExtended() const noexcept + { + return operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND || + operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR; + } + [[nodiscard]] binder::Variable *Target() noexcept { return target_; @@ -110,6 +132,7 @@ protected: private: Expression *left_ = nullptr; Expression *right_ = nullptr; + Expression *result_ = nullptr; lexer::TokenType operator_; binder::Variable *target_ {}; checker::Type *operation_type_ {}; diff --git a/ets2panda/ir/expressions/binaryExpression.cpp b/ets2panda/ir/expressions/binaryExpression.cpp index 5c041a13fff7fcb6623b34bd93b5e46d9b1f90bc..5b29757730cba8547e8e1282f44e1457ca7f85a8 100644 --- a/ets2panda/ir/expressions/binaryExpression.cpp +++ b/ets2panda/ir/expressions/binaryExpression.cpp @@ -20,11 +20,9 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "compiler/core/regScope.h" -#include "checker/ETSchecker.h" #include "checker/TSchecker.h" #include "ir/astDump.h" #include "ir/expressions/identifier.h" -#include "lexer/token/tokenType.h" namespace panda::es2panda::ir { void BinaryExpression::TransformChildren(const NodeTransformer &cb) @@ -149,20 +147,46 @@ void BinaryExpression::CompileLogical(compiler::ETSGen *etsg) const { auto *end_label = etsg->AllocLabel(); + if (operator_ == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING) { + left_->Compile(etsg); + etsg->ApplyConversion(left_, operation_type_); + etsg->BranchIfNotNull(this, end_label); + right_->Compile(etsg); + etsg->ApplyConversion(right_, operation_type_); + etsg->SetLabel(this, end_label); + return; + } + + ASSERT(IsLogicalExtended()); + auto ttctx = compiler::TargetTypeContext(etsg, OperationType()); + compiler::RegScope rs(etsg); + auto lhs = etsg->AllocReg(); + auto rhs = etsg->AllocReg(); left_->Compile(etsg); - etsg->ApplyConversion(left_, operation_type_); + etsg->ApplyConversionAndStoreAccumulator(left_, lhs, OperationType()); + auto left_false_label = etsg->AllocLabel(); if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND) { - etsg->BranchIfFalse(this, end_label); - } else if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR) { - etsg->BranchIfTrue(this, end_label); + etsg->ResolveConditionalResultIfFalse(left_, left_false_label); + etsg->BranchIfFalse(this, left_false_label); + + right_->Compile(etsg); + etsg->ApplyConversionAndStoreAccumulator(right_, rhs, OperationType()); + etsg->Branch(this, end_label); + + etsg->SetLabel(this, left_false_label); + etsg->LoadAccumulator(this, lhs); } else { - ASSERT(operator_ == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING); - etsg->BranchIfNotNull(this, end_label); - } + etsg->ResolveConditionalResultIfFalse(left_, left_false_label); + etsg->BranchIfFalse(this, left_false_label); - right_->Compile(etsg); - etsg->ApplyConversion(right_, operation_type_); + etsg->LoadAccumulator(this, lhs); + etsg->Branch(this, end_label); + + etsg->SetLabel(this, left_false_label); + right_->Compile(etsg); + etsg->ApplyConversionAndStoreAccumulator(right_, rhs, OperationType()); + } etsg->SetLabel(this, end_label); } @@ -239,7 +263,7 @@ checker::Type *BinaryExpression::Check(checker::ETSChecker *checker) return TsType(); } checker::Type *new_ts_type {nullptr}; - std::tie(new_ts_type, operation_type_) = checker->CheckBinaryOperator(left_, right_, operator_, Start()); + std::tie(new_ts_type, operation_type_) = checker->CheckBinaryOperator(left_, right_, this, operator_, Start()); SetTsType(new_ts_type); return TsType(); } diff --git a/ets2panda/ir/expressions/binaryExpression.h b/ets2panda/ir/expressions/binaryExpression.h index de4488f0c1571d467b2d6f819e940d131e981ef2..c2a1a47bbbc2943a56b1b2822cfa2227ecd71963 100644 --- a/ets2panda/ir/expressions/binaryExpression.h +++ b/ets2panda/ir/expressions/binaryExpression.h @@ -53,6 +53,16 @@ public: return right_; } + [[nodiscard]] const Expression *Result() const noexcept + { + return result_; + } + + [[nodiscard]] Expression *Result() noexcept + { + return result_; + } + [[nodiscard]] lexer::TokenType OperatorType() const noexcept { return operator_; @@ -63,12 +73,24 @@ public: return operator_ <= lexer::TokenType::PUNCTUATOR_LOGICAL_AND; } + [[nodiscard]] bool IsLogicalExtended() const noexcept + { + return operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND || + operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR; + } + void SetLeft(Expression *expr) noexcept { left_ = expr; SetStart(left_->Start()); } + void SetResult(Expression *expr) noexcept + { + left_ = expr; + SetStart(left_->Start()); + } + void SetOperator(lexer::TokenType operator_type) noexcept { operator_ = operator_type; @@ -106,6 +128,7 @@ public: private: Expression *left_ = nullptr; Expression *right_ = nullptr; + Expression *result_ = nullptr; lexer::TokenType operator_; checker::Type *operation_type_ {}; }; diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 6789de0bfbd4157634287be1f72030e85efe57d0..ffb64abcd97a10840aa8ce92e0a61a11375537f8 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -256,6 +256,8 @@ void MemberExpression::Compile(compiler::ETSGen *etsg) const } else if (object_->TsType()->IsETSDynamicType()) { auto lang = object_->TsType()->AsETSDynamicType()->Language(); etsg->LoadPropertyDynamic(this, TsType(), obj_reg, prop_name, lang); + } else if (object_->TsType()->IsETSUnionType()) { + etsg->LoadUnionProperty(this, TsType(), obj_reg, prop_name); } else { const auto full_name = etsg->FormClassPropReference(object_->TsType()->AsETSObjectType(), prop_name); etsg->LoadProperty(this, TsType(), obj_reg, full_name); @@ -355,50 +357,29 @@ checker::Type *MemberExpression::Check(checker::TSChecker *checker) return nullptr; } -checker::Type *MemberExpression::Check(checker::ETSChecker *checker) +checker::Type *MemberExpression::CheckEnumMember(checker::ETSChecker *checker, checker::Type *type) { - if (TsType() != nullptr) { - return TsType(); - } + auto const *const enum_interface = [type]() -> checker::ETSEnumInterface const * { + if (type->IsETSEnumType()) { + return type->AsETSEnumType(); + } + return type->AsETSStringEnumType(); + }(); - if (computed_) { - SetTsType(checker->CheckArrayElementAccess(this)); + if (parent_->Type() == ir::AstNodeType::CALL_EXPRESSION && parent_->AsCallExpression()->Callee() == this) { + auto *const enum_method_type = enum_interface->LookupMethod(checker, object_, property_->AsIdentifier()); + SetTsType(enum_method_type); return TsType(); } - checker::Type *const base_type = object_->Check(checker); - - if (!base_type->IsETSObjectType()) { - if (base_type->IsETSArrayType() && property_->AsIdentifier()->Name().Is("length")) { - SetTsType(checker->GlobalIntType()); - return TsType(); - } - - if (base_type->IsETSEnumType() || base_type->IsETSStringEnumType()) { - auto const *const enum_interface = [base_type]() -> checker::ETSEnumInterface const * { - if (base_type->IsETSEnumType()) { - return base_type->AsETSEnumType(); - } - return base_type->AsETSStringEnumType(); - }(); - - if (parent_->Type() == ir::AstNodeType::CALL_EXPRESSION && parent_->AsCallExpression()->Callee() == this) { - auto *const enum_method_type = - enum_interface->LookupMethod(checker, object_, property_->AsIdentifier()); - SetTsType(enum_method_type); - return TsType(); - } - - auto *const enum_literal_type = enum_interface->LookupConstant(checker, object_, property_->AsIdentifier()); - SetTsType(enum_literal_type); - SetPropVar(enum_literal_type->GetMemberVar()); - return TsType(); - } - - checker->ThrowTypeError({"Cannot access property of non-object or non-enum type"}, object_->Start()); - } + auto *const enum_literal_type = enum_interface->LookupConstant(checker, object_, property_->AsIdentifier()); + SetTsType(enum_literal_type); + SetPropVar(enum_literal_type->GetMemberVar()); + return TsType(); +} - obj_type_ = base_type->AsETSObjectType(); +checker::Type *MemberExpression::CheckObjectMember(checker::ETSChecker *checker) +{ auto resolve_res = checker->ResolveMemberReference(this, obj_type_); ASSERT(!resolve_res.empty()); checker::Type *type_to_set = nullptr; @@ -435,6 +416,61 @@ checker::Type *MemberExpression::Check(checker::ETSChecker *checker) return TsType(); } +checker::Type *MemberExpression::Check(checker::ETSChecker *checker) +{ + if (TsType() != nullptr) { + return TsType(); + } + + if (computed_) { + SetTsType(checker->CheckArrayElementAccess(this)); + return TsType(); + } + + checker::Type *const base_type = object_->Check(checker); + + if (!base_type->IsETSObjectType()) { + if (base_type->IsETSArrayType() && property_->AsIdentifier()->Name().Is("length")) { + SetTsType(checker->GlobalIntType()); + return TsType(); + } + + if (base_type->IsETSUnionType()) { + auto *const union_type = base_type->AsETSUnionType(); + checker::Type *member_type = nullptr; + auto check_member_type = [this, checker, &member_type]() { + if (member_type != nullptr && member_type != TsType()) { + checker->ThrowTypeError("Member type must be the same for all union objects.", Start()); + } + member_type = TsType(); + }; + for (auto *type : union_type->ConstituentTypes()) { + if (type->IsETSObjectType()) { + obj_type_ = type->AsETSObjectType(); + CheckObjectMember(checker); + check_member_type(); + } + + if (type->IsETSEnumType() || base_type->IsETSStringEnumType()) { + CheckEnumMember(checker, type); + check_member_type(); + } + } + obj_type_ = union_type->GetLeastUpperBoundType(checker)->AsETSObjectType(); + return TsType(); + } + + if (base_type->IsETSEnumType() || base_type->IsETSStringEnumType()) { + return CheckEnumMember(checker, base_type); + } + + checker->ThrowTypeError({"Cannot access property of non-object or non-enum type"}, object_->Start()); + } + + obj_type_ = base_type->AsETSObjectType(); + return CheckObjectMember(checker); +} + // NOLINTNEXTLINE(google-default-arguments) Expression *MemberExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { diff --git a/ets2panda/ir/expressions/memberExpression.h b/ets2panda/ir/expressions/memberExpression.h index 4c786300a047026c71d37d898f591a51c82f30e1..9226a7afe14c7ae3f60b0318af49d0301ead0da0 100644 --- a/ets2panda/ir/expressions/memberExpression.h +++ b/ets2panda/ir/expressions/memberExpression.h @@ -17,6 +17,7 @@ #define ES2PANDA_IR_EXPRESSION_MEMBER_EXPRESSION_H #include "binder/variable.h" +#include "checker/types/ets/etsObjectType.h" #include "ir/expression.h" #include "ir/irnode.h" @@ -146,6 +147,9 @@ public: [[nodiscard]] bool IsPrivateReference() const noexcept; + checker::Type *CheckEnumMember(checker::ETSChecker *checker, checker::Type *type); + checker::Type *CheckObjectMember(checker::ETSChecker *checker); + // NOLINTNEXTLINE(google-default-arguments) [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; diff --git a/ets2panda/ir/expressions/unaryExpression.cpp b/ets2panda/ir/expressions/unaryExpression.cpp index 4570f0e2b361183929bf586de0a420c167a24e01..677b1886a3f0a09b29fcd05ffbe09fc7efe3079f 100644 --- a/ets2panda/ir/expressions/unaryExpression.cpp +++ b/ets2panda/ir/expressions/unaryExpression.cpp @@ -24,6 +24,7 @@ #include "ir/expressions/identifier.h" #include "ir/expressions/literals/bigIntLiteral.h" #include "ir/expressions/literals/numberLiteral.h" +#include "ir/expressions/callExpression.h" #include "ir/expressions/memberExpression.h" namespace panda::es2panda::ir { @@ -216,8 +217,10 @@ checker::Type *UnaryExpression::Check(checker::ETSChecker *checker) } auto arg_type = argument_->Check(checker); - checker::Type *operand_type = checker->ApplyUnaryOperatorPromotion(arg_type); - auto unboxed_operand_type = checker->ETSBuiltinTypeAsPrimitiveType(arg_type); + const auto is_cond_expr = operator_ == lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK; + checker::Type *operand_type = checker->ApplyUnaryOperatorPromotion(arg_type, true, true, is_cond_expr); + auto unboxed_operand_type = is_cond_expr ? checker->ETSBuiltinTypeAsConditionalType(arg_type) + : checker->ETSBuiltinTypeAsPrimitiveType(arg_type); switch (operator_) { case lexer::TokenType::PUNCTUATOR_MINUS: @@ -251,13 +254,23 @@ checker::Type *UnaryExpression::Check(checker::ETSChecker *checker) break; } case lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK: { - if (operand_type == nullptr || !operand_type->HasTypeFlag(checker::TypeFlag::ETS_BOOLEAN)) { + if (checker->IsNullOrVoidExpression(argument_)) { + auto ts_type = checker->CreateETSBooleanType(true); + ts_type->AddTypeFlag(checker::TypeFlag::CONSTANT); + SetTsType(ts_type); + break; + } + + if (operand_type == nullptr || !operand_type->IsConditionalExprType()) { checker->ThrowTypeError("Bad operand type, the type of the operand must be boolean type.", argument_->Start()); } - if (operand_type->HasTypeFlag(checker::TypeFlag::CONSTANT)) { - SetTsType(checker->CreateETSBooleanType(!operand_type->AsETSBooleanType()->GetValue())); + auto expr_res = operand_type->ResolveConditionExpr(); + if (std::get<0>(expr_res)) { + auto ts_type = checker->CreateETSBooleanType(!std::get<1>(expr_res)); + ts_type->AddTypeFlag(checker::TypeFlag::CONSTANT); + SetTsType(ts_type); break; } @@ -274,7 +287,8 @@ checker::Type *UnaryExpression::Check(checker::ETSChecker *checker) } } - if (arg_type->IsETSObjectType() && (unboxed_operand_type != nullptr)) { + if (arg_type->IsETSObjectType() && (unboxed_operand_type != nullptr) && + unboxed_operand_type->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { argument_->AddBoxingUnboxingFlag(checker->GetUnboxingFlag(unboxed_operand_type)); } diff --git a/ets2panda/ir/statements/assertStatement.cpp b/ets2panda/ir/statements/assertStatement.cpp index 01181345ae480efc149c7bca0917e2d8f810d940..d327b03b00910900b8cf73d18cc43f06741ec7b7 100644 --- a/ets2panda/ir/statements/assertStatement.cpp +++ b/ets2panda/ir/statements/assertStatement.cpp @@ -71,7 +71,7 @@ void AssertStatement::ThrowError(compiler::ETSGen *const etsg) const void AssertStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const { - auto res = compiler::Condition::CheckConstantExpr(test_); + auto res = compiler::Condition::CheckConstantExpr(etsg, test_); if (res == compiler::Condition::Result::CONST_TRUE) { return; diff --git a/ets2panda/ir/statements/ifStatement.cpp b/ets2panda/ir/statements/ifStatement.cpp index b931fe18b8e94cd6a346990c5a22145b11176b11..91736420b75736618e78decbc7f615ace7edb5e9 100644 --- a/ets2panda/ir/statements/ifStatement.cpp +++ b/ets2panda/ir/statements/ifStatement.cpp @@ -72,7 +72,7 @@ void IfStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const void IfStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const { - auto res = compiler::Condition::CheckConstantExpr(test_); + auto res = compiler::Condition::CheckConstantExpr(etsg, test_); if (res == compiler::Condition::Result::CONST_TRUE) { consequent_->Compile(etsg); diff --git a/ets2panda/ir/statements/tryStatement.h b/ets2panda/ir/statements/tryStatement.h index 74b955c87aca3f798585faf0c83457db28d74159..af69c1eb9d91f8f33a9ee79f4244c548b90e706b 100644 --- a/ets2panda/ir/statements/tryStatement.h +++ b/ets2panda/ir/statements/tryStatement.h @@ -16,7 +16,7 @@ #ifndef ES2PANDA_IR_STATEMENT_TRY_STATEMENT_H #define ES2PANDA_IR_STATEMENT_TRY_STATEMENT_H -#include "compiler/core/ETSGen.h" +#include "compiler/core/labelPair.h" #include "ir/statement.h" namespace panda::es2panda::compiler { diff --git a/ets2panda/ir/ts/tsAsExpression.cpp b/ets2panda/ir/ts/tsAsExpression.cpp index 98823f75ad4181c5b073dfa02b24b613b1779685..85994477a8a4a68cef0366f2ae1452749c715880 100644 --- a/ets2panda/ir/ts/tsAsExpression.cpp +++ b/ets2panda/ir/ts/tsAsExpression.cpp @@ -18,6 +18,7 @@ #include "binder/scope.h" #include "checker/TSchecker.h" #include "checker/ets/castingContext.h" +#include "checker/types/ets/etsUnionType.h" #include "compiler/core/ETSGen.h" #include "ir/expressions/identifier.h" #include "ir/expressions/literal.h" @@ -58,7 +59,7 @@ void TSAsExpression::Dump(ir::AstDumper *dumper) const void TSAsExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} -void TSAsExpression::Compile(compiler::ETSGen *const etsg) const +void TSAsExpression::Compile(compiler::ETSGen *etsg) const { if (!etsg->TryLoadConstantExpression(expression_)) { expression_->Compile(etsg); @@ -66,7 +67,12 @@ void TSAsExpression::Compile(compiler::ETSGen *const etsg) const etsg->ApplyConversion(expression_, nullptr); - const auto target_type_kind = checker::ETSChecker::TypeKind(TsType()); + auto *target_type = TsType(); + if (target_type->IsETSUnionType()) { + target_type = target_type->AsETSUnionType()->FindTypeIsCastableToThis(expression_, etsg->Checker()->Relation(), + expression_->TsType()); + } + const auto target_type_kind = checker::ETSChecker::TypeKind(target_type); switch (target_type_kind) { case checker::TypeFlag::ETS_BOOLEAN: { etsg->CastToBoolean(this); @@ -103,7 +109,7 @@ void TSAsExpression::Compile(compiler::ETSGen *const etsg) const case checker::TypeFlag::ETS_ARRAY: case checker::TypeFlag::ETS_OBJECT: case checker::TypeFlag::ETS_DYNAMIC_TYPE: { - etsg->CastToArrayOrObject(this, TsType(), is_unchecked_cast_); + etsg->CastToArrayOrObject(this, target_type, is_unchecked_cast_); break; } case checker::TypeFlag::ETS_STRING_ENUM: diff --git a/ets2panda/ir/ts/tsUnionType.cpp b/ets2panda/ir/ts/tsUnionType.cpp index c833b7f5b2858830ac29e5328676775c9235fa42..6a55683ea6a051026af1846a669efbd3171eb8f2 100644 --- a/ets2panda/ir/ts/tsUnionType.cpp +++ b/ets2panda/ir/ts/tsUnionType.cpp @@ -50,6 +50,11 @@ checker::Type *TSUnionType::Check([[maybe_unused]] checker::TSChecker *checker) return nullptr; } +checker::Type *TSUnionType::Check([[maybe_unused]] checker::ETSChecker *checker) +{ + return nullptr; +} + checker::Type *TSUnionType::GetType(checker::TSChecker *checker) { if (TsType() != nullptr) { @@ -65,9 +70,4 @@ checker::Type *TSUnionType::GetType(checker::TSChecker *checker) SetTsType(checker->CreateUnionType(std::move(types))); return TsType(); } - -checker::Type *TSUnionType::Check([[maybe_unused]] checker::ETSChecker *checker) -{ - return nullptr; -} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ts/tsUnionType.h b/ets2panda/ir/ts/tsUnionType.h index 65343e59e6483344f321f2c9c7d59ffed59f63c7..feaa03b28268044c85a438751015bda6aded7c34 100644 --- a/ets2panda/ir/ts/tsUnionType.h +++ b/ets2panda/ir/ts/tsUnionType.h @@ -36,8 +36,8 @@ public: void Dump(ir::AstDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; - checker::Type *GetType([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Type *GetType([[maybe_unused]] checker::TSChecker *checker) override; private: ArenaVector types_; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index bccc01c60a7dcc0bb5a8881908654e9ad0fc4ca9..be2fa90d61980685bcd837d4a1c2b1914a2bf2b9 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -91,6 +91,7 @@ #include "ir/ets/etsScript.h" #include "ir/ets/etsTypeReference.h" #include "ir/ets/etsTypeReferencePart.h" +#include "ir/ets/etsUnionType.h" #include "ir/ets/etsImportSource.h" #include "ir/ets/etsImportDeclaration.h" #include "ir/ets/etsStructDeclaration.h" @@ -2238,10 +2239,15 @@ void ETSParser::AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition * proxy_method_def->Function()->AddFlag(ir::ScriptFunctionFlags::OVERLOAD); } -std::string ETSParser::GetNameForTypeNode(const ir::TypeNode *const type_annotation) +std::string ETSParser::GetNameForTypeNode(const ir::TypeNode *type_annotation) { const std::string optional_nullable = type_annotation->IsNullable() ? "|null" : ""; + // FIXME(aakmaev): Support nullable types as unions + if (type_annotation->IsNullable() && type_annotation->IsETSUnionType()) { + type_annotation = type_annotation->AsETSUnionType()->Types().front(); + } + if (type_annotation->IsETSPrimitiveType()) { switch (type_annotation->AsETSPrimitiveType()->GetPrimitiveType()) { case ir::PrimitiveType::BYTE: @@ -2464,6 +2470,42 @@ ir::TypeNode *ETSParser::ParsePrimitiveType(TypeAnnotationParsingOptions *option return type_annotation; } +ir::ETSUnionType *ETSParser::ParseUnionType(ir::Expression *type) +{ + TypeAnnotationParsingOptions options = + TypeAnnotationParsingOptions::THROW_ERROR | TypeAnnotationParsingOptions::DISALLOW_UNION; + lexer::SourcePosition start_loc = type->Start(); + ArenaVector types(Allocator()->Adapter()); + ASSERT(type->IsTypeNode()); + types.push_back(type->AsTypeNode()); + + bool is_nullable {false}; + while (true) { + if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_BITWISE_OR) { + break; + } + + Lexer()->NextToken(); // eat '|' + + if (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_NULL) { + Lexer()->NextToken(); // eat 'null' + type->AddModifier(ir::ModifierFlags::NULLABLE); + is_nullable = true; + continue; + } + + types.push_back(ParseTypeAnnotation(&options)); + } + + lexer::SourcePosition end_loc = types.back()->End(); + auto *union_type = AllocNode(std::move(types)); + union_type->SetRange({start_loc, end_loc}); + if (is_nullable) { + union_type->AddModifier(ir::ModifierFlags::NULLABLE); + } + return union_type; +} + ir::TSIntersectionType *ETSParser::ParseIntersectionType(ir::Expression *type) { auto start_loc = type->Start(); @@ -2654,19 +2696,9 @@ ir::TypeNode *ETSParser::ParseTypeAnnotation(TypeAnnotationParsingOptions *optio type_annotation->SetRange({start_pos, Lexer()->GetToken().End()}); } - while (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_BITWISE_OR) { - Lexer()->NextToken(); // eat '|' - - if (Lexer()->GetToken().Type() != lexer::TokenType::LITERAL_NULL) { - if (throw_error) { - ThrowExpectedToken(lexer::TokenType::LITERAL_NULL); - } - - return nullptr; - } - Lexer()->NextToken(); // eat 'null' - - type_annotation->AddModifier(ir::ModifierFlags::NULLABLE); + if (((*options) & TypeAnnotationParsingOptions::DISALLOW_UNION) == 0 && + Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_BITWISE_OR) { + return ParseUnionType(type_annotation); } return type_annotation; @@ -3546,7 +3578,8 @@ ir::Expression *ETSParser::ParsePrimaryExpression(ExpressionParseFlags flags) auto start_loc = Lexer()->GetToken().Start(); auto saved_pos = Lexer()->Save(); TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::POTENTIAL_CLASS_LITERAL | - TypeAnnotationParsingOptions::IGNORE_FUNCTION_TYPE; + TypeAnnotationParsingOptions::IGNORE_FUNCTION_TYPE | + TypeAnnotationParsingOptions::DISALLOW_UNION; ir::TypeNode *potential_type = ParseTypeAnnotation(&options); if (potential_type != nullptr) { diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 87d1fbc9800bc10fbf2d939ca74c8ece6e7d46cd..684022b482ffa548231dae66f479cf563f641bc2 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -110,6 +110,7 @@ private: ir::TypeNode *ParseTypeReference(TypeAnnotationParsingOptions *options); ir::TypeNode *ParseBaseTypeReference(TypeAnnotationParsingOptions *options); ir::TypeNode *ParsePrimitiveType(TypeAnnotationParsingOptions *options, ir::PrimitiveType type); + ir::ETSUnionType *ParseUnionType(ir::Expression *type); ir::TSIntersectionType *ParseIntersectionType(ir::Expression *type); ir::TypeNode *ParseWildcardType(TypeAnnotationParsingOptions *options); ir::TypeNode *ParseFunctionType(); diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index 66d5d46710a956e48b68dc4457956c0c06558c82..984e284081b11bc27b5ec2640d59501da4867671 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -163,6 +163,7 @@ enum class TypeAnnotationParsingOptions : uint32_t { ALLOW_WILDCARD = 1U << 12U, IGNORE_FUNCTION_TYPE = 1U << 13U, ALLOW_DECLARATION_SITE_VARIANCE = 1U << 14U, + DISALLOW_UNION = 1U << 15U, }; DEFINE_BITOPS(TypeAnnotationParsingOptions) diff --git a/ets2panda/test/CMakeLists.txt b/ets2panda/test/CMakeLists.txt index f40aed24be528f99fc880954a9bb04380f30fce6..ff14df7c4ae66ff22c3618052179032ef05d69e4 100644 --- a/ets2panda/test/CMakeLists.txt +++ b/ets2panda/test/CMakeLists.txt @@ -92,5 +92,17 @@ if(PANDA_WITH_ETS) add_dependencies(ets_tests es2panda_tests) endif() + panda_add_gtest( + NAME es2panda_astverifier_tests + SOURCES + public/ast_verifier_test.cpp + LIBRARIES + es2panda-lib + INCLUDE_DIRS + ${ES2PANDA_PATH} + SANITIZERS + ${PANDA_SANITIZERS_LIST} + ) + add_subdirectory(tsconfig) endif() diff --git a/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt b/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt index e902bdbdba6b18c5653e8f9385f15b4f1f1e1952..ab661e0a090c8b29e226e770f365520e37552e2e 100644 --- a/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt +++ b/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt @@ -1256,35 +1256,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Listt", - "decorators": [], - "loc": { - "start": { - "line": 29, - "column": 31 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Listt", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 31 + }, + "end": { + "line": 29, + "column": 36 + } + } }, - "end": { - "line": 29, - "column": 36 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 37 + }, + "end": { + "line": 29, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 37 + }, + "end": { + "line": 29, + "column": 39 + } + } + }, "loc": { "start": { "line": 29, @@ -1292,55 +1317,45 @@ }, "end": { "line": 29, - "column": 38 + "column": 39 } } - }, - "loc": { - "start": { - "line": 29, - "column": 37 - }, - "end": { - "line": 29, - "column": 39 - } } - }, + ], "loc": { "start": { "line": 29, - "column": 37 + "column": 36 }, "end": { "line": 29, "column": 39 } } + }, + "loc": { + "start": { + "line": 29, + "column": 31 + }, + "end": { + "line": 29, + "column": 41 + } } - ], + }, "loc": { "start": { "line": 29, - "column": 36 + "column": 31 }, "end": { "line": 29, - "column": 39 + "column": 41 } } - }, - "loc": { - "start": { - "line": 29, - "column": 31 - }, - "end": { - "line": 29, - "column": 41 - } } - }, + ], "loc": { "start": { "line": 29, @@ -2032,13 +2047,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 43 + }, + "end": { + "line": 31, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 43 + }, + "end": { + "line": 31, + "column": 46 + } + } + }, "loc": { "start": { "line": 31, @@ -2046,21 +2086,11 @@ }, "end": { "line": 31, - "column": 44 + "column": 46 } } - }, - "loc": { - "start": { - "line": 31, - "column": 43 - }, - "end": { - "line": 31, - "column": 46 - } } - }, + ], "loc": { "start": { "line": 31, @@ -2737,35 +2767,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Listt", - "decorators": [], - "loc": { - "start": { - "line": 33, - "column": 45 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Listt", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 45 + }, + "end": { + "line": 33, + "column": 50 + } + } }, - "end": { - "line": 33, - "column": 50 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 51 + }, + "end": { + "line": 33, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 51 + }, + "end": { + "line": 33, + "column": 53 + } + } + }, "loc": { "start": { "line": 33, @@ -2773,55 +2828,45 @@ }, "end": { "line": 33, - "column": 52 + "column": 53 } } - }, - "loc": { - "start": { - "line": 33, - "column": 51 - }, - "end": { - "line": 33, - "column": 53 - } } - }, + ], "loc": { "start": { "line": 33, - "column": 51 + "column": 50 }, "end": { "line": 33, "column": 53 } } + }, + "loc": { + "start": { + "line": 33, + "column": 45 + }, + "end": { + "line": 33, + "column": 55 + } } - ], + }, "loc": { "start": { "line": 33, - "column": 50 + "column": 45 }, "end": { "line": 33, - "column": 53 + "column": 55 } } - }, - "loc": { - "start": { - "line": 33, - "column": 45 - }, - "end": { - "line": 33, - "column": 55 - } } - }, + ], "loc": { "start": { "line": 33, @@ -3110,35 +3155,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Listt", - "decorators": [], - "loc": { - "start": { - "line": 34, - "column": 53 - }, - "end": { - "line": 34, - "column": 58 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Listt", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 53 + }, + "end": { + "line": 34, + "column": 58 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 59 + }, + "end": { + "line": 34, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 59 + }, + "end": { + "line": 34, + "column": 61 + } + } + }, "loc": { "start": { "line": 34, @@ -3146,55 +3216,45 @@ }, "end": { "line": 34, - "column": 60 + "column": 61 } } - }, - "loc": { - "start": { - "line": 34, - "column": 59 - }, - "end": { - "line": 34, - "column": 61 - } } - }, + ], "loc": { "start": { "line": 34, - "column": 59 + "column": 58 }, "end": { "line": 34, "column": 61 } } + }, + "loc": { + "start": { + "line": 34, + "column": 53 + }, + "end": { + "line": 34, + "column": 63 + } } - ], + }, "loc": { "start": { "line": 34, - "column": 58 + "column": 53 }, "end": { "line": 34, - "column": 61 + "column": 63 } } - }, - "loc": { - "start": { - "line": 34, - "column": 53 - }, - "end": { - "line": 34, - "column": 63 - } } - }, + ], "loc": { "start": { "line": 34, @@ -3591,13 +3651,38 @@ "type": "Identifier", "name": "val", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 38 + }, + "end": { + "line": 39, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 38 + }, + "end": { + "line": 39, + "column": 41 + } + } + }, "loc": { "start": { "line": 39, @@ -3605,21 +3690,11 @@ }, "end": { "line": 39, - "column": 39 + "column": 41 } } - }, - "loc": { - "start": { - "line": 39, - "column": 38 - }, - "end": { - "line": 39, - "column": 41 - } } - }, + ], "loc": { "start": { "line": 39, @@ -11380,35 +11455,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Listt", - "decorators": [], - "loc": { - "start": { - "line": 141, - "column": 47 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Listt", + "decorators": [], + "loc": { + "start": { + "line": 141, + "column": 47 + }, + "end": { + "line": 141, + "column": 52 + } + } }, - "end": { - "line": 141, - "column": 52 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 141, + "column": 53 + }, + "end": { + "line": 141, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 141, + "column": 53 + }, + "end": { + "line": 141, + "column": 55 + } + } + }, "loc": { "start": { "line": 141, @@ -11416,55 +11516,45 @@ }, "end": { "line": 141, - "column": 54 + "column": 55 } } - }, - "loc": { - "start": { - "line": 141, - "column": 53 - }, - "end": { - "line": 141, - "column": 55 - } } - }, + ], "loc": { "start": { "line": 141, - "column": 53 + "column": 52 }, "end": { "line": 141, "column": 55 } } + }, + "loc": { + "start": { + "line": 141, + "column": 47 + }, + "end": { + "line": 141, + "column": 57 + } } - ], + }, "loc": { "start": { "line": 141, - "column": 52 + "column": 47 }, "end": { "line": 141, - "column": 55 + "column": 57 } } - }, - "loc": { - "start": { - "line": 141, - "column": 47 - }, - "end": { - "line": 141, - "column": 57 - } } - }, + ], "loc": { "start": { "line": 141, @@ -13137,28 +13227,53 @@ "line": 156, "column": 57 } - } - }, - "loc": { - "start": { - "line": 156, - "column": 26 + } + }, + "loc": { + "start": { + "line": 156, + "column": 26 + }, + "end": { + "line": 156, + "column": 57 + } + } + } + ], + "returnType": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 156, + "column": 59 + }, + "end": { + "line": 156, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 156, + "column": 59 + }, + "end": { + "line": 156, + "column": 62 + } + } }, - "end": { - "line": 156, - "column": 57 - } - } - } - ], - "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], "loc": { "start": { "line": 156, @@ -13166,21 +13281,11 @@ }, "end": { "line": 156, - "column": 60 + "column": 62 } } - }, - "loc": { - "start": { - "line": 156, - "column": 59 - }, - "end": { - "line": 156, - "column": 62 - } } - }, + ], "loc": { "start": { "line": 156, @@ -15082,35 +15187,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "ArrayAsListt", - "decorators": [], - "loc": { - "start": { - "line": 178, - "column": 60 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "ArrayAsListt", + "decorators": [], + "loc": { + "start": { + "line": 178, + "column": 60 + }, + "end": { + "line": 178, + "column": 72 + } + } }, - "end": { - "line": 178, - "column": 72 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 178, + "column": 73 + }, + "end": { + "line": 178, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 178, + "column": 73 + }, + "end": { + "line": 178, + "column": 75 + } + } + }, "loc": { "start": { "line": 178, @@ -15118,55 +15248,45 @@ }, "end": { "line": 178, - "column": 74 + "column": 75 } } - }, - "loc": { - "start": { - "line": 178, - "column": 73 - }, - "end": { - "line": 178, - "column": 75 - } } - }, + ], "loc": { "start": { "line": 178, - "column": 73 + "column": 72 }, "end": { "line": 178, "column": 75 } } + }, + "loc": { + "start": { + "line": 178, + "column": 60 + }, + "end": { + "line": 178, + "column": 77 + } } - ], + }, "loc": { "start": { "line": 178, - "column": 72 + "column": 60 }, "end": { "line": 178, - "column": 75 + "column": 77 } } - }, - "loc": { - "start": { - "line": 178, - "column": 60 - }, - "end": { - "line": 178, - "column": 77 - } } - }, + ], "loc": { "start": { "line": 178, @@ -16868,35 +16988,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "ArrayAsListt", - "decorators": [], - "loc": { - "start": { - "line": 198, - "column": 68 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "ArrayAsListt", + "decorators": [], + "loc": { + "start": { + "line": 198, + "column": 68 + }, + "end": { + "line": 198, + "column": 80 + } + } }, - "end": { - "line": 198, - "column": 80 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 198, + "column": 81 + }, + "end": { + "line": 198, + "column": 82 + } + } + }, + "loc": { + "start": { + "line": 198, + "column": 81 + }, + "end": { + "line": 198, + "column": 83 + } + } + }, "loc": { "start": { "line": 198, @@ -16904,55 +17049,45 @@ }, "end": { "line": 198, - "column": 82 + "column": 83 } } - }, - "loc": { - "start": { - "line": 198, - "column": 81 - }, - "end": { - "line": 198, - "column": 83 - } } - }, + ], "loc": { "start": { "line": 198, - "column": 81 + "column": 80 }, "end": { "line": 198, "column": 83 } } + }, + "loc": { + "start": { + "line": 198, + "column": 68 + }, + "end": { + "line": 198, + "column": 85 + } } - ], + }, "loc": { "start": { "line": 198, - "column": 80 + "column": 68 }, "end": { "line": 198, - "column": 83 + "column": 85 } } - }, - "loc": { - "start": { - "line": 198, - "column": 68 - }, - "end": { - "line": 198, - "column": 85 - } } - }, + ], "loc": { "start": { "line": 198, diff --git a/ets2panda/test/compiler/ets/generics_instantiation_2-expected.txt b/ets2panda/test/compiler/ets/generics_instantiation_2-expected.txt index 9378ef0aa26c2df1f48d9148668533fa64f723af..02c29ecade92aef99ff48385e2c94c85e9d5c0d8 100644 --- a/ets2panda/test/compiler/ets/generics_instantiation_2-expected.txt +++ b/ets2panda/test/compiler/ets/generics_instantiation_2-expected.txt @@ -111,35 +111,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Foo", - "decorators": [], - "loc": { - "start": { - "line": 17, - "column": 16 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 19 + } + } }, - "end": { - "line": 17, - "column": 19 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "U", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, "loc": { "start": { "line": 17, @@ -147,55 +172,45 @@ }, "end": { "line": 17, - "column": 21 + "column": 22 } } - }, - "loc": { - "start": { - "line": 17, - "column": 20 - }, - "end": { - "line": 17, - "column": 22 - } } - }, + ], "loc": { "start": { "line": 17, - "column": 20 + "column": 19 }, "end": { "line": 17, "column": 22 } } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 24 + } } - ], + }, "loc": { "start": { "line": 17, - "column": 19 + "column": 16 }, "end": { "line": 17, - "column": 22 + "column": 24 } } - }, - "loc": { - "start": { - "line": 17, - "column": 16 - }, - "end": { - "line": 17, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 17, @@ -630,35 +645,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], - "loc": { - "start": { - "line": 25, - "column": 12 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 13 + } + } }, - "end": { - "line": 25, - "column": 13 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, "loc": { "start": { "line": 25, @@ -666,55 +706,45 @@ }, "end": { "line": 25, - "column": 15 + "column": 16 } } - }, - "loc": { - "start": { - "line": 25, - "column": 14 - }, - "end": { - "line": 25, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 25, - "column": 14 + "column": 13 }, "end": { "line": 25, "column": 16 } } + }, + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 18 + } } - ], + }, "loc": { "start": { "line": 25, - "column": 13 + "column": 12 }, "end": { "line": 25, - "column": 16 + "column": 18 } } - }, - "loc": { - "start": { - "line": 25, - "column": 12 - }, - "end": { - "line": 25, - "column": 18 - } } - }, + ], "loc": { "start": { "line": 25, @@ -849,35 +879,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Foo", - "decorators": [], - "loc": { - "start": { - "line": 29, - "column": 12 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Foo", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 15 + } + } }, - "end": { - "line": 29, - "column": 15 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 18 + } + } + }, "loc": { "start": { "line": 29, @@ -885,55 +940,45 @@ }, "end": { "line": 29, - "column": 17 + "column": 18 } } - }, - "loc": { - "start": { - "line": 29, - "column": 16 - }, - "end": { - "line": 29, - "column": 18 - } } - }, + ], "loc": { "start": { "line": 29, - "column": 16 + "column": 15 }, "end": { "line": 29, "column": 18 } } + }, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 20 + } } - ], + }, "loc": { "start": { "line": 29, - "column": 15 + "column": 12 }, "end": { "line": 29, - "column": 18 + "column": 20 } } - }, - "loc": { - "start": { - "line": 29, - "column": 12 - }, - "end": { - "line": 29, - "column": 20 - } } - }, + ], "loc": { "start": { "line": 29, @@ -1477,35 +1522,60 @@ "type": "Identifier", "name": "p1", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Foo", - "decorators": [], - "loc": { - "start": { - "line": 35, - "column": 13 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Foo", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 16 + } + } }, - "end": { - "line": 35, - "column": 16 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 24 + } + } + }, "loc": { "start": { "line": 35, @@ -1513,55 +1583,45 @@ }, "end": { "line": 35, - "column": 23 + "column": 24 } } - }, - "loc": { - "start": { - "line": 35, - "column": 17 - }, - "end": { - "line": 35, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 35, - "column": 17 + "column": 16 }, "end": { "line": 35, "column": 24 } } + }, + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 26 + } } - ], + }, "loc": { "start": { "line": 35, - "column": 16 + "column": 13 }, "end": { "line": 35, - "column": 24 + "column": 26 } } - }, - "loc": { - "start": { - "line": 35, - "column": 13 - }, - "end": { - "line": 35, - "column": 26 - } } - }, + ], "loc": { "start": { "line": 35, diff --git a/ets2panda/test/compiler/ets/generics_instantiation_4-expected.txt b/ets2panda/test/compiler/ets/generics_instantiation_4-expected.txt index 4d568d9bcf9474a674492185989acb10183bb7d9..5d98ea0792711bf974f51c431933684fce731e53 100644 --- a/ets2panda/test/compiler/ets/generics_instantiation_4-expected.txt +++ b/ets2panda/test/compiler/ets/generics_instantiation_4-expected.txt @@ -1199,35 +1199,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "C", - "decorators": [], - "loc": { - "start": { - "line": 31, - "column": 12 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 13 + } + } }, - "end": { - "line": 31, - "column": 13 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 31, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 31, + "column": 16 + } + } + }, "loc": { "start": { "line": 31, @@ -1235,55 +1260,45 @@ }, "end": { "line": 31, - "column": 15 + "column": 16 } } - }, - "loc": { - "start": { - "line": 31, - "column": 14 - }, - "end": { - "line": 31, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 31, - "column": 14 + "column": 13 }, "end": { "line": 31, "column": 16 } } + }, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 18 + } } - ], + }, "loc": { "start": { "line": 31, - "column": 13 + "column": 12 }, "end": { "line": 31, - "column": 16 + "column": 18 } } - }, - "loc": { - "start": { - "line": 31, - "column": 12 - }, - "end": { - "line": 31, - "column": 18 - } } - }, + ], "loc": { "start": { "line": 31, @@ -1668,35 +1683,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "C", - "decorators": [], - "loc": { - "start": { - "line": 37, - "column": 21 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 21 + }, + "end": { + "line": 37, + "column": 22 + } + } }, - "end": { - "line": 37, - "column": 22 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "U", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 23 + }, + "end": { + "line": 37, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 23 + }, + "end": { + "line": 37, + "column": 25 + } + } + }, "loc": { "start": { "line": 37, @@ -1704,55 +1744,45 @@ }, "end": { "line": 37, - "column": 24 + "column": 25 } } - }, - "loc": { - "start": { - "line": 37, - "column": 23 - }, - "end": { - "line": 37, - "column": 25 - } } - }, + ], "loc": { "start": { "line": 37, - "column": 23 + "column": 22 }, "end": { "line": 37, "column": 25 } } + }, + "loc": { + "start": { + "line": 37, + "column": 21 + }, + "end": { + "line": 37, + "column": 27 + } } - ], + }, "loc": { "start": { "line": 37, - "column": 22 + "column": 21 }, "end": { "line": 37, - "column": 25 + "column": 27 } } - }, - "loc": { - "start": { - "line": 37, - "column": 21 - }, - "end": { - "line": 37, - "column": 27 - } } - }, + ], "loc": { "start": { "line": 37, @@ -2757,35 +2787,60 @@ "type": "Identifier", "name": "p1", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "C", - "decorators": [], - "loc": { - "start": { - "line": 48, - "column": 13 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 48, + "column": 13 + }, + "end": { + "line": 48, + "column": 14 + } + } }, - "end": { - "line": 48, - "column": 14 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 48, + "column": 15 + }, + "end": { + "line": 48, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 48, + "column": 15 + }, + "end": { + "line": 48, + "column": 22 + } + } + }, "loc": { "start": { "line": 48, @@ -2793,55 +2848,45 @@ }, "end": { "line": 48, - "column": 21 + "column": 22 } } - }, - "loc": { - "start": { - "line": 48, - "column": 15 - }, - "end": { - "line": 48, - "column": 22 - } } - }, + ], "loc": { "start": { "line": 48, - "column": 15 + "column": 14 }, "end": { "line": 48, "column": 22 } } + }, + "loc": { + "start": { + "line": 48, + "column": 13 + }, + "end": { + "line": 48, + "column": 24 + } } - ], + }, "loc": { "start": { "line": 48, - "column": 14 + "column": 13 }, "end": { "line": 48, - "column": 22 + "column": 24 } } - }, - "loc": { - "start": { - "line": 48, - "column": 13 - }, - "end": { - "line": 48, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 48, @@ -4428,35 +4473,60 @@ "type": "Identifier", "name": "p3", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "C", - "decorators": [], - "loc": { - "start": { - "line": 54, - "column": 13 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 54, + "column": 13 + }, + "end": { + "line": 54, + "column": 14 + } + } }, - "end": { - "line": 54, - "column": 14 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Double", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 54, + "column": 15 + }, + "end": { + "line": 54, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 54, + "column": 15 + }, + "end": { + "line": 54, + "column": 22 + } + } + }, "loc": { "start": { "line": 54, @@ -4464,55 +4534,45 @@ }, "end": { "line": 54, - "column": 21 + "column": 22 } } - }, - "loc": { - "start": { - "line": 54, - "column": 15 - }, - "end": { - "line": 54, - "column": 22 - } } - }, + ], "loc": { "start": { "line": 54, - "column": 15 + "column": 14 }, "end": { "line": 54, "column": 22 } } + }, + "loc": { + "start": { + "line": 54, + "column": 13 + }, + "end": { + "line": 54, + "column": 24 + } } - ], + }, "loc": { "start": { "line": 54, - "column": 14 + "column": 13 }, "end": { "line": 54, - "column": 22 + "column": 24 } } - }, - "loc": { - "start": { - "line": 54, - "column": 13 - }, - "end": { - "line": 54, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 54, diff --git a/ets2panda/test/compiler/ets/n_nullableTypeInArgNotRef-expected.txt b/ets2panda/test/compiler/ets/n_nullableTypeInArgNotRef-expected.txt index 52a16293b34ac77d273306d5549a2a87ff2e1dca..9d48d62f2754add599c00fd925e6c1be3d6e6e38 100644 --- a/ets2panda/test/compiler/ets/n_nullableTypeInArgNotRef-expected.txt +++ b/ets2panda/test/compiler/ets/n_nullableTypeInArgNotRef-expected.txt @@ -449,7 +449,22 @@ "type": "Identifier", "name": "a", "typeAnnotation": { - "type": "ETSPrimitiveType", + "type": "ETSUnionType", + "types": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 21 + } + } + } + ], "loc": { "start": { "line": 21, @@ -486,13 +501,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 32 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 32 + }, + "end": { + "line": 21, + "column": 37 + } + } + }, "loc": { "start": { "line": 21, @@ -500,21 +540,11 @@ }, "end": { "line": 21, - "column": 35 + "column": 37 } } - }, - "loc": { - "start": { - "line": 21, - "column": 32 - }, - "end": { - "line": 21, - "column": 37 - } } - }, + ], "loc": { "start": { "line": 21, diff --git a/ets2panda/test/compiler/ets/n_nullableTypeInReturnNotRef-expected.txt b/ets2panda/test/compiler/ets/n_nullableTypeInReturnNotRef-expected.txt index 88d32c64268c451f08632a85436f45fdd9091fe0..f66aa37242c7def82ef5601b9a368d73c0b803de 100644 --- a/ets2panda/test/compiler/ets/n_nullableTypeInReturnNotRef-expected.txt +++ b/ets2panda/test/compiler/ets/n_nullableTypeInReturnNotRef-expected.txt @@ -429,7 +429,22 @@ "expression": false, "params": [], "returnType": { - "type": "ETSPrimitiveType", + "type": "ETSUnionType", + "types": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 22 + } + } + } + ], "loc": { "start": { "line": 21, diff --git a/ets2panda/test/compiler/ets/n_nullableTypeNotRef-expected.txt b/ets2panda/test/compiler/ets/n_nullableTypeNotRef-expected.txt index de072b9e7b5f706195fffecffff6be8198b3e8e3..5dedc88c9cdf8aea84a7e30178f842e69db200a8 100644 --- a/ets2panda/test/compiler/ets/n_nullableTypeNotRef-expected.txt +++ b/ets2panda/test/compiler/ets/n_nullableTypeNotRef-expected.txt @@ -214,7 +214,22 @@ "type": "Identifier", "name": "a", "typeAnnotation": { - "type": "ETSPrimitiveType", + "type": "ETSUnionType", + "types": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 14 + } + } + } + ], "loc": { "start": { "line": 17, diff --git a/ets2panda/test/compiler/ets/optionalLambdaParameter-expected.txt b/ets2panda/test/compiler/ets/optionalLambdaParameter-expected.txt index d551053d61ba41f43eacdf77efef4b2fd91a6959..f070ebd4cff8a3c3ae2b2baa0166836d0b088902 100644 --- a/ets2panda/test/compiler/ets/optionalLambdaParameter-expected.txt +++ b/ets2panda/test/compiler/ets/optionalLambdaParameter-expected.txt @@ -432,16 +432,41 @@ "type": "Identifier", "name": "exec", "typeAnnotation": { - "type": "ETSFunctionType", - "params": [], - "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "void", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, "loc": { "start": { "line": 1, @@ -463,18 +488,8 @@ "column": 3 } } - }, - "loc": { - "start": { - "line": 1, - "column": 3 - }, - "end": { - "line": 1, - "column": 3 - } } - }, + ], "loc": { "start": { "line": 1, diff --git a/ets2panda/test/compiler/ets/parenthesizedType-expected.txt b/ets2panda/test/compiler/ets/parenthesizedType-expected.txt index 66ec32d13735169de62634804767afe41114a573..4b076a8743f04a4d2d1fb1e9bd6137eebbd1535c 100644 --- a/ets2panda/test/compiler/ets/parenthesizedType-expected.txt +++ b/ets2panda/test/compiler/ets/parenthesizedType-expected.txt @@ -358,13 +358,38 @@ "type": "Identifier", "name": "c", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 20 + } + } + }, "loc": { "start": { "line": 19, @@ -372,21 +397,11 @@ }, "end": { "line": 19, - "column": 18 + "column": 20 } } - }, - "loc": { - "start": { - "line": 19, - "column": 12 - }, - "end": { - "line": 19, - "column": 20 - } } - }, + ], "loc": { "start": { "line": 19, diff --git a/ets2panda/test/compiler/ets/union_types_1-expected.txt b/ets2panda/test/compiler/ets/union_types_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..2f919ebed65ae7616dcc0200e106b89a53bdda59 --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_1-expected.txt @@ -0,0 +1,3567 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 41, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 5 + }, + "end": { + "line": 6, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 6, + "column": 10 + }, + "end": { + "line": 6, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 7, + "column": 2 + }, + "end": { + "line": 7, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 7, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 5, + "column": 1 + }, + "end": { + "line": 7, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 7 + }, + "end": { + "line": 9, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 5 + }, + "end": { + "line": 10, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 10, + "column": 16 + }, + "end": { + "line": 10, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 10, + "column": 10 + }, + "end": { + "line": 10, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "num", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 11, + "column": 22 + }, + "end": { + "line": 11, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 25 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 14 + }, + "end": { + "line": 12, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 17 + } + } + }, + "right": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 20 + }, + "end": { + "line": 12, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 11, + "column": 27 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 16 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 16 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 5 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "num_to_return", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 5 + }, + "end": { + "line": 14, + "column": 18 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "num_to_return", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 5 + }, + "end": { + "line": 14, + "column": 18 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 14, + "column": 23 + }, + "end": { + "line": 14, + "column": 26 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 15, + "column": 16 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 21 + }, + "end": { + "line": 15, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 15, + "column": 16 + }, + "end": { + "line": 15, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 9 + }, + "end": { + "line": 15, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 14, + "column": 27 + }, + "end": { + "line": 16, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 18 + }, + "end": { + "line": 16, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 18 + }, + "end": { + "line": 16, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 5 + }, + "end": { + "line": 16, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 17, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 1 + }, + "end": { + "line": 17, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 28 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "right": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "xx", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "init": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 19 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 21, + "column": 19 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "xx", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "property": { + "type": "Identifier", + "name": "num_to_return", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 32 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 32 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 777, + "loc": { + "start": { + "line": 22, + "column": 38 + }, + "end": { + "line": 22, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 41 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of class `C` must be 777", + "loc": { + "start": { + "line": 22, + "column": 43 + }, + "end": { + "line": 22, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 91 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 10 + } + } + }, + "right": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 23 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 21 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! x is instaceof C but not A", + "loc": { + "start": { + "line": 25, + "column": 23 + }, + "end": { + "line": 25, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 59 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 14 + }, + "end": { + "line": 27, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 777, + "loc": { + "start": { + "line": 27, + "column": 21 + }, + "end": { + "line": 27, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 24 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of union must be 777", + "loc": { + "start": { + "line": 27, + "column": 26 + }, + "end": { + "line": 27, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 70 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 15 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 10 + } + } + }, + "init": { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 19 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 20 + }, + "end": { + "line": 28, + "column": 23 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 23 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 28, + "column": 26 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 27 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 28 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 13 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 780, + "loc": { + "start": { + "line": 29, + "column": 17 + }, + "end": { + "line": 29, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 20 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Variable 'a' must be 780", + "loc": { + "start": { + "line": 29, + "column": 22 + }, + "end": { + "line": 29, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 56 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "+=", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 6 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 7 + }, + "end": { + "line": 30, + "column": 10 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 10 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 223, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 30, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 18 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 31, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1000, + "loc": { + "start": { + "line": 31, + "column": 21 + }, + "end": { + "line": 31, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 25 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of union must be 1000", + "loc": { + "start": { + "line": 31, + "column": 27 + }, + "end": { + "line": 31, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 72 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 29 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 10 + }, + "end": { + "line": 34, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 10 + }, + "end": { + "line": 34, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 16 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 20 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 21 + }, + "end": { + "line": 35, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 21 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 21 + }, + "end": { + "line": 35, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 35, + "column": 5 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 15 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 777, + "loc": { + "start": { + "line": 36, + "column": 15 + }, + "end": { + "line": 36, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 36, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 20 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 9 + }, + "end": { + "line": 37, + "column": 10 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 12 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 12 + }, + "end": { + "line": 38, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 14 + }, + "end": { + "line": 38, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 38, + "column": 12 + }, + "end": { + "line": 38, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1000, + "loc": { + "start": { + "line": 38, + "column": 21 + }, + "end": { + "line": 38, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 12 + }, + "end": { + "line": 38, + "column": 25 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of union must be 1000", + "loc": { + "start": { + "line": 38, + "column": 27 + }, + "end": { + "line": 38, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 72 + } + } + }, + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 9 + }, + "end": { + "line": 39, + "column": 10 + } + } + }, + "right": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 22 + }, + "end": { + "line": 39, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 9 + }, + "end": { + "line": 39, + "column": 23 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "xx", + "decorators": [], + "loc": { + "start": { + "line": 40, + "column": 13 + }, + "end": { + "line": 40, + "column": 15 + } + } + }, + "init": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 40, + "column": 18 + }, + "end": { + "line": 40, + "column": 19 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 40, + "column": 23 + }, + "end": { + "line": 40, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 40, + "column": 23 + }, + "end": { + "line": 40, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 40, + "column": 23 + }, + "end": { + "line": 40, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 40, + "column": 18 + }, + "end": { + "line": 40, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 40, + "column": 13 + }, + "end": { + "line": 40, + "column": 19 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 40, + "column": 9 + }, + "end": { + "line": 40, + "column": 25 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "xx", + "decorators": [], + "loc": { + "start": { + "line": 41, + "column": 16 + }, + "end": { + "line": 41, + "column": 18 + } + } + }, + "property": { + "type": "Identifier", + "name": "num_to_return", + "decorators": [], + "loc": { + "start": { + "line": 41, + "column": 19 + }, + "end": { + "line": 41, + "column": 32 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 41, + "column": 16 + }, + "end": { + "line": 41, + "column": 32 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 41, + "column": 16 + }, + "end": { + "line": 41, + "column": 34 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1000, + "loc": { + "start": { + "line": 41, + "column": 38 + }, + "end": { + "line": 41, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 16 + }, + "end": { + "line": 41, + "column": 42 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of class `C` must be 1000", + "loc": { + "start": { + "line": 41, + "column": 44 + }, + "end": { + "line": 41, + "column": 92 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 9 + }, + "end": { + "line": 41, + "column": 93 + } + } + } + ], + "loc": { + "start": { + "line": 39, + "column": 25 + }, + "end": { + "line": 42, + "column": 6 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 39, + "column": 5 + }, + "end": { + "line": 42, + "column": 6 + } + } + }, + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 43, + "column": 9 + }, + "end": { + "line": 43, + "column": 10 + } + } + }, + "right": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 43, + "column": 22 + }, + "end": { + "line": 43, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 43, + "column": 9 + }, + "end": { + "line": 43, + "column": 23 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 44, + "column": 16 + }, + "end": { + "line": 44, + "column": 21 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! x is instaceof C but not B", + "loc": { + "start": { + "line": 44, + "column": 23 + }, + "end": { + "line": 44, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 44, + "column": 9 + }, + "end": { + "line": 44, + "column": 59 + } + } + } + ], + "loc": { + "start": { + "line": 43, + "column": 25 + }, + "end": { + "line": 45, + "column": 6 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 43, + "column": 5 + }, + "end": { + "line": 45, + "column": 6 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 46, + "column": 5 + }, + "end": { + "line": 46, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 46, + "column": 13 + }, + "end": { + "line": 46, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 46, + "column": 13 + }, + "end": { + "line": 46, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 46, + "column": 13 + }, + "end": { + "line": 46, + "column": 15 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 46, + "column": 9 + }, + "end": { + "line": 46, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 46, + "column": 5 + }, + "end": { + "line": 46, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 46, + "column": 5 + }, + "end": { + "line": 46, + "column": 17 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 47, + "column": 12 + }, + "end": { + "line": 47, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 47, + "column": 14 + }, + "end": { + "line": 47, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 47, + "column": 12 + }, + "end": { + "line": 47, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 41, + "loc": { + "start": { + "line": 47, + "column": 21 + }, + "end": { + "line": 47, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 47, + "column": 12 + }, + "end": { + "line": 47, + "column": 23 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of union must be 41", + "loc": { + "start": { + "line": 47, + "column": 25 + }, + "end": { + "line": 47, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 47, + "column": 5 + }, + "end": { + "line": 47, + "column": 68 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 48, + "column": 5 + }, + "end": { + "line": 48, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 48, + "column": 13 + }, + "end": { + "line": 48, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 48, + "column": 13 + }, + "end": { + "line": 48, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 48, + "column": 13 + }, + "end": { + "line": 48, + "column": 15 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 48, + "column": 9 + }, + "end": { + "line": 48, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 48, + "column": 5 + }, + "end": { + "line": 48, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 48, + "column": 5 + }, + "end": { + "line": 48, + "column": 17 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 49, + "column": 12 + }, + "end": { + "line": 49, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 49, + "column": 14 + }, + "end": { + "line": 49, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 49, + "column": 12 + }, + "end": { + "line": 49, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 49, + "column": 21 + }, + "end": { + "line": 49, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 49, + "column": 12 + }, + "end": { + "line": 49, + "column": 23 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! The num field of union must be 42", + "loc": { + "start": { + "line": 49, + "column": 25 + }, + "end": { + "line": 49, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 49, + "column": 5 + }, + "end": { + "line": 49, + "column": 68 + } + } + } + ], + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 50, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 14 + }, + "end": { + "line": 50, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 14 + }, + "end": { + "line": 50, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 1 + }, + "end": { + "line": 50, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 51, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/union_types_1.ets b/ets2panda/test/compiler/ets/union_types_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..fa815c0cf13ab7e8d9e9511c84f31378b8e823fd --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_1.ets @@ -0,0 +1,50 @@ +class A { + num: int = 41; +} + +class B { + num: int = 42; +} + +class C { + num: int = 43; + constructor(num: int) { + this.num = num; + } + num_to_return() : int { + return this.num; + } +} + +function foo(x : A | B | C) { + if (x instanceof C) { + let xx = x as C; + assert xx.num_to_return() == 777: "Error! The num field of class `C` must be 777"; + } + if (x instanceof A) { + assert false: "Error! x is instaceof C but not A"; + } + assert x.num == 777: "Error! The num field of union must be 777"; + let a: int = x.num + 3; + assert a == 780: "Error! Variable 'a' must be 780"; + x.num += 223; + assert x.num == 1000: "Error! The num field of union must be 1000"; +} + +function main() { + let x : A | B | C; + x = new C(777); + foo(x); + assert x.num == 1000: "Error! The num field of union must be 1000"; + if (x instanceof C) { + let xx = x as C; + assert xx.num_to_return() == 1000: "Error! The num field of class `C` must be 1000"; + } + if (x instanceof B) { + assert false: "Error! x is instaceof C but not B"; + } + x = new A(); + assert x.num == 41: "Error! The num field of union must be 41"; + x = new B(); + assert x.num == 42: "Error! The num field of union must be 42"; +} diff --git a/ets2panda/test/compiler/ets/union_types_2-expected.txt b/ets2panda/test/compiler/ets/union_types_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..77d7fb6ed98b7f1879b2ddc6d51852fdcd9f1a13 --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_2-expected.txt @@ -0,0 +1,1753 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 4, + "column": 16 + }, + "end": { + "line": 4, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 5, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 5, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 7, + "column": 7 + }, + "end": { + "line": 7, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 8, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 8, + "column": 16 + }, + "end": { + "line": 8, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 8, + "column": 10 + }, + "end": { + "line": 8, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "num", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 9, + "column": 22 + }, + "end": { + "line": 9, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 25 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 17 + } + } + }, + "right": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 20 + }, + "end": { + "line": 10, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 9, + "column": 27 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 16 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 16 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 5 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "num_to_return", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 5 + }, + "end": { + "line": 12, + "column": 18 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "num_to_return", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 5 + }, + "end": { + "line": 12, + "column": 18 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 12, + "column": 23 + }, + "end": { + "line": 12, + "column": 26 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 13, + "column": 16 + }, + "end": { + "line": 13, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 13, + "column": 21 + }, + "end": { + "line": 13, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 13, + "column": 16 + }, + "end": { + "line": 13, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 9 + }, + "end": { + "line": 13, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 12, + "column": 27 + }, + "end": { + "line": 14, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 18 + }, + "end": { + "line": 14, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 18 + }, + "end": { + "line": 14, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 5 + }, + "end": { + "line": 14, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 15, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 1 + }, + "end": { + "line": 15, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 28 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 29 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 777, + "loc": { + "start": { + "line": 23, + "column": 15 + }, + "end": { + "line": 23, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 10 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 26, + "column": 1 + } + } +} +TypeError: Property 'num' does not exist on type 'A' [union_types_2.ets:18:7] diff --git a/ets2panda/test/compiler/ets/union_types_2.ets b/ets2panda/test/compiler/ets/union_types_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..c2c95304f3edb9507c5ee3427ad99e7a77b8850d --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_2.ets @@ -0,0 +1,25 @@ +class A {} + +class B { + num: int = 42; +} + +class C { + num: int = 43; + constructor(num: int) { + this.num = num; + } + num_to_return() : int { + return this.num; + } +} + +function foo(x : A | B | C) { + x.num; // CTE - `A` doesn't have field `num` +} + +function main() { + let x : A | B | C; + x = new C(777); + foo(x); +} diff --git a/ets2panda/test/compiler/ets/union_types_3-expected.txt b/ets2panda/test/compiler/ets/union_types_3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fcc01a67c87a5f42b2092f31d171a21ade2cd93f --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_3-expected.txt @@ -0,0 +1,1968 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x0", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 28 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 41 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "*", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 23 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 27 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 4, + "loc": { + "start": { + "line": 3, + "column": 31 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be 4", + "loc": { + "start": { + "line": 3, + "column": 34 + }, + "end": { + "line": 3, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 53 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 7 + } + } + }, + "right": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "*", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 5, + "column": 13 + }, + "end": { + "line": 5, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 5, + "column": 19 + }, + "end": { + "line": 5, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 23 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 5, + "column": 26 + }, + "end": { + "line": 5, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 27 + } + } + }, + "right": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 6, + "loc": { + "start": { + "line": 5, + "column": 32 + }, + "end": { + "line": 5, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 5, + "column": 31 + }, + "end": { + "line": 5, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 33 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be -6", + "loc": { + "start": { + "line": 5, + "column": 35 + }, + "end": { + "line": 5, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 5, + "column": 5 + }, + "end": { + "line": 5, + "column": 55 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 5 + }, + "end": { + "line": 6, + "column": 7 + } + } + }, + "right": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 6, + "column": 10 + }, + "end": { + "line": 6, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 6, + "column": 5 + }, + "end": { + "line": 6, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 6, + "column": 5 + }, + "end": { + "line": 6, + "column": 14 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 7, + "column": 19 + }, + "end": { + "line": 7, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 12 + }, + "end": { + "line": 7, + "column": 27 + } + } + }, + "right": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 7, + "column": 31 + }, + "end": { + "line": 7, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 12 + }, + "end": { + "line": 7, + "column": 35 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be true", + "loc": { + "start": { + "line": 7, + "column": 37 + }, + "end": { + "line": 7, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 5 + }, + "end": { + "line": 7, + "column": 59 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 8, + "column": 7 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "STR1", + "loc": { + "start": { + "line": 8, + "column": 10 + }, + "end": { + "line": 8, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 8, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 8, + "column": 16 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x0", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 14 + }, + "end": { + "line": 9, + "column": 16 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 20 + }, + "end": { + "line": 9, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 20 + }, + "end": { + "line": 9, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 20 + }, + "end": { + "line": 9, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 27 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "STR2", + "loc": { + "start": { + "line": 9, + "column": 30 + }, + "end": { + "line": 9, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 12 + }, + "end": { + "line": 9, + "column": 37 + } + } + }, + "property": { + "type": "Identifier", + "name": "equals", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 38 + }, + "end": { + "line": 9, + "column": 44 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 9, + "column": 12 + }, + "end": { + "line": 9, + "column": 44 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "STR1STR2", + "loc": { + "start": { + "line": 9, + "column": 45 + }, + "end": { + "line": 9, + "column": 55 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 9, + "column": 12 + }, + "end": { + "line": 9, + "column": 56 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be `STR1STR2`", + "loc": { + "start": { + "line": 9, + "column": 58 + }, + "end": { + "line": 9, + "column": 85 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 5 + }, + "end": { + "line": 9, + "column": 86 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x1", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 22 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 10, + "column": 23 + }, + "end": { + "line": 10, + "column": 30 + } + } + } + ], + "loc": { + "start": { + "line": 10, + "column": 14 + }, + "end": { + "line": 10, + "column": 30 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 11 + } + } + }, + "init": { + "type": "StringLiteral", + "value": "AAA", + "loc": { + "start": { + "line": 10, + "column": 33 + }, + "end": { + "line": 10, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 38 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 10, + "column": 5 + }, + "end": { + "line": 10, + "column": 39 + } + } + }, + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x1", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 9 + }, + "end": { + "line": 11, + "column": 11 + } + } + }, + "right": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 23 + }, + "end": { + "line": 11, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 9 + }, + "end": { + "line": 11, + "column": 29 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x1", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 17 + }, + "end": { + "line": 12, + "column": 19 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 23 + }, + "end": { + "line": 12, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 23 + }, + "end": { + "line": 12, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 23 + }, + "end": { + "line": 12, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 16 + }, + "end": { + "line": 12, + "column": 30 + } + } + }, + "property": { + "type": "Identifier", + "name": "equals", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 31 + }, + "end": { + "line": 12, + "column": 37 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 12, + "column": 16 + }, + "end": { + "line": 12, + "column": 37 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "AAA", + "loc": { + "start": { + "line": 12, + "column": 38 + }, + "end": { + "line": 12, + "column": 43 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 12, + "column": 16 + }, + "end": { + "line": 12, + "column": 44 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be `AAA`", + "loc": { + "start": { + "line": 12, + "column": 46 + }, + "end": { + "line": 12, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 69 + } + } + } + ], + "loc": { + "start": { + "line": 11, + "column": 31 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 11, + "column": 5 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x2", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 14 + }, + "end": { + "line": 14, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 14 + }, + "end": { + "line": 14, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 14 + }, + "end": { + "line": 14, + "column": 22 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 14, + "column": 23 + }, + "end": { + "line": 14, + "column": 30 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 14, + "column": 33 + }, + "end": { + "line": 14, + "column": 36 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 14, + "column": 39 + }, + "end": { + "line": 14, + "column": 45 + } + } + } + ], + "loc": { + "start": { + "line": 14, + "column": 14 + }, + "end": { + "line": 14, + "column": 45 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 9 + }, + "end": { + "line": 14, + "column": 11 + } + } + }, + "init": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 14, + "column": 48 + }, + "end": { + "line": 14, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 9 + }, + "end": { + "line": 14, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 14, + "column": 5 + }, + "end": { + "line": 14, + "column": 53 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x2", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 13 + }, + "end": { + "line": 15, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 15, + "column": 19 + }, + "end": { + "line": 15, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 12 + }, + "end": { + "line": 15, + "column": 27 + } + } + }, + "right": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 15, + "column": 31 + }, + "end": { + "line": 15, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 12 + }, + "end": { + "line": 15, + "column": 35 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be `true`", + "loc": { + "start": { + "line": 15, + "column": 37 + }, + "end": { + "line": 15, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 5 + }, + "end": { + "line": 15, + "column": 61 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x3", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 30 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 45 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 45 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 3.14, + "loc": { + "start": { + "line": 16, + "column": 48 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 53 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 3.14, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be `3.14`", + "loc": { + "start": { + "line": 17, + "column": 36 + }, + "end": { + "line": 17, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 60 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 19, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/union_types_3.ets b/ets2panda/test/compiler/ets/union_types_3.ets new file mode 100644 index 0000000000000000000000000000000000000000..2673d3cc324487c5c0742619bbb7219d3d40ef72 --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_3.ets @@ -0,0 +1,18 @@ +function main() { + let x0 : int | String | boolean = 2; + assert (x0 as int) * 2 == 4: "Error! Must be 4"; + x0 = -2; + assert (x0 as int) * 3 == -6: "Error! Must be -6"; + x0 = true + assert (x0 as boolean) == true: "Error! Must be true"; + x0 = "STR1" + assert ((x0 as String) + "STR2").equals("STR1STR2"): "Error! Must be `STR1STR2`"; + let x1 : String | boolean = "AAA"; + if (x1 instanceof String) { + assert (x1 as String).equals("AAA"): "Error! Must be `AAA`"; + } + let x2 : String | boolean | int | double = true; + assert (x2 as boolean) == true: "Error! Must be `true`"; + let x3 : String | boolean | int | double = 3.14; + assert (x3 as double) == 3.14: "Error! Must be `3.14`"; +} diff --git a/ets2panda/test/compiler/ets/union_types_4-expected.txt b/ets2panda/test/compiler/ets/union_types_4-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..92c003e82496919eeea34d4ebdbcf777c06232fe --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_4-expected.txt @@ -0,0 +1,1532 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 11 + }, + "end": { + "line": 6, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 6, + "column": 1 + }, + "end": { + "line": 6, + "column": 11 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 8, + "column": 10 + }, + "end": { + "line": 8, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 8, + "column": 10 + }, + "end": { + "line": 8, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 21 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 9, + "column": 22 + }, + "end": { + "line": 9, + "column": 27 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 30 + }, + "end": { + "line": 9, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 30 + }, + "end": { + "line": 9, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 30 + }, + "end": { + "line": 9, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 9, + "column": 13 + }, + "end": { + "line": 9, + "column": 33 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 10 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 9, + "column": 34 + }, + "end": { + "line": 9, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 9, + "column": 5 + }, + "end": { + "line": 9, + "column": 36 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "*", + "left": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 13 + }, + "end": { + "line": 10, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 10, + "column": 18 + }, + "end": { + "line": 10, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 12 + }, + "end": { + "line": 10, + "column": 22 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 10, + "column": 25 + }, + "end": { + "line": 10, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 12 + }, + "end": { + "line": 10, + "column": 26 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 10, + "column": 30 + }, + "end": { + "line": 10, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 12 + }, + "end": { + "line": 10, + "column": 32 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be 10", + "loc": { + "start": { + "line": 10, + "column": 34 + }, + "end": { + "line": 10, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 5 + }, + "end": { + "line": 10, + "column": 54 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 5 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "STRSTR", + "loc": { + "start": { + "line": 11, + "column": 9 + }, + "end": { + "line": 11, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 5 + }, + "end": { + "line": 11, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 5 + }, + "end": { + "line": 11, + "column": 18 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 13 + }, + "end": { + "line": 12, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 18 + }, + "end": { + "line": 12, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 18 + }, + "end": { + "line": 12, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 18 + }, + "end": { + "line": 12, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 12 + }, + "end": { + "line": 12, + "column": 25 + } + } + }, + "property": { + "type": "Identifier", + "name": "equals", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 26 + }, + "end": { + "line": 12, + "column": 32 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 12, + "column": 12 + }, + "end": { + "line": 12, + "column": 32 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "STRSTR", + "loc": { + "start": { + "line": 12, + "column": 33 + }, + "end": { + "line": 12, + "column": 41 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 12, + "column": 12 + }, + "end": { + "line": 12, + "column": 42 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Must be `STRSTR`", + "loc": { + "start": { + "line": 12, + "column": 44 + }, + "end": { + "line": 12, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 5 + }, + "end": { + "line": 12, + "column": 70 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 13, + "column": 5 + }, + "end": { + "line": 13, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 13, + "column": 13 + }, + "end": { + "line": 13, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 13 + }, + "end": { + "line": 13, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 13 + }, + "end": { + "line": 13, + "column": 15 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 13, + "column": 9 + }, + "end": { + "line": 13, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 5 + }, + "end": { + "line": 13, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 5 + }, + "end": { + "line": 13, + "column": 17 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 13 + }, + "end": { + "line": 14, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 18 + }, + "end": { + "line": 14, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 18 + }, + "end": { + "line": 14, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 18 + }, + "end": { + "line": 14, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 12 + }, + "end": { + "line": 14, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 21 + }, + "end": { + "line": 14, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 14, + "column": 12 + }, + "end": { + "line": 14, + "column": 24 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 14, + "column": 28 + }, + "end": { + "line": 14, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 12 + }, + "end": { + "line": 14, + "column": 30 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! Field of A must be `42`", + "loc": { + "start": { + "line": 14, + "column": 32 + }, + "end": { + "line": 14, + "column": 64 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 5 + }, + "end": { + "line": 14, + "column": 65 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 5 + }, + "end": { + "line": 15, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 13 + }, + "end": { + "line": 15, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 13 + }, + "end": { + "line": 15, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 13 + }, + "end": { + "line": 15, + "column": 15 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 15, + "column": 9 + }, + "end": { + "line": 15, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 5 + }, + "end": { + "line": 15, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 5 + }, + "end": { + "line": 15, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 8, + "column": 17 + }, + "end": { + "line": 16, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 8, + "column": 14 + }, + "end": { + "line": 16, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 8, + "column": 14 + }, + "end": { + "line": 16, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 8, + "column": 1 + }, + "end": { + "line": 16, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 17, + "column": 1 + } + } +} +TypeError: Initializers type is not assignable to the target type [union_types_4.ets:15:9] diff --git a/ets2panda/test/compiler/ets/union_types_4.ets b/ets2panda/test/compiler/ets/union_types_4.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6916489c4521aba672ddc7e80715aed8de75b2f --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_4.ets @@ -0,0 +1,16 @@ +class A { + num: int = 42; + foo() {} +} + +class B {} + +function main() { + let x : String | short | A = 5; + assert (x as int) * 2 == 10: "Error! Must be 10"; + x = "STRSTR"; + assert (x as String).equals("STRSTR"): "Error! Must be `STRSTR`"; + x = new A(); + assert (x as A).num == 42: "Error! Field of A must be `42`"; + x = new B(); // CTE +} diff --git a/ets2panda/test/compiler/ets/union_types_5-expected.txt b/ets2panda/test/compiler/ets/union_types_5-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3ced3385cbe63fa209876a6a6fd9c8047ecec60c --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_5-expected.txt @@ -0,0 +1,3319 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + "constraint": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 5, + "column": 5 + }, + "end": { + "line": 5, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 2 + }, + "end": { + "line": 6, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 6, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 4, + "column": 1 + }, + "end": { + "line": 6, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 7, + "column": 7 + }, + "end": { + "line": 7, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 8, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 8, + "column": 16 + }, + "end": { + "line": 8, + "column": 18 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 8, + "column": 10 + }, + "end": { + "line": 8, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 2 + }, + "end": { + "line": 9, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 9, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 1 + }, + "end": { + "line": 9, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 10 + }, + "end": { + "line": 11, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 10 + }, + "end": { + "line": 11, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 19 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 19 + }, + "end": { + "line": 11, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 11, + "column": 17 + }, + "end": { + "line": 11, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 14 + }, + "end": { + "line": 11, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 14 + }, + "end": { + "line": 11, + "column": 21 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 10 + } + } + }, + "right": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 22 + }, + "end": { + "line": 12, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 9 + }, + "end": { + "line": 12, + "column": 23 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 13, + "column": 16 + }, + "end": { + "line": 13, + "column": 17 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 13, + "column": 18 + }, + "end": { + "line": 13, + "column": 21 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 13, + "column": 16 + }, + "end": { + "line": 13, + "column": 21 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 13, + "column": 25 + }, + "end": { + "line": 13, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 16 + }, + "end": { + "line": 13, + "column": 27 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! num field must be 42", + "loc": { + "start": { + "line": 13, + "column": 29 + }, + "end": { + "line": 13, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 13, + "column": 9 + }, + "end": { + "line": 13, + "column": 59 + } + } + } + ], + "loc": { + "start": { + "line": 12, + "column": 25 + }, + "end": { + "line": 14, + "column": 6 + } + } + }, + "alternate": { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 16 + }, + "end": { + "line": 14, + "column": 17 + } + } + }, + "right": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 14, + "column": 29 + }, + "end": { + "line": 14, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 16 + }, + "end": { + "line": 14, + "column": 30 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 16 + }, + "end": { + "line": 15, + "column": 17 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 21 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 15, + "column": 16 + }, + "end": { + "line": 15, + "column": 21 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 15, + "column": 25 + }, + "end": { + "line": 15, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 16 + }, + "end": { + "line": 15, + "column": 27 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! num field must be 43", + "loc": { + "start": { + "line": 15, + "column": 29 + }, + "end": { + "line": 15, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 9 + }, + "end": { + "line": 15, + "column": 59 + } + } + } + ], + "loc": { + "start": { + "line": 14, + "column": 32 + }, + "end": { + "line": 16, + "column": 6 + } + } + }, + "alternate": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! x must be instanceof B|C", + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 57 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 14, + "column": 12 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 11, + "column": 22 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 11, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 11, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 18 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 24 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 10 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "right": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 29 + }, + "end": { + "line": 22, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 30 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 17 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 24 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! x.union0.num field must be 42", + "loc": { + "start": { + "line": 23, + "column": 36 + }, + "end": { + "line": 23, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 75 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "alternate": { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "instanceof", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 17 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 18 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "right": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 36 + }, + "end": { + "line": 24, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 37 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 17 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 18 + }, + "end": { + "line": 25, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 24 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 25 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 25, + "column": 32 + }, + "end": { + "line": 25, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 34 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! x.union0.num field must be 43", + "loc": { + "start": { + "line": 25, + "column": 36 + }, + "end": { + "line": 25, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 75 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 39 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "alternate": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 27, + "column": 21 + } + } + }, + "second": { + "type": "StringLiteral", + "value": "Error! x.union0 must be instanceof B|C", + "loc": { + "start": { + "line": 27, + "column": 23 + }, + "end": { + "line": 27, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 9 + }, + "end": { + "line": 27, + "column": 64 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 28, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 12 + }, + "end": { + "line": 32, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 32, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 32, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 32, + "column": 16 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 16 + }, + "end": { + "line": 32, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 16 + }, + "end": { + "line": 32, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 16 + }, + "end": { + "line": 32, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 32, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 13 + }, + "end": { + "line": 32, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 12 + }, + "end": { + "line": 32, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 12 + }, + "end": { + "line": 32, + "column": 20 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 9 + }, + "end": { + "line": 32, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 32, + "column": 26 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 27 + }, + "end": { + "line": 32, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 27 + }, + "end": { + "line": 32, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 27 + }, + "end": { + "line": 32, + "column": 29 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 27 + }, + "end": { + "line": 32, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 26 + }, + "end": { + "line": 32, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 32, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 32, + "column": 32 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 32, + "column": 21 + }, + "end": { + "line": 32, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 9 + }, + "end": { + "line": 32, + "column": 34 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 34 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 6 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 7 + }, + "end": { + "line": 33, + "column": 13 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 13 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 20 + }, + "end": { + "line": 33, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 20 + }, + "end": { + "line": 33, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 20 + }, + "end": { + "line": 33, + "column": 22 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 33, + "column": 16 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 34, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 34, + "column": 10 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 11 + }, + "end": { + "line": 34, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 34, + "column": 17 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 34, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 34, + "column": 19 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 5 + }, + "end": { + "line": 35, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 10 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 35, + "column": 5 + }, + "end": { + "line": 35, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 5 + }, + "end": { + "line": 35, + "column": 12 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 6 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 7 + }, + "end": { + "line": 36, + "column": 13 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 13 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 20 + }, + "end": { + "line": 36, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 20 + }, + "end": { + "line": 36, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 20 + }, + "end": { + "line": 36, + "column": 22 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 36, + "column": 16 + }, + "end": { + "line": 36, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 24 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 9 + }, + "end": { + "line": 37, + "column": 10 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 12 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 9 + }, + "end": { + "line": 38, + "column": 10 + } + } + }, + "property": { + "type": "Identifier", + "name": "union0", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 11 + }, + "end": { + "line": 38, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 38, + "column": 9 + }, + "end": { + "line": 38, + "column": 17 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 23 + }, + "end": { + "line": 39, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 39, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 39, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 39, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 40, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/union_types_5.ets b/ets2panda/test/compiler/ets/union_types_5.ets new file mode 100644 index 0000000000000000000000000000000000000000..f392e9b94cf1a8996c1f5902a15dc44a7130d607 --- /dev/null +++ b/ets2panda/test/compiler/ets/union_types_5.ets @@ -0,0 +1,39 @@ +class A { + union0: T; +} +class B { + num: int = 42 +} +class C { + num: int = 43 +} + +function foo(x: B|C) { + if (x instanceof B) { + assert x.num == 42: "Error! num field must be 42"; + } else if (x instanceof C) { + assert x.num == 43: "Error! num field must be 43"; + } else { + assert false: "Error! x must be instanceof B|C"; + } +} + +function bar(x: A) { + if (x.union0 instanceof B) { + assert x.union0.num == 42: "Error! x.union0.num field must be 42"; + } else if (x.union0 instanceof C) { + assert x.union0.num == 43: "Error! x.union0.num field must be 43"; + } else { + assert false: "Error! x.union0 must be instanceof B|C"; + } +} + +function main(): void { + let a: A = new A(); + a.union0 = new B(); + foo(a.union0); + bar(a); + a.union0 = new C(); + bar(a); + foo(a.union0); +} diff --git a/ets2panda/test/compiler/ets/voidTypeInBinaryOperation-expected.txt b/ets2panda/test/compiler/ets/voidTypeInBinaryOperation-expected.txt index aa1df0dcac059f43f0821df76763041792174452..9447d6cad027d103c65b993701124e7455b4a3ba 100644 --- a/ets2panda/test/compiler/ets/voidTypeInBinaryOperation-expected.txt +++ b/ets2panda/test/compiler/ets/voidTypeInBinaryOperation-expected.txt @@ -494,4 +494,3 @@ } } } -TypeError: Bad operand type, the types of the operands must be boolean type. [voidTypeInBinaryOperation.ets:20:10] diff --git a/ets2panda/test/parser/ets/AccessBinaryTrees-expected.txt b/ets2panda/test/parser/ets/AccessBinaryTrees-expected.txt index df130879674f709131f58e8118451cf1885bee5f..9e4d3701c85ca3daee2097a52b569719d20d470d 100644 --- a/ets2panda/test/parser/ets/AccessBinaryTrees-expected.txt +++ b/ets2panda/test/parser/ets/AccessBinaryTrees-expected.txt @@ -46,13 +46,38 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "TreeNode", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TreeNode", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, "loc": { "start": { "line": 17, @@ -60,21 +85,11 @@ }, "end": { "line": 17, - "column": 25 + "column": 27 } } - }, - "loc": { - "start": { - "line": 17, - "column": 17 - }, - "end": { - "line": 17, - "column": 27 - } } - }, + ], "loc": { "start": { "line": 17, @@ -123,13 +138,38 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "TreeNode", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TreeNode", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, "loc": { "start": { "line": 18, @@ -137,21 +177,11 @@ }, "end": { "line": 18, - "column": 26 + "column": 28 } } - }, - "loc": { - "start": { - "line": 18, - "column": 18 - }, - "end": { - "line": 18, - "column": 28 - } } - }, + ], "loc": { "start": { "line": 18, @@ -276,13 +306,38 @@ "type": "Identifier", "name": "left", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "TreeNode", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TreeNode", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, "loc": { "start": { "line": 21, @@ -290,21 +345,11 @@ }, "end": { "line": 21, - "column": 29 + "column": 31 } } - }, - "loc": { - "start": { - "line": 21, - "column": 21 - }, - "end": { - "line": 21, - "column": 31 - } } - }, + ], "loc": { "start": { "line": 21, @@ -345,13 +390,38 @@ "type": "Identifier", "name": "right", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "TreeNode", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TreeNode", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 45 + }, + "end": { + "line": 21, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 45 + }, + "end": { + "line": 21, + "column": 55 + } + } + }, "loc": { "start": { "line": 21, @@ -359,21 +429,11 @@ }, "end": { "line": 21, - "column": 53 + "column": 55 } } - }, - "loc": { - "start": { - "line": 21, - "column": 45 - }, - "end": { - "line": 21, - "column": 55 - } } - }, + ], "loc": { "start": { "line": 21, diff --git a/ets2panda/test/parser/ets/arrayHoldingNullValue-expected.txt b/ets2panda/test/parser/ets/arrayHoldingNullValue-expected.txt index dabdff4623bcbe79ae17ebc00332ac88bf5b23b4..564772a80e03cf6561642324d5832d3b043c9566 100644 --- a/ets2panda/test/parser/ets/arrayHoldingNullValue-expected.txt +++ b/ets2panda/test/parser/ets/arrayHoldingNullValue-expected.txt @@ -309,20 +309,35 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 16, - "column": 8 + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } }, - "end": { - "line": 16, - "column": 11 + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } } } - }, + ], "loc": { "start": { "line": 16, @@ -371,20 +386,35 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 17, - "column": 8 + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 14 + } + } }, - "end": { - "line": 17, - "column": 14 + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 18 + } } } - }, + ], "loc": { "start": { "line": 17, @@ -433,15 +463,40 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Double", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, "loc": { "start": { "line": 18, @@ -449,32 +504,22 @@ }, "end": { "line": 18, - "column": 14 + "column": 15 } } }, "loc": { "start": { "line": 18, - "column": 8 + "column": 17 }, "end": { "line": 18, - "column": 15 + "column": 18 } } - }, - "loc": { - "start": { - "line": 18, - "column": 8 - }, - "end": { - "line": 18, - "column": 15 - } } - }, + ], "loc": { "start": { "line": 18, @@ -597,20 +642,35 @@ "type": "Identifier", "name": "c", "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 21, - "column": 14 + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 19 + } + } }, - "end": { - "line": 21, - "column": 19 + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 23 + } } } - }, + ], "loc": { "start": { "line": 21, @@ -681,20 +741,35 @@ "type": "Identifier", "name": "d", "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 22, - "column": 12 + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 16 + } + } }, - "end": { - "line": 22, - "column": 16 + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 20 + } } } - }, + ], "loc": { "start": { "line": 22, diff --git a/ets2panda/test/parser/ets/async_function-expected.txt b/ets2panda/test/parser/ets/async_function-expected.txt index f9b41ad62976230f90159731e78beb366c4d2b08..e98df896b962f6cbae62b7fd7c410321223a39db 100644 --- a/ets2panda/test/parser/ets/async_function-expected.txt +++ b/ets2panda/test/parser/ets/async_function-expected.txt @@ -68,35 +68,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 17, - "column": 25 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 32 + } + } }, - "end": { - "line": 17, - "column": 32 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 40 + } + } + }, "loc": { "start": { "line": 17, @@ -104,55 +129,45 @@ }, "end": { "line": 17, - "column": 39 + "column": 40 } } - }, - "loc": { - "start": { - "line": 17, - "column": 33 - }, - "end": { - "line": 17, - "column": 40 - } } - }, + ], "loc": { "start": { "line": 17, - "column": 33 + "column": 32 }, "end": { "line": 17, "column": 40 } } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 42 + } } - ], + }, "loc": { "start": { "line": 17, - "column": 32 + "column": 25 }, "end": { "line": 17, - "column": 40 + "column": 42 } } - }, - "loc": { - "start": { - "line": 17, - "column": 25 - }, - "end": { - "line": 17, - "column": 42 - } } - }, + ], "loc": { "start": { "line": 17, @@ -518,35 +533,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 20, - "column": 23 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 20, + "column": 30 + } + } }, - "end": { - "line": 20, - "column": 30 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, "loc": { "start": { "line": 20, @@ -554,55 +594,45 @@ }, "end": { "line": 20, - "column": 37 + "column": 38 } } - }, - "loc": { - "start": { - "line": 20, - "column": 31 - }, - "end": { - "line": 20, - "column": 38 - } } - }, + ], "loc": { "start": { "line": 20, - "column": 31 + "column": 30 }, "end": { "line": 20, "column": 38 } } + }, + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 20, + "column": 40 + } } - ], + }, "loc": { "start": { "line": 20, - "column": 30 + "column": 23 }, "end": { "line": 20, - "column": 38 + "column": 40 } } - }, - "loc": { - "start": { - "line": 20, - "column": 23 - }, - "end": { - "line": 20, - "column": 40 - } } - }, + ], "loc": { "start": { "line": 20, @@ -718,35 +748,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 22, - "column": 54 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 54 + }, + "end": { + "line": 22, + "column": 61 + } + } }, - "end": { - "line": 22, - "column": 61 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 62 + }, + "end": { + "line": 22, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 62 + }, + "end": { + "line": 22, + "column": 69 + } + } + }, "loc": { "start": { "line": 22, @@ -754,55 +809,45 @@ }, "end": { "line": 22, - "column": 68 + "column": 69 } } - }, - "loc": { - "start": { - "line": 22, - "column": 62 - }, - "end": { - "line": 22, - "column": 69 - } } - }, + ], "loc": { "start": { "line": 22, - "column": 62 + "column": 61 }, "end": { "line": 22, "column": 69 } } + }, + "loc": { + "start": { + "line": 22, + "column": 54 + }, + "end": { + "line": 22, + "column": 71 + } } - ], + }, "loc": { "start": { "line": 22, - "column": 61 + "column": 54 }, "end": { "line": 22, - "column": 69 + "column": 71 } } - }, - "loc": { - "start": { - "line": 22, - "column": 54 - }, - "end": { - "line": 22, - "column": 71 - } } - }, + ], "loc": { "start": { "line": 22, @@ -888,35 +933,60 @@ "type": "ETSFunctionType", "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 22, - "column": 19 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 26 + } + } }, - "end": { - "line": 22, - "column": 26 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 27 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 27 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, "loc": { "start": { "line": 22, @@ -924,55 +994,45 @@ }, "end": { "line": 22, - "column": 33 + "column": 34 } } - }, - "loc": { - "start": { - "line": 22, - "column": 27 - }, - "end": { - "line": 22, - "column": 34 - } } - }, + ], "loc": { "start": { "line": 22, - "column": 27 + "column": 26 }, "end": { "line": 22, "column": 34 } } + }, + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 36 + } } - ], + }, "loc": { "start": { "line": 22, - "column": 26 + "column": 19 }, "end": { "line": 22, - "column": 34 + "column": 36 } } - }, - "loc": { - "start": { - "line": 22, - "column": 19 - }, - "end": { - "line": 22, - "column": 36 - } } - }, + ], "loc": { "start": { "line": 22, diff --git a/ets2panda/test/parser/ets/async_overload-expected.txt b/ets2panda/test/parser/ets/async_overload-expected.txt index 5e103098465eac12ce4bc95270494443db78b42d..e8b4419b7fad45a5eb861834745a232c3ea1f5f9 100644 --- a/ets2panda/test/parser/ets/async_overload-expected.txt +++ b/ets2panda/test/parser/ets/async_overload-expected.txt @@ -110,35 +110,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 17, - "column": 24 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 31 + } + } }, - "end": { - "line": 17, - "column": 31 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, "loc": { "start": { "line": 17, @@ -146,55 +171,45 @@ }, "end": { "line": 17, - "column": 38 + "column": 39 } } - }, - "loc": { - "start": { - "line": 17, - "column": 32 - }, - "end": { - "line": 17, - "column": 39 - } } - }, + ], "loc": { "start": { "line": 17, - "column": 32 + "column": 31 }, "end": { "line": 17, "column": 39 } } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 41 + } } - ], + }, "loc": { "start": { "line": 17, - "column": 31 + "column": 24 }, "end": { "line": 17, - "column": 39 + "column": 41 } } - }, - "loc": { - "start": { - "line": 17, - "column": 24 - }, - "end": { - "line": 17, - "column": 41 - } } - }, + ], "loc": { "start": { "line": 17, @@ -322,13 +337,38 @@ "type": "Identifier", "name": "o", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, "loc": { "start": { "line": 21, @@ -336,21 +376,11 @@ }, "end": { "line": 21, - "column": 24 + "column": 26 } } - }, - "loc": { - "start": { - "line": 21, - "column": 18 - }, - "end": { - "line": 21, - "column": 26 - } } - }, + ], "loc": { "start": { "line": 21, @@ -428,35 +458,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 21, - "column": 42 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 42 + }, + "end": { + "line": 21, + "column": 49 + } + } }, - "end": { - "line": 21, - "column": 49 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 50 + }, + "end": { + "line": 21, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 50 + }, + "end": { + "line": 21, + "column": 57 + } + } + }, "loc": { "start": { "line": 21, @@ -464,55 +519,45 @@ }, "end": { "line": 21, - "column": 56 + "column": 57 } } - }, - "loc": { - "start": { - "line": 21, - "column": 50 - }, - "end": { - "line": 21, - "column": 57 - } } - }, + ], "loc": { "start": { "line": 21, - "column": 50 + "column": 49 }, "end": { "line": 21, "column": 57 } } + }, + "loc": { + "start": { + "line": 21, + "column": 42 + }, + "end": { + "line": 21, + "column": 59 + } } - ], + }, "loc": { "start": { "line": 21, - "column": 49 + "column": 42 }, "end": { "line": 21, - "column": 57 + "column": 59 } } - }, - "loc": { - "start": { - "line": 21, - "column": 42 - }, - "end": { - "line": 21, - "column": 59 - } } - }, + ], "loc": { "start": { "line": 21, @@ -905,35 +950,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 25, - "column": 29 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 29 + }, + "end": { + "line": 25, + "column": 36 + } + } }, - "end": { - "line": 25, - "column": 36 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 44 + } + } + }, "loc": { "start": { "line": 25, @@ -941,55 +1011,45 @@ }, "end": { "line": 25, - "column": 43 + "column": 44 } } - }, - "loc": { - "start": { - "line": 25, - "column": 37 - }, - "end": { - "line": 25, - "column": 44 - } } - }, + ], "loc": { "start": { "line": 25, - "column": 37 + "column": 36 }, "end": { "line": 25, "column": 44 } } + }, + "loc": { + "start": { + "line": 25, + "column": 29 + }, + "end": { + "line": 25, + "column": 46 + } } - ], + }, "loc": { "start": { "line": 25, - "column": 36 + "column": 29 }, "end": { "line": 25, - "column": 44 + "column": 46 } } - }, - "loc": { - "start": { - "line": 25, - "column": 29 - }, - "end": { - "line": 25, - "column": 46 - } } - }, + ], "loc": { "start": { "line": 25, @@ -1117,13 +1177,38 @@ "type": "Identifier", "name": "o", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 31 + } + } + }, "loc": { "start": { "line": 29, @@ -1131,21 +1216,11 @@ }, "end": { "line": 29, - "column": 29 + "column": 31 } } - }, - "loc": { - "start": { - "line": 29, - "column": 23 - }, - "end": { - "line": 29, - "column": 31 - } } - }, + ], "loc": { "start": { "line": 29, @@ -1223,35 +1298,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 29, - "column": 47 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 47 + }, + "end": { + "line": 29, + "column": 54 + } + } }, - "end": { - "line": 29, - "column": 54 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 61 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 62 + } + } + }, "loc": { "start": { "line": 29, @@ -1259,55 +1359,45 @@ }, "end": { "line": 29, - "column": 61 + "column": 62 } } - }, - "loc": { - "start": { - "line": 29, - "column": 55 - }, - "end": { - "line": 29, - "column": 62 - } } - }, + ], "loc": { "start": { "line": 29, - "column": 55 + "column": 54 }, "end": { "line": 29, "column": 62 } } + }, + "loc": { + "start": { + "line": 29, + "column": 47 + }, + "end": { + "line": 29, + "column": 64 + } } - ], + }, "loc": { "start": { "line": 29, - "column": 54 + "column": 47 }, "end": { "line": 29, - "column": 62 + "column": 64 } } - }, - "loc": { - "start": { - "line": 29, - "column": 47 - }, - "end": { - "line": 29, - "column": 64 - } } - }, + ], "loc": { "start": { "line": 29, diff --git a/ets2panda/test/parser/ets/async_with_lambda-expected.txt b/ets2panda/test/parser/ets/async_with_lambda-expected.txt index 2007b8a7be58dbdd60a7102bfe8470e58ad5a622..74c87c249980f72fb13929b00a8298a004e67398 100644 --- a/ets2panda/test/parser/ets/async_with_lambda-expected.txt +++ b/ets2panda/test/parser/ets/async_with_lambda-expected.txt @@ -253,35 +253,60 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 18, - "column": 34 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 18, + "column": 41 + } + } }, - "end": { - "line": 18, - "column": 41 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "String", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 18, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 18, + "column": 49 + } + } + }, "loc": { "start": { "line": 18, @@ -289,55 +314,45 @@ }, "end": { "line": 18, - "column": 48 + "column": 49 } } - }, - "loc": { - "start": { - "line": 18, - "column": 42 - }, - "end": { - "line": 18, - "column": 49 - } } - }, + ], "loc": { "start": { "line": 18, - "column": 42 + "column": 41 }, "end": { "line": 18, "column": 49 } } + }, + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 18, + "column": 51 + } } - ], + }, "loc": { "start": { "line": 18, - "column": 41 + "column": 34 }, "end": { "line": 18, - "column": 49 + "column": 51 } } - }, - "loc": { - "start": { - "line": 18, - "column": 34 - }, - "end": { - "line": 18, - "column": 51 - } } - }, + ], "loc": { "start": { "line": 18, @@ -750,35 +765,60 @@ "type": "ETSFunctionType", "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 26, - "column": 29 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 26, + "column": 36 + } + } }, - "end": { - "line": 26, - "column": 36 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 37 + }, + "end": { + "line": 26, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 37 + }, + "end": { + "line": 26, + "column": 44 + } + } + }, "loc": { "start": { "line": 26, @@ -786,55 +826,45 @@ }, "end": { "line": 26, - "column": 43 + "column": 44 } } - }, - "loc": { - "start": { - "line": 26, - "column": 37 - }, - "end": { - "line": 26, - "column": 44 - } } - }, + ], "loc": { "start": { "line": 26, - "column": 37 + "column": 36 }, "end": { "line": 26, "column": 44 } } + }, + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 26, + "column": 46 + } } - ], + }, "loc": { "start": { "line": 26, - "column": 36 + "column": 29 }, "end": { "line": 26, - "column": 44 + "column": 46 } } - }, - "loc": { - "start": { - "line": 26, - "column": 29 - }, - "end": { - "line": 26, - "column": 46 - } } - }, + ], "loc": { "start": { "line": 26, @@ -879,35 +909,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 26, - "column": 64 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 64 + }, + "end": { + "line": 26, + "column": 71 + } + } }, - "end": { - "line": 26, - "column": 71 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 72 + }, + "end": { + "line": 26, + "column": 78 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 72 + }, + "end": { + "line": 26, + "column": 79 + } + } + }, "loc": { "start": { "line": 26, @@ -915,55 +970,45 @@ }, "end": { "line": 26, - "column": 78 + "column": 79 } } - }, - "loc": { - "start": { - "line": 26, - "column": 72 - }, - "end": { - "line": 26, - "column": 79 - } } - }, + ], "loc": { "start": { "line": 26, - "column": 72 + "column": 71 }, "end": { "line": 26, "column": 79 } } + }, + "loc": { + "start": { + "line": 26, + "column": 64 + }, + "end": { + "line": 26, + "column": 81 + } } - ], + }, "loc": { "start": { "line": 26, - "column": 71 + "column": 64 }, "end": { "line": 26, - "column": 79 + "column": 81 } } - }, - "loc": { - "start": { - "line": 26, - "column": 64 - }, - "end": { - "line": 26, - "column": 81 - } } - }, + ], "loc": { "start": { "line": 26, diff --git a/ets2panda/test/parser/ets/await_keyword-expected.txt b/ets2panda/test/parser/ets/await_keyword-expected.txt index 9adc24bf5003ce6ca65d8a6dcb9b75b06f74f263..4db57e694b0b058022fa3286e98d3b410478697d 100644 --- a/ets2panda/test/parser/ets/await_keyword-expected.txt +++ b/ets2panda/test/parser/ets/await_keyword-expected.txt @@ -289,35 +289,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 28 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 35 + } + } }, - "end": { - "line": 16, - "column": 35 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 36 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 36 + }, + "end": { + "line": 16, + "column": 43 + } + } + }, "loc": { "start": { "line": 16, @@ -325,55 +350,45 @@ }, "end": { "line": 16, - "column": 42 + "column": 43 } } - }, - "loc": { - "start": { - "line": 16, - "column": 36 - }, - "end": { - "line": 16, - "column": 43 - } } - }, + ], "loc": { "start": { "line": 16, - "column": 36 + "column": 35 }, "end": { "line": 16, "column": 43 } } + }, + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 45 + } } - ], + }, "loc": { "start": { "line": 16, - "column": 35 + "column": 28 }, "end": { "line": 16, - "column": 43 + "column": 45 } } - }, - "loc": { - "start": { - "line": 16, - "column": 28 - }, - "end": { - "line": 16, - "column": 45 - } } - }, + ], "loc": { "start": { "line": 16, @@ -397,35 +412,60 @@ "type": "Identifier", "name": "promise", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 17, - "column": 18 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 25 + } + } }, - "end": { - "line": 17, - "column": 25 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 33 + } + } + }, "loc": { "start": { "line": 17, @@ -433,55 +473,45 @@ }, "end": { "line": 17, - "column": 32 + "column": 33 } } - }, - "loc": { - "start": { - "line": 17, - "column": 26 - }, - "end": { - "line": 17, - "column": 33 - } } - }, + ], "loc": { "start": { "line": 17, - "column": 26 + "column": 25 }, "end": { "line": 17, "column": 33 } } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 35 + } } - ], + }, "loc": { "start": { "line": 17, - "column": 25 + "column": 18 }, "end": { "line": 17, - "column": 33 + "column": 35 } } - }, - "loc": { - "start": { - "line": 17, - "column": 18 - }, - "end": { - "line": 17, - "column": 35 - } } - }, + ], "loc": { "start": { "line": 17, @@ -758,35 +788,60 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 22, - "column": 59 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 59 + }, + "end": { + "line": 22, + "column": 66 + } + } }, - "end": { - "line": 22, - "column": 66 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 67 + }, + "end": { + "line": 22, + "column": 73 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 67 + }, + "end": { + "line": 22, + "column": 74 + } + } + }, "loc": { "start": { "line": 22, @@ -794,55 +849,45 @@ }, "end": { "line": 22, - "column": 73 + "column": 74 } } - }, - "loc": { - "start": { - "line": 22, - "column": 67 - }, - "end": { - "line": 22, - "column": 74 - } } - }, + ], "loc": { "start": { "line": 22, - "column": 67 + "column": 66 }, "end": { "line": 22, "column": 74 } } + }, + "loc": { + "start": { + "line": 22, + "column": 59 + }, + "end": { + "line": 22, + "column": 76 + } } - ], + }, "loc": { "start": { "line": 22, - "column": 66 + "column": 59 }, "end": { "line": 22, - "column": 74 + "column": 76 } } - }, - "loc": { - "start": { - "line": 22, - "column": 59 - }, - "end": { - "line": 22, - "column": 76 - } } - }, + ], "loc": { "start": { "line": 22, @@ -866,35 +911,60 @@ "type": "Identifier", "name": "promise", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 23, - "column": 18 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 25 + } + } }, - "end": { - "line": 23, - "column": 25 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 33 + } + } + }, "loc": { "start": { "line": 23, @@ -902,55 +972,45 @@ }, "end": { "line": 23, - "column": 32 + "column": 33 } } - }, - "loc": { - "start": { - "line": 23, - "column": 26 - }, - "end": { - "line": 23, - "column": 33 - } } - }, + ], "loc": { "start": { "line": 23, - "column": 26 + "column": 25 }, "end": { "line": 23, "column": 33 } } + }, + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 35 + } } - ], + }, "loc": { "start": { "line": 23, - "column": 25 - }, - "end": { - "line": 23, - "column": 33 - } - } - }, - "loc": { - "start": { - "line": 23, - "column": 18 - }, - "end": { - "line": 23, - "column": 35 + "column": 18 + }, + "end": { + "line": 23, + "column": 35 + } } } - }, + ], "loc": { "start": { "line": 23, @@ -1197,35 +1257,60 @@ "type": "ETSFunctionType", "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 22, - "column": 24 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 31 + } + } }, - "end": { - "line": 22, - "column": 31 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 39 + } + } + }, "loc": { "start": { "line": 22, @@ -1233,55 +1318,45 @@ }, "end": { "line": 22, - "column": 38 + "column": 39 } } - }, - "loc": { - "start": { - "line": 22, - "column": 32 - }, - "end": { - "line": 22, - "column": 39 - } } - }, + ], "loc": { "start": { "line": 22, - "column": 32 + "column": 31 }, "end": { "line": 22, "column": 39 } } + }, + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 41 + } } - ], + }, "loc": { "start": { "line": 22, - "column": 31 + "column": 24 }, "end": { "line": 22, - "column": 39 + "column": 41 } } - }, - "loc": { - "start": { - "line": 22, - "column": 24 - }, - "end": { - "line": 22, - "column": 41 - } } - }, + ], "loc": { "start": { "line": 22, @@ -1415,35 +1490,60 @@ "type": "Identifier", "name": "promise", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 29, - "column": 18 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 18 + }, + "end": { + "line": 29, + "column": 25 + } + } }, - "end": { - "line": 29, - "column": 25 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, "loc": { "start": { "line": 29, @@ -1451,55 +1551,45 @@ }, "end": { "line": 29, - "column": 32 + "column": 33 } } - }, - "loc": { - "start": { - "line": 29, - "column": 26 - }, - "end": { - "line": 29, - "column": 33 - } } - }, + ], "loc": { "start": { "line": 29, - "column": 26 + "column": 25 }, "end": { "line": 29, "column": 33 } } + }, + "loc": { + "start": { + "line": 29, + "column": 18 + }, + "end": { + "line": 29, + "column": 35 + } } - ], + }, "loc": { "start": { "line": 29, - "column": 25 + "column": 18 }, "end": { "line": 29, - "column": 33 + "column": 35 } } - }, - "loc": { - "start": { - "line": 29, - "column": 18 - }, - "end": { - "line": 29, - "column": 35 - } } - }, + ], "loc": { "start": { "line": 29, @@ -1800,35 +1890,60 @@ "type": "Identifier", "name": "promise", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 34, - "column": 18 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 18 + }, + "end": { + "line": 34, + "column": 25 + } + } }, - "end": { - "line": 34, - "column": 25 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 26 + }, + "end": { + "line": 34, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 26 + }, + "end": { + "line": 34, + "column": 33 + } + } + }, "loc": { "start": { "line": 34, @@ -1836,55 +1951,45 @@ }, "end": { "line": 34, - "column": 32 + "column": 33 } } - }, - "loc": { - "start": { - "line": 34, - "column": 26 - }, - "end": { - "line": 34, - "column": 33 - } } - }, + ], "loc": { "start": { "line": 34, - "column": 26 + "column": 25 }, "end": { "line": 34, "column": 33 } } + }, + "loc": { + "start": { + "line": 34, + "column": 18 + }, + "end": { + "line": 34, + "column": 35 + } } - ], + }, "loc": { "start": { "line": 34, - "column": 25 + "column": 18 }, "end": { "line": 34, - "column": 33 + "column": 35 } } - }, - "loc": { - "start": { - "line": 34, - "column": 18 - }, - "end": { - "line": 34, - "column": 35 - } } - }, + ], "loc": { "start": { "line": 34, @@ -2191,35 +2296,60 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Promise", - "decorators": [], - "loc": { - "start": { - "line": 38, - "column": 14 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 14 + }, + "end": { + "line": 38, + "column": 21 + } + } }, - "end": { - "line": 38, - "column": 21 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 22 + }, + "end": { + "line": 38, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 22 + }, + "end": { + "line": 38, + "column": 29 + } + } + }, "loc": { "start": { "line": 38, @@ -2227,55 +2357,45 @@ }, "end": { "line": 38, - "column": 28 + "column": 29 } } - }, - "loc": { - "start": { - "line": 38, - "column": 22 - }, - "end": { - "line": 38, - "column": 29 - } } - }, + ], "loc": { "start": { "line": 38, - "column": 22 + "column": 21 }, "end": { "line": 38, "column": 29 } } + }, + "loc": { + "start": { + "line": 38, + "column": 14 + }, + "end": { + "line": 38, + "column": 31 + } } - ], + }, "loc": { "start": { "line": 38, - "column": 21 + "column": 14 }, "end": { "line": 38, - "column": 29 + "column": 31 } } - }, - "loc": { - "start": { - "line": 38, - "column": 14 - }, - "end": { - "line": 38, - "column": 31 - } } - }, + ], "loc": { "start": { "line": 38, diff --git a/ets2panda/test/parser/ets/binary_op-expected.txt b/ets2panda/test/parser/ets/binary_op-expected.txt index 4be4d2ea48517d38274b99172e9921e0e2c1134b..dc41d3cfc8eac57f254b3d6e922bfcc066d59112 100644 --- a/ets2panda/test/parser/ets/binary_op-expected.txt +++ b/ets2panda/test/parser/ets/binary_op-expected.txt @@ -5325,13 +5325,38 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, "loc": { "start": { "line": 22, @@ -5339,21 +5364,11 @@ }, "end": { "line": 22, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 22, - "column": 9 - }, - "end": { - "line": 22, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 22, diff --git a/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt b/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt index cf852cc4c9f8e79ebaff9db05a99a2370c323fb4..dba27704398813251870499a6e5e33e8f087ac55 100644 --- a/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt @@ -299,13 +299,38 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, "loc": { "start": { "line": 18, @@ -313,21 +338,11 @@ }, "end": { "line": 18, - "column": 19 + "column": 21 } } - }, - "loc": { - "start": { - "line": 18, - "column": 18 - }, - "end": { - "line": 18, - "column": 21 - } } - }, + ], "loc": { "start": { "line": 18, diff --git a/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt b/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt index 2f57863379270d883e3c51ef185d743e23ee990e..e485881d1f8eea8912459c005aac377cec5ce20b 100644 --- a/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt @@ -68,13 +68,38 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, "loc": { "start": { "line": 17, @@ -82,21 +107,11 @@ }, "end": { "line": 17, - "column": 14 + "column": 16 } } - }, - "loc": { - "start": { - "line": 17, - "column": 13 - }, - "end": { - "line": 17, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 17, @@ -555,13 +570,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 17 + } + } + }, "loc": { "start": { "line": 23, @@ -569,21 +609,11 @@ }, "end": { "line": 23, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 23, - "column": 14 - }, - "end": { - "line": 23, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 23, diff --git a/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt b/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt index c8a3c89c3654e9524ca71a4129652428bfaec1ea..0fcb60e3135322a6bf7762189ad56de8f159726e 100644 --- a/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt @@ -437,13 +437,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, "loc": { "start": { "line": 20, @@ -451,21 +476,11 @@ }, "end": { "line": 20, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 20, - "column": 14 - }, - "end": { - "line": 20, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 20, diff --git a/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt b/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt index 9b424caf23208fbe35f822459afa89f11846f049..d37fa6d8135c28a9e10d5cd4f1a6fc2cfb651f13 100644 --- a/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt @@ -450,15 +450,40 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, "loc": { "start": { "line": 20, @@ -466,32 +491,22 @@ }, "end": { "line": 20, - "column": 15 + "column": 16 } } }, "loc": { "start": { "line": 20, - "column": 14 + "column": 18 }, "end": { "line": 20, - "column": 16 + "column": 19 } } - }, - "loc": { - "start": { - "line": 20, - "column": 14 - }, - "end": { - "line": 20, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 20, diff --git a/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt b/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt index 2ac81c66dd171fa491b4225ef1256f366c00ad42..50334b8aefd86e6c9ba73256d15d31c6eb73c88f 100644 --- a/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt @@ -156,13 +156,38 @@ } }, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, "loc": { "start": { "line": 18, @@ -170,21 +195,11 @@ }, "end": { "line": 18, - "column": 12 + "column": 14 } } - }, - "loc": { - "start": { - "line": 18, - "column": 11 - }, - "end": { - "line": 18, - "column": 14 - } } - }, + ], "loc": { "start": { "line": 18, diff --git a/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt b/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt index 5dffc7d900e674573395ae100ff05468f16059b6..82e7920ea128faada8351445825cd32abbf6b8f2 100644 --- a/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt @@ -556,13 +556,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, "loc": { "start": { "line": 21, @@ -570,21 +595,11 @@ }, "end": { "line": 21, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 21, - "column": 14 - }, - "end": { - "line": 21, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 21, diff --git a/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt b/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt index 1aed437dc327df8abf08b3f8e11dca184dfc7825..205d6300be37ff1f152160f3106439f3f0ced8ad 100644 --- a/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt @@ -847,13 +847,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "I", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "I", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 17 + } + } + }, "loc": { "start": { "line": 25, @@ -861,21 +886,11 @@ }, "end": { "line": 25, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 25, - "column": 14 - }, - "end": { - "line": 25, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 25, diff --git a/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt b/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt index 66b6147871606ec2d4d837e67c5ab72fea6a0ba4..cd6fc79aa53cceb990cf77c4bb380d3d8e79db06 100644 --- a/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt @@ -556,13 +556,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, "loc": { "start": { "line": 21, @@ -570,21 +595,11 @@ }, "end": { "line": 21, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 21, - "column": 14 - }, - "end": { - "line": 21, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 21, diff --git a/ets2panda/test/parser/ets/n_nullableTypeMissingNull-expected.txt b/ets2panda/test/parser/ets/n_nullableTypeMissingNull-expected.txt index ac3f5bbb268f902ecbfb1a4d1d65ffe54a0db7a8..8e4ebb99a9ee6d99251afbd9f77be180269157e5 100644 --- a/ets2panda/test/parser/ets/n_nullableTypeMissingNull-expected.txt +++ b/ets2panda/test/parser/ets/n_nullableTypeMissingNull-expected.txt @@ -1 +1 @@ -SyntaxError: Unexpected token, expected: 'null'. [n_nullableTypeMissingNull.ets:17:16] +SyntaxError: Invalid Type [n_nullableTypeMissingNull.ets:17:16] diff --git a/ets2panda/test/parser/ets/n_overrideWithNullable-expected.txt b/ets2panda/test/parser/ets/n_overrideWithNullable-expected.txt index 32fb21e40b74bf5320f95e3366b48929e7c85b89..d0923f2e1557c513b44c4bfd208b6a67424ffbd8 100644 --- a/ets2panda/test/parser/ets/n_overrideWithNullable-expected.txt +++ b/ets2panda/test/parser/ets/n_overrideWithNullable-expected.txt @@ -1141,13 +1141,38 @@ "type": "Identifier", "name": "a", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 27 + }, + "end": { + "line": 31, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 27 + }, + "end": { + "line": 31, + "column": 30 + } + } + }, "loc": { "start": { "line": 31, @@ -1155,21 +1180,11 @@ }, "end": { "line": 31, - "column": 28 + "column": 30 } } - }, - "loc": { - "start": { - "line": 31, - "column": 27 - }, - "end": { - "line": 31, - "column": 30 - } } - }, + ], "loc": { "start": { "line": 31, @@ -1346,13 +1361,38 @@ "type": "Identifier", "name": "t", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 40 + }, + "end": { + "line": 35, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 40 + }, + "end": { + "line": 35, + "column": 43 + } + } + }, "loc": { "start": { "line": 35, @@ -1360,21 +1400,11 @@ }, "end": { "line": 35, - "column": 41 + "column": 43 } } - }, - "loc": { - "start": { - "line": 35, - "column": 40 - }, - "end": { - "line": 35, - "column": 43 - } } - }, + ], "loc": { "start": { "line": 35, diff --git a/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt b/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt index 7a4275b3329c44d21b6eac4df4ed0bd6933af59d..7b845a475581a53fee4151647dc30c20a83d4361 100644 --- a/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt +++ b/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt @@ -351,13 +351,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, "loc": { "start": { "line": 19, @@ -365,21 +390,11 @@ }, "end": { "line": 19, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 19, - "column": 14 - }, - "end": { - "line": 19, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 19, diff --git a/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt b/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt index df711d1502c49da6a4a6a678fca403d1719b8860..28f7594bba14f3c613d0e68cbaa363b8855cdcb8 100644 --- a/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt +++ b/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt @@ -120,13 +120,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, "loc": { "start": { "line": 18, @@ -134,21 +159,11 @@ }, "end": { "line": 18, - "column": 19 + "column": 21 } } - }, - "loc": { - "start": { - "line": 18, - "column": 18 - }, - "end": { - "line": 18, - "column": 21 - } } - }, + ], "loc": { "start": { "line": 18, diff --git a/ets2panda/test/parser/ets/null-expected.txt b/ets2panda/test/parser/ets/null-expected.txt index b5a328254c9326b65170b466fedf55b3277ab123..62948820bc33709c34c0a0f477e5a7f7585c0331 100644 --- a/ets2panda/test/parser/ets/null-expected.txt +++ b/ets2panda/test/parser/ets/null-expected.txt @@ -508,13 +508,38 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "cls", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "cls", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, "loc": { "start": { "line": 19, @@ -522,21 +547,11 @@ }, "end": { "line": 19, - "column": 12 + "column": 14 } } - }, - "loc": { - "start": { - "line": 19, - "column": 9 - }, - "end": { - "line": 19, - "column": 14 - } } - }, + ], "loc": { "start": { "line": 19, @@ -689,13 +704,38 @@ "type": "Identifier", "name": "arg", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "cls", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "cls", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 24 + } + } + }, "loc": { "start": { "line": 22, @@ -703,21 +743,11 @@ }, "end": { "line": 22, - "column": 22 + "column": 24 } } - }, - "loc": { - "start": { - "line": 22, - "column": 19 - }, - "end": { - "line": 22, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 22, @@ -1204,13 +1234,38 @@ "type": "Identifier", "name": "e", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "cls", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "cls", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 31, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 31, + "column": 18 + } + } + }, "loc": { "start": { "line": 31, @@ -1218,21 +1273,11 @@ }, "end": { "line": 31, - "column": 16 + "column": 18 } } - }, - "loc": { - "start": { - "line": 31, - "column": 13 - }, - "end": { - "line": 31, - "column": 18 - } } - }, + ], "loc": { "start": { "line": 31, diff --git a/ets2panda/test/parser/ets/null_valid-expected.txt b/ets2panda/test/parser/ets/null_valid-expected.txt index 78a7b9c1a8d0f1bae13a38b1bc0e8bd8dd1f5f82..5167140afe6d8cc2b42e257a62f0e4d4ffb9456b 100644 --- a/ets2panda/test/parser/ets/null_valid-expected.txt +++ b/ets2panda/test/parser/ets/null_valid-expected.txt @@ -248,13 +248,38 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, "loc": { "start": { "line": 17, @@ -262,21 +287,11 @@ }, "end": { "line": 17, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 17, - "column": 9 - }, - "end": { - "line": 17, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 17, diff --git a/ets2panda/test/parser/ets/nullableType-expected.txt b/ets2panda/test/parser/ets/nullableType-expected.txt index 4ffaa76dd684359644472e7adf7c6e4ac0783152..6f873f8e0e193cb33eb5f0b9c0b199957cc36015 100644 --- a/ets2panda/test/parser/ets/nullableType-expected.txt +++ b/ets2panda/test/parser/ets/nullableType-expected.txt @@ -513,35 +513,65 @@ } }, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + } + ], "loc": { "start": { "line": 19, - "column": 13 + "column": 12 }, "end": { "line": 19, - "column": 14 + "column": 16 } } - }, - "loc": { - "start": { - "line": 19, - "column": 13 - }, - "end": { - "line": 19, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 19, @@ -582,13 +612,38 @@ } }, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, "loc": { "start": { "line": 20, @@ -596,21 +651,11 @@ }, "end": { "line": 20, - "column": 14 + "column": 16 } } - }, - "loc": { - "start": { - "line": 20, - "column": 13 - }, - "end": { - "line": 20, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 20, @@ -720,35 +765,65 @@ } }, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A_alias", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A_alias", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 34 + } + } + } + ], "loc": { "start": { "line": 22, - "column": 25 + "column": 24 }, "end": { "line": 22, - "column": 32 + "column": 34 } } - }, - "loc": { - "start": { - "line": 22, - "column": 25 - }, - "end": { - "line": 22, - "column": 34 - } } - }, + ], "loc": { "start": { "line": 22, @@ -914,13 +989,38 @@ "optional": false, "computed": false, "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 132, + "column": 15 + }, + "end": { + "line": 132, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 132, + "column": 15 + }, + "end": { + "line": 132, + "column": 18 + } + } + }, "loc": { "start": { "line": 132, @@ -928,21 +1028,11 @@ }, "end": { "line": 132, - "column": 16 + "column": 18 } } - }, - "loc": { - "start": { - "line": 132, - "column": 15 - }, - "end": { - "line": 132, - "column": 18 - } } - }, + ], "loc": { "start": { "line": 132, @@ -1018,13 +1108,38 @@ "type": "Identifier", "name": "t", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 133, + "column": 18 + }, + "end": { + "line": 133, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 133, + "column": 18 + }, + "end": { + "line": 133, + "column": 21 + } + } + }, "loc": { "start": { "line": 133, @@ -1032,21 +1147,11 @@ }, "end": { "line": 133, - "column": 19 + "column": 21 } } - }, - "loc": { - "start": { - "line": 133, - "column": 18 - }, - "end": { - "line": 133, - "column": 21 - } } - }, + ], "loc": { "start": { "line": 133, @@ -1083,13 +1188,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 133, + "column": 30 + }, + "end": { + "line": 133, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 133, + "column": 30 + }, + "end": { + "line": 133, + "column": 33 + } + } + }, "loc": { "start": { "line": 133, @@ -1097,21 +1227,11 @@ }, "end": { "line": 133, - "column": 31 + "column": 33 } } - }, - "loc": { - "start": { - "line": 133, - "column": 30 - }, - "end": { - "line": 133, - "column": 33 - } } - }, + ], "loc": { "start": { "line": 133, @@ -1908,13 +2028,38 @@ "type": "Identifier", "name": "a", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 11 + }, + "end": { + "line": 31, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 11 + }, + "end": { + "line": 31, + "column": 19 + } + } + }, "loc": { "start": { "line": 31, @@ -1922,21 +2067,11 @@ }, "end": { "line": 31, - "column": 17 + "column": 19 } } - }, - "loc": { - "start": { - "line": 31, - "column": 11 - }, - "end": { - "line": 31, - "column": 19 - } } - }, + ], "loc": { "start": { "line": 31, @@ -2007,13 +2142,38 @@ "type": "Identifier", "name": "b", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 11 + }, + "end": { + "line": 32, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 11 + }, + "end": { + "line": 32, + "column": 19 + } + } + }, "loc": { "start": { "line": 32, @@ -2021,21 +2181,11 @@ }, "end": { "line": 32, - "column": 17 + "column": 19 } } - }, - "loc": { - "start": { - "line": 32, - "column": 11 - }, - "end": { - "line": 32, - "column": 19 - } } - }, + ], "loc": { "start": { "line": 32, @@ -2147,13 +2297,38 @@ "type": "Identifier", "name": "c", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 11 + }, + "end": { + "line": 33, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 11 + }, + "end": { + "line": 33, + "column": 19 + } + } + }, "loc": { "start": { "line": 33, @@ -2161,21 +2336,11 @@ }, "end": { "line": 33, - "column": 17 + "column": 19 } } - }, - "loc": { - "start": { - "line": 33, - "column": 11 - }, - "end": { - "line": 33, - "column": 19 - } } - }, + ], "loc": { "start": { "line": 33, @@ -2247,35 +2412,80 @@ "type": "Identifier", "name": "d", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 14 + }, + "end": { + "line": 34, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 14 + }, + "end": { + "line": 34, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 13 + }, + "end": { + "line": 34, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 34, + "column": 12 + }, + "end": { + "line": 34, + "column": 21 + } + } + } + ], "loc": { "start": { "line": 34, - "column": 14 + "column": 11 }, "end": { "line": 34, - "column": 20 + "column": 21 } } - }, - "loc": { - "start": { - "line": 34, - "column": 14 - }, - "end": { - "line": 34, - "column": 21 - } } - }, + ], "loc": { "start": { "line": 34, @@ -2362,13 +2572,38 @@ "type": "Identifier", "name": "f", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 11 + }, + "end": { + "line": 35, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 11 + }, + "end": { + "line": 35, + "column": 14 + } + } + }, "loc": { "start": { "line": 35, @@ -2376,21 +2611,11 @@ }, "end": { "line": 35, - "column": 12 + "column": 14 } } - }, - "loc": { - "start": { - "line": 35, - "column": 11 - }, - "end": { - "line": 35, - "column": 14 - } } - }, + ], "loc": { "start": { "line": 35, @@ -2461,13 +2686,38 @@ "type": "Identifier", "name": "g", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "tmp", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "tmp", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 11 + }, + "end": { + "line": 36, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 11 + }, + "end": { + "line": 36, + "column": 16 + } + } + }, "loc": { "start": { "line": 36, @@ -2475,21 +2725,11 @@ }, "end": { "line": 36, - "column": 14 + "column": 16 } } - }, - "loc": { - "start": { - "line": 36, - "column": 11 - }, - "end": { - "line": 36, - "column": 16 - } } - }, + ], "loc": { "start": { "line": 36, @@ -2871,35 +3111,75 @@ "type": "Identifier", "name": "generic", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Gen", - "decorators": [], - "loc": { - "start": { - "line": 42, - "column": 17 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Gen", + "decorators": [], + "loc": { + "start": { + "line": 42, + "column": 17 + }, + "end": { + "line": 42, + "column": 20 + } + } }, - "end": { - "line": 42, - "column": 20 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 42, + "column": 21 + }, + "end": { + "line": 42, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 42, + "column": 21 + }, + "end": { + "line": 42, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 42, + "column": 21 + }, + "end": { + "line": 42, + "column": 24 + } + } + } + ], "loc": { "start": { "line": 42, @@ -2907,55 +3187,45 @@ }, "end": { "line": 42, - "column": 22 + "column": 24 } } - }, - "loc": { - "start": { - "line": 42, - "column": 21 - }, - "end": { - "line": 42, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 42, - "column": 21 + "column": 20 }, "end": { "line": 42, - "column": 24 + "column": 30 } } + }, + "loc": { + "start": { + "line": 42, + "column": 17 + }, + "end": { + "line": 42, + "column": 32 + } } - ], + }, "loc": { "start": { "line": 42, - "column": 20 + "column": 17 }, "end": { "line": 42, - "column": 30 + "column": 32 } } - }, - "loc": { - "start": { - "line": 42, - "column": 17 - }, - "end": { - "line": 42, - "column": 32 - } } - }, + ], "loc": { "start": { "line": 42, @@ -3004,13 +3274,38 @@ "type": "TSTypeParameterInstantiation", "params": [ { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 42, + "column": 48 + }, + "end": { + "line": 42, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 42, + "column": 48 + }, + "end": { + "line": 42, + "column": 51 + } + } + }, "loc": { "start": { "line": 42, @@ -3018,21 +3313,11 @@ }, "end": { "line": 42, - "column": 49 + "column": 51 } } - }, - "loc": { - "start": { - "line": 42, - "column": 48 - }, - "end": { - "line": 42, - "column": 51 - } } - }, + ], "loc": { "start": { "line": 42, @@ -3123,13 +3408,38 @@ "type": "Identifier", "name": "h", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Char", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Char", + "decorators": [], + "loc": { + "start": { + "line": 44, + "column": 11 + }, + "end": { + "line": 44, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 44, + "column": 11 + }, + "end": { + "line": 44, + "column": 17 + } + } + }, "loc": { "start": { "line": 44, @@ -3137,21 +3447,11 @@ }, "end": { "line": 44, - "column": 15 + "column": 17 } } - }, - "loc": { - "start": { - "line": 44, - "column": 11 - }, - "end": { - "line": 44, - "column": 17 - } } - }, + ], "loc": { "start": { "line": 44, @@ -3325,35 +3625,75 @@ "type": "Identifier", "name": "gt1", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Gen", - "decorators": [], - "loc": { - "start": { - "line": 47, - "column": 13 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Gen", + "decorators": [], + "loc": { + "start": { + "line": 47, + "column": 13 + }, + "end": { + "line": 47, + "column": 16 + } + } }, - "end": { - "line": 47, - "column": 16 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 47, + "column": 17 + }, + "end": { + "line": 47, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 47, + "column": 17 + }, + "end": { + "line": 47, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 47, + "column": 17 + }, + "end": { + "line": 47, + "column": 20 + } + } + } + ], "loc": { "start": { "line": 47, @@ -3361,55 +3701,45 @@ }, "end": { "line": 47, - "column": 18 + "column": 20 } } - }, - "loc": { - "start": { - "line": 47, - "column": 17 - }, - "end": { - "line": 47, - "column": 20 - } } - }, + ], "loc": { "start": { "line": 47, - "column": 17 + "column": 16 }, "end": { "line": 47, - "column": 20 + "column": 26 } } + }, + "loc": { + "start": { + "line": 47, + "column": 13 + }, + "end": { + "line": 47, + "column": 28 + } } - ], + }, "loc": { "start": { "line": 47, - "column": 16 + "column": 13 }, "end": { "line": 47, - "column": 26 + "column": 28 } } - }, - "loc": { - "start": { - "line": 47, - "column": 13 - }, - "end": { - "line": 47, - "column": 28 - } } - }, + ], "loc": { "start": { "line": 47, @@ -4248,20 +4578,35 @@ "type": "Identifier", "name": "narr", "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 61, - "column": 14 + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 61, + "column": 14 + }, + "end": { + "line": 61, + "column": 17 + } + } }, - "end": { - "line": 61, - "column": 17 + "loc": { + "start": { + "line": 61, + "column": 20 + }, + "end": { + "line": 61, + "column": 21 + } } } - }, + ], "loc": { "start": { "line": 61, @@ -4525,48 +4870,78 @@ "type": "Identifier", "name": "arron", "typeAnnotation": { - "type": "TSArrayType", - "elementType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "TSArrayType", + "elementType": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 66, + "column": 16 + }, + "end": { + "line": 66, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 66, + "column": 16 + }, + "end": { + "line": 66, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 66, + "column": 16 + }, + "end": { + "line": 66, + "column": 24 + } + } + } + ], "loc": { "start": { "line": 66, - "column": 16 + "column": 15 }, "end": { "line": 66, - "column": 22 + "column": 24 } } }, "loc": { "start": { "line": 66, - "column": 16 + "column": 33 }, "end": { "line": 66, - "column": 24 + "column": 34 } } - }, - "loc": { - "start": { - "line": 66, - "column": 15 - }, - "end": { - "line": 66, - "column": 24 - } } - }, + ], "loc": { "start": { "line": 66, @@ -5346,13 +5721,38 @@ "type": "Identifier", "name": "on", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 77, + "column": 12 + }, + "end": { + "line": 77, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 77, + "column": 12 + }, + "end": { + "line": 77, + "column": 20 + } + } + }, "loc": { "start": { "line": 77, @@ -5360,21 +5760,11 @@ }, "end": { "line": 77, - "column": 18 + "column": 20 } } - }, - "loc": { - "start": { - "line": 77, - "column": 12 - }, - "end": { - "line": 77, - "column": 20 - } } - }, + ], "loc": { "start": { "line": 77, @@ -6030,13 +6420,38 @@ "type": "Identifier", "name": "an", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 86, + "column": 12 + }, + "end": { + "line": 86, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 86, + "column": 12 + }, + "end": { + "line": 86, + "column": 15 + } + } + }, "loc": { "start": { "line": 86, @@ -6044,21 +6459,11 @@ }, "end": { "line": 86, - "column": 13 + "column": 15 } } - }, - "loc": { - "start": { - "line": 86, - "column": 12 - }, - "end": { - "line": 86, - "column": 15 - } } - }, + ], "loc": { "start": { "line": 86, @@ -7261,13 +7666,38 @@ "type": "Identifier", "name": "bn", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "B", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 103, + "column": 12 + }, + "end": { + "line": 103, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 103, + "column": 12 + }, + "end": { + "line": 103, + "column": 15 + } + } + }, "loc": { "start": { "line": 103, @@ -7275,21 +7705,11 @@ }, "end": { "line": 103, - "column": 13 + "column": 15 } } - }, - "loc": { - "start": { - "line": 103, - "column": 12 - }, - "end": { - "line": 103, - "column": 15 - } } - }, + ], "loc": { "start": { "line": 103, @@ -7755,13 +8175,38 @@ "expression": false, "params": [], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Object", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 112, + "column": 33 + }, + "end": { + "line": 112, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 112, + "column": 33 + }, + "end": { + "line": 112, + "column": 41 + } + } + }, "loc": { "start": { "line": 112, @@ -7769,21 +8214,11 @@ }, "end": { "line": 112, - "column": 39 + "column": 41 } } - }, - "loc": { - "start": { - "line": 112, - "column": 33 - }, - "end": { - "line": 112, - "column": 41 - } } - }, + ], "loc": { "start": { "line": 112, @@ -7878,13 +8313,38 @@ "type": "Identifier", "name": "x", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 114, + "column": 10 + }, + "end": { + "line": 114, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 114, + "column": 10 + }, + "end": { + "line": 114, + "column": 15 + } + } + }, "loc": { "start": { "line": 114, @@ -7892,21 +8352,11 @@ }, "end": { "line": 114, - "column": 13 + "column": 15 } } - }, - "loc": { - "start": { - "line": 114, - "column": 10 - }, - "end": { - "line": 114, - "column": 15 - } } - }, + ], "loc": { "start": { "line": 114, @@ -8063,35 +8513,50 @@ "type": "Identifier", "name": "y", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Long", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Long", + "decorators": [], + "loc": { + "start": { + "line": 117, + "column": 10 + }, + "end": { + "line": 117, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 117, + "column": 10 + }, + "end": { + "line": 117, + "column": 16 + } + } + }, "loc": { "start": { "line": 117, "column": 10 }, - "end": { - "line": 117, - "column": 14 - } - } - }, - "loc": { - "start": { - "line": 117, - "column": 10 - }, - "end": { - "line": 117, - "column": 16 + "end": { + "line": 117, + "column": 16 + } } } - }, + ], "loc": { "start": { "line": 117, @@ -8335,13 +8800,38 @@ "type": "Identifier", "name": "arg1", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 123, + "column": 39 + }, + "end": { + "line": 123, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 123, + "column": 39 + }, + "end": { + "line": 123, + "column": 44 + } + } + }, "loc": { "start": { "line": 123, @@ -8349,21 +8839,11 @@ }, "end": { "line": 123, - "column": 42 + "column": 44 } } - }, - "loc": { - "start": { - "line": 123, - "column": 39 - }, - "end": { - "line": 123, - "column": 44 - } } - }, + ], "loc": { "start": { "line": 123, @@ -8404,35 +8884,65 @@ "type": "Identifier", "name": "args2", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "tmp", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "tmp", + "decorators": [], + "loc": { + "start": { + "line": 123, + "column": 67 + }, + "end": { + "line": 123, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 123, + "column": 67 + }, + "end": { + "line": 123, + "column": 72 + } + } + }, + "loc": { + "start": { + "line": 123, + "column": 67 + }, + "end": { + "line": 123, + "column": 72 + } + } + } + ], "loc": { "start": { "line": 123, - "column": 67 + "column": 66 }, "end": { "line": 123, - "column": 70 + "column": 72 } } - }, - "loc": { - "start": { - "line": 123, - "column": 67 - }, - "end": { - "line": 123, - "column": 72 - } } - }, + ], "loc": { "start": { "line": 123, @@ -8469,13 +8979,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Char", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Char", + "decorators": [], + "loc": { + "start": { + "line": 123, + "column": 89 + }, + "end": { + "line": 123, + "column": 93 + } + } + }, + "loc": { + "start": { + "line": 123, + "column": 89 + }, + "end": { + "line": 123, + "column": 95 + } + } + }, "loc": { "start": { "line": 123, @@ -8483,21 +9018,11 @@ }, "end": { "line": 123, - "column": 93 + "column": 95 } } - }, - "loc": { - "start": { - "line": 123, - "column": 89 - }, - "end": { - "line": 123, - "column": 95 - } } - }, + ], "loc": { "start": { "line": 123, @@ -8637,35 +9162,75 @@ "type": "Identifier", "name": "arg", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Gen", - "decorators": [], - "loc": { - "start": { - "line": 127, - "column": 41 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Gen", + "decorators": [], + "loc": { + "start": { + "line": 127, + "column": 41 + }, + "end": { + "line": 127, + "column": 44 + } + } }, - "end": { - "line": 127, - "column": 44 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 127, + "column": 45 + }, + "end": { + "line": 127, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 127, + "column": 45 + }, + "end": { + "line": 127, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 127, + "column": 45 + }, + "end": { + "line": 127, + "column": 48 + } + } + } + ], "loc": { "start": { "line": 127, @@ -8673,55 +9238,45 @@ }, "end": { "line": 127, - "column": 46 + "column": 48 } } - }, - "loc": { - "start": { - "line": 127, - "column": 45 - }, - "end": { - "line": 127, - "column": 48 - } } - }, + ], "loc": { "start": { "line": 127, - "column": 45 + "column": 44 }, "end": { "line": 127, - "column": 48 + "column": 54 } } + }, + "loc": { + "start": { + "line": 127, + "column": 41 + }, + "end": { + "line": 127, + "column": 55 + } } - ], + }, "loc": { "start": { "line": 127, - "column": 44 + "column": 40 }, "end": { "line": 127, - "column": 54 + "column": 55 } } - }, - "loc": { - "start": { - "line": 127, - "column": 41 - }, - "end": { - "line": 127, - "column": 55 - } } - }, + ], "loc": { "start": { "line": 127, @@ -8758,35 +9313,75 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Gen", - "decorators": [], - "loc": { - "start": { - "line": 127, - "column": 66 + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Gen", + "decorators": [], + "loc": { + "start": { + "line": 127, + "column": 66 + }, + "end": { + "line": 127, + "column": 69 + } + } }, - "end": { - "line": 127, - "column": 69 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "tmp", - "decorators": [], + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "tmp", + "decorators": [], + "loc": { + "start": { + "line": 127, + "column": 70 + }, + "end": { + "line": 127, + "column": 73 + } + } + }, + "loc": { + "start": { + "line": 127, + "column": 70 + }, + "end": { + "line": 127, + "column": 75 + } + } + }, + "loc": { + "start": { + "line": 127, + "column": 70 + }, + "end": { + "line": 127, + "column": 75 + } + } + } + ], "loc": { "start": { "line": 127, @@ -8794,55 +9389,45 @@ }, "end": { "line": 127, - "column": 73 + "column": 75 } } - }, - "loc": { - "start": { - "line": 127, - "column": 70 - }, - "end": { - "line": 127, - "column": 75 - } } - }, + ], "loc": { "start": { "line": 127, - "column": 70 + "column": 69 }, "end": { "line": 127, - "column": 75 + "column": 81 } } + }, + "loc": { + "start": { + "line": 127, + "column": 66 + }, + "end": { + "line": 127, + "column": 83 + } } - ], + }, "loc": { "start": { "line": 127, - "column": 69 + "column": 66 }, "end": { "line": 127, - "column": 81 + "column": 83 } } - }, - "loc": { - "start": { - "line": 127, - "column": 66 - }, - "end": { - "line": 127, - "column": 83 - } } - }, + ], "loc": { "start": { "line": 127, @@ -8983,13 +9568,38 @@ "type": "Identifier", "name": "arg", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 140, + "column": 37 + }, + "end": { + "line": 140, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 140, + "column": 37 + }, + "end": { + "line": 140, + "column": 40 + } + } + }, "loc": { "start": { "line": 140, @@ -8997,21 +9607,11 @@ }, "end": { "line": 140, - "column": 38 + "column": 40 } } - }, - "loc": { - "start": { - "line": 140, - "column": 37 - }, - "end": { - "line": 140, - "column": 40 - } } - }, + ], "loc": { "start": { "line": 140, @@ -9048,13 +9648,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "T", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 140, + "column": 49 + }, + "end": { + "line": 140, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 140, + "column": 49 + }, + "end": { + "line": 140, + "column": 52 + } + } + }, "loc": { "start": { "line": 140, @@ -9062,21 +9687,11 @@ }, "end": { "line": 140, - "column": 50 + "column": 52 } } - }, - "loc": { - "start": { - "line": 140, - "column": 49 - }, - "end": { - "line": 140, - "column": 52 - } } - }, + ], "loc": { "start": { "line": 140, @@ -9464,13 +10079,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 146, + "column": 23 + }, + "end": { + "line": 146, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 146, + "column": 23 + }, + "end": { + "line": 146, + "column": 28 + } + } + }, "loc": { "start": { "line": 146, @@ -9478,21 +10118,11 @@ }, "end": { "line": 146, - "column": 26 + "column": 28 } } - }, - "loc": { - "start": { - "line": 146, - "column": 23 - }, - "end": { - "line": 146, - "column": 28 - } } - }, + ], "loc": { "start": { "line": 146, @@ -9579,13 +10209,38 @@ } ], "returnType": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 146, + "column": 47 + }, + "end": { + "line": 146, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 146, + "column": 47 + }, + "end": { + "line": 146, + "column": 52 + } + } + }, "loc": { "start": { "line": 146, @@ -9593,21 +10248,11 @@ }, "end": { "line": 146, - "column": 50 + "column": 52 } } - }, - "loc": { - "start": { - "line": 146, - "column": 47 - }, - "end": { - "line": 146, - "column": 52 - } } - }, + ], "loc": { "start": { "line": 146, @@ -10057,13 +10702,38 @@ "type": "Identifier", "name": "a", "typeAnnotation": { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "A", - "decorators": [], + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 154, + "column": 27 + }, + "end": { + "line": 154, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 154, + "column": 27 + }, + "end": { + "line": 154, + "column": 30 + } + } + }, "loc": { "start": { "line": 154, @@ -10071,21 +10741,11 @@ }, "end": { "line": 154, - "column": 28 + "column": 30 } } - }, - "loc": { - "start": { - "line": 154, - "column": 27 - }, - "end": { - "line": 154, - "column": 30 - } } - }, + ], "loc": { "start": { "line": 154, diff --git a/ets2panda/test/public/ast_verifier_test.cpp b/ets2panda/test/public/ast_verifier_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd36a1937988cfe8ea1ae3622f6bfae857a57f81 --- /dev/null +++ b/ets2panda/test/public/ast_verifier_test.cpp @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "macros.h" + +#include "compiler/core/ASTVerifier.h" +#include "ir/astDump.h" +#include "ir/expressions/literals/stringLiteral.h" + +class ASTVerifierTest : public testing::Test { +public: + ASTVerifierTest() = default; + ~ASTVerifierTest() override = default; + + NO_COPY_SEMANTIC(ASTVerifierTest); + NO_MOVE_SEMANTIC(ASTVerifierTest); + +private: +}; + +TEST_F(ASTVerifierTest, NullParent) +{ + panda::es2panda::compiler::ASTVerifier verifier {}; + panda::es2panda::ir::StringLiteral empty_node; + + bool has_parent = verifier.HasParent(&empty_node); + auto messages = verifier.GetErrorMessages(); + + ASSERT_EQ(has_parent, false); + ASSERT_NE(messages.size(), 0); + ASSERT_EQ(messages[0], "NULL_PARENT: STR_LITERAL "); +} + +TEST_F(ASTVerifierTest, NullType) +{ + panda::es2panda::compiler::ASTVerifier verifier {}; + panda::es2panda::ir::StringLiteral empty_node; + + bool has_type = verifier.HasType(&empty_node); + auto messages = verifier.GetErrorMessages(); + + ASSERT_EQ(has_type, false); + ASSERT_NE(messages.size(), 0); + ASSERT_EQ(messages[0], "NULL_TS_TYPE: STR_LITERAL "); +} + +TEST_F(ASTVerifierTest, WithoutScope) +{ + panda::es2panda::compiler::ASTVerifier verifier {}; + panda::es2panda::ir::StringLiteral empty_node; + + bool has_scope = verifier.HasScope(&empty_node); + auto messages = verifier.GetErrorMessages(); + + ASSERT_EQ(has_scope, true); + ASSERT_EQ(messages.size(), 0); +} diff --git a/ets2panda/util/options.cpp b/ets2panda/util/options.cpp index a5c73f877dac0102aeb9d5b33c3da6c6c148cda9..f5d6b87d989a9251c759e0300d71a9ce9f92c19a 100644 --- a/ets2panda/util/options.cpp +++ b/ets2panda/util/options.cpp @@ -36,21 +36,21 @@ Options::~Options() delete argparser_; } -static std::unordered_set StringToStringSet(const std::string &str) +static std::unordered_set StringToStringSet(const std::string &str) { - std::unordered_set res; + std::unordered_set res; std::string_view curr_str {str}; auto ix = curr_str.find(','); while (ix != std::string::npos) { if (ix != 0) { - res.insert(curr_str.substr(0, ix)); + res.insert(std::string(curr_str.substr(0, ix))); } curr_str = curr_str.substr(ix + 1); ix = curr_str.find(','); } if (!curr_str.empty()) { - res.insert(curr_str); + res.insert(std::string(curr_str)); } return res; }