-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler_Module_Name_Table.H
More file actions
169 lines (145 loc) · 5.93 KB
/
Compiler_Module_Name_Table.H
File metadata and controls
169 lines (145 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Aleph_w
Data structures & Algorithms
version 2.0.0b
https://github.com/lrleon/Aleph-w
This file is part of Aleph-w library
Copyright (c) 2002-2026 Leandro Rabindranath Leon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/** @file Compiler_Module_Name_Table.H
* @brief Reusable per-module cache for top-level name resolution.
*
* This header precomputes one deterministic name table from
* `Compiler_Module_Binding_Result`. The resulting structure is suitable for
* repeated semantic queries without rescanning the full binding result and its
* issues every time.
*
* @ingroup Utilities
*/
#ifndef COMPILER_MODULE_NAME_TABLE_H
#define COMPILER_MODULE_NAME_TABLE_H
#include <sstream>
#include <string>
#include <Compiler_Module_Binder.H>
namespace Aleph {
/** @brief One ambiguous name tracked in a per-module name table. */
struct Compiler_Module_Ambiguous_Name
{
std::string name;
Compiler_Module_Export_Kind namespace_kind = Compiler_Module_Export_Kind::Global;
};
/** @brief Cached top-level names for one module. */
struct Compiler_Module_Name_Table_Entry
{
std::string module_name;
size_t module_index = static_cast<size_t>(-1);
size_t order_index = static_cast<size_t>(-1);
DynArray<Compiler_Module_Bound_Name> names;
DynArray<Compiler_Module_Ambiguous_Name> ambiguous_names;
};
/** @brief Cached top-level name table over all visible modules. */
struct Compiler_Module_Name_Table
{
DynArray<Compiler_Module_Name_Table_Entry> modules;
};
namespace Compiler_Module_Name_Table_Detail {
inline Compiler_Module_Export_Kind issue_namespace_kind(const Compiler_Module_Binding_Issue_Kind kind) noexcept
{
return kind == Compiler_Module_Binding_Issue_Kind::Ambiguous_Type_Name
? Compiler_Module_Export_Kind::Type
: Compiler_Module_Export_Kind::Global;
}
} // namespace Compiler_Module_Name_Table_Detail
/** @brief Builds one reusable per-module name table from bindings.
*
* @param bindings Bound module surfaces.
* @return Deterministic cache for repeated top-level name queries.
*/
inline Compiler_Module_Name_Table compiler_build_module_name_table(
const Compiler_Module_Binding_Result &bindings)
{
Compiler_Module_Name_Table table;
for (size_t i = 0; i < bindings.modules.size(); ++i)
{
const auto &source = bindings.modules.access(i);
Compiler_Module_Name_Table_Entry entry;
entry.module_name = source.module_name;
entry.module_index = source.module_index;
entry.order_index = source.order_index;
for (size_t j = 0; j < source.names.size(); ++j)
entry.names.append(source.names.access(j));
for (size_t j = 0; j < bindings.issues.size(); ++j)
{
const auto &issue = bindings.issues.access(j);
if (issue.module_name != source.module_name)
continue;
if (issue.kind != Compiler_Module_Binding_Issue_Kind::Ambiguous_Value_Name
and issue.kind != Compiler_Module_Binding_Issue_Kind::Ambiguous_Type_Name)
continue;
bool duplicate = false;
for (size_t k = 0; k < entry.ambiguous_names.size(); ++k)
{
const auto ¤t = entry.ambiguous_names.access(k);
if (current.name == issue.symbol_name
and current.namespace_kind
== Compiler_Module_Name_Table_Detail::issue_namespace_kind(issue.kind))
{
duplicate = true;
break;
}
}
if (duplicate)
continue;
entry.ambiguous_names.append(
{issue.symbol_name, Compiler_Module_Name_Table_Detail::issue_namespace_kind(issue.kind)});
}
table.modules.append(std::move(entry));
}
return table;
}
/** @brief Renders one deterministic text artifact for cached module names.
*
* @param table Cached table to render.
* @return Stable plain-text representation of the per-module name cache.
*/
inline std::string compiler_render_module_name_table(const Compiler_Module_Name_Table &table)
{
std::ostringstream out;
out << "ModuleNameTable\n";
for (size_t i = 0; i < table.modules.size(); ++i)
{
const auto &module = table.modules.access(i);
out << " Module(" << module.module_name << ") order=" << module.order_index << '\n';
for (size_t j = 0; j < module.names.size(); ++j)
{
const auto &item = module.names.access(j);
out << " Name(" << compiler_module_export_kind_name(item.kind) << "(" << item.name
<< ")) -> " << item.provider_module_name << ' '
<< (item.imported ? "imported" : "local") << '\n';
}
for (size_t j = 0; j < module.ambiguous_names.size(); ++j)
{
const auto &item = module.ambiguous_names.access(j);
out << " Ambiguous(" << compiler_module_export_kind_name(item.namespace_kind) << "("
<< item.name << "))\n";
}
}
return out.str();
}
} // namespace Aleph
#endif