OSDN Git Service

2006-01-18 Richard Henderson <rth@redhat.com>
[pf3gnuchains/gcc-fork.git] / 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    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 2, 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 COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "function.h"
29 #include "cpplib.h"
30 #include "c-pragma.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "ggc.h"
34 #include "c-common.h"
35 #include "output.h"
36 #include "tm_p.h"
37 #include "vec.h"
38 #include "target.h"
39
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       align = TREE_INT_CST_LOW (x);
163       action = set;
164       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
165         GCC_BAD ("malformed %<#pragma pack%> - ignored");
166     }
167   else if (token == CPP_NAME)
168     {
169 #define GCC_BAD_ACTION do { if (action != pop) \
170           GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
171         else \
172           GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
173         } while (0)
174
175       const char *op = IDENTIFIER_POINTER (x);
176       if (!strcmp (op, "push"))
177         action = push;
178       else if (!strcmp (op, "pop"))
179         action = pop;
180       else
181         GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op);
182
183       while ((token = pragma_lex (&x)) == CPP_COMMA)
184         {
185           token = pragma_lex (&x);
186           if (token == CPP_NAME && id == 0)
187             {
188               id = x;
189             }
190           else if (token == CPP_NUMBER && action == push && align == -1)
191             {
192               align = TREE_INT_CST_LOW (x);
193               if (align == -1)
194                 action = set;
195             }
196           else
197             GCC_BAD_ACTION;
198         }
199
200       if (token != CPP_CLOSE_PAREN)
201         GCC_BAD_ACTION;
202 #undef GCC_BAD_ACTION
203     }
204   else
205     GCC_BAD ("malformed %<#pragma pack%> - ignored");
206
207   if (pragma_lex (&x) != CPP_EOF)
208     warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
209
210   if (flag_pack_struct)
211     GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
212
213   if (action != pop)
214     switch (align)
215       {
216       case 0:
217       case 1:
218       case 2:
219       case 4:
220       case 8:
221       case 16:
222         align *= BITS_PER_UNIT;
223         break;
224       case -1:
225         if (action == push)
226           {
227             align = maximum_field_alignment;
228             break;
229           }
230       default:
231         GCC_BAD2 ("alignment must be a small power of two, not %d", align);
232       }
233
234   switch (action)
235     {
236     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
237     case push:  push_alignment (align, id);    break;
238     case pop:   pop_alignment (id);            break;
239     }
240 }
241 #endif  /* HANDLE_PRAGMA_PACK */
242
243 static GTY(()) tree pending_weaks;
244
245 #ifdef HANDLE_PRAGMA_WEAK
246 static void apply_pragma_weak (tree, tree);
247 static void handle_pragma_weak (cpp_reader *);
248
249 static void
250 apply_pragma_weak (tree decl, tree value)
251 {
252   if (value)
253     {
254       value = build_string (IDENTIFIER_LENGTH (value),
255                             IDENTIFIER_POINTER (value));
256       decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
257                                                build_tree_list (NULL, value)),
258                        0);
259     }
260
261   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
262       && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma.  */
263       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
264     warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
265              "results in unspecified behavior", decl);
266
267   declare_weak (decl);
268 }
269
270 void
271 maybe_apply_pragma_weak (tree decl)
272 {
273   tree *p, t, id;
274
275   /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed.  */
276
277   /* No weak symbols pending, take the short-cut.  */
278   if (!pending_weaks)
279     return;
280   /* If it's not visible outside this file, it doesn't matter whether
281      it's weak.  */
282   if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
283     return;
284   /* If it's not a function or a variable, it can't be weak.
285      FIXME: what kinds of things are visible outside this file but
286      aren't functions or variables?   Should this be an assert instead?  */
287   if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
288     return;
289
290   id = DECL_ASSEMBLER_NAME (decl);
291
292   for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
293     if (id == TREE_PURPOSE (t))
294       {
295         apply_pragma_weak (decl, TREE_VALUE (t));
296         *p = TREE_CHAIN (t);
297         break;
298       }
299 }
300
301 /* Process all "#pragma weak A = B" directives where we have not seen
302    a decl for A.  */
303 void
304 maybe_apply_pending_pragma_weaks (void)
305 {
306   tree *p, t, alias_id, id, decl, *next;
307
308   for (p = &pending_weaks; (t = *p) ; p = next)
309     {
310       next = &TREE_CHAIN (t);
311       alias_id = TREE_PURPOSE (t);
312       id = TREE_VALUE (t);
313
314       if (TREE_VALUE (t) == NULL)
315         continue;
316
317       decl = build_decl (FUNCTION_DECL, alias_id, default_function_type);
318
319       DECL_ARTIFICIAL (decl) = 1;
320       TREE_PUBLIC (decl) = 1;
321       DECL_EXTERNAL (decl) = 1;
322       DECL_WEAK (decl) = 1;
323
324       assemble_alias (decl, id);
325     }
326 }
327
328 /* #pragma weak name [= value] */
329 static void
330 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
331 {
332   tree name, value, x, decl;
333   enum cpp_ttype t;
334
335   value = 0;
336
337   if (pragma_lex (&name) != CPP_NAME)
338     GCC_BAD ("malformed #pragma weak, ignored");
339   t = pragma_lex (&x);
340   if (t == CPP_EQ)
341     {
342       if (pragma_lex (&value) != CPP_NAME)
343         GCC_BAD ("malformed #pragma weak, ignored");
344       t = pragma_lex (&x);
345     }
346   if (t != CPP_EOF)
347     warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
348
349   decl = identifier_global_value (name);
350   if (decl && DECL_P (decl))
351     {
352       apply_pragma_weak (decl, value);
353       if (value)
354         assemble_alias (decl, value);
355     }
356   else
357     pending_weaks = tree_cons (name, value, pending_weaks);
358 }
359 #else
360 void
361 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
362 {
363 }
364
365 void
366 maybe_apply_pending_pragma_weaks (void)
367 {
368 }
369 #endif /* HANDLE_PRAGMA_WEAK */
370
371 /* GCC supports two #pragma directives for renaming the external
372    symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
373    compatibility with the Solaris and Tru64 system headers.  GCC also
374    has its own notation for this, __asm__("name") annotations.
375
376    Corner cases of these features and their interaction:
377
378    1) Both pragmas silently apply only to declarations with external
379       linkage (that is, TREE_PUBLIC || DECL_EXTERNAL).  Asm labels
380       do not have this restriction.
381
382    2) In C++, both #pragmas silently apply only to extern "C" declarations.
383       Asm labels do not have this restriction.
384
385    3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
386       applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
387       new name is different, a warning issues and the name does not change.
388
389    4) The "source name" for #pragma redefine_extname is the DECL_NAME,
390       *not* the DECL_ASSEMBLER_NAME.
391
392    5) If #pragma extern_prefix is in effect and a declaration occurs
393       with an __asm__ name, the #pragma extern_prefix is silently
394       ignored for that declaration.
395
396    6) If #pragma extern_prefix and #pragma redefine_extname apply to
397       the same declaration, whichever triggered first wins, and a warning
398       is issued.  (We would like to have #pragma redefine_extname always
399       win, but it can appear either before or after the declaration, and
400       if it appears afterward, we have no way of knowing whether a modified
401       DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.)  */
402
403 static GTY(()) tree pending_redefine_extname;
404
405 static void handle_pragma_redefine_extname (cpp_reader *);
406
407 /* #pragma redefine_extname oldname newname */
408 static void
409 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
410 {
411   tree oldname, newname, decl, x;
412   enum cpp_ttype t;
413
414   if (pragma_lex (&oldname) != CPP_NAME)
415     GCC_BAD ("malformed #pragma redefine_extname, ignored");
416   if (pragma_lex (&newname) != CPP_NAME)
417     GCC_BAD ("malformed #pragma redefine_extname, ignored");
418   t = pragma_lex (&x);
419   if (t != CPP_EOF)
420     warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
421
422   if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
423     {
424       if (warn_unknown_pragmas > in_system_header)
425         warning (OPT_Wunknown_pragmas,
426                  "#pragma redefine_extname not supported on this target");
427       return;
428     }
429
430   decl = identifier_global_value (oldname);
431   if (decl
432       && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
433       && (TREE_CODE (decl) == FUNCTION_DECL
434           || TREE_CODE (decl) == VAR_DECL)
435       && has_c_linkage (decl))
436     {
437       if (DECL_ASSEMBLER_NAME_SET_P (decl))
438         {
439           const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
440           name = targetm.strip_name_encoding (name);
441
442           if (strcmp (name, IDENTIFIER_POINTER (newname)))
443             warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
444                      "conflict with previous rename");
445         }
446       else
447         change_decl_assembler_name (decl, newname);
448     }
449   else
450     /* We have to add this to the rename list even if there's already
451        a global value that doesn't meet the above criteria, because in
452        C++ "struct foo {...};" puts "foo" in the current namespace but
453        does *not* conflict with a subsequent declaration of a function
454        or variable foo.  See g++.dg/other/pragma-re-2.C.  */
455     add_to_renaming_pragma_list (oldname, newname);
456 }
457
458 /* This is called from here and from ia64.c.  */
459 void
460 add_to_renaming_pragma_list (tree oldname, tree newname)
461 {
462   tree previous = purpose_member (oldname, pending_redefine_extname);
463   if (previous)
464     {
465       if (TREE_VALUE (previous) != newname)
466         warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
467                  "conflict with previous #pragma redefine_extname");
468       return;
469     }
470   
471   pending_redefine_extname
472     = tree_cons (oldname, newname, pending_redefine_extname);
473 }
474
475 static GTY(()) tree pragma_extern_prefix;
476
477 /* #pragma extern_prefix "prefix" */
478 static void
479 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
480 {
481   tree prefix, x;
482   enum cpp_ttype t;
483
484   if (pragma_lex (&prefix) != CPP_STRING)
485     GCC_BAD ("malformed #pragma extern_prefix, ignored");
486   t = pragma_lex (&x);
487   if (t != CPP_EOF)
488     warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
489
490   if (targetm.handle_pragma_extern_prefix)
491     /* Note that the length includes the null terminator.  */
492     pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
493   else if (warn_unknown_pragmas > in_system_header)
494     warning (OPT_Wunknown_pragmas,
495              "#pragma extern_prefix not supported on this target");
496 }
497
498 /* Hook from the front ends to apply the results of one of the preceding
499    pragmas that rename variables.  */
500
501 tree
502 maybe_apply_renaming_pragma (tree decl, tree asmname)
503 {
504   tree *p, t;
505
506   /* The renaming pragmas are only applied to declarations with
507      external linkage.  */
508   if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
509       || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
510       || !has_c_linkage (decl))
511     return asmname;
512
513   /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
514      but we may warn about a rename that conflicts.  */
515   if (DECL_ASSEMBLER_NAME_SET_P (decl))
516     {
517       const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
518       oldname = targetm.strip_name_encoding (oldname);
519
520       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
521           warning (OPT_Wpragmas, "asm declaration ignored due to "
522                    "conflict with previous rename");
523
524       /* Take any pending redefine_extname off the list.  */
525       for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
526         if (DECL_NAME (decl) == TREE_PURPOSE (t))
527           {
528             /* Only warn if there is a conflict.  */
529             if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
530               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
531                        "conflict with previous rename");
532
533             *p = TREE_CHAIN (t);
534             break;
535           }
536       return 0;
537     }
538
539   /* Find out if we have a pending #pragma redefine_extname.  */
540   for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
541     if (DECL_NAME (decl) == TREE_PURPOSE (t))
542       {
543         tree newname = TREE_VALUE (t);
544         *p = TREE_CHAIN (t);
545
546         /* If we already have an asmname, #pragma redefine_extname is
547            ignored (with a warning if it conflicts).  */
548         if (asmname)
549           {
550             if (strcmp (TREE_STRING_POINTER (asmname),
551                         IDENTIFIER_POINTER (newname)) != 0)
552               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
553                        "conflict with __asm__ declaration");
554             return asmname;
555           }
556
557         /* Otherwise we use what we've got; #pragma extern_prefix is
558            silently ignored.  */
559         return build_string (IDENTIFIER_LENGTH (newname),
560                              IDENTIFIER_POINTER (newname));
561       }
562
563   /* If we've got an asmname, #pragma extern_prefix is silently ignored.  */
564   if (asmname)
565     return asmname;
566
567   /* If #pragma extern_prefix is in effect, apply it.  */
568   if (pragma_extern_prefix)
569     {
570       const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
571       size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
572
573       const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
574       size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
575         
576       char *newname = (char *) alloca (plen + ilen + 1);
577
578       memcpy (newname,        prefix, plen);
579       memcpy (newname + plen, id, ilen + 1);
580
581       return build_string (plen + ilen, newname);
582     }
583
584   /* Nada.  */
585   return 0;
586 }
587
588
589 #ifdef HANDLE_PRAGMA_VISIBILITY
590 static void handle_pragma_visibility (cpp_reader *);
591
592 typedef enum symbol_visibility visibility;
593 DEF_VEC_I (visibility);
594 DEF_VEC_ALLOC_I (visibility, heap);
595
596 /* Sets the default visibility for symbols to something other than that
597    specified on the command line.  */
598 static void
599 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
600 {
601   /* Form is #pragma GCC visibility push(hidden)|pop */
602   tree x;
603   enum cpp_ttype token;
604   enum { bad, push, pop } action = bad;
605   static VEC (visibility, heap) *visstack;
606  
607   token = pragma_lex (&x);
608   if (token == CPP_NAME)
609     {
610       const char *op = IDENTIFIER_POINTER (x);
611       if (!strcmp (op, "push"))
612         action = push;
613       else if (!strcmp (op, "pop"))
614         action = pop;
615     }
616   if (bad == action)
617     GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
618   else
619     {
620       if (pop == action)
621         {
622           if (!VEC_length (visibility, visstack))
623             {
624               GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
625             }
626           else
627             {
628               default_visibility = VEC_pop (visibility, visstack);
629               visibility_options.inpragma
630                 = VEC_length (visibility, visstack) != 0;
631             }
632         }
633       else
634         {
635           if (pragma_lex (&x) != CPP_OPEN_PAREN)
636             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
637           token = pragma_lex (&x);
638           if (token != CPP_NAME)
639             {
640               GCC_BAD ("malformed #pragma GCC visibility push");
641             }
642           else
643             {
644               const char *str = IDENTIFIER_POINTER (x);
645               VEC_safe_push (visibility, heap, visstack,
646                              default_visibility);
647               if (!strcmp (str, "default"))
648                 default_visibility = VISIBILITY_DEFAULT;
649               else if (!strcmp (str, "internal"))
650                 default_visibility = VISIBILITY_INTERNAL;
651               else if (!strcmp (str, "hidden"))
652                 default_visibility = VISIBILITY_HIDDEN;  
653               else if (!strcmp (str, "protected"))
654                 default_visibility = VISIBILITY_PROTECTED;
655               else
656                 {
657                   GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
658                 }
659               visibility_options.inpragma = 1;
660             }
661           if (pragma_lex (&x) != CPP_CLOSE_PAREN)
662             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
663         }
664     }
665   if (pragma_lex (&x) != CPP_EOF)
666     warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
667 }
668
669 #endif
670
671 /* A vector of registered pragma callbacks.  */
672
673 DEF_VEC_O (pragma_handler);
674 DEF_VEC_ALLOC_O (pragma_handler, heap);
675
676 static VEC(pragma_handler, heap) *registered_pragmas;
677
678 /* Front-end wrappers for pragma registration to avoid dragging
679    cpplib.h in almost everywhere.  */
680
681 static void
682 c_register_pragma_1 (const char *space, const char *name,
683                      pragma_handler handler, bool allow_expansion)
684 {
685   unsigned id;
686
687   VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
688   id = VEC_length (pragma_handler, registered_pragmas);
689   id += PRAGMA_FIRST_EXTERNAL - 1;
690
691   /* The C++ front end allocates 6 bits in cp_token; the C front end
692      allocates 7 bits in c_token.  At present this is sufficient.  */
693   gcc_assert (id < 64);
694
695   cpp_register_deferred_pragma (parse_in, space, name, id,
696                                 allow_expansion, false);
697 }
698
699 void
700 c_register_pragma (const char *space, const char *name, pragma_handler handler)
701 {
702   c_register_pragma_1 (space, name, handler, false);
703 }
704
705 void
706 c_register_pragma_with_expansion (const char *space, const char *name,
707                                   pragma_handler handler)
708 {
709   c_register_pragma_1 (space, name, handler, true);
710 }
711
712 void
713 c_invoke_pragma_handler (unsigned int id)
714 {
715   pragma_handler handler;
716
717   id -= PRAGMA_FIRST_EXTERNAL;
718   handler = *VEC_index (pragma_handler, registered_pragmas, id);
719
720   handler (parse_in);
721 }
722
723 /* Set up front-end pragmas.  */
724 void
725 init_pragma (void)
726 {
727   if (flag_openmp && !flag_preprocess_only)
728     {
729       struct omp_pragma_def { const char *name; unsigned int id; };
730       static const struct omp_pragma_def omp_pragmas[] = {
731         { "atomic", PRAGMA_OMP_ATOMIC },
732         { "barrier", PRAGMA_OMP_BARRIER },
733         { "critical", PRAGMA_OMP_CRITICAL },
734         { "flush", PRAGMA_OMP_FLUSH },
735         { "for", PRAGMA_OMP_FOR },
736         { "master", PRAGMA_OMP_MASTER },
737         { "ordered", PRAGMA_OMP_ORDERED },
738         { "parallel", PRAGMA_OMP_PARALLEL },
739         { "section", PRAGMA_OMP_SECTION },
740         { "sections", PRAGMA_OMP_SECTIONS },
741         { "single", PRAGMA_OMP_SINGLE },
742         { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
743       };
744
745       const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
746       int i;
747
748       for (i = 0; i < n_omp_pragmas; ++i)
749         cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
750                                       omp_pragmas[i].id, true, true);
751     }
752
753   cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
754                                 PRAGMA_GCC_PCH_PREPROCESS, false, false);
755
756 #ifdef HANDLE_PRAGMA_PACK
757 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
758   c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
759 #else
760   c_register_pragma (0, "pack", handle_pragma_pack);
761 #endif
762 #endif
763 #ifdef HANDLE_PRAGMA_WEAK
764   c_register_pragma (0, "weak", handle_pragma_weak);
765 #endif
766 #ifdef HANDLE_PRAGMA_VISIBILITY
767   c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
768 #endif
769
770   c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
771   c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
772
773 #ifdef REGISTER_TARGET_PRAGMAS
774   REGISTER_TARGET_PRAGMAS ();
775 #endif
776 }
777
778 #include "gt-c-pragma.h"