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
37 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
38 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
40 #ifdef HANDLE_PRAGMA_PACK
41 static void handle_pragma_pack
PARAMS ((cpp_reader
*));
43 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
44 typedef struct align_stack
47 unsigned int num_pushes
;
49 struct align_stack
* prev
;
52 static struct align_stack
* alignment_stack
= NULL
;
54 /* If we have a "global" #pragma pack(<n>) in effect when the first
55 #pragma pack(push,<n>) is encountered, this stores the value of
56 maximum_field_alignment in effect. When the final pop_alignment()
57 happens, we restore the value to this, not to a value of 0 for
58 maximum_field_alignment. Value is in bits. */
59 static int default_alignment
;
60 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
61 (default_alignment = maximum_field_alignment = (ALIGN))
63 static void push_alignment
PARAMS ((int, tree
));
64 static void pop_alignment
PARAMS ((tree
));
65 static void mark_align_stack
PARAMS ((void *));
67 /* Push an alignment value onto the stack. */
69 push_alignment (alignment
, id
)
73 if (alignment_stack
== NULL
74 || alignment_stack
->alignment
!= alignment
79 entry
= (align_stack
*) xmalloc (sizeof (* entry
));
81 entry
->alignment
= alignment
;
82 entry
->num_pushes
= 1;
84 entry
->prev
= alignment_stack
;
86 /* The current value of maximum_field_alignment is not necessarily
87 0 since there may be a #pragma pack(<n>) in effect; remember it
88 so that we can restore it after the final #pragma pop(). */
89 if (alignment_stack
== NULL
)
90 default_alignment
= maximum_field_alignment
;
92 alignment_stack
= entry
;
94 maximum_field_alignment
= alignment
;
97 alignment_stack
->num_pushes
++;
100 /* Undo a push of an alignment onto the stack. */
107 if (alignment_stack
== NULL
)
110 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
115 /* If we got an identifier, strip away everything above the target
116 entry so that the next step will restore the state just below it. */
119 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
122 entry
->num_pushes
= 1;
123 alignment_stack
= entry
;
128 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
129 , IDENTIFIER_POINTER (id
), IDENTIFIER_POINTER (id
));
132 if (-- alignment_stack
->num_pushes
== 0)
134 entry
= alignment_stack
->prev
;
137 maximum_field_alignment
= default_alignment
;
139 maximum_field_alignment
= entry
->alignment
;
141 free (alignment_stack
);
143 alignment_stack
= entry
;
151 align_stack
*a
= *(align_stack
**) p
;
155 ggc_mark_tree (a
->id
);
159 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
160 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
161 #define push_alignment(ID, N) \
162 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
163 #define pop_alignment(ID) \
164 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
165 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
170 #pragma pack (push, N)
171 #pragma pack (push, ID, N)
173 #pragma pack (pop, ID) */
175 handle_pragma_pack (dummy
)
176 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
180 enum cpp_ttype token
;
181 enum { set
, push
, pop
} action
;
183 if (c_lex (&x
) != CPP_OPEN_PAREN
)
184 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
187 if (token
== CPP_CLOSE_PAREN
)
192 else if (token
== CPP_NUMBER
)
194 align
= TREE_INT_CST_LOW (x
);
196 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
197 GCC_BAD ("malformed '#pragma pack' - ignored");
199 else if (token
== CPP_NAME
)
201 #define GCC_BAD_ACTION do { if (action == push) \
202 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
204 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
207 const char *op
= IDENTIFIER_POINTER (x
);
208 if (!strcmp (op
, "push"))
210 else if (!strcmp (op
, "pop"))
213 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op
);
216 if (token
!= CPP_COMMA
&& action
== push
)
219 if (token
== CPP_COMMA
)
222 if (token
== CPP_NAME
)
225 if (action
== push
&& c_lex (&x
) != CPP_COMMA
)
232 if (token
== CPP_NUMBER
)
234 align
= TREE_INT_CST_LOW (x
);
242 if (token
!= CPP_CLOSE_PAREN
)
244 #undef GCC_BAD_ACTION
247 GCC_BAD ("malformed '#pragma pack' - ignored");
249 if (c_lex (&x
) != CPP_EOF
)
250 warning ("junk at end of '#pragma pack'");
261 align
*= BITS_PER_UNIT
;
264 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
269 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
270 case push
: push_alignment (align
, id
); break;
271 case pop
: pop_alignment (id
); break;
274 #endif /* HANDLE_PRAGMA_PACK */
276 #ifdef HANDLE_PRAGMA_WEAK
277 static void apply_pragma_weak
PARAMS ((tree
, tree
));
278 static void handle_pragma_weak
PARAMS ((cpp_reader
*));
280 static tree pending_weaks
;
283 apply_pragma_weak (decl
, value
)
287 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
288 build_tree_list (NULL
, value
)),
294 maybe_apply_pragma_weak (decl
)
299 /* Copied from the check in set_decl_assembler_name. */
300 if (TREE_CODE (decl
) == FUNCTION_DECL
301 || (TREE_CODE (decl
) == VAR_DECL
302 && (TREE_STATIC (decl
)
303 || DECL_EXTERNAL (decl
)
304 || TREE_PUBLIC (decl
))))
305 id
= DECL_ASSEMBLER_NAME (decl
);
309 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
310 if (id
== TREE_PURPOSE (t
))
312 apply_pragma_weak (decl
, TREE_VALUE (t
));
318 /* #pragma weak name [= value] */
320 handle_pragma_weak (dummy
)
321 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
323 tree name
, value
, x
, decl
;
328 if (c_lex (&name
) != CPP_NAME
)
329 GCC_BAD ("malformed #pragma weak, ignored");
333 if (c_lex (&value
) != CPP_NAME
)
334 GCC_BAD ("malformed #pragma weak, ignored");
338 warning ("junk at end of #pragma weak");
340 decl
= identifier_global_value (name
);
341 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
342 apply_pragma_weak (decl
, value
);
344 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
348 maybe_apply_pragma_weak (decl
)
349 tree decl ATTRIBUTE_UNUSED
;
352 #endif /* HANDLE_PRAGMA_WEAK */
354 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
355 static void handle_pragma_redefine_extname
PARAMS ((cpp_reader
*));
357 static tree pending_redefine_extname
;
359 /* #pragma redefined_extname oldname newname */
361 handle_pragma_redefine_extname (dummy
)
362 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
364 tree oldname
, newname
, decl
, x
;
367 if (c_lex (&oldname
) != CPP_NAME
)
369 warning ("malformed #pragma redefine_extname, ignored");
372 if (c_lex (&newname
) != CPP_NAME
)
374 warning ("malformed #pragma redefine_extname, ignored");
379 warning ("junk at end of #pragma redefine_extname");
381 decl
= identifier_global_value (oldname
);
382 if (decl
&& TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd')
384 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
385 && DECL_ASSEMBLER_NAME (decl
) != newname
)
386 warning ("#pragma redefine_extname conflicts with declaration");
387 SET_DECL_ASSEMBLER_NAME (decl
, newname
);
390 pending_redefine_extname
391 = tree_cons (oldname
, newname
, pending_redefine_extname
);
395 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
396 static void handle_pragma_extern_prefix
PARAMS ((cpp_reader
*));
398 static tree pragma_extern_prefix
;
400 /* #pragma extern_prefix "prefix" */
402 handle_pragma_extern_prefix (dummy
)
403 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
408 if (c_lex (&prefix
) != CPP_STRING
)
410 warning ("malformed #pragma extern_prefix, ignored");
415 warning ("junk at end of #pragma extern_prefix");
417 /* Note that the length includes the null terminator. */
418 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
422 /* Hook from the front ends to apply the results of one of the preceeding
423 pragmas that rename variables. */
426 maybe_apply_renaming_pragma (decl
, asmname
)
431 /* Copied from the check in set_decl_assembler_name. */
432 if (TREE_CODE (decl
) == FUNCTION_DECL
433 || (TREE_CODE (decl
) == VAR_DECL
434 && (TREE_STATIC (decl
)
435 || DECL_EXTERNAL (decl
)
436 || TREE_PUBLIC (decl
))))
437 oldname
= DECL_ASSEMBLER_NAME (decl
);
441 /* If the name begins with a *, that's a sign of an asmname attached to
442 a previous declaration. */
443 if (IDENTIFIER_POINTER (oldname
)[0] == '*')
445 const char *oldasmname
= IDENTIFIER_POINTER (oldname
) + 1;
446 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldasmname
) != 0)
447 warning ("asm declaration conficts with previous rename");
448 asmname
= build_string (strlen (oldasmname
), oldasmname
);
451 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
455 for (p
= &pending_redefine_extname
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
456 if (oldname
== TREE_PURPOSE (t
))
458 const char *newname
= IDENTIFIER_POINTER (TREE_VALUE (t
));
460 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), newname
) != 0)
461 warning ("#pragma redefine_extname conflicts with declaration");
464 return build_string (strlen (newname
), newname
);
469 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
470 if (pragma_extern_prefix
&& !asmname
)
472 char *x
= concat (TREE_STRING_POINTER (pragma_extern_prefix
),
473 IDENTIFIER_POINTER (oldname
), NULL
);
474 asmname
= build_string (strlen (x
), x
);
486 #ifdef HANDLE_PRAGMA_PACK
487 cpp_register_pragma (parse_in
, 0, "pack", handle_pragma_pack
);
489 #ifdef HANDLE_PRAGMA_WEAK
490 cpp_register_pragma (parse_in
, 0, "weak", handle_pragma_weak
);
491 ggc_add_tree_root (&pending_weaks
, 1);
493 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
494 cpp_register_pragma (parse_in
, 0, "redefine_extname",
495 handle_pragma_redefine_extname
);
496 ggc_add_tree_root (&pending_redefine_extname
, 1);
498 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
499 cpp_register_pragma (parse_in
, 0, "extern_prefix",
500 handle_pragma_extern_prefix
);
501 ggc_add_tree_root (&pragma_extern_prefix
, 1);
504 #ifdef REGISTER_TARGET_PRAGMAS
505 REGISTER_TARGET_PRAGMAS (parse_in
);
508 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
509 ggc_add_root (&alignment_stack
, 1, sizeof(alignment_stack
),