Logo ROOT   6.08/07
Reference Guide
TClingCallFunc.cxx
Go to the documentation of this file.
1 // root/core/meta
2 // vim: sw=3
3 // Author: Paul Russo 30/07/2012
4 // Author: Vassil Vassilev 9/02/2013
5 
6 /*************************************************************************
7  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
8  * All rights reserved. *
9  * *
10  * For the licensing terms see $ROOTSYS/LICENSE. *
11  * For the list of contributors see $ROOTSYS/README/CREDITS. *
12  *************************************************************************/
13 
14 /** \class TClingCallFunc
15 Emulation of the CINT CallFunc class.
16 
17 The CINT C++ interpreter provides an interface for calling
18 functions through the generated wrappers in dictionaries with
19 the CallFunc class. This class provides the same functionality,
20 using an interface as close as possible to CallFunc but the
21 function metadata and calling service comes from the Cling
22 C++ interpreter and the Clang C++ compiler, not CINT.
23 */
24 
25 #include "TClingCallFunc.h"
26 
27 #include "TClingClassInfo.h"
28 #include "TClingMethodInfo.h"
29 #include "TInterpreterValue.h"
30 #include "TMetaUtils.h"
31 #include "TSystem.h"
32 
33 #include "TError.h"
34 #include "TCling.h"
35 
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"
42 
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"
54 
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"
66 
67 #include "clang/Sema/SemaInternal.h"
68 
69 #include <iomanip>
70 #include <map>
71 #include <string>
72 #include <sstream>
73 
74 using namespace ROOT;
75 using namespace llvm;
76 using namespace clang;
77 using namespace std;
78 
79 static unsigned long long gWrapperSerial = 0LL;
80 static const string kIndentString(" ");
81 
82 static map<const FunctionDecl *, void *> gWrapperStore;
83 static map<const Decl *, void *> gCtorWrapperStore;
84 static map<const Decl *, void *> gDtorWrapperStore;
85 
86 static
87 inline
88 void
89 indent(ostringstream &buf, int indent_level)
90 {
91  for (int i = 0; i < indent_level; ++i) {
92  buf << kIndentString;
93  }
94 }
95 
96 static
97 void
98 EvaluateExpr(cling::Interpreter &interp, const Expr *E, cling::Value &V)
99 {
101 
102  // Evaluate an Expr* and return its cling::Value
103  ASTContext &C = interp.getCI()->getASTContext();
104  APSInt res;
105  if (E->EvaluateAsInt(res, C, /*AllowSideEffects*/Expr::SE_NoSideEffects)) {
106  // IntTy or maybe better E->getType()?
107  V = cling::Value(C.IntTy, interp);
108  // We must use the correct signedness otherwise the zero extension
109  // fails if the actual type is strictly less than long long.
110  if (res.isSigned())
111  V.getLL() = res.getSExtValue();
112  else
113  V.getULL() = res.getZExtValue();
114  return;
115  }
116  // TODO: Build a wrapper around the expression to avoid decompilation and
117  // compilation and other string operations.
118  PrintingPolicy Policy(C.getPrintingPolicy());
119  Policy.SuppressTagKeyword = true;
120  Policy.SuppressUnwrittenScope = false;
121  Policy.SuppressInitializers = false;
122  Policy.AnonymousTagLocations = false;
123  string buf;
124  raw_string_ostream out(buf);
125  E->printPretty(out, /*Helper=*/0, Policy, /*Indentation=*/0);
126  out << ';'; // no value printing
127  out.flush();
128  // Evaluate() will set V to invalid if evaluation fails.
129  interp.evaluate(buf, V);
130 }
131 
132 namespace {
133  template <typename returnType>
134  returnType sv_to(const cling::Value &val)
135  {
136  QualType QT = val.getType().getCanonicalType();
137  if (const BuiltinType *BT =
138  dyn_cast<BuiltinType>(&*QT)) {
139  //
140  // WARNING!!!
141  //
142  // This switch is organized in order-of-declaration
143  // so that the produced assembly code is optimal.
144  // Do not reorder!
145  //
146  switch (BT->getKind()) {
147  case BuiltinType::Void:
148  // CINT used to expect a result of 0.
149  return (returnType) 0;
150  break;
151  //
152  // Unsigned Types
153  //
154  case BuiltinType::Bool:
155  case BuiltinType::Char_U: // char on targets where it is unsigned
156  case BuiltinType::UChar:
157  return (returnType) val.getULL();
158  break;
159 
160  case BuiltinType::WChar_U:
161  // wchar_t on targets where it is unsigned
162  // The standard doesn't allow to specify signednedd of wchar_t
163  // thus this maps simply to wchar_t.
164  return (returnType)(wchar_t) val.getULL();
165  break;
166 
167  case BuiltinType::Char16:
168  case BuiltinType::Char32:
169  case BuiltinType::UShort:
170  case BuiltinType::UInt:
171  case BuiltinType::ULong:
172  case BuiltinType::ULongLong:
173  return (returnType) val.getULL();
174  break;
175 
176  case BuiltinType::UInt128:
177  // __uint128_t
178  break;
179 
180  //
181  // Signed Types
182  //
183  case BuiltinType::Char_S: // char on targets where it is signed
184  case BuiltinType::SChar:
185  return (returnType) val.getLL();
186  break;
187 
188  case BuiltinType::WChar_S:
189  // wchar_t on targets where it is signed
190  // The standard doesn't allow to specify signednedd of wchar_t
191  // thus this maps simply to wchar_t.
192  return (returnType)(wchar_t) val.getLL();
193  break;
194 
195  case BuiltinType::Short:
196  case BuiltinType::Int:
197  case BuiltinType::Long:
198  case BuiltinType::LongLong:
199  return (returnType) val.getLL();
200  break;
201 
202  case BuiltinType::Int128:
203  break;
204 
205  case BuiltinType::Half:
206  // half in OpenCL, __fp16 in ARM NEON
207  break;
208 
209  case BuiltinType::Float:
210  return (returnType) val.getFloat();
211  break;
212  case BuiltinType::Double:
213  return (returnType) val.getDouble();
214  break;
215  case BuiltinType::LongDouble:
216  return (returnType) val.getLongDouble();
217  break;
218 
219  case BuiltinType::NullPtr:
220  return (returnType) 0;
221  break;
222 
223  default:
224  break;
225  }
226  }
227  if (QT->isPointerType() || QT->isArrayType() || QT->isRecordType() ||
228  QT->isReferenceType()) {
229  return (returnType)(long) val.getPtr();
230  }
231  if (const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
232  if (ET->getDecl()->getIntegerType()->hasSignedIntegerRepresentation())
233  return (returnType) val.getLL();
234  else
235  return (returnType) val.getULL();
236  }
237  if (QT->isMemberPointerType()) {
238  const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
239  if (MPT->isMemberDataPointer()) {
240  return (returnType)(ptrdiff_t)val.getPtr();
241  }
242  return (returnType)(long) val.getPtr();
243  }
244  ::Error("TClingCallFunc::sv_to", "Invalid Type!");
245  QT->dump();
246  return 0;
247  }
248 
249  static
250  long long sv_to_long_long(const cling::Value &val)
251  {
252  return sv_to<long long>(val);
253  }
254  static
255  unsigned long long sv_to_ulong_long(const cling::Value &val)
256  {
257  return sv_to<unsigned long long>(val);
258  }
259 
260 } // unnamed namespace.
261 
262 void *TClingCallFunc::compile_wrapper(const string &wrapper_name, const string &wrapper,
263  bool withAccessControl/*=true*/)
264 {
265  return fInterp->compileFunction(wrapper_name, wrapper, false /*ifUnique*/,
266  withAccessControl);
267 }
268 
269 void TClingCallFunc::collect_type_info(QualType &QT, ostringstream &typedefbuf,
270  ostringstream &callbuf, string &type_name,
271  bool &isReference, bool &isPointer, int indent_level,
272  bool forArgument)
273 {
274  //
275  // Collect information about type type of a function parameter
276  // needed for building the wrapper function.
277  //
278  const FunctionDecl *FD = fMethod->GetMethodDecl();
279  PrintingPolicy Policy(FD->getASTContext().getPrintingPolicy());
280  isReference = false;
281  if (QT->isRecordType() && forArgument) {
282  ROOT::TMetaUtils::GetNormalizedName(type_name, QT, *fInterp, fNormCtxt);
283  return;
284  }
285  if (QT->isFunctionPointerType()) {
286  string fp_typedef_name;
287  {
288  ostringstream nm;
289  nm << "FP" << gWrapperSerial++;
290  type_name = nm.str();
291  raw_string_ostream OS(fp_typedef_name);
292  QT.print(OS, Policy, type_name);
293  OS.flush();
294  }
295  for (int i = 0; i < indent_level; ++i) {
296  typedefbuf << kIndentString;
297  }
298  typedefbuf << "typedef " << fp_typedef_name << ";\n";
299  return;
300  } else if (QT->isMemberPointerType()) {
301  string mp_typedef_name;
302  {
303  ostringstream nm;
304  nm << "MP" << gWrapperSerial++;
305  type_name = nm.str();
306  raw_string_ostream OS(mp_typedef_name);
307  QT.print(OS, Policy, type_name);
308  OS.flush();
309  }
310  for (int i = 0; i < indent_level; ++i) {
311  typedefbuf << kIndentString;
312  }
313  typedefbuf << "typedef " << mp_typedef_name << ";\n";
314  return;
315  } else if (QT->isPointerType()) {
316  isPointer = true;
317  QT = cast<clang::PointerType>(QT)->getPointeeType();
318  } else if (QT->isReferenceType()) {
319  isReference = true;
320  QT = cast<ReferenceType>(QT)->getPointeeType();
321  }
322  // Fall through for the array type to deal with reference/pointer ro array type.
323  if (QT->isArrayType()) {
324  string ar_typedef_name;
325  {
326  ostringstream ar;
327  ar << "AR" << gWrapperSerial++;
328  type_name = ar.str();
329  raw_string_ostream OS(ar_typedef_name);
330  QT.print(OS, Policy, type_name);
331  OS.flush();
332  }
333  for (int i = 0; i < indent_level; ++i) {
334  typedefbuf << kIndentString;
335  }
336  typedefbuf << "typedef " << ar_typedef_name << ";\n";
337  return;
338  }
339  ROOT::TMetaUtils::GetNormalizedName(type_name, QT, *fInterp, fNormCtxt);
340 }
341 
342 void TClingCallFunc::make_narg_ctor(const unsigned N, ostringstream &typedefbuf,
343  ostringstream &callbuf, const string &class_name,
344  int indent_level)
345 {
346  // Make a code string that follows this pattern:
347  //
348  // new ClassName(args...)
349  //
350  const FunctionDecl *FD = fMethod->GetMethodDecl();
351 
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();
357  string type_name;
358  bool isReference = false;
359  bool isPointer = false;
360  collect_type_info(QT, typedefbuf, callbuf, type_name,
361  isReference, isPointer, indent_level, true);
362  if (i) {
363  callbuf << ',';
364  if (i % 2) {
365  callbuf << ' ';
366  } else {
367  callbuf << "\n";
368  for (int j = 0; j <= indent_level; ++j) {
369  callbuf << kIndentString;
370  }
371  }
372  }
373  if (isReference) {
374  callbuf << "(" << type_name.c_str() << "&)*(" << type_name.c_str() << "*)args["
375  << i << "]";
376  } else if (isPointer) {
377  callbuf << "*(" << type_name.c_str() << "**)args["
378  << i << "]";
379  } else {
380  callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
381  }
382  }
383  callbuf << ")";
384 }
385 
386 void TClingCallFunc::make_narg_call(const unsigned N, ostringstream &typedefbuf,
387  ostringstream &callbuf, const string &class_name,
388  int indent_level)
389 {
390  //
391  // Make a code string that follows this pattern:
392  //
393  // ((<class>*)obj)-><method>(*(<arg-i-type>*)args[i], ...)
394  //
395  const FunctionDecl *FD = fMethod->GetMethodDecl();
396  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
397  // This is a class, struct, or union member.
398  if (MD->isConst())
399  callbuf << "((const " << class_name << "*)obj)->";
400  else
401  callbuf << "((" << class_name << "*)obj)->";
402  } else if (const NamedDecl *ND =
403  dyn_cast<NamedDecl>(FD->getDeclContext())) {
404  // This is a namespace member.
405  (void) ND;
406  callbuf << class_name << "::";
407  }
408  // callbuf << fMethod->Name() << "(";
409  {
410  std::string name;
411  {
412  llvm::raw_string_ostream stream(name);
413  FD->getNameForDiagnostic(stream, FD->getASTContext().getPrintingPolicy(), /*Qualified=*/false);
414  }
415  callbuf << name << "(";
416  }
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();
421  string type_name;
422  bool isReference = false;
423  bool isPointer = false;
424  collect_type_info(QT, typedefbuf, callbuf, type_name,
425  isReference, isPointer, indent_level, true);
426  if (i) {
427  callbuf << ',';
428  if (i % 2) {
429  callbuf << ' ';
430  } else {
431  callbuf << "\n";
432  for (int j = 0; j <= indent_level; ++j) {
433  callbuf << kIndentString;
434  }
435  }
436  }
437  if (isReference) {
438  callbuf << "(" << type_name.c_str() << "&)*(" << type_name.c_str() << "*)args["
439  << i << "]";
440  } else if (isPointer) {
441  callbuf << "*(" << type_name.c_str() << "**)args["
442  << i << "]";
443  } else {
444  // pointer falls back to non-pointer case; the argument preserves
445  // the "pointerness" (i.e. doesn't reference the value).
446  callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
447  }
448  }
449  callbuf << ")";
450 }
451 
452 void TClingCallFunc::make_narg_ctor_with_return(const unsigned N, const string &class_name,
453  ostringstream &buf, int indent_level)
454 {
455  // Make a code string that follows this pattern:
456  //
457  // if (ret) {
458  // (*(ClassName**)ret) = new ClassName(args...);
459  // }
460  // else {
461  // new ClassName(args...);
462  // }
463  //
464  for (int i = 0; i < indent_level; ++i) {
465  buf << kIndentString;
466  }
467  buf << "if (ret) {\n";
468  ++indent_level;
469  {
470  ostringstream typedefbuf;
471  ostringstream callbuf;
472  //
473  // Write the return value assignment part.
474  //
475  for (int i = 0; i < indent_level; ++i) {
476  callbuf << kIndentString;
477  }
478  callbuf << "(*(" << class_name << "**)ret) = ";
479  //
480  // Write the actual new expression.
481  //
482  make_narg_ctor(N, typedefbuf, callbuf, class_name, indent_level);
483  //
484  // End the new expression statement.
485  //
486  callbuf << ";\n";
487  for (int i = 0; i < indent_level; ++i) {
488  callbuf << kIndentString;
489  }
490  callbuf << "return;\n";
491  //
492  // Output the whole new expression and return statement.
493  //
494  buf << typedefbuf.str() << callbuf.str();
495  }
496  --indent_level;
497  for (int i = 0; i < indent_level; ++i) {
498  buf << kIndentString;
499  }
500  buf << "}\n";
501  for (int i = 0; i < indent_level; ++i) {
502  buf << kIndentString;
503  }
504  buf << "else {\n";
505  ++indent_level;
506  {
507  ostringstream typedefbuf;
508  ostringstream callbuf;
509  for (int i = 0; i < indent_level; ++i) {
510  callbuf << kIndentString;
511  }
512  make_narg_ctor(N, typedefbuf, callbuf, class_name, indent_level);
513  callbuf << ";\n";
514  for (int i = 0; i < indent_level; ++i) {
515  callbuf << kIndentString;
516  }
517  callbuf << "return;\n";
518  buf << typedefbuf.str() << callbuf.str();
519  }
520  --indent_level;
521  for (int i = 0; i < indent_level; ++i) {
522  buf << kIndentString;
523  }
524  buf << "}\n";
525 }
526 
527 void TClingCallFunc::make_narg_call_with_return(const unsigned N, const string &class_name,
528  ostringstream &buf, int indent_level)
529 {
530  // Make a code string that follows this pattern:
531  //
532  // if (ret) {
533  // new (ret) (return_type) ((class_name*)obj)->func(args...);
534  // }
535  // else {
536  // ((class_name*)obj)->func(args...);
537  // }
538  //
539  const FunctionDecl *FD = fMethod->GetMethodDecl();
540  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
541  (void) CD;
542  make_narg_ctor_with_return(N, class_name, buf, indent_level);
543  return;
544  }
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) {
550  callbuf << kIndentString;
551  }
552  make_narg_call(N, typedefbuf, callbuf, class_name, indent_level);
553  callbuf << ";\n";
554  for (int i = 0; i < indent_level; ++i) {
555  callbuf << kIndentString;
556  }
557  callbuf << "return;\n";
558  buf << typedefbuf.str() << callbuf.str();
559  } else {
560  for (int i = 0; i < indent_level; ++i) {
561  buf << kIndentString;
562  }
563  buf << "if (ret) {\n";
564  ++indent_level;
565  {
566  ostringstream typedefbuf;
567  ostringstream callbuf;
568  //
569  // Write the placement part of the placement new.
570  //
571  for (int i = 0; i < indent_level; ++i) {
572  callbuf << kIndentString;
573  }
574  callbuf << "new (ret) ";
575  string type_name;
576  bool isReference = false;
577  bool isPointer = false;
578  collect_type_info(QT, typedefbuf, callbuf, type_name,
579  isReference, isPointer, indent_level, false);
580  //
581  // Write the type part of the placement new.
582  //
583  callbuf << "(" << type_name.c_str();
584  if (isReference) {
585  callbuf << "*) (&";
586  } else if (isPointer) {
587  callbuf << "*) (";
588  } else {
589  callbuf << ") (";
590  }
591  //
592  // Write the actual function call.
593  //
594  make_narg_call(N, typedefbuf, callbuf, class_name, indent_level);
595  //
596  // End the placement new.
597  //
598  callbuf << ");\n";
599  for (int i = 0; i < indent_level; ++i) {
600  callbuf << kIndentString;
601  }
602  callbuf << "return;\n";
603  //
604  // Output the whole placement new expression and return statement.
605  //
606  buf << typedefbuf.str() << callbuf.str();
607  }
608  --indent_level;
609  for (int i = 0; i < indent_level; ++i) {
610  buf << kIndentString;
611  }
612  buf << "}\n";
613  for (int i = 0; i < indent_level; ++i) {
614  buf << kIndentString;
615  }
616  buf << "else {\n";
617  ++indent_level;
618  {
619  ostringstream typedefbuf;
620  ostringstream callbuf;
621  for (int i = 0; i < indent_level; ++i) {
622  callbuf << kIndentString;
623  }
624  make_narg_call(N, typedefbuf, callbuf, class_name, indent_level);
625  callbuf << ";\n";
626  for (int i = 0; i < indent_level; ++i) {
627  callbuf << kIndentString;
628  }
629  callbuf << "return;\n";
630  buf << typedefbuf.str() << callbuf.str();
631  }
632  --indent_level;
633  for (int i = 0; i < indent_level; ++i) {
634  buf << kIndentString;
635  }
636  buf << "}\n";
637  }
638 }
639 
641 {
643 
644  const FunctionDecl *FD = fMethod->GetMethodDecl();
645  ASTContext &Context = FD->getASTContext();
646  PrintingPolicy Policy(Context.getPrintingPolicy());
647  //
648  // Get the class or namespace name.
649  //
650  string class_name;
651  if (const TypeDecl *TD =
652  dyn_cast<TypeDecl>(FD->getDeclContext())) {
653  // This is a class, struct, or union member.
654  QualType QT(TD->getTypeForDecl(), 0);
655  ROOT::TMetaUtils::GetNormalizedName(class_name, QT, *fInterp, fNormCtxt);
656  } else if (const NamedDecl *ND =
657  dyn_cast<NamedDecl>(FD->getDeclContext())) {
658  // This is a namespace member.
659  raw_string_ostream stream(class_name);
660  ND->getNameForDiagnostic(stream, Policy, /*Qualified=*/true);
661  stream.flush();
662  }
663  //
664  // Check to make sure that we can
665  // instantiate and codegen this function.
666  //
667  bool needInstantiation = false;
668  const FunctionDecl *Definition = 0;
669  if (!FD->isDefined(Definition)) {
670  FunctionDecl::TemplatedKind TK = FD->getTemplatedKind();
671  switch (TK) {
672  case FunctionDecl::TK_NonTemplate: {
673  // Ordinary function, not a template specialization.
674  // Note: This might be ok, the body might be defined
675  // in a library, and all we have seen is the
676  // header file.
677  //::Error("TClingCallFunc::make_wrapper",
678  // "Cannot make wrapper for a function which is "
679  // "declared but not defined!");
680  //return 0;
681  }
682  break;
683  case FunctionDecl::TK_FunctionTemplate: {
684  // This decl is actually a function template,
685  // not a function at all.
686  ::Error("TClingCallFunc::make_wrapper",
687  "Cannot make wrapper for a function template!");
688  return 0;
689  }
690  break;
691  case FunctionDecl::TK_MemberSpecialization: {
692  // This function is the result of instantiating an ordinary
693  // member function of a class template, or of instantiating
694  // an ordinary member function of a class member of a class
695  // template, or of specializing a member function template
696  // of a class template, or of specializing a member function
697  // template of a class member of a class template.
698  if (!FD->isTemplateInstantiation()) {
699  // We are either TSK_Undeclared or
700  // TSK_ExplicitSpecialization.
701  // Note: This might be ok, the body might be defined
702  // in a library, and all we have seen is the
703  // header file.
704  //::Error("TClingCallFunc::make_wrapper",
705  // "Cannot make wrapper for a function template "
706  // "explicit specialization which is declared "
707  // "but not defined!");
708  //return 0;
709  break;
710  }
711  const FunctionDecl *Pattern =
712  FD->getTemplateInstantiationPattern();
713  if (!Pattern) {
714  ::Error("TClingCallFunc::make_wrapper",
715  "Cannot make wrapper for a member function "
716  "instantiation with no pattern!");
717  return 0;
718  }
719  FunctionDecl::TemplatedKind PTK = Pattern->getTemplatedKind();
720  TemplateSpecializationKind PTSK =
721  Pattern->getTemplateSpecializationKind();
722  if (
723  // The pattern is an ordinary member function.
724  (PTK == FunctionDecl::TK_NonTemplate) ||
725  // The pattern is an explicit specialization, and
726  // so is not a template.
727  ((PTK != FunctionDecl::TK_FunctionTemplate) &&
728  ((PTSK == TSK_Undeclared) ||
729  (PTSK == TSK_ExplicitSpecialization)))
730  ) {
731  // Note: This might be ok, the body might be defined
732  // in a library, and all we have seen is the
733  // header file.
734  break;
735  } else if (!Pattern->hasBody()) {
736  ::Error("TClingCallFunc::make_wrapper",
737  "Cannot make wrapper for a member function "
738  "instantiation with no body!");
739  return 0;
740  }
741  if (FD->isImplicitlyInstantiable()) {
742  needInstantiation = true;
743  }
744  }
745  break;
746  case FunctionDecl::TK_FunctionTemplateSpecialization: {
747  // This function is the result of instantiating a function
748  // template or possibly an explicit specialization of a
749  // function template. Could be a namespace scope function or a
750  // member function.
751  if (!FD->isTemplateInstantiation()) {
752  // We are either TSK_Undeclared or
753  // TSK_ExplicitSpecialization.
754  // Note: This might be ok, the body might be defined
755  // in a library, and all we have seen is the
756  // header file.
757  //::Error("TClingCallFunc::make_wrapper",
758  // "Cannot make wrapper for a function template "
759  // "explicit specialization which is declared "
760  // "but not defined!");
761  //return 0;
762  break;
763  }
764  const FunctionDecl *Pattern =
765  FD->getTemplateInstantiationPattern();
766  if (!Pattern) {
767  ::Error("TClingCallFunc::make_wrapper",
768  "Cannot make wrapper for a function template"
769  "instantiation with no pattern!");
770  return 0;
771  }
772  FunctionDecl::TemplatedKind PTK = Pattern->getTemplatedKind();
773  TemplateSpecializationKind PTSK =
774  Pattern->getTemplateSpecializationKind();
775  if (
776  // The pattern is an ordinary member function.
777  (PTK == FunctionDecl::TK_NonTemplate) ||
778  // The pattern is an explicit specialization, and
779  // so is not a template.
780  ((PTK != FunctionDecl::TK_FunctionTemplate) &&
781  ((PTSK == TSK_Undeclared) ||
782  (PTSK == TSK_ExplicitSpecialization)))
783  ) {
784  // Note: This might be ok, the body might be defined
785  // in a library, and all we have seen is the
786  // header file.
787  break;
788  }
789  if (!Pattern->hasBody()) {
790  ::Error("TClingCallFunc::make_wrapper",
791  "Cannot make wrapper for a function template"
792  "instantiation with no body!");
793  return 0;
794  }
795  if (FD->isImplicitlyInstantiable()) {
796  needInstantiation = true;
797  }
798  }
799  break;
800  case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
801  // This function is the result of instantiating or
802  // specializing a member function of a class template,
803  // or a member function of a class member of a class template,
804  // or a member function template of a class template, or a
805  // member function template of a class member of a class
806  // template where at least some part of the function is
807  // dependent on a template argument.
808  if (!FD->isTemplateInstantiation()) {
809  // We are either TSK_Undeclared or
810  // TSK_ExplicitSpecialization.
811  // Note: This might be ok, the body might be defined
812  // in a library, and all we have seen is the
813  // header file.
814  //::Error("TClingCallFunc::make_wrapper",
815  // "Cannot make wrapper for a dependent function "
816  // "template explicit specialization which is declared "
817  // "but not defined!");
818  //return 0;
819  break;
820  }
821  const FunctionDecl *Pattern =
822  FD->getTemplateInstantiationPattern();
823  if (!Pattern) {
824  ::Error("TClingCallFunc::make_wrapper",
825  "Cannot make wrapper for a dependent function template"
826  "instantiation with no pattern!");
827  return 0;
828  }
829  FunctionDecl::TemplatedKind PTK = Pattern->getTemplatedKind();
830  TemplateSpecializationKind PTSK =
831  Pattern->getTemplateSpecializationKind();
832  if (
833  // The pattern is an ordinary member function.
834  (PTK == FunctionDecl::TK_NonTemplate) ||
835  // The pattern is an explicit specialization, and
836  // so is not a template.
837  ((PTK != FunctionDecl::TK_FunctionTemplate) &&
838  ((PTSK == TSK_Undeclared) ||
839  (PTSK == TSK_ExplicitSpecialization)))
840  ) {
841  // Note: This might be ok, the body might be defined
842  // in a library, and all we have seen is the
843  // header file.
844  break;
845  }
846  if (!Pattern->hasBody()) {
847  ::Error("TClingCallFunc::make_wrapper",
848  "Cannot make wrapper for a dependent function template"
849  "instantiation with no body!");
850  return 0;
851  }
852  if (FD->isImplicitlyInstantiable()) {
853  needInstantiation = true;
854  }
855  }
856  break;
857  default: {
858  // Will only happen if clang implementation changes.
859  // Protect ourselves in case that happens.
860  ::Error("TClingCallFunc::make_wrapper",
861  "Unhandled template kind!");
862  return 0;
863  }
864  break;
865  }
866  // We do not set needInstantiation to true in these cases:
867  //
868  // isInvalidDecl()
869  // TSK_Undeclared
870  // TSK_ExplicitInstantiationDefinition
871  // TSK_ExplicitSpecialization && !getClassScopeSpecializationPattern()
872  // TSK_ExplicitInstantiationDeclaration &&
873  // getTemplateInstantiationPattern() &&
874  // PatternDecl->hasBody() &&
875  // !PatternDecl->isInlined()
876  //
877  // Set it true in these cases:
878  //
879  // TSK_ImplicitInstantiation
880  // TSK_ExplicitInstantiationDeclaration && (!getPatternDecl() ||
881  // !PatternDecl->hasBody() || PatternDecl->isInlined())
882  //
883  }
884  if (needInstantiation) {
885  clang::FunctionDecl *FDmod = const_cast<clang::FunctionDecl *>(FD);
886  clang::Sema &S = fInterp->getSema();
887  // Could trigger deserialization of decls.
888  cling::Interpreter::PushTransactionRAII RAII(fInterp);
889  S.InstantiateFunctionDefinition(SourceLocation(), FDmod,
890  /*Recursive=*/ true,
891  /*DefinitionRequired=*/ true);
892  if (!FD->isDefined(Definition)) {
893  ::Error("TClingCallFunc::make_wrapper",
894  "Failed to force template instantiation!");
895  return 0;
896  }
897  }
898  if (Definition) {
899  FunctionDecl::TemplatedKind TK = Definition->getTemplatedKind();
900  switch (TK) {
901  case FunctionDecl::TK_NonTemplate: {
902  // Ordinary function, not a template specialization.
903  if (Definition->isDeleted()) {
904  ::Error("TClingCallFunc::make_wrapper",
905  "Cannot make wrapper for a deleted function!");
906  return 0;
907  } else if (Definition->isLateTemplateParsed()) {
908  ::Error("TClingCallFunc::make_wrapper",
909  "Cannot make wrapper for a late template parsed "
910  "function!");
911  return 0;
912  }
913  //else if (Definition->isDefaulted()) {
914  // // Might not have a body, but we can still use it.
915  //}
916  //else {
917  // // Has a body.
918  //}
919  }
920  break;
921  case FunctionDecl::TK_FunctionTemplate: {
922  // This decl is actually a function template,
923  // not a function at all.
924  ::Error("TClingCallFunc::make_wrapper",
925  "Cannot make wrapper for a function template!");
926  return 0;
927  }
928  break;
929  case FunctionDecl::TK_MemberSpecialization: {
930  // This function is the result of instantiating an ordinary
931  // member function of a class template or of a member class
932  // of a class template.
933  if (Definition->isDeleted()) {
934  ::Error("TClingCallFunc::make_wrapper",
935  "Cannot make wrapper for a deleted member function "
936  "of a specialization!");
937  return 0;
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!");
942  return 0;
943  }
944  //else if (Definition->isDefaulted()) {
945  // // Might not have a body, but we can still use it.
946  //}
947  //else {
948  // // Has a body.
949  //}
950  }
951  break;
952  case FunctionDecl::TK_FunctionTemplateSpecialization: {
953  // This function is the result of instantiating a function
954  // template or possibly an explicit specialization of a
955  // function template. Could be a namespace scope function or a
956  // member function.
957  if (Definition->isDeleted()) {
958  ::Error("TClingCallFunc::make_wrapper",
959  "Cannot make wrapper for a deleted function "
960  "template specialization!");
961  return 0;
962  } else if (Definition->isLateTemplateParsed()) {
963  ::Error("TClingCallFunc::make_wrapper",
964  "Cannot make wrapper for a late template parsed "
965  "function template specialization!");
966  return 0;
967  }
968  //else if (Definition->isDefaulted()) {
969  // // Might not have a body, but we can still use it.
970  //}
971  //else {
972  // // Has a body.
973  //}
974  }
975  break;
976  case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
977  // This function is the result of instantiating or
978  // specializing a member function of a class template,
979  // or a member function of a class member of a class template,
980  // or a member function template of a class template, or a
981  // member function template of a class member of a class
982  // template where at least some part of the function is
983  // dependent on a template argument.
984  if (Definition->isDeleted()) {
985  ::Error("TClingCallFunc::make_wrapper",
986  "Cannot make wrapper for a deleted dependent function "
987  "template specialization!");
988  return 0;
989  } else if (Definition->isLateTemplateParsed()) {
990  ::Error("TClingCallFunc::make_wrapper",
991  "Cannot make wrapper for a late template parsed "
992  "dependent function template specialization!");
993  return 0;
994  }
995  //else if (Definition->isDefaulted()) {
996  // // Might not have a body, but we can still use it.
997  //}
998  //else {
999  // // Has a body.
1000  //}
1001  }
1002  break;
1003  default: {
1004  // Will only happen if clang implementation changes.
1005  // Protect ourselves in case that happens.
1006  ::Error("TClingCallFunc::make_wrapper",
1007  "Unhandled template kind!");
1008  return 0;
1009  }
1010  break;
1011  }
1012  }
1013  unsigned min_args = FD->getMinRequiredArguments();
1014  unsigned num_params = FD->getNumParams();
1015  //
1016  // Make the wrapper name.
1017  //
1018  string wrapper_name;
1019  {
1020  ostringstream buf;
1021  buf << "__cf";
1022  //const NamedDecl* ND = dyn_cast<NamedDecl>(FD);
1023  //string mn;
1024  //fInterp->maybeMangleDeclName(ND, mn);
1025  //buf << '_' << mn;
1026  buf << '_' << gWrapperSerial++;
1027  wrapper_name = buf.str();
1028  }
1029  //
1030  // Write the wrapper code.
1031  // FIXME: this should be synthesized into the AST!
1032  //
1033  int indent_level = 0;
1034  ostringstream buf;
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"
1041  "{\n";
1042  ++indent_level;
1043  if (min_args == num_params) {
1044  // No parameters with defaults.
1045  make_narg_call_with_return(num_params, class_name, buf, indent_level);
1046  } else {
1047  // We need one function call clause compiled for every
1048  // possible number of arguments per call.
1049  for (unsigned N = min_args; N <= num_params; ++N) {
1050  for (int i = 0; i < indent_level; ++i) {
1051  buf << kIndentString;
1052  }
1053  buf << "if (nargs == " << N << ") {\n";
1054  ++indent_level;
1055  make_narg_call_with_return(N, class_name, buf, indent_level);
1056  --indent_level;
1057  for (int i = 0; i < indent_level; ++i) {
1058  buf << kIndentString;
1059  }
1060  buf << "}\n";
1061  }
1062  }
1063  --indent_level;
1064  buf << "}\n"
1065  "#pragma clang diagnostic pop";
1066  string wrapper(buf.str());
1067  //fprintf(stderr, "%s\n", wrapper.c_str());
1068  //
1069  // Compile the wrapper code.
1070  //
1071  void *F = compile_wrapper(wrapper_name, wrapper);
1072  if (F) {
1073  gWrapperStore.insert(make_pair(FD, F));
1074  } else {
1075  ::Error("TClingCallFunc::make_wrapper",
1076  "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1077  wrapper.c_str());
1078  }
1079  return (tcling_callfunc_Wrapper_t)F;
1080 }
1081 
1083 {
1084  // Make a code string that follows this pattern:
1085  //
1086  // void
1087  // unique_wrapper_ddd(void** ret, void* arena, unsigned long nary)
1088  // {
1089  // if (!arena) {
1090  // if (!nary) {
1091  // *ret = new ClassName;
1092  // }
1093  // else {
1094  // *ret = new ClassName[nary];
1095  // }
1096  // }
1097  // else {
1098  // if (!nary) {
1099  // *ret = new (arena) ClassName;
1100  // }
1101  // else {
1102  // *ret = new (arena) ClassName[nary];
1103  // }
1104  // }
1105  // }
1106  //
1107  // Note:
1108  //
1109  // If the class is of POD type, the form:
1110  //
1111  // new ClassName;
1112  //
1113  // does not initialize the object at all, and the form:
1114  //
1115  // new ClassName();
1116  //
1117  // default-initializes the object.
1118  //
1119  // We are using the form without parentheses because that is what
1120  // CINT did.
1121  //
1122  //--
1123  ASTContext &Context = info->GetDecl()->getASTContext();
1124  PrintingPolicy Policy(Context.getPrintingPolicy());
1125  Policy.SuppressTagKeyword = true;
1126  Policy.SuppressUnwrittenScope = true;
1127  //
1128  // Get the class or namespace name.
1129  //
1130  string class_name;
1131  if (const TypeDecl *TD = dyn_cast<TypeDecl>(info->GetDecl())) {
1132  // This is a class, struct, or union member.
1133  QualType QT(TD->getTypeForDecl(), 0);
1134  ROOT::TMetaUtils::GetNormalizedName(class_name, QT, *fInterp, fNormCtxt);
1135  } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(info->GetDecl())) {
1136  // This is a namespace member.
1137  raw_string_ostream stream(class_name);
1138  ND->getNameForDiagnostic(stream, Policy, /*Qualified=*/true);
1139  stream.flush();
1140  }
1141  //
1142  // Make the wrapper name.
1143  //
1144  string wrapper_name;
1145  {
1146  ostringstream buf;
1147  buf << "__ctor";
1148  //const NamedDecl* ND = dyn_cast<NamedDecl>(FD);
1149  //string mn;
1150  //fInterp->maybeMangleDeclName(ND, mn);
1151  //buf << '_dtor_' << mn;
1152  buf << '_' << gWrapperSerial++;
1153  wrapper_name = buf.str();
1154  }
1155  //
1156  // Write the wrapper code.
1157  //
1158  int indent_level = 0;
1159  ostringstream buf;
1160  buf << "__attribute__((used)) ";
1161  buf << "extern \"C\" void ";
1162  buf << wrapper_name;
1163  buf << "(void** ret, void* arena, unsigned long nary)\n";
1164  buf << "{\n";
1165  // if (!arena) {
1166  // if (!nary) {
1167  // *ret = new ClassName;
1168  // }
1169  // else {
1170  // *ret = new ClassName[nary];
1171  // }
1172  // }
1173  ++indent_level;
1174  indent(buf, indent_level);
1175  buf << "if (!arena) {\n";
1176  ++indent_level;
1177  indent(buf, indent_level);
1178  buf << "if (!nary) {\n";
1179  ++indent_level;
1180  indent(buf, indent_level);
1181  buf << "*ret = new " << class_name << ";\n";
1182  --indent_level;
1183  indent(buf, indent_level);
1184  buf << "}\n";
1185  indent(buf, indent_level);
1186  buf << "else {\n";
1187  ++indent_level;
1188  indent(buf, indent_level);
1189  buf << "*ret = new " << class_name << "[nary];\n";
1190  --indent_level;
1191  indent(buf, indent_level);
1192  buf << "}\n";
1193  --indent_level;
1194  indent(buf, indent_level);
1195  buf << "}\n";
1196  // else {
1197  // if (!nary) {
1198  // *ret = new (arena) ClassName;
1199  // }
1200  // else {
1201  // *ret = new (arena) ClassName[nary];
1202  // }
1203  // }
1204  indent(buf, indent_level);
1205  buf << "else {\n";
1206  ++indent_level;
1207  indent(buf, indent_level);
1208  buf << "if (!nary) {\n";
1209  ++indent_level;
1210  indent(buf, indent_level);
1211  buf << "*ret = new (arena) " << class_name << ";\n";
1212  --indent_level;
1213  indent(buf, indent_level);
1214  buf << "}\n";
1215  indent(buf, indent_level);
1216  buf << "else {\n";
1217  ++indent_level;
1218  indent(buf, indent_level);
1219  buf << "*ret = new (arena) " << class_name << "[nary];\n";
1220  --indent_level;
1221  indent(buf, indent_level);
1222  buf << "}\n";
1223  --indent_level;
1224  indent(buf, indent_level);
1225  buf << "}\n";
1226  // End wrapper.
1227  --indent_level;
1228  buf << "}\n";
1229  // Done.
1230  string wrapper(buf.str());
1231  //fprintf(stderr, "%s\n", wrapper.c_str());
1232  //
1233  // Compile the wrapper code.
1234  //
1235  void *F = compile_wrapper(wrapper_name, wrapper,
1236  /*withAccessControl=*/false);
1237  if (F) {
1238  gCtorWrapperStore.insert(make_pair(info->GetDecl(), F));
1239  } else {
1240  ::Error("TClingCallFunc::make_ctor_wrapper",
1241  "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1242  wrapper.c_str());
1243  }
1245 }
1246 
1249 {
1250  // Make a code string that follows this pattern:
1251  //
1252  // void
1253  // unique_wrapper_ddd(void* obj, unsigned long nary, int withFree)
1254  // {
1255  // if (withFree) {
1256  // if (!nary) {
1257  // delete (ClassName*) obj;
1258  // }
1259  // else {
1260  // delete[] (ClassName*) obj;
1261  // }
1262  // }
1263  // else {
1264  // typedef ClassName DtorName;
1265  // if (!nary) {
1266  // ((ClassName*)obj)->~DtorName();
1267  // }
1268  // else {
1269  // for (unsigned long i = nary - 1; i > -1; --i) {
1270  // (((ClassName*)obj)+i)->~DtorName();
1271  // }
1272  // }
1273  // }
1274  // }
1275  //
1276  //--
1277  ASTContext &Context = info->GetDecl()->getASTContext();
1278  PrintingPolicy Policy(Context.getPrintingPolicy());
1279  Policy.SuppressTagKeyword = true;
1280  Policy.SuppressUnwrittenScope = true;
1281  //
1282  // Get the class or namespace name.
1283  //
1284  string class_name;
1285  if (const TypeDecl *TD = dyn_cast<TypeDecl>(info->GetDecl())) {
1286  // This is a class, struct, or union member.
1287  QualType QT(TD->getTypeForDecl(), 0);
1288  ROOT::TMetaUtils::GetNormalizedName(class_name, QT, *fInterp, fNormCtxt);
1289  } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(info->GetDecl())) {
1290  // This is a namespace member.
1291  raw_string_ostream stream(class_name);
1292  ND->getNameForDiagnostic(stream, Policy, /*Qualified=*/true);
1293  stream.flush();
1294  }
1295  //
1296  // Make the wrapper name.
1297  //
1298  string wrapper_name;
1299  {
1300  ostringstream buf;
1301  buf << "__dtor";
1302  //const NamedDecl* ND = dyn_cast<NamedDecl>(FD);
1303  //string mn;
1304  //fInterp->maybeMangleDeclName(ND, mn);
1305  //buf << '_dtor_' << mn;
1306  buf << '_' << gWrapperSerial++;
1307  wrapper_name = buf.str();
1308  }
1309  //
1310  // Write the wrapper code.
1311  //
1312  int indent_level = 0;
1313  ostringstream buf;
1314  buf << "__attribute__((used)) ";
1315  buf << "extern \"C\" void ";
1316  buf << wrapper_name;
1317  buf << "(void* obj, unsigned long nary, int withFree)\n";
1318  buf << "{\n";
1319  // if (withFree) {
1320  // if (!nary) {
1321  // delete (ClassName*) obj;
1322  // }
1323  // else {
1324  // delete[] (ClassName*) obj;
1325  // }
1326  // }
1327  ++indent_level;
1328  indent(buf, indent_level);
1329  buf << "if (withFree) {\n";
1330  ++indent_level;
1331  indent(buf, indent_level);
1332  buf << "if (!nary) {\n";
1333  ++indent_level;
1334  indent(buf, indent_level);
1335  buf << "delete (" << class_name << "*) obj;\n";
1336  --indent_level;
1337  indent(buf, indent_level);
1338  buf << "}\n";
1339  indent(buf, indent_level);
1340  buf << "else {\n";
1341  ++indent_level;
1342  indent(buf, indent_level);
1343  buf << "delete[] (" << class_name << "*) obj;\n";
1344  --indent_level;
1345  indent(buf, indent_level);
1346  buf << "}\n";
1347  --indent_level;
1348  indent(buf, indent_level);
1349  buf << "}\n";
1350  // else {
1351  // typedef ClassName Nm;
1352  // if (!nary) {
1353  // ((Nm*)obj)->~Nm();
1354  // }
1355  // else {
1356  // for (unsigned long i = nary - 1; i > -1; --i) {
1357  // (((Nm*)obj)+i)->~Nm();
1358  // }
1359  // }
1360  // }
1361  indent(buf, indent_level);
1362  buf << "else {\n";
1363  ++indent_level;
1364  indent(buf, indent_level);
1365  buf << "typedef " << class_name << " Nm;\n";
1366  buf << "if (!nary) {\n";
1367  ++indent_level;
1368  indent(buf, indent_level);
1369  buf << "((Nm*)obj)->~Nm();\n";
1370  --indent_level;
1371  indent(buf, indent_level);
1372  buf << "}\n";
1373  indent(buf, indent_level);
1374  buf << "else {\n";
1375  ++indent_level;
1376  indent(buf, indent_level);
1377  buf << "do {\n";
1378  ++indent_level;
1379  indent(buf, indent_level);
1380  buf << "(((Nm*)obj)+(--nary))->~Nm();\n";
1381  --indent_level;
1382  indent(buf, indent_level);
1383  buf << "} while (nary);\n";
1384  --indent_level;
1385  indent(buf, indent_level);
1386  buf << "}\n";
1387  --indent_level;
1388  indent(buf, indent_level);
1389  buf << "}\n";
1390  // End wrapper.
1391  --indent_level;
1392  buf << "}\n";
1393  // Done.
1394  string wrapper(buf.str());
1395  //fprintf(stderr, "%s\n", wrapper.c_str());
1396  //
1397  // Compile the wrapper code.
1398  //
1399  void *F = compile_wrapper(wrapper_name, wrapper,
1400  /*withAccessControl=*/false);
1401  if (F) {
1402  gDtorWrapperStore.insert(make_pair(info->GetDecl(), F));
1403  } else {
1404  ::Error("TClingCallFunc::make_dtor_wrapper",
1405  "Failed to compile\n ==== SOURCE BEGIN ====\n%s\n ==== SOURCE END ====",
1406  wrapper.c_str());
1407  }
1408 
1410 }
1411 
1412 class ValHolder {
1413 public:
1414  union {
1415  long double ldbl;
1416  double dbl;
1417  float flt;
1418  //__uint128_t ui128;
1419  //__int128_t i128;
1420  unsigned long long ull;
1421  long long ll;
1422  unsigned long ul;
1423  long l;
1424  unsigned int ui;
1425  int i;
1426  unsigned short us;
1427  short s;
1428  //char32_t c32;
1429  //char16_t c16;
1430  //unsigned wchar_t uwc; - non-standard
1431  wchar_t wc;
1432  unsigned char uc;
1433  signed char sc;
1434  char c;
1435  bool b;
1436  void *vp;
1437  } u;
1438 };
1439 
1440 void TClingCallFunc::exec(void *address, void *ret) const
1441 {
1442  SmallVector<ValHolder, 8> vh_ary;
1443  SmallVector<void *, 8> vp_ary;
1444 
1445  unsigned num_args = fArgVals.size();
1446  {
1448 
1449  const FunctionDecl *FD = fMethod->GetMethodDecl();
1450 
1451  //
1452  // Convert the arguments from cling::Value to their
1453  // actual type and store them in a holder for passing to the
1454  // wrapper function by pointer to value.
1455  //
1456  unsigned num_params = FD->getNumParams();
1457 
1458  if (num_args < FD->getMinRequiredArguments()) {
1459  ::Error("TClingCallFunc::exec",
1460  "Not enough arguments provided for %s (%d instead of the minimum %d)",
1461  fMethod->Name(ROOT::TMetaUtils::TNormalizedCtxt(fInterp->getLookupHelper())),
1462  num_args, FD->getMinRequiredArguments());
1463  return;
1464  }
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.",
1470  fMethod->Name(ROOT::TMetaUtils::TNormalizedCtxt(fInterp->getLookupHelper())));
1471  return;
1472  }
1473  vh_ary.reserve(num_args);
1474  vp_ary.reserve(num_args);
1475  for (unsigned i = 0U; i < num_args; ++i) {
1476  QualType Ty;
1477  if (i < num_params) {
1478  const ParmVarDecl *PVD = FD->getParamDecl(i);
1479  Ty = PVD->getType();
1480  } else {
1481  Ty = fArgVals[i].getType();
1482  }
1483  QualType QT = Ty.getCanonicalType();
1484  if (const BuiltinType *BT =
1485  dyn_cast<BuiltinType>(&*QT)) {
1486  //
1487  // WARNING!!!
1488  //
1489  // This switch is organized in order-of-declaration
1490  // so that the produced assembly code is optimal.
1491  // Do not reorder!
1492  //
1493  switch (BT->getKind()) {
1494  //
1495  // Builtin Types
1496  //
1497  case BuiltinType::Void: {
1498  // void
1499  ::Error("TClingCallFunc::exec(void*)",
1500  "Invalid type 'Void'!");
1501  return;
1502  }
1503  break;
1504  //
1505  // Unsigned Types
1506  //
1507  case BuiltinType::Bool: {
1508  // bool
1509  ValHolder vh;
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());
1513  }
1514  break;
1515  case BuiltinType::Char_U: {
1516  // char on targets where it is unsigned
1517  ValHolder vh;
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());
1521  }
1522  break;
1523  case BuiltinType::UChar: {
1524  // unsigned char
1525  ValHolder vh;
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());
1529  }
1530  break;
1531  case BuiltinType::WChar_U: {
1532  // wchar_t on targets where it is unsigned.
1533  // The standard doesn't allow to specify signednedd of wchar_t
1534  // thus this maps simply to wchar_t.
1535  ValHolder vh;
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());
1539  }
1540  break;
1541  case BuiltinType::Char16: {
1542  // char16_t
1543  //ValHolder vh;
1544  //vh.u.c16 = (char16_t) sv_to_ulong_long(fArgVals[i]);
1545  //vh_ary.push_back(vh);
1546  //vp_ary.push_back(&vh_ary.back());
1547  }
1548  break;
1549  case BuiltinType::Char32: {
1550  // char32_t
1551  //ValHolder vh;
1552  //vh.u.c32 = (char32_t) sv_to_ulong_long(fArgVals[i]);
1553  //vh_ary.push_back(vh);
1554  //vp_ary.push_back(&vh_ary.back());
1555  }
1556  break;
1557  case BuiltinType::UShort: {
1558  // unsigned short
1559  ValHolder vh;
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());
1563  }
1564  break;
1565  case BuiltinType::UInt: {
1566  // unsigned int
1567  ValHolder vh;
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());
1571  }
1572  break;
1573  case BuiltinType::ULong: {
1574  // unsigned long
1575  ValHolder vh;
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());
1579  }
1580  break;
1581  case BuiltinType::ULongLong: {
1582  // unsigned long long
1583  ValHolder vh;
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());
1587  }
1588  break;
1589  case BuiltinType::UInt128: {
1590  // __uint128_t
1591  }
1592  break;
1593  //
1594  // Signed Types
1595  //
1596  //
1597  // Signed Types
1598  //
1599  case BuiltinType::Char_S: {
1600  // char on targets where it is signed
1601  ValHolder vh;
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());
1605  }
1606  break;
1607  case BuiltinType::SChar: {
1608  // signed char
1609  ValHolder vh;
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());
1613  }
1614  break;
1615  case BuiltinType::WChar_S: {
1616  // wchar_t on targets where it is signed.
1617  // The standard doesn't allow to specify signednedd of wchar_t
1618  // thus this maps simply to wchar_t.
1619  ValHolder vh;
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());
1623  }
1624  break;
1625  case BuiltinType::Short: {
1626  // short
1627  ValHolder vh;
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());
1631  }
1632  break;
1633  case BuiltinType::Int: {
1634  // int
1635  ValHolder vh;
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());
1639  }
1640  break;
1641  case BuiltinType::Long: {
1642  // long
1643  ValHolder vh;
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());
1647  }
1648  break;
1649  case BuiltinType::LongLong: {
1650  // long long
1651  ValHolder vh;
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());
1655  }
1656  break;
1657  case BuiltinType::Int128: {
1658  // __int128_t
1659  ::Error("TClingCallFunc::exec(void*)",
1660  "Invalid type 'Int128'!");
1661  return;
1662  }
1663  break;
1664  case BuiltinType::Half: {
1665  // half in OpenCL, __fp16 in ARM NEON
1666  ::Error("TClingCallFunc::exec(void*)",
1667  "Invalid type 'Half'!");
1668  return;
1669  }
1670  break;
1671  case BuiltinType::Float: {
1672  // float
1673  ValHolder vh;
1674  vh.u.flt = sv_to<float>(fArgVals[i]);
1675  vh_ary.push_back(vh);
1676  vp_ary.push_back(&vh_ary.back());
1677  }
1678  break;
1679  case BuiltinType::Double: {
1680  // double
1681  ValHolder vh;
1682  vh.u.dbl = sv_to<double>(fArgVals[i]);
1683  vh_ary.push_back(vh);
1684  vp_ary.push_back(&vh_ary.back());
1685  }
1686  break;
1687  case BuiltinType::LongDouble: {
1688  // long double
1689  ValHolder vh;
1690  vh.u.ldbl = sv_to<long double>(fArgVals[i]);
1691  vh_ary.push_back(vh);
1692  vp_ary.push_back(&vh_ary.back());
1693  }
1694  break;
1695  //
1696  // Language-Specific Types
1697  //
1698  case BuiltinType::NullPtr: {
1699  // C++11 nullptr
1700  ValHolder vh;
1701  vh.u.vp = fArgVals[i].getPtr();
1702  vh_ary.push_back(vh);
1703  vp_ary.push_back(&vh_ary.back());
1704  }
1705  break;
1706  default: {
1707  // There should be no others. This is here in case
1708  // this changes in the future.
1709  ::Error("TClingCallFunc::exec(void*)",
1710  "Unhandled builtin type!");
1711  QT->dump();
1712  return;
1713  }
1714  break;
1715  }
1716  } else if (QT->isReferenceType()) {
1717  // the argument is already a pointer value (point to the same thing
1718  // as the reference.
1719  vp_ary.push_back((void *) sv_to_ulong_long(fArgVals[i]));
1720  } else if (QT->isPointerType() || QT->isArrayType()) {
1721  ValHolder vh;
1722  vh.u.vp = (void *) sv_to_ulong_long(fArgVals[i]);
1723  vh_ary.push_back(vh);
1724  vp_ary.push_back(&vh_ary.back());
1725  } else if (QT->isRecordType()) {
1726  // the argument is already a pointer value (pointing to object passed
1727  // by value).
1728  vp_ary.push_back((void *) sv_to_ulong_long(fArgVals[i]));
1729  } else if (const EnumType *ET =
1730  dyn_cast<EnumType>(&*QT)) {
1731  // Note: We may need to worry about the underlying type
1732  // of the enum here.
1733  (void) ET;
1734  ValHolder vh;
1735  vh.u.i = (int) sv_to_long_long(fArgVals[i]);
1736  vh_ary.push_back(vh);
1737  vp_ary.push_back(&vh_ary.back());
1738  } else if (QT->isMemberPointerType()) {
1739  ValHolder vh;
1740  vh.u.vp = (void *) sv_to_ulong_long(fArgVals[i]);
1741  vh_ary.push_back(vh);
1742  vp_ary.push_back(&vh_ary.back());
1743  } else {
1744  ::Error("TClingCallFunc::exec(void*)",
1745  "Invalid type (unrecognized)!");
1746  QT->dump();
1747  return;
1748  }
1749  }
1750  } // End of scope holding the lock
1751  (*fWrapper)(address, (int)num_args, (void **)vp_ary.data(), ret);
1752 }
1753 
1754 template <typename T>
1755 void TClingCallFunc::execWithLL(void *address, clang::QualType QT,
1756  cling::Value *val) const
1757 {
1758  T ret; // leave uninit for valgrind's sake!
1759  exec(address, &ret);
1760  val->getLL() = ret;
1761 }
1762 
1763 template <typename T>
1764 void TClingCallFunc::execWithULL(void *address, clang::QualType QT,
1765  cling::Value *val) const
1766 {
1767  T ret; // leave uninit for valgrind's sake!
1768  exec(address, &ret);
1769  val->getULL() = ret;
1770 }
1771 
1773 {
1774  if (!ret) {
1775  exec(address, 0);
1776  return;
1777  }
1778 
1780 
1781  const FunctionDecl *FD = fMethod->GetMethodDecl();
1782 
1783  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
1784  ASTContext &Context = FD->getASTContext();
1785  const TypeDecl *TD = dyn_cast<TypeDecl>(CD->getDeclContext());
1786  QualType ClassTy(TD->getTypeForDecl(), 0);
1787  QualType QT = Context.getLValueReferenceType(ClassTy);
1788  *ret = cling::Value(QT, *fInterp);
1789  // Store the new()'ed address in getPtr()
1790  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1791  exec(address, &ret->getPtr());
1792  return;
1793  }
1794  QualType QT = FD->getReturnType().getCanonicalType();
1795  if (QT->isReferenceType()) {
1796  *ret = cling::Value(QT, *fInterp);
1797  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1798  exec(address, &ret->getPtr());
1799  return;
1800  } else if (QT->isMemberPointerType()) {
1801  const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
1802  if (MPT->isMemberDataPointer()) {
1803  // A member data pointer is a actually a struct with one
1804  // member of ptrdiff_t, the offset from the base of the object
1805  // storage to the storage for the designated data member.
1806  // But that's not relevant: we use it as a non-builtin, allocated
1807  // type.
1808  *ret = cling::Value(QT, *fInterp);
1809  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1810  exec(address, ret->getPtr());
1811  return;
1812  }
1813  // We are a function member pointer.
1814  *ret = cling::Value(QT, *fInterp);
1815  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1816  exec(address, &ret->getPtr());
1817  return;
1818  } else if (QT->isPointerType() || QT->isArrayType()) {
1819  // Note: ArrayType is an illegal function return value type.
1820  *ret = cling::Value(QT, *fInterp);
1821  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1822  exec(address, &ret->getPtr());
1823  return;
1824  } else if (QT->isRecordType()) {
1825  *ret = cling::Value(QT, *fInterp);
1826  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1827  exec(address, ret->getPtr());
1828  return;
1829  } else if (const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
1830  // Note: We may need to worry about the underlying type
1831  // of the enum here.
1832  (void) ET;
1833  *ret = cling::Value(QT, *fInterp);
1834  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1835  execWithLL<int>(address, QT, ret);
1836  return;
1837  } else if (const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
1838  *ret = cling::Value(QT, *fInterp);
1839  switch (BT->getKind()) {
1840  case BuiltinType::Void:
1841  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1842  exec(address, 0);
1843  return;
1844  break;
1845 
1846  //
1847  // Unsigned Types
1848  //
1849  case BuiltinType::Bool:
1850  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1851  execWithULL<bool>(address, QT, ret);
1852  return;
1853  break;
1854  case BuiltinType::Char_U: // char on targets where it is unsigned
1855  case BuiltinType::UChar:
1856  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1857  execWithULL<char>(address, QT, ret);
1858  return;
1859  break;
1860  case BuiltinType::WChar_U:
1861  // wchar_t on targets where it is unsigned.
1862  // The standard doesn't allow to specify signednedd of wchar_t
1863  // thus this maps simply to wchar_t.
1864  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1865  execWithULL<wchar_t>(address, QT, ret);
1866  return;
1867  break;
1868  case BuiltinType::Char16:
1869  ::Error("TClingCallFunc::exec_with_valref_return",
1870  "Invalid type 'char16_t'!");
1871  return;
1872  break;
1873  case BuiltinType::Char32:
1874  ::Error("TClingCallFunc::exec_with_valref_return",
1875  "Invalid type 'char32_t'!");
1876  return;
1877  break;
1878  case BuiltinType::UShort:
1879  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1880  execWithULL<unsigned short>(address, QT, ret);
1881  return;
1882  break;
1883  case BuiltinType::UInt:
1884  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1885  execWithULL<unsigned int>(address, QT, ret);
1886  return;
1887  break;
1888  case BuiltinType::ULong:
1889  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1890  execWithULL<unsigned long>(address, QT, ret);
1891  return;
1892  break;
1893  case BuiltinType::ULongLong:
1894  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1895  execWithULL<unsigned long long>(address, QT, ret);
1896  return;
1897  break;
1898  case BuiltinType::UInt128: {
1899  ::Error("TClingCallFunc::exec_with_valref_return",
1900  "Invalid type '__uint128_t'!");
1901  return;
1902  }
1903  break;
1904 
1905  //
1906  // Signed Types
1907  //
1908  case BuiltinType::Char_S: // char on targets where it is signed
1909  case BuiltinType::SChar:
1910  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1911  execWithLL<signed char>(address, QT, ret);
1912  return;
1913  break;
1914  case BuiltinType::WChar_S:
1915  // wchar_t on targets where it is signed.
1916  // The standard doesn't allow to specify signednedd of wchar_t
1917  // thus this maps simply to wchar_t.
1918  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1919  execWithLL<wchar_t>(address, QT, ret);
1920  return;
1921  break;
1922  case BuiltinType::Short:
1923  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1924  execWithLL<short>(address, QT, ret);
1925  return;
1926  break;
1927  case BuiltinType::Int:
1928  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1929  execWithLL<int>(address, QT, ret);
1930  return;
1931  break;
1932  case BuiltinType::Long:
1933  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1934  execWithLL<long>(address, QT, ret);
1935  return;
1936  break;
1937  case BuiltinType::LongLong:
1938  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1939  execWithLL<long long>(address, QT, ret);
1940  return;
1941  break;
1942  case BuiltinType::Int128:
1943  ::Error("TClingCallFunc::exec_with_valref_return",
1944  "Invalid type '__int128_t'!");
1945  return;
1946  break;
1947  case BuiltinType::Half:
1948  // half in OpenCL, __fp16 in ARM NEON
1949  ::Error("TClingCallFunc::exec_with_valref_return",
1950  "Invalid type 'Half'!");
1951  return;
1952  break;
1953  case BuiltinType::Float:
1954  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1955  exec(address, &ret->getFloat());
1956  return;
1957  break;
1958  case BuiltinType::Double:
1959  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1960  exec(address, &ret->getDouble());
1961  return;
1962  break;
1963  case BuiltinType::LongDouble:
1964  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1965  exec(address, &ret->getLongDouble());
1966  return;
1967  break;
1968  //
1969  // Language-Specific Types
1970  //
1971  case BuiltinType::NullPtr:
1972  // C++11 nullptr
1973  ::Error("TClingCallFunc::exec_with_valref_return",
1974  "Invalid type 'nullptr'!");
1975  return;
1976  break;
1977  default:
1978  break;
1979  }
1980  }
1981  ::Error("TClingCallFunc::exec_with_valref_return",
1982  "Unrecognized return type!");
1983  QT->dump();
1984  return;
1985 }
1986 
1987 void TClingCallFunc::EvaluateArgList(const string &ArgList)
1988 {
1989  SmallVector<Expr *, 4> exprs;
1990  fInterp->getLookupHelper().findArgList(ArgList, exprs,
1991  gDebug > 5 ? cling::LookupHelper::WithDiagnostics
1992  : cling::LookupHelper::NoDiagnostics);
1993  for (SmallVectorImpl<Expr *>::const_iterator I = exprs.begin(),
1994  E = exprs.end(); I != E; ++I) {
1995  cling::Value val;
1996  EvaluateExpr(*fInterp, *I, val);
1997  if (!val.isValid()) {
1998  // Bad expression, all done.
1999  ::Error("TClingCallFunc::EvaluateArgList",
2000  "Bad expression in parameter %d of '%s'!",
2001  (int)(I - exprs.begin()),
2002  ArgList.c_str());
2003  return;
2004  }
2005  fArgVals.push_back(val);
2006  }
2007 }
2008 
2009 void TClingCallFunc::Exec(void *address, TInterpreterValue *interpVal/*=0*/)
2010 {
2011  IFacePtr();
2012  if (!fWrapper) {
2013  ::Error("TClingCallFunc::Exec(address, interpVal)",
2014  "Called with no wrapper, not implemented!");
2015  return;
2016  }
2017  if (!interpVal) {
2018  exec(address, 0);
2019  return;
2020  }
2021  cling::Value *val = reinterpret_cast<cling::Value *>(interpVal->GetValAddr());
2022  exec_with_valref_return(address, val);
2023 }
2024 
2025 template <typename T>
2026 T TClingCallFunc::ExecT(void *address)
2027 {
2028  IFacePtr();
2029  if (!fWrapper) {
2030  ::Error("TClingCallFunc::ExecT",
2031  "Called with no wrapper, not implemented!");
2032  return 0;
2033  }
2034  cling::Value ret;
2035  exec_with_valref_return(address, &ret);
2036  if (!ret.isValid()) {
2037  // Sometimes we are called on a function returning void!
2038  return 0;
2039  }
2040 
2041  if (fReturnIsRecordType)
2042  ((TCling *)gCling)->RegisterTemporary(ret);
2043  return sv_to<T>(ret);
2044 }
2045 
2047 {
2048  return ExecT<long>(address);
2049 }
2050 
2051 long long TClingCallFunc::ExecInt64(void *address)
2052 {
2053  return ExecT<long long>(address);
2054 }
2055 
2056 double TClingCallFunc::ExecDouble(void *address)
2057 {
2058  return ExecT<double>(address);
2059 }
2060 
2061 void TClingCallFunc::ExecWithArgsAndReturn(void *address, const void *args[] /*= 0*/,
2062  int nargs /*= 0*/, void *ret/*= 0*/)
2063 {
2064  IFacePtr();
2065  if (!fWrapper) {
2066  ::Error("TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2067  "Called with no wrapper, not implemented!");
2068  return;
2069  }
2070  (*fWrapper)(address, nargs, const_cast<void **>(args), ret);
2071 }
2072 
2073 void TClingCallFunc::ExecWithReturn(void *address, void *ret/*= 0*/)
2074 {
2075  IFacePtr();
2076  if (!fWrapper) {
2077  ::Error("TClingCallFunc::ExecWithReturn(address, ret)",
2078  "Called with no wrapper, not implemented!");
2079  return;
2080  }
2081  exec(address, ret);
2082 }
2083 
2084 void *TClingCallFunc::ExecDefaultConstructor(const TClingClassInfo *info, void *address /*=0*/,
2085  unsigned long nary /*= 0UL*/)
2086 {
2087  if (!info->IsValid()) {
2088  ::Error("TClingCallFunc::ExecDefaultConstructor", "Invalid class info!");
2089  return 0;
2090  }
2091  tcling_callfunc_ctor_Wrapper_t wrapper = 0;
2092  {
2094  const Decl *D = info->GetDecl();
2095  //if (!info->HasDefaultConstructor()) {
2096  // // FIXME: We might have a ROOT ioctor, we might
2097  // // have to check for that here.
2098  // ::Error("TClingCallFunc::ExecDefaultConstructor",
2099  // "Class has no default constructor: %s",
2100  // info->Name());
2101  // return 0;
2102  //}
2103  map<const Decl *, void *>::iterator I = gCtorWrapperStore.find(D);
2104  if (I != gCtorWrapperStore.end()) {
2105  wrapper = (tcling_callfunc_ctor_Wrapper_t) I->second;
2106  } else {
2107  wrapper = make_ctor_wrapper(info);
2108  }
2109  }
2110  if (!wrapper) {
2111  ::Error("TClingCallFunc::ExecDefaultConstructor",
2112  "Called with no wrapper, not implemented!");
2113  return 0;
2114  }
2115  void *obj = 0;
2116  (*wrapper)(&obj, address, nary);
2117  return obj;
2118 }
2119 
2120 void TClingCallFunc::ExecDestructor(const TClingClassInfo *info, void *address /*=0*/,
2121  unsigned long nary /*= 0UL*/, bool withFree /*= true*/)
2122 {
2123  if (!info->IsValid()) {
2124  ::Error("TClingCallFunc::ExecDestructor", "Invalid class info!");
2125  return;
2126  }
2127 
2128  tcling_callfunc_dtor_Wrapper_t wrapper = 0;
2129  {
2131  const Decl *D = info->GetDecl();
2132  map<const Decl *, void *>::iterator I = gDtorWrapperStore.find(D);
2133  if (I != gDtorWrapperStore.end()) {
2134  wrapper = (tcling_callfunc_dtor_Wrapper_t) I->second;
2135  } else {
2136  wrapper = make_dtor_wrapper(info);
2137  }
2138  }
2139  if (!wrapper) {
2140  ::Error("TClingCallFunc::ExecDestructor",
2141  "Called with no wrapper, not implemented!");
2142  return;
2143  }
2144  (*wrapper)(address, nary, withFree);
2145 }
2146 
2149 {
2150  return new TClingMethodInfo(*fMethod);
2151 }
2152 
2154 {
2155  delete fMethod;
2156  fMethod = 0;
2157  fWrapper = 0;
2158  ResetArg();
2159 }
2160 
2162 {
2163  delete fMethod;
2164  fMethod = new TClingMethodInfo(*minfo);
2165  fWrapper = 0;
2166  ResetArg();
2167 }
2168 
2170 {
2171  if (!IsValid()) {
2172  return 0;
2173  }
2174  if (!fWrapper) {
2175  const FunctionDecl *decl = fMethod->GetMethodDecl();
2176 
2178  map<const FunctionDecl *, void *>::iterator I = gWrapperStore.find(decl);
2179  if (I != gWrapperStore.end()) {
2180  fWrapper = (tcling_callfunc_Wrapper_t) I->second;
2181  } else {
2182  fWrapper = make_wrapper();
2183  }
2184  }
2185  return (void *)fWrapper;
2186 }
2187 
2189 {
2190  if (!fMethod) {
2191  return false;
2192  }
2193  return fMethod->IsValid();
2194 }
2195 
2197 {
2198  if (!IsValid()) {
2199  ::Error("TClingCallFunc::IFacePtr(kind)",
2200  "Attempt to get interface while invalid.");
2202  }
2203  if (!fWrapper) {
2204  const FunctionDecl *decl = fMethod->GetMethodDecl();
2205 
2207  map<const FunctionDecl *, void *>::iterator I = gWrapperStore.find(decl);
2208  if (I != gWrapperStore.end()) {
2209  fWrapper = (tcling_callfunc_Wrapper_t) I->second;
2210  } else {
2211  fWrapper = make_wrapper();
2212  }
2213 
2214  fReturnIsRecordType = decl->getReturnType().getCanonicalType()->isRecordType();
2215  }
2216  return TInterpreter::CallFuncIFacePtr_t(fWrapper);
2217 }
2218 
2219 
2221 {
2222  fArgVals.clear();
2223 }
2224 
2225 void TClingCallFunc::SetArg(unsigned long param)
2226 {
2227  ASTContext &C = fInterp->getCI()->getASTContext();
2228  fArgVals.push_back(cling::Value(C.UnsignedLongTy, *fInterp));
2229  fArgVals.back().getLL() = param;
2230 }
2231 
2232 void TClingCallFunc::SetArg(long param)
2233 {
2234  ASTContext &C = fInterp->getCI()->getASTContext();
2235  fArgVals.push_back(cling::Value(C.LongTy, *fInterp));
2236  fArgVals.back().getLL() = param;
2237 }
2238 
2239 void TClingCallFunc::SetArg(float param)
2240 {
2241  ASTContext &C = fInterp->getCI()->getASTContext();
2242  fArgVals.push_back(cling::Value(C.FloatTy, *fInterp));
2243  fArgVals.back().getFloat() = param;
2244 }
2245 
2246 void TClingCallFunc::SetArg(double param)
2247 {
2248  ASTContext &C = fInterp->getCI()->getASTContext();
2249  fArgVals.push_back(cling::Value(C.DoubleTy, *fInterp));
2250  fArgVals.back().getDouble() = param;
2251 }
2252 
2253 void TClingCallFunc::SetArg(long long param)
2254 {
2255  ASTContext &C = fInterp->getCI()->getASTContext();
2256  fArgVals.push_back(cling::Value(C.LongLongTy, *fInterp));
2257  fArgVals.back().getLL() = param;
2258 }
2259 
2260 void TClingCallFunc::SetArg(unsigned long long param)
2261 {
2262  ASTContext &C = fInterp->getCI()->getASTContext();
2263  fArgVals.push_back(cling::Value(C.UnsignedLongLongTy, *fInterp));
2264  fArgVals.back().getULL() = param;
2265 }
2266 
2267 void TClingCallFunc::SetArgArray(long *paramArr, int nparam)
2268 {
2269  ResetArg();
2270  for (int i = 0; i < nparam; ++i) {
2271  SetArg(paramArr[i]);
2272  }
2273 }
2274 
2275 void TClingCallFunc::SetArgs(const char *params)
2276 {
2277  ResetArg();
2278  EvaluateArgList(params);
2279 }
2280 
2281 void TClingCallFunc::SetFunc(const TClingClassInfo *info, const char *method, const char *arglist,
2282  long *poffset)
2283 {
2284  SetFunc(info, method, arglist, false, poffset);
2285 }
2286 
2287 void TClingCallFunc::SetFunc(const TClingClassInfo *info, const char *method, const char *arglist,
2288  bool objectIsConst, long *poffset)
2289 {
2290  fWrapper = 0;
2291  delete fMethod;
2292  fMethod = new TClingMethodInfo(fInterp);
2293  if (poffset) {
2294  *poffset = 0L;
2295  }
2296  ResetArg();
2297  if (!info->IsValid()) {
2298  ::Error("TClingCallFunc::SetFunc", "Class info is invalid!");
2299  return;
2300  }
2301  if (!strcmp(arglist, ")")) {
2302  // CINT accepted a single right paren as meaning no arguments.
2303  arglist = "";
2304  }
2305  *fMethod = info->GetMethodWithArgs(method, arglist, objectIsConst, poffset);
2306  if (!fMethod->IsValid()) {
2307  //::Error("TClingCallFunc::SetFunc", "Could not find method %s(%s)", method,
2308  // arglist);
2309  return;
2310  }
2311  // FIXME: The arglist was already parsed by the lookup, we should
2312  // enhance the lookup to return the resulting expression
2313  // list so we do not need to parse it again here.
2314  EvaluateArgList(arglist);
2315 }
2316 
2318 {
2319  fWrapper = 0;
2320  delete fMethod;
2321  fMethod = new TClingMethodInfo(*info);
2322  ResetArg();
2323  if (!fMethod->IsValid()) {
2324  return;
2325  }
2326 }
2327 
2328 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2329  const char *proto, long *poffset,
2330  EFunctionMatchMode mode/*=kConversionMatch*/)
2331 {
2332  SetFuncProto(info, method, proto, false, poffset, mode);
2333 }
2334 
2335 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2336  const char *proto, bool objectIsConst, long *poffset,
2337  EFunctionMatchMode mode/*=kConversionMatch*/)
2338 {
2339  fWrapper = 0;
2340  delete fMethod;
2341  fMethod = new TClingMethodInfo(fInterp);
2342  if (poffset) {
2343  *poffset = 0L;
2344  }
2345  ResetArg();
2346  if (!info->IsValid()) {
2347  ::Error("TClingCallFunc::SetFuncProto", "Class info is invalid!");
2348  return;
2349  }
2350  *fMethod = info->GetMethod(method, proto, objectIsConst, poffset, mode);
2351  if (!fMethod->IsValid()) {
2352  //::Error("TClingCallFunc::SetFuncProto", "Could not find method %s(%s)",
2353  // method, proto);
2354  return;
2355  }
2356 }
2357 
2358 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2359  const llvm::SmallVectorImpl<clang::QualType> &proto, long *poffset,
2360  EFunctionMatchMode mode/*=kConversionMatch*/)
2361 {
2362  SetFuncProto(info, method, proto, false, poffset, mode);
2363 }
2364 
2365 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2366  const llvm::SmallVectorImpl<clang::QualType> &proto,
2367  bool objectIsConst, long *poffset,
2368  EFunctionMatchMode mode/*=kConversionMatch*/)
2369 {
2370  delete fMethod;
2371  fMethod = new TClingMethodInfo(fInterp);
2372  if (poffset) {
2373  *poffset = 0L;
2374  }
2375  ResetArg();
2376  if (!info->IsValid()) {
2377  ::Error("TClingCallFunc::SetFuncProto", "Class info is invalid!");
2378  return;
2379  }
2380  *fMethod = info->GetMethod(method, proto, objectIsConst, poffset, mode);
2381  if (!fMethod->IsValid()) {
2382  //::Error("TClingCallFunc::SetFuncProto", "Could not find method %s(%s)",
2383  // method, proto);
2384  return;
2385  }
2386 }
2387 
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)
bool IsValid() const
Definition: TString.h:780
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
return c
const char * Int
RooArgList L(const RooAbsArg &v1)
const clang::Decl * GetDecl() const
double ExecDouble(void *address)
double T(double x)
Definition: ChebyshevPol.h:34
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:46
static map< const FunctionDecl *, void * > gWrapperStore
TClingMethodInfo GetMethod(const char *fname) const
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.
void SetArg(long arg)
#define N
static map< const Decl *, void * > gDtorWrapperStore
const char * Long
Definition: Pattern.h:7
static map< const Decl *, void * > gCtorWrapperStore
STL namespace.
const char * UShort
const char * UInt
void execWithLL(void *address, clang::QualType QT, cling::Value *val) 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)
T ExecT(void *address)
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
EFunctionMatchMode
Definition: TDictionary.h:155
void exec(void *address, void *ret) const
This class defines an interface to the cling C++ interpreter.
Definition: TCling.h:92
void * InterfaceMethod()
const char * UChar
const char * Float
void GetNormalizedName(std::string &norm_name, const clang::QualType &type, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt)
Return the type name normalized for ROOT, keeping only the ROOT opaque typedef (Double32_t, etc.) and adding default template argument for all types except the STL collections where we remove the default template argument if any.
RooArgSet S(const RooAbsArg &v1)
#define F(x, y, z)
static const string kIndentString(" ")
void execWithULL(void *address, clang::QualType QT, cling::Value *val) const
static double C[]
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)
const char * ULong
Double_t E()
Definition: TMath.h:54
TLine * l
Definition: textangle.C:4
const char * Double
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)
long Long_t
Definition: RtypesCore.h:50
tcling_callfunc_Wrapper_t make_wrapper()
#define R__LOCKGUARD_NAMED(name, mutex)
virtual const void * GetValAddr() const =0
Definition: TCling.h:48
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)
const char * Bool
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)())
void exec_with_valref_return(void *address, cling::Value *ret) const
bool IsValid() 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)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
long ExecInt(void *address)
const char * proto
Definition: civetweb.c:11652
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:519
#define I(x, y, z)
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *info)
char name[80]
Definition: TGX11.cxx:109
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.
const char * Value
Definition: TXMLSetup.cxx:73
void ExecWithReturn(void *address, void *ret=0)
TClingMethodInfo GetMethodWithArgs(const char *fname, const char *arglist, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch, EInheritanceMode imode=kWithInheritance) const
const char * Short
long long ExecInt64(void *address)