2015-05-22 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / objc / objc-encoding.c
blob31c44e8afcacbff33c9bb071c1331adbfd2fadbf
1 /* Routines dealing with ObjC encoding of types
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "hash-set.h"
24 #include "machmode.h"
25 #include "vec.h"
26 #include "double-int.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "options.h"
31 #include "wide-int.h"
32 #include "inchash.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "stor-layout.h"
37 #ifdef OBJCPLUS
38 #include "cp/cp-tree.h"
39 #else
40 #include "c/c-tree.h"
41 #include "c/c-lang.h"
42 #endif
44 #include "c-family/c-common.h"
45 #include "c-family/c-objc.h"
47 #include "objc-encoding.h"
48 #include "objc-act.h"
50 /* For my_build_string(). */
51 #include "objc-runtime-shared-support.h"
53 /* For BITS_PER_UNIT. */
54 #include "tm.h"
56 /* When building Objective-C++, we are not linking against the C front-end
57 and so need to replicate the C tree-construction functions in some way. */
58 #ifdef OBJCPLUS
59 #define OBJCP_REMAP_FUNCTIONS
60 #include "objcp-decl.h"
61 #endif /* OBJCPLUS */
63 /* Set up for use of obstacks. */
64 #include "obstack.h"
66 /* This obstack is used to accumulate the encoding of a data type. */
67 static struct obstack util_obstack;
69 /* This points to the beginning of obstack contents, so we can free
70 the whole contents. */
71 static char *util_firstobj;
73 void objc_encoding_init (void)
75 gcc_obstack_init (&util_obstack);
76 util_firstobj = (char *) obstack_finish (&util_obstack);
79 int generating_instance_variables = 0;
81 static void encode_type_qualifiers (tree);
82 static void encode_type (tree, int, int);
83 static void encode_field (tree field_decl, int curtype, int format);
85 static tree
86 objc_method_parm_type (tree type)
88 type = TREE_VALUE (TREE_TYPE (type));
89 if (TREE_CODE (type) == TYPE_DECL)
90 type = TREE_TYPE (type);
91 return type;
94 static int
95 objc_encoded_type_size (tree type)
97 int sz = int_size_in_bytes (type);
99 /* Make all integer and enum types at least as large
100 as an int. */
101 if (sz > 0 && INTEGRAL_TYPE_P (type))
102 sz = MAX (sz, int_size_in_bytes (integer_type_node));
103 /* Treat arrays as pointers, since that's how they're
104 passed in. */
105 else if (TREE_CODE (type) == ARRAY_TYPE)
106 sz = int_size_in_bytes (ptr_type_node);
107 return sz;
110 /* Encode a method prototype. */
111 tree
112 encode_method_prototype (tree method_decl)
114 tree parms;
115 int parm_offset, i;
116 char buf[40];
117 tree result;
119 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
120 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
122 /* Encode return type. */
123 encode_type (objc_method_parm_type (method_decl),
124 obstack_object_size (&util_obstack),
125 OBJC_ENCODE_INLINE_DEFS);
127 /* Stack size. */
128 /* The first two arguments (self and _cmd) are pointers; account for
129 their size. */
130 i = int_size_in_bytes (ptr_type_node);
131 parm_offset = 2 * i;
132 for (parms = METHOD_SEL_ARGS (method_decl); parms;
133 parms = DECL_CHAIN (parms))
135 tree type = objc_method_parm_type (parms);
136 int sz = objc_encoded_type_size (type);
138 /* If a type size is not known, bail out. */
139 if (sz < 0)
141 error_at (DECL_SOURCE_LOCATION (method_decl),
142 "type %qT does not have a known size",
143 type);
144 /* Pretend that the encoding succeeded; the compilation will
145 fail nevertheless. */
146 goto finish_encoding;
148 parm_offset += sz;
151 sprintf (buf, "%d@0:%d", parm_offset, i);
152 obstack_grow (&util_obstack, buf, strlen (buf));
154 /* Argument types. */
155 parm_offset = 2 * i;
156 for (parms = METHOD_SEL_ARGS (method_decl); parms;
157 parms = DECL_CHAIN (parms))
159 tree type = objc_method_parm_type (parms);
161 /* Process argument qualifiers for user supplied arguments. */
162 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
164 /* Type. */
165 encode_type (type, obstack_object_size (&util_obstack),
166 OBJC_ENCODE_INLINE_DEFS);
168 /* Compute offset. */
169 sprintf (buf, "%d", parm_offset);
170 parm_offset += objc_encoded_type_size (type);
172 obstack_grow (&util_obstack, buf, strlen (buf));
175 finish_encoding:
176 obstack_1grow (&util_obstack, '\0');
177 result = get_identifier (XOBFINISH (&util_obstack, char *));
178 obstack_free (&util_obstack, util_firstobj);
179 return result;
182 /* This is used to implement @encode(). */
183 tree
184 objc_build_encode_expr (tree type)
186 tree result;
187 const char *string;
189 encode_type (type, obstack_object_size (&util_obstack),
190 OBJC_ENCODE_INLINE_DEFS);
191 obstack_1grow (&util_obstack, 0); /* null terminate string */
192 string = XOBFINISH (&util_obstack, const char *);
194 /* Synthesize a string that represents the encoded struct/union. */
195 result = my_build_string (strlen (string) + 1, string);
196 obstack_free (&util_obstack, util_firstobj);
197 return result;
200 /* "Encode" a data type into a string, which grows in util_obstack.
202 The format is described in gcc/doc/objc.texi, section 'Type
203 encoding'.
205 Most of the encode_xxx functions have a 'type' argument, which is
206 the type to encode, and an integer 'curtype' argument, which is the
207 index in the encoding string of the beginning of the encoding of
208 the current type, and allows you to find what characters have
209 already been written for the current type (they are the ones in the
210 current encoding string starting from 'curtype').
212 For example, if we are encoding a method which returns 'int' and
213 takes a 'char **' argument, then when we get to the point of
214 encoding the 'char **' argument, the encoded string already
215 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
216 'curtype' will be set to 7 when starting to encode 'char **'.
217 During the whole of the encoding of 'char **', 'curtype' will be
218 fixed at 7, so the routine encoding the second pointer can find out
219 that it's actually encoding a pointer to a pointer by looking
220 backwards at what has already been encoded for the current type,
221 and seeing there is a "^" (meaning a pointer) in there. */
224 /* Encode type qualifiers encodes one of the "PQ" Objective-C
225 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
226 'const', instead, is encoded directly as part of the type. */
227 static void
228 encode_type_qualifiers (tree declspecs)
230 tree spec;
232 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
234 /* FIXME: Shouldn't we use token->keyword here ? */
235 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
236 obstack_1grow (&util_obstack, 'n');
237 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
238 obstack_1grow (&util_obstack, 'N');
239 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
240 obstack_1grow (&util_obstack, 'o');
241 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
242 obstack_1grow (&util_obstack, 'O');
243 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
244 obstack_1grow (&util_obstack, 'R');
245 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
246 obstack_1grow (&util_obstack, 'V');
247 else
248 gcc_unreachable ();
252 /* Determine if a pointee is marked read-only. Only used by the NeXT
253 runtime to be compatible with gcc-3.3. */
254 static bool
255 pointee_is_readonly (tree pointee)
257 while (POINTER_TYPE_P (pointee))
258 pointee = TREE_TYPE (pointee);
260 return TYPE_READONLY (pointee);
263 /* Encode a pointer type. */
264 static void
265 encode_pointer (tree type, int curtype, int format)
267 tree pointer_to = TREE_TYPE (type);
269 if (flag_next_runtime)
271 /* This code is used to be compatible with gcc-3.3. */
272 /* For historical/compatibility reasons, the read-only qualifier
273 of the pointee gets emitted _before_ the '^'. The read-only
274 qualifier of the pointer itself gets ignored, _unless_ we are
275 looking at a typedef! Also, do not emit the 'r' for anything
276 but the outermost type! */
277 if (!generating_instance_variables
278 && (obstack_object_size (&util_obstack) - curtype <= 1)
279 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
280 ? TYPE_READONLY (type)
281 : pointee_is_readonly (pointer_to)))
282 obstack_1grow (&util_obstack, 'r');
285 if (TREE_CODE (pointer_to) == RECORD_TYPE)
287 if (OBJC_TYPE_NAME (pointer_to)
288 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
290 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
292 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
294 obstack_1grow (&util_obstack, '@');
295 return;
297 else if (TYPE_HAS_OBJC_INFO (pointer_to)
298 && TYPE_OBJC_INTERFACE (pointer_to))
300 if (generating_instance_variables)
302 obstack_1grow (&util_obstack, '@');
303 obstack_1grow (&util_obstack, '"');
304 obstack_grow (&util_obstack, name, strlen (name));
305 obstack_1grow (&util_obstack, '"');
306 return;
308 else
310 obstack_1grow (&util_obstack, '@');
311 return;
314 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
316 obstack_1grow (&util_obstack, '#');
317 return;
319 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
321 obstack_1grow (&util_obstack, ':');
322 return;
326 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
327 && TYPE_MODE (pointer_to) == QImode)
329 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
330 ? OBJC_TYPE_NAME (pointer_to)
331 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
333 /* (BOOL *) are an exception and are encoded as ^c, while all
334 other pointers to char are encoded as *. */
335 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
337 if (!flag_next_runtime)
339 /* The NeXT runtime adds the 'r' before getting here. */
341 /* It appears that "r*" means "const char *" rather than
342 "char *const". "char *const" is encoded as "*",
343 which is identical to "char *", so the "const" is
344 unfortunately lost. */
345 if (TYPE_READONLY (pointer_to))
346 obstack_1grow (&util_obstack, 'r');
349 obstack_1grow (&util_obstack, '*');
350 return;
354 /* We have a normal pointer type that does not get special treatment. */
355 obstack_1grow (&util_obstack, '^');
356 encode_type (pointer_to, curtype, format);
359 static void
360 encode_array (tree type, int curtype, int format)
362 tree an_int_cst = TYPE_SIZE (type);
363 tree array_of = TREE_TYPE (type);
364 char buffer[40];
366 if (an_int_cst == NULL)
368 /* We are trying to encode an incomplete array. An incomplete
369 array is forbidden as part of an instance variable; but it
370 may occur if the instance variable is a pointer to such an
371 array. */
373 /* So the only case in which an incomplete array could occur
374 (without being pointed to) is if we are encoding the
375 arguments or return value of a method. In that case, an
376 incomplete array argument or return value (eg,
377 -(void)display: (char[])string) is treated like a pointer
378 because that is how the compiler does the function call. A
379 special, more complicated case, is when the incomplete array
380 is the last member of a struct (eg, if we are encoding
381 "struct { unsigned long int a;double b[];}"), which is again
382 part of a method argument/return value. In that case, we
383 really need to communicate to the runtime that there is an
384 incomplete array (not a pointer!) there. So, we detect that
385 special case and encode it as a zero-length array.
387 Try to detect that we are part of a struct. We do this by
388 searching for '=' in the type encoding for the current type.
389 NB: This hack assumes that you can't use '=' as part of a C
390 identifier.
393 char *enc = (char *) obstack_base (&util_obstack) + curtype;
394 if (memchr (enc, '=',
395 obstack_object_size (&util_obstack) - curtype) == NULL)
397 /* We are not inside a struct. Encode the array as a
398 pointer. */
399 encode_pointer (type, curtype, format);
400 return;
404 /* Else, we are in a struct, and we encode it as a zero-length
405 array. */
406 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
408 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
409 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
410 else
411 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
412 TREE_INT_CST_LOW (an_int_cst)
413 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
415 obstack_grow (&util_obstack, buffer, strlen (buffer));
416 encode_type (array_of, curtype, format);
417 obstack_1grow (&util_obstack, ']');
418 return;
421 /* Encode a vector. The vector type is a GCC extension to C. */
422 static void
423 encode_vector (tree type, int curtype, int format)
425 tree vector_of = TREE_TYPE (type);
426 char buffer[40];
428 /* Vectors are like simple fixed-size arrays. */
430 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
431 alignment of the vector, and <code> is the base type. Eg, int
432 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
433 assuming that the alignment is 32 bytes. We include size and
434 alignment in bytes so that the runtime does not have to have any
435 knowledge of the actual types.
437 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
438 /* We want to compute the equivalent of sizeof (<vector>).
439 Code inspired by c_sizeof_or_alignof_type. */
440 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
441 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
442 /* We want to compute the equivalent of __alignof__
443 (<vector>). Code inspired by
444 c_sizeof_or_alignof_type. */
445 TYPE_ALIGN_UNIT (type));
446 obstack_grow (&util_obstack, buffer, strlen (buffer));
447 encode_type (vector_of, curtype, format);
448 obstack_1grow (&util_obstack, ']');
449 return;
452 static void
453 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
455 tree field = TYPE_FIELDS (type);
457 for (; field; field = DECL_CHAIN (field))
459 #ifdef OBJCPLUS
460 /* C++ static members, and things that are not field at all,
461 should not appear in the encoding. */
462 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
463 continue;
464 #endif
466 /* Recursively encode fields of embedded base classes. */
467 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
468 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
470 encode_aggregate_fields (TREE_TYPE (field),
471 pointed_to, curtype, format);
472 continue;
475 if (generating_instance_variables && !pointed_to)
477 tree fname = DECL_NAME (field);
479 obstack_1grow (&util_obstack, '"');
481 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
482 obstack_grow (&util_obstack,
483 IDENTIFIER_POINTER (fname),
484 strlen (IDENTIFIER_POINTER (fname)));
486 obstack_1grow (&util_obstack, '"');
489 encode_field (field, curtype, format);
493 static void
494 encode_aggregate_within (tree type, int curtype, int format, int left,
495 int right)
497 tree name;
498 /* NB: aggregates that are pointed to have slightly different encoding
499 rules in that you never encode the names of instance variables. */
500 int ob_size = obstack_object_size (&util_obstack);
501 bool inline_contents = false;
502 bool pointed_to = false;
504 if (flag_next_runtime)
506 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
507 pointed_to = true;
509 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
510 && (!pointed_to || ob_size - curtype == 1
511 || (ob_size - curtype == 2
512 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
513 inline_contents = true;
515 else
517 /* c0 and c1 are the last two characters in the encoding of the
518 current type; if the last two characters were '^' or '^r',
519 then we are encoding an aggregate that is "pointed to". The
520 comment above applies: in that case we should avoid encoding
521 the names of instance variables.
523 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
524 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
526 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
527 pointed_to = true;
529 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
531 if (!pointed_to)
532 inline_contents = true;
533 else
535 /* Note that the check (ob_size - curtype < 2) prevents
536 infinite recursion when encoding a structure which is
537 a linked list (eg, struct node { struct node *next;
538 }). Each time we follow a pointer, we add one
539 character to ob_size, and curtype is fixed, so after
540 at most two pointers we stop inlining contents and
541 break the loop.
543 The other case where we don't inline is "^r", which
544 is a pointer to a constant struct.
546 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
547 inline_contents = true;
552 /* Traverse struct aliases; it is important to get the
553 original struct and its tag name (if any). */
554 type = TYPE_MAIN_VARIANT (type);
555 name = OBJC_TYPE_NAME (type);
556 /* Open parenth/bracket. */
557 obstack_1grow (&util_obstack, left);
559 /* Encode the struct/union tag name, or '?' if a tag was
560 not provided. Typedef aliases do not qualify. */
561 #ifdef OBJCPLUS
562 /* For compatibility with the NeXT runtime, ObjC++ encodes template
563 args as a composite struct tag name. */
564 if (name && TREE_CODE (name) == IDENTIFIER_NODE
565 /* Did this struct have a tag? */
566 && !TYPE_WAS_ANONYMOUS (type))
567 obstack_grow (&util_obstack,
568 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
569 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
570 #else
571 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
572 obstack_grow (&util_obstack,
573 IDENTIFIER_POINTER (name),
574 strlen (IDENTIFIER_POINTER (name)));
575 #endif
576 else
577 obstack_1grow (&util_obstack, '?');
579 /* Encode the types (and possibly names) of the inner fields,
580 if required. */
581 if (inline_contents)
583 obstack_1grow (&util_obstack, '=');
584 encode_aggregate_fields (type, pointed_to, curtype, format);
586 /* Close parenth/bracket. */
587 obstack_1grow (&util_obstack, right);
590 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
591 field type. */
592 static void
593 encode_next_bitfield (int width)
595 char buffer[40];
596 sprintf (buffer, "b%d", width);
597 obstack_grow (&util_obstack, buffer, strlen (buffer));
600 /* Encodes 'type', ignoring type qualifiers (which you should encode
601 beforehand if needed) with the exception of 'const', which is
602 encoded by encode_type. See above for the explanation of
603 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
604 OBJC_ENCODE_DONT_INLINE_DEFS. */
605 static void
606 encode_type (tree type, int curtype, int format)
608 enum tree_code code = TREE_CODE (type);
610 /* Ignore type qualifiers other than 'const' when encoding a
611 type. */
613 if (type == error_mark_node)
614 return;
616 if (!flag_next_runtime)
618 if (TYPE_READONLY (type))
619 obstack_1grow (&util_obstack, 'r');
622 switch (code)
624 case ENUMERAL_TYPE:
625 if (flag_next_runtime)
627 /* Kludge for backwards-compatibility with gcc-3.3: enums
628 are always encoded as 'i' no matter what type they
629 actually are (!). */
630 obstack_1grow (&util_obstack, 'i');
631 break;
633 /* Else, they are encoded exactly like the integer type that is
634 used by the compiler to store them. */
635 case INTEGER_TYPE:
637 char c;
638 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
640 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
641 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
642 case 32:
644 tree int_type = type;
645 if (flag_next_runtime)
647 /* Another legacy kludge for compatibility with
648 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
649 but not always. For typedefs, we need to use 'i'
650 or 'I' instead if encoding a struct field, or a
651 pointer! */
652 int_type = ((!generating_instance_variables
653 && (obstack_object_size (&util_obstack)
654 == (unsigned) curtype))
655 ? TYPE_MAIN_VARIANT (type)
656 : type);
658 if (int_type == long_unsigned_type_node
659 || int_type == long_integer_type_node)
660 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
661 else
662 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
664 break;
665 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
666 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
667 default: gcc_unreachable ();
669 obstack_1grow (&util_obstack, c);
670 break;
672 case REAL_TYPE:
674 char c;
675 /* Floating point types. */
676 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
678 case 32: c = 'f'; break;
679 case 64: c = 'd'; break;
680 case 96:
681 case 128: c = 'D'; break;
682 default: gcc_unreachable ();
684 obstack_1grow (&util_obstack, c);
685 break;
687 case VOID_TYPE:
688 obstack_1grow (&util_obstack, 'v');
689 break;
691 case BOOLEAN_TYPE:
692 obstack_1grow (&util_obstack, 'B');
693 break;
695 case ARRAY_TYPE:
696 encode_array (type, curtype, format);
697 break;
699 case POINTER_TYPE:
700 #ifdef OBJCPLUS
701 case REFERENCE_TYPE:
702 #endif
703 encode_pointer (type, curtype, format);
704 break;
706 case RECORD_TYPE:
707 encode_aggregate_within (type, curtype, format, '{', '}');
708 break;
710 case UNION_TYPE:
711 encode_aggregate_within (type, curtype, format, '(', ')');
712 break;
714 case FUNCTION_TYPE: /* '?' means an unknown type. */
715 obstack_1grow (&util_obstack, '?');
716 break;
718 case COMPLEX_TYPE:
719 /* A complex is encoded as 'j' followed by the inner type (eg,
720 "_Complex int" is encoded as 'ji'). */
721 obstack_1grow (&util_obstack, 'j');
722 encode_type (TREE_TYPE (type), curtype, format);
723 break;
725 case VECTOR_TYPE:
726 encode_vector (type, curtype, format);
727 break;
729 default:
730 warning (0, "unknown type %<%T%> found during Objective-C encoding",
731 TREE_TYPE (type));
732 obstack_1grow (&util_obstack, '?');
733 break;
736 if (flag_next_runtime)
738 /* Super-kludge. Some ObjC qualifier and type combinations need
739 to be rearranged for compatibility with gcc-3.3. */
740 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
742 char *enc = (char *) obstack_base (&util_obstack) + curtype;
744 /* Rewrite "in const" from "nr" to "rn". */
745 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
746 strncpy (enc - 1, "rn", 2);
751 static void
752 encode_gnu_bitfield (int position, tree type, int size)
754 enum tree_code code = TREE_CODE (type);
755 char buffer[40];
756 char charType = '?';
758 /* This code is only executed for the GNU runtime, so we can ignore
759 the NeXT runtime kludge of always encoding enums as 'i' no matter
760 what integers they actually are. */
761 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
763 if (integer_zerop (TYPE_MIN_VALUE (type)))
764 /* Unsigned integer types. */
766 switch (TYPE_MODE (type))
768 case QImode:
769 charType = 'C'; break;
770 case HImode:
771 charType = 'S'; break;
772 case SImode:
774 if (type == long_unsigned_type_node)
775 charType = 'L';
776 else
777 charType = 'I';
778 break;
780 case DImode:
781 charType = 'Q'; break;
782 default:
783 gcc_unreachable ();
786 else
787 /* Signed integer types. */
789 switch (TYPE_MODE (type))
791 case QImode:
792 charType = 'c'; break;
793 case HImode:
794 charType = 's'; break;
795 case SImode:
797 if (type == long_integer_type_node)
798 charType = 'l';
799 else
800 charType = 'i';
801 break;
803 case DImode:
804 charType = 'q'; break;
805 default:
806 gcc_unreachable ();
810 else
812 /* Do not do any encoding, produce an error and keep going. */
813 error ("trying to encode non-integer type as a bitfield");
814 return;
817 sprintf (buffer, "b%d%c%d", position, charType, size);
818 obstack_grow (&util_obstack, buffer, strlen (buffer));
821 static void
822 encode_field (tree field_decl, int curtype, int format)
824 #ifdef OBJCPLUS
825 /* C++ static members, and things that are not fields at all,
826 should not appear in the encoding. */
827 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
828 return;
829 #endif
831 /* Generate the bitfield typing information, if needed. Note the difference
832 between GNU and NeXT runtimes. */
833 if (DECL_BIT_FIELD_TYPE (field_decl))
835 int size = tree_to_uhwi (DECL_SIZE (field_decl));
837 if (flag_next_runtime)
838 encode_next_bitfield (size);
839 else
840 encode_gnu_bitfield (int_bit_position (field_decl),
841 DECL_BIT_FIELD_TYPE (field_decl), size);
843 else
844 encode_type (TREE_TYPE (field_decl), curtype, format);
847 tree
848 encode_field_decl (tree field_decl)
850 tree result;
852 encode_field (field_decl,
853 obstack_object_size (&util_obstack),
854 OBJC_ENCODE_DONT_INLINE_DEFS);
856 /* Null terminate string. */
857 obstack_1grow (&util_obstack, 0);
859 /* Get identifier for the string. */
860 result = get_identifier (XOBFINISH (&util_obstack, char *));
861 obstack_free (&util_obstack, util_firstobj);
863 return result;
866 /* This routine encodes the attribute of the input PROPERTY according
867 to following formula:
869 Property attributes are stored as a comma-delimited C string.
870 Simple attributes such as readonly are encoded as single
871 character. The parametrized attributes, getter=name and
872 setter=name, are encoded as a single character followed by an
873 identifier. Property types are also encoded as a parametrized
874 attribute. The characters used to encode these attributes are
875 defined by the following enumeration:
877 enum PropertyAttributes {
878 kPropertyReadOnly = 'R',
879 kPropertyBycopy = 'C',
880 kPropertyByref = '&',
881 kPropertyDynamic = 'D',
882 kPropertyGetter = 'G',
883 kPropertySetter = 'S',
884 kPropertyInstanceVariable = 'V',
885 kPropertyType = 'T',
886 kPropertyWeak = 'W',
887 kPropertyStrong = 'P',
888 kPropertyNonAtomic = 'N'
889 }; */
890 tree
891 objc_v2_encode_prop_attr (tree property)
893 const char *string;
894 tree type = TREE_TYPE (property);
896 obstack_1grow (&util_obstack, 'T');
897 encode_type (type, obstack_object_size (&util_obstack),
898 OBJC_ENCODE_INLINE_DEFS);
900 if (PROPERTY_READONLY (property))
901 obstack_grow (&util_obstack, ",R", 2);
903 switch (PROPERTY_ASSIGN_SEMANTICS (property))
905 case OBJC_PROPERTY_COPY:
906 obstack_grow (&util_obstack, ",C", 2);
907 break;
908 case OBJC_PROPERTY_RETAIN:
909 obstack_grow (&util_obstack, ",&", 2);
910 break;
911 case OBJC_PROPERTY_ASSIGN:
912 default:
913 break;
916 if (PROPERTY_DYNAMIC (property))
917 obstack_grow (&util_obstack, ",D", 2);
919 if (PROPERTY_NONATOMIC (property))
920 obstack_grow (&util_obstack, ",N", 2);
922 /* Here we want to encode the getter name, but only if it's not the
923 standard one. */
924 if (PROPERTY_GETTER_NAME (property) != PROPERTY_NAME (property))
926 obstack_grow (&util_obstack, ",G", 2);
927 string = IDENTIFIER_POINTER (PROPERTY_GETTER_NAME (property));
928 obstack_grow (&util_obstack, string, strlen (string));
931 if (!PROPERTY_READONLY (property))
933 /* Here we want to encode the setter name, but only if it's not
934 the standard one. */
935 tree standard_setter = get_identifier (objc_build_property_setter_name (PROPERTY_NAME (property)));
936 if (PROPERTY_SETTER_NAME (property) != standard_setter)
938 obstack_grow (&util_obstack, ",S", 2);
939 string = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (property));
940 obstack_grow (&util_obstack, string, strlen (string));
944 /* TODO: Encode strong ('P'), weak ('W') for garbage collection. */
946 if (!PROPERTY_DYNAMIC (property))
948 obstack_grow (&util_obstack, ",V", 2);
949 if (PROPERTY_IVAR_NAME (property))
950 string = IDENTIFIER_POINTER (PROPERTY_IVAR_NAME (property));
951 else
952 string = IDENTIFIER_POINTER (PROPERTY_NAME (property));
953 obstack_grow (&util_obstack, string, strlen (string));
956 /* NULL-terminate string. */
957 obstack_1grow (&util_obstack, 0);
958 string = XOBFINISH (&util_obstack, char *);
959 obstack_free (&util_obstack, util_firstobj);
960 return get_identifier (string);