parser.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* Data structures and function exported by the C++ Parser.
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_CP_PARSER_H
  16. #define GCC_CP_PARSER_H
  17. #include "tree.h"
  18. #include "cp/cp-tree.h"
  19. #include "c-family/c-pragma.h"
  20. /* A token's value and its associated deferred access checks and
  21. qualifying scope. */
  22. struct GTY(()) tree_check {
  23. /* The value associated with the token. */
  24. tree value;
  25. /* The checks that have been associated with value. */
  26. vec<deferred_access_check, va_gc> *checks;
  27. /* The token's qualifying scope (used when it is a
  28. CPP_NESTED_NAME_SPECIFIER). */
  29. tree qualifying_scope;
  30. };
  31. /* A C++ token. */
  32. typedef struct GTY (()) cp_token {
  33. /* The kind of token. */
  34. ENUM_BITFIELD (cpp_ttype) type : 8;
  35. /* If this token is a keyword, this value indicates which keyword.
  36. Otherwise, this value is RID_MAX. */
  37. ENUM_BITFIELD (rid) keyword : 8;
  38. /* Token flags. */
  39. unsigned char flags;
  40. /* Identifier for the pragma. */
  41. ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
  42. /* True if this token is from a context where it is implicitly extern "C" */
  43. BOOL_BITFIELD implicit_extern_c : 1;
  44. /* True if an error has already been reported for this token, such as a
  45. CPP_NAME token that is not a keyword (i.e., for which KEYWORD is
  46. RID_MAX) iff this name was looked up and found to be ambiguous. */
  47. BOOL_BITFIELD error_reported : 1;
  48. /* True for a token that has been purged. If a token is purged,
  49. it is no longer a valid token and it should be considered
  50. deleted. */
  51. BOOL_BITFIELD purged_p : 1;
  52. /* The location at which this token was found. */
  53. location_t location;
  54. /* The value associated with this token, if any. */
  55. union cp_token_value {
  56. /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
  57. struct tree_check* GTY((tag ("1"))) tree_check_value;
  58. /* Use for all other tokens. */
  59. tree GTY((tag ("0"))) value;
  60. } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
  61. } cp_token;
  62. /* We use a stack of token pointer for saving token sets. */
  63. typedef struct cp_token *cp_token_position;
  64. /* The cp_lexer structure represents the C++ lexer. It is responsible
  65. for managing the token stream from the preprocessor and supplying
  66. it to the parser. Tokens are never added to the cp_lexer after
  67. it is created. */
  68. typedef struct GTY (()) cp_lexer {
  69. /* The memory allocated for the buffer. NULL if this lexer does not
  70. own the token buffer. */
  71. vec<cp_token, va_gc> *buffer;
  72. /* A pointer just past the last available token. The tokens
  73. in this lexer are [buffer, last_token). */
  74. cp_token_position GTY ((skip)) last_token;
  75. /* The next available token. If NEXT_TOKEN is &eof_token, then there are
  76. no more available tokens. */
  77. cp_token_position GTY ((skip)) next_token;
  78. /* A stack indicating positions at which cp_lexer_save_tokens was
  79. called. The top entry is the most recent position at which we
  80. began saving tokens. If the stack is non-empty, we are saving
  81. tokens. */
  82. vec<cp_token_position> GTY ((skip)) saved_tokens;
  83. /* The next lexer in a linked list of lexers. */
  84. struct cp_lexer *next;
  85. /* True if we should output debugging information. */
  86. bool debugging_p;
  87. /* True if we're in the context of parsing a pragma, and should not
  88. increment past the end-of-line marker. */
  89. bool in_pragma;
  90. } cp_lexer;
  91. /* cp_token_cache is a range of tokens. There is no need to represent
  92. allocate heap memory for it, since tokens are never removed from the
  93. lexer's array. There is also no need for the GC to walk through
  94. a cp_token_cache, since everything in here is referenced through
  95. a lexer. */
  96. typedef struct GTY(()) cp_token_cache {
  97. /* The beginning of the token range. */
  98. cp_token * GTY((skip)) first;
  99. /* Points immediately after the last token in the range. */
  100. cp_token * GTY ((skip)) last;
  101. } cp_token_cache;
  102. typedef cp_token_cache *cp_token_cache_ptr;
  103. struct cp_token_ident_d
  104. {
  105. unsigned int ident_len;
  106. const char *ident_str;
  107. unsigned int before_len;
  108. const char *before_str;
  109. unsigned int after_len;
  110. const char *after_str;
  111. };
  112. typedef struct cp_token_ident_d cp_token_ident;
  113. /* An entry in a queue of function arguments that require post-processing. */
  114. typedef struct GTY(()) cp_default_arg_entry_d {
  115. /* The current_class_type when we parsed this arg. */
  116. tree class_type;
  117. /* The function decl itself. */
  118. tree decl;
  119. } cp_default_arg_entry;
  120. /* An entry in a stack for member functions defined within their classes. */
  121. typedef struct GTY(()) cp_unparsed_functions_entry_d {
  122. /* Functions with default arguments that require post-processing.
  123. Functions appear in this list in declaration order. */
  124. vec<cp_default_arg_entry, va_gc> *funs_with_default_args;
  125. /* Functions with defintions that require post-processing. Functions
  126. appear in this list in declaration order. */
  127. vec<tree, va_gc> *funs_with_definitions;
  128. /* Non-static data members with initializers that require post-processing.
  129. FIELD_DECLs appear in this list in declaration order. */
  130. vec<tree, va_gc> *nsdmis;
  131. /* Nested classes go in this vector, so that we can do some final
  132. processing after parsing any NSDMIs. */
  133. vec<tree, va_gc> *classes;
  134. } cp_unparsed_functions_entry;
  135. /* The status of a tentative parse. */
  136. typedef enum cp_parser_status_kind
  137. {
  138. /* No errors have occurred. */
  139. CP_PARSER_STATUS_KIND_NO_ERROR,
  140. /* An error has occurred. */
  141. CP_PARSER_STATUS_KIND_ERROR,
  142. /* We are committed to this tentative parse, whether or not an error
  143. has occurred. */
  144. CP_PARSER_STATUS_KIND_COMMITTED
  145. } cp_parser_status_kind;
  146. /* Context that is saved and restored when parsing tentatively. */
  147. typedef struct GTY (()) cp_parser_context {
  148. /* If this is a tentative parsing context, the status of the
  149. tentative parse. */
  150. enum cp_parser_status_kind status;
  151. /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
  152. that are looked up in this context must be looked up both in the
  153. scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
  154. the context of the containing expression. */
  155. tree object_type;
  156. /* The next parsing context in the stack. */
  157. struct cp_parser_context *next;
  158. } cp_parser_context;
  159. /* Control structure for #pragma omp declare simd parsing. */
  160. struct cp_omp_declare_simd_data {
  161. bool error_seen; /* Set if error has been reported. */
  162. bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
  163. vec<cp_token_cache_ptr> tokens;
  164. };
  165. /* The cp_parser structure represents the C++ parser. */
  166. typedef struct GTY(()) cp_parser {
  167. /* The lexer from which we are obtaining tokens. */
  168. cp_lexer *lexer;
  169. /* The scope in which names should be looked up. If NULL_TREE, then
  170. we look up names in the scope that is currently open in the
  171. source program. If non-NULL, this is either a TYPE or
  172. NAMESPACE_DECL for the scope in which we should look. It can
  173. also be ERROR_MARK, when we've parsed a bogus scope.
  174. This value is not cleared automatically after a name is looked
  175. up, so we must be careful to clear it before starting a new look
  176. up sequence. (If it is not cleared, then `X::Y' followed by `Z'
  177. will look up `Z' in the scope of `X', rather than the current
  178. scope.) Unfortunately, it is difficult to tell when name lookup
  179. is complete, because we sometimes peek at a token, look it up,
  180. and then decide not to consume it. */
  181. tree scope;
  182. /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
  183. last lookup took place. OBJECT_SCOPE is used if an expression
  184. like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
  185. respectively. QUALIFYING_SCOPE is used for an expression of the
  186. form "X::Y"; it refers to X. */
  187. tree object_scope;
  188. tree qualifying_scope;
  189. /* A stack of parsing contexts. All but the bottom entry on the
  190. stack will be tentative contexts.
  191. We parse tentatively in order to determine which construct is in
  192. use in some situations. For example, in order to determine
  193. whether a statement is an expression-statement or a
  194. declaration-statement we parse it tentatively as a
  195. declaration-statement. If that fails, we then reparse the same
  196. token stream as an expression-statement. */
  197. cp_parser_context *context;
  198. /* True if we are parsing GNU C++. If this flag is not set, then
  199. GNU extensions are not recognized. */
  200. bool allow_gnu_extensions_p;
  201. /* TRUE if the `>' token should be interpreted as the greater-than
  202. operator. FALSE if it is the end of a template-id or
  203. template-parameter-list. In C++0x mode, this flag also applies to
  204. `>>' tokens, which are viewed as two consecutive `>' tokens when
  205. this flag is FALSE. */
  206. bool greater_than_is_operator_p;
  207. /* TRUE if default arguments are allowed within a parameter list
  208. that starts at this point. FALSE if only a gnu extension makes
  209. them permissible. */
  210. bool default_arg_ok_p;
  211. /* TRUE if we are parsing an integral constant-expression. See
  212. [expr.const] for a precise definition. */
  213. bool integral_constant_expression_p;
  214. /* TRUE if we are parsing an integral constant-expression -- but a
  215. non-constant expression should be permitted as well. This flag
  216. is used when parsing an array bound so that GNU variable-length
  217. arrays are tolerated. */
  218. bool allow_non_integral_constant_expression_p;
  219. /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
  220. been seen that makes the expression non-constant. */
  221. bool non_integral_constant_expression_p;
  222. /* TRUE if local variable names and `this' are forbidden in the
  223. current context. */
  224. bool local_variables_forbidden_p;
  225. /* TRUE if the declaration we are parsing is part of a
  226. linkage-specification of the form `extern string-literal
  227. declaration'. */
  228. bool in_unbraced_linkage_specification_p;
  229. /* TRUE if we are presently parsing a declarator, after the
  230. direct-declarator. */
  231. bool in_declarator_p;
  232. /* TRUE if we are presently parsing a template-argument-list. */
  233. bool in_template_argument_list_p;
  234. /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
  235. to IN_OMP_BLOCK if parsing OpenMP structured block and
  236. IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
  237. this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
  238. iteration-statement, OpenMP block or loop within that switch. */
  239. #define IN_SWITCH_STMT 1
  240. #define IN_ITERATION_STMT 2
  241. #define IN_OMP_BLOCK 4
  242. #define IN_OMP_FOR 8
  243. #define IN_IF_STMT 16
  244. #define IN_CILK_SIMD_FOR 32
  245. #define IN_CILK_SPAWN 64
  246. unsigned char in_statement;
  247. /* TRUE if we are presently parsing the body of a switch statement.
  248. Note that this doesn't quite overlap with in_statement above.
  249. The difference relates to giving the right sets of error messages:
  250. "case not in switch" vs "break statement used with OpenMP...". */
  251. bool in_switch_statement_p;
  252. /* TRUE if we are parsing a type-id in an expression context. In
  253. such a situation, both "type (expr)" and "type (type)" are valid
  254. alternatives. */
  255. bool in_type_id_in_expr_p;
  256. /* TRUE if we are currently in a header file where declarations are
  257. implicitly extern "C". */
  258. bool implicit_extern_c;
  259. /* TRUE if strings in expressions should be translated to the execution
  260. character set. */
  261. bool translate_strings_p;
  262. /* TRUE if we are presently parsing the body of a function, but not
  263. a local class. */
  264. bool in_function_body;
  265. /* Nonzero if we're processing a __transaction_atomic or
  266. __transaction_relaxed statement. */
  267. unsigned char in_transaction;
  268. /* TRUE if we can auto-correct a colon to a scope operator. */
  269. bool colon_corrects_to_scope_p;
  270. /* TRUE if : doesn't start a class definition. Should be only used
  271. together with type_definition_forbidden_message non-NULL, in
  272. contexts where new types may not be defined, and the type list
  273. is terminated by colon. */
  274. bool colon_doesnt_start_class_def_p;
  275. /* If non-NULL, then we are parsing a construct where new type
  276. definitions are not permitted. The string stored here will be
  277. issued as an error message if a type is defined. */
  278. const char *type_definition_forbidden_message;
  279. /* A stack used for member functions of local classes. The lists
  280. contained in an individual entry can only be processed once the
  281. outermost class being defined is complete. */
  282. vec<cp_unparsed_functions_entry, va_gc> *unparsed_queues;
  283. /* The number of classes whose definitions are currently in
  284. progress. */
  285. unsigned num_classes_being_defined;
  286. /* The number of template parameter lists that apply directly to the
  287. current declaration. */
  288. unsigned num_template_parameter_lists;
  289. /* When parsing #pragma omp declare simd, this is a pointer to a
  290. data structure with everything needed for parsing the clauses. */
  291. cp_omp_declare_simd_data * GTY((skip)) omp_declare_simd;
  292. /* When parsing the vector attribute in Cilk Plus SIMD-enabled function,
  293. this is a pointer to data structure with everything needed for parsing
  294. the clauses. The cp_omp_declare_simd_data struct will hold all the
  295. necessary information, so creating another struct for this is not
  296. necessary. */
  297. cp_omp_declare_simd_data * GTY((skip)) cilk_simd_fn_info;
  298. /* Nonzero if parsing a parameter list where 'auto' should trigger an implicit
  299. template parameter. */
  300. bool auto_is_implicit_function_template_parm_p;
  301. /* TRUE if the function being declared was made a template due to its
  302. parameter list containing generic type specifiers (`auto' or concept
  303. identifiers) rather than an explicit template parameter list. */
  304. bool fully_implicit_function_template_p;
  305. /* Tracks the function's template parameter list when declaring a function
  306. using generic type parameters. This is either a new chain in the case of a
  307. fully implicit function template or an extension of the function's existing
  308. template parameter list. This is tracked to optimize calls subsequent
  309. calls to synthesize_implicit_template_parm during
  310. cp_parser_parameter_declaration. */
  311. tree implicit_template_parms;
  312. /* The scope into which an implicit template parameter list has been
  313. introduced or an existing template parameter list is being extended with
  314. implicit template paramaters. In most cases this is the sk_function_parms
  315. scope containing the use of a generic type. In the case of an out-of-line
  316. member definition using a generic type, it is the sk_class scope. */
  317. cp_binding_level* implicit_template_scope;
  318. } cp_parser;
  319. /* In parser.c */
  320. extern void debug (cp_token &ref);
  321. extern void debug (cp_token *ptr);
  322. extern void cp_lexer_debug_tokens (vec<cp_token, va_gc> *);
  323. extern void debug (vec<cp_token, va_gc> &ref);
  324. extern void debug (vec<cp_token, va_gc> *ptr);
  325. extern void cp_debug_parser (FILE *, cp_parser *);
  326. extern void debug (cp_parser &ref);
  327. extern void debug (cp_parser *ptr);
  328. #endif /* GCC_CP_PARSER_H */