-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathcpp_preprocessor.hpp
More file actions
204 lines (169 loc) · 6.19 KB
/
cpp_preprocessor.hpp
File metadata and controls
204 lines (169 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#ifndef CPPAST_CPP_PREPROCESSOR_HPP_INCLUDED
#define CPPAST_CPP_PREPROCESSOR_HPP_INCLUDED
#include <type_safe/optional.hpp>
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_file.hpp>
namespace cppast
{
/// A [cppast::cpp_entity]() modelling a macro parameter.
class cpp_macro_parameter final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly built macro parameter.
/// \notes It is not meant to be registered in the [cppast::cpp_entity_index]() as no other
/// [cppast::cpp_entity]() can refer to it.
static std::unique_ptr<cpp_macro_parameter> build(std::string name)
{
return std::unique_ptr<cpp_macro_parameter>(new cpp_macro_parameter(std::move(name)));
}
private:
cpp_macro_parameter(std::string name) : cpp_entity(std::move(name)) {}
cpp_entity_kind do_get_entity_kind() const noexcept override;
};
/// A [cppast::cpp_entity]() modelling a macro definition.
class cpp_macro_definition final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly built object like macro.
/// \notes It is not meant to be registered in the [cppast::cpp_entity_index](),
/// as no other [cppast::cpp_entity]() can refer to it.
static std::unique_ptr<cpp_macro_definition> build_object_like(std::string name,
std::string replacement)
{
std::unique_ptr<cpp_macro_definition> result{new cpp_macro_definition(std::move(name))};
result->replacement_ = std::move(replacement);
return result;
}
/// Builds a function like macro.
class function_like_builder
{
public:
/// \effects Sets the name of the function like macro.
function_like_builder(std::string name) : result_(new cpp_macro_definition(std::move(name)))
{
result_->kind_ = function_like;
}
/// \effects Sets the replacement text.
void replacement(std::string replacement)
{
result_->replacement_ = std::move(replacement);
}
/// \effects Marks the macro as variadic.
void is_variadic()
{
result_->kind_ = variadic_function;
}
/// \effects Adds a parameter.
/// \group param
void parameter(std::unique_ptr<cpp_macro_parameter> param)
{
result_->parameters_.push_back(*result_, std::move(param));
}
/// \group param
void parameter(std::string name)
{
parameter(cpp_macro_parameter::build(std::move(name)));
}
/// \returns The finished macro.
/// \notes It is not meant to be registered in the [cppast::cpp_entity_index](),
/// as no other [cppast::cpp_entity]() can refer to it.
std::unique_ptr<cpp_macro_definition> finish()
{
return std::move(result_);
}
private:
std::unique_ptr<cpp_macro_definition> result_;
};
/// \returns The replacement text of the macro.
const std::string& replacement() const noexcept
{
return replacement_;
}
/// \returns Whether or not it is an object like macro.
bool is_object_like() const noexcept
{
return kind_ == object_like;
}
/// \returns Whether or not it is a function like macro.
bool is_function_like() const noexcept
{
return kind_ != object_like;
}
/// \returns Whether or not it is a variadic macro.
bool is_variadic() const noexcept
{
return kind_ == variadic_function;
}
/// \returns The parameters of the macro.
/// \notes It has none if it is not a function like macro.
detail::iteratable_intrusive_list<cpp_macro_parameter> parameters() const noexcept
{
return type_safe::ref(parameters_);
}
private:
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_macro_definition(std::string name) : cpp_entity(std::move(name)), kind_(object_like) {}
detail::intrusive_list<cpp_macro_parameter> parameters_;
std::string replacement_;
enum : char
{
object_like,
function_like,
variadic_function,
} kind_;
friend function_like_builder;
};
/// The kind of [cppast::cpp_include_directive]().
enum class cpp_include_kind
{
system, //< An `#include <...>`.
local, //< An `#include "..."`.
};
/// A [cppast::cpp_entity]() modelling an `#include`.
class cpp_include_directive final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly built include directive.
/// \notes It is not meant to be registered in the [cppast::cpp_entity_index](),
/// as no other [cppast::cpp_entity]() can refer to it.
static std::unique_ptr<cpp_include_directive> build(const cpp_file_ref& target,
cpp_include_kind kind,
std::string full_path)
{
return std::unique_ptr<cpp_include_directive>(
new cpp_include_directive(target, kind, std::move(full_path)));
}
/// \returns A reference to the [cppast::cpp_file]() it includes.
cpp_file_ref target() const noexcept
{
return cpp_file_ref(target_, name());
}
/// \returns The kind of include it is.
cpp_include_kind include_kind() const noexcept
{
return kind_;
}
/// \returns The full path of the included file.
const std::string& full_path() const noexcept
{
return full_path_;
}
private:
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_include_directive(const cpp_file_ref& target, cpp_include_kind kind, std::string full_path)
: cpp_entity(target.name()), target_(target.id()[0u]), kind_(kind),
full_path_(std::move(full_path))
{
DEBUG_ASSERT(!target.is_overloaded(), detail::precondition_error_handler{});
}
cpp_entity_id target_;
cpp_include_kind kind_;
std::string full_path_;
};
} // namespace cppast
#endif // CPPAST_CPP_PREPROCESSOR_HPP_INCLUDED