diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index e498a933f7a1b59130d2b44c63b7268abafb779a..30bba267c94ff308429d0214a574b896b7375555 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -254,6 +254,7 @@ libes2panda_sources = [ "ir/module/importNamespaceSpecifier.cpp", "ir/module/importSpecifier.cpp", "ir/opaqueTypeNode.cpp", + "ir/srcDump.cpp", "ir/statement.cpp", "ir/statements/assertStatement.cpp", "ir/statements/blockStatement.cpp", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index f5bf5b757176ec146ec278197187c5725150f96f..2cc33b5480082c1454963bdcaaf8ce060761db2a 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -175,6 +175,7 @@ set(ES2PANDA_LIB_SRC compiler/lowering/ets/expandBrackets.cpp compiler/lowering/ets/promiseVoid.cpp ir/astDump.cpp + ir/srcDump.cpp ir/astNode.cpp ir/irnode.cpp ir/typeNode.cpp diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index bf673fc5f44375a1eabf622c5891fb09a525df53..77b7302021f6e33c74284f4eb5b696926ac5095c 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -166,7 +166,7 @@ static void CheckExtensionIsShadowedByMethod(checker::ETSChecker *checker, check static void CheckExtensionMethod(checker::ETSChecker *checker, ir::ScriptFunction *extension_func, ir::MethodDefinition *node) { - auto *const class_type = extension_func->Signature()->Params()[0]->TsType(); + auto *const class_type = ETSChecker::GetApparentType(extension_func->Signature()->Params()[0]->TsType()); if (!class_type->IsETSObjectType() || (!class_type->AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::CLASS) && !class_type->AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::INTERFACE))) { @@ -493,7 +493,8 @@ checker::Type *ETSAnalyzer::Check(ir::ETSLaunchExpression *expr) const : expr->expr_->TsType(); checker::Substitution *substitution = checker->NewSubstitution(); ASSERT(launch_promise_type->TypeArguments().size() == 1); - substitution->emplace(checker->GetOriginalBaseType(launch_promise_type->TypeArguments()[0]), expr_type); + checker::ETSChecker::EmplaceSubstituted( + substitution, launch_promise_type->TypeArguments()[0]->AsETSTypeParameter()->GetOriginal(), expr_type); expr->SetTsType(launch_promise_type->Substitute(checker->Relation(), substitution)); return expr->TsType(); @@ -506,6 +507,22 @@ checker::Type *ETSAnalyzer::Check(ir::ETSNewArrayInstanceExpression *expr) const auto *element_type = expr->type_reference_->GetType(checker); checker->ValidateArrayIndex(expr->dimension_, true); + if (!element_type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE) && !element_type->IsNullish() && + !element_type->HasTypeFlag(TypeFlag::GENERIC) && !element_type->HasTypeFlag(TypeFlag::ETS_ARRAY) && + element_type->ToAssemblerName().str() != "Ball") { + // Check only valid for ETS_PRIMITIVE and IsNullish, GENERIC and ETS_ARRAY are workaround checks for stdlib + // Ball is workaround for koala ui lib + if (element_type->IsETSObjectType()) { + auto *callee_obj = element_type->AsETSObjectType(); + if (!callee_obj->HasObjectFlag(checker::ETSObjectFlags::ABSTRACT)) { + // A workaround check for new Interface[...] in test cases + expr->default_constructor_signature_ = + checker->CollectParameterlessConstructor(callee_obj->ConstructSignatures(), expr->Start()); + checker->ValidateSignatureAccessibility(callee_obj, nullptr, expr->default_constructor_signature_, + expr->Start()); + } + } + } expr->SetTsType(checker->CreateETSArrayType(element_type)); checker->CreateBuiltinArraySignature(expr->TsType()->AsETSArrayType(), 1); return expr->TsType(); @@ -553,7 +570,7 @@ checker::Type *ETSAnalyzer::Check(ir::ETSNewClassInstanceExpression *expr) const checker->CheckObjectLiteralArguments(signature, expr->GetArguments()); checker->AddUndefinedParamsForDefaultParams(signature, expr->arguments_, checker); - checker->ValidateSignatureAccessibility(callee_obj, signature, expr->Start()); + checker->ValidateSignatureAccessibility(callee_obj, nullptr, signature, expr->Start()); ASSERT(signature->Function() != nullptr); @@ -779,31 +796,7 @@ checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const 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()) { - /* - when function body is ArrowFunctionExpression, need infer type for this function body - example code: - ``` - let x = () => () => {} - ``` - */ - if (expr->Function()->ReturnTypeAnnotation() == nullptr) { - if (expr->Function()->Body()->IsArrowFunctionExpression()) { - auto *arrow_func = expr->Function()->Body()->AsArrowFunctionExpression(); - auto *type_annotation = arrow_func->CreateTypeAnnotation(checker); - func_type->CallSignatures()[0]->SetReturnType(type_annotation->GetType(checker)); - } else { - 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); - } + expr->Function()->Body()->Check(checker); checker->Context().SetContainingSignature(nullptr); checker->CheckCapturedVariables(); @@ -899,7 +892,7 @@ checker::Type *ETSAnalyzer::Check(ir::AwaitExpression *expr) const return expr->TsType(); } - checker::Type *arg_type = expr->argument_->Check(checker); + checker::Type *arg_type = ETSChecker::GetApparentType(expr->argument_->Check(checker)); // Check the argument type of await expression if (!arg_type->IsETSObjectType() || (arg_type->AsETSObjectType()->AssemblerName() != compiler::Signatures::BUILTIN_PROMISE)) { @@ -1055,7 +1048,7 @@ checker::Type *ETSAnalyzer::GetReturnType(ir::CallExpression *expr, checker::Typ if (!is_functional_interface) { checker::ETSObjectType *callee_obj = ChooseCalleeObj(checker, expr, callee_type, is_constructor_call); - checker->ValidateSignatureAccessibility(callee_obj, signature, expr->Start()); + checker->ValidateSignatureAccessibility(callee_obj, expr, signature, expr->Start()); } ASSERT(signature->Function() != nullptr); @@ -1072,7 +1065,13 @@ checker::Type *ETSAnalyzer::GetReturnType(ir::CallExpression *expr, checker::Typ expr->SetSignature(signature); } - return signature->ReturnType(); + auto *return_type = signature->ReturnType(); + + if (signature->HasSignatureFlag(SignatureFlags::THIS_RETURN_TYPE)) { + return_type = ChooseCalleeObj(checker, expr, callee_type, is_constructor_call); + } + + return return_type; } checker::Type *ETSAnalyzer::Check(ir::CallExpression *expr) const @@ -1082,7 +1081,7 @@ checker::Type *ETSAnalyzer::Check(ir::CallExpression *expr) const return expr->TsType(); } auto *old_callee = expr->Callee(); - checker::Type *callee_type = expr->Callee()->Check(checker); + checker::Type *callee_type = ETSChecker::GetApparentType(expr->Callee()->Check(checker)); if (expr->Callee() != old_callee) { // If it is a static invoke, the callee will be transformed from an identifier to a member expression // Type check the callee again for member expression @@ -1111,6 +1110,7 @@ checker::Type *ETSAnalyzer::Check(ir::CallExpression *expr) const if (expr->Signature()->HasSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE)) { expr->Signature()->OwnerVar()->Declaration()->Node()->Check(checker); return_type = expr->Signature()->ReturnType(); + // NOTE(vpukhov): #14902 substituted signature is not updated } expr->SetOptionalType(return_type); if (expr->IsOptional() && callee_type->IsNullishOrNullLike()) { @@ -1119,6 +1119,7 @@ checker::Type *ETSAnalyzer::Check(ir::CallExpression *expr) const checker->Relation()->SetNode(nullptr); } expr->SetTsType(return_type); + expr->SetUncheckedType(checker->GuaranteedTypeForUncheckedCallReturn(expr->Signature())); return expr->TsType(); } @@ -1169,7 +1170,7 @@ checker::Type *ETSAnalyzer::Check(ir::ConditionalExpression *expr) const } } else { if (!(consequent_type->IsETSArrayType() || alternate_type->IsETSArrayType()) && - !(consequent_type->IsETSObjectType() && alternate_type->IsETSObjectType())) { + !(checker->IsReferenceType(consequent_type) && checker->IsReferenceType(alternate_type))) { checker->ThrowTypeError("Type error", expr->Range().start); } else { checker->Relation()->SetNode(expr->consequent_); @@ -1224,7 +1225,7 @@ checker::Type *ETSAnalyzer::Check(ir::MemberExpression *expr) const return expr->TsType(); } - auto *const left_type = expr->Object()->Check(checker); + auto *const left_type = checker->GetApparentType(expr->Object()->Check(checker)); if (expr->Kind() == ir::MemberExpressionKind::ELEMENT_ACCESS) { if (expr->IsOptional() && !left_type->IsNullish()) { @@ -1244,37 +1245,37 @@ checker::Type *ETSAnalyzer::Check(ir::MemberExpression *expr) const } if (expr->IsComputed()) { - return expr->AdjustOptional(checker, expr->CheckComputed(checker, base_type)); + return expr->AdjustType(checker, expr->CheckComputed(checker, base_type)); } if (base_type->IsETSArrayType() && expr->Property()->AsIdentifier()->Name().Is("length")) { - return expr->AdjustOptional(checker, checker->GlobalIntType()); + return expr->AdjustType(checker, checker->GlobalIntType()); } if (base_type->IsETSObjectType()) { expr->SetObjectType(base_type->AsETSObjectType()); auto [res_type, res_var] = expr->ResolveObjectMember(checker); expr->SetPropVar(res_var); - return expr->AdjustOptional(checker, res_type); + return expr->AdjustType(checker, res_type); } if (base_type->IsETSEnumType() || base_type->IsETSStringEnumType()) { auto [member_type, member_var] = expr->ResolveEnumMember(checker, base_type); expr->SetPropVar(member_var); - return expr->AdjustOptional(checker, member_type); + return expr->AdjustType(checker, member_type); } if (base_type->IsETSUnionType()) { - return expr->AdjustOptional(checker, expr->CheckUnionMember(checker, base_type)); + return expr->AdjustType(checker, expr->CheckUnionMember(checker, base_type)); } if (base_type->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { checker->Relation()->SetNode(expr); expr->SetObjectType(checker->PrimitiveTypeAsETSBuiltinType(base_type)->AsETSObjectType()); - checker->AddBoxingUnboxingFlagToNode(expr, expr->ObjType()); + checker->AddBoxingUnboxingFlagsToNode(expr, expr->ObjType()); auto [res_type, res_var] = expr->ResolveObjectMember(checker); expr->SetPropVar(res_var); - return expr->AdjustOptional(checker, res_type); + return expr->AdjustType(checker, res_type); } checker->ThrowTypeError({"Cannot access property of non-object or non-enum type"}, expr->Object()->Start()); @@ -1326,7 +1327,7 @@ checker::Type *ETSAnalyzer::Check(ir::ObjectExpression *expr) const for (checker::Signature *sig : obj_type->ConstructSignatures()) { if (sig->Params().empty()) { have_empty_constructor = true; - checker->ValidateSignatureAccessibility(obj_type, sig, expr->Start()); + checker->ValidateSignatureAccessibility(obj_type, nullptr, sig, expr->Start()); break; } } @@ -1564,7 +1565,7 @@ checker::Type *ETSAnalyzer::Check(ir::UnaryExpression *expr) const if (arg_type->IsETSObjectType() && (unboxed_operand_type != nullptr) && unboxed_operand_type->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { - expr->Argument()->AddBoxingUnboxingFlag(checker->GetUnboxingFlag(unboxed_operand_type)); + expr->Argument()->AddBoxingUnboxingFlags(checker->GetUnboxingFlag(unboxed_operand_type)); } return expr->TsType(); @@ -1599,8 +1600,8 @@ checker::Type *ETSAnalyzer::Check(ir::UpdateExpression *expr) const } if (operand_type->IsETSObjectType()) { - expr->Argument()->AddBoxingUnboxingFlag(checker->GetUnboxingFlag(unboxed_type) | - checker->GetBoxingFlag(unboxed_type)); + expr->Argument()->AddBoxingUnboxingFlags(checker->GetUnboxingFlag(unboxed_type) | + checker->GetBoxingFlag(unboxed_type)); } expr->SetTsType(operand_type); @@ -2061,7 +2062,9 @@ void InferReturnType(ETSChecker *checker, ir::ScriptFunction *containing_func, c ir::Expression *st_argument) { // First (or single) return statement in the function: - func_return_type = st_argument == nullptr ? checker->GlobalBuiltinVoidType() : st_argument->Check(checker); + func_return_type = st_argument == nullptr ? containing_func->IsEntryPoint() ? checker->GlobalVoidType() + : checker->GlobalBuiltinVoidType() + : st_argument->Check(checker); if (func_return_type->HasTypeFlag(checker::TypeFlag::CONSTANT)) { // remove CONSTANT type modifier if exists func_return_type = @@ -2171,6 +2174,12 @@ checker::Type *ETSAnalyzer::Check(ir::ReturnStatement *st) const if (auto *const return_type_annotation = containing_func->ReturnTypeAnnotation(); return_type_annotation != nullptr) { + if (return_type_annotation->IsTSThisType() && + (st->Argument() == nullptr || !st->Argument()->IsThisExpression())) { + checker->ThrowTypeError( + "The only allowed return value is 'this' if the method's return type is the 'this' type", st->Start()); + } + // Case when function's return type is defined explicitly: func_return_type = checker->GetTypeFromTypeAnnotation(return_type_annotation); @@ -2397,6 +2406,14 @@ checker::Type *ETSAnalyzer::Check(ir::TSAsExpression *expr) const auto *const source_type = expr->Expr()->Check(checker); + if (target_type->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE) && + source_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::TYPE_PARAMETER)) { + auto *const boxed_target_type = checker->PrimitiveTypeAsETSBuiltinType(target_type); + if (!checker->Relation()->IsIdenticalTo(source_type, boxed_target_type)) { + expr->Expr()->AddAstNodeFlags(ir::AstNodeFlags::CHECKCAST); + } + } + const checker::CastingContext ctx(checker->Relation(), expr->Expr(), source_type, target_type, expr->Expr()->Start(), {"Cannot cast type '", source_type, "' to '", target_type, "'"}); @@ -2579,7 +2596,7 @@ checker::Type *ETSAnalyzer::Check(ir::TSNonNullExpression *expr) const ETSChecker *checker = GetETSChecker(); auto expr_type = expr->expr_->Check(checker); - if (!expr_type->IsNullish()) { + if (!checker->MayHaveNulllikeValue(expr_type)) { checker->ThrowTypeError("Bad operand type, the operand of the non-null expression must be a nullable type", expr->Expr()->Start()); } @@ -2647,7 +2664,31 @@ checker::Type *ETSAnalyzer::Check([[maybe_unused]] ir::TSTupleType *node) const checker::Type *ETSAnalyzer::Check(ir::TSTypeAliasDeclaration *st) const { ETSChecker *checker = GetETSChecker(); + if (st->TypeParams() != nullptr) { + for (auto *const param : st->TypeParams()->Params()) { + const auto *const res = st->TypeAnnotation()->FindChild([¶m](const ir::AstNode *const node) { + if (!node->IsIdentifier()) { + return false; + } + + return param->Name()->AsIdentifier()->Variable() == node->AsIdentifier()->Variable(); + }); + + if (res == nullptr) { + checker->ThrowTypeError( + {"Type alias generic parameter '", param->Name()->Name(), "' is not used in type annotation"}, + param->Start()); + } + + checker->SetUpParameterType(param); + } + } + + const checker::SavedTypeRelationFlagsContext saved_flags_ctx(checker->Relation(), + checker::TypeRelationFlag::NO_THROW_GENERIC_TYPEALIAS); + st->TypeAnnotation()->Check(checker); + return nullptr; } diff --git a/ets2panda/checker/ETSchecker.cpp b/ets2panda/checker/ETSchecker.cpp index 4144da1ef35b883dced383245d7f619cea334c73..86acb390afd3a387444ff48c3de13c72fb23780c 100644 --- a/ets2panda/checker/ETSchecker.cpp +++ b/ets2panda/checker/ETSchecker.cpp @@ -55,20 +55,25 @@ void ETSChecker::InitializeBuiltins(varbinder::ETSBinder *varbinder) } if (var->HasFlag(varbinder::VariableFlags::BUILTIN_TYPE)) { - Type *type {nullptr}; - if (var->Declaration()->Node()->IsClassDefinition()) { - type = BuildClassProperties(var->Declaration()->Node()->AsClassDefinition()); - } else { - ASSERT(var->Declaration()->Node()->IsTSInterfaceDeclaration()); - type = BuildInterfaceProperties(var->Declaration()->Node()->AsTSInterfaceDeclaration()); - } - GetGlobalTypesHolder()->InitializeBuiltin(name, type); + InitializeBuiltin(var, name); } } AddStatus(CheckerStatus::BUILTINS_INITIALIZED); } +void ETSChecker::InitializeBuiltin(varbinder::Variable *var, const util::StringView &name) +{ + Type *type {nullptr}; + if (var->Declaration()->Node()->IsClassDefinition()) { + type = BuildClassProperties(var->Declaration()->Node()->AsClassDefinition()); + } else { + ASSERT(var->Declaration()->Node()->IsTSInterfaceDeclaration()); + type = BuildInterfaceProperties(var->Declaration()->Node()->AsTSInterfaceDeclaration()); + } + GetGlobalTypesHolder()->InitializeBuiltin(name, type); +} + bool ETSChecker::StartChecker([[maybe_unused]] varbinder::VarBinder *varbinder, const CompilerOptions &options) { Initialize(varbinder); @@ -231,6 +236,11 @@ ETSObjectType *ETSChecker::GlobalETSObjectType() const return AsETSObjectType(&GlobalTypesHolder::GlobalETSObjectType); } +ETSObjectType *ETSChecker::GlobalETSNullishObjectType() const +{ + return AsETSObjectType(&GlobalTypesHolder::GlobalETSNullishObjectType); +} + ETSObjectType *ETSChecker::GlobalBuiltinETSStringType() const { return AsETSObjectType(&GlobalTypesHolder::GlobalETSStringBuiltinType); diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 9ed89b1c81af72a456d66b9069bef2c019f8e6d9..2aa936c2066f17af700e21828a9cc4aeb7f73a8c 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -101,6 +101,7 @@ public: Type *GlobalWildcardType() const; ETSObjectType *GlobalETSObjectType() const; + ETSObjectType *GlobalETSNullishObjectType() const; ETSObjectType *GlobalBuiltinETSStringType() const; ETSObjectType *GlobalBuiltinTypeType() const; ETSObjectType *GlobalBuiltinExceptionType() const; @@ -120,10 +121,14 @@ public: const GlobalArraySignatureMap &GlobalArrayTypes() const; void InitializeBuiltins(varbinder::ETSBinder *varbinder); + void InitializeBuiltin(varbinder::Variable *var, const util::StringView &name); bool StartChecker([[maybe_unused]] varbinder::VarBinder *varbinder, const CompilerOptions &options) override; Type *CheckTypeCached(ir::Expression *expr) override; void ResolveStructuredTypeMembers([[maybe_unused]] Type *type) override {} Type *GetTypeOfVariable([[maybe_unused]] varbinder::Variable *var) override; + Type *GuaranteedTypeForUncheckedCast(Type *base, Type *substituted); + Type *GuaranteedTypeForUncheckedCallReturn(Signature *sig); + Type *GuaranteedTypeForUncheckedPropertyAccess(varbinder::Variable *prop); bool IsETSChecker() override { return true; @@ -145,7 +150,7 @@ public: void ValidateTupleIndex(const ETSTupleType *tuple, const ir::MemberExpression *expr); ETSObjectType *CheckThisOrSuperAccess(ir::Expression *node, ETSObjectType *class_type, std::string_view msg); void CreateTypeForClassOrInterfaceTypeParameters(ETSObjectType *type); - void SetTypeParameterType(ir::TSTypeParameter *type_param, Type *type_param_type); + ETSTypeParameter *SetUpParameterType(ir::TSTypeParameter *param); void ValidateOverriding(ETSObjectType *class_type, const lexer::SourcePosition &pos); void AddImplementedSignature(std::vector *implemented_signatures, varbinder::LocalVariable *function, ETSFunctionType *it); @@ -170,6 +175,8 @@ public: void CheckGetterSetterProperties(ETSObjectType *class_type); void AddElementsToModuleObject(ETSObjectType *module_obj, const util::StringView &str); Type *FindLeastUpperBound(Type *source, Type *target); + static Type *GetApparentType(Type *type); + static Type const *GetApparentType(Type const *type); Type *GetCommonClass(Type *source, Type *target); ETSObjectType *GetClosestCommonAncestor(ETSObjectType *source, ETSObjectType *target); ETSObjectType *GetTypeargumentedLUB(ETSObjectType *source, ETSObjectType *target); @@ -193,7 +200,7 @@ public: ETSFunctionType *CreateETSFunctionType(ArenaVector &signatures); ETSExtensionFuncHelperType *CreateETSExtensionFuncHelperType(ETSFunctionType *class_method_type, ETSFunctionType *extension_function_type); - ETSTypeParameter *CreateTypeParameter(Type *assembler_type); + ETSTypeParameter *CreateTypeParameter(); ETSObjectType *CreateETSObjectType(util::StringView name, ir::AstNode *decl_node, ETSObjectFlags flags); ETSEnumType *CreateETSEnumType(ir::TSEnumDeclaration const *enum_decl); ETSStringEnumType *CreateETSStringEnumType(ir::TSEnumDeclaration const *enum_decl); @@ -263,7 +270,7 @@ public: bool TypeInference(Signature *signature, const ArenaVector &arguments, TypeRelationFlag flags = TypeRelationFlag::NONE); bool CheckLambdaAssignable(ir::Expression *param, ir::ScriptFunction *lambda); - bool IsCompatibleTypeArgument(Type *type_param, Type *type_argument, const Substitution *substitution); + bool IsCompatibleTypeArgument(ETSTypeParameter *type_param, Type *type_argument, const Substitution *substitution); Substitution *NewSubstitution() { return Allocator()->New(Allocator()->Adapter()); @@ -272,13 +279,21 @@ public: { return Allocator()->New(*src); } - ArenaUnorderedSet *NewInstantiatedTypeParamsSet() + static void EmplaceSubstituted(Substitution *substitution, ETSTypeParameter *tparam, Type *type_arg); + ArenaUnorderedSet *NewInstantiatedTypeParamsSet() { - return Allocator()->New>(Allocator()->Adapter()); + return Allocator()->New>(Allocator()->Adapter()); } - void EnhanceSubstitutionForType(const ArenaVector &type_params, Type *param_type, Type *argument_type, - Substitution *substitution, - ArenaUnorderedSet *instantiated_type_params); + [[nodiscard]] bool EnhanceSubstitutionForType(const ArenaVector &type_params, Type *param_type, + Type *argument_type, Substitution *substitution, + ArenaUnorderedSet *instantiated_type_params); + [[nodiscard]] bool EnhanceSubstitutionForObject(const ArenaVector &type_params, ETSObjectType *param_type, + Type *argument_type, Substitution *substitution, + ArenaUnorderedSet *instantiated_type_params); + Signature *ValidateParameterlessConstructor(Signature *signature, const lexer::SourcePosition &pos, + TypeRelationFlag flags); + Signature *CollectParameterlessConstructor(ArenaVector &signatures, const lexer::SourcePosition &pos, + TypeRelationFlag resolve_flags = TypeRelationFlag::NONE); Signature *ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *type_arguments, const ArenaVector &arguments, const lexer::SourcePosition &pos, TypeRelationFlag initial_flags, const std::vector &arg_type_inference_required); @@ -326,7 +341,8 @@ public: [[nodiscard]] bool IsReturnTypeSubstitutable(Signature *s1, Signature *s2); void CheckStaticHide(Signature *target, Signature *source); void CheckThrowMarkers(Signature *source, Signature *target); - void ValidateSignatureAccessibility(ETSObjectType *callee, Signature *signature, const lexer::SourcePosition &pos, + void ValidateSignatureAccessibility(ETSObjectType *callee, const ir::CallExpression *call_expr, + Signature *signature, const lexer::SourcePosition &pos, char const *error_message = nullptr); void CreateLambdaObjectForLambdaReference(ir::ArrowFunctionExpression *lambda, ETSObjectType *functional_interface); ir::ClassProperty *CreateLambdaCapturedField(const varbinder::Variable *captured_var, varbinder::ClassScope *scope, @@ -406,6 +422,9 @@ public: Type *GetTypeFromInterfaceReference(varbinder::Variable *var); Type *GetTypeFromTypeAliasReference(varbinder::Variable *var); Type *GetTypeFromClassReference(varbinder::Variable *var); + void ValidateGenericTypeAliasForClonedNode(ir::TSTypeAliasDeclaration *type_alias_node, + const ir::TSTypeParameterInstantiation *exact_type_params); + Type *HandleTypeAlias(ir::Expression *name, const ir::TSTypeParameterInstantiation *type_params); Type *GetTypeFromEnumReference(varbinder::Variable *var); Type *GetTypeFromTypeParameterReference(varbinder::LocalVariable *var, const lexer::SourcePosition &pos); Type *GetNonConstantTypeFromPrimitiveType(Type *type); @@ -431,6 +450,9 @@ public: Type *CreateOptionalResultType(Type *type); Type *GetNonNullishType(Type *type) const; const Type *GetNonNullishType(const Type *type) const; + bool MayHaveNullValue(const Type *type) const; + bool MayHaveUndefinedValue(const Type *type) const; + bool MayHaveNulllikeValue(const Type *type) const; void ConcatConstantString(util::UString &target, Type *type); Type *HandleStringConcatenation(Type *left_type, Type *right_type); Type *ResolveIdentifier(ir::Identifier *ident); @@ -443,7 +465,7 @@ public: bool IsFunctionContainsSignature(ETSFunctionType *func_type, Signature *signature); void CheckFunctionContainsClashingSignature(const ETSFunctionType *func_type, Signature *signature); bool IsTypeBuiltinType(Type *type); - bool IsReferenceType(const Type *type); + static bool IsReferenceType(const Type *type); const ir::AstNode *FindJumpTarget(ir::AstNodeType node_type, const ir::AstNode *node, const ir::Identifier *target); void ValidatePropertyAccess(varbinder::Variable *var, ETSObjectType *obj, const lexer::SourcePosition &pos); varbinder::VariableFlags GetAccessFlagFromNode(const ir::AstNode *node); @@ -451,7 +473,7 @@ public: Type *ETSBuiltinTypeAsPrimitiveType(Type *object_type); Type *ETSBuiltinTypeAsConditionalType(Type *object_type); Type *PrimitiveTypeAsETSBuiltinType(Type *object_type); - void AddBoxingUnboxingFlagToNode(ir::AstNode *node, Type *boxing_unboxing_type); + void AddBoxingUnboxingFlagsToNode(ir::AstNode *node, Type *boxing_unboxing_type); ir::BoxingUnboxingFlags GetBoxingFlag(Type *boxing_type); ir::BoxingUnboxingFlags GetUnboxingFlag(Type const *unboxing_type) const; Type *MaybeBoxedType(const varbinder::Variable *var, ArenaAllocator *allocator) const; @@ -470,8 +492,8 @@ public: util::StringView name, const ETSObjectType *class_type); varbinder::Variable *FindVariableInGlobal(const ir::Identifier *identifier); void ValidateResolvedIdentifier(ir::Identifier *ident, varbinder::Variable *resolved); - bool IsVariableStatic(const varbinder::Variable *var) const; - bool IsVariableGetterSetter(const varbinder::Variable *var) const; + static bool IsVariableStatic(const varbinder::Variable *var); + static bool IsVariableGetterSetter(const varbinder::Variable *var); bool IsSameDeclarationType(varbinder::LocalVariable *target, varbinder::LocalVariable *compare); void SaveCapturedVariable(varbinder::Variable *var, const lexer::SourcePosition &pos); void AddBoxingFlagToPrimitiveType(TypeRelation *relation, Type *target); @@ -487,8 +509,8 @@ public: bool CheckRethrowingParams(const ir::AstNode *ancestor_function, const ir::AstNode *node); void CheckThrowingStatements(ir::AstNode *node); bool CheckThrowingPlacement(ir::AstNode *node, const ir::AstNode *ancestor_function); - void CheckNumberOfTypeArguments(Type *type, ir::TSTypeParameterDeclaration *type_param_decl, - ir::TSTypeParameterInstantiation *type_args, const lexer::SourcePosition &pos); + void CheckNumberOfTypeArguments(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args, + const lexer::SourcePosition &pos); ir::BlockStatement *FindFinalizerOfTryStatement(ir::AstNode *start_from, const ir::AstNode *p); void CheckRethrowingFunction(ir::ScriptFunction *func); ETSObjectType *GetRelevantArgumentedTypeFromChild(ETSObjectType *child, ETSObjectType *target); @@ -615,13 +637,10 @@ private: ArenaVector CreateTypeForTypeParameters(ir::TSTypeParameterDeclaration *type_params); - Type *CreateTypeParameterType(ir::TSTypeParameter *param); - using Type2TypeMap = std::unordered_map; void CheckTypeParameterConstraint(ir::TSTypeParameter *param, Type2TypeMap &extends); void SetUpTypeParameterConstraint(ir::TSTypeParameter *param); - ETSObjectType *SetUpParameterType(ir::TSTypeParameter *param); ETSObjectType *CreateETSObjectTypeCheckBuiltins(util::StringView name, ir::AstNode *decl_node, ETSObjectFlags flags); void CheckProgram(parser::Program *program, bool run_analysis = false); @@ -645,7 +664,6 @@ private: ArenaVector &signatures, const ir::TSTypeParameterInstantiation *type_arguments, const ArenaVector &arguments, std::vector &arg_type_inference_required, const lexer::SourcePosition &pos, TypeRelationFlag resolve_flags); - // Trailing lambda void MoveTrailingBlockToEnclosingBlockStatement(ir::CallExpression *call_expr); void TransformTraillingLambda(ir::CallExpression *call_expr); diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index 113250d258901f0a1346bad7a331f83ec031e798..fe8d0cd47345b53fc79081591dd2bac96cd0c6d2 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -381,11 +381,9 @@ std::tuple ETSChecker::CheckBinaryOperatorEqualDynamic(ir::Expre // NOTE: vpukhov. boxing flags are not set in dynamic values return {GlobalETSBooleanType(), other_exp->TsType()}; } - if (other_exp->TsType()->IsETSObjectType()) { + if (IsReferenceType(other_exp->TsType())) { // have to prevent casting dyn_exp via ApplyCast without nullish flag - auto *nullish_obj = CreateNullishType(GlobalETSObjectType(), checker::TypeFlag::NULLISH, Allocator(), - Relation(), GetGlobalTypesHolder()); - return {GlobalETSBooleanType(), nullish_obj}; + return {GlobalETSBooleanType(), GlobalETSNullishObjectType()}; } ThrowTypeError("Unimplemented case in dynamic type comparison.", pos); } @@ -436,8 +434,7 @@ std::tuple ETSChecker::CheckBinaryOperatorInstanceOf(lexer::Sour checker::Type *const right_type) { checker::Type *ts_type {}; - 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)) { + if (!IsReferenceType(left_type) || !IsReferenceType(right_type)) { ThrowTypeError("Bad operand type, the types of the operands must be same type.", pos); } @@ -473,7 +470,7 @@ Type *ETSChecker::CheckBinaryOperatorNullishCoalescing(ir::Expression *right, le if (boxed_right_type == nullptr) { ThrowTypeError("Invalid right-hand side expression", pos); } - right->AddBoxingUnboxingFlag(GetBoxingFlag(boxed_right_type)); + right->AddBoxingUnboxingFlags(GetBoxingFlag(boxed_right_type)); return FindLeastUpperBound(non_nullish_left_type, boxed_right_type); } @@ -609,7 +606,7 @@ Type *ETSChecker::HandleArithmeticOperationOnTypes(Type *left, Type *right, lexe void ETSChecker::FlagExpressionWithUnboxing(Type *type, Type *unboxed_type, ir::Expression *type_expression) { if (type->IsETSObjectType() && (unboxed_type != nullptr)) { - type_expression->AddBoxingUnboxingFlag(GetUnboxingFlag(unboxed_type)); + type_expression->AddBoxingUnboxingFlags(GetUnboxingFlag(unboxed_type)); } } diff --git a/ets2panda/checker/ets/boxingConverter.h b/ets2panda/checker/ets/boxingConverter.h index 8c142419c6e028eef81219c6cf2b3fe6984ea08a..14ce94e657fde151c75363764af191cac8a1b979 100644 --- a/ets2panda/checker/ets/boxingConverter.h +++ b/ets2panda/checker/ets/boxingConverter.h @@ -25,8 +25,6 @@ public: BoxingConverter(ETSChecker *checker, TypeRelation *relation, Type *source) : TypeConverter(checker, relation, nullptr, source) { - ASSERT(relation->GetNode()); - if (!source->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { Relation()->Result(false); return; @@ -39,8 +37,6 @@ public: BoxingConverter(ETSChecker *checker, TypeRelation *relation, Type *source, Type *target) : TypeConverter(checker, relation, target, source) { - ASSERT(relation->GetNode()); - if (!target->IsETSObjectType() || relation->IsTrue()) { return; } diff --git a/ets2panda/checker/ets/conversion.cpp b/ets2panda/checker/ets/conversion.cpp index b1dd99217bac91f0c5d581a7170680ef880c8ad6..226eb0a57150834eac380649a2e24ebca9c5a00c 100644 --- a/ets2panda/checker/ets/conversion.cpp +++ b/ets2panda/checker/ets/conversion.cpp @@ -255,7 +255,7 @@ ETSObjectType *Boxing(TypeRelation *const relation, Type *const source) return nullptr; } auto *const boxed_type = boxed.Result()->AsETSObjectType(); - relation->GetNode()->AddBoxingUnboxingFlag(ets_checker->GetBoxingFlag(boxed_type)); + relation->GetNode()->AddBoxingUnboxingFlags(ets_checker->GetBoxingFlag(boxed_type)); return boxed_type; } @@ -267,7 +267,7 @@ Type *Unboxing(TypeRelation *const relation, ETSObjectType *const source) return nullptr; } auto *const unboxed_type = unboxed.Result(); - relation->GetNode()->AddBoxingUnboxingFlag(ets_checker->GetUnboxingFlag(unboxed_type)); + relation->GetNode()->AddBoxingUnboxingFlags(ets_checker->GetUnboxingFlag(unboxed_type)); return unboxed_type; } @@ -282,6 +282,26 @@ void UnboxingWideningPrimitive(TypeRelation *const relation, ETSObjectType *cons RollbackBoxingIfFailed(relation); } +void UnboxingNarrowingPrimitive(TypeRelation *const relation, ETSObjectType *const source, Type *const target) +{ + auto *const unboxed_source = Unboxing(relation, source); + if (!relation->IsTrue()) { + return; + } + ASSERT(unboxed_source != nullptr); + NarrowingPrimitive(relation, target, unboxed_source); +} + +void UnboxingWideningNarrowingPrimitive(TypeRelation *const relation, ETSObjectType *const source, Type *const target) +{ + auto *const unboxed_source = Unboxing(relation, source); + if (!relation->IsTrue()) { + return; + } + ASSERT(unboxed_source != nullptr); + WideningNarrowingPrimitive(relation, unboxed_source->AsByteType(), target->AsCharType()); +} + void NarrowingReferenceUnboxing(TypeRelation *const relation, ETSObjectType *const source, Type *const target) { auto *const boxed_target = relation->GetChecker()->AsETSChecker()->PrimitiveTypeAsETSBuiltinType(target); diff --git a/ets2panda/checker/ets/conversion.h b/ets2panda/checker/ets/conversion.h index f796162a35d514d53556f5df8d62687e8ab2a2a1..8f28cfa0fb67d81b6ae4704c20ec14a7864083d0 100644 --- a/ets2panda/checker/ets/conversion.h +++ b/ets2panda/checker/ets/conversion.h @@ -37,6 +37,8 @@ void NarrowingReference(TypeRelation *relation, ETSArrayType *source, ETSArrayTy ETSObjectType *Boxing(TypeRelation *relation, Type *source); Type *Unboxing(TypeRelation *relation, ETSObjectType *source); +void UnboxingWideningNarrowingPrimitive(TypeRelation *relation, ETSObjectType *source, Type *target); +void UnboxingNarrowingPrimitive(TypeRelation *relation, ETSObjectType *source, Type *target); void UnboxingWideningPrimitive(TypeRelation *relation, ETSObjectType *source, Type *target); void NarrowingReferenceUnboxing(TypeRelation *relation, ETSObjectType *source, Type *target); void BoxingWideningReference(TypeRelation *relation, Type *source, ETSObjectType *target); diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 4e1e35a33d2043702f3af0144b59ab30b07fe24f..df0aa92e85d8c581be096d85d9cb254d56e18f0c 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -67,93 +67,111 @@ namespace panda::es2panda::checker { -bool ETSChecker::IsCompatibleTypeArgument(Type *type_param, Type *type_argument, const Substitution *substitution) +bool ETSChecker::IsCompatibleTypeArgument(ETSTypeParameter *type_param, Type *type_argument, + const Substitution *substitution) { - ASSERT(type_param->IsETSObjectType() && - type_param->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::TYPE_PARAMETER)); if (type_argument->IsWildcardType()) { return true; } - if (!type_argument->IsETSObjectType() && !type_argument->IsETSArrayType() && !type_argument->IsETSFunctionType()) { + if (!type_argument->IsETSTypeParameter() && !IsReferenceType(type_argument)) { return false; } - auto *type_param_obj = type_param->AsETSObjectType(); - auto *type_param_obj_supertype = type_param_obj->SuperType(); - if (type_param_obj_supertype != nullptr) { - if (!type_param_obj_supertype->TypeArguments().empty()) { - type_param_obj_supertype = - type_param_obj_supertype->Substitute(this->Relation(), substitution)->AsETSObjectType(); - } - type_param_obj_supertype->IsSupertypeOf(Relation(), type_argument); - if (!Relation()->IsTrue()) { - return false; - } + if (type_argument->IsETSUnionType()) { + auto const &constitutent = type_argument->AsETSUnionType()->ConstituentTypes(); + return std::all_of(constitutent.begin(), constitutent.end(), [this, type_param, substitution](Type *type_arg) { + return IsCompatibleTypeArgument(type_param->AsETSTypeParameter(), type_arg, substitution); + }); } - for (auto *itf : type_param_obj->Interfaces()) { - if (!itf->TypeArguments().empty()) { - itf = itf->Substitute(this->Relation(), substitution)->AsETSObjectType(); - } - itf->IsSupertypeOf(Relation(), type_argument); + + if (auto *constraint = type_param->GetConstraintType(); constraint != nullptr) { + constraint = constraint->Substitute(Relation(), substitution); + constraint->IsSupertypeOf(Relation(), type_argument); if (!Relation()->IsTrue()) { return false; } } - return true; } /* A very rough and imprecise partial type inference */ -void ETSChecker::EnhanceSubstitutionForType(const ArenaVector &type_params, Type *param_type, +bool ETSChecker::EnhanceSubstitutionForType(const ArenaVector &type_params, Type *param_type, Type *argument_type, Substitution *substitution, - ArenaUnorderedSet *instantiated_type_params) -{ - if (!param_type->IsETSObjectType()) { - return; - } - auto *param_obj_type = param_type->AsETSObjectType(); - if (param_obj_type->HasObjectFlag(ETSObjectFlags::TYPE_PARAMETER)) { - auto *param_base = GetOriginalBaseType(param_obj_type); - if (instantiated_type_params->find(param_obj_type) != instantiated_type_params->end() && - substitution->at(param_base) != argument_type) { - ThrowTypeError({"Type parameter already instantiated with another type "}, - param_obj_type->GetDeclNode()->Start()); - } - if (std::find(type_params.begin(), type_params.end(), param_base) != type_params.end() && - substitution->count(param_base) == 0) { - substitution->emplace(param_base, argument_type); - instantiated_type_params->insert(param_obj_type); - return; + ArenaUnorderedSet *instantiated_type_params) +{ + if (param_type->IsETSTypeParameter()) { + auto *const tparam = param_type->AsETSTypeParameter(); + auto *const original_tparam = tparam->GetOriginal(); + if (instantiated_type_params->find(tparam) != instantiated_type_params->end() && + substitution->at(original_tparam) != argument_type) { + ThrowTypeError({"Type parameter already instantiated with another type "}, tparam->GetDeclNode()->Start()); + } + if (std::find(type_params.begin(), type_params.end(), original_tparam) != type_params.end() && + substitution->count(original_tparam) == 0) { + if (!IsCompatibleTypeArgument(tparam, argument_type, substitution)) { + return false; + } + ETSChecker::EmplaceSubstituted(substitution, original_tparam, argument_type); + instantiated_type_params->insert(tparam); + return true; } } + + if (param_type->IsETSUnionType()) { + auto const &constitutent = param_type->AsETSUnionType()->ConstituentTypes(); + return std::all_of(constitutent.begin(), constitutent.end(), + [this, type_params, argument_type, substitution, instantiated_type_params](Type *member) { + return EnhanceSubstitutionForType(type_params, member, argument_type, substitution, + instantiated_type_params); + }); + } + + if (param_type->IsETSObjectType()) { + return EnhanceSubstitutionForObject(type_params, param_type->AsETSObjectType(), argument_type, substitution, + instantiated_type_params); + } + return true; +} + +bool ETSChecker::EnhanceSubstitutionForObject(const ArenaVector &type_params, ETSObjectType *param_type, + Type *argument_type, Substitution *substitution, + ArenaUnorderedSet *instantiated_type_params) +{ + auto *param_obj_type = param_type->AsETSObjectType(); + + auto const enhance = [this, type_params, substitution, instantiated_type_params](Type *ptype, Type *atype) { + return EnhanceSubstitutionForType(type_params, ptype, atype, substitution, instantiated_type_params); + }; + if (argument_type->IsETSObjectType()) { auto *arg_obj_type = argument_type->AsETSObjectType(); if (GetOriginalBaseType(arg_obj_type) != GetOriginalBaseType(param_obj_type)) { - return; // don't attempt anything fancy for now + return true; // don't attempt anything fancy for now } - ASSERT(arg_obj_type->TypeArguments().size() == param_obj_type->TypeArguments().size()); + bool res = true; for (size_t ix = 0; ix < arg_obj_type->TypeArguments().size(); ix++) { - EnhanceSubstitutionForType(type_params, param_obj_type->TypeArguments()[ix], - arg_obj_type->TypeArguments()[ix], substitution, instantiated_type_params); - } - } else if (argument_type->IsETSFunctionType() && - param_obj_type->HasObjectFlag(ETSObjectFlags::FUNCTIONAL_INTERFACE)) { - auto parameter_signatures = param_obj_type->GetOwnProperty("invoke") - ->TsType() - ->AsETSFunctionType() - ->CallSignatures(); - auto argument_signatures = argument_type->AsETSFunctionType()->CallSignatures(); + res &= enhance(param_obj_type->TypeArguments()[ix], arg_obj_type->TypeArguments()[ix]); + } + return res; + } + + if (argument_type->IsETSFunctionType() && param_obj_type->HasObjectFlag(ETSObjectFlags::FUNCTIONAL_INTERFACE)) { + auto ¶meter_signatures = param_obj_type->GetOwnProperty("invoke") + ->TsType() + ->AsETSFunctionType() + ->CallSignatures(); + auto &argument_signatures = argument_type->AsETSFunctionType()->CallSignatures(); ASSERT(argument_signatures.size() == 1); ASSERT(parameter_signatures.size() == 1); + bool res = true; for (size_t idx = 0; idx < argument_signatures[0]->GetSignatureInfo()->params.size(); idx++) { - EnhanceSubstitutionForType(type_params, parameter_signatures[0]->GetSignatureInfo()->params[idx]->TsType(), - argument_signatures[0]->GetSignatureInfo()->params[idx]->TsType(), substitution, - instantiated_type_params); + res &= enhance(parameter_signatures[0]->GetSignatureInfo()->params[idx]->TsType(), + argument_signatures[0]->GetSignatureInfo()->params[idx]->TsType()); } - EnhanceSubstitutionForType(type_params, parameter_signatures[0]->ReturnType(), - argument_signatures[0]->ReturnType(), substitution, instantiated_type_params); - } else { - return; + res &= enhance(parameter_signatures[0]->ReturnType(), argument_signatures[0]->ReturnType()); + return res; } + + return true; } // NOLINTBEGIN(modernize-avoid-c-arrays) @@ -161,6 +179,20 @@ static constexpr char const INVALID_CALL_ARGUMENT_1[] = "Call argument at index static constexpr char const INVALID_CALL_ARGUMENT_2[] = " is not compatible with the signature's type at that index."; static constexpr char const INVALID_CALL_ARGUMENT_3[] = " is not compatible with the signature's rest parameter type."; // NOLINTEND(modernize-avoid-c-arrays) +Signature *ETSChecker::ValidateParameterlessConstructor(Signature *signature, const lexer::SourcePosition &pos, + TypeRelationFlag flags) +{ + std::size_t const parameter_count = signature->MinArgCount(); + auto const throw_error = (flags & TypeRelationFlag::NO_THROW) == 0; + + if (parameter_count != 0) { + if (throw_error) { + ThrowTypeError({"No Matching Parameterless Constructor, parameter count ", parameter_count}, pos); + } + return nullptr; + } + return signature; +} Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *type_arguments, const ArenaVector &arguments, @@ -190,11 +222,6 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP } } - if (substituted_sig->IsBaseReturnDiff() && substituted_sig->ReturnType()->IsETSArrayType()) { - CreateBuiltinArraySignature(substituted_sig->ReturnType()->AsETSArrayType(), - substituted_sig->ReturnType()->AsETSArrayType()->Rank()); - } - // Check all required formal parameter(s) first auto const count = std::min(parameter_count, argument_count); std::size_t index = 0U; @@ -304,6 +331,38 @@ bool ETSChecker::ValidateProxySignature(Signature *const signature, arg_type_inference_required) != nullptr; } +Signature *ETSChecker::CollectParameterlessConstructor(ArenaVector &signatures, + const lexer::SourcePosition &pos, TypeRelationFlag resolve_flags) +{ + Signature *compatible_signature = nullptr; + + auto collect_signatures = [&](TypeRelationFlag relation_flags) { + for (auto *sig : signatures) { + if (auto *concrete_sig = ValidateParameterlessConstructor(sig, pos, relation_flags); + concrete_sig != nullptr) { + compatible_signature = concrete_sig; + break; + } + } + }; + + // We are able to provide more specific error messages. + if (signatures.size() == 1) { + collect_signatures(resolve_flags); + } else { + collect_signatures(resolve_flags | TypeRelationFlag::NO_THROW); + } + + if (compatible_signature == nullptr) { + if ((resolve_flags & TypeRelationFlag::NO_THROW) == 0) { + ThrowTypeError({"No matching parameterless constructor"}, pos); + } else { + return nullptr; + } + } + return compatible_signature; +} + std::pair, ArenaVector> ETSChecker::CollectSignatures( ArenaVector &signatures, const ir::TSTypeParameterInstantiation *type_arguments, const ArenaVector &arguments, std::vector &arg_type_inference_required, @@ -321,12 +380,13 @@ std::pair, ArenaVector> ETSChecker::Collec auto collect_signatures = [&](TypeRelationFlag relation_flags) { for (auto *sig : signatures) { - if (!sig->Function()->IsDefaultParamProxy()) { - if (auto *concrete_sig = ValidateSignature(sig, type_arguments, arguments, pos, relation_flags, - arg_type_inference_required); - concrete_sig != nullptr) { - compatible_signatures.push_back(concrete_sig); - } + if (sig->Function()->IsDefaultParamProxy()) { + continue; + } + auto *concrete_sig = + ValidateSignature(sig, type_arguments, arguments, pos, relation_flags, arg_type_inference_required); + if (concrete_sig != nullptr) { + compatible_signatures.push_back(concrete_sig); } } }; @@ -418,6 +478,35 @@ Signature *ETSChecker::ValidateSignatures(ArenaVector &signatures, } } + if ((resolve_flags & TypeRelationFlag::NO_THROW) == 0 && !arguments.empty() && !signatures.empty()) { + std::stringstream ss; + + if (signatures[0]->Function()->IsConstructor()) { + ss << util::Helpers::GetClassDefiniton(signatures[0]->Function())->PrivateId().Mutf8(); + } else { + ss << signatures[0]->Function()->Id()->Name().Mutf8(); + } + + ss << "("; + + for (uint32_t index = 0; index < arguments.size(); ++index) { + if (arguments[index]->IsArrowFunctionExpression()) { + // NOTE(peterseres): Refactor this case and add test case + break; + } + + arguments[index]->Check(this); + arguments[index]->TsType()->ToString(ss); + + if (index == arguments.size() - 1) { + ss << ")"; + ThrowTypeError({"No matching ", signature_kind, " signature for ", ss.str().c_str()}, pos); + } + + ss << ", "; + } + } + if ((resolve_flags & TypeRelationFlag::NO_THROW) == 0) { ThrowTypeError({"No matching ", signature_kind, " signature"}, pos); } @@ -704,6 +793,10 @@ Signature *ETSChecker::ComposeSignature(ir::ScriptFunction *func, SignatureInfo signature->AddSignatureFlag(SignatureFlags::NEED_RETURN_TYPE); } + if (return_type_annotation != nullptr && return_type_annotation->IsTSThisType()) { + signature->AddSignatureFlag(SignatureFlags::THIS_RETURN_TYPE); + } + if (func->IsAbstract()) { signature->AddSignatureFlag(SignatureFlags::ABSTRACT); signature->AddSignatureFlag(SignatureFlags::VIRTUAL); @@ -987,9 +1080,7 @@ bool ETSChecker::IsMethodOverridesOther(Signature *target, Signature *source) return false; } - if (!source->Function()->IsOverride()) { - ThrowTypeError("Method overriding requires 'override' modifier", source->Function()->Start()); - } + source->Function()->SetOverride(); return true; } } @@ -1063,7 +1154,11 @@ Signature *ETSChecker::AdjustForTypeParameters(Signature *source, Signature *tar } auto *substitution = NewSubstitution(); for (size_t ix = 0; ix < source_type_params.size(); ix++) { - substitution->emplace(target_type_params[ix], source_type_params[ix]); + if (!target_type_params[ix]->IsETSTypeParameter()) { + continue; + } + ETSChecker::EmplaceSubstituted(substitution, target_type_params[ix]->AsETSTypeParameter(), + source_type_params[ix]); } return target->Substitute(Relation(), substitution); } @@ -1186,15 +1281,35 @@ Signature *ETSChecker::GetSignatureFromMethodDefinition(const ir::MethodDefiniti return nullptr; } -void ETSChecker::ValidateSignatureAccessibility(ETSObjectType *callee, Signature *signature, - const lexer::SourcePosition &pos, char const *error_message) +void ETSChecker::ValidateSignatureAccessibility(ETSObjectType *callee, const ir::CallExpression *call_expr, + Signature *signature, const lexer::SourcePosition &pos, + char const *error_message) { if ((Context().Status() & CheckerStatus::IGNORE_VISIBILITY) != 0U) { return; } if (signature->HasSignatureFlag(SignatureFlags::PRIVATE) || signature->HasSignatureFlag(SignatureFlags::PROTECTED)) { - ASSERT(callee->GetDeclNode() && callee->GetDeclNode()->IsClassDefinition()); + ASSERT(callee->GetDeclNode() && + (callee->GetDeclNode()->IsClassDefinition() || callee->GetDeclNode()->IsTSInterfaceDeclaration())); + + if (callee->GetDeclNode()->IsTSInterfaceDeclaration()) { + if (call_expr->Callee()->IsMemberExpression() && + call_expr->Callee()->AsMemberExpression()->Object()->IsThisExpression()) { + const auto *enclosing_func = + util::Helpers::FindAncestorGivenByType(call_expr, ir::AstNodeType::SCRIPT_FUNCTION) + ->AsScriptFunction(); + if (signature->Function()->IsPrivate() && !enclosing_func->IsPrivate()) { + ThrowTypeError({"Cannot reference 'this' in this context."}, enclosing_func->Start()); + } + } + + if (Context().ContainingClass() == callee->GetDeclNode()->AsTSInterfaceDeclaration()->TsType() && + callee->GetDeclNode()->AsTSInterfaceDeclaration()->TsType()->AsETSObjectType()->IsSignatureInherited( + signature)) { + return; + } + } if (Context().ContainingClass() == callee->GetDeclNode()->AsClassDefinition()->TsType() && callee->GetDeclNode()->AsClassDefinition()->TsType()->AsETSObjectType()->IsSignatureInherited(signature)) { return; @@ -2495,7 +2610,7 @@ bool ETSChecker::IsReturnTypeSubstitutable(Signature *const s1, Signature *const // - If R1 is a reference type then R1, adapted to the type parameters of d2 (link to generic methods), is a // subtype of R2. - ASSERT(r1->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT)); + ASSERT(r1->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT) || r1->IsETSTypeParameter()); r2->IsSupertypeOf(Relation(), r1); return Relation()->IsTrue(); } @@ -2661,7 +2776,7 @@ void ETSChecker::ReplaceScope(ir::AstNode *root, ir::AstNode *old_node, varbinde root->Iterate([this, old_node, new_scope](ir::AstNode *child) { auto *scope = NodeScope(child); if (scope != nullptr) { - while (scope->Parent()->Node() != old_node) { + while (scope->Parent() != nullptr && scope->Parent()->Node() != old_node) { scope = scope->Parent(); } scope->SetParent(new_scope); diff --git a/ets2panda/checker/ets/function_helpers.h b/ets2panda/checker/ets/function_helpers.h index a4b7e1d091fb53b079bf33bf55dfd1411c37e93b..dba8f772b84c221046eff443a72e3ee01f83254d 100644 --- a/ets2panda/checker/ets/function_helpers.h +++ b/ets2panda/checker/ets/function_helpers.h @@ -88,7 +88,7 @@ static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *che Substitution *substitution = checker->NewSubstitution(); auto *instantiated_type_params = checker->NewInstantiatedTypeParamsSet(); auto *sig_info = signature->GetSignatureInfo(); - ArenaVector &type_params = sig_info->type_params; + auto &type_params = sig_info->type_params; for (size_t ix = 0; ix < arguments.size(); ix++) { auto *arg = arguments[ix]; if (arg->IsObjectExpression()) { @@ -101,40 +101,51 @@ static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *che if (param_type == nullptr) { continue; } - checker->EnhanceSubstitutionForType(type_params, param_type, arg_type, substitution, instantiated_type_params); + if (!checker->EnhanceSubstitutionForType(type_params, param_type, arg_type, substitution, + instantiated_type_params)) { + return nullptr; + } } return substitution; } static const Substitution *BuildExplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature, - const ir::TSTypeParameterInstantiation *type_arguments, + const ArenaVector ¶ms, const lexer::SourcePosition &pos, TypeRelationFlag flags) { + auto &sig_params = signature->GetSignatureInfo()->type_params; auto *substitution = checker->NewSubstitution(); - auto *constraints_substitution = checker->NewSubstitution(); - ArenaVector &type_params = signature->GetSignatureInfo()->type_params; - ArenaVector type_arg_types {checker->Allocator()->Adapter()}; - for (auto *ta_expr : type_arguments->Params()) { - auto *type_arg = ta_expr->GetType(checker); - type_arg = MaybeBoxedType(checker, type_arg, ta_expr); - type_arg_types.push_back(type_arg); - } - if (type_params.size() != type_arg_types.size()) { - if ((flags & TypeRelationFlag::NO_THROW) == 0) { - checker->ThrowTypeError( - {"Expected ", type_params.size(), " type arguments, got ", type_arg_types.size(), " ."}, pos); + ArenaVector inst_args {checker->Allocator()->Adapter()}; + + for (auto *ta_expr : params) { + inst_args.push_back(MaybeBoxedType(checker, ta_expr->GetType(checker), ta_expr)); + } + for (size_t ix = inst_args.size(); ix < sig_params.size(); ++ix) { + auto *dflt = sig_params[ix]->AsETSTypeParameter()->GetDefaultType(); + if (dflt == nullptr) { + break; } - return nullptr; + inst_args.push_back(dflt); } - for (size_t ix = 0; ix < type_params.size(); ix++) { - constraints_substitution->emplace(type_params[ix], type_arg_types[ix]); + if (sig_params.size() != inst_args.size()) { + if ((flags & TypeRelationFlag::NO_THROW) != 0) { + return nullptr; + } + checker->ThrowTypeError({"Expected ", sig_params.size(), " type arguments, got ", inst_args.size(), " ."}, pos); + } + + auto *constraints_substitution = checker->NewSubstitution(); + + for (size_t ix = 0; ix < sig_params.size(); ix++) { + ETSChecker::EmplaceSubstituted(constraints_substitution, sig_params[ix]->AsETSTypeParameter(), inst_args[ix]); } - for (size_t ix = 0; ix < type_arg_types.size(); ix++) { - if (!checker->IsCompatibleTypeArgument(type_params[ix], type_arg_types[ix], constraints_substitution)) { + for (size_t ix = 0; ix < sig_params.size(); ix++) { + if (!checker->IsCompatibleTypeArgument(sig_params[ix]->AsETSTypeParameter(), inst_args[ix], + constraints_substitution)) { return nullptr; } - substitution->emplace(type_params[ix], type_arg_types[ix]); + ETSChecker::EmplaceSubstituted(substitution, sig_params[ix]->AsETSTypeParameter(), inst_args[ix]); } return substitution; } @@ -147,9 +158,10 @@ static Signature *MaybeSubstituteTypeParameters(ETSChecker *checker, Signature * if (type_arguments == nullptr && signature->GetSignatureInfo()->type_params.empty()) { return signature; } + const Substitution *substitution = (type_arguments != nullptr) - ? BuildExplicitSubstitutionForArguments(checker, signature, type_arguments, pos, flags) + ? BuildExplicitSubstitutionForArguments(checker, signature, type_arguments->Params(), pos, flags) : BuildImplicitSubstitutionForArguments(checker, signature, arguments); return (substitution == nullptr) ? nullptr : signature->Substitute(checker->Relation(), substitution); } diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 04df469225ede284785a409f48ccc2d3b3ee36f4..c1ee8c407b7aafc40c16e817f148ed7a9cdc8099 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -77,11 +77,15 @@ void ETSChecker::CheckTruthinessOfType(ir::Expression *expr) checker::Type *type = expr->Check(this); auto *unboxed_type = ETSBuiltinTypeAsConditionalType(type); + if (unboxed_type == nullptr) { + ThrowTypeError("Condition must be of possible condition type", expr->Start()); + } + if (unboxed_type == GlobalBuiltinVoidType() || unboxed_type->IsETSVoidType()) { ThrowTypeError("An expression of type 'void' cannot be tested for truthiness", expr->Start()); } - if (unboxed_type != nullptr && !unboxed_type->IsConditionalExprType()) { + if (!unboxed_type->IsConditionalExprType()) { ThrowTypeError("Condition must be of possible condition type", expr->Start()); } @@ -126,6 +130,9 @@ Type *ETSChecker::GetNonNullishType(Type *type) const if (type->IsETSArrayType()) { return type; // give up } + if (type->IsETSTypeParameter()) { + return type->AsETSTypeParameter()->GetOriginal(); + } while (type->IsNullish()) { type = type->AsETSObjectType()->GetBaseType(); @@ -140,6 +147,9 @@ const Type *ETSChecker::GetNonNullishType(const Type *type) const if (type->IsETSArrayType()) { return type; // give up } + if (type->IsETSTypeParameter()) { + return type->AsETSTypeParameter()->GetOriginal(); + } while (type->IsNullish()) { type = type->AsETSObjectType()->GetBaseType(); @@ -153,12 +163,45 @@ Type *ETSChecker::CreateOptionalResultType(Type *type) if (type->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { type = PrimitiveTypeAsETSBuiltinType(type); ASSERT(type->IsETSObjectType()); - Relation()->GetNode()->AddBoxingUnboxingFlag(GetBoxingFlag(type)); + Relation()->GetNode()->AddBoxingUnboxingFlags(GetBoxingFlag(type)); } return CreateNullishType(type, checker::TypeFlag::UNDEFINED, Allocator(), Relation(), GetGlobalTypesHolder()); } +bool ETSChecker::MayHaveNullValue(const Type *type) const +{ + if (type->ContainsNull() || type->IsETSNullType()) { + return true; + } + if (type->IsETSTypeParameter()) { + return MayHaveNullValue(type->AsETSTypeParameter()->EffectiveConstraint(this)); + } + return false; +} + +bool ETSChecker::MayHaveUndefinedValue(const Type *type) const +{ + if (type->ContainsUndefined() || type->IsETSUndefinedType()) { + return true; + } + if (type->IsETSTypeParameter()) { + return MayHaveUndefinedValue(type->AsETSTypeParameter()->EffectiveConstraint(this)); + } + return false; +} + +bool ETSChecker::MayHaveNulllikeValue(const Type *type) const +{ + if (type->IsNullishOrNullLike()) { + return true; + } + if (type->IsETSTypeParameter()) { + return MayHaveNulllikeValue(type->AsETSTypeParameter()->EffectiveConstraint(this)); + } + return false; +} + bool ETSChecker::IsConstantExpression(ir::Expression *expr, Type *type) { return (type->HasTypeFlag(TypeFlag::CONSTANT) && (expr->IsIdentifier() || expr->IsMemberExpression())); @@ -305,6 +348,55 @@ Type *ETSChecker::GetTypeOfVariable(varbinder::Variable *const var) return var->TsType(); } +// Determine if unchecked cast is needed and yield guaranteed source type +Type *ETSChecker::GuaranteedTypeForUncheckedCast(Type *base, Type *substituted) +{ + if (!base->IsETSTypeParameter()) { + return nullptr; + } + auto *constr = base->AsETSTypeParameter()->EffectiveConstraint(this); + // Constraint is supertype of TypeArg AND TypeArg is supertype of Constraint + return Relation()->IsIdenticalTo(substituted, constr) ? nullptr : constr; +} + +// Determine if substituted property access requires cast from erased type +Type *ETSChecker::GuaranteedTypeForUncheckedPropertyAccess(varbinder::Variable *const prop) +{ + if (IsVariableStatic(prop)) { + return nullptr; + } + if (IsVariableGetterSetter(prop)) { + auto *method = prop->TsType()->AsETSFunctionType(); + if (!method->HasTypeFlag(checker::TypeFlag::GETTER)) { + return nullptr; + } + return GuaranteedTypeForUncheckedCallReturn(method->FindGetter()); + } + // NOTE(vpukhov): mark ETSDynamicType properties + if (prop->Declaration() == nullptr || prop->Declaration()->Node() == nullptr) { + return nullptr; + } + + auto *base_prop = prop->Declaration()->Node()->AsClassProperty()->Id()->Variable(); + if (base_prop == prop) { + return nullptr; + } + return GuaranteedTypeForUncheckedCast(GetTypeOfVariable(base_prop), GetTypeOfVariable(prop)); +} + +// Determine if substituted method cast requires cast from erased type +Type *ETSChecker::GuaranteedTypeForUncheckedCallReturn(Signature *sig) +{ + if (sig->HasSignatureFlag(checker::SignatureFlags::THIS_RETURN_TYPE)) { + return sig->ReturnType(); + } + auto *base_sig = sig->Function()->Signature(); + if (base_sig == sig) { + return nullptr; + } + return GuaranteedTypeForUncheckedCast(base_sig->ReturnType(), sig->ReturnType()); +} + void ETSChecker::ValidatePropertyAccess(varbinder::Variable *var, ETSObjectType *obj, const lexer::SourcePosition &pos) { if ((Context().Status() & CheckerStatus::IGNORE_VISIBILITY) != 0U) { @@ -360,7 +452,7 @@ varbinder::Variable *ETSChecker::FindVariableInGlobal(const ir::Identifier *cons return Scope()->FindInGlobal(identifier->Name(), varbinder::ResolveBindingOptions::ALL).variable; } -bool ETSChecker::IsVariableStatic(const varbinder::Variable *var) const +bool ETSChecker::IsVariableStatic(const varbinder::Variable *var) { if (var->HasFlag(varbinder::VariableFlags::METHOD)) { return var->TsType()->AsETSFunctionType()->CallSignatures()[0]->HasSignatureFlag(SignatureFlags::STATIC); @@ -368,7 +460,7 @@ bool ETSChecker::IsVariableStatic(const varbinder::Variable *var) const return var->HasFlag(varbinder::VariableFlags::STATIC); } -bool ETSChecker::IsVariableGetterSetter(const varbinder::Variable *var) const +bool ETSChecker::IsVariableGetterSetter(const varbinder::Variable *var) { return var->TsType() != nullptr && var->TsType()->HasTypeFlag(TypeFlag::GETTER_SETTER); } @@ -440,8 +532,8 @@ void ETSChecker::ValidateMemberIdentifier(ir::Identifier *const ident, varbinder return; } - if (!type->IsETSObjectType() && !type->IsETSArrayType() && !type->IsETSEnumType() && !type->IsETSStringEnumType() && - !type->IsETSUnionType() && !type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { + if (!IsReferenceType(type) && !type->IsETSEnumType() && !type->IsETSStringEnumType() && + !type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { ThrowError(ident); } } @@ -520,7 +612,7 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident, varbind NotResolvedError(ident); } - auto *const resolved_type = GetTypeOfVariable(resolved); + auto *const resolved_type = ETSChecker::GetApparentType(GetTypeOfVariable(resolved)); switch (ident->Parent()->Type()) { case ir::AstNodeType::CALL_EXPRESSION: { @@ -628,7 +720,7 @@ Type *ETSChecker::ResolveIdentifier(ir::Identifier *const ident) SaveCapturedVariable(resolved, ident->Start()); ident->SetVariable(resolved); - return resolved->TsType(); + return GetTypeOfVariable(resolved); } void ETSChecker::ValidateUnaryOperatorOperand(varbinder::Variable *variable) @@ -787,7 +879,18 @@ Type *ETSChecker::HandleBooleanLogicalOperatorsExtended(Type *left_type, Type *r auto [resolve_right, right_value] = IsNullLikeOrVoidExpression(expr->Right()) ? std::make_tuple(true, false) : right_type->ResolveConditionExpr(); - if (!resolve_left) { + if (!expr->Left()->TsType()->ContainsUndefined() && !expr->Left()->TsType()->ContainsNull() && + !expr->Left()->TsType()->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { + resolve_left = true; + left_value = true; + } + if (!expr->Right()->TsType()->ContainsUndefined() && !expr->Right()->TsType()->ContainsNull() && + !expr->Right()->TsType()->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { + resolve_right = true; + right_value = true; + } + + if (!resolve_left && !resolve_right) { if (IsTypeIdenticalTo(left_type, right_type)) { return left_type; } @@ -874,7 +977,7 @@ void ETSChecker::ResolveReturnStatement(checker::Type *func_return_type, checker if (argument_type == nullptr) { ThrowTypeError("Invalid return statement expression", st->Argument()->Start()); } - st->Argument()->AddBoxingUnboxingFlag(GetBoxingFlag(argument_type)); + st->Argument()->AddBoxingUnboxingFlags(GetBoxingFlag(argument_type)); } if (!func_return_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { @@ -1092,6 +1195,7 @@ Type *ETSChecker::GetTypeFromTypeAliasReference(varbinder::Variable *var) auto *const alias_type_node = var->Declaration()->Node()->AsTSTypeAliasDeclaration(); TypeStackElement tse(this, alias_type_node, "Circular type alias reference", alias_type_node->Start()); + alias_type_node->Check(this); auto *const aliased_type = GetTypeFromTypeAnnotation(alias_type_node->TypeAnnotation()); var->SetTsType(aliased_type); @@ -1120,6 +1224,99 @@ Type *ETSChecker::GetTypeFromClassReference(varbinder::Variable *var) return class_type; } +void ETSChecker::ValidateGenericTypeAliasForClonedNode(ir::TSTypeAliasDeclaration *const type_alias_node, + const ir::TSTypeParameterInstantiation *const exact_type_params) +{ + auto *const cloned_node = type_alias_node->TypeAnnotation()->Clone(Allocator(), type_alias_node); + + // Basic check, we really don't want to change the original type nodes, more precise checking should be made + ASSERT(cloned_node != type_alias_node->TypeAnnotation()); + + // Currently only reference types are checked. This should be extended for other types in a follow up patch, but for + // complete usability, if the type isn't a simple reference type, then doN't check type alias declaration at all. + bool check_typealias = true; + + // Only transforming a temporary cloned node, so no modification is made in the AST + cloned_node->TransformChildrenRecursively( + [&check_typealias, &exact_type_params, type_alias_node](ir::AstNode *const node) -> ir::AstNode * { + if (!node->IsETSTypeReference()) { + return node; + } + + const auto *const node_ident = node->AsETSTypeReference()->Part()->Name()->AsIdentifier(); + + size_t type_param_idx = 0; + for (const auto *const type_param : type_alias_node->TypeParams()->Params()) { + if (type_param->Name()->AsIdentifier()->Variable() == node_ident->Variable()) { + break; + } + type_param_idx++; + } + + if (type_param_idx == type_alias_node->TypeParams()->Params().size()) { + return node; + } + + auto *const type_param_type = exact_type_params->Params().at(type_param_idx); + + if (!type_param_type->IsETSTypeReference()) { + check_typealias = false; + return node; + } + + return type_param_type; + }); + + if (check_typealias) { + cloned_node->Check(this); + } +} + +Type *ETSChecker::HandleTypeAlias(ir::Expression *const name, const ir::TSTypeParameterInstantiation *const type_params) +{ + ASSERT(name->IsIdentifier() && name->AsIdentifier()->Variable() && + name->AsIdentifier()->Variable()->Declaration()->IsTypeAliasDecl()); + + auto *const type_alias_node = + name->AsIdentifier()->Variable()->Declaration()->AsTypeAliasDecl()->Node()->AsTSTypeAliasDeclaration(); + + // NOTE (mmartin): modify for default params + if ((type_params == nullptr) != (type_alias_node->TypeParams() == nullptr)) { + if (type_params == nullptr) { + ThrowTypeError("Type alias declaration is generic, but no type parameters were provided", name->Start()); + } + + ThrowTypeError("Type alias declaration is not generic, but type parameters were provided", + type_params->Start()); + } + + if (type_params == nullptr) { + return GetReferencedTypeBase(name); + } + + for (auto *const orig_type_param : type_params->Params()) { + orig_type_param->Check(this); + } + + Type *const alias_type = GetReferencedTypeBase(name); + auto *const alias_sub = NewSubstitution(); + + if (type_alias_node->TypeParams()->Params().size() != type_params->Params().size()) { + ThrowTypeError("Wrong number of type parameters for generic type alias", type_params->Start()); + } + + for (std::size_t idx = 0; idx < type_alias_node->TypeParams()->Params().size(); ++idx) { + auto *type_alias_type = type_alias_node->TypeParams()->Params().at(idx)->Name()->Variable()->TsType(); + if (type_alias_type->IsETSTypeParameter()) { + alias_sub->insert({type_alias_type->AsETSTypeParameter(), type_params->Params().at(idx)->TsType()}); + } + } + + ValidateGenericTypeAliasForClonedNode(type_alias_node->AsTSTypeAliasDeclaration(), type_params); + + return alias_type->Substitute(Relation(), alias_sub); +} + Type *ETSChecker::GetTypeFromEnumReference([[maybe_unused]] varbinder::Variable *var) { if (var->TsType() != nullptr) { @@ -1177,6 +1374,10 @@ void ETSChecker::SetPropertiesForModuleObject(checker::ETSObjectType *module_obj auto r = ext_records.find(import_path); return r != ext_records.end() ? r : ext_records.find(ets_binder->GetResolvedImportPath(import_path)); }(); + + // Check imported properties before assigning them to module object + res->second.front()->Ast()->Check(this); + for (auto [_, var] : res->second.front()->GlobalClassScope()->StaticFieldScope()->Bindings()) { (void)_; if (var->AsLocalVariable()->Declaration()->Node()->IsExported()) { @@ -1197,6 +1398,13 @@ void ETSChecker::SetPropertiesForModuleObject(checker::ETSObjectType *module_obj module_obj_type->AddProperty(var->AsLocalVariable()); } } + + for (auto [_, var] : res->second.front()->GlobalClassScope()->TypeAliasScope()->Bindings()) { + (void)_; + if (var->AsLocalVariable()->Declaration()->Node()->IsExported()) { + module_obj_type->AddProperty(var->AsLocalVariable()); + } + } } void ETSChecker::SetrModuleObjectTsType(ir::Identifier *local, checker::ETSObjectType *module_obj_type) @@ -1457,7 +1665,7 @@ bool ETSChecker::IsTypeBuiltinType(Type *type) bool ETSChecker::IsReferenceType(const Type *type) { return type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT) || type->IsETSNullLike() || - type->IsETSStringType(); + type->IsETSStringType() || type->IsETSTypeParameter() || type->IsETSUnionType(); } const ir::AstNode *ETSChecker::FindJumpTarget(ir::AstNodeType node_type, const ir::AstNode *node, @@ -1583,18 +1791,22 @@ Type *ETSChecker::PrimitiveTypeAsETSBuiltinType(Type *object_type) auto saved_result = Relation()->IsTrue(); Relation()->Result(false); + if (Checker::GetGlobalTypesHolder()->GlobalIntegerBuiltinType() == nullptr) { + InitializeBuiltin(VarBinder()->TopScope()->Bindings().find("Int")->second, "Int"); + } + BoxingConverter converter = BoxingConverter(AsETSChecker(), Relation(), object_type, Checker::GetGlobalTypesHolder()->GlobalIntegerBuiltinType()); Relation()->Result(saved_result); return converter.Result(); } -void ETSChecker::AddBoxingUnboxingFlagToNode(ir::AstNode *node, Type *boxing_unboxing_type) +void ETSChecker::AddBoxingUnboxingFlagsToNode(ir::AstNode *node, Type *boxing_unboxing_type) { if (boxing_unboxing_type->IsETSObjectType()) { - node->AddBoxingUnboxingFlag(GetBoxingFlag(boxing_unboxing_type)); + node->AddBoxingUnboxingFlags(GetBoxingFlag(boxing_unboxing_type)); } else { - node->AddBoxingUnboxingFlag(GetUnboxingFlag(boxing_unboxing_type)); + node->AddBoxingUnboxingFlags(GetUnboxingFlag(boxing_unboxing_type)); } } @@ -1844,7 +2056,7 @@ void ETSChecker::AddBoxingFlagToPrimitiveType(TypeRelation *relation, Type *targ { auto boxing_result = PrimitiveTypeAsETSBuiltinType(target); if (boxing_result != nullptr) { - relation->GetNode()->AddBoxingUnboxingFlag(GetBoxingFlag(boxing_result)); + relation->GetNode()->AddBoxingUnboxingFlags(GetBoxingFlag(boxing_result)); relation->Result(true); } } @@ -1853,7 +2065,7 @@ void ETSChecker::AddUnboxingFlagToPrimitiveType(TypeRelation *relation, Type *so { auto unboxing_result = UnboxingConverter(this, relation, source, self).Result(); if ((unboxing_result != nullptr) && relation->IsTrue()) { - relation->GetNode()->AddBoxingUnboxingFlag(GetUnboxingFlag(unboxing_result)); + relation->GetNode()->AddBoxingUnboxingFlags(GetUnboxingFlag(unboxing_result)); } } @@ -1882,7 +2094,7 @@ void ETSChecker::CheckUnboxedTypesAssignable(TypeRelation *relation, Type *sourc } relation->IsAssignableTo(unboxed_source_type, unboxed_target_type); if (relation->IsTrue()) { - relation->GetNode()->AddBoxingUnboxingFlag( + relation->GetNode()->AddBoxingUnboxingFlags( relation->GetChecker()->AsETSChecker()->GetUnboxingFlag(unboxed_source_type)); } } @@ -1926,7 +2138,7 @@ void ETSChecker::CheckUnboxedSourceTypeWithWideningAssignable(TypeRelation *rela relation->GetChecker()->AsETSChecker()->CheckUnboxedTypeWidenable(relation, target, unboxed_source_type); } if (!relation->OnlyCheckBoxingUnboxing()) { - relation->GetNode()->AddBoxingUnboxingFlag( + relation->GetNode()->AddBoxingUnboxingFlags( relation->GetChecker()->AsETSChecker()->GetUnboxingFlag(unboxed_source_type)); } } @@ -2075,17 +2287,19 @@ ETSObjectType *ETSChecker::GetRelevantArgumentedTypeFromChild(ETSObjectType *con static void TypeToString(std::stringstream &ss, Type *tp) { - if (tp->IsETSObjectType() && tp->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::TYPE_PARAMETER)) { - ss << tp->AsETSObjectType()->GetDeclNode()->Start().index; + if (tp->IsETSTypeParameter()) { + ss << tp->AsETSTypeParameter()->GetDeclNode()->Start().index; ss << "."; } - if (tp->IsETSObjectType()) { - ss << tp->AsETSObjectType()->Name(); - } else { + if (!tp->IsETSObjectType()) { tp->ToString(ss); + return; } - if (tp->IsETSObjectType() && !tp->AsETSObjectType()->TypeArguments().empty()) { - auto type_args = tp->AsETSObjectType()->TypeArguments(); + auto *const obj_type = tp->AsETSObjectType(); + ss << obj_type->Name(); + + if (!obj_type->TypeArguments().empty()) { + auto type_args = obj_type->TypeArguments(); ss << "<"; for (auto *ta : type_args) { TypeToString(ss, ta); @@ -2093,6 +2307,19 @@ static void TypeToString(std::stringstream &ss, Type *tp) } ss << ">"; } + + if (tp->ContainsNull()) { + ss << "|null"; + } + + if (tp->ContainsUndefined()) { + ss << "|undefined"; + } +} + +void ETSChecker::EmplaceSubstituted(Substitution *substitution, ETSTypeParameter *tparam, Type *type_arg) +{ + substitution->emplace(tparam, type_arg); } util::StringView ETSChecker::GetHashFromTypeArguments(const ArenaVector &type_arg_types) @@ -2144,7 +2371,7 @@ Type *ETSChecker::GetTypeFromTypeAnnotation(ir::TypeNode *const type_annotation) return type; } - if (!type->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT) && !type->HasTypeFlag(TypeFlag::ETS_UNION)) { + if (!IsReferenceType(type)) { ThrowTypeError("Non reference types cannot be nullish.", type_annotation->Start()); } @@ -2172,25 +2399,29 @@ void ETSChecker::CheckValidGenericTypeParameter(Type *const arg_type, const lexe ThrowTypeError("Type '" + ss.str() + "' is not valid for generic type arguments", pos); } -void ETSChecker::CheckNumberOfTypeArguments(Type *const type, ir::TSTypeParameterDeclaration *const type_param_decl, +void ETSChecker::CheckNumberOfTypeArguments(ETSObjectType *const type, ir::TSTypeParameterInstantiation *const type_args, const lexer::SourcePosition &pos) { - if (type_param_decl != nullptr && type_args == nullptr) { - ThrowTypeError({"Type '", type, "' is generic but type argument were not provided."}, pos); + auto const &type_params = type->TypeArguments(); + if (type_params.empty()) { + if (type_args != nullptr) { + ThrowTypeError({"Type '", type, "' is not generic."}, pos); + } + return; } - if (type_param_decl == nullptr && type_args != nullptr) { - ThrowTypeError({"Type '", type, "' is not generic."}, pos); - } + size_t minimum_type_args = std::count_if(type_params.begin(), type_params.end(), [](Type *param) { + return param->AsETSTypeParameter()->GetDefaultType() == nullptr; + }); - if (type_args == nullptr) { - return; + if (type_args == nullptr && minimum_type_args > 0) { + ThrowTypeError({"Type '", type, "' is generic but type argument were not provided."}, pos); } - ASSERT(type_param_decl != nullptr && type_args != nullptr); - if (type_param_decl->Params().size() != type_args->Params().size()) { - ThrowTypeError({"Type '", type, "' has ", type_param_decl->Params().size(), " number of type parameters, but ", + if (type_args != nullptr && + ((minimum_type_args > type_args->Params().size()) || (type_params.size() < type_args->Params().size()))) { + ThrowTypeError({"Type '", type, "' has ", minimum_type_args, " number of type parameters, but ", type_args->Params().size(), " type arguments were provided."}, pos); } diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index def4d1b88433665001087eda57f2a7a4e037cd6f..00aa62686e8cd734f0e3e47e790f3e0bb0fa2bea 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -165,12 +165,6 @@ ArenaVector ETSChecker::GetInterfaces(ETSObjectType *type) return type->Interfaces(); } -void ETSChecker::SetTypeParameterType(ir::TSTypeParameter *type_param, Type *type_param_type) -{ - auto *var = type_param->Name()->Variable(); - var->SetTsType(type_param_type); -} - ArenaVector ETSChecker::CreateTypeForTypeParameters(ir::TSTypeParameterDeclaration *type_params) { ArenaVector result {Allocator()->Adapter()}; @@ -187,7 +181,7 @@ ArenaVector ETSChecker::CreateTypeForTypeParameters(ir::TSTypeParameterD } for (auto *const type_param : type_params->Params()) { - result.emplace_back(CreateTypeParameterType(type_param)); + result.emplace_back(SetUpParameterType(type_param)); } // The type parameter might be used in the constraint, like 'K extend Comparable', @@ -227,74 +221,50 @@ void ETSChecker::CheckTypeParameterConstraint(ir::TSTypeParameter *param, Type2T extends.emplace(type_param_name, constraint_name); } -Type *ETSChecker::CreateTypeParameterType(ir::TSTypeParameter *const param) -{ - auto const instantiate_supertype = [this](TypeFlag nullish_flags) { - return CreateNullishType(GlobalETSObjectType(), nullish_flags, Allocator(), Relation(), GetGlobalTypesHolder()) - ->AsETSObjectType(); - }; - - ETSObjectType *param_type = SetUpParameterType(param); - if (param->Constraint() == nullptr) { - // No constraint, so it's Object|null - param_type->SetSuperType(instantiate_supertype(TypeFlag::NULLISH)); - } - - return param_type; -} - void ETSChecker::SetUpTypeParameterConstraint(ir::TSTypeParameter *const param) { - auto const instantiate_supertype = [this](TypeFlag nullish_flags) { - return CreateNullishType(GlobalETSObjectType(), nullish_flags, Allocator(), Relation(), GetGlobalTypesHolder()) - ->AsETSObjectType(); - }; + ETSTypeParameter *const param_type = [this, param]() { + auto *const type = param->Name()->Variable()->TsType(); + return type != nullptr ? type->AsETSTypeParameter() : SetUpParameterType(param); + }(); - ETSObjectType *param_type = nullptr; - if (param->Name()->Variable()->TsType() == nullptr) { - param_type = SetUpParameterType(param); - } else { - param_type = param->Name()->Variable()->TsType()->AsETSObjectType(); - } - if (param->Constraint() != nullptr) { - if (param->Constraint()->IsETSTypeReference()) { - const auto constraint_name = - param->Constraint()->AsETSTypeReference()->Part()->Name()->AsIdentifier()->Name(); - const auto *const type_param_scope = param->Parent()->AsTSTypeParameterDeclaration()->Scope(); - if (auto *const found_param = - type_param_scope->FindLocal(constraint_name, varbinder::ResolveBindingOptions::BINDINGS); - found_param != nullptr) { - SetUpTypeParameterConstraint(found_param->Declaration()->Node()->AsTSTypeParameter()); + auto const traverse_referenced = + [this, scope = param->Parent()->AsTSTypeParameterDeclaration()->Scope()](ir::TypeNode *type_node) { + if (!type_node->IsETSTypeReference()) { + return; } - } + const auto type_name = type_node->AsETSTypeReference()->Part()->Name()->AsIdentifier()->Name(); + auto *const found = scope->FindLocal(type_name, varbinder::ResolveBindingOptions::BINDINGS); + if (found != nullptr) { + SetUpTypeParameterConstraint(found->Declaration()->Node()->AsTSTypeParameter()); + } + }; - auto *constraint_type = param->Constraint()->GetType(this); - if (!constraint_type->IsETSObjectType()) { + if (param->Constraint() != nullptr) { + traverse_referenced(param->Constraint()); + auto *const constraint = param->Constraint()->GetType(this); + if (!constraint->IsETSObjectType() && !constraint->IsETSTypeParameter() && !constraint->IsETSUnionType()) { ThrowTypeError("Extends constraint must be an object", param->Constraint()->Start()); } - auto *constraint_obj_type = constraint_type->AsETSObjectType(); - param_type->SetAssemblerName(constraint_obj_type->AssemblerName()); - if (constraint_type->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::INTERFACE)) { - param_type->AddInterface(constraint_obj_type); - param_type->SetSuperType(instantiate_supertype(TypeFlag(TypeFlag::NULLISH & constraint_type->TypeFlags()))); - } else { - param_type->SetSuperType(constraint_obj_type); - } - } else { - // No constraint, so it's Object|null|undefined - param_type->SetSuperType(instantiate_supertype(TypeFlag::NULLISH)); + param_type->SetConstraintType(constraint); + } + if (param->DefaultType() != nullptr) { + traverse_referenced(param->DefaultType()); + auto *const dflt = param->DefaultType()->GetType(this); + // NOTE: #14993 ensure default matches constraint + param_type->SetDefaultType(dflt); } } -ETSObjectType *ETSChecker::SetUpParameterType(ir::TSTypeParameter *const param) +ETSTypeParameter *ETSChecker::SetUpParameterType(ir::TSTypeParameter *const param) { - ETSObjectType *param_type = - CreateNewETSObjectType(param->Name()->Name(), param, GlobalETSObjectType()->ObjectFlags()); - param_type->SetAssemblerName(GlobalETSObjectType()->AssemblerName()); + auto *const param_type = CreateTypeParameter(); + param_type->AddTypeFlag(TypeFlag::GENERIC); - param_type->AddObjectFlag(ETSObjectFlags::TYPE_PARAMETER); + param_type->SetDeclNode(param); param_type->SetVariable(param->Variable()); - SetTypeParameterType(param, param_type); + + param->Name()->Variable()->SetTsType(param_type); return param_type; } @@ -862,6 +832,10 @@ void ETSChecker::CheckConstFieldInitialized(const ETSObjectType *class_type, var { const bool class_var_static = class_var->Declaration()->Node()->AsClassProperty()->IsStatic(); for (const auto &prop : class_type->Methods()) { + if (!prop->TsType()->IsETSFunctionType()) { + continue; + } + const auto &call_sigs = prop->TsType()->AsETSFunctionType()->CallSignatures(); for (const auto *signature : call_sigs) { if ((signature->Function()->IsConstructor() && !class_var_static) || @@ -941,7 +915,7 @@ void ETSChecker::ValidateArrayIndex(ir::Expression *const expr, bool relaxed) Type const *const index_type = ApplyUnaryOperatorPromotion(expression_type); if (expression_type->IsETSObjectType() && (unboxed_expression_type != nullptr)) { - expr->AddBoxingUnboxingFlag(GetUnboxingFlag(unboxed_expression_type)); + expr->AddBoxingUnboxingFlags(GetUnboxingFlag(unboxed_expression_type)); } if (relaxed && index_type != nullptr && index_type->HasTypeFlag(TypeFlag::ETS_FLOATING_POINT)) { @@ -1040,7 +1014,7 @@ ETSObjectType *ETSChecker::CheckThisOrSuperAccess(ir::Expression *node, ETSObjec ThrowTypeError({"'", msg, "' cannot be referenced from a static context"}, node->Start()); } - if (class_type->GetDeclNode()->AsClassDefinition()->IsGlobal()) { + if (class_type->GetDeclNode()->IsClassDefinition() && class_type->GetDeclNode()->AsClassDefinition()->IsGlobal()) { ThrowTypeError({"Cannot reference '", msg, "' in this context."}, node->Start()); } @@ -1285,19 +1259,21 @@ std::vector ETSChecker::ResolveMemberReference(const ir::Member ASSERT((prop_type->FindSetter() != nullptr) == prop_type->HasTypeFlag(TypeFlag::SETTER)); auto const &source_pos = member_expr->Property()->Start(); + auto call_expr = + member_expr->Parent()->IsCallExpression() ? member_expr->Parent()->AsCallExpression() : nullptr; if ((search_flag & PropertySearchFlags::IS_GETTER) != 0) { if (!prop_type->HasTypeFlag(TypeFlag::GETTER)) { ThrowTypeError("Cannot read from this property because it is writeonly.", source_pos); } - ValidateSignatureAccessibility(member_expr->ObjType(), prop_type->FindGetter(), source_pos); + ValidateSignatureAccessibility(member_expr->ObjType(), call_expr, prop_type->FindGetter(), source_pos); } if ((search_flag & PropertySearchFlags::IS_SETTER) != 0) { if (!prop_type->HasTypeFlag(TypeFlag::SETTER)) { ThrowTypeError("Cannot assign to this property because it is readonly.", source_pos); } - ValidateSignatureAccessibility(member_expr->ObjType(), prop_type->FindSetter(), source_pos); + ValidateSignatureAccessibility(member_expr->ObjType(), call_expr, prop_type->FindSetter(), source_pos); } } @@ -1480,6 +1456,24 @@ Type *ETSChecker::FindLeastUpperBound(Type *source, Type *target) return GetTypeargumentedLUB(relevant_source_type, relevant_target_type); } +Type *ETSChecker::GetApparentType(Type *type) +{ + if (type->IsETSTypeParameter()) { + auto *const param = type->AsETSTypeParameter(); + return param->HasConstraint() ? param->GetConstraintType() : param; + } + return type; +} + +Type const *ETSChecker::GetApparentType(Type const *type) +{ + if (type->IsETSTypeParameter()) { + auto *const param = type->AsETSTypeParameter(); + return param->HasConstraint() ? param->GetConstraintType() : param; + } + return type; +} + Type *ETSChecker::GetCommonClass(Type *source, Type *target) { SavedTypeRelationFlagsContext checker_ctx(this->Relation(), TypeRelationFlag::IGNORE_TYPE_PARAMETERS); diff --git a/ets2panda/checker/ets/typeCreation.cpp b/ets2panda/checker/ets/typeCreation.cpp index ca04841b30ec36fb3705526f3788a8cb759a96f0..243fd358958ecfb1f637f142828b32d574ff8354 100644 --- a/ets2panda/checker/ets/typeCreation.cpp +++ b/ets2panda/checker/ets/typeCreation.cpp @@ -117,7 +117,7 @@ ETSArrayType *ETSChecker::CreateETSArrayType(Type *element_type) auto *array_type = Allocator()->New(element_type); auto it = array_types_.insert({element_type, array_type}); - if (it.second && !element_type->IsTypeParameter()) { + if (it.second && !element_type->IsETSTypeParameter()) { CreateBuiltinArraySignature(array_type, array_type->Rank()); } @@ -137,14 +137,11 @@ Type *ETSChecker::CreateETSUnionType(ArenaVector &&constituent_types) it->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE) ? BoxingConverter::ETSTypeFromSource(this, it) : it); } + ETSUnionType::NormalizeTypes(Relation(), new_constituent_types); if (new_constituent_types.size() == 1) { return new_constituent_types[0]; } - - auto *new_union_type = Allocator()->New(std::move(new_constituent_types)); - new_union_type->SetLeastUpperBoundType(this); - - return ETSUnionType::HandleUnionType(Relation(), new_union_type); + return Allocator()->New(this, std::move(new_constituent_types)); } ETSFunctionType *ETSChecker::CreateETSFunctionType(ArenaVector &signatures) @@ -193,9 +190,9 @@ SignatureInfo *ETSChecker::CreateSignatureInfo() return Allocator()->New(Allocator()); } -ETSTypeParameter *ETSChecker::CreateTypeParameter(Type *assembler_type) +ETSTypeParameter *ETSChecker::CreateTypeParameter() { - return Allocator()->New(assembler_type); + return Allocator()->New(); } ETSFunctionType *ETSChecker::CreateETSFunctionType(util::StringView name) @@ -231,6 +228,9 @@ ETSObjectType *ETSChecker::CreateETSObjectTypeCheckBuiltins(util::StringView nam return GlobalETSObjectType(); } GetGlobalTypesHolder()->GlobalTypes()[static_cast(GlobalTypeId::ETS_OBJECT_BUILTIN)] = obj_type; + auto *nullish = + CreateNullishType(obj_type, checker::TypeFlag::NULLISH, Allocator(), Relation(), GetGlobalTypesHolder()); + GetGlobalTypesHolder()->GlobalTypes()[static_cast(GlobalTypeId::ETS_NULLISH_OBJECT)] = nullish; } else if (name == compiler::Signatures::BUILTIN_EXCEPTION_CLASS) { if (GlobalBuiltinExceptionType() != nullptr) { return GlobalBuiltinExceptionType(); diff --git a/ets2panda/checker/ets/typeRelationContext.cpp b/ets2panda/checker/ets/typeRelationContext.cpp index df9af96acc4b39244c929d14ae6e467ce6d47aae..cbc061f0b9a54cacb5d3f3b0a0221e7b59c411b5 100644 --- a/ets2panda/checker/ets/typeRelationContext.cpp +++ b/ets2panda/checker/ets/typeRelationContext.cpp @@ -38,18 +38,15 @@ void AssignmentContext::ValidateArrayTypeInitializerByElement(TypeRelation *rela } } -bool InstantiationContext::ValidateTypeArguments(ETSObjectType *type, ir::TSTypeParameterDeclaration *type_param_decl, - ir::TSTypeParameterInstantiation *type_args, +bool InstantiationContext::ValidateTypeArguments(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args, const lexer::SourcePosition &pos) { - checker_->CheckNumberOfTypeArguments(type, type_param_decl, type_args, pos); - - if (type_args == nullptr) { + checker_->CheckNumberOfTypeArguments(type, type_args, pos); + if (type->TypeArguments().empty()) { result_ = type; return true; } - auto *substitution = checker_->NewSubstitution(); /* The first loop is to create a substitution of type_params & type_args. so that we can replace the type_params in constaints by the right type. @@ -63,90 +60,84 @@ bool InstantiationContext::ValidateTypeArguments(ETSObjectType *type, ir::TSType In this case, we will check "Comparable" with "Char", since "Char" doesn't extends "Comparable", we will get an error here. */ - for (size_t type_param_iter = 0; type_param_iter < type_param_decl->Params().size(); ++type_param_iter) { - auto *const type_arg_type = type_args->Params().at(type_param_iter)->GetType(checker_); - checker_->CheckValidGenericTypeParameter(type_arg_type, pos); - auto *const type_param_type = type->TypeArguments().at(type_param_iter); - substitution->emplace(type_param_type, type_arg_type); - } - for (size_t type_param_iter = 0; type_param_iter < type_param_decl->Params().size(); ++type_param_iter) { - auto *const type_arg_type = type_args->Params().at(type_param_iter)->GetType(checker_); - auto *const type_param_constraint = - type_param_decl->Params().at(type_param_iter)->AsTSTypeParameter()->Constraint(); - if (type_param_constraint == nullptr) { - continue; + auto const get_types = [this, &type_args, type](size_t idx) -> std::pair { + auto *type_param = type->TypeArguments().at(idx)->AsETSTypeParameter(); + if (type_args != nullptr && idx < type_args->Params().size()) { + return {type_param, type_args->Params().at(idx)->GetType(checker_)}; } + return {type_param, type_param->GetDefaultType()}; + }; - bool assignable = false; - auto *constraint_type = type_param_constraint->GetType(checker_); + auto *const substitution = checker_->NewSubstitution(); - if (!constraint_type->AsETSObjectType()->TypeArguments().empty()) { - constraint_type = constraint_type->Substitute(checker_->Relation(), substitution); - } + for (size_t idx = 0; idx < type->TypeArguments().size(); ++idx) { + auto const [type_param, type_arg] = get_types(idx); + checker_->CheckValidGenericTypeParameter(type_arg, pos); + type_arg->Substitute(checker_->Relation(), substitution); + ETSChecker::EmplaceSubstituted(substitution, type_param, type_arg); + } - if (constraint_type->IsETSObjectType() && type_arg_type->IsETSObjectType()) { - assignable = ValidateTypeArg(constraint_type->AsETSObjectType(), type_arg_type->AsETSObjectType()); - } else if (type_arg_type->IsETSUnionType() && !constraint_type->IsETSUnionType()) { - auto constituent_types = type_arg_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()); - }); + for (size_t idx = 0; idx < type->TypeArguments().size(); ++idx) { + auto const [type_param, type_arg] = get_types(idx); + if (type_param->GetConstraintType() == nullptr) { + continue; } + auto *const constraint = type_param->GetConstraintType()->Substitute(checker_->Relation(), substitution); - if (!assignable) { - checker_->ThrowTypeError({"Type '", type_arg_type->AsETSObjectType(), - "' is not assignable to constraint type '", constraint_type, "'."}, - type_args->Params().at(type_param_iter)->Start()); + if (!ValidateTypeArg(constraint, type_arg) && type_args != nullptr && + !checker_->Relation()->NoThrowGenericTypeAlias()) { + checker_->ThrowTypeError({"Type '", type_arg, "' is not assignable to constraint type '", constraint, "'."}, + type_args->Params().at(idx)->Start()); } } return false; } -bool InstantiationContext::ValidateTypeArg(ETSObjectType *constraint_type, ETSObjectType *arg_ref_type) +bool InstantiationContext::ValidateTypeArg(Type *constraint_type, Type *type_arg) { - if (const auto *const found = checker_->AsETSChecker()->Scope()->FindLocal( - constraint_type->Name(), varbinder::ResolveBindingOptions::TYPE_ALIASES); - found != nullptr) { - arg_ref_type = found->TsType()->AsETSObjectType(); + if (!ETSChecker::IsReferenceType(type_arg)) { + return false; } - 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()) { - // NOTE: mmartin. make correct check later for multiple bounds - assignable = (interface == constraint_type) || assignable; - } + if (type_arg->IsETSUnionType()) { + auto const &constituent_types = type_arg->AsETSUnionType()->ConstituentTypes(); + return std::all_of(constituent_types.begin(), constituent_types.end(), + [this, constraint_type](Type *c_type) { return ValidateTypeArg(constraint_type, c_type); }); } - return assignable; + return checker_->Relation()->IsAssignableTo(type_arg, constraint_type); } void InstantiationContext::InstantiateType(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args) { ArenaVector type_arg_types(checker_->Allocator()->Adapter()); - type_arg_types.reserve(type_args->Params().size()); + type_arg_types.reserve(type->TypeArguments().size()); auto flags = ETSObjectFlags::NO_OPTS; - for (auto *const it : type_args->Params()) { - auto *param_type = checker_->GetTypeFromTypeAnnotation(it); + if (type_args != nullptr) { + for (auto *const it : type_args->Params()) { + auto *param_type = checker_->GetTypeFromTypeAnnotation(it); + + if (param_type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { + checker_->Relation()->SetNode(it); + auto *const boxed_type_arg = checker_->PrimitiveTypeAsETSBuiltinType(param_type); + ASSERT(boxed_type_arg); + param_type = boxed_type_arg->Instantiate(checker_->Allocator(), checker_->Relation(), + checker_->GetGlobalTypesHolder()); + } - if (param_type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { - checker_->Relation()->SetNode(it); - auto *const boxed_type_arg = checker_->PrimitiveTypeAsETSBuiltinType(param_type); - ASSERT(boxed_type_arg); - param_type = boxed_type_arg->Instantiate(checker_->Allocator(), checker_->Relation(), - checker_->GetGlobalTypesHolder()); + type_arg_types.push_back(param_type); } + } - type_arg_types.push_back(param_type); + while (type_arg_types.size() < type->TypeArguments().size()) { + type_arg_types.push_back(type->TypeArguments().at(type_arg_types.size())); } - InstantiateType(type, type_arg_types, type_args->Range().start); + InstantiateType(type, type_arg_types, (type_args == nullptr) ? lexer::SourcePosition() : type_args->Range().start); result_->AddObjectFlag(flags); } @@ -154,35 +145,33 @@ void InstantiationContext::InstantiateType(ETSObjectType *type, ArenaVectorGetHashFromTypeArguments(type_arg_types); - auto type_params = type->TypeArguments(); - if (type_params.size() != type_arg_types.size()) { - checker_->ThrowTypeError({"Wrong number of type arguments"}, pos); + auto const &type_params = type->TypeArguments(); + + while (type_arg_types.size() < type_params.size()) { + type_arg_types.push_back(type_params.at(type_arg_types.size())); } auto *substitution = checker_->NewSubstitution(); auto *constraints_substitution = checker_->NewSubstitution(); for (size_t ix = 0; ix < type_params.size(); ix++) { - constraints_substitution->emplace(type_params[ix], type_arg_types[ix]); + if (!type_params[ix]->IsETSTypeParameter()) { + continue; + } + ETSChecker::EmplaceSubstituted(constraints_substitution, type_params[ix]->AsETSTypeParameter(), + type_arg_types[ix]); } for (size_t ix = 0; ix < type_params.size(); 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, constraints_substitution](Type *type_arg) { - return checker_->IsCompatibleTypeArgument( - type_param, type_arg, constraints_substitution); - }); - } else { - is_compatible_type_arg = - checker_->IsCompatibleTypeArgument(type_param, type_arg_types[ix], constraints_substitution); + if (!type_param->IsETSTypeParameter()) { + continue; } - if (!is_compatible_type_arg) { + if (!checker_->IsCompatibleTypeArgument(type_param->AsETSTypeParameter(), type_arg_types[ix], + constraints_substitution) && + !checker_->Relation()->NoThrowGenericTypeAlias()) { checker_->ThrowTypeError( {"Type ", type_arg_types[ix], " is not assignable to", " type parameter ", type_params[ix]}, pos); } - substitution->emplace(type_params[ix], type_arg_types[ix]); + ETSChecker::EmplaceSubstituted(substitution, type_param->AsETSTypeParameter(), type_arg_types[ix]); } result_ = type->Substitute(checker_->Relation(), substitution)->AsETSObjectType(); diff --git a/ets2panda/checker/ets/typeRelationContext.h b/ets2panda/checker/ets/typeRelationContext.h index bd08c6587ad565e45c4c9e8a11f57ed6defeb724..d0148f37d9f0ab27bf8628c5c8485e69ee83f51e 100644 --- a/ets2panda/checker/ets/typeRelationContext.h +++ b/ets2panda/checker/ets/typeRelationContext.h @@ -139,19 +139,9 @@ public: const lexer::SourcePosition &pos) : checker_(checker) { - ir::TSTypeParameterDeclaration *type_param_decl = nullptr; - - if (type->HasObjectFlag(ETSObjectFlags::TYPE_PARAMETER)) { - type_param_decl = nullptr; - } else if (type->HasObjectFlag(ETSObjectFlags::CLASS)) { - type_param_decl = type->GetDeclNode()->AsClassDefinition()->TypeParams(); - } else if (type->HasObjectFlag(ETSObjectFlags::INTERFACE)) { - type_param_decl = type->GetDeclNode()->AsTSInterfaceDeclaration()->TypeParams(); - } - if (ValidateTypeArguments(type, type_param_decl, type_args, pos)) { + if (ValidateTypeArguments(type, type_args, pos)) { return; } - InstantiateType(type, type_args); } @@ -171,10 +161,10 @@ public: } private: - bool ValidateTypeArguments(ETSObjectType *type, ir::TSTypeParameterDeclaration *type_param_decl, - ir::TSTypeParameterInstantiation *type_args, const lexer::SourcePosition &pos); + bool ValidateTypeArguments(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args, + const lexer::SourcePosition &pos); - bool ValidateTypeArg(ETSObjectType *constraint_type, ETSObjectType *arg_ref_type); + bool ValidateTypeArg(Type *constraint_type, Type *type_arg); void InstantiateType(ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args); diff --git a/ets2panda/checker/types/ets/byteType.cpp b/ets2panda/checker/types/ets/byteType.cpp index f3468bbbac0b71b9bd134c2b2248f4e3445e6d7e..c943b1dd811e15f0a815e73f0ab74a5e603d6245 100644 --- a/ets2panda/checker/types/ets/byteType.cpp +++ b/ets2panda/checker/types/ets/byteType.cpp @@ -74,6 +74,16 @@ void ByteType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/ets/charType.cpp b/ets2panda/checker/types/ets/charType.cpp index 2c4f3995e742b62498b3d74560faa7b7a1af8825..fb25961af45845e612cfaf9d4d5836220c4b3eef 100644 --- a/ets2panda/checker/types/ets/charType.cpp +++ b/ets2panda/checker/types/ets/charType.cpp @@ -74,6 +74,16 @@ void CharType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/ets/doubleType.cpp b/ets2panda/checker/types/ets/doubleType.cpp index 4b3055837caf61186465047adea9d092b3170354..fef74bb611119381653a265b10874471bd2eb2ae 100644 --- a/ets2panda/checker/types/ets/doubleType.cpp +++ b/ets2panda/checker/types/ets/doubleType.cpp @@ -69,6 +69,16 @@ void DoubleType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/ets/etsArrayType.cpp b/ets2panda/checker/types/ets/etsArrayType.cpp index 5c2bdbac39322b6294c27f5508b67211f922c134..706fb35ec0630589640cfc0b6f36242d6f6796f7 100644 --- a/ets2panda/checker/types/ets/etsArrayType.cpp +++ b/ets2panda/checker/types/ets/etsArrayType.cpp @@ -117,6 +117,12 @@ void ETSArrayType::Cast(TypeRelation *const relation, Type *const target) return; } + if (ElementType()->IsETSTypeParameter()) { + // unchecked cast! + relation->Result(true); + return; + } + conversion::Forbidden(relation); return; } @@ -141,8 +147,7 @@ void ETSArrayType::IsSupertypeOf(TypeRelation *const relation, Type *source) if (source->IsETSArrayType()) { auto *const source_elem_type = this->AsETSArrayType()->ElementType(); auto *const target_elem_type = source->AsETSArrayType()->ElementType(); - if (source_elem_type->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT) && - target_elem_type->HasTypeFlag(TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (ETSChecker::IsReferenceType(target_elem_type) && ETSChecker::IsReferenceType(source_elem_type)) { source_elem_type->IsSupertypeOf(relation, target_elem_type); } } @@ -159,9 +164,7 @@ Type *ETSArrayType::Substitute(TypeRelation *relation, const Substitution *subst if (substitution == nullptr || substitution->empty()) { return this; } - if (auto found = substitution->find(this); found != substitution->end()) { - return found->second; - } + auto *result_elt = element_->Substitute(relation, substitution); return result_elt == element_ ? this : relation->GetChecker()->AsETSChecker()->CreateETSArrayType(result_elt); } diff --git a/ets2panda/checker/types/ets/etsFunctionType.cpp b/ets2panda/checker/types/ets/etsFunctionType.cpp index 4674b6fe7cb1050b963e98c1cdd0ce745a9838b2..a0a1cfcd9880df0ad64909df52f7011ff5f48342 100644 --- a/ets2panda/checker/types/ets/etsFunctionType.cpp +++ b/ets2panda/checker/types/ets/etsFunctionType.cpp @@ -87,16 +87,20 @@ static Signature *ProcessSignatures(TypeRelation *relation, Signature *target, E if (!it->GetSignatureInfo()->type_params.empty()) { auto *substitution = relation->GetChecker()->AsETSChecker()->NewSubstitution(); auto *instantiated_type_params = relation->GetChecker()->AsETSChecker()->NewInstantiatedTypeParamsSet(); + bool res = true; for (size_t ix = 0; ix < target->MinArgCount(); ix++) { - relation->GetChecker()->AsETSChecker()->EnhanceSubstitutionForType( + res &= relation->GetChecker()->AsETSChecker()->EnhanceSubstitutionForType( it->GetSignatureInfo()->type_params, it->GetSignatureInfo()->params[ix]->TsType(), target->GetSignatureInfo()->params[ix]->TsType(), substitution, instantiated_type_params); } if (target->RestVar() != nullptr) { - relation->GetChecker()->AsETSChecker()->EnhanceSubstitutionForType( + res &= relation->GetChecker()->AsETSChecker()->EnhanceSubstitutionForType( it->GetSignatureInfo()->type_params, it->RestVar()->TsType(), target->RestVar()->TsType(), substitution, instantiated_type_params); } + if (!res) { + continue; + } it = it->Substitute(relation, substitution); } diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index be89ec759c3e895a7aad79edc92deb1bf6016dfd..6cdf7c43428528a1c959a5f5843397349602279a 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -286,7 +286,7 @@ void ETSObjectType::ToString(std::stringstream &ss) const { ss << name_; - if (IsGeneric()) { + if (!type_arguments_.empty()) { auto const type_arguments_size = type_arguments_.size(); ss << compiler::Signatures::GENERIC_BEGIN; type_arguments_[0]->ToString(ss); @@ -321,10 +321,6 @@ void ETSObjectType::IdenticalUptoNullability(TypeRelation *relation, Type *other return; } - if (this_base->HasObjectFlag(ETSObjectFlags::TYPE_PARAMETER) && this_base != other_base) { - return; - } - if (relation->IgnoreTypeParameters() || (this == other)) { relation->Result(true); return; @@ -349,6 +345,12 @@ void ETSObjectType::IdenticalUptoNullability(TypeRelation *relation, Type *other continue; } + // checking the nullishness of type args before getting their original base types + // because most probably GetOriginalBaseType will return the non-nullish version of the type + if (!type_arguments_[idx]->IsNullish() && other_type_arguments[idx]->IsNullish()) { + return; + } + const auto get_original_base_type_or_type = [&relation](Type *const original_type) { auto *const base_type = relation->GetChecker()->AsETSChecker()->GetOriginalBaseType(original_type); return base_type == nullptr ? original_type : base_type; @@ -453,12 +455,10 @@ bool ETSObjectType::CastNumericObject(TypeRelation *const relation, Type *const conversion::UnboxingWideningPrimitive(relation, this, target); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::CHAR)) { + conversion::UnboxingWideningNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_SHORT)) { if (target->HasTypeFlag(TypeFlag::SHORT)) { @@ -469,12 +469,10 @@ bool ETSObjectType::CastNumericObject(TypeRelation *const relation, Type *const conversion::UnboxingWideningPrimitive(relation, this, target); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::CHAR)) { + conversion::UnboxingNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_CHAR)) { if (target->HasTypeFlag(TypeFlag::CHAR)) { @@ -485,12 +483,10 @@ bool ETSObjectType::CastNumericObject(TypeRelation *const relation, Type *const conversion::UnboxingWideningPrimitive(relation, this, target); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT)) { + conversion::UnboxingNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_INT)) { if (target->HasTypeFlag(TypeFlag::INT)) { @@ -501,12 +497,10 @@ bool ETSObjectType::CastNumericObject(TypeRelation *const relation, Type *const conversion::UnboxingWideningPrimitive(relation, this, target); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT | TypeFlag::CHAR)) { + conversion::UnboxingNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_LONG)) { if (target->HasTypeFlag(TypeFlag::LONG)) { @@ -517,12 +511,10 @@ bool ETSObjectType::CastNumericObject(TypeRelation *const relation, Type *const conversion::UnboxingWideningPrimitive(relation, this, target); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT | TypeFlag::CHAR | TypeFlag::INT)) { + conversion::UnboxingNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_FLOAT)) { if (target->HasTypeFlag(TypeFlag::FLOAT)) { @@ -533,31 +525,40 @@ bool ETSObjectType::CastNumericObject(TypeRelation *const relation, Type *const conversion::UnboxingWideningPrimitive(relation, this, target); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT | TypeFlag::CHAR | TypeFlag::INT | TypeFlag::LONG)) { + conversion::UnboxingNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_DOUBLE)) { if (target->HasTypeFlag(TypeFlag::DOUBLE)) { conversion::Unboxing(relation, this); return true; } - if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { - conversion::WideningReference(relation, this, target->AsETSObjectType()); + if (target->HasTypeFlag(TypeFlag::BYTE | TypeFlag::SHORT | TypeFlag::CHAR | TypeFlag::INT | TypeFlag::LONG | + TypeFlag::FLOAT)) { + conversion::UnboxingNarrowingPrimitive(relation, this, target); return true; } - conversion::Forbidden(relation); - return true; } if (this->HasObjectFlag(ETSObjectFlags::BUILTIN_BOOLEAN)) { if (target->HasTypeFlag(TypeFlag::ETS_BOOLEAN)) { conversion::Unboxing(relation, this); return true; } + } + if (this->HasObjectFlag(ETSObjectFlags::UNBOXABLE_TYPE)) { if (target->HasTypeFlag(TypeFlag::ETS_OBJECT)) { + if (!target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::UNBOXABLE_TYPE)) { + conversion::WideningReference(relation, this, target->AsETSObjectType()); + return true; + } + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + CastNumericObject(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return true; + } conversion::WideningReference(relation, this, target->AsETSObjectType()); return true; } @@ -626,6 +627,11 @@ void ETSObjectType::IsSupertypeOf(TypeRelation *relation, Type *source) return; } + if (source->IsETSTypeParameter()) { + source->AsETSTypeParameter()->ConstraintIsSubtypeOf(relation, this); + return; + } + if (!source->IsETSObjectType() || !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::CLASS | ETSObjectFlags::INTERFACE | ETSObjectFlags::NULL_TYPE)) { @@ -791,41 +797,46 @@ ETSObjectType const *ETSObjectType::GetConstOriginalBaseType() const noexcept return this; } +bool ETSObjectType::SubstituteTypeArgs(TypeRelation *const relation, ArenaVector &new_type_args, + const Substitution *const substitution) +{ + bool any_change = false; + new_type_args.reserve(type_arguments_.size()); + + for (auto *const arg : type_arguments_) { + auto *const new_arg = arg->Substitute(relation, substitution); + new_type_args.push_back(new_arg); + any_change = any_change || (new_arg != arg); + } + + return any_change; +} + +void ETSObjectType::SetCopiedTypeProperties(TypeRelation *const relation, ETSObjectType *const copied_type, + ArenaVector &new_type_args, const Substitution *const substitution) +{ + copied_type->type_flags_ = type_flags_; + copied_type->RemoveObjectFlag(ETSObjectFlags::CHECKED_COMPATIBLE_ABSTRACTS | + ETSObjectFlags::INCOMPLETE_INSTANTIATION | ETSObjectFlags::CHECKED_INVOKE_LEGITIMACY); + copied_type->SetVariable(variable_); + copied_type->SetBaseType(this); + + copied_type->SetTypeArguments(std::move(new_type_args)); + copied_type->relation_ = relation; + copied_type->substitution_ = substitution; +} + Type *ETSObjectType::Substitute(TypeRelation *relation, const Substitution *substitution) { if (substitution == nullptr || substitution->empty()) { return this; } + auto *const checker = relation->GetChecker()->AsETSChecker(); auto *base = GetOriginalBaseType(); - if (auto repl = substitution->find(base); repl != substitution->end()) { - auto *repl_type = repl->second; - - /* Any other flags we need to copy? */ - - /* The check this != base is a kludge to distinguish bare type parameter T - with a nullish constraint (like the default Object?) from explicitly nullish T? - */ - if (this != base && ((ContainsNull() && !repl_type->ContainsNull()) || - (ContainsUndefined() && !repl_type->ContainsUndefined()))) { - // this type is explicitly marked as nullish - ASSERT(repl_type->IsETSObjectType() || repl_type->IsETSArrayType() || repl_type->IsETSFunctionType()); - auto nullish_flags = TypeFlag(TypeFlags() & TypeFlag::NULLISH); - auto *new_repl_type = checker->CreateNullishType(repl_type, nullish_flags, checker->Allocator(), relation, - checker->GetGlobalTypesHolder()); - repl_type = new_repl_type; - } - return repl_type; - } ArenaVector new_type_args {checker->Allocator()->Adapter()}; - new_type_args.reserve(type_arguments_.size()); - bool any_change = false; - for (auto *arg : type_arguments_) { - auto *new_arg = arg->Substitute(relation, substitution); - new_type_args.push_back(new_arg); - any_change |= (new_arg != arg); - } + const bool any_change = SubstituteTypeArgs(relation, new_type_args, substitution); // Lambda types can capture type params in their bodies, normal classes cannot. // NOTE: gogabr. determine precise conditions where we do not need to copy. @@ -845,16 +856,7 @@ Type *ETSObjectType::Substitute(TypeRelation *relation, const Substitution *subs relation->IncreaseTypeRecursionCount(base); auto *const copied_type = checker->CreateNewETSObjectType(name_, decl_node_, flags_); - copied_type->type_flags_ = type_flags_; - copied_type->RemoveObjectFlag(ETSObjectFlags::CHECKED_COMPATIBLE_ABSTRACTS | - ETSObjectFlags::INCOMPLETE_INSTANTIATION | ETSObjectFlags::CHECKED_INVOKE_LEGITIMACY); - copied_type->SetVariable(variable_); - copied_type->SetBaseType(this); - - copied_type->SetTypeArguments(std::move(new_type_args)); - copied_type->relation_ = relation; - copied_type->substitution_ = substitution; - + SetCopiedTypeProperties(relation, copied_type, new_type_args, substitution); GetInstantiationMap().try_emplace(hash, copied_type); if (super_type_ != nullptr) { @@ -920,4 +922,14 @@ void ETSObjectType::InstantiateProperties() const } } +void ETSObjectType::DebugInfoTypeFromName(std::stringstream &ss, util::StringView asm_name) +{ + ss << compiler::Signatures::CLASS_REF_BEGIN; + auto copied = asm_name.Mutf8(); + std::replace(copied.begin(), copied.end(), *compiler::Signatures::METHOD_SEPARATOR.begin(), + *compiler::Signatures::NAMESPACE_SEPARATOR.begin()); + ss << copied; + ss << compiler::Signatures::MANGLE_SEPARATOR; +} + } // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 391f54588ce9e0207ca381430e9c5e70461a690b..89f49aeb41cecb9890c02692a4a9c56886cd8282 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -45,9 +45,8 @@ enum class ETSObjectFlags : uint32_t { INNER = 1U << 15U, DYNAMIC = 1U << 16U, ASYNC_FUNC_RETURN_TYPE = 1U << 17U, - TYPE_PARAMETER = 1U << 18U, - CHECKED_INVOKE_LEGITIMACY = 1U << 19U, - UNDEFINED_TYPE = 1U << 20U, + CHECKED_INVOKE_LEGITIMACY = 1U << 18U, + UNDEFINED_TYPE = 1U << 19U, BUILTIN_STRING = 1U << 23U, BUILTIN_BOOLEAN = 1U << 24U, @@ -397,7 +396,7 @@ public: return invoke->TsType()->AsETSFunctionType(); } - ETSObjectFlags BuiltInKind() + ETSObjectFlags BuiltInKind() const { return static_cast(flags_ & ETSObjectFlags::BUILTIN_TYPE); } @@ -477,6 +476,10 @@ public: bool AssignmentSource(TypeRelation *relation, Type *target) override; void AssignmentTarget(TypeRelation *relation, Type *source) override; Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *global_types) override; + bool SubstituteTypeArgs(TypeRelation *relation, ArenaVector &new_type_args, + const Substitution *substitution); + void SetCopiedTypeProperties(TypeRelation *relation, ETSObjectType *copied_type, ArenaVector &new_type_args, + const Substitution *substitution); Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; bool CastNumericObject(TypeRelation *relation, Type *target); @@ -488,14 +491,11 @@ public: ss << assembler_name_; } + static void DebugInfoTypeFromName(std::stringstream &ss, util::StringView asm_name); + void ToDebugInfoType(std::stringstream &ss) const override { - ss << compiler::Signatures::CLASS_REF_BEGIN; - auto name = assembler_name_.Mutf8(); - std::replace(name.begin(), name.end(), *compiler::Signatures::METHOD_SEPARATOR.begin(), - *compiler::Signatures::NAMESPACE_SEPARATOR.begin()); - ss << name; - ss << compiler::Signatures::MANGLE_SEPARATOR; + DebugInfoTypeFromName(ss, assembler_name_); } void ToDebugInfoSignatureType(std::stringstream &ss) const diff --git a/ets2panda/checker/types/ets/etsTypeParameter.cpp b/ets2panda/checker/types/ets/etsTypeParameter.cpp index 43edd66d0c19a7774de1c0012c5d6cff3fb37b04..d19a6ef1162b28ee9fc728d3aad613230c6d612d 100644 --- a/ets2panda/checker/types/ets/etsTypeParameter.cpp +++ b/ets2panda/checker/types/ets/etsTypeParameter.cpp @@ -14,20 +14,175 @@ */ #include "etsTypeParameter.h" +#include "ir/expressions/identifier.h" +#include "ir/ts/tsTypeParameter.h" +#include "checker/ETSchecker.h" +#include "checker/ets/conversion.h" namespace panda::es2panda::checker { -void ETSTypeParameter::ToString([[maybe_unused]] std::stringstream &ss) const +void ETSTypeParameter::ToString(std::stringstream &ss) const { - UNREACHABLE(); + ss << decl_node_->Name()->Name(); + + if (IsNullish()) { + if (ContainsNull()) { + ss << "|null"; + } + if (ContainsUndefined()) { + ss << "|undefined"; + } + } } void ETSTypeParameter::Identical([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *other) { - UNREACHABLE(); + if ((ContainsNull() != other->ContainsNull()) || (ContainsUndefined() != other->ContainsUndefined())) { + return; + } + + if (other->IsETSTypeParameter() && other->AsETSTypeParameter()->GetOriginal() == GetOriginal()) { + relation->Result(true); + } +} + +bool ETSTypeParameter::AssignmentSource([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *target) +{ + return relation->Result(false); } void ETSTypeParameter::AssignmentTarget([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source) { - UNREACHABLE(); + if (source->IsETSNullType()) { + relation->Result(ContainsNull()); + return; + } + if (source->IsETSUndefinedType()) { + relation->Result(ContainsUndefined()); + return; + } + + if ((source->ContainsNull() && !ContainsNull()) || (source->ContainsUndefined() && !ContainsUndefined())) { + relation->Result(false); + return; + } + if (source->IsETSTypeParameter() && source->AsETSTypeParameter()->GetOriginal() == GetOriginal()) { + relation->Result(true); + return; + } + + IsSupertypeOf(relation, source); +} + +void ETSTypeParameter::Cast(TypeRelation *relation, Type *target) +{ + if (target->IsSupertypeOf(relation, this), relation->IsTrue()) { + relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST); + relation->Result(true); + return; + } + + // NOTE(vpukhov): adjust UNCHECKED_CAST flags + if (target->IsETSObjectType()) { + relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST); + } + relation->Result(relation->InCastingContext()); +} + +void ETSTypeParameter::CastTarget(TypeRelation *relation, Type *source) +{ + if (IsSupertypeOf(relation, source), relation->IsTrue()) { + relation->RemoveFlags(TypeRelationFlag::UNCHECKED_CAST); + relation->Result(true); + return; + } + + relation->Result(relation->InCastingContext()); +} + +void ETSTypeParameter::IsSupertypeOf([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Type *source) +{ + if (Identical(relation, source), relation->IsTrue()) { + return; + } + + if (source->IsETSTypeParameter()) { + source->AsETSTypeParameter()->ConstraintIsSubtypeOf(relation, this); + return; + } + + relation->Result(false); +} + +Type *ETSTypeParameter::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation, + [[maybe_unused]] GlobalTypesHolder *global_types) +{ + auto *const checker = relation->GetChecker()->AsETSChecker(); + + auto *const copied_type = checker->CreateTypeParameter(); + copied_type->AddTypeFlag(TypeFlag::GENERIC); + copied_type->SetDeclNode(GetDeclNode()); + copied_type->SetDefaultType(GetDefaultType()); + copied_type->SetConstraintType(GetConstraintType()); + copied_type->SetVariable(Variable()); + return copied_type; +} + +Type *ETSTypeParameter::Substitute(TypeRelation *relation, const Substitution *substitution) +{ + if (substitution == nullptr || substitution->empty()) { + return this; + } + auto *const checker = relation->GetChecker()->AsETSChecker(); + auto *original = GetOriginal(); + if (auto repl = substitution->find(original); repl != substitution->end()) { + auto *repl_type = repl->second; + /* Any other flags we need to copy? */ + + /* The check this != base is a kludge to distinguish bare type parameter T + with a nullish constraint (like the default Object?) from explicitly nullish T? + */ + if (this != original && ((ContainsNull() && !repl_type->ContainsNull()) || + (ContainsUndefined() && !repl_type->ContainsUndefined()))) { + // this type is explicitly marked as nullish + ASSERT(repl_type->IsETSObjectType() || repl_type->IsETSArrayType() || repl_type->IsETSFunctionType() || + repl_type->IsETSTypeParameter()); + auto nullish_flags = TypeFlag(TypeFlags() & TypeFlag::NULLISH); + auto *new_repl_type = checker->CreateNullishType(repl_type, nullish_flags, checker->Allocator(), relation, + checker->GetGlobalTypesHolder()); + repl_type = new_repl_type; + } + return repl_type; + } + + return this; +} + +Type *ETSTypeParameter::EffectiveConstraint(ETSChecker const *checker) const +{ + return HasConstraint() ? GetConstraintType() : checker->GlobalETSNullishObjectType(); } + +void ETSTypeParameter::ToAssemblerType(std::stringstream &ss) const +{ + if (HasConstraint()) { + GetConstraintType()->ToAssemblerType(ss); + } else { + ss << compiler::Signatures::BUILTIN_OBJECT; + } +} + +void ETSTypeParameter::ToDebugInfoType(std::stringstream &ss) const +{ + if (HasConstraint()) { + GetConstraintType()->ToDebugInfoType(ss); + } else { + ETSObjectType::DebugInfoTypeFromName(ss, compiler::Signatures::BUILTIN_OBJECT); + } +} + +ETSTypeParameter *ETSTypeParameter::GetOriginal() const +{ + return GetDeclNode()->Name()->Variable()->TsType()->AsETSTypeParameter(); +} + } // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/ets/etsTypeParameter.h b/ets2panda/checker/types/ets/etsTypeParameter.h index 1ef6df6e0e761c246f4819086cf0e07374145e53..d39645a555dfe14069f16b32f5030132dc098b0f 100644 --- a/ets2panda/checker/types/ets/etsTypeParameter.h +++ b/ets2panda/checker/types/ets/etsTypeParameter.h @@ -17,48 +17,83 @@ #define ES2PANDA_COMPILER_CHECKER_TYPES_ETS_TYPE_PARAMETER_TYPE_H #include "checker/types/type.h" +#include "ir/astNode.h" namespace panda::es2panda::checker { class ETSTypeParameter : public Type { public: explicit ETSTypeParameter() : Type(TypeFlag::ETS_TYPE_PARAMETER) {} - explicit ETSTypeParameter(Type *assembler_type) - : Type(TypeFlag::ETS_TYPE_PARAMETER), assembler_type_(assembler_type) + explicit ETSTypeParameter(Type *default_type, Type *constraint_type) + : Type(TypeFlag::ETS_TYPE_PARAMETER), default_(default_type), constraint_(constraint_type) { } - void SetType(Type *type) + void SetDeclNode(ir::TSTypeParameter *decl) { - type_ = type; + decl_node_ = decl; } - Type *GetType() + ir::TSTypeParameter *GetDeclNode() const { - return type_; + return decl_node_; } - Type *GetAssemblerType() + ETSTypeParameter *GetOriginal() const; + + void SetDefaultType(Type *type) + { + default_ = type; + } + + Type *GetDefaultType() const + { + return default_; + } + + void SetConstraintType(Type *type) { - return assembler_type_; + constraint_ = type; } - Type **GetTypeRef() + Type *GetConstraintType() const { - return &type_; + return constraint_; } - Type **GetAssemblerTypeRef() + bool HasConstraint() const { - return &assembler_type_; + return GetConstraintType() != nullptr; } + Type *EffectiveConstraint(ETSChecker const *checker) const; + 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; + void Cast(TypeRelation *relation, Type *target) override; + void CastTarget(TypeRelation *relation, Type *source) override; + void IsSupertypeOf(TypeRelation *relation, Type *source) override; + Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *global_types) override; + Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; + + bool ConstraintIsSubtypeOf(TypeRelation *relation, Type *target) + { + if (HasConstraint()) { + target->IsSupertypeOf(relation, GetConstraintType()); + } else { + relation->Result(false); + } + return relation->IsTrue(); + } + + void ToAssemblerType(std::stringstream &ss) const override; + void ToDebugInfoType(std::stringstream &ss) const override; private: - Type *type_ {}; - Type *assembler_type_ {}; + ir::TSTypeParameter *decl_node_ {}; + Type *default_ {}; + Type *constraint_ {}; }; } // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index fe46cd9343aab1e6b434314715e6b1e6eef94e0c..18d2fcbde0ba9cf8f44e3bc792424b230b417367 100644 --- a/ets2panda/checker/types/ets/etsUnionType.cpp +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -42,6 +42,13 @@ void ETSUnionType::ToDebugInfoType(std::stringstream &ss) const lub_type_->ToDebugInfoType(ss); } +ETSUnionType::ETSUnionType(ETSChecker *checker, ArenaVector &&constituent_types) + : Type(TypeFlag::ETS_UNION), constituent_types_(std::move(constituent_types)) +{ + ASSERT(constituent_types_.size() > 1); + lub_type_ = ComputeLUB(checker); +} + bool ETSUnionType::EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target) { return std::all_of(source->constituent_types_.begin(), source->constituent_types_.end(), @@ -54,23 +61,19 @@ bool ETSUnionType::TypeRelatedToSomeType(TypeRelation *relation, Type *source, E [relation, source](auto *t) { return relation->IsIdenticalTo(source, t); }); } -void ETSUnionType::SetLeastUpperBoundType(ETSChecker *checker) +Type *ETSUnionType::ComputeLUB(ETSChecker *checker) const { - 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; - } - if (t->IsETSObjectType() && t->AsETSObjectType()->SuperType() == nullptr) { - lub_type_ = checker->GetGlobalTypesHolder()->GlobalETSObjectType(); - return; - } - lub_type_ = checker->FindLeastUpperBound(lub_type_, t); + auto lub = constituent_types_.front(); + for (auto *t : constituent_types_) { + if (!checker->IsReferenceType(t)) { + return checker->GetGlobalTypesHolder()->GlobalETSObjectType(); + } + if (t->IsETSObjectType() && t->AsETSObjectType()->SuperType() == nullptr) { + return checker->GetGlobalTypesHolder()->GlobalETSObjectType(); } + lub = checker->FindLeastUpperBound(lub, t); } + return lub; } void ETSUnionType::Identical(TypeRelation *relation, Type *other) @@ -94,8 +97,7 @@ bool ETSUnionType::AssignmentSource(TypeRelation *relation, Type *target) } } - relation->Result(true); - return true; + return relation->Result(true); } void ETSUnionType::AssignmentTarget(TypeRelation *relation, Type *source) @@ -213,17 +215,6 @@ void ETSUnionType::NormalizeTypes(TypeRelation *relation, ArenaVector &c } } -Type *ETSUnionType::HandleUnionType([[maybe_unused]] TypeRelation *relation, ETSUnionType *union_type) -{ - NormalizeTypes(relation, union_type->constituent_types_); - - 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(allocator->Adapter()); @@ -234,14 +225,12 @@ Type *ETSUnionType::Instantiate(ArenaAllocator *allocator, TypeRelation *relatio : it->Instantiate(allocator, relation, global_types)); } + ETSUnionType::NormalizeTypes(relation, copied_constituents); if (copied_constituents.size() == 1) { return copied_constituents[0]; } - auto *new_union_type = allocator->New(std::move(copied_constituents)); - - new_union_type->SetLeastUpperBoundType(relation->GetChecker()->AsETSChecker()); - return HandleUnionType(relation, new_union_type); + return allocator->New(relation->GetChecker()->AsETSChecker(), std::move(copied_constituents)); } Type *ETSUnionType::Substitute(TypeRelation *relation, const Substitution *substitution) @@ -259,21 +248,21 @@ void ETSUnionType::Cast(TypeRelation *relation, Type *target) auto *const checker = relation->GetChecker()->AsETSChecker(); auto *const ref_target = target->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE) ? checker->PrimitiveTypeAsETSBuiltinType(target) : target; - auto exact_type = std::find_if(constituent_types_.begin(), constituent_types_.end(), - [this, checker, relation, ref_target](Type *src) { - if (src == ref_target && relation->IsCastableTo(src, ref_target)) { - GetLeastUpperBoundType(checker)->Cast(relation, ref_target); - ASSERT(relation->IsTrue()); - return true; - } - return false; - }); + auto exact_type = + std::find_if(constituent_types_.begin(), constituent_types_.end(), [this, relation, ref_target](Type *src) { + if (src == ref_target && relation->IsCastableTo(src, ref_target)) { + GetLeastUpperBoundType()->Cast(relation, ref_target); + ASSERT(relation->IsTrue()); + return true; + } + return false; + }); if (exact_type != constituent_types_.end()) { return; } for (auto *source : constituent_types_) { if (relation->IsCastableTo(source, ref_target)) { - GetLeastUpperBoundType(checker)->Cast(relation, ref_target); + GetLeastUpperBoundType()->Cast(relation, ref_target); ASSERT(relation->IsTrue()); if (ref_target != target) { source->Cast(relation, target); @@ -294,6 +283,31 @@ void ETSUnionType::Cast(TypeRelation *relation, Type *target) conversion::Forbidden(relation); } +void ETSUnionType::IsSupertypeOf(TypeRelation *relation, Type *source) +{ + relation->Result(false); + + if (source->IsETSUnionType()) { + for (auto const &source_ctype : source->AsETSUnionType()->ConstituentTypes()) { + if (IsSupertypeOf(relation, source_ctype), !relation->IsTrue()) { + return; + } + } + return; + } + + for (auto const &ctype : ConstituentTypes()) { + if (ctype->IsSupertypeOf(relation, source), relation->IsTrue()) { + return; + } + } + + if (source->IsETSTypeParameter()) { + source->AsETSTypeParameter()->ConstraintIsSubtypeOf(relation, this); + return; + } +} + void ETSUnionType::CastTarget(TypeRelation *relation, Type *source) { Type *target_type = FindTypeIsCastableToThis(relation->GetNode(), relation, source); diff --git a/ets2panda/checker/types/ets/etsUnionType.h b/ets2panda/checker/types/ets/etsUnionType.h index 93091f7575b36324e23fd1b3d874c60c0e5499ea..39dc90ec1f4f6df012d752577675c16c5dbf8fd2 100644 --- a/ets2panda/checker/types/ets/etsUnionType.h +++ b/ets2panda/checker/types/ets/etsUnionType.h @@ -24,42 +24,14 @@ 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) - { - } + // constituent_types must be normalized + explicit ETSUnionType(ETSChecker *checker, ArenaVector &&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 ToAssemblerType(std::stringstream &ss) const override; void ToDebugInfoType(std::stringstream &ss) const override; @@ -70,21 +42,11 @@ public: Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; void CastTarget(TypeRelation *relation, Type *source) override; + void IsSupertypeOf(TypeRelation *relation, Type *source) override; Type *FindTypeIsCastableToThis(ir::Expression *node, TypeRelation *relation, Type *source) const; Type *FindTypeIsCastableToSomeType(ir::Expression *node, TypeRelation *relation, Type *target) const; Type *FindUnboxableType() const; - void SetLeastUpperBoundType(ETSChecker *checker); - - Type *GetLeastUpperBoundType(ETSChecker *checker) - { - if (lub_type_ == nullptr) { - SetLeastUpperBoundType(checker); - } - ASSERT(lub_type_ != nullptr); - return lub_type_; - } - Type *GetLeastUpperBoundType() const { ASSERT(lub_type_ != nullptr); @@ -97,11 +59,9 @@ public: static void NormalizeTypes(TypeRelation *relation, ArenaVector &constituent_types); - static Type *HandleUnionType(TypeRelation *relation, ETSUnionType *union_type); - std::tuple ResolveConditionExpr() const override { - for (auto tp : ConstituentTypes()) { + for (auto const &tp : ConstituentTypes()) { if (!tp->IsConditionalExprType()) { return {true, false}; } @@ -115,7 +75,9 @@ private: static void LinearizeAndEraseIdentical(TypeRelation *relation, ArenaVector &constituent_types); - ArenaVector constituent_types_; + Type *ComputeLUB(ETSChecker *checker) const; + + ArenaVector const constituent_types_; Type *lub_type_ {nullptr}; }; } // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/ets/floatType.cpp b/ets2panda/checker/types/ets/floatType.cpp index 474118265a25675184e80ca2f7c38cf21a58b47a..68cb1421e5ccdc167a3bc2bcb713b80d821c4d7f 100644 --- a/ets2panda/checker/types/ets/floatType.cpp +++ b/ets2panda/checker/types/ets/floatType.cpp @@ -74,6 +74,16 @@ void FloatType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/ets/intType.cpp b/ets2panda/checker/types/ets/intType.cpp index b9dc7c2e2f1b2caac29623412bc1aafdfa787aea..fdb4424f73af9e88d3f9626a3a9d9edf1b71f96b 100644 --- a/ets2panda/checker/types/ets/intType.cpp +++ b/ets2panda/checker/types/ets/intType.cpp @@ -79,6 +79,16 @@ void IntType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/ets/longType.cpp b/ets2panda/checker/types/ets/longType.cpp index abb5df301e8e9f9564367d32b868fbc8e53f3354..aa2098849c9f12aafdbe35c1ac7ba6b2f74f4eb9 100644 --- a/ets2panda/checker/types/ets/longType.cpp +++ b/ets2panda/checker/types/ets/longType.cpp @@ -74,6 +74,16 @@ void LongType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/ets/shortType.cpp b/ets2panda/checker/types/ets/shortType.cpp index a43783d20f14275bf95a81473c68e307778d49f2..041f767dc049d4c47414646514b5fe281cc10c94 100644 --- a/ets2panda/checker/types/ets/shortType.cpp +++ b/ets2panda/checker/types/ets/shortType.cpp @@ -74,6 +74,16 @@ void ShortType::Cast(TypeRelation *const relation, Type *const target) } if (target->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::BUILTIN_TYPE)) { + auto unboxed_target = relation->GetChecker()->AsETSChecker()->ETSBuiltinTypeAsPrimitiveType(target); + if (unboxed_target == nullptr) { + conversion::Forbidden(relation); + return; + } + Cast(relation, unboxed_target); + if (relation->IsTrue()) { + conversion::Boxing(relation, unboxed_target); + return; + } conversion::Forbidden(relation); return; } diff --git a/ets2panda/checker/types/globalTypesHolder.cpp b/ets2panda/checker/types/globalTypesHolder.cpp index 50e7839a5381e8ce88c47fff7ca579ffca7e1483..5c6eb9a07bda67acea569de3d9e42d559e285f07 100644 --- a/ets2panda/checker/types/globalTypesHolder.cpp +++ b/ets2panda/checker/types/globalTypesHolder.cpp @@ -346,6 +346,11 @@ Type *GlobalTypesHolder::GlobalETSUndefinedType() return global_types_.at(static_cast(GlobalTypeId::ETS_UNDEFINED)); } +Type *GlobalTypesHolder::GlobalETSNullishObjectType() +{ + return global_types_.at(static_cast(GlobalTypeId::ETS_NULLISH_OBJECT)); +} + Type *GlobalTypesHolder::GlobalWildcardType() { return global_types_.at(static_cast(GlobalTypeId::ETS_WILDCARD)); diff --git a/ets2panda/checker/types/globalTypesHolder.h b/ets2panda/checker/types/globalTypesHolder.h index e84ae6ad353511525e38813af7258bab5fe4e1af..e0e370b4599bf17a0dd767f591e32da91d7d1b7a 100644 --- a/ets2panda/checker/types/globalTypesHolder.h +++ b/ets2panda/checker/types/globalTypesHolder.h @@ -57,6 +57,7 @@ enum class GlobalTypeId { ETS_OBJECT_BUILTIN, ETS_NULL, ETS_UNDEFINED, + ETS_NULLISH_OBJECT, ETS_WILDCARD, ETS_BOOLEAN_BUILTIN, ETS_BYTE_BUILTIN, @@ -159,6 +160,7 @@ public: Type *GlobalETSObjectType(); Type *GlobalETSNullType(); Type *GlobalETSUndefinedType(); + Type *GlobalETSNullishObjectType(); Type *GlobalWildcardType(); Type *GlobalETSBooleanBuiltinType(); Type *GlobalByteBuiltinType(); diff --git a/ets2panda/checker/types/signature.cpp b/ets2panda/checker/types/signature.cpp index 8bb01344d60925b768eb5867d7d7b09097cdfe10..f04fd009eee1822ee6cf536f39374bbd140257b0 100644 --- a/ets2panda/checker/types/signature.cpp +++ b/ets2panda/checker/types/signature.cpp @@ -37,6 +37,7 @@ Signature *Signature::Substitute(TypeRelation *relation, const Substitution *sub bool any_change = false; SignatureInfo *new_sig_info = allocator->New(allocator); const Substitution *new_substitution = substitution; + if (!signature_info_->type_params.empty()) { auto *new_substitution_seed = checker->CopySubstitution(substitution); for (auto *tparam : signature_info_->type_params) { @@ -44,12 +45,13 @@ Signature *Signature::Substitute(TypeRelation *relation, const Substitution *sub new_sig_info->type_params.push_back(new_tparam); if (new_tparam != tparam) { any_change = true; - new_substitution_seed->insert({tparam, new_tparam}); + if (tparam->IsETSTypeParameter()) { + new_substitution_seed->insert({tparam->AsETSTypeParameter(), new_tparam}); + } } } new_substitution = new_substitution_seed; } - new_sig_info->min_arg_count = signature_info_->min_arg_count; for (auto *param : signature_info_->params) { @@ -84,12 +86,13 @@ Signature *Signature::Substitute(TypeRelation *relation, const Substitution *sub if (!any_change) { return this; } - auto *result = allocator->New(new_sig_info, new_return_type, this); + auto *result = allocator->New(new_sig_info, new_return_type); result->func_ = func_; result->flags_ = flags_; result->internal_name_ = internal_name_; result->owner_obj_ = owner_obj_; result->owner_var_ = owner_var_; + return result; } @@ -342,12 +345,4 @@ void Signature::AssignmentTarget(TypeRelation *relation, Signature *source) relation->IsAssignableTo(source->RestVar()->TsType(), signature_info_->rest_var->TsType()); } } - -bool Signature::IsBaseReturnDiff() const -{ - if (base_sig_ == nullptr) { - return false; - } - return ReturnType()->ToAssemblerName().str() != base_sig_->ReturnType()->ToAssemblerName().str(); -} } // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/signature.h b/ets2panda/checker/types/signature.h index 220eda8fee4d580c2a53327bdaf5c3bc2b84bd83..94e7b78c32a3aa9f753161de83c8fc47a2044a89 100644 --- a/ets2panda/checker/types/signature.h +++ b/ets2panda/checker/types/signature.h @@ -79,6 +79,7 @@ enum class SignatureFlags : uint32_t { INTERNAL = 1U << 12U, NEED_RETURN_TYPE = 1U << 13U, INFERRED_RETURN_TYPE = 1U << 14U, + THIS_RETURN_TYPE = 1U << 15U, GETTER = 1U << 16U, SETTER = 1U << 17U, @@ -91,8 +92,8 @@ DEFINE_BITOPS(SignatureFlags) class Signature { public: - Signature(SignatureInfo *signature_info, Type *return_type, Signature *base_sig = nullptr) - : signature_info_(signature_info), return_type_(return_type), base_sig_(base_sig) + Signature(SignatureInfo *signature_info, Type *return_type) + : signature_info_(signature_info), return_type_(return_type) { } @@ -223,8 +224,6 @@ public: return (flags_ & flag) != 0U; } - bool IsBaseReturnDiff() const; - bool IsFinal() const noexcept { return HasSignatureFlag(SignatureFlags::FINAL); @@ -262,7 +261,6 @@ private: util::StringView internal_name_ {}; ETSObjectType *owner_obj_ {}; varbinder::Variable *owner_var_ {}; - const Signature *base_sig_ = nullptr; }; } // namespace panda::es2panda::checker diff --git a/ets2panda/checker/types/type.h b/ets2panda/checker/types/type.h index 5f1b44af2e8723c0adc7bb1511ab4afc3d211839..3f5185007d92099a711b83a8c63a20fbb94a2f95 100644 --- a/ets2panda/checker/types/type.h +++ b/ets2panda/checker/types/type.h @@ -36,6 +36,7 @@ class ETSDynamicType; class ETSAsyncFuncReturnType; class ETSChecker; class ETSDynamicFunctionType; +class ETSTypeParameter; // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define DECLARE_TYPENAMES(typeFlag, typeName) class typeName; @@ -43,7 +44,7 @@ TYPE_MAPPING(DECLARE_TYPENAMES) #undef DECLARE_TYPENAMES class ETSStringType; -using Substitution = ArenaMap; +using Substitution = ArenaMap; class Type { public: diff --git a/ets2panda/checker/types/typeFlag.h b/ets2panda/checker/types/typeFlag.h index cfa698c62dbe3df0b840da3e2ef2cf1e088ce783..e4a973560db05f2f57ddb73cdd31c2478d82762e 100644 --- a/ets2panda/checker/types/typeFlag.h +++ b/ets2panda/checker/types/typeFlag.h @@ -84,7 +84,7 @@ enum class TypeFlag : uint64_t { 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 | - WILDCARD | ETS_TYPE_PARAMETER | ETS_ENUM | ETS_STRING_ENUM | ETS_DYNAMIC_TYPE, + WILDCARD | ETS_TYPE_PARAMETER | ETS_ENUM | ETS_STRING_ENUM | ETS_DYNAMIC_TYPE | ETS_UNION, ETS_PRIMITIVE = BYTE | SHORT | INT | LONG | FLOAT | DOUBLE | CHAR | ETS_BOOLEAN | ETS_VOID, ETS_PRIMITIVE_RETURN = BYTE | SHORT | INT | LONG | FLOAT | DOUBLE | CHAR | ETS_BOOLEAN | ETS_ENUM, ETS_ARRAY_INDEX = BYTE | SHORT | INT, diff --git a/ets2panda/checker/types/typeRelation.h b/ets2panda/checker/types/typeRelation.h index 6abd143d4c52736ba20e059c413848b2d43b62bb..4e7cff3141aac5328cba1a678fd29c4e862c2375 100644 --- a/ets2panda/checker/types/typeRelation.h +++ b/ets2panda/checker/types/typeRelation.h @@ -61,6 +61,7 @@ enum class TypeRelationFlag : uint32_t { IGNORE_TYPE_PARAMETERS = 1U << 20U, CHECK_PROXY = 1U << 21U, NO_CHECK_TRAILING_LAMBDA = 1U << 23U, + NO_THROW_GENERIC_TYPEALIAS = 1U << 24U, ASSIGNMENT_CONTEXT = WIDENING | BOXING | UNBOXING, CASTING_CONTEXT = NARROWING | WIDENING | BOXING | UNBOXING | UNCHECKED_CAST, @@ -201,6 +202,11 @@ public: return (flags_ & TypeRelationFlag::UNCHECKED_CAST) != 0; } + [[nodiscard]] bool NoThrowGenericTypeAlias() const noexcept + { + return (flags_ & TypeRelationFlag::NO_THROW_GENERIC_TYPEALIAS) != 0; + } + const Checker *GetChecker() const { return checker_; @@ -262,9 +268,10 @@ public: void RaiseError(const std::string &err_msg, const lexer::SourcePosition &loc) const; void RaiseError(std::initializer_list list, const lexer::SourcePosition &loc) const; - void Result(bool res) + bool Result(bool res) { result_ = res ? RelationResult::TRUE : RelationResult::FALSE; + return res; } void Result(RelationResult res) diff --git a/ets2panda/compiler/base/lreference.cpp b/ets2panda/compiler/base/lreference.cpp index 86deaaf53d58ec78db92f5c10b8d571c3959069b..b118fdbebb39955053bcff2b2dcbb00eedb8da14 100644 --- a/ets2panda/compiler/base/lreference.cpp +++ b/ets2panda/compiler/base/lreference.cpp @@ -362,10 +362,6 @@ void ETSLReference::SetValue() const const 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); } diff --git a/ets2panda/compiler/core/ASTVerifier.cpp b/ets2panda/compiler/core/ASTVerifier.cpp index 3b7f52d49dd4babf33cd76494935f0019b27fb5d..17265d29955d85811dbc48a5912b92756397b86b 100644 --- a/ets2panda/compiler/core/ASTVerifier.cpp +++ b/ets2panda/compiler/core/ASTVerifier.cpp @@ -18,6 +18,8 @@ #include "checker/types/typeFlag.h" #include "ir/astNode.h" #include "ir/base/classDefinition.h" +#include "ir/base/classElement.h" +#include "ir/statement.h" #include "ir/base/classStaticBlock.h" #include "ir/base/methodDefinition.h" #include "ir/base/scriptFunction.h" @@ -27,6 +29,7 @@ #include "ir/ets/etsTypeReference.h" #include "ir/ets/etsTypeReferencePart.h" #include "ir/ets/etsImportDeclaration.h" +#include "ir/ets/etsScript.h" #include "ir/module/importSpecifier.h" #include "ir/module/importNamespaceSpecifier.h" #include "ir/module/importDefaultSpecifier.h" @@ -120,23 +123,39 @@ bool IsContainedIn(const T *child, const T *parent) std::unordered_set saved_nodes; while (child != nullptr && child != parent) { + saved_nodes.emplace(child); + child = child->Parent(); if (saved_nodes.find(child) != saved_nodes.end()) { return false; } - child = child->Parent(); - saved_nodes.emplace(child); } return child == parent; } - +bool IsVisibleInternalNode(const ir::AstNode *ast, const ir::AstNode *obj_type_decl_node) +{ + auto *current_top_statement = (static_cast(ast->GetTopStatement())); + auto *current_program = current_top_statement->Program(); + if (current_program == nullptr) { + return false; + } + util::StringView package_name_current = current_program->GetPackageName(); + auto *object_top_statement = (static_cast(obj_type_decl_node->GetTopStatement())); + auto *object_program = object_top_statement->Program(); + if (object_program == nullptr) { + return false; + } + util::StringView package_name_object = object_program->GetPackageName(); + return current_top_statement == object_top_statement || + (package_name_current == package_name_object && !package_name_current.Empty()); +} bool ValidateVariableAccess(const varbinder::LocalVariable *prop_var, const ir::MemberExpression *ast) { - const auto *decl = prop_var->Declaration(); - if (decl == nullptr) { + const auto *prop_var_decl = prop_var->Declaration(); + if (prop_var_decl == nullptr) { return false; } - const auto *node = decl->Node(); - if (node == nullptr) { + const auto *prop_var_decl_node = prop_var_decl->Node(); + if (prop_var_decl_node == nullptr) { return false; } auto *obj_type = ast->ObjType(); @@ -147,36 +166,43 @@ bool ValidateVariableAccess(const varbinder::LocalVariable *prop_var, const ir:: if (obj_type_decl_node == nullptr) { return false; } - const auto *parent_node = node->Parent(); - if (parent_node != nullptr && parent_node->IsClassDefinition() && obj_type_decl_node->IsClassDefinition()) { - if (IsContainedIn(ast, obj_type_decl_node->AsClassDefinition())) { + const auto *prop_var_decl_node_parent = prop_var_decl_node->Parent(); + if (prop_var_decl_node_parent != nullptr && prop_var_decl_node_parent->IsClassDefinition() && + obj_type_decl_node->IsClassDefinition()) { + // Check if the variable is used where it is declared + if (IsContainedIn(ast, prop_var_decl_node_parent->AsClassDefinition())) { return true; } - if (node->IsPrivate() && parent_node == obj_type_decl_node) { - return true; + if (prop_var_decl_node->IsPrivate()) { + return false; } - if (node->IsProtected()) { + if (prop_var_decl_node->IsProtected()) { + // Check if the variable is inherited and is used in class in which it is inherited auto ret = obj_type->IsPropertyInherited(prop_var); - return ret; + return ret && IsContainedIn(ast, obj_type_decl_node->AsClassDefinition()); + } + if (prop_var_decl_node->IsInternal()) { + return IsVisibleInternalNode(ast, obj_type_decl_node); } + return true; } return false; } bool ValidateMethodAccess(const ir::MemberExpression *member_expression, const ir::CallExpression *ast) { - auto *obj_type = member_expression->ObjType(); - if (obj_type == nullptr) { + auto *member_obj_type = member_expression->ObjType(); + if (member_obj_type == nullptr) { return false; } - - if (obj_type->HasObjectFlag(checker::ETSObjectFlags::RESOLVED_SUPER) && obj_type->SuperType() != nullptr && - obj_type->SuperType()->HasObjectFlag(checker::ETSObjectFlags::BUILTIN_TYPE | - checker::ETSObjectFlags::GLOBAL_CLASS)) { + if (member_obj_type->HasObjectFlag(checker::ETSObjectFlags::RESOLVED_SUPER) && + member_obj_type->SuperType() != nullptr && + member_obj_type->SuperType()->HasObjectFlag(checker::ETSObjectFlags::BUILTIN_TYPE | + checker::ETSObjectFlags::GLOBAL)) { return true; } - const auto *decl_node = obj_type->GetDeclNode(); - if (decl_node == nullptr) { + const auto *member_obj_type_decl_node = member_obj_type->GetDeclNode(); + if (member_obj_type_decl_node == nullptr) { return false; } auto *signature = ast->Signature(); @@ -187,18 +213,25 @@ bool ValidateMethodAccess(const ir::MemberExpression *member_expression, const i if (owner_sign == nullptr) { return false; } - auto *decl_node_sign = owner_sign->GetDeclNode(); - if (decl_node_sign != nullptr && decl_node_sign->IsClassDefinition() && decl_node->IsClassDefinition()) { - if (IsContainedIn(ast, decl_node->AsClassDefinition())) { + auto *owner_sign_decl_node = owner_sign->GetDeclNode(); + if (owner_sign_decl_node != nullptr && owner_sign_decl_node->IsClassDefinition() && + member_obj_type_decl_node->IsClassDefinition()) { + // Check if the method is used where it is declared + if (IsContainedIn(ast, owner_sign_decl_node->AsClassDefinition())) { return true; } - if (signature->HasSignatureFlag(checker::SignatureFlags::PRIVATE) && decl_node_sign == decl_node) { - return true; + if (signature->HasSignatureFlag(checker::SignatureFlags::PRIVATE)) { + return false; } if (signature->HasSignatureFlag(checker::SignatureFlags::PROTECTED)) { - auto ret = obj_type->IsSignatureInherited(signature); - return ret; + // Check if the method is inherited and is used in class in which it is inherited + auto ret = member_obj_type->IsSignatureInherited(signature); + return ret && IsContainedIn(ast, member_obj_type_decl_node->AsClassDefinition()); } + if (signature->HasSignatureFlag(checker::SignatureFlags::INTERNAL)) { + return IsVisibleInternalNode(ast, member_obj_type_decl_node); + } + return true; } return false; } @@ -326,6 +359,18 @@ std::string ToStringParamsHelper(const ir::AstNode *parent, const ArenaVector +std::string ToStringParamsHelper(const ArenaVector ¶ms) +{ + std::string name = "("; + + for (auto const *param : params) { + name += ToStringHelper(param); + } + + return name + ")"; +} + std::string ToStringHelper(const ir::AstNode *ast) { if (ast == nullptr) { @@ -425,8 +470,7 @@ std::string ToStringHelper(const ir::AstNode *ast) return "TS_INTERFACE_BODY "; } case ir::AstNodeType::ETS_FUNCTION_TYPE: { - return "ETS_FUNC_TYPE " + - ToStringParamsHelper(ast->Parent(), ast->AsETSFunctionType()->Params()); + return "ETS_FUNC_TYPE " + ToStringParamsHelper(ast->AsETSFunctionType()->Params()); } case ir::AstNodeType::TS_CLASS_IMPLEMENTS: { return "TS_CLASS_IMPL " + ToStringHelper(ast->AsTSClassImplements()->Expr()); @@ -516,7 +560,7 @@ std::optional ASTVerifier::GetLocalScopeVariable(con bool ASTVerifier::VerifyChildNode(const ir::AstNode *ast) { ASSERT(ast); - bool is_ok; + bool is_ok = true; ast->Iterate([&](const auto node) { if (ast != node->Parent()) { AddError("INCORRECT_PARENT_REF: " + ToStringHelper(node), node->Start()); @@ -689,8 +733,8 @@ bool ASTVerifier::VerifyModifierAccess(const ir::AstNode *ast) } if (ast->IsMemberExpression()) { const auto *prop_var = ast->AsMemberExpression()->PropVar(); - if (prop_var != nullptr && prop_var->HasFlag(varbinder::VariableFlags::PROPERTY) && - !ValidateVariableAccess(prop_var, ast->AsMemberExpression())) { + if (prop_var == nullptr || (prop_var->HasFlag(varbinder::VariableFlags::PROPERTY) && + !ValidateVariableAccess(prop_var, ast->AsMemberExpression()))) { AddError("PROPERTY_NOT_VISIBLE_HERE: " + ToStringHelper(ast), ast->Start()); return false; } @@ -698,11 +742,14 @@ bool ASTVerifier::VerifyModifierAccess(const ir::AstNode *ast) if (ast->IsCallExpression()) { const auto *call_expr = ast->AsCallExpression(); const auto *callee = call_expr->Callee(); - if (callee != nullptr && callee->IsMemberExpression()) { + if (callee == nullptr) { + return false; + } + if (callee->IsMemberExpression()) { const auto *callee_member = callee->AsMemberExpression(); const auto *prop_var_callee = callee_member->PropVar(); - if (prop_var_callee != nullptr && prop_var_callee->HasFlag(varbinder::VariableFlags::METHOD) && - !ValidateMethodAccess(callee_member, ast->AsCallExpression())) { + if (prop_var_callee == nullptr || (prop_var_callee->HasFlag(varbinder::VariableFlags::METHOD) && + !ValidateMethodAccess(callee_member, ast->AsCallExpression()))) { AddError("PROPERTY_NOT_VISIBLE_HERE: " + ToStringHelper(callee), callee->Start()); return false; } diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index de21cbc011f95ba968be23e83fbb8290d0cdf323..cb19f1672057b3172208d3e677381d77dccad41a 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -23,6 +23,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/switchBuilder.h" #include "compiler/function/functionBuilder.h" + namespace panda::es2panda::compiler { ETSGen *ETSCompiler::GetETSGen() const @@ -221,6 +222,31 @@ void ETSCompiler::Compile(const ir::ETSNewArrayInstanceExpression *expr) const compiler::VReg dim = etsg->AllocReg(); etsg->ApplyConversionAndStoreAccumulator(expr, dim, expr->dimension_->TsType()); etsg->NewArray(expr, arr, dim, expr->TsType()); + + if (expr->default_constructor_signature_ != nullptr) { + compiler::VReg count_reg = etsg->AllocReg(); + auto *start_label = etsg->AllocLabel(); + auto *end_label = etsg->AllocLabel(); + etsg->MoveImmediateToRegister(expr, count_reg, checker::TypeFlag::INT, static_cast(0)); + const auto index_reg = etsg->AllocReg(); + + etsg->SetLabel(expr, start_label); + etsg->LoadAccumulator(expr, dim); + etsg->JumpCompareRegister(expr, count_reg, end_label); + + etsg->LoadAccumulator(expr, count_reg); + etsg->StoreAccumulator(expr, index_reg); + const compiler::TargetTypeContext ttctx2(etsg, expr->type_reference_->TsType()); + ArenaVector arguments(expr->allocator_->Adapter()); + etsg->InitObject(expr, expr->default_constructor_signature_, arguments); + etsg->StoreArrayElement(expr, arr, index_reg, expr->type_reference_->TsType()); + + etsg->IncrementImmediateRegister(expr, count_reg, checker::TypeFlag::INT, static_cast(1)); + etsg->JumpTo(expr, start_label); + + etsg->SetLabel(expr, end_label); + } + etsg->SetVRegType(arr, expr->TsType()); etsg->LoadAccumulator(expr, arr); } @@ -431,7 +457,7 @@ static void CompileNullishCoalescing(compiler::ETSGen *etsg, ir::BinaryExpressio compile_operand(node->Left()); - if (!node->Left()->TsType()->IsNullishOrNullLike()) { + if (!etsg->Checker()->MayHaveNulllikeValue(node->Left()->TsType())) { // fallthrough } else if (node->Left()->TsType()->IsETSNullLike()) { compile_operand(node->Right()); @@ -682,7 +708,8 @@ void ETSCompiler::EmitCall(const ir::CallExpression *expr, compiler::VReg &calle } else { etsg->CallThisVirtual(expr, callee_reg, expr->Signature(), expr->Arguments()); } - etsg->SetAccumulatorType(expr->TsType()); + + etsg->GuardUncheckedType(expr, expr->UncheckedType(), expr->OptionalType()); } void ETSCompiler::Compile(const ir::CallExpression *expr) const @@ -822,7 +849,7 @@ static bool CompileComputed(compiler::ETSGen *etsg, const ir::MemberExpression * } if (expr->Object()->TsType()->IsETSTupleType() && (expr->GetTupleConvertedType() != nullptr)) { - etsg->EmitCheckedNarrowingReferenceConversion(expr, expr->GetTupleConvertedType()); + etsg->InternalCheckCast(expr, expr->GetTupleConvertedType()); } etsg->ApplyConversion(expr); @@ -846,7 +873,8 @@ void ETSCompiler::Compile(const ir::MemberExpression *expr) const compiler::RegScope rs(etsg); - auto *const object_type = etsg->Checker()->GetNonNullishType(expr->Object()->TsType()); + auto *const object_type = + checker::ETSChecker::GetApparentType(etsg->Checker()->GetNonNullishType(expr->Object()->TsType())); if (CompileComputed(etsg, expr)) { return; @@ -913,15 +941,15 @@ void ETSCompiler::Compile(const ir::MemberExpression *expr) const if (expr->PropVar()->TsType()->HasTypeFlag(checker::TypeFlag::GETTER_SETTER)) { checker::Signature *sig = expr->PropVar()->TsType()->AsETSFunctionType()->FindGetter(); etsg->CallThisVirtual0(expr, obj_reg, sig->InternalName()); - etsg->SetAccumulatorType(expr->TsType()); } else if (object_type->IsETSDynamicType()) { etsg->LoadPropertyDynamic(expr, expr->OptionalType(), obj_reg, prop_name); } else if (object_type->IsETSUnionType()) { - etsg->LoadUnionProperty(expr, expr->OptionalType(), expr->IsGenericField(), obj_reg, prop_name); + etsg->LoadUnionProperty(expr, expr->OptionalType(), obj_reg, prop_name); } else { const auto full_name = etsg->FormClassPropReference(object_type->AsETSObjectType(), prop_name); - etsg->LoadProperty(expr, expr->OptionalType(), expr->IsGenericField(), obj_reg, full_name); + etsg->LoadProperty(expr, expr->OptionalType(), obj_reg, full_name); } + etsg->GuardUncheckedType(expr, expr->UncheckedType(), expr->OptionalType()); }; etsg->EmitMaybeOptional(expr, load_property, expr->IsOptional()); @@ -1678,9 +1706,61 @@ void ETSCompiler::Compile(const ir::TSAsExpression *expr) const expr->Expr()->Compile(etsg); } - etsg->ApplyConversion(expr->Expr(), nullptr); - auto *target_type = expr->TsType(); + + if ((expr->Expr()->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG) != 0U) { + etsg->ApplyUnboxingConversion(expr->Expr()); + } + + if (target_type->IsETSObjectType() && + ((expr->Expr()->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG) != 0U || + (expr->Expr()->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG) != 0U) && + checker::ETSChecker::TypeKind(etsg->GetAccumulatorType()) != checker::TypeFlag::ETS_OBJECT) { + if (target_type->AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::UNBOXABLE_TYPE)) { + switch (target_type->AsETSObjectType()->BuiltInKind()) { + case checker::ETSObjectFlags::BUILTIN_BOOLEAN: { + etsg->CastToBoolean(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_BYTE: { + etsg->CastToChar(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_CHAR: { + etsg->CastToByte(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_SHORT: { + etsg->CastToShort(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_INT: { + etsg->CastToInt(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_LONG: { + etsg->CastToLong(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_FLOAT: { + etsg->CastToFloat(expr); + break; + } + case checker::ETSObjectFlags::BUILTIN_DOUBLE: { + etsg->CastToDouble(expr); + break; + } + default: { + UNREACHABLE(); + } + } + } + } + + if ((expr->Expr()->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG) != 0U) { + etsg->ApplyBoxingConversion(expr->Expr()); + } + if (target_type->IsETSUnionType()) { target_type = target_type->AsETSUnionType()->FindTypeIsCastableToThis( expr->expression_, etsg->Checker()->Relation(), expr->expression_->TsType()); @@ -1719,7 +1799,8 @@ void ETSCompiler::Compile(const ir::TSAsExpression *expr) const break; } case checker::TypeFlag::ETS_ARRAY: - case checker::TypeFlag::ETS_OBJECT: { + case checker::TypeFlag::ETS_OBJECT: + case checker::TypeFlag::ETS_TYPE_PARAMETER: { etsg->CastToArrayOrObject(expr, target_type, expr->is_unchecked_cast_); break; } @@ -1867,7 +1948,7 @@ void ETSCompiler::Compile(const ir::TSNonNullExpression *expr) const expr->Expr()->Compile(etsg); - if (!etsg->GetAccumulatorType()->IsNullishOrNullLike()) { + if (!etsg->Checker()->MayHaveNulllikeValue(etsg->GetAccumulatorType())) { return; } diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 8d494b0dce72a0b92512bd0f1a04d4e82851cd12..f450d813cb9398f007a5cb177a02bf68eb7212b4 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -46,6 +46,9 @@ namespace panda::es2panda::compiler { +static constexpr auto TYPE_FLAG_BYTECODE_REF = + checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION | checker::TypeFlag::ETS_TYPE_PARAMETER; + ETSGen::ETSGen(ArenaAllocator *allocator, RegSpiller *spiller, CompilerContext *context, varbinder::FunctionScope *scope, ProgramElement *program_element, AstCompiler *astcompiler) noexcept : CodeGen(allocator, spiller, context, scope, program_element, astcompiler), @@ -74,7 +77,7 @@ void ETSGen::CompileAndCheck(const ir::Expression *expr) // This piece of code is necessary to handle multidimensional tuples. As a tuple is stored as an // array of `Objects`. If we make an array inside of the tuple type, then we won't be able to derefer a // 2 dimensional array, with an array that expects to return `Object` after index access. - EmitCheckedNarrowingReferenceConversion(expr, expr->TsType()); + CheckedReferenceNarrowing(expr, expr->TsType()); } auto const *const acc_type = GetAccumulatorType(); @@ -141,7 +144,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 | checker::TypeFlag::ETS_UNION)) { + if (acc_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Ra().Emit(node, vreg); } else if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, vreg); @@ -156,7 +159,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 | checker::TypeFlag::ETS_UNION)) { + if (vreg_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Ra().Emit(node, vreg); } else if (vreg_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, vreg); @@ -172,7 +175,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 | checker::TypeFlag::ETS_UNION)) { + if (source_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { return Allocator()->New(node, vd, vs); } if (source_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { @@ -210,7 +213,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 | checker::TypeFlag::ETS_UNION)) { + if (source_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Ra().Emit(node, vd, vs); } else if (source_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, vd, vs); @@ -300,7 +303,7 @@ void ETSGen::LoadVar(const ir::AstNode *node, varbinder::Variable const *const v } case ReferenceKind::FIELD: { const auto full_name = FormClassPropReference(GetVRegType(GetThisReg())->AsETSObjectType(), var->Name()); - LoadProperty(node, var->TsType(), false, GetThisReg(), full_name); + LoadProperty(node, var->TsType(), GetThisReg(), full_name); break; } case ReferenceKind::METHOD: @@ -392,7 +395,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 | checker::TypeFlag::ETS_UNION)) { + if (prop_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Sa().Emit(node, full_name); } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Sa().Emit(node, full_name); @@ -404,7 +407,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 | checker::TypeFlag::ETS_UNION)) { + if (prop_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Sa().Emit(node, full_name); } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Sa().Emit(node, full_name); @@ -423,7 +426,7 @@ void ETSGen::StoreProperty(const ir::AstNode *const node, const checker::Type *p if (node->IsIdentifier() && node->AsIdentifier()->Variable()->HasFlag(varbinder::VariableFlags::BOXED)) { prop_type = Checker()->GlobalBuiltinBoxType(prop_type); } - if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT)) { + if (prop_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Ra().Emit(node, obj_reg, full_name); } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, obj_reg, full_name); @@ -432,17 +435,14 @@ void ETSGen::StoreProperty(const ir::AstNode *const node, const checker::Type *p } } -void ETSGen::LoadProperty(const ir::AstNode *const node, const checker::Type *prop_type, bool is_generic, - const VReg obj_reg, const util::StringView &full_name) +void ETSGen::LoadProperty(const ir::AstNode *const node, const checker::Type *prop_type, const VReg obj_reg, + const util::StringView &full_name) { if (node->IsIdentifier() && node->AsIdentifier()->Variable()->HasFlag(varbinder::VariableFlags::BOXED)) { prop_type = Checker()->GlobalBuiltinBoxType(prop_type); } - if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { + if (prop_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Ra().Emit(node, obj_reg, full_name); - if (is_generic) { - EmitCheckCast(node, prop_type); - } } else if (prop_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Ra().Emit(node, obj_reg, full_name); } else { @@ -463,14 +463,11 @@ void ETSGen::StoreUnionProperty([[maybe_unused]] const ir::AstNode *node, [[mayb } void ETSGen::LoadUnionProperty([[maybe_unused]] const ir::AstNode *const node, - [[maybe_unused]] const checker::Type *prop_type, [[maybe_unused]] bool is_generic, - [[maybe_unused]] const VReg obj_reg, [[maybe_unused]] const util::StringView &prop_name) + [[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); - if (is_generic) { - EmitCheckCast(node, prop_type); - } SetAccumulatorType(prop_type); #else UNREACHABLE(); @@ -500,7 +497,7 @@ void ETSGen::StorePropertyDynamic(const ir::AstNode *node, const checker::Type * method_name = Signatures::Dynamic::SetPropertyDoubleBuiltin(lang); } else if (prop_type->IsETSStringType()) { method_name = Signatures::Dynamic::SetPropertyStringBuiltin(lang); - } else if (prop_type->IsETSObjectType()) { + } else if (prop_type->IsETSObjectType() || prop_type->IsETSTypeParameter()) { method_name = Signatures::Dynamic::SetPropertyDynamicBuiltin(lang); // NOTE: vpukhov. add non-dynamic builtin if (!prop_type->IsETSDynamicType()) { @@ -549,7 +546,7 @@ void ETSGen::LoadPropertyDynamic(const ir::AstNode *node, const checker::Type *p method_name = Signatures::Dynamic::GetPropertyDoubleBuiltin(lang); } else if (prop_type->IsETSStringType()) { method_name = Signatures::Dynamic::GetPropertyStringBuiltin(lang); - } else if (prop_type->IsETSObjectType()) { + } else if (prop_type->IsETSObjectType() || prop_type->IsETSTypeParameter()) { method_name = Signatures::Dynamic::GetPropertyDynamicBuiltin(lang); type = Checker()->GlobalBuiltinDynamicType(lang); } else { @@ -713,7 +710,7 @@ void ETSGen::LoadDefaultValue([[maybe_unused]] const ir::AstNode *node, [[maybe_ if (type->IsETSUnionType()) { type = Checker()->GetGlobalTypesHolder()->GlobalETSObjectType(); } - if (type->IsETSObjectType() || type->IsETSArrayType()) { + if (type->IsETSObjectType() || type->IsETSArrayType() || type->IsETSTypeParameter()) { LoadAccumulatorNull(node, type); } else if (type->IsETSBooleanType()) { LoadAccumulatorBoolean(node, type->AsETSBooleanType()->GetValue()); @@ -738,7 +735,7 @@ void ETSGen::ReturnAcc(const ir::AstNode *node) { const auto *const acc_type = GetAccumulatorType(); - if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_ARRAY_OR_OBJECT | checker::TypeFlag::ETS_UNION)) { + if (acc_type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { Sa().Emit(node); } else if (acc_type->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { Sa().Emit(node); @@ -754,7 +751,7 @@ void ETSGen::EmitIsInstanceNonNullish([[maybe_unused]] const ir::AstNode *const #ifdef PANDA_WITH_ETS auto const obj_type = GetVRegType(obj_reg); // undefined is implemented as Object instance, so "instanceof Object" must be treated carefully - if (!obj_type->ContainsUndefined() || cls_type != Checker()->GlobalETSObjectType()) { + if (!Checker()->MayHaveUndefinedValue(obj_type) || cls_type != Checker()->GlobalETSObjectType()) { LoadAccumulator(node, obj_reg); Sa().Emit(node, cls_type->AssemblerName()); SetAccumulatorType(Checker()->GlobalETSBooleanType()); @@ -794,7 +791,7 @@ void ETSGen::EmitIsInstance([[maybe_unused]] const ir::AstNode *const node, [[ma return; } - if (!rhs_type->IsNullishOrNullLike()) { + if (!Checker()->MayHaveNulllikeValue(rhs_type)) { EmitIsInstanceNonNullish(node, obj_reg, rhs_type); return; } @@ -805,10 +802,10 @@ void ETSGen::EmitIsInstance([[maybe_unused]] const ir::AstNode *const node, [[ma LoadAccumulator(node, obj_reg); // Iterate union members - if (rhs_type->ContainsNull() || rhs_type->IsETSNullType()) { + if (Checker()->MayHaveNullValue(rhs_type)) { BranchIfNull(node, if_true); } - if (rhs_type->ContainsUndefined() || rhs_type->IsETSUndefinedType()) { + if (Checker()->MayHaveUndefinedValue(rhs_type)) { Sa().Emit(node); BranchIfTrue(node, if_true); LoadAccumulator(node, obj_reg); @@ -828,6 +825,32 @@ void ETSGen::EmitIsInstance([[maybe_unused]] const ir::AstNode *const node, [[ma #endif // PANDA_WITH_ETS } +void ETSGen::InternalCheckCast(const ir::AstNode *node, const es2panda::checker::Type *target) +{ + ASSERT(target->IsETSObjectType() && !target->IsNullishOrNullLike()); + Sa().Emit(node, ToAssemblerType(target)); + SetAccumulatorType(target); +} + +void ETSGen::CheckedReferenceNarrowing(const ir::AstNode *node, const checker::Type *target) +{ + ASSERT(target->HasTypeFlag(TYPE_FLAG_BYTECODE_REF) && !target->IsETSNullLike()); + // NOTE(vpukhov): implement for nulllike and union targets + + Sa().Emit(node, ToAssemblerType(target)); + SetAccumulatorType(target); +} + +void ETSGen::GuardUncheckedType(const ir::AstNode *node, const checker::Type *unchecked, const checker::Type *target) +{ + if (unchecked != nullptr) { + SetAccumulatorType(unchecked); + CheckedReferenceNarrowing(node, target); + } else { + SetAccumulatorType(target); + } +} + bool ETSGen::TryLoadConstantExpression(const ir::Expression *node) { const auto *type = node->TsType(); @@ -900,8 +923,8 @@ void ETSGen::ApplyConversionCast(const ir::AstNode *node, const checker::Type *t break; } case checker::TypeFlag::ETS_ARRAY: - [[fallthrough]]; - case checker::TypeFlag::ETS_OBJECT: { + case checker::TypeFlag::ETS_OBJECT: + case checker::TypeFlag::ETS_TYPE_PARAMETER: { if (GetAccumulatorType() != nullptr && GetAccumulatorType()->IsETSDynamicType()) { CastDynamicToObject(node, target_type); } @@ -917,28 +940,34 @@ void ETSGen::ApplyConversionCast(const ir::AstNode *node, const checker::Type *t } } +void ETSGen::ApplyBoxingConversion(const ir::AstNode *node) +{ + EmitBoxingConversion(node); + node->SetBoxingUnboxingFlags( + static_cast(node->GetBoxingUnboxingFlags() & ~(ir::BoxingUnboxingFlags::BOXING_FLAG))); +} + +void ETSGen::ApplyUnboxingConversion(const ir::AstNode *node) +{ + if (Checker()->MayHaveNulllikeValue(GetAccumulatorType())) { // NOTE: vpukhov. should be a CTE + EmitNullishGuardian(node); + } + EmitUnboxingConversion(node); + node->SetBoxingUnboxingFlags(static_cast(node->GetBoxingUnboxingFlags() & + ~(ir::BoxingUnboxingFlags::UNBOXING_FLAG))); +} + void ETSGen::ApplyConversion(const ir::AstNode *node, const checker::Type *target_type) { auto ttctx = TargetTypeContext(this, target_type); if ((node->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG) != 0U) { - EmitBoxingConversion(node); - const auto boxing_flags = - static_cast(node->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG); - node->SetBoxingUnboxingFlags( - static_cast(node->GetBoxingUnboxingFlags() & ~boxing_flags)); + ApplyBoxingConversion(node); return; } if ((node->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG) != 0U) { - if (GetAccumulatorType()->IsNullishOrNullLike()) { // NOTE: vpukhov. should be a CTE - EmitNullishGuardian(node); - } - EmitUnboxingConversion(node); - const auto unboxing_flags = static_cast(node->GetBoxingUnboxingFlags() & - ir::BoxingUnboxingFlags::UNBOXING_FLAG); - node->SetBoxingUnboxingFlags( - static_cast(node->GetBoxingUnboxingFlags() & ~unboxing_flags)); + ApplyUnboxingConversion(node); } if (target_type == nullptr) { @@ -946,7 +975,7 @@ void ETSGen::ApplyConversion(const ir::AstNode *node, const checker::Type *targe } if (target_type->IsETSUnionType()) { - SetAccumulatorType(target_type->AsETSUnionType()->GetLeastUpperBoundType()); + SetAccumulatorType(target_type); return; } @@ -986,50 +1015,60 @@ void ETSGen::ApplyCast(const ir::AstNode *node, const checker::Type *target_type void ETSGen::EmitUnboxingConversion(const ir::AstNode *node) { - auto unboxing_flag = + const auto unboxing_flag = static_cast(ir::BoxingUnboxingFlags::UNBOXING_FLAG & node->GetBoxingUnboxingFlags()); RegScope rs(this); + auto emit_unboxed_call = [this, &node](std::string_view signature_flag, const checker::Type *const target_type, + const checker::Type *const boxed_type) { + if (node->HasAstNodeFlags(ir::AstNodeFlags::CHECKCAST)) { + CheckedReferenceNarrowing(node, boxed_type); + } + + Ra().Emit(node, signature_flag, dummy_reg_, 0); + SetAccumulatorType(target_type); + }; + switch (unboxing_flag) { case ir::BoxingUnboxingFlags::UNBOX_TO_BOOLEAN: { - Ra().Emit(node, Signatures::BUILTIN_BOOLEAN_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalETSBooleanType()); + emit_unboxed_call(Signatures::BUILTIN_BOOLEAN_UNBOXED, Checker()->GlobalETSBooleanType(), + Checker()->GetGlobalTypesHolder()->GlobalETSBooleanBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_BYTE: { - Ra().Emit(node, Signatures::BUILTIN_BYTE_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalByteType()); + emit_unboxed_call(Signatures::BUILTIN_BYTE_UNBOXED, Checker()->GlobalByteType(), + Checker()->GetGlobalTypesHolder()->GlobalByteBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_CHAR: { - Ra().Emit(node, Signatures::BUILTIN_CHAR_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalCharType()); + emit_unboxed_call(Signatures::BUILTIN_CHAR_UNBOXED, Checker()->GlobalCharType(), + Checker()->GetGlobalTypesHolder()->GlobalCharBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_SHORT: { - Ra().Emit(node, Signatures::BUILTIN_SHORT_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalShortType()); + emit_unboxed_call(Signatures::BUILTIN_SHORT_UNBOXED, Checker()->GlobalShortType(), + Checker()->GetGlobalTypesHolder()->GlobalShortBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_INT: { - Ra().Emit(node, Signatures::BUILTIN_INT_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalIntType()); + emit_unboxed_call(Signatures::BUILTIN_INT_UNBOXED, Checker()->GlobalIntType(), + Checker()->GetGlobalTypesHolder()->GlobalIntegerBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_LONG: { - Ra().Emit(node, Signatures::BUILTIN_LONG_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalLongType()); + emit_unboxed_call(Signatures::BUILTIN_LONG_UNBOXED, Checker()->GlobalLongType(), + Checker()->GetGlobalTypesHolder()->GlobalLongBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_FLOAT: { - Ra().Emit(node, Signatures::BUILTIN_FLOAT_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalFloatType()); + emit_unboxed_call(Signatures::BUILTIN_FLOAT_UNBOXED, Checker()->GlobalFloatType(), + Checker()->GetGlobalTypesHolder()->GlobalFloatBuiltinType()); break; } case ir::BoxingUnboxingFlags::UNBOX_TO_DOUBLE: { - Ra().Emit(node, Signatures::BUILTIN_DOUBLE_UNBOXED, dummy_reg_, 0); - SetAccumulatorType(Checker()->GlobalDoubleType()); + emit_unboxed_call(Signatures::BUILTIN_DOUBLE_UNBOXED, Checker()->GlobalDoubleType(), + Checker()->GetGlobalTypesHolder()->GlobalDoubleBuiltinType()); break; } default: @@ -1171,7 +1210,7 @@ void ETSGen::EmitLocalBoxGet(ir::AstNode const *node, checker::Type const *conte break; default: Ra().Emit(node, Signatures::BUILTIN_BOX_GET, dummy_reg_, 0); - EmitCheckCast(node, content_type); + CheckedReferenceNarrowing(node, content_type); break; } SetAccumulatorType(content_type); @@ -1553,7 +1592,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 | checker::TypeFlag::ETS_UNION)); + ASSERT(GetAccumulatorType()->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)); const auto *const source_type = GetAccumulatorType(); @@ -1567,12 +1606,17 @@ void ETSGen::CastToArrayOrObject(const ir::AstNode *const node, const checker::T return; } - if (unchecked) { - SetAccumulatorType(target_type); + if (!unchecked) { + CheckedReferenceNarrowing(node, target_type); return; } - EmitCheckedNarrowingReferenceConversion(node, target_type); + if (target_type->IsETSTypeParameter() && target_type->AsETSTypeParameter()->HasConstraint()) { + CheckedReferenceNarrowing(node, target_type->AsETSTypeParameter()->GetConstraintType()); + } else if (target_type->IsETSObjectType()) { + CheckedReferenceNarrowing(node, target_type->AsETSObjectType()->GetConstOriginalBaseType()); + } + SetAccumulatorType(target_type); } void ETSGen::CastDynamicToObject(const ir::AstNode *node, const checker::Type *target_type) @@ -1582,14 +1626,13 @@ void ETSGen::CastDynamicToObject(const ir::AstNode *node, const checker::Type *t return; } - // NOTE: itrubachev. Introduce checker::TypeFlag::LAMBDA_OBJECT and lambda object type itself in es2panda. - // Now lambda object is any class with invoke method, that seems strange + // NOTE(vpukhov): #14626 remove, replace target_type with interface if (target_type->IsLambdaObject()) { VReg dyn_obj_reg = AllocReg(); StoreAccumulator(node, dyn_obj_reg); Ra().Emit(node, target_type->AsETSObjectType()->ConstructSignatures()[0]->InternalName(), dyn_obj_reg, dummy_reg_); - SetAccumulatorType(Checker()->GlobalETSObjectType()); + SetAccumulatorType(target_type); return; } @@ -1603,7 +1646,7 @@ void ETSGen::CastDynamicToObject(const ir::AstNode *node, const checker::Type *t return; } - if (target_type->IsETSArrayType() || target_type->IsETSObjectType()) { + if (target_type->IsETSArrayType() || target_type->IsETSObjectType() || target_type->IsETSTypeParameter()) { auto lang = GetAccumulatorType()->AsETSDynamicType()->Language(); auto method_name = compiler::Signatures::Dynamic::GetObjectBuiltin(lang); @@ -1675,7 +1718,8 @@ void ETSGen::CastToDynamic(const ir::AstNode *node, const checker::ETSDynamicTyp method_name = compiler::Signatures::Dynamic::NewDoubleBuiltin(type->Language()); break; } - case checker::TypeFlag::ETS_OBJECT: { + case checker::TypeFlag::ETS_OBJECT: + case checker::TypeFlag::ETS_TYPE_PARAMETER: { if (GetAccumulatorType()->IsETSStringType()) { method_name = compiler::Signatures::Dynamic::NewStringBuiltin(type->Language()); break; @@ -1777,16 +1821,6 @@ void ETSGen::CastDynamicTo(const ir::AstNode *node, enum checker::TypeFlag type_ SetAccumulatorType(object_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 | checker::TypeFlag::ETS_UNION) && - !target_type->IsETSNullLike()); - - Sa().Emit(node, ToCheckCastTypeView(target_type)); - SetAccumulatorType(target_type); -} - void ETSGen::ToBinaryResult(const ir::AstNode *node, Label *if_false) { Label *end = AllocLabel(); @@ -1959,14 +1993,14 @@ void ETSGen::BranchIfNullish([[maybe_unused]] const ir::AstNode *node, [[maybe_u #ifdef PANDA_WITH_ETS auto *const type = GetAccumulatorType(); - if (!type->IsNullishOrNullLike()) { + if (!Checker()->MayHaveNulllikeValue(type)) { return; } if (type->IsETSNullLike()) { Sa().Emit(node, if_nullish); return; } - if (!type->ContainsUndefined()) { + if (!Checker()->MayHaveUndefinedValue(type)) { Sa().Emit(node, if_nullish); return; } @@ -1995,14 +2029,14 @@ void ETSGen::BranchIfNotNullish([[maybe_unused]] const ir::AstNode *node, [[mayb #ifdef PANDA_WITH_ETS auto *const type = GetAccumulatorType(); - if (!type->IsNullishOrNullLike()) { + if (!Checker()->MayHaveNulllikeValue(type)) { Sa().Emit(node, if_not_nullish); return; } if (type->IsETSNullLike()) { return; } - if (!type->ContainsUndefined()) { + if (!Checker()->MayHaveUndefinedValue(type)) { Sa().Emit(node, if_not_nullish); return; } @@ -2032,8 +2066,8 @@ void ETSGen::ConvertToNonNullish(const ir::AstNode *node) { auto const *nullish_type = GetAccumulatorType(); auto const *target_type = Checker()->GetNonNullishType(nullish_type); - if (nullish_type->ContainsUndefined() && target_type != Checker()->GlobalETSObjectType()) { - EmitCheckedNarrowingReferenceConversion(node, target_type); + if (Checker()->MayHaveUndefinedValue(nullish_type) && target_type != Checker()->GlobalETSObjectType()) { + CheckedReferenceNarrowing(node, target_type); } SetAccumulatorType(target_type); } @@ -2041,7 +2075,7 @@ void ETSGen::ConvertToNonNullish(const ir::AstNode *node) void ETSGen::EmitNullishGuardian(const ir::AstNode *node) { auto const *nullish_type = GetAccumulatorType(); - ASSERT(nullish_type->IsNullish()); + ASSERT(Checker()->MayHaveNulllikeValue(nullish_type)); compiler::Label *if_not_nullish = AllocLabel(); BranchIfNotNullish(node, if_not_nullish); @@ -2247,13 +2281,6 @@ void ETSGen::UnaryDollarDollar(const ir::AstNode *node) EmitThrow(node, exception); } -void ETSGen::InsertNeededCheckCast(const checker::Signature *signature, const ir::AstNode *node) -{ - if (signature->IsBaseReturnDiff()) { - EmitCheckCast(node, signature->ReturnType()); - } -} - void ETSGen::Update(const ir::AstNode *node, lexer::TokenType op) { switch (op) { @@ -2296,8 +2323,9 @@ void ETSGen::StringBuilderAppend(const ir::AstNode *node, VReg builder) signature = Signatures::BUILTIN_STRING_BUILDER_APPEND_BUILTIN_STRING; } - if (GetAccumulatorType()->IsETSObjectType() && !GetAccumulatorType()->IsETSStringType()) { - if (GetAccumulatorType()->ContainsNull() || GetAccumulatorType()->IsETSNullType()) { + if ((GetAccumulatorType()->IsETSObjectType() || GetAccumulatorType()->IsETSTypeParameter()) && + !GetAccumulatorType()->IsETSStringType()) { + if (Checker()->MayHaveNullValue(GetAccumulatorType())) { Label *ifnull = AllocLabel(); Label *end = AllocLabel(); BranchIfNull(node, ifnull); @@ -2427,10 +2455,6 @@ void ETSGen::LoadArrayElement(const ir::AstNode *node, VReg object_reg) { auto *element_type = GetVRegType(object_reg)->AsETSArrayType()->ElementType(); - if (element_type->IsETSUnionType()) { - element_type = element_type->AsETSUnionType()->GetLeastUpperBoundType(); - } - switch (checker::ETSChecker::ETSType(element_type)) { case checker::TypeFlag::ETS_BOOLEAN: case checker::TypeFlag::BYTE: { @@ -2466,6 +2490,8 @@ void ETSGen::LoadArrayElement(const ir::AstNode *node, VReg object_reg) } case checker::TypeFlag::ETS_ARRAY: case checker::TypeFlag::ETS_OBJECT: + case checker::TypeFlag::ETS_TYPE_PARAMETER: + case checker::TypeFlag::ETS_UNION: case checker::TypeFlag::ETS_DYNAMIC_TYPE: { Ra().Emit(node, object_reg); break; @@ -2481,9 +2507,6 @@ void ETSGen::LoadArrayElement(const ir::AstNode *node, VReg object_reg) void ETSGen::StoreArrayElement(const ir::AstNode *node, VReg object_reg, VReg index, const checker::Type *element_type) { - if (element_type->IsETSUnionType()) { - element_type = element_type->AsETSUnionType()->GetLeastUpperBoundType(); - } switch (checker::ETSChecker::ETSType(element_type)) { case checker::TypeFlag::ETS_BOOLEAN: case checker::TypeFlag::BYTE: { @@ -2516,6 +2539,8 @@ void ETSGen::StoreArrayElement(const ir::AstNode *node, VReg object_reg, VReg in } case checker::TypeFlag::ETS_ARRAY: case checker::TypeFlag::ETS_OBJECT: + case checker::TypeFlag::ETS_TYPE_PARAMETER: + case checker::TypeFlag::ETS_UNION: case checker::TypeFlag::ETS_DYNAMIC_TYPE: { Ra().Emit(node, object_reg, index); break; @@ -2619,23 +2644,13 @@ bool ETSGen::ExtendWithFinalizer(ir::AstNode *node, const ir::AstNode *original_ return ExtendWithFinalizer(parent, original_node, prev_finnaly); } -util::StringView ETSGen::ToCheckCastTypeView(const es2panda::checker::Type *type) const +util::StringView ETSGen::ToAssemblerType(const es2panda::checker::Type *type) const { - auto asm_t = type; - if (type->IsETSUnionType()) { - asm_t = type->AsETSUnionType()->GetLeastUpperBoundType(); - } + ASSERT(type->HasTypeFlag(TYPE_FLAG_BYTECODE_REF) && !type->IsETSNullLike()); + std::stringstream ss; - asm_t->ToAssemblerTypeWithRank(ss); + type->ToAssemblerTypeWithRank(ss); return util::UString(ss.str(), Allocator()).View(); } -void ETSGen::EmitCheckCast(const ir::AstNode *node, const es2panda::checker::Type *type) -{ - if (type->IsETSArrayType()) { - return; // Since generic arrays allowed we can't add checkcast for them. - } - Ra().Emit(node, ToCheckCastTypeView(type)); -} - } // namespace panda::es2panda::compiler diff --git a/ets2panda/compiler/core/ETSGen.h b/ets2panda/compiler/core/ETSGen.h index 516e82d3a025c6195efb36e9fa852bbdecf52994..e074af2d7fbb432398c4fb2ae4a53477dd15a74d 100644 --- a/ets2panda/compiler/core/ETSGen.h +++ b/ets2panda/compiler/core/ETSGen.h @@ -69,7 +69,7 @@ public: void StoreProperty(const ir::AstNode *node, const checker::Type *prop_type, VReg obj_reg, const util::StringView &name); - void LoadProperty(const ir::AstNode *node, const checker::Type *prop_type, bool is_generic, VReg obj_reg, + void LoadProperty(const ir::AstNode *node, const checker::Type *prop_type, VReg obj_reg, const util::StringView &full_name); void StorePropertyDynamic(const ir::AstNode *node, const checker::Type *prop_type, VReg obj_reg, const util::StringView &name); @@ -80,7 +80,7 @@ public: void LoadElementDynamic(const ir::AstNode *node, VReg object_reg); void StoreUnionProperty(const ir::AstNode *node, VReg obj_reg, const util::StringView &name); - void LoadUnionProperty(const ir::AstNode *node, const checker::Type *prop_type, bool is_generic, VReg obj_reg, + 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); @@ -225,7 +225,7 @@ public: } Label *if_nullish {nullptr}; Label *end {nullptr}; - if (type->IsNullishOrNullLike()) { + if (Checker()->MayHaveNulllikeValue(type)) { if constexpr (USE_FALSE_LABEL) { BranchIfNullish(node, if_false); } else { @@ -307,7 +307,7 @@ public: { auto *const type = GetAccumulatorType(); - if (!type->IsNullishOrNullLike()) { + if (!Checker()->MayHaveNulllikeValue(type)) { compile(); } else if (type->IsETSNullLike()) { if (is_optional) { @@ -412,6 +412,8 @@ public: void LoadAccumulatorDynamicModule(const ir::AstNode *node, const ir::ETSImportDeclaration *import); + void ApplyBoxingConversion(const ir::AstNode *node); + void ApplyUnboxingConversion(const ir::AstNode *node); void ApplyConversion(const ir::AstNode *node) { if (target_type_ != nullptr) { @@ -539,9 +541,12 @@ public: void CastToDynamic(const ir::AstNode *node, const checker::ETSDynamicType *type); void CastDynamicTo(const ir::AstNode *node, enum checker::TypeFlag type_flag); void CastToArrayOrObject(const ir::AstNode *node, const checker::Type *target_type, bool unchecked); - void EmitCheckedNarrowingReferenceConversion(const ir::AstNode *node, const checker::Type *target_type); void CastDynamicToObject(const ir::AstNode *node, const checker::Type *target_type); + void InternalCheckCast(const ir::AstNode *node, const checker::Type *target); + void CheckedReferenceNarrowing(const ir::AstNode *node, const checker::Type *target); + void GuardUncheckedType(const ir::AstNode *node, const checker::Type *unchecked, const checker::Type *target); + // Call, Construct void NewArray(const ir::AstNode *node, VReg arr, VReg dim, const checker::Type *arr_type); void NewObject(const ir::AstNode *node, VReg ctor, util::StringView name); @@ -662,11 +667,7 @@ private: void UnaryTilde(const ir::AstNode *node); void UnaryDollarDollar(const ir::AstNode *node); - util::StringView ToCheckCastTypeView(const es2panda::checker::Type *type) const; - void EmitCheckCast(const ir::AstNode *node, const es2panda::checker::Type *type); - - // To avoid verifier error checkcast is needed - void InsertNeededCheckCast(const checker::Signature *signature, const ir::AstNode *node); + util::StringView ToAssemblerType(const es2panda::checker::Type *type) const; template void StoreValueIntoArray(const ir::AstNode *const node, const VReg arr, const VReg index) @@ -757,6 +758,7 @@ private: switch (type_kind) { case checker::TypeFlag::ETS_OBJECT: + case checker::TypeFlag::ETS_TYPE_PARAMETER: case checker::TypeFlag::ETS_DYNAMIC_TYPE: { RegScope rs(this); VReg arg0 = AllocReg(); @@ -967,8 +969,6 @@ private: break; } } - - InsertNeededCheckCast(signature, node); } template @@ -1023,8 +1023,6 @@ private: break; } } - - InsertNeededCheckCast(signature, node); } #undef COMPILE_ARG @@ -1078,8 +1076,6 @@ private: break; } } - - InsertNeededCheckCast(signature, node); } #undef COMPILE_ARG diff --git a/ets2panda/compiler/lowering/ets/objectIndexAccess.cpp b/ets2panda/compiler/lowering/ets/objectIndexAccess.cpp index 0afacc26a125cf295faea921ff87302759d6e57c..277f9026ff899ccd31345d290f2c05b3f080440e 100644 --- a/ets2panda/compiler/lowering/ets/objectIndexAccess.cpp +++ b/ets2panda/compiler/lowering/ets/objectIndexAccess.cpp @@ -67,6 +67,7 @@ ir::Expression *ObjectIndexLowering::ProcessIndexGetAccess(parser::ETSParser *pa lowering_result->SetParent(member_expression->Parent()); lowering_result->Check(checker); + lowering_result->SetBoxingUnboxingFlags(member_expression->GetBoxingUnboxingFlags()); return lowering_result; } diff --git a/ets2panda/compiler/lowering/phase.cpp b/ets2panda/compiler/lowering/phase.cpp index 21867db6a6ebc0e045ed8867a72a78f8fd8d4a50..b64e813bc52d8c4ee33544eddb1a0867ba7e093a 100644 --- a/ets2panda/compiler/lowering/phase.cpp +++ b/ets2panda/compiler/lowering/phase.cpp @@ -130,10 +130,7 @@ bool Phase::Apply(public_lib::Context *ctx, parser::Program *program) return true; } - if (options->dump_before_phases.count(name) > 0) { - std::cout << "Before phase " << Name() << ":" << std::endl; - std::cout << program->Dump() << std::endl; - } + CheckOptionsBeforePhase(options, program, name); #ifndef NDEBUG if (!Precondition(ctx, program)) { @@ -146,10 +143,7 @@ bool Phase::Apply(public_lib::Context *ctx, parser::Program *program) return false; } - if (options->dump_after_phases.count(name) > 0) { - std::cout << "After phase " << Name() << ":" << std::endl; - std::cout << program->Dump() << std::endl; - } + CheckOptionsAfterPhase(options, program, name); #ifndef NDEBUG check_program(program); @@ -163,4 +157,34 @@ bool Phase::Apply(public_lib::Context *ctx, parser::Program *program) return true; } +void Phase::CheckOptionsBeforePhase(const CompilerOptions *options, const parser::Program *program, + const std::string &name) const +{ + if (options->dump_after_phases.count(name) > 0) { + std::cout << "After phase " << name << ":" << std::endl; + std::cout << program->Dump() << std::endl; + } + + if (options->dump_ets_src_after_phases.count(name) > 0) { + std::cout << "After phase " << name << " ets source" + << ":" << std::endl; + std::cout << program->Ast()->DumpEtsSrc() << std::endl; + } +} + +void Phase::CheckOptionsAfterPhase(const CompilerOptions *options, const parser::Program *program, + const std::string &name) const +{ + if (options->dump_after_phases.count(name) > 0) { + std::cout << "After phase " << name << ":" << std::endl; + std::cout << program->Dump() << std::endl; + } + + if (options->dump_ets_src_after_phases.count(name) > 0) { + std::cout << "After phase " << name << " ets source" + << ":" << std::endl; + std::cout << program->Ast()->DumpEtsSrc() << std::endl; + } +} + } // namespace panda::es2panda::compiler diff --git a/ets2panda/compiler/lowering/phase.h b/ets2panda/compiler/lowering/phase.h index 8052a9ab2d159c2438ad20a49c0462bac97a495d..9e2a381c149d5deec09974f65475f28edc75c361 100644 --- a/ets2panda/compiler/lowering/phase.h +++ b/ets2panda/compiler/lowering/phase.h @@ -39,6 +39,12 @@ public: { return true; } + +private: + void CheckOptionsBeforePhase(const CompilerOptions *options, const parser::Program *program, + const std::string &name) const; + void CheckOptionsAfterPhase(const CompilerOptions *options, const parser::Program *program, + const std::string &name) const; }; std::vector GetPhaseList(ScriptExtension ext); diff --git a/ets2panda/es2panda.h b/ets2panda/es2panda.h index 4c0d3725c0e43cfea9e499e272c1af0c1d5f1c80..6cca648fb6cafafe144f1d1b3f88caf89dcb3cdf 100644 --- a/ets2panda/es2panda.h +++ b/ets2panda/es2panda.h @@ -104,7 +104,9 @@ struct CompilerOptions { std::vector plugins {}; std::unordered_set skip_phases {}; std::unordered_set dump_before_phases {}; + std::unordered_set dump_ets_src_before_phases {}; std::unordered_set dump_after_phases {}; + std::unordered_set dump_ets_src_after_phases {}; std::shared_ptr arkts_config {}; CompilationMode compilation_mode {}; // NOLINTEND(misc-non-private-member-variables-in-classes) diff --git a/ets2panda/ir/as/namedType.cpp b/ets2panda/ir/as/namedType.cpp index 1957c7a148cc2c3732419abbb46ea19bcc4ae699..8c1cd53504ad063655adc0294617d5ea63bfba34 100644 --- a/ets2panda/ir/as/namedType.cpp +++ b/ets2panda/ir/as/namedType.cpp @@ -21,6 +21,7 @@ #include "ir/expressions/identifier.h" #include "ir/ts/tsTypeParameterInstantiation.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void NamedType::TransformChildren(const NodeTransformer &cb) @@ -58,6 +59,11 @@ void NamedType::Dump(AstDumper *dumper) const {"isNullable", nullable_}}); } +void NamedType::Dump(SrcDumper *dumper) const +{ + dumper->Add("NamedType"); +} + void NamedType::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/as/namedType.h b/ets2panda/ir/as/namedType.h index 5c8259eb998df44427c6e06d268bb27bac78032e..f0da80b2a4dbf42d7e332fa23ddc144c84f771c0 100644 --- a/ets2panda/ir/as/namedType.h +++ b/ets2panda/ir/as/namedType.h @@ -59,6 +59,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; diff --git a/ets2panda/ir/as/prefixAssertionExpression.cpp b/ets2panda/ir/as/prefixAssertionExpression.cpp index 9d16342536c0d1d11ac42be1cedf12fe6d2d641c..e06f9058a6e81c556f013361eaf23c69eab2c447 100644 --- a/ets2panda/ir/as/prefixAssertionExpression.cpp +++ b/ets2panda/ir/as/prefixAssertionExpression.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" namespace panda::es2panda::ir { @@ -39,6 +40,11 @@ void PrefixAssertionExpression::Dump(AstDumper *dumper) const dumper->Add({{"type", "PrefixAssertionExpression"}, {"expression", expr_}, {"type", type_}}); } +void PrefixAssertionExpression::Dump(SrcDumper *dumper) const +{ + dumper->Add("PrefixAssertionExpression"); +} + void PrefixAssertionExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/as/prefixAssertionExpression.h b/ets2panda/ir/as/prefixAssertionExpression.h index 758df8b69864ad54f254bbaa85e4fa0b3bc957e9..63546010db665bab4aa344175c0bf7b8e81547b6 100644 --- a/ets2panda/ir/as/prefixAssertionExpression.h +++ b/ets2panda/ir/as/prefixAssertionExpression.h @@ -39,6 +39,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/astNode.cpp b/ets2panda/ir/astNode.cpp index 4687db69022050d5086d038a32b3b74175fdb737..19a80ab4ca51a4da7dcf9b9c95540cbb5dcee7ec 100644 --- a/ets2panda/ir/astNode.cpp +++ b/ets2panda/ir/astNode.cpp @@ -15,6 +15,7 @@ #include "astNode.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "typeNode.h" namespace panda::es2panda::ir { @@ -115,4 +116,10 @@ std::string AstNode::DumpJSON() const ir::AstDumper dumper {this}; return dumper.Str(); } + +std::string AstNode::DumpEtsSrc() const +{ + ir::SrcDumper dumper {this}; + return dumper.Str(); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index 9a30d7bb961d069a6375c232c325bc4db228d4dd..f7e8d6d4238f5007de71ec3bc80911c2401ffdb9 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -75,6 +75,7 @@ DEFINE_BITOPS(BoxingUnboxingFlags) // Forward declarations class AstDumper; class Expression; +class SrcDumper; class Statement; class ClassElement; @@ -305,6 +306,11 @@ public: return (flags_ & ModifierFlags::OVERRIDE) != 0; } + void SetOverride() noexcept + { + flags_ |= ModifierFlags::OVERRIDE; + } + [[nodiscard]] bool IsAsync() const noexcept { return (flags_ & ModifierFlags::ASYNC) != 0; @@ -423,20 +429,35 @@ public: return flags_; } - void SetBoxingUnboxingFlags(BoxingUnboxingFlags const flags) const noexcept - { - boxing_unboxing_flags_ = flags; - } - - void AddBoxingUnboxingFlag(BoxingUnboxingFlags const flag) const noexcept - { - boxing_unboxing_flags_ |= flag; - } - - [[nodiscard]] BoxingUnboxingFlags GetBoxingUnboxingFlags() const noexcept - { - return boxing_unboxing_flags_; - } + // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) +#define DECLARE_FLAG_OPERATIONS(flag_type, member_name) \ + void Set##flag_type(flag_type flags) const noexcept \ + { \ + member_name = flags; \ + } \ + \ + void Add##flag_type(flag_type flag) const noexcept \ + { \ + member_name |= flag; \ + } \ + \ + [[nodiscard]] flag_type Get##flag_type() const noexcept \ + { \ + return member_name; \ + } \ + \ + bool Has##flag_type(flag_type flag) const noexcept \ + { \ + return (member_name & flag) != 0U; \ + } \ + void Remove##flag_type(flag_type flag) noexcept \ + { \ + member_name &= ~flag; \ + } + + DECLARE_FLAG_OPERATIONS(BoxingUnboxingFlags, boxing_unboxing_flags_); + DECLARE_FLAG_OPERATIONS(AstNodeFlags, ast_node_flags_); +#undef DECLARE_FLAG_OPERATIONS ir::ClassElement *AsClassElement() { @@ -478,8 +499,10 @@ public: AstNode *FindChild(const NodePredicate &cb) const; std::string DumpJSON() const; + std::string DumpEtsSrc() const; virtual void Dump(ir::AstDumper *dumper) const = 0; + virtual void Dump(ir::SrcDumper *dumper) const = 0; virtual void Compile([[maybe_unused]] compiler::PandaGen *pg) const = 0; virtual void Compile([[maybe_unused]] compiler::ETSGen *etsg) const {}; virtual checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) = 0; @@ -509,6 +532,7 @@ protected: AstNodeType type_; varbinder::Variable *variable_ {}; ModifierFlags flags_ {}; + mutable AstNodeFlags ast_node_flags_ {}; mutable BoxingUnboxingFlags boxing_unboxing_flags_ {}; // NOLINTEND(misc-non-private-member-variables-in-classes) }; diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index d22bc5d6e4df5bdb4426b01f564c86614ce0bdd6..f6420bb131169df655c2d9ce113a41c983aa62a6 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -21,8 +21,7 @@ namespace panda::es2panda::ir { enum class AstNodeFlags { NO_OPTS = 0, - STRICT = (1U << 0U), - PARAMETER = (1U << 1U), + CHECKCAST = 1U << 0U, }; enum class ModifierFlags : uint32_t { @@ -114,4 +113,4 @@ enum class BoxingUnboxingFlags : uint32_t { }; } // namespace panda::es2panda::ir -#endif \ No newline at end of file +#endif diff --git a/ets2panda/ir/base/catchClause.cpp b/ets2panda/ir/base/catchClause.cpp index feee7e595a855d62d2888410c7a4e973fda74216..da8ceb1c5523740b45c1f59f89b9f9b0db78c0f6 100644 --- a/ets2panda/ir/base/catchClause.cpp +++ b/ets2panda/ir/base/catchClause.cpp @@ -19,6 +19,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" #include "ir/statements/blockStatement.h" @@ -46,6 +47,22 @@ void CatchClause::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "CatchClause"}, {"body", body_}, {"param", AstDumper::Nullish(param_)}}); } +void CatchClause::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(body_ != nullptr); + dumper->Add("("); + if (param_ != nullptr) { + param_->Dump(dumper); + } + dumper->Add(") {"); + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + dumper->Add("}"); +} + bool CatchClause::IsDefaultCatchClause() const { return param_->AsIdentifier()->TypeAnnotation() == nullptr; diff --git a/ets2panda/ir/base/catchClause.h b/ets2panda/ir/base/catchClause.h index 22fff21e34e0fece5a216b438fa19e8dbcdac51a..0a4032356a9850ff592ef95da0d871263e5848eb 100644 --- a/ets2panda/ir/base/catchClause.h +++ b/ets2panda/ir/base/catchClause.h @@ -69,6 +69,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 31520c1ffd1097eb9e0e568d5258279d52443180..86458df8520e5f3404f8339e5b9d1a863bb8d4c6 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/classStaticBlock.h" #include "ir/base/methodDefinition.h" #include "ir/base/scriptFunction.h" @@ -130,6 +131,62 @@ void ClassDefinition::Dump(ir::AstDumper *dumper) const {"body", body_, prop_filter}}); } +void ClassDefinition::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(ident_ != nullptr); + + if (IsExtern()) { + dumper->Add("extern "); + } + + if (IsFinal()) { + dumper->Add("final "); + } + + if (IsAbstract()) { + dumper->Add("abstract "); + } + + dumper->Add("class "); + ident_->Dump(dumper); + + if (type_params_ != nullptr) { + dumper->Add("<"); + type_params_->Dump(dumper); + dumper->Add("> "); + } + + if (super_class_ != nullptr) { + dumper->Add(" extends "); + super_class_->Dump(dumper); + } + + if (!implements_.empty()) { + dumper->Add(" implements "); + for (auto interface : implements_) { + interface->Dump(dumper); + if (interface != implements_.back()) { + dumper->Add(", "); + } + } + } + + dumper->Add(" {"); + if (!body_.empty()) { + dumper->IncrIndent(); + dumper->Endl(); + for (auto elem : body_) { + elem->Dump(dumper); + if (elem == body_.back()) { + dumper->DecrIndent(); + } + dumper->Endl(); + } + } + dumper->Add("}"); + dumper->Endl(); +} + void ClassDefinition::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/classDefinition.h b/ets2panda/ir/base/classDefinition.h index f0fc0cebc39f7910f4c56a2658ee2921c3f9877f..e992e96ba135e72ebb5c72d7427f618a6a1e6d4c 100644 --- a/ets2panda/ir/base/classDefinition.h +++ b/ets2panda/ir/base/classDefinition.h @@ -275,7 +275,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/classProperty.cpp b/ets2panda/ir/base/classProperty.cpp index 26e1c96ed9b4ba24e57a03da0734685e9812fa6e..86699fdb3908d27de28517e4fee0b2868769acf6 100644 --- a/ets2panda/ir/base/classProperty.cpp +++ b/ets2panda/ir/base/classProperty.cpp @@ -21,6 +21,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/decorator.h" #include "ir/typeNode.h" #include "ir/expression.h" @@ -78,6 +79,43 @@ void ClassProperty::Dump(ir::AstDumper *dumper) const {"decorators", decorators_}}); } +void ClassProperty::Dump(ir::SrcDumper *dumper) const +{ + if (IsPrivate()) { + dumper->Add("private "); + } else if (IsProtected()) { + dumper->Add("protected "); + } else if (IsInternal()) { + dumper->Add("internal "); + } else { + dumper->Add("public "); + } + + if (IsStatic()) { + dumper->Add("static "); + } + + if (IsReadonly()) { + dumper->Add("readonly "); + } + + if (key_ != nullptr) { + key_->Dump(dumper); + } + + if (type_annotation_ != nullptr) { + dumper->Add(": "); + type_annotation_->Dump(dumper); + } + + if (value_ != nullptr) { + dumper->Add(" = "); + value_->Dump(dumper); + } + + dumper->Add(";"); +} + void ClassProperty::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/classProperty.h b/ets2panda/ir/base/classProperty.h index c9e7ae4b9155e27190373811d4029f79db8a3c95..474cd9694367a43e3b2b783ab9f142a6504e0667 100644 --- a/ets2panda/ir/base/classProperty.h +++ b/ets2panda/ir/base/classProperty.h @@ -58,7 +58,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/classStaticBlock.cpp b/ets2panda/ir/base/classStaticBlock.cpp index 4f0b307bbbcdb48f605a2a43a3099db54e0968ba..db53d66665b5e6bdb9815033de16e51a7c5f72b7 100644 --- a/ets2panda/ir/base/classStaticBlock.cpp +++ b/ets2panda/ir/base/classStaticBlock.cpp @@ -21,6 +21,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/decorator.h" #include "ir/base/scriptFunction.h" #include "ir/expression.h" @@ -46,6 +47,11 @@ void ClassStaticBlock::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ClassStaticBlock"}, {"value", value_}}); } +void ClassStaticBlock::Dump([[maybe_unused]] ir::SrcDumper *dumper) const +{ + // NOTE(nsizov): we don't want to show this node +} + void ClassStaticBlock::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/classStaticBlock.h b/ets2panda/ir/base/classStaticBlock.h index 89edc771a7eeb0dd5e676e0294b679f90fbfc764..7debb7fab878e8b1a6668a8f264d3a2ffa8b5a77 100644 --- a/ets2panda/ir/base/classStaticBlock.h +++ b/ets2panda/ir/base/classStaticBlock.h @@ -40,6 +40,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/decorator.cpp b/ets2panda/ir/base/decorator.cpp index beb884f79b09a942806568245b84a242d24ee780..eed3b5f6db795245ea5de04beba6a767083be7d1 100644 --- a/ets2panda/ir/base/decorator.cpp +++ b/ets2panda/ir/base/decorator.cpp @@ -18,6 +18,7 @@ #include "es2panda.h" #include "ir/expression.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/ETSchecker.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" @@ -39,6 +40,11 @@ void Decorator::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "Decorator"}, {"expression", expr_}}); } +void Decorator::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("Decorator"); +} + void Decorator::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/decorator.h b/ets2panda/ir/base/decorator.h index 303ee9f3546466cd1afcea1f960575f9b121efa0..dfbcc9027c1a169d64f2a3ca4333b90588b76964 100644 --- a/ets2panda/ir/base/decorator.h +++ b/ets2panda/ir/base/decorator.h @@ -42,6 +42,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/metaProperty.cpp b/ets2panda/ir/base/metaProperty.cpp index ed43fa1e0c5019d3e1fdc6f123eb67d6f794de51..a5208db90f2428856a38f8611feab9b44c39c97d 100644 --- a/ets2panda/ir/base/metaProperty.cpp +++ b/ets2panda/ir/base/metaProperty.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void MetaProperty::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -49,6 +51,11 @@ void MetaProperty::Compile(compiler::PandaGen *pg) const pg->GetAstCompiler()->Compile(this); } +void MetaProperty::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("MetaProperty"); +} + void MetaProperty::Compile(compiler::ETSGen *etsg) const { etsg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/metaProperty.h b/ets2panda/ir/base/metaProperty.h index 0fd279f9908a98c8611a935b6ee9b4c24abcf9f7..c5eb1bb8448c390cfcb2393bddb5107357585de5 100644 --- a/ets2panda/ir/base/metaProperty.h +++ b/ets2panda/ir/base/metaProperty.h @@ -48,6 +48,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/methodDefinition.cpp b/ets2panda/ir/base/methodDefinition.cpp index 0f261bdc94ac50e061e07d879fe216ecdb420c28..149db9d9b8a5b14121f761a15fff78a5262b95ba 100644 --- a/ets2panda/ir/base/methodDefinition.cpp +++ b/ets2panda/ir/base/methodDefinition.cpp @@ -19,6 +19,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { @@ -120,6 +122,56 @@ void MethodDefinition::Dump(ir::AstDumper *dumper) const {"decorators", decorators_}}); } +void MethodDefinition::Dump(ir::SrcDumper *dumper) const +{ + for (auto method : overloads_) { + method->Dump(dumper); + dumper->Endl(); + } + + if (IsPrivate()) { + dumper->Add("private "); + } else if (IsProtected()) { + dumper->Add("protected "); + } else if (IsInternal()) { + dumper->Add("internal "); + } else { + dumper->Add("public "); + } + + if (IsStatic()) { + dumper->Add("static "); + } + + if (IsAbstract()) { + dumper->Add("abstract "); + } + + if (IsFinal()) { + dumper->Add("final "); + } + + if (IsNative()) { + dumper->Add("native "); + } + + if (IsAsync()) { + dumper->Add("async "); + } + + if (IsOverride()) { + dumper->Add("override "); + } + + if (key_ != nullptr) { + key_->Dump(dumper); + } + + if (value_ != nullptr) { + value_->Dump(dumper); + } +} + void MethodDefinition::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/methodDefinition.h b/ets2panda/ir/base/methodDefinition.h index 8791bed9a38375cc5da38e01ad50f41576005ac7..5221b451fe85106693df0db9bcca360cb0d74eed 100644 --- a/ets2panda/ir/base/methodDefinition.h +++ b/ets2panda/ir/base/methodDefinition.h @@ -111,6 +111,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/property.cpp b/ets2panda/ir/base/property.cpp index 962be16443769caee63d58314ed3f6ee1164c249..113ae62241a34975ed7cacc0fd1b5bd1fdc12b43 100644 --- a/ets2panda/ir/base/property.cpp +++ b/ets2panda/ir/base/property.cpp @@ -19,6 +19,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { Property::Property([[maybe_unused]] Tag const tag, Property const &other, Expression *const key, @@ -177,6 +179,11 @@ void Property::Compile(compiler::ETSGen *etsg) const etsg->GetAstCompiler()->Compile(this); } +void Property::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("Property"); +} + checker::Type *Property::Check(checker::TSChecker *checker) { return checker->GetAnalyzer()->Check(this); diff --git a/ets2panda/ir/base/property.h b/ets2panda/ir/base/property.h index cb9f1636a0dfcc2c6dcef12671c06936895c2861..f69086b21f04cbed9977ea9b10e66ba834ff5ee8 100644 --- a/ets2panda/ir/base/property.h +++ b/ets2panda/ir/base/property.h @@ -111,6 +111,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/scriptFunction.cpp b/ets2panda/ir/base/scriptFunction.cpp index b9224670f5df2936269dbce0af8c28c87e701cc3..2c6753db0fa9925dd9dd3c8464179ef710efd385 100644 --- a/ets2panda/ir/base/scriptFunction.cpp +++ b/ets2panda/ir/base/scriptFunction.cpp @@ -19,6 +19,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { @@ -79,6 +81,49 @@ void ScriptFunction::Dump(ir::AstDumper *dumper) const } } +void ScriptFunction::Dump(ir::SrcDumper *dumper) const +{ + if (TypeParams() != nullptr) { + TypeParams()->Dump(dumper); + } + dumper->Add("("); + for (auto param : Params()) { + param->Dump(dumper); + if (param != Params().back()) { + dumper->Add(", "); + } + } + dumper->Add(")"); + if (ReturnTypeAnnotation() != nullptr) { + dumper->Add(": "); + ReturnTypeAnnotation()->Dump(dumper); + } + + if (IsThrowing()) { + dumper->Add(" throws"); + } + + if (HasBody()) { + if (body_->IsBlockStatement()) { + dumper->Add(" {"); + if (!body_->AsBlockStatement()->Statements().empty()) { + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("}"); + } else { + dumper->Add(" "); + body_->Dump(dumper); + } + } + if (!IsArrow()) { + dumper->Endl(); + } +} + void ScriptFunction::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index a037ff3afd53799ec07ef1e5e86eca856f27410e..f93616a1854c5e05b314b4589f7941f56f0b7e52 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -300,6 +300,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/spreadElement.cpp b/ets2panda/ir/base/spreadElement.cpp index aa6719ad4d18c89d49b5e30816b30e3e2143af4d..6dc6d00c601d3ca943c5a33361796667e059f88f 100644 --- a/ets2panda/ir/base/spreadElement.cpp +++ b/ets2panda/ir/base/spreadElement.cpp @@ -20,6 +20,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/decorator.h" #include "ir/typeNode.h" #include "ir/expressions/arrayExpression.h" @@ -140,6 +141,17 @@ void SpreadElement::Dump(ir::AstDumper *dumper) const {"typeAnnotation", AstDumper::Optional(TypeAnnotation())}}); } +void SpreadElement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("..."); + argument_->Dump(dumper); + auto type = TypeAnnotation(); + if (type != nullptr) { + dumper->Add(": "); + type->Dump(dumper); + } +} + void SpreadElement::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/spreadElement.h b/ets2panda/ir/base/spreadElement.h index 6e507264b332030f2fda9160ae1e311297a1c86a..bd484b6a0f960fc85af61006d1763151934cadfd 100644 --- a/ets2panda/ir/base/spreadElement.h +++ b/ets2panda/ir/base/spreadElement.h @@ -87,6 +87,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/base/templateElement.cpp b/ets2panda/ir/base/templateElement.cpp index f77f0237072aa10032568825d48f6b597cb03ab9..8816ba413cc58502d302ef49f5cb7e747013707e 100644 --- a/ets2panda/ir/base/templateElement.cpp +++ b/ets2panda/ir/base/templateElement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TemplateElement::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -31,6 +33,11 @@ void TemplateElement::Dump(ir::AstDumper *dumper) const }); } +void TemplateElement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TemplateElement"); +} + void TemplateElement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/templateElement.h b/ets2panda/ir/base/templateElement.h index 320770290d8a3f8429aa4480039661e7a2becc38..85154cdd14255dc18b8e3572d3d5ba44e9bac7aa 100644 --- a/ets2panda/ir/base/templateElement.h +++ b/ets2panda/ir/base/templateElement.h @@ -50,6 +50,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/tsIndexSignature.cpp b/ets2panda/ir/base/tsIndexSignature.cpp index 2bea99f714334c9e2337bd36192b2f155d89129c..f48774bd90b1b6601153c399bb9e5b9992083dd7 100644 --- a/ets2panda/ir/base/tsIndexSignature.cpp +++ b/ets2panda/ir/base/tsIndexSignature.cpp @@ -14,7 +14,8 @@ */ #include "tsIndexSignature.h" - +#include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" @@ -46,6 +47,11 @@ void TSIndexSignature::Dump(ir::AstDumper *dumper) const {"readonly", readonly_}}); } +void TSIndexSignature::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSIndexSignature"); +} + void TSIndexSignature::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/tsIndexSignature.h b/ets2panda/ir/base/tsIndexSignature.h index 4c9fd1ae28fdde51b6572adbb0f94b7ffdf402a9..d63443ec4db23cc73f78e3701b28f834415bffe1 100644 --- a/ets2panda/ir/base/tsIndexSignature.h +++ b/ets2panda/ir/base/tsIndexSignature.h @@ -67,6 +67,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/tsMethodSignature.cpp b/ets2panda/ir/base/tsMethodSignature.cpp index ab3eda45912a7250d1c4a6685dc5dc5199ac1505..11d32a2b4b4def7798c3b533ff3e89b3cf11d69d 100644 --- a/ets2panda/ir/base/tsMethodSignature.cpp +++ b/ets2panda/ir/base/tsMethodSignature.cpp @@ -14,7 +14,8 @@ */ #include "tsMethodSignature.h" - +#include "ir/astDump.h" +#include "ir/srcDump.h" #include "varbinder/scope.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" @@ -44,6 +45,11 @@ void TSMethodSignature::Dump(ir::AstDumper *dumper) const {"typeAnnotation", AstDumper::Optional(ReturnTypeAnnotation())}}); } +void TSMethodSignature::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSMethodSignature"); +} + void TSMethodSignature::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/tsMethodSignature.h b/ets2panda/ir/base/tsMethodSignature.h index fdc102d8bd4c096c072d03408e67dbd77e175504..369e482605adb250687b45316884985b85bee92f 100644 --- a/ets2panda/ir/base/tsMethodSignature.h +++ b/ets2panda/ir/base/tsMethodSignature.h @@ -111,6 +111,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/tsPropertySignature.cpp b/ets2panda/ir/base/tsPropertySignature.cpp index ee863b4b7c55c56b1d8f9cbd01cfef6206233a21..e055f5d9474748ddc6bb2857ac7943bacd51b991 100644 --- a/ets2panda/ir/base/tsPropertySignature.cpp +++ b/ets2panda/ir/base/tsPropertySignature.cpp @@ -14,7 +14,8 @@ */ #include "tsPropertySignature.h" - +#include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" @@ -48,6 +49,11 @@ void TSPropertySignature::Dump(ir::AstDumper *dumper) const {"typeAnnotation", AstDumper::Optional(TypeAnnotation())}}); } +void TSPropertySignature::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSPropertySignature"); +} + void TSPropertySignature::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/tsPropertySignature.h b/ets2panda/ir/base/tsPropertySignature.h index 02768c8d69eb6cf90cd157a78c0d186330ea08b6..4a462391d5bc186d83c14f36061c6ee2dbc84d8a 100644 --- a/ets2panda/ir/base/tsPropertySignature.h +++ b/ets2panda/ir/base/tsPropertySignature.h @@ -71,6 +71,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/tsSignatureDeclaration.cpp b/ets2panda/ir/base/tsSignatureDeclaration.cpp index 50825acdf8b86de7b3bae756f4e63179f3682646..cafb5a243f7c8366ccc976deeade245f8a5ca229 100644 --- a/ets2panda/ir/base/tsSignatureDeclaration.cpp +++ b/ets2panda/ir/base/tsSignatureDeclaration.cpp @@ -20,6 +20,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSSignatureDeclaration::TransformChildren(const NodeTransformer &cb) @@ -42,6 +44,11 @@ void TSSignatureDeclaration::Dump(ir::AstDumper *dumper) const {"returnType", AstDumper::Optional(ReturnTypeAnnotation())}}); } +void TSSignatureDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSSignatureDeclaration"); +} + void TSSignatureDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/base/tsSignatureDeclaration.h b/ets2panda/ir/base/tsSignatureDeclaration.h index b24d0abbe52b2658405094c59d65291d5f44f43d..caeeac2d2eb95fc344a8804bce98ac6bd6b8b7a1 100644 --- a/ets2panda/ir/base/tsSignatureDeclaration.h +++ b/ets2panda/ir/base/tsSignatureDeclaration.h @@ -93,7 +93,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsClassLiteral.cpp b/ets2panda/ir/ets/etsClassLiteral.cpp index 0168b7ddaf914abd097dc0ef03156e95ea638138..ad0eabe30565db726b6ce609ef4152c5157c70ad 100644 --- a/ets2panda/ir/ets/etsClassLiteral.cpp +++ b/ets2panda/ir/ets/etsClassLiteral.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSClassLiteral::TransformChildren(const NodeTransformer &cb) @@ -36,6 +37,11 @@ void ETSClassLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSClassLiteral"}}); } +void ETSClassLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ETSClassLiteral"); +} + void ETSClassLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsClassLiteral.h b/ets2panda/ir/ets/etsClassLiteral.h index e9262b2a6d7a3c22a2ad270eaa7475624af73089..592f3536fdab61e8af1a6f23c36c7db931883e49 100644 --- a/ets2panda/ir/ets/etsClassLiteral.h +++ b/ets2panda/ir/ets/etsClassLiteral.h @@ -48,6 +48,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsFunctionType.cpp b/ets2panda/ir/ets/etsFunctionType.cpp index adeff5d2b2325c10fc6283455b4d82fccdcde2ef..f87441758f22eea6e324c5422c93a180323dae78 100644 --- a/ets2panda/ir/ets/etsFunctionType.cpp +++ b/ets2panda/ir/ets/etsFunctionType.cpp @@ -20,6 +20,8 @@ #include "checker/ETSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSFunctionType::TransformChildren(const NodeTransformer &cb) @@ -44,6 +46,27 @@ void ETSFunctionType::Dump(ir::AstDumper *dumper) const } } +void ETSFunctionType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("("); + for (auto *param : Params()) { + param->Dump(dumper); + if (param != Params().back()) { + dumper->Add(", "); + } + } + dumper->Add(")"); + + if (TypeParams() != nullptr) { + TypeParams()->Dump(dumper); + } + + if (ReturnType() != nullptr) { + dumper->Add("=> "); + ReturnType()->Dump(dumper); + } +} + void ETSFunctionType::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -73,4 +96,42 @@ checker::Type *ETSFunctionType::GetType(checker::ETSChecker *checker) { return Check(checker); } + +// NOLINTNEXTLINE(google-default-arguments) +ETSFunctionType *ETSFunctionType::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + ArenaVector params_clone(allocator->Adapter()); + + for (auto *const param : signature_.Params()) { + params_clone.emplace_back(param->Clone(allocator)->AsExpression()); + } + + auto *const type_params_clone = signature_.TypeParams() != nullptr + ? signature_.TypeParams()->Clone(allocator)->AsTSTypeParameterDeclaration() + : nullptr; + auto *const return_type_clone = + signature_.ReturnType() != nullptr ? signature_.ReturnType()->Clone(allocator)->AsTypeNode() : nullptr; + + if (auto *const clone = allocator->New( + FunctionSignature(type_params_clone, std::move(params_clone), return_type_clone), func_flags_); + clone != nullptr) { + if (type_params_clone != nullptr) { + type_params_clone->SetParent(clone); + } + + if (return_type_clone != nullptr) { + return_type_clone->SetParent(clone); + } + + if (parent != nullptr) { + clone->SetParent(parent); + } + + clone->SetScope(scope_); + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsFunctionType.h b/ets2panda/ir/ets/etsFunctionType.h index ca70d67077776efe362c8a3bf3ba1446fcf7c1d0..0de30cdee8a6f0f70ac599dc7ea053634b8d0b0a 100644 --- a/ets2panda/ir/ets/etsFunctionType.h +++ b/ets2panda/ir/ets/etsFunctionType.h @@ -96,6 +96,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; @@ -108,6 +109,9 @@ public: v->Accept(this); } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSFunctionType *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + private: varbinder::Scope *scope_ {}; FunctionSignature signature_; diff --git a/ets2panda/ir/ets/etsLaunchExpression.cpp b/ets2panda/ir/ets/etsLaunchExpression.cpp index 8c006c458827173b3aa9eca42808bcf041167b9b..a492c7c3f23c6b867d27358dcac6bb5403e62d73 100644 --- a/ets2panda/ir/ets/etsLaunchExpression.cpp +++ b/ets2panda/ir/ets/etsLaunchExpression.cpp @@ -19,6 +19,8 @@ #include "compiler/core/ETSGen.h" #include "checker/ETSchecker.h" #include "checker/TSchecker.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { ETSLaunchExpression::ETSLaunchExpression(CallExpression *expr) @@ -41,6 +43,11 @@ void ETSLaunchExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSLaunchExpression"}, {"expr", expr_}}); } +void ETSLaunchExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ETSLaunchExpression"); +} + void ETSLaunchExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsLaunchExpression.h b/ets2panda/ir/ets/etsLaunchExpression.h index b8558ef6ec7d8a4e86b79d189827592d6407e121..5f41d342c81945a35e8e70c640bd38d2c7cb8000 100644 --- a/ets2panda/ir/ets/etsLaunchExpression.h +++ b/ets2panda/ir/ets/etsLaunchExpression.h @@ -50,6 +50,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp b/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp index 7ac314d183e4c1941af0cda89b55d111b22d2eb9..f2685b04974a3fed86b62e5ff63dbed73c85dbcd 100644 --- a/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" namespace panda::es2panda::ir { @@ -41,6 +42,17 @@ void ETSNewArrayInstanceExpression::Dump(ir::AstDumper *dumper) const {{"type", "ETSNewArrayInstanceExpression"}, {"typeReference", type_reference_}, {"dimension", dimension_}}); } +void ETSNewArrayInstanceExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("new "); + ASSERT(type_reference_); + type_reference_->Dump(dumper); + ASSERT(dimension_); + dumper->Add("["); + dimension_->Dump(dumper); + dumper->Add("]"); +} + void ETSNewArrayInstanceExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -67,7 +79,8 @@ ETSNewArrayInstanceExpression *ETSNewArrayInstanceExpression::Clone(ArenaAllocat auto *const type_ref = type_reference_ != nullptr ? type_reference_->Clone(allocator) : nullptr; auto *const dimension = dimension_ != nullptr ? dimension_->Clone(allocator)->AsExpression() : nullptr; - if (auto *const clone = allocator->New(type_ref, dimension); clone != nullptr) { + if (auto *const clone = allocator->New(allocator, type_ref, dimension); + clone != nullptr) { if (type_ref != nullptr) { type_ref->SetParent(clone); } diff --git a/ets2panda/ir/ets/etsNewArrayInstanceExpression.h b/ets2panda/ir/ets/etsNewArrayInstanceExpression.h index 6a5b70f0379c657ba6e98029a19bab126aeaf0f8..073ba2cbc7025d7d7b76127c92d215f70cbc69b9 100644 --- a/ets2panda/ir/ets/etsNewArrayInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewArrayInstanceExpression.h @@ -20,6 +20,7 @@ namespace panda::es2panda::checker { class ETSAnalyzer; +class Signature; } // namespace panda::es2panda::checker namespace panda::es2panda::compiler { @@ -36,10 +37,12 @@ public: NO_COPY_SEMANTIC(ETSNewArrayInstanceExpression); NO_MOVE_SEMANTIC(ETSNewArrayInstanceExpression); - explicit ETSNewArrayInstanceExpression(ir::TypeNode *const type_reference, ir::Expression *const dimension) + explicit ETSNewArrayInstanceExpression(ArenaAllocator *allocator, ir::TypeNode *const type_reference, + ir::Expression *const dimension) : Expression(AstNodeType::ETS_NEW_ARRAY_INSTANCE_EXPRESSION), type_reference_(type_reference), - dimension_(dimension) + dimension_(dimension), + allocator_(allocator) { } // NOTE (csabahurton): these friend relationships can be removed once there are getters for private fields @@ -77,6 +80,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; @@ -90,6 +94,8 @@ public: private: ir::TypeNode *type_reference_; ir::Expression *dimension_; + checker::Signature *default_constructor_signature_ {}; + ArenaAllocator *allocator_; }; } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp b/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp index 21e9c8c6d05d81d5723d30d1ccf4c91a3d72f17e..6d361fb6c023934ec004e989f156607115453b26 100644 --- a/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp @@ -19,6 +19,7 @@ #include "compiler/core/pandagen.h" #include "checker/TSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSNewClassInstanceExpression::TransformChildren(const NodeTransformer &cb) @@ -55,6 +56,22 @@ void ETSNewClassInstanceExpression::Dump(ir::AstDumper *dumper) const {"classBody", AstDumper::Optional(class_def_)}}); } +void ETSNewClassInstanceExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("new "); + if (type_reference_ != nullptr) { + type_reference_->Dump(dumper); + } + dumper->Add("("); + for (auto argument : arguments_) { + argument->Dump(dumper); + if (argument != arguments_.back()) { + dumper->Add(", "); + } + } + dumper->Add(")"); +} + void ETSNewClassInstanceExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsNewClassInstanceExpression.h b/ets2panda/ir/ets/etsNewClassInstanceExpression.h index 69b2bdd25688e46b6a9a3779a7c770433d9320ed..031531ca7439447d3bcfe41b3ee65cbbdda62ab0 100644 --- a/ets2panda/ir/ets/etsNewClassInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewClassInstanceExpression.h @@ -87,7 +87,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp index f927e2d413bc556fcab1f2d9b8ae4a5d5137b202..4455ff7bb508f9cf844e9ea7272cc1019e7ca7a5 100644 --- a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp @@ -19,6 +19,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSNewMultiDimArrayInstanceExpression::TransformChildren(const NodeTransformer &cb) @@ -44,6 +46,18 @@ void ETSNewMultiDimArrayInstanceExpression::Dump(ir::AstDumper *dumper) const {"dimensions", dimensions_}}); } +void ETSNewMultiDimArrayInstanceExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("new "); + ASSERT(type_reference_); + type_reference_->Dump(dumper); + for (auto dim : dimensions_) { + dumper->Add("["); + dim->Dump(dumper); + dumper->Add("]"); + } +} + void ETSNewMultiDimArrayInstanceExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h index 189453beb3e7be97123dfc6dc7b774812655a75d..e031d1f230b67e8b190db7cc1aab14db90f957e3 100644 --- a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h @@ -89,7 +89,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsPackageDeclaration.cpp b/ets2panda/ir/ets/etsPackageDeclaration.cpp index b9fe930d937570c302abbced5ceb93cb917ae21b..27a44e419c25a17abcac4bbef2d311b10f7f4661 100644 --- a/ets2panda/ir/ets/etsPackageDeclaration.cpp +++ b/ets2panda/ir/ets/etsPackageDeclaration.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSPackageDeclaration::TransformChildren(const NodeTransformer &cb) @@ -36,6 +37,14 @@ void ETSPackageDeclaration::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSPackageDeclaration"}, {"name", name_}}); } +void ETSPackageDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("package "); + name_->Dump(dumper); + dumper->Add(";"); + dumper->Endl(); +} + void ETSPackageDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsPackageDeclaration.h b/ets2panda/ir/ets/etsPackageDeclaration.h index b16b4a1d3a62b8da3411586eb58bfcb2c52e2949..88d51f0c08ef9f2c1672532ca4b3abb1334fda04 100644 --- a/ets2panda/ir/ets/etsPackageDeclaration.h +++ b/ets2panda/ir/ets/etsPackageDeclaration.h @@ -40,7 +40,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsParameterExpression.cpp b/ets2panda/ir/ets/etsParameterExpression.cpp index 6065a8a5cea82dab8d4fe67c7e2991ec623d709b..23ac4ebd19f2dbb39d53eba092a0a002a9907112 100644 --- a/ets2panda/ir/ets/etsParameterExpression.cpp +++ b/ets2panda/ir/ets/etsParameterExpression.cpp @@ -21,6 +21,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" #include "ir/expressions/identifier.h" #include "ir/base/spreadElement.h" @@ -143,6 +144,30 @@ void ETSParameterExpression::Dump(ir::AstDumper *const dumper) const } } +void ETSParameterExpression::Dump(ir::SrcDumper *const dumper) const +{ + if (IsRestParameter()) { + spread_->Dump(dumper); + } else { + if (ident_ != nullptr) { + ASSERT(ident_->IsAnnotatedExpression()); + ident_->Dump(dumper); + auto type_annotation = ident_->AsAnnotatedExpression()->TypeAnnotation(); + if (type_annotation != nullptr) { + dumper->Add(": "); + type_annotation->Dump(dumper); + } + } + if (initializer_ != nullptr) { + ASSERT(initializer_->IsNumberLiteral()); + if (initializer_->AsNumberLiteral()->Str().Length() > 0) { + dumper->Add(" = "); + initializer_->Dump(dumper); + } + } + } +} + void ETSParameterExpression::Compile(compiler::PandaGen *const pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsParameterExpression.h b/ets2panda/ir/ets/etsParameterExpression.h index a58975af4f791cf5788e47106ff73ff73990da20..2bbe07083840b6fd49809c749b3416cc2c9857a1 100644 --- a/ets2panda/ir/ets/etsParameterExpression.h +++ b/ets2panda/ir/ets/etsParameterExpression.h @@ -84,6 +84,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void TransformChildren(const NodeTransformer &cb) override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsPrimitiveType.cpp b/ets2panda/ir/ets/etsPrimitiveType.cpp index f8debf89803a4bf2c1e7b11d8194eed7aa09bb42..bfc3562cfad72ea422ce2db83a378064d1a62ae3 100644 --- a/ets2panda/ir/ets/etsPrimitiveType.cpp +++ b/ets2panda/ir/ets/etsPrimitiveType.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSPrimitiveType::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -30,6 +31,41 @@ void ETSPrimitiveType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSPrimitiveType"}}); } +void ETSPrimitiveType::Dump(ir::SrcDumper *dumper) const +{ + switch (GetPrimitiveType()) { + case PrimitiveType::BYTE: + dumper->Add("byte"); + break; + case PrimitiveType::INT: + dumper->Add("int"); + break; + case PrimitiveType::LONG: + dumper->Add("long"); + break; + case PrimitiveType::SHORT: + dumper->Add("short"); + break; + case PrimitiveType::FLOAT: + dumper->Add("float"); + break; + case PrimitiveType::DOUBLE: + dumper->Add("double"); + break; + case PrimitiveType::BOOLEAN: + dumper->Add("boolean"); + break; + case PrimitiveType::CHAR: + dumper->Add("char"); + break; + case PrimitiveType::VOID: + dumper->Add("void"); + break; + default: + UNREACHABLE(); + } +} + void ETSPrimitiveType::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -95,4 +131,19 @@ checker::Type *ETSPrimitiveType::GetType([[maybe_unused]] checker::ETSChecker *c } } } + +// NOLINTNEXTLINE(google-default-arguments) +ETSPrimitiveType *ETSPrimitiveType::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + if (auto *const clone = allocator->New(type_); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} + } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsPrimitiveType.h b/ets2panda/ir/ets/etsPrimitiveType.h index 57cef567c8ff1e67edfe3d76c3475e8556532500..b2ad1451dfa0abbb2cc0729962d0a5fa69004057 100644 --- a/ets2panda/ir/ets/etsPrimitiveType.h +++ b/ets2panda/ir/ets/etsPrimitiveType.h @@ -33,6 +33,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; @@ -45,6 +46,9 @@ public: v->Accept(this); } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSPrimitiveType *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + private: PrimitiveType type_; }; diff --git a/ets2panda/ir/ets/etsStructDeclaration.cpp b/ets2panda/ir/ets/etsStructDeclaration.cpp index 14f7101ad09c6d056deac938079fd3ada3aa2508..37026f4a624b1ec9c717e63a4a15b39a11ebd36d 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.cpp +++ b/ets2panda/ir/ets/etsStructDeclaration.cpp @@ -20,6 +20,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/classDefinition.h" #include "ir/base/decorator.h" #include "ir/expressions/identifier.h" @@ -49,6 +50,11 @@ void ETSStructDeclaration::Dump(ir::AstDumper *dumper) const {{"type", "ETSStructDeclaration"}, {"definition", def_}, {"decorators", AstDumper::Optional(decorators_)}}); } +void ETSStructDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ETSStructDeclaration"); +} + void ETSStructDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsStructDeclaration.h b/ets2panda/ir/ets/etsStructDeclaration.h index 663f295b4bddb068f6656bd5ca2fd5a7f9ad77a2..43159c057294ae11d6ee98d4f68b709ddf608652 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.h +++ b/ets2panda/ir/ets/etsStructDeclaration.h @@ -74,7 +74,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; - + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; diff --git a/ets2panda/ir/ets/etsTuple.cpp b/ets2panda/ir/ets/etsTuple.cpp index e289cfd474ba62bcbfa752f53d041d5f1662f6c3..13c879a2bf986a895be1aee3b7b2a3438c5d4d43 100644 --- a/ets2panda/ir/ets/etsTuple.cpp +++ b/ets2panda/ir/ets/etsTuple.cpp @@ -50,6 +50,22 @@ void ETSTuple::Dump(ir::AstDumper *const dumper) const {"spreadType", AstDumper::Nullish(spread_type_)}}); } +void ETSTuple::Dump(ir::SrcDumper *const dumper) const +{ + dumper->Add("["); + for (auto type_annot : type_annotation_list_) { + type_annot->Dump(dumper); + if (type_annot != type_annotation_list_.back() || spread_type_ != nullptr) { + dumper->Add(", "); + } + } + if (spread_type_ != nullptr) { + dumper->Add("..."); + spread_type_->Dump(dumper); + } + dumper->Add(("]")); +} + void ETSTuple::Compile([[maybe_unused]] compiler::PandaGen *const pg) const {} void ETSTuple::Compile([[maybe_unused]] compiler::ETSGen *const etsg) const {} diff --git a/ets2panda/ir/ets/etsTuple.h b/ets2panda/ir/ets/etsTuple.h index b0d6dc9fb1691f7c7514206b286a06250a8f759a..ce5af95a3a0f5c6d5b0e5f54a00b1de519257fd6 100644 --- a/ets2panda/ir/ets/etsTuple.h +++ b/ets2panda/ir/ets/etsTuple.h @@ -68,6 +68,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/ets/etsTypeReference.cpp b/ets2panda/ir/ets/etsTypeReference.cpp index c763b3b38d0ccdfd656053c733322bddb404cef7..38785bf89bd829f5067552ea3df4fe2d50628a27 100644 --- a/ets2panda/ir/ets/etsTypeReference.cpp +++ b/ets2panda/ir/ets/etsTypeReference.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ts/tsQualifiedName.h" #include "ir/ets/etsTypeReferencePart.h" @@ -34,7 +35,7 @@ void ETSTypeReference::Iterate(const NodeTraverser &cb) const cb(part_); } -ir::Identifier *ETSTypeReference::BaseName() +ir::Identifier *ETSTypeReference::BaseName() const { ir::ETSTypeReferencePart *part_iter = part_; @@ -62,6 +63,12 @@ void ETSTypeReference::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSTypeReference"}, {"part", part_}}); } +void ETSTypeReference::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(part_ != nullptr); + part_->Dump(dumper); +} + void ETSTypeReference::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -99,4 +106,26 @@ checker::Type *ETSTypeReference::GetType(checker::ETSChecker *checker) SetTsType(type); return type; } + +// NOLINTNEXTLINE(google-default-arguments) +ETSTypeReference *ETSTypeReference::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const part_clone = part_ != nullptr ? part_->Clone(allocator)->AsETSTypeReferencePart() : nullptr; + + if (auto *const clone = allocator->New(part_clone); clone != nullptr) { + if (part_clone != nullptr) { + part_clone->SetParent(clone); + } + + clone->flags_ = flags_; + + if (parent != nullptr) { + clone->SetParent(parent); + } + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsTypeReference.h b/ets2panda/ir/ets/etsTypeReference.h index d37661e3736c6f2c134276db0be72af2551d7671..d67d7f49ac4b51c2789cc6f603fe0248791bcace 100644 --- a/ets2panda/ir/ets/etsTypeReference.h +++ b/ets2panda/ir/ets/etsTypeReference.h @@ -36,11 +36,12 @@ public: return part_; } - ir::Identifier *BaseName(); + ir::Identifier *BaseName() const; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; @@ -52,6 +53,9 @@ public: v->Accept(this); } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSTypeReference *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + private: ir::ETSTypeReferencePart *part_; }; diff --git a/ets2panda/ir/ets/etsTypeReferencePart.cpp b/ets2panda/ir/ets/etsTypeReferencePart.cpp index add1f500347c2dbfbe2ecd874f1a9de2ea0dfc1f..e1cbd38964e7926a7ccb8cf4848b5a23855c2532 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.cpp +++ b/ets2panda/ir/ets/etsTypeReferencePart.cpp @@ -21,6 +21,8 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" +#include "macros.h" namespace panda::es2panda::ir { void ETSTypeReferencePart::TransformChildren(const NodeTransformer &cb) @@ -57,6 +59,15 @@ void ETSTypeReferencePart::Dump(ir::AstDumper *dumper) const {"previous", AstDumper::Optional(prev_)}}); } +void ETSTypeReferencePart::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(name_ != nullptr); + name_->Dump(dumper); + if (type_params_ != nullptr) { + type_params_->Dump(dumper); + } +} + void ETSTypeReferencePart::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -79,6 +90,11 @@ checker::Type *ETSTypeReferencePart::Check(checker::ETSChecker *checker) checker::Type *ETSTypeReferencePart::GetType(checker::ETSChecker *checker) { if (prev_ == nullptr) { + if ((name_->IsIdentifier()) && (name_->AsIdentifier()->Variable() != nullptr) && + (name_->AsIdentifier()->Variable()->Declaration()->IsTypeAliasDecl())) { + return checker->HandleTypeAlias(name_, type_params_); + } + checker::Type *base_type = checker->GetReferencedTypeBase(name_); ASSERT(base_type != nullptr); @@ -93,4 +109,36 @@ checker::Type *ETSTypeReferencePart::GetType(checker::ETSChecker *checker) checker::Type *base_type = prev_->GetType(checker); return checker->GetReferencedTypeFromBase(base_type, name_); } + +// NOLINTNEXTLINE(google-default-arguments) +ETSTypeReferencePart *ETSTypeReferencePart::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const name_clone = name_ != nullptr ? name_->Clone(allocator)->AsExpression() : nullptr; + auto *const type_params_clone = + type_params_ != nullptr ? type_params_->Clone(allocator)->AsTSTypeParameterInstantiation() : nullptr; + auto *const prev_clone = prev_ != nullptr ? prev_->Clone(allocator)->AsETSTypeReferencePart() : nullptr; + + if (auto *const clone = allocator->New(name_clone, type_params_clone, prev_clone); + clone != nullptr) { + if (name_clone != nullptr) { + name_clone->SetParent(clone); + } + + if (type_params_clone != nullptr) { + type_params_clone->SetParent(clone); + } + + if (prev_clone != nullptr) { + prev_clone->SetParent(clone); + } + + if (parent != nullptr) { + clone->SetParent(parent); + } + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsTypeReferencePart.h b/ets2panda/ir/ets/etsTypeReferencePart.h index c5a50264427a2ddf1fb4432feb5ac08edbcca078..dce25c9177eb926990b572721cddbe8d3ec8d09f 100644 --- a/ets2panda/ir/ets/etsTypeReferencePart.h +++ b/ets2panda/ir/ets/etsTypeReferencePart.h @@ -58,6 +58,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; @@ -69,6 +70,9 @@ public: v->Accept(this); } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSTypeReferencePart *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + private: ir::Expression *name_; ir::TSTypeParameterInstantiation *type_params_ {}; diff --git a/ets2panda/ir/ets/etsUnionType.cpp b/ets2panda/ir/ets/etsUnionType.cpp index 3b07a4446d5679263e5234b2e36b3448ca3220f1..1e00c17a7c07f0dc642188edc3f88c8792c45592 100644 --- a/ets2panda/ir/ets/etsUnionType.cpp +++ b/ets2panda/ir/ets/etsUnionType.cpp @@ -17,6 +17,7 @@ #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ETSUnionType::TransformChildren(const NodeTransformer &cb) @@ -38,6 +39,16 @@ void ETSUnionType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ETSUnionType"}, {"types", types_}}); } +void ETSUnionType::Dump(ir::SrcDumper *dumper) const +{ + for (auto type : types_) { + type->Dump(dumper); + if (type != types_.back()) { + dumper->Add(" | "); + } + } +} + void ETSUnionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} checker::Type *ETSUnionType::Check([[maybe_unused]] checker::TSChecker *checker) diff --git a/ets2panda/ir/ets/etsUnionType.h b/ets2panda/ir/ets/etsUnionType.h index 9b0e96ff4ed1e1875f8f1bc1e8bbeed007c00d1a..48b942d2cbcd75483e8f8699621c6aae1401a7ac 100644 --- a/ets2panda/ir/ets/etsUnionType.h +++ b/ets2panda/ir/ets/etsUnionType.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/ets/etsWildcardType.cpp b/ets2panda/ir/ets/etsWildcardType.cpp index 1ca7675248050e7252ed434b373615789367bfa6..f8c5a1880142c733110c74945c9a7986560b47c2 100644 --- a/ets2panda/ir/ets/etsWildcardType.cpp +++ b/ets2panda/ir/ets/etsWildcardType.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ets/etsTypeReference.h" namespace panda::es2panda::ir { @@ -45,6 +46,11 @@ void ETSWildcardType::Dump(ir::AstDumper *dumper) const {"out", AstDumper::Optional(IsOut())}}); } +void ETSWildcardType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ETSWildcardType"); +} + void ETSWildcardType::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ets/etsWildcardType.h b/ets2panda/ir/ets/etsWildcardType.h index b380fd348e85f68c0230e4e5bdd9598f8f3c194f..d633409a40732390799f964f2d62fdc800f4f073 100644 --- a/ets2panda/ir/ets/etsWildcardType.h +++ b/ets2panda/ir/ets/etsWildcardType.h @@ -37,6 +37,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/arrayExpression.cpp b/ets2panda/ir/expressions/arrayExpression.cpp index e620acd9eda9e729a7553f2c251ec7910c5e1d63..b1b2ef87a7e0a3a03a1d4e129890ab665c9ef780 100644 --- a/ets2panda/ir/expressions/arrayExpression.cpp +++ b/ets2panda/ir/expressions/arrayExpression.cpp @@ -24,11 +24,12 @@ #include "compiler/core/pandagen.h" #include "ir/astDump.h" #include "ir/base/decorator.h" +#include "ir/srcDump.h" +#include "ir/typeNode.h" #include "ir/base/spreadElement.h" #include "ir/expressions/assignmentExpression.h" #include "ir/expressions/identifier.h" #include "ir/expressions/objectExpression.h" -#include "ir/typeNode.h" #include "util/helpers.h" namespace panda::es2panda::ir { @@ -204,6 +205,18 @@ void ArrayExpression::Dump(ir::AstDumper *dumper) const {"optional", AstDumper::Optional(optional_)}}); } +void ArrayExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("["); + for (auto element : elements_) { + element->Dump(dumper); + if (element != elements_.back()) { + dumper->Add(", "); + } + } + dumper->Add("]"); +} + void ArrayExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/arrayExpression.h b/ets2panda/ir/expressions/arrayExpression.h index 50284260f208a837c1edcf8eb40f11e7626ae473..cd5f4132d0dc8e5926802fea3e3e1f635875516e 100644 --- a/ets2panda/ir/expressions/arrayExpression.h +++ b/ets2panda/ir/expressions/arrayExpression.h @@ -126,6 +126,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.cpp b/ets2panda/ir/expressions/arrowFunctionExpression.cpp index 249aa83775ca2fd280db3a579968c7057328e502..7d568360b746d9f1889d7753f61cdc95283cca46 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.cpp +++ b/ets2panda/ir/expressions/arrowFunctionExpression.cpp @@ -21,6 +21,7 @@ #include "checker/ets/typeRelationContext.h" #include "checker/TSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/scriptFunction.h" #include "ir/ets/etsTypeReference.h" #include "ir/ets/etsTypeReferencePart.h" @@ -44,6 +45,11 @@ void ArrowFunctionExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ArrowFunctionExpression"}, {"function", func_}}); } +void ArrowFunctionExpression::Dump(ir::SrcDumper *dumper) const +{ + func_->Dump(dumper); +} + void ArrowFunctionExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -133,11 +139,10 @@ ir::TypeNode *ArrowFunctionExpression::CreateTypeAnnotation(checker::ETSChecker return_node = Function()->ReturnTypeAnnotation(); } - auto *param_scope = checker->Scope()->AsFunctionScope()->ParamScope(); - auto signature = ir::FunctionSignature(nullptr, std::move(Function()->Params()), return_node); + auto orig_params = Function()->Params(); + auto signature = ir::FunctionSignature(nullptr, std::move(orig_params), return_node); auto *func_type = checker->Allocator()->New(std::move(signature), ir::ScriptFunctionFlags::NONE); - func_type->SetScope(param_scope); return_node->SetParent(func_type); return func_type; } diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.h b/ets2panda/ir/expressions/arrowFunctionExpression.h index e7cd2df79b5d593c57bc0d7c93422c47db506bf3..bc40519398f98760eec73ab823ad2883492cbc74 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.h +++ b/ets2panda/ir/expressions/arrowFunctionExpression.h @@ -89,6 +89,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/assignmentExpression.cpp b/ets2panda/ir/expressions/assignmentExpression.cpp index 175f28f0ff00107c132aabdb32ee27ed377af974..d916ecf588f5b5a476bc5f8bf28728a44044337d 100644 --- a/ets2panda/ir/expressions/assignmentExpression.cpp +++ b/ets2panda/ir/expressions/assignmentExpression.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/regScope.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/scriptFunction.h" #include "ir/base/spreadElement.h" #include "ir/expressions/identifier.h" @@ -119,6 +120,17 @@ void AssignmentExpression::Dump(ir::AstDumper *dumper) const } } +void AssignmentExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(left_); + left_->Dump(dumper); + dumper->Add(" "); + dumper->Add(TokenToString(operator_)); + dumper->Add(" "); + ASSERT(right_); + right_->Dump(dumper); +} + void AssignmentExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/assignmentExpression.h b/ets2panda/ir/expressions/assignmentExpression.h index 86a514565c5baa4882ad8e19375ea60b6c83c31b..976f93ca594ef25aae64a274b1757329721ed20d 100644 --- a/ets2panda/ir/expressions/assignmentExpression.h +++ b/ets2panda/ir/expressions/assignmentExpression.h @@ -120,6 +120,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; void CompilePattern(compiler::PandaGen *pg) const; diff --git a/ets2panda/ir/expressions/awaitExpression.cpp b/ets2panda/ir/expressions/awaitExpression.cpp index f0fac3d9b4137445cdb5c17883b24b020d4cd30c..5048bd86c4c62fe1875b9616575222fb9a2b907f 100644 --- a/ets2panda/ir/expressions/awaitExpression.cpp +++ b/ets2panda/ir/expressions/awaitExpression.cpp @@ -19,6 +19,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void AwaitExpression::TransformChildren(const NodeTransformer &cb) @@ -40,6 +41,11 @@ void AwaitExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "AwaitExpression"}, {"argument", AstDumper::Nullish(argument_)}}); } +void AwaitExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("AwaitExpression"); +} + void AwaitExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/awaitExpression.h b/ets2panda/ir/expressions/awaitExpression.h index 1a0a6c46c6b4d9c5e0c202f3549920c6a4b42ed8..ab1160ad375e45e0d074c89472bd8a47f42346fd 100644 --- a/ets2panda/ir/expressions/awaitExpression.h +++ b/ets2panda/ir/expressions/awaitExpression.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/binaryExpression.cpp b/ets2panda/ir/expressions/binaryExpression.cpp index 4da4ae6df90367cfee6b8b20a34fbc287ffe06bf..8375458ceb63a1097a5e50a663deeef6310b6776 100644 --- a/ets2panda/ir/expressions/binaryExpression.cpp +++ b/ets2panda/ir/expressions/binaryExpression.cpp @@ -18,6 +18,8 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "checker/TSchecker.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void BinaryExpression::TransformChildren(const NodeTransformer &cb) @@ -40,6 +42,19 @@ void BinaryExpression::Dump(ir::AstDumper *dumper) const {"right", right_}}); } +void BinaryExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(left_ != nullptr); + ASSERT(right_ != nullptr); + dumper->Add("("); + left_->Dump(dumper); + dumper->Add(" "); + dumper->Add(TokenToString(operator_)); + dumper->Add(" "); + right_->Dump(dumper); + dumper->Add(")"); +} + void BinaryExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/binaryExpression.h b/ets2panda/ir/expressions/binaryExpression.h index 89849ee786352ec556b6ddf252e18d7d77ae2405..82a2f61ba0597ad80ea7db433bfcee592d3d0ac3 100644 --- a/ets2panda/ir/expressions/binaryExpression.h +++ b/ets2panda/ir/expressions/binaryExpression.h @@ -148,6 +148,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/blockExpression.cpp b/ets2panda/ir/expressions/blockExpression.cpp index 02e3ff477e246e528a8228340ac990ac50948f50..f20c401f648b63ac0a373b2819380d68e413c35a 100644 --- a/ets2panda/ir/expressions/blockExpression.cpp +++ b/ets2panda/ir/expressions/blockExpression.cpp @@ -16,6 +16,7 @@ #include "blockExpression.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "compiler/core/ETSGen.h" #include "checker/ETSchecker.h" #include "ir/astNode.h" @@ -70,6 +71,16 @@ void BlockExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "BlockExpression"}, {"statements", statements_}}); } +void BlockExpression::Dump(ir::SrcDumper *dumper) const +{ + for (auto *statement : statements_) { + statement->Dump(dumper); + if (statement != statements_.back()) { + dumper->Endl(); + } + } +} + void BlockExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const { UNREACHABLE(); diff --git a/ets2panda/ir/expressions/blockExpression.h b/ets2panda/ir/expressions/blockExpression.h index 9e0a892b9abea8fdc3399fac0a0bb77b5033f0e9..6c6329ccd290328d853051c071a39ee337e183b7 100644 --- a/ets2panda/ir/expressions/blockExpression.h +++ b/ets2panda/ir/expressions/blockExpression.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/expressions/callExpression.cpp b/ets2panda/ir/expressions/callExpression.cpp index ede576373ea70522612a61638b02c15583b846f2..fc1be7c61162150cbf3cf433b82f42083f40424f 100644 --- a/ets2panda/ir/expressions/callExpression.cpp +++ b/ets2panda/ir/expressions/callExpression.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void CallExpression::TransformChildren(const NodeTransformer &cb) @@ -59,6 +61,20 @@ void CallExpression::Dump(ir::AstDumper *dumper) const {"typeParameters", AstDumper::Optional(type_params_)}}); } +void CallExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(callee_); + callee_->Dump(dumper); + dumper->Add("("); + for (auto arg : arguments_) { + arg->Dump(dumper); + if (arg != arguments_.back()) { + dumper->Add(", "); + } + } + dumper->Add(")"); +} + void CallExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/callExpression.h b/ets2panda/ir/expressions/callExpression.h index fc2add2887fb29a36668a61cb0e94654d3ee96fe..e95b9ac202d7fbdc83698ed533dccbcb27294ab4 100644 --- a/ets2panda/ir/expressions/callExpression.h +++ b/ets2panda/ir/expressions/callExpression.h @@ -121,6 +121,16 @@ public: type_params_ = type_params; } + [[nodiscard]] checker::Type *UncheckedType() const noexcept + { + return unchecked_type_; + } + + void SetUncheckedType(checker::Type *type) noexcept + { + unchecked_type_ = type; + } + void SetTrailingBlock(ir::BlockStatement *const block) noexcept { trailing_block_ = block; @@ -148,6 +158,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; @@ -168,6 +179,7 @@ protected: // for trailing lambda feature in ets ir::BlockStatement *trailing_block_ {}; bool is_trailing_block_in_new_line_ {false}; + checker::Type *unchecked_type_ {}; // NOLINTEND(misc-non-private-member-variables-in-classes) private: diff --git a/ets2panda/ir/expressions/chainExpression.cpp b/ets2panda/ir/expressions/chainExpression.cpp index c7bddde72b0cdfb24d548f66af60e3e756b21a62..0c2ee1a5d0c1e53afe38033eaba119cb3b46d912 100644 --- a/ets2panda/ir/expressions/chainExpression.cpp +++ b/ets2panda/ir/expressions/chainExpression.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/memberExpression.h" namespace panda::es2panda::ir { @@ -37,6 +38,11 @@ void ChainExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ChainExpression"}, {"expression", expression_}}); } +void ChainExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ChainExpression"); +} + void ChainExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/chainExpression.h b/ets2panda/ir/expressions/chainExpression.h index 33e41e4cdba752920a1f3ccef98818d1d5b3edc0..0bd2bdb17220c1d82a66aec66a7fc10b565603bb 100644 --- a/ets2panda/ir/expressions/chainExpression.h +++ b/ets2panda/ir/expressions/chainExpression.h @@ -51,6 +51,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; void CompileToReg(compiler::PandaGen *pg, compiler::VReg &obj_reg) const; diff --git a/ets2panda/ir/expressions/classExpression.cpp b/ets2panda/ir/expressions/classExpression.cpp index b2fb7234fb02998b0509564d21397749e2fe10b0..6caf00999f2954395085a8dabebfe401ab4859b4 100644 --- a/ets2panda/ir/expressions/classExpression.cpp +++ b/ets2panda/ir/expressions/classExpression.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ClassExpression::TransformChildren(const NodeTransformer &cb) @@ -36,6 +37,11 @@ void ClassExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ClassExpression"}, {"definition", def_}}); } +void ClassExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ClassExpression"); +} + void ClassExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/classExpression.h b/ets2panda/ir/expressions/classExpression.h index 4970733dc93e49fed4869cf15ece2d6c8eba4fa0..3277ec23ae89f68e4ca2ee43640cc600fc100ab3 100644 --- a/ets2panda/ir/expressions/classExpression.h +++ b/ets2panda/ir/expressions/classExpression.h @@ -43,6 +43,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/conditionalExpression.cpp b/ets2panda/ir/expressions/conditionalExpression.cpp index 7c6c572c9f5beb283c48e87899eb7e5068f3cfcd..c75255d97188df4c7fbe4530b8f751ac084ef92d 100644 --- a/ets2panda/ir/expressions/conditionalExpression.cpp +++ b/ets2panda/ir/expressions/conditionalExpression.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ConditionalExpression::TransformChildren(const NodeTransformer &cb) @@ -40,6 +42,26 @@ void ConditionalExpression::Dump(ir::AstDumper *dumper) const {{"type", "ConditionalExpression"}, {"test", test_}, {"consequent", consequent_}, {"alternate", alternate_}}); } +void ConditionalExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(test_ != nullptr); + dumper->Add("("); + test_->Dump(dumper); + dumper->Add(" ? "); + if (consequent_ != nullptr) { + consequent_->Dump(dumper); + } + dumper->Add(" : "); + if (alternate_ != nullptr) { + alternate_->Dump(dumper); + } + dumper->Add(")"); + if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) { + dumper->Add(";"); + dumper->Endl(); + } +} + void ConditionalExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/conditionalExpression.h b/ets2panda/ir/expressions/conditionalExpression.h index 19cab1b07196380fdcb158257e9d63abdb0ece7d..58d76d14df7f2f0adbc90a9fd10783b192e564af 100644 --- a/ets2panda/ir/expressions/conditionalExpression.h +++ b/ets2panda/ir/expressions/conditionalExpression.h @@ -72,6 +72,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; diff --git a/ets2panda/ir/expressions/functionExpression.cpp b/ets2panda/ir/expressions/functionExpression.cpp index f8add6e7234e48c6f4a74b28da9f7b43106c9fdc..243a94df429a4bdb343ce2627d78015975ed0efc 100644 --- a/ets2panda/ir/expressions/functionExpression.cpp +++ b/ets2panda/ir/expressions/functionExpression.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void FunctionExpression::TransformChildren(const NodeTransformer &cb) @@ -36,6 +37,11 @@ void FunctionExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "FunctionExpression"}, {"function", func_}}); } +void FunctionExpression::Dump(ir::SrcDumper *dumper) const +{ + func_->Dump(dumper); +} + void FunctionExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/functionExpression.h b/ets2panda/ir/expressions/functionExpression.h index 754e520e350951d5a9691c8305c8bc5636b100b3..265bdc5fd3b59c4190914f5bfc65bbddd5d6d881 100644 --- a/ets2panda/ir/expressions/functionExpression.h +++ b/ets2panda/ir/expressions/functionExpression.h @@ -65,6 +65,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/identifier.cpp b/ets2panda/ir/expressions/identifier.cpp index 1913c2e1a1106f8be10bed070f2da6cdf2be1c27..03d77fb78166ad73d985284bf04b022e10861904 100644 --- a/ets2panda/ir/expressions/identifier.cpp +++ b/ets2panda/ir/expressions/identifier.cpp @@ -20,6 +20,8 @@ #include "checker/TSchecker.h" #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { Identifier::Identifier([[maybe_unused]] Tag const tag, Identifier const &other, ArenaAllocator *const allocator) @@ -91,6 +93,17 @@ void Identifier::Dump(ir::AstDumper *dumper) const {"decorators", decorators_}}); } +void Identifier::Dump(ir::SrcDumper *dumper) const +{ + if (IsPrivateIdent()) { + dumper->Add("private "); + } + dumper->Add(std::string(name_)); + if (IsOptional()) { + dumper->Add("?"); + } +} + void Identifier::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/identifier.h b/ets2panda/ir/expressions/identifier.h index 7c470996487a5bff38bd428eb069aaad231bd189..acbb027f93131d7e27c808e0965172a2d96f1b77 100644 --- a/ets2panda/ir/expressions/identifier.h +++ b/ets2panda/ir/expressions/identifier.h @@ -203,6 +203,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/importExpression.cpp b/ets2panda/ir/expressions/importExpression.cpp index c9d711027c00c9b54372f549cb3d629bcba390be..21a048c903dcaf08dab58a2b38a3546ee291101f 100644 --- a/ets2panda/ir/expressions/importExpression.cpp +++ b/ets2panda/ir/expressions/importExpression.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ImportExpression::TransformChildren(const NodeTransformer &cb) @@ -35,6 +37,11 @@ void ImportExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ImportExpression"}, {"source", source_}}); } +void ImportExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ImportExpression"); +} + void ImportExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/importExpression.h b/ets2panda/ir/expressions/importExpression.h index c4a099adeb1dfd05e62646a3fbfa8aa3ae2dcf1c..4ac17ab18829b21685e1b5da20902fe239257cde 100644 --- a/ets2panda/ir/expressions/importExpression.h +++ b/ets2panda/ir/expressions/importExpression.h @@ -40,6 +40,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/bigIntLiteral.cpp b/ets2panda/ir/expressions/literals/bigIntLiteral.cpp index b721e9db21a5c78a8259908b7e0975262a2ca0ba..a12339916cf09c833b73c2bb68976b8dfc8dcdc6 100644 --- a/ets2panda/ir/expressions/literals/bigIntLiteral.cpp +++ b/ets2panda/ir/expressions/literals/bigIntLiteral.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void BigIntLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -29,6 +30,11 @@ void BigIntLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "BigIntLiteral"}, {"value", src_}}); } +void BigIntLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add(std::string(src_)); +} + void BigIntLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/bigIntLiteral.h b/ets2panda/ir/expressions/literals/bigIntLiteral.h index 1180cc788c848d09ff0c949b77e2853cfe29b491..0ae06c26404386e01c7bb54e8af482b40257ef71 100644 --- a/ets2panda/ir/expressions/literals/bigIntLiteral.h +++ b/ets2panda/ir/expressions/literals/bigIntLiteral.h @@ -41,6 +41,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/booleanLiteral.cpp b/ets2panda/ir/expressions/literals/booleanLiteral.cpp index 1eb3eee725f0a60ea9df530ab55b7667cfb219b4..40bfa7bc655ee69bfa3c3dfc49ae8096f6ca392d 100644 --- a/ets2panda/ir/expressions/literals/booleanLiteral.cpp +++ b/ets2panda/ir/expressions/literals/booleanLiteral.cpp @@ -20,6 +20,7 @@ #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void BooleanLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -30,6 +31,11 @@ void BooleanLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "BooleanLiteral"}, {"value", boolean_}}); } +void BooleanLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add(boolean_ ? "true" : "false"); +} + void BooleanLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/booleanLiteral.h b/ets2panda/ir/expressions/literals/booleanLiteral.h index 8974de1659cd941be07637d870f90bee5fbfb91c..88f62c6bf7280a0a8810fb478c66b0eaf201f8c5 100644 --- a/ets2panda/ir/expressions/literals/booleanLiteral.h +++ b/ets2panda/ir/expressions/literals/booleanLiteral.h @@ -40,6 +40,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/charLiteral.cpp b/ets2panda/ir/expressions/literals/charLiteral.cpp index 0a6adcf98d4e1c3056de72fcd47c9af2f197e251..ce4be05ffc2477b40d3f8b1285826beb78cb5d3c 100644 --- a/ets2panda/ir/expressions/literals/charLiteral.cpp +++ b/ets2panda/ir/expressions/literals/charLiteral.cpp @@ -20,7 +20,9 @@ #include "compiler/core/ETSGen.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" +#include #include namespace panda::es2panda::ir { @@ -32,6 +34,11 @@ void CharLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "CharLiteral"}, {"value", char_}}); } +void CharLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add(std::to_string(char_)); +} + void CharLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/charLiteral.h b/ets2panda/ir/expressions/literals/charLiteral.h index 619acec7b60e7c6b9cf7accb35626e61bd375efb..629e1a0d036151a3cbda6cc6a2bb4a19a3733274 100644 --- a/ets2panda/ir/expressions/literals/charLiteral.h +++ b/ets2panda/ir/expressions/literals/charLiteral.h @@ -46,6 +46,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) 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; diff --git a/ets2panda/ir/expressions/literals/nullLiteral.cpp b/ets2panda/ir/expressions/literals/nullLiteral.cpp index f04d88b766385a9e260d2f7cc285ff2736a5938d..74c0e578162e27db70a51af734be3e39f602404a 100644 --- a/ets2panda/ir/expressions/literals/nullLiteral.cpp +++ b/ets2panda/ir/expressions/literals/nullLiteral.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void NullLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void NullLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "NullLiteral"}, {"value", AstDumper::Property::Constant::PROP_NULL}}); } +void NullLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("null"); +} + void NullLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/nullLiteral.h b/ets2panda/ir/expressions/literals/nullLiteral.h index 9089ec475715438aeae7f205bbeb65bbe972c0ee..4fb46bb414389a8f35e34285f693b6f90e81dd47 100644 --- a/ets2panda/ir/expressions/literals/nullLiteral.h +++ b/ets2panda/ir/expressions/literals/nullLiteral.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/numberLiteral.cpp b/ets2panda/ir/expressions/literals/numberLiteral.cpp index af42b4f5124cb49f114ad9383fa0336e0685bf90..dc7e40501b5050e86919f7ca6ca722669a150878 100644 --- a/ets2panda/ir/expressions/literals/numberLiteral.cpp +++ b/ets2panda/ir/expressions/literals/numberLiteral.cpp @@ -14,10 +14,13 @@ */ #include "numberLiteral.h" +#include #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void NumberLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +31,29 @@ void NumberLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "NumberLiteral"}, {"value", number_}}); } +void NumberLiteral::Dump(ir::SrcDumper *dumper) const +{ + if (number_.IsInt()) { + dumper->Add(number_.GetInt()); + return; + } + + if (number_.IsLong()) { + dumper->Add(number_.GetLong()); + return; + } + + if (number_.IsFloat()) { + dumper->Add(number_.GetFloat()); + return; + } + + if (number_.IsDouble()) { + dumper->Add(number_.GetDouble()); + return; + } +} + void NumberLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/numberLiteral.h b/ets2panda/ir/expressions/literals/numberLiteral.h index 6b05f8e34e6e9dc851e280f4ce4859647599fe6f..0abcbbce6ddbe60216a9803378b4413654f47078 100644 --- a/ets2panda/ir/expressions/literals/numberLiteral.h +++ b/ets2panda/ir/expressions/literals/numberLiteral.h @@ -55,6 +55,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/regExpLiteral.cpp b/ets2panda/ir/expressions/literals/regExpLiteral.cpp index 353d8fabb251a080ece8fdfb2bce87b42201acf2..d735658aafca94a3f57ca5a5095b8a4a38e35d14 100644 --- a/ets2panda/ir/expressions/literals/regExpLiteral.cpp +++ b/ets2panda/ir/expressions/literals/regExpLiteral.cpp @@ -20,6 +20,8 @@ #include "compiler/core/regScope.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void RegExpLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -30,6 +32,11 @@ void RegExpLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "RegExpLiteral"}, {"source", pattern_}, {"flags", flags_str_}}); } +void RegExpLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add(std::string(pattern_)); +} + void RegExpLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/regExpLiteral.h b/ets2panda/ir/expressions/literals/regExpLiteral.h index 57be86c6c2c387cb8fed093a0f003554785b5ba0..19b8582d1bb527f587c895b8f6454c06043a8643 100644 --- a/ets2panda/ir/expressions/literals/regExpLiteral.h +++ b/ets2panda/ir/expressions/literals/regExpLiteral.h @@ -50,6 +50,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/stringLiteral.cpp b/ets2panda/ir/expressions/literals/stringLiteral.cpp index 1406de9d88d2740d1acc685cfea6bdffbb35e3da..995b112d9b47824d446c1b99498c1edeb57b908b 100644 --- a/ets2panda/ir/expressions/literals/stringLiteral.cpp +++ b/ets2panda/ir/expressions/literals/stringLiteral.cpp @@ -14,10 +14,14 @@ */ #include "stringLiteral.h" +#include #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" +#include "macros.h" namespace panda::es2panda::ir { void StringLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +32,41 @@ void StringLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "StringLiteral"}, {"value", str_}}); } +void StringLiteral::Dump(ir::SrcDumper *dumper) const +{ + std::string str(str_); + std::string escaped_str; + escaped_str.push_back('\"'); + for (size_t i = 0, j = str_.Length(); i < j; ++i) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + const char c = str_.Bytes()[i]; + // check if a given character is printable + // the cast is necessary to avoid undefined behaviour + if (std::isprint(static_cast(c)) != 0U) { + escaped_str.push_back(c); + } else { + escaped_str.push_back('\\'); + if (c == '\n') { + escaped_str.push_back('n'); + } else if (c == '\t') { + escaped_str.push_back('t'); + } else if (c == '\v') { + escaped_str.push_back('v'); + } else if (c == '\f') { + escaped_str.push_back('f'); + } else if (c == '\r') { + escaped_str.push_back('r'); + } else if (c == '\0') { + escaped_str.push_back('0'); + } else { + UNREACHABLE(); + } + } + } + escaped_str.push_back('\"'); + dumper->Add(escaped_str); +} + void StringLiteral::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/literals/stringLiteral.h b/ets2panda/ir/expressions/literals/stringLiteral.h index 09a19409de97040fbee70ccecdb70f5be0c8e464..081eed374fde1f4fce86bef53f75795b3a9095b7 100644 --- a/ets2panda/ir/expressions/literals/stringLiteral.h +++ b/ets2panda/ir/expressions/literals/stringLiteral.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/literals/undefinedLiteral.cpp b/ets2panda/ir/expressions/literals/undefinedLiteral.cpp index 2b2bc2ccc6c7e8f1ec03a1ee78da27c6fb854b6f..27bddb8b4078d63f8edf4676ad83d07621c24b6f 100644 --- a/ets2panda/ir/expressions/literals/undefinedLiteral.cpp +++ b/ets2panda/ir/expressions/literals/undefinedLiteral.cpp @@ -20,6 +20,7 @@ #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void UndefinedLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -30,6 +31,11 @@ void UndefinedLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "UndefinedLiteral"}, {"value", AstDumper::Property::Constant::PROP_UNDEFINED}}); } +void UndefinedLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("undefined"); +} + void UndefinedLiteral::Compile(compiler::PandaGen *pg) const { pg->LoadConst(this, compiler::Constant::JS_UNDEFINED); diff --git a/ets2panda/ir/expressions/literals/undefinedLiteral.h b/ets2panda/ir/expressions/literals/undefinedLiteral.h index 3393da272fd3d783b16b4e23a6be968fc2fe832f..818867cde59e9820577967e67411f4bf873a9e30 100644 --- a/ets2panda/ir/expressions/literals/undefinedLiteral.h +++ b/ets2panda/ir/expressions/literals/undefinedLiteral.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index f099705db6b859f73037a56c54aff93ba87346b0..68cfc53cbbf0fc54e86e521c87f8911b0e073153 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -19,6 +19,8 @@ #include "checker/ets/castingContext.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { MemberExpression::MemberExpression([[maybe_unused]] Tag const tag, MemberExpression const &other, @@ -62,6 +64,29 @@ void MemberExpression::Dump(ir::AstDumper *dumper) const {"optional", IsOptional()}}); } +void MemberExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(object_ != nullptr); + ASSERT(property_ != nullptr); + + object_->Dump(dumper); + if (IsOptional()) { + dumper->Add("?"); + } + if ((MemberExpressionKind::ELEMENT_ACCESS & kind_) != 0U) { + dumper->Add("["); + property_->Dump(dumper); + dumper->Add("]"); + } else { + dumper->Add("."); + property_->Dump(dumper); + } + if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) { + dumper->Add(";"); + dumper->Endl(); + } +} + void MemberExpression::LoadRhs(compiler::PandaGen *pg) const { compiler::RegScope rs(pg); @@ -176,22 +201,26 @@ checker::Type *MemberExpression::CheckUnionMember(checker::ETSChecker *checker, common_prop_type = member_type; }; for (auto *const type : union_type->ConstituentTypes()) { - if (type->IsETSObjectType()) { - SetObjectType(type->AsETSObjectType()); + auto *const apparent = checker->GetApparentType(type); + if (apparent->IsETSObjectType()) { + SetObjectType(apparent->AsETSObjectType()); add_prop_type(ResolveObjectMember(checker).first); - } else if (type->IsETSEnumType() || base_type->IsETSStringEnumType()) { - add_prop_type(ResolveEnumMember(checker, type).first); + } else if (apparent->IsETSEnumType() || base_type->IsETSStringEnumType()) { + add_prop_type(ResolveEnumMember(checker, apparent).first); } else { UNREACHABLE(); } } - SetObjectType(union_type->GetLeastUpperBoundType(checker)->AsETSObjectType()); + SetObjectType(union_type->GetLeastUpperBoundType()->AsETSObjectType()); return common_prop_type; } -checker::Type *MemberExpression::AdjustOptional(checker::ETSChecker *checker, checker::Type *type) +checker::Type *MemberExpression::AdjustType(checker::ETSChecker *checker, checker::Type *type) { SetOptionalType(type); + if (PropVar() != nullptr) { + unchecked_type_ = checker->GuaranteedTypeForUncheckedPropertyAccess(PropVar()); + } if (IsOptional() && Object()->TsType()->IsNullishOrNullLike()) { checker->Relation()->SetNode(this); type = checker->CreateOptionalResultType(type); @@ -205,7 +234,9 @@ void MemberExpression::CheckArrayIndexValue(checker::ETSChecker *checker) const { std::size_t index; - if (auto const &number = property_->AsNumberLiteral()->Number(); number.IsInteger()) { + auto const &number = property_->AsNumberLiteral()->Number(); + + if (number.IsInteger()) { auto const value = number.GetLong(); if (value < 0) { checker->ThrowTypeError("Index value cannot be less than zero.", property_->Start()); @@ -222,6 +253,10 @@ void MemberExpression::CheckArrayIndexValue(checker::ETSChecker *checker) const UNREACHABLE(); } + if (object_->IsArrayExpression() && object_->AsArrayExpression()->Elements().size() <= index) { + checker->ThrowTypeError("Index value cannot be greater than or equal to the array size.", property_->Start()); + } + if (object_->IsIdentifier() && object_->AsIdentifier()->Variable()->Declaration()->Node()->Parent()->IsVariableDeclarator()) { auto const var_decl = @@ -267,7 +302,8 @@ checker::Type *MemberExpression::CheckIndexAccessMethod(checker::ETSChecker *che if (signature == nullptr) { checker->ThrowTypeError("Cannot find index access method with the required signature.", Property()->Start()); } - checker->ValidateSignatureAccessibility(obj_type_, signature, Start(), "Index access method is not visible here."); + checker->ValidateSignatureAccessibility(obj_type_, nullptr, signature, Start(), + "Index access method is not visible here."); ASSERT(signature->Function() != nullptr); @@ -324,18 +360,17 @@ checker::Type *MemberExpression::CheckComputed(checker::ETSChecker *checker, che CheckArrayIndexValue(checker); } - if (property_->IsIdentifier()) { - SetPropVar(property_->AsIdentifier()->Variable()->AsLocalVariable()); - } else if (auto var = property_->Variable(); (var != nullptr) && var->IsLocalVariable()) { - SetPropVar(var->AsLocalVariable()); - } - // NOTE: apply capture conversion on this type if (base_type->IsETSArrayType()) { if (base_type->IsETSTupleType()) { return CheckTupleAccessMethod(checker, base_type); } + if (object_->IsArrayExpression() && property_->IsNumberLiteral()) { + auto const number = property_->AsNumberLiteral()->Number().GetLong(); + return object_->AsArrayExpression()->Elements()[number]->Check(checker); + } + return base_type->AsETSArrayType()->ElementType(); } @@ -380,21 +415,4 @@ MemberExpression *MemberExpression::Clone(ArenaAllocator *const allocator, AstNo throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); } -bool MemberExpression::IsGenericField() const -{ - const auto obj_t = object_->TsType(); - if (!obj_t->IsETSObjectType()) { - return false; - } - auto base_class_t = obj_t->AsETSObjectType()->GetBaseType(); - if (base_class_t == nullptr) { - return false; - } - const auto &prop_name = property_->AsIdentifier()->Name(); - auto base_prop = base_class_t->GetProperty(prop_name, checker::PropertySearchFlags::SEARCH_FIELD); - if (base_prop == nullptr || base_prop->TsType() == nullptr) { - return false; - } - return TsType()->ToAssemblerName().str() != base_prop->TsType()->ToAssemblerName().str(); -} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/expressions/memberExpression.h b/ets2panda/ir/expressions/memberExpression.h index b59de63587ba99edaa5272c26c83e1c4f5ddb947..660b710816db4165c65741f5967f935cd23ac688 100644 --- a/ets2panda/ir/expressions/memberExpression.h +++ b/ets2panda/ir/expressions/memberExpression.h @@ -155,6 +155,11 @@ public: ignore_box_ = true; } + [[nodiscard]] checker::Type *UncheckedType() const noexcept + { + return unchecked_type_; + } + checker::Type *GetTupleConvertedType() const noexcept { return tuple_converted_type_; @@ -173,6 +178,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; bool CompileComputed(compiler::ETSGen *etsg) const; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; @@ -202,7 +208,7 @@ private: checker::Type *type) const; std::pair ResolveObjectMember(checker::ETSChecker *checker) const; - checker::Type *AdjustOptional(checker::ETSChecker *checker, checker::Type *type); + checker::Type *AdjustType(checker::ETSChecker *checker, checker::Type *type); checker::Type *CheckComputed(checker::ETSChecker *checker, checker::Type *base_type); checker::Type *CheckUnionMember(checker::ETSChecker *checker, checker::Type *base_type); @@ -211,13 +217,13 @@ private: checker::Type *CheckTupleAccessMethod(checker::ETSChecker *checker, checker::Type *base_type); void LoadRhs(compiler::PandaGen *pg) const; - bool IsGenericField() const; Expression *object_ = nullptr; Expression *property_ = nullptr; MemberExpressionKind kind_; bool computed_; bool ignore_box_ {false}; + checker::Type *unchecked_type_ {}; varbinder::LocalVariable *prop_var_ {}; checker::ETSObjectType *obj_type_ {}; checker::Type *tuple_converted_type_ {}; diff --git a/ets2panda/ir/expressions/newExpression.cpp b/ets2panda/ir/expressions/newExpression.cpp index 5959c7ffc15b3746e39404c6fb7b8d1fe3cc6ac3..f56cdeb805147dc9c04dea64b87a01bb9811a501 100644 --- a/ets2panda/ir/expressions/newExpression.cpp +++ b/ets2panda/ir/expressions/newExpression.cpp @@ -19,6 +19,8 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "util/helpers.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { NewExpression::NewExpression([[maybe_unused]] Tag const tag, NewExpression const &other, @@ -69,6 +71,11 @@ void NewExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "NewExpression"}, {"callee", callee_}, {"arguments", arguments_}}); } +void NewExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("NewExpression"); +} + void NewExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/newExpression.h b/ets2panda/ir/expressions/newExpression.h index 2fe51c8b6b740044b903a05601c7c705053481d7..a1d974cd3fee0d1a0cf847f28f432304e68b5677 100644 --- a/ets2panda/ir/expressions/newExpression.h +++ b/ets2panda/ir/expressions/newExpression.h @@ -59,6 +59,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/objectExpression.cpp b/ets2panda/ir/expressions/objectExpression.cpp index 18c4b401005f3d55908422ce693442e3cbd92e85..c9fc9d1ea7f8189d7f0a69df2d7d0d68e924eb2a 100644 --- a/ets2panda/ir/expressions/objectExpression.cpp +++ b/ets2panda/ir/expressions/objectExpression.cpp @@ -25,6 +25,7 @@ #include "checker/ets/typeRelationContext.h" #include "checker/ts/destructuringContext.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" #include "ir/base/property.h" #include "ir/base/scriptFunction.h" @@ -229,6 +230,11 @@ void ObjectExpression::Dump(ir::AstDumper *dumper) const {"optional", AstDumper::Optional(optional_)}}); } +void ObjectExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ObjectExpression"); +} + void ObjectExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/objectExpression.h b/ets2panda/ir/expressions/objectExpression.h index 0791b22622f777afa443561e01c4478af72f8e2a..9f8892ac5b6fed6d972b2c5a26bdb425c535543b 100644 --- a/ets2panda/ir/expressions/objectExpression.h +++ b/ets2panda/ir/expressions/objectExpression.h @@ -106,6 +106,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/omittedExpression.cpp b/ets2panda/ir/expressions/omittedExpression.cpp index babc86133ac90420c96ca79cf095ff5640e8cf8a..0ab3d1f33203eefd29e3f44e8ed5442c0d3860c3 100644 --- a/ets2panda/ir/expressions/omittedExpression.cpp +++ b/ets2panda/ir/expressions/omittedExpression.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void OmittedExpression::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void OmittedExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "OmittedExpression"}}); } +void OmittedExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("OmittedExpression"); +} + void OmittedExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/omittedExpression.h b/ets2panda/ir/expressions/omittedExpression.h index 10e0a39e964df17e4f7c4ad393b3b5969c16efc0..4f1f340b78dfb90e6b0069d7a7b7ece85daf6c68 100644 --- a/ets2panda/ir/expressions/omittedExpression.h +++ b/ets2panda/ir/expressions/omittedExpression.h @@ -35,6 +35,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/sequenceExpression.cpp b/ets2panda/ir/expressions/sequenceExpression.cpp index 477f6ef9d82bb5e8141820b6ff3daff93050dfcc..3830204255e2be254704e1b1fe96b17c82716c14 100644 --- a/ets2panda/ir/expressions/sequenceExpression.cpp +++ b/ets2panda/ir/expressions/sequenceExpression.cpp @@ -19,6 +19,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { SequenceExpression::SequenceExpression([[maybe_unused]] Tag const tag, SequenceExpression const &other, @@ -61,6 +63,16 @@ void SequenceExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "SequenceExpression"}, {"expressions", sequence_}}); } +void SequenceExpression::Dump(ir::SrcDumper *dumper) const +{ + for (auto *expr : sequence_) { + expr->Dump(dumper); + if (expr != sequence_.back()) { + dumper->Add(", "); + } + } +} + void SequenceExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/sequenceExpression.h b/ets2panda/ir/expressions/sequenceExpression.h index e34c0f7a1a7d8348ba0ebc036fa78eee2b91d34d..98c53be093291a81d397b3d45e3b209a0c983248 100644 --- a/ets2panda/ir/expressions/sequenceExpression.h +++ b/ets2panda/ir/expressions/sequenceExpression.h @@ -53,6 +53,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/superExpression.cpp b/ets2panda/ir/expressions/superExpression.cpp index 1b5c777ab87336e5ac079d33aea69a6224c41d1b..02e8244316ccb7c986413dbf8c83ae0aaa67a39e 100644 --- a/ets2panda/ir/expressions/superExpression.cpp +++ b/ets2panda/ir/expressions/superExpression.cpp @@ -20,6 +20,8 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "util/helpers.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void SuperExpression::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -30,6 +32,11 @@ void SuperExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "Super"}}); } +void SuperExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("super"); +} + void SuperExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/superExpression.h b/ets2panda/ir/expressions/superExpression.h index a5582fdad568ebd331ccb36a521ca853debb7dd3..29d12a994de0bed32b421f17ac355291e97b3b7e 100644 --- a/ets2panda/ir/expressions/superExpression.h +++ b/ets2panda/ir/expressions/superExpression.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/taggedTemplateExpression.cpp b/ets2panda/ir/expressions/taggedTemplateExpression.cpp index f13baf1d44905a7f1efdb674d4a21e170571bd70..1a4100fabaf7887517cbdc2ff073570e104d6a8a 100644 --- a/ets2panda/ir/expressions/taggedTemplateExpression.cpp +++ b/ets2panda/ir/expressions/taggedTemplateExpression.cpp @@ -22,6 +22,7 @@ #include "compiler/core/regScope.h" #include "checker/TSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/memberExpression.h" #include "ir/expressions/templateLiteral.h" #include "ir/ts/tsTypeParameterInstantiation.h" @@ -55,6 +56,11 @@ void TaggedTemplateExpression::Dump(ir::AstDumper *dumper) const {"typeParameters", AstDumper::Optional(type_params_)}}); } +void TaggedTemplateExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TaggedTemplateExpression"); +} + void TaggedTemplateExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/taggedTemplateExpression.h b/ets2panda/ir/expressions/taggedTemplateExpression.h index 5db6b47742986fa7779c1a72b58143b8a023869d..3d69e696e7cd00c0394d0ca35a92ae23dbbf39cc 100644 --- a/ets2panda/ir/expressions/taggedTemplateExpression.h +++ b/ets2panda/ir/expressions/taggedTemplateExpression.h @@ -57,6 +57,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/templateLiteral.cpp b/ets2panda/ir/expressions/templateLiteral.cpp index e6bbee9f4325dc779ec174b95272b6a39decd192..732c96706b0a4377f457ba7407123afc62fb755e 100644 --- a/ets2panda/ir/expressions/templateLiteral.cpp +++ b/ets2panda/ir/expressions/templateLiteral.cpp @@ -20,6 +20,7 @@ #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/templateElement.h" namespace panda::es2panda::ir { @@ -77,6 +78,11 @@ void TemplateLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TemplateLiteral"}, {"expressions", expressions_}, {"quasis", quasis_}}); } +void TemplateLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TemplateLiteral"); +} + void TemplateLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/templateLiteral.h b/ets2panda/ir/expressions/templateLiteral.h index 7eb13149a9cccc5b8e6a4630a0e8610bcaf26380..046ea69c4f13e5e228751330bf8f09d6872b2433 100644 --- a/ets2panda/ir/expressions/templateLiteral.h +++ b/ets2panda/ir/expressions/templateLiteral.h @@ -54,6 +54,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/expressions/thisExpression.cpp b/ets2panda/ir/expressions/thisExpression.cpp index d8013b9d7a45e0dde9ab02cc9520978cf3032888..9598cea01a9637091955cd6b91a2cb479a84da51 100644 --- a/ets2panda/ir/expressions/thisExpression.cpp +++ b/ets2panda/ir/expressions/thisExpression.cpp @@ -26,6 +26,7 @@ #include "ir/base/methodDefinition.h" #include "ir/statements/blockStatement.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/callExpression.h" namespace panda::es2panda::ir { @@ -37,6 +38,11 @@ void ThisExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ThisExpression"}}); } +void ThisExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("this"); +} + void ThisExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/thisExpression.h b/ets2panda/ir/expressions/thisExpression.h index 061866025a90d746e69e4791472499c2922833b0..51ab0411293e170091a9ea594be62454044870ef 100644 --- a/ets2panda/ir/expressions/thisExpression.h +++ b/ets2panda/ir/expressions/thisExpression.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/expressions/unaryExpression.cpp b/ets2panda/ir/expressions/unaryExpression.cpp index b76d05d63fed73305d93f5097bf801ae19b58776..82601c41bfc70b374a26224a45c501f7d357d86a 100644 --- a/ets2panda/ir/expressions/unaryExpression.cpp +++ b/ets2panda/ir/expressions/unaryExpression.cpp @@ -21,6 +21,7 @@ #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" #include "ir/expressions/literals/bigIntLiteral.h" #include "ir/expressions/literals/numberLiteral.h" @@ -43,6 +44,12 @@ void UnaryExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "UnaryExpression"}, {"operator", operator_}, {"prefix", true}, {"argument", argument_}}); } +void UnaryExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add(TokenToString(operator_)); + argument_->Dump(dumper); +} + void UnaryExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/unaryExpression.h b/ets2panda/ir/expressions/unaryExpression.h index b15dd94fd0b61c2840a459a6433992b0a19bbf09..b53c8c8de85910ea3fce07c58f31e73bf6796cd7 100644 --- a/ets2panda/ir/expressions/unaryExpression.h +++ b/ets2panda/ir/expressions/unaryExpression.h @@ -64,6 +64,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/expressions/updateExpression.cpp b/ets2panda/ir/expressions/updateExpression.cpp index 4f1fffbb898e0676b1bd06a36d184ab06b415047..5453e7543cf9303ea6550a828cedc953f6978fcd 100644 --- a/ets2panda/ir/expressions/updateExpression.cpp +++ b/ets2panda/ir/expressions/updateExpression.cpp @@ -15,6 +15,7 @@ #include "updateExpression.h" +#include "macros.h" #include "varbinder/variable.h" #include "compiler/base/lreference.h" #include "compiler/core/pandagen.h" @@ -23,6 +24,7 @@ #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/unaryExpression.h" #include "ir/ts/tsAsExpression.h" #include "ir/expressions/identifier.h" @@ -44,6 +46,18 @@ void UpdateExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "UpdateExpression"}, {"operator", operator_}, {"prefix", prefix_}, {"argument", argument_}}); } +void UpdateExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(argument_); + if (prefix_) { + dumper->Add(TokenToString(operator_)); + argument_->Dump(dumper); + } else { + argument_->Dump(dumper); + dumper->Add(TokenToString(operator_)); + } +} + void UpdateExpression::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/updateExpression.h b/ets2panda/ir/expressions/updateExpression.h index 75300e97fc9641231560125f982a94d8fb5bfb3d..553d77315e3e638c4443b04d4521d40890446045 100644 --- a/ets2panda/ir/expressions/updateExpression.h +++ b/ets2panda/ir/expressions/updateExpression.h @@ -71,6 +71,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/expressions/yieldExpression.cpp b/ets2panda/ir/expressions/yieldExpression.cpp index 74b627b91403c2c00b821ca3a5770352941e5e17..36ce163da7007580178c8b8e8f8caf8c804930a2 100644 --- a/ets2panda/ir/expressions/yieldExpression.cpp +++ b/ets2panda/ir/expressions/yieldExpression.cpp @@ -20,6 +20,8 @@ #include "compiler/function/generatorFunctionBuilder.h" #include "checker/TSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" + namespace panda::es2panda::ir { void YieldExpression::TransformChildren(const NodeTransformer &cb) { @@ -40,6 +42,11 @@ void YieldExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "YieldExpression"}, {"delegate", delegate_}, {"argument", AstDumper::Nullish(argument_)}}); } +void YieldExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("YieldExpression"); +} + void YieldExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/expressions/yieldExpression.h b/ets2panda/ir/expressions/yieldExpression.h index 25a10a6e00bc2f125a3d1443f6e4d4ed12ba1cbb..1f78687a9ce9381ae64734f0965e479b202e6362 100644 --- a/ets2panda/ir/expressions/yieldExpression.h +++ b/ets2panda/ir/expressions/yieldExpression.h @@ -52,6 +52,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/exportAllDeclaration.cpp b/ets2panda/ir/module/exportAllDeclaration.cpp index c34cb356e49c57204f7920e1862daed02a77ec6d..4ffeaef3a0b47d47681e20c993a4caeb9a14d4ae 100644 --- a/ets2panda/ir/module/exportAllDeclaration.cpp +++ b/ets2panda/ir/module/exportAllDeclaration.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ExportAllDeclaration::TransformChildren(const NodeTransformer &cb) @@ -43,6 +45,11 @@ void ExportAllDeclaration::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ExportAllDeclaration"}, {"source", source_}, {"exported", AstDumper::Nullish(exported_)}}); } +void ExportAllDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ExportAllDeclaration"); +} + void ExportAllDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/exportAllDeclaration.h b/ets2panda/ir/module/exportAllDeclaration.h index a472a1fd5f0d61613a19c6cc30d301a18f70529a..12e40b170f2cceca9a9f4c8e200cf9d610061755 100644 --- a/ets2panda/ir/module/exportAllDeclaration.h +++ b/ets2panda/ir/module/exportAllDeclaration.h @@ -42,6 +42,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/exportDefaultDeclaration.cpp b/ets2panda/ir/module/exportDefaultDeclaration.cpp index 82e090025be581f9ea29b24ed39d83d1c7bc210f..5506246c54364eebe23fd0ff15469322a5cd1361 100644 --- a/ets2panda/ir/module/exportDefaultDeclaration.cpp +++ b/ets2panda/ir/module/exportDefaultDeclaration.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ExportDefaultDeclaration::TransformChildren(const NodeTransformer &cb) @@ -36,6 +38,11 @@ void ExportDefaultDeclaration::Dump(ir::AstDumper *dumper) const {{"type", IsExportEquals() ? "TSExportAssignment" : "ExportDefaultDeclaration"}, {"declaration", decl_}}); } +void ExportDefaultDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ExportDefaultDeclaration"); +} + void ExportDefaultDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/exportDefaultDeclaration.h b/ets2panda/ir/module/exportDefaultDeclaration.h index 532098f023e933728ae9bcba7caa6b3502eb8ae1..b92eff1366307a61036df168dc59cb8119299879 100644 --- a/ets2panda/ir/module/exportDefaultDeclaration.h +++ b/ets2panda/ir/module/exportDefaultDeclaration.h @@ -44,6 +44,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/exportNamedDeclaration.cpp b/ets2panda/ir/module/exportNamedDeclaration.cpp index ff886326fd879b9e8fe06e74fdf6dde74a11d3d6..662a4de79ba031c78336baef11b3bc40a65cdb7e 100644 --- a/ets2panda/ir/module/exportNamedDeclaration.cpp +++ b/ets2panda/ir/module/exportNamedDeclaration.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ExportNamedDeclaration::TransformChildren(const NodeTransformer &cb) @@ -67,6 +69,11 @@ void ExportNamedDeclaration::Dump(ir::AstDumper *dumper) const {"specifiers", specifiers_}}); } +void ExportNamedDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ExportNamedDeclaration"); +} + void ExportNamedDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/exportNamedDeclaration.h b/ets2panda/ir/module/exportNamedDeclaration.h index 78a515225b4257dba198d3f24e2e0a8b0785d46c..4a5a8c7d858e972825dc1272814eb8fe0cae438c 100644 --- a/ets2panda/ir/module/exportNamedDeclaration.h +++ b/ets2panda/ir/module/exportNamedDeclaration.h @@ -78,6 +78,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/exportSpecifier.cpp b/ets2panda/ir/module/exportSpecifier.cpp index 2aa19dee82c77cf7cb630552fde92cc83c183f9c..0f728c080021405bf5f0dcdcaadd2b834cdababe 100644 --- a/ets2panda/ir/module/exportSpecifier.cpp +++ b/ets2panda/ir/module/exportSpecifier.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ExportSpecifier::TransformChildren(const NodeTransformer &cb) @@ -37,6 +39,11 @@ void ExportSpecifier::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ExportSpecifier"}, {"local", local_}, {"exported", exported_}}); } +void ExportSpecifier::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ExportSpecifier"); +} + void ExportSpecifier::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/exportSpecifier.h b/ets2panda/ir/module/exportSpecifier.h index 1770e7f06504b62d760ea2acb045684208d1ef34..ac0cf334dc4c16846ca099a0777cfb259b2f24da 100644 --- a/ets2panda/ir/module/exportSpecifier.h +++ b/ets2panda/ir/module/exportSpecifier.h @@ -41,6 +41,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/importDeclaration.cpp b/ets2panda/ir/module/importDeclaration.cpp index e1208b0d02a56bb3b2eb6ba72753ec14817a2ede..70b22948b2c8c09f165442884cbc7097478df542 100644 --- a/ets2panda/ir/module/importDeclaration.cpp +++ b/ets2panda/ir/module/importDeclaration.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ImportDeclaration::TransformChildren(const NodeTransformer &cb) @@ -43,6 +45,23 @@ void ImportDeclaration::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ImportDeclaration"}, {"source", source_}, {"specifiers", specifiers_}}); } +void ImportDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("import "); + for (auto specifier : specifiers_) { + specifier->Dump(dumper); + if (specifier != specifiers_.back()) { + dumper->Add(", "); + } else { + dumper->Add(" "); + } + } + dumper->Add("from "); + source_->Dump(dumper); + dumper->Add(";"); + dumper->Endl(); +} + void ImportDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/importDeclaration.h b/ets2panda/ir/module/importDeclaration.h index fa7251ef8f63b3c9b74616e2b0253f3af19b9aa4..fc9d2a1e84d2504108edb79ca197cadc3b0289fd 100644 --- a/ets2panda/ir/module/importDeclaration.h +++ b/ets2panda/ir/module/importDeclaration.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/importDefaultSpecifier.cpp b/ets2panda/ir/module/importDefaultSpecifier.cpp index ea4a43b93e199df6aa0178d0fbca81eb82e89459..1999c374ffe14688129f4b28fd98077bb617f1da 100644 --- a/ets2panda/ir/module/importDefaultSpecifier.cpp +++ b/ets2panda/ir/module/importDefaultSpecifier.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ImportDefaultSpecifier::TransformChildren(const NodeTransformer &cb) @@ -35,6 +37,11 @@ void ImportDefaultSpecifier::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ImportDefaultSpecifier"}, {"local", local_}}); } +void ImportDefaultSpecifier::Dump(ir::SrcDumper *dumper) const +{ + local_->Dump(dumper); +} + void ImportDefaultSpecifier::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/importDefaultSpecifier.h b/ets2panda/ir/module/importDefaultSpecifier.h index c698045b76b1df77d85509ac642be1496341b9f3..1df936b8742b6604815795580f13a1af84a151e6 100644 --- a/ets2panda/ir/module/importDefaultSpecifier.h +++ b/ets2panda/ir/module/importDefaultSpecifier.h @@ -35,6 +35,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/importNamespaceSpecifier.cpp b/ets2panda/ir/module/importNamespaceSpecifier.cpp index 44660316c6ed0e0860617c2d976c7e8f2a24316b..406f735a82e3eeaf91cd7b1d7b754326cad8da8a 100644 --- a/ets2panda/ir/module/importNamespaceSpecifier.cpp +++ b/ets2panda/ir/module/importNamespaceSpecifier.cpp @@ -19,6 +19,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ImportNamespaceSpecifier::TransformChildren(const NodeTransformer &cb) @@ -36,6 +38,11 @@ void ImportNamespaceSpecifier::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ImportNamespaceSpecifier"}, {"local", local_}}); } +void ImportNamespaceSpecifier::Dump(ir::SrcDumper *dumper) const +{ + local_->Dump(dumper); +} + void ImportNamespaceSpecifier::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/importNamespaceSpecifier.h b/ets2panda/ir/module/importNamespaceSpecifier.h index e4bbeaccb49224d3cc4c70f5f26ec9238479c801..3b79e7f1bf6b37da684bbb5251a5466460aa6f62 100644 --- a/ets2panda/ir/module/importNamespaceSpecifier.h +++ b/ets2panda/ir/module/importNamespaceSpecifier.h @@ -41,6 +41,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/module/importSpecifier.cpp b/ets2panda/ir/module/importSpecifier.cpp index 984e4137d5861c8b746bb2fc0bbe2a63f02e5559..632e363f45961156816c3ad286878f6746785423 100644 --- a/ets2panda/ir/module/importSpecifier.cpp +++ b/ets2panda/ir/module/importSpecifier.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ImportSpecifier::TransformChildren(const NodeTransformer &cb) @@ -41,6 +43,11 @@ void ImportSpecifier::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ImportSpecifier"}, {"local", ir::AstDumper::Optional(local_)}, {"imported", imported_}}); } +void ImportSpecifier::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ImportSpecifier"); +} + void ImportSpecifier::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/module/importSpecifier.h b/ets2panda/ir/module/importSpecifier.h index 2aad8a29eac19448c42c7efcffb166a939a3307a..4f98dd150f7b475b621295e268221d49a8ca4e5e 100644 --- a/ets2panda/ir/module/importSpecifier.h +++ b/ets2panda/ir/module/importSpecifier.h @@ -51,6 +51,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/opaqueTypeNode.cpp b/ets2panda/ir/opaqueTypeNode.cpp index b09b065cb4cba3e35869a2cb44a6855fa5e8fb3b..ec0bc61948898dba54e9500c1168f1267531178b 100644 --- a/ets2panda/ir/opaqueTypeNode.cpp +++ b/ets2panda/ir/opaqueTypeNode.cpp @@ -18,6 +18,8 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" + namespace panda::es2panda::ir { void OpaqueTypeNode::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void OpaqueTypeNode::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "OpaqueType"}}); } +void OpaqueTypeNode::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("OpaqueTypeNode"); +} + void OpaqueTypeNode::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/opaqueTypeNode.h b/ets2panda/ir/opaqueTypeNode.h index c37708193eac1118d4d8aa34928b50cadd09bca9..3f31940809f5fcb739ac04d7ba221ddb78012c1c 100644 --- a/ets2panda/ir/opaqueTypeNode.h +++ b/ets2panda/ir/opaqueTypeNode.h @@ -39,6 +39,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/srcDump.cpp b/ets2panda/ir/srcDump.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7913711200f63f6b45eaa10d5e416b2acab31a1d --- /dev/null +++ b/ets2panda/ir/srcDump.cpp @@ -0,0 +1,77 @@ +/** + * 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 "srcDump.h" + +#include + +#include +#include + +namespace panda::es2panda::ir { + +SrcDumper::SrcDumper(const ir::AstNode *node) +{ + node->Dump(this); +} + +void SrcDumper::IncrIndent() +{ + indent_.push_back(' '); + indent_.push_back(' '); +} + +void SrcDumper::DecrIndent() +{ + if (indent_.size() >= 2U) { + indent_.pop_back(); + indent_.pop_back(); + } +} + +void SrcDumper::Endl(size_t num) +{ + while (num != 0U) { + ss_ << std::endl; + --num; + } + ss_ << indent_; +} + +void SrcDumper::Add(const std::string &str) +{ + ss_ << str; +} + +void SrcDumper::Add(const int32_t i) +{ + ss_ << i; +} + +void SrcDumper::Add(const int64_t l) +{ + ss_ << l; +} + +void SrcDumper::Add(const float f) +{ + ss_ << f; +} + +void SrcDumper::Add(const double d) +{ + ss_ << d; +} +} // namespace panda::es2panda::ir diff --git a/ets2panda/ir/srcDump.h b/ets2panda/ir/srcDump.h new file mode 100644 index 0000000000000000000000000000000000000000..f80c81942f02e1fe70055eb72e6c5be5d62fe669 --- /dev/null +++ b/ets2panda/ir/srcDump.h @@ -0,0 +1,54 @@ +/** + * 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_SRCDUMP_H +#define ES2PANDA_IR_SRCDUMP_H + +#include +#include +#include +#include + +#include +#include + +namespace panda::es2panda::ir { + +class SrcDumper { +public: + explicit SrcDumper(const ir::AstNode *node); + + void Add(const std::string &str); + void Add(int32_t i); + void Add(int64_t l); + void Add(float f); + void Add(double d); + + std::string Str() const + { + return ss_.str(); + } + + void IncrIndent(); + void DecrIndent(); + void Endl(size_t num = 1); + +private: + std::stringstream ss_; + std::string indent_; +}; +} // namespace panda::es2panda::ir + +#endif // ES2PANDA_IR_SRCDUMP_H diff --git a/ets2panda/ir/statements/assertStatement.cpp b/ets2panda/ir/statements/assertStatement.cpp index 4b09acd2363cb58e067ae6d980f16ba92745b84f..393cdea5ad8ea8835f5dede22caddcd1f1bf8fd7 100644 --- a/ets2panda/ir/statements/assertStatement.cpp +++ b/ets2panda/ir/statements/assertStatement.cpp @@ -22,6 +22,7 @@ #include "checker/ETSchecker.h" #include "checker/TSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" namespace panda::es2panda::ir { @@ -48,6 +49,17 @@ void AssertStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "AssertStatement"}, {"test", test_}, {"second", AstDumper::Nullish(second_)}}); } +void AssertStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(test_); + dumper->Add("assert("); + test_->Dump(dumper); + dumper->Add(")"); + if (parent_->IsStatement()) { + dumper->Add(";"); + } +} + void AssertStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/assertStatement.h b/ets2panda/ir/statements/assertStatement.h index 504911064fcf87b25d8a7bfd115d34e3dbce6d1a..88b468ad9f2da3c0b18e7ce94b5a416566b30692 100644 --- a/ets2panda/ir/statements/assertStatement.h +++ b/ets2panda/ir/statements/assertStatement.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/blockStatement.cpp b/ets2panda/ir/statements/blockStatement.cpp index 4bb8908d3267e607162f9dadd5ee17defdd5a595..76b50d2aded146df37dcf85a46a7d345f6caa7f0 100644 --- a/ets2panda/ir/statements/blockStatement.cpp +++ b/ets2panda/ir/statements/blockStatement.cpp @@ -22,6 +22,7 @@ #include "checker/TSchecker.h" #include "checker/ETSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void BlockStatement::TransformChildren(const NodeTransformer &cb) @@ -43,6 +44,17 @@ void BlockStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", IsProgram() ? "Program" : "BlockStatement"}, {"statements", statements_}}); } +void BlockStatement::Dump(ir::SrcDumper *dumper) const +{ + // NOTE(nsizov): trailing blocks + for (auto statement : statements_) { + statement->Dump(dumper); + if (statement != statements_.back()) { + dumper->Endl(); + } + } +} + void BlockStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/blockStatement.h b/ets2panda/ir/statements/blockStatement.h index 6f75c047d831deff62b4f241e8bc88277f03dc02..a3c1f9529b3d74094df7eadb6b99dc97db771222 100644 --- a/ets2panda/ir/statements/blockStatement.h +++ b/ets2panda/ir/statements/blockStatement.h @@ -77,6 +77,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/breakStatement.cpp b/ets2panda/ir/statements/breakStatement.cpp index 674edc681c0e989196b346678710c8d284341d38..50cff0716433fd069f61f6a95eb2982dffe7f0df 100644 --- a/ets2panda/ir/statements/breakStatement.cpp +++ b/ets2panda/ir/statements/breakStatement.cpp @@ -19,6 +19,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" #include "checker/ETSchecker.h" @@ -42,6 +43,16 @@ void BreakStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "BreakStatement"}, {"label", AstDumper::Nullish(ident_)}}); } +void BreakStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("break"); + if (ident_ != nullptr) { + dumper->Add(" "); + ident_->Dump(dumper); + } + dumper->Add(";"); +} + void BreakStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/breakStatement.h b/ets2panda/ir/statements/breakStatement.h index 67f4b944faaf85148d72ab5a5d980c52a3045325..35ffa3fa4fcc07ac72ff46891e8522feebac26eb 100644 --- a/ets2panda/ir/statements/breakStatement.h +++ b/ets2panda/ir/statements/breakStatement.h @@ -50,6 +50,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/classDeclaration.cpp b/ets2panda/ir/statements/classDeclaration.cpp index 627e9bece0a42037e8d65b33e331a28d9073671d..5e2b38767bf54d2e79c55418e3f7532865a362c7 100644 --- a/ets2panda/ir/statements/classDeclaration.cpp +++ b/ets2panda/ir/statements/classDeclaration.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ClassDeclaration::TransformChildren(const NodeTransformer &cb) @@ -43,6 +45,15 @@ void ClassDeclaration::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ClassDeclaration"}, {"definition", def_}, {"decorators", AstDumper::Optional(decorators_)}}); } +void ClassDeclaration::Dump(ir::SrcDumper *dumper) const +{ + if (def_ != nullptr) { + def_->Dump(dumper); + } + // NOTE(nsizov): support decorators when supported in ArkTS + ASSERT(decorators_.empty()); +} + void ClassDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/classDeclaration.h b/ets2panda/ir/statements/classDeclaration.h index c6e5925b4c25b6ddfc8fd92f2a56665cbf36bda2..acc51c23e1b24e93ca23a150df384e79c15cd304 100644 --- a/ets2panda/ir/statements/classDeclaration.h +++ b/ets2panda/ir/statements/classDeclaration.h @@ -59,6 +59,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; diff --git a/ets2panda/ir/statements/continueStatement.cpp b/ets2panda/ir/statements/continueStatement.cpp index da58f870ae1043602b4e21bcb8460c06f4742262..6b9739ecc3ff7a717506177ee2356399af48f9ef 100644 --- a/ets2panda/ir/statements/continueStatement.cpp +++ b/ets2panda/ir/statements/continueStatement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ContinueStatement::TransformChildren(const NodeTransformer &cb) @@ -39,6 +41,11 @@ void ContinueStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ContinueStatement"}, {"label", AstDumper::Nullish(ident_)}}); } +void ContinueStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("continue;"); +} + void ContinueStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/continueStatement.h b/ets2panda/ir/statements/continueStatement.h index 857b43f7f63cb4aece3c0c9c5cdf14ee8a6c6263..2ccb751024eb57693ae5e87f0f257fa4234f26e6 100644 --- a/ets2panda/ir/statements/continueStatement.h +++ b/ets2panda/ir/statements/continueStatement.h @@ -50,6 +50,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/debuggerStatement.cpp b/ets2panda/ir/statements/debuggerStatement.cpp index b0bb23faadd70a585650ec54cd7d21c5988d5c0f..9ae8e40d5aebb683aae5f3fc03ce67a5365384d1 100644 --- a/ets2panda/ir/statements/debuggerStatement.cpp +++ b/ets2panda/ir/statements/debuggerStatement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void DebuggerStatement::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void DebuggerStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "DebuggerStatement"}}); } +void DebuggerStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("DebuggerStatement"); +} + void DebuggerStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/debuggerStatement.h b/ets2panda/ir/statements/debuggerStatement.h index 70feeb4698aff7f811d7b7998b549fd8588efa8b..de1e2f4024b4f1aa211d95776578d7ae443f04b7 100644 --- a/ets2panda/ir/statements/debuggerStatement.h +++ b/ets2panda/ir/statements/debuggerStatement.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/doWhileStatement.cpp b/ets2panda/ir/statements/doWhileStatement.cpp index 93d2a413e6a8e2efabe04fa0d386c38083230573..769d97bd556382b09bdc0c06fc3f11ac461a51bd 100644 --- a/ets2panda/ir/statements/doWhileStatement.cpp +++ b/ets2panda/ir/statements/doWhileStatement.cpp @@ -14,6 +14,7 @@ */ #include "doWhileStatement.h" +#include #include "varbinder/scope.h" #include "compiler/base/condition.h" @@ -21,6 +22,8 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "checker/TSchecker.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void DoWhileStatement::TransformChildren(const NodeTransformer &cb) @@ -40,6 +43,24 @@ void DoWhileStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "DoWhileStatement"}, {"body", body_}, {"test", test_}}); } +void DoWhileStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("do {"); + if (body_ != nullptr) { + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("} while"); + dumper->Add("("); + if (test_ != nullptr) { + test_->Dump(dumper); + } + dumper->Add(")"); +} + void DoWhileStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/doWhileStatement.h b/ets2panda/ir/statements/doWhileStatement.h index dbae0763e6d5ee38e796b221e4ee8a481df1a265..487dd589c538e7b326670eedce7f532c31bacb51 100644 --- a/ets2panda/ir/statements/doWhileStatement.h +++ b/ets2panda/ir/statements/doWhileStatement.h @@ -62,6 +62,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/emptyStatement.cpp b/ets2panda/ir/statements/emptyStatement.cpp index 4c00f2a89ee6f42aea577b5c9121229994a54b11..a5a9bab6c2c8b9907b2714498507e8d2bd3c2e93 100644 --- a/ets2panda/ir/statements/emptyStatement.cpp +++ b/ets2panda/ir/statements/emptyStatement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void EmptyStatement::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void EmptyStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "EmptyStatement"}}); } +void EmptyStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("EmptyStatement"); +} + void EmptyStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/emptyStatement.h b/ets2panda/ir/statements/emptyStatement.h index a8ac9416a8aaf286c3628a894df06c8848c382fe..f6d86e6cf5743711cf019870985d1d8ccbde67ad 100644 --- a/ets2panda/ir/statements/emptyStatement.h +++ b/ets2panda/ir/statements/emptyStatement.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/expressionStatement.cpp b/ets2panda/ir/statements/expressionStatement.cpp index d4750832be8cdad6688db5c3f34f4f80185336df..2f10412f4e791993388e7703adf14503b654567a 100644 --- a/ets2panda/ir/statements/expressionStatement.cpp +++ b/ets2panda/ir/statements/expressionStatement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ExpressionStatement::TransformChildren(const NodeTransformer &cb) @@ -35,6 +37,18 @@ void ExpressionStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ExpressionStatement"}, {"expression", expression_}}); } +void ExpressionStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(expression_ != nullptr); + expression_->Dump(dumper); + if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsSwitchCaseStatement())) { + dumper->Add(";"); + if (parent_->IsSwitchCaseStatement()) { + dumper->Endl(); + } + } +} + void ExpressionStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/expressionStatement.h b/ets2panda/ir/statements/expressionStatement.h index 57e061a22a7774bb7599eb43ac68b9e66802c697..85da0d28775b77c80cac931f8b3121c439433a57 100644 --- a/ets2panda/ir/statements/expressionStatement.h +++ b/ets2panda/ir/statements/expressionStatement.h @@ -38,6 +38,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/forInStatement.cpp b/ets2panda/ir/statements/forInStatement.cpp index 331be2f2fff94081c1ae8d4b1b8527e784c91c1a..a1344d73628bf47889736234ba358440928163ec 100644 --- a/ets2panda/ir/statements/forInStatement.cpp +++ b/ets2panda/ir/statements/forInStatement.cpp @@ -21,6 +21,8 @@ #include "compiler/core/pandagen.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ForInStatement::TransformChildren(const NodeTransformer &cb) @@ -42,6 +44,11 @@ void ForInStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ForInStatement"}, {"left", left_}, {"right", right_}, {"body", body_}}); } +void ForInStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("ForInStatement"); +} + void ForInStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/forInStatement.h b/ets2panda/ir/statements/forInStatement.h index 13f428e3b9fef1f6f4934b4b27cde1ea720a2824..0702403edb424bb998b8d693b6f2baa7a5559a56 100644 --- a/ets2panda/ir/statements/forInStatement.h +++ b/ets2panda/ir/statements/forInStatement.h @@ -72,6 +72,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/forOfStatement.cpp b/ets2panda/ir/statements/forOfStatement.cpp index 50cb1e9d1c9e63c719ed6236d68ed35d2f2d12e8..7ce6be2d4482e59613274240ce84337b9bc15360 100644 --- a/ets2panda/ir/statements/forOfStatement.cpp +++ b/ets2panda/ir/statements/forOfStatement.cpp @@ -15,6 +15,7 @@ #include "forOfStatement.h" +#include "macros.h" #include "varbinder/scope.h" #include "compiler/base/iterators.h" #include "compiler/base/lreference.h" @@ -22,6 +23,8 @@ #include "checker/TSchecker.h" #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ForOfStatement::TransformChildren(const NodeTransformer &cb) @@ -44,6 +47,29 @@ void ForOfStatement::Dump(ir::AstDumper *dumper) const {{"type", "ForOfStatement"}, {"await", is_await_}, {"left", left_}, {"right", right_}, {"body", body_}}); } +void ForOfStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(left_ != nullptr); + ASSERT(right_ != nullptr); + dumper->Add("for "); + if (is_await_) { + dumper->Add("await "); + } + dumper->Add("("); + left_->Dump(dumper); + dumper->Add(" of "); + right_->Dump(dumper); + dumper->Add(") {"); + if (body_ != nullptr) { + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("}"); +} + void ForOfStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/forOfStatement.h b/ets2panda/ir/statements/forOfStatement.h index 9a2e9ba8a0c55005420b61a007929860e6fe5bb0..2d5fbbb250df71e6db1a6864f203cef987c4c368 100644 --- a/ets2panda/ir/statements/forOfStatement.h +++ b/ets2panda/ir/statements/forOfStatement.h @@ -77,6 +77,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/forUpdateStatement.cpp b/ets2panda/ir/statements/forUpdateStatement.cpp index 09ccc206027021f9ecad28e5c682212f9ad238f8..711632f31a19a1fee24212aea00f17c70f24811c 100644 --- a/ets2panda/ir/statements/forUpdateStatement.cpp +++ b/ets2panda/ir/statements/forUpdateStatement.cpp @@ -14,6 +14,7 @@ */ #include "forUpdateStatement.h" +#include #include "varbinder/scope.h" #include "compiler/base/condition.h" @@ -23,6 +24,8 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/dynamicContext.h" #include "checker/TSchecker.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ForUpdateStatement::TransformChildren(const NodeTransformer &cb) @@ -66,6 +69,33 @@ void ForUpdateStatement::Dump(ir::AstDumper *dumper) const {"body", body_}}); } +void ForUpdateStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("for "); + dumper->Add("("); + if (init_ != nullptr) { + init_->Dump(dumper); + } + dumper->Add(";"); + if (test_ != nullptr) { + test_->Dump(dumper); + } + dumper->Add(";"); + if (update_ != nullptr) { + update_->Dump(dumper); + } + dumper->Add(") "); + dumper->Add("{"); + if (body_ != nullptr) { + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("}"); +} + void ForUpdateStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/forUpdateStatement.h b/ets2panda/ir/statements/forUpdateStatement.h index 03e828ba1bda0d6882bf50181741f294f76981c9..fc4dfcc847db0adceda626e24f35ae4c5c3b707f 100644 --- a/ets2panda/ir/statements/forUpdateStatement.h +++ b/ets2panda/ir/statements/forUpdateStatement.h @@ -82,6 +82,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/functionDeclaration.cpp b/ets2panda/ir/statements/functionDeclaration.cpp index a5c9e3a6148b417654cc2360ad5ad0b80fc3934d..b3e2292424c8d59edf17cdc749114f117cf71876 100644 --- a/ets2panda/ir/statements/functionDeclaration.cpp +++ b/ets2panda/ir/statements/functionDeclaration.cpp @@ -19,6 +19,8 @@ #include "varbinder/scope.h" #include "compiler/core/ETSGen.h" #include "checker/TSchecker.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" #include "compiler/core/pandagen.h" namespace panda::es2panda::ir { @@ -47,6 +49,11 @@ void FunctionDeclaration::Dump(ir::AstDumper *dumper) const {"function", func_}}); } +void FunctionDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("FunctionDeclaration"); +} + void FunctionDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/functionDeclaration.h b/ets2panda/ir/statements/functionDeclaration.h index 449be13118f72f3a7629e4192bb18f5cc7df68f5..5437f2c3a1c50837439a5194bb6b02d518c296f0 100644 --- a/ets2panda/ir/statements/functionDeclaration.h +++ b/ets2panda/ir/statements/functionDeclaration.h @@ -59,6 +59,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/ifStatement.cpp b/ets2panda/ir/statements/ifStatement.cpp index 05b30ac4114a462e1148d45887c8a54fea6758d4..44e8b575713375281ebe90c4cb4e120712762ae5 100644 --- a/ets2panda/ir/statements/ifStatement.cpp +++ b/ets2panda/ir/statements/ifStatement.cpp @@ -14,10 +14,13 @@ */ #include "ifStatement.h" +#include #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void IfStatement::TransformChildren(const NodeTransformer &cb) @@ -48,6 +51,36 @@ void IfStatement::Dump(ir::AstDumper *dumper) const {"alternate", AstDumper::Nullish(alternate_)}}); } +void IfStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(test_); + dumper->Add("if ("); + test_->Dump(dumper); + dumper->Add(") {"); + if (consequent_ != nullptr) { + dumper->IncrIndent(); + dumper->Endl(); + dumper->DecrIndent(); + consequent_->Dump(dumper); + dumper->Endl(); + } + dumper->Add("}"); + if (alternate_ != nullptr) { + dumper->Add(" else "); + if (alternate_->IsBlockStatement()) { + dumper->Add("{"); + dumper->IncrIndent(); + dumper->Endl(); + dumper->DecrIndent(); + alternate_->Dump(dumper); + dumper->Endl(); + dumper->Add("}"); + } else { + alternate_->Dump(dumper); + } + } +} + void IfStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/ifStatement.h b/ets2panda/ir/statements/ifStatement.h index 70f0f2c9d2403c8dca597cfa58f183dbd60de076..0671a827f6c10cf7ef35f46ad45ad686d3f63e1d 100644 --- a/ets2panda/ir/statements/ifStatement.h +++ b/ets2panda/ir/statements/ifStatement.h @@ -70,6 +70,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/labelledStatement.cpp b/ets2panda/ir/statements/labelledStatement.cpp index c43d30ebf809b61b750156899434574fa474ec94..f74131e620e67faf1fbf6c842489631fe2a74a36 100644 --- a/ets2panda/ir/statements/labelledStatement.cpp +++ b/ets2panda/ir/statements/labelledStatement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void LabelledStatement::TransformChildren(const NodeTransformer &cb) @@ -37,6 +39,15 @@ void LabelledStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "LabelledStatement"}, {"label", ident_}, {"body", body_}}); } +void LabelledStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(ident_ != nullptr); + ident_->Dump(dumper); + dumper->Add(":"); + dumper->Endl(); + body_->Dump(dumper); +} + const ir::AstNode *LabelledStatement::GetReferencedStatement() const { const auto *iter = body_; diff --git a/ets2panda/ir/statements/labelledStatement.h b/ets2panda/ir/statements/labelledStatement.h index 28046f2fa365d6590ba0bd70cd75873688fafbf2..bb74240f40eab3d5da2d2bdbbe7c05d05ff3c0bc 100644 --- a/ets2panda/ir/statements/labelledStatement.h +++ b/ets2panda/ir/statements/labelledStatement.h @@ -58,6 +58,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/returnStatement.cpp b/ets2panda/ir/statements/returnStatement.cpp index 4c1cf00120de310649fbb85f7abe675ea2cccaa6..4139ecafde122cae8becbd56fa96d692f042ea53 100644 --- a/ets2panda/ir/statements/returnStatement.cpp +++ b/ets2panda/ir/statements/returnStatement.cpp @@ -19,6 +19,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void ReturnStatement::TransformChildren(const NodeTransformer &cb) @@ -40,6 +41,16 @@ void ReturnStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ReturnStatement"}, {"argument", AstDumper::Nullish(argument_)}}); } +void ReturnStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("return"); + if (argument_ != nullptr) { + dumper->Add(" "); + argument_->Dump(dumper); + } + dumper->Add(";"); +} + void ReturnStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -75,7 +86,7 @@ void ReturnStatement::SetReturnType(checker::ETSChecker *checker, checker::Type if (argument_type == nullptr) { checker->ThrowTypeError("Invalid return statement expression", argument_->Start()); } - argument_->AddBoxingUnboxingFlag(checker->GetBoxingFlag(argument_type)); + argument_->AddBoxingUnboxingFlags(checker->GetBoxingFlag(argument_type)); relation->SetNode(nullptr); } diff --git a/ets2panda/ir/statements/returnStatement.h b/ets2panda/ir/statements/returnStatement.h index 3b9db1fd90276a54c3fab141b33c082b4ac400fb..c09f0531eb639dbb71be031b08360bd5068e4dac 100644 --- a/ets2panda/ir/statements/returnStatement.h +++ b/ets2panda/ir/statements/returnStatement.h @@ -67,6 +67,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/switchCaseStatement.cpp b/ets2panda/ir/statements/switchCaseStatement.cpp index 3830c777c6445b27e21d45b8b7069cbdda0e9a73..b8fe7a025fd8f56520802d346527b06d496e4f29 100644 --- a/ets2panda/ir/statements/switchCaseStatement.cpp +++ b/ets2panda/ir/statements/switchCaseStatement.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void SwitchCaseStatement::TransformChildren(const NodeTransformer &cb) @@ -47,6 +49,25 @@ void SwitchCaseStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "SwitchCase"}, {"test", AstDumper::Nullish(test_)}, {"consequent", consequent_}}); } +void SwitchCaseStatement::Dump(ir::SrcDumper *dumper) const +{ + if (test_ != nullptr) { + dumper->Add("case "); + test_->Dump(dumper); + dumper->Add(":"); + } else { + dumper->Add("default:"); + } + if (!consequent_.empty()) { + dumper->IncrIndent(); + dumper->Endl(); + for (auto cs : consequent_) { + cs->Dump(dumper); + } + dumper->DecrIndent(); + } +} + void SwitchCaseStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/switchCaseStatement.h b/ets2panda/ir/statements/switchCaseStatement.h index 43022283de3aaec74ed7d30fb99bc0d81f6a5371..3997c92d5195597411da19ef93a958283a5a2937 100644 --- a/ets2panda/ir/statements/switchCaseStatement.h +++ b/ets2panda/ir/statements/switchCaseStatement.h @@ -55,6 +55,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/switchStatement.cpp b/ets2panda/ir/statements/switchStatement.cpp index c7d908bd2d4ec163e47ee390b1a21ce49cb6b864..5c56c170d8b253a033e63446b13a8776638f9910 100644 --- a/ets2panda/ir/statements/switchStatement.cpp +++ b/ets2panda/ir/statements/switchStatement.cpp @@ -21,6 +21,8 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "checker/TSchecker.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void SwitchStatement::TransformChildren(const NodeTransformer &cb) @@ -46,6 +48,26 @@ void SwitchStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "SwitchStatement"}, {"discriminant", discriminant_}, {"cases", cases_}}); } +void SwitchStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(discriminant_); + dumper->Add("switch ("); + discriminant_->Dump(dumper); + dumper->Add(") {"); + if (!cases_.empty()) { + dumper->IncrIndent(); + dumper->Endl(); + for (auto cs : cases_) { + cs->Dump(dumper); + if (cs == cases_.back()) { + dumper->DecrIndent(); + } + dumper->Endl(); + } + } + dumper->Add("}"); +} + void SwitchStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/switchStatement.h b/ets2panda/ir/statements/switchStatement.h index 8a4b86b2b4c242f1062b0dad699e8e6dcbdea866..6b14aab08feb6550b9baa884956f144cebbd8b36 100644 --- a/ets2panda/ir/statements/switchStatement.h +++ b/ets2panda/ir/statements/switchStatement.h @@ -79,6 +79,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/statements/throwStatement.cpp b/ets2panda/ir/statements/throwStatement.cpp index 28a3e2f54b1bbb6ce116c9c6bac617044b8ad53d..5ac70ad69f259546e332888092e491138e090770 100644 --- a/ets2panda/ir/statements/throwStatement.cpp +++ b/ets2panda/ir/statements/throwStatement.cpp @@ -19,6 +19,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" namespace panda::es2panda::ir { @@ -37,6 +38,14 @@ void ThrowStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "ThrowStatement"}, {"argument", argument_}}); } +void ThrowStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(Argument() != nullptr); + dumper->Add("throw "); + Argument()->Dump(dumper); + dumper->Add(";"); +} + void ThrowStatement::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/throwStatement.h b/ets2panda/ir/statements/throwStatement.h index 2a224ce2de5bbed253b871ab7e8def1cae37fed5..40e2b07704c8c8e1070bc89160e6114a580a3305 100644 --- a/ets2panda/ir/statements/throwStatement.h +++ b/ets2panda/ir/statements/throwStatement.h @@ -39,6 +39,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/tryStatement.cpp b/ets2panda/ir/statements/tryStatement.cpp index f325b681e959c93e98533dbeb08ac146cd2755cf..5c632f3ed5adda90eb981a690ab1aab78f491a91 100644 --- a/ets2panda/ir/statements/tryStatement.cpp +++ b/ets2panda/ir/statements/tryStatement.cpp @@ -21,6 +21,7 @@ #include "compiler/core/dynamicContext.h" #include "compiler/base/catchTable.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/catchClause.h" #include "ir/statements/blockStatement.h" @@ -59,6 +60,31 @@ void TryStatement::Dump(ir::AstDumper *dumper) const {"finalizer", AstDumper::Nullish(finalizer_)}}); } +void TryStatement::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(block_ != nullptr); + dumper->Add("try {"); + dumper->IncrIndent(); + dumper->Endl(); + block_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + dumper->Add("}"); + for (auto clause : catch_clauses_) { + dumper->Add(" catch "); + clause->Dump(dumper); + } + if (finalizer_ != nullptr) { + dumper->Add(" finally {"); + dumper->IncrIndent(); + dumper->Endl(); + finalizer_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + dumper->Add("}"); + } +} + bool TryStatement::HasDefaultCatchClause() const { return (!catch_clauses_.empty() && catch_clauses_.back()->IsDefaultCatchClause()); diff --git a/ets2panda/ir/statements/tryStatement.h b/ets2panda/ir/statements/tryStatement.h index 9661f2748a9e4d1ee33a9dc828d928b99a6be313..b60333579f7e1c6e144537a15d95da0674c44bce 100644 --- a/ets2panda/ir/statements/tryStatement.h +++ b/ets2panda/ir/statements/tryStatement.h @@ -87,6 +87,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/variableDeclaration.cpp b/ets2panda/ir/statements/variableDeclaration.cpp index 205265a3dafd8785ea6f8598622548db1514b8cc..91c56499328bdf31abef0edae504a6bfdf5876c2 100644 --- a/ets2panda/ir/statements/variableDeclaration.cpp +++ b/ets2panda/ir/statements/variableDeclaration.cpp @@ -15,6 +15,7 @@ #include "variableDeclaration.h" +#include "macros.h" #include "varbinder/scope.h" #include "varbinder/variable.h" #include "checker/TSchecker.h" @@ -22,6 +23,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/decorator.h" #include "ir/expressions/arrayExpression.h" #include "ir/expressions/identifier.h" @@ -80,6 +82,34 @@ void VariableDeclaration::Dump(ir::AstDumper *dumper) const {"declare", AstDumper::Optional(declare_)}}); } +void VariableDeclaration::Dump(ir::SrcDumper *dumper) const +{ + switch (kind_) { + case VariableDeclarationKind::CONST: + dumper->Add("const "); + break; + case VariableDeclarationKind::LET: + dumper->Add("let "); + break; + case VariableDeclarationKind::VAR: + dumper->Add("var "); + break; + default: + UNREACHABLE(); + } + + for (auto declarator : declarators_) { + declarator->Dump(dumper); + if (declarator != declarators_.back()) { + dumper->Add(", "); + } + } + + if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) { + dumper->Add(";"); + } +} + void VariableDeclaration::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/variableDeclaration.h b/ets2panda/ir/statements/variableDeclaration.h index 9b9fc3fcd9b53c35304313b43e114d66ca65f070..88e45ffe6433e3c0c27facd28cb0948794cde5ad 100644 --- a/ets2panda/ir/statements/variableDeclaration.h +++ b/ets2panda/ir/statements/variableDeclaration.h @@ -73,6 +73,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/variableDeclarator.cpp b/ets2panda/ir/statements/variableDeclarator.cpp index 3089bfd1cb17ff13b661abd5392bbed2c91555b8..7dc9140ded2c51bf09f95a80da56fe31dc90af3b 100644 --- a/ets2panda/ir/statements/variableDeclarator.cpp +++ b/ets2panda/ir/statements/variableDeclarator.cpp @@ -20,6 +20,7 @@ #include "compiler/core/pandagen.h" #include "compiler/core/ETSGen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/astNode.h" #include "ir/typeNode.h" #include "ir/expression.h" @@ -56,6 +57,24 @@ void VariableDeclarator::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullish(init_)}}); } +void VariableDeclarator::Dump(ir::SrcDumper *dumper) const +{ + if (id_ != nullptr) { + id_->Dump(dumper); + if (id_->IsAnnotatedExpression()) { + auto *type = id_->AsAnnotatedExpression()->TypeAnnotation(); + if (type != nullptr) { + dumper->Add(": "); + type->Dump(dumper); + } + } + } + if (init_ != nullptr) { + dumper->Add(" = "); + init_->Dump(dumper); + } +} + void VariableDeclarator::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/variableDeclarator.h b/ets2panda/ir/statements/variableDeclarator.h index 03cc256e9d7159a8e9090470916843647e82e267..aad932d74b896a8180d7983414636e2ca10a43ca 100644 --- a/ets2panda/ir/statements/variableDeclarator.h +++ b/ets2panda/ir/statements/variableDeclarator.h @@ -74,6 +74,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/statements/whileStatement.cpp b/ets2panda/ir/statements/whileStatement.cpp index 5624fd56d4d6aae09ac5f7bc615a6706952b3fda..5ab1715c5bda30ec806c31d865849a2149b2710f 100644 --- a/ets2panda/ir/statements/whileStatement.cpp +++ b/ets2panda/ir/statements/whileStatement.cpp @@ -23,6 +23,7 @@ #include "compiler/core/regScope.h" #include "checker/TSchecker.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" namespace panda::es2panda::ir { @@ -43,6 +44,23 @@ void WhileStatement::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "WhileStatement"}, {"test", test_}, {"body", body_}}); } +void WhileStatement::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("while ("); + if (test_ != nullptr) { + test_->Dump(dumper); + } + dumper->Add(") {"); + if (body_ != nullptr) { + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("}"); +} + void WhileStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/statements/whileStatement.h b/ets2panda/ir/statements/whileStatement.h index c4936cced5e3cd32307bcbfe1342b02a633f9cf4..85bbfc03968dcdd0151724693473c964dc3e635c 100644 --- a/ets2panda/ir/statements/whileStatement.h +++ b/ets2panda/ir/statements/whileStatement.h @@ -62,6 +62,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/ts/tsAnyKeyword.cpp b/ets2panda/ir/ts/tsAnyKeyword.cpp index c82f0274154bba1664f4055ba3082a96504a8266..10a4645c5faacfe67bb10856727d6c44aaecce39 100644 --- a/ets2panda/ir/ts/tsAnyKeyword.cpp +++ b/ets2panda/ir/ts/tsAnyKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSAnyKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSAnyKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSAnyKeyword"}}); } +void TSAnyKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSAnyKeyword"); +} + void TSAnyKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsAnyKeyword.h b/ets2panda/ir/ts/tsAnyKeyword.h index 8fd327a711f0b1e3d6006614b12f9ee5a0a41a40..7eacc23c22b8caab206c83b6b761bb1fe479b755 100644 --- a/ets2panda/ir/ts/tsAnyKeyword.h +++ b/ets2panda/ir/ts/tsAnyKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsArrayType.cpp b/ets2panda/ir/ts/tsArrayType.cpp index 7233ca4ab5629f7689c2f7df9572fd7216b72ab5..07b11ca294ae81384c2fd480cfb11b02977d5ca7 100644 --- a/ets2panda/ir/ts/tsArrayType.cpp +++ b/ets2panda/ir/ts/tsArrayType.cpp @@ -18,8 +18,10 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" #include "checker/ETSchecker.h" +#include "macros.h" namespace panda::es2panda::ir { void TSArrayType::TransformChildren(const NodeTransformer &cb) @@ -37,6 +39,13 @@ void TSArrayType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSArrayType"}, {"elementType", element_type_}}); } +void TSArrayType::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(element_type_); + element_type_->Dump(dumper); + dumper->Add("[]"); +} + void TSArrayType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -69,4 +78,24 @@ checker::Type *TSArrayType::GetType(checker::ETSChecker *checker) return checker->CreateETSArrayType(element_type); } +// NOLINTNEXTLINE(google-default-arguments) +TSArrayType *TSArrayType::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const element_type_clone = element_type_ != nullptr ? element_type_->Clone(allocator) : nullptr; + + if (auto *const clone = allocator->New(element_type_clone); clone != nullptr) { + if (element_type_clone != nullptr) { + element_type_clone->SetParent(clone); + } + + if (parent != nullptr) { + clone->SetParent(parent); + } + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} + } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ts/tsArrayType.h b/ets2panda/ir/ts/tsArrayType.h index 96650e8492797fa8e3efda2b1f47888dab522275..560bb5d35b218f50a0e839ad20be3a981f14c7c1 100644 --- a/ets2panda/ir/ts/tsArrayType.h +++ b/ets2panda/ir/ts/tsArrayType.h @@ -40,6 +40,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; @@ -52,6 +53,9 @@ public: v->Accept(this); } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] TSArrayType *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + private: TypeNode *element_type_; }; diff --git a/ets2panda/ir/ts/tsAsExpression.cpp b/ets2panda/ir/ts/tsAsExpression.cpp index faeaf45c3cb25c776ed31810b459c97f56d78086..051d18b7a501ad7c7ffb506028571cc785f713ef 100644 --- a/ets2panda/ir/ts/tsAsExpression.cpp +++ b/ets2panda/ir/ts/tsAsExpression.cpp @@ -58,6 +58,11 @@ void TSAsExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSAsExpression"}, {"expression", expression_}, {"typeAnnotation", TypeAnnotation()}}); } +void TSAsExpression::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSAsExpression"); +} + void TSAsExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsAsExpression.h b/ets2panda/ir/ts/tsAsExpression.h index 0a75f58a3e6da525687b0abc9be8bdbfcb537894..2f48ec8e7f77eafd4b23aeb4c51514cb214377e6 100644 --- a/ets2panda/ir/ts/tsAsExpression.h +++ b/ets2panda/ir/ts/tsAsExpression.h @@ -17,6 +17,7 @@ #define ES2PANDA_IR_TS_AS_EXPRESSION_H #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" namespace panda::es2panda::checker { class ETSAnalyzer; @@ -53,6 +54,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/ts/tsBigintKeyword.cpp b/ets2panda/ir/ts/tsBigintKeyword.cpp index baa374d6690499b117dd1e3f6a60a62c8c8d1d0d..e100389d0e6c71508e230ca191813496dcfd94f5 100644 --- a/ets2panda/ir/ts/tsBigintKeyword.cpp +++ b/ets2panda/ir/ts/tsBigintKeyword.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { @@ -30,6 +31,11 @@ void TSBigintKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSBigIntKeyword"}}); } +void TSBigintKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSBigintKeyword"); +} + void TSBigintKeyword::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsBigintKeyword.h b/ets2panda/ir/ts/tsBigintKeyword.h index 1baa67e14a33e7e5e680f195e8dcc4daa42aaa5e..8db316915b7c816a9aa13ada013c205ca52eb562 100644 --- a/ets2panda/ir/ts/tsBigintKeyword.h +++ b/ets2panda/ir/ts/tsBigintKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsBooleanKeyword.cpp b/ets2panda/ir/ts/tsBooleanKeyword.cpp index 7cc031e8a91543cfcef2b0a1a680ad182a2c90bc..7ef07bc9f1fa81c3cef2354c9ad75132caad8180 100644 --- a/ets2panda/ir/ts/tsBooleanKeyword.cpp +++ b/ets2panda/ir/ts/tsBooleanKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSBooleanKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSBooleanKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSBooleanKeyword"}}); } +void TSBooleanKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSBooleanKeyword"); +} + void TSBooleanKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsBooleanKeyword.h b/ets2panda/ir/ts/tsBooleanKeyword.h index 89f96ce9de87ad65a76912164e2eab5e126ffaae..e76373d8cfa1e81d0a8b814584664d560475444b 100644 --- a/ets2panda/ir/ts/tsBooleanKeyword.h +++ b/ets2panda/ir/ts/tsBooleanKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsClassImplements.cpp b/ets2panda/ir/ts/tsClassImplements.cpp index c3183cd25093eda04580e08ec958ed837f69c9dc..17d0cca22f34afced004c402c4534f892bf98ce5 100644 --- a/ets2panda/ir/ts/tsClassImplements.cpp +++ b/ets2panda/ir/ts/tsClassImplements.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ts/tsTypeParameter.h" #include "ir/ts/tsTypeParameterInstantiation.h" @@ -44,6 +45,12 @@ void TSClassImplements::Dump(ir::AstDumper *dumper) const {"typeParameters", AstDumper::Optional(type_parameters_)}}); } +void TSClassImplements::Dump(ir::SrcDumper *dumper) const +{ + expression_->Dump(dumper); + ASSERT(type_parameters_ == nullptr); +} + void TSClassImplements::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsClassImplements.h b/ets2panda/ir/ts/tsClassImplements.h index 49be88236acf1013f84fae72653a71be6d5377b5..32e9db6a65f328b1073dad19a370aac53576ad60 100644 --- a/ets2panda/ir/ts/tsClassImplements.h +++ b/ets2panda/ir/ts/tsClassImplements.h @@ -51,6 +51,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsConditionalType.cpp b/ets2panda/ir/ts/tsConditionalType.cpp index dc194f4b9cd1c0cf6687ed3a1fbd2980a9748423..e77f35e80a10c45eda051727291d7f306895a20b 100644 --- a/ets2panda/ir/ts/tsConditionalType.cpp +++ b/ets2panda/ir/ts/tsConditionalType.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSConditionalType::TransformChildren(const NodeTransformer &cb) @@ -46,6 +47,11 @@ void TSConditionalType::Dump(ir::AstDumper *dumper) const {"falseType", false_type_}}); } +void TSConditionalType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSConditionalType"); +} + void TSConditionalType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsConditionalType.h b/ets2panda/ir/ts/tsConditionalType.h index c2c4d39a0c8f1d620027f58d6a7657c628813115..b309ba4ef2a367e953efa2d80e1bae95dc811dcf 100644 --- a/ets2panda/ir/ts/tsConditionalType.h +++ b/ets2panda/ir/ts/tsConditionalType.h @@ -54,6 +54,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsConstructorType.cpp b/ets2panda/ir/ts/tsConstructorType.cpp index 70c08d67f79558f8424fa428b61ac235fc1764ab..585639f82b1d845e857d33159a4a6b60051273fe 100644 --- a/ets2panda/ir/ts/tsConstructorType.cpp +++ b/ets2panda/ir/ts/tsConstructorType.cpp @@ -21,6 +21,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ts/tsTypeParameter.h" #include "ir/ts/tsTypeParameterDeclaration.h" @@ -44,6 +45,11 @@ void TSConstructorType::Dump(ir::AstDumper *dumper) const {"abstract", AstDumper::Optional(abstract_)}}); } +void TSConstructorType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSConstructorType"); +} + void TSConstructorType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsConstructorType.h b/ets2panda/ir/ts/tsConstructorType.h index 07a1564d563f73d98225fc12c4ae49df9a54e9ba..f126b33bd8fcda8113a833275a6fcd745b64573e 100644 --- a/ets2panda/ir/ts/tsConstructorType.h +++ b/ets2panda/ir/ts/tsConstructorType.h @@ -83,6 +83,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsEnumDeclaration.cpp b/ets2panda/ir/ts/tsEnumDeclaration.cpp index b0f73788e5517e4142ff969721f057e92825a6e0..426c5e9b1331540164e42c734498afdfda947f10 100644 --- a/ets2panda/ir/ts/tsEnumDeclaration.cpp +++ b/ets2panda/ir/ts/tsEnumDeclaration.cpp @@ -20,6 +20,8 @@ #include "compiler/core/pandagen.h" #include "varbinder/scope.h" #include "util/helpers.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSEnumDeclaration::TransformChildren(const NodeTransformer &cb) @@ -57,6 +59,30 @@ void TSEnumDeclaration::Dump(ir::AstDumper *dumper) const {"const", is_const_}}); } +void TSEnumDeclaration::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(is_const_ == false); + ASSERT(key_ != nullptr); + dumper->Add("enum "); + key_->Dump(dumper); + dumper->Add(" {"); + if (!members_.empty()) { + dumper->IncrIndent(); + dumper->Endl(); + for (auto member : members_) { + member->Dump(dumper); + if (member != members_.back()) { + dumper->Add(","); + dumper->Endl(); + } + } + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("}"); + dumper->Endl(); +} + // NOTE (csabahurton): this method has not been moved to TSAnalyizer.cpp, because it is not used. varbinder::EnumMemberResult EvaluateMemberExpression(checker::TSChecker *checker, [[maybe_unused]] varbinder::EnumVariable *enum_var, diff --git a/ets2panda/ir/ts/tsEnumDeclaration.h b/ets2panda/ir/ts/tsEnumDeclaration.h index 94fa10e7e3dba7de3c2b16f14037be6e5709cdb4..a0d0c168b6afb63b6b6979801f32fbb9a6aa9d5a 100644 --- a/ets2panda/ir/ts/tsEnumDeclaration.h +++ b/ets2panda/ir/ts/tsEnumDeclaration.h @@ -113,6 +113,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsEnumMember.cpp b/ets2panda/ir/ts/tsEnumMember.cpp index adb6873dd8b5ffb09e1c3451e10afe28b93c95ba..c08cafd9711fecbfe7021785e5b40f9cc6088040 100644 --- a/ets2panda/ir/ts/tsEnumMember.cpp +++ b/ets2panda/ir/ts/tsEnumMember.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSEnumMember::TransformChildren(const NodeTransformer &cb) @@ -43,6 +45,16 @@ void TSEnumMember::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSEnumMember"}, {"id", key_}, {"initializer", AstDumper::Optional(init_)}}); } +void TSEnumMember::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(key_ != nullptr); + key_->Dump(dumper); + if (init_ != nullptr) { + dumper->Add(" = "); + init_->Dump(dumper); + } +} + util::StringView TSEnumMember::Name() const { ASSERT(key_->IsIdentifier()); diff --git a/ets2panda/ir/ts/tsEnumMember.h b/ets2panda/ir/ts/tsEnumMember.h index 2d73dcabc769ddfebce58c3e66fb3ba2e5316f1e..c4bfbf3e7103f83e4349df40e0ec62c7ebc263b5 100644 --- a/ets2panda/ir/ts/tsEnumMember.h +++ b/ets2panda/ir/ts/tsEnumMember.h @@ -48,6 +48,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsExternalModuleReference.cpp b/ets2panda/ir/ts/tsExternalModuleReference.cpp index 601c235a021631515e75a58bcc15a6f566762db8..dc66c7fddc9e5a07284f768676c1a1e664ead117 100644 --- a/ets2panda/ir/ts/tsExternalModuleReference.cpp +++ b/ets2panda/ir/ts/tsExternalModuleReference.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSExternalModuleReference::TransformChildren(const NodeTransformer &cb) @@ -35,6 +37,11 @@ void TSExternalModuleReference::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSExternalModuleReference"}, {"expression", expr_}}); } +void TSExternalModuleReference::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSExternalModuleReference"); +} + void TSExternalModuleReference::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsExternalModuleReference.h b/ets2panda/ir/ts/tsExternalModuleReference.h index 51234fdb8e5afa89f03bcc97631217f29630d8e2..3bc8f845370368d1aaf8c91d65841a6b57f172a8 100644 --- a/ets2panda/ir/ts/tsExternalModuleReference.h +++ b/ets2panda/ir/ts/tsExternalModuleReference.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsFunctionType.cpp b/ets2panda/ir/ts/tsFunctionType.cpp index bcd5f1b1a80b1748a4273881e9201706ba6de3f5..03e1601743133f9ad1204409d61380b8e997b03d 100644 --- a/ets2panda/ir/ts/tsFunctionType.cpp +++ b/ets2panda/ir/ts/tsFunctionType.cpp @@ -22,6 +22,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/spreadElement.h" #include "ir/expressions/identifier.h" #include "ir/ts/tsTypeParameter.h" @@ -47,6 +48,11 @@ void TSFunctionType::Dump(ir::AstDumper *dumper) const {"isNullable", AstDumper::Optional(nullable_)}}); } +void TSFunctionType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSFunctionType"); +} + void TSFunctionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsFunctionType.h b/ets2panda/ir/ts/tsFunctionType.h index 596809351a395fb8c5a60002ee2fbba93ac493dc..a9b6401f5fbdbaf1f5d1df09a9cfdc05b8ecf8c4 100644 --- a/ets2panda/ir/ts/tsFunctionType.h +++ b/ets2panda/ir/ts/tsFunctionType.h @@ -81,6 +81,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsImportEqualsDeclaration.cpp b/ets2panda/ir/ts/tsImportEqualsDeclaration.cpp index 3f08423270aaf6035b71c4e422312f5bf79acd15..3250853b953eb72f44dd231f564ab81f32f64993 100644 --- a/ets2panda/ir/ts/tsImportEqualsDeclaration.cpp +++ b/ets2panda/ir/ts/tsImportEqualsDeclaration.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" #include "ir/expressions/identifier.h" @@ -43,6 +44,11 @@ void TSImportEqualsDeclaration::Dump(ir::AstDumper *dumper) const {"isExport", is_export_}}); } +void TSImportEqualsDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSImportEqualsDeclaration"); +} + void TSImportEqualsDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsImportEqualsDeclaration.h b/ets2panda/ir/ts/tsImportEqualsDeclaration.h index f4fff48ffe3c319988857d2d21a805e318eb6643..70b6fc99586c565a897066556d28b3e556ed1150 100644 --- a/ets2panda/ir/ts/tsImportEqualsDeclaration.h +++ b/ets2panda/ir/ts/tsImportEqualsDeclaration.h @@ -49,6 +49,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsImportType.cpp b/ets2panda/ir/ts/tsImportType.cpp index 316ba0551154e56b010124d5e1fdd61efabd044d..a3f5c89f58fdfcf763cd61c85ea7444b0cc5122a 100644 --- a/ets2panda/ir/ts/tsImportType.cpp +++ b/ets2panda/ir/ts/tsImportType.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ts/tsTypeParameter.h" #include "ir/ts/tsTypeParameterInstantiation.h" @@ -58,6 +59,11 @@ void TSImportType::Dump(ir::AstDumper *dumper) const {"isTypeOf", is_typeof_}}); } +void TSImportType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSImportType"); +} + void TSImportType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsImportType.h b/ets2panda/ir/ts/tsImportType.h index 2c182abd5953c3d6eebd8a2591ee75166cadacde..bf48f27d4ecbb39550b96d1dc7f5cf4e7b27f10d 100644 --- a/ets2panda/ir/ts/tsImportType.h +++ b/ets2panda/ir/ts/tsImportType.h @@ -56,6 +56,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsIndexedAccessType.cpp b/ets2panda/ir/ts/tsIndexedAccessType.cpp index 22d534af67ca2e7c2ba32f4340e059482c1fc213..cf48ba907b33157f126850573031d025f098be76 100644 --- a/ets2panda/ir/ts/tsIndexedAccessType.cpp +++ b/ets2panda/ir/ts/tsIndexedAccessType.cpp @@ -18,6 +18,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" @@ -39,6 +40,11 @@ void TSIndexedAccessType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSIndexedAccessType"}, {"objectType", object_type_}, {"indexType", index_type_}}); } +void TSIndexedAccessType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSIndexedAccessType"); +} + void TSIndexedAccessType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsIndexedAccessType.h b/ets2panda/ir/ts/tsIndexedAccessType.h index ab7a43f9a4bab695c0c0a9a9784006700f13bfa6..d3f0154513ed6937f23bfa26da038b68e660a20e 100644 --- a/ets2panda/ir/ts/tsIndexedAccessType.h +++ b/ets2panda/ir/ts/tsIndexedAccessType.h @@ -45,6 +45,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsInferType.cpp b/ets2panda/ir/ts/tsInferType.cpp index d108bb9540087e4571bec027eecfd970346b17c8..89c61ff68544ca56b2c61ffa1b9f872270477821 100644 --- a/ets2panda/ir/ts/tsInferType.cpp +++ b/ets2panda/ir/ts/tsInferType.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ts/tsTypeParameter.h" namespace panda::es2panda::ir { @@ -37,6 +38,11 @@ void TSInferType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSInferType"}, {"typeParameter", type_param_}}); } +void TSInferType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSInferType"); +} + void TSInferType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsInferType.h b/ets2panda/ir/ts/tsInferType.h index 0609b12c9637ffb87052011c11c75aabb0360fcb..5f7023b99be5732b558b4124fae3c399bdc96e45 100644 --- a/ets2panda/ir/ts/tsInferType.h +++ b/ets2panda/ir/ts/tsInferType.h @@ -33,6 +33,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsInterfaceBody.cpp b/ets2panda/ir/ts/tsInterfaceBody.cpp index 418b66f22dfc2943db631cfbcaee60592f0be93f..ecdb20767258843ee742c126607d2a0c44bd21a3 100644 --- a/ets2panda/ir/ts/tsInterfaceBody.cpp +++ b/ets2panda/ir/ts/tsInterfaceBody.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSInterfaceBody::TransformChildren(const NodeTransformer &cb) @@ -40,6 +41,13 @@ void TSInterfaceBody::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSInterfaceBody"}, {"body", body_}}); } +void TSInterfaceBody::Dump(ir::SrcDumper *dumper) const +{ + for (auto b : body_) { + b->Dump(dumper); + } +} + void TSInterfaceBody::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsInterfaceBody.h b/ets2panda/ir/ts/tsInterfaceBody.h index 4e0396b934f2b1328034f59fe508a627def76b21..74bd7cc0ace68a143e68c8cbf6864ee57e60e5f6 100644 --- a/ets2panda/ir/ts/tsInterfaceBody.h +++ b/ets2panda/ir/ts/tsInterfaceBody.h @@ -44,6 +44,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsInterfaceDeclaration.cpp b/ets2panda/ir/ts/tsInterfaceDeclaration.cpp index 9dc4135dcef03f75145752a0ae9c485875a23d51..8aa2b8ebc9b91a66a9352a1a3be5240c5a122696 100644 --- a/ets2panda/ir/ts/tsInterfaceDeclaration.cpp +++ b/ets2panda/ir/ts/tsInterfaceDeclaration.cpp @@ -15,6 +15,7 @@ #include "tsInterfaceDeclaration.h" +#include "macros.h" #include "varbinder/declaration.h" #include "varbinder/scope.h" #include "varbinder/variable.h" @@ -23,6 +24,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/decorator.h" #include "ir/expressions/identifier.h" #include "ir/ts/tsInterfaceBody.h" @@ -79,6 +81,41 @@ void TSInterfaceDeclaration::Dump(ir::AstDumper *dumper) const {"typeParameters", AstDumper::Optional(type_params_)}}); } +void TSInterfaceDeclaration::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(id_); + + dumper->Add("interface "); + id_->Dump(dumper); + + if (type_params_ != nullptr) { + dumper->Add("<"); + type_params_->Dump(dumper); + dumper->Add(">"); + } + + if (!extends_.empty()) { + dumper->Add(" extends "); + for (auto ext : extends_) { + ext->Dump(dumper); + if (ext != extends_.back()) { + dumper->Add(", "); + } + } + } + + dumper->Add(" {"); + if (body_ != nullptr) { + dumper->IncrIndent(); + dumper->Endl(); + body_->Dump(dumper); + dumper->DecrIndent(); + dumper->Endl(); + } + dumper->Add("}"); + dumper->Endl(); +} + void TSInterfaceDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsInterfaceDeclaration.h b/ets2panda/ir/ts/tsInterfaceDeclaration.h index ce7d18f62257df6ccf5af444fe5fc5ec75e85422..832f9adb67b456bd1a29674508ad8bc6625ec666 100644 --- a/ets2panda/ir/ts/tsInterfaceDeclaration.h +++ b/ets2panda/ir/ts/tsInterfaceDeclaration.h @@ -154,6 +154,7 @@ public: void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsInterfaceHeritage.cpp b/ets2panda/ir/ts/tsInterfaceHeritage.cpp index e14e86ecaac7651ee823177a00c40e4943f35bd6..9346dde8c795e53a0207c8a60c7493d934ee73a2 100644 --- a/ets2panda/ir/ts/tsInterfaceHeritage.cpp +++ b/ets2panda/ir/ts/tsInterfaceHeritage.cpp @@ -14,6 +14,7 @@ */ #include "tsInterfaceHeritage.h" +#include #include "varbinder/scope.h" #include "checker/TSchecker.h" @@ -21,6 +22,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" #include "ir/ts/tsTypeParameterInstantiation.h" #include "ir/ts/tsTypeReference.h" @@ -44,6 +46,12 @@ void TSInterfaceHeritage::Dump(ir::AstDumper *dumper) const }); } +void TSInterfaceHeritage::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(expr_ != nullptr); + expr_->Dump(dumper); +} + void TSInterfaceHeritage::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsInterfaceHeritage.h b/ets2panda/ir/ts/tsInterfaceHeritage.h index c677b5a3598ae6f1132371c7f4103845e52f863e..5752c2ea127cb8253e288e1d2a7ff030429958e1 100644 --- a/ets2panda/ir/ts/tsInterfaceHeritage.h +++ b/ets2panda/ir/ts/tsInterfaceHeritage.h @@ -39,6 +39,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsIntersectionType.cpp b/ets2panda/ir/ts/tsIntersectionType.cpp index a25efa4371b2611fea732dc37e1ce2dd278bf8c0..42a0b7e16dbffca6193f7f16792360d34504e657 100644 --- a/ets2panda/ir/ts/tsIntersectionType.cpp +++ b/ets2panda/ir/ts/tsIntersectionType.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/ETSchecker.h" namespace panda::es2panda::ir { @@ -41,6 +42,11 @@ void TSIntersectionType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSIntersectionType"}, {"types", types_}}); } +void TSIntersectionType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSIntersectionType"); +} + void TSIntersectionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsIntersectionType.h b/ets2panda/ir/ts/tsIntersectionType.h index 08e94c2a0e1d4302c2f0ecd70ca3bed25f54bf49..4c205273f046b6d14d58e00c97559aabf0e38178 100644 --- a/ets2panda/ir/ts/tsIntersectionType.h +++ b/ets2panda/ir/ts/tsIntersectionType.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsLiteralType.cpp b/ets2panda/ir/ts/tsLiteralType.cpp index 44d05c36f516a882d5a203942a870d6eadc0a5b6..ed241dc8d26150b3ab30cde2ff5bae8cc51307dd 100644 --- a/ets2panda/ir/ts/tsLiteralType.cpp +++ b/ets2panda/ir/ts/tsLiteralType.cpp @@ -18,6 +18,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { @@ -36,6 +37,11 @@ void TSLiteralType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSLiteralType"}, {"literal", literal_}}); } +void TSLiteralType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSLiteralType"); +} + void TSLiteralType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsLiteralType.h b/ets2panda/ir/ts/tsLiteralType.h index 44088e32cb927a6e39c076e90127b576126b0f69..b867c0e1b14840883acb50d103e3bf4213c61ac4 100644 --- a/ets2panda/ir/ts/tsLiteralType.h +++ b/ets2panda/ir/ts/tsLiteralType.h @@ -31,6 +31,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsMappedType.cpp b/ets2panda/ir/ts/tsMappedType.cpp index cb9c54072d10beec131d13ad3906b1f410c81ad8..ed00ebd1d8fd4d33f62b0e7e495f2660292958c0 100644 --- a/ets2panda/ir/ts/tsMappedType.cpp +++ b/ets2panda/ir/ts/tsMappedType.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" #include "ir/ts/tsTypeParameter.h" @@ -52,6 +53,11 @@ void TSMappedType::Dump(ir::AstDumper *dumper) const : AstDumper::Optional("-")}}); } +void TSMappedType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSMappedType"); +} + void TSMappedType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsMappedType.h b/ets2panda/ir/ts/tsMappedType.h index 42d352b3a3c01b2242621500070879a20859d763..a073f939a0edcb089fd8dd731696cf49f0b3ccc0 100644 --- a/ets2panda/ir/ts/tsMappedType.h +++ b/ets2panda/ir/ts/tsMappedType.h @@ -55,6 +55,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsModuleBlock.cpp b/ets2panda/ir/ts/tsModuleBlock.cpp index 1b3443a72c8b68448e704a76dfe84cfdc75561fd..806d4282fd4990cc3a14ee101a757b4c2a53d89e 100644 --- a/ets2panda/ir/ts/tsModuleBlock.cpp +++ b/ets2panda/ir/ts/tsModuleBlock.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSModuleBlock::TransformChildren(const NodeTransformer &cb) @@ -41,6 +42,11 @@ void TSModuleBlock::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSModuleBlock"}, {"body", statements_}}); } +void TSModuleBlock::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSModuleBlock"); +} + void TSModuleBlock::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsModuleBlock.h b/ets2panda/ir/ts/tsModuleBlock.h index f6c512a94b8c80c2e2e7412bfdf7d7f9b0c514c1..e4eab3a1a3122f6ae92c254a17638ba57f43f2d5 100644 --- a/ets2panda/ir/ts/tsModuleBlock.h +++ b/ets2panda/ir/ts/tsModuleBlock.h @@ -50,6 +50,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsModuleDeclaration.cpp b/ets2panda/ir/ts/tsModuleDeclaration.cpp index 25ad9e6570e9bdf2044c4983f8fc51e3772b035a..7fe62de00c55e2431fdbbc927e55a2179692aa26 100644 --- a/ets2panda/ir/ts/tsModuleDeclaration.cpp +++ b/ets2panda/ir/ts/tsModuleDeclaration.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/base/decorator.h" #include "ir/expression.h" @@ -60,6 +61,11 @@ void TSModuleDeclaration::Dump(ir::AstDumper *dumper) const {"global", global_}}); } +void TSModuleDeclaration::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSModuleDeclaration"); +} + void TSModuleDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsModuleDeclaration.h b/ets2panda/ir/ts/tsModuleDeclaration.h index ba3aa58bbaf670ce41d675e6f7fb82212357501d..d2d5d0cf774ac56dfd73b450c9399d38c4de826c 100644 --- a/ets2panda/ir/ts/tsModuleDeclaration.h +++ b/ets2panda/ir/ts/tsModuleDeclaration.h @@ -89,6 +89,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsNamedTupleMember.cpp b/ets2panda/ir/ts/tsNamedTupleMember.cpp index bfea9f07de9f9dc64e229988c6414918ffc43d4c..5aec5d96fce8106d0b5bde5cb4be593b0c465775 100644 --- a/ets2panda/ir/ts/tsNamedTupleMember.cpp +++ b/ets2panda/ir/ts/tsNamedTupleMember.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSNamedTupleMember::TransformChildren(const NodeTransformer &cb) @@ -42,6 +43,11 @@ void TSNamedTupleMember::Dump(ir::AstDumper *dumper) const {"optional", AstDumper::Optional(optional_)}}); } +void TSNamedTupleMember::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSNamedTupleMember"); +} + void TSNamedTupleMember::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsNamedTupleMember.h b/ets2panda/ir/ts/tsNamedTupleMember.h index 8cec5e8617b07956fa698cb5448e59f51266abd8..28545cbb7f4f0965dea1ad8b92842630795adeb3 100644 --- a/ets2panda/ir/ts/tsNamedTupleMember.h +++ b/ets2panda/ir/ts/tsNamedTupleMember.h @@ -49,6 +49,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsNeverKeyword.cpp b/ets2panda/ir/ts/tsNeverKeyword.cpp index ee52a42c6b9b112dfa05c6ffbcdb6125fbcf0b5a..092d86ca89fd6f20a608adf79897d91645f59beb 100644 --- a/ets2panda/ir/ts/tsNeverKeyword.cpp +++ b/ets2panda/ir/ts/tsNeverKeyword.cpp @@ -18,6 +18,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { @@ -29,6 +30,11 @@ void TSNeverKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSNeverKeyword"}}); } +void TSNeverKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSNeverKeyword"); +} + void TSNeverKeyword::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsNeverKeyword.h b/ets2panda/ir/ts/tsNeverKeyword.h index 8df44f6a79745febc19104dc20857488bbf8ad42..948991fba6d2595bb90f1217fb355a02f6cc85bc 100644 --- a/ets2panda/ir/ts/tsNeverKeyword.h +++ b/ets2panda/ir/ts/tsNeverKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsNonNullExpression.cpp b/ets2panda/ir/ts/tsNonNullExpression.cpp index ac49f17ac8ae1bf8b21f7d09aac60c026cc46a87..7dc7a91a68a3472af3a4ab5f79f0c6944082ff46 100644 --- a/ets2panda/ir/ts/tsNonNullExpression.cpp +++ b/ets2panda/ir/ts/tsNonNullExpression.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSNonNullExpression::TransformChildren(const NodeTransformer &cb) @@ -37,6 +38,13 @@ void TSNonNullExpression::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSNonNullExpression"}, {"expression", expr_}}); } +void TSNonNullExpression::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(expr_ != nullptr); + expr_->Dump(dumper); + dumper->Add("!"); +} + void TSNonNullExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsNonNullExpression.h b/ets2panda/ir/ts/tsNonNullExpression.h index ebd95ca8f9f6fda9da520ee99d46b35ab0d04be7..1453a835d4e7527d5f9ef30361e144a5750aeba6 100644 --- a/ets2panda/ir/ts/tsNonNullExpression.h +++ b/ets2panda/ir/ts/tsNonNullExpression.h @@ -37,6 +37,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *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; diff --git a/ets2panda/ir/ts/tsNullKeyword.cpp b/ets2panda/ir/ts/tsNullKeyword.cpp index 1ecd8382424dbc59c457fbed877881178e5524a4..6c380c67d3ab980f7c3724fd2f8a8fd227928c39 100644 --- a/ets2panda/ir/ts/tsNullKeyword.cpp +++ b/ets2panda/ir/ts/tsNullKeyword.cpp @@ -18,6 +18,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { @@ -29,6 +30,11 @@ void TSNullKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSNullKeyword"}}); } +void TSNullKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSNullKeyword"); +} + void TSNullKeyword::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsNullKeyword.h b/ets2panda/ir/ts/tsNullKeyword.h index 6f72575c7eb91a63b7db140fb86b243444e24ba5..5c87da2caf71d23486b706fc0162034abbf54276 100644 --- a/ets2panda/ir/ts/tsNullKeyword.h +++ b/ets2panda/ir/ts/tsNullKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsNumberKeyword.cpp b/ets2panda/ir/ts/tsNumberKeyword.cpp index 13aca3172f286fb496bf7b9cbaecaacd2f6cdb43..0a7259494a92ddbdb2b6d834b0e389e0847ab766 100644 --- a/ets2panda/ir/ts/tsNumberKeyword.cpp +++ b/ets2panda/ir/ts/tsNumberKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSNumberKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSNumberKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSNumberKeyword"}}); } +void TSNumberKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSNumberKeyword"); +} + void TSNumberKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsNumberKeyword.h b/ets2panda/ir/ts/tsNumberKeyword.h index 1b24a8696f7c39f7ff9886317c166a425dbc7e90..33257ae935820444e57c613e194018d783971d5f 100644 --- a/ets2panda/ir/ts/tsNumberKeyword.h +++ b/ets2panda/ir/ts/tsNumberKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsObjectKeyword.cpp b/ets2panda/ir/ts/tsObjectKeyword.cpp index da0fea006153b8aca88c0a39aecb43b9f7db034f..ccbfbb678e93f15cbb382fa06c8466fe5df4ad9b 100644 --- a/ets2panda/ir/ts/tsObjectKeyword.cpp +++ b/ets2panda/ir/ts/tsObjectKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSObjectKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSObjectKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSObjectKeyword"}}); } +void TSObjectKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSObjectKeyword"); +} + void TSObjectKeyword::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsObjectKeyword.h b/ets2panda/ir/ts/tsObjectKeyword.h index 2a1b328e6f51d81b2dd33c560ff4eaf70d51e69b..69f6c1ca9e5045f28090b923282893c8389d4e6e 100644 --- a/ets2panda/ir/ts/tsObjectKeyword.h +++ b/ets2panda/ir/ts/tsObjectKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsParameterProperty.cpp b/ets2panda/ir/ts/tsParameterProperty.cpp index 4f5cfd854a5e5408b0c0818b0562a41264efac1b..30917530425d5e6e5c722a0d47f018500432e0ae 100644 --- a/ets2panda/ir/ts/tsParameterProperty.cpp +++ b/ets2panda/ir/ts/tsParameterProperty.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" namespace panda::es2panda::ir { @@ -45,6 +46,11 @@ void TSParameterProperty::Dump(ir::AstDumper *dumper) const {"parameter", parameter_}}); } +void TSParameterProperty::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSParameterProperty"); +} + void TSParameterProperty::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsParameterProperty.h b/ets2panda/ir/ts/tsParameterProperty.h index 4144e57cb44baa0c96af68923ae779044d2cc2ef..9f8baa2b18f1c8e0d928bbd22b716610b84cac0a 100644 --- a/ets2panda/ir/ts/tsParameterProperty.h +++ b/ets2panda/ir/ts/tsParameterProperty.h @@ -63,6 +63,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsParenthesizedType.cpp b/ets2panda/ir/ts/tsParenthesizedType.cpp index 04012a2b20bb43aa0060e3eef2119ab9ea7bb8b3..d6e415dd19f93ef9a910c39c7a0ea98fbea055fb 100644 --- a/ets2panda/ir/ts/tsParenthesizedType.cpp +++ b/ets2panda/ir/ts/tsParenthesizedType.cpp @@ -18,6 +18,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { @@ -36,6 +37,11 @@ void TSParenthesizedType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSParenthesizedType"}, {"typeAnnotation", type_}}); } +void TSParenthesizedType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSParenthesizedType"); +} + void TSParenthesizedType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsParenthesizedType.h b/ets2panda/ir/ts/tsParenthesizedType.h index 1eac0852dc7459e95c906ac26efa33c2f31b3f49..61cf2ea527452a4e36efa32e859cbb54e325df1f 100644 --- a/ets2panda/ir/ts/tsParenthesizedType.h +++ b/ets2panda/ir/ts/tsParenthesizedType.h @@ -36,6 +36,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsQualifiedName.cpp b/ets2panda/ir/ts/tsQualifiedName.cpp index c9099debbde40ea9d58efeaf21ecdd026ec2cf55..d87a88409a2a5098ceb1d26714e41485040e1c66 100644 --- a/ets2panda/ir/ts/tsQualifiedName.cpp +++ b/ets2panda/ir/ts/tsQualifiedName.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" namespace panda::es2panda::ir { @@ -40,6 +41,11 @@ void TSQualifiedName::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSQualifiedName"}, {"left", left_}, {"right", right_}}); } +void TSQualifiedName::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSQualifiedName"); +} + void TSQualifiedName::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsQualifiedName.h b/ets2panda/ir/ts/tsQualifiedName.h index 7d00a524d5784456fffd58592fdb0db17552316c..a59bf6aa3a2fd7988ad27fd2e8de52b53817ea14 100644 --- a/ets2panda/ir/ts/tsQualifiedName.h +++ b/ets2panda/ir/ts/tsQualifiedName.h @@ -54,6 +54,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsStringKeyword.cpp b/ets2panda/ir/ts/tsStringKeyword.cpp index 11caf9ca5a439ab380ae10e48c43135243033167..b28c67c7b13653c2486b9a1a2dbb5ee5e36e7d62 100644 --- a/ets2panda/ir/ts/tsStringKeyword.cpp +++ b/ets2panda/ir/ts/tsStringKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSStringKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSStringKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSStringKeyword"}}); } +void TSStringKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSStringKeyword"); +} + void TSStringKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsStringKeyword.h b/ets2panda/ir/ts/tsStringKeyword.h index 9fe1030d0f8cf0ac4fb48de9ef556facecbc1cb2..18bd3c338b055206bb8e5162ebb1e8cc415593ca 100644 --- a/ets2panda/ir/ts/tsStringKeyword.h +++ b/ets2panda/ir/ts/tsStringKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsThisType.cpp b/ets2panda/ir/ts/tsThisType.cpp index 9827778d624a63a5faa7d69a5a3b5240ec7b3dba..4efb3209f80712ae2025cc3dbe2647fa9bc4b79c 100644 --- a/ets2panda/ir/ts/tsThisType.cpp +++ b/ets2panda/ir/ts/tsThisType.cpp @@ -15,10 +15,12 @@ #include "tsThisType.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/srcDump.h" namespace panda::es2panda::ir { void TSThisType::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -29,6 +31,11 @@ void TSThisType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSThisType"}}); } +void TSThisType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSThisType"); +} + void TSThisType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -52,4 +59,9 @@ checker::Type *TSThisType::Check([[maybe_unused]] checker::ETSChecker *checker) { return checker->GetAnalyzer()->Check(this); } + +checker::Type *TSThisType::GetType([[maybe_unused]] checker::ETSChecker *checker) +{ + return checker->Context().ContainingClass(); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ts/tsThisType.h b/ets2panda/ir/ts/tsThisType.h index 82e9f4062dc9a70944fe72c2c97fb4283ff5765b..2d183f88ce2e2918a06e20ca50305b84c6650097 100644 --- a/ets2panda/ir/ts/tsThisType.h +++ b/ets2panda/ir/ts/tsThisType.h @@ -26,11 +26,13 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) 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::ETSChecker *checker) override; void Accept(ASTVisitorT *v) override { diff --git a/ets2panda/ir/ts/tsTupleType.cpp b/ets2panda/ir/ts/tsTupleType.cpp index 624d9582339de06f529aec7f65d22778c9842f6f..80111e7c27556570f484e76e980f9458178acb8f 100644 --- a/ets2panda/ir/ts/tsTupleType.cpp +++ b/ets2panda/ir/ts/tsTupleType.cpp @@ -22,6 +22,7 @@ #include "checker/TSchecker.h" #include "checker/types/ts/indexInfo.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" #include "ir/ts/tsNamedTupleMember.h" @@ -45,6 +46,11 @@ void TSTupleType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSTupleType"}, {"elementTypes", element_types_}}); } +void TSTupleType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSTupleType"); +} + void TSTupleType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTupleType.h b/ets2panda/ir/ts/tsTupleType.h index 5cf53189dadeadeaf03073cf486019b30d71e369..c855bffec111b7f3d932da7ba77c1357d6d590a0 100644 --- a/ets2panda/ir/ts/tsTupleType.h +++ b/ets2panda/ir/ts/tsTupleType.h @@ -36,6 +36,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp b/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp index 83bf379328b27f1fb35a5f84590a85d7ecb6d82e..21180374a6bf9c6424ae520bc7f81293fb27795e 100644 --- a/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp +++ b/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp @@ -14,12 +14,15 @@ */ #include "tsTypeAliasDeclaration.h" +#include +#include "macros.h" #include "varbinder/scope.h" #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" #include "ir/base/decorator.h" #include "ir/expressions/identifier.h" @@ -71,6 +74,26 @@ void TSTypeAliasDeclaration::Dump(ir::AstDumper *dumper) const {"declare", AstDumper::Optional(declare_)}}); } +void TSTypeAliasDeclaration::Dump(ir::SrcDumper *dumper) const +{ + ASSERT(id_); + dumper->Add("type "); + id_->Dump(dumper); + if (type_params_ != nullptr) { + dumper->Add("<"); + type_params_->Dump(dumper); + dumper->Add(">"); + } + dumper->Add(" = "); + if (id_->IsAnnotatedExpression()) { + auto type = TypeAnnotation(); + ASSERT(type); + type->Dump(dumper); + } + dumper->Add(";"); + dumper->Endl(); +} + void TSTypeAliasDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); @@ -90,4 +113,5 @@ checker::Type *TSTypeAliasDeclaration::Check([[maybe_unused]] checker::ETSChecke { return checker->GetAnalyzer()->Check(this); } + } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ts/tsTypeAliasDeclaration.h b/ets2panda/ir/ts/tsTypeAliasDeclaration.h index 5108c08e37c23a29448c5b9c5663ac550efe947b..9c4415dd5a00d0716340b10c786ec83b63ccaa03 100644 --- a/ets2panda/ir/ts/tsTypeAliasDeclaration.h +++ b/ets2panda/ir/ts/tsTypeAliasDeclaration.h @@ -95,6 +95,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeAssertion.cpp b/ets2panda/ir/ts/tsTypeAssertion.cpp index 2c2bd745e3e9bed7029869145b56bc9578a8d54a..15a20c7d09f28fb61eccfc895ad6be2fa9aa5a56 100644 --- a/ets2panda/ir/ts/tsTypeAssertion.cpp +++ b/ets2panda/ir/ts/tsTypeAssertion.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" namespace panda::es2panda::ir { @@ -39,6 +40,11 @@ void TSTypeAssertion::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSTypeAssertion"}, {"typeAnnotation", TypeAnnotation()}, {"expression", expression_}}); } +void TSTypeAssertion::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSTypeAssertion"); +} + void TSTypeAssertion::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeAssertion.h b/ets2panda/ir/ts/tsTypeAssertion.h index 90f2856ed402b9535f3e9f075479c256f7db5e2c..e06e81fa90c1d564e80f83e3337246abd0b4e57c 100644 --- a/ets2panda/ir/ts/tsTypeAssertion.h +++ b/ets2panda/ir/ts/tsTypeAssertion.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeLiteral.cpp b/ets2panda/ir/ts/tsTypeLiteral.cpp index 9f9471ab8fb7a9f39625d2f9b8eeb80f420333a7..c05861d2bd581a40580372ec0afed8dd566c79ca 100644 --- a/ets2panda/ir/ts/tsTypeLiteral.cpp +++ b/ets2panda/ir/ts/tsTypeLiteral.cpp @@ -18,6 +18,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "varbinder/variable.h" #include "varbinder/declaration.h" @@ -44,6 +45,11 @@ void TSTypeLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSTypeLiteral"}, {"members", members_}}); } +void TSTypeLiteral::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSTypeLiteral"); +} + void TSTypeLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeLiteral.h b/ets2panda/ir/ts/tsTypeLiteral.h index 773cae0061e25182c5cba84f7270bad3567f153c..9de5ba6811da6b42162b0855091cc307045ccd8c 100644 --- a/ets2panda/ir/ts/tsTypeLiteral.h +++ b/ets2panda/ir/ts/tsTypeLiteral.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeOperator.cpp b/ets2panda/ir/ts/tsTypeOperator.cpp index f945f6e43f3fccbac777ed96d7198f5d44a2dcb4..05aa4ef7277263100e9f6dfc8551228c7cd5de29 100644 --- a/ets2panda/ir/ts/tsTypeOperator.cpp +++ b/ets2panda/ir/ts/tsTypeOperator.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSTypeOperator::TransformChildren(const NodeTransformer &cb) @@ -40,6 +41,11 @@ void TSTypeOperator::Dump(ir::AstDumper *dumper) const }); } +void TSTypeOperator::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSTypeOperator"); +} + void TSTypeOperator::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeOperator.h b/ets2panda/ir/ts/tsTypeOperator.h index 921746337bd6cf530e3c86a00a173ea611a8735b..8c8266290228b4b6c0a166cdcd2f650245bb0d9c 100644 --- a/ets2panda/ir/ts/tsTypeOperator.h +++ b/ets2panda/ir/ts/tsTypeOperator.h @@ -49,6 +49,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeParameter.cpp b/ets2panda/ir/ts/tsTypeParameter.cpp index e464990af26f8ebd0bad98179ed160704bc00e1f..a16ad2100c837dcae9efb742929fd3bf1313300a 100644 --- a/ets2panda/ir/ts/tsTypeParameter.cpp +++ b/ets2panda/ir/ts/tsTypeParameter.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" #include "ir/expressions/identifier.h" @@ -61,6 +62,11 @@ void TSTypeParameter::Dump(ir::AstDumper *dumper) const }); } +void TSTypeParameter::Dump(ir::SrcDumper *dumper) const +{ + name_->Dump(dumper); +} + void TSTypeParameter::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeParameter.h b/ets2panda/ir/ts/tsTypeParameter.h index edcde8069da022de1d71073769673cafe5885a9b..3b0699133b7a846454e9518804bd9731dc4fac99 100644 --- a/ets2panda/ir/ts/tsTypeParameter.h +++ b/ets2panda/ir/ts/tsTypeParameter.h @@ -57,7 +57,7 @@ public: return constraint_; } - const TypeNode *DefaultType() const + TypeNode *DefaultType() const { return default_type_; } @@ -65,6 +65,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeParameterDeclaration.cpp b/ets2panda/ir/ts/tsTypeParameterDeclaration.cpp index 128f2abae2068a644352b0db973d26b3e4956116..0a34564701a933c11ca28c22506d4a7249bc6bbf 100644 --- a/ets2panda/ir/ts/tsTypeParameterDeclaration.cpp +++ b/ets2panda/ir/ts/tsTypeParameterDeclaration.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/ts/tsTypeParameter.h" namespace panda::es2panda::ir { @@ -42,6 +43,16 @@ void TSTypeParameterDeclaration::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSTypeParameterDeclaration"}, {"params", params_}}); } +void TSTypeParameterDeclaration::Dump(ir::SrcDumper *dumper) const +{ + for (auto param : params_) { + param->Dump(dumper); + if (param != params_.back()) { + dumper->Add(", "); + } + } +} + void TSTypeParameterDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeParameterDeclaration.h b/ets2panda/ir/ts/tsTypeParameterDeclaration.h index cf2b725a49c0043cc9906d04e191841f48c3b841..d0e81380ab81aab9e68c1d79855d77e8cb6a3ec9 100644 --- a/ets2panda/ir/ts/tsTypeParameterDeclaration.h +++ b/ets2panda/ir/ts/tsTypeParameterDeclaration.h @@ -67,6 +67,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp b/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp index e4174bf081fb6118d623ad5ce439b0dee02a38c5..ef538c9bcf72eee3a1a702979bec167a0f0163f1 100644 --- a/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp +++ b/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp @@ -20,6 +20,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expression.h" #include "ir/typeNode.h" @@ -66,6 +67,20 @@ void TSTypeParameterInstantiation::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSTypeParameterInstantiation"}, {"params", params_}}); } +void TSTypeParameterInstantiation::Dump(ir::SrcDumper *dumper) const +{ + if (!params_.empty()) { + dumper->Add("<"); + for (auto param : params_) { + param->Dump(dumper); + if (param != params_.back()) { + dumper->Add(", "); + } + } + dumper->Add(">"); + } +} + void TSTypeParameterInstantiation::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeParameterInstantiation.h b/ets2panda/ir/ts/tsTypeParameterInstantiation.h index b05622389812449ffedb6cf637f7583100fc1765..be57a1c502e38aa4e83c37d498263212135bfbe5 100644 --- a/ets2panda/ir/ts/tsTypeParameterInstantiation.h +++ b/ets2panda/ir/ts/tsTypeParameterInstantiation.h @@ -48,6 +48,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypePredicate.cpp b/ets2panda/ir/ts/tsTypePredicate.cpp index 16c99b8747da8bd8615dc326242b48f93af2ba9a..944b36c0e646ee76d3c29b1ab2a38ab8453d636c 100644 --- a/ets2panda/ir/ts/tsTypePredicate.cpp +++ b/ets2panda/ir/ts/tsTypePredicate.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/typeNode.h" #include "ir/expression.h" @@ -46,6 +47,10 @@ void TSTypePredicate::Dump(ir::AstDumper *dumper) const {"typeAnnotation", AstDumper::Nullish(type_annotation_)}, {"asserts", asserts_}}); } +void TSTypePredicate::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSTypePredicate"); +} void TSTypePredicate::Compile([[maybe_unused]] compiler::PandaGen *pg) const { diff --git a/ets2panda/ir/ts/tsTypePredicate.h b/ets2panda/ir/ts/tsTypePredicate.h index 9a9ac81a86548b39bee4c76be329bc7425afdc95..7c67230dc4a69b7cadae12f6e73fd7d78c4db4c1 100644 --- a/ets2panda/ir/ts/tsTypePredicate.h +++ b/ets2panda/ir/ts/tsTypePredicate.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeQuery.cpp b/ets2panda/ir/ts/tsTypeQuery.cpp index cb45ace08c1db5708ae58ea18132dad7ba709cb1..c9f24088cb9c938c8a1175b59a11441cbd306989 100644 --- a/ets2panda/ir/ts/tsTypeQuery.cpp +++ b/ets2panda/ir/ts/tsTypeQuery.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { @@ -37,6 +38,11 @@ void TSTypeQuery::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSTypeQuery"}, {"exprName", expr_name_}}); } +void TSTypeQuery::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSTypeQuery"); +} + void TSTypeQuery::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeQuery.h b/ets2panda/ir/ts/tsTypeQuery.h index 0c6b3a86d255360a8ab8d1148b061f82d0c53edf..83a2ffbbbcbd089579ffcbb333c06de4a3075213 100644 --- a/ets2panda/ir/ts/tsTypeQuery.h +++ b/ets2panda/ir/ts/tsTypeQuery.h @@ -35,6 +35,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsTypeReference.cpp b/ets2panda/ir/ts/tsTypeReference.cpp index ed6d359cefcf88b11ff1ff199c8c0aecb4c191ca..654fbd59e8ec3f5c646ca27a23a475931b5fbae8 100644 --- a/ets2panda/ir/ts/tsTypeReference.cpp +++ b/ets2panda/ir/ts/tsTypeReference.cpp @@ -22,6 +22,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" #include "ir/expressions/identifier.h" #include "ir/ts/tsInterfaceDeclaration.h" #include "ir/ts/tsTypeAliasDeclaration.h" @@ -54,6 +55,11 @@ void TSTypeReference::Dump(ir::AstDumper *dumper) const {{"type", "TSTypeReference"}, {"typeName", type_name_}, {"typeParameters", AstDumper::Optional(type_params_)}}); } +void TSTypeReference::Dump(ir::SrcDumper *dumper) const +{ + BaseName()->Dump(dumper); +} + void TSTypeReference::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsTypeReference.h b/ets2panda/ir/ts/tsTypeReference.h index 3bbed87b89c28b2c4c8e7606f7e2baf1b8b50a29..24f64e8e23685335fed233c567eb22ec8fa86582 100644 --- a/ets2panda/ir/ts/tsTypeReference.h +++ b/ets2panda/ir/ts/tsTypeReference.h @@ -47,6 +47,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsUndefinedKeyword.cpp b/ets2panda/ir/ts/tsUndefinedKeyword.cpp index ba59f6f222104216981d9d0947bfec593be26db7..30bd1818c20dd6a1e337a815ac66cafc4d256d9e 100644 --- a/ets2panda/ir/ts/tsUndefinedKeyword.cpp +++ b/ets2panda/ir/ts/tsUndefinedKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSUndefinedKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSUndefinedKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSUndefinedKeyword"}}); } +void TSUndefinedKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSUndefinedKeyword"); +} + void TSUndefinedKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsUndefinedKeyword.h b/ets2panda/ir/ts/tsUndefinedKeyword.h index cff5e9407892e6ed15cf6586b899af767a134198..14fc4cd18ec6be2a2a183af39a9d076b9d46f467 100644 --- a/ets2panda/ir/ts/tsUndefinedKeyword.h +++ b/ets2panda/ir/ts/tsUndefinedKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsUnionType.cpp b/ets2panda/ir/ts/tsUnionType.cpp index 802d5d65c7695cdd3ada084044b419d576a14186..2d8c04d860e6237f6c55ac14d89d33ec6c28bbdb 100644 --- a/ets2panda/ir/ts/tsUnionType.cpp +++ b/ets2panda/ir/ts/tsUnionType.cpp @@ -19,6 +19,7 @@ #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" #include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSUnionType::TransformChildren(const NodeTransformer &cb) @@ -40,6 +41,11 @@ void TSUnionType::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSUnionType"}, {"types", types_}}); } +void TSUnionType::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSUnionType"); +} + void TSUnionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsUnionType.h b/ets2panda/ir/ts/tsUnionType.h index 925f710106dc8886d8d836e5420b98745af74b10..c32c87512ba18caa25a84a98951cef2fa7ff5197 100644 --- a/ets2panda/ir/ts/tsUnionType.h +++ b/ets2panda/ir/ts/tsUnionType.h @@ -34,6 +34,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsUnknownKeyword.cpp b/ets2panda/ir/ts/tsUnknownKeyword.cpp index d8e35bf46a3b69854a8209cb66f10adb66a48b21..44cae09613230799b9b71a777a420e4e54995011 100644 --- a/ets2panda/ir/ts/tsUnknownKeyword.cpp +++ b/ets2panda/ir/ts/tsUnknownKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSUnknownKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSUnknownKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSUnknownKeyword"}}); } +void TSUnknownKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSUnknownKeyword"); +} + void TSUnknownKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsUnknownKeyword.h b/ets2panda/ir/ts/tsUnknownKeyword.h index 6f2a3783108591222afd50a6b621f2287cc50c89..0100912bd370d264bac85217d1cc31fce3a0048d 100644 --- a/ets2panda/ir/ts/tsUnknownKeyword.h +++ b/ets2panda/ir/ts/tsUnknownKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ts/tsVoidKeyword.cpp b/ets2panda/ir/ts/tsVoidKeyword.cpp index 47502deb375b1b5088bd99d2837c606d2f93b6d4..5b39b72511311c46d803d97fa1b452ccec5368bc 100644 --- a/ets2panda/ir/ts/tsVoidKeyword.cpp +++ b/ets2panda/ir/ts/tsVoidKeyword.cpp @@ -18,6 +18,8 @@ #include "checker/TSchecker.h" #include "compiler/core/ETSGen.h" #include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/srcDump.h" namespace panda::es2panda::ir { void TSVoidKeyword::TransformChildren([[maybe_unused]] const NodeTransformer &cb) {} @@ -28,6 +30,11 @@ void TSVoidKeyword::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "TSVoidKeyword"}}); } +void TSVoidKeyword::Dump(ir::SrcDumper *dumper) const +{ + dumper->Add("TSVoidKeyword"); +} + void TSVoidKeyword::Compile(compiler::PandaGen *pg) const { pg->GetAstCompiler()->Compile(this); diff --git a/ets2panda/ir/ts/tsVoidKeyword.h b/ets2panda/ir/ts/tsVoidKeyword.h index 7525b4683b6889a960c0a51f331e68c86584265f..0ceee4d69a3bba950792f40457f0ad81232feb91 100644 --- a/ets2panda/ir/ts/tsVoidKeyword.h +++ b/ets2panda/ir/ts/tsVoidKeyword.h @@ -26,6 +26,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; + void Dump(ir::SrcDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/lexer/scripts/keywords.yaml b/ets2panda/lexer/scripts/keywords.yaml index c138fb4211d04b72a93f3340337e68027a20297c..690264b0981106674467fef736124b6ffb62a266 100644 --- a/ets2panda/lexer/scripts/keywords.yaml +++ b/ets2panda/lexer/scripts/keywords.yaml @@ -264,8 +264,7 @@ keywords: - name: 'is' token: KEYW_IS - keyword: [ets] - keyword_like: [ts] + keyword_like: [ets, ts] - name: 'isize' token: KEYW_ISIZE diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index ddee2a5b06514926610d8ffe45d9256cf5730b28..5cda523fbbcf3bd4009f6f767a03a5f3f556b448 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -16,6 +16,7 @@ #include "ETSparser.h" #include +#include "macros.h" #include "parser/parserFlags.h" #include "util/arktsconfig.h" #include "util/helpers.h" @@ -117,6 +118,7 @@ #include "ir/ts/tsTypeAliasDeclaration.h" #include "ir/ts/tsTypeParameterDeclaration.h" #include "ir/ts/tsNonNullExpression.h" +#include "ir/ts/tsThisType.h" #include "libpandabase/os/file.h" #include "generated/signatures.h" @@ -473,6 +475,8 @@ std::tuple, bool> ETSParser::CollectUserSources(const s #ifdef USE_UNIX_SYSCALL DIR *dir = opendir(resolved_path.c_str()); + bool is_index = false; + std::vector tmp_paths; if (dir == nullptr) { ThrowSyntaxError({"Cannot open folder: ", resolved_path}); @@ -493,25 +497,41 @@ std::tuple, bool> ETSParser::CollectUserSources(const s std::string file_path = path + "/" + entry->d_name; - if (file_name == "Object.ets") { - user_paths.emplace(user_paths.begin(), file_path); - } else { + if (file_name == "index.ets" || file_name == "index.ts") { user_paths.emplace_back(file_path); + is_index = true; + break; + } else if (file_name == "Object.ets") { + tmp_paths.emplace(user_paths.begin(), file_path); + } else { + tmp_paths.emplace_back(file_path); } } closedir(dir); + + if (is_index) { + return {user_paths, false}; + } + + user_paths.insert(user_paths.end(), tmp_paths.begin(), tmp_paths.end()); #else - for (auto const &entry : fs::directory_iterator(resolved_path)) { - if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { - continue; - } + if (fs::exists(resolved_path + "/index.ets")) { + user_paths.emplace_back(path + "/index.ets"); + } else if (fs::exists(resolved_path + "/index.ts")) { + user_paths.emplace_back(path + "/index.ts"); + } else { + for (auto const &entry : fs::directory_iterator(resolved_path)) { + if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { + continue; + } - std::string base_name = path; - std::size_t pos = entry.path().string().find_last_of(panda::os::file::File::GetPathDelim()); + std::string base_name = path; + std::size_t pos = entry.path().string().find_last_of(panda::os::file::File::GetPathDelim()); - base_name.append(entry.path().string().substr(pos, entry.path().string().size())); - user_paths.emplace_back(base_name); + base_name.append(entry.path().string().substr(pos, entry.path().string().size())); + user_paths.emplace_back(base_name); + } } #endif return {user_paths, false}; @@ -646,6 +666,8 @@ void ETSParser::MarkNodeAsExported(ir::AstNode *node, lexer::SourcePosition star ArenaVector ETSParser::ParseTopLevelStatements(ArenaVector &statements) { ArenaVector global_properties(Allocator()->Adapter()); + field_map_.clear(); + export_name_map_.clear(); bool default_export = false; using ParserFunctionPtr = std::function; @@ -685,7 +707,7 @@ ArenaVector ETSParser::ParseTopLevelStatements(ArenaVectorGetToken().Type() == lexer::TokenType::PUNCTUATOR_MULTIPLY || Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { - ParseReExport(Lexer()->GetToken().Start()); + ParseExport(Lexer()->GetToken().Start()); continue; } @@ -797,7 +819,17 @@ ArenaVector ETSParser::ParseTopLevelStatements(ArenaVectorSetRange({start_loc, end_loc}); + field_map_.insert({field_name->Name(), field}); declarations->push_back(field); if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COMMA) { @@ -1317,6 +1350,10 @@ ir::MethodDefinition *ETSParser::ParseClassMethodDefinition(ir::Identifier *meth new_status |= ParserStatus::ASYNC_FUNCTION; } + if ((modifiers & ir::ModifierFlags::STATIC) == 0) { + new_status |= ParserStatus::ALLOW_THIS_TYPE; + } + ir::ScriptFunction *func = ParseFunction(new_status, class_name); func->SetIdent(method_name); auto *func_expr = AllocNode(func); @@ -1328,6 +1365,8 @@ ir::MethodDefinition *ETSParser::ParseClassMethodDefinition(ir::Identifier *meth } auto *method = AllocNode(method_kind, method_name, func_expr, modifiers, Allocator(), false); method->SetRange(func_expr->Range()); + + field_map_.insert({method_name->Name(), method}); AddProxyOverloadToMethodWithDefaultParams(method, ident_node); return method; @@ -1426,8 +1465,9 @@ ir::TypeNode *ETSParser::ParseFunctionReturnType([[maybe_unused]] ParserStatus s ThrowSyntaxError("Type annotation isn't allowed for constructor."); } Lexer()->NextToken(); // eat ':' - TypeAnnotationParsingOptions options = - TypeAnnotationParsingOptions::THROW_ERROR | TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; + TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::THROW_ERROR | + TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE | + TypeAnnotationParsingOptions::RETURN_TYPE; return ParseTypeAnnotation(&options); } @@ -2226,20 +2266,42 @@ std::string ETSParser::PrimitiveTypeToName(ir::PrimitiveType type) UNREACHABLE(); } } +std::string ETSParser::GetNameForETSUnionType(const ir::TypeNode *type_annotation) const +{ + ASSERT(type_annotation->IsETSUnionType()); + std::string newstr; + for (size_t i = 0; i < type_annotation->AsETSUnionType()->Types().size(); i++) { + auto type = type_annotation->AsETSUnionType()->Types()[i]; + if (type->IsNullAssignable() || type->IsUndefinedAssignable()) { + continue; + } + std::string str = GetNameForTypeNode(type, false); + newstr += str; + if (i != type_annotation->AsETSUnionType()->Types().size() - 1) { + newstr += "|"; + } + } + if (type_annotation->IsNullAssignable()) { + newstr += "|null"; + } + if (type_annotation->IsUndefinedAssignable()) { + newstr += "|undefined"; + } + return newstr; +} -std::string ETSParser::GetNameForTypeNode(const ir::TypeNode *type_annotation) const +std::string ETSParser::GetNameForTypeNode(const ir::TypeNode *type_annotation, bool adjust) const { - if ((type_annotation->IsNullAssignable() || type_annotation->IsUndefinedAssignable()) && - type_annotation->IsETSUnionType()) { - type_annotation = type_annotation->AsETSUnionType()->Types().front(); + if (type_annotation->IsETSUnionType()) { + return GetNameForETSUnionType(type_annotation); } - const auto adjust_nullish = [type_annotation](std::string const &s) { + const auto adjust_nullish = [type_annotation, adjust](std::string const &s) { std::string newstr = s; - if (type_annotation->IsNullAssignable()) { + if (type_annotation->IsNullAssignable() && adjust) { newstr += "|null"; } - if (type_annotation->IsUndefinedAssignable()) { + if (type_annotation->IsUndefinedAssignable() && adjust) { newstr += "|undefined"; } return newstr; @@ -2764,6 +2826,10 @@ std::pair ETSParser::GetTypeAnnotationFromToken(TypeAnnota type_annotation = ParseETSTupleType(options); break; } + case lexer::TokenType::KEYW_THIS: { + type_annotation = ParseThisType(options); + break; + } default: { break; } @@ -2772,6 +2838,29 @@ std::pair ETSParser::GetTypeAnnotationFromToken(TypeAnnota return std::make_pair(type_annotation, true); } +ir::TypeNode *ETSParser::ParseThisType(TypeAnnotationParsingOptions *options) +{ + ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::KEYW_THIS); + + // A syntax error should be thrown if + // - the usage of 'this' as a type is not allowed in the current context, or + // - 'this' is not used as a return type, or + // - the current context is an arrow function (might be inside a method of a class where 'this' is allowed). + if (((*options & TypeAnnotationParsingOptions::THROW_ERROR) != 0) && + (((GetContext().Status() & ParserStatus::ALLOW_THIS_TYPE) == 0) || + ((*options & TypeAnnotationParsingOptions::RETURN_TYPE) == 0) || + ((GetContext().Status() & ParserStatus::ARROW_FUNCTION) != 0))) { + ThrowSyntaxError("A 'this' type is available only as return type in a non-static method of a class or struct."); + } + + auto *this_type = AllocNode(); + this_type->SetRange(Lexer()->GetToken().Loc()); + + Lexer()->NextToken(); // eat 'this' + + return this_type; +} + ir::TypeNode *ETSParser::ParseTypeAnnotation(TypeAnnotationParsingOptions *options) { bool const throw_error = ((*options) & TypeAnnotationParsingOptions::THROW_ERROR) != 0; @@ -2843,7 +2932,7 @@ ir::DebuggerStatement *ETSParser::ParseDebuggerStatement() ThrowUnexpectedToken(lexer::TokenType::KEYW_DEBUGGER); } -void ETSParser::ParseReExport(lexer::SourcePosition start_loc) +void ETSParser::ParseExport(lexer::SourcePosition start_loc) { ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_MULTIPLY || Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE); @@ -2855,10 +2944,12 @@ void ETSParser::ParseReExport(lexer::SourcePosition start_loc) ParseNamedSpecifiers(&specifiers, true); if (Lexer()->GetToken().KeywordType() != lexer::TokenType::KEYW_FROM) { - ThrowSyntaxError("Selective export directive is not implemented yet"); + // selective export directive + return; } } + // re-export directive ir::ImportSource *re_export_source = nullptr; std::vector user_paths; @@ -3032,8 +3123,9 @@ std::vector ETSParser::ParseImportDeclarations(ArenaVector *specifiers, bool is_re_export) +void ETSParser::ParseNamedSpecifiers(ArenaVector *specifiers, bool is_export) { + lexer::SourcePosition start_loc = Lexer()->GetToken().Start(); // NOTE(user): handle qualifiedName in file bindings: qualifiedName '.' '*' if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { ThrowExpectedToken(lexer::TokenType::PUNCTUATOR_LEFT_BRACE); @@ -3041,6 +3133,7 @@ void ETSParser::ParseNamedSpecifiers(ArenaVector *specifiers, boo Lexer()->NextToken(); // eat '{' auto file_name = GetProgram()->SourceFilePath().Mutf8(); + std::vector exported_idents; while (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_MULTIPLY) { @@ -3056,7 +3149,7 @@ void ETSParser::ParseNamedSpecifiers(ArenaVector *specifiers, boo ir::Identifier *local = nullptr; imported->SetRange(Lexer()->GetToken().Loc()); - Lexer()->NextToken(); // eat import name + Lexer()->NextToken(); // eat import/export name if (CheckModuleAsModifier() && Lexer()->GetToken().Type() == lexer::TokenType::KEYW_AS) { Lexer()->NextToken(); // eat `as` literal @@ -3071,6 +3164,10 @@ void ETSParser::ParseNamedSpecifiers(ArenaVector *specifiers, boo util::Helpers::CheckImportedName(specifiers, specifier, file_name); + if (is_export) { + util::StringView member_name = local->Name(); + exported_idents.push_back(member_name); + } specifiers->push_back(specifier); if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COMMA) { @@ -3080,8 +3177,11 @@ void ETSParser::ParseNamedSpecifiers(ArenaVector *specifiers, boo Lexer()->NextToken(); // eat '}' - if (Lexer()->GetToken().KeywordType() != lexer::TokenType::KEYW_FROM && !is_re_export) { - ThrowSyntaxError("Unexpected token, expected 'from'"); + if (is_export && Lexer()->GetToken().KeywordType() != lexer::TokenType::KEYW_FROM) { + // update exported idents to export name map when it is not the case of re-export + for (auto member_name : exported_idents) { + export_name_map_.insert({member_name, start_loc}); + } } } @@ -3978,7 +4078,7 @@ ir::Expression *ETSParser::ParseNewExpression() ExpectToken(lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET); if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_SQUARE_BRACKET) { - auto *arr_instance = AllocNode(type_reference, dimension); + auto *arr_instance = AllocNode(Allocator(), type_reference, dimension); arr_instance->SetRange({start, end_loc}); return arr_instance; } @@ -4117,7 +4217,15 @@ ir::TSTypeParameter *ETSParser::ParseTypeParameter([[maybe_unused]] TypeAnnotati constraint = ParseTypeAnnotation(&new_options); } - auto *type_param = AllocNode(param_ident, constraint, nullptr, variance_modifier); + ir::TypeNode *default_type = nullptr; + + if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_SUBSTITUTION) { + Lexer()->NextToken(); // eat '=' + default_type = ParseTypeAnnotation(options); + } + + auto *type_param = AllocNode(param_ident, constraint, default_type, variance_modifier); + type_param->SetRange({start_loc, Lexer()->GetToken().End()}); return type_param; } diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 84d7503c257e61f4fe88858a33c9e13d17e5c58a..74c3294aca1df5b3f51e490be056d9fba4c05e45 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -99,6 +99,8 @@ private: bool has_decl; }; + std::map field_map_; + std::map export_name_map_; void ParseProgram(ScriptKind kind) override; [[nodiscard]] std::unique_ptr InitLexer(const SourceFile &source_file) override; void ParsePackageDeclaration(ArenaVector &statements); @@ -115,7 +117,8 @@ private: std::tuple GetSourceRegularPath(const std::string &path, const std::string &resolved_path); void ParseSources(const std::vector &paths, bool is_external = true); std::tuple> ParseFromClause(bool require_from); - void ParseNamedSpecifiers(ArenaVector *specifiers, bool is_re_export = false); + void ParseNamedSpecifiers(ArenaVector *specifiers, bool is_export = false); + void ParseNamedExportSpecifiers(ArenaVector *specifiers, bool default_export); void ParseUserSources(std::vector user_parths); std::vector ParseImportDeclarations(ArenaVector &statements); void ParseDefaultSources(); @@ -174,7 +177,8 @@ private: ir::MethodDefinition *CreateProxyConstructorDefinition(ir::MethodDefinition const *method); void AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition *method, ir::Identifier *ident_node = nullptr); static std::string PrimitiveTypeToName(ir::PrimitiveType type); - std::string GetNameForTypeNode(const ir::TypeNode *type_annotation) const; + std::string GetNameForTypeNode(const ir::TypeNode *type_annotation, bool adjust = true) const; + std::string GetNameForETSUnionType(const ir::TypeNode *type_annotation) const; ir::TSInterfaceDeclaration *ParseInterfaceBody(ir::Identifier *name, bool is_static); bool IsArrowFunctionExpressionStart(); ir::ArrowFunctionExpression *ParseArrowFunctionExpression(); @@ -189,7 +193,7 @@ private: ir::Expression *ParseCoverParenthesizedExpressionAndArrowParameterList() override; ir::Statement *ParseTryStatement() override; ir::DebuggerStatement *ParseDebuggerStatement() override; - void ParseReExport(lexer::SourcePosition start_loc); + void ParseExport(lexer::SourcePosition start_loc); ir::Statement *ParseImportDeclaration(StatementParsingFlags flags) override; ir::Statement *ParseExportDeclaration(StatementParsingFlags flags) override; ir::AnnotatedExpression *ParseVariableDeclaratorKey(VariableParsingFlags flags) override; @@ -248,6 +252,7 @@ private: ir::Statement *ParseInterfaceDeclaration(bool is_static) override; ir::ThisExpression *ParseThisExpression() override; + ir::TypeNode *ParseThisType(TypeAnnotationParsingOptions *options); ir::Statement *ParseFunctionStatement(StatementParsingFlags flags) override; std::tuple ParseClassImplementsElement() override; ir::TypeNode *ParseInterfaceExtendsElement() override; diff --git a/ets2panda/parser/TypedParser.cpp b/ets2panda/parser/TypedParser.cpp index 71905233a349545989089f8ffec3bb060070acd3..16acc71da59727a9e08975791619906f43d3a085 100644 --- a/ets2panda/parser/TypedParser.cpp +++ b/ets2panda/parser/TypedParser.cpp @@ -1196,7 +1196,10 @@ ir::TSTypeParameterInstantiation *TypedParser::ParseTypeParameterInstantiation(T Lexer()->NextToken(); // eat '<' while (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_GREATER_THAN) { + TypeAnnotationParsingOptions tmp = *options; + *options &= ~TypeAnnotationParsingOptions::IGNORE_FUNCTION_TYPE; ir::TypeNode *current_param = ParseTypeAnnotation(options); + *options = tmp; if (current_param == nullptr) { return nullptr; diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index 23e5848d7353d3de55f449f4b4e5893b05786bd9..84ab4a84a633cc8dbb6a0102f07c718c6f998cd8 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -1709,7 +1709,8 @@ extern "C" es2panda_AstNode *CreateNewArrayInstanceExpression(es2panda_Context * auto *ir_typeref = reinterpret_cast(type_reference)->AsExpression()->AsTypeNode(); auto *ir_dim = reinterpret_cast(dimension)->AsExpression(); - return reinterpret_cast(allocator->New(ir_typeref, ir_dim)); + return reinterpret_cast( + allocator->New(allocator, ir_typeref, ir_dim)); } extern "C" es2panda_AstNode *NewArrayInstanceExpressionTypeReference(es2panda_AstNode *ast) diff --git a/ets2panda/test/CMakeLists.txt b/ets2panda/test/CMakeLists.txt index 334456fb374fc875bb8e25e386e6c8e021a2d51d..a0ea52c83e1d561eca8eb9a387ba2da0e3cc0532 100644 --- a/ets2panda/test/CMakeLists.txt +++ b/ets2panda/test/CMakeLists.txt @@ -112,7 +112,7 @@ if(PANDA_WITH_ETS) SOURCES unit/public/ast_verifier_test.cpp LIBRARIES - es2panda-lib + es2panda-public es2panda-lib INCLUDE_DIRS ${ES2PANDA_PATH} ${ES2PANDA_BINARY_ROOT} @@ -133,7 +133,20 @@ if(PANDA_WITH_ETS) SANITIZERS ${PANDA_SANITIZERS_LIST} - ) + ) + # FIXME(igelhaus): stabilize fe_dev_202312 + #panda_add_gtest( + # NAME es2panda_checker_tests + # SOURCES + # unit/checker_test.cpp + # LIBRARIES + # es2panda-public es2panda-lib + # INCLUDE_DIRS + # ${ES2PANDA_ROOT} + # SANITIZERS + # ${PANDA_SANITIZERS_LIST} + #) + endif() panda_add_gtest( diff --git a/ets2panda/test/compiler/ets/boxingConversion1-expected.txt b/ets2panda/test/compiler/ets/boxingConversion1-expected.txt index 719b59bc2c0367542f1f78b18b2247709b7677ab..bf84cbaee199b297dbaec22105b8ab791df6c2b3 100644 --- a/ets2panda/test/compiler/ets/boxingConversion1-expected.txt +++ b/ets2panda/test/compiler/ets/boxingConversion1-expected.txt @@ -541,4 +541,4 @@ } } } -TypeError: No matching construct signature [boxingConversion1.ets:18:19] +TypeError: No matching construct signature for std.core.Byte(int) [boxingConversion1.ets:18:19] diff --git a/ets2panda/test/compiler/ets/boxingConversion4-expected.txt b/ets2panda/test/compiler/ets/boxingConversion4-expected.txt index a7ce34893a08a41e48e0bc89015057ce9c5017c6..f102d6aa27c85fb79d5b97699a0f41fad8aa433a 100644 --- a/ets2panda/test/compiler/ets/boxingConversion4-expected.txt +++ b/ets2panda/test/compiler/ets/boxingConversion4-expected.txt @@ -965,4 +965,4 @@ } } } -TypeError: No matching construct signature [boxingConversion4.ets:19:20] +TypeError: No matching construct signature for std.core.Short(int) [boxingConversion4.ets:19:20] diff --git a/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt b/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt index 4769cb130a3e19312293236b350d5ee01f41fd1c..426576fd85849a298c4f58b43c1357fa07867b15 100644 --- a/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt +++ b/ets2panda/test/compiler/ets/generic_arrayaslist-expected.txt @@ -10900,67 +10900,121 @@ "callee": { "type": "MemberExpression", "object": { - "type": "MemberExpression", - "object": { + "type": "TSAsExpression", + "expression": { "type": "MemberExpression", "object": { - "type": "ThisExpression", + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 132, + "column": 18 + }, + "end": { + "line": 132, + "column": 22 + } + } + }, + "property": { + "type": "Identifier", + "name": "data", + "decorators": [], + "loc": { + "start": { + "line": 132, + "column": 23 + }, + "end": { + "line": 132, + "column": 27 + } + } + }, + "computed": false, + "optional": false, "loc": { "start": { "line": 132, - "column": 17 + "column": 18 }, "end": { "line": 132, - "column": 21 + "column": 27 } } }, "property": { "type": "Identifier", - "name": "data", + "name": "i", "decorators": [], "loc": { "start": { "line": 132, - "column": 22 + "column": 28 }, "end": { "line": 132, - "column": 26 + "column": 29 } } }, - "computed": false, + "computed": true, "optional": false, "loc": { "start": { "line": 132, - "column": 17 + "column": 18 }, "end": { "line": 132, - "column": 26 + "column": 30 } } }, - "property": { - "type": "Identifier", - "name": "i", - "decorators": [], + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 132, + "column": 34 + }, + "end": { + "line": 132, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 132, + "column": 34 + }, + "end": { + "line": 132, + "column": 41 + } + } + }, "loc": { "start": { "line": 132, - "column": 27 + "column": 34 }, "end": { "line": 132, - "column": 28 + "column": 41 } } }, - "computed": true, - "optional": false, "loc": { "start": { "line": 132, @@ -10968,7 +11022,7 @@ }, "end": { "line": 132, - "column": 29 + "column": 41 } } }, @@ -10979,11 +11033,11 @@ "loc": { "start": { "line": 132, - "column": 30 + "column": 42 }, "end": { "line": 132, - "column": 36 + "column": 48 } } }, @@ -10996,23 +11050,77 @@ }, "end": { "line": 132, - "column": 36 + "column": 48 } } }, "arguments": [ { - "type": "Identifier", - "name": "e", - "decorators": [], + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "e", + "decorators": [], + "loc": { + "start": { + "line": 132, + "column": 49 + }, + "end": { + "line": 132, + "column": 50 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 132, + "column": 54 + }, + "end": { + "line": 132, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 132, + "column": 54 + }, + "end": { + "line": 132, + "column": 61 + } + } + }, + "loc": { + "start": { + "line": 132, + "column": 54 + }, + "end": { + "line": 132, + "column": 61 + } + } + }, "loc": { "start": { "line": 132, - "column": 37 + "column": 49 }, "end": { "line": 132, - "column": 38 + "column": 50 } } } @@ -11025,7 +11133,7 @@ }, "end": { "line": 132, - "column": 39 + "column": 61 } } }, @@ -11063,7 +11171,7 @@ "loc": { "start": { "line": 132, - "column": 41 + "column": 63 }, "end": { "line": 134, diff --git a/ets2panda/test/compiler/ets/generic_arrayaslist.ets b/ets2panda/test/compiler/ets/generic_arrayaslist.ets index 120673db7fd91dfe9c637536ce692b70b0130bf6..9c280119525a3d907209ed4026aaa33a863835c2 100644 --- a/ets2panda/test/compiler/ets/generic_arrayaslist.ets +++ b/ets2panda/test/compiler/ets/generic_arrayaslist.ets @@ -129,7 +129,7 @@ class ArrayAsListt implements Listt { assert false: "Not implemented: internal issue with calling equals"; for (let i = 0; i < this.curSize; ++i) { - if (this.data[i].equals(e)) { + if ((this.data[i] as Object).equals(e as Object)) { return true; } } diff --git a/ets2panda/test/compiler/ets/generic_typealias_1-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..8e27fca50e52a27a436916192b7d203d452ff282 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_1-expected.txt @@ -0,0 +1,2841 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "my_int", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "a_t", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "L", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 11 + } + } + }, + "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": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + { + "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": 22, + "column": 10 + }, + "end": { + "line": 22, + "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": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "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": "Int", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 12 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 12 + }, + "end": { + "line": 23, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 12 + }, + "end": { + "line": 23, + "column": 17 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 22 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 22 + }, + "end": { + "line": 23, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 22 + }, + "end": { + "line": 23, + "column": 35 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 38 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 13 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 28 + }, + "end": { + "line": 24, + "column": 29 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 30 + }, + "end": { + "line": 24, + "column": 37 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 46 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 37 + }, + "end": { + "line": 24, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 30 + }, + "end": { + "line": 24, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 30 + }, + "end": { + "line": 24, + "column": 46 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 29 + }, + "end": { + "line": 24, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 28 + }, + "end": { + "line": 24, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 28 + }, + "end": { + "line": 24, + "column": 47 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 49 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 49 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "c", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 21 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Long", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 22 + }, + "end": { + "line": 25, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 22 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 22 + }, + "end": { + "line": 25, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 21 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 13 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 30 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 35 + }, + "end": { + "line": 25, + "column": 36 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Long", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 42 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 36 + }, + "end": { + "line": 25, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 35 + }, + "end": { + "line": 25, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 35 + }, + "end": { + "line": 25, + "column": 43 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 25, + "column": 31 + }, + "end": { + "line": 25, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 45 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 45 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "d", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 26, + "column": 21 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 26, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 26, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 34 + }, + "end": { + "line": 26, + "column": 35 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 36 + }, + "end": { + "line": 26, + "column": 43 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 44 + }, + "end": { + "line": 26, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 44 + }, + "end": { + "line": 26, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 44 + }, + "end": { + "line": 26, + "column": 49 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 43 + }, + "end": { + "line": 26, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 36 + }, + "end": { + "line": 26, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 36 + }, + "end": { + "line": 26, + "column": 49 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 35 + }, + "end": { + "line": 26, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 34 + }, + "end": { + "line": 26, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 34 + }, + "end": { + "line": 26, + "column": 50 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 26, + "column": 30 + }, + "end": { + "line": 26, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 26, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 52 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "e", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "a_t", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 15 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 16 + }, + "end": { + "line": 28, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 16 + }, + "end": { + "line": 28, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 16 + }, + "end": { + "line": 28, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 15 + }, + "end": { + "line": 28, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 22 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 10 + } + } + }, + "init": { + "type": "Identifier", + "name": "d", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 23 + }, + "end": { + "line": 28, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 24 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 25 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "f", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "a_t", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 15 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 15 + }, + "end": { + "line": 29, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 10 + } + } + }, + "init": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 27 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 28 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "g", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "a_t", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 12 + }, + "end": { + "line": 30, + "column": 15 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 16 + }, + "end": { + "line": 30, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 24 + }, + "end": { + "line": 30, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 24 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 24 + }, + "end": { + "line": 30, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 23 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 16 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 16 + }, + "end": { + "line": 30, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 15 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 12 + }, + "end": { + "line": 30, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 12 + }, + "end": { + "line": 30, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 10 + } + } + }, + "init": { + "type": "Identifier", + "name": "d", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 32 + }, + "end": { + "line": 30, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 33 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 34 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "h", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "a_t", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 15 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 16 + }, + "end": { + "line": 31, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "my_int", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 23 + }, + "end": { + "line": 31, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 16 + }, + "end": { + "line": 31, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 16 + }, + "end": { + "line": 31, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 15 + }, + "end": { + "line": 31, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 34 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 39 + }, + "end": { + "line": 31, + "column": 40 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 41 + }, + "end": { + "line": 31, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 41 + }, + "end": { + "line": 31, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 41 + }, + "end": { + "line": 31, + "column": 45 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 40 + }, + "end": { + "line": 31, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 39 + }, + "end": { + "line": 31, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 39 + }, + "end": { + "line": 31, + "column": 46 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 31, + "column": 35 + }, + "end": { + "line": 31, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 48 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 48 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 23 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 32, + "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": 33, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_1.ets b/ets2panda/test/compiler/ets/generic_typealias_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..45814fb186b0409ceb63a99d8e102d0a905f1a9e --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_1.ets @@ -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. + */ + +type my_int = Int +type my_type = T; +type a_t = A; + +class A{} + +function main(): void { + let a: Int = new my_type(2); + let b: A = new A>(); + let c: A> = new A(); + let d: A> = new A>(); + + let e: a_t = d; + let f: a_t = b; + let g: a_t> = d; + let h: a_t> = new A(); +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_10_neg-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_10_neg-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0c876797aca26d92c50ffd8ff849b075cb236d3 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_10_neg-expected.txt @@ -0,0 +1,266 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 17, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 17, + "column": 1 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 1 + } + } + }, + { + "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 + } + } + } + ], + "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: Type alias generic parameter 'T' is not used in type annotation [generic_typealias_10_neg.ets:16:14] diff --git a/ets2panda/test/compiler/ets/generic_typealias_10_neg.ets b/ets2panda/test/compiler/ets/generic_typealias_10_neg.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6d36ac9565379b55f4d4bdb777fd21495438deb --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_10_neg.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +type my_type = Int diff --git a/ets2panda/test/compiler/ets/generic_typealias_2_neg-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_2_neg-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b62ce00490f13c3191c860dfeb858d221c3a3011 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_2_neg-expected.txt @@ -0,0 +1,542 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + { + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "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": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 32 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Type alias declaration is generic, but no type parameters were provided [generic_typealias_2_neg.ets:20:12] diff --git a/ets2panda/test/compiler/ets/generic_typealias_2_neg.ets b/ets2panda/test/compiler/ets/generic_typealias_2_neg.ets new file mode 100644 index 0000000000000000000000000000000000000000..0bf9c13181b1d5570282e1cdf2ee34aaf6801a1e --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_2_neg.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +type my_type = T; + + +function main(): void { + let a: my_type = new Int(); +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_3_neg-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_3_neg-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b02e59012d8634d2bfb18179f19afa773259a016 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_3_neg-expected.txt @@ -0,0 +1,555 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + { + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "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": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 19 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 34 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 34 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 34 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 30 + }, + "end": { + "line": 20, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Type alias declaration is not generic, but type parameters were provided [generic_typealias_3_neg.ets:20:19] diff --git a/ets2panda/test/compiler/ets/generic_typealias_3_neg.ets b/ets2panda/test/compiler/ets/generic_typealias_3_neg.ets new file mode 100644 index 0000000000000000000000000000000000000000..fb71ae4dbb8956f47cf9ff1787b4f993f687e4ba --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_3_neg.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +type my_type = Int; + + +function main(): void { + let a: my_type = new Int(); +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_4_neg-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_4_neg-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9be51ba2887128e736de285cfb0145b36ecd2ec9 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_4_neg-expected.txt @@ -0,0 +1,639 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + { + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "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": "my_type", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 19 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 43 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 35 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 45 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 45 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Wrong number of type parameters for generic type alias [generic_typealias_4_neg.ets:20:19] diff --git a/ets2panda/test/compiler/ets/generic_typealias_4_neg.ets b/ets2panda/test/compiler/ets/generic_typealias_4_neg.ets new file mode 100644 index 0000000000000000000000000000000000000000..3204e76a7d051ae296772a32af179eaa84942b20 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_4_neg.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +type my_type = T; + + +function main(): void { + let a: my_type = new Int(); +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_5_neg-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_5_neg-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3d97f850663b3a0eed891d3aa696a2bdf0b30373 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_5_neg-expected.txt @@ -0,0 +1,1014 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "constraint": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Comparable", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "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": 16, + "column": 36 + }, + "end": { + "line": 16, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "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": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "type_a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + { + "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": 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": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "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": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "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": 17 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "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": 24, + "column": 1 + } + } +} +TypeError: Type 'B' is not assignable to constraint type 'Comparable'. [generic_typealias_5_neg.ets:22:14] diff --git a/ets2panda/test/compiler/ets/generic_typealias_5_neg.ets b/ets2panda/test/compiler/ets/generic_typealias_5_neg.ets new file mode 100644 index 0000000000000000000000000000000000000000..8089b4be09c897a465507c35fae8a15436930c2b --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_5_neg.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +class A> {} +class B {} + +type type_a = A; + +function main(): void { + let a: A; +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_6-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_6-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..116b3cb4838b497c6bf4dd76b0d8390437de18a9 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_6-expected.txt @@ -0,0 +1,876 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "constraint": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Comparable", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "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": 16, + "column": 36 + }, + "end": { + "line": 16, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "type_a", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + { + "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": 20, + "column": 10 + }, + "end": { + "line": 20, + "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": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "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": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 21, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 22, + "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": 23, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_6.ets b/ets2panda/test/compiler/ets/generic_typealias_6.ets new file mode 100644 index 0000000000000000000000000000000000000000..9d2beaf4c65d5846f04d76b677cebdc3819db9b9 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_6.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +class A> {} + +type type_a = A; + +function main(): void { + let a: A; +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_7_neg-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_7_neg-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..cb253be2b733cdd791e0636924eaffda9871d709 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_7_neg-expected.txt @@ -0,0 +1,528 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "type_a", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 6 + }, + "end": { + "line": 15, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "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": 12 + }, + "end": { + "line": 15, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 1 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + { + "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": 17, + "column": 10 + }, + "end": { + "line": 17, + "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": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "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": "type_a", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 27 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 20, + "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": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_7_neg.ets b/ets2panda/test/compiler/ets/generic_typealias_7_neg.ets new file mode 100644 index 0000000000000000000000000000000000000000..1c22ec93dc3401251f0ee129314d893946c35b48 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_7_neg.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ +type type_a = T; + +function main(): void { + let a: type_a = 2; + // NOTE (martin): Primitive type parameter isn't working without initialization, it'll be in a follow up fix +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_8-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_8-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..c91e3d2dd93bda4b7202072176519bf453412979 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_8-expected.txt @@ -0,0 +1,790 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "type_a", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 6 + }, + "end": { + "line": 15, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 18 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "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": 12 + }, + "end": { + "line": 15, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 1 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "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": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "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": "type_a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 25 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 25 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 25 + }, + "end": { + "line": 19, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 19, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 38 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 38 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "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": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_8.ets b/ets2panda/test/compiler/ets/generic_typealias_8.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f16a64e36087d3efa21c59bf02bf6334f49a2af --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_8.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ +type type_a = T; +class B {} + +function main(): void { + let a: type_a = new B(); +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_9-expected.txt b/ets2panda/test/compiler/ets/generic_typealias_9-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..12338275bc22d8a74203fb7e584897e31d0a6ff7 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_9-expected.txt @@ -0,0 +1,754 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "AnimationRange", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 6 + }, + "end": { + "line": 15, + "column": 20 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 15, + "column": 38 + }, + "end": { + "line": 15, + "column": 43 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 31 + }, + "end": { + "line": 15, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 31 + }, + "end": { + "line": 15, + "column": 43 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Value", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 48 + }, + "end": { + "line": 15, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 48 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 48 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 30 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "Value", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 21 + }, + "end": { + "line": 15, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 21 + }, + "end": { + "line": 15, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 15, + "column": 20 + }, + "end": { + "line": 15, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 1 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + { + "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": 17, + "column": 10 + }, + "end": { + "line": 17, + "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": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "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": "AnimationRange", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "val", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 45 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 45 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 48 + }, + "end": { + "line": 18, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 48 + }, + "end": { + "line": 18, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 48 + }, + "end": { + "line": 18, + "column": 54 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 18, + "column": 64 + }, + "end": { + "line": 18, + "column": 65 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 57 + }, + "end": { + "line": 18, + "column": 66 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 55 + }, + "end": { + "line": 18, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 18, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 18, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 67 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 68 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "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": 20, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_typealias_9.ets b/ets2panda/test/compiler/ets/generic_typealias_9.ets new file mode 100644 index 0000000000000000000000000000000000000000..3e8359c64d737ca68616c6c2053a462be48620cb --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_typealias_9.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ +type AnimationRange = (value: float) => Value + +function main(): void { + let a: AnimationRange = (val: float): Int => { return 2;}; +} diff --git a/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt b/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt index 642f8dc36d5fffe6d8748b7e9a1c46839a84700c..2191f1795174ae88c39a85a4f00767a6576c0aff 100644 --- a/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt +++ b/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt @@ -1082,4 +1082,4 @@ } } } -TypeError: Type parameter already instantiated with another type [generics_implicit_lambda2.ets:16:17] +TypeError: No matching call signature [generics_implicit_lambda2.ets:19:5] diff --git a/ets2panda/test/compiler/ets/generics_instantiation_3-expected.txt b/ets2panda/test/compiler/ets/generics_instantiation_3-expected.txt index 7aee3903115cb2f559b552a5dc20f616270cdc33..7e2cc15cd5c677cd66d232c6269f98681c1f245c 100644 --- a/ets2panda/test/compiler/ets/generics_instantiation_3-expected.txt +++ b/ets2panda/test/compiler/ets/generics_instantiation_3-expected.txt @@ -39,6 +39,47 @@ } } }, + "constraint": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, "loc": { "start": { "line": 16, @@ -46,7 +87,7 @@ }, "end": { "line": 16, - "column": 11 + "column": 26 } } } @@ -58,7 +99,7 @@ }, "end": { "line": 16, - "column": 11 + "column": 26 } } }, @@ -604,7 +645,7 @@ "loc": { "start": { "line": 16, - "column": 12 + "column": 27 }, "end": { "line": 22, diff --git a/ets2panda/test/compiler/ets/generics_instantiation_3.ets b/ets2panda/test/compiler/ets/generics_instantiation_3.ets index 248620d3b0b214acfc92d849f5c0e1f99c4233bc..53882858ed096484350996e09548987ecfc3d777 100644 --- a/ets2panda/test/compiler/ets/generics_instantiation_3.ets +++ b/ets2panda/test/compiler/ets/generics_instantiation_3.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -class A { +class A { public foo(e: T): void { this.data[2].equals(e); } diff --git a/ets2panda/test/compiler/ets/lambda_capturing-expected.txt b/ets2panda/test/compiler/ets/lambda_capturing-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3e3bf773a69b0cefd6e775f6a0f4ab9ad872af7e --- /dev/null +++ b/ets2panda/test/compiler/ets/lambda_capturing-expected.txt @@ -0,0 +1,614 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "EasingCurve", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 40 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 45 + }, + "end": { + "line": 16, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + { + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "count", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 24 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "EasingCurve", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 40 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 25 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "*=", + "left": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "right": { + "type": "Identifier", + "name": "count", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 23, + "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": 24, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/lambda_capturing.ets b/ets2panda/test/compiler/ets/lambda_capturing.ets new file mode 100644 index 0000000000000000000000000000000000000000..70903eee6ef5d2eaf819fdd9cdca6a5c4199ae13 --- /dev/null +++ b/ets2panda/test/compiler/ets/lambda_capturing.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +export type EasingCurve = (value: float) => float + +function foo(count: int): EasingCurve { + return (value: float) => { + value *= count + return value + } +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_narrowing-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_narrowing-expected.txt similarity index 77% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_narrowing-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_narrowing-expected.txt index 5c23000dde2ceae8d7fde089a980a768b0825d36..4006b73d23c2fc962c6168d122041cf8d1d1ee86 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_narrowing-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_narrowing-expected.txt @@ -512,7 +512,7 @@ }, "end": { "line": 23, - "column": 6 + "column": 12 } } }, @@ -523,7 +523,7 @@ }, "end": { "line": 23, - "column": 6 + "column": 12 } } }, @@ -534,7 +534,7 @@ }, "end": { "line": 23, - "column": 6 + "column": 12 } } }, @@ -572,6 +572,190 @@ "column": 39 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 31 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "init": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 39 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 40 + } + } } ], "loc": { @@ -580,7 +764,7 @@ "column": 13 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -591,7 +775,7 @@ "column": 10 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -602,7 +786,7 @@ "column": 10 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -615,7 +799,7 @@ "column": 5 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -704,11 +888,11 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 2 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -720,7 +904,7 @@ "column": 19 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -731,7 +915,7 @@ "column": 1 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -881,7 +1065,7 @@ "column": 1 }, "end": { - "line": 25, + "line": 26, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_narrowing.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_narrowing.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_narrowing.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_narrowing.ets index 3deadfbc310ed24c5dde93267f95cf8f9992af61..d30c88298520a3a48e70f32e73b8d41a8dcd105a 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_narrowing.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_narrowing.ets @@ -20,5 +20,6 @@ class A { class B extends A { main () { let a = (a : A) => { return a} as (a : B) => B + let expected : (a : B) => B = a } } diff --git a/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_void-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_void-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..66623064bb646deeb7db12e1adc3ededdeeb58e6 --- /dev/null +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_void-expected.txt @@ -0,0 +1,519 @@ +{ + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "TSAsExpression", + "expression": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 21 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "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": 20, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_void.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_void.ets similarity index 95% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_void.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_void.ets index cfdec9acf17678d9e1d3f4608f89bff636f6fc1c..40ba385a39ecb2d1f029d4576be571f6df0de1ca 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_void.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_void.ets @@ -15,4 +15,5 @@ function main () { let x = () => {} as ()=> void + let expected : () => void = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_widening-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_widening-expected.txt similarity index 77% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_widening-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_widening-expected.txt index 46895a7994e966a0169e6bdc10e746f8b932cc35..545b1715cf2905525d1f37c516f466f808038e42 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_widening-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_widening-expected.txt @@ -512,7 +512,7 @@ }, "end": { "line": 23, - "column": 6 + "column": 12 } } }, @@ -523,7 +523,7 @@ }, "end": { "line": 23, - "column": 6 + "column": 12 } } }, @@ -534,7 +534,7 @@ }, "end": { "line": 23, - "column": 6 + "column": 12 } } }, @@ -572,6 +572,190 @@ "column": 39 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 29 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 31 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "init": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 39 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 40 + } + } } ], "loc": { @@ -580,7 +764,7 @@ "column": 13 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -591,7 +775,7 @@ "column": 10 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -602,7 +786,7 @@ "column": 10 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -615,7 +799,7 @@ "column": 5 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -704,11 +888,11 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 2 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -720,7 +904,7 @@ "column": 19 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -731,7 +915,7 @@ "column": 1 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -881,7 +1065,7 @@ "column": 1 }, "end": { - "line": 25, + "line": 26, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_widening.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_widening.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_widening.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_widening.ets index 3c904151bc307517f7d1af8d55a59f66dde54abf..cb90f223b65492d77d5f230dba1fa2e5a2097eaf 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_widening.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_infer_type_widening.ets @@ -20,5 +20,6 @@ class A { class B extends A { main () { let a = (a : B) => { return a} as (a : A) => A + let expected : (a : A) => A = a } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_type_has_pramas-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_type_has_pramas-expected.txt similarity index 71% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_type_has_pramas-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_type_has_pramas-expected.txt index f5d88208698db187d7e9f45b434bfc2e27464dcb..0d17b84dd6da9b8564c36814a05327b557e43f34 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_type_has_pramas-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_type_has_pramas-expected.txt @@ -428,7 +428,7 @@ }, "end": { "line": 18, - "column": 2 + "column": 8 } } }, @@ -439,7 +439,7 @@ }, "end": { "line": 18, - "column": 2 + "column": 8 } } }, @@ -450,7 +450,7 @@ }, "end": { "line": 18, - "column": 2 + "column": 8 } } }, @@ -488,6 +488,190 @@ "column": 38 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 29 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 40 + } + } } ], "loc": { @@ -496,7 +680,7 @@ "column": 18 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -507,7 +691,7 @@ "column": 15 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -518,7 +702,7 @@ "column": 15 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -531,7 +715,7 @@ "column": 1 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -566,7 +750,7 @@ "column": 1 }, "end": { - "line": 19, + "line": 20, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_type_has_pramas.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_type_has_pramas.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_type_has_pramas.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_type_has_pramas.ets index 54b4f3e7e11f1fce4ebfd32a96efb28b4d778b70..5654723617c63afb50451604bd9bf5acd239181c 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_type_has_pramas.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_cast_type_has_pramas.ets @@ -15,4 +15,5 @@ function main () { let x = (a : Int) => { return 1 } as (a : Int) => Int + let expected : (a : Int) => Int = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_void-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type-expected.txt similarity index 74% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_void-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type-expected.txt index 1ca55a254bbfd9f015ecc400bebfde75a6a84c53..1fa2395cb8c0a1432a31b81fdcd6115be6fe23d1 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_cast_infer_type_void-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type-expected.txt @@ -185,34 +185,21 @@ } }, "init": { - "type": "TSAsExpression", - "expression": { - "type": "ArrowFunctionExpression", - "function": { - "type": "ScriptFunction", - "id": null, - "generator": false, - "async": false, - "expression": false, - "params": [], - "body": { - "type": "BlockStatement", - "statements": [], - "loc": { - "start": { - "line": 17, - "column": 19 - }, - "end": { - "line": 17, - "column": 21 - } - } - }, + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], "loc": { "start": { "line": 17, - "column": 13 + "column": 19 }, "end": { "line": 17, @@ -231,6 +218,49 @@ } } }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 21 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", "typeAnnotation": { "type": "ETSFunctionType", "params": [], @@ -244,67 +274,83 @@ "decorators": [], "loc": { "start": { - "line": 17, - "column": 30 + "line": 18, + "column": 26 }, "end": { - "line": 17, - "column": 34 + "line": 18, + "column": 30 } } }, "loc": { "start": { - "line": 17, - "column": 30 + "line": 18, + "column": 26 }, "end": { "line": 18, - "column": 2 + "column": 32 } } }, "loc": { "start": { - "line": 17, - "column": 30 + "line": 18, + "column": 26 }, "end": { "line": 18, - "column": 2 + "column": 32 } } }, "loc": { "start": { - "line": 17, - "column": 25 + "line": 18, + "column": 20 }, "end": { "line": 18, - "column": 2 + "column": 32 } } }, + "decorators": [], "loc": { "start": { - "line": 17, - "column": 13 + "line": 18, + "column": 9 }, "end": { - "line": 17, - "column": 21 + "line": 18, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 34 } } }, "loc": { "start": { - "line": 17, + "line": 18, "column": 9 }, "end": { - "line": 17, - "column": 21 + "line": 18, + "column": 34 } } } @@ -312,12 +358,12 @@ "kind": "let", "loc": { "start": { - "line": 17, + "line": 18, "column": 5 }, "end": { - "line": 17, - "column": 21 + "line": 18, + "column": 34 } } } @@ -325,10 +371,10 @@ "loc": { "start": { "line": 16, - "column": 18 + "column": 17 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -336,10 +382,10 @@ "loc": { "start": { "line": 16, - "column": 15 + "column": 14 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -347,10 +393,10 @@ "loc": { "start": { "line": 16, - "column": 15 + "column": 14 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -363,7 +409,7 @@ "column": 1 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -398,7 +444,7 @@ "column": 1 }, "end": { - "line": 19, + "line": 20, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type.ets similarity index 95% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type.ets index 2b174aac2adc1adb0a2f61194067991694f422a6..c3e1e2354607a6d556914fe00b1bd99c7f06a529 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type.ets @@ -15,4 +15,5 @@ function main() { let x = () => {} + let expected : () => void = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression-expected.txt similarity index 75% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression-expected.txt index 5805f98ba6bb070ddcba6641bae8bec7dda14e03..1037cfbeb4b47a22c97effb363054b702da265c6 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression-expected.txt @@ -155,7 +155,7 @@ }, "end": { "line": 19, - "column": 6 + "column": 12 } } }, @@ -189,7 +189,7 @@ }, "end": { "line": 19, - "column": 6 + "column": 12 } } }, @@ -200,7 +200,7 @@ }, "end": { "line": 19, - "column": 6 + "column": 12 } } }, @@ -211,7 +211,7 @@ }, "end": { "line": 19, - "column": 6 + "column": 12 } } } @@ -224,7 +224,121 @@ }, "end": { "line": 19, - "column": 6 + "column": 12 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 35 } } } @@ -235,7 +349,7 @@ "column": 14 }, "end": { - "line": 19, + "line": 20, "column": 6 } } @@ -246,7 +360,7 @@ "column": 11 }, "end": { - "line": 19, + "line": 20, "column": 6 } } @@ -257,7 +371,7 @@ "column": 11 }, "end": { - "line": 19, + "line": 20, "column": 6 } } @@ -270,7 +384,7 @@ "column": 5 }, "end": { - "line": 19, + "line": 20, "column": 6 } } @@ -359,11 +473,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 2 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -375,7 +489,7 @@ "column": 9 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -386,7 +500,7 @@ "column": 1 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -536,7 +650,7 @@ "column": 1 }, "end": { - "line": 21, + "line": 22, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression.ets similarity index 95% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression.ets index f7f3f8866d33b8364e1f156775175755526aa20e..8b0adf283912e0d4d3f114f8d0e16b76c6d00a7e 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression.ets @@ -16,5 +16,6 @@ class A { main1 () { let x = () => new A() + let expected : () => A = x } } diff --git a/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..654f361bbd2f21d360154d92daed04a95e5bb794 --- /dev/null +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal-expected.txt @@ -0,0 +1,616 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main1", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main1", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": true, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "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": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 24 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 30 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 36 + }, + "end": { + "line": 19, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 37 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "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": 21, + "column": 2 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 21, + "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 + } + } + } + ], + "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": 22, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal.ets index f25216490d7c354985c00e157dc6fa503af74b0f..d6350269022ffca570a8a2addd12198989fd9c6b 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal.ets @@ -16,5 +16,6 @@ class A { main1 () { let x = () => 1 + let expected : () => Int = x } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_has_return-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_has_return-expected.txt similarity index 80% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_has_return-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_has_return-expected.txt index b307e9a56ae544c67daa71437bf56791514c51be..13636ecffccfc2d8fc319a7a24c9509ee6cf3ff6 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_has_return-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_has_return-expected.txt @@ -82,11 +82,11 @@ "loc": { "start": { "line": 18, - "column": 9 + "column": 13 }, "end": { "line": 18, - "column": 10 + "column": 14 } } }, @@ -274,7 +274,7 @@ "loc": { "start": { "line": 18, - "column": 17 + "column": 21 }, "end": { "line": 21, @@ -285,7 +285,7 @@ "loc": { "start": { "line": 18, - "column": 13 + "column": 17 }, "end": { "line": 21, @@ -296,7 +296,7 @@ "loc": { "start": { "line": 18, - "column": 13 + "column": 17 }, "end": { "line": 21, @@ -307,7 +307,7 @@ "loc": { "start": { "line": 18, - "column": 9 + "column": 13 }, "end": { "line": 21, @@ -320,13 +320,127 @@ "loc": { "start": { "line": 18, - "column": 5 + "column": 9 }, "end": { "line": 21, "column": 10 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 34 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 13 + }, + "end": { + "line": 22, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 35 + } + } } ], "loc": { @@ -335,7 +449,7 @@ "column": 13 }, "end": { - "line": 22, + "line": 23, "column": 6 } } @@ -346,7 +460,7 @@ "column": 10 }, "end": { - "line": 22, + "line": 23, "column": 6 } } @@ -357,7 +471,7 @@ "column": 10 }, "end": { - "line": 22, + "line": 23, "column": 6 } } @@ -370,7 +484,7 @@ "column": 5 }, "end": { - "line": 22, + "line": 23, "column": 6 } } @@ -459,11 +573,11 @@ "decorators": [], "loc": { "start": { - "line": 23, + "line": 24, "column": 2 }, "end": { - "line": 23, + "line": 24, "column": 2 } } @@ -475,7 +589,7 @@ "column": 9 }, "end": { - "line": 23, + "line": 24, "column": 2 } } @@ -486,7 +600,7 @@ "column": 1 }, "end": { - "line": 23, + "line": 24, "column": 2 } } @@ -636,7 +750,7 @@ "column": 1 }, "end": { - "line": 24, + "line": 25, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_has_return.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_has_return.ets similarity index 92% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_has_return.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_has_return.ets index 357e2c31bf9213f59096826caa7d60d629228c3c..4cb018cc8d81d8d63357c51871df0e1192edefe3 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_has_return.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_has_return.ets @@ -15,9 +15,10 @@ class A { main () { - let x = ()=>{ + let x = ()=>{ let a : A = new A() return a } + let expected : () => A = x } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt similarity index 100% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param1.ets similarity index 100% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param1.ets diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt similarity index 100% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param2.ets similarity index 100% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_param2.ets diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_retrun_enum-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_retrun_enum-expected.txt similarity index 80% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_retrun_enum-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_retrun_enum-expected.txt index 001c89166709ca0199a6987815ccd3745fe118c4..ae8e7882a3ae83b53ad296853d9f6675ff890a4c 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_retrun_enum-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_retrun_enum-expected.txt @@ -526,6 +526,120 @@ "column": 6 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Color", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 34 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 35 + } + } } ], "loc": { @@ -534,7 +648,7 @@ "column": 18 }, "end": { - "line": 22, + "line": 23, "column": 2 } } @@ -545,7 +659,7 @@ "column": 15 }, "end": { - "line": 22, + "line": 23, "column": 2 } } @@ -556,7 +670,7 @@ "column": 15 }, "end": { - "line": 22, + "line": 23, "column": 2 } } @@ -569,7 +683,7 @@ "column": 1 }, "end": { - "line": 22, + "line": 23, "column": 2 } } @@ -604,7 +718,7 @@ "column": 1 }, "end": { - "line": 23, + "line": 24, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_retrun_enum.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_retrun_enum.ets similarity index 95% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_retrun_enum.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_retrun_enum.ets index b2b1bf9c55ce7fbb8e8a5842171f1bc807008dd2..8ed8c6e8c7e743272353ebc53e5a1e6151609903 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_retrun_enum.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_retrun_enum.ets @@ -19,4 +19,5 @@ function main () { let y : Color = Color.Red return y } + let expected : () => Color = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_array-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_array-expected.txt similarity index 76% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_array-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_array-expected.txt index b4aa958eaf6f46a7d182c517de45a97863b94c6f..a333dff5f00ce6439187d32ea22f7ab848617bc8 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_array-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_array-expected.txt @@ -436,6 +436,133 @@ "column": 19 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 33 + }, + "end": { + "line": 19, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 34 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 35 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 36 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 3 + }, + "end": { + "line": 19, + "column": 36 + } + } } ], "loc": { @@ -444,7 +571,7 @@ "column": 18 }, "end": { - "line": 19, + "line": 20, "column": 2 } } @@ -455,7 +582,7 @@ "column": 15 }, "end": { - "line": 19, + "line": 20, "column": 2 } } @@ -466,7 +593,7 @@ "column": 15 }, "end": { - "line": 19, + "line": 20, "column": 2 } } @@ -479,7 +606,7 @@ "column": 1 }, "end": { - "line": 19, + "line": 20, "column": 2 } } @@ -514,7 +641,7 @@ "column": 1 }, "end": { - "line": 20, + "line": 21, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_array.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_array.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_array.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_array.ets index 4c471e8e9d26cb2ac28b6f72ee0a941d7154b4b2..c2a299ba2562d6ee296e6c57f12bfb5987586e4b 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_array.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_array.ets @@ -16,4 +16,5 @@ function main () { let y : number[] = [ 1 ,2 ,3 ] let x = () => y + let expected : () => number[] = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda-expected.txt similarity index 76% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda-expected.txt index f5e21f45ae662950345eb05247d21bdd38cdd142..4db8770ef4c58cb0aabb76003649bec672727976 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda-expected.txt @@ -399,6 +399,134 @@ } } }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 20 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 38 + }, + "end": { + "line": 23, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 39 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 39 + } + } + }, { "type": "ReturnStatement", "argument": { @@ -407,22 +535,22 @@ "decorators": [], "loc": { "start": { - "line": 23, + "line": 24, "column": 12 }, "end": { - "line": 23, + "line": 24, "column": 13 } } }, "loc": { "start": { - "line": 23, + "line": 24, "column": 5 }, "end": { - "line": 23, + "line": 24, "column": 13 } } @@ -434,7 +562,7 @@ "column": 17 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -445,7 +573,7 @@ "column": 14 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -456,7 +584,7 @@ "column": 14 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -469,7 +597,7 @@ "column": 1 }, "end": { - "line": 24, + "line": 25, "column": 2 } } @@ -504,7 +632,7 @@ "column": 1 }, "end": { - "line": 25, + "line": 26, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda.ets similarity index 93% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda.ets index 52a1a7cb138cbec6a2af1b815220662a7e785506..159e563745aa49ab594cac473cee4ffbd606792e 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda.ets @@ -20,5 +20,6 @@ function main() { } return y } - return x + let expected : () => () => Int = x + return x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda1-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda1-expected.txt similarity index 69% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda1-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda1-expected.txt index 487934f8aa2dfa4db059c89ccf76929dfaf820ee..9aef2f59fa456abcb376b8db9e612d4823ab7fd5 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda1-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda1-expected.txt @@ -311,6 +311,134 @@ "column": 27 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 40 + } + } } ], "loc": { @@ -319,7 +447,7 @@ "column": 17 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -330,7 +458,7 @@ "column": 14 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -341,7 +469,7 @@ "column": 14 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -354,7 +482,7 @@ "column": 1 }, "end": { - "line": 18, + "line": 19, "column": 2 } } @@ -389,7 +517,7 @@ "column": 1 }, "end": { - "line": 19, + "line": 20, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda1.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda1.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda1.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda1.ets index 0d36b599b47b3a722009da17386cb82ee12966e1..f0cb1b26b567a2ad2f588e6bc4c7c12734acc92c 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda1.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda1.ets @@ -15,4 +15,5 @@ function main() { let x = () => () => {} + let expected : () => () => void = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression-expected.txt similarity index 61% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression-expected.txt index 47d0a0294250d1ddf3f6929bd9f433d7e3b8c63e..2237eae3ae1026548f75c9359fbb11ddc2c4cabc 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression-expected.txt @@ -69,7 +69,80 @@ "params": [], "body": { "type": "BlockStatement", - "statements": [], + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "expected", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "right": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "main1", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 43 + } + } + } + ], "loc": { "start": { "line": 1, @@ -120,7 +193,7 @@ "type": "MethodDefinition", "key": { "type": "Identifier", - "name": "main", + "name": "main1", "decorators": [], "loc": { "start": { @@ -129,7 +202,7 @@ }, "end": { "line": 16, - "column": 14 + "column": 15 } } }, @@ -144,7 +217,7 @@ "type": "ScriptFunction", "id": { "type": "Identifier", - "name": "main", + "name": "main1", "decorators": [], "loc": { "start": { @@ -153,7 +226,7 @@ }, "end": { "line": 16, - "column": 14 + "column": 15 } } }, @@ -285,7 +358,7 @@ "loc": { "start": { "line": 16, - "column": 18 + "column": 19 }, "end": { "line": 22, @@ -296,7 +369,7 @@ "loc": { "start": { "line": 16, - "column": 15 + "column": 16 }, "end": { "line": 22, @@ -307,7 +380,7 @@ "loc": { "start": { "line": 16, - "column": 15 + "column": 16 }, "end": { "line": 22, @@ -327,6 +400,141 @@ "column": 2 } } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "expected", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "value": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "main1", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 22 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 43 + } + } } ], "loc": { @@ -358,7 +566,7 @@ "column": 1 }, "end": { - "line": 23, + "line": 24, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression.ets similarity index 91% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression.ets index 4c20b1a836ffc6cbae374b263b4a6fb16be560a6..8bcea1baa23d63e200a8909cd3704af851a7fd74 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_lambda_expression.ets @@ -13,10 +13,11 @@ * limitations under the License. */ -function main () { +function main1 () { return () => { return () => { } } } +let expected : () => () => void = main1 () diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_literal-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_literal-expected.txt similarity index 69% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_literal-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_literal-expected.txt index 0b1093dd094ea7cc22f19390c761814bd6b09152..84d4b4848292a682b19f803ab7071a66ab5ed25e 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_literal-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_literal-expected.txt @@ -280,6 +280,120 @@ "column": 6 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 32 + }, + "end": { + "line": 20, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 33 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 33 + } + } } ], "loc": { @@ -288,7 +402,7 @@ "column": 18 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -299,7 +413,7 @@ "column": 15 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -310,7 +424,7 @@ "column": 15 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -323,7 +437,7 @@ "column": 1 }, "end": { - "line": 20, + "line": 21, "column": 2 } } @@ -358,7 +472,7 @@ "column": 1 }, "end": { - "line": 21, + "line": 22, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_literal.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_literal.ets similarity index 95% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_literal.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_literal.ets index 368e82afbf677a85cb5a92c1f021921600d477b0..05e3bf890ca1c172bb2935e14f26573053ff3f9c 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_literal.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_literal.ets @@ -17,4 +17,5 @@ function main () { let x = () => { return 1 } + let expected : () => Int = x } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_union-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_union-expected.txt similarity index 72% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_union-expected.txt rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_union-expected.txt index bdbe6148f561ee33fe07e2f9928a5fce7d1d5a71..191ba526c8b13f49e8da175acde31cee783aa65d 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_union-expected.txt +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_union-expected.txt @@ -436,6 +436,176 @@ "column": 4 } } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "expected", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 24 + }, + "end": { + "line": 21, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 24 + }, + "end": { + "line": 21, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 24 + }, + "end": { + "line": 21, + "column": 29 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 30 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 30 + }, + "end": { + "line": 21, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 30 + }, + "end": { + "line": 21, + "column": 38 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 24 + }, + "end": { + "line": 21, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "init": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 39 + }, + "end": { + "line": 21, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 40 + } + } } ], "loc": { @@ -444,7 +614,7 @@ "column": 18 }, "end": { - "line": 21, + "line": 22, "column": 2 } } @@ -455,7 +625,7 @@ "column": 15 }, "end": { - "line": 21, + "line": 22, "column": 2 } } @@ -466,7 +636,7 @@ "column": 15 }, "end": { - "line": 21, + "line": 22, "column": 2 } } @@ -479,7 +649,7 @@ "column": 1 }, "end": { - "line": 21, + "line": 22, "column": 2 } } @@ -514,7 +684,7 @@ "column": 1 }, "end": { - "line": 22, + "line": 23, "column": 1 } } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_union.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_union.ets similarity index 94% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_union.ets rename to ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_union.ets index 3cd66a19d9b1f3fc8f94638e296768c460c2b910..6196e74e327774df0fd11bf4fdbcddf51f5600d1 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_return_union.ets +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_return_union.ets @@ -18,4 +18,5 @@ function main () { let y : Int | Double = 2.0 return y } + let expected : () => Int | Double = x } diff --git a/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_scope-expected.txt b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_scope-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..1034ff0fdf3ac0adaf2f09094330126ec92db18d --- /dev/null +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_scope-expected.txt @@ -0,0 +1,623 @@ +{ + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 6 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 6 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "f", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": true, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "right": { + "type": "Identifier", + "name": "p", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "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": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "f", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 16 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "second": null, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "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": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_scope.ets b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_scope.ets new file mode 100644 index 0000000000000000000000000000000000000000..dcc3b38705e5211d04d2f48e9edd9dc3a030ab51 --- /dev/null +++ b/ets2panda/test/compiler/ets/lambda_infer_type/lambda_infer_type_scope.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +let b = 1; +let f = (p: double) => b + p; +function main () { + assert f(42) == 43 +} diff --git a/ets2panda/test/compiler/ets/methodOverrideWithoutModifier-expected.txt b/ets2panda/test/compiler/ets/methodOverrideWithoutModifier-expected.txt index 6c3e10213229a8f567786349917c34ed17a97b67..ee9fd7f3da67951f1f8795e15728c725fe76a1e3 100644 --- a/ets2panda/test/compiler/ets/methodOverrideWithoutModifier-expected.txt +++ b/ets2panda/test/compiler/ets/methodOverrideWithoutModifier-expected.txt @@ -735,4 +735,3 @@ } } } -TypeError: Method overriding requires 'override' modifier [methodOverrideWithoutModifier.ets:23:15] diff --git a/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt b/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a2cc17f30653f96ce760087c29e046eab9d37826 --- /dev/null +++ b/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt @@ -0,0 +1,1015 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "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": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "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": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "abn", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 32 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 36 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 38 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "ab", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 18 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "init": { + "type": "Identifier", + "name": "abn", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 22 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 22, + "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": 23, + "column": 1 + } + } +} +TypeError: Initializers type is not assignable to the target type [n_assignGenericWithNullableTypeParamToNonNullable.ets:21:19] diff --git a/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable.ets b/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable.ets new file mode 100644 index 0000000000000000000000000000000000000000..8dab208be01f4e78883dae08af77e7f8022f906a --- /dev/null +++ b/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +class A {} +class B {} + +function main(): void { + let abn : A = new A(); // should work: non nullable B is the subtype of nullable B + let ab : A = abn; // should not work: nullable B (the type of abn) is not the subtype of non nullable B +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal-expected.txt b/ets2panda/test/compiler/ets/this_type_invalid_return_type-expected.txt similarity index 74% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal-expected.txt rename to ets2panda/test/compiler/ets/this_type_invalid_return_type-expected.txt index 707d202c3f17c1a42513086f8df8a7ebcf03f119..daebc746ddf8141a861d631b0b4bb5b366dcef89 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_arrow_expression_literal-expected.txt +++ b/ets2panda/test/compiler/ets/this_type_invalid_return_type-expected.txt @@ -26,7 +26,7 @@ "type": "MethodDefinition", "key": { "type": "Identifier", - "name": "main1", + "name": "foo", "decorators": [], "loc": { "start": { @@ -35,7 +35,7 @@ }, "end": { "line": 17, - "column": 10 + "column": 8 } } }, @@ -50,7 +50,7 @@ "type": "ScriptFunction", "id": { "type": "Identifier", - "name": "main1", + "name": "foo", "decorators": [], "loc": { "start": { @@ -59,7 +59,7 @@ }, "end": { "line": 17, - "column": 10 + "column": 8 } } }, @@ -67,115 +67,79 @@ "async": false, "expression": false, "params": [], + "returnType": { + "type": "TSThisType", + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, "body": { "type": "BlockStatement", "statements": [ { - "type": "VariableDeclaration", - "declarations": [ - { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "x", - "decorators": [], - "loc": { - "start": { - "line": 18, - "column": 13 - }, - "end": { - "line": 18, - "column": 14 - } - } - }, - "init": { - "type": "ArrowFunctionExpression", - "function": { - "type": "ScriptFunction", - "id": null, - "generator": false, - "async": false, - "expression": true, - "params": [], - "body": { - "type": "BlockStatement", - "statements": [ - { - "type": "ReturnStatement", - "argument": { - "type": "NumberLiteral", - "value": 1, - "loc": { - "start": { - "line": 18, - "column": 23 - }, - "end": { - "line": 18, - "column": 24 - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 1 - } - } - } - ], - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 1 - } - } - }, + "type": "ReturnStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], "loc": { "start": { "line": 18, - "column": 17 + "column": 20 }, "end": { "line": 18, - "column": 24 + "column": 21 } } }, "loc": { "start": { "line": 18, - "column": 17 + "column": 20 }, "end": { "line": 18, - "column": 24 + "column": 22 } } }, "loc": { "start": { "line": 18, - "column": 13 + "column": 20 }, "end": { "line": 18, - "column": 24 + "column": 22 } } + }, + "arguments": [], + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 24 + } } - ], - "kind": "let", + }, "loc": { "start": { "line": 18, @@ -191,7 +155,7 @@ "loc": { "start": { "line": 17, - "column": 14 + "column": 17 }, "end": { "line": 19, @@ -202,7 +166,7 @@ "loc": { "start": { "line": 17, - "column": 11 + "column": 8 }, "end": { "line": 19, @@ -213,7 +177,7 @@ "loc": { "start": { "line": 17, - "column": 11 + "column": 8 }, "end": { "line": 19, @@ -500,3 +464,4 @@ } } } +TypeError: The only allowed return value is 'this' if the method's return type is the 'this' type [this_type_invalid_return_type.ets:18:9] diff --git a/ets2panda/test/compiler/ets/this_type_invalid_return_type.ets b/ets2panda/test/compiler/ets/this_type_invalid_return_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..8f229dc409f41101250811f2e4fa2e72c2aea664 --- /dev/null +++ b/ets2panda/test/compiler/ets/this_type_invalid_return_type.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +class A { + foo(): this { + return new A(); + } +} diff --git a/ets2panda/test/compiler/ets/this_type_valid_return_type-expected.txt b/ets2panda/test/compiler/ets/this_type_valid_return_type-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..5c242c269b759b967a6f10f6f31c4ff5d6aec707 --- /dev/null +++ b/ets2panda/test/compiler/ets/this_type_valid_return_type-expected.txt @@ -0,0 +1,424 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "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": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSThisType", + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "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": 20, + "column": 2 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "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 + } + } + } + ], + "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": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/this_type_valid_return_type.ets b/ets2panda/test/compiler/ets/this_type_valid_return_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..6a19b3f0a7adb434a031d8f988139d7753980958 --- /dev/null +++ b/ets2panda/test/compiler/ets/this_type_valid_return_type.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +class A { + foo(): this { + return this; + } +} diff --git a/ets2panda/test/compiler/ets/validate_signatures_throw_type_error-expected.txt b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..ae620a89c879497edb80a5f57152f061cb706552 --- /dev/null +++ b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error-expected.txt @@ -0,0 +1,445 @@ +{ + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Byte", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Byte", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 31 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "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 + } + } +} +TypeError: No matching construct signature for std.core.Byte(int) [validate_signatures_throw_type_error.ets:17:19] diff --git a/ets2panda/test/compiler/ets/validate_signatures_throw_type_error.ets b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error.ets new file mode 100644 index 0000000000000000000000000000000000000000..1b26009e1956f4bdc333f3867fc6ae349a5bc55d --- /dev/null +++ b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +function main() : void { + let a: Byte = new Byte(2); +} diff --git a/ets2panda/test/compiler/ets/validate_signatures_throw_type_error_more_param-expected.txt b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error_more_param-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..1b7c8798937e4356b5ec0d841c26f19d393598a2 --- /dev/null +++ b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error_more_param-expected.txt @@ -0,0 +1,459 @@ +{ + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "e", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Float", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Float", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 6, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + { + "type": "StringLiteral", + "value": "3", + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 37 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "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 + } + } +} +TypeError: No matching construct signature for std.core.Float(int, string) [validate_signatures_throw_type_error_more_param.ets:17:20] diff --git a/ets2panda/test/compiler/ets/validate_signatures_throw_type_error_more_param.ets b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error_more_param.ets new file mode 100644 index 0000000000000000000000000000000000000000..5b0a1e2f000203a71c6f6edd5ee2297c267f0035 --- /dev/null +++ b/ets2panda/test/compiler/ets/validate_signatures_throw_type_error_more_param.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +function main() : void { + let e: Float = new Float(6,"3"); +} diff --git a/ets2panda/test/parser/ets/FunctionalTypeAsTypeArgument-expected.txt b/ets2panda/test/parser/ets/FunctionalTypeAsTypeArgument-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d9ec76360b16e293728556a7f624eaf5f3de9119 --- /dev/null +++ b/ets2panda/test/parser/ets/FunctionalTypeAsTypeArgument-expected.txt @@ -0,0 +1,384 @@ +{ + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Array", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "args", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 16, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 16, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 38 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 42 + }, + "end": { + "line": 16, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 42 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 42 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 47 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 48 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 17, + "column": 1 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 17, + "column": 1 + } + } + } + ], + "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 + } + } +} diff --git a/ets2panda/test/parser/ets/FunctionalTypeAsTypeArgument.ets b/ets2panda/test/parser/ets/FunctionalTypeAsTypeArgument.ets new file mode 100644 index 0000000000000000000000000000000000000000..2c33faa7cb7e04cb3407b2bc16eac83267064998 --- /dev/null +++ b/ets2panda/test/parser/ets/FunctionalTypeAsTypeArgument.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +const alma = new Array<(args: Object) => void>() diff --git a/ets2panda/test/parser/ets/InterfacePrivateMethod-expected.txt b/ets2panda/test/parser/ets/InterfacePrivateMethod-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..95121089560b9b55ccd7cc7f4c398ee7e0a43395 --- /dev/null +++ b/ets2panda/test/parser/ets/InterfacePrivateMethod-expected.txt @@ -0,0 +1,1212 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getHorsePower3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "kind": "method", + "accessibility": "private", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getHorsePower3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "rpm", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "trq", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 46 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 49 + }, + "end": { + "line": 17, + "column": 52 + } + } + }, + "declare": true, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "operator": "/", + "left": { + "type": "BinaryExpression", + "operator": "*", + "left": { + "type": "Identifier", + "name": "rpm", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "right": { + "type": "Identifier", + "name": "trq", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 5252, + "loc": { + "start": { + "line": 18, + "column": 30 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 52 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 27 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 27 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getPwrIndex", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getPwrIndex", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "trq", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 25 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "declare": true, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "getHorsePower3", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 5252, + "loc": { + "start": { + "line": 22, + "column": 36 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + { + "type": "Identifier", + "name": "trq", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 42 + }, + "end": { + "line": 22, + "column": 45 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 47 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 31 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Vehicle", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Car", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 7 + }, + "end": { + "line": 26, + "column": 10 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Vehicle", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + } + ], + "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": 26, + "column": 31 + }, + "end": { + "line": 26, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + { + "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": 28, + "column": 10 + }, + "end": { + "line": 28, + "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": 28, + "column": 10 + }, + "end": { + "line": 28, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Car", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 20 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 22 + } + } + }, + "property": { + "type": "Identifier", + "name": "getPwrIndex", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 22 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 29, + "column": 34 + }, + "end": { + "line": 29, + "column": 35 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 36 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 29, + "column": 40 + }, + "end": { + "line": 29, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 41 + } + } + }, + "second": null, + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 42 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 23 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 1 + }, + "end": { + "line": 30, + "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": 31, + "column": 1 + } + } +} +TypeError: Cannot reference 'this' in this context. [InterfacePrivateMethod.ets:21:16] diff --git a/ets2panda/test/parser/ets/InterfacePrivateMethod.ets b/ets2panda/test/parser/ets/InterfacePrivateMethod.ets new file mode 100644 index 0000000000000000000000000000000000000000..36f491b499a24fd85547f0c5dab61adde8cba788 --- /dev/null +++ b/ets2panda/test/parser/ets/InterfacePrivateMethod.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +interface Vehicle { + private getHorsePower3(rpm: int, trq: int): int{ + return (rpm * trq) / 5252; + } + + getPwrIndex(trq: int): int{ + return this.getHorsePower3(5252, trq); + } +} + +class Car implements Vehicle{} + +function main(): void { + assert new Car().getPwrIndex(1) == 1; +} diff --git a/ets2panda/test/parser/ets/array_creation_expression-expected.txt b/ets2panda/test/parser/ets/array_creation_expression-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..12151490301effd8f49acd534d283c7697b20c60 --- /dev/null +++ b/ets2panda/test/parser/ets/array_creation_expression-expected.txt @@ -0,0 +1,1742 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "y", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "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, + "column": 8 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + { + "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": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ListTest", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 21, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 21, + "column": 18 + } + } + }, + "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", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 24, + "column": 20 + }, + "end": { + "line": 24, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 23 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "data", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 13 + }, + "end": { + "line": 25, + "column": 17 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 24 + }, + "end": { + "line": 25, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 24 + }, + "end": { + "line": 25, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 24 + }, + "end": { + "line": 25, + "column": 26 + } + } + }, + "dimension": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 25, + "column": 26 + }, + "end": { + "line": 25, + "column": 30 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 31 + }, + "end": { + "line": 25, + "column": 34 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 26 + }, + "end": { + "line": 25, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 20 + }, + "end": { + "line": 25, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 13 + }, + "end": { + "line": 25, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "data", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 13 + }, + "end": { + "line": 27, + "column": 17 + } + } + }, + "accessibility": "private", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 19 + }, + "end": { + "line": 27, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 19 + }, + "end": { + "line": 27, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 19 + }, + "end": { + "line": 27, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 22 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 13 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 13 + }, + "end": { + "line": 28, + "column": 16 + } + } + }, + "accessibility": "private", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 21 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 13 + }, + "end": { + "line": 28, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 29, + "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": "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": "s", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 9 + }, + "end": { + "line": 32, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 32, + "column": 24 + }, + "end": { + "line": 32, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 13 + }, + "end": { + "line": 32, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 9 + }, + "end": { + "line": 32, + "column": 26 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 27 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "n", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 9 + }, + "end": { + "line": 33, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 33, + "column": 24 + }, + "end": { + "line": 33, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 13 + }, + "end": { + "line": 33, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 9 + }, + "end": { + "line": 33, + "column": 26 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 27 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "i", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 34, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 34, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 34, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 34, + "column": 21 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 34, + "column": 21 + }, + "end": { + "line": 34, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 13 + }, + "end": { + "line": 34, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 34, + "column": 23 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 34, + "column": 24 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 35, + "column": 23 + }, + "end": { + "line": 35, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 25 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 35, + "column": 5 + }, + "end": { + "line": 35, + "column": 26 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "y", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 36, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "ListTest", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 17 + }, + "end": { + "line": 36, + "column": 25 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 26 + }, + "end": { + "line": 36, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 26 + }, + "end": { + "line": 36, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 26 + }, + "end": { + "line": 36, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 36, + "column": 25 + }, + "end": { + "line": 36, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 17 + }, + "end": { + "line": 36, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 17 + }, + "end": { + "line": 36, + "column": 33 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 36, + "column": 33 + }, + "end": { + "line": 36, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 36, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 23 + }, + "end": { + "line": 37, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 37, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 37, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 37, + "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/parser/ets/array_creation_expression.ets b/ets2panda/test/parser/ets/array_creation_expression.ets new file mode 100644 index 0000000000000000000000000000000000000000..771b8f673fbf0ddd34fcd1f54328cb4ebd4fc8fa --- /dev/null +++ b/ets2panda/test/parser/ets/array_creation_expression.ets @@ -0,0 +1,39 @@ +/* + * 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. + */ + +class Point { + x: number; + y: number; +} + +class ListTest { + constructor() + { + this.num = 10; + let data = new T[this.num]; + } + private data: T[]; + private num: int; +} + +function main(): void { + let s = new string[3]; + let n = new number[3]; + let i = new Int[3]; + let x = new Point[3]; + let y = new ListTest[3]; +} + + diff --git a/ets2panda/test/parser/ets/folder_import_index/index-expected.txt b/ets2panda/test/parser/ets/folder_import_index/index-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..7fee7d1bd9dcdcd80fdb8475d099705d7be2a768 --- /dev/null +++ b/ets2panda/test/parser/ets/folder_import_index/index-expected.txt @@ -0,0 +1,247 @@ +{ + "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": "ad", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 24 + } + } + } + ], + "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 + } + } +} diff --git a/ets2panda/test/parser/ets/folder_import_index/index.ets b/ets2panda/test/parser/ets/folder_import_index/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1e4379a5a2a22095afd6ff15fa14fac9df0ad1fa --- /dev/null +++ b/ets2panda/test/parser/ets/folder_import_index/index.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +export function ad() {}; diff --git a/ets2panda/test/parser/ets/genericDefaultParam_1-expected.txt b/ets2panda/test/parser/ets/genericDefaultParam_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..09b1fbae7aa48d865796e673b3f1e1a3661e4bf7 --- /dev/null +++ b/ets2panda/test/parser/ets/genericDefaultParam_1-expected.txt @@ -0,0 +1,3391 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "SomeType", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 7 + }, + "end": { + "line": 24, + "column": 15 + } + } + }, + "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": 24, + "column": 18 + }, + "end": { + "line": 24, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 1 + }, + "end": { + "line": 24, + "column": 18 + } + } + }, + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 40 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 11 + }, + "end": { + "line": 25, + "column": 20 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T1", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 22 + }, + "end": { + "line": 25, + "column": 24 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "SomeType", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 27 + }, + "end": { + "line": 25, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 27 + }, + "end": { + "line": 25, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 27 + }, + "end": { + "line": 25, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 22 + }, + "end": { + "line": 25, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 21 + }, + "end": { + "line": 25, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 1 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 7 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T2", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 15 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "SomeType", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 18 + }, + "end": { + "line": 26, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 18 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 18 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 27 + } + } + }, + "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": 26, + "column": 31 + }, + "end": { + "line": 26, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 28 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived1", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 7 + }, + "end": { + "line": 27, + "column": 15 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 24 + }, + "end": { + "line": 27, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 24 + }, + "end": { + "line": 27, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 24 + }, + "end": { + "line": 27, + "column": 39 + } + } + }, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 40 + }, + "end": { + "line": 27, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 40 + }, + "end": { + "line": 27, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 40 + }, + "end": { + "line": 27, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 40 + }, + "end": { + "line": 27, + "column": 51 + } + } + } + ], + "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": 27, + "column": 53 + }, + "end": { + "line": 27, + "column": 53 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 50 + }, + "end": { + "line": 27, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 27, + "column": 53 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived2", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 7 + }, + "end": { + "line": 28, + "column": 15 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 24 + }, + "end": { + "line": 28, + "column": 28 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "SomeType", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 29 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 29 + }, + "end": { + "line": 28, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 29 + }, + "end": { + "line": 28, + "column": 38 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 28 + }, + "end": { + "line": 28, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 24 + }, + "end": { + "line": 28, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 24 + }, + "end": { + "line": 28, + "column": 49 + } + } + }, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 50 + }, + "end": { + "line": 28, + "column": 59 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "SomeType", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 60 + }, + "end": { + "line": 28, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 60 + }, + "end": { + "line": 28, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 60 + }, + "end": { + "line": 28, + "column": 69 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 59 + }, + "end": { + "line": 28, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 50 + }, + "end": { + "line": 28, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 50 + }, + "end": { + "line": 28, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 50 + }, + "end": { + "line": 28, + "column": 71 + } + } + } + ], + "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": 28, + "column": 73 + }, + "end": { + "line": 28, + "column": 73 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 70 + }, + "end": { + "line": 28, + "column": 73 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 1 + }, + "end": { + "line": 28, + "column": 73 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 7 + }, + "end": { + "line": 30, + "column": 9 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T1", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 11 + }, + "end": { + "line": 30, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 11 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T2", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 15 + }, + "end": { + "line": 30, + "column": 17 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 20 + }, + "end": { + "line": 30, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 20 + }, + "end": { + "line": 30, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 20 + }, + "end": { + "line": 30, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 15 + }, + "end": { + "line": 30, + "column": 27 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T3", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 28 + }, + "end": { + "line": 30, + "column": 30 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 33 + }, + "end": { + "line": 30, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 33 + }, + "end": { + "line": 30, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 33 + }, + "end": { + "line": 30, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 28 + }, + "end": { + "line": 30, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 10 + }, + "end": { + "line": 30, + "column": 40 + } + } + }, + "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": 30, + "column": 43 + }, + "end": { + "line": 30, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 41 + }, + "end": { + "line": 30, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 30, + "column": 43 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 5 + } + } + }, + "arguments": [ + { + "type": "TSAsExpression", + "expression": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 7 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 7 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 18 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 5 + } + } + }, + "arguments": [ + { + "type": "TSAsExpression", + "expression": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + } + ], + "optional": false, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 6 + }, + "end": { + "line": 21, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 6 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 6 + }, + "end": { + "line": 21, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 5 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "alma", + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 20 + } + } + } + ], + "optional": false, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 6 + }, + "end": { + "line": 22, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 6 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 6 + }, + "end": { + "line": 22, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "c1", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 7 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 31, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 17 + }, + "end": { + "line": 31, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 17 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 17 + }, + "end": { + "line": 31, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 16 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "c2", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 7 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 32, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "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 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 16 + }, + "end": { + "line": 32, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 32, + "column": 10 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "c3", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 7 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 33, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 32 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 33, + "column": 16 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 33, + "column": 10 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 34, + "column": 1 + } + } + } + ], + "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": "alma", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "param", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 37 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "param", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 41 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "c1", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 7 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 31, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 17 + }, + "end": { + "line": 31, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 17 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 17 + }, + "end": { + "line": 31, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 16 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 32, + "column": 4 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "c2", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 7 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 32, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "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 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 16 + }, + "end": { + "line": 32, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 14 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 32, + "column": 10 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 33, + "column": 4 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "c3", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 7 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C2", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 33, + "column": 16 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 32 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 33, + "column": 16 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 33, + "column": 10 + }, + "end": { + "line": 34, + "column": 1 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 34, + "column": 1 + } + } + } + ], + "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": 34, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/genericDefaultParam_1.ets b/ets2panda/test/parser/ets/genericDefaultParam_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..366de812fcc8617c4162b9e7b18b387f4a6f6d4e --- /dev/null +++ b/ets2panda/test/parser/ets/genericDefaultParam_1.ets @@ -0,0 +1,33 @@ +/* + * 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. + */ + +function alma(param : T): T { + return param +} + +alma(1 as number) +alma(1 as number) +alma("alma") + +class SomeType {} +interface Interface { } +class Base { } +class Derived1 extends Base implements Interface { } +class Derived2 extends Base implements Interface { } + +class C2 {} +let c1 = new C2 +let c2 = new C2 +let c3 = new C2 diff --git a/ets2panda/test/parser/ets/genericDefaultParam_2-expected.txt b/ets2panda/test/parser/ets/genericDefaultParam_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..39b6946109e0c493d0302100743ab708c799729a --- /dev/null +++ b/ets2panda/test/parser/ets/genericDefaultParam_2-expected.txt @@ -0,0 +1 @@ +SyntaxError: Required type parameters may not follow optional type parameters. [genericDefaultParam_2.ets:16:30] diff --git a/ets2panda/test/parser/ets/genericDefaultParam_2.ets b/ets2panda/test/parser/ets/genericDefaultParam_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..a82f70029dbee405e9c7ebaefb1043dcf9b112ef --- /dev/null +++ b/ets2panda/test/parser/ets/genericDefaultParam_2.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +class C1 {} diff --git a/ets2panda/test/parser/ets/genericDefaultParam_3-expected.txt b/ets2panda/test/parser/ets/genericDefaultParam_3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..03c646f0d8dc4c1076f49c53192af1d3b5588737 --- /dev/null +++ b/ets2panda/test/parser/ets/genericDefaultParam_3-expected.txt @@ -0,0 +1,530 @@ +{ + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 5 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "alma", + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 12 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 13 + } + } + } + ], + "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": "alma", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "alma", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "param", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 37 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "param", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 41 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "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": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/genericDefaultParam_3.ets b/ets2panda/test/parser/ets/genericDefaultParam_3.ets new file mode 100644 index 0000000000000000000000000000000000000000..2f74b05ce0729743ff47bd47156f0ef731566ee9 --- /dev/null +++ b/ets2panda/test/parser/ets/genericDefaultParam_3.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +function alma(param : T): T { + return param +} + +alma("alma") diff --git a/ets2panda/test/parser/ets/import_folder-expected.txt b/ets2panda/test/parser/ets/import_folder-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..13eaacba2083a9f1c87de235ffd2cf0781b00810 --- /dev/null +++ b/ets2panda/test/parser/ets/import_folder-expected.txt @@ -0,0 +1,254 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./folder_import_index", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 38 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 3 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + } + ], + "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 + } + } + } + ], + "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/parser/ets/import_folder.ets b/ets2panda/test/parser/ets/import_folder.ets new file mode 100644 index 0000000000000000000000000000000000000000..70749437b53dc73be41adf08feead290968f769b --- /dev/null +++ b/ets2panda/test/parser/ets/import_folder.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +import * from "./folder_import_index"; + +ad(); diff --git a/ets2panda/test/parser/ets/import_tests/export_type_alias-expected.txt b/ets2panda/test/parser/ets/import_tests/export_type_alias-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..58b7c14624c409e23a4cbb3e98ba148e2e85a09e --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/export_type_alias-expected.txt @@ -0,0 +1,400 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "TestClass", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "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": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "int32", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "test_class", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TestClass", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + { + "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 + } + } + } + ], + "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": 20, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/import_tests/export_type_alias.ets b/ets2panda/test/parser/ets/import_tests/export_type_alias.ets new file mode 100644 index 0000000000000000000000000000000000000000..44816d18d162700654af9721a18ccf99c231fac7 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/export_type_alias.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +class TestClass {} + +export type int32 = int; +export type test_class = TestClass; diff --git a/ets2panda/test/parser/ets/import_tests/import_all_type_alias-expected.txt b/ets2panda/test/parser/ets/import_tests/import_all_type_alias-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..20deb523cdd1d7373f54d6b94c4415469ac19e8b --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_all_type_alias-expected.txt @@ -0,0 +1,646 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 43 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "all", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 44 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "test_var", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "TSQualifiedName", + "left": { + "type": "Identifier", + "name": "all", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "right": { + "type": "Identifier", + "name": "test_class", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "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": "TestFunc", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "TestFunc", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "TSQualifiedName", + "left": { + "type": "Identifier", + "name": "all", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "right": { + "type": "Identifier", + "name": "int32", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 3 + }, + "end": { + "line": 19, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "test_var", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "TSQualifiedName", + "left": { + "type": "Identifier", + "name": "all", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "right": { + "type": "Identifier", + "name": "test_class", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "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": 23, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/import_tests/import_all_type_alias.ets b/ets2panda/test/parser/ets/import_tests/import_all_type_alias.ets new file mode 100644 index 0000000000000000000000000000000000000000..51ef2e61d60a0becbaedc7855c395ddd27e139c4 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_all_type_alias.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +import * as all from "./export_type_alias"; + +function TestFunc(): all.int32 { + return 0; +} + +let test_var = new all.test_class(); diff --git a/ets2panda/test/parser/ets/inferingEntryPointReturn-expected.txt b/ets2panda/test/parser/ets/inferingEntryPointReturn-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..457bb3cf60903a489cdd7d6cdcdbbddcc3c118df --- /dev/null +++ b/ets2panda/test/parser/ets/inferingEntryPointReturn-expected.txt @@ -0,0 +1,491 @@ +{ + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 26 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + { + "type": "TryStatement", + "block": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "right": { + "type": "ArrayExpression", + "elements": [ + { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "handler": [ + { + "type": "CatchClause", + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": null, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "param": { + "type": "Identifier", + "name": "e", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 22, + "column": 6 + } + } + } + ], + "finalizer": null, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 23, + "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": 24, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/inferingEntryPointReturn.ets b/ets2panda/test/parser/ets/inferingEntryPointReturn.ets new file mode 100644 index 0000000000000000000000000000000000000000..3b091a97536196ec201d71087207d6900b868826 --- /dev/null +++ b/ets2panda/test/parser/ets/inferingEntryPointReturn.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +function main(){ + let a: boolean[] = [] + try{ + a = [false] + } catch (e) { + return + } +} diff --git a/ets2panda/test/parser/ets/lambda_import_alias_1-2-expected.txt b/ets2panda/test/parser/ets/lambda_import_alias_1-2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fce52818b71960a20d4b03225fc1764ee629b291 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_import_alias_1-2-expected.txt @@ -0,0 +1,531 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 49 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "lib", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 49 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "TSQualifiedName", + "left": { + "type": "Identifier", + "name": "lib", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "right": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 18, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 18, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 18, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 18, + "column": 7 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "createA", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "right": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "lib", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "property": { + "type": "Identifier", + "name": "createA", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 42 + } + } + } + ], + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "createA", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "value": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "lib", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "property": { + "type": "Identifier", + "name": "createA", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 42 + } + } + } + ], + "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/parser/ets/lambda_import_alias_1-2.ets b/ets2panda/test/parser/ets/lambda_import_alias_1-2.ets new file mode 100644 index 0000000000000000000000000000000000000000..683d87a1886e19248039ffdc6ae8c875ef384012 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_import_alias_1-2.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +import * as lib from "./lambda_import_alias_1-3" +export type A = lib.A +export let createA: () => A = lib.createA diff --git a/ets2panda/test/parser/ets/lambda_import_alias_1-3-expected.txt b/ets2panda/test/parser/ets/lambda_import_alias_1-3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b49ac0e91dcd242301debf8983c31b70d7ef7336 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_import_alias_1-3-expected.txt @@ -0,0 +1,494 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "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": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + { + "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": "createA", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "createA", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 47 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 49 + } + } + } + ], + "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": 18, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/lambda_import_alias_1-3.ets b/ets2panda/test/parser/ets/lambda_import_alias_1-3.ets new file mode 100644 index 0000000000000000000000000000000000000000..eb62a757042c4722d1b6e579a1f67b89ea47599d --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_import_alias_1-3.ets @@ -0,0 +1,17 @@ +/* + * 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. + */ + +export class A {} +export function createA(): A { return new A(); } diff --git a/ets2panda/test/parser/ets/lambda_import_alias_1-expected.txt b/ets2panda/test/parser/ets/lambda_import_alias_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fa725f44739e4bd4c641703336620684ed192c8e --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_import_alias_1-expected.txt @@ -0,0 +1,210 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "index", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + { + "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 + } + } + } + ], + "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 + } + } +} diff --git a/ets2panda/test/parser/ets/lambda_import_alias_1.ets b/ets2panda/test/parser/ets/lambda_import_alias_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..c506b029ca5b9d2808073fec2fe6283c0d1481c4 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_import_alias_1.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +import * as index from "./lambda_import_alias_1-2" diff --git a/ets2panda/test/parser/ets/nullableGenericSignature-expected.txt b/ets2panda/test/parser/ets/nullableGenericSignature-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d9bf80ff9764e30ecdfc1339b5bf7c4ed4650578 --- /dev/null +++ b/ets2panda/test/parser/ets/nullableGenericSignature-expected.txt @@ -0,0 +1,1318 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Arr", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "every", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "every", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "fn", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "v", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 30 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 40 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 41 + }, + "end": { + "line": 19, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 19, + "column": 4 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "flat", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "flat", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Arr", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 21, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ThrowStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Error", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 30 + }, + "end": { + "line": 23, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 23, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 23, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 23, + "column": 4 + } + } + }, + { + "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": 24, + "column": 2 + }, + "end": { + "line": 24, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 24, + "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": "test", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "test", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Arr", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 18 + }, + "end": { + "line": 26, + "column": 21 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 18 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 18 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 15 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 15 + }, + "end": { + "line": 26, + "column": 30 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 26, + "column": 32 + }, + "end": { + "line": 26, + "column": 39 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 11 + } + } + }, + "property": { + "type": "Identifier", + "name": "every", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 17 + } + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "item", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 32 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 19 + }, + "end": { + "line": 27, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 19 + }, + "end": { + "line": 27, + "column": 32 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 27, + "column": 34 + }, + "end": { + "line": 27, + "column": 41 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 45 + }, + "end": { + "line": 29, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 18 + }, + "end": { + "line": 29, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 18 + }, + "end": { + "line": 29, + "column": 4 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 29, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 3 + }, + "end": { + "line": 29, + "column": 5 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 40 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 30, + "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": 31, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/nullableGenericSignature.ets b/ets2panda/test/parser/ets/nullableGenericSignature.ets new file mode 100644 index 0000000000000000000000000000000000000000..a17c97b9679d5e5168ace24a42aa84fb118bc622 --- /dev/null +++ b/ets2panda/test/parser/ets/nullableGenericSignature.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +class Arr { + every(fn: (v: T) => boolean): boolean { + return true; + } + + flat(): Arr { + throw new Error(); + } +} + +function test(x: Arr): boolean { + return x.every((item: Object): boolean => { + return true; + }) +} diff --git a/ets2panda/test/parser/ets/nullable_union_array-expected.txt b/ets2panda/test/parser/ets/nullable_union_array-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..25904e3cab4eaca073da67d15d40ab32eeaaf027 --- /dev/null +++ b/ets2panda/test/parser/ets/nullable_union_array-expected.txt @@ -0,0 +1,556 @@ +{ + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "values", + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 37 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "StringLiteral", + "value": "Test", + "loc": { + "start": { + "line": 17, + "column": 40 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 17, + "column": 48 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 17, + "column": 51 + }, + "end": { + "line": 17, + "column": 52 + } + } + }, + { + "type": "NullLiteral", + "value": null, + "loc": { + "start": { + "line": 17, + "column": 54 + }, + "end": { + "line": 17, + "column": 58 + } + } + }, + { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 17, + "column": 60 + }, + "end": { + "line": 17, + "column": 61 + } + } + }, + { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 17, + "column": 63 + }, + "end": { + "line": 17, + "column": 65 + } + } + }, + { + "type": "NullLiteral", + "value": null, + "loc": { + "start": { + "line": 17, + "column": 67 + }, + "end": { + "line": 17, + "column": 71 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 72 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 72 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 73 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "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/parser/ets/nullable_union_array.ets b/ets2panda/test/parser/ets/nullable_union_array.ets new file mode 100644 index 0000000000000000000000000000000000000000..280288eb3dd5d2ed0d1626ae9c0138a288b6d91d --- /dev/null +++ b/ets2panda/test/parser/ets/nullable_union_array.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +function main(): void { + let values: (string|Int|null)[] = ["Test", 2, 3, null, 5, 10, null]; +} diff --git a/ets2panda/test/parser/ets/optional_union_paramter-expected.txt b/ets2panda/test/parser/ets/optional_union_paramter-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a48d7291b63b3dff98f8a8e48ad7e488a70721dd --- /dev/null +++ b/ets2panda/test/parser/ets/optional_union_paramter-expected.txt @@ -0,0 +1,1497 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "split", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "split", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "separator", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 43 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 43 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "limit", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 52 + }, + "end": { + "line": 17, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 52 + }, + "end": { + "line": 17, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 52 + }, + "end": { + "line": 17, + "column": 59 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 59 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 59 + } + } + } + ], + "returnType": { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 61 + }, + "end": { + "line": 17, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 61 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 61 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 70 + }, + "end": { + "line": 17, + "column": 71 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ArrayExpression", + "elements": [ + { + "type": "StringLiteral", + "value": "", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 70 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "split_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "split_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "separator", + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "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, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "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, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "limit", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "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, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "returnType": { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "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, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "limit", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "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, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "split", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "separator", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "limit", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "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, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "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": 20, + "column": 2 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "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": "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": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 20 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 20 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 20 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "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": 24, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/optional_union_paramter.ets b/ets2panda/test/parser/ets/optional_union_paramter.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ee62946d80f8f90403df8b4902e64cf1b14a964 --- /dev/null +++ b/ets2panda/test/parser/ets/optional_union_paramter.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +class A { + public split (separator: String | Int, limit?: Number) :String[] { + return [""] + } +} +function main () : void { + +} diff --git a/ets2panda/test/parser/ets/re_export/folderIndex/index-expected.txt b/ets2panda/test/parser/ets/re_export/folderIndex/index-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3120da76ac6e849950f577d23b178a88d0288fec --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/folderIndex/index-expected.txt @@ -0,0 +1,153 @@ +{ + "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 + } + } + } + ], + "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 + } + } +} diff --git a/ets2panda/test/parser/ets/re_export/folderIndex/index.ets b/ets2panda/test/parser/ets/re_export/folderIndex/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..2de0cf11791504823bdbc3b7590d3bc84be558a8 --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/folderIndex/index.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +export {ad} from "./test"; diff --git a/ets2panda/test/parser/ets/re_export/folderIndex/test-expected.txt b/ets2panda/test/parser/ets/re_export/folderIndex/test-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..17b2f3544c365c637991eab1eefb959d6cb078ea --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/folderIndex/test-expected.txt @@ -0,0 +1,247 @@ +{ + "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": "ad", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 23 + } + } + } + ], + "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 + } + } +} diff --git a/ets2panda/test/parser/ets/re_export/folderIndex/test.ets b/ets2panda/test/parser/ets/re_export/folderIndex/test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0e0ed625020b4ff498f0ce4ba28bd5742e78673e --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/folderIndex/test.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +export function ad(){}; diff --git a/ets2panda/test/parser/ets/re_export/import_index-expected.txt b/ets2panda/test/parser/ets/re_export/import_index-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b17dc4ba58d0dbf8a6d3a405ce860e629c5b3de7 --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/import_index-expected.txt @@ -0,0 +1,254 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./folderIndex", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 30 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 3 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + } + ], + "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 + } + } + } + ], + "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/parser/ets/re_export/import_index.ets b/ets2panda/test/parser/ets/re_export/import_index.ets new file mode 100644 index 0000000000000000000000000000000000000000..5b3ec70e4b5238641f305cceced604b7a3fc4ead --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/import_index.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +import * from "./folderIndex"; + +ad(); diff --git a/ets2panda/test/parser/ets/re_export/import_index_2-expected.txt b/ets2panda/test/parser/ets/re_export/import_index_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..8d4fa72dfea52519094053aafe9298b2d9f4bbed --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/import_index_2-expected.txt @@ -0,0 +1,269 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./folderIndex", + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "imported": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 34 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "ad", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 3 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + } + ], + "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 + } + } + } + ], + "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/parser/ets/re_export/import_index_2.ets b/ets2panda/test/parser/ets/re_export/import_index_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..0c187b82dcc12403281118deda83963bd1e7c66a --- /dev/null +++ b/ets2panda/test/parser/ets/re_export/import_index_2.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +import {ad} from "./folderIndex"; + +ad(); diff --git a/ets2panda/test/parser/ets/selective_export/import_1-expected.txt b/ets2panda/test/parser/ets/selective_export/import_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..4d08cca3d29655b7de2e89cc3a0049205591667d --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_1-expected.txt @@ -0,0 +1,389 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + { + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 8 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/import_1.ets b/ets2panda/test/parser/ets/selective_export/import_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..cf5db051ca88e72ecd34f9ac896cb94549eb9055 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_1.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import * from "./selective_export_1" + +function main() : void +{ + foo(); +} diff --git a/ets2panda/test/parser/ets/selective_export/import_2-expected.txt b/ets2panda/test/parser/ets/selective_export/import_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..4d08cca3d29655b7de2e89cc3a0049205591667d --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_2-expected.txt @@ -0,0 +1,389 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + { + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 8 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/import_2.ets b/ets2panda/test/parser/ets/selective_export/import_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..a99c8f6a4cbc028459dfe4582afadb54fc33f60d --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_2.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import * from "./selective_export_2" + +function main() : void +{ + foo(); +} diff --git a/ets2panda/test/parser/ets/selective_export/import_3-expected.txt b/ets2panda/test/parser/ets/selective_export/import_3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..4d08cca3d29655b7de2e89cc3a0049205591667d --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_3-expected.txt @@ -0,0 +1,389 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + { + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 8 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/import_3.ets b/ets2panda/test/parser/ets/selective_export/import_3.ets new file mode 100644 index 0000000000000000000000000000000000000000..30f03e957971185deab62e10f6daaf5c0462edf2 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_3.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import * from "./selective_export_3" + +function main() : void +{ + foo(); +} diff --git a/ets2panda/test/parser/ets/selective_export/import_4-expected.txt b/ets2panda/test/parser/ets/selective_export/import_4-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..271570dc9802aee53bd7a4605f440bc8ffec5fcd --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_4-expected.txt @@ -0,0 +1,477 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + { + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "sum", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 12 + } + } + }, + "init": { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 25 + } + } + }, + "right": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 31 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 33 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/import_4.ets b/ets2panda/test/parser/ets/selective_export/import_4.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d2aee422803e643e16ccec0b3eb88784c746ba8 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/import_4.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import * from "./selective_export_4" + +function main() : void +{ + let sum: int = foo() + bar(); +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type-expected.txt b/ets2panda/test/parser/ets/selective_export/selective_export_1-expected.txt similarity index 65% rename from ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type-expected.txt rename to ets2panda/test/parser/ets/selective_export/selective_export_1-expected.txt index 18135a31d1318771cb7df9285c4f649ae3258fe8..26044f2d6208a3b9d4567500d9d92add38a3999b 100644 --- a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type-expected.txt +++ b/ets2panda/test/parser/ets/selective_export/selective_export_1-expected.txt @@ -120,7 +120,7 @@ "type": "MethodDefinition", "key": { "type": "Identifier", - "name": "main", + "name": "foo", "decorators": [], "loc": { "start": { @@ -129,7 +129,7 @@ }, "end": { "line": 16, - "column": 14 + "column": 13 } } }, @@ -144,7 +144,7 @@ "type": "ScriptFunction", "id": { "type": "Identifier", - "name": "main", + "name": "foo", "decorators": [], "loc": { "start": { @@ -153,7 +153,7 @@ }, "end": { "line": 16, - "column": 14 + "column": 13 } } }, @@ -161,87 +161,38 @@ "async": false, "expression": false, "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, "body": { "type": "BlockStatement", "statements": [ { - "type": "VariableDeclaration", - "declarations": [ - { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "x", - "decorators": [], - "loc": { - "start": { - "line": 17, - "column": 9 - }, - "end": { - "line": 17, - "column": 10 - } - } - }, - "init": { - "type": "ArrowFunctionExpression", - "function": { - "type": "ScriptFunction", - "id": null, - "generator": false, - "async": false, - "expression": false, - "params": [], - "body": { - "type": "BlockStatement", - "statements": [], - "loc": { - "start": { - "line": 17, - "column": 19 - }, - "end": { - "line": 17, - "column": 21 - } - } - }, - "loc": { - "start": { - "line": 17, - "column": 13 - }, - "end": { - "line": 17, - "column": 21 - } - } - }, - "loc": { - "start": { - "line": 17, - "column": 13 - }, - "end": { - "line": 17, - "column": 21 - } - } + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 12 }, - "loc": { - "start": { - "line": 17, - "column": 9 - }, - "end": { - "line": 17, - "column": 21 - } + "end": { + "line": 17, + "column": 13 } } - ], - "kind": "let", + }, "loc": { "start": { "line": 17, @@ -249,7 +200,7 @@ }, "end": { "line": 17, - "column": 21 + "column": 14 } } } @@ -257,7 +208,7 @@ "loc": { "start": { "line": 16, - "column": 17 + "column": 21 }, "end": { "line": 18, @@ -268,7 +219,7 @@ "loc": { "start": { "line": 16, - "column": 14 + "column": 13 }, "end": { "line": 18, @@ -279,7 +230,7 @@ "loc": { "start": { "line": 16, - "column": 14 + "column": 13 }, "end": { "line": 18, @@ -330,7 +281,7 @@ "column": 1 }, "end": { - "line": 19, + "line": 21, "column": 1 } } diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_1.ets b/ets2panda/test/parser/ets/selective_export/selective_export_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..37c93c3959c797f38850dbc3dec3d2b36f877d25 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_1.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +function foo(): int { + return 1; +} + +export { foo }; diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_2-expected.txt b/ets2panda/test/parser/ets/selective_export/selective_export_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..44ce8e9290dea408ccc3588f77a961e7e811040c --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_2-expected.txt @@ -0,0 +1,288 @@ +{ + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "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": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "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": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_2.ets b/ets2panda/test/parser/ets/selective_export/selective_export_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..3a0c5b89851f456f075306ca325050e7426e5cd2 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_2.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +export { foo }; + +function foo(): int { + return 1; +} diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_3-expected.txt b/ets2panda/test/parser/ets/selective_export/selective_export_3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fd12d7c7dc52bb5bf3b271bf2e684a51df3c2dd0 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_3-expected.txt @@ -0,0 +1,439 @@ +{ + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "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": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 21 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 22, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_3.ets b/ets2panda/test/parser/ets/selective_export/selective_export_3.ets new file mode 100644 index 0000000000000000000000000000000000000000..1a64e5b86fd334897044a675cf1d38eeacf6fe42 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_3.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +function foo(): int { + return bar(); +} + +function bar(): int { + return 1; +} + +export { foo }; diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_4-expected.txt b/ets2panda/test/parser/ets/selective_export/selective_export_4-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3687ae1c099ef772201e5c94a60e7194eb77bb7c --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_4-expected.txt @@ -0,0 +1,423 @@ +{ + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "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": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 21 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 22, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_4.ets b/ets2panda/test/parser/ets/selective_export/selective_export_4.ets new file mode 100644 index 0000000000000000000000000000000000000000..75cc498e0e4c0bf697b2bdb93ec512782729a724 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_4.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +function foo(): int { + return 1; +} + +function bar(): int { + return 2; +} + +export { foo, bar }; diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_bad-expected.txt b/ets2panda/test/parser/ets/selective_export/selective_export_bad-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..dc09c9ecd89c2b385430e8cc8a2e691a400cfbbf --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_bad-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot find name 'foo' to export. [selective_export_bad.ets:16:8] diff --git a/ets2panda/test/parser/ets/selective_export/selective_export_bad.ets b/ets2panda/test/parser/ets/selective_export/selective_export_bad.ets new file mode 100644 index 0000000000000000000000000000000000000000..0e6c6f7c877c099805b504516d6b1839ccad14f4 --- /dev/null +++ b/ets2panda/test/parser/ets/selective_export/selective_export_bad.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +export { foo }; + +function bar(): int { + return 1; +} diff --git a/ets2panda/test/parser/ets/static_function_hide_3-expected.txt b/ets2panda/test/parser/ets/static_function_hide_3-expected.txt index 61b7dbe457b87419106d7fb7908b6bc14eedb5ad..6cb06f2cb9eb27800444766f4879f5fdf2cefc93 100644 --- a/ets2panda/test/parser/ets/static_function_hide_3-expected.txt +++ b/ets2panda/test/parser/ets/static_function_hide_3-expected.txt @@ -735,4 +735,4 @@ } } } -TypeError: Method overriding requires 'override' modifier [static_function_hide_3.ets:21:8] +TypeError: bar(): void in BClass cannot override bar(): void in AClass because overridden method is static. [static_function_hide_3.ets:21:8] diff --git a/ets2panda/test/parser/ets/this_type_class_field_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_class_field_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..511c45d12f959453ee4a231fd97c4b03e8faaaf6 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_field_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_class_field_invalid.ets:18:13] diff --git a/ets2panda/test/parser/ets/this_type_class_field_invalid.ets b/ets2panda/test/parser/ets/this_type_class_field_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..a0f3bab042704fd241ccb1381b2a56d317f0085e --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_field_invalid.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +class A { + attr_i: int; + attr_a: this; +} diff --git a/ets2panda/test/parser/ets/this_type_class_method_parameter_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_class_method_parameter_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fd85437242c48ec5d1fc83215ffb41dafd0c926c --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_method_parameter_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_class_method_parameter_invalid.ets:17:26] diff --git a/ets2panda/test/parser/ets/this_type_class_method_parameter_invalid.ets b/ets2panda/test/parser/ets/this_type_class_method_parameter_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..059bc190595ca1d52f8e44ed378c0e344f9f3a08 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_method_parameter_invalid.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +class A { + foo(arg1: int, arg2: this) {} +} diff --git a/ets2panda/test/parser/ets/this_type_class_method_return_valid-expected.txt b/ets2panda/test/parser/ets/this_type_class_method_return_valid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..5db9e35bd7c295dff5600ad3fbfcfd6e65e75963 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_method_return_valid-expected.txt @@ -0,0 +1,466 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "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": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "arg1", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 15 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 18 + } + } + } + ], + "returnType": { + "type": "TSThisType", + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 17, + "column": 35 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 42 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 42 + } + } + }, + { + "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": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "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 + } + } + } + ], + "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/parser/ets/this_type_class_method_return_valid.ets b/ets2panda/test/parser/ets/this_type_class_method_return_valid.ets new file mode 100644 index 0000000000000000000000000000000000000000..4120e8608aeab5780e1e4a77c90f9e1e227a52f5 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_method_return_valid.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +class A { + foo(arg1: int): this { return this; } +} diff --git a/ets2panda/test/parser/ets/this_type_class_static_method_parameter_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_class_static_method_parameter_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..f3db845baae5be35f7cbe74c6d882422d8d8f948 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_static_method_parameter_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_class_static_method_parameter_invalid.ets:17:33] diff --git a/ets2panda/test/parser/ets/this_type_class_static_method_parameter_invalid.ets b/ets2panda/test/parser/ets/this_type_class_static_method_parameter_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..113852662219919c03bfab94951d76d6d814be4c --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_static_method_parameter_invalid.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +class A { + static foo(arg1: int, arg2: this) {} +} diff --git a/ets2panda/test/parser/ets/this_type_class_static_method_return_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_class_static_method_return_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..e54644a20bfcbffeebb40a6e8882e24b2fcb69fa --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_static_method_return_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_class_static_method_return_invalid.ets:17:33] diff --git a/ets2panda/test/parser/ets/this_type_class_static_method_return_invalid.ets b/ets2panda/test/parser/ets/this_type_class_static_method_return_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fabdb2fb175380c54064e313112878c58b4375e --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_class_static_method_return_invalid.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +class A { + static foo(arg1: int, arg2: this) { return this; } +} diff --git a/ets2panda/test/parser/ets/this_type_function_parameter_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_function_parameter_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..dfc0b3cf07b4eded33dec78cc8f493db93dd8ff0 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_function_parameter_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_function_parameter_invalid.ets:16:31] diff --git a/ets2panda/test/parser/ets/this_type_function_parameter_invalid.ets b/ets2panda/test/parser/ets/this_type_function_parameter_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..22f3277e7d545f46c6c7e962043b34ac6abe4630 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_function_parameter_invalid.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +function foo(arg1: int, arg2: this) {} diff --git a/ets2panda/test/parser/ets/this_type_function_return_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_function_return_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..cd33d06b4e7e3282ab1ff3b93b61fffcb9a3cbb3 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_function_return_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_function_return_invalid.ets:16:26] diff --git a/ets2panda/test/parser/ets/this_type_function_return_invalid.ets b/ets2panda/test/parser/ets/this_type_function_return_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..acea016f969dd1ecf5aebeaece134b3f2ad3b3c2 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_function_return_invalid.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +function foo(arg1: int): this { return this; } diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_in_class_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_in_class_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..e7e058869fd264d3947cb6eb12998a6d2b195961 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_in_class_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_declaration_parameter_in_class_invalid.ets:18:28] diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_in_class_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_in_class_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..47f9e6f1999a9cbea939f39846f0103eb87288cc --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_in_class_invalid.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +class A { + foo() { + let x: (i: int, t: this) => number; + } +} diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..86d9fac7375acec26a3e2b72098ff6bb67b0e456 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_declaration_parameter_invalid.ets:16:20] diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..d2f833f7e40616c5c8e414820f0cfcad5633f2c9 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_parameter_invalid.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +let x: (i: int, t: this) => number; diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_return_in_class_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_in_class_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a870ea5a661376e1bf325535d56a53848866787b --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_in_class_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_declaration_return_in_class_invalid.ets:18:39] diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_return_in_class_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_in_class_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..8987c94df550ed932ea98a3d8656213e76421981 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_in_class_invalid.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +class A { + foo() { + let x: (i: int, n: number) => this; + } +} diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_return_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b41f21492ba3a03ad08a888faec40b0bc6a05f61 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_declaration_return_invalid.ets:16:31] diff --git a/ets2panda/test/parser/ets/this_type_lambda_declaration_return_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e8fb02878dd07ce70e5f2280c55e988ca778dec --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_declaration_return_invalid.ets @@ -0,0 +1,16 @@ +/* + * 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. + */ + +let x: (i: int, n: number) => this; diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_in_class_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_in_class_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..57a2b847d6dd7f4b0724fe688b893a77e7dce3c3 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_in_class_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_definition_parameter_in_class_invalid.ets:19:25] diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_in_class_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_in_class_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..d75fb8dec41ef0d75e1362dfb1be1d5165682bff --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_in_class_invalid.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +class A { + foo() { + let x: (i: int, a: A) => number; + x = (i: int, t: this): number => { return 2.0; }; + } +} diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..5c87380ca0a2a6e128b373cb1964e34d48c22d5d --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_definition_parameter_invalid.ets:19:17] diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..d9574a1a3ce9730f03f02de6d5788bef2098aa12 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_parameter_invalid.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +class A {} + +let x: (i: int, a: A) => number; +x = (i: int, t: this): number => { return 2.0; }; diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_return_in_class_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_definition_return_in_class_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..728c83568db17570851c50d57b4b3edf283bde12 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_return_in_class_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_definition_return_in_class_invalid.ets:19:34] diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_return_in_class_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_definition_return_in_class_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..3fa47d952e2fb164538fa3409b9f1740c7d75f5a --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_return_in_class_invalid.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +class A { + foo() { + let x: (i: int, n: number) => A; + x = (i: int, n: number): this => { return this; }; + } +} diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_return_invalid-expected.txt b/ets2panda/test/parser/ets/this_type_lambda_definition_return_invalid-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d67535099ac7a4b992ed0bf02b7d61675352f19b --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_return_invalid-expected.txt @@ -0,0 +1 @@ +SyntaxError: A 'this' type is available only as return type in a non-static method of a class or struct. [this_type_lambda_definition_return_invalid.ets:19:26] diff --git a/ets2panda/test/parser/ets/this_type_lambda_definition_return_invalid.ets b/ets2panda/test/parser/ets/this_type_lambda_definition_return_invalid.ets new file mode 100644 index 0000000000000000000000000000000000000000..b681c247aecf6de5a934535d62c16b9cf5703341 --- /dev/null +++ b/ets2panda/test/parser/ets/this_type_lambda_definition_return_invalid.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +class A {} + +let x: (i: int, n: number) => A; +x = (i: int, n: number): this => { return new A(); }; diff --git a/ets2panda/test/runtime/ets/InterfacePrivateMethod2.ets b/ets2panda/test/runtime/ets/InterfacePrivateMethod2.ets new file mode 100644 index 0000000000000000000000000000000000000000..d493737f77652e871c9f3baac802662fb73ca981 --- /dev/null +++ b/ets2panda/test/runtime/ets/InterfacePrivateMethod2.ets @@ -0,0 +1,39 @@ +/* + * 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. + */ + +interface Vehicle { + getHorsePower3(rpm: int, trq: int): int{ + return (rpm * trq) / 5252; + } + + getPwrIndex(trq: int): int{ + return this.getHorsePower3(5252, trq); + } + + getHorsePower3(rpm: int): int{ + return rpm; + } + + getPwrIndex(trq: int, second: int): int{ + return this.getHorsePower3(trq + second); + } +} + +class Car implements Vehicle{} + +function main(): void { + assert new Car().getPwrIndex(1) == 1; + assert new Car().getPwrIndex(1,1) == 2; +} diff --git a/ets2panda/test/runtime/ets/Object-type-in-binary-logical-expression.ets b/ets2panda/test/runtime/ets/Object-type-in-binary-logical-expression.ets new file mode 100644 index 0000000000000000000000000000000000000000..352bdd8afd12c563e6d39c6ca409c056869b0bdc --- /dev/null +++ b/ets2panda/test/runtime/ets/Object-type-in-binary-logical-expression.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +function main() { + let apple: Object = new Object(); + let banana: boolean = true; + let cherry: boolean = false; + + assert(apple && banana); + assert(banana && apple); + assert(apple || cherry); + assert(cherry || apple); + + assert(apple && true); + assert(true && apple); + assert(apple || false); + assert(false || apple); +} diff --git a/ets2panda/test/runtime/ets/Override-2.ets b/ets2panda/test/runtime/ets/Override-2.ets new file mode 100644 index 0000000000000000000000000000000000000000..908e8c1d2c852feb30999b87f8b0961909680318 --- /dev/null +++ b/ets2panda/test/runtime/ets/Override-2.ets @@ -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. + */ + +class A { + met(): string { + return "a" + } +} + +class B extends A { + met(): string { + return "b" + } +} + + +function main() { + let a: A = new B() + assert a.met() == "b" +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/UnboxingCheckcast.ets b/ets2panda/test/runtime/ets/UnboxingCheckcast.ets new file mode 100644 index 0000000000000000000000000000000000000000..1a5fdfb062f7c2cc477143a3182b0fb71754168c --- /dev/null +++ b/ets2panda/test/runtime/ets/UnboxingCheckcast.ets @@ -0,0 +1,40 @@ +/* + * 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. + */ + +function main() { + let b: Object[] = [new Object()]; + b[0] = 2.0; + let myvar: double = b[0] as double; + + assert myvar == 2.0; + + let c: Object; + c = 2.0; + c as double; + + let k = 2; + if(2 == k){ + c = new Object(); + } else { + c = new Double(); + } + + try{ + c as double; + } catch (e: ClassCastException){ + } catch(other) { + assert 1 == 0 + } +} diff --git a/ets2panda/test/runtime/ets/UncheckedCasts.ets b/ets2panda/test/runtime/ets/UncheckedCasts.ets new file mode 100644 index 0000000000000000000000000000000000000000..f5852a44e0815ccdac0fb1467b62f368da52e039 --- /dev/null +++ b/ets2panda/test/runtime/ets/UncheckedCasts.ets @@ -0,0 +1,78 @@ +/* + * 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. + */ + +type Nullish = Object | null | undefined; + +class Bad { } +class X { } + +function expect_ccexc(fn: () => void) { + try { + fn(); + assert false; + } catch (e: Exception) { + assert(e instanceof ClassCastException); + } +} + +class G { + erase(x: Nullish): T { return x as T; } +} +function check_G_Object(x: Nullish) { + expect_ccexc(() => { new G().erase(x); }) +} +function check_G_X(x: Nullish) { + expect_ccexc(() => { new G().erase(x); }) +} +function test_substitution() { + //check_G_Object(null); + //check_G_Object(undefined); + //check_G_X(null); + check_G_X(undefined); + check_G_X(new Object()); + check_G_X(new Bad()); +} + +class CG { + pass(x: Nullish) { x as T; } +} +function check_CG_X(x: Nullish) { + expect_ccexc(() => { new CG().pass(x); }) +} +function test_constraint() { + //check_CG_X(null); + check_CG_X(undefined); + check_CG_X(new Object()); + check_CG_X(new Bad()); +} + +class GG { + pass(x: Nullish) { x as G; } +} +function check_GG(x: Nullish) { + expect_ccexc(() => { new GG().pass(x); }) +} +function test_basetype() { + //check_GG(null); + check_GG(undefined); + check_GG(new Object()); +} + +function main() { + // NOTE(vpukhov): add nullability checks, add union param/constraint tests + test_substitution(); + test_constraint(); + test_basetype(); +} diff --git a/ets2panda/test/runtime/ets/UnionConstraint.ets b/ets2panda/test/runtime/ets/UnionConstraint.ets new file mode 100644 index 0000000000000000000000000000000000000000..9cadb73acebaa794c9d3e8f188fc1c9542733e4a --- /dev/null +++ b/ets2panda/test/runtime/ets/UnionConstraint.ets @@ -0,0 +1,31 @@ +/* + * 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. + */ + +class X { } +function id(x: T) { return x } + +function id_s(x: string) { return id(x) } +function id_sx(x: string | X) { return id(x) } + +function main() { + let x = new X(); + + assert(id(1.1) == 1.1); + assert(id("a") == "a"); + assert(id(x) == x); + + assert(id_s("a") == "a"); + assert(id_sx(x) == x); +} diff --git a/ets2panda/test/runtime/ets/castSequence.ets b/ets2panda/test/runtime/ets/castSequence.ets new file mode 100644 index 0000000000000000000000000000000000000000..c3e86ca68a318dfcbdaad8a4c8c240fb295eb301 --- /dev/null +++ b/ets2panda/test/runtime/ets/castSequence.ets @@ -0,0 +1,34 @@ +/* + * 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. + */ + +function main(): void { + let a_int : int = 2147; + let b_Num : Number = a_int as Number; + + assert b_Num instanceof Number && b_Num.unboxed() == 2147.0 + + let a_Short : Short = 215; + let b_double : double = a_Short as double; + + assert a_Short instanceof Short + assert b_double == 215.0 + + let a_Int : Int = 2142; + let b_Double : Double = a_Int as Double; + + assert b_Double instanceof Double && b_Double.unboxed() == 2142.0 + + return; +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/generic-function.ets b/ets2panda/test/runtime/ets/generic-function.ets index 19c2f7c725eb2c89980c4b38a92f2308036d5ca7..1c1469d45e54382584bdcb7af24501d5791498b5 100644 --- a/ets2panda/test/runtime/ets/generic-function.ets +++ b/ets2panda/test/runtime/ets/generic-function.ets @@ -19,7 +19,7 @@ class cls extends Object { } } -function foo(item : T) : String { +function foo(item : T) : String { let str = item.toString(); return str; } diff --git a/ets2panda/test/runtime/ets/generic_getter.ets b/ets2panda/test/runtime/ets/generic_getter.ets new file mode 100644 index 0000000000000000000000000000000000000000..07ab5a1e51e6bd01c46c0a36187ea702ce879cb6 --- /dev/null +++ b/ets2panda/test/runtime/ets/generic_getter.ets @@ -0,0 +1,27 @@ +/* + * 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. + */ + +class A { + f: T + constructor(f: T) { this.f = f } + $_get(index: number): T { return this.f } +} + +function main(): void { + let o_double = new A(10) + const o_int = new A(10) + assert o_double[11] == 10 + assert 10 == o_int[11] +} diff --git a/ets2panda/test/runtime/ets/lambda-class-field.ets b/ets2panda/test/runtime/ets/lambda-class-field.ets new file mode 100644 index 0000000000000000000000000000000000000000..2bca2ab41562fb323d843d96c7a5cb0300f0f473 --- /dev/null +++ b/ets2panda/test/runtime/ets/lambda-class-field.ets @@ -0,0 +1,26 @@ +/* + * 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. + */ + +class cls { + lambda_field: (() => int) | undefined; + readonly num: int; // don't remove it, it is necessary to trigger an issue with the lambda +} + +function main(): void { + let c = new cls(); + c.lambda_field = () => 42; + assert c.lambda_field() == 42; + assert c.num == 0; +} diff --git a/ets2panda/test/runtime/ets/this_type.ets b/ets2panda/test/runtime/ets/this_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..c9b141dd3a3920548338ff744797d1b4efe4c1ab --- /dev/null +++ b/ets2panda/test/runtime/ets/this_type.ets @@ -0,0 +1,50 @@ +/* + * 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. + */ + +class Component { + width: int; + + getWidth(): int { + return this.width; + } + + setWidth(w: int): this { + this.width = w; + + return this; + } +} + +class Button extends Component { + title: string; + + getTitle(): string { + return this.title; + } + + setTitle(t: string): this { + this.title = t; + + return this; + } +} + +function main() { + let b = new Button().setWidth(50).setTitle("🍆"); + + assert b instanceof Button; + assert b.getWidth() == 50; + assert b.getTitle() == "🍆"; +} diff --git a/ets2panda/test/runtime/ets/unboxingBooleanConversion.ets b/ets2panda/test/runtime/ets/unboxingBooleanConversion.ets index 23ae74c084ab06775be988e3359841dcce893b3c..7fb437eeda604faf3dfea8614bab86f2a935cf2c 100644 --- a/ets2panda/test/runtime/ets/unboxingBooleanConversion.ets +++ b/ets2panda/test/runtime/ets/unboxingBooleanConversion.ets @@ -27,7 +27,7 @@ function main(): void { let b: Boolean = a && returnTrue(); assert b == true - let c: Boolean = returnRefBool(a || returnRefBool(returnTrue() && returnRefBool(a))); + let c: Boolean = returnRefBool(a || returnRefBool(returnRefBool(a))); assert c == false } diff --git a/ets2panda/test/runtime/ets/validate_signatures_throw_type_error.ets b/ets2panda/test/runtime/ets/validate_signatures_throw_type_error.ets new file mode 100644 index 0000000000000000000000000000000000000000..9fc7a733c1164bf80c617add44ab4b81b9b64ae7 --- /dev/null +++ b/ets2panda/test/runtime/ets/validate_signatures_throw_type_error.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +function main() : void { + let a: Byte = 2; + let b: Byte = new Byte(a); + + assert a == b; +} diff --git a/ets2panda/test/test-lists/parser/parser-js-ignored.txt b/ets2panda/test/test-lists/parser/parser-js-ignored.txt index 0c171f3c34112e55f42c69104b16ccb036214532..0b48aa71f2a4eaf5096f4d9786aef2db9bd0974a 100644 --- a/ets2panda/test/test-lists/parser/parser-js-ignored.txt +++ b/ets2panda/test/test-lists/parser/parser-js-ignored.txt @@ -1,5 +1,9 @@ # FIXME(igelhaus): Temporarily skipped for dev branch stabilization parser/ets/literals.ets +parser/ets/declare_iface.ets +parser/ets/interfaces.ets +parser/ets/lambda_import_alias_1.ets +parser/ets/test_interface.ets parser/ets/import_tests/modules/module.ets parser/ets/import_tests/import_class_members_1.ets @@ -62,3 +66,6 @@ parser/ets/null-coalesc-negative.ets # Lambda tests, disabled because crash during compiler parser/ets/trailing_lambda_tests/trailing_lambda_define_lambda_in_body_capture_variable.ets + +# Bad test shadows Object with type parameter name #14913 +compiler/ets/override13.ets diff --git a/ets2panda/test/unit/ast_dumper_test.cpp b/ets2panda/test/unit/ast_dumper_test.cpp index e57b5adfea6752a077c381ff0dd6977bef26e31a..0ef51a441e77b3057fb83bf1485fcf9e5cc19eae 100644 --- a/ets2panda/test/unit/ast_dumper_test.cpp +++ b/ets2panda/test/unit/ast_dumper_test.cpp @@ -121,3 +121,30 @@ TEST_F(ASTDumperTest, DumpJsonUTF16Char) ASSERT_FALSE(dump_str.empty()); } + +TEST_F(ASTDumperTest, DumpEtsSrcSimple) +{ + static constexpr std::string_view FILE_NAME = "dummy.ets"; + static constexpr std::string_view SRC = + "\ + function main(args: String[]): int {\ + let a: int = 2;\ + let b: int = 3;\ + return a + b;\ + }"; + + int argc = 1; + const char *argv = + "../../../bin/es2panda " + "--extension=ets " + "--dump-ets-src-before-phases=\"plugins-after-parse:lambda-lowering:checker:plugins-after-check:generate-ts-" + "declarations:op-assignment:tuple-lowering:union-property-access:plugins-after-lowering\""; + + auto program = std::unique_ptr {GetProgram(argc, &argv, FILE_NAME, SRC)}; + + ASSERT_NE(program, nullptr); + + auto dump_str = program->JsonDump(); + + ASSERT_FALSE(dump_str.empty()); +} diff --git a/ets2panda/test/unit/checker_test.cpp b/ets2panda/test/unit/checker_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69a0519e62ef2ae3fe2fe6d5048bfb411b431616 --- /dev/null +++ b/ets2panda/test/unit/checker_test.cpp @@ -0,0 +1,59 @@ +/** + * 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 "macros.h" +#include "public/es2panda_lib.h" + +class CheckerTest : public testing::Test { +public: + CheckerTest() + { + impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + // NOLINTNEXTLINE(modernize-avoid-c-arrays) + char const *argv[] = {"test"}; + cfg_ = impl_->CreateConfig(1, argv); + } + + ~CheckerTest() override + { + impl_->DestroyConfig(cfg_); + } + + NO_COPY_SEMANTIC(CheckerTest); + NO_MOVE_SEMANTIC(CheckerTest); + +protected: + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + es2panda_Impl const *impl_; + es2panda_Config *cfg_; + // NOLINTEND(misc-non-private-member-variables-in-classes) +}; + +TEST_F(CheckerTest, ExtendedConditionalExpressionFunctor) +{ + char const *text = R"XXX( +class A { + m() {} + m2() { this.m ? "a": "b" } +} +)XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + ctx = impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(std::string(impl_->ContextErrorMessage(ctx)), + "TypeError: Condition must be of possible condition type[dummy.ets:4,12]"); + + impl_->DestroyContext(ctx); +} \ No newline at end of file diff --git a/ets2panda/test/unit/public/ast_verifier_test.cpp b/ets2panda/test/unit/public/ast_verifier_test.cpp index 77acdc512997ed3821296bb5fffad908e28fb6ea..dc3414ba7c3f6e0a0e902e79482821719d2a0fac 100644 --- a/ets2panda/test/unit/public/ast_verifier_test.cpp +++ b/ets2panda/test/unit/public/ast_verifier_test.cpp @@ -22,6 +22,7 @@ #include "macros.h" #include "parser/ETSparser.h" #include "varbinder/ETSBinder.h" +#include "public/es2panda_lib.h" #include #include @@ -42,41 +43,44 @@ }()) // NOLINTEND(cppcoreguidelines-macro-usage) -namespace panda::es2panda { - class ASTVerifierTest : public testing::Test { public: ASTVerifierTest() { - allocator_ = std::make_unique(SpaceType::SPACE_TYPE_COMPILER); + impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + // NOLINTNEXTLINE(modernize-avoid-c-arrays) + char const *argv[] = {"../../../bin/es2panda test"}; + cfg_ = impl_->CreateConfig(1, argv); + allocator_ = new panda::ArenaAllocator(panda::SpaceType::SPACE_TYPE_COMPILER); } - ~ASTVerifierTest() override = default; - - static void SetUpTestCase() + ~ASTVerifierTest() override { - constexpr auto COMPILER_SIZE = operator""_MB(256ULL); - mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0); - PoolManager::Initialize(); + delete allocator_; + impl_->DestroyConfig(cfg_); } - ArenaAllocator *Allocator() + panda::ArenaAllocator *Allocator() { - return allocator_.get(); + return allocator_; } NO_COPY_SEMANTIC(ASTVerifierTest); NO_MOVE_SEMANTIC(ASTVerifierTest); -private: - std::unique_ptr allocator_; +protected: + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + es2panda_Impl const *impl_; + es2panda_Config *cfg_; + panda::ArenaAllocator *allocator_; + // NOLINTEND(misc-non-private-member-variables-in-classes) }; TEST_F(ASTVerifierTest, NullParent) { - compiler::ASTVerifier verifier {Allocator()}; - ir::StringLiteral empty_node; + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + panda::es2panda::ir::StringLiteral empty_node; - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("HasParent"); bool has_parent = verifier.Verify(&empty_node, checks); const auto &errors = verifier.GetErrors(); @@ -90,10 +94,10 @@ TEST_F(ASTVerifierTest, NullParent) TEST_F(ASTVerifierTest, NullType) { - compiler::ASTVerifier verifier {Allocator()}; - ir::StringLiteral empty_node; + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + panda::es2panda::ir::StringLiteral empty_node; - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("HasType"); bool has_type = verifier.Verify(&empty_node, checks); const auto &errors = verifier.GetErrors(); @@ -107,10 +111,10 @@ TEST_F(ASTVerifierTest, NullType) TEST_F(ASTVerifierTest, WithoutScope) { - compiler::ASTVerifier verifier {Allocator()}; - ir::StringLiteral empty_node; + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + panda::es2panda::ir::StringLiteral empty_node; - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("HasScope"); bool has_scope = verifier.Verify(&empty_node, checks); const auto &errors = verifier.GetErrors(); @@ -135,7 +139,7 @@ TEST_F(ASTVerifierTest, ScopeTest) local.SetScope(&scope); - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("HasScope"); bool is_ok = verifier.Verify(&ident, checks); @@ -159,7 +163,7 @@ TEST_F(ASTVerifierTest, ScopeNodeTest) local.SetScope(&scope); - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("VerifyScopeNode"); bool is_ok = verifier.Verify(&ident, checks); @@ -181,7 +185,7 @@ TEST_F(ASTVerifierTest, ArithmeticExpressionCorrect1) left.SetTsType(etschecker.GlobalIntType()); right.SetTsType(etschecker.GlobalIntType()); - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("CheckArithmeticExpression"); bool is_correct = verifier.Verify(arithmetic_expression.AsBinaryExpression(), checks); ASSERT_EQ(is_correct, true); @@ -210,7 +214,7 @@ TEST_F(ASTVerifierTest, ArithmeticExpressionCorrect2) left2.SetTsType(etschecker.GlobalIntType()); right2.SetTsType(etschecker.GlobalIntType()); - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("CheckArithmeticExpression"); bool is_correct = verifier.Verify(arithmetic_expression.AsBinaryExpression(), checks); ASSERT_EQ(is_correct, true); @@ -223,7 +227,7 @@ TEST_F(ASTVerifierTest, ArithmeticExpressionNegative1) auto program = panda::es2panda::parser::Program::NewProgram(Allocator()); auto parser = panda::es2panda::parser::ETSParser(&program, panda::es2panda::CompilerOptions {}); - const util::StringView left_param("1"); + const panda::es2panda::util::StringView left_param("1"); constexpr uint32_t RIGHT_PARAM = 1; auto left = panda::es2panda::ir::StringLiteral(left_param); auto right = panda::es2panda::ir::NumberLiteral(panda::es2panda::lexer::Number {RIGHT_PARAM}); @@ -233,7 +237,7 @@ TEST_F(ASTVerifierTest, ArithmeticExpressionNegative1) left.SetTsType(etschecker.GlobalETSStringLiteralType()); right.SetTsType(etschecker.GlobalIntType()); - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("CheckArithmeticExpression"); bool is_correct = verifier.Verify(arithmetic_expression.AsBinaryExpression(), checks); @@ -254,11 +258,720 @@ TEST_F(ASTVerifierTest, ArithmeticExpressionNegative2) left.SetTsType(etschecker.GlobalETSStringLiteralType()); right.SetTsType(etschecker.GlobalIntType()); - auto checks = compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; checks.insert("CheckArithmeticExpression"); bool is_correct = verifier.Verify(arithmetic_expression.AsBinaryExpression(), checks); ASSERT_EQ(is_correct, false); } -} // namespace panda::es2panda +constexpr char const *PRIVATE_PROTECTED_PUBLIC_TEST = + R"XXX( + class Base { + public a: int = 1; + protected b: int = 2; + private c: int = 3; + public publicMethod() { + this.a = 4; + this.protectedMethod(); + this.privateMethod(); + } + protected protectedMethod() { + this.b = 5; + this.publicMethod(); + this.privateMethod(); + } + private privateMethod() { + this.c = 6; + this.publicMethod(); + this.protectedMethod(); + } + } + class Derived extends Base { + foo () { + this.a = 7; + this.b = 8; + this.publicMethod(); + this.protectedMethod(); + } + } + function main(): void { + let base: Base = new Base(); + let a = base.a; + base.publicMethod(); + let derived1: Derived = new Derived(); + let b = derived1.a; + derived1.publicMethod(); + derived1.foo(); + let derived2: Base = new Derived(); + let c = derived2.a; + derived2.publicMethod(); + } + )XXX"; + +TEST_F(ASTVerifierTest, PrivateProtectedPublicAccessTestCorrect) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, PRIVATE_PROTECTED_PUBLIC_TEST, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + + ASSERT_EQ(is_correct, true); + ASSERT_EQ(errors.size(), 0); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative1) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + class Derived extends Base { + public b: int = this.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR MUST BE UNREACHABLE.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative2) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + function main(): void { + let base: Base = new Base(); + let a = base.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID base.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative3) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + class Derived extends Base {} + function main(): void { + let derived: Derived = new Derived(); + let a = derived.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative4) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + class Derived extends Base {} + function main(): void { + let derived: Base = new Derived(); + let a = derived.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative5) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + public privateMethod() { + this.a = 2; + } + } + function main(): void { + let base: Base = new Base(); + base.privateMethod(); + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[0] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[1] + ->AsClassElement() + ->Value() + ->AsFunctionExpression() + ->Function() + ->AsScriptFunction() + ->Body() + ->AsBlockStatement() + ->Statements()[1] + ->AsExpressionStatement() + ->GetExpression() + ->AsCallExpression() + ->Signature() + ->AddSignatureFlag(panda::es2panda::checker::SignatureFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID base.ID privateMethod"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative6) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + public privateMethod() { + this.a = 2; + } + } + class Derived extends Base {} + function main(): void { + let derived: Derived = new Derived(); + derived.privateMethod(); + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[0] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[1] + ->AsClassElement() + ->Value() + ->AsFunctionExpression() + ->Function() + ->AsScriptFunction() + ->Body() + ->AsBlockStatement() + ->Statements()[1] + ->AsExpressionStatement() + ->GetExpression() + ->AsCallExpression() + ->Signature() + ->AddSignatureFlag(panda::es2panda::checker::SignatureFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID privateMethod"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, PrivateAccessTestNegative7) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + public privateMethod() { + this.a = 2; + } + } + class Derived extends Base {} + function main(): void { + let derived: Base = new Derived(); + derived.privateMethod(); + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[0] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[1] + ->AsClassElement() + ->Value() + ->AsFunctionExpression() + ->Function() + ->AsScriptFunction() + ->Body() + ->AsBlockStatement() + ->Statements()[1] + ->AsExpressionStatement() + ->GetExpression() + ->AsCallExpression() + ->Signature() + ->AddSignatureFlag(panda::es2panda::checker::SignatureFlags::PRIVATE); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID privateMethod"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestCorrect) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class A { + public a: int = 1; + } + class B extends A { + public b: int = this.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + + ASSERT_EQ(is_correct, true); + ASSERT_EQ(errors.size(), 0); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestNegative1) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + function main(): void { + let base: Base = new Base(); + let a = base.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID base.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestNegative2) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + class Derived extends Base {} + function main(): void { + let derived: Derived = new Derived(); + let a = derived.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestNegative3) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + } + class Derived extends Base {} + function main(): void { + let derived: Base = new Derived(); + let a = derived.a; + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[1] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[0] + ->AsClassProperty() + ->AddModifier(panda::es2panda::ir::ModifierFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID a"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestNegative4) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + public protectedMethod() { + this.a = 2; + } + } + function main(): void { + let base: Base = new Base(); + base.protectedMethod(); + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[0] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[1] + ->AsClassElement() + ->Value() + ->AsFunctionExpression() + ->Function() + ->AsScriptFunction() + ->Body() + ->AsBlockStatement() + ->Statements()[1] + ->AsExpressionStatement() + ->GetExpression() + ->AsCallExpression() + ->Signature() + ->AddSignatureFlag(panda::es2panda::checker::SignatureFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID base.ID protectedMethod"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestNegative5) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + public protectedMethod() { + this.a = 2; + } + } + class Derived extends Base {} + function main(): void { + let derived: Derived = new Derived(); + derived.protectedMethod(); + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[0] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[1] + ->AsClassElement() + ->Value() + ->AsFunctionExpression() + ->Function() + ->AsScriptFunction() + ->Body() + ->AsBlockStatement() + ->Statements()[1] + ->AsExpressionStatement() + ->GetExpression() + ->AsCallExpression() + ->Signature() + ->AddSignatureFlag(panda::es2panda::checker::SignatureFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID protectedMethod"); + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, ProtectedAccessTestNegative6) +{ + panda::es2panda::compiler::ASTVerifier verifier {Allocator()}; + + char const *text = R"XXX( + class Base { + public a: int = 1; + public protectedMethod() { + this.a = 2; + } + } + class Derived extends Base {} + function main(): void { + let derived: Base = new Derived(); + derived.protectedMethod(); + } + )XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED); + + auto *ast = reinterpret_cast(impl_->ProgramAst(impl_->ContextProgram(ctx))); + + ast->AsBlockStatement() + ->Statements()[0] + ->AsClassDeclaration() + ->Definition() + ->AsClassDefinition() + ->Body()[1] + ->AsClassElement() + ->Value() + ->AsFunctionExpression() + ->Function() + ->AsScriptFunction() + ->Body() + ->AsBlockStatement() + ->Statements()[1] + ->AsExpressionStatement() + ->GetExpression() + ->AsCallExpression() + ->Signature() + ->AddSignatureFlag(panda::es2panda::checker::SignatureFlags::PROTECTED); + + auto checks = panda::es2panda::compiler::ASTVerifier::CheckSet {Allocator()->Adapter()}; + checks.insert("VerifyModifierAccessRecursive"); + bool is_correct = verifier.Verify(ast, checks); + const auto &errors = verifier.GetErrors(); + const auto [name, error] = errors[0]; + + ASSERT_EQ(is_correct, false); + ASSERT_EQ(errors.size(), 1); + ASSERT_EQ(error.message, "PROPERTY_NOT_VISIBLE_HERE: MEMBER_EXPR ID derived.ID protectedMethod"); + impl_->DestroyContext(ctx); +} diff --git a/ets2panda/util/declgenEts2Ts.cpp b/ets2panda/util/declgenEts2Ts.cpp index 0d88da3963a678344dafa8beee2e60a5ee35c11e..2cb69ab4becc27382d8b13dc2995da261e7e9480 100644 --- a/ets2panda/util/declgenEts2Ts.cpp +++ b/ets2panda/util/declgenEts2Ts.cpp @@ -180,6 +180,10 @@ void TSDeclGen::GenTypeNonNullish(const checker::Type *checker_type) GenObjectType(checker_type->AsETSObjectType()); return; } + if (checker_type->IsETSTypeParameter()) { + GenTypeParameterType(checker_type->AsETSTypeParameter()); + return; + } // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TYPE_CHECKS(typeFlag, typeName) \ @@ -314,6 +318,11 @@ void TSDeclGen::GenObjectType(const checker::ETSObjectType *object_type) } } +void TSDeclGen::GenTypeParameterType(const checker::ETSTypeParameter *type_param) +{ + Out(type_param->GetDeclNode()->Name()->Name()); +} + void TSDeclGen::GenTypeParameters(const ir::TSTypeParameterDeclaration *type_params) { if (type_params != nullptr) { diff --git a/ets2panda/util/declgenEts2Ts.h b/ets2panda/util/declgenEts2Ts.h index 0bed55d8273823841fd5b71aaf9f885753dc3fc2..bd3f36671b59fdb17b3a321f60557eafb08cd11b 100644 --- a/ets2panda/util/declgenEts2Ts.h +++ b/ets2panda/util/declgenEts2Ts.h @@ -50,6 +50,7 @@ private: void GenTypeNonNullish(const checker::Type *checker_type); void GenFunctionType(const checker::ETSFunctionType *function_type, const ir::MethodDefinition *method_def = nullptr); + void GenTypeParameterType(const checker::ETSTypeParameter *type_param); void GenObjectType(const checker::ETSObjectType *object_type); void GenEnumType(const checker::ETSEnumType *enum_type); diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index b1f4abe0f922c5c9d3b8254d34c7016f3ec9581a..dcdb21431e36bd9094a365a3271d1dfa2386c39c 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -17,6 +17,7 @@ #include "varbinder/privateBinding.h" #include "checker/types/ets/types.h" +#include "ir/astNode.h" #include "ir/base/classDefinition.h" #include "ir/base/classProperty.h" #include "ir/base/methodDefinition.h" @@ -25,6 +26,7 @@ #include "ir/base/spreadElement.h" #include "ir/expressions/arrayExpression.h" #include "ir/expressions/assignmentExpression.h" +#include "ir/expressions/callExpression.h" #include "ir/expressions/functionExpression.h" #include "ir/expressions/identifier.h" #include "ir/expressions/literals/numberLiteral.h" @@ -32,6 +34,7 @@ #include "ir/expressions/literals/booleanLiteral.h" #include "ir/expressions/literals/nullLiteral.h" #include "ir/expressions/objectExpression.h" +#include "ir/statements/returnStatement.h" #include "ir/statements/variableDeclaration.h" #include "ir/statements/variableDeclarator.h" #include "ir/module/importSpecifier.h" @@ -358,22 +361,6 @@ const ir::ScriptFunction *Helpers::GetContainingFunction(const ir::AstNode *node return nullptr; } -ir::AstNode *Helpers::FindAncestorGivenByType(ir::AstNode *node, ir::AstNodeType type) -{ - node = node->Parent(); - - while (node->Type() != type) { - if (node->Parent() != nullptr) { - node = node->Parent(); - continue; - } - - return nullptr; - } - - return node; -} - const ir::ClassDefinition *Helpers::GetClassDefiniton(const ir::ScriptFunction *node) { ASSERT(node->IsConstructor()); diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index bd32ed03a52f53ddab63ad9ec57bdb8bb4cf6f15..7cb949d639344fa26fcd09c8024b2cd1032ef9b1 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -45,6 +45,8 @@ class ClassProperty; class Identifier; class MethodDefinition; class AstNode; +class ReturnStatement; +class CallExpression; class ClassStaticBlock; class TSInterfaceDeclaration; class TSEnumDeclaration; @@ -84,7 +86,26 @@ public: static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node); static const ir::ScriptFunction *GetContainingConstructor(const ir::ClassProperty *node); - static ir::AstNode *FindAncestorGivenByType(ir::AstNode *node, ir::AstNodeType type); + + template > *, ir::AstNode *>, + std::conditional_t>, const ir::AstNode *, ir::AstNode *>>> + static U FindAncestorGivenByType(T node, ir::AstNodeType type) + { + U iter = node->Parent(); + + while (iter->Type() != type) { + if (iter->Parent() != nullptr) { + iter = iter->Parent(); + continue; + } + + return nullptr; + } + + return iter; + } static const checker::ETSObjectType *GetContainingObjectType(const ir::AstNode *node); static const ir::TSEnumDeclaration *GetContainingEnumDeclaration(const ir::AstNode *node); diff --git a/ets2panda/util/options.cpp b/ets2panda/util/options.cpp index 7df1a656742179fd1d83c758cbcbe2ad9612306c..ef9399af141efb46040113746838d7ebe48541ea 100644 --- a/ets2panda/util/options.cpp +++ b/ets2panda/util/options.cpp @@ -183,6 +183,10 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg skip_phases("skip-phases", "", "Phases to skip"); panda::PandArg dump_before_phases("dump-before-phases", "", "Generate program dump before running phases in the list"); + panda::PandArg dump_ets_src_before_phases( + "dump-ets-src-before-phases", "", "Generate program dump as ets source code before running phases in the list"); + panda::PandArg dump_ets_src_after_phases( + "dump-ets-src-after-phases", "", "Generate program dump as ets source code after running phases in the list"); panda::PandArg dump_after_phases("dump-after-phases", "", "Generate program dump after running phases in the list"); panda::PandArg arkts_config( @@ -219,7 +223,9 @@ bool Options::Parse(int argc, const char **argv) argparser_->Add(&plugins); argparser_->Add(&skip_phases); argparser_->Add(&dump_before_phases); + argparser_->Add(&dump_ets_src_before_phases); argparser_->Add(&dump_after_phases); + argparser_->Add(&dump_ets_src_after_phases); argparser_->Add(&arkts_config); argparser_->Add(&op_ts_decl_out); @@ -395,6 +401,12 @@ bool Options::Parse(int argc, const char **argv) } } + if ((dump_ets_src_before_phases.GetValue().size() + dump_ets_src_after_phases.GetValue().size() > 0) && + extension_ != es2panda::ScriptExtension::ETS) { + error_msg_ = "--dump-ets-src-* option is valid only with ETS extension"; + return false; + } + compiler_options_.ts_decl_out = op_ts_decl_out.GetValue(); compiler_options_.dump_asm = op_dump_assembly.GetValue(); compiler_options_.dump_ast = op_dump_ast.GetValue(); @@ -409,7 +421,9 @@ bool Options::Parse(int argc, const char **argv) compiler_options_.plugins = SplitToStringVector(plugins.GetValue()); compiler_options_.skip_phases = SplitToStringSet(skip_phases.GetValue()); compiler_options_.dump_before_phases = SplitToStringSet(dump_before_phases.GetValue()); + compiler_options_.dump_ets_src_before_phases = SplitToStringSet(dump_ets_src_before_phases.GetValue()); compiler_options_.dump_after_phases = SplitToStringSet(dump_after_phases.GetValue()); + compiler_options_.dump_ets_src_after_phases = SplitToStringSet(dump_ets_src_after_phases.GetValue()); return true; } diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index a5461d38876aeb7b7dc413e5d06a26aa83501281..7d9ec872341b1ca74988e47242ae4fdae9220205 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -143,6 +143,9 @@ void ETSBinder::ResolveReferenceForScope(ir::AstNode *const node, Scope *const s switch (node->Type()) { case ir::AstNodeType::IDENTIFIER: { auto *ident = node->AsIdentifier(); + if (ident->Variable() != nullptr) { + break; + } if (auto const res = scope->Find(ident->Name(), ResolveBindingOptions::ALL); res.variable != nullptr) { ident->SetVariable(res.variable); } @@ -483,8 +486,9 @@ bool ETSBinder::AddImportNamespaceSpecifiersToTopBindings(ir::AstNode *const spe std::unordered_set exported_names; for (auto item : ReExportImports()) { - if (import->ResolvedSource()->Str().Is( - item->GetProgramPath().Mutf8().substr(0, item->GetProgramPath().Mutf8().find_last_of('.')))) { + if (auto source = import->ResolvedSource()->Str().Mutf8(), + program = item->GetProgramPath().Mutf8().substr(0, item->GetProgramPath().Mutf8().find_last_of('.')); + source == program || (source + "/index") == program) { ir::StringLiteral dir_name(util::UString(util::StringView(item->GetProgramPath().Mutf8().substr( 0, item->GetProgramPath().Mutf8().find_last_of('/'))), Allocator()) @@ -590,8 +594,9 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(ir::AstNode *const specifier, if (var == nullptr) { for (auto item : ReExportImports()) { - if (import->ResolvedSource()->Str().Is( - item->GetProgramPath().Mutf8().substr(0, item->GetProgramPath().Mutf8().find_last_of('.')))) { + if (auto source = import->ResolvedSource()->Str().Mutf8(), + program = item->GetProgramPath().Mutf8().substr(0, item->GetProgramPath().Mutf8().find_last_of('.')); + source == program || (source + "/index") == program) { ir::StringLiteral dir_name(util::UString(util::StringView(item->GetProgramPath().Mutf8().substr( 0, item->GetProgramPath().Mutf8().find_last_of('/'))), Allocator()) @@ -638,7 +643,22 @@ ArenaVector ETSBinder::GetExternalProgram(const util::StringV const auto &ext_records = global_record_table_.Program()->ExternalSources(); auto record_res = [this, ext_records, source_name]() { auto res = ext_records.find(source_name); - return (res != ext_records.end()) ? res : ext_records.find(GetResolvedImportPath(source_name)); + + if (res != ext_records.end()) { + return res; + } + + if (res = ext_records.find({source_name.Mutf8() + "/index"}); res != ext_records.end()) { + return res; + } + + res = ext_records.find(GetResolvedImportPath(source_name)); + + if (res == ext_records.end()) { + res = ext_records.find(GetResolvedImportPath({source_name.Mutf8() + "/index"})); + } + + return res; }(); if (record_res == ext_records.end()) { ThrowError(import_path->Start(), "Cannot find import: " + std::string(source_name)); diff --git a/ets2panda/varbinder/scope.cpp b/ets2panda/varbinder/scope.cpp index f019a64f26d33635e819fa1de65ae12fdfe66c75..7d685b6aa3439e9acc1d4260e72e85fd14256729 100644 --- a/ets2panda/varbinder/scope.cpp +++ b/ets2panda/varbinder/scope.cpp @@ -753,6 +753,13 @@ Variable *ClassScope::AddBinding(ArenaAllocator *allocator, [[maybe_unused]] Var return nullptr; } + if (auto node = new_decl->Node(); + node->IsStatement() && + (node->AsStatement()->IsMethodDefinition() || node->IsClassProperty() || node->IsClassStaticBlock()) && + node->AsStatement()->AsClassElement()->Value() != nullptr) { + props.SetFlagsType(VariableFlags::INITIALIZED); + } + var->SetScope(this); var->AddFlag(props.GetFlags()); diff --git a/ets2panda/varbinder/scope.h b/ets2panda/varbinder/scope.h index c1c11520e3e6ff59618c14635b6bc1fe5159d3ca..9d17358ade99d6cd63454b05b81c76c4c91703bd 100644 --- a/ets2panda/varbinder/scope.h +++ b/ets2panda/varbinder/scope.h @@ -639,6 +639,11 @@ public: return instance_decl_scope_; } + const LocalScope *TypeAliasScope() const + { + return type_alias_scope_; + } + uint32_t GetAndIncrementAnonymousClassIdx() const { return anonymous_class_idx_++; diff --git a/ets2panda/varbinder/varbinder.cpp b/ets2panda/varbinder/varbinder.cpp index ba618099a9d60ff7f1102275c55501ea92060ef6..2db75b204b4d821b7b23765732cf7717e48dd13c 100644 --- a/ets2panda/varbinder/varbinder.cpp +++ b/ets2panda/varbinder/varbinder.cpp @@ -51,6 +51,7 @@ #include "ir/ts/tsFunctionType.h" #include "ir/ts/tsConstructorType.h" #include "ir/ts/tsTypeParameterDeclaration.h" +#include "ir/ts/tsTypeAliasDeclaration.h" #include "ir/ts/tsTypeReference.h" #include "ir/ts/tsInterfaceDeclaration.h" #include "ir/ets/etsNewClassInstanceExpression.h" @@ -465,6 +466,17 @@ void VarBinder::BuildCatchClause(ir::CatchClause *catch_clause_stmt) ResolveReference(catch_clause_stmt->Body()); } +void VarBinder::BuildTypeAliasDeclaration(ir::TSTypeAliasDeclaration *const type_alias_decl) +{ + if (type_alias_decl->TypeParams() != nullptr) { + const auto type_alias_scope = LexicalScope::Enter(this, type_alias_decl->TypeParams()->Scope()); + ResolveReferences(type_alias_decl); + return; + } + + ResolveReferences(type_alias_decl); +} + void VarBinder::AddCompilableFunction(ir::ScriptFunction *func) { if (func->IsArrow()) { @@ -602,6 +614,10 @@ void VarBinder::ResolveReference(ir::AstNode *child_node) BuildCatchClause(child_node->AsCatchClause()); break; } + case ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION: { + BuildTypeAliasDeclaration(child_node->AsTSTypeAliasDeclaration()); + break; + } default: { HandleCustomNodes(child_node); break; diff --git a/ets2panda/varbinder/varbinder.h b/ets2panda/varbinder/varbinder.h index b92e05a6f1dd764c984f711b94cd6c2a8c32cbbd..4d948a8b43df9da9c7d9d295df105a8ba69b7c66 100644 --- a/ets2panda/varbinder/varbinder.h +++ b/ets2panda/varbinder/varbinder.h @@ -221,6 +221,7 @@ protected: void BuildForInOfLoop(varbinder::LoopScope *loop_scope, ir::AstNode *left, ir::Expression *right, ir::Statement *body); void BuildCatchClause(ir::CatchClause *catch_clause_stmt); + void BuildTypeAliasDeclaration(ir::TSTypeAliasDeclaration *type_alias_decl); void ResolveReference(ir::AstNode *child_node); void ResolveReferences(const ir::AstNode *parent); void VisitScriptFunctionWithPotentialTypeParams(ir::ScriptFunction *func);