• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "etsNewMultiDimArrayInstanceExpression.h"
17 
18 #include "checker/ets/typeRelationContext.h"
19 #include "checker/TSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 
23 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)24 void ETSNewMultiDimArrayInstanceExpression::TransformChildren(const NodeTransformer &cb,
25                                                               std::string_view const transformationName)
26 {
27     if (auto *transformedNode = cb(typeReference_); typeReference_ != transformedNode) {
28         typeReference_->SetTransformedNode(transformationName, transformedNode);
29         typeReference_ = static_cast<TypeNode *>(transformedNode);
30     }
31 
32     for (auto *&dim : dimensions_) {
33         if (auto *transformedNode = cb(dim); dim != transformedNode) {
34             dim->SetTransformedNode(transformationName, transformedNode);
35             dim = transformedNode->AsExpression();
36         }
37     }
38 }
39 
Iterate(const NodeTraverser & cb) const40 void ETSNewMultiDimArrayInstanceExpression::Iterate(const NodeTraverser &cb) const
41 {
42     cb(typeReference_);
43     for (auto *dim : dimensions_) {
44         cb(dim);
45     }
46 }
47 
Dump(ir::AstDumper * dumper) const48 void ETSNewMultiDimArrayInstanceExpression::Dump(ir::AstDumper *dumper) const
49 {
50     dumper->Add({{"type", "ETSNewMultiDimArrayInstanceExpression"},
51                  {"typeReference", typeReference_},
52                  {"dimensions", dimensions_}});
53 }
54 
Dump(ir::SrcDumper * dumper) const55 void ETSNewMultiDimArrayInstanceExpression::Dump(ir::SrcDumper *dumper) const
56 {
57     dumper->Add("new ");
58     ES2PANDA_ASSERT(typeReference_);
59     typeReference_->Dump(dumper);
60     for (auto dim : dimensions_) {
61         dumper->Add("[");
62         dim->Dump(dumper);
63         dumper->Add("]");
64     }
65 }
66 
Compile(compiler::PandaGen * pg) const67 void ETSNewMultiDimArrayInstanceExpression::Compile(compiler::PandaGen *pg) const
68 {
69     pg->GetAstCompiler()->Compile(this);
70 }
Compile(compiler::ETSGen * etsg) const71 void ETSNewMultiDimArrayInstanceExpression::Compile(compiler::ETSGen *etsg) const
72 {
73     etsg->GetAstCompiler()->Compile(this);
74 }
75 
Check(checker::TSChecker * checker)76 checker::Type *ETSNewMultiDimArrayInstanceExpression::Check(checker::TSChecker *checker)
77 {
78     return checker->GetAnalyzer()->Check(this);
79 }
80 
Check(checker::ETSChecker * checker)81 checker::VerifiedType ETSNewMultiDimArrayInstanceExpression::Check(checker::ETSChecker *checker)
82 {
83     return {this, checker->GetAnalyzer()->Check(this)};
84 }
85 
ClearPreferredType()86 void ETSNewMultiDimArrayInstanceExpression::ClearPreferredType()
87 {
88     SetPreferredType(nullptr);
89     SetTsType(nullptr);
90     TypeReference()->SetBoxingUnboxingFlags(BoxingUnboxingFlags::NONE);
91 }
92 
ETSNewMultiDimArrayInstanceExpression(ETSNewMultiDimArrayInstanceExpression const & other,ArenaAllocator * const allocator)93 ETSNewMultiDimArrayInstanceExpression::ETSNewMultiDimArrayInstanceExpression(
94     ETSNewMultiDimArrayInstanceExpression const &other, ArenaAllocator *const allocator)
95     : Expression(static_cast<Expression const &>(other)),
96       dimensions_(allocator->Adapter()),
97       signature_(other.signature_)
98 {
99     typeReference_ = other.typeReference_->Clone(allocator, this);
100 
101     for (auto *const dimension : other.dimensions_) {
102         dimensions_.emplace_back(dimension->Clone(allocator, this)->AsExpression());
103     }
104 }
105 
Clone(ArenaAllocator * const allocator,AstNode * const parent)106 ETSNewMultiDimArrayInstanceExpression *ETSNewMultiDimArrayInstanceExpression::Clone(ArenaAllocator *const allocator,
107                                                                                     AstNode *const parent)
108 {
109     auto *const clone = allocator->New<ETSNewMultiDimArrayInstanceExpression>(*this, allocator);
110     ES2PANDA_ASSERT(clone);
111     if (parent != nullptr) {
112         clone->SetParent(parent);
113     }
114     clone->SetRange(Range());
115     return clone;
116 }
117 
SetPreferredTypeBasedOnFuncParam(checker::ETSChecker * checker,checker::Type * param,checker::TypeRelationFlag flags)118 void ETSNewMultiDimArrayInstanceExpression::SetPreferredTypeBasedOnFuncParam(checker::ETSChecker *checker,
119                                                                              checker::Type *param,
120                                                                              checker::TypeRelationFlag flags)
121 {
122     // NOTE (mmartin): This needs a complete solution
123     if (preferredType_ != nullptr) {
124         return;
125     }
126     if (!param->IsETSArrayType()) {
127         return;
128     }
129     checker::Type *elementType = param->AsETSArrayType();
130 
131     for (size_t i = 0; i < dimensions_.size(); i++) {
132         if (!elementType->IsETSArrayType()) {
133             return;
134         }
135         elementType = elementType->AsETSArrayType()->ElementType();
136     }
137     auto assignCtx =
138         checker::AssignmentContext(checker->Relation(), typeReference_, typeReference_->GetType(checker), elementType,
139                                    typeReference_->Start(), std::nullopt, checker::TypeRelationFlag::NO_THROW | flags);
140     if (assignCtx.IsAssignable()) {
141         SetPreferredType(param);
142     }
143 }
144 }  // namespace ark::es2panda::ir
145