30#include "TClingUtils.h"
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/RecordLayout.h"
49#include "clang/AST/Type.h"
50#include "clang/Frontend/CompilerInstance.h"
51#include "clang/Lex/Preprocessor.h"
52#include "clang/Sema/Sema.h"
53#include "clang/Sema/Lookup.h"
55#include "llvm/ADT/APInt.h"
56#include "llvm/ExecutionEngine/ExecutionEngine.h"
57#include "llvm/ExecutionEngine/GenericValue.h"
58#include "llvm/Support/Casting.h"
59#include "llvm/Support/raw_ostream.h"
60#include "llvm/IR/LLVMContext.h"
61#include "llvm/IR/DerivedTypes.h"
62#include "llvm/IR/Function.h"
63#include "llvm/IR/GlobalValue.h"
64#include "llvm/IR/Module.h"
65#include "llvm/IR/Type.h"
67#include "clang/Sema/SemaInternal.h"
89indent(ostringstream &buf,
int indent_level)
91 for (
int i = 0; i < indent_level; ++i) {
101 ASTContext &
C = interp.getCI()->getASTContext();
103 if (
E->EvaluateAsInt(res,
C, Expr::SE_NoSideEffects)) {
109 V.getLL() = res.getSExtValue();
111 V.getULL() = res.getZExtValue();
116 PrintingPolicy Policy(
C.getPrintingPolicy());
117 Policy.SuppressTagKeyword =
true;
118 Policy.SuppressUnwrittenScope =
false;
119 Policy.SuppressInitializers =
false;
120 Policy.AnonymousTagLocations =
false;
122 raw_string_ostream out(buf);
123 E->printPretty(out, 0, Policy, 0);
127 interp.evaluate(buf, V);
131 template <
typename returnType>
134 QualType QT = val.getType().getCanonicalType();
135 if (
const BuiltinType *BT =
136 dyn_cast<BuiltinType>(&*QT)) {
144 switch (BT->getKind()) {
145 case BuiltinType::Void:
147 return (returnType) 0;
153 case BuiltinType::Char_U:
155 return (returnType) val.getULL();
158 case BuiltinType::WChar_U:
162 return (returnType)(wchar_t) val.getULL();
165 case BuiltinType::Char16:
166 case BuiltinType::Char32:
170 case BuiltinType::ULongLong:
171 return (returnType) val.getULL();
174 case BuiltinType::UInt128:
181 case BuiltinType::Char_S:
182 case BuiltinType::SChar:
183 return (returnType) val.getLL();
186 case BuiltinType::WChar_S:
190 return (returnType)(wchar_t) val.getLL();
196 case BuiltinType::LongLong:
197 return (returnType) val.getLL();
200 case BuiltinType::Int128:
203 case BuiltinType::Half:
208 return (returnType) val.getFloat();
211 return (returnType) val.getDouble();
213 case BuiltinType::LongDouble:
214 return (returnType) val.getLongDouble();
217 case BuiltinType::NullPtr:
218 return (returnType) 0;
225 if (QT->isPointerType() || QT->isArrayType() || QT->isRecordType() ||
226 QT->isReferenceType()) {
227 return (returnType)(long) val.getPtr();
229 if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
230 if (ET->getDecl()->getIntegerType()->hasSignedIntegerRepresentation())
231 return (returnType) val.getLL();
233 return (returnType) val.getULL();
235 if (QT->isMemberPointerType()) {
236 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
237 if (MPT->isMemberDataPointer()) {
238 return (returnType)(ptrdiff_t)val.getPtr();
240 return (returnType)(long) val.getPtr();
242 ::Error(
"TClingCallFunc::sv_to",
"Invalid Type!");
250 return sv_to<long long>(val);
253 unsigned long long sv_to_ulong_long(
const cling::Value &val)
255 return sv_to<unsigned long long>(val);
263 return GetDecl()->getMinRequiredArguments();
267 bool withAccessControl)
269 return fInterp->compileFunction(wrapper_name, wrapper,
false ,
274 string &type_name,
EReferenceType &refType,
bool &isPointer,
int indent_level,
281 const FunctionDecl *FD =
GetDecl();
282 PrintingPolicy Policy(FD->getASTContext().getPrintingPolicy());
284 if (QT->isRecordType() && forArgument) {
288 if (QT->isFunctionPointerType()) {
289 string fp_typedef_name;
293 type_name =
nm.str();
294 raw_string_ostream OS(fp_typedef_name);
295 QT.print(OS, Policy, type_name);
298 for (
int i = 0; i < indent_level; ++i) {
301 typedefbuf <<
"typedef " << fp_typedef_name <<
";\n";
303 }
else if (QT->isMemberPointerType()) {
304 string mp_typedef_name;
308 type_name =
nm.str();
309 raw_string_ostream OS(mp_typedef_name);
310 QT.print(OS, Policy, type_name);
313 for (
int i = 0; i < indent_level; ++i) {
316 typedefbuf <<
"typedef " << mp_typedef_name <<
";\n";
318 }
else if (QT->isPointerType()) {
320 QT = cast<clang::PointerType>(QT)->getPointeeType();
321 }
else if (QT->isReferenceType()) {
324 QT = cast<ReferenceType>(QT)->getPointeeType();
327 if (QT->isArrayType()) {
328 string ar_typedef_name;
332 type_name = ar.str();
333 raw_string_ostream OS(ar_typedef_name);
334 QT.print(OS, Policy, type_name);
337 for (
int i = 0; i < indent_level; ++i) {
340 typedefbuf <<
"typedef " << ar_typedef_name <<
";\n";
347 ostringstream &callbuf,
const string &class_name,
354 const FunctionDecl *FD =
GetDecl();
356 callbuf <<
"new " << class_name <<
"(";
357 for (
unsigned i = 0U; i <
N; ++i) {
358 const ParmVarDecl *PVD = FD->getParamDecl(i);
359 QualType Ty = PVD->getType();
360 QualType QT = Ty.getCanonicalType();
363 bool isPointer =
false;
365 refType, isPointer, indent_level,
true);
372 for (
int j = 0; j <= indent_level; ++j) {
378 callbuf <<
"(" << type_name.c_str() <<
379 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
381 }
else if (isPointer) {
382 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
385 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
392 ostringstream &callbuf,
const string &class_name,
int indent_level)
399 const FunctionDecl *FD =
GetDecl();
412 bool ShouldCastFunction = !isa<CXXMethodDecl>(FD) &&
N == FD->getNumParams();
413 if (ShouldCastFunction) {
416 callbuf << return_type <<
" (&)";
419 for (
unsigned i = 0U; i <
N; ++i) {
426 for (
int j = 0; j <= indent_level; ++j) {
431 const ParmVarDecl *PVD = FD->getParamDecl(i);
432 QualType Ty = PVD->getType();
433 QualType QT = Ty.getCanonicalType();
434 std::string arg_type;
438 if (FD->isVariadic())
446 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
449 callbuf <<
"((const " << class_name <<
"*)obj)->";
451 callbuf <<
"((" << class_name <<
"*)obj)->";
452 }
else if (
const NamedDecl *ND =
453 dyn_cast<NamedDecl>(FD->getDeclContext())) {
456 callbuf << class_name <<
"::";
462 llvm::raw_string_ostream stream(
name);
463 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
467 if (ShouldCastFunction) callbuf <<
")";
470 for (
unsigned i = 0U; i <
N; ++i) {
471 const ParmVarDecl *PVD = FD->getParamDecl(i);
472 QualType Ty = PVD->getType();
473 QualType QT = Ty.getCanonicalType();
476 bool isPointer =
false;
477 collect_type_info(QT, typedefbuf, callbuf, type_name, refType, isPointer, indent_level,
true);
485 for (
int j = 0; j <= indent_level; ++j) {
492 callbuf <<
"(" << type_name.c_str() <<
493 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
495 }
else if (isPointer) {
496 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
501 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
508 ostringstream &buf,
int indent_level)
519 for (
int i = 0; i < indent_level; ++i) {
522 buf <<
"if (ret) {\n";
525 ostringstream typedefbuf;
526 ostringstream callbuf;
530 for (
int i = 0; i < indent_level; ++i) {
533 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
542 for (
int i = 0; i < indent_level; ++i) {
545 callbuf <<
"return;\n";
549 buf << typedefbuf.str() << callbuf.str();
552 for (
int i = 0; i < indent_level; ++i) {
556 for (
int i = 0; i < indent_level; ++i) {
562 ostringstream typedefbuf;
563 ostringstream callbuf;
564 for (
int i = 0; i < indent_level; ++i) {
569 for (
int i = 0; i < indent_level; ++i) {
572 callbuf <<
"return;\n";
573 buf << typedefbuf.str() << callbuf.str();
576 for (
int i = 0; i < indent_level; ++i) {
584 const FunctionDecl *FD =
GetDecl();
585 assert(FD &&
"generate_wrapper called without a function decl!");
586 ASTContext &Context = FD->getASTContext();
587 PrintingPolicy Policy(Context.getPrintingPolicy());
592 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(FD->getDeclContext())) {
594 QualType QT(TD->getTypeForDecl(), 0);
596 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(FD->getDeclContext())) {
598 raw_string_ostream stream(class_name);
599 ND->getNameForDiagnostic(stream, Policy,
true);
606 bool needInstantiation =
false;
607 const FunctionDecl *Definition = 0;
608 if (!FD->isDefined(Definition)) {
609 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
611 case FunctionDecl::TK_NonTemplate: {
621 case FunctionDecl::TK_FunctionTemplate: {
624 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
627 case FunctionDecl::TK_MemberSpecialization: {
634 if (!FD->isTemplateInstantiation()) {
647 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
649 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
650 "instantiation with no pattern!");
653 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
654 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
657 (PTK == FunctionDecl::TK_NonTemplate) ||
660 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
661 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
666 }
else if (!
Pattern->hasBody()) {
667 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
668 "instantiation with no body!");
671 if (FD->isImplicitlyInstantiable()) {
672 needInstantiation =
true;
675 case FunctionDecl::TK_FunctionTemplateSpecialization: {
680 if (!FD->isTemplateInstantiation()) {
693 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
695 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
696 "instantiation with no pattern!");
699 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
700 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
703 (PTK == FunctionDecl::TK_NonTemplate) ||
706 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
707 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
714 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
715 "instantiation with no body!");
718 if (FD->isImplicitlyInstantiable()) {
719 needInstantiation =
true;
722 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
730 if (!FD->isTemplateInstantiation()) {
743 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
745 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
746 "instantiation with no pattern!");
749 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
750 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
753 (PTK == FunctionDecl::TK_NonTemplate) ||
756 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
757 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
764 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
765 "instantiation with no body!");
768 if (FD->isImplicitlyInstantiable()) {
769 needInstantiation =
true;
775 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
797 if (needInstantiation) {
798 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
799 clang::Sema &
S =
fInterp->getSema();
801 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
802 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
805 if (!FD->isDefined(Definition)) {
806 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
811 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
813 case FunctionDecl::TK_NonTemplate: {
815 if (Definition->isDeleted()) {
816 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
818 }
else if (Definition->isLateTemplateParsed()) {
819 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
830 case FunctionDecl::TK_FunctionTemplate: {
833 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
836 case FunctionDecl::TK_MemberSpecialization: {
840 if (Definition->isDeleted()) {
841 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
842 "of a specialization!");
844 }
else if (Definition->isLateTemplateParsed()) {
845 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
846 "member function of a specialization!");
856 case FunctionDecl::TK_FunctionTemplateSpecialization: {
861 if (Definition->isDeleted()) {
862 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
863 "template specialization!");
865 }
else if (Definition->isLateTemplateParsed()) {
866 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
867 "function template specialization!");
877 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
885 if (Definition->isDeleted()) {
886 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
887 "template specialization!");
889 }
else if (Definition->isLateTemplateParsed()) {
890 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
891 "dependent function template specialization!");
904 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
910 unsigned num_params = FD->getNumParams();
922 wrapper_name = buf.str();
928 int indent_level = 0;
930 buf <<
"#pragma clang diagnostic push\n"
931 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
932 "__attribute__((used)) "
933 "extern \"C\" void ";
935 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
938 if (min_args == num_params) {
944 for (
unsigned N = min_args;
N <= num_params; ++
N) {
945 for (
int i = 0; i < indent_level; ++i) {
948 buf <<
"if (nargs == " <<
N <<
") {\n";
952 for (
int i = 0; i < indent_level; ++i) {
960 "#pragma clang diagnostic pop";
966 ostringstream &buf,
int indent_level)
977 const FunctionDecl *FD =
GetDecl();
978 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
983 QualType QT = FD->getReturnType().getCanonicalType();
984 if (QT->isVoidType()) {
985 ostringstream typedefbuf;
986 ostringstream callbuf;
987 for (
int i = 0; i < indent_level; ++i) {
990 make_narg_call(
"void",
N, typedefbuf, callbuf, class_name, indent_level);
992 for (
int i = 0; i < indent_level; ++i) {
995 callbuf <<
"return;\n";
996 buf << typedefbuf.str() << callbuf.str();
998 for (
int i = 0; i < indent_level; ++i) {
1004 bool isPointer =
false;
1006 buf <<
"if (ret) {\n";
1009 ostringstream typedefbuf;
1010 ostringstream callbuf;
1014 for (
int i = 0; i < indent_level; ++i) {
1017 callbuf <<
"new (ret) ";
1019 refType, isPointer, indent_level,
false);
1023 callbuf <<
"(" << type_name.c_str();
1027 }
else if (isPointer) {
1036 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1041 for (
int i = 0; i < indent_level; ++i) {
1044 callbuf <<
"return;\n";
1048 buf << typedefbuf.str() << callbuf.str();
1051 for (
int i = 0; i < indent_level; ++i) {
1055 for (
int i = 0; i < indent_level; ++i) {
1061 ostringstream typedefbuf;
1062 ostringstream callbuf;
1063 for (
int i = 0; i < indent_level; ++i) {
1066 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1068 for (
int i = 0; i < indent_level; ++i) {
1071 callbuf <<
"return;\n";
1072 buf << typedefbuf.str() << callbuf.str();
1075 for (
int i = 0; i < indent_level; ++i) {
1086 const FunctionDecl *FD =
GetDecl();
1087 string wrapper_name;
1100 ::Error(
"TClingCallFunc::make_wrapper",
1101 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1148 ASTContext &Context = info->
GetDecl()->getASTContext();
1149 PrintingPolicy Policy(Context.getPrintingPolicy());
1150 Policy.SuppressTagKeyword =
true;
1151 Policy.SuppressUnwrittenScope =
true;
1156 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1158 QualType QT(TD->getTypeForDecl(), 0);
1160 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1162 raw_string_ostream stream(class_name);
1163 ND->getNameForDiagnostic(stream, Policy,
true);
1169 string wrapper_name;
1178 wrapper_name = buf.str();
1183 int indent_level = 0;
1185 buf <<
"__attribute__((used)) ";
1186 buf <<
"extern \"C\" void ";
1187 buf << wrapper_name;
1188 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1199 indent(buf, indent_level);
1200 buf <<
"if (!arena) {\n";
1202 indent(buf, indent_level);
1203 buf <<
"if (!nary) {\n";
1205 indent(buf, indent_level);
1206 buf <<
"*ret = new " << class_name <<
";\n";
1208 indent(buf, indent_level);
1210 indent(buf, indent_level);
1213 indent(buf, indent_level);
1214 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1216 indent(buf, indent_level);
1219 indent(buf, indent_level);
1229 indent(buf, indent_level);
1232 indent(buf, indent_level);
1233 buf <<
"if (!nary) {\n";
1235 indent(buf, indent_level);
1236 buf <<
"*ret = new (arena) " << class_name <<
";\n";
1238 indent(buf, indent_level);
1240 indent(buf, indent_level);
1243 indent(buf, indent_level);
1244 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1246 indent(buf, indent_level);
1249 indent(buf, indent_level);
1255 string wrapper(buf.str());
1265 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1266 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1302 ASTContext &Context = info->
GetDecl()->getASTContext();
1303 PrintingPolicy Policy(Context.getPrintingPolicy());
1304 Policy.SuppressTagKeyword =
true;
1305 Policy.SuppressUnwrittenScope =
true;
1310 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1312 QualType QT(TD->getTypeForDecl(), 0);
1314 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1316 raw_string_ostream stream(class_name);
1317 ND->getNameForDiagnostic(stream, Policy,
true);
1323 string wrapper_name;
1332 wrapper_name = buf.str();
1337 int indent_level = 0;
1339 buf <<
"__attribute__((used)) ";
1340 buf <<
"extern \"C\" void ";
1341 buf << wrapper_name;
1342 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1353 indent(buf, indent_level);
1354 buf <<
"if (withFree) {\n";
1356 indent(buf, indent_level);
1357 buf <<
"if (!nary) {\n";
1359 indent(buf, indent_level);
1360 buf <<
"delete (" << class_name <<
"*) obj;\n";
1362 indent(buf, indent_level);
1364 indent(buf, indent_level);
1367 indent(buf, indent_level);
1368 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1370 indent(buf, indent_level);
1373 indent(buf, indent_level);
1386 indent(buf, indent_level);
1389 indent(buf, indent_level);
1390 buf <<
"typedef " << class_name <<
" Nm;\n";
1391 buf <<
"if (!nary) {\n";
1393 indent(buf, indent_level);
1394 buf <<
"((Nm*)obj)->~Nm();\n";
1396 indent(buf, indent_level);
1398 indent(buf, indent_level);
1401 indent(buf, indent_level);
1404 indent(buf, indent_level);
1405 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1407 indent(buf, indent_level);
1408 buf <<
"} while (nary);\n";
1410 indent(buf, indent_level);
1413 indent(buf, indent_level);
1419 string wrapper(buf.str());
1429 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1430 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1445 unsigned long long ull;
1467 SmallVector<ValHolder, 8> vh_ary;
1468 SmallVector<void *, 8> vp_ary;
1470 unsigned num_args =
fArgVals.size();
1474 const FunctionDecl *FD =
GetDecl();
1481 unsigned num_params = FD->getNumParams();
1484 ::Error(
"TClingCallFunc::exec",
1485 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1490 if (address == 0 && dyn_cast<CXXMethodDecl>(FD)
1491 && !(dyn_cast<CXXMethodDecl>(FD))->isStatic()
1492 && !dyn_cast<CXXConstructorDecl>(FD)) {
1493 ::Error(
"TClingCallFunc::exec",
1494 "The method %s is called without an object.",
1498 vh_ary.reserve(num_args);
1499 vp_ary.reserve(num_args);
1500 for (
unsigned i = 0U; i < num_args; ++i) {
1502 if (i < num_params) {
1503 const ParmVarDecl *PVD = FD->getParamDecl(i);
1504 Ty = PVD->getType();
1508 QualType QT = Ty.getCanonicalType();
1509 if (
const BuiltinType *BT =
1510 dyn_cast<BuiltinType>(&*QT)) {
1518 switch (BT->getKind()) {
1522 case BuiltinType::Void: {
1524 ::Error(
"TClingCallFunc::exec(void*)",
1525 "Invalid type 'Void'!");
1535 vh.u.b = (bool) sv_to_ulong_long(
fArgVals[i]);
1536 vh_ary.push_back(vh);
1537 vp_ary.push_back(&vh_ary.back());
1540 case BuiltinType::Char_U: {
1543 vh.u.c = (char) sv_to_ulong_long(
fArgVals[i]);
1544 vh_ary.push_back(vh);
1545 vp_ary.push_back(&vh_ary.back());
1551 vh.u.uc = (
unsigned char) sv_to_ulong_long(
fArgVals[i]);
1552 vh_ary.push_back(vh);
1553 vp_ary.push_back(&vh_ary.back());
1556 case BuiltinType::WChar_U: {
1561 vh.u.wc = (wchar_t) sv_to_ulong_long(
fArgVals[i]);
1562 vh_ary.push_back(vh);
1563 vp_ary.push_back(&vh_ary.back());
1566 case BuiltinType::Char16: {
1574 case BuiltinType::Char32: {
1585 vh.u.us = (
unsigned short) sv_to_ulong_long(
fArgVals[i]);
1586 vh_ary.push_back(vh);
1587 vp_ary.push_back(&vh_ary.back());
1593 vh.u.ui = (
unsigned int) sv_to_ulong_long(
fArgVals[i]);
1594 vh_ary.push_back(vh);
1595 vp_ary.push_back(&vh_ary.back());
1601 vh.u.ul = (
unsigned long) sv_to_ulong_long(
fArgVals[i]);
1602 vh_ary.push_back(vh);
1603 vp_ary.push_back(&vh_ary.back());
1606 case BuiltinType::ULongLong: {
1609 vh.u.ull = (
unsigned long long) sv_to_ulong_long(
fArgVals[i]);
1610 vh_ary.push_back(vh);
1611 vp_ary.push_back(&vh_ary.back());
1614 case BuiltinType::UInt128: {
1624 case BuiltinType::Char_S: {
1627 vh.u.c = (char) sv_to_long_long(
fArgVals[i]);
1628 vh_ary.push_back(vh);
1629 vp_ary.push_back(&vh_ary.back());
1632 case BuiltinType::SChar: {
1635 vh.u.sc = (
signed char) sv_to_long_long(
fArgVals[i]);
1636 vh_ary.push_back(vh);
1637 vp_ary.push_back(&vh_ary.back());
1640 case BuiltinType::WChar_S: {
1645 vh.u.wc = (wchar_t) sv_to_long_long(
fArgVals[i]);
1646 vh_ary.push_back(vh);
1647 vp_ary.push_back(&vh_ary.back());
1653 vh.u.s = (short) sv_to_long_long(
fArgVals[i]);
1654 vh_ary.push_back(vh);
1655 vp_ary.push_back(&vh_ary.back());
1661 vh.u.i = (int) sv_to_long_long(
fArgVals[i]);
1662 vh_ary.push_back(vh);
1663 vp_ary.push_back(&vh_ary.back());
1669 vh.u.l = (long) sv_to_long_long(
fArgVals[i]);
1670 vh_ary.push_back(vh);
1671 vp_ary.push_back(&vh_ary.back());
1674 case BuiltinType::LongLong: {
1677 vh.u.ll = (
long long) sv_to_long_long(
fArgVals[i]);
1678 vh_ary.push_back(vh);
1679 vp_ary.push_back(&vh_ary.back());
1682 case BuiltinType::Int128: {
1684 ::Error(
"TClingCallFunc::exec(void*)",
1685 "Invalid type 'Int128'!");
1689 case BuiltinType::Half: {
1691 ::Error(
"TClingCallFunc::exec(void*)",
1692 "Invalid type 'Half'!");
1699 vh.u.flt = sv_to<float>(
fArgVals[i]);
1700 vh_ary.push_back(vh);
1701 vp_ary.push_back(&vh_ary.back());
1707 vh.u.dbl = sv_to<double>(
fArgVals[i]);
1708 vh_ary.push_back(vh);
1709 vp_ary.push_back(&vh_ary.back());
1712 case BuiltinType::LongDouble: {
1715 vh.u.ldbl = sv_to<long double>(
fArgVals[i]);
1716 vh_ary.push_back(vh);
1717 vp_ary.push_back(&vh_ary.back());
1723 case BuiltinType::NullPtr: {
1727 vh_ary.push_back(vh);
1728 vp_ary.push_back(&vh_ary.back());
1734 ::Error(
"TClingCallFunc::exec(void*)",
1735 "Unhandled builtin type!");
1741 }
else if (QT->isReferenceType()) {
1744 vp_ary.push_back((
void *) sv_to_ulong_long(
fArgVals[i]));
1745 }
else if (QT->isPointerType() || QT->isArrayType()) {
1747 vh.u.vp = (
void *) sv_to_ulong_long(
fArgVals[i]);
1748 vh_ary.push_back(vh);
1749 vp_ary.push_back(&vh_ary.back());
1750 }
else if (QT->isRecordType()) {
1753 vp_ary.push_back((
void *) sv_to_ulong_long(
fArgVals[i]));
1754 }
else if (
const EnumType *ET =
1755 dyn_cast<EnumType>(&*QT)) {
1760 vh.u.i = (int) sv_to_long_long(
fArgVals[i]);
1761 vh_ary.push_back(vh);
1762 vp_ary.push_back(&vh_ary.back());
1763 }
else if (QT->isMemberPointerType()) {
1765 vh.u.vp = (
void *) sv_to_ulong_long(
fArgVals[i]);
1766 vh_ary.push_back(vh);
1767 vp_ary.push_back(&vh_ary.back());
1769 ::Error(
"TClingCallFunc::exec(void*)",
1770 "Invalid type (unrecognized)!");
1776 (*fWrapper)(address, (int)num_args, (
void **)vp_ary.data(), ret);
1779template <
typename T>
1783 exec(address, &ret);
1787template <
typename T>
1791 exec(address, &ret);
1792 val->getULL() = ret;
1799 ret = cling::Value::Create<T>(QT.getAsOpaquePtr(), *
fInterp);
1800 static_assert(std::is_integral<T>::value,
"Must be called with integral T");
1801 if (std::is_signed<T>::value)
1802 return [
this](
void* address,
cling::Value& ret) { execWithLL<T>(address, &ret); };
1804 return [
this](
void* address,
cling::Value& ret) { execWithULL<T>(address, &ret); };
1810 switch (BT->getKind()) {
1811 case BuiltinType::Void: {
1812 ret = cling::Value::Create<void>(QT.getAsOpaquePtr(), *
fInterp);
1821 return InitRetAndExecIntegral<bool>(QT, ret);
1823 case BuiltinType::Char_U:
1825 return InitRetAndExecIntegral<char>(QT, ret);
1827 case BuiltinType::WChar_U:
1828 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1830 case BuiltinType::Char16:
1831 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1832 "Invalid type 'char16_t'!");
1835 case BuiltinType::Char32:
1836 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1837 "Invalid type 'char32_t'!");
1841 return InitRetAndExecIntegral<unsigned short>(QT, ret);
1844 return InitRetAndExecIntegral<unsigned int>(QT, ret);
1847 return InitRetAndExecIntegral<unsigned long>(QT, ret);
1849 case BuiltinType::ULongLong:
1850 return InitRetAndExecIntegral<unsigned long long>(QT, ret);
1852 case BuiltinType::UInt128: {
1853 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1854 "Invalid type '__uint128_t'!");
1862 case BuiltinType::Char_S:
1863 case BuiltinType::SChar:
1864 return InitRetAndExecIntegral<signed char>(QT, ret);
1866 case BuiltinType::WChar_S:
1870 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1873 return InitRetAndExecIntegral<short>(QT, ret);
1876 return InitRetAndExecIntegral<int>(QT, ret);
1879 return InitRetAndExecIntegral<long>(QT, ret);
1881 case BuiltinType::LongLong:
1882 return InitRetAndExecIntegral<long long>(QT, ret);
1884 case BuiltinType::Int128:
1885 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1886 "Invalid type '__int128_t'!");
1889 case BuiltinType::Half:
1891 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1892 "Invalid type 'Half'!");
1896 ret = cling::Value::Create<float>(QT.getAsOpaquePtr(), *
fInterp);
1897 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getFloat()); };
1901 ret = cling::Value::Create<double>(QT.getAsOpaquePtr(), *
fInterp);
1902 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getDouble()); };
1905 case BuiltinType::LongDouble: {
1906 ret = cling::Value::Create<long double>(QT.getAsOpaquePtr(), *
fInterp);
1907 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getLongDouble()); };
1913 case BuiltinType::NullPtr:
1915 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1916 "Invalid type 'nullptr'!");
1928 if (QT->isReferenceType()) {
1930 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
1931 }
else if (QT->isMemberPointerType()) {
1932 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
1933 if (MPT->isMemberDataPointer()) {
1940 return [
this](
void* address,
cling::Value& ret) {
exec(address, ret.getPtr()); };
1944 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
1945 }
else if (QT->isPointerType() || QT->isArrayType()) {
1947 ret = cling::Value::Create<void*>(QT.getAsOpaquePtr(), *
fInterp);
1948 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
1949 }
else if (QT->isRecordType()) {
1951 return [
this](
void* address,
cling::Value& ret) {
exec(address, ret.getPtr()); };
1952 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
1957 return [
this](
void* address,
cling::Value& ret) { execWithLL<int>(address, &ret); };
1958 }
else if (
const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
1961 ::Error(
"TClingCallFunc::exec_with_valref_return",
1962 "Unrecognized return type!");
1969 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
1970 ASTContext &Context = FD->getASTContext();
1971 const TypeDecl *TD = dyn_cast<TypeDecl>(CD->getDeclContext());
1972 QualType ClassTy(TD->getTypeForDecl(), 0);
1973 QualType QT = Context.getLValueReferenceType(ClassTy);
1976 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
1978 QualType QT = FD->getReturnType().getCanonicalType();
1998 execFunc(address, *ret);
2006 SmallVector<Expr *, 4> exprs;
2007 fInterp->getLookupHelper().findArgList(ArgList, exprs,
2008 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
2009 : cling::LookupHelper::NoDiagnostics);
2010 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
2011 E = exprs.end();
I !=
E; ++
I) {
2014 if (!val.isValid()) {
2016 ::Error(
"TClingCallFunc::EvaluateArgList",
2017 "Bad expression in parameter %d of '%s'!",
2018 (
int)(
I - exprs.begin()),
2030 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
2031 "Called with no wrapper, not implemented!");
2042template <
typename T>
2047 ::Error(
"TClingCallFunc::ExecT",
2048 "Called with no wrapper, not implemented!");
2053 if (!ret.isValid()) {
2060 return sv_to<T>(ret);
2065 return ExecT<long>(address);
2070 return ExecT<long long>(address);
2075 return ExecT<double>(address);
2079 int nargs ,
void *ret)
2083 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2084 "Called with no wrapper, not implemented!");
2087 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
2094 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
2095 "Called with no wrapper, not implemented!");
2102 unsigned long nary )
2105 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
2111 const Decl *D = info->
GetDecl();
2128 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
2129 "Called with no wrapper, not implemented!");
2133 (*wrapper)(&obj, address, nary);
2138 unsigned long nary ,
bool withFree )
2141 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
2148 const Decl *D = info->
GetDecl();
2157 ::Error(
"TClingCallFunc::ExecDestructor",
2158 "Called with no wrapper, not implemented!");
2161 (*wrapper)(address, nary, withFree);
2197 const FunctionDecl *decl =
GetDecl();
2200 map<const FunctionDecl *, void *>::iterator
I =
gWrapperStore.find(decl);
2221 ::Error(
"TClingCallFunc::IFacePtr(kind)",
2222 "Attempt to get interface while invalid.");
2226 const FunctionDecl *decl =
GetDecl();
2229 map<const FunctionDecl *, void *>::iterator
I =
gWrapperStore.find(decl);
2249 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2256 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2263 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2265 fArgVals.back().getFloat() = param;
2270 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2272 fArgVals.back().getDouble() = param;
2277 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2284 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2292 for (
int i = 0; i < nparam; ++i) {
2306 SetFunc(info, method, arglist,
false, poffset);
2310 bool objectIsConst,
long *poffset)
2318 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
2321 if (!strcmp(arglist,
")")) {
2347 const char *
proto,
long *poffset,
2354 const char *
proto,
bool objectIsConst,
long *poffset,
2363 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2375 const llvm::SmallVectorImpl<clang::QualType> &
proto,
long *poffset,
2382 const llvm::SmallVectorImpl<clang::QualType> &
proto,
2383 bool objectIsConst,
long *poffset,
2392 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
static void EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
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 FunctionDecl *, 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)
void Error(const char *location, const char *msgfmt,...)
R__EXTERN TVirtualMutex * gInterpreterMutex
#define R__LOCKGUARD_CLING(mutex)
R__EXTERN TInterpreter * gCling
typedef void((*Func_t)())
long ExecInt(void *address)
std::unique_ptr< TClingMethodInfo > fMethod
Current method, we own.
ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret)
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)
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *info)
void ExecWithReturn(void *address, void *ret=0)
void SetArgs(const char *args)
size_t fMinRequiredArguments
Number of required arguments.
size_t CalculateMinRequiredArguments()
double ExecDouble(void *address)
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, long *poffset)
tcling_callfunc_Wrapper_t make_wrapper()
ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret)
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
std::function< void(void *address, cling::Value &ret)> ExecWithRetFunc_t
void execWithLL(void *address, cling::Value *val)
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
TInterpreter::CallFuncIFacePtr_t IFacePtr()
void exec(void *address, void *ret)
void exec_with_valref_return(void *address, cling::Value *ret)
void make_narg_ctor(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
const clang::FunctionDecl * GetDecl()
void execWithULL(void *address, cling::Value *val)
void EvaluateArgList(const std::string &ArgList)
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
void SetArgArray(long *argArr, int narg)
void Exec(void *address, TInterpreterValue *interpVal=0)
TClingMethodInfo * FactoryMethod() const
void ExecDestructor(const TClingClassInfo *info, void *address=0, unsigned long nary=0UL, bool withFree=true)
ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret)
int get_wrapper_code(std::string &wrapper_name, std::string &wrapper)
size_t GetMinRequiredArguments()
const ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
ROOT normalized context for that interpreter.
tcling_callfunc_Wrapper_t fWrapper
Pointer to compiled wrapper, we do not own.
void * ExecDefaultConstructor(const TClingClassInfo *info, void *address=0, unsigned long nary=0UL)
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)
ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret)
Emulation of the CINT ClassInfo class.
TClingMethodInfo GetMethodWithArgs(const char *fname, const char *arglist, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch, EInheritanceMode imode=kWithInheritance) const
TClingMethodInfo GetMethod(const char *fname) const
virtual bool IsValid() const
virtual const clang::Decl * GetDecl() const
Emulation of the CINT MethodInfo class.
This class defines an interface to the cling C++ interpreter.
virtual const void * GetValAddr() const =0
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
RooArgSet S(const RooAbsArg &v1)
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.
static constexpr double nm
static constexpr double us
static constexpr double s
static constexpr double L
constexpr Double_t E()
Base of natural log: