Browse Source

* prefer overloaded methods in current object over
inherited overloaded methods

git-svn-id: trunk@7355 -

peter 18 years ago
parent
commit
ea92a0e1a7
3 changed files with 68 additions and 5 deletions
  1. 1 0
      .gitattributes
  2. 9 5
      compiler/htypechk.pas
  3. 58 0
      tests/webtbs/tw8847.pp

+ 1 - 0
.gitattributes

@@ -8221,6 +8221,7 @@ tests/webtbs/tw8777g.pp svneol=native#text/plain
 tests/webtbs/tw8777i.pp svneol=native#text/plain
 tests/webtbs/tw8777i.pp svneol=native#text/plain
 tests/webtbs/tw8810.pp svneol=native#text/plain
 tests/webtbs/tw8810.pp svneol=native#text/plain
 tests/webtbs/tw8838.pp svneol=native#text/plain
 tests/webtbs/tw8838.pp svneol=native#text/plain
+tests/webtbs/tw8847.pp svneol=native#text/plain
 tests/webtbs/tw8861.pp svneol=native#text/plain
 tests/webtbs/tw8861.pp svneol=native#text/plain
 tests/webtbs/tw8870.pp svneol=native#text/plain
 tests/webtbs/tw8870.pp svneol=native#text/plain
 tests/webtbs/tw8883.pp svneol=native#text/plain
 tests/webtbs/tw8883.pp svneol=native#text/plain

+ 9 - 5
compiler/htypechk.pas

@@ -63,7 +63,7 @@ interface
         FParaNode   : tnode;
         FParaNode   : tnode;
         FParaLength : smallint;
         FParaLength : smallint;
         FAllowVariant : boolean;
         FAllowVariant : boolean;
-        function proc_add(pd:tprocdef):pcandidate;
+        function proc_add(ps:tprocsym;pd:tprocdef):pcandidate;
       public
       public
         constructor create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
         constructor create(sym:tprocsym;st:TSymtable;ppn:tnode;isprop,ignorevis : boolean);
         constructor create_operator(op:ttoken;ppn:tnode);
         constructor create_operator(op:ttoken;ppn:tnode);
@@ -1645,7 +1645,7 @@ implementation
                if (FParalength>=pd.minparacount) and
                if (FParalength>=pd.minparacount) and
                   ((po_varargs in pd.procoptions) or { varargs }
                   ((po_varargs in pd.procoptions) or { varargs }
                    (FParalength<=pd.maxparacount)) then
                    (FParalength<=pd.maxparacount)) then
-                 proc_add(pd);
+                 proc_add(sym,pd);
              end;
              end;
           end;
           end;
 
 
@@ -1710,7 +1710,7 @@ implementation
                                   hp:=hp^.next;
                                   hp:=hp^.next;
                                 end;
                                 end;
                                if not found then
                                if not found then
-                                 proc_add(pd);
+                                 proc_add(srprocsym,pd);
                              end;
                              end;
                          end;
                          end;
                       end;
                       end;
@@ -1792,7 +1792,7 @@ implementation
                                 hp:=hp^.next;
                                 hp:=hp^.next;
                               end;
                               end;
                             if not found then
                             if not found then
-                              proc_add(pd);
+                              proc_add(srprocsym,pd);
                           end;
                           end;
                       end;
                       end;
                   end;
                   end;
@@ -1817,7 +1817,7 @@ implementation
       end;
       end;
 
 
 
 
-    function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
+    function tcallcandidates.proc_add(ps:tprocsym;pd:tprocdef):pcandidate;
       var
       var
         defaultparacnt : integer;
         defaultparacnt : integer;
       begin
       begin
@@ -1844,6 +1844,10 @@ implementation
                dec(result^.firstparaidx,defaultparacnt);
                dec(result^.firstparaidx,defaultparacnt);
              end;
              end;
          end;
          end;
+        { Give a small penalty for overloaded methods not in
+          defined the current class/unit }
+        if ps.owner<>pd.owner then
+          result^.ordinal_distance:=result^.ordinal_distance+1.0;
       end;
       end;
 
 
 
 

+ 58 - 0
tests/webtbs/tw8847.pp

@@ -0,0 +1,58 @@
+{$ifdef fpc}{$mode objfpc}{$h+}{$endif}
+
+uses
+  Classes;
+
+type
+  tbase = class(tobject)
+  public
+    function add: tobject; overload;
+    function add(aitem: tobject): integer; overload;
+  end;
+
+  timpl = class(tbase)
+  public
+    function add: tpersistent; overload;
+    function add(aitem: tpersistent): integer; overload;
+  end;
+
+var
+  err : boolean;
+	
+function tbase.add: tobject;
+begin
+  writeln('tbase.add:tobject');
+  result := nil;
+end;
+
+function tbase.add(aitem: tobject): integer;
+begin
+  writeln('tbase.add(aitem: tobject)');
+  result := -1;
+end;
+
+function timpl.add: tpersistent;
+begin
+  writeln('timpl.add:tpersistent');
+  result := nil;
+end;
+
+function timpl.add(aitem: tpersistent): integer;
+begin
+  writeln('timpl.add(aitem: tpersistent)');
+	err:=false;
+  result := -1
+end;
+
+var
+  vimpl: timpl;
+
+begin
+  err:=true;
+  vimpl := timpl.create;
+  vimpl.add(nil);
+  vimpl.free;
+	if err then
+	  halt(1);
+end.
+