macro_definition.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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(BOOST_MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED)
  9. #define BOOST_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. template<typename ContextT>
  63. void replace_parameters(ContextT const & ctx)
  64. {
  65. using namespace boost::wave;
  66. if (!replaced_parameters) {
  67. typename definition_container_type::iterator end = macrodefinition.end();
  68. typename definition_container_type::iterator it = macrodefinition.begin();
  69. for (/**/; it != end; ++it) {
  70. token_id id = *it;
  71. if (T_IDENTIFIER == id ||
  72. IS_CATEGORY(id, KeywordTokenType) ||
  73. IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) ||
  74. IS_CATEGORY(id, OperatorTokenType))
  75. {
  76. // may be a parameter to replace
  77. const_parameter_iterator_t cend = macroparameters.end();
  78. const_parameter_iterator_t cit = macroparameters.begin();
  79. for (typename parameter_container_type::size_type i = 0;
  80. cit != cend; ++cit, ++i)
  81. {
  82. if ((*it).get_value() == (*cit).get_value()) {
  83. (*it).set_token_id(token_id(T_PARAMETERBASE+i));
  84. break;
  85. }
  86. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  87. else if (need_variadics(ctx.get_language()) &&
  88. T_ELLIPSIS == token_id(*cit) &&
  89. "__VA_ARGS__" == (*it).get_value())
  90. {
  91. // __VA_ARGS__ requires special handling
  92. (*it).set_token_id(token_id(T_EXTPARAMETERBASE+i));
  93. break;
  94. }
  95. #if BOOST_WAVE_SUPPORT_VA_OPT != 0
  96. else if (need_va_opt(ctx.get_language()) &&
  97. T_ELLIPSIS == token_id(*cit) &&
  98. "__VA_OPT__" == (*it).get_value())
  99. {
  100. // __VA_OPT__ also requires related special handling
  101. (*it).set_token_id(token_id(T_OPTPARAMETERBASE+i));
  102. break;
  103. }
  104. #endif
  105. #endif
  106. }
  107. }
  108. }
  109. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  110. // we need to know, if the last of the formal arguments is an ellipsis
  111. if (macroparameters.size() > 0 &&
  112. T_ELLIPSIS == token_id(macroparameters.back()))
  113. {
  114. has_ellipsis = true;
  115. }
  116. #endif
  117. replaced_parameters = true; // do it only once
  118. }
  119. }
  120. TokenT macroname; // macro name
  121. parameter_container_type macroparameters; // formal parameters
  122. definition_container_type macrodefinition; // macro definition token sequence
  123. long uid; // unique id of this macro
  124. bool is_functionlike;
  125. bool replaced_parameters;
  126. bool is_available_for_replacement;
  127. bool is_predefined;
  128. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  129. bool has_ellipsis;
  130. #endif
  131. boost::detail::atomic_count use_count;
  132. #if BOOST_WAVE_SERIALIZATION != 0
  133. // default constructor is needed for serialization only
  134. macro_definition()
  135. : uid(0), is_functionlike(false), replaced_parameters(false),
  136. is_available_for_replacement(false), is_predefined(false)
  137. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  138. , has_ellipsis(false)
  139. #endif
  140. , use_count(0)
  141. {}
  142. private:
  143. friend class boost::serialization::access;
  144. template<typename Archive>
  145. void serialize(Archive &ar, const unsigned int version)
  146. {
  147. using namespace boost::serialization;
  148. ar & make_nvp("name", macroname);
  149. ar & make_nvp("parameters", macroparameters);
  150. ar & make_nvp("definition", macrodefinition);
  151. ar & make_nvp("uid", uid);
  152. ar & make_nvp("is_functionlike", is_functionlike);
  153. ar & make_nvp("has_replaced_parameters", replaced_parameters);
  154. ar & make_nvp("is_available_for_replacement", is_available_for_replacement);
  155. ar & make_nvp("is_predefined", is_predefined);
  156. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  157. ar & make_nvp("has_ellipsis", has_ellipsis);
  158. #endif
  159. }
  160. #endif
  161. };
  162. #if BOOST_WAVE_SERIALIZATION == 0
  163. ///////////////////////////////////////////////////////////////////////////////
  164. template <typename TokenT, typename ContainerT>
  165. inline void
  166. intrusive_ptr_add_ref(macro_definition<TokenT, ContainerT>* p)
  167. {
  168. ++p->use_count;
  169. }
  170. template <typename TokenT, typename ContainerT>
  171. inline void
  172. intrusive_ptr_release(macro_definition<TokenT, ContainerT>* p)
  173. {
  174. if (--p->use_count == 0)
  175. delete p;
  176. }
  177. #endif
  178. ///////////////////////////////////////////////////////////////////////////////
  179. } // namespace util
  180. } // namespace wave
  181. } // namespace boost
  182. // the suffix header occurs after all of the code
  183. #ifdef BOOST_HAS_ABI_HEADERS
  184. #include BOOST_ABI_SUFFIX
  185. #endif
  186. #endif // !defined(BOOST_MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED)