* config/ia64/ia64.md: Define new attribute "empty".
[official-gcc.git] / gcc / config / darwin-c.c
blob281d166868544bc832e60d4a164f1aaab022e713
1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001, 2003, 2004 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 "tm_p.h"
34 /* Pragmas. */
36 #define BAD(msgid) do { warning (msgid); return; } while (0)
38 static bool using_frameworks = false;
40 /* Maintain a small stack of alignments. This is similar to pragma
41 pack's stack, but simpler. */
43 static void push_field_alignment (int);
44 static void pop_field_alignment (void);
45 static const char *find_subframework_file (const char *, const char *);
46 static void add_system_framework_path (char *);
47 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
48 cpp_dir **dirp);
50 typedef struct align_stack
52 int alignment;
53 struct align_stack * prev;
54 } align_stack;
56 static struct align_stack * field_align_stack = NULL;
58 static void
59 push_field_alignment (int bit_alignment)
61 align_stack *entry = (align_stack *) xmalloc (sizeof (align_stack));
63 entry->alignment = maximum_field_alignment;
64 entry->prev = field_align_stack;
65 field_align_stack = entry;
67 maximum_field_alignment = bit_alignment;
70 static void
71 pop_field_alignment (void)
73 if (field_align_stack)
75 align_stack *entry = field_align_stack;
77 maximum_field_alignment = entry->alignment;
78 field_align_stack = entry->prev;
79 free (entry);
81 else
82 error ("too many #pragma options align=reset");
85 /* Handlers for Darwin-specific pragmas. */
87 void
88 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
90 /* Do nothing. */
93 /* #pragma options align={mac68k|power|reset} */
95 void
96 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
98 const char *arg;
99 tree t, x;
101 if (c_lex (&t) != CPP_NAME)
102 BAD ("malformed '#pragma options', ignoring");
103 arg = IDENTIFIER_POINTER (t);
104 if (strcmp (arg, "align"))
105 BAD ("malformed '#pragma options', ignoring");
106 if (c_lex (&t) != CPP_EQ)
107 BAD ("malformed '#pragma options', ignoring");
108 if (c_lex (&t) != CPP_NAME)
109 BAD ("malformed '#pragma options', ignoring");
111 if (c_lex (&x) != CPP_EOF)
112 warning ("junk at end of '#pragma options'");
114 arg = IDENTIFIER_POINTER (t);
115 if (!strcmp (arg, "mac68k"))
116 push_field_alignment (16);
117 else if (!strcmp (arg, "power"))
118 push_field_alignment (0);
119 else if (!strcmp (arg, "reset"))
120 pop_field_alignment ();
121 else
122 warning ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
125 /* #pragma unused ([var {, var}*]) */
127 void
128 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
130 tree decl, x;
131 int tok;
133 if (c_lex (&x) != CPP_OPEN_PAREN)
134 BAD ("missing '(' after '#pragma unused', ignoring");
136 while (1)
138 tok = c_lex (&decl);
139 if (tok == CPP_NAME && decl)
141 tree local = lookup_name (decl);
142 if (local && (TREE_CODE (local) == PARM_DECL
143 || TREE_CODE (local) == VAR_DECL))
144 TREE_USED (local) = 1;
145 tok = c_lex (&x);
146 if (tok != CPP_COMMA)
147 break;
151 if (tok != CPP_CLOSE_PAREN)
152 BAD ("missing ')' after '#pragma unused', ignoring");
154 if (c_lex (&x) != CPP_EOF)
155 warning ("junk at end of '#pragma unused'");
158 static struct {
159 size_t len;
160 const char *name;
161 cpp_dir* dir;
162 } *frameworks_in_use;
163 static int num_frameworks = 0;
164 static int max_frameworks = 0;
167 /* Remember which frameworks have been seen, so that we can ensure
168 that all uses of that framework come from the same framework. DIR
169 is the place where the named framework NAME, which is of length
170 LEN, was found. We copy the directory name from NAME, as it will be
171 freed by others. */
173 static void
174 add_framework (const char *name, size_t len, cpp_dir *dir)
176 char *dir_name;
177 int i;
178 for (i = 0; i < num_frameworks; ++i)
180 if (len == frameworks_in_use[i].len
181 && strncmp (name, frameworks_in_use[i].name, len) == 0)
183 return;
186 if (i >= max_frameworks)
188 max_frameworks = i*2;
189 max_frameworks += i == 0;
190 frameworks_in_use = xrealloc (frameworks_in_use,
191 max_frameworks*sizeof(*frameworks_in_use));
193 dir_name = xmalloc (len + 1);
194 memcpy (dir_name, name, len);
195 dir_name[len] = '\0';
196 frameworks_in_use[num_frameworks].name = dir_name;
197 frameworks_in_use[num_frameworks].len = len;
198 frameworks_in_use[num_frameworks].dir = dir;
199 ++num_frameworks;
202 /* Recall if we have seen the named framework NAME, before, and where
203 we saw it. NAME is LEN bytes long. The return value is the place
204 where it was seen before. */
206 static struct cpp_dir*
207 find_framework (const char *name, size_t len)
209 int i;
210 for (i = 0; i < num_frameworks; ++i)
212 if (len == frameworks_in_use[i].len
213 && strncmp (name, frameworks_in_use[i].name, len) == 0)
215 return frameworks_in_use[i].dir;
218 return 0;
221 /* There are two directories in a framework that contain header files,
222 Headers and PrivateHeaders. We search Headers first as it is more
223 common to upgrade a header from PrivateHeaders to Headers and when
224 that is done, the old one might hang around and be out of data,
225 causing grief. */
227 struct framework_header {const char * dirName; int dirNameLen; };
228 static struct framework_header framework_header_dirs[] = {
229 { "Headers", 7 },
230 { "PrivateHeaders", 14 },
231 { NULL, 0 }
234 /* Returns a pointer to a malloced string that contains the real pathname
235 to the file, given the base name and the name. */
237 static char *
238 framework_construct_pathname (const char *fname, cpp_dir *dir)
240 char *buf;
241 size_t fname_len, frname_len;
242 cpp_dir *fast_dir;
243 char *frname;
244 struct stat st;
245 int i;
247 /* Framework names must have a / in them. */
248 buf = strchr (fname, '/');
249 if (buf)
250 fname_len = buf - fname;
251 else
252 return 0;
254 fast_dir = find_framework (fname, fname_len);
256 /* Framework includes must all come from one framework. */
257 if (fast_dir && dir != fast_dir)
258 return 0;
260 frname = xmalloc (strlen (fname) + dir->len + 2
261 + strlen(".framework/") + strlen("PrivateHeaders"));
262 strncpy (&frname[0], dir->name, dir->len);
263 frname_len = dir->len;
264 if (frname_len && frname[frname_len-1] != '/')
265 frname[frname_len++] = '/';
266 strncpy (&frname[frname_len], fname, fname_len);
267 frname_len += fname_len;
268 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
269 frname_len += strlen (".framework/");
271 /* Append framework_header_dirs and header file name */
272 for (i = 0; framework_header_dirs[i].dirName; i++)
274 strncpy (&frname[frname_len],
275 framework_header_dirs[i].dirName,
276 framework_header_dirs[i].dirNameLen);
277 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
278 &fname[fname_len]);
280 if (stat (frname, &st) == 0)
282 if (fast_dir == 0)
283 add_framework (fname, fname_len, dir);
284 return frname;
288 free (frname);
289 return 0;
292 /* Search for FNAME in sub-frameworks. pname is the context that we
293 wish to search in. Return the path the file was found at,
294 otherwise return 0. */
296 static const char*
297 find_subframework_file (const char *fname, const char *pname)
299 char *sfrname;
300 const char *dot_framework = ".framework/";
301 char *bufptr;
302 int sfrname_len, i, fname_len;
303 struct cpp_dir *fast_dir;
304 static struct cpp_dir subframe_dir;
305 struct stat st;
307 bufptr = strchr (fname, '/');
309 /* Subframework files must have / in the name. */
310 if (bufptr == 0)
311 return 0;
313 fname_len = bufptr - fname;
314 fast_dir = find_framework (fname, fname_len);
316 /* Sub framework header filename includes parent framework name and
317 header name in the "CarbonCore/OSUtils.h" form. If it does not
318 include slash it is not a sub framework include. */
319 bufptr = strstr (pname, dot_framework);
321 /* If the parent header is not of any framework, then this header
322 can not be part of any subframework. */
323 if (!bufptr)
324 return 0;
326 /* Now translate. For example, +- bufptr
327 fname = CarbonCore/OSUtils.h |
328 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
329 into
330 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
332 sfrname = (char *) xmalloc (strlen (pname) + strlen (fname) + 2 +
333 strlen ("Frameworks/") + strlen (".framework/")
334 + strlen ("PrivateHeaders"));
336 bufptr += strlen (dot_framework);
338 sfrname_len = bufptr - pname;
340 strncpy (&sfrname[0], pname, sfrname_len);
342 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
343 sfrname_len += strlen("Frameworks/");
345 strncpy (&sfrname[sfrname_len], fname, fname_len);
346 sfrname_len += fname_len;
348 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
349 sfrname_len += strlen (".framework/");
351 /* Append framework_header_dirs and header file name */
352 for (i = 0; framework_header_dirs[i].dirName; i++)
354 strncpy (&sfrname[sfrname_len],
355 framework_header_dirs[i].dirName,
356 framework_header_dirs[i].dirNameLen);
357 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
358 &fname[fname_len]);
360 if (stat (sfrname, &st) == 0)
362 if (fast_dir != &subframe_dir)
364 if (fast_dir)
365 warning ("subframework include %s conflicts with framework include",
366 fname);
367 else
368 add_framework (fname, fname_len, &subframe_dir);
371 return sfrname;
374 free (sfrname);
376 return 0;
379 /* Add PATH to the system includes. PATH must be malloc-ed and
380 NUL-terminated. System framework paths are C++ aware. */
382 static void
383 add_system_framework_path (char *path)
385 int cxx_aware = 1;
386 cpp_dir *p;
388 p = xmalloc (sizeof (cpp_dir));
389 p->next = NULL;
390 p->name = path;
391 p->sysp = 1 + !cxx_aware;
392 p->construct = framework_construct_pathname;
393 using_frameworks = 1;
395 add_cpp_dir_path (p, SYSTEM);
398 /* Add PATH to the bracket includes. PATH must be malloc-ed and
399 NUL-terminated. */
401 void
402 add_framework_path (char *path)
404 cpp_dir *p;
406 p = xmalloc (sizeof (cpp_dir));
407 p->next = NULL;
408 p->name = path;
409 p->sysp = 0;
410 p->construct = framework_construct_pathname;
411 using_frameworks = 1;
413 add_cpp_dir_path (p, BRACKET);
416 static const char *framework_defaults [] =
418 "/System/Library/Frameworks",
419 "/Library/Frameworks",
420 "/Local/Library/Frameworks",
424 /* Register all the system framework paths if STDINC is true and setup
425 the missing_header callback for subframework searching if any
426 frameworks had been registered. */
428 void
429 darwin_register_frameworks (int stdinc)
431 if (stdinc)
433 size_t i;
435 /* Setup default search path for frameworks. */
436 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
438 /* System Framework headers are cxx aware. */
439 add_system_framework_path (xstrdup (framework_defaults[i]));
443 if (using_frameworks)
444 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
447 /* Search for HEADER in context dependent way. The return value is
448 the malloced name of a header to try and open, if any, or NULL
449 otherwise. This is called after normal header lookup processing
450 fails to find a header. We search each file in the include stack,
451 using FUNC, starting from the most deeply nested include and
452 finishing with the main input file. We stop searching when FUNC
453 returns non-zero. */
455 static const char*
456 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
458 const char *fname = header;
459 struct cpp_buffer *b;
460 const char *n;
462 for (b = cpp_get_buffer (pfile);
463 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
464 b = cpp_get_prev (b))
466 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
467 if (n)
469 /* Logically, the place where we found the subframework is
470 the place where we found the Framework that contains the
471 subframework. This is useful for tracking wether or not
472 we are in a system header. */
473 *dirp = cpp_get_dir (cpp_get_file (b));
474 return n;
478 return 0;
481 struct target_c_incpath_s target_c_incpath = C_INCPATH_INIT;