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"
76 using namespace clang;
89 indent(ostringstream &buf,
int indent_level)
91 for (
int i = 0; i < indent_level; ++i) {
103 ASTContext &
C = interp.getCI()->getASTContext();
105 if (E->EvaluateAsInt(res, C, Expr::SE_NoSideEffects)) {
111 V.getLL() = res.getSExtValue();
113 V.getULL() = res.getZExtValue();
118 PrintingPolicy Policy(C.getPrintingPolicy());
119 Policy.SuppressTagKeyword =
true;
120 Policy.SuppressUnwrittenScope =
false;
121 Policy.SuppressInitializers =
false;
122 Policy.AnonymousTagLocations =
false;
124 raw_string_ostream
out(buf);
125 E->printPretty(out, 0, Policy, 0);
129 interp.evaluate(buf, V);
133 template <
typename returnType>
136 QualType QT = val.getType().getCanonicalType();
137 if (
const BuiltinType *BT =
138 dyn_cast<BuiltinType>(&*QT)) {
146 switch (BT->getKind()) {
147 case BuiltinType::Void:
149 return (returnType) 0;
155 case BuiltinType::Char_U:
157 return (returnType) val.getULL();
160 case BuiltinType::WChar_U:
164 return (returnType)(wchar_t) val.getULL();
167 case BuiltinType::Char16:
168 case BuiltinType::Char32:
172 case BuiltinType::ULongLong:
173 return (returnType) val.getULL();
176 case BuiltinType::UInt128:
183 case BuiltinType::Char_S:
184 case BuiltinType::SChar:
185 return (returnType) val.getLL();
188 case BuiltinType::WChar_S:
192 return (returnType)(wchar_t) val.getLL();
198 case BuiltinType::LongLong:
199 return (returnType) val.getLL();
202 case BuiltinType::Int128:
205 case BuiltinType::Half:
210 return (returnType) val.getFloat();
213 return (returnType) val.getDouble();
215 case BuiltinType::LongDouble:
216 return (returnType) val.getLongDouble();
219 case BuiltinType::NullPtr:
220 return (returnType) 0;
227 if (QT->isPointerType() || QT->isArrayType() || QT->isRecordType() ||
228 QT->isReferenceType()) {
229 return (returnType)(long) val.getPtr();
231 if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
232 if (ET->getDecl()->getIntegerType()->hasSignedIntegerRepresentation())
233 return (returnType) val.getLL();
235 return (returnType) val.getULL();
237 if (QT->isMemberPointerType()) {
238 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
239 if (MPT->isMemberDataPointer()) {
240 return (returnType)(ptrdiff_t)val.getPtr();
242 return (returnType)(long) val.getPtr();
244 Error(
"TClingCallFunc::sv_to",
"Invalid Type!");
252 return sv_to<long long>(val);
255 unsigned long long sv_to_ulong_long(
const cling::Value &val)
257 return sv_to<unsigned long long>(val);
263 bool withAccessControl)
265 return fInterp->compileFunction(wrapper_name, wrapper,
false ,
270 ostringstream &callbuf,
string &type_name,
271 bool &isReference,
bool &isPointer,
int indent_level,
278 const FunctionDecl *FD = fMethod->GetMethodDecl();
279 PrintingPolicy Policy(FD->getASTContext().getPrintingPolicy());
281 if (QT->isRecordType() && forArgument) {
285 if (QT->isFunctionPointerType()) {
286 string fp_typedef_name;
289 nm <<
"FP" << gWrapperSerial++;
290 type_name = nm.str();
291 raw_string_ostream OS(fp_typedef_name);
292 QT.print(OS, Policy, type_name);
295 for (
int i = 0; i < indent_level; ++i) {
298 typedefbuf <<
"typedef " << fp_typedef_name <<
";\n";
300 }
else if (QT->isMemberPointerType()) {
301 string mp_typedef_name;
304 nm <<
"MP" << gWrapperSerial++;
305 type_name = nm.str();
306 raw_string_ostream OS(mp_typedef_name);
307 QT.print(OS, Policy, type_name);
310 for (
int i = 0; i < indent_level; ++i) {
313 typedefbuf <<
"typedef " << mp_typedef_name <<
";\n";
315 }
else if (QT->isPointerType()) {
317 QT = cast<clang::PointerType>(QT)->getPointeeType();
318 }
else if (QT->isReferenceType()) {
320 QT = cast<ReferenceType>(QT)->getPointeeType();
323 if (QT->isArrayType()) {
324 string ar_typedef_name;
327 ar <<
"AR" << gWrapperSerial++;
328 type_name = ar.str();
329 raw_string_ostream OS(ar_typedef_name);
330 QT.print(OS, Policy, type_name);
333 for (
int i = 0; i < indent_level; ++i) {
336 typedefbuf <<
"typedef " << ar_typedef_name <<
";\n";
343 ostringstream &callbuf,
const string &class_name,
350 const FunctionDecl *FD = fMethod->GetMethodDecl();
352 callbuf <<
"new " << class_name <<
"(";
353 for (
unsigned i = 0U; i <
N; ++i) {
354 const ParmVarDecl *PVD = FD->getParamDecl(i);
355 QualType Ty = PVD->getType();
356 QualType QT = Ty.getCanonicalType();
358 bool isReference =
false;
359 bool isPointer =
false;
360 collect_type_info(QT, typedefbuf, callbuf, type_name,
361 isReference, isPointer, indent_level,
true);
368 for (
int j = 0; j <= indent_level; ++j) {
374 callbuf <<
"(" << type_name.c_str() <<
"&)*(" << type_name.c_str() <<
"*)args["
376 }
else if (isPointer) {
377 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
380 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
387 ostringstream &callbuf,
const string &class_name,
395 const FunctionDecl *FD = fMethod->GetMethodDecl();
396 if (
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
399 callbuf <<
"((const " << class_name <<
"*)obj)->";
401 callbuf <<
"((" << class_name <<
"*)obj)->";
402 }
else if (
const NamedDecl *ND =
403 dyn_cast<NamedDecl>(FD->getDeclContext())) {
406 callbuf << class_name <<
"::";
412 llvm::raw_string_ostream stream(name);
413 FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(),
false);
415 callbuf << name <<
"(";
417 for (
unsigned i = 0U; i <
N; ++i) {
418 const ParmVarDecl *PVD = FD->getParamDecl(i);
419 QualType Ty = PVD->getType();
420 QualType QT = Ty.getCanonicalType();
422 bool isReference =
false;
423 bool isPointer =
false;
424 collect_type_info(QT, typedefbuf, callbuf, type_name,
425 isReference, isPointer, indent_level,
true);
432 for (
int j = 0; j <= indent_level; ++j) {
438 callbuf <<
"(" << type_name.c_str() <<
"&)*(" << type_name.c_str() <<
"*)args["
440 }
else if (isPointer) {
441 callbuf <<
"*(" << type_name.c_str() <<
"**)args["
446 callbuf <<
"*(" << type_name.c_str() <<
"*)args[" << i <<
"]";
453 ostringstream &buf,
int indent_level)
464 for (
int i = 0; i < indent_level; ++i) {
467 buf <<
"if (ret) {\n";
470 ostringstream typedefbuf;
471 ostringstream callbuf;
475 for (
int i = 0; i < indent_level; ++i) {
478 callbuf <<
"(*(" << class_name <<
"**)ret) = ";
482 make_narg_ctor(N, typedefbuf, callbuf, class_name, indent_level);
487 for (
int i = 0; i < indent_level; ++i) {
490 callbuf <<
"return;\n";
494 buf << typedefbuf.str() << callbuf.str();
497 for (
int i = 0; i < indent_level; ++i) {
501 for (
int i = 0; i < indent_level; ++i) {
507 ostringstream typedefbuf;
508 ostringstream callbuf;
509 for (
int i = 0; i < indent_level; ++i) {
512 make_narg_ctor(N, typedefbuf, callbuf, class_name, indent_level);
514 for (
int i = 0; i < indent_level; ++i) {
517 callbuf <<
"return;\n";
518 buf << typedefbuf.str() << callbuf.str();
521 for (
int i = 0; i < indent_level; ++i) {
528 ostringstream &buf,
int indent_level)
539 const FunctionDecl *FD = fMethod->GetMethodDecl();
540 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
542 make_narg_ctor_with_return(N, class_name, buf, indent_level);
545 QualType QT = FD->getReturnType().getCanonicalType();
546 if (QT->isVoidType()) {
547 ostringstream typedefbuf;
548 ostringstream callbuf;
549 for (
int i = 0; i < indent_level; ++i) {
552 make_narg_call(N, typedefbuf, callbuf, class_name, indent_level);
554 for (
int i = 0; i < indent_level; ++i) {
557 callbuf <<
"return;\n";
558 buf << typedefbuf.str() << callbuf.str();
560 for (
int i = 0; i < indent_level; ++i) {
563 buf <<
"if (ret) {\n";
566 ostringstream typedefbuf;
567 ostringstream callbuf;
571 for (
int i = 0; i < indent_level; ++i) {
574 callbuf <<
"new (ret) ";
576 bool isReference =
false;
577 bool isPointer =
false;
578 collect_type_info(QT, typedefbuf, callbuf, type_name,
579 isReference, isPointer, indent_level,
false);
583 callbuf <<
"(" << type_name.c_str();
586 }
else if (isPointer) {
594 make_narg_call(N, typedefbuf, callbuf, class_name, indent_level);
599 for (
int i = 0; i < indent_level; ++i) {
602 callbuf <<
"return;\n";
606 buf << typedefbuf.str() << callbuf.str();
609 for (
int i = 0; i < indent_level; ++i) {
613 for (
int i = 0; i < indent_level; ++i) {
619 ostringstream typedefbuf;
620 ostringstream callbuf;
621 for (
int i = 0; i < indent_level; ++i) {
624 make_narg_call(N, typedefbuf, callbuf, class_name, indent_level);
626 for (
int i = 0; i < indent_level; ++i) {
629 callbuf <<
"return;\n";
630 buf << typedefbuf.str() << callbuf.str();
633 for (
int i = 0; i < indent_level; ++i) {
644 const FunctionDecl *FD = fMethod->GetMethodDecl();
645 ASTContext &
Context = FD->getASTContext();
646 PrintingPolicy Policy(Context.getPrintingPolicy());
651 if (
const TypeDecl *TD =
652 dyn_cast<TypeDecl>(FD->getDeclContext())) {
654 QualType QT(TD->getTypeForDecl(), 0);
656 }
else if (
const NamedDecl *ND =
657 dyn_cast<NamedDecl>(FD->getDeclContext())) {
659 raw_string_ostream stream(class_name);
660 ND->getNameForDiagnostic(stream, Policy,
true);
667 bool needInstantiation =
false;
668 const FunctionDecl *Definition = 0;
669 if (!FD->isDefined(Definition)) {
670 FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
672 case FunctionDecl::TK_NonTemplate: {
683 case FunctionDecl::TK_FunctionTemplate: {
686 Error(
"TClingCallFunc::make_wrapper",
687 "Cannot make wrapper for a function template!");
691 case FunctionDecl::TK_MemberSpecialization: {
698 if (!FD->isTemplateInstantiation()) {
711 const FunctionDecl *Pattern =
712 FD->getTemplateInstantiationPattern();
714 Error(
"TClingCallFunc::make_wrapper",
715 "Cannot make wrapper for a member function "
716 "instantiation with no pattern!");
719 FunctionDecl::TemplatedKind PTK = Pattern->getTemplatedKind();
720 TemplateSpecializationKind PTSK =
721 Pattern->getTemplateSpecializationKind();
724 (PTK == FunctionDecl::TK_NonTemplate) ||
727 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
728 ((PTSK == TSK_Undeclared) ||
729 (PTSK == TSK_ExplicitSpecialization)))
735 }
else if (!Pattern->hasBody()) {
736 Error(
"TClingCallFunc::make_wrapper",
737 "Cannot make wrapper for a member function "
738 "instantiation with no body!");
741 if (FD->isImplicitlyInstantiable()) {
742 needInstantiation =
true;
746 case FunctionDecl::TK_FunctionTemplateSpecialization: {
751 if (!FD->isTemplateInstantiation()) {
764 const FunctionDecl *Pattern =
765 FD->getTemplateInstantiationPattern();
767 Error(
"TClingCallFunc::make_wrapper",
768 "Cannot make wrapper for a function template"
769 "instantiation with no pattern!");
772 FunctionDecl::TemplatedKind PTK = Pattern->getTemplatedKind();
773 TemplateSpecializationKind PTSK =
774 Pattern->getTemplateSpecializationKind();
777 (PTK == FunctionDecl::TK_NonTemplate) ||
780 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
781 ((PTSK == TSK_Undeclared) ||
782 (PTSK == TSK_ExplicitSpecialization)))
789 if (!Pattern->hasBody()) {
790 Error(
"TClingCallFunc::make_wrapper",
791 "Cannot make wrapper for a function template"
792 "instantiation with no body!");
795 if (FD->isImplicitlyInstantiable()) {
796 needInstantiation =
true;
800 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
808 if (!FD->isTemplateInstantiation()) {
821 const FunctionDecl *Pattern =
822 FD->getTemplateInstantiationPattern();
824 Error(
"TClingCallFunc::make_wrapper",
825 "Cannot make wrapper for a dependent function template"
826 "instantiation with no pattern!");
829 FunctionDecl::TemplatedKind PTK = Pattern->getTemplatedKind();
830 TemplateSpecializationKind PTSK =
831 Pattern->getTemplateSpecializationKind();
834 (PTK == FunctionDecl::TK_NonTemplate) ||
837 ((PTK != FunctionDecl::TK_FunctionTemplate) &&
838 ((PTSK == TSK_Undeclared) ||
839 (PTSK == TSK_ExplicitSpecialization)))
846 if (!Pattern->hasBody()) {
847 Error(
"TClingCallFunc::make_wrapper",
848 "Cannot make wrapper for a dependent function template"
849 "instantiation with no body!");
852 if (FD->isImplicitlyInstantiable()) {
853 needInstantiation =
true;
860 Error(
"TClingCallFunc::make_wrapper",
861 "Unhandled template kind!");
884 if (needInstantiation) {
885 clang::FunctionDecl *FDmod =
const_cast<clang::FunctionDecl *
>(FD);
886 clang::Sema &
S = fInterp->getSema();
888 cling::Interpreter::PushTransactionRAII RAII(fInterp);
889 S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
892 if (!FD->isDefined(Definition)) {
893 Error(
"TClingCallFunc::make_wrapper",
894 "Failed to force template instantiation!");
899 FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
901 case FunctionDecl::TK_NonTemplate: {
903 if (Definition->isDeleted()) {
904 Error(
"TClingCallFunc::make_wrapper",
905 "Cannot make wrapper for a deleted function!");
907 }
else if (Definition->isLateTemplateParsed()) {
908 Error(
"TClingCallFunc::make_wrapper",
909 "Cannot make wrapper for a late template parsed "
921 case FunctionDecl::TK_FunctionTemplate: {
924 Error(
"TClingCallFunc::make_wrapper",
925 "Cannot make wrapper for a function template!");
929 case FunctionDecl::TK_MemberSpecialization: {
933 if (Definition->isDeleted()) {
934 Error(
"TClingCallFunc::make_wrapper",
935 "Cannot make wrapper for a deleted member function "
936 "of a specialization!");
938 }
else if (Definition->isLateTemplateParsed()) {
939 Error(
"TClingCallFunc::make_wrapper",
940 "Cannot make wrapper for a late template parsed "
941 "member function of a specialization!");
952 case FunctionDecl::TK_FunctionTemplateSpecialization: {
957 if (Definition->isDeleted()) {
958 Error(
"TClingCallFunc::make_wrapper",
959 "Cannot make wrapper for a deleted function "
960 "template specialization!");
962 }
else if (Definition->isLateTemplateParsed()) {
963 Error(
"TClingCallFunc::make_wrapper",
964 "Cannot make wrapper for a late template parsed "
965 "function template specialization!");
976 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
984 if (Definition->isDeleted()) {
985 Error(
"TClingCallFunc::make_wrapper",
986 "Cannot make wrapper for a deleted dependent function "
987 "template specialization!");
989 }
else if (Definition->isLateTemplateParsed()) {
990 Error(
"TClingCallFunc::make_wrapper",
991 "Cannot make wrapper for a late template parsed "
992 "dependent function template specialization!");
1006 Error(
"TClingCallFunc::make_wrapper",
1007 "Unhandled template kind!");
1013 unsigned min_args = FD->getMinRequiredArguments();
1014 unsigned num_params = FD->getNumParams();
1018 string wrapper_name;
1026 buf <<
'_' << gWrapperSerial++;
1027 wrapper_name = buf.str();
1033 int indent_level = 0;
1035 buf <<
"#pragma clang diagnostic push\n"
1036 "#pragma clang diagnostic ignored \"-Wformat-security\"\n"
1037 "__attribute__((used)) "
1038 "extern \"C\" void ";
1039 buf << wrapper_name;
1040 buf <<
"(void* obj, int nargs, void** args, void* ret)\n"
1043 if (min_args == num_params) {
1045 make_narg_call_with_return(num_params, class_name, buf, indent_level);
1049 for (
unsigned N = min_args;
N <= num_params; ++
N) {
1050 for (
int i = 0; i < indent_level; ++i) {
1053 buf <<
"if (nargs == " <<
N <<
") {\n";
1055 make_narg_call_with_return(N, class_name, buf, indent_level);
1057 for (
int i = 0; i < indent_level; ++i) {
1065 "#pragma clang diagnostic pop";
1066 string wrapper(buf.str());
1071 void *
F = compile_wrapper(wrapper_name, wrapper);
1073 gWrapperStore.insert(make_pair(FD, F));
1075 Error(
"TClingCallFunc::make_wrapper",
1076 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1124 PrintingPolicy Policy(Context.getPrintingPolicy());
1125 Policy.SuppressTagKeyword =
true;
1126 Policy.SuppressUnwrittenScope =
true;
1131 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1133 QualType QT(TD->getTypeForDecl(), 0);
1135 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1137 raw_string_ostream stream(class_name);
1138 ND->getNameForDiagnostic(stream, Policy,
true);
1144 string wrapper_name;
1152 buf <<
'_' << gWrapperSerial++;
1153 wrapper_name = buf.str();
1158 int indent_level = 0;
1160 buf <<
"__attribute__((used)) ";
1161 buf <<
"extern \"C\" void ";
1162 buf << wrapper_name;
1163 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1174 indent(buf, indent_level);
1175 buf <<
"if (!arena) {\n";
1177 indent(buf, indent_level);
1178 buf <<
"if (!nary) {\n";
1180 indent(buf, indent_level);
1181 buf <<
"*ret = new " << class_name <<
";\n";
1183 indent(buf, indent_level);
1185 indent(buf, indent_level);
1188 indent(buf, indent_level);
1189 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1191 indent(buf, indent_level);
1194 indent(buf, indent_level);
1204 indent(buf, indent_level);
1207 indent(buf, indent_level);
1208 buf <<
"if (!nary) {\n";
1210 indent(buf, indent_level);
1211 buf <<
"*ret = new (arena) " << class_name <<
";\n";
1213 indent(buf, indent_level);
1215 indent(buf, indent_level);
1218 indent(buf, indent_level);
1219 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1221 indent(buf, indent_level);
1224 indent(buf, indent_level);
1230 string wrapper(buf.str());
1235 void *
F = compile_wrapper(wrapper_name, wrapper,
1238 gCtorWrapperStore.insert(make_pair(info->
GetDecl(),
F));
1240 Error(
"TClingCallFunc::make_ctor_wrapper",
1241 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1278 PrintingPolicy Policy(Context.getPrintingPolicy());
1279 Policy.SuppressTagKeyword =
true;
1280 Policy.SuppressUnwrittenScope =
true;
1285 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1287 QualType QT(TD->getTypeForDecl(), 0);
1289 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1291 raw_string_ostream stream(class_name);
1292 ND->getNameForDiagnostic(stream, Policy,
true);
1298 string wrapper_name;
1306 buf <<
'_' << gWrapperSerial++;
1307 wrapper_name = buf.str();
1312 int indent_level = 0;
1314 buf <<
"__attribute__((used)) ";
1315 buf <<
"extern \"C\" void ";
1316 buf << wrapper_name;
1317 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1328 indent(buf, indent_level);
1329 buf <<
"if (withFree) {\n";
1331 indent(buf, indent_level);
1332 buf <<
"if (!nary) {\n";
1334 indent(buf, indent_level);
1335 buf <<
"delete (" << class_name <<
"*) obj;\n";
1337 indent(buf, indent_level);
1339 indent(buf, indent_level);
1342 indent(buf, indent_level);
1343 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1345 indent(buf, indent_level);
1348 indent(buf, indent_level);
1361 indent(buf, indent_level);
1364 indent(buf, indent_level);
1365 buf <<
"typedef " << class_name <<
" Nm;\n";
1366 buf <<
"if (!nary) {\n";
1368 indent(buf, indent_level);
1369 buf <<
"((Nm*)obj)->~Nm();\n";
1371 indent(buf, indent_level);
1373 indent(buf, indent_level);
1376 indent(buf, indent_level);
1379 indent(buf, indent_level);
1380 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1382 indent(buf, indent_level);
1383 buf <<
"} while (nary);\n";
1385 indent(buf, indent_level);
1388 indent(buf, indent_level);
1394 string wrapper(buf.str());
1399 void *
F = compile_wrapper(wrapper_name, wrapper,
1402 gDtorWrapperStore.insert(make_pair(info->
GetDecl(),
F));
1404 Error(
"TClingCallFunc::make_dtor_wrapper",
1405 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1420 unsigned long long ull;
1442 SmallVector<ValHolder, 8> vh_ary;
1443 SmallVector<void *, 8> vp_ary;
1445 unsigned num_args = fArgVals.size();
1449 const FunctionDecl *FD = fMethod->GetMethodDecl();
1456 unsigned num_params = FD->getNumParams();
1458 if (num_args < FD->getMinRequiredArguments()) {
1459 Error(
"TClingCallFunc::exec",
1460 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1462 num_args, FD->getMinRequiredArguments());
1465 if (address == 0 && dyn_cast<CXXMethodDecl>(FD)
1466 && !(dyn_cast<CXXMethodDecl>(FD))->isStatic()
1467 && !dyn_cast<CXXConstructorDecl>(FD)) {
1468 Error(
"TClingCallFunc::exec",
1469 "The method %s is called without an object.",
1473 vh_ary.reserve(num_args);
1474 vp_ary.reserve(num_args);
1475 for (
unsigned i = 0U; i < num_args; ++i) {
1477 if (i < num_params) {
1478 const ParmVarDecl *PVD = FD->getParamDecl(i);
1479 Ty = PVD->getType();
1481 Ty = fArgVals[i].getType();
1483 QualType QT = Ty.getCanonicalType();
1484 if (
const BuiltinType *BT =
1485 dyn_cast<BuiltinType>(&*QT)) {
1493 switch (BT->getKind()) {
1497 case BuiltinType::Void: {
1499 Error(
"TClingCallFunc::exec(void*)",
1500 "Invalid type 'Void'!");
1510 vh.u.b = (bool) sv_to_ulong_long(fArgVals[i]);
1511 vh_ary.push_back(vh);
1512 vp_ary.push_back(&vh_ary.back());
1515 case BuiltinType::Char_U: {
1518 vh.u.c = (char) sv_to_ulong_long(fArgVals[i]);
1519 vh_ary.push_back(vh);
1520 vp_ary.push_back(&vh_ary.back());
1526 vh.u.uc = (
unsigned char) sv_to_ulong_long(fArgVals[i]);
1527 vh_ary.push_back(vh);
1528 vp_ary.push_back(&vh_ary.back());
1531 case BuiltinType::WChar_U: {
1536 vh.u.wc = (wchar_t) sv_to_ulong_long(fArgVals[i]);
1537 vh_ary.push_back(vh);
1538 vp_ary.push_back(&vh_ary.back());
1541 case BuiltinType::Char16: {
1549 case BuiltinType::Char32: {
1560 vh.u.us = (
unsigned short) sv_to_ulong_long(fArgVals[i]);
1561 vh_ary.push_back(vh);
1562 vp_ary.push_back(&vh_ary.back());
1568 vh.u.ui = (
unsigned int) sv_to_ulong_long(fArgVals[i]);
1569 vh_ary.push_back(vh);
1570 vp_ary.push_back(&vh_ary.back());
1576 vh.u.ul = (
unsigned long) sv_to_ulong_long(fArgVals[i]);
1577 vh_ary.push_back(vh);
1578 vp_ary.push_back(&vh_ary.back());
1581 case BuiltinType::ULongLong: {
1584 vh.u.ull = (
unsigned long long) sv_to_ulong_long(fArgVals[i]);
1585 vh_ary.push_back(vh);
1586 vp_ary.push_back(&vh_ary.back());
1589 case BuiltinType::UInt128: {
1599 case BuiltinType::Char_S: {
1602 vh.u.c = (char) sv_to_long_long(fArgVals[i]);
1603 vh_ary.push_back(vh);
1604 vp_ary.push_back(&vh_ary.back());
1607 case BuiltinType::SChar: {
1610 vh.u.sc = (
signed char) sv_to_long_long(fArgVals[i]);
1611 vh_ary.push_back(vh);
1612 vp_ary.push_back(&vh_ary.back());
1615 case BuiltinType::WChar_S: {
1620 vh.u.wc = (wchar_t) sv_to_long_long(fArgVals[i]);
1621 vh_ary.push_back(vh);
1622 vp_ary.push_back(&vh_ary.back());
1628 vh.u.s = (short) sv_to_long_long(fArgVals[i]);
1629 vh_ary.push_back(vh);
1630 vp_ary.push_back(&vh_ary.back());
1636 vh.u.i = (int) sv_to_long_long(fArgVals[i]);
1637 vh_ary.push_back(vh);
1638 vp_ary.push_back(&vh_ary.back());
1644 vh.u.l = (long) sv_to_long_long(fArgVals[i]);
1645 vh_ary.push_back(vh);
1646 vp_ary.push_back(&vh_ary.back());
1649 case BuiltinType::LongLong: {
1652 vh.u.ll = (
long long) sv_to_long_long(fArgVals[i]);
1653 vh_ary.push_back(vh);
1654 vp_ary.push_back(&vh_ary.back());
1657 case BuiltinType::Int128: {
1659 Error(
"TClingCallFunc::exec(void*)",
1660 "Invalid type 'Int128'!");
1664 case BuiltinType::Half: {
1666 Error(
"TClingCallFunc::exec(void*)",
1667 "Invalid type 'Half'!");
1674 vh.u.flt = sv_to<float>(fArgVals[i]);
1675 vh_ary.push_back(vh);
1676 vp_ary.push_back(&vh_ary.back());
1682 vh.u.dbl = sv_to<double>(fArgVals[i]);
1683 vh_ary.push_back(vh);
1684 vp_ary.push_back(&vh_ary.back());
1687 case BuiltinType::LongDouble: {
1690 vh.u.ldbl = sv_to<long double>(fArgVals[i]);
1691 vh_ary.push_back(vh);
1692 vp_ary.push_back(&vh_ary.back());
1698 case BuiltinType::NullPtr: {
1701 vh.u.vp = fArgVals[i].getPtr();
1702 vh_ary.push_back(vh);
1703 vp_ary.push_back(&vh_ary.back());
1706 case BuiltinType::ObjCId: {
1708 Error(
"TClingCallFunc::exec(void*)",
1709 "Invalid type 'ObjCId'!");
1713 case BuiltinType::ObjCClass: {
1715 Error(
"TClingCallFunc::exec(void*)",
1716 "Invalid type 'ObjCClass'!");
1720 case BuiltinType::ObjCSel: {
1722 Error(
"TClingCallFunc::exec(void*)",
1723 "Invalid type 'ObjCSel'!");
1727 case BuiltinType::OCLImage1d: {
1729 Error(
"TClingCallFunc::exec(void*)",
1730 "Invalid type 'OCLImage1d'!");
1734 case BuiltinType::OCLImage1dArray: {
1736 Error(
"TClingCallFunc::exec(void*)",
1737 "Invalid type 'OCLImage1dArray'!");
1741 case BuiltinType::OCLImage1dBuffer: {
1743 Error(
"TClingCallFunc::exec(void*)",
1744 "Invalid type 'OCLImage1dBuffer'!");
1748 case BuiltinType::OCLImage2d: {
1750 Error(
"TClingCallFunc::exec(void*)",
1751 "Invalid type 'OCLImage2d'!");
1755 case BuiltinType::OCLImage2dArray: {
1757 Error(
"TClingCallFunc::exec(void*)",
1758 "Invalid type 'OCLImage2dArray'!");
1762 case BuiltinType::OCLImage3d: {
1764 Error(
"TClingCallFunc::exec(void*)",
1765 "Invalid type 'OCLImage3d'!");
1769 case BuiltinType::OCLSampler: {
1771 Error(
"TClingCallFunc::exec(void*)",
1772 "Invalid type 'OCLSampler'!");
1776 case BuiltinType::OCLEvent: {
1778 Error(
"TClingCallFunc::exec(void*)",
1779 "Invalid type 'OCLEvent'!");
1790 case BuiltinType::Dependent: {
1792 Error(
"TClingCallFunc::exec(void*)",
1793 "Invalid type 'Dependent'!");
1797 case BuiltinType::Overload: {
1799 Error(
"TClingCallFunc::exec(void*)",
1800 "Invalid type 'Overload'!");
1804 case BuiltinType::BoundMember: {
1806 Error(
"TClingCallFunc::exec(void*)",
1807 "Invalid type 'BoundMember'!");
1811 case BuiltinType::PseudoObject: {
1813 Error(
"TClingCallFunc::exec(void*)",
1814 "Invalid type 'PseudoObject'!");
1818 case BuiltinType::UnknownAny: {
1820 Error(
"TClingCallFunc::exec(void*)",
1821 "Invalid type 'UnknownAny'!");
1825 case BuiltinType::BuiltinFn: {
1827 Error(
"TClingCallFunc::exec(void*)",
1828 "Invalid type 'BuiltinFn'!");
1832 case BuiltinType::ARCUnbridgedCast: {
1836 Error(
"TClingCallFunc::exec(void*)",
1837 "Invalid type 'ARCUnbridgedCast'!");
1844 Error(
"TClingCallFunc::exec(void*)",
1845 "Invalid builtin type (unrecognized)!");
1851 }
else if (QT->isReferenceType()) {
1854 vp_ary.push_back((
void *) sv_to_ulong_long(fArgVals[i]));
1855 }
else if (QT->isPointerType() || QT->isArrayType()) {
1857 vh.u.vp = (
void *) sv_to_ulong_long(fArgVals[i]);
1858 vh_ary.push_back(vh);
1859 vp_ary.push_back(&vh_ary.back());
1860 }
else if (QT->isRecordType()) {
1863 vp_ary.push_back((
void *) sv_to_ulong_long(fArgVals[i]));
1864 }
else if (
const EnumType *ET =
1865 dyn_cast<EnumType>(&*QT)) {
1870 vh.u.i = (int) sv_to_long_long(fArgVals[i]);
1871 vh_ary.push_back(vh);
1872 vp_ary.push_back(&vh_ary.back());
1873 }
else if (QT->isMemberPointerType()) {
1875 vh.u.vp = (
void *) sv_to_ulong_long(fArgVals[i]);
1876 vh_ary.push_back(vh);
1877 vp_ary.push_back(&vh_ary.back());
1879 Error(
"TClingCallFunc::exec(void*)",
1880 "Invalid type (unrecognized)!");
1886 (*fWrapper)(address, (int)num_args, (
void **)vp_ary.data(), ret);
1889 template <
typename T>
1894 exec(address, &ret);
1898 template <
typename T>
1903 exec(address, &ret);
1904 val->getULL() = ret;
1916 const FunctionDecl *FD = fMethod->GetMethodDecl();
1918 if (
const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
1919 ASTContext &
Context = FD->getASTContext();
1920 const TypeDecl *TD = dyn_cast<TypeDecl>(CD->getDeclContext());
1921 QualType ClassTy(TD->getTypeForDecl(), 0);
1922 QualType QT = Context.getLValueReferenceType(ClassTy);
1926 exec(address, &ret->getPtr());
1929 QualType QT = FD->getReturnType().getCanonicalType();
1930 if (QT->isReferenceType()) {
1933 exec(address, &ret->getPtr());
1935 }
else if (QT->isMemberPointerType()) {
1936 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
1937 if (MPT->isMemberDataPointer()) {
1945 exec(address, ret->getPtr());
1951 exec(address, &ret->getPtr());
1953 }
else if (QT->isPointerType() || QT->isArrayType()) {
1957 exec(address, &ret->getPtr());
1959 }
else if (QT->isRecordType()) {
1962 exec(address, ret->getPtr());
1964 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
1970 execWithLL<int>(address, QT, ret);
1972 }
else if (
const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
1974 switch (BT->getKind()) {
1975 case BuiltinType::Void:
1986 execWithULL<bool>(address, QT, ret);
1989 case BuiltinType::Char_U:
1992 execWithULL<char>(address, QT, ret);
1995 case BuiltinType::WChar_U:
2000 execWithULL<wchar_t>(address, QT, ret);
2003 case BuiltinType::Char16:
2004 Error(
"TClingCallFunc::exec_with_valref_return",
2005 "Invalid type 'char16_t'!");
2008 case BuiltinType::Char32:
2009 Error(
"TClingCallFunc::exec_with_valref_return",
2010 "Invalid type 'char32_t'!");
2015 execWithULL<unsigned short>(address, QT, ret);
2020 execWithULL<unsigned int>(address, QT, ret);
2025 execWithULL<unsigned long>(address, QT, ret);
2028 case BuiltinType::ULongLong:
2030 execWithULL<unsigned long long>(address, QT, ret);
2033 case BuiltinType::UInt128: {
2034 Error(
"TClingCallFunc::exec_with_valref_return",
2035 "Invalid type '__uint128_t'!");
2043 case BuiltinType::Char_S:
2044 case BuiltinType::SChar:
2046 execWithLL<signed char>(address, QT, ret);
2049 case BuiltinType::WChar_S:
2054 execWithLL<wchar_t>(address, QT, ret);
2059 execWithLL<short>(address, QT, ret);
2064 execWithLL<int>(address, QT, ret);
2069 execWithLL<long>(address, QT, ret);
2072 case BuiltinType::LongLong:
2074 execWithLL<long long>(address, QT, ret);
2077 case BuiltinType::Int128:
2078 Error(
"TClingCallFunc::exec_with_valref_return",
2079 "Invalid type '__int128_t'!");
2082 case BuiltinType::Half:
2084 Error(
"TClingCallFunc::exec_with_valref_return",
2085 "Invalid type 'Half'!");
2090 exec(address, &ret->getFloat());
2095 exec(address, &ret->getDouble());
2098 case BuiltinType::LongDouble:
2100 exec(address, &ret->getLongDouble());
2106 case BuiltinType::NullPtr:
2108 Error(
"TClingCallFunc::exec_with_valref_return",
2109 "Invalid type 'nullptr'!");
2116 Error(
"TClingCallFunc::exec_with_valref_return",
2117 "Unrecognized return type!");
2124 SmallVector<Expr *, 4> exprs;
2125 fInterp->getLookupHelper().findArgList(ArgList, exprs,
2126 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
2127 : cling::LookupHelper::NoDiagnostics);
2128 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
2129 E = exprs.end();
I !=
E; ++
I) {
2132 if (!val.isValid()) {
2134 Error(
"TClingCallFunc::EvaluateArgList",
2135 "Bad expression in parameter %d of '%s'!",
2136 (
int)(
I - exprs.begin()),
2140 fArgVals.push_back(val);
2148 Error(
"TClingCallFunc::Exec(address, interpVal)",
2149 "Called with no wrapper, not implemented!");
2157 exec_with_valref_return(address, val);
2160 template <
typename T>
2165 Error(
"TClingCallFunc::ExecT",
2166 "Called with no wrapper, not implemented!");
2170 exec_with_valref_return(address, &ret);
2171 if (!ret.isValid()) {
2176 if (fReturnIsRecordType)
2178 return sv_to<T>(ret);
2183 return ExecT<long>(address);
2188 return ExecT<long long>(address);
2193 return ExecT<double>(address);
2197 int nargs ,
void *ret)
2201 Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2202 "Called with no wrapper, not implemented!");
2205 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
2212 Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
2213 "Called with no wrapper, not implemented!");
2220 unsigned long nary )
2223 Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
2229 const Decl *D = info->
GetDecl();
2238 map<const Decl *, void *>::iterator
I = gCtorWrapperStore.find(D);
2239 if (I != gCtorWrapperStore.end()) {
2242 wrapper = make_ctor_wrapper(info);
2246 Error(
"TClingCallFunc::ExecDefaultConstructor",
2247 "Called with no wrapper, not implemented!");
2251 (*wrapper)(&
obj, address, nary);
2256 unsigned long nary ,
bool withFree )
2259 Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
2266 const Decl *D = info->
GetDecl();
2267 map<const Decl *, void *>::iterator
I = gDtorWrapperStore.find(D);
2268 if (I != gDtorWrapperStore.end()) {
2271 wrapper = make_dtor_wrapper(info);
2275 Error(
"TClingCallFunc::ExecDestructor",
2276 "Called with no wrapper, not implemented!");
2279 (*wrapper)(address, nary, withFree);
2310 const FunctionDecl *decl = fMethod->GetMethodDecl();
2313 map<const FunctionDecl *, void *>::iterator
I = gWrapperStore.find(decl);
2314 if (I != gWrapperStore.end()) {
2317 fWrapper = make_wrapper();
2320 return (
void *)fWrapper;
2328 return fMethod->IsValid();
2334 Error(
"TClingCallFunc::IFacePtr(kind)",
2335 "Attempt to get interface while invalid.");
2339 const FunctionDecl *decl = fMethod->GetMethodDecl();
2342 map<const FunctionDecl *, void *>::iterator
I = gWrapperStore.find(decl);
2343 if (I != gWrapperStore.end()) {
2346 fWrapper = make_wrapper();
2349 fReturnIsRecordType = decl->getReturnType().getCanonicalType()->isRecordType();
2362 ASTContext &
C = fInterp->getCI()->getASTContext();
2363 fArgVals.push_back(
cling::Value(C.UnsignedLongTy, *fInterp));
2364 fArgVals.back().getLL() = param;
2369 ASTContext &
C = fInterp->getCI()->getASTContext();
2371 fArgVals.back().getLL() = param;
2376 ASTContext &
C = fInterp->getCI()->getASTContext();
2378 fArgVals.back().getFloat() = param;
2383 ASTContext &
C = fInterp->getCI()->getASTContext();
2384 fArgVals.push_back(
cling::Value(C.DoubleTy, *fInterp));
2385 fArgVals.back().getDouble() = param;
2390 ASTContext &
C = fInterp->getCI()->getASTContext();
2391 fArgVals.push_back(
cling::Value(C.LongLongTy, *fInterp));
2392 fArgVals.back().getLL() = param;
2397 ASTContext &
C = fInterp->getCI()->getASTContext();
2398 fArgVals.push_back(
cling::Value(C.UnsignedLongLongTy, *fInterp));
2399 fArgVals.back().getULL() = param;
2405 for (
int i = 0; i < nparam; ++i) {
2406 SetArg(paramArr[i]);
2413 EvaluateArgList(params);
2419 SetFunc(info, method, arglist,
false, poffset);
2423 bool objectIsConst,
long *poffset)
2433 Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
2436 if (!strcmp(arglist,
")")) {
2441 if (!fMethod->IsValid()) {
2449 EvaluateArgList(arglist);
2458 if (!fMethod->IsValid()) {
2464 const char *proto,
long *poffset,
2467 SetFuncProto(info, method, proto,
false, poffset, mode);
2471 const char *proto,
bool objectIsConst,
long *poffset,
2482 Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2485 *fMethod = info->
GetMethod(method, proto, objectIsConst, poffset, mode);
2486 if (!fMethod->IsValid()) {
2494 const llvm::SmallVectorImpl<clang::QualType> &proto,
long *poffset,
2497 SetFuncProto(info, method, proto,
false, poffset, mode);
2501 const llvm::SmallVectorImpl<clang::QualType> &proto,
2502 bool objectIsConst,
long *poffset,
2512 Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2515 *fMethod = info->
GetMethod(method, proto, objectIsConst, poffset, mode);
2516 if (!fMethod->IsValid()) {
void collect_type_info(clang::QualType &QT, std::ostringstream &typedefbuf, std::ostringstream &callbuf, std::string &type_name, bool &isReference, bool &isPointer, int indent_level, bool forArgument)
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
Namespace for new ROOT classes and functions.
RooArgList L(const RooAbsArg &v1)
double ExecDouble(void *address)
R__EXTERN TVirtualMutex * gInterpreterMutex
static map< const FunctionDecl *, void * > gWrapperStore
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, long *poffset)
void SetArgArray(long *argArr, int narg)
Small helper to keep current directory context.
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
Emulation of the CINT MethodInfo class.
static map< const Decl *, void * > gDtorWrapperStore
void execWithLL(void *address, clang::QualType QT, cling::Value *val) const
TClingMethodInfo GetMethod(const char *fname) const
static map< const Decl *, void * > gCtorWrapperStore
void exec_with_valref_return(void *address, cling::Value *ret) const
void Exec(void *address, TInterpreterValue *interpVal=0)
TInterpreter::CallFuncIFacePtr_t IFacePtr()
static unsigned long long gWrapperSerial
void SetArgs(const char *args)
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
This class defines an interface to the cling C++ interpreter.
void exec(void *address, void *ret) const
void execWithULL(void *address, clang::QualType QT, cling::Value *val) const
static const string kIndentString(" ")
TClingMethodInfo * FactoryMethod() const
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
#define R__LOCKGUARD_UNLOCK(name)
void EvaluateArgList(const std::string &ArgList)
void make_narg_ctor(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
static void indent(ostringstream &buf, int indent_level)
static void EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
const clang::Decl * GetDecl() const
tcling_callfunc_Wrapper_t make_wrapper()
#define R__LOCKGUARD_NAMED(name, mutex)
virtual const void * GetValAddr() const =0
void make_narg_call_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
Emulation of the CINT ClassInfo class.
#define R__LOCKGUARD(mutex)
void * ExecDefaultConstructor(const TClingClassInfo *info, void *address=0, unsigned long nary=0UL)
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
typedef void((*Func_t)())
TClingMethodInfo GetMethodWithArgs(const char *fname, const char *arglist, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch, EInheritanceMode imode=kWithInheritance) const
void ExecDestructor(const TClingClassInfo *info, void *address=0, unsigned long nary=0UL, bool withFree=true)
void make_narg_ctor_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
long ExecInt(void *address)
R__EXTERN TInterpreter * gCling
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *info)
void make_narg_call(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.
void ExecWithReturn(void *address, void *ret=0)
long long ExecInt64(void *address)