Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Module_Resolver.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_RESOLVER_H
43#define COMPILER_MODULE_RESOLVER_H
44
45#include <sstream>
46#include <string>
47
48#include <ah-source.H>
49#include <tpl_dynArray.H>
50
51namespace Aleph {
58
65
72
84
92
95 const Compiler_Module_Resolution_Issue_Kind kind) noexcept
96{
97 switch (kind)
98 {
100 return "Missing_Dependency";
102 return "Cyclic_Dependency";
103 }
104
105 return "Unknown";
106}
107
109inline std::string compiler_render_module_order(const DynArray<size_t> &order,
111{
112 std::ostringstream out;
113 out << "ImportOrder\n";
114 for (size_t i = 0; i < order.size(); ++i)
115 {
116 const auto index = order.access(i);
117 const auto name = index < modules.size() ? modules.access(index).name : "<invalid-module>";
118 out << " [" << i << "] " << name << '\n';
119 }
120 return out.str();
121}
122
123namespace Compiler_Module_Resolver_Detail {
125 const std::string &name) noexcept
126{
127 for (size_t i = 0; i < modules.size(); ++i)
128 if (modules.access(i).name == name)
129 return i;
130 return static_cast<size_t>(-1);
131}
132
134 const size_t index,
137{
138 const auto state = states.access(index);
139 if (state == 2)
140 return true;
141 if (state == 1 or state == 3)
142 return false;
143
144 states.access(index) = 1;
145 const auto &module = modules.access(index);
146 for (size_t i = 0; i < module.imports.size(); ++i)
147 {
148 const auto &dep = module.imports.access(i);
149 const auto dep_index = find_module_index(modules, dep.module_name);
150 if (dep_index == static_cast<size_t>(-1))
151 {
152 result.valid = false;
154 index,
155 static_cast<size_t>(-1),
156 module.name,
157 dep.module_name,
158 dep.span});
159 continue;
160 }
161
162 if (dep_index == index)
163 {
164 result.valid = false;
166 index,
167 dep_index,
168 module.name,
169 dep.module_name,
170 dep.span});
171 continue;
172 }
173
174 if (states.access(dep_index) == 1)
175 {
176 result.valid = false;
178 index,
179 dep_index,
180 module.name,
181 dep.module_name,
182 dep.span});
183 states.access(index) = 3;
184 return false;
185 }
186
187 if (states.access(dep_index) == 0 and not resolve_visit(modules, dep_index, states, result))
188 {
189 states.access(index) = 3;
190 return false;
191 }
192 }
193
194 states.access(index) = 2;
195 result.order.append(index);
196 return true;
197}
198} // namespace Compiler_Module_Resolver_Detail
199
208{
211 for (size_t i = 0; i < modules.size(); ++i)
212 states.append(0);
213
214 for (size_t i = 0; i < modules.size(); ++i)
215 if (states.access(i) == 0)
216 Compiler_Module_Resolver_Detail::resolve_visit(modules, i, states, result);
217
218 return result;
219}
220} // namespace Aleph
221
222#endif
Source file and span management utilities for compiler-style tooling.
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
size_t find_module_index(const DynArray< Compiler_Module_Descriptor > &modules, const std::string &name) noexcept
bool resolve_visit(const DynArray< Compiler_Module_Descriptor > &modules, const size_t index, DynArray< unsigned char > &states, Compiler_Module_Resolution_Result &result)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
std::string compiler_render_module_order(const DynArray< size_t > &order, const DynArray< Compiler_Module_Descriptor > &modules)
Renders one deterministic text artifact for a module order.
Compiler_Module_Resolution_Result compiler_resolve_module_order(const DynArray< Compiler_Module_Descriptor > &modules)
Resolves one dependency-first order for exact-name module imports.
const char * compiler_module_resolution_issue_name(const Compiler_Module_Resolution_Issue_Kind kind) noexcept
Returns the stable debug name of one module-resolution issue kind.
Compiler_Module_Resolution_Issue_Kind
Structural issue kinds produced by module dependency resolution.
One source-name dependency edge originating from one module.
std::string module_name
Exact imported source name.
Source_Span span
Source location where the import was declared.
One module descriptor consumed by the reusable resolver.
DynArray< Compiler_Module_Dependency > imports
Ordered dependency list.
std::string name
Stable module/source name.
One structured issue emitted by the reusable module resolver.
Source_Span span
Source location of the problematic import.
std::string dependency_name
Missing or cyclic dependency name.
Compiler_Module_Resolution_Issue_Kind kind
std::string module_name
Stable importer name.
size_t module_index
Module whose import triggered the issue.
size_t dependency_index
Known dependency index when available.
Resolution result for one set of source-name module descriptors.
DynArray< size_t > order
Stable dependency-first module order.
bool valid
true when no missing dependencies or cycles were found.
DynArray< Compiler_Module_Resolution_Issue > issues
Structured issues, if any.
Half-open byte range inside a source file.
Definition ah-source.H:100
Lazy and scalable dynamic array implementation.