MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Collector.cpp
Go to the documentation of this file.
1#include "Collector.hpp"
2
4
5namespace {
6
7 constexpr const char* request_id = "request";
8 constexpr const char* request_pattern = "mf_reflection_request_";
9
10 bool nameable(const clang::CXXRecordDecl* record)
11 {
12 if (record->isDependentContext()) {
13 return false;
14 }
15 const clang::Decl* current = record;
16 while (true) {
17 if (current->getAccess() == clang::AS_private || current->getAccess() == clang::AS_protected) {
18 return false;
19 }
20 const clang::DeclContext* parent = current->getDeclContext();
21 if (parent->isTranslationUnit()) {
22 return true;
23 }
24 if (const auto* space = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
25 if (space->isAnonymousNamespace()) {
26 return false;
27 }
28 } else if (const auto* outer = llvm::dyn_cast<clang::CXXRecordDecl>(parent)) {
29 if (outer->getName().empty() || outer->isDependentContext()) {
30 return false;
31 }
32 } else if (!llvm::isa<clang::LinkageSpecDecl>(parent)) {
33 return false;
34 }
35 current = clang::Decl::castFromDeclContext(parent);
36 }
37 }
38
39 void collect_nested(clang::QualType type, std::vector<const clang::CXXRecordDecl*>& out)
40 {
41 const clang::Type* underlying = type.getNonReferenceType()->getBaseElementTypeUnsafe();
42 const clang::CXXRecordDecl* record = underlying->getAsCXXRecordDecl();
43 if (record != nullptr) {
44 record = record->getDefinition();
45 }
46 if (record == nullptr) {
47 return;
48 }
49 if (const auto* specialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(record)) {
50 if (!record->isInStdNamespace()) {
51 return;
52 }
53 for (const clang::TemplateArgument& argument : specialization->getTemplateArgs().asArray()) {
54 if (argument.getKind() == clang::TemplateArgument::Type) {
55 collect_nested(argument.getAsType(), out);
56 }
57 }
58 return;
59 }
60 if (record->isInStdNamespace() || record->isUnion() || !record->isAggregate() || !nameable(record)) {
61 return;
62 }
63 if (std::ranges::find(out, record) == out.end()) {
64 out.push_back(record);
65 }
66 }
67
68 bool collect_members(const clang::CXXRecordDecl* record, std::vector<std::string>& names,
69 std::vector<const clang::CXXRecordDecl*>& nested, std::string& error)
70 {
71 for (const auto& base : record->bases()) {
72 if (base.getAccessSpecifier() != clang::AS_public) {
73 continue;
74 }
75 const clang::CXXRecordDecl* parent = base.getType()->getAsCXXRecordDecl();
76 if (parent != nullptr) {
77 parent = parent->getDefinition();
78 }
79 if (parent == nullptr) {
80 error = "a public base class is not a complete record";
81 return false;
82 }
83 if (!collect_members(parent, names, nested, error)) {
84 return false;
85 }
86 }
87 for (const clang::FieldDecl* field : record->fields()) {
88 if (field->getAccess() != clang::AS_public) {
89 continue;
90 }
91 const std::string name = field->getNameAsString();
92 if (name.empty()) {
93 error = "an anonymous member cannot use a member-pointer descriptor";
94 return false;
95 }
96 if (field->isBitField() || field->getType()->isReferenceType()) {
97 error = "member " + name + " is a bit-field or reference and cannot use a member-pointer descriptor";
98 return false;
99 }
100 if (std::ranges::find(names, name) != names.end()) {
101 error = "member name " + name + " appears in more than one class of the hierarchy";
102 return false;
103 }
104 names.push_back(name);
105 collect_nested(field->getType(), nested);
106 }
107 return true;
108 }
109
110 std::filesystem::path real_path(llvm::StringRef file)
111 {
112 llvm::SmallString<256> resolved;
113 if (llvm::sys::fs::real_path(file, resolved)) {
114 return { file.str() };
115 }
116 return { resolved.str().str() };
117 }
118
119 std::filesystem::path file_of(const clang::SourceManager& manager, clang::SourceLocation location)
120 {
121 return real_path(manager.getFilename(manager.getExpansionLoc(location)));
122 }
123
124}
125
126bool RequestCollector::store(const std::filesystem::path& header, RecordRequest request, const std::string& where)
127{
128 HeaderRequests& entry = m_requests[header];
129 entry.header = header;
130 const auto existing = std::ranges::find(entry.records, request.name, &RecordRequest::name);
131 if (existing != entry.records.end()) {
132 existing->line = std::max(existing->line, request.line);
133 return false;
134 }
135 for (const auto& [other_header, other] : m_requests) {
136 if (other_header != header
137 && std::ranges::find(other.records, request.name, &RecordRequest::name) != other.records.end()) {
138 m_errors.push_back(where + request.name + ": already described in " + other_header.string());
139 return false;
140 }
141 }
142 entry.records.push_back(std::move(request));
143 return true;
144}
145
146void RequestCollector::describe_nested(const clang::CXXRecordDecl* record, const std::filesystem::path& header,
147 unsigned line, const clang::SourceManager& manager)
148{
149 RecordRequest request;
150 request.name = record->getQualifiedNameAsString();
151 request.line = line;
152
153 std::vector<const clang::CXXRecordDecl*> nested;
154 std::string error;
155 const std::string where = header.string() + ": ";
156 if (!collect_members(record, request.members, nested, error)) {
157 m_errors.push_back(where + "nested type " + request.name + ": " + error);
158 return;
159 }
160 if (!store(header, std::move(request), where)) {
161 return;
162 }
163 for (const clang::CXXRecordDecl* child : nested) {
164 if (file_of(manager, child->getLocation()) == header) {
165 describe_nested(child, header, line, manager);
166 }
167 }
168}
169
170void RequestCollector::attach(clang::ast_matchers::MatchFinder& finder)
171{
172 using namespace clang::ast_matchers;
173 finder.addMatcher(typeAliasDecl(matchesName(request_pattern)).bind(request_id), this);
174}
175
176void RequestCollector::run(const clang::ast_matchers::MatchFinder::MatchResult& result)
177{
178 const auto* alias = result.Nodes.getNodeAs<clang::TypeAliasDecl>(request_id);
179 if (alias == nullptr) {
180 return;
181 }
182
183 ++m_matched;
184
185 const clang::SourceManager& manager = *result.SourceManager;
186 const clang::SourceLocation location = manager.getExpansionLoc(alias->getBeginLoc());
187 if (!location.isValid() || manager.isInSystemHeader(location)) {
188 ++m_skipped;
189 return;
190 }
191
192 const std::filesystem::path header = real_path(manager.getFilename(location));
193 const unsigned line = manager.getSpellingLineNumber(location);
194 const std::string where = header.string() + ":" + std::to_string(line) + ": ";
195 const std::string spelling = alias->getUnderlyingType().getAsString();
196
197 if (!alias->getDeclContext()->getRedeclContext()->isFileContext()) {
198 m_errors.push_back(where + "MF_DESCRIBE must appear at namespace scope");
199 return;
200 }
201
202 const clang::CXXRecordDecl* record = alias->getUnderlyingType()->getAsCXXRecordDecl();
203 if (record != nullptr) {
204 record = record->getDefinition();
205 }
206 if (record == nullptr) {
207 m_errors.push_back(where + spelling + ": request must name a complete struct or class");
208 return;
209 }
210 if (llvm::isa<clang::ClassTemplateSpecializationDecl>(record) || record->isUnion() || !nameable(record)) {
211 m_errors.push_back(where + spelling + ": template, union, anonymous and inaccessible records are unsupported");
212 return;
213 }
214
215 unsigned anchor = line;
216 const clang::NamespaceDecl* outermost = nullptr;
217 for (const clang::DeclContext* context = alias->getLexicalDeclContext();
218 context != nullptr && !context->isTranslationUnit(); context = context->getLexicalParent()) {
219 if (const auto* space = llvm::dyn_cast<clang::NamespaceDecl>(context)) {
220 outermost = space;
221 }
222 }
223 if (outermost != nullptr && outermost->getRBraceLoc().isValid()) {
224 anchor = manager.getSpellingLineNumber(manager.getExpansionLoc(outermost->getRBraceLoc()));
225 }
226
227 RecordRequest request;
228 request.name = record->getQualifiedNameAsString();
229 request.line = anchor;
230
231 std::vector<const clang::CXXRecordDecl*> nested;
232 std::string error;
233 if (!collect_members(record, request.members, nested, error)) {
234 m_errors.push_back(where + request.name + ": " + error);
235 return;
236 }
237 if (!store(header, std::move(request), where)) {
238 return;
239 }
240 for (const clang::CXXRecordDecl* child : nested) {
241 if (file_of(manager, child->getLocation()) == header) {
242 describe_nested(child, header, anchor, manager);
243 }
244 }
245}
246
247}
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
std::vector< std::string > m_errors
Definition Collector.hpp:37
bool store(const std::filesystem::path &header, RecordRequest request, const std::string &where)
void describe_nested(const clang::CXXRecordDecl *record, const std::filesystem::path &header, unsigned line, const clang::SourceManager &manager)
void run(const clang::ast_matchers::MatchFinder::MatchResult &result) override
void attach(clang::ast_matchers::MatchFinder &finder)
Registers the matcher for the marker aliases on finder.
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
std::pair< std::shared_ptr< Kriya::BufferPipeline >, std::shared_ptr< Buffers::AudioBuffer > > record(const std::shared_ptr< Kakshya::DynamicSoundStream > &stream, uint32_t input_channel, double seconds, std::optional< uint32_t > monitor, uint32_t channel)
Record a live input channel into a stream for a number of seconds.
Definition Rigs.cpp:262
std::vector< RecordRequest > records
Definition Request.hpp:27
Every record described by the MF_DESCRIBE markers of one header.
Definition Request.hpp:25
std::vector< std::string > members
Definition Request.hpp:17
One described record: its qualified name and the names of its public data members.
Definition Request.hpp:15