OSDN Git Service

PR c++/31074
[pf3gnuchains/gcc-fork.git] / gcc / gengtype-yacc.y
1 /* -*- indented-text -*- */
2 /* Process source files and output type information.
3    Copyright (C) 2002, 2004 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 %{
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "gengtype.h"
28 #define YYERROR_VERBOSE
29 %}
30
31 %union {
32   type_p t;
33   pair_p p;
34   options_p o;
35   const char *s;
36 }
37
38 %token <s>ENT_TYPEDEF_STRUCT
39 %token <s>ENT_STRUCT
40 %token <s>ENT_TYPEDEF_UNION
41 %token <s>ENT_UNION
42 %token ENT_EXTERNSTATIC
43 %token GTY_TOKEN
44 %token VEC_TOKEN
45 %token UNION
46 %token STRUCT
47 %token ENUM
48 %token ALIAS
49 %token NESTED_PTR
50 %token <s>PARAM_IS
51 %token NUM
52 %token <s>SCALAR
53 %token <s>ID
54 %token <s>STRING
55 %token <s>ARRAY
56 %token <s>CHAR
57
58 %type <p> struct_fields
59 %type <t> type lasttype
60 %type <o> optionsopt options optionseq
61 %type <s> type_option stringseq
62
63 %%
64
65 start: /* empty */
66        | typedef_struct start
67        | externstatic start
68        | start
69        ;
70
71 typedef_struct: ENT_TYPEDEF_STRUCT options '{' struct_fields '}' ID
72                    {
73                      type_p t = new_structure ($1, false, &lexer_line, $4, $2);
74                      do_typedef ($6, t, &lexer_line);
75                      lexer_toplevel_done = 1;
76                    }
77                   ';'
78                 | ENT_TYPEDEF_UNION options '{' struct_fields '}' ID
79                    {
80                      type_p t = new_structure ($1, true, &lexer_line, $4, $2);
81                      do_typedef ($6, t, &lexer_line);
82                      lexer_toplevel_done = 1;
83                    }
84                   ';'
85                 | ENT_STRUCT options '{' struct_fields '}'
86                    {
87                      new_structure ($1, false, &lexer_line, $4, $2);
88                      lexer_toplevel_done = 1;
89                    }
90                   ';'
91                 | ENT_UNION options '{' struct_fields '}'
92                    {
93                      new_structure ($1, true, &lexer_line, $4, $2);
94                      lexer_toplevel_done = 1;
95                    }
96                   ';'
97                 ;
98
99 externstatic: ENT_EXTERNSTATIC options lasttype ID semiequal
100                  {
101                    note_variable ($4, adjust_field_type ($3, $2), $2,
102                                   &lexer_line);
103                  }
104               | ENT_EXTERNSTATIC options lasttype ID ARRAY semiequal
105                  {
106                    note_variable ($4, create_array ($3, $5),
107                             $2, &lexer_line);
108                  }
109               | ENT_EXTERNSTATIC options lasttype ID ARRAY ARRAY semiequal
110                  {
111                    note_variable ($4, create_array (create_array ($3, $6),
112                                               $5),
113                             $2, &lexer_line);
114                  }
115               ;
116
117 lasttype: type
118             {
119               lexer_toplevel_done = 1;
120               $$ = $1;
121             }
122             ;
123
124 semiequal: ';'
125            | '='
126            ;
127
128 struct_fields: { $$ = NULL; }
129                | type optionsopt ID bitfieldopt ';' struct_fields
130                   {
131                     $$ = create_field_at ($6, $1, $3, $2, &lexer_line);
132                   }
133                | type optionsopt ID ARRAY ';' struct_fields
134                   {
135                     $$ = create_field_at ($6, create_array ($1, $4),
136                                           $3, $2, &lexer_line);
137                   }
138                | type optionsopt ID ARRAY ARRAY ';' struct_fields
139                   {
140                     type_p arr = create_array (create_array ($1, $5), $4);
141                     $$ = create_field_at ($7, arr, $3, $2, &lexer_line);
142                   }
143                | type ':' bitfieldlen ';' struct_fields
144                   { $$ = $5; }
145                ;
146
147 bitfieldopt: /* empty */
148              | ':' bitfieldlen
149              ;
150
151 bitfieldlen: NUM | ID
152                 { }
153              ;
154
155 type: SCALAR
156          { $$ = create_scalar_type ($1); }
157       | ID
158          { $$ = resolve_typedef ($1, &lexer_line); }
159       | VEC_TOKEN '(' ID ',' ID ')'
160          { $$ = resolve_typedef (concat ("VEC_", $3, "_", $5, (char *)0),
161                                  &lexer_line); }
162       | type '*'
163          { $$ = create_pointer ($1); }
164       | STRUCT ID '{' struct_fields '}'
165          { $$ = new_structure ($2, 0, &lexer_line, $4, NULL); }
166       | STRUCT ID
167          { $$ = find_structure ($2, 0); }
168       | UNION ID '{' struct_fields '}'
169          { $$ = new_structure ($2, 1, &lexer_line, $4, NULL); }
170       | UNION ID
171          { $$ = find_structure ($2, 1); }
172       | ENUM ID
173          { $$ = create_scalar_type ($2); }
174       | ENUM ID '{' enum_items '}'
175          { $$ = create_scalar_type ($2); }
176       ;
177
178 enum_items: /* empty */
179             | ID '=' NUM ',' enum_items
180               { }
181             | ID ',' enum_items
182               { }
183             | ID enum_items
184               { }
185             ;
186
187 optionsopt: { $$ = NULL; }
188             | options { $$ = $1; }
189             ;
190
191 options: GTY_TOKEN '(' '(' optionseq ')' ')'
192            { $$ = $4; }
193          ;
194
195 type_option : ALIAS
196                 { $$ = "ptr_alias"; }
197               | PARAM_IS
198                 { $$ = $1; }
199               ;
200
201 optionseq: { $$ = NULL; }
202         | optionseq commaopt ID
203            { $$ = create_option ($1, $3, (void *)""); }
204         | optionseq commaopt ID '(' stringseq ')'
205            { $$ = create_option ($1, $3, (void *)$5); }
206         | optionseq commaopt type_option '(' type ')'
207            { $$ = create_option ($1, $3, adjust_field_type ($5, 0)); }
208         | optionseq commaopt NESTED_PTR '(' type ',' stringseq ',' stringseq ')'
209            { $$ = create_nested_ptr_option ($1, $5, $7, $9); }
210
211 commaopt: /* nothing */
212           | ','
213           ;
214
215 stringseq: STRING
216              { $$ = $1; }
217            | stringseq STRING
218              {
219                size_t l1 = strlen ($1);
220                size_t l2 = strlen ($2);
221                char *s = XRESIZEVEC (char, $1, l1 + l2 + 1);
222                memcpy (s + l1, $2, l2 + 1);
223                XDELETE ($2);
224                $$ = s;
225              }
226            ;
227 %%