OSDN Git Service

* c-pragma.h: Define HANDLE_GENERIC_PRAGMAS if
[pf3gnuchains/gcc-fork.git] / gcc / c-pragma.c
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.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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.  */
20
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"
34
35 #ifdef HANDLE_GENERIC_PRAGMAS
36
37 #if USE_CPPLIB
38 extern cpp_reader parse_in;
39 #else
40 struct pragma_entry;
41 static struct pragma_entry *pragmas;
42
43 void cpp_register_pragma PARAMS ((cpp_reader *, const char *, const char *,
44                                   void (*) PARAMS ((cpp_reader *)) ));
45 void cpp_register_pragma_space PARAMS ((cpp_reader *, const char *));
46 #endif
47
48 #define BAD(msgid) do { warning (msgid); return; } while (0)
49 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
50
51 #ifdef HANDLE_PRAGMA_PACK
52 static void handle_pragma_pack PARAMS ((cpp_reader *));
53
54 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
55 typedef struct align_stack
56 {
57   int                  alignment;
58   unsigned int         num_pushes;
59   tree                 id;
60   struct align_stack * prev;
61 } align_stack;
62
63 static struct align_stack * alignment_stack = NULL;
64
65 /* If we have a "global" #pragma pack(<n>) if effect when the first
66    #pragma push(pack,<n>) is encountered, this stores the the value of 
67    maximum_field_alignment in effect.  When the final pop_alignment() 
68    happens, we restore the value to this, not to a value of 0 for
69    maximum_field_alignment.  Value is in bits. */
70 static int  default_alignment;
71 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
72 (default_alignment = maximum_field_alignment = (ALIGN))
73
74 static void push_alignment PARAMS ((int, tree));
75 static void pop_alignment  PARAMS ((tree));
76 static void mark_align_stack PARAMS ((void *));
77
78 /* Push an alignment value onto the stack.  */
79 static void
80 push_alignment (alignment, id)
81      int alignment;
82      tree id;
83 {
84   
85   if (alignment_stack == NULL
86       || alignment_stack->alignment != alignment
87       || id != NULL_TREE)
88     {
89       align_stack * entry;
90
91       entry = (align_stack *) xmalloc (sizeof (* entry));
92
93       entry->alignment  = alignment;
94       entry->num_pushes = 1;
95       entry->id         = id;
96       entry->prev       = alignment_stack;
97       
98       /* The current value of maximum_field_alignment is not necessarily 
99          0 since there may be a #pragma pack(<n>) in effect; remember it 
100          so that we can restore it after the final #pragma pop(). */
101       if (alignment_stack == NULL)
102         default_alignment = maximum_field_alignment;
103       
104       alignment_stack = entry;
105
106       maximum_field_alignment = alignment;
107     }
108   else
109     alignment_stack->num_pushes ++;
110 }
111
112 /* Undo a push of an alignment onto the stack.  */
113 static void
114 pop_alignment (id)
115      tree id;
116 {
117   align_stack * entry;
118       
119   if (alignment_stack == NULL)
120     {
121       warning ("\
122 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
123                );
124       return;
125     }
126
127   /* If we got an identifier, strip away everything above the target
128      entry so that the next step will restore the state just below it.  */
129   if (id)
130     {
131       for (entry = alignment_stack; entry; entry = entry->prev)
132         if (entry->id == id)
133           {
134             entry->num_pushes = 1;
135             alignment_stack = entry;
136             break;
137           }
138       if (entry == NULL)
139         warning ("\
140 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
141                  , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
142     }
143
144   if (-- alignment_stack->num_pushes == 0)
145     {
146       entry = alignment_stack->prev;
147
148       if (entry == NULL)
149         maximum_field_alignment = default_alignment;
150       else
151         maximum_field_alignment = entry->alignment;
152
153       free (alignment_stack);
154
155       alignment_stack = entry;
156     }
157 }
158
159 static void
160 mark_align_stack (p)
161     void *p;
162 {
163   align_stack *a = *(align_stack **) p;
164
165   while (a)
166     {
167       ggc_mark_tree (a->id);
168       a = a->prev;
169     }
170 }
171 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
172 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
173 #define push_alignment(ID, N) \
174     BAD("#pragma pack(push[, id], <n>) is not supported on this target")
175 #define pop_alignment(ID) \
176     BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
177 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
178
179 /* #pragma pack ()
180    #pragma pack (N)
181    
182    #pragma pack (push, N)
183    #pragma pack (push, ID, N)
184    #pragma pack (pop)
185    #pragma pack (pop, ID) */
186 static void
187 handle_pragma_pack (dummy)
188      cpp_reader *dummy ATTRIBUTE_UNUSED;
189 {
190   tree x, id = 0;
191   int align;
192   enum cpp_ttype token;
193   enum { set, reset, push, pop } action;
194
195   if (c_lex (&x) != CPP_OPEN_PAREN)
196     BAD ("missing '(' after '#pragma pack' - ignored");
197
198   token = c_lex (&x);
199   if (token == CPP_CLOSE_PAREN)
200     action = reset;
201   else if (token == CPP_NUMBER)
202     {
203       align = TREE_INT_CST_LOW (x);
204       action = set;
205     }
206   else if (token == CPP_NAME)
207     {
208       if (!strcmp (IDENTIFIER_POINTER (x), "push"))
209         action = push;
210       else if (!strcmp (IDENTIFIER_POINTER (x), "pop"))
211         action = pop;
212       else
213         BAD2 ("unknown action '%s' for '#pragma pack' - ignored",
214               IDENTIFIER_POINTER (x));
215     }
216   else
217     BAD ("malformed '#pragma pack' - ignored");
218
219   token = c_lex (&x);
220   if ((action == set || action == reset) && token != CPP_CLOSE_PAREN)
221     BAD ("malformed '#pragma pack' - ignored");
222   if ((action == push || action == pop) && token != CPP_COMMA)
223     BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored",
224           action == push ? "push" : "pop");
225
226   if (action == push || action == pop)
227     {
228       token = c_lex (&x);
229       if (token == CPP_NAME)
230         {
231           id = x;
232           if (c_lex (&x) != CPP_COMMA)
233             BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored",
234                   action == push ? "push" : "pop");
235           token = c_lex (&x);
236         }
237       if (token == CPP_NUMBER)
238         align = TREE_INT_CST_LOW (x);
239       else
240         BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored",
241               action == push ? "push" : "pop");
242
243       if (c_lex (&x) != CPP_CLOSE_PAREN)
244         BAD ("malformed '#pragma pack' - ignored");
245     }
246   if (c_lex (&x) != CPP_EOF)
247     warning ("junk at end of '#pragma pack'");
248
249   switch (align)
250     {
251     case 0:
252     case 1:
253     case 2:
254     case 4:
255     case 8:
256     case 16:
257       align *= BITS_PER_UNIT;
258       break;
259     default:
260       BAD2 ("alignment must be a small power of two, not %d", align);
261     }
262
263   switch (action)
264     {
265     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
266     case reset: SET_GLOBAL_ALIGNMENT (0);      break;
267     case push:  push_alignment (align, id);    break;
268     case pop:   pop_alignment (id);            break;
269     }
270 }
271 #endif  /* HANDLE_PRAGMA_PACK */
272
273 #ifdef HANDLE_PRAGMA_WEAK
274 static void handle_pragma_weak PARAMS ((cpp_reader *));
275
276 /* #pragma weak name [= value] */
277 static void
278 handle_pragma_weak (dummy)
279      cpp_reader *dummy ATTRIBUTE_UNUSED;
280 {
281   tree name, value, x;
282   enum cpp_ttype t;
283
284   value = 0;
285
286   if (c_lex (&name) != CPP_NAME)
287     BAD ("malformed #pragma weak, ignored");
288   t = c_lex (&x);
289   if (t == CPP_EQ)
290     {
291       if (c_lex (&value) != CPP_NAME)
292         BAD ("malformed #pragma weak, ignored");
293       t = c_lex (&x);
294     }
295   if (t != CPP_EOF)
296     warning ("junk at end of #pragma weak");
297
298   add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
299 }
300 #endif
301
302 #if !USE_CPPLIB
303 /* Glue version of cpplib's pragma registration and dispatch system.  */
304 struct pragma_entry
305 {
306   struct pragma_entry *next;
307   const char *name;
308   size_t len;
309   int isnspace;
310   union {
311     void (*handler) PARAMS ((cpp_reader *));
312     struct pragma_entry *space;
313   } u;
314 };
315
316 void
317 cpp_register_pragma_space (pfile, space)
318      cpp_reader *pfile ATTRIBUTE_UNUSED;
319      const char *space;
320 {
321   struct pragma_entry *new;
322   const struct pragma_entry *p = pragmas;
323   size_t len = strlen (space);
324
325   while (p)
326     {
327       if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
328         return;
329       p = p->next;
330     }
331
332   new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
333   new->name = space;
334   new->len = len;
335   new->isnspace = 1;
336   new->u.space = 0;
337
338   new->next = pragmas;
339   pragmas = new;
340 }
341
342 void
343 cpp_register_pragma (pfile, space, name, handler)
344      cpp_reader *pfile ATTRIBUTE_UNUSED;
345      const char *space;
346      const char *name;
347      void (*handler) PARAMS ((cpp_reader *));
348 {
349   struct pragma_entry **x, *new;
350   size_t len;
351
352   x = &pragmas;
353   if (space)
354     {
355       struct pragma_entry *p = pragmas;
356       len = strlen (space);
357       while (p)
358         {
359           if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
360             {
361               x = &p->u.space;
362               goto found;
363             }
364           p = p->next;
365         }
366       abort ();
367     }
368
369  found:
370   new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
371   new->name = name;
372   new->len = strlen (name);
373   new->isnspace = 0;
374   new->u.handler = handler;
375
376   new->next = *x;
377   *x = new;
378 }
379
380 /* Called from process_directive() for #pragma lines.  */
381 void
382 dispatch_pragma ()
383 {
384   enum cpp_ttype t;
385   tree x;
386   const struct pragma_entry *p;
387   const char *name, *space = 0;
388   size_t len;
389
390   p = pragmas;
391
392  new_space:
393   t = c_lex (&x);
394   if (t == CPP_EOF)
395     return;
396
397   if (t != CPP_NAME)
398     {
399       warning ("malformed #pragma directive");
400       return;
401     }
402
403   name = IDENTIFIER_POINTER (x);
404   len = IDENTIFIER_LENGTH (x);
405   while (p)
406     {
407       if (strlen (p->name) == len && !memcmp (p->name, name, len))
408         {
409           if (p->isnspace)
410             {
411               space = p->name;
412               p = p->u.space;
413               goto new_space;
414             }
415           else
416             {
417               (*p->u.handler) (0);
418               return;
419             }
420         }
421       p = p->next;
422     }
423
424   /* Issue a warning message if we have been asked to do so.  Ignore
425      unknown pragmas in system headers unless an explicit
426      -Wunknown-pragmas has been given. */
427   if (warn_unknown_pragmas > in_system_header)
428     {
429       if (space)
430         warning ("ignoring #pragma %s %s", space, name);
431       else
432         warning ("ignoring #pragma %s", name);
433     }
434 }
435
436 #endif
437
438 void
439 init_pragma ()
440 {
441 #if !USE_CPPLIB
442   cpp_reader *pfile = 0;
443 #else
444   cpp_reader *pfile = &parse_in;
445 #endif
446
447 #ifdef HANDLE_PRAGMA_PACK
448   cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
449 #endif
450 #ifdef HANDLE_PRAGMA_WEAK
451   cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
452 #endif
453
454 #ifdef REGISTER_TARGET_PRAGMAS
455   REGISTER_TARGET_PRAGMAS (pfile);
456 #endif
457
458 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
459   ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
460                 mark_align_stack);
461 #endif
462 }
463
464 #endif /* HANDLE_GENERIC_PRAGMAS */