From 207d172c6d48ae56754b0b893758c05f2f546b20 Mon Sep 17 00:00:00 2001 From: Andrei Voronin Date: Mon, 8 Jul 2024 10:32:41 +0300 Subject: [PATCH] Removing IsReference flag completely https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IA72MU Removing flag IsReference in order to make it easier to add new code and refactore existing. Removed flag IsRefence in Identifier. Removed function SetRefence in Identifier and all it occurences. Added AstNode check if Identifier is reference in ASTVerifier.cpp This is draft commit. Signed-off-by: Andrei Voronin --- .../referenceTypeAnnotationIsNull.cpp | 56 ++++- ets2panda/checker/ETSAnalyzerHelpers.cpp | 1 - ets2panda/checker/ets/dynamic.cpp | 4 - ets2panda/checker/ets/function.cpp | 1 - .../lowering/ets/defaultParameterLowering.cpp | 6 +- .../compiler/lowering/ets/enumLowering.cpp | 17 -- .../lowering/ets/enumPostCheckLowering.cpp | 1 - .../compiler/lowering/ets/lambdaLowering.cpp | 2 - .../lowering/ets/localClassLowering.cpp | 1 - .../lowering/ets/objectLiteralLowering.cpp | 2 + .../lowering/ets/optionalLowering.cpp | 1 - .../compiler/lowering/ets/promiseVoid.cpp | 2 - .../lowering/ets/stringComparison.cpp | 1 - .../compiler/lowering/ets/structLowering.cpp | 4 +- .../ets/topLevelStmts/globalClassHandler.cpp | 1 - .../topLevelStmts/globalDeclTransformer.cpp | 1 - ets2panda/ir/expressions/identifier.cpp | 211 +++++++++++++++++- ets2panda/ir/expressions/identifier.h | 34 +-- ets2panda/parser/ASparser.cpp | 2 - ets2panda/parser/ETSparser.cpp | 7 - ets2panda/parser/ETSparserClasses.cpp | 4 - ets2panda/parser/TSparser.cpp | 2 - ets2panda/parser/TypedParser.cpp | 3 - ets2panda/parser/expressionParser.cpp | 6 - ets2panda/parser/expressionTSParser.cpp | 2 - ets2panda/parser/parserImpl.cpp | 5 +- ets2panda/parser/statementParser.cpp | 5 - ets2panda/varbinder/ETSBinder.cpp | 28 +++ ets2panda/varbinder/varbinder.cpp | 5 +- 29 files changed, 314 insertions(+), 101 deletions(-) diff --git a/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp b/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp index 5770b83e92..0146e7cb9f 100644 --- a/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp +++ b/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp @@ -22,13 +22,55 @@ namespace ark::es2panda::compiler::ast_verifier { CheckResult ReferenceTypeAnnotationIsNull::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; -} + if (!ast->IsIdentifier()) { + return result; + ctx.AddCheckMessage("TYPE_ANNOTATION_NOT_NULLPTR", *ast, ast->Start()); + } +/* + auto id = ast->AsIdentifier(); + if (id->IsReference() && id->TypeAnnotation() != nullptr) { + if (id->Parent()->Parent() != nullptr) { + if (id->Parent()->Parent()->IsReturnStatement()) { + return result; + } + } + + //(AEVoronin) NOTE: currently some OPAQUETYPE are references AND have typeannotation + // Example: function apply(fp: (n: int) => void) {} + // class C { + // private f(n: int) {} + // test() { + // apply(this.f)}} + if (id->TypeAnnotation()->Type() == ir::AstNodeType::OPAQUE_TYPE_NODE) { + return result; + } + + //(AEVoronin) NOTE: currently a lot of references have typeannotation with type = ETS_TYPE_REFERENCE + // Example: function apply(fp: (n: int) => void) {} + // class A { + // met(s : string, q ?: string){ + // console.log("met " + s)}} + // function main()/{ + // new A().met('S')} + if (id->TypeAnnotation()->Type() == ir::AstNodeType::ETS_TYPE_REFERENCE) { + return result; + } + //(AEVoronin) NOTE: currently some references have typeannotation with type = ETSPrimitiveType + // Example: + // public static test(c: C, x: int) { + // test(c, x, 0);} + if (id->TypeAnnotation()->Type() == ir::AstNodeType::ETS_PRIMITIVE_TYPE) { + return result; + } + + // std::cout << "Json: " << id->Parent()->DumpJSON() << std::endl; + // std::cout << "AST Json: " << id->Parent()->Parent()->DumpEtsSrc() << std::endl; + + ctx.AddCheckMessage("TYPE_ANNOTATION_NOT_NULLPTR", *ast, ast->Start()); + result = {CheckDecision::INCORRECT, CheckAction::CONTINUE}; + + }*/ +} } // namespace ark::es2panda::compiler::ast_verifier diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index 58f6dcec0e..6d690e6907 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.cpp +++ b/ets2panda/checker/ETSAnalyzerHelpers.cpp @@ -157,7 +157,6 @@ void ComposeAsyncImplFuncReturnType(ETSChecker *checker, ir::ScriptFunction *scr 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)); diff --git a/ets2panda/checker/ets/dynamic.cpp b/ets2panda/checker/ets/dynamic.cpp index b17a6e5aaa..77304d5735 100644 --- a/ets2panda/checker/ets/dynamic.cpp +++ b/ets2panda/checker/ets/dynamic.cpp @@ -324,7 +324,6 @@ ir::ClassStaticBlock *ETSChecker::CreateDynamicCallClassInitializer(Language lan auto *classId = AllocNode(builtin_class_name, Allocator()); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *methodId = AllocNode(builtin_method_name, Allocator()); - methodId->SetReference(); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *callee = AllocNode(classId, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); @@ -422,7 +421,6 @@ void ETSChecker::ClassInitializerFromImport(ir::ETSImportDeclaration *import, Ar auto *classId = AllocNode(builtin_class_name, Allocator()); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *methodId = AllocNode(builtin_method_name, Allocator()); - methodId->SetReference(); auto *callee = // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) AllocNode(classId, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); @@ -441,7 +439,6 @@ void ETSChecker::ClassInitializerFromImport(ir::ETSImportDeclaration *import, Ar auto *moduleClassId = AllocNode(compiler::Signatures::DYNAMIC_MODULE_CLASS, Allocator()); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *fieldId = AllocNode(import->AssemblerName(), Allocator()); - fieldId->SetReference(); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *property = AllocNode(moduleClassId, fieldId, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); @@ -561,7 +558,6 @@ void ETSChecker::EmitDynamicModuleClassInitCall() auto *classId = AllocNode(compiler::Signatures::DYNAMIC_MODULE_CLASS, Allocator()); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *methodId = AllocNode(compiler::Signatures::DYNAMIC_MODULE_CLASS_INIT, Allocator()); - methodId->SetReference(); auto *callee = // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) AllocNode(classId, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 656e28d831..e991645046 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1801,7 +1801,6 @@ ir::MethodDefinition *ETSChecker::CreateAsyncImplMethod(ir::MethodDefinition *as 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)); diff --git a/ets2panda/compiler/lowering/ets/defaultParameterLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParameterLowering.cpp index 32494526e5..a39a1fea06 100644 --- a/ets2panda/compiler/lowering/ets/defaultParameterLowering.cpp +++ b/ets2panda/compiler/lowering/ets/defaultParameterLowering.cpp @@ -119,7 +119,6 @@ ir::TSTypeParameterInstantiation *DefaultParameterLowering::CreateTypeParameterI for (const auto ¶m : method->Function()->TypeParams()->Params()) { auto *identRef = checker->AllocNode(param->AsTSTypeParameter()->Name()->Name(), checker->Allocator()); - identRef->AsIdentifier()->SetReference(); referencePart = checker->AllocNode(identRef, nullptr, nullptr); @@ -141,7 +140,6 @@ ir::BlockStatement *DefaultParameterLowering::CreateFunctionBody(ir::MethodDefin ir::Expression *id = nullptr; ir::Expression *accessor = nullptr; auto *const callee = checker->AllocNode(method->Id()->Name(), checker->Allocator()); - callee->SetReference(); if (method->IsConstructor()) { accessor = checker->AllocNode(); @@ -150,7 +148,6 @@ ir::BlockStatement *DefaultParameterLowering::CreateFunctionBody(ir::MethodDefin if (method->IsStatic()) { id = checker->AllocNode(method->Parent()->AsClassDefinition()->Ident()->Name(), checker->Allocator()); - id->AsIdentifier()->SetReference(); } else { id = checker->AllocNode(); } @@ -226,8 +223,7 @@ void DefaultParameterLowering::CreateOverloadFunction(ir::MethodDefinition *meth auto *ident = funcExpression->Function()->Id()->Clone(checker->Allocator(), nullptr); auto *const overloadMethod = checker->AllocNode( method->Kind(), ident, funcExpression, method->Modifiers(), checker->Allocator(), false); - ident->SetReference(); - + overloadMethod->Function()->AddFlag(ir::ScriptFunctionFlags::OVERLOAD); overloadMethod->SetRange(funcExpression->Range()); diff --git a/ets2panda/compiler/lowering/ets/enumLowering.cpp b/ets2panda/compiler/lowering/ets/enumLowering.cpp index 86639c5c3b..34f1b7799a 100644 --- a/ets2panda/compiler/lowering/ets/enumLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumLowering.cpp @@ -49,7 +49,6 @@ namespace { [[nodiscard]] ir::ETSTypeReference *MakeTypeReference(checker::ETSChecker *const checker, const util::StringView &name) { auto *const ident = checker->AllocNode(name, checker->Allocator()); - ident->SetReference(); auto *const referencePart = checker->AllocNode(ident); return checker->AllocNode(referencePart); } @@ -304,7 +303,6 @@ void EnumLoweringPhase::CreateCtorForEnumClass(ir::ClassDefinition *const enumCl auto *thisExpr = Allocator()->New(); auto *fieldIdentifier = Allocator()->New("ordinal", Allocator()); - fieldIdentifier->SetReference(); auto *leftHandSide = checker_->AllocNode( thisExpr, fieldIdentifier, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); auto *rightHandSide = checker_->AllocNode("ordinal", Allocator()); @@ -486,11 +484,9 @@ ir::Identifier *EnumLoweringPhase::CreateEnumItemsArray(const ir::TSEnumDeclarat [this, enumDecl](const ir::TSEnumMember *const member) { auto *const enumTypeIdent = checker_->AllocNode(enumDecl->Key()->Name(), Allocator()); - enumTypeIdent->SetReference(); auto *const enumMemberIdent = checker_->AllocNode( member->AsTSEnumMember()->Key()->AsIdentifier()->Name(), Allocator()); - enumMemberIdent->SetReference(); auto *const enumMemberExpr = checker_->AllocNode( enumTypeIdent, enumMemberIdent, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); return enumMemberExpr; @@ -509,11 +505,9 @@ ir::Identifier *EnumLoweringPhase::CreateBoxedEnumItemsArray(const ir::TSEnumDec [this, enumDecl, &boxedClassName](const ir::TSEnumMember *const member) { auto *const enumTypeIdent = checker_->AllocNode(enumDecl->Key()->Name(), Allocator()); - enumTypeIdent->SetReference(); auto *const enumMemberIdent = checker_->AllocNode( member->AsTSEnumMember()->Key()->AsIdentifier()->Name(), Allocator()); - enumMemberIdent->SetReference(); auto *const enumMemberExpr = checker_->AllocNode( enumTypeIdent, enumMemberIdent, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); @@ -539,7 +533,6 @@ ir::BinaryExpression *CreateIfTest(EnumLoweringPhase *const elp, ir::Identifier { auto *const checker = elp->Checker(); auto *const lengthIdent = checker->AllocNode("length", checker->Allocator()); - lengthIdent->SetReference(); auto *const valuesArrayLengthExpr = checker->AllocNode( itemsArrayIdentifier, lengthIdent, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); auto *const paramRefIdent = MakeParamRefIdent(checker, parameter); @@ -635,7 +628,6 @@ void EnumLoweringPhase::CreateEnumFromIntMethod(const ir::TSEnumDeclaration *con function->Scope()->BindInternalName(ident->Name()); MakeMethodDef(checker_, enumClass, varbinder_, ident, function); - ident->SetReference(); } void EnumLoweringPhase::CreateEnumToStringMethod(const ir::TSEnumDeclaration *const enumDecl, @@ -663,7 +655,6 @@ void EnumLoweringPhase::CreateEnumToStringMethod(const ir::TSEnumDeclaration *co function->SetIdent(functionIdent); function->Scope()->BindInternalName(functionIdent->Name()); MakeMethodDef(checker_, enumClass, varbinder_, functionIdent, function); - functionIdent->SetReference(); } void EnumLoweringPhase::CreateEnumValueOfMethod(const ir::TSEnumDeclaration *const enumDecl, @@ -691,8 +682,6 @@ void EnumLoweringPhase::CreateEnumValueOfMethod(const ir::TSEnumDeclaration *con function->Scope()->BindInternalName(functionIdent->Name()); MakeMethodDef(checker_, enumClass, varbinder_, functionIdent, function); - - functionIdent->SetReference(); } void EnumLoweringPhase::CreateEnumGetNameMethod(const ir::TSEnumDeclaration *const enumDecl, @@ -722,7 +711,6 @@ void EnumLoweringPhase::CreateEnumGetNameMethod(const ir::TSEnumDeclaration *con function->Scope()->BindInternalName(functionIdent->Name()); MakeMethodDef(checker_, enumClass, varbinder_, functionIdent, function); - functionIdent->SetReference(); } namespace { @@ -759,7 +747,6 @@ ir::BinaryExpression *CreateForLoopTest(EnumLoweringPhase *const elp, ir::Identi { auto *const checker = elp->Checker(); auto *const lengthIdent = checker->AllocNode("length", checker->Allocator()); - lengthIdent->SetReference(); auto *const arrayLengthExpr = checker->AllocNode( namesArrayIdentifier, lengthIdent, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); auto *const forLoopIdentClone = loopIdentifier->Clone(checker->Allocator(), nullptr); @@ -849,7 +836,6 @@ void EnumLoweringPhase::CreateEnumGetValueOfMethod(const ir::TSEnumDeclaration * function->SetIdent(functionIdent); function->Scope()->BindInternalName(functionIdent->Name()); MakeMethodDef(checker_, enumClass, varbinder_, functionIdent, function); - functionIdent->SetReference(); } void EnumLoweringPhase::CreateEnumValuesMethod(const ir::TSEnumDeclaration *const enumDecl, @@ -874,7 +860,6 @@ void EnumLoweringPhase::CreateEnumValuesMethod(const ir::TSEnumDeclaration *cons function->Scope()->BindInternalName(functionIdent->Name()); MakeMethodDef(checker_, enumClass, varbinder_, functionIdent, function); - functionIdent->SetReference(); } void EnumLoweringPhase::CreateUnboxingMethod(ir::TSEnumDeclaration const *const enumDecl, @@ -889,7 +874,6 @@ void EnumLoweringPhase::CreateUnboxingMethod(ir::TSEnumDeclaration const *const auto *thisExpr = Allocator()->New(); auto *fieldIdentifier = Allocator()->New("ordinal", Allocator()); - fieldIdentifier->SetReference(); auto *arrayIndexExpr = checker_->AllocNode( thisExpr, fieldIdentifier, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); @@ -914,7 +898,6 @@ void EnumLoweringPhase::CreateUnboxingMethod(ir::TSEnumDeclaration const *const function->Scope()->BindInternalName(functionIdent->Name()); MakeMethodDef(checker_, enumClass, varbinder_, functionIdent, function); - functionIdent->SetReference(); } ArenaAllocator *EnumLoweringPhase::Allocator() diff --git a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp index a90b5deee1..fcfded93e1 100644 --- a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp @@ -29,7 +29,6 @@ ir::CallExpression *EnumPostCheckLoweringPhase::CreateCall( auto *classId = checker->AllocNode(classDef->Ident()->Name(), checker->Allocator()); auto *methodId = checker->AllocNode( (argument->TsType()->AsETSEnumType()->*getMethod)().memberProxyType->Name(), checker->Allocator()); - methodId->SetReference(); auto *callee = checker->AllocNode(classId, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index 40b43ce759..6792cd457e 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -465,13 +465,11 @@ static ir::BlockStatement *CreateFunctionBody(public_lib::Context *ctx, ir::Arro ir::Expression *id = nullptr; ir::Expression *accessor = nullptr; auto *const callee = checker->AllocNode(defaultMethod->Id()->Name(), checker->Allocator()); - callee->SetReference(); if (defaultMethod->IsStatic() && defaultMethod->Parent()->IsClassDefinition() && (!defaultMethod->Parent()->AsClassDefinition()->IsGlobal())) { id = checker->AllocNode(defaultMethod->Parent()->AsClassDefinition()->Ident()->Name(), checker->Allocator()); - id->AsIdentifier()->SetReference(); accessor = checker->AllocNode(id, callee, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); callee->SetParent(accessor); diff --git a/ets2panda/compiler/lowering/ets/localClassLowering.cpp b/ets2panda/compiler/lowering/ets/localClassLowering.cpp index c79dfda9af..59e581d280 100644 --- a/ets2panda/compiler/lowering/ets/localClassLowering.cpp +++ b/ets2panda/compiler/lowering/ets/localClassLowering.cpp @@ -68,7 +68,6 @@ static ir::Statement *CreateCtorFieldInit(checker::ETSChecker *checker, util::St auto *thisExpr = allocator->New(); auto *fieldAccessExpr = allocator->New(name, allocator); - fieldAccessExpr->SetReference(); auto *leftHandSide = util::NodeAllocator::ForceSetParent( allocator, thisExpr, fieldAccessExpr, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); auto *rightHandSide = allocator->New(name, allocator); diff --git a/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp b/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp index 43a72da7c9..b5f1c99034 100644 --- a/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp +++ b/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp @@ -217,6 +217,8 @@ bool ObjectLiteralLowering::Perform(public_lib::Context *ctx, parser::Program *p return ast; }, Name()); +// std::cout << "ETS: " << program->Ast()->DumpEtsSrc() << std::endl; +// std::cout << "AST: " << program->Ast()->DumpJSON() << std::endl; return true; } diff --git a/ets2panda/compiler/lowering/ets/optionalLowering.cpp b/ets2panda/compiler/lowering/ets/optionalLowering.cpp index 3ecf31d07d..b91d0f213f 100644 --- a/ets2panda/compiler/lowering/ets/optionalLowering.cpp +++ b/ets2panda/compiler/lowering/ets/optionalLowering.cpp @@ -57,7 +57,6 @@ static ir::AstNode *LowerOptionalExpr(GetSource const &getSource, SetSource cons auto expressionCtx = varbinder::LexicalScope::Enter(varbinder, NearestScope(expr)); auto *tmpIdent = Gensym(allocator); auto *tmpIdentClone = tmpIdent->Clone(allocator, nullptr); - tmpIdentClone->SetReference(); // '0's act as placeholders auto *sequenceExpr = parser->CreateFormattedExpression( diff --git a/ets2panda/compiler/lowering/ets/promiseVoid.cpp b/ets2panda/compiler/lowering/ets/promiseVoid.cpp index 30de0dbe7a..ca9ce6bc21 100644 --- a/ets2panda/compiler/lowering/ets/promiseVoid.cpp +++ b/ets2panda/compiler/lowering/ets/promiseVoid.cpp @@ -71,7 +71,6 @@ ir::TypeNode *PromiseVoidInferencePhase::CreatePromiseVoidType(checker::ETSCheck auto *voidParam = [checker]() { auto paramsVector = ArenaVector(checker->Allocator()->Adapter()); auto *voidId = checker->AllocNode(compiler::Signatures::UNDEFINED, checker->Allocator()); - voidId->SetReference(); auto *part = checker->AllocNode(voidId); paramsVector.push_back(checker->AllocNode(part)); auto *params = checker->AllocNode(std::move(paramsVector)); @@ -81,7 +80,6 @@ ir::TypeNode *PromiseVoidInferencePhase::CreatePromiseVoidType(checker::ETSCheck auto *promiseVoidType = [checker, voidParam]() { auto *promiseId = checker->AllocNode(compiler::Signatures::BUILTIN_PROMISE_CLASS, checker->Allocator()); - promiseId->SetReference(); auto *part = checker->AllocNode(promiseId, voidParam, nullptr); auto *type = checker->AllocNode(part); return type; diff --git a/ets2panda/compiler/lowering/ets/stringComparison.cpp b/ets2panda/compiler/lowering/ets/stringComparison.cpp index 2ee5393c71..94c31f638c 100644 --- a/ets2panda/compiler/lowering/ets/stringComparison.cpp +++ b/ets2panda/compiler/lowering/ets/stringComparison.cpp @@ -85,7 +85,6 @@ void StringComparisonLowering::ProcessBinaryExpression(ir::BinaryExpression *exp auto *const callee = checker->AllocNode("compareTo", checker->Allocator()); accessor = checker->AllocNode(expr->Left(), callee, ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); - callee->SetReference(); callArgs.push_back(expr->Right()); auto callExpression = checker->AllocNode(accessor, std::move(callArgs), nullptr, false, false); diff --git a/ets2panda/compiler/lowering/ets/structLowering.cpp b/ets2panda/compiler/lowering/ets/structLowering.cpp index 16524732f5..d60c7cb547 100644 --- a/ets2panda/compiler/lowering/ets/structLowering.cpp +++ b/ets2panda/compiler/lowering/ets/structLowering.cpp @@ -45,7 +45,6 @@ ir::ETSTypeReference *CreateStructTypeReference(checker::ETSChecker *checker, for (const auto ¶m : etsStrucDeclaration->Definition()->TypeParams()->Params()) { auto *identRef = checker->AllocNode(param->AsTSTypeParameter()->Name()->Name(), allocator); - identRef->AsIdentifier()->SetReference(); referencePart = checker->AllocNode(identRef, nullptr, nullptr); @@ -59,7 +58,6 @@ ir::ETSTypeReference *CreateStructTypeReference(checker::ETSChecker *checker, auto *identSelfRef = checker->AllocNode(etsStrucDeclaration->Definition()->Ident()->Name(), allocator); - identSelfRef->AsIdentifier()->SetReference(); auto *referenceSelfPart = checker->AllocNode(identSelfRef, typeParamSelfInst, nullptr); @@ -70,7 +68,7 @@ ir::ETSTypeReference *CreateStructTypeReference(checker::ETSChecker *checker, auto *typeParamInst = checker->AllocNode(std::move(params)); auto *identRef = checker->AllocNode(util::StringView(STRUCT_CLASS_NAME), allocator); - identRef->AsIdentifier()->SetReference(); + auto *referencePart = checker->AllocNode(identRef, typeParamInst, nullptr); auto *typeReference = checker->AllocNode(referencePart); diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp index 5b6a0629a0..883c2a797f 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp @@ -163,7 +163,6 @@ void GlobalClassHandler::AddInitCall(ir::ClassDefinition *globalClass, ir::Metho ir::Identifier *GlobalClassHandler::RefIdent(const util::StringView &name) { auto *const callee = NodeAllocator::Alloc(allocator_, name, allocator_); - callee->SetReference(); return callee; } diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/globalDeclTransformer.cpp b/ets2panda/compiler/lowering/ets/topLevelStmts/globalDeclTransformer.cpp index a19c606ce5..faa4a6d904 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/globalDeclTransformer.cpp +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/globalDeclTransformer.cpp @@ -86,7 +86,6 @@ void GlobalDeclTransformer::VisitVariableDeclaration(ir::VariableDeclaration *va ir::Identifier *GlobalDeclTransformer::RefIdent(const util::StringView &name) { auto *const callee = util::NodeAllocator::Alloc(allocator_, name, allocator_); - callee->SetReference(); return callee; } diff --git a/ets2panda/ir/expressions/identifier.cpp b/ets2panda/ir/expressions/identifier.cpp index 33ae22c066..d7cac762ec 100644 --- a/ets2panda/ir/expressions/identifier.cpp +++ b/ets2panda/ir/expressions/identifier.cpp @@ -42,10 +42,6 @@ Identifier *Identifier::Clone(ArenaAllocator *const allocator, AstNode *const pa clone->SetParent(parent); } - if (this->IsReference()) { - clone->SetReference(); - } - clone->SetRange(Range()); return clone; @@ -134,4 +130,211 @@ checker::Type *Identifier::Check(checker::ETSChecker *checker) { return checker->GetAnalyzer()->Check(this); } + +bool Identifier::CommonDeclarationCheck(const ir::Identifier *id) const +{ + if (id->Parent() != nullptr) { + auto parent = id->Parent(); + // We need two Parts because check is too long to fit in one function + if (CheckDeclarationsPart1(parent) || CheckDeclarationsPart2(parent)) { + return true; + } + + // Parameters in methods are not references + // Example: + // function foo(a: int) + if (parent->IsETSParameterExpression()) { + return true; + } + + // Class Properties are not references + if (parent->IsClassProperty()) { + return true; + } + + // Identifier in catch is not a reference + // Example: + // + // _try_{ + // _throw_new_Exception();}_catch_(e)_{} + if (parent->IsCatchClause()) { + return true; + } + + // Type Parameter is not a reference + // Example: + // interface X {} + if (parent->IsTSTypeParameter()) { + return true; + } + + // Rest Parameter is not a reference + // Example: + // class A { + // constructor(... items :Object[]){} + // } + if (parent->IsRestElement()) { + return true; + } + + // Script function identifiers are not references + // Example: + // public static foo() + if (parent->IsScriptFunction()) { + return true; + } + + // New methods are not references + if (parent->IsMethodDefinition()) { + return true; + } + + // New classes are not references + if (parent->IsClassDefinition()) { + return true; + } + } + + // if (CheckNameRelated(id)) { + // return true; + // } + + return false; +} + + +bool Identifier::CheckDeclarationsPart1(const ir::AstNode *parentNode) const +{ + // All declarations are not references + if (parentNode->IsVariableDeclaration()) { + return true; + } + + if (parentNode->IsClassDeclaration()) { + return true; + } + + if (parentNode->IsETSPackageDeclaration()) { + return true; + } + + // if (parentNode->IsVariableDeclarator()) { + // return true; + // } + + if (parentNode->IsFunctionDeclaration()) { + return true; + } + + if (parentNode->IsImportDeclaration()) { + return true; + } + + if (parentNode->IsETSImportDeclaration()) { + return true; + } + + if (parentNode->IsETSStructDeclaration()) { + return true; + } + + if (parentNode->IsExportAllDeclaration()) { + return true; + } + + if (parentNode->IsExportDefaultDeclaration()) { + return true; + } + + return false; +} + +bool Identifier::CheckDeclarationsPart2(const ir::AstNode *parentNode) const +{ + // All declarations are not references + if (parentNode->IsExportNamedDeclaration()) { + return true; + } + + if (parentNode->IsTSEnumDeclaration()) { + return true; + } + + if (parentNode->IsTSInterfaceDeclaration()) { + return true; + } + + if (parentNode->IsTSModuleDeclaration()) { + return true; + } + + if (parentNode->IsTSSignatureDeclaration()) { + return true; + } + + if (parentNode->IsETSReExportDeclaration()) { + return true; + } + + if (parentNode->IsTSImportEqualsDeclaration()) { + return true; + } + + if (parentNode->IsTSTypeAliasDeclaration()) { + return true; + } + + if (parentNode->IsTSTypeParameterDeclaration()) { + return true; + } + + if (parentNode->IsDeclare()) { + return true; + } + + if (parentNode->Parent() != nullptr) { + auto parent = parentNode->Parent(); + if (parent->IsTSEnumDeclaration()) { + return true; + } + } + + return false; +} + +bool Identifier::CheckNameRelated(const ir::Identifier *id) const +{ + auto name = id->Name(); + // NOTE: skip $dynmodule and JSRuntime identifiers that are not references + if (name == "$dynmodule" || name == "JSRuntime") { + return true; + } + + // NOTE: skip identifiers that are not references + if (name.Utf8().find("") == 0) { + return true; + } + + // NOTE: should be deleted as soon as issue with Identifiers with no name is fixed + if (name.Empty()) { + return true; + } + + // NOTE: some lambda identifiers are not references + if (name.Utf8().find("lambda$invoke") == 0) { + return true; + } + // NOTE: currently some generated gensym identifiers are not references + if (name.Utf8().find("gensym") == 0) { + return true; + } + + // NOTE: currently some generated field identifiers are not references + if (name.Utf8().find("field") == 0) { + return true; + } + + return false; +} + } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/expressions/identifier.h b/ets2panda/ir/expressions/identifier.h index f25252a1b8..ed412231a9 100644 --- a/ets2panda/ir/expressions/identifier.h +++ b/ets2panda/ir/expressions/identifier.h @@ -31,12 +31,11 @@ using ENUMBITOPS_OPERATORS; enum class IdentifierFlags : uint32_t { NONE = 0U, OPTIONAL = 1U << 0U, - REFERENCE = 1U << 1U, - TDZ = 1U << 2U, - PRIVATE = 1U << 3U, - GET = 1U << 4U, - SET = 1U << 5U, - IGNORE_BOX = 1U << 6U, + TDZ = 1U << 1U, + PRIVATE = 1U << 2U, + GET = 1U << 3U, + SET = 1U << 4U, + IGNORE_BOX = 1U << 5U, }; } // namespace ark::es2panda::ir @@ -113,16 +112,16 @@ public: [[nodiscard]] bool IsReference() const noexcept { - return (flags_ & IdentifierFlags::REFERENCE) != 0; - } - - void SetReference(bool const isReference = true) noexcept - { - if (isReference) { - flags_ |= IdentifierFlags::REFERENCE; - } else { - flags_ &= ~IdentifierFlags::REFERENCE; + // GLOBAL class is not a reference + if (Name() == "ETSGLOBAL") { + return false; } + // Checking most of the cases + if (CommonDeclarationCheck(this)) { + return false; + } + + return true; } [[nodiscard]] bool IsTdz() const noexcept @@ -202,6 +201,11 @@ public: checker::Type *Check(checker::TSChecker *checker) override; checker::Type *Check(checker::ETSChecker *checker) override; + bool CheckNameRelated(const ir::Identifier *id) const; + bool CheckDeclarationsPart2(const ir::AstNode *parentNode) const; + bool CheckDeclarationsPart1(const ir::AstNode *parentNode) const; + bool CommonDeclarationCheck(const ir::Identifier *id) const; + void Accept(ASTVisitorT *v) override { v->Accept(this); diff --git a/ets2panda/parser/ASparser.cpp b/ets2panda/parser/ASparser.cpp index bed6e27bc7..e6aa085c9f 100644 --- a/ets2panda/parser/ASparser.cpp +++ b/ets2panda/parser/ASparser.cpp @@ -345,7 +345,6 @@ std::tuple ASParser::ParsePatternElementToken(E } case lexer::TokenType::LITERAL_IDENT: { returnNode = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - returnNode->AsIdentifier()->SetReference(); returnNode->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); @@ -846,7 +845,6 @@ ir::Expression *ASParser::ParsePotentialAsExpression(ir::Expression *primaryExpr ir::Identifier *ASParser::ParsePrimaryExpressionIdent([[maybe_unused]] ExpressionParseFlags flags) { auto *identNode = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - identNode->SetReference(); identNode->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index adc578920a..9c3599e4a8 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -500,7 +500,6 @@ ir::AstNode *ETSParser::ParseInnerRest(const ArenaVector &propert util::StringView tokenName = util::StringView {compiler::Signatures::STATIC_INVOKE_METHOD}; memberModifiers |= ir::ModifierFlags::STATIC; auto *ident = AllocNode(tokenName, Allocator()); - ident->SetReference(false); ident->SetRange({Lexer()->GetToken().Start(), Lexer()->GetToken().End()}); return parseClassMethod(ident); } @@ -1150,7 +1149,6 @@ ir::ExportNamedDeclaration *ETSParser::ParseSingleExport(ir::ModifierFlags modif { lexer::Token token = Lexer()->GetToken(); auto *exported = AllocNode(token.Ident(), Allocator()); - exported->SetReference(); exported->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); // eat exported variable name @@ -1206,7 +1204,6 @@ std::pair ETSParser::ParseN lexer::Token importedToken = Lexer()->GetToken(); auto *imported = AllocNode(importedToken.Ident(), Allocator()); ir::Identifier *local = nullptr; - imported->SetReference(); imported->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); @@ -1226,7 +1223,6 @@ std::pair ETSParser::ParseN result.emplace_back(specifier); } else { auto *imported = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - imported->SetReference(); imported->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); auto *specifier = AllocNode(imported); @@ -1266,7 +1262,6 @@ void ETSParser::ParseNameSpaceSpecifier(ArenaVector *specifiers, auto *local = AllocNode(util::StringView(""), Allocator()); if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COMMA || Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_FROM || isReExport) { - local->SetReference(); auto *specifier = AllocNode(local); specifier->SetRange({namespaceStart, Lexer()->GetToken().End()}); specifiers->push_back(specifier); @@ -1290,7 +1285,6 @@ ir::AstNode *ETSParser::ParseImportDefaultSpecifier(ArenaVector * } auto *imported = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - imported->SetReference(); imported->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); // Eat import specifier. @@ -1424,7 +1418,6 @@ ir::Expression *ETSParser::CreateParameterThis(const util::StringView className) paramIdent->SetRange(Lexer()->GetToken().Loc()); ir::Expression *classTypeName = AllocNode(className, Allocator()); - classTypeName->AsIdentifier()->SetReference(); classTypeName->SetRange(Lexer()->GetToken().Loc()); auto typeRefPart = AllocNode(classTypeName, nullptr, nullptr); diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index de66723267..04aad25a10 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -470,7 +470,6 @@ ir::MethodDefinition *ETSParser::ParseClassMethodDefinition(ir::Identifier *meth auto *method = AllocNode(methodKind, methodName->Clone(Allocator(), nullptr)->AsExpression(), funcExpr, modifiers, Allocator(), false); method->SetRange(funcExpr->Range()); - func->Id()->SetReference(); return method; } @@ -486,7 +485,6 @@ ir::MethodDefinition *ETSParser::ParseClassMethod(ClassElementDescriptor *desc, ir::ScriptFunction *func = ParseFunction(desc->newStatus); if (propName->IsIdentifier()) { func->SetIdent(propName->AsIdentifier()->Clone(Allocator(), nullptr)); - func->Id()->SetReference(); } auto *funcExpr = AllocNode(func); @@ -911,8 +909,6 @@ ir::MethodDefinition *ETSParser::ParseInterfaceMethod(ir::ModifierFlags flags, i Allocator(), false); method->SetRange(funcExpr->Range()); - func->Id()->SetReference(); - ConsumeSemicolon(method); return method; diff --git a/ets2panda/parser/TSparser.cpp b/ets2panda/parser/TSparser.cpp index 0173a37252..9de7d59ebc 100644 --- a/ets2panda/parser/TSparser.cpp +++ b/ets2panda/parser/TSparser.cpp @@ -689,7 +689,6 @@ ir::TypeNode *TSParser::ParseTypeReferenceOrQuery(bool parseQuery) ir::Expression *typeName = AllocNode(Lexer()->GetToken().Ident(), Allocator()); typeName->SetRange(Lexer()->GetToken().Loc()); - typeName->AsIdentifier()->SetReference(); if (Lexer()->Lookahead() == lexer::LEX_CHAR_LESS_THAN) { Lexer()->ForwardToken(lexer::TokenType::PUNCTUATOR_LESS_THAN, 1); @@ -1465,7 +1464,6 @@ bool TSParser::IsNamedFunctionExpression() ir::Identifier *TSParser::ParsePrimaryExpressionIdent([[maybe_unused]] ExpressionParseFlags flags) { auto *identNode = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - identNode->SetReference(); identNode->SetRange(Lexer()->GetToken().Loc()); Lexer()->NextToken(); diff --git a/ets2panda/parser/TypedParser.cpp b/ets2panda/parser/TypedParser.cpp index 7bec130afa..b54e00f3ed 100644 --- a/ets2panda/parser/TypedParser.cpp +++ b/ets2panda/parser/TypedParser.cpp @@ -1165,8 +1165,6 @@ ir::Expression *TypedParser::ParseQualifiedName(ExpressionParseFlags flags) ThrowSyntaxError("Identifier expected"); } - expr->AsIdentifier()->SetReference(); - if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_PERIOD) { expr = ParseQualifiedReference(expr, flags); } @@ -1210,7 +1208,6 @@ ir::Expression *TypedParser::ParseQualifiedReference(ir::Expression *typeName, E propName = AllocNode(Lexer()->GetToken().Ident(), Allocator()); } - propName->SetReference(); propName->SetRange(Lexer()->GetToken().Loc()); typeName = AllocNode(typeName, propName); diff --git a/ets2panda/parser/expressionParser.cpp b/ets2panda/parser/expressionParser.cpp index 39e4dc506d..2e8fcfa1c6 100644 --- a/ets2panda/parser/expressionParser.cpp +++ b/ets2panda/parser/expressionParser.cpp @@ -852,7 +852,6 @@ ir::MetaProperty *ParserImpl::ParsePotentialNewTarget() ir::Identifier *ParserImpl::ParsePrimaryExpressionIdent([[maybe_unused]] ExpressionParseFlags flags) { auto *identNode = AllocNode(lexer_->GetToken().Ident(), Allocator()); - identNode->SetReference(); identNode->SetRange(lexer_->GetToken().Loc()); lexer_->NextToken(); @@ -1024,7 +1023,6 @@ ir::Expression *ParserImpl::ParseHashMaskOperator() ValidatePrivateIdentifier(); auto *privateIdent = AllocNode(lexer_->GetToken().Ident(), Allocator()); privateIdent->SetPrivate(true); - privateIdent->SetReference(); lexer_->NextToken(); if (lexer_->GetToken().Type() != lexer::TokenType::KEYW_IN) { @@ -1450,7 +1448,6 @@ ir::Expression *ParserImpl::ParseOptionalChain(ir::Expression *leftSideExpr) const auto tokenType = lexer_->GetToken().Type(); if (tokenType == lexer::TokenType::LITERAL_IDENT) { auto *identNode = AllocNode(lexer_->GetToken().Ident(), Allocator()); - identNode->SetReference(); identNode->SetPrivate(isPrivate); identNode->SetRange(lexer_->GetToken().Loc()); @@ -1582,7 +1579,6 @@ ir::MemberExpression *ParserImpl::ParsePrivatePropertyAccess(ir::Expression *pri auto *privateIdent = AllocNode(lexer_->GetToken().Ident(), Allocator()); privateIdent->SetRange({memberStart, lexer_->GetToken().End()}); privateIdent->SetPrivate(true); - privateIdent->SetReference(); lexer_->NextToken(); auto *memberExpr = AllocNode(primaryExpr, privateIdent, @@ -1803,7 +1799,6 @@ ir::Expression *ParserImpl::ParsePatternElement(ExpressionParseFlags flags, bool } case lexer::TokenType::LITERAL_IDENT: { returnNode = AllocNode(lexer_->GetToken().Ident(), Allocator()); - returnNode->AsIdentifier()->SetReference(); returnNode->SetRange(lexer_->GetToken().Loc()); lexer_->NextToken(); break; @@ -1931,7 +1926,6 @@ ir::Property *ParserImpl::ParseShorthandProperty(const lexer::LexerPosition *sta key->SetRange(lexer_->GetToken().Loc()); ir::Expression *value = AllocNode(ident, Allocator()); - value->AsIdentifier()->SetReference(); value->SetRange(lexer_->GetToken().Loc()); lexer::SourcePosition end; diff --git a/ets2panda/parser/expressionTSParser.cpp b/ets2panda/parser/expressionTSParser.cpp index 006a4b5fd0..304ad42973 100644 --- a/ets2panda/parser/expressionTSParser.cpp +++ b/ets2panda/parser/expressionTSParser.cpp @@ -169,7 +169,6 @@ ir::AnnotatedExpression *TSParser::ParsePatternElementGetReturnNode(ExpressionPa } case lexer::TokenType::LITERAL_IDENT: { ir::AnnotatedExpression *returnNode = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - returnNode->AsIdentifier()->SetReference(); if (returnNode->AsIdentifier()->Decorators().empty()) { returnNode->SetRange(Lexer()->GetToken().Loc()); @@ -354,7 +353,6 @@ ir::Expression *TSParser::ParseModuleReference() ir::TSTypeReference *TSParser::ParseConstExpression() { auto *identRef = AllocNode(Lexer()->GetToken().Ident(), Allocator()); - identRef->SetReference(); identRef->SetRange(Lexer()->GetToken().Loc()); auto *typeReference = AllocNode(identRef, nullptr); diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index d9f83bfac9..9d3049f2a3 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -1172,6 +1172,10 @@ ir::Identifier *ParserImpl::ExpectIdentifier(bool isReference, bool isUserDefine if (token.IsDefinableTypeName() && isUserDefinedType) { ThrowSyntaxError("Cannot be used as user-defined type."); } + if(isReference) + { + + } auto const &tokenStart = token.Start(); util::StringView tokenName {}; @@ -1188,7 +1192,6 @@ ir::Identifier *ParserImpl::ExpectIdentifier(bool isReference, bool isUserDefine } auto *ident = AllocNode(tokenName, Allocator()); - ident->SetReference(isReference); // NOTE: here actual token can be changed! ident->SetRange({tokenStart, lexer_->GetToken().End()}); diff --git a/ets2panda/parser/statementParser.cpp b/ets2panda/parser/statementParser.cpp index 127d808c12..f52d65a2e1 100644 --- a/ets2panda/parser/statementParser.cpp +++ b/ets2panda/parser/statementParser.cpp @@ -467,7 +467,6 @@ ir::BreakStatement *ParserImpl::ParseBreakStatement() } auto *identNode = AllocNode(label, Allocator()); - identNode->SetReference(); identNode->SetRange(lexer_->GetToken().Loc()); auto *breakStatement = AllocNode(identNode); @@ -521,7 +520,6 @@ ir::ContinueStatement *ParserImpl::ParseContinueStatement() } auto *identNode = AllocNode(label, Allocator()); - identNode->SetReference(); identNode->SetRange(lexer_->GetToken().Loc()); auto *continueStatement = AllocNode(identNode); @@ -1018,7 +1016,6 @@ ir::LabelledStatement *ParserImpl::ParseLabelledStatement(const lexer::LexerPosi SavedParserContext newCtx(this, ParserStatus::IN_LABELED, actualLabel); auto *identNode = AllocNode(actualLabel, Allocator()); - identNode->SetReference(); identNode->SetRange(pos.GetToken().Loc()); lexer_->NextToken(); @@ -1720,7 +1717,6 @@ ir::Identifier *ParserImpl::ParseNamedImport(const lexer::Token &importedToken) CheckRestrictedBinding(importedToken.KeywordType()); auto *local = AllocNode(importedToken.Ident(), Allocator()); - local->SetReference(); local->SetRange(importedToken.Loc()); return local; @@ -1738,7 +1734,6 @@ void ParserImpl::ParseNamedImportSpecifiers(ArenaVector *specifie lexer::Token importedToken = lexer_->GetToken(); auto *imported = AllocNode(importedToken.Ident(), Allocator()); ir::Identifier *local = nullptr; - imported->SetReference(); imported->SetRange(lexer_->GetToken().Loc()); lexer_->NextToken(); // eat import name diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 988ffb856c..051b0ecb44 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -60,6 +60,8 @@ #include "util/ustring.h" #include "checker/types/type.h" #include "checker/types/ets/types.h" +#include "variable.h" +#include "variableFlags.h" namespace ark::es2panda::varbinder { @@ -207,8 +209,34 @@ void ETSBinder::LookupIdentReference(ir::Identifier *ident) return; } + // if(name.Mutf8() == "p") + // { + // std::cout << "bingo" << std::endl; + // } + + auto ref = ident->IsReference(); + auto dec = res.variable->Declaration()->IsLetOrConstDecl(); + auto flag = res.variable->HasFlag(VariableFlags::INITIALIZED); + if(ref && dec && flag) + { + + } + if (ident->IsReference() && res.variable->Declaration()->IsLetOrConstDecl() && !res.variable->HasFlag(VariableFlags::INITIALIZED)) { +// std::cout << "Prog " << Program()->Ast()->DumpEtsSrc() << std::endl; +// std::cout << "Parent: " << ident->Parent()->Parent()->Parent()->Parent()->DumpEtsSrc() << std::endl; +// std::cout << "id " << ident->DumpEtsSrc() << std::endl; +// auto type = res.variable->Type(); +// if(res.variable->IsLocalVariable() /*&& res.variable->AsLocalVariable()->GetScope()->Node()->AsClassElement()->Value() != nullptr*/) +// { +// std::cout << "this is nightmare" << std::endl; +// res.variable->AddFlag(VariableFlags::INITIALIZED); +// return; +// } + +// std::cout << "ts type " << type << std::endl; + ThrowTDZ(ident->Start(), name); } } diff --git a/ets2panda/varbinder/varbinder.cpp b/ets2panda/varbinder/varbinder.cpp index b9c9c3e1ef..03450626a4 100644 --- a/ets2panda/varbinder/varbinder.cpp +++ b/ets2panda/varbinder/varbinder.cpp @@ -14,7 +14,6 @@ */ #include "varbinder.h" - #include "varbinder/privateBinding.h" #include "parser/program/program.h" #include "util/helpers.h" @@ -246,7 +245,10 @@ void VarBinder::InstantiatePrivateContext(const ir::Identifier *ident) const void VarBinder::LookupIdentReference(ir::Identifier *ident) { + auto res = scope_->Find(ident->Name(), BindingOptions()); + if (!ident->IsReference()) { + ident->SetVariable(res.variable); return; } @@ -259,7 +261,6 @@ void VarBinder::LookupIdentReference(ir::Identifier *ident) return; } - auto res = scope_->Find(ident->Name(), BindingOptions()); if (res.level != 0) { ASSERT(res.variable); res.variable->SetLexical(res.scope); -- Gitee