Is there any way to get a list of all symbols in a module?

I'm working on a web-based tool to browse the contents of a swift module, and I'm looking for a way to get a list of all the symbols (classes, structs, enums, functions, members) by name. It should be possible to reconstruct this by parsing the AST, but is there an easier way to get this information?

I suppose something like this is already done for the XCode symbol navigator, and probably Swift LSP. What would be the easiest way to achieve it? Maybe it's possible to parse DSYM files?

edit: ideally I would be able to do this in the context of a webserver running Linux - I'm not sure whether dSYM files are cross-platform

Could you talk more about why you want to extract this information? Since you mention the Xcode symbol navigator, I suspect that the right unit of granularity you're looking for is not a single Swift module but a module DAG (i.e. including dependencies), because you probably want to understand cross-module references.

My best guess is that you'd would want to use SourceKit LSP for this; it already understands Swift code by talking to the Swift compiler. I don't know if LSP has APIs for "give me everything" though.

Depending on the use case, SourceKit might be sufficient. The protocol spec here describes its capabilities pretty well. It doesn't directly dump symbols as far as I can tell, but it should be possible to find all the declarations in a module and mangle them if necessary.

SourceKit probably won't be sufficient if you want to work with serialized modules though as opposed to source code. In that case I think you'd probably need to interface with the compiler libraries more directly (I'm not an expert in this area though).

Terms of Service

Privacy Policy

Cookie Policy