* config/xtensa/xtensa.c (call_insn_operand): Check
[official-gcc.git] / gcc / config / darwin-c.c
blobabe0a6b28b864f0fb5ce4599619d0da415099943
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);
49 typedef struct align_stack
51 int alignment;
52 struct align_stack * prev;
53 } align_stack;
55 static struct align_stack * field_align_stack = NULL;
57 static void
58 push_field_alignment (int bit_alignment)
60 align_stack *entry = (align_stack *) xmalloc (sizeof (align_stack));
62 entry->alignment = maximum_field_alignment;
63 entry->prev = field_align_stack;
64 field_align_stack = entry;
66 maximum_field_alignment = bit_alignment;
69 static void
70 pop_field_alignment (void)
72 if (field_align_stack)
74 align_stack *entry = field_align_stack;
76 maximum_field_alignment = entry->alignment;
77 field_align_stack = entry->prev;
78 free (entry);
80 else
81 error ("too many #pragma options align=reset");
84 /* Handlers for Darwin-specific pragmas. */
86 void
87 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
89 /* Do nothing. */
92 /* #pragma options align={mac68k|power|reset} */
94 void
95 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
97 const char *arg;
98 tree t, x;
100 if (c_lex (&t) != CPP_NAME)
101 BAD ("malformed '#pragma options', ignoring");
102 arg = IDENTIFIER_POINTER (t);
103 if (strcmp (arg, "align"))
104 BAD ("malformed '#pragma options', ignoring");
105 if (c_lex (&t) != CPP_EQ)
106 BAD ("malformed '#pragma options', ignoring");
107 if (c_lex (&t) != CPP_NAME)
108 BAD ("malformed '#pragma options', ignoring");
110 if (c_lex (&x) != CPP_EOF)
111 warning ("junk at end of '#pragma options'");
113 arg = IDENTIFIER_POINTER (t);
114 if (!strcmp (arg, "mac68k"))
115 push_field_alignment (16);
116 else if (!strcmp (arg, "power"))
117 push_field_alignment (0);
118 else if (!strcmp (arg, "reset"))
119 pop_field_alignment ();
120 else
121 warning ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
124 /* #pragma unused ([var {, var}*]) */
126 void
127 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
129 tree decl, x;
130 int tok;
132 if (c_lex (&x) != CPP_OPEN_PAREN)
133 BAD ("missing '(' after '#pragma unused', ignoring");
135 while (1)
137 tok = c_lex (&decl);
138 if (tok == CPP_NAME && decl)
140 tree local = lookup_name (decl);
141 if (local && (TREE_CODE (local) == PARM_DECL
142 || TREE_CODE (local) == VAR_DECL))
143 TREE_USED (local) = 1;
144 tok = c_lex (&x);
145 if (tok != CPP_COMMA)
146 break;
150 if (tok != CPP_CLOSE_PAREN)
151 BAD ("missing ')' after '#pragma unused', ignoring");
153 if (c_lex (&x) != CPP_EOF)
154 warning ("junk at end of '#pragma unused'");
157 static struct {
158 size_t len;
159 const char *name;
160 cpp_dir* dir;
161 } *frameworks_in_use;
162 static int num_frameworks = 0;
163 static int max_frameworks = 0;
166 /* Remember which frameworks have been seen, so that we can ensure
167 that all uses of that framework come from the same framework. DIR
168 is the place where the named framework NAME, which is of length
169 LEN, was found. */
171 static void
172 add_framework (const char *name, size_t len, cpp_dir *dir)
174 int i;
175 for (i = 0; i < num_frameworks; ++i)
177 if (len == frameworks_in_use[i].len
178 && strncmp (name, frameworks_in_use[i].name, len) == 0)
180 return;
183 if (i >= max_frameworks)
185 max_frameworks = i*2;
186 frameworks_in_use = xrealloc (frameworks_in_use,
187 max_frameworks*sizeof(*frameworks_in_use));
189 frameworks_in_use[num_frameworks].name = name;
190 frameworks_in_use[num_frameworks].len = len;
191 frameworks_in_use[num_frameworks].dir = dir;
192 ++num_frameworks;
195 /* Recall if we have seen the named framework NAME, before, and where
196 we saw it. NAME is LEN bytes long. The return value is the place
197 where it was seen before. */
199 static struct cpp_dir*
200 find_framework (const char *name, size_t len)
202 int i;
203 for (i = 0; i < num_frameworks; ++i)
205 if (len == frameworks_in_use[i].len
206 && strncmp (name, frameworks_in_use[i].name, len) == 0)
208 return frameworks_in_use[i].dir;
211 return 0;
214 /* There are two directories in a framework that contain header files,
215 Headers and PrivateHeaders. We search Headers first as it is more
216 common to upgrade a header from PrivateHeaders to Headers and when
217 that is done, the old one might hang around and be out of data,
218 causing grief. */
220 struct framework_header {const char * dirName; int dirNameLen; };
221 static struct framework_header framework_header_dirs[] = {
222 { "Headers", 7 },
223 { "PrivateHeaders", 14 },
224 { NULL, 0 }
227 /* Returns a pointer to a malloced string that contains the real pathname
228 to the file, given the base name and the name. */
230 static char *
231 framework_construct_pathname (const char *fname, cpp_dir *dir)
233 char *buf;
234 size_t fname_len, frname_len;
235 cpp_dir *fast_dir;
236 char *frname;
237 struct stat st;
238 int i;
240 /* Framework names must have a / in them. */
241 buf = strchr (fname, '/');
242 if (buf)
243 fname_len = buf - fname;
244 else
245 return 0;
247 fast_dir = find_framework (fname, fname_len);
249 /* Framework includes must all come from one framework. */
250 if (fast_dir && dir != fast_dir)
251 return 0;
253 frname = xmalloc (strlen (fname) + dir->len + 2
254 + strlen(".framework/") + strlen("PrivateHeaders"));
255 strncpy (&frname[0], dir->name, dir->len);
256 frname_len = dir->len;
257 if (frname_len && frname[frname_len-1] != '/')
258 frname[frname_len++] = '/';
259 strncpy (&frname[frname_len], fname, fname_len);
260 frname_len += fname_len;
261 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
262 frname_len += strlen (".framework/");
264 /* Append framework_header_dirs and header file name */
265 for (i = 0; framework_header_dirs[i].dirName; i++)
267 strncpy (&frname[frname_len],
268 framework_header_dirs[i].dirName,
269 framework_header_dirs[i].dirNameLen);
270 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
271 &fname[fname_len]);
273 if (stat (frname, &st) == 0)
275 add_framework (fname, fname_len, dir);
276 return frname;
280 free (frname);
281 return 0;
284 /* Search for FNAME in sub-frameworks. pname is the context that we
285 wish to search in. Return the path the file was found at,
286 otherwise return 0. */
288 static const char*
289 find_subframework_file (const char *fname, const char *pname)
291 char *sfrname;
292 const char *dot_framework = ".framework/";
293 char *bufptr;
294 int sfrname_len, i, fname_len;
295 struct cpp_dir *fast_dir;
296 static struct cpp_dir subframe_dir;
297 struct stat st;
299 bufptr = strchr (fname, '/');
301 /* Subframework files must have / in the name. */
302 if (bufptr == 0)
303 return 0;
305 fname_len = bufptr - fname;
306 fast_dir = find_framework (fname, fname_len);
308 /* Sub framework header filename includes parent framework name and
309 header name in the "CarbonCore/OSUtils.h" form. If it does not
310 include slash it is not a sub framework include. */
311 bufptr = strstr (pname, dot_framework);
313 /* If the parent header is not of any framework, then this header
314 can not be part of any subframework. */
315 if (!bufptr)
316 return 0;
318 /* Now translate. For example, +- bufptr
319 fname = CarbonCore/OSUtils.h |
320 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
321 into
322 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
324 sfrname = (char *) xmalloc (strlen (pname) + strlen (fname) + 2 +
325 strlen ("Frameworks/") + strlen (".framework/")
326 + strlen ("PrivateHeaders"));
328 bufptr += strlen (dot_framework);
330 sfrname_len = bufptr - pname;
332 strncpy (&sfrname[0], pname, sfrname_len);
334 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
335 sfrname_len += strlen("Frameworks/");
337 strncpy (&sfrname[sfrname_len], fname, fname_len);
338 sfrname_len += fname_len;
340 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
341 sfrname_len += strlen (".framework/");
343 /* Append framework_header_dirs and header file name */
344 for (i = 0; framework_header_dirs[i].dirName; i++)
346 strncpy (&sfrname[sfrname_len],
347 framework_header_dirs[i].dirName,
348 framework_header_dirs[i].dirNameLen);
349 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
350 &fname[fname_len]);
352 if (stat (sfrname, &st) == 0)
354 if (fast_dir != &subframe_dir)
356 if (fast_dir)
357 warning ("subframework include %s conflicts with framework include",
358 fname);
359 else
360 add_framework (fname, fname_len, &subframe_dir);
363 return sfrname;
366 free (sfrname);
368 return 0;
371 /* Add PATH to the system includes. PATH must be malloc-ed and
372 NUL-terminated. System framework paths are C++ aware. */
374 static void
375 add_system_framework_path (char *path)
377 int cxx_aware = 1;
378 cpp_dir *p;
380 p = xmalloc (sizeof (cpp_dir));
381 p->next = NULL;
382 p->name = path;
383 p->sysp = 1 + !cxx_aware;
384 p->construct = framework_construct_pathname;
385 using_frameworks = 1;
387 add_cpp_dir_path (p, SYSTEM);
390 /* Add PATH to the bracket includes. PATH must be malloc-ed and
391 NUL-terminated. */
393 void
394 add_framework_path (char *path)
396 cpp_dir *p;
398 p = xmalloc (sizeof (cpp_dir));
399 p->next = NULL;
400 p->name = path;
401 p->sysp = 0;
402 p->construct = framework_construct_pathname;
403 using_frameworks = 1;
405 add_cpp_dir_path (p, BRACKET);
408 static const char *framework_defaults [] =
410 "/System/Library/Frameworks",
411 "/Library/Frameworks",
412 "/Local/Library/Frameworks",
416 /* Register all the system framework paths if STDINC is true and setup
417 the missing_header callback for subframework searching if any
418 frameworks had been registered. */
420 void
421 darwin_register_frameworks (int stdinc)
423 if (stdinc)
425 size_t i;
427 /* Setup default search path for frameworks. */
428 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
430 /* System Framework headers are cxx aware. */
431 add_system_framework_path (xstrdup (framework_defaults[i]));
435 if (using_frameworks)
436 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
439 /* Search for HEADER in context dependent way. The return value is
440 the malloced name of a header to try and open, if any, or NULL
441 otherwise. This is called after normal header lookup processing
442 fails to find a header. We search each file in the include stack,
443 using FUNC, starting from the most deeply nested include and
444 finishing with the main input file. We stop searching when FUNC
445 returns non-zero. */
447 static const char*
448 find_subframework_header (cpp_reader *pfile, const char *header)
450 const char *fname = header;
451 struct cpp_buffer *b;
452 const char *n;
454 for (b = cpp_get_buffer (pfile);
455 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
456 b = cpp_get_prev (b))
458 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
459 if (n)
460 return n;
463 return 0;
466 struct target_c_incpath_s target_c_incpath = C_INCPATH_INIT;