Restore 2012 entries that hasn't been saved.
[official-gcc.git] / gcc / config / darwin-c.c
blob08de2f8ef6c3f2d2a5bf80c0c4dc962d80ab12c7
1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001, 2003, 2004, 2005, 2007, 2008, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Apple Computer Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "incpath.h"
29 #include "c-family/c-common.h"
30 #include "c-family/c-pragma.h"
31 #include "c-family/c-format.h"
32 #include "diagnostic-core.h"
33 #include "flags.h"
34 #include "tm_p.h"
35 #include "cppdefault.h"
36 #include "prefix.h"
37 #include "c-family/c-target.h"
38 #include "c-family/c-target-def.h"
40 /* Pragmas. */
42 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
43 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
45 static bool using_frameworks = false;
47 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
48 cpp_dir **dirp);
50 typedef struct align_stack
52 int alignment;
53 struct align_stack * prev;
54 } align_stack;
56 static struct align_stack * field_align_stack = NULL;
58 /* Maintain a small stack of alignments. This is similar to pragma
59 pack's stack, but simpler. */
61 static void
62 push_field_alignment (int bit_alignment)
64 align_stack *entry = XNEW (align_stack);
66 entry->alignment = maximum_field_alignment;
67 entry->prev = field_align_stack;
68 field_align_stack = entry;
70 maximum_field_alignment = bit_alignment;
73 static void
74 pop_field_alignment (void)
76 if (field_align_stack)
78 align_stack *entry = field_align_stack;
80 maximum_field_alignment = entry->alignment;
81 field_align_stack = entry->prev;
82 free (entry);
84 else
85 error ("too many #pragma options align=reset");
88 /* Handlers for Darwin-specific pragmas. */
90 void
91 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
93 /* Do nothing. */
96 /* #pragma options align={mac68k|power|reset} */
98 void
99 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
101 const char *arg;
102 tree t, x;
104 if (pragma_lex (&t) != CPP_NAME)
105 BAD ("malformed '#pragma options', ignoring");
106 arg = IDENTIFIER_POINTER (t);
107 if (strcmp (arg, "align"))
108 BAD ("malformed '#pragma options', ignoring");
109 if (pragma_lex (&t) != CPP_EQ)
110 BAD ("malformed '#pragma options', ignoring");
111 if (pragma_lex (&t) != CPP_NAME)
112 BAD ("malformed '#pragma options', ignoring");
114 if (pragma_lex (&x) != CPP_EOF)
115 warning (OPT_Wpragmas, "junk at end of '#pragma options'");
117 arg = IDENTIFIER_POINTER (t);
118 if (!strcmp (arg, "mac68k"))
119 push_field_alignment (16);
120 else if (!strcmp (arg, "power"))
121 push_field_alignment (0);
122 else if (!strcmp (arg, "reset"))
123 pop_field_alignment ();
124 else
125 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
128 /* #pragma unused ([var {, var}*]) */
130 void
131 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
133 tree decl, x;
134 int tok;
136 if (pragma_lex (&x) != CPP_OPEN_PAREN)
137 BAD ("missing '(' after '#pragma unused', ignoring");
139 while (1)
141 tok = pragma_lex (&decl);
142 if (tok == CPP_NAME && decl)
144 tree local = lookup_name (decl);
145 if (local && (TREE_CODE (local) == PARM_DECL
146 || TREE_CODE (local) == VAR_DECL))
148 TREE_USED (local) = 1;
149 DECL_READ_P (local) = 1;
151 tok = pragma_lex (&x);
152 if (tok != CPP_COMMA)
153 break;
157 if (tok != CPP_CLOSE_PAREN)
158 BAD ("missing ')' after '#pragma unused', ignoring");
160 if (pragma_lex (&x) != CPP_EOF)
161 BAD ("junk at end of '#pragma unused'");
164 /* Parse the ms_struct pragma. */
165 void
166 darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
168 const char *arg;
169 tree t;
171 if (pragma_lex (&t) != CPP_NAME)
172 BAD ("malformed '#pragma ms_struct', ignoring");
173 arg = IDENTIFIER_POINTER (t);
175 if (!strcmp (arg, "on"))
176 darwin_ms_struct = true;
177 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
178 darwin_ms_struct = false;
179 else
180 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
182 if (pragma_lex (&t) != CPP_EOF)
183 BAD ("junk at end of '#pragma ms_struct'");
186 static struct frameworks_in_use {
187 size_t len;
188 const char *name;
189 cpp_dir* dir;
190 } *frameworks_in_use;
191 static int num_frameworks = 0;
192 static int max_frameworks = 0;
195 /* Remember which frameworks have been seen, so that we can ensure
196 that all uses of that framework come from the same framework. DIR
197 is the place where the named framework NAME, which is of length
198 LEN, was found. We copy the directory name from NAME, as it will be
199 freed by others. */
201 static void
202 add_framework (const char *name, size_t len, cpp_dir *dir)
204 char *dir_name;
205 int i;
206 for (i = 0; i < num_frameworks; ++i)
208 if (len == frameworks_in_use[i].len
209 && strncmp (name, frameworks_in_use[i].name, len) == 0)
211 return;
214 if (i >= max_frameworks)
216 max_frameworks = i*2;
217 max_frameworks += i == 0;
218 frameworks_in_use = XRESIZEVEC (struct frameworks_in_use,
219 frameworks_in_use, max_frameworks);
221 dir_name = XNEWVEC (char, len + 1);
222 memcpy (dir_name, name, len);
223 dir_name[len] = '\0';
224 frameworks_in_use[num_frameworks].name = dir_name;
225 frameworks_in_use[num_frameworks].len = len;
226 frameworks_in_use[num_frameworks].dir = dir;
227 ++num_frameworks;
230 /* Recall if we have seen the named framework NAME, before, and where
231 we saw it. NAME is LEN bytes long. The return value is the place
232 where it was seen before. */
234 static struct cpp_dir*
235 find_framework (const char *name, size_t len)
237 int i;
238 for (i = 0; i < num_frameworks; ++i)
240 if (len == frameworks_in_use[i].len
241 && strncmp (name, frameworks_in_use[i].name, len) == 0)
243 return frameworks_in_use[i].dir;
246 return 0;
249 /* There are two directories in a framework that contain header files,
250 Headers and PrivateHeaders. We search Headers first as it is more
251 common to upgrade a header from PrivateHeaders to Headers and when
252 that is done, the old one might hang around and be out of data,
253 causing grief. */
255 struct framework_header {const char * dirName; int dirNameLen; };
256 static struct framework_header framework_header_dirs[] = {
257 { "Headers", 7 },
258 { "PrivateHeaders", 14 },
259 { NULL, 0 }
262 /* Returns a pointer to a malloced string that contains the real pathname
263 to the file, given the base name and the name. */
265 static char *
266 framework_construct_pathname (const char *fname, cpp_dir *dir)
268 char *buf;
269 size_t fname_len, frname_len;
270 cpp_dir *fast_dir;
271 char *frname;
272 struct stat st;
273 int i;
275 /* Framework names must have a / in them. */
276 buf = strchr (fname, '/');
277 if (buf)
278 fname_len = buf - fname;
279 else
280 return 0;
282 fast_dir = find_framework (fname, fname_len);
284 /* Framework includes must all come from one framework. */
285 if (fast_dir && dir != fast_dir)
286 return 0;
288 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
289 + strlen(".framework/") + strlen("PrivateHeaders"));
290 strncpy (&frname[0], dir->name, dir->len);
291 frname_len = dir->len;
292 if (frname_len && frname[frname_len-1] != '/')
293 frname[frname_len++] = '/';
294 strncpy (&frname[frname_len], fname, fname_len);
295 frname_len += fname_len;
296 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
297 frname_len += strlen (".framework/");
299 if (fast_dir == 0)
301 frname[frname_len-1] = 0;
302 if (stat (frname, &st) == 0)
304 /* As soon as we find the first instance of the framework,
305 we stop and never use any later instance of that
306 framework. */
307 add_framework (fname, fname_len, dir);
309 else
311 /* If we can't find the parent directory, no point looking
312 further. */
313 free (frname);
314 return 0;
316 frname[frname_len-1] = '/';
319 /* Append framework_header_dirs and header file name */
320 for (i = 0; framework_header_dirs[i].dirName; i++)
322 strncpy (&frname[frname_len],
323 framework_header_dirs[i].dirName,
324 framework_header_dirs[i].dirNameLen);
325 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
326 &fname[fname_len]);
328 if (stat (frname, &st) == 0)
329 return frname;
332 free (frname);
333 return 0;
336 /* Search for FNAME in sub-frameworks. pname is the context that we
337 wish to search in. Return the path the file was found at,
338 otherwise return 0. */
340 static const char*
341 find_subframework_file (const char *fname, const char *pname)
343 char *sfrname;
344 const char *dot_framework = ".framework/";
345 char *bufptr;
346 int sfrname_len, i, fname_len;
347 struct cpp_dir *fast_dir;
348 static struct cpp_dir subframe_dir;
349 struct stat st;
351 bufptr = strchr (fname, '/');
353 /* Subframework files must have / in the name. */
354 if (bufptr == 0)
355 return 0;
357 fname_len = bufptr - fname;
358 fast_dir = find_framework (fname, fname_len);
360 /* Sub framework header filename includes parent framework name and
361 header name in the "CarbonCore/OSUtils.h" form. If it does not
362 include slash it is not a sub framework include. */
363 bufptr = strstr (pname, dot_framework);
365 /* If the parent header is not of any framework, then this header
366 cannot be part of any subframework. */
367 if (!bufptr)
368 return 0;
370 /* Now translate. For example, +- bufptr
371 fname = CarbonCore/OSUtils.h |
372 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
373 into
374 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
376 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
377 strlen ("Frameworks/") + strlen (".framework/")
378 + strlen ("PrivateHeaders"));
380 bufptr += strlen (dot_framework);
382 sfrname_len = bufptr - pname;
384 strncpy (&sfrname[0], pname, sfrname_len);
386 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
387 sfrname_len += strlen("Frameworks/");
389 strncpy (&sfrname[sfrname_len], fname, fname_len);
390 sfrname_len += fname_len;
392 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
393 sfrname_len += strlen (".framework/");
395 /* Append framework_header_dirs and header file name */
396 for (i = 0; framework_header_dirs[i].dirName; i++)
398 strncpy (&sfrname[sfrname_len],
399 framework_header_dirs[i].dirName,
400 framework_header_dirs[i].dirNameLen);
401 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
402 &fname[fname_len]);
404 if (stat (sfrname, &st) == 0)
406 if (fast_dir != &subframe_dir)
408 if (fast_dir)
409 warning (0, "subframework include %s conflicts with framework include",
410 fname);
411 else
412 add_framework (fname, fname_len, &subframe_dir);
415 return sfrname;
418 free (sfrname);
420 return 0;
423 /* Add PATH to the system includes. PATH must be malloc-ed and
424 NUL-terminated. System framework paths are C++ aware. */
426 static void
427 add_system_framework_path (char *path)
429 int cxx_aware = 1;
430 cpp_dir *p;
432 p = XNEW (cpp_dir);
433 p->next = NULL;
434 p->name = path;
435 p->sysp = 1 + !cxx_aware;
436 p->construct = framework_construct_pathname;
437 using_frameworks = 1;
439 add_cpp_dir_path (p, SYSTEM);
442 /* Add PATH to the bracket includes. PATH must be malloc-ed and
443 NUL-terminated. */
445 void
446 add_framework_path (char *path)
448 cpp_dir *p;
450 p = XNEW (cpp_dir);
451 p->next = NULL;
452 p->name = path;
453 p->sysp = 0;
454 p->construct = framework_construct_pathname;
455 using_frameworks = 1;
457 add_cpp_dir_path (p, BRACKET);
460 static const char *framework_defaults [] =
462 "/System/Library/Frameworks",
463 "/Library/Frameworks",
466 /* Register the GNU objective-C runtime include path if STDINC. */
468 void
469 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
470 int stdinc)
472 const char *fname;
473 size_t len;
474 /* We do not do anything if we do not want the standard includes. */
475 if (!stdinc)
476 return;
478 fname = GCC_INCLUDE_DIR "-gnu-runtime";
480 /* Register the GNU OBJC runtime include path if we are compiling OBJC
481 with GNU-runtime. */
483 if (c_dialect_objc () && !flag_next_runtime)
485 char *str;
486 /* See if our directory starts with the standard prefix.
487 "Translate" them, i.e. replace /usr/local/lib/gcc... with
488 IPREFIX and search them first. */
489 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
490 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
492 str = concat (iprefix, fname + len, NULL);
493 /* FIXME: wrap the headers for C++awareness. */
494 add_path (str, SYSTEM, /*c++aware=*/false, false);
497 /* Should this directory start with the sysroot? */
498 if (sysroot)
499 str = concat (sysroot, fname, NULL);
500 else
501 str = update_path (fname, "");
503 add_path (str, SYSTEM, /*c++aware=*/false, false);
508 /* Register all the system framework paths if STDINC is true and setup
509 the missing_header callback for subframework searching if any
510 frameworks had been registered. */
512 void
513 darwin_register_frameworks (const char *sysroot,
514 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
516 if (stdinc)
518 size_t i;
520 /* Setup default search path for frameworks. */
521 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
523 char *str;
524 if (sysroot)
525 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
526 else
527 str = xstrdup (framework_defaults[i]);
528 /* System Framework headers are cxx aware. */
529 add_system_framework_path (str);
533 if (using_frameworks)
534 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
537 /* Search for HEADER in context dependent way. The return value is
538 the malloced name of a header to try and open, if any, or NULL
539 otherwise. This is called after normal header lookup processing
540 fails to find a header. We search each file in the include stack,
541 using FUNC, starting from the most deeply nested include and
542 finishing with the main input file. We stop searching when FUNC
543 returns nonzero. */
545 static const char*
546 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
548 const char *fname = header;
549 struct cpp_buffer *b;
550 const char *n;
552 for (b = cpp_get_buffer (pfile);
553 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
554 b = cpp_get_prev (b))
556 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
557 if (n)
559 /* Logically, the place where we found the subframework is
560 the place where we found the Framework that contains the
561 subframework. This is useful for tracking wether or not
562 we are in a system header. */
563 *dirp = cpp_get_dir (cpp_get_file (b));
564 return n;
568 return 0;
571 /* Return the value of darwin_macosx_version_min suitable for the
572 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
573 so '10.4.2' becomes 1040. The lowest digit is always zero.
574 Print a warning if the version number can't be understood. */
575 static const char *
576 version_as_macro (void)
578 static char result[] = "1000";
580 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
581 goto fail;
582 if (! ISDIGIT (darwin_macosx_version_min[3]))
583 goto fail;
584 result[2] = darwin_macosx_version_min[3];
585 if (darwin_macosx_version_min[4] != '\0'
586 && darwin_macosx_version_min[4] != '.')
587 goto fail;
589 return result;
591 fail:
592 error ("unknown value %qs of -mmacosx-version-min",
593 darwin_macosx_version_min);
594 return "1000";
597 /* Define additional CPP flags for Darwin. */
599 #define builtin_define(TXT) cpp_define (pfile, TXT)
601 void
602 darwin_cpp_builtins (cpp_reader *pfile)
604 builtin_define ("__MACH__");
605 builtin_define ("__APPLE__");
607 /* __APPLE_CC__ is defined as some old Apple include files expect it
608 to be defined and won't work if it isn't. */
609 builtin_define_with_value ("__APPLE_CC__", "1", false);
611 if (darwin_constant_cfstrings)
612 builtin_define ("__CONSTANT_CFSTRINGS__");
614 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
615 version_as_macro(), false);
617 /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
618 following will cause a syntax error if one tries to compile gc attributed
619 items. However, without this, NeXT system headers cannot be parsed
620 properly (on systems >= darwin 9). */
621 if (flag_objc_gc)
623 builtin_define ("__strong=__attribute__((objc_gc(strong)))");
624 builtin_define ("__weak=__attribute__((objc_gc(weak)))");
625 builtin_define ("__OBJC_GC__");
627 else
629 builtin_define ("__strong=");
630 builtin_define ("__weak=");
633 if (flag_objc_abi == 2)
634 builtin_define ("__OBJC2__");
637 /* Handle C family front-end options. */
639 static bool
640 handle_c_option (size_t code,
641 const char *arg,
642 int value ATTRIBUTE_UNUSED)
644 switch (code)
646 default:
647 /* Unrecognized options that we said we'd handle turn into
648 errors if not listed here. */
649 return false;
651 case OPT_iframework:
652 add_system_framework_path (xstrdup (arg));
653 break;
655 case OPT_fapple_kext:
659 /* We recognized the option. */
660 return true;
663 /* Allow ObjC* access to CFStrings. */
664 static tree
665 darwin_objc_construct_string (tree str)
667 if (!darwin_constant_cfstrings)
669 /* Even though we are not using CFStrings, place our literal
670 into the cfstring_htab hash table, so that the
671 darwin_constant_cfstring_p() function will see it. */
672 darwin_enter_string_into_cfstring_table (str);
673 /* Fall back to NSConstantString. */
674 return NULL_TREE;
677 return darwin_build_constant_cfstring (str);
680 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
681 must match for it explicitly, since it's outside the gcc code. */
683 static bool
684 darwin_cfstring_ref_p (const_tree strp)
686 tree tn;
687 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
688 return false;
690 tn = TYPE_NAME (strp);
691 if (tn)
692 tn = DECL_NAME (tn);
693 return (tn
694 && IDENTIFIER_POINTER (tn)
695 && !strncmp (IDENTIFIER_POINTER (tn), "CFStringRef", 8));
698 /* At present the behavior of this is undefined and it does nothing. */
699 static void
700 darwin_check_cfstring_format_arg (tree ARG_UNUSED (format_arg),
701 tree ARG_UNUSED (args_list))
705 /* The extra format types we recognize. */
706 EXPORTED_CONST format_kind_info darwin_additional_format_types[] = {
707 { "CFString", NULL, NULL, NULL, NULL,
708 NULL, NULL,
709 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
710 NULL, NULL
714 #undef TARGET_HANDLE_C_OPTION
715 #define TARGET_HANDLE_C_OPTION handle_c_option
717 #undef TARGET_OBJC_CONSTRUCT_STRING_OBJECT
718 #define TARGET_OBJC_CONSTRUCT_STRING_OBJECT darwin_objc_construct_string
720 #undef TARGET_STRING_OBJECT_REF_TYPE_P
721 #define TARGET_STRING_OBJECT_REF_TYPE_P darwin_cfstring_ref_p
723 #undef TARGET_CHECK_STRING_OBJECT_FORMAT_ARG
724 #define TARGET_CHECK_STRING_OBJECT_FORMAT_ARG darwin_check_cfstring_format_arg
726 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;