OSDN Git Service

* c-decl.c (finish_decl): When setting the DECL_ASSEMBLER_NAME
[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, 2003
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 "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "function.h"
29 #include "cpplib.h"
30 #include "c-pragma.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "ggc.h"
34 #include "c-common.h"
35 #include "output.h"
36 #include "tm_p.h"
37
38 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
39 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
40
41 typedef struct align_stack GTY(())
42 {
43   int                  alignment;
44   unsigned int         num_pushes;
45   tree                 id;
46   struct align_stack * prev;
47 } align_stack;
48
49 static GTY(()) struct align_stack * alignment_stack;
50
51 #ifdef HANDLE_PRAGMA_PACK
52 static void handle_pragma_pack PARAMS ((cpp_reader *));
53
54 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
55 /* If we have a "global" #pragma pack(<n>) in effect when the first
56    #pragma pack(push,<n>) is encountered, this stores the value of 
57    maximum_field_alignment in effect.  When the final pop_alignment() 
58    happens, we restore the value to this, not to a value of 0 for
59    maximum_field_alignment.  Value is in bits.  */
60 static int default_alignment;
61 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
62   (default_alignment = maximum_field_alignment = (ALIGN))
63
64 static void push_alignment PARAMS ((int, tree));
65 static void pop_alignment  PARAMS ((tree));
66
67 /* Push an alignment value onto the stack.  */
68 static void
69 push_alignment (alignment, id)
70      int alignment;
71      tree id;
72 {
73   if (alignment_stack == NULL
74       || alignment_stack->alignment != alignment
75       || id != NULL_TREE)
76     {
77       align_stack * entry;
78
79       entry = (align_stack *) ggc_alloc (sizeof (* entry));
80
81       entry->alignment  = alignment;
82       entry->num_pushes = 1;
83       entry->id         = id;
84       entry->prev       = alignment_stack;
85       
86       /* The current value of maximum_field_alignment is not necessarily 
87          0 since there may be a #pragma pack(<n>) in effect; remember it 
88          so that we can restore it after the final #pragma pop().  */
89       if (alignment_stack == NULL)
90         default_alignment = maximum_field_alignment;
91       
92       alignment_stack = entry;
93
94       maximum_field_alignment = alignment;
95     }
96   else
97     alignment_stack->num_pushes ++;
98 }
99
100 /* Undo a push of an alignment onto the stack.  */
101 static void
102 pop_alignment (id)
103      tree id;
104 {
105   align_stack * entry;
106       
107   if (alignment_stack == NULL)
108     {
109       warning ("\
110 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
111                );
112       return;
113     }
114
115   /* If we got an identifier, strip away everything above the target
116      entry so that the next step will restore the state just below it.  */
117   if (id)
118     {
119       for (entry = alignment_stack; entry; entry = entry->prev)
120         if (entry->id == id)
121           {
122             entry->num_pushes = 1;
123             alignment_stack = entry;
124             break;
125           }
126       if (entry == NULL)
127         warning ("\
128 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
129                  , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
130     }
131
132   if (-- alignment_stack->num_pushes == 0)
133     {
134       entry = alignment_stack->prev;
135
136       if (entry == NULL)
137         maximum_field_alignment = default_alignment;
138       else
139         maximum_field_alignment = entry->alignment;
140
141       alignment_stack = entry;
142     }
143 }
144 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
145 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
146 #define push_alignment(ID, N) \
147     GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
148 #define pop_alignment(ID) \
149     GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
150 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
151
152 /* #pragma pack ()
153    #pragma pack (N)
154    
155    #pragma pack (push, N)
156    #pragma pack (push, ID, N)
157    #pragma pack (pop)
158    #pragma pack (pop, ID) */
159 static void
160 handle_pragma_pack (dummy)
161      cpp_reader *dummy ATTRIBUTE_UNUSED;
162 {
163   tree x, id = 0;
164   int align = -1;
165   enum cpp_ttype token;
166   enum { set, push, pop } action;
167
168   if (c_lex (&x) != CPP_OPEN_PAREN)
169     GCC_BAD ("missing '(' after '#pragma pack' - ignored");
170
171   token = c_lex (&x);
172   if (token == CPP_CLOSE_PAREN)
173     {
174       action = set;
175       align = 0;
176     }
177   else if (token == CPP_NUMBER)
178     {
179       align = TREE_INT_CST_LOW (x);
180       action = set;
181       if (c_lex (&x) != CPP_CLOSE_PAREN)
182         GCC_BAD ("malformed '#pragma pack' - ignored");
183     }
184   else if (token == CPP_NAME)
185     {
186 #define GCC_BAD_ACTION do { if (action == push) \
187           GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
188         else \
189           GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
190         } while (0)
191
192       const char *op = IDENTIFIER_POINTER (x);
193       if (!strcmp (op, "push"))
194         action = push;
195       else if (!strcmp (op, "pop"))
196         action = pop;
197       else
198         GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
199
200       token = c_lex (&x);
201       if (token != CPP_COMMA && action == push)
202         GCC_BAD_ACTION;
203
204       if (token == CPP_COMMA)
205         {
206           token = c_lex (&x);
207           if (token == CPP_NAME)
208             {
209               id = x;
210               if (action == push && c_lex (&x) != CPP_COMMA)
211                 GCC_BAD_ACTION;
212               token = c_lex (&x);
213             }
214
215           if (action == push)
216             {
217               if (token == CPP_NUMBER)
218                 {
219                   align = TREE_INT_CST_LOW (x);
220                   token = c_lex (&x);
221                 }
222               else
223                 GCC_BAD_ACTION;
224             }
225         }
226
227       if (token != CPP_CLOSE_PAREN)
228         GCC_BAD_ACTION;
229 #undef GCC_BAD_ACTION
230     }
231   else
232     GCC_BAD ("malformed '#pragma pack' - ignored");
233
234   if (c_lex (&x) != CPP_EOF)
235     warning ("junk at end of '#pragma pack'");
236
237   if (action != pop)
238     switch (align)
239       {
240       case 0:
241       case 1:
242       case 2:
243       case 4:
244       case 8:
245       case 16:
246         align *= BITS_PER_UNIT;
247         break;
248       default:
249         GCC_BAD2 ("alignment must be a small power of two, not %d", align);
250       }
251
252   switch (action)
253     {
254     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
255     case push:  push_alignment (align, id);    break;
256     case pop:   pop_alignment (id);            break;
257     }
258 }
259 #endif  /* HANDLE_PRAGMA_PACK */
260
261 static GTY(()) tree pending_weaks;
262
263 #ifdef HANDLE_PRAGMA_WEAK
264 static void apply_pragma_weak PARAMS ((tree, tree));
265 static void handle_pragma_weak PARAMS ((cpp_reader *));
266
267 static void
268 apply_pragma_weak (decl, value)
269      tree decl, value;
270 {
271   if (value)
272     {
273       value = build_string (IDENTIFIER_LENGTH (value),
274                             IDENTIFIER_POINTER (value));
275       decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
276                                                build_tree_list (NULL, value)),
277                        0);
278     }
279
280   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
281       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
282     warning_with_decl (decl, "applying #pragma weak `%s' after first use results in unspecified behavior");
283
284   declare_weak (decl);
285 }
286
287 void
288 maybe_apply_pragma_weak (decl)
289      tree decl;
290 {
291   tree *p, t, id;
292
293   /* Copied from the check in set_decl_assembler_name.  */
294   if (TREE_CODE (decl) == FUNCTION_DECL
295       || (TREE_CODE (decl) == VAR_DECL 
296           && (TREE_STATIC (decl) 
297               || DECL_EXTERNAL (decl) 
298               || TREE_PUBLIC (decl))))
299     id = DECL_ASSEMBLER_NAME (decl);
300   else
301     return;
302
303   for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
304     if (id == TREE_PURPOSE (t))
305       {
306         apply_pragma_weak (decl, TREE_VALUE (t));
307         *p = TREE_CHAIN (t);
308         break;
309       }
310 }
311
312 /* #pragma weak name [= value] */
313 static void
314 handle_pragma_weak (dummy)
315      cpp_reader *dummy ATTRIBUTE_UNUSED;
316 {
317   tree name, value, x, decl;
318   enum cpp_ttype t;
319
320   value = 0;
321
322   if (c_lex (&name) != CPP_NAME)
323     GCC_BAD ("malformed #pragma weak, ignored");
324   t = c_lex (&x);
325   if (t == CPP_EQ)
326     {
327       if (c_lex (&value) != CPP_NAME)
328         GCC_BAD ("malformed #pragma weak, ignored");
329       t = c_lex (&x);
330     }
331   if (t != CPP_EOF)
332     warning ("junk at end of #pragma weak");
333
334   decl = identifier_global_value (name);
335   if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
336     {
337       apply_pragma_weak (decl, value);
338       if (value)
339         assemble_alias (decl, value);
340     }
341   else
342     pending_weaks = tree_cons (name, value, pending_weaks);
343 }
344 #else
345 void
346 maybe_apply_pragma_weak (decl)
347      tree decl ATTRIBUTE_UNUSED;
348 {
349 }
350 #endif /* HANDLE_PRAGMA_WEAK */
351
352 static GTY(()) tree pending_redefine_extname;
353
354 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
355 static void handle_pragma_redefine_extname PARAMS ((cpp_reader *));
356
357 /* #pragma redefined_extname oldname newname */
358 static void
359 handle_pragma_redefine_extname (dummy)
360      cpp_reader *dummy ATTRIBUTE_UNUSED;
361 {
362   tree oldname, newname, decl, x;
363   enum cpp_ttype t;
364
365   if (c_lex (&oldname) != CPP_NAME)
366     {
367       warning ("malformed #pragma redefine_extname, ignored");
368       return;
369     }
370   if (c_lex (&newname) != CPP_NAME)
371     {
372       warning ("malformed #pragma redefine_extname, ignored");
373       return;
374     }
375   t = c_lex (&x);
376   if (t != CPP_EOF)
377     warning ("junk at end of #pragma redefine_extname");
378
379   decl = identifier_global_value (oldname);
380   if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
381     {
382       if (DECL_ASSEMBLER_NAME_SET_P (decl)
383           && DECL_ASSEMBLER_NAME (decl) != newname)
384         warning ("#pragma redefine_extname conflicts with declaration");
385       SET_DECL_ASSEMBLER_NAME (decl, newname);
386     }
387   else
388     add_to_renaming_pragma_list(oldname, newname);
389 }
390 #endif
391
392 void
393 add_to_renaming_pragma_list (oldname, newname)
394         tree oldname, newname;
395 {
396   pending_redefine_extname
397     = tree_cons (oldname, newname, pending_redefine_extname);
398 }
399
400 static GTY(()) tree pragma_extern_prefix;
401
402 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
403 static void handle_pragma_extern_prefix PARAMS ((cpp_reader *));
404
405 /* #pragma extern_prefix "prefix" */
406 static void
407 handle_pragma_extern_prefix (dummy)
408      cpp_reader *dummy ATTRIBUTE_UNUSED;
409 {
410   tree prefix, x;
411   enum cpp_ttype t;
412
413   if (c_lex (&prefix) != CPP_STRING)
414     {
415       warning ("malformed #pragma extern_prefix, ignored");
416       return;
417     }
418   t = c_lex (&x);
419   if (t != CPP_EOF)
420     warning ("junk at end of #pragma extern_prefix");
421
422   /* Note that the length includes the null terminator.  */
423   pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
424 }
425 #endif
426
427 /* Hook from the front ends to apply the results of one of the preceding
428    pragmas that rename variables.  */
429
430 tree
431 maybe_apply_renaming_pragma (decl, asmname)
432      tree decl, asmname;
433 {
434   tree oldname;
435
436   /* Copied from the check in set_decl_assembler_name.  */
437   if (TREE_CODE (decl) == FUNCTION_DECL
438       || (TREE_CODE (decl) == VAR_DECL 
439           && (TREE_STATIC (decl) 
440               || DECL_EXTERNAL (decl) 
441               || TREE_PUBLIC (decl))))
442     oldname = DECL_ASSEMBLER_NAME (decl);
443   else
444     return asmname;
445
446   /* If the name begins with a *, that's a sign of an asmname attached to
447      a previous declaration.  */
448   if (IDENTIFIER_POINTER (oldname)[0] == '*')
449     {
450       const char *oldasmname = IDENTIFIER_POINTER (oldname) + 1;
451       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldasmname) != 0)
452         warning ("asm declaration conflicts with previous rename");
453       asmname = build_string (strlen (oldasmname), oldasmname);
454     }
455
456   {
457     tree *p, t;
458
459     for (p = &pending_redefine_extname; (t = *p) ; p = &TREE_CHAIN (t))
460       if (oldname == TREE_PURPOSE (t))
461         {
462           const char *newname = IDENTIFIER_POINTER (TREE_VALUE (t));
463
464           if (asmname && strcmp (TREE_STRING_POINTER (asmname), newname) != 0)
465             warning ("#pragma redefine_extname conflicts with declaration");
466           *p = TREE_CHAIN (t);
467
468           return build_string (strlen (newname), newname);
469         }
470   }
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 /* Front-end wrapper for pragma registration to avoid dragging
487    cpplib.h in almost everywhere.  */
488 void
489 c_register_pragma (space, name, handler)
490      const char *space;
491      const char *name;
492      void (*handler) PARAMS ((struct cpp_reader *));
493 {
494   cpp_register_pragma (parse_in, space, name, handler);
495 }
496
497 /* Set up front-end pragmas.  */
498 void
499 init_pragma ()
500 {
501 #ifdef HANDLE_PRAGMA_PACK
502   c_register_pragma (0, "pack", handle_pragma_pack);
503 #endif
504 #ifdef HANDLE_PRAGMA_WEAK
505   c_register_pragma (0, "weak", handle_pragma_weak);
506 #endif
507 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
508   c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
509 #endif
510 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
511   c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
512 #endif
513
514 #ifdef REGISTER_TARGET_PRAGMAS
515   REGISTER_TARGET_PRAGMAS ();
516 #endif
517 }
518
519 #include "gt-c-pragma.h"