annotate gcc/objc/objc-gnu-runtime-abi-01.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* GNU Runtime ABI version 8
kono
parents:
diff changeset
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3 Contributed by Iain Sandoe (split from objc-act.c)
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
10 any later version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
15 GNU General Public License for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #include "config.h"
kono
parents:
diff changeset
22 #include "system.h"
kono
parents:
diff changeset
23 #include "coretypes.h"
kono
parents:
diff changeset
24 #include "options.h"
kono
parents:
diff changeset
25 #include "tree.h"
kono
parents:
diff changeset
26 #include "stringpool.h"
kono
parents:
diff changeset
27 #include "attribs.h"
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 #ifdef OBJCPLUS
kono
parents:
diff changeset
30 #include "cp/cp-tree.h"
kono
parents:
diff changeset
31 #else
kono
parents:
diff changeset
32 #include "c/c-tree.h"
kono
parents:
diff changeset
33 #include "c/c-lang.h"
kono
parents:
diff changeset
34 #endif
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 #include "langhooks.h"
kono
parents:
diff changeset
37 #include "c-family/c-objc.h"
kono
parents:
diff changeset
38 #include "objc-act.h"
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 /* When building Objective-C++, we are not linking against the C front-end
kono
parents:
diff changeset
41 and so need to replicate the C tree-construction functions in some way. */
kono
parents:
diff changeset
42 #ifdef OBJCPLUS
kono
parents:
diff changeset
43 #define OBJCP_REMAP_FUNCTIONS
kono
parents:
diff changeset
44 #include "objcp-decl.h"
kono
parents:
diff changeset
45 #endif /* OBJCPLUS */
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 #include "toplev.h"
kono
parents:
diff changeset
48 #include "tree-iterator.h"
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 #include "objc-runtime-hooks.h"
kono
parents:
diff changeset
51 #include "objc-runtime-shared-support.h"
kono
parents:
diff changeset
52 #include "objc-encoding.h"
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 /* GNU runtime private definitions. */
kono
parents:
diff changeset
55 #define DEF_CONSTANT_STRING_CLASS_NAME "NXConstantString"
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 #define TAG_GETCLASS "objc_get_class"
kono
parents:
diff changeset
58 #define TAG_GETMETACLASS "objc_get_meta_class"
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 #define TAG_MSGSEND "objc_msg_lookup"
kono
parents:
diff changeset
61 #define TAG_MSGSENDSUPER "objc_msg_lookup_super"
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 /* GNU-specific tags. */
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 #define TAG_EXECCLASS "__objc_exec_class"
kono
parents:
diff changeset
66 #define TAG_GNUINIT "__objc_gnu_init"
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 /* The version identifies which language generation and runtime
kono
parents:
diff changeset
69 the module (file) was compiled for, and is recorded in the
kono
parents:
diff changeset
70 module descriptor. */
kono
parents:
diff changeset
71 #define OBJC_VERSION 8
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 #define PROTOCOL_VERSION 2
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 /* This macro provides a method of removing ambiguity between runtimes
kono
parents:
diff changeset
76 when LTO is in use on targets supporting multiple runtimes.
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 For example, at present, any target that includes an implementation of
kono
parents:
diff changeset
79 the NeXT runtime needs to place Objective-C meta-data into specific
kono
parents:
diff changeset
80 named sections. This should _not_ be done for the GNU runtime, and the
kono
parents:
diff changeset
81 following macro is used to attach Objective-C private attributes that may
kono
parents:
diff changeset
82 be used to identify the runtime for which the meta-data are intended. */
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 #define OBJCMETA(DECL,VERS,KIND) \
kono
parents:
diff changeset
85 if (VERS) \
kono
parents:
diff changeset
86 DECL_ATTRIBUTES (DECL) = build_tree_list ((VERS), (KIND));
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 static void gnu_runtime_01_initialize (void);
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 static void build_selector_template (void);
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 static tree gnu_runtime_abi_01_super_superclassfield_id (void);
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 static tree gnu_runtime_abi_01_class_decl (tree);
kono
parents:
diff changeset
95 static tree gnu_runtime_abi_01_metaclass_decl (tree);
kono
parents:
diff changeset
96 static tree gnu_runtime_abi_01_category_decl (tree);
kono
parents:
diff changeset
97 static tree gnu_runtime_abi_01_protocol_decl (tree);
kono
parents:
diff changeset
98 static tree gnu_runtime_abi_01_string_decl (tree, const char *, string_section);
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 static tree gnu_runtime_abi_01_get_class_reference (tree);
kono
parents:
diff changeset
101 static tree gnu_runtime_abi_01_build_typed_selector_reference (location_t, tree,
kono
parents:
diff changeset
102 tree);
kono
parents:
diff changeset
103 static tree gnu_runtime_abi_01_get_protocol_reference (location_t, tree);
kono
parents:
diff changeset
104 static tree gnu_runtime_abi_01_build_ivar_ref (location_t, tree, tree);
kono
parents:
diff changeset
105 static tree gnu_runtime_abi_01_get_class_super_ref (location_t, struct imp_entry *, bool);
kono
parents:
diff changeset
106 static tree gnu_runtime_abi_01_get_category_super_ref (location_t, struct imp_entry *, bool);
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 static tree gnu_runtime_abi_01_receiver_is_class_object (tree);
kono
parents:
diff changeset
109 static void gnu_runtime_abi_01_get_arg_type_list_base (vec<tree, va_gc> **,
kono
parents:
diff changeset
110 tree, int, int);
kono
parents:
diff changeset
111 static tree gnu_runtime_abi_01_build_objc_method_call (location_t, tree, tree,
kono
parents:
diff changeset
112 tree, tree, tree, int);
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 static bool gnu_runtime_abi_01_setup_const_string_class_decl (void);
kono
parents:
diff changeset
115 static tree gnu_runtime_abi_01_build_const_string_constructor (location_t, tree,int);
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 static void objc_generate_v1_gnu_metadata (void);
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 static tree objc_eh_runtime_type (tree type);
kono
parents:
diff changeset
120 static tree objc_eh_personality (void);
kono
parents:
diff changeset
121 static tree objc_build_exc_ptr (struct objc_try_context **);
kono
parents:
diff changeset
122 static tree build_throw_stmt (location_t, tree, bool);
kono
parents:
diff changeset
123 static tree begin_catch (struct objc_try_context **, tree, tree, tree, bool);
kono
parents:
diff changeset
124 static void finish_catch (struct objc_try_context **, tree);
kono
parents:
diff changeset
125 static tree finish_try_stmt (struct objc_try_context **);
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 bool
kono
parents:
diff changeset
128 objc_gnu_runtime_abi_01_init (objc_runtime_hooks *rthooks)
kono
parents:
diff changeset
129 {
kono
parents:
diff changeset
130 /* GNU runtime does not need the compiler to change code in order to do GC. */
kono
parents:
diff changeset
131 if (flag_objc_gc)
kono
parents:
diff changeset
132 {
kono
parents:
diff changeset
133 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
kono
parents:
diff changeset
134 flag_objc_gc = 0;
kono
parents:
diff changeset
135 }
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 /* Although I guess we could, we don't currently support SJLJ exceptions for the
kono
parents:
diff changeset
138 GNU runtime. */
kono
parents:
diff changeset
139 if (flag_objc_sjlj_exceptions)
kono
parents:
diff changeset
140 {
kono
parents:
diff changeset
141 inform (UNKNOWN_LOCATION, "%<-fobjc-sjlj-exceptions%> is ignored for %<-fgnu-runtime%>");
kono
parents:
diff changeset
142 flag_objc_sjlj_exceptions = 0;
kono
parents:
diff changeset
143 }
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 /* TODO: Complain if -fobjc-abi-version=N was used. */
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 /* TODO: Complain if -fobj-nilcheck was used. */
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 rthooks->initialize = gnu_runtime_01_initialize;
kono
parents:
diff changeset
150 rthooks->default_constant_string_class_name = DEF_CONSTANT_STRING_CLASS_NAME;
kono
parents:
diff changeset
151 rthooks->tag_getclass = TAG_GETCLASS;
kono
parents:
diff changeset
152 rthooks->super_superclassfield_ident = gnu_runtime_abi_01_super_superclassfield_id;
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 rthooks->class_decl = gnu_runtime_abi_01_class_decl;
kono
parents:
diff changeset
155 rthooks->metaclass_decl = gnu_runtime_abi_01_metaclass_decl;
kono
parents:
diff changeset
156 rthooks->category_decl = gnu_runtime_abi_01_category_decl;
kono
parents:
diff changeset
157 rthooks->protocol_decl = gnu_runtime_abi_01_protocol_decl;
kono
parents:
diff changeset
158 rthooks->string_decl = gnu_runtime_abi_01_string_decl;
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 rthooks->get_class_reference = gnu_runtime_abi_01_get_class_reference;
kono
parents:
diff changeset
161 rthooks->build_selector_reference = gnu_runtime_abi_01_build_typed_selector_reference;
kono
parents:
diff changeset
162 rthooks->get_protocol_reference = gnu_runtime_abi_01_get_protocol_reference;
kono
parents:
diff changeset
163 rthooks->build_ivar_reference = gnu_runtime_abi_01_build_ivar_ref;
kono
parents:
diff changeset
164 rthooks->get_class_super_ref = gnu_runtime_abi_01_get_class_super_ref;
kono
parents:
diff changeset
165 rthooks->get_category_super_ref = gnu_runtime_abi_01_get_category_super_ref;
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 rthooks->receiver_is_class_object = gnu_runtime_abi_01_receiver_is_class_object;
kono
parents:
diff changeset
168 rthooks->get_arg_type_list_base = gnu_runtime_abi_01_get_arg_type_list_base;
kono
parents:
diff changeset
169 rthooks->build_objc_method_call = gnu_runtime_abi_01_build_objc_method_call;
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 rthooks->setup_const_string_class_decl =
kono
parents:
diff changeset
172 gnu_runtime_abi_01_setup_const_string_class_decl;
kono
parents:
diff changeset
173 rthooks->build_const_string_constructor =
kono
parents:
diff changeset
174 gnu_runtime_abi_01_build_const_string_constructor;
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 rthooks->build_throw_stmt = build_throw_stmt;
kono
parents:
diff changeset
177 rthooks->build_exc_ptr = objc_build_exc_ptr;
kono
parents:
diff changeset
178 rthooks->begin_catch = begin_catch;
kono
parents:
diff changeset
179 rthooks->finish_catch = finish_catch;
kono
parents:
diff changeset
180 rthooks->finish_try_stmt = finish_try_stmt;
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 rthooks->generate_metadata = objc_generate_v1_gnu_metadata;
kono
parents:
diff changeset
183 return true;
kono
parents:
diff changeset
184 }
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 static void build_selector_table_decl (void);
kono
parents:
diff changeset
187 static void build_class_template (void);
kono
parents:
diff changeset
188 static void build_category_template (void);
kono
parents:
diff changeset
189 static void build_protocol_template (void);
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 static GTY(()) tree objc_meta;
kono
parents:
diff changeset
192 static GTY(()) tree meta_base;
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 static void gnu_runtime_01_initialize (void)
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 tree type, ftype, IMP_type;
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 /* We do not need to mark GNU ObjC metadata for different sections,
kono
parents:
diff changeset
199 however, we do need to make sure that it is not mistaken for NeXT
kono
parents:
diff changeset
200 metadata. */
kono
parents:
diff changeset
201 objc_meta = get_identifier ("OBJC1METG");
kono
parents:
diff changeset
202 meta_base = get_identifier ("NONE");
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 /* Declare type of selector-objects that represent an operation name. */
kono
parents:
diff changeset
205 /* `const struct objc_selector *' */
kono
parents:
diff changeset
206 type = xref_tag (RECORD_TYPE, get_identifier (TAG_SELECTOR));
kono
parents:
diff changeset
207 type = build_qualified_type (type, TYPE_QUAL_CONST);
kono
parents:
diff changeset
208 objc_selector_type = build_pointer_type (type);
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 /* typedef id (*IMP)(id, SEL, ...); */
kono
parents:
diff changeset
211 ftype = build_varargs_function_type_list (objc_object_type,
kono
parents:
diff changeset
212 objc_object_type,
kono
parents:
diff changeset
213 objc_selector_type,
kono
parents:
diff changeset
214 NULL_TREE);
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 IMP_type = build_pointer_type (ftype);
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 build_class_template ();
kono
parents:
diff changeset
219 build_super_template ();
kono
parents:
diff changeset
220 build_protocol_template ();
kono
parents:
diff changeset
221 build_category_template ();
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 /* GNU runtime messenger entry points. */
kono
parents:
diff changeset
224 /* TREE_NOTHROW is cleared for the message-sending functions,
kono
parents:
diff changeset
225 because the function that gets called can throw in Obj-C++, or
kono
parents:
diff changeset
226 could itself call something that can throw even in Obj-C. */
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 /* IMP objc_msg_lookup (id, SEL); */
kono
parents:
diff changeset
229 type = build_function_type_list (IMP_type,
kono
parents:
diff changeset
230 objc_object_type,
kono
parents:
diff changeset
231 objc_selector_type,
kono
parents:
diff changeset
232 NULL_TREE);
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 umsg_decl = add_builtin_function (TAG_MSGSEND,
kono
parents:
diff changeset
235 type, 0, NOT_BUILT_IN,
kono
parents:
diff changeset
236 NULL, NULL_TREE);
kono
parents:
diff changeset
237 TREE_NOTHROW (umsg_decl) = 0;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
kono
parents:
diff changeset
240 type = build_function_type_list (IMP_type,
kono
parents:
diff changeset
241 objc_super_type,
kono
parents:
diff changeset
242 objc_selector_type,
kono
parents:
diff changeset
243 NULL_TREE);
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
kono
parents:
diff changeset
246 type, 0, NOT_BUILT_IN,
kono
parents:
diff changeset
247 NULL, NULL_TREE);
kono
parents:
diff changeset
248 TREE_NOTHROW (umsg_super_decl) = 0;
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 /* The following GNU runtime entry point is called to initialize
kono
parents:
diff changeset
251 each module:
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 __objc_exec_class (void *); */
kono
parents:
diff changeset
254 type = build_function_type_list (void_type_node,
kono
parents:
diff changeset
255 ptr_type_node,
kono
parents:
diff changeset
256 NULL_TREE);
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 execclass_decl = add_builtin_function (TAG_EXECCLASS,
kono
parents:
diff changeset
259 type, 0, NOT_BUILT_IN,
kono
parents:
diff changeset
260 NULL, NULL_TREE);
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 type = build_function_type_list (objc_object_type,
kono
parents:
diff changeset
263 const_string_type_node,
kono
parents:
diff changeset
264 NULL_TREE);
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 /* id objc_getClass (const char *); */
kono
parents:
diff changeset
267 objc_get_class_decl
kono
parents:
diff changeset
268 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
kono
parents:
diff changeset
269 NULL, NULL_TREE);
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 /* id objc_getMetaClass (const char *); */
kono
parents:
diff changeset
272 objc_get_meta_class_decl = add_builtin_function (TAG_GETMETACLASS, type,
kono
parents:
diff changeset
273 0, NOT_BUILT_IN, NULL,
kono
parents:
diff changeset
274 NULL_TREE);
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 /* static SEL _OBJC_SELECTOR_TABLE[]; */
kono
parents:
diff changeset
277 build_selector_table_decl ();
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 /* Stuff for properties.
kono
parents:
diff changeset
280 The codegen relies on this being NULL for GNU. */
kono
parents:
diff changeset
281 objc_copyStruct_decl = NULL_TREE;
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 /* This is the type of all of the following functions
kono
parents:
diff changeset
284 bjc_getPropertyStruct() and objc_setPropertyStruct(). */
kono
parents:
diff changeset
285 type = build_function_type_list (void_type_node,
kono
parents:
diff changeset
286 ptr_type_node,
kono
parents:
diff changeset
287 const_ptr_type_node,
kono
parents:
diff changeset
288 ptrdiff_type_node,
kono
parents:
diff changeset
289 boolean_type_node,
kono
parents:
diff changeset
290 boolean_type_node,
kono
parents:
diff changeset
291 NULL_TREE);
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 /* Declare the following function:
kono
parents:
diff changeset
294 void
kono
parents:
diff changeset
295 objc_getPropertyStruct (void *destination, const void *source,
kono
parents:
diff changeset
296 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
kono
parents:
diff changeset
297 objc_getPropertyStruct_decl = add_builtin_function ("objc_getPropertyStruct",
kono
parents:
diff changeset
298 type, 0, NOT_BUILT_IN,
kono
parents:
diff changeset
299 NULL, NULL_TREE);
kono
parents:
diff changeset
300 TREE_NOTHROW (objc_getPropertyStruct_decl) = 0;
kono
parents:
diff changeset
301 /* Declare the following function:
kono
parents:
diff changeset
302 void
kono
parents:
diff changeset
303 objc_setPropertyStruct (void *destination, const void *source,
kono
parents:
diff changeset
304 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
kono
parents:
diff changeset
305 objc_setPropertyStruct_decl = add_builtin_function ("objc_setPropertyStruct",
kono
parents:
diff changeset
306 type, 0, NOT_BUILT_IN,
kono
parents:
diff changeset
307 NULL, NULL_TREE);
kono
parents:
diff changeset
308 TREE_NOTHROW (objc_setPropertyStruct_decl) = 0;
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 using_eh_for_cleanups ();
kono
parents:
diff changeset
311 lang_hooks.eh_runtime_type = objc_eh_runtime_type;
kono
parents:
diff changeset
312 lang_hooks.eh_personality = objc_eh_personality;
kono
parents:
diff changeset
313 }
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 /* --- templates --- */
kono
parents:
diff changeset
316 /* struct _objc_selector {
kono
parents:
diff changeset
317 SEL sel_id;
kono
parents:
diff changeset
318 char *sel_type;
kono
parents:
diff changeset
319 }; */
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 static void
kono
parents:
diff changeset
322 build_selector_template (void)
kono
parents:
diff changeset
323 {
kono
parents:
diff changeset
324 tree decls, *chain = NULL;
kono
parents:
diff changeset
325
kono
parents:
diff changeset
326 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 /* SEL sel_id; */
kono
parents:
diff changeset
329 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 /* char *sel_type; */
kono
parents:
diff changeset
332 add_field_decl (string_type_node, "sel_type", &chain);
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 objc_finish_struct (objc_selector_template, decls);
kono
parents:
diff changeset
335 }
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 /* struct _objc_class {
kono
parents:
diff changeset
338 struct _objc_class *isa;
kono
parents:
diff changeset
339 struct _objc_class *super_class;
kono
parents:
diff changeset
340 char *name;
kono
parents:
diff changeset
341 long version;
kono
parents:
diff changeset
342 long info;
kono
parents:
diff changeset
343 long instance_size;
kono
parents:
diff changeset
344 struct _objc_ivar_list *ivars;
kono
parents:
diff changeset
345 struct _objc_method_list *methods;
kono
parents:
diff changeset
346 struct sarray *dtable;
kono
parents:
diff changeset
347 struct _objc_class *subclass_list;
kono
parents:
diff changeset
348 struct _objc_class *sibling_class;
kono
parents:
diff changeset
349 struct _objc_protocol_list *protocols;
kono
parents:
diff changeset
350 void *gc_object_type;
kono
parents:
diff changeset
351 }; */
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 static void
kono
parents:
diff changeset
354 build_class_template (void)
kono
parents:
diff changeset
355 {
kono
parents:
diff changeset
356 tree ptype, decls, *chain = NULL;
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 /* struct _objc_class *isa; */
kono
parents:
diff changeset
361 decls = add_field_decl (build_pointer_type (objc_class_template),
kono
parents:
diff changeset
362 "isa", &chain);
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 /* struct _objc_class *super_class; */
kono
parents:
diff changeset
365 add_field_decl (build_pointer_type (objc_class_template),
kono
parents:
diff changeset
366 "super_class", &chain);
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 /* char *name; */
kono
parents:
diff changeset
369 add_field_decl (string_type_node, "name", &chain);
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 /* long version; */
kono
parents:
diff changeset
372 add_field_decl (long_integer_type_node, "version", &chain);
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 /* long info; */
kono
parents:
diff changeset
375 add_field_decl (long_integer_type_node, "info", &chain);
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 /* long instance_size; */
kono
parents:
diff changeset
378 add_field_decl (long_integer_type_node, "instance_size", &chain);
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 /* struct _objc_ivar_list *ivars; */
kono
parents:
diff changeset
381 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 /* struct _objc_method_list *methods; */
kono
parents:
diff changeset
384 add_field_decl (objc_method_list_ptr, "methods", &chain);
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 /* struct sarray *dtable; */
kono
parents:
diff changeset
387 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
kono
parents:
diff changeset
388 get_identifier ("sarray")));
kono
parents:
diff changeset
389 add_field_decl (ptype, "dtable", &chain);
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 /* struct objc_class *subclass_list; */
kono
parents:
diff changeset
392 ptype = build_pointer_type (objc_class_template);
kono
parents:
diff changeset
393 add_field_decl (ptype, "subclass_list", &chain);
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 /* struct objc_class *sibling_class; */
kono
parents:
diff changeset
396 ptype = build_pointer_type (objc_class_template);
kono
parents:
diff changeset
397 add_field_decl (ptype, "sibling_class", &chain);
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 /* struct _objc_protocol **protocol_list; */
kono
parents:
diff changeset
400 ptype = build_pointer_type (build_pointer_type
kono
parents:
diff changeset
401 (xref_tag (RECORD_TYPE,
kono
parents:
diff changeset
402 get_identifier (UTAG_PROTOCOL))));
kono
parents:
diff changeset
403 add_field_decl (ptype, "protocol_list", &chain);
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 /* void *gc_object_type; */
kono
parents:
diff changeset
406 add_field_decl (build_pointer_type (void_type_node),
kono
parents:
diff changeset
407 "gc_object_type", &chain);
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 objc_finish_struct (objc_class_template, decls);
kono
parents:
diff changeset
410 }
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 /* struct _objc_category {
kono
parents:
diff changeset
413 char *category_name;
kono
parents:
diff changeset
414 char *class_name;
kono
parents:
diff changeset
415 struct _objc_method_list *instance_methods;
kono
parents:
diff changeset
416 struct _objc_method_list *class_methods;
kono
parents:
diff changeset
417 struct _objc_protocol_list *protocols;
kono
parents:
diff changeset
418 }; */
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 static void
kono
parents:
diff changeset
421 build_category_template (void)
kono
parents:
diff changeset
422 {
kono
parents:
diff changeset
423 tree ptype, decls, *chain = NULL;
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 /* char *category_name; */
kono
parents:
diff changeset
428 decls = add_field_decl (string_type_node, "category_name", &chain);
kono
parents:
diff changeset
429
kono
parents:
diff changeset
430 /* char *class_name; */
kono
parents:
diff changeset
431 add_field_decl (string_type_node, "class_name", &chain);
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 /* struct _objc_method_list *instance_methods; */
kono
parents:
diff changeset
434 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 /* struct _objc_method_list *class_methods; */
kono
parents:
diff changeset
437 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 /* struct _objc_protocol **protocol_list; */
kono
parents:
diff changeset
440 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
kono
parents:
diff changeset
441 add_field_decl (ptype, "protocol_list", &chain);
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 objc_finish_struct (objc_category_template, decls);
kono
parents:
diff changeset
444 }
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 /* struct _objc_protocol {
kono
parents:
diff changeset
447 struct _objc_class *isa;
kono
parents:
diff changeset
448 char *protocol_name;
kono
parents:
diff changeset
449 struct _objc_protocol **protocol_list;
kono
parents:
diff changeset
450 struct _objc__method_prototype_list *instance_methods;
kono
parents:
diff changeset
451 struct _objc__method_prototype_list *class_methods;
kono
parents:
diff changeset
452 }; */
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454 static void
kono
parents:
diff changeset
455 build_protocol_template (void)
kono
parents:
diff changeset
456 {
kono
parents:
diff changeset
457 tree ptype, decls, *chain = NULL;
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 /* struct _objc_class *isa; */
kono
parents:
diff changeset
462 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
kono
parents:
diff changeset
463 get_identifier (UTAG_CLASS)));
kono
parents:
diff changeset
464 decls = add_field_decl (ptype, "isa", &chain);
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 /* char *protocol_name; */
kono
parents:
diff changeset
467 add_field_decl (string_type_node, "protocol_name", &chain);
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 /* struct _objc_protocol **protocol_list; */
kono
parents:
diff changeset
470 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
kono
parents:
diff changeset
471 add_field_decl (ptype, "protocol_list", &chain);
kono
parents:
diff changeset
472
kono
parents:
diff changeset
473 /* struct _objc__method_prototype_list *instance_methods; */
kono
parents:
diff changeset
474 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 /* struct _objc__method_prototype_list *class_methods; */
kono
parents:
diff changeset
477 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 objc_finish_struct (objc_protocol_template, decls);
kono
parents:
diff changeset
480 }
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 /* --- names, decls + identifiers --- */
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 static void
kono
parents:
diff changeset
485 build_selector_table_decl (void)
kono
parents:
diff changeset
486 {
kono
parents:
diff changeset
487 tree temp;
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 build_selector_template ();
kono
parents:
diff changeset
490 temp = build_array_type (objc_selector_template, NULL_TREE);
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
kono
parents:
diff changeset
493 /* Squash `defined but not used' warning check_global_declaration. */
kono
parents:
diff changeset
494 TREE_USED (UOBJC_SELECTOR_TABLE_decl) = 1;
kono
parents:
diff changeset
495 OBJCMETA (UOBJC_SELECTOR_TABLE_decl, objc_meta, meta_base);
kono
parents:
diff changeset
496 }
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498
kono
parents:
diff changeset
499 static tree
kono
parents:
diff changeset
500 gnu_runtime_abi_01_super_superclassfield_id (void)
kono
parents:
diff changeset
501 {
kono
parents:
diff changeset
502 if (!super_superclassfield_id)
kono
parents:
diff changeset
503 super_superclassfield_id = get_identifier ("super_class");
kono
parents:
diff changeset
504 return super_superclassfield_id;
kono
parents:
diff changeset
505 }
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 static tree
kono
parents:
diff changeset
509 gnu_runtime_abi_01_class_decl (tree klass)
kono
parents:
diff changeset
510 {
kono
parents:
diff changeset
511 tree decl;
kono
parents:
diff changeset
512 char buf[BUFSIZE];
kono
parents:
diff changeset
513 snprintf (buf, BUFSIZE, "_OBJC_Class_%s",
kono
parents:
diff changeset
514 IDENTIFIER_POINTER (CLASS_NAME (klass)));
kono
parents:
diff changeset
515 decl = start_var_decl (objc_class_template, buf);
kono
parents:
diff changeset
516 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
517 return decl;
kono
parents:
diff changeset
518 }
kono
parents:
diff changeset
519
kono
parents:
diff changeset
520 static tree
kono
parents:
diff changeset
521 gnu_runtime_abi_01_metaclass_decl (tree klass)
kono
parents:
diff changeset
522 {
kono
parents:
diff changeset
523 tree decl;
kono
parents:
diff changeset
524 char buf[BUFSIZE];
kono
parents:
diff changeset
525 snprintf (buf, BUFSIZE, "_OBJC_MetaClass_%s",
kono
parents:
diff changeset
526 IDENTIFIER_POINTER (CLASS_NAME (klass)));
kono
parents:
diff changeset
527 decl = start_var_decl (objc_class_template, buf);
kono
parents:
diff changeset
528 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
529 return decl;
kono
parents:
diff changeset
530 }
kono
parents:
diff changeset
531
kono
parents:
diff changeset
532 static tree
kono
parents:
diff changeset
533 gnu_runtime_abi_01_category_decl (tree klass)
kono
parents:
diff changeset
534 {
kono
parents:
diff changeset
535 tree decl;
kono
parents:
diff changeset
536 char buf[BUFSIZE];
kono
parents:
diff changeset
537 snprintf (buf, BUFSIZE, "_OBJC_Category_%s_on_%s",
kono
parents:
diff changeset
538 IDENTIFIER_POINTER (CLASS_SUPER_NAME (klass)),
kono
parents:
diff changeset
539 IDENTIFIER_POINTER (CLASS_NAME (klass)));
kono
parents:
diff changeset
540 decl = start_var_decl (objc_category_template, buf);
kono
parents:
diff changeset
541 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
542 return decl;
kono
parents:
diff changeset
543 }
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 static tree
kono
parents:
diff changeset
546 gnu_runtime_abi_01_protocol_decl (tree p)
kono
parents:
diff changeset
547 {
kono
parents:
diff changeset
548 tree decl;
kono
parents:
diff changeset
549 char buf[BUFSIZE];
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 /* static struct _objc_protocol _OBJC_Protocol_<mumble>; */
kono
parents:
diff changeset
552 snprintf (buf, BUFSIZE, "_OBJC_Protocol_%s",
kono
parents:
diff changeset
553 IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
kono
parents:
diff changeset
554 decl = start_var_decl (objc_protocol_template, buf);
kono
parents:
diff changeset
555 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
556 return decl;
kono
parents:
diff changeset
557 }
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 static tree
kono
parents:
diff changeset
560 gnu_runtime_abi_01_string_decl (tree type, const char *name,
kono
parents:
diff changeset
561 string_section where ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
562 {
kono
parents:
diff changeset
563 tree decl = start_var_decl (type, name);
kono
parents:
diff changeset
564 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
565 return decl;
kono
parents:
diff changeset
566 }
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 /* --- entry --- */
kono
parents:
diff changeset
569
kono
parents:
diff changeset
570 static tree
kono
parents:
diff changeset
571 gnu_runtime_abi_01_get_class_reference (tree ident)
kono
parents:
diff changeset
572 {
kono
parents:
diff changeset
573 tree params;
kono
parents:
diff changeset
574
kono
parents:
diff changeset
575 add_class_reference (ident);
kono
parents:
diff changeset
576
kono
parents:
diff changeset
577 params = build_tree_list (NULL_TREE, my_build_string_pointer
kono
parents:
diff changeset
578 (IDENTIFIER_LENGTH (ident) + 1,
kono
parents:
diff changeset
579 IDENTIFIER_POINTER (ident)));
kono
parents:
diff changeset
580
kono
parents:
diff changeset
581 return build_function_call (input_location, objc_get_class_decl, params);
kono
parents:
diff changeset
582 }
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 /* Used by build_function_type_for_method. Append the types for
kono
parents:
diff changeset
585 receiver & _cmd at the start of a method argument list to ARGTYPES.
kono
parents:
diff changeset
586 CONTEXT is either METHOD_DEF or METHOD_REF, saying whether we are
kono
parents:
diff changeset
587 trying to define a method or call one. SUPERFLAG says this is for a
kono
parents:
diff changeset
588 send to super. METH may be NULL, in the case that there is no
kono
parents:
diff changeset
589 prototype. */
kono
parents:
diff changeset
590
kono
parents:
diff changeset
591 static void
kono
parents:
diff changeset
592 gnu_runtime_abi_01_get_arg_type_list_base (vec<tree, va_gc> **argtypes,
kono
parents:
diff changeset
593 tree meth, int context,
kono
parents:
diff changeset
594 int superflag ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
595 {
kono
parents:
diff changeset
596 tree receiver_type;
kono
parents:
diff changeset
597
kono
parents:
diff changeset
598 if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
kono
parents:
diff changeset
599 receiver_type = objc_instance_type;
kono
parents:
diff changeset
600 else
kono
parents:
diff changeset
601 receiver_type = objc_object_type;
kono
parents:
diff changeset
602
kono
parents:
diff changeset
603 vec_safe_push (*argtypes, receiver_type);
kono
parents:
diff changeset
604 /* Selector type - will eventually change to `int'. */
kono
parents:
diff changeset
605 vec_safe_push (*argtypes, objc_selector_type);
kono
parents:
diff changeset
606 }
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 /* Unused for GNU runtime. */
kono
parents:
diff changeset
609 static tree
kono
parents:
diff changeset
610 gnu_runtime_abi_01_receiver_is_class_object (tree a ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
611 {
kono
parents:
diff changeset
612 return NULL_TREE;
kono
parents:
diff changeset
613 }
kono
parents:
diff changeset
614
kono
parents:
diff changeset
615 /* sel_ref_chain is a list whose "value" fields will be instances of
kono
parents:
diff changeset
616 identifier_node that represent the selector. LOC is the location of
kono
parents:
diff changeset
617 the @selector. */
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 static tree
kono
parents:
diff changeset
620 gnu_runtime_abi_01_build_typed_selector_reference (location_t loc, tree ident,
kono
parents:
diff changeset
621 tree prototype)
kono
parents:
diff changeset
622 {
kono
parents:
diff changeset
623 tree *chain = &sel_ref_chain;
kono
parents:
diff changeset
624 tree expr;
kono
parents:
diff changeset
625 int index = 0;
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 while (*chain)
kono
parents:
diff changeset
628 {
kono
parents:
diff changeset
629 /* When we do a lookup for @selector () we have no idea of the
kono
parents:
diff changeset
630 prototype - so match the first we find. */
kono
parents:
diff changeset
631 if (TREE_VALUE (*chain) == ident
kono
parents:
diff changeset
632 && (!prototype || TREE_PURPOSE (*chain) == prototype))
kono
parents:
diff changeset
633 goto return_at_index;
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 index++;
kono
parents:
diff changeset
636 chain = &TREE_CHAIN (*chain);
kono
parents:
diff changeset
637 }
kono
parents:
diff changeset
638
kono
parents:
diff changeset
639 *chain = tree_cons (prototype, ident, NULL_TREE);
kono
parents:
diff changeset
640
kono
parents:
diff changeset
641 /* TODO: Use a vec and keep this in it to (a) avoid re-creating and
kono
parents:
diff changeset
642 (b) provide better diagnostics for the first time an undefined
kono
parents:
diff changeset
643 selector is used. */
kono
parents:
diff changeset
644 return_at_index:
kono
parents:
diff changeset
645 expr = build_unary_op (loc, ADDR_EXPR,
kono
parents:
diff changeset
646 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
kono
parents:
diff changeset
647 build_int_cst (NULL_TREE, index)),
kono
parents:
diff changeset
648 1);
kono
parents:
diff changeset
649 return convert (objc_selector_type, expr);
kono
parents:
diff changeset
650 }
kono
parents:
diff changeset
651
kono
parents:
diff changeset
652 /* Build a tree expression to send OBJECT the operation SELECTOR,
kono
parents:
diff changeset
653 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
kono
parents:
diff changeset
654 assuming the method has prototype METHOD_PROTOTYPE.
kono
parents:
diff changeset
655 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
kono
parents:
diff changeset
656 LOC is the location of the expression to build.
kono
parents:
diff changeset
657 Use METHOD_PARAMS as list of args to pass to the method.
kono
parents:
diff changeset
658 If SUPER_FLAG is nonzero, we look up the superclass's method. */
kono
parents:
diff changeset
659
kono
parents:
diff changeset
660 static tree
kono
parents:
diff changeset
661 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
kono
parents:
diff changeset
662 tree lookup_object, tree selector,
kono
parents:
diff changeset
663 tree method_params)
kono
parents:
diff changeset
664 {
kono
parents:
diff changeset
665 tree sender = (super_flag ? umsg_super_decl
kono
parents:
diff changeset
666 : (flag_objc_direct_dispatch ? umsg_fast_decl
kono
parents:
diff changeset
667 : umsg_decl));
kono
parents:
diff changeset
668 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
kono
parents:
diff changeset
669 vec<tree, va_gc> *parms;
kono
parents:
diff changeset
670 vec<tree, va_gc> *tv;
kono
parents:
diff changeset
671 unsigned nparm = (method_params ? list_length (method_params) : 0);
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 /* If a prototype for the method to be called exists, then cast
kono
parents:
diff changeset
674 the sender's return type and arguments to match that of the method.
kono
parents:
diff changeset
675 Otherwise, leave sender as is. */
kono
parents:
diff changeset
676 tree ret_type
kono
parents:
diff changeset
677 = (method_prototype
kono
parents:
diff changeset
678 ? TREE_VALUE (TREE_TYPE (method_prototype))
kono
parents:
diff changeset
679 : objc_object_type);
kono
parents:
diff changeset
680 tree ftype
kono
parents:
diff changeset
681 = build_function_type_for_method (ret_type, method_prototype,
kono
parents:
diff changeset
682 METHOD_REF, super_flag);
kono
parents:
diff changeset
683 tree sender_cast;
kono
parents:
diff changeset
684 tree method, t;
kono
parents:
diff changeset
685
kono
parents:
diff changeset
686 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
kono
parents:
diff changeset
687 ftype = build_type_attribute_variant (ftype,
kono
parents:
diff changeset
688 METHOD_TYPE_ATTRIBUTES
kono
parents:
diff changeset
689 (method_prototype));
kono
parents:
diff changeset
690
kono
parents:
diff changeset
691 sender_cast = build_pointer_type (ftype);
kono
parents:
diff changeset
692
kono
parents:
diff changeset
693 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
kono
parents:
diff changeset
694
kono
parents:
diff changeset
695 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
kono
parents:
diff changeset
696 lookup_object = save_expr (lookup_object);
kono
parents:
diff changeset
697
kono
parents:
diff changeset
698 /* Param list + 2 slots for object and selector. */
kono
parents:
diff changeset
699 vec_alloc (parms, nparm + 2);
kono
parents:
diff changeset
700 vec_alloc (tv, 2);
kono
parents:
diff changeset
701
kono
parents:
diff changeset
702 /* First, call the lookup function to get a pointer to the method,
kono
parents:
diff changeset
703 then cast the pointer, then call it with the method arguments. */
kono
parents:
diff changeset
704 tv->quick_push (lookup_object);
kono
parents:
diff changeset
705 tv->quick_push (selector);
kono
parents:
diff changeset
706 method = build_function_call_vec (loc, vNULL, sender, tv, NULL);
kono
parents:
diff changeset
707 vec_free (tv);
kono
parents:
diff changeset
708
kono
parents:
diff changeset
709 /* Pass the appropriate object to the method. */
kono
parents:
diff changeset
710 parms->quick_push ((super_flag ? self_decl : lookup_object));
kono
parents:
diff changeset
711
kono
parents:
diff changeset
712 /* Pass the selector to the method. */
kono
parents:
diff changeset
713 parms->quick_push (selector);
kono
parents:
diff changeset
714 /* Now append the remainder of the parms. */
kono
parents:
diff changeset
715 if (nparm)
kono
parents:
diff changeset
716 for (; method_params; method_params = TREE_CHAIN (method_params))
kono
parents:
diff changeset
717 parms->quick_push (TREE_VALUE (method_params));
kono
parents:
diff changeset
718
kono
parents:
diff changeset
719 /* Build an obj_type_ref, with the correct cast for the method call. */
kono
parents:
diff changeset
720 t = build3 (OBJ_TYPE_REF, sender_cast, method, lookup_object, size_zero_node);
kono
parents:
diff changeset
721 t = build_function_call_vec (loc, vNULL, t, parms, NULL);
kono
parents:
diff changeset
722 vec_free (parms);
kono
parents:
diff changeset
723 return t;
kono
parents:
diff changeset
724 }
kono
parents:
diff changeset
725
kono
parents:
diff changeset
726 static tree
kono
parents:
diff changeset
727 gnu_runtime_abi_01_build_objc_method_call (location_t loc,
kono
parents:
diff changeset
728 tree method_prototype,
kono
parents:
diff changeset
729 tree receiver,
kono
parents:
diff changeset
730 tree rtype ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
731 tree sel_name,
kono
parents:
diff changeset
732 tree method_params,
kono
parents:
diff changeset
733 int super ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
734 {
kono
parents:
diff changeset
735 tree selector =
kono
parents:
diff changeset
736 gnu_runtime_abi_01_build_typed_selector_reference (loc,
kono
parents:
diff changeset
737 sel_name,
kono
parents:
diff changeset
738 method_prototype);
kono
parents:
diff changeset
739
kono
parents:
diff changeset
740 return build_objc_method_call (loc, super, method_prototype, receiver,
kono
parents:
diff changeset
741 selector, method_params);
kono
parents:
diff changeset
742 }
kono
parents:
diff changeset
743
kono
parents:
diff changeset
744 static tree
kono
parents:
diff changeset
745 gnu_runtime_abi_01_get_protocol_reference (location_t loc, tree p)
kono
parents:
diff changeset
746 {
kono
parents:
diff changeset
747 tree expr, protocol_struct_type, *chain;
kono
parents:
diff changeset
748 if (!PROTOCOL_FORWARD_DECL (p))
kono
parents:
diff changeset
749 PROTOCOL_FORWARD_DECL (p) = gnu_runtime_abi_01_protocol_decl (p);
kono
parents:
diff changeset
750
kono
parents:
diff changeset
751 expr = build_unary_op (loc, ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
kono
parents:
diff changeset
754 if we have it, rather than converting it here. */
kono
parents:
diff changeset
755 expr = convert (objc_protocol_type, expr);
kono
parents:
diff changeset
756
kono
parents:
diff changeset
757 /* The @protocol() expression is being compiled into a pointer to a
kono
parents:
diff changeset
758 statically allocated instance of the Protocol class. To become
kono
parents:
diff changeset
759 usable at runtime, the 'isa' pointer of the instance need to be
kono
parents:
diff changeset
760 fixed up at runtime by the runtime library, to point to the
kono
parents:
diff changeset
761 actual 'Protocol' class. */
kono
parents:
diff changeset
762
kono
parents:
diff changeset
763 /* For the GNU runtime, put the static Protocol instance in the list
kono
parents:
diff changeset
764 of statically allocated instances, so that we make sure that its
kono
parents:
diff changeset
765 'isa' pointer is fixed up at runtime by the GNU runtime library
kono
parents:
diff changeset
766 to point to the Protocol class (at runtime, when loading the
kono
parents:
diff changeset
767 module, the GNU runtime library loops on the statically allocated
kono
parents:
diff changeset
768 instances (as found in the defs field in objc_symtab) and fixups
kono
parents:
diff changeset
769 all the 'isa' pointers of those objects). */
kono
parents:
diff changeset
770
kono
parents:
diff changeset
771 /* This type is a struct containing the fields of a Protocol
kono
parents:
diff changeset
772 object. (Cfr. objc_protocol_type instead is the type of a pointer
kono
parents:
diff changeset
773 to such a struct). */
kono
parents:
diff changeset
774 protocol_struct_type = xref_tag (RECORD_TYPE,
kono
parents:
diff changeset
775 get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
kono
parents:
diff changeset
776
kono
parents:
diff changeset
777 /* Look for the list of Protocol statically allocated instances
kono
parents:
diff changeset
778 to fixup at runtime. Create a new list to hold Protocol
kono
parents:
diff changeset
779 statically allocated instances, if the list is not found. At
kono
parents:
diff changeset
780 present there is only another list, holding NSConstantString
kono
parents:
diff changeset
781 static instances to be fixed up at runtime. */
kono
parents:
diff changeset
782
kono
parents:
diff changeset
783 for (chain = &objc_static_instances;
kono
parents:
diff changeset
784 *chain && TREE_VALUE (*chain) != protocol_struct_type;
kono
parents:
diff changeset
785 chain = &TREE_CHAIN (*chain));
kono
parents:
diff changeset
786
kono
parents:
diff changeset
787 if (!*chain)
kono
parents:
diff changeset
788 {
kono
parents:
diff changeset
789 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
kono
parents:
diff changeset
790 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
kono
parents:
diff changeset
791 class_names);
kono
parents:
diff changeset
792 }
kono
parents:
diff changeset
793
kono
parents:
diff changeset
794 /* Add this statically allocated instance to the Protocol list. */
kono
parents:
diff changeset
795 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
kono
parents:
diff changeset
796 PROTOCOL_FORWARD_DECL (p),
kono
parents:
diff changeset
797 TREE_PURPOSE (*chain));
kono
parents:
diff changeset
798 return expr;
kono
parents:
diff changeset
799 }
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 /* For ABI 8 an IVAR is just a fixed offset in the class struct. */
kono
parents:
diff changeset
802
kono
parents:
diff changeset
803 static tree
kono
parents:
diff changeset
804 gnu_runtime_abi_01_build_ivar_ref (location_t loc ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
805 tree base, tree id)
kono
parents:
diff changeset
806 {
kono
parents:
diff changeset
807 return objc_build_component_ref (base, id);
kono
parents:
diff changeset
808 }
kono
parents:
diff changeset
809
kono
parents:
diff changeset
810 /* We build super class references as we need them (but keep them once
kono
parents:
diff changeset
811 built for the sake of efficiency). */
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 static tree
kono
parents:
diff changeset
814 gnu_runtime_abi_01_get_class_super_ref (location_t loc ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
815 struct imp_entry *imp, bool inst_meth)
kono
parents:
diff changeset
816 {
kono
parents:
diff changeset
817 if (inst_meth)
kono
parents:
diff changeset
818 {
kono
parents:
diff changeset
819 if (!ucls_super_ref)
kono
parents:
diff changeset
820 ucls_super_ref =
kono
parents:
diff changeset
821 objc_build_component_ref (imp->class_decl,
kono
parents:
diff changeset
822 get_identifier ("super_class"));
kono
parents:
diff changeset
823 return ucls_super_ref;
kono
parents:
diff changeset
824 }
kono
parents:
diff changeset
825 else
kono
parents:
diff changeset
826 {
kono
parents:
diff changeset
827 if (!uucls_super_ref)
kono
parents:
diff changeset
828 uucls_super_ref =
kono
parents:
diff changeset
829 objc_build_component_ref (imp->meta_decl,
kono
parents:
diff changeset
830 get_identifier ("super_class"));
kono
parents:
diff changeset
831 return uucls_super_ref;
kono
parents:
diff changeset
832 }
kono
parents:
diff changeset
833 }
kono
parents:
diff changeset
834
kono
parents:
diff changeset
835 static tree
kono
parents:
diff changeset
836 gnu_runtime_abi_01_get_category_super_ref (location_t loc ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
837 struct imp_entry *imp, bool inst_meth)
kono
parents:
diff changeset
838 {
kono
parents:
diff changeset
839 tree super_name = CLASS_SUPER_NAME (imp->imp_template);
kono
parents:
diff changeset
840 tree super_class;
kono
parents:
diff changeset
841
kono
parents:
diff changeset
842 add_class_reference (super_name);
kono
parents:
diff changeset
843 super_class = (inst_meth ? objc_get_class_decl : objc_get_meta_class_decl);
kono
parents:
diff changeset
844 super_name = my_build_string_pointer (IDENTIFIER_LENGTH (super_name) + 1,
kono
parents:
diff changeset
845 IDENTIFIER_POINTER (super_name));
kono
parents:
diff changeset
846 /* super_class = get_{meta_}class("CLASS_SUPER_NAME"); */
kono
parents:
diff changeset
847 return build_function_call (input_location,
kono
parents:
diff changeset
848 super_class,
kono
parents:
diff changeset
849 build_tree_list (NULL_TREE, super_name));
kono
parents:
diff changeset
850 }
kono
parents:
diff changeset
851
kono
parents:
diff changeset
852 static bool
kono
parents:
diff changeset
853 gnu_runtime_abi_01_setup_const_string_class_decl (void)
kono
parents:
diff changeset
854 {
kono
parents:
diff changeset
855 /* Do nothing, and create no error. */
kono
parents:
diff changeset
856 return true;
kono
parents:
diff changeset
857 }
kono
parents:
diff changeset
858
kono
parents:
diff changeset
859 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
kono
parents:
diff changeset
860
kono
parents:
diff changeset
861 static GTY(()) int num_static_inst;
kono
parents:
diff changeset
862
kono
parents:
diff changeset
863 static tree
kono
parents:
diff changeset
864 objc_add_static_instance (tree constructor, tree class_decl)
kono
parents:
diff changeset
865 {
kono
parents:
diff changeset
866 tree *chain, decl;
kono
parents:
diff changeset
867 char buf[BUFSIZE];
kono
parents:
diff changeset
868
kono
parents:
diff changeset
869 /* Find the list of static instances for the CLASS_DECL. Create one if
kono
parents:
diff changeset
870 not found. */
kono
parents:
diff changeset
871 for (chain = &objc_static_instances;
kono
parents:
diff changeset
872 *chain && TREE_VALUE (*chain) != class_decl;
kono
parents:
diff changeset
873 chain = &TREE_CHAIN (*chain));
kono
parents:
diff changeset
874 if (!*chain)
kono
parents:
diff changeset
875 {
kono
parents:
diff changeset
876 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
kono
parents:
diff changeset
877 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
kono
parents:
diff changeset
878 }
kono
parents:
diff changeset
879
kono
parents:
diff changeset
880 snprintf (buf, BUFSIZE, "_OBJC_INSTANCE_%d", num_static_inst++);
kono
parents:
diff changeset
881 decl = build_decl (input_location,
kono
parents:
diff changeset
882 VAR_DECL, get_identifier (buf), class_decl);
kono
parents:
diff changeset
883 TREE_STATIC (decl) = 1;
kono
parents:
diff changeset
884 DECL_ARTIFICIAL (decl) = 1;
kono
parents:
diff changeset
885 TREE_USED (decl) = 1;
kono
parents:
diff changeset
886 DECL_INITIAL (decl) = constructor;
kono
parents:
diff changeset
887 DECL_CONTEXT (decl) = NULL;
kono
parents:
diff changeset
888 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
889
kono
parents:
diff changeset
890 /* We may be writing something else just now.
kono
parents:
diff changeset
891 Postpone till end of input. */
kono
parents:
diff changeset
892 DECL_DEFER_OUTPUT (decl) = 1;
kono
parents:
diff changeset
893 lang_hooks.decls.pushdecl (decl);
kono
parents:
diff changeset
894 rest_of_decl_compilation (decl, 1, 0);
kono
parents:
diff changeset
895
kono
parents:
diff changeset
896 /* Add the DECL to the head of this CLASS' list. */
kono
parents:
diff changeset
897 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
kono
parents:
diff changeset
898
kono
parents:
diff changeset
899 return decl;
kono
parents:
diff changeset
900 }
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 static tree
kono
parents:
diff changeset
903 gnu_runtime_abi_01_build_const_string_constructor (location_t loc, tree string,
kono
parents:
diff changeset
904 int length)
kono
parents:
diff changeset
905 {
kono
parents:
diff changeset
906 tree constructor, fields;
kono
parents:
diff changeset
907 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
908
kono
parents:
diff changeset
909 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
kono
parents:
diff changeset
910 fields = TYPE_FIELDS (internal_const_str_type);
kono
parents:
diff changeset
911 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
912
kono
parents:
diff changeset
913 fields = DECL_CHAIN (fields);
kono
parents:
diff changeset
914 CONSTRUCTOR_APPEND_ELT (v, fields, build_unary_op (loc,
kono
parents:
diff changeset
915 ADDR_EXPR, string, 1));
kono
parents:
diff changeset
916
kono
parents:
diff changeset
917 fields = DECL_CHAIN (fields);
kono
parents:
diff changeset
918 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
kono
parents:
diff changeset
919 constructor = objc_build_constructor (internal_const_str_type, v);
kono
parents:
diff changeset
920
kono
parents:
diff changeset
921 constructor = objc_add_static_instance (constructor, constant_string_type);
kono
parents:
diff changeset
922 return constructor;
kono
parents:
diff changeset
923 }
kono
parents:
diff changeset
924
kono
parents:
diff changeset
925 /* --- metadata - module initializer --- */
kono
parents:
diff changeset
926
kono
parents:
diff changeset
927 /* The GNU runtime requires us to provide a static initializer function
kono
parents:
diff changeset
928 for each module:
kono
parents:
diff changeset
929
kono
parents:
diff changeset
930 static void __objc_gnu_init (void) {
kono
parents:
diff changeset
931 __objc_exec_class (&L_OBJC_MODULES);
kono
parents:
diff changeset
932 } */
kono
parents:
diff changeset
933
kono
parents:
diff changeset
934
kono
parents:
diff changeset
935 static void
kono
parents:
diff changeset
936 build_module_initializer_routine (void)
kono
parents:
diff changeset
937 {
kono
parents:
diff changeset
938 tree body;
kono
parents:
diff changeset
939
kono
parents:
diff changeset
940 #ifdef OBJCPLUS
kono
parents:
diff changeset
941 push_lang_context (lang_name_c); /* extern "C" */
kono
parents:
diff changeset
942 #endif
kono
parents:
diff changeset
943
kono
parents:
diff changeset
944 objc_push_parm (build_decl (input_location,
kono
parents:
diff changeset
945 PARM_DECL, NULL_TREE, void_type_node));
kono
parents:
diff changeset
946 #ifdef OBJCPLUS
kono
parents:
diff changeset
947 objc_start_function (get_identifier (TAG_GNUINIT),
kono
parents:
diff changeset
948 build_function_type_list (void_type_node, NULL_TREE),
kono
parents:
diff changeset
949 NULL_TREE, NULL_TREE);
kono
parents:
diff changeset
950 #else
kono
parents:
diff changeset
951 objc_start_function (get_identifier (TAG_GNUINIT),
kono
parents:
diff changeset
952 build_function_type_list (void_type_node, NULL_TREE),
kono
parents:
diff changeset
953 NULL_TREE, objc_get_parm_info (0, NULL_TREE));
kono
parents:
diff changeset
954 #endif
kono
parents:
diff changeset
955 body = c_begin_compound_stmt (true);
kono
parents:
diff changeset
956 add_stmt (build_function_call
kono
parents:
diff changeset
957 (input_location,
kono
parents:
diff changeset
958 execclass_decl,
kono
parents:
diff changeset
959 build_tree_list
kono
parents:
diff changeset
960 (NULL_TREE,
kono
parents:
diff changeset
961 build_unary_op (input_location, ADDR_EXPR,
kono
parents:
diff changeset
962 UOBJC_MODULES_decl, 0))));
kono
parents:
diff changeset
963 add_stmt (c_end_compound_stmt (input_location, body, true));
kono
parents:
diff changeset
964
kono
parents:
diff changeset
965 TREE_PUBLIC (current_function_decl) = 0;
kono
parents:
diff changeset
966
kono
parents:
diff changeset
967 #ifndef OBJCPLUS
kono
parents:
diff changeset
968 /* For Objective-C++, we will need to call __objc_gnu_init
kono
parents:
diff changeset
969 from objc_generate_static_init_call() below. */
kono
parents:
diff changeset
970 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
kono
parents:
diff changeset
971 #endif
kono
parents:
diff changeset
972
kono
parents:
diff changeset
973 GNU_INIT_decl = current_function_decl;
kono
parents:
diff changeset
974 finish_function ();
kono
parents:
diff changeset
975
kono
parents:
diff changeset
976 #ifdef OBJCPLUS
kono
parents:
diff changeset
977 pop_lang_context ();
kono
parents:
diff changeset
978 #endif
kono
parents:
diff changeset
979 }
kono
parents:
diff changeset
980
kono
parents:
diff changeset
981 #ifdef OBJCPLUS
kono
parents:
diff changeset
982 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
kono
parents:
diff changeset
983 to be called by the module initializer routine. */
kono
parents:
diff changeset
984
kono
parents:
diff changeset
985 int
kono
parents:
diff changeset
986 objc_static_init_needed_p (void)
kono
parents:
diff changeset
987 {
kono
parents:
diff changeset
988 return (GNU_INIT_decl != NULL_TREE);
kono
parents:
diff changeset
989 }
kono
parents:
diff changeset
990
kono
parents:
diff changeset
991 /* Generate a call to the __objc_gnu_init initializer function. */
kono
parents:
diff changeset
992
kono
parents:
diff changeset
993 tree
kono
parents:
diff changeset
994 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
995 {
kono
parents:
diff changeset
996 add_stmt (build_stmt (input_location, EXPR_STMT,
kono
parents:
diff changeset
997 build_function_call (input_location,
kono
parents:
diff changeset
998 GNU_INIT_decl, NULL_TREE)));
kono
parents:
diff changeset
999
kono
parents:
diff changeset
1000 return ctors;
kono
parents:
diff changeset
1001 }
kono
parents:
diff changeset
1002 #endif /* OBJCPLUS */
kono
parents:
diff changeset
1003
kono
parents:
diff changeset
1004 /* --- Output GNU Meta-data --- */
kono
parents:
diff changeset
1005
kono
parents:
diff changeset
1006 static void
kono
parents:
diff changeset
1007 generate_classref_translation_entry (tree chain)
kono
parents:
diff changeset
1008 {
kono
parents:
diff changeset
1009 tree expr, decl, type;
kono
parents:
diff changeset
1010
kono
parents:
diff changeset
1011 decl = TREE_PURPOSE (chain);
kono
parents:
diff changeset
1012 type = TREE_TYPE (decl);
kono
parents:
diff changeset
1013
kono
parents:
diff changeset
1014 expr = add_objc_string (TREE_VALUE (chain), class_names);
kono
parents:
diff changeset
1015 expr = convert (type, expr); /* cast! */
kono
parents:
diff changeset
1016
kono
parents:
diff changeset
1017 /* This is a class reference. It is re-written by the runtime,
kono
parents:
diff changeset
1018 but will be optimized away unless we force it. */
kono
parents:
diff changeset
1019 DECL_PRESERVE_P (decl) = 1;
kono
parents:
diff changeset
1020 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
1021 finish_var_decl (decl, expr);
kono
parents:
diff changeset
1022 return;
kono
parents:
diff changeset
1023 }
kono
parents:
diff changeset
1024
kono
parents:
diff changeset
1025
kono
parents:
diff changeset
1026 static void
kono
parents:
diff changeset
1027 handle_impent (struct imp_entry *impent)
kono
parents:
diff changeset
1028 {
kono
parents:
diff changeset
1029 char *string;
kono
parents:
diff changeset
1030
kono
parents:
diff changeset
1031 /* objc_implementation_context = impent->imp_context;
kono
parents:
diff changeset
1032 implementation_template = impent->imp_template;*/
kono
parents:
diff changeset
1033
kono
parents:
diff changeset
1034 switch (TREE_CODE (impent->imp_context))
kono
parents:
diff changeset
1035 {
kono
parents:
diff changeset
1036 case CLASS_IMPLEMENTATION_TYPE:
kono
parents:
diff changeset
1037 {
kono
parents:
diff changeset
1038 const char *const class_name =
kono
parents:
diff changeset
1039 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
kono
parents:
diff changeset
1040
kono
parents:
diff changeset
1041 string = (char *) alloca (strlen (class_name) + 30);
kono
parents:
diff changeset
1042
kono
parents:
diff changeset
1043 sprintf (string, "__objc_class_name_%s", class_name);
kono
parents:
diff changeset
1044 break;
kono
parents:
diff changeset
1045 }
kono
parents:
diff changeset
1046 case CATEGORY_IMPLEMENTATION_TYPE:
kono
parents:
diff changeset
1047 {
kono
parents:
diff changeset
1048 const char *const class_name =
kono
parents:
diff changeset
1049 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
kono
parents:
diff changeset
1050 const char *const class_super_name =
kono
parents:
diff changeset
1051 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
kono
parents:
diff changeset
1052
kono
parents:
diff changeset
1053 string = (char *) alloca (strlen (class_name)
kono
parents:
diff changeset
1054 + strlen (class_super_name) + 30);
kono
parents:
diff changeset
1055
kono
parents:
diff changeset
1056 /* Do the same for categories. Even though no references to
kono
parents:
diff changeset
1057 these symbols are generated automatically by the compiler,
kono
parents:
diff changeset
1058 it gives you a handle to pull them into an archive by
kono
parents:
diff changeset
1059 hand. */
kono
parents:
diff changeset
1060 sprintf (string, "*__objc_category_name_%s_%s", class_name, class_super_name);
kono
parents:
diff changeset
1061 break;
kono
parents:
diff changeset
1062 }
kono
parents:
diff changeset
1063 default:
kono
parents:
diff changeset
1064 return;
kono
parents:
diff changeset
1065 }
kono
parents:
diff changeset
1066
kono
parents:
diff changeset
1067 {
kono
parents:
diff changeset
1068 tree decl, init;
kono
parents:
diff changeset
1069
kono
parents:
diff changeset
1070 init = integer_zero_node;
kono
parents:
diff changeset
1071 decl = build_decl (input_location,
kono
parents:
diff changeset
1072 VAR_DECL, get_identifier (string), TREE_TYPE (init));
kono
parents:
diff changeset
1073 TREE_PUBLIC (decl) = 1;
kono
parents:
diff changeset
1074 TREE_READONLY (decl) = 1;
kono
parents:
diff changeset
1075 TREE_USED (decl) = 1;
kono
parents:
diff changeset
1076 TREE_CONSTANT (decl) = 1;
kono
parents:
diff changeset
1077 DECL_CONTEXT (decl) = NULL_TREE;
kono
parents:
diff changeset
1078 DECL_ARTIFICIAL (decl) = 1;
kono
parents:
diff changeset
1079 TREE_STATIC (decl) = 1;
kono
parents:
diff changeset
1080 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
kono
parents:
diff changeset
1081 /* We must force the reference. */
kono
parents:
diff changeset
1082 DECL_PRESERVE_P (decl) = 1;
kono
parents:
diff changeset
1083
kono
parents:
diff changeset
1084 finish_var_decl(decl, init) ;
kono
parents:
diff changeset
1085 }
kono
parents:
diff changeset
1086 }
kono
parents:
diff changeset
1087
kono
parents:
diff changeset
1088 tree
kono
parents:
diff changeset
1089 build_protocol_initializer (tree type, tree protocol_name, tree protocol_list,
kono
parents:
diff changeset
1090 tree inst_methods, tree class_methods)
kono
parents:
diff changeset
1091 {
kono
parents:
diff changeset
1092 tree expr, ttyp;
kono
parents:
diff changeset
1093 location_t loc;
kono
parents:
diff changeset
1094 vec<constructor_elt, va_gc> *inits = NULL;
kono
parents:
diff changeset
1095
kono
parents:
diff changeset
1096 /* TODO: pass the loc in or find it from args. */
kono
parents:
diff changeset
1097 loc = input_location;
kono
parents:
diff changeset
1098 ttyp = build_pointer_type (xref_tag (RECORD_TYPE,
kono
parents:
diff changeset
1099 get_identifier (UTAG_CLASS)));
kono
parents:
diff changeset
1100 /* Filling the "isa" in with a version allows the runtime system to
kono
parents:
diff changeset
1101 detect this ... */
kono
parents:
diff changeset
1102 expr = build_int_cst (ttyp, PROTOCOL_VERSION);
kono
parents:
diff changeset
1103
kono
parents:
diff changeset
1104 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
kono
parents:
diff changeset
1105
kono
parents:
diff changeset
1106 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
kono
parents:
diff changeset
1107 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
kono
parents:
diff changeset
1108
kono
parents:
diff changeset
1109 ttyp = objc_method_proto_list_ptr;
kono
parents:
diff changeset
1110 if (inst_methods)
kono
parents:
diff changeset
1111 expr = convert (ttyp, build_unary_op (loc, ADDR_EXPR, inst_methods, 0));
kono
parents:
diff changeset
1112 else
kono
parents:
diff changeset
1113 expr = convert (ttyp, null_pointer_node);
kono
parents:
diff changeset
1114 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
kono
parents:
diff changeset
1115
kono
parents:
diff changeset
1116 if (class_methods)
kono
parents:
diff changeset
1117 expr = convert (ttyp, build_unary_op (loc, ADDR_EXPR, class_methods, 0));
kono
parents:
diff changeset
1118 else
kono
parents:
diff changeset
1119 expr = convert (ttyp, null_pointer_node);
kono
parents:
diff changeset
1120 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
kono
parents:
diff changeset
1121
kono
parents:
diff changeset
1122 return objc_build_constructor (type, inits);
kono
parents:
diff changeset
1123 }
kono
parents:
diff changeset
1124
kono
parents:
diff changeset
1125 static tree
kono
parents:
diff changeset
1126 generate_protocol_list (tree i_or_p, tree klass_ctxt)
kono
parents:
diff changeset
1127 {
kono
parents:
diff changeset
1128 tree array_type, ptype, refs_decl, lproto, e, plist;
kono
parents:
diff changeset
1129 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1130 char buf[BUFSIZE];
kono
parents:
diff changeset
1131 int size = 0;
kono
parents:
diff changeset
1132
kono
parents:
diff changeset
1133 switch (TREE_CODE (i_or_p))
kono
parents:
diff changeset
1134 {
kono
parents:
diff changeset
1135 case CLASS_INTERFACE_TYPE:
kono
parents:
diff changeset
1136 case CATEGORY_INTERFACE_TYPE:
kono
parents:
diff changeset
1137 plist = CLASS_PROTOCOL_LIST (i_or_p);
kono
parents:
diff changeset
1138 break;
kono
parents:
diff changeset
1139 case PROTOCOL_INTERFACE_TYPE:
kono
parents:
diff changeset
1140 plist = PROTOCOL_LIST (i_or_p);
kono
parents:
diff changeset
1141 break;
kono
parents:
diff changeset
1142 default:
kono
parents:
diff changeset
1143 gcc_unreachable ();
kono
parents:
diff changeset
1144 }
kono
parents:
diff changeset
1145
kono
parents:
diff changeset
1146 /* Compute size. */
kono
parents:
diff changeset
1147 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
kono
parents:
diff changeset
1148 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
kono
parents:
diff changeset
1149 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
kono
parents:
diff changeset
1150 size++;
kono
parents:
diff changeset
1151
kono
parents:
diff changeset
1152 /* Build initializer. */
kono
parents:
diff changeset
1153 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1154 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
kono
parents:
diff changeset
1155 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
kono
parents:
diff changeset
1156
kono
parents:
diff changeset
1157 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
kono
parents:
diff changeset
1158 {
kono
parents:
diff changeset
1159 tree pval = TREE_VALUE (lproto);
kono
parents:
diff changeset
1160
kono
parents:
diff changeset
1161 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
kono
parents:
diff changeset
1162 && PROTOCOL_FORWARD_DECL (pval))
kono
parents:
diff changeset
1163 {
kono
parents:
diff changeset
1164 tree fwref = PROTOCOL_FORWARD_DECL (pval);
kono
parents:
diff changeset
1165 location_t loc = DECL_SOURCE_LOCATION (fwref) ;
kono
parents:
diff changeset
1166 e = build_unary_op (loc, ADDR_EXPR, fwref, 0);
kono
parents:
diff changeset
1167 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
kono
parents:
diff changeset
1168 }
kono
parents:
diff changeset
1169 }
kono
parents:
diff changeset
1170
kono
parents:
diff changeset
1171 /* static struct objc_protocol *refs[n]; */
kono
parents:
diff changeset
1172
kono
parents:
diff changeset
1173 switch (TREE_CODE (i_or_p))
kono
parents:
diff changeset
1174 {
kono
parents:
diff changeset
1175 case PROTOCOL_INTERFACE_TYPE:
kono
parents:
diff changeset
1176 snprintf (buf, BUFSIZE, "_OBJC_ProtocolRefs_%s",
kono
parents:
diff changeset
1177 IDENTIFIER_POINTER (PROTOCOL_NAME (i_or_p)));
kono
parents:
diff changeset
1178 break;
kono
parents:
diff changeset
1179 case CLASS_INTERFACE_TYPE:
kono
parents:
diff changeset
1180 snprintf (buf, BUFSIZE, "_OBJC_ClassProtocols_%s",
kono
parents:
diff changeset
1181 IDENTIFIER_POINTER (CLASS_NAME (i_or_p)));
kono
parents:
diff changeset
1182 break;
kono
parents:
diff changeset
1183 case CATEGORY_INTERFACE_TYPE:
kono
parents:
diff changeset
1184 snprintf (buf, BUFSIZE, "_OBJC_CategoryProtocols_%s_%s",
kono
parents:
diff changeset
1185 IDENTIFIER_POINTER (CLASS_NAME (klass_ctxt)),
kono
parents:
diff changeset
1186 IDENTIFIER_POINTER (CLASS_SUPER_NAME (klass_ctxt)));
kono
parents:
diff changeset
1187 break;
kono
parents:
diff changeset
1188 default:
kono
parents:
diff changeset
1189 gcc_unreachable ();
kono
parents:
diff changeset
1190 }
kono
parents:
diff changeset
1191
kono
parents:
diff changeset
1192 ptype = build_pointer_type (objc_protocol_template);
kono
parents:
diff changeset
1193 array_type = build_sized_array_type (ptype, size + 3);
kono
parents:
diff changeset
1194 refs_decl = start_var_decl (array_type, buf);
kono
parents:
diff changeset
1195 OBJCMETA (refs_decl, objc_meta, meta_base);
kono
parents:
diff changeset
1196 finish_var_decl (refs_decl,
kono
parents:
diff changeset
1197 objc_build_constructor (TREE_TYPE (refs_decl), v));
kono
parents:
diff changeset
1198
kono
parents:
diff changeset
1199 return refs_decl;
kono
parents:
diff changeset
1200 }
kono
parents:
diff changeset
1201
kono
parents:
diff changeset
1202 static tree
kono
parents:
diff changeset
1203 generate_v1_meth_descriptor_table (tree chain, tree protocol, const char *prefix)
kono
parents:
diff changeset
1204 {
kono
parents:
diff changeset
1205 tree method_list_template, initlist, decl;
kono
parents:
diff changeset
1206 int size;
kono
parents:
diff changeset
1207 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1208 char buf[BUFSIZE];
kono
parents:
diff changeset
1209
kono
parents:
diff changeset
1210 if (!chain || !prefix)
kono
parents:
diff changeset
1211 return NULL_TREE;
kono
parents:
diff changeset
1212
kono
parents:
diff changeset
1213 if (!objc_method_prototype_template)
kono
parents:
diff changeset
1214 objc_method_prototype_template = build_method_prototype_template ();
kono
parents:
diff changeset
1215
kono
parents:
diff changeset
1216 size = list_length (chain);
kono
parents:
diff changeset
1217 method_list_template =
kono
parents:
diff changeset
1218 build_method_prototype_list_template (objc_method_prototype_template,
kono
parents:
diff changeset
1219 size);
kono
parents:
diff changeset
1220 snprintf (buf, BUFSIZE, "%s_%s", prefix,
kono
parents:
diff changeset
1221 IDENTIFIER_POINTER (PROTOCOL_NAME (protocol)));
kono
parents:
diff changeset
1222
kono
parents:
diff changeset
1223 decl = start_var_decl (method_list_template, buf);
kono
parents:
diff changeset
1224
kono
parents:
diff changeset
1225 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
kono
parents:
diff changeset
1226 initlist =
kono
parents:
diff changeset
1227 build_descriptor_table_initializer (objc_method_prototype_template,
kono
parents:
diff changeset
1228 chain);
kono
parents:
diff changeset
1229 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, initlist);
kono
parents:
diff changeset
1230 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
1231 finish_var_decl (decl, objc_build_constructor (method_list_template, v));
kono
parents:
diff changeset
1232 return decl;
kono
parents:
diff changeset
1233 }
kono
parents:
diff changeset
1234
kono
parents:
diff changeset
1235 /* For each protocol which was referenced either from a @protocol()
kono
parents:
diff changeset
1236 expression, or because a class/category implements it (then a
kono
parents:
diff changeset
1237 pointer to the protocol is stored in the struct describing the
kono
parents:
diff changeset
1238 class/category), we create a statically allocated instance of the
kono
parents:
diff changeset
1239 Protocol class. The code is written in such a way as to generate
kono
parents:
diff changeset
1240 as few Protocol objects as possible; we generate a unique Protocol
kono
parents:
diff changeset
1241 instance for each protocol, and we don't generate a Protocol
kono
parents:
diff changeset
1242 instance if the protocol is never referenced (either from a
kono
parents:
diff changeset
1243 @protocol() or from a class/category implementation). These
kono
parents:
diff changeset
1244 statically allocated objects can be referred to via the static
kono
parents:
diff changeset
1245 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
kono
parents:
diff changeset
1246
kono
parents:
diff changeset
1247 The statically allocated Protocol objects that we generate here
kono
parents:
diff changeset
1248 need to be fixed up at runtime in order to be used: the 'isa'
kono
parents:
diff changeset
1249 pointer of the objects need to be set up to point to the 'Protocol'
kono
parents:
diff changeset
1250 class, as known at runtime.
kono
parents:
diff changeset
1251
kono
parents:
diff changeset
1252 The GNU runtime fixes up all protocols before user code from the module
kono
parents:
diff changeset
1253 is executed; it requires pointers to those symbols
kono
parents:
diff changeset
1254 to be put in the objc_symtab (which is then passed as argument to
kono
parents:
diff changeset
1255 the function __objc_exec_class() which the compiler sets up to be
kono
parents:
diff changeset
1256 executed automatically when the module is loaded); setup of those
kono
parents:
diff changeset
1257 Protocol objects happen in two ways in the GNU runtime: all
kono
parents:
diff changeset
1258 Protocol objects referred to by a class or category implementation
kono
parents:
diff changeset
1259 are fixed up when the class/category is loaded; all Protocol
kono
parents:
diff changeset
1260 objects referred to by a @protocol() expression are added by the
kono
parents:
diff changeset
1261 compiler to the list of statically allocated instances to fixup
kono
parents:
diff changeset
1262 (the same list holding the statically allocated constant string
kono
parents:
diff changeset
1263 objects). Because, as explained above, the compiler generates as
kono
parents:
diff changeset
1264 few Protocol objects as possible, some Protocol object might end up
kono
parents:
diff changeset
1265 being referenced multiple times when compiled with the GNU runtime,
kono
parents:
diff changeset
1266 and end up being fixed up multiple times at runtime initialization.
kono
parents:
diff changeset
1267 But that doesn't hurt, it's just a little inefficient. */
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 static void
kono
parents:
diff changeset
1270 generate_protocols (void)
kono
parents:
diff changeset
1271 {
kono
parents:
diff changeset
1272 tree p, encoding;
kono
parents:
diff changeset
1273 tree decl;
kono
parents:
diff changeset
1274 tree initlist, protocol_name_expr, refs_decl, refs_expr;
kono
parents:
diff changeset
1275
kono
parents:
diff changeset
1276 /* If a protocol was directly referenced, pull in indirect references. */
kono
parents:
diff changeset
1277 for (p = protocol_chain; p; p = TREE_CHAIN (p))
kono
parents:
diff changeset
1278 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
kono
parents:
diff changeset
1279 generate_protocol_references (PROTOCOL_LIST (p));
kono
parents:
diff changeset
1280
kono
parents:
diff changeset
1281 for (p = protocol_chain; p; p = TREE_CHAIN (p))
kono
parents:
diff changeset
1282 {
kono
parents:
diff changeset
1283 tree nst_methods = PROTOCOL_NST_METHODS (p);
kono
parents:
diff changeset
1284 tree cls_methods = PROTOCOL_CLS_METHODS (p);
kono
parents:
diff changeset
1285
kono
parents:
diff changeset
1286 /* If protocol wasn't referenced, don't generate any code. */
kono
parents:
diff changeset
1287 decl = PROTOCOL_FORWARD_DECL (p);
kono
parents:
diff changeset
1288
kono
parents:
diff changeset
1289 if (!decl)
kono
parents:
diff changeset
1290 continue;
kono
parents:
diff changeset
1291
kono
parents:
diff changeset
1292 /* Make sure we link in the Protocol class. */
kono
parents:
diff changeset
1293 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
kono
parents:
diff changeset
1294
kono
parents:
diff changeset
1295 while (nst_methods)
kono
parents:
diff changeset
1296 {
kono
parents:
diff changeset
1297 if (! METHOD_ENCODING (nst_methods))
kono
parents:
diff changeset
1298 {
kono
parents:
diff changeset
1299 encoding = encode_method_prototype (nst_methods);
kono
parents:
diff changeset
1300 METHOD_ENCODING (nst_methods) = encoding;
kono
parents:
diff changeset
1301 }
kono
parents:
diff changeset
1302 nst_methods = DECL_CHAIN (nst_methods);
kono
parents:
diff changeset
1303 }
kono
parents:
diff changeset
1304
kono
parents:
diff changeset
1305 UOBJC_INSTANCE_METHODS_decl =
kono
parents:
diff changeset
1306 generate_v1_meth_descriptor_table (PROTOCOL_NST_METHODS (p), p,
kono
parents:
diff changeset
1307 "_OBJC_PROTOCOL_INSTANCE_METHODS");
kono
parents:
diff changeset
1308
kono
parents:
diff changeset
1309 while (cls_methods)
kono
parents:
diff changeset
1310 {
kono
parents:
diff changeset
1311 if (! METHOD_ENCODING (cls_methods))
kono
parents:
diff changeset
1312 {
kono
parents:
diff changeset
1313 encoding = encode_method_prototype (cls_methods);
kono
parents:
diff changeset
1314 METHOD_ENCODING (cls_methods) = encoding;
kono
parents:
diff changeset
1315 }
kono
parents:
diff changeset
1316
kono
parents:
diff changeset
1317 cls_methods = DECL_CHAIN (cls_methods);
kono
parents:
diff changeset
1318 }
kono
parents:
diff changeset
1319
kono
parents:
diff changeset
1320 UOBJC_CLASS_METHODS_decl =
kono
parents:
diff changeset
1321 generate_v1_meth_descriptor_table (PROTOCOL_CLS_METHODS (p), p,
kono
parents:
diff changeset
1322 "_OBJC_PROTOCOL_CLASS_METHODS");
kono
parents:
diff changeset
1323 /* generate_method_descriptors (p);*/
kono
parents:
diff changeset
1324
kono
parents:
diff changeset
1325 if (PROTOCOL_LIST (p))
kono
parents:
diff changeset
1326 refs_decl = generate_protocol_list (p, NULL_TREE);
kono
parents:
diff changeset
1327 else
kono
parents:
diff changeset
1328 refs_decl = 0;
kono
parents:
diff changeset
1329
kono
parents:
diff changeset
1330 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
kono
parents:
diff changeset
1331 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
kono
parents:
diff changeset
1332
kono
parents:
diff changeset
1333 if (refs_decl)
kono
parents:
diff changeset
1334 refs_expr = convert (build_pointer_type (build_pointer_type
kono
parents:
diff changeset
1335 (objc_protocol_template)),
kono
parents:
diff changeset
1336 build_unary_op (input_location,
kono
parents:
diff changeset
1337 ADDR_EXPR, refs_decl, 0));
kono
parents:
diff changeset
1338 else
kono
parents:
diff changeset
1339 refs_expr = build_int_cst (NULL_TREE, 0);
kono
parents:
diff changeset
1340
kono
parents:
diff changeset
1341 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
kono
parents:
diff changeset
1342 by generate_method_descriptors, which is called above. */
kono
parents:
diff changeset
1343 initlist = build_protocol_initializer (TREE_TYPE (decl),
kono
parents:
diff changeset
1344 protocol_name_expr, refs_expr,
kono
parents:
diff changeset
1345 UOBJC_INSTANCE_METHODS_decl,
kono
parents:
diff changeset
1346 UOBJC_CLASS_METHODS_decl);
kono
parents:
diff changeset
1347 finish_var_decl (decl, initlist);
kono
parents:
diff changeset
1348 }
kono
parents:
diff changeset
1349 }
kono
parents:
diff changeset
1350
kono
parents:
diff changeset
1351 static tree
kono
parents:
diff changeset
1352 generate_dispatch_table (tree chain, const char *name)
kono
parents:
diff changeset
1353 {
kono
parents:
diff changeset
1354 tree decl, method_list_template, initlist;
kono
parents:
diff changeset
1355 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1356 int size = list_length (chain);
kono
parents:
diff changeset
1357
kono
parents:
diff changeset
1358 if (!objc_method_template)
kono
parents:
diff changeset
1359 objc_method_template = build_method_template ();
kono
parents:
diff changeset
1360
kono
parents:
diff changeset
1361 method_list_template = build_method_list_template (objc_method_template,
kono
parents:
diff changeset
1362 size);
kono
parents:
diff changeset
1363 initlist = build_dispatch_table_initializer (objc_method_template, chain);
kono
parents:
diff changeset
1364
kono
parents:
diff changeset
1365 decl = start_var_decl (method_list_template, name);
kono
parents:
diff changeset
1366
kono
parents:
diff changeset
1367 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
kono
parents:
diff changeset
1368 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1369 build_int_cst (integer_type_node, size));
kono
parents:
diff changeset
1370 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, initlist);
kono
parents:
diff changeset
1371
kono
parents:
diff changeset
1372 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
1373 finish_var_decl (decl,
kono
parents:
diff changeset
1374 objc_build_constructor (TREE_TYPE (decl), v));
kono
parents:
diff changeset
1375
kono
parents:
diff changeset
1376 return decl;
kono
parents:
diff changeset
1377 }
kono
parents:
diff changeset
1378
kono
parents:
diff changeset
1379 /* Init a category. */
kono
parents:
diff changeset
1380 static tree
kono
parents:
diff changeset
1381 build_category_initializer (tree type, tree cat_name, tree class_name,
kono
parents:
diff changeset
1382 tree inst_methods, tree class_methods,
kono
parents:
diff changeset
1383 tree protocol_list)
kono
parents:
diff changeset
1384 {
kono
parents:
diff changeset
1385 tree expr, ltyp;
kono
parents:
diff changeset
1386 location_t loc;
kono
parents:
diff changeset
1387 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1388
kono
parents:
diff changeset
1389 /* TODO: pass the loc in or find it from args. */
kono
parents:
diff changeset
1390 /* TODO: pass the loc in or find it from args. */
kono
parents:
diff changeset
1391 loc = UNKNOWN_LOCATION;
kono
parents:
diff changeset
1392 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
kono
parents:
diff changeset
1393 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
kono
parents:
diff changeset
1394
kono
parents:
diff changeset
1395 ltyp = objc_method_list_ptr;
kono
parents:
diff changeset
1396 if (inst_methods)
kono
parents:
diff changeset
1397 expr = convert (ltyp, build_unary_op (loc, ADDR_EXPR, inst_methods, 0));
kono
parents:
diff changeset
1398 else
kono
parents:
diff changeset
1399 expr = convert (ltyp, null_pointer_node);
kono
parents:
diff changeset
1400 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1401
kono
parents:
diff changeset
1402 if (class_methods)
kono
parents:
diff changeset
1403 expr = convert (ltyp, build_unary_op (loc, ADDR_EXPR, class_methods, 0));
kono
parents:
diff changeset
1404 else
kono
parents:
diff changeset
1405 expr = convert (ltyp, null_pointer_node);
kono
parents:
diff changeset
1406 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1407
kono
parents:
diff changeset
1408 /* protocol_list = */
kono
parents:
diff changeset
1409 ltyp = build_pointer_type (build_pointer_type (objc_protocol_template));
kono
parents:
diff changeset
1410 if (protocol_list)
kono
parents:
diff changeset
1411 expr = convert (ltyp, build_unary_op (loc, ADDR_EXPR, protocol_list, 0));
kono
parents:
diff changeset
1412 else
kono
parents:
diff changeset
1413 expr = convert (ltyp, null_pointer_node);
kono
parents:
diff changeset
1414 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1415
kono
parents:
diff changeset
1416 return objc_build_constructor (type, v);
kono
parents:
diff changeset
1417 }
kono
parents:
diff changeset
1418
kono
parents:
diff changeset
1419 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
kono
parents:
diff changeset
1420
kono
parents:
diff changeset
1421 static void
kono
parents:
diff changeset
1422 generate_category (struct imp_entry *impent)
kono
parents:
diff changeset
1423 {
kono
parents:
diff changeset
1424 tree initlist, cat_name_expr, class_name_expr;
kono
parents:
diff changeset
1425 tree protocol_decl, category, cat_decl;
kono
parents:
diff changeset
1426 tree inst_methods = NULL_TREE, class_methods = NULL_TREE;
kono
parents:
diff changeset
1427 tree cat = impent->imp_context;
kono
parents:
diff changeset
1428 char buf[BUFSIZE];
kono
parents:
diff changeset
1429
kono
parents:
diff changeset
1430 cat_decl = impent->class_decl;
kono
parents:
diff changeset
1431
kono
parents:
diff changeset
1432 add_class_reference (CLASS_NAME (cat));
kono
parents:
diff changeset
1433 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
kono
parents:
diff changeset
1434
kono
parents:
diff changeset
1435 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
kono
parents:
diff changeset
1436
kono
parents:
diff changeset
1437 category = lookup_category (impent->imp_template, CLASS_SUPER_NAME (cat));
kono
parents:
diff changeset
1438
kono
parents:
diff changeset
1439 if (category && CLASS_PROTOCOL_LIST (category))
kono
parents:
diff changeset
1440 {
kono
parents:
diff changeset
1441 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
kono
parents:
diff changeset
1442 protocol_decl = generate_protocol_list (category, cat);
kono
parents:
diff changeset
1443 }
kono
parents:
diff changeset
1444 else
kono
parents:
diff changeset
1445 protocol_decl = 0;
kono
parents:
diff changeset
1446
kono
parents:
diff changeset
1447 if (CLASS_NST_METHODS (cat))
kono
parents:
diff changeset
1448 {
kono
parents:
diff changeset
1449 snprintf (buf, BUFSIZE, "_OBJC_CategoryInstanceMethods_%s_%s",
kono
parents:
diff changeset
1450 IDENTIFIER_POINTER (CLASS_NAME (cat)),
kono
parents:
diff changeset
1451 IDENTIFIER_POINTER (CLASS_SUPER_NAME (cat)));
kono
parents:
diff changeset
1452 inst_methods = generate_dispatch_table (CLASS_NST_METHODS (cat), buf);
kono
parents:
diff changeset
1453 }
kono
parents:
diff changeset
1454
kono
parents:
diff changeset
1455 if (CLASS_CLS_METHODS (cat))
kono
parents:
diff changeset
1456 {
kono
parents:
diff changeset
1457 snprintf (buf, BUFSIZE, "_OBJC_CategoryClassMethods_%s_%s",
kono
parents:
diff changeset
1458 IDENTIFIER_POINTER (CLASS_NAME (cat)),
kono
parents:
diff changeset
1459 IDENTIFIER_POINTER (CLASS_SUPER_NAME (cat)));
kono
parents:
diff changeset
1460 class_methods = generate_dispatch_table (CLASS_CLS_METHODS (cat), buf);
kono
parents:
diff changeset
1461 }
kono
parents:
diff changeset
1462
kono
parents:
diff changeset
1463 initlist = build_category_initializer (TREE_TYPE (cat_decl),
kono
parents:
diff changeset
1464 cat_name_expr, class_name_expr,
kono
parents:
diff changeset
1465 inst_methods, class_methods,
kono
parents:
diff changeset
1466 protocol_decl);
kono
parents:
diff changeset
1467 /* Finish and initialize the forward decl. */
kono
parents:
diff changeset
1468 finish_var_decl (cat_decl, initlist);
kono
parents:
diff changeset
1469 impent->class_decl = cat_decl;
kono
parents:
diff changeset
1470 }
kono
parents:
diff changeset
1471
kono
parents:
diff changeset
1472 /* struct _objc_class {
kono
parents:
diff changeset
1473 struct objc_class *isa;
kono
parents:
diff changeset
1474 struct objc_class *super_class;
kono
parents:
diff changeset
1475 char *name;
kono
parents:
diff changeset
1476 long version;
kono
parents:
diff changeset
1477 long info;
kono
parents:
diff changeset
1478 long instance_size;
kono
parents:
diff changeset
1479 struct objc_ivar_list *ivars;
kono
parents:
diff changeset
1480 struct objc_method_list *methods;
kono
parents:
diff changeset
1481 struct sarray *dtable;
kono
parents:
diff changeset
1482 struct objc_class *subclass_list;
kono
parents:
diff changeset
1483 struct objc_class *sibling_class;
kono
parents:
diff changeset
1484 struct objc_protocol_list *protocols;
kono
parents:
diff changeset
1485 void *gc_object_type;
kono
parents:
diff changeset
1486 }; */
kono
parents:
diff changeset
1487
kono
parents:
diff changeset
1488 static tree
kono
parents:
diff changeset
1489 build_shared_structure_initializer (tree type, tree isa, tree super,
kono
parents:
diff changeset
1490 tree name, tree size, int status,
kono
parents:
diff changeset
1491 tree dispatch_table, tree ivar_list,
kono
parents:
diff changeset
1492 tree protocol_list)
kono
parents:
diff changeset
1493 {
kono
parents:
diff changeset
1494 tree expr, ltyp;
kono
parents:
diff changeset
1495 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1496
kono
parents:
diff changeset
1497 /* isa = */
kono
parents:
diff changeset
1498 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
kono
parents:
diff changeset
1499
kono
parents:
diff changeset
1500 /* super_class = */
kono
parents:
diff changeset
1501 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
kono
parents:
diff changeset
1502
kono
parents:
diff changeset
1503 /* name = */
kono
parents:
diff changeset
1504 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
kono
parents:
diff changeset
1505
kono
parents:
diff changeset
1506 /* version = */
kono
parents:
diff changeset
1507 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1508 build_int_cst (long_integer_type_node, 0));
kono
parents:
diff changeset
1509
kono
parents:
diff changeset
1510 /* info = */
kono
parents:
diff changeset
1511 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1512 build_int_cst (long_integer_type_node, status));
kono
parents:
diff changeset
1513
kono
parents:
diff changeset
1514 /* instance_size = */
kono
parents:
diff changeset
1515 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1516 convert (long_integer_type_node, size));
kono
parents:
diff changeset
1517
kono
parents:
diff changeset
1518 /* objc_ivar_list = */
kono
parents:
diff changeset
1519 if (!ivar_list)
kono
parents:
diff changeset
1520 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1521 build_int_cst (objc_ivar_list_ptr, 0));
kono
parents:
diff changeset
1522 else
kono
parents:
diff changeset
1523 {
kono
parents:
diff changeset
1524 expr = convert (objc_ivar_list_ptr,
kono
parents:
diff changeset
1525 build_unary_op (input_location, ADDR_EXPR,
kono
parents:
diff changeset
1526 ivar_list, 0));
kono
parents:
diff changeset
1527 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1528 }
kono
parents:
diff changeset
1529
kono
parents:
diff changeset
1530 /* objc_method_list = */
kono
parents:
diff changeset
1531 if (!dispatch_table)
kono
parents:
diff changeset
1532 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1533 convert (objc_method_list_ptr, null_pointer_node));
kono
parents:
diff changeset
1534 else
kono
parents:
diff changeset
1535 {
kono
parents:
diff changeset
1536 expr = convert (objc_method_list_ptr,
kono
parents:
diff changeset
1537 build_unary_op (input_location, ADDR_EXPR,
kono
parents:
diff changeset
1538 dispatch_table, 0));
kono
parents:
diff changeset
1539 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1540 }
kono
parents:
diff changeset
1541
kono
parents:
diff changeset
1542 /* FIXME: Remove NeXT runtime code. */
kono
parents:
diff changeset
1543 if (flag_next_runtime)
kono
parents:
diff changeset
1544 {
kono
parents:
diff changeset
1545 ltyp = build_pointer_type (xref_tag (RECORD_TYPE,
kono
parents:
diff changeset
1546 get_identifier ("objc_cache")));
kono
parents:
diff changeset
1547 /* method_cache = */
kono
parents:
diff changeset
1548 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (ltyp, null_pointer_node));
kono
parents:
diff changeset
1549 }
kono
parents:
diff changeset
1550 else
kono
parents:
diff changeset
1551 {
kono
parents:
diff changeset
1552 /* dtable = */
kono
parents:
diff changeset
1553 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1554
kono
parents:
diff changeset
1555 /* subclass_list = */
kono
parents:
diff changeset
1556 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1557
kono
parents:
diff changeset
1558 /* sibling_class = */
kono
parents:
diff changeset
1559 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1560 }
kono
parents:
diff changeset
1561
kono
parents:
diff changeset
1562 /* protocol_list = */
kono
parents:
diff changeset
1563 ltyp = build_pointer_type (build_pointer_type (objc_protocol_template));
kono
parents:
diff changeset
1564 if (! protocol_list)
kono
parents:
diff changeset
1565 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (ltyp, 0));
kono
parents:
diff changeset
1566 else
kono
parents:
diff changeset
1567 {
kono
parents:
diff changeset
1568 expr = convert (ltyp,
kono
parents:
diff changeset
1569 build_unary_op (input_location, ADDR_EXPR,
kono
parents:
diff changeset
1570 protocol_list, 0));
kono
parents:
diff changeset
1571 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1572 }
kono
parents:
diff changeset
1573
kono
parents:
diff changeset
1574 /* FIXME: Remove NeXT runtime code. */
kono
parents:
diff changeset
1575 if (flag_next_runtime)
kono
parents:
diff changeset
1576 /* sel_id = NULL */
kono
parents:
diff changeset
1577 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1578
kono
parents:
diff changeset
1579 /* gc_object_type = NULL */
kono
parents:
diff changeset
1580 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1581
kono
parents:
diff changeset
1582 return objc_build_constructor (type, v);
kono
parents:
diff changeset
1583 }
kono
parents:
diff changeset
1584
kono
parents:
diff changeset
1585
kono
parents:
diff changeset
1586 static tree
kono
parents:
diff changeset
1587 generate_ivars_list (tree chain, const char *name)
kono
parents:
diff changeset
1588 {
kono
parents:
diff changeset
1589 tree initlist, ivar_list_template, decl;
kono
parents:
diff changeset
1590 int size;
kono
parents:
diff changeset
1591 vec<constructor_elt, va_gc> *inits = NULL;
kono
parents:
diff changeset
1592
kono
parents:
diff changeset
1593 if (!chain)
kono
parents:
diff changeset
1594 return NULL_TREE;
kono
parents:
diff changeset
1595
kono
parents:
diff changeset
1596 if (!objc_ivar_template)
kono
parents:
diff changeset
1597 objc_ivar_template = build_ivar_template ();
kono
parents:
diff changeset
1598
kono
parents:
diff changeset
1599 size = ivar_list_length (chain);
kono
parents:
diff changeset
1600
kono
parents:
diff changeset
1601 generating_instance_variables = 1;
kono
parents:
diff changeset
1602 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
kono
parents:
diff changeset
1603 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
kono
parents:
diff changeset
1604 generating_instance_variables = 0;
kono
parents:
diff changeset
1605
kono
parents:
diff changeset
1606 decl = start_var_decl (ivar_list_template, name);
kono
parents:
diff changeset
1607
kono
parents:
diff changeset
1608 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
kono
parents:
diff changeset
1609 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, initlist);
kono
parents:
diff changeset
1610
kono
parents:
diff changeset
1611 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
1612 finish_var_decl (decl,
kono
parents:
diff changeset
1613 objc_build_constructor (TREE_TYPE (decl), inits));
kono
parents:
diff changeset
1614
kono
parents:
diff changeset
1615 return decl;
kono
parents:
diff changeset
1616 }
kono
parents:
diff changeset
1617
kono
parents:
diff changeset
1618 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
kono
parents:
diff changeset
1619 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
kono
parents:
diff changeset
1620
kono
parents:
diff changeset
1621 static void
kono
parents:
diff changeset
1622 generate_class_structures (struct imp_entry *impent)
kono
parents:
diff changeset
1623 {
kono
parents:
diff changeset
1624 tree name_expr, super_expr, root_expr, class_decl, meta_decl;
kono
parents:
diff changeset
1625 tree my_root_id, my_super_id;
kono
parents:
diff changeset
1626 tree cast_type, initlist, protocol_decl;
kono
parents:
diff changeset
1627 tree inst_methods = NULL_TREE, class_methods = NULL_TREE;
kono
parents:
diff changeset
1628 tree chain, inst_ivars = NULL_TREE, class_ivars = NULL_TREE;
kono
parents:
diff changeset
1629 location_t loc;
kono
parents:
diff changeset
1630 char buf[BUFSIZE];
kono
parents:
diff changeset
1631 int cls_flags = 0 ;
kono
parents:
diff changeset
1632
kono
parents:
diff changeset
1633 /* objc_implementation_context = impent->imp_context;
kono
parents:
diff changeset
1634 implementation_template = impent->imp_template;*/
kono
parents:
diff changeset
1635 class_decl = impent->class_decl;
kono
parents:
diff changeset
1636 meta_decl = impent->meta_decl;
kono
parents:
diff changeset
1637 /* UOBJC_CLASS_decl = impent->class_decl;
kono
parents:
diff changeset
1638 UOBJC_METACLASS_decl = impent->meta_decl;*/
kono
parents:
diff changeset
1639
kono
parents:
diff changeset
1640 loc = DECL_SOURCE_LOCATION (impent->class_decl);
kono
parents:
diff changeset
1641
kono
parents:
diff changeset
1642 my_super_id = CLASS_SUPER_NAME (impent->imp_template);
kono
parents:
diff changeset
1643 if (my_super_id)
kono
parents:
diff changeset
1644 {
kono
parents:
diff changeset
1645 add_class_reference (my_super_id);
kono
parents:
diff changeset
1646
kono
parents:
diff changeset
1647 /* Compute "my_root_id" - this is required for code generation.
kono
parents:
diff changeset
1648 the "isa" for all meta class structures points to the root of
kono
parents:
diff changeset
1649 the inheritance hierarchy (e.g. "__Object")... */
kono
parents:
diff changeset
1650 my_root_id = my_super_id;
kono
parents:
diff changeset
1651 do
kono
parents:
diff changeset
1652 {
kono
parents:
diff changeset
1653 tree my_root_int = lookup_interface (my_root_id);
kono
parents:
diff changeset
1654
kono
parents:
diff changeset
1655 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
kono
parents:
diff changeset
1656 my_root_id = CLASS_SUPER_NAME (my_root_int);
kono
parents:
diff changeset
1657 else
kono
parents:
diff changeset
1658 break;
kono
parents:
diff changeset
1659 }
kono
parents:
diff changeset
1660 while (1);
kono
parents:
diff changeset
1661 }
kono
parents:
diff changeset
1662 else
kono
parents:
diff changeset
1663 /* No super class. */
kono
parents:
diff changeset
1664 my_root_id = CLASS_NAME (impent->imp_template);
kono
parents:
diff changeset
1665
kono
parents:
diff changeset
1666 cast_type = build_pointer_type (objc_class_template);
kono
parents:
diff changeset
1667 name_expr = add_objc_string (CLASS_NAME (impent->imp_template),
kono
parents:
diff changeset
1668 class_names);
kono
parents:
diff changeset
1669
kono
parents:
diff changeset
1670 /* Install class `isa' and `super' pointers at runtime. */
kono
parents:
diff changeset
1671 if (my_super_id)
kono
parents:
diff changeset
1672 super_expr = add_objc_string (my_super_id, class_names);
kono
parents:
diff changeset
1673 else
kono
parents:
diff changeset
1674 super_expr = null_pointer_node;
kono
parents:
diff changeset
1675
kono
parents:
diff changeset
1676 super_expr = build_c_cast (loc, cast_type, super_expr);
kono
parents:
diff changeset
1677
kono
parents:
diff changeset
1678 root_expr = add_objc_string (my_root_id, class_names);
kono
parents:
diff changeset
1679 root_expr = build_c_cast (loc, cast_type, root_expr);
kono
parents:
diff changeset
1680
kono
parents:
diff changeset
1681 if (CLASS_PROTOCOL_LIST (impent->imp_template))
kono
parents:
diff changeset
1682 {
kono
parents:
diff changeset
1683 generate_protocol_references (CLASS_PROTOCOL_LIST (impent->imp_template));
kono
parents:
diff changeset
1684 protocol_decl = generate_protocol_list (impent->imp_template,
kono
parents:
diff changeset
1685 impent->imp_context);
kono
parents:
diff changeset
1686 }
kono
parents:
diff changeset
1687 else
kono
parents:
diff changeset
1688 protocol_decl = NULL_TREE;
kono
parents:
diff changeset
1689
kono
parents:
diff changeset
1690 if (CLASS_CLS_METHODS (impent->imp_context))
kono
parents:
diff changeset
1691 {
kono
parents:
diff changeset
1692 snprintf (buf, BUFSIZE, "_OBJC_ClassMethods_%s",
kono
parents:
diff changeset
1693 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)));
kono
parents:
diff changeset
1694 class_methods = generate_dispatch_table (CLASS_CLS_METHODS (impent->imp_context),
kono
parents:
diff changeset
1695 buf);
kono
parents:
diff changeset
1696 }
kono
parents:
diff changeset
1697
kono
parents:
diff changeset
1698 if (CLASS_SUPER_NAME (impent->imp_template) == NULL_TREE
kono
parents:
diff changeset
1699 && (chain = TYPE_FIELDS (objc_class_template)))
kono
parents:
diff changeset
1700 {
kono
parents:
diff changeset
1701 snprintf (buf, BUFSIZE, "_OBJC_ClassIvars_%s",
kono
parents:
diff changeset
1702 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)));
kono
parents:
diff changeset
1703 class_ivars = generate_ivars_list (chain, buf);
kono
parents:
diff changeset
1704 }
kono
parents:
diff changeset
1705
kono
parents:
diff changeset
1706 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
kono
parents:
diff changeset
1707
kono
parents:
diff changeset
1708 initlist =
kono
parents:
diff changeset
1709 build_shared_structure_initializer
kono
parents:
diff changeset
1710 (TREE_TYPE (meta_decl),
kono
parents:
diff changeset
1711 root_expr, super_expr, name_expr,
kono
parents:
diff changeset
1712 convert (integer_type_node,
kono
parents:
diff changeset
1713 TYPE_SIZE_UNIT (objc_class_template)),
kono
parents:
diff changeset
1714 CLS_META, class_methods, class_ivars,
kono
parents:
diff changeset
1715 protocol_decl);
kono
parents:
diff changeset
1716
kono
parents:
diff changeset
1717 finish_var_decl (meta_decl, initlist);
kono
parents:
diff changeset
1718 impent->meta_decl = meta_decl;
kono
parents:
diff changeset
1719
kono
parents:
diff changeset
1720 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
kono
parents:
diff changeset
1721 if (CLASS_NST_METHODS (impent->imp_context))
kono
parents:
diff changeset
1722 {
kono
parents:
diff changeset
1723 snprintf (buf, BUFSIZE, "_OBJC_InstanceMethods_%s",
kono
parents:
diff changeset
1724 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)));
kono
parents:
diff changeset
1725 inst_methods = generate_dispatch_table (CLASS_NST_METHODS (impent->imp_context),
kono
parents:
diff changeset
1726 buf);
kono
parents:
diff changeset
1727 }
kono
parents:
diff changeset
1728
kono
parents:
diff changeset
1729 if ((chain = CLASS_IVARS (impent->imp_template)))
kono
parents:
diff changeset
1730 {
kono
parents:
diff changeset
1731 snprintf (buf, BUFSIZE, "_OBJC_InstanceIvars_%s",
kono
parents:
diff changeset
1732 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)));
kono
parents:
diff changeset
1733 inst_ivars = generate_ivars_list (chain, buf);
kono
parents:
diff changeset
1734 }
kono
parents:
diff changeset
1735
kono
parents:
diff changeset
1736 initlist =
kono
parents:
diff changeset
1737 build_shared_structure_initializer
kono
parents:
diff changeset
1738 (TREE_TYPE (class_decl),
kono
parents:
diff changeset
1739 build_unary_op (loc, ADDR_EXPR, meta_decl, 0),
kono
parents:
diff changeset
1740 super_expr, name_expr,
kono
parents:
diff changeset
1741 convert (integer_type_node,
kono
parents:
diff changeset
1742 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
kono
parents:
diff changeset
1743 (impent->imp_template))),
kono
parents:
diff changeset
1744 CLS_FACTORY | cls_flags, inst_methods, inst_ivars,
kono
parents:
diff changeset
1745 protocol_decl);
kono
parents:
diff changeset
1746
kono
parents:
diff changeset
1747 finish_var_decl (class_decl, initlist);
kono
parents:
diff changeset
1748 impent->class_decl = class_decl;
kono
parents:
diff changeset
1749 }
kono
parents:
diff changeset
1750
kono
parents:
diff changeset
1751 /* --- Output GNU Metadata --- */
kono
parents:
diff changeset
1752
kono
parents:
diff changeset
1753 /* TODO: Make this into an array of refs. */
kono
parents:
diff changeset
1754 static void
kono
parents:
diff changeset
1755 handle_class_ref (tree chain)
kono
parents:
diff changeset
1756 {
kono
parents:
diff changeset
1757 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
kono
parents:
diff changeset
1758 char *string = (char *) alloca (strlen (name) + 30);
kono
parents:
diff changeset
1759 tree decl;
kono
parents:
diff changeset
1760 tree exp;
kono
parents:
diff changeset
1761
kono
parents:
diff changeset
1762 sprintf (string, "__objc_class_name_%s", name);
kono
parents:
diff changeset
1763
kono
parents:
diff changeset
1764 /* Make a decl for this name, so we can use its address in a tree. */
kono
parents:
diff changeset
1765 decl = build_decl (input_location,
kono
parents:
diff changeset
1766 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
kono
parents:
diff changeset
1767 DECL_EXTERNAL (decl) = 1;
kono
parents:
diff changeset
1768 TREE_PUBLIC (decl) = 1;
kono
parents:
diff changeset
1769 DECL_CONTEXT (decl) = NULL_TREE;
kono
parents:
diff changeset
1770 finish_var_decl (decl, 0);
kono
parents:
diff changeset
1771
kono
parents:
diff changeset
1772 /* Make a decl for the address. */
kono
parents:
diff changeset
1773 sprintf (string, "__objc_class_ref_%s", name);
kono
parents:
diff changeset
1774 exp = build1 (ADDR_EXPR, string_type_node, decl);
kono
parents:
diff changeset
1775 decl = build_decl (input_location,
kono
parents:
diff changeset
1776 VAR_DECL, get_identifier (string), string_type_node);
kono
parents:
diff changeset
1777 TREE_STATIC (decl) = 1;
kono
parents:
diff changeset
1778 TREE_USED (decl) = 1;
kono
parents:
diff changeset
1779 DECL_READ_P (decl) = 1;
kono
parents:
diff changeset
1780 DECL_ARTIFICIAL (decl) = 1;
kono
parents:
diff changeset
1781 DECL_INITIAL (decl) = error_mark_node;
kono
parents:
diff changeset
1782
kono
parents:
diff changeset
1783 /* We must force the reference. */
kono
parents:
diff changeset
1784 DECL_PRESERVE_P (decl) = 1;
kono
parents:
diff changeset
1785
kono
parents:
diff changeset
1786 DECL_CONTEXT (decl) = NULL_TREE;
kono
parents:
diff changeset
1787 finish_var_decl (decl, exp);
kono
parents:
diff changeset
1788 }
kono
parents:
diff changeset
1789
kono
parents:
diff changeset
1790 static tree
kono
parents:
diff changeset
1791 get_proto_encoding (tree proto)
kono
parents:
diff changeset
1792 {
kono
parents:
diff changeset
1793 tree encoding;
kono
parents:
diff changeset
1794 if (proto)
kono
parents:
diff changeset
1795 {
kono
parents:
diff changeset
1796 if (! METHOD_ENCODING (proto))
kono
parents:
diff changeset
1797 {
kono
parents:
diff changeset
1798 encoding = encode_method_prototype (proto);
kono
parents:
diff changeset
1799 METHOD_ENCODING (proto) = encoding;
kono
parents:
diff changeset
1800 }
kono
parents:
diff changeset
1801 else
kono
parents:
diff changeset
1802 encoding = METHOD_ENCODING (proto);
kono
parents:
diff changeset
1803
kono
parents:
diff changeset
1804 return add_objc_string (encoding, meth_var_types);
kono
parents:
diff changeset
1805 }
kono
parents:
diff changeset
1806 else
kono
parents:
diff changeset
1807 return build_int_cst (NULL_TREE, 0);
kono
parents:
diff changeset
1808 }
kono
parents:
diff changeset
1809
kono
parents:
diff changeset
1810 static void
kono
parents:
diff changeset
1811 build_gnu_selector_translation_table (void)
kono
parents:
diff changeset
1812 {
kono
parents:
diff changeset
1813 tree chain, expr;
kono
parents:
diff changeset
1814 vec<constructor_elt, va_gc> *inits = NULL;
kono
parents:
diff changeset
1815 vec<constructor_elt, va_gc> *v ;
kono
parents:
diff changeset
1816
kono
parents:
diff changeset
1817 /* Cause the selector table (previously forward-declared)
kono
parents:
diff changeset
1818 to be actually output. */
kono
parents:
diff changeset
1819
kono
parents:
diff changeset
1820 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
kono
parents:
diff changeset
1821 {
kono
parents:
diff changeset
1822 tree encoding;
kono
parents:
diff changeset
1823 if (warn_selector)
kono
parents:
diff changeset
1824 {
kono
parents:
diff changeset
1825 /* TODO: improve on the location for the diagnostic. */
kono
parents:
diff changeset
1826 location_t loc = input_location;
kono
parents:
diff changeset
1827 diagnose_missing_method (TREE_VALUE (chain), loc);
kono
parents:
diff changeset
1828 }
kono
parents:
diff changeset
1829
kono
parents:
diff changeset
1830 v = NULL;
kono
parents:
diff changeset
1831 expr = build_selector (TREE_VALUE (chain));
kono
parents:
diff changeset
1832 encoding = get_proto_encoding (TREE_PURPOSE (chain));
kono
parents:
diff changeset
1833 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1834 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
kono
parents:
diff changeset
1835 expr = objc_build_constructor (objc_selector_template, v);
kono
parents:
diff changeset
1836
kono
parents:
diff changeset
1837 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
kono
parents:
diff changeset
1838 } /* each element in the chain */
kono
parents:
diff changeset
1839
kono
parents:
diff changeset
1840 /* List terminator. */
kono
parents:
diff changeset
1841 v = NULL;
kono
parents:
diff changeset
1842 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
kono
parents:
diff changeset
1843 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
kono
parents:
diff changeset
1844 expr = objc_build_constructor (objc_selector_template, v);
kono
parents:
diff changeset
1845
kono
parents:
diff changeset
1846 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
kono
parents:
diff changeset
1847 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
kono
parents:
diff changeset
1848 inits);
kono
parents:
diff changeset
1849 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
kono
parents:
diff changeset
1850 }
kono
parents:
diff changeset
1851
kono
parents:
diff changeset
1852 /* Output references to all statically allocated objects. Return the DECL
kono
parents:
diff changeset
1853 for the array built. */
kono
parents:
diff changeset
1854
kono
parents:
diff changeset
1855 static void
kono
parents:
diff changeset
1856 generate_static_references (void)
kono
parents:
diff changeset
1857 {
kono
parents:
diff changeset
1858 tree expr = NULL_TREE;
kono
parents:
diff changeset
1859 tree class_name, klass, decl;
kono
parents:
diff changeset
1860 tree cl_chain, in_chain, type
kono
parents:
diff changeset
1861 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
kono
parents:
diff changeset
1862 int num_inst, num_class;
kono
parents:
diff changeset
1863 char buf[BUFSIZE];
kono
parents:
diff changeset
1864 vec<constructor_elt, va_gc> *decls = NULL;
kono
parents:
diff changeset
1865
kono
parents:
diff changeset
1866 /* FIXME: Remove NeXT runtime code. */
kono
parents:
diff changeset
1867 if (flag_next_runtime)
kono
parents:
diff changeset
1868 gcc_unreachable ();
kono
parents:
diff changeset
1869
kono
parents:
diff changeset
1870 for (cl_chain = objc_static_instances, num_class = 0;
kono
parents:
diff changeset
1871 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
kono
parents:
diff changeset
1872 {
kono
parents:
diff changeset
1873 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1874
kono
parents:
diff changeset
1875 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
kono
parents:
diff changeset
1876 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
kono
parents:
diff changeset
1877
kono
parents:
diff changeset
1878 snprintf (buf, BUFSIZE, "_OBJC_STATIC_INSTANCES_%d", num_class);
kono
parents:
diff changeset
1879 decl = start_var_decl (type, buf);
kono
parents:
diff changeset
1880
kono
parents:
diff changeset
1881 /* Output {class_name, ...}. */
kono
parents:
diff changeset
1882 klass = TREE_VALUE (cl_chain);
kono
parents:
diff changeset
1883 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
kono
parents:
diff changeset
1884 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
1885 build_unary_op (input_location,
kono
parents:
diff changeset
1886 ADDR_EXPR, class_name, 1));
kono
parents:
diff changeset
1887
kono
parents:
diff changeset
1888 /* Output {..., instance, ...}. */
kono
parents:
diff changeset
1889 for (in_chain = TREE_PURPOSE (cl_chain);
kono
parents:
diff changeset
1890 in_chain; in_chain = TREE_CHAIN (in_chain))
kono
parents:
diff changeset
1891 {
kono
parents:
diff changeset
1892 expr = build_unary_op (input_location,
kono
parents:
diff changeset
1893 ADDR_EXPR, TREE_VALUE (in_chain), 1);
kono
parents:
diff changeset
1894 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1895 }
kono
parents:
diff changeset
1896
kono
parents:
diff changeset
1897 /* Output {..., NULL}. */
kono
parents:
diff changeset
1898 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1899
kono
parents:
diff changeset
1900 expr = objc_build_constructor (TREE_TYPE (decl), v);
kono
parents:
diff changeset
1901 OBJCMETA (decl, objc_meta, meta_base);
kono
parents:
diff changeset
1902 finish_var_decl (decl, expr);
kono
parents:
diff changeset
1903 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
kono
parents:
diff changeset
1904 build_unary_op (input_location,
kono
parents:
diff changeset
1905 ADDR_EXPR, decl, 1));
kono
parents:
diff changeset
1906 }
kono
parents:
diff changeset
1907
kono
parents:
diff changeset
1908 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
kono
parents:
diff changeset
1909 expr = objc_build_constructor (type, decls);
kono
parents:
diff changeset
1910 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
kono
parents:
diff changeset
1911 OBJCMETA (static_instances_decl, objc_meta, meta_base);
kono
parents:
diff changeset
1912 finish_var_decl (static_instances_decl, expr);
kono
parents:
diff changeset
1913 }
kono
parents:
diff changeset
1914
kono
parents:
diff changeset
1915 /* Create the initial value for the `defs' field of _objc_symtab.
kono
parents:
diff changeset
1916 This is a CONSTRUCTOR. */
kono
parents:
diff changeset
1917
kono
parents:
diff changeset
1918 static tree
kono
parents:
diff changeset
1919 init_def_list (tree type)
kono
parents:
diff changeset
1920 {
kono
parents:
diff changeset
1921 tree expr;
kono
parents:
diff changeset
1922 struct imp_entry *impent;
kono
parents:
diff changeset
1923 location_t loc;
kono
parents:
diff changeset
1924 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
1925
kono
parents:
diff changeset
1926 if (imp_count)
kono
parents:
diff changeset
1927 for (impent = imp_list; impent; impent = impent->next)
kono
parents:
diff changeset
1928 {
kono
parents:
diff changeset
1929 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
kono
parents:
diff changeset
1930 {
kono
parents:
diff changeset
1931 loc = DECL_SOURCE_LOCATION (impent->class_decl);
kono
parents:
diff changeset
1932 expr = build_unary_op (loc,
kono
parents:
diff changeset
1933 ADDR_EXPR, impent->class_decl, 0);
kono
parents:
diff changeset
1934 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1935 }
kono
parents:
diff changeset
1936 }
kono
parents:
diff changeset
1937
kono
parents:
diff changeset
1938 if (cat_count)
kono
parents:
diff changeset
1939 for (impent = imp_list; impent; impent = impent->next)
kono
parents:
diff changeset
1940 {
kono
parents:
diff changeset
1941 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
kono
parents:
diff changeset
1942 {
kono
parents:
diff changeset
1943 loc = DECL_SOURCE_LOCATION (impent->class_decl);
kono
parents:
diff changeset
1944 expr = build_unary_op (loc,
kono
parents:
diff changeset
1945 ADDR_EXPR, impent->class_decl, 0);
kono
parents:
diff changeset
1946 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1947 }
kono
parents:
diff changeset
1948 }
kono
parents:
diff changeset
1949
kono
parents:
diff changeset
1950 loc = UNKNOWN_LOCATION;
kono
parents:
diff changeset
1951 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
kono
parents:
diff changeset
1952 if (static_instances_decl)
kono
parents:
diff changeset
1953 expr = build_unary_op (loc, ADDR_EXPR, static_instances_decl, 0);
kono
parents:
diff changeset
1954 else
kono
parents:
diff changeset
1955 expr = integer_zero_node;
kono
parents:
diff changeset
1956 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
1957
kono
parents:
diff changeset
1958 return objc_build_constructor (type, v);
kono
parents:
diff changeset
1959 }
kono
parents:
diff changeset
1960
kono
parents:
diff changeset
1961 /* Take care of defining and initializing _OBJC_SYMBOLS. */
kono
parents:
diff changeset
1962
kono
parents:
diff changeset
1963 /* Predefine the following data type:
kono
parents:
diff changeset
1964
kono
parents:
diff changeset
1965 struct _objc_symtab
kono
parents:
diff changeset
1966 {
kono
parents:
diff changeset
1967 long sel_ref_cnt;
kono
parents:
diff changeset
1968 SEL *refs;
kono
parents:
diff changeset
1969 short cls_def_cnt;
kono
parents:
diff changeset
1970 short cat_def_cnt;
kono
parents:
diff changeset
1971 void *defs[cls_def_cnt + cat_def_cnt];
kono
parents:
diff changeset
1972 }; */
kono
parents:
diff changeset
1973
kono
parents:
diff changeset
1974 static void
kono
parents:
diff changeset
1975 build_objc_symtab_template (void)
kono
parents:
diff changeset
1976 {
kono
parents:
diff changeset
1977 tree fields, array_type, *chain = NULL;
kono
parents:
diff changeset
1978 int index;
kono
parents:
diff changeset
1979
kono
parents:
diff changeset
1980 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
kono
parents:
diff changeset
1981
kono
parents:
diff changeset
1982 /* long sel_ref_cnt; */
kono
parents:
diff changeset
1983 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
kono
parents:
diff changeset
1984
kono
parents:
diff changeset
1985 /* SEL *refs; */
kono
parents:
diff changeset
1986 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
kono
parents:
diff changeset
1987
kono
parents:
diff changeset
1988 /* short cls_def_cnt; */
kono
parents:
diff changeset
1989 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
kono
parents:
diff changeset
1990
kono
parents:
diff changeset
1991 /* short cat_def_cnt; */
kono
parents:
diff changeset
1992 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
kono
parents:
diff changeset
1993
kono
parents:
diff changeset
1994 /* Note that padding will be added here on LP64. */
kono
parents:
diff changeset
1995
kono
parents:
diff changeset
1996 /* void *defs[imp_count + cat_count (+ 1)]; */
kono
parents:
diff changeset
1997 /* NB: The index is one less than the size of the array. */
kono
parents:
diff changeset
1998 index = imp_count + cat_count;
kono
parents:
diff changeset
1999 array_type = build_sized_array_type (ptr_type_node, index + 1);
kono
parents:
diff changeset
2000 add_field_decl (array_type, "defs", &chain);
kono
parents:
diff changeset
2001
kono
parents:
diff changeset
2002 objc_finish_struct (objc_symtab_template, fields);
kono
parents:
diff changeset
2003 }
kono
parents:
diff changeset
2004 /* Construct the initial value for all of _objc_symtab. */
kono
parents:
diff changeset
2005
kono
parents:
diff changeset
2006 static tree
kono
parents:
diff changeset
2007 init_objc_symtab (tree type)
kono
parents:
diff changeset
2008 {
kono
parents:
diff changeset
2009 tree field, expr, ltyp;
kono
parents:
diff changeset
2010 location_t loc;
kono
parents:
diff changeset
2011 vec<constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
2012
kono
parents:
diff changeset
2013 loc = UNKNOWN_LOCATION;
kono
parents:
diff changeset
2014
kono
parents:
diff changeset
2015 /* sel_ref_cnt = { ..., 5, ... } */
kono
parents:
diff changeset
2016
kono
parents:
diff changeset
2017 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
2018 build_int_cst (long_integer_type_node, 0));
kono
parents:
diff changeset
2019
kono
parents:
diff changeset
2020 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
kono
parents:
diff changeset
2021
kono
parents:
diff changeset
2022 ltyp = build_pointer_type (objc_selector_type);
kono
parents:
diff changeset
2023 if (sel_ref_chain)
kono
parents:
diff changeset
2024 expr = convert (ltyp, build_unary_op (loc, ADDR_EXPR,
kono
parents:
diff changeset
2025 UOBJC_SELECTOR_TABLE_decl, 1));
kono
parents:
diff changeset
2026 else
kono
parents:
diff changeset
2027 expr = convert (ltyp, null_pointer_node);
kono
parents:
diff changeset
2028 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
kono
parents:
diff changeset
2029
kono
parents:
diff changeset
2030 /* cls_def_cnt = { ..., 5, ... } */
kono
parents:
diff changeset
2031
kono
parents:
diff changeset
2032 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
2033 build_int_cst (short_integer_type_node, imp_count));
kono
parents:
diff changeset
2034
kono
parents:
diff changeset
2035 /* cat_def_cnt = { ..., 5, ... } */
kono
parents:
diff changeset
2036
kono
parents:
diff changeset
2037 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
2038 build_int_cst (short_integer_type_node, cat_count));
kono
parents:
diff changeset
2039
kono
parents:
diff changeset
2040 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
kono
parents:
diff changeset
2041
kono
parents:
diff changeset
2042 field = TYPE_FIELDS (type);
kono
parents:
diff changeset
2043 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
kono
parents:
diff changeset
2044
kono
parents:
diff changeset
2045 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
kono
parents:
diff changeset
2046
kono
parents:
diff changeset
2047 return objc_build_constructor (type, v);
kono
parents:
diff changeset
2048 }
kono
parents:
diff changeset
2049
kono
parents:
diff changeset
2050 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
kono
parents:
diff changeset
2051 and initialized appropriately. */
kono
parents:
diff changeset
2052
kono
parents:
diff changeset
2053 static void
kono
parents:
diff changeset
2054 generate_objc_symtab_decl (void)
kono
parents:
diff changeset
2055 {
kono
parents:
diff changeset
2056 build_objc_symtab_template ();
kono
parents:
diff changeset
2057 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
kono
parents:
diff changeset
2058 OBJCMETA (UOBJC_SYMBOLS_decl, objc_meta, meta_base);
kono
parents:
diff changeset
2059 finish_var_decl (UOBJC_SYMBOLS_decl,
kono
parents:
diff changeset
2060 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
kono
parents:
diff changeset
2061 }
kono
parents:
diff changeset
2062
kono
parents:
diff changeset
2063 static void
kono
parents:
diff changeset
2064 objc_generate_v1_gnu_metadata (void)
kono
parents:
diff changeset
2065 {
kono
parents:
diff changeset
2066 struct imp_entry *impent;
kono
parents:
diff changeset
2067 tree chain;
kono
parents:
diff changeset
2068
kono
parents:
diff changeset
2069 /* Process the static instances here because initialization of objc_symtab
kono
parents:
diff changeset
2070 depends on them. */
kono
parents:
diff changeset
2071 if (objc_static_instances)
kono
parents:
diff changeset
2072 generate_static_references ();
kono
parents:
diff changeset
2073
kono
parents:
diff changeset
2074 objc_implementation_context =
kono
parents:
diff changeset
2075 implementation_template =
kono
parents:
diff changeset
2076 UOBJC_CLASS_decl =
kono
parents:
diff changeset
2077 UOBJC_METACLASS_decl = NULL_TREE;
kono
parents:
diff changeset
2078
kono
parents:
diff changeset
2079 for (impent = imp_list; impent; impent = impent->next)
kono
parents:
diff changeset
2080 {
kono
parents:
diff changeset
2081 /* If -gen-decls is present, Dump the @interface of each class.
kono
parents:
diff changeset
2082 TODO: Dump the classes in the order they were found, rather than in
kono
parents:
diff changeset
2083 reverse order as we are doing now. */
kono
parents:
diff changeset
2084 if (flag_gen_declaration)
kono
parents:
diff changeset
2085 dump_interface (gen_declaration_file, impent->imp_context);
kono
parents:
diff changeset
2086
kono
parents:
diff changeset
2087 /* all of the following reference the string pool... */
kono
parents:
diff changeset
2088 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
kono
parents:
diff changeset
2089 generate_class_structures (impent);
kono
parents:
diff changeset
2090 else
kono
parents:
diff changeset
2091 generate_category (impent);
kono
parents:
diff changeset
2092 }
kono
parents:
diff changeset
2093
kono
parents:
diff changeset
2094 /* If we are using an array of selectors, we must always
kono
parents:
diff changeset
2095 finish up the array decl even if no selectors were used. */
kono
parents:
diff changeset
2096 build_gnu_selector_translation_table ();
kono
parents:
diff changeset
2097
kono
parents:
diff changeset
2098 if (protocol_chain)
kono
parents:
diff changeset
2099 generate_protocols ();
kono
parents:
diff changeset
2100
kono
parents:
diff changeset
2101 /* Arrange for ObjC data structures to be initialized at run time. */
kono
parents:
diff changeset
2102 /* FIXME: Have some more elegant way to determine if we need to
kono
parents:
diff changeset
2103 generate objc_symtab_decl or not, instead of checking these
kono
parents:
diff changeset
2104 global symbols. */
kono
parents:
diff changeset
2105 if (imp_list || class_names_chain
kono
parents:
diff changeset
2106 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain
kono
parents:
diff changeset
2107 || prop_names_attr_chain)
kono
parents:
diff changeset
2108 generate_objc_symtab_decl ();
kono
parents:
diff changeset
2109
kono
parents:
diff changeset
2110 if (imp_list || class_names_chain || objc_static_instances
kono
parents:
diff changeset
2111 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
kono
parents:
diff changeset
2112 {
kono
parents:
diff changeset
2113 /* Make sure that the meta-data are identified as being
kono
parents:
diff changeset
2114 GNU-runtime. */
kono
parents:
diff changeset
2115 build_module_descriptor (OBJC_VERSION,
kono
parents:
diff changeset
2116 build_tree_list (objc_meta, meta_base));
kono
parents:
diff changeset
2117 build_module_initializer_routine ();
kono
parents:
diff changeset
2118 }
kono
parents:
diff changeset
2119
kono
parents:
diff changeset
2120 /* Dump the class references. This forces the appropriate classes
kono
parents:
diff changeset
2121 to be linked into the executable image, preserving unix archive
kono
parents:
diff changeset
2122 semantics. This can be removed when we move to a more dynamically
kono
parents:
diff changeset
2123 linked environment. */
kono
parents:
diff changeset
2124
kono
parents:
diff changeset
2125 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
kono
parents:
diff changeset
2126 {
kono
parents:
diff changeset
2127 handle_class_ref (chain);
kono
parents:
diff changeset
2128 if (TREE_PURPOSE (chain))
kono
parents:
diff changeset
2129 generate_classref_translation_entry (chain);
kono
parents:
diff changeset
2130 }
kono
parents:
diff changeset
2131
kono
parents:
diff changeset
2132 for (impent = imp_list; impent; impent = impent->next)
kono
parents:
diff changeset
2133 handle_impent (impent);
kono
parents:
diff changeset
2134
kono
parents:
diff changeset
2135 generate_strings ();
kono
parents:
diff changeset
2136 }
kono
parents:
diff changeset
2137
kono
parents:
diff changeset
2138 /* --- exceptions --- */
kono
parents:
diff changeset
2139
kono
parents:
diff changeset
2140 static GTY(()) tree objc_eh_personality_decl;
kono
parents:
diff changeset
2141
kono
parents:
diff changeset
2142 static tree
kono
parents:
diff changeset
2143 objc_eh_runtime_type (tree type)
kono
parents:
diff changeset
2144 {
kono
parents:
diff changeset
2145 tree ident, eh_id, decl, str;
kono
parents:
diff changeset
2146
kono
parents:
diff changeset
2147 if (type == error_mark_node
kono
parents:
diff changeset
2148 || errorcount || sorrycount)
kono
parents:
diff changeset
2149 {
kono
parents:
diff changeset
2150 /* Use 'ErrorMarkNode' as class name when error_mark_node is found
kono
parents:
diff changeset
2151 to prevent an ICE. Note that we know that the compiler will
kono
parents:
diff changeset
2152 terminate with an error and this 'ErrorMarkNode' class name will
kono
parents:
diff changeset
2153 never be actually used. */
kono
parents:
diff changeset
2154 ident = get_identifier ("ErrorMarkNode");
kono
parents:
diff changeset
2155 goto make_err_class;
kono
parents:
diff changeset
2156 }
kono
parents:
diff changeset
2157
kono
parents:
diff changeset
2158 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
kono
parents:
diff changeset
2159 /* We don't want to identify 'id' for GNU. Instead, build a 0
kono
parents:
diff changeset
2160 entry in the exceptions table. */
kono
parents:
diff changeset
2161 return null_pointer_node;
kono
parents:
diff changeset
2162
kono
parents:
diff changeset
2163 if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
kono
parents:
diff changeset
2164 {
kono
parents:
diff changeset
2165 #ifdef OBJCPLUS
kono
parents:
diff changeset
2166 /* This routine is also called for c++ catch clauses; in which case,
kono
parents:
diff changeset
2167 we use the c++ typeinfo decl. */
kono
parents:
diff changeset
2168 return build_eh_type_type (type);
kono
parents:
diff changeset
2169 #else
kono
parents:
diff changeset
2170 error ("non-objective-c type %qT cannot be caught", type);
kono
parents:
diff changeset
2171 ident = get_identifier ("ErrorMarkNode");
kono
parents:
diff changeset
2172 goto make_err_class;
kono
parents:
diff changeset
2173 #endif
kono
parents:
diff changeset
2174 }
kono
parents:
diff changeset
2175 else
kono
parents:
diff changeset
2176 ident = OBJC_TYPE_NAME (TREE_TYPE (type));
kono
parents:
diff changeset
2177
kono
parents:
diff changeset
2178 make_err_class:
kono
parents:
diff changeset
2179 /* If this class was already referenced, then it will be output during
kono
parents:
diff changeset
2180 meta-data emission, so we don't need to do it here. */
kono
parents:
diff changeset
2181 decl = get_objc_string_decl (ident, class_names);
kono
parents:
diff changeset
2182 eh_id = add_objc_string (ident, class_names);
kono
parents:
diff changeset
2183 if (!decl)
kono
parents:
diff changeset
2184 {
kono
parents:
diff changeset
2185 /* Not found ... so we need to build it - from the freshly-entered id. */
kono
parents:
diff changeset
2186 decl = get_objc_string_decl (ident, class_names);
kono
parents:
diff changeset
2187 str = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
kono
parents:
diff changeset
2188 IDENTIFIER_POINTER (ident));
kono
parents:
diff changeset
2189 /* We have to finalize this var here, because this might be called after
kono
parents:
diff changeset
2190 all the other metadata strings have been emitted. */
kono
parents:
diff changeset
2191 finish_var_decl (decl, str);
kono
parents:
diff changeset
2192 }
kono
parents:
diff changeset
2193 return eh_id;
kono
parents:
diff changeset
2194 }
kono
parents:
diff changeset
2195
kono
parents:
diff changeset
2196 static tree
kono
parents:
diff changeset
2197 objc_eh_personality (void)
kono
parents:
diff changeset
2198 {
kono
parents:
diff changeset
2199 if (!objc_eh_personality_decl)
kono
parents:
diff changeset
2200 #ifndef OBJCPLUS
kono
parents:
diff changeset
2201 objc_eh_personality_decl = build_personality_function ("gnu_objc");
kono
parents:
diff changeset
2202 #else
kono
parents:
diff changeset
2203 objc_eh_personality_decl = build_personality_function ("gxx");
kono
parents:
diff changeset
2204 #endif
kono
parents:
diff changeset
2205 return objc_eh_personality_decl;
kono
parents:
diff changeset
2206 }
kono
parents:
diff changeset
2207
kono
parents:
diff changeset
2208 /* -- interfaces --- */
kono
parents:
diff changeset
2209
kono
parents:
diff changeset
2210 static tree
kono
parents:
diff changeset
2211 build_throw_stmt (location_t loc, tree throw_expr, bool rethrown ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
2212 {
kono
parents:
diff changeset
2213 tree t;
kono
parents:
diff changeset
2214 vec<tree, va_gc> *parms;
kono
parents:
diff changeset
2215 vec_alloc (parms, 1);
kono
parents:
diff changeset
2216 /* A throw is just a call to the runtime throw function with the
kono
parents:
diff changeset
2217 object as a parameter. */
kono
parents:
diff changeset
2218 parms->quick_push (throw_expr);
kono
parents:
diff changeset
2219 t = build_function_call_vec (loc, vNULL, objc_exception_throw_decl, parms,
kono
parents:
diff changeset
2220 NULL);
kono
parents:
diff changeset
2221 vec_free (parms);
kono
parents:
diff changeset
2222 return add_stmt (t);
kono
parents:
diff changeset
2223 }
kono
parents:
diff changeset
2224
kono
parents:
diff changeset
2225 /* Build __builtin_eh_pointer. */
kono
parents:
diff changeset
2226
kono
parents:
diff changeset
2227 static tree
kono
parents:
diff changeset
2228 objc_build_exc_ptr (struct objc_try_context **x ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
2229 {
kono
parents:
diff changeset
2230 tree t;
kono
parents:
diff changeset
2231 t = builtin_decl_explicit (BUILT_IN_EH_POINTER);
kono
parents:
diff changeset
2232 t = build_call_expr (t, 1, integer_zero_node);
kono
parents:
diff changeset
2233 return fold_convert (objc_object_type, t);
kono
parents:
diff changeset
2234 }
kono
parents:
diff changeset
2235
kono
parents:
diff changeset
2236 static tree
kono
parents:
diff changeset
2237 begin_catch (struct objc_try_context **cur_try_context, tree type,
kono
parents:
diff changeset
2238 tree decl, tree compound, bool ellipsis ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
2239 {
kono
parents:
diff changeset
2240 tree t;
kono
parents:
diff changeset
2241 /* Record the data for the catch in the try context so that we can
kono
parents:
diff changeset
2242 finalize it later. */
kono
parents:
diff changeset
2243 if (ellipsis)
kono
parents:
diff changeset
2244 t = build_stmt (input_location, CATCH_EXPR, NULL, compound);
kono
parents:
diff changeset
2245 else
kono
parents:
diff changeset
2246 t = build_stmt (input_location, CATCH_EXPR, type, compound);
kono
parents:
diff changeset
2247 (*cur_try_context)->current_catch = t;
kono
parents:
diff changeset
2248
kono
parents:
diff changeset
2249 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
kono
parents:
diff changeset
2250 t = objc_build_exc_ptr (cur_try_context);
kono
parents:
diff changeset
2251 t = convert (TREE_TYPE (decl), t);
kono
parents:
diff changeset
2252 return build2 (MODIFY_EXPR, void_type_node, decl, t);
kono
parents:
diff changeset
2253 }
kono
parents:
diff changeset
2254
kono
parents:
diff changeset
2255 static void
kono
parents:
diff changeset
2256 finish_catch (struct objc_try_context **cur_try_context, tree current_catch)
kono
parents:
diff changeset
2257 {
kono
parents:
diff changeset
2258 append_to_statement_list (current_catch, &((*cur_try_context)->catch_list));
kono
parents:
diff changeset
2259 }
kono
parents:
diff changeset
2260
kono
parents:
diff changeset
2261 static tree
kono
parents:
diff changeset
2262 finish_try_stmt (struct objc_try_context **cur_try_context)
kono
parents:
diff changeset
2263 {
kono
parents:
diff changeset
2264 struct objc_try_context *c = *cur_try_context;
kono
parents:
diff changeset
2265 tree stmt = c->try_body;
kono
parents:
diff changeset
2266 if (c->catch_list)
kono
parents:
diff changeset
2267 stmt = build_stmt (c->try_locus, TRY_CATCH_EXPR, stmt, c->catch_list);
kono
parents:
diff changeset
2268 if (c->finally_body)
kono
parents:
diff changeset
2269 stmt = build_stmt (c->try_locus, TRY_FINALLY_EXPR, stmt, c->finally_body);
kono
parents:
diff changeset
2270 return stmt;
kono
parents:
diff changeset
2271 }
kono
parents:
diff changeset
2272
kono
parents:
diff changeset
2273 #include "gt-objc-objc-gnu-runtime-abi-01.h"