Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Module_Binder.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
43#ifndef COMPILER_MODULE_BINDER_H
44#define COMPILER_MODULE_BINDER_H
45
46#include <sstream>
47#include <string>
48
49#include <tpl_dynMapTree.H>
51
52namespace Aleph {
63
66{
67 std::string module_name;
68 size_t module_index = static_cast<size_t>(-1);
69 size_t order_index = static_cast<size_t>(-1);
71};
72
80
93
101
104{
105 switch (kind)
106 {
108 return "Missing_Metadata";
110 return "Ambiguous_Value_Name";
112 return "Ambiguous_Type_Name";
113 }
114
115 return "Unknown";
116}
117
118namespace Compiler_Module_Binder_Detail {
120{
121 return kind == Compiler_Module_Export_Kind::Type ? 1 : 0;
122}
123
124
126{
128 translated.module_index = issue.module_index;
129 translated.module_name = issue.module_name;
130 translated.span = issue.span;
131
132 switch (issue.kind)
133 {
136 translated.referenced_module_name = issue.second_provider_module;
137 break;
140 translated.symbol_name = issue.export_name;
141 translated.first_provider_module = issue.first_provider_module;
142 translated.second_provider_module = issue.second_provider_module;
143 break;
146 translated.symbol_name = issue.export_name;
147 translated.first_provider_module = issue.first_provider_module;
148 translated.second_provider_module = issue.second_provider_module;
149 break;
150 }
151
152 return translated;
153}
154} // namespace Compiler_Module_Binder_Detail
155
167{
169 result.valid = linkage.valid;
170
171 for (size_t i = 0; i < linkage.issues.size(); ++i)
172 result.issues.append(Compiler_Module_Binder_Detail::translate_issue(linkage.issues.access(i)));
173
174 for (size_t i = 0; i < linkage.modules.size(); ++i)
175 {
176 const auto &linked_module = linkage.modules.access(i);
178 bindings.module_name = linked_module.module_name;
179 bindings.module_index = linked_module.module_index;
180 bindings.order_index = linked_module.order_index;
181
182 // Precompute conflict set: keys with more than one distinct provider
183 // in the same namespace bucket are ambiguous. O(N log N) single pass.
184 DynMapTree<std::string, size_t> first_provider; // key -> source_module_index of first
186 for (size_t j = 0; j < linked_module.visible_exports.size(); ++j)
187 {
188 const auto &exp = linked_module.visible_exports.access(j);
189 const std::string key =
191 + ":" + exp.name;
192 const auto *prev = first_provider.search(key);
193 if (prev == nullptr)
194 {
195 first_provider[key] = exp.source_module_index;
196 }
197 else if (prev->second != exp.source_module_index)
198 {
199 ambiguous_keys[key] = true;
200 }
201 }
202
203 // Binding pass: skip ambiguous names; deduplicate with a bound-names map.
205 for (size_t j = 0; j < linked_module.visible_exports.size(); ++j)
206 {
207 const auto &item = linked_module.visible_exports.access(j);
208 const std::string key =
210 + ":" + item.name;
211 if (ambiguous_keys.search(key) != nullptr)
212 continue;
213 if (bound_keys.search(key) != nullptr)
214 continue;
215 bound_keys[key] = true;
216 bindings.names.append({item.kind,
217 item.name,
218 item.source_module_index,
219 item.source_module_name,
220 item.declaration_span,
221 item.imported});
222 }
223
224 result.modules.append(std::move(bindings));
225 }
226
227 return result;
228}
229
236{
237 std::ostringstream out;
238 out << "ModuleBindings\n";
239 for (size_t i = 0; i < result.modules.size(); ++i)
240 {
241 const auto &module = result.modules.access(i);
242 out << " Module(" << module.module_name << ") order=" << module.order_index << '\n';
243 for (size_t j = 0; j < module.names.size(); ++j)
244 {
245 const auto &item = module.names.access(j);
246 out << " Bind(" << compiler_module_export_kind_name(item.kind) << "(" << item.name
247 << ")) -> " << item.provider_module_name << ' '
248 << (item.imported ? "imported" : "local");
249 if (item.declaration_span.is_valid())
250 out << " @" << item.declaration_span.to_string();
251 out << '\n';
252 }
253 }
254
255 for (size_t i = 0; i < result.issues.size(); ++i)
256 {
257 const auto &issue = result.issues.access(i);
258 out << " Issue(" << compiler_module_binding_issue_name(issue.kind)
259 << ") module=" << issue.module_name;
260 if (not issue.symbol_name.empty())
261 out << " name=" << issue.symbol_name;
262 if (not issue.referenced_module_name.empty())
263 out << " dependency=" << issue.referenced_module_name;
264 if (not issue.first_provider_module.empty())
265 out << " first=" << issue.first_provider_module;
266 if (not issue.second_provider_module.empty())
267 out << " second=" << issue.second_provider_module;
268 if (issue.span.is_valid())
269 out << " @" << issue.span.to_string();
270 out << '\n';
271 }
272
273 return out.str();
274}
275} // namespace Aleph
276
277#endif
Reusable linking helpers for module surfaces and direct imports.
size_t size_t int32_t * out
Definition ca-c-api.h:120
Generic key-value map implemented on top of a binary search tree.
Key & access(size_t i)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4077
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
int export_namespace_bucket(const Compiler_Module_Export_Kind kind) noexcept
Compiler_Module_Binding_Issue translate_issue(const Compiler_Module_Linkage_Issue &issue)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Compiler_Module_Export_Kind
Export kinds currently modeled by reusable module metadata.
Compiler_Module_Binding_Issue_Kind
Structural issues emitted while binding linked module surfaces.
const char * compiler_module_export_kind_name(const Compiler_Module_Export_Kind kind) noexcept
Returns the stable debug name for one module-export kind.
const char * compiler_module_binding_issue_name(const Compiler_Module_Binding_Issue_Kind kind) noexcept
Returns the stable debug name for one module-binding issue kind.
std::string compiler_render_module_bindings(const Compiler_Module_Binding_Result &result)
Renders one deterministic text artifact for bound module names.
Compiler_Module_Binding_Result compiler_bind_module_surfaces(const Compiler_Module_Link_Result &linkage)
Binds linked module surfaces into one deterministic name view.
One structural issue reported by the reusable module binder.
Compiler_Module_Binding_Issue_Kind kind
Binding result over one linked module-surface view.
DynArray< Compiler_Module_Bindings > modules
DynArray< Compiler_Module_Binding_Issue > issues
Bound top-level names visible from one module.
DynArray< Compiler_Module_Bound_Name > names
One top-level name successfully bound in a module namespace.
bool imported
false for local bindings, true for direct-imported bindings.
One structural issue reported by the reusable module linker.
Half-open byte range inside a source file.
Definition ah-source.H:100
Dynamic key-value map based on balanced binary search trees.