forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_namespace.hpp
More file actions
194 lines (158 loc) · 5.96 KB
/
Copy pathcpp_namespace.hpp
File metadata and controls
194 lines (158 loc) · 5.96 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
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_CPP_NAMESPACE_HPP_INCLUDED
#define CPPAST_CPP_NAMESPACE_HPP_INCLUDED
#include <cppast/cpp_entity_container.hpp>
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_entity_kind.hpp>
#include <cppast/cpp_entity_ref.hpp>
namespace cppast
{
/// A [cppast::cpp_entity]() modelling a namespace.
class cpp_namespace final : public cpp_entity,
public cpp_entity_container<cpp_namespace, cpp_entity>
{
public:
static cpp_entity_kind kind() noexcept;
/// Builds a [cppast::cpp_namespace]().
class builder
{
public:
/// \effects Sets the namespace name and whether it is inline and nested.
explicit builder(std::string name, bool is_inline, bool is_nested)
: namespace_(new cpp_namespace(std::move(name), is_inline, is_nested))
{}
/// \effects Adds an entity.
void add_child(std::unique_ptr<cpp_entity> child) noexcept
{
namespace_->add_child(std::move(child));
}
/// \returns The not yet finished namespace.
cpp_namespace& get() const noexcept
{
return *namespace_;
}
/// \effects Registers the namespace in the [cppast::cpp_entity_index](),
/// using the given [cppast::cpp_entity_id]().
/// \returns The finished namespace.
std::unique_ptr<cpp_namespace> finish(const cpp_entity_index& idx, cpp_entity_id id)
{
idx.register_namespace(std::move(id), type_safe::ref(*namespace_));
return std::move(namespace_);
}
private:
std::unique_ptr<cpp_namespace> namespace_;
};
/// \returns Whether or not the namespace is an `inline namespace`.
bool is_inline() const noexcept
{
return inline_;
}
/// \returns Whether or not the namespace is part of a C++17 nested namespace.
bool is_nested() const noexcept
{
return nested_;
}
/// \returns Whether or not the namespace is anonymous.
bool is_anonymous() const noexcept
{
return name().empty();
}
private:
cpp_namespace(std::string name, bool is_inline, bool is_nested)
: cpp_entity(std::move(name)), inline_(is_inline), nested_(is_nested)
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
type_safe::optional<cpp_scope_name> do_get_scope_name() const override
{
return type_safe::ref(*this);
}
bool inline_;
bool nested_;
};
/// \exclude
namespace detail
{
struct cpp_namespace_ref_predicate
{
bool operator()(const cpp_entity& e);
};
} // namespace detail
/// A reference to a [cppast::cpp_namespace]().
using cpp_namespace_ref = basic_cpp_entity_ref<cpp_namespace, detail::cpp_namespace_ref_predicate>;
/// A [cppast::cpp_entity]() modelling a namespace alias.
class cpp_namespace_alias final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly created and registered namespace alias.
static std::unique_ptr<cpp_namespace_alias> build(const cpp_entity_index& idx, cpp_entity_id id,
std::string name, cpp_namespace_ref target);
/// \returns The [cppast::cpp_namespace_ref]() to the aliased namespace.
/// \notes If the namespace aliases aliases another namespace alias,
/// the target entity will still be the namespace, not the alias.
const cpp_namespace_ref& target() const noexcept
{
return target_;
}
private:
cpp_namespace_alias(std::string name, cpp_namespace_ref target)
: cpp_entity(std::move(name)), target_(std::move(target))
{}
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_namespace_ref target_;
};
/// A [cppast::cpp_entity]() modelling a using directive.
///
/// A using directive is `using namespace std`, for example.
/// \notes It does not have a name.
class cpp_using_directive final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly created using directive.
/// \notes It is not meant to be registered at the [cppast::cpp_entity_index](),
/// as nothing can refer to it.
static std::unique_ptr<cpp_using_directive> build(cpp_namespace_ref target)
{
return std::unique_ptr<cpp_using_directive>(new cpp_using_directive(std::move(target)));
}
/// \returns The [cppast::cpp_namespace_ref]() that is being used.
const cpp_namespace_ref& target() const
{
return target_;
}
private:
cpp_using_directive(cpp_namespace_ref target) : cpp_entity(""), target_(std::move(target)) {}
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_namespace_ref target_;
};
/// A [cppast::cpp_entity]() modelling a using declaration.
///
/// A using declaration is `using std::vector`, for example.
/// \notes It does not have a name.
class cpp_using_declaration final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly created using declaration.
/// \notes It is not meant to be registered at the [cppast::cpp_entity_index](),
/// as nothing can refer to it.
static std::unique_ptr<cpp_using_declaration> build(cpp_entity_ref target)
{
return std::unique_ptr<cpp_using_declaration>(new cpp_using_declaration(std::move(target)));
}
/// \returns The [cppast::cpp_entity_ref]() that is being used.
/// \notes The name of the reference is the same as the name of this entity.
const cpp_entity_ref& target() const noexcept
{
return target_;
}
private:
cpp_using_declaration(cpp_entity_ref target) : cpp_entity(""), target_(std::move(target)) {}
cpp_entity_kind do_get_entity_kind() const noexcept override;
cpp_entity_ref target_;
};
} // namespace cppast
#endif // CPPAST_CPP_NAMESPACE_HPP_INCLUDED