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, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
40 #define GCC_BAD(msgid) do { warning (0, msgid); return; } while (0)
41 #define GCC_BAD2(msgid, arg) do { warning (0, msgid, arg); return; } while (0)
43 typedef struct align_stack
GTY(())
47 struct align_stack
* prev
;
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) (maximum_field_alignment = *(alignment_stack == NULL \
63 ? &default_alignment \
64 : &alignment_stack->alignment) = (ALIGN))
66 static void push_alignment (int, tree
);
67 static void pop_alignment (tree
);
69 /* Push an alignment value onto the stack. */
71 push_alignment (int alignment
, tree id
)
75 entry
= ggc_alloc (sizeof (* entry
));
77 entry
->alignment
= alignment
;
79 entry
->prev
= alignment_stack
;
81 /* The current value of maximum_field_alignment is not necessarily
82 0 since there may be a #pragma pack(<n>) in effect; remember it
83 so that we can restore it after the final #pragma pop(). */
84 if (alignment_stack
== NULL
)
85 default_alignment
= maximum_field_alignment
;
87 alignment_stack
= entry
;
89 maximum_field_alignment
= alignment
;
92 /* Undo a push of an alignment onto the stack. */
94 pop_alignment (tree id
)
98 if (alignment_stack
== NULL
)
99 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
101 /* If we got an identifier, strip away everything above the target
102 entry so that the next step will restore the state just below it. */
105 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
108 alignment_stack
= entry
;
113 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
114 , IDENTIFIER_POINTER (id
), IDENTIFIER_POINTER (id
));
117 entry
= alignment_stack
->prev
;
119 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
121 alignment_stack
= entry
;
123 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
124 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
125 #define push_alignment(ID, N) \
126 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
127 #define pop_alignment(ID) \
128 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
129 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
135 #pragma pack (push, N)
136 #pragma pack (push, ID)
137 #pragma pack (push, ID, N)
139 #pragma pack (pop, ID) */
141 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
145 enum cpp_ttype token
;
146 enum { set
, push
, pop
} action
;
148 if (c_lex (&x
) != CPP_OPEN_PAREN
)
149 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
152 if (token
== CPP_CLOSE_PAREN
)
155 align
= initial_max_fld_align
;
157 else if (token
== CPP_NUMBER
)
159 align
= TREE_INT_CST_LOW (x
);
161 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
162 GCC_BAD ("malformed %<#pragma pack%> - ignored");
164 else if (token
== CPP_NAME
)
166 #define GCC_BAD_ACTION do { if (action != pop) \
167 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
169 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
172 const char *op
= IDENTIFIER_POINTER (x
);
173 if (!strcmp (op
, "push"))
175 else if (!strcmp (op
, "pop"))
178 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op
);
180 while ((token
= c_lex (&x
)) == CPP_COMMA
)
183 if (token
== CPP_NAME
&& id
== 0)
187 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
189 align
= TREE_INT_CST_LOW (x
);
197 if (token
!= CPP_CLOSE_PAREN
)
199 #undef GCC_BAD_ACTION
202 GCC_BAD ("malformed %<#pragma pack%> - ignored");
204 if (c_lex (&x
) != CPP_EOF
)
205 warning (0, "junk at end of %<#pragma pack%>");
207 if (flag_pack_struct
)
208 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
219 align
*= BITS_PER_UNIT
;
224 align
= maximum_field_alignment
;
228 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
233 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
234 case push
: push_alignment (align
, id
); break;
235 case pop
: pop_alignment (id
); break;
238 #endif /* HANDLE_PRAGMA_PACK */
240 static GTY(()) tree pending_weaks
;
242 #ifdef HANDLE_PRAGMA_WEAK
243 static void apply_pragma_weak (tree
, tree
);
244 static void handle_pragma_weak (cpp_reader
*);
247 apply_pragma_weak (tree decl
, tree value
)
251 value
= build_string (IDENTIFIER_LENGTH (value
),
252 IDENTIFIER_POINTER (value
));
253 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
254 build_tree_list (NULL
, value
)),
258 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
259 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
260 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
261 warning (0, "%Japplying #pragma weak %qD after first use results "
262 "in unspecified behavior", decl
, decl
);
268 maybe_apply_pragma_weak (tree decl
)
272 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
274 /* No weak symbols pending, take the short-cut. */
277 /* If it's not visible outside this file, it doesn't matter whether
279 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
281 /* If it's not a function or a variable, it can't be weak.
282 FIXME: what kinds of things are visible outside this file but
283 aren't functions or variables? Should this be an assert instead? */
284 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
287 id
= DECL_ASSEMBLER_NAME (decl
);
289 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
290 if (id
== TREE_PURPOSE (t
))
292 apply_pragma_weak (decl
, TREE_VALUE (t
));
298 /* Process all "#pragma weak A = B" directives where we have not seen
301 maybe_apply_pending_pragma_weaks (void)
303 tree
*p
, t
, alias_id
, id
, decl
, *next
;
305 for (p
= &pending_weaks
; (t
= *p
) ; p
= next
)
307 next
= &TREE_CHAIN (t
);
308 alias_id
= TREE_PURPOSE (t
);
311 if (TREE_VALUE (t
) == NULL
)
314 decl
= build_decl (FUNCTION_DECL
, alias_id
, default_function_type
);
316 DECL_ARTIFICIAL (decl
) = 1;
317 TREE_PUBLIC (decl
) = 1;
318 DECL_EXTERNAL (decl
) = 1;
319 DECL_WEAK (decl
) = 1;
321 assemble_alias (decl
, id
);
325 /* #pragma weak name [= value] */
327 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
329 tree name
, value
, x
, decl
;
334 if (c_lex (&name
) != CPP_NAME
)
335 GCC_BAD ("malformed #pragma weak, ignored");
339 if (c_lex (&value
) != CPP_NAME
)
340 GCC_BAD ("malformed #pragma weak, ignored");
344 warning (0, "junk at end of #pragma weak");
346 decl
= identifier_global_value (name
);
347 if (decl
&& DECL_P (decl
))
349 apply_pragma_weak (decl
, value
);
351 assemble_alias (decl
, value
);
354 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
358 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
363 maybe_apply_pending_pragma_weaks (void)
366 #endif /* HANDLE_PRAGMA_WEAK */
368 /* GCC supports two #pragma directives for renaming the external
369 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
370 compatibility with the Solaris and Tru64 system headers. GCC also
371 has its own notation for this, __asm__("name") annotations.
373 Corner cases of these features and their interaction:
375 1) Both pragmas silently apply only to declarations with external
376 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
377 do not have this restriction.
379 2) In C++, both #pragmas silently apply only to extern "C" declarations.
380 Asm labels do not have this restriction.
382 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
383 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
384 new name is different, a warning issues and the name does not change.
386 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
387 *not* the DECL_ASSEMBLER_NAME.
389 5) If #pragma extern_prefix is in effect and a declaration occurs
390 with an __asm__ name, the #pragma extern_prefix is silently
391 ignored for that declaration.
393 6) If #pragma extern_prefix and #pragma redefine_extname apply to
394 the same declaration, whichever triggered first wins, and a warning
395 is issued. (We would like to have #pragma redefine_extname always
396 win, but it can appear either before or after the declaration, and
397 if it appears afterward, we have no way of knowing whether a modified
398 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
400 static GTY(()) tree pending_redefine_extname
;
402 static void handle_pragma_redefine_extname (cpp_reader
*);
404 /* #pragma redefine_extname oldname newname */
406 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
408 tree oldname
, newname
, decl
, x
;
411 if (c_lex (&oldname
) != CPP_NAME
)
412 GCC_BAD ("malformed #pragma redefine_extname, ignored");
413 if (c_lex (&newname
) != CPP_NAME
)
414 GCC_BAD ("malformed #pragma redefine_extname, ignored");
417 warning (0, "junk at end of #pragma redefine_extname");
419 if (!flag_mudflap
&& !targetm
.handle_pragma_redefine_extname
)
421 if (warn_unknown_pragmas
> in_system_header
)
422 warning (OPT_Wunknown_pragmas
,
423 "#pragma redefine_extname not supported on this target");
427 decl
= identifier_global_value (oldname
);
429 && (TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
430 && (TREE_CODE (decl
) == FUNCTION_DECL
431 || TREE_CODE (decl
) == VAR_DECL
)
432 && has_c_linkage (decl
))
434 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
436 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
437 name
= targetm
.strip_name_encoding (name
);
439 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
440 warning (0, "#pragma redefine_extname ignored due to conflict with "
444 change_decl_assembler_name (decl
, newname
);
447 /* We have to add this to the rename list even if there's already
448 a global value that doesn't meet the above criteria, because in
449 C++ "struct foo {...};" puts "foo" in the current namespace but
450 does *not* conflict with a subsequent declaration of a function
451 or variable foo. See g++.dg/other/pragma-re-2.C. */
452 add_to_renaming_pragma_list (oldname
, newname
);
455 /* This is called from here and from ia64.c. */
457 add_to_renaming_pragma_list (tree oldname
, tree newname
)
459 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
462 if (TREE_VALUE (previous
) != newname
)
463 warning (0, "#pragma redefine_extname ignored due to conflict with "
464 "previous #pragma redefine_extname");
468 pending_redefine_extname
469 = tree_cons (oldname
, newname
, pending_redefine_extname
);
472 static GTY(()) tree pragma_extern_prefix
;
474 /* #pragma extern_prefix "prefix" */
476 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
481 if (c_lex (&prefix
) != CPP_STRING
)
482 GCC_BAD ("malformed #pragma extern_prefix, ignored");
485 warning (0, "junk at end of #pragma extern_prefix");
487 if (targetm
.handle_pragma_extern_prefix
)
488 /* Note that the length includes the null terminator. */
489 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
490 else if (warn_unknown_pragmas
> in_system_header
)
491 warning (OPT_Wunknown_pragmas
,
492 "#pragma extern_prefix not supported on this target");
495 /* Hook from the front ends to apply the results of one of the preceding
496 pragmas that rename variables. */
499 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
503 /* The renaming pragmas are only applied to declarations with
505 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
506 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
507 || !has_c_linkage (decl
))
510 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
511 but we may warn about a rename that conflicts. */
512 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
514 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
515 oldname
= targetm
.strip_name_encoding (oldname
);
517 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
518 warning (0, "asm declaration ignored due to "
519 "conflict with previous rename");
521 /* Take any pending redefine_extname off the list. */
522 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
523 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
525 /* Only warn if there is a conflict. */
526 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t
)), oldname
))
527 warning (0, "#pragma redefine_extname ignored due to "
528 "conflict with previous rename");
536 /* Find out if we have a pending #pragma redefine_extname. */
537 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
538 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
540 tree newname
= TREE_VALUE (t
);
543 /* If we already have an asmname, #pragma redefine_extname is
544 ignored (with a warning if it conflicts). */
547 if (strcmp (TREE_STRING_POINTER (asmname
),
548 IDENTIFIER_POINTER (newname
)) != 0)
549 warning (0, "#pragma redefine_extname ignored due to "
550 "conflict with __asm__ declaration");
554 /* Otherwise we use what we've got; #pragma extern_prefix is
556 return build_string (IDENTIFIER_LENGTH (newname
),
557 IDENTIFIER_POINTER (newname
));
560 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
564 /* If #pragma extern_prefix is in effect, apply it. */
565 if (pragma_extern_prefix
)
567 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
568 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
570 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
571 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
573 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
575 memcpy (newname
, prefix
, plen
);
576 memcpy (newname
+ plen
, id
, ilen
+ 1);
578 return build_string (plen
+ ilen
, newname
);
586 #ifdef HANDLE_PRAGMA_VISIBILITY
587 static void handle_pragma_visibility (cpp_reader
*);
589 typedef enum symbol_visibility visibility
;
590 DEF_VEC_I (visibility
);
591 DEF_VEC_ALLOC_I (visibility
, heap
);
593 /* Sets the default visibility for symbols to something other than that
594 specified on the command line. */
596 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
598 /* Form is #pragma GCC visibility push(hidden)|pop */
600 enum cpp_ttype token
;
601 enum { bad
, push
, pop
} action
= bad
;
602 static VEC (visibility
, heap
) *visstack
;
605 if (token
== CPP_NAME
)
607 const char *op
= IDENTIFIER_POINTER (x
);
608 if (!strcmp (op
, "push"))
610 else if (!strcmp (op
, "pop"))
614 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
619 if (!VEC_length (visibility
, visstack
))
621 GCC_BAD ("No matching push for %<#pragma GCC visibility pop%>");
625 default_visibility
= VEC_pop (visibility
, visstack
);
626 visibility_options
.inpragma
627 = VEC_length (visibility
, visstack
) != 0;
632 if (c_lex (&x
) != CPP_OPEN_PAREN
)
633 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
635 if (token
!= CPP_NAME
)
637 GCC_BAD ("malformed #pragma GCC visibility push");
641 const char *str
= IDENTIFIER_POINTER (x
);
642 VEC_safe_push (visibility
, heap
, visstack
,
644 if (!strcmp (str
, "default"))
645 default_visibility
= VISIBILITY_DEFAULT
;
646 else if (!strcmp (str
, "internal"))
647 default_visibility
= VISIBILITY_INTERNAL
;
648 else if (!strcmp (str
, "hidden"))
649 default_visibility
= VISIBILITY_HIDDEN
;
650 else if (!strcmp (str
, "protected"))
651 default_visibility
= VISIBILITY_PROTECTED
;
654 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
656 visibility_options
.inpragma
= 1;
658 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
659 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
662 if (c_lex (&x
) != CPP_EOF
)
663 warning (0, "junk at end of %<#pragma GCC visibility%>");
668 /* Front-end wrappers for pragma registration to avoid dragging
669 cpplib.h in almost everywhere. */
671 c_register_pragma (const char *space
, const char *name
,
672 void (*handler
) (struct cpp_reader
*))
674 cpp_register_pragma (parse_in
, space
, name
, handler
, 0);
678 c_register_pragma_with_expansion (const char *space
, const char *name
,
679 void (*handler
) (struct cpp_reader
*))
681 cpp_register_pragma (parse_in
, space
, name
, handler
, 1);
684 /* Set up front-end pragmas. */
688 #ifdef HANDLE_PRAGMA_PACK
689 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
690 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
692 c_register_pragma (0, "pack", handle_pragma_pack
);
695 #ifdef HANDLE_PRAGMA_WEAK
696 c_register_pragma (0, "weak", handle_pragma_weak
);
698 #ifdef HANDLE_PRAGMA_VISIBILITY
699 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
702 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname
);
703 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
705 c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma
);
707 #ifdef REGISTER_TARGET_PRAGMAS
708 REGISTER_TARGET_PRAGMAS ();
712 #include "gt-c-pragma.h"