• 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 "tsInterfaceDeclaration.h"
17 
18 #include <typescript/checker.h>
19 #include <ir/astDump.h>
20 #include <ir/expressions/identifier.h>
21 #include <ir/ts/tsInterfaceBody.h>
22 #include <ir/ts/tsInterfaceHeritage.h>
23 #include <ir/ts/tsTypeParameterDeclaration.h>
24 
25 namespace panda::es2panda::ir {
26 
Iterate(const NodeTraverser & cb) const27 void TSInterfaceDeclaration::Iterate(const NodeTraverser &cb) const
28 {
29     cb(id_);
30 
31     if (typeParams_) {
32         cb(typeParams_);
33     }
34 
35     for (auto *it : extends_) {
36         cb(it);
37     }
38 
39     cb(body_);
40 }
41 
Dump(ir::AstDumper * dumper) const42 void TSInterfaceDeclaration::Dump(ir::AstDumper *dumper) const
43 {
44     dumper->Add({{"type", "TSInterfaceDeclaration"},
45                  {"body", body_},
46                  {"id", id_},
47                  {"extends", extends_},
48                  {"typeParameters", AstDumper::Optional(typeParams_)}});
49 }
50 
Compile(compiler::PandaGen * pg) const51 void TSInterfaceDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
52 
CheckInheritedPropertiesAreIdentical(checker::Checker * checker,checker::InterfaceType * type,const lexer::SourcePosition & locInfo)53 void CheckInheritedPropertiesAreIdentical(checker::Checker *checker, checker::InterfaceType *type,
54                                           const lexer::SourcePosition &locInfo)
55 {
56     checker->GetBaseTypes(type);
57 
58     size_t constexpr BASE_SIZE_LIMIT = 2;
59     if (type->Bases().size() < BASE_SIZE_LIMIT) {
60         return;
61     }
62 
63     checker->ResolveDeclaredMembers(type);
64 
65     checker::InterfacePropertyMap properties;
66 
67     for (auto *it : type->Properties()) {
68         properties.insert({it->Name(), {it, type}});
69     }
70 
71     for (auto *base : type->Bases()) {
72         checker->ResolveStructuredTypeMembers(base);
73         ArenaVector<binder::LocalVariable *> inheritedProperties(checker->Allocator()->Adapter());
74         base->AsInterfaceType()->CollectProperties(&inheritedProperties);
75 
76         for (auto *inheritedProp : inheritedProperties) {
77             auto res = properties.find(inheritedProp->Name());
78             if (res == properties.end()) {
79                 properties.insert({inheritedProp->Name(), {inheritedProp, base->AsInterfaceType()}});
80             } else if (res->second.second != type) {
81                 checker::Type *sourceType = checker->GetTypeOfVariable(inheritedProp);
82                 checker::Type *targetType = checker->GetTypeOfVariable(res->second.first);
83                 checker->IsTypeIdenticalTo(sourceType, targetType,
84                                            {"Interface '", type, "' cannot simultaneously extend types '",
85                                             res->second.second, "' and '", base->AsInterfaceType(), "'."},
86                                            locInfo);
87             }
88         }
89     }
90 }
91 
Check(checker::Checker * checker) const92 checker::Type *TSInterfaceDeclaration::Check(checker::Checker *checker) const
93 {
94     binder::Variable *var = id_->Variable();
95     ASSERT(var->Declaration()->Node() && var->Declaration()->Node()->IsTSInterfaceDeclaration());
96 
97     if (this == var->Declaration()->Node()) {
98         checker::Type *resolvedType = var->TsType();
99 
100         if (!resolvedType) {
101             checker::ObjectDescriptor *desc =
102                 checker->Allocator()->New<checker::ObjectDescriptor>(checker->Allocator());
103             resolvedType = checker->Allocator()->New<checker::InterfaceType>(checker->Allocator(), id_->Name(), desc);
104             CHECK_NOT_NULL(resolvedType);
105             resolvedType->SetVariable(var);
106             var->SetTsType(resolvedType);
107         }
108 
109         checker::InterfaceType *resolvedInterface = resolvedType->AsObjectType()->AsInterfaceType();
110         CheckInheritedPropertiesAreIdentical(checker, resolvedInterface, id_->Start());
111 
112         for (auto *base : resolvedInterface->Bases()) {
113             checker->IsTypeAssignableTo(resolvedInterface, base,
114                                         {"Interface '", id_->Name(), "' incorrectly extends interface '", base, "'"},
115                                         id_->Start());
116         }
117 
118         checker->CheckIndexConstraints(resolvedInterface);
119     }
120 
121     body_->Check(checker);
122 
123     return nullptr;
124 }
125 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)126 void TSInterfaceDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
127 {
128     id_ = std::get<ir::AstNode *>(cb(id_))->AsIdentifier();
129 
130     if (typeParams_) {
131         typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
132     }
133 
134     for (auto iter = extends_.begin(); iter != extends_.end(); iter++) {
135         *iter = std::get<ir::AstNode *>(cb(*iter))->AsTSInterfaceHeritage();
136     }
137 
138     body_ = std::get<ir::AstNode *>(cb(body_))->AsTSInterfaceBody();
139 }
140 
141 }  // namespace panda::es2panda::ir
142