cppPreprocessor.h 6.5 KB

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