* cp-tree.h (enum cp_storage_class): Remove trailing comma.
[official-gcc.git] / gcc / c-pragma.c
blobba59fb3a589a1ae67ed7f65381590f66932c5aec
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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. */
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 #include "target.h"
39 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
40 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
42 typedef struct align_stack GTY(())
44 int alignment;
45 unsigned int num_pushes;
46 tree id;
47 struct align_stack * prev;
48 } align_stack;
50 static GTY(()) struct align_stack * alignment_stack;
52 #ifdef HANDLE_PRAGMA_PACK
53 static void handle_pragma_pack (cpp_reader *);
55 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
56 /* If we have a "global" #pragma pack(<n>) in effect when the first
57 #pragma pack(push,<n>) is encountered, this stores the value of
58 maximum_field_alignment in effect. When the final pop_alignment()
59 happens, we restore the value to this, not to a value of 0 for
60 maximum_field_alignment. Value is in bits. */
61 static int default_alignment;
62 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
63 (default_alignment = maximum_field_alignment = (ALIGN))
65 static void push_alignment (int, tree);
66 static void pop_alignment (tree);
68 /* Push an alignment value onto the stack. */
69 static void
70 push_alignment (int alignment, tree id)
72 if (alignment_stack == NULL
73 || alignment_stack->alignment != alignment
74 || id != NULL_TREE)
76 align_stack * entry;
78 entry = ggc_alloc (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 (tree id)
103 align_stack * entry;
105 if (alignment_stack == NULL)
107 warning ("\
108 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
110 return;
113 /* If we got an identifier, strip away everything above the target
114 entry so that the next step will restore the state just below it. */
115 if (id)
117 for (entry = alignment_stack; entry; entry = entry->prev)
118 if (entry->id == id)
120 entry->num_pushes = 1;
121 alignment_stack = entry;
122 break;
124 if (entry == NULL)
125 warning ("\
126 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
127 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
130 if (-- alignment_stack->num_pushes == 0)
132 entry = alignment_stack->prev;
134 if (entry == NULL)
135 maximum_field_alignment = default_alignment;
136 else
137 maximum_field_alignment = entry->alignment;
139 alignment_stack = entry;
142 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
143 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
144 #define push_alignment(ID, N) \
145 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
146 #define pop_alignment(ID) \
147 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
148 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
150 /* #pragma pack ()
151 #pragma pack (N)
153 #pragma pack (push, N)
154 #pragma pack (push, ID, N)
155 #pragma pack (pop)
156 #pragma pack (pop, ID) */
157 static void
158 handle_pragma_pack (cpp_reader *dummy ATTRIBUTE_UNUSED)
160 tree x, id = 0;
161 int align = -1;
162 enum cpp_ttype token;
163 enum { set, push, pop } action;
165 if (c_lex (&x) != CPP_OPEN_PAREN)
166 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
168 token = c_lex (&x);
169 if (token == CPP_CLOSE_PAREN)
171 action = set;
172 align = 0;
174 else if (token == CPP_NUMBER)
176 align = TREE_INT_CST_LOW (x);
177 action = set;
178 if (c_lex (&x) != CPP_CLOSE_PAREN)
179 GCC_BAD ("malformed '#pragma pack' - ignored");
181 else if (token == CPP_NAME)
183 #define GCC_BAD_ACTION do { if (action == push) \
184 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
185 else \
186 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
187 } while (0)
189 const char *op = IDENTIFIER_POINTER (x);
190 if (!strcmp (op, "push"))
191 action = push;
192 else if (!strcmp (op, "pop"))
193 action = pop;
194 else
195 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
197 token = c_lex (&x);
198 if (token != CPP_COMMA && action == push)
199 GCC_BAD_ACTION;
201 if (token == CPP_COMMA)
203 token = c_lex (&x);
204 if (token == CPP_NAME)
206 id = x;
207 if (action == push && c_lex (&x) != CPP_COMMA)
208 GCC_BAD_ACTION;
209 token = c_lex (&x);
212 if (action == push)
214 if (token == CPP_NUMBER)
216 align = TREE_INT_CST_LOW (x);
217 token = c_lex (&x);
219 else
220 GCC_BAD_ACTION;
224 if (token != CPP_CLOSE_PAREN)
225 GCC_BAD_ACTION;
226 #undef GCC_BAD_ACTION
228 else
229 GCC_BAD ("malformed '#pragma pack' - ignored");
231 if (c_lex (&x) != CPP_EOF)
232 warning ("junk at end of '#pragma pack'");
234 if (action != pop)
235 switch (align)
237 case 0:
238 case 1:
239 case 2:
240 case 4:
241 case 8:
242 case 16:
243 align *= BITS_PER_UNIT;
244 break;
245 default:
246 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
249 switch (action)
251 case set: SET_GLOBAL_ALIGNMENT (align); break;
252 case push: push_alignment (align, id); break;
253 case pop: pop_alignment (id); break;
256 #endif /* HANDLE_PRAGMA_PACK */
258 static GTY(()) tree pending_weaks;
260 #ifdef HANDLE_PRAGMA_WEAK
261 static void apply_pragma_weak (tree, tree);
262 static void handle_pragma_weak (cpp_reader *);
264 static void
265 apply_pragma_weak (tree decl, tree value)
267 if (value)
269 value = build_string (IDENTIFIER_LENGTH (value),
270 IDENTIFIER_POINTER (value));
271 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
272 build_tree_list (NULL, value)),
276 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
277 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
278 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
279 warning ("%Japplying #pragma weak '%D' after first use results "
280 "in unspecified behavior", decl, decl);
282 declare_weak (decl);
285 void
286 maybe_apply_pragma_weak (tree decl)
288 tree *p, t, id;
290 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
292 /* No weak symbols pending, take the short-cut. */
293 if (!pending_weaks)
294 return;
295 /* If it's not visible outside this file, it doesn't matter whether
296 it's weak. */
297 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
298 return;
299 /* If it's not a function or a variable, it can't be weak.
300 FIXME: what kinds of things are visible outside this file but
301 aren't functions or variables? Should this be an abort() instead? */
302 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
303 return;
305 id = DECL_ASSEMBLER_NAME (decl);
307 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
308 if (id == TREE_PURPOSE (t))
310 apply_pragma_weak (decl, TREE_VALUE (t));
311 *p = TREE_CHAIN (t);
312 break;
316 /* #pragma weak name [= value] */
317 static void
318 handle_pragma_weak (cpp_reader *dummy ATTRIBUTE_UNUSED)
320 tree name, value, x, decl;
321 enum cpp_ttype t;
323 value = 0;
325 if (c_lex (&name) != CPP_NAME)
326 GCC_BAD ("malformed #pragma weak, ignored");
327 t = c_lex (&x);
328 if (t == CPP_EQ)
330 if (c_lex (&value) != CPP_NAME)
331 GCC_BAD ("malformed #pragma weak, ignored");
332 t = c_lex (&x);
334 if (t != CPP_EOF)
335 warning ("junk at end of #pragma weak");
337 decl = identifier_global_value (name);
338 if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
340 apply_pragma_weak (decl, value);
341 if (value)
342 assemble_alias (decl, value);
344 else
345 pending_weaks = tree_cons (name, value, pending_weaks);
347 #else
348 void
349 maybe_apply_pragma_weak (tree decl ATTRIBUTE_UNUSED)
352 #endif /* HANDLE_PRAGMA_WEAK */
354 /* GCC supports two #pragma directives for renaming the external
355 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
356 compatibility with the Solaris and Tru64 system headers. GCC also
357 has its own notation for this, __asm__("name") annotations.
359 Corner cases of these features and their interaction:
361 1) Both pragmas silently apply only to declarations with external
362 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
363 do not have this restriction.
365 2) In C++, both #pragmas silently apply only to extern "C" declarations.
366 Asm labels do not have this restriction.
368 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
369 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
370 new name is different, a warning issues and the name does not change.
372 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
373 *not* the DECL_ASSEMBLER_NAME.
375 5) If #pragma extern_prefix is in effect and a declaration occurs
376 with an __asm__ name, the #pragma extern_prefix is silently
377 ignored for that declaration.
379 6) If #pragma extern_prefix and #pragma redefine_extname apply to
380 the same declaration, whichever triggered first wins, and a warning
381 is issued. (We would like to have #pragma redefine_extname always
382 win, but it can appear either before or after the declaration, and
383 if it appears afterward, we have no way of knowing whether a modified
384 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
386 static GTY(()) tree pending_redefine_extname;
388 static void handle_pragma_redefine_extname (cpp_reader *);
390 /* #pragma redefine_extname oldname newname */
391 static void
392 handle_pragma_redefine_extname (cpp_reader *dummy ATTRIBUTE_UNUSED)
394 tree oldname, newname, decl, x;
395 enum cpp_ttype t;
397 if (c_lex (&oldname) != CPP_NAME)
398 GCC_BAD ("malformed #pragma redefine_extname, ignored");
399 if (c_lex (&newname) != CPP_NAME)
400 GCC_BAD ("malformed #pragma redefine_extname, ignored");
401 t = c_lex (&x);
402 if (t != CPP_EOF)
403 warning ("junk at end of #pragma redefine_extname");
405 if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
407 if (warn_unknown_pragmas > in_system_header)
408 warning ("#pragma redefine_extname not supported on this target");
409 return;
412 decl = identifier_global_value (oldname);
413 if (decl
414 && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
415 && (TREE_CODE (decl) == FUNCTION_DECL
416 || TREE_CODE (decl) == VAR_DECL)
417 && has_c_linkage (decl))
419 if (DECL_ASSEMBLER_NAME_SET_P (decl))
421 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
422 name = targetm.strip_name_encoding (name);
424 if (strcmp (name, IDENTIFIER_POINTER (newname)))
425 warning ("#pragma redefine_extname ignored due to conflict with "
426 "previous rename");
428 else
429 change_decl_assembler_name (decl, newname);
431 else
432 /* We have to add this to the rename list even if there's already
433 a global value that doesn't meet the above criteria, because in
434 C++ "struct foo {...};" puts "foo" in the current namespace but
435 does *not* conflict with a subsequent declaration of a function
436 or variable foo. See g++.dg/other/pragma-re-2.C. */
437 add_to_renaming_pragma_list (oldname, newname);
440 /* This is called from here and from ia64.c. */
441 void
442 add_to_renaming_pragma_list (tree oldname, tree newname)
444 tree previous = purpose_member (oldname, pending_redefine_extname);
445 if (previous)
447 if (TREE_VALUE (previous) != newname)
448 warning ("#pragma redefine_extname ignored due to conflict with "
449 "previous #pragma redefine_extname");
450 return;
453 pending_redefine_extname
454 = tree_cons (oldname, newname, pending_redefine_extname);
457 static GTY(()) tree pragma_extern_prefix;
459 /* #pragma extern_prefix "prefix" */
460 static void
461 handle_pragma_extern_prefix (cpp_reader *dummy ATTRIBUTE_UNUSED)
463 tree prefix, x;
464 enum cpp_ttype t;
466 if (c_lex (&prefix) != CPP_STRING)
467 GCC_BAD ("malformed #pragma extern_prefix, ignored");
468 t = c_lex (&x);
469 if (t != CPP_EOF)
470 warning ("junk at end of #pragma extern_prefix");
472 if (targetm.handle_pragma_extern_prefix)
473 /* Note that the length includes the null terminator. */
474 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
475 else if (warn_unknown_pragmas > in_system_header)
476 warning ("#pragma extern_prefix not supported on this target");
479 /* Hook from the front ends to apply the results of one of the preceding
480 pragmas that rename variables. */
482 tree
483 maybe_apply_renaming_pragma (tree decl, tree asmname)
485 tree *p, t;
487 /* The renaming pragmas are only applied to declarations with
488 external linkage. */
489 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
490 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
491 || !has_c_linkage (decl))
492 return asmname;
494 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
495 but we may warn about a rename that conflicts. */
496 if (DECL_ASSEMBLER_NAME_SET_P (decl))
498 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
499 oldname = targetm.strip_name_encoding (oldname);
501 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
502 warning ("asm declaration ignored due to "
503 "conflict with previous rename");
505 /* Take any pending redefine_extname off the list. */
506 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
507 if (DECL_NAME (decl) == TREE_PURPOSE (t))
509 /* Only warn if there is a conflict. */
510 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
511 warning ("#pragma redefine_extname ignored due to "
512 "conflict with previous rename");
514 *p = TREE_CHAIN (t);
515 break;
517 return 0;
520 /* Find out if we have a pending #pragma redefine_extname. */
521 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
522 if (DECL_NAME (decl) == TREE_PURPOSE (t))
524 tree newname = TREE_VALUE (t);
525 *p = TREE_CHAIN (t);
527 /* If we already have an asmname, #pragma redefine_extname is
528 ignored (with a warning if it conflicts). */
529 if (asmname)
531 if (strcmp (TREE_STRING_POINTER (asmname),
532 IDENTIFIER_POINTER (newname)) != 0)
533 warning ("#pragma redefine_extname ignored due to "
534 "conflict with __asm__ declaration");
535 return asmname;
538 /* Otherwise we use what we've got; #pragma extern_prefix is
539 silently ignored. */
540 return build_string (IDENTIFIER_LENGTH (newname),
541 IDENTIFIER_POINTER (newname));
544 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
545 if (asmname)
546 return asmname;
548 /* If #pragma extern_prefix is in effect, apply it. */
549 if (pragma_extern_prefix)
551 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
552 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
554 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
555 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
557 char *newname = alloca (plen + ilen + 1);
559 memcpy (newname, prefix, plen);
560 memcpy (newname + plen, id, ilen + 1);
562 return build_string (plen + ilen, newname);
565 /* Nada. */
566 return 0;
569 /* Front-end wrapper for pragma registration to avoid dragging
570 cpplib.h in almost everywhere. */
571 void
572 c_register_pragma (const char *space, const char *name,
573 void (*handler) (struct cpp_reader *))
575 cpp_register_pragma (parse_in, space, name, handler);
578 /* Set up front-end pragmas. */
579 void
580 init_pragma (void)
582 #ifdef HANDLE_PRAGMA_PACK
583 c_register_pragma (0, "pack", handle_pragma_pack);
584 #endif
585 #ifdef HANDLE_PRAGMA_WEAK
586 c_register_pragma (0, "weak", handle_pragma_weak);
587 #endif
589 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
590 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
592 c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma);
594 #ifdef REGISTER_TARGET_PRAGMAS
595 REGISTER_TARGET_PRAGMAS ();
596 #endif
599 #include "gt-c-pragma.h"