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 #ifdef HANDLE_PRAGMA_PACK
40 static void handle_pragma_pack
PARAMS ((cpp_reader
*));
42 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
43 typedef struct align_stack
46 unsigned int num_pushes
;
48 struct align_stack
* prev
;
51 static struct align_stack
* alignment_stack
= NULL
;
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
));
64 static void mark_align_stack
PARAMS ((void *));
66 /* Push an alignment value onto the stack. */
68 push_alignment (alignment
, id
)
72 if (alignment_stack
== NULL
73 || alignment_stack
->alignment
!= alignment
78 entry
= (align_stack
*) xmalloc (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. */
106 if (alignment_stack
== NULL
)
109 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
114 /* If we got an identifier, strip away everything above the target
115 entry so that the next step will restore the state just below it. */
118 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
121 entry
->num_pushes
= 1;
122 alignment_stack
= entry
;
127 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
128 , IDENTIFIER_POINTER (id
), IDENTIFIER_POINTER (id
));
131 if (-- alignment_stack
->num_pushes
== 0)
133 entry
= alignment_stack
->prev
;
136 maximum_field_alignment
= default_alignment
;
138 maximum_field_alignment
= entry
->alignment
;
140 free (alignment_stack
);
142 alignment_stack
= entry
;
150 align_stack
*a
= *(align_stack
**) p
;
154 ggc_mark_tree (a
->id
);
158 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
159 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
160 #define push_alignment(ID, N) \
161 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
162 #define pop_alignment(ID) \
163 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
164 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
169 #pragma pack (push, N)
170 #pragma pack (push, ID, N)
172 #pragma pack (pop, ID) */
174 handle_pragma_pack (dummy
)
175 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
179 enum cpp_ttype token
;
180 enum { set
, push
, pop
} action
;
182 if (c_lex (&x
) != CPP_OPEN_PAREN
)
183 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
186 if (token
== CPP_CLOSE_PAREN
)
191 else if (token
== CPP_NUMBER
)
193 align
= TREE_INT_CST_LOW (x
);
195 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
196 GCC_BAD ("malformed '#pragma pack' - ignored");
198 else if (token
== CPP_NAME
)
200 #define GCC_BAD_ACTION do { if (action == push) \
201 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
203 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
206 const char *op
= IDENTIFIER_POINTER (x
);
207 if (!strcmp (op
, "push"))
209 else if (!strcmp (op
, "pop"))
212 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op
);
215 if (token
!= CPP_COMMA
&& action
== push
)
218 if (token
== CPP_COMMA
)
221 if (token
== CPP_NAME
)
224 if (action
== push
&& c_lex (&x
) != CPP_COMMA
)
231 if (token
== CPP_NUMBER
)
233 align
= TREE_INT_CST_LOW (x
);
241 if (token
!= CPP_CLOSE_PAREN
)
243 #undef GCC_BAD_ACTION
246 GCC_BAD ("malformed '#pragma pack' - ignored");
248 if (c_lex (&x
) != CPP_EOF
)
249 warning ("junk at end of '#pragma pack'");
260 align
*= BITS_PER_UNIT
;
263 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
268 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
269 case push
: push_alignment (align
, id
); break;
270 case pop
: pop_alignment (id
); break;
273 #endif /* HANDLE_PRAGMA_PACK */
275 #ifdef HANDLE_PRAGMA_WEAK
276 static void apply_pragma_weak
PARAMS ((tree
, tree
));
277 static void handle_pragma_weak
PARAMS ((cpp_reader
*));
279 static tree pending_weaks
;
282 apply_pragma_weak (decl
, value
)
287 value
= build_string (IDENTIFIER_LENGTH (value
),
288 IDENTIFIER_POINTER (value
));
289 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
290 build_tree_list (NULL
, value
)),
294 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
295 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
296 warning_with_decl (decl
, "applying #pragma weak `%s' after first use results in unspecified behavior");
302 maybe_apply_pragma_weak (decl
)
307 /* Copied from the check in set_decl_assembler_name. */
308 if (TREE_CODE (decl
) == FUNCTION_DECL
309 || (TREE_CODE (decl
) == VAR_DECL
310 && (TREE_STATIC (decl
)
311 || DECL_EXTERNAL (decl
)
312 || TREE_PUBLIC (decl
))))
313 id
= DECL_ASSEMBLER_NAME (decl
);
317 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
318 if (id
== TREE_PURPOSE (t
))
320 apply_pragma_weak (decl
, TREE_VALUE (t
));
326 /* #pragma weak name [= value] */
328 handle_pragma_weak (dummy
)
329 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
331 tree name
, value
, x
, decl
;
336 if (c_lex (&name
) != CPP_NAME
)
337 GCC_BAD ("malformed #pragma weak, ignored");
341 if (c_lex (&value
) != CPP_NAME
)
342 GCC_BAD ("malformed #pragma weak, ignored");
346 warning ("junk at end of #pragma weak");
348 decl
= identifier_global_value (name
);
349 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
351 apply_pragma_weak (decl
, value
);
353 assemble_alias (decl
, value
);
356 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
360 maybe_apply_pragma_weak (decl
)
361 tree decl ATTRIBUTE_UNUSED
;
364 #endif /* HANDLE_PRAGMA_WEAK */
366 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
367 static void handle_pragma_redefine_extname
PARAMS ((cpp_reader
*));
369 static tree pending_redefine_extname
;
371 /* #pragma redefined_extname oldname newname */
373 handle_pragma_redefine_extname (dummy
)
374 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
376 tree oldname
, newname
, decl
, x
;
379 if (c_lex (&oldname
) != CPP_NAME
)
381 warning ("malformed #pragma redefine_extname, ignored");
384 if (c_lex (&newname
) != CPP_NAME
)
386 warning ("malformed #pragma redefine_extname, ignored");
391 warning ("junk at end of #pragma redefine_extname");
393 decl
= identifier_global_value (oldname
);
394 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
396 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
397 && DECL_ASSEMBLER_NAME (decl
) != newname
)
398 warning ("#pragma redefine_extname conflicts with declaration");
399 SET_DECL_ASSEMBLER_NAME (decl
, newname
);
402 pending_redefine_extname
403 = tree_cons (oldname
, newname
, pending_redefine_extname
);
407 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
408 static void handle_pragma_extern_prefix
PARAMS ((cpp_reader
*));
410 static tree pragma_extern_prefix
;
412 /* #pragma extern_prefix "prefix" */
414 handle_pragma_extern_prefix (dummy
)
415 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
420 if (c_lex (&prefix
) != CPP_STRING
)
422 warning ("malformed #pragma extern_prefix, ignored");
427 warning ("junk at end of #pragma extern_prefix");
429 /* Note that the length includes the null terminator. */
430 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
434 /* Hook from the front ends to apply the results of one of the preceeding
435 pragmas that rename variables. */
438 maybe_apply_renaming_pragma (decl
, asmname
)
443 /* Copied from the check in set_decl_assembler_name. */
444 if (TREE_CODE (decl
) == FUNCTION_DECL
445 || (TREE_CODE (decl
) == VAR_DECL
446 && (TREE_STATIC (decl
)
447 || DECL_EXTERNAL (decl
)
448 || TREE_PUBLIC (decl
))))
449 oldname
= DECL_ASSEMBLER_NAME (decl
);
453 /* If the name begins with a *, that's a sign of an asmname attached to
454 a previous declaration. */
455 if (IDENTIFIER_POINTER (oldname
)[0] == '*')
457 const char *oldasmname
= IDENTIFIER_POINTER (oldname
) + 1;
458 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldasmname
) != 0)
459 warning ("asm declaration conficts with previous rename");
460 asmname
= build_string (strlen (oldasmname
), oldasmname
);
463 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
467 for (p
= &pending_redefine_extname
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
468 if (oldname
== TREE_PURPOSE (t
))
470 const char *newname
= IDENTIFIER_POINTER (TREE_VALUE (t
));
472 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), newname
) != 0)
473 warning ("#pragma redefine_extname conflicts with declaration");
476 return build_string (strlen (newname
), newname
);
481 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
482 if (pragma_extern_prefix
&& !asmname
)
484 char *x
= concat (TREE_STRING_POINTER (pragma_extern_prefix
),
485 IDENTIFIER_POINTER (oldname
), NULL
);
486 asmname
= build_string (strlen (x
), x
);
498 #ifdef HANDLE_PRAGMA_PACK
499 cpp_register_pragma (parse_in
, 0, "pack", handle_pragma_pack
);
501 #ifdef HANDLE_PRAGMA_WEAK
502 cpp_register_pragma (parse_in
, 0, "weak", handle_pragma_weak
);
503 ggc_add_tree_root (&pending_weaks
, 1);
505 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
506 cpp_register_pragma (parse_in
, 0, "redefine_extname",
507 handle_pragma_redefine_extname
);
508 ggc_add_tree_root (&pending_redefine_extname
, 1);
510 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
511 cpp_register_pragma (parse_in
, 0, "extern_prefix",
512 handle_pragma_extern_prefix
);
513 ggc_add_tree_root (&pragma_extern_prefix
, 1);
516 #ifdef REGISTER_TARGET_PRAGMAS
517 REGISTER_TARGET_PRAGMAS (parse_in
);
520 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
521 ggc_add_root (&alignment_stack
, 1, sizeof(alignment_stack
),