• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "etsNewClassInstanceExpression.h"
17 
18 #include "compiler/core/ETSGen.h"
19 #include "compiler/core/pandagen.h"
20 #include "checker/TSchecker.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 
24 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void ETSNewClassInstanceExpression::TransformChildren(const NodeTransformer &cb)
26 {
27     typeReference_ = cb(typeReference_)->AsExpression();
28 
29     for (auto *&arg : arguments_) {
30         arg = cb(arg)->AsExpression();
31     }
32 
33     if (classDef_ != nullptr) {
34         classDef_ = cb(classDef_)->AsClassDefinition();
35     }
36 }
37 
Iterate(const NodeTraverser & cb) const38 void ETSNewClassInstanceExpression::Iterate([[maybe_unused]] const NodeTraverser &cb) const
39 {
40     cb(typeReference_);
41 
42     for (auto *arg : arguments_) {
43         cb(arg);
44     }
45 
46     if (classDef_ != nullptr) {
47         cb(classDef_);
48     }
49 }
50 
Dump(ir::AstDumper * dumper) const51 void ETSNewClassInstanceExpression::Dump(ir::AstDumper *dumper) const
52 {
53     dumper->Add({{"type", "ETSNewClassInstanceExpression"},
54                  {"typeReference", typeReference_},
55                  {"arguments", arguments_},
56                  {"classBody", AstDumper::Optional(classDef_)}});
57 }
58 
Dump(ir::SrcDumper * dumper) const59 void ETSNewClassInstanceExpression::Dump(ir::SrcDumper *dumper) const
60 {
61     dumper->Add("new ");
62     if (typeReference_ != nullptr) {
63         typeReference_->Dump(dumper);
64     }
65     dumper->Add("(");
66     for (auto argument : arguments_) {
67         argument->Dump(dumper);
68         if (argument != arguments_.back()) {
69             dumper->Add(", ");
70         }
71     }
72     dumper->Add(")");
73 }
74 
Compile(compiler::PandaGen * pg) const75 void ETSNewClassInstanceExpression::Compile(compiler::PandaGen *pg) const
76 {
77     pg->GetAstCompiler()->Compile(this);
78 }
79 
Compile(compiler::ETSGen * etsg) const80 void ETSNewClassInstanceExpression::Compile(compiler::ETSGen *etsg) const
81 {
82     etsg->GetAstCompiler()->Compile(this);
83 }
84 
Check(checker::TSChecker * checker)85 checker::Type *ETSNewClassInstanceExpression::Check(checker::TSChecker *checker)
86 {
87     return checker->GetAnalyzer()->Check(this);
88 }
89 
Check(checker::ETSChecker * checker)90 checker::Type *ETSNewClassInstanceExpression::Check(checker::ETSChecker *checker)
91 {
92     return checker->GetAnalyzer()->Check(this);
93 }
94 
ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const & other,ArenaAllocator * const allocator)95 ETSNewClassInstanceExpression::ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const &other,
96                                                              ArenaAllocator *const allocator)
97     : Expression(static_cast<Expression const &>(other)), arguments_(allocator->Adapter()), signature_(other.signature_)
98 {
99     typeReference_ = other.typeReference_->Clone(allocator, this)->AsExpression();
100     classDef_ = other.classDef_->Clone(allocator, this)->AsClassDefinition();
101 
102     for (auto *const argument : other.arguments_) {
103         arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression());
104     }
105 }
106 
107 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)108 ETSNewClassInstanceExpression *ETSNewClassInstanceExpression::Clone(ArenaAllocator *const allocator,
109                                                                     AstNode *const parent)
110 {
111     if (auto *const clone = allocator->New<ETSNewClassInstanceExpression>(*this, allocator); clone != nullptr) {
112         if (parent != nullptr) {
113             clone->SetParent(parent);
114         }
115         return clone;
116     }
117 
118     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
119 }
120 }  // namespace panda::es2panda::ir
121