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 "moduleRecordEmitter.h"
17
18 namespace panda::es2panda::compiler {
GenModuleRequests()19 void ModuleRecordEmitter::GenModuleRequests()
20 {
21 ASSERT(moduleRecord_ != nullptr);
22 auto &moduleRequests = moduleRecord_->GetModuleRequests();
23 panda::pandasm::LiteralArray::Literal moduleSize = {
24 .tag_ = panda::panda_file::LiteralTag::INTEGER, .value_ = static_cast<uint32_t>(moduleRequests.size())};
25 buffer_.emplace_back(moduleSize);
26 for (auto request : moduleRequests) {
27 panda::pandasm::LiteralArray::Literal moduleRequest = {
28 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = request.Mutf8()};
29 buffer_.emplace_back(moduleRequest);
30 }
31 }
32
GenRegularImportEntries()33 void ModuleRecordEmitter::GenRegularImportEntries()
34 {
35 ASSERT(moduleRecord_ != nullptr);
36 auto ®ularImportEntries = moduleRecord_->GetRegularImportEntries();
37 panda::pandasm::LiteralArray::Literal entrySize = {
38 .tag_ = panda::panda_file::LiteralTag::INTEGER,
39 .value_ = static_cast<uint32_t>(regularImportEntries.size())};
40 buffer_.emplace_back(entrySize);
41 for (auto it = regularImportEntries.begin(); it != regularImportEntries.end(); ++it) {
42 auto *entry = it->second;
43 panda::pandasm::LiteralArray::Literal localName = {
44 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->localName_.Mutf8()};
45 buffer_.emplace_back(localName);
46 panda::pandasm::LiteralArray::Literal importName = {
47 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->importName_.Mutf8()};
48 buffer_.emplace_back(importName);
49 panda::pandasm::LiteralArray::Literal moduleRequest = {
50 .tag_ = panda::panda_file::LiteralTag::METHODAFFILIATE,
51 .value_ = static_cast<uint16_t>(entry->moduleRequestIdx_)};
52 buffer_.emplace_back(moduleRequest);
53 }
54 }
55
GenNamespaceImportEntries()56 void ModuleRecordEmitter::GenNamespaceImportEntries()
57 {
58 ASSERT(moduleRecord_ != nullptr);
59 auto &namespaceImportEntries = moduleRecord_->GetNamespaceImportEntries();
60 panda::pandasm::LiteralArray::Literal entrySize = {
61 .tag_ = panda::panda_file::LiteralTag::INTEGER,
62 .value_ = static_cast<uint32_t>(namespaceImportEntries.size())};
63 buffer_.emplace_back(entrySize);
64 for (const auto *entry : namespaceImportEntries) {
65 panda::pandasm::LiteralArray::Literal localName = {
66 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->localName_.Mutf8()};
67 buffer_.emplace_back(localName);
68 panda::pandasm::LiteralArray::Literal moduleRequest = {
69 .tag_ = panda::panda_file::LiteralTag::METHODAFFILIATE,
70 .value_ = static_cast<uint16_t>(entry->moduleRequestIdx_)};
71 buffer_.emplace_back(moduleRequest);
72 }
73 }
74
GenLocalExportEntries()75 void ModuleRecordEmitter::GenLocalExportEntries()
76 {
77 ASSERT(moduleRecord_ != nullptr);
78 auto &localExportEntries = moduleRecord_->GetLocalExportEntries();
79 panda::pandasm::LiteralArray::Literal entrySize = {
80 .tag_ = panda::panda_file::LiteralTag::INTEGER, .value_ = static_cast<uint32_t>(localExportEntries.size())};
81 buffer_.emplace_back(entrySize);
82 for (auto it = localExportEntries.begin(); it != localExportEntries.end(); ++it) {
83 auto *entry = it->second;
84 panda::pandasm::LiteralArray::Literal localName = {
85 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->localName_.Mutf8()};
86 buffer_.emplace_back(localName);
87 panda::pandasm::LiteralArray::Literal exportName = {
88 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->exportName_.Mutf8()};
89 buffer_.emplace_back(exportName);
90 }
91 }
92
GenIndirectExportEntries()93 void ModuleRecordEmitter::GenIndirectExportEntries()
94 {
95 ASSERT(moduleRecord_ != nullptr);
96 auto &indirectExportEntries = moduleRecord_->GetIndirectExportEntries();
97 panda::pandasm::LiteralArray::Literal entrySize = {
98 .tag_ = panda::panda_file::LiteralTag::INTEGER, .value_ = static_cast<uint32_t>(indirectExportEntries.size())};
99 buffer_.emplace_back(entrySize);
100 for (const auto *entry : indirectExportEntries) {
101 panda::pandasm::LiteralArray::Literal exportName = {
102 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->exportName_.Mutf8()};
103 buffer_.emplace_back(exportName);
104 panda::pandasm::LiteralArray::Literal importName = {
105 .tag_ = panda::panda_file::LiteralTag::STRING, .value_ = entry->importName_.Mutf8()};
106 buffer_.emplace_back(importName);
107 panda::pandasm::LiteralArray::Literal moduleRequest = {
108 .tag_ = panda::panda_file::LiteralTag::METHODAFFILIATE,
109 .value_ = static_cast<uint16_t>(entry->moduleRequestIdx_)};
110 buffer_.emplace_back(moduleRequest);
111 }
112 }
113
GenStarExportEntries()114 void ModuleRecordEmitter::GenStarExportEntries()
115 {
116 ASSERT(moduleRecord_ != nullptr);
117 auto &starExportEntries = moduleRecord_->GetStarExportEntries();
118 panda::pandasm::LiteralArray::Literal entrySize = {
119 .tag_ = panda::panda_file::LiteralTag::INTEGER, .value_ = static_cast<uint32_t>(starExportEntries.size())};
120 buffer_.emplace_back(entrySize);
121 for (const auto *entry : starExportEntries) {
122 panda::pandasm::LiteralArray::Literal moduleRequest = {
123 .tag_ = panda::panda_file::LiteralTag::METHODAFFILIATE,
124 .value_ = static_cast<uint16_t>(entry->moduleRequestIdx_)};
125 buffer_.emplace_back(moduleRequest);
126 }
127 }
128
Generate()129 void ModuleRecordEmitter::Generate()
130 {
131 GenModuleRequests();
132 GenRegularImportEntries();
133 GenNamespaceImportEntries();
134 GenLocalExportEntries();
135 GenIndirectExportEntries();
136 GenStarExportEntries();
137 }
138 } // namespace panda::es2panda::compiler
139