Fix IA-64 abort compiling ping.
[official-gcc.git] / gcc / c-pragma.c
blob8503f3cacca41362ea1cb52f1b6e9b13f6220964
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "function.h"
26 #include "defaults.h"
27 #include "cpplib.h"
28 #include "c-pragma.h"
29 #include "flags.h"
30 #include "toplev.h"
31 #include "ggc.h"
32 #include "c-lex.h"
33 #include "tm_p.h"
35 #if USE_CPPLIB
36 extern cpp_reader parse_in;
37 #else
38 struct pragma_entry;
39 static struct pragma_entry *pragmas;
41 void cpp_register_pragma PARAMS ((cpp_reader *, const char *, const char *,
42 void (*) PARAMS ((cpp_reader *)) ));
43 void cpp_register_pragma_space PARAMS ((cpp_reader *, const char *));
44 #endif
46 #define BAD(msgid) do { warning (msgid); return; } while (0)
47 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
49 #ifdef HANDLE_PRAGMA_PACK
50 static void handle_pragma_pack PARAMS ((cpp_reader *));
52 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
53 typedef struct align_stack
55 int alignment;
56 unsigned int num_pushes;
57 tree id;
58 struct align_stack * prev;
59 } align_stack;
61 static struct align_stack * alignment_stack = NULL;
63 /* If we have a "global" #pragma pack(<n>) if effect when the first
64 #pragma push(pack,<n>) is encountered, this stores the the value of
65 maximum_field_alignment in effect. When the final pop_alignment()
66 happens, we restore the value to this, not to a value of 0 for
67 maximum_field_alignment. Value is in bits. */
68 static int default_alignment;
69 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
70 (default_alignment = maximum_field_alignment = (ALIGN))
72 static void push_alignment PARAMS ((int, tree));
73 static void pop_alignment PARAMS ((tree));
74 static void mark_align_stack PARAMS ((void *));
76 /* Push an alignment value onto the stack. */
77 static void
78 push_alignment (alignment, id)
79 int alignment;
80 tree id;
83 if (alignment_stack == NULL
84 || alignment_stack->alignment != alignment
85 || id != NULL_TREE)
87 align_stack * entry;
89 entry = (align_stack *) xmalloc (sizeof (* entry));
91 entry->alignment = alignment;
92 entry->num_pushes = 1;
93 entry->id = id;
94 entry->prev = alignment_stack;
96 /* The current value of maximum_field_alignment is not necessarily
97 0 since there may be a #pragma pack(<n>) in effect; remember it
98 so that we can restore it after the final #pragma pop(). */
99 if (alignment_stack == NULL)
100 default_alignment = maximum_field_alignment;
102 alignment_stack = entry;
104 maximum_field_alignment = alignment;
106 else
107 alignment_stack->num_pushes ++;
110 /* Undo a push of an alignment onto the stack. */
111 static void
112 pop_alignment (id)
113 tree id;
115 align_stack * entry;
117 if (alignment_stack == NULL)
119 warning ("\
120 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
122 return;
125 /* If we got an identifier, strip away everything above the target
126 entry so that the next step will restore the state just below it. */
127 if (id)
129 for (entry = alignment_stack; entry; entry = entry->prev)
130 if (entry->id == id)
132 entry->num_pushes = 1;
133 alignment_stack = entry;
134 break;
136 if (entry == NULL)
137 warning ("\
138 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
139 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
142 if (-- alignment_stack->num_pushes == 0)
144 entry = alignment_stack->prev;
146 if (entry == NULL)
147 maximum_field_alignment = default_alignment;
148 else
149 maximum_field_alignment = entry->alignment;
151 free (alignment_stack);
153 alignment_stack = entry;
157 static void
158 mark_align_stack (p)
159 void *p;
161 align_stack *a = *(align_stack **) p;
163 while (a)
165 ggc_mark_tree (a->id);
166 a = a->prev;
169 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
170 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
171 #define push_alignment(ID, N) \
172 BAD("#pragma pack(push[, id], <n>) is not supported on this target")
173 #define pop_alignment(ID) \
174 BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
175 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
177 /* #pragma pack ()
178 #pragma pack (N)
180 #pragma pack (push, N)
181 #pragma pack (push, ID, N)
182 #pragma pack (pop)
183 #pragma pack (pop, ID) */
184 static void
185 handle_pragma_pack (dummy)
186 cpp_reader *dummy ATTRIBUTE_UNUSED;
188 tree x, id = 0;
189 int align;
190 enum cpp_ttype token;
191 enum { set, push, pop } action;
193 if (c_lex (&x) != CPP_OPEN_PAREN)
194 BAD ("missing '(' after '#pragma pack' - ignored");
196 token = c_lex (&x);
197 if (token == CPP_CLOSE_PAREN)
199 action = set;
200 align = 0;
202 else if (token == CPP_NUMBER)
204 align = TREE_INT_CST_LOW (x);
205 action = set;
206 if (c_lex (&x) != CPP_CLOSE_PAREN)
207 BAD ("malformed '#pragma pack' - ignored");
209 else if (token == CPP_NAME)
211 const char *op = IDENTIFIER_POINTER (x);
212 if (!strcmp (op, "push"))
213 action = push;
214 else if (!strcmp (op, "pop"))
215 action = pop;
216 else
217 BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
219 if (c_lex (&x) != CPP_COMMA)
220 BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored", op);
222 token = c_lex (&x);
223 if (token == CPP_NAME)
225 id = x;
226 if (c_lex (&x) != CPP_COMMA)
227 BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored", op);
228 token = c_lex (&x);
231 if (token == CPP_NUMBER)
232 align = TREE_INT_CST_LOW (x);
233 else
234 BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored", op);
236 if (c_lex (&x) != CPP_CLOSE_PAREN)
237 BAD ("malformed '#pragma pack' - ignored");
239 else
240 BAD ("malformed '#pragma pack' - ignored");
242 if (c_lex (&x) != CPP_EOF)
243 warning ("junk at end of '#pragma pack'");
245 switch (align)
247 case 0:
248 case 1:
249 case 2:
250 case 4:
251 case 8:
252 case 16:
253 align *= BITS_PER_UNIT;
254 break;
255 default:
256 BAD2 ("alignment must be a small power of two, not %d", align);
259 switch (action)
261 case set: SET_GLOBAL_ALIGNMENT (align); break;
262 case push: push_alignment (align, id); break;
263 case pop: pop_alignment (id); break;
266 #endif /* HANDLE_PRAGMA_PACK */
268 #ifdef HANDLE_PRAGMA_WEAK
269 static void handle_pragma_weak PARAMS ((cpp_reader *));
271 /* #pragma weak name [= value] */
272 static void
273 handle_pragma_weak (dummy)
274 cpp_reader *dummy ATTRIBUTE_UNUSED;
276 tree name, value, x;
277 enum cpp_ttype t;
279 value = 0;
281 if (c_lex (&name) != CPP_NAME)
282 BAD ("malformed #pragma weak, ignored");
283 t = c_lex (&x);
284 if (t == CPP_EQ)
286 if (c_lex (&value) != CPP_NAME)
287 BAD ("malformed #pragma weak, ignored");
288 t = c_lex (&x);
290 if (t != CPP_EOF)
291 warning ("junk at end of #pragma weak");
293 add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
295 #endif
297 #if !USE_CPPLIB
298 /* Glue version of cpplib's pragma registration and dispatch system. */
299 struct pragma_entry
301 struct pragma_entry *next;
302 const char *name;
303 size_t len;
304 int isnspace;
305 union {
306 void (*handler) PARAMS ((cpp_reader *));
307 struct pragma_entry *space;
308 } u;
311 void
312 cpp_register_pragma_space (pfile, space)
313 cpp_reader *pfile ATTRIBUTE_UNUSED;
314 const char *space;
316 struct pragma_entry *new;
317 const struct pragma_entry *p = pragmas;
318 size_t len = strlen (space);
320 while (p)
322 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
323 return;
324 p = p->next;
327 new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
328 new->name = space;
329 new->len = len;
330 new->isnspace = 1;
331 new->u.space = 0;
333 new->next = pragmas;
334 pragmas = new;
337 void
338 cpp_register_pragma (pfile, space, name, handler)
339 cpp_reader *pfile ATTRIBUTE_UNUSED;
340 const char *space;
341 const char *name;
342 void (*handler) PARAMS ((cpp_reader *));
344 struct pragma_entry **x, *new;
345 size_t len;
347 x = &pragmas;
348 if (space)
350 struct pragma_entry *p = pragmas;
351 len = strlen (space);
352 while (p)
354 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
356 x = &p->u.space;
357 goto found;
359 p = p->next;
361 abort ();
364 found:
365 new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
366 new->name = name;
367 new->len = strlen (name);
368 new->isnspace = 0;
369 new->u.handler = handler;
371 new->next = *x;
372 *x = new;
375 /* Called from process_directive() for #pragma lines. */
376 void
377 dispatch_pragma ()
379 enum cpp_ttype t;
380 tree x;
381 const struct pragma_entry *p;
382 const char *name, *space = 0;
383 size_t len;
385 p = pragmas;
387 new_space:
388 t = c_lex (&x);
389 if (t == CPP_EOF)
390 return;
392 if (t != CPP_NAME)
394 warning ("malformed #pragma directive");
395 return;
398 name = IDENTIFIER_POINTER (x);
399 len = IDENTIFIER_LENGTH (x);
400 while (p)
402 if (strlen (p->name) == len && !memcmp (p->name, name, len))
404 if (p->isnspace)
406 space = p->name;
407 p = p->u.space;
408 goto new_space;
410 else
412 (*p->u.handler) (0);
413 return;
416 p = p->next;
419 /* Issue a warning message if we have been asked to do so. Ignore
420 unknown pragmas in system headers unless an explicit
421 -Wunknown-pragmas has been given. */
422 if (warn_unknown_pragmas > in_system_header)
424 if (space)
425 warning ("ignoring #pragma %s %s", space, name);
426 else
427 warning ("ignoring #pragma %s", name);
431 #endif
433 void
434 init_pragma ()
436 cpp_reader *pfile ATTRIBUTE_UNUSED;
437 #if !USE_CPPLIB
438 pfile = 0;
439 #else
440 pfile = &parse_in;
441 #endif
443 #ifdef HANDLE_PRAGMA_PACK
444 cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
445 #endif
446 #ifdef HANDLE_PRAGMA_WEAK
447 cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
448 #endif
449 #ifdef REGISTER_TARGET_PRAGMAS
450 REGISTER_TARGET_PRAGMAS (pfile);
451 #endif
453 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
454 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
455 mark_align_stack);
456 #endif