11using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . Diagnostics ;
45using System . IO ;
1415
1516namespace KScr . Compiler ;
1617
17- public abstract class SourceNode : AbstractVisitor < SourceNode >
18+ public abstract class SourceNode : AbstractVisitor < SourceNode > , IValidatable
1819{
1920 public readonly List < SourceNode > Nodes = new ( ) ;
2021
@@ -57,6 +58,8 @@ public static int RevisitRec(IEnumerable<SourceNode> nodes, bool rec = false)
5758 Log < CompilerRuntime > . At ( LogLevel . Debug , $ "Revisited { c } members") ;
5859 return c ;
5960 }
61+
62+ public abstract void Validate ( ) ;
6063}
6164
6265public class PackageNode : SourceNode
@@ -103,7 +106,9 @@ public int ReadPackages()
103106 try
104107 {
105108 var dir = new DirectoryInfo ( sub ) ;
106- Nodes . Add ( ForPackage ( vm , ctx , dir , Package ) ) ;
109+ var node = ForPackage ( vm , ctx , dir , Package ) ;
110+ node . Validate ( ) ;
111+ Nodes . Add ( node ) ;
107112 c ++ ;
108113 }
109114 catch ( CompilerException cex )
@@ -134,7 +139,9 @@ public int ReadFiles()
134139 try
135140 {
136141 var file = new FileInfo ( sub ) ;
137- Nodes . Add ( new FileNode ( vm , ctx , this , file ) ) ;
142+ var node = new FileNode ( vm , ctx , this , file ) ;
143+ node . Validate ( ) ;
144+ Nodes . Add ( node ) ;
138145 c ++ ;
139146 }
140147 catch ( CompilerException cex )
@@ -154,6 +161,7 @@ public static int ReadClassesRec(IEnumerable<SourceNode> nodes)
154161 if ( node is FileNode fn )
155162 c += fn . ReadClass ( ) ;
156163 c += ReadClassesRec ( node . Nodes ) ;
164+ node . Validate ( ) ;
157165 }
158166 catch ( CompilerException cex )
159167 {
@@ -162,6 +170,13 @@ public static int ReadClassesRec(IEnumerable<SourceNode> nodes)
162170
163171 return c ;
164172 }
173+
174+ public override void Validate ( )
175+ {
176+ if ( ! Package . FullName . All ( c => ! char . IsLetter ( c ) || char . IsLower ( c ) ) )
177+ throw new CompilerException ( RuntimeBase . SystemSrcPos , CompilerErrorMessage . InvalidName ,
178+ "package" , Package . FullName , "must be lowercase" ) ;
179+ }
165180}
166181
167182public class FileNode : SourceNode
@@ -224,6 +239,32 @@ public MemberNode CreateClassNode()
224239 Member = Cls
225240 } ;
226241 }
242+
243+ public override void Validate ( )
244+ {
245+ // 1 validate constructors using all superconstructors
246+ if ( Cls . DeclaredMembers . ContainsKey ( Method . ConstructorName ) )
247+ {
248+ var ctor = ( Cls . DeclaredMembers [ Method . ConstructorName ] as Method ) ! ;
249+ foreach ( var error in Cls . Superclasses
250+ . Where ( cls => cls . Name is not "object" and not "void" )
251+ . Where ( cls => ! ctor . SuperCalls . Any ( spr => spr . Arg . StartsWith ( cls . CanonicalName ) ) )
252+ . Select ( missing => new CompilerException ( ctor . SourceLocation ,
253+ CompilerErrorMessage . ClassSuperTypeNotCalled , Cls , missing ) ) )
254+ vm . CompilerErrors . Add ( error ) ;
255+ }
256+
257+ // 2 validate class abstract or all abstract members implemented
258+ if ( ! Cls . IsAbstract ( ) )
259+ foreach ( var error in ( ( IClass ) Cls ) . InheritedMembers
260+ . Where ( ModifierMethods . IsAbstract )
261+ . Where ( mem => ( ( IClass ) Cls ) . ClassMembers . All ( x => x . Name != mem . Name ) )
262+ . Select ( missing => new CompilerException ( Cls . SourceLocation ,
263+ CompilerErrorMessage . ClassAbstractMemberNotImplemented , Cls , missing ) ) )
264+ vm . CompilerErrors . Add ( error ) ;
265+
266+ // 3 validate used type parameters
267+ }
227268}
228269
229270public class MemberNode : SourceNode
@@ -250,7 +291,9 @@ public int ReadMembers()
250291 foreach ( var mem in cls . member ( ) )
251292 try
252293 {
253- Nodes . Add ( Visit ( mem ) ) ;
294+ var node = Visit ( mem ) ;
295+ node . Validate ( ) ;
296+ Nodes . Add ( node ) ;
254297 c ++ ;
255298 }
256299 catch ( CompilerException cex )
@@ -412,4 +455,11 @@ private Core.System.Class ContainingClass()
412455 return cls ;
413456 return Parent ! . ContainingClass ( ) ;
414457 }
458+
459+ public override void Validate ( )
460+ {
461+ // 1 validate overriding member is constructor or matches supermember footprint
462+
463+ // 2 validate used type parameters
464+ }
415465}
0 commit comments