35#include "cling/Interpreter/CompilationOptions.h"
36#include "cling/Interpreter/Interpreter.h"
37#include "cling/Interpreter/LookupHelper.h"
38#include "cling/Interpreter/Transaction.h"
39#include "cling/Interpreter/Value.h"
40#include "cling/Utils/AST.h"
42#include "clang/AST/ASTContext.h"
43#include "clang/AST/Decl.h"
44#include "clang/AST/DeclCXX.h"
45#include "clang/AST/GlobalDecl.h"
46#include "clang/AST/PrettyPrinter.h"
47#include "clang/AST/RecordLayout.h"
48#include "clang/AST/Type.h"
49#include "clang/Frontend/CompilerInstance.h"
50#include "clang/Lex/Preprocessor.h"
51#include "clang/Sema/Sema.h"
52#include "clang/Sema/Lookup.h"
54#include "llvm/ADT/APInt.h"
55#include "llvm/ExecutionEngine/ExecutionEngine.h"
56#include "llvm/ExecutionEngine/GenericValue.h"
57#include "llvm/Support/Casting.h"
58#include "llvm/Support/raw_ostream.h"
59#include "llvm/IR/LLVMContext.h"
60#include "llvm/IR/DerivedTypes.h"
61#include "llvm/IR/Function.h"
62#include "llvm/IR/GlobalValue.h"
63#include "llvm/IR/Module.h"
64#include "llvm/IR/Type.h"
66#include "clang/Sema/SemaInternal.h"
87indent(ostringstream &buf,
int indent_level)
89 for (
int i = 0; i < indent_level; ++i) {
96EvaluateExpr(cling::Interpreter &interp,
const Expr *E, cling::Value &V)
99 ASTContext &C = interp.getCI()->getASTContext();
100 clang::Expr::EvalResult evalRes;
101 if (E->EvaluateAsInt(evalRes, C, Expr::SE_NoSideEffects)) {
102 APSInt res = evalRes.Val.getInt();
104 V = cling::Value(C.IntTy, interp);
108 V.getLL() = res.getSExtValue();
110 V.getULL() = res.getZExtValue();
115 PrintingPolicy Policy(C.getPrintingPolicy());
116 Policy.SuppressTagKeyword =
true;
117 Policy.SuppressUnwrittenScope =
false;
118 Policy.SuppressInitializers =
false;
119 Policy.AnonymousTagLocations =
false;
121 raw_string_ostream out(buf);
122 E->printPretty(out, 0, Policy, 0);
126 interp.evaluate(buf, V);
130 template <
typename returnType>
131 returnType sv_to(
const cling::Value &val)
133 QualType QT = val.getType().getCanonicalType();
134 if (
const BuiltinType *BT =
135 dyn_cast<BuiltinType>(&*QT)) {
143 switch (BT->getKind()) {
144 case BuiltinType::Void:
146 return (returnType) 0;
151 case BuiltinType::Bool:
152 case BuiltinType::Char_U:
153 case BuiltinType::UChar:
154 return (returnType) val.getULL();
157 case BuiltinType::WChar_U:
161 return (returnType)(wchar_t) val.getULL();
164 case BuiltinType::Char16:
165 case BuiltinType::Char32:
166 case BuiltinType::UShort:
167 case BuiltinType::UInt:
168 case BuiltinType::ULong:
169 case BuiltinType::ULongLong:
170 return (returnType) val.getULL();
173 case BuiltinType::UInt128:
180 case BuiltinType::Char_S:
181 case BuiltinType::SChar:
182 return (returnType) val.getLL();
185 case BuiltinType::WChar_S:
189 return (returnType)(wchar_t) val.getLL();
192 case BuiltinType::Short:
193 case BuiltinType::Int:
194 case BuiltinType::Long:
195 case BuiltinType::LongLong:
196 return (returnType) val.getLL();
199 case BuiltinType::Int128:
202 case BuiltinType::Half:
206 case BuiltinType::Float:
207 return (returnType) val.getFloat();
209 case BuiltinType::Double:
210 return (returnType) val.getDouble();
212 case BuiltinType::LongDouble:
213 return (returnType) val.getLongDouble();
216 case BuiltinType::NullPtr:
217 return (returnType) 0;
224 if (QT->isPointerType() || QT->isArrayType() || QT->isRecordType() ||
225 QT->isReferenceType()) {
226 return (returnType)(
Longptr_t) val.getPtr();
228 if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
229 if (ET->getDecl()->getIntegerType()->hasSignedIntegerRepresentation())
230 return (returnType) val.getLL();
232 return (returnType) val.getULL();
234 if (QT->isMemberPointerType()) {
235 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
236 if (MPT && MPT->isMemberDataPointer()) {
237 return (returnType)(ptrdiff_t)val.getPtr();
239 return (returnType)(
Longptr_t) val.getPtr();
241 ::Error(
"TClingCallFunc::sv_to",
"Invalid Type!");
247 long long sv_to_long_long(
const cling::Value &val)
249 return sv_to<long long>(val);
252 unsigned long long sv_to_ulong_long(
const cling::Value &val)
254 return sv_to<unsigned long long>(val);
262 return GetDecl()->getMinRequiredArguments();
266 bool withAccessControl)
268 return fInterp->compileFunction(wrapper_name, wrapper,
false ,
273 string &type_name,
EReferenceType &refType,
bool &isPointer,
int indent_level,
280 const FunctionDecl *FD =
GetDecl();
281 PrintingPolicy Policy(FD->getASTContext().getPrintingPolicy());
283 if (QT->isRecordType() && forArgument) {
287 if (QT->isFunctionPointerType()) {
288 string fp_typedef_name;
292 type_name = nm.str();
293 raw_string_ostream OS(fp_typedef_name);
294 QT.print(OS, Policy, type_name);
297 for (
int i = 0; i < indent_level; ++i) {
300 typedefbuf <<
"typedef " << fp_typedef_name <<
";\n";
302 }
else if (QT->isMemberPointerType()) {
303 string mp_typedef_name;
307 type_name = nm.str();
308 raw_string_ostream OS(mp_typedef_name);
309 QT.print(OS, Policy, type_name);
312 for (
int i = 0; i < indent_level; ++i) {
315 typedefbuf <<
"typedef " << mp_typedef_name <<
";\n";
317 }
else if (QT->isPointerType()) {
319 QT = cast<clang::PointerType>(QT)->getPointeeType();
320 }
else if (QT->isReferenceType()) {
323 QT = cast<ReferenceType>(QT)->getPointeeType();
326 if (QT->isArrayType()) {
327 string ar_typedef_name;
331 type_name = ar.str();
332 raw_string_ostream OS(ar_typedef_name);
333 QT.print(OS, Policy, type_name);
336 for (
int i = 0; i < indent_level; ++i) {
339 typedefbuf <<
"typedef " << ar_typedef_name <<
";\n";
346 ostringstream &callbuf,
const string &class_name,
353 const FunctionDecl *FD =
GetDecl();
355 callbuf <<
"new " << class_name <<
"(";
356 for (
unsigned i = 0U; i <
N; ++i) {
357 const ParmVarDecl *PVD = FD->getParamDecl(i);
358 QualType Ty = PVD->getType();
359 QualType QT = Ty.getCanonicalType();
362 bool isPointer =
false;
364 refType, isPointer, indent_level,
true);
371 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["
384 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
391 ostringstream &callbuf,
const string &class_name,
int indent_level)
398 const FunctionDecl *FD =
GetDecl();
411 bool ShouldCastFunction = !isa<CXXMethodDecl>(FD) &&
N == FD->getNumParams();
412 if (ShouldCastFunction) {
415 callbuf << return_type <<
" (&)";
418 for (
unsigned i = 0U; i <
N; ++i) {
425 for (
int j = 0; j <= indent_level; ++j) {
430 const ParmVarDecl *PVD = FD->getParamDecl(i);
431 QualType Ty = PVD->getType();
432 QualType QT = Ty.getCanonicalType();
433 std::string arg_type;
437 if (FD->isVariadic())
445 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
448 callbuf <<
"((const " << class_name <<
"*)obj)->";
450 callbuf <<
"((" << class_name <<
"*)obj)->";
451 }
else if (
const NamedDecl *ND =
455 callbuf << class_name <<
"::";
461 llvm::raw_string_ostream stream(
name);
462 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
466 if (ShouldCastFunction) callbuf <<
")";
469 for (
unsigned i = 0U; i <
N; ++i) {
470 const ParmVarDecl *PVD = FD->getParamDecl(i);
471 QualType Ty = PVD->getType();
472 QualType QT = Ty.getCanonicalType();
475 bool isPointer =
false;
476 collect_type_info(QT, typedefbuf, callbuf, type_name, refType, isPointer, indent_level,
true);
484 for (
int j = 0; j <= indent_level; ++j) {
491 callbuf <<
"(" << type_name.c_str() <<
492 (refType ==
kLValueReference ?
"&" :
"&&") <<
")*(" << type_name.c_str() <<
"*)args["
494 }
else if (isPointer) {
495 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
500 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
507 ostringstream &buf,
int indent_level)
518 for (
int i = 0; i < indent_level; ++i) {
521 buf <<
"if (ret) {\n";
524 ostringstream typedefbuf;
525 ostringstream callbuf;
529 for (
int i = 0; i < indent_level; ++i) {
532 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
541 for (
int i = 0; i < indent_level; ++i) {
544 callbuf <<
"return;\n";
548 buf << typedefbuf.str() << callbuf.str();
551 for (
int i = 0; i < indent_level; ++i) {
555 for (
int i = 0; i < indent_level; ++i) {
561 ostringstream typedefbuf;
562 ostringstream callbuf;
563 for (
int i = 0; i < indent_level; ++i) {
568 for (
int i = 0; i < indent_level; ++i) {
571 callbuf <<
"return;\n";
572 buf << typedefbuf.str() << callbuf.str();
575 for (
int i = 0; i < indent_level; ++i) {
589 return fMethod->GetDecl()->getDeclContext();
594 const FunctionDecl *FD =
GetDecl();
595 assert(FD &&
"generate_wrapper called without a function decl!");
596 ASTContext &Context = FD->getASTContext();
597 PrintingPolicy Policy(Context.getPrintingPolicy());
603 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(DC)) {
605 QualType QT(TD->getTypeForDecl(), 0);
607 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
609 raw_string_ostream stream(class_name);
610 ND->getNameForDiagnostic(stream, Policy,
true);
617 bool needInstantiation =
false;
618 const FunctionDecl *Definition = 0;
619 if (!FD->isDefined(Definition)) {
620 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
622 case FunctionDecl::TK_NonTemplate: {
632 case FunctionDecl::TK_FunctionTemplate: {
635 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
638 case FunctionDecl::TK_MemberSpecialization: {
645 if (!FD->isTemplateInstantiation()) {
658 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
660 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
661 "instantiation with no pattern!");
664 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
665 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
668 (PTK == FunctionDecl::TK_NonTemplate) ||
671 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
672 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
677 }
else if (!
Pattern->hasBody()) {
678 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a member function "
679 "instantiation with no body!");
682 if (FD->isImplicitlyInstantiable()) {
683 needInstantiation =
true;
686 case FunctionDecl::TK_FunctionTemplateSpecialization: {
691 if (!FD->isTemplateInstantiation()) {
704 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
706 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
707 "instantiation with no pattern!");
710 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
711 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
714 (PTK == FunctionDecl::TK_NonTemplate) ||
717 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
718 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
725 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template"
726 "instantiation with no body!");
729 if (FD->isImplicitlyInstantiable()) {
730 needInstantiation =
true;
733 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
741 if (!FD->isTemplateInstantiation()) {
754 const FunctionDecl *
Pattern = FD->getTemplateInstantiationPattern();
756 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
757 "instantiation with no pattern!");
760 FunctionDecl::TemplatedKind PTK =
Pattern->getTemplatedKind();
761 TemplateSpecializationKind PTSK =
Pattern->getTemplateSpecializationKind();
764 (PTK == FunctionDecl::TK_NonTemplate) ||
767 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
768 ((PTSK == TSK_Undeclared) || (PTSK == TSK_ExplicitSpecialization)))) {
775 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a dependent function template"
776 "instantiation with no body!");
779 if (FD->isImplicitlyInstantiable()) {
780 needInstantiation =
true;
786 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
808 if (needInstantiation) {
809 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
810 clang::Sema &S =
fInterp->getSema();
812 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
813 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
816 if (!FD->isDefined(Definition)) {
817 ::Error(
"TClingCallFunc::make_wrapper",
"Failed to force template instantiation!");
822 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
824 case FunctionDecl::TK_NonTemplate: {
826 if (Definition->isDeleted()) {
827 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function!");
829 }
else if (Definition->isLateTemplateParsed()) {
830 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
841 case FunctionDecl::TK_FunctionTemplate: {
844 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a function template!");
847 case FunctionDecl::TK_MemberSpecialization: {
851 if (Definition->isDeleted()) {
852 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted member function "
853 "of a specialization!");
855 }
else if (Definition->isLateTemplateParsed()) {
856 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
857 "member function of a specialization!");
867 case FunctionDecl::TK_FunctionTemplateSpecialization: {
872 if (Definition->isDeleted()) {
873 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted function "
874 "template specialization!");
876 }
else if (Definition->isLateTemplateParsed()) {
877 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
878 "function template specialization!");
888 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
896 if (Definition->isDeleted()) {
897 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a deleted dependent function "
898 "template specialization!");
900 }
else if (Definition->isLateTemplateParsed()) {
901 ::Error(
"TClingCallFunc::make_wrapper",
"Cannot make wrapper for a late template parsed "
902 "dependent function template specialization!");
915 ::Error(
"TClingCallFunc::make_wrapper",
"Unhandled template kind!");
921 unsigned num_params = FD->getNumParams();
933 wrapper_name = buf.str();
939 int indent_level = 0;
941 buf <<
"#pragma clang diagnostic push\n"
942 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
943 "__attribute__((used)) "
944 "__attribute__((annotate(\"__cling__ptrcheck(off)\")))\n"
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 callbuf <<
"(void)(";
1090 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1092 for (
int i = 0; i < indent_level; ++i) {
1095 callbuf <<
"return;\n";
1096 buf << typedefbuf.str() << callbuf.str();
1099 for (
int i = 0; i < indent_level; ++i) {
1111 string wrapper_name;
1124 ::Error(
"TClingCallFunc::make_wrapper",
1125 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1202 ASTContext &Context = info->
GetDecl()->getASTContext();
1203 PrintingPolicy Policy(Context.getPrintingPolicy());
1204 Policy.SuppressTagKeyword =
true;
1205 Policy.SuppressUnwrittenScope =
true;
1210 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1212 QualType QT(TD->getTypeForDecl(), 0);
1214 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1216 raw_string_ostream stream(class_name);
1217 ND->getNameForDiagnostic(stream, Policy,
true);
1225 string wrapper_name;
1234 wrapper_name = buf.str();
1239 constr_arg = string(
"((") + type_name +
"*)nullptr)";
1241 constr_arg = string(
"(*((") + type_name +
"*)arena))";
1246 int indent_level = 0;
1248 buf <<
"__attribute__((used)) ";
1249 buf <<
"extern \"C\" void ";
1250 buf << wrapper_name;
1251 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1262 indent(buf, ++indent_level);
1263 buf <<
"if (!arena) {\n";
1264 indent(buf, ++indent_level);
1265 buf <<
"if (!nary) {\n";
1266 indent(buf, ++indent_level);
1267 buf <<
"*ret = new " << class_name << constr_arg <<
";\n";
1268 indent(buf, --indent_level);
1270 indent(buf, indent_level);
1272 indent(buf, ++indent_level);
1273 if (constr_arg.empty()) {
1274 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1276 buf <<
"char *buf = (char *) malloc(nary * sizeof(" << class_name <<
"));\n";
1277 indent(buf, indent_level);
1278 buf <<
"for (int k=0;k<nary;++k)\n";
1279 indent(buf, ++indent_level);
1280 buf <<
"new (buf + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1281 indent(buf, --indent_level);
1282 buf <<
"*ret = buf;\n";
1284 indent(buf, --indent_level);
1286 indent(buf, --indent_level);
1296 indent(buf, indent_level);
1298 indent(buf, ++indent_level);
1299 buf <<
"if (!nary) {\n";
1300 indent(buf, ++indent_level);
1301 buf <<
"*ret = new (arena) " << class_name << constr_arg <<
";\n";
1302 indent(buf, --indent_level);
1304 indent(buf, indent_level);
1306 indent(buf, ++indent_level);
1307 if (constr_arg.empty()) {
1308 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1310 buf <<
"for (int k=0;k<nary;++k)\n";
1311 indent(buf, ++indent_level);
1312 buf <<
"new ((char *) arena + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1313 indent(buf, --indent_level);
1314 buf <<
"*ret = arena;\n";
1316 indent(buf, --indent_level);
1318 indent(buf, --indent_level);
1324 string wrapper(buf.str());
1334 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1335 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1371 ASTContext &Context = info->
GetDecl()->getASTContext();
1372 PrintingPolicy Policy(Context.getPrintingPolicy());
1373 Policy.SuppressTagKeyword =
true;
1374 Policy.SuppressUnwrittenScope =
true;
1379 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1381 QualType QT(TD->getTypeForDecl(), 0);
1383 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1385 raw_string_ostream stream(class_name);
1386 ND->getNameForDiagnostic(stream, Policy,
true);
1392 string wrapper_name;
1401 wrapper_name = buf.str();
1406 int indent_level = 0;
1408 buf <<
"__attribute__((used)) ";
1409 buf <<
"extern \"C\" void ";
1410 buf << wrapper_name;
1411 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1422 indent(buf, indent_level);
1423 buf <<
"if (withFree) {\n";
1425 indent(buf, indent_level);
1426 buf <<
"if (!nary) {\n";
1428 indent(buf, indent_level);
1429 buf <<
"delete (" << class_name <<
"*) obj;\n";
1431 indent(buf, indent_level);
1433 indent(buf, indent_level);
1436 indent(buf, indent_level);
1437 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1439 indent(buf, indent_level);
1442 indent(buf, indent_level);
1455 indent(buf, indent_level);
1458 indent(buf, indent_level);
1459 buf <<
"typedef " << class_name <<
" Nm;\n";
1460 buf <<
"if (!nary) {\n";
1462 indent(buf, indent_level);
1463 buf <<
"((Nm*)obj)->~Nm();\n";
1465 indent(buf, indent_level);
1467 indent(buf, indent_level);
1470 indent(buf, indent_level);
1473 indent(buf, indent_level);
1474 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1476 indent(buf, indent_level);
1477 buf <<
"} while (nary);\n";
1479 indent(buf, indent_level);
1482 indent(buf, indent_level);
1488 string wrapper(buf.str());
1498 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1499 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1536 SmallVector<ValHolder, 8> vh_ary;
1537 SmallVector<void *, 8> vp_ary;
1539 unsigned num_args =
fArgVals.size();
1543 const FunctionDecl *FD =
GetDecl();
1550 unsigned num_params = FD->getNumParams();
1553 ::Error(
"TClingCallFunc::exec",
1554 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1559 if (address == 0 && dyn_cast<CXXMethodDecl>(FD)
1560 && !(dyn_cast<CXXMethodDecl>(FD))->isStatic()
1561 && !dyn_cast<CXXConstructorDecl>(FD)) {
1562 ::Error(
"TClingCallFunc::exec",
1563 "The method %s is called without an object.",
1567 vh_ary.reserve(num_args);
1568 vp_ary.reserve(num_args);
1569 for (
unsigned i = 0U; i < num_args; ++i) {
1571 if (i < num_params) {
1572 const ParmVarDecl *PVD = FD->getParamDecl(i);
1573 Ty = PVD->getType();
1577 QualType QT = Ty.getCanonicalType();
1578 if (
const BuiltinType *BT =
1579 dyn_cast<BuiltinType>(&*QT)) {
1587 switch (BT->getKind()) {
1591 case BuiltinType::Void: {
1593 ::Error(
"TClingCallFunc::exec(void*)",
1594 "Invalid type 'Void'!");
1601 case BuiltinType::Bool: {
1605 vh_ary.push_back(vh);
1606 vp_ary.push_back(&vh_ary.back());
1609 case BuiltinType::Char_U: {
1612 vh.
u.
c = (char) sv_to_ulong_long(
fArgVals[i]);
1613 vh_ary.push_back(vh);
1614 vp_ary.push_back(&vh_ary.back());
1617 case BuiltinType::UChar: {
1620 vh.
u.
uc = (
unsigned char) sv_to_ulong_long(
fArgVals[i]);
1621 vh_ary.push_back(vh);
1622 vp_ary.push_back(&vh_ary.back());
1625 case BuiltinType::WChar_U: {
1630 vh.
u.
wc = (wchar_t) sv_to_ulong_long(
fArgVals[i]);
1631 vh_ary.push_back(vh);
1632 vp_ary.push_back(&vh_ary.back());
1635 case BuiltinType::Char16: {
1643 case BuiltinType::Char32: {
1651 case BuiltinType::UShort: {
1655 vh_ary.push_back(vh);
1656 vp_ary.push_back(&vh_ary.back());
1659 case BuiltinType::UInt: {
1663 vh_ary.push_back(vh);
1664 vp_ary.push_back(&vh_ary.back());
1667 case BuiltinType::ULong: {
1671 vh_ary.push_back(vh);
1672 vp_ary.push_back(&vh_ary.back());
1675 case BuiltinType::ULongLong: {
1679 vh_ary.push_back(vh);
1680 vp_ary.push_back(&vh_ary.back());
1683 case BuiltinType::UInt128: {
1693 case BuiltinType::Char_S: {
1696 vh.
u.
c = (char) sv_to_long_long(
fArgVals[i]);
1697 vh_ary.push_back(vh);
1698 vp_ary.push_back(&vh_ary.back());
1701 case BuiltinType::SChar: {
1704 vh.
u.
sc = (
signed char) sv_to_long_long(
fArgVals[i]);
1705 vh_ary.push_back(vh);
1706 vp_ary.push_back(&vh_ary.back());
1709 case BuiltinType::WChar_S: {
1715 vh_ary.push_back(vh);
1716 vp_ary.push_back(&vh_ary.back());
1719 case BuiltinType::Short: {
1723 vh_ary.push_back(vh);
1724 vp_ary.push_back(&vh_ary.back());
1727 case BuiltinType::Int: {
1731 vh_ary.push_back(vh);
1732 vp_ary.push_back(&vh_ary.back());
1735 case BuiltinType::Long: {
1739 vh_ary.push_back(vh);
1740 vp_ary.push_back(&vh_ary.back());
1743 case BuiltinType::LongLong: {
1747 vh_ary.push_back(vh);
1748 vp_ary.push_back(&vh_ary.back());
1751 case BuiltinType::Int128: {
1753 ::Error(
"TClingCallFunc::exec(void*)",
1754 "Invalid type 'Int128'!");
1758 case BuiltinType::Half: {
1760 ::Error(
"TClingCallFunc::exec(void*)",
1761 "Invalid type 'Half'!");
1765 case BuiltinType::Float: {
1769 vh_ary.push_back(vh);
1770 vp_ary.push_back(&vh_ary.back());
1773 case BuiltinType::Double: {
1777 vh_ary.push_back(vh);
1778 vp_ary.push_back(&vh_ary.back());
1781 case BuiltinType::LongDouble: {
1785 vh_ary.push_back(vh);
1786 vp_ary.push_back(&vh_ary.back());
1792 case BuiltinType::NullPtr: {
1796 vh_ary.push_back(vh);
1797 vp_ary.push_back(&vh_ary.back());
1803 ::Error(
"TClingCallFunc::exec(void*)",
1804 "Unhandled builtin type!");
1810 }
else if (QT->isReferenceType()) {
1813 vp_ary.push_back((
void *) sv_to_ulong_long(
fArgVals[i]));
1814 }
else if (QT->isPointerType() || QT->isArrayType()) {
1817 vh_ary.push_back(vh);
1818 vp_ary.push_back(&vh_ary.back());
1819 }
else if (QT->isRecordType()) {
1822 vp_ary.push_back((
void *) sv_to_ulong_long(
fArgVals[i]));
1823 }
else if (
const EnumType *ET =
1824 dyn_cast<EnumType>(&*QT)) {
1830 vh_ary.push_back(vh);
1831 vp_ary.push_back(&vh_ary.back());
1832 }
else if (QT->isMemberPointerType()) {
1835 vh_ary.push_back(vh);
1836 vp_ary.push_back(&vh_ary.back());
1838 ::Error(
"TClingCallFunc::exec(void*)",
1839 "Invalid type (unrecognized)!");
1845 (*fWrapper)(address, (
int)num_args, (
void **)vp_ary.data(), ret);
1848template <
typename T>
1852 exec(address, &ret);
1856template <
typename T>
1860 exec(address, &ret);
1861 val->getULL() = ret;
1868 ret = cling::Value::Create<T>(QT.getAsOpaquePtr(), *
fInterp);
1869 static_assert(std::is_integral<T>::value,
"Must be called with integral T");
1870 if (std::is_signed<T>::value)
1871 return [
this](
void* address, cling::Value& ret) { execWithLL<T>(address, &ret); };
1873 return [
this](
void* address, cling::Value& ret) { execWithULL<T>(address, &ret); };
1879 switch (BT->getKind()) {
1880 case BuiltinType::Void: {
1881 ret = cling::Value::Create<void>(QT.getAsOpaquePtr(), *
fInterp);
1882 return [
this](
void* address, cling::Value& ret) {
exec(address, 0); };
1889 case BuiltinType::Bool:
1890 return InitRetAndExecIntegral<bool>(QT, ret);
1892 case BuiltinType::Char_U:
1893 case BuiltinType::UChar:
1894 return InitRetAndExecIntegral<char>(QT, ret);
1896 case BuiltinType::WChar_U:
1897 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1899 case BuiltinType::Char16:
1900 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1901 "Invalid type 'char16_t'!");
1904 case BuiltinType::Char32:
1905 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1906 "Invalid type 'char32_t'!");
1909 case BuiltinType::UShort:
1910 return InitRetAndExecIntegral<unsigned short>(QT, ret);
1912 case BuiltinType::UInt:
1913 return InitRetAndExecIntegral<unsigned int>(QT, ret);
1915 case BuiltinType::ULong:
1916 return InitRetAndExecIntegral<unsigned long>(QT, ret);
1918 case BuiltinType::ULongLong:
1919 return InitRetAndExecIntegral<unsigned long long>(QT, ret);
1921 case BuiltinType::UInt128: {
1922 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1923 "Invalid type '__uint128_t'!");
1931 case BuiltinType::Char_S:
1932 case BuiltinType::SChar:
1933 return InitRetAndExecIntegral<signed char>(QT, ret);
1935 case BuiltinType::WChar_S:
1939 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1941 case BuiltinType::Short:
1942 return InitRetAndExecIntegral<short>(QT, ret);
1944 case BuiltinType::Int:
1945 return InitRetAndExecIntegral<int>(QT, ret);
1947 case BuiltinType::Long:
1948 return InitRetAndExecIntegral<long>(QT, ret);
1950 case BuiltinType::LongLong:
1951 return InitRetAndExecIntegral<long long>(QT, ret);
1953 case BuiltinType::Int128:
1954 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1955 "Invalid type '__int128_t'!");
1958 case BuiltinType::Half:
1960 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1961 "Invalid type 'Half'!");
1964 case BuiltinType::Float: {
1965 ret = cling::Value::Create<float>(QT.getAsOpaquePtr(), *
fInterp);
1966 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getFloat()); };
1969 case BuiltinType::Double: {
1970 ret = cling::Value::Create<double>(QT.getAsOpaquePtr(), *
fInterp);
1971 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getDouble()); };
1974 case BuiltinType::LongDouble: {
1975 ret = cling::Value::Create<long double>(QT.getAsOpaquePtr(), *
fInterp);
1976 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getLongDouble()); };
1982 case BuiltinType::NullPtr:
1984 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1985 "Invalid type 'nullptr'!");
1997 if (QT->isReferenceType()) {
1998 ret = cling::Value(QT, *
fInterp);
1999 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
2000 }
else if (QT->isMemberPointerType()) {
2001 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
2002 if (MPT && MPT->isMemberDataPointer()) {
2008 ret = cling::Value(QT, *
fInterp);
2009 return [
this](
void* address, cling::Value& ret) {
exec(address, ret.getPtr()); };
2012 ret = cling::Value(QT, *
fInterp);
2013 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
2014 }
else if (QT->isPointerType() || QT->isArrayType()) {
2016 ret = cling::Value::Create<void*>(QT.getAsOpaquePtr(), *
fInterp);
2017 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
2018 }
else if (QT->isRecordType()) {
2019 ret = cling::Value(QT, *
fInterp);
2020 return [
this](
void* address, cling::Value& ret) {
exec(address, ret.getPtr()); };
2021 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
2025 ret = cling::Value(QT, *
fInterp);
2026 return [
this](
void* address, cling::Value& ret) { execWithLL<int>(address, &ret); };
2027 }
else if (
const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
2030 ::Error(
"TClingCallFunc::exec_with_valref_return",
2031 "Unrecognized return type!");
2038 if (llvm::isa<CXXConstructorDecl>(FD)) {
2039 ASTContext &Context = FD->getASTContext();
2041 QualType ClassTy(TD->getTypeForDecl(), 0);
2042 QualType QT = Context.getLValueReferenceType(ClassTy);
2043 ret = cling::Value(QT, *
fInterp);
2045 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
2047 QualType QT = FD->getReturnType().getCanonicalType();
2058 std::function<
void(
void*, cling::Value&)> execFunc;
2067 execFunc(address, *ret);
2075 SmallVector<Expr *, 4> exprs;
2076 fInterp->getLookupHelper().findArgList(ArgList, exprs,
2077 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
2078 : cling::LookupHelper::NoDiagnostics);
2079 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
2080 E = exprs.end();
I != E; ++
I) {
2083 if (!val.isValid()) {
2085 ::Error(
"TClingCallFunc::EvaluateArgList",
2086 "Bad expression in parameter %d of '%s'!",
2087 (
int)(
I - exprs.begin()),
2099 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
2100 "Called with no wrapper, not implemented!");
2107 cling::Value *val =
reinterpret_cast<cling::Value *
>(interpVal->
GetValAddr());
2111template <
typename T>
2116 ::Error(
"TClingCallFunc::ExecT",
2117 "Called with no wrapper, not implemented!");
2122 if (!ret.isValid()) {
2129 return sv_to<T>(ret);
2134 return ExecT<Longptr_t>(address);
2139 return ExecT<long long>(address);
2144 return ExecT<double>(address);
2148 int nargs ,
void *ret)
2152 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2153 "Called with no wrapper, not implemented!");
2156 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
2163 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
2164 "Called with no wrapper, not implemented!");
2172 const std::string &type_name,
2173 void *address ,
unsigned long nary )
2176 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
2199 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
2200 "Called with no wrapper, not implemented!");
2204 (*wrapper)(&obj, address, nary);
2209 unsigned long nary ,
bool withFree )
2212 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
2219 const Decl *D = info->
GetDecl();
2228 ::Error(
"TClingCallFunc::ExecDestructor",
2229 "Called with no wrapper, not implemented!");
2232 (*wrapper)(address, nary, withFree);
2271 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2292 ::Error(
"TClingCallFunc::IFacePtr(kind)",
2293 "Attempt to get interface while invalid.");
2300 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2320 const ASTContext &C =
fInterp->getCI()->getASTContext();
2327 const ASTContext &C =
fInterp->getCI()->getASTContext();
2334 const ASTContext &C =
fInterp->getCI()->getASTContext();
2336 fArgVals.back().getFloat() = param;
2341 const ASTContext &C =
fInterp->getCI()->getASTContext();
2343 fArgVals.back().getDouble() = param;
2348 const ASTContext &C =
fInterp->getCI()->getASTContext();
2355 const ASTContext &C =
fInterp->getCI()->getASTContext();
2363 for (
int i = 0; i < nparam; ++i) {
2377 SetFunc(info, method, arglist,
false, poffset);
2389 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
2392 if (!strcmp(arglist,
")")) {
2434 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2446 const llvm::SmallVectorImpl<clang::QualType> &
proto,
Longptr_t *poffset,
2453 const llvm::SmallVectorImpl<clang::QualType> &
proto,
2463 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
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,...)
Use this function in case an error occurred.
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)
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 &)
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, 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...