1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001-2014 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)
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/>. */
23 #include "coretypes.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"
35 #include "cppdefault.h"
37 #include "c-family/c-target.h"
38 #include "c-family/c-target-def.h"
40 #include "dominance.h"
46 #include "cfgcleanup.h"
47 #include "basic-block.h"
50 #include "plugin-api.h"
55 #include "hard-reg-set.h"
60 #include "../../libcpp/internal.h"
64 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
65 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
67 static bool using_frameworks
= false;
69 static const char *find_subframework_header (cpp_reader
*pfile
, const char *header
,
72 typedef struct align_stack
75 struct align_stack
* prev
;
78 static struct align_stack
* field_align_stack
= NULL
;
80 /* Maintain a small stack of alignments. This is similar to pragma
81 pack's stack, but simpler. */
84 push_field_alignment (int bit_alignment
)
86 align_stack
*entry
= XNEW (align_stack
);
88 entry
->alignment
= maximum_field_alignment
;
89 entry
->prev
= field_align_stack
;
90 field_align_stack
= entry
;
92 maximum_field_alignment
= bit_alignment
;
96 pop_field_alignment (void)
98 if (field_align_stack
)
100 align_stack
*entry
= field_align_stack
;
102 maximum_field_alignment
= entry
->alignment
;
103 field_align_stack
= entry
->prev
;
107 error ("too many #pragma options align=reset");
110 /* Handlers for Darwin-specific pragmas. */
113 darwin_pragma_ignore (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
118 /* #pragma options align={mac68k|power|reset} */
121 darwin_pragma_options (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
126 if (pragma_lex (&t
) != CPP_NAME
)
127 BAD ("malformed '#pragma options', ignoring");
128 arg
= IDENTIFIER_POINTER (t
);
129 if (strcmp (arg
, "align"))
130 BAD ("malformed '#pragma options', ignoring");
131 if (pragma_lex (&t
) != CPP_EQ
)
132 BAD ("malformed '#pragma options', ignoring");
133 if (pragma_lex (&t
) != CPP_NAME
)
134 BAD ("malformed '#pragma options', ignoring");
136 if (pragma_lex (&x
) != CPP_EOF
)
137 warning (OPT_Wpragmas
, "junk at end of '#pragma options'");
139 arg
= IDENTIFIER_POINTER (t
);
140 if (!strcmp (arg
, "mac68k"))
141 push_field_alignment (16);
142 else if (!strcmp (arg
, "power"))
143 push_field_alignment (0);
144 else if (!strcmp (arg
, "reset"))
145 pop_field_alignment ();
147 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
150 /* #pragma unused ([var {, var}*]) */
153 darwin_pragma_unused (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
158 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
159 BAD ("missing '(' after '#pragma unused', ignoring");
163 tok
= pragma_lex (&decl
);
164 if (tok
== CPP_NAME
&& decl
)
166 tree local
= lookup_name (decl
);
167 if (local
&& (TREE_CODE (local
) == PARM_DECL
168 || TREE_CODE (local
) == VAR_DECL
))
170 TREE_USED (local
) = 1;
171 DECL_READ_P (local
) = 1;
173 tok
= pragma_lex (&x
);
174 if (tok
!= CPP_COMMA
)
179 if (tok
!= CPP_CLOSE_PAREN
)
180 BAD ("missing ')' after '#pragma unused', ignoring");
182 if (pragma_lex (&x
) != CPP_EOF
)
183 BAD ("junk at end of '#pragma unused'");
186 /* Parse the ms_struct pragma. */
188 darwin_pragma_ms_struct (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
193 if (pragma_lex (&t
) != CPP_NAME
)
194 BAD ("malformed '#pragma ms_struct', ignoring");
195 arg
= IDENTIFIER_POINTER (t
);
197 if (!strcmp (arg
, "on"))
198 darwin_ms_struct
= true;
199 else if (!strcmp (arg
, "off") || !strcmp (arg
, "reset"))
200 darwin_ms_struct
= false;
202 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
204 if (pragma_lex (&t
) != CPP_EOF
)
205 BAD ("junk at end of '#pragma ms_struct'");
208 static struct frameworks_in_use
{
212 } *frameworks_in_use
;
213 static int num_frameworks
= 0;
214 static int max_frameworks
= 0;
217 /* Remember which frameworks have been seen, so that we can ensure
218 that all uses of that framework come from the same framework. DIR
219 is the place where the named framework NAME, which is of length
220 LEN, was found. We copy the directory name from NAME, as it will be
224 add_framework (const char *name
, size_t len
, cpp_dir
*dir
)
228 for (i
= 0; i
< num_frameworks
; ++i
)
230 if (len
== frameworks_in_use
[i
].len
231 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
236 if (i
>= max_frameworks
)
238 max_frameworks
= i
*2;
239 max_frameworks
+= i
== 0;
240 frameworks_in_use
= XRESIZEVEC (struct frameworks_in_use
,
241 frameworks_in_use
, max_frameworks
);
243 dir_name
= XNEWVEC (char, len
+ 1);
244 memcpy (dir_name
, name
, len
);
245 dir_name
[len
] = '\0';
246 frameworks_in_use
[num_frameworks
].name
= dir_name
;
247 frameworks_in_use
[num_frameworks
].len
= len
;
248 frameworks_in_use
[num_frameworks
].dir
= dir
;
252 /* Recall if we have seen the named framework NAME, before, and where
253 we saw it. NAME is LEN bytes long. The return value is the place
254 where it was seen before. */
256 static struct cpp_dir
*
257 find_framework (const char *name
, size_t len
)
260 for (i
= 0; i
< num_frameworks
; ++i
)
262 if (len
== frameworks_in_use
[i
].len
263 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
265 return frameworks_in_use
[i
].dir
;
271 /* There are two directories in a framework that contain header files,
272 Headers and PrivateHeaders. We search Headers first as it is more
273 common to upgrade a header from PrivateHeaders to Headers and when
274 that is done, the old one might hang around and be out of data,
277 struct framework_header
{const char * dirName
; int dirNameLen
; };
278 static struct framework_header framework_header_dirs
[] = {
280 { "PrivateHeaders", 14 },
284 /* Returns a pointer to a malloced string that contains the real pathname
285 to the file, given the base name and the name. */
288 framework_construct_pathname (const char *fname
, cpp_dir
*dir
)
291 size_t fname_len
, frname_len
;
297 /* Framework names must have a / in them. */
298 buf
= strchr (fname
, '/');
300 fname_len
= buf
- fname
;
304 fast_dir
= find_framework (fname
, fname_len
);
306 /* Framework includes must all come from one framework. */
307 if (fast_dir
&& dir
!= fast_dir
)
310 frname
= XNEWVEC (char, strlen (fname
) + dir
->len
+ 2
311 + strlen(".framework/") + strlen("PrivateHeaders"));
312 strncpy (&frname
[0], dir
->name
, dir
->len
);
313 frname_len
= dir
->len
;
314 if (frname_len
&& frname
[frname_len
-1] != '/')
315 frname
[frname_len
++] = '/';
316 strncpy (&frname
[frname_len
], fname
, fname_len
);
317 frname_len
+= fname_len
;
318 strncpy (&frname
[frname_len
], ".framework/", strlen (".framework/"));
319 frname_len
+= strlen (".framework/");
323 frname
[frname_len
-1] = 0;
324 if (stat (frname
, &st
) == 0)
326 /* As soon as we find the first instance of the framework,
327 we stop and never use any later instance of that
329 add_framework (fname
, fname_len
, dir
);
333 /* If we can't find the parent directory, no point looking
338 frname
[frname_len
-1] = '/';
341 /* Append framework_header_dirs and header file name */
342 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
344 strncpy (&frname
[frname_len
],
345 framework_header_dirs
[i
].dirName
,
346 framework_header_dirs
[i
].dirNameLen
);
347 strcpy (&frname
[frname_len
+ framework_header_dirs
[i
].dirNameLen
],
350 if (stat (frname
, &st
) == 0)
358 /* Search for FNAME in sub-frameworks. pname is the context that we
359 wish to search in. Return the path the file was found at,
360 otherwise return 0. */
363 find_subframework_file (const char *fname
, const char *pname
)
366 const char *dot_framework
= ".framework/";
368 int sfrname_len
, i
, fname_len
;
369 struct cpp_dir
*fast_dir
;
370 static struct cpp_dir subframe_dir
;
373 bufptr
= strchr (fname
, '/');
375 /* Subframework files must have / in the name. */
379 fname_len
= bufptr
- fname
;
380 fast_dir
= find_framework (fname
, fname_len
);
382 /* Sub framework header filename includes parent framework name and
383 header name in the "CarbonCore/OSUtils.h" form. If it does not
384 include slash it is not a sub framework include. */
385 bufptr
= strstr (pname
, dot_framework
);
387 /* If the parent header is not of any framework, then this header
388 cannot be part of any subframework. */
392 /* Now translate. For example, +- bufptr
393 fname = CarbonCore/OSUtils.h |
394 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
396 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
398 sfrname
= XNEWVEC (char, strlen (pname
) + strlen (fname
) + 2 +
399 strlen ("Frameworks/") + strlen (".framework/")
400 + strlen ("PrivateHeaders"));
402 bufptr
+= strlen (dot_framework
);
404 sfrname_len
= bufptr
- pname
;
406 strncpy (&sfrname
[0], pname
, sfrname_len
);
408 strncpy (&sfrname
[sfrname_len
], "Frameworks/", strlen ("Frameworks/"));
409 sfrname_len
+= strlen("Frameworks/");
411 strncpy (&sfrname
[sfrname_len
], fname
, fname_len
);
412 sfrname_len
+= fname_len
;
414 strncpy (&sfrname
[sfrname_len
], ".framework/", strlen (".framework/"));
415 sfrname_len
+= strlen (".framework/");
417 /* Append framework_header_dirs and header file name */
418 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
420 strncpy (&sfrname
[sfrname_len
],
421 framework_header_dirs
[i
].dirName
,
422 framework_header_dirs
[i
].dirNameLen
);
423 strcpy (&sfrname
[sfrname_len
+ framework_header_dirs
[i
].dirNameLen
],
426 if (stat (sfrname
, &st
) == 0)
428 if (fast_dir
!= &subframe_dir
)
431 warning (0, "subframework include %s conflicts with framework include",
434 add_framework (fname
, fname_len
, &subframe_dir
);
445 /* Add PATH to the system includes. PATH must be malloc-ed and
446 NUL-terminated. System framework paths are C++ aware. */
449 add_system_framework_path (char *path
)
457 p
->sysp
= 1 + !cxx_aware
;
458 p
->construct
= framework_construct_pathname
;
459 using_frameworks
= 1;
461 add_cpp_dir_path (p
, SYSTEM
);
464 /* Add PATH to the bracket includes. PATH must be malloc-ed and
468 add_framework_path (char *path
)
476 p
->construct
= framework_construct_pathname
;
477 using_frameworks
= 1;
479 add_cpp_dir_path (p
, BRACKET
);
482 static const char *framework_defaults
[] =
484 "/System/Library/Frameworks",
485 "/Library/Frameworks",
488 /* Register the GNU objective-C runtime include path if STDINC. */
491 darwin_register_objc_includes (const char *sysroot
, const char *iprefix
,
496 /* We do not do anything if we do not want the standard includes. */
500 fname
= GCC_INCLUDE_DIR
"-gnu-runtime";
502 /* Register the GNU OBJC runtime include path if we are compiling OBJC
505 if (c_dialect_objc () && !flag_next_runtime
)
508 /* See if our directory starts with the standard prefix.
509 "Translate" them, i.e. replace /usr/local/lib/gcc... with
510 IPREFIX and search them first. */
511 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0 && !sysroot
512 && !strncmp (fname
, cpp_GCC_INCLUDE_DIR
, len
))
514 str
= concat (iprefix
, fname
+ len
, NULL
);
515 /* FIXME: wrap the headers for C++awareness. */
516 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
519 /* Should this directory start with the sysroot? */
521 str
= concat (sysroot
, fname
, NULL
);
523 str
= update_path (fname
, "");
525 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
530 /* Register all the system framework paths if STDINC is true and setup
531 the missing_header callback for subframework searching if any
532 frameworks had been registered. */
535 darwin_register_frameworks (const char *sysroot
,
536 const char *iprefix ATTRIBUTE_UNUSED
, int stdinc
)
542 /* Setup default search path for frameworks. */
543 for (i
=0; i
<sizeof (framework_defaults
)/sizeof(const char *); ++i
)
547 str
= concat (sysroot
, xstrdup (framework_defaults
[i
]), NULL
);
549 str
= xstrdup (framework_defaults
[i
]);
550 /* System Framework headers are cxx aware. */
551 add_system_framework_path (str
);
555 if (using_frameworks
)
556 cpp_get_callbacks (parse_in
)->missing_header
= find_subframework_header
;
559 /* Search for HEADER in context dependent way. The return value is
560 the malloced name of a header to try and open, if any, or NULL
561 otherwise. This is called after normal header lookup processing
562 fails to find a header. We search each file in the include stack,
563 using FUNC, starting from the most deeply nested include and
564 finishing with the main input file. We stop searching when FUNC
568 find_subframework_header (cpp_reader
*pfile
, const char *header
, cpp_dir
**dirp
)
570 const char *fname
= header
;
571 struct cpp_buffer
*b
;
574 for (b
= cpp_get_buffer (pfile
);
575 b
&& cpp_get_file (b
) && cpp_get_path (cpp_get_file (b
));
576 b
= cpp_get_prev (b
))
578 n
= find_subframework_file (fname
, cpp_get_path (cpp_get_file (b
)));
581 /* Logically, the place where we found the subframework is
582 the place where we found the Framework that contains the
583 subframework. This is useful for tracking wether or not
584 we are in a system header. */
585 *dirp
= cpp_get_dir (cpp_get_file (b
));
593 /* Return the value of darwin_macosx_version_min suitable for the
594 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro, so '10.4.2'
595 becomes 1040 and '10.10.0' becomes 101000. The lowest digit is
596 always zero, as is the second lowest for '10.10.x' and above.
597 Print a warning if the version number can't be understood. */
599 version_as_macro (void)
601 static char result
[7] = "1000";
604 if (strncmp (darwin_macosx_version_min
, "10.", 3) != 0)
606 if (! ISDIGIT (darwin_macosx_version_min
[3]))
610 result
[2] = darwin_macosx_version_min
[minorDigitIdx
++];
611 if (ISDIGIT (darwin_macosx_version_min
[minorDigitIdx
]))
613 /* Starting with OS X 10.10, the macro ends '00' rather than '0',
614 i.e. 10.10.x becomes 101000 rather than 10100. */
615 result
[3] = darwin_macosx_version_min
[minorDigitIdx
++];
620 if (darwin_macosx_version_min
[minorDigitIdx
] != '\0'
621 && darwin_macosx_version_min
[minorDigitIdx
] != '.')
627 error ("unknown value %qs of -mmacosx-version-min",
628 darwin_macosx_version_min
);
632 /* Define additional CPP flags for Darwin. */
634 #define builtin_define(TXT) cpp_define (pfile, TXT)
637 darwin_cpp_builtins (cpp_reader
*pfile
)
639 builtin_define ("__MACH__");
640 builtin_define ("__APPLE__");
642 /* __APPLE_CC__ is defined as some old Apple include files expect it
643 to be defined and won't work if it isn't. */
644 builtin_define_with_value ("__APPLE_CC__", "1", false);
646 if (darwin_constant_cfstrings
)
647 builtin_define ("__CONSTANT_CFSTRINGS__");
649 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
650 version_as_macro(), false);
652 /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
653 following will cause a syntax error if one tries to compile gc attributed
654 items. However, without this, NeXT system headers cannot be parsed
655 properly (on systems >= darwin 9). */
658 builtin_define ("__strong=__attribute__((objc_gc(strong)))");
659 builtin_define ("__weak=__attribute__((objc_gc(weak)))");
660 builtin_define ("__OBJC_GC__");
664 builtin_define ("__strong=");
665 builtin_define ("__weak=");
668 if (CPP_OPTION (pfile
, objc
) && flag_objc_abi
== 2)
669 builtin_define ("__OBJC2__");
672 /* Handle C family front-end options. */
675 handle_c_option (size_t code
,
677 int value ATTRIBUTE_UNUSED
)
682 /* Unrecognized options that we said we'd handle turn into
683 errors if not listed here. */
687 add_system_framework_path (xstrdup (arg
));
690 case OPT_fapple_kext
:
694 /* We recognized the option. */
698 /* Allow ObjC* access to CFStrings. */
700 darwin_objc_construct_string (tree str
)
702 if (!darwin_constant_cfstrings
)
704 /* Even though we are not using CFStrings, place our literal
705 into the cfstring_htab hash table, so that the
706 darwin_constant_cfstring_p() function will see it. */
707 darwin_enter_string_into_cfstring_table (str
);
708 /* Fall back to NSConstantString. */
712 return darwin_build_constant_cfstring (str
);
715 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
716 must match for it explicitly, since it's outside the gcc code. */
719 darwin_cfstring_ref_p (const_tree strp
)
722 if (!strp
|| TREE_CODE (strp
) != POINTER_TYPE
)
725 tn
= TYPE_NAME (strp
);
729 && IDENTIFIER_POINTER (tn
)
730 && !strncmp (IDENTIFIER_POINTER (tn
), "CFStringRef", 8));
733 /* At present the behavior of this is undefined and it does nothing. */
735 darwin_check_cfstring_format_arg (tree
ARG_UNUSED (format_arg
),
736 tree
ARG_UNUSED (args_list
))
740 /* The extra format types we recognize. */
741 EXPORTED_CONST format_kind_info darwin_additional_format_types
[] = {
742 { "CFString", NULL
, NULL
, NULL
, NULL
,
744 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
750 /* Support routines to dump the class references for NeXT ABI v1, aka
751 32-bits ObjC-2.0, as top-level asms.
752 The following two functions should only be called from
753 objc/objc-next-runtime-abi-01.c. */
756 darwin_objc_declare_unresolved_class_reference (const char *name
)
758 const char *lazy_reference
= ".lazy_reference\t";
759 const char *hard_reference
= ".reference\t";
760 const char *reference
= MACHOPIC_INDIRECT
? lazy_reference
: hard_reference
;
761 size_t len
= strlen (reference
) + strlen(name
) + 2;
762 char *buf
= (char *) alloca (len
);
764 gcc_checking_assert (!strncmp (name
, ".objc_class_name_", 17));
766 snprintf (buf
, len
, "%s%s", reference
, name
);
767 symtab
->finalize_toplevel_asm (build_string (strlen (buf
), buf
));
771 darwin_objc_declare_class_definition (const char *name
)
773 const char *xname
= targetm
.strip_name_encoding (name
);
774 size_t len
= strlen (xname
) + 7 + 5;
775 char *buf
= (char *) alloca (len
);
777 gcc_checking_assert (!strncmp (name
, ".objc_class_name_", 17)
778 || !strncmp (name
, "*.objc_category_name_", 21));
780 /* Mimic default_globalize_label. */
781 snprintf (buf
, len
, ".globl\t%s", xname
);
782 symtab
->finalize_toplevel_asm (build_string (strlen (buf
), buf
));
784 snprintf (buf
, len
, "%s = 0", xname
);
785 symtab
->finalize_toplevel_asm (build_string (strlen (buf
), buf
));
788 #undef TARGET_HANDLE_C_OPTION
789 #define TARGET_HANDLE_C_OPTION handle_c_option
791 #undef TARGET_OBJC_CONSTRUCT_STRING_OBJECT
792 #define TARGET_OBJC_CONSTRUCT_STRING_OBJECT darwin_objc_construct_string
794 #undef TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE
795 #define TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE \
796 darwin_objc_declare_unresolved_class_reference
798 #undef TARGET_OBJC_DECLARE_CLASS_DEFINITION
799 #define TARGET_OBJC_DECLARE_CLASS_DEFINITION \
800 darwin_objc_declare_class_definition
802 #undef TARGET_STRING_OBJECT_REF_TYPE_P
803 #define TARGET_STRING_OBJECT_REF_TYPE_P darwin_cfstring_ref_p
805 #undef TARGET_CHECK_STRING_OBJECT_FORMAT_ARG
806 #define TARGET_CHECK_STRING_OBJECT_FORMAT_ARG darwin_check_cfstring_format_arg
808 struct gcc_targetcm targetcm
= TARGETCM_INITIALIZER
;