36#include "cling/Interpreter/CompilationOptions.h" 
   37#include "cling/Interpreter/Interpreter.h" 
   38#include "cling/Interpreter/LookupHelper.h" 
   39#include "cling/Interpreter/Transaction.h" 
   40#include "cling/Interpreter/Value.h" 
   41#include "cling/Utils/AST.h" 
   43#include "clang/AST/ASTContext.h" 
   44#include "clang/AST/Decl.h" 
   45#include "clang/AST/DeclCXX.h" 
   46#include "clang/AST/GlobalDecl.h" 
   47#include "clang/AST/PrettyPrinter.h" 
   48#include "clang/AST/QualTypeNames.h" 
   49#include "clang/AST/RecordLayout.h" 
   50#include "clang/AST/Type.h" 
   51#include "clang/Frontend/CompilerInstance.h" 
   52#include "clang/Lex/Preprocessor.h" 
   53#include "clang/Sema/Sema.h" 
   54#include "clang/Sema/Lookup.h" 
   56#include "llvm/ADT/APInt.h" 
   57#include "llvm/ExecutionEngine/ExecutionEngine.h" 
   58#include "llvm/ExecutionEngine/GenericValue.h" 
   59#include "llvm/Support/Casting.h" 
   60#include "llvm/Support/raw_ostream.h" 
   61#include "llvm/IR/LLVMContext.h" 
   62#include "llvm/IR/DerivedTypes.h" 
   63#include "llvm/IR/Function.h" 
   64#include "llvm/IR/GlobalValue.h" 
   65#include "llvm/IR/Module.h" 
   66#include "llvm/IR/Type.h" 
   68#include "clang/Sema/SemaInternal.h" 
   76using llvm::APSInt, llvm::raw_string_ostream;
 
   77using std::string, std::map, std::ostringstream, std::make_pair;
 
   98   clang::Expr::EvalResult 
evalRes;
 
   99   if (E->EvaluateAsInt(
evalRes, C, Expr::SE_NoSideEffects)) {
 
  102      APSInt res = 
evalRes.Val.getInt();
 
  104      V = cling::Value(C.IntTy, 
interp);
 
  108        V.setLongLong(res.getSExtValue());
 
  110        V.setULongLong(res.getZExtValue());
 
  116   Policy.SuppressTagKeyword = 
true;
 
  117   Policy.SuppressUnwrittenScope = 
false;
 
  118   Policy.SuppressInitializers = 
false;
 
  119   Policy.AnonymousTagLocations = 
false;
 
  121   raw_string_ostream out(buf);
 
  122   E->printPretty(out, 
nullptr, Policy, 0);
 
 
  132   return GetDecl()->getMinRequiredArguments();
 
 
  149   cling::utils::Transform::Config Config;
 
  150   QT = cling::utils::Transform::GetPartiallyDesugaredType(C, 
QT, Config, 
true);
 
 
  159   Policy.SuppressTagKeyword = 
true;
 
  160   Policy.SuppressUnwrittenScope = 
true;
 
  163      QualType 
QT(
TD->getTypeForDecl(), 0);
 
  167      raw_string_ostream stream(
name);
 
  168      ND->getNameForDiagnostic(stream, Policy, 
true);
 
 
  189   if (
QT->isFunctionPointerType()) {
 
  202   } 
else if (
QT->isMemberPointerType()) {
 
  215   } 
else if (
QT->isPointerType()) {
 
  218   } 
else if (
QT->isReferenceType()) {
 
  224   if (
QT->isArrayType()) {
 
 
  250   RD = 
RD->getDefinition();
 
  251   assert(
RD && 
"expecting a definition");
 
  253   if (
RD->hasSimpleCopyConstructor())
 
  256   for (
auto *
Ctor : 
RD->ctors()) {
 
  257      if (
Ctor->isCopyConstructor()) {
 
  258         return Ctor->isDeleted();
 
  262   assert(0 && 
"did not find a copy constructor?");
 
 
  281   cling::Interpreter::PushTransactionRAII 
RAII(
fInterp);
 
  283   for (
unsigned i = 0U; i < 
N; ++i) {
 
  285      QualType 
Ty = 
PVD->getType();
 
  286      QualType 
QT = 
Ty.getCanonicalType();
 
 
  353         for (
unsigned i = 0U; i < 
N; ++i) {
 
  364            QualType 
Ty = 
PVD->getType();
 
  365            QualType 
QT = 
Ty.getCanonicalType();
 
  371         if (
FD->isVariadic())
 
  395         llvm::raw_string_ostream stream(
name);
 
  396         FD->getNameForDiagnostic(stream, 
FD->getASTContext().getPrintingPolicy(), 
false);
 
  405   cling::Interpreter::PushTransactionRAII 
RAII(
fInterp);
 
  407   for (
unsigned i = 0U; i < 
N; ++i) {
 
  409      QualType 
Ty = 
PVD->getType();
 
  410      QualType 
QT = 
Ty.getCanonicalType();
 
 
  463   buf << 
"if (ret) {\n";
 
 
  527   return fMethod->GetDecl()->getDeclContext();
 
 
  533   assert(
FD && 
"generate_wrapper called without a function decl!");
 
  548      FunctionDecl::TemplatedKind 
TK = 
FD->getTemplatedKind();
 
  550      case FunctionDecl::TK_NonTemplate: {
 
  560      case FunctionDecl::TK_FunctionTemplate: {
 
  563         ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a function template!");
 
  566      case FunctionDecl::TK_MemberSpecialization: {
 
  573         if (!
FD->isTemplateInstantiation()) {
 
  588            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a member function " 
  589                                                    "instantiation with no pattern!");
 
  592         FunctionDecl::TemplatedKind 
PTK = 
Pattern->getTemplatedKind();
 
  596            (
PTK == FunctionDecl::TK_NonTemplate) ||
 
  599            ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
 
  605         } 
else if (!
Pattern->hasBody()) {
 
  606            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a member function " 
  607                                                    "instantiation with no body!");
 
  610         if (
FD->isImplicitlyInstantiable()) {
 
  614      case FunctionDecl::TK_FunctionTemplateSpecialization: {
 
  619         if (!
FD->isTemplateInstantiation()) {
 
  634            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a function template" 
  635                                                    "instantiation with no pattern!");
 
  638         FunctionDecl::TemplatedKind 
PTK = 
Pattern->getTemplatedKind();
 
  642            (
PTK == FunctionDecl::TK_NonTemplate) ||
 
  645            ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
 
  653            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a function template" 
  654                                                    "instantiation with no body!");
 
  657         if (
FD->isImplicitlyInstantiable()) {
 
  661      case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
 
  669         if (!
FD->isTemplateInstantiation()) {
 
  684            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a dependent function template" 
  685                                                    "instantiation with no pattern!");
 
  688         FunctionDecl::TemplatedKind 
PTK = 
Pattern->getTemplatedKind();
 
  692            (
PTK == FunctionDecl::TK_NonTemplate) ||
 
  695            ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
 
  703            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a dependent function template" 
  704                                                    "instantiation with no body!");
 
  707         if (
FD->isImplicitlyInstantiable()) {
 
  714         ::Error(
"TClingCallFunc::make_wrapper", 
"Unhandled template kind!");
 
  737      clang::FunctionDecl *
FDmod = 
const_cast<clang::FunctionDecl *
>(
FD);
 
  738      clang::Sema &S = 
fInterp->getSema();
 
  740      cling::Interpreter::PushTransactionRAII 
RAII(
fInterp);
 
  745         ::Error(
"TClingCallFunc::make_wrapper", 
"Failed to force template instantiation!");
 
  750      FunctionDecl::TemplatedKind 
TK = 
Definition->getTemplatedKind();
 
  752      case FunctionDecl::TK_NonTemplate: {
 
  755            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a deleted function!");
 
  757         } 
else if (
Definition->isLateTemplateParsed()) {
 
  758            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a late template parsed " 
  769      case FunctionDecl::TK_FunctionTemplate: {
 
  772         ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a function template!");
 
  775      case FunctionDecl::TK_MemberSpecialization: {
 
  780            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a deleted member function " 
  781                                                    "of a specialization!");
 
  783         } 
else if (
Definition->isLateTemplateParsed()) {
 
  784            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a late template parsed " 
  785                                                    "member function of a specialization!");
 
  795      case FunctionDecl::TK_FunctionTemplateSpecialization: {
 
  801            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a deleted function " 
  802                                                    "template specialization!");
 
  804         } 
else if (
Definition->isLateTemplateParsed()) {
 
  805            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a late template parsed " 
  806                                                    "function template specialization!");
 
  816      case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
 
  825            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a deleted dependent function " 
  826                                                    "template specialization!");
 
  828         } 
else if (
Definition->isLateTemplateParsed()) {
 
  829            ::Error(
"TClingCallFunc::make_wrapper", 
"Cannot make wrapper for a late template parsed " 
  830                                                    "dependent function template specialization!");
 
  843         ::Error(
"TClingCallFunc::make_wrapper", 
"Unhandled template kind!");
 
  869   buf << 
"#pragma clang diagnostic push\n" 
  870          "#pragma clang diagnostic ignored \"-Wformat-security\"\n" 
  871          "__attribute__((used)) " 
  872          "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n" 
  873          "extern \"C\" void ";
 
  875   buf << 
"(void* obj, int nargs, void** args, void* ret)\n" 
  886         buf << 
"if (nargs == " << 
N << 
") {\n";
 
  896          "#pragma clang diagnostic pop";
 
 
  917         if ((
N == 0 && 
SpecMemKind == clang::CXXSpecialMemberKind::DefaultConstructor) ||
 
  919              (
SpecMemKind == clang::CXXSpecialMemberKind::CopyConstructor || 
SpecMemKind == clang::CXXSpecialMemberKind::MoveConstructor))) {
 
  930   QualType 
QT = 
FD->getReturnType().getCanonicalType();
 
  931   if (
QT->isVoidType()) {
 
  947      buf << 
"if (ret) {\n";
 
 
 1035      ::Error(
"TClingCallFunc::make_wrapper",
 
 1036            "Failed to compile\n  ==== SOURCE BEGIN ====\n%s\n  ==== SOURCE END ====",
 
 
 1052         ::Error(
"TClingCallFunc::exec",
 
 1053                 "Not enough arguments provided for %s (%d instead of the minimum %d)",
 
 1058         ::Error(
"TClingCallFunc::exec",
 
 1059                 "Too many arguments provided for %s (%d instead of the minimum %d)",
 
 1066            ::Error(
"TClingCallFunc::exec",
 
 1067                    "The method %s is called without an object.",
 
 1073      for (
unsigned i = 0; i < 
num_args; ++i) {
 
 1085           QT = 
FD->getParamDecl(i)->getType();
 
 1086         QT = 
QT.getCanonicalType();
 
 1087         if (
QT->isReferenceType() || 
QT->isRecordType()) {
 
 1096            if (
QT->isBuiltinType() && !C.hasSameType(
QT, 
fArgVals[i].getType())) {
 
 1104#define X(type, name)                                                   \ 
 1105                  case BuiltinType::name: fArgVals[i] = cling::Value::Create(*fInterp, fArgVals[i].castAs<type>()); break; 
 
 1122   if (llvm::isa<CXXConstructorDecl>(
FD)) {
 
 1126     QualType 
ClassTy(
TD->getTypeForDecl(), 0);
 
 1127     QT = Context.getLValueReferenceType(
ClassTy);
 
 1130     QT = 
FD->getReturnType().getCanonicalType();
 
 1133     if (
QT->isRecordType() || 
QT->isMemberDataPointerType())
 
 1134       return exec(address, 
ret.getPtr());
 
 1136   exec(address, 
ret.getPtrAddress());
 
 
 1145                                          gDebug > 5 ? cling::LookupHelper::WithDiagnostics
 
 1146                                          : cling::LookupHelper::NoDiagnostics);
 
 1151      if (!val.isValid()) {
 
 1153         ::Error(
"TClingCallFunc::EvaluateArgList",
 
 1154               "Bad expression in parameter %d of '%s'!",
 
 
 1167      ::Error(
"TClingCallFunc::Exec(address, interpVal)",
 
 1168            "Called with no wrapper, not implemented!");
 
 1172      exec(address, 
nullptr);
 
 1175   cling::Value *val = 
reinterpret_cast<cling::Value *
>(
interpVal->GetValAddr());
 
 
 1179template <
typename T>
 
 1184      ::Error(
"TClingCallFunc::ExecT",
 
 1185            "Called with no wrapper, not implemented!");
 
 1194   if (
ret.needsManagedAllocation())
 
 1197   return ret.castAs<T>();
 
 
 1220      ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
 
 1221            "Called with no wrapper, not implemented!");
 
 1224   (*fWrapper)(address, 
nargs, 
const_cast<void **
>(args), 
ret);
 
 
 1231      ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
 
 1232            "Called with no wrapper, not implemented!");
 
 
 1241                                             void *address , 
unsigned long nary )
 
 1243   if (!
info->IsValid()) {
 
 1244      ::Error(
"TClingCallFunc::ExecDefaultConstructor", 
"Invalid class info!");
 
 1253   clang::Decl *D = 
const_cast<clang::Decl *
>(
info->GetDecl());
 
 1255   if (Cpp::IsClass(D) || Cpp::IsConstructor(D)) {
 
 1257      return Cpp::Construct(D, address, 
nary);
 
 1260   ::Error(
"TClingCallFunc::ExecDefaultConstructor", 
"ClassInfo missing a valid Scope/Constructor");
 
 
 1267   if (!
info->IsValid()) {
 
 1268      ::Error(
"TClingCallFunc::ExecDestructor", 
"Invalid class info!");
 
 1277   ::Error(
"TClingCallFunc::ExecDestructor", 
"Called with no wrapper, not implemented!");
 
 
 1330      ::Error(
"TClingCallFunc::IFacePtr(kind)",
 
 1331            "Attempt to get interface while invalid.");
 
 
 1350   for (
int i = 0; i < 
nparam; ++i) {
 
 
 1375   if (!
info->IsValid()) {
 
 1376      ::Error(
"TClingCallFunc::SetFunc", 
"Class info is invalid!");
 
 
 1420   if (!
info->IsValid()) {
 
 1421      ::Error(
"TClingCallFunc::SetFuncProto", 
"Class info is invalid!");
 
 
 1440                                  const llvm::SmallVectorImpl<clang::QualType> &
proto,
 
 1449   if (!
info->IsValid()) {
 
 1450      ::Error(
"TClingCallFunc::SetFuncProto", 
"Class info is invalid!");
 
 
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
 
static void EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
 
static bool IsCopyConstructorDeleted(QualType QT)
 
static void GetTypeAsString(QualType QT, string &type_name, ASTContext &C, PrintingPolicy Policy)
 
static unsigned long long gWrapperSerial
 
static void GetDeclName(const clang::Decl *D, ASTContext &Context, std::string &name)
 
static const string kIndentString("   ")
 
static void indent(ostringstream &buf, int indent_level)
 
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
 
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
 
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
 
Option_t Option_t TPoint TPoint const char mode
 
R__EXTERN TVirtualMutex * gInterpreterMutex
 
#define R__LOCKGUARD_CLING(mutex)
 
R__EXTERN TInterpreter * gCling
 
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
 
const_iterator begin() const
 
const_iterator end() const
 
void * ExecDefaultConstructor(const TClingClassInfo *info, ROOT::TMetaUtils::EIOCtorCategory kind, const std::string &type_name, void *address=nullptr, unsigned long nary=0UL)
 
void ExecWithReturn(void *address, void *ret=nullptr)
 
void exec_with_valref_return(void *address, cling::Value &ret)
 
std::unique_ptr< TClingMethodInfo > fMethod
Current method, we own.
 
void collect_type_info(clang::QualType &QT, std::ostringstream &typedefbuf, std::ostringstream &callbuf, std::string &type_name, EReferenceType &refType, bool &isPointer, int indent_level, bool forArgument)
 
void SetArgs(const char *args)
 
size_t fMinRequiredArguments
Number of required arguments.
 
size_t CalculateMinRequiredArguments()
 
double ExecDouble(void *address)
 
void SetArgArray(Longptr_t *argArr, int narg)
 
tcling_callfunc_Wrapper_t make_wrapper()
 
void ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
 
Longptr_t ExecInt(void *address)
 
const clang::DeclContext * GetDeclContext() const
 
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
 
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, Longptr_t *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
 
TInterpreter::CallFuncIFacePtr_t IFacePtr()
 
void exec(void *address, void *ret)
 
std::atomic< tcling_callfunc_Wrapper_t > fWrapper
Pointer to compiled wrapper, we do not own.
 
void make_narg_ctor(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
 
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, Longptr_t *poffset)
 
const clang::FunctionDecl * GetDecl()
 
void EvaluateArgList(const std::string &ArgList)
 
const clang::Decl * GetFunctionOrShadowDecl() const
 
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
 
void Exec(void *address, TInterpreterValue *interpVal=0)
 
TClingMethodInfo * FactoryMethod() const
 
int get_wrapper_code(std::string &wrapper_name, std::string &wrapper)
 
size_t GetMinRequiredArguments()
 
long long ExecInt64(void *address)
 
cling::Interpreter * fInterp
Cling interpreter, we do not own.
 
void make_narg_call(const std::string &return_type, const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
 
const clang::FunctionDecl * fDecl
Decl for the method.
 
void make_narg_call_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
 
llvm::SmallVector< cling::Value, 8 > fArgVals
Stored function arguments, we own.
 
void make_narg_ctor_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
 
Emulation of the CINT ClassInfo class.
 
Emulation of the CINT MethodInfo class.
 
This class defines an interface to the cling C++ interpreter.