Skip to content

Commit 7e2a1e1

Browse files
committed
refine graph apis
1 parent 7a18085 commit 7e2a1e1

3 files changed

Lines changed: 192 additions & 110 deletions

File tree

crates/llmcc-core/src/block.rs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ declare_arena!(BlockArena {
3434
Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter, EnumString, FromRepr, Display, Default,
3535
)]
3636
#[strum(serialize_all = "snake_case")]
37+
/// Normalized block categories used by the language-agnostic graph.
38+
///
39+
/// These names are shared graph vocabulary, not a checklist every language must
40+
/// emit. A language should materialize only the kinds that fit its syntax and
41+
/// semantics, and can add a new kind when an existing category would be
42+
/// misleading. For example, `Class` is the current nominal/aggregate type bucket
43+
/// used by classes, structs, and records; `Trait` and `Interface` are two
44+
/// contract-like categories for languages that distinguish those concepts; and
45+
/// `Impl` models an implementation/extension/conformance block.
3746
pub enum BlockKind {
3847
#[default]
3948
Undefined,
@@ -354,49 +363,49 @@ pub enum BlockRelation {
354363
#[default]
355364
Unknown,
356365

357-
/// Parent contains child (Root→Func, Class→Method, etc.)
366+
/// Parent block contains a child block.
358367
Contains,
359-
/// Child is contained by parent
368+
/// Child block is contained by a parent block.
360369
ContainedBy,
361370

362-
/// Func/Method → Parameters block
371+
/// Callable block owns a parameter block.
363372
HasParameters,
364-
/// Func/Method → Return block
373+
/// Callable block owns a return block.
365374
HasReturn,
366-
/// Func/Method → Func/Method it calls
375+
/// Callable block calls another callable block.
367376
Calls,
368-
/// Func/Method is called by another Func/Method
377+
/// Callable block is called by another callable block.
369378
CalledBy,
370379

371-
/// Class/Enum → Field blocks
380+
/// Aggregate or nominal type owns a member field block.
372381
HasField,
373-
/// Field → Class/Enum that owns it
382+
/// Member field block belongs to an aggregate or nominal type.
374383
FieldOf,
375-
/// Field/Parameter/Return → Type definition (the type of this element)
384+
/// Typed block refers to the block that defines its type.
376385
TypeOf,
377-
/// Type definition → Field/Parameter/Return that uses this type
386+
/// Type-definition block is used as the type for another block.
378387
TypeFor,
379-
/// Impl → Type it implements for
388+
/// Implementation/extension block targets a type block.
380389
ImplFor,
381-
/// Type → Impl blocks for this type
390+
/// Type block has an implementation/extension block.
382391
HasImpl,
383-
/// Impl/Trait → Method blocks
392+
/// Container block owns a member callable block.
384393
HasMethod,
385-
/// Method → Impl/Trait/Class that owns it
394+
/// Member callable block belongs to a container block.
386395
MethodOf,
387-
/// Type → Trait it implements
396+
/// Type or implementation block conforms to a contract block.
388397
Implements,
389-
/// Trait → Types that implement it
398+
/// Contract block is implemented by a type or implementation block.
390399
ImplementedBy,
391400

392-
/// Uses a type/const/function
401+
/// Block uses another type, constant, callable, or contract block.
393402
Uses,
394-
/// Is used by
403+
/// Block is used by another block.
395404
UsedBy,
396405

397-
/// Trait/Interface extends another (TypeScript extends, Rust supertraits)
406+
/// Type or contract block specializes another type or contract block.
398407
Extends,
399-
/// Trait/Interface is extended by another
408+
/// Type or contract block is specialized by another block.
400409
ExtendedBy,
401410
}
402411

@@ -440,9 +449,12 @@ pub struct BlockBase<'blk> {
440449
/// Direct reference to the symbol that defines this block.
441450
/// Set during block building. Enables: block.symbol().type_of().block_id()
442451
pub symbol: Option<&'blk Symbol>,
443-
/// Types this block depends on (used for arch graph edges)
444-
/// For impl blocks: type arguments from trait reference (e.g., User in `impl Repository<User>`)
445-
/// For structs/enums: could include generic bounds or trait objects
452+
/// Type-like blocks this block depends on for architecture graph edges.
453+
///
454+
/// The collection is intentionally broad: languages use it for generic
455+
/// arguments, contract constraints, decorator/annotation symbols, and other
456+
/// type-shaped dependencies that should be rendered as graph edges but do
457+
/// not require their own dedicated relation variant.
446458
pub type_deps: RwLock<HashSet<BlockId>>,
447459
}
448460

@@ -946,8 +958,11 @@ pub struct BlockClass<'blk> {
946958
name: String,
947959
pub fields: RwLock<Vec<BlockId>>,
948960
pub methods: RwLock<Vec<BlockId>>,
949-
/// Extended class (for TypeScript/JavaScript class inheritance)
950-
/// A class can only extend one other class (single inheritance)
961+
/// Optional base type for languages with single primary inheritance.
962+
///
963+
/// Languages without this concept leave it empty. Languages with multiple
964+
/// base types should store additional generalization edges in graph
965+
/// relations rather than overloading this display field.
951966
pub extends: RwLock<Option<(String, Option<BlockId>)>>,
952967
}
953968

@@ -1019,12 +1034,12 @@ impl<'blk> BlockClass<'blk> {
10191034
self.methods.read().clone()
10201035
}
10211036

1022-
/// Set the extended class
1037+
/// Set the displayed base type for this nominal type block.
10231038
pub fn set_extends(&self, name: String, block_id: Option<BlockId>) {
10241039
*self.extends.write() = Some((name, block_id));
10251040
}
10261041

1027-
/// Get the extended class
1042+
/// Return the displayed base type for this nominal type block.
10281043
pub fn extends(&self) -> Option<(String, Option<BlockId>)> {
10291044
self.extends.read().clone()
10301045
}
@@ -1054,12 +1069,11 @@ impl<'blk> fmt::Display for BlockClass<'blk> {
10541069
}
10551070

10561071
impl<'blk> BlockClass<'blk> {
1057-
/// Format dependency entries as pseudo-children (to be rendered after real children)
1058-
/// Returns lines like "@tdep:3 Bar" for implemented interfaces
1072+
/// Format dependency entries as pseudo-children after real children.
10591073
pub fn dependency_labels(&self, unit: CompileUnit<'blk>) -> Vec<String> {
10601074
let mut deps = Vec::new();
10611075

1062-
// Add type_deps (includes implemented interfaces)
1076+
// Includes generic arguments, constraints, decorators, and contracts.
10631077
let type_deps = self.type_deps();
10641078
if !type_deps.is_empty() {
10651079
let mut sorted: Vec<_> = type_deps.iter().collect();
@@ -1131,14 +1145,14 @@ impl<'blk> fmt::Display for BlockTrait<'blk> {
11311145
}
11321146
}
11331147

1134-
/// Block representing a TypeScript interface declaration
1148+
/// Block representing a structural or declared contract body.
11351149
#[derive(Debug)]
11361150
pub struct BlockInterface<'blk> {
11371151
pub base: BlockBase<'blk>,
11381152
name: String,
11391153
pub methods: RwLock<Vec<BlockId>>,
11401154
pub fields: RwLock<Vec<BlockId>>,
1141-
/// Extended interfaces (for TypeScript extends)
1155+
/// Base contracts for languages with contract inheritance/refinement.
11421156
pub extends: RwLock<Vec<(String, Option<BlockId>)>>,
11431157
}
11441158

@@ -1198,7 +1212,7 @@ impl<'blk> BlockInterface<'blk> {
11981212
self.base.nested_types()
11991213
}
12001214

1201-
/// Add an extended interface
1215+
/// Add a displayed base contract.
12021216
pub fn add_extends(&self, name: String, block_id: Option<BlockId>) {
12031217
let mut extends = self.extends.write();
12041218
if !extends
@@ -1209,7 +1223,7 @@ impl<'blk> BlockInterface<'blk> {
12091223
}
12101224
}
12111225

1212-
/// Get extended interfaces
1226+
/// Return displayed base contracts.
12131227
pub fn extends(&self) -> Vec<(String, Option<BlockId>)> {
12141228
self.extends.read().clone()
12151229
}
@@ -1251,9 +1265,9 @@ pub struct BlockImpl<'blk> {
12511265
target: RwLock<Option<BlockId>>,
12521266
/// Target type symbol (for deferred block_id resolution)
12531267
target_sym: Option<&'blk Symbol>,
1254-
/// Trait block ID (resolved during link_blocks if needed)
1268+
/// Contract block ID (resolved during link_blocks if needed).
12551269
trait_ref: RwLock<Option<BlockId>>,
1256-
/// Trait symbol (for deferred block_id resolution)
1270+
/// Contract symbol (for deferred block_id resolution).
12571271
trait_sym: Option<&'blk Symbol>,
12581272
methods: RwLock<Vec<BlockId>>,
12591273
}
@@ -1299,7 +1313,7 @@ impl<'blk> BlockImpl<'blk> {
12991313
self.target_sym = Some(symbol);
13001314
}
13011315

1302-
/// Set the implemented trait from its bound symbol.
1316+
/// Set the implemented contract from its bound symbol.
13031317
pub fn set_trait(&mut self, unit: CompileUnit<'blk>, symbol: &'blk Symbol) {
13041318
let resolved = unit.try_type(symbol).unwrap_or(symbol);
13051319
*self.trait_ref.write() = resolved.block_id();

0 commit comments

Comments
 (0)