• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "insertOptionalParametersAnnotation.h"
17 #include "compiler/lowering/util.h"
18 
19 namespace ark::es2panda::compiler {
20 
21 using UAlloc = util::NodeAllocator;
22 
GenAnnoQualifiedLeft(ArenaAllocator * allocator)23 static ir::Identifier *GenAnnoQualifiedLeft(ArenaAllocator *allocator)
24 {
25     return UAlloc::ForceSetParent<ir::Identifier>(allocator, Signatures::DEFAULT_ANNO_QUALIFIED_LEFT, allocator);
26 }
27 
GenDefaultAnnoId(ArenaAllocator * allocator)28 static ir::Identifier *GenDefaultAnnoId(ArenaAllocator *allocator)
29 {
30     return UAlloc::ForceSetParent<ir::Identifier>(allocator, Signatures::DEFAULT_ANNO_FOR_FUNC, allocator);
31 }
32 
GenDefaultPropId(ArenaAllocator * allocator,util::StringView const name)33 static ir::Identifier *GenDefaultPropId(ArenaAllocator *allocator, util::StringView const name)
34 {
35     return UAlloc::ForceSetParent<ir::Identifier>(allocator, name, allocator);
36 }
37 
GenDefaultAnnoNameRef(ArenaAllocator * allocator)38 static ir::ETSTypeReference *GenDefaultAnnoNameRef(ArenaAllocator *allocator)
39 {
40     auto defaultQualifiedAnnoName = UAlloc::ForceSetParent<ir::TSQualifiedName>(
41         allocator, GenAnnoQualifiedLeft(allocator), GenDefaultAnnoId(allocator), allocator);
42     auto defaultRefPart = UAlloc::ForceSetParent<ir::ETSTypeReferencePart>(allocator, defaultQualifiedAnnoName, nullptr,
43                                                                            nullptr, allocator);
44 
45     auto defaultRef = UAlloc::ForceSetParent<ir::ETSTypeReference>(allocator, defaultRefPart, allocator);
46     return defaultRef;
47 }
48 
GenMinArgCountItem(ArenaAllocator * allocator,const int32_t requiredArgs)49 static ir::AstNode *GenMinArgCountItem(ArenaAllocator *allocator, const int32_t requiredArgs)
50 {
51     auto *minArgsCountId = GenDefaultPropId(allocator, Signatures::MIN_ARGSCOUNT_OF_FUNC);
52     auto *minArgsCountValue = UAlloc::ForceSetParent<ir::NumberLiteral>(allocator, lexer::Number(requiredArgs));
53     auto *minArgsCount = UAlloc::ForceSetParent<ir::ClassProperty>(
54         allocator, minArgsCountId, minArgsCountValue, nullptr, ir::ModifierFlags::ANNOTATION_USAGE, allocator, false);
55     return minArgsCount;
56 }
57 
CreateDefaultAnnotationUsageForFunction(public_lib::Context * ctx,const ir::ScriptFunction * func)58 static ir::AstNode *CreateDefaultAnnotationUsageForFunction(public_lib::Context *ctx, const ir::ScriptFunction *func)
59 {
60     auto allocator = ctx->allocator;
61     // Note: for function that contain optional param or rest param, need to create a default annotation usage for them
62     // the annotation usage in the same format: `@std.functions.OptionalParametersAnnotation({minArgCount: ??})`
63     int32_t requiredArgsCnt = 0;
64     bool needInsert = false;
65     for (auto const param : func->Params()) {
66         if (!param->IsETSParameterExpression()) {
67             ES2PANDA_ASSERT(param->IsIdentifier() && param->AsIdentifier()->IsErrorPlaceHolder());
68             return nullptr;
69         }
70 
71         if (param->AsETSParameterExpression()->IsOptional() || param->AsETSParameterExpression()->IsRestParameter()) {
72             needInsert = param->AsETSParameterExpression()->IsOptional();
73             break;
74         }
75 
76         ++requiredArgsCnt;
77     }
78     if (!needInsert) {
79         // Note: no optional param, no need to generate and insert OptionalParametersAnnotation.
80         return nullptr;
81     }
82 
83     ir::ETSTypeReference *defaultRef = GenDefaultAnnoNameRef(allocator);
84     auto *minArgsCount = GenMinArgCountItem(allocator, requiredArgsCnt);
85     ArenaVector<ir::AstNode *> properties(allocator->Adapter());
86     properties.emplace_back(minArgsCount);
87     return util::NodeAllocator::ForceSetParent<ir::AnnotationUsage>(allocator, defaultRef, std::move(properties));
88 }
89 
TryInsertDefaultAnnotation(public_lib::Context * ctx,ir::AstNode * node)90 static void TryInsertDefaultAnnotation(public_lib::Context *ctx, ir::AstNode *node)
91 {
92     if (!node->IsMethodDefinition()) {
93         return;
94     }
95 
96     auto methodDef = node->AsMethodDefinition();
97     if (!methodDef->IsConstructor() && !methodDef->IsAbstract() && methodDef->Function() != nullptr) {
98         auto methodFunc = methodDef->Function();
99         auto defaultAnno = CreateDefaultAnnotationUsageForFunction(ctx, methodFunc);
100         if (defaultAnno != nullptr) {
101             methodFunc->Annotations().emplace_back(defaultAnno->AsAnnotationUsage());
102             defaultAnno->SetParent(methodFunc);
103             RefineSourceRanges(defaultAnno);
104         }
105     }
106 }
107 
PerformForModule(public_lib::Context * ctx,parser::Program * program)108 bool InsertOptionalParametersAnnotation::PerformForModule(public_lib::Context *ctx, parser::Program *program)
109 {
110     if (program->Extension() != ScriptExtension::ETS) {
111         return true;
112     }
113 
114     if (program->GetFlag(parser::ProgramFlags::AST_HAS_OPTIONAL_PARAMETER_ANNOTATION)) {
115         return true;
116     }
117 
118     program->Ast()->IterateRecursivelyPostorder(
119         [ctx](ir::AstNode *node) -> void { TryInsertDefaultAnnotation(ctx, node); });
120 
121     program->SetFlag(parser::ProgramFlags::AST_HAS_OPTIONAL_PARAMETER_ANNOTATION);
122 
123     return true;
124 }
125 }  // namespace ark::es2panda::compiler
126