comparison gcc/gengtype-parse.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* Process source files and output type information. 1 /* Process source files and output type information.
2 Copyright (C) 2006, 2007, 2010 Free Software Foundation, Inc. 2 Copyright (C) 2006-2017 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it under 6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free 7 the terms of the GNU General Public License as published by the Free
15 15
16 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see 17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */ 18 <http://www.gnu.org/licenses/>. */
19 19
20 #ifdef HOST_GENERATOR_FILE
21 #include "config.h"
22 #define GENERATOR_FILE 1
23 #else
20 #include "bconfig.h" 24 #include "bconfig.h"
25 #endif
21 #include "system.h" 26 #include "system.h"
22 #include "gengtype.h" 27 #include "gengtype.h"
23 28
24 /* This is a simple recursive-descent parser which understands a subset of 29 /* This is a simple recursive-descent parser which understands a subset of
25 the C type grammar. 30 the C type grammar.
70 "extern", 75 "extern",
71 "static", 76 "static",
72 "union", 77 "union",
73 "struct", 78 "struct",
74 "enum", 79 "enum",
75 "VEC",
76 "DEF_VEC_[OP]",
77 "DEF_VEC_I",
78 "DEF_VEC_ALLOC_[IOP]",
79 "...", 80 "...",
80 "ptr_alias", 81 "ptr_alias",
81 "nested_ptr", 82 "nested_ptr",
82 "a param<N>_is option", 83 "a param<N>_is option",
83 "a number", 84 "a number",
84 "a scalar type", 85 "a scalar type",
85 "an identifier", 86 "an identifier",
86 "a string constant", 87 "a string constant",
87 "a character constant", 88 "a character constant",
88 "an array declarator", 89 "an array declarator",
90 "a C++ keyword to ignore"
89 }; 91 };
90 92
91 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */ 93 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
92 static const char *const token_value_format[] = { 94 static const char *const token_value_format[] = {
93 "%s", 95 "%s",
95 "'%s'", 97 "'%s'",
96 "'%s'", 98 "'%s'",
97 "'\"%s\"'", 99 "'\"%s\"'",
98 "\"'%s'\"", 100 "\"'%s'\"",
99 "'[%s]'", 101 "'[%s]'",
102 "'%s'",
100 }; 103 };
101 104
102 /* Produce a printable representation for a token defined by CODE and 105 /* Produce a printable representation for a token defined by CODE and
103 VALUE. This sometimes returns pointers into malloc memory and 106 VALUE. This sometimes returns pointers into malloc memory and
104 sometimes not, therefore it is unsafe to free the pointer it 107 sometimes not, therefore it is unsafe to free the pointer it
161 return 0; 164 return 0;
162 } 165 }
163 return v; 166 return v;
164 } 167 }
165 168
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
166 /* If the next token does not have one of the codes T1 or T2, report a 184 /* If the next token does not have one of the codes T1 or T2, report a
167 parse error; otherwise return the token's value. */ 185 parse error; otherwise return the token's value. */
168 static const char * 186 static const char *
169 require2 (int t1, int t2) 187 require2 (int t1, int t2)
170 { 188 {
172 const char *v = advance (); 190 const char *v = advance ();
173 if (u != t1 && u != t2) 191 if (u != t1 && u != t2)
174 { 192 {
175 parse_error ("expected %s or %s, have %s", 193 parse_error ("expected %s or %s, have %s",
176 print_token (t1, 0), print_token (t2, 0), 194 print_token (t1, 0), print_token (t2, 0),
195 print_token (u, v));
196 return 0;
197 }
198 return v;
199 }
200
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),
177 print_token (u, v)); 213 print_token (u, v));
178 return 0; 214 return 0;
179 } 215 }
180 return v; 216 return v;
181 } 217 }
206 s1 = buf; 242 s1 = buf;
207 } 243 }
208 return s1; 244 return s1;
209 } 245 }
210 246
211 /* typedef_name: either an ID, or VEC(x,y) which is translated to VEC_x_y. 247
212 Use only where VEC(x,y) is legitimate, i.e. in positions where a 248 /* The caller has detected a template declaration that starts
213 typedef name may appear. */ 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
214 static const char * 322 static const char *
215 typedef_name (void) 323 typedef_name (void)
216 { 324 {
217 if (token () == VEC_TOKEN) 325 const char *id = require (ID);
218 { 326 if (token () == '<')
219 const char *c1, *c2, *r; 327 return require_template_declaration (id);
220 advance ();
221 require ('(');
222 c1 = require2 (ID, SCALAR);
223 require (',');
224 c2 = require (ID);
225 require (')');
226 r = concat ("VEC_", c1, "_", c2, (char *) 0);
227 free (CONST_CAST (char *, c1));
228 free (CONST_CAST (char *, c2));
229 return r;
230 }
231 else 328 else
232 return require (ID); 329 return id;
233 } 330 }
234 331
235 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */ 332 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
236 static void 333 static void
237 consume_balanced (int opener, int closer) 334 consume_balanced (int opener, int closer)
268 return; 365 return;
269 } 366 }
270 } 367 }
271 368
272 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited 369 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
273 expressions, until we encounter a semicolon outside any such 370 expressions, until we encounter an end-of-statement marker (a ';' or
274 delimiters; absorb that too. If IMMEDIATE is true, it is an error 371 a '}') outside any such delimiters; absorb that too. */
275 if the semicolon is not the first token encountered. */ 372
276 static void 373 static void
277 consume_until_semi (bool immediate) 374 consume_until_eos (void)
278 { 375 {
279 if (immediate && token () != ';')
280 require (';');
281 for (;;) 376 for (;;)
282 switch (token ()) 377 switch (token ())
283 { 378 {
284 case ';': 379 case ';':
285 advance (); 380 advance ();
286 return; 381 return;
287 default: 382
288 advance (); 383 case '{':
289 break; 384 consume_balanced ('{', '}');
385 return;
290 386
291 case '(': 387 case '(':
292 consume_balanced ('(', ')'); 388 consume_balanced ('(', ')');
293 break; 389 break;
390
294 case '[': 391 case '[':
295 consume_balanced ('[', ']'); 392 consume_balanced ('[', ']');
296 break;
297 case '{':
298 consume_balanced ('{', '}');
299 break; 393 break;
300 394
301 case '}': 395 case '}':
302 case ']': 396 case ']':
303 case ')': 397 case ')':
304 parse_error ("unmatched '%c' while scanning for ';'", token ()); 398 parse_error ("unmatched '%c' while scanning for ';'", token ());
305 return; 399 return;
306 400
307 case EOF_TOKEN: 401 case EOF_TOKEN:
308 parse_error ("unexpected end of file while scanning for ';'"); 402 parse_error ("unexpected end of file while scanning for ';'");
309 return; 403 return;
404
405 default:
406 advance ();
407 break;
310 } 408 }
311 } 409 }
312 410
313 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited 411 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
314 expressions, until we encounter a comma or semicolon outside any 412 expressions, until we encounter a comma or semicolon outside any
315 such delimiters; absorb that too. If IMMEDIATE is true, it is an 413 such delimiters; absorb that too. Returns true if the loop ended
316 error if the comma or semicolon is not the first token encountered. 414 with a comma. */
317 Returns true if the loop ended with a comma. */ 415
318 static bool 416 static bool
319 consume_until_comma_or_semi (bool immediate) 417 consume_until_comma_or_eos ()
320 { 418 {
321 if (immediate && token () != ',' && token () != ';')
322 require2 (',', ';');
323 for (;;) 419 for (;;)
324 switch (token ()) 420 switch (token ())
325 { 421 {
326 case ',': 422 case ',':
327 advance (); 423 advance ();
328 return true; 424 return true;
425
329 case ';': 426 case ';':
330 advance (); 427 advance ();
331 return false; 428 return false;
332 default: 429
333 advance (); 430 case '{':
334 break; 431 consume_balanced ('{', '}');
432 return false;
335 433
336 case '(': 434 case '(':
337 consume_balanced ('(', ')'); 435 consume_balanced ('(', ')');
338 break; 436 break;
437
339 case '[': 438 case '[':
340 consume_balanced ('[', ']'); 439 consume_balanced ('[', ']');
341 break;
342 case '{':
343 consume_balanced ('{', '}');
344 break; 440 break;
345 441
346 case '}': 442 case '}':
347 case ']': 443 case ']':
348 case ')': 444 case ')':
351 return false; 447 return false;
352 448
353 case EOF_TOKEN: 449 case EOF_TOKEN:
354 parse_error ("unexpected end of file while scanning for ',' or ';'"); 450 parse_error ("unexpected end of file while scanning for ',' or ';'");
355 return false; 451 return false;
452
453 default:
454 advance ();
455 break;
356 } 456 }
357 } 457 }
358 458
359 459
360 /* GTY(()) option handling. */ 460 /* GTY(()) option handling. */
432 } 532 }
433 533
434 /* One GTY(()) option: 534 /* One GTY(()) option:
435 ID str_optvalue_opt 535 ID str_optvalue_opt
436 | PTR_ALIAS type_optvalue 536 | PTR_ALIAS type_optvalue
437 | PARAM_IS type_optvalue
438 | NESTED_PTR nestedptr_optvalue 537 | NESTED_PTR nestedptr_optvalue
439 */ 538 */
440 static options_p 539 static options_p
441 option (options_p prev) 540 option (options_p prev)
442 { 541 {
447 546
448 case PTR_ALIAS: 547 case PTR_ALIAS:
449 advance (); 548 advance ();
450 return type_optvalue (prev, "ptr_alias"); 549 return type_optvalue (prev, "ptr_alias");
451 550
452 case PARAM_IS:
453 return type_optvalue (prev, advance ());
454
455 case NESTED_PTR: 551 case NESTED_PTR:
456 advance (); 552 advance ();
457 return nestedptr_optvalue (prev); 553 return nestedptr_optvalue (prev);
554
555 case USER_GTY:
556 advance ();
557 return create_string_option (prev, "user", "");
458 558
459 default: 559 default:
460 parse_error ("expected an option keyword, have %s", print_cur_token ()); 560 parse_error ("expected an option keyword, have %s", print_cur_token ());
461 advance (); 561 advance ();
462 return create_string_option (prev, "", ""); 562 return create_string_option (prev, "", "");
499 { 599 {
500 if (token () != GTY_TOKEN) 600 if (token () != GTY_TOKEN)
501 return 0; 601 return 0;
502 return gtymarker (); 602 return gtymarker ();
503 } 603 }
604
605
504 606
505 /* Declarators. The logic here is largely lifted from c-parser.c. 607 /* Declarators. The logic here is largely lifted from c-parser.c.
506 Note that we do not have to process abstract declarators, which can 608 Note that we do not have to process abstract declarators, which can
507 appear only in parameter type lists or casts (but see absdecl, 609 appear only in parameter type lists or casts (but see absdecl,
508 above). Also, type qualifiers are thrown out in gengtype-lex.l so 610 above). Also, type qualifiers are thrown out in gengtype-lex.l so
535 } 637 }
536 else 638 else
537 return ty; 639 return ty;
538 } 640 }
539 641
540 static type_p inner_declarator (type_p, const char **, options_p *); 642 static type_p inner_declarator (type_p, const char **, options_p *, bool);
541 643
542 /* direct_declarator: 644 /* direct_declarator:
543 '(' inner_declarator ')' 645 '(' inner_declarator ')'
646 '(' \epsilon ')' <-- C++ ctors/dtors
544 gtymarker_opt ID array_and_function_declarators_opt 647 gtymarker_opt ID array_and_function_declarators_opt
545 648
546 Subroutine of declarator, mutually recursive with inner_declarator; 649 Subroutine of declarator, mutually recursive with inner_declarator;
547 do not use elsewhere. */ 650 do not use elsewhere.
651
652 IN_STRUCT is true if we are called while parsing structures or classes. */
653
548 static type_p 654 static type_p
549 direct_declarator (type_p ty, const char **namep, options_p *optsp) 655 direct_declarator (type_p ty, const char **namep, options_p *optsp,
656 bool in_struct)
550 { 657 {
551 /* The first token in a direct-declarator must be an ID, a 658 /* The first token in a direct-declarator must be an ID, a
552 GTY marker, or an open parenthesis. */ 659 GTY marker, or an open parenthesis. */
553 switch (token ()) 660 switch (token ())
554 { 661 {
555 case GTY_TOKEN: 662 case GTY_TOKEN:
556 *optsp = gtymarker (); 663 *optsp = gtymarker ();
557 /* fall through */ 664 /* fall through */
665
558 case ID: 666 case ID:
559 *namep = require (ID); 667 *namep = require (ID);
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;
560 break; 672 break;
561 673
562 case '(': 674 case '(':
563 advance (); 675 /* If the declarator starts with a '(', we have three options. We
564 ty = inner_declarator (ty, namep, optsp); 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. */
683 advance ();
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);
565 require (')'); 694 require (')');
566 break; 695 break;
567 696
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
568 default: 702 default:
569 parse_error ("expected '(', 'GTY', or an identifier, have %s", 703 parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
570 print_cur_token ()); 704 print_cur_token ());
571 /* Do _not_ advance if what we have is a close squiggle brace, as 705 /* Do _not_ advance if what we have is a close squiggle brace, as
572 we will get much better error recovery that way. */ 706 we will get much better error recovery that way. */
573 if (token () != '}') 707 if (token () != '}')
574 advance (); 708 advance ();
594 /* inner_declarator: 728 /* inner_declarator:
595 '*' inner_declarator 729 '*' inner_declarator
596 direct_declarator 730 direct_declarator
597 731
598 Mutually recursive subroutine of direct_declarator; do not use 732 Mutually recursive subroutine of direct_declarator; do not use
599 elsewhere. */ 733 elsewhere.
734
735 IN_STRUCT is true if we are called while parsing structures or classes. */
600 736
601 static type_p 737 static type_p
602 inner_declarator (type_p ty, const char **namep, options_p *optsp) 738 inner_declarator (type_p ty, const char **namep, options_p *optsp,
739 bool in_struct)
603 { 740 {
604 if (token () == '*') 741 if (token () == '*')
605 { 742 {
606 type_p inner; 743 type_p inner;
607 advance (); 744 advance ();
608 inner = inner_declarator (ty, namep, optsp); 745 inner = inner_declarator (ty, namep, optsp, in_struct);
609 if (inner == 0) 746 if (inner == 0)
610 return 0; 747 return 0;
611 else 748 else
612 return create_pointer (ty); 749 return create_pointer (ty);
613 } 750 }
614 else 751 else
615 return direct_declarator (ty, namep, optsp); 752 return direct_declarator (ty, namep, optsp, in_struct);
616 } 753 }
617 754
618 /* declarator: '*'+ direct_declarator 755 /* declarator: '*'+ direct_declarator
619 756
620 This is the sole public interface to this part of the grammar. 757 This is the sole public interface to this part of the grammar.
621 Arguments are the type known so far, a pointer to where the name 758 Arguments are the type known so far, a pointer to where the name
622 may be stored, and a pointer to where GTY options may be stored. 759 may be stored, and a pointer to where GTY options may be stored.
623 Returns the final type. */ 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. */
624 765
625 static type_p 766 static type_p
626 declarator (type_p ty, const char **namep, options_p *optsp) 767 declarator (type_p ty, const char **namep, options_p *optsp,
768 bool in_struct = false)
627 { 769 {
628 *namep = 0; 770 *namep = 0;
629 *optsp = 0; 771 *optsp = 0;
630 while (token () == '*') 772 while (token () == '*')
631 { 773 {
632 advance (); 774 advance ();
633 ty = create_pointer (ty); 775 ty = create_pointer (ty);
634 } 776 }
635 return direct_declarator (ty, namep, optsp); 777 return direct_declarator (ty, namep, optsp, in_struct);
636 } 778 }
637 779
638 /* Types and declarations. */ 780 /* Types and declarations. */
639 781
640 /* Structure field(s) declaration: 782 /* Structure field(s) declaration:
641 ( 783 (
642 type bitfield ';' 784 type bitfield ';'
643 | type declarator bitfield? ( ',' declarator bitfield? )+ ';' 785 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
644 )+ 786 )*
645 787
646 Knows that such declarations must end with a close brace (or, 788 Knows that such declarations must end with a close brace (or,
647 erroneously, at EOF). 789 erroneously, at EOF).
648 */ 790 */
649 static pair_p 791 static pair_p
653 type_p ty, dty; 795 type_p ty, dty;
654 options_p opts, dopts; 796 options_p opts, dopts;
655 const char *name; 797 const char *name;
656 bool another; 798 bool another;
657 799
658 do 800 while (token () != '}' && token () != EOF_TOKEN)
659 { 801 {
660 ty = type (&opts, true); 802 ty = type (&opts, true);
661 /* Another piece of the IFCVT_EXTRA_FIELDS special case, see type(). */ 803
662 if (!ty && token () == '}') 804 /* Ignore access-control keywords ("public:" etc). */
663 break; 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 }
664 814
665 if (!ty || token () == ':') 815 if (!ty || token () == ':')
666 { 816 {
667 consume_until_semi (false); 817 consume_until_eos ();
668 continue; 818 continue;
669 } 819 }
670 820
671 do 821 do
672 { 822 {
673 dty = declarator (ty, &name, &dopts); 823 dty = declarator (ty, &name, &dopts, true);
824
674 /* There could be any number of weird things after the declarator, 825 /* There could be any number of weird things after the declarator,
675 notably bitfield declarations and __attribute__s. If this 826 notably bitfield declarations and __attribute__s. If this
676 function returns true, the last thing was a comma, so we have 827 function returns true, the last thing was a comma, so we have
677 more than one declarator paired with the current type. */ 828 more than one declarator paired with the current type. */
678 another = consume_until_comma_or_semi (false); 829 another = consume_until_comma_or_eos ();
679 830
680 if (!dty) 831 if (!dty)
681 continue; 832 continue;
682 833
683 if (opts && dopts) 834 if (opts && dopts)
687 838
688 f = create_field_at (f, dty, name, dopts, &lexer_line); 839 f = create_field_at (f, dty, name, dopts, &lexer_line);
689 } 840 }
690 while (another); 841 while (another);
691 } 842 }
692 while (token () != '}' && token () != EOF_TOKEN);
693 return nreverse_pairs (f); 843 return nreverse_pairs (f);
694 } 844 }
845
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
695 857
696 /* This is called type(), but what it parses (sort of) is what C calls 858 /* This is called type(), but what it parses (sort of) is what C calls
697 declaration-specifiers and specifier-qualifier-list: 859 declaration-specifiers and specifier-qualifier-list:
698 860
699 SCALAR 861 SCALAR
702 | ENUM ID ( '{' ... '}' )? 864 | ENUM ID ( '{' ... '}' )?
703 865
704 Returns a partial type; under some conditions (notably 866 Returns a partial type; under some conditions (notably
705 "struct foo GTY((...)) thing;") it may write an options 867 "struct foo GTY((...)) thing;") it may write an options
706 structure to *OPTSP. 868 structure to *OPTSP.
707 */ 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
708 static type_p 875 static type_p
709 type (options_p *optsp, bool nested) 876 type (options_p *optsp, bool nested)
710 { 877 {
711 const char *s; 878 const char *s;
712 *optsp = 0; 879 *optsp = 0;
715 case SCALAR: 882 case SCALAR:
716 s = advance (); 883 s = advance ();
717 return create_scalar_type (s); 884 return create_scalar_type (s);
718 885
719 case ID: 886 case ID:
720 case VEC_TOKEN:
721 s = typedef_name (); 887 s = typedef_name ();
722 return resolve_typedef (s, &lexer_line); 888 return resolve_typedef (s, &lexer_line);
889
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;
723 895
724 case STRUCT: 896 case STRUCT:
725 case UNION: 897 case UNION:
726 { 898 {
899 type_p base_class = NULL;
727 options_p opts = 0; 900 options_p opts = 0;
728 /* GTY annotations follow attribute syntax 901 /* GTY annotations follow attribute syntax
729 GTY_BEFORE_ID is for union/struct declarations 902 GTY_BEFORE_ID is for union/struct declarations
730 GTY_AFTER_ID is for variable declarations. */ 903 GTY_AFTER_ID is for variable declarations. */
731 enum 904 enum
732 { 905 {
733 NO_GTY, 906 NO_GTY,
734 GTY_BEFORE_ID, 907 GTY_BEFORE_ID,
735 GTY_AFTER_ID 908 GTY_AFTER_ID
736 } is_gty = NO_GTY; 909 } is_gty = NO_GTY;
737 bool is_union = (token () == UNION); 910 enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
738 advance (); 911 advance ();
739 912
740 /* Top-level structures that are not explicitly tagged GTY(()) 913 /* Top-level structures that are not explicitly tagged GTY(())
741 are treated as mere forward declarations. This is because 914 are treated as mere forward declarations. This is because
742 there are a lot of structures that we don't need to know 915 there are a lot of structures that we don't need to know
743 about, and some of those have weird macro stuff in them 916 about, and some of those have C++ and macro constructs that
744 that we can't handle. */ 917 we cannot handle. */
745 if (nested || token () == GTY_TOKEN) 918 if (nested || token () == GTY_TOKEN)
746 { 919 {
747 is_gty = GTY_BEFORE_ID; 920 is_gty = GTY_BEFORE_ID;
748 opts = gtymarker_opt (); 921 opts = gtymarker_opt ();
749 } 922 }
761 { 934 {
762 is_gty = GTY_AFTER_ID; 935 is_gty = GTY_AFTER_ID;
763 opts = gtymarker_opt (); 936 opts = gtymarker_opt ();
764 } 937 }
765 938
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. */
951 if (0 == strcmp (basename, "public")
952 || 0 == strcmp (basename, "protected")
953 || 0 == strcmp (basename, "private"))
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
766 if (is_gty) 970 if (is_gty)
767 { 971 {
768 if (token () == '{') 972 if (token () == '{')
769 { 973 {
770 pair_p fields; 974 pair_p fields;
771 975
772 if (is_gty == GTY_AFTER_ID) 976 if (is_gty == GTY_AFTER_ID)
773 parse_error ("GTY must be specified before identifier"); 977 parse_error ("GTY must be specified before identifier");
774 978
775 advance (); 979 if (!is_user_gty)
776 fields = struct_field_seq (); 980 {
777 require ('}'); 981 advance ();
778 return new_structure (s, is_union, &lexer_line, fields, opts); 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);
779 } 996 }
780 } 997 }
781 else if (token () == '{') 998 else if (token () == '{')
782 consume_balanced ('{', '}'); 999 consume_balanced ('{', '}');
783 if (opts) 1000 if (opts)
784 *optsp = opts; 1001 *optsp = opts;
785 return find_structure (s, is_union); 1002 return find_structure (s, kind);
786 } 1003 }
1004
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;
787 1019
788 case ENUM: 1020 case ENUM:
789 advance (); 1021 advance ();
790 if (token () == ID) 1022 if (token () == ID)
791 s = advance (); 1023 s = advance ();
794 get_input_file_name (lexer_line.file), 1026 get_input_file_name (lexer_line.file),
795 lexer_line.line); 1027 lexer_line.line);
796 1028
797 if (token () == '{') 1029 if (token () == '{')
798 consume_balanced ('{', '}'); 1030 consume_balanced ('{', '}');
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
799 return create_scalar_type (s); 1048 return create_scalar_type (s);
800 1049
801 default: 1050 default:
802 parse_error ("expected a type specifier, have %s", print_cur_token ()); 1051 parse_error ("expected a type specifier, have %s", print_cur_token ());
803 advance (); 1052 advance ();
831 if (opts) 1080 if (opts)
832 parse_error ("GTY((...)) cannot be applied to a typedef"); 1081 parse_error ("GTY((...)) cannot be applied to a typedef");
833 1082
834 /* Yet another place where we could have junk (notably attributes) 1083 /* Yet another place where we could have junk (notably attributes)
835 after the declarator. */ 1084 after the declarator. */
836 another = consume_until_comma_or_semi (false); 1085 another = consume_until_comma_or_eos ();
837 if (dty) 1086 if (dty)
838 do_typedef (name, dty, &lexer_line); 1087 do_typedef (name, dty, &lexer_line);
839 } 1088 }
840 while (another); 1089 while (another);
841 } 1090 }
888 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line); 1137 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
889 require2 (';', '='); 1138 require2 (';', '=');
890 } 1139 }
891 } 1140 }
892 1141
893 /* Definition of a generic VEC structure:
894
895 'DEF_VEC_[IPO]' '(' id ')' ';'
896
897 Scalar VECs require slightly different treatment than otherwise -
898 that's handled in note_def_vec, we just pass it along.*/
899 static void
900 def_vec (void)
901 {
902 bool is_scalar = (token () == DEFVEC_I);
903 const char *type;
904
905 require2 (DEFVEC_OP, DEFVEC_I);
906 require ('(');
907 type = require2 (ID, SCALAR);
908 require (')');
909 require (';');
910
911 if (!type)
912 return;
913
914 note_def_vec (type, is_scalar, &lexer_line);
915 note_def_vec_alloc (type, "none", &lexer_line);
916 }
917
918 /* Definition of an allocation strategy for a VEC structure:
919
920 'DEF_VEC_ALLOC_[IPO]' '(' id ',' id ')' ';'
921
922 For purposes of gengtype, this just declares a wrapper structure. */
923 static void
924 def_vec_alloc (void)
925 {
926 const char *type, *astrat;
927
928 require (DEFVEC_ALLOC);
929 require ('(');
930 type = require2 (ID, SCALAR);
931 require (',');
932 astrat = require (ID);
933 require (')');
934 require (';');
935
936 if (!type || !astrat)
937 return;
938
939 note_def_vec_alloc (type, astrat, &lexer_line);
940 }
941
942 /* Parse the file FNAME for GC-relevant declarations and definitions. 1142 /* Parse the file FNAME for GC-relevant declarations and definitions.
943 This is the only entry point to this file. */ 1143 This is the only entry point to this file. */
944 void 1144 void
945 parse_file (const char *fname) 1145 parse_file (const char *fname)
946 { 1146 {
961 1161
962 case TYPEDEF: 1162 case TYPEDEF:
963 typedef_decl (); 1163 typedef_decl ();
964 break; 1164 break;
965 1165
966 case DEFVEC_OP:
967 case DEFVEC_I:
968 def_vec ();
969 break;
970
971 case DEFVEC_ALLOC:
972 def_vec_alloc ();
973 break;
974
975 case EOF_TOKEN: 1166 case EOF_TOKEN:
976 goto eof; 1167 goto eof;
977 1168
978 default: 1169 default:
979 parse_error ("unexpected top level token, %s", print_cur_token ()); 1170 parse_error ("unexpected top level token, %s", print_cur_token ());