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;
98 clang::Expr::EvalResult
evalRes;
99 if (E->EvaluateAsInt(
evalRes, C, Expr::SE_NoSideEffects)) {
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::Sema::CXXDefaultConstructor) ||
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 ====",
1144 auto D =
info->GetDecl();
1166 buf <<
"__attribute__((used)) ";
1167 buf <<
"extern \"C\" void ";
1169 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1181 buf <<
"if (!arena) {\n";
1183 buf <<
"if (!nary) {\n";
1192 buf <<
"*ret = new " <<
class_name <<
"[nary];\n";
1194 buf <<
"char *buf = (char *) malloc(nary * sizeof(" <<
class_name <<
"));\n";
1196 buf <<
"for (int k=0;k<nary;++k)\n";
1200 buf <<
"*ret = buf;\n";
1217 buf <<
"if (!nary) {\n";
1226 buf <<
"*ret = new (arena) " <<
class_name <<
"[nary];\n";
1228 buf <<
"for (int k=0;k<nary;++k)\n";
1232 buf <<
"*ret = arena;\n";
1252 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1253 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1309 buf <<
"__attribute__((used)) ";
1310 buf <<
"extern \"C\" void ";
1312 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1324 buf <<
"if (withFree) {\n";
1327 buf <<
"if (!nary) {\n";
1330 buf <<
"delete (" <<
class_name <<
"*) obj;\n";
1338 buf <<
"delete[] (" <<
class_name <<
"*) obj;\n";
1361 buf <<
"if (!nary) {\n";
1364 buf <<
"((Nm*)obj)->~Nm();\n";
1375 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1378 buf <<
"} while (nary);\n";
1399 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1400 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1417 ::Error(
"TClingCallFunc::exec",
1418 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1423 ::Error(
"TClingCallFunc::exec",
1424 "Too many arguments provided for %s (%d instead of the minimum %d)",
1431 ::Error(
"TClingCallFunc::exec",
1432 "The method %s is called without an object.",
1438 for (
unsigned i = 0; i <
num_args; ++i) {
1450 QT =
FD->getParamDecl(i)->getType();
1451 QT =
QT.getCanonicalType();
1452 if (
QT->isReferenceType() ||
QT->isRecordType()) {
1461 if (
QT->isBuiltinType() && !C.hasSameType(
QT,
fArgVals[i].getType())) {
1469#define X(type, name) \
1470 case BuiltinType::name: fArgVals[i] = cling::Value::Create(*fInterp, fArgVals[i].castAs<type>()); break;
1487 if (llvm::isa<CXXConstructorDecl>(
FD)) {
1491 QualType
ClassTy(
TD->getTypeForDecl(), 0);
1492 QT = Context.getLValueReferenceType(
ClassTy);
1495 QT =
FD->getReturnType().getCanonicalType();
1498 if (
QT->isRecordType() ||
QT->isMemberDataPointerType())
1499 return exec(address,
ret.getPtr());
1501 exec(address,
ret.getPtrAddress());
1510 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1511 : cling::LookupHelper::NoDiagnostics);
1516 if (!val.isValid()) {
1518 ::Error(
"TClingCallFunc::EvaluateArgList",
1519 "Bad expression in parameter %d of '%s'!",
1532 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
1533 "Called with no wrapper, not implemented!");
1537 exec(address,
nullptr);
1540 cling::Value *val =
reinterpret_cast<cling::Value *
>(
interpVal->GetValAddr());
1544template <
typename T>
1549 ::Error(
"TClingCallFunc::ExecT",
1550 "Called with no wrapper, not implemented!");
1559 if (
ret.needsManagedAllocation())
1562 return ret.castAs<T>();
1585 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
1586 "Called with no wrapper, not implemented!");
1589 (*fWrapper)(address,
nargs,
const_cast<void **
>(args),
ret);
1596 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
1597 "Called with no wrapper, not implemented!");
1606 void *address ,
unsigned long nary )
1608 if (!
info->IsValid()) {
1609 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
1621 void *obj =
nullptr;
1622 (*wrapper)(&obj, address,
nary);
1625 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Called with no wrapper, not implemented!");
1632 if (!
info->IsValid()) {
1633 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
1642 ::Error(
"TClingCallFunc::ExecDestructor",
"Called with no wrapper, not implemented!");
1695 ::Error(
"TClingCallFunc::IFacePtr(kind)",
1696 "Attempt to get interface while invalid.");
1715 for (
int i = 0; i <
nparam; ++i) {
1740 if (!
info->IsValid()) {
1741 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
1785 if (!
info->IsValid()) {
1786 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
1805 const llvm::SmallVectorImpl<clang::QualType> &
proto,
1814 if (!
info->IsValid()) {
1815 ::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 std::string PrepareStructorWrapper(const Decl *D, const char *wrapper_prefix, std::string &class_name)
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_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...