Generation of CallExpr to builtin function

In our application we need to generate a new CallExpr to a builtin
function, but we are unable to find a way to do that.

In SemaDecl.cpp we have found

/// LazilyCreateBuiltin - The specified Builtin-ID was first used at
/// file scope. lazily create a decl for it. ForRedeclaration is true
/// if we're creating this built-in in anticipation of redeclaring the
/// built-in.
NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
                                     Scope *S, bool ForRedeclaration,
                                     SourceLocation Loc);

but it's inaccessible (and does also other things).

What's the right way to do that?

You can find the built-in function using Sema::ActOnIdentifierExpr, which will have the effect of implicitly declaring the builtin (or finding it if it already existed) and then building a DeclRefExpr that you can use as the callee of your call expression.

  - Doug

Douglas Gregor ha scritto:

In our application we need to generate a new CallExpr to a builtin
function, but we are unable to find a way to do that.

In SemaDecl.cpp we have found

/// LazilyCreateBuiltin - The specified Builtin-ID was first used at
/// file scope. lazily create a decl for it. ForRedeclaration is true
/// if we're creating this built-in in anticipation of redeclaring the
/// built-in.
NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
                                    Scope *S, bool ForRedeclaration,
                                    SourceLocation Loc);

but it's inaccessible (and does also other things).

What's the right way to do that?

You can find the built-in function using Sema::ActOnIdentifierExpr,
which will have the effect of implicitly declaring the builtin (or
finding it if it already existed) and then building a DeclRefExpr that
you can use as the callee of your call expression.

Sorry, I think I have not explained well the problem.

What we need is a way for a program that uses clang as a library to
create a CallExpr to a builtin function.

Of course inside clang we would be able to use Sema to do that, but this
is not the case.

I think that having something similar to

NamedDecl* ASTContext::BuiltinDecl(IdentifierInfo *II, Builtin::ID BID);

could be a solution.

Douglas Gregor ha scritto:

In our application we need to generate a new CallExpr to a builtin
function, but we are unable to find a way to do that.

In SemaDecl.cpp we have found

/// LazilyCreateBuiltin - The specified Builtin-ID was first used at
/// file scope. lazily create a decl for it. ForRedeclaration is true
/// if we're creating this built-in in anticipation of redeclaring the
/// built-in.
NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
                                   Scope *S, bool ForRedeclaration,
                                   SourceLocation Loc);

but it's inaccessible (and does also other things).

What's the right way to do that?

You can find the built-in function using Sema::ActOnIdentifierExpr,
which will have the effect of implicitly declaring the builtin (or
finding it if it already existed) and then building a DeclRefExpr that
you can use as the callee of your call expression.

Sorry, I think I have not explained well the problem.

What we need is a way for a program that uses clang as a library to
create a CallExpr to a builtin function.

Of course inside clang we would be able to use Sema to do that, but this
is not the case.

Ah, okay.

I think that having something similar to

NamedDecl* ASTContext::BuiltinDecl(IdentifierInfo *II, Builtin::ID BID);

could be a solution.

This would be a useful refactoring. The IdentifierInfo itself knows whether it is a builtin, so BuiltinDecl could just take an IdentifierInfo pointer or reference. Will you provide a patch that provides this function?

  - Doug