13ad841b2Smrg /* Declarations for the parser for C and Objective-C.
2*4c3eb207Smrg Copyright (C) 1987-2020 Free Software Foundation, Inc.
33ad841b2Smrg
43ad841b2Smrg Parser actions based on the old Bison parser; structure somewhat
53ad841b2Smrg influenced by and fragments based on the C++ parser.
63ad841b2Smrg
73ad841b2Smrg This file is part of GCC.
83ad841b2Smrg
93ad841b2Smrg GCC is free software; you can redistribute it and/or modify it under
103ad841b2Smrg the terms of the GNU General Public License as published by the Free
113ad841b2Smrg Software Foundation; either version 3, or (at your option) any later
123ad841b2Smrg version.
133ad841b2Smrg
143ad841b2Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
153ad841b2Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
163ad841b2Smrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
173ad841b2Smrg for more details.
183ad841b2Smrg
193ad841b2Smrg You should have received a copy of the GNU General Public License
203ad841b2Smrg along with GCC; see the file COPYING3. If not see
213ad841b2Smrg <http://www.gnu.org/licenses/>. */
223ad841b2Smrg
233ad841b2Smrg #ifndef GCC_C_PARSER_H
243ad841b2Smrg #define GCC_C_PARSER_H
253ad841b2Smrg
263ad841b2Smrg /* The C lexer intermediates between the lexer in cpplib and c-lex.c
273ad841b2Smrg and the C parser. Unlike the C++ lexer, the parser structure
283ad841b2Smrg stores the lexer information instead of using a separate structure.
293ad841b2Smrg Identifiers are separated into ordinary identifiers, type names,
303ad841b2Smrg keywords and some other Objective-C types of identifiers, and some
313ad841b2Smrg look-ahead is maintained.
323ad841b2Smrg
333ad841b2Smrg ??? It might be a good idea to lex the whole file up front (as for
343ad841b2Smrg C++). It would then be possible to share more of the C and C++
353ad841b2Smrg lexer code, if desired. */
363ad841b2Smrg
373ad841b2Smrg /* More information about the type of a CPP_NAME token. */
383ad841b2Smrg enum c_id_kind {
393ad841b2Smrg /* An ordinary identifier. */
403ad841b2Smrg C_ID_ID,
413ad841b2Smrg /* An identifier declared as a typedef name. */
423ad841b2Smrg C_ID_TYPENAME,
433ad841b2Smrg /* An identifier declared as an Objective-C class name. */
443ad841b2Smrg C_ID_CLASSNAME,
453ad841b2Smrg /* An address space identifier. */
463ad841b2Smrg C_ID_ADDRSPACE,
473ad841b2Smrg /* Not an identifier. */
483ad841b2Smrg C_ID_NONE
493ad841b2Smrg };
503ad841b2Smrg
513ad841b2Smrg /* A single C token after string literal concatenation and conversion
523ad841b2Smrg of preprocessing tokens to tokens. */
533ad841b2Smrg struct GTY (()) c_token {
543ad841b2Smrg /* The kind of token. */
553ad841b2Smrg ENUM_BITFIELD (cpp_ttype) type : 8;
563ad841b2Smrg /* If this token is a CPP_NAME, this value indicates whether also
573ad841b2Smrg declared as some kind of type. Otherwise, it is C_ID_NONE. */
583ad841b2Smrg ENUM_BITFIELD (c_id_kind) id_kind : 8;
593ad841b2Smrg /* If this token is a keyword, this value indicates which keyword.
603ad841b2Smrg Otherwise, this value is RID_MAX. */
613ad841b2Smrg ENUM_BITFIELD (rid) keyword : 8;
623ad841b2Smrg /* If this token is a CPP_PRAGMA, this indicates the pragma that
633ad841b2Smrg was seen. Otherwise it is PRAGMA_NONE. */
643ad841b2Smrg ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
653ad841b2Smrg /* The location at which this token was found. */
663ad841b2Smrg location_t location;
673ad841b2Smrg /* The value associated with this token, if any. */
683ad841b2Smrg tree value;
693ad841b2Smrg /* Token flags. */
703ad841b2Smrg unsigned char flags;
713ad841b2Smrg
get_rangec_token723ad841b2Smrg source_range get_range () const
733ad841b2Smrg {
743ad841b2Smrg return get_range_from_loc (line_table, location);
753ad841b2Smrg }
763ad841b2Smrg
get_finishc_token773ad841b2Smrg location_t get_finish () const
783ad841b2Smrg {
793ad841b2Smrg return get_range ().m_finish;
803ad841b2Smrg }
813ad841b2Smrg };
823ad841b2Smrg
833ad841b2Smrg /* The parser. */
843ad841b2Smrg struct c_parser;
853ad841b2Smrg
863ad841b2Smrg /* Possibly kinds of declarator to parse. */
873ad841b2Smrg enum c_dtr_syn {
883ad841b2Smrg /* A normal declarator with an identifier. */
893ad841b2Smrg C_DTR_NORMAL,
903ad841b2Smrg /* An abstract declarator (maybe empty). */
913ad841b2Smrg C_DTR_ABSTRACT,
923ad841b2Smrg /* A parameter declarator: may be either, but after a type name does
933ad841b2Smrg not redeclare a typedef name as an identifier if it can
943ad841b2Smrg alternatively be interpreted as a typedef name; see DR#009,
953ad841b2Smrg applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
963ad841b2Smrg following DR#249. For example, given a typedef T, "int T" and
973ad841b2Smrg "int *T" are valid parameter declarations redeclaring T, while
983ad841b2Smrg "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
993ad841b2Smrg abstract declarators rather than involving redundant parentheses;
1003ad841b2Smrg the same applies with attributes inside the parentheses before
1013ad841b2Smrg "T". */
1023ad841b2Smrg C_DTR_PARM
1033ad841b2Smrg };
1043ad841b2Smrg
1053ad841b2Smrg /* The binary operation precedence levels, where 0 is a dummy lowest level
1063ad841b2Smrg used for the bottom of the stack. */
1073ad841b2Smrg enum c_parser_prec {
1083ad841b2Smrg PREC_NONE,
1093ad841b2Smrg PREC_LOGOR,
1103ad841b2Smrg PREC_LOGAND,
1113ad841b2Smrg PREC_BITOR,
1123ad841b2Smrg PREC_BITXOR,
1133ad841b2Smrg PREC_BITAND,
1143ad841b2Smrg PREC_EQ,
1153ad841b2Smrg PREC_REL,
1163ad841b2Smrg PREC_SHIFT,
1173ad841b2Smrg PREC_ADD,
1183ad841b2Smrg PREC_MULT,
1193ad841b2Smrg NUM_PRECS
1203ad841b2Smrg };
1213ad841b2Smrg
1223ad841b2Smrg enum c_lookahead_kind {
1233ad841b2Smrg /* Always treat unknown identifiers as typenames. */
1243ad841b2Smrg cla_prefer_type,
1253ad841b2Smrg
1263ad841b2Smrg /* Could be parsing a nonabstract declarator. Only treat an identifier
1273ad841b2Smrg as a typename if followed by another identifier or a star. */
1283ad841b2Smrg cla_nonabstract_decl,
1293ad841b2Smrg
1303ad841b2Smrg /* Never treat identifiers as typenames. */
1313ad841b2Smrg cla_prefer_id
1323ad841b2Smrg };
1333ad841b2Smrg
1343ad841b2Smrg
1353ad841b2Smrg extern c_token * c_parser_peek_token (c_parser *parser);
1363ad841b2Smrg extern c_token * c_parser_peek_2nd_token (c_parser *parser);
1373ad841b2Smrg extern c_token * c_parser_peek_nth_token (c_parser *parser, unsigned int n);
1383ad841b2Smrg extern bool c_parser_require (c_parser *parser, enum cpp_ttype type,
139cef8759bSmrg const char *msgid,
140cef8759bSmrg location_t matching_location = UNKNOWN_LOCATION,
141cef8759bSmrg bool type_is_unique=true);
142cef8759bSmrg extern bool c_parser_error (c_parser *parser, const char *gmsgid);
1433ad841b2Smrg extern void c_parser_consume_token (c_parser *parser);
1443ad841b2Smrg extern void c_parser_skip_until_found (c_parser *parser, enum cpp_ttype type,
145cef8759bSmrg const char *msgid,
146cef8759bSmrg location_t = UNKNOWN_LOCATION);
1473ad841b2Smrg extern bool c_parser_next_token_starts_declspecs (c_parser *parser);
1483ad841b2Smrg bool c_parser_next_tokens_start_declaration (c_parser *parser);
1493ad841b2Smrg bool c_token_starts_typename (c_token *token);
1503ad841b2Smrg
1513ad841b2Smrg /* Abstraction to avoid defining c_parser here which messes up gengtype
1523ad841b2Smrg output wrt ObjC due to vec<c_token> routines being put in gtype-c.h
1533ad841b2Smrg but not gtype-objc.h. */
1543ad841b2Smrg extern c_token * c_parser_tokens_buf (c_parser *parser, unsigned n);
1553ad841b2Smrg extern bool c_parser_error (c_parser *parser);
1563ad841b2Smrg extern void c_parser_set_error (c_parser *parser, bool);
1573ad841b2Smrg
158627f7eb2Smrg /* A bit of a hack to have this here. It would be better in a c-decl.h. */
159627f7eb2Smrg extern bool old_style_parameter_scope (void);
160627f7eb2Smrg
1613ad841b2Smrg /* Return true if the next token from PARSER has the indicated
1623ad841b2Smrg TYPE. */
1633ad841b2Smrg
1643ad841b2Smrg static inline bool
c_parser_next_token_is(c_parser * parser,enum cpp_ttype type)1653ad841b2Smrg c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
1663ad841b2Smrg {
1673ad841b2Smrg return c_parser_peek_token (parser)->type == type;
1683ad841b2Smrg }
1693ad841b2Smrg
1703ad841b2Smrg /* Return true if the next token from PARSER does not have the
1713ad841b2Smrg indicated TYPE. */
1723ad841b2Smrg
1733ad841b2Smrg static inline bool
c_parser_next_token_is_not(c_parser * parser,enum cpp_ttype type)1743ad841b2Smrg c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
1753ad841b2Smrg {
1763ad841b2Smrg return !c_parser_next_token_is (parser, type);
1773ad841b2Smrg }
1783ad841b2Smrg
1793ad841b2Smrg /* Return true if the next token from PARSER is the indicated
1803ad841b2Smrg KEYWORD. */
1813ad841b2Smrg
1823ad841b2Smrg static inline bool
c_parser_next_token_is_keyword(c_parser * parser,enum rid keyword)1833ad841b2Smrg c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
1843ad841b2Smrg {
1853ad841b2Smrg return c_parser_peek_token (parser)->keyword == keyword;
1863ad841b2Smrg }
1873ad841b2Smrg
188*4c3eb207Smrg struct c_expr c_parser_string_literal (c_parser *, bool, bool);
1893ad841b2Smrg extern struct c_declarator *
1903ad841b2Smrg c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
1913ad841b2Smrg bool *seen_id);
1923ad841b2Smrg extern void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
193*4c3eb207Smrg bool, bool, bool, bool, bool,
194*4c3eb207Smrg enum c_lookahead_kind);
195cef8759bSmrg extern struct c_type_name *c_parser_type_name (c_parser *, bool = false);
1963ad841b2Smrg
1973ad841b2Smrg #endif
198