• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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_COMPILER_TYPESCRIPT_TYPES_SIGNATURE_H
17 #define ES2PANDA_COMPILER_TYPESCRIPT_TYPES_SIGNATURE_H
18 
19 #include "type.h"
20 
21 #include <binder/variable.h>
22 
23 namespace panda::es2panda::binder {
24 class LocalVariable;
25 }  // namespace panda::es2panda::binder
26 
27 namespace panda::es2panda::checker {
28 
29 class SignatureInfo {
30 public:
SignatureInfo(ArenaAllocator * allocator)31     explicit SignatureInfo(ArenaAllocator *allocator) : params(allocator->Adapter()) {}
32 
SignatureInfo(const SignatureInfo * other,ArenaAllocator * allocator)33     SignatureInfo(const SignatureInfo *other, ArenaAllocator *allocator) : params(allocator->Adapter())
34     {
35         for (auto *it : other->params) {
36             params.push_back(it->Copy(allocator, it->Declaration()));
37         }
38 
39         minArgCount = other->minArgCount;
40 
41         if (other->restVar) {
42             restVar = other->restVar->Copy(allocator, other->restVar->Declaration());
43         }
44     }
45 
46     ~SignatureInfo() = default;
47     NO_COPY_SEMANTIC(SignatureInfo);
48     NO_MOVE_SEMANTIC(SignatureInfo);
49 
50     uint32_t minArgCount {};
51     binder::LocalVariable *restVar {};
52     ArenaVector<binder::LocalVariable *> params;
53 };
54 
55 class Signature {
56 public:
Signature(SignatureInfo * signatureInfo,Type * returnType)57     Signature(SignatureInfo *signatureInfo, Type *returnType) : signatureInfo_(signatureInfo), returnType_(returnType)
58     {
59     }
60 
61     ~Signature() = default;
62     NO_COPY_SEMANTIC(Signature);
63     NO_MOVE_SEMANTIC(Signature);
64 
GetSignatureInfo()65     const SignatureInfo *GetSignatureInfo() const
66     {
67         return signatureInfo_;
68     }
69 
Params()70     const ArenaVector<binder::LocalVariable *> &Params() const
71     {
72         return signatureInfo_->params;
73     }
74 
ReturnType()75     const Type *ReturnType() const
76     {
77         return returnType_;
78     }
79 
ReturnType()80     Type *ReturnType()
81     {
82         return returnType_;
83     }
84 
MinArgCount()85     uint32_t MinArgCount() const
86     {
87         return signatureInfo_->minArgCount;
88     }
89 
OptionalArgCount()90     uint32_t OptionalArgCount() const
91     {
92         return signatureInfo_->params.size() - signatureInfo_->minArgCount;
93     }
94 
SetReturnType(Type * type)95     void SetReturnType(Type *type)
96     {
97         returnType_ = type;
98     }
99 
SetNode(const ir::AstNode * node)100     void SetNode(const ir::AstNode *node)
101     {
102         node_ = node;
103     }
104 
Node()105     const ir::AstNode *Node() const
106     {
107         return node_;
108     }
109 
RestVar()110     const binder::LocalVariable *RestVar() const
111     {
112         return signatureInfo_->restVar;
113     }
114 
115     Signature *Copy(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes);
116 
117     void ToString(std::stringstream &ss, const binder::Variable *variable, bool printAsMethod = false) const;
118     void Identical(TypeRelation *relation, Signature *other);
119     void AssignmentTarget(TypeRelation *relation, Signature *source);
120 
121 private:
122     checker::SignatureInfo *signatureInfo_;
123     Type *returnType_;
124     const ir::AstNode *node_ {nullptr};
125 };
126 
127 }  // namespace panda::es2panda::checker
128 
129 #endif /* TYPESCRIPT_TYPES_SIGNATURE_H */
130