• 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 "resizableArrayLowering.h"
17 #include <utility>
18 #include "compiler/lowering/util.h"
19 #include "ir/ets/etsUnionType.h"
20 
21 namespace ark::es2panda::compiler {
22 
23 using AstNodePtr = ir::AstNode *;
24 
ConvertToResizableArrayType(ir::TSArrayType * node,public_lib::Context * ctx)25 static ir::AstNode *ConvertToResizableArrayType(ir::TSArrayType *node, public_lib::Context *ctx)
26 {
27     auto *parser = ctx->parser->AsETSParser();
28     ir::TypeNode *typeAnnotation =
29         parser->CreateFormattedTypeAnnotation("Array<" + node->ElementType()->DumpEtsSrc() + ">");
30     ES2PANDA_ASSERT(typeAnnotation != nullptr);
31     typeAnnotation->SetAnnotations(std::move(node->Annotations()));
32     typeAnnotation->SetParent(node->Parent());
33     typeAnnotation->SetRange(node->Range());
34     RefineSourceRanges(node);
35     typeAnnotation->AddModifier(node->Modifiers());
36     return typeAnnotation;
37 }
38 
PerformForModule(public_lib::Context * ctx,parser::Program * program)39 bool ResizableArrayConvert::PerformForModule(public_lib::Context *ctx, parser::Program *program)
40 {
41     program->Ast()->TransformChildrenRecursivelyPreorder(
42         [ctx](ir::AstNode *node) -> AstNodePtr {
43             if (node->IsTSArrayType()) {
44                 return ConvertToResizableArrayType(node->AsTSArrayType(), ctx);
45             }
46             return node;
47         },
48         Name());
49 
50     return true;
51 }
52 }  // namespace ark::es2panda::compiler
53