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