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";
234 CXXRecordDecl *RD = QT->getAsCXXRecordDecl();
241 RD = RD->getDefinition();
242 assert(RD &&
"expecting a definition");
244 if (RD->hasSimpleCopyConstructor())
247 for (
auto *Ctor : RD->ctors()) {
248 if (Ctor->isCopyConstructor()) {
249 return Ctor->isDeleted();
253 assert(0 &&
"did not find a copy constructor?");
260 ostringstream &callbuf,
const string &class_name,
267 const FunctionDecl *FD =
GetDecl();
269 callbuf <<
"new " << class_name <<
"(";
272 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
274 for (
unsigned i = 0U; i <
N; ++i) {
275 const ParmVarDecl *PVD = FD->getParamDecl(i);
276 QualType Ty = PVD->getType();
277 QualType QT = Ty.getCanonicalType();
280 bool isPointer =
false;
282 refType, isPointer, indent_level,
true);
289 for (
int j = 0; j <= indent_level; ++j) {
295 callbuf <<
"(" << type_name.c_str() <<
296 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
298 }
else if (isPointer) {
299 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
307 callbuf <<
"static_cast<" << type_name <<
"&&>(";
309 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
319 ostringstream &callbuf,
const string &class_name,
int indent_level)
326 const FunctionDecl *FD =
GetDecl();
339 bool ShouldCastFunction = !isa<CXXMethodDecl>(FD) &&
N == FD->getNumParams();
340 if (ShouldCastFunction) {
343 callbuf << return_type <<
" (&)";
346 for (
unsigned i = 0U; i <
N; ++i) {
353 for (
int j = 0; j <= indent_level; ++j) {
358 const ParmVarDecl *PVD = FD->getParamDecl(i);
359 QualType Ty = PVD->getType();
360 QualType QT = Ty.getCanonicalType();
361 std::string arg_type;
362 ASTContext &C = FD->getASTContext();
366 if (FD->isVariadic())
374 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
377 callbuf <<
"((const " << class_name <<
"*)obj)->";
379 callbuf <<
"((" << class_name <<
"*)obj)->";
380 }
else if (
const NamedDecl *ND =
384 callbuf << class_name <<
"::";
390 llvm::raw_string_ostream stream(
name);
391 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
395 if (ShouldCastFunction) callbuf <<
")";
400 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
402 for (
unsigned i = 0U; i <
N; ++i) {
403 const ParmVarDecl *PVD = FD->getParamDecl(i);
404 QualType Ty = PVD->getType();
405 QualType QT = Ty.getCanonicalType();
408 bool isPointer =
false;
409 collect_type_info(QT, typedefbuf, callbuf, type_name, refType, isPointer, indent_level,
true);
417 for (
int j = 0; j <= indent_level; ++j) {
424 callbuf <<
"(" << type_name.c_str() <<
425 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
427 }
else if (isPointer) {
428 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
436 callbuf <<
"static_cast<" << type_name <<
"&&>(";
438 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
448 ostringstream &buf,
int indent_level)
459 for (
int i = 0; i < indent_level; ++i) {
462 buf <<
"if (ret) {\n";
465 ostringstream typedefbuf;
466 ostringstream callbuf;
470 for (
int i = 0; i < indent_level; ++i) {
473 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
482 for (
int i = 0; i < indent_level; ++i) {
485 callbuf <<
"return;\n";
489 buf << typedefbuf.str() << callbuf.str();
492 for (
int i = 0; i < indent_level; ++i) {
496 for (
int i = 0; i < indent_level; ++i) {
502 ostringstream typedefbuf;
503 ostringstream callbuf;
504 for (
int i = 0; i < indent_level; ++i) {
509 for (
int i = 0; i < indent_level; ++i) {
512 callbuf <<
"return;\n";
513 buf << typedefbuf.str() << callbuf.str();
516 for (
int i = 0; i < indent_level; ++i) {
530 return fMethod->GetDecl()->getDeclContext();
535 const FunctionDecl *FD =
GetDecl();
536 assert(FD &&
"generate_wrapper called without a function decl!");
537 ASTContext &Context = FD->getASTContext();
538 PrintingPolicy Policy(Context.getPrintingPolicy());
544 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(DC)) {
546 QualType QT(TD->getTypeForDecl(), 0);
548 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
550 raw_string_ostream stream(class_name);
551 ND->getNameForDiagnostic(stream, Policy,
true);
558 bool needInstantiation =
false;
559 const FunctionDecl *Definition =
nullptr;
560 if (!FD->isDefined(Definition)) {
561 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
563 case FunctionDecl::TK_NonTemplate: {
573 case FunctionDecl::TK_FunctionTemplate: {
576 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
579 case FunctionDecl::TK_MemberSpecialization: {
586 if (!FD->isTemplateInstantiation()) {
599 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
601 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
602 "instantiation with no pattern!");
605 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
606 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
609 (PTK == FunctionDecl::TK_NonTemplate) ||
612 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
613 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
618 }
else if (!
Pattern->hasBody()) {
619 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
620 "instantiation with no body!");
623 if (FD->isImplicitlyInstantiable()) {
624 needInstantiation =
true;
627 case FunctionDecl::TK_FunctionTemplateSpecialization: {
632 if (!FD->isTemplateInstantiation()) {
645 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
647 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
648 "instantiation with no pattern!");
651 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
652 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
655 (PTK == FunctionDecl::TK_NonTemplate) ||
658 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
659 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
666 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
667 "instantiation with no body!");
670 if (FD->isImplicitlyInstantiable()) {
671 needInstantiation =
true;
674 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
682 if (!FD->isTemplateInstantiation()) {
695 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
697 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
698 "instantiation with no pattern!");
701 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
702 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
705 (PTK == FunctionDecl::TK_NonTemplate) ||
708 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
709 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
716 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
717 "instantiation with no body!");
720 if (FD->isImplicitlyInstantiable()) {
721 needInstantiation =
true;
727 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
749 if (needInstantiation) {
750 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
751 clang::Sema &S =
fInterp->getSema();
753 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
754 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
757 if (!FD->isDefined(Definition)) {
758 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
763 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
765 case FunctionDecl::TK_NonTemplate: {
767 if (Definition->isDeleted()) {
768 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
770 }
else if (Definition->isLateTemplateParsed()) {
771 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
782 case FunctionDecl::TK_FunctionTemplate: {
785 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
788 case FunctionDecl::TK_MemberSpecialization: {
792 if (Definition->isDeleted()) {
793 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
794 "of a specialization!");
796 }
else if (Definition->isLateTemplateParsed()) {
797 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
798 "member function of a specialization!");
808 case FunctionDecl::TK_FunctionTemplateSpecialization: {
813 if (Definition->isDeleted()) {
814 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
815 "template specialization!");
817 }
else if (Definition->isLateTemplateParsed()) {
818 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
819 "function template specialization!");
829 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
837 if (Definition->isDeleted()) {
838 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
839 "template specialization!");
841 }
else if (Definition->isLateTemplateParsed()) {
842 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
843 "dependent function template specialization!");
856 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
862 unsigned num_params = FD->getNumParams();
874 wrapper_name = buf.str();
880 int indent_level = 0;
882 buf <<
"#pragma clang diagnostic push\n"
883 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
884 "__attribute__((used)) "
885 "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
886 "extern \"C\" void ";
888 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
891 if (min_args == num_params) {
897 for (
unsigned N = min_args;
N <= num_params; ++
N) {
898 for (
int i = 0; i < indent_level; ++i) {
901 buf <<
"if (nargs == " <<
N <<
") {\n";
905 for (
int i = 0; i < indent_level; ++i) {
913 "#pragma clang diagnostic pop";
919 ostringstream &buf,
int indent_level)
930 const FunctionDecl *FD =
GetDecl();
931 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
933 auto SpecMemKind =
fInterp->getSema().getSpecialMember(CD);
934 if ((
N == 0 && SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
936 (SpecMemKind == clang::Sema::CXXCopyConstructor || SpecMemKind == clang::Sema::CXXMoveConstructor))) {
947 QualType QT = FD->getReturnType().getCanonicalType();
948 if (QT->isVoidType()) {
949 ostringstream typedefbuf;
950 ostringstream callbuf;
951 for (
int i = 0; i < indent_level; ++i) {
954 make_narg_call(
"void",
N, typedefbuf, callbuf, class_name, indent_level);
956 for (
int i = 0; i < indent_level; ++i) {
959 callbuf <<
"return;\n";
960 buf << typedefbuf.str() << callbuf.str();
962 for (
int i = 0; i < indent_level; ++i) {
968 bool isPointer =
false;
970 buf <<
"if (ret) {\n";
973 ostringstream typedefbuf;
974 ostringstream callbuf;
978 for (
int i = 0; i < indent_level; ++i) {
981 callbuf <<
"new (ret) ";
983 refType, isPointer, indent_level,
false);
987 callbuf <<
"(" << type_name.c_str();
991 }
else if (isPointer) {
1000 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1005 for (
int i = 0; i < indent_level; ++i) {
1008 callbuf <<
"return;\n";
1012 buf << typedefbuf.str() << callbuf.str();
1015 for (
int i = 0; i < indent_level; ++i) {
1019 for (
int i = 0; i < indent_level; ++i) {
1025 ostringstream typedefbuf;
1026 ostringstream callbuf;
1027 for (
int i = 0; i < indent_level; ++i) {
1030 callbuf <<
"(void)(";
1031 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1033 for (
int i = 0; i < indent_level; ++i) {
1036 callbuf <<
"return;\n";
1037 buf << typedefbuf.str() << callbuf.str();
1040 for (
int i = 0; i < indent_level; ++i) {
1052 string wrapper_name;
1065 ::Error(
"TClingCallFunc::make_wrapper",
1066 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1143 ASTContext &Context = info->
GetDecl()->getASTContext();
1144 PrintingPolicy Policy(Context.getPrintingPolicy());
1145 Policy.SuppressTagKeyword =
true;
1146 Policy.SuppressUnwrittenScope =
true;
1151 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1153 QualType QT(TD->getTypeForDecl(), 0);
1155 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1157 raw_string_ostream stream(class_name);
1158 ND->getNameForDiagnostic(stream, Policy,
true);
1166 string wrapper_name;
1175 wrapper_name = buf.str();
1180 constr_arg = string(
"((") + type_name +
"*)nullptr)";
1182 constr_arg = string(
"(*((") + type_name +
"*)arena))";
1187 int indent_level = 0;
1189 buf <<
"__attribute__((used)) ";
1190 buf <<
"extern \"C\" void ";
1191 buf << wrapper_name;
1192 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1203 indent(buf, ++indent_level);
1204 buf <<
"if (!arena) {\n";
1205 indent(buf, ++indent_level);
1206 buf <<
"if (!nary) {\n";
1207 indent(buf, ++indent_level);
1208 buf <<
"*ret = new " << class_name << constr_arg <<
";\n";
1209 indent(buf, --indent_level);
1211 indent(buf, indent_level);
1213 indent(buf, ++indent_level);
1214 if (constr_arg.empty()) {
1215 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1217 buf <<
"char *buf = (char *) malloc(nary * sizeof(" << class_name <<
"));\n";
1218 indent(buf, indent_level);
1219 buf <<
"for (int k=0;k<nary;++k)\n";
1220 indent(buf, ++indent_level);
1221 buf <<
"new (buf + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1222 indent(buf, --indent_level);
1223 buf <<
"*ret = buf;\n";
1225 indent(buf, --indent_level);
1227 indent(buf, --indent_level);
1237 indent(buf, indent_level);
1239 indent(buf, ++indent_level);
1240 buf <<
"if (!nary) {\n";
1241 indent(buf, ++indent_level);
1242 buf <<
"*ret = new (arena) " << class_name << constr_arg <<
";\n";
1243 indent(buf, --indent_level);
1245 indent(buf, indent_level);
1247 indent(buf, ++indent_level);
1248 if (constr_arg.empty()) {
1249 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1251 buf <<
"for (int k=0;k<nary;++k)\n";
1252 indent(buf, ++indent_level);
1253 buf <<
"new ((char *) arena + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1254 indent(buf, --indent_level);
1255 buf <<
"*ret = arena;\n";
1257 indent(buf, --indent_level);
1259 indent(buf, --indent_level);
1265 string wrapper(buf.str());
1275 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1276 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1312 ASTContext &Context = info->
GetDecl()->getASTContext();
1313 PrintingPolicy Policy(Context.getPrintingPolicy());
1314 Policy.SuppressTagKeyword =
true;
1315 Policy.SuppressUnwrittenScope =
true;
1320 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1322 QualType QT(TD->getTypeForDecl(), 0);
1324 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1326 raw_string_ostream stream(class_name);
1327 ND->getNameForDiagnostic(stream, Policy,
true);
1333 string wrapper_name;
1342 wrapper_name = buf.str();
1347 int indent_level = 0;
1349 buf <<
"__attribute__((used)) ";
1350 buf <<
"extern \"C\" void ";
1351 buf << wrapper_name;
1352 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1363 indent(buf, indent_level);
1364 buf <<
"if (withFree) {\n";
1366 indent(buf, indent_level);
1367 buf <<
"if (!nary) {\n";
1369 indent(buf, indent_level);
1370 buf <<
"delete (" << class_name <<
"*) obj;\n";
1372 indent(buf, indent_level);
1374 indent(buf, indent_level);
1377 indent(buf, indent_level);
1378 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1380 indent(buf, indent_level);
1383 indent(buf, indent_level);
1396 indent(buf, indent_level);
1399 indent(buf, indent_level);
1400 buf <<
"typedef " << class_name <<
" Nm;\n";
1401 buf <<
"if (!nary) {\n";
1403 indent(buf, indent_level);
1404 buf <<
"((Nm*)obj)->~Nm();\n";
1406 indent(buf, indent_level);
1408 indent(buf, indent_level);
1411 indent(buf, indent_level);
1414 indent(buf, indent_level);
1415 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1417 indent(buf, indent_level);
1418 buf <<
"} while (nary);\n";
1420 indent(buf, indent_level);
1423 indent(buf, indent_level);
1429 string wrapper(buf.str());
1439 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1440 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1449 SmallVector<void *, 8> vp_ary;
1450 const unsigned num_args =
fArgVals.size();
1453 const FunctionDecl *FD =
GetDecl();
1457 ::Error(
"TClingCallFunc::exec",
1458 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1462 }
else if (!isa<CXXMethodDecl>(FD) && num_args > FD->getNumParams()) {
1463 ::Error(
"TClingCallFunc::exec",
1464 "Too many arguments provided for %s (%d instead of the minimum %d)",
1469 if (
auto CXXMD = dyn_cast<CXXMethodDecl>(FD))
1470 if (!address && CXXMD && !CXXMD->isStatic() && !isa<CXXConstructorDecl>(FD)) {
1471 ::Error(
"TClingCallFunc::exec",
1472 "The method %s is called without an object.",
1477 vp_ary.reserve(num_args);
1478 for (
unsigned i = 0; i < num_args; ++i) {
1486 bool implicitThisPassed = i == 0 && isa<CXXMethodDecl>(FD) && num_args - FD->getNumParams() == 1;
1487 if (implicitThisPassed)
1488 QT = cast<CXXMethodDecl>(FD)->getThisType();
1490 QT = FD->getParamDecl(i)->getType();
1491 QT = QT.getCanonicalType();
1492 if (QT->isReferenceType() || QT->isRecordType()) {
1495 vp_ary.push_back(
fArgVals[i].getPtr());
1500 ASTContext &C = FD->getASTContext();
1501 if (QT->isBuiltinType() && !C.hasSameType(QT,
fArgVals[i].getType())) {
1502 switch(QT->getAs<BuiltinType>()->getKind()) {
1509#define X(type, name) \
1510 case BuiltinType::name: fArgVals[i] = cling::Value::Create(*fInterp, fArgVals[i].castAs<type>()); break;
1511 CLING_VALUE_BUILTIN_TYPES
1515 vp_ary.push_back(
fArgVals[i].getPtrAddress());
1519 (*fWrapper)(address, (
int)num_args, (
void **)vp_ary.data(), ret);
1524 const FunctionDecl *FD =
GetDecl();
1527 if (llvm::isa<CXXConstructorDecl>(FD)) {
1529 ASTContext &Context = FD->getASTContext();
1531 QualType ClassTy(TD->getTypeForDecl(), 0);
1532 QT = Context.getLValueReferenceType(ClassTy);
1533 ret = cling::Value(QT, *
fInterp);
1535 QT = FD->getReturnType().getCanonicalType();
1536 ret = cling::Value(QT, *
fInterp);
1538 if (QT->isRecordType() || QT->isMemberDataPointerType())
1539 return exec(address, ret.getPtr());
1541 exec(address, ret.getPtrAddress());
1548 SmallVector<Expr *, 4> exprs;
1549 fInterp->getLookupHelper().findArgList(ArgList, exprs,
1550 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1551 : cling::LookupHelper::NoDiagnostics);
1552 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
1553 E = exprs.end();
I != E; ++
I) {
1556 if (!val.isValid()) {
1558 ::Error(
"TClingCallFunc::EvaluateArgList",
1559 "Bad expression in parameter %d of '%s'!",
1560 (
int)(
I - exprs.begin()),
1572 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
1573 "Called with no wrapper, not implemented!");
1576 if (!interpVal || !interpVal->
GetValAddr()) {
1577 exec(address,
nullptr);
1580 cling::Value *val =
reinterpret_cast<cling::Value *
>(interpVal->
GetValAddr());
1584template <
typename T>
1589 ::Error(
"TClingCallFunc::ExecT",
1590 "Called with no wrapper, not implemented!");
1599 if (ret.needsManagedAllocation())
1602 return ret.castAs<T>();
1607 return ExecT<Longptr_t>(address);
1612 return ExecT<long long>(address);
1617 return ExecT<double>(address);
1621 int nargs ,
void *ret)
1625 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
1626 "Called with no wrapper, not implemented!");
1629 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
1636 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
1637 "Called with no wrapper, not implemented!");
1645 const std::string &type_name,
1646 void *address ,
unsigned long nary )
1649 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
1672 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
1673 "Called with no wrapper, not implemented!");
1676 void *obj =
nullptr;
1677 (*wrapper)(&obj, address, nary);
1682 unsigned long nary ,
bool withFree )
1685 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
1692 const Decl *D = info->
GetDecl();
1701 ::Error(
"TClingCallFunc::ExecDestructor",
1702 "Called with no wrapper, not implemented!");
1705 (*wrapper)(address, nary, withFree);
1746 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
1768 ::Error(
"TClingCallFunc::IFacePtr(kind)",
1769 "Attempt to get interface while invalid.");
1778 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
1798 for (
int i = 0; i < nparam; ++i) {
1812 SetFunc(info, method, arglist,
false, poffset);
1824 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
1827 if (!strcmp(arglist,
")")) {
1869 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
1881 const llvm::SmallVectorImpl<clang::QualType> &
proto,
Longptr_t *poffset,
1888 const llvm::SmallVectorImpl<clang::QualType> &
proto,
1898 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
static void EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
static bool IsCopyConstructorDeleted(QualType QT)
static void GetTypeAsString(QualType QT, string &type_name, ASTContext &C, PrintingPolicy Policy)
static unsigned long long gWrapperSerial
static map< const Decl *, void * > gCtorWrapperStore
static const string kIndentString(" ")
static void indent(ostringstream &buf, int indent_level)
static map< const Decl *, void * > gDtorWrapperStore
static map< const Decl *, void * > gWrapperStore
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
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...