1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
35 #define BAD(msgid) do { warning (msgid); return; } while (0)
36 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
38 #ifdef HANDLE_PRAGMA_PACK
39 static void handle_pragma_pack
PARAMS ((cpp_reader
*));
41 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
42 typedef struct align_stack
45 unsigned int num_pushes
;
47 struct align_stack
* prev
;
50 static struct align_stack
* alignment_stack
= NULL
;
52 /* If we have a "global" #pragma pack(<n>) in effect when the first
53 #pragma pack(push,<n>) is encountered, this stores the value of
54 maximum_field_alignment in effect. When the final pop_alignment()
55 happens, we restore the value to this, not to a value of 0 for
56 maximum_field_alignment. Value is in bits. */
57 static int default_alignment
;
58 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
59 (default_alignment = maximum_field_alignment = (ALIGN))
61 static void push_alignment
PARAMS ((int, tree
));
62 static void pop_alignment
PARAMS ((tree
));
63 static void mark_align_stack
PARAMS ((void *));
65 /* Push an alignment value onto the stack. */
67 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 BAD("#pragma pack(push[, id], <n>) is not supported on this target")
162 #define pop_alignment(ID) \
163 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 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 BAD ("malformed '#pragma pack' - ignored");
198 else if (token
== CPP_NAME
)
200 #define BAD_ACTION do { if (action == push) \
201 BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
203 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 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
)
246 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 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 handle_pragma_weak
PARAMS ((cpp_reader
*));
278 /* #pragma weak name [= value] */
280 handle_pragma_weak (dummy
)
281 cpp_reader
*dummy ATTRIBUTE_UNUSED
;
288 if (c_lex (&name
) != CPP_NAME
)
289 BAD ("malformed #pragma weak, ignored");
293 if (c_lex (&value
) != CPP_NAME
)
294 BAD ("malformed #pragma weak, ignored");
298 warning ("junk at end of #pragma weak");
300 add_weak (IDENTIFIER_POINTER (name
), value
? IDENTIFIER_POINTER (value
) : 0);
307 #ifdef HANDLE_PRAGMA_PACK
308 cpp_register_pragma (parse_in
, 0, "pack", handle_pragma_pack
);
310 #ifdef HANDLE_PRAGMA_WEAK
311 cpp_register_pragma (parse_in
, 0, "weak", handle_pragma_weak
);
313 #ifdef REGISTER_TARGET_PRAGMAS
314 REGISTER_TARGET_PRAGMAS (parse_in
);
317 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
318 ggc_add_root (&alignment_stack
, 1, sizeof(alignment_stack
),