OSDN Git Service

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