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 "TClingUtils.h"
32#include "TClingMethodInfo.h"
33#include "TInterpreter.h"
34
35#include "cling/Interpreter/Value.h"
36
37#include <llvm/ADT/SmallVector.h>
38
39namespace clang {
40class BuiltinType;
41class CXXMethodDecl;
42class DeclContext;
43class Expr;
44class FunctionDecl;
45}
46
47namespace cling {
48class Interpreter;
49}
50
51class 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 /// Current method, we own.
66 std::unique_ptr<TClingMethodInfo> fMethod;
67 /// Decl for the method
68 const clang::FunctionDecl *fDecl = nullptr;
69 /// Number of required arguments
71 /// Pointer to compiled wrapper, we do *not* own.
72 std::atomic<tcling_callfunc_Wrapper_t> fWrapper;
73 /// Stored function arguments, we own.
74 mutable llvm::SmallVector<cling::Value, 8> fArgVals;
75
76private:
81 };
82
83 using ExecWithRetFunc_t = std::function<void(void* address, cling::Value &ret)>;
84
85 void* compile_wrapper(const std::string& wrapper_name,
86 const std::string& wrapper,
87 bool withAccessControl = true);
88
89 void collect_type_info(clang::QualType& QT, std::ostringstream& typedefbuf,
90 std::ostringstream& callbuf, std::string& type_name,
91 EReferenceType& refType, bool& isPointer, int indent_level,
92 bool forArgument);
93
94 void make_narg_call(const std::string &return_type, const unsigned N, std::ostringstream &typedefbuf,
95 std::ostringstream &callbuf, const std::string &class_name, int indent_level);
96
97 void make_narg_ctor(const unsigned N, std::ostringstream& typedefbuf,
98 std::ostringstream& callbuf,
99 const std::string& class_name, int indent_level);
100
101 void make_narg_call_with_return(const unsigned N,
102 const std::string& class_name,
103 std::ostringstream& buf, int indent_level);
104
105 void make_narg_ctor_with_return(const unsigned N,
106 const std::string& class_name,
107 std::ostringstream& buf, int indent_level);
108
110
113
116
117 // Implemented in source file.
118 template <typename T>
119 void execWithLL(void* address, cling::Value* val);
120 template <typename T>
121 void execWithULL(void* address, cling::Value* val);
122 template <class T>
123 ExecWithRetFunc_t InitRetAndExecIntegral(clang::QualType QT, cling::Value &ret);
124
125 ExecWithRetFunc_t InitRetAndExecBuiltin(clang::QualType QT, const clang::BuiltinType *BT, cling::Value &ret);
126 ExecWithRetFunc_t InitRetAndExecNoCtor(clang::QualType QT, cling::Value &ret);
127 ExecWithRetFunc_t InitRetAndExec(const clang::FunctionDecl *FD, cling::Value &ret);
128
129 void exec(void* address, void* ret);
130
131 void exec_with_valref_return(void* address,
132 cling::Value* ret);
133 void EvaluateArgList(const std::string& ArgList);
134
136
138 if (fMinRequiredArguments == (size_t)-1)
141 }
142
143 // Implemented in source file.
144 template <typename T>
145 T ExecT(void* address);
146
147
148public:
149
150 ~TClingCallFunc() = default;
151
152 explicit TClingCallFunc(cling::Interpreter *interp)
153 : fInterp(interp), fWrapper(0)
154 {
155 fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(interp));
156 }
157
158 explicit TClingCallFunc(const TClingMethodInfo &minfo)
159 : fInterp(minfo.GetInterpreter()), fWrapper(0)
160
161 {
162 fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(minfo));
163 }
164
166 : fInterp(rhs.fInterp), fWrapper(rhs.fWrapper.load()), fArgVals(rhs.fArgVals)
167 {
168 fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(*rhs.fMethod));
169 }
170
172
173 void* ExecDefaultConstructor(const TClingClassInfo* info,
175 const std::string &type_name,
176 void* address = nullptr, unsigned long nary = 0UL);
177 void ExecDestructor(const TClingClassInfo* info, void* address = nullptr,
178 unsigned long nary = 0UL, bool withFree = true);
179 void ExecWithReturn(void* address, void *ret = nullptr);
180 void ExecWithArgsAndReturn(void* address,
181 const void* args[] = 0,
182 int nargs = 0,
183 void* ret = 0);
184 void Exec(void* address, TInterpreterValue* interpVal = 0);
185 Longptr_t ExecInt(void* address);
186 long long ExecInt64(void* address);
187 double ExecDouble(void* address);
189 void IgnoreExtraArgs(bool ignore) { /*FIXME Remove that interface */ }
190 void Init();
191 void Init(const TClingMethodInfo&);
192 void Init(std::unique_ptr<TClingMethodInfo>);
193 void Invoke(cling::Value* result = 0) const;
194 void* InterfaceMethod();
195 bool IsValid() const;
197 const clang::DeclContext *GetDeclContext() const;
198
199 int get_wrapper_code(std::string &wrapper_name, std::string &wrapper);
200
201 const clang::FunctionDecl *GetDecl() {
202 if (!fDecl)
203 fDecl = fMethod->GetTargetFunctionDecl();
204 return fDecl;
205 }
206 const clang::FunctionDecl* GetDecl() const {
207 if (fDecl)
208 return fDecl;
209 return fMethod->GetTargetFunctionDecl();
210 }
211 const clang::Decl *GetFunctionOrShadowDecl() const {
212 return fMethod->GetDecl();
213 }
214 void ResetArg();
215 void SetArg(long arg);
216 void SetArg(unsigned long arg);
217 void SetArg(float arg);
218 void SetArg(double arg);
219 void SetArg(long long arg);
220 void SetArg(unsigned long long arg);
221 void SetArgArray(Longptr_t* argArr, int narg);
222 void SetArgs(const char* args);
223 void SetFunc(const TClingClassInfo* info, const char* method,
224 const char* arglist, Longptr_t* poffset);
225 void SetFunc(const TClingClassInfo* info, const char* method,
226 const char* arglist, bool objectIsConst, Longptr_t* poffset);
227 void SetFunc(const TClingMethodInfo* info);
228 void SetFuncProto(const TClingClassInfo* info, const char* method,
229 const char* proto, Longptr_t* poffset,
231 void SetFuncProto(const TClingClassInfo* info, const char* method,
232 const char* proto, bool objectIsConst, Longptr_t* poffset,
234 void SetFuncProto(const TClingClassInfo* info, const char* method,
235 const llvm::SmallVectorImpl<clang::QualType>& proto,
236 Longptr_t* poffset,
238 void SetFuncProto(const TClingClassInfo* info, const char* method,
239 const llvm::SmallVectorImpl<clang::QualType>& proto,
240 bool objectIsConst, Longptr_t* poffset,
242};
243
244#endif // ROOT_CallFunc
long Longptr_t
Definition RtypesCore.h:82
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char mode
const char * proto
Definition civetweb.c:17502
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)
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)
size_t CalculateMinRequiredArguments()
double ExecDouble(void *address)
void SetArgArray(Longptr_t *argArr, int narg)
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(const TClingMethodInfo &minfo)
~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)
Longptr_t ExecInt(void *address)
void execWithLL(void *address, cling::Value *val)
const clang::DeclContext * GetDeclContext() const
void * compile_wrapper(const std::string &wrapper_name, const std::string &wrapper, bool withAccessControl=true)
void SetFuncProto(const TClingClassInfo *info, const char *method, const char *proto, Longptr_t *poffset, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
TClingCallFunc(cling::Interpreter *interp)
TInterpreter::CallFuncIFacePtr_t IFacePtr()
void exec(void *address, void *ret)
std::atomic< tcling_callfunc_Wrapper_t > fWrapper
Pointer to compiled wrapper, we do not own.
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)
void SetFunc(const TClingClassInfo *info, const char *method, const char *arglist, Longptr_t *poffset)
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)
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 &)
void IgnoreExtraArgs(bool ignore)
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