* sv.po, zh_CN.po: Update.
[official-gcc.git] / gcc / c-pragma.c
blob65b12ce54de9a29d413c0a0068f0e0cb3b99d5d7
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, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 "vec.h"
38 #include "target.h"
40 #define GCC_BAD(gmsgid) do { warning (0, gmsgid); return; } while (0)
41 #define GCC_BAD2(gmsgid, arg) \
42 do { warning (0, gmsgid, arg); return; } while (0)
44 typedef struct align_stack GTY(())
46 int alignment;
47 tree id;
48 struct align_stack * prev;
49 } align_stack;
51 static GTY(()) struct align_stack * alignment_stack;
53 #ifdef HANDLE_PRAGMA_PACK
54 static void handle_pragma_pack (cpp_reader *);
56 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
57 /* If we have a "global" #pragma pack(<n>) in effect when the first
58 #pragma pack(push,<n>) is encountered, this stores the value of
59 maximum_field_alignment in effect. When the final pop_alignment()
60 happens, we restore the value to this, not to a value of 0 for
61 maximum_field_alignment. Value is in bits. */
62 static int default_alignment;
63 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
64 ? &default_alignment \
65 : &alignment_stack->alignment) = (ALIGN))
67 static void push_alignment (int, tree);
68 static void pop_alignment (tree);
70 /* Push an alignment value onto the stack. */
71 static void
72 push_alignment (int alignment, tree id)
74 align_stack * entry;
76 entry = ggc_alloc (sizeof (* entry));
78 entry->alignment = alignment;
79 entry->id = id;
80 entry->prev = alignment_stack;
82 /* The current value of maximum_field_alignment is not necessarily
83 0 since there may be a #pragma pack(<n>) in effect; remember it
84 so that we can restore it after the final #pragma pop(). */
85 if (alignment_stack == NULL)
86 default_alignment = maximum_field_alignment;
88 alignment_stack = entry;
90 maximum_field_alignment = alignment;
93 /* Undo a push of an alignment onto the stack. */
94 static void
95 pop_alignment (tree id)
97 align_stack * entry;
99 if (alignment_stack == NULL)
100 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
102 /* If we got an identifier, strip away everything above the target
103 entry so that the next step will restore the state just below it. */
104 if (id)
106 for (entry = alignment_stack; entry; entry = entry->prev)
107 if (entry->id == id)
109 alignment_stack = entry;
110 break;
112 if (entry == NULL)
113 warning (0, "\
114 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
115 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
118 entry = alignment_stack->prev;
120 maximum_field_alignment = entry ? entry->alignment : default_alignment;
122 alignment_stack = entry;
124 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
125 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
126 #define push_alignment(ID, N) \
127 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
128 #define pop_alignment(ID) \
129 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
130 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
132 /* #pragma pack ()
133 #pragma pack (N)
135 #pragma pack (push)
136 #pragma pack (push, N)
137 #pragma pack (push, ID)
138 #pragma pack (push, ID, N)
139 #pragma pack (pop)
140 #pragma pack (pop, ID) */
141 static void
142 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
144 tree x, id = 0;
145 int align = -1;
146 enum cpp_ttype token;
147 enum { set, push, pop } action;
149 if (c_lex (&x) != CPP_OPEN_PAREN)
150 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
152 token = c_lex (&x);
153 if (token == CPP_CLOSE_PAREN)
155 action = set;
156 align = initial_max_fld_align;
158 else if (token == CPP_NUMBER)
160 align = TREE_INT_CST_LOW (x);
161 action = set;
162 if (c_lex (&x) != CPP_CLOSE_PAREN)
163 GCC_BAD ("malformed %<#pragma pack%> - ignored");
165 else if (token == CPP_NAME)
167 #define GCC_BAD_ACTION do { if (action != pop) \
168 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
169 else \
170 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
171 } while (0)
173 const char *op = IDENTIFIER_POINTER (x);
174 if (!strcmp (op, "push"))
175 action = push;
176 else if (!strcmp (op, "pop"))
177 action = pop;
178 else
179 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op);
181 while ((token = c_lex (&x)) == CPP_COMMA)
183 token = c_lex (&x);
184 if (token == CPP_NAME && id == 0)
186 id = x;
188 else if (token == CPP_NUMBER && action == push && align == -1)
190 align = TREE_INT_CST_LOW (x);
191 if (align == -1)
192 action = set;
194 else
195 GCC_BAD_ACTION;
198 if (token != CPP_CLOSE_PAREN)
199 GCC_BAD_ACTION;
200 #undef GCC_BAD_ACTION
202 else
203 GCC_BAD ("malformed %<#pragma pack%> - ignored");
205 if (c_lex (&x) != CPP_EOF)
206 warning (0, "junk at end of %<#pragma pack%>");
208 if (flag_pack_struct)
209 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
211 if (action != pop)
212 switch (align)
214 case 0:
215 case 1:
216 case 2:
217 case 4:
218 case 8:
219 case 16:
220 align *= BITS_PER_UNIT;
221 break;
222 case -1:
223 if (action == push)
225 align = maximum_field_alignment;
226 break;
228 default:
229 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
232 switch (action)
234 case set: SET_GLOBAL_ALIGNMENT (align); break;
235 case push: push_alignment (align, id); break;
236 case pop: pop_alignment (id); break;
239 #endif /* HANDLE_PRAGMA_PACK */
241 static GTY(()) tree pending_weaks;
243 #ifdef HANDLE_PRAGMA_WEAK
244 static void apply_pragma_weak (tree, tree);
245 static void handle_pragma_weak (cpp_reader *);
247 static void
248 apply_pragma_weak (tree decl, tree value)
250 if (value)
252 value = build_string (IDENTIFIER_LENGTH (value),
253 IDENTIFIER_POINTER (value));
254 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
255 build_tree_list (NULL, value)),
259 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
260 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
261 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
262 warning (0, "applying #pragma weak %q+D after first use results "
263 "in unspecified behavior", decl);
265 declare_weak (decl);
268 void
269 maybe_apply_pragma_weak (tree decl)
271 tree *p, t, id;
273 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
275 /* No weak symbols pending, take the short-cut. */
276 if (!pending_weaks)
277 return;
278 /* If it's not visible outside this file, it doesn't matter whether
279 it's weak. */
280 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
281 return;
282 /* If it's not a function or a variable, it can't be weak.
283 FIXME: what kinds of things are visible outside this file but
284 aren't functions or variables? Should this be an assert instead? */
285 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
286 return;
288 id = DECL_ASSEMBLER_NAME (decl);
290 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
291 if (id == TREE_PURPOSE (t))
293 apply_pragma_weak (decl, TREE_VALUE (t));
294 *p = TREE_CHAIN (t);
295 break;
299 /* Process all "#pragma weak A = B" directives where we have not seen
300 a decl for A. */
301 void
302 maybe_apply_pending_pragma_weaks (void)
304 tree *p, t, alias_id, id, decl, *next;
306 for (p = &pending_weaks; (t = *p) ; p = next)
308 next = &TREE_CHAIN (t);
309 alias_id = TREE_PURPOSE (t);
310 id = TREE_VALUE (t);
312 if (TREE_VALUE (t) == NULL)
313 continue;
315 decl = build_decl (FUNCTION_DECL, alias_id, default_function_type);
317 DECL_ARTIFICIAL (decl) = 1;
318 TREE_PUBLIC (decl) = 1;
319 DECL_EXTERNAL (decl) = 1;
320 DECL_WEAK (decl) = 1;
322 assemble_alias (decl, id);
326 /* #pragma weak name [= value] */
327 static void
328 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
330 tree name, value, x, decl;
331 enum cpp_ttype t;
333 value = 0;
335 if (c_lex (&name) != CPP_NAME)
336 GCC_BAD ("malformed #pragma weak, ignored");
337 t = c_lex (&x);
338 if (t == CPP_EQ)
340 if (c_lex (&value) != CPP_NAME)
341 GCC_BAD ("malformed #pragma weak, ignored");
342 t = c_lex (&x);
344 if (t != CPP_EOF)
345 warning (0, "junk at end of #pragma weak");
347 decl = identifier_global_value (name);
348 if (decl && DECL_P (decl))
350 apply_pragma_weak (decl, value);
351 if (value)
352 assemble_alias (decl, value);
354 else
355 pending_weaks = tree_cons (name, value, pending_weaks);
357 #else
358 void
359 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
363 void
364 maybe_apply_pending_pragma_weaks (void)
367 #endif /* HANDLE_PRAGMA_WEAK */
369 /* GCC supports two #pragma directives for renaming the external
370 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
371 compatibility with the Solaris and Tru64 system headers. GCC also
372 has its own notation for this, __asm__("name") annotations.
374 Corner cases of these features and their interaction:
376 1) Both pragmas silently apply only to declarations with external
377 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
378 do not have this restriction.
380 2) In C++, both #pragmas silently apply only to extern "C" declarations.
381 Asm labels do not have this restriction.
383 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
384 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
385 new name is different, a warning issues and the name does not change.
387 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
388 *not* the DECL_ASSEMBLER_NAME.
390 5) If #pragma extern_prefix is in effect and a declaration occurs
391 with an __asm__ name, the #pragma extern_prefix is silently
392 ignored for that declaration.
394 6) If #pragma extern_prefix and #pragma redefine_extname apply to
395 the same declaration, whichever triggered first wins, and a warning
396 is issued. (We would like to have #pragma redefine_extname always
397 win, but it can appear either before or after the declaration, and
398 if it appears afterward, we have no way of knowing whether a modified
399 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
401 static GTY(()) tree pending_redefine_extname;
403 static void handle_pragma_redefine_extname (cpp_reader *);
405 /* #pragma redefine_extname oldname newname */
406 static void
407 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
409 tree oldname, newname, decl, x;
410 enum cpp_ttype t;
412 if (c_lex (&oldname) != CPP_NAME)
413 GCC_BAD ("malformed #pragma redefine_extname, ignored");
414 if (c_lex (&newname) != CPP_NAME)
415 GCC_BAD ("malformed #pragma redefine_extname, ignored");
416 t = c_lex (&x);
417 if (t != CPP_EOF)
418 warning (0, "junk at end of #pragma redefine_extname");
420 if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
422 if (warn_unknown_pragmas > in_system_header)
423 warning (OPT_Wunknown_pragmas,
424 "#pragma redefine_extname not supported on this target");
425 return;
428 decl = identifier_global_value (oldname);
429 if (decl
430 && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
431 && (TREE_CODE (decl) == FUNCTION_DECL
432 || TREE_CODE (decl) == VAR_DECL)
433 && has_c_linkage (decl))
435 if (DECL_ASSEMBLER_NAME_SET_P (decl))
437 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
438 name = targetm.strip_name_encoding (name);
440 if (strcmp (name, IDENTIFIER_POINTER (newname)))
441 warning (0, "#pragma redefine_extname ignored due to conflict with "
442 "previous rename");
444 else
445 change_decl_assembler_name (decl, newname);
447 else
448 /* We have to add this to the rename list even if there's already
449 a global value that doesn't meet the above criteria, because in
450 C++ "struct foo {...};" puts "foo" in the current namespace but
451 does *not* conflict with a subsequent declaration of a function
452 or variable foo. See g++.dg/other/pragma-re-2.C. */
453 add_to_renaming_pragma_list (oldname, newname);
456 /* This is called from here and from ia64.c. */
457 void
458 add_to_renaming_pragma_list (tree oldname, tree newname)
460 tree previous = purpose_member (oldname, pending_redefine_extname);
461 if (previous)
463 if (TREE_VALUE (previous) != newname)
464 warning (0, "#pragma redefine_extname ignored due to conflict with "
465 "previous #pragma redefine_extname");
466 return;
469 pending_redefine_extname
470 = tree_cons (oldname, newname, pending_redefine_extname);
473 static GTY(()) tree pragma_extern_prefix;
475 /* #pragma extern_prefix "prefix" */
476 static void
477 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
479 tree prefix, x;
480 enum cpp_ttype t;
482 if (c_lex (&prefix) != CPP_STRING)
483 GCC_BAD ("malformed #pragma extern_prefix, ignored");
484 t = c_lex (&x);
485 if (t != CPP_EOF)
486 warning (0, "junk at end of #pragma extern_prefix");
488 if (targetm.handle_pragma_extern_prefix)
489 /* Note that the length includes the null terminator. */
490 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
491 else if (warn_unknown_pragmas > in_system_header)
492 warning (OPT_Wunknown_pragmas,
493 "#pragma extern_prefix not supported on this target");
496 /* Hook from the front ends to apply the results of one of the preceding
497 pragmas that rename variables. */
499 tree
500 maybe_apply_renaming_pragma (tree decl, tree asmname)
502 tree *p, t;
504 /* The renaming pragmas are only applied to declarations with
505 external linkage. */
506 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
507 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
508 || !has_c_linkage (decl))
509 return asmname;
511 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
512 but we may warn about a rename that conflicts. */
513 if (DECL_ASSEMBLER_NAME_SET_P (decl))
515 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
516 oldname = targetm.strip_name_encoding (oldname);
518 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
519 warning (0, "asm declaration ignored due to "
520 "conflict with previous rename");
522 /* Take any pending redefine_extname off the list. */
523 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
524 if (DECL_NAME (decl) == TREE_PURPOSE (t))
526 /* Only warn if there is a conflict. */
527 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
528 warning (0, "#pragma redefine_extname ignored due to "
529 "conflict with previous rename");
531 *p = TREE_CHAIN (t);
532 break;
534 return 0;
537 /* Find out if we have a pending #pragma redefine_extname. */
538 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
539 if (DECL_NAME (decl) == TREE_PURPOSE (t))
541 tree newname = TREE_VALUE (t);
542 *p = TREE_CHAIN (t);
544 /* If we already have an asmname, #pragma redefine_extname is
545 ignored (with a warning if it conflicts). */
546 if (asmname)
548 if (strcmp (TREE_STRING_POINTER (asmname),
549 IDENTIFIER_POINTER (newname)) != 0)
550 warning (0, "#pragma redefine_extname ignored due to "
551 "conflict with __asm__ declaration");
552 return asmname;
555 /* Otherwise we use what we've got; #pragma extern_prefix is
556 silently ignored. */
557 return build_string (IDENTIFIER_LENGTH (newname),
558 IDENTIFIER_POINTER (newname));
561 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
562 if (asmname)
563 return asmname;
565 /* If #pragma extern_prefix is in effect, apply it. */
566 if (pragma_extern_prefix)
568 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
569 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
571 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
572 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
574 char *newname = (char *) alloca (plen + ilen + 1);
576 memcpy (newname, prefix, plen);
577 memcpy (newname + plen, id, ilen + 1);
579 return build_string (plen + ilen, newname);
582 /* Nada. */
583 return 0;
587 #ifdef HANDLE_PRAGMA_VISIBILITY
588 static void handle_pragma_visibility (cpp_reader *);
590 typedef enum symbol_visibility visibility;
591 DEF_VEC_I (visibility);
592 DEF_VEC_ALLOC_I (visibility, heap);
594 /* Sets the default visibility for symbols to something other than that
595 specified on the command line. */
596 static void
597 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
599 /* Form is #pragma GCC visibility push(hidden)|pop */
600 tree x;
601 enum cpp_ttype token;
602 enum { bad, push, pop } action = bad;
603 static VEC (visibility, heap) *visstack;
605 token = c_lex (&x);
606 if (token == CPP_NAME)
608 const char *op = IDENTIFIER_POINTER (x);
609 if (!strcmp (op, "push"))
610 action = push;
611 else if (!strcmp (op, "pop"))
612 action = pop;
614 if (bad == action)
615 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
616 else
618 if (pop == action)
620 if (!VEC_length (visibility, visstack))
622 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
624 else
626 default_visibility = VEC_pop (visibility, visstack);
627 visibility_options.inpragma
628 = VEC_length (visibility, visstack) != 0;
631 else
633 if (c_lex (&x) != CPP_OPEN_PAREN)
634 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
635 token = c_lex (&x);
636 if (token != CPP_NAME)
638 GCC_BAD ("malformed #pragma GCC visibility push");
640 else
642 const char *str = IDENTIFIER_POINTER (x);
643 VEC_safe_push (visibility, heap, visstack,
644 default_visibility);
645 if (!strcmp (str, "default"))
646 default_visibility = VISIBILITY_DEFAULT;
647 else if (!strcmp (str, "internal"))
648 default_visibility = VISIBILITY_INTERNAL;
649 else if (!strcmp (str, "hidden"))
650 default_visibility = VISIBILITY_HIDDEN;
651 else if (!strcmp (str, "protected"))
652 default_visibility = VISIBILITY_PROTECTED;
653 else
655 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
657 visibility_options.inpragma = 1;
659 if (c_lex (&x) != CPP_CLOSE_PAREN)
660 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
663 if (c_lex (&x) != CPP_EOF)
664 warning (0, "junk at end of %<#pragma GCC visibility%>");
667 #endif
669 /* Front-end wrappers for pragma registration to avoid dragging
670 cpplib.h in almost everywhere. */
671 void
672 c_register_pragma (const char *space, const char *name,
673 void (*handler) (struct cpp_reader *))
675 cpp_register_pragma (parse_in, space, name, handler, 0);
678 void
679 c_register_pragma_with_expansion (const char *space, const char *name,
680 void (*handler) (struct cpp_reader *))
682 cpp_register_pragma (parse_in, space, name, handler, 1);
685 /* Set up front-end pragmas. */
686 void
687 init_pragma (void)
689 #ifdef HANDLE_PRAGMA_PACK
690 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
691 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
692 #else
693 c_register_pragma (0, "pack", handle_pragma_pack);
694 #endif
695 #endif
696 #ifdef HANDLE_PRAGMA_WEAK
697 c_register_pragma (0, "weak", handle_pragma_weak);
698 #endif
699 #ifdef HANDLE_PRAGMA_VISIBILITY
700 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
701 #endif
703 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
704 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
706 c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma);
708 #ifdef REGISTER_TARGET_PRAGMAS
709 REGISTER_TARGET_PRAGMAS ();
710 #endif
713 #include "gt-c-pragma.h"