annotate gcc/objc/objc-gnu-runtime-abi-01.c @ 145:1830386684a0

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