Browse Source

fcl-passrc: resolver: enum range: assign enum:=enumrange

git-svn-id: trunk@37441 -
Mattias Gaertner 7 years ago
parent
commit
bb15bbec52
2 changed files with 32 additions and 10 deletions
  1. 25 3
      packages/fcl-passrc/src/pasresolver.pp
  2. 7 7
      packages/fcl-passrc/tests/tcresolver.pas

+ 25 - 3
packages/fcl-passrc/src/pasresolver.pp

@@ -11455,11 +11455,12 @@ function TPasResolver.CheckAssignResCompatibility(const LHS,
   RHS: TPasResolverResult; ErrorEl: TPasElement; RaiseOnIncompatible: boolean
   ): integer;
 var
-  TypeEl: TPasType;
+  TypeEl, RTypeEl: TPasType;
   Handled: Boolean;
   C: TClass;
   LBT, RBT: TResolverBaseType;
   LRange: TResEvalValue;
+  RightSubResolved: TPasResolverResult;
 begin
   // check if the RHS can be converted to LHS
   {$IFDEF VerbosePasResolver}
@@ -11729,8 +11730,29 @@ begin
           end;
         end;
       end
-    else if (LBT=btContext) and (LHS.TypeEl is TPasArrayType) then
-      Result:=CheckAssignCompatibilityArrayType(LHS,RHS,ErrorEl,RaiseOnIncompatible);
+    else if (LBT=btContext) then
+      begin
+      TypeEl:=ResolveAliasType(LHS.TypeEl);
+      if (TypeEl.ClassType=TPasArrayType) then
+        Result:=CheckAssignCompatibilityArrayType(LHS,RHS,ErrorEl,RaiseOnIncompatible)
+      else if TypeEl.ClassType=TPasEnumType then
+        begin
+        if (RHS.BaseType=btRange) and (RHS.SubType=btContext) then
+          begin
+          RTypeEl:=ResolveAliasType(RHS.TypeEl);
+          if RTypeEl.ClassType=TPasRangeType then
+            begin
+            ComputeElement(TPasRangeType(RTypeEl).RangeExpr.left,RightSubResolved,[rcConstant]);
+            if (RightSubResolved.BaseType=btContext)
+                and IsSameType(TypeEl,RightSubResolved.TypeEl,true) then
+              begin
+              // enumtype := enumrange
+              Result:=cExact;
+              end;
+            end;
+          end;
+        end;
+      end;
     end;
 
   if (Result>=0) and (Result<cIncompatible) then

+ 7 - 7
packages/fcl-passrc/tests/tcresolver.pas

@@ -3100,15 +3100,15 @@ begin
   '  c3: TEnumRg = pred(high(TEnumRg));',
   '  c4: TEnumRg = TEnumRg(2);',
   'var',
-  '  s: TEnumRg;',
+  '  er: TEnumRg;',
   '  Enum: TEnum;',
   'begin',
-  // s:=d;
-  // Enum:=s;
-  // if Enum=s then ;
-  // if s=Enum then ;
-  // if s=c then ;
-  // if c=s then ;
+  '  er:=d;',
+  '  Enum:=er;',
+  //'  if Enum=er then ;',
+  //'  if er=Enum then ;',
+  //'  if er=c then ;',
+  //'  if c=er then ;',
   '']);
   ParseProgram;
   // see also: TestPropertyDefaultValue