Adding a FieldDecl

Hello,

i want to add a FieldDecl to a class/struct. For example an integer variable.
So far my code looks like this:

clang::CXXRecordDecl* d;
...
clang::ASTContext& astContext = d->getASTContext();
clang::FieldDecl* fd = clang::FieldDecl::Create(
    astContext,
    d,
    clang::SourceLocation(),
    0 /*identifierinfo*/,
    astContext.IntTy,
    0 /*typesourceinfo*/,
    0 /*bitwidth*/,
    0 /*mutable*/
);

But i'm missing some pieces. Where do i get the IdentifierInfo for my
new field? The field
should be called 'foo' for example. Where do i get the TypeSourceInfo?
Is it enough
to specify the SourceLocation like this? Am i missing anything else?

Thanks

Hello,

i want to add a FieldDecl to a class/struct. For example an integer variable.
So far my code looks like this:

clang::CXXRecordDecl* d;

clang::ASTContext& astContext = d->getASTContext();
clang::FieldDecl* fd = clang::FieldDecl::Create(
astContext,
d,
clang::SourceLocation(),
0 /identifierinfo/,
astContext.IntTy,
0 /typesourceinfo/,
0 /bitwidth/,
0 /mutable/
);

But i’m missing some pieces. Where do i get the IdentifierInfo for my
new field?

Use the get() function of the ASTContext’s identifier table.

The field
should be called ‘foo’ for example. Where do i get the TypeSourceInfo?

You can fake a TypeSourceInfo using ASTContext’s getTrivialTypeSourceInfo()

Is it enough
to specify the SourceLocation like this? Am i missing anything else?

It’s usually best to have some SourceLocation information. You could re-use the source location of the class/struct.

  • Doug