Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Module_Semantic_Environment.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
42#ifndef COMPILER_MODULE_SEMANTIC_ENVIRONMENT_H
43#define COMPILER_MODULE_SEMANTIC_ENVIRONMENT_H
44
45#include <sstream>
46#include <string>
47#include <string_view>
48
50
51namespace Aleph {
63
69
70namespace Compiler_Module_Semantic_Environment_Detail {
71inline void append_unique_name(DynArray<std::string> &names, const std::string &name)
72{
73 for (size_t i = 0; i < names.size(); ++i)
74 if (names.access(i) == name)
75 return;
76 names.append(name);
77}
78} // namespace Compiler_Module_Semantic_Environment_Detail
79
86 const Compiler_Module_Name_Table &table)
87{
89
90 for (size_t i = 0; i < table.modules.size(); ++i)
91 {
92 const auto &source = table.modules.access(i);
94 module.module_name = source.module_name;
95 module.module_index = source.module_index;
96 module.order_index = source.order_index;
97
98 for (size_t j = 0; j < source.names.size(); ++j)
99 {
100 const auto &item = source.names.access(j);
101 if (item.kind == Compiler_Module_Export_Kind::Type)
102 module.types.append(item);
103 else
104 module.values.append(item);
105 }
106
107 for (size_t j = 0; j < source.ambiguous_names.size(); ++j)
108 {
109 const auto &item = source.ambiguous_names.access(j);
110 if (item.namespace_kind == Compiler_Module_Export_Kind::Type)
112 item.name);
113 else
115 item.name);
116 }
117
118 env.modules.append(std::move(module));
119 }
120
121 return env;
122}
123
126 const Compiler_Module_Semantic_Environment &env, const std::string_view module_name) noexcept
127{
128 for (size_t i = 0; i < env.modules.size(); ++i)
129 if (env.modules.access(i).module_name == module_name)
130 return &env.modules.access(i);
131 return nullptr;
132}
133
137 const std::string_view module_name,
138 const std::string_view symbol_name) noexcept
139{
140 const auto *module = compiler_find_module_semantic_environment(env, module_name);
141 if (module == nullptr)
142 return nullptr;
143
144 for (size_t i = 0; i < module->values.size(); ++i)
145 if (module->values.access(i).name == symbol_name)
146 return &module->values.access(i);
147 return nullptr;
148}
149
153 const std::string_view module_name,
154 const std::string_view symbol_name) noexcept
155{
156 const auto *module = compiler_find_module_semantic_environment(env, module_name);
157 if (module == nullptr)
158 return nullptr;
159
160 for (size_t i = 0; i < module->types.size(); ++i)
161 if (module->types.access(i).name == symbol_name)
162 return &module->types.access(i);
163 return nullptr;
164}
165
169{
170 std::ostringstream out;
171 out << "ModuleSemanticEnvironment\n";
172 for (size_t i = 0; i < env.modules.size(); ++i)
173 {
174 const auto &module = env.modules.access(i);
175 out << " Module(" << module.module_name << ") order=" << module.order_index << '\n';
176 for (size_t j = 0; j < module.values.size(); ++j)
177 {
178 const auto &item = module.values.access(j);
179 out << " Value(" << compiler_module_export_kind_name(item.kind) << "(" << item.name
180 << ")) -> " << item.provider_module_name << ' '
181 << (item.imported ? "imported" : "local") << '\n';
182 }
183 for (size_t j = 0; j < module.types.size(); ++j)
184 {
185 const auto &item = module.types.access(j);
186 out << " Type(" << item.name << ") -> " << item.provider_module_name << ' '
187 << (item.imported ? "imported" : "local") << '\n';
188 }
189 for (size_t j = 0; j < module.ambiguous_values.size(); ++j)
190 out << " AmbiguousValue(" << module.ambiguous_values.access(j) << ")\n";
191 for (size_t j = 0; j < module.ambiguous_types.size(); ++j)
192 out << " AmbiguousType(" << module.ambiguous_types.access(j) << ")\n";
193 }
194 return out.str();
195}
196} // namespace Aleph
197
198#endif
Reusable per-module cache for top-level name resolution.
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
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
void append_unique_name(DynArray< std::string > &names, const std::string &name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Compiler_Module_Semantic_Environment compiler_build_module_semantic_environment(const Compiler_Module_Name_Table &table)
Builds one namespace-separated semantic environment from a name table.
std::string compiler_render_module_semantic_environment(const Compiler_Module_Semantic_Environment &env)
Renders one deterministic text artifact for semantic environments.
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 Compiler_Module_Bound_Name * compiler_lookup_module_value(const Compiler_Module_Semantic_Environment &env, const std::string_view module_name, const std::string_view symbol_name) noexcept
Looks up one visible value-namespace binding in a module environment.
const Compiler_Module_Bound_Name * compiler_lookup_module_type(const Compiler_Module_Semantic_Environment &env, const std::string_view module_name, const std::string_view symbol_name) noexcept
Looks up one visible type-namespace binding in a module environment.
const Compiler_Module_Semantic_Module * compiler_find_module_semantic_environment(const Compiler_Module_Semantic_Environment &env, const std::string_view module_name) noexcept
Finds one semantic environment by module name.
One top-level name successfully bound in a module namespace.
Cached top-level name table over all visible modules.
DynArray< Compiler_Module_Name_Table_Entry > modules
Semantic environments for all modules in one compilation unit set.
DynArray< Compiler_Module_Semantic_Module > modules
One namespace-separated semantic environment for a module.