1 /**
2  * Copyright (c) 2021-2024 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 #ifndef ES2PANDA_PARSER_INCLUDE_AST_TS_SIGNATURE_DECLARATION_H
17 #define ES2PANDA_PARSER_INCLUDE_AST_TS_SIGNATURE_DECLARATION_H
18 
19 #include "ir/astNode.h"
20 #include "ir/base/scriptFunctionSignature.h"
21 #include "ir/typed.h"
22 
23 namespace ark::es2panda::checker {
24 class TSAnalyzer;
25 }  // namespace ark::es2panda::checker
26 
27 namespace ark::es2panda::ir {
28 class TSTypeParameterDeclaration;
29 
30 class TSSignatureDeclaration : public TypedAstNode {
31 public:
32     enum class TSSignatureDeclarationKind { CALL_SIGNATURE, CONSTRUCT_SIGNATURE };
33 
34     TSSignatureDeclaration() = delete;
35     ~TSSignatureDeclaration() override = default;
36 
37     NO_COPY_SEMANTIC(TSSignatureDeclaration);
38     NO_MOVE_SEMANTIC(TSSignatureDeclaration);
39 
TSSignatureDeclaration(TSSignatureDeclarationKind const kind, FunctionSignature &&signature)40     explicit TSSignatureDeclaration(TSSignatureDeclarationKind const kind, FunctionSignature &&signature)
41         : TypedAstNode(AstNodeType::TS_SIGNATURE_DECLARATION), kind_(kind), signature_(std::move(signature))
42     {
43     }
44     // NOTE (csabahurton): friend relationship can be removed once there are getters for private fields
45     friend class checker::TSAnalyzer;
46 
47     [[nodiscard]] bool IsScopeBearer() const noexcept override
48     {
49         return true;
50     }
51 
52     [[nodiscard]] varbinder::Scope *Scope() const noexcept override
53     {
54         return scope_;
55     }
56 
SetScope(varbinder::Scope *scope)57     void SetScope(varbinder::Scope *scope)
58     {
59         ASSERT(scope_ == nullptr);
60         scope_ = scope;
61     }
62 
63     void ClearScope() noexcept override
64     {
65         scope_ = nullptr;
66     }
67 
68     [[nodiscard]] const TSTypeParameterDeclaration *TypeParams() const noexcept
69     {
70         return signature_.TypeParams();
71     }
72 
TypeParams()73     [[nodiscard]] TSTypeParameterDeclaration *TypeParams()
74     {
75         return signature_.TypeParams();
76     }
77 
78     [[nodiscard]] const ArenaVector<Expression *> &Params() const noexcept
79     {
80         return signature_.Params();
81     }
82 
83     [[nodiscard]] const TypeNode *ReturnTypeAnnotation() const noexcept
84     {
85         return signature_.ReturnType();
86     }
87 
88     TypeNode *ReturnTypeAnnotation() noexcept
89     {
90         return signature_.ReturnType();
91     }
92 
93     [[nodiscard]] TSSignatureDeclarationKind Kind() const noexcept
94     {
95         return kind_;
96     }
97 
98     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
99     void Iterate(const NodeTraverser &cb) const override;
100 
101     void Dump(ir::AstDumper *dumper) const override;
102     void Dump(ir::SrcDumper *dumper) const override;
103     void Compile(compiler::PandaGen *pg) const override;
104     void Compile(compiler::ETSGen *etsg) const override;
105     checker::Type *Check(checker::TSChecker *checker) override;
106     checker::Type *Check(checker::ETSChecker *checker) override;
107 
108     void Accept(ASTVisitorT *v) override
109     {
110         v->Accept(this);
111     }
112 
113 private:
114     varbinder::Scope *scope_ {nullptr};
115     TSSignatureDeclarationKind kind_;
116     ir::FunctionSignature signature_;
117 };
118 }  // namespace ark::es2panda::ir
119 
120 #endif
121