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

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

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

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

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

Then how do I generate debug info?

-Jochen