cppPreprocessor.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppPreprocessor.h
  10. * @author drose
  11. * @date 1999-10-22
  12. */
  13. #ifndef CPPPREPROCESSOR_H
  14. #define CPPPREPROCESSOR_H
  15. #include "dtoolbase.h"
  16. #include "cppManifest.h"
  17. #include "cppToken.h"
  18. #include "cppFile.h"
  19. #include "cppCommentBlock.h"
  20. #include "dSearchPath.h"
  21. #include "vector_string.h"
  22. #include <map>
  23. #include <list>
  24. #include <vector>
  25. class CPPScope;
  26. class CPPTemplateParameterList;
  27. class CPPExpression;
  28. // #define CPP_VERBOSE_LEX
  29. /**
  30. *
  31. */
  32. class CPPPreprocessor {
  33. public:
  34. CPPPreprocessor();
  35. void set_verbose(int verbose);
  36. int get_verbose() const;
  37. void copy_filepos(const CPPPreprocessor &other);
  38. CPPFile get_file() const;
  39. int get_line_number() const;
  40. int get_col_number() const;
  41. CPPToken get_next_token();
  42. CPPToken peek_next_token();
  43. #ifdef CPP_VERBOSE_LEX
  44. CPPToken get_next_token0();
  45. int _token_index;
  46. #endif
  47. void warning(const string &message);
  48. void warning(const string &message, const YYLTYPE &loc);
  49. void error(const string &message);
  50. void error(const string &message, const YYLTYPE &loc);
  51. void show_line(const YYLTYPE &loc);
  52. CPPCommentBlock *get_comment_before(int line, CPPFile file);
  53. CPPCommentBlock *get_comment_on(int line, CPPFile file);
  54. int get_warning_count() const;
  55. int get_error_count() const;
  56. typedef map<string, CPPManifest *> Manifests;
  57. Manifests _manifests;
  58. typedef pvector<CPPManifest *> ManifestStack;
  59. map<string, ManifestStack> _manifest_stack;
  60. pvector<CPPFile::Source> _quote_include_kind;
  61. DSearchPath _quote_include_path;
  62. DSearchPath _angle_include_path;
  63. bool _noangles;
  64. CPPComments _comments;
  65. typedef set<CPPFile> ParsedFiles;
  66. ParsedFiles _parsed_files;
  67. typedef set<string> Includes;
  68. Includes _quote_includes;
  69. Includes _angle_includes;
  70. set<Filename> _explicit_files;
  71. // This is normally true, to indicate that the preprocessor should decode
  72. // identifiers like foo::bar<snarf> into a single IDENTIFIER,
  73. // TYPENAME_IDENTIFIER, or SCOPING token for yacc's convenience. When
  74. // false, it leaves them alone and returns a sequence of SIMPLE_IDENTIFIER
  75. // and SCOPE tokens instead.
  76. bool _resolve_identifiers;
  77. // The default _verbose level is 1, which will output normal error and
  78. // warning messages but nothing else. Set this to 0 to make the warning
  79. // messages go away (although the counts will still be incremented), or set
  80. // it higher to get more debugging information.
  81. int _verbose;
  82. // The location of the last token.
  83. cppyyltype _last_token_loc;
  84. protected:
  85. bool init_cpp(const CPPFile &file);
  86. bool init_const_expr(const string &expr);
  87. bool init_type(const string &type);
  88. bool push_file(const CPPFile &file);
  89. bool push_string(const string &input, bool lock_position);
  90. string expand_manifests(const string &input_expr, bool expand_undefined,
  91. const YYLTYPE &loc);
  92. CPPExpression *parse_expr(const string &expr, CPPScope *current_scope,
  93. CPPScope *global_scope, const YYLTYPE &loc);
  94. private:
  95. CPPToken internal_get_next_token();
  96. int check_digraph(int c);
  97. int check_trigraph(int c);
  98. int skip_whitespace(int c);
  99. int skip_comment(int c);
  100. int skip_c_comment(int c);
  101. int skip_cpp_comment(int c);
  102. int skip_digit_separator(int c);
  103. int process_directive(int c);
  104. int get_preprocessor_command(int c, string &command);
  105. int get_preprocessor_args(int c, string &args);
  106. void handle_define_directive(const string &args, const YYLTYPE &loc);
  107. void handle_undef_directive(const string &args, const YYLTYPE &loc);
  108. void handle_ifdef_directive(const string &args, const YYLTYPE &loc);
  109. void handle_ifndef_directive(const string &args, const YYLTYPE &loc);
  110. void handle_if_directive(const string &args, const YYLTYPE &loc);
  111. void handle_include_directive(const string &args, const YYLTYPE &loc);
  112. void handle_pragma_directive(const string &args, const YYLTYPE &loc);
  113. void handle_error_directive(const string &args, const YYLTYPE &loc);
  114. void skip_false_if_block(bool consider_elifs);
  115. bool is_manifest_defined(const string &manifest_name);
  116. bool find_include(Filename &filename, bool angle_quotes, CPPFile::Source &source);
  117. CPPToken get_quoted_char(int c);
  118. CPPToken get_quoted_string(int c);
  119. CPPToken get_identifier(int c);
  120. CPPToken get_literal(int token, YYLTYPE loc, const string &str,
  121. const YYSTYPE &result = YYSTYPE());
  122. CPPToken expand_manifest(const CPPManifest *manifest);
  123. void extract_manifest_args(const string &name, int num_args,
  124. int va_arg, vector_string &args);
  125. void expand_defined_function(string &expr, size_t q, size_t &p);
  126. void expand_has_include_function(string &expr, size_t q, size_t &p, YYLTYPE loc);
  127. void expand_manifest_inline(string &expr, size_t q, size_t &p,
  128. const CPPManifest *manifest);
  129. void extract_manifest_args_inline(const string &name, int num_args,
  130. int va_arg, vector_string &args,
  131. const string &expr, size_t &p);
  132. CPPToken get_number(int c);
  133. static int check_keyword(const string &name);
  134. int scan_escape_sequence(int c);
  135. string scan_quoted(int c);
  136. bool should_ignore_manifest(const CPPManifest *manifest) const;
  137. bool should_ignore_preprocessor() const;
  138. int get();
  139. int peek();
  140. void unget(int c);
  141. CPPTemplateParameterList *
  142. nested_parse_template_instantiation(CPPTemplateScope *scope);
  143. void skip_to_end_nested();
  144. void skip_to_angle_bracket();
  145. class InputFile {
  146. public:
  147. InputFile();
  148. ~InputFile();
  149. bool open(const CPPFile &file);
  150. bool connect_input(const string &input);
  151. int get();
  152. int peek();
  153. const CPPManifest *_ignore_manifest;
  154. CPPFile _file;
  155. string _input;
  156. istream *_in;
  157. int _line_number;
  158. int _col_number;
  159. int _next_line_number;
  160. int _next_col_number;
  161. bool _lock_position;
  162. int _prev_last_c;
  163. };
  164. // This must be a list and not a vector because we don't have a good copy
  165. // constructor defined for InputFile.
  166. typedef list<InputFile> Files;
  167. Files _files;
  168. enum State {
  169. S_normal, S_eof, S_nested, S_end_nested
  170. };
  171. State _state;
  172. int _paren_nesting;
  173. bool _parsing_template_params;
  174. bool _start_of_line;
  175. int _unget;
  176. int _last_c;
  177. bool _last_cpp_comment;
  178. bool _save_comments;
  179. vector<CPPToken> _saved_tokens;
  180. int _warning_count;
  181. int _error_count;
  182. bool _error_abort;
  183. };
  184. #endif