* config/arm/arm.c (arm_legitimize_address): Limit the value passed
[official-gcc.git] / gcc / config / darwin-c.c
blob738f07c81a730c9821348b534e6dd03bfc277c2c
1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001, 2003, 2004, 2005 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 2, or (at your option)
10 any later version.
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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
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 "c-pragma.h"
29 #include "c-tree.h"
30 #include "c-incpath.h"
31 #include "toplev.h"
32 #include "flags.h"
33 #include "tm_p.h"
34 #include "cppdefault.h"
35 #include "prefix.h"
37 /* Pragmas. */
39 #define BAD(gmsgid) do { warning (0, gmsgid); return; } while (0)
41 static bool using_frameworks = false;
43 /* Maintain a small stack of alignments. This is similar to pragma
44 pack's stack, but simpler. */
46 static void push_field_alignment (int);
47 static void pop_field_alignment (void);
48 static const char *find_subframework_file (const char *, const char *);
49 static void add_system_framework_path (char *);
50 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
51 cpp_dir **dirp);
53 typedef struct align_stack
55 int alignment;
56 struct align_stack * prev;
57 } align_stack;
59 static struct align_stack * field_align_stack = NULL;
61 static void
62 push_field_alignment (int bit_alignment)
64 align_stack *entry = (align_stack *) xmalloc (sizeof (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 (c_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 (c_lex (&t) != CPP_EQ)
110 BAD ("malformed '#pragma options', ignoring");
111 if (c_lex (&t) != CPP_NAME)
112 BAD ("malformed '#pragma options', ignoring");
114 if (c_lex (&x) != CPP_EOF)
115 warning (0, "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 warning (0, "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 (c_lex (&x) != CPP_OPEN_PAREN)
137 BAD ("missing '(' after '#pragma unused', ignoring");
139 while (1)
141 tok = c_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))
147 TREE_USED (local) = 1;
148 tok = c_lex (&x);
149 if (tok != CPP_COMMA)
150 break;
154 if (tok != CPP_CLOSE_PAREN)
155 BAD ("missing ')' after '#pragma unused', ignoring");
157 if (c_lex (&x) != CPP_EOF)
158 warning (0, "junk at end of '#pragma unused'");
161 static struct {
162 size_t len;
163 const char *name;
164 cpp_dir* dir;
165 } *frameworks_in_use;
166 static int num_frameworks = 0;
167 static int max_frameworks = 0;
170 /* Remember which frameworks have been seen, so that we can ensure
171 that all uses of that framework come from the same framework. DIR
172 is the place where the named framework NAME, which is of length
173 LEN, was found. We copy the directory name from NAME, as it will be
174 freed by others. */
176 static void
177 add_framework (const char *name, size_t len, cpp_dir *dir)
179 char *dir_name;
180 int i;
181 for (i = 0; i < num_frameworks; ++i)
183 if (len == frameworks_in_use[i].len
184 && strncmp (name, frameworks_in_use[i].name, len) == 0)
186 return;
189 if (i >= max_frameworks)
191 max_frameworks = i*2;
192 max_frameworks += i == 0;
193 frameworks_in_use = xrealloc (frameworks_in_use,
194 max_frameworks*sizeof(*frameworks_in_use));
196 dir_name = xmalloc (len + 1);
197 memcpy (dir_name, name, len);
198 dir_name[len] = '\0';
199 frameworks_in_use[num_frameworks].name = dir_name;
200 frameworks_in_use[num_frameworks].len = len;
201 frameworks_in_use[num_frameworks].dir = dir;
202 ++num_frameworks;
205 /* Recall if we have seen the named framework NAME, before, and where
206 we saw it. NAME is LEN bytes long. The return value is the place
207 where it was seen before. */
209 static struct cpp_dir*
210 find_framework (const char *name, size_t len)
212 int i;
213 for (i = 0; i < num_frameworks; ++i)
215 if (len == frameworks_in_use[i].len
216 && strncmp (name, frameworks_in_use[i].name, len) == 0)
218 return frameworks_in_use[i].dir;
221 return 0;
224 /* There are two directories in a framework that contain header files,
225 Headers and PrivateHeaders. We search Headers first as it is more
226 common to upgrade a header from PrivateHeaders to Headers and when
227 that is done, the old one might hang around and be out of data,
228 causing grief. */
230 struct framework_header {const char * dirName; int dirNameLen; };
231 static struct framework_header framework_header_dirs[] = {
232 { "Headers", 7 },
233 { "PrivateHeaders", 14 },
234 { NULL, 0 }
237 /* Returns a pointer to a malloced string that contains the real pathname
238 to the file, given the base name and the name. */
240 static char *
241 framework_construct_pathname (const char *fname, cpp_dir *dir)
243 char *buf;
244 size_t fname_len, frname_len;
245 cpp_dir *fast_dir;
246 char *frname;
247 struct stat st;
248 int i;
250 /* Framework names must have a / in them. */
251 buf = strchr (fname, '/');
252 if (buf)
253 fname_len = buf - fname;
254 else
255 return 0;
257 fast_dir = find_framework (fname, fname_len);
259 /* Framework includes must all come from one framework. */
260 if (fast_dir && dir != fast_dir)
261 return 0;
263 frname = xmalloc (strlen (fname) + dir->len + 2
264 + strlen(".framework/") + strlen("PrivateHeaders"));
265 strncpy (&frname[0], dir->name, dir->len);
266 frname_len = dir->len;
267 if (frname_len && frname[frname_len-1] != '/')
268 frname[frname_len++] = '/';
269 strncpy (&frname[frname_len], fname, fname_len);
270 frname_len += fname_len;
271 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
272 frname_len += strlen (".framework/");
274 if (fast_dir == 0)
276 frname[frname_len-1] = 0;
277 if (stat (frname, &st) == 0)
279 /* As soon as we find the first instance of the framework,
280 we stop and never use any later instance of that
281 framework. */
282 add_framework (fname, fname_len, dir);
284 else
286 /* If we can't find the parent directory, no point looking
287 further. */
288 free (frname);
289 return 0;
291 frname[frname_len-1] = '/';
294 /* Append framework_header_dirs and header file name */
295 for (i = 0; framework_header_dirs[i].dirName; i++)
297 strncpy (&frname[frname_len],
298 framework_header_dirs[i].dirName,
299 framework_header_dirs[i].dirNameLen);
300 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
301 &fname[fname_len]);
303 if (stat (frname, &st) == 0)
304 return frname;
307 free (frname);
308 return 0;
311 /* Search for FNAME in sub-frameworks. pname is the context that we
312 wish to search in. Return the path the file was found at,
313 otherwise return 0. */
315 static const char*
316 find_subframework_file (const char *fname, const char *pname)
318 char *sfrname;
319 const char *dot_framework = ".framework/";
320 char *bufptr;
321 int sfrname_len, i, fname_len;
322 struct cpp_dir *fast_dir;
323 static struct cpp_dir subframe_dir;
324 struct stat st;
326 bufptr = strchr (fname, '/');
328 /* Subframework files must have / in the name. */
329 if (bufptr == 0)
330 return 0;
332 fname_len = bufptr - fname;
333 fast_dir = find_framework (fname, fname_len);
335 /* Sub framework header filename includes parent framework name and
336 header name in the "CarbonCore/OSUtils.h" form. If it does not
337 include slash it is not a sub framework include. */
338 bufptr = strstr (pname, dot_framework);
340 /* If the parent header is not of any framework, then this header
341 cannot be part of any subframework. */
342 if (!bufptr)
343 return 0;
345 /* Now translate. For example, +- bufptr
346 fname = CarbonCore/OSUtils.h |
347 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
348 into
349 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
351 sfrname = (char *) xmalloc (strlen (pname) + strlen (fname) + 2 +
352 strlen ("Frameworks/") + strlen (".framework/")
353 + strlen ("PrivateHeaders"));
355 bufptr += strlen (dot_framework);
357 sfrname_len = bufptr - pname;
359 strncpy (&sfrname[0], pname, sfrname_len);
361 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
362 sfrname_len += strlen("Frameworks/");
364 strncpy (&sfrname[sfrname_len], fname, fname_len);
365 sfrname_len += fname_len;
367 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
368 sfrname_len += strlen (".framework/");
370 /* Append framework_header_dirs and header file name */
371 for (i = 0; framework_header_dirs[i].dirName; i++)
373 strncpy (&sfrname[sfrname_len],
374 framework_header_dirs[i].dirName,
375 framework_header_dirs[i].dirNameLen);
376 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
377 &fname[fname_len]);
379 if (stat (sfrname, &st) == 0)
381 if (fast_dir != &subframe_dir)
383 if (fast_dir)
384 warning (0, "subframework include %s conflicts with framework include",
385 fname);
386 else
387 add_framework (fname, fname_len, &subframe_dir);
390 return sfrname;
393 free (sfrname);
395 return 0;
398 /* Add PATH to the system includes. PATH must be malloc-ed and
399 NUL-terminated. System framework paths are C++ aware. */
401 static void
402 add_system_framework_path (char *path)
404 int cxx_aware = 1;
405 cpp_dir *p;
407 p = xmalloc (sizeof (cpp_dir));
408 p->next = NULL;
409 p->name = path;
410 p->sysp = 1 + !cxx_aware;
411 p->construct = framework_construct_pathname;
412 using_frameworks = 1;
414 add_cpp_dir_path (p, SYSTEM);
417 /* Add PATH to the bracket includes. PATH must be malloc-ed and
418 NUL-terminated. */
420 void
421 add_framework_path (char *path)
423 cpp_dir *p;
425 p = xmalloc (sizeof (cpp_dir));
426 p->next = NULL;
427 p->name = path;
428 p->sysp = 0;
429 p->construct = framework_construct_pathname;
430 using_frameworks = 1;
432 add_cpp_dir_path (p, BRACKET);
435 static const char *framework_defaults [] =
437 "/System/Library/Frameworks",
438 "/Library/Frameworks",
441 /* Register the GNU objective-C runtime include path if STDINC. */
443 void
444 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
445 int stdinc)
447 const char *fname;
448 size_t len;
449 /* We do not do anything if we do not want the standard includes. */
450 if (!stdinc)
451 return;
453 fname = GCC_INCLUDE_DIR "-gnu-runtime";
455 /* Register the GNU OBJC runtime include path if we are compiling OBJC
456 with GNU-runtime. */
458 if (c_dialect_objc () && !flag_next_runtime)
460 char *str;
461 /* See if our directory starts with the standard prefix.
462 "Translate" them, i.e. replace /usr/local/lib/gcc... with
463 IPREFIX and search them first. */
464 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
465 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
467 str = concat (iprefix, fname + len, NULL);
468 /* FIXME: wrap the headers for C++awareness. */
469 add_path (str, SYSTEM, /*c++aware=*/false, false);
472 /* Should this directory start with the sysroot? */
473 if (sysroot)
474 str = concat (sysroot, fname, NULL);
475 else
476 str = update_path (fname, "");
478 add_path (str, SYSTEM, /*c++aware=*/false, false);
483 /* Register all the system framework paths if STDINC is true and setup
484 the missing_header callback for subframework searching if any
485 frameworks had been registered. */
487 void
488 darwin_register_frameworks (const char *sysroot,
489 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
491 if (stdinc)
493 size_t i;
495 /* Setup default search path for frameworks. */
496 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
498 char *str;
499 if (sysroot)
500 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
501 else
502 str = xstrdup (framework_defaults[i]);
503 /* System Framework headers are cxx aware. */
504 add_system_framework_path (str);
508 if (using_frameworks)
509 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
512 /* Search for HEADER in context dependent way. The return value is
513 the malloced name of a header to try and open, if any, or NULL
514 otherwise. This is called after normal header lookup processing
515 fails to find a header. We search each file in the include stack,
516 using FUNC, starting from the most deeply nested include and
517 finishing with the main input file. We stop searching when FUNC
518 returns nonzero. */
520 static const char*
521 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
523 const char *fname = header;
524 struct cpp_buffer *b;
525 const char *n;
527 for (b = cpp_get_buffer (pfile);
528 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
529 b = cpp_get_prev (b))
531 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
532 if (n)
534 /* Logically, the place where we found the subframework is
535 the place where we found the Framework that contains the
536 subframework. This is useful for tracking wether or not
537 we are in a system header. */
538 *dirp = cpp_get_dir (cpp_get_file (b));
539 return n;
543 return 0;
546 /* Return the value of darwin_macosx_version_min suitable for the
547 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
548 so '10.4.2' becomes 1042.
549 Print a warning if the version number is not known. */
550 static const char *
551 version_as_macro (void)
553 static char result[] = "1000";
555 if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
556 goto fail;
557 if (! ISDIGIT (darwin_macosx_version_min[3]))
558 goto fail;
559 result[2] = darwin_macosx_version_min[3];
560 if (darwin_macosx_version_min[4] != '\0')
562 if (darwin_macosx_version_min[4] != '.')
563 goto fail;
564 if (! ISDIGIT (darwin_macosx_version_min[5]))
565 goto fail;
566 if (darwin_macosx_version_min[6] != '\0')
567 goto fail;
568 result[3] = darwin_macosx_version_min[5];
570 else
571 result[3] = '0';
573 return result;
575 fail:
576 error ("Unknown value %qs of -mmacosx-version-min",
577 darwin_macosx_version_min);
578 return "1000";
581 /* Define additional CPP flags for Darwin. */
583 #define builtin_define(TXT) cpp_define (pfile, TXT)
585 void
586 darwin_cpp_builtins (cpp_reader *pfile)
588 builtin_define ("__MACH__");
589 builtin_define ("__APPLE__");
591 /* __APPLE_CC__ is defined as some old Apple include files expect it
592 to be defined and won't work if it isn't. */
593 builtin_define_with_value ("__APPLE_CC__", "1", false);
595 if (darwin_macosx_version_min)
596 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
597 version_as_macro(), false);