Logo ROOT  
Reference Guide
TClassEdit.h
Go to the documentation of this file.
1 // @(#)root/metautils:$Id$
2 // Author: Victor Perev 10/04/2003
3 // Philippe Canal 05/2004
4 
5 /*************************************************************************
6  * Copyright (C) 1995-2003, 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_TClassEdit
14 #define ROOT_TClassEdit
15 
16 #include <ROOT/RConfig.hxx>
17 #include "RConfigure.h"
18 #include <stdlib.h>
19 #ifdef R__WIN32
20 #ifndef UNDNAME_COMPLETE
21 #define UNDNAME_COMPLETE 0x0000
22 #endif
23 extern "C" {
24  char *__unDName(char *demangled, const char *mangled, int out_len,
25  void * (* pAlloc )(size_t), void (* pFree )(void *),
26  unsigned short int flags);
27 }
28 #else
29 #include <cxxabi.h>
30 #endif
31 #include <string>
32 #include <vector>
33 #include <array>
34 
35 #include "ESTLType.h"
36 
37 #ifdef R__OLDHPACC
38 namespace std {
39  using ::string;
40  using ::vector;
41 }
42 #endif
43 
44 #if defined(__CYGWIN__)
45 // std::to_string is missing on cygwin with gcc 4.8.2-2 and 4.8.3
46 #include <sstream>
47 namespace std {
48  template <typename T>
49  string to_string(T value) {
50  ostringstream os;
51  os << value;
52  return os.str();
53  }
54 }
55 #endif
56 
57 namespace cling {
58  class Interpreter;
59 }
60 namespace ROOT {
61  namespace TMetaUtils {
62  class TNormalizedCtxt;
63  }
64 }
65 #include "ROOT/RStringView.hxx"
66 
67 // TClassEdit is used to manipulate class and type names.
68 //
69 // This class does not dependent on any other ROOT facility
70 // so that it can be used by rootcint.
71 
72 namespace TClassEdit {
73 
74  enum EModType {
75  kNone = 0,
78  kDropAlloc = 1<<2,
79  kInnerClass = 1<<3,
81  kDropStlDefault = 1<<5, /* implies kDropDefaultAlloc */
82  kDropComparator = 1<<6, /* if the class has a comparator, drops BOTH the comparator and the Allocator */
83  kDropAllDefault = 1<<7, /* Drop default template parameter even in non STL classes */
84  kLong64 = 1<<8, /* replace all 'long long' with Long64_t. */
85  kDropStd = 1<<9, /* Drop any std:: */
86  kKeepOuterConst = 1<<10,/* Make sure to keep the const keyword even outside the template parameters */
87  kResolveTypedef = 1<<11,/* Strip all typedef except Double32_t and co. */
88  kDropPredicate = 1<<12,/* Drop the predicate if applies to the collection */
89  kDropHash = 1<<13 /* Drop the hash if applies to the collection */
90  };
91 
92  enum ESTLType {
108  };
109 
110  enum class EComplexType : short {
111  kNone,
112  kDouble,
113  kFloat,
114  kInt,
115  kLong
116  };
117 
118  EComplexType GetComplexType(const char*);
119 
121  public:
123  virtual ~TInterpreterLookupHelper();
124 
125  virtual bool ExistingTypeCheck(const std::string & /*tname*/,
126  std::string & /*result*/) = 0;
127  virtual void GetPartiallyDesugaredName(std::string & /*nameLong*/) = 0;
128  virtual bool IsAlreadyPartiallyDesugaredName(const std::string & /*nondef*/,
129  const std::string & /*nameLong*/) = 0;
130  virtual bool IsDeclaredScope(const std::string & /*base*/, bool & /*isInlined*/) = 0;
131  virtual bool GetPartiallyDesugaredNameWithScopeHandling(const std::string & /*tname*/,
132  std::string & /*result*/,
133  bool /* dropstd */ = true) = 0;
134  virtual void ShuttingDownSignal() = 0;
135  };
136 
137  struct TSplitType {
138 
139  const char *fName; // Original spelling of the name.
140  std::vector<std::string> fElements;
141  int fNestedLocation; // Stores the location of the tail (nested names) in nestedLoc (0 indicates no tail).
142 
143  TSplitType(const char *type2split, EModType mode = TClassEdit::kNone);
144 
145  int IsSTLCont(int testAlloc=0) const;
146  ROOT::ESTLType IsInSTL() const;
147  void ShortType(std::string &answer, int mode);
148  bool IsTemplate();
149 
150  private:
151  TSplitType(const TSplitType&); // intentionally not implemented
152  TSplitType &operator=(const TSplitType &); // intentionally not implemented
153  };
154 
156 
157  std::string CleanType (const char *typeDesc,int mode = 0,const char **tail=0);
158  bool IsDefAlloc(const char *alloc, const char *classname);
159  bool IsDefAlloc(const char *alloc, const char *keyclassname, const char *valueclassname);
160  bool IsDefComp (const char *comp , const char *classname);
161  bool IsDefPred(const char *predname, const char *classname);
162  bool IsDefHash(const char *hashname, const char *classname);
163  bool IsInterpreterDetail(const char *type);
164  bool IsSTLBitset(const char *type);
169  int IsSTLCont (const char *type,int testAlloc);
170  bool IsStdClass(const char *type);
171  bool IsVectorBool(const char *name);
172  void GetNormalizedName(std::string &norm_name, std::string_view name);
173  inline void GetNormalizedName (std::string &norm_name, ROOT::Internal::TStringView name) { return GetNormalizedName(norm_name, std::string_view(name)); }
174  std::string GetLong64_Name(const char *original);
175  std::string GetLong64_Name(const std::string& original);
176  int GetSplit (const char *type, std::vector<std::string> &output, int &nestedLoc, EModType mode = TClassEdit::kNone);
177  ROOT::ESTLType STLKind(std::string_view type); //Kind of stl container
179  int STLArgs (int kind); //Min number of arguments without allocator
180  std::string ResolveTypedef(const char *tname, bool resolveAll = false);
181  std::string ShortType (const char *typeDesc, int mode);
182  std::string InsertStd(const char *tname);
183  const char* GetUnqualifiedName(const char*name);
184  inline bool IsUniquePtr(std::string_view name) {return 0 == name.compare(0, 11, "unique_ptr<");}
186  inline bool IsStdArray(std::string_view name) {return 0 == name.compare(0, 6, "array<");}
189  {
190  // Find the first template parameter
191  std::vector<std::string> v;
192  int i;
193  GetSplit(name.data(), v, i);
194  return v[1];
195  }
197  std::string GetNameForIO(const std::string& templateInstanceName,
199  bool* hasChanged = nullptr);
200  bool GetStdArrayProperties(const char* typeName,
201  std::string& typeNameBuf,
202  std::array<int, 5>& maxIndices,
203  int& ndim);
204 
205  inline char* DemangleName(const char* mangled_name, int& errorCode)
206  {
207  // Demangle in a portable way the name.
208  // IMPORTANT: The caller is responsible for freeing the returned const char*
209 
210  errorCode=0;
211 #ifdef R__WIN32
212  char *demangled_name = __unDName(0, mangled_name, 0, malloc, free, UNDNAME_COMPLETE);
213  if (!demangled_name) {
214  errorCode = -1;
215  return nullptr;
216  }
217  std::string demangledName = demangled_name;
218  if (demangledName.compare(0, 6, "class ") == 0)
219  demangledName.erase(0, 6);
220  else if (demangledName.compare(0, 7, "struct ") == 0)
221  demangledName.erase(0, 7);
222  strcpy(demangled_name, demangledName.c_str());
223 #else
224  char *demangled_name = abi::__cxa_demangle(mangled_name, 0, 0, &errorCode);
225  if (!demangled_name || errorCode) {
226  free(demangled_name);
227  return nullptr;
228  }
229 #endif
230  return demangled_name;
231  }
232  char* DemangleTypeIdName(const std::type_info& ti, int& errorCode);
233 
234 
235  /// Result of splitting a function declaration into
236  /// fReturnType fScopeName::fFunctionName<fFunctionTemplateArguments>(fFunctionParameters)
238  /// Return type of the function, might be empty if the function declaration string did not provide it.
239  std::string fReturnType;
240 
241  /// Name of the scope qualification of the function, possibly empty
242  std::string fScopeName;
243 
244  /// Name of the function
245  std::string fFunctionName;
246 
247  /// Template arguments of the function template specialization, if any; will contain one element "" for
248  /// `function<>()`
249  std::vector<std::string> fFunctionTemplateArguments;
250 
251  /// Function parameters.
252  std::vector<std::string> fFunctionParameters;
253  };
254 
255  /// Split a function declaration into its different parts.
257 }
258 
259 #endif
TClassEdit::FunctionSplitInfo
Result of splitting a function declaration into fReturnType fScopeName::fFunctionName<fFunctionTempla...
Definition: TClassEdit.h:237
TClassEdit::kUnorderedMultiSet
@ kUnorderedMultiSet
Definition: TClassEdit.h:103
TClassEdit::kNotSTL
@ kNotSTL
Definition: TClassEdit.h:93
ROOT::kSTLdeque
@ kSTLdeque
Definition: ESTLType.h:32
ROOT::kSTLvector
@ kSTLvector
Definition: ESTLType.h:30
ROOT::kSTLset
@ kSTLset
Definition: ESTLType.h:35
ROOT::kSTLunorderedmultimap
@ kSTLunorderedmultimap
Definition: ESTLType.h:45
ROOT::kSTLbitset
@ kSTLbitset
Definition: ESTLType.h:37
TClassEdit::GetNameForIO
std::string GetNameForIO(const std::string &templateInstanceName, TClassEdit::EModType mode=TClassEdit::kNone, bool *hasChanged=nullptr)
Definition: TClassEdit.cxx:2063
TClassEdit::GetNormalizedName
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.
Definition: TClassEdit.cxx:833
TClassEdit::TInterpreterLookupHelper::TInterpreterLookupHelper
TInterpreterLookupHelper()
Definition: TClassEdit.h:122
TClassEdit::TInterpreterLookupHelper::GetPartiallyDesugaredNameWithScopeHandling
virtual bool GetPartiallyDesugaredNameWithScopeHandling(const std::string &, std::string &, bool=true)=0
TClassEdit::TSplitType::operator=
TSplitType & operator=(const TSplitType &)
TClassEdit::EComplexType::kFloat
@ kFloat
TClassEdit::FunctionSplitInfo::fFunctionName
std::string fFunctionName
Name of the function.
Definition: TClassEdit.h:245
TClassEdit::kDeque
@ kDeque
Definition: TClassEdit.h:97
TClassEdit::kVector
@ kVector
Definition: TClassEdit.h:94
TClassEdit::GetStdArrayProperties
bool GetStdArrayProperties(const char *typeName, std::string &typeNameBuf, std::array< int, 5 > &maxIndices, int &ndim)
Definition: TClassEdit.cxx:2085
ROOT::kSTLunorderedmultiset
@ kSTLunorderedmultiset
Definition: ESTLType.h:43
ROOT::kNotSTL
@ kNotSTL
Definition: ESTLType.h:29
TClassEdit::TInterpreterLookupHelper::ExistingTypeCheck
virtual bool ExistingTypeCheck(const std::string &, std::string &)=0
TClassEdit::ShortType
std::string ShortType(const char *typeDesc, int mode)
Return the absolute type of typeDesc.
Definition: TClassEdit.cxx:1276
TClassEdit::STLArgs
int STLArgs(int kind)
Return number of arguments for STL container before allocator.
Definition: TClassEdit.cxx:555
TClassEdit::kLong64
@ kLong64
Definition: TClassEdit.h:84
output
static void output(int code)
Definition: gifencode.c:226
string_view
basic_string_view< char > string_view
Definition: libcpp_string_view.h:785
TClassEdit::FunctionSplitInfo::fScopeName
std::string fScopeName
Name of the scope qualification of the function, possibly empty.
Definition: TClassEdit.h:242
ROOT::kSTLend
@ kSTLend
Definition: ESTLType.h:46
TClassEdit::kDropAlloc
@ kDropAlloc
Definition: TClassEdit.h:78
TClassEdit
Definition: TClassEdit.h:72
TClassEdit::kMultiMap
@ kMultiMap
Definition: TClassEdit.h:99
TClassEdit::kUnorderedSet
@ kUnorderedSet
Definition: TClassEdit.h:102
TClassEdit::IsStdClass
bool IsStdClass(const char *type)
return true if the class belongs to the std namespace
Definition: TClassEdit.cxx:1389
TClassEdit::kDropTrailStar
@ kDropTrailStar
Definition: TClassEdit.h:76
TClassEdit::GetLong64_Name
std::string GetLong64_Name(const char *original)
Replace 'long long' and 'unsigned long long' by 'Long64_t' and 'ULong64_t'.
Definition: TClassEdit.cxx:879
TClassEdit::IsVectorBool
bool IsVectorBool(const char *name)
Definition: TClassEdit.cxx:1423
TClassEdit::TSplitType::IsInSTL
ROOT::ESTLType IsInSTL() const
type : type name: vector<list<classA,allocator>,allocator>[::iterator] result: 0 : not stl container ...
Definition: TClassEdit.cxx:172
TClassEdit::TSplitType::IsSTLCont
int IsSTLCont(int testAlloc=0) const
type : type name: vector<list<classA,allocator>,allocator> testAlloc: if true, we test allocator,...
Definition: TClassEdit.cxx:189
TClassEdit::EComplexType::kNone
@ kNone
TClassEdit::kDropDefaultAlloc
@ kDropDefaultAlloc
Definition: TClassEdit.h:77
TClassEdit::kDropHash
@ kDropHash
Definition: TClassEdit.h:89
TClassEdit::TSplitType::TSplitType
TSplitType(const TSplitType &)
RConfig.hxx
TClassEdit::kSet
@ kSet
Definition: TClassEdit.h:100
TClassEdit::TSplitType
Definition: TClassEdit.h:137
TClassEdit::SplitFunction
bool SplitFunction(std::string_view decl, FunctionSplitInfo &result)
Split a function declaration into its different parts.
Definition: TClassEdit.cxx:2210
ROOT::kSTLlist
@ kSTLlist
Definition: ESTLType.h:31
TClassEdit::kDropAllDefault
@ kDropAllDefault
Definition: TClassEdit.h:83
v
@ v
Definition: rootcling_impl.cxx:3635
TClassEdit::kResolveTypedef
@ kResolveTypedef
Definition: TClassEdit.h:87
TClassEdit::TInterpreterLookupHelper::ShuttingDownSignal
virtual void ShuttingDownSignal()=0
TClassEdit::kInnedMostClass
@ kInnedMostClass
Definition: TClassEdit.h:80
ROOT::kSTLmap
@ kSTLmap
Definition: ESTLType.h:33
TClassEdit::kList
@ kList
Definition: TClassEdit.h:95
ROOT::Internal::TStringView
Definition: RStringView.hxx:77
TClassEdit::TSplitType::fElements
std::vector< std::string > fElements
Definition: TClassEdit.h:140
TClassEdit::kMap
@ kMap
Definition: TClassEdit.h:98
ROOT::ESTLType
ESTLType
Definition: ESTLType.h:28
TClassEdit::FunctionSplitInfo::fReturnType
std::string fReturnType
Return type of the function, might be empty if the function declaration string did not provide it.
Definition: TClassEdit.h:239
TClassEdit::DemangleName
char * DemangleName(const char *mangled_name, int &errorCode)
Definition: TClassEdit.h:205
TClassEdit::IsSTLCont
ROOT::ESTLType IsSTLCont(std::string_view type)
type : type name: vector<list<classA,allocator>,allocator> result: 0 : not stl container code of cont...
Definition: TClassEdit.cxx:1349
TClassEdit::kNone
@ kNone
Definition: TClassEdit.h:75
ROOT::kSTLmultiset
@ kSTLmultiset
Definition: ESTLType.h:36
TClassEdit::kBitSet
@ kBitSet
Definition: TClassEdit.h:106
malloc
#define malloc
Definition: civetweb.c:1536
TClassEdit::kUnorderedMap
@ kUnorderedMap
Definition: TClassEdit.h:104
TClassEdit::EComplexType
EComplexType
Definition: TClassEdit.h:110
ROOT::kSTLforwardlist
@ kSTLforwardlist
Definition: ESTLType.h:41
TClassEdit::CleanType
std::string CleanType(const char *typeDesc, int mode=0, const char **tail=0)
Cleanup type description, redundant blanks removed and redundant tail ignored return *tail = pointer ...
Definition: TClassEdit.cxx:1203
TClassEdit::TInterpreterLookupHelper
Definition: TClassEdit.h:120
RStringView.hxx
TClassEdit::IsSTLBitset
bool IsSTLBitset(const char *type)
Return true is the name is std::bitset<number> or bitset<number>
Definition: TClassEdit.cxx:1311
TClassEdit::kDropComparator
@ kDropComparator
Definition: TClassEdit.h:82
TClassEdit::TInterpreterLookupHelper::IsAlreadyPartiallyDesugaredName
virtual bool IsAlreadyPartiallyDesugaredName(const std::string &, const std::string &)=0
TClassEdit::kUnorderedMultiMap
@ kUnorderedMultiMap
Definition: TClassEdit.h:105
TClassEdit::TSplitType::fName
const char * fName
Definition: TClassEdit.h:139
TClassEdit::EComplexType::kInt
@ kInt
ROOT::kSTLunorderedset
@ kSTLunorderedset
Definition: ESTLType.h:42
TClassEdit::IsDefComp
bool IsDefComp(const char *comp, const char *classname)
return whether or not 'compare' is the STL default comparator for type 'classname'
Definition: TClassEdit.cxx:800
TClassEdit::kInnerClass
@ kInnerClass
Definition: TClassEdit.h:79
ROOT::kSTLmultimap
@ kSTLmultimap
Definition: ESTLType.h:34
TClassEdit::GetUnqualifiedName
const char * GetUnqualifiedName(const char *name)
Return the start of the unqualified name include in 'original'.
Definition: TClassEdit.cxx:913
TClassEdit::TSplitType::fNestedLocation
int fNestedLocation
Definition: TClassEdit.h:141
TClassEdit::TInterpreterLookupHelper::IsDeclaredScope
virtual bool IsDeclaredScope(const std::string &, bool &)=0
TClassEdit::IsInterpreterDetail
bool IsInterpreterDetail(const char *type)
Return true if the type is one the interpreter details which are only forward declared (ClassInfo_t e...
Definition: TClassEdit.cxx:1293
TClassEdit::IsDefHash
bool IsDefHash(const char *hashname, const char *classname)
return whether or not 'hashname' is the STL default hash for type 'classname'
Definition: TClassEdit.cxx:818
TClassEdit::IsDefPred
bool IsDefPred(const char *predname, const char *classname)
return whether or not 'predname' is the STL default predicate for type 'classname'
Definition: TClassEdit.cxx:809
TClassEdit::TSplitType::ShortType
void ShortType(std::string &answer, int mode)
Return the absolute type of typeDesc into the string answ.
Definition: TClassEdit.cxx:233
TClassEdit::ESTLType
ESTLType
Definition: TClassEdit.h:92
TClassEdit::EComplexType::kLong
@ kLong
TClassEdit::InsertStd
std::string InsertStd(const char *tname)
Definition: TClassEdit.cxx:1755
TClassEdit::GetSplit
int GetSplit(const char *type, std::vector< std::string > &output, int &nestedLoc, EModType mode=TClassEdit::kNone)
Stores in output (after emptying it) the split type.
Definition: TClassEdit.cxx:1000
TClassEdit::TSplitType::IsTemplate
bool IsTemplate()
Check if the type is a template.
Definition: TClassEdit.cxx:502
TClassEdit::IsUniquePtr
bool IsUniquePtr(std::string_view name)
Definition: TClassEdit.h:184
TClassEdit::GetUniquePtrType
std::string GetUniquePtrType(std::string_view name)
Definition: TClassEdit.h:188
TClassEdit::TInterpreterLookupHelper::GetPartiallyDesugaredName
virtual void GetPartiallyDesugaredName(std::string &)=0
TClassEdit::kDropPredicate
@ kDropPredicate
Definition: TClassEdit.h:88
TClassEdit::UnderlyingIsSTLCont
ROOT::ESTLType UnderlyingIsSTLCont(std::string_view type)
Return the type of STL collection, if any, that is the underlying type of the given type.
Definition: TClassEdit.cxx:1330
short
l unsigned short
Definition: Converters.cxx:862
TClassEdit::TInterpreterLookupHelper::~TInterpreterLookupHelper
virtual ~TInterpreterLookupHelper()
Definition: TClassEdit.cxx:141
TClassEdit::EModType
EModType
Definition: TClassEdit.h:74
TClassEdit::Init
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:154
name
char name[80]
Definition: TGX11.cxx:110
TClassEdit::kDropStd
@ kDropStd
Definition: TClassEdit.h:85
ROOT::kSTLunorderedmap
@ kSTLunorderedmap
Definition: ESTLType.h:44
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
TClassEdit::kEnd
@ kEnd
Definition: TClassEdit.h:107
ESTLType.h
TClassEdit::ResolveTypedef
std::string ResolveTypedef(const char *tname, bool resolveAll=false)
Definition: TClassEdit.cxx:1719
TClassEdit::STLKind
ROOT::ESTLType STLKind(std::string_view type)
Converts STL container name to number.
Definition: TClassEdit.cxx:511
TClassEdit::EComplexType::kDouble
@ kDouble
type
int type
Definition: TGX11.cxx:121
free
#define free
Definition: civetweb.c:1539
TClassEdit::FunctionSplitInfo::fFunctionParameters
std::vector< std::string > fFunctionParameters
Function parameters.
Definition: TClassEdit.h:252
TClassEdit::kKeepOuterConst
@ kKeepOuterConst
Definition: TClassEdit.h:86
TClassEdit::kForwardlist
@ kForwardlist
Definition: TClassEdit.h:96
TClassEdit::FunctionSplitInfo::fFunctionTemplateArguments
std::vector< std::string > fFunctionTemplateArguments
Template arguments of the function template specialization, if any; will contain one element "" for f...
Definition: TClassEdit.h:249
TClassEdit::GetComplexType
EComplexType GetComplexType(const char *)
Definition: TClassEdit.cxx:120
ROOT
VSD Structures.
Definition: StringConv.hxx:21
TClassEdit::kDropStlDefault
@ kDropStlDefault
Definition: TClassEdit.h:81
TClassEdit::kMultiSet
@ kMultiSet
Definition: TClassEdit.h:101
TClassEdit::DemangleTypeIdName
char * DemangleTypeIdName(const std::type_info &ti, int &errorCode)
Demangle in a portable way the type id name.
Definition: TClassEdit.cxx:2116
TClassEdit::IsDefAlloc
bool IsDefAlloc(const char *alloc, const char *classname)
return whether or not 'allocname' is the STL default allocator for type 'classname'
Definition: TClassEdit.cxx:600
TClassEdit::TSplitType::TSplitType
TSplitType(const char *type2split, EModType mode=TClassEdit::kNone)
default constructor
Definition: TClassEdit.cxx:162
TClassEdit::IsStdArray
bool IsStdArray(std::string_view name)
Definition: TClassEdit.h:186