Sfoglia il codice sorgente

* split tstringtype for stringdef and stringconstnode
to prevent wrong typecasts

git-svn-id: trunk@2276 -

peter 19 anni fa
parent
commit
45176ea2a8

+ 1 - 3
compiler/defcmp.pas

@@ -305,9 +305,7 @@ implementation
                      { Constant string }
                      if (fromtreetype=stringconstn) then
                       begin
-                        { we can change the stringconst node }
-                        if (tstringdef(def_from).string_typ=st_conststring) or
-                           (tstringdef(def_from).string_typ=tstringdef(def_to).string_typ) then
+                        if (tstringdef(def_from).string_typ=tstringdef(def_to).string_typ) then
                           eq:=te_equal
                         else
                          begin

+ 5 - 5
compiler/nadd.pas

@@ -473,8 +473,8 @@ implementation
         { this isn't veryy efficient, but I don't think    }
         { that it does matter that much (FK)               }
         if (lt=stringconstn) and (rt=stringconstn) and
-          (tstringconstnode(left).st_type=st_widestring) and
-          (tstringconstnode(right).st_type=st_widestring) then
+          (tstringconstnode(left).cst_type=cst_widestring) and
+          (tstringconstnode(right).cst_type=cst_widestring) then
           begin
              initwidestring(ws1);
              initwidestring(ws2);
@@ -558,7 +558,7 @@ implementation
           begin
              case nodetype of
                 addn :
-                  t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2,st_conststring);
+                  t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
                 ltn :
                   t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype,true);
                 lten :
@@ -1559,8 +1559,8 @@ implementation
               { generate better code for comparison with empty string, we
                 only need to compare the length with 0 }
               if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
-                 (((left.nodetype=stringconstn) and (str_length(left)=0)) or
-                  ((right.nodetype=stringconstn) and (str_length(right)=0))) then
+                 (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
+                  ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
                 begin
                   { switch so that the constant is always on the right }
                   if left.nodetype = stringconstn then

+ 3 - 3
compiler/ncal.pas

@@ -267,7 +267,7 @@ type
                       is_object(p.resulttype.def);
             { avoid refcount increase }
             usevoidpointer:=is_interface(p.resulttype.def);
- 
+
             if usederef then
               htype.setdef(tpointerdef.create(p.resulttype))
             else
@@ -340,7 +340,7 @@ type
               { passing a string to an array of char }
               if (p.nodetype=stringconstn) then
                 begin
-                  len:=str_length(p);
+                  len:=tstringconstnode(p).len;
                   if len>0 then
                    dec(len);
                 end
@@ -396,7 +396,7 @@ type
                  { passing a string to an array of char }
                  if (p.nodetype=stringconstn) then
                    begin
-                     len:=str_length(p);
+                     len:=tstringconstnode(p).len;
                      if len>0 then
                       dec(len);
                    end

+ 11 - 12
compiler/ncgcnv.pas

@@ -135,23 +135,24 @@ interface
         hr : treference;
 
       begin
+         if left.nodetype<>stringconstn then
+           internalerror(200601131);
          location_reset(location,LOC_REGISTER,OS_ADDR);
-         case tstringdef(left.resulttype.def).string_typ of
-           st_conststring :
+         case tstringconstnode(left).cst_type of
+           cst_conststring :
              begin
                location.register:=cg.getaddressregister(exprasmlist);
                cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
              end;
-           st_shortstring :
+           cst_shortstring :
              begin
                inc(left.location.reference.offset);
                location.register:=cg.getaddressregister(exprasmlist);
                cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
              end;
-           st_ansistring :
+           cst_ansistring :
              begin
-               if (left.nodetype=stringconstn) and
-                  (str_length(left)=0) then
+               if tstringconstnode(left).len=0 then
                 begin
                   reference_reset(hr);
                   hr.symbol:=objectlibrary.newasmsymbol('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
@@ -164,15 +165,14 @@ interface
                   cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.register);
                 end;
              end;
-           st_longstring:
+           cst_longstring:
              begin
                {!!!!!!!}
                internalerror(8888);
              end;
-           st_widestring:
+           cst_widestring:
              begin
-               if (left.nodetype=stringconstn) and
-                  (str_length(left)=0) then
+               if tstringconstnode(left).len=0 then
                 begin
                   reference_reset(hr);
                   hr.symbol:=objectlibrary.newasmsymbol('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
@@ -200,8 +200,7 @@ interface
 
     procedure tcgtypeconvnode.second_string_to_chararray;
       begin
-        if (left.nodetype = stringconstn) and
-           (tstringdef(left.resulttype.def).string_typ=st_conststring) then
+        if is_chararray(left.resulttype.def) then
           begin
             location_copy(location,left.location);
             exit;

+ 11 - 12
compiler/ncgcon.pas

@@ -85,7 +85,6 @@ implementation
          hp1 : tai;
          lastlabel : tasmlabel;
          realait : taitype;
-         value_real_sign, hp1_sign: pbyte;
 {$ifdef ARM}
          hiloswapped : boolean;
 {$endif ARM}
@@ -249,7 +248,7 @@ implementation
          i,mylength  : longint;
       begin
          { for empty ansistrings we could return a constant 0 }
-         if (st_type in [st_ansistring,st_widestring]) and (len=0) then
+         if (cst_type in [cst_ansistring,cst_widestring]) and (len=0) then
           begin
             location_reset(location,LOC_CONSTANT,OS_ADDR);
             location.value:=0;
@@ -285,8 +284,8 @@ implementation
                                (lastlabel<>nil) and
                                (tai_string(hp1).len=mylength) then
                               begin
-                                 case st_type of
-                                   st_conststring:
+                                 case cst_type of
+                                   cst_conststring:
                                      begin
                                        j:=0;
                                        same_string:=true;
@@ -303,7 +302,7 @@ implementation
                                              end;
                                          end;
                                      end;
-                                   st_shortstring:
+                                   cst_shortstring:
                                      begin
                                        { if shortstring then check the length byte first and
                                          set the start index to 1 }
@@ -325,8 +324,8 @@ implementation
                                              end;
                                          end;
                                      end;
-                                   st_ansistring,
-                                   st_widestring :
+                                   cst_ansistring,
+                                   cst_widestring :
                                      begin
                                        { before the string the following sequence must be found:
                                          <label>
@@ -386,8 +385,8 @@ implementation
                    new_section(asmlist[al_typedconsts],sec_rodata,lastlabel.name,const_align(sizeof(aint)));
                    asmlist[al_typedconsts].concat(Tai_label.Create(lastlabel));
                    { generate an ansi string ? }
-                   case st_type of
-                      st_ansistring:
+                   case cst_type of
+                      cst_ansistring:
                         begin
                            { an empty ansi string is nil! }
                            if len=0 then
@@ -410,7 +409,7 @@ implementation
                                 lab_str:=l2;
                              end;
                         end;
-                      st_widestring:
+                      cst_widestring:
                         begin
                            { an empty wide string is nil! }
                            if len=0 then
@@ -436,7 +435,7 @@ implementation
                                 lab_str:=l2;
                              end;
                         end;
-                      st_shortstring:
+                      cst_shortstring:
                         begin
                           { truncate strings larger than 255 chars }
                           if len>255 then
@@ -450,7 +449,7 @@ implementation
                           pc[l+1]:=#0;
                           asmlist[al_typedconsts].concat(Tai_string.Create_pchar(pc,l+2));
                         end;
-                      st_conststring:
+                      cst_conststring:
                         begin
                           { include terminating zero }
                           getmem(pc,len+1);

+ 1 - 1
compiler/ncginl.pas

@@ -204,7 +204,7 @@ implementation
        { First call secondpass() before we can push the parameters, otherwise
          parameters allocated in the registers can be destroyed }
        { generate filename string parameter }
-       hp2:=cstringconstnode.createstr(current_module.sourcefiles.get_file_name(aktfilepos.fileindex),st_shortstring);
+       hp2:=cstringconstnode.createstr(current_module.sourcefiles.get_file_name(aktfilepos.fileindex));
        firstpass(hp2);
        secondpass(hp2);
        if codegenerror then

+ 5 - 2
compiler/ncnv.pas

@@ -716,7 +716,7 @@ implementation
             arrsize := highrange-lowrange+1;
           end;
         if (left.nodetype = stringconstn) and
-           (tstringdef(left.resulttype.def).string_typ=st_conststring) then
+           (tstringconstnode(left).cst_type=cst_conststring) then
            begin
              { if the array of char is large enough we can use the string
                constant directly. This is handled in ncgcnv }
@@ -805,7 +805,10 @@ implementation
                  donewidestring(ws);
                end
               else
-               hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
+                begin
+                  hp:=cstringconstnode.createstr(chr(tordconstnode(left).value));
+                  tstringconstnode(hp).changestringtype(resulttype);
+                end;
               result:=hp;
            end
          else

+ 38 - 33
compiler/ncon.pas

@@ -85,13 +85,21 @@ interface
        end;
        tpointerconstnodeclass = class of tpointerconstnode;
 
+       tconststringtype = (
+         cst_conststring,
+         cst_shortstring,
+         cst_longstring,
+         cst_ansistring,
+         cst_widestring
+       );
+
        tstringconstnode = class(tnode)
           value_str : pchar;
           len     : longint;
           lab_str : tasmlabel;
-          st_type : tstringtype;
-          constructor createstr(const s : string;st:tstringtype);virtual;
-          constructor createpchar(s : pchar;l : longint;st:tstringtype);virtual;
+          cst_type : tconststringtype;
+          constructor createstr(const s : string);virtual;
+          constructor createpchar(s : pchar;l : longint);virtual;
           constructor createwstr(w : pcompilerwidestring);virtual;
           constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
           procedure ppuwrite(ppufile:tcompilerppufile);override;
@@ -158,7 +166,6 @@ interface
     { some helper routines }
     function get_ordinal_value(p : tnode) : TConstExprInt;
     function is_constresourcestringnode(p : tnode) : boolean;
-    function str_length(p : tnode) : longint;
     function is_emptyset(p : tnode):boolean;
     function genconstsymtree(p : tconstsym) : tnode;
 
@@ -212,12 +219,6 @@ implementation
       end;
 
 
-    function str_length(p : tnode) : longint;
-
-      begin
-         str_length:=tstringconstnode(p).len;
-      end;
-
     function is_emptyset(p : tnode):boolean;
       begin
         is_emptyset:=(p.nodetype=setconstn) and
@@ -241,7 +242,7 @@ implementation
               getmem(pc,len+1);
               move(pchar(p.value.valueptr)^,pc^,len);
               pc[len]:=#0;
-              p1:=cstringconstnode.createpchar(pc,len,st_conststring);
+              p1:=cstringconstnode.createpchar(pc,len);
             end;
           constreal :
             p1:=crealconstnode.create(pbestreal(p.value.valueptr)^,pbestrealtype^);
@@ -518,7 +519,7 @@ implementation
                              TSTRINGCONSTNODE
 *****************************************************************************}
 
-    constructor tstringconstnode.createstr(const s : string;st:tstringtype);
+    constructor tstringconstnode.createstr(const s : string);
       var
          l : longint;
       begin
@@ -530,7 +531,7 @@ implementation
          move(s[1],value_str^,l);
          value_str[l]:=#0;
          lab_str:=nil;
-         st_type:=st;
+         cst_type:=cst_conststring;
       end;
 
 
@@ -541,23 +542,23 @@ implementation
          initwidestring(pcompilerwidestring(value_str));
          copywidestring(w,pcompilerwidestring(value_str));
          lab_str:=nil;
-         st_type:=st_widestring;
+         cst_type:=cst_widestring;
       end;
 
 
-    constructor tstringconstnode.createpchar(s : pchar;l : longint;st:tstringtype);
+    constructor tstringconstnode.createpchar(s : pchar;l : longint);
       begin
          inherited create(stringconstn);
          len:=l;
          value_str:=s;
-         st_type:=st;
+         cst_type:=cst_conststring;
          lab_str:=nil;
       end;
 
 
     destructor tstringconstnode.destroy;
       begin
-        if st_type=st_widestring then
+        if cst_type=cst_widestring then
           donewidestring(pcompilerwidestring(value_str))
         else
           ansistringdispose(value_str,len);
@@ -570,9 +571,9 @@ implementation
         pw : pcompilerwidestring;
       begin
         inherited ppuload(t,ppufile);
-        st_type:=tstringtype(ppufile.getbyte);
+        cst_type:=tconststringtype(ppufile.getbyte);
         len:=ppufile.getlongint;
-        if st_type=st_widestring then
+        if cst_type=cst_widestring then
           begin
             initwidestring(pw);
             setlengthwidestring(pw,len);
@@ -592,9 +593,9 @@ implementation
     procedure tstringconstnode.ppuwrite(ppufile:tcompilerppufile);
       begin
         inherited ppuwrite(ppufile);
-        ppufile.putbyte(byte(st_type));
+        ppufile.putbyte(byte(cst_type));
         ppufile.putlongint(len);
-        if st_type=st_widestring then
+        if cst_type=cst_widestring then
           ppufile.putdata(pcompilerwidestring(value_str)^.data,len*sizeof(tcompilerwidechar))
         else
           ppufile.putdata(value_str^,len);
@@ -622,10 +623,10 @@ implementation
 
       begin
          n:=tstringconstnode(inherited _getcopy);
-         n.st_type:=st_type;
+         n.cst_type:=cst_type;
          n.len:=len;
          n.lab_str:=lab_str;
-         if st_type=st_widestring then
+         if cst_type=cst_widestring then
            begin
              initwidestring(pcompilerwidestring(n.value_str));
              copywidestring(pcompilerwidestring(value_str),pcompilerwidestring(n.value_str));
@@ -640,8 +641,8 @@ implementation
         l : aint;
       begin
         result:=nil;
-        case st_type of
-          st_conststring :
+        case cst_type of
+          cst_conststring :
             begin
               { handle and store as array[0..len-1] of char }
               if len>0 then
@@ -651,13 +652,13 @@ implementation
               resulttype.setdef(tarraydef.create(0,l,s32inttype));
               tarraydef(resulttype.def).setelementtype(cchartype);
             end;
-          st_shortstring :
+          cst_shortstring :
             resulttype:=cshortstringtype;
-          st_ansistring :
+          cst_ansistring :
             resulttype:=cansistringtype;
-          st_widestring :
+          cst_widestring :
             resulttype:=cwidestringtype;
-          st_longstring :
+          cst_longstring :
             resulttype:=clongstringtype;
         end;
       end;
@@ -665,7 +666,7 @@ implementation
     function tstringconstnode.pass_1 : tnode;
       begin
         result:=nil;
-        if (st_type in [st_ansistring,st_widestring]) and
+        if (cst_type in [cst_ansistring,cst_widestring]) and
            (len=0) then
          expectloc:=LOC_CONSTANT
         else
@@ -698,6 +699,10 @@ implementation
 
 
     procedure tstringconstnode.changestringtype(const newtype:ttype);
+      const
+        st2cst : array[tstringtype] of tconststringtype = (
+          cst_shortstring,cst_longstring,cst_ansistring,cst_widestring
+        );
       var
         pw : pcompilerwidestring;
         pc : pchar;
@@ -706,7 +711,7 @@ implementation
           internalerror(200510011);
         { convert ascii 2 unicode }
         if (tstringdef(newtype.def).string_typ=st_widestring) and
-           (st_type<>st_widestring) then
+           (cst_type<>cst_widestring) then
           begin
             initwidestring(pw);
             ascii2unicode(value_str,len,pw);
@@ -715,7 +720,7 @@ implementation
           end
         else
           { convert unicode 2 ascii }
-          if (st_type=st_widestring) and
+          if (cst_type=cst_widestring) and
             (tstringdef(newtype.def).string_typ<>st_widestring) then
             begin
               pw:=pcompilerwidestring(value_str);
@@ -724,7 +729,7 @@ implementation
               donewidestring(pw);
               value_str:=pc;
             end;
-        st_type:=tstringdef(newtype.def).string_typ;
+        cst_type:=st2cst[tstringdef(newtype.def).string_typ];
         resulttype:=newtype;
       end;
 

+ 1 - 1
compiler/nmem.pas

@@ -649,7 +649,7 @@ implementation
          { implicitly convert stringconstant to stringdef,
            see tbs/tb0476.pp for a test }
          if (left.nodetype=stringconstn) and
-            (tstringconstnode(left).st_type=st_conststring) then
+            (tstringconstnode(left).cst_type=cst_conststring) then
            begin
              if tstringconstnode(left).len>255 then
                inserttypeconv(left,cansistringtype)

+ 3 - 3
compiler/pexpr.pas

@@ -808,7 +808,7 @@ implementation
               else
                begin
                  { then insert an empty string }
-                 p2:=cstringconstnode.createstr('',st_conststring);
+                 p2:=cstringconstnode.createstr('');
                end;
               statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
               consume(_RKLAMMER);
@@ -1424,7 +1424,7 @@ implementation
                           getmem(pc,len+1);
                           move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
                           pc[len]:=#0;
-                          p1:=cstringconstnode.createpchar(pc,len,st_conststring);
+                          p1:=cstringconstnode.createpchar(pc,len);
                         end;
                       constwstring :
                         p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
@@ -2255,7 +2255,7 @@ implementation
 
            _CSTRING :
              begin
-               p1:=cstringconstnode.createstr(pattern,st_conststring);
+               p1:=cstringconstnode.createstr(pattern);
                consume(_CSTRING);
              end;
 

+ 1 - 1
compiler/ptconst.pas

@@ -372,7 +372,7 @@ implementation
                         { convert to widestring stringconstn }
                         inserttypeconv(p,cwidestringtype);
                         if (p.nodetype=stringconstn) and
-                           (tstringconstnode(p).st_type=st_widestring) then
+                           (tstringconstnode(p).cst_type=cst_widestring) then
                          begin
                            pw:=pcompilerwidestring(tstringconstnode(p).value_str);
                            for i:=0 to tstringconstnode(p).len-1 do

+ 0 - 1
compiler/symconst.pas

@@ -187,7 +187,6 @@ type
 
   { string types }
   tstringtype = (
-    st_conststring,
     st_shortstring,
     st_longstring,
     st_ansistring,

+ 2 - 2
compiler/symdef.pas

@@ -1261,7 +1261,7 @@ implementation
 
     function tstringdef.stringtypname:string;
       const
-        typname:array[tstringtype] of string[8]=('',
+        typname:array[tstringtype] of string[8]=(
           'shortstr','longstr','ansistr','widestr'
         );
       begin
@@ -1298,7 +1298,7 @@ implementation
 
     function tstringdef.gettypename : string;
       const
-         names : array[tstringtype] of string[20] = ('',
+         names : array[tstringtype] of string[10] = (
            'ShortString','LongString','AnsiString','WideString');
       begin
          gettypename:=names[string_typ];