OSDN Git Service

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