cppast icon indicating copy to clipboard operation
cppast copied to clipboard

Walking a template specialization

Open rokups opened this issue 7 years ago • 7 comments

Consider std::vector<std::string> used as return type somewhere. Is there a way to somehow use that return type and walk instantiation of class std::vector<std::string> so that methods of std::vector have std::string type inserted where appropriate? Or do i have to resolve these template types manually somehow?

P.S. I hope i am not getting annoying with spamming issue tracker with random questions. If there is a better place to ask these things please let me know. P.P.S. Thank you for the help with other issues, i greatly appreciate swift response. Just never posted "thanks" messages to save you some spam in the mailbox :)

rokups avatar Feb 20 '18 15:02 rokups

No, there is no way to do that.

Why do you need it?

I hope i am not getting annoying with spamming issue tracker with random questions. If there is a better place to ask these things please let me know.

No, it's okay.

foonathan avatar Feb 21 '18 14:02 foonathan

I am using cppast to generate .net bindings for one project. Unfortunately some template containers are a big part of API and i am looking for ways to wrap these containers. I was considering to wrap every instantiation of template class as separate type with separate name so for example std::vector<std::string> on c# side would end up being non-generic StringVector type.

rokups avatar Feb 21 '18 14:02 rokups

And you are looking for an automated way of generating such a StringVector?

foonathan avatar Feb 21 '18 14:02 foonathan

Yes indeed i am. I already have wrapper generation automated for good chunk of API. these template classes are like a last piece of puzzle.

rokups avatar Feb 21 '18 14:02 rokups

Then I'm afraid there isn't an easy way to do it with the current API.

foonathan avatar Feb 21 '18 14:02 foonathan

But is there a "non-easy" way to do it ?

trying to walk through a template instantiation, it seems that inner template arguments are unexposed and available as a string (for instance for std::vector<std::shared_ptr<std::string>> unexposed_args contains std::shared_ptr<std::string> but arguments are empty), but not as an entity tree, am I wrong ?

I thought I could be able to retrieve arguments then get back to their types through the arguments array, but the template instantiation parsing code (in cppast::type_parser.cpp) seems to only provide unexposed_arguments ?

Firefly35 avatar Jan 02 '20 14:01 Firefly35

I made a pull request (https://github.com/foonathan/cppast/pull/99) that parses every template instantiation argument. There may be limitations, but up till now it worked for instantiations such as std::vector<std::shared_ptr,std::map<std::shared_ptrstd::string,std::vectorstd::string>> and provide the full AST.

The template instantiation full argument parsing is built when the option-DCPPAST_TEMPLATE_FULLARGUMENTSPARSING=ON is used upon invoking cmake.

Firefly35 avatar Jan 16 '20 14:01 Firefly35