7 constexpr const char* record_id =
"record";
11 return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9') || c ==
'_';
14 std::filesystem::path real_path(llvm::StringRef file)
16 llvm::SmallString<256> resolved;
17 if (llvm::sys::fs::real_path(file, resolved)) {
18 return { file.str() };
20 return { resolved.str().str() };
23 std::filesystem::path file_of(
const clang::SourceManager& manager, clang::SourceLocation location)
25 return real_path(manager.getFilename(manager.getExpansionLoc(location)));
28 std::set<std::string> nested_names(
const clang::CXXRecordDecl*
record)
30 std::set<std::string> names;
31 for (
const clang::Decl* declaration :
record->decls()) {
32 if (declaration->isImplicit()) {
35 const bool type_like = llvm::isa<clang::CXXRecordDecl>(declaration)
36 || llvm::isa<clang::EnumDecl>(declaration)
37 || llvm::isa<clang::TypeAliasDecl>(declaration)
38 || llvm::isa<clang::TypedefDecl>(declaration);
42 const auto* named = llvm::cast<clang::NamedDecl>(declaration);
43 if (named->getIdentifier() !=
nullptr) {
44 names.insert(named->getName().str());
50 std::string relative_to_root(
const std::string& spelling)
52 constexpr std::string_view prefix =
"MayaFlux::";
54 std::size_t cursor = 0;
55 while (cursor < spelling.size()) {
56 const std::size_t
at = spelling.find(prefix, cursor);
57 if (at == std::string::npos) {
60 out.append(spelling, cursor, at - cursor);
61 const bool starts_name =
at == 0 || (!is_word(spelling[at - 1]) && spelling[
at - 1] !=
':');
65 cursor =
at + prefix.size();
67 out.append(spelling, cursor, std::string::npos);
71 std::string qualify_nested(std::string value,
const std::string& owner,
const std::set<std::string>& names)
73 std::vector<std::string> ordered(names.begin(), names.end());
74 std::ranges::sort(ordered, [](
const std::string& a,
const std::string& b) {
return a.size() > b.size(); });
75 for (
const std::string& name : ordered) {
77 std::size_t cursor = 0;
78 while (cursor < value.size()) {
79 const std::size_t
at = value.find(name, cursor);
80 if (at == std::string::npos) {
83 const std::size_t end =
at + name.size();
84 const bool before =
at == 0 || (!is_word(value[at - 1]) && value[
at - 1] !=
':');
85 const bool after = end >= value.size() || !is_word(value[end]);
86 result.append(value, cursor, at - cursor);
87 if (before && after) {
88 result += owner +
"::" + name;
94 result.append(value, cursor, std::string::npos);
95 value = std::move(result);
100 std::string spell(clang::QualType type,
const clang::ASTContext& context)
102 const clang::QualType qualified = clang::TypeName::getFullyQualifiedType(type, context,
false);
103 return relative_to_root(qualified.getAsString(context.getPrintingPolicy()));
110 class HeaderCollector {
112 HeaderCollector(
const clang::SourceManager& manager, std::filesystem::path source_root,
113 const std::set<std::string>& excluded, std::set<std::filesystem::path>& out)
115 , m_source_root(std::
move(source_root))
116 , m_excluded(excluded)
121 void add_type(clang::QualType type)
123 type = type.getNonReferenceType();
124 while (!type.isNull() && type->getAs<clang::PointerType>() !=
nullptr) {
125 type = type->getAs<clang::PointerType>()->getPointeeType();
130 type = clang::QualType(type->getBaseElementTypeUnsafe(), 0);
132 if (
const auto* alias = type->getAs<clang::TypedefType>()) {
133 add_declaration(alias->getDecl());
136 if (
const auto* specialization = type->getAs<clang::TemplateSpecializationType>()) {
137 for (
const clang::TemplateArgument& argument : specialization->template_arguments()) {
138 if (argument.getKind() == clang::TemplateArgument::Type) {
139 add_type(argument.getAsType());
144 if (
const auto* function = type->getAs<clang::FunctionProtoType>()) {
145 add_type(function->getReturnType());
146 for (
const clang::QualType parameter : function->param_types()) {
151 if (
const clang::TagDecl* tag = type->getAsTagDecl()) {
156 void add_value(
const clang::ValueDecl* value)
158 if (
const auto* enumerator = llvm::dyn_cast<clang::EnumConstantDecl>(value)) {
159 if (
const auto* owner = llvm::dyn_cast<clang::EnumDecl>(enumerator->getDeclContext())) {
164 if (
const auto* owner = llvm::dyn_cast<clang::TagDecl>(value->getDeclContext())) {
170 void add_tag(
const clang::TagDecl* tag)
172 if (m_excluded.contains(tag->getQualifiedNameAsString())) {
175 add_declaration(tag);
178 void add_declaration(
const clang::Decl* declaration)
180 const std::filesystem::path relative
181 = file_of(m_manager, declaration->getLocation()).lexically_relative(m_source_root);
182 if (relative.empty() || *relative.begin() !=
"MayaFlux" || relative.extension() !=
".hpp") {
185 m_out.insert(relative);
188 const clang::SourceManager& m_manager;
189 std::filesystem::path m_source_root;
190 const std::set<std::string>& m_excluded;
191 std::set<std::filesystem::path>& m_out;
195 class DefaultArgumentVisitor :
public clang::RecursiveASTVisitor<DefaultArgumentVisitor> {
197 explicit DefaultArgumentVisitor(HeaderCollector& headers)
202 bool VisitExpr(clang::Expr* expression)
204 m_headers.add_type(expression->getType());
208 bool VisitDeclRefExpr(clang::DeclRefExpr* reference)
210 m_headers.add_value(reference->getDecl());
215 HeaderCollector& m_headers;
218 std::string argument_text(
const clang::ParmVarDecl* parameter,
const clang::ASTContext& context)
220 const clang::SourceManager& manager = context.getSourceManager();
221 const clang::CharSourceRange
range = manager.getExpansionRange(parameter->getDefaultArg()->getSourceRange());
222 bool invalid =
false;
223 const llvm::StringRef text = clang::Lexer::getSourceText(range, manager, context.getLangOpts(), &invalid);
224 std::string spelling = invalid ? std::string {} : text.str();
225 if (spelling.starts_with(
'=')) {
226 const std::size_t start = spelling.find_first_not_of(
" \t", 1);
227 spelling = start == std::string::npos ? std::string {} : spelling.substr(start);
240 m_simple_names.insert(entry.type_name.substr(entry.type_name.rfind(
"::") + 2));
244 m_excluded.insert(
"MayaFlux::Nodes::Network::NodeNetwork");
250 using namespace clang::ast_matchers;
251 finder.addMatcher(cxxRecordDecl(isDefinition()).bind(record_id),
this);
256 const auto*
record = result.Nodes.getNodeAs<clang::CXXRecordDecl>(record_id);
257 if (
record ==
nullptr ||
record->getIdentifier() ==
nullptr ||
record->isDependentContext()
262 const std::string qualified =
record->getQualifiedNameAsString();
267 std::optional<SignatureClass>& slot =
m_found[
static_cast<std::size_t
>(entry -
m_entries.begin())];
272 const clang::ASTContext& context = *result.Context;
273 const clang::SourceManager& manager = *result.SourceManager;
280 slot = std::move(info);
284 const std::set<std::string> nested = nested_names(
record);
287 for (
const clang::Decl* declaration :
record->decls()) {
288 if (
const auto* declared_template = llvm::dyn_cast<clang::FunctionTemplateDecl>(declaration)) {
289 if (llvm::isa<clang::CXXConstructorDecl>(declared_template->getTemplatedDecl())) {
290 m_errors.push_back(entry->name +
": constructor template needs explicit generator support");
295 const auto* constructor = llvm::dyn_cast<clang::CXXConstructorDecl>(declaration);
296 if (constructor ==
nullptr || constructor->isImplicit() || constructor->isDeleted()
297 || constructor->getAccess() != clang::AS_public) {
303 for (
const clang::ParmVarDecl* parameter : constructor->parameters()) {
305 item.
type = spell(parameter->getType(), context);
306 if (item.
type.find(
"(*)") != std::string::npos || item.
type.find(
"(&)") != std::string::npos) {
307 m_errors.push_back(entry->name +
": cannot spell parameter " + std::to_string(number)
308 +
" as a method parameter");
311 item.
name = parameter->getNameAsString();
312 if (item.
name.empty()) {
313 item.
name =
"arg" + std::to_string(number);
316 headers.add_type(parameter->getType());
317 if (parameter->hasDefaultArg() && !parameter->hasUnparsedDefaultArg()
318 && !parameter->hasUninstantiatedDefaultArg()) {
319 const std::string text = argument_text(parameter, context);
321 m_errors.push_back(entry->name +
": cannot recover default for parameter " + std::to_string(number));
324 item.
default_value = qualify_nested(text, relative_to_root(qualified), nested);
325 DefaultArgumentVisitor visitor(headers);
326 visitor.TraverseStmt(
const_cast<clang::Expr*
>(parameter->getDefaultArg()));
335 m_errors.push_back(entry->name +
": no public source-declared constructor found");
338 slot = std::move(info);
343 std::vector<SignatureClass> found;
344 for (
const auto& slot :
m_found) {
346 found.push_back(*slot);
354 std::vector<std::string> names;
355 for (std::size_t i = 0; i <
m_entries.size(); ++i) {
std::vector< double > range(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Value range (max - min) per window.
void move(EditableText &s, int n)
Move the cursor by n codepoints.
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.
std::filesystem::path header
One class of the Creator registry: the method name, its qualified type and its header.