13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "tsInterfaceDeclaration.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include <binder/declaration.h>
193af6ab5fSopenharmony_ci#include <binder/scope.h>
203af6ab5fSopenharmony_ci#include <binder/variable.h>
213af6ab5fSopenharmony_ci#include <typescript/checker.h>
223af6ab5fSopenharmony_ci#include <ir/astDump.h>
233af6ab5fSopenharmony_ci#include <ir/expressions/identifier.h>
243af6ab5fSopenharmony_ci#include <ir/ts/tsInterfaceBody.h>
253af6ab5fSopenharmony_ci#include <ir/ts/tsInterfaceHeritage.h>
263af6ab5fSopenharmony_ci#include <ir/ts/tsTypeParameter.h>
273af6ab5fSopenharmony_ci#include <ir/ts/tsTypeParameterDeclaration.h>
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_cinamespace panda::es2panda::ir {
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_civoid TSInterfaceDeclaration::Iterate(const NodeTraverser &cb) const
323af6ab5fSopenharmony_ci{
333af6ab5fSopenharmony_ci    cb(id_);
343af6ab5fSopenharmony_ci
353af6ab5fSopenharmony_ci    if (typeParams_) {
363af6ab5fSopenharmony_ci        cb(typeParams_);
373af6ab5fSopenharmony_ci    }
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci    for (auto *it : extends_) {
403af6ab5fSopenharmony_ci        cb(it);
413af6ab5fSopenharmony_ci    }
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_ci    cb(body_);
443af6ab5fSopenharmony_ci}
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_civoid TSInterfaceDeclaration::Dump(ir::AstDumper *dumper) const
473af6ab5fSopenharmony_ci{
483af6ab5fSopenharmony_ci    dumper->Add({{"type", "TSInterfaceDeclaration"},
493af6ab5fSopenharmony_ci                 {"body", body_},
503af6ab5fSopenharmony_ci                 {"id", id_},
513af6ab5fSopenharmony_ci                 {"extends", extends_},
523af6ab5fSopenharmony_ci                 {"typeParameters", AstDumper::Optional(typeParams_)}});
533af6ab5fSopenharmony_ci}
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_civoid TSInterfaceDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
563af6ab5fSopenharmony_ci
573af6ab5fSopenharmony_civoid CheckInheritedPropertiesAreIdentical(checker::Checker *checker, checker::InterfaceType *type,
583af6ab5fSopenharmony_ci                                          const lexer::SourcePosition &locInfo)
593af6ab5fSopenharmony_ci{
603af6ab5fSopenharmony_ci    checker->GetBaseTypes(type);
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ci    size_t constexpr BASE_SIZE_LIMIT = 2;
633af6ab5fSopenharmony_ci    if (type->Bases().size() < BASE_SIZE_LIMIT) {
643af6ab5fSopenharmony_ci        return;
653af6ab5fSopenharmony_ci    }
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_ci    checker->ResolveDeclaredMembers(type);
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ci    checker::InterfacePropertyMap properties;
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_ci    for (auto *it : type->Properties()) {
723af6ab5fSopenharmony_ci        properties.insert({it->Name(), {it, type}});
733af6ab5fSopenharmony_ci    }
743af6ab5fSopenharmony_ci
753af6ab5fSopenharmony_ci    for (auto *base : type->Bases()) {
763af6ab5fSopenharmony_ci        checker->ResolveStructuredTypeMembers(base);
773af6ab5fSopenharmony_ci        ArenaVector<binder::LocalVariable *> inheritedProperties(checker->Allocator()->Adapter());
783af6ab5fSopenharmony_ci        base->AsInterfaceType()->CollectProperties(&inheritedProperties);
793af6ab5fSopenharmony_ci
803af6ab5fSopenharmony_ci        for (auto *inheritedProp : inheritedProperties) {
813af6ab5fSopenharmony_ci            auto res = properties.find(inheritedProp->Name());
823af6ab5fSopenharmony_ci            if (res == properties.end()) {
833af6ab5fSopenharmony_ci                properties.insert({inheritedProp->Name(), {inheritedProp, base->AsInterfaceType()}});
843af6ab5fSopenharmony_ci            } else if (res->second.second != type) {
853af6ab5fSopenharmony_ci                checker::Type *sourceType = checker->GetTypeOfVariable(inheritedProp);
863af6ab5fSopenharmony_ci                checker::Type *targetType = checker->GetTypeOfVariable(res->second.first);
873af6ab5fSopenharmony_ci                checker->IsTypeIdenticalTo(sourceType, targetType,
883af6ab5fSopenharmony_ci                                           {"Interface '", type, "' cannot simultaneously extend types '",
893af6ab5fSopenharmony_ci                                            res->second.second, "' and '", base->AsInterfaceType(), "'."},
903af6ab5fSopenharmony_ci                                           locInfo);
913af6ab5fSopenharmony_ci            }
923af6ab5fSopenharmony_ci        }
933af6ab5fSopenharmony_ci    }
943af6ab5fSopenharmony_ci}
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_cichecker::Type *TSInterfaceDeclaration::Check(checker::Checker *checker) const
973af6ab5fSopenharmony_ci{
983af6ab5fSopenharmony_ci    binder::Variable *var = id_->Variable();
993af6ab5fSopenharmony_ci    ASSERT(var->Declaration()->Node() && var->Declaration()->Node()->IsTSInterfaceDeclaration());
1003af6ab5fSopenharmony_ci
1013af6ab5fSopenharmony_ci    if (this == var->Declaration()->Node()) {
1023af6ab5fSopenharmony_ci        checker::Type *resolvedType = var->TsType();
1033af6ab5fSopenharmony_ci
1043af6ab5fSopenharmony_ci        if (!resolvedType) {
1053af6ab5fSopenharmony_ci            checker::ObjectDescriptor *desc =
1063af6ab5fSopenharmony_ci                checker->Allocator()->New<checker::ObjectDescriptor>(checker->Allocator());
1073af6ab5fSopenharmony_ci            resolvedType = checker->Allocator()->New<checker::InterfaceType>(checker->Allocator(), id_->Name(), desc);
1083af6ab5fSopenharmony_ci            CHECK_NOT_NULL(resolvedType);
1093af6ab5fSopenharmony_ci            resolvedType->SetVariable(var);
1103af6ab5fSopenharmony_ci            var->SetTsType(resolvedType);
1113af6ab5fSopenharmony_ci        }
1123af6ab5fSopenharmony_ci
1133af6ab5fSopenharmony_ci        checker::InterfaceType *resolvedInterface = resolvedType->AsObjectType()->AsInterfaceType();
1143af6ab5fSopenharmony_ci        CheckInheritedPropertiesAreIdentical(checker, resolvedInterface, id_->Start());
1153af6ab5fSopenharmony_ci
1163af6ab5fSopenharmony_ci        for (auto *base : resolvedInterface->Bases()) {
1173af6ab5fSopenharmony_ci            checker->IsTypeAssignableTo(resolvedInterface, base,
1183af6ab5fSopenharmony_ci                                        {"Interface '", id_->Name(), "' incorrectly extends interface '", base, "'"},
1193af6ab5fSopenharmony_ci                                        id_->Start());
1203af6ab5fSopenharmony_ci        }
1213af6ab5fSopenharmony_ci
1223af6ab5fSopenharmony_ci        checker->CheckIndexConstraints(resolvedInterface);
1233af6ab5fSopenharmony_ci    }
1243af6ab5fSopenharmony_ci
1253af6ab5fSopenharmony_ci    body_->Check(checker);
1263af6ab5fSopenharmony_ci
1273af6ab5fSopenharmony_ci    return nullptr;
1283af6ab5fSopenharmony_ci}
1293af6ab5fSopenharmony_ci
1303af6ab5fSopenharmony_civoid TSInterfaceDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
1313af6ab5fSopenharmony_ci{
1323af6ab5fSopenharmony_ci    id_ = std::get<ir::AstNode *>(cb(id_))->AsIdentifier();
1333af6ab5fSopenharmony_ci
1343af6ab5fSopenharmony_ci    if (typeParams_) {
1353af6ab5fSopenharmony_ci        typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
1363af6ab5fSopenharmony_ci    }
1373af6ab5fSopenharmony_ci
1383af6ab5fSopenharmony_ci    for (auto iter = extends_.begin(); iter != extends_.end(); iter++) {
1393af6ab5fSopenharmony_ci        *iter = std::get<ir::AstNode *>(cb(*iter))->AsTSInterfaceHeritage();
1403af6ab5fSopenharmony_ci    }
1413af6ab5fSopenharmony_ci
1423af6ab5fSopenharmony_ci    body_ = std::get<ir::AstNode *>(cb(body_))->AsTSInterfaceBody();
1433af6ab5fSopenharmony_ci}
1443af6ab5fSopenharmony_ci
1453af6ab5fSopenharmony_ci}  // namespace panda::es2panda::ir
146