Patchwork ObjC: hide encoding obstacks inside objc-encoding.c

login
register
mail settings
Submitter Nicola Pero
Date July 10, 2011, 4:59 p.m.
Message ID <8509173B-10D8-4C6C-BD30-A9E2D6660201@meta-innovation.com>
Download mbox | patch
Permalink /patch/104064/
State New
Headers show

Comments

Nicola Pero - July 10, 2011, 4:59 p.m.
This Objective-C patch does an obvious cleanup of the encoding code internal API, by hiding
the obstacks used to create the encoding strings inside objc-encoding.c.  This provides
a cleaner, simpler API, and improves code modularity.

In practice, the patch makes the following changes:

 * have objc-act.c use the new objc_encoding_init() instead of setting up the obstacks directly;

 * improve encode_field_decl() to take only the field_decl as argument, and return the encoding
    string as identifier instead of requiring callers to access and manipulate the obstacks directly.

 * consequent cleanups, including removing dependency of various files from obstack.h.

Ok to commit ?

Thanks
Mike Stump - July 11, 2011, 1:16 a.m.
On Jul 10, 2011, at 9:59 AM, Nicola Pero wrote:
> This Objective-C patch does an obvious cleanup of the encoding code internal API, by hiding
> the obstacks used to create the encoding strings inside objc-encoding.c.  This provides
> a cleaner, simpler API, and improves code modularity.

> Ok to commit ?

Ok.

Patch

Index: objc/objc-encoding.c
===================================================================
--- objc/objc-encoding.c        (revision 176090)
+++ objc/objc-encoding.c        (working copy)
@@ -53,18 +53,24 @@  along with GCC; see the file COPYING3.  If not see
 /* Set up for use of obstacks.  */
 #include "obstack.h"
 
-/* This obstack is used to accumulate the encoding of a data type.
-   TODO: Make this static.  */
-struct obstack util_obstack;
+/* This obstack is used to accumulate the encoding of a data type.  */
+static struct obstack util_obstack;
 
 /* This points to the beginning of obstack contents, so we can free
-   the whole contents.  TODO: Make this static.  */
-char *util_firstobj;
+   the whole contents.  */
+static char *util_firstobj;
 
+void objc_encoding_init (void)
+{
+  gcc_obstack_init (&util_obstack);
+  util_firstobj = (char *) obstack_finish (&util_obstack);
+}
+
 int generating_instance_variables = 0;
 
 static void encode_type_qualifiers (tree);
 static void encode_type (tree, int, int);
+static void encode_field (tree field_decl, int curtype, int format);
 
 static tree
 objc_method_parm_type (tree type)
@@ -470,7 +476,7 @@  encode_aggregate_fields (tree type, bool pointed_t
          obstack_1grow (&util_obstack, '"');
         }
 
-      encode_field_decl (field, curtype, format);
+      encode_field (field, curtype, format);
     }
 }
 
@@ -802,8 +808,8 @@  encode_gnu_bitfield (int position, tree type, int
   obstack_grow (&util_obstack, buffer, strlen (buffer));
 }
 
-void
-encode_field_decl (tree field_decl, int curtype, int format)
+static void
+encode_field (tree field_decl, int curtype, int format)
 {
 #ifdef OBJCPLUS
   /* C++ static members, and things that are not fields at all,
@@ -828,6 +834,25 @@  encode_gnu_bitfield (int position, tree type, int
     encode_type (TREE_TYPE (field_decl), curtype, format);
 }
 
+tree
+encode_field_decl (tree field_decl)
+{
+  tree result;
+
+  encode_field (field_decl,
+               obstack_object_size (&util_obstack),
+               OBJC_ENCODE_DONT_INLINE_DEFS);
+  
+  /* Null terminate string.  */
+  obstack_1grow (&util_obstack, 0);
+
+  /* Get identifier for the string.  */
+  result = get_identifier (XOBFINISH (&util_obstack, char *));
+  obstack_free (&util_obstack, util_firstobj);
+
+  return result;
+}
+
 /* This routine encodes the attribute of the input PROPERTY according
    to following formula:
 
Index: objc/ChangeLog
===================================================================
--- objc/ChangeLog      (revision 176090)
+++ objc/ChangeLog      (working copy)
@@ -1,3 +1,30 @@ 
+2011-07-10  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+       * objc-encoding.h (obstack.h): Do not include.
+       (util_obstack, util_firstobj): Do not declare.
+       (encode_field_decl): Updated prototype to return a tree and take a
+       single tree argument.  Updated comments.
+       * objc-encoding.c (util_obstack, util_firstobj): Made static.
+       (objc_encoding_init): New.
+       (encode_field_decl): Existing function renamed to encode_field and
+       made static.  New encode_field_decl wrapper function added.
+       (encode_aggregate_fields): Update call to encode_field_decl to
+       call encode_field.
+       * objc-next-runtime-abi-02.c (obstack.h): Do not include.
+       (util_obstack, util_firstobj): Do not declare.
+       (build_v2_ivar_list_initializer): Updated call to
+       encode_field_decl.
+       * objc-runtime-shared-support.c (obstack.h): Do not include.
+       (util_obstack, util_firstobj): Do not declare.
+       (build_ivar_list_initializer): Updated call to encode_field_decl.
+       * objc-act.c (objc_init): Use objc_encoding_init.
+       * Make-lang.in (objc/objc-runtime-shared-support.o): Do not depend
+       on OBSTACK_H.
+       (objc/objc-gnu-runtime-abi-01.o): Likewise.
+       (objc/objc-next-runtime-abi-01.o): Likewise.
+       (objc/objc-next-runtime-abi-02.o): Likewise.
+       (objc/objc-act.o): Likewise.
+       
 2011-07-04  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        Refactored encoding code into objc-encoding.h and objc-encoding.c.
Index: objc/objc-encoding.h
===================================================================
--- objc/objc-encoding.h        (revision 176090)
+++ objc/objc-encoding.h        (working copy)
@@ -22,26 +22,11 @@  along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_OBJC_ENCODING_H
 #define GCC_OBJC_ENCODING_H
 
-/* TODO: Hide the following obstack code in objc-encoding.c, and have
-   a objc_encoding_init() that is called by objc_init() to set them
-   up.  */
-
-/* Set up for use of obstacks.  */
-#include "obstack.h"
-
-/* This obstack is used to accumulate the encoding of a data type.  */
-extern struct obstack util_obstack;
-
-/* This points to the beginning of obstack contents, so we can free
-   the whole contents.  */
-extern char *util_firstobj;
-
-/* This will be used to initialize the obstacks used by encoding.  It
+/* This is used to initialize the obstacks used by encoding.  It
    should be called before any encoding function is used.  It is
    usually done in objc_init().  */
-/* extern void objc_encoding_init (void); */
+extern void objc_encoding_init (void);
 
-
 /* Encode a method prototype.  The format is described in
    gcc/doc/objc.texi, section 'Method signatures'.  */
 extern tree encode_method_prototype (tree method_decl);
@@ -57,8 +42,10 @@  extern tree objc_build_encode_expr (tree type);
 /* Encode the attributes of a property.  */
 extern tree objc_v2_encode_prop_attr (tree property);
 
-/* Encode the type of a field.  */
-extern void encode_field_decl (tree field_decl, int curtype, int format);
+/* Encode the type of a field.  Return an identifier with the type
+   encoding for the field.  The type encoding is a null-terminated
+   string.  */
+extern tree encode_field_decl (tree field_decl);
 
 /* Tells "encode_pointer/encode_aggregate" whether we are generating
    type descriptors for instance variables (as opposed to methods).
Index: objc/objc-runtime-shared-support.c
===================================================================
--- objc/objc-runtime-shared-support.c  (revision 176090)
+++ objc/objc-runtime-shared-support.c  (working copy)
@@ -41,12 +41,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "objcp-decl.h"
 #endif  /* OBJCPLUS */
 
-#include "obstack.h"
-
-/* These are only used for encoding ivars.  */
-extern struct obstack util_obstack;
-extern char *util_firstobj;
-
 /* Hooks for string decls etc.  */
 #include "objc-runtime-hooks.h"
 
@@ -551,16 +545,9 @@  build_ivar_list_initializer (tree type, tree field
        CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
 
       /* Set type.  */
-      encode_field_decl (field_decl,
-                        obstack_object_size (&util_obstack),
-                        OBJC_ENCODE_DONT_INLINE_DEFS);
-
-      /* Null terminate string.  */
-      obstack_1grow (&util_obstack, 0);
-      id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
+      id = add_objc_string (encode_field_decl (field_decl),
                             meth_var_types);
       CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
-      obstack_free (&util_obstack, util_firstobj);
 
       /* Set offset.  */
       CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
Index: objc/Make-lang.in
===================================================================
--- objc/Make-lang.in   (revision 176090)
+++ objc/Make-lang.in   (working copy)
@@ -88,7 +88,6 @@  objc/objc-runtime-shared-support.o : objc/objc-run
    gt-objc-objc-runtime-shared-support.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    objc/objc-encoding.h \
    objc/objc-next-metadata-tags.h \
    objc/objc-runtime-shared-support.h
@@ -97,7 +96,6 @@  objc/objc-gnu-runtime-abi-01.o: objc/objc-gnu-runt
    gt-objc-objc-gnu-runtime-abi-01.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    toplev.h \
    objc/objc-encoding.h \
    objc/objc-runtime-hooks.h \
@@ -107,7 +105,6 @@  objc/objc-next-runtime-abi-01.o: objc/objc-next-ru
    gt-objc-objc-next-runtime-abi-01.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    $(TARGET_H) output.h \
    objc/objc-encoding.h \
    objc/objc-next-metadata-tags.h \
@@ -118,7 +115,6 @@  objc/objc-next-runtime-abi-02.o: objc/objc-next-ru
    gt-objc-objc-next-runtime-abi-02.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    $(TARGET_H) \
    objc/objc-encoding.h \
    objc/objc-next-metadata-tags.h \
@@ -129,7 +125,6 @@  objc/objc-act.o : objc/objc-act.c \
    gt-objc-objc-act.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    toplev.h $(FUNCTION_H) output.h debug.h $(LANGHOOKS_DEF_H) \
    $(HASHTAB_H) $(GIMPLE_H) \
    $(C_PRAGMA_H) $(C_TARGET_H) \
Index: objc/objc-act.c
===================================================================
--- objc/objc-act.c     (revision 176090)
+++ objc/objc-act.c     (working copy)
@@ -381,9 +381,7 @@  objc_init (void)
   /* Set up stuff used by FE parser and all runtimes.  */
   errbuf = XNEWVEC (char, 1024 * 10);
   hash_init ();
-  /* TODO: Use objc_encoding_init().  */
-  gcc_obstack_init (&util_obstack);
-  util_firstobj = (char *) obstack_finish (&util_obstack);
+  objc_encoding_init ();
   /* ... and then check flags and set-up for the selected runtime ... */
   if (flag_next_runtime && flag_objc_abi >= 2)
     ok = objc_next_runtime_abi_02_init (&runtime);
Index: objc/objc-next-runtime-abi-02.c
===================================================================
--- objc/objc-next-runtime-abi-02.c     (revision 176090)
+++ objc/objc-next-runtime-abi-02.c     (working copy)
@@ -50,13 +50,8 @@  along with GCC; see the file COPYING3.  If not see
 
 #include "ggc.h"
 #include "target.h"
-#include "obstack.h"
 #include "tree-iterator.h"
 
-/* These are only used for encoding ivars.  */
-extern struct obstack util_obstack;
-extern char *util_firstobj;
-
 #include "objc-runtime-hooks.h"
 #include "objc-runtime-shared-support.h"
 #include "objc-encoding.h"
@@ -2852,15 +2847,9 @@  build_v2_ivar_list_initializer (tree class_name, t
                                                meth_var_names));
 
       /* Set type.  */
-      encode_field_decl (field_decl,
-                        obstack_object_size (&util_obstack),
-                        OBJC_ENCODE_DONT_INLINE_DEFS);
-      /* Null terminate string.  */
-      obstack_1grow (&util_obstack, 0);
-      id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
+      id = add_objc_string (encode_field_decl (field_decl),
                             meth_var_types);
       CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
-      obstack_free (&util_obstack, util_firstobj);
 
       /* Set alignment.  */
       val = DECL_ALIGN_UNIT (field_decl);
Index: objcp/Make-lang.in
===================================================================
--- objcp/Make-lang.in  (revision 176090)
+++ objcp/Make-lang.in  (working copy)
@@ -92,7 +92,6 @@  objcp/objc-runtime-shared-support.o : objc/objc-ru
    gt-objc-objc-runtime-shared-support.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    objc/objc-encoding.h \
    objc/objc-next-metadata-tags.h \
    objc/objc-runtime-shared-support.h \
@@ -104,7 +103,6 @@  objcp/objc-gnu-runtime-abi-01.o: objc/objc-gnu-run
    gt-objc-objc-gnu-runtime-abi-01.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    toplev.h \
    objc/objc-encoding.h \
    objc/objc-runtime-hooks.h \
@@ -117,7 +115,6 @@  objcp/objc-next-runtime-abi-01.o: objc/objc-next-r
    gt-objc-objc-next-runtime-abi-01.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    $(TARGET_H) output.h \
    objc/objc-encoding.h \
    objc/objc-next-metadata-tags.h \
@@ -131,7 +128,6 @@  objcp/objc-next-runtime-abi-02.o: objc/objc-next-r
    gt-objc-objc-next-runtime-abi-02.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_CORE_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    $(TARGET_H) \
    objc/objc-encoding.h \
    objc/objc-next-metadata-tags.h \
@@ -148,7 +144,6 @@  objcp/objcp-act.o : objc/objc-act.c \
    gt-objc-objc-act.h \
    $(START_HDRS) \
    $(GGC_H) $(DIAGNOSTIC_H) $(FLAGS_H) input.h \
-   $(OBSTACK_H) \
    toplev.h $(FUNCTION_H) output.h debug.h $(LANGHOOKS_DEF_H) \
    $(HASHTAB_H) $(GIMPLE_H) \
    $(RTL_H) $(EXPR_H) $(TARGET_H) \
Index: objcp/ChangeLog
===================================================================
--- objcp/ChangeLog     (revision 176090)
+++ objcp/ChangeLog     (working copy)
@@ -1,3 +1,12 @@ 
+2011-07-10  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+       * Make-lang.in (objcp/objc-runtime-shared-support.o): Do not
+       depend on OBSTACK_H.
+       (objcp/objc-gnu-runtime-abi-01.o): Likewise.
+       (objcp/objc-next-runtime-abi-01.o): Likewise.
+       (objcp/objc-next-runtime-abi-02.o): Likewise.
+       (objcp/objcp-act.o): Likewise.
+
 2011-07-04  Nicola Pero  <nicola.pero@meta-innovation.com>
 
        * Make-lang.in (OBJCXX_OBJS): Added objc-encoding.o.