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