OSDN Git Service

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