Logo ROOT  
Reference Guide
TClingCallFunc.h
Go to the documentation of this file.
1 // root/core/meta
2 // vim: sw=3
3 // Author: Paul Russo 30/07/2012
4 
5 /*************************************************************************
6  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 
13 #ifndef ROOT_CallFunc
14 #define ROOT_CallFunc
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TClingCallFunc //
19 // //
20 // Emulation of the CINT CallFunc class. //
21 // //
22 // The CINT C++ interpreter provides an interface for calling //
23 // functions through the generated wrappers in dictionaries with //
24 // the CallFunc class. This class provides the same functionality, //
25 // using an interface as close as possible to CallFunc but the //
26 // function metadata and calling service comes from the Cling //
27 // C++ interpreter and the Clang C++ compiler, not CINT. //
28 // //
29 //////////////////////////////////////////////////////////////////////////
30 
31 #include "TClingMethodInfo.h"
32 #include "TClingClassInfo.h"
33 #include "TClingUtils.h"
34 #include "TInterpreter.h"
35 #include <string>
36 
37 #include "cling/Interpreter/Value.h"
38 
39 #include <llvm/ADT/SmallVector.h>
40 
41 namespace clang {
42 class BuiltinType;
43 class CXXMethodDecl;
44 class DeclContext;
45 class Expr;
46 class FunctionDecl;
47 }
48 
49 namespace cling {
50 class Interpreter;
51 }
52 
53 class TClingClassInfo;
54 class TInterpreterValue;
55 
56 typedef void (*tcling_callfunc_Wrapper_t)(void*, int, void**, void*);
57 typedef void (*tcling_callfunc_ctor_Wrapper_t)(void**, void*, unsigned long);
58 typedef void (*tcling_callfunc_dtor_Wrapper_t)(void*, unsigned long, int);
59 
61 
62 private:
63 
64  /// Cling interpreter, we do *not* own.
65  cling::Interpreter* fInterp;
66  /// ROOT normalized context for that interpreter
68  /// Current method, we own.
69  std::unique_ptr<TClingMethodInfo> fMethod;
70  /// Decl for the method
71  const clang::FunctionDecl *fDecl = nullptr;
72  /// Number of required arguments
73  size_t fMinRequiredArguments = -1;
74  /// Pointer to compiled wrapper, we do *not* own.
76  /// Stored function arguments, we own.
77  mutable llvm::SmallVector<cling::Value, 8> fArgVals;
78  /// If true, do not limit number of function arguments to declared number.
79  bool fIgnoreExtraArgs : 1;
81 
82 private:
87  };
88 
89  using ExecWithRetFunc_t = std::function<void(void* address, cling::Value &ret)>;
90 
91  void* compile_wrapper(const std::string& wrapper_name,
92  const std::string& wrapper,
93  bool withAccessControl = true);
94 
95  void collect_type_info(clang::QualType& QT, std::ostringstream& typedefbuf,
96  std::ostringstream& callbuf, std::string& type_name,
97  EReferenceType& refType, bool& isPointer, int indent_level,
98  bool forArgument);
99 
100  void make_narg_call(const std::string &return_type, const unsigned N, std::ostringstream &typedefbuf,
101  std::ostringstream &callbuf, const std::string &class_name, int indent_level);
102 
103  void make_narg_ctor(const unsigned N, std::ostringstream& typedefbuf,
104  std::ostringstream& callbuf,
105  const std::string& class_name, int indent_level);
106 
107  void make_narg_call_with_return(const unsigned N,
108  const std::string& class_name,
109  std::ostringstream& buf, int indent_level);
110 
111  void make_narg_ctor_with_return(const unsigned N,
112  const std::string& class_name,
113  std::ostringstream& buf, int indent_level);
114 
116 
119 
121  make_dtor_wrapper(const TClingClassInfo* info);
122 
123  // Implemented in source file.
124  template <typename T>
125  void execWithLL(void* address, cling::Value* val);
126  template <typename T>
127  void execWithULL(void* address, cling::Value* val);
128  template <class T>
130 
131  ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret);
132  ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret);
133  ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret);
134 
135  void exec(void* address, void* ret);
136 
137  void exec_with_valref_return(void* address,
138  cling::Value* ret);
139  void EvaluateArgList(const std::string& ArgList);
140 
142 
144  if (fMinRequiredArguments == (size_t)-1)
146  return fMinRequiredArguments;
147  }
148 
149  // Implemented in source file.
150  template <typename T>
151  T ExecT(void* address);
152 
153 
154 public:
155 
156  ~TClingCallFunc() = default;
157 
158  explicit TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
159  : fInterp(interp), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(false), fReturnIsRecordType(false)
160  {
161  fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(interp));
162  }
163 
164  explicit TClingCallFunc(const TClingMethodInfo &minfo, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
165  : fInterp(minfo.GetInterpreter()), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(false),
166  fReturnIsRecordType(false)
167 
168  {
169  fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(minfo));
170  }
171 
175  {
176  fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(*rhs.fMethod));
177  }
178 
179  TClingCallFunc &operator=(const TClingCallFunc &rhs) = delete;
180 
181  void* ExecDefaultConstructor(const TClingClassInfo* info,
183  const std::string &type_name,
184  void* address = nullptr, unsigned long nary = 0UL);
185  void ExecDestructor(const TClingClassInfo* info, void* address = nullptr,
186  unsigned long nary = 0UL, bool withFree = true);
187  void ExecWithReturn(void* address, void *ret = nullptr);
188  void ExecWithArgsAndReturn(void* address,
189  const void* args[] = 0,
190  int nargs = 0,
191  void* ret = 0);
192  void Exec(void* address, TInterpreterValue* interpVal = 0);
193  Longptr_t ExecInt(void* address);
194  long long ExecInt64(void* address);
195  double ExecDouble(void* address);
197  void IgnoreExtraArgs(bool ignore) { fIgnoreExtraArgs = ignore; }
198  void Init();
199  void Init(const TClingMethodInfo&);
200  void Init(std::unique_ptr<TClingMethodInfo>);
201  void Invoke(cling::Value* result = 0) const;
202  void* InterfaceMethod();
203  bool IsValid() const;
205  const clang::DeclContext *GetDeclContext() const;
206 
207  int get_wrapper_code(std::string &wrapper_name, std::string &wrapper);
208 
209  const clang::FunctionDecl *GetDecl() {
210  if (!fDecl)
211  fDecl = fMethod->GetTargetFunctionDecl();
212  return fDecl;
213  }
214  const clang::FunctionDecl* GetDecl() const {
215  if (fDecl)
216  return fDecl;
217  return fMethod->GetTargetFunctionDecl();
218  }
219  const clang::Decl *GetFunctionOrShadowDecl() const {
220  return fMethod->GetDecl();
221  }
222  void ResetArg();
223  void SetArg(long arg);
224  void SetArg(unsigned long arg);
225  void SetArg(float arg);
226  void SetArg(double arg);
227  void SetArg(long long arg);
228  void SetArg(unsigned long long arg);
229  void SetArgArray(Longptr_t* argArr, int narg);
230  void SetArgs(const char* args);
231  void SetFunc(const TClingClassInfo* info, const char* method,
232  const char* arglist, Longptr_t* poffset);
233  void SetFunc(const TClingClassInfo* info, const char* method,
234  const char* arglist, bool objectIsConst, Longptr_t* poffset);
235  void SetFunc(const TClingMethodInfo* info);
236  void SetFuncProto(const TClingClassInfo* info, const char* method,
237  const char* proto, Longptr_t* poffset,
239  void SetFuncProto(const TClingClassInfo* info, const char* method,
240  const char* proto, bool objectIsConst, Longptr_t* poffset,
242  void SetFuncProto(const TClingClassInfo* info, const char* method,
243  const llvm::SmallVectorImpl<clang::QualType>& proto,
244  Longptr_t* poffset,
246  void SetFuncProto(const TClingClassInfo* info, const char* method,
247  const llvm::SmallVectorImpl<clang::QualType>& proto,
248  bool objectIsConst, Longptr_t* poffset,
250 };
251 
252 #endif // ROOT_CallFunc
TClingCallFunc::fReturnIsRecordType
bool fReturnIsRecordType
Definition: TClingCallFunc.h:80
TClingCallFunc::InitRetAndExecBuiltin
ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret)
Definition: TClingCallFunc.cxx:1876
TClingCallFunc::execWithULL
void execWithULL(void *address, cling::Value *val)
Definition: TClingCallFunc.cxx:1855
TClingCallFunc::make_narg_call
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)
Definition: TClingCallFunc.cxx:390
TClingCallFunc::InitRetAndExecNoCtor
ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret)
Definition: TClingCallFunc.cxx:1994
TClingCallFunc::make_wrapper
tcling_callfunc_Wrapper_t make_wrapper()
Definition: TClingCallFunc.cxx:1104
TClingCallFunc::execWithLL
void execWithLL(void *address, cling::Value *val)
Definition: TClingCallFunc.cxx:1847
TClingCallFunc::IsValid
bool IsValid() const
Definition: TClingCallFunc.cxx:2279
TClingCallFunc::exec_with_valref_return
void exec_with_valref_return(void *address, cling::Value *ret)
Definition: TClingCallFunc.cxx:2050
TClingCallFunc::ExecWithRetFunc_t
std::function< void(void *address, cling::Value &ret)> ExecWithRetFunc_t
Definition: TClingCallFunc.h:89
TClingCallFunc::compile_wrapper
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
Definition: TClingCallFunc.cxx:265
TClingCallFunc::TClingCallFunc
TClingCallFunc(const TClingCallFunc &rhs)
Definition: TClingCallFunc.h:172
ROOT::kConversionMatch
@ kConversionMatch
Definition: TDictionary.h:162
TClingCallFunc::make_narg_ctor_with_return
void make_narg_ctor_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:506
TClingMethodInfo.h
TClingCallFunc::ExecDestructor
void ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
Definition: TClingCallFunc.cxx:2206
TClingCallFunc::fMethod
std::unique_ptr< TClingMethodInfo > fMethod
Current method, we own.
Definition: TClingCallFunc.h:69
TClingCallFunc::ExecDefaultConstructor
void * ExecDefaultConstructor(const TClingClassInfo *info, ROOT::TMetaUtils::EIOCtorCategory kind, const std::string &type_name, void *address=nullptr, unsigned long nary=0UL)
Definition: TClingCallFunc.cxx:2168
TClingCallFunc::make_dtor_wrapper
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
Definition: TClingCallFunc.cxx:1340
TClingCallFunc::SetArg
void SetArg(long arg)
Definition: TClingCallFunc.cxx:2323
TClingCallFunc::GetDecl
const clang::FunctionDecl * GetDecl()
Definition: TClingCallFunc.h:209
TClingCallFunc::ExecDouble
double ExecDouble(void *address)
Definition: TClingCallFunc.cxx:2140
TClingCallFunc::kLValueReference
@ kLValueReference
Definition: TClingCallFunc.h:85
N
#define N
long
long
Definition: Converters.cxx:876
tcling_callfunc_Wrapper_t
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
Definition: TClingCallFunc.h:56
TClingCallFunc::TClingCallFunc
TClingCallFunc(const TClingMethodInfo &minfo, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
Definition: TClingCallFunc.h:164
TClingCallFunc::GetMinRequiredArguments
size_t GetMinRequiredArguments()
Definition: TClingCallFunc.h:143
TClingCallFunc::make_ctor_wrapper
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *, ROOT::TMetaUtils::EIOCtorCategory, const std::string &)
Definition: TClingCallFunc.cxx:1129
TClingClassInfo.h
TClingCallFunc::fWrapper
tcling_callfunc_Wrapper_t fWrapper
Pointer to compiled wrapper, we do not own.
Definition: TClingCallFunc.h:75
TInterpreter::CallFuncIFacePtr_t
Definition: TInterpreter.h:86
TClingCallFunc::FactoryMethod
TClingMethodInfo * FactoryMethod() const
Definition: TClingCallFunc.cxx:2234
TClingMethodInfo
Emulation of the CINT MethodInfo class.
Definition: TClingMethodInfo.h:124
TClingCallFunc::EReferenceType
EReferenceType
Definition: TClingCallFunc.h:83
TClingCallFunc::SetArgs
void SetArgs(const char *args)
Definition: TClingCallFunc.cxx:2366
TClingCallFunc::GetFunctionOrShadowDecl
const clang::Decl * GetFunctionOrShadowDecl() const
Definition: TClingCallFunc.h:219
xmlio::Value
const char * Value
Definition: TXMLSetup.cxx:73
TClingCallFunc::kNotReference
@ kNotReference
Definition: TClingCallFunc.h:84
tcling_callfunc_ctor_Wrapper_t
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
Definition: TClingCallFunc.h:57
TClingCallFunc::get_wrapper_code
int get_wrapper_code(std::string &wrapper_name, std::string &wrapper)
Definition: TClingCallFunc.cxx:592
ROOT::R::function
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
TClingClassInfo
Emulation of the CINT ClassInfo class.
Definition: TClingClassInfo.h:59
TClingCallFunc::kRValueReference
@ kRValueReference
Definition: TClingCallFunc.h:86
TClingCallFunc::EvaluateArgList
void EvaluateArgList(const std::string &ArgList)
Definition: TClingCallFunc.cxx:2069
tcling_callfunc_dtor_Wrapper_t
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
Definition: TClingCallFunc.h:58
TClingCallFunc::fArgVals
llvm::SmallVector< cling::Value, 8 > fArgVals
Stored function arguments, we own.
Definition: TClingCallFunc.h:77
TClingCallFunc::make_narg_call_with_return
void make_narg_call_with_return(const unsigned N, const std::string &class_name, std::ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:976
TClingCallFunc::Invoke
void Invoke(cling::Value *result=0) const
TClingCallFunc::ExecWithReturn
void ExecWithReturn(void *address, void *ret=nullptr)
Definition: TClingCallFunc.cxx:2157
TClingCallFunc::InterfaceMethod
void * InterfaceMethod()
Definition: TClingCallFunc.cxx:2260
TClingCallFunc::Exec
void Exec(void *address, TInterpreterValue *interpVal=0)
Definition: TClingCallFunc.cxx:2093
void
typedef void((*Func_t)())
TClingCallFunc::ExecT
T ExecT(void *address)
Definition: TClingCallFunc.cxx:2110
ROOT::TMetaUtils::TNormalizedCtxt
Definition: TClingUtils.h:138
TClingCallFunc::make_narg_ctor
void make_narg_ctor(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
Definition: TClingCallFunc.cxx:345
ROOT::EFunctionMatchMode
EFunctionMatchMode
Definition: TDictionary.h:160
ROOT::TMetaUtils::EIOCtorCategory
EIOCtorCategory
Definition: TClingUtils.h:359
TClingCallFunc::ExecInt
Longptr_t ExecInt(void *address)
Definition: TClingCallFunc.cxx:2130
Longptr_t
long Longptr_t
Definition: RtypesCore.h:82
TClingCallFunc::CalculateMinRequiredArguments
size_t CalculateMinRequiredArguments()
Definition: TClingCallFunc.cxx:259
TClingCallFunc::Init
void Init()
Definition: TClingCallFunc.cxx:2239
proto
const char * proto
Definition: civetweb.c:16604
TClingCallFunc::SetFuncProto
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, Longptr_t *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Definition: TClingCallFunc.cxx:2415
TClingCallFunc::fMinRequiredArguments
size_t fMinRequiredArguments
Number of required arguments.
Definition: TClingCallFunc.h:73
TInterpreterValue
Definition: TInterpreterValue.h:32
TClingCallFunc
Emulation of the CINT CallFunc class.
Definition: TClingCallFunc.h:60
TClingCallFunc::GetDecl
const clang::FunctionDecl * GetDecl() const
Definition: TClingCallFunc.h:214
TClingCallFunc::fDecl
const clang::FunctionDecl * fDecl
Decl for the method.
Definition: TClingCallFunc.h:71
TInterpreter.h
clang
Definition: BaseSelectionRule.h:29
TClingCallFunc::SetArgArray
void SetArgArray(Longptr_t *argArr, int narg)
Definition: TClingCallFunc.cxx:2358
TClingCallFunc::fIgnoreExtraArgs
bool fIgnoreExtraArgs
If true, do not limit number of function arguments to declared number.
Definition: TClingCallFunc.h:79
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
TClingCallFunc::operator=
TClingCallFunc & operator=(const TClingCallFunc &rhs)=delete
TClingUtils.h
TClingCallFunc::GetDeclContext
const clang::DeclContext * GetDeclContext() const
Definition: TClingCallFunc.cxx:588
TClingCallFunc::InitRetAndExec
ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret)
Definition: TClingCallFunc.cxx:2035
TClingCallFunc::exec
void exec(void *address, void *ret)
Definition: TClingCallFunc.cxx:1532
TClingCallFunc::SetFunc
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, Longptr_t *poffset)
Definition: TClingCallFunc.cxx:2372
TClingCallFunc::ExecWithArgsAndReturn
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
Definition: TClingCallFunc.cxx:2145
TClingCallFunc::TClingCallFunc
TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
Definition: TClingCallFunc.h:158
TClingCallFunc::fInterp
cling::Interpreter * fInterp
Cling interpreter, we do not own.
Definition: TClingCallFunc.h:65
TClingCallFunc::fNormCtxt
const ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
ROOT normalized context for that interpreter.
Definition: TClingCallFunc.h:67
TClingCallFunc::collect_type_info
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)
Definition: TClingCallFunc.cxx:272
TClingCallFunc::ExecInt64
long long ExecInt64(void *address)
Definition: TClingCallFunc.cxx:2135
TClingCallFunc::InitRetAndExecIntegral
ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret)
TClingCallFunc::IgnoreExtraArgs
void IgnoreExtraArgs(bool ignore)
Definition: TClingCallFunc.h:197
int
TClingCallFunc::IFacePtr
TInterpreter::CallFuncIFacePtr_t IFacePtr()
Definition: TClingCallFunc.cxx:2287
TClingCallFunc::ResetArg
void ResetArg()
Definition: TClingCallFunc.cxx:2311
TClingCallFunc::~TClingCallFunc
~TClingCallFunc()=default