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