From 7bd4393cfd57c885d9db9d66bf5a55835e83252b Mon Sep 17 00:00:00 2001 From: Anna Antipina Date: Tue, 22 Jul 2025 16:24:05 +0300 Subject: [PATCH] Emit parameter readonly annotation Emit parameter annotation to determine in runtime whether the parameter is readonly. Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICN2CC Change-Id: I9f64bd3ae4169f67c4d14d9253168d2b705e43f5 Signed-off-by: Anna Antipina --- ets2panda/BUILD.gn | 2 +- ets2panda/CMakeLists.txt | 2 +- ...ion.cpp => insertParametersAnnotation.cpp} | 65 +++++++++++++++---- ...otation.h => insertParametersAnnotation.h} | 4 +- ets2panda/compiler/lowering/phase.cpp | 4 +- ets2panda/compiler/scripts/signatures.yaml | 4 ++ ets2panda/parser/program/program.h | 2 +- 7 files changed, 64 insertions(+), 19 deletions(-) rename ets2panda/compiler/lowering/ets/{insertOptionalParametersAnnotation.cpp => insertParametersAnnotation.cpp} (60%) rename ets2panda/compiler/lowering/ets/{insertOptionalParametersAnnotation.h => insertParametersAnnotation.h} (88%) diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index 60c32ea888d..fc19cb6a911 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -240,7 +240,7 @@ libes2panda_sources = [ "compiler/lowering/ets/extensionAccessorLowering.cpp", "compiler/lowering/ets/genericBridgesLowering.cpp", "compiler/lowering/ets/gradualTypeNarrowing.cpp", - "compiler/lowering/ets/insertOptionalParametersAnnotation.cpp", + "compiler/lowering/ets/insertParametersAnnotation.cpp", "compiler/lowering/ets/interfaceObjectLiteralLowering.cpp", "compiler/lowering/ets/interfacePropertyDeclarations.cpp", "compiler/lowering/ets/lambdaLowering.cpp", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index 73d465ae364..f356de9fce0 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -295,7 +295,7 @@ set(ES2PANDA_LIB_SRC compiler/lowering/ets/spreadLowering.cpp compiler/lowering/ets/objectIndexAccess.cpp compiler/lowering/ets/objectIterator.cpp - compiler/lowering/ets/insertOptionalParametersAnnotation.cpp + compiler/lowering/ets/insertParametersAnnotation.cpp compiler/lowering/ets/interfacePropertyDeclarations.cpp compiler/lowering/ets/opAssignment.cpp compiler/lowering/ets/ambientLowering.cpp diff --git a/ets2panda/compiler/lowering/ets/insertOptionalParametersAnnotation.cpp b/ets2panda/compiler/lowering/ets/insertParametersAnnotation.cpp similarity index 60% rename from ets2panda/compiler/lowering/ets/insertOptionalParametersAnnotation.cpp rename to ets2panda/compiler/lowering/ets/insertParametersAnnotation.cpp index 44d8ae95b58..9efb86c137c 100644 --- a/ets2panda/compiler/lowering/ets/insertOptionalParametersAnnotation.cpp +++ b/ets2panda/compiler/lowering/ets/insertParametersAnnotation.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "insertOptionalParametersAnnotation.h" +#include "insertParametersAnnotation.h" #include "compiler/lowering/util.h" namespace ark::es2panda::compiler { @@ -30,20 +30,39 @@ static ir::Identifier *GenDefaultAnnoId(ArenaAllocator *allocator) return UAlloc::ForceSetParent(allocator, Signatures::DEFAULT_ANNO_FOR_FUNC, allocator); } +static ir::Identifier *GenReadonlyAnnoQualifiedLeft(ArenaAllocator *allocator) +{ + return UAlloc::ForceSetParent(allocator, Signatures::READONLY_ANNO_QUALIFIED_LEFT, allocator); +} + +static ir::Identifier *GenReadonlyAnnoId(ArenaAllocator *allocator) +{ + return UAlloc::ForceSetParent(allocator, Signatures::READONLY_ANNO_FOR_PARAMETER, allocator); +} + static ir::Identifier *GenDefaultPropId(ArenaAllocator *allocator, util::StringView const name) { return UAlloc::ForceSetParent(allocator, name, allocator); } +static ir::ETSTypeReference *GenAnnoNameRef(ArenaAllocator *allocator, ir::Identifier *qualifier, + ir::Identifier *refName) +{ + auto qualifiedAnnoName = UAlloc::ForceSetParent(allocator, qualifier, refName, allocator); + auto refPart = + UAlloc::ForceSetParent(allocator, qualifiedAnnoName, nullptr, nullptr, allocator); + auto annoRef = UAlloc::ForceSetParent(allocator, refPart, allocator); + return annoRef; +} + static ir::ETSTypeReference *GenDefaultAnnoNameRef(ArenaAllocator *allocator) { - auto defaultQualifiedAnnoName = UAlloc::ForceSetParent( - allocator, GenAnnoQualifiedLeft(allocator), GenDefaultAnnoId(allocator), allocator); - auto defaultRefPart = UAlloc::ForceSetParent(allocator, defaultQualifiedAnnoName, nullptr, - nullptr, allocator); + return GenAnnoNameRef(allocator, GenAnnoQualifiedLeft(allocator), GenDefaultAnnoId(allocator)); +} - auto defaultRef = UAlloc::ForceSetParent(allocator, defaultRefPart, allocator); - return defaultRef; +static ir::ETSTypeReference *GenReadonlyAnnoNameRef(ArenaAllocator *allocator) +{ + return GenAnnoNameRef(allocator, GenReadonlyAnnoQualifiedLeft(allocator), GenReadonlyAnnoId(allocator)); } static ir::AstNode *GenMinArgCountItem(ArenaAllocator *allocator, const int32_t requiredArgs) @@ -87,7 +106,27 @@ static ir::AstNode *CreateDefaultAnnotationUsageForFunction(public_lib::Context return util::NodeAllocator::ForceSetParent(allocator, defaultRef, std::move(properties)); } -static void TryInsertDefaultAnnotation(public_lib::Context *ctx, ir::AstNode *node) +static void CreateReadonlyAnnotationUsageForFunctionParams(public_lib::Context *ctx, const ir::ScriptFunction *func) +{ + auto allocator = ctx->allocator; + for (auto const param : func->Params()) { + if (!param->IsETSParameterExpression()) { + ES2PANDA_ASSERT(param->IsIdentifier() && param->AsIdentifier()->IsErrorPlaceHolder()); + return; + } + auto *typeAnnot = param->AsETSParameterExpression()->Ident()->TypeAnnotation(); + if (typeAnnot != nullptr && typeAnnot->IsReadonlyType()) { + ir::ETSTypeReference *readonlyRef = GenReadonlyAnnoNameRef(allocator); + auto *annotUsage = + util::NodeAllocator::ForceSetParent(allocator, readonlyRef, allocator); + param->AsETSParameterExpression()->EmplaceAnnotations(annotUsage); + annotUsage->SetParent(param); + RefineSourceRanges(annotUsage); + } + } +} + +static void TryInsertParametersAnnotation(public_lib::Context *ctx, ir::AstNode *node) { if (!node->IsMethodDefinition()) { return; @@ -96,6 +135,8 @@ static void TryInsertDefaultAnnotation(public_lib::Context *ctx, ir::AstNode *no auto methodDef = node->AsMethodDefinition(); if (!methodDef->IsConstructor() && !methodDef->IsAbstract() && methodDef->Function() != nullptr) { auto methodFunc = methodDef->Function(); + CreateReadonlyAnnotationUsageForFunctionParams(ctx, methodFunc); + auto defaultAnno = CreateDefaultAnnotationUsageForFunction(ctx, methodFunc); if (defaultAnno != nullptr) { methodFunc->EmplaceAnnotations(defaultAnno->AsAnnotationUsage()); @@ -105,20 +146,20 @@ static void TryInsertDefaultAnnotation(public_lib::Context *ctx, ir::AstNode *no } } -bool InsertOptionalParametersAnnotation::PerformForModule(public_lib::Context *ctx, parser::Program *program) +bool InsertParametersAnnotation::PerformForModule(public_lib::Context *ctx, parser::Program *program) { if (program->Extension() != ScriptExtension::ETS) { return true; } - if (program->GetFlag(parser::ProgramFlags::AST_HAS_OPTIONAL_PARAMETER_ANNOTATION)) { + if (program->GetFlag(parser::ProgramFlags::AST_HAS_PARAMETERS_ANNOTATION)) { return true; } program->Ast()->IterateRecursivelyPostorder( - [ctx](ir::AstNode *node) -> void { TryInsertDefaultAnnotation(ctx, node); }); + [ctx](ir::AstNode *node) -> void { TryInsertParametersAnnotation(ctx, node); }); - program->SetFlag(parser::ProgramFlags::AST_HAS_OPTIONAL_PARAMETER_ANNOTATION); + program->SetFlag(parser::ProgramFlags::AST_HAS_PARAMETERS_ANNOTATION); return true; } diff --git a/ets2panda/compiler/lowering/ets/insertOptionalParametersAnnotation.h b/ets2panda/compiler/lowering/ets/insertParametersAnnotation.h similarity index 88% rename from ets2panda/compiler/lowering/ets/insertOptionalParametersAnnotation.h rename to ets2panda/compiler/lowering/ets/insertParametersAnnotation.h index 780d43d5140..3881e54b7ea 100644 --- a/ets2panda/compiler/lowering/ets/insertOptionalParametersAnnotation.h +++ b/ets2panda/compiler/lowering/ets/insertParametersAnnotation.h @@ -20,11 +20,11 @@ namespace ark::es2panda::compiler { -class InsertOptionalParametersAnnotation : public PhaseForDeclarations { +class InsertParametersAnnotation : public PhaseForDeclarations { public: std::string_view Name() const override { - return "InsertOptionalParametersAnnotation"; + return "InsertParametersAnnotation"; } bool PerformForModule(public_lib::Context *ctx, parser::Program *program) override; diff --git a/ets2panda/compiler/lowering/phase.cpp b/ets2panda/compiler/lowering/phase.cpp index bc06d4dca05..a67b6cec224 100644 --- a/ets2panda/compiler/lowering/phase.cpp +++ b/ets2panda/compiler/lowering/phase.cpp @@ -42,7 +42,7 @@ #include "compiler/lowering/ets/extensionAccessorLowering.h" #include "compiler/lowering/ets/genericBridgesLowering.h" #include "compiler/lowering/ets/gradualTypeNarrowing.h" -#include "compiler/lowering/ets/insertOptionalParametersAnnotation.h" +#include "compiler/lowering/ets/insertParametersAnnotation.h" #include "compiler/lowering/ets/interfaceObjectLiteralLowering.h" #include "compiler/lowering/ets/interfacePropertyDeclarations.h" #include "compiler/lowering/ets/lambdaLowering.h" @@ -107,7 +107,7 @@ std::vector GetETSPhaseList() new TopLevelStatements, new ResizableArrayConvert, new ExpressionLambdaConstructionPhase, - new InsertOptionalParametersAnnotation, + new InsertParametersAnnotation, new DefaultParametersInConstructorLowering, new DefaultParametersLowering, new AmbientLowering, diff --git a/ets2panda/compiler/scripts/signatures.yaml b/ets2panda/compiler/scripts/signatures.yaml index 15dfcdbfe96..0afe4f90552 100644 --- a/ets2panda/compiler/scripts/signatures.yaml +++ b/ets2panda/compiler/scripts/signatures.yaml @@ -222,6 +222,10 @@ defines: ref: DEFAULT_ANNO_FOR_FUNC - name: 'minArgCount' ref: MIN_ARGSCOUNT_OF_FUNC + - name: 'parameters' + ref: READONLY_ANNO_QUALIFIED_LEFT + - name: 'ReadonlyAnnotation' + ref: READONLY_ANNO_FOR_PARAMETER - name: 'gensym%%_anonymous_const' ref: EXPORT_DEFAULT_CONSTANT_ANONYMOUSLY - name: 'toByte' diff --git a/ets2panda/parser/program/program.h b/ets2panda/parser/program/program.h index 50563781160..5d76bcc4a83 100644 --- a/ets2panda/parser/program/program.h +++ b/ets2panda/parser/program/program.h @@ -65,7 +65,7 @@ enum class ProgramFlags : uint32_t { AST_STRING_CONSTANT_LOWERED = 1U << 6U, AST_IDENTIFIER_ANALYZED = 1U << 7U, AST_HAS_SCOPES_INITIALIZED = 1U << 8U, - AST_HAS_OPTIONAL_PARAMETER_ANNOTATION = 1U << 9U, + AST_HAS_PARAMETERS_ANNOTATION = 1U << 9U, }; class Program { -- Gitee