Files
Language-Server/include/vls/modules/ModuleGraph.hpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

228 lines
8.1 KiB
C++

#pragma once
#include <Void/LanguageServer/Build/Model.hpp>
#include <compare>
#include <cstddef>
#include <expected>
#include <filesystem>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace vls::modules {
namespace Build = Void::LanguageServer::Build;
inline constexpr std::size_t max_scan_bytes = 256 * 1024, max_scan_depth = 16;
inline constexpr std::size_t max_field_bytes = 4096, max_rule_items = 512;
inline constexpr std::size_t max_units = 4096, max_graph_items = 32768;
inline constexpr std::size_t max_graph_bytes = 16 * 1024 * 1024;
struct ExpectedOutput {
std::filesystem::path working_directory;
std::string spelling;
bool operator==(const ExpectedOutput &other) const {
return working_directory.native() == other.working_directory.native() &&
spelling == other.spelling;
}
};
enum class LookupMethod { by_name, include_angle, include_quote };
struct ModuleDescription {
std::string logical_name;
std::optional<std::string> source_path, compiled_module_path;
bool unique_on_source_path{};
auto operator<=>(const ModuleDescription &) const = default;
};
struct ProvidedModule : ModuleDescription {
bool is_interface{true};
auto operator<=>(const ProvidedModule &) const = default;
};
struct RequiredModule : ModuleDescription {
LookupMethod lookup{LookupMethod::by_name};
auto operator<=>(const RequiredModule &) const = default;
};
enum class ScanError {
invalid_json,
invalid_shape,
unsupported_version,
unsupported_field,
invalid_invocation,
wrong_directory,
wrong_output,
limit
};
// GCC 16.2 single-rule version 0, revision 0. Only the decoder constructs rules.
class ScanRule {
public:
const ExpectedOutput &output() const { return output_; }
const auto &additional_outputs() const { return additional_outputs_; }
const auto &provides() const { return provides_; }
const auto &requirements() const { return requirements_; }
std::size_t wire_bytes() const { return wire_bytes_; }
private:
friend std::expected<ScanRule, ScanError> decode_scan(std::string_view, const ExpectedOutput &);
ScanRule() = default;
ExpectedOutput output_;
std::vector<std::string> additional_outputs_;
std::vector<ProvidedModule> provides_;
std::vector<RequiredModule> requirements_;
std::size_t wire_bytes_{};
};
using ScanResult = std::expected<ScanRule, ScanError>;
[[nodiscard]] ScanResult decode_scan(std::string_view bytes, const ExpectedOutput &expected);
// The input owner admits this compatible set. Compatibility identifies frontend, target ABI,
// stdlib, flags/environment affecting module interchange. Lookup covers all ordered search
// roots, including misses, shadowing, overlays and generated inputs.
struct ModuleDomain {
Build::Generation generation;
std::string compatibility, lookup;
bool operator==(const ModuleDomain &) const = default;
};
// Collision-checked identities for the exact invocation, owning source and immutable transitive
// inputs. textual_closure includes header contents behind staged imports and their relevant
// artifact generations. A user-only depfile additionally needs independently sealed system inputs.
struct UnitInputs {
std::string command, source, textual_closure;
std::optional<std::string> system_closure;
bool operator==(const UnitInputs &) const = default;
};
// Provenance from staging an immutable header CMI. The scanner separately identifies the
// generation it actually consumed. source_identity names the resolved header across spellings.
struct StagedHeader {
std::string generation, source_identity;
ModuleDomain domain;
Build::ContextKey producer;
UnitInputs inputs;
bool operator==(const StagedHeader &) const = default;
};
struct CensusMember {
Build::ContextKey context;
ExpectedOutput output;
UnitInputs inputs;
// Changes on scanner replacement, even for equal source/command or a formerly absent rule.
std::string scan_revision;
std::optional<StagedHeader> staged_header;
bool operator==(const CensusMember &) const = default;
};
struct ModuleCensus {
ModuleDomain domain;
std::string revision;
bool closed{};
// All eligible commands in this dependency domain. Other BuildGeneration alternatives
// remain invisible. Distinct visibility sets require distinct censuses.
std::vector<CensusMember> members;
};
enum class ScanMethod { unknown, preprocess_md, preprocess_mmd, missing_allowed };
enum class UnitKind {
unknown,
ordinary,
implementation,
primary_interface,
interface_partition,
internal_partition,
header_unit
};
struct HeaderProvision {
std::size_t index{}; // Index in ScanRule::provides().
std::string source_identity;
};
struct HeaderUse {
std::size_t index{}; // Index in ScanRule::requirements().
std::string source_identity;
std::optional<std::string> loaded_generation;
};
struct ScanEvidence {
ModuleDomain domain;
Build::ContextKey context;
UnitInputs inputs;
std::string scan_revision;
std::optional<int> exit_status;
std::optional<ScanResult> rule;
ScanMethod method{ScanMethod::unknown};
// The owner proves a complete pass under these inputs (including generated inputs) and
// classifies every header-unit provision from its actual invocation and resolved source.
bool complete{};
// Implementation and ordinary importer can have identical rules. Only independent frontend
// evidence can set one of these two kinds on an import-only TU.
UnitKind import_only_kind{UnitKind::unknown};
std::vector<HeaderProvision> provided_headers;
// Engaged empty means the owner positively established absence of header-unit imports.
// Bindings attest every reported lookup/source spelling to the resolved source identity.
std::optional<std::vector<HeaderUse>> header_uses;
// Verified writer identities in primary-output, then additional_outputs() order. An
// isolated invocation can bind the same spelling to a different private destination.
std::vector<std::string> writers;
};
enum class PlanIssue {
incomplete_census,
invalid_context,
wrong_domain,
duplicate_member,
missing_scan,
duplicate_scan,
scan_failed,
missing_rule,
invalid_rule,
stale_scan,
missing_source,
incomplete_inputs,
incomplete_scan,
unproven_mapping,
missing_provider,
duplicate_provider,
stale_header,
writer_collision,
cycle,
limit
};
struct Blocker {
PlanIssue code{};
std::optional<std::size_t> unit; // Index in ModulePlan::units, when available.
std::string detail;
std::optional<ScanError> scan_error;
auto operator<=>(const Blocker &) const = default;
};
struct PlannedUnit {
CensusMember member;
UnitKind kind{UnitKind::unknown};
// Indices in ModulePlan::units. These are direct consumers across the admitted census.
std::vector<std::size_t> consumers;
};
struct ModulePlan {
ModuleDomain domain;
std::string census_revision;
std::vector<PlannedUnit> units; // Ascending ContextKey order.
// Prerequisites first, requested TU last. Failure leaves the order empty.
std::vector<Build::ContextKey> order;
std::vector<Blocker> blockers;
// All eligible scans can establish provider claims. A complete census may still have
// missing imports, duplicate providers, conflicting writers or cycles. Unproven claims
// withhold unit kinds and reverse edges.
bool claims_complete{};
[[nodiscard]] bool ready() const {
return claims_complete && !order.empty() && blockers.empty();
}
// Also checks non-reachable members. A renamed or newly visible provider can change a
// previous successful or missing lookup without having had any positive reverse edge.
[[nodiscard]] bool matches_census(const ModuleCensus &current) const;
};
[[nodiscard]] ModulePlan build_module_plan(const ModuleCensus &census,
std::span<const ScanEvidence> scans,
const Build::ContextKey &requested);
} // namespace vls::modules