@@ -187,6 +187,8 @@ export class Compiler extends DiagnosticEmitter {
187187
188188 /** Current function in compilation. */
189189 currentFunction : Function ;
190+ /** Current enum in compilation. */
191+ currentEnum : Enum | null = null ;
190192 /** Current type in compilation. */
191193 currentType : Type = Type . void ;
192194
@@ -214,6 +216,7 @@ export class Compiler extends DiagnosticEmitter {
214216 // set up start function
215217 var startFunctionTemplate = new FunctionPrototype ( program , "start" , "start" , null ) ;
216218 var startFunctionInstance = new Function ( startFunctionTemplate , startFunctionTemplate . internalName , [ ] , [ ] , Type . void , null ) ;
219+ startFunctionInstance . set ( ElementFlags . START ) ;
217220 this . currentFunction = this . startFunction = startFunctionInstance ;
218221 }
219222
@@ -472,6 +475,7 @@ export class Compiler extends DiagnosticEmitter {
472475 // members might reference each other, triggering another compile
473476 element . set ( ElementFlags . COMPILED ) ;
474477
478+ this . currentEnum = element ;
475479 var previousValue : EnumValue | null = null ;
476480 if ( element . members )
477481 for ( var member of element . members . values ( ) ) {
@@ -480,6 +484,7 @@ export class Compiler extends DiagnosticEmitter {
480484 var initInStart = false ;
481485 var val = < EnumValue > member ;
482486 var valueDeclaration = val . declaration ;
487+ val . set ( ElementFlags . COMPILED ) ;
483488 if ( val . is ( ElementFlags . INLINED ) ) {
484489 if ( ! element . declaration || element . declaration . isTopLevelExport )
485490 this . module . addGlobal ( val . internalName , NativeType . I32 , false , this . module . createI32 ( val . constantValue ) ) ;
@@ -533,6 +538,7 @@ export class Compiler extends DiagnosticEmitter {
533538 this . warning ( DiagnosticCode . Cannot_export_a_mutable_global , valueDeclaration . range ) ;
534539 }
535540 }
541+ this . currentEnum = null ;
536542 return true ;
537543 }
538544
@@ -2775,7 +2781,7 @@ export class Compiler extends DiagnosticEmitter {
27752781 }
27762782
27772783 // otherwise resolve
2778- var resolved = this . program . resolveIdentifier ( expression , this . currentFunction ) ; // reports
2784+ var resolved = this . program . resolveIdentifier ( expression , this . currentFunction , this . currentEnum ) ; // reports
27792785 if ( ! resolved )
27802786 return this . module . createUnreachable ( ) ;
27812787
@@ -2799,6 +2805,17 @@ export class Compiler extends DiagnosticEmitter {
27992805 return this . compileInlineConstant ( < Global > element , contextualType ) ;
28002806 this . currentType = ( < Global > element ) . type ;
28012807 return this . module . createGetGlobal ( ( < Global > element ) . internalName , this . currentType . toNativeType ( ) ) ;
2808+
2809+ case ElementKind . ENUMVALUE : // here: if referenced from within the same enum
2810+ if ( ! element . is ( ElementFlags . COMPILED ) ) {
2811+ this . error ( DiagnosticCode . A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums , expression . range ) ;
2812+ this . currentType = Type . i32 ;
2813+ return this . module . createUnreachable ( ) ;
2814+ }
2815+ this . currentType = Type . i32 ;
2816+ if ( ( < EnumValue > element ) . is ( ElementFlags . INLINED ) )
2817+ return this . module . createI32 ( ( < EnumValue > element ) . constantValue ) ;
2818+ return this . module . createGetGlobal ( ( < EnumValue > element ) . internalName , NativeType . I32 ) ;
28022819 }
28032820 this . error ( DiagnosticCode . Operation_not_supported , expression . range ) ;
28042821 return this . module . createUnreachable ( ) ;
0 commit comments