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.SuppressTagKeywordInAnonNames =
true;
118 Policy.SuppressUnwrittenScope =
false;
119 Policy.SuppressInitializers =
false;
120 Policy.AnonymousTagLocations =
false;
122 raw_string_ostream out(buf);
123 E->printPretty(out,
nullptr, Policy, 0);
133 return GetDecl()->getMinRequiredArguments();
150 cling::utils::Transform::Config Config;
151 QT = cling::utils::Transform::GetPartiallyDesugaredType(C,
QT, Config,
true);
160 Policy.SuppressTagKeyword =
true;
161 Policy.SuppressTagKeywordInAnonNames =
true;
162 Policy.SuppressUnwrittenScope =
true;
165 QualType
QT =
TD->getASTContext().getTypeDeclType(
TD);
169 raw_string_ostream stream(
name);
170 ND->getNameForDiagnostic(stream, Policy,
true);
191 if (
QT->isFunctionPointerType()) {
204 }
else if (
QT->isMemberPointerType()) {
217 }
else if (
QT->isPointerType()) {
220 }
else if (
QT->isReferenceType()) {
226 if (
QT->isArrayType()) {
252 RD =
RD->getDefinition();
253 assert(
RD &&
"expecting a definition");
255 if (
RD->hasSimpleCopyConstructor())
258 for (
auto *
Ctor :
RD->ctors()) {
259 if (
Ctor->isCopyConstructor()) {
260 return Ctor->isDeleted();
264 assert(0 &&
"did not find a copy constructor?");
283 cling::Interpreter::PushTransactionRAII
RAII(
fInterp);
285 for (
unsigned i = 0U; i <
N; ++i) {
287 QualType
Ty =
PVD->getType();
288 QualType
QT =
Ty.getCanonicalType();
355 for (
unsigned i = 0U; i <
N; ++i) {
366 QualType
Ty =
PVD->getType();
367 QualType
QT =
Ty.getCanonicalType();
373 if (
FD->isVariadic())
397 llvm::raw_string_ostream stream(
name);
398 FD->getNameForDiagnostic(stream,
FD->getASTContext().getPrintingPolicy(),
false);
407 cling::Interpreter::PushTransactionRAII
RAII(
fInterp);
409 for (
unsigned i = 0U; i <
N; ++i) {
411 QualType
Ty =
PVD->getType();
412 QualType
QT =
Ty.getCanonicalType();
465 buf <<
"if (ret) {\n";
529 return fMethod->GetDecl()->getDeclContext();
535 assert(
FD &&
"generate_wrapper called without a function decl!");
550 FunctionDecl::TemplatedKind
TK =
FD->getTemplatedKind();
552 case FunctionDecl::TK_NonTemplate: {
562 case FunctionDecl::TK_FunctionTemplate: {
565 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
568 case FunctionDecl::TK_MemberSpecialization: {
575 if (!
FD->isTemplateInstantiation()) {
590 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
591 "instantiation with no pattern!");
594 FunctionDecl::TemplatedKind
PTK =
Pattern->getTemplatedKind();
598 (
PTK == FunctionDecl::TK_NonTemplate) ||
601 ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
607 }
else if (!
Pattern->hasBody()) {
608 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
609 "instantiation with no body!");
612 if (
FD->isImplicitlyInstantiable()) {
616 case FunctionDecl::TK_FunctionTemplateSpecialization: {
621 if (!
FD->isTemplateInstantiation()) {
636 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
637 "instantiation with no pattern!");
640 FunctionDecl::TemplatedKind
PTK =
Pattern->getTemplatedKind();
644 (
PTK == FunctionDecl::TK_NonTemplate) ||
647 ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
655 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
656 "instantiation with no body!");
659 if (
FD->isImplicitlyInstantiable()) {
663 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
671 if (!
FD->isTemplateInstantiation()) {
686 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
687 "instantiation with no pattern!");
690 FunctionDecl::TemplatedKind
PTK =
Pattern->getTemplatedKind();
694 (
PTK == FunctionDecl::TK_NonTemplate) ||
697 ((
PTK != FunctionDecl::TK_FunctionTemplate) &&
705 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
706 "instantiation with no body!");
709 if (
FD->isImplicitlyInstantiable()) {
716 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
739 clang::FunctionDecl *
FDmod =
const_cast<clang::FunctionDecl *
>(
FD);
740 clang::Sema &S =
fInterp->getSema();
742 cling::Interpreter::PushTransactionRAII
RAII(
fInterp);
747 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
752 FunctionDecl::TemplatedKind
TK =
Definition->getTemplatedKind();
754 case FunctionDecl::TK_NonTemplate: {
757 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
759 }
else if (
Definition->isLateTemplateParsed()) {
760 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
771 case FunctionDecl::TK_FunctionTemplate: {
774 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
777 case FunctionDecl::TK_MemberSpecialization: {
782 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
783 "of a specialization!");
785 }
else if (
Definition->isLateTemplateParsed()) {
786 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
787 "member function of a specialization!");
797 case FunctionDecl::TK_FunctionTemplateSpecialization: {
803 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
804 "template specialization!");
806 }
else if (
Definition->isLateTemplateParsed()) {
807 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
808 "function template specialization!");
818 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
827 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
828 "template specialization!");
830 }
else if (
Definition->isLateTemplateParsed()) {
831 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
832 "dependent function template specialization!");
845 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
871 buf <<
"#pragma clang diagnostic push\n"
872 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
873 "__attribute__((used)) "
874 "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
875 "extern \"C\" void ";
877 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
888 buf <<
"if (nargs == " <<
N <<
") {\n";
898 "#pragma clang diagnostic pop";
919 if ((
N == 0 &&
SpecMemKind == clang::CXXSpecialMemberKind::DefaultConstructor) ||
921 (
SpecMemKind == clang::CXXSpecialMemberKind::CopyConstructor ||
SpecMemKind == clang::CXXSpecialMemberKind::MoveConstructor))) {
932 QualType
QT =
FD->getReturnType().getCanonicalType();
933 if (
QT->isVoidType()) {
949 buf <<
"if (ret) {\n";
1037 ::Error(
"TClingCallFunc::make_wrapper",
1038 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1054 ::Error(
"TClingCallFunc::exec",
1055 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1060 ::Error(
"TClingCallFunc::exec",
1061 "Too many arguments provided for %s (%d instead of the minimum %d)",
1068 ::Error(
"TClingCallFunc::exec",
1069 "The method %s is called without an object.",
1075 for (
unsigned i = 0; i <
num_args; ++i) {
1087 QT =
FD->getParamDecl(i)->getType();
1088 QT =
QT.getCanonicalType();
1089 if (
QT->isReferenceType() ||
QT->isRecordType()) {
1098 if (
QT->isBuiltinType() && !C.hasSameType(
QT,
fArgVals[i].getType())) {
1106#define X(type, name) \
1107 case BuiltinType::name: fArgVals[i] = cling::Value::Create(*fInterp, fArgVals[i].castAs<type>()); break;
1124 if (llvm::isa<CXXConstructorDecl>(
FD)) {
1128 QualType
ClassTy =
TD->getASTContext().getTypeDeclType(
TD);
1129 QT = Context.getLValueReferenceType(
ClassTy);
1132 QT =
FD->getReturnType().getCanonicalType();
1135 if (
QT->isRecordType() ||
QT->isMemberDataPointerType())
1136 return exec(address,
ret.getPtr());
1138 exec(address,
ret.getPtrAddress());
1147 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1148 : cling::LookupHelper::NoDiagnostics);
1153 if (!val.isValid()) {
1155 ::Error(
"TClingCallFunc::EvaluateArgList",
1156 "Bad expression in parameter %d of '%s'!",
1169 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
1170 "Called with no wrapper, not implemented!");
1174 exec(address,
nullptr);
1177 cling::Value *val =
reinterpret_cast<cling::Value *
>(
interpVal->GetValAddr());
1181template <
typename T>
1186 ::Error(
"TClingCallFunc::ExecT",
1187 "Called with no wrapper, not implemented!");
1196 if (
ret.needsManagedAllocation())
1199 return ret.castAs<T>();
1222 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
1223 "Called with no wrapper, not implemented!");
1226 (*fWrapper)(address,
nargs,
const_cast<void **
>(args),
ret);
1233 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
1234 "Called with no wrapper, not implemented!");
1243 void *address ,
unsigned long nary )
1245 if (!
info->IsValid()) {
1246 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
1255 clang::Decl *D =
const_cast<clang::Decl *
>(
info->GetDecl());
1257 if (Cpp::IsClass(D) || Cpp::IsConstructor(D)) {
1259 return Cpp::Construct(D, address,
nary).data;
1262 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"ClassInfo missing a valid Scope/Constructor");
1269 if (!
info->IsValid()) {
1270 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
1276 if (Cpp::Destruct(address,
const_cast<clang::Decl *
>(
info->GetDecl()),
withFree,
nary))
1279 ::Error(
"TClingCallFunc::ExecDestructor",
"Called with no wrapper, not implemented!");
1332 ::Error(
"TClingCallFunc::IFacePtr(kind)",
1333 "Attempt to get interface while invalid.");
1352 for (
int i = 0; i <
nparam; ++i) {
1377 if (!
info->IsValid()) {
1378 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
1422 if (!
info->IsValid()) {
1423 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
1442 const llvm::SmallVectorImpl<clang::QualType> &
proto,
1451 if (!
info->IsValid()) {
1452 ::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.