• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 "newExpression.h"
17 
18 #include <compiler/core/pandagen.h>
19 #include <typescript/checker.h>
20 #include <ir/astDump.h>
21 #include <ir/ts/tsTypeParameterInstantiation.h>
22 
23 namespace panda::es2panda::ir {
24 
Iterate(const NodeTraverser & cb) const25 void NewExpression::Iterate(const NodeTraverser &cb) const
26 {
27     cb(callee_);
28 
29     if (typeParams_) {
30         cb(typeParams_);
31     }
32 
33     for (auto *it : arguments_) {
34         cb(it);
35     }
36 }
37 
Dump(ir::AstDumper * dumper) const38 void NewExpression::Dump(ir::AstDumper *dumper) const
39 {
40     dumper->Add({{"type", "NewExpression"}, {"callee", callee_}, {"typeParameters", AstDumper::Optional(typeParams_)},
41                  {"arguments", arguments_}});
42 }
43 
Compile(compiler::PandaGen * pg) const44 void NewExpression::Compile(compiler::PandaGen *pg) const
45 {
46     compiler::RegScope rs(pg);
47     compiler::VReg ctor = pg->AllocReg();
48 
49     callee_->Compile(pg);
50     pg->StoreAccumulator(this, ctor);
51 
52     if (!util::Helpers::ContainSpreadElement(arguments_)) {
53         for (const auto *it : arguments_) {
54             compiler::VReg arg = pg->AllocReg();
55             it->Compile(pg);
56             pg->StoreAccumulator(this, arg);
57         }
58 
59         pg->NewObject(this, ctor, arguments_.size() + 1);
60     } else {
61         compiler::VReg argsObj = pg->AllocReg();
62 
63         pg->CreateArray(this, arguments_, argsObj);
64         pg->NewObjSpread(this, ctor);
65     }
66 }
67 
Check(checker::Checker * checker) const68 checker::Type *NewExpression::Check(checker::Checker *checker) const
69 {
70     checker::Type *calleeType = callee_->Check(checker);
71 
72     // TODO(aszilagyi): handle optional chain
73     if (calleeType->IsObjectType()) {
74         checker::ObjectType *calleeObj = calleeType->AsObjectType();
75         return checker->resolveCallOrNewExpression(calleeObj->ConstructSignatures(), arguments_, Start());
76     }
77 
78     checker->ThrowTypeError("This expression is not callable.", Start());
79     return nullptr;
80 }
81 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)82 void NewExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
83 {
84     callee_ = std::get<ir::AstNode *>(cb(callee_))->AsExpression();
85 
86     for (auto iter = arguments_.begin(); iter != arguments_.end(); iter++) {
87         *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
88     }
89 }
90 
91 }  // namespace panda::es2panda::ir
92