#pragma once #include #include #include #include #include #include #include #include #include namespace vls::reflection { // Version of the provenance/value snapshot schema. Derived index and trace // renderers must reject an incompatible version rather than reinterpret it. inline constexpr std::uint32_t schema_version = 1; // Canonical, context-qualified semantic identity of one reflected or generated // entity. It is assigned by the extractor from compiler state; this library // never invents an identity from a source string. using EntityId = std::string; struct Range { std::string path; std::uint32_t line{}, character{}, endLine{}, endCharacter{}; auto operator<=>(const Range &) const = default; }; // What one link of a provenance chain represents. A spelling is an editable // token; expansion and splice sites are the generated constructs that produced // a token; a reflected declaration is the original entity behind a splice; a // synthesized entity has no editable spelling at all. enum class OriginKind { spelling, expansion, splice, reflected_declaration, synthesized, }; struct Origin { OriginKind kind{OriginKind::synthesized}; std::optional range; // absent when the compiler gave no location std::optional entity; // reflected or generated entity identity std::string note; // proposal/facility or instantiation context auto operator<=>(const Origin &) const = default; }; // Innermost origin first. A generated entity can stay navigable through the // chain while remaining non-editable: generated names without a reliable // editable origin are never automatically renameable. struct Provenance { std::vector origins; bool gap{}; // at least one link in the chain is unavailable std::string context; // snapshot/instantiation identity [[nodiscard]] bool navigable() const; // some origin carries a real range [[nodiscard]] bool editable() const; // complete chain rooted at a spelling auto operator<=>(const Provenance &) const = default; }; enum class ValueKind { integral, text, boolean, entity, opaque, // a real value the compiler cannot render; text is the reason unavailable, // the value could not be obtained; text is the reason }; struct Value { ValueKind kind{ValueKind::unavailable}; std::int64_t integral{}; bool boolean{}; std::string text; // text value, or the reason for opaque/unavailable std::string type; // canonical type spelling when the compiler provides it EntityId entity; // referenced entity when kind is entity auto operator<=>(const Value &) const = default; }; struct Member { std::string name; Value value; auto operator<=>(const Member &) const = default; }; // One real reflected or synthesized entity. Members may reference other // entities; expansion resolves those references lazily and boundedly. struct Entity { EntityId id; std::string name; std::string type; std::vector members; Provenance provenance; bool synthesized{}; auto operator<=>(const Entity &) const = default; }; struct Limits { std::size_t entities{4096}; std::size_t membersPerEntity{64}; std::size_t originsPerProvenance{16}; std::size_t nameBytes{256}; std::size_t typeBytes{1024}; std::size_t textBytes{4096}; std::size_t contextBytes{1024}; std::size_t snapshotBytes{8 * 1024 * 1024}; std::size_t expansionDepth{8}; std::size_t expansionValues{1024}; std::size_t capabilities{256}; }; // Flattened, depth-first expansion of one entity. Cycles and limit cuts are // explicit paths, never silently omitted values. struct Expansion { struct Entry { std::string path; // member path from the root, e.g. "outer.inner" std::size_t depth{}; ValueKind kind{ValueKind::unavailable}; std::string type; std::string text; EntityId entity; std::optional link; // source range of the referenced entity auto operator<=>(const Entry &) const = default; }; std::vector entries; std::vector cycles; // repeated entity on the current path std::vector truncated; // cut by depth or value count [[nodiscard]] bool complete() const { return cycles.empty() && truncated.empty(); } }; // One explanation line of a virtual generated view. A line is a semantic // explanation, never compilable text: it may be indented prose with a source // link, and consumers must never feed it back to a compiler. struct VirtualLine { std::string text; std::size_t depth{}; std::optional link; auto operator<=>(const VirtualLine &) const = default; }; struct VirtualView { std::vector lines; bool truncated{}; // Virtual generated views are read-only explanations by construction. [[nodiscard]] bool compilable() const { return false; } }; // LSP-shaped hover for one entity: a semantic explanation plus the source // links it explains. The markup is Markdown but never compilable text. struct Hover { std::string markup; std::optional range; auto operator<=>(const Hover &) const = default; }; // Navigation targets in provenance-chain order, deduplicated and bounded. // A generated entity stays navigable through the chain that produced it. [[nodiscard]] std::vector locations(const Provenance &, std::size_t limit = 16); // Renders one entity and its bounded expansion as a read-only semantic // explanation with source links. Cycles, truncation and opaque values stay // explicit; the result is never compilable text. [[nodiscard]] VirtualView render(const Entity &, const Expansion &, const Limits & = Limits{}); // LSP-shaped hover markup for one entity and its expansion, with the entity's // first source range. The markup lists the virtual view and its source links. [[nodiscard]] Hover hover(const Entity &, const Expansion &, const Limits & = Limits{}); // Bounded snapshot of one analysis generation. Serialization is versioned and // every consumer validates its own limits before trusting the bytes. class Snapshot { public: explicit Snapshot(Limits = Limits{}); // Rejects duplicates, oversized strings, too many entities or members. [[nodiscard]] bool add(Entity); [[nodiscard]] bool contains(const EntityId &) const; [[nodiscard]] const Entity *find(const EntityId &) const; [[nodiscard]] const std::vector &entities() const noexcept; [[nodiscard]] std::size_t size() const; [[nodiscard]] const Limits &limits() const noexcept; // Depth-first expansion of one entity. Unknown identities are an error. [[nodiscard]] std::expected expand(const EntityId &, std::optional depth = std::nullopt) const; [[nodiscard]] std::expected serialize() const; [[nodiscard]] static std::expected parse(std::string_view, Limits = Limits{}); private: Limits limits_; std::vector entities_; }; // Proposal/facility support declared by the accepted backend. An unknown entry // is an explicit capability error; it never falls back to a lexical answer. struct Capability { std::string proposal; // e.g. "P2996" std::string facility; // e.g. "splices" bool supported{}; std::string reason; // required when unsupported auto operator<=>(const Capability &) const = default; }; class Capabilities { public: explicit Capabilities(Limits = Limits{}); // Rejects duplicates, oversized fields and an unsupported entry without a // reason. [[nodiscard]] bool add(Capability); [[nodiscard]] std::expected query(std::string_view proposal, std::string_view facility) const; [[nodiscard]] std::size_t size() const; private: Limits limits_; std::vector entries_; }; // One splice origin reported by the compiler: the splice site and the entity // it designates. Columns are 1-based UTF-8 byte offsets. The compiler reports // a start coordinate only, so ranges are zero-width until an end is available. struct SpliceOrigin { std::string source; std::uint32_t line{}, byteColumn{}; std::string targetKind; // "declaration", "type" or "value" std::string targetName; std::string targetSource; std::optional targetLine, targetByteColumn; }; // Composes entities from compiler-reported splice origins. Identity is // composed from the reported target kind, name and declaration location; the // provenance chain is the splice site followed by the reflected declaration, // or an explicit gap when the compiler reported no target location. The first // chain for an identity wins and the result stays within the snapshot limits. [[nodiscard]] Snapshot snapshot_from_splices(std::span, Limits = Limits{}); } // namespace vls::reflection