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;
89indent(ostringstream &buf,
int indent_level)
91 for (
int i = 0; i < indent_level; ++i) {
98EvaluateExpr(cling::Interpreter &interp,
const Expr *E, cling::Value &V)
101 ASTContext &C = interp.getCI()->getASTContext();
102 clang::Expr::EvalResult evalRes;
103 if (E->EvaluateAsInt(evalRes, C, Expr::SE_NoSideEffects)) {
106 APSInt res = evalRes.Val.getInt();
108 V = cling::Value(C.IntTy, interp);
112 V.setLongLong(res.getSExtValue());
114 V.setULongLong(res.getZExtValue());
119 PrintingPolicy Policy(C.getPrintingPolicy());
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);
130 interp.evaluate(buf, V);
136 return GetDecl()->getMinRequiredArguments();
140 bool withAccessControl)
142 return fInterp->compileFunction(wrapper_name, wrapper,
false ,
147 PrintingPolicy Policy) {
153 cling::utils::Transform::Config Config;
154 QT = cling::utils::Transform::GetPartiallyDesugaredType(C, QT, Config,
true);
155 QT.getAsStringInternal(type_name, Policy);
159 string &type_name,
EReferenceType &refType,
bool &isPointer,
int indent_level,
166 const FunctionDecl *FD =
GetDecl();
167 ASTContext &C = FD->getASTContext();
168 PrintingPolicy Policy(C.getPrintingPolicy());
170 if (QT->isRecordType() && forArgument) {
174 if (QT->isFunctionPointerType()) {
175 string fp_typedef_name;
179 type_name = nm.str();
180 raw_string_ostream OS(fp_typedef_name);
181 QT.print(OS, Policy, type_name);
184 for (
int i = 0; i < indent_level; ++i) {
187 typedefbuf <<
"typedef " << fp_typedef_name <<
";\n";
189 }
else if (QT->isMemberPointerType()) {
190 string mp_typedef_name;
194 type_name = nm.str();
195 raw_string_ostream OS(mp_typedef_name);
196 QT.print(OS, Policy, type_name);
199 for (
int i = 0; i < indent_level; ++i) {
202 typedefbuf <<
"typedef " << mp_typedef_name <<
";\n";
204 }
else if (QT->isPointerType()) {
206 QT = cast<clang::PointerType>(QT)->getPointeeType();
207 }
else if (QT->isReferenceType()) {
210 QT = cast<ReferenceType>(QT)->getPointeeType();
213 if (QT->isArrayType()) {
214 string ar_typedef_name;
218 type_name = ar.str();
219 raw_string_ostream OS(ar_typedef_name);
220 QT.print(OS, Policy, type_name);
223 for (
int i = 0; i < indent_level; ++i) {
226 typedefbuf <<
"typedef " << ar_typedef_name <<
";\n";
233 ostringstream &callbuf,
const string &class_name,
240 const FunctionDecl *FD =
GetDecl();
242 callbuf <<
"new " << class_name <<
"(";
243 for (
unsigned i = 0U; i <
N; ++i) {
244 const ParmVarDecl *PVD = FD->getParamDecl(i);
245 QualType Ty = PVD->getType();
246 QualType QT = Ty.getCanonicalType();
249 bool isPointer =
false;
251 refType, isPointer, indent_level,
true);
258 for (
int j = 0; j <= indent_level; ++j) {
264 callbuf <<
"(" << type_name.c_str() <<
265 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
267 }
else if (isPointer) {
268 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
271 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
278 ostringstream &callbuf,
const string &class_name,
int indent_level)
285 const FunctionDecl *FD =
GetDecl();
298 bool ShouldCastFunction = !isa<CXXMethodDecl>(FD) &&
N == FD->getNumParams();
299 if (ShouldCastFunction) {
302 callbuf << return_type <<
" (&)";
305 for (
unsigned i = 0U; i <
N; ++i) {
312 for (
int j = 0; j <= indent_level; ++j) {
317 const ParmVarDecl *PVD = FD->getParamDecl(i);
318 QualType Ty = PVD->getType();
319 QualType QT = Ty.getCanonicalType();
320 std::string arg_type;
321 ASTContext &C = FD->getASTContext();
325 if (FD->isVariadic())
333 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
336 callbuf <<
"((const " << class_name <<
"*)obj)->";
338 callbuf <<
"((" << class_name <<
"*)obj)->";
339 }
else if (
const NamedDecl *ND =
343 callbuf << class_name <<
"::";
349 llvm::raw_string_ostream stream(
name);
350 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
354 if (ShouldCastFunction) callbuf <<
")";
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;
364 collect_type_info(QT, typedefbuf, callbuf, type_name, refType, isPointer, indent_level,
true);
372 for (
int j = 0; j <= indent_level; ++j) {
379 callbuf <<
"(" << type_name.c_str() <<
380 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
382 }
else if (isPointer) {
383 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
388 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
395 ostringstream &buf,
int indent_level)
406 for (
int i = 0; i < indent_level; ++i) {
409 buf <<
"if (ret) {\n";
412 ostringstream typedefbuf;
413 ostringstream callbuf;
417 for (
int i = 0; i < indent_level; ++i) {
420 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
429 for (
int i = 0; i < indent_level; ++i) {
432 callbuf <<
"return;\n";
436 buf << typedefbuf.str() << callbuf.str();
439 for (
int i = 0; i < indent_level; ++i) {
443 for (
int i = 0; i < indent_level; ++i) {
449 ostringstream typedefbuf;
450 ostringstream callbuf;
451 for (
int i = 0; i < indent_level; ++i) {
456 for (
int i = 0; i < indent_level; ++i) {
459 callbuf <<
"return;\n";
460 buf << typedefbuf.str() << callbuf.str();
463 for (
int i = 0; i < indent_level; ++i) {
477 return fMethod->GetDecl()->getDeclContext();
482 const FunctionDecl *FD =
GetDecl();
483 assert(FD &&
"generate_wrapper called without a function decl!");
484 ASTContext &Context = FD->getASTContext();
485 PrintingPolicy Policy(Context.getPrintingPolicy());
491 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(DC)) {
493 QualType QT(TD->getTypeForDecl(), 0);
495 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
497 raw_string_ostream stream(class_name);
498 ND->getNameForDiagnostic(stream, Policy,
true);
505 bool needInstantiation =
false;
506 const FunctionDecl *Definition =
nullptr;
507 if (!FD->isDefined(Definition)) {
508 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
510 case FunctionDecl::TK_NonTemplate: {
520 case FunctionDecl::TK_FunctionTemplate: {
523 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
526 case FunctionDecl::TK_MemberSpecialization: {
533 if (!FD->isTemplateInstantiation()) {
546 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
548 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
549 "instantiation with no pattern!");
552 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
553 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
556 (PTK == FunctionDecl::TK_NonTemplate) ||
559 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
560 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
565 }
else if (!
Pattern->hasBody()) {
566 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
567 "instantiation with no body!");
570 if (FD->isImplicitlyInstantiable()) {
571 needInstantiation =
true;
574 case FunctionDecl::TK_FunctionTemplateSpecialization: {
579 if (!FD->isTemplateInstantiation()) {
592 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
594 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
595 "instantiation with no pattern!");
598 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
599 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
602 (PTK == FunctionDecl::TK_NonTemplate) ||
605 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
606 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
613 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
614 "instantiation with no body!");
617 if (FD->isImplicitlyInstantiable()) {
618 needInstantiation =
true;
621 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
629 if (!FD->isTemplateInstantiation()) {
642 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
644 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
645 "instantiation with no pattern!");
648 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
649 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
652 (PTK == FunctionDecl::TK_NonTemplate) ||
655 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
656 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
663 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
664 "instantiation with no body!");
667 if (FD->isImplicitlyInstantiable()) {
668 needInstantiation =
true;
674 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
696 if (needInstantiation) {
697 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
698 clang::Sema &S =
fInterp->getSema();
700 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
701 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
704 if (!FD->isDefined(Definition)) {
705 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
710 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
712 case FunctionDecl::TK_NonTemplate: {
714 if (Definition->isDeleted()) {
715 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
717 }
else if (Definition->isLateTemplateParsed()) {
718 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
729 case FunctionDecl::TK_FunctionTemplate: {
732 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
735 case FunctionDecl::TK_MemberSpecialization: {
739 if (Definition->isDeleted()) {
740 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
741 "of a specialization!");
743 }
else if (Definition->isLateTemplateParsed()) {
744 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
745 "member function of a specialization!");
755 case FunctionDecl::TK_FunctionTemplateSpecialization: {
760 if (Definition->isDeleted()) {
761 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
762 "template specialization!");
764 }
else if (Definition->isLateTemplateParsed()) {
765 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
766 "function template specialization!");
776 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
784 if (Definition->isDeleted()) {
785 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
786 "template specialization!");
788 }
else if (Definition->isLateTemplateParsed()) {
789 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
790 "dependent function template specialization!");
803 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
809 unsigned num_params = FD->getNumParams();
821 wrapper_name = buf.str();
827 int indent_level = 0;
829 buf <<
"#pragma clang diagnostic push\n"
830 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
831 "__attribute__((used)) "
832 "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
833 "extern \"C\" void ";
835 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
838 if (min_args == num_params) {
844 for (
unsigned N = min_args;
N <= num_params; ++
N) {
845 for (
int i = 0; i < indent_level; ++i) {
848 buf <<
"if (nargs == " <<
N <<
") {\n";
852 for (
int i = 0; i < indent_level; ++i) {
860 "#pragma clang diagnostic pop";
866 ostringstream &buf,
int indent_level)
877 const FunctionDecl *FD =
GetDecl();
878 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
880 auto SpecMemKind =
fInterp->getSema().getSpecialMember(CD);
881 if ((
N == 0 && SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
883 (SpecMemKind == clang::Sema::CXXCopyConstructor || SpecMemKind == clang::Sema::CXXMoveConstructor))) {
894 QualType QT = FD->getReturnType().getCanonicalType();
895 if (QT->isVoidType()) {
896 ostringstream typedefbuf;
897 ostringstream callbuf;
898 for (
int i = 0; i < indent_level; ++i) {
901 make_narg_call(
"void",
N, typedefbuf, callbuf, class_name, indent_level);
903 for (
int i = 0; i < indent_level; ++i) {
906 callbuf <<
"return;\n";
907 buf << typedefbuf.str() << callbuf.str();
909 for (
int i = 0; i < indent_level; ++i) {
915 bool isPointer =
false;
917 buf <<
"if (ret) {\n";
920 ostringstream typedefbuf;
921 ostringstream callbuf;
925 for (
int i = 0; i < indent_level; ++i) {
928 callbuf <<
"new (ret) ";
930 refType, isPointer, indent_level,
false);
934 callbuf <<
"(" << type_name.c_str();
938 }
else if (isPointer) {
947 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
952 for (
int i = 0; i < indent_level; ++i) {
955 callbuf <<
"return;\n";
959 buf << typedefbuf.str() << callbuf.str();
962 for (
int i = 0; i < indent_level; ++i) {
966 for (
int i = 0; i < indent_level; ++i) {
972 ostringstream typedefbuf;
973 ostringstream callbuf;
974 for (
int i = 0; i < indent_level; ++i) {
977 callbuf <<
"(void)(";
978 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
980 for (
int i = 0; i < indent_level; ++i) {
983 callbuf <<
"return;\n";
984 buf << typedefbuf.str() << callbuf.str();
987 for (
int i = 0; i < indent_level; ++i) {
1012 ::Error(
"TClingCallFunc::make_wrapper",
1013 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1090 ASTContext &Context = info->
GetDecl()->getASTContext();
1091 PrintingPolicy Policy(Context.getPrintingPolicy());
1092 Policy.SuppressTagKeyword =
true;
1093 Policy.SuppressUnwrittenScope =
true;
1098 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1100 QualType QT(TD->getTypeForDecl(), 0);
1102 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1104 raw_string_ostream stream(class_name);
1105 ND->getNameForDiagnostic(stream, Policy,
true);
1113 string wrapper_name;
1122 wrapper_name = buf.str();
1127 constr_arg = string(
"((") + type_name +
"*)nullptr)";
1129 constr_arg = string(
"(*((") + type_name +
"*)arena))";
1134 int indent_level = 0;
1136 buf <<
"__attribute__((used)) ";
1137 buf <<
"extern \"C\" void ";
1138 buf << wrapper_name;
1139 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1150 indent(buf, ++indent_level);
1151 buf <<
"if (!arena) {\n";
1152 indent(buf, ++indent_level);
1153 buf <<
"if (!nary) {\n";
1154 indent(buf, ++indent_level);
1155 buf <<
"*ret = new " << class_name << constr_arg <<
";\n";
1156 indent(buf, --indent_level);
1158 indent(buf, indent_level);
1160 indent(buf, ++indent_level);
1161 if (constr_arg.empty()) {
1162 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1164 buf <<
"char *buf = (char *) malloc(nary * sizeof(" << class_name <<
"));\n";
1165 indent(buf, indent_level);
1166 buf <<
"for (int k=0;k<nary;++k)\n";
1167 indent(buf, ++indent_level);
1168 buf <<
"new (buf + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1169 indent(buf, --indent_level);
1170 buf <<
"*ret = buf;\n";
1172 indent(buf, --indent_level);
1174 indent(buf, --indent_level);
1184 indent(buf, indent_level);
1186 indent(buf, ++indent_level);
1187 buf <<
"if (!nary) {\n";
1188 indent(buf, ++indent_level);
1189 buf <<
"*ret = new (arena) " << class_name << constr_arg <<
";\n";
1190 indent(buf, --indent_level);
1192 indent(buf, indent_level);
1194 indent(buf, ++indent_level);
1195 if (constr_arg.empty()) {
1196 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1198 buf <<
"for (int k=0;k<nary;++k)\n";
1199 indent(buf, ++indent_level);
1200 buf <<
"new ((char *) arena + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1201 indent(buf, --indent_level);
1202 buf <<
"*ret = arena;\n";
1204 indent(buf, --indent_level);
1206 indent(buf, --indent_level);
1212 string wrapper(buf.str());
1222 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1223 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1259 ASTContext &Context = info->
GetDecl()->getASTContext();
1260 PrintingPolicy Policy(Context.getPrintingPolicy());
1261 Policy.SuppressTagKeyword =
true;
1262 Policy.SuppressUnwrittenScope =
true;
1267 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1269 QualType QT(TD->getTypeForDecl(), 0);
1271 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1273 raw_string_ostream stream(class_name);
1274 ND->getNameForDiagnostic(stream, Policy,
true);
1280 string wrapper_name;
1289 wrapper_name = buf.str();
1294 int indent_level = 0;
1296 buf <<
"__attribute__((used)) ";
1297 buf <<
"extern \"C\" void ";
1298 buf << wrapper_name;
1299 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1310 indent(buf, indent_level);
1311 buf <<
"if (withFree) {\n";
1313 indent(buf, indent_level);
1314 buf <<
"if (!nary) {\n";
1316 indent(buf, indent_level);
1317 buf <<
"delete (" << class_name <<
"*) obj;\n";
1319 indent(buf, indent_level);
1321 indent(buf, indent_level);
1324 indent(buf, indent_level);
1325 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1327 indent(buf, indent_level);
1330 indent(buf, indent_level);
1343 indent(buf, indent_level);
1346 indent(buf, indent_level);
1347 buf <<
"typedef " << class_name <<
" Nm;\n";
1348 buf <<
"if (!nary) {\n";
1350 indent(buf, indent_level);
1351 buf <<
"((Nm*)obj)->~Nm();\n";
1353 indent(buf, indent_level);
1355 indent(buf, indent_level);
1358 indent(buf, indent_level);
1361 indent(buf, indent_level);
1362 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1364 indent(buf, indent_level);
1365 buf <<
"} while (nary);\n";
1367 indent(buf, indent_level);
1370 indent(buf, indent_level);
1376 string wrapper(buf.str());
1386 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1387 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1396 SmallVector<void *, 8> vp_ary;
1397 const unsigned num_args =
fArgVals.size();
1400 const FunctionDecl *FD =
GetDecl();
1404 ::Error(
"TClingCallFunc::exec",
1405 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1409 }
else if (!isa<CXXMethodDecl>(FD) && num_args > FD->getNumParams()) {
1410 ::Error(
"TClingCallFunc::exec",
1411 "Too many arguments provided for %s (%d instead of the minimum %d)",
1416 if (
auto CXXMD = dyn_cast<CXXMethodDecl>(FD))
1417 if (!address && CXXMD && !CXXMD->isStatic() && !isa<CXXConstructorDecl>(FD)) {
1418 ::Error(
"TClingCallFunc::exec",
1419 "The method %s is called without an object.",
1424 vp_ary.reserve(num_args);
1425 for (
unsigned i = 0; i < num_args; ++i) {
1433 bool implicitThisPassed = i == 0 && isa<CXXMethodDecl>(FD) && num_args - FD->getNumParams() == 1;
1434 if (implicitThisPassed)
1435 QT = cast<CXXMethodDecl>(FD)->getThisType();
1437 QT = FD->getParamDecl(i)->getType();
1438 QT = QT.getCanonicalType();
1439 if (QT->isReferenceType() || QT->isRecordType()) {
1442 vp_ary.push_back(
fArgVals[i].getPtr());
1447 ASTContext &C = FD->getASTContext();
1448 if (QT->isBuiltinType() && !C.hasSameType(QT,
fArgVals[i].getType())) {
1449 switch(QT->getAs<BuiltinType>()->getKind()) {
1456#define X(type, name) \
1457 case BuiltinType::name: fArgVals[i] = cling::Value::Create(*fInterp, fArgVals[i].castAs<type>()); break;
1458 CLING_VALUE_BUILTIN_TYPES
1462 vp_ary.push_back(
fArgVals[i].getPtrAddress());
1466 (*fWrapper)(address, (
int)num_args, (
void **)vp_ary.data(), ret);
1471 const FunctionDecl *FD =
GetDecl();
1474 if (llvm::isa<CXXConstructorDecl>(FD)) {
1476 ASTContext &Context = FD->getASTContext();
1478 QualType ClassTy(TD->getTypeForDecl(), 0);
1479 QT = Context.getLValueReferenceType(ClassTy);
1480 ret = cling::Value(QT, *
fInterp);
1482 QT = FD->getReturnType().getCanonicalType();
1483 ret = cling::Value(QT, *
fInterp);
1485 if (QT->isRecordType() || QT->isMemberDataPointerType())
1486 return exec(address, ret.getPtr());
1488 exec(address, ret.getPtrAddress());
1495 SmallVector<Expr *, 4> exprs;
1496 fInterp->getLookupHelper().findArgList(ArgList, exprs,
1497 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1498 : cling::LookupHelper::NoDiagnostics);
1499 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
1500 E = exprs.end();
I != E; ++
I) {
1503 if (!val.isValid()) {
1505 ::Error(
"TClingCallFunc::EvaluateArgList",
1506 "Bad expression in parameter %d of '%s'!",
1507 (
int)(
I - exprs.begin()),
1519 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
1520 "Called with no wrapper, not implemented!");
1523 if (!interpVal || !interpVal->
GetValAddr()) {
1524 exec(address,
nullptr);
1527 cling::Value *val =
reinterpret_cast<cling::Value *
>(interpVal->
GetValAddr());
1531template <
typename T>
1536 ::Error(
"TClingCallFunc::ExecT",
1537 "Called with no wrapper, not implemented!");
1546 if (ret.needsManagedAllocation())
1549 return ret.castAs<T>();
1554 return ExecT<Longptr_t>(address);
1559 return ExecT<long long>(address);
1564 return ExecT<double>(address);
1568 int nargs ,
void *ret)
1572 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
1573 "Called with no wrapper, not implemented!");
1576 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
1583 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
1584 "Called with no wrapper, not implemented!");
1592 const std::string &type_name,
1593 void *address ,
unsigned long nary )
1596 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
1619 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
1620 "Called with no wrapper, not implemented!");
1623 void *obj =
nullptr;
1624 (*wrapper)(&obj, address, nary);
1629 unsigned long nary ,
bool withFree )
1632 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
1639 const Decl *D = info->
GetDecl();
1648 ::Error(
"TClingCallFunc::ExecDestructor",
1649 "Called with no wrapper, not implemented!");
1652 (*wrapper)(address, nary, withFree);
1693 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
1715 ::Error(
"TClingCallFunc::IFacePtr(kind)",
1716 "Attempt to get interface while invalid.");
1725 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
1745 for (
int i = 0; i < nparam; ++i) {
1759 SetFunc(info, method, arglist,
false, poffset);
1771 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
1774 if (!strcmp(arglist,
")")) {
1816 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
1828 const llvm::SmallVectorImpl<clang::QualType> &
proto,
Longptr_t *poffset,
1835 const llvm::SmallVectorImpl<clang::QualType> &
proto,
1845 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
static void EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
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)
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
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.
TClingMethodInfo GetMethodWithArgs(const char *fname, const char *arglist, Longptr_t *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
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...