1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002
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
36 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
37 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
39 typedef struct align_stack
GTY(())
42 unsigned int num_pushes
;
44 struct align_stack
* prev
;
47 static GTY(()) struct align_stack
* alignment_stack
;
49 #ifdef HANDLE_PRAGMA_PACK
50 static void handle_pragma_pack
PARAMS ((cpp_reader
*));
52 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
53 /* If we have a "global" #pragma pack(<n>) in effect when the first
54 #pragma pack(push,<n>) is encountered, this stores the value of
55 maximum_field_alignment in effect. When the final pop_alignment()
56 happens, we restore the value to this, not to a value of 0 for
57 maximum_field_alignment. Value is in bits. */
58 static int default_alignment
;
59 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
60 (default_alignment = maximum_field_alignment = (ALIGN))
62 static void push_alignment
PARAMS ((int, tree
));
63 static void pop_alignment
PARAMS ((tree
));
65 /* Push an alignment value onto the stack. */
67 push_alignment (alignment
, id
)
71 if (alignment_stack
== NULL
72 || alignment_stack
->alignment
!= alignment
77 entry
= (align_stack
*) ggc_alloc (sizeof (* entry
));
79 entry
->alignment
= alignment
;
80 entry
->num_pushes
= 1;
82 entry
->prev
= alignment_stack
;
84 /* The current value of maximum_field_alignment is not necessarily
85 0 since there may be a #pragma pack(<n>) in effect; remember it
86 so that we can restore it after the final #pragma pop(). */
87 if (alignment_stack
== NULL
)
88 default_alignment
= maximum_field_alignment
;
90 alignment_stack
= entry
;
92 maximum_field_alignment
= alignment
;
95 alignment_stack
->num_pushes
++;
98 /* Undo a push of an alignment onto the stack. */
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 (dummy
)
159 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
163 enum cpp_ttype token
;
164 enum { set
, push
, pop
} action
;
166 if (c_lex (&x
) != CPP_OPEN_PAREN
)
167 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
170 if (token
== CPP_CLOSE_PAREN
)
175 else if (token
== CPP_NUMBER
)
177 align
= TREE_INT_CST_LOW (x
);
179 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
180 GCC_BAD ("malformed '#pragma pack' - ignored");
182 else if (token
== CPP_NAME
)
184 #define GCC_BAD_ACTION do { if (action == push) \
185 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
187 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
190 const char *op
= IDENTIFIER_POINTER (x
);
191 if (!strcmp (op
, "push"))
193 else if (!strcmp (op
, "pop"))
196 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op
);
199 if (token
!= CPP_COMMA
&& action
== push
)
202 if (token
== CPP_COMMA
)
205 if (token
== CPP_NAME
)
208 if (action
== push
&& c_lex (&x
) != CPP_COMMA
)
215 if (token
== CPP_NUMBER
)
217 align
= TREE_INT_CST_LOW (x
);
225 if (token
!= CPP_CLOSE_PAREN
)
227 #undef GCC_BAD_ACTION
230 GCC_BAD ("malformed '#pragma pack' - ignored");
232 if (c_lex (&x
) != CPP_EOF
)
233 warning ("junk at end of '#pragma pack'");
244 align
*= BITS_PER_UNIT
;
247 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
252 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
253 case push
: push_alignment (align
, id
); break;
254 case pop
: pop_alignment (id
); break;
257 #endif /* HANDLE_PRAGMA_PACK */
259 static GTY(()) tree pending_weaks
;
261 #ifdef HANDLE_PRAGMA_WEAK
262 static void apply_pragma_weak
PARAMS ((tree
, tree
));
263 static void handle_pragma_weak
PARAMS ((cpp_reader
*));
266 apply_pragma_weak (decl
, value
)
271 value
= build_string (IDENTIFIER_LENGTH (value
),
272 IDENTIFIER_POINTER (value
));
273 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
274 build_tree_list (NULL
, value
)),
278 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
279 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
280 warning_with_decl (decl
, "applying #pragma weak `%s' after first use results in unspecified behavior");
286 maybe_apply_pragma_weak (decl
)
291 /* Copied from the check in set_decl_assembler_name. */
292 if (TREE_CODE (decl
) == FUNCTION_DECL
293 || (TREE_CODE (decl
) == VAR_DECL
294 && (TREE_STATIC (decl
)
295 || DECL_EXTERNAL (decl
)
296 || TREE_PUBLIC (decl
))))
297 id
= DECL_ASSEMBLER_NAME (decl
);
301 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
302 if (id
== TREE_PURPOSE (t
))
304 apply_pragma_weak (decl
, TREE_VALUE (t
));
310 /* #pragma weak name [= value] */
312 handle_pragma_weak (dummy
)
313 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
315 tree name
, value
, x
, decl
;
320 if (c_lex (&name
) != CPP_NAME
)
321 GCC_BAD ("malformed #pragma weak, ignored");
325 if (c_lex (&value
) != CPP_NAME
)
326 GCC_BAD ("malformed #pragma weak, ignored");
330 warning ("junk at end of #pragma weak");
332 decl
= identifier_global_value (name
);
333 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
335 apply_pragma_weak (decl
, value
);
337 assemble_alias (decl
, value
);
340 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
344 maybe_apply_pragma_weak (decl
)
345 tree decl ATTRIBUTE_UNUSED
;
348 #endif /* HANDLE_PRAGMA_WEAK */
350 static GTY(()) tree pending_redefine_extname
;
352 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
353 static void handle_pragma_redefine_extname
PARAMS ((cpp_reader
*));
355 /* #pragma redefined_extname oldname newname */
357 handle_pragma_redefine_extname (dummy
)
358 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
360 tree oldname
, newname
, decl
, x
;
363 if (c_lex (&oldname
) != CPP_NAME
)
365 warning ("malformed #pragma redefine_extname, ignored");
368 if (c_lex (&newname
) != CPP_NAME
)
370 warning ("malformed #pragma redefine_extname, ignored");
375 warning ("junk at end of #pragma redefine_extname");
377 decl
= identifier_global_value (oldname
);
378 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
380 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
381 && DECL_ASSEMBLER_NAME (decl
) != newname
)
382 warning ("#pragma redefine_extname conflicts with declaration");
383 SET_DECL_ASSEMBLER_NAME (decl
, newname
);
386 add_to_renaming_pragma_list(oldname
, newname
);
391 add_to_renaming_pragma_list (oldname
, newname
)
392 tree oldname
, newname
;
394 pending_redefine_extname
395 = tree_cons (oldname
, newname
, pending_redefine_extname
);
398 static GTY(()) tree pragma_extern_prefix
;
400 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
401 static void handle_pragma_extern_prefix
PARAMS ((cpp_reader
*));
403 /* #pragma extern_prefix "prefix" */
405 handle_pragma_extern_prefix (dummy
)
406 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
411 if (c_lex (&prefix
) != CPP_STRING
)
413 warning ("malformed #pragma extern_prefix, ignored");
418 warning ("junk at end of #pragma extern_prefix");
420 /* Note that the length includes the null terminator. */
421 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
425 /* Hook from the front ends to apply the results of one of the preceeding
426 pragmas that rename variables. */
429 maybe_apply_renaming_pragma (decl
, asmname
)
434 /* Copied from the check in set_decl_assembler_name. */
435 if (TREE_CODE (decl
) == FUNCTION_DECL
436 || (TREE_CODE (decl
) == VAR_DECL
437 && (TREE_STATIC (decl
)
438 || DECL_EXTERNAL (decl
)
439 || TREE_PUBLIC (decl
))))
440 oldname
= DECL_ASSEMBLER_NAME (decl
);
444 /* If the name begins with a *, that's a sign of an asmname attached to
445 a previous declaration. */
446 if (IDENTIFIER_POINTER (oldname
)[0] == '*')
448 const char *oldasmname
= IDENTIFIER_POINTER (oldname
) + 1;
449 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldasmname
) != 0)
450 warning ("asm declaration conficts with previous rename");
451 asmname
= build_string (strlen (oldasmname
), oldasmname
);
457 for (p
= &pending_redefine_extname
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
458 if (oldname
== TREE_PURPOSE (t
))
460 const char *newname
= IDENTIFIER_POINTER (TREE_VALUE (t
));
462 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), newname
) != 0)
463 warning ("#pragma redefine_extname conflicts with declaration");
466 return build_string (strlen (newname
), newname
);
470 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
471 if (pragma_extern_prefix
&& !asmname
)
473 char *x
= concat (TREE_STRING_POINTER (pragma_extern_prefix
),
474 IDENTIFIER_POINTER (oldname
), NULL
);
475 asmname
= build_string (strlen (x
), x
);
487 #ifdef HANDLE_PRAGMA_PACK
488 cpp_register_pragma (parse_in
, 0, "pack", handle_pragma_pack
);
490 #ifdef HANDLE_PRAGMA_WEAK
491 cpp_register_pragma (parse_in
, 0, "weak", handle_pragma_weak
);
493 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
494 cpp_register_pragma (parse_in
, 0, "redefine_extname",
495 handle_pragma_redefine_extname
);
497 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
498 cpp_register_pragma (parse_in
, 0, "extern_prefix",
499 handle_pragma_extern_prefix
);
502 #ifdef REGISTER_TARGET_PRAGMAS
503 REGISTER_TARGET_PRAGMAS (parse_in
);
507 #include "gt-c-pragma.h"