OSDN Git Service

ba3340765d4a03e63ce96c59a195d5704d0a0995
[pf3gnuchains/gcc-fork.git] / gcc / gengtype-yacc.y
1 /* -*- indented-text -*- */
2 /* Process source files and output type information.
3    Copyright (C) 2002 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 %{
23 #include "hconfig.h"
24 #include "system.h"
25 #include "gengtype.h"
26 #define YYERROR_VERBOSE
27 %}
28
29 %union {
30   type_p t;
31   pair_p p;
32   options_p o;
33   const char *s;
34 }
35
36 %token <t>ENT_TYPEDEF_STRUCT
37 %token <t>ENT_STRUCT
38 %token ENT_EXTERNSTATIC
39 %token ENT_YACCUNION
40 %token GTY_TOKEN "GTY"
41 %token UNION "union"
42 %token STRUCT "struct"
43 %token ENUM "enum"
44 %token ALIAS "ptr_alias"
45 %token PARAM_IS "param_is"
46 %token NUM
47 %token PERCENTPERCENT "%%"
48 %token <t>SCALAR
49 %token <s>ID
50 %token <s>STRING
51 %token <s>ARRAY
52 %token <s>PERCENT_ID
53 %token <s>CHAR
54
55 %type <p> struct_fields yacc_ids yacc_typematch
56 %type <t> type lasttype
57 %type <o> optionsopt options option optionseq optionseqopt
58 %type <s> type_option
59
60 %%
61
62 start: /* empty */
63        | typedef_struct start
64        | externstatic start
65        | yacc_union start
66        ;
67
68 typedef_struct: ENT_TYPEDEF_STRUCT options '{' struct_fields '}' ID
69                    {
70                      new_structure ($1->u.s.tag, UNION_P ($1), &lexer_line,
71                                     $4, $2);
72                      do_typedef ($6, $1, &lexer_line);
73                      lexer_toplevel_done = 1;
74                    }
75                  ';'
76                 | ENT_STRUCT options '{' struct_fields '}'
77                    {
78                      new_structure ($1->u.s.tag, UNION_P ($1), &lexer_line,
79                                     $4, $2);
80                      lexer_toplevel_done = 1;
81                    }
82                  ';'
83                 ;
84
85 externstatic: ENT_EXTERNSTATIC options lasttype ID semiequal
86                  {
87                    note_variable ($4, adjust_field_type ($3, $2), $2, 
88                                   &lexer_line);
89                  }
90               | ENT_EXTERNSTATIC options lasttype ID ARRAY semiequal
91                  {
92                    note_variable ($4, create_array ($3, $5),
93                             $2, &lexer_line);
94                  }
95               | ENT_EXTERNSTATIC options lasttype ID ARRAY ARRAY semiequal
96                  {
97                    note_variable ($4, create_array (create_array ($3, $6),
98                                               $5),
99                             $2, &lexer_line);
100                  }
101               ;
102
103 lasttype: type
104             { 
105               lexer_toplevel_done = 1;
106               $$ = $1;
107             }
108             ;
109
110 semiequal: ';'
111            | '='
112            ;
113
114 yacc_union: ENT_YACCUNION options struct_fields '}' yacc_typematch
115             PERCENTPERCENT
116               {
117                 note_yacc_type ($2, $3, $5, &lexer_line);
118               }
119             ;
120
121 yacc_typematch: /* empty */
122                    { $$ = NULL; }
123                 | yacc_typematch PERCENT_ID yacc_ids
124                    { 
125                      pair_p p;
126                      for (p = $3; p->next != NULL; p = p->next)
127                        {
128                          p->name = NULL;
129                          p->type = NULL;
130                        }
131                      p->name = NULL;
132                      p->type = NULL;
133                      p->next = $1;
134                      $$ = $3;
135                    }
136                 | yacc_typematch PERCENT_ID '<' ID '>' yacc_ids
137                    {
138                      pair_p p;
139                      type_p newtype = NULL;
140                      if (strcmp ($2, "type") == 0)
141                        newtype = (type_p) 1;
142                      for (p = $6; p->next != NULL; p = p->next)
143                        {
144                          p->name = $4;
145                          p->type = newtype;
146                        }
147                      p->name = $4;
148                      p->next = $1;
149                      p->type = newtype;
150                      $$ = $6;
151                    }
152                 ;
153
154 yacc_ids: /* empty */
155         { $$ = NULL; }
156      | yacc_ids ID
157         { 
158           pair_p p = xcalloc (1, sizeof (*p));
159           p->next = $1;
160           p->line = lexer_line;
161           p->opt = xmalloc (sizeof (*(p->opt)));
162           p->opt->name = "tag";
163           p->opt->next = NULL;
164           p->opt->info = (char *)$2;
165           $$ = p;
166         }
167      | yacc_ids CHAR
168         {
169           pair_p p = xcalloc (1, sizeof (*p));
170           p->next = $1;
171           p->line = lexer_line;
172           p->opt = xmalloc (sizeof (*(p->opt)));
173           p->opt->name = "tag";
174           p->opt->next = NULL;
175           p->opt->info = xmalloc (3 + strlen ($2));
176           sprintf (p->opt->info, "'%s'", $2);
177           $$ = p;
178         }
179      ;
180
181 struct_fields: { $$ = NULL; }
182                | type optionsopt ID bitfieldopt ';' struct_fields
183                   {
184                     pair_p p = xmalloc (sizeof (*p));
185                     p->type = adjust_field_type ($1, $2);
186                     p->opt = $2;
187                     p->name = $3;
188                     p->next = $6;
189                     p->line = lexer_line;
190                     $$ = p;
191                   }
192                | type optionsopt ID ARRAY ';' struct_fields
193                   {
194                     pair_p p = xmalloc (sizeof (*p));
195                     p->type = adjust_field_type (create_array ($1, $4), $2);
196                     p->opt = $2;
197                     p->name = $3;
198                     p->next = $6;
199                     p->line = lexer_line;
200                     $$ = p;
201                   }
202                | type optionsopt ID ARRAY ARRAY ';' struct_fields
203                   {
204                     pair_p p = xmalloc (sizeof (*p));
205                     p->type = create_array (create_array ($1, $5), $4);
206                     p->opt = $2;
207                     p->name = $3;
208                     p->next = $7;
209                     p->line = lexer_line;
210                     $$ = p;
211                   }
212                ;
213
214 bitfieldopt: /* empty */
215              | ':' NUM
216              ;
217
218 type: SCALAR
219          { $$ = $1; }
220       | ID
221          { $$ = resolve_typedef ($1, &lexer_line); }
222       | type '*'
223          { $$ = create_pointer ($1); }
224       | STRUCT ID '{' struct_fields '}'
225          {
226            new_structure ($2, 0, &lexer_line, $4, NULL);
227            $$ = find_structure ($2, 0);
228          }
229       | STRUCT ID
230          { $$ = find_structure ($2, 0); }
231       | UNION ID '{' struct_fields '}'
232          {
233            new_structure ($2, 1, &lexer_line, $4, NULL);
234            $$ = find_structure ($2, 1);
235          }
236       | UNION ID
237          { $$ = find_structure ($2, 1); }
238       | ENUM ID
239          { $$ = create_scalar_type ($2, strlen ($2)); }
240       | ENUM ID '{' enum_items '}'
241          { $$ = create_scalar_type ($2, strlen ($2)); }
242       ;
243
244 enum_items: /* empty */
245             | ID '=' NUM ',' enum_items
246               { }
247             | ID ',' enum_items
248               { }
249             | ID enum_items
250               { }
251             ;
252
253 optionsopt: { $$ = NULL; }
254             | options { $$ = $1; }
255             ;
256
257 options: GTY_TOKEN '(' '(' optionseqopt ')' ')'
258            { $$ = $4; }
259          ;
260
261 type_option : ALIAS
262                 { $$ = "ptr_alias"; }
263               | PARAM_IS
264                 { $$ = "param_is"; }
265               ;
266
267 option: type_option '(' type ')'
268            {
269              options_p o = xmalloc (sizeof (*o));
270              o->name = $1;
271              o->info = $3;
272              $$ = o;
273            }
274         | ID '(' STRING ')'
275            { 
276              options_p o = xmalloc (sizeof (*o));
277              o->name = $1;
278              o->info = (void *)$3;
279              $$ = o;
280            }
281         ;
282
283 optionseq: option
284               {
285                 $1->next = NULL;
286                 $$ = $1;
287               }
288             | optionseq ',' option
289               {
290                 $3->next = $1;
291                 $$ = $3;
292               }
293             ;
294
295 optionseqopt: { $$ = NULL }
296               | optionseq { $$ = $1; }
297               ;
298 %%