* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / config / darwin-c.c
blobe9232c0aaf6801dcf6722e3b866552b9faef91f9
1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "target.h"
30 #include "incpath.h"
31 #include "c-family/c-common.h"
32 #include "c-family/c-pragma.h"
33 #include "c-family/c-format.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "tm_p.h"
37 #include "cppdefault.h"
38 #include "prefix.h"
39 #include "c-family/c-target.h"
40 #include "c-family/c-target-def.h"
41 #include "predict.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfgrtl.h"
45 #include "cfganal.h"
46 #include "lcm.h"
47 #include "cfgbuild.h"
48 #include "cfgcleanup.h"
49 #include "basic-block.h"
50 #include "hard-reg-set.h"
51 #include "function.h"
52 #include "cgraph.h"
53 #include "../../libcpp/internal.h"
55 /* Pragmas. */
57 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
58 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
60 static bool using_frameworks = false;
62 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
63 cpp_dir **dirp);
65 typedef struct align_stack
67 int alignment;
68 struct align_stack * prev;
69 } align_stack;
71 static struct align_stack * field_align_stack = NULL;
73 /* Maintain a small stack of alignments. This is similar to pragma
74 pack's stack, but simpler. */
76 static void
77 push_field_alignment (int bit_alignment)
79 align_stack *entry = XNEW (align_stack);
81 entry->alignment = maximum_field_alignment;
82 entry->prev = field_align_stack;
83 field_align_stack = entry;
85 maximum_field_alignment = bit_alignment;
88 static void
89 pop_field_alignment (void)
91 if (field_align_stack)
93 align_stack *entry = field_align_stack;
95 maximum_field_alignment = entry->alignment;
96 field_align_stack = entry->prev;
97 free (entry);
99 else
100 error ("too many #pragma options align=reset");
103 /* Handlers for Darwin-specific pragmas. */
105 void
106 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
108 /* Do nothing. */
111 /* #pragma options align={mac68k|power|reset} */
113 void
114 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
116 const char *arg;
117 tree t, x;
119 if (pragma_lex (&t) != CPP_NAME)
120 BAD ("malformed '#pragma options', ignoring");
121 arg = IDENTIFIER_POINTER (t);
122 if (strcmp (arg, "align"))
123 BAD ("malformed '#pragma options', ignoring");
124 if (pragma_lex (&t) != CPP_EQ)
125 BAD ("malformed '#pragma options', ignoring");
126 if (pragma_lex (&t) != CPP_NAME)
127 BAD ("malformed '#pragma options', ignoring");
129 if (pragma_lex (&x) != CPP_EOF)
130 warning (OPT_Wpragmas, "junk at end of '#pragma options'");
132 arg = IDENTIFIER_POINTER (t);
133 if (!strcmp (arg, "mac68k"))
134 push_field_alignment (16);
135 else if (!strcmp (arg, "power"))
136 push_field_alignment (0);
137 else if (!strcmp (arg, "reset"))
138 pop_field_alignment ();
139 else
140 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
143 /* #pragma unused ([var {, var}*]) */
145 void
146 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
148 tree decl, x;
149 int tok;
151 if (pragma_lex (&x) != CPP_OPEN_PAREN)
152 BAD ("missing '(' after '#pragma unused', ignoring");
154 while (1)
156 tok = pragma_lex (&decl);
157 if (tok == CPP_NAME && decl)
159 tree local = lookup_name (decl);
160 if (local && (TREE_CODE (local) == PARM_DECL
161 || TREE_CODE (local) == VAR_DECL))
163 TREE_USED (local) = 1;
164 DECL_READ_P (local) = 1;
166 tok = pragma_lex (&x);
167 if (tok != CPP_COMMA)
168 break;
172 if (tok != CPP_CLOSE_PAREN)
173 BAD ("missing ')' after '#pragma unused', ignoring");
175 if (pragma_lex (&x) != CPP_EOF)
176 BAD ("junk at end of '#pragma unused'");
179 /* Parse the ms_struct pragma. */
180 void
181 darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
183 const char *arg;
184 tree t;
186 if (pragma_lex (&t) != CPP_NAME)
187 BAD ("malformed '#pragma ms_struct', ignoring");
188 arg = IDENTIFIER_POINTER (t);
190 if (!strcmp (arg, "on"))
191 darwin_ms_struct = true;
192 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
193 darwin_ms_struct = false;
194 else
195 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
197 if (pragma_lex (&t) != CPP_EOF)
198 BAD ("junk at end of '#pragma ms_struct'");
201 static struct frameworks_in_use {
202 size_t len;
203 const char *name;
204 cpp_dir* dir;
205 } *frameworks_in_use;
206 static int num_frameworks = 0;
207 static int max_frameworks = 0;
210 /* Remember which frameworks have been seen, so that we can ensure
211 that all uses of that framework come from the same framework. DIR
212 is the place where the named framework NAME, which is of length
213 LEN, was found. We copy the directory name from NAME, as it will be
214 freed by others. */
216 static void
217 add_framework (const char *name, size_t len, cpp_dir *dir)
219 char *dir_name;
220 int i;
221 for (i = 0; i < num_frameworks; ++i)
223 if (len == frameworks_in_use[i].len
224 && strncmp (name, frameworks_in_use[i].name, len) == 0)
226 return;
229 if (i >= max_frameworks)
231 max_frameworks = i*2;
232 max_frameworks += i == 0;
233 frameworks_in_use = XRESIZEVEC (struct frameworks_in_use,
234 frameworks_in_use, max_frameworks);
236 dir_name = XNEWVEC (char, len + 1);
237 memcpy (dir_name, name, len);
238 dir_name[len] = '\0';
239 frameworks_in_use[num_frameworks].name = dir_name;
240 frameworks_in_use[num_frameworks].len = len;
241 frameworks_in_use[num_frameworks].dir = dir;
242 ++num_frameworks;
245 /* Recall if we have seen the named framework NAME, before, and where
246 we saw it. NAME is LEN bytes long. The return value is the place
247 where it was seen before. */
249 static struct cpp_dir*
250 find_framework (const char *name, size_t len)
252 int i;
253 for (i = 0; i < num_frameworks; ++i)
255 if (len == frameworks_in_use[i].len
256 && strncmp (name, frameworks_in_use[i].name, len) == 0)
258 return frameworks_in_use[i].dir;
261 return 0;
264 /* There are two directories in a framework that contain header files,
265 Headers and PrivateHeaders. We search Headers first as it is more
266 common to upgrade a header from PrivateHeaders to Headers and when
267 that is done, the old one might hang around and be out of data,
268 causing grief. */
270 struct framework_header {const char * dirName; int dirNameLen; };
271 static struct framework_header framework_header_dirs[] = {
272 { "Headers", 7 },
273 { "PrivateHeaders", 14 },
274 { NULL, 0 }
277 /* Returns a pointer to a malloced string that contains the real pathname
278 to the file, given the base name and the name. */
280 static char *
281 framework_construct_pathname (const char *fname, cpp_dir *dir)
283 const char *buf;
284 size_t fname_len, frname_len;
285 cpp_dir *fast_dir;
286 char *frname;
287 struct stat st;
288 int i;
290 /* Framework names must have a / in them. */
291 buf = strchr (fname, '/');
292 if (buf)
293 fname_len = buf - fname;
294 else
295 return 0;
297 fast_dir = find_framework (fname, fname_len);
299 /* Framework includes must all come from one framework. */
300 if (fast_dir && dir != fast_dir)
301 return 0;
303 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
304 + strlen(".framework/") + strlen("PrivateHeaders"));
305 strncpy (&frname[0], dir->name, dir->len);
306 frname_len = dir->len;
307 if (frname_len && frname[frname_len-1] != '/')
308 frname[frname_len++] = '/';
309 strncpy (&frname[frname_len], fname, fname_len);
310 frname_len += fname_len;
311 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
312 frname_len += strlen (".framework/");
314 if (fast_dir == 0)
316 frname[frname_len-1] = 0;
317 if (stat (frname, &st) == 0)
319 /* As soon as we find the first instance of the framework,
320 we stop and never use any later instance of that
321 framework. */
322 add_framework (fname, fname_len, dir);
324 else
326 /* If we can't find the parent directory, no point looking
327 further. */
328 free (frname);
329 return 0;
331 frname[frname_len-1] = '/';
334 /* Append framework_header_dirs and header file name */
335 for (i = 0; framework_header_dirs[i].dirName; i++)
337 strncpy (&frname[frname_len],
338 framework_header_dirs[i].dirName,
339 framework_header_dirs[i].dirNameLen);
340 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
341 &fname[fname_len]);
343 if (stat (frname, &st) == 0)
344 return frname;
347 free (frname);
348 return 0;
351 /* Search for FNAME in sub-frameworks. pname is the context that we
352 wish to search in. Return the path the file was found at,
353 otherwise return 0. */
355 static const char*
356 find_subframework_file (const char *fname, const char *pname)
358 char *sfrname;
359 const char *dot_framework = ".framework/";
360 const char *bufptr;
361 int sfrname_len, i, fname_len;
362 struct cpp_dir *fast_dir;
363 static struct cpp_dir subframe_dir;
364 struct stat st;
366 bufptr = strchr (fname, '/');
368 /* Subframework files must have / in the name. */
369 if (bufptr == 0)
370 return 0;
372 fname_len = bufptr - fname;
373 fast_dir = find_framework (fname, fname_len);
375 /* Sub framework header filename includes parent framework name and
376 header name in the "CarbonCore/OSUtils.h" form. If it does not
377 include slash it is not a sub framework include. */
378 bufptr = strstr (pname, dot_framework);
380 /* If the parent header is not of any framework, then this header
381 cannot be part of any subframework. */
382 if (!bufptr)
383 return 0;
385 /* Now translate. For example, +- bufptr
386 fname = CarbonCore/OSUtils.h |
387 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
388 into
389 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
391 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
392 strlen ("Frameworks/") + strlen (".framework/")
393 + strlen ("PrivateHeaders"));
395 bufptr += strlen (dot_framework);
397 sfrname_len = bufptr - pname;
399 strncpy (&sfrname[0], pname, sfrname_len);
401 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
402 sfrname_len += strlen("Frameworks/");
404 strncpy (&sfrname[sfrname_len], fname, fname_len);
405 sfrname_len += fname_len;
407 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
408 sfrname_len += strlen (".framework/");
410 /* Append framework_header_dirs and header file name */
411 for (i = 0; framework_header_dirs[i].dirName; i++)
413 strncpy (&sfrname[sfrname_len],
414 framework_header_dirs[i].dirName,
415 framework_header_dirs[i].dirNameLen);
416 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
417 &fname[fname_len]);
419 if (stat (sfrname, &st) == 0)
421 if (fast_dir != &subframe_dir)
423 if (fast_dir)
424 warning (0, "subframework include %s conflicts with framework include",
425 fname);
426 else
427 add_framework (fname, fname_len, &subframe_dir);
430 return sfrname;
433 free (sfrname);
435 return 0;
438 /* Add PATH to the system includes. PATH must be malloc-ed and
439 NUL-terminated. System framework paths are C++ aware. */
441 static void
442 add_system_framework_path (char *path)
444 int cxx_aware = 1;
445 cpp_dir *p;
447 p = XNEW (cpp_dir);
448 p->next = NULL;
449 p->name = path;
450 p->sysp = 1 + !cxx_aware;
451 p->construct = framework_construct_pathname;
452 using_frameworks = 1;
454 add_cpp_dir_path (p, SYSTEM);
457 /* Add PATH to the bracket includes. PATH must be malloc-ed and
458 NUL-terminated. */
460 void
461 add_framework_path (char *path)
463 cpp_dir *p;
465 p = XNEW (cpp_dir);
466 p->next = NULL;
467 p->name = path;
468 p->sysp = 0;
469 p->construct = framework_construct_pathname;
470 using_frameworks = 1;
472 add_cpp_dir_path (p, BRACKET);
475 static const char *framework_defaults [] =
477 "/System/Library/Frameworks",
478 "/Library/Frameworks",
481 /* Register the GNU objective-C runtime include path if STDINC. */
483 void
484 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
485 int stdinc)
487 const char *fname;
488 size_t len;
489 /* We do not do anything if we do not want the standard includes. */
490 if (!stdinc)
491 return;
493 fname = GCC_INCLUDE_DIR "-gnu-runtime";
495 /* Register the GNU OBJC runtime include path if we are compiling OBJC
496 with GNU-runtime. */
498 if (c_dialect_objc () && !flag_next_runtime)
500 char *str;
501 /* See if our directory starts with the standard prefix.
502 "Translate" them, i.e. replace /usr/local/lib/gcc... with
503 IPREFIX and search them first. */
504 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
505 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
507 str = concat (iprefix, fname + len, NULL);
508 /* FIXME: wrap the headers for C++awareness. */
509 add_path (str, SYSTEM, /*c++aware=*/false, false);
512 /* Should this directory start with the sysroot? */
513 if (sysroot)
514 str = concat (sysroot, fname, NULL);
515 else
516 str = update_path (fname, "");
518 add_path (str, SYSTEM, /*c++aware=*/false, false);
523 /* Register all the system framework paths if STDINC is true and setup
524 the missing_header callback for subframework searching if any
525 frameworks had been registered. */
527 void
528 darwin_register_frameworks (const char *sysroot,
529 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
531 if (stdinc)
533 size_t i;
535 /* Setup default search path for frameworks. */
536 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
538 char *str;
539 if (sysroot)
540 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
541 else
542 str = xstrdup (framework_defaults[i]);
543 /* System Framework headers are cxx aware. */
544 add_system_framework_path (str);
548 if (using_frameworks)
549 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
552 /* Search for HEADER in context dependent way. The return value is
553 the malloced name of a header to try and open, if any, or NULL
554 otherwise. This is called after normal header lookup processing
555 fails to find a header. We search each file in the include stack,
556 using FUNC, starting from the most deeply nested include and
557 finishing with the main input file. We stop searching when FUNC
558 returns nonzero. */
560 static const char*
561 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
563 const char *fname = header;
564 struct cpp_buffer *b;
565 const char *n;
567 for (b = cpp_get_buffer (pfile);
568 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
569 b = cpp_get_prev (b))
571 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
572 if (n)
574 /* Logically, the place where we found the subframework is
575 the place where we found the Framework that contains the
576 subframework. This is useful for tracking wether or not
577 we are in a system header. */
578 *dirp = cpp_get_dir (cpp_get_file (b));
579 return n;
583 return 0;
586 /* Given an OS X version VERSION_STR, return it as a statically-allocated array
587 of three integers. If VERSION_STR is invalid, return NULL.
589 VERSION_STR must consist of one, two, or three tokens, each separated by
590 a single period. Each token must contain only the characters '0' through
591 '9' and is converted to an equivalent non-negative decimal integer. Omitted
592 tokens become zeros. For example:
594 "10" becomes {10,0,0}
595 "10.10" becomes {10,10,0}
596 "10.10.1" becomes {10,10,1}
597 "10.000010.1" becomes {10,10,1}
598 "10.010.001" becomes {10,10,1}
599 "000010.10.00001" becomes {10,10,1}
600 ".9.1" is invalid
601 "10..9" is invalid
602 "10.10." is invalid */
604 enum version_components { MAJOR, MINOR, TINY };
606 static const unsigned long *
607 parse_version (const char *version_str)
609 size_t version_len;
610 char *end;
611 static unsigned long version_array[3];
613 version_len = strlen (version_str);
614 if (version_len < 1)
615 return NULL;
617 /* Version string must consist of digits and periods only. */
618 if (strspn (version_str, "0123456789.") != version_len)
619 return NULL;
621 if (!ISDIGIT (version_str[0]) || !ISDIGIT (version_str[version_len - 1]))
622 return NULL;
624 version_array[MAJOR] = strtoul (version_str, &end, 10);
625 version_str = end + ((*end == '.') ? 1 : 0);
627 /* Version string must not contain adjacent periods. */
628 if (*version_str == '.')
629 return NULL;
631 version_array[MINOR] = strtoul (version_str, &end, 10);
632 version_str = end + ((*end == '.') ? 1 : 0);
634 version_array[TINY] = strtoul (version_str, &end, 10);
636 /* Version string must contain no more than three tokens. */
637 if (*end != '\0')
638 return NULL;
640 return version_array;
643 /* Given VERSION -- a three-component OS X version represented as an array of
644 non-negative integers -- return a statically-allocated string suitable for
645 the legacy __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro. If VERSION
646 is invalid and cannot be coerced into a valid form, return NULL.
648 The legacy format is a four-character string -- two chars for the major
649 number and one each for the minor and tiny numbers. Minor and tiny numbers
650 from 10 through 99 are permitted but are clamped to 9 (for example, {10,9,10}
651 produces "1099"). If VERSION contains numbers greater than 99, it is
652 rejected. */
654 static const char *
655 version_as_legacy_macro (const unsigned long *version)
657 unsigned long major, minor, tiny;
658 static char result[5];
660 major = version[MAJOR];
661 minor = version[MINOR];
662 tiny = version[TINY];
664 if (major > 99 || minor > 99 || tiny > 99)
665 return NULL;
667 minor = ((minor > 9) ? 9 : minor);
668 tiny = ((tiny > 9) ? 9 : tiny);
670 if (sprintf (result, "%lu%lu%lu", major, minor, tiny) != 4)
671 return NULL;
673 return result;
676 /* Given VERSION -- a three-component OS X version represented as an array of
677 non-negative integers -- return a statically-allocated string suitable for
678 the modern __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro. If VERSION
679 is invalid, return NULL.
681 The modern format is a six-character string -- two chars for each component,
682 with zero-padding if necessary (for example, {10,10,1} produces "101001"). If
683 VERSION contains numbers greater than 99, it is rejected. */
685 static const char *
686 version_as_modern_macro (const unsigned long *version)
688 unsigned long major, minor, tiny;
689 static char result[7];
691 major = version[MAJOR];
692 minor = version[MINOR];
693 tiny = version[TINY];
695 if (major > 99 || minor > 99 || tiny > 99)
696 return NULL;
698 if (sprintf (result, "%02lu%02lu%02lu", major, minor, tiny) != 6)
699 return NULL;
701 return result;
704 /* Return the value of darwin_macosx_version_min, suitably formatted for the
705 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro. Values representing
706 OS X 10.9 and earlier are encoded using the legacy four-character format,
707 while 10.10 and later use a modern six-character format. (For example,
708 "10.9" produces "1090", and "10.10.1" produces "101001".) If
709 darwin_macosx_version_min is invalid and cannot be coerced into a valid
710 form, print a warning and return "1000". */
712 static const char *
713 macosx_version_as_macro (void)
715 const unsigned long *version_array;
716 const char *version_macro;
718 version_array = parse_version (darwin_macosx_version_min);
719 if (!version_array)
720 goto fail;
722 if (version_array[MAJOR] != 10)
723 goto fail;
725 if (version_array[MINOR] < 10)
726 version_macro = version_as_legacy_macro (version_array);
727 else
728 version_macro = version_as_modern_macro (version_array);
730 if (!version_macro)
731 goto fail;
733 return version_macro;
735 fail:
736 error ("unknown value %qs of -mmacosx-version-min",
737 darwin_macosx_version_min);
738 return "1000";
741 /* Define additional CPP flags for Darwin. */
743 #define builtin_define(TXT) cpp_define (pfile, TXT)
745 void
746 darwin_cpp_builtins (cpp_reader *pfile)
748 builtin_define ("__MACH__");
749 builtin_define ("__APPLE__");
751 /* __APPLE_CC__ is defined as some old Apple include files expect it
752 to be defined and won't work if it isn't. */
753 builtin_define_with_value ("__APPLE_CC__", "1", false);
755 if (darwin_constant_cfstrings)
756 builtin_define ("__CONSTANT_CFSTRINGS__");
758 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
759 macosx_version_as_macro(), false);
761 /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
762 following will cause a syntax error if one tries to compile gc attributed
763 items. However, without this, NeXT system headers cannot be parsed
764 properly (on systems >= darwin 9). */
765 if (flag_objc_gc)
767 builtin_define ("__strong=__attribute__((objc_gc(strong)))");
768 builtin_define ("__weak=__attribute__((objc_gc(weak)))");
769 builtin_define ("__OBJC_GC__");
771 else
773 builtin_define ("__strong=");
774 builtin_define ("__weak=");
777 if (CPP_OPTION (pfile, objc) && flag_objc_abi == 2)
778 builtin_define ("__OBJC2__");
781 /* Handle C family front-end options. */
783 static bool
784 handle_c_option (size_t code,
785 const char *arg,
786 int value ATTRIBUTE_UNUSED)
788 switch (code)
790 default:
791 /* Unrecognized options that we said we'd handle turn into
792 errors if not listed here. */
793 return false;
795 case OPT_iframework:
796 add_system_framework_path (xstrdup (arg));
797 break;
799 case OPT_fapple_kext:
803 /* We recognized the option. */
804 return true;
807 /* Allow ObjC* access to CFStrings. */
808 static tree
809 darwin_objc_construct_string (tree str)
811 if (!darwin_constant_cfstrings)
813 /* Even though we are not using CFStrings, place our literal
814 into the cfstring_htab hash table, so that the
815 darwin_constant_cfstring_p() function will see it. */
816 darwin_enter_string_into_cfstring_table (str);
817 /* Fall back to NSConstantString. */
818 return NULL_TREE;
821 return darwin_build_constant_cfstring (str);
824 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
825 must match for it explicitly, since it's outside the gcc code. */
827 static bool
828 darwin_cfstring_ref_p (const_tree strp)
830 tree tn;
831 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
832 return false;
834 tn = TYPE_NAME (strp);
835 if (tn)
836 tn = DECL_NAME (tn);
837 return (tn
838 && IDENTIFIER_POINTER (tn)
839 && !strncmp (IDENTIFIER_POINTER (tn), "CFStringRef", 8));
842 /* At present the behavior of this is undefined and it does nothing. */
843 static void
844 darwin_check_cfstring_format_arg (tree ARG_UNUSED (format_arg),
845 tree ARG_UNUSED (args_list))
849 /* The extra format types we recognize. */
850 EXPORTED_CONST format_kind_info darwin_additional_format_types[] = {
851 { "CFString", NULL, NULL, NULL, NULL,
852 NULL, NULL,
853 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
854 NULL, NULL
859 /* Support routines to dump the class references for NeXT ABI v1, aka
860 32-bits ObjC-2.0, as top-level asms.
861 The following two functions should only be called from
862 objc/objc-next-runtime-abi-01.c. */
864 static void
865 darwin_objc_declare_unresolved_class_reference (const char *name)
867 const char *lazy_reference = ".lazy_reference\t";
868 const char *hard_reference = ".reference\t";
869 const char *reference = MACHOPIC_INDIRECT ? lazy_reference : hard_reference;
870 size_t len = strlen (reference) + strlen(name) + 2;
871 char *buf = (char *) alloca (len);
873 gcc_checking_assert (!strncmp (name, ".objc_class_name_", 17));
875 snprintf (buf, len, "%s%s", reference, name);
876 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
879 static void
880 darwin_objc_declare_class_definition (const char *name)
882 const char *xname = targetm.strip_name_encoding (name);
883 size_t len = strlen (xname) + 7 + 5;
884 char *buf = (char *) alloca (len);
886 gcc_checking_assert (!strncmp (name, ".objc_class_name_", 17)
887 || !strncmp (name, "*.objc_category_name_", 21));
889 /* Mimic default_globalize_label. */
890 snprintf (buf, len, ".globl\t%s", xname);
891 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
893 snprintf (buf, len, "%s = 0", xname);
894 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
897 #undef TARGET_HANDLE_C_OPTION
898 #define TARGET_HANDLE_C_OPTION handle_c_option
900 #undef TARGET_OBJC_CONSTRUCT_STRING_OBJECT
901 #define TARGET_OBJC_CONSTRUCT_STRING_OBJECT darwin_objc_construct_string
903 #undef TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE
904 #define TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE \
905 darwin_objc_declare_unresolved_class_reference
907 #undef TARGET_OBJC_DECLARE_CLASS_DEFINITION
908 #define TARGET_OBJC_DECLARE_CLASS_DEFINITION \
909 darwin_objc_declare_class_definition
911 #undef TARGET_STRING_OBJECT_REF_TYPE_P
912 #define TARGET_STRING_OBJECT_REF_TYPE_P darwin_cfstring_ref_p
914 #undef TARGET_CHECK_STRING_OBJECT_FORMAT_ARG
915 #define TARGET_CHECK_STRING_OBJECT_FORMAT_ARG darwin_check_cfstring_format_arg
917 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;