This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH: Fix ObjC @"string" machinery


At present, gcc-4.0 complains about invalid class layout if NSConstantString,
NXConstantString or the class specified via -fconstant-string-class=... has
a non-trivial inheritance structure (see the 4 test cases attached). This
used to work before the recent ObjC comptypes rewrite went in :-(; now, it
is no longer permissible to check TYPE_FIELDS() to get all the ivars for
a given class implementation, since TYPE_BINFO (and other goodies pleasing
to the middle- and back-ends) is being used.


To work around this, I synthesize an internal string type, called__builtin_ObjCString,
construct const objects of that type, and then cast to NSConstantString/NXConstantString/
whatever as appropriate. Note that this has the side-effect of streamlining the section
selection logic in darwin.c a bit.


Thoughts? I will wait until tomorrow morning (Pacific time) before committing,
should people have comments.


--Zem

[gcc/ChangeLog]
2005-06-22  Ziemowit Laski  <zlaski@apple.com>

* config/darwin.c (machopic_select_section): constant ObjC string
objects now always have type "__builtin_ObjCString".


[gcc/objc/ChangeLog]
2005-06-22  Ziemowit Laski  <zlaski@apple.com>

* objc-act.c (objc_build_internal_const_str_type): New function.
(check_string_class_template): Use objc_get_class_ivars() instead
of TYPE_FIELDS() to retrieve ivar list.
(AT_LEAST_AS_LARGE_AS): Check the size of each field's type rather
than the field itself.
(objc_build_string_object): Synthesize a "__builtin_ObjCString"
type and use it to lay out compile-time string objects.
* objc-act.h (OCTI_INTERNAL_CNST_STR_TYPE, internal_const_str_type):
New.


[gcc/testsuite/ChangeLog]
2005-06-22  Ziemowit Laski  <zlaski@apple.com>

        * obj-c++.dg/const-str-1[0-1].mm: New.
        * objc.dg/const-str-1[0-1].m: New.

Index: gcc/config/darwin.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/darwin.c,v
retrieving revision 1.121
diff -u -3 -p -r1.121 darwin.c
--- gcc/config/darwin.c 9 Jun 2005 03:53:20 -0000       1.121
+++ gcc/config/darwin.c 22 Jun 2005 00:49:33 -0000
@@ -1045,13 +1045,19 @@ machopic_select_section (tree exp, int r
           && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
           && TYPE_NAME (TREE_TYPE (exp)))
     {
+      extern int flag_next_runtime;
       tree name = TYPE_NAME (TREE_TYPE (exp));
+
       if (TREE_CODE (name) == TYPE_DECL)
        name = DECL_NAME (name);
-      if (!strcmp (IDENTIFIER_POINTER (name), "NSConstantString"))
-       objc_constant_string_object_section ();
-      else if (!strcmp (IDENTIFIER_POINTER (name), "NXConstantString"))
-       objc_string_object_section ();
+
+      if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
+       {
+         if (flag_next_runtime)
+           objc_constant_string_object_section ();
+         else
+           objc_string_object_section ();
+       }
       else
        base_function ();
     }
Index: gcc/objc/objc-act.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/objc/objc-act.c,v
retrieving revision 1.277
diff -u -3 -p -r1.277 objc-act.c
--- gcc/objc/objc-act.c 15 Jun 2005 15:55:30 -0000      1.277
+++ gcc/objc/objc-act.c 22 Jun 2005 00:49:34 -0000
@@ -209,6 +209,9 @@ enum string_section
 };

 static tree add_objc_string (tree, enum string_section);
+static tree setup_string_decl (void);
+static int check_string_class_template (void);
+static tree objc_build_internal_const_str_type (void);
 static tree build_objc_string_decl (enum string_section);
 static void build_selector_table_decl (void);

@@ -1691,11 +1694,11 @@ synth_module_prologue (void)
 static int
 check_string_class_template (void)
 {
-  tree field_decl = TYPE_FIELDS (constant_string_type);
+  tree field_decl = objc_get_class_ivars (constant_string_id);

 #define AT_LEAST_AS_LARGE_AS(F, T) \
   (F && TREE_CODE (F) == FIELD_DECL \
-     && (TREE_INT_CST_LOW (DECL_SIZE (F)) \
+     && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
         >= TREE_INT_CST_LOW (TYPE_SIZE (T))))

   if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
@@ -1714,6 +1717,27 @@ check_string_class_template (void)
 /* Avoid calling `check_string_class_template ()' more than once.  */
 static GTY(()) int string_layout_checked;

+/* Construct an internal string layout to be used as a template for
+   creating NSConstantString/NXConstantString instances.  */
+
+static tree
+objc_build_internal_const_str_type (void)
+{
+  tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
+  tree fields = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
+  tree field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
+
+  TREE_CHAIN (field) = fields; fields = field;
+  field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
+  TREE_CHAIN (field) = fields; fields = field;
+  /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
+     reverse order!  */
+  finish_builtin_struct (type, "__builtin_ObjCString",
+                        fields, NULL_TREE);
+
+  return type;
+}
+
 /* Custom build_string which sets TREE_TYPE!  */

 static tree
@@ -1786,6 +1810,7 @@ objc_build_string_object (tree string)
     {
       string_layout_checked = -1;
       constant_string_class = lookup_interface (constant_string_id);
+      internal_const_str_type = objc_build_internal_const_str_type ();

       if (!constant_string_class
          || !(constant_string_type
@@ -1822,9 +1847,9 @@ objc_build_string_object (tree string)
       *loc = desc = ggc_alloc (sizeof (*desc));
       desc->literal = string;

- /* GNU: & ((NXConstantString) { NULL, string, length }) */
- /* NeXT: & ((NSConstantString) { isa, string, length }) */
- fields = TYPE_FIELDS (constant_string_type);
+ /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
+ /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
+ fields = TYPE_FIELDS (internal_const_str_type);
initlist
= build_tree_list (fields,
flag_next_runtime
@@ -1836,13 +1861,13 @@ objc_build_string_object (tree string)
fields = TREE_CHAIN (fields);
initlist = tree_cons (fields, build_int_cst (NULL_TREE, length),
initlist);
- constructor = objc_build_constructor (constant_string_type,
+ constructor = objc_build_constructor (internal_const_str_type,
nreverse (initlist));
TREE_INVARIANT (constructor) = true;


if (!flag_next_runtime)
constructor
- = objc_add_static_instance (constructor, constant_string_type);
+ = objc_add_static_instance (constructor, internal_const_str_type);
else
{
var = build_decl (CONST_DECL, NULL, TREE_TYPE (constructor));
@@ -1854,7 +1879,8 @@ objc_build_string_object (tree string)
desc->constructor = constructor;
}


-  addr = build_unary_op (ADDR_EXPR, desc->constructor, 1);
+  addr = convert (build_pointer_type (constant_string_type),
+                 build_unary_op (ADDR_EXPR, desc->constructor, 1));

return addr;
}
Index: gcc/objc/objc-act.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/objc/objc-act.h,v
retrieving revision 1.39
diff -u -3 -p -r1.39 objc-act.h
--- gcc/objc/objc-act.h 24 May 2005 22:21:00 -0000 1.39
+++ gcc/objc/objc-act.h 22 Jun 2005 00:49:34 -0000
@@ -263,6 +263,7 @@ enum objc_tree_index
OCTI_CNST_STR_TYPE,
OCTI_CNST_STR_GLOB_ID,
OCTI_STRING_CLASS_DECL,
+ OCTI_INTERNAL_CNST_STR_TYPE,
OCTI_SUPER_DECL,
OCTI_UMSG_NONNIL_DECL,
OCTI_UMSG_NONNIL_STRET_DECL,
@@ -450,6 +451,7 @@ extern GTY(()) tree objc_global_trees[OC
#define constant_string_global_id \
objc_global_trees[OCTI_CNST_STR_GLOB_ID]
#define string_class_decl objc_global_trees[OCTI_STRING_CLASS_DECL]
+#define internal_const_str_type objc_global_trees[OCTI_INTERNAL_CNST_STR_TYPE]
#define UOBJC_SUPER_decl objc_global_trees[OCTI_SUPER_DECL]


#endif /* GCC_OBJC_ACT_H */
Index: gcc/testsuite/obj-c++.dg/const-str-10.mm
===================================================================
RCS file: gcc/testsuite/obj-c++.dg/const-str-10.mm
diff -N gcc/testsuite/obj-c++.dg/const-str-10.mm
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/obj-c++.dg/const-str-10.mm 22 Jun 2005 00:49:38 -0000
@@ -0,0 +1,27 @@
+/* Test if ObjC constant string layout is checked properly, regardless of how
+ constant string classes get derived. */
+/* Contributed by Ziemowit Laski <zlaski@apple.com> */
+
+/* { dg-do compile } */
+
+#include <objc/Object.h>
+
+@interface NSString: Object
+@end
+
+@interface NSSimpleCString : NSString {
+@protected
+ char *bytes;
+ unsigned int numBytes;
+}
+@end
+
+@interface NSConstantString : NSSimpleCString
+@end
+
+extern struct objc_class _NSConstantStringClassReference;
+
+const NSConstantString *appKey = @"MyApp";
+
+/* { dg-final { scan-assembler ".section __OBJC, __cstring_object" { target *-*-darwin* } } } */
+/* { dg-final { scan-assembler ".long\t__NSConstantStringClassReference\n\t.long\t.*\n\t.long\t5\n\t.da ta" { target *-*-darwin* } } } */
Index: gcc/testsuite/obj-c++.dg/const-str-11.mm
===================================================================
RCS file: gcc/testsuite/obj-c++.dg/const-str-11.mm
diff -N gcc/testsuite/obj-c++.dg/const-str-11.mm
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/obj-c++.dg/const-str-11.mm 22 Jun 2005 00:49:38 -0000
@@ -0,0 +1,27 @@
+/* Test if ObjC constant string layout is checked properly, regardless of how
+ constant string classes get derived. */
+/* Contributed by Ziemowit Laski <zlaski@apple.com> */
+
+/* { dg-options "-fconstant-string-class=XStr" } */
+/* { dg-do compile } */
+
+#include <objc/Object.h>
+
+@interface XString: Object {
+@protected
+ char *bytes;
+}
+@end
+
+@interface XStr : XString {
+@public
+ unsigned int len;
+}
+@end
+
+extern struct objc_class _XStrClassReference;
+
+const XStr *appKey = @"MyApp";
+
+/* { dg-final { scan-assembler ".section __OBJC, __cstring_object" { target *-*-darwin* } } } */
+/* { dg-final { scan-assembler ".long\t__XStrClassReference\n\t.long\t.*\n\t.long\t5\n\t.data" { target *-*-darwin* } } } */
Index: gcc/testsuite/objc.dg/const-str-10.m
===================================================================
RCS file: gcc/testsuite/objc.dg/const-str-10.m
diff -N gcc/testsuite/objc.dg/const-str-10.m
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/objc.dg/const-str-10.m 22 Jun 2005 00:49:38 -0000
@@ -0,0 +1,27 @@
+/* Test if ObjC constant string layout is checked properly, regardless of how
+ constant string classes get derived. */
+/* Contributed by Ziemowit Laski <zlaski@apple.com> */
+
+/* { dg-do compile } */
+
+#include <objc/Object.h>
+
+@interface NSString: Object
+@end
+
+@interface NSSimpleCString : NSString {
+@protected
+ char *bytes;
+ unsigned int numBytes;
+}
+@end
+
+@interface NSConstantString : NSSimpleCString
+@end
+
+extern struct objc_class _NSConstantStringClassReference;
+
+const NSConstantString *appKey = @"MyApp";
+
+/* { dg-final { scan-assembler ".section __OBJC, __cstring_object" { target *-*-darwin* } } } */
+/* { dg-final { scan-assembler ".long\t__NSConstantStringClassReference\n\t.long\t.*\n\t.long\t5\n\t.da ta" { target *-*-darwin* } } } */
Index: gcc/testsuite/objc.dg/const-str-11.m
===================================================================
RCS file: gcc/testsuite/objc.dg/const-str-11.m
diff -N gcc/testsuite/objc.dg/const-str-11.m
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/objc.dg/const-str-11.m 22 Jun 2005 00:49:38 -0000
@@ -0,0 +1,27 @@
+/* Test if ObjC constant string layout is checked properly, regardless of how
+ constant string classes get derived. */
+/* Contributed by Ziemowit Laski <zlaski@apple.com> */
+
+/* { dg-options "-fconstant-string-class=XStr" } */
+/* { dg-do compile } */
+
+#include <objc/Object.h>
+
+@interface XString: Object {
+@protected
+ char *bytes;
+}
+@end
+
+@interface XStr : XString {
+@public
+ unsigned int len;
+}
+@end
+
+extern struct objc_class _XStrClassReference;
+
+const XStr *appKey = @"MyApp";
+
+/* { dg-final { scan-assembler ".section __OBJC, __cstring_object" { target *-*-darwin* } } } */
+/* { dg-final { scan-assembler ".long\t__XStrClassReference\n\t.long\t.*\n\t.long\t5\n\t.data" { target *-*-darwin* } } } */


--------------------------------------------------------------
Ziemowit Laski                 1 Infinite Loop, MS 301-2K
Mac OS X Compiler Group        Cupertino, CA USA  95014-2083
Apple Computer, Inc.           +1.408.974.6229  Fax .5477


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]