36#include "cling/Interpreter/CompilationOptions.h"
37#include "cling/Interpreter/Interpreter.h"
38#include "cling/Interpreter/LookupHelper.h"
39#include "cling/Interpreter/Transaction.h"
40#include "cling/Interpreter/Value.h"
41#include "cling/Utils/AST.h"
43#include "clang/AST/ASTContext.h"
44#include "clang/AST/Decl.h"
45#include "clang/AST/DeclCXX.h"
46#include "clang/AST/GlobalDecl.h"
47#include "clang/AST/PrettyPrinter.h"
48#include "clang/AST/RecordLayout.h"
49#include "clang/AST/Type.h"
50#include "clang/Frontend/CompilerInstance.h"
51#include "clang/Lex/Preprocessor.h"
52#include "clang/Sema/Sema.h"
53#include "clang/Sema/Lookup.h"
55#include "llvm/ADT/APInt.h"
56#include "llvm/ExecutionEngine/ExecutionEngine.h"
57#include "llvm/ExecutionEngine/GenericValue.h"
58#include "llvm/Support/Casting.h"
59#include "llvm/Support/raw_ostream.h"
60#include "llvm/IR/LLVMContext.h"
61#include "llvm/IR/DerivedTypes.h"
62#include "llvm/IR/Function.h"
63#include "llvm/IR/GlobalValue.h"
64#include "llvm/IR/Module.h"
65#include "llvm/IR/Type.h"
67#include "clang/Sema/SemaInternal.h"
89indent(ostringstream &buf,
int indent_level)
91 for (
int i = 0; i < indent_level; ++i) {
101 ASTContext &
C = interp.getCI()->getASTContext();
103 if (
E->EvaluateAsInt(res,
C, Expr::SE_NoSideEffects)) {
109 V.getLL() = res.getSExtValue();
111 V.getULL() = res.getZExtValue();
116 PrintingPolicy Policy(
C.getPrintingPolicy());
117 Policy.SuppressTagKeyword =
true;
118 Policy.SuppressUnwrittenScope =
false;
119 Policy.SuppressInitializers =
false;
120 Policy.AnonymousTagLocations =
false;
122 raw_string_ostream out(buf);
123 E->printPretty(out, 0, Policy, 0);
127 interp.evaluate(buf, V);
131 template <
typename returnType>
134 QualType QT = val.getType().getCanonicalType();
135 if (
const BuiltinType *BT =
136 dyn_cast<BuiltinType>(&*QT)) {
144 switch (BT->getKind()) {
145 case BuiltinType::Void:
147 return (returnType) 0;
153 case BuiltinType::Char_U:
155 return (returnType) val.getULL();
158 case BuiltinType::WChar_U:
162 return (returnType)(wchar_t) val.getULL();
165 case BuiltinType::Char16:
166 case BuiltinType::Char32:
170 case BuiltinType::ULongLong:
171 return (returnType) val.getULL();
174 case BuiltinType::UInt128:
181 case BuiltinType::Char_S:
182 case BuiltinType::SChar:
183 return (returnType) val.getLL();
186 case BuiltinType::WChar_S:
190 return (returnType)(wchar_t) val.getLL();
196 case BuiltinType::LongLong:
197 return (returnType) val.getLL();
200 case BuiltinType::Int128:
203 case BuiltinType::Half:
208 return (returnType) val.getFloat();
211 return (returnType) val.getDouble();
213 case BuiltinType::LongDouble:
214 return (returnType) val.getLongDouble();
217 case BuiltinType::NullPtr:
218 return (returnType) 0;
225 if (QT->isPointerType() || QT->isArrayType() || QT->isRecordType() ||
226 QT->isReferenceType()) {
227 return (returnType)(
long) val.getPtr();
229 if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
230 if (ET->getDecl()->getIntegerType()->hasSignedIntegerRepresentation())
231 return (returnType) val.getLL();
233 return (returnType) val.getULL();
235 if (QT->isMemberPointerType()) {
236 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
237 if (MPT->isMemberDataPointer()) {
238 return (returnType)(ptrdiff_t)val.getPtr();
240 return (returnType)(
long) val.getPtr();
242 ::Error(
"TClingCallFunc::sv_to",
"Invalid Type!");
250 return sv_to<long long>(val);
253 unsigned long long sv_to_ulong_long(
const cling::Value &val)
255 return sv_to<unsigned long long>(val);
263 return GetDecl()->getMinRequiredArguments();
267 bool withAccessControl)
269 return fInterp->compileFunction(wrapper_name, wrapper,
false ,
274 string &type_name,
EReferenceType &refType,
bool &isPointer,
int indent_level,
281 const FunctionDecl *FD =
GetDecl();
282 PrintingPolicy Policy(FD->getASTContext().getPrintingPolicy());
284 if (QT->isRecordType() && forArgument) {
288 if (QT->isFunctionPointerType()) {
289 string fp_typedef_name;
293 type_name =
nm.str();
294 raw_string_ostream OS(fp_typedef_name);
295 QT.print(OS, Policy, type_name);
298 for (
int i = 0; i < indent_level; ++i) {
301 typedefbuf <<
"typedef " << fp_typedef_name <<
";\n";
303 }
else if (QT->isMemberPointerType()) {
304 string mp_typedef_name;
308 type_name =
nm.str();
309 raw_string_ostream OS(mp_typedef_name);
310 QT.print(OS, Policy, type_name);
313 for (
int i = 0; i < indent_level; ++i) {
316 typedefbuf <<
"typedef " << mp_typedef_name <<
";\n";
318 }
else if (QT->isPointerType()) {
320 QT = cast<clang::PointerType>(QT)->getPointeeType();
321 }
else if (QT->isReferenceType()) {
324 QT = cast<ReferenceType>(QT)->getPointeeType();
327 if (QT->isArrayType()) {
328 string ar_typedef_name;
332 type_name = ar.str();
333 raw_string_ostream OS(ar_typedef_name);
334 QT.print(OS, Policy, type_name);
337 for (
int i = 0; i < indent_level; ++i) {
340 typedefbuf <<
"typedef " << ar_typedef_name <<
";\n";
347 ostringstream &callbuf,
const string &class_name,
354 const FunctionDecl *FD =
GetDecl();
356 callbuf <<
"new " << class_name <<
"(";
357 for (
unsigned i = 0U; i <
N; ++i) {
358 const ParmVarDecl *PVD = FD->getParamDecl(i);
359 QualType Ty = PVD->getType();
360 QualType QT = Ty.getCanonicalType();
363 bool isPointer =
false;
365 refType, isPointer, indent_level,
true);
372 for (
int j = 0; j <= indent_level; ++j) {
378 callbuf <<
"(" << type_name.c_str() <<
379 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
381 }
else if (isPointer) {
382 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
385 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
392 ostringstream &callbuf,
const string &class_name,
int indent_level)
399 const FunctionDecl *FD =
GetDecl();
412 bool ShouldCastFunction = !isa<CXXMethodDecl>(FD) &&
N == FD->getNumParams();
413 if (ShouldCastFunction) {
416 callbuf << return_type <<
" (&)";
419 for (
unsigned i = 0U; i <
N; ++i) {
426 for (
int j = 0; j <= indent_level; ++j) {
431 const ParmVarDecl *PVD = FD->getParamDecl(i);
432 QualType Ty = PVD->getType();
433 QualType QT = Ty.getCanonicalType();
434 std::string arg_type;
438 if (FD->isVariadic())
446 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
449 callbuf <<
"((const " << class_name <<
"*)obj)->";
451 callbuf <<
"((" << class_name <<
"*)obj)->";
452 }
else if (
const NamedDecl *ND =
456 callbuf << class_name <<
"::";
462 llvm::raw_string_ostream stream(
name);
463 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
467 if (ShouldCastFunction) callbuf <<
")";
470 for (
unsigned i = 0U; i <
N; ++i) {
471 const ParmVarDecl *PVD = FD->getParamDecl(i);
472 QualType Ty = PVD->getType();
473 QualType QT = Ty.getCanonicalType();
476 bool isPointer =
false;
477 collect_type_info(QT, typedefbuf, callbuf, type_name, refType, isPointer, indent_level,
true);
485 for (
int j = 0; j <= indent_level; ++j) {
492 callbuf <<
"(" << type_name.c_str() <<
493 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
495 }
else if (isPointer) {
496 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
501 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
508 ostringstream &buf,
int indent_level)
519 for (
int i = 0; i < indent_level; ++i) {
522 buf <<
"if (ret) {\n";
525 ostringstream typedefbuf;
526 ostringstream callbuf;
530 for (
int i = 0; i < indent_level; ++i) {
533 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
542 for (
int i = 0; i < indent_level; ++i) {
545 callbuf <<
"return;\n";
549 buf << typedefbuf.str() << callbuf.str();
552 for (
int i = 0; i < indent_level; ++i) {
556 for (
int i = 0; i < indent_level; ++i) {
562 ostringstream typedefbuf;
563 ostringstream callbuf;
564 for (
int i = 0; i < indent_level; ++i) {
569 for (
int i = 0; i < indent_level; ++i) {
572 callbuf <<
"return;\n";
573 buf << typedefbuf.str() << callbuf.str();
576 for (
int i = 0; i < indent_level; ++i) {
590 return fMethod->GetDecl()->getDeclContext();
595 const FunctionDecl *FD =
GetDecl();
596 assert(FD &&
"generate_wrapper called without a function decl!");
597 ASTContext &Context = FD->getASTContext();
598 PrintingPolicy Policy(Context.getPrintingPolicy());
604 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(DC)) {
606 QualType QT(TD->getTypeForDecl(), 0);
608 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
610 raw_string_ostream stream(class_name);
611 ND->getNameForDiagnostic(stream, Policy,
true);
618 bool needInstantiation =
false;
619 const FunctionDecl *Definition = 0;
620 if (!FD->isDefined(Definition)) {
621 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
623 case FunctionDecl::TK_NonTemplate: {
633 case FunctionDecl::TK_FunctionTemplate: {
636 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
639 case FunctionDecl::TK_MemberSpecialization: {
646 if (!FD->isTemplateInstantiation()) {
659 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
661 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
662 "instantiation with no pattern!");
665 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
666 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
669 (PTK == FunctionDecl::TK_NonTemplate) ||
672 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
673 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
678 }
else if (!
Pattern->hasBody()) {
679 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
680 "instantiation with no body!");
683 if (FD->isImplicitlyInstantiable()) {
684 needInstantiation =
true;
687 case FunctionDecl::TK_FunctionTemplateSpecialization: {
692 if (!FD->isTemplateInstantiation()) {
705 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
707 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
708 "instantiation with no pattern!");
711 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
712 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
715 (PTK == FunctionDecl::TK_NonTemplate) ||
718 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
719 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
726 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
727 "instantiation with no body!");
730 if (FD->isImplicitlyInstantiable()) {
731 needInstantiation =
true;
734 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
742 if (!FD->isTemplateInstantiation()) {
755 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
757 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
758 "instantiation with no pattern!");
761 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
762 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
765 (PTK == FunctionDecl::TK_NonTemplate) ||
768 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
769 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
776 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
777 "instantiation with no body!");
780 if (FD->isImplicitlyInstantiable()) {
781 needInstantiation =
true;
787 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
809 if (needInstantiation) {
810 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
811 clang::Sema &
S =
fInterp->getSema();
813 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
814 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
817 if (!FD->isDefined(Definition)) {
818 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
823 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
825 case FunctionDecl::TK_NonTemplate: {
827 if (Definition->isDeleted()) {
828 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
830 }
else if (Definition->isLateTemplateParsed()) {
831 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
842 case FunctionDecl::TK_FunctionTemplate: {
845 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
848 case FunctionDecl::TK_MemberSpecialization: {
852 if (Definition->isDeleted()) {
853 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
854 "of a specialization!");
856 }
else if (Definition->isLateTemplateParsed()) {
857 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
858 "member function of a specialization!");
868 case FunctionDecl::TK_FunctionTemplateSpecialization: {
873 if (Definition->isDeleted()) {
874 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
875 "template specialization!");
877 }
else if (Definition->isLateTemplateParsed()) {
878 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
879 "function template specialization!");
889 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
897 if (Definition->isDeleted()) {
898 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
899 "template specialization!");
901 }
else if (Definition->isLateTemplateParsed()) {
902 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
903 "dependent function template specialization!");
916 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
922 unsigned num_params = FD->getNumParams();
934 wrapper_name = buf.str();
940 int indent_level = 0;
942 buf <<
"#pragma clang diagnostic push\n"
943 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
944 "__attribute__((used)) "
945 "extern \"C\" void ";
947 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
950 if (min_args == num_params) {
956 for (
unsigned N = min_args;
N <= num_params; ++
N) {
957 for (
int i = 0; i < indent_level; ++i) {
960 buf <<
"if (nargs == " <<
N <<
") {\n";
964 for (
int i = 0; i < indent_level; ++i) {
972 "#pragma clang diagnostic pop";
978 ostringstream &buf,
int indent_level)
989 const FunctionDecl *FD =
GetDecl();
990 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
992 auto SpecMemKind =
fInterp->getSema().getSpecialMember(CD);
993 if ((
N == 0 && SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
995 (SpecMemKind == clang::Sema::CXXCopyConstructor || SpecMemKind == clang::Sema::CXXMoveConstructor))) {
1006 QualType QT = FD->getReturnType().getCanonicalType();
1007 if (QT->isVoidType()) {
1008 ostringstream typedefbuf;
1009 ostringstream callbuf;
1010 for (
int i = 0; i < indent_level; ++i) {
1013 make_narg_call(
"void",
N, typedefbuf, callbuf, class_name, indent_level);
1015 for (
int i = 0; i < indent_level; ++i) {
1018 callbuf <<
"return;\n";
1019 buf << typedefbuf.str() << callbuf.str();
1021 for (
int i = 0; i < indent_level; ++i) {
1027 bool isPointer =
false;
1029 buf <<
"if (ret) {\n";
1032 ostringstream typedefbuf;
1033 ostringstream callbuf;
1037 for (
int i = 0; i < indent_level; ++i) {
1040 callbuf <<
"new (ret) ";
1042 refType, isPointer, indent_level,
false);
1046 callbuf <<
"(" << type_name.c_str();
1050 }
else if (isPointer) {
1059 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1064 for (
int i = 0; i < indent_level; ++i) {
1067 callbuf <<
"return;\n";
1071 buf << typedefbuf.str() << callbuf.str();
1074 for (
int i = 0; i < indent_level; ++i) {
1078 for (
int i = 0; i < indent_level; ++i) {
1084 ostringstream typedefbuf;
1085 ostringstream callbuf;
1086 for (
int i = 0; i < indent_level; ++i) {
1089 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1091 for (
int i = 0; i < indent_level; ++i) {
1094 callbuf <<
"return;\n";
1095 buf << typedefbuf.str() << callbuf.str();
1098 for (
int i = 0; i < indent_level; ++i) {
1110 string wrapper_name;
1123 ::Error(
"TClingCallFunc::make_wrapper",
1124 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1201 ASTContext &Context = info->
GetDecl()->getASTContext();
1202 PrintingPolicy Policy(Context.getPrintingPolicy());
1203 Policy.SuppressTagKeyword =
true;
1204 Policy.SuppressUnwrittenScope =
true;
1209 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1211 QualType QT(TD->getTypeForDecl(), 0);
1213 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1215 raw_string_ostream stream(class_name);
1216 ND->getNameForDiagnostic(stream, Policy,
true);
1224 string wrapper_name;
1233 wrapper_name = buf.str();
1238 constr_arg = string(
"((") + type_name +
"*)nullptr)";
1240 constr_arg = string(
"(*((") + type_name +
"*)arena))";
1245 int indent_level = 0;
1247 buf <<
"__attribute__((used)) ";
1248 buf <<
"extern \"C\" void ";
1249 buf << wrapper_name;
1250 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1261 indent(buf, ++indent_level);
1262 buf <<
"if (!arena) {\n";
1263 indent(buf, ++indent_level);
1264 buf <<
"if (!nary) {\n";
1265 indent(buf, ++indent_level);
1266 buf <<
"*ret = new " << class_name << constr_arg <<
";\n";
1267 indent(buf, --indent_level);
1269 indent(buf, indent_level);
1271 indent(buf, ++indent_level);
1272 if (constr_arg.empty()) {
1273 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1275 buf <<
"char *buf = (char *) malloc(nary * sizeof(" << class_name <<
"));\n";
1276 indent(buf, indent_level);
1277 buf <<
"for (int k=0;k<nary;++k)\n";
1278 indent(buf, ++indent_level);
1279 buf <<
"new (buf + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1280 indent(buf, --indent_level);
1281 buf <<
"*ret = buf;\n";
1283 indent(buf, --indent_level);
1285 indent(buf, --indent_level);
1295 indent(buf, indent_level);
1297 indent(buf, ++indent_level);
1298 buf <<
"if (!nary) {\n";
1299 indent(buf, ++indent_level);
1300 buf <<
"*ret = new (arena) " << class_name << constr_arg <<
";\n";
1301 indent(buf, --indent_level);
1303 indent(buf, indent_level);
1305 indent(buf, ++indent_level);
1306 if (constr_arg.empty()) {
1307 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1309 buf <<
"for (int k=0;k<nary;++k)\n";
1310 indent(buf, ++indent_level);
1311 buf <<
"new ((char *) arena + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1312 indent(buf, --indent_level);
1313 buf <<
"*ret = arena;\n";
1315 indent(buf, --indent_level);
1317 indent(buf, --indent_level);
1323 string wrapper(buf.str());
1333 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1334 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1370 ASTContext &Context = info->
GetDecl()->getASTContext();
1371 PrintingPolicy Policy(Context.getPrintingPolicy());
1372 Policy.SuppressTagKeyword =
true;
1373 Policy.SuppressUnwrittenScope =
true;
1378 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1380 QualType QT(TD->getTypeForDecl(), 0);
1382 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1384 raw_string_ostream stream(class_name);
1385 ND->getNameForDiagnostic(stream, Policy,
true);
1391 string wrapper_name;
1400 wrapper_name = buf.str();
1405 int indent_level = 0;
1407 buf <<
"__attribute__((used)) ";
1408 buf <<
"extern \"C\" void ";
1409 buf << wrapper_name;
1410 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1421 indent(buf, indent_level);
1422 buf <<
"if (withFree) {\n";
1424 indent(buf, indent_level);
1425 buf <<
"if (!nary) {\n";
1427 indent(buf, indent_level);
1428 buf <<
"delete (" << class_name <<
"*) obj;\n";
1430 indent(buf, indent_level);
1432 indent(buf, indent_level);
1435 indent(buf, indent_level);
1436 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1438 indent(buf, indent_level);
1441 indent(buf, indent_level);
1454 indent(buf, indent_level);
1457 indent(buf, indent_level);
1458 buf <<
"typedef " << class_name <<
" Nm;\n";
1459 buf <<
"if (!nary) {\n";
1461 indent(buf, indent_level);
1462 buf <<
"((Nm*)obj)->~Nm();\n";
1464 indent(buf, indent_level);
1466 indent(buf, indent_level);
1469 indent(buf, indent_level);
1472 indent(buf, indent_level);
1473 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1475 indent(buf, indent_level);
1476 buf <<
"} while (nary);\n";
1478 indent(buf, indent_level);
1481 indent(buf, indent_level);
1487 string wrapper(buf.str());
1497 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1498 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1513 unsigned long long ull;
1535 SmallVector<ValHolder, 8> vh_ary;
1536 SmallVector<void *, 8> vp_ary;
1538 unsigned num_args =
fArgVals.size();
1542 const FunctionDecl *FD =
GetDecl();
1549 unsigned num_params = FD->getNumParams();
1552 ::Error(
"TClingCallFunc::exec",
1553 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1558 if (address == 0 && dyn_cast<CXXMethodDecl>(FD)
1559 && !(dyn_cast<CXXMethodDecl>(FD))->isStatic()
1560 && !dyn_cast<CXXConstructorDecl>(FD)) {
1561 ::Error(
"TClingCallFunc::exec",
1562 "The method %s is called without an object.",
1566 vh_ary.reserve(num_args);
1567 vp_ary.reserve(num_args);
1568 for (
unsigned i = 0U; i < num_args; ++i) {
1570 if (i < num_params) {
1571 const ParmVarDecl *PVD = FD->getParamDecl(i);
1572 Ty = PVD->getType();
1576 QualType QT = Ty.getCanonicalType();
1577 if (
const BuiltinType *BT =
1578 dyn_cast<BuiltinType>(&*QT)) {
1586 switch (BT->getKind()) {
1590 case BuiltinType::Void: {
1592 ::Error(
"TClingCallFunc::exec(void*)",
1593 "Invalid type 'Void'!");
1604 vh_ary.push_back(vh);
1605 vp_ary.push_back(&vh_ary.back());
1608 case BuiltinType::Char_U: {
1611 vh.u.c = (char) sv_to_ulong_long(
fArgVals[i]);
1612 vh_ary.push_back(vh);
1613 vp_ary.push_back(&vh_ary.back());
1619 vh.u.uc = (
unsigned char) sv_to_ulong_long(
fArgVals[i]);
1620 vh_ary.push_back(vh);
1621 vp_ary.push_back(&vh_ary.back());
1624 case BuiltinType::WChar_U: {
1629 vh.u.wc = (wchar_t) sv_to_ulong_long(
fArgVals[i]);
1630 vh_ary.push_back(vh);
1631 vp_ary.push_back(&vh_ary.back());
1634 case BuiltinType::Char16: {
1642 case BuiltinType::Char32: {
1654 vh_ary.push_back(vh);
1655 vp_ary.push_back(&vh_ary.back());
1661 vh.u.ui = (
unsigned int) sv_to_ulong_long(
fArgVals[i]);
1662 vh_ary.push_back(vh);
1663 vp_ary.push_back(&vh_ary.back());
1669 vh.u.ul = (
unsigned long) sv_to_ulong_long(
fArgVals[i]);
1670 vh_ary.push_back(vh);
1671 vp_ary.push_back(&vh_ary.back());
1674 case BuiltinType::ULongLong: {
1677 vh.u.ull = (
unsigned long long) sv_to_ulong_long(
fArgVals[i]);
1678 vh_ary.push_back(vh);
1679 vp_ary.push_back(&vh_ary.back());
1682 case BuiltinType::UInt128: {
1692 case BuiltinType::Char_S: {
1695 vh.u.c = (char) sv_to_long_long(
fArgVals[i]);
1696 vh_ary.push_back(vh);
1697 vp_ary.push_back(&vh_ary.back());
1700 case BuiltinType::SChar: {
1703 vh.u.sc = (
signed char) sv_to_long_long(
fArgVals[i]);
1704 vh_ary.push_back(vh);
1705 vp_ary.push_back(&vh_ary.back());
1708 case BuiltinType::WChar_S: {
1713 vh.u.wc = (wchar_t) sv_to_long_long(
fArgVals[i]);
1714 vh_ary.push_back(vh);
1715 vp_ary.push_back(&vh_ary.back());
1722 vh_ary.push_back(vh);
1723 vp_ary.push_back(&vh_ary.back());
1730 vh_ary.push_back(vh);
1731 vp_ary.push_back(&vh_ary.back());
1738 vh_ary.push_back(vh);
1739 vp_ary.push_back(&vh_ary.back());
1742 case BuiltinType::LongLong: {
1746 vh_ary.push_back(vh);
1747 vp_ary.push_back(&vh_ary.back());
1750 case BuiltinType::Int128: {
1752 ::Error(
"TClingCallFunc::exec(void*)",
1753 "Invalid type 'Int128'!");
1757 case BuiltinType::Half: {
1759 ::Error(
"TClingCallFunc::exec(void*)",
1760 "Invalid type 'Half'!");
1767 vh.u.flt = sv_to<float>(
fArgVals[i]);
1768 vh_ary.push_back(vh);
1769 vp_ary.push_back(&vh_ary.back());
1775 vh.u.dbl = sv_to<double>(
fArgVals[i]);
1776 vh_ary.push_back(vh);
1777 vp_ary.push_back(&vh_ary.back());
1780 case BuiltinType::LongDouble: {
1783 vh.u.ldbl = sv_to<long double>(
fArgVals[i]);
1784 vh_ary.push_back(vh);
1785 vp_ary.push_back(&vh_ary.back());
1791 case BuiltinType::NullPtr: {
1795 vh_ary.push_back(vh);
1796 vp_ary.push_back(&vh_ary.back());
1802 ::Error(
"TClingCallFunc::exec(void*)",
1803 "Unhandled builtin type!");
1809 }
else if (QT->isReferenceType()) {
1812 vp_ary.push_back((
void *) sv_to_ulong_long(
fArgVals[i]));
1813 }
else if (QT->isPointerType() || QT->isArrayType()) {
1815 vh.u.vp = (
void *) sv_to_ulong_long(
fArgVals[i]);
1816 vh_ary.push_back(vh);
1817 vp_ary.push_back(&vh_ary.back());
1818 }
else if (QT->isRecordType()) {
1821 vp_ary.push_back((
void *) sv_to_ulong_long(
fArgVals[i]));
1822 }
else if (
const EnumType *ET =
1823 dyn_cast<EnumType>(&*QT)) {
1829 vh_ary.push_back(vh);
1830 vp_ary.push_back(&vh_ary.back());
1831 }
else if (QT->isMemberPointerType()) {
1833 vh.u.vp = (
void *) sv_to_ulong_long(
fArgVals[i]);
1834 vh_ary.push_back(vh);
1835 vp_ary.push_back(&vh_ary.back());
1837 ::Error(
"TClingCallFunc::exec(void*)",
1838 "Invalid type (unrecognized)!");
1844 (*fWrapper)(address, (
int)num_args, (
void **)vp_ary.data(), ret);
1847template <
typename T>
1851 exec(address, &ret);
1855template <
typename T>
1859 exec(address, &ret);
1860 val->getULL() = ret;
1867 ret = cling::Value::Create<T>(QT.getAsOpaquePtr(), *
fInterp);
1868 static_assert(std::is_integral<T>::value,
"Must be called with integral T");
1869 if (std::is_signed<T>::value)
1870 return [
this](
void* address,
cling::Value& ret) { execWithLL<T>(address, &ret); };
1872 return [
this](
void* address,
cling::Value& ret) { execWithULL<T>(address, &ret); };
1878 switch (BT->getKind()) {
1879 case BuiltinType::Void: {
1880 ret = cling::Value::Create<void>(QT.getAsOpaquePtr(), *
fInterp);
1889 return InitRetAndExecIntegral<bool>(QT, ret);
1891 case BuiltinType::Char_U:
1893 return InitRetAndExecIntegral<char>(QT, ret);
1895 case BuiltinType::WChar_U:
1896 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1898 case BuiltinType::Char16:
1899 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1900 "Invalid type 'char16_t'!");
1903 case BuiltinType::Char32:
1904 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1905 "Invalid type 'char32_t'!");
1909 return InitRetAndExecIntegral<unsigned short>(QT, ret);
1912 return InitRetAndExecIntegral<unsigned int>(QT, ret);
1915 return InitRetAndExecIntegral<unsigned long>(QT, ret);
1917 case BuiltinType::ULongLong:
1918 return InitRetAndExecIntegral<unsigned long long>(QT, ret);
1920 case BuiltinType::UInt128: {
1921 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1922 "Invalid type '__uint128_t'!");
1930 case BuiltinType::Char_S:
1931 case BuiltinType::SChar:
1932 return InitRetAndExecIntegral<signed char>(QT, ret);
1934 case BuiltinType::WChar_S:
1938 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1941 return InitRetAndExecIntegral<short>(QT, ret);
1944 return InitRetAndExecIntegral<int>(QT, ret);
1947 return InitRetAndExecIntegral<long>(QT, ret);
1949 case BuiltinType::LongLong:
1950 return InitRetAndExecIntegral<long long>(QT, ret);
1952 case BuiltinType::Int128:
1953 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1954 "Invalid type '__int128_t'!");
1957 case BuiltinType::Half:
1959 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1960 "Invalid type 'Half'!");
1964 ret = cling::Value::Create<float>(QT.getAsOpaquePtr(), *
fInterp);
1965 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getFloat()); };
1969 ret = cling::Value::Create<double>(QT.getAsOpaquePtr(), *
fInterp);
1970 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getDouble()); };
1973 case BuiltinType::LongDouble: {
1974 ret = cling::Value::Create<long double>(QT.getAsOpaquePtr(), *
fInterp);
1975 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getLongDouble()); };
1981 case BuiltinType::NullPtr:
1983 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1984 "Invalid type 'nullptr'!");
1996 if (QT->isReferenceType()) {
1998 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
1999 }
else if (QT->isMemberPointerType()) {
2000 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
2001 if (MPT->isMemberDataPointer()) {
2008 return [
this](
void* address,
cling::Value& ret) {
exec(address, ret.getPtr()); };
2012 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
2013 }
else if (QT->isPointerType() || QT->isArrayType()) {
2015 ret = cling::Value::Create<void*>(QT.getAsOpaquePtr(), *
fInterp);
2016 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
2017 }
else if (QT->isRecordType()) {
2019 return [
this](
void* address,
cling::Value& ret) {
exec(address, ret.getPtr()); };
2020 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
2025 return [
this](
void* address,
cling::Value& ret) { execWithLL<int>(address, &ret); };
2026 }
else if (
const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
2029 ::Error(
"TClingCallFunc::exec_with_valref_return",
2030 "Unrecognized return type!");
2037 if (llvm::isa<CXXConstructorDecl>(FD)) {
2038 ASTContext &Context = FD->getASTContext();
2040 QualType ClassTy(TD->getTypeForDecl(), 0);
2041 QualType QT = Context.getLValueReferenceType(ClassTy);
2044 return [
this](
void* address,
cling::Value& ret) {
exec(address, &ret.getPtr()); };
2046 QualType QT = FD->getReturnType().getCanonicalType();
2066 execFunc(address, *ret);
2074 SmallVector<Expr *, 4> exprs;
2075 fInterp->getLookupHelper().findArgList(ArgList, exprs,
2076 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
2077 : cling::LookupHelper::NoDiagnostics);
2078 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
2079 E = exprs.end();
I !=
E; ++
I) {
2082 if (!val.isValid()) {
2084 ::Error(
"TClingCallFunc::EvaluateArgList",
2085 "Bad expression in parameter %d of '%s'!",
2086 (
int)(
I - exprs.begin()),
2098 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
2099 "Called with no wrapper, not implemented!");
2110template <
typename T>
2115 ::Error(
"TClingCallFunc::ExecT",
2116 "Called with no wrapper, not implemented!");
2121 if (!ret.isValid()) {
2128 return sv_to<T>(ret);
2133 return ExecT<long>(address);
2138 return ExecT<long long>(address);
2143 return ExecT<double>(address);
2147 int nargs ,
void *ret)
2151 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2152 "Called with no wrapper, not implemented!");
2155 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
2162 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
2163 "Called with no wrapper, not implemented!");
2171 const std::string &type_name,
2172 void *address ,
unsigned long nary )
2175 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
2198 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
2199 "Called with no wrapper, not implemented!");
2203 (*wrapper)(&obj, address, nary);
2208 unsigned long nary ,
bool withFree )
2211 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
2218 const Decl *D = info->
GetDecl();
2227 ::Error(
"TClingCallFunc::ExecDestructor",
2228 "Called with no wrapper, not implemented!");
2231 (*wrapper)(address, nary, withFree);
2270 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2291 ::Error(
"TClingCallFunc::IFacePtr(kind)",
2292 "Attempt to get interface while invalid.");
2299 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2319 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2326 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2333 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2335 fArgVals.back().getFloat() = param;
2340 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2342 fArgVals.back().getDouble() = param;
2347 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2354 const ASTContext &
C =
fInterp->getCI()->getASTContext();
2362 for (
int i = 0; i < nparam; ++i) {
2376 SetFunc(info, method, arglist,
false, poffset);
2380 bool objectIsConst,
long *poffset)
2388 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
2391 if (!strcmp(arglist,
")")) {
2417 const char *
proto,
long *poffset,
2424 const char *
proto,
bool objectIsConst,
long *poffset,
2433 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2445 const llvm::SmallVectorImpl<clang::QualType> &
proto,
long *poffset,
2452 const llvm::SmallVectorImpl<clang::QualType> &
proto,
2453 bool objectIsConst,
long *poffset,
2462 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
static void EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
static unsigned long long gWrapperSerial
static map< const Decl *, void * > gCtorWrapperStore
static const string kIndentString(" ")
static void indent(ostringstream &buf, int indent_level)
static map< const Decl *, void * > gDtorWrapperStore
static map< const 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,...)
R__EXTERN TVirtualMutex * gInterpreterMutex
#define R__LOCKGUARD_CLING(mutex)
R__EXTERN TInterpreter * gCling
typedef void((*Func_t)())
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)
long ExecInt(void *address)
std::unique_ptr< TClingMethodInfo > fMethod
Current method, we own.
ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret)
void collect_type_info(clang::QualType &QT, std::ostringstream &typedefbuf, std::ostringstream &callbuf, std::string &type_name, EReferenceType &refType, bool &isPointer, int indent_level, bool forArgument)
void SetArgs(const char *args)
size_t fMinRequiredArguments
Number of required arguments.
size_t CalculateMinRequiredArguments()
double ExecDouble(void *address)
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, long *poffset)
tcling_callfunc_Wrapper_t make_wrapper()
ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret)
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
std::function< void(void *address, cling::Value &ret)> ExecWithRetFunc_t
void ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
void execWithLL(void *address, cling::Value *val)
const clang::DeclContext * GetDeclContext() const
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
TInterpreter::CallFuncIFacePtr_t IFacePtr()
void exec(void *address, void *ret)
void exec_with_valref_return(void *address, cling::Value *ret)
void make_narg_ctor(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
const clang::FunctionDecl * GetDecl()
void execWithULL(void *address, cling::Value *val)
void EvaluateArgList(const std::string &ArgList)
const clang::Decl * GetFunctionOrShadowDecl() const
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
void SetArgArray(long *argArr, int narg)
void Exec(void *address, TInterpreterValue *interpVal=0)
TClingMethodInfo * FactoryMethod() const
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 &)
const ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
ROOT normalized context for that interpreter.
tcling_callfunc_Wrapper_t fWrapper
Pointer to compiled wrapper, we do not own.
long long ExecInt64(void *address)
cling::Interpreter * fInterp
Cling interpreter, we do not own.
void make_narg_call(const std::string &return_type, const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
const clang::FunctionDecl * fDecl
Decl for the method.
void make_narg_call_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
llvm::SmallVector< cling::Value, 8 > fArgVals
Stored function arguments, we own.
void make_narg_ctor_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret)
Emulation of the CINT ClassInfo class.
TClingMethodInfo GetMethodWithArgs(const char *fname, const char *arglist, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch, EInheritanceMode imode=kWithInheritance) const
TClingMethodInfo GetMethod(const char *fname) const
virtual bool IsValid() const
virtual const clang::Decl * GetDecl() const
Emulation of the CINT MethodInfo class.
This class defines an interface to the cling C++ interpreter.
virtual const void * GetValAddr() const =0
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
RooArgSet S(const RooAbsArg &v1)
static constexpr double nm
static constexpr double us
static constexpr double s
static constexpr double L
constexpr Double_t E()
Base of natural log: