1 /* Support routines shared by all runtimes.
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 Contributed by Iain Sandoe (partially split from objc-act.c)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "cp/cp-tree.h"
33 #include "langhooks.h"
34 #include "c-family/c-objc.h"
37 /* When building Objective-C++, we are not linking against the C front-end
38 and so need to replicate the C tree-construction functions in some way. */
40 #define OBJCP_REMAP_FUNCTIONS
41 #include "objcp-decl.h"
44 /* Hooks for string decls etc. */
45 #include "objc-runtime-hooks.h"
47 #include "objc-runtime-shared-support.h"
48 #include "objc-encoding.h"
50 /* rt_trees identifiers - shared between NeXT implementations. These allow
51 the FE to tag meta-data in a manner that survives LTO and can be used when
52 the runtime requires that certain meta-data items appear in particular
54 #include "objc-next-metadata-tags.h"
55 extern GTY(()) tree objc_rt_trees
[OCTI_RT_META_MAX
];
57 /* Rather than repeatedly looking up the identifiers, we save them here. */
58 tree objc_rt_trees
[OCTI_RT_META_MAX
];
60 /* For building an objc struct. These might not be used when this file
61 is compiled as part of obj-c++. */
63 static bool objc_building_struct
;
64 static struct c_struct_parse_info
*objc_struct_info ATTRIBUTE_UNUSED
;
66 /* Start building a struct for objc. */
69 objc_start_struct (tree name
)
71 gcc_assert (!objc_building_struct
);
72 objc_building_struct
= true;
73 return start_struct (input_location
, RECORD_TYPE
, name
, &objc_struct_info
);
76 /* Finish building a struct for objc. */
79 objc_finish_struct (tree type
, tree fieldlist
)
81 gcc_assert (objc_building_struct
);
82 objc_building_struct
= false;
83 return finish_struct (input_location
, type
, fieldlist
, NULL_TREE
,
88 build_sized_array_type (tree base_type
, int size
)
90 tree index_type
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
91 return build_array_type (base_type
, index_type
);
94 /* Create a declaration for field NAME of a given TYPE. */
97 create_field_decl (tree type
, const char *name
)
99 return build_decl (input_location
,
100 FIELD_DECL
, get_identifier (name
), type
);
104 add_field_decl (tree type
, const char *name
, tree
**chain
)
106 tree field
= create_field_decl (type
, name
);
110 *chain
= &DECL_CHAIN (field
);
115 /* Create a global, static declaration for variable NAME of a given TYPE. The
116 finish_var_decl() routine will need to be called on it afterwards. */
119 start_var_decl (tree type
, const char *name
)
121 tree var
= build_decl (input_location
,
122 VAR_DECL
, get_identifier (name
), type
);
123 TREE_STATIC (var
) = 1;
124 DECL_INITIAL (var
) = error_mark_node
; /* A real initializer is coming... */
125 DECL_IGNORED_P (var
) = 1;
126 DECL_ARTIFICIAL (var
) = 1;
127 DECL_CONTEXT (var
) = NULL_TREE
;
129 DECL_THIS_STATIC (var
) = 1; /* squash redeclaration errors */
134 /* Finish off the variable declaration created by start_var_decl(). */
137 finish_var_decl (tree var
, tree initializer
)
139 finish_decl (var
, input_location
, initializer
, NULL_TREE
, NULL_TREE
);
142 /* Just a handy wrapper for add_objc_string. */
145 build_selector (tree ident
)
147 return convert (objc_selector_type
, add_objc_string (ident
, meth_var_names
));
150 /* --- templates --- */
152 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
153 This needs to be done just once per compilation. */
155 /* struct _objc_super {
156 struct _objc_object *self;
157 struct _objc_class *super_class;
158 [or Class cls; for the abi v2]
162 build_super_template (void)
164 tree decls
, *chain
= NULL
;
166 objc_super_template
= objc_start_struct (get_identifier (UTAG_SUPER
));
168 /* struct _objc_object *self; */
169 decls
= add_field_decl (objc_object_type
, "self", &chain
);
171 /* struct _objc_class *super_class; */
172 add_field_decl (build_pointer_type (objc_class_template
),
173 "super_class", &chain
);
175 objc_finish_struct (objc_super_template
, decls
);
178 /* To accomplish method prototyping without generating all kinds of
179 inane warnings, the definition of the dispatch table entries were
182 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
184 struct objc_method { SEL _cmd; ...; void *_imp; }; */
187 build_method_template (void)
190 tree decls
, *chain
= NULL
;
192 _SLT_record
= objc_start_struct (get_identifier (UTAG_METHOD
));
195 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
197 /* char *method_types; */
198 add_field_decl (string_type_node
, "method_types", &chain
);
201 add_field_decl (build_pointer_type (void_type_node
), "_imp", &chain
);
203 objc_finish_struct (_SLT_record
, decls
);
209 build_method_prototype_template (void)
212 tree decls
, *chain
= NULL
;
214 proto_record
= objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE
));
217 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
219 /* char *method_types; */
220 add_field_decl (string_type_node
, "method_types", &chain
);
222 objc_finish_struct (proto_record
, decls
);
228 struct _objc__method_prototype_list *method_next;
230 struct objc_method method_list[method_count];
234 build_method_list_template (tree list_type
, int size
)
236 tree objc_ivar_list_record
;
237 tree array_type
, decls
, *chain
= NULL
;
239 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
241 /* struct _objc__method_prototype_list *method_next; */
242 decls
= add_field_decl (objc_method_proto_list_ptr
, "method_next", &chain
);
244 /* int method_count; */
245 add_field_decl (integer_type_node
, "method_count", &chain
);
247 /* struct objc_method method_list[]; */
248 array_type
= build_sized_array_type (list_type
, size
);
249 add_field_decl (array_type
, "method_list", &chain
);
251 objc_finish_struct (objc_ivar_list_record
, decls
);
253 return objc_ivar_list_record
;
256 /* struct objc_method_prototype_list {
258 struct objc_method_prototype {
265 build_method_prototype_list_template (tree list_type
, int size
)
267 tree objc_ivar_list_record
;
268 tree array_type
, decls
, *chain
= NULL
;
270 /* Generate an unnamed struct definition. */
272 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
274 /* int method_count; */
275 decls
= add_field_decl (integer_type_node
, "method_count", &chain
);
277 /* struct objc_method method_list[]; */
278 array_type
= build_sized_array_type (list_type
, size
);
279 add_field_decl (array_type
, "method_list", &chain
);
281 objc_finish_struct (objc_ivar_list_record
, decls
);
283 return objc_ivar_list_record
;
286 /* --- names, decls entry --- */
288 /* For each string section we have a chain which maps identifier nodes
289 to decls for the strings. */
291 static GTY(()) int meth_var_names_idx
;
292 static GTY(()) int meth_var_types_idx
;
293 static GTY(()) int property_name_attr_idx
;
296 add_objc_string (tree ident
, string_section section
)
298 tree
*chain
, decl
, type
;
304 chain
= &class_names_chain
;
305 snprintf (buf
, BUFSIZE
, "_OBJC_ClassName_%s", IDENTIFIER_POINTER (ident
));
308 chain
= &meth_var_names_chain
;
309 snprintf (buf
, BUFSIZE
, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx
++);
312 chain
= &meth_var_types_chain
;
313 snprintf (buf
, BUFSIZE
, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx
++);
315 case prop_names_attr
:
316 chain
= &prop_names_attr_chain
;
317 snprintf (buf
, BUFSIZE
, "_OBJC_PropertyAttributeOrName_%d", property_name_attr_idx
++);
325 if (TREE_VALUE (*chain
) == ident
)
326 return convert (string_type_node
,
327 build_unary_op (input_location
,
328 ADDR_EXPR
, TREE_PURPOSE (*chain
), 1));
330 chain
= &TREE_CHAIN (*chain
);
333 type
= build_sized_array_type (char_type_node
, IDENTIFIER_LENGTH (ident
) + 1);
334 /* Get a runtime-specific string decl which will be finish_var()'ed in
335 generate_strings (). */
336 decl
= (*runtime
.string_decl
) (type
, buf
, section
);
337 TREE_CONSTANT (decl
) = 1;
338 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
340 return convert (string_type_node
,
341 build_unary_op (input_location
, ADDR_EXPR
, decl
, 1));
344 /* --- shared metadata routines --- */
347 build_descriptor_table_initializer (tree type
, tree entries
)
349 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
353 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
355 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
356 build_selector (METHOD_SEL_NAME (entries
)));
357 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
358 add_objc_string (METHOD_ENCODING (entries
),
361 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
362 objc_build_constructor (type
, elts
));
364 entries
= DECL_CHAIN (entries
);
368 return objc_build_constructor (build_array_type (type
, 0), inits
);
372 build_dispatch_table_initializer (tree type
, tree entries
)
374 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
378 vec
<constructor_elt
, va_gc
> *elems
= NULL
;
381 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
382 build_selector (METHOD_SEL_NAME (entries
)));
384 /* Generate the method encoding if we don't have one already. */
385 if (! METHOD_ENCODING (entries
))
386 METHOD_ENCODING (entries
) =
387 encode_method_prototype (entries
);
389 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
390 add_objc_string (METHOD_ENCODING (entries
),
393 expr
= convert (ptr_type_node
,
394 build_unary_op (input_location
, ADDR_EXPR
,
395 METHOD_DEFINITION (entries
), 1));
396 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
, expr
);
398 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
399 objc_build_constructor (type
, elems
));
401 entries
= DECL_CHAIN (entries
);
405 return objc_build_constructor (build_array_type (type
, 0), inits
);
408 /* Used only by build_*_selector_translation_table (). */
410 diagnose_missing_method (tree meth
, location_t here
)
414 for (method_chain
= meth_var_names_chain
;
416 method_chain
= TREE_CHAIN (method_chain
))
418 if (TREE_VALUE (method_chain
) == meth
)
426 warning_at (here
, 0, "creating selector for nonexistent method %qE",
432 init_module_descriptor (tree type
, long vers
)
436 vec
<constructor_elt
, va_gc
> *v
= NULL
;
438 /* No really useful place to point to. */
439 loc
= UNKNOWN_LOCATION
;
441 /* version = { 1, ... } */
443 expr
= build_int_cst (long_integer_type_node
, vers
);
444 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
446 /* size = { ..., sizeof (struct _objc_module), ... } */
448 expr
= convert (long_integer_type_node
,
449 size_in_bytes (objc_module_template
));
450 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
452 /* Don't provide any file name for security reasons. */
453 /* name = { ..., "", ... } */
455 expr
= add_objc_string (get_identifier (""), class_names
);
456 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
458 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
460 ltyp
= build_pointer_type (xref_tag (RECORD_TYPE
,
461 get_identifier (UTAG_SYMTAB
)));
462 if (UOBJC_SYMBOLS_decl
)
463 expr
= convert (ltyp
, build_unary_op (loc
,
464 ADDR_EXPR
, UOBJC_SYMBOLS_decl
, 0));
466 expr
= convert (ltyp
, null_pointer_node
);
467 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
469 return objc_build_constructor (type
, v
);
472 /* Write out the data structures to describe Objective C classes defined.
474 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
477 build_module_descriptor (long vers
, tree attr
)
479 tree decls
, *chain
= NULL
;
482 push_lang_context (lang_name_c
); /* extern "C" */
485 objc_module_template
= objc_start_struct (get_identifier (UTAG_MODULE
));
488 decls
= add_field_decl (long_integer_type_node
, "version", &chain
);
491 add_field_decl (long_integer_type_node
, "size", &chain
);
494 add_field_decl (string_type_node
, "name", &chain
);
496 /* struct _objc_symtab *symtab; */
497 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE
,
498 get_identifier (UTAG_SYMTAB
))),
501 objc_finish_struct (objc_module_template
, decls
);
503 /* Create an instance of "_objc_module". */
504 UOBJC_MODULES_decl
= start_var_decl (objc_module_template
,
505 /* FIXME - why the conditional
508 flag_next_runtime
? "_OBJC_Module" : "_OBJC_Module");
510 /* This is the root of the metadata for defined classes and categories, it
511 is referenced by the runtime and, therefore, needed. */
512 DECL_PRESERVE_P (UOBJC_MODULES_decl
) = 1;
514 /* Allow the runtime to mark meta-data such that it can be assigned to target
515 specific sections by the back-end. */
517 DECL_ATTRIBUTES (UOBJC_MODULES_decl
) = attr
;
519 finish_var_decl (UOBJC_MODULES_decl
,
520 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl
),
529 build_ivar_list_initializer (tree type
, tree field_decl
)
531 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
535 vec
<constructor_elt
, va_gc
> *ivar
= NULL
;
539 if (DECL_NAME (field_decl
))
540 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
,
541 add_objc_string (DECL_NAME (field_decl
),
544 /* Unnamed bit-field ivar (yuck). */
545 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
548 id
= add_objc_string (encode_field_decl (field_decl
),
550 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, id
);
553 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, byte_position (field_decl
));
554 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
555 objc_build_constructor (type
, ivar
));
557 field_decl
= DECL_CHAIN (field_decl
);
558 while (field_decl
&& TREE_CODE (field_decl
) != FIELD_DECL
);
562 return objc_build_constructor (build_array_type (type
, 0), inits
);
567 struct objc_ivar ivar_list[ivar_count];
571 build_ivar_list_template (tree list_type
, int size
)
573 tree objc_ivar_list_record
;
574 tree array_type
, decls
, *chain
= NULL
;
576 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
578 /* int ivar_count; */
579 decls
= add_field_decl (integer_type_node
, "ivar_count", &chain
);
581 /* struct objc_ivar ivar_list[]; */
582 array_type
= build_sized_array_type (list_type
, size
);
583 add_field_decl (array_type
, "ivar_list", &chain
);
585 objc_finish_struct (objc_ivar_list_record
, decls
);
587 return objc_ivar_list_record
;
590 /* struct _objc_ivar {
597 build_ivar_template (void)
599 tree objc_ivar_id
, objc_ivar_record
;
600 tree decls
, *chain
= NULL
;
602 objc_ivar_id
= get_identifier (UTAG_IVAR
);
603 objc_ivar_record
= objc_start_struct (objc_ivar_id
);
605 /* char *ivar_name; */
606 decls
= add_field_decl (string_type_node
, "ivar_name", &chain
);
608 /* char *ivar_type; */
609 add_field_decl (string_type_node
, "ivar_type", &chain
);
611 /* int ivar_offset; */
612 add_field_decl (integer_type_node
, "ivar_offset", &chain
);
614 objc_finish_struct (objc_ivar_record
, decls
);
616 return objc_ivar_record
;
619 /* Used by NeXT ABI=0..2 */
621 build_next_selector_translation_table (void)
624 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
627 tree decl
= TREE_PURPOSE (chain
);
632 loc
= DECL_SOURCE_LOCATION (decl
);
634 loc
= UNKNOWN_LOCATION
;
635 diagnose_missing_method (TREE_VALUE (chain
), loc
);
638 expr
= build_selector (TREE_VALUE (chain
));
642 /* Entries of this form are used for references to methods.
643 The runtime re-writes these on start-up, but the compiler can't see
644 that and optimizes it away unless we force it. */
645 DECL_PRESERVE_P (decl
) = 1;
646 finish_var_decl (decl
, expr
);
652 generate_protocol_references (tree plist
)
656 /* Forward declare protocols referenced. */
657 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
659 tree proto
= TREE_VALUE (lproto
);
661 if (TREE_CODE (proto
) == PROTOCOL_INTERFACE_TYPE
662 && PROTOCOL_NAME (proto
))
664 if (! PROTOCOL_FORWARD_DECL (proto
))
665 PROTOCOL_FORWARD_DECL (proto
) = (*runtime
.protocol_decl
) (proto
);
667 if (PROTOCOL_LIST (proto
))
668 generate_protocol_references (PROTOCOL_LIST (proto
));
673 /* --- new routines --- */
675 /* Output all strings. */
677 /* FIXME: don't use global vars for all this... */
679 /* This emits all the meta-data string tables (and finalizes each var
682 generate_strings (void)
684 tree chain
, string_expr
;
685 tree string
, decl
; /* , type;*/
687 for (chain
= class_names_chain
; chain
; chain
= TREE_CHAIN (chain
))
689 string
= TREE_VALUE (chain
);
690 decl
= TREE_PURPOSE (chain
);
691 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
692 IDENTIFIER_POINTER (string
));
693 finish_var_decl (decl
, string_expr
);
696 for (chain
= meth_var_names_chain
; chain
; chain
= TREE_CHAIN (chain
))
698 string
= TREE_VALUE (chain
);
699 decl
= TREE_PURPOSE (chain
);
700 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
701 IDENTIFIER_POINTER (string
));
702 finish_var_decl (decl
, string_expr
);
705 for (chain
= meth_var_types_chain
; chain
; chain
= TREE_CHAIN (chain
))
707 string
= TREE_VALUE (chain
);
708 decl
= TREE_PURPOSE (chain
);
709 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
710 IDENTIFIER_POINTER (string
));
711 finish_var_decl (decl
, string_expr
);
714 for (chain
= prop_names_attr_chain
; chain
; chain
= TREE_CHAIN (chain
))
716 string
= TREE_VALUE (chain
);
717 decl
= TREE_PURPOSE (chain
);
718 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
719 IDENTIFIER_POINTER (string
));
720 finish_var_decl (decl
, string_expr
);
724 #include "gt-objc-objc-runtime-shared-support.h"