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 "recordTable.h"
17 #include "parser/program/program.h"
18 #include "varbinder/ETSBinder.h"
19 #include "ir/base/classDefinition.h"
20 #include "ir/expressions/identifier.h"
21 #include "ir/ts/tsEnumDeclaration.h"
22 #include "ir/ts/tsInterfaceDeclaration.h"
23 #include "generated/signatures.h"
24
25 namespace panda::es2panda::varbinder {
BoundContext(RecordTable * recordTable,ir::ClassDefinition * classDef)26 BoundContext::BoundContext(RecordTable *recordTable, ir::ClassDefinition *classDef)
27 : prev_(recordTable->boundCtx_), recordTable_(recordTable), savedRecord_(recordTable->record_)
28 {
29 if (classDef == nullptr || !recordTable_->classDefinitions_.insert(classDef).second) {
30 return;
31 }
32
33 recordTable_->boundCtx_ = this;
34 recordTable_->record_ = classDef;
35 recordIdent_ = classDef->Ident();
36 classDef->SetInternalName(FormRecordName());
37 }
38
BoundContext(RecordTable * recordTable,ir::TSInterfaceDeclaration * interfaceDecl)39 BoundContext::BoundContext(RecordTable *recordTable, ir::TSInterfaceDeclaration *interfaceDecl)
40 : prev_(recordTable->boundCtx_), recordTable_(recordTable), savedRecord_(recordTable->record_)
41 {
42 if (interfaceDecl == nullptr || !recordTable_->interfaceDeclarations_.insert(interfaceDecl).second) {
43 return;
44 }
45
46 recordTable_->boundCtx_ = this;
47 recordTable_->record_ = interfaceDecl;
48 recordIdent_ = interfaceDecl->Id();
49 interfaceDecl->SetInternalName(FormRecordName());
50 }
51
~BoundContext()52 BoundContext::~BoundContext()
53 {
54 recordTable_->record_ = savedRecord_;
55 recordTable_->boundCtx_ = prev_;
56 }
57
FormRecordName() const58 util::StringView BoundContext::FormRecordName() const
59 {
60 const auto &packageName = recordTable_->program_->GetPackageName();
61 if (prev_ == nullptr) {
62 if (packageName.Empty()) {
63 return recordIdent_->Name();
64 }
65
66 util::UString recordName(recordTable_->program_->Allocator());
67 recordName.Append(packageName);
68 recordName.Append(compiler::Signatures::METHOD_SEPARATOR);
69 recordName.Append(recordIdent_->Name());
70 return recordName.View();
71 }
72
73 util::UString recordName(recordTable_->program_->Allocator());
74 recordName.Append(prev_->FormRecordName());
75 recordName.Append(compiler::Signatures::METHOD_SEPARATOR);
76 recordName.Append(recordIdent_->Name());
77 return recordName.View();
78 }
79
RecordName() const80 util::StringView RecordTable::RecordName() const
81 {
82 if (std::holds_alternative<ir::ClassDefinition *>(record_)) {
83 return std::get<ir::ClassDefinition *>(record_)->InternalName();
84 }
85
86 ASSERT(std::holds_alternative<ir::TSInterfaceDeclaration *>(record_));
87 return std::get<ir::TSInterfaceDeclaration *>(record_)->InternalName();
88 }
89
90 } // namespace panda::es2panda::varbinder
91