OSDN Git Service

Merge from pch-branch up to tag pch-commit-20020603.
[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 typedef_struct: ENT_TYPEDEF_STRUCT options '{' struct_fields '}' ID
68                    {
69                      new_structure ($1->u.s.tag, UNION_P ($1), &lexer_line,
70                                     $4, $2);
71                      do_typedef ($6, $1, &lexer_line);
72                      lexer_toplevel_done = 1;
73                    }
74                  ';'
75                 | ENT_STRUCT options '{' struct_fields '}'
76                    {
77                      new_structure ($1->u.s.tag, UNION_P ($1), &lexer_line,
78                                     $4, $2);
79                      lexer_toplevel_done = 1;
80                    }
81                  ';'
82
83 externstatic: ENT_EXTERNSTATIC options lasttype ID semiequal
84                  {
85                    note_variable ($4, adjust_field_type ($3, $2), $2, 
86                                   &lexer_line);
87                  }
88               | ENT_EXTERNSTATIC options lasttype ID ARRAY semiequal
89                  {
90                    note_variable ($4, create_array ($3, $5),
91                             $2, &lexer_line);
92                  }
93               | ENT_EXTERNSTATIC options lasttype ID ARRAY ARRAY semiequal
94                  {
95                    note_variable ($4, create_array (create_array ($3, $6),
96                                               $5),
97                             $2, &lexer_line);
98                  }
99
100 lasttype: type
101             { 
102               lexer_toplevel_done = 1;
103               $$ = $1;
104             }
105
106 semiequal: ';'
107            | '='
108            ;
109
110 yacc_union: ENT_YACCUNION options struct_fields '}' yacc_typematch PERCENTPERCENT
111               {
112                 note_yacc_type ($2, $3, $5, &lexer_line);
113               }
114
115 yacc_typematch: /* empty */
116                    { $$ = NULL; }
117                 | yacc_typematch PERCENT_ID yacc_ids
118                    { 
119                      pair_p p;
120                      for (p = $3; p->next != NULL; p = p->next)
121                        {
122                          p->name = NULL;
123                          p->type = NULL;
124                        }
125                      p->name = NULL;
126                      p->type = NULL;
127                      p->next = $1;
128                      $$ = $3;
129                    }
130                 | yacc_typematch PERCENT_ID '<' ID '>' yacc_ids
131                    {
132                      pair_p p;
133                      type_p newtype = NULL;
134                      if (strcmp ($2, "type") == 0)
135                        newtype = (type_p) 1;
136                      for (p = $6; p->next != NULL; p = p->next)
137                        {
138                          p->name = $4;
139                          p->type = newtype;
140                        }
141                      p->name = $4;
142                      p->next = $1;
143                      p->type = newtype;
144                      $$ = $6;
145                    }
146                 ;
147
148 yacc_ids: /* empty */
149         { $$ = NULL; }
150      | yacc_ids ID
151         { 
152           pair_p p = xcalloc (1, sizeof (*p));
153           p->next = $1;
154           p->line = lexer_line;
155           p->opt = xmalloc (sizeof (*(p->opt)));
156           p->opt->name = "tag";
157           p->opt->next = NULL;
158           p->opt->info = (char *)$2;
159           $$ = p;
160         }
161      | yacc_ids CHAR
162         {
163           pair_p p = xcalloc (1, sizeof (*p));
164           p->next = $1;
165           p->line = lexer_line;
166           p->opt = xmalloc (sizeof (*(p->opt)));
167           p->opt->name = "tag";
168           p->opt->next = NULL;
169           p->opt->info = xmalloc (3 + strlen ($2));
170           sprintf (p->opt->info, "'%s'", $2);
171           $$ = p;
172         }
173
174 struct_fields: { $$ = NULL; }
175                | type optionsopt ID bitfieldopt ';' struct_fields
176                   {
177                     pair_p p = xmalloc (sizeof (*p));
178                     p->type = adjust_field_type ($1, $2);
179                     p->opt = $2;
180                     p->name = $3;
181                     p->next = $6;
182                     p->line = lexer_line;
183                     $$ = p;
184                   }
185                | type optionsopt ID ARRAY ';' struct_fields
186                   {
187                     pair_p p = xmalloc (sizeof (*p));
188                     p->type = adjust_field_type (create_array ($1, $4), $2);
189                     p->opt = $2;
190                     p->name = $3;
191                     p->next = $6;
192                     p->line = lexer_line;
193                     $$ = p;
194                   }
195                | type optionsopt ID ARRAY ARRAY ';' struct_fields
196                   {
197                     pair_p p = xmalloc (sizeof (*p));
198                     p->type = create_array (create_array ($1, $5), $4);
199                     p->opt = $2;
200                     p->name = $3;
201                     p->next = $7;
202                     p->line = lexer_line;
203                     $$ = p;
204                   }
205
206 bitfieldopt: /* empty */
207              | ':' NUM
208
209 type: SCALAR
210          { $$ = $1; }
211       | ID
212          { $$ = resolve_typedef ($1, &lexer_line); }
213       | type '*'
214          { $$ = create_pointer ($1); }
215       | STRUCT ID '{' struct_fields '}'
216          {
217            new_structure ($2, 0, &lexer_line, $4, NULL);
218            $$ = find_structure ($2, 0);
219          }
220       | STRUCT ID
221          { $$ = find_structure ($2, 0); }
222       | UNION ID '{' struct_fields '}'
223          {
224            new_structure ($2, 1, &lexer_line, $4, NULL);
225            $$ = find_structure ($2, 1);
226          }
227       | UNION ID
228          { $$ = find_structure ($2, 1); }
229       | ENUM ID
230          { $$ = create_scalar_type ($2, strlen ($2)); }
231       | ENUM ID '{' enum_items '}'
232          { $$ = create_scalar_type ($2, strlen ($2)); }
233
234 enum_items: /* empty */
235             | ID '=' NUM ',' enum_items
236               { }
237             | ID ',' enum_items
238               { }
239             | ID enum_items
240               { }
241             ;
242
243 optionsopt: { $$ = NULL; }
244             | options { $$ = $1; }
245
246 options: GTY_TOKEN '(' '(' optionseqopt ')' ')' { $$ = $4; }
247
248 type_option : ALIAS
249                 { $$ = "ptr_alias"; }
250               | PARAM_IS
251                 { $$ = "param_is"; }
252
253 option: type_option '(' type ')'
254            {
255              options_p o = xmalloc (sizeof (*o));
256              o->name = $1;
257              o->info = $3;
258              $$ = o;
259            }
260         | ID '(' STRING ')'
261            { 
262              options_p o = xmalloc (sizeof (*o));
263              o->name = $1;
264              o->info = (void *)$3;
265              $$ = o;
266            }
267
268 optionseq: option
269               {
270                 $1->next = NULL;
271                 $$ = $1;
272               }
273             | optionseq ',' option
274               {
275                 $3->next = $1;
276                 $$ = $3;
277               }
278
279 optionseqopt: { $$ = NULL }
280               | optionseq { $$ = $1; }
281
282 %%