how to get c++ name for llvm::Type

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

how to get c++ name for llvm::Type

Jochen Wilhelmy
Hi!

if I compile code from c++ to llvm using clang I want to reverse-lookup
the type names of some simple structures e.g.
struct Foo
{
     int bar;
};

I would need a function that returns the name or clang::TagDecl
given the llvm::Type.

How can this be done?

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how to get c++ name for llvm::Type

Eli Friedman
On Wed, Jul 7, 2010 at 8:44 AM, Jochen Wilhelmy <[hidden email]> wrote:

> Hi!
>
> if I compile code from c++ to llvm using clang I want to reverse-lookup
> the type names of some simple structures e.g.
> struct Foo
> {
>     int bar;
> };
>
> I would need a function that returns the name or clang::TagDecl
> given the llvm::Type.
>
> How can this be done?

In general, it can't; LLVM types are purely structural, so there may
be many C++ types which map to the same LLVM type.  What are you
trying to do?

-Eli

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how to get c++ name for llvm::Type

David Chisnall
In reply to this post by Jochen Wilhelmy
On 7 Jul 2010, at 16:44, Jochen Wilhelmy wrote:

> How can this be done?

The first question is 'can this be done?' and the answer is 'no'.  LLVM types encode the structure of a type, but nothing else.  Two structs in C with the same layout will be mapped to the same LLVM type, therefore you can not go from an LLVM type back to a C type, because there is no unambiguous mapping.

That said, there are a few things that you can do that might be 'good enough.'

You might iterate through the types declared in a given compilation unit, generate the corresponding LLVM types, and produce a reverse mapping.  This would work for unambiguous types, but not for ambiguous ones.  For example, the Cocoa standard types NSPoint and NSSize would both map to the same LLVM type.

Alternatively, you could use the LLVM debugging info, which does include the source type name.  This is not completely trivial to parse, but is relatively easy.  This will tell you the source-language type of a specific value, but not of an LLVM type (some things, such as source-language unions, map to multiple LLVM types).

David
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how to get c++ name for llvm::Type

Jochen Wilhelmy
In reply to this post by Eli Friedman

> In general, it can't; LLVM types are purely structural, so there may
> be many C++ types which map to the same LLVM type.  What are you
> trying to do?
>    
I convert the resulting LLVM IR to some other representation and for
this I need some
named types (i.e. intrinsic types). It would be ok to use __attribute__
on the type to set the name
as attribute that makes into the backend. something like
__attribute__(myType("Foo")) struct Foo
{
...
};

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how to get c++ name for llvm::Type

Jochen Wilhelmy
In reply to this post by David Chisnall
Then how do I generate debug info?

-Jochen

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev