1*38fd1498Szrj /* Process source files and output type information.
2*38fd1498Szrj Copyright (C) 2006-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj #ifdef HOST_GENERATOR_FILE
21*38fd1498Szrj #include "config.h"
22*38fd1498Szrj #define GENERATOR_FILE 1
23*38fd1498Szrj #else
24*38fd1498Szrj #include "bconfig.h"
25*38fd1498Szrj #endif
26*38fd1498Szrj #include "system.h"
27*38fd1498Szrj #include "gengtype.h"
28*38fd1498Szrj
29*38fd1498Szrj /* This is a simple recursive-descent parser which understands a subset of
30*38fd1498Szrj the C type grammar.
31*38fd1498Szrj
32*38fd1498Szrj Rule functions are suffixed _seq if they scan a sequence of items;
33*38fd1498Szrj _opt if they may consume zero tokens; _seqopt if both are true. The
34*38fd1498Szrj "consume_" prefix indicates that a sequence of tokens is parsed for
35*38fd1498Szrj syntactic correctness and then thrown away. */
36*38fd1498Szrj
37*38fd1498Szrj /* Simple one-token lookahead mechanism. */
38*38fd1498Szrj
39*38fd1498Szrj struct token
40*38fd1498Szrj {
41*38fd1498Szrj const char *value;
42*38fd1498Szrj int code;
43*38fd1498Szrj bool valid;
44*38fd1498Szrj };
45*38fd1498Szrj static struct token T;
46*38fd1498Szrj
47*38fd1498Szrj /* Retrieve the code of the current token; if there is no current token,
48*38fd1498Szrj get the next one from the lexer. */
49*38fd1498Szrj static inline int
token(void)50*38fd1498Szrj token (void)
51*38fd1498Szrj {
52*38fd1498Szrj if (!T.valid)
53*38fd1498Szrj {
54*38fd1498Szrj T.code = yylex (&T.value);
55*38fd1498Szrj T.valid = true;
56*38fd1498Szrj }
57*38fd1498Szrj return T.code;
58*38fd1498Szrj }
59*38fd1498Szrj
60*38fd1498Szrj /* Retrieve the value of the current token (if any) and mark it consumed.
61*38fd1498Szrj The next call to token() will get another token from the lexer. */
62*38fd1498Szrj static inline const char *
advance(void)63*38fd1498Szrj advance (void)
64*38fd1498Szrj {
65*38fd1498Szrj T.valid = false;
66*38fd1498Szrj return T.value;
67*38fd1498Szrj }
68*38fd1498Szrj
69*38fd1498Szrj /* Diagnostics. */
70*38fd1498Szrj
71*38fd1498Szrj /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
72*38fd1498Szrj static const char *const token_names[] = {
73*38fd1498Szrj "GTY",
74*38fd1498Szrj "typedef",
75*38fd1498Szrj "extern",
76*38fd1498Szrj "static",
77*38fd1498Szrj "union",
78*38fd1498Szrj "struct",
79*38fd1498Szrj "enum",
80*38fd1498Szrj "...",
81*38fd1498Szrj "ptr_alias",
82*38fd1498Szrj "nested_ptr",
83*38fd1498Szrj "a param<N>_is option",
84*38fd1498Szrj "a number",
85*38fd1498Szrj "a scalar type",
86*38fd1498Szrj "an identifier",
87*38fd1498Szrj "a string constant",
88*38fd1498Szrj "a character constant",
89*38fd1498Szrj "an array declarator",
90*38fd1498Szrj "a C++ keyword to ignore"
91*38fd1498Szrj };
92*38fd1498Szrj
93*38fd1498Szrj /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
94*38fd1498Szrj static const char *const token_value_format[] = {
95*38fd1498Szrj "%s",
96*38fd1498Szrj "'%s'",
97*38fd1498Szrj "'%s'",
98*38fd1498Szrj "'%s'",
99*38fd1498Szrj "'\"%s\"'",
100*38fd1498Szrj "\"'%s'\"",
101*38fd1498Szrj "'[%s]'",
102*38fd1498Szrj "'%s'",
103*38fd1498Szrj };
104*38fd1498Szrj
105*38fd1498Szrj /* Produce a printable representation for a token defined by CODE and
106*38fd1498Szrj VALUE. This sometimes returns pointers into malloc memory and
107*38fd1498Szrj sometimes not, therefore it is unsafe to free the pointer it
108*38fd1498Szrj returns, so that memory is leaked. This does not matter, as this
109*38fd1498Szrj function is only used for diagnostics, and in a successful run of
110*38fd1498Szrj the program there will be none. */
111*38fd1498Szrj static const char *
print_token(int code,const char * value)112*38fd1498Szrj print_token (int code, const char *value)
113*38fd1498Szrj {
114*38fd1498Szrj if (code < CHAR_TOKEN_OFFSET)
115*38fd1498Szrj return xasprintf ("'%c'", code);
116*38fd1498Szrj else if (code < FIRST_TOKEN_WITH_VALUE)
117*38fd1498Szrj return xasprintf ("'%s'", token_names[code - CHAR_TOKEN_OFFSET]);
118*38fd1498Szrj else if (!value)
119*38fd1498Szrj return token_names[code - CHAR_TOKEN_OFFSET]; /* don't quote these */
120*38fd1498Szrj else
121*38fd1498Szrj return xasprintf (token_value_format[code - FIRST_TOKEN_WITH_VALUE],
122*38fd1498Szrj value);
123*38fd1498Szrj }
124*38fd1498Szrj
125*38fd1498Szrj /* Convenience wrapper around print_token which produces the printable
126*38fd1498Szrj representation of the current token. */
127*38fd1498Szrj static inline const char *
print_cur_token(void)128*38fd1498Szrj print_cur_token (void)
129*38fd1498Szrj {
130*38fd1498Szrj return print_token (T.code, T.value);
131*38fd1498Szrj }
132*38fd1498Szrj
133*38fd1498Szrj /* Report a parse error on the current line, with diagnostic MSG.
134*38fd1498Szrj Behaves as standard printf with respect to additional arguments and
135*38fd1498Szrj format escapes. */
136*38fd1498Szrj static void ATTRIBUTE_PRINTF_1
parse_error(const char * msg,...)137*38fd1498Szrj parse_error (const char *msg, ...)
138*38fd1498Szrj {
139*38fd1498Szrj va_list ap;
140*38fd1498Szrj
141*38fd1498Szrj fprintf (stderr, "%s:%d: parse error: ",
142*38fd1498Szrj get_input_file_name (lexer_line.file), lexer_line.line);
143*38fd1498Szrj
144*38fd1498Szrj va_start (ap, msg);
145*38fd1498Szrj vfprintf (stderr, msg, ap);
146*38fd1498Szrj va_end (ap);
147*38fd1498Szrj
148*38fd1498Szrj fputc ('\n', stderr);
149*38fd1498Szrj
150*38fd1498Szrj hit_error = true;
151*38fd1498Szrj }
152*38fd1498Szrj
153*38fd1498Szrj /* If the next token does not have code T, report a parse error; otherwise
154*38fd1498Szrj return the token's value. */
155*38fd1498Szrj static const char *
require(int t)156*38fd1498Szrj require (int t)
157*38fd1498Szrj {
158*38fd1498Szrj int u = token ();
159*38fd1498Szrj const char *v = advance ();
160*38fd1498Szrj if (u != t)
161*38fd1498Szrj {
162*38fd1498Szrj parse_error ("expected %s, have %s",
163*38fd1498Szrj print_token (t, 0), print_token (u, v));
164*38fd1498Szrj return 0;
165*38fd1498Szrj }
166*38fd1498Szrj return v;
167*38fd1498Szrj }
168*38fd1498Szrj
169*38fd1498Szrj /* As per require, but do not advance. */
170*38fd1498Szrj static const char *
require_without_advance(int t)171*38fd1498Szrj require_without_advance (int t)
172*38fd1498Szrj {
173*38fd1498Szrj int u = token ();
174*38fd1498Szrj const char *v = T.value;
175*38fd1498Szrj if (u != t)
176*38fd1498Szrj {
177*38fd1498Szrj parse_error ("expected %s, have %s",
178*38fd1498Szrj print_token (t, 0), print_token (u, v));
179*38fd1498Szrj return 0;
180*38fd1498Szrj }
181*38fd1498Szrj return v;
182*38fd1498Szrj }
183*38fd1498Szrj
184*38fd1498Szrj /* If the next token does not have one of the codes T1 or T2, report a
185*38fd1498Szrj parse error; otherwise return the token's value. */
186*38fd1498Szrj static const char *
require2(int t1,int t2)187*38fd1498Szrj require2 (int t1, int t2)
188*38fd1498Szrj {
189*38fd1498Szrj int u = token ();
190*38fd1498Szrj const char *v = advance ();
191*38fd1498Szrj if (u != t1 && u != t2)
192*38fd1498Szrj {
193*38fd1498Szrj parse_error ("expected %s or %s, have %s",
194*38fd1498Szrj print_token (t1, 0), print_token (t2, 0),
195*38fd1498Szrj print_token (u, v));
196*38fd1498Szrj return 0;
197*38fd1498Szrj }
198*38fd1498Szrj return v;
199*38fd1498Szrj }
200*38fd1498Szrj
201*38fd1498Szrj /* If the next token does not have one of the codes T1, T2, T3 or T4, report a
202*38fd1498Szrj parse error; otherwise return the token's value. */
203*38fd1498Szrj static const char *
require4(int t1,int t2,int t3,int t4)204*38fd1498Szrj require4 (int t1, int t2, int t3, int t4)
205*38fd1498Szrj {
206*38fd1498Szrj int u = token ();
207*38fd1498Szrj const char *v = advance ();
208*38fd1498Szrj if (u != t1 && u != t2 && u != t3 && u != t4)
209*38fd1498Szrj {
210*38fd1498Szrj parse_error ("expected %s, %s, %s or %s, have %s",
211*38fd1498Szrj print_token (t1, 0), print_token (t2, 0),
212*38fd1498Szrj print_token (t3, 0), print_token (t4, 0),
213*38fd1498Szrj print_token (u, v));
214*38fd1498Szrj return 0;
215*38fd1498Szrj }
216*38fd1498Szrj return v;
217*38fd1498Szrj }
218*38fd1498Szrj
219*38fd1498Szrj /* Near-terminals. */
220*38fd1498Szrj
221*38fd1498Szrj /* C-style string constant concatenation: STRING+
222*38fd1498Szrj Bare STRING should appear nowhere else in this file. */
223*38fd1498Szrj static const char *
string_seq(void)224*38fd1498Szrj string_seq (void)
225*38fd1498Szrj {
226*38fd1498Szrj const char *s1, *s2;
227*38fd1498Szrj size_t l1, l2;
228*38fd1498Szrj char *buf;
229*38fd1498Szrj
230*38fd1498Szrj s1 = require (STRING);
231*38fd1498Szrj if (s1 == 0)
232*38fd1498Szrj return "";
233*38fd1498Szrj while (token () == STRING)
234*38fd1498Szrj {
235*38fd1498Szrj s2 = advance ();
236*38fd1498Szrj
237*38fd1498Szrj l1 = strlen (s1);
238*38fd1498Szrj l2 = strlen (s2);
239*38fd1498Szrj buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
240*38fd1498Szrj memcpy (buf + l1, s2, l2 + 1);
241*38fd1498Szrj XDELETE (CONST_CAST (char *, s2));
242*38fd1498Szrj s1 = buf;
243*38fd1498Szrj }
244*38fd1498Szrj return s1;
245*38fd1498Szrj }
246*38fd1498Szrj
247*38fd1498Szrj
248*38fd1498Szrj /* The caller has detected a template declaration that starts
249*38fd1498Szrj with TMPL_NAME. Parse up to the closing '>'. This recognizes
250*38fd1498Szrj simple template declarations of the form ID<ID1,ID2,...,IDn>,
251*38fd1498Szrj potentially with a single level of indirection e.g.
252*38fd1498Szrj ID<ID1 *, ID2, ID3 *, ..., IDn>.
253*38fd1498Szrj It does not try to parse anything more sophisticated than that.
254*38fd1498Szrj
255*38fd1498Szrj Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
256*38fd1498Szrj
257*38fd1498Szrj static const char *
require_template_declaration(const char * tmpl_name)258*38fd1498Szrj require_template_declaration (const char *tmpl_name)
259*38fd1498Szrj {
260*38fd1498Szrj char *str;
261*38fd1498Szrj int num_indirections = 0;
262*38fd1498Szrj
263*38fd1498Szrj /* Recognize the opening '<'. */
264*38fd1498Szrj require ('<');
265*38fd1498Szrj str = concat (tmpl_name, "<", (char *) 0);
266*38fd1498Szrj
267*38fd1498Szrj /* Read the comma-separated list of identifiers. */
268*38fd1498Szrj int depth = 1;
269*38fd1498Szrj while (depth > 0)
270*38fd1498Szrj {
271*38fd1498Szrj if (token () == ENUM)
272*38fd1498Szrj {
273*38fd1498Szrj advance ();
274*38fd1498Szrj str = concat (str, "enum ", (char *) 0);
275*38fd1498Szrj continue;
276*38fd1498Szrj }
277*38fd1498Szrj if (token () == NUM
278*38fd1498Szrj || token () == ':'
279*38fd1498Szrj || token () == '+')
280*38fd1498Szrj {
281*38fd1498Szrj str = concat (str, advance (), (char *) 0);
282*38fd1498Szrj continue;
283*38fd1498Szrj }
284*38fd1498Szrj if (token () == '<')
285*38fd1498Szrj {
286*38fd1498Szrj advance ();
287*38fd1498Szrj str = concat (str, "<", (char *) 0);
288*38fd1498Szrj depth += 1;
289*38fd1498Szrj continue;
290*38fd1498Szrj }
291*38fd1498Szrj if (token () == '>')
292*38fd1498Szrj {
293*38fd1498Szrj advance ();
294*38fd1498Szrj str = concat (str, ">", (char *) 0);
295*38fd1498Szrj depth -= 1;
296*38fd1498Szrj continue;
297*38fd1498Szrj }
298*38fd1498Szrj const char *id = require4 (SCALAR, ID, '*', ',');
299*38fd1498Szrj if (id == NULL)
300*38fd1498Szrj {
301*38fd1498Szrj if (T.code == '*')
302*38fd1498Szrj {
303*38fd1498Szrj id = "*";
304*38fd1498Szrj if (num_indirections++)
305*38fd1498Szrj parse_error ("only one level of indirection is supported"
306*38fd1498Szrj " in template arguments");
307*38fd1498Szrj }
308*38fd1498Szrj else
309*38fd1498Szrj id = ",";
310*38fd1498Szrj }
311*38fd1498Szrj else
312*38fd1498Szrj num_indirections = 0;
313*38fd1498Szrj str = concat (str, id, (char *) 0);
314*38fd1498Szrj }
315*38fd1498Szrj return str;
316*38fd1498Szrj }
317*38fd1498Szrj
318*38fd1498Szrj
319*38fd1498Szrj /* typedef_name: either an ID, or a template type
320*38fd1498Szrj specification of the form ID<t1,t2,...,tn>. */
321*38fd1498Szrj
322*38fd1498Szrj static const char *
typedef_name(void)323*38fd1498Szrj typedef_name (void)
324*38fd1498Szrj {
325*38fd1498Szrj const char *id = require (ID);
326*38fd1498Szrj if (token () == '<')
327*38fd1498Szrj return require_template_declaration (id);
328*38fd1498Szrj else
329*38fd1498Szrj return id;
330*38fd1498Szrj }
331*38fd1498Szrj
332*38fd1498Szrj /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
333*38fd1498Szrj static void
consume_balanced(int opener,int closer)334*38fd1498Szrj consume_balanced (int opener, int closer)
335*38fd1498Szrj {
336*38fd1498Szrj require (opener);
337*38fd1498Szrj for (;;)
338*38fd1498Szrj switch (token ())
339*38fd1498Szrj {
340*38fd1498Szrj default:
341*38fd1498Szrj advance ();
342*38fd1498Szrj break;
343*38fd1498Szrj case '(':
344*38fd1498Szrj consume_balanced ('(', ')');
345*38fd1498Szrj break;
346*38fd1498Szrj case '[':
347*38fd1498Szrj consume_balanced ('[', ']');
348*38fd1498Szrj break;
349*38fd1498Szrj case '{':
350*38fd1498Szrj consume_balanced ('{', '}');
351*38fd1498Szrj break;
352*38fd1498Szrj
353*38fd1498Szrj case '}':
354*38fd1498Szrj case ']':
355*38fd1498Szrj case ')':
356*38fd1498Szrj if (token () != closer)
357*38fd1498Szrj parse_error ("unbalanced delimiters - expected '%c', have '%c'",
358*38fd1498Szrj closer, token ());
359*38fd1498Szrj advance ();
360*38fd1498Szrj return;
361*38fd1498Szrj
362*38fd1498Szrj case EOF_TOKEN:
363*38fd1498Szrj parse_error ("unexpected end of file within %c%c-delimited construct",
364*38fd1498Szrj opener, closer);
365*38fd1498Szrj return;
366*38fd1498Szrj }
367*38fd1498Szrj }
368*38fd1498Szrj
369*38fd1498Szrj /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
370*38fd1498Szrj expressions, until we encounter an end-of-statement marker (a ';' or
371*38fd1498Szrj a '}') outside any such delimiters; absorb that too. */
372*38fd1498Szrj
373*38fd1498Szrj static void
consume_until_eos(void)374*38fd1498Szrj consume_until_eos (void)
375*38fd1498Szrj {
376*38fd1498Szrj for (;;)
377*38fd1498Szrj switch (token ())
378*38fd1498Szrj {
379*38fd1498Szrj case ';':
380*38fd1498Szrj advance ();
381*38fd1498Szrj return;
382*38fd1498Szrj
383*38fd1498Szrj case '{':
384*38fd1498Szrj consume_balanced ('{', '}');
385*38fd1498Szrj return;
386*38fd1498Szrj
387*38fd1498Szrj case '(':
388*38fd1498Szrj consume_balanced ('(', ')');
389*38fd1498Szrj break;
390*38fd1498Szrj
391*38fd1498Szrj case '[':
392*38fd1498Szrj consume_balanced ('[', ']');
393*38fd1498Szrj break;
394*38fd1498Szrj
395*38fd1498Szrj case '}':
396*38fd1498Szrj case ']':
397*38fd1498Szrj case ')':
398*38fd1498Szrj parse_error ("unmatched '%c' while scanning for ';'", token ());
399*38fd1498Szrj return;
400*38fd1498Szrj
401*38fd1498Szrj case EOF_TOKEN:
402*38fd1498Szrj parse_error ("unexpected end of file while scanning for ';'");
403*38fd1498Szrj return;
404*38fd1498Szrj
405*38fd1498Szrj default:
406*38fd1498Szrj advance ();
407*38fd1498Szrj break;
408*38fd1498Szrj }
409*38fd1498Szrj }
410*38fd1498Szrj
411*38fd1498Szrj /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
412*38fd1498Szrj expressions, until we encounter a comma or semicolon outside any
413*38fd1498Szrj such delimiters; absorb that too. Returns true if the loop ended
414*38fd1498Szrj with a comma. */
415*38fd1498Szrj
416*38fd1498Szrj static bool
consume_until_comma_or_eos()417*38fd1498Szrj consume_until_comma_or_eos ()
418*38fd1498Szrj {
419*38fd1498Szrj for (;;)
420*38fd1498Szrj switch (token ())
421*38fd1498Szrj {
422*38fd1498Szrj case ',':
423*38fd1498Szrj advance ();
424*38fd1498Szrj return true;
425*38fd1498Szrj
426*38fd1498Szrj case ';':
427*38fd1498Szrj advance ();
428*38fd1498Szrj return false;
429*38fd1498Szrj
430*38fd1498Szrj case '{':
431*38fd1498Szrj consume_balanced ('{', '}');
432*38fd1498Szrj return false;
433*38fd1498Szrj
434*38fd1498Szrj case '(':
435*38fd1498Szrj consume_balanced ('(', ')');
436*38fd1498Szrj break;
437*38fd1498Szrj
438*38fd1498Szrj case '[':
439*38fd1498Szrj consume_balanced ('[', ']');
440*38fd1498Szrj break;
441*38fd1498Szrj
442*38fd1498Szrj case '}':
443*38fd1498Szrj case ']':
444*38fd1498Szrj case ')':
445*38fd1498Szrj parse_error ("unmatched '%s' while scanning for ',' or ';'",
446*38fd1498Szrj print_cur_token ());
447*38fd1498Szrj return false;
448*38fd1498Szrj
449*38fd1498Szrj case EOF_TOKEN:
450*38fd1498Szrj parse_error ("unexpected end of file while scanning for ',' or ';'");
451*38fd1498Szrj return false;
452*38fd1498Szrj
453*38fd1498Szrj default:
454*38fd1498Szrj advance ();
455*38fd1498Szrj break;
456*38fd1498Szrj }
457*38fd1498Szrj }
458*38fd1498Szrj
459*38fd1498Szrj
460*38fd1498Szrj /* GTY(()) option handling. */
461*38fd1498Szrj static type_p type (options_p *optsp, bool nested);
462*38fd1498Szrj
463*38fd1498Szrj /* Optional parenthesized string: ('(' string_seq ')')? */
464*38fd1498Szrj static options_p
str_optvalue_opt(options_p prev)465*38fd1498Szrj str_optvalue_opt (options_p prev)
466*38fd1498Szrj {
467*38fd1498Szrj const char *name = advance ();
468*38fd1498Szrj const char *value = "";
469*38fd1498Szrj if (token () == '(')
470*38fd1498Szrj {
471*38fd1498Szrj advance ();
472*38fd1498Szrj value = string_seq ();
473*38fd1498Szrj require (')');
474*38fd1498Szrj }
475*38fd1498Szrj return create_string_option (prev, name, value);
476*38fd1498Szrj }
477*38fd1498Szrj
478*38fd1498Szrj /* absdecl: type '*'*
479*38fd1498Szrj -- a vague approximation to what the C standard calls an abstract
480*38fd1498Szrj declarator. The only kinds that are actually used are those that
481*38fd1498Szrj are just a bare type and those that have trailing pointer-stars.
482*38fd1498Szrj Further kinds should be implemented if and when they become
483*38fd1498Szrj necessary. Used only within GTY(()) option values, therefore
484*38fd1498Szrj further GTY(()) tags within the type are invalid. Note that the
485*38fd1498Szrj return value has already been run through adjust_field_type. */
486*38fd1498Szrj static type_p
absdecl(void)487*38fd1498Szrj absdecl (void)
488*38fd1498Szrj {
489*38fd1498Szrj type_p ty;
490*38fd1498Szrj options_p opts;
491*38fd1498Szrj
492*38fd1498Szrj ty = type (&opts, true);
493*38fd1498Szrj while (token () == '*')
494*38fd1498Szrj {
495*38fd1498Szrj ty = create_pointer (ty);
496*38fd1498Szrj advance ();
497*38fd1498Szrj }
498*38fd1498Szrj
499*38fd1498Szrj if (opts)
500*38fd1498Szrj parse_error ("nested GTY(()) options are invalid");
501*38fd1498Szrj
502*38fd1498Szrj return adjust_field_type (ty, 0);
503*38fd1498Szrj }
504*38fd1498Szrj
505*38fd1498Szrj /* Type-option: '(' absdecl ')' */
506*38fd1498Szrj static options_p
type_optvalue(options_p prev,const char * name)507*38fd1498Szrj type_optvalue (options_p prev, const char *name)
508*38fd1498Szrj {
509*38fd1498Szrj type_p ty;
510*38fd1498Szrj require ('(');
511*38fd1498Szrj ty = absdecl ();
512*38fd1498Szrj require (')');
513*38fd1498Szrj return create_type_option (prev, name, ty);
514*38fd1498Szrj }
515*38fd1498Szrj
516*38fd1498Szrj /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
517*38fd1498Szrj static options_p
nestedptr_optvalue(options_p prev)518*38fd1498Szrj nestedptr_optvalue (options_p prev)
519*38fd1498Szrj {
520*38fd1498Szrj type_p ty;
521*38fd1498Szrj const char *from, *to;
522*38fd1498Szrj
523*38fd1498Szrj require ('(');
524*38fd1498Szrj ty = absdecl ();
525*38fd1498Szrj require (',');
526*38fd1498Szrj to = string_seq ();
527*38fd1498Szrj require (',');
528*38fd1498Szrj from = string_seq ();
529*38fd1498Szrj require (')');
530*38fd1498Szrj
531*38fd1498Szrj return create_nested_ptr_option (prev, ty, to, from);
532*38fd1498Szrj }
533*38fd1498Szrj
534*38fd1498Szrj /* One GTY(()) option:
535*38fd1498Szrj ID str_optvalue_opt
536*38fd1498Szrj | PTR_ALIAS type_optvalue
537*38fd1498Szrj | NESTED_PTR nestedptr_optvalue
538*38fd1498Szrj */
539*38fd1498Szrj static options_p
option(options_p prev)540*38fd1498Szrj option (options_p prev)
541*38fd1498Szrj {
542*38fd1498Szrj switch (token ())
543*38fd1498Szrj {
544*38fd1498Szrj case ID:
545*38fd1498Szrj return str_optvalue_opt (prev);
546*38fd1498Szrj
547*38fd1498Szrj case PTR_ALIAS:
548*38fd1498Szrj advance ();
549*38fd1498Szrj return type_optvalue (prev, "ptr_alias");
550*38fd1498Szrj
551*38fd1498Szrj case NESTED_PTR:
552*38fd1498Szrj advance ();
553*38fd1498Szrj return nestedptr_optvalue (prev);
554*38fd1498Szrj
555*38fd1498Szrj case USER_GTY:
556*38fd1498Szrj advance ();
557*38fd1498Szrj return create_string_option (prev, "user", "");
558*38fd1498Szrj
559*38fd1498Szrj default:
560*38fd1498Szrj parse_error ("expected an option keyword, have %s", print_cur_token ());
561*38fd1498Szrj advance ();
562*38fd1498Szrj return create_string_option (prev, "", "");
563*38fd1498Szrj }
564*38fd1498Szrj }
565*38fd1498Szrj
566*38fd1498Szrj /* One comma-separated list of options. */
567*38fd1498Szrj static options_p
option_seq(void)568*38fd1498Szrj option_seq (void)
569*38fd1498Szrj {
570*38fd1498Szrj options_p o;
571*38fd1498Szrj
572*38fd1498Szrj o = option (0);
573*38fd1498Szrj while (token () == ',')
574*38fd1498Szrj {
575*38fd1498Szrj advance ();
576*38fd1498Szrj o = option (o);
577*38fd1498Szrj }
578*38fd1498Szrj return o;
579*38fd1498Szrj }
580*38fd1498Szrj
581*38fd1498Szrj /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
582*38fd1498Szrj static options_p
gtymarker(void)583*38fd1498Szrj gtymarker (void)
584*38fd1498Szrj {
585*38fd1498Szrj options_p result = 0;
586*38fd1498Szrj require (GTY_TOKEN);
587*38fd1498Szrj require ('(');
588*38fd1498Szrj require ('(');
589*38fd1498Szrj if (token () != ')')
590*38fd1498Szrj result = option_seq ();
591*38fd1498Szrj require (')');
592*38fd1498Szrj require (')');
593*38fd1498Szrj return result;
594*38fd1498Szrj }
595*38fd1498Szrj
596*38fd1498Szrj /* Optional GTY marker. */
597*38fd1498Szrj static options_p
gtymarker_opt(void)598*38fd1498Szrj gtymarker_opt (void)
599*38fd1498Szrj {
600*38fd1498Szrj if (token () != GTY_TOKEN)
601*38fd1498Szrj return 0;
602*38fd1498Szrj return gtymarker ();
603*38fd1498Szrj }
604*38fd1498Szrj
605*38fd1498Szrj
606*38fd1498Szrj
607*38fd1498Szrj /* Declarators. The logic here is largely lifted from c-parser.c.
608*38fd1498Szrj Note that we do not have to process abstract declarators, which can
609*38fd1498Szrj appear only in parameter type lists or casts (but see absdecl,
610*38fd1498Szrj above). Also, type qualifiers are thrown out in gengtype-lex.l so
611*38fd1498Szrj we don't have to do it. */
612*38fd1498Szrj
613*38fd1498Szrj /* array_and_function_declarators_opt:
614*38fd1498Szrj \epsilon
615*38fd1498Szrj array_and_function_declarators_opt ARRAY
616*38fd1498Szrj array_and_function_declarators_opt '(' ... ')'
617*38fd1498Szrj
618*38fd1498Szrj where '...' indicates stuff we ignore except insofar as grouping
619*38fd1498Szrj symbols ()[]{} must balance.
620*38fd1498Szrj
621*38fd1498Szrj Subroutine of direct_declarator - do not use elsewhere. */
622*38fd1498Szrj
623*38fd1498Szrj static type_p
array_and_function_declarators_opt(type_p ty)624*38fd1498Szrj array_and_function_declarators_opt (type_p ty)
625*38fd1498Szrj {
626*38fd1498Szrj if (token () == ARRAY)
627*38fd1498Szrj {
628*38fd1498Szrj const char *array = advance ();
629*38fd1498Szrj return create_array (array_and_function_declarators_opt (ty), array);
630*38fd1498Szrj }
631*38fd1498Szrj else if (token () == '(')
632*38fd1498Szrj {
633*38fd1498Szrj /* We don't need exact types for functions. */
634*38fd1498Szrj consume_balanced ('(', ')');
635*38fd1498Szrj array_and_function_declarators_opt (ty);
636*38fd1498Szrj return create_scalar_type ("function type");
637*38fd1498Szrj }
638*38fd1498Szrj else
639*38fd1498Szrj return ty;
640*38fd1498Szrj }
641*38fd1498Szrj
642*38fd1498Szrj static type_p inner_declarator (type_p, const char **, options_p *, bool);
643*38fd1498Szrj
644*38fd1498Szrj /* direct_declarator:
645*38fd1498Szrj '(' inner_declarator ')'
646*38fd1498Szrj '(' \epsilon ')' <-- C++ ctors/dtors
647*38fd1498Szrj gtymarker_opt ID array_and_function_declarators_opt
648*38fd1498Szrj
649*38fd1498Szrj Subroutine of declarator, mutually recursive with inner_declarator;
650*38fd1498Szrj do not use elsewhere.
651*38fd1498Szrj
652*38fd1498Szrj IN_STRUCT is true if we are called while parsing structures or classes. */
653*38fd1498Szrj
654*38fd1498Szrj static type_p
direct_declarator(type_p ty,const char ** namep,options_p * optsp,bool in_struct)655*38fd1498Szrj direct_declarator (type_p ty, const char **namep, options_p *optsp,
656*38fd1498Szrj bool in_struct)
657*38fd1498Szrj {
658*38fd1498Szrj /* The first token in a direct-declarator must be an ID, a
659*38fd1498Szrj GTY marker, or an open parenthesis. */
660*38fd1498Szrj switch (token ())
661*38fd1498Szrj {
662*38fd1498Szrj case GTY_TOKEN:
663*38fd1498Szrj *optsp = gtymarker ();
664*38fd1498Szrj /* fall through */
665*38fd1498Szrj
666*38fd1498Szrj case ID:
667*38fd1498Szrj *namep = require (ID);
668*38fd1498Szrj /* If the next token is '(', we are parsing a function declaration.
669*38fd1498Szrj Functions are ignored by gengtype, so we return NULL. */
670*38fd1498Szrj if (token () == '(')
671*38fd1498Szrj return NULL;
672*38fd1498Szrj break;
673*38fd1498Szrj
674*38fd1498Szrj case '(':
675*38fd1498Szrj /* If the declarator starts with a '(', we have three options. We
676*38fd1498Szrj are either parsing 'TYPE (*ID)' (i.e., a function pointer)
677*38fd1498Szrj or 'TYPE(...)'.
678*38fd1498Szrj
679*38fd1498Szrj The latter will be a constructor iff we are inside a
680*38fd1498Szrj structure or class. Otherwise, it could be a typedef, but
681*38fd1498Szrj since we explicitly reject typedefs inside structures, we can
682*38fd1498Szrj assume that we found a ctor and return NULL. */
683*38fd1498Szrj advance ();
684*38fd1498Szrj if (in_struct && token () != '*')
685*38fd1498Szrj {
686*38fd1498Szrj /* Found a constructor. Find and consume the closing ')'. */
687*38fd1498Szrj while (token () != ')')
688*38fd1498Szrj advance ();
689*38fd1498Szrj advance ();
690*38fd1498Szrj /* Tell the caller to ignore this. */
691*38fd1498Szrj return NULL;
692*38fd1498Szrj }
693*38fd1498Szrj ty = inner_declarator (ty, namep, optsp, in_struct);
694*38fd1498Szrj require (')');
695*38fd1498Szrj break;
696*38fd1498Szrj
697*38fd1498Szrj case IGNORABLE_CXX_KEYWORD:
698*38fd1498Szrj /* Any C++ keyword like 'operator' means that we are not looking
699*38fd1498Szrj at a regular data declarator. */
700*38fd1498Szrj return NULL;
701*38fd1498Szrj
702*38fd1498Szrj default:
703*38fd1498Szrj parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
704*38fd1498Szrj print_cur_token ());
705*38fd1498Szrj /* Do _not_ advance if what we have is a close squiggle brace, as
706*38fd1498Szrj we will get much better error recovery that way. */
707*38fd1498Szrj if (token () != '}')
708*38fd1498Szrj advance ();
709*38fd1498Szrj return 0;
710*38fd1498Szrj }
711*38fd1498Szrj return array_and_function_declarators_opt (ty);
712*38fd1498Szrj }
713*38fd1498Szrj
714*38fd1498Szrj /* The difference between inner_declarator and declarator is in the
715*38fd1498Szrj handling of stars. Consider this declaration:
716*38fd1498Szrj
717*38fd1498Szrj char * (*pfc) (void)
718*38fd1498Szrj
719*38fd1498Szrj It declares a pointer to a function that takes no arguments and
720*38fd1498Szrj returns a char*. To construct the correct type for this
721*38fd1498Szrj declaration, the star outside the parentheses must be processed
722*38fd1498Szrj _before_ the function type, the star inside the parentheses must
723*38fd1498Szrj be processed _after_ the function type. To accomplish this,
724*38fd1498Szrj declarator() creates pointers before recursing (it is actually
725*38fd1498Szrj coded as a while loop), whereas inner_declarator() recurses before
726*38fd1498Szrj creating pointers. */
727*38fd1498Szrj
728*38fd1498Szrj /* inner_declarator:
729*38fd1498Szrj '*' inner_declarator
730*38fd1498Szrj direct_declarator
731*38fd1498Szrj
732*38fd1498Szrj Mutually recursive subroutine of direct_declarator; do not use
733*38fd1498Szrj elsewhere.
734*38fd1498Szrj
735*38fd1498Szrj IN_STRUCT is true if we are called while parsing structures or classes. */
736*38fd1498Szrj
737*38fd1498Szrj static type_p
inner_declarator(type_p ty,const char ** namep,options_p * optsp,bool in_struct)738*38fd1498Szrj inner_declarator (type_p ty, const char **namep, options_p *optsp,
739*38fd1498Szrj bool in_struct)
740*38fd1498Szrj {
741*38fd1498Szrj if (token () == '*')
742*38fd1498Szrj {
743*38fd1498Szrj type_p inner;
744*38fd1498Szrj advance ();
745*38fd1498Szrj inner = inner_declarator (ty, namep, optsp, in_struct);
746*38fd1498Szrj if (inner == 0)
747*38fd1498Szrj return 0;
748*38fd1498Szrj else
749*38fd1498Szrj return create_pointer (ty);
750*38fd1498Szrj }
751*38fd1498Szrj else
752*38fd1498Szrj return direct_declarator (ty, namep, optsp, in_struct);
753*38fd1498Szrj }
754*38fd1498Szrj
755*38fd1498Szrj /* declarator: '*'+ direct_declarator
756*38fd1498Szrj
757*38fd1498Szrj This is the sole public interface to this part of the grammar.
758*38fd1498Szrj Arguments are the type known so far, a pointer to where the name
759*38fd1498Szrj may be stored, and a pointer to where GTY options may be stored.
760*38fd1498Szrj
761*38fd1498Szrj IN_STRUCT is true when we are called to parse declarators inside
762*38fd1498Szrj a structure or class.
763*38fd1498Szrj
764*38fd1498Szrj Returns the final type. */
765*38fd1498Szrj
766*38fd1498Szrj static type_p
767*38fd1498Szrj declarator (type_p ty, const char **namep, options_p *optsp,
768*38fd1498Szrj bool in_struct = false)
769*38fd1498Szrj {
770*38fd1498Szrj *namep = 0;
771*38fd1498Szrj *optsp = 0;
772*38fd1498Szrj while (token () == '*')
773*38fd1498Szrj {
774*38fd1498Szrj advance ();
775*38fd1498Szrj ty = create_pointer (ty);
776*38fd1498Szrj }
777*38fd1498Szrj return direct_declarator (ty, namep, optsp, in_struct);
778*38fd1498Szrj }
779*38fd1498Szrj
780*38fd1498Szrj /* Types and declarations. */
781*38fd1498Szrj
782*38fd1498Szrj /* Structure field(s) declaration:
783*38fd1498Szrj (
784*38fd1498Szrj type bitfield ';'
785*38fd1498Szrj | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
786*38fd1498Szrj )*
787*38fd1498Szrj
788*38fd1498Szrj Knows that such declarations must end with a close brace (or,
789*38fd1498Szrj erroneously, at EOF).
790*38fd1498Szrj */
791*38fd1498Szrj static pair_p
struct_field_seq(void)792*38fd1498Szrj struct_field_seq (void)
793*38fd1498Szrj {
794*38fd1498Szrj pair_p f = 0;
795*38fd1498Szrj type_p ty, dty;
796*38fd1498Szrj options_p opts, dopts;
797*38fd1498Szrj const char *name;
798*38fd1498Szrj bool another;
799*38fd1498Szrj
800*38fd1498Szrj while (token () != '}' && token () != EOF_TOKEN)
801*38fd1498Szrj {
802*38fd1498Szrj ty = type (&opts, true);
803*38fd1498Szrj
804*38fd1498Szrj /* Ignore access-control keywords ("public:" etc). */
805*38fd1498Szrj while (!ty && token () == IGNORABLE_CXX_KEYWORD)
806*38fd1498Szrj {
807*38fd1498Szrj const char *keyword = advance ();
808*38fd1498Szrj if (strcmp (keyword, "public:") != 0
809*38fd1498Szrj && strcmp (keyword, "private:") != 0
810*38fd1498Szrj && strcmp (keyword, "protected:") != 0)
811*38fd1498Szrj break;
812*38fd1498Szrj ty = type (&opts, true);
813*38fd1498Szrj }
814*38fd1498Szrj
815*38fd1498Szrj if (!ty || token () == ':')
816*38fd1498Szrj {
817*38fd1498Szrj consume_until_eos ();
818*38fd1498Szrj continue;
819*38fd1498Szrj }
820*38fd1498Szrj
821*38fd1498Szrj do
822*38fd1498Szrj {
823*38fd1498Szrj dty = declarator (ty, &name, &dopts, true);
824*38fd1498Szrj
825*38fd1498Szrj /* There could be any number of weird things after the declarator,
826*38fd1498Szrj notably bitfield declarations and __attribute__s. If this
827*38fd1498Szrj function returns true, the last thing was a comma, so we have
828*38fd1498Szrj more than one declarator paired with the current type. */
829*38fd1498Szrj another = consume_until_comma_or_eos ();
830*38fd1498Szrj
831*38fd1498Szrj if (!dty)
832*38fd1498Szrj continue;
833*38fd1498Szrj
834*38fd1498Szrj if (opts && dopts)
835*38fd1498Szrj parse_error ("two GTY(()) options for field %s", name);
836*38fd1498Szrj if (opts && !dopts)
837*38fd1498Szrj dopts = opts;
838*38fd1498Szrj
839*38fd1498Szrj f = create_field_at (f, dty, name, dopts, &lexer_line);
840*38fd1498Szrj }
841*38fd1498Szrj while (another);
842*38fd1498Szrj }
843*38fd1498Szrj return nreverse_pairs (f);
844*38fd1498Szrj }
845*38fd1498Szrj
846*38fd1498Szrj /* Return true if OPTS contain the option named STR. */
847*38fd1498Szrj
848*38fd1498Szrj bool
opts_have(options_p opts,const char * str)849*38fd1498Szrj opts_have (options_p opts, const char *str)
850*38fd1498Szrj {
851*38fd1498Szrj for (options_p opt = opts; opt; opt = opt->next)
852*38fd1498Szrj if (strcmp (opt->name, str) == 0)
853*38fd1498Szrj return true;
854*38fd1498Szrj return false;
855*38fd1498Szrj }
856*38fd1498Szrj
857*38fd1498Szrj
858*38fd1498Szrj /* This is called type(), but what it parses (sort of) is what C calls
859*38fd1498Szrj declaration-specifiers and specifier-qualifier-list:
860*38fd1498Szrj
861*38fd1498Szrj SCALAR
862*38fd1498Szrj | ID // typedef
863*38fd1498Szrj | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
864*38fd1498Szrj | ENUM ID ( '{' ... '}' )?
865*38fd1498Szrj
866*38fd1498Szrj Returns a partial type; under some conditions (notably
867*38fd1498Szrj "struct foo GTY((...)) thing;") it may write an options
868*38fd1498Szrj structure to *OPTSP.
869*38fd1498Szrj
870*38fd1498Szrj NESTED is true when parsing a declaration already known to have a
871*38fd1498Szrj GTY marker. In these cases, typedef and enum declarations are not
872*38fd1498Szrj allowed because gengtype only understands types at the global
873*38fd1498Szrj scope. */
874*38fd1498Szrj
875*38fd1498Szrj static type_p
type(options_p * optsp,bool nested)876*38fd1498Szrj type (options_p *optsp, bool nested)
877*38fd1498Szrj {
878*38fd1498Szrj const char *s;
879*38fd1498Szrj *optsp = 0;
880*38fd1498Szrj switch (token ())
881*38fd1498Szrj {
882*38fd1498Szrj case SCALAR:
883*38fd1498Szrj s = advance ();
884*38fd1498Szrj return create_scalar_type (s);
885*38fd1498Szrj
886*38fd1498Szrj case ID:
887*38fd1498Szrj s = typedef_name ();
888*38fd1498Szrj return resolve_typedef (s, &lexer_line);
889*38fd1498Szrj
890*38fd1498Szrj case IGNORABLE_CXX_KEYWORD:
891*38fd1498Szrj /* By returning NULL here, we indicate to the caller that they
892*38fd1498Szrj should ignore everything following this keyword up to the
893*38fd1498Szrj next ';' or '}'. */
894*38fd1498Szrj return NULL;
895*38fd1498Szrj
896*38fd1498Szrj case STRUCT:
897*38fd1498Szrj case UNION:
898*38fd1498Szrj {
899*38fd1498Szrj type_p base_class = NULL;
900*38fd1498Szrj options_p opts = 0;
901*38fd1498Szrj /* GTY annotations follow attribute syntax
902*38fd1498Szrj GTY_BEFORE_ID is for union/struct declarations
903*38fd1498Szrj GTY_AFTER_ID is for variable declarations. */
904*38fd1498Szrj enum
905*38fd1498Szrj {
906*38fd1498Szrj NO_GTY,
907*38fd1498Szrj GTY_BEFORE_ID,
908*38fd1498Szrj GTY_AFTER_ID
909*38fd1498Szrj } is_gty = NO_GTY;
910*38fd1498Szrj enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
911*38fd1498Szrj advance ();
912*38fd1498Szrj
913*38fd1498Szrj /* Top-level structures that are not explicitly tagged GTY(())
914*38fd1498Szrj are treated as mere forward declarations. This is because
915*38fd1498Szrj there are a lot of structures that we don't need to know
916*38fd1498Szrj about, and some of those have C++ and macro constructs that
917*38fd1498Szrj we cannot handle. */
918*38fd1498Szrj if (nested || token () == GTY_TOKEN)
919*38fd1498Szrj {
920*38fd1498Szrj is_gty = GTY_BEFORE_ID;
921*38fd1498Szrj opts = gtymarker_opt ();
922*38fd1498Szrj }
923*38fd1498Szrj
924*38fd1498Szrj if (token () == ID)
925*38fd1498Szrj s = advance ();
926*38fd1498Szrj else
927*38fd1498Szrj s = xasprintf ("anonymous:%s:%d",
928*38fd1498Szrj get_input_file_name (lexer_line.file),
929*38fd1498Szrj lexer_line.line);
930*38fd1498Szrj
931*38fd1498Szrj /* Unfortunately above GTY_TOKEN check does not capture the
932*38fd1498Szrj typedef struct_type GTY case. */
933*38fd1498Szrj if (token () == GTY_TOKEN)
934*38fd1498Szrj {
935*38fd1498Szrj is_gty = GTY_AFTER_ID;
936*38fd1498Szrj opts = gtymarker_opt ();
937*38fd1498Szrj }
938*38fd1498Szrj
939*38fd1498Szrj bool is_user_gty = opts_have (opts, "user");
940*38fd1498Szrj
941*38fd1498Szrj if (token () == ':')
942*38fd1498Szrj {
943*38fd1498Szrj if (is_gty && !is_user_gty)
944*38fd1498Szrj {
945*38fd1498Szrj /* For GTY-marked types that are not "user", parse some C++
946*38fd1498Szrj inheritance specifications.
947*38fd1498Szrj We require single-inheritance from a non-template type. */
948*38fd1498Szrj advance ();
949*38fd1498Szrj const char *basename = require (ID);
950*38fd1498Szrj /* This may be either an access specifier, or the base name. */
951*38fd1498Szrj if (strcmp (basename, "public") == 0
952*38fd1498Szrj || strcmp (basename, "protected") == 0
953*38fd1498Szrj || strcmp (basename, "private") == 0)
954*38fd1498Szrj basename = require (ID);
955*38fd1498Szrj base_class = find_structure (basename, TYPE_STRUCT);
956*38fd1498Szrj if (!base_class)
957*38fd1498Szrj parse_error ("unrecognized base class: %s", basename);
958*38fd1498Szrj require_without_advance ('{');
959*38fd1498Szrj }
960*38fd1498Szrj else
961*38fd1498Szrj {
962*38fd1498Szrj /* For types lacking GTY-markings, skip over C++ inheritance
963*38fd1498Szrj specification (and thus avoid having to parse e.g. template
964*38fd1498Szrj types). */
965*38fd1498Szrj while (token () != '{')
966*38fd1498Szrj advance ();
967*38fd1498Szrj }
968*38fd1498Szrj }
969*38fd1498Szrj
970*38fd1498Szrj if (is_gty)
971*38fd1498Szrj {
972*38fd1498Szrj if (token () == '{')
973*38fd1498Szrj {
974*38fd1498Szrj pair_p fields;
975*38fd1498Szrj
976*38fd1498Szrj if (is_gty == GTY_AFTER_ID)
977*38fd1498Szrj parse_error ("GTY must be specified before identifier");
978*38fd1498Szrj
979*38fd1498Szrj if (!is_user_gty)
980*38fd1498Szrj {
981*38fd1498Szrj advance ();
982*38fd1498Szrj fields = struct_field_seq ();
983*38fd1498Szrj require ('}');
984*38fd1498Szrj }
985*38fd1498Szrj else
986*38fd1498Szrj {
987*38fd1498Szrj /* Do not look inside user defined structures. */
988*38fd1498Szrj fields = NULL;
989*38fd1498Szrj kind = TYPE_USER_STRUCT;
990*38fd1498Szrj consume_balanced ('{', '}');
991*38fd1498Szrj return create_user_defined_type (s, &lexer_line);
992*38fd1498Szrj }
993*38fd1498Szrj
994*38fd1498Szrj return new_structure (s, kind, &lexer_line, fields, opts,
995*38fd1498Szrj base_class);
996*38fd1498Szrj }
997*38fd1498Szrj }
998*38fd1498Szrj else if (token () == '{')
999*38fd1498Szrj consume_balanced ('{', '}');
1000*38fd1498Szrj if (opts)
1001*38fd1498Szrj *optsp = opts;
1002*38fd1498Szrj return find_structure (s, kind);
1003*38fd1498Szrj }
1004*38fd1498Szrj
1005*38fd1498Szrj case TYPEDEF:
1006*38fd1498Szrj /* In C++, a typedef inside a struct/class/union defines a new
1007*38fd1498Szrj type for that inner scope. We cannot support this in
1008*38fd1498Szrj gengtype because we have no concept of scoping.
1009*38fd1498Szrj
1010*38fd1498Szrj We handle typedefs in the global scope separately (see
1011*38fd1498Szrj parse_file), so if we find a 'typedef', we must be inside
1012*38fd1498Szrj a struct. */
1013*38fd1498Szrj gcc_assert (nested);
1014*38fd1498Szrj parse_error ("typedefs not supported in structures marked with "
1015*38fd1498Szrj "automatic GTY markers. Use GTY((user)) to mark "
1016*38fd1498Szrj "this structure.");
1017*38fd1498Szrj advance ();
1018*38fd1498Szrj return NULL;
1019*38fd1498Szrj
1020*38fd1498Szrj case ENUM:
1021*38fd1498Szrj advance ();
1022*38fd1498Szrj if (token () == ID)
1023*38fd1498Szrj s = advance ();
1024*38fd1498Szrj else
1025*38fd1498Szrj s = xasprintf ("anonymous:%s:%d",
1026*38fd1498Szrj get_input_file_name (lexer_line.file),
1027*38fd1498Szrj lexer_line.line);
1028*38fd1498Szrj
1029*38fd1498Szrj if (token () == '{')
1030*38fd1498Szrj consume_balanced ('{', '}');
1031*38fd1498Szrj
1032*38fd1498Szrj /* If after parsing the enum we are at the end of the statement,
1033*38fd1498Szrj and we are currently inside a structure, then this was an
1034*38fd1498Szrj enum declaration inside this scope.
1035*38fd1498Szrj
1036*38fd1498Szrj We cannot support this for the same reason we cannot support
1037*38fd1498Szrj 'typedef' inside structures (see the TYPEDEF handler above).
1038*38fd1498Szrj If this happens, emit an error and return NULL. */
1039*38fd1498Szrj if (nested && token () == ';')
1040*38fd1498Szrj {
1041*38fd1498Szrj parse_error ("enum definitions not supported in structures marked "
1042*38fd1498Szrj "with automatic GTY markers. Use GTY((user)) to mark "
1043*38fd1498Szrj "this structure.");
1044*38fd1498Szrj advance ();
1045*38fd1498Szrj return NULL;
1046*38fd1498Szrj }
1047*38fd1498Szrj
1048*38fd1498Szrj return create_scalar_type (s);
1049*38fd1498Szrj
1050*38fd1498Szrj default:
1051*38fd1498Szrj parse_error ("expected a type specifier, have %s", print_cur_token ());
1052*38fd1498Szrj advance ();
1053*38fd1498Szrj return create_scalar_type ("erroneous type");
1054*38fd1498Szrj }
1055*38fd1498Szrj }
1056*38fd1498Szrj
1057*38fd1498Szrj /* Top level constructs. */
1058*38fd1498Szrj
1059*38fd1498Szrj /* Dispatch declarations beginning with 'typedef'. */
1060*38fd1498Szrj
1061*38fd1498Szrj static void
typedef_decl(void)1062*38fd1498Szrj typedef_decl (void)
1063*38fd1498Szrj {
1064*38fd1498Szrj type_p ty, dty;
1065*38fd1498Szrj const char *name;
1066*38fd1498Szrj options_p opts;
1067*38fd1498Szrj bool another;
1068*38fd1498Szrj
1069*38fd1498Szrj gcc_assert (token () == TYPEDEF);
1070*38fd1498Szrj advance ();
1071*38fd1498Szrj
1072*38fd1498Szrj ty = type (&opts, false);
1073*38fd1498Szrj if (!ty)
1074*38fd1498Szrj return;
1075*38fd1498Szrj if (opts)
1076*38fd1498Szrj parse_error ("GTY((...)) cannot be applied to a typedef");
1077*38fd1498Szrj do
1078*38fd1498Szrj {
1079*38fd1498Szrj dty = declarator (ty, &name, &opts);
1080*38fd1498Szrj if (opts)
1081*38fd1498Szrj parse_error ("GTY((...)) cannot be applied to a typedef");
1082*38fd1498Szrj
1083*38fd1498Szrj /* Yet another place where we could have junk (notably attributes)
1084*38fd1498Szrj after the declarator. */
1085*38fd1498Szrj another = consume_until_comma_or_eos ();
1086*38fd1498Szrj if (dty)
1087*38fd1498Szrj do_typedef (name, dty, &lexer_line);
1088*38fd1498Szrj }
1089*38fd1498Szrj while (another);
1090*38fd1498Szrj }
1091*38fd1498Szrj
1092*38fd1498Szrj /* Structure definition: type() does all the work. */
1093*38fd1498Szrj
1094*38fd1498Szrj static void
struct_or_union(void)1095*38fd1498Szrj struct_or_union (void)
1096*38fd1498Szrj {
1097*38fd1498Szrj options_p dummy;
1098*38fd1498Szrj type (&dummy, false);
1099*38fd1498Szrj /* There may be junk after the type: notably, we cannot currently
1100*38fd1498Szrj distinguish 'struct foo *function(prototype);' from 'struct foo;'
1101*38fd1498Szrj ... we could call declarator(), but it's a waste of time at
1102*38fd1498Szrj present. Instead, just eat whatever token is currently lookahead
1103*38fd1498Szrj and go back to lexical skipping mode. */
1104*38fd1498Szrj advance ();
1105*38fd1498Szrj }
1106*38fd1498Szrj
1107*38fd1498Szrj /* GC root declaration:
1108*38fd1498Szrj (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
1109*38fd1498Szrj If the gtymarker is not present, we ignore the rest of the declaration. */
1110*38fd1498Szrj static void
extern_or_static(void)1111*38fd1498Szrj extern_or_static (void)
1112*38fd1498Szrj {
1113*38fd1498Szrj options_p opts, opts2, dopts;
1114*38fd1498Szrj type_p ty, dty;
1115*38fd1498Szrj const char *name;
1116*38fd1498Szrj require2 (EXTERN, STATIC);
1117*38fd1498Szrj
1118*38fd1498Szrj if (token () != GTY_TOKEN)
1119*38fd1498Szrj {
1120*38fd1498Szrj advance ();
1121*38fd1498Szrj return;
1122*38fd1498Szrj }
1123*38fd1498Szrj
1124*38fd1498Szrj opts = gtymarker ();
1125*38fd1498Szrj ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
1126*38fd1498Szrj dty = declarator (ty, &name, &dopts);
1127*38fd1498Szrj
1128*38fd1498Szrj if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
1129*38fd1498Szrj parse_error ("GTY((...)) specified more than once for %s", name);
1130*38fd1498Szrj else if (opts2)
1131*38fd1498Szrj opts = opts2;
1132*38fd1498Szrj else if (dopts)
1133*38fd1498Szrj opts = dopts;
1134*38fd1498Szrj
1135*38fd1498Szrj if (dty)
1136*38fd1498Szrj {
1137*38fd1498Szrj note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
1138*38fd1498Szrj require2 (';', '=');
1139*38fd1498Szrj }
1140*38fd1498Szrj }
1141*38fd1498Szrj
1142*38fd1498Szrj /* Parse the file FNAME for GC-relevant declarations and definitions.
1143*38fd1498Szrj This is the only entry point to this file. */
1144*38fd1498Szrj void
parse_file(const char * fname)1145*38fd1498Szrj parse_file (const char *fname)
1146*38fd1498Szrj {
1147*38fd1498Szrj yybegin (fname);
1148*38fd1498Szrj for (;;)
1149*38fd1498Szrj {
1150*38fd1498Szrj switch (token ())
1151*38fd1498Szrj {
1152*38fd1498Szrj case EXTERN:
1153*38fd1498Szrj case STATIC:
1154*38fd1498Szrj extern_or_static ();
1155*38fd1498Szrj break;
1156*38fd1498Szrj
1157*38fd1498Szrj case STRUCT:
1158*38fd1498Szrj case UNION:
1159*38fd1498Szrj struct_or_union ();
1160*38fd1498Szrj break;
1161*38fd1498Szrj
1162*38fd1498Szrj case TYPEDEF:
1163*38fd1498Szrj typedef_decl ();
1164*38fd1498Szrj break;
1165*38fd1498Szrj
1166*38fd1498Szrj case EOF_TOKEN:
1167*38fd1498Szrj goto eof;
1168*38fd1498Szrj
1169*38fd1498Szrj default:
1170*38fd1498Szrj parse_error ("unexpected top level token, %s", print_cur_token ());
1171*38fd1498Szrj goto eof;
1172*38fd1498Szrj }
1173*38fd1498Szrj lexer_toplevel_done = 1;
1174*38fd1498Szrj }
1175*38fd1498Szrj
1176*38fd1498Szrj eof:
1177*38fd1498Szrj advance ();
1178*38fd1498Szrj yyend ();
1179*38fd1498Szrj }
1180