Patchwork ObjC: remove temporary tree list used when calling objc_declare_class()

login
register
mail settings
Submitter Nicola Pero
Date April 13, 2011, 8:52 p.m.
Message ID <1302727921.10478072@192.168.4.58>
Download mbox | patch
Permalink /patch/91110/
State New
Headers show

Comments

Nicola Pero - April 13, 2011, 8:52 p.m.
This patch removes another case of temporary tree chains from the ObjC
compiler.  When the parser finds a

 @class NSArray;

declaration, it would call objc_declare_class() passing as argument 
a temporary tree list containing the identifier.  This tree list is
created, used for the function call, then discarded.  Btw, this made
some sense when you have a list with multiple identifiers, as in

 @class NSArray, NSString;

as they are all passed in the same linked list.

it is still cleaner and more efficient to call objc_declare_class() for each
identifier, as opposed to creating a temporary tree list, populating it with
the identifiers, then calling objc_declare_class() which then iterates
over the temporary tree list to read the identifiers, and then the list
gets thrown away.

The patch speeds things up but the effect is very small; the GNUstep system
headers do contain about 800 @class declarations or so, so the patch typically
saves something like 1k allocations or so when compiling a file.  That is
probably a 0.05% speedup (order of magnitude).  But we get rid of a the tree list. :-)

The ObjC++ parser needed a bit of changes, but while changing it I also
made the ObjC++ parsing code almost identical to the ObjC one, which simplifies
maintenance.

Ok to commit ?

Thanks
Mike Stump - April 13, 2011, 11:01 p.m.
On Apr 13, 2011, at 1:52 PM, Nicola Pero wrote:
> This patch removes another case of temporary tree chains from the ObjC
> compiler.

> Ok to commit ?

Ok.

Patch

Index: c-family/ChangeLog
===================================================================
--- c-family/ChangeLog  (revision 172360)
+++ c-family/ChangeLog  (working copy)
@@ -1,3 +1,7 @@ 
+2011-04-13  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+       * stub-objc.c (objc_declare_class): Updated argument name.
+
 2011-04-12  Nathan Froyd  <froydnj@codesourcery.com>
 
        * c-common.h (c_common_init_ts): Declare.
Index: c-family/stub-objc.c
===================================================================
--- c-family/stub-objc.c        (revision 172360)
+++ c-family/stub-objc.c        (working copy)
@@ -110,7 +110,7 @@  objc_declare_alias (tree ARG_UNUSED (alias), tree
 }
 
 void
-objc_declare_class (tree ARG_UNUSED (list))
+objc_declare_class (tree ARG_UNUSED (identifier))
 {
 }
 
Index: objc/ChangeLog
===================================================================
--- objc/ChangeLog      (revision 172360)
+++ objc/ChangeLog      (working copy)
@@ -1,5 +1,12 @@ 
 2011-04-13  Nicola Pero  <nicola.pero@meta-innovation.com>
 
+       * objc-act.c (objc_declare_class): Changed to take a single
+       identifier as argument instead of a tree list.  This means callers
+       don't have to build temporary tree lists to call this function.
+       (synth_module_prologue): Updated calls to objc_declare_class.
+       
+2011-04-13  Nicola Pero  <nicola.pero@meta-innovation.com>
+
        * objc-act.c (build_keyword_selector): Use get_identifier_with_length
        instead of get_identifier.
 
Index: objc/objc-act.c
===================================================================
--- objc/objc-act.c     (revision 172360)
+++ objc/objc-act.c     (working copy)
@@ -2953,7 +2953,7 @@  synth_module_prologue (void)
 
   /* Forward-declare '@interface Protocol'.  */
   type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
-  objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
+  objc_declare_class (type);
   objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
 
   /* Declare receiver type used for dispatching messages to 'super'.  */
@@ -2985,7 +2985,7 @@  synth_module_prologue (void)
   if (!constant_string_class_name)
     constant_string_class_name = runtime.default_constant_string_class_name;
   constant_string_id = get_identifier (constant_string_class_name);
-  objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
+  objc_declare_class (constant_string_id);
 
   /* Pre-build the following entities - for speed/convenience.  */
   self_id = get_identifier ("self");
@@ -3360,48 +3360,42 @@  objc_declare_alias (tree alias_ident, tree class_i
 }
 
 void
-objc_declare_class (tree ident_list)
+objc_declare_class (tree identifier)
 {
-  tree list;
 #ifdef OBJCPLUS
   if (current_namespace != global_namespace) {
     error ("Objective-C declarations may only appear in global scope");
   }
 #endif /* OBJCPLUS */
 
-  for (list = ident_list; list; list = TREE_CHAIN (list))
+  if (! objc_is_class_name (ident))
     {
-      tree ident = TREE_VALUE (list);
-
-      if (! objc_is_class_name (ident))
+      tree record = lookup_name (ident), type = record;
+      
+      if (record)
        {
-         tree record = lookup_name (ident), type = record;
-
-         if (record)
+         if (TREE_CODE (record) == TYPE_DECL)
+           type = DECL_ORIGINAL_TYPE (record)
+             ? DECL_ORIGINAL_TYPE (record)
+             : TREE_TYPE (record);
+         
+         if (!TYPE_HAS_OBJC_INFO (type)
+             || !TYPE_OBJC_INTERFACE (type))
            {
-             if (TREE_CODE (record) == TYPE_DECL)
-               type = DECL_ORIGINAL_TYPE (record)
-                                       ? DECL_ORIGINAL_TYPE (record)
-                                       : TREE_TYPE (record);
-
-             if (!TYPE_HAS_OBJC_INFO (type)
-                 || !TYPE_OBJC_INTERFACE (type))
-               {
-                 error ("%qE redeclared as different kind of symbol",
-                        ident);
-                 error ("previous declaration of %q+D",
-                        record);
-               }
+             error ("%qE redeclared as different kind of symbol",
+                    ident);
+             error ("previous declaration of %q+D",
+                    record);
            }
-
-         record = xref_tag (RECORD_TYPE, ident);
-         INIT_TYPE_OBJC_INFO (record);
-         /* In the case of a @class declaration, we store the ident
-            in the TYPE_OBJC_INTERFACE.  If later an @interface is
-            found, we'll replace the ident with the interface.  */
-         TYPE_OBJC_INTERFACE (record) = ident;
-         hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
        }
+      
+      record = xref_tag (RECORD_TYPE, ident);
+      INIT_TYPE_OBJC_INFO (record);
+      /* In the case of a @class declaration, we store the ident in
+        the TYPE_OBJC_INTERFACE.  If later an @interface is found,
+        we'll replace the ident with the interface.  */
+      TYPE_OBJC_INTERFACE (record) = ident;
+      hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
     }
 }
 
Index: ChangeLog
===================================================================
--- ChangeLog   (revision 172360)
+++ ChangeLog   (working copy)
@@ -1,3 +1,8 @@ 
+2011-04-13  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+       * c-parser.c (c_parser_objc_class_declaration): Updated call to
+       objc_declare_class.
+
 2011-04-12  Nathan Froyd  <froydnj@codesourcery.com>
 
        * c-decl.c (union lang_tree_node): Check for TS_COMMON before
Index: cp/ChangeLog
===================================================================
--- cp/ChangeLog        (revision 172360)
+++ cp/ChangeLog        (working copy)
@@ -1,3 +1,8 @@ 
+2011-04-13  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+       * parser.c (cp_parser_objc_class_declaration): Updated for change
+       in objc_declare_class().
+
 2011-04-12  Nathan Froyd  <froydnj@codesourcery.com>
 
        * cp-lang.c (cp_init_ts): Call cp_common_init_ts.  Move
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 172360)
+++ cp/parser.c (working copy)
@@ -21615,7 +21615,21 @@  static void
 cp_parser_objc_class_declaration (cp_parser* parser)
 {
   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
-  objc_declare_class (cp_parser_objc_identifier_list (parser));
+  while (true)
+    {
+      tree id;
+      
+      id = cp_parser_identifier (parser);
+      if (id == error_mark_node)
+       break;
+      
+      objc_declare_class (id);
+
+      if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
+       cp_lexer_consume_token (parser->lexer);
+      else
+       break;
+    }
   cp_parser_consume_semicolon_at_end_of_statement (parser);
 }
 
Index: c-parser.c
===================================================================
--- c-parser.c  (revision 172360)
+++ c-parser.c  (working copy)
@@ -6994,7 +6994,6 @@  c_parser_objc_class_instance_variables (c_parser *
 static void
 c_parser_objc_class_declaration (c_parser *parser)
 {
-  tree list = NULL_TREE;
   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
   c_parser_consume_token (parser);
   /* Any identifiers, including those declared as type names, are OK
@@ -7010,7 +7009,7 @@  c_parser_objc_class_declaration (c_parser *parser)
          return;
        }
       id = c_parser_peek_token (parser)->value;
-      list = chainon (list, build_tree_list (NULL_TREE, id));
+      objc_declare_class (id);
       c_parser_consume_token (parser);
       if (c_parser_next_token_is (parser, CPP_COMMA))
        c_parser_consume_token (parser);
@@ -7018,7 +7017,6 @@  c_parser_objc_class_declaration (c_parser *parser)
        break;
     }
   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
-  objc_declare_class (list);
 }
 
 /* Parse an objc-alias-declaration.