Skip to content

Commit 97b399c

Browse files
committed
Semantic type diffing support
1 parent 2d91c7c commit 97b399c

File tree

7 files changed

+2211
-3
lines changed

7 files changed

+2211
-3
lines changed

crates/semantic-analysis/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ tempfile = "3.8"
2828
[[example]]
2929
name = "symbol_resolution_demo"
3030
path = "../../examples/symbol_resolution_demo.rs"
31+
32+
[[example]]
33+
name = "type_extraction_demo"
34+
path = "../../examples/type_extraction_demo.rs"
35+
36+
[[example]]
37+
name = "dependency_graph_demo"
38+
path = "../../examples/dependency_graph_demo.rs"

crates/semantic-analysis/README.md

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ Advanced semantic analysis engine that provides comprehensive symbol resolution,
2525
- **Cross-file dependency tracking**
2626
- **Import graph construction** for dependency analysis
2727

28+
### 🔧 **Type Information Extraction**
29+
- **Comprehensive type signature parsing** with generics and arrays
30+
- **Type equivalence checking** across different languages
31+
- **Type dependency graph construction** with relationship analysis
32+
- **Language-specific type handling**:
33+
- Java: Generics, wildcards, primitive boxing
34+
- Python: Type hints, union types, optional types
35+
- JavaScript: TypeScript types, JSDoc annotations
36+
- C/C++: Templates, pointers, references, const/volatile
37+
- **Inheritance and interface relationship tracking**
38+
- **Type coupling metrics** (afferent/efferent coupling, instability)
39+
40+
### 🏗️ **Comprehensive Dependency Graph Construction**
41+
- **Multi-dimensional dependency analysis**:
42+
- Function call relationships with call type classification
43+
- Type dependencies (inheritance, composition, aggregation)
44+
- Variable usage and data flow dependencies
45+
- Import/export and module dependencies
46+
- **Advanced coupling metrics**:
47+
- Afferent/efferent coupling calculation
48+
- Instability and abstractness metrics
49+
- Function call, type, data, and control coupling
50+
- **Dependency analysis features**:
51+
- Circular dependency detection
52+
- Strongly connected component identification
53+
- Topological dependency layer calculation
54+
- Dependency hotspot identification
55+
- **Cross-file dependency tracking** with import graph construction
56+
2857
### 🎯 **Scope Management**
2958
- **Hierarchical scope resolution** (global → file → class → function → block)
3059
- **Symbol shadowing detection** and resolution
@@ -120,6 +149,125 @@ println!("Classes: {}", stats.class_count);
120149
println!("Average references per symbol: {:.2}", stats.avg_references_per_symbol);
121150
```
122151

152+
### Type Information Extraction
153+
154+
```rust
155+
use smart_diff_semantic::{TypeExtractor, TypeExtractorConfig, TypeSignature, TypeEquivalence};
156+
157+
// Create type extractor
158+
let mut extractor = TypeExtractor::with_defaults(Language::Java);
159+
160+
// Parse type signatures
161+
let generic_type = TypeSignature::parse("List<String>")?;
162+
println!("Base type: {}", generic_type.base_type);
163+
println!("Generic params: {}", generic_type.generic_params.len());
164+
165+
// Check type equivalence
166+
let equivalent = TypeEquivalence::are_equivalent("int", "i32");
167+
println!("int ≡ i32: {}", equivalent);
168+
169+
// Calculate type similarity
170+
let type1 = TypeSignature::parse("List<String>")?;
171+
let type2 = TypeSignature::parse("ArrayList<String>")?;
172+
let similarity = TypeEquivalence::calculate_type_similarity(&type1, &type2);
173+
println!("Similarity: {:.2}", similarity);
174+
175+
// Extract types from code
176+
let extraction_result = extractor.extract_types("MyClass.java", &parse_result)?;
177+
for extracted_type in &extraction_result.types {
178+
println!("Type: {} (kind: {:?})",
179+
extracted_type.type_info.name,
180+
extracted_type.type_info.kind);
181+
}
182+
```
183+
184+
### Type Dependency Analysis
185+
186+
```rust
187+
use smart_diff_semantic::TypeDependencyGraphBuilder;
188+
189+
// Build type dependency graph
190+
let mut dependency_builder = TypeDependencyGraphBuilder::new();
191+
dependency_builder.build_from_extraction_result(&extraction_result)?;
192+
193+
// Analyze dependencies
194+
let analysis = dependency_builder.analyze_dependencies();
195+
println!("Total types: {}", analysis.total_types);
196+
println!("Inheritance chains: {}", analysis.inheritance_chains.len());
197+
println!("Circular dependencies: {}", analysis.circular_dependencies.len());
198+
199+
// Get coupling metrics
200+
for (type_name, metrics) in &analysis.coupling_metrics {
201+
println!("{}: AC={}, EC={}, Instability={:.3}",
202+
type_name,
203+
metrics.afferent_coupling,
204+
metrics.efferent_coupling,
205+
metrics.instability);
206+
}
207+
```
208+
209+
### Comprehensive Dependency Graph Construction
210+
211+
```rust
212+
use smart_diff_semantic::{
213+
ComprehensiveDependencyGraphBuilder, DependencyAnalysisConfig,
214+
SymbolResolver, TypeExtractor
215+
};
216+
217+
// Create comprehensive dependency graph builder
218+
let config = DependencyAnalysisConfig {
219+
include_function_calls: true,
220+
include_type_dependencies: true,
221+
include_variable_usage: true,
222+
include_import_dependencies: true,
223+
include_inheritance: true,
224+
min_dependency_strength: 0.2,
225+
max_transitive_depth: 8,
226+
};
227+
228+
let mut builder = ComprehensiveDependencyGraphBuilder::new(config);
229+
230+
// Add symbol resolution data
231+
let mut symbol_resolver = SymbolResolver::with_defaults();
232+
symbol_resolver.process_file("MyClass.java", &parse_result)?;
233+
builder = builder.with_symbol_table(symbol_resolver.get_symbol_table().clone());
234+
235+
// Add type extraction data
236+
let mut type_extractor = TypeExtractor::with_defaults(Language::Java);
237+
let type_result = type_extractor.extract_types("MyClass.java", &parse_result)?;
238+
builder.add_type_extraction_result("MyClass.java".to_string(), type_result);
239+
240+
// Build comprehensive dependency graph
241+
let files = vec![("MyClass.java".to_string(), parse_result)];
242+
builder.build_comprehensive_graph(files)?;
243+
244+
// Analyze dependencies
245+
let analysis = builder.analyze_comprehensive_dependencies();
246+
println!("Total nodes: {}", analysis.total_nodes);
247+
println!("Function call dependencies: {}", analysis.function_call_dependencies);
248+
println!("Type dependencies: {}", analysis.type_dependencies);
249+
println!("Circular dependencies: {}", analysis.circular_dependencies.len());
250+
251+
// Get coupling metrics
252+
for (name, metrics) in &analysis.coupling_metrics {
253+
println!("{}: AC={}, EC={}, Instability={:.3}, Function calls={}, Types={}",
254+
name,
255+
metrics.afferent_coupling,
256+
metrics.efferent_coupling,
257+
metrics.instability,
258+
metrics.function_call_coupling,
259+
metrics.type_coupling);
260+
}
261+
262+
// Identify hotspots
263+
for hotspot in &analysis.hotspots {
264+
println!("Hotspot: {} (score: {:.1}, file: {})",
265+
hotspot.name,
266+
hotspot.coupling_score,
267+
hotspot.file_path);
268+
}
269+
```
270+
123271
## Configuration
124272

125273
### SymbolResolverConfig
@@ -216,7 +364,9 @@ const fs = require('fs'); // CommonJS require
216364

217365
## Examples
218366

219-
Run the comprehensive demo:
367+
### Symbol Resolution Demo
368+
369+
Run the comprehensive symbol resolution demo:
220370

221371
```bash
222372
cargo run --example symbol_resolution_demo
@@ -229,6 +379,39 @@ This demonstrates:
229379
- Scope management
230380
- Symbol statistics
231381

382+
### Type Extraction Demo
383+
384+
Run the comprehensive type extraction demo:
385+
386+
```bash
387+
cargo run --example type_extraction_demo
388+
```
389+
390+
This demonstrates:
391+
- Type signature parsing and normalization
392+
- Type equivalence checking across languages
393+
- Type information extraction from code
394+
- Type dependency graph construction
395+
- Cross-language type handling
396+
- Coupling metrics calculation
397+
398+
### Dependency Graph Demo
399+
400+
Run the comprehensive dependency graph construction demo:
401+
402+
```bash
403+
cargo run --example dependency_graph_demo
404+
```
405+
406+
This demonstrates:
407+
- Multi-dimensional dependency analysis
408+
- Function call relationship tracking
409+
- Type and inheritance dependency mapping
410+
- Variable usage and data flow analysis
411+
- Comprehensive coupling metrics calculation
412+
- Dependency hotspot identification
413+
- Cross-file dependency tracking
414+
232415
## Testing
233416

234417
Run the test suite:

0 commit comments

Comments
 (0)