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)
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/>. */
24 #include "coretypes.h"
30 #include "c-family/c-common.h"
31 #include "c-family/c-pragma.h"
32 #include "c-family/c-format.h"
33 #include "diagnostic-core.h"
36 #include "cppdefault.h"
38 #include "c-family/c-target.h"
39 #include "c-family/c-target-def.h"
44 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
45 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
47 static bool using_frameworks
= false;
49 static const char *find_subframework_header (cpp_reader
*pfile
, const char *header
,
52 typedef struct align_stack
55 struct align_stack
* prev
;
58 static struct align_stack
* field_align_stack
= NULL
;
60 /* Maintain a small stack of alignments. This is similar to pragma
61 pack's stack, but simpler. */
64 push_field_alignment (int bit_alignment
)
66 align_stack
*entry
= XNEW (align_stack
);
68 entry
->alignment
= maximum_field_alignment
;
69 entry
->prev
= field_align_stack
;
70 field_align_stack
= entry
;
72 maximum_field_alignment
= bit_alignment
;
76 pop_field_alignment (void)
78 if (field_align_stack
)
80 align_stack
*entry
= field_align_stack
;
82 maximum_field_alignment
= entry
->alignment
;
83 field_align_stack
= entry
->prev
;
87 error ("too many #pragma options align=reset");
90 /* Handlers for Darwin-specific pragmas. */
93 darwin_pragma_ignore (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
98 /* #pragma options align={mac68k|power|reset} */
101 darwin_pragma_options (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
106 if (pragma_lex (&t
) != CPP_NAME
)
107 BAD ("malformed '#pragma options', ignoring");
108 arg
= IDENTIFIER_POINTER (t
);
109 if (strcmp (arg
, "align"))
110 BAD ("malformed '#pragma options', ignoring");
111 if (pragma_lex (&t
) != CPP_EQ
)
112 BAD ("malformed '#pragma options', ignoring");
113 if (pragma_lex (&t
) != CPP_NAME
)
114 BAD ("malformed '#pragma options', ignoring");
116 if (pragma_lex (&x
) != CPP_EOF
)
117 warning (OPT_Wpragmas
, "junk at end of '#pragma options'");
119 arg
= IDENTIFIER_POINTER (t
);
120 if (!strcmp (arg
, "mac68k"))
121 push_field_alignment (16);
122 else if (!strcmp (arg
, "power"))
123 push_field_alignment (0);
124 else if (!strcmp (arg
, "reset"))
125 pop_field_alignment ();
127 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
130 /* #pragma unused ([var {, var}*]) */
133 darwin_pragma_unused (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
138 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
139 BAD ("missing '(' after '#pragma unused', ignoring");
143 tok
= pragma_lex (&decl
);
144 if (tok
== CPP_NAME
&& decl
)
146 tree local
= lookup_name (decl
);
147 if (local
&& (TREE_CODE (local
) == PARM_DECL
148 || TREE_CODE (local
) == VAR_DECL
))
150 TREE_USED (local
) = 1;
151 DECL_READ_P (local
) = 1;
153 tok
= pragma_lex (&x
);
154 if (tok
!= CPP_COMMA
)
159 if (tok
!= CPP_CLOSE_PAREN
)
160 BAD ("missing ')' after '#pragma unused', ignoring");
162 if (pragma_lex (&x
) != CPP_EOF
)
163 BAD ("junk at end of '#pragma unused'");
166 /* Parse the ms_struct pragma. */
168 darwin_pragma_ms_struct (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
173 if (pragma_lex (&t
) != CPP_NAME
)
174 BAD ("malformed '#pragma ms_struct', ignoring");
175 arg
= IDENTIFIER_POINTER (t
);
177 if (!strcmp (arg
, "on"))
178 darwin_ms_struct
= true;
179 else if (!strcmp (arg
, "off") || !strcmp (arg
, "reset"))
180 darwin_ms_struct
= false;
182 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
184 if (pragma_lex (&t
) != CPP_EOF
)
185 BAD ("junk at end of '#pragma ms_struct'");
188 static struct frameworks_in_use
{
192 } *frameworks_in_use
;
193 static int num_frameworks
= 0;
194 static int max_frameworks
= 0;
197 /* Remember which frameworks have been seen, so that we can ensure
198 that all uses of that framework come from the same framework. DIR
199 is the place where the named framework NAME, which is of length
200 LEN, was found. We copy the directory name from NAME, as it will be
204 add_framework (const char *name
, size_t len
, cpp_dir
*dir
)
208 for (i
= 0; i
< num_frameworks
; ++i
)
210 if (len
== frameworks_in_use
[i
].len
211 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
216 if (i
>= max_frameworks
)
218 max_frameworks
= i
*2;
219 max_frameworks
+= i
== 0;
220 frameworks_in_use
= XRESIZEVEC (struct frameworks_in_use
,
221 frameworks_in_use
, max_frameworks
);
223 dir_name
= XNEWVEC (char, len
+ 1);
224 memcpy (dir_name
, name
, len
);
225 dir_name
[len
] = '\0';
226 frameworks_in_use
[num_frameworks
].name
= dir_name
;
227 frameworks_in_use
[num_frameworks
].len
= len
;
228 frameworks_in_use
[num_frameworks
].dir
= dir
;
232 /* Recall if we have seen the named framework NAME, before, and where
233 we saw it. NAME is LEN bytes long. The return value is the place
234 where it was seen before. */
236 static struct cpp_dir
*
237 find_framework (const char *name
, size_t len
)
240 for (i
= 0; i
< num_frameworks
; ++i
)
242 if (len
== frameworks_in_use
[i
].len
243 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
245 return frameworks_in_use
[i
].dir
;
251 /* There are two directories in a framework that contain header files,
252 Headers and PrivateHeaders. We search Headers first as it is more
253 common to upgrade a header from PrivateHeaders to Headers and when
254 that is done, the old one might hang around and be out of data,
257 struct framework_header
{const char * dirName
; int dirNameLen
; };
258 static struct framework_header framework_header_dirs
[] = {
260 { "PrivateHeaders", 14 },
264 /* Returns a pointer to a malloced string that contains the real pathname
265 to the file, given the base name and the name. */
268 framework_construct_pathname (const char *fname
, cpp_dir
*dir
)
271 size_t fname_len
, frname_len
;
277 /* Framework names must have a / in them. */
278 buf
= strchr (fname
, '/');
280 fname_len
= buf
- fname
;
284 fast_dir
= find_framework (fname
, fname_len
);
286 /* Framework includes must all come from one framework. */
287 if (fast_dir
&& dir
!= fast_dir
)
290 frname
= XNEWVEC (char, strlen (fname
) + dir
->len
+ 2
291 + strlen(".framework/") + strlen("PrivateHeaders"));
292 strncpy (&frname
[0], dir
->name
, dir
->len
);
293 frname_len
= dir
->len
;
294 if (frname_len
&& frname
[frname_len
-1] != '/')
295 frname
[frname_len
++] = '/';
296 strncpy (&frname
[frname_len
], fname
, fname_len
);
297 frname_len
+= fname_len
;
298 strncpy (&frname
[frname_len
], ".framework/", strlen (".framework/"));
299 frname_len
+= strlen (".framework/");
303 frname
[frname_len
-1] = 0;
304 if (stat (frname
, &st
) == 0)
306 /* As soon as we find the first instance of the framework,
307 we stop and never use any later instance of that
309 add_framework (fname
, fname_len
, dir
);
313 /* If we can't find the parent directory, no point looking
318 frname
[frname_len
-1] = '/';
321 /* Append framework_header_dirs and header file name */
322 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
324 strncpy (&frname
[frname_len
],
325 framework_header_dirs
[i
].dirName
,
326 framework_header_dirs
[i
].dirNameLen
);
327 strcpy (&frname
[frname_len
+ framework_header_dirs
[i
].dirNameLen
],
330 if (stat (frname
, &st
) == 0)
338 /* Search for FNAME in sub-frameworks. pname is the context that we
339 wish to search in. Return the path the file was found at,
340 otherwise return 0. */
343 find_subframework_file (const char *fname
, const char *pname
)
346 const char *dot_framework
= ".framework/";
348 int sfrname_len
, i
, fname_len
;
349 struct cpp_dir
*fast_dir
;
350 static struct cpp_dir subframe_dir
;
353 bufptr
= strchr (fname
, '/');
355 /* Subframework files must have / in the name. */
359 fname_len
= bufptr
- fname
;
360 fast_dir
= find_framework (fname
, fname_len
);
362 /* Sub framework header filename includes parent framework name and
363 header name in the "CarbonCore/OSUtils.h" form. If it does not
364 include slash it is not a sub framework include. */
365 bufptr
= strstr (pname
, dot_framework
);
367 /* If the parent header is not of any framework, then this header
368 cannot be part of any subframework. */
372 /* Now translate. For example, +- bufptr
373 fname = CarbonCore/OSUtils.h |
374 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
376 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
378 sfrname
= XNEWVEC (char, strlen (pname
) + strlen (fname
) + 2 +
379 strlen ("Frameworks/") + strlen (".framework/")
380 + strlen ("PrivateHeaders"));
382 bufptr
+= strlen (dot_framework
);
384 sfrname_len
= bufptr
- pname
;
386 strncpy (&sfrname
[0], pname
, sfrname_len
);
388 strncpy (&sfrname
[sfrname_len
], "Frameworks/", strlen ("Frameworks/"));
389 sfrname_len
+= strlen("Frameworks/");
391 strncpy (&sfrname
[sfrname_len
], fname
, fname_len
);
392 sfrname_len
+= fname_len
;
394 strncpy (&sfrname
[sfrname_len
], ".framework/", strlen (".framework/"));
395 sfrname_len
+= strlen (".framework/");
397 /* Append framework_header_dirs and header file name */
398 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
400 strncpy (&sfrname
[sfrname_len
],
401 framework_header_dirs
[i
].dirName
,
402 framework_header_dirs
[i
].dirNameLen
);
403 strcpy (&sfrname
[sfrname_len
+ framework_header_dirs
[i
].dirNameLen
],
406 if (stat (sfrname
, &st
) == 0)
408 if (fast_dir
!= &subframe_dir
)
411 warning (0, "subframework include %s conflicts with framework include",
414 add_framework (fname
, fname_len
, &subframe_dir
);
425 /* Add PATH to the system includes. PATH must be malloc-ed and
426 NUL-terminated. System framework paths are C++ aware. */
429 add_system_framework_path (char *path
)
437 p
->sysp
= 1 + !cxx_aware
;
438 p
->construct
= framework_construct_pathname
;
439 using_frameworks
= 1;
441 add_cpp_dir_path (p
, SYSTEM
);
444 /* Add PATH to the bracket includes. PATH must be malloc-ed and
448 add_framework_path (char *path
)
456 p
->construct
= framework_construct_pathname
;
457 using_frameworks
= 1;
459 add_cpp_dir_path (p
, BRACKET
);
462 static const char *framework_defaults
[] =
464 "/System/Library/Frameworks",
465 "/Library/Frameworks",
468 /* Register the GNU objective-C runtime include path if STDINC. */
471 darwin_register_objc_includes (const char *sysroot
, const char *iprefix
,
476 /* We do not do anything if we do not want the standard includes. */
480 fname
= GCC_INCLUDE_DIR
"-gnu-runtime";
482 /* Register the GNU OBJC runtime include path if we are compiling OBJC
485 if (c_dialect_objc () && !flag_next_runtime
)
488 /* See if our directory starts with the standard prefix.
489 "Translate" them, i.e. replace /usr/local/lib/gcc... with
490 IPREFIX and search them first. */
491 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0 && !sysroot
492 && !strncmp (fname
, cpp_GCC_INCLUDE_DIR
, len
))
494 str
= concat (iprefix
, fname
+ len
, NULL
);
495 /* FIXME: wrap the headers for C++awareness. */
496 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
499 /* Should this directory start with the sysroot? */
501 str
= concat (sysroot
, fname
, NULL
);
503 str
= update_path (fname
, "");
505 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
510 /* Register all the system framework paths if STDINC is true and setup
511 the missing_header callback for subframework searching if any
512 frameworks had been registered. */
515 darwin_register_frameworks (const char *sysroot
,
516 const char *iprefix ATTRIBUTE_UNUSED
, int stdinc
)
522 /* Setup default search path for frameworks. */
523 for (i
=0; i
<sizeof (framework_defaults
)/sizeof(const char *); ++i
)
527 str
= concat (sysroot
, xstrdup (framework_defaults
[i
]), NULL
);
529 str
= xstrdup (framework_defaults
[i
]);
530 /* System Framework headers are cxx aware. */
531 add_system_framework_path (str
);
535 if (using_frameworks
)
536 cpp_get_callbacks (parse_in
)->missing_header
= find_subframework_header
;
539 /* Search for HEADER in context dependent way. The return value is
540 the malloced name of a header to try and open, if any, or NULL
541 otherwise. This is called after normal header lookup processing
542 fails to find a header. We search each file in the include stack,
543 using FUNC, starting from the most deeply nested include and
544 finishing with the main input file. We stop searching when FUNC
548 find_subframework_header (cpp_reader
*pfile
, const char *header
, cpp_dir
**dirp
)
550 const char *fname
= header
;
551 struct cpp_buffer
*b
;
554 for (b
= cpp_get_buffer (pfile
);
555 b
&& cpp_get_file (b
) && cpp_get_path (cpp_get_file (b
));
556 b
= cpp_get_prev (b
))
558 n
= find_subframework_file (fname
, cpp_get_path (cpp_get_file (b
)));
561 /* Logically, the place where we found the subframework is
562 the place where we found the Framework that contains the
563 subframework. This is useful for tracking wether or not
564 we are in a system header. */
565 *dirp
= cpp_get_dir (cpp_get_file (b
));
573 /* Return the value of darwin_macosx_version_min suitable for the
574 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
575 so '10.4.2' becomes 1040. The lowest digit is always zero.
576 Print a warning if the version number can't be understood. */
578 version_as_macro (void)
580 static char result
[] = "1000";
582 if (strncmp (darwin_macosx_version_min
, "10.", 3) != 0)
584 if (! ISDIGIT (darwin_macosx_version_min
[3]))
586 result
[2] = darwin_macosx_version_min
[3];
587 if (darwin_macosx_version_min
[4] != '\0'
588 && darwin_macosx_version_min
[4] != '.')
594 error ("unknown value %qs of -mmacosx-version-min",
595 darwin_macosx_version_min
);
599 /* Define additional CPP flags for Darwin. */
601 #define builtin_define(TXT) cpp_define (pfile, TXT)
604 darwin_cpp_builtins (cpp_reader
*pfile
)
606 builtin_define ("__MACH__");
607 builtin_define ("__APPLE__");
609 /* __APPLE_CC__ is defined as some old Apple include files expect it
610 to be defined and won't work if it isn't. */
611 builtin_define_with_value ("__APPLE_CC__", "1", false);
613 if (darwin_constant_cfstrings
)
614 builtin_define ("__CONSTANT_CFSTRINGS__");
616 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
617 version_as_macro(), false);
619 /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
620 following will cause a syntax error if one tries to compile gc attributed
621 items. However, without this, NeXT system headers cannot be parsed
622 properly (on systems >= darwin 9). */
625 builtin_define ("__strong=__attribute__((objc_gc(strong)))");
626 builtin_define ("__weak=__attribute__((objc_gc(weak)))");
627 builtin_define ("__OBJC_GC__");
631 builtin_define ("__strong=");
632 builtin_define ("__weak=");
635 if (flag_objc_abi
== 2)
636 builtin_define ("__OBJC2__");
639 /* Handle C family front-end options. */
642 handle_c_option (size_t code
,
644 int value ATTRIBUTE_UNUSED
)
649 /* Unrecognized options that we said we'd handle turn into
650 errors if not listed here. */
654 add_system_framework_path (xstrdup (arg
));
657 case OPT_fapple_kext
:
661 /* We recognized the option. */
665 /* Allow ObjC* access to CFStrings. */
667 darwin_objc_construct_string (tree str
)
669 if (!darwin_constant_cfstrings
)
671 /* Even though we are not using CFStrings, place our literal
672 into the cfstring_htab hash table, so that the
673 darwin_constant_cfstring_p() function will see it. */
674 darwin_enter_string_into_cfstring_table (str
);
675 /* Fall back to NSConstantString. */
679 return darwin_build_constant_cfstring (str
);
682 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
683 must match for it explicitly, since it's outside the gcc code. */
686 darwin_cfstring_ref_p (const_tree strp
)
689 if (!strp
|| TREE_CODE (strp
) != POINTER_TYPE
)
692 tn
= TYPE_NAME (strp
);
696 && IDENTIFIER_POINTER (tn
)
697 && !strncmp (IDENTIFIER_POINTER (tn
), "CFStringRef", 8));
700 /* At present the behavior of this is undefined and it does nothing. */
702 darwin_check_cfstring_format_arg (tree
ARG_UNUSED (format_arg
),
703 tree
ARG_UNUSED (args_list
))
707 /* The extra format types we recognize. */
708 EXPORTED_CONST format_kind_info darwin_additional_format_types
[] = {
709 { "CFString", NULL
, NULL
, NULL
, NULL
,
711 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
717 /* Support routines to dump the class references for NeXT ABI v1, aka
718 32-bits ObjC-2.0, as top-level asms.
719 The following two functions should only be called from
720 objc/objc-next-runtime-abi-01.c. */
723 darwin_objc_declare_unresolved_class_reference (const char *name
)
725 const char *lazy_reference
= ".lazy_reference\t";
726 const char *hard_reference
= ".reference\t";
727 const char *reference
= MACHOPIC_INDIRECT
? lazy_reference
: hard_reference
;
728 size_t len
= strlen (reference
) + strlen(name
) + 2;
729 char *buf
= (char *) alloca (len
);
731 gcc_checking_assert (!strncmp (name
, ".objc_class_name_", 17));
733 snprintf (buf
, len
, "%s%s", reference
, name
);
734 add_asm_node (build_string (strlen (buf
), buf
));
738 darwin_objc_declare_class_definition (const char *name
)
740 const char *xname
= targetm
.strip_name_encoding (name
);
741 size_t len
= strlen (xname
) + 7 + 5;
742 char *buf
= (char *) alloca (len
);
744 gcc_checking_assert (!strncmp (name
, ".objc_class_name_", 17)
745 || !strncmp (name
, "*.objc_category_name_", 21));
747 /* Mimic default_globalize_label. */
748 snprintf (buf
, len
, ".globl\t%s", xname
);
749 add_asm_node (build_string (strlen (buf
), buf
));
751 snprintf (buf
, len
, "%s = 0", xname
);
752 add_asm_node (build_string (strlen (buf
), buf
));
755 #undef TARGET_HANDLE_C_OPTION
756 #define TARGET_HANDLE_C_OPTION handle_c_option
758 #undef TARGET_OBJC_CONSTRUCT_STRING_OBJECT
759 #define TARGET_OBJC_CONSTRUCT_STRING_OBJECT darwin_objc_construct_string
761 #undef TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE
762 #define TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE \
763 darwin_objc_declare_unresolved_class_reference
765 #undef TARGET_OBJC_DECLARE_CLASS_DEFINITION
766 #define TARGET_OBJC_DECLARE_CLASS_DEFINITION \
767 darwin_objc_declare_class_definition
769 #undef TARGET_STRING_OBJECT_REF_TYPE_P
770 #define TARGET_STRING_OBJECT_REF_TYPE_P darwin_cfstring_ref_p
772 #undef TARGET_CHECK_STRING_OBJECT_FORMAT_ARG
773 #define TARGET_CHECK_STRING_OBJECT_FORMAT_ARG darwin_check_cfstring_format_arg
775 struct gcc_targetcm targetcm
= TARGETCM_INITIALIZER
;