瀏覽代碼

compiler: fix access to static class fields from the static class methods + extended test

git-svn-id: trunk@14574 -
paul 15 年之前
父節點
當前提交
ea88883915
共有 2 個文件被更改,包括 12 次插入3 次删除
  1. 8 3
      compiler/pexpr.pas
  2. 4 0
      tests/test/tstatic1.pp

+ 8 - 3
compiler/pexpr.pas

@@ -847,7 +847,7 @@ implementation
                begin
                  if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
                    { We are calling from the static class method which has no self node }
-                   p1 := cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
+                   p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
                  else
                    p1:=load_self_node;
                  { We are calling a member }
@@ -1383,9 +1383,14 @@ implementation
                     if is_member_read(srsym,srsymtable,p1,hdef) then
                       begin
                         { if the field was originally found in an    }
-                        { objectsymtable, it means it's part of self }
+                        { objectsymtable, it means it's part of self
+                          if only method from which it was called is
+                          not class static                          }
                         if (srsymtable.symtabletype=ObjectSymtable) then
-                          p1:=load_self_node;
+                          if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
+                            p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
+                          else
+                            p1:=load_self_node;
                         { now, if the field itself is part of an objectsymtab }
                         { (it can be even if it was found in a withsymtable,  }
                         {  e.g., "with classinstance do field := 5"), then    }

+ 4 - 0
tests/test/tstatic1.pp

@@ -6,6 +6,8 @@ program tstatic1;
 
 type
   TSomeClass = class
+  private
+    {$ifndef fpc}class var{$endif}FSomethingStatic: Integer; {$ifdef fpc}static;{$endif}
   public
     class procedure SomeClassMethod(A: Integer);
     class procedure SomeStaticMethod(A: Integer); static;
@@ -22,9 +24,11 @@ end;
 class procedure TSomeClass.SomeStaticMethod(A: Integer); {$ifdef fpc} static; {$endif}
 begin
   WriteLn('TSomeClass.SomeStaticMethod: ', A);
+  WriteLn('TSomeClass.FSomethingStatic: ', FSomethingStatic);
   SomeClassMethod(A + 1);
 end;
 
 begin
+  TSomeClass.FSomethingStatic := 4;
   TSomeClass.SomeStaticMethod(1);
 end.