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