1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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"
38 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
39 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
41 typedef struct align_stack
GTY(())
44 unsigned int num_pushes
;
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) \
62 (default_alignment = maximum_field_alignment = (ALIGN))
64 static void push_alignment (int, tree
);
65 static void pop_alignment (tree
);
67 /* Push an alignment value onto the stack. */
69 push_alignment (int alignment
, tree 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. */
100 pop_alignment (tree id
)
104 if (alignment_stack
== NULL
)
107 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
112 /* If we got an identifier, strip away everything above the target
113 entry so that the next step will restore the state just below it. */
116 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
119 entry
->num_pushes
= 1;
120 alignment_stack
= entry
;
125 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
126 , IDENTIFIER_POINTER (id
), IDENTIFIER_POINTER (id
));
129 if (-- alignment_stack
->num_pushes
== 0)
131 entry
= alignment_stack
->prev
;
134 maximum_field_alignment
= default_alignment
;
136 maximum_field_alignment
= entry
->alignment
;
138 alignment_stack
= entry
;
141 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
142 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
143 #define push_alignment(ID, N) \
144 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
145 #define pop_alignment(ID) \
146 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
147 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
152 #pragma pack (push, N)
153 #pragma pack (push, ID, N)
155 #pragma pack (pop, ID) */
157 handle_pragma_pack (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
161 enum cpp_ttype token
;
162 enum { set
, push
, pop
} action
;
164 if (c_lex (&x
) != CPP_OPEN_PAREN
)
165 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
168 if (token
== CPP_CLOSE_PAREN
)
173 else if (token
== CPP_NUMBER
)
175 align
= TREE_INT_CST_LOW (x
);
177 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
178 GCC_BAD ("malformed '#pragma pack' - ignored");
180 else if (token
== CPP_NAME
)
182 #define GCC_BAD_ACTION do { if (action == push) \
183 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
185 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
188 const char *op
= IDENTIFIER_POINTER (x
);
189 if (!strcmp (op
, "push"))
191 else if (!strcmp (op
, "pop"))
194 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op
);
197 if (token
!= CPP_COMMA
&& action
== push
)
200 if (token
== CPP_COMMA
)
203 if (token
== CPP_NAME
)
206 if (action
== push
&& c_lex (&x
) != CPP_COMMA
)
213 if (token
== CPP_NUMBER
)
215 align
= TREE_INT_CST_LOW (x
);
223 if (token
!= CPP_CLOSE_PAREN
)
225 #undef GCC_BAD_ACTION
228 GCC_BAD ("malformed '#pragma pack' - ignored");
230 if (c_lex (&x
) != CPP_EOF
)
231 warning ("junk at end of '#pragma pack'");
242 align
*= BITS_PER_UNIT
;
245 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
250 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
251 case push
: push_alignment (align
, id
); break;
252 case pop
: pop_alignment (id
); break;
255 #endif /* HANDLE_PRAGMA_PACK */
257 static GTY(()) tree pending_weaks
;
259 #ifdef HANDLE_PRAGMA_WEAK
260 static void apply_pragma_weak (tree
, tree
);
261 static void handle_pragma_weak (cpp_reader
*);
264 apply_pragma_weak (tree decl
, tree value
)
268 value
= build_string (IDENTIFIER_LENGTH (value
),
269 IDENTIFIER_POINTER (value
));
270 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
271 build_tree_list (NULL
, value
)),
275 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
276 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
277 warning_with_decl (decl
, "applying #pragma weak `%s' after first use results in unspecified behavior");
283 maybe_apply_pragma_weak (tree decl
)
287 /* Copied from the check in set_decl_assembler_name. */
288 if (TREE_CODE (decl
) == FUNCTION_DECL
289 || (TREE_CODE (decl
) == VAR_DECL
290 && (TREE_STATIC (decl
)
291 || DECL_EXTERNAL (decl
)
292 || TREE_PUBLIC (decl
))))
293 id
= DECL_ASSEMBLER_NAME (decl
);
297 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
298 if (id
== TREE_PURPOSE (t
))
300 apply_pragma_weak (decl
, TREE_VALUE (t
));
306 /* #pragma weak name [= value] */
308 handle_pragma_weak (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
310 tree name
, value
, x
, decl
;
315 if (c_lex (&name
) != CPP_NAME
)
316 GCC_BAD ("malformed #pragma weak, ignored");
320 if (c_lex (&value
) != CPP_NAME
)
321 GCC_BAD ("malformed #pragma weak, ignored");
325 warning ("junk at end of #pragma weak");
327 decl
= identifier_global_value (name
);
328 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
330 apply_pragma_weak (decl
, value
);
332 assemble_alias (decl
, value
);
335 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
339 maybe_apply_pragma_weak (tree decl ATTRIBUTE_UNUSED
)
342 #endif /* HANDLE_PRAGMA_WEAK */
344 static GTY(()) tree pending_redefine_extname
;
346 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
347 static void handle_pragma_redefine_extname (cpp_reader
*);
349 /* #pragma redefined_extname oldname newname */
351 handle_pragma_redefine_extname (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
353 tree oldname
, newname
, decl
, x
;
356 if (c_lex (&oldname
) != CPP_NAME
)
358 warning ("malformed #pragma redefine_extname, ignored");
361 if (c_lex (&newname
) != CPP_NAME
)
363 warning ("malformed #pragma redefine_extname, ignored");
368 warning ("junk at end of #pragma redefine_extname");
370 decl
= identifier_global_value (oldname
);
371 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
373 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
374 && DECL_ASSEMBLER_NAME (decl
) != newname
)
375 warning ("#pragma redefine_extname conflicts with declaration");
376 SET_DECL_ASSEMBLER_NAME (decl
, newname
);
379 add_to_renaming_pragma_list(oldname
, newname
);
384 add_to_renaming_pragma_list (tree oldname
, tree newname
)
386 pending_redefine_extname
387 = tree_cons (oldname
, newname
, pending_redefine_extname
);
390 static GTY(()) tree pragma_extern_prefix
;
392 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
393 static void handle_pragma_extern_prefix (cpp_reader
*);
395 /* #pragma extern_prefix "prefix" */
397 handle_pragma_extern_prefix (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
402 if (c_lex (&prefix
) != CPP_STRING
)
404 warning ("malformed #pragma extern_prefix, ignored");
409 warning ("junk at end of #pragma extern_prefix");
411 /* Note that the length includes the null terminator. */
412 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
416 /* Hook from the front ends to apply the results of one of the preceding
417 pragmas that rename variables. */
420 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
424 /* Copied from the check in set_decl_assembler_name. */
425 if (TREE_CODE (decl
) == FUNCTION_DECL
426 || (TREE_CODE (decl
) == VAR_DECL
427 && (TREE_STATIC (decl
)
428 || DECL_EXTERNAL (decl
)
429 || TREE_PUBLIC (decl
))))
430 oldname
= DECL_ASSEMBLER_NAME (decl
);
434 /* If the name begins with a *, that's a sign of an asmname attached to
435 a previous declaration. */
436 if (IDENTIFIER_POINTER (oldname
)[0] == '*')
438 const char *oldasmname
= IDENTIFIER_POINTER (oldname
) + 1;
439 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldasmname
) != 0)
440 warning ("asm declaration conflicts with previous rename");
441 asmname
= build_string (strlen (oldasmname
), oldasmname
);
447 for (p
= &pending_redefine_extname
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
448 if (oldname
== TREE_PURPOSE (t
))
450 const char *newname
= IDENTIFIER_POINTER (TREE_VALUE (t
));
452 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), newname
) != 0)
453 warning ("#pragma redefine_extname conflicts with declaration");
456 return build_string (strlen (newname
), newname
);
460 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
461 if (pragma_extern_prefix
&& !asmname
)
463 char *x
= concat (TREE_STRING_POINTER (pragma_extern_prefix
),
464 IDENTIFIER_POINTER (oldname
), NULL
);
465 asmname
= build_string (strlen (x
), x
);
474 /* Front-end wrapper for pragma registration to avoid dragging
475 cpplib.h in almost everywhere. */
477 c_register_pragma (const char *space
, const char *name
,
478 void (*handler
) (struct cpp_reader
*))
480 cpp_register_pragma (parse_in
, space
, name
, handler
);
483 /* Set up front-end pragmas. */
487 #ifdef HANDLE_PRAGMA_PACK
488 c_register_pragma (0, "pack", handle_pragma_pack
);
490 #ifdef HANDLE_PRAGMA_WEAK
491 c_register_pragma (0, "weak", handle_pragma_weak
);
493 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
494 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname
);
496 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
497 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
500 #ifdef REGISTER_TARGET_PRAGMAS
501 REGISTER_TARGET_PRAGMAS ();
505 #include "gt-c-pragma.h"