1 /* Support routines shared by all runtimes.
2 Copyright (C) 2011-2015 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 "double-int.h"
36 #include "stringpool.h"
39 #include "cp/cp-tree.h"
44 #include "langhooks.h"
45 #include "c-family/c-objc.h"
48 /* When building Objective-C++, we are not linking against the C front-end
49 and so need to replicate the C tree-construction functions in some way. */
51 #define OBJCP_REMAP_FUNCTIONS
52 #include "objcp-decl.h"
55 /* Hooks for string decls etc. */
56 #include "objc-runtime-hooks.h"
58 #include "objc-runtime-shared-support.h"
59 #include "objc-encoding.h"
61 /* rt_trees identifiers - shared between NeXT implementations. These allow
62 the FE to tag meta-data in a manner that survives LTO and can be used when
63 the runtime requires that certain meta-data items appear in particular
65 #include "objc-next-metadata-tags.h"
66 extern GTY(()) tree objc_rt_trees[OCTI_RT_META_MAX];
68 /* Rather than repeatedly looking up the identifiers, we save them here. */
69 tree objc_rt_trees[OCTI_RT_META_MAX];
71 /* For building an objc struct. These might not be used when this file
72 is compiled as part of obj-c++. */
74 static bool objc_building_struct;
75 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
77 /* Start building a struct for objc. */
80 objc_start_struct (tree name)
82 gcc_assert (!objc_building_struct);
83 objc_building_struct = true;
84 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
87 /* Finish building a struct for objc. */
90 objc_finish_struct (tree type, tree fieldlist)
92 gcc_assert (objc_building_struct);
93 objc_building_struct = false;
94 return finish_struct (input_location, type, fieldlist, NULL_TREE,
99 build_sized_array_type (tree base_type, int size)
101 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
102 return build_array_type (base_type, index_type);
105 /* Create a declaration for field NAME of a given TYPE. */
108 create_field_decl (tree type, const char *name)
110 return build_decl (input_location,
111 FIELD_DECL, get_identifier (name), type);
115 add_field_decl (tree type, const char *name, tree **chain)
117 tree field = create_field_decl (type, name);
121 *chain = &DECL_CHAIN (field);
126 /* Create a global, static declaration for variable NAME of a given TYPE. The
127 finish_var_decl() routine will need to be called on it afterwards. */
130 start_var_decl (tree type, const char *name)
132 tree var = build_decl (input_location,
133 VAR_DECL, get_identifier (name), type);
134 TREE_STATIC (var) = 1;
135 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
136 DECL_IGNORED_P (var) = 1;
137 DECL_ARTIFICIAL (var) = 1;
138 DECL_CONTEXT (var) = NULL_TREE;
140 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
145 /* Finish off the variable declaration created by start_var_decl(). */
148 finish_var_decl (tree var, tree initializer)
150 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
153 /* Just a handy wrapper for add_objc_string. */
156 build_selector (tree ident)
158 return convert (objc_selector_type, add_objc_string (ident, meth_var_names));
161 /* --- templates --- */
163 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
164 This needs to be done just once per compilation. */
166 /* struct _objc_super {
167 struct _objc_object *self;
168 struct _objc_class *super_class;
169 [or Class cls; for the abi v2]
173 build_super_template (void)
175 tree decls, *chain = NULL;
177 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
179 /* struct _objc_object *self; */
180 decls = add_field_decl (objc_object_type, "self", &chain);
182 /* struct _objc_class *super_class; */
183 add_field_decl (build_pointer_type (objc_class_template),
184 "super_class", &chain);
186 objc_finish_struct (objc_super_template, decls);
189 /* To accomplish method prototyping without generating all kinds of
190 inane warnings, the definition of the dispatch table entries were
193 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
195 struct objc_method { SEL _cmd; ...; void *_imp; }; */
198 build_method_template (void)
201 tree decls, *chain = NULL;
203 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
206 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
208 /* char *method_types; */
209 add_field_decl (string_type_node, "method_types", &chain);
212 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
214 objc_finish_struct (_SLT_record, decls);
220 build_method_prototype_template (void)
223 tree decls, *chain = NULL;
225 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
228 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
230 /* char *method_types; */
231 add_field_decl (string_type_node, "method_types", &chain);
233 objc_finish_struct (proto_record, decls);
239 struct _objc__method_prototype_list *method_next;
241 struct objc_method method_list[method_count];
245 build_method_list_template (tree list_type, int size)
247 tree objc_ivar_list_record;
248 tree array_type, decls, *chain = NULL;
250 objc_ivar_list_record = objc_start_struct (NULL_TREE);
252 /* struct _objc__method_prototype_list *method_next; */
253 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
255 /* int method_count; */
256 add_field_decl (integer_type_node, "method_count", &chain);
258 /* struct objc_method method_list[]; */
259 array_type = build_sized_array_type (list_type, size);
260 add_field_decl (array_type, "method_list", &chain);
262 objc_finish_struct (objc_ivar_list_record, decls);
264 return objc_ivar_list_record;
267 /* struct objc_method_prototype_list {
269 struct objc_method_prototype {
276 build_method_prototype_list_template (tree list_type, int size)
278 tree objc_ivar_list_record;
279 tree array_type, decls, *chain = NULL;
281 /* Generate an unnamed struct definition. */
283 objc_ivar_list_record = objc_start_struct (NULL_TREE);
285 /* int method_count; */
286 decls = add_field_decl (integer_type_node, "method_count", &chain);
288 /* struct objc_method method_list[]; */
289 array_type = build_sized_array_type (list_type, size);
290 add_field_decl (array_type, "method_list", &chain);
292 objc_finish_struct (objc_ivar_list_record, decls);
294 return objc_ivar_list_record;
297 /* --- names, decls entry --- */
299 /* For each string section we have a chain which maps identifier nodes
300 to decls for the strings. */
302 static GTY(()) int meth_var_names_idx;
303 static GTY(()) int meth_var_types_idx;
304 static GTY(()) int property_name_attr_idx;
307 add_objc_string (tree ident, string_section section)
309 tree *chain, decl, type;
315 chain = &class_names_chain;
316 snprintf (buf, BUFSIZE, "_OBJC_ClassName_%s", IDENTIFIER_POINTER (ident));
319 chain = &meth_var_names_chain;
320 snprintf (buf, BUFSIZE, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
323 chain = &meth_var_types_chain;
324 snprintf (buf, BUFSIZE, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
326 case prop_names_attr:
327 chain = &prop_names_attr_chain;
328 snprintf (buf, BUFSIZE, "_OBJC_PropertyAttributeOrName_%d", property_name_attr_idx++);
336 if (TREE_VALUE (*chain) == ident)
337 return convert (string_type_node,
338 build_unary_op (input_location,
339 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
341 chain = &TREE_CHAIN (*chain);
344 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
345 /* Get a runtime-specific string decl which will be finish_var()'ed in
346 generate_strings (). */
347 decl = (*runtime.string_decl) (type, buf, section);
348 TREE_CONSTANT (decl) = 1;
349 *chain = tree_cons (decl, ident, NULL_TREE);
351 return convert (string_type_node,
352 build_unary_op (input_location, ADDR_EXPR, decl, 1));
355 /* --- shared metadata routines --- */
358 build_descriptor_table_initializer (tree type, tree entries)
360 vec<constructor_elt, va_gc> *inits = NULL;
364 vec<constructor_elt, va_gc> *elts = NULL;
366 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
367 build_selector (METHOD_SEL_NAME (entries)));
368 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
369 add_objc_string (METHOD_ENCODING (entries),
372 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
373 objc_build_constructor (type, elts));
375 entries = DECL_CHAIN (entries);
379 return objc_build_constructor (build_array_type (type, 0), inits);
383 build_dispatch_table_initializer (tree type, tree entries)
385 vec<constructor_elt, va_gc> *inits = NULL;
389 vec<constructor_elt, va_gc> *elems = NULL;
392 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
393 build_selector (METHOD_SEL_NAME (entries)));
395 /* Generate the method encoding if we don't have one already. */
396 if (! METHOD_ENCODING (entries))
397 METHOD_ENCODING (entries) =
398 encode_method_prototype (entries);
400 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
401 add_objc_string (METHOD_ENCODING (entries),
404 expr = convert (ptr_type_node,
405 build_unary_op (input_location, ADDR_EXPR,
406 METHOD_DEFINITION (entries), 1));
407 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
409 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
410 objc_build_constructor (type, elems));
412 entries = DECL_CHAIN (entries);
416 return objc_build_constructor (build_array_type (type, 0), inits);
419 /* Used only by build_*_selector_translation_table (). */
421 diagnose_missing_method (tree meth, location_t here)
425 for (method_chain = meth_var_names_chain;
427 method_chain = TREE_CHAIN (method_chain))
429 if (TREE_VALUE (method_chain) == meth)
437 warning_at (here, 0, "creating selector for nonexistent method %qE",
443 init_module_descriptor (tree type, long vers)
447 vec<constructor_elt, va_gc> *v = NULL;
449 /* No really useful place to point to. */
450 loc = UNKNOWN_LOCATION;
452 /* version = { 1, ... } */
454 expr = build_int_cst (long_integer_type_node, vers);
455 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
457 /* size = { ..., sizeof (struct _objc_module), ... } */
459 expr = convert (long_integer_type_node,
460 size_in_bytes (objc_module_template));
461 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
463 /* Don't provide any file name for security reasons. */
464 /* name = { ..., "", ... } */
466 expr = add_objc_string (get_identifier (""), class_names);
467 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
469 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
471 ltyp = build_pointer_type (xref_tag (RECORD_TYPE,
472 get_identifier (UTAG_SYMTAB)));
473 if (UOBJC_SYMBOLS_decl)
474 expr = convert (ltyp, build_unary_op (loc,
475 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0));
477 expr = convert (ltyp, null_pointer_node);
478 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
480 return objc_build_constructor (type, v);
483 /* Write out the data structures to describe Objective C classes defined.
485 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
488 build_module_descriptor (long vers, tree attr)
490 tree decls, *chain = NULL;
493 push_lang_context (lang_name_c); /* extern "C" */
496 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
499 decls = add_field_decl (long_integer_type_node, "version", &chain);
502 add_field_decl (long_integer_type_node, "size", &chain);
505 add_field_decl (string_type_node, "name", &chain);
507 /* struct _objc_symtab *symtab; */
508 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
509 get_identifier (UTAG_SYMTAB))),
512 objc_finish_struct (objc_module_template, decls);
514 /* Create an instance of "_objc_module". */
515 UOBJC_MODULES_decl = start_var_decl (objc_module_template,
516 /* FIXME - why the conditional
519 flag_next_runtime ? "_OBJC_Module" : "_OBJC_Module");
521 /* This is the root of the metadata for defined classes and categories, it
522 is referenced by the runtime and, therefore, needed. */
523 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
525 /* Allow the runtime to mark meta-data such that it can be assigned to target
526 specific sections by the back-end. */
528 DECL_ATTRIBUTES (UOBJC_MODULES_decl) = attr;
530 finish_var_decl (UOBJC_MODULES_decl,
531 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl),
540 build_ivar_list_initializer (tree type, tree field_decl)
542 vec<constructor_elt, va_gc> *inits = NULL;
546 vec<constructor_elt, va_gc> *ivar = NULL;
550 if (DECL_NAME (field_decl))
551 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
552 add_objc_string (DECL_NAME (field_decl),
555 /* Unnamed bit-field ivar (yuck). */
556 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
559 id = add_objc_string (encode_field_decl (field_decl),
561 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
564 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
565 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
566 objc_build_constructor (type, ivar));
568 field_decl = DECL_CHAIN (field_decl);
569 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
573 return objc_build_constructor (build_array_type (type, 0), inits);
578 struct objc_ivar ivar_list[ivar_count];
582 build_ivar_list_template (tree list_type, int size)
584 tree objc_ivar_list_record;
585 tree array_type, decls, *chain = NULL;
587 objc_ivar_list_record = objc_start_struct (NULL_TREE);
589 /* int ivar_count; */
590 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
592 /* struct objc_ivar ivar_list[]; */
593 array_type = build_sized_array_type (list_type, size);
594 add_field_decl (array_type, "ivar_list", &chain);
596 objc_finish_struct (objc_ivar_list_record, decls);
598 return objc_ivar_list_record;
601 /* struct _objc_ivar {
608 build_ivar_template (void)
610 tree objc_ivar_id, objc_ivar_record;
611 tree decls, *chain = NULL;
613 objc_ivar_id = get_identifier (UTAG_IVAR);
614 objc_ivar_record = objc_start_struct (objc_ivar_id);
616 /* char *ivar_name; */
617 decls = add_field_decl (string_type_node, "ivar_name", &chain);
619 /* char *ivar_type; */
620 add_field_decl (string_type_node, "ivar_type", &chain);
622 /* int ivar_offset; */
623 add_field_decl (integer_type_node, "ivar_offset", &chain);
625 objc_finish_struct (objc_ivar_record, decls);
627 return objc_ivar_record;
630 /* Used by NeXT ABI=0..2 */
632 build_next_selector_translation_table (void)
635 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
638 tree decl = TREE_PURPOSE (chain);
643 loc = DECL_SOURCE_LOCATION (decl);
645 loc = UNKNOWN_LOCATION;
646 diagnose_missing_method (TREE_VALUE (chain), loc);
649 expr = build_selector (TREE_VALUE (chain));
653 /* Entries of this form are used for references to methods.
654 The runtime re-writes these on start-up, but the compiler can't see
655 that and optimizes it away unless we force it. */
656 DECL_PRESERVE_P (decl) = 1;
657 finish_var_decl (decl, expr);
663 generate_protocol_references (tree plist)
667 /* Forward declare protocols referenced. */
668 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
670 tree proto = TREE_VALUE (lproto);
672 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
673 && PROTOCOL_NAME (proto))
675 if (! PROTOCOL_FORWARD_DECL (proto))
676 PROTOCOL_FORWARD_DECL (proto) = (*runtime.protocol_decl) (proto);
678 if (PROTOCOL_LIST (proto))
679 generate_protocol_references (PROTOCOL_LIST (proto));
684 /* --- new routines --- */
686 /* Output all strings. */
688 /* FIXME: don't use global vars for all this... */
690 /* This emits all the meta-data string tables (and finalizes each var
693 generate_strings (void)
695 tree chain, string_expr;
696 tree string, decl; /* , type;*/
698 for (chain = class_names_chain; chain; chain = TREE_CHAIN (chain))
700 string = TREE_VALUE (chain);
701 decl = TREE_PURPOSE (chain);
702 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
703 IDENTIFIER_POINTER (string));
704 finish_var_decl (decl, string_expr);
707 for (chain = meth_var_names_chain; chain; chain = TREE_CHAIN (chain))
709 string = TREE_VALUE (chain);
710 decl = TREE_PURPOSE (chain);
711 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
712 IDENTIFIER_POINTER (string));
713 finish_var_decl (decl, string_expr);
716 for (chain = meth_var_types_chain; chain; chain = TREE_CHAIN (chain))
718 string = TREE_VALUE (chain);
719 decl = TREE_PURPOSE (chain);
720 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
721 IDENTIFIER_POINTER (string));
722 finish_var_decl (decl, string_expr);
725 for (chain = prop_names_attr_chain; chain; chain = TREE_CHAIN (chain))
727 string = TREE_VALUE (chain);
728 decl = TREE_PURPOSE (chain);
729 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
730 IDENTIFIER_POINTER (string));
731 finish_var_decl (decl, string_expr);
735 #include "gt-objc-objc-runtime-shared-support.h"