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 
36 #include "cling/Interpreter/Value.h"
37 
38 #include <llvm/ADT/SmallVector.h>
39 
40 namespace clang {
41 class BuiltinType;
42 class CXXMethodDecl;
43 class DeclContext;
44 class Expr;
45 class FunctionDecl;
46 }
47 
48 namespace cling {
49 class Interpreter;
50 }
51 
52 class TClingClassInfo;
53 class TInterpreterValue;
54 
55 typedef void (*tcling_callfunc_Wrapper_t)(void*, int, void**, void*);
56 typedef void (*tcling_callfunc_ctor_Wrapper_t)(void**, void*, unsigned long);
57 typedef void (*tcling_callfunc_dtor_Wrapper_t)(void*, unsigned long, int);
58 
60 
61 private:
62 
63  /// Cling interpreter, we do *not* own.
64  cling::Interpreter* fInterp;
65  /// ROOT normalized context for that interpreter
67  /// Current method, we own.
68  std::unique_ptr<TClingMethodInfo> fMethod;
69  /// Decl for the method
70  const clang::FunctionDecl *fDecl = nullptr;
71  /// Number of required arguments
72  size_t fMinRequiredArguments = -1;
73  /// Pointer to compiled wrapper, we do *not* own.
75  /// Stored function arguments, we own.
76  mutable llvm::SmallVector<cling::Value, 8> fArgVals;
77  /// If true, do not limit number of function arguments to declared number.
78  bool fIgnoreExtraArgs : 1;
80 
81 private:
86  };
87 
88  using ExecWithRetFunc_t = std::function<void(void* address, cling::Value &ret)>;
89 
90  void* compile_wrapper(const std::string& wrapper_name,
91  const std::string& wrapper,
92  bool withAccessControl = true);
93 
94  void collect_type_info(clang::QualType& QT, std::ostringstream& typedefbuf,
95  std::ostringstream& callbuf, std::string& type_name,
96  EReferenceType& refType, bool& isPointer, int indent_level,
97  bool forArgument);
98 
99  void make_narg_call(const std::string &return_type, const unsigned N, std::ostringstream &typedefbuf,
100  std::ostringstream &callbuf, const std::string &class_name, int indent_level);
101 
102  void make_narg_ctor(const unsigned N, std::ostringstream& typedefbuf,
103  std::ostringstream& callbuf,
104  const std::string& class_name, int indent_level);
105 
106  void make_narg_call_with_return(const unsigned N,
107  const std::string& class_name,
108  std::ostringstream& buf, int indent_level);
109 
110  void make_narg_ctor_with_return(const unsigned N,
111  const std::string& class_name,
112  std::ostringstream& buf, int indent_level);
113 
115 
118 
120  make_dtor_wrapper(const TClingClassInfo* info);
121 
122  // Implemented in source file.
123  template <typename T>
124  void execWithLL(void* address, cling::Value* val);
125  template <typename T>
126  void execWithULL(void* address, cling::Value* val);
127  template <class T>
129 
130  ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret);
131  ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret);
132  ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret);
133 
134  void exec(void* address, void* ret);
135 
136  void exec_with_valref_return(void* address,
137  cling::Value* ret);
138  void EvaluateArgList(const std::string& ArgList);
139 
141 
143  if (fMinRequiredArguments == (size_t)-1)
145  return fMinRequiredArguments;
146  }
147 
148  // Implemented in source file.
149  template <typename T>
150  T ExecT(void* address);
151 
152 
153 public:
154 
155  ~TClingCallFunc() = default;
156 
157  explicit TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
158  : fInterp(interp), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(false), fReturnIsRecordType(false)
159  {
160  fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(interp));
161  }
162 
163  explicit TClingCallFunc(const TClingMethodInfo &minfo, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
164  : fInterp(minfo.GetInterpreter()), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(false),
165  fReturnIsRecordType(false)
166 
167  {
168  fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(minfo));
169  }
170 
174  {
175  fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(*rhs.fMethod));
176  }
177 
178  TClingCallFunc &operator=(const TClingCallFunc &rhs) = delete;
179 
180  void* ExecDefaultConstructor(const TClingClassInfo* info,
182  const std::string &type_name,
183  void* address = nullptr, unsigned long nary = 0UL);
184  void ExecDestructor(const TClingClassInfo* info, void* address = nullptr,
185  unsigned long nary = 0UL, bool withFree = true);
186  void ExecWithReturn(void* address, void *ret = nullptr);
187  void ExecWithArgsAndReturn(void* address,
188  const void* args[] = 0,
189  int nargs = 0,
190  void* ret = 0);
191  void Exec(void* address, TInterpreterValue* interpVal = 0);
192  long ExecInt(void* address);
193  long long ExecInt64(void* address);
194  double ExecDouble(void* address);
196  void IgnoreExtraArgs(bool ignore) { fIgnoreExtraArgs = ignore; }
197  void Init();
198  void Init(const TClingMethodInfo&);
199  void Init(std::unique_ptr<TClingMethodInfo>);
200  void Invoke(cling::Value* result = 0) const;
201  void* InterfaceMethod();
202  bool IsValid() const;
204  const clang::DeclContext *GetDeclContext() const;
205 
206  int get_wrapper_code(std::string &wrapper_name, std::string &wrapper);
207 
208  const clang::FunctionDecl *GetDecl() {
209  if (!fDecl)
210  fDecl = fMethod->GetTargetFunctionDecl();
211  return fDecl;
212  }
213  const clang::FunctionDecl* GetDecl() const {
214  if (fDecl)
215  return fDecl;
216  return fMethod->GetTargetFunctionDecl();
217  }
218  const clang::Decl *GetFunctionOrShadowDecl() const {
219  return fMethod->GetDecl();
220  }
221  void ResetArg();
222  void SetArg(long arg);
223  void SetArg(unsigned long arg);
224  void SetArg(float arg);
225  void SetArg(double arg);
226  void SetArg(long long arg);
227  void SetArg(unsigned long long arg);
228  void SetArgArray(long* argArr, int narg);
229  void SetArgs(const char* args);
230  void SetFunc(const TClingClassInfo* info, const char* method,
231  const char* arglist, long* poffset);
232  void SetFunc(const TClingClassInfo* info, const char* method,
233  const char* arglist, bool objectIsConst, long* poffset);
234  void SetFunc(const TClingMethodInfo* info);
235  void SetFuncProto(const TClingClassInfo* info, const char* method,
236  const char* proto, long* poffset,
238  void SetFuncProto(const TClingClassInfo* info, const char* method,
239  const char* proto, bool objectIsConst, long* poffset,
241  void SetFuncProto(const TClingClassInfo* info, const char* method,
242  const llvm::SmallVectorImpl<clang::QualType>& proto,
243  long* poffset,
245  void SetFuncProto(const TClingClassInfo* info, const char* method,
246  const llvm::SmallVectorImpl<clang::QualType>& proto,
247  bool objectIsConst, long* poffset,
249 };
250 
251 #endif // ROOT_CallFunc
TClingCallFunc::fReturnIsRecordType
bool fReturnIsRecordType
Definition: TClingCallFunc.h:79
TClingCallFunc::InitRetAndExecBuiltin
ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret)
Definition: TClingCallFunc.cxx:1877
TClingCallFunc::execWithULL
void execWithULL(void *address, cling::Value *val)
Definition: TClingCallFunc.cxx:1856
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:391
TClingCallFunc::InitRetAndExecNoCtor
ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret)
Definition: TClingCallFunc.cxx:1995
TClingCallFunc::make_wrapper
tcling_callfunc_Wrapper_t make_wrapper()
Definition: TClingCallFunc.cxx:1105
TClingCallFunc::execWithLL
void execWithLL(void *address, cling::Value *val)
Definition: TClingCallFunc.cxx:1848
TClingCallFunc::IsValid
bool IsValid() const
Definition: TClingCallFunc.cxx:2280
TClingCallFunc::exec_with_valref_return
void exec_with_valref_return(void *address, cling::Value *ret)
Definition: TClingCallFunc.cxx:2051
TClingCallFunc::ExecWithRetFunc_t
std::function< void(void *address, cling::Value &ret)> ExecWithRetFunc_t
Definition: TClingCallFunc.h:88
TClingCallFunc::compile_wrapper
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
Definition: TClingCallFunc.cxx:266
TClingCallFunc::TClingCallFunc
TClingCallFunc(const TClingCallFunc &rhs)
Definition: TClingCallFunc.h:171
ROOT::kConversionMatch
@ kConversionMatch
Definition: TDictionary.h:161
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:507
TClingMethodInfo.h
TClingCallFunc::ExecDestructor
void ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
Definition: TClingCallFunc.cxx:2207
TClingCallFunc::fMethod
std::unique_ptr< TClingMethodInfo > fMethod
Current method, we own.
Definition: TClingCallFunc.h:68
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:2169
TClingCallFunc::make_dtor_wrapper
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
Definition: TClingCallFunc.cxx:1341
TClingCallFunc::SetArg
void SetArg(long arg)
Definition: TClingCallFunc.cxx:2324
TClingCallFunc::GetDecl
const clang::FunctionDecl * GetDecl()
Definition: TClingCallFunc.h:208
TClingCallFunc::ExecDouble
double ExecDouble(void *address)
Definition: TClingCallFunc.cxx:2141
TClingCallFunc::kLValueReference
@ kLValueReference
Definition: TClingCallFunc.h:84
N
#define N
TClingCallFunc::SetFunc
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, long *poffset)
Definition: TClingCallFunc.cxx:2373
long
long
Definition: Converters.cxx:858
tcling_callfunc_Wrapper_t
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
Definition: TClingCallFunc.h:55
TClingCallFunc::TClingCallFunc
TClingCallFunc(const TClingMethodInfo &minfo, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
Definition: TClingCallFunc.h:163
TClingCallFunc::GetMinRequiredArguments
size_t GetMinRequiredArguments()
Definition: TClingCallFunc.h:142
TClingCallFunc::make_ctor_wrapper
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *, ROOT::TMetaUtils::EIOCtorCategory, const std::string &)
Definition: TClingCallFunc.cxx:1130
TClingClassInfo.h
TClingCallFunc::fWrapper
tcling_callfunc_Wrapper_t fWrapper
Pointer to compiled wrapper, we do not own.
Definition: TClingCallFunc.h:74
TInterpreter::CallFuncIFacePtr_t
Definition: TInterpreter.h:83
TClingCallFunc::FactoryMethod
TClingMethodInfo * FactoryMethod() const
Definition: TClingCallFunc.cxx:2235
TClingMethodInfo
Emulation of the CINT MethodInfo class.
Definition: TClingMethodInfo.h:124
TClingCallFunc::EReferenceType
EReferenceType
Definition: TClingCallFunc.h:82
TClingCallFunc::SetArgs
void SetArgs(const char *args)
Definition: TClingCallFunc.cxx:2367
TClingCallFunc::GetFunctionOrShadowDecl
const clang::Decl * GetFunctionOrShadowDecl() const
Definition: TClingCallFunc.h:218
xmlio::Value
const char * Value
Definition: TXMLSetup.cxx:72
TClingCallFunc::kNotReference
@ kNotReference
Definition: TClingCallFunc.h:83
tcling_callfunc_ctor_Wrapper_t
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
Definition: TClingCallFunc.h:56
TClingCallFunc::get_wrapper_code
int get_wrapper_code(std::string &wrapper_name, std::string &wrapper)
Definition: TClingCallFunc.cxx:593
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:85
TClingCallFunc::EvaluateArgList
void EvaluateArgList(const std::string &ArgList)
Definition: TClingCallFunc.cxx:2070
tcling_callfunc_dtor_Wrapper_t
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
Definition: TClingCallFunc.h:57
TClingCallFunc::fArgVals
llvm::SmallVector< cling::Value, 8 > fArgVals
Stored function arguments, we own.
Definition: TClingCallFunc.h:76
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:977
TClingCallFunc::Invoke
void Invoke(cling::Value *result=0) const
TClingCallFunc::ExecWithReturn
void ExecWithReturn(void *address, void *ret=nullptr)
Definition: TClingCallFunc.cxx:2158
TClingCallFunc::InterfaceMethod
void * InterfaceMethod()
Definition: TClingCallFunc.cxx:2261
TClingCallFunc::Exec
void Exec(void *address, TInterpreterValue *interpVal=0)
Definition: TClingCallFunc.cxx:2094
void
typedef void((*Func_t)())
TClingCallFunc::ExecT
T ExecT(void *address)
Definition: TClingCallFunc.cxx:2111
ROOT::TMetaUtils::TNormalizedCtxt
Definition: TClingUtils.h:135
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:346
ROOT::EFunctionMatchMode
EFunctionMatchMode
Definition: TDictionary.h:159
ROOT::TMetaUtils::EIOCtorCategory
EIOCtorCategory
Definition: TClingUtils.h:356
TClingCallFunc::CalculateMinRequiredArguments
size_t CalculateMinRequiredArguments()
Definition: TClingCallFunc.cxx:260
TClingCallFunc::Init
void Init()
Definition: TClingCallFunc.cxx:2240
proto
const char * proto
Definition: civetweb.c:16604
TClingCallFunc::fMinRequiredArguments
size_t fMinRequiredArguments
Number of required arguments.
Definition: TClingCallFunc.h:72
TInterpreterValue
Definition: TInterpreterValue.h:31
TClingCallFunc::SetFuncProto
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Definition: TClingCallFunc.cxx:2416
TClingCallFunc
Emulation of the CINT CallFunc class.
Definition: TClingCallFunc.h:59
TClingCallFunc::SetArgArray
void SetArgArray(long *argArr, int narg)
Definition: TClingCallFunc.cxx:2359
TClingCallFunc::GetDecl
const clang::FunctionDecl * GetDecl() const
Definition: TClingCallFunc.h:213
TClingCallFunc::fDecl
const clang::FunctionDecl * fDecl
Decl for the method.
Definition: TClingCallFunc.h:70
TInterpreter.h
clang
Definition: BaseSelectionRule.h:29
TClingCallFunc::fIgnoreExtraArgs
bool fIgnoreExtraArgs
If true, do not limit number of function arguments to declared number.
Definition: TClingCallFunc.h:78
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:589
TClingCallFunc::InitRetAndExec
ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret)
Definition: TClingCallFunc.cxx:2036
TClingCallFunc::exec
void exec(void *address, void *ret)
Definition: TClingCallFunc.cxx:1533
TClingCallFunc::ExecWithArgsAndReturn
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
Definition: TClingCallFunc.cxx:2146
TClingCallFunc::TClingCallFunc
TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
Definition: TClingCallFunc.h:157
TClingCallFunc::fInterp
cling::Interpreter * fInterp
Cling interpreter, we do not own.
Definition: TClingCallFunc.h:64
TClingCallFunc::fNormCtxt
const ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
ROOT normalized context for that interpreter.
Definition: TClingCallFunc.h:66
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:273
TClingCallFunc::ExecInt64
long long ExecInt64(void *address)
Definition: TClingCallFunc.cxx:2136
TClingCallFunc::InitRetAndExecIntegral
ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret)
TClingCallFunc::ExecInt
long ExecInt(void *address)
Definition: TClingCallFunc.cxx:2131
TClingCallFunc::IgnoreExtraArgs
void IgnoreExtraArgs(bool ignore)
Definition: TClingCallFunc.h:196
int
TClingCallFunc::IFacePtr
TInterpreter::CallFuncIFacePtr_t IFacePtr()
Definition: TClingCallFunc.cxx:2288
TClingCallFunc::ResetArg
void ResetArg()
Definition: TClingCallFunc.cxx:2312
TClingCallFunc::~TClingCallFunc
~TClingCallFunc()=default