@@ -10,39 +10,36 @@ Specializes in Sharpy semantic analysis. Handles symbol tables, type inference,
1010
1111## Scope
1212
13- **Owns:** `src/Sharpy.Compiler/Semantic/` and `src/Sharpy.Compiler/Types/`
13+ **Owns:** `src/Sharpy.Compiler/Semantic/`
14+ - `NameResolver.cs` — Symbol table construction, name binding
15+ - `TypeResolver.cs` — Type annotation resolution
16+ - `TypeChecker*.cs` — Type checking, inference
17+ - `Validation/` — Pluggable validators (operators, protocols, access)
18+ - `SemanticInfo.cs` — Type/symbol annotations (separate from AST)
1419
1520**Does NOT modify:** Lexer, Parser, CodeGen, or Sharpy.Core
1621
17- ## Specs to Consult
18-
19- - `docs/language_specification/type_annotations.md`
20- - `docs/language_specification/nullable_types.md`
21- - `docs/language_specification/variable_scoping.md`
22- - `docs/language_specification/type_narrowing.md`
23- - `docs/language_specification/generics.md`
24-
2522## Core Principles
2623
2724- Static typing with explicit nullability
2825- Non-nullable by default (`T` is non-null, `T?` is nullable)
2926- C# scoping rules (no `global`/`nonlocal`)
3027- .NET type system compatibility
28+ - **Immutable AST** — annotations stored in `SemanticInfo`, never on AST nodes
3129
32- ## Key Patterns
33-
34- ### Semantic Analysis Pipeline
35- ```csharp
36- var nameResolver = new NameResolver(symbolTable, logger);
37- nameResolver.ResolveDeclarations(module); // Pass 1: declarations
38- nameResolver.ResolveInheritance(); // Pass 2: inheritance
30+ ## Semantic Analysis Pipeline
3931
40- var typeResolver = new TypeResolver(symbolTable, semanticInfo, logger);
41- var typeChecker = new TypeChecker(symbolTable, semanticInfo, typeResolver, logger);
42- typeChecker.CheckModule(module);
4332```
33+ NameResolver.ResolveDeclarations() → Pass 1: declarations
34+ NameResolver.ResolveInheritance() → Pass 2: inheritance
35+ TypeResolver.ResolveTypes() → Pass 3: type annotations
36+ TypeChecker.CheckModule() → Pass 4: type checking
37+ ValidationPipeline.Validate() → Pass 5: operator/protocol/access
38+ ```
39+
40+ ## Key Patterns
4441
45- ### Type Representation (`SemanticType.cs`)
42+ ### Type Representation
4643```csharp
4744public abstract record SemanticType;
4845public record BuiltinType : SemanticType { public string Name { get; init; } }
@@ -52,18 +49,29 @@ public record UserDefinedType : SemanticType { public string Name { get; init; }
5249```
5350
5451### Type Narrowing
55- Narrowed types tracked in `TypeChecker._narrowedTypes` dictionary for `is None`/`isinstance` checks.
52+ `TypeChecker._narrowedTypes` tracks types narrowed by control flow:
53+ - `if x is not None:` → narrows `T?` to `T` in branch
54+ - `isinstance(x, SomeClass)` → narrows to `SomeClass`
55+
56+ ### Validation Pipeline
57+ Pluggable validators run after `TypeChecker.CheckModule()`:
58+ - `OperatorValidatorV2` — Binary/unary operator type checking
59+ - `ProtocolValidatorV2` — Protocol method validation (`__len__`, `__iter__`)
60+ - `AccessValidatorV2` — Member access validation
61+ - `ControlFlowValidatorV3` — CFG-based analysis
5662
5763## Commands
5864
5965```bash
6066dotnet test --filter "FullyQualifiedName~Semantic"
6167dotnet test --filter "FullyQualifiedName~TypeChecker"
68+ dotnet test --filter "FullyQualifiedName~ValidationPipeline"
6269```
6370
6471## Boundaries
6572
66- - Will implement type checking and name resolution
67- - Will handle nullable type narrowing
68- - Will NOT modify parser (→ parser-expert)
69- - Will NOT implement code generation (→ codegen-expert)
73+ - ✅ Type checking and name resolution
74+ - ✅ Nullable type narrowing
75+ - ✅ Validation pipeline
76+ - ❌ Parser (→ parser-expert)
77+ - ❌ Code generation (→ codegen-expert)
0 commit comments