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
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
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
24 #include "coretypes.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(())
48 struct align_stack
* prev
;
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. */
72 push_alignment (int alignment
, tree id
)
76 entry
= ggc_alloc (sizeof (* entry
));
78 entry
->alignment
= alignment
;
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. */
95 pop_alignment (tree id
)
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. */
106 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
109 alignment_stack
= entry
;
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 */
136 #pragma pack (push, N)
137 #pragma pack (push, ID)
138 #pragma pack (push, ID, N)
140 #pragma pack (pop, ID) */
142 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
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");
153 if (token
== CPP_CLOSE_PAREN
)
156 align
= initial_max_fld_align
;
158 else if (token
== CPP_NUMBER
)
160 align
= TREE_INT_CST_LOW (x
);
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"); \
170 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
173 const char *op
= IDENTIFIER_POINTER (x
);
174 if (!strcmp (op
, "push"))
176 else if (!strcmp (op
, "pop"))
179 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op
);
181 while ((token
= c_lex (&x
)) == CPP_COMMA
)
184 if (token
== CPP_NAME
&& id
== 0)
188 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
190 align
= TREE_INT_CST_LOW (x
);
198 if (token
!= CPP_CLOSE_PAREN
)
200 #undef GCC_BAD_ACTION
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");
220 align
*= BITS_PER_UNIT
;
225 align
= maximum_field_alignment
;
229 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
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
*);
248 apply_pragma_weak (tree decl
, tree 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
);
269 maybe_apply_pragma_weak (tree decl
)
273 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
275 /* No weak symbols pending, take the short-cut. */
278 /* If it's not visible outside this file, it doesn't matter whether
280 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
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
)
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
));
299 /* Process all "#pragma weak A = B" directives where we have not seen
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
);
312 if (TREE_VALUE (t
) == NULL
)
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] */
328 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
330 tree name
, value
, x
, decl
;
335 if (c_lex (&name
) != CPP_NAME
)
336 GCC_BAD ("malformed #pragma weak, ignored");
340 if (c_lex (&value
) != CPP_NAME
)
341 GCC_BAD ("malformed #pragma weak, ignored");
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
);
352 assemble_alias (decl
, value
);
355 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
359 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
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 */
407 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
409 tree oldname
, newname
, decl
, x
;
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");
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");
428 decl
= identifier_global_value (oldname
);
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 "
445 change_decl_assembler_name (decl
, newname
);
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. */
458 add_to_renaming_pragma_list (tree oldname
, tree newname
)
460 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
463 if (TREE_VALUE (previous
) != newname
)
464 warning (0, "#pragma redefine_extname ignored due to conflict with "
465 "previous #pragma redefine_extname");
469 pending_redefine_extname
470 = tree_cons (oldname
, newname
, pending_redefine_extname
);
473 static GTY(()) tree pragma_extern_prefix
;
475 /* #pragma extern_prefix "prefix" */
477 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
482 if (c_lex (&prefix
) != CPP_STRING
)
483 GCC_BAD ("malformed #pragma extern_prefix, ignored");
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. */
500 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
504 /* The renaming pragmas are only applied to declarations with
506 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
507 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
508 || !has_c_linkage (decl
))
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");
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
);
544 /* If we already have an asmname, #pragma redefine_extname is
545 ignored (with a warning if it conflicts). */
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");
555 /* Otherwise we use what we've got; #pragma extern_prefix is
557 return build_string (IDENTIFIER_LENGTH (newname
),
558 IDENTIFIER_POINTER (newname
));
561 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
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
);
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. */
597 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
599 /* Form is #pragma GCC visibility push(hidden)|pop */
601 enum cpp_ttype token
;
602 enum { bad
, push
, pop
} action
= bad
;
603 static VEC (visibility
, heap
) *visstack
;
606 if (token
== CPP_NAME
)
608 const char *op
= IDENTIFIER_POINTER (x
);
609 if (!strcmp (op
, "push"))
611 else if (!strcmp (op
, "pop"))
615 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
620 if (!VEC_length (visibility
, visstack
))
622 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
626 default_visibility
= VEC_pop (visibility
, visstack
);
627 visibility_options
.inpragma
628 = VEC_length (visibility
, visstack
) != 0;
633 if (c_lex (&x
) != CPP_OPEN_PAREN
)
634 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
636 if (token
!= CPP_NAME
)
638 GCC_BAD ("malformed #pragma GCC visibility push");
642 const char *str
= IDENTIFIER_POINTER (x
);
643 VEC_safe_push (visibility
, heap
, visstack
,
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
;
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%>");
669 /* Front-end wrappers for pragma registration to avoid dragging
670 cpplib.h in almost everywhere. */
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);
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. */
689 #ifdef HANDLE_PRAGMA_PACK
690 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
691 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
693 c_register_pragma (0, "pack", handle_pragma_pack
);
696 #ifdef HANDLE_PRAGMA_WEAK
697 c_register_pragma (0, "weak", handle_pragma_weak
);
699 #ifdef HANDLE_PRAGMA_VISIBILITY
700 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
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 ();
713 #include "gt-c-pragma.h"