2
0
Эх сурвалжийг харах

+ Patch from peter to support initial values for local variables

michael 22 жил өмнө
parent
commit
fbd28675c9

+ 24 - 9
compiler/pdecvar.pas

@@ -317,14 +317,26 @@ implementation
                     Message(parser_e_initialized_only_one_var);
                     Message(parser_e_initialized_only_one_var);
                   if is_threadvar then
                   if is_threadvar then
                     Message(parser_e_initialized_not_for_threadvar);
                     Message(parser_e_initialized_not_for_threadvar);
-                  tconstsym:=ttypedconstsym.createtype(vs.realname,tt,true);
-                  tconstsym.fileinfo:=vs.fileinfo;
-                  symtablestack.replace(vs,tconstsym);
-                  vs.free;
-                  symtablestack.insertconstdata(tconstsym);
-                  consume(_EQUAL);
-                  readtypedconst(tt,tconstsym,true);
-                  symdone:=true;
+                  if symtablestack.symtabletype=localsymtable then
+                   begin
+                     consume(_EQUAL);
+                     tconstsym:=ttypedconstsym.createtype('default'+vs.realname,tt,false);
+                     vs.defaultconstsym:=tconstsym;
+                     symtablestack.insert(tconstsym);
+                     symtablestack.insertconstdata(tconstsym);
+                     readtypedconst(tt,tconstsym,false);
+                   end
+                  else
+                   begin
+                     tconstsym:=ttypedconstsym.createtype(vs.realname,tt,true);
+                     tconstsym.fileinfo:=vs.fileinfo;
+                     symtablestack.replace(vs,tconstsym);
+                     vs.free;
+                     symtablestack.insertconstdata(tconstsym);
+                     consume(_EQUAL);
+                     readtypedconst(tt,tconstsym,true);
+                     symdone:=true;
+                   end; 
                end;
                end;
              { if the symbol is not completely handled, then try to parse the
              { if the symbol is not completely handled, then try to parse the
                hint directives }
                hint directives }
@@ -597,7 +609,10 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.43  2002-12-27 15:22:20  peter
+  Revision 1.44  2003-01-02 11:14:02  michael
+  + Patch from peter to support initial values for local variables
+
+  Revision 1.43  2002/12/27 15:22:20  peter
     * don't allow initialized threadvars
     * don't allow initialized threadvars
 
 
   Revision 1.42  2002/12/07 14:04:59  carl
   Revision 1.42  2002/12/07 14:04:59  carl

+ 31 - 3
compiler/psub.pas

@@ -51,7 +51,7 @@ implementation
        ppu,fmodule,
        ppu,fmodule,
        { pass 1 }
        { pass 1 }
        node,
        node,
-       nbas,
+       nbas,nld,
        pass_1,
        pass_1,
     {$ifdef state_tracking}
     {$ifdef state_tracking}
        nstate,
        nstate,
@@ -80,6 +80,27 @@ implementation
                       PROCEDURE/FUNCTION BODY PARSING
                       PROCEDURE/FUNCTION BODY PARSING
 ****************************************************************************}
 ****************************************************************************}
 
 
+    procedure initializevars(p:tnamedindexitem;arg:pointer);
+      var
+        b : tblocknode;
+      begin
+        if tsym(p).typ<>varsym then
+         exit;
+        with tvarsym(p) do
+         begin
+           if assigned(defaultconstsym) then
+            begin
+              b:=tblocknode(arg);
+              b.left:=cstatementnode.create(
+                        cassignmentnode.create(
+                            cloadnode.create(tsym(p),tsym(p).owner),
+                            cloadnode.create(defaultconstsym,defaultconstsym.owner)),
+                        b.left);
+            end;
+         end;
+      end;
+
+
     function block(islibrary : boolean) : tnode;
     function block(islibrary : boolean) : tnode;
       var
       var
          storepos : tfileposinfo;
          storepos : tfileposinfo;
@@ -168,7 +189,11 @@ implementation
                 end;
                 end;
             end
             end
          else
          else
-            block:=statement_block(_BEGIN);
+            begin
+               block:=statement_block(_BEGIN);
+               if symtablestack.symtabletype=localsymtable then
+                 symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
+            end;   
       end;
       end;
 
 
 
 
@@ -818,7 +843,10 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.85  2002-12-29 18:59:34  peter
+  Revision 1.86  2003-01-02 11:14:02  michael
+  + Patch from peter to support initial values for local variables
+
+  Revision 1.85  2002/12/29 18:59:34  peter
     * fixed parsing of declarations before asm statement
     * fixed parsing of declarations before asm statement
 
 
   Revision 1.84  2002/12/29 18:25:18  peter
   Revision 1.84  2002/12/29 18:25:18  peter

+ 7 - 1
compiler/symsym.pas

@@ -175,6 +175,7 @@ interface
           address       : longint;
           address       : longint;
           localvarsym   : tvarsym;
           localvarsym   : tvarsym;
           highvarsym    : tvarsym;
           highvarsym    : tvarsym;
+          defaultconstsym : tsym;
           vartype       : ttype;
           vartype       : ttype;
           varoptions    : tvaroptions;
           varoptions    : tvaroptions;
           reg           : tregister; { if reg<>R_NO, then the variable is an register variable }
           reg           : tregister; { if reg<>R_NO, then the variable is an register variable }
@@ -1598,6 +1599,7 @@ implementation
          address:=0;
          address:=0;
          localvarsym:=nil;
          localvarsym:=nil;
          highvarsym:=nil;
          highvarsym:=nil;
+         defaultconstsym:=nil;
          refs:=0;
          refs:=0;
          varstate:=vs_used;
          varstate:=vs_used;
          varoptions:=[];
          varoptions:=[];
@@ -1641,6 +1643,7 @@ implementation
          address:=ppufile.getlongint;
          address:=ppufile.getlongint;
          localvarsym:=nil;
          localvarsym:=nil;
          highvarsym:=nil;
          highvarsym:=nil;
+         defaultconstsym:=nil;
          ppufile.gettype(vartype);
          ppufile.gettype(vartype);
          ppufile.getsmallset(varoptions);
          ppufile.getsmallset(varoptions);
          if (vo_is_C_var in varoptions) then
          if (vo_is_C_var in varoptions) then
@@ -2566,7 +2569,10 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.88  2003-01-01 22:51:03  peter
+  Revision 1.89  2003-01-02 11:14:02  michael
+  + Patch from peter to support initial values for local variables
+
+  Revision 1.88  2003/01/01 22:51:03  peter
     * high value insertion changed so it works also when 2 parameters
     * high value insertion changed so it works also when 2 parameters
       are passed
       are passed