]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/darwin-c.c
* config/rs6000/rs6000.c: Clean up trailing whitespace.
[thirdparty/gcc.git] / gcc / config / darwin-c.c
CommitLineData
0168a849 1/* Darwin support needed only by C/C++ frontends.
fe9565ed 2 Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
0168a849
SS
3 Contributed by Apple Computer Inc.
4
7ec022b2 5This file is part of GCC.
0168a849 6
7ec022b2 7GCC is free software; you can redistribute it and/or modify
0168a849
SS
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
7ec022b2 12GCC is distributed in the hope that it will be useful,
0168a849
SS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
7ec022b2 18along with GCC; see the file COPYING. If not, write to
39d14dda
KC
19the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
0168a849
SS
21
22#include "config.h"
23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
0168a849
SS
26#include "cpplib.h"
27#include "tree.h"
28#include "c-pragma.h"
0168a849 29#include "c-tree.h"
94d1613b 30#include "c-incpath.h"
10e6657a 31#include "c-common.h"
0168a849 32#include "toplev.h"
43b3a5b1 33#include "flags.h"
0168a849 34#include "tm_p.h"
3dd53121
AP
35#include "cppdefault.h"
36#include "prefix.h"
0168a849
SS
37
38/* Pragmas. */
39
76ad5c10
MS
40#define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
41#define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
0168a849 42
94d1613b
MS
43static bool using_frameworks = false;
44
0168a849
SS
45/* Maintain a small stack of alignments. This is similar to pragma
46 pack's stack, but simpler. */
47
9c808aad
AJ
48static void push_field_alignment (int);
49static void pop_field_alignment (void);
94d1613b
MS
50static const char *find_subframework_file (const char *, const char *);
51static void add_system_framework_path (char *);
e3c287c9
MS
52static const char *find_subframework_header (cpp_reader *pfile, const char *header,
53 cpp_dir **dirp);
0168a849
SS
54
55typedef struct align_stack
56{
57 int alignment;
58 struct align_stack * prev;
59} align_stack;
60
61static struct align_stack * field_align_stack = NULL;
62
63static void
9c808aad 64push_field_alignment (int bit_alignment)
0168a849 65{
5ed6ace5 66 align_stack *entry = XNEW (align_stack);
0168a849
SS
67
68 entry->alignment = maximum_field_alignment;
69 entry->prev = field_align_stack;
70 field_align_stack = entry;
71
72 maximum_field_alignment = bit_alignment;
73}
74
75static void
9c808aad 76pop_field_alignment (void)
0168a849
SS
77{
78 if (field_align_stack)
79 {
80 align_stack *entry = field_align_stack;
81
82 maximum_field_alignment = entry->alignment;
83 field_align_stack = entry->prev;
84 free (entry);
85 }
86 else
87 error ("too many #pragma options align=reset");
88}
89
90/* Handlers for Darwin-specific pragmas. */
91
92void
9c808aad 93darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
0168a849
SS
94{
95 /* Do nothing. */
96}
97
98/* #pragma options align={mac68k|power|reset} */
99
100void
9c808aad 101darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
0168a849 102{
7ae8cf75 103 const char *arg;
0168a849
SS
104 tree t, x;
105
75ce3d48 106 if (pragma_lex (&t) != CPP_NAME)
0168a849
SS
107 BAD ("malformed '#pragma options', ignoring");
108 arg = IDENTIFIER_POINTER (t);
109 if (strcmp (arg, "align"))
110 BAD ("malformed '#pragma options', ignoring");
75ce3d48 111 if (pragma_lex (&t) != CPP_EQ)
0168a849 112 BAD ("malformed '#pragma options', ignoring");
75ce3d48 113 if (pragma_lex (&t) != CPP_NAME)
0168a849
SS
114 BAD ("malformed '#pragma options', ignoring");
115
75ce3d48 116 if (pragma_lex (&x) != CPP_EOF)
aea8e035 117 warning (OPT_Wpragmas, "junk at end of '#pragma options'");
0168a849
SS
118
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 ();
126 else
213af8c8 127 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
0168a849
SS
128}
129
130/* #pragma unused ([var {, var}*]) */
131
132void
9c808aad 133darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
0168a849
SS
134{
135 tree decl, x;
136 int tok;
137
75ce3d48 138 if (pragma_lex (&x) != CPP_OPEN_PAREN)
0168a849
SS
139 BAD ("missing '(' after '#pragma unused', ignoring");
140
141 while (1)
142 {
75ce3d48 143 tok = pragma_lex (&decl);
0168a849
SS
144 if (tok == CPP_NAME && decl)
145 {
10e6657a 146 tree local = lookup_name (decl);
0168a849
SS
147 if (local && (TREE_CODE (local) == PARM_DECL
148 || TREE_CODE (local) == VAR_DECL))
149 TREE_USED (local) = 1;
75ce3d48 150 tok = pragma_lex (&x);
0168a849
SS
151 if (tok != CPP_COMMA)
152 break;
153 }
154 }
155
156 if (tok != CPP_CLOSE_PAREN)
157 BAD ("missing ')' after '#pragma unused', ignoring");
158
75ce3d48 159 if (pragma_lex (&x) != CPP_EOF)
213af8c8 160 BAD ("junk at end of '#pragma unused'");
0168a849 161}
94d1613b 162
16d6f994
EC
163/* Parse the ms_struct pragma. */
164void
165darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
166{
167 const char *arg;
168 tree t;
169
170 if (pragma_lex (&t) != CPP_NAME)
171 BAD ("malformed '#pragma ms_struct', ignoring");
172 arg = IDENTIFIER_POINTER (t);
173
174 if (!strcmp (arg, "on"))
175 darwin_ms_struct = true;
176 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
177 darwin_ms_struct = false;
178 else
213af8c8 179 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
16d6f994
EC
180
181 if (pragma_lex (&t) != CPP_EOF)
213af8c8 182 BAD ("junk at end of '#pragma ms_struct'");
16d6f994
EC
183}
184
94d1613b
MS
185static struct {
186 size_t len;
187 const char *name;
188 cpp_dir* dir;
189} *frameworks_in_use;
190static int num_frameworks = 0;
191static int max_frameworks = 0;
192
193
194/* Remember which frameworks have been seen, so that we can ensure
195 that all uses of that framework come from the same framework. DIR
196 is the place where the named framework NAME, which is of length
e3c287c9
MS
197 LEN, was found. We copy the directory name from NAME, as it will be
198 freed by others. */
94d1613b
MS
199
200static void
201add_framework (const char *name, size_t len, cpp_dir *dir)
202{
e3c287c9 203 char *dir_name;
94d1613b
MS
204 int i;
205 for (i = 0; i < num_frameworks; ++i)
206 {
207 if (len == frameworks_in_use[i].len
208 && strncmp (name, frameworks_in_use[i].name, len) == 0)
209 {
210 return;
211 }
212 }
213 if (i >= max_frameworks)
214 {
215 max_frameworks = i*2;
e3c287c9 216 max_frameworks += i == 0;
94d1613b
MS
217 frameworks_in_use = xrealloc (frameworks_in_use,
218 max_frameworks*sizeof(*frameworks_in_use));
219 }
5ed6ace5 220 dir_name = XNEWVEC (char, len + 1);
e3c287c9
MS
221 memcpy (dir_name, name, len);
222 dir_name[len] = '\0';
223 frameworks_in_use[num_frameworks].name = dir_name;
94d1613b
MS
224 frameworks_in_use[num_frameworks].len = len;
225 frameworks_in_use[num_frameworks].dir = dir;
226 ++num_frameworks;
227}
228
229/* Recall if we have seen the named framework NAME, before, and where
230 we saw it. NAME is LEN bytes long. The return value is the place
231 where it was seen before. */
232
233static struct cpp_dir*
234find_framework (const char *name, size_t len)
235{
236 int i;
237 for (i = 0; i < num_frameworks; ++i)
238 {
239 if (len == frameworks_in_use[i].len
240 && strncmp (name, frameworks_in_use[i].name, len) == 0)
241 {
242 return frameworks_in_use[i].dir;
243 }
244 }
245 return 0;
246}
247
248/* There are two directories in a framework that contain header files,
249 Headers and PrivateHeaders. We search Headers first as it is more
250 common to upgrade a header from PrivateHeaders to Headers and when
251 that is done, the old one might hang around and be out of data,
252 causing grief. */
253
254struct framework_header {const char * dirName; int dirNameLen; };
255static struct framework_header framework_header_dirs[] = {
256 { "Headers", 7 },
257 { "PrivateHeaders", 14 },
258 { NULL, 0 }
259};
260
261/* Returns a pointer to a malloced string that contains the real pathname
262 to the file, given the base name and the name. */
263
264static char *
265framework_construct_pathname (const char *fname, cpp_dir *dir)
266{
267 char *buf;
268 size_t fname_len, frname_len;
269 cpp_dir *fast_dir;
270 char *frname;
271 struct stat st;
272 int i;
273
274 /* Framework names must have a / in them. */
275 buf = strchr (fname, '/');
276 if (buf)
277 fname_len = buf - fname;
278 else
279 return 0;
280
281 fast_dir = find_framework (fname, fname_len);
282
283 /* Framework includes must all come from one framework. */
284 if (fast_dir && dir != fast_dir)
285 return 0;
286
5ed6ace5 287 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
94d1613b
MS
288 + strlen(".framework/") + strlen("PrivateHeaders"));
289 strncpy (&frname[0], dir->name, dir->len);
290 frname_len = dir->len;
291 if (frname_len && frname[frname_len-1] != '/')
292 frname[frname_len++] = '/';
293 strncpy (&frname[frname_len], fname, fname_len);
294 frname_len += fname_len;
295 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
296 frname_len += strlen (".framework/");
297
a68bdb0b
MS
298 if (fast_dir == 0)
299 {
300 frname[frname_len-1] = 0;
301 if (stat (frname, &st) == 0)
302 {
303 /* As soon as we find the first instance of the framework,
304 we stop and never use any later instance of that
305 framework. */
306 add_framework (fname, fname_len, dir);
307 }
308 else
309 {
310 /* If we can't find the parent directory, no point looking
311 further. */
312 free (frname);
313 return 0;
314 }
315 frname[frname_len-1] = '/';
316 }
317
94d1613b
MS
318 /* Append framework_header_dirs and header file name */
319 for (i = 0; framework_header_dirs[i].dirName; i++)
320 {
16d6f994 321 strncpy (&frname[frname_len],
94d1613b
MS
322 framework_header_dirs[i].dirName,
323 framework_header_dirs[i].dirNameLen);
324 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
325 &fname[fname_len]);
326
327 if (stat (frname, &st) == 0)
a68bdb0b 328 return frname;
94d1613b
MS
329 }
330
331 free (frname);
332 return 0;
333}
334
335/* Search for FNAME in sub-frameworks. pname is the context that we
336 wish to search in. Return the path the file was found at,
337 otherwise return 0. */
338
339static const char*
340find_subframework_file (const char *fname, const char *pname)
341{
342 char *sfrname;
343 const char *dot_framework = ".framework/";
16d6f994
EC
344 char *bufptr;
345 int sfrname_len, i, fname_len;
94d1613b
MS
346 struct cpp_dir *fast_dir;
347 static struct cpp_dir subframe_dir;
348 struct stat st;
349
350 bufptr = strchr (fname, '/');
351
352 /* Subframework files must have / in the name. */
353 if (bufptr == 0)
354 return 0;
16d6f994 355
94d1613b
MS
356 fname_len = bufptr - fname;
357 fast_dir = find_framework (fname, fname_len);
358
359 /* Sub framework header filename includes parent framework name and
360 header name in the "CarbonCore/OSUtils.h" form. If it does not
361 include slash it is not a sub framework include. */
362 bufptr = strstr (pname, dot_framework);
363
364 /* If the parent header is not of any framework, then this header
1e5f1716 365 cannot be part of any subframework. */
94d1613b
MS
366 if (!bufptr)
367 return 0;
368
369 /* Now translate. For example, +- bufptr
16d6f994 370 fname = CarbonCore/OSUtils.h |
94d1613b
MS
371 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
372 into
373 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
374
5ed6ace5 375 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
94d1613b
MS
376 strlen ("Frameworks/") + strlen (".framework/")
377 + strlen ("PrivateHeaders"));
16d6f994 378
94d1613b
MS
379 bufptr += strlen (dot_framework);
380
16d6f994 381 sfrname_len = bufptr - pname;
94d1613b
MS
382
383 strncpy (&sfrname[0], pname, sfrname_len);
384
385 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
386 sfrname_len += strlen("Frameworks/");
387
388 strncpy (&sfrname[sfrname_len], fname, fname_len);
389 sfrname_len += fname_len;
390
391 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
392 sfrname_len += strlen (".framework/");
393
394 /* Append framework_header_dirs and header file name */
395 for (i = 0; framework_header_dirs[i].dirName; i++)
396 {
16d6f994 397 strncpy (&sfrname[sfrname_len],
94d1613b
MS
398 framework_header_dirs[i].dirName,
399 framework_header_dirs[i].dirNameLen);
400 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
401 &fname[fname_len]);
16d6f994 402
94d1613b
MS
403 if (stat (sfrname, &st) == 0)
404 {
405 if (fast_dir != &subframe_dir)
406 {
407 if (fast_dir)
d4ee4d25 408 warning (0, "subframework include %s conflicts with framework include",
94d1613b
MS
409 fname);
410 else
411 add_framework (fname, fname_len, &subframe_dir);
412 }
413
414 return sfrname;
415 }
416 }
417 free (sfrname);
418
419 return 0;
420}
421
422/* Add PATH to the system includes. PATH must be malloc-ed and
423 NUL-terminated. System framework paths are C++ aware. */
424
425static void
426add_system_framework_path (char *path)
427{
428 int cxx_aware = 1;
429 cpp_dir *p;
430
5ed6ace5 431 p = XNEW (cpp_dir);
94d1613b
MS
432 p->next = NULL;
433 p->name = path;
434 p->sysp = 1 + !cxx_aware;
435 p->construct = framework_construct_pathname;
436 using_frameworks = 1;
437
438 add_cpp_dir_path (p, SYSTEM);
439}
440
441/* Add PATH to the bracket includes. PATH must be malloc-ed and
442 NUL-terminated. */
443
444void
445add_framework_path (char *path)
446{
447 cpp_dir *p;
448
5ed6ace5 449 p = XNEW (cpp_dir);
94d1613b
MS
450 p->next = NULL;
451 p->name = path;
452 p->sysp = 0;
453 p->construct = framework_construct_pathname;
454 using_frameworks = 1;
455
456 add_cpp_dir_path (p, BRACKET);
457}
458
16d6f994 459static const char *framework_defaults [] =
94d1613b
MS
460 {
461 "/System/Library/Frameworks",
462 "/Library/Frameworks",
94d1613b
MS
463 };
464
3dd53121
AP
465/* Register the GNU objective-C runtime include path if STDINC. */
466
467void
468darwin_register_objc_includes (const char *sysroot, const char *iprefix,
469 int stdinc)
470{
471 const char *fname;
472 size_t len;
473 /* We do not do anything if we do not want the standard includes. */
474 if (!stdinc)
475 return;
16d6f994 476
3dd53121 477 fname = GCC_INCLUDE_DIR "-gnu-runtime";
16d6f994 478
3dd53121
AP
479 /* Register the GNU OBJC runtime include path if we are compiling OBJC
480 with GNU-runtime. */
481
482 if (c_dialect_objc () && !flag_next_runtime)
483 {
484 char *str;
485 /* See if our directory starts with the standard prefix.
112cdef5 486 "Translate" them, i.e. replace /usr/local/lib/gcc... with
3dd53121
AP
487 IPREFIX and search them first. */
488 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
489 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
490 {
491 str = concat (iprefix, fname + len, NULL);
492 /* FIXME: wrap the headers for C++awareness. */
493 add_path (str, SYSTEM, /*c++aware=*/false, false);
494 }
16d6f994 495
3dd53121
AP
496 /* Should this directory start with the sysroot? */
497 if (sysroot)
498 str = concat (sysroot, fname, NULL);
499 else
500 str = update_path (fname, "");
16d6f994 501
3dd53121
AP
502 add_path (str, SYSTEM, /*c++aware=*/false, false);
503 }
504}
505
94d1613b
MS
506
507/* Register all the system framework paths if STDINC is true and setup
508 the missing_header callback for subframework searching if any
509 frameworks had been registered. */
510
511void
479ec1d1 512darwin_register_frameworks (const char *sysroot,
3dd53121 513 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
94d1613b
MS
514{
515 if (stdinc)
516 {
517 size_t i;
518
519 /* Setup default search path for frameworks. */
520 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
521 {
479ec1d1
DP
522 char *str;
523 if (sysroot)
524 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
525 else
526 str = xstrdup (framework_defaults[i]);
94d1613b 527 /* System Framework headers are cxx aware. */
479ec1d1 528 add_system_framework_path (str);
94d1613b
MS
529 }
530 }
531
532 if (using_frameworks)
533 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
534}
535
536/* Search for HEADER in context dependent way. The return value is
537 the malloced name of a header to try and open, if any, or NULL
538 otherwise. This is called after normal header lookup processing
539 fails to find a header. We search each file in the include stack,
540 using FUNC, starting from the most deeply nested include and
541 finishing with the main input file. We stop searching when FUNC
0e40b5f2 542 returns nonzero. */
94d1613b
MS
543
544static const char*
e3c287c9 545find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
94d1613b
MS
546{
547 const char *fname = header;
548 struct cpp_buffer *b;
549 const char *n;
550
551 for (b = cpp_get_buffer (pfile);
552 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
553 b = cpp_get_prev (b))
554 {
555 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
556 if (n)
e3c287c9
MS
557 {
558 /* Logically, the place where we found the subframework is
559 the place where we found the Framework that contains the
560 subframework. This is useful for tracking wether or not
561 we are in a system header. */
562 *dirp = cpp_get_dir (cpp_get_file (b));
563 return n;
564 }
94d1613b
MS
565 }
566
567 return 0;
568}
ed5b9f96
GK
569
570/* Return the value of darwin_macosx_version_min suitable for the
571 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
16d6f994 572 so '10.4.2' becomes 1042.
ed5b9f96
GK
573 Print a warning if the version number is not known. */
574static const char *
575version_as_macro (void)
576{
577 static char result[] = "1000";
16d6f994 578
ed5b9f96
GK
579 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
580 goto fail;
581 if (! ISDIGIT (darwin_macosx_version_min[3]))
582 goto fail;
583 result[2] = darwin_macosx_version_min[3];
584 if (darwin_macosx_version_min[4] != '\0')
585 {
586 if (darwin_macosx_version_min[4] != '.')
587 goto fail;
588 if (! ISDIGIT (darwin_macosx_version_min[5]))
589 goto fail;
590 if (darwin_macosx_version_min[6] != '\0')
591 goto fail;
592 result[3] = darwin_macosx_version_min[5];
593 }
594 else
595 result[3] = '0';
16d6f994 596
ed5b9f96 597 return result;
16d6f994 598
ed5b9f96
GK
599 fail:
600 error ("Unknown value %qs of -mmacosx-version-min",
601 darwin_macosx_version_min);
602 return "1000";
603}
604
605/* Define additional CPP flags for Darwin. */
606
607#define builtin_define(TXT) cpp_define (pfile, TXT)
608
609void
610darwin_cpp_builtins (cpp_reader *pfile)
611{
612 builtin_define ("__MACH__");
613 builtin_define ("__APPLE__");
614
615 /* __APPLE_CC__ is defined as some old Apple include files expect it
616 to be defined and won't work if it isn't. */
617 builtin_define_with_value ("__APPLE_CC__", "1", false);
618
619 if (darwin_macosx_version_min)
620 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
621 version_as_macro(), false);
622}