OSDN Git Service

PR target/26427
[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, 2007, 2008, 2010
3    Free Software Foundation, Inc.
4    Contributed by Apple Computer Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "incpath.h"
29 #include "c-family/c-common.h"
30 #include "c-family/c-pragma.h"
31 #include "c-family/c-format.h"
32 #include "diagnostic-core.h"
33 #include "toplev.h"
34 #include "flags.h"
35 #include "tm_p.h"
36 #include "cppdefault.h"
37 #include "prefix.h"
38 #include "target.h"
39 #include "target-def.h"
40
41 /* Pragmas.  */
42
43 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
44 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
45
46 static bool using_frameworks = false;
47
48 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
49                                              cpp_dir **dirp);
50
51 typedef struct align_stack
52 {
53   int alignment;
54   struct align_stack * prev;
55 } align_stack;
56
57 static struct align_stack * field_align_stack = NULL;
58
59 /* Maintain a small stack of alignments.  This is similar to pragma
60    pack's stack, but simpler.  */
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 (OPT_Wpragmas, "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     BAD ("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             {
149               TREE_USED (local) = 1;
150               DECL_READ_P (local) = 1;
151             }
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     BAD ("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     BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
182
183   if (pragma_lex (&t) != CPP_EOF)
184     BAD ("junk at end of '#pragma ms_struct'");
185 }
186
187 static struct frameworks_in_use {
188   size_t len;
189   const char *name;
190   cpp_dir* dir;
191 } *frameworks_in_use;
192 static int num_frameworks = 0;
193 static int max_frameworks = 0;
194
195
196 /* Remember which frameworks have been seen, so that we can ensure
197    that all uses of that framework come from the same framework.  DIR
198    is the place where the named framework NAME, which is of length
199    LEN, was found.  We copy the directory name from NAME, as it will be
200    freed by others.  */
201
202 static void
203 add_framework (const char *name, size_t len, cpp_dir *dir)
204 {
205   char *dir_name;
206   int i;
207   for (i = 0; i < num_frameworks; ++i)
208     {
209       if (len == frameworks_in_use[i].len
210           && strncmp (name, frameworks_in_use[i].name, len) == 0)
211         {
212           return;
213         }
214     }
215   if (i >= max_frameworks)
216     {
217       max_frameworks = i*2;
218       max_frameworks += i == 0;
219       frameworks_in_use = XRESIZEVEC (struct frameworks_in_use,
220                                       frameworks_in_use, max_frameworks);
221     }
222   dir_name = XNEWVEC (char, len + 1);
223   memcpy (dir_name, name, len);
224   dir_name[len] = '\0';
225   frameworks_in_use[num_frameworks].name = dir_name;
226   frameworks_in_use[num_frameworks].len = len;
227   frameworks_in_use[num_frameworks].dir = dir;
228   ++num_frameworks;
229 }
230
231 /* Recall if we have seen the named framework NAME, before, and where
232    we saw it.  NAME is LEN bytes long.  The return value is the place
233    where it was seen before.  */
234
235 static struct cpp_dir*
236 find_framework (const char *name, size_t len)
237 {
238   int i;
239   for (i = 0; i < num_frameworks; ++i)
240     {
241       if (len == frameworks_in_use[i].len
242           && strncmp (name, frameworks_in_use[i].name, len) == 0)
243         {
244           return frameworks_in_use[i].dir;
245         }
246     }
247   return 0;
248 }
249
250 /* There are two directories in a framework that contain header files,
251    Headers and PrivateHeaders.  We search Headers first as it is more
252    common to upgrade a header from PrivateHeaders to Headers and when
253    that is done, the old one might hang around and be out of data,
254    causing grief.  */
255
256 struct framework_header {const char * dirName; int dirNameLen; };
257 static struct framework_header framework_header_dirs[] = {
258   { "Headers", 7 },
259   { "PrivateHeaders", 14 },
260   { NULL, 0 }
261 };
262
263 /* Returns a pointer to a malloced string that contains the real pathname
264    to the file, given the base name and the name.  */
265
266 static char *
267 framework_construct_pathname (const char *fname, cpp_dir *dir)
268 {
269   char *buf;
270   size_t fname_len, frname_len;
271   cpp_dir *fast_dir;
272   char *frname;
273   struct stat st;
274   int i;
275
276   /* Framework names must have a / in them.  */
277   buf = strchr (fname, '/');
278   if (buf)
279     fname_len = buf - fname;
280   else
281     return 0;
282
283   fast_dir = find_framework (fname, fname_len);
284
285   /* Framework includes must all come from one framework.  */
286   if (fast_dir && dir != fast_dir)
287     return 0;
288
289   frname = XNEWVEC (char, strlen (fname) + dir->len + 2
290                     + strlen(".framework/") + strlen("PrivateHeaders"));
291   strncpy (&frname[0], dir->name, dir->len);
292   frname_len = dir->len;
293   if (frname_len && frname[frname_len-1] != '/')
294     frname[frname_len++] = '/';
295   strncpy (&frname[frname_len], fname, fname_len);
296   frname_len += fname_len;
297   strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
298   frname_len += strlen (".framework/");
299
300   if (fast_dir == 0)
301     {
302       frname[frname_len-1] = 0;
303       if (stat (frname, &st) == 0)
304         {
305           /* As soon as we find the first instance of the framework,
306              we stop and never use any later instance of that
307              framework.  */
308           add_framework (fname, fname_len, dir);
309         }
310       else
311         {
312           /* If we can't find the parent directory, no point looking
313              further.  */
314           free (frname);
315           return 0;
316         }
317       frname[frname_len-1] = '/';
318     }
319
320   /* Append framework_header_dirs and header file name */
321   for (i = 0; framework_header_dirs[i].dirName; i++)
322     {
323       strncpy (&frname[frname_len],
324                framework_header_dirs[i].dirName,
325                framework_header_dirs[i].dirNameLen);
326       strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
327               &fname[fname_len]);
328
329       if (stat (frname, &st) == 0)
330         return frname;
331     }
332
333   free (frname);
334   return 0;
335 }
336
337 /* Search for FNAME in sub-frameworks.  pname is the context that we
338    wish to search in.  Return the path the file was found at,
339    otherwise return 0.  */
340
341 static const char*
342 find_subframework_file (const char *fname, const char *pname)
343 {
344   char *sfrname;
345   const char *dot_framework = ".framework/";
346   char *bufptr;
347   int sfrname_len, i, fname_len;
348   struct cpp_dir *fast_dir;
349   static struct cpp_dir subframe_dir;
350   struct stat st;
351
352   bufptr = strchr (fname, '/');
353
354   /* Subframework files must have / in the name.  */
355   if (bufptr == 0)
356     return 0;
357
358   fname_len = bufptr - fname;
359   fast_dir = find_framework (fname, fname_len);
360
361   /* Sub framework header filename includes parent framework name and
362      header name in the "CarbonCore/OSUtils.h" form. If it does not
363      include slash it is not a sub framework include.  */
364   bufptr = strstr (pname, dot_framework);
365
366   /* If the parent header is not of any framework, then this header
367      cannot be part of any subframework.  */
368   if (!bufptr)
369     return 0;
370
371   /* Now translate. For example,                  +- bufptr
372      fname = CarbonCore/OSUtils.h                 |
373      pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
374      into
375      sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
376
377   sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
378                               strlen ("Frameworks/") + strlen (".framework/")
379                               + strlen ("PrivateHeaders"));
380
381   bufptr += strlen (dot_framework);
382
383   sfrname_len = bufptr - pname;
384
385   strncpy (&sfrname[0], pname, sfrname_len);
386
387   strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
388   sfrname_len += strlen("Frameworks/");
389
390   strncpy (&sfrname[sfrname_len], fname, fname_len);
391   sfrname_len += fname_len;
392
393   strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
394   sfrname_len += strlen (".framework/");
395
396   /* Append framework_header_dirs and header file name */
397   for (i = 0; framework_header_dirs[i].dirName; i++)
398     {
399       strncpy (&sfrname[sfrname_len],
400                framework_header_dirs[i].dirName,
401                framework_header_dirs[i].dirNameLen);
402       strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
403               &fname[fname_len]);
404
405       if (stat (sfrname, &st) == 0)
406         {
407           if (fast_dir != &subframe_dir)
408             {
409               if (fast_dir)
410                 warning (0, "subframework include %s conflicts with framework include",
411                          fname);
412               else
413                 add_framework (fname, fname_len, &subframe_dir);
414             }
415
416           return sfrname;
417         }
418     }
419   free (sfrname);
420
421   return 0;
422 }
423
424 /* Add PATH to the system includes. PATH must be malloc-ed and
425    NUL-terminated.  System framework paths are C++ aware.  */
426
427 static void
428 add_system_framework_path (char *path)
429 {
430   int cxx_aware = 1;
431   cpp_dir *p;
432
433   p = XNEW (cpp_dir);
434   p->next = NULL;
435   p->name = path;
436   p->sysp = 1 + !cxx_aware;
437   p->construct = framework_construct_pathname;
438   using_frameworks = 1;
439
440   add_cpp_dir_path (p, SYSTEM);
441 }
442
443 /* Add PATH to the bracket includes. PATH must be malloc-ed and
444    NUL-terminated.  */
445
446 void
447 add_framework_path (char *path)
448 {
449   cpp_dir *p;
450
451   p = XNEW (cpp_dir);
452   p->next = NULL;
453   p->name = path;
454   p->sysp = 0;
455   p->construct = framework_construct_pathname;
456   using_frameworks = 1;
457
458   add_cpp_dir_path (p, BRACKET);
459 }
460
461 static const char *framework_defaults [] =
462   {
463     "/System/Library/Frameworks",
464     "/Library/Frameworks",
465   };
466
467 /* Register the GNU objective-C runtime include path if STDINC.  */
468
469 void
470 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
471                                int stdinc)
472 {
473   const char *fname;
474   size_t len;
475   /* We do not do anything if we do not want the standard includes. */
476   if (!stdinc)
477     return;
478
479   fname = GCC_INCLUDE_DIR "-gnu-runtime";
480
481   /* Register the GNU OBJC runtime include path if we are compiling  OBJC
482     with GNU-runtime.  */
483
484   if (c_dialect_objc () && !flag_next_runtime)
485     {
486       char *str;
487       /* See if our directory starts with the standard prefix.
488          "Translate" them, i.e. replace /usr/local/lib/gcc... with
489          IPREFIX and search them first.  */
490       if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
491           && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
492         {
493           str = concat (iprefix, fname + len, NULL);
494           /* FIXME: wrap the headers for C++awareness.  */
495           add_path (str, SYSTEM, /*c++aware=*/false, false);
496         }
497
498       /* Should this directory start with the sysroot?  */
499       if (sysroot)
500         str = concat (sysroot, fname, NULL);
501       else
502         str = update_path (fname, "");
503
504       add_path (str, SYSTEM, /*c++aware=*/false, false);
505     }
506 }
507
508
509 /* Register all the system framework paths if STDINC is true and setup
510    the missing_header callback for subframework searching if any
511    frameworks had been registered.  */
512
513 void
514 darwin_register_frameworks (const char *sysroot,
515                             const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
516 {
517   if (stdinc)
518     {
519       size_t i;
520
521       /* Setup default search path for frameworks.  */
522       for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
523         {
524           char *str;
525           if (sysroot)
526             str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
527           else
528             str = xstrdup (framework_defaults[i]);
529           /* System Framework headers are cxx aware.  */
530           add_system_framework_path (str);
531         }
532     }
533
534   if (using_frameworks)
535     cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
536 }
537
538 /* Search for HEADER in context dependent way.  The return value is
539    the malloced name of a header to try and open, if any, or NULL
540    otherwise.  This is called after normal header lookup processing
541    fails to find a header.  We search each file in the include stack,
542    using FUNC, starting from the most deeply nested include and
543    finishing with the main input file.  We stop searching when FUNC
544    returns nonzero.  */
545
546 static const char*
547 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
548 {
549   const char *fname = header;
550   struct cpp_buffer *b;
551   const char *n;
552
553   for (b = cpp_get_buffer (pfile);
554        b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
555        b = cpp_get_prev (b))
556     {
557       n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
558       if (n)
559         {
560           /* Logically, the place where we found the subframework is
561              the place where we found the Framework that contains the
562              subframework.  This is useful for tracking wether or not
563              we are in a system header.  */
564           *dirp = cpp_get_dir (cpp_get_file (b));
565           return n;
566         }
567     }
568
569   return 0;
570 }
571
572 /* Return the value of darwin_macosx_version_min suitable for the
573    __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro,
574    so '10.4.2' becomes 1040.  The lowest digit is always zero.
575    Print a warning if the version number can't be understood.  */
576 static const char *
577 version_as_macro (void)
578 {
579   static char result[] = "1000";
580
581   if (strncmp (darwin_macosx_version_min, "10.", 3) != 0)
582     goto fail;
583   if (! ISDIGIT (darwin_macosx_version_min[3]))
584     goto fail;
585   result[2] = darwin_macosx_version_min[3];
586   if (darwin_macosx_version_min[4] != '\0'
587       && darwin_macosx_version_min[4] != '.')
588     goto fail;
589
590   return result;
591
592  fail:
593   error ("unknown value %qs of -mmacosx-version-min",
594          darwin_macosx_version_min);
595   return "1000";
596 }
597
598 /* Define additional CPP flags for Darwin.   */
599
600 #define builtin_define(TXT) cpp_define (pfile, TXT)
601
602 void
603 darwin_cpp_builtins (cpp_reader *pfile)
604 {
605   builtin_define ("__MACH__");
606   builtin_define ("__APPLE__");
607
608   /* __APPLE_CC__ is defined as some old Apple include files expect it
609      to be defined and won't work if it isn't.  */
610   builtin_define_with_value ("__APPLE_CC__", "1", false);
611
612   if (darwin_constant_cfstrings)
613     builtin_define ("__CONSTANT_CFSTRINGS__");
614
615   builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
616                              version_as_macro(), false);
617
618   /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
619      following will cause a syntax error if one tries to compile gc attributed
620      items.  However, without this, NeXT system headers cannot be parsed 
621      properly (on systems >= darwin 9).  */
622   if (flag_objc_gc)
623     {
624       builtin_define ("__strong=__attribute__((objc_gc(strong)))");
625       builtin_define ("__weak=__attribute__((objc_gc(weak)))");
626       builtin_define ("__OBJC_GC__");
627     }
628   else
629     {
630       builtin_define ("__strong=");
631       builtin_define ("__weak=");
632     }
633 }
634
635 /* Handle C family front-end options.  */
636
637 static bool
638 handle_c_option (size_t code,
639                  const char *arg,
640                  int value ATTRIBUTE_UNUSED)
641 {
642   switch (code)
643     {
644     default:
645       /* Unrecognized options that we said we'd handle turn into
646          errors if not listed here.  */
647       return false;
648
649     case OPT_iframework:
650       add_system_framework_path (xstrdup (arg));
651       break;
652
653     case OPT_fapple_kext:
654       ;
655     }
656
657   /* We recognized the option.  */
658   return true;
659 }
660
661 #undef TARGET_HANDLE_C_OPTION
662 #define TARGET_HANDLE_C_OPTION handle_c_option
663
664 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;
665
666 /* Allow ObjC* access to CFStrings.  */
667 tree
668 darwin_objc_construct_string (tree str)
669 {
670   if (!darwin_constant_cfstrings)
671     {
672     /* Even though we are not using CFStrings, place our literal
673        into the cfstring_htab hash table, so that the
674        darwin_constant_cfstring_p() function will see it.  */
675       darwin_enter_string_into_cfstring_table (str);
676       /* Fall back to NSConstantString.  */
677       return NULL_TREE;
678     }
679
680   return darwin_build_constant_cfstring (str);
681 }
682
683 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
684    must match for it explicitly, since it's outside the gcc code.  */
685
686 bool
687 darwin_cfstring_ref_p (const_tree strp)
688 {
689   tree tn;
690   if (!strp || TREE_CODE (strp) != POINTER_TYPE)
691     return false;
692
693   tn = TYPE_NAME (strp);
694   if (tn) 
695     tn = DECL_NAME (tn);
696   return (tn 
697           && IDENTIFIER_POINTER (tn)
698           && !strncmp (IDENTIFIER_POINTER (tn), "CFStringRef", 8));
699 }
700
701 /* At present the behavior of this is undefined and it does nothing.  */
702 void
703 darwin_check_cfstring_format_arg (tree ARG_UNUSED (format_arg), 
704                                   tree ARG_UNUSED (args_list))
705 {
706 }
707
708 /* The extra format types we recognize.  */
709 EXPORTED_CONST format_kind_info darwin_additional_format_types[] = {
710   { "CFString",   NULL,  NULL, NULL, NULL, 
711     NULL, NULL, 
712     FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
713     NULL, NULL
714   }
715 };