xref: /netbsd-src/external/gpl3/gcc/dist/gcc/c/c-parser.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1b17d1066Smrg /* Declarations for the parser for C and Objective-C.
2*b1e83836Smrg    Copyright (C) 1987-2022 Free Software Foundation, Inc.
3b17d1066Smrg 
4b17d1066Smrg    Parser actions based on the old Bison parser; structure somewhat
5b17d1066Smrg    influenced by and fragments based on the C++ parser.
6b17d1066Smrg 
7b17d1066Smrg This file is part of GCC.
8b17d1066Smrg 
9b17d1066Smrg GCC is free software; you can redistribute it and/or modify it under
10b17d1066Smrg the terms of the GNU General Public License as published by the Free
11b17d1066Smrg Software Foundation; either version 3, or (at your option) any later
12b17d1066Smrg version.
13b17d1066Smrg 
14b17d1066Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15b17d1066Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
16b17d1066Smrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17b17d1066Smrg for more details.
18b17d1066Smrg 
19b17d1066Smrg You should have received a copy of the GNU General Public License
20b17d1066Smrg along with GCC; see the file COPYING3.  If not see
21b17d1066Smrg <http://www.gnu.org/licenses/>.  */
22b17d1066Smrg 
23b17d1066Smrg #ifndef GCC_C_PARSER_H
24b17d1066Smrg #define GCC_C_PARSER_H
25b17d1066Smrg 
26*b1e83836Smrg /* The C lexer intermediates between the lexer in cpplib and c-lex.cc
27b17d1066Smrg    and the C parser.  Unlike the C++ lexer, the parser structure
28b17d1066Smrg    stores the lexer information instead of using a separate structure.
29b17d1066Smrg    Identifiers are separated into ordinary identifiers, type names,
30b17d1066Smrg    keywords and some other Objective-C types of identifiers, and some
31b17d1066Smrg    look-ahead is maintained.
32b17d1066Smrg 
33b17d1066Smrg    ??? It might be a good idea to lex the whole file up front (as for
34b17d1066Smrg    C++).  It would then be possible to share more of the C and C++
35b17d1066Smrg    lexer code, if desired.  */
36b17d1066Smrg 
37b17d1066Smrg /* More information about the type of a CPP_NAME token.  */
38b17d1066Smrg enum c_id_kind {
39b17d1066Smrg   /* An ordinary identifier.  */
40b17d1066Smrg   C_ID_ID,
41b17d1066Smrg   /* An identifier declared as a typedef name.  */
42b17d1066Smrg   C_ID_TYPENAME,
43b17d1066Smrg   /* An identifier declared as an Objective-C class name.  */
44b17d1066Smrg   C_ID_CLASSNAME,
45b17d1066Smrg   /* An address space identifier.  */
46b17d1066Smrg   C_ID_ADDRSPACE,
47b17d1066Smrg   /* Not an identifier.  */
48b17d1066Smrg   C_ID_NONE
49b17d1066Smrg };
50b17d1066Smrg 
51b17d1066Smrg /* A single C token after string literal concatenation and conversion
52b17d1066Smrg    of preprocessing tokens to tokens.  */
53b17d1066Smrg struct GTY (()) c_token {
54b17d1066Smrg   /* The kind of token.  */
55b17d1066Smrg   ENUM_BITFIELD (cpp_ttype) type : 8;
56b17d1066Smrg   /* If this token is a CPP_NAME, this value indicates whether also
57b17d1066Smrg      declared as some kind of type.  Otherwise, it is C_ID_NONE.  */
58b17d1066Smrg   ENUM_BITFIELD (c_id_kind) id_kind : 8;
59b17d1066Smrg   /* If this token is a keyword, this value indicates which keyword.
60b17d1066Smrg      Otherwise, this value is RID_MAX.  */
61b17d1066Smrg   ENUM_BITFIELD (rid) keyword : 8;
62b17d1066Smrg   /* If this token is a CPP_PRAGMA, this indicates the pragma that
63b17d1066Smrg      was seen.  Otherwise it is PRAGMA_NONE.  */
64b17d1066Smrg   ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
65b17d1066Smrg   /* The location at which this token was found.  */
66b17d1066Smrg   location_t location;
67b17d1066Smrg   /* The value associated with this token, if any.  */
68b17d1066Smrg   tree value;
69b17d1066Smrg   /* Token flags.  */
70b17d1066Smrg   unsigned char flags;
71b17d1066Smrg 
get_rangec_token72b17d1066Smrg   source_range get_range () const
73b17d1066Smrg   {
74b17d1066Smrg     return get_range_from_loc (line_table, location);
75b17d1066Smrg   }
76b17d1066Smrg 
get_finishc_token77b17d1066Smrg   location_t get_finish () const
78b17d1066Smrg   {
79b17d1066Smrg     return get_range ().m_finish;
80b17d1066Smrg   }
81b17d1066Smrg };
82b17d1066Smrg 
83b17d1066Smrg /* The parser.  */
84b17d1066Smrg struct c_parser;
85b17d1066Smrg 
86b17d1066Smrg /* Possibly kinds of declarator to parse.  */
87b17d1066Smrg enum c_dtr_syn {
88b17d1066Smrg   /* A normal declarator with an identifier.  */
89b17d1066Smrg   C_DTR_NORMAL,
90b17d1066Smrg   /* An abstract declarator (maybe empty).  */
91b17d1066Smrg   C_DTR_ABSTRACT,
92b17d1066Smrg   /* A parameter declarator: may be either, but after a type name does
93b17d1066Smrg      not redeclare a typedef name as an identifier if it can
94b17d1066Smrg      alternatively be interpreted as a typedef name; see DR#009,
95b17d1066Smrg      applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
96b17d1066Smrg      following DR#249.  For example, given a typedef T, "int T" and
97b17d1066Smrg      "int *T" are valid parameter declarations redeclaring T, while
98b17d1066Smrg      "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
99b17d1066Smrg      abstract declarators rather than involving redundant parentheses;
100b17d1066Smrg      the same applies with attributes inside the parentheses before
101b17d1066Smrg      "T".  */
102b17d1066Smrg   C_DTR_PARM
103b17d1066Smrg };
104b17d1066Smrg 
105b17d1066Smrg /* The binary operation precedence levels, where 0 is a dummy lowest level
106b17d1066Smrg    used for the bottom of the stack.  */
107b17d1066Smrg enum c_parser_prec {
108b17d1066Smrg   PREC_NONE,
109b17d1066Smrg   PREC_LOGOR,
110b17d1066Smrg   PREC_LOGAND,
111b17d1066Smrg   PREC_BITOR,
112b17d1066Smrg   PREC_BITXOR,
113b17d1066Smrg   PREC_BITAND,
114b17d1066Smrg   PREC_EQ,
115b17d1066Smrg   PREC_REL,
116b17d1066Smrg   PREC_SHIFT,
117b17d1066Smrg   PREC_ADD,
118b17d1066Smrg   PREC_MULT,
119b17d1066Smrg   NUM_PRECS
120b17d1066Smrg };
121b17d1066Smrg 
122b17d1066Smrg enum c_lookahead_kind {
123b17d1066Smrg   /* Always treat unknown identifiers as typenames.  */
124b17d1066Smrg   cla_prefer_type,
125b17d1066Smrg 
126b17d1066Smrg   /* Could be parsing a nonabstract declarator.  Only treat an identifier
127b17d1066Smrg      as a typename if followed by another identifier or a star.  */
128b17d1066Smrg   cla_nonabstract_decl,
129b17d1066Smrg 
130b17d1066Smrg   /* Never treat identifiers as typenames.  */
131b17d1066Smrg   cla_prefer_id
132b17d1066Smrg };
133b17d1066Smrg 
134b17d1066Smrg 
135b17d1066Smrg extern c_token * c_parser_peek_token (c_parser *parser);
136b17d1066Smrg extern c_token * c_parser_peek_2nd_token (c_parser *parser);
137b17d1066Smrg extern c_token * c_parser_peek_nth_token (c_parser *parser, unsigned int n);
138b17d1066Smrg extern bool c_parser_require (c_parser *parser, enum cpp_ttype type,
139a3e9eb18Smrg 			      const char *msgid,
140a3e9eb18Smrg 			      location_t matching_location = UNKNOWN_LOCATION,
141a3e9eb18Smrg 			      bool type_is_unique=true);
142a3e9eb18Smrg extern bool c_parser_error (c_parser *parser, const char *gmsgid);
143b17d1066Smrg extern void c_parser_consume_token (c_parser *parser);
144b17d1066Smrg extern void c_parser_skip_until_found (c_parser *parser, enum cpp_ttype type,
145a3e9eb18Smrg 				       const char *msgid,
146a3e9eb18Smrg 				       location_t = UNKNOWN_LOCATION);
147b17d1066Smrg extern bool c_parser_next_token_starts_declspecs (c_parser *parser);
148b17d1066Smrg bool c_parser_next_tokens_start_declaration (c_parser *parser);
149b17d1066Smrg bool c_token_starts_typename (c_token *token);
150b17d1066Smrg 
151b17d1066Smrg /* Abstraction to avoid defining c_parser here which messes up gengtype
152b17d1066Smrg    output wrt ObjC due to vec<c_token> routines being put in gtype-c.h
153b17d1066Smrg    but not gtype-objc.h.  */
154b17d1066Smrg extern c_token * c_parser_tokens_buf (c_parser *parser, unsigned n);
155b17d1066Smrg extern bool c_parser_error (c_parser *parser);
156b17d1066Smrg extern void c_parser_set_error (c_parser *parser, bool);
157b17d1066Smrg 
158181254a7Smrg /* A bit of a hack to have this here.  It would be better in a c-decl.h.  */
159181254a7Smrg extern bool old_style_parameter_scope (void);
160181254a7Smrg 
161b17d1066Smrg /* Return true if the next token from PARSER has the indicated
162b17d1066Smrg    TYPE.  */
163b17d1066Smrg 
164b17d1066Smrg static inline bool
c_parser_next_token_is(c_parser * parser,enum cpp_ttype type)165b17d1066Smrg c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
166b17d1066Smrg {
167b17d1066Smrg   return c_parser_peek_token (parser)->type == type;
168b17d1066Smrg }
169b17d1066Smrg 
170b17d1066Smrg /* Return true if the next token from PARSER does not have the
171b17d1066Smrg    indicated TYPE.  */
172b17d1066Smrg 
173b17d1066Smrg static inline bool
c_parser_next_token_is_not(c_parser * parser,enum cpp_ttype type)174b17d1066Smrg c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
175b17d1066Smrg {
176b17d1066Smrg   return !c_parser_next_token_is (parser, type);
177b17d1066Smrg }
178b17d1066Smrg 
179b17d1066Smrg /* Return true if the next token from PARSER is the indicated
180b17d1066Smrg    KEYWORD.  */
181b17d1066Smrg 
182b17d1066Smrg static inline bool
c_parser_next_token_is_keyword(c_parser * parser,enum rid keyword)183b17d1066Smrg c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
184b17d1066Smrg {
185b17d1066Smrg   return c_parser_peek_token (parser)->keyword == keyword;
186b17d1066Smrg }
187b17d1066Smrg 
188ad640425Smrg struct c_expr c_parser_string_literal (c_parser *, bool, bool);
189b17d1066Smrg extern struct c_declarator *
190b17d1066Smrg c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
191b17d1066Smrg 		     bool *seen_id);
192b17d1066Smrg extern void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
193ad640425Smrg 				bool, bool, bool, bool, bool,
194ad640425Smrg 				enum c_lookahead_kind);
195a3e9eb18Smrg extern struct c_type_name *c_parser_type_name (c_parser *, bool = false);
196b17d1066Smrg 
197b17d1066Smrg #endif
198