[fpc-devel] Scoped VMTDefs

Blaise at blaise.ru Blaise at blaise.ru
Sat Aug 3 15:54:58 CEST 2019


Before the main course, I offer the attached refactorings for trecorddef.create_global_internal:
1) streamline insertions into the symtable;
2) avoid shadow-copying of the parameter "n", which is now const

-- 
βþ
-------------- next part --------------
# HG changeset patch
# User Blaise
# Date 1564833600 -10800
#      Sat Aug 03 15:00:00 2019 +0300
# Node ID 2873de0e4992db706b03f52c007f6ef7b39f5f8a
# Parent  b7e97cefc21b2ffdfb22c37abb9bbae8f8a8e7cc
= trecorddef.create_global_internal: streamline insertions into the symtable

diff -r b7e97cefc21b -r 2873de0e4992 symdef.pas
--- a/symdef.pas	Fri Aug 02 19:35:25 2019 +0300
+++ b/symdef.pas	Sat Aug 03 15:00:00 2019 +0300
@@ -4786,6 +4786,7 @@
     constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin: shortint);
       var
         oldsymtablestack: tsymtablestack;
+        where: tsymtable;
         ts: ttypesym;
         definedname: boolean;
       begin
@@ -4802,27 +4803,19 @@
         symtable.defowner:=self;
         isunion:=false;
         inherited create(n,recorddef,true);
+        where:=current_module.localsymtable;
+        if not assigned(where) then
+          where:=current_module.globalsymtable;
+        where.insertdef(self);
         { if we specified a name, then we'll probably want to look up the
           type again by name too -> create typesym }
-        ts:=nil;
         if definedname then
           begin
             ts:=ctypesym.create(n,self,true);
             { avoid hints about unused types (these may only be used for
               typed constant data) }
             ts.increfcount;
-          end;
-        if assigned(current_module.localsymtable) then
-          begin
-            current_module.localsymtable.insertdef(self);
-            if definedname then
-              current_module.localsymtable.insert(ts);
-          end
-        else
-          begin
-            current_module.globalsymtable.insertdef(self);
-            if definedname then
-              current_module.globalsymtable.insert(ts);
+            where.insert(ts);
           end;
         symtablestack:=oldsymtablestack;
         { don't create RTTI for internal types, these are not exported }
-------------- next part --------------
# HG changeset patch
# User Blaise
# Date 1564838933 -10800
#      Sat Aug 03 16:28:53 2019 +0300
# Node ID 49cbbb22f46dc471223290b1b24f312f24d65e76
# Parent  2873de0e4992db706b03f52c007f6ef7b39f5f8a
= trecorddef.create_global_internal: avoid shadow-copying of the parameter "n", which is now const

diff -r 2873de0e4992 -r 49cbbb22f46d symdef.pas
--- a/symdef.pas	Sat Aug 03 15:00:00 2019 +0300
+++ b/symdef.pas	Sat Aug 03 16:28:53 2019 +0300
@@ -374,7 +374,7 @@
           variantrecdesc : pvariantrecdesc;
           isunion       : boolean;
           constructor create(const n:string; p:TSymtable);virtual;
-          constructor create_global_internal(n: string; packrecords, recordalignmin: shortint); virtual;
+          constructor create_global_internal(const n: string; packrecords, recordalignmin: shortint); virtual;
           function add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
           procedure add_fields_from_deflist(fieldtypes: tfplist);
           constructor ppuload(ppufile:tcompilerppufile);
@@ -4783,33 +4783,37 @@
       end;
 
 
-    constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin: shortint);
+    constructor trecorddef.create_global_internal(const n: string; packrecords, recordalignmin: shortint);
       var
+        name: string; pname: pshortstring;
         oldsymtablestack: tsymtablestack;
         where: tsymtable;
         ts: ttypesym;
-        definedname: boolean;
       begin
         { construct name }
-        definedname:=n<>'';
-        if not definedname then
-          n:='$InternalRec'+tostr(current_module.deflist.count);
+        if n<>'' then
+          pname := @n
+        else
+          begin
+            name:='$InternalRec'+tostr(current_module.deflist.count);
+            pname:=@name;
+          end;
         oldsymtablestack:=symtablestack;
         { do not simply push/pop current_module.localsymtable, because
           that can have side-effects (e.g., it removes helpers) }
         symtablestack:=nil;
 
-        symtable:=trecordsymtable.create(n,packrecords,recordalignmin);
+        symtable:=trecordsymtable.create(pname^,packrecords,recordalignmin);
         symtable.defowner:=self;
         isunion:=false;
-        inherited create(n,recorddef,true);
+        inherited create(pname^,recorddef,true);//!
         where:=current_module.localsymtable;
         if not assigned(where) then
           where:=current_module.globalsymtable;
         where.insertdef(self);
         { if we specified a name, then we'll probably want to look up the
           type again by name too -> create typesym }
-        if definedname then
+        if n<>'' then
           begin
             ts:=ctypesym.create(n,self,true);
             { avoid hints about unused types (these may only be used for


More information about the fpc-devel mailing list