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"
77using std::string, std::map, std::ostringstream, std::make_pair;
102 clang::Expr::EvalResult
evalRes;
103 if (E->EvaluateAsInt(
evalRes, C, Expr::SE_NoSideEffects)) {
108 V = cling::Value(C.IntTy,
interp);
112 V.setLongLong(res.getSExtValue());
114 V.setULongLong(res.getZExtValue());
120 Policy.SuppressTagKeyword =
true;
121 Policy.SuppressUnwrittenScope =
false;
122 Policy.SuppressInitializers =
false;
123 Policy.AnonymousTagLocations =
false;
125 raw_string_ostream out(buf);
126 E->printPretty(out,
nullptr, Policy, 0);
136 return GetDecl()->getMinRequiredArguments();
153 cling::utils::Transform::Config Config;
154 QT = cling::utils::Transform::GetPartiallyDesugaredType(C,
QT, Config,
true);
174 if (
QT->isFunctionPointerType()) {
189 }
else if (
QT->isMemberPointerType()) {
204 }
else if (
QT->isPointerType()) {
207 }
else if (
QT->isReferenceType()) {
213 if (
QT->isArrayType()) {
241 RD =
RD->getDefinition();
242 assert(
RD &&
"expecting a definition");
244 if (
RD->hasSimpleCopyConstructor())
247 for (
auto *
Ctor :
RD->ctors()) {
248 if (
Ctor->isCopyConstructor()) {
249 return Ctor->isDeleted();
253 assert(0 &&
"did not find a copy constructor?");
272 cling::Interpreter::PushTransactionRAII
RAII(
fInterp);
274 for (
unsigned i = 0U; i <
N; ++i) {
276 QualType
Ty =
PVD->getType();
277 QualType
QT =
Ty.getCanonicalType();
346 for (
unsigned i = 0U; i <
N; ++i) {
359 QualType
Ty =
PVD->getType();
360 QualType
QT =
Ty.getCanonicalType();
366 if (
FD->isVariadic())
390 llvm::raw_string_ostream stream(
name);
391 FD->getNameForDiagnostic(stream,
FD->getASTContext().getPrintingPolicy(),
false);
400 cling::Interpreter::PushTransactionRAII
RAII(
fInterp);
402 for (
unsigned i = 0U; i <
N; ++i) {
404 QualType
Ty =
PVD->getType();
405 QualType
QT =
Ty.getCanonicalType();
462 buf <<
"if (ret) {\n";
530 return fMethod->GetDecl()->getDeclContext();
536 assert(
FD &&
"generate_wrapper called without a function decl!");
546 QualType
QT(
TD->getTypeForDecl(), 0);
551 ND->getNameForDiagnostic(stream, Policy,
true);
561 FunctionDecl::TemplatedKind
TK =
FD->getTemplatedKind();
563 case FunctionDecl::TK_NonTemplate: {
573 case FunctionDecl::TK_FunctionTemplate: {
576 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
579 case FunctionDecl::TK_MemberSpecialization: {
586 if (!
FD->isTemplateInstantiation()) {
601 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
602 "instantiation with no pattern!");
605 FunctionDecl::TemplatedKind
PTK =
Pattern->getTemplatedKind();
609 (
PTK == FunctionDecl::TK_NonTemplate) ||
612 ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
618 }
else if (!
Pattern->hasBody()) {
619 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
620 "instantiation with no body!");
623 if (
FD->isImplicitlyInstantiable()) {
627 case FunctionDecl::TK_FunctionTemplateSpecialization: {
632 if (!
FD->isTemplateInstantiation()) {
647 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
648 "instantiation with no pattern!");
651 FunctionDecl::TemplatedKind
PTK =
Pattern->getTemplatedKind();
655 (
PTK == FunctionDecl::TK_NonTemplate) ||
658 ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
666 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
667 "instantiation with no body!");
670 if (
FD->isImplicitlyInstantiable()) {
674 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
682 if (!
FD->isTemplateInstantiation()) {
697 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
698 "instantiation with no pattern!");
701 FunctionDecl::TemplatedKind
PTK =
Pattern->getTemplatedKind();
705 (
PTK == FunctionDecl::TK_NonTemplate) ||
708 ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
716 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
717 "instantiation with no body!");
720 if (
FD->isImplicitlyInstantiable()) {
727 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
750 clang::FunctionDecl *
FDmod =
const_cast<clang::FunctionDecl *
>(
FD);
751 clang::Sema &S =
fInterp->getSema();
753 cling::Interpreter::PushTransactionRAII
RAII(
fInterp);
758 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
763 FunctionDecl::TemplatedKind
TK =
Definition->getTemplatedKind();
765 case FunctionDecl::TK_NonTemplate: {
768 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
770 }
else if (
Definition->isLateTemplateParsed()) {
771 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
782 case FunctionDecl::TK_FunctionTemplate: {
785 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
788 case FunctionDecl::TK_MemberSpecialization: {
793 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
794 "of a specialization!");
796 }
else if (
Definition->isLateTemplateParsed()) {
797 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
798 "member function of a specialization!");
808 case FunctionDecl::TK_FunctionTemplateSpecialization: {
814 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
815 "template specialization!");
817 }
else if (
Definition->isLateTemplateParsed()) {
818 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
819 "function template specialization!");
829 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
838 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
839 "template specialization!");
841 }
else if (
Definition->isLateTemplateParsed()) {
842 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
843 "dependent function template specialization!");
856 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
882 buf <<
"#pragma clang diagnostic push\n"
883 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
884 "__attribute__((used)) "
885 "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
886 "extern \"C\" void ";
888 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
901 buf <<
"if (nargs == " <<
N <<
") {\n";
913 "#pragma clang diagnostic pop";
934 if ((
N == 0 &&
SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
947 QualType
QT =
FD->getReturnType().getCanonicalType();
948 if (
QT->isVoidType()) {
970 buf <<
"if (ret) {\n";
1065 ::Error(
"TClingCallFunc::make_wrapper",
1066 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1145 Policy.SuppressTagKeyword =
true;
1146 Policy.SuppressUnwrittenScope =
true;
1153 QualType
QT(
TD->getTypeForDecl(), 0);
1158 ND->getNameForDiagnostic(stream, Policy,
true);
1189 buf <<
"__attribute__((used)) ";
1190 buf <<
"extern \"C\" void ";
1192 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1204 buf <<
"if (!arena) {\n";
1206 buf <<
"if (!nary) {\n";
1215 buf <<
"*ret = new " <<
class_name <<
"[nary];\n";
1217 buf <<
"char *buf = (char *) malloc(nary * sizeof(" <<
class_name <<
"));\n";
1219 buf <<
"for (int k=0;k<nary;++k)\n";
1223 buf <<
"*ret = buf;\n";
1240 buf <<
"if (!nary) {\n";
1249 buf <<
"*ret = new (arena) " <<
class_name <<
"[nary];\n";
1251 buf <<
"for (int k=0;k<nary;++k)\n";
1255 buf <<
"*ret = arena;\n";
1275 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1276 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1314 Policy.SuppressTagKeyword =
true;
1315 Policy.SuppressUnwrittenScope =
true;
1322 QualType
QT(
TD->getTypeForDecl(), 0);
1327 ND->getNameForDiagnostic(stream, Policy,
true);
1349 buf <<
"__attribute__((used)) ";
1350 buf <<
"extern \"C\" void ";
1352 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1364 buf <<
"if (withFree) {\n";
1367 buf <<
"if (!nary) {\n";
1370 buf <<
"delete (" <<
class_name <<
"*) obj;\n";
1378 buf <<
"delete[] (" <<
class_name <<
"*) obj;\n";
1401 buf <<
"if (!nary) {\n";
1404 buf <<
"((Nm*)obj)->~Nm();\n";
1415 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1418 buf <<
"} while (nary);\n";
1439 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1440 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1457 ::Error(
"TClingCallFunc::exec",
1458 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1463 ::Error(
"TClingCallFunc::exec",
1464 "Too many arguments provided for %s (%d instead of the minimum %d)",
1471 ::Error(
"TClingCallFunc::exec",
1472 "The method %s is called without an object.",
1478 for (
unsigned i = 0; i <
num_args; ++i) {
1490 QT =
FD->getParamDecl(i)->getType();
1491 QT =
QT.getCanonicalType();
1492 if (
QT->isReferenceType() ||
QT->isRecordType()) {
1501 if (
QT->isBuiltinType() && !C.hasSameType(
QT,
fArgVals[i].getType())) {
1509#define X(type, name) \
1510 case BuiltinType::name: fArgVals[i] = cling::Value::Create(*fInterp, fArgVals[i].castAs<type>()); break;
1527 if (llvm::isa<CXXConstructorDecl>(
FD)) {
1531 QualType
ClassTy(
TD->getTypeForDecl(), 0);
1532 QT = Context.getLValueReferenceType(
ClassTy);
1535 QT =
FD->getReturnType().getCanonicalType();
1538 if (
QT->isRecordType() ||
QT->isMemberDataPointerType())
1539 return exec(address,
ret.getPtr());
1541 exec(address,
ret.getPtrAddress());
1550 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1551 : cling::LookupHelper::NoDiagnostics);
1556 if (!val.isValid()) {
1558 ::Error(
"TClingCallFunc::EvaluateArgList",
1559 "Bad expression in parameter %d of '%s'!",
1572 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
1573 "Called with no wrapper, not implemented!");
1577 exec(address,
nullptr);
1580 cling::Value *val =
reinterpret_cast<cling::Value *
>(
interpVal->GetValAddr());
1584template <
typename T>
1589 ::Error(
"TClingCallFunc::ExecT",
1590 "Called with no wrapper, not implemented!");
1599 if (
ret.needsManagedAllocation())
1602 return ret.castAs<T>();
1625 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
1626 "Called with no wrapper, not implemented!");
1629 (*fWrapper)(address,
nargs,
const_cast<void **
>(args),
ret);
1636 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
1637 "Called with no wrapper, not implemented!");
1646 void *address ,
unsigned long nary )
1648 if (!
info->IsValid()) {
1649 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
1655 auto D =
info->GetDecl();
1672 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
1673 "Called with no wrapper, not implemented!");
1676 void *obj =
nullptr;
1677 (*wrapper)(&obj, address,
nary);
1684 if (!
info->IsValid()) {
1685 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
1701 ::Error(
"TClingCallFunc::ExecDestructor",
1702 "Called with no wrapper, not implemented!");
1768 ::Error(
"TClingCallFunc::IFacePtr(kind)",
1769 "Attempt to get interface while invalid.");
1798 for (
int i = 0; i <
nparam; ++i) {
1823 if (!
info->IsValid()) {
1824 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
1868 if (!
info->IsValid()) {
1869 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
1888 const llvm::SmallVectorImpl<clang::QualType> &
proto,
1897 if (!
info->IsValid()) {
1898 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
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 map< const Decl *, void * > gCtorWrapperStore
static const string kIndentString(" ")
static void indent(ostringstream &buf, int indent_level)
static map< const Decl *, void * > gDtorWrapperStore
static map< const Decl *, void * > gWrapperStore
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
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
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()
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
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()
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *, ROOT::TMetaUtils::EIOCtorCategory, const std::string &)
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...