Mangling: looking up declarations by their mangled name

tl;dr:

is there any existing facility for looking up function declarations by their name, label list and type, and if not, what would be the best way to implement it and what to look out for?

I'm working on extending SourceKit, and I've stumbled upon an issue with the CursorInfo request.
Basically, the CursorInfo request allows you to find a declaration not only by the position of a reference of that declaration in the source code, but also by its USR (which AFAIU is effectively its mangled name with the $s prefix replaced by s:).

What I've found is that right now you can only do that with type declarations. I. e. if you want to find a function declaration, or generic type parameter declaration (which is what I initially wanted to get) by its USR, you're out of luck.

So I've started working on extending the demangler to support lookup for these kinds of declarations. The first step, I figured, is to teach the demangler to find function declarations by their mangled names.
I've learned how to extract info from a function's mangled name like declaring module, name, label list, and type. (While we're at it, I've implemented an LLDB data formatter that greatly simplified working with mangle nodes, please take a look.)
Now what I want to do is to look up the matching function in that module.

So, my question is: is there any existing facility for looking up function declarations by their name, label list and type, and if not, what would be the best way to implement it and what to look out for?

(What I want is something akin to ASTBuilder::findTypeDecl, but for functions.)

cc @Slava_Pestov

ping @Slava_Pestov

ping @Slava_Pestov

Hi @broadway_lamb,
We’ve had a discussion on exactly the issue you are reporting in Implement `completionItem/resolve` by LebJe · Pull Request #432 · apple/sourcekit-lsp · GitHub. The gist is: CursorInfo only works for a narrow subset of USRs and it is not encouraged to use it. What are you trying to achieve with it? Maybe there’s a better way do it than USR-based cursor info.

I want to teach SourceKit to send substitution maps for references of generic declarations (like generic function calls), which would be useful for us at the AppCode team. The substitution map in the response may just be an array of pairs of names (GenericParamName -> SubstitutedTypeName), but that wouldn't give us much information, as we don't know the source locations of those names. So my idea was to instead send an array of pairs of USRs. But those USRs would be useless unless there was a way to get a declaration's location by its USR.

Also, substitution maps aside, looking up declarations by their USR could probably be useful, for example, for implementing the textDocument/typeDefinition request in SourceKit-LSP.

Ah, OK. In that case, I don’t think I can help here. I’ll leave it up to Slava (or someone else) to help out here.