]> gcc.gnu.org Git - gcc.git/blame - gcc/c-pragma.c
* cfgloop.c (flow_loops_cfg_dump): Use bb->index, not i.
[gcc.git] / gcc / c-pragma.c
CommitLineData
a187ac95 1/* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
79c4e63f 2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002
c913b6f1 3 Free Software Foundation, Inc.
a187ac95 4
1322177d 5This file is part of GCC.
a187ac95 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
a187ac95 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
a187ac95
RS
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
a187ac95 21
4b578ebf 22#include "config.h"
670ee920 23#include "system.h"
d05a5492 24#include "rtl.h"
4b578ebf 25#include "tree.h"
ca695ac9 26#include "function.h"
8b97c5f8 27#include "cpplib.h"
3d6f7931 28#include "c-pragma.h"
750caf0d 29#include "flags.h"
5f6da302 30#include "toplev.h"
da9da134 31#include "ggc.h"
ecb0eece 32#include "c-common.h"
78b41166 33#include "output.h"
8b97c5f8 34#include "tm_p.h"
4b578ebf 35
da4083c7
CR
36#define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
37#define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
0e5921e8
ZW
38
39#ifdef HANDLE_PRAGMA_PACK
40static void handle_pragma_pack PARAMS ((cpp_reader *));
41
e2af664c
NC
42#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
43typedef struct align_stack
44{
45 int alignment;
46 unsigned int num_pushes;
0f92adae 47 tree id;
e2af664c
NC
48 struct align_stack * prev;
49} align_stack;
50
51static struct align_stack * alignment_stack = NULL;
52
63eb1269
MC
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
61e8b354
MK
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
ec5c56db 57 maximum_field_alignment. Value is in bits. */
ecb0eece 58static int default_alignment;
0e5921e8 59#define SET_GLOBAL_ALIGNMENT(ALIGN) \
ecb0eece 60 (default_alignment = maximum_field_alignment = (ALIGN))
61e8b354 61
0e5921e8
ZW
62static void push_alignment PARAMS ((int, tree));
63static void pop_alignment PARAMS ((tree));
c6991660 64static void mark_align_stack PARAMS ((void *));
e2af664c
NC
65
66/* Push an alignment value onto the stack. */
0e5921e8 67static void
0f92adae 68push_alignment (alignment, id)
e2af664c 69 int alignment;
0f92adae 70 tree id;
e2af664c 71{
e2af664c 72 if (alignment_stack == NULL
0f92adae
JM
73 || alignment_stack->alignment != alignment
74 || id != NULL_TREE)
e2af664c
NC
75 {
76 align_stack * entry;
77
78 entry = (align_stack *) xmalloc (sizeof (* entry));
79
e2af664c
NC
80 entry->alignment = alignment;
81 entry->num_pushes = 1;
0f92adae 82 entry->id = id;
e2af664c
NC
83 entry->prev = alignment_stack;
84
61e8b354
MK
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
ec5c56db 87 so that we can restore it after the final #pragma pop(). */
61e8b354
MK
88 if (alignment_stack == NULL)
89 default_alignment = maximum_field_alignment;
90
e2af664c
NC
91 alignment_stack = entry;
92
0e5921e8 93 maximum_field_alignment = alignment;
e2af664c
NC
94 }
95 else
96 alignment_stack->num_pushes ++;
e2af664c
NC
97}
98
99/* Undo a push of an alignment onto the stack. */
0e5921e8 100static void
0f92adae
JM
101pop_alignment (id)
102 tree id;
e2af664c 103{
0f92adae
JM
104 align_stack * entry;
105
e2af664c
NC
106 if (alignment_stack == NULL)
107 {
108 warning ("\
0f92adae
JM
109#pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
110 );
0e5921e8 111 return;
e2af664c
NC
112 }
113
0f92adae
JM
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. */
116 if (id)
117 {
118 for (entry = alignment_stack; entry; entry = entry->prev)
119 if (entry->id == id)
120 {
121 entry->num_pushes = 1;
122 alignment_stack = entry;
123 break;
124 }
125 if (entry == NULL)
126 warning ("\
127#pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
128 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
129 }
130
e2af664c
NC
131 if (-- alignment_stack->num_pushes == 0)
132 {
e2af664c
NC
133 entry = alignment_stack->prev;
134
224bb373 135 if (entry == NULL)
61e8b354 136 maximum_field_alignment = default_alignment;
e2af664c 137 else
0e5921e8 138 maximum_field_alignment = entry->alignment;
e2af664c
NC
139
140 free (alignment_stack);
141
142 alignment_stack = entry;
143 }
e2af664c 144}
0e5921e8
ZW
145
146static void
147mark_align_stack (p)
148 void *p;
a187ac95 149{
0e5921e8 150 align_stack *a = *(align_stack **) p;
a187ac95 151
0e5921e8 152 while (a)
a187ac95 153 {
0e5921e8
ZW
154 ggc_mark_tree (a->id);
155 a = a->prev;
156 }
157}
158#else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
159#define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
160#define push_alignment(ID, N) \
da4083c7 161 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
0e5921e8 162#define pop_alignment(ID) \
da4083c7 163 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
e2af664c 164#endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
a187ac95 165
0e5921e8
ZW
166/* #pragma pack ()
167 #pragma pack (N)
168
169 #pragma pack (push, N)
170 #pragma pack (push, ID, N)
171 #pragma pack (pop)
172 #pragma pack (pop, ID) */
173static void
174handle_pragma_pack (dummy)
175 cpp_reader *dummy ATTRIBUTE_UNUSED;
176{
177 tree x, id = 0;
63eb1269 178 int align = -1;
0e5921e8 179 enum cpp_ttype token;
4337bc93 180 enum { set, push, pop } action;
0e5921e8
ZW
181
182 if (c_lex (&x) != CPP_OPEN_PAREN)
da4083c7 183 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
0e5921e8
ZW
184
185 token = c_lex (&x);
186 if (token == CPP_CLOSE_PAREN)
4337bc93
ZW
187 {
188 action = set;
189 align = 0;
190 }
0e5921e8
ZW
191 else if (token == CPP_NUMBER)
192 {
193 align = TREE_INT_CST_LOW (x);
194 action = set;
4337bc93 195 if (c_lex (&x) != CPP_CLOSE_PAREN)
da4083c7 196 GCC_BAD ("malformed '#pragma pack' - ignored");
0e5921e8
ZW
197 }
198 else if (token == CPP_NAME)
199 {
da4083c7
CR
200#define GCC_BAD_ACTION do { if (action == push) \
201 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
63eb1269 202 else \
da4083c7 203 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
63eb1269
MC
204 } while (0)
205
4337bc93
ZW
206 const char *op = IDENTIFIER_POINTER (x);
207 if (!strcmp (op, "push"))
0e5921e8 208 action = push;
4337bc93 209 else if (!strcmp (op, "pop"))
0e5921e8
ZW
210 action = pop;
211 else
da4083c7 212 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
a187ac95 213
0e5921e8 214 token = c_lex (&x);
63eb1269 215 if (token != CPP_COMMA && action == push)
da4083c7 216 GCC_BAD_ACTION;
63eb1269
MC
217
218 if (token == CPP_COMMA)
e2af664c 219 {
0e5921e8 220 token = c_lex (&x);
63eb1269
MC
221 if (token == CPP_NAME)
222 {
223 id = x;
224 if (action == push && c_lex (&x) != CPP_COMMA)
da4083c7 225 GCC_BAD_ACTION;
63eb1269
MC
226 token = c_lex (&x);
227 }
228
229 if (action == push)
230 {
231 if (token == CPP_NUMBER)
232 {
233 align = TREE_INT_CST_LOW (x);
234 token = c_lex (&x);
235 }
236 else
da4083c7 237 GCC_BAD_ACTION;
63eb1269 238 }
e2af664c 239 }
4337bc93 240
63eb1269 241 if (token != CPP_CLOSE_PAREN)
da4083c7
CR
242 GCC_BAD_ACTION;
243#undef GCC_BAD_ACTION
e2af664c 244 }
4337bc93 245 else
da4083c7 246 GCC_BAD ("malformed '#pragma pack' - ignored");
4337bc93 247
0e5921e8
ZW
248 if (c_lex (&x) != CPP_EOF)
249 warning ("junk at end of '#pragma pack'");
250
63eb1269
MC
251 if (action != pop)
252 switch (align)
253 {
254 case 0:
255 case 1:
256 case 2:
257 case 4:
258 case 8:
259 case 16:
260 align *= BITS_PER_UNIT;
261 break;
262 default:
da4083c7 263 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
63eb1269 264 }
fc009f96 265
0e5921e8
ZW
266 switch (action)
267 {
268 case set: SET_GLOBAL_ALIGNMENT (align); break;
0e5921e8
ZW
269 case push: push_alignment (align, id); break;
270 case pop: pop_alignment (id); break;
271 }
272}
273#endif /* HANDLE_PRAGMA_PACK */
a187ac95 274
0e5921e8 275#ifdef HANDLE_PRAGMA_WEAK
ecb0eece 276static void apply_pragma_weak PARAMS ((tree, tree));
0e5921e8 277static void handle_pragma_weak PARAMS ((cpp_reader *));
a187ac95 278
ecb0eece
RH
279static tree pending_weaks;
280
281static void
282apply_pragma_weak (decl, value)
283 tree decl, value;
284{
285 if (value)
b53bb376
RH
286 {
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)),
291 0);
292 }
293
45806a3f
FS
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");
297
ecb0eece
RH
298 declare_weak (decl);
299}
300
301void
302maybe_apply_pragma_weak (decl)
303 tree decl;
304{
305 tree *p, t, id;
306
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);
314 else
315 return;
316
317 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
318 if (id == TREE_PURPOSE (t))
319 {
320 apply_pragma_weak (decl, TREE_VALUE (t));
321 *p = TREE_CHAIN (t);
322 break;
323 }
324}
325
0e5921e8
ZW
326/* #pragma weak name [= value] */
327static void
328handle_pragma_weak (dummy)
329 cpp_reader *dummy ATTRIBUTE_UNUSED;
330{
ecb0eece 331 tree name, value, x, decl;
0e5921e8 332 enum cpp_ttype t;
a187ac95 333
0e5921e8 334 value = 0;
7169a029 335
0e5921e8 336 if (c_lex (&name) != CPP_NAME)
da4083c7 337 GCC_BAD ("malformed #pragma weak, ignored");
0e5921e8
ZW
338 t = c_lex (&x);
339 if (t == CPP_EQ)
340 {
341 if (c_lex (&value) != CPP_NAME)
da4083c7 342 GCC_BAD ("malformed #pragma weak, ignored");
0e5921e8
ZW
343 t = c_lex (&x);
344 }
345 if (t != CPP_EOF)
346 warning ("junk at end of #pragma weak");
0f92adae 347
ecb0eece
RH
348 decl = identifier_global_value (name);
349 if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
b53bb376
RH
350 {
351 apply_pragma_weak (decl, value);
352 if (value)
353 assemble_alias (decl, value);
354 }
ecb0eece
RH
355 else
356 pending_weaks = tree_cons (name, value, pending_weaks);
0e5921e8 357}
ecb0eece
RH
358#else
359void
360maybe_apply_pragma_weak (decl)
361 tree decl ATTRIBUTE_UNUSED;
362{
363}
364#endif /* HANDLE_PRAGMA_WEAK */
0f92adae 365
41c64394
RH
366#ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
367static void handle_pragma_redefine_extname PARAMS ((cpp_reader *));
368
369static tree pending_redefine_extname;
370
371/* #pragma redefined_extname oldname newname */
372static void
373handle_pragma_redefine_extname (dummy)
374 cpp_reader *dummy ATTRIBUTE_UNUSED;
375{
376 tree oldname, newname, decl, x;
377 enum cpp_ttype t;
378
379 if (c_lex (&oldname) != CPP_NAME)
380 {
381 warning ("malformed #pragma redefine_extname, ignored");
382 return;
383 }
384 if (c_lex (&newname) != CPP_NAME)
385 {
386 warning ("malformed #pragma redefine_extname, ignored");
387 return;
388 }
389 t = c_lex (&x);
390 if (t != CPP_EOF)
391 warning ("junk at end of #pragma redefine_extname");
392
393 decl = identifier_global_value (oldname);
394 if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
395 {
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);
400 }
401 else
402 pending_redefine_extname
403 = tree_cons (oldname, newname, pending_redefine_extname);
404}
405#endif
406
407#ifdef HANDLE_PRAGMA_EXTERN_PREFIX
408static void handle_pragma_extern_prefix PARAMS ((cpp_reader *));
409
410static tree pragma_extern_prefix;
411
412/* #pragma extern_prefix "prefix" */
413static void
414handle_pragma_extern_prefix (dummy)
415 cpp_reader *dummy ATTRIBUTE_UNUSED;
416{
417 tree prefix, x;
418 enum cpp_ttype t;
419
420 if (c_lex (&prefix) != CPP_STRING)
421 {
422 warning ("malformed #pragma extern_prefix, ignored");
423 return;
424 }
425 t = c_lex (&x);
426 if (t != CPP_EOF)
427 warning ("junk at end of #pragma extern_prefix");
428
429 /* Note that the length includes the null terminator. */
430 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
431}
432#endif
433
434/* Hook from the front ends to apply the results of one of the preceeding
435 pragmas that rename variables. */
436
437tree
438maybe_apply_renaming_pragma (decl, asmname)
439 tree decl, asmname;
440{
441 tree oldname;
442
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);
450 else
451 return asmname;
452
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] == '*')
456 {
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);
461 }
462
463#ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
464 {
465 tree *p, t;
466
467 for (p = &pending_redefine_extname; (t = *p) ; p = &TREE_CHAIN (t))
468 if (oldname == TREE_PURPOSE (t))
469 {
470 const char *newname = IDENTIFIER_POINTER (TREE_VALUE (t));
471
472 if (asmname && strcmp (TREE_STRING_POINTER (asmname), newname) != 0)
473 warning ("#pragma redefine_extname conflicts with declaration");
474 *p = TREE_CHAIN (t);
475
476 return build_string (strlen (newname), newname);
477 }
478 }
479#endif
480
481#ifdef HANDLE_PRAGMA_EXTERN_PREFIX
482 if (pragma_extern_prefix && !asmname)
483 {
484 char *x = concat (TREE_STRING_POINTER (pragma_extern_prefix),
485 IDENTIFIER_POINTER (oldname), NULL);
486 asmname = build_string (strlen (x), x);
487 free (x);
488 return asmname;
489 }
490#endif
491
492 return asmname;
493}
494
568767a6
RH
495void
496init_pragma ()
497{
0e5921e8 498#ifdef HANDLE_PRAGMA_PACK
f3b55474 499 cpp_register_pragma (parse_in, 0, "pack", handle_pragma_pack);
0e5921e8
ZW
500#endif
501#ifdef HANDLE_PRAGMA_WEAK
f3b55474 502 cpp_register_pragma (parse_in, 0, "weak", handle_pragma_weak);
ecb0eece 503 ggc_add_tree_root (&pending_weaks, 1);
0e5921e8 504#endif
41c64394
RH
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);
509#endif
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);
514#endif
515
8b97c5f8 516#ifdef REGISTER_TARGET_PRAGMAS
f3b55474 517 REGISTER_TARGET_PRAGMAS (parse_in);
8b97c5f8
ZW
518#endif
519
568767a6
RH
520#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
521 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
522 mark_align_stack);
523#endif
524}
This page took 1.004843 seconds and 5 git commands to generate.