Find template types of std::map

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Find template types of std::map

Sebastian Dreßler
Hi,

for a current project I'm required to get the types of the template
arguments key and value for std::map<K,V>. I've noticed that the STL
implementation used by Clang defines the type of the map as

     %"class.std::map" = type { %"class.std::_Rb_tree" }

which then is further defined and finally ends as

     %"struct.std::_Rb_tree_node_base" = type {
         i32,
         %"struct.std::_Rb_tree_node_base"*,
         %"struct.std::_Rb_tree_node_base"*,
         %"struct.std::_Rb_tree_node_base"*
     }

However, indifferent what K and V are and how many different maps are
instantiated, it always ends at this definition. But, I also noticed, that

     %"struct.std::pair" = type { i32, double }

is defined, which then provides the information I need.

Now the problem arises to connect %"class.std::map" to
%"struct.std::pair" correctly. To solve this I looked at the IR and
noticed, that the std::pair's are always defined in the reverse order of
the definitions of std::map. So I think about using some index to find
the correct std::pair.

 From this idea two issues arise:

1. I feel that this approach is not that stable as it should be, for
instance, I think that the ordering of instantiation I'm making use of
may change.

2. I'm unable to retrieve all defined named types from the module. I
know there is LLVMContextImpl but this class is a forward declaration
and therefore I'm not able to access LLVMContextImpl::NamedStructTypes.


Thanks for any suggestions.


Best regards,
Sebastian

--
Mit freundlichen Grüßen / Kind regards

Sebastian Dreßler

Zuse Institute Berlin (ZIB)
Takustraße 7
D-14195 Berlin-Dahlem
Germany

[hidden email]
Phone: +49 30 84185-261

http://www.zib.de/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Find template types of std::map

Sebastian Dreßler
Sebastian Dreßler wrote
Now the problem arises to connect %"class.std::map" to
%"struct.std::pair" correctly. To solve this I looked at the IR and
noticed, that the std::pair's are always defined in the reverse order of
the definitions of std::map. So I think about using some index to find
the correct std::pair.
I just thought again about this approach: this actually is the most inelegant way I could come up with. The following might be better: I will try to use the Clang library directly to convert the C++ type string, e. g. std::string, int, ..., to a LLVM type.


However, any other suggestions are welcome. Maybe someone else already had this sort of problem?


Best regards,
Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Find template types of std::map

Eli Friedman-2
In reply to this post by Sebastian Dreßler
On Tue, Oct 16, 2012 at 4:17 AM, Sebastian Dreßler <[hidden email]> wrote:
> Hi,
>
> for a current project I'm required to get the types of the template
> arguments key and value for std::map<K,V>. I've noticed that the STL
> implementation used by Clang defines the type of the map as
>
>     %"class.std::map" = type { %"class.std::_Rb_tree" }

Trying to dig template arguments out of LLVM IR types is a bad idea.
Try a C++ demangler on a related function name, or the debug
information.

-Eli

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Find template types of std::map

Sebastian Dreßler
Eli Friedman-2 wrote
On Tue, Oct 16, 2012 at 4:17 AM, Sebastian Dreßler <[hidden email]> wrote:
> Hi,
>
> for a current project I'm required to get the types of the template
> arguments key and value for std::map<K,V>. I've noticed that the STL
> implementation used by Clang defines the type of the map as
>
>     %"class.std::map" = type { %"class.std::_Rb_tree" }

Trying to dig template arguments out of LLVM IR types is a bad idea.
Try a C++ demangler on a related function name, or the debug
information.
Thanks for the hint, I'll try that. However, when I successfully determined the name of the template parameters, I'll have to find the LLVM IR std::pair definition nevertheless. So, is there any function providing all names of named structs from a module? When I dump the module, I'm able to see all those structs, but I didn't find a function for e.g. iterating them.


Best,
Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Find template types of std::map

Sebastian Dreßler
Sebastian Dreßler wrote
So, is there any function providing all names of named structs from a module? When I dump the module, I'm able to see all those structs, but I didn't find a function for e.g. iterating them.
Just realized, that there is llvm::TypeFinder, which I've never seen before, but does exactly that what I want.



Thanks for the help,
Sebastian