OSDN Git Service

8801c697a8cdaf7f734c16593c31e2c060210096
[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, 2001, 2002
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
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-common.h"
33 #include "output.h"
34 #include "tm_p.h"
35
36 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
37 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
38
39 #ifdef HANDLE_PRAGMA_PACK
40 static void handle_pragma_pack PARAMS ((cpp_reader *));
41
42 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
43 typedef struct align_stack
44 {
45   int                  alignment;
46   unsigned int         num_pushes;
47   tree                 id;
48   struct align_stack * prev;
49 } align_stack;
50
51 static struct align_stack * alignment_stack = NULL;
52
53 /* If we have a "global" #pragma pack(<n>) in effect when the first
54    #pragma pack(push,<n>) is encountered, this stores the value of 
55    maximum_field_alignment in effect.  When the final pop_alignment() 
56    happens, we restore the value to this, not to a value of 0 for
57    maximum_field_alignment.  Value is in bits.  */
58 static int default_alignment;
59 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
60   (default_alignment = maximum_field_alignment = (ALIGN))
61
62 static void push_alignment PARAMS ((int, tree));
63 static void pop_alignment  PARAMS ((tree));
64 static void mark_align_stack PARAMS ((void *));
65
66 /* Push an alignment value onto the stack.  */
67 static void
68 push_alignment (alignment, id)
69      int alignment;
70      tree id;
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     GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
162 #define pop_alignment(ID) \
163     GCC_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     GCC_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         GCC_BAD ("malformed '#pragma pack' - ignored");
197     }
198   else if (token == CPP_NAME)
199     {
200 #define GCC_BAD_ACTION do { if (action == push) \
201           GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
202         else \
203           GCC_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         GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
213
214       token = c_lex (&x);
215       if (token != CPP_COMMA && action == push)
216         GCC_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                 GCC_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                 GCC_BAD_ACTION;
238             }
239         }
240
241       if (token != CPP_CLOSE_PAREN)
242         GCC_BAD_ACTION;
243 #undef GCC_BAD_ACTION
244     }
245   else
246     GCC_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         GCC_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 apply_pragma_weak PARAMS ((tree, tree));
277 static void handle_pragma_weak PARAMS ((cpp_reader *));
278
279 static tree pending_weaks;
280
281 static void
282 apply_pragma_weak (decl, value)
283      tree decl, value;
284 {
285   if (value)
286     decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
287                                              build_tree_list (NULL, value)),
288                      0);
289   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
290       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
291     warning_with_decl (decl, "applying #pragma weak `%s' after first use results in unspecified behavior");
292
293   declare_weak (decl);
294 }
295
296 void
297 maybe_apply_pragma_weak (decl)
298      tree decl;
299 {
300   tree *p, t, id;
301
302   /* Copied from the check in set_decl_assembler_name.  */
303   if (TREE_CODE (decl) == FUNCTION_DECL
304       || (TREE_CODE (decl) == VAR_DECL 
305           && (TREE_STATIC (decl) 
306               || DECL_EXTERNAL (decl) 
307               || TREE_PUBLIC (decl))))
308     id = DECL_ASSEMBLER_NAME (decl);
309   else
310     return;
311
312   for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
313     if (id == TREE_PURPOSE (t))
314       {
315         apply_pragma_weak (decl, TREE_VALUE (t));
316         *p = TREE_CHAIN (t);
317         break;
318       }
319 }
320
321 /* #pragma weak name [= value] */
322 static void
323 handle_pragma_weak (dummy)
324      cpp_reader *dummy ATTRIBUTE_UNUSED;
325 {
326   tree name, value, x, decl;
327   enum cpp_ttype t;
328
329   value = 0;
330
331   if (c_lex (&name) != CPP_NAME)
332     GCC_BAD ("malformed #pragma weak, ignored");
333   t = c_lex (&x);
334   if (t == CPP_EQ)
335     {
336       if (c_lex (&value) != CPP_NAME)
337         GCC_BAD ("malformed #pragma weak, ignored");
338       t = c_lex (&x);
339     }
340   if (t != CPP_EOF)
341     warning ("junk at end of #pragma weak");
342
343   decl = identifier_global_value (name);
344   if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
345     apply_pragma_weak (decl, value);
346   else
347     pending_weaks = tree_cons (name, value, pending_weaks);
348 }
349 #else
350 void
351 maybe_apply_pragma_weak (decl)
352      tree decl ATTRIBUTE_UNUSED;
353 {
354 }
355 #endif /* HANDLE_PRAGMA_WEAK */
356
357 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
358 static void handle_pragma_redefine_extname PARAMS ((cpp_reader *));
359
360 static tree pending_redefine_extname;
361
362 /* #pragma redefined_extname oldname newname */
363 static void
364 handle_pragma_redefine_extname (dummy)
365      cpp_reader *dummy ATTRIBUTE_UNUSED;
366 {
367   tree oldname, newname, decl, x;
368   enum cpp_ttype t;
369
370   if (c_lex (&oldname) != CPP_NAME)
371     {
372       warning ("malformed #pragma redefine_extname, ignored");
373       return;
374     }
375   if (c_lex (&newname) != CPP_NAME)
376     {
377       warning ("malformed #pragma redefine_extname, ignored");
378       return;
379     }
380   t = c_lex (&x);
381   if (t != CPP_EOF)
382     warning ("junk at end of #pragma redefine_extname");
383
384   decl = identifier_global_value (oldname);
385   if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
386     {
387       if (DECL_ASSEMBLER_NAME_SET_P (decl)
388           && DECL_ASSEMBLER_NAME (decl) != newname)
389         warning ("#pragma redefine_extname conflicts with declaration");
390       SET_DECL_ASSEMBLER_NAME (decl, newname);
391     }
392   else
393     pending_redefine_extname
394       = tree_cons (oldname, newname, pending_redefine_extname);
395 }
396 #endif
397
398 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
399 static void handle_pragma_extern_prefix PARAMS ((cpp_reader *));
400
401 static tree pragma_extern_prefix;
402
403 /* #pragma extern_prefix "prefix" */
404 static void
405 handle_pragma_extern_prefix (dummy)
406      cpp_reader *dummy ATTRIBUTE_UNUSED;
407 {
408   tree prefix, x;
409   enum cpp_ttype t;
410
411   if (c_lex (&prefix) != CPP_STRING)
412     {
413       warning ("malformed #pragma extern_prefix, ignored");
414       return;
415     }
416   t = c_lex (&x);
417   if (t != CPP_EOF)
418     warning ("junk at end of #pragma extern_prefix");
419
420   /* Note that the length includes the null terminator.  */
421   pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
422 }
423 #endif
424
425 /* Hook from the front ends to apply the results of one of the preceeding
426    pragmas that rename variables.  */
427
428 tree
429 maybe_apply_renaming_pragma (decl, asmname)
430      tree decl, asmname;
431 {
432   tree oldname;
433
434   /* Copied from the check in set_decl_assembler_name.  */
435   if (TREE_CODE (decl) == FUNCTION_DECL
436       || (TREE_CODE (decl) == VAR_DECL 
437           && (TREE_STATIC (decl) 
438               || DECL_EXTERNAL (decl) 
439               || TREE_PUBLIC (decl))))
440     oldname = DECL_ASSEMBLER_NAME (decl);
441   else
442     return asmname;
443
444   /* If the name begins with a *, that's a sign of an asmname attached to
445      a previous declaration.  */
446   if (IDENTIFIER_POINTER (oldname)[0] == '*')
447     {
448       const char *oldasmname = IDENTIFIER_POINTER (oldname) + 1;
449       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldasmname) != 0)
450         warning ("asm declaration conficts with previous rename");
451       asmname = build_string (strlen (oldasmname), oldasmname);
452     }
453
454 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
455   {
456     tree *p, t;
457
458     for (p = &pending_redefine_extname; (t = *p) ; p = &TREE_CHAIN (t))
459       if (oldname == TREE_PURPOSE (t))
460         {
461           const char *newname = IDENTIFIER_POINTER (TREE_VALUE (t));
462
463           if (asmname && strcmp (TREE_STRING_POINTER (asmname), newname) != 0)
464             warning ("#pragma redefine_extname conflicts with declaration");
465           *p = TREE_CHAIN (t);
466
467           return build_string (strlen (newname), newname);
468         }
469   }
470 #endif
471
472 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
473   if (pragma_extern_prefix && !asmname)
474     {
475       char *x = concat (TREE_STRING_POINTER (pragma_extern_prefix),
476                         IDENTIFIER_POINTER (oldname), NULL);
477       asmname = build_string (strlen (x), x);
478       free (x);
479       return asmname;
480     }
481 #endif
482
483   return asmname;
484 }
485
486 void
487 init_pragma ()
488 {
489 #ifdef HANDLE_PRAGMA_PACK
490   cpp_register_pragma (parse_in, 0, "pack", handle_pragma_pack);
491 #endif
492 #ifdef HANDLE_PRAGMA_WEAK
493   cpp_register_pragma (parse_in, 0, "weak", handle_pragma_weak);
494   ggc_add_tree_root (&pending_weaks, 1);
495 #endif
496 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
497   cpp_register_pragma (parse_in, 0, "redefine_extname",
498                        handle_pragma_redefine_extname);
499   ggc_add_tree_root (&pending_redefine_extname, 1);
500 #endif
501 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
502   cpp_register_pragma (parse_in, 0, "extern_prefix",
503                        handle_pragma_extern_prefix);
504   ggc_add_tree_root (&pragma_extern_prefix, 1);
505 #endif
506
507 #ifdef REGISTER_TARGET_PRAGMAS
508   REGISTER_TARGET_PRAGMAS (parse_in);
509 #endif
510
511 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
512   ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
513                 mark_align_stack);
514 #endif
515 }