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 "tsConstructorType.h"
17
18 #include <binder/binder.h>
19 #include <binder/scope.h>
20 #include <typescript/checker.h>
21 #include <typescript/types/signature.h>
22 #include <ir/astDump.h>
23 #include <ir/ts/tsTypeParameter.h>
24 #include <ir/ts/tsTypeParameterDeclaration.h>
25
26 namespace panda::es2panda::ir {
27
Iterate(const NodeTraverser & cb) const28 void TSConstructorType::Iterate(const NodeTraverser &cb) const
29 {
30 if (typeParams_) {
31 cb(typeParams_);
32 }
33
34 for (auto *it : params_) {
35 cb(it);
36 }
37
38 cb(returnType_);
39 }
40
Dump(ir::AstDumper * dumper) const41 void TSConstructorType::Dump(ir::AstDumper *dumper) const
42 {
43 dumper->Add({{"type", "TSConstructorType"},
44 {"params", params_},
45 {"typeParameters", AstDumper::Optional(typeParams_)},
46 {"returnType", returnType_},
47 {"abstract", AstDumper::Optional(abstract_)}});
48 }
49
Compile(compiler::PandaGen * pg) const50 void TSConstructorType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
51
Check(checker::Checker * checker) const52 checker::Type *TSConstructorType::Check(checker::Checker *checker) const
53 {
54 checker::ScopeContext scopeCtx(checker, scope_);
55
56 auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator());
57 checker->CheckFunctionParameterDeclarations(params_, signatureInfo);
58 returnType_->Check(checker);
59 auto *constructSignature =
60 checker->Allocator()->New<checker::Signature>(signatureInfo, returnType_->AsTypeNode()->GetType(checker));
61
62 return checker->CreateConstructorTypeWithSignature(constructSignature);
63 }
64
GetType(checker::Checker * checker) const65 checker::Type *TSConstructorType::GetType(checker::Checker *checker) const
66 {
67 return checker->CheckTypeCached(this);
68 }
69
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)70 void TSConstructorType::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
71 {
72 auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder, scope_);
73
74 if (typeParams_) {
75 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
76 }
77
78 for (auto iter = params_.begin(); iter != params_.end(); iter++) {
79 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
80 }
81
82 returnType_ = std::get<ir::AstNode *>(cb(returnType_))->AsExpression();
83 }
84
85 } // namespace panda::es2panda::ir
86