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
40namespace clang {
41class BuiltinType;
42class CXXMethodDecl;
43class DeclContext;
44class Expr;
45class FunctionDecl;
46}
47
48namespace cling {
49class Interpreter;
50}
51
52class TClingClassInfo;
54
55typedef void (*tcling_callfunc_Wrapper_t)(void*, int, void**, void*);
56typedef void (*tcling_callfunc_ctor_Wrapper_t)(void**, void*, unsigned long);
57typedef void (*tcling_callfunc_dtor_Wrapper_t)(void*, unsigned long, int);
58
60
61private:
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
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.
80
81private:
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
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)
146 }
147
148 // Implemented in source file.
149 template <typename T>
150 T ExecT(void* address);
151
152
153public:
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
164 : fInterp(minfo.GetInterpreter()), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(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
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
long
Definition: Converters.cxx:858
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)
#define N
typedef void((*Func_t)())
const char * proto
Definition: civetweb.c:16604
Emulation of the CINT CallFunc class.
void * ExecDefaultConstructor(const TClingClassInfo *info, ROOT::TMetaUtils::EIOCtorCategory kind, const std::string &type_name, void *address=nullptr, unsigned long nary=0UL)
void ExecWithReturn(void *address, void *ret=nullptr)
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)
const clang::FunctionDecl * GetDecl() const
void SetArgs(const char *args)
size_t fMinRequiredArguments
Number of required arguments.
T ExecT(void *address)
TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
size_t CalculateMinRequiredArguments()
double ExecDouble(void *address)
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, long *poffset)
bool fIgnoreExtraArgs
If true, do not limit number of function arguments to declared number.
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)
~TClingCallFunc()=default
std::function< void(void *address, cling::Value &ret)> ExecWithRetFunc_t
void ExecDestructor(const TClingClassInfo *info, void *address=nullptr, unsigned long nary=0UL, bool withFree=true)
void execWithLL(void *address, cling::Value *val)
const clang::DeclContext * GetDeclContext() const
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 Invoke(cling::Value *result=0) const
void make_narg_ctor(const unsigned N, std::ostringstream &typedefbuf, std::ostringstream &callbuf, const std::string &class_name, int indent_level)
TClingCallFunc(const TClingMethodInfo &minfo, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
const clang::FunctionDecl * GetDecl()
void execWithULL(void *address, cling::Value *val)
void * InterfaceMethod()
void EvaluateArgList(const std::string &ArgList)
const clang::Decl * GetFunctionOrShadowDecl() const
void ExecWithArgsAndReturn(void *address, const void *args[]=0, int nargs=0, void *ret=0)
void SetArgArray(long *argArr, int narg)
TClingCallFunc(const TClingCallFunc &rhs)
void Exec(void *address, TInterpreterValue *interpVal=0)
TClingMethodInfo * FactoryMethod() const
ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret)
int get_wrapper_code(std::string &wrapper_name, std::string &wrapper)
size_t GetMinRequiredArguments()
tcling_callfunc_ctor_Wrapper_t make_ctor_wrapper(const TClingClassInfo *, ROOT::TMetaUtils::EIOCtorCategory, const std::string &)
const ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
ROOT normalized context for that interpreter.
void IgnoreExtraArgs(bool ignore)
tcling_callfunc_Wrapper_t fWrapper
Pointer to compiled wrapper, we do not own.
TClingCallFunc & operator=(const TClingCallFunc &rhs)=delete
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.
Emulation of the CINT MethodInfo class.
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
EFunctionMatchMode
Definition: TDictionary.h:159
@ kConversionMatch
Definition: TDictionary.h:161
const char * Value
Definition: TXMLSetup.cxx:72