How can I get the detailed information of Structure/Union

Hi all,

I can use isStructureType() and isUnionType() to check whether an Expr is structure type or union type. Now I want to get the structure/union type’s detailed information, such as the fields and their types of a structure. How can I get these information? Thanks a lot.

Hi all,

I can use isStructureType() and isUnionType() to check whether an Expr is
structure type or union type. Now I want to get the structure/union type's
detailed information, such as the fields and their types of a structure. How
can I get these information? Thanks a lot.

So you have a Type already (since isStructureType isn't a member of
Expr, it's a member of Type), so looking at the Type documentation:

http://clang.llvm.org/doxygen/classclang_1_1Type.html

& then follow through to the source of isStructureType (
http://clang.llvm.org/doxygen/Type_8cpp_source.html#l00366 ) you'll
see it's simply:

if (const RecordType *RT = getAs<RecordType>())
  return RT->getDecl()->isStruct();
return false;

So you can do something similar -
cast<RecordDecl>(cast<RecordType>(T)->getDecl()) and then once you've
got the RecordDecl you can use its API to access the list of members,
etc. (you may need to make sure it's a definition of a record, not
just a declaration, etc)

David:

Thank you very much for your help. Now I can get the the information of a structure’s fields. :slight_smile:

By the way, I want to know that does Clang provide us the methods to get the length of some data type? For examples, the length of int is 4 bytes, the length of char is one byte and so on.
And is there any method that can provide me the information of alignment for some structure type?

Thank you.

David:

Thank you very much for your help. Now I can get the the information of a
structure's fields. :slight_smile:

By the way, I want to know that does Clang provide us the methods to get the
length of some data type? For examples, the length of int is 4 bytes, the
length of char is one byte and so on.
And is there any method that can provide me the information of alignment for
some structure type?

I imagine you could discover what code to use for this by, say,
writing the code:

int main() {
  static_assert(sizeof(int) == 42, "foo");
}

compiling it, breaking on the compilation error, then working
backwards through the stack/calls to see where the sizeof value was
derived. I tend to take this sort of approach when I don't know where
something is in the compiler.

If I had to guess, I would assume somewhere on the ASTContext, if it's
not on the clang Type itself - but I could be wrong.

If I had to guess, I would assume somewhere on the ASTContext, if it’s not on the clang Type itself - but I could be wrong.

You’re correct, it’s called ASTContext::getTypeSize: http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html#a3e16bd856974e4ac9710e0c507a60cee

And is there any method that can provide me the information of alignment for some structure type?

To get such information - like type size, and alignment, and so on - you usually will want to check the ASTContext type first. There is a method for getting alignment, but finding it among ASTContext’s methods falls to you. :slight_smile:

Thank you**,** Gabor.

With your tips, I find the class clang::ASTRecordLayout. With an ASTRecordLayout object, I can get the information of data size and alignment of some structure type.