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"
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)) {
104 APSInt res = evalRes.Val.getInt();
106 V = cling::Value(C.IntTy, interp);
110 V.getLL() = res.getSExtValue();
112 V.getULL() = res.getZExtValue();
117 PrintingPolicy Policy(C.getPrintingPolicy());
118 Policy.SuppressTagKeyword =
true;
119 Policy.SuppressUnwrittenScope =
false;
120 Policy.SuppressInitializers =
false;
121 Policy.AnonymousTagLocations =
false;
123 raw_string_ostream out(buf);
124 E->printPretty(out, 0, Policy, 0);
128 interp.evaluate(buf, V);
134 return GetDecl()->getMinRequiredArguments();
138 bool withAccessControl)
140 return fInterp->compileFunction(wrapper_name, wrapper,
false ,
145 PrintingPolicy Policy) {
151 cling::utils::Transform::Config Config;
152 QT = cling::utils::Transform::GetPartiallyDesugaredType(C, QT, Config,
true);
153 QT.getAsStringInternal(type_name, Policy);
157 string &type_name,
EReferenceType &refType,
bool &isPointer,
int indent_level,
164 const FunctionDecl *FD =
GetDecl();
165 ASTContext &C = FD->getASTContext();
166 PrintingPolicy Policy(C.getPrintingPolicy());
168 if (QT->isRecordType() && forArgument) {
172 if (QT->isFunctionPointerType()) {
173 string fp_typedef_name;
177 type_name = nm.str();
178 raw_string_ostream OS(fp_typedef_name);
179 QT.print(OS, Policy, type_name);
182 for (
int i = 0; i < indent_level; ++i) {
185 typedefbuf <<
"typedef " << fp_typedef_name <<
";\n";
187 }
else if (QT->isMemberPointerType()) {
188 string mp_typedef_name;
192 type_name = nm.str();
193 raw_string_ostream OS(mp_typedef_name);
194 QT.print(OS, Policy, type_name);
197 for (
int i = 0; i < indent_level; ++i) {
200 typedefbuf <<
"typedef " << mp_typedef_name <<
";\n";
202 }
else if (QT->isPointerType()) {
204 QT = cast<clang::PointerType>(QT)->getPointeeType();
205 }
else if (QT->isReferenceType()) {
208 QT = cast<ReferenceType>(QT)->getPointeeType();
211 if (QT->isArrayType()) {
212 string ar_typedef_name;
216 type_name = ar.str();
217 raw_string_ostream OS(ar_typedef_name);
218 QT.print(OS, Policy, type_name);
221 for (
int i = 0; i < indent_level; ++i) {
224 typedefbuf <<
"typedef " << ar_typedef_name <<
";\n";
231 ostringstream &callbuf,
const string &class_name,
238 const FunctionDecl *FD =
GetDecl();
240 callbuf <<
"new " << class_name <<
"(";
241 for (
unsigned i = 0U; i <
N; ++i) {
242 const ParmVarDecl *PVD = FD->getParamDecl(i);
243 QualType Ty = PVD->getType();
244 QualType QT = Ty.getCanonicalType();
247 bool isPointer =
false;
249 refType, isPointer, indent_level,
true);
256 for (
int j = 0; j <= indent_level; ++j) {
262 callbuf <<
"(" << type_name.c_str() <<
263 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
265 }
else if (isPointer) {
266 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
269 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
276 ostringstream &callbuf,
const string &class_name,
int indent_level)
283 const FunctionDecl *FD =
GetDecl();
296 bool ShouldCastFunction = !isa<CXXMethodDecl>(FD) &&
N == FD->getNumParams();
297 if (ShouldCastFunction) {
300 callbuf << return_type <<
" (&)";
303 for (
unsigned i = 0U; i <
N; ++i) {
310 for (
int j = 0; j <= indent_level; ++j) {
315 const ParmVarDecl *PVD = FD->getParamDecl(i);
316 QualType Ty = PVD->getType();
317 QualType QT = Ty.getCanonicalType();
318 std::string arg_type;
319 ASTContext &C = FD->getASTContext();
323 if (FD->isVariadic())
331 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
334 callbuf <<
"((const " << class_name <<
"*)obj)->";
336 callbuf <<
"((" << class_name <<
"*)obj)->";
337 }
else if (
const NamedDecl *ND =
341 callbuf << class_name <<
"::";
347 llvm::raw_string_ostream stream(
name);
348 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
352 if (ShouldCastFunction) callbuf <<
")";
355 for (
unsigned i = 0U; i <
N; ++i) {
356 const ParmVarDecl *PVD = FD->getParamDecl(i);
357 QualType Ty = PVD->getType();
358 QualType QT = Ty.getCanonicalType();
361 bool isPointer =
false;
362 collect_type_info(QT, typedefbuf, callbuf, type_name, refType, isPointer, indent_level,
true);
370 for (
int j = 0; j <= indent_level; ++j) {
377 callbuf <<
"(" << type_name.c_str() <<
378 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
380 }
else if (isPointer) {
381 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
386 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
393 ostringstream &buf,
int indent_level)
404 for (
int i = 0; i < indent_level; ++i) {
407 buf <<
"if (ret) {\n";
410 ostringstream typedefbuf;
411 ostringstream callbuf;
415 for (
int i = 0; i < indent_level; ++i) {
418 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
427 for (
int i = 0; i < indent_level; ++i) {
430 callbuf <<
"return;\n";
434 buf << typedefbuf.str() << callbuf.str();
437 for (
int i = 0; i < indent_level; ++i) {
441 for (
int i = 0; i < indent_level; ++i) {
447 ostringstream typedefbuf;
448 ostringstream callbuf;
449 for (
int i = 0; i < indent_level; ++i) {
454 for (
int i = 0; i < indent_level; ++i) {
457 callbuf <<
"return;\n";
458 buf << typedefbuf.str() << callbuf.str();
461 for (
int i = 0; i < indent_level; ++i) {
475 return fMethod->GetDecl()->getDeclContext();
480 const FunctionDecl *FD =
GetDecl();
481 assert(FD &&
"generate_wrapper called without a function decl!");
482 ASTContext &Context = FD->getASTContext();
483 PrintingPolicy Policy(Context.getPrintingPolicy());
489 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(DC)) {
491 QualType QT(TD->getTypeForDecl(), 0);
493 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
495 raw_string_ostream stream(class_name);
496 ND->getNameForDiagnostic(stream, Policy,
true);
503 bool needInstantiation =
false;
504 const FunctionDecl *Definition = 0;
505 if (!FD->isDefined(Definition)) {
506 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
508 case FunctionDecl::TK_NonTemplate: {
518 case FunctionDecl::TK_FunctionTemplate: {
521 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
524 case FunctionDecl::TK_MemberSpecialization: {
531 if (!FD->isTemplateInstantiation()) {
544 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
546 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
547 "instantiation with no pattern!");
550 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
551 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
554 (PTK == FunctionDecl::TK_NonTemplate) ||
557 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
558 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
563 }
else if (!
Pattern->hasBody()) {
564 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
565 "instantiation with no body!");
568 if (FD->isImplicitlyInstantiable()) {
569 needInstantiation =
true;
572 case FunctionDecl::TK_FunctionTemplateSpecialization: {
577 if (!FD->isTemplateInstantiation()) {
590 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
592 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
593 "instantiation with no pattern!");
596 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
597 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
600 (PTK == FunctionDecl::TK_NonTemplate) ||
603 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
604 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
611 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
612 "instantiation with no body!");
615 if (FD->isImplicitlyInstantiable()) {
616 needInstantiation =
true;
619 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
627 if (!FD->isTemplateInstantiation()) {
640 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
642 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
643 "instantiation with no pattern!");
646 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
647 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
650 (PTK == FunctionDecl::TK_NonTemplate) ||
653 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
654 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
661 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
662 "instantiation with no body!");
665 if (FD->isImplicitlyInstantiable()) {
666 needInstantiation =
true;
672 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
694 if (needInstantiation) {
695 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
696 clang::Sema &S =
fInterp->getSema();
698 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
699 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
702 if (!FD->isDefined(Definition)) {
703 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
708 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
710 case FunctionDecl::TK_NonTemplate: {
712 if (Definition->isDeleted()) {
713 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
715 }
else if (Definition->isLateTemplateParsed()) {
716 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
727 case FunctionDecl::TK_FunctionTemplate: {
730 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
733 case FunctionDecl::TK_MemberSpecialization: {
737 if (Definition->isDeleted()) {
738 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
739 "of a specialization!");
741 }
else if (Definition->isLateTemplateParsed()) {
742 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
743 "member function of a specialization!");
753 case FunctionDecl::TK_FunctionTemplateSpecialization: {
758 if (Definition->isDeleted()) {
759 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
760 "template specialization!");
762 }
else if (Definition->isLateTemplateParsed()) {
763 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
764 "function template specialization!");
774 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
782 if (Definition->isDeleted()) {
783 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
784 "template specialization!");
786 }
else if (Definition->isLateTemplateParsed()) {
787 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
788 "dependent function template specialization!");
801 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
807 unsigned num_params = FD->getNumParams();
819 wrapper_name = buf.str();
825 int indent_level = 0;
827 buf <<
"#pragma clang diagnostic push\n"
828 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
829 "__attribute__((used)) "
830 "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
831 "extern \"C\" void ";
833 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
836 if (min_args == num_params) {
842 for (
unsigned N = min_args;
N <= num_params; ++
N) {
843 for (
int i = 0; i < indent_level; ++i) {
846 buf <<
"if (nargs == " <<
N <<
") {\n";
850 for (
int i = 0; i < indent_level; ++i) {
858 "#pragma clang diagnostic pop";
864 ostringstream &buf,
int indent_level)
875 const FunctionDecl *FD =
GetDecl();
876 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
878 auto SpecMemKind =
fInterp->getSema().getSpecialMember(CD);
879 if ((
N == 0 && SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
881 (SpecMemKind == clang::Sema::CXXCopyConstructor || SpecMemKind == clang::Sema::CXXMoveConstructor))) {
892 QualType QT = FD->getReturnType().getCanonicalType();
893 if (QT->isVoidType()) {
894 ostringstream typedefbuf;
895 ostringstream callbuf;
896 for (
int i = 0; i < indent_level; ++i) {
899 make_narg_call(
"void",
N, typedefbuf, callbuf, class_name, indent_level);
901 for (
int i = 0; i < indent_level; ++i) {
904 callbuf <<
"return;\n";
905 buf << typedefbuf.str() << callbuf.str();
907 for (
int i = 0; i < indent_level; ++i) {
913 bool isPointer =
false;
915 buf <<
"if (ret) {\n";
918 ostringstream typedefbuf;
919 ostringstream callbuf;
923 for (
int i = 0; i < indent_level; ++i) {
926 callbuf <<
"new (ret) ";
928 refType, isPointer, indent_level,
false);
932 callbuf <<
"(" << type_name.c_str();
936 }
else if (isPointer) {
945 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
950 for (
int i = 0; i < indent_level; ++i) {
953 callbuf <<
"return;\n";
957 buf << typedefbuf.str() << callbuf.str();
960 for (
int i = 0; i < indent_level; ++i) {
964 for (
int i = 0; i < indent_level; ++i) {
970 ostringstream typedefbuf;
971 ostringstream callbuf;
972 for (
int i = 0; i < indent_level; ++i) {
975 callbuf <<
"(void)(";
976 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
978 for (
int i = 0; i < indent_level; ++i) {
981 callbuf <<
"return;\n";
982 buf << typedefbuf.str() << callbuf.str();
985 for (
int i = 0; i < indent_level; ++i) {
1010 ::Error(
"TClingCallFunc::make_wrapper",
1011 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1088 ASTContext &Context = info->
GetDecl()->getASTContext();
1089 PrintingPolicy Policy(Context.getPrintingPolicy());
1090 Policy.SuppressTagKeyword =
true;
1091 Policy.SuppressUnwrittenScope =
true;
1096 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1098 QualType QT(TD->getTypeForDecl(), 0);
1100 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1102 raw_string_ostream stream(class_name);
1103 ND->getNameForDiagnostic(stream, Policy,
true);
1111 string wrapper_name;
1120 wrapper_name = buf.str();
1125 constr_arg = string(
"((") + type_name +
"*)nullptr)";
1127 constr_arg = string(
"(*((") + type_name +
"*)arena))";
1132 int indent_level = 0;
1134 buf <<
"__attribute__((used)) ";
1135 buf <<
"extern \"C\" void ";
1136 buf << wrapper_name;
1137 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1148 indent(buf, ++indent_level);
1149 buf <<
"if (!arena) {\n";
1150 indent(buf, ++indent_level);
1151 buf <<
"if (!nary) {\n";
1152 indent(buf, ++indent_level);
1153 buf <<
"*ret = new " << class_name << constr_arg <<
";\n";
1154 indent(buf, --indent_level);
1156 indent(buf, indent_level);
1158 indent(buf, ++indent_level);
1159 if (constr_arg.empty()) {
1160 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1162 buf <<
"char *buf = (char *) malloc(nary * sizeof(" << class_name <<
"));\n";
1163 indent(buf, indent_level);
1164 buf <<
"for (int k=0;k<nary;++k)\n";
1165 indent(buf, ++indent_level);
1166 buf <<
"new (buf + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1167 indent(buf, --indent_level);
1168 buf <<
"*ret = buf;\n";
1170 indent(buf, --indent_level);
1172 indent(buf, --indent_level);
1182 indent(buf, indent_level);
1184 indent(buf, ++indent_level);
1185 buf <<
"if (!nary) {\n";
1186 indent(buf, ++indent_level);
1187 buf <<
"*ret = new (arena) " << class_name << constr_arg <<
";\n";
1188 indent(buf, --indent_level);
1190 indent(buf, indent_level);
1192 indent(buf, ++indent_level);
1193 if (constr_arg.empty()) {
1194 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1196 buf <<
"for (int k=0;k<nary;++k)\n";
1197 indent(buf, ++indent_level);
1198 buf <<
"new ((char *) arena + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1199 indent(buf, --indent_level);
1200 buf <<
"*ret = arena;\n";
1202 indent(buf, --indent_level);
1204 indent(buf, --indent_level);
1210 string wrapper(buf.str());
1220 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1221 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1257 ASTContext &Context = info->
GetDecl()->getASTContext();
1258 PrintingPolicy Policy(Context.getPrintingPolicy());
1259 Policy.SuppressTagKeyword =
true;
1260 Policy.SuppressUnwrittenScope =
true;
1265 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1267 QualType QT(TD->getTypeForDecl(), 0);
1269 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1271 raw_string_ostream stream(class_name);
1272 ND->getNameForDiagnostic(stream, Policy,
true);
1278 string wrapper_name;
1287 wrapper_name = buf.str();
1292 int indent_level = 0;
1294 buf <<
"__attribute__((used)) ";
1295 buf <<
"extern \"C\" void ";
1296 buf << wrapper_name;
1297 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1308 indent(buf, indent_level);
1309 buf <<
"if (withFree) {\n";
1311 indent(buf, indent_level);
1312 buf <<
"if (!nary) {\n";
1314 indent(buf, indent_level);
1315 buf <<
"delete (" << class_name <<
"*) obj;\n";
1317 indent(buf, indent_level);
1319 indent(buf, indent_level);
1322 indent(buf, indent_level);
1323 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1325 indent(buf, indent_level);
1328 indent(buf, indent_level);
1341 indent(buf, indent_level);
1344 indent(buf, indent_level);
1345 buf <<
"typedef " << class_name <<
" Nm;\n";
1346 buf <<
"if (!nary) {\n";
1348 indent(buf, indent_level);
1349 buf <<
"((Nm*)obj)->~Nm();\n";
1351 indent(buf, indent_level);
1353 indent(buf, indent_level);
1356 indent(buf, indent_level);
1359 indent(buf, indent_level);
1360 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1362 indent(buf, indent_level);
1363 buf <<
"} while (nary);\n";
1365 indent(buf, indent_level);
1368 indent(buf, indent_level);
1374 string wrapper(buf.str());
1384 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1385 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1422 SmallVector<ValHolder, 8> vh_ary;
1423 SmallVector<void *, 8> vp_ary;
1425 unsigned num_args =
fArgVals.size();
1429 const FunctionDecl *FD =
GetDecl();
1436 unsigned num_params = FD->getNumParams();
1439 ::Error(
"TClingCallFunc::exec",
1440 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1445 if (address == 0 && dyn_cast<CXXMethodDecl>(FD)
1446 && !(dyn_cast<CXXMethodDecl>(FD))->isStatic()
1447 && !dyn_cast<CXXConstructorDecl>(FD)) {
1448 ::Error(
"TClingCallFunc::exec",
1449 "The method %s is called without an object.",
1453 vh_ary.reserve(num_args);
1454 vp_ary.reserve(num_args);
1455 for (
unsigned i = 0U; i < num_args; ++i) {
1457 if (i < num_params) {
1458 const ParmVarDecl *PVD = FD->getParamDecl(i);
1459 Ty = PVD->getType();
1463 QualType QT = Ty.getCanonicalType();
1464 if (
const BuiltinType *BT = dyn_cast<BuiltinType>(QT.getTypePtr())) {
1466 switch (BT->getKind()) {
1468 case BuiltinType::Bool: vh.
u.
b =
fArgVals[i].simplisticCastAs<
bool>();
1470 case BuiltinType::Char_U: vh.
u.
c =
fArgVals[i].simplisticCastAs<
char>();
1472 case BuiltinType::UChar: vh.
u.
uc =
fArgVals[i].simplisticCastAs<
unsigned char>();
1474 case BuiltinType::WChar_U: vh.
u.
wc =
fArgVals[i].simplisticCastAs<
wchar_t>();
1476 case BuiltinType::Char16:
1478 case BuiltinType::Char32:
1480 case BuiltinType::UShort: vh.
u.
us =
fArgVals[i].simplisticCastAs<
unsigned short>();
1482 case BuiltinType::UInt: vh.
u.
ui =
fArgVals[i].simplisticCastAs<
unsigned int>();
1484 case BuiltinType::ULong: vh.
u.
ul =
fArgVals[i].simplisticCastAs<
unsigned long>();
1486 case BuiltinType::ULongLong: vh.
u.
ull =
fArgVals[i].simplisticCastAs<
unsigned long long>();
1489 case BuiltinType::Char_S: vh.
u.
c =
fArgVals[i].simplisticCastAs<
char>();
1491 case BuiltinType::SChar: vh.
u.
sc =
fArgVals[i].simplisticCastAs<
signed char>();
1493 case BuiltinType::WChar_S: vh.
u.
wc =
fArgVals[i].simplisticCastAs<
wchar_t>();
1495 case BuiltinType::Short: vh.
u.
s =
fArgVals[i].simplisticCastAs<
short>();
1497 case BuiltinType::Int: vh.
u.
i =
fArgVals[i].simplisticCastAs<
int>();
1499 case BuiltinType::Long: vh.
u.
l =
fArgVals[i].simplisticCastAs<
long>();
1501 case BuiltinType::LongLong: vh.
u.
ll =
fArgVals[i].simplisticCastAs<
long long>();
1503 case BuiltinType::Float: vh.
u.
flt =
fArgVals[i].simplisticCastAs<
float>();
1505 case BuiltinType::Double: vh.
u.
dbl =
fArgVals[i].simplisticCastAs<
double>();
1507 case BuiltinType::LongDouble: vh.
u.
ldbl =
fArgVals[i].simplisticCastAs<
long double>();
1509 case BuiltinType::NullPtr: vh.
u.
vp =
fArgVals[i].getPtr();
1514 ::Error(
"TClingCallFunc::exec(void*)",
1515 "Unhandled builtin type '%s'",
1516 BT->getTypeClassName());
1520 vh_ary.push_back(vh);
1521 vp_ary.push_back(&vh_ary.back());
1522 }
else if (QT->isReferenceType() || QT->isRecordType()) {
1525 vp_ary.push_back((
void *)
fArgVals[i].simplisticCastAs<unsigned long long>());
1526 }
else if (QT->isPointerType() || QT->isArrayType() || QT->isMemberPointerType()) {
1528 vh.
u.
vp = (
void *)
fArgVals[i].simplisticCastAs<unsigned long long>();
1529 vh_ary.push_back(vh);
1530 vp_ary.push_back(&vh_ary.back());
1531 }
else if (QT->isEnumeralType()) {
1535 vh.
u.
i =
fArgVals[i].simplisticCastAs<
int>();
1536 vh_ary.push_back(vh);
1537 vp_ary.push_back(&vh_ary.back());
1539 ::Error(
"TClingCallFunc::exec(void*)",
1540 "Invalid type '%s'", QT->getTypeClassName());
1546 (*fWrapper)(address, (
int)num_args, (
void **)vp_ary.data(), ret);
1549template <
typename T>
1553 exec(address, &ret);
1557template <
typename T>
1561 exec(address, &ret);
1562 val->getULL() = ret;
1569 ret = cling::Value::Create<T>(QT.getAsOpaquePtr(), *
fInterp);
1570 static_assert(std::is_integral<T>::value,
"Must be called with integral T");
1571 if (std::is_signed<T>::value)
1572 return [
this](
void* address, cling::Value& ret) { execWithLL<T>(address, &ret); };
1574 return [
this](
void* address, cling::Value& ret) { execWithULL<T>(address, &ret); };
1580 switch (BT->getKind()) {
1581 case BuiltinType::Void: {
1582 ret = cling::Value::Create<void>(QT.getAsOpaquePtr(), *
fInterp);
1583 return [
this](
void* address, cling::Value& ret) {
exec(address, 0); };
1590 case BuiltinType::Bool:
1591 return InitRetAndExecIntegral<bool>(QT, ret);
1593 case BuiltinType::Char_U:
1594 case BuiltinType::UChar:
1595 return InitRetAndExecIntegral<char>(QT, ret);
1597 case BuiltinType::WChar_U:
1598 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1600 case BuiltinType::Char16:
1601 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1602 "Invalid type 'char16_t'!");
1605 case BuiltinType::Char32:
1606 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1607 "Invalid type 'char32_t'!");
1610 case BuiltinType::UShort:
1611 return InitRetAndExecIntegral<unsigned short>(QT, ret);
1613 case BuiltinType::UInt:
1614 return InitRetAndExecIntegral<unsigned int>(QT, ret);
1616 case BuiltinType::ULong:
1617 return InitRetAndExecIntegral<unsigned long>(QT, ret);
1619 case BuiltinType::ULongLong:
1620 return InitRetAndExecIntegral<unsigned long long>(QT, ret);
1622 case BuiltinType::UInt128: {
1623 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1624 "Invalid type '__uint128_t'!");
1632 case BuiltinType::Char_S:
1633 case BuiltinType::SChar:
1634 return InitRetAndExecIntegral<signed char>(QT, ret);
1636 case BuiltinType::WChar_S:
1640 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1642 case BuiltinType::Short:
1643 return InitRetAndExecIntegral<short>(QT, ret);
1645 case BuiltinType::Int:
1646 return InitRetAndExecIntegral<int>(QT, ret);
1648 case BuiltinType::Long:
1649 return InitRetAndExecIntegral<long>(QT, ret);
1651 case BuiltinType::LongLong:
1652 return InitRetAndExecIntegral<long long>(QT, ret);
1654 case BuiltinType::Int128:
1655 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1656 "Invalid type '__int128_t'!");
1659 case BuiltinType::Half:
1661 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1662 "Invalid type 'Half'!");
1665 case BuiltinType::Float: {
1666 ret = cling::Value::Create<float>(QT.getAsOpaquePtr(), *
fInterp);
1667 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getFloat()); };
1670 case BuiltinType::Double: {
1671 ret = cling::Value::Create<double>(QT.getAsOpaquePtr(), *
fInterp);
1672 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getDouble()); };
1675 case BuiltinType::LongDouble: {
1676 ret = cling::Value::Create<long double>(QT.getAsOpaquePtr(), *
fInterp);
1677 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getLongDouble()); };
1683 case BuiltinType::NullPtr:
1685 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1686 "Invalid type 'nullptr'!");
1698 if (QT->isReferenceType()) {
1699 ret = cling::Value(QT, *
fInterp);
1700 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
1701 }
else if (QT->isMemberPointerType()) {
1702 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
1703 if (MPT && MPT->isMemberDataPointer()) {
1709 ret = cling::Value(QT, *
fInterp);
1710 return [
this](
void* address, cling::Value& ret) {
exec(address, ret.getPtr()); };
1713 ret = cling::Value(QT, *
fInterp);
1714 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
1715 }
else if (QT->isPointerType() || QT->isArrayType()) {
1717 ret = cling::Value::Create<void*>(QT.getAsOpaquePtr(), *
fInterp);
1718 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
1719 }
else if (QT->isRecordType()) {
1720 ret = cling::Value(QT, *
fInterp);
1721 return [
this](
void* address, cling::Value& ret) {
exec(address, ret.getPtr()); };
1722 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
1726 ret = cling::Value(QT, *
fInterp);
1727 return [
this](
void* address, cling::Value& ret) { execWithLL<int>(address, &ret); };
1728 }
else if (
const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
1731 ::Error(
"TClingCallFunc::exec_with_valref_return",
1732 "Unrecognized return type!");
1739 if (llvm::isa<CXXConstructorDecl>(FD)) {
1740 ASTContext &Context = FD->getASTContext();
1742 QualType ClassTy(TD->getTypeForDecl(), 0);
1743 QualType QT = Context.getLValueReferenceType(ClassTy);
1744 ret = cling::Value(QT, *
fInterp);
1746 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
1748 QualType QT = FD->getReturnType().getCanonicalType();
1759 std::function<void(
void*, cling::Value&)> execFunc;
1768 execFunc(address, *ret);
1776 SmallVector<Expr *, 4> exprs;
1777 fInterp->getLookupHelper().findArgList(ArgList, exprs,
1778 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1779 : cling::LookupHelper::NoDiagnostics);
1780 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
1781 E = exprs.end();
I != E; ++
I) {
1784 if (!val.isValid()) {
1786 ::Error(
"TClingCallFunc::EvaluateArgList",
1787 "Bad expression in parameter %d of '%s'!",
1788 (
int)(
I - exprs.begin()),
1800 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
1801 "Called with no wrapper, not implemented!");
1808 cling::Value *val =
reinterpret_cast<cling::Value *
>(interpVal->
GetValAddr());
1812template <
typename T>
1817 ::Error(
"TClingCallFunc::ExecT",
1818 "Called with no wrapper, not implemented!");
1827 if (ret.needsManagedAllocation())
1829 return ret.simplisticCastAs<T>();
1834 return ExecT<Longptr_t>(address);
1839 return ExecT<long long>(address);
1844 return ExecT<double>(address);
1848 int nargs ,
void *ret)
1852 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
1853 "Called with no wrapper, not implemented!");
1856 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
1863 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
1864 "Called with no wrapper, not implemented!");
1872 const std::string &type_name,
1873 void *address ,
unsigned long nary )
1876 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
1899 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
1900 "Called with no wrapper, not implemented!");
1904 (*wrapper)(&obj, address, nary);
1909 unsigned long nary ,
bool withFree )
1912 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
1919 const Decl *D = info->
GetDecl();
1928 ::Error(
"TClingCallFunc::ExecDestructor",
1929 "Called with no wrapper, not implemented!");
1932 (*wrapper)(address, nary, withFree);
1973 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
1995 ::Error(
"TClingCallFunc::IFacePtr(kind)",
1996 "Attempt to get interface while invalid.");
2005 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2024 const ASTContext &C =
fInterp->getCI()->getASTContext();
2031 const ASTContext &C =
fInterp->getCI()->getASTContext();
2038 const ASTContext &C =
fInterp->getCI()->getASTContext();
2040 fArgVals.back().getFloat() = param;
2045 const ASTContext &C =
fInterp->getCI()->getASTContext();
2047 fArgVals.back().getDouble() = param;
2052 const ASTContext &C =
fInterp->getCI()->getASTContext();
2059 const ASTContext &C =
fInterp->getCI()->getASTContext();
2067 for (
int i = 0; i < nparam; ++i) {
2081 SetFunc(info, method, arglist,
false, poffset);
2093 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
2096 if (!strcmp(arglist,
")")) {
2138 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2150 const llvm::SmallVectorImpl<clang::QualType> &
proto,
Longptr_t *poffset,
2157 const llvm::SmallVectorImpl<clang::QualType> &
proto,
2167 ::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)
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)
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()
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 ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
Longptr_t ExecInt(void *address)
void execWithLL(void *address, cling::Value *val)
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 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)
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, Longptr_t *poffset)
const clang::FunctionDecl * GetDecl()
void execWithULL(void *address, cling::Value *val)
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
ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret)
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)
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, 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
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.