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
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"
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(())
46 struct align_stack
* prev
;
49 static GTY(()) struct align_stack
* alignment_stack
;
51 #ifdef HANDLE_PRAGMA_PACK
52 static void handle_pragma_pack (cpp_reader
*);
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) (maximum_field_alignment = *(alignment_stack == NULL \
62 ? &default_alignment \
63 : &alignment_stack->alignment) = (ALIGN))
65 static void push_alignment (int, tree
);
66 static void pop_alignment (tree
);
68 /* Push an alignment value onto the stack. */
70 push_alignment (int alignment
, tree id
)
74 entry
= ggc_alloc (sizeof (* entry
));
76 entry
->alignment
= alignment
;
78 entry
->prev
= alignment_stack
;
80 /* The current value of maximum_field_alignment is not necessarily
81 0 since there may be a #pragma pack(<n>) in effect; remember it
82 so that we can restore it after the final #pragma pop(). */
83 if (alignment_stack
== NULL
)
84 default_alignment
= maximum_field_alignment
;
86 alignment_stack
= entry
;
88 maximum_field_alignment
= alignment
;
91 /* Undo a push of an alignment onto the stack. */
93 pop_alignment (tree id
)
97 if (alignment_stack
== NULL
)
98 GCC_BAD("#pragma pack (pop) encountered without matching #pragma pack (push)");
100 /* If we got an identifier, strip away everything above the target
101 entry so that the next step will restore the state just below it. */
104 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
107 alignment_stack
= entry
;
112 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
113 , IDENTIFIER_POINTER (id
), IDENTIFIER_POINTER (id
));
116 entry
= alignment_stack
->prev
;
118 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
120 alignment_stack
= entry
;
122 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
123 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
124 #define push_alignment(ID, N) \
125 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
126 #define pop_alignment(ID) \
127 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
128 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
134 #pragma pack (push, N)
135 #pragma pack (push, ID)
136 #pragma pack (push, ID, N)
138 #pragma pack (pop, ID) */
140 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
144 enum cpp_ttype token
;
145 enum { set
, push
, pop
} action
;
147 if (c_lex (&x
) != CPP_OPEN_PAREN
)
148 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
151 if (token
== CPP_CLOSE_PAREN
)
154 align
= initial_max_fld_align
;
156 else if (token
== CPP_NUMBER
)
158 align
= TREE_INT_CST_LOW (x
);
160 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
161 GCC_BAD ("malformed '#pragma pack' - ignored");
163 else if (token
== CPP_NAME
)
165 #define GCC_BAD_ACTION do { if (action != pop) \
166 GCC_BAD ("malformed '#pragma pack(push[, id][, <n>])' - ignored"); \
168 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
171 const char *op
= IDENTIFIER_POINTER (x
);
172 if (!strcmp (op
, "push"))
174 else if (!strcmp (op
, "pop"))
177 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op
);
179 while ((token
= c_lex (&x
)) == CPP_COMMA
)
182 if (token
== CPP_NAME
&& id
== 0)
186 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
188 align
= TREE_INT_CST_LOW (x
);
196 if (token
!= CPP_CLOSE_PAREN
)
198 #undef GCC_BAD_ACTION
201 GCC_BAD ("malformed '#pragma pack' - ignored");
203 if (c_lex (&x
) != CPP_EOF
)
204 warning ("junk at end of '#pragma pack'");
206 if (flag_pack_struct
)
207 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
218 align
*= BITS_PER_UNIT
;
223 align
= maximum_field_alignment
;
227 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
232 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
233 case push
: push_alignment (align
, id
); break;
234 case pop
: pop_alignment (id
); break;
237 #endif /* HANDLE_PRAGMA_PACK */
239 static GTY(()) tree pending_weaks
;
241 #ifdef HANDLE_PRAGMA_WEAK
242 static void apply_pragma_weak (tree
, tree
);
243 static void handle_pragma_weak (cpp_reader
*);
246 apply_pragma_weak (tree decl
, tree value
)
250 value
= build_string (IDENTIFIER_LENGTH (value
),
251 IDENTIFIER_POINTER (value
));
252 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
253 build_tree_list (NULL
, value
)),
257 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
258 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
259 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
260 warning ("%Japplying #pragma weak '%D' after first use results "
261 "in unspecified behavior", decl
, decl
);
267 maybe_apply_pragma_weak (tree decl
)
271 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
273 /* No weak symbols pending, take the short-cut. */
276 /* If it's not visible outside this file, it doesn't matter whether
278 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
280 /* If it's not a function or a variable, it can't be weak.
281 FIXME: what kinds of things are visible outside this file but
282 aren't functions or variables? Should this be an assert instead? */
283 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
286 id
= DECL_ASSEMBLER_NAME (decl
);
288 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
289 if (id
== TREE_PURPOSE (t
))
291 apply_pragma_weak (decl
, TREE_VALUE (t
));
297 /* #pragma weak name [= value] */
299 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
301 tree name
, value
, x
, decl
;
306 if (c_lex (&name
) != CPP_NAME
)
307 GCC_BAD ("malformed #pragma weak, ignored");
311 if (c_lex (&value
) != CPP_NAME
)
312 GCC_BAD ("malformed #pragma weak, ignored");
316 warning ("junk at end of #pragma weak");
318 decl
= identifier_global_value (name
);
319 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
321 apply_pragma_weak (decl
, value
);
323 assemble_alias (decl
, value
);
326 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
330 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
333 #endif /* HANDLE_PRAGMA_WEAK */
335 /* GCC supports two #pragma directives for renaming the external
336 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
337 compatibility with the Solaris and Tru64 system headers. GCC also
338 has its own notation for this, __asm__("name") annotations.
340 Corner cases of these features and their interaction:
342 1) Both pragmas silently apply only to declarations with external
343 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
344 do not have this restriction.
346 2) In C++, both #pragmas silently apply only to extern "C" declarations.
347 Asm labels do not have this restriction.
349 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
350 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
351 new name is different, a warning issues and the name does not change.
353 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
354 *not* the DECL_ASSEMBLER_NAME.
356 5) If #pragma extern_prefix is in effect and a declaration occurs
357 with an __asm__ name, the #pragma extern_prefix is silently
358 ignored for that declaration.
360 6) If #pragma extern_prefix and #pragma redefine_extname apply to
361 the same declaration, whichever triggered first wins, and a warning
362 is issued. (We would like to have #pragma redefine_extname always
363 win, but it can appear either before or after the declaration, and
364 if it appears afterward, we have no way of knowing whether a modified
365 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
367 static GTY(()) tree pending_redefine_extname
;
369 static void handle_pragma_redefine_extname (cpp_reader
*);
371 /* #pragma redefine_extname oldname newname */
373 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
375 tree oldname
, newname
, decl
, x
;
378 if (c_lex (&oldname
) != CPP_NAME
)
379 GCC_BAD ("malformed #pragma redefine_extname, ignored");
380 if (c_lex (&newname
) != CPP_NAME
)
381 GCC_BAD ("malformed #pragma redefine_extname, ignored");
384 warning ("junk at end of #pragma redefine_extname");
386 if (!flag_mudflap
&& !targetm
.handle_pragma_redefine_extname
)
388 if (warn_unknown_pragmas
> in_system_header
)
389 warning ("#pragma redefine_extname not supported on this target");
393 decl
= identifier_global_value (oldname
);
395 && (TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
396 && (TREE_CODE (decl
) == FUNCTION_DECL
397 || TREE_CODE (decl
) == VAR_DECL
)
398 && has_c_linkage (decl
))
400 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
402 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
403 name
= targetm
.strip_name_encoding (name
);
405 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
406 warning ("#pragma redefine_extname ignored due to conflict with "
410 change_decl_assembler_name (decl
, newname
);
413 /* We have to add this to the rename list even if there's already
414 a global value that doesn't meet the above criteria, because in
415 C++ "struct foo {...};" puts "foo" in the current namespace but
416 does *not* conflict with a subsequent declaration of a function
417 or variable foo. See g++.dg/other/pragma-re-2.C. */
418 add_to_renaming_pragma_list (oldname
, newname
);
421 /* This is called from here and from ia64.c. */
423 add_to_renaming_pragma_list (tree oldname
, tree newname
)
425 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
428 if (TREE_VALUE (previous
) != newname
)
429 warning ("#pragma redefine_extname ignored due to conflict with "
430 "previous #pragma redefine_extname");
434 pending_redefine_extname
435 = tree_cons (oldname
, newname
, pending_redefine_extname
);
438 static GTY(()) tree pragma_extern_prefix
;
440 /* #pragma extern_prefix "prefix" */
442 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
447 if (c_lex (&prefix
) != CPP_STRING
)
448 GCC_BAD ("malformed #pragma extern_prefix, ignored");
451 warning ("junk at end of #pragma extern_prefix");
453 if (targetm
.handle_pragma_extern_prefix
)
454 /* Note that the length includes the null terminator. */
455 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
456 else if (warn_unknown_pragmas
> in_system_header
)
457 warning ("#pragma extern_prefix not supported on this target");
460 /* Hook from the front ends to apply the results of one of the preceding
461 pragmas that rename variables. */
464 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
468 /* The renaming pragmas are only applied to declarations with
470 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
471 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
472 || !has_c_linkage (decl
))
475 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
476 but we may warn about a rename that conflicts. */
477 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
479 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
480 oldname
= targetm
.strip_name_encoding (oldname
);
482 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
483 warning ("asm declaration ignored due to "
484 "conflict with previous rename");
486 /* Take any pending redefine_extname off the list. */
487 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
488 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
490 /* Only warn if there is a conflict. */
491 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t
)), oldname
))
492 warning ("#pragma redefine_extname ignored due to "
493 "conflict with previous rename");
501 /* Find out if we have a pending #pragma redefine_extname. */
502 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
503 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
505 tree newname
= TREE_VALUE (t
);
508 /* If we already have an asmname, #pragma redefine_extname is
509 ignored (with a warning if it conflicts). */
512 if (strcmp (TREE_STRING_POINTER (asmname
),
513 IDENTIFIER_POINTER (newname
)) != 0)
514 warning ("#pragma redefine_extname ignored due to "
515 "conflict with __asm__ declaration");
519 /* Otherwise we use what we've got; #pragma extern_prefix is
521 return build_string (IDENTIFIER_LENGTH (newname
),
522 IDENTIFIER_POINTER (newname
));
525 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
529 /* If #pragma extern_prefix is in effect, apply it. */
530 if (pragma_extern_prefix
)
532 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
533 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
535 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
536 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
538 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
540 memcpy (newname
, prefix
, plen
);
541 memcpy (newname
+ plen
, id
, ilen
+ 1);
543 return build_string (plen
+ ilen
, newname
);
551 #ifdef HANDLE_PRAGMA_VISIBILITY
552 static void handle_pragma_visibility (cpp_reader
*);
554 /* Sets the default visibility for symbols to something other than that
555 specified on the command line. */
557 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
558 { /* Form is #pragma GCC visibility push(hidden)|pop */
559 static int visstack
[16], visidx
;
561 enum cpp_ttype token
;
562 enum { bad
, push
, pop
} action
= bad
;
565 if (token
== CPP_NAME
)
567 const char *op
= IDENTIFIER_POINTER (x
);
568 if (!strcmp (op
, "push"))
570 else if (!strcmp (op
, "pop"))
574 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
581 GCC_BAD ("No matching push for '#pragma GCC visibility pop'");
585 default_visibility
= visstack
[--visidx
];
586 visibility_options
.inpragma
= (visidx
>0);
591 if (c_lex (&x
) != CPP_OPEN_PAREN
)
592 GCC_BAD ("missing '(' after '#pragma GCC visibility push' - ignored");
594 if (token
!= CPP_NAME
)
596 GCC_BAD ("malformed #pragma GCC visibility push");
598 else if (visidx
>= 16)
600 GCC_BAD ("No more than sixteen #pragma GCC visibility pushes allowed at once");
604 const char *str
= IDENTIFIER_POINTER (x
);
605 visstack
[visidx
++] = default_visibility
;
606 if (!strcmp (str
, "default"))
607 default_visibility
= VISIBILITY_DEFAULT
;
608 else if (!strcmp (str
, "internal"))
609 default_visibility
= VISIBILITY_INTERNAL
;
610 else if (!strcmp (str
, "hidden"))
611 default_visibility
= VISIBILITY_HIDDEN
;
612 else if (!strcmp (str
, "protected"))
613 default_visibility
= VISIBILITY_PROTECTED
;
616 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
618 visibility_options
.inpragma
= 1;
620 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
621 GCC_BAD ("missing '(' after '#pragma GCC visibility push' - ignored");
624 if (c_lex (&x
) != CPP_EOF
)
625 warning ("junk at end of '#pragma GCC visibility'");
630 /* Front-end wrapper for pragma registration to avoid dragging
631 cpplib.h in almost everywhere. */
633 c_register_pragma (const char *space
, const char *name
,
634 void (*handler
) (struct cpp_reader
*))
636 cpp_register_pragma (parse_in
, space
, name
, handler
);
639 /* Set up front-end pragmas. */
643 #ifdef HANDLE_PRAGMA_PACK
644 c_register_pragma (0, "pack", handle_pragma_pack
);
646 #ifdef HANDLE_PRAGMA_WEAK
647 c_register_pragma (0, "weak", handle_pragma_weak
);
649 #ifdef HANDLE_PRAGMA_VISIBILITY
650 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
653 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname
);
654 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
656 c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma
);
658 #ifdef REGISTER_TARGET_PRAGMAS
659 REGISTER_TARGET_PRAGMAS ();
663 #include "gt-c-pragma.h"