OSDN Git Service

* Makefile.in: Remove MAYBE_CPPLIB and maybe_cpplib.
[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 #define BAD(msgid) do { warning (msgid); return; } while (0)
36 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
37
38 #ifdef HANDLE_PRAGMA_PACK
39 static void handle_pragma_pack PARAMS ((cpp_reader *));
40
41 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
42 typedef struct align_stack
43 {
44   int                  alignment;
45   unsigned int         num_pushes;
46   tree                 id;
47   struct align_stack * prev;
48 } align_stack;
49
50 static struct align_stack * alignment_stack = NULL;
51
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))
60
61 static void push_alignment PARAMS ((int, tree));
62 static void pop_alignment  PARAMS ((tree));
63 static void mark_align_stack PARAMS ((void *));
64
65 /* Push an alignment value onto the stack.  */
66 static void
67 push_alignment (alignment, id)
68      int alignment;
69      tree id;
70 {
71   
72   if (alignment_stack == NULL
73       || alignment_stack->alignment != alignment
74       || id != NULL_TREE)
75     {
76       align_stack * entry;
77
78       entry = (align_stack *) xmalloc (sizeof (* entry));
79
80       entry->alignment  = alignment;
81       entry->num_pushes = 1;
82       entry->id         = id;
83       entry->prev       = alignment_stack;
84       
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;
90       
91       alignment_stack = entry;
92
93       maximum_field_alignment = alignment;
94     }
95   else
96     alignment_stack->num_pushes ++;
97 }
98
99 /* Undo a push of an alignment onto the stack.  */
100 static void
101 pop_alignment (id)
102      tree id;
103 {
104   align_stack * entry;
105       
106   if (alignment_stack == NULL)
107     {
108       warning ("\
109 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
110                );
111       return;
112     }
113
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)
117     {
118       for (entry = alignment_stack; entry; entry = entry->prev)
119         if (entry->id == id)
120           {
121             entry->num_pushes = 1;
122             alignment_stack = entry;
123             break;
124           }
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));
129     }
130
131   if (-- alignment_stack->num_pushes == 0)
132     {
133       entry = alignment_stack->prev;
134
135       if (entry == NULL)
136         maximum_field_alignment = default_alignment;
137       else
138         maximum_field_alignment = entry->alignment;
139
140       free (alignment_stack);
141
142       alignment_stack = entry;
143     }
144 }
145
146 static void
147 mark_align_stack (p)
148     void *p;
149 {
150   align_stack *a = *(align_stack **) p;
151
152   while (a)
153     {
154       ggc_mark_tree (a->id);
155       a = a->prev;
156     }
157 }
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 */
165
166 /* #pragma pack ()
167    #pragma pack (N)
168    
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;
176 {
177   tree x, id = 0;
178   int align = -1;
179   enum cpp_ttype token;
180   enum { set, push, pop } action;
181
182   if (c_lex (&x) != CPP_OPEN_PAREN)
183     BAD ("missing '(' after '#pragma pack' - ignored");
184
185   token = c_lex (&x);
186   if (token == CPP_CLOSE_PAREN)
187     {
188       action = set;
189       align = 0;
190     }
191   else if (token == CPP_NUMBER)
192     {
193       align = TREE_INT_CST_LOW (x);
194       action = set;
195       if (c_lex (&x) != CPP_CLOSE_PAREN)
196         BAD ("malformed '#pragma pack' - ignored");
197     }
198   else if (token == CPP_NAME)
199     {
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)
205
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);
213
214       token = c_lex (&x);
215       if (token != CPP_COMMA && action == push)
216         BAD_ACTION;
217
218       if (token == CPP_COMMA)
219         {
220           token = c_lex (&x);
221           if (token == CPP_NAME)
222             {
223               id = x;
224               if (action == push && c_lex (&x) != CPP_COMMA)
225                 BAD_ACTION;
226               token = c_lex (&x);
227             }
228
229           if (action == push)
230             {
231               if (token == CPP_NUMBER)
232                 {
233                   align = TREE_INT_CST_LOW (x);
234                   token = c_lex (&x);
235                 }
236               else
237                 BAD_ACTION;
238             }
239         }
240
241       if (token != CPP_CLOSE_PAREN)
242         BAD_ACTION;
243 #undef BAD_ACTION
244     }
245   else
246     BAD ("malformed '#pragma pack' - ignored");
247
248   if (c_lex (&x) != CPP_EOF)
249     warning ("junk at end of '#pragma pack'");
250
251   if (action != pop)
252     switch (align)
253       {
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);
264       }
265
266   switch (action)
267     {
268     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
269     case push:  push_alignment (align, id);    break;
270     case pop:   pop_alignment (id);            break;
271     }
272 }
273 #endif  /* HANDLE_PRAGMA_PACK */
274
275 #ifdef HANDLE_PRAGMA_WEAK
276 static void handle_pragma_weak PARAMS ((cpp_reader *));
277
278 /* #pragma weak name [= value] */
279 static void
280 handle_pragma_weak (dummy)
281      cpp_reader *dummy ATTRIBUTE_UNUSED;
282 {
283   tree name, value, x;
284   enum cpp_ttype t;
285
286   value = 0;
287
288   if (c_lex (&name) != CPP_NAME)
289     BAD ("malformed #pragma weak, ignored");
290   t = c_lex (&x);
291   if (t == CPP_EQ)
292     {
293       if (c_lex (&value) != CPP_NAME)
294         BAD ("malformed #pragma weak, ignored");
295       t = c_lex (&x);
296     }
297   if (t != CPP_EOF)
298     warning ("junk at end of #pragma weak");
299
300   add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
301 }
302 #endif
303
304 void
305 init_pragma ()
306 {
307   cpp_reader *pfile ATTRIBUTE_UNUSED;
308   pfile = &parse_in;
309
310 #ifdef HANDLE_PRAGMA_PACK
311   cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
312 #endif
313 #ifdef HANDLE_PRAGMA_WEAK
314   cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
315 #endif
316 #ifdef REGISTER_TARGET_PRAGMAS
317   REGISTER_TARGET_PRAGMAS (pfile);
318 #endif
319
320 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
321   ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
322                 mark_align_stack);
323 #endif
324 }