1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using System . Diagnostics ;
4
5
using System . IO ;
@@ -57,6 +58,8 @@ public static int RevisitRec(IEnumerable<SourceNode> nodes, bool rec = false)
57
58
Log < CompilerRuntime > . At ( LogLevel . Debug , $ "Revisited { c } members") ;
58
59
return c ;
59
60
}
61
+
62
+ public abstract void Validate ( ) ;
60
63
}
61
64
62
65
public class PackageNode : SourceNode
@@ -103,7 +106,9 @@ public int ReadPackages()
103
106
try
104
107
{
105
108
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 ) ;
107
112
c ++ ;
108
113
}
109
114
catch ( CompilerException cex )
@@ -134,7 +139,9 @@ public int ReadFiles()
134
139
try
135
140
{
136
141
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 ) ;
138
145
c ++ ;
139
146
}
140
147
catch ( CompilerException cex )
@@ -154,6 +161,7 @@ public static int ReadClassesRec(IEnumerable<SourceNode> nodes)
154
161
if ( node is FileNode fn )
155
162
c += fn . ReadClass ( ) ;
156
163
c += ReadClassesRec ( node . Nodes ) ;
164
+ node . Validate ( ) ;
157
165
}
158
166
catch ( CompilerException cex )
159
167
{
@@ -162,6 +170,13 @@ public static int ReadClassesRec(IEnumerable<SourceNode> nodes)
162
170
163
171
return c ;
164
172
}
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
+ }
165
180
}
166
181
167
182
public class FileNode : SourceNode
@@ -224,6 +239,32 @@ public MemberNode CreateClassNode()
224
239
Member = Cls
225
240
} ;
226
241
}
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
+ }
227
268
}
228
269
229
270
public class MemberNode : SourceNode
@@ -250,7 +291,9 @@ public int ReadMembers()
250
291
foreach ( var mem in cls . member ( ) )
251
292
try
252
293
{
253
- Nodes . Add ( Visit ( mem ) ) ;
294
+ var node = Visit ( mem ) ;
295
+ node . Validate ( ) ;
296
+ Nodes . Add ( node ) ;
254
297
c ++ ;
255
298
}
256
299
catch ( CompilerException cex )
@@ -412,4 +455,11 @@ private Core.System.Class ContainingClass()
412
455
return cls ;
413
456
return Parent ! . ContainingClass ( ) ;
414
457
}
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
+ }
415
465
}
0 commit comments