macro_definition.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED)
  9. #define MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED
  10. #include <vector>
  11. #include <list>
  12. #include <boost/detail/atomic_count.hpp>
  13. #include <boost/intrusive_ptr.hpp>
  14. #include <boost/wave/wave_config.hpp>
  15. #if BOOST_WAVE_SERIALIZATION != 0
  16. #include <boost/serialization/serialization.hpp>
  17. #include <boost/serialization/list.hpp>
  18. #include <boost/serialization/vector.hpp>
  19. #endif
  20. #include <boost/wave/token_ids.hpp>
  21. // this must occur after all of the includes and before any code appears
  22. #ifdef BOOST_HAS_ABI_HEADERS
  23. #include BOOST_ABI_PREFIX
  24. #endif
  25. ///////////////////////////////////////////////////////////////////////////////
  26. namespace boost {
  27. namespace wave {
  28. namespace util {
  29. ///////////////////////////////////////////////////////////////////////////////
  30. //
  31. // macro_definition
  32. //
  33. // This class containes all infos for a defined macro.
  34. //
  35. ///////////////////////////////////////////////////////////////////////////////
  36. template <typename TokenT, typename ContainerT>
  37. struct macro_definition {
  38. typedef std::vector<TokenT> parameter_container_type;
  39. typedef ContainerT definition_container_type;
  40. typedef typename parameter_container_type::const_iterator
  41. const_parameter_iterator_t;
  42. typedef typename definition_container_type::const_iterator
  43. const_definition_iterator_t;
  44. macro_definition(TokenT const &token_, bool has_parameters,
  45. bool is_predefined_, long uid_)
  46. : macroname(token_), uid(uid_), is_functionlike(has_parameters),
  47. replaced_parameters(false), is_available_for_replacement(true),
  48. is_predefined(is_predefined_)
  49. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  50. , has_ellipsis(false)
  51. #endif
  52. , use_count(0)
  53. {
  54. }
  55. // generated copy constructor
  56. // generated destructor
  57. // generated assignment operator
  58. // Replace all occurrences of the parameters throughout the macrodefinition
  59. // with special parameter tokens to simplify later macro replacement.
  60. // Additionally mark all occurrences of the macro name itself throughout
  61. // the macro definition
  62. void replace_parameters()
  63. {
  64. using namespace boost::wave;
  65. if (!replaced_parameters) {
  66. typename definition_container_type::iterator end = macrodefinition.end();
  67. typename definition_container_type::iterator it = macrodefinition.begin();
  68. for (/**/; it != end; ++it) {
  69. token_id id = *it;
  70. if (T_IDENTIFIER == id ||
  71. IS_CATEGORY(id, KeywordTokenType) ||
  72. IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) ||
  73. IS_CATEGORY(id, OperatorTokenType))
  74. {
  75. // may be a parameter to replace
  76. const_parameter_iterator_t cend = macroparameters.end();
  77. const_parameter_iterator_t cit = macroparameters.begin();
  78. for (typename parameter_container_type::size_type i = 0;
  79. cit != cend; ++cit, ++i)
  80. {
  81. if ((*it).get_value() == (*cit).get_value()) {
  82. (*it).set_token_id(token_id(T_PARAMETERBASE+i));
  83. break;
  84. }
  85. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  86. else if (T_ELLIPSIS == token_id(*cit) &&
  87. "__VA_ARGS__" == (*it).get_value())
  88. {
  89. // __VA_ARGS__ requires special handling
  90. (*it).set_token_id(token_id(T_EXTPARAMETERBASE+i));
  91. break;
  92. }
  93. #endif
  94. }
  95. }
  96. }
  97. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  98. // we need to know, if the last of the formal arguments is an ellipsis
  99. if (macroparameters.size() > 0 &&
  100. T_ELLIPSIS == token_id(macroparameters.back()))
  101. {
  102. has_ellipsis = true;
  103. }
  104. #endif
  105. replaced_parameters = true; // do it only once
  106. }
  107. }
  108. TokenT macroname; // macro name
  109. parameter_container_type macroparameters; // formal parameters
  110. definition_container_type macrodefinition; // macro definition token sequence
  111. long uid; // unique id of this macro
  112. bool is_functionlike;
  113. bool replaced_parameters;
  114. bool is_available_for_replacement;
  115. bool is_predefined;
  116. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  117. bool has_ellipsis;
  118. #endif
  119. boost::detail::atomic_count use_count;
  120. #if BOOST_WAVE_SERIALIZATION != 0
  121. // default constructor is needed for serialization only
  122. macro_definition()
  123. : uid(0), is_functionlike(false), replaced_parameters(false),
  124. is_available_for_replacement(false), is_predefined(false)
  125. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  126. , has_ellipsis(false)
  127. #endif
  128. , use_count(0)
  129. {}
  130. private:
  131. friend class boost::serialization::access;
  132. template<typename Archive>
  133. void serialize(Archive &ar, const unsigned int version)
  134. {
  135. using namespace boost::serialization;
  136. ar & make_nvp("name", macroname);
  137. ar & make_nvp("parameters", macroparameters);
  138. ar & make_nvp("definition", macrodefinition);
  139. ar & make_nvp("uid", uid);
  140. ar & make_nvp("is_functionlike", is_functionlike);
  141. ar & make_nvp("has_replaced_parameters", replaced_parameters);
  142. ar & make_nvp("is_available_for_replacement", is_available_for_replacement);
  143. ar & make_nvp("is_predefined", is_predefined);
  144. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  145. ar & make_nvp("has_ellipsis", has_ellipsis);
  146. #endif
  147. }
  148. #endif
  149. };
  150. #if BOOST_WAVE_SERIALIZATION == 0
  151. ///////////////////////////////////////////////////////////////////////////////
  152. template <typename TokenT, typename ContainerT>
  153. inline void
  154. intrusive_ptr_add_ref(macro_definition<TokenT, ContainerT>* p)
  155. {
  156. ++p->use_count;
  157. }
  158. template <typename TokenT, typename ContainerT>
  159. inline void
  160. intrusive_ptr_release(macro_definition<TokenT, ContainerT>* p)
  161. {
  162. if (--p->use_count == 0)
  163. delete p;
  164. }
  165. #endif
  166. ///////////////////////////////////////////////////////////////////////////////
  167. } // namespace util
  168. } // namespace wave
  169. } // namespace boost
  170. // the suffix header occurs after all of the code
  171. #ifdef BOOST_HAS_ABI_HEADERS
  172. #include BOOST_ABI_SUFFIX
  173. #endif
  174. #endif // !defined(MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED)