• 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 "signature.h"
17 
18 namespace panda::es2panda::checker {
19 
Copy(ArenaAllocator * allocator,TypeRelation * relation,GlobalTypesHolder * globalTypes)20 Signature *Signature::Copy(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes)
21 {
22     checker::SignatureInfo *copiedInfo = allocator->New<checker::SignatureInfo>(signatureInfo_, allocator);
23 
24     for (auto *it : copiedInfo->params) {
25         it->SetTsType(it->TsType()->Instantiate(allocator, relation, globalTypes));
26     }
27 
28     Type *copiedReturnType = returnType_->Instantiate(allocator, relation, globalTypes);
29 
30     return allocator->New<Signature>(copiedInfo, copiedReturnType);
31 }
32 
ToString(std::stringstream & ss,const binder::Variable * variable,bool printAsMethod) const33 void Signature::ToString(std::stringstream &ss, const binder::Variable *variable, bool printAsMethod) const
34 {
35     ss << "(";
36 
37     for (auto it = signatureInfo_->params.begin(); it != signatureInfo_->params.end(); it++) {
38         ss << (*it)->Name();
39 
40         if ((*it)->HasFlag(binder::VariableFlags::OPTIONAL)) {
41             ss << "?";
42         }
43 
44         ss << ": ";
45 
46         (*it)->TsType()->ToString(ss);
47 
48         if (std::next(it) != signatureInfo_->params.end()) {
49             ss << ", ";
50         }
51     }
52 
53     if (signatureInfo_->restVar) {
54         if (!signatureInfo_->params.empty()) {
55             ss << ", ";
56         }
57 
58         ss << "...";
59         ss << signatureInfo_->restVar->Name();
60         ss << ": ";
61         signatureInfo_->restVar->TsType()->ToString(ss);
62         ss << "[]";
63     }
64 
65     ss << ")";
66 
67     if (printAsMethod || (variable && variable->HasFlag(binder::VariableFlags::METHOD))) {
68         ss << ": ";
69     } else {
70         ss << " => ";
71     }
72 
73     returnType_->ToString(ss);
74 }
75 
Identical(TypeRelation * relation,Signature * other)76 void Signature::Identical(TypeRelation *relation, Signature *other)
77 {
78     if (signatureInfo_->minArgCount != other->MinArgCount() ||
79         signatureInfo_->params.size() != other->Params().size()) {
80         relation->Result(false);
81         return;
82     }
83 
84     relation->IsIdenticalTo(returnType_, other->ReturnType());
85 
86     if (relation->IsTrue()) {
87         for (uint64_t i = 0; i < signatureInfo_->params.size(); i++) {
88             relation->IsIdenticalTo(signatureInfo_->params[i]->TsType(), other->Params()[i]->TsType());
89             if (!relation->IsTrue()) {
90                 return;
91             }
92         }
93 
94         if (signatureInfo_->restVar && other->RestVar()) {
95             relation->IsIdenticalTo(signatureInfo_->restVar->TsType(), other->RestVar()->TsType());
96         } else if ((signatureInfo_->restVar && !other->RestVar()) || (!signatureInfo_->restVar && other->RestVar())) {
97             relation->Result(false);
98         }
99     }
100 }
101 
AssignmentTarget(TypeRelation * relation,Signature * source)102 void Signature::AssignmentTarget(TypeRelation *relation, Signature *source)
103 {
104     if (!signatureInfo_->restVar &&
105         (source->Params().size() - source->OptionalArgCount()) > signatureInfo_->params.size()) {
106         relation->Result(false);
107         return;
108     }
109 
110     for (size_t i = 0; i < source->Params().size(); i++) {
111         if (!signatureInfo_->restVar && i >= Params().size()) {
112             break;
113         }
114 
115         if (signatureInfo_->restVar) {
116             relation->IsAssignableTo(source->Params()[i]->TsType(), signatureInfo_->restVar->TsType());
117 
118             if (!relation->IsTrue()) {
119                 return;
120             }
121 
122             continue;
123         }
124 
125         relation->IsAssignableTo(source->Params()[i]->TsType(), Params()[i]->TsType());
126 
127         if (!relation->IsTrue()) {
128             return;
129         }
130     }
131 
132     relation->IsAssignableTo(source->ReturnType(), returnType_);
133 
134     if (relation->IsTrue() && signatureInfo_->restVar && source->RestVar()) {
135         relation->IsAssignableTo(source->RestVar()->TsType(), signatureInfo_->restVar->TsType());
136     }
137 }
138 
139 }  // namespace panda::es2panda::checker
140