Jelajahi Sumber

* register loopvarsym for fields instead of record variable
* don't allow class fields as loop var

peter 21 tahun lalu
induk
melakukan
6180168a5a
2 mengubah file dengan 77 tambahan dan 45 penghapusan
  1. 10 2
      compiler/htypechk.pas
  2. 67 43
      compiler/pstatmnt.pas

+ 10 - 2
compiler/htypechk.pas

@@ -960,6 +960,10 @@ implementation
              subscriptn :
              subscriptn :
                begin
                begin
                  gotsubscript:=true;
                  gotsubscript:=true;
+                 { loop counter? }
+                 if not(Valid_Const in opts) and
+                    (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
+                   CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
                  { a class/interface access is an implicit }
                  { a class/interface access is an implicit }
                  { dereferencing                           }
                  { dereferencing                           }
                  hp:=tsubscriptnode(hp).left;
                  hp:=tsubscriptnode(hp).left;
@@ -1030,7 +1034,7 @@ implementation
                        if not(Valid_Const in opts) and
                        if not(Valid_Const in opts) and
                           (vo_is_loop_counter in tvarsym(tloadnode(hp).symtableentry).varoptions) then
                           (vo_is_loop_counter in tvarsym(tloadnode(hp).symtableentry).varoptions) then
                          CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
                          CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
-                       { derefed pointer }  
+                       { derefed pointer }
                        if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
                        if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
                         begin
                         begin
                           { allow p^:= constructions with p is const parameter }
                           { allow p^:= constructions with p is const parameter }
@@ -1918,7 +1922,11 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.97  2004-09-13 20:28:27  peter
+  Revision 1.98  2004-09-27 15:15:52  peter
+    * register loopvarsym for fields instead of record variable
+    * don't allow class fields as loop var
+
+  Revision 1.97  2004/09/13 20:28:27  peter
     * for loop variable assignment is not allowed anymore
     * for loop variable assignment is not allowed anymore
 
 
   Revision 1.96  2004/08/22 11:24:09  peter
   Revision 1.96  2004/08/22 11:24:09  peter

+ 67 - 43
compiler/pstatmnt.pas

@@ -364,56 +364,76 @@ implementation
                 then
                 then
                MessagePos(hp.fileinfo,type_e_ordinal_expr_expected);
                MessagePos(hp.fileinfo,type_e_ordinal_expr_expected);
 
 
-             { record fields are also allowed in tp7 }
              while assigned(hp) and
              while assigned(hp) and
                    (
                    (
-                    (hp.nodetype=subscriptn) or
-                    ((hp.nodetype=vecn) and
-                     is_constintnode(tvecnode(hp).right)) or
-                    ((hp.nodetype=typeconvn) and
-                     (ttypeconvnode(hp).convtype=tc_equal))
+                    { record/object fields are allowed }
+                    (
+                     (hp.nodetype=subscriptn) and
+                     ((tsubscriptnode(hp).left.resulttype.def.deftype=recorddef) or
+                      is_object(tsubscriptnode(hp).left.resulttype.def))
+                    ) or
+                    { constant array index }
+                    (
+                     (hp.nodetype=vecn) and
+                     is_constintnode(tvecnode(hp).right)
+                    ) or
+                    { equal typeconversions }
+                    (
+                     (hp.nodetype=typeconvn) and
+                     (ttypeconvnode(hp).convtype=tc_equal)
+                    )
                    ) do
                    ) do
-               hp:=tunarynode(hp).left;
-           end;
-         if assigned(hp) and
-            (hp.nodetype=loadn) then
-           begin
-             case tloadnode(hp).symtableentry.typ of
-               varsym :
-                 begin
-                   { we need a simple loadn and the load must be in a global symtable or
-                     in the same level as the para of the current proc }
-                   if (
-                       (tloadnode(hp).symtable.symtablelevel=main_program_level) or
-                       (tloadnode(hp).symtable.symtablelevel=current_procinfo.procdef.parast.symtablelevel)
-                      ) and
-                      not(
-                          (tloadnode(hp).symtableentry.typ=varsym) and
-                          ((tvarsym(tloadnode(hp).symtableentry).varspez in [vs_var,vs_out]) or
-                           (vo_is_thread_var in tvarsym(tloadnode(hp).symtableentry).varoptions))
-                         ) then
-                     begin
-                       tvarsym(tloadnode(hp).symtableentry).varstate:=vs_used;
+               begin
+                 { Use the recordfield for loopvarsym }
+                 if not assigned(loopvarsym) and
+                    (hp.nodetype=subscriptn) then
+                   loopvarsym:=tsubscriptnode(hp).vs;
+                 hp:=tunarynode(hp).left;
+               end;
 
 
-                       { Assigning for-loop variable is only allowed in tp7 }
-                       if not(m_tp7 in aktmodeswitches) then
+             if assigned(hp) and
+                (hp.nodetype=loadn) then
+               begin
+                 case tloadnode(hp).symtableentry.typ of
+                   varsym :
+                     begin
+                       { we need a simple loadn and the load must be in a global symtable or
+                         in the same level as the para of the current proc }
+                       if (
+                           (tloadnode(hp).symtable.symtablelevel=main_program_level) or
+                           (tloadnode(hp).symtable.symtablelevel=current_procinfo.procdef.parast.symtablelevel)
+                          ) and
+                          not(
+                              (tloadnode(hp).symtableentry.typ=varsym) and
+                              ((tvarsym(tloadnode(hp).symtableentry).varspez in [vs_var,vs_out]) or
+                               (vo_is_thread_var in tvarsym(tloadnode(hp).symtableentry).varoptions))
+                             ) then
                          begin
                          begin
-                           loopvarsym:=tvarsym(tloadnode(hp).symtableentry);
-                           include(loopvarsym.varoptions,vo_is_loop_counter);
-                         end;
-                     end
+                           tvarsym(tloadnode(hp).symtableentry).varstate:=vs_used;
+
+                           { Assigning for-loop variable is only allowed in tp7 }
+                           if not(m_tp7 in aktmodeswitches) then
+                             begin
+                               if not assigned(loopvarsym) then
+                                 loopvarsym:=tvarsym(tloadnode(hp).symtableentry);
+                               include(loopvarsym.varoptions,vo_is_loop_counter);
+                             end;
+                         end
+                       else
+                         MessagePos(hp.fileinfo,type_e_illegal_count_var);
+                     end;
+                   typedconstsym :
+                     begin
+                       { Bad programming, only allowed in tp7 mode }
+                       if not(m_tp7 in aktmodeswitches) then
+                         MessagePos(hp.fileinfo,type_e_illegal_count_var);
+                     end;
                    else
                    else
                      MessagePos(hp.fileinfo,type_e_illegal_count_var);
                      MessagePos(hp.fileinfo,type_e_illegal_count_var);
                  end;
                  end;
-               typedconstsym :
-                 begin
-                   { Bad programming, only allowed in tp7 mode }
-                   if not(m_tp7 in aktmodeswitches) then
-                     MessagePos(hp.fileinfo,type_e_illegal_count_var);
-                 end;
-               else
-                 MessagePos(hp.fileinfo,type_e_illegal_count_var);
-             end;
+               end
+             else
+               MessagePos(tassignmentnode(p_e).left.fileinfo,type_e_illegal_count_var);
            end
            end
          else
          else
            Message(parser_e_illegal_expression);
            Message(parser_e_illegal_expression);
@@ -1186,7 +1206,11 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.140  2004-09-26 17:45:30  peter
+  Revision 1.141  2004-09-27 15:15:52  peter
+    * register loopvarsym for fields instead of record variable
+    * don't allow class fields as loop var
+
+  Revision 1.140  2004/09/26 17:45:30  peter
     * simple regvar support, not yet finished
     * simple regvar support, not yet finished
 
 
   Revision 1.139  2004/09/21 17:25:12  peter
   Revision 1.139  2004/09/21 17:25:12  peter