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(())
45 unsigned int num_pushes
;
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) \
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. */
70 push_alignment (int alignment
, tree id
)
72 if (alignment_stack
== NULL
73 || alignment_stack
->alignment
!= alignment
78 entry
= ggc_alloc (sizeof (* entry
));
80 entry
->alignment
= alignment
;
81 entry
->num_pushes
= 1;
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
;
96 alignment_stack
->num_pushes
++;
99 /* Undo a push of an alignment onto the stack. */
101 pop_alignment (tree id
)
105 if (alignment_stack
== NULL
)
108 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
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. */
117 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
120 entry
->num_pushes
= 1;
121 alignment_stack
= entry
;
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
;
135 maximum_field_alignment
= default_alignment
;
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 */
153 #pragma pack (push, N)
154 #pragma pack (push, ID, N)
156 #pragma pack (pop, ID) */
158 handle_pragma_pack (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
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");
169 if (token
== CPP_CLOSE_PAREN
)
174 else if (token
== CPP_NUMBER
)
176 align
= TREE_INT_CST_LOW (x
);
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"); \
186 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
189 const char *op
= IDENTIFIER_POINTER (x
);
190 if (!strcmp (op
, "push"))
192 else if (!strcmp (op
, "pop"))
195 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op
);
198 if (token
!= CPP_COMMA
&& action
== push
)
201 if (token
== CPP_COMMA
)
204 if (token
== CPP_NAME
)
207 if (action
== push
&& c_lex (&x
) != CPP_COMMA
)
214 if (token
== CPP_NUMBER
)
216 align
= TREE_INT_CST_LOW (x
);
224 if (token
!= CPP_CLOSE_PAREN
)
226 #undef GCC_BAD_ACTION
229 GCC_BAD ("malformed '#pragma pack' - ignored");
231 if (c_lex (&x
) != CPP_EOF
)
232 warning ("junk at end of '#pragma pack'");
243 align
*= BITS_PER_UNIT
;
246 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
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
*);
265 apply_pragma_weak (tree decl
, tree 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
);
286 maybe_apply_pragma_weak (tree decl
)
290 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
292 /* No weak symbols pending, take the short-cut. */
295 /* If it's not visible outside this file, it doesn't matter whether
297 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
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
)
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
));
316 /* #pragma weak name [= value] */
318 handle_pragma_weak (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
320 tree name
, value
, x
, decl
;
325 if (c_lex (&name
) != CPP_NAME
)
326 GCC_BAD ("malformed #pragma weak, ignored");
330 if (c_lex (&value
) != CPP_NAME
)
331 GCC_BAD ("malformed #pragma weak, ignored");
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
);
342 assemble_alias (decl
, value
);
345 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
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 */
392 handle_pragma_redefine_extname (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
394 tree oldname
, newname
, decl
, x
;
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");
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");
412 decl
= identifier_global_value (oldname
);
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 "
429 change_decl_assembler_name (decl
, newname
);
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. */
442 add_to_renaming_pragma_list (tree oldname
, tree newname
)
444 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
447 if (TREE_VALUE (previous
) != newname
)
448 warning ("#pragma redefine_extname ignored due to conflict with "
449 "previous #pragma redefine_extname");
453 pending_redefine_extname
454 = tree_cons (oldname
, newname
, pending_redefine_extname
);
457 static GTY(()) tree pragma_extern_prefix
;
459 /* #pragma extern_prefix "prefix" */
461 handle_pragma_extern_prefix (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
466 if (c_lex (&prefix
) != CPP_STRING
)
467 GCC_BAD ("malformed #pragma extern_prefix, ignored");
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. */
483 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
487 /* The renaming pragmas are only applied to declarations with
489 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
490 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
491 || !has_c_linkage (decl
))
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");
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
);
527 /* If we already have an asmname, #pragma redefine_extname is
528 ignored (with a warning if it conflicts). */
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");
538 /* Otherwise we use what we've got; #pragma extern_prefix is
540 return build_string (IDENTIFIER_LENGTH (newname
),
541 IDENTIFIER_POINTER (newname
));
544 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
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
);
569 /* Front-end wrapper for pragma registration to avoid dragging
570 cpplib.h in almost everywhere. */
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. */
582 #ifdef HANDLE_PRAGMA_PACK
583 c_register_pragma (0, "pack", handle_pragma_pack
);
585 #ifdef HANDLE_PRAGMA_WEAK
586 c_register_pragma (0, "weak", handle_pragma_weak
);
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 ();
599 #include "gt-c-pragma.h"