Fix typo
[official-gcc.git] / gcc / c-pragma.c
blob8be3a6b065435ccaf228d0068b39185fac702835
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; 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 "rtl.h"
25 #include "tree.h"
26 #include "function.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 #define BAD(msgid) do { warning (msgid); return; } while (0)
36 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
38 #ifdef HANDLE_PRAGMA_PACK
39 static void handle_pragma_pack PARAMS ((cpp_reader *));
41 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
42 typedef struct align_stack
44 int alignment;
45 unsigned int num_pushes;
46 tree id;
47 struct align_stack * prev;
48 } align_stack;
50 static struct align_stack * alignment_stack = NULL;
52 /* If we have a "global" #pragma pack(<n>) in effect when the first
53 #pragma pack(push,<n>) is encountered, this stores the value of
54 maximum_field_alignment in effect. When the final pop_alignment()
55 happens, we restore the value to this, not to a value of 0 for
56 maximum_field_alignment. Value is in bits. */
57 static int default_alignment;
58 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
59 (default_alignment = maximum_field_alignment = (ALIGN))
61 static void push_alignment PARAMS ((int, tree));
62 static void pop_alignment PARAMS ((tree));
63 static void mark_align_stack PARAMS ((void *));
65 /* Push an alignment value onto the stack. */
66 static void
67 push_alignment (alignment, id)
68 int alignment;
69 tree id;
72 if (alignment_stack == NULL
73 || alignment_stack->alignment != alignment
74 || id != NULL_TREE)
76 align_stack * entry;
78 entry = (align_stack *) xmalloc (sizeof (* entry));
80 entry->alignment = alignment;
81 entry->num_pushes = 1;
82 entry->id = id;
83 entry->prev = alignment_stack;
85 /* The current value of maximum_field_alignment is not necessarily
86 0 since there may be a #pragma pack(<n>) in effect; remember it
87 so that we can restore it after the final #pragma pop(). */
88 if (alignment_stack == NULL)
89 default_alignment = maximum_field_alignment;
91 alignment_stack = entry;
93 maximum_field_alignment = alignment;
95 else
96 alignment_stack->num_pushes ++;
99 /* Undo a push of an alignment onto the stack. */
100 static void
101 pop_alignment (id)
102 tree id;
104 align_stack * entry;
106 if (alignment_stack == NULL)
108 warning ("\
109 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
111 return;
114 /* If we got an identifier, strip away everything above the target
115 entry so that the next step will restore the state just below it. */
116 if (id)
118 for (entry = alignment_stack; entry; entry = entry->prev)
119 if (entry->id == id)
121 entry->num_pushes = 1;
122 alignment_stack = entry;
123 break;
125 if (entry == NULL)
126 warning ("\
127 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
128 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
131 if (-- alignment_stack->num_pushes == 0)
133 entry = alignment_stack->prev;
135 if (entry == NULL)
136 maximum_field_alignment = default_alignment;
137 else
138 maximum_field_alignment = entry->alignment;
140 free (alignment_stack);
142 alignment_stack = entry;
146 static void
147 mark_align_stack (p)
148 void *p;
150 align_stack *a = *(align_stack **) p;
152 while (a)
154 ggc_mark_tree (a->id);
155 a = a->prev;
158 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
159 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
160 #define push_alignment(ID, N) \
161 BAD("#pragma pack(push[, id], <n>) is not supported on this target")
162 #define pop_alignment(ID) \
163 BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
164 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
166 /* #pragma pack ()
167 #pragma pack (N)
169 #pragma pack (push, N)
170 #pragma pack (push, ID, N)
171 #pragma pack (pop)
172 #pragma pack (pop, ID) */
173 static void
174 handle_pragma_pack (dummy)
175 cpp_reader *dummy ATTRIBUTE_UNUSED;
177 tree x, id = 0;
178 int align = -1;
179 enum cpp_ttype token;
180 enum { set, push, pop } action;
182 if (c_lex (&x) != CPP_OPEN_PAREN)
183 BAD ("missing '(' after '#pragma pack' - ignored");
185 token = c_lex (&x);
186 if (token == CPP_CLOSE_PAREN)
188 action = set;
189 align = 0;
191 else if (token == CPP_NUMBER)
193 align = TREE_INT_CST_LOW (x);
194 action = set;
195 if (c_lex (&x) != CPP_CLOSE_PAREN)
196 BAD ("malformed '#pragma pack' - ignored");
198 else if (token == CPP_NAME)
200 #define BAD_ACTION do { if (action == push) \
201 BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
202 else \
203 BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
204 } while (0)
206 const char *op = IDENTIFIER_POINTER (x);
207 if (!strcmp (op, "push"))
208 action = push;
209 else if (!strcmp (op, "pop"))
210 action = pop;
211 else
212 BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
214 token = c_lex (&x);
215 if (token != CPP_COMMA && action == push)
216 BAD_ACTION;
218 if (token == CPP_COMMA)
220 token = c_lex (&x);
221 if (token == CPP_NAME)
223 id = x;
224 if (action == push && c_lex (&x) != CPP_COMMA)
225 BAD_ACTION;
226 token = c_lex (&x);
229 if (action == push)
231 if (token == CPP_NUMBER)
233 align = TREE_INT_CST_LOW (x);
234 token = c_lex (&x);
236 else
237 BAD_ACTION;
241 if (token != CPP_CLOSE_PAREN)
242 BAD_ACTION;
243 #undef BAD_ACTION
245 else
246 BAD ("malformed '#pragma pack' - ignored");
248 if (c_lex (&x) != CPP_EOF)
249 warning ("junk at end of '#pragma pack'");
251 if (action != pop)
252 switch (align)
254 case 0:
255 case 1:
256 case 2:
257 case 4:
258 case 8:
259 case 16:
260 align *= BITS_PER_UNIT;
261 break;
262 default:
263 BAD2 ("alignment must be a small power of two, not %d", align);
266 switch (action)
268 case set: SET_GLOBAL_ALIGNMENT (align); break;
269 case push: push_alignment (align, id); break;
270 case pop: pop_alignment (id); break;
273 #endif /* HANDLE_PRAGMA_PACK */
275 #ifdef HANDLE_PRAGMA_WEAK
276 static void handle_pragma_weak PARAMS ((cpp_reader *));
278 /* #pragma weak name [= value] */
279 static void
280 handle_pragma_weak (dummy)
281 cpp_reader *dummy ATTRIBUTE_UNUSED;
283 tree name, value, x;
284 enum cpp_ttype t;
286 value = 0;
288 if (c_lex (&name) != CPP_NAME)
289 BAD ("malformed #pragma weak, ignored");
290 t = c_lex (&x);
291 if (t == CPP_EQ)
293 if (c_lex (&value) != CPP_NAME)
294 BAD ("malformed #pragma weak, ignored");
295 t = c_lex (&x);
297 if (t != CPP_EOF)
298 warning ("junk at end of #pragma weak");
300 add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
302 #endif
304 void
305 init_pragma ()
307 #ifdef HANDLE_PRAGMA_PACK
308 cpp_register_pragma (parse_in, 0, "pack", handle_pragma_pack);
309 #endif
310 #ifdef HANDLE_PRAGMA_WEAK
311 cpp_register_pragma (parse_in, 0, "weak", handle_pragma_weak);
312 #endif
313 #ifdef REGISTER_TARGET_PRAGMAS
314 REGISTER_TARGET_PRAGMAS (parse_in);
315 #endif
317 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
318 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
319 mark_align_stack);
320 #endif