Merge branch 'vendor/MDOCML'
[dragonfly.git] / contrib / gcc-4.4 / gcc / c-pragma.c
1 /* Handle #pragma, system V.4 style.  Supports #pragma weak and #pragma pack.
2    Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3    2006, 2007, 2008 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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 3, or (at your option) any later
10 version.
11
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
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "function.h"
28 #include "cpplib.h"
29 #include "c-pragma.h"
30 #include "flags.h"
31 #include "toplev.h"
32 #include "ggc.h"
33 #include "c-common.h"
34 #include "output.h"
35 #include "tm_p.h"
36 #include "vec.h"
37 #include "target.h"
38 #include "diagnostic.h"
39 #include "opts.h"
40
41 #define GCC_BAD(gmsgid) \
42   do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
43 #define GCC_BAD2(gmsgid, arg) \
44   do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
45
46 typedef struct align_stack GTY(())
47 {
48   int                  alignment;
49   tree                 id;
50   struct align_stack * prev;
51 } align_stack;
52
53 static GTY(()) struct align_stack * alignment_stack;
54
55 #ifdef HANDLE_PRAGMA_PACK
56 static void handle_pragma_pack (cpp_reader *);
57
58 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
59 /* If we have a "global" #pragma pack(<n>) in effect when the first
60    #pragma pack(push,<n>) is encountered, this stores the value of
61    maximum_field_alignment in effect.  When the final pop_alignment()
62    happens, we restore the value to this, not to a value of 0 for
63    maximum_field_alignment.  Value is in bits.  */
64 static int default_alignment;
65 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
66         ? &default_alignment \
67         : &alignment_stack->alignment) = (ALIGN))
68
69 static void push_alignment (int, tree);
70 static void pop_alignment (tree);
71
72 /* Push an alignment value onto the stack.  */
73 static void
74 push_alignment (int alignment, tree id)
75 {
76   align_stack * entry;
77
78   entry = GGC_NEW (align_stack);
79
80   entry->alignment  = alignment;
81   entry->id         = id;
82   entry->prev       = alignment_stack;
83
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;
89
90   alignment_stack = entry;
91
92   maximum_field_alignment = alignment;
93 }
94
95 /* Undo a push of an alignment onto the stack.  */
96 static void
97 pop_alignment (tree id)
98 {
99   align_stack * entry;
100
101   if (alignment_stack == NULL)
102     GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
103
104   /* If we got an identifier, strip away everything above the target
105      entry so that the next step will restore the state just below it.  */
106   if (id)
107     {
108       for (entry = alignment_stack; entry; entry = entry->prev)
109         if (entry->id == id)
110           {
111             alignment_stack = entry;
112             break;
113           }
114       if (entry == NULL)
115         warning (OPT_Wpragmas, "\
116 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
117                  , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
118     }
119
120   entry = alignment_stack->prev;
121
122   maximum_field_alignment = entry ? entry->alignment : default_alignment;
123
124   alignment_stack = entry;
125 }
126 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
127 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
128 #define push_alignment(ID, N) \
129     GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
130 #define pop_alignment(ID) \
131     GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
132 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
133
134 /* #pragma pack ()
135    #pragma pack (N)
136
137    #pragma pack (push)
138    #pragma pack (push, N)
139    #pragma pack (push, ID)
140    #pragma pack (push, ID, N)
141    #pragma pack (pop)
142    #pragma pack (pop, ID) */
143 static void
144 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
145 {
146   tree x, id = 0;
147   int align = -1;
148   enum cpp_ttype token;
149   enum { set, push, pop } action;
150
151   if (pragma_lex (&x) != CPP_OPEN_PAREN)
152     GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
153
154   token = pragma_lex (&x);
155   if (token == CPP_CLOSE_PAREN)
156     {
157       action = set;
158       align = initial_max_fld_align;
159     }
160   else if (token == CPP_NUMBER)
161     {
162       if (TREE_CODE (x) != INTEGER_CST)
163         GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
164       align = TREE_INT_CST_LOW (x);
165       action = set;
166       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
167         GCC_BAD ("malformed %<#pragma pack%> - ignored");
168     }
169   else if (token == CPP_NAME)
170     {
171 #define GCC_BAD_ACTION do { if (action != pop) \
172           GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
173         else \
174           GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
175         } while (0)
176
177       const char *op = IDENTIFIER_POINTER (x);
178       if (!strcmp (op, "push"))
179         action = push;
180       else if (!strcmp (op, "pop"))
181         action = pop;
182       else
183         GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op);
184
185       while ((token = pragma_lex (&x)) == CPP_COMMA)
186         {
187           token = pragma_lex (&x);
188           if (token == CPP_NAME && id == 0)
189             {
190               id = x;
191             }
192           else if (token == CPP_NUMBER && action == push && align == -1)
193             {
194               if (TREE_CODE (x) != INTEGER_CST)
195                 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
196               align = TREE_INT_CST_LOW (x);
197               if (align == -1)
198                 action = set;
199             }
200           else
201             GCC_BAD_ACTION;
202         }
203
204       if (token != CPP_CLOSE_PAREN)
205         GCC_BAD_ACTION;
206 #undef GCC_BAD_ACTION
207     }
208   else
209     GCC_BAD ("malformed %<#pragma pack%> - ignored");
210
211   if (pragma_lex (&x) != CPP_EOF)
212     warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
213
214   if (flag_pack_struct)
215     GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
216
217   if (action != pop)
218     switch (align)
219       {
220       case 0:
221       case 1:
222       case 2:
223       case 4:
224       case 8:
225       case 16:
226         align *= BITS_PER_UNIT;
227         break;
228       case -1:
229         if (action == push)
230           {
231             align = maximum_field_alignment;
232             break;
233           }
234       default:
235         GCC_BAD2 ("alignment must be a small power of two, not %d", align);
236       }
237
238   switch (action)
239     {
240     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
241     case push:  push_alignment (align, id);    break;
242     case pop:   pop_alignment (id);            break;
243     }
244 }
245 #endif  /* HANDLE_PRAGMA_PACK */
246
247 static GTY(()) tree pending_weaks;
248
249 #ifdef HANDLE_PRAGMA_WEAK
250 static void apply_pragma_weak (tree, tree);
251 static void handle_pragma_weak (cpp_reader *);
252
253 static void
254 apply_pragma_weak (tree decl, tree value)
255 {
256   if (value)
257     {
258       value = build_string (IDENTIFIER_LENGTH (value),
259                             IDENTIFIER_POINTER (value));
260       decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
261                                                build_tree_list (NULL, value)),
262                        0);
263     }
264
265   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
266       && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma.  */
267       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
268     warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
269              "results in unspecified behavior", decl);
270
271   declare_weak (decl);
272 }
273
274 void
275 maybe_apply_pragma_weak (tree decl)
276 {
277   tree *p, t, id;
278
279   /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed.  */
280
281   /* No weak symbols pending, take the short-cut.  */
282   if (!pending_weaks)
283     return;
284   /* If it's not visible outside this file, it doesn't matter whether
285      it's weak.  */
286   if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
287     return;
288   /* If it's not a function or a variable, it can't be weak.
289      FIXME: what kinds of things are visible outside this file but
290      aren't functions or variables?   Should this be an assert instead?  */
291   if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
292     return;
293
294   id = DECL_ASSEMBLER_NAME (decl);
295
296   for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
297     if (id == TREE_PURPOSE (t))
298       {
299         apply_pragma_weak (decl, TREE_VALUE (t));
300         *p = TREE_CHAIN (t);
301         break;
302       }
303 }
304
305 /* Process all "#pragma weak A = B" directives where we have not seen
306    a decl for A.  */
307 void
308 maybe_apply_pending_pragma_weaks (void)
309 {
310   tree *p, t, alias_id, id, decl, *next;
311
312   for (p = &pending_weaks; (t = *p) ; p = next)
313     {
314       next = &TREE_CHAIN (t);
315       alias_id = TREE_PURPOSE (t);
316       id = TREE_VALUE (t);
317
318       if (TREE_VALUE (t) == NULL)
319         continue;
320
321       decl = build_decl (FUNCTION_DECL, alias_id, default_function_type);
322
323       DECL_ARTIFICIAL (decl) = 1;
324       TREE_PUBLIC (decl) = 1;
325       DECL_EXTERNAL (decl) = 1;
326       DECL_WEAK (decl) = 1;
327
328       assemble_alias (decl, id);
329     }
330 }
331
332 /* #pragma weak name [= value] */
333 static void
334 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
335 {
336   tree name, value, x, decl;
337   enum cpp_ttype t;
338
339   value = 0;
340
341   if (pragma_lex (&name) != CPP_NAME)
342     GCC_BAD ("malformed #pragma weak, ignored");
343   t = pragma_lex (&x);
344   if (t == CPP_EQ)
345     {
346       if (pragma_lex (&value) != CPP_NAME)
347         GCC_BAD ("malformed #pragma weak, ignored");
348       t = pragma_lex (&x);
349     }
350   if (t != CPP_EOF)
351     warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
352
353   decl = identifier_global_value (name);
354   if (decl && DECL_P (decl))
355     {
356       apply_pragma_weak (decl, value);
357       if (value)
358         assemble_alias (decl, value);
359     }
360   else
361     pending_weaks = tree_cons (name, value, pending_weaks);
362 }
363 #else
364 void
365 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
366 {
367 }
368
369 void
370 maybe_apply_pending_pragma_weaks (void)
371 {
372 }
373 #endif /* HANDLE_PRAGMA_WEAK */
374
375 /* GCC supports two #pragma directives for renaming the external
376    symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
377    compatibility with the Solaris and Tru64 system headers.  GCC also
378    has its own notation for this, __asm__("name") annotations.
379
380    Corner cases of these features and their interaction:
381
382    1) Both pragmas silently apply only to declarations with external
383       linkage (that is, TREE_PUBLIC || DECL_EXTERNAL).  Asm labels
384       do not have this restriction.
385
386    2) In C++, both #pragmas silently apply only to extern "C" declarations.
387       Asm labels do not have this restriction.
388
389    3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
390       applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
391       new name is different, a warning issues and the name does not change.
392
393    4) The "source name" for #pragma redefine_extname is the DECL_NAME,
394       *not* the DECL_ASSEMBLER_NAME.
395
396    5) If #pragma extern_prefix is in effect and a declaration occurs
397       with an __asm__ name, the #pragma extern_prefix is silently
398       ignored for that declaration.
399
400    6) If #pragma extern_prefix and #pragma redefine_extname apply to
401       the same declaration, whichever triggered first wins, and a warning
402       is issued.  (We would like to have #pragma redefine_extname always
403       win, but it can appear either before or after the declaration, and
404       if it appears afterward, we have no way of knowing whether a modified
405       DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.)  */
406
407 static GTY(()) tree pending_redefine_extname;
408
409 static void handle_pragma_redefine_extname (cpp_reader *);
410
411 /* #pragma redefine_extname oldname newname */
412 static void
413 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
414 {
415   tree oldname, newname, decl, x;
416   enum cpp_ttype t;
417
418   if (pragma_lex (&oldname) != CPP_NAME)
419     GCC_BAD ("malformed #pragma redefine_extname, ignored");
420   if (pragma_lex (&newname) != CPP_NAME)
421     GCC_BAD ("malformed #pragma redefine_extname, ignored");
422   t = pragma_lex (&x);
423   if (t != CPP_EOF)
424     warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
425
426   if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
427     {
428       if (warn_unknown_pragmas > in_system_header)
429         warning (OPT_Wunknown_pragmas,
430                  "#pragma redefine_extname not supported on this target");
431       return;
432     }
433
434   decl = identifier_global_value (oldname);
435   if (decl
436       && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
437       && (TREE_CODE (decl) == FUNCTION_DECL
438           || TREE_CODE (decl) == VAR_DECL)
439       && has_c_linkage (decl))
440     {
441       if (DECL_ASSEMBLER_NAME_SET_P (decl))
442         {
443           const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
444           name = targetm.strip_name_encoding (name);
445
446           if (strcmp (name, IDENTIFIER_POINTER (newname)))
447             warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
448                      "conflict with previous rename");
449         }
450       else
451         change_decl_assembler_name (decl, newname);
452     }
453   else
454     /* We have to add this to the rename list even if there's already
455        a global value that doesn't meet the above criteria, because in
456        C++ "struct foo {...};" puts "foo" in the current namespace but
457        does *not* conflict with a subsequent declaration of a function
458        or variable foo.  See g++.dg/other/pragma-re-2.C.  */
459     add_to_renaming_pragma_list (oldname, newname);
460 }
461
462 /* This is called from here and from ia64.c.  */
463 void
464 add_to_renaming_pragma_list (tree oldname, tree newname)
465 {
466   tree previous = purpose_member (oldname, pending_redefine_extname);
467   if (previous)
468     {
469       if (TREE_VALUE (previous) != newname)
470         warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
471                  "conflict with previous #pragma redefine_extname");
472       return;
473     }
474
475   pending_redefine_extname
476     = tree_cons (oldname, newname, pending_redefine_extname);
477 }
478
479 static GTY(()) tree pragma_extern_prefix;
480
481 /* #pragma extern_prefix "prefix" */
482 static void
483 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
484 {
485   tree prefix, x;
486   enum cpp_ttype t;
487
488   if (pragma_lex (&prefix) != CPP_STRING)
489     GCC_BAD ("malformed #pragma extern_prefix, ignored");
490   t = pragma_lex (&x);
491   if (t != CPP_EOF)
492     warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
493
494   if (targetm.handle_pragma_extern_prefix)
495     /* Note that the length includes the null terminator.  */
496     pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
497   else if (warn_unknown_pragmas > in_system_header)
498     warning (OPT_Wunknown_pragmas,
499              "#pragma extern_prefix not supported on this target");
500 }
501
502 /* Hook from the front ends to apply the results of one of the preceding
503    pragmas that rename variables.  */
504
505 tree
506 maybe_apply_renaming_pragma (tree decl, tree asmname)
507 {
508   tree *p, t;
509
510   /* The renaming pragmas are only applied to declarations with
511      external linkage.  */
512   if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
513       || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
514       || !has_c_linkage (decl))
515     return asmname;
516
517   /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
518      but we may warn about a rename that conflicts.  */
519   if (DECL_ASSEMBLER_NAME_SET_P (decl))
520     {
521       const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
522       oldname = targetm.strip_name_encoding (oldname);
523
524       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
525           warning (OPT_Wpragmas, "asm declaration ignored due to "
526                    "conflict with previous rename");
527
528       /* Take any pending redefine_extname off the list.  */
529       for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
530         if (DECL_NAME (decl) == TREE_PURPOSE (t))
531           {
532             /* Only warn if there is a conflict.  */
533             if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
534               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
535                        "conflict with previous rename");
536
537             *p = TREE_CHAIN (t);
538             break;
539           }
540       return 0;
541     }
542
543   /* Find out if we have a pending #pragma redefine_extname.  */
544   for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
545     if (DECL_NAME (decl) == TREE_PURPOSE (t))
546       {
547         tree newname = TREE_VALUE (t);
548         *p = TREE_CHAIN (t);
549
550         /* If we already have an asmname, #pragma redefine_extname is
551            ignored (with a warning if it conflicts).  */
552         if (asmname)
553           {
554             if (strcmp (TREE_STRING_POINTER (asmname),
555                         IDENTIFIER_POINTER (newname)) != 0)
556               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
557                        "conflict with __asm__ declaration");
558             return asmname;
559           }
560
561         /* Otherwise we use what we've got; #pragma extern_prefix is
562            silently ignored.  */
563         return build_string (IDENTIFIER_LENGTH (newname),
564                              IDENTIFIER_POINTER (newname));
565       }
566
567   /* If we've got an asmname, #pragma extern_prefix is silently ignored.  */
568   if (asmname)
569     return asmname;
570
571   /* If #pragma extern_prefix is in effect, apply it.  */
572   if (pragma_extern_prefix)
573     {
574       const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
575       size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
576
577       const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
578       size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
579
580       char *newname = (char *) alloca (plen + ilen + 1);
581
582       memcpy (newname,        prefix, plen);
583       memcpy (newname + plen, id, ilen + 1);
584
585       return build_string (plen + ilen, newname);
586     }
587
588   /* Nada.  */
589   return 0;
590 }
591
592
593 #ifdef HANDLE_PRAGMA_VISIBILITY
594 static void handle_pragma_visibility (cpp_reader *);
595
596 typedef enum symbol_visibility visibility;
597 DEF_VEC_I (visibility);
598 DEF_VEC_ALLOC_I (visibility, heap);
599 static VEC (visibility, heap) *visstack;
600
601 /* Push the visibility indicated by STR onto the top of the #pragma
602    visibility stack.  */
603
604 void
605 push_visibility (const char *str)
606 {
607   VEC_safe_push (visibility, heap, visstack,
608                  default_visibility);
609   if (!strcmp (str, "default"))
610     default_visibility = VISIBILITY_DEFAULT;
611   else if (!strcmp (str, "internal"))
612     default_visibility = VISIBILITY_INTERNAL;
613   else if (!strcmp (str, "hidden"))
614     default_visibility = VISIBILITY_HIDDEN;
615   else if (!strcmp (str, "protected"))
616     default_visibility = VISIBILITY_PROTECTED;
617   else
618     GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
619   visibility_options.inpragma = 1;
620 }
621
622 /* Pop a level of the #pragma visibility stack.  */
623
624 void
625 pop_visibility (void)
626 {
627   default_visibility = VEC_pop (visibility, visstack);
628   visibility_options.inpragma
629     = VEC_length (visibility, visstack) != 0;
630 }
631
632 /* Sets the default visibility for symbols to something other than that
633    specified on the command line.  */
634
635 static void
636 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
637 {
638   /* Form is #pragma GCC visibility push(hidden)|pop */
639   tree x;
640   enum cpp_ttype token;
641   enum { bad, push, pop } action = bad;
642
643   token = pragma_lex (&x);
644   if (token == CPP_NAME)
645     {
646       const char *op = IDENTIFIER_POINTER (x);
647       if (!strcmp (op, "push"))
648         action = push;
649       else if (!strcmp (op, "pop"))
650         action = pop;
651     }
652   if (bad == action)
653     GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
654   else
655     {
656       if (pop == action)
657         {
658           if (!VEC_length (visibility, visstack))
659             GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
660           else
661             pop_visibility ();
662         }
663       else
664         {
665           if (pragma_lex (&x) != CPP_OPEN_PAREN)
666             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
667           token = pragma_lex (&x);
668           if (token != CPP_NAME)
669             GCC_BAD ("malformed #pragma GCC visibility push");
670           else
671             push_visibility (IDENTIFIER_POINTER (x));
672           if (pragma_lex (&x) != CPP_CLOSE_PAREN)
673             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
674         }
675     }
676   if (pragma_lex (&x) != CPP_EOF)
677     warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
678 }
679
680 #endif
681
682 static void
683 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
684 {
685   const char *kind_string, *option_string;
686   unsigned int option_index;
687   enum cpp_ttype token;
688   diagnostic_t kind;
689   tree x;
690
691   if (cfun)
692     {
693       error ("#pragma GCC diagnostic not allowed inside functions");
694       return;
695     }
696
697   token = pragma_lex (&x);
698   if (token != CPP_NAME)
699     GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
700   kind_string = IDENTIFIER_POINTER (x);
701   if (strcmp (kind_string, "error") == 0)
702     kind = DK_ERROR;
703   else if (strcmp (kind_string, "warning") == 0)
704     kind = DK_WARNING;
705   else if (strcmp (kind_string, "ignored") == 0)
706     kind = DK_IGNORED;
707   else
708     GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
709
710   token = pragma_lex (&x);
711   if (token != CPP_STRING)
712     GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
713   option_string = TREE_STRING_POINTER (x);
714   for (option_index = 0; option_index < cl_options_count; option_index++)
715     if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
716       {
717         /* This overrides -Werror, for example.  */
718         diagnostic_classify_diagnostic (global_dc, option_index, kind);
719         /* This makes sure the option is enabled, like -Wfoo would do.  */
720         if (cl_options[option_index].var_type == CLVC_BOOLEAN
721             && cl_options[option_index].flag_var
722             && kind != DK_IGNORED)
723             *(int *) cl_options[option_index].flag_var = 1;
724         return;
725       }
726   GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
727 }
728
729 /*  Parse #pragma GCC target (xxx) to set target specific options.  */
730 static void
731 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
732 {
733   enum cpp_ttype token;
734   tree x;
735   bool close_paren_needed_p = false;
736
737   if (cfun)
738     {
739       error ("#pragma GCC option is not allowed inside functions");
740       return;
741     }
742
743   token = pragma_lex (&x);
744   if (token == CPP_OPEN_PAREN)
745     {
746       close_paren_needed_p = true;
747       token = pragma_lex (&x);
748     }
749
750   if (token != CPP_STRING)
751     {
752       GCC_BAD ("%<#pragma GCC option%> is not a string");
753       return;
754     }
755
756   /* Strings are user options.  */
757   else
758     {
759       tree args = NULL_TREE;
760
761       do
762         {
763           /* Build up the strings now as a tree linked list.  Skip empty
764              strings.  */
765           if (TREE_STRING_LENGTH (x) > 0)
766             args = tree_cons (NULL_TREE, x, args);
767
768           token = pragma_lex (&x);
769           while (token == CPP_COMMA)
770             token = pragma_lex (&x);
771         }
772       while (token == CPP_STRING);
773
774       if (close_paren_needed_p)
775         {
776           if (token == CPP_CLOSE_PAREN)
777             token = pragma_lex (&x);
778           else
779             GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
780                      "not have a final %<)%>.");
781         }
782
783       if (token != CPP_EOF)
784         {
785           error ("#pragma GCC target string... is badly formed");
786           return;
787         }
788
789       /* put arguments in the order the user typed them.  */
790       args = nreverse (args);
791
792       if (targetm.target_option.pragma_parse (args, NULL_TREE))
793         current_target_pragma = args;
794     }
795 }
796
797 /* Handle #pragma GCC optimize to set optimization options.  */
798 static void
799 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
800 {
801   enum cpp_ttype token;
802   tree x;
803   bool close_paren_needed_p = false;
804   tree optimization_previous_node = optimization_current_node;
805
806   if (cfun)
807     {
808       error ("#pragma GCC optimize is not allowed inside functions");
809       return;
810     }
811
812   token = pragma_lex (&x);
813   if (token == CPP_OPEN_PAREN)
814     {
815       close_paren_needed_p = true;
816       token = pragma_lex (&x);
817     }
818
819   if (token != CPP_STRING && token != CPP_NUMBER)
820     {
821       GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
822       return;
823     }
824
825   /* Strings/numbers are user options.  */
826   else
827     {
828       tree args = NULL_TREE;
829
830       do
831         {
832           /* Build up the numbers/strings now as a list.  */
833           if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
834             args = tree_cons (NULL_TREE, x, args);
835
836           token = pragma_lex (&x);
837           while (token == CPP_COMMA)
838             token = pragma_lex (&x);
839         }
840       while (token == CPP_STRING || token == CPP_NUMBER);
841
842       if (close_paren_needed_p)
843         {
844           if (token == CPP_CLOSE_PAREN)
845             token = pragma_lex (&x);
846           else
847             GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
848                      "not have a final %<)%>.");
849         }
850
851       if (token != CPP_EOF)
852         {
853           error ("#pragma GCC optimize string... is badly formed");
854           return;
855         }
856
857       /* put arguments in the order the user typed them.  */
858       args = nreverse (args);
859
860       parse_optimize_options (args, false);
861       current_optimize_pragma = chainon (current_optimize_pragma, args);
862       optimization_current_node = build_optimization_node ();
863       c_cpp_builtins_optimize_pragma (parse_in,
864                                       optimization_previous_node,
865                                       optimization_current_node);
866     }
867 }
868
869 /* Stack of the #pragma GCC options created with #pragma GCC push_option.  Save
870    both the binary representation of the options and the TREE_LIST of
871    strings that will be added to the function's attribute list.  */
872 typedef struct opt_stack GTY(())
873 {
874   struct opt_stack *prev;
875   tree target_binary;
876   tree target_strings;
877   tree optimize_binary;
878   tree optimize_strings;
879 } opt_stack;
880
881 static GTY(()) struct opt_stack * options_stack;
882
883 /* Handle #pragma GCC push_options to save the current target and optimization
884    options.  */
885
886 static void
887 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
888 {
889   enum cpp_ttype token;
890   tree x = 0;
891   opt_stack *p;
892
893   token = pragma_lex (&x);
894   if (token != CPP_EOF)
895     {
896       warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
897       return;
898     }
899
900   p = GGC_NEW (opt_stack);
901   p->prev = options_stack;
902   options_stack = p;
903
904   /* Save optimization and target flags in binary format.  */
905   p->optimize_binary = build_optimization_node ();
906   p->target_binary = build_target_option_node ();
907
908   /* Save optimization and target flags in string list format.  */
909   p->optimize_strings = copy_list (current_optimize_pragma);
910   p->target_strings = copy_list (current_target_pragma);
911 }
912
913 /* Handle #pragma GCC pop_options to restore the current target and
914    optimization options from a previous push_options.  */
915
916 static void
917 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
918 {
919   enum cpp_ttype token;
920   tree x = 0;
921   opt_stack *p;
922
923   token = pragma_lex (&x);
924   if (token != CPP_EOF)
925     {
926       warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
927       return;
928     }
929
930   if (! options_stack)
931     {
932       warning (OPT_Wpragmas,
933                "%<#pragma GCC pop_options%> without a corresponding "
934                "%<#pragma GCC push_options%>");
935       return;
936     }
937
938   p = options_stack;
939   options_stack = p->prev;
940
941   if (p->target_binary != target_option_current_node)
942     {
943       (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
944       target_option_current_node = p->target_binary;
945     }
946
947   if (p->optimize_binary != optimization_current_node)
948     {
949       tree old_optimize = optimization_current_node;
950       cl_optimization_restore (TREE_OPTIMIZATION (p->optimize_binary));
951       c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
952                                       p->optimize_binary);
953       optimization_current_node = p->optimize_binary;
954     }
955
956   current_target_pragma = p->target_strings;
957   current_optimize_pragma = p->optimize_strings;
958 }
959
960 /* Handle #pragma GCC reset_options to restore the current target and
961    optimization options to the original options used on the command line.  */
962
963 static void
964 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
965 {
966   enum cpp_ttype token;
967   tree x = 0;
968   tree new_optimize = optimization_default_node;
969   tree new_target = target_option_default_node;
970
971   token = pragma_lex (&x);
972   if (token != CPP_EOF)
973     {
974       warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
975       return;
976     }
977
978   if (new_target != target_option_current_node)
979     {
980       (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
981       target_option_current_node = new_target;
982     }
983
984   if (new_optimize != optimization_current_node)
985     {
986       tree old_optimize = optimization_current_node;
987       cl_optimization_restore (TREE_OPTIMIZATION (new_optimize));
988       c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
989       optimization_current_node = new_optimize;
990     }
991
992   current_target_pragma = NULL_TREE;
993   current_optimize_pragma = NULL_TREE;
994 }
995
996 /* Print a plain user-specified message.  */
997
998 static void
999 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1000 {
1001   enum cpp_ttype token;
1002   tree x, message = 0;
1003
1004   token = pragma_lex (&x);
1005   if (token == CPP_OPEN_PAREN)
1006     {
1007       token = pragma_lex (&x);
1008       if (token == CPP_STRING)
1009         message = x;
1010       else
1011         GCC_BAD ("expected a string after %<#pragma message%>");
1012       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1013         GCC_BAD ("malformed %<#pragma message%>, ignored");
1014     }
1015   else if (token == CPP_STRING)
1016     message = x;
1017   else
1018     GCC_BAD ("expected a string after %<#pragma message%>");
1019
1020   gcc_assert (message);
1021
1022   if (pragma_lex (&x) != CPP_EOF)
1023     warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1024
1025   if (TREE_STRING_LENGTH (message) > 1)
1026     inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1027 }
1028
1029 /* A vector of registered pragma callbacks.  */
1030
1031 DEF_VEC_O (pragma_handler);
1032 DEF_VEC_ALLOC_O (pragma_handler, heap);
1033
1034 static VEC(pragma_handler, heap) *registered_pragmas;
1035
1036 typedef struct
1037 {
1038   const char *space;
1039   const char *name;
1040 } pragma_ns_name;
1041
1042 DEF_VEC_O (pragma_ns_name);
1043 DEF_VEC_ALLOC_O (pragma_ns_name, heap);
1044
1045 static VEC(pragma_ns_name, heap) *registered_pp_pragmas;
1046
1047 struct omp_pragma_def { const char *name; unsigned int id; };
1048 static const struct omp_pragma_def omp_pragmas[] = {
1049   { "atomic", PRAGMA_OMP_ATOMIC },
1050   { "barrier", PRAGMA_OMP_BARRIER },
1051   { "critical", PRAGMA_OMP_CRITICAL },
1052   { "flush", PRAGMA_OMP_FLUSH },
1053   { "for", PRAGMA_OMP_FOR },
1054   { "master", PRAGMA_OMP_MASTER },
1055   { "ordered", PRAGMA_OMP_ORDERED },
1056   { "parallel", PRAGMA_OMP_PARALLEL },
1057   { "section", PRAGMA_OMP_SECTION },
1058   { "sections", PRAGMA_OMP_SECTIONS },
1059   { "single", PRAGMA_OMP_SINGLE },
1060   { "task", PRAGMA_OMP_TASK },
1061   { "taskwait", PRAGMA_OMP_TASKWAIT },
1062   { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1063 };
1064
1065 void
1066 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1067 {
1068   const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1069   int i;
1070
1071   for (i = 0; i < n_omp_pragmas; ++i)
1072     if (omp_pragmas[i].id == id)
1073       {
1074         *space = "omp";
1075         *name = omp_pragmas[i].name;
1076         return;
1077       }
1078
1079   if (id >= PRAGMA_FIRST_EXTERNAL
1080       && (id < PRAGMA_FIRST_EXTERNAL
1081           + VEC_length (pragma_ns_name, registered_pp_pragmas)))
1082     {
1083       *space = VEC_index (pragma_ns_name, registered_pp_pragmas,
1084                           id - PRAGMA_FIRST_EXTERNAL)->space;
1085       *name = VEC_index (pragma_ns_name, registered_pp_pragmas,
1086                          id - PRAGMA_FIRST_EXTERNAL)->name;
1087       return;
1088     }
1089
1090   gcc_unreachable ();
1091 }
1092
1093 /* Front-end wrappers for pragma registration to avoid dragging
1094    cpplib.h in almost everywhere.  */
1095
1096 static void
1097 c_register_pragma_1 (const char *space, const char *name,
1098                      pragma_handler handler, bool allow_expansion)
1099 {
1100   unsigned id;
1101
1102   if (flag_preprocess_only)
1103     {
1104       pragma_ns_name ns_name;
1105
1106       if (!allow_expansion)
1107         return;
1108
1109       ns_name.space = space;
1110       ns_name.name = name;
1111       VEC_safe_push (pragma_ns_name, heap, registered_pp_pragmas, &ns_name);
1112       id = VEC_length (pragma_ns_name, registered_pp_pragmas);
1113       id += PRAGMA_FIRST_EXTERNAL - 1;
1114     }
1115   else
1116     {
1117       VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
1118       id = VEC_length (pragma_handler, registered_pragmas);
1119       id += PRAGMA_FIRST_EXTERNAL - 1;
1120
1121       /* The C++ front end allocates 6 bits in cp_token; the C front end
1122          allocates 7 bits in c_token.  At present this is sufficient.  */
1123       gcc_assert (id < 64);
1124     }
1125
1126   cpp_register_deferred_pragma (parse_in, space, name, id,
1127                                 allow_expansion, false);
1128 }
1129
1130 void
1131 c_register_pragma (const char *space, const char *name, pragma_handler handler)
1132 {
1133   c_register_pragma_1 (space, name, handler, false);
1134 }
1135
1136 void
1137 c_register_pragma_with_expansion (const char *space, const char *name,
1138                                   pragma_handler handler)
1139 {
1140   c_register_pragma_1 (space, name, handler, true);
1141 }
1142
1143 void
1144 c_invoke_pragma_handler (unsigned int id)
1145 {
1146   pragma_handler handler;
1147
1148   id -= PRAGMA_FIRST_EXTERNAL;
1149   handler = *VEC_index (pragma_handler, registered_pragmas, id);
1150
1151   handler (parse_in);
1152 }
1153
1154 /* Set up front-end pragmas.  */
1155 void
1156 init_pragma (void)
1157 {
1158   if (flag_openmp)
1159     {
1160       const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1161       int i;
1162
1163       for (i = 0; i < n_omp_pragmas; ++i)
1164         cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1165                                       omp_pragmas[i].id, true, true);
1166     }
1167
1168   if (!flag_preprocess_only)
1169     cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1170                                   PRAGMA_GCC_PCH_PREPROCESS, false, false);
1171
1172 #ifdef HANDLE_PRAGMA_PACK
1173 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1174   c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1175 #else
1176   c_register_pragma (0, "pack", handle_pragma_pack);
1177 #endif
1178 #endif
1179 #ifdef HANDLE_PRAGMA_WEAK
1180   c_register_pragma (0, "weak", handle_pragma_weak);
1181 #endif
1182 #ifdef HANDLE_PRAGMA_VISIBILITY
1183   c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1184 #endif
1185
1186   c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1187   c_register_pragma ("GCC", "target", handle_pragma_target);
1188   c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1189   c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1190   c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1191   c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1192
1193   c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname);
1194   c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
1195
1196   c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1197
1198 #ifdef REGISTER_TARGET_PRAGMAS
1199   REGISTER_TARGET_PRAGMAS ();
1200 #endif
1201 }
1202
1203 #include "gt-c-pragma.h"