DeclareCppFunction()?

Is there an equivalent of DeclareCFunction()/compileFunction() for C++ (not extern “C”) functions?

Thanks,
Andras

Hi,

You can use cling::Interpreter::declare(&T) with Transaction* T = 0; and then use getAddressOfGlobal() on the Decl in T to get the function’s address.

Cheers, Axel.

getAddressOfGlobal() needs a GlobalDecl, but AFAICT i can only get a Decl from Transaction, and i have not found a way to get the former from the latter.

See clang.llvm.org/doxygen/classclan … lDecl.html - you construct it from a clang::FunctionDecl.

Axel.

Sorry for being dense: i do not see how to get a FunctionDecl from a Transaction either (other than the wrapper fn, which is not what i need). Most likely i am missing something trivial because i am not familiar with the object relationships inside clang/llvm.

I have found a way that (almost) works, maybe describing that will help you to help me:
The first step is getting a Transaction; from that i can get an llvm::Module, whose iterator (but for some reason not reverse iterators) can be converted to llvm::Function; i fetch the function name and get the named value (an llvm::GlobalValue) from the module, finally convert that into a pointer with m_Executor->getPointerToGlobalFromJIT(). This last step is the most problematic one, as the interpreter does not provide public access to the executor.

This approach seems to be parallel to what you are suggesting (using llvm::GlobalValue/Function instead of clang::GlobalDecl/FunctionDecl), i cannot tell if that similarity is illusory or not.

Hi,

That only works if that function was actually emitted (which is not necessarily the case). Pseudo-code of what I suggest:

for (auto D: *pTransaction) {
  if (auto FD = llvm::dyn_cast<FunctionDecl>(FD))
   if (FD->getNameAsString() == "ThatsMyFunction"))
     return interp.getAddressOfGlobal(FD);
}

Makes sense?

Axel.

Thanks! This introduces some direct dependencies in our code on clang/llvm (things like dyn_cast<> and FunctionDecl::getNameAsString()) but that is fine.

Under what circumstances will there be no code emitted? (Can it happen that i provide the source code for a complete function definition, but for some reason code generation is still postponed?)

Hi,

yes. Think templated functions, or inlined function. Until you ask for an instantiation no code gets emitted. And in principle I’d like to keep the door open for cling to be lazy with emission of code until it’s needed - so even if at the moment regular functions get emitted they might not in the future, but only when referenced. It’s unlikely to happen, though.

Cheers, Axel.