-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathcpp_class.cpp
179 lines (154 loc) · 5.43 KB
/
cpp_class.cpp
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
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include <cppast/cpp_class.hpp>
#include <cppast/cpp_alias_template.hpp>
#include <cppast/cpp_class_template.hpp>
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_entity_kind.hpp>
using namespace cppast;
std::unique_ptr<cpp_class> cpp_class::builder::finish(
const cpp_entity_index& idx, cpp_entity_id id,
type_safe::optional<cpp_entity_ref> semantic_parent)
{
class_->set_semantic_parent(std::move(semantic_parent));
idx.register_definition(std::move(id), type_safe::ref(*class_));
return std::move(class_);
}
std::unique_ptr<cpp_class> cpp_class::builder::finish_declaration(const cpp_entity_index& idx,
cpp_entity_id definition_id)
{
class_->mark_declaration(definition_id);
idx.register_forward_declaration(std::move(definition_id), type_safe::ref(*class_));
return std::move(class_);
}
std::unique_ptr<cpp_class> cpp_class::builder::finish(
type_safe::optional<cpp_entity_ref> semantic_parent)
{
class_->set_semantic_parent(std::move(semantic_parent));
return std::move(class_);
}
std::unique_ptr<cpp_class> cpp_class::builder::finish_declaration(cpp_entity_id definition_id)
{
class_->mark_declaration(definition_id);
return std::move(class_);
}
const char* cppast::to_string(cpp_class_kind kind) noexcept
{
switch (kind)
{
case cpp_class_kind::class_t:
return "class";
case cpp_class_kind::struct_t:
return "struct";
case cpp_class_kind::union_t:
return "union";
}
return "should not get here";
}
const char* cppast::to_string(cpp_access_specifier_kind access) noexcept
{
switch (access)
{
case cpp_public:
return "public";
case cpp_protected:
return "protected";
case cpp_private:
return "private";
}
return "should not get here either";
}
cpp_entity_kind cpp_access_specifier::kind() noexcept
{
return cpp_entity_kind::access_specifier_t;
}
cpp_entity_kind cpp_access_specifier::do_get_entity_kind() const noexcept
{
return kind();
}
cpp_entity_kind cpp_base_class::kind() noexcept
{
return cpp_entity_kind::base_class_t;
}
cpp_entity_kind cpp_base_class::do_get_entity_kind() const noexcept
{
return kind();
}
cpp_entity_kind cpp_class::kind() noexcept
{
return cpp_entity_kind::class_t;
}
cpp_entity_kind cpp_class::do_get_entity_kind() const noexcept
{
return kind();
}
namespace
{
cpp_entity_ref get_type_ref(const cpp_type& type)
{
if (type.kind() == cpp_type_kind::user_defined_t)
{
auto& ref = static_cast<const cpp_user_defined_type&>(type).entity();
return cpp_entity_ref(ref.id()[0u], ref.name());
}
else if (type.kind() == cpp_type_kind::template_instantiation_t)
{
auto& ref = static_cast<const cpp_template_instantiation_type&>(type).primary_template();
return cpp_entity_ref(ref.id()[0u], ref.name());
}
DEBUG_ASSERT(type.kind() == cpp_type_kind::template_parameter_t
|| type.kind() == cpp_type_kind::decltype_t
|| type.kind() == cpp_type_kind::decltype_auto_t
|| type.kind() == cpp_type_kind::unexposed_t,
detail::assert_handler{});
return cpp_entity_ref(cpp_entity_id("<null id>"), "");
}
type_safe::optional_ref<const cpp_entity> get_entity_impl(const cpp_entity_index& index,
const cpp_entity_ref& ref)
{
auto result = ref.get(index);
if (result.empty())
return nullptr;
DEBUG_ASSERT(result.size() == 1u, detail::assert_handler{});
auto entity = result.front();
if (entity->kind() == cpp_class_template::kind())
return type_safe::ref(static_cast<const cpp_class_template&>(*entity).class_());
else if (entity->kind() == cpp_class_template_specialization::kind())
return type_safe::ref(
static_cast<const cpp_class_template_specialization&>(*entity).class_());
else
return entity;
}
type_safe::optional_ref<const cpp_class> get_class_impl(const cpp_entity_index& index,
const cpp_entity_ref& ref)
{
auto entity = get_entity_impl(index, ref);
if (!entity)
return nullptr;
if (entity.value().kind() == cpp_alias_template::kind())
{
auto& alias = static_cast<const cppast::cpp_alias_template&>(entity.value());
return get_class_impl(index, get_type_ref(alias.type_alias().underlying_type()));
}
else if (entity.value().kind() == cpp_type_alias::kind())
{
auto& alias = static_cast<const cppast::cpp_type_alias&>(entity.value());
return get_class_impl(index, get_type_ref(alias.underlying_type()));
}
else
{
DEBUG_ASSERT(entity.value().kind() == cpp_class::kind(), detail::assert_handler{});
return type_safe::ref(static_cast<const cpp_class&>(entity.value()));
}
}
} // namespace
type_safe::optional_ref<const cpp_class> cppast::get_class(const cpp_entity_index& index,
const cpp_base_class& base)
{
return get_class_impl(index, get_type_ref(base.type()));
}
type_safe::optional_ref<const cpp_entity> cppast::get_class_or_typedef(
const cpp_entity_index& index, const cpp_base_class& base)
{
return get_entity_impl(index, get_type_ref(base.type()));
}