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)(
long) 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->isMemberDataPointer()) {
237 return (returnType)(ptrdiff_t)val.getPtr();
239 return (returnType)(
long) 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 "extern \"C\" void ";
946 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
949 if (min_args == num_params) {
955 for (
unsigned N = min_args;
N <= num_params; ++
N) {
956 for (
int i = 0; i < indent_level; ++i) {
959 buf <<
"if (nargs == " <<
N <<
") {\n";
963 for (
int i = 0; i < indent_level; ++i) {
971 "#pragma clang diagnostic pop";
977 ostringstream &buf,
int indent_level)
988 const FunctionDecl *FD =
GetDecl();
989 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
991 auto SpecMemKind =
fInterp->getSema().getSpecialMember(CD);
992 if ((
N == 0 && SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
994 (SpecMemKind == clang::Sema::CXXCopyConstructor || SpecMemKind == clang::Sema::CXXMoveConstructor))) {
1005 QualType QT = FD->getReturnType().getCanonicalType();
1006 if (QT->isVoidType()) {
1007 ostringstream typedefbuf;
1008 ostringstream callbuf;
1009 for (
int i = 0; i < indent_level; ++i) {
1012 make_narg_call(
"void",
N, typedefbuf, callbuf, class_name, indent_level);
1014 for (
int i = 0; i < indent_level; ++i) {
1017 callbuf <<
"return;\n";
1018 buf << typedefbuf.str() << callbuf.str();
1020 for (
int i = 0; i < indent_level; ++i) {
1026 bool isPointer =
false;
1028 buf <<
"if (ret) {\n";
1031 ostringstream typedefbuf;
1032 ostringstream callbuf;
1036 for (
int i = 0; i < indent_level; ++i) {
1039 callbuf <<
"new (ret) ";
1041 refType, isPointer, indent_level,
false);
1045 callbuf <<
"(" << type_name.c_str();
1049 }
else if (isPointer) {
1058 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1063 for (
int i = 0; i < indent_level; ++i) {
1066 callbuf <<
"return;\n";
1070 buf << typedefbuf.str() << callbuf.str();
1073 for (
int i = 0; i < indent_level; ++i) {
1077 for (
int i = 0; i < indent_level; ++i) {
1083 ostringstream typedefbuf;
1084 ostringstream callbuf;
1085 for (
int i = 0; i < indent_level; ++i) {
1088 callbuf <<
"(void)(";
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'!");
1600 case BuiltinType::Bool: {
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());
1616 case BuiltinType::UChar: {
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: {
1650 case BuiltinType::UShort: {
1654 vh_ary.push_back(vh);
1655 vp_ary.push_back(&vh_ary.back());
1658 case BuiltinType::UInt: {
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());
1666 case BuiltinType::ULong: {
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());
1718 case BuiltinType::Short: {
1722 vh_ary.push_back(vh);
1723 vp_ary.push_back(&vh_ary.back());
1726 case BuiltinType::Int: {
1730 vh_ary.push_back(vh);
1731 vp_ary.push_back(&vh_ary.back());
1734 case BuiltinType::Long: {
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'!");
1764 case BuiltinType::Float: {
1767 vh.u.flt = sv_to<float>(
fArgVals[i]);
1768 vh_ary.push_back(vh);
1769 vp_ary.push_back(&vh_ary.back());
1772 case BuiltinType::Double: {
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);
1881 return [
this](
void* address, cling::Value& ret) {
exec(address, 0); };
1888 case BuiltinType::Bool:
1889 return InitRetAndExecIntegral<bool>(QT, ret);
1891 case BuiltinType::Char_U:
1892 case BuiltinType::UChar:
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'!");
1908 case BuiltinType::UShort:
1909 return InitRetAndExecIntegral<unsigned short>(QT, ret);
1911 case BuiltinType::UInt:
1912 return InitRetAndExecIntegral<unsigned int>(QT, ret);
1914 case BuiltinType::ULong:
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);
1940 case BuiltinType::Short:
1941 return InitRetAndExecIntegral<short>(QT, ret);
1943 case BuiltinType::Int:
1944 return InitRetAndExecIntegral<int>(QT, ret);
1946 case BuiltinType::Long:
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'!");
1963 case BuiltinType::Float: {
1964 ret = cling::Value::Create<float>(QT.getAsOpaquePtr(), *
fInterp);
1965 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getFloat()); };
1968 case BuiltinType::Double: {
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()) {
1997 ret = cling::Value(QT, *
fInterp);
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()) {
2007 ret = cling::Value(QT, *
fInterp);
2008 return [
this](
void* address, cling::Value& ret) {
exec(address, ret.getPtr()); };
2011 ret = cling::Value(QT, *
fInterp);
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()) {
2018 ret = cling::Value(QT, *
fInterp);
2019 return [
this](
void* address, cling::Value& ret) {
exec(address, ret.getPtr()); };
2020 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
2024 ret = cling::Value(QT, *
fInterp);
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);
2042 ret = cling::Value(QT, *
fInterp);
2044 return [
this](
void* address, cling::Value& ret) {
exec(address, &ret.getPtr()); };
2046 QualType QT = FD->getReturnType().getCanonicalType();
2057 std::function<
void(
void*, cling::Value&)> execFunc;
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!");
2106 cling::Value *val =
reinterpret_cast<cling::Value *
>(interpVal->
GetValAddr());
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,...)
Use this function in case an error occurred.
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
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...