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 void exec(void* address, void* ret);
118
119 void exec_with_valref_return(void* address,
120 cling::Value& ret);
121 void EvaluateArgList(const std::string& ArgList);
122
124
126 if (fMinRequiredArguments == (size_t)-1)
129 }
130
131 // Implemented in source file.
132 template <typename T>
133 T ExecT(void* address);
134
135
136public:
137
138 ~TClingCallFunc() = default;
139
140 explicit TClingCallFunc(cling::Interpreter *interp)
141 : fInterp(interp), fWrapper(0)
142 {
143 fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(interp));
144 }
145
146 explicit TClingCallFunc(const TClingMethodInfo &minfo)
147 : fInterp(minfo.GetInterpreter()), fWrapper(0)
148
149 {
150 fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(minfo));
151 }
152
154 : fInterp(rhs.fInterp), fWrapper(rhs.fWrapper.load()), fArgVals(rhs.fArgVals)
155 {
156 fMethod = std::unique_ptr<TClingMethodInfo>(new TClingMethodInfo(*rhs.fMethod));
157 }
158
160
161 void* ExecDefaultConstructor(const TClingClassInfo* info,
163 const std::string &type_name,
164 void* address = nullptr, unsigned long nary = 0UL);
165 void ExecDestructor(const TClingClassInfo* info, void* address = nullptr,
166 unsigned long nary = 0UL, bool withFree = true);
167 void ExecWithReturn(void* address, void *ret = nullptr);
168 void ExecWithArgsAndReturn(void* address,
169 const void* args[] = 0,
170 int nargs = 0,
171 void* ret = 0);
172 void Exec(void* address, TInterpreterValue* interpVal = 0);
173 Longptr_t ExecInt(void* address);
174 long long ExecInt64(void* address);
175 double ExecDouble(void* address);
177 void IgnoreExtraArgs(bool ignore) { /*FIXME Remove that interface */ }
178 void Init();
179 void Init(const TClingMethodInfo&);
180 void Init(std::unique_ptr<TClingMethodInfo>);
181 void Invoke(cling::Value* result = 0) const;
182 void* InterfaceMethod();
183 bool IsValid() const;
185 const clang::DeclContext *GetDeclContext() const;
186
187 int get_wrapper_code(std::string &wrapper_name, std::string &wrapper);
188
189 const clang::FunctionDecl *GetDecl() {
191 if (!fDecl)
192 fDecl = fMethod->GetTargetFunctionDecl();
193 return fDecl;
194 }
195 const clang::FunctionDecl* GetDecl() const {
196 if (fDecl)
197 return fDecl;
198 return fMethod->GetTargetFunctionDecl();
199 }
200 const clang::Decl *GetFunctionOrShadowDecl() const {
201 return fMethod->GetDecl();
202 }
203 void ResetArg();
204 template<typename T, std::enable_if_t<std::is_fundamental<T>::value, bool> = true>
205 void SetArg(T arg) {
206 fArgVals.push_back(cling::Value::Create(*fInterp, arg));
207 }
208 void SetArgArray(Longptr_t* argArr, int narg);
209 void SetArgs(const char* args);
210 void SetFunc(const TClingClassInfo* info, const char* method,
211 const char* arglist, Longptr_t* poffset);
212 void SetFunc(const TClingClassInfo* info, const char* method,
213 const char* arglist, bool objectIsConst, Longptr_t* poffset);
214 void SetFunc(const TClingMethodInfo* info);
215 void SetFuncProto(const TClingClassInfo* info, const char* method,
216 const char* proto, Longptr_t* poffset,
218 void SetFuncProto(const TClingClassInfo* info, const char* method,
219 const char* proto, bool objectIsConst, Longptr_t* poffset,
221 void SetFuncProto(const TClingClassInfo* info, const char* method,
222 const llvm::SmallVectorImpl<clang::QualType>& proto,
223 Longptr_t* poffset,
225 void SetFuncProto(const TClingClassInfo* info, const char* method,
226 const llvm::SmallVectorImpl<clang::QualType>& proto,
227 bool objectIsConst, Longptr_t* poffset,
229};
230
231#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
R__EXTERN TVirtualMutex * gInterpreterMutex
#define R__LOCKGUARD_CLING(mutex)
const char * proto
Definition civetweb.c:17536
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)
void exec_with_valref_return(void *address, cling::Value &ret)
std::unique_ptr< TClingMethodInfo > fMethod
Current method, we own.
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()
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)
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 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 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
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)
void SetArg(T arg)
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)
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)
Emulation of the CINT ClassInfo class.
Emulation of the CINT MethodInfo class.
EFunctionMatchMode
@ kConversionMatch