Logo ROOT   6.08/07
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 "TInterpreter.h"
34 
35 #include "cling/Interpreter/Value.h"
36 
37 #include <llvm/ADT/SmallVector.h>
38 
39 namespace clang {
40 class Expr;
41 class FunctionDecl;
42 class CXXMethodDecl;
43 }
44 
45 namespace cling {
46 class Interpreter;
47 }
48 
49 class TClingClassInfo;
50 class TInterpreterValue;
51 
52 typedef void (*tcling_callfunc_Wrapper_t)(void*, int, void**, void*);
53 typedef void (*tcling_callfunc_ctor_Wrapper_t)(void**, void*, unsigned long);
54 typedef void (*tcling_callfunc_dtor_Wrapper_t)(void*, unsigned long, int);
55 
57 
58 private:
59 
60  /// Cling interpreter, we do *not* own.
61  cling::Interpreter* fInterp;
62  /// ROOT normalized context for that interpreter
64  /// Current method, we own.
66  /// Pointer to compiled wrapper, we do *not* own.
68  /// Stored function arguments, we own.
69  mutable llvm::SmallVector<cling::Value, 8> fArgVals;
70  /// If true, do not limit number of function arguments to declared number.
71  bool fIgnoreExtraArgs : 1;
73 
74 private:
75  void* compile_wrapper(const std::string& wrapper_name,
76  const std::string& wrapper,
77  bool withAccessControl = true);
78 
79  void collect_type_info(clang::QualType& QT, std::ostringstream& typedefbuf,
80  std::ostringstream& callbuf, std::string& type_name,
81  bool& isReference, bool& isPointer, int indent_level,
82  bool forArgument);
83 
84  void make_narg_call(const unsigned N, std::ostringstream& typedefbuf,
85  std::ostringstream& callbuf,
86  const std::string& class_name, int indent_level);
87 
88  void make_narg_ctor(const unsigned N, std::ostringstream& typedefbuf,
89  std::ostringstream& callbuf,
90  const std::string& class_name, int indent_level);
91 
92  void make_narg_call_with_return(const unsigned N,
93  const std::string& class_name,
94  std::ostringstream& buf, int indent_level);
95 
96  void make_narg_ctor_with_return(const unsigned N,
97  const std::string& class_name,
98  std::ostringstream& buf, int indent_level);
99 
100  tcling_callfunc_Wrapper_t make_wrapper();
101 
103  make_ctor_wrapper(const TClingClassInfo* info);
104 
106  make_dtor_wrapper(const TClingClassInfo* info);
107 
108  // Implemented in source file.
109  template <typename T>
110  void execWithLL(void* address, clang::QualType QT,
111  cling::Value* val) const;
112  // Implemented in source file.
113  template <typename T>
114  void execWithULL(void* address, clang::QualType QT,
115  cling::Value* val) const;
116  void exec(void* address, void* ret) const;
117  void exec_with_valref_return(void* address,
118  cling::Value* ret) const;
119  void EvaluateArgList(const std::string& ArgList);
120 
121  // Implemented in source file.
122  template <typename T>
123  T ExecT(void* address);
124 
125 
126 public:
127 
129  delete fMethod;
130  }
131 
132  explicit TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
133  : fInterp(interp), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(false), fReturnIsRecordType(false)
134  {
135  fMethod = new TClingMethodInfo(interp);
136  }
137 
139  : fInterp(minfo.GetInterpreter()), fNormCtxt(normCtxt), fWrapper(0), fIgnoreExtraArgs(false),
140  fReturnIsRecordType(false)
141 
142  {
143  fMethod = new TClingMethodInfo(minfo);
144  }
145 
147  : fInterp(rhs.fInterp), fNormCtxt(rhs.fNormCtxt), fWrapper(rhs.fWrapper), fArgVals(rhs.fArgVals),
148  fIgnoreExtraArgs(rhs.fIgnoreExtraArgs), fReturnIsRecordType(rhs.fReturnIsRecordType)
149  {
150  fMethod = new TClingMethodInfo(*rhs.fMethod);
151  }
152 
153  TClingCallFunc &operator=(const TClingCallFunc &rhs) = delete;
154 
155  void* ExecDefaultConstructor(const TClingClassInfo* info, void* address = 0,
156  unsigned long nary = 0UL);
157  void ExecDestructor(const TClingClassInfo* info, void* address = 0,
158  unsigned long nary = 0UL, bool withFree = true);
159  void ExecWithReturn(void* address, void* ret = 0);
160  void ExecWithArgsAndReturn(void* address,
161  const void* args[] = 0,
162  int nargs = 0,
163  void* ret = 0);
164  void Exec(void* address, TInterpreterValue* interpVal = 0);
165  long ExecInt(void* address);
166  long long ExecInt64(void* address);
167  double ExecDouble(void* address);
168  TClingMethodInfo* FactoryMethod() const;
169  void IgnoreExtraArgs(bool ignore) { fIgnoreExtraArgs = ignore; }
170  void Init();
171  void Init(const clang::FunctionDecl *);
172  void Init(TClingMethodInfo*);
173  void Invoke(cling::Value* result = 0) const;
174  void* InterfaceMethod();
175  bool IsValid() const;
177  const clang::FunctionDecl* GetDecl() const { return fMethod->GetMethodDecl(); }
178  void ResetArg();
179  void SetArg(long arg);
180  void SetArg(unsigned long arg);
181  void SetArg(float arg);
182  void SetArg(double arg);
183  void SetArg(long long arg);
184  void SetArg(unsigned long long arg);
185  void SetArgArray(long* argArr, int narg);
186  void SetArgs(const char* args);
187  void SetFunc(const TClingClassInfo* info, const char* method,
188  const char* arglist, long* poffset);
189  void SetFunc(const TClingClassInfo* info, const char* method,
190  const char* arglist, bool objectIsConst, long* poffset);
191  void SetFunc(const TClingMethodInfo* info);
192  void SetFuncProto(const TClingClassInfo* info, const char* method,
193  const char* proto, long* poffset,
195  void SetFuncProto(const TClingClassInfo* info, const char* method,
196  const char* proto, bool objectIsConst, long* poffset,
198  void SetFuncProto(const TClingClassInfo* info, const char* method,
199  const llvm::SmallVectorImpl<clang::QualType>& proto,
200  long* poffset,
202  void SetFuncProto(const TClingClassInfo* info, const char* method,
203  const llvm::SmallVectorImpl<clang::QualType>& proto,
204  bool objectIsConst, long* poffset,
206 };
207 
208 #endif // ROOT_CallFunc
double T(double x)
Definition: ChebyshevPol.h:34
Emulation of the CINT MethodInfo class.
cling::Interpreter * fInterp
Cling interpreter, we do not own.
const clang::FunctionDecl * GetDecl() const
#define N
Emulation of the CINT CallFunc class.
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
void(* tcling_callfunc_ctor_Wrapper_t)(void **, void *, unsigned long)
TClingMethodInfo * fMethod
Current method, we own.
void(* tcling_callfunc_dtor_Wrapper_t)(void *, unsigned long, int)
EFunctionMatchMode
Definition: TDictionary.h:155
const ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
ROOT normalized context for that interpreter.
llvm::SmallVector< cling::Value, 8 > fArgVals
Stored function arguments, we own.
const clang::FunctionDecl * GetMethodDecl() const
bool fIgnoreExtraArgs
If true, do not limit number of function arguments to declared number.
Definition: TCling.h:48
Print a TSeq at the prompt:
Definition: TDatime.h:114
Emulation of the CINT ClassInfo class.
TClingCallFunc(cling::Interpreter *interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
tcling_callfunc_Wrapper_t fWrapper
Pointer to compiled wrapper, we do not own.
void(* tcling_callfunc_Wrapper_t)(void *, int, void **, void *)
typedef void((*Func_t)())
TClingCallFunc(TClingMethodInfo &minfo, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
bool fReturnIsRecordType
const char * proto
Definition: civetweb.c:11652
void IgnoreExtraArgs(bool ignore)
double result[121]
TClingCallFunc(const TClingCallFunc &rhs)
const char * Value
Definition: TXMLSetup.cxx:73