@@ -350,7 +350,7 @@ private static void HandleEnumIsDefinedByName(MethodConvert methodConvert, Seman
350350
351351 var enumMembers = enumTypeSymbol . GetMembers ( ) . OfType < IFieldSymbol > ( )
352352 . Where ( field => field is { HasConstantValue : true , IsImplicitlyDeclared : false } ) . ToArray ( ) ;
353-
353+ var endTarget = new JumpTarget ( ) ;
354354 foreach ( var t in enumMembers )
355355 {
356356 methodConvert . Dup ( ) ; // Duplicate input name
@@ -359,18 +359,17 @@ private static void HandleEnumIsDefinedByName(MethodConvert methodConvert, Seman
359359
360360 var nextCheck = new JumpTarget ( ) ;
361361 methodConvert . JumpIfNot ( nextCheck ) ; // If not equal, check next
362-
363362 methodConvert . Drop ( ) ; // Remove the duplicated input name
364363 methodConvert . Push ( true ) ; // Push true (enum name is defined)
365- methodConvert . Ret ( ) ; // Return true
364+ methodConvert . JumpAlways ( endTarget ) ;
366365
367366 nextCheck . Instruction = methodConvert . Nop ( ) ;
368367 }
369368
370369 // No match found
371370 methodConvert . Drop ( ) ; // Remove the input name
372371 methodConvert . Push ( false ) ; // Push false (enum name is not defined)
373- methodConvert . Ret ( ) ; // Return false
372+ endTarget . Instruction = methodConvert . Nop ( ) ;
374373 }
375374
376375 /// <summary>
@@ -401,6 +400,7 @@ private static void HandleEnumGetName(MethodConvert methodConvert, SemanticModel
401400
402401 var enumMembers = enumTypeSymbol . GetMembers ( ) . OfType < IFieldSymbol > ( )
403402 . Where ( field => field is { HasConstantValue : true , IsImplicitlyDeclared : false } ) . ToArray ( ) ;
403+ var endTarget = new JumpTarget ( ) ;
404404 foreach ( var t in enumMembers )
405405 {
406406 methodConvert . Dup ( ) ; // Duplicate input value
@@ -411,15 +411,15 @@ private static void HandleEnumGetName(MethodConvert methodConvert, SemanticModel
411411
412412 methodConvert . Drop ( ) ; // Remove the duplicated input value
413413 methodConvert . Push ( t . Name ) ; // Push enum name
414- methodConvert . Ret ( ) ; // Return enum name
414+ methodConvert . JumpAlways ( endTarget ) ;
415415
416416 nextCheck . Instruction = methodConvert . Nop ( ) ;
417417 }
418418
419419 // No match found
420420 methodConvert . Drop ( ) ; // Remove the input value
421421 methodConvert . PushNull ( ) ; // Push null (no matching enum name)
422- methodConvert . Ret ( ) ; // Return null
422+ endTarget . Instruction = methodConvert . Nop ( ) ;
423423 }
424424
425425 /// <summary>
@@ -467,7 +467,7 @@ private static void HandleEnumGetNameWithType(MethodConvert methodConvert, Seman
467467
468468 var enumMembers = enumTypeSymbol . GetMembers ( ) . OfType < IFieldSymbol > ( )
469469 . Where ( field => field is { HasConstantValue : true , IsImplicitlyDeclared : false } ) . ToArray ( ) ;
470-
470+ var endTarget = new JumpTarget ( ) ;
471471 var valueItem = methodConvert . PopInstruction ( ) ; // The second argument is the value to get the name of
472472 var enumTypeItem = methodConvert . PopInstruction ( ) ; // The first argument is the enum type, It's unused when running
473473 methodConvert . AddInstruction ( valueItem ) ;
@@ -481,15 +481,15 @@ private static void HandleEnumGetNameWithType(MethodConvert methodConvert, Seman
481481
482482 methodConvert . Drop ( ) ; // Remove the duplicated input value
483483 methodConvert . Push ( t . Name ) ; // Push enum name
484- methodConvert . Ret ( ) ; // Return enum name
484+ methodConvert . JumpAlways ( endTarget ) ;
485485
486486 nextCheck . Instruction = methodConvert . Nop ( ) ;
487487 }
488488
489489 // No match found
490490 methodConvert . Drop ( ) ; // Remove the input value
491491 methodConvert . PushNull ( ) ; // Push null (no matching enum name)
492- methodConvert . Ret ( ) ; // Return null
492+ endTarget . Instruction = methodConvert . Nop ( ) ;
493493 }
494494
495495 /// <summary>
@@ -555,17 +555,17 @@ private static void HandleEnumTryParse(MethodConvert methodConvert, SemanticMode
555555 methodConvert . JumpIfNot ( nextCheck ) ;
556556
557557 // If equal:
558- methodConvert . Drop ( 2 ) ; // Remove the inputString
558+ methodConvert . Drop ( 2 ) ; // Remove the EnumType and inputString
559559 methodConvert . Push ( t . ConstantValue ) ; // Stack: [..., enumValue]
560560 methodConvert . AccessSlot ( OpCode . STSFLD , index ) ; // Store enum value in out parameter
561561 methodConvert . Push ( true ) ; // Stack: [..., true]
562- methodConvert . Ret ( ) ;
562+ methodConvert . JumpAlways ( endTarget ) ;
563563
564564 nextCheck . Instruction = methodConvert . Nop ( ) ;
565565 }
566566
567567 // No match found
568- methodConvert . Drop ( 2 ) ; // Remove the inputString
568+ methodConvert . Drop ( 2 ) ; // Remove the EumType and inputString
569569 methodConvert . Push ( 0 ) ; // Default enum value
570570 methodConvert . AccessSlot ( OpCode . STSFLD , index ) ; // Store default value in out parameter
571571 methodConvert . Push ( false ) ; // Success flag set to false
@@ -623,8 +623,6 @@ private static void HandleEnumGetNames(MethodConvert methodConvert, SemanticMode
623623 foreach ( IFieldSymbol m in enumMembers . Reverse ( ) ) // PACK works in a reversed way
624624 methodConvert . Push ( m . Name ) ;
625625 methodConvert . Pack ( enumMembers . Length ) ;
626-
627- methodConvert . Ret ( ) ;
628626 }
629627
630628 /// <summary>
@@ -677,8 +675,6 @@ private static void HandleEnumGetValues(MethodConvert methodConvert, SemanticMod
677675 foreach ( IFieldSymbol m in enumMembers . Reverse ( ) ) // PACK works in a reversed way
678676 methodConvert . Push ( m . ConstantValue ) ;
679677 methodConvert . Pack ( enumMembers . Length ) ;
680-
681- methodConvert . Ret ( ) ;
682678 }
683679
684680 /// <summary>
@@ -733,7 +729,8 @@ private static void HandleEnumIsDefined(MethodConvert methodConvert, SemanticMod
733729 var valueType = model . GetTypeInfo ( argument . Expression ) . Type ;
734730
735731 // We need to compare the input value against the enum values
736- var endLabel = new JumpTarget ( ) ;
732+ var endTarget = new JumpTarget ( ) ;
733+
737734 // here add check logic to verify if valueType is string
738735 var isName = valueType is INamedTypeSymbol { Name : "String" } ;
739736
@@ -756,15 +753,14 @@ private static void HandleEnumIsDefined(MethodConvert methodConvert, SemanticMod
756753 methodConvert . JumpIfNot ( nextCheck ) ;
757754
758755 // If equal, set result to true
759- methodConvert . Drop ( ) ;
760- methodConvert . Drop ( ) ; // Remove the false
756+ methodConvert . Drop ( 2 ) ; // Remove the EnumType and duplicated value
761757 methodConvert . Push ( true ) ; // Set result to true
762- methodConvert . Ret ( ) ;
758+ methodConvert . JumpAlways ( endTarget ) ;
763759 nextCheck . Instruction = methodConvert . Nop ( ) ;
764760 }
765- methodConvert . Drop ( ) ;
766- methodConvert . Drop ( ) ; // Remove the duplicated value
761+ methodConvert . Drop ( 2 ) ; // Remove the EnumType and duplicated value
767762 methodConvert . Push ( false ) ;
763+ endTarget . Instruction = methodConvert . Nop ( ) ;
768764 }
769765
770766 private static INamedTypeSymbol EnsureEnumTypeArgument ( IMethodSymbol symbol , int index , SyntaxNode ? errorNode )
@@ -794,7 +790,7 @@ private static void HandleEnumToString(MethodConvert methodConvert, SemanticMode
794790 throw new CompilationException ( symbol , DiagnosticId . InvalidType , "Enum.ToString is only supported on enum values." ) ;
795791
796792 var enumMembers = GetEnumFields ( enumType ) ;
797-
793+ var endTarget = new JumpTarget ( ) ;
798794 foreach ( var member in enumMembers )
799795 {
800796 methodConvert . Dup ( ) ;
@@ -805,12 +801,13 @@ private static void HandleEnumToString(MethodConvert methodConvert, SemanticMode
805801 methodConvert . JumpIfFalse ( next ) ;
806802 methodConvert . Drop ( ) ;
807803 methodConvert . Push ( member . Name ) ;
808- methodConvert . Ret ( ) ;
804+ methodConvert . JumpAlways ( endTarget ) ;
809805 next . Instruction = methodConvert . Nop ( ) ;
810806 }
811807
812808 // Fallback to numeric representation
813809 methodConvert . CallContractMethod ( NativeContract . StdLib . Hash , "itoa" , 1 , true ) ;
810+ endTarget . Instruction = methodConvert . Nop ( ) ;
814811 }
815812
816813 private static void HandleEnumHasFlag ( MethodConvert methodConvert , SemanticModel model , IMethodSymbol symbol ,
@@ -942,6 +939,7 @@ private static void HandleEnumTryParseGeneric(MethodConvert methodConvert, Seman
942939
943940 byte resultSlot = methodConvert . AddAnonymousVariable ( ) ;
944941 byte successSlot = methodConvert . AddAnonymousVariable ( ) ;
942+ var successTarget = new JumpTarget ( ) ;
945943
946944 methodConvert . Push ( 0 ) ;
947945 methodConvert . AccessSlot ( OpCode . STLOC , resultSlot ) ;
@@ -961,21 +959,16 @@ private static void HandleEnumTryParseGeneric(MethodConvert methodConvert, Seman
961959 methodConvert . AccessSlot ( OpCode . STLOC , resultSlot ) ;
962960 methodConvert . Push ( true ) ;
963961 methodConvert . AccessSlot ( OpCode . STLOC , successSlot ) ;
964- JumpTarget end = new ( ) ;
965- methodConvert . JumpAlways ( end ) ;
966- end . Instruction = methodConvert . Nop ( ) ;
967- methodConvert . AccessSlot ( OpCode . LDLOC , resultSlot ) ;
968- methodConvert . AccessSlot ( OpCode . STSFLD , index ) ;
969- methodConvert . AccessSlot ( OpCode . LDLOC , successSlot ) ;
970- methodConvert . Ret ( ) ;
962+ methodConvert . JumpAlways ( successTarget ) ;
963+
971964 next . Instruction = methodConvert . Nop ( ) ;
972965 }
973966
974967 methodConvert . Drop ( ) ;
968+ successTarget . Instruction = methodConvert . Nop ( ) ;
975969 methodConvert . AccessSlot ( OpCode . LDLOC , resultSlot ) ;
976970 methodConvert . AccessSlot ( OpCode . STSFLD , index ) ;
977971 methodConvert . AccessSlot ( OpCode . LDLOC , successSlot ) ;
978- methodConvert . Ret ( ) ;
979972 }
980973
981974 private static void HandleEnumTryParseGenericIgnoreCase ( MethodConvert methodConvert , SemanticModel model , IMethodSymbol symbol ,
@@ -1004,7 +997,9 @@ private static void HandleEnumTryParseGenericIgnoreCase(MethodConvert methodConv
1004997 methodConvert . Push ( false ) ;
1005998 methodConvert . AccessSlot ( OpCode . STLOC , successSlot ) ;
1006999
1007- JumpTarget skipUpper = new ( ) ;
1000+ var skipUpper = new JumpTarget ( ) ;
1001+ var successTarget = new JumpTarget ( ) ;
1002+
10081003 methodConvert . AccessSlot ( OpCode . LDLOC , ignoreSlot ) ;
10091004 methodConvert . JumpIfFalse ( skipUpper ) ;
10101005 ConvertToUpper ( methodConvert , preserveInput : true ) ;
@@ -1031,21 +1026,16 @@ private static void HandleEnumTryParseGenericIgnoreCase(MethodConvert methodConv
10311026 methodConvert . AccessSlot ( OpCode . STLOC , resultSlot ) ;
10321027 methodConvert . Push ( true ) ;
10331028 methodConvert . AccessSlot ( OpCode . STLOC , successSlot ) ;
1034- JumpTarget end = new ( ) ;
1035- methodConvert . JumpAlways ( end ) ;
1036- end . Instruction = methodConvert . Nop ( ) ;
1037- methodConvert . AccessSlot ( OpCode . LDLOC , resultSlot ) ;
1038- methodConvert . AccessSlot ( OpCode . STSFLD , index ) ;
1039- methodConvert . AccessSlot ( OpCode . LDLOC , successSlot ) ;
1040- methodConvert . Ret ( ) ;
1029+ methodConvert . JumpAlways ( successTarget ) ;
1030+
10411031 next . Instruction = methodConvert . Nop ( ) ;
10421032 }
10431033
10441034 methodConvert . Drop ( ) ;
1035+ successTarget . Instruction = methodConvert . Nop ( ) ;
10451036 methodConvert . AccessSlot ( OpCode . LDLOC , resultSlot ) ;
10461037 methodConvert . AccessSlot ( OpCode . STSFLD , index ) ;
10471038 methodConvert . AccessSlot ( OpCode . LDLOC , successSlot ) ;
1048- methodConvert . Ret ( ) ;
10491039 }
10501040
10511041 private static void HandleEnumGetValuesGeneric ( MethodConvert methodConvert , SemanticModel model , IMethodSymbol symbol ,
@@ -1058,7 +1048,6 @@ private static void HandleEnumGetValuesGeneric(MethodConvert methodConvert, Sema
10581048 methodConvert . Push ( member . ConstantValue ) ;
10591049
10601050 methodConvert . Pack ( members . Length ) ;
1061- methodConvert . Ret ( ) ;
10621051 }
10631052
10641053 private static void HandleEnumGetNamesGeneric ( MethodConvert methodConvert , SemanticModel model , IMethodSymbol symbol ,
@@ -1071,6 +1060,5 @@ private static void HandleEnumGetNamesGeneric(MethodConvert methodConvert, Seman
10711060 methodConvert . Push ( member . Name ) ;
10721061
10731062 methodConvert . Pack ( members . Length ) ;
1074- methodConvert . Ret ( ) ;
10751063 }
10761064}
0 commit comments