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 29namespace panda::es2panda::ir { 30 31void 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 46void 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 55void TSInterfaceDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} 56 57void 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 96checker::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 CHECK_NOT_NULL(resolvedType); 109 resolvedType->SetVariable(var); 110 var->SetTsType(resolvedType); 111 } 112 113 checker::InterfaceType *resolvedInterface = resolvedType->AsObjectType()->AsInterfaceType(); 114 CheckInheritedPropertiesAreIdentical(checker, resolvedInterface, id_->Start()); 115 116 for (auto *base : resolvedInterface->Bases()) { 117 checker->IsTypeAssignableTo(resolvedInterface, base, 118 {"Interface '", id_->Name(), "' incorrectly extends interface '", base, "'"}, 119 id_->Start()); 120 } 121 122 checker->CheckIndexConstraints(resolvedInterface); 123 } 124 125 body_->Check(checker); 126 127 return nullptr; 128} 129 130void TSInterfaceDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) 131{ 132 id_ = std::get<ir::AstNode *>(cb(id_))->AsIdentifier(); 133 134 if (typeParams_) { 135 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration(); 136 } 137 138 for (auto iter = extends_.begin(); iter != extends_.end(); iter++) { 139 *iter = std::get<ir::AstNode *>(cb(*iter))->AsTSInterfaceHeritage(); 140 } 141 142 body_ = std::get<ir::AstNode *>(cb(body_))->AsTSInterfaceBody(); 143} 144 145} // namespace panda::es2panda::ir 146