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)
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 "double-int.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-pragma.h"
40 #include "c-family/c-format.h"
41 #include "diagnostic-core.h"
44 #include "cppdefault.h"
46 #include "c-family/c-target.h"
47 #include "c-family/c-target-def.h"
49 #include "dominance.h"
55 #include "cfgcleanup.h"
56 #include "basic-block.h"
59 #include "plugin-api.h"
64 #include "hard-reg-set.h"
69 #include "../../libcpp/internal.h"
73 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
74 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
76 static bool using_frameworks
= false;
78 static const char *find_subframework_header (cpp_reader
*pfile
, const char *header
,
81 typedef struct align_stack
84 struct align_stack
* prev
;
87 static struct align_stack
* field_align_stack
= NULL
;
89 /* Maintain a small stack of alignments. This is similar to pragma
90 pack's stack, but simpler. */
93 push_field_alignment (int bit_alignment
)
95 align_stack
*entry
= XNEW (align_stack
);
97 entry
->alignment
= maximum_field_alignment
;
98 entry
->prev
= field_align_stack
;
99 field_align_stack
= entry
;
101 maximum_field_alignment
= bit_alignment
;
105 pop_field_alignment (void)
107 if (field_align_stack
)
109 align_stack
*entry
= field_align_stack
;
111 maximum_field_alignment
= entry
->alignment
;
112 field_align_stack
= entry
->prev
;
116 error ("too many #pragma options align=reset");
119 /* Handlers for Darwin-specific pragmas. */
122 darwin_pragma_ignore (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
127 /* #pragma options align={mac68k|power|reset} */
130 darwin_pragma_options (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
135 if (pragma_lex (&t
) != CPP_NAME
)
136 BAD ("malformed '#pragma options', ignoring");
137 arg
= IDENTIFIER_POINTER (t
);
138 if (strcmp (arg
, "align"))
139 BAD ("malformed '#pragma options', ignoring");
140 if (pragma_lex (&t
) != CPP_EQ
)
141 BAD ("malformed '#pragma options', ignoring");
142 if (pragma_lex (&t
) != CPP_NAME
)
143 BAD ("malformed '#pragma options', ignoring");
145 if (pragma_lex (&x
) != CPP_EOF
)
146 warning (OPT_Wpragmas
, "junk at end of '#pragma options'");
148 arg
= IDENTIFIER_POINTER (t
);
149 if (!strcmp (arg
, "mac68k"))
150 push_field_alignment (16);
151 else if (!strcmp (arg
, "power"))
152 push_field_alignment (0);
153 else if (!strcmp (arg
, "reset"))
154 pop_field_alignment ();
156 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
159 /* #pragma unused ([var {, var}*]) */
162 darwin_pragma_unused (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
167 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
168 BAD ("missing '(' after '#pragma unused', ignoring");
172 tok
= pragma_lex (&decl
);
173 if (tok
== CPP_NAME
&& decl
)
175 tree local
= lookup_name (decl
);
176 if (local
&& (TREE_CODE (local
) == PARM_DECL
177 || TREE_CODE (local
) == VAR_DECL
))
179 TREE_USED (local
) = 1;
180 DECL_READ_P (local
) = 1;
182 tok
= pragma_lex (&x
);
183 if (tok
!= CPP_COMMA
)
188 if (tok
!= CPP_CLOSE_PAREN
)
189 BAD ("missing ')' after '#pragma unused', ignoring");
191 if (pragma_lex (&x
) != CPP_EOF
)
192 BAD ("junk at end of '#pragma unused'");
195 /* Parse the ms_struct pragma. */
197 darwin_pragma_ms_struct (cpp_reader
*pfile ATTRIBUTE_UNUSED
)
202 if (pragma_lex (&t
) != CPP_NAME
)
203 BAD ("malformed '#pragma ms_struct', ignoring");
204 arg
= IDENTIFIER_POINTER (t
);
206 if (!strcmp (arg
, "on"))
207 darwin_ms_struct
= true;
208 else if (!strcmp (arg
, "off") || !strcmp (arg
, "reset"))
209 darwin_ms_struct
= false;
211 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
213 if (pragma_lex (&t
) != CPP_EOF
)
214 BAD ("junk at end of '#pragma ms_struct'");
217 static struct frameworks_in_use
{
221 } *frameworks_in_use
;
222 static int num_frameworks
= 0;
223 static int max_frameworks
= 0;
226 /* Remember which frameworks have been seen, so that we can ensure
227 that all uses of that framework come from the same framework. DIR
228 is the place where the named framework NAME, which is of length
229 LEN, was found. We copy the directory name from NAME, as it will be
233 add_framework (const char *name
, size_t len
, cpp_dir
*dir
)
237 for (i
= 0; i
< num_frameworks
; ++i
)
239 if (len
== frameworks_in_use
[i
].len
240 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
245 if (i
>= max_frameworks
)
247 max_frameworks
= i
*2;
248 max_frameworks
+= i
== 0;
249 frameworks_in_use
= XRESIZEVEC (struct frameworks_in_use
,
250 frameworks_in_use
, max_frameworks
);
252 dir_name
= XNEWVEC (char, len
+ 1);
253 memcpy (dir_name
, name
, len
);
254 dir_name
[len
] = '\0';
255 frameworks_in_use
[num_frameworks
].name
= dir_name
;
256 frameworks_in_use
[num_frameworks
].len
= len
;
257 frameworks_in_use
[num_frameworks
].dir
= dir
;
261 /* Recall if we have seen the named framework NAME, before, and where
262 we saw it. NAME is LEN bytes long. The return value is the place
263 where it was seen before. */
265 static struct cpp_dir
*
266 find_framework (const char *name
, size_t len
)
269 for (i
= 0; i
< num_frameworks
; ++i
)
271 if (len
== frameworks_in_use
[i
].len
272 && strncmp (name
, frameworks_in_use
[i
].name
, len
) == 0)
274 return frameworks_in_use
[i
].dir
;
280 /* There are two directories in a framework that contain header files,
281 Headers and PrivateHeaders. We search Headers first as it is more
282 common to upgrade a header from PrivateHeaders to Headers and when
283 that is done, the old one might hang around and be out of data,
286 struct framework_header
{const char * dirName
; int dirNameLen
; };
287 static struct framework_header framework_header_dirs
[] = {
289 { "PrivateHeaders", 14 },
293 /* Returns a pointer to a malloced string that contains the real pathname
294 to the file, given the base name and the name. */
297 framework_construct_pathname (const char *fname
, cpp_dir
*dir
)
300 size_t fname_len
, frname_len
;
306 /* Framework names must have a / in them. */
307 buf
= strchr (fname
, '/');
309 fname_len
= buf
- fname
;
313 fast_dir
= find_framework (fname
, fname_len
);
315 /* Framework includes must all come from one framework. */
316 if (fast_dir
&& dir
!= fast_dir
)
319 frname
= XNEWVEC (char, strlen (fname
) + dir
->len
+ 2
320 + strlen(".framework/") + strlen("PrivateHeaders"));
321 strncpy (&frname
[0], dir
->name
, dir
->len
);
322 frname_len
= dir
->len
;
323 if (frname_len
&& frname
[frname_len
-1] != '/')
324 frname
[frname_len
++] = '/';
325 strncpy (&frname
[frname_len
], fname
, fname_len
);
326 frname_len
+= fname_len
;
327 strncpy (&frname
[frname_len
], ".framework/", strlen (".framework/"));
328 frname_len
+= strlen (".framework/");
332 frname
[frname_len
-1] = 0;
333 if (stat (frname
, &st
) == 0)
335 /* As soon as we find the first instance of the framework,
336 we stop and never use any later instance of that
338 add_framework (fname
, fname_len
, dir
);
342 /* If we can't find the parent directory, no point looking
347 frname
[frname_len
-1] = '/';
350 /* Append framework_header_dirs and header file name */
351 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
353 strncpy (&frname
[frname_len
],
354 framework_header_dirs
[i
].dirName
,
355 framework_header_dirs
[i
].dirNameLen
);
356 strcpy (&frname
[frname_len
+ framework_header_dirs
[i
].dirNameLen
],
359 if (stat (frname
, &st
) == 0)
367 /* Search for FNAME in sub-frameworks. pname is the context that we
368 wish to search in. Return the path the file was found at,
369 otherwise return 0. */
372 find_subframework_file (const char *fname
, const char *pname
)
375 const char *dot_framework
= ".framework/";
377 int sfrname_len
, i
, fname_len
;
378 struct cpp_dir
*fast_dir
;
379 static struct cpp_dir subframe_dir
;
382 bufptr
= strchr (fname
, '/');
384 /* Subframework files must have / in the name. */
388 fname_len
= bufptr
- fname
;
389 fast_dir
= find_framework (fname
, fname_len
);
391 /* Sub framework header filename includes parent framework name and
392 header name in the "CarbonCore/OSUtils.h" form. If it does not
393 include slash it is not a sub framework include. */
394 bufptr
= strstr (pname
, dot_framework
);
396 /* If the parent header is not of any framework, then this header
397 cannot be part of any subframework. */
401 /* Now translate. For example, +- bufptr
402 fname = CarbonCore/OSUtils.h |
403 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
405 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
407 sfrname
= XNEWVEC (char, strlen (pname
) + strlen (fname
) + 2 +
408 strlen ("Frameworks/") + strlen (".framework/")
409 + strlen ("PrivateHeaders"));
411 bufptr
+= strlen (dot_framework
);
413 sfrname_len
= bufptr
- pname
;
415 strncpy (&sfrname
[0], pname
, sfrname_len
);
417 strncpy (&sfrname
[sfrname_len
], "Frameworks/", strlen ("Frameworks/"));
418 sfrname_len
+= strlen("Frameworks/");
420 strncpy (&sfrname
[sfrname_len
], fname
, fname_len
);
421 sfrname_len
+= fname_len
;
423 strncpy (&sfrname
[sfrname_len
], ".framework/", strlen (".framework/"));
424 sfrname_len
+= strlen (".framework/");
426 /* Append framework_header_dirs and header file name */
427 for (i
= 0; framework_header_dirs
[i
].dirName
; i
++)
429 strncpy (&sfrname
[sfrname_len
],
430 framework_header_dirs
[i
].dirName
,
431 framework_header_dirs
[i
].dirNameLen
);
432 strcpy (&sfrname
[sfrname_len
+ framework_header_dirs
[i
].dirNameLen
],
435 if (stat (sfrname
, &st
) == 0)
437 if (fast_dir
!= &subframe_dir
)
440 warning (0, "subframework include %s conflicts with framework include",
443 add_framework (fname
, fname_len
, &subframe_dir
);
454 /* Add PATH to the system includes. PATH must be malloc-ed and
455 NUL-terminated. System framework paths are C++ aware. */
458 add_system_framework_path (char *path
)
466 p
->sysp
= 1 + !cxx_aware
;
467 p
->construct
= framework_construct_pathname
;
468 using_frameworks
= 1;
470 add_cpp_dir_path (p
, SYSTEM
);
473 /* Add PATH to the bracket includes. PATH must be malloc-ed and
477 add_framework_path (char *path
)
485 p
->construct
= framework_construct_pathname
;
486 using_frameworks
= 1;
488 add_cpp_dir_path (p
, BRACKET
);
491 static const char *framework_defaults
[] =
493 "/System/Library/Frameworks",
494 "/Library/Frameworks",
497 /* Register the GNU objective-C runtime include path if STDINC. */
500 darwin_register_objc_includes (const char *sysroot
, const char *iprefix
,
505 /* We do not do anything if we do not want the standard includes. */
509 fname
= GCC_INCLUDE_DIR
"-gnu-runtime";
511 /* Register the GNU OBJC runtime include path if we are compiling OBJC
514 if (c_dialect_objc () && !flag_next_runtime
)
517 /* See if our directory starts with the standard prefix.
518 "Translate" them, i.e. replace /usr/local/lib/gcc... with
519 IPREFIX and search them first. */
520 if (iprefix
&& (len
= cpp_GCC_INCLUDE_DIR_len
) != 0 && !sysroot
521 && !strncmp (fname
, cpp_GCC_INCLUDE_DIR
, len
))
523 str
= concat (iprefix
, fname
+ len
, NULL
);
524 /* FIXME: wrap the headers for C++awareness. */
525 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
528 /* Should this directory start with the sysroot? */
530 str
= concat (sysroot
, fname
, NULL
);
532 str
= update_path (fname
, "");
534 add_path (str
, SYSTEM
, /*c++aware=*/false, false);
539 /* Register all the system framework paths if STDINC is true and setup
540 the missing_header callback for subframework searching if any
541 frameworks had been registered. */
544 darwin_register_frameworks (const char *sysroot
,
545 const char *iprefix ATTRIBUTE_UNUSED
, int stdinc
)
551 /* Setup default search path for frameworks. */
552 for (i
=0; i
<sizeof (framework_defaults
)/sizeof(const char *); ++i
)
556 str
= concat (sysroot
, xstrdup (framework_defaults
[i
]), NULL
);
558 str
= xstrdup (framework_defaults
[i
]);
559 /* System Framework headers are cxx aware. */
560 add_system_framework_path (str
);
564 if (using_frameworks
)
565 cpp_get_callbacks (parse_in
)->missing_header
= find_subframework_header
;
568 /* Search for HEADER in context dependent way. The return value is
569 the malloced name of a header to try and open, if any, or NULL
570 otherwise. This is called after normal header lookup processing
571 fails to find a header. We search each file in the include stack,
572 using FUNC, starting from the most deeply nested include and
573 finishing with the main input file. We stop searching when FUNC
577 find_subframework_header (cpp_reader
*pfile
, const char *header
, cpp_dir
**dirp
)
579 const char *fname
= header
;
580 struct cpp_buffer
*b
;
583 for (b
= cpp_get_buffer (pfile
);
584 b
&& cpp_get_file (b
) && cpp_get_path (cpp_get_file (b
));
585 b
= cpp_get_prev (b
))
587 n
= find_subframework_file (fname
, cpp_get_path (cpp_get_file (b
)));
590 /* Logically, the place where we found the subframework is
591 the place where we found the Framework that contains the
592 subframework. This is useful for tracking wether or not
593 we are in a system header. */
594 *dirp
= cpp_get_dir (cpp_get_file (b
));
602 /* Return the value of darwin_macosx_version_min suitable for the
603 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro, so '10.4.2'
604 becomes 1040 and '10.10.0' becomes 101000. The lowest digit is
605 always zero, as is the second lowest for '10.10.x' and above.
606 Print a warning if the version number can't be understood. */
608 version_as_macro (void)
610 static char result
[7] = "1000";
613 if (strncmp (darwin_macosx_version_min
, "10.", 3) != 0)
615 if (! ISDIGIT (darwin_macosx_version_min
[3]))
619 result
[2] = darwin_macosx_version_min
[minorDigitIdx
++];
620 if (ISDIGIT (darwin_macosx_version_min
[minorDigitIdx
]))
622 /* Starting with OS X 10.10, the macro ends '00' rather than '0',
623 i.e. 10.10.x becomes 101000 rather than 10100. */
624 result
[3] = darwin_macosx_version_min
[minorDigitIdx
++];
629 if (darwin_macosx_version_min
[minorDigitIdx
] != '\0'
630 && darwin_macosx_version_min
[minorDigitIdx
] != '.')
636 error ("unknown value %qs of -mmacosx-version-min",
637 darwin_macosx_version_min
);
641 /* Define additional CPP flags for Darwin. */
643 #define builtin_define(TXT) cpp_define (pfile, TXT)
646 darwin_cpp_builtins (cpp_reader
*pfile
)
648 builtin_define ("__MACH__");
649 builtin_define ("__APPLE__");
651 /* __APPLE_CC__ is defined as some old Apple include files expect it
652 to be defined and won't work if it isn't. */
653 builtin_define_with_value ("__APPLE_CC__", "1", false);
655 if (darwin_constant_cfstrings
)
656 builtin_define ("__CONSTANT_CFSTRINGS__");
658 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
659 version_as_macro(), false);
661 /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
662 following will cause a syntax error if one tries to compile gc attributed
663 items. However, without this, NeXT system headers cannot be parsed
664 properly (on systems >= darwin 9). */
667 builtin_define ("__strong=__attribute__((objc_gc(strong)))");
668 builtin_define ("__weak=__attribute__((objc_gc(weak)))");
669 builtin_define ("__OBJC_GC__");
673 builtin_define ("__strong=");
674 builtin_define ("__weak=");
677 if (CPP_OPTION (pfile
, objc
) && flag_objc_abi
== 2)
678 builtin_define ("__OBJC2__");
681 /* Handle C family front-end options. */
684 handle_c_option (size_t code
,
686 int value ATTRIBUTE_UNUSED
)
691 /* Unrecognized options that we said we'd handle turn into
692 errors if not listed here. */
696 add_system_framework_path (xstrdup (arg
));
699 case OPT_fapple_kext
:
703 /* We recognized the option. */
707 /* Allow ObjC* access to CFStrings. */
709 darwin_objc_construct_string (tree str
)
711 if (!darwin_constant_cfstrings
)
713 /* Even though we are not using CFStrings, place our literal
714 into the cfstring_htab hash table, so that the
715 darwin_constant_cfstring_p() function will see it. */
716 darwin_enter_string_into_cfstring_table (str
);
717 /* Fall back to NSConstantString. */
721 return darwin_build_constant_cfstring (str
);
724 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
725 must match for it explicitly, since it's outside the gcc code. */
728 darwin_cfstring_ref_p (const_tree strp
)
731 if (!strp
|| TREE_CODE (strp
) != POINTER_TYPE
)
734 tn
= TYPE_NAME (strp
);
738 && IDENTIFIER_POINTER (tn
)
739 && !strncmp (IDENTIFIER_POINTER (tn
), "CFStringRef", 8));
742 /* At present the behavior of this is undefined and it does nothing. */
744 darwin_check_cfstring_format_arg (tree
ARG_UNUSED (format_arg
),
745 tree
ARG_UNUSED (args_list
))
749 /* The extra format types we recognize. */
750 EXPORTED_CONST format_kind_info darwin_additional_format_types
[] = {
751 { "CFString", NULL
, NULL
, NULL
, NULL
,
753 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
759 /* Support routines to dump the class references for NeXT ABI v1, aka
760 32-bits ObjC-2.0, as top-level asms.
761 The following two functions should only be called from
762 objc/objc-next-runtime-abi-01.c. */
765 darwin_objc_declare_unresolved_class_reference (const char *name
)
767 const char *lazy_reference
= ".lazy_reference\t";
768 const char *hard_reference
= ".reference\t";
769 const char *reference
= MACHOPIC_INDIRECT
? lazy_reference
: hard_reference
;
770 size_t len
= strlen (reference
) + strlen(name
) + 2;
771 char *buf
= (char *) alloca (len
);
773 gcc_checking_assert (!strncmp (name
, ".objc_class_name_", 17));
775 snprintf (buf
, len
, "%s%s", reference
, name
);
776 symtab
->finalize_toplevel_asm (build_string (strlen (buf
), buf
));
780 darwin_objc_declare_class_definition (const char *name
)
782 const char *xname
= targetm
.strip_name_encoding (name
);
783 size_t len
= strlen (xname
) + 7 + 5;
784 char *buf
= (char *) alloca (len
);
786 gcc_checking_assert (!strncmp (name
, ".objc_class_name_", 17)
787 || !strncmp (name
, "*.objc_category_name_", 21));
789 /* Mimic default_globalize_label. */
790 snprintf (buf
, len
, ".globl\t%s", xname
);
791 symtab
->finalize_toplevel_asm (build_string (strlen (buf
), buf
));
793 snprintf (buf
, len
, "%s = 0", xname
);
794 symtab
->finalize_toplevel_asm (build_string (strlen (buf
), buf
));
797 #undef TARGET_HANDLE_C_OPTION
798 #define TARGET_HANDLE_C_OPTION handle_c_option
800 #undef TARGET_OBJC_CONSTRUCT_STRING_OBJECT
801 #define TARGET_OBJC_CONSTRUCT_STRING_OBJECT darwin_objc_construct_string
803 #undef TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE
804 #define TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE \
805 darwin_objc_declare_unresolved_class_reference
807 #undef TARGET_OBJC_DECLARE_CLASS_DEFINITION
808 #define TARGET_OBJC_DECLARE_CLASS_DEFINITION \
809 darwin_objc_declare_class_definition
811 #undef TARGET_STRING_OBJECT_REF_TYPE_P
812 #define TARGET_STRING_OBJECT_REF_TYPE_P darwin_cfstring_ref_p
814 #undef TARGET_CHECK_STRING_OBJECT_FORMAT_ARG
815 #define TARGET_CHECK_STRING_OBJECT_FORMAT_ARG darwin_check_cfstring_format_arg
817 struct gcc_targetcm targetcm
= TARGETCM_INITIALIZER
;