7 constexpr const char* request_id =
"request";
8 constexpr const char* request_pattern =
"mf_reflection_request_";
10 bool nameable(
const clang::CXXRecordDecl*
record)
12 if (
record->isDependentContext()) {
15 const clang::Decl* current =
record;
17 if (current->getAccess() == clang::AS_private || current->getAccess() == clang::AS_protected) {
20 const clang::DeclContext* parent = current->getDeclContext();
21 if (parent->isTranslationUnit()) {
24 if (
const auto* space = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
25 if (space->isAnonymousNamespace()) {
28 }
else if (
const auto* outer = llvm::dyn_cast<clang::CXXRecordDecl>(parent)) {
29 if (outer->getName().empty() || outer->isDependentContext()) {
32 }
else if (!llvm::isa<clang::LinkageSpecDecl>(parent)) {
35 current = clang::Decl::castFromDeclContext(parent);
39 void collect_nested(clang::QualType type, std::vector<const clang::CXXRecordDecl*>& out)
41 const clang::Type* underlying = type.getNonReferenceType()->getBaseElementTypeUnsafe();
42 const clang::CXXRecordDecl*
record = underlying->getAsCXXRecordDecl();
49 if (
const auto* specialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
record)) {
50 if (!
record->isInStdNamespace()) {
53 for (
const clang::TemplateArgument& argument : specialization->getTemplateArgs().asArray()) {
54 if (argument.getKind() == clang::TemplateArgument::Type) {
55 collect_nested(argument.getAsType(), out);
63 if (std::ranges::find(out,
record) == out.end()) {
68 bool collect_members(
const clang::CXXRecordDecl*
record, std::vector<std::string>& names,
69 std::vector<const clang::CXXRecordDecl*>& nested, std::string& error)
71 for (
const auto& base :
record->bases()) {
72 if (base.getAccessSpecifier() != clang::AS_public) {
75 const clang::CXXRecordDecl* parent = base.getType()->getAsCXXRecordDecl();
76 if (parent !=
nullptr) {
77 parent = parent->getDefinition();
79 if (parent ==
nullptr) {
80 error =
"a public base class is not a complete record";
83 if (!collect_members(parent, names, nested, error)) {
87 for (
const clang::FieldDecl* field :
record->fields()) {
88 if (field->getAccess() != clang::AS_public) {
91 const std::string name = field->getNameAsString();
93 error =
"an anonymous member cannot use a member-pointer descriptor";
96 if (field->isBitField() || field->getType()->isReferenceType()) {
97 error =
"member " + name +
" is a bit-field or reference and cannot use a member-pointer descriptor";
100 if (std::ranges::find(names, name) != names.end()) {
101 error =
"member name " + name +
" appears in more than one class of the hierarchy";
104 names.push_back(name);
105 collect_nested(field->getType(), nested);
110 std::filesystem::path real_path(llvm::StringRef file)
112 llvm::SmallString<256> resolved;
113 if (llvm::sys::fs::real_path(file, resolved)) {
114 return { file.str() };
116 return { resolved.str().str() };
119 std::filesystem::path file_of(
const clang::SourceManager& manager, clang::SourceLocation location)
121 return real_path(manager.getFilename(manager.getExpansionLoc(location)));
135 for (
const auto& [other_header, other] :
m_requests) {
136 if (other_header != header
138 m_errors.push_back(where + request.
name +
": already described in " + other_header.string());
142 entry.
records.push_back(std::move(request));
147 unsigned line,
const clang::SourceManager& manager)
150 request.
name =
record->getQualifiedNameAsString();
153 std::vector<const clang::CXXRecordDecl*> nested;
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);
160 if (!
store(header, std::move(request), where)) {
163 for (
const clang::CXXRecordDecl* child : nested) {
164 if (file_of(manager, child->getLocation()) == header) {
172 using namespace clang::ast_matchers;
173 finder.addMatcher(typeAliasDecl(matchesName(request_pattern)).bind(request_id),
this);
178 const auto* alias = result.Nodes.getNodeAs<clang::TypeAliasDecl>(request_id);
179 if (alias ==
nullptr) {
185 const clang::SourceManager& manager = *result.SourceManager;
186 const clang::SourceLocation location = manager.getExpansionLoc(alias->getBeginLoc());
187 if (!location.isValid() || manager.isInSystemHeader(location)) {
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();
197 if (!alias->getDeclContext()->getRedeclContext()->isFileContext()) {
198 m_errors.push_back(where +
"MF_DESCRIBE must appear at namespace scope");
202 const clang::CXXRecordDecl*
record = alias->getUnderlyingType()->getAsCXXRecordDecl();
207 m_errors.push_back(where + spelling +
": request must name a complete struct or class");
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");
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)) {
223 if (outermost !=
nullptr && outermost->getRBraceLoc().isValid()) {
224 anchor = manager.getSpellingLineNumber(manager.getExpansionLoc(outermost->getRBraceLoc()));
228 request.
name =
record->getQualifiedNameAsString();
229 request.
line = anchor;
231 std::vector<const clang::CXXRecordDecl*> nested;
233 if (!collect_members(
record, request.
members, nested, error)) {
234 m_errors.push_back(where + request.
name +
": " + error);
237 if (!
store(header, std::move(request), where)) {
240 for (
const clang::CXXRecordDecl* child : nested) {
241 if (file_of(manager, child->getLocation()) == header) {
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
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.