13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 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 "recordTable.h" 173af6ab5fSopenharmony_ci#include "parser/program/program.h" 183af6ab5fSopenharmony_ci#include "varbinder/ETSBinder.h" 193af6ab5fSopenharmony_ci#include "ir/base/classDefinition.h" 203af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h" 213af6ab5fSopenharmony_ci#include "ir/ts/tsEnumDeclaration.h" 223af6ab5fSopenharmony_ci#include "ir/ts/tsInterfaceDeclaration.h" 233af6ab5fSopenharmony_ci#include "checker/types/ets/etsObjectType.h" 243af6ab5fSopenharmony_ci#include "generated/signatures.h" 253af6ab5fSopenharmony_ci 263af6ab5fSopenharmony_cinamespace ark::es2panda::varbinder { 273af6ab5fSopenharmony_ciBoundContext::BoundContext(RecordTable *recordTable, ir::ClassDefinition *classDef, bool force) 283af6ab5fSopenharmony_ci : prev_(recordTable->boundCtx_), 293af6ab5fSopenharmony_ci recordTable_(recordTable), 303af6ab5fSopenharmony_ci currentRecord_(classDef), 313af6ab5fSopenharmony_ci savedRecord_(recordTable->record_) 323af6ab5fSopenharmony_ci{ 333af6ab5fSopenharmony_ci if (classDef == nullptr || (!force && !recordTable_->classDefinitions_.insert(classDef).second)) { 343af6ab5fSopenharmony_ci return; 353af6ab5fSopenharmony_ci } 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci recordTable_->boundCtx_ = this; 383af6ab5fSopenharmony_ci recordTable_->record_ = classDef; 393af6ab5fSopenharmony_ci recordIdent_ = classDef->Ident(); 403af6ab5fSopenharmony_ci if (classDef->InternalName().Empty()) { 413af6ab5fSopenharmony_ci classDef->SetInternalName(FormRecordName()); 423af6ab5fSopenharmony_ci } 433af6ab5fSopenharmony_ci} 443af6ab5fSopenharmony_ci 453af6ab5fSopenharmony_ciBoundContext::BoundContext(RecordTable *recordTable, ir::TSInterfaceDeclaration *interfaceDecl, bool force) 463af6ab5fSopenharmony_ci : prev_(recordTable->boundCtx_), 473af6ab5fSopenharmony_ci recordTable_(recordTable), 483af6ab5fSopenharmony_ci currentRecord_(interfaceDecl), 493af6ab5fSopenharmony_ci savedRecord_(recordTable->record_) 503af6ab5fSopenharmony_ci{ 513af6ab5fSopenharmony_ci if (interfaceDecl == nullptr || (!force && !recordTable_->interfaceDeclarations_.insert(interfaceDecl).second)) { 523af6ab5fSopenharmony_ci return; 533af6ab5fSopenharmony_ci } 543af6ab5fSopenharmony_ci 553af6ab5fSopenharmony_ci recordTable_->boundCtx_ = this; 563af6ab5fSopenharmony_ci recordTable_->record_ = interfaceDecl; 573af6ab5fSopenharmony_ci recordIdent_ = interfaceDecl->Id(); 583af6ab5fSopenharmony_ci if (interfaceDecl->InternalName() == "") { 593af6ab5fSopenharmony_ci interfaceDecl->SetInternalName(FormRecordName()); 603af6ab5fSopenharmony_ci } 613af6ab5fSopenharmony_ci} 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_ciBoundContext::~BoundContext() 643af6ab5fSopenharmony_ci{ 653af6ab5fSopenharmony_ci recordTable_->record_ = savedRecord_; 663af6ab5fSopenharmony_ci recordTable_->boundCtx_ = prev_; 673af6ab5fSopenharmony_ci} 683af6ab5fSopenharmony_ci 693af6ab5fSopenharmony_ciutil::StringView BoundContext::FormRecordName() const 703af6ab5fSopenharmony_ci{ 713af6ab5fSopenharmony_ci if (prev_ == nullptr) { 723af6ab5fSopenharmony_ci if (recordTable_->program_->OmitModuleName()) { 733af6ab5fSopenharmony_ci return recordIdent_->Name(); 743af6ab5fSopenharmony_ci } 753af6ab5fSopenharmony_ci 763af6ab5fSopenharmony_ci const auto &moduleName = recordTable_->program_->ModuleName(); 773af6ab5fSopenharmony_ci 783af6ab5fSopenharmony_ci // concatenate the module name with the record ident 793af6ab5fSopenharmony_ci return util::UString(moduleName.Mutf8() + compiler::Signatures::METHOD_SEPARATOR.data() + 803af6ab5fSopenharmony_ci recordIdent_->Name().Mutf8(), 813af6ab5fSopenharmony_ci recordTable_->program_->Allocator()) 823af6ab5fSopenharmony_ci .View(); 833af6ab5fSopenharmony_ci } 843af6ab5fSopenharmony_ci 853af6ab5fSopenharmony_ci util::UString recordName(recordTable_->program_->Allocator()); 863af6ab5fSopenharmony_ci recordName.Append(prev_->FormRecordName()); 873af6ab5fSopenharmony_ci recordName.Append(compiler::Signatures::METHOD_SEPARATOR); 883af6ab5fSopenharmony_ci if (std::holds_alternative<ir::ClassDefinition *>(currentRecord_)) { 893af6ab5fSopenharmony_ci const auto *classDef = std::get<ir::ClassDefinition *>(currentRecord_); 903af6ab5fSopenharmony_ci if (classDef->IsLocal()) { 913af6ab5fSopenharmony_ci recordName.Append(classDef->LocalPrefix()); 923af6ab5fSopenharmony_ci } 933af6ab5fSopenharmony_ci } 943af6ab5fSopenharmony_ci 953af6ab5fSopenharmony_ci recordName.Append(recordIdent_->Name()); 963af6ab5fSopenharmony_ci return recordName.View(); 973af6ab5fSopenharmony_ci} 983af6ab5fSopenharmony_ci 993af6ab5fSopenharmony_ciutil::StringView RecordTable::RecordName() const 1003af6ab5fSopenharmony_ci{ 1013af6ab5fSopenharmony_ci if (std::holds_alternative<ir::ClassDefinition *>(record_)) { 1023af6ab5fSopenharmony_ci return std::get<ir::ClassDefinition *>(record_)->InternalName(); 1033af6ab5fSopenharmony_ci } 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_ci ASSERT(std::holds_alternative<ir::TSInterfaceDeclaration *>(record_)); 1063af6ab5fSopenharmony_ci return std::get<ir::TSInterfaceDeclaration *>(record_)->InternalName(); 1073af6ab5fSopenharmony_ci} 1083af6ab5fSopenharmony_ci 1093af6ab5fSopenharmony_ci} // namespace ark::es2panda::varbinder 110