Browse Source

* default parameters working !

peter 25 years ago
parent
commit
658a1f4fcd
6 changed files with 193 additions and 75 deletions
  1. 8 4
      compiler/psub.pas
  2. 60 1
      compiler/symconst.pas
  3. 32 60
      compiler/symdef.inc
  4. 14 2
      compiler/symdefh.inc
  5. 29 7
      compiler/tccal.pas
  6. 50 1
      compiler/tree.pas

+ 8 - 4
compiler/psub.pas

@@ -632,7 +632,8 @@ var
 begin
   { check parameter type }
   if not(po_containsself in aktprocsym^.definition^.procoptions) and
-     ((aktprocsym^.definition^.para^.count<>1) or
+     ((aktprocsym^.definition^.minparacount<>1) or
+      (aktprocsym^.definition^.maxparacount<>1) or
       (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
    Message(parser_e_ill_msg_param);
   pt:=comp_expr(true);
@@ -1248,7 +1249,7 @@ begin
 
            { check the parameters }
            if (not(m_repeat_forward in aktmodeswitches) and
-               (aktprocsym^.definition^.para^.count=0)) or
+               (aktprocsym^.definition^.maxparacount=0)) or
               (equal_paras(aktprocsym^.definition^.para,hd^.para,cp_none) and
               { for operators equal_paras is not enough !! }
               ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
@@ -1256,7 +1257,7 @@ begin
              begin
                if not equal_paras(aktprocsym^.definition^.para,hd^.para,cp_all) and
                   ((m_repeat_forward in aktmodeswitches) or
-                   (aktprocsym^.definition^.para^.count>0)) then
+                   (aktprocsym^.definition^.maxparacount>0)) then
                  begin
                     MessagePos1(aktprocsym^.definition^.fileinfo,parser_e_header_dont_match_forward,
                                 aktprocsym^.demangledName);
@@ -2074,7 +2075,10 @@ end.
 
 {
   $Log$
-  Revision 1.5  2000-08-06 14:17:15  peter
+  Revision 1.6  2000-08-06 19:39:28  peter
+    * default parameters working !
+
+  Revision 1.5  2000/08/06 14:17:15  peter
     * overload fixes (merged)
 
   Revision 1.4  2000/07/30 17:04:43  peter

+ 60 - 1
compiler/symconst.pas

@@ -31,6 +31,62 @@ interface
 const
   def_alignment = 4;
 
+  { if you change one of the following contants, }
+  { you have also to change the typinfo unit}
+  { and the rtl/i386,template/rttip.inc files    }
+  tkUnknown  = 0;
+  tkInteger  = 1;
+  tkChar     = 2;
+  tkEnumeration   = 3;
+  tkFloat    = 4;
+  tkSet      = 5;
+  tkMethod   = 6;
+  tkSString  = 7;
+  tkString   = tkSString;
+  tkLString  = 8;
+  tkAString  = 9;
+  tkWString  = 10;
+  tkVariant  = 11;
+  tkArray    = 12;
+  tkRecord   = 13;
+  tkInterface= 14;
+  tkClass    = 15;
+  tkObject   = 16;
+  tkWChar    = 17;
+  tkBool     = 18;
+  tkInt64    = 19;
+  tkQWord    = 20;
+
+  otSByte    = 0;
+  otUByte    = 1;
+  otSWord    = 2;
+  otUWord    = 3;
+  otSLong    = 4;
+  otULong    = 5;
+
+  ftSingle   = 0;
+  ftDouble   = 1;
+  ftExtended = 2;
+  ftComp     = 3;
+  ftCurr     = 4;
+  ftFixed16  = 5;
+  ftFixed32  = 6;
+
+  mkProcedure= 0;
+  mkFunction = 1;
+  mkConstructor   = 2;
+  mkDestructor    = 3;
+  mkClassProcedure= 4;
+  mkClassFunction = 5;
+
+  pfvar      = 1;
+  pfConst    = 2;
+  pfArray    = 4;
+  pfAddress  = 8;
+  pfReference= 16;
+  pfOut      = 32;
+
+
 type
   { symbol options }
   tsymoption=(sp_none,
@@ -221,7 +277,10 @@ implementation
 end.
 {
   $Log$
-  Revision 1.4  2000-08-05 13:25:06  peter
+  Revision 1.5  2000-08-06 19:39:28  peter
+    * default parameters working !
+
+  Revision 1.4  2000/08/05 13:25:06  peter
     * packenum 1 fixes (merged)
 
   Revision 1.3  2000/07/13 12:08:27  michael

+ 32 - 60
compiler/symdef.inc

@@ -24,61 +24,17 @@
                      TDEF (base class for definitions)
 ****************************************************************************}
 
-    const
-       { if you change one of the following contants, }
-       { you have also to change the typinfo unit     }
-       { and the rtl/i386,template/rttip.inc files    }
-       tkUnknown       = 0;
-       tkInteger       = 1;
-       tkChar          = 2;
-       tkEnumeration   = 3;
-       tkFloat         = 4;
-       tkSet           = 5;
-       tkMethod        = 6;
-       tkSString       = 7;
-       tkString        = tkSString;
-       tkLString       = 8;
-       tkAString       = 9;
-       tkWString       = 10;
-       tkVariant       = 11;
-       tkArray         = 12;
-       tkRecord        = 13;
-       tkInterface     = 14;
-       tkClass         = 15;
-       tkObject        = 16;
-       tkWChar         = 17;
-       tkBool          = 18;
-       tkInt64         = 19;
-       tkQWord         = 20;
-
-       otSByte         = 0;
-       otUByte         = 1;
-       otSWord         = 2;
-       otUWord         = 3;
-       otSLong         = 4;
-       otULong         = 5;
-
-       ftSingle        = 0;
-       ftDouble        = 1;
-       ftExtended      = 2;
-       ftComp          = 3;
-       ftCurr          = 4;
-       ftFixed16       = 5;
-       ftFixed32       = 6;
-
-       mkProcedure     = 0;
-       mkFunction      = 1;
-       mkConstructor   = 2;
-       mkDestructor    = 3;
-       mkClassProcedure= 4;
-       mkClassFunction = 5;
-
-       pfvar           = 1;
-       pfConst         = 2;
-       pfArray         = 4;
-       pfAddress       = 8;
-       pfReference     = 16;
-       pfOut           = 32;
+    function tparalinkedlist.count:longint;
+      begin
+        { You must use tabstractprocdef.minparacount and .maxparacount instead }
+        internalerror(432432978);
+        count:=0;
+      end;
+
+
+{****************************************************************************
+                     TDEF (base class for definitions)
+****************************************************************************}
 
 
     constructor tdef.init;
@@ -2370,6 +2326,8 @@
       begin
          inherited init;
          new(para,init);
+         minparacount:=0;
+         maxparacount:=0;
          fpu_used:=0;
          proctypeoption:=potype_none;
          proccalloptions:=[];
@@ -2397,6 +2355,9 @@
         hp^.register:=R_NO;
         hp^.defaultvalue:=defval;
         para^.insert(hp);
+        if not assigned(defval) then
+         inc(minparacount);
+        inc(maxparacount);
       end;
 
 
@@ -2421,6 +2382,7 @@
          while assigned(hp) do
           begin
             hp^.paratype.resolve;
+            resolvesym(psym(hp^.defaultvalue));
             hp:=pparaitem(hp^.next);
           end;
       end;
@@ -2433,6 +2395,8 @@
       begin
          inherited load;
          new(para,init);
+         minparacount:=0;
+         maxparacount:=0;
          rettype.load;
          fpu_used:=readbyte;
          proctypeoption:=tproctypeoption(readlong);
@@ -2447,6 +2411,10 @@
             { hp^.register:=tregister(readbyte); }
             hp^.register:=R_NO;
             hp^.paratype.load;
+            hp^.defaultvalue:=readsymref;
+            if not assigned(hp^.defaultvalue) then
+             inc(minparacount);
+            inc(maxparacount);
             para^.concat(hp);
           end;
       end;
@@ -2463,13 +2431,14 @@
          writelong(ord(proctypeoption));
          writesmallset(proccalloptions);
          writesmallset(procoptions);
-         writeword(para^.count);
+         writeword(maxparacount);
          hp:=pparaitem(para^.first);
          while assigned(hp) do
           begin
             writebyte(byte(hp^.paratyp));
             { writebyte(byte(hp^.register)); }
             hp^.paratype.write;
+            writesymref(hp^.defaultvalue);
             hp:=pparaitem(hp^.next);
           end;
       end;
@@ -2951,7 +2920,7 @@ Const local_symtable_index : longint = $8001;
       oldrec := stabrecstring;
       getmem(StabRecString,1024);
       strpcopy(StabRecString,'f'+rettype.def^.numberstring);
-      i:=para^.count;
+      i:=maxparacount;
       if i>0 then
         begin
         strpcopy(strend(StabRecString),','+tostr(i)+';');
@@ -3183,7 +3152,7 @@ Const local_symtable_index : longint = $8001;
          nss : pchar;
         { i   : longint; }
       begin
-        { i := para^.count; }
+        { i := maxparacount; }
         getmem(nss,1024);
         { it is not a function but a function pointer !! (PM) }
 
@@ -3243,7 +3212,7 @@ Const local_symtable_index : longint = $8001;
              rttilist^.concat(new(pai_const,init_8bit(methodkind)));
 
              { get # of parameters }
-             rttilist^.concat(new(pai_const,init_8bit(para^.count)));
+             rttilist^.concat(new(pai_const,init_8bit(maxparacount)));
 
              { write parameter info. The parameters must be written in reverse order
                if this method uses right to left parameter pushing! }
@@ -4195,7 +4164,10 @@ Const local_symtable_index : longint = $8001;
 
 {
   $Log$
-  Revision 1.6  2000-08-06 14:17:15  peter
+  Revision 1.7  2000-08-06 19:39:28  peter
+    * default parameters working !
+
+  Revision 1.6  2000/08/06 14:17:15  peter
     * overload fixes (merged)
 
   Revision 1.5  2000/08/03 13:17:26  jonas

+ 14 - 2
compiler/symdefh.inc

@@ -98,6 +98,13 @@
           defaultvalue : psym; { pconstsym }
        end;
 
+       { this is only here to override the count method,
+         which can't be used }
+       pparalinkedlist = ^tparalinkedlist;
+       tparalinkedlist = object(tlinkedlist)
+          function count:longint;
+       end;
+
        tfiletyp = (ft_text,ft_typed,ft_untyped);
 
        pfiledef = ^tfiledef;
@@ -345,7 +352,9 @@
           proctypeoption  : tproctypeoption;
           proccalloptions : tproccalloptions;
           procoptions     : tprocoptions;
-          para            : plinkedlist;
+          para            : pparalinkedlist;
+          maxparacount,
+          minparacount    : longint;
           symtablelevel   : byte;
           fpu_used        : byte;    { how many stack fpu must be empty }
           constructor init;
@@ -539,7 +548,10 @@
 
 {
   $Log$
-  Revision 1.6  2000-08-06 14:17:15  peter
+  Revision 1.7  2000-08-06 19:39:28  peter
+    * default parameters working !
+
+  Revision 1.6  2000/08/06 14:17:15  peter
     * overload fixes (merged)
 
   Revision 1.5  2000/08/03 13:17:26  jonas

+ 29 - 7
compiler/tccal.pas

@@ -474,6 +474,7 @@ implementation
 
       var
         is_const : boolean;
+        i : longint;
       begin
          { release registers! }
          { if procdefinition<>nil then we called firstpass already }
@@ -606,14 +607,19 @@ implementation
                    pd:=aktcallprocsym^.definition;
                    while assigned(pd) do
                      begin
-                        { only when the # of parameter are equal }
-                        if (pd^.para^.count=paralength) then
+                        { only when the # of parameter are supported by the
+                          procedure }
+                        if (paralength>=pd^.minparacount) and (paralength<=pd^.maxparacount) then
                           begin
                              new(hp);
                              hp^.data:=pd;
                              hp^.next:=procs;
-                             hp^.nextpara:=pparaitem(pd^.para^.first);
                              hp^.firstpara:=pparaitem(pd^.para^.first);
+                             { if not all parameters are given, then skip the
+                               default parameters }
+                             for i:=1 to pd^.maxparacount-paralength do
+                              hp^.firstpara:=pparaitem(hp^.firstpara^.next);
+                             hp^.nextpara:=hp^.firstpara;
                              procs:=hp;
                           end;
                         pd:=pd^.nextoverloaded;
@@ -1054,10 +1060,23 @@ implementation
               else
                 procinfo^.flags:=procinfo^.flags or pi_do_call;
 
-              {if (po_interrupt in p^.procdefinition^.procoptions) then
-                CGmessage1(cg_e_no_call_to_interrupt,p^.symtableprocentry^.name);}
+              { add needed default parameters }
+              if assigned(procs) and
+                 (paralength<p^.procdefinition^.maxparacount) then
+               begin
+                 { add default parameters, just read back the skipped
+                   paras starting from firstpara^.previous }
+                 pdc:=pparaitem(procs^.firstpara^.previous);
+                 while assigned(pdc) do
+                  begin
+                    if not assigned(pdc^.defaultvalue) then
+                     internalerror(751349858);
+                    p^.left:=gencallparanode(genconstsymtree(pconstsym(pdc^.defaultvalue)),p^.left);
+                    pdc:=pparaitem(pdc^.previous);
+                  end;
+               end;
+
               { work trough all parameters to insert the type conversions }
-              { !!! done now after internproc !! (PM) }
               if assigned(p^.left) then
                 begin
                    firstcallparan(p^.left,pparaitem(p^.procdefinition^.para^.first),true);
@@ -1229,7 +1248,10 @@ implementation
 end.
 {
   $Log$
-  Revision 1.3  2000-07-13 12:08:28  michael
+  Revision 1.4  2000-08-06 19:39:28  peter
+    * default parameters working !
+
+  Revision 1.3  2000/07/13 12:08:28  michael
   + patched to 1.1.0 with former 1.09patch from peter
 
   Revision 1.2  2000/07/13 11:32:51  michael

+ 50 - 1
compiler/tree.pas

@@ -305,6 +305,7 @@ unit tree;
 {$ELSE}
     function genwithnode(symtable:pwithsymtable;l,r : ptree;count : longint) : ptree;
 {$ENDIF NEWST}
+    function genconstsymtree(p:pconstsym):ptree;
 
     function getcopy(p : ptree) : ptree;
 
@@ -1452,6 +1453,51 @@ unit tree;
          gensetconstnode:=p;
       end;
 
+
+    function genconstsymtree(p:pconstsym):ptree;
+      var
+        p1  : ptree;
+        len : longint;
+        pc  : pchar;
+      begin
+        p1:=nil;
+        case p^.consttyp of
+          constint :
+            p1:=genordinalconstnode(p^.value,s32bitdef);
+          conststring :
+            begin
+              len:=p^.len;
+              if not(cs_ansistrings in aktlocalswitches) and (len>255) then
+               len:=255;
+              getmem(pc,len+1);
+              move(pchar(p^.value)^,pc^,len);
+              pc[len]:=#0;
+              p1:=genpcharconstnode(pc,len);
+            end;
+          constchar :
+            p1:=genordinalconstnode(p^.value,cchardef);
+          constreal :
+            p1:=genrealconstnode(pbestreal(p^.value)^,bestrealdef^);
+          constbool :
+            p1:=genordinalconstnode(p^.value,booldef);
+          constset :
+            p1:=gensetconstnode(pconstset(p^.value),psetdef(p^.consttype.def));
+          constord :
+            p1:=genordinalconstnode(p^.value,p^.consttype.def);
+          constpointer :
+            p1:=genpointerconstnode(p^.value,p^.consttype.def);
+          constnil :
+            p1:=genzeronode(niln);
+          constresourcestring:
+            begin
+              p1:=genloadnode(pvarsym(p),pvarsym(p)^.owner);
+              p1^.resulttype:=cansistringdef;
+            end;
+        end;
+        genconstsymtree:=p1;
+      end;
+
+
 {$ifdef extdebug}
     procedure compare_trees(oldp,p : ptree);
 
@@ -2087,7 +2133,10 @@ unit tree;
 end.
 {
   $Log$
-  Revision 1.3  2000-08-04 22:00:52  peter
+  Revision 1.4  2000-08-06 19:39:28  peter
+    * default parameters working !
+
+  Revision 1.3  2000/08/04 22:00:52  peter
     * merges from fixes
 
   Revision 1.2  2000/07/13 11:32:52  michael