ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  case BuiltinType::ObjCId: {
1707  // Objective C 'id' type
1708  Error("TClingCallFunc::exec(void*)",
1709  "Invalid type 'ObjCId'!");
1710  return;
1711  }
1712  break;
1713  case BuiltinType::ObjCClass: {
1714  // Objective C 'Class' type
1715  Error("TClingCallFunc::exec(void*)",
1716  "Invalid type 'ObjCClass'!");
1717  return;
1718  }
1719  break;
1720  case BuiltinType::ObjCSel: {
1721  // Objective C 'SEL' type
1722  Error("TClingCallFunc::exec(void*)",
1723  "Invalid type 'ObjCSel'!");
1724  return;
1725  }
1726  break;
1727  case BuiltinType::OCLImage1d: {
1728  // OpenCL image type
1729  Error("TClingCallFunc::exec(void*)",
1730  "Invalid type 'OCLImage1d'!");
1731  return;
1732  }
1733  break;
1734  case BuiltinType::OCLImage1dArray: {
1735  // OpenCL image type
1736  Error("TClingCallFunc::exec(void*)",
1737  "Invalid type 'OCLImage1dArray'!");
1738  return;
1739  }
1740  break;
1741  case BuiltinType::OCLImage1dBuffer: {
1742  // OpenCL image type
1743  Error("TClingCallFunc::exec(void*)",
1744  "Invalid type 'OCLImage1dBuffer'!");
1745  return;
1746  }
1747  break;
1748  case BuiltinType::OCLImage2d: {
1749  // OpenCL image type
1750  Error("TClingCallFunc::exec(void*)",
1751  "Invalid type 'OCLImage2d'!");
1752  return;
1753  }
1754  break;
1755  case BuiltinType::OCLImage2dArray: {
1756  // OpenCL image type
1757  Error("TClingCallFunc::exec(void*)",
1758  "Invalid type 'OCLImage2dArray'!");
1759  return;
1760  }
1761  break;
1762  case BuiltinType::OCLImage3d: {
1763  // OpenCL image type
1764  Error("TClingCallFunc::exec(void*)",
1765  "Invalid type 'OCLImage3d'!");
1766  return;
1767  }
1768  break;
1769  case BuiltinType::OCLSampler: {
1770  // OpenCL sampler_t
1771  Error("TClingCallFunc::exec(void*)",
1772  "Invalid type 'OCLSampler'!");
1773  return;
1774  }
1775  break;
1776  case BuiltinType::OCLEvent: {
1777  // OpenCL event_t
1778  Error("TClingCallFunc::exec(void*)",
1779  "Invalid type 'OCLEvent'!");
1780  return;
1781  }
1782  break;
1783  //
1784  // Placeholder types.
1785  //
1786  // These types are used during intermediate phases
1787  // of semantic analysis. They are eventually resolved
1788  // to one of the preceeding types.
1789  //
1790  case BuiltinType::Dependent: {
1791  // dependent on a template argument
1792  Error("TClingCallFunc::exec(void*)",
1793  "Invalid type 'Dependent'!");
1794  return;
1795  }
1796  break;
1797  case BuiltinType::Overload: {
1798  // an unresolved function overload set
1799  Error("TClingCallFunc::exec(void*)",
1800  "Invalid type 'Overload'!");
1801  return;
1802  }
1803  break;
1804  case BuiltinType::BoundMember: {
1805  // a bound C++ non-static member function
1806  Error("TClingCallFunc::exec(void*)",
1807  "Invalid type 'BoundMember'!");
1808  return;
1809  }
1810  break;
1811  case BuiltinType::PseudoObject: {
1812  // Object C @property or VS.NET __property
1813  Error("TClingCallFunc::exec(void*)",
1814  "Invalid type 'PseudoObject'!");
1815  return;
1816  }
1817  break;
1818  case BuiltinType::UnknownAny: {
1819  // represents an unknown type
1820  Error("TClingCallFunc::exec(void*)",
1821  "Invalid type 'UnknownAny'!");
1822  return;
1823  }
1824  break;
1825  case BuiltinType::BuiltinFn: {
1826  // a compiler builtin function
1827  Error("TClingCallFunc::exec(void*)",
1828  "Invalid type 'BuiltinFn'!");
1829  return;
1830  }
1831  break;
1832  case BuiltinType::ARCUnbridgedCast: {
1833  // Objective C Automatic Reference Counting cast
1834  // which would normally require __bridge, but which
1835  // may be ok because of the context.
1836  Error("TClingCallFunc::exec(void*)",
1837  "Invalid type 'ARCUnbridgedCast'!");
1838  return;
1839  }
1840  break;
1841  default: {
1842  // There should be no others. This is here in case
1843  // this changes in the future.
1844  Error("TClingCallFunc::exec(void*)",
1845  "Invalid builtin type (unrecognized)!");
1846  QT->dump();
1847  return;
1848  }
1849  break;
1850  }
1851  } else if (QT->isReferenceType()) {
1852  // the argument is already a pointer value (point to the same thing
1853  // as the reference.
1854  vp_ary.push_back((void *) sv_to_ulong_long(fArgVals[i]));
1855  } else if (QT->isPointerType() || QT->isArrayType()) {
1856  ValHolder vh;
1857  vh.u.vp = (void *) sv_to_ulong_long(fArgVals[i]);
1858  vh_ary.push_back(vh);
1859  vp_ary.push_back(&vh_ary.back());
1860  } else if (QT->isRecordType()) {
1861  // the argument is already a pointer value (pointing to object passed
1862  // by value).
1863  vp_ary.push_back((void *) sv_to_ulong_long(fArgVals[i]));
1864  } else if (const EnumType *ET =
1865  dyn_cast<EnumType>(&*QT)) {
1866  // Note: We may need to worry about the underlying type
1867  // of the enum here.
1868  (void) ET;
1869  ValHolder vh;
1870  vh.u.i = (int) sv_to_long_long(fArgVals[i]);
1871  vh_ary.push_back(vh);
1872  vp_ary.push_back(&vh_ary.back());
1873  } else if (QT->isMemberPointerType()) {
1874  ValHolder vh;
1875  vh.u.vp = (void *) sv_to_ulong_long(fArgVals[i]);
1876  vh_ary.push_back(vh);
1877  vp_ary.push_back(&vh_ary.back());
1878  } else {
1879  Error("TClingCallFunc::exec(void*)",
1880  "Invalid type (unrecognized)!");
1881  QT->dump();
1882  return;
1883  }
1884  }
1885  } // End of scope holding the lock
1886  (*fWrapper)(address, (int)num_args, (void **)vp_ary.data(), ret);
1887 }
1888 
1889 template <typename T>
1890 void TClingCallFunc::execWithLL(void *address, clang::QualType QT,
1891  cling::Value *val) const
1892 {
1893  T ret; // leave uninit for valgrind's sake!
1894  exec(address, &ret);
1895  val->getLL() = ret;
1896 }
1897 
1898 template <typename T>
1899 void TClingCallFunc::execWithULL(void *address, clang::QualType QT,
1900  cling::Value *val) const
1901 {
1902  T ret; // leave uninit for valgrind's sake!
1903  exec(address, &ret);
1904  val->getULL() = ret;
1905 }
1906 
1908 {
1909  if (!ret) {
1910  exec(address, 0);
1911  return;
1912  }
1913 
1915 
1916  const FunctionDecl *FD = fMethod->GetMethodDecl();
1917 
1918  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
1919  ASTContext &Context = FD->getASTContext();
1920  const TypeDecl *TD = dyn_cast<TypeDecl>(CD->getDeclContext());
1921  QualType ClassTy(TD->getTypeForDecl(), 0);
1922  QualType QT = Context.getLValueReferenceType(ClassTy);
1923  *ret = cling::Value(QT, *fInterp);
1924  // Store the new()'ed address in getPtr()
1925  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1926  exec(address, &ret->getPtr());
1927  return;
1928  }
1929  QualType QT = FD->getReturnType().getCanonicalType();
1930  if (QT->isReferenceType()) {
1931  *ret = cling::Value(QT, *fInterp);
1932  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1933  exec(address, &ret->getPtr());
1934  return;
1935  } else if (QT->isMemberPointerType()) {
1936  const MemberPointerType *MPT = QT->getAs<MemberPointerType>();
1937  if (MPT->isMemberDataPointer()) {
1938  // A member data pointer is a actually a struct with one
1939  // member of ptrdiff_t, the offset from the base of the object
1940  // storage to the storage for the designated data member.
1941  // But that's not relevant: we use it as a non-builtin, allocated
1942  // type.
1943  *ret = cling::Value(QT, *fInterp);
1944  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1945  exec(address, ret->getPtr());
1946  return;
1947  }
1948  // We are a function member pointer.
1949  *ret = cling::Value(QT, *fInterp);
1950  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1951  exec(address, &ret->getPtr());
1952  return;
1953  } else if (QT->isPointerType() || QT->isArrayType()) {
1954  // Note: ArrayType is an illegal function return value type.
1955  *ret = cling::Value(QT, *fInterp);
1956  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1957  exec(address, &ret->getPtr());
1958  return;
1959  } else if (QT->isRecordType()) {
1960  *ret = cling::Value(QT, *fInterp);
1961  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1962  exec(address, ret->getPtr());
1963  return;
1964  } else if (const EnumType *ET = dyn_cast<EnumType>(&*QT)) {
1965  // Note: We may need to worry about the underlying type
1966  // of the enum here.
1967  (void) ET;
1968  *ret = cling::Value(QT, *fInterp);
1969  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1970  execWithLL<int>(address, QT, ret);
1971  return;
1972  } else if (const BuiltinType *BT = dyn_cast<BuiltinType>(&*QT)) {
1973  *ret = cling::Value(QT, *fInterp);
1974  switch (BT->getKind()) {
1975  case BuiltinType::Void:
1976  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1977  exec(address, 0);
1978  return;
1979  break;
1980 
1981  //
1982  // Unsigned Types
1983  //
1984  case BuiltinType::Bool:
1985  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1986  execWithULL<bool>(address, QT, ret);
1987  return;
1988  break;
1989  case BuiltinType::Char_U: // char on targets where it is unsigned
1990  case BuiltinType::UChar:
1991  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
1992  execWithULL<char>(address, QT, ret);
1993  return;
1994  break;
1995  case BuiltinType::WChar_U:
1996  // wchar_t on targets where it is unsigned.
1997  // The standard doesn't allow to specify signednedd of wchar_t
1998  // thus this maps simply to wchar_t.
1999  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2000  execWithULL<wchar_t>(address, QT, ret);
2001  return;
2002  break;
2003  case BuiltinType::Char16:
2004  Error("TClingCallFunc::exec_with_valref_return",
2005  "Invalid type 'char16_t'!");
2006  return;
2007  break;
2008  case BuiltinType::Char32:
2009  Error("TClingCallFunc::exec_with_valref_return",
2010  "Invalid type 'char32_t'!");
2011  return;
2012  break;
2013  case BuiltinType::UShort:
2014  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2015  execWithULL<unsigned short>(address, QT, ret);
2016  return;
2017  break;
2018  case BuiltinType::UInt:
2019  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2020  execWithULL<unsigned int>(address, QT, ret);
2021  return;
2022  break;
2023  case BuiltinType::ULong:
2024  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2025  execWithULL<unsigned long>(address, QT, ret);
2026  return;
2027  break;
2028  case BuiltinType::ULongLong:
2029  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2030  execWithULL<unsigned long long>(address, QT, ret);
2031  return;
2032  break;
2033  case BuiltinType::UInt128: {
2034  Error("TClingCallFunc::exec_with_valref_return",
2035  "Invalid type '__uint128_t'!");
2036  return;
2037  }
2038  break;
2039 
2040  //
2041  // Signed Types
2042  //
2043  case BuiltinType::Char_S: // char on targets where it is signed
2044  case BuiltinType::SChar:
2045  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2046  execWithLL<signed char>(address, QT, ret);
2047  return;
2048  break;
2049  case BuiltinType::WChar_S:
2050  // wchar_t on targets where it is signed.
2051  // The standard doesn't allow to specify signednedd of wchar_t
2052  // thus this maps simply to wchar_t.
2053  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2054  execWithLL<wchar_t>(address, QT, ret);
2055  return;
2056  break;
2057  case BuiltinType::Short:
2058  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2059  execWithLL<short>(address, QT, ret);
2060  return;
2061  break;
2062  case BuiltinType::Int:
2063  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2064  execWithLL<int>(address, QT, ret);
2065  return;
2066  break;
2067  case BuiltinType::Long:
2068  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2069  execWithLL<long>(address, QT, ret);
2070  return;
2071  break;
2072  case BuiltinType::LongLong:
2073  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2074  execWithLL<long long>(address, QT, ret);
2075  return;
2076  break;
2077  case BuiltinType::Int128:
2078  Error("TClingCallFunc::exec_with_valref_return",
2079  "Invalid type '__int128_t'!");
2080  return;
2081  break;
2082  case BuiltinType::Half:
2083  // half in OpenCL, __fp16 in ARM NEON
2084  Error("TClingCallFunc::exec_with_valref_return",
2085  "Invalid type 'Half'!");
2086  return;
2087  break;
2088  case BuiltinType::Float:
2089  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2090  exec(address, &ret->getFloat());
2091  return;
2092  break;
2093  case BuiltinType::Double:
2094  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2095  exec(address, &ret->getDouble());
2096  return;
2097  break;
2098  case BuiltinType::LongDouble:
2099  R__LOCKGUARD_UNLOCK(global); // Release lock during user function execution
2100  exec(address, &ret->getLongDouble());
2101  return;
2102  break;
2103  //
2104  // Language-Specific Types
2105  //
2106  case BuiltinType::NullPtr:
2107  // C++11 nullptr
2108  Error("TClingCallFunc::exec_with_valref_return",
2109  "Invalid type 'nullptr'!");
2110  return;
2111  break;
2112  default:
2113  break;
2114  }
2115  }
2116  Error("TClingCallFunc::exec_with_valref_return",
2117  "Unrecognized return type!");
2118  QT->dump();
2119  return;
2120 }
2121 
2122 void TClingCallFunc::EvaluateArgList(const string &ArgList)
2123 {
2124  SmallVector<Expr *, 4> exprs;
2125  fInterp->getLookupHelper().findArgList(ArgList, exprs,
2126  gDebug > 5 ? cling::LookupHelper::WithDiagnostics
2127  : cling::LookupHelper::NoDiagnostics);
2128  for (SmallVectorImpl<Expr *>::const_iterator I = exprs.begin(),
2129  E = exprs.end(); I != E; ++I) {
2130  cling::Value val;
2131  EvaluateExpr(*fInterp, *I, val);
2132  if (!val.isValid()) {
2133  // Bad expression, all done.
2134  Error("TClingCallFunc::EvaluateArgList",
2135  "Bad expression in parameter %d of '%s'!",
2136  (int)(I - exprs.begin()),
2137  ArgList.c_str());
2138  return;
2139  }
2140  fArgVals.push_back(val);
2141  }
2142 }
2143 
2144 void TClingCallFunc::Exec(void *address, TInterpreterValue *interpVal/*=0*/)
2145 {
2146  IFacePtr();
2147  if (!fWrapper) {
2148  Error("TClingCallFunc::Exec(address, interpVal)",
2149  "Called with no wrapper, not implemented!");
2150  return;
2151  }
2152  if (!interpVal) {
2153  exec(address, 0);
2154  return;
2155  }
2156  cling::Value *val = reinterpret_cast<cling::Value *>(interpVal->GetValAddr());
2157  exec_with_valref_return(address, val);
2158 }
2159 
2160 template <typename T>
2161 T TClingCallFunc::ExecT(void *address)
2162 {
2163  IFacePtr();
2164  if (!fWrapper) {
2165  Error("TClingCallFunc::ExecT",
2166  "Called with no wrapper, not implemented!");
2167  return 0;
2168  }
2169  cling::Value ret;
2170  exec_with_valref_return(address, &ret);
2171  if (!ret.isValid()) {
2172  // Sometimes we are called on a function returning void!
2173  return 0;
2174  }
2175 
2176  if (fReturnIsRecordType)
2177  ((TCling *)gCling)->RegisterTemporary(ret);
2178  return sv_to<T>(ret);
2179 }
2180 
2182 {
2183  return ExecT<long>(address);
2184 }
2185 
2186 long long TClingCallFunc::ExecInt64(void *address)
2187 {
2188  return ExecT<long long>(address);
2189 }
2190 
2191 double TClingCallFunc::ExecDouble(void *address)
2192 {
2193  return ExecT<double>(address);
2194 }
2195 
2196 void TClingCallFunc::ExecWithArgsAndReturn(void *address, const void *args[] /*= 0*/,
2197  int nargs /*= 0*/, void *ret/*= 0*/)
2198 {
2199  IFacePtr();
2200  if (!fWrapper) {
2201  Error("TClingCallFunc::ExecWithArgsAndReturn(address, args, ret)",
2202  "Called with no wrapper, not implemented!");
2203  return;
2204  }
2205  (*fWrapper)(address, nargs, const_cast<void **>(args), ret);
2206 }
2207 
2208 void TClingCallFunc::ExecWithReturn(void *address, void *ret/*= 0*/)
2209 {
2210  IFacePtr();
2211  if (!fWrapper) {
2212  Error("TClingCallFunc::ExecWithReturn(address, ret)",
2213  "Called with no wrapper, not implemented!");
2214  return;
2215  }
2216  exec(address, ret);
2217 }
2218 
2219 void *TClingCallFunc::ExecDefaultConstructor(const TClingClassInfo *info, void *address /*=0*/,
2220  unsigned long nary /*= 0UL*/)
2221 {
2222  if (!info->IsValid()) {
2223  Error("TClingCallFunc::ExecDefaultConstructor", "Invalid class info!");
2224  return 0;
2225  }
2226  tcling_callfunc_ctor_Wrapper_t wrapper = 0;
2227  {
2229  const Decl *D = info->GetDecl();
2230  //if (!info->HasDefaultConstructor()) {
2231  // // FIXME: We might have a ROOT ioctor, we might
2232  // // have to check for that here.
2233  // Error("TClingCallFunc::ExecDefaultConstructor",
2234  // "Class has no default constructor: %s",
2235  // info->Name());
2236  // return 0;
2237  //}
2238  map<const Decl *, void *>::iterator I = gCtorWrapperStore.find(D);
2239  if (I != gCtorWrapperStore.end()) {
2240  wrapper = (tcling_callfunc_ctor_Wrapper_t) I->second;
2241  } else {
2242  wrapper = make_ctor_wrapper(info);
2243  }
2244  }
2245  if (!wrapper) {
2246  Error("TClingCallFunc::ExecDefaultConstructor",
2247  "Called with no wrapper, not implemented!");
2248  return 0;
2249  }
2250  void *obj = 0;
2251  (*wrapper)(&obj, address, nary);
2252  return obj;
2253 }
2254 
2255 void TClingCallFunc::ExecDestructor(const TClingClassInfo *info, void *address /*=0*/,
2256  unsigned long nary /*= 0UL*/, bool withFree /*= true*/)
2257 {
2258  if (!info->IsValid()) {
2259  Error("TClingCallFunc::ExecDestructor", "Invalid class info!");
2260  return;
2261  }
2262 
2263  tcling_callfunc_dtor_Wrapper_t wrapper = 0;
2264  {
2266  const Decl *D = info->GetDecl();
2267  map<const Decl *, void *>::iterator I = gDtorWrapperStore.find(D);
2268  if (I != gDtorWrapperStore.end()) {
2269  wrapper = (tcling_callfunc_dtor_Wrapper_t) I->second;
2270  } else {
2271  wrapper = make_dtor_wrapper(info);
2272  }
2273  }
2274  if (!wrapper) {
2275  Error("TClingCallFunc::ExecDestructor",
2276  "Called with no wrapper, not implemented!");
2277  return;
2278  }
2279  (*wrapper)(address, nary, withFree);
2280 }
2281 
2284 {
2285  return new TClingMethodInfo(*fMethod);
2286 }
2287 
2289 {
2290  delete fMethod;
2291  fMethod = 0;
2292  fWrapper = 0;
2293  ResetArg();
2294 }
2295 
2297 {
2298  delete fMethod;
2299  fMethod = new TClingMethodInfo(*minfo);
2300  fWrapper = 0;
2301  ResetArg();
2302 }
2303 
2305 {
2306  if (!IsValid()) {
2307  return 0;
2308  }
2309  if (!fWrapper) {
2310  const FunctionDecl *decl = fMethod->GetMethodDecl();
2311 
2313  map<const FunctionDecl *, void *>::iterator I = gWrapperStore.find(decl);
2314  if (I != gWrapperStore.end()) {
2315  fWrapper = (tcling_callfunc_Wrapper_t) I->second;
2316  } else {
2317  fWrapper = make_wrapper();
2318  }
2319  }
2320  return (void *)fWrapper;
2321 }
2322 
2324 {
2325  if (!fMethod) {
2326  return false;
2327  }
2328  return fMethod->IsValid();
2329 }
2330 
2332 {
2333  if (!IsValid()) {
2334  Error("TClingCallFunc::IFacePtr(kind)",
2335  "Attempt to get interface while invalid.");
2337  }
2338  if (!fWrapper) {
2339  const FunctionDecl *decl = fMethod->GetMethodDecl();
2340 
2342  map<const FunctionDecl *, void *>::iterator I = gWrapperStore.find(decl);
2343  if (I != gWrapperStore.end()) {
2344  fWrapper = (tcling_callfunc_Wrapper_t) I->second;
2345  } else {
2346  fWrapper = make_wrapper();
2347  }
2348 
2349  fReturnIsRecordType = decl->getReturnType().getCanonicalType()->isRecordType();
2350  }
2351  return TInterpreter::CallFuncIFacePtr_t(fWrapper);
2352 }
2353 
2354 
2356 {
2357  fArgVals.clear();
2358 }
2359 
2360 void TClingCallFunc::SetArg(unsigned long param)
2361 {
2362  ASTContext &C = fInterp->getCI()->getASTContext();
2363  fArgVals.push_back(cling::Value(C.UnsignedLongTy, *fInterp));
2364  fArgVals.back().getLL() = param;
2365 }
2366 
2367 void TClingCallFunc::SetArg(long param)
2368 {
2369  ASTContext &C = fInterp->getCI()->getASTContext();
2370  fArgVals.push_back(cling::Value(C.LongTy, *fInterp));
2371  fArgVals.back().getLL() = param;
2372 }
2373 
2374 void TClingCallFunc::SetArg(float param)
2375 {
2376  ASTContext &C = fInterp->getCI()->getASTContext();
2377  fArgVals.push_back(cling::Value(C.FloatTy, *fInterp));
2378  fArgVals.back().getFloat() = param;
2379 }
2380 
2381 void TClingCallFunc::SetArg(double param)
2382 {
2383  ASTContext &C = fInterp->getCI()->getASTContext();
2384  fArgVals.push_back(cling::Value(C.DoubleTy, *fInterp));
2385  fArgVals.back().getDouble() = param;
2386 }
2387 
2388 void TClingCallFunc::SetArg(long long param)
2389 {
2390  ASTContext &C = fInterp->getCI()->getASTContext();
2391  fArgVals.push_back(cling::Value(C.LongLongTy, *fInterp));
2392  fArgVals.back().getLL() = param;
2393 }
2394 
2395 void TClingCallFunc::SetArg(unsigned long long param)
2396 {
2397  ASTContext &C = fInterp->getCI()->getASTContext();
2398  fArgVals.push_back(cling::Value(C.UnsignedLongLongTy, *fInterp));
2399  fArgVals.back().getULL() = param;
2400 }
2401 
2402 void TClingCallFunc::SetArgArray(long *paramArr, int nparam)
2403 {
2404  ResetArg();
2405  for (int i = 0; i < nparam; ++i) {
2406  SetArg(paramArr[i]);
2407  }
2408 }
2409 
2410 void TClingCallFunc::SetArgs(const char *params)
2411 {
2412  ResetArg();
2413  EvaluateArgList(params);
2414 }
2415 
2416 void TClingCallFunc::SetFunc(const TClingClassInfo *info, const char *method, const char *arglist,
2417  long *poffset)
2418 {
2419  SetFunc(info, method, arglist, false, poffset);
2420 }
2421 
2422 void TClingCallFunc::SetFunc(const TClingClassInfo *info, const char *method, const char *arglist,
2423  bool objectIsConst, long *poffset)
2424 {
2425  fWrapper = 0;
2426  delete fMethod;
2427  fMethod = new TClingMethodInfo(fInterp);
2428  if (poffset) {
2429  *poffset = 0L;
2430  }
2431  ResetArg();
2432  if (!info->IsValid()) {
2433  Error("TClingCallFunc::SetFunc", "Class info is invalid!");
2434  return;
2435  }
2436  if (!strcmp(arglist, ")")) {
2437  // CINT accepted a single right paren as meaning no arguments.
2438  arglist = "";
2439  }
2440  *fMethod = info->GetMethodWithArgs(method, arglist, objectIsConst, poffset);
2441  if (!fMethod->IsValid()) {
2442  //Error("TClingCallFunc::SetFunc", "Could not find method %s(%s)", method,
2443  // arglist);
2444  return;
2445  }
2446  // FIXME: The arglist was already parsed by the lookup, we should
2447  // enhance the lookup to return the resulting expression
2448  // list so we do not need to parse it again here.
2449  EvaluateArgList(arglist);
2450 }
2451 
2453 {
2454  fWrapper = 0;
2455  delete fMethod;
2456  fMethod = new TClingMethodInfo(*info);
2457  ResetArg();
2458  if (!fMethod->IsValid()) {
2459  return;
2460  }
2461 }
2462 
2463 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2464  const char *proto, long *poffset,
2465  EFunctionMatchMode mode/*=kConversionMatch*/)
2466 {
2467  SetFuncProto(info, method, proto, false, poffset, mode);
2468 }
2469 
2470 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2471  const char *proto, bool objectIsConst, long *poffset,
2472  EFunctionMatchMode mode/*=kConversionMatch*/)
2473 {
2474  fWrapper = 0;
2475  delete fMethod;
2476  fMethod = new TClingMethodInfo(fInterp);
2477  if (poffset) {
2478  *poffset = 0L;
2479  }
2480  ResetArg();
2481  if (!info->IsValid()) {
2482  Error("TClingCallFunc::SetFuncProto", "Class info is invalid!");
2483  return;
2484  }
2485  *fMethod = info->GetMethod(method, proto, objectIsConst, poffset, mode);
2486  if (!fMethod->IsValid()) {
2487  //Error("TClingCallFunc::SetFuncProto", "Could not find method %s(%s)",
2488  // method, proto);
2489  return;
2490  }
2491 }
2492 
2493 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2494  const llvm::SmallVectorImpl<clang::QualType> &proto, long *poffset,
2495  EFunctionMatchMode mode/*=kConversionMatch*/)
2496 {
2497  SetFuncProto(info, method, proto, false, poffset, mode);
2498 }
2499 
2500 void TClingCallFunc::SetFuncProto(const TClingClassInfo *info, const char *method,
2501  const llvm::SmallVectorImpl<clang::QualType> &proto,
2502  bool objectIsConst, long *poffset,
2503  EFunctionMatchMode mode/*=kConversionMatch*/)
2504 {
2505  delete fMethod;
2506  fMethod = new TClingMethodInfo(fInterp);
2507  if (poffset) {
2508  *poffset = 0L;
2509  }
2510  ResetArg();
2511  if (!info->IsValid()) {
2512  Error("TClingCallFunc::SetFuncProto", "Class info is invalid!");
2513  return;
2514  }
2515  *fMethod = info->GetMethod(method, proto, objectIsConst, poffset, mode);
2516  if (!fMethod->IsValid()) {
2517  //Error("TClingCallFunc::SetFuncProto", "Could not find method %s(%s)",
2518  // method, proto);
2519  return;
2520  }
2521 }
2522 
void collect_type_info(clang::QualType &QT, std::ostringstream &typedefbuf, std::ostringstream &callbuf, std::string &type_name, bool &isReference, bool &isPointer, int indent_level, bool forArgument)
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
bool IsValid() const
return c
const char * Int
RooArgList L(const RooAbsArg &v1)
double ExecDouble(void *address)
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:46
static map< const FunctionDecl *, void * > gWrapperStore
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
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
void execWithLL(void *address, clang::QualType QT, cling::Value *val) const
const char * Long
TClingMethodInfo GetMethod(const char *fname) const
static map< const Decl *, void * > gCtorWrapperStore
const char * UShort
const char * UInt
void exec_with_valref_return(void *address, cling::Value *ret) const
TTree * T
void Exec(void *address, TInterpreterValue *interpVal=0)
TInterpreter::CallFuncIFacePtr_t IFacePtr()
static unsigned long long gWrapperSerial
void SetArgs(const char *args)
T ExecT(void *address)
EFunctionMatchMode
Definition: TDictionary.h:155
This class defines an interface to the cling C++ interpreter.
Definition: TCling.h:92
void exec(void *address, void *ret) const
char * out
Definition: TBase64.cxx:29
void * InterfaceMethod()
const char * UChar
const char * Float
void execWithULL(void *address, clang::QualType QT, cling::Value *val) const
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.
#define F(x, y, z)
static const string kIndentString(" ")
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
const clang::Decl * GetDecl() const
tcling_callfunc_Wrapper_t make_wrapper()
#define R__LOCKGUARD_NAMED(name, mutex)
virtual const void * GetValAddr() const =0
static const float S
Definition: mandel.cpp:113
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)
#define name(a, b)
Definition: linkTestLib0.cpp:5
const char * Bool
void * ExecDefaultConstructor(const TClingClassInfo *info, void *address=0, unsigned long nary=0UL)
typedef void((*Func_t)())
TClingMethodInfo GetMethodWithArgs(const char *fname, const char *arglist, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch, EInheritanceMode imode=kWithInheritance) const
void ExecDestructor(const TClingClassInfo *info, void *address=0, unsigned long nary=0UL, bool withFree=true)
void make_narg_ctor_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
long ExecInt(void *address)
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
TArrow ar(9, 23, 9, 21.6, 0.015,"|>")
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:504
#define I(x, y, z)
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *info)
TObject * obj
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
bool IsValid() const
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)
const char * Short
long long ExecInt64(void *address)