• 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 "tsFunctionType.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 TSFunctionType::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 TSFunctionType::Dump(ir::AstDumper *dumper) const
42 {
43     dumper->Add({{"type", "TSFunctionType"},
44                  {"params", params_},
45                  {"typeParameters", AstDumper::Optional(typeParams_)},
46                  {"returnType", returnType_}});
47 }
48 
Compile(compiler::PandaGen * pg) const49 void TSFunctionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
50 
Check(checker::Checker * checker) const51 checker::Type *TSFunctionType::Check(checker::Checker *checker) const
52 {
53     checker::ScopeContext scopeCtx(checker, scope_);
54 
55     auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator());
56     checker->CheckFunctionParameterDeclarations(params_, signatureInfo);
57     returnType_->Check(checker);
58     auto *callSignature =
59         checker->Allocator()->New<checker::Signature>(signatureInfo, returnType_->AsTypeNode()->GetType(checker));
60 
61     return checker->CreateFunctionTypeWithSignature(callSignature);
62 }
63 
GetType(checker::Checker * checker) const64 checker::Type *TSFunctionType::GetType(checker::Checker *checker) const
65 {
66     return checker->CheckTypeCached(this);
67 }
68 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)69 void TSFunctionType::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
70 {
71     auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder, scope_);
72 
73     if (typeParams_) {
74         typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
75     }
76 
77     for (auto iter = params_.begin(); iter != params_.end(); iter++) {
78         *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
79     }
80 
81     returnType_ = std::get<ir::AstNode *>(cb(returnType_))->AsExpression();
82 }
83 
84 }  // namespace panda::es2panda::ir
85