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 Expr;
44 class FunctionDecl;
45 class CXXMethodDecl;
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>
128  ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret);
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::FunctionDecl *GetDecl() {
205  if (!fDecl)
206  fDecl = fMethod->GetMethodDecl();
207  return fDecl;
208  }
209 
210  int get_wrapper_code(std::string &wrapper_name, std::string &wrapper);
211 
212  const clang::FunctionDecl* GetDecl() const {
213  if (fDecl)
214  return fDecl;
215  return fMethod->GetMethodDecl();
216  }
217  void ResetArg();
218  void SetArg(long arg);
219  void SetArg(unsigned long arg);
220  void SetArg(float arg);
221  void SetArg(double arg);
222  void SetArg(long long arg);
223  void SetArg(unsigned long long arg);
224  void SetArgArray(long* argArr, int narg);
225  void SetArgs(const char* args);
226  void SetFunc(const TClingClassInfo* info, const char* method,
227  const char* arglist, long* poffset);
228  void SetFunc(const TClingClassInfo* info, const char* method,
229  const char* arglist, bool objectIsConst, long* poffset);
230  void SetFunc(const TClingMethodInfo* info);
231  void SetFuncProto(const TClingClassInfo* info, const char* method,
232  const char* proto, long* poffset,
234  void SetFuncProto(const TClingClassInfo* info, const char* method,
235  const char* proto, bool objectIsConst, long* poffset,
237  void SetFuncProto(const TClingClassInfo* info, const char* method,
238  const llvm::SmallVectorImpl<clang::QualType>& proto,
239  long* poffset,
241  void SetFuncProto(const TClingClassInfo* info, const char* method,
242  const llvm::SmallVectorImpl<clang::QualType>& proto,
243  bool objectIsConst, long* poffset,
245 };
246 
247 #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:1852
TClingCallFunc::execWithULL
void execWithULL(void *address, cling::Value *val)
Definition: TClingCallFunc.cxx:1831
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:389
TClingCallFunc::InitRetAndExecNoCtor
ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret)
Definition: TClingCallFunc.cxx:1970
TClingCallFunc::make_wrapper
tcling_callfunc_Wrapper_t make_wrapper()
Definition: TClingCallFunc.cxx:1080
TClingCallFunc::execWithLL
void execWithLL(void *address, cling::Value *val)
Definition: TClingCallFunc.cxx:1823
TClingCallFunc::IsValid
bool IsValid() const
Definition: TClingCallFunc.cxx:2255
TClingCallFunc::exec_with_valref_return
void exec_with_valref_return(void *address, cling::Value *ret)
Definition: TClingCallFunc.cxx:2026
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:264
TClingCallFunc::TClingCallFunc
TClingCallFunc(const TClingCallFunc &rhs)
Definition: TClingCallFunc.h:171
ROOT::kConversionMatch
@ kConversionMatch
Definition: TDictionary.h:157
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:505
TClingMethodInfo.h
TClingCallFunc::ExecDestructor
void ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
Definition: TClingCallFunc.cxx:2182
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:2144
TClingCallFunc::make_dtor_wrapper
tcling_callfunc_dtor_Wrapper_t make_dtor_wrapper(const TClingClassInfo *info)
Definition: TClingCallFunc.cxx:1316
TClingCallFunc::SetArg
void SetArg(long arg)
Definition: TClingCallFunc.cxx:2299
TClingCallFunc::GetDecl
const clang::FunctionDecl * GetDecl()
Definition: TClingCallFunc.h:204
TClingCallFunc::ExecDouble
double ExecDouble(void *address)
Definition: TClingCallFunc.cxx:2116
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:2348
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:1105
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:85
TClingCallFunc::FactoryMethod
TClingMethodInfo * FactoryMethod() const
Definition: TClingCallFunc.cxx:2210
TClingMethodInfo
Definition: TClingMethodInfo.h:56
TClingCallFunc::EReferenceType
EReferenceType
Definition: TClingCallFunc.h:82
TClingCallFunc::SetArgs
void SetArgs(const char *args)
Definition: TClingCallFunc.cxx:2342
xmlio::Value
const char * Value
Definition: TXMLSetup.cxx:79
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:580
ROOT::R::function
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
TClingClassInfo
Definition: TClingClassInfo.h:58
TClingCallFunc::kRValueReference
@ kRValueReference
Definition: TClingCallFunc.h:85
TClingCallFunc::EvaluateArgList
void EvaluateArgList(const std::string &ArgList)
Definition: TClingCallFunc.cxx:2045
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:963
TClingCallFunc::Invoke
void Invoke(cling::Value *result=0) const
TClingCallFunc::ExecWithReturn
void ExecWithReturn(void *address, void *ret=nullptr)
Definition: TClingCallFunc.cxx:2133
TClingCallFunc::InterfaceMethod
void * InterfaceMethod()
Definition: TClingCallFunc.cxx:2236
TClingCallFunc::Exec
void Exec(void *address, TInterpreterValue *interpVal=0)
Definition: TClingCallFunc.cxx:2069
void
typedef void((*Func_t)())
TClingCallFunc::ExecT
T ExecT(void *address)
Definition: TClingCallFunc.cxx:2086
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:344
ROOT::EFunctionMatchMode
EFunctionMatchMode
Definition: TDictionary.h:155
ROOT::TMetaUtils::EIOCtorCategory
EIOCtorCategory
Definition: TClingUtils.h:359
TClingCallFunc::CalculateMinRequiredArguments
size_t CalculateMinRequiredArguments()
Definition: TClingCallFunc.cxx:258
TClingCallFunc::Init
void Init()
Definition: TClingCallFunc.cxx:2215
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:32
TClingCallFunc::SetFuncProto
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, long *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Definition: TClingCallFunc.cxx:2391
TClingCallFunc
Definition: TClingCallFunc.h:59
TClingCallFunc::SetArgArray
void SetArgArray(long *argArr, int narg)
Definition: TClingCallFunc.cxx:2334
TClingCallFunc::GetDecl
const clang::FunctionDecl * GetDecl() const
Definition: TClingCallFunc.h:212
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:52
TClingCallFunc::operator=
TClingCallFunc & operator=(const TClingCallFunc &rhs)=delete
TClingUtils.h
TClingCallFunc::InitRetAndExec
ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret)
Definition: TClingCallFunc.cxx:2011
TClingCallFunc::exec
void exec(void *address, void *ret)
Definition: TClingCallFunc.cxx:1508
TClingCallFunc::ExecWithArgsAndReturn
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
Definition: TClingCallFunc.cxx:2121
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:271
TClingCallFunc::ExecInt64
long long ExecInt64(void *address)
Definition: TClingCallFunc.cxx:2111
TClingCallFunc::InitRetAndExecIntegral
ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret)
TClingCallFunc::ExecInt
long ExecInt(void *address)
Definition: TClingCallFunc.cxx:2106
TClingCallFunc::IgnoreExtraArgs
void IgnoreExtraArgs(bool ignore)
Definition: TClingCallFunc.h:196
int
TClingCallFunc::IFacePtr
TInterpreter::CallFuncIFacePtr_t IFacePtr()
Definition: TClingCallFunc.cxx:2263
TClingCallFunc::ResetArg
void ResetArg()
Definition: TClingCallFunc.cxx:2287
TClingCallFunc::~TClingCallFunc
~TClingCallFunc()=default