diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index b479a13bd989908b2f74c152dd23e40e00684bd1..a223d13ba1d386581b69dfcfa3624769bd59e595 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -21,6 +21,7 @@ #include "checker/ets/typeRelationContext.h" #include "checker/types/globalTypesHolder.h" #include "checker/types/ets/etsTupleType.h" +#include "checker/types/ets/etsAsyncFuncReturnType.h" namespace ark::es2panda::checker { @@ -162,13 +163,26 @@ checker::Type *ETSAnalyzer::Check(ir::MethodDefinition *node) const } } + if (IsAsyncMethod(node)) { + if (scriptFunc->ReturnTypeAnnotation() != nullptr) { + auto *asyncFuncReturnType = scriptFunc->Signature()->ReturnType(); + + if (!asyncFuncReturnType->IsETSObjectType() || + asyncFuncReturnType->AsETSObjectType()->GetOriginalBaseType() != checker->GlobalBuiltinPromiseType()) { + checker->ThrowTypeError("Return type of async function must be 'Promise'.", scriptFunc->Start()); + } + } + + ComposeAsyncImplMethod(checker, node); + } + DoBodyTypeChecking(checker, node, scriptFunc); CheckPredefinedMethodReturnType(checker, scriptFunc); checker->CheckOverride(node->TsType()->AsETSFunctionType()->FindSignature(node->Function())); - for (auto *it : node->Overloads()) { - it->Check(checker); + for (auto *overload : node->Overloads()) { + overload->Check(checker); } if (scriptFunc->IsRethrowing()) { @@ -356,10 +370,18 @@ checker::Type *ETSAnalyzer::Check(ir::ETSLaunchExpression *expr) const // Launch expression returns a Promise type, so we need to insert the expression's type // as type parameter for the Promise class. - auto *exprType = - expr->expr_->TsType()->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE) && !expr->expr_->TsType()->IsETSVoidType() - ? checker->PrimitiveTypeAsETSBuiltinType(expr->expr_->TsType()) - : expr->expr_->TsType(); + auto exprType = [&checker](auto *tsType) { + if (tsType->IsETSVoidType()) { + return checker->GlobalETSUndefinedType(); + } + + if (tsType->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE)) { + return checker->PrimitiveTypeAsETSBuiltinType(tsType); + } + + return tsType; + }(expr->expr_->TsType()); + checker::Substitution *substitution = checker->NewSubstitution(); ASSERT(launchPromiseType->TypeArguments().size() == 1); checker::ETSChecker::EmplaceSubstituted( diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index e9263c2dd896a231a3c37262649e23682f3e5dd4..571a3d50cef8c2851e1189771e62b23169aaafea 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.cpp +++ b/ets2panda/checker/ETSAnalyzerHelpers.cpp @@ -14,6 +14,7 @@ */ #include "ETSAnalyzerHelpers.h" +#include "checker/types/ets/etsAsyncFuncReturnType.h" namespace ark::es2panda::checker { void CheckExtensionIsShadowedInCurrentClassOrInterface(checker::ETSChecker *checker, checker::ETSObjectType *objType, @@ -83,13 +84,7 @@ void DoBodyTypeChecking(ETSChecker *checker, ir::MethodDefinition *node, ir::Scr checker->ThrowTypeError("Native, Abstract and Declare methods cannot have body.", scriptFunc->Body()->Start()); } - if (scriptFunc->IsAsyncFunc()) { - auto *retType = scriptFunc->Signature()->ReturnType(); - if (!retType->IsETSObjectType() || - retType->AsETSObjectType()->GetOriginalBaseType() != checker->GlobalBuiltinPromiseType()) { - checker->ThrowTypeError("Return type of async function must be 'Promise'.", scriptFunc->Start()); - } - } else if (scriptFunc->HasBody() && !scriptFunc->IsExternal()) { + if (!scriptFunc->IsAsyncFunc() && scriptFunc->HasBody() && !scriptFunc->IsExternal()) { checker::ScopeContext scopeCtx(checker, scriptFunc->Scope()); checker::SavedCheckerContext savedContext(checker, checker->Context().Status(), checker->Context().ContainingClass()); @@ -111,6 +106,10 @@ void DoBodyTypeChecking(ETSChecker *checker, ir::MethodDefinition *node, ir::Scr scriptFunc->Body()->Check(checker); if (scriptFunc->ReturnTypeAnnotation() == nullptr) { + if (scriptFunc->IsAsyncImplFunc()) { + ComposeAsyncImplFuncReturnType(checker, scriptFunc); + } + for (auto &returnStatement : scriptFunc->ReturnStatements()) { returnStatement->SetReturnType(checker, scriptFunc->Signature()->ReturnType()); } @@ -120,6 +119,54 @@ void DoBodyTypeChecking(ETSChecker *checker, ir::MethodDefinition *node, ir::Scr } } +void ComposeAsyncImplFuncReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc) +{ + const auto &promiseGlobal = checker->GlobalBuiltinPromiseType()->AsETSObjectType(); + auto promiseType = + promiseGlobal->Instantiate(checker->Allocator(), checker->Relation(), checker->GetGlobalTypesHolder()) + ->AsETSObjectType(); + promiseType->AddTypeFlag(checker::TypeFlag::GENERIC); + promiseType->TypeArguments().clear(); + promiseType->TypeArguments().emplace_back(scriptFunc->Signature()->ReturnType()); + + auto *objectId = + checker->AllocNode(compiler::Signatures::BUILTIN_OBJECT_CLASS, checker->Allocator()); + objectId->SetReference(); + checker->VarBinder()->AsETSBinder()->LookupTypeReference(objectId, false); + auto *returnType = checker->AllocNode( + checker->AllocNode(objectId, nullptr, nullptr)); + objectId->SetParent(returnType->Part()); + returnType->Part()->SetParent(returnType); + returnType->SetTsType( + checker->Allocator()->New(checker->Allocator(), checker->Relation(), promiseType)); + returnType->Check(checker); + scriptFunc->Signature()->SetReturnType(returnType->TsType()); +} + +void ComposeAsyncImplMethod(ETSChecker *checker, ir::MethodDefinition *node) +{ + auto *classDef = node->Parent()->AsClassDefinition(); + auto *scriptFunc = node->Function(); + ir::MethodDefinition *implMethod = checker->CreateAsyncProxy(node, classDef); + + implMethod->Check(checker); + node->SetAsyncPairMethod(implMethod); + + if (scriptFunc->Signature()->HasSignatureFlag(SignatureFlags::NEED_RETURN_TYPE)) { + node->Function()->Signature()->SetReturnType( + implMethod->Function()->Signature()->ReturnType()->AsETSAsyncFuncReturnType()->PromiseType()); + scriptFunc->Signature()->RemoveSignatureFlag(SignatureFlags::NEED_RETURN_TYPE); + } + + if (node->Function()->IsOverload()) { + auto *baseOverloadImplMethod = node->BaseOverloadMethod()->AsyncPairMethod(); + implMethod->Function()->Id()->SetVariable(baseOverloadImplMethod->Function()->Id()->Variable()); + baseOverloadImplMethod->AddOverload(implMethod); + } else { + classDef->Body().push_back(implMethod); + } +} + void CheckPredefinedMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc) { auto const &position = scriptFunc->Start(); @@ -148,18 +195,27 @@ void CheckPredefinedMethodReturnType(ETSChecker *checker, ir::ScriptFunction *sc } } -void CheckIteratorMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc, - const lexer::SourcePosition &position, const std::string &methodName) +static bool HasIteratorInterface(ETSObjectType const *const objectType) { - const auto hasIteratorInterface = [](ETSObjectType const *const objectType) -> bool { - for (const auto *const interface : objectType->Interfaces()) { - if (interface->Name().Is(ir::ITERATOR_INTERFACE_NAME)) { + auto const hasIteratorInterfaceImpl = [](ETSObjectType const *const checkType, + auto &&iteratorInterfaceImpl) -> bool { + if (checkType->Name().Is(ir::ITERATOR_INTERFACE_NAME)) { + return true; + } + for (const auto *const interface : checkType->Interfaces()) { + if (iteratorInterfaceImpl(interface, iteratorInterfaceImpl)) { return true; } } return false; }; + return hasIteratorInterfaceImpl(objectType, hasIteratorInterfaceImpl); +} + +void CheckIteratorMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc, + const lexer::SourcePosition &position, const std::string &methodName) +{ const auto *returnType = scriptFunc->Signature()->ReturnType(); if (returnType == nullptr) { @@ -170,13 +226,13 @@ void CheckIteratorMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scri returnType = checker->GetApparentType(returnType->AsETSTypeParameter()->GetConstraintType()); } - if (returnType->IsETSObjectType() && hasIteratorInterface(returnType->AsETSObjectType())) { + if (returnType->IsETSObjectType() && HasIteratorInterface(returnType->AsETSObjectType())) { return; } while (returnType->IsETSObjectType() && returnType->AsETSObjectType()->SuperType() != nullptr) { returnType = returnType->AsETSObjectType()->SuperType(); - if (returnType->IsETSObjectType() && hasIteratorInterface(returnType->AsETSObjectType())) { + if (returnType->IsETSObjectType() && HasIteratorInterface(returnType->AsETSObjectType())) { return; } } @@ -525,6 +581,7 @@ void InferReturnType(ETSChecker *checker, ir::ScriptFunction *containingFunc, ch containingFunc->Signature()->SetReturnType(funcReturnType); containingFunc->Signature()->RemoveSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE); + containingFunc->Signature()->AddSignatureFlag(checker::SignatureFlags::INFERRED_RETURN_TYPE); checker->VarBinder()->AsETSBinder()->BuildFunctionName(containingFunc); if (stArgument != nullptr && stArgument->IsObjectExpression()) { @@ -544,15 +601,6 @@ void ProcessReturnStatements(ETSChecker *checker, ir::ScriptFunction *containing st->Start()); } } else { - // previous return statement(s) don't have any value - if (funcReturnType->IsETSVoidType() || funcReturnType == checker->GlobalVoidType()) { - checker->ThrowTypeError("All return statements in the function should be empty or have a value.", - stArgument->Start()); - } - - const auto name = containingFunc->Scope()->InternalName().Mutf8(); - CheckArgumentVoidType(funcReturnType, checker, name, st); - if (stArgument->IsObjectExpression()) { stArgument->AsObjectExpression()->SetPreferredType(funcReturnType); } @@ -562,6 +610,16 @@ void ProcessReturnStatements(ETSChecker *checker, ir::ScriptFunction *containing } checker::Type *argumentType = stArgument->Check(checker); + + // previous return statement(s) don't have any value + if (funcReturnType->IsETSVoidType() && !argumentType->IsETSVoidType()) { + checker->ThrowTypeError("All return statements in the function should be empty or have a value.", + stArgument->Start()); + } + + const auto name = containingFunc->Scope()->InternalName().Mutf8(); + CheckArgumentVoidType(funcReturnType, checker, name, st); + // remove CONSTANT type modifier if exists if (argumentType->HasTypeFlag(checker::TypeFlag::CONSTANT)) { argumentType = diff --git a/ets2panda/checker/ETSAnalyzerHelpers.h b/ets2panda/checker/ETSAnalyzerHelpers.h index 2f9e5e08259ec869b90e901e73b0bec5f9fab6fd..a9e63b5a7a9c72f88c58eab0c6dcd712f908c9ad 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.h +++ b/ets2panda/checker/ETSAnalyzerHelpers.h @@ -33,6 +33,8 @@ void CheckExtensionIsShadowedByMethod(checker::ETSChecker *checker, checker::ETS ir::ScriptFunction *extensionFunc, checker::Signature *signature); void CheckExtensionMethod(checker::ETSChecker *checker, ir::ScriptFunction *extensionFunc, ir::MethodDefinition *node); void DoBodyTypeChecking(ETSChecker *checker, ir::MethodDefinition *node, ir::ScriptFunction *scriptFunc); +void ComposeAsyncImplFuncReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc); +void ComposeAsyncImplMethod(ETSChecker *checker, ir::MethodDefinition *node); void CheckPredefinedMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc); void CheckIteratorMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc, const lexer::SourcePosition &position, const std::string &methodName); @@ -61,6 +63,7 @@ void InferReturnType(ETSChecker *checker, ir::ScriptFunction *containingFunc, ch ir::Expression *stArgument); void ProcessReturnStatements(ETSChecker *checker, ir::ScriptFunction *containingFunc, checker::Type *&funcReturnType, ir::ReturnStatement *st, ir::Expression *stArgument); +bool IsAsyncMethod(ir::AstNode *node); } // namespace ark::es2panda::checker #endif // ES2PANDA_CHECKER_ETSANALYZERHELPERS_H diff --git a/ets2panda/checker/ETSchecker.cpp b/ets2panda/checker/ETSchecker.cpp index 47db24597297863ee63d7c5ec0ea580112c938cb..d4e2e4630d074bfcaac9992360ab6a43cccf0200 100644 --- a/ets2panda/checker/ETSchecker.cpp +++ b/ets2panda/checker/ETSchecker.cpp @@ -213,7 +213,7 @@ void ETSChecker::CheckProgram(parser::Program *program, bool runAnalysis) for (auto &[_, extPrograms] : program->ExternalSources()) { (void)_; for (auto *extProg : extPrograms) { - CheckProgram(extProg); + CheckProgram(extProg, VarBinder()->IsGenStdLib()); } } diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 54094ac3827375854a8667a6a37b7fc93cbac888..4ac4fb8b9f6aefc1e1a8301251268b5633396531 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -247,7 +247,7 @@ public: // Arithmetic Type *NegateNumericType(Type *type, ir::Expression *node); Type *BitwiseNegateNumericType(Type *type, ir::Expression *node); - bool CheckBinaryOperatorForBigInt(Type *left, Type *right, ir::Expression *expr, lexer::TokenType op); + bool CheckBinaryOperatorForBigInt(Type *left, Type *right, lexer::TokenType op); void CheckBinaryPlusMultDivOperandsForUnionType(const Type *leftType, const Type *rightType, const ir::Expression *left, const ir::Expression *right); std::tuple CheckBinaryOperator(ir::Expression *left, ir::Expression *right, ir::Expression *expr, @@ -300,9 +300,12 @@ public: bool NeedTypeInference(const ir::ScriptFunction *lambda); std::vector FindTypeInferenceArguments(const ArenaVector &arguments); void InferTypesForLambda(ir::ScriptFunction *lambda, ir::ETSFunctionType *calleeType); - void HandleLambdaTypeInfer(ir::AstNode *typeAnn, ir::ScriptFunction *const lambda); bool TypeInference(Signature *signature, const ArenaVector &arguments, TypeRelationFlag flags = TypeRelationFlag::NONE); + bool CheckLambdaTypeAnnotation(ir::AstNode *typeAnnotation, ir::ArrowFunctionExpression *arrowFuncExpr, + Type *parameterType, TypeRelationFlag flags); + bool CheckLambdaInvocable(ir::AstNode *typeAnnotation, ir::ArrowFunctionExpression *arrowFuncExpr, + Type *parameterType, TypeRelationFlag flags); bool CheckLambdaAssignable(ir::Expression *param, ir::ScriptFunction *lambda); bool CheckLambdaAssignableUnion(ir::AstNode *typeAnn, ir::ScriptFunction *lambda); bool IsCompatibleTypeArgument(ETSTypeParameter *typeParam, Type *typeArgument, const Substitution *substitution); @@ -315,7 +318,6 @@ public: return Allocator()->New(*src); } static void EmplaceSubstituted(Substitution *substitution, ETSTypeParameter *tparam, Type *typeArg); - ArenaVector CreateTypeForTypeParameters(ir::TSTypeParameterDeclaration const *typeParams); [[nodiscard]] bool EnhanceSubstitutionForType(const ArenaVector &typeParams, Type *paramType, Type *argumentType, Substitution *substitution); [[nodiscard]] bool EnhanceSubstitutionForObject(const ArenaVector &typeParams, ETSObjectType *paramType, @@ -324,6 +326,14 @@ public: Type *argumentType, Substitution *substitution); [[nodiscard]] bool EnhanceSubstitutionForArray(const ArenaVector &typeParams, ETSArrayType *paramType, Type *argumentType, Substitution *substitution); + [[nodiscard]] bool EnhanceSubstitutionForGenericType(const ArenaVector &typeParams, const Type *argType, + const Type *paramType, Substitution *substitution); + ArenaVector GetSourceParameters(const ETSObjectType *object, const Type *paramType, + const ArenaVector &requiredOrder); + [[nodiscard]] static bool HasTypeArgsOfObject(Type *argType, Type *paramType); + [[nodiscard]] bool InsertTypeIntoSubstitution(const ArenaVector &typeParams, const Type *typeParam, + const size_t index, Substitution *substitution, Type *objectParam); + ArenaVector CreateTypeForTypeParameters(ir::TSTypeParameterDeclaration const *typeParams); Signature *ValidateParameterlessConstructor(Signature *signature, const lexer::SourcePosition &pos, TypeRelationFlag flags); Signature *CollectParameterlessConstructor(ArenaVector &signatures, const lexer::SourcePosition &pos, @@ -478,7 +488,7 @@ public: bool IsNullLikeOrVoidExpression(const ir::Expression *expr) const; bool IsConstantExpression(ir::Expression *expr, Type *type); void ValidateUnaryOperatorOperand(varbinder::Variable *variable); - void InferAliasLambdaType(ir::TypeNode *localTypeAnnotation, ir::Expression *init); + void InferAliasLambdaType(ir::TypeNode *localTypeAnnotation, ir::ArrowFunctionExpression *init); bool TestUnionType(Type *type, TypeFlag test); bool CheckPossibilityPromotion(Type *left, Type *right, TypeFlag test); std::tuple ApplyBinaryOperatorPromotion(Type *left, Type *right, TypeFlag test, @@ -694,15 +704,17 @@ private: [[noreturn]] void ThrowError(ir::Identifier *ident); void WrongContextErrorClassifyByType(ir::Identifier *ident, varbinder::Variable *resolved); void CheckEtsFunctionType(ir::Identifier *ident, ir::Identifier const *id, ir::TypeNode const *annotation); - [[noreturn]] void NotResolvedError(ir::Identifier *ident, const varbinder::Variable *classVar, + [[noreturn]] void NotResolvedError(ir::Identifier *const ident, const varbinder::Variable *classVar, const ETSObjectType *classType); - void ValidateCallExpressionIdentifier(ir::Identifier *ident, Type *type); - void ValidateNewClassInstanceIdentifier(ir::Identifier *ident, varbinder::Variable *resolved); - void ValidateMemberIdentifier(ir::Identifier *ident, varbinder::Variable *resolved, Type *type); - void ValidatePropertyOrDeclaratorIdentifier(ir::Identifier *ident, varbinder::Variable *resolved); - void ValidateAssignmentIdentifier(ir::Identifier *ident, varbinder::Variable *resolved, Type *type); - bool ValidateBinaryExpressionIdentifier(ir::Identifier *ident, Type *type); - void ValidateGetterSetter(const ir::MemberExpression *memberExpr, const varbinder::LocalVariable *prop, + void ValidateCallExpressionIdentifier(ir::Identifier *const ident, varbinder::Variable *const resolved, + Type *const type); + void ValidateNewClassInstanceIdentifier(ir::Identifier *const ident, varbinder::Variable *const resolved); + void ValidateMemberIdentifier(ir::Identifier *const ident, varbinder::Variable *const resolved, Type *const type); + void ValidatePropertyOrDeclaratorIdentifier(ir::Identifier *const ident, varbinder::Variable *const resolved); + void ValidateAssignmentIdentifier(ir::Identifier *const ident, varbinder::Variable *const resolved, + Type *const type); + bool ValidateBinaryExpressionIdentifier(ir::Identifier *const ident, Type *const type); + void ValidateGetterSetter(const ir::MemberExpression *const memberExpr, const varbinder::LocalVariable *const prop, PropertySearchFlags searchFlag); void ValidateVarDeclaratorOrClassProperty(const ir::MemberExpression *memberExpr, varbinder::LocalVariable *prop); void ResolveMemberReferenceValidate(varbinder::LocalVariable *prop, PropertySearchFlags searchFlag, diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index bebe4bec0a4a2fd5f3912f567685fcb241185529..916465bd72f59b46c14ec63fabcbe8b1e48430f9 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -134,7 +134,7 @@ Type *ETSChecker::HandleRelationOperationOnTypes(Type *left, Type *right, lexer: return PerformRelationOperationOnTypes(left, right, operationType); } -bool ETSChecker::CheckBinaryOperatorForBigInt(Type *left, Type *right, ir::Expression *expr, lexer::TokenType op) +bool ETSChecker::CheckBinaryOperatorForBigInt(Type *left, Type *right, lexer::TokenType op) { if ((left == nullptr) || (right == nullptr)) { return false; @@ -148,16 +148,11 @@ bool ETSChecker::CheckBinaryOperatorForBigInt(Type *left, Type *right, ir::Expre return false; } - if (expr->IsBinaryExpression()) { - ir::BinaryExpression *be = expr->AsBinaryExpression(); - if (be->OperatorType() == lexer::TokenType::PUNCTUATOR_STRICT_EQUAL) { - // Handle strict comparison as normal comparison for bigint objects - be->SetOperator(lexer::TokenType::PUNCTUATOR_EQUAL); - } - } - switch (op) { case lexer::TokenType::PUNCTUATOR_EQUAL: + case lexer::TokenType::PUNCTUATOR_NOT_EQUAL: + case lexer::TokenType::PUNCTUATOR_STRICT_EQUAL: + case lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL: case lexer::TokenType::KEYW_INSTANCEOF: // This is handled in the main CheckBinaryOperator function return false; @@ -165,10 +160,6 @@ bool ETSChecker::CheckBinaryOperatorForBigInt(Type *left, Type *right, ir::Expre break; } - // Remove const flag - currently there are no compile time operations for bigint - left->RemoveTypeFlag(TypeFlag::CONSTANT); - right->RemoveTypeFlag(TypeFlag::CONSTANT); - return true; } @@ -683,7 +674,7 @@ std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, operationType < lexer::TokenType::PUNCTUATOR_ARROW) && !forcePromotion; - if (CheckBinaryOperatorForBigInt(leftType, rightType, expr, operationType)) { + if (CheckBinaryOperatorForBigInt(leftType, rightType, operationType)) { switch (operationType) { case lexer::TokenType::PUNCTUATOR_GREATER_THAN: case lexer::TokenType::PUNCTUATOR_LESS_THAN: @@ -693,7 +684,7 @@ std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, default: return {leftType, rightType}; } - }; + } auto checkMap = GetCheckMap(); if (checkMap.find(operationType) != checkMap.end()) { diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 1dcdf7966530c852c67883d7e31ea8c62197b107..0818ca6a30f57195b2a2007d71f6d419a5b6efb4 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -70,6 +70,79 @@ bool ETSChecker::IsCompatibleTypeArgument(ETSTypeParameter *typeParam, Type *typ return Relation()->IsSupertypeOf(constraint, typeArgument); } +bool ETSChecker::HasTypeArgsOfObject(Type *argType, Type *paramType) +{ + return paramType->IsETSObjectType() && argType->IsETSObjectType() && + !argType->AsETSObjectType()->TypeArguments().empty() && + !paramType->AsETSObjectType()->TypeArguments().empty(); +} + +bool ETSChecker::InsertTypeIntoSubstitution(const ArenaVector &typeParams, const Type *typeParam, + const size_t index, Substitution *substitution, Type *objectParam) +{ + // Check if the type parameter is in the signature, and the type argument is not already in the return vector + if (typeParams.size() > index && + IsCompatibleTypeArgument(typeParams[index]->AsETSTypeParameter(), objectParam, substitution) && + std::find(typeParams.begin(), typeParams.end(), typeParam) != typeParams.end()) { + substitution->emplace(typeParams[index]->AsETSTypeParameter(), objectParam); + return true; + } + + return false; +} + +ArenaVector ETSChecker::GetSourceParameters(const ETSObjectType *object, const Type *paramType, + const ArenaVector &requiredOrder) +{ + ArenaVector retVal(Allocator()->Adapter()); + + if (!object->GetDeclNode()->IsClassDefinition()) { + return retVal; + } + + const auto params = paramType->AsETSObjectType()->TypeArguments(); + + for (const auto it : requiredOrder) { + bool found = false; + + for (size_t i = 0; i < params.size(); i++) { + if (params[i] == it) { + retVal.push_back(object->TypeArguments()[i]); + found = true; + break; + } + } + + if (!found) { + retVal.push_back(nullptr); + } + } + + return retVal; +} + +bool ETSChecker::EnhanceSubstitutionForGenericType(const ArenaVector &typeParams, const Type *argType, + const Type *paramType, Substitution *substitution) +{ + const auto paramTypeArgs = paramType->AsETSObjectType()->TypeArguments(); + const auto objectParams = GetSourceParameters(argType->AsETSObjectType(), paramType, typeParams); + + bool res = true; + for (size_t j = 0; j < paramTypeArgs.size() && res; ++j) { + if (objectParams.size() <= j) { + res = false; + break; + } + if (objectParams[j] == nullptr) { + continue; + } + + res = InsertTypeIntoSubstitution(typeParams, paramTypeArgs[j], j, substitution, objectParams[j]); + } + + return res; +} + /* A very rough and imprecise partial type inference */ bool ETSChecker::EnhanceSubstitutionForType(const ArenaVector &typeParams, Type *paramType, Type *argumentType, Substitution *substitution) @@ -99,6 +172,10 @@ bool ETSChecker::EnhanceSubstitutionForType(const ArenaVector &typeParam return EnhanceSubstitutionForUnion(typeParams, paramType->AsETSUnionType(), argumentType, substitution); } if (paramType->IsETSObjectType()) { + if (HasTypeArgsOfObject(argumentType, paramType) && + EnhanceSubstitutionForGenericType(typeParams, argumentType, paramType, substitution)) { + return true; + } return EnhanceSubstitutionForObject(typeParams, paramType->AsETSObjectType(), argumentType, substitution); } if (paramType->IsETSArrayType()) { @@ -112,12 +189,10 @@ bool ETSChecker::EnhanceSubstitutionForUnion(const ArenaVector &typePara Type *argumentType, Substitution *substitution) { if (!argumentType->IsETSUnionType()) { - for (auto *ctype : paramUn->ConstituentTypes()) { - if (!EnhanceSubstitutionForType(typeParams, ctype, argumentType, substitution)) { - return false; - } - } - return true; + return std::any_of(paramUn->ConstituentTypes().begin(), paramUn->ConstituentTypes().end(), + [this, typeParams, argumentType, substitution](Type *ctype) { + return EnhanceSubstitutionForType(typeParams, ctype, argumentType, substitution); + }); } auto *const argUn = argumentType->AsETSUnionType(); @@ -161,8 +236,8 @@ bool ETSChecker::EnhanceSubstitutionForObject(const ArenaVector &typePar return true; // don't attempt anything fancy for now } bool res = true; - for (size_t ix = 0; ix < argObjType->TypeArguments().size(); ix++) { - res &= enhance(paramObjType->TypeArguments()[ix], argObjType->TypeArguments()[ix]); + for (size_t i = 0; i < argObjType->TypeArguments().size(); i++) { + res &= enhance(paramObjType->TypeArguments()[i], argObjType->TypeArguments()[i]); } return res; } @@ -3092,9 +3167,15 @@ ir::MethodDefinition *ETSChecker::CreateAsyncImplMethod(ir::MethodDefinition *as modifiers &= ~ir::ModifierFlags::ASYNC; ir::ScriptFunction *asyncFunc = asyncMethod->Function(); ir::ScriptFunctionFlags flags = ir::ScriptFunctionFlags::METHOD; + if (asyncFunc->IsProxy()) { flags |= ir::ScriptFunctionFlags::PROXY; } + + if (asyncFunc->HasReturnStatement()) { + flags |= ir::ScriptFunctionFlags::HAS_RETURN; + } + asyncMethod->AddModifier(ir::ModifierFlags::NATIVE); asyncFunc->AddModifier(ir::ModifierFlags::NATIVE); // Create async_impl method copied from CreateInvokeFunction @@ -3105,31 +3186,39 @@ ir::MethodDefinition *ETSChecker::CreateAsyncImplMethod(ir::MethodDefinition *as // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) varbinder::FunctionParamScope *paramScope = CopyParams(asyncFunc->Params(), params); - // Set impl method return type "Object" because it may return Promise as well as Promise parameter's type - auto *objectId = AllocNode(compiler::Signatures::BUILTIN_OBJECT_CLASS, Allocator()); - objectId->SetReference(); - VarBinder()->AsETSBinder()->LookupTypeReference(objectId, false); - auto *returnTypeAnn = - AllocNode(AllocNode(objectId, nullptr, nullptr)); - objectId->SetParent(returnTypeAnn->Part()); - returnTypeAnn->Part()->SetParent(returnTypeAnn); - auto *asyncFuncRetTypeAnn = asyncFunc->ReturnTypeAnnotation(); - auto *promiseType = [this](ir::TypeNode *type) { - if (type != nullptr) { - return type->GetType(this)->AsETSObjectType(); - } - - return GlobalBuiltinPromiseType()->AsETSObjectType(); - }(asyncFuncRetTypeAnn); - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - auto *retType = Allocator()->New(Allocator(), Relation(), promiseType); - returnTypeAnn->SetTsType(retType); + ir::ETSTypeReference *returnTypeAnn = nullptr; + + if (!asyncFunc->Signature()->HasSignatureFlag(SignatureFlags::NEED_RETURN_TYPE)) { + // Set impl method return type "Object" because it may return Promise as well as Promise parameter's type + auto *objectId = AllocNode(compiler::Signatures::BUILTIN_OBJECT_CLASS, Allocator()); + objectId->SetReference(); + VarBinder()->AsETSBinder()->LookupTypeReference(objectId, false); + returnTypeAnn = + AllocNode(AllocNode(objectId, nullptr, nullptr)); + objectId->SetParent(returnTypeAnn->Part()); + returnTypeAnn->Part()->SetParent(returnTypeAnn); + auto *asyncFuncRetTypeAnn = asyncFunc->ReturnTypeAnnotation(); + auto *promiseType = [this](ir::TypeNode *type) { + if (type != nullptr) { + return type->GetType(this)->AsETSObjectType(); + } - ir::MethodDefinition *implMethod = + return GlobalBuiltinPromiseType()->AsETSObjectType(); + }(asyncFuncRetTypeAnn); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + auto *retType = Allocator()->New(Allocator(), Relation(), promiseType); + returnTypeAnn->SetTsType(retType); + } + + ir::MethodDefinition *implMethod = CreateMethod(implName.View(), modifiers, flags, std::move(params), paramScope, returnTypeAnn, body); asyncFunc->SetBody(nullptr); - returnTypeAnn->SetParent(implMethod->Function()); + + if (returnTypeAnn != nullptr) { + returnTypeAnn->SetParent(implMethod->Function()); + } + + implMethod->Function()->AddFlag(ir::ScriptFunctionFlags::ASYNC_IMPL); implMethod->SetParent(asyncMethod->Parent()); return implMethod; } diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index df03042cdc3c23bb1193896abf1aec4144369fe5..2d8f61588fe12a9c9c0bf5ea7c78d8019c8029bf 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -18,10 +18,7 @@ #include "parser/ETSparser.h" #include "checker/types/ets/etsTupleType.h" -#include "checker/ets/narrowingWideningConverter.h" #include "checker/ets/typeRelationContext.h" -#include "checker/ets/boxingConverter.h" -#include "checker/ets/unboxingConverter.h" #include "checker/ETSchecker.h" #include "checker/types/globalTypesHolder.h" @@ -561,9 +558,11 @@ checker::Type *ETSChecker::CheckArrayElements(ir::Identifier *ident, ir::ArrayEx return annotationType; } -void ETSChecker::InferAliasLambdaType(ir::TypeNode *localTypeAnnotation, ir::Expression *init) +void ETSChecker::InferAliasLambdaType(ir::TypeNode *localTypeAnnotation, ir::ArrowFunctionExpression *init) { - if (localTypeAnnotation != nullptr && localTypeAnnotation->IsETSTypeReference()) { + ASSERT(localTypeAnnotation != nullptr); + + if (localTypeAnnotation->IsETSTypeReference()) { bool isAnnotationTypeAlias = true; while (localTypeAnnotation->IsETSTypeReference() && isAnnotationTypeAlias) { auto *node = localTypeAnnotation->AsETSTypeReference() @@ -581,9 +580,8 @@ void ETSChecker::InferAliasLambdaType(ir::TypeNode *localTypeAnnotation, ir::Exp } } - if (localTypeAnnotation != nullptr && localTypeAnnotation->IsETSFunctionType() && - init->IsArrowFunctionExpression()) { - auto *const arrowFuncExpr = init->AsArrowFunctionExpression(); + if (localTypeAnnotation->IsETSFunctionType()) { + auto *const arrowFuncExpr = init; ir::ScriptFunction *const lambda = arrowFuncExpr->Function(); if (lambda->Params().size() == localTypeAnnotation->AsETSFunctionType()->Params().size() && NeedTypeInference(lambda)) { @@ -661,7 +659,9 @@ checker::Type *ETSChecker::CheckVariableDeclaration(ir::Identifier *ident, ir::T init->AsObjectExpression()->SetPreferredType(annotationType); } - InferAliasLambdaType(typeAnnotation, init); + if (typeAnnotation != nullptr && init->IsArrowFunctionExpression()) { + InferAliasLambdaType(typeAnnotation, init->AsArrowFunctionExpression()); + } checker::Type *initType = init->Check(this); @@ -1348,7 +1348,8 @@ Type *ETSChecker::HandleStringConcatenation(Type *leftType, Type *rightType) { ASSERT(leftType->IsETSStringType() || rightType->IsETSStringType()); - if (!leftType->HasTypeFlag(checker::TypeFlag::CONSTANT) || !rightType->HasTypeFlag(checker::TypeFlag::CONSTANT)) { + if (!leftType->HasTypeFlag(checker::TypeFlag::CONSTANT) || !rightType->HasTypeFlag(checker::TypeFlag::CONSTANT) || + leftType->IsETSBigIntType() || rightType->IsETSBigIntType()) { return GlobalETSStringLiteralType(); } @@ -2128,12 +2129,18 @@ void ETSChecker::GenerateGetterSetterBody(ArenaVector &stmts, A bool isSetter) { auto *classDef = field->Parent()->AsClassDefinition(); - auto *thisExpression = Allocator()->New(); - thisExpression->SetParent(classDef); - thisExpression->SetTsType(classDef->TsType()); + + ir::Expression *baseExpression; + if ((field->Modifiers() & ir::ModifierFlags::SUPER_OWNER) != 0U) { + baseExpression = Allocator()->New(); + } else { + baseExpression = Allocator()->New(); + } + baseExpression->SetParent(classDef); + baseExpression->SetTsType(classDef->TsType()); auto *memberExpression = - AllocNode(thisExpression, field->Key()->AsIdentifier()->Clone(Allocator(), nullptr), + AllocNode(baseExpression, field->Key()->AsIdentifier()->Clone(Allocator(), nullptr), ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); memberExpression->SetTsType(field->TsType()); memberExpression->SetPropVar(field->Key()->Variable()->AsLocalVariable()); @@ -2210,6 +2217,7 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty checker->Allocator(), property->Key()->AsIdentifier()->Name(), property->Key()->AsIdentifier()->Variable()->Declaration()->Node()); auto *var = functionScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); + var->AddFlag(varbinder::VariableFlags::METHOD); methodIdent->SetVariable(var); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) @@ -2229,10 +2237,8 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty paramScope->BindNode(func); functionScope->BindNode(func); - { - auto classCtx = varbinder::LexicalScope::Enter(checker->VarBinder(), classScope); - checker->VarBinder()->AsETSBinder()->ResolveMethodDefinition(method); - } + auto classCtx = varbinder::LexicalScope::Enter(checker->VarBinder(), classScope); + checker->VarBinder()->AsETSBinder()->ResolveMethodDefinition(method); functionScope->BindName(classScope->Node()->AsClassDefinition()->InternalName()); method->Check(checker); @@ -2240,6 +2246,30 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty return method; } +ir::ClassProperty *GetImplementationClassProp(ETSChecker *checker, ir::ClassProperty *interfaceProp, + ir::ClassProperty *originalProp, ETSObjectType *classType) +{ + bool isSuperOwner = ((originalProp->Modifiers() & ir::ModifierFlags::SUPER_OWNER) != 0U); + if (!isSuperOwner) { + auto *const classDef = classType->GetDeclNode()->AsClassDefinition(); + auto *const scope = checker->Scope()->AsClassScope(); + auto *const classProp = checker->ClassPropToImplementationProp( + interfaceProp->Clone(checker->Allocator(), originalProp->Parent()), scope); + classType->AddProperty(classProp->Key()->Variable()->AsLocalVariable()); + classDef->Body().push_back(classProp); + return classProp; + } + + auto *const classProp = classType + ->GetProperty(interfaceProp->Key()->AsIdentifier()->Name(), + PropertySearchFlags::SEARCH_ALL | PropertySearchFlags::SEARCH_IN_BASE) + ->Declaration() + ->Node() + ->AsClassProperty(); + classProp->AddModifier(ir::ModifierFlags::SUPER_OWNER); + return classProp; +} + void ETSChecker::GenerateGetterSetterPropertyAndMethod(ir::ClassProperty *originalProp, ETSObjectType *classType) { auto *const classDef = classType->GetDeclNode()->AsClassDefinition(); @@ -2251,10 +2281,7 @@ void ETSChecker::GenerateGetterSetterPropertyAndMethod(ir::ClassProperty *origin scope->InstanceFieldScope()->EraseBinding(interfaceProp->Key()->AsIdentifier()->Name()); interfaceProp->SetRange(originalProp->Range()); - auto *const classProp = - ClassPropToImplementationProp(interfaceProp->Clone(Allocator(), originalProp->Parent()), scope); - classType->AddProperty(classProp->Key()->Variable()->AsLocalVariable()); - classDef->Body().push_back(classProp); + auto *const classProp = GetImplementationClassProp(this, interfaceProp, originalProp, classType); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) ir::MethodDefinition *getter = GenerateDefaultGetterSetter(interfaceProp, classProp, scope, false, this); diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 384573a26975c7e07e8e4b3eff6edc71ab77430c..439c23f0371d66b0ffc82ca055da488851de9552 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -764,6 +764,41 @@ void ETSChecker::ValidateOverriding(ETSObjectType *classType, const lexer::Sourc } } + if (functionOverridden) { + continue; + } + + auto superClassType = classType->SuperType(); + while (!functionOverridden && superClassType != nullptr) { + for (auto *field : superClassType->Fields()) { + if (field->Name() == (*it)->Name()) { + auto *newProp = + field->Declaration()->Node()->Clone(Allocator(), classType->GetDeclNode())->AsClassProperty(); + newProp->AddModifier(ir::ModifierFlags::SUPER_OWNER); + newProp->AddModifier(isGetter && isSetter ? ir::ModifierFlags::GETTER_SETTER + : isGetter ? ir::ModifierFlags::GETTER + : ir::ModifierFlags::SETTER); + auto *newFieldDecl = Allocator()->New(newProp->Key()->AsIdentifier()->Name()); + newFieldDecl->BindNode(newProp); + + auto newFieldVar = classType->GetDeclNode() + ->Scope() + ->AsClassScope() + ->InstanceFieldScope() + ->AddDecl(Allocator(), newFieldDecl, ScriptExtension::ETS) + ->AsLocalVariable(); + newFieldVar->AddFlag(varbinder::VariableFlags::PROPERTY); + newFieldVar->AddFlag(varbinder::VariableFlags::PUBLIC); + classType->AddProperty(newFieldVar); + it = abstractsToBeImplemented.erase(it); + functionOverridden = true; + break; + } + } + + superClassType = superClassType->SuperType(); + } + if (!functionOverridden) { it++; } @@ -771,9 +806,19 @@ void ETSChecker::ValidateOverriding(ETSObjectType *classType, const lexer::Sourc if (!abstractsToBeImplemented.empty() && throwError) { auto unimplementedSignature = abstractsToBeImplemented.front()->CallSignatures().front(); + auto containingObjectName = GetContainingObjectNameFromSignature(unimplementedSignature); + if (unimplementedSignature->HasSignatureFlag(SignatureFlags::GETTER)) { + ThrowTypeError({classType->Name(), " is not abstract and does not implement getter for ", + unimplementedSignature->Function()->Id()->Name(), " property in ", containingObjectName}, + pos); + } else if (unimplementedSignature->HasSignatureFlag(SignatureFlags::SETTER)) { + ThrowTypeError({classType->Name(), " is not abstract and does not implement setter for ", + unimplementedSignature->Function()->Id()->Name(), " property in ", containingObjectName}, + pos); + } ThrowTypeError({classType->Name(), " is not abstract and does not override abstract method ", unimplementedSignature->Function()->Id()->Name(), unimplementedSignature, " in ", - GetContainingObjectNameFromSignature(unimplementedSignature)}, + containingObjectName}, pos); } @@ -853,8 +898,6 @@ void ETSChecker::CheckClassDefinition(ir::ClassDefinition *classDef) it->Check(this); } } - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - CreateAsyncProxyMethods(classDef); if (classDef->IsGlobal()) { return; @@ -881,7 +924,7 @@ void ETSChecker::CheckConstructors(ir::ClassDefinition *classDef, ETSObjectType } } -static bool IsAsyncMethod(ir::AstNode *node) +bool IsAsyncMethod(ir::AstNode *node) { if (!node->IsMethodDefinition()) { return false; @@ -893,24 +936,27 @@ static bool IsAsyncMethod(ir::AstNode *node) void ETSChecker::CreateAsyncProxyMethods(ir::ClassDefinition *classDef) { ArenaVector asyncImpls(Allocator()->Adapter()); + for (auto *it : classDef->Body()) { if (!IsAsyncMethod(it)) { continue; } - auto *method = it->AsMethodDefinition(); - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - asyncImpls.push_back(CreateAsyncProxy(method, classDef)); - auto *proxy = asyncImpls.back(); - for (auto *overload : method->Overloads()) { - if (!overload->IsAsync()) { + + auto *asyncMethod = it->AsMethodDefinition(); + auto *proxy = CreateAsyncProxy(asyncMethod, classDef); + asyncImpls.push_back(proxy); + + for (auto *overload : asyncMethod->Overloads()) { + if (!IsAsyncMethod(overload)) { continue; } - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + auto *impl = CreateAsyncProxy(overload, classDef, false); impl->Function()->Id()->SetVariable(proxy->Function()->Id()->Variable()); proxy->AddOverload(impl); } } + for (auto *it : asyncImpls) { it->SetParent(classDef); it->Check(this); @@ -1530,15 +1576,12 @@ std::vector ETSChecker::ResolveMemberReference(const ir::Member varbinder::Variable *globalFunctionVar = nullptr; - if (memberExpr->Parent()->IsCallExpression()) { - if (memberExpr->Parent()->AsCallExpression()->Callee() == memberExpr) { - globalFunctionVar = ResolveInstanceExtension(memberExpr); - } + if (memberExpr->Parent()->IsCallExpression() && memberExpr->Parent()->AsCallExpression()->Callee() == memberExpr) { + globalFunctionVar = ResolveInstanceExtension(memberExpr); } else if (target->GetDeclNode() != nullptr && target->GetDeclNode()->IsClassDefinition() && !target->GetDeclNode()->AsClassDefinition()->IsClassDefinitionChecked()) { this->CheckClassDefinition(target->GetDeclNode()->AsClassDefinition()); } - const auto *const targetRef = GetTargetRef(memberExpr); auto searchFlag = GetSearchFlags(memberExpr, targetRef); @@ -1630,17 +1673,18 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini auto *found = classType->SuperType()->GetProperty(it->Name(), searchFlag); ETSObjectType *interfaceFound = nullptr; - if (found == nullptr) { - auto interfaceList = GetInterfacesOfClass(classType); - for (auto *interface : interfaceList) { - auto *propertyFound = interface->GetProperty(it->Name(), searchFlag); - if (propertyFound == nullptr) { - continue; - } - found = propertyFound; - interfaceFound = interface; - break; + if (found != nullptr) { + CheckProperties(classType, classDef, it, found, interfaceFound); + } + auto interfaceList = GetInterfacesOfClass(classType); + for (auto *interface : interfaceList) { + auto *propertyFound = interface->GetProperty(it->Name(), searchFlag); + if (propertyFound == nullptr) { + continue; } + found = propertyFound; + interfaceFound = interface; + break; } if (found == nullptr) { continue; @@ -1653,11 +1697,26 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini void ETSChecker::CheckProperties(ETSObjectType *classType, ir::ClassDefinition *classDef, varbinder::LocalVariable *it, varbinder::LocalVariable *found, ETSObjectType *interfaceFound) { + if (found->TsType() == nullptr) { + GetTypeOfVariable(found); + } + if (!IsSameDeclarationType(it, found)) { if (IsVariableStatic(it) != IsVariableStatic(found)) { return; } + if (it->TsType()->IsETSFunctionType()) { + auto getter = it->TsType()->AsETSFunctionType()->FindGetter(); + if (getter != nullptr && getter->ReturnType() == found->TsType()) { + return; + } + auto setter = it->TsType()->AsETSFunctionType()->FindSetter(); + if (setter != nullptr && setter->Params().front()->TsType() == found->TsType()) { + return; + } + } + const char *targetType {}; if (it->HasFlag(varbinder::VariableFlags::PROPERTY)) { diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 52d8b95f6ae630bfdb345e2a0c4464dfac3eb0c8..6c361224a15901d23a2395c59d2ed6a4851086e2 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -13,62 +13,42 @@ * limitations under the License. */ -#include "compiler/lowering/scopesInit/scopesInitPhase.h" -#include "varbinder/variableFlags.h" #include "checker/checker.h" -#include "checker/checkerContext.h" #include "checker/ets/narrowingWideningConverter.h" #include "checker/types/globalTypesHolder.h" #include "checker/types/ets/etsObjectType.h" #include "ir/astNode.h" -#include "lexer/token/tokenType.h" #include "ir/base/catchClause.h" #include "ir/expression.h" #include "ir/typeNode.h" #include "ir/base/scriptFunction.h" #include "ir/base/classProperty.h" #include "ir/base/methodDefinition.h" -#include "ir/statements/blockStatement.h" -#include "ir/statements/classDeclaration.h" #include "ir/statements/variableDeclarator.h" #include "ir/statements/switchCaseStatement.h" #include "ir/expressions/identifier.h" -#include "ir/expressions/arrayExpression.h" -#include "ir/expressions/objectExpression.h" #include "ir/expressions/callExpression.h" #include "ir/expressions/memberExpression.h" -#include "ir/expressions/literals/booleanLiteral.h" -#include "ir/expressions/literals/charLiteral.h" -#include "ir/expressions/binaryExpression.h" -#include "ir/expressions/assignmentExpression.h" #include "ir/expressions/arrowFunctionExpression.h" -#include "ir/expressions/literals/numberLiteral.h" -#include "ir/expressions/literals/undefinedLiteral.h" -#include "ir/expressions/literals/nullLiteral.h" #include "ir/statements/labelledStatement.h" #include "ir/statements/tryStatement.h" -#include "ir/ets/etsFunctionType.h" #include "ir/ets/etsNewClassInstanceExpression.h" #include "ir/ets/etsParameterExpression.h" -#include "ir/ts/tsAsExpression.h" #include "ir/ts/tsTypeAliasDeclaration.h" #include "ir/ts/tsEnumMember.h" #include "ir/ts/tsTypeParameter.h" +#include "ir/ets/etsUnionType.h" #include "ir/ets/etsTypeReference.h" #include "ir/ets/etsTypeReferencePart.h" -#include "ir/ets/etsPrimitiveType.h" -#include "ir/ts/tsQualifiedName.h" #include "varbinder/variable.h" #include "varbinder/scope.h" #include "varbinder/declaration.h" -#include "parser/ETSparser.h" #include "parser/program/program.h" #include "checker/ETSchecker.h" #include "varbinder/ETSBinder.h" #include "checker/ets/typeRelationContext.h" #include "checker/ets/boxingConverter.h" #include "checker/ets/unboxingConverter.h" -#include "checker/types/ets/types.h" #include "util/helpers.h" namespace ark::es2panda::checker { @@ -900,6 +880,65 @@ bool ETSChecker::CheckLambdaAssignable(ir::Expression *param, ir::ScriptFunction return lambda->Params().size() == calleeType->Params().size(); } +bool ETSChecker::CheckLambdaInvocable(ir::AstNode *typeAnnotation, ir::ArrowFunctionExpression *const arrowFuncExpr, + Type *const parameterType, TypeRelationFlag flags) +{ + if (typeAnnotation->IsETSTypeReference()) { + typeAnnotation = DerefETSTypeReference(typeAnnotation); + } + + if (!typeAnnotation->IsETSFunctionType()) { + return false; + } + + flags |= TypeRelationFlag::NO_THROW; + ir::ScriptFunction *const lambda = arrowFuncExpr->Function(); + + InferTypesForLambda(lambda, typeAnnotation->AsETSFunctionType()); + Type *const argumentType = arrowFuncExpr->Check(this); + + checker::InvocationContext invocationCtx(Relation(), arrowFuncExpr, argumentType, parameterType, + arrowFuncExpr->Start(), {}, flags); + return invocationCtx.IsInvocable(); +} + +bool ETSChecker::CheckLambdaTypeAnnotation(ir::AstNode *typeAnnotation, + ir::ArrowFunctionExpression *const arrowFuncExpr, Type *const parameterType, + TypeRelationFlag flags) +{ + // process `single` type as usual. + if (!typeAnnotation->IsETSUnionType()) { + return CheckLambdaInvocable(typeAnnotation, arrowFuncExpr, parameterType, flags); + } + + // Preserve actual lambda types + ir::ScriptFunction *const lambda = arrowFuncExpr->Function(); + ArenaVector lambdaParamTypes {Allocator()->Adapter()}; + for (auto *const lambdaParam : lambda->Params()) { + lambdaParamTypes.emplace_back(lambdaParam->AsETSParameterExpression()->Ident()->TypeAnnotation()); + } + auto *const lambdaReturnTypeAnnotation = lambda->ReturnTypeAnnotation(); + + for (ir::AstNode *typeNode : typeAnnotation->AsETSUnionType()->Types()) { + if (CheckLambdaInvocable(typeNode, arrowFuncExpr, parameterType, flags)) { + return true; + } + + // Restore inferring lambda types: + for (std::size_t i = 0U; i < lambda->Params().size(); ++i) { + auto *const lambdaParamTypeAnnotation = lambdaParamTypes[i]; + if (lambdaParamTypeAnnotation == nullptr) { + lambda->Params()[i]->AsETSParameterExpression()->Ident()->SetTsTypeAnnotation(nullptr); + } + } + if (lambdaReturnTypeAnnotation == nullptr) { + lambda->SetReturnTypeAnnotation(nullptr); + } + } + + return false; +} + bool ETSChecker::TypeInference(Signature *signature, const ArenaVector &arguments, TypeRelationFlag flags) { @@ -926,42 +965,21 @@ bool ETSChecker::TypeInference(Signature *signature, const ArenaVectorFunction()->Params()[index]->AsETSParameterExpression()->Ident(); ir::AstNode *typeAnn = param->TypeAnnotation(); + Type *const parameterType = signature->Params()[index]->TsType(); - if (typeAnn->IsETSTypeReference()) { - typeAnn = DerefETSTypeReference(typeAnn); + bool const rc = CheckLambdaTypeAnnotation(typeAnn, arrowFuncExpr, parameterType, flags); + if (!rc && (flags & TypeRelationFlag::NO_THROW) == 0) { + Type *const argumentType = arrowFuncExpr->Check(this); + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(parameterType); + const std::initializer_list list = { + "Type '", argumentType, "' is not compatible with type '", targetType, "' at index ", index + 1}; + ThrowTypeError(list, arrowFuncExpr->Start()); } - HandleLambdaTypeInfer(typeAnn, lambda); - - Type *const argumentType = GetApparentType(arrowFuncExpr->Check(this)); - Type *const parameterType = GetApparentType(signature->Params()[index]->TsType()); - const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(parameterType); - const std::initializer_list msg = { - "Type '", argumentType, "' is not compatible with type '", targetType, "' at index ", index + 1}; - - checker::InvocationContext invokationCtx(Relation(), arguments[index], argumentType, parameterType, - arrowFuncExpr->Start(), msg, flags); - - invocable &= invokationCtx.IsInvocable(); - } - return invocable; -} - -void ETSChecker::HandleLambdaTypeInfer(ir::AstNode *typeAnn, ir::ScriptFunction *const lambda) -{ - ASSERT(typeAnn->IsETSFunctionType() || typeAnn->IsETSUnionType()); - - if (typeAnn->IsETSFunctionType()) { - InferTypesForLambda(lambda, typeAnn->AsETSFunctionType()); - return; + invocable &= rc; } - for (auto type : typeAnn->AsETSUnionType()->Types()) { - if (type->IsETSFunctionType()) { - InferTypesForLambda(lambda, type->AsETSFunctionType()); - return; - } - } + return invocable; } bool ETSChecker::ExtensionETSFunctionType(checker::Type *type) diff --git a/ets2panda/checker/ets/validateHelpers.cpp b/ets2panda/checker/ets/validateHelpers.cpp index f78c1f608b75d81bf055ff9aa6e03ed61e847aaa..5c2df40e43096d3161ec0efcd6d6227899403d9f 100644 --- a/ets2panda/checker/ets/validateHelpers.cpp +++ b/ets2panda/checker/ets/validateHelpers.cpp @@ -83,8 +83,14 @@ void ETSChecker::ValidatePropertyAccess(varbinder::Variable *var, ETSObjectType } } -void ETSChecker::ValidateCallExpressionIdentifier(ir::Identifier *const ident, Type *const type) +void ETSChecker::ValidateCallExpressionIdentifier(ir::Identifier *const ident, varbinder::Variable *const resolved, + Type *const type) { + if (resolved->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE) && + ident->Parent()->AsCallExpression()->Callee() != ident) { + ThrowTypeError({"Class or interface '", ident->Name(), "' cannot be used as object"}, ident->Start()); + } + if (ident->Parent()->AsCallExpression()->Callee() != ident) { return; } @@ -185,7 +191,7 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident, varbind switch (ident->Parent()->Type()) { case ir::AstNodeType::CALL_EXPRESSION: { // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - ValidateCallExpressionIdentifier(ident, resolvedType); + ValidateCallExpressionIdentifier(ident, resolved, resolvedType); break; } case ir::AstNodeType::ETS_NEW_CLASS_INSTANCE_EXPRESSION: { diff --git a/ets2panda/checker/types/ets/etsAsyncFuncReturnType.h b/ets2panda/checker/types/ets/etsAsyncFuncReturnType.h index 71b0cede7b61497fe556b0ce39c54dbacdcb5928..d25df5c5b6d227b75583016c4024d1a5a5be6af6 100644 --- a/ets2panda/checker/types/ets/etsAsyncFuncReturnType.h +++ b/ets2panda/checker/types/ets/etsAsyncFuncReturnType.h @@ -45,6 +45,16 @@ public: return promiseType_->TypeArguments()[0]; } + [[nodiscard]] const Type *PromiseType() const noexcept + { + return promiseType_; + } + + [[nodiscard]] Type *PromiseType() noexcept + { + return promiseType_; + } + private: ETSObjectType *promiseType_; }; diff --git a/ets2panda/compiler/core/ASTVerifier.cpp b/ets2panda/compiler/core/ASTVerifier.cpp index 5c376a434aabab2b9bc65eff472bb49d0861cd1d..ccb7f657496761e548672d4b6aee23c9119e95b6 100644 --- a/ets2panda/compiler/core/ASTVerifier.cpp +++ b/ets2panda/compiler/core/ASTVerifier.cpp @@ -118,6 +118,18 @@ static bool IsBooleanType(const ir::AstNode *ast) typedAst->TsType()->HasTypeFlag(checker::TypeFlag::BOOLEAN_LIKE); } +bool IsImportLike(const ir::AstNode *ast) +{ + return (ast->IsETSImportDeclaration() || ast->IsETSReExportDeclaration() || ast->IsImportExpression() || + ast->IsImportSpecifier() || ast->IsImportDefaultSpecifier() || ast->IsImportNamespaceSpecifier()); +} + +bool IsExportLike(const ir::AstNode *ast) +{ + return (ast->IsExportDefaultDeclaration() || ast->IsExportSpecifier() || ast->IsExportAllDeclaration() || + ast->IsExportNamedDeclaration() || ast->IsETSReExportDeclaration()); +} + static bool IsValidTypeForBinaryOp(const ir::AstNode *ast, bool isBitwise) { if (ast == nullptr) { @@ -421,6 +433,23 @@ public: } }; +class ReferenceTypeAnnotationIsNull { +public: + explicit ReferenceTypeAnnotationIsNull([[maybe_unused]] ArenaAllocator &allocator) {} + + [[nodiscard]] CheckResult operator()(CheckContext &ctx, const ir::AstNode *ast) + { + auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE); + if (ast->IsIdentifier()) { + if (ast->AsIdentifier()->IsReference() && ast->AsIdentifier()->TypeAnnotation() != nullptr) { + ctx.AddCheckMessage("TYPE_ANNOTATION_NOT_NULLPTR", *ast, ast->Start()); + result = {CheckDecision::INCORRECT, CheckAction::CONTINUE}; + } + } + return result; + } +}; + class NodeHasSourceRange { public: explicit NodeHasSourceRange([[maybe_unused]] ArenaAllocator &allocator) {} @@ -451,24 +480,126 @@ public: return {CheckDecision::CORRECT, CheckAction::CONTINUE}; } - /* - * NOTICE: That is temporary fix for identifies without variable - * That should be removed in future after fix issues in - * varbinder and checker - */ - if (ast->AsIdentifier()->Variable() != nullptr || ast->AsIdentifier()->IsReference() || - ast->AsIdentifier()->Name().Empty() || ast->AsIdentifier()->Name() == "Void" || - ast->AsIdentifier()->Name().Utf8().find("lambda$invoke$") == 0 || - (ast->AsIdentifier()->Parent() != nullptr && ast->AsIdentifier()->Parent()->IsProperty())) { + const auto *id = ast->AsIdentifier(); + if (CheckAstExceptions(id)) { + return {CheckDecision::CORRECT, CheckAction::CONTINUE}; + } + + // Another function with exceptions to reduce function size + if (CheckMoreAstExceptions(id)) { return {CheckDecision::CORRECT, CheckAction::CONTINUE}; } - const auto *id = ast->AsIdentifier(); ctx.AddCheckMessage("NULL_VARIABLE", *id, id->Start()); return {CheckDecision::INCORRECT, CheckAction::CONTINUE}; } private: + bool CheckMoreAstExceptions(const ir::Identifier *ast) const + { + // NOTE(kkonkuznetsov): skip extension functions + if (ast->Parent() != nullptr && ast->Parent()->IsMemberExpression()) { + // Only properties cause verifier warnings + auto property = ast->Parent()->AsMemberExpression()->Property(); + if (property == ast) { + return true; + } + } + + // NOTE(kkonkuznetsov): skip async functions + auto parent = ast->Parent(); + while (parent != nullptr) { + if (parent->IsScriptFunction()) { + auto script = parent->AsScriptFunction(); + if (script->IsAsyncFunc()) { + return true; + } + + break; + } + + parent = parent->Parent(); + } + + // NOTE(kkonkuznetsov): skip labels identifiers + if (ast->Parent() != nullptr && (ast->Parent()->IsLabelledStatement() || ast->Parent()->IsBreakStatement() || + ast->Parent()->IsContinueStatement())) { + return true; + } + + // NOTE(kkonkuznetsov): skip reexport declarations + if (ast->Parent() != nullptr && ast->Parent()->Parent() != nullptr) { + parent = ast->Parent()->Parent(); + if (parent->IsETSReExportDeclaration()) { + return true; + } + } + + // NOTE(kkonkuznetsov): object expressions + parent = ast->Parent(); + while (parent != nullptr) { + if (parent->IsObjectExpression()) { + return true; + } + + parent = parent->Parent(); + } + + // NOTE(kkonkuznetsov): lambdas + if (ast->Name().Utf8().find("lambda$invoke$") == 0) { + return true; + } + + // NOTE(kkonkuznetsov): some identifiers have empty names + if (ast->Name().Empty()) { + return true; + } + + // NOTE(kkonkuznetsov): skip import alias + if (ast->Parent()->IsTSQualifiedName()) { + return true; + } + + return false; + } + + bool CheckAstExceptions(const ir::Identifier *ast) const + { + // NOTE(kkonkuznetsov): skip enums + if (ast->Parent()->IsMemberExpression() && + (ast->Parent()->AsMemberExpression()->Object()->TsType() == nullptr || + ast->Parent()->AsMemberExpression()->Object()->TsType()->IsETSEnumType() || + ast->Parent()->AsMemberExpression()->Object()->TsType()->IsETSStringEnumType())) { + return true; + } + + // NOTE(kkonkuznetsov): skip length property + if (ast->Parent()->IsMemberExpression() && ast->Name().Is("length")) { + return true; + } + + // NOTE(kkonkuznetsov): skip package declarations + auto parent = ast->Parent(); + while (parent != nullptr) { + if (parent->IsETSPackageDeclaration()) { + return true; + } + + parent = parent->Parent(); + } + + // NOTE(kkonkuznetsov): skip imports + if (IsImportLike(ast->Parent())) { + return true; + } + + // NOTE(kkonkuznetsov): skip anonymous class id + if (ast->Parent()->Parent() != nullptr && ast->Parent()->Parent()->IsETSNewClassInstanceExpression()) { + return true; + } + + return false; + } }; class NodeHasType { @@ -514,49 +645,6 @@ public: } private: - bool IsImportLike(const ir::AstNode *ast) const - { - if (ast->IsETSImportDeclaration()) { - return true; - } - if (ast->IsETSReExportDeclaration()) { - return true; - } - if (ast->IsImportExpression()) { - return true; - } - if (ast->IsImportSpecifier()) { - return true; - } - if (ast->IsImportDefaultSpecifier()) { - return true; - } - if (ast->IsImportNamespaceSpecifier()) { - return true; - } - return false; - } - - bool IsExportLike(const ir::AstNode *ast) const - { - if (ast->IsExportDefaultDeclaration()) { - return true; - } - if (ast->IsExportSpecifier()) { - return true; - } - if (ast->IsExportAllDeclaration()) { - return true; - } - if (ast->IsExportNamedDeclaration()) { - return true; - } - if (ast->IsETSReExportDeclaration()) { - return true; - } - return false; - } - CheckResult CheckCompound(CheckContext &ctx, const ir::AstNode *ast) { if (ast->IsTSInterfaceDeclaration()) { @@ -588,17 +676,12 @@ public: [[nodiscard]] CheckResult operator()(CheckContext &ctx, const ir::AstNode *ast) { if (!ast->IsIdentifier()) { - return {CheckDecision::CORRECT, CheckAction::CONTINUE}; // we will check invariant of Identifier only + // Check invariant of Identifier only + return {CheckDecision::CORRECT, CheckAction::CONTINUE}; } - /* - * NOTICE: That is temporary exclusion for identifies without variable - * Should removed in future - */ - if (ast->AsIdentifier()->IsReference() || ast->AsIdentifier()->TypeAnnotation() != nullptr || - ast->AsIdentifier()->Name().Empty() || ast->AsIdentifier()->Name().Utf8().find("Void") == 0 || - ast->AsIdentifier()->Name().Utf8().find("lambda$invoke$") == 0 || - (ast->AsIdentifier()->Parent() != nullptr && ast->AsIdentifier()->Parent()->IsProperty())) { + // NOTE(kkonkuznetsov): lambdas + if (ast->AsIdentifier()->Name().Utf8().find("lambda$invoke$") == 0) { return {CheckDecision::CORRECT, CheckAction::CONTINUE}; } @@ -610,12 +693,15 @@ public: ctx.AddCheckMessage("NULL_SCOPE_LOCAL_VAR", *ast, ast->Start()); return {CheckDecision::INCORRECT, CheckAction::CONTINUE}; } + auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE); if (!ScopeEncloseVariable(ctx, var)) { result = {CheckDecision::INCORRECT, CheckAction::CONTINUE}; } + return result; } + return {CheckDecision::CORRECT, CheckAction::CONTINUE}; } @@ -626,27 +712,24 @@ public: return std::nullopt; } - /* - * NOTICE: That is temporary exclusion for identifies without variable and scope - * Should removed in future - */ - if (ast->AsIdentifier()->IsReference() || ast->AsIdentifier()->TypeAnnotation() != nullptr || - ast->AsIdentifier()->Name().Empty() || ast->AsIdentifier()->Name().Utf8().find("Void") == 0 || - ast->AsIdentifier()->Name().Utf8().find("field") == 0 || - ast->AsIdentifier()->Name().Utf8().find("lambda$invoke$") == 0 || - (ast->AsIdentifier()->Parent() != nullptr && ast->AsIdentifier()->Parent()->IsProperty())) { - return std::nullopt; - } - auto invariantHasVariable = IdentifierHasVariable {allocator}; const auto variable = ast->AsIdentifier()->Variable(); const auto [decision, action] = invariantHasVariable(ctx, ast); + + if (variable == nullptr) { + // NOTE(kkonkuznetsov): variable should not be null + // but currently some identifiers do not have variables, + // see exceptions in IdentifierHasVariable check + return std::nullopt; + } + if (decision == CheckDecision::CORRECT && variable->IsLocalVariable()) { const auto localVar = variable->AsLocalVariable(); if (localVar->HasFlag(varbinder::VariableFlags::LOCAL)) { return localVar; } } + return std::nullopt; } @@ -658,33 +741,85 @@ public: if (scope == nullptr || var->Declaration() == nullptr) { return true; } + const auto node = var->Declaration()->Node(); if (node == nullptr) { return true; } + const auto varStart = node->Start(); bool isOk = true; if (scope->Bindings().count(var->Name()) == 0) { ctx.AddCheckMessage("SCOPE_DO_NOT_ENCLOSE_LOCAL_VAR", *node, varStart); isOk = false; } + const auto scopeNode = scope->Node(); - auto varNode = node; + const auto varNode = node; + bool skip = CheckAstExceptions(varNode); + if (!IsContainedIn(varNode, scopeNode) || scopeNode == nullptr) { - ctx.AddCheckMessage("SCOPE_NODE_DONT_DOMINATE_VAR_NODE", *node, varStart); - isOk = false; + if (!skip) { + ctx.AddCheckMessage("SCOPE_NODE_DONT_DOMINATE_VAR_NODE", *node, varStart); + isOk = false; + } } + const auto &decls = scope->Decls(); const auto declDominate = std::count(decls.begin(), decls.end(), var->Declaration()); if (declDominate == 0) { - ctx.AddCheckMessage("SCOPE_DECL_DONT_DOMINATE_VAR_DECL", *node, varStart); - isOk = false; + if (!skip) { + ctx.AddCheckMessage("SCOPE_DECL_DONT_DOMINATE_VAR_DECL", *node, varStart); + isOk = false; + } } + return isOk; } private: ArenaAllocator &allocator_; + + bool CheckAstExceptions(const ir::AstNode *ast) + { + // NOTE(kkonkuznetsov): in some cases with lambdas scope node is null + if (ast->Parent() != nullptr && ast->Parent()->IsETSFunctionType()) { + return true; + } + + // NOTE(kkonkuznetsov): skip parameters in async functions + if (ast->Parent() != nullptr && ast->Parent()->IsScriptFunction()) { + auto scriptFunction = ast->Parent()->AsScriptFunction(); + if (scriptFunction->IsAsyncFunc()) { + return true; + } + } + + // NOTE(kkonkuznetsov): skip unions/async lambdas + if (ast->IsIdentifier()) { + auto id = ast->AsIdentifier(); + if (id->TypeAnnotation() != nullptr && id->TypeAnnotation()->IsETSUnionType()) { + return true; + } + } + + // NOTE(kkonkuznetsov): lambdas + auto parent = ast->Parent(); + while (parent != nullptr) { + if (parent->IsScriptFunction()) { + auto script = parent->AsScriptFunction(); + if (script->Id() != nullptr && script->Id()->Name().Utf8().find("lambda$invoke$") == 0) { + return true; + } + + break; + } + + parent = parent->Parent(); + } + + return false; + } }; class EveryChildInParentRange { @@ -779,18 +914,96 @@ public: const auto node = scope->Node(); auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE); if (!IsContainedIn(ast, node)) { + if (CheckScopeNodeExceptions(node)) { + return {CheckDecision::CORRECT, CheckAction::CONTINUE}; + } + + if (CheckAstExceptions(ast)) { + return {CheckDecision::CORRECT, CheckAction::CONTINUE}; + } + result = {CheckDecision::INCORRECT, CheckAction::CONTINUE}; ctx.AddCheckMessage("VARIABLE_NOT_ENCLOSE_SCOPE", *ast, ast->Start()); } + if (!IsContainedIn(scope, encloseScope)) { result = {CheckDecision::INCORRECT, CheckAction::CONTINUE}; ctx.AddCheckMessage("VARIABLE_NOT_ENCLOSE_SCOPE", *ast, ast->Start()); } + return result; } private: ArenaAllocator &allocator_; + + bool CheckScopeNodeExceptions(const ir::AstNode *node) const + { + if (node == nullptr) { + return false; + } + + // NOTE(kkonkuznetsov): skip catch clause + if (node->Parent() != nullptr && node->Parent()->IsCatchClause()) { + return true; + } + + // NOTE(kkonkuznetsov): lambdas + auto parent = node->Parent(); + while (parent != nullptr) { + if (parent->IsFunctionExpression()) { + auto script = parent->AsFunctionExpression()->Function(); + if (script->Id()->Name().Utf8().find("lambda$invoke$") == 0) { + return true; + } + + break; + } + + parent = parent->Parent(); + } + + return false; + } + + bool CheckAstExceptions(const ir::AstNode *ast) const + { + // NOTE(kkonkuznetsov): skip parameter expression inside arrow function expression + auto parent = ast->Parent(); + while (parent != nullptr) { + if (parent->IsETSParameterExpression()) { + return true; + } + + parent = parent->Parent(); + } + + // NOTE(kkonkuznetsov): skip, something with unions + if (ast->IsIdentifier()) { + auto id = ast->AsIdentifier(); + auto annotation = id->TypeAnnotation(); + if (annotation != nullptr && annotation->IsETSUnionType()) { + return true; + } + } + + // NOTE(kkonkuznetsov): skip lambdas + parent = ast->Parent(); + while (parent != nullptr) { + if (parent->IsFunctionExpression()) { + auto script = parent->AsFunctionExpression()->Function(); + if (script->Id()->Name().Utf8().find("lambda$invoke$") == 0) { + return true; + } + + break; + } + + parent = parent->Parent(); + } + + return false; + } }; class SequenceExpressionHasLastType { @@ -1141,6 +1354,7 @@ ASTVerifier::ASTVerifier(ArenaAllocator *allocator) AddInvariant(allocator, "ImportExportAccessValid"); AddInvariant(allocator, "ArithmeticOperationValid"); AddInvariant(allocator, "SequenceExpressionHasLastType"); + AddInvariant(allocator, "ReferenceTypeAnnotationIsNull"); } Messages ASTVerifier::VerifyFull(const ir::AstNode *ast) diff --git a/ets2panda/compiler/core/ASTVerifier.h b/ets2panda/compiler/core/ASTVerifier.h index d5673d966adfa2bb09862a4487f770d631c42609..e8df43aa6be80cde6f1e189420d1e50a63747841 100644 --- a/ets2panda/compiler/core/ASTVerifier.h +++ b/ets2panda/compiler/core/ASTVerifier.h @@ -103,6 +103,7 @@ public: if (phaseName == "CheckerPhase") { accumulatedChecks_.insert("NodeHasTypeForAll"); accumulatedChecks_.insert("IdentifierHasVariableForAll"); + accumulatedChecks_.insert("ReferenceTypeAnnotationIsNullForAll"); accumulatedChecks_.insert("ArithmeticOperationValidForAll"); accumulatedChecks_.insert("SequenceExpressionHasLastTypeForAll"); accumulatedChecks_.insert("ForLoopCorrectlyInitializedForAll"); diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index 370fcd4fa8c0234fe06cce6e4e2d5fbfc17329bc..4239a7142ac5247057402bb3dd4ea3a93605a81f 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -1124,9 +1124,9 @@ void ETSCompiler::Compile(const ir::MemberExpression *expr) const etsg->StoreAccumulator(expr, objReg); auto ttctx = compiler::TargetTypeContext(etsg, expr->TsType()); - auto const *const variable = expr->PropVar(); - if (auto const *const variableType = variable->TsType(); - variableType->HasTypeFlag(checker::TypeFlag::GETTER_SETTER)) { + ASSERT(expr->PropVar()->TsType() != nullptr); + const checker::Type *const variableType = expr->PropVar()->TsType(); + if (variableType->HasTypeFlag(checker::TypeFlag::GETTER_SETTER)) { checker::Signature *sig = variableType->AsETSFunctionType()->FindGetter(); etsg->CallThisVirtual0(expr, objReg, sig->InternalName()); } else if (objectType->IsETSDynamicType()) { @@ -1135,7 +1135,7 @@ void ETSCompiler::Compile(const ir::MemberExpression *expr) const etsg->LoadUnionProperty(expr, expr->TsType(), objReg, propName); } else { const auto fullName = etsg->FormClassPropReference(objectType->AsETSObjectType(), propName); - etsg->LoadProperty(expr, expr->TsType(), objReg, fullName); + etsg->LoadProperty(expr, variableType, objReg, fullName); } etsg->GuardUncheckedType(expr, expr->UncheckedType(), expr->TsType()); diff --git a/ets2panda/compiler/lowering/ets/bigintLowering.cpp b/ets2panda/compiler/lowering/ets/bigintLowering.cpp index 61354c558406ddb2331e9ee1d6e250a08e5b529b..ef9d07876fa282aa7e7de32be936cf93bc666945 100644 --- a/ets2panda/compiler/lowering/ets/bigintLowering.cpp +++ b/ets2panda/compiler/lowering/ets/bigintLowering.cpp @@ -15,6 +15,9 @@ #include "bigintLowering.h" +#include "compiler/lowering/scopesInit/scopesInitPhase.h" +#include "compiler/lowering/util.h" + namespace ark::es2panda::compiler { std::string_view BigIntLowering::Name() const @@ -22,20 +25,67 @@ std::string_view BigIntLowering::Name() const return "BigIntLowering"; } -void CreateBigInt(parser::ETSParser *parser, ir::ClassProperty *property) +ir::Expression *CreateBigInt(public_lib::Context *ctx, ir::BigIntLiteral *literal) { - if (property != nullptr && property->Value() != nullptr && property->Value()->IsBigIntLiteral()) { - auto literal = property->Value()->AsBigIntLiteral(); - auto value = literal->Str(); - - // This will change the bigint literal node into the new class instance expression. - std::string src {"new BigInt("}; - src += value.Utf8(); - src += ")"; - auto newValue = parser->AsETSParser()->CreateExpression(src); - newValue->SetParent(property); - property->SetValue(newValue); + auto parser = ctx->parser->AsETSParser(); + auto checker = ctx->checker->AsETSChecker(); + + // This will change the bigint literal node into the new class instance expression: + // 123456n => new BigInt("123456") + std::string src {"new "}; + src += Signatures::BUILTIN_BIGINT_CLASS; + src += "(\""; + src += literal->Str().Utf8(); + src += "\")"; + + auto loweringResult = parser->CreateExpression(src); + loweringResult->SetParent(literal->Parent()); + + InitScopesPhaseETS::RunExternalNode(loweringResult, checker->VarBinder()); + checker->VarBinder()->AsETSBinder()->ResolveReferencesForScope(loweringResult, NearestScope(loweringResult)); + loweringResult->Check(checker); + + return loweringResult; +} + +bool ReplaceStrictEqualByNormalEqual(ir::BinaryExpression *expr) +{ + auto left = expr->Left()->TsType(); + auto isBigintLeft = (left != nullptr && left->IsETSBigIntType()) || expr->Left()->IsBigIntLiteral(); + auto right = expr->Right()->TsType(); + auto isBigintRight = (right != nullptr && right->IsETSBigIntType()) || expr->Right()->IsBigIntLiteral(); + if (!isBigintLeft && !isBigintRight) { + return false; } + + if (expr->OperatorType() == lexer::TokenType::PUNCTUATOR_STRICT_EQUAL) { + expr->SetOperator(lexer::TokenType::PUNCTUATOR_EQUAL); + } else if (expr->OperatorType() == lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL) { + expr->SetOperator(lexer::TokenType::PUNCTUATOR_NOT_EQUAL); + } else { + return false; + } + + return true; +} + +// Currently there are no compile time operations for bigint. +bool RemoveConst(ir::BinaryExpression *expr) +{ + bool isRemoved = false; + auto left = expr->Left()->TsType(); + if (left != nullptr && left->IsETSBigIntType()) { + left->RemoveTypeFlag(checker::TypeFlag::CONSTANT); + isRemoved = true; + } + + auto right = expr->Right()->TsType(); + if (right != nullptr && right->IsETSBigIntType()) { + right->RemoveTypeFlag(checker::TypeFlag::CONSTANT); + isRemoved = true; + } + + return isRemoved; } bool BigIntLowering::Perform(public_lib::Context *const ctx, parser::Program *const program) @@ -47,12 +97,21 @@ bool BigIntLowering::Perform(public_lib::Context *const ctx, parser::Program *co } } - auto *const parser = ctx->parser->AsETSParser(); + auto checker = ctx->checker->AsETSChecker(); program->Ast()->TransformChildrenRecursively( - [parser](ir::AstNode *ast) -> ir::AstNode * { - if (ast->IsClassProperty()) { - CreateBigInt(parser, ast->AsClassProperty()); + [ctx, checker](ir::AstNode *ast) -> ir::AstNode * { + if (ast->IsBigIntLiteral() && ast->Parent() != nullptr && ast->Parent()->IsClassProperty()) { + return CreateBigInt(ctx, ast->AsBigIntLiteral()); + } + + if (ast->IsBinaryExpression()) { + auto expr = ast->AsBinaryExpression(); + bool doCheck = ReplaceStrictEqualByNormalEqual(expr); + doCheck |= RemoveConst(expr); + if (doCheck) { + expr->Check(checker); + } } return ast; diff --git a/ets2panda/compiler/lowering/ets/objectIterator.cpp b/ets2panda/compiler/lowering/ets/objectIterator.cpp index 7ab124b90cf84aa03c163a71b82ab14b62ea0beb..7b0d59c61060e06213dd574a11b4bad06205000f 100644 --- a/ets2panda/compiler/lowering/ets/objectIterator.cpp +++ b/ets2panda/compiler/lowering/ets/objectIterator.cpp @@ -68,7 +68,7 @@ void ObjectIteratorLowering::TransferForOfLoopBody(ir::Statement *const forBody, statement->SetParent(whileBody); if (needCleaning) { // Note: we don't need to clean top-level statement itself because it doesn't have type. - ClearTypesAndVariables(statement); + ClearTypesVariablesAndScopes(statement); } whileStatements[WHILE_LOOP_SIZE + i - 1U] = statement; } @@ -78,7 +78,7 @@ void ObjectIteratorLowering::TransferForOfLoopBody(ir::Statement *const forBody, forBody->SetParent(whileBody); if (needCleaning) { - ClearTypesAndVariables(forBody); + ClearTypesVariablesAndScopes(forBody); } whileStatements[WHILE_LOOP_SIZE - 1U] = forBody; } diff --git a/ets2panda/compiler/lowering/ets/promiseVoid.cpp b/ets2panda/compiler/lowering/ets/promiseVoid.cpp index 1e8934bc30deba9513f017a5d6ea23d12333632b..3ad58325fc1404a6af51c634c0ee82d53a370be2 100644 --- a/ets2panda/compiler/lowering/ets/promiseVoid.cpp +++ b/ets2panda/compiler/lowering/ets/promiseVoid.cpp @@ -180,8 +180,10 @@ bool PromiseVoidInferencePhase::Perform(public_lib::Context *ctx, parser::Progra const auto hasPromiseVoid = CheckForPromiseVoid(returnAnn); if (!hasReturnAnn) { - const auto &loc = genTypeLocation(function); - function->SetReturnTypeAnnotation(CreatePromiseVoidType(checker, loc)); + if (!function->HasReturnStatement()) { + const auto &loc = genTypeLocation(function); + function->SetReturnTypeAnnotation(CreatePromiseVoidType(checker, loc)); + } if (function->HasBody()) { HandleAsyncScriptFunctionBody(checker, function->Body()->AsBlockStatement()); diff --git a/ets2panda/compiler/lowering/phase.cpp b/ets2panda/compiler/lowering/phase.cpp index 54808d3f8baecbf68082c1f2f2f5ea8c0f61c532..59495899be2d7b3f82cffd83650190114ca8b2b1 100644 --- a/ets2panda/compiler/lowering/phase.cpp +++ b/ets2panda/compiler/lowering/phase.cpp @@ -86,11 +86,11 @@ static void CheckOptionsAfterPhase(const CompilerOptions &options, const parser: std::vector GetETSPhaseList() { + // clang-format off return { &g_pluginsAfterParse, &g_topLevelStatements, &g_defaultParameterLowering, - &g_bigintLowering, &g_initScopesPhaseEts, &g_optionalLowering, &g_promiseVoidInferencePhase, @@ -99,6 +99,7 @@ std::vector GetETSPhaseList() &g_interfacePropDeclPhase, &g_checkerPhase, &g_pluginsAfterCheck, + &g_bigintLowering, &g_opAssignmentLowering, &g_recordLowering, &g_objectIndexLowering, @@ -110,6 +111,7 @@ std::vector GetETSPhaseList() &g_objectLiteralLowering, &g_pluginsAfterLowerings, }; + // clang-format on } std::vector GetASPhaseList() diff --git a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp index 66028b32b473b58aad874394e1c15442aa9f3994..02d17a5f5821f9511ff131464a96533f9433e814 100644 --- a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp +++ b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp @@ -17,6 +17,27 @@ #include "scopesInitPhase.h" namespace ark::es2panda::compiler { + +template +varbinder::LexicalScope LexicalScopeCreateOrEnter(varbinder::VarBinder *varBinder, ir::AstNode *ast) +{ + if (ast != nullptr && ast->Scope() != nullptr) { + return varbinder::LexicalScope::Enter(varBinder, reinterpret_cast(ast->Scope())); + } + return varbinder::LexicalScope(varBinder); +} + +template +T *AddOrGetDecl(varbinder::VarBinder *varBinder, util::StringView name, ir::AstNode *ast, + const lexer::SourcePosition &pos, Args &&...args) +{ + if (auto *var = varBinder->GetScope()->FindLocal(name, varbinder::ResolveBindingOptions::BINDINGS); + var != nullptr && var->Declaration() != nullptr && var->Declaration()->Node() == ast) { + return reinterpret_cast(var->Declaration()); + } + return varBinder->AddDecl(pos, args...); +} + bool ScopesInitPhase::Perform(PhaseContext *ctx, parser::Program *program) { Prepare(ctx, program); @@ -33,7 +54,7 @@ void ScopesInitPhase::VisitScriptFunction(ir::ScriptFunction *scriptFunction) void ScopesInitPhase::VisitBlockStatement(ir::BlockStatement *blockStmt) { - auto localCtx = varbinder::LexicalScope(VarBinder()); + auto localCtx = LexicalScopeCreateOrEnter(VarBinder(), blockStmt); HandleBlockStmt(blockStmt, GetScope()); } @@ -71,11 +92,13 @@ varbinder::FunctionParamScope *ScopesInitPhase::HandleFunctionSig(ir::TSTypePara void ScopesInitPhase::HandleFunction(ir::ScriptFunction *function) { CallNode(function->Id()); - auto funcParamScope = - HandleFunctionSig(function->TypeParams(), function->Params(), function->ReturnTypeAnnotation()); + // NOTE(gogabr): this will skip type/value parameters when they are added to an existing function sig + auto funcParamScope = (function->Scope() == nullptr) ? HandleFunctionSig(function->TypeParams(), function->Params(), + function->ReturnTypeAnnotation()) + : function->Scope()->ParamScope(); auto paramCtx = varbinder::LexicalScope::Enter(VarBinder(), funcParamScope, false); - auto functionCtx = varbinder::LexicalScope(VarBinder()); + auto functionCtx = LexicalScopeCreateOrEnter(VarBinder(), function); auto *functionScope = functionCtx.GetScope(); BindFunctionScopes(functionScope, funcParamScope); @@ -90,14 +113,17 @@ void ScopesInitPhase::HandleFunction(ir::ScriptFunction *function) void ScopesInitPhase::HandleBlockStmt(ir::BlockStatement *block, varbinder::Scope *scope) { - BindScopeNode(scope, block); + if (block->Scope() == nullptr) { + BindScopeNode(scope, block); + } Iterate(block); } void ScopesInitPhase::VisitClassDefinition(ir::ClassDefinition *classDef) { - auto classCtx = varbinder::LexicalScope(VarBinder()); - VarBinder()->AddDecl(classDef->Start(), classDef->PrivateId()); + auto classCtx = LexicalScopeCreateOrEnter(VarBinder(), classDef); + AddOrGetDecl(VarBinder(), classDef->PrivateId(), classDef, classDef->Start(), + classDef->PrivateId()); BindClassName(classDef); auto *classScope = classCtx.GetScope(); @@ -107,10 +133,13 @@ void ScopesInitPhase::VisitClassDefinition(ir::ClassDefinition *classDef) void ScopesInitPhase::VisitForUpdateStatement(ir::ForUpdateStatement *forUpdateStmt) { - auto declCtx = varbinder::LexicalScope(VarBinder()); + auto declCtx = (forUpdateStmt->Scope() == nullptr) + ? varbinder::LexicalScope(VarBinder()) + : varbinder::LexicalScope::Enter( + VarBinder(), forUpdateStmt->Scope()->DeclScope()); CallNode(forUpdateStmt->Init()); - varbinder::LexicalScope lexicalScope(VarBinder()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), forUpdateStmt); CallNode(forUpdateStmt->Test()); CallNode(forUpdateStmt->Update()); CallNode(forUpdateStmt->Body()); @@ -120,20 +149,26 @@ void ScopesInitPhase::VisitForUpdateStatement(ir::ForUpdateStatement *forUpdateS void ScopesInitPhase::VisitForInStatement(ir::ForInStatement *forInStmt) { - auto declCtx = varbinder::LexicalScope(VarBinder()); + auto declCtx = (forInStmt->Scope() == nullptr) + ? varbinder::LexicalScope(VarBinder()) + : varbinder::LexicalScope::Enter( + VarBinder(), forInStmt->Scope()->DeclScope()); CallNode(forInStmt->Left()); - varbinder::LexicalScope lexicalScope(VarBinder()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), forInStmt); CallNode(forInStmt->Right()); CallNode(forInStmt->Body()); HandleFor(declCtx.GetScope(), lexicalScope.GetScope(), forInStmt); } void ScopesInitPhase::VisitForOfStatement(ir::ForOfStatement *forOfStmt) { - auto declCtx = varbinder::LexicalScope(VarBinder()); + auto declCtx = (forOfStmt->Scope() == nullptr) + ? varbinder::LexicalScope(VarBinder()) + : varbinder::LexicalScope::Enter( + VarBinder(), forOfStmt->Scope()->DeclScope()); CallNode(forOfStmt->Left()); - varbinder::LexicalScope lexicalScope(VarBinder()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), forOfStmt); CallNode(forOfStmt->Right()); CallNode(forOfStmt->Body()); HandleFor(declCtx.GetScope(), lexicalScope.GetScope(), forOfStmt); @@ -141,7 +176,10 @@ void ScopesInitPhase::VisitForOfStatement(ir::ForOfStatement *forOfStmt) void ScopesInitPhase::VisitCatchClause(ir::CatchClause *catchClause) { - auto catchParamCtx = varbinder::LexicalScope(VarBinder()); + auto catchParamCtx = (catchClause->Scope() == nullptr) + ? varbinder::LexicalScope(VarBinder()) + : varbinder::LexicalScope::Enter( + VarBinder(), catchClause->Scope()->ParamScope()); auto *catchParamScope = catchParamCtx.GetScope(); auto *param = catchClause->Param(); @@ -157,7 +195,7 @@ void ScopesInitPhase::VisitCatchClause(ir::CatchClause *catchClause) } catchParamScope->BindNode(param); - auto catchCtx = varbinder::LexicalScope(VarBinder()); + auto catchCtx = LexicalScopeCreateOrEnter(VarBinder(), catchClause); auto *catchScope = catchCtx.GetScope(); catchScope->AssignParamScope(catchParamScope); @@ -172,7 +210,7 @@ void ScopesInitPhase::VisitVariableDeclarator(ir::VariableDeclarator *varDecl) auto init = varDecl->Id(); std::vector bindings = util::Helpers::CollectBindingNames(init); for (auto *binding : bindings) { - auto [decl, var] = AddVarDecl(varDecl->Flag(), varDecl->Start(), binding->Name()); + auto [decl, var] = AddOrGetVarDecl(varDecl->Flag(), varDecl->Start(), binding); BindVarDecl(binding, init, decl, var); } Iterate(varDecl); @@ -181,7 +219,7 @@ void ScopesInitPhase::VisitVariableDeclarator(ir::VariableDeclarator *varDecl) void ScopesInitPhase::VisitSwitchStatement(ir::SwitchStatement *switchStmt) { CallNode(switchStmt->Discriminant()); - auto localCtx = varbinder::LexicalScope(VarBinder()); + auto localCtx = LexicalScopeCreateOrEnter(VarBinder(), switchStmt); BindScopeNode(localCtx.GetScope(), switchStmt); CallNode(switchStmt->Cases()); } @@ -189,7 +227,7 @@ void ScopesInitPhase::VisitSwitchStatement(ir::SwitchStatement *switchStmt) void ScopesInitPhase::VisitWhileStatement(ir::WhileStatement *whileStmt) { CallNode(whileStmt->Test()); - varbinder::LexicalScope lexicalScope(VarBinder()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), whileStmt); BindScopeNode(lexicalScope.GetScope(), whileStmt); CallNode(whileStmt->Body()); } @@ -208,7 +246,7 @@ void ScopesInitPhase::VisitClassDeclaration(ir::ClassDeclaration *classDecl) void ScopesInitPhase::VisitDoWhileStatement(ir::DoWhileStatement *doWhileStmt) { - varbinder::LexicalScope lexicalScope(VarBinder()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), doWhileStmt); BindScopeNode(lexicalScope.GetScope(), doWhileStmt); Iterate(doWhileStmt); } @@ -226,29 +264,32 @@ void ScopesInitPhase::VisitExportAllDeclaration(ir::ExportAllDeclaration *export { Iterate(exportAllDecl); const auto name = exportAllDecl->Exported() != nullptr ? exportAllDecl->Exported()->Name() : "*"; - auto *decl = VarBinder()->AddDecl(exportAllDecl->Start(), name, "*"); + auto *decl = + AddOrGetDecl(VarBinder(), name, exportAllDecl, exportAllDecl->Start(), name, "*"); VarBinder()->GetScope()->AsModuleScope()->AddExportDecl(exportAllDecl, decl); } void ScopesInitPhase::VisitImportNamespaceSpecifier(ir::ImportNamespaceSpecifier *importSpec) { Iterate(importSpec); - VarBinder()->AddDecl(importSpec->Start(), "*", importSpec->Local()->Name(), importSpec); + AddOrGetDecl(VarBinder(), importSpec->Local()->Name(), importSpec, importSpec->Start(), "*", + importSpec->Local()->Name(), importSpec); } void ScopesInitPhase::VisitImportSpecifier(ir::ImportSpecifier *importSpec) { Iterate(importSpec); const auto *imported = importSpec->Imported(); - VarBinder()->AddDecl(importSpec->Start(), imported->Name(), importSpec->Local()->Name(), - importSpec); + AddOrGetDecl(VarBinder(), importSpec->Local()->Name(), importSpec, importSpec->Start(), + imported->Name(), importSpec->Local()->Name(), importSpec); } void ScopesInitPhase::VisitImportDefaultSpecifier(ir::ImportDefaultSpecifier *importSpec) { Iterate(importSpec); const auto *local = importSpec->Local(); - VarBinder()->AddDecl(local->Start(), "default", local->Name(), importSpec); + AddOrGetDecl(VarBinder(), local->Name(), importSpec, local->Start(), "default", + local->Name(), importSpec); } void ScopesInitPhase::VisitExportDefaultDeclaration(ir::ExportDefaultDeclaration *exportDecl) @@ -279,8 +320,9 @@ void ScopesInitPhase::VisitExportNamedDeclaration(ir::ExportNamedDeclaration *ex varbinder::ModuleScope::ExportDeclList exportDecls(program_->Allocator()->Adapter()); for (auto *spec : exportDecl->Specifiers()) { - auto *decl = VarBinder()->AddDecl(exportDecl->Start(), spec->Exported()->Name(), - spec->Local()->Name(), spec); + auto *decl = + AddOrGetDecl(VarBinder(), spec->Local()->Name(), spec, exportDecl->Start(), + spec->Exported()->Name(), spec->Local()->Name(), spec); exportDecls.push_back(decl); } VarBinder()->GetScope()->AsModuleScope()->AddExportDecl(exportDecl, std::move(exportDecls)); @@ -289,7 +331,7 @@ void ScopesInitPhase::VisitExportNamedDeclaration(ir::ExportNamedDeclaration *ex void ScopesInitPhase::VisitTSFunctionType(ir::TSFunctionType *funcType) { - varbinder::LexicalScope lexicalScope(VarBinder()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), funcType); auto *funcParamScope = lexicalScope.GetScope(); BindScopeNode(funcParamScope, funcType); Iterate(funcType); @@ -327,7 +369,8 @@ void ScopesInitPhase::ThrowSyntaxError(std::string_view errorMessage, const lexe void ScopesInitPhase::CreateFuncDecl(ir::ScriptFunction *func) { - VarBinder()->AddDecl(func->Id()->Start(), Allocator(), func->Id()->Name(), func); + AddOrGetDecl(VarBinder(), func->Id()->Name(), func, func->Id()->Start(), Allocator(), + func->Id()->Name(), func); } util::StringView ScopesInitPhase::FormInterfaceOrEnumDeclarationIdBinding(ir::Identifier *id) @@ -342,7 +385,8 @@ varbinder::Decl *ScopesInitPhase::BindClassName(ir::ClassDefinition *classDef) return nullptr; } - auto identDecl = VarBinder()->AddDecl(identNode->Start(), identNode->Name()); + auto identDecl = AddOrGetDecl(VarBinder(), identNode->Name(), classDef, identNode->Start(), + identNode->Name()); if (identDecl != nullptr) { identDecl->BindNode(classDef); } @@ -363,16 +407,20 @@ void ScopesInitPhase::BindClassDefinition(ir::ClassDefinition *classDef) const auto locStart = classDef->Ident()->Start(); const auto &className = classDef->Ident()->Name(); if ((classDef->Modifiers() & ir::ClassDefinitionModifiers::CLASS_DECL) != 0U) { - VarBinder()->AddDecl(locStart, className, classDef); + AddOrGetDecl(VarBinder(), className, classDef, locStart, className, classDef); } else { - VarBinder()->AddDecl(locStart, className, classDef); + AddOrGetDecl(VarBinder(), className, classDef, locStart, className, classDef); } } -std::tuple ScopesInitPhase::AddVarDecl(ir::VariableDeclaratorFlag flag, - lexer::SourcePosition startLoc, - const util::StringView &name) +std::tuple ScopesInitPhase::AddOrGetVarDecl(ir::VariableDeclaratorFlag flag, + lexer::SourcePosition startLoc, + const ir::Identifier *id) { + if (auto var = id->Variable(); var != nullptr) { + return {var->Declaration(), var}; + } + auto name = id->Name(); switch (flag) { case ir::VariableDeclaratorFlag::LET: return VarBinder()->NewVarDecl(startLoc, name); @@ -425,18 +473,18 @@ void ScopesInitPhase::AnalyzeExports() void ScopeInitTyped::VisitTSModuleDeclaration(ir::TSModuleDeclaration *moduleDecl) { if (!moduleDecl->IsExternalOrAmbient()) { - auto *decl = VarBinder()->AddDecl(moduleDecl->Name()->Start(), - moduleDecl->Name()->AsIdentifier()->Name()); + auto name = moduleDecl->Name()->AsIdentifier()->Name(); + auto *decl = AddOrGetDecl(VarBinder(), name, moduleDecl, moduleDecl->Name()->Start(), name); decl->BindNode(moduleDecl); } - auto localCtx = varbinder::LexicalScope(VarBinder()); + auto localCtx = LexicalScopeCreateOrEnter(VarBinder(), moduleDecl); BindScopeNode(localCtx.GetScope(), moduleDecl); Iterate(moduleDecl); } void ScopeInitTyped::VisitTSModuleBlock(ir::TSModuleBlock *block) { - auto localCtx = varbinder::LexicalScope(VarBinder()); + auto localCtx = LexicalScopeCreateOrEnter(VarBinder(), block); Iterate(block); BindScopeNode(localCtx.GetScope(), block); } @@ -446,7 +494,7 @@ void ScopeInitTyped::VisitTSTypeAliasDeclaration(ir::TSTypeAliasDeclaration *typ const auto id = typeAliasDecl->Id(); varbinder::TSBinding tsBinding(Allocator(), id->Name()); auto *decl = VarBinder()->AddTsDecl(id->Start(), tsBinding.View()); - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), typeAliasDecl->TypeParams()); decl->BindNode(typeAliasDecl); Iterate(typeAliasDecl); } @@ -479,12 +527,13 @@ void ScopeInitTyped::VisitTSInterfaceDeclaration(ir::TSInterfaceDeclaration *int } CallNode(ident); - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), interfDecl->TypeParams()); CallNode(interfDecl->TypeParams()); CallNode(interfDecl->Extends()); - auto localScope = varbinder::LexicalScope(VarBinder()); - auto *identDecl = VarBinder()->AddDecl(ident->Start(), ident->Name()); + auto localScope = LexicalScopeCreateOrEnter(VarBinder(), interfDecl); + auto *identDecl = + AddOrGetDecl(VarBinder(), ident->Name(), interfDecl, ident->Start(), ident->Name()); identDecl->BindNode(interfDecl); BindScopeNode(localScope.GetScope(), interfDecl); @@ -506,7 +555,7 @@ void ScopeInitTyped::VisitTSEnumMember(ir::TSEnumMember *enumMember) } else { UNREACHABLE(); } - auto *decl = VarBinder()->AddDecl(key->Start(), name); + auto *decl = AddOrGetDecl(VarBinder(), name, enumMember, key->Start(), name); decl->BindNode(enumMember); } @@ -519,7 +568,7 @@ void ScopeInitTyped::VisitTSEnumDeclaration(ir::TSEnumDeclaration *enumDecl) varbinder::EnumLiteralDecl *decl {}; if (res == bindings.end()) { decl = VarBinder()->AddTsDecl(enumDecl->Start(), ident, enumDecl->IsConst()); - varbinder::LexicalScope enumCtx = varbinder::LexicalScope(VarBinder()); + varbinder::LexicalScope enumCtx = LexicalScopeCreateOrEnter(VarBinder(), enumDecl); decl->BindScope(enumCtx.GetScope()); BindScopeNode(VarBinder()->GetScope()->AsLocalScope(), enumDecl); } else if (!res->second->Declaration()->IsEnumLiteralDecl() || @@ -538,7 +587,8 @@ void ScopeInitTyped::VisitTSEnumDeclaration(ir::TSEnumDeclaration *enumDecl) void ScopeInitTyped::VisitTSTypeParameter(ir::TSTypeParameter *typeParam) { - auto decl = VarBinder()->AddDecl(typeParam->Start(), typeParam->Name()->Name()); + auto name = typeParam->Name()->Name(); + auto decl = AddOrGetDecl(VarBinder(), name, typeParam, typeParam->Start(), name); decl->BindNode(typeParam); Iterate(typeParam); } @@ -551,12 +601,13 @@ void ScopeInitTyped::VisitTSTypeParameterDeclaration(ir::TSTypeParameterDeclarat void ScopeInitTyped::VisitClassDefinition(ir::ClassDefinition *classDef) { - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), classDef->TypeParams()); CallNode(classDef->TypeParams()); - auto classCtx = varbinder::LexicalScope(VarBinder()); + auto classCtx = LexicalScopeCreateOrEnter(VarBinder(), classDef); BindClassName(classDef); - VarBinder()->AddDecl(classDef->Start(), classDef->PrivateId()); + AddOrGetDecl(VarBinder(), classDef->PrivateId(), classDef, classDef->Start(), + classDef->PrivateId()); BindScopeNode(classCtx.GetScope(), classDef); IterateNoTParams(classDef); } @@ -581,7 +632,10 @@ void InitScopesPhaseTs::VisitImportDeclaration(ir::ImportDeclaration *importDecl void InitScopesPhaseTs::VisitTSFunctionType(ir::TSFunctionType *constrType) { - auto lexicalScope = HandleFunctionSig(constrType->TypeParams(), constrType->Params(), constrType->ReturnType()); + auto lexicalScope = + (constrType->Scope() == nullptr) + ? HandleFunctionSig(constrType->TypeParams(), constrType->Params(), constrType->ReturnType()) + : constrType->Scope(); BindScopeNode(lexicalScope, constrType); } @@ -598,6 +652,11 @@ void InitScopesPhaseTs::CreateFuncDecl(ir::ScriptFunction *func) } else { varbinder::Decl *currentDecl = res->second->Declaration(); + auto &existing = currentDecl->AsFunctionDecl()->Decls(); + if (std::find(existing.begin(), existing.end(), func) != existing.end()) { + return; + } + if (!currentDecl->IsFunctionDecl() || !currentDecl->AsFunctionDecl()->Node()->AsScriptFunction()->IsOverload()) { VarBinder()->ThrowRedeclaration(startLoc, currentDecl->Name()); @@ -610,27 +669,33 @@ void InitScopesPhaseTs::CreateFuncDecl(ir::ScriptFunction *func) void InitScopesPhaseTs::VisitTSConstructorType(ir::TSConstructorType *constrT) { - auto funcParamScope = HandleFunctionSig(constrT->TypeParams(), constrT->Params(), constrT->ReturnType()); + auto funcParamScope = (constrT->Scope() == nullptr) + ? HandleFunctionSig(constrT->TypeParams(), constrT->Params(), constrT->ReturnType()) + : constrT->Scope(); BindScopeNode(funcParamScope, constrT); } void InitScopesPhaseTs::VisitArrowFunctionExpression(ir::ArrowFunctionExpression *arrowFExpr) { - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + auto typeParamsCtx = + LexicalScopeCreateOrEnter(VarBinder(), arrowFExpr->Function()->TypeParams()); Iterate(arrowFExpr); } void InitScopesPhaseTs::VisitTSSignatureDeclaration(ir::TSSignatureDeclaration *signDecl) { - auto funcParamScope = - HandleFunctionSig(signDecl->TypeParams(), signDecl->Params(), signDecl->ReturnTypeAnnotation()); + auto funcParamScope = (signDecl->Scope() == nullptr) ? HandleFunctionSig(signDecl->TypeParams(), signDecl->Params(), + signDecl->ReturnTypeAnnotation()) + : signDecl->Scope(); BindScopeNode(funcParamScope, signDecl); } void InitScopesPhaseTs::VisitTSMethodSignature(ir::TSMethodSignature *methodSign) { auto funcParamScope = - HandleFunctionSig(methodSign->TypeParams(), methodSign->Params(), methodSign->ReturnTypeAnnotation()); + (methodSign->Scope() == nullptr) + ? HandleFunctionSig(methodSign->TypeParams(), methodSign->Params(), methodSign->ReturnTypeAnnotation()) + : methodSign->Scope(); BindScopeNode(funcParamScope, methodSign); } @@ -698,8 +763,10 @@ void InitScopesPhaseETS::BindVarDecl(ir::Identifier *binding, ir::Expression *in void InitScopesPhaseETS::VisitBlockExpression(ir::BlockExpression *blockExpr) { - auto localCtx = varbinder::LexicalScope(VarBinder()); - BindScopeNode(GetScope(), blockExpr); + auto localCtx = LexicalScopeCreateOrEnter(VarBinder(), blockExpr); + if (blockExpr->Scope() == nullptr) { + BindScopeNode(GetScope(), blockExpr); + } Iterate(blockExpr); } @@ -708,9 +775,12 @@ void InitScopesPhaseETS::VisitClassStaticBlock(ir::ClassStaticBlock *staticBlock const auto func = staticBlock->Function(); { - auto funcParamCtx = varbinder::LexicalScope(VarBinder()); + auto funcParamCtx = (func->Scope() == nullptr) + ? varbinder::LexicalScope(VarBinder()) + : varbinder::LexicalScope::Enter( + VarBinder(), func->Scope()->ParamScope()); auto *funcParamScope = funcParamCtx.GetScope(); - auto funcCtx = varbinder::LexicalScope(VarBinder()); + auto funcCtx = LexicalScopeCreateOrEnter(VarBinder(), func); auto *funcScope = funcCtx.GetScope(); func->Body()->AsBlockStatement()->SetScope(funcScope); @@ -723,6 +793,10 @@ void InitScopesPhaseETS::VisitClassStaticBlock(ir::ClassStaticBlock *staticBlock auto classCtx = varbinder::LexicalScope::Enter( VarBinder(), VarBinder()->GetScope()->AsClassScope()->StaticMethodScope()); + if (func->Id()->Variable() != nullptr) { + return; + } + auto [_, var] = VarBinder()->NewVarDecl(staticBlock->Start(), Allocator(), func->Id()->Name(), staticBlock); (void)_; @@ -735,8 +809,8 @@ void InitScopesPhaseETS::VisitImportNamespaceSpecifier(ir::ImportNamespaceSpecif if (importSpec->Local()->Name().Empty()) { return; } - VarBinder()->AddDecl(importSpec->Start(), importSpec->Local()->Name(), - importSpec->Local()->Name(), importSpec); + AddOrGetDecl(VarBinder(), importSpec->Local()->Name(), importSpec, importSpec->Start(), + importSpec->Local()->Name(), importSpec->Local()->Name(), importSpec); Iterate(importSpec); } @@ -793,11 +867,15 @@ void InitScopesPhaseETS::MaybeAddOverload(ir::MethodDefinition *method, ir::Iden { if (found == nullptr) { auto classCtx = varbinder::LexicalScope::Enter(VarBinder(), targetScope); - [[maybe_unused]] auto [_, var] = VarBinder()->NewVarDecl( - methodName->Start(), Allocator(), methodName->Name(), method); - var->SetScope(clsScope); - var->AddFlag(varbinder::VariableFlags::METHOD); - methodName->SetVariable(var); + + auto *var = methodName->Variable(); + if (var == nullptr) { + var = std::get<1>(VarBinder()->NewVarDecl(methodName->Start(), Allocator(), + methodName->Name(), method)); + var->SetScope(clsScope); + var->AddFlag(varbinder::VariableFlags::METHOD); + methodName->SetVariable(var); + } for (auto *overload : method->Overloads()) { ASSERT((overload->Function()->Flags() & ir::ScriptFunctionFlags::OVERLOAD)); overload->Id()->SetVariable(var); @@ -847,6 +925,9 @@ void InitScopesPhaseETS::VisitETSImportDeclaration(ir::ETSImportDeclaration *imp void InitScopesPhaseETS::VisitTSEnumMember(ir::TSEnumMember *enumMember) { auto ident = enumMember->Key()->AsIdentifier(); + if (ident->Variable() != nullptr) { + return; + } auto [decl, var] = VarBinder()->NewVarDecl(ident->Start(), ident->Name()); var->SetScope(VarBinder()->GetScope()); var->AddFlag(varbinder::VariableFlags::STATIC); @@ -871,8 +952,8 @@ void InitScopesPhaseETS::VisitMethodDefinition(ir::MethodDefinition *method) void InitScopesPhaseETS::VisitETSFunctionType(ir::ETSFunctionType *funcType) { - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); - varbinder::LexicalScope lexicalScope(VarBinder()); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), funcType->TypeParams()); + auto lexicalScope = LexicalScopeCreateOrEnter(VarBinder(), funcType); auto *funcParamScope = lexicalScope.GetScope(); BindScopeNode(funcParamScope, funcType); Iterate(funcType); @@ -889,7 +970,7 @@ void InitScopesPhaseETS::VisitETSNewClassInstanceExpression(ir::ETSNewClassInsta ASSERT(parentClassScope->Parent()); parentClassScope = parentClassScope->Parent(); } - auto classCtx = varbinder::LexicalScope(VarBinder()); + auto classCtx = LexicalScopeCreateOrEnter(VarBinder(), newClassExpr->ClassDefinition()); util::UString anonymousName(util::StringView("#"), Allocator()); anonymousName.Append(std::to_string(parentClassScope->AsClassScope()->GetAndIncrementAnonymousClassIdx())); classDef->SetInternalName(anonymousName.View()); @@ -901,6 +982,9 @@ void InitScopesPhaseETS::VisitETSNewClassInstanceExpression(ir::ETSNewClassInsta void InitScopesPhaseETS::VisitTSTypeParameter(ir::TSTypeParameter *typeParam) { + if (typeParam->Name()->Variable() != nullptr) { + return; + } auto [decl, var] = VarBinder()->NewVarDecl(typeParam->Name()->Start(), typeParam->Name()->Name()); typeParam->Name()->SetVariable(var); @@ -912,46 +996,50 @@ void InitScopesPhaseETS::VisitTSTypeParameter(ir::TSTypeParameter *typeParam) void InitScopesPhaseETS::VisitTSInterfaceDeclaration(ir::TSInterfaceDeclaration *interfaceDecl) { { - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), interfaceDecl->TypeParams()); CallNode(interfaceDecl->TypeParams()); CallNode(interfaceDecl->Extends()); - auto localScope = varbinder::LexicalScope(VarBinder()); + auto localScope = LexicalScopeCreateOrEnter(VarBinder(), interfaceDecl); CallNode(interfaceDecl->Body()); BindScopeNode(localScope.GetScope(), interfaceDecl); } auto name = FormInterfaceOrEnumDeclarationIdBinding(interfaceDecl->Id()); - auto *decl = - VarBinder()->AddDecl(interfaceDecl->Start(), Allocator(), name, interfaceDecl); + auto *decl = AddOrGetDecl(VarBinder(), name, interfaceDecl, interfaceDecl->Start(), + Allocator(), name, interfaceDecl); decl->AsInterfaceDecl()->Add(interfaceDecl); } void InitScopesPhaseETS::VisitTSEnumDeclaration(ir::TSEnumDeclaration *enumDecl) { { - const auto enumCtx = varbinder::LexicalScope(VarBinder()); + const auto enumCtx = LexicalScopeCreateOrEnter(VarBinder(), enumDecl); BindScopeNode(enumCtx.GetScope(), enumDecl); Iterate(enumDecl); } auto name = FormInterfaceOrEnumDeclarationIdBinding(enumDecl->Key()); - auto *decl = - VarBinder()->AddDecl(enumDecl->Start(), name, enumDecl, enumDecl->IsConst()); + auto *decl = AddOrGetDecl(VarBinder(), name, enumDecl, enumDecl->Start(), name, + enumDecl, enumDecl->IsConst()); decl->BindScope(enumDecl->Scope()); } void InitScopesPhaseETS::VisitTSTypeAliasDeclaration(ir::TSTypeAliasDeclaration *typeAlias) { - VarBinder()->AddDecl(typeAlias->Id()->Start(), typeAlias->Id()->Name(), typeAlias); - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + AddOrGetDecl(VarBinder(), typeAlias->Id()->Name(), typeAlias, typeAlias->Id()->Start(), + typeAlias->Id()->Name(), typeAlias); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), typeAlias->TypeParams()); Iterate(typeAlias); } void InitScopesPhaseETS::AddGlobalToBinder(parser::Program *program) { auto globalId = program->GlobalClass()->Ident(); + if (globalId->Variable() != nullptr) { + return; + } auto [decl2, var] = program->VarBinder()->NewVarDecl(globalId->Start(), globalId->Name()); - auto classCtx = varbinder::LexicalScope(program->VarBinder()); + auto classCtx = LexicalScopeCreateOrEnter(program->VarBinder(), program->GlobalClass()); classCtx.GetScope()->BindNode(program->GlobalClass()); program->GlobalClass()->SetScope(classCtx.GetScope()); @@ -984,9 +1072,9 @@ void InitScopesPhaseETS::VisitClassDefinition(ir::ClassDefinition *classDef) ParseGlobalClass(classDef); return; } - auto typeParamsCtx = varbinder::LexicalScope(VarBinder()); + auto typeParamsCtx = LexicalScopeCreateOrEnter(VarBinder(), classDef->TypeParams()); CallNode(classDef->TypeParams()); - auto classCtx = varbinder::LexicalScope(VarBinder()); + auto classCtx = LexicalScopeCreateOrEnter(VarBinder(), classDef); IterateNoTParams(classDef); FilterOverloads(classDef->Body()); @@ -1027,13 +1115,16 @@ void InitScopesPhaseETS::FilterOverloads(ArenaVector &prop void InitScopesPhaseETS::VisitClassProperty(ir::ClassProperty *classProp) { auto curScope = VarBinder()->GetScope(); + const auto name = classProp->Key()->AsIdentifier()->Name(); if (classProp->IsClassStaticBlock()) { ASSERT(curScope->IsClassScope()); auto classCtx = varbinder::LexicalScope::Enter( VarBinder(), curScope->AsClassScope()->StaticMethodScope()); - auto [_, var] = VarBinder()->NewVarDecl(classProp->Start(), Allocator(), - classProp->Id()->Name(), classProp); - (void)_; + auto *var = classProp->Id()->Variable(); + if (var == nullptr) { + var = std::get<1>(VarBinder()->NewVarDecl(classProp->Start(), Allocator(), + classProp->Id()->Name(), classProp)); + } var->AddFlag(varbinder::VariableFlags::METHOD); classProp->AsClassStaticBlock()->Function()->Id()->SetVariable(var); } else if (classProp->IsConst()) { @@ -1047,11 +1138,9 @@ void InitScopesPhaseETS::VisitClassProperty(ir::ClassProperty *classProp) } ThrowSyntaxError("Missing initializer in const declaration", pos); } - VarBinder()->AddDecl(classProp->Key()->Start(), classProp->Key()->AsIdentifier()->Name(), - classProp); + AddOrGetDecl(VarBinder(), name, classProp, classProp->Key()->Start(), name, classProp); } else { - VarBinder()->AddDecl(classProp->Key()->Start(), classProp->Key()->AsIdentifier()->Name(), - classProp); + AddOrGetDecl(VarBinder(), name, classProp, classProp->Key()->Start(), name, classProp); } Iterate(classProp); } diff --git a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h index f3a918695f59949218ff8a6e7ecf47ecde62462a..5f5299f6940247ec480f2e35857a0a15b55ba7d3 100644 --- a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h +++ b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h @@ -180,9 +180,9 @@ protected: void BindClassDefinition(ir::ClassDefinition *classDef); - std::tuple AddVarDecl(ir::VariableDeclaratorFlag flag, - lexer::SourcePosition startLoc, - const util::StringView &name); + std::tuple AddOrGetVarDecl(ir::VariableDeclaratorFlag flag, + lexer::SourcePosition startLoc, + const ir::Identifier *id); virtual void BindVarDecl([[maybe_unused]] ir::Identifier *binding, ir::Expression *init, varbinder::Decl *decl, [[maybe_unused]] varbinder::Variable *var); diff --git a/ets2panda/compiler/lowering/util.cpp b/ets2panda/compiler/lowering/util.cpp index cde02f0b615d68c9cff3e5b0ad3300eb3255fe1e..494c9ce361542bbd807e235e1a7de5b6174a40bf 100644 --- a/ets2panda/compiler/lowering/util.cpp +++ b/ets2panda/compiler/lowering/util.cpp @@ -43,9 +43,12 @@ util::UString GenName(ArenaAllocator *const allocator) } // Function to clear expression node types and identifier node variables (for correct re-binding and re-checking) -void ClearTypesAndVariables(ir::AstNode *node) noexcept +void ClearTypesVariablesAndScopes(ir::AstNode *node) noexcept { node->Iterate([](ir::AstNode *child) -> void { + if (child->IsScopeBearer()) { + child->ClearScope(); + } if (child->IsExpression()) { auto *expression = child->AsExpression(); if (!expression->IsTypeNode()) { @@ -56,7 +59,7 @@ void ClearTypesAndVariables(ir::AstNode *node) noexcept return; } } - ClearTypesAndVariables(child); + ClearTypesVariablesAndScopes(child); }); } } // namespace ark::es2panda::compiler diff --git a/ets2panda/compiler/lowering/util.h b/ets2panda/compiler/lowering/util.h index c25db6c2553b9544d88f550e5c47546237c54a0f..32e9c3bba0b6f08494509b9657737fea895c6467 100644 --- a/ets2panda/compiler/lowering/util.h +++ b/ets2panda/compiler/lowering/util.h @@ -23,7 +23,7 @@ namespace ark::es2panda::compiler { varbinder::Scope *NearestScope(const ir::AstNode *ast); ir::Identifier *Gensym(ArenaAllocator *allocator); util::UString GenName(ArenaAllocator *allocator); -void ClearTypesAndVariables(ir::AstNode *node) noexcept; +void ClearTypesVariablesAndScopes(ir::AstNode *node) noexcept; } // namespace ark::es2panda::compiler diff --git a/ets2panda/ir/astNode.cpp b/ets2panda/ir/astNode.cpp index 64d2983a31160b8fec0721ab189d9424e91a10fc..7ae10b9f1db25661d5dc66704af93abccd371ae3 100644 --- a/ets2panda/ir/astNode.cpp +++ b/ets2panda/ir/astNode.cpp @@ -39,6 +39,11 @@ AstNode::AstNode(AstNode const &other) return (flags_ & ModifierFlags::EXPORT_TYPE) != 0; } +void AstNode::ClearScope() noexcept +{ + UNREACHABLE(); +} + template static R GetTopStatementImpl(T *self) { diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index e2a69f5e99e140061baad105434cdcb7e4ea6eaa..676096f5267d794f1dca0600ce348f6f2a5237b2 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -20,6 +20,7 @@ #include "ir/astNodeMapping.h" #include "ir/visitor/AstVisitor.h" #include "lexer/token/sourceLocation.h" +#include "macros.h" #include "util/enumbitops.h" namespace ark::es2panda::compiler { @@ -477,6 +478,8 @@ public: UNREACHABLE(); } + virtual void ClearScope() noexcept; + [[nodiscard]] ir::BlockStatement *GetTopStatement(); [[nodiscard]] const ir::BlockStatement *GetTopStatement() const; diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index ffd34027a957eabbb6404145013bab429c7373cb..98bff6db393b19fe7427a64ac135c800812e210c 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -54,6 +54,7 @@ enum class ModifierFlags : uint32_t { DEFAULT_EXPORT = 1U << 23U, EXPORT_TYPE = 1U << 24U, EXTERNAL = 1U << 25U, + SUPER_OWNER = 1U << 26U, ACCESS = PUBLIC | PROTECTED | PRIVATE | INTERNAL, ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT, ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY, @@ -86,7 +87,8 @@ enum class ScriptFunctionFlags : uint32_t { SETTER = 1U << 16U, ENTRY_POINT = 1U << 17U, INSTANCE_EXTENSION_METHOD = 1U << 18U, - HAS_RETURN = 1U << 19U + HAS_RETURN = 1U << 19U, + ASYNC_IMPL = 1U << 20U, }; enum class TSOperatorType { READONLY, KEYOF, UNIQUE }; diff --git a/ets2panda/ir/base/catchClause.h b/ets2panda/ir/base/catchClause.h index 69b7561b26116df248ee4709945509d2e639d0cc..008d80366a4683fbed7111b25d4cae216ae84e67 100644 --- a/ets2panda/ir/base/catchClause.h +++ b/ets2panda/ir/base/catchClause.h @@ -66,6 +66,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + bool IsDefaultCatchClause() const; void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/base/classDefinition.h b/ets2panda/ir/base/classDefinition.h index 6a0f7816d8249dbc876a45b818ecc498b93a185f..36c058931ae0b4aa13de79e750e80d2f2763b2a4 100644 --- a/ets2panda/ir/base/classDefinition.h +++ b/ets2panda/ir/base/classDefinition.h @@ -130,6 +130,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + [[nodiscard]] const Identifier *Ident() const noexcept { return ident_; diff --git a/ets2panda/ir/base/methodDefinition.h b/ets2panda/ir/base/methodDefinition.h index 553cc44d1887add24dd2ae4a7b8967266017cab7..50f23a652814df42b937108720589b8f4c620956 100644 --- a/ets2panda/ir/base/methodDefinition.h +++ b/ets2panda/ir/base/methodDefinition.h @@ -43,7 +43,9 @@ public: ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed) : ClassElement(AstNodeType::METHOD_DEFINITION, key, value, modifiers, allocator, isComputed), kind_(kind), - overloads_(allocator->Adapter()) + overloads_(allocator->Adapter()), + baseOverloadMethod_(nullptr), + asyncPairMethod_(nullptr) { ASSERT(key_ != nullptr && value_ != nullptr); } @@ -71,6 +73,26 @@ public: return overloads_; } + [[nodiscard]] const MethodDefinition *BaseOverloadMethod() const noexcept + { + return baseOverloadMethod_; + } + + [[nodiscard]] MethodDefinition *BaseOverloadMethod() noexcept + { + return baseOverloadMethod_; + } + + [[nodiscard]] const MethodDefinition *AsyncPairMethod() const noexcept + { + return asyncPairMethod_; + } + + [[nodiscard]] MethodDefinition *AsyncPairMethod() noexcept + { + return asyncPairMethod_; + } + void SetOverloads(OverloadsT &&overloads) { overloads_ = std::move(overloads); @@ -84,6 +106,17 @@ public: void AddOverload(MethodDefinition *const overload) { overloads_.emplace_back(overload); + overload->SetBaseOverloadMethod(this); + } + + void SetBaseOverloadMethod(MethodDefinition *const baseOverloadMethod) + { + baseOverloadMethod_ = baseOverloadMethod; + } + + void SetAsyncPairMethod(MethodDefinition *const method) + { + asyncPairMethod_ = method; } [[nodiscard]] bool HasOverload(MethodDefinition *overload) noexcept @@ -116,7 +149,14 @@ public: private: MethodDefinitionKind kind_; + // Overloads are stored like in an 1:N fashion. + // The very firstly processed method becomes the base(1) and the others tied into it as overloads(N). OverloadsT overloads_; + // Base overload method points at the first overload of the overloads. + MethodDefinition *baseOverloadMethod_; + // Pair method points at the original async method in case of an implement method and vice versa an implement + // method's point at the async method + MethodDefinition *asyncPairMethod_; }; } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index 7511ae3a2617dace7ac512f70920441d3d8df77e..dcd188b9554842d6c034e7b525fde6c68197ccc2 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -150,6 +150,11 @@ public: return (funcFlags_ & ir::ScriptFunctionFlags::ASYNC) != 0; } + [[nodiscard]] bool IsAsyncImplFunc() const noexcept + { + return (funcFlags_ & ir::ScriptFunctionFlags::ASYNC_IMPL) != 0; + } + [[nodiscard]] bool IsArrow() const noexcept { return (funcFlags_ & ir::ScriptFunctionFlags::ARROW) != 0; @@ -220,6 +225,11 @@ public: return signature_->RestVar() != nullptr; } + [[nodiscard]] bool HasReturnStatement() const noexcept + { + return (funcFlags_ & ir::ScriptFunctionFlags::HAS_RETURN) != 0; + } + [[nodiscard]] bool IsThrowing() const noexcept { return (funcFlags_ & ir::ScriptFunctionFlags::THROWS) != 0; @@ -284,6 +294,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + [[nodiscard]] es2panda::Language Language() const noexcept { return lang_; diff --git a/ets2panda/ir/base/tsMethodSignature.h b/ets2panda/ir/base/tsMethodSignature.h index 293156ad2eb5d921d2bd174124bca1873067fb1c..dfc7998936e5d32eac3b157f690a49ee86bfd751 100644 --- a/ets2panda/ir/base/tsMethodSignature.h +++ b/ets2panda/ir/base/tsMethodSignature.h @@ -63,6 +63,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + [[nodiscard]] const Expression *Key() const noexcept { return key_; diff --git a/ets2panda/ir/base/tsSignatureDeclaration.h b/ets2panda/ir/base/tsSignatureDeclaration.h index 034b9a40d68d9d88226a2b3592b10757e49f283d..8524c7516ec8766bcc0fc9f950c62a40de637d0b 100644 --- a/ets2panda/ir/base/tsSignatureDeclaration.h +++ b/ets2panda/ir/base/tsSignatureDeclaration.h @@ -60,6 +60,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + [[nodiscard]] const TSTypeParameterDeclaration *TypeParams() const noexcept { return signature_.TypeParams(); diff --git a/ets2panda/ir/ets/etsFunctionType.h b/ets2panda/ir/ets/etsFunctionType.h index cf1539f8f205753c53fd61a9d2986bd09fd1b71f..deffd301fa3a81d697d0b096589f7761a033d4ce 100644 --- a/ets2panda/ir/ets/etsFunctionType.h +++ b/ets2panda/ir/ets/etsFunctionType.h @@ -49,11 +49,21 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const TSTypeParameterDeclaration *TypeParams() const { return signature_.TypeParams(); } + TSTypeParameterDeclaration *TypeParams() + { + return signature_.TypeParams(); + } + const ArenaVector &Params() const { return signature_.Params(); diff --git a/ets2panda/ir/expressions/blockExpression.h b/ets2panda/ir/expressions/blockExpression.h index 1b5a7c073ec3f5e29f193efb7346267416e2e293..a28acc723010771939a5b7eda2cccf7f6cf11dc6 100644 --- a/ets2panda/ir/expressions/blockExpression.h +++ b/ets2panda/ir/expressions/blockExpression.h @@ -84,6 +84,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; diff --git a/ets2panda/ir/expressions/literals/stringLiteral.cpp b/ets2panda/ir/expressions/literals/stringLiteral.cpp index 6112363bc6c761ae97866832edac7f9a8504d017..cb8c10f17c991bf952099c945f8cd439971c1d78 100644 --- a/ets2panda/ir/expressions/literals/stringLiteral.cpp +++ b/ets2panda/ir/expressions/literals/stringLiteral.cpp @@ -34,6 +34,8 @@ void StringLiteral::Dump(ir::AstDumper *dumper) const dumper->Add({{"type", "StringLiteral"}, {"value", str_}}); } +static unsigned int constexpr CHAR_UPPER_HALF = 128U; + void StringLiteral::Dump(ir::SrcDumper *dumper) const { std::string str(str_); @@ -44,7 +46,7 @@ void StringLiteral::Dump(ir::SrcDumper *dumper) const 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) { + if (std::isprint(static_cast(c)) != 0U || static_cast(c) >= CHAR_UPPER_HALF) { escapedStr.push_back(c); } else { escapedStr.push_back('\\'); diff --git a/ets2panda/ir/statements/blockStatement.h b/ets2panda/ir/statements/blockStatement.h index 277f4ef156a1dc19d6b34b4c1f34ca1973ea91fd..04fe521c868a6ac7a88b56cbff006414eef8a3af 100644 --- a/ets2panda/ir/statements/blockStatement.h +++ b/ets2panda/ir/statements/blockStatement.h @@ -50,6 +50,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const ArenaVector &Statements() const { return statements_; diff --git a/ets2panda/ir/statements/loopStatement.h b/ets2panda/ir/statements/loopStatement.h index 7d996280d0aa868cc28af6716bb7469ce04ef930..4a5753edf2695d1cff7691b07e1d7167346e374c 100644 --- a/ets2panda/ir/statements/loopStatement.h +++ b/ets2panda/ir/statements/loopStatement.h @@ -44,6 +44,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + void TransformChildren([[maybe_unused]] const NodeTransformer &cb, [[maybe_unused]] std::string_view const transformationName) override { diff --git a/ets2panda/ir/statements/switchStatement.h b/ets2panda/ir/statements/switchStatement.h index 2785d038485aa4d6a3e660b55f94f87c1196cac4..82cfd63397cc19f43f8e31df178a4e5ce891bfdb 100644 --- a/ets2panda/ir/statements/switchStatement.h +++ b/ets2panda/ir/statements/switchStatement.h @@ -77,6 +77,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/ts/tsAsExpression.cpp b/ets2panda/ir/ts/tsAsExpression.cpp index 03f6a92d21a5098338141b84d0892ee0bc95434b..aa4f4ac163bff795c53b0dfd4a953f913c2a3c03 100644 --- a/ets2panda/ir/ts/tsAsExpression.cpp +++ b/ets2panda/ir/ts/tsAsExpression.cpp @@ -63,7 +63,10 @@ void TSAsExpression::Dump(ir::AstDumper *dumper) const void TSAsExpression::Dump(ir::SrcDumper *dumper) const { - dumper->Add("TSAsExpression"); + dumper->Add("("); + expression_->Dump(dumper); + dumper->Add(") as "); + TypeAnnotation()->Dump(dumper); } void TSAsExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const diff --git a/ets2panda/ir/ts/tsConstructorType.h b/ets2panda/ir/ts/tsConstructorType.h index 3cddadfd8ffd73b4d888ac03b1e83bb3be7f5c1b..33cb620a698dfd816490b886104f300fddee8673 100644 --- a/ets2panda/ir/ts/tsConstructorType.h +++ b/ets2panda/ir/ts/tsConstructorType.h @@ -52,6 +52,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const TSTypeParameterDeclaration *TypeParams() const { return signature_.TypeParams(); diff --git a/ets2panda/ir/ts/tsEnumDeclaration.h b/ets2panda/ir/ts/tsEnumDeclaration.h index d414546a538503fc9068691d32b1e5ca0c1739a9..fa134e96e320a415cab25b06c89d7fe20d93aa69 100644 --- a/ets2panda/ir/ts/tsEnumDeclaration.h +++ b/ets2panda/ir/ts/tsEnumDeclaration.h @@ -63,6 +63,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const Identifier *Key() const { return key_; diff --git a/ets2panda/ir/ts/tsFunctionType.h b/ets2panda/ir/ts/tsFunctionType.h index b81678b8b5c84952744b0ef804aa413733486bdc..5c02894401abf005ecc646016669577fa0f902ef 100644 --- a/ets2panda/ir/ts/tsFunctionType.h +++ b/ets2panda/ir/ts/tsFunctionType.h @@ -50,6 +50,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const TSTypeParameterDeclaration *TypeParams() const { return signature_.TypeParams(); diff --git a/ets2panda/ir/ts/tsInterfaceDeclaration.h b/ets2panda/ir/ts/tsInterfaceDeclaration.h index d1c876b069ed7e083d20a0e6f5073134b96fb794..065bf31a22c64253ad1f4df06978d81ccbc8175e 100644 --- a/ets2panda/ir/ts/tsInterfaceDeclaration.h +++ b/ets2panda/ir/ts/tsInterfaceDeclaration.h @@ -66,6 +66,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + TSInterfaceBody *Body() { return body_; diff --git a/ets2panda/ir/ts/tsModuleBlock.h b/ets2panda/ir/ts/tsModuleBlock.h index 877eae789e594e312e8b2d6f248e8f0398b7148f..5978b77400ff7c4eef0ef73139dd23a170fac747 100644 --- a/ets2panda/ir/ts/tsModuleBlock.h +++ b/ets2panda/ir/ts/tsModuleBlock.h @@ -43,6 +43,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const ArenaVector &Statements() const { return statements_; diff --git a/ets2panda/ir/ts/tsModuleDeclaration.h b/ets2panda/ir/ts/tsModuleDeclaration.h index 304c38fac6295637518f51d3cb39de31ef592a4b..0e94fef11d16c9c06a262e2d5401999b7f473109 100644 --- a/ets2panda/ir/ts/tsModuleDeclaration.h +++ b/ets2panda/ir/ts/tsModuleDeclaration.h @@ -52,6 +52,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const Expression *Name() const { return name_; diff --git a/ets2panda/ir/ts/tsTypeParameterDeclaration.h b/ets2panda/ir/ts/tsTypeParameterDeclaration.h index a145e0dcec89c884645787bb849958aeb3b075ff..7719694e815c39b9c8b2167e781cbdd1742c11a4 100644 --- a/ets2panda/ir/ts/tsTypeParameterDeclaration.h +++ b/ets2panda/ir/ts/tsTypeParameterDeclaration.h @@ -47,6 +47,11 @@ public: scope_ = scope; } + void ClearScope() noexcept override + { + scope_ = nullptr; + } + const ArenaVector &Params() const { return params_; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 4ee4c681c655011dab9135e73a3d4f45f786fbaf..677031d49dffd47583e99aa81a419f3a0322f264 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -2411,8 +2411,16 @@ ir::ImportSource *ETSParser::ParseSourceFromClause(bool requireFrom) auto importPath = Lexer()->GetToken().Ident(); auto resolvedImportPath = importPathManager_->ResolvePath(GetProgram()->AbsoluteName(), importPath); - importPathManager_->AddToParseList(resolvedImportPath, - (GetContext().Status() & ParserStatus::IN_DEFAULT_IMPORTS) != 0U); + if (globalProgram_->AbsoluteName() != resolvedImportPath) { + importPathManager_->AddToParseList(resolvedImportPath, + (GetContext().Status() & ParserStatus::IN_DEFAULT_IMPORTS) != 0U); + } else { + if (!IsETSModule()) { + ThrowSyntaxError("Please compile `" + globalProgram_->FileName().Mutf8() + "." + + globalProgram_->SourceFile().GetExtension().Mutf8() + + "` with `--ets-module` option. It is being imported by another file."); + } + } auto *resolvedSource = AllocNode(resolvedImportPath); auto importData = importPathManager_->GetImportData(resolvedImportPath, Extension()); diff --git a/ets2panda/test/compiler/ets/lowering-interaction-expected.txt b/ets2panda/test/compiler/ets/lowering-interaction-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..190e0b48fde61a498fc3df9bc60f514fd46eb234 --- /dev/null +++ b/ets2panda/test/compiler/ets/lowering-interaction-expected.txt @@ -0,0 +1,1289 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "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": "IR", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "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": 18, + "column": 10 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "CIterator", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "ind_", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 11 + }, + "end": { + "line": 24, + "column": 15 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 24, + "column": 23 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "accessibility": "private", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 11 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "base_", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 11 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, + "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": "C", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 18 + }, + "end": { + "line": 25, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 18 + }, + "end": { + "line": 25, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 18 + }, + "end": { + "line": 25, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 21 + }, + "end": { + "line": 25, + "column": 22 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 11 + }, + "end": { + "line": 25, + "column": 22 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "base", + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 22 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 22 + }, + "end": { + "line": 27, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 22 + }, + "end": { + "line": 27, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 26 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 27, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 27, + "column": 26 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 9 + } + } + }, + "property": { + "type": "Identifier", + "name": "base_", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 10 + }, + "end": { + "line": 28, + "column": 15 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 15 + } + } + }, + "right": { + "type": "Identifier", + "name": "base", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 27 + }, + "end": { + "line": 29, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 15 + }, + "end": { + "line": 29, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 15 + }, + "end": { + "line": 29, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 3 + }, + "end": { + "line": 29, + "column": 4 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "next", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 3 + }, + "end": { + "line": 31, + "column": 7 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "next", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 3 + }, + "end": { + "line": 31, + "column": 7 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "IR", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 11 + }, + "end": { + "line": 31, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 11 + }, + "end": { + "line": 31, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 11 + }, + "end": { + "line": 31, + "column": 15 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ObjectExpression", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 7 + }, + "end": { + "line": 33, + "column": 12 + } + } + }, + "value": { + "type": "MemberExpression", + "object": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 33, + "column": 18 + } + } + }, + "property": { + "type": "Identifier", + "name": "base_", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 19 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 33, + "column": 24 + } + } + }, + "property": { + "type": "AssignmentExpression", + "operator": "+=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 29 + } + } + }, + "property": { + "type": "Identifier", + "name": "ind_", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 30 + }, + "end": { + "line": 33, + "column": 34 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 34 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 33, + "column": 38 + }, + "end": { + "line": 33, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 25 + }, + "end": { + "line": 33, + "column": 39 + } + } + }, + "computed": true, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 33, + "column": 7 + }, + "end": { + "line": 33, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 12 + }, + "end": { + "line": 34, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 34, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 35, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 7 + }, + "end": { + "line": 35, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 7 + }, + "end": { + "line": 35, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 3 + }, + "end": { + "line": 35, + "column": 4 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 36, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 36, + "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": 38, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/lowering-interaction.ets b/ets2panda/test/compiler/ets/lowering-interaction.ets new file mode 100644 index 0000000000000000000000000000000000000000..8afc147d6053655a5fd01617592fb4ad2dc5dc7d --- /dev/null +++ b/ets2panda/test/compiler/ets/lowering-interaction.ets @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2024 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 C {} + +class IR { + value: C +} + +class CIterator { + + private ind_: int = 0; + private base_: C[]; + + constructor (base: C[]) { + this.base_ = base; + } + + next(): IR { + return { + value: this.base_[this.ind_ += 1] + } + } +} + diff --git a/ets2panda/test/options/CMakeLists.txt b/ets2panda/test/options/CMakeLists.txt index dd46aabd24b3a973ddc8c820bc2c5ce49e6e2a7f..8a7e1fbbc57e5fcebee1ea660cabee96909d071c 100644 --- a/ets2panda/test/options/CMakeLists.txt +++ b/ets2panda/test/options/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 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 @@ -34,3 +34,4 @@ check_option_help_otput(bco_opt "--help" "bco-optimizer:") check_option_help_otput(bco_opt_help "--bco-optimizer --help" "bytecode-opt-peepholes:") check_option_help_otput(comp_opt "--help" "bco-compiler:") check_option_help_otput(comp_opt_help "--bco-compiler --help" "compiler-disasm-dump:") +check_option_help_otput(opt_level "--opt-level 7" "out of range parameter value \\\"7\\\"") diff --git a/ets2panda/test/parser/ets/InferTypeParamFromParam1-expected.txt b/ets2panda/test/parser/ets/InferTypeParamFromParam1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3e0fb010ec40ba851798242ced52ff1d0108284d --- /dev/null +++ b/ets2panda/test/parser/ets/InferTypeParamFromParam1-expected.txt @@ -0,0 +1,2331 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 27 + } + } + }, + "id": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "id": { + "type": "Identifier", + "name": "BInterface", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "extends": [ + { + "type": "TSInterfaceHeritage", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 43 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 46 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 48 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "BInterface", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 39 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 42 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 43 + } + } + } + ], + "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": 18, + "column": 44 + }, + "end": { + "line": 18, + "column": 44 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 18, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 44 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 27 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 49 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 50 + }, + "end": { + "line": 20, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 50 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 50 + }, + "end": { + "line": 20, + "column": 52 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 49 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 53 + } + } + } + ], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 42 + }, + "end": { + "line": 21, + "column": 52 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 53 + }, + "end": { + "line": 21, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 53 + }, + "end": { + "line": 21, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 53 + }, + "end": { + "line": 21, + "column": 55 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 52 + }, + "end": { + "line": 21, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 42 + }, + "end": { + "line": 21, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 42 + }, + "end": { + "line": 21, + "column": 56 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 56 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 59 + }, + "end": { + "line": 21, + "column": 70 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 71 + }, + "end": { + "line": 21, + "column": 72 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 71 + }, + "end": { + "line": 21, + "column": 73 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 71 + }, + "end": { + "line": 21, + "column": 73 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 70 + }, + "end": { + "line": 21, + "column": 73 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 59 + }, + "end": { + "line": 21, + "column": 75 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 59 + }, + "end": { + "line": 21, + "column": 75 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 34 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 34 + }, + "end": { + "line": 21, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 31 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 31 + }, + "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 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 19 + }, + "end": { + "line": 25, + "column": 33 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 19 + }, + "end": { + "line": 25, + "column": 33 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 42 + }, + "end": { + "line": 25, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 42 + }, + "end": { + "line": 25, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 42 + }, + "end": { + "line": 25, + "column": 49 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 37 + }, + "end": { + "line": 25, + "column": 49 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 34 + }, + "end": { + "line": 25, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 34 + }, + "end": { + "line": 25, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 33 + }, + "end": { + "line": 25, + "column": 36 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "arg", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 26, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 50 + }, + "end": { + "line": 27, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 33 + }, + "end": { + "line": 27, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 33 + }, + "end": { + "line": 27, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 27, + "column": 6 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 23, + "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": 28, + "column": 2 + }, + "end": { + "line": 28, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 52 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 28, + "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": "func", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 10 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "func", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 10 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 22 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 23 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 23 + }, + "end": { + "line": 30, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 23 + }, + "end": { + "line": 30, + "column": 30 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 22 + }, + "end": { + "line": 30, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 16 + }, + "end": { + "line": 30, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 16 + }, + "end": { + "line": 30, + "column": 31 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 23 + } + } + }, + "property": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 38 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 38 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "arg", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 39 + }, + "end": { + "line": 31, + "column": 42 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 31, + "column": 12 + }, + "end": { + "line": 31, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 44 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 32 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 10 + }, + "end": { + "line": 34, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 10 + }, + "end": { + "line": 34, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 10 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "func", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 17 + } + } + }, + "arguments": [ + { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 22 + }, + "end": { + "line": 35, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 24 + }, + "end": { + "line": 35, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 24 + }, + "end": { + "line": 35, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 24 + }, + "end": { + "line": 35, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 35, + "column": 23 + }, + "end": { + "line": 35, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 22 + }, + "end": { + "line": 35, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 22 + }, + "end": { + "line": 35, + "column": 32 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 35, + "column": 18 + }, + "end": { + "line": 35, + "column": 34 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 35, + "column": 13 + }, + "end": { + "line": 35, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 9 + }, + "end": { + "line": 35, + "column": 34 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 35, + "column": 5 + }, + "end": { + "line": 35, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 36, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 14 + }, + "end": { + "line": 36, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 14 + }, + "end": { + "line": 36, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 1 + }, + "end": { + "line": 36, + "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": 37, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/InferTypeParamFromParam1.ets b/ets2panda/test/parser/ets/InferTypeParamFromParam1.ets new file mode 100644 index 0000000000000000000000000000000000000000..7b14279d1fbacf035a2c1f5d4146015d0df84a2e --- /dev/null +++ b/ets2panda/test/parser/ets/InferTypeParamFromParam1.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 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 AInterface {} +interface BInterface extends AInterface {} +final class B implements BInterface{} + +final class StaticClass implements AInterface{ + public static staticFunction(arg: AInterface) : StaticClass { + return new StaticClass(); + } + + public static staticFunction(arg: number) { + return arg; + } +} + +function func (arg: B) { + return StaticClass.staticFunction(arg); +} + +function main() { + let a = func(new B()); +} diff --git a/ets2panda/test/parser/ets/InferTypeParamFromParam2-expected.txt b/ets2panda/test/parser/ets/InferTypeParamFromParam2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..f7a5e1ed6dee8f843f9ab402e381b8eb1141951b --- /dev/null +++ b/ets2panda/test/parser/ets/InferTypeParamFromParam2-expected.txt @@ -0,0 +1,2136 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 27 + } + } + }, + "id": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "id": { + "type": "Identifier", + "name": "BInterface", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "extends": [ + { + "type": "TSInterfaceHeritage", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 43 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 46 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 48 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "BInterface", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 39 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 42 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 43 + } + } + } + ], + "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": 18, + "column": 44 + }, + "end": { + "line": 18, + "column": 44 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 18, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 44 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 27 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 49 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 50 + }, + "end": { + "line": 20, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 50 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 50 + }, + "end": { + "line": 20, + "column": 52 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 49 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 39 + }, + "end": { + "line": 20, + "column": 53 + } + } + } + ], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 57 + }, + "end": { + "line": 21, + "column": 67 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 68 + }, + "end": { + "line": 21, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 68 + }, + "end": { + "line": 21, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 68 + }, + "end": { + "line": 21, + "column": 70 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 67 + }, + "end": { + "line": 21, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 57 + }, + "end": { + "line": 21, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 57 + }, + "end": { + "line": 21, + "column": 71 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 52 + }, + "end": { + "line": 21, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 52 + }, + "end": { + "line": 21, + "column": 71 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 21, + "column": 85 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 86 + }, + "end": { + "line": 21, + "column": 87 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 86 + }, + "end": { + "line": 21, + "column": 88 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 86 + }, + "end": { + "line": 21, + "column": 88 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 85 + }, + "end": { + "line": 21, + "column": 88 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 21, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 21, + "column": 90 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 34 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "constraint": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 44 + }, + "end": { + "line": 21, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 44 + }, + "end": { + "line": 21, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 44 + }, + "end": { + "line": 21, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 34 + }, + "end": { + "line": 21, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 21, + "column": 51 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 31 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 31 + }, + "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 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 89 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 23, + "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": 24, + "column": 2 + }, + "end": { + "line": 24, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 52 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "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": "func", + "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": "func", + "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": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 22 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 23 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 23 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 23 + }, + "end": { + "line": 26, + "column": 30 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 31 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + "property": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 24 + }, + "end": { + "line": 27, + "column": 38 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 38 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "arg", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 39 + }, + "end": { + "line": 27, + "column": 42 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 44 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 32 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 10 + }, + "end": { + "line": 30, + "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": 30, + "column": 10 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 10 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "func", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 31, + "column": 17 + } + } + }, + "arguments": [ + { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 23 + }, + "end": { + "line": 31, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 32 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 34 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 31, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 34 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 17 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 30, + "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 + } + } +} +TypeError: Type 'B' is not compatible with type 'AInterface' at index 1 [InferTypeParamFromParam2.ets:27:39] diff --git a/ets2panda/test/parser/ets/InferTypeParamFromParam2.ets b/ets2panda/test/parser/ets/InferTypeParamFromParam2.ets new file mode 100644 index 0000000000000000000000000000000000000000..31eb7151ffb38377371eb8fe08ed16f823abe9b1 --- /dev/null +++ b/ets2panda/test/parser/ets/InferTypeParamFromParam2.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 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 AInterface {} +interface BInterface extends AInterface {} +final class B implements BInterface{} + +final class StaticClass implements AInterface{ + public static staticFunction(arg: AInterface) : StaticClass { + return new StaticClass(); + } +} + +function func (arg: B) { + return StaticClass.staticFunction(arg); +} + +function main() { + let a = func(new B()); +} diff --git a/ets2panda/test/parser/ets/InferTypeParamFromParam3-expected.txt b/ets2panda/test/parser/ets/InferTypeParamFromParam3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..8273367de0d644e7d4e127be8ff08ebec45baa61 --- /dev/null +++ b/ets2panda/test/parser/ets/InferTypeParamFromParam3-expected.txt @@ -0,0 +1,2603 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 30 + } + } + }, + "id": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 55 + } + } + }, + "id": { + "type": "Identifier", + "name": "BInterface", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "extends": [ + { + "type": "TSInterfaceHeritage", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 36 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 50 + }, + "end": { + "line": 17, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 50 + }, + "end": { + "line": 17, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 50 + }, + "end": { + "line": 17, + "column": 52 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 46 + }, + "end": { + "line": 17, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 36 + }, + "end": { + "line": 17, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 36 + }, + "end": { + "line": 17, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 36 + }, + "end": { + "line": 17, + "column": 54 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "BInterface", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 45 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 46 + }, + "end": { + "line": 18, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 46 + }, + "end": { + "line": 18, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 46 + }, + "end": { + "line": 18, + "column": 48 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 18, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 49 + } + } + } + ], + "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": 18, + "column": 50 + }, + "end": { + "line": 18, + "column": 50 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 48 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 27 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 30 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 42 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 53 + }, + "end": { + "line": 20, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 53 + }, + "end": { + "line": 20, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 53 + }, + "end": { + "line": 20, + "column": 55 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 56 + }, + "end": { + "line": 20, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 56 + }, + "end": { + "line": 20, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 56 + }, + "end": { + "line": 20, + "column": 58 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 52 + }, + "end": { + "line": 20, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 42 + }, + "end": { + "line": 20, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 42 + }, + "end": { + "line": 20, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 42 + }, + "end": { + "line": 20, + "column": 59 + } + } + } + ], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "AInterface", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 54 + }, + "end": { + "line": 21, + "column": 64 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 65 + }, + "end": { + "line": 21, + "column": 66 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 65 + }, + "end": { + "line": 21, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 65 + }, + "end": { + "line": 21, + "column": 67 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 68 + }, + "end": { + "line": 21, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 68 + }, + "end": { + "line": 21, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 68 + }, + "end": { + "line": 21, + "column": 70 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 64 + }, + "end": { + "line": 21, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 54 + }, + "end": { + "line": 21, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 54 + }, + "end": { + "line": 21, + "column": 71 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 71 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 71 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 21, + "column": 85 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 86 + }, + "end": { + "line": 21, + "column": 87 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 86 + }, + "end": { + "line": 21, + "column": 88 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 86 + }, + "end": { + "line": 21, + "column": 88 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 89 + }, + "end": { + "line": 21, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 89 + }, + "end": { + "line": 21, + "column": 91 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 89 + }, + "end": { + "line": 21, + "column": 91 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 85 + }, + "end": { + "line": 21, + "column": 91 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 21, + "column": 93 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 74 + }, + "end": { + "line": 21, + "column": 93 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 34 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 34 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 38 + } + } + }, + "default": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 41 + }, + "end": { + "line": 21, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 41 + }, + "end": { + "line": 21, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 41 + }, + "end": { + "line": 21, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 48 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 21, + "column": 48 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 31 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 35 + }, + "end": { + "line": 22, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 35 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 35 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 31 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 20 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 92 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 23, + "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": 24, + "column": 2 + }, + "end": { + "line": 24, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 58 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "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": "func", + "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": "func", + "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": "arg", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 22 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 23 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 23 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 23 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 31 + }, + "end": { + "line": 26, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 31 + }, + "end": { + "line": 26, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 31 + }, + "end": { + "line": 26, + "column": 38 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 21 + }, + "end": { + "line": 26, + "column": 39 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 39 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "StaticClass", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + "property": { + "type": "Identifier", + "name": "staticFunction", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 24 + }, + "end": { + "line": 27, + "column": 38 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 38 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "arg", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 39 + }, + "end": { + "line": 27, + "column": 42 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 44 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 40 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 10 + }, + "end": { + "line": 30, + "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": 30, + "column": 10 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 10 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "func", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 31, + "column": 17 + } + } + }, + "arguments": [ + { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 31 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 32 + }, + "end": { + "line": 31, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 32 + }, + "end": { + "line": 31, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 32 + }, + "end": { + "line": 31, + "column": 39 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 23 + }, + "end": { + "line": 31, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 40 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 42 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 31, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 42 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 17 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 14 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 30, + "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/parser/ets/InferTypeParamFromParam3.ets b/ets2panda/test/parser/ets/InferTypeParamFromParam3.ets new file mode 100644 index 0000000000000000000000000000000000000000..353b1ca152416fbd7a9b277b27b2078fdb547b79 --- /dev/null +++ b/ets2panda/test/parser/ets/InferTypeParamFromParam3.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 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 AInterface {} +interface BInterface extends AInterface {} +final class B implements BInterface{} + +final class StaticClass implements AInterface{ + public static staticFunction(arg: AInterface) : StaticClass { + return new StaticClass(); + } +} + +function func (arg: B) { + return StaticClass.staticFunction(arg); +} + +function main() { + let a = func(new B()); +} diff --git a/ets2panda/test/parser/ets/classAsFunctionParam-expected.txt b/ets2panda/test/parser/ets/classAsFunctionParam-expected.txt index e04c1f64f0864a92f40df048bca9a35c6afc6bb4..280e2139e4ccd746581c0905cbd14e625438d84f 100644 --- a/ets2panda/test/parser/ets/classAsFunctionParam-expected.txt +++ b/ets2panda/test/parser/ets/classAsFunctionParam-expected.txt @@ -648,4 +648,4 @@ } } } -TypeError: Class name can't be the argument of function or method. [classAsFunctionParam.ets:23:10] +TypeError: Class or interface 'C' cannot be used as object [classAsFunctionParam.ets:23:10] diff --git a/ets2panda/test/parser/ets/class_as_object_1-expected.txt b/ets2panda/test/parser/ets/class_as_object_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9740d91ed3024152f306ebe77a6f1a6a6dd4dc26 --- /dev/null +++ b/ets2panda/test/parser/ets/class_as_object_1-expected.txt @@ -0,0 +1,338 @@ +{ + "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": "main", + "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": "main", + "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": "_$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": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 19 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 20 + } + } + } + ], + "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: Class or interface 'Object' cannot be used as object [class_as_object_1.ets:16:13] diff --git a/ets2panda/test/parser/ets/class_as_object_1.ets b/ets2panda/test/parser/ets/class_as_object_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..6ad534c73a1643595e28d61fc7dd8b42d52820a6 --- /dev/null +++ b/ets2panda/test/parser/ets/class_as_object_1.ets @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 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. + */ + +console.log(Object) diff --git a/ets2panda/test/parser/ets/interface_with_different_type-expected.txt b/ets2panda/test/parser/ets/interface_with_different_type-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..c961801813487d2efad72695247bee0e5e55487c --- /dev/null +++ b/ets2panda/test/parser/ets/interface_with_different_type-expected.txt @@ -0,0 +1,1273 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "declare": true, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": null, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Abstract", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 24 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 19 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 23, + "column": 31 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "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": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + { + "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": 22, + "column": 25 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 7 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Abstract", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "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": 27, + "column": 2 + }, + "end": { + "line": 27, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 7 + }, + "end": { + "line": 29, + "column": 14 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 38 + } + } + }, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 50 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 19 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 30, + "column": 31 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 30 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + "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": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + { + "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": 31, + "column": 2 + }, + "end": { + "line": 31, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 49 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 1 + }, + "end": { + "line": 31, + "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": 33, + "column": 10 + }, + "end": { + "line": 33, + "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": 33, + "column": 10 + }, + "end": { + "line": 33, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "d", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 34, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 34, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 34, + "column": 13 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 35, + "column": 2 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 35, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 35, + "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": 36, + "column": 1 + } + } +} +TypeError: Cannot inherit from interface Interface because field instance_field is inherited with a different declaration type [interface_with_different_type.ets:16:1] diff --git a/ets2panda/test/parser/ets/interface_with_different_type.ets b/ets2panda/test/parser/ets/interface_with_different_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..e3aef72cb34df429a3e7f6d47c10e955e1cdcf9a --- /dev/null +++ b/ets2panda/test/parser/ets/interface_with_different_type.ets @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 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 Interface { + instance_field(): void { + return + } +} + +abstract class Abstract { + instance_field: number = -1 +} + +class Base extends Abstract { +} + +class Derived extends Base implements Interface { + instance_field: number = -1 +} + +function main() { + let d = new Derived +} diff --git a/ets2panda/test/parser/ets/launch_function_returning_void-expected.txt b/ets2panda/test/parser/ets/launch_function_returning_void-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..525b9eebbd89f3e571a3b8691988cd680cb4029d --- /dev/null +++ b/ets2panda/test/parser/ets/launch_function_returning_void-expected.txt @@ -0,0 +1,498 @@ +{ + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + { + "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": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "r", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": { + "type": "ETSLaunchExpression", + "expr": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 35 + }, + "end": { + "line": 19, + "column": 38 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 35 + }, + "end": { + "line": 19, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 28 + }, + "end": { + "line": 19, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 40 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "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": 20, + "column": 2 + } + } +} diff --git a/ets2panda/test/parser/ets/launch_function_returning_void.ets b/ets2panda/test/parser/ets/launch_function_returning_void.ets new file mode 100644 index 0000000000000000000000000000000000000000..66f2a2665afbd7a7e8db1568170823cf3ccd9ffd --- /dev/null +++ b/ets2panda/test/parser/ets/launch_function_returning_void.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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(){} + +function main(){ + let r: Promise = launch foo() +} \ No newline at end of file diff --git a/ets2panda/test/parser/ets/missing_implementation_1-expected.txt b/ets2panda/test/parser/ets/missing_implementation_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9d2edc59a66b7ae11c86776009fc0f87affd65b3 --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_1-expected.txt @@ -0,0 +1,1113 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 38 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "accessibility": "private", + "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": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "get", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 33 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, + "property": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 17 + }, + "end": { + "line": 25, + "column": 32 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 32 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 26, + "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": 28, + "column": 2 + }, + "end": { + "line": 28, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 28, + "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": 29, + "column": 1 + } + } +} +TypeError: Derived is not abstract and does not implement setter for instance_field property in Interface [missing_implementation_1.ets:20:36] diff --git a/ets2panda/test/parser/ets/missing_implementation_1.ets b/ets2panda/test/parser/ets/missing_implementation_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..dcf28f2c0c5073b211c61eb0ebe47bd1c720ea69 --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_1.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 Interface { + instance_field: number +} + +class Derived implements Interface { + private instance_field_: number = -1; + + + get instance_field(): number { + return this.instance_field_; + } + +} diff --git a/ets2panda/test/parser/ets/missing_implementation_2-expected.txt b/ets2panda/test/parser/ets/missing_implementation_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a7fe24d20931eb12190c00479d1a373573395a33 --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_2-expected.txt @@ -0,0 +1,1185 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 38 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "accessibility": "private", + "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": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "set", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 45 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 45 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 9 + } + } + }, + "property": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 25 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 25 + } + } + }, + "right": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 28 + }, + "end": { + "line": 25, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 43 + } + } + }, + { + "type": "ReturnStatement", + "argument": null, + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 46 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 27, + "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": 29, + "column": 2 + }, + "end": { + "line": 29, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "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 + } + } + } + ], + "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": 30, + "column": 1 + } + } +} +TypeError: Derived is not abstract and does not implement getter for instance_field property in Interface [missing_implementation_2.ets:20:36] diff --git a/ets2panda/test/parser/ets/missing_implementation_2.ets b/ets2panda/test/parser/ets/missing_implementation_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..a76dbb3607c6bfab8557571f2715938a723f5fbb --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_2.ets @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 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 Interface { + instance_field: number +} + +class Derived implements Interface { + private instance_field_: number = -1; + + + set instance_field(instance_field: number) { + this.instance_field_ = instance_field; + return; + } + +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_0/foo2__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_0/foo2__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..0066ab9929f7c70f6c8425b35194c65e8c68fc66 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_0/foo2__module-expected.txt @@ -0,0 +1,424 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./main__foo1_as__module", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "imported": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + { + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "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": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 26 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 28 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 25 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/recursive_import/case_0/foo2__module.ets b/ets2panda/test/parser/ets/recursive_import/case_0/foo2__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..0ab31c2b366c9dde2383fe782eca202f9c559835 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_0/foo2__module.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 {A} from "./main__foo1_as__module" + +export function foo(a: A): int { + console.log("in foo"); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_0/main__foo1_as__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_0/main__foo1_as__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..72b4c78e71162d6134fe1f6a4758c0ca682dbfee --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_0/main__foo1_as__module-expected.txt @@ -0,0 +1,583 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo2__module", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + { + "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": 18, + "column": 16 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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": "main", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "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": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "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": 17 + }, + "end": { + "line": 24, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 24, + "column": 13 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 21 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 10 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 26, + "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": 27, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_0/main__foo1_as__module.ets b/ets2panda/test/parser/ets/recursive_import/case_0/main__foo1_as__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..0ac6ceee06d0719b2e01dae6dc288eebb8bd6e22 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_0/main__foo1_as__module.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 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 {foo} from "./foo2__module" + +export class A { + a: int; +} + + +function main() : void { + let a = new A(); + foo(a); +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/foo2__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/foo2__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b505646cc5d1fd0063344f2ca9b33a38654aa324 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/foo2__module-expected.txt @@ -0,0 +1,424 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./main__foo1", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "imported": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + } + ], + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "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": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 26 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 28 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 25 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/recursive_import/case_1_fail_not_called_w_module/foo2__module.ets b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/foo2__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..0885357093d4b3c486b86b8015011baa7aa48838 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/foo2__module.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 {A} from "./main__foo1" + +export function foo(a: A): int { + console.log("in foo"); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/main__foo1-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/main__foo1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a8f80a6368abacce905cbb4173be04b888c18afa --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/main__foo1-expected.txt @@ -0,0 +1 @@ +SyntaxError: Please compile `main__foo1.ets` with `--ets-module` option. It is being imported by another file. [foo2__module.ets:16:17] diff --git a/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/main__foo1.ets b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/main__foo1.ets new file mode 100644 index 0000000000000000000000000000000000000000..0ac6ceee06d0719b2e01dae6dc288eebb8bd6e22 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_1_fail_not_called_w_module/main__foo1.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 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 {foo} from "./foo2__module" + +export class A { + a: int; +} + + +function main() : void { + let a = new A(); + foo(a); +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo2__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo2__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..2302fb5558b12fa3c6b106458ea046bf5535a083 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo2__module-expected.txt @@ -0,0 +1,354 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./main__foo1_as__module", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "imported": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + { + "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": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_2", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/recursive_import/case_2_3-sources-in-circle/foo2__module.ets b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo2__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..cb9236d125862eb505374fb2b9f69cd2fd6a382f --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo2__module.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 {A} from "./main__foo1_as__module" + +export function foo_in_2(): int { + console.log("in foo_in_2"); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0338509f06711db3977b09dfc7ecaf52a257f85 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module-expected.txt @@ -0,0 +1,397 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo2__module", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "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": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_3", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "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": 18, + "column": 33 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module.ets b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..e3f5710b25aede52f3e5a93305fc720cc83e4653 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 {foo_in_2} from "./foo2__module" + +export function foo_in_3(): int { + console.log("in foo_in_3"); + foo_in_2(); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..5ad82849108db47f33c5019b29a393d7f9c49177 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module-expected.txt @@ -0,0 +1,567 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo3__module", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + { + "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": 18, + "column": 16 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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": "main", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "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": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "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": 17 + }, + "end": { + "line": 24, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 24, + "column": 13 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 21 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 26, + "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": 27, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module.ets b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..45f20acd8f12d6cb77a2d6b94e3a9adc37a5cc6c --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 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 {foo_in_3} from "./foo3__module" + +export class A { + a: int; +} + + +function main() : void { + let a = new A(); + foo_in_3(); +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..bcd5f0a97effd881b5c82e3db2abf02605adc555 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module-expected.txt @@ -0,0 +1,354 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./main__foo1", + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "imported": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + } + ], + "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": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_2", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module.ets b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..99a8034055dd8080fd0ad4c75ed5138d67457a8e --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 {A} from "./main__foo1" + +export function foo_in_2(): int { + console.log("in foo_in_2"); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0338509f06711db3977b09dfc7ecaf52a257f85 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module-expected.txt @@ -0,0 +1,397 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo2__module", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "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": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_3", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "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": 18, + "column": 33 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module.ets b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..e3f5710b25aede52f3e5a93305fc720cc83e4653 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 {foo_in_2} from "./foo2__module" + +export function foo_in_3(): int { + console.log("in foo_in_3"); + foo_in_2(); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/main__foo1-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/main__foo1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a8f80a6368abacce905cbb4173be04b888c18afa --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/main__foo1-expected.txt @@ -0,0 +1 @@ +SyntaxError: Please compile `main__foo1.ets` with `--ets-module` option. It is being imported by another file. [foo2__module.ets:16:17] diff --git a/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/main__foo1.ets b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/main__foo1.ets new file mode 100644 index 0000000000000000000000000000000000000000..45f20acd8f12d6cb77a2d6b94e3a9adc37a5cc6c --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/main__foo1.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 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 {foo_in_3} from "./foo3__module" + +export class A { + a: int; +} + + +function main() : void { + let a = new A(); + foo_in_3(); +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo2-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..c8d3996963eaa46fea6e8f9b62299cae8c15a7ed --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo2-expected.txt @@ -0,0 +1 @@ +SyntaxError: Please compile `foo2.ets` with `--ets-module` option. It is being imported by another file. [foo3__module.ets:16:24] diff --git a/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo2.ets b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo2.ets new file mode 100644 index 0000000000000000000000000000000000000000..cb9236d125862eb505374fb2b9f69cd2fd6a382f --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo2.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 {A} from "./main__foo1_as__module" + +export function foo_in_2(): int { + console.log("in foo_in_2"); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..2400c82d6fa5a02335d8ef0368b09b30e69f1f66 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module-expected.txt @@ -0,0 +1,397 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo2", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + { + "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": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_3", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "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": 18, + "column": 33 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module.ets b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..cb13fab47c8120f47aa306383063a1876be761f7 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 {foo_in_2} from "./foo2" + +export function foo_in_3(): int { + console.log("in foo_in_3"); + foo_in_2(); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..5ad82849108db47f33c5019b29a393d7f9c49177 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module-expected.txt @@ -0,0 +1,567 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo3__module", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + { + "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": 18, + "column": 16 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "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": "main", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "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": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "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": 17 + }, + "end": { + "line": 24, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 24, + "column": 13 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 21 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 26, + "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": 27, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module.ets b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..45f20acd8f12d6cb77a2d6b94e3a9adc37a5cc6c --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 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 {foo_in_3} from "./foo3__module" + +export class A { + a: int; +} + + +function main() : void { + let a = new A(); + foo_in_3(); +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo2__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo2__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..1fcd9da6ef94a6943ce864ff97d00b5cd757684c --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo2__module-expected.txt @@ -0,0 +1,534 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo3__module", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "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": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "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": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 20, + "column": 29 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_2", + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 32 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 23, + "column": 12 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 24, + "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/recursive_import/case_5_main-is-not-part-of-circle/foo2__module.ets b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo2__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..d9d2023fdf83c9011b1bbaaae93c9aba4e5013ed --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo2__module.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 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 {foo_in_3} from "./foo3__module" + +export class A {} + +export function foo_in_2(): int { + console.log("in foo_in_2"); + foo_in_3(); + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo3__module-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo3__module-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..97d461121eabb07ff9427897220583b61e88e847 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo3__module-expected.txt @@ -0,0 +1,453 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo2__module", + "loc": { + "start": { + "line": 15, + "column": 17 + }, + "end": { + "line": 15, + "column": 33 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 9 + }, + "end": { + "line": 15, + "column": 10 + } + } + }, + "imported": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 9 + }, + "end": { + "line": 15, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 9 + }, + "end": { + "line": 15, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 15, + "column": 1 + }, + "end": { + "line": 15, + "column": 33 + } + } + }, + { + "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": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_in_3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 29 + }, + "end": { + "line": 17, + "column": 32 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "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 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 21 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "in foo_in_3", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 30 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "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/recursive_import/case_5_main-is-not-part-of-circle/foo3__module.ets b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo3__module.ets new file mode 100644 index 0000000000000000000000000000000000000000..615dd74a9bf7997e62d5c1632b9480fde44761e4 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo3__module.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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 {A} from "./foo2__module" + +export function foo_in_3(): int { + let a = new A(); + console.log("in foo_in_3") + return 0; +} diff --git a/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/main__foo1-expected.txt b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/main__foo1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..8910b1506dbd8a48d1b49f766be54573b428a761 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/main__foo1-expected.txt @@ -0,0 +1,376 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./foo2__module", + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "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": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo_in_2", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "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/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/main__foo1.ets b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/main__foo1.ets new file mode 100644 index 0000000000000000000000000000000000000000..9fef5aeb84ec75c3338e8e629cff26fa30f24675 --- /dev/null +++ b/ets2panda/test/parser/ets/recursive_import/case_5_main-is-not-part-of-circle/main__foo1.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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 {foo_in_2} from "./foo2__module" + +function main() : void { + foo_in_2(); +} diff --git a/ets2panda/test/runtime/ets/ObjectIterable_3.ets b/ets2panda/test/runtime/ets/ObjectIterable_3.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6145226f89c3aef4b4a7fe7f1bbe24465dc9196 --- /dev/null +++ b/ets2panda/test/runtime/ets/ObjectIterable_3.ets @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 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 MyIterator1 extends Iterator {} + +interface MyIterator2 extends MyIterator1 {} + +interface MyIter { + $_iterator(): Iterator; +} + +interface MyIter1 { + $_iterator(): MyIterator1; +} + +interface MyIter2 { + $_iterator(): MyIterator2; +} + +function main() { +} diff --git a/ets2panda/test/runtime/ets/TypeInferInterface1.ets b/ets2panda/test/runtime/ets/TypeInferInterface1.ets new file mode 100644 index 0000000000000000000000000000000000000000..8f041df9c31291e709f89c2cba2843040204f118 --- /dev/null +++ b/ets2panda/test/runtime/ets/TypeInferInterface1.ets @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2024 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(a:Set){ + return Array.from(a); +} + +function main() { + let a = new Set(); + a.add(1); + let arr = foo(a); + assert(arr[0] == 1); +} diff --git a/ets2panda/test/runtime/ets/TypeInferObject1.ets b/ets2panda/test/runtime/ets/TypeInferObject1.ets new file mode 100644 index 0000000000000000000000000000000000000000..7cbd16c634a0a95a5c8fa31344bf855bf07d02f3 --- /dev/null +++ b/ets2panda/test/runtime/ets/TypeInferObject1.ets @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 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 value1: U; + public value2: T; + + constructor(a: U, b: T) { + this.value1 = a; + this.value2 = b; + } +} + +class B { + public value1: U; + public value2: T; + public value3: K; + + constructor(a: U, b: T, c: K) { + this.value1 = a; + this.value2 = b; + this.value3 = c; + } +} + +function func1(arg: A, arg1: K){ + assert(typeof arg1 == "int"); + return new A(arg.value2, arg.value1); +} + +function func2(arg: B){ + return new B(arg.value2, arg.value1, arg.value3); +} + +function func3(arg: A){ + return new A(arg.value2, arg.value1); +} + +function main(): void { + let a = new A(10, "Test"); + let funcValue1 = func1(a, 12); + assert(typeof funcValue1.value1 == "string"); + assert(typeof funcValue1.value2 == "int"); + + let b = new B(10, "Test", 40); + let funcValue2 = func2(b); + assert(typeof funcValue2.value1 == "string"); + assert(typeof funcValue2.value2 == "int"); + assert(typeof funcValue2.value3 == "int"); + + let c = new A(10, "Test"); + let funcValue3 = func3(c); + assert(typeof funcValue3.value1 == "string"); + assert(typeof funcValue3.value2 == "int"); +} diff --git a/ets2panda/test/runtime/ets/TypeInferObject2.ets b/ets2panda/test/runtime/ets/TypeInferObject2.ets new file mode 100644 index 0000000000000000000000000000000000000000..311b7cdea0d87de3dc8bdf605f7ccd576c71d926 --- /dev/null +++ b/ets2panda/test/runtime/ets/TypeInferObject2.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 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 a: T; + public b: U; + public c: K; + + constructor(a: T, b: U, c: K) { + this.a = a; + this.b = b; + this.c = c; + } +} + +function foo>(a: A, b: U, c: K, d: U, e: L) { + assert(typeof a.a == "int"); + assert(typeof a.b == "string"); + assert(typeof a.c == "int"); + assert(typeof b == "number"); + assert(typeof c == "string"); + assert(typeof d == "number"); + assert(typeof e == "object"); + return new A(a.a, b, a.b); +} + +function bar(a: A, b : U) { + assert(typeof a.a == "int"); + assert(typeof a.b == "string"); + assert(typeof a.c == "int"); + assert(typeof b == "number"); + return new A(a.a, b, a.b); +} + +function main() { + let a = new A(10, "Test", 40); + let b: number = 20; + let c = foo(a, b, "Test", 20, a); + assert(typeof c.a == "int"); + assert(typeof c.b == "number"); + assert(typeof c.c == "string"); + let d = bar(a, b); + assert(typeof d.a == "int"); + assert(typeof d.b == "number"); + assert(typeof d.c == "string"); +} diff --git a/ets2panda/test/runtime/ets/UnionWithLambdaInParameter.ets b/ets2panda/test/runtime/ets/UnionWithLambdaInParameter.ets new file mode 100644 index 0000000000000000000000000000000000000000..8badcaff1d94b60cb90d19b7e116de36e2a6cfaa --- /dev/null +++ b/ets2panda/test/runtime/ets/UnionWithLambdaInParameter.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 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(bar: (() => int) | undefined) { + assert (bar?.() == 2024 || bar == undefined); +} + +function main(): void { + foo(() => { return 2024; }); + foo(undefined); +} diff --git a/ets2panda/test/runtime/ets/async-func-overload-and-type-infer.ets b/ets2panda/test/runtime/ets/async-func-overload-and-type-infer.ets new file mode 100644 index 0000000000000000000000000000000000000000..d3a5a9fd24285da57b5d04f34b2c38f85905e838 --- /dev/null +++ b/ets2panda/test/runtime/ets/async-func-overload-and-type-infer.ets @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 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. + */ + +async function multipleReturns(flag: boolean, foo: int) { + if (flag) { + return foo; + } + + return "string1" +} + + +async function multipleReturns(flag: boolean): Promise { + if (flag) { + return 2; + } + + return "string2" +} + +function main() { + let a: int|string = await multipleReturns(true, 42); + let b: int|string = await multipleReturns(false); + + assert a == 42; + assert b == "string2"; +} + diff --git a/ets2panda/test/runtime/ets/multisource_inheritance-2.ets b/ets2panda/test/runtime/ets/multisource_inheritance-2.ets new file mode 100644 index 0000000000000000000000000000000000000000..38a8825924e1cfdc76823bc1811dea0c2f4d67a5 --- /dev/null +++ b/ets2panda/test/runtime/ets/multisource_inheritance-2.ets @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 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 Interface { + instance_field: number +} + +abstract class Abstract { + instance_field: number = -1 + +} + + +class Derived extends Abstract implements Interface { + +} + +function main() { + let d = new Derived + assert d.instance_field == -1 + + d.instance_field = 3 + assert d.instance_field == 3 +} + diff --git a/ets2panda/test/runtime/ets/multisource_inheritance.ets b/ets2panda/test/runtime/ets/multisource_inheritance.ets new file mode 100644 index 0000000000000000000000000000000000000000..d6c03ef44b729b4dd2a3091d25cbc19f8e596179 --- /dev/null +++ b/ets2panda/test/runtime/ets/multisource_inheritance.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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 Interface { + instance_field: number +} + +abstract class Abstract { + instance_field: number = -1 +} + +class Base extends Abstract { +} + +class Derived extends Base implements Interface { + instance_field: number = -1 + + public method(): number { + this.instance_field -= 1 + super.instance_field += 1 + return super.instance_field + } +} + +function main() { + let d = new Derived + assert d.method() == 0 + assert d.instance_field == -2 +} diff --git a/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt b/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt index 10e3cebe4161bd9c5ca88c9a4a9f38f8e73ec21b..0aaf825dac220381d8b57ff8af8b5947142a0563 100644 --- a/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt +++ b/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt @@ -45,9 +45,6 @@ struct-init2.ets struct_implements.ets top_level_03.ets -# Union with undefined -OptionalCall.ets - # Functional type with rest parameter lambdaExpressionWithRestParameter.ets diff --git a/ets2panda/test/test-lists/parser/parser-ets-ignored.txt b/ets2panda/test/test-lists/parser/parser-ets-ignored.txt index 0e9d07cfd17956975b409b43f7bbdb3ef2b11221..e15a43350cd2f07453ec6e987e9a026dcd3a9ed4 100644 --- a/ets2panda/test/test-lists/parser/parser-ets-ignored.txt +++ b/ets2panda/test/test-lists/parser/parser-ets-ignored.txt @@ -30,3 +30,19 @@ parser/ets/async_with_lambda.ets # Skipped until #16329 is done. It is not properly checked and does not cause a CTE when using namespace import with alias. parser/ets/re_export/import_5.ets + +# Some tests within recursive import folder should run with `--ets-module` flag. These files has "__module" in their +# name. Tests below are ignored because they should result in the ats provided in their -expected.txt. Currently they +# result in an error message due to not compiling with `--ets-module` flag. +parser/ets/recursive_import/case_0/foo2__module.ets +parser/ets/recursive_import/case_0/main__foo1_as__module.ets +parser/ets/recursive_import/case_1_fail_not_called_w_module/foo2__module.ets +parser/ets/recursive_import/case_2_3-sources-in-circle/foo2__module.ets +parser/ets/recursive_import/case_2_3-sources-in-circle/foo3__module.ets +parser/ets/recursive_import/case_2_3-sources-in-circle/main__foo1_as__module.ets +parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo2__module.ets +parser/ets/recursive_import/case_3_3-sources-in-circle-fail-no-ets-module-compile/foo3__module.ets +parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/foo3__module.ets +parser/ets/recursive_import/case_4_3-sources-in-circle_no-ets-module-compile_2/main__foo1_as__module.ets +parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo2__module.ets +parser/ets/recursive_import/case_5_main-is-not-part-of-circle/foo3__module.ets diff --git a/ets2panda/test/unit/public/CMakeLists.txt b/ets2panda/test/unit/public/CMakeLists.txt index 1d43b311349b9412a25ea956e5ab9ab3be0cc347..e0c4dec0b0a43a5cd1ce1c25490ce6ffa7926ea6 100644 --- a/ets2panda/test/unit/public/CMakeLists.txt +++ b/ets2panda/test/unit/public/CMakeLists.txt @@ -43,6 +43,10 @@ ets2panda_add_gtest(ast_verifier_protected_access_negative_test_4_6 CPP_SOURCES ast_verifier_protected_access_negative_test_4_6.cpp ) +ets2panda_add_gtest(ast_verifier_reference_typeannotation_test + CPP_SOURCES ast_verifier_reference_typeannotation_test.cpp +) + panda_add_library(e2p_test_plugin SHARED e2p_test_plugin.c) panda_target_include_directories(e2p_test_plugin PRIVATE "${ES2PANDA_PATH}") panda_target_link_libraries(e2p_test_plugin es2panda-public) diff --git a/ets2panda/test/unit/public/ast_verifier_reference_typeannotation_test.cpp b/ets2panda/test/unit/public/ast_verifier_reference_typeannotation_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de0147f4cf9471701c0f93e82f583b3f181ecf2 --- /dev/null +++ b/ets2panda/test/unit/public/ast_verifier_reference_typeannotation_test.cpp @@ -0,0 +1,110 @@ +/** + * Copyright (c) 2024 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 "ast_verifier_test.h" +#include "checker/ETSchecker.h" +#include "ir/expressions/identifier.h" +#include "parser/ETSparser.h" +#include "ir/astNode.h" + +#include + +using ark::es2panda::compiler::ast_verifier::ASTVerifier; +using ark::es2panda::compiler::ast_verifier::InvariantNameSet; +using ark::es2panda::ir::AstNode; + +TEST_F(ASTVerifierTest, RefAnnotationNullNegative) +{ + ASTVerifier verifier {Allocator()}; + + char const *text = R"( + let refNull = 10; + let trueRef = refNull; + )"; + + 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))); + + auto *type = new ark::es2panda::ir::ETSPrimitiveType(ark::es2panda::ir::PrimitiveType::CHAR); + + ast->IterateRecursively([type](AstNode *astT) { + if (astT->IsIdentifier()) { + if (astT->AsIdentifier()->Name() == "trueRef") { + astT->AsIdentifier()->SetTsTypeAnnotation(type); + } + } + }); + + InvariantNameSet checks; + checks.insert("ReferenceTypeAnnotationIsNullForAll"); + + const auto &messages = verifier.Verify(ast, checks); + ASSERT_EQ(messages.size(), 1); + const auto check = "ReferenceTypeAnnotationIsNullForAll"; + ASSERT_EQ(messages[0].Invariant(), check); + + delete type; + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, RefAnnotationNull1) +{ + ASTVerifier verifier {Allocator()}; + + char const *text = R"( + let refNull = 10; + let trueRef = refNull; + )"; + + 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))); + + InvariantNameSet checks; + checks.insert("ReferenceTypeAnnotationIsNullForAll"); + const auto &messages = verifier.Verify(ast, checks); + + ASSERT_EQ(messages.size(), 0); + + impl_->DestroyContext(ctx); +} + +TEST_F(ASTVerifierTest, RefAnnotationNull2) +{ + ASTVerifier verifier {Allocator()}; + + char const *text = R"( + let refNull = 10; + )"; + + 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))); + + InvariantNameSet checks; + checks.insert("ReferenceTypeAnnotationIsNullForAll"); + const auto &messages = verifier.Verify(ast, checks); + + ASSERT_EQ(messages.size(), 0); + + impl_->DestroyContext(ctx); +} diff --git a/ets2panda/util/options.cpp b/ets2panda/util/options.cpp index a86b88edb8e35912c957ec5b3df9bfc31505b9a1..3f1fc41b8f7c1d5bb67a3b46b44935eb4b8f1548 100644 --- a/ets2panda/util/options.cpp +++ b/ets2panda/util/options.cpp @@ -173,7 +173,7 @@ bool Options::Parse(int argc, const char **argv) ark::PandArg opDumpAssembly("dump-assembly", false, "Dump pandasm"); ark::PandArg opDebugInfo("debug-info", false, "Compile with debug info"); ark::PandArg opDumpDebugInfo("dump-debug-info", false, "Dump debug info"); - ark::PandArg opOptLevel("opt-level", 0, "Compiler optimization level (options: 0 | 1 | 2)"); + ark::PandArg opOptLevel("opt-level", 0, "Compiler optimization level (options: 0 | 1 | 2)", 0, MAX_OPT_LEVEL); ark::PandArg opEtsModule("ets-module", false, "Compile the input as ets-module"); // ETS-warnings @@ -212,18 +212,21 @@ bool Options::Parse(int argc, const char **argv) "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll," "IdentifierHasVariableForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll," "ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll," - "ImportExportAccessValid,NodeHasSourceRangeForAll,EveryChildInParentRangeForAll"); + "ImportExportAccessValid,NodeHasSourceRangeForAll,EveryChildInParentRangeForAll," + "ReferenceTypeAnnotationIsNullForAll"); ark::PandArg verifierErrors( "verifier-errors", "ForLoopCorrectlyInitializedForAll,SequenceExpressionHasLastTypeForAll,NodeHasTypeForAll,NodeHasParentForAll," "EveryChildHasValidParentForAll,ModifierAccessValidForAll,ArithmeticOperationValidForAll," - "VariableHasScopeForAll,IdentifierHasVariableForAll,VariableHasEnclosingScopeForAll", + "VariableHasScopeForAll,IdentifierHasVariableForAll,VariableHasEnclosingScopeForAll," + "ReferenceTypeAnnotationIsNullForAll", "Print errors and stop compilation if AST tree is incorrect. " "Possible values: " "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll," "IdentifierHasVariableForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll," "ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll," - "ImportExportAccessValid,NodeHasSourceRangeForAll,EveryChildInParentRangeForAll"); + "ImportExportAccessValid,NodeHasSourceRangeForAll,EveryChildInParentRangeForAll," + "ReferenceTypeAnnotationIsNullForAll"); ark::PandArg verifierAllChecks( "verifier-all-checks", false, "Run verifier checks on every phase, monotonically expanding them on every phase"); diff --git a/ets2panda/util/options.h b/ets2panda/util/options.h index 38d367298046baa19e9f3d2ab0e8073dcdfe3326..d697d9422903ed67afa22cc273b1403c22d72566 100644 --- a/ets2panda/util/options.h +++ b/ets2panda/util/options.h @@ -40,6 +40,8 @@ enum class OptionFlags : uint32_t { SIZE_STAT = 1U << 2U, }; +constexpr int MAX_OPT_LEVEL = 2; + inline std::underlying_type_t operator&(OptionFlags a, OptionFlags b) { using Utype = std::underlying_type_t; diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index d0d1beb7abe8fd48a44371fe9d6ce3a0b08fa22d..e659bb3f11d353bd9759987a8f65d19fe2a1f51e 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -680,8 +680,14 @@ varbinder::Variable *ETSBinder::FindStaticBinding(const ArenaVector ETSBinder::GetExternalProgram(const util::StringView &sourceName, const ir::StringLiteral *importPath) { - const auto &extRecords = globalRecordTable_.Program()->ExternalSources(); + // NOTE: quick fix to make sure not to look for the global program among the external sources + if (sourceName.Compare(globalRecordTable_.Program()->AbsoluteName()) == 0) { + ArenaVector mainModule(Allocator()->Adapter()); + mainModule.emplace_back(globalRecordTable_.Program()); + return mainModule; + } + const auto &extRecords = globalRecordTable_.Program()->ExternalSources(); auto [name, _] = GetModuleInfo(sourceName); auto res = extRecords.find(name); if (res == extRecords.end()) { diff --git a/ets2panda/varbinder/scope.cpp b/ets2panda/varbinder/scope.cpp index 3d5eb4fc641a9f55b5c845193cdc0f5380bf4832..0b4a29ad8068583e5525196e21e12d6ee60f49ce 100644 --- a/ets2panda/varbinder/scope.cpp +++ b/ets2panda/varbinder/scope.cpp @@ -157,9 +157,11 @@ Scope::InsertResult Scope::TryInsertBinding(const util::StringView &name, Variab return bindings_.try_emplace(name, var); } -void Scope::ReplaceBindings(VariableMap bindings) +void Scope::MergeBindings(VariableMap const &bindings) { - bindings_ = std::move(bindings); + for (auto &[k, v] : bindings) { + bindings_.try_emplace(k, v); + } } Scope::VariableMap::size_type Scope::EraseBinding(const util::StringView &name) @@ -535,7 +537,7 @@ Scope::InsertResult GlobalScope::TryInsertBinding(const util::StringView &name, return insRes; } -void GlobalScope::ReplaceBindings([[maybe_unused]] const VariableMap bindings) +void GlobalScope::MergeBindings([[maybe_unused]] const VariableMap &bindings) { UNREACHABLE(); } @@ -890,7 +892,7 @@ void LoopDeclarationScope::ConvertToVariableScope(ArenaAllocator *allocator) evalBindings_ = parentVarScope->EvalBindings(); initScope_ = allocator->New(allocator, Parent()); initScope_->BindNode(Node()); - initScope_->ReplaceBindings(bindings); + initScope_->MergeBindings(bindings); } } diff --git a/ets2panda/varbinder/scope.h b/ets2panda/varbinder/scope.h index d9762c1ce6a7c8220e2003012230356c5ae3cfa7..db79b1cae5c4b39a4613b6ade07818746575ea78 100644 --- a/ets2panda/varbinder/scope.h +++ b/ets2panda/varbinder/scope.h @@ -243,7 +243,7 @@ public: virtual InsertResult InsertBinding(const util::StringView &name, Variable *var); virtual InsertResult TryInsertBinding(const util::StringView &name, Variable *var); - virtual void ReplaceBindings(VariableMap bindings); + virtual void MergeBindings(VariableMap const &bindings); virtual VariableMap::size_type EraseBinding(const util::StringView &name); const VariableMap &Bindings() const @@ -498,13 +498,12 @@ public: void BindParamScope(T *paramScope) { AssignParamScope(paramScope); - this->ReplaceBindings(paramScope->Bindings()); + this->MergeBindings(paramScope->Bindings()); } void AssignParamScope(T *paramScope) { ASSERT(this->Parent() == paramScope); - ASSERT(this->Bindings().empty()); paramScope_ = paramScope; } @@ -833,7 +832,7 @@ public: InsertResult InsertBinding(const util::StringView &name, Variable *var) override; InsertResult TryInsertBinding(const util::StringView &name, Variable *var) override; - void ReplaceBindings(VariableMap bindings) override; + void MergeBindings(VariableMap const &bindings) override; VariableMap::size_type EraseBinding(const util::StringView &name) override; InsertResult InsertForeignBinding(const util::StringView &name, Variable *var);