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 "stringpool.h"
31 #include "cp/cp-tree.h"
36 #include "langhooks.h"
37 #include "c-family/c-objc.h"
40 /* When building Objective-C++, we are not linking against the C front-end
41 and so need to replicate the C tree-construction functions in some way. */
43 #define OBJCP_REMAP_FUNCTIONS
44 #include "objcp-decl.h"
47 /* Hooks for string decls etc. */
48 #include "objc-runtime-hooks.h"
50 #include "objc-runtime-shared-support.h"
51 #include "objc-encoding.h"
53 /* rt_trees identifiers - shared between NeXT implementations. These allow
54 the FE to tag meta-data in a manner that survives LTO and can be used when
55 the runtime requires that certain meta-data items appear in particular
57 #include "objc-next-metadata-tags.h"
58 extern GTY(()) tree objc_rt_trees
[OCTI_RT_META_MAX
];
60 /* Rather than repeatedly looking up the identifiers, we save them here. */
61 tree objc_rt_trees
[OCTI_RT_META_MAX
];
63 /* For building an objc struct. These might not be used when this file
64 is compiled as part of obj-c++. */
66 static bool objc_building_struct
;
67 static struct c_struct_parse_info
*objc_struct_info ATTRIBUTE_UNUSED
;
69 /* Start building a struct for objc. */
72 objc_start_struct (tree name
)
74 gcc_assert (!objc_building_struct
);
75 objc_building_struct
= true;
76 return start_struct (input_location
, RECORD_TYPE
, name
, &objc_struct_info
);
79 /* Finish building a struct for objc. */
82 objc_finish_struct (tree type
, tree fieldlist
)
84 gcc_assert (objc_building_struct
);
85 objc_building_struct
= false;
86 return finish_struct (input_location
, type
, fieldlist
, NULL_TREE
,
91 build_sized_array_type (tree base_type
, int size
)
93 tree index_type
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
94 return build_array_type (base_type
, index_type
);
97 /* Create a declaration for field NAME of a given TYPE. */
100 create_field_decl (tree type
, const char *name
)
102 return build_decl (input_location
,
103 FIELD_DECL
, get_identifier (name
), type
);
107 add_field_decl (tree type
, const char *name
, tree
**chain
)
109 tree field
= create_field_decl (type
, name
);
113 *chain
= &DECL_CHAIN (field
);
118 /* Create a global, static declaration for variable NAME of a given TYPE. The
119 finish_var_decl() routine will need to be called on it afterwards. */
122 start_var_decl (tree type
, const char *name
)
124 tree var
= build_decl (input_location
,
125 VAR_DECL
, get_identifier (name
), type
);
126 TREE_STATIC (var
) = 1;
127 DECL_INITIAL (var
) = error_mark_node
; /* A real initializer is coming... */
128 DECL_IGNORED_P (var
) = 1;
129 DECL_ARTIFICIAL (var
) = 1;
130 DECL_CONTEXT (var
) = NULL_TREE
;
132 DECL_THIS_STATIC (var
) = 1; /* squash redeclaration errors */
137 /* Finish off the variable declaration created by start_var_decl(). */
140 finish_var_decl (tree var
, tree initializer
)
142 finish_decl (var
, input_location
, initializer
, NULL_TREE
, NULL_TREE
);
145 /* Just a handy wrapper for add_objc_string. */
148 build_selector (tree ident
)
150 return convert (objc_selector_type
, add_objc_string (ident
, meth_var_names
));
153 /* --- templates --- */
155 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
156 This needs to be done just once per compilation. */
158 /* struct _objc_super {
159 struct _objc_object *self;
160 struct _objc_class *super_class;
161 [or Class cls; for the abi v2]
165 build_super_template (void)
167 tree decls
, *chain
= NULL
;
169 objc_super_template
= objc_start_struct (get_identifier (UTAG_SUPER
));
171 /* struct _objc_object *self; */
172 decls
= add_field_decl (objc_object_type
, "self", &chain
);
174 /* struct _objc_class *super_class; */
175 add_field_decl (build_pointer_type (objc_class_template
),
176 "super_class", &chain
);
178 objc_finish_struct (objc_super_template
, decls
);
181 /* To accomplish method prototyping without generating all kinds of
182 inane warnings, the definition of the dispatch table entries were
185 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
187 struct objc_method { SEL _cmd; ...; void *_imp; }; */
190 build_method_template (void)
193 tree decls
, *chain
= NULL
;
195 _SLT_record
= objc_start_struct (get_identifier (UTAG_METHOD
));
198 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
200 /* char *method_types; */
201 add_field_decl (string_type_node
, "method_types", &chain
);
204 add_field_decl (build_pointer_type (void_type_node
), "_imp", &chain
);
206 objc_finish_struct (_SLT_record
, decls
);
212 build_method_prototype_template (void)
215 tree decls
, *chain
= NULL
;
217 proto_record
= objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE
));
220 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
222 /* char *method_types; */
223 add_field_decl (string_type_node
, "method_types", &chain
);
225 objc_finish_struct (proto_record
, decls
);
231 struct _objc__method_prototype_list *method_next;
233 struct objc_method method_list[method_count];
237 build_method_list_template (tree list_type
, int size
)
239 tree objc_ivar_list_record
;
240 tree array_type
, decls
, *chain
= NULL
;
242 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
244 /* struct _objc__method_prototype_list *method_next; */
245 decls
= add_field_decl (objc_method_proto_list_ptr
, "method_next", &chain
);
247 /* int method_count; */
248 add_field_decl (integer_type_node
, "method_count", &chain
);
250 /* struct objc_method method_list[]; */
251 array_type
= build_sized_array_type (list_type
, size
);
252 add_field_decl (array_type
, "method_list", &chain
);
254 objc_finish_struct (objc_ivar_list_record
, decls
);
256 return objc_ivar_list_record
;
259 /* struct objc_method_prototype_list {
261 struct objc_method_prototype {
268 build_method_prototype_list_template (tree list_type
, int size
)
270 tree objc_ivar_list_record
;
271 tree array_type
, decls
, *chain
= NULL
;
273 /* Generate an unnamed struct definition. */
275 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
277 /* int method_count; */
278 decls
= add_field_decl (integer_type_node
, "method_count", &chain
);
280 /* struct objc_method method_list[]; */
281 array_type
= build_sized_array_type (list_type
, size
);
282 add_field_decl (array_type
, "method_list", &chain
);
284 objc_finish_struct (objc_ivar_list_record
, decls
);
286 return objc_ivar_list_record
;
289 /* --- names, decls entry --- */
291 /* For each string section we have a chain which maps identifier nodes
292 to decls for the strings. */
294 static GTY(()) int meth_var_names_idx
;
295 static GTY(()) int meth_var_types_idx
;
296 static GTY(()) int property_name_attr_idx
;
299 add_objc_string (tree ident
, string_section section
)
301 tree
*chain
, decl
, type
;
307 chain
= &class_names_chain
;
308 snprintf (buf
, BUFSIZE
, "_OBJC_ClassName_%s", IDENTIFIER_POINTER (ident
));
311 chain
= &meth_var_names_chain
;
312 snprintf (buf
, BUFSIZE
, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx
++);
315 chain
= &meth_var_types_chain
;
316 snprintf (buf
, BUFSIZE
, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx
++);
318 case prop_names_attr
:
319 chain
= &prop_names_attr_chain
;
320 snprintf (buf
, BUFSIZE
, "_OBJC_PropertyAttributeOrName_%d", property_name_attr_idx
++);
328 if (TREE_VALUE (*chain
) == ident
)
329 return convert (string_type_node
,
330 build_unary_op (input_location
,
331 ADDR_EXPR
, TREE_PURPOSE (*chain
), 1));
333 chain
= &TREE_CHAIN (*chain
);
336 type
= build_sized_array_type (char_type_node
, IDENTIFIER_LENGTH (ident
) + 1);
337 /* Get a runtime-specific string decl which will be finish_var()'ed in
338 generate_strings (). */
339 decl
= (*runtime
.string_decl
) (type
, buf
, section
);
340 TREE_CONSTANT (decl
) = 1;
341 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
343 return convert (string_type_node
,
344 build_unary_op (input_location
, ADDR_EXPR
, decl
, 1));
347 /* --- shared metadata routines --- */
350 build_descriptor_table_initializer (tree type
, tree entries
)
352 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
356 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
358 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
359 build_selector (METHOD_SEL_NAME (entries
)));
360 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
361 add_objc_string (METHOD_ENCODING (entries
),
364 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
365 objc_build_constructor (type
, elts
));
367 entries
= DECL_CHAIN (entries
);
371 return objc_build_constructor (build_array_type (type
, 0), inits
);
375 build_dispatch_table_initializer (tree type
, tree entries
)
377 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
381 vec
<constructor_elt
, va_gc
> *elems
= NULL
;
384 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
385 build_selector (METHOD_SEL_NAME (entries
)));
387 /* Generate the method encoding if we don't have one already. */
388 if (! METHOD_ENCODING (entries
))
389 METHOD_ENCODING (entries
) =
390 encode_method_prototype (entries
);
392 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
393 add_objc_string (METHOD_ENCODING (entries
),
396 expr
= convert (ptr_type_node
,
397 build_unary_op (input_location
, ADDR_EXPR
,
398 METHOD_DEFINITION (entries
), 1));
399 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
, expr
);
401 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
402 objc_build_constructor (type
, elems
));
404 entries
= DECL_CHAIN (entries
);
408 return objc_build_constructor (build_array_type (type
, 0), inits
);
411 /* Used only by build_*_selector_translation_table (). */
413 diagnose_missing_method (tree meth
, location_t here
)
417 for (method_chain
= meth_var_names_chain
;
419 method_chain
= TREE_CHAIN (method_chain
))
421 if (TREE_VALUE (method_chain
) == meth
)
429 warning_at (here
, 0, "creating selector for nonexistent method %qE",
435 init_module_descriptor (tree type
, long vers
)
439 vec
<constructor_elt
, va_gc
> *v
= NULL
;
441 /* No really useful place to point to. */
442 loc
= UNKNOWN_LOCATION
;
444 /* version = { 1, ... } */
446 expr
= build_int_cst (long_integer_type_node
, vers
);
447 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
449 /* size = { ..., sizeof (struct _objc_module), ... } */
451 expr
= convert (long_integer_type_node
,
452 size_in_bytes (objc_module_template
));
453 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
455 /* Don't provide any file name for security reasons. */
456 /* name = { ..., "", ... } */
458 expr
= add_objc_string (get_identifier (""), class_names
);
459 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
461 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
463 ltyp
= build_pointer_type (xref_tag (RECORD_TYPE
,
464 get_identifier (UTAG_SYMTAB
)));
465 if (UOBJC_SYMBOLS_decl
)
466 expr
= convert (ltyp
, build_unary_op (loc
,
467 ADDR_EXPR
, UOBJC_SYMBOLS_decl
, 0));
469 expr
= convert (ltyp
, null_pointer_node
);
470 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
472 return objc_build_constructor (type
, v
);
475 /* Write out the data structures to describe Objective C classes defined.
477 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
480 build_module_descriptor (long vers
, tree attr
)
482 tree decls
, *chain
= NULL
;
485 push_lang_context (lang_name_c
); /* extern "C" */
488 objc_module_template
= objc_start_struct (get_identifier (UTAG_MODULE
));
491 decls
= add_field_decl (long_integer_type_node
, "version", &chain
);
494 add_field_decl (long_integer_type_node
, "size", &chain
);
497 add_field_decl (string_type_node
, "name", &chain
);
499 /* struct _objc_symtab *symtab; */
500 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE
,
501 get_identifier (UTAG_SYMTAB
))),
504 objc_finish_struct (objc_module_template
, decls
);
506 /* Create an instance of "_objc_module". */
507 UOBJC_MODULES_decl
= start_var_decl (objc_module_template
,
508 /* FIXME - why the conditional
511 flag_next_runtime
? "_OBJC_Module" : "_OBJC_Module");
513 /* This is the root of the metadata for defined classes and categories, it
514 is referenced by the runtime and, therefore, needed. */
515 DECL_PRESERVE_P (UOBJC_MODULES_decl
) = 1;
517 /* Squash `defined but not used' warning. */
518 TREE_USED (UOBJC_MODULES_decl
) = 1;
520 /* Allow the runtime to mark meta-data such that it can be assigned to target
521 specific sections by the back-end. */
523 DECL_ATTRIBUTES (UOBJC_MODULES_decl
) = attr
;
525 finish_var_decl (UOBJC_MODULES_decl
,
526 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl
),
535 build_ivar_list_initializer (tree type
, tree field_decl
)
537 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
541 vec
<constructor_elt
, va_gc
> *ivar
= NULL
;
545 if (DECL_NAME (field_decl
))
546 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
,
547 add_objc_string (DECL_NAME (field_decl
),
550 /* Unnamed bit-field ivar (yuck). */
551 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
554 id
= add_objc_string (encode_field_decl (field_decl
),
556 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, id
);
559 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, byte_position (field_decl
));
560 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
561 objc_build_constructor (type
, ivar
));
563 field_decl
= DECL_CHAIN (field_decl
);
564 while (field_decl
&& TREE_CODE (field_decl
) != FIELD_DECL
);
568 return objc_build_constructor (build_array_type (type
, 0), inits
);
573 struct objc_ivar ivar_list[ivar_count];
577 build_ivar_list_template (tree list_type
, int size
)
579 tree objc_ivar_list_record
;
580 tree array_type
, decls
, *chain
= NULL
;
582 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
584 /* int ivar_count; */
585 decls
= add_field_decl (integer_type_node
, "ivar_count", &chain
);
587 /* struct objc_ivar ivar_list[]; */
588 array_type
= build_sized_array_type (list_type
, size
);
589 add_field_decl (array_type
, "ivar_list", &chain
);
591 objc_finish_struct (objc_ivar_list_record
, decls
);
593 return objc_ivar_list_record
;
596 /* struct _objc_ivar {
603 build_ivar_template (void)
605 tree objc_ivar_id
, objc_ivar_record
;
606 tree decls
, *chain
= NULL
;
608 objc_ivar_id
= get_identifier (UTAG_IVAR
);
609 objc_ivar_record
= objc_start_struct (objc_ivar_id
);
611 /* char *ivar_name; */
612 decls
= add_field_decl (string_type_node
, "ivar_name", &chain
);
614 /* char *ivar_type; */
615 add_field_decl (string_type_node
, "ivar_type", &chain
);
617 /* int ivar_offset; */
618 add_field_decl (integer_type_node
, "ivar_offset", &chain
);
620 objc_finish_struct (objc_ivar_record
, decls
);
622 return objc_ivar_record
;
625 /* Used by NeXT ABI=0..2 */
627 build_next_selector_translation_table (void)
630 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
633 tree decl
= TREE_PURPOSE (chain
);
638 loc
= DECL_SOURCE_LOCATION (decl
);
640 loc
= UNKNOWN_LOCATION
;
641 diagnose_missing_method (TREE_VALUE (chain
), loc
);
644 expr
= build_selector (TREE_VALUE (chain
));
648 /* Entries of this form are used for references to methods.
649 The runtime re-writes these on start-up, but the compiler can't see
650 that and optimizes it away unless we force it. */
651 DECL_PRESERVE_P (decl
) = 1;
652 finish_var_decl (decl
, expr
);
658 generate_protocol_references (tree plist
)
662 /* Forward declare protocols referenced. */
663 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
665 tree proto
= TREE_VALUE (lproto
);
667 if (TREE_CODE (proto
) == PROTOCOL_INTERFACE_TYPE
668 && PROTOCOL_NAME (proto
))
670 if (! PROTOCOL_FORWARD_DECL (proto
))
671 PROTOCOL_FORWARD_DECL (proto
) = (*runtime
.protocol_decl
) (proto
);
673 if (PROTOCOL_LIST (proto
))
674 generate_protocol_references (PROTOCOL_LIST (proto
));
679 /* --- new routines --- */
681 /* Output all strings. */
683 /* FIXME: don't use global vars for all this... */
685 /* This emits all the meta-data string tables (and finalizes each var
688 generate_strings (void)
690 tree chain
, string_expr
;
691 tree string
, decl
; /* , type;*/
693 for (chain
= class_names_chain
; chain
; chain
= TREE_CHAIN (chain
))
695 string
= TREE_VALUE (chain
);
696 decl
= TREE_PURPOSE (chain
);
697 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
698 IDENTIFIER_POINTER (string
));
699 finish_var_decl (decl
, string_expr
);
702 for (chain
= meth_var_names_chain
; chain
; chain
= TREE_CHAIN (chain
))
704 string
= TREE_VALUE (chain
);
705 decl
= TREE_PURPOSE (chain
);
706 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
707 IDENTIFIER_POINTER (string
));
708 finish_var_decl (decl
, string_expr
);
711 for (chain
= meth_var_types_chain
; chain
; chain
= TREE_CHAIN (chain
))
713 string
= TREE_VALUE (chain
);
714 decl
= TREE_PURPOSE (chain
);
715 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
716 IDENTIFIER_POINTER (string
));
717 finish_var_decl (decl
, string_expr
);
720 for (chain
= prop_names_attr_chain
; chain
; chain
= TREE_CHAIN (chain
))
722 string
= TREE_VALUE (chain
);
723 decl
= TREE_PURPOSE (chain
);
724 string_expr
= my_build_string (IDENTIFIER_LENGTH (string
) + 1,
725 IDENTIFIER_POINTER (string
));
726 finish_var_decl (decl
, string_expr
);
730 #include "gt-objc-objc-runtime-shared-support.h"