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"
74 using namespace clang;
87 indent(ostringstream &buf,
int indent_level)
89 for (
int i = 0; i < indent_level; ++i) {
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();
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>
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;
152 case BuiltinType::Char_U:
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:
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();
195 case BuiltinType::LongLong:
196 return (returnType) val.getLL();
199 case BuiltinType::Int128:
202 case BuiltinType::Half:
207 return (returnType) val.getFloat();
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!");
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());
282 refType = kNotReference;
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()) {
321 if (QT->isRValueReferenceType()) refType = kRValueReference;
322 else refType = kLValueReference;
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;
363 collect_type_info(QT, typedefbuf, callbuf, type_name,
364 refType, isPointer, indent_level,
true);
371 for (
int j = 0; j <= indent_level; ++j) {
376 if (refType != kNotReference) {
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 =
452 dyn_cast<NamedDecl>(GetDeclContext())) {
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) {
490 if (refType != kNotReference) {
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) = ";
536 make_narg_ctor(
N, typedefbuf, callbuf, class_name, indent_level);
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) {
566 make_narg_ctor(
N, typedefbuf, callbuf, class_name, indent_level);
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());
602 const clang::DeclContext *DC = GetDeclContext();
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!");
920 unsigned min_args = GetMinRequiredArguments();
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) {
951 make_narg_call_with_return(num_params, class_name, buf, indent_level);
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";
961 make_narg_call_with_return(
N, class_name, buf, indent_level);
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)) {
990 if (
N <= 1 && llvm::isa<UsingShadowDecl>(GetFunctionOrShadowDecl())) {
991 auto SpecMemKind = fInterp->getSema().getSpecialMember(CD);
992 if ((
N == 0 && SpecMemKind == clang::Sema::CXXDefaultConstructor) ||
994 (SpecMemKind == clang::Sema::CXXCopyConstructor || SpecMemKind == clang::Sema::CXXMoveConstructor))) {
1002 make_narg_ctor_with_return(
N, class_name, buf, indent_level);
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) ";
1040 collect_type_info(QT, typedefbuf, callbuf, type_name,
1041 refType, isPointer, indent_level,
false);
1045 callbuf <<
"(" << type_name.c_str();
1046 if (refType != kNotReference) {
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 make_narg_call(type_name,
N, typedefbuf, callbuf, class_name, indent_level);
1090 for (
int i = 0; i < indent_level; ++i) {
1093 callbuf <<
"return;\n";
1094 buf << typedefbuf.str() << callbuf.str();
1097 for (
int i = 0; i < indent_level; ++i) {
1108 const Decl *D = GetFunctionOrShadowDecl();
1109 string wrapper_name;
1112 if (get_wrapper_code(wrapper_name, wrapper) == 0)
return 0;
1118 void *
F = compile_wrapper(wrapper_name, wrapper);
1122 ::Error(
"TClingCallFunc::make_wrapper",
1123 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1200 ASTContext &Context = info->
GetDecl()->getASTContext();
1201 PrintingPolicy Policy(Context.getPrintingPolicy());
1202 Policy.SuppressTagKeyword =
true;
1203 Policy.SuppressUnwrittenScope =
true;
1208 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1210 QualType QT(TD->getTypeForDecl(), 0);
1212 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1214 raw_string_ostream stream(class_name);
1215 ND->getNameForDiagnostic(stream, Policy,
true);
1223 string wrapper_name;
1232 wrapper_name = buf.str();
1237 constr_arg = string(
"((") + type_name +
"*)nullptr)";
1239 constr_arg = string(
"(*((") + type_name +
"*)arena))";
1244 int indent_level = 0;
1246 buf <<
"__attribute__((used)) ";
1247 buf <<
"extern \"C\" void ";
1248 buf << wrapper_name;
1249 buf <<
"(void** ret, void* arena, unsigned long nary)\n";
1260 indent(buf, ++indent_level);
1261 buf <<
"if (!arena) {\n";
1262 indent(buf, ++indent_level);
1263 buf <<
"if (!nary) {\n";
1264 indent(buf, ++indent_level);
1265 buf <<
"*ret = new " << class_name << constr_arg <<
";\n";
1266 indent(buf, --indent_level);
1268 indent(buf, indent_level);
1270 indent(buf, ++indent_level);
1271 if (constr_arg.empty()) {
1272 buf <<
"*ret = new " << class_name <<
"[nary];\n";
1274 buf <<
"char *buf = (char *) malloc(nary * sizeof(" << class_name <<
"));\n";
1275 indent(buf, indent_level);
1276 buf <<
"for (int k=0;k<nary;++k)\n";
1277 indent(buf, ++indent_level);
1278 buf <<
"new (buf + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1279 indent(buf, --indent_level);
1280 buf <<
"*ret = buf;\n";
1282 indent(buf, --indent_level);
1284 indent(buf, --indent_level);
1294 indent(buf, indent_level);
1296 indent(buf, ++indent_level);
1297 buf <<
"if (!nary) {\n";
1298 indent(buf, ++indent_level);
1299 buf <<
"*ret = new (arena) " << class_name << constr_arg <<
";\n";
1300 indent(buf, --indent_level);
1302 indent(buf, indent_level);
1304 indent(buf, ++indent_level);
1305 if (constr_arg.empty()) {
1306 buf <<
"*ret = new (arena) " << class_name <<
"[nary];\n";
1308 buf <<
"for (int k=0;k<nary;++k)\n";
1309 indent(buf, ++indent_level);
1310 buf <<
"new ((char *) arena + k * sizeof(" << class_name <<
")) " << class_name << constr_arg <<
";\n";
1311 indent(buf, --indent_level);
1312 buf <<
"*ret = arena;\n";
1314 indent(buf, --indent_level);
1316 indent(buf, --indent_level);
1322 string wrapper(buf.str());
1327 void *
F = compile_wrapper(wrapper_name, wrapper,
1332 ::Error(
"TClingCallFunc::make_ctor_wrapper",
1333 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1369 ASTContext &Context = info->
GetDecl()->getASTContext();
1370 PrintingPolicy Policy(Context.getPrintingPolicy());
1371 Policy.SuppressTagKeyword =
true;
1372 Policy.SuppressUnwrittenScope =
true;
1377 if (
const TypeDecl *TD = dyn_cast<TypeDecl>(info->
GetDecl())) {
1379 QualType QT(TD->getTypeForDecl(), 0);
1381 }
else if (
const NamedDecl *ND = dyn_cast<NamedDecl>(info->
GetDecl())) {
1383 raw_string_ostream stream(class_name);
1384 ND->getNameForDiagnostic(stream, Policy,
true);
1390 string wrapper_name;
1399 wrapper_name = buf.str();
1404 int indent_level = 0;
1406 buf <<
"__attribute__((used)) ";
1407 buf <<
"extern \"C\" void ";
1408 buf << wrapper_name;
1409 buf <<
"(void* obj, unsigned long nary, int withFree)\n";
1420 indent(buf, indent_level);
1421 buf <<
"if (withFree) {\n";
1423 indent(buf, indent_level);
1424 buf <<
"if (!nary) {\n";
1426 indent(buf, indent_level);
1427 buf <<
"delete (" << class_name <<
"*) obj;\n";
1429 indent(buf, indent_level);
1431 indent(buf, indent_level);
1434 indent(buf, indent_level);
1435 buf <<
"delete[] (" << class_name <<
"*) obj;\n";
1437 indent(buf, indent_level);
1440 indent(buf, indent_level);
1453 indent(buf, indent_level);
1456 indent(buf, indent_level);
1457 buf <<
"typedef " << class_name <<
" Nm;\n";
1458 buf <<
"if (!nary) {\n";
1460 indent(buf, indent_level);
1461 buf <<
"((Nm*)obj)->~Nm();\n";
1463 indent(buf, indent_level);
1465 indent(buf, indent_level);
1468 indent(buf, indent_level);
1471 indent(buf, indent_level);
1472 buf <<
"(((Nm*)obj)+(--nary))->~Nm();\n";
1474 indent(buf, indent_level);
1475 buf <<
"} while (nary);\n";
1477 indent(buf, indent_level);
1480 indent(buf, indent_level);
1486 string wrapper(buf.str());
1491 void *
F = compile_wrapper(wrapper_name, wrapper,
1496 ::Error(
"TClingCallFunc::make_dtor_wrapper",
1497 "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1512 unsigned long long ull;
1534 SmallVector<ValHolder, 8> vh_ary;
1535 SmallVector<void *, 8> vp_ary;
1537 unsigned num_args = fArgVals.size();
1541 const FunctionDecl *FD = GetDecl();
1548 unsigned num_params = FD->getNumParams();
1550 if (num_args < GetMinRequiredArguments()) {
1551 ::Error(
"TClingCallFunc::exec",
1552 "Not enough arguments provided for %s (%d instead of the minimum %d)",
1554 num_args, (
int)GetMinRequiredArguments());
1557 if (address == 0 && dyn_cast<CXXMethodDecl>(FD)
1558 && !(dyn_cast<CXXMethodDecl>(FD))->isStatic()
1559 && !dyn_cast<CXXConstructorDecl>(FD)) {
1560 ::Error(
"TClingCallFunc::exec",
1561 "The method %s is called without an object.",
1565 vh_ary.reserve(num_args);
1566 vp_ary.reserve(num_args);
1567 for (
unsigned i = 0U; i < num_args; ++i) {
1569 if (i < num_params) {
1570 const ParmVarDecl *PVD = FD->getParamDecl(i);
1571 Ty = PVD->getType();
1573 Ty = fArgVals[i].getType();
1575 QualType QT = Ty.getCanonicalType();
1576 if (
const BuiltinType *BT =
1577 dyn_cast<BuiltinType>(&*QT)) {
1585 switch (BT->getKind()) {
1589 case BuiltinType::Void: {
1591 ::Error(
"TClingCallFunc::exec(void*)",
1592 "Invalid type 'Void'!");
1602 vh.u.b = (
bool) sv_to_ulong_long(fArgVals[i]);
1603 vh_ary.push_back(vh);
1604 vp_ary.push_back(&vh_ary.back());
1607 case BuiltinType::Char_U: {
1610 vh.u.c = (char) sv_to_ulong_long(fArgVals[i]);
1611 vh_ary.push_back(vh);
1612 vp_ary.push_back(&vh_ary.back());
1618 vh.u.uc = (
unsigned char) sv_to_ulong_long(fArgVals[i]);
1619 vh_ary.push_back(vh);
1620 vp_ary.push_back(&vh_ary.back());
1623 case BuiltinType::WChar_U: {
1628 vh.u.wc = (wchar_t) sv_to_ulong_long(fArgVals[i]);
1629 vh_ary.push_back(vh);
1630 vp_ary.push_back(&vh_ary.back());
1633 case BuiltinType::Char16: {
1641 case BuiltinType::Char32: {
1652 vh.u.us = (
unsigned short) sv_to_ulong_long(fArgVals[i]);
1653 vh_ary.push_back(vh);
1654 vp_ary.push_back(&vh_ary.back());
1660 vh.u.ui = (
unsigned int) sv_to_ulong_long(fArgVals[i]);
1661 vh_ary.push_back(vh);
1662 vp_ary.push_back(&vh_ary.back());
1668 vh.u.ul = (
unsigned long) sv_to_ulong_long(fArgVals[i]);
1669 vh_ary.push_back(vh);
1670 vp_ary.push_back(&vh_ary.back());
1673 case BuiltinType::ULongLong: {
1676 vh.u.ull = (
unsigned long long) sv_to_ulong_long(fArgVals[i]);
1677 vh_ary.push_back(vh);
1678 vp_ary.push_back(&vh_ary.back());
1681 case BuiltinType::UInt128: {
1691 case BuiltinType::Char_S: {
1694 vh.u.c = (char) sv_to_long_long(fArgVals[i]);
1695 vh_ary.push_back(vh);
1696 vp_ary.push_back(&vh_ary.back());
1699 case BuiltinType::SChar: {
1702 vh.u.sc = (
signed char) sv_to_long_long(fArgVals[i]);
1703 vh_ary.push_back(vh);
1704 vp_ary.push_back(&vh_ary.back());
1707 case BuiltinType::WChar_S: {
1712 vh.u.wc = (wchar_t) sv_to_long_long(fArgVals[i]);
1713 vh_ary.push_back(vh);
1714 vp_ary.push_back(&vh_ary.back());
1720 vh.u.s = (
short) sv_to_long_long(fArgVals[i]);
1721 vh_ary.push_back(vh);
1722 vp_ary.push_back(&vh_ary.back());
1728 vh.u.i = (
int) sv_to_long_long(fArgVals[i]);
1729 vh_ary.push_back(vh);
1730 vp_ary.push_back(&vh_ary.back());
1736 vh.u.l = (
long) sv_to_long_long(fArgVals[i]);
1737 vh_ary.push_back(vh);
1738 vp_ary.push_back(&vh_ary.back());
1741 case BuiltinType::LongLong: {
1744 vh.u.ll = (
long long) sv_to_long_long(fArgVals[i]);
1745 vh_ary.push_back(vh);
1746 vp_ary.push_back(&vh_ary.back());
1749 case BuiltinType::Int128: {
1751 ::Error(
"TClingCallFunc::exec(void*)",
1752 "Invalid type 'Int128'!");
1756 case BuiltinType::Half: {
1758 ::Error(
"TClingCallFunc::exec(void*)",
1759 "Invalid type 'Half'!");
1766 vh.u.flt = sv_to<float>(fArgVals[i]);
1767 vh_ary.push_back(vh);
1768 vp_ary.push_back(&vh_ary.back());
1774 vh.u.dbl = sv_to<double>(fArgVals[i]);
1775 vh_ary.push_back(vh);
1776 vp_ary.push_back(&vh_ary.back());
1779 case BuiltinType::LongDouble: {
1782 vh.u.ldbl = sv_to<long double>(fArgVals[i]);
1783 vh_ary.push_back(vh);
1784 vp_ary.push_back(&vh_ary.back());
1790 case BuiltinType::NullPtr: {
1793 vh.u.vp = fArgVals[i].getPtr();
1794 vh_ary.push_back(vh);
1795 vp_ary.push_back(&vh_ary.back());
1801 ::Error(
"TClingCallFunc::exec(void*)",
1802 "Unhandled builtin type!");
1808 }
else if (QT->isReferenceType()) {
1811 vp_ary.push_back((
void *) sv_to_ulong_long(fArgVals[i]));
1812 }
else if (QT->isPointerType() || QT->isArrayType()) {
1814 vh.u.vp = (
void *) sv_to_ulong_long(fArgVals[i]);
1815 vh_ary.push_back(vh);
1816 vp_ary.push_back(&vh_ary.back());
1817 }
else if (QT->isRecordType()) {
1820 vp_ary.push_back((
void *) sv_to_ulong_long(fArgVals[i]));
1821 }
else if (
const EnumType *ET =
1822 dyn_cast<EnumType>(&*QT)) {
1827 vh.u.i = (
int) sv_to_long_long(fArgVals[i]);
1828 vh_ary.push_back(vh);
1829 vp_ary.push_back(&vh_ary.back());
1830 }
else if (QT->isMemberPointerType()) {
1832 vh.u.vp = (
void *) sv_to_ulong_long(fArgVals[i]);
1833 vh_ary.push_back(vh);
1834 vp_ary.push_back(&vh_ary.back());
1836 ::Error(
"TClingCallFunc::exec(void*)",
1837 "Invalid type (unrecognized)!");
1843 (*fWrapper)(address, (
int)num_args, (
void **)vp_ary.data(), ret);
1846 template <
typename T>
1850 exec(address, &ret);
1854 template <
typename T>
1858 exec(address, &ret);
1859 val->getULL() = ret;
1866 ret = cling::Value::Create<T>(QT.getAsOpaquePtr(), *fInterp);
1867 static_assert(std::is_integral<T>::value,
"Must be called with integral T");
1868 if (std::is_signed<T>::value)
1869 return [
this](
void* address,
cling::Value& ret) { execWithLL<T>(address, &ret); };
1871 return [
this](
void* address,
cling::Value& ret) { execWithULL<T>(address, &ret); };
1877 switch (BT->getKind()) {
1878 case BuiltinType::Void: {
1879 ret = cling::Value::Create<void>(QT.getAsOpaquePtr(), *fInterp);
1880 return [
this](
void* address,
cling::Value& ret) { exec(address, 0); };
1888 return InitRetAndExecIntegral<bool>(QT, ret);
1890 case BuiltinType::Char_U:
1892 return InitRetAndExecIntegral<char>(QT, ret);
1894 case BuiltinType::WChar_U:
1895 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1897 case BuiltinType::Char16:
1898 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1899 "Invalid type 'char16_t'!");
1902 case BuiltinType::Char32:
1903 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1904 "Invalid type 'char32_t'!");
1908 return InitRetAndExecIntegral<unsigned short>(QT, ret);
1911 return InitRetAndExecIntegral<unsigned int>(QT, ret);
1914 return InitRetAndExecIntegral<unsigned long>(QT, ret);
1916 case BuiltinType::ULongLong:
1917 return InitRetAndExecIntegral<unsigned long long>(QT, ret);
1919 case BuiltinType::UInt128: {
1920 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1921 "Invalid type '__uint128_t'!");
1929 case BuiltinType::Char_S:
1930 case BuiltinType::SChar:
1931 return InitRetAndExecIntegral<signed char>(QT, ret);
1933 case BuiltinType::WChar_S:
1937 return InitRetAndExecIntegral<wchar_t>(QT, ret);
1940 return InitRetAndExecIntegral<short>(QT, ret);
1943 return InitRetAndExecIntegral<int>(QT, ret);
1946 return InitRetAndExecIntegral<long>(QT, ret);
1948 case BuiltinType::LongLong:
1949 return InitRetAndExecIntegral<long long>(QT, ret);
1951 case BuiltinType::Int128:
1952 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1953 "Invalid type '__int128_t'!");
1956 case BuiltinType::Half:
1958 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1959 "Invalid type 'Half'!");
1963 ret = cling::Value::Create<float>(QT.getAsOpaquePtr(), *fInterp);
1964 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getFloat()); };
1968 ret = cling::Value::Create<double>(QT.getAsOpaquePtr(), *fInterp);
1969 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getDouble()); };
1972 case BuiltinType::LongDouble: {
1973 ret = cling::Value::Create<long double>(QT.getAsOpaquePtr(), *fInterp);
1974 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getLongDouble()); };
1980 case BuiltinType::NullPtr:
1982 ::Error(
"TClingCallFunc::InitRetAndExecBuiltin",
1983 "Invalid type 'nullptr'!");
1995 if (QT->isReferenceType()) {
1997 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getPtr()); };
1998 }
else if (QT->isMemberPointerType()) {
1999 const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
2000 if (MPT->isMemberDataPointer()) {
2007 return [
this](
void* address,
cling::Value& ret) { exec(address, ret.getPtr()); };
2011 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getPtr()); };
2012 }
else if (QT->isPointerType() || QT->isArrayType()) {
2014 ret = cling::Value::Create<void*>(QT.getAsOpaquePtr(), *fInterp);
2015 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getPtr()); };
2016 }
else if (QT->isRecordType()) {
2018 return [
this](
void* address,
cling::Value& ret) { exec(address, ret.getPtr()); };
2019 }
else if (
const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
2024 return [
this](
void* address,
cling::Value& ret) { execWithLL<int>(address, &ret); };
2025 }
else if (
const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
2026 return InitRetAndExecBuiltin(QT, BT, ret);
2028 ::Error(
"TClingCallFunc::exec_with_valref_return",
2029 "Unrecognized return type!");
2036 if (llvm::isa<CXXConstructorDecl>(FD)) {
2037 ASTContext &Context = FD->getASTContext();
2038 const TypeDecl *TD = dyn_cast<TypeDecl>(GetDeclContext());
2039 QualType ClassTy(TD->getTypeForDecl(), 0);
2040 QualType QT = Context.getLValueReferenceType(ClassTy);
2043 return [
this](
void* address,
cling::Value& ret) { exec(address, &ret.getPtr()); };
2045 QualType QT = FD->getReturnType().getCanonicalType();
2046 return InitRetAndExecNoCtor(QT, ret);
2061 execFunc = InitRetAndExec(GetDecl(), *ret);
2065 execFunc(address, *ret);
2073 SmallVector<Expr *, 4> exprs;
2074 fInterp->getLookupHelper().findArgList(ArgList, exprs,
2075 gDebug > 5 ? cling::LookupHelper::WithDiagnostics
2076 : cling::LookupHelper::NoDiagnostics);
2077 for (SmallVectorImpl<Expr *>::const_iterator
I = exprs.begin(),
2078 E = exprs.end();
I !=
E; ++
I) {
2081 if (!val.isValid()) {
2083 ::Error(
"TClingCallFunc::EvaluateArgList",
2084 "Bad expression in parameter %d of '%s'!",
2085 (
int)(
I - exprs.begin()),
2089 fArgVals.push_back(val);
2097 ::Error(
"TClingCallFunc::Exec(address, interpVal)",
2098 "Called with no wrapper, not implemented!");
2106 exec_with_valref_return(address, val);
2109 template <
typename T>
2114 ::Error(
"TClingCallFunc::ExecT",
2115 "Called with no wrapper, not implemented!");
2119 exec_with_valref_return(address, &ret);
2120 if (!ret.isValid()) {
2125 if (fReturnIsRecordType)
2127 return sv_to<T>(ret);
2132 return ExecT<long>(address);
2137 return ExecT<long long>(address);
2142 return ExecT<double>(address);
2146 int nargs ,
void *ret)
2150 ::Error(
"TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2151 "Called with no wrapper, not implemented!");
2154 (*fWrapper)(address, nargs,
const_cast<void **
>(args), ret);
2161 ::Error(
"TClingCallFunc::ExecWithReturn(address, ret)",
2162 "Called with no wrapper, not implemented!");
2170 const std::string &type_name,
2171 void *address ,
unsigned long nary )
2174 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
"Invalid class info!");
2193 wrapper = make_ctor_wrapper(info, kind, type_name);
2197 ::Error(
"TClingCallFunc::ExecDefaultConstructor",
2198 "Called with no wrapper, not implemented!");
2202 (*wrapper)(&obj, address, nary);
2207 unsigned long nary ,
bool withFree )
2210 ::Error(
"TClingCallFunc::ExecDestructor",
"Invalid class info!");
2217 const Decl *D = info->
GetDecl();
2222 wrapper = make_dtor_wrapper(info);
2226 ::Error(
"TClingCallFunc::ExecDestructor",
2227 "Called with no wrapper, not implemented!");
2230 (*wrapper)(address, nary, withFree);
2244 fMinRequiredArguments = -1;
2257 fMethod = std::move(minfo);
2266 const Decl *decl = GetFunctionOrShadowDecl();
2269 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2273 fWrapper = make_wrapper();
2276 return (
void *)fWrapper;
2284 return fMethod->IsValid();
2290 ::Error(
"TClingCallFunc::IFacePtr(kind)",
2291 "Attempt to get interface while invalid.");
2295 const Decl *decl = GetFunctionOrShadowDecl();
2298 map<const Decl *, void *>::iterator
I =
gWrapperStore.find(decl);
2302 fWrapper = make_wrapper();
2305 fReturnIsRecordType = GetDecl()->getReturnType().getCanonicalType()->isRecordType();
2318 const ASTContext &
C = fInterp->getCI()->getASTContext();
2319 fArgVals.push_back(
cling::Value(
C.UnsignedLongTy, *fInterp));
2320 fArgVals.back().getLL() = param;
2325 const ASTContext &
C = fInterp->getCI()->getASTContext();
2327 fArgVals.back().getLL() = param;
2332 const ASTContext &
C = fInterp->getCI()->getASTContext();
2334 fArgVals.back().getFloat() = param;
2339 const ASTContext &
C = fInterp->getCI()->getASTContext();
2341 fArgVals.back().getDouble() = param;
2346 const ASTContext &
C = fInterp->getCI()->getASTContext();
2348 fArgVals.back().getLL() = param;
2353 const ASTContext &
C = fInterp->getCI()->getASTContext();
2354 fArgVals.push_back(
cling::Value(
C.UnsignedLongLongTy, *fInterp));
2355 fArgVals.back().getULL() = param;
2361 for (
int i = 0; i < nparam; ++i) {
2362 SetArg(paramArr[i]);
2369 EvaluateArgList(params);
2375 SetFunc(info, method, arglist,
false, poffset);
2379 bool objectIsConst,
long *poffset)
2387 ::Error(
"TClingCallFunc::SetFunc",
"Class info is invalid!");
2390 if (!strcmp(arglist,
")")) {
2395 if (!fMethod->IsValid()) {
2403 EvaluateArgList(arglist);
2410 if (!fMethod->IsValid()) {
2416 const char *
proto,
long *poffset,
2419 SetFuncProto(info, method,
proto,
false, poffset, mode);
2423 const char *
proto,
bool objectIsConst,
long *poffset,
2432 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2435 *fMethod = info->
GetMethod(method,
proto, objectIsConst, poffset, mode);
2436 if (!fMethod->IsValid()) {
2444 const llvm::SmallVectorImpl<clang::QualType> &
proto,
long *poffset,
2447 SetFuncProto(info, method,
proto,
false, poffset, mode);
2451 const llvm::SmallVectorImpl<clang::QualType> &
proto,
2452 bool objectIsConst,
long *poffset,
2461 ::Error(
"TClingCallFunc::SetFuncProto",
"Class info is invalid!");
2464 *fMethod = info->
GetMethod(method,
proto, objectIsConst, poffset, mode);
2465 if (!fMethod->IsValid()) {