Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
41namespace clang {
42class BuiltinType;
43class CXXMethodDecl;
44class DeclContext;
45class Expr;
46class FunctionDecl;
47}
48
49namespace cling {
50class Interpreter;
51}
52
53class TClingClassInfo;
55
56typedef void (*tcling_callfunc_Wrapper_t)(void*, int, void**, void*);
57typedef void (*tcling_callfunc_ctor_Wrapper_t)(void**, void*, unsigned long);
58typedef void (*tcling_callfunc_dtor_Wrapper_t)(void*, unsigned long, int);
59
61
62private:
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
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.
81
82private:
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
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>
129 ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret);
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)
147 }
148
149 // Implemented in source file.
150 template <typename T>
151 T ExecT(void* address);
152
153
154public:
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
165 : fInterp(minfo.GetInterpreter()), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(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
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 long 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(long* argArr, int narg);
230 void SetArgs(const char* args);
231 void SetFunc(const TClingClassInfo* info, const char* method,
232 const char* arglist, long* poffset);
233 void SetFunc(const TClingClassInfo* info, const char* method,
234 const char* arglist, bool objectIsConst, long* poffset);
235 void SetFunc(const TClingMethodInfo* info);
236 void SetFuncProto(const TClingClassInfo* info, const char* method,
237 const char* proto, long* poffset,
239 void SetFuncProto(const TClingClassInfo* info, const char* method,
240 const char* proto, bool objectIsConst, long* poffset,
242 void SetFuncProto(const TClingClassInfo* info, const char* method,
243 const llvm::SmallVectorImpl<clang::QualType>& proto,
244 long* poffset,
246 void SetFuncProto(const TClingClassInfo* info, const char* method,
247 const llvm::SmallVectorImpl<clang::QualType>& proto,
248 bool objectIsConst, long* poffset,
250};
251
252#endif // ROOT_CallFunc
long
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)
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 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.
EFunctionMatchMode
@ kConversionMatch