Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <cstdlib>
19#include <stdexcept>
20#ifdef R__WIN32
21#ifndef UNDNAME_COMPLETE
22#define UNDNAME_COMPLETE 0x0000
23#endif
24extern "C" {
25 char *__unDName(char *demangled, const char *mangled, int out_len,
26 void * (* pAlloc )(size_t), void (* pFree )(void *),
27 unsigned short int flags);
28}
29#else
30#include <cxxabi.h>
31#endif
32#include <string>
33#include <vector>
34#include <array>
35#include <typeinfo>
36
37#include "ESTLType.h"
38
39#ifdef R__OLDHPACC
40namespace std {
41 using ::string;
42 using ::vector;
43}
44#endif
45
46#if defined(__CYGWIN__)
47// std::to_string is missing on cygwin with gcc 4.8.2-2 and 4.8.3
48#include <sstream>
49namespace std {
50 template <typename T>
51 string to_string(T value) {
52 ostringstream os;
53 os << value;
54 return os.str();
55 }
56}
57#endif
58
59namespace cling {
60 class Interpreter;
61}
62namespace ROOT {
63 namespace TMetaUtils {
64 class TNormalizedCtxt;
65 }
66}
67#include <string_view>
68
69// TClassEdit is used to manipulate class and type names.
70//
71// This class does not dependent on any other ROOT facility
72// so that it can be used by rootcint.
73
74namespace TClassEdit {
75
76 enum EModType {
77 kNone = 0,
80 kDropAlloc = 1<<2,
83 kDropStlDefault = 1<<5, /* implies kDropDefaultAlloc */
84 kDropComparator = 1<<6, /* if the class has a comparator, drops BOTH the comparator and the Allocator */
85 kDropAllDefault = 1<<7, /* Drop default template parameter even in non STL classes */
86 kLong64 = 1<<8, /* replace all 'long long' with Long64_t. */
87 kDropStd = 1<<9, /* Drop any std:: */
88 kKeepOuterConst = 1<<10,/* Make sure to keep the const keyword even outside the template parameters */
89 kResolveTypedef = 1<<11,/* Strip all typedef except Double32_t and co. */
90 kDropPredicate = 1<<12,/* Drop the predicate if applies to the collection */
91 kDropHash = 1<<13 /* Drop the hash if applies to the collection */
92 };
93
94 enum class EComplexType : short {
95 kNone,
96 kDouble,
97 kFloat,
98 kInt,
99 kLong
100 };
101
102 EComplexType GetComplexType(const char*);
103
105 public:
108
109 virtual bool CheckInClassTable(const std::string &, std::string &) = 0;
110 virtual bool ExistingTypeCheck(const std::string & /*tname*/,
111 std::string & /*result*/) = 0;
112 virtual void GetPartiallyDesugaredName(std::string & /*nameLong*/) = 0;
113 virtual bool IsAlreadyPartiallyDesugaredName(const std::string & /*nondef*/,
114 const std::string & /*nameLong*/) = 0;
115 virtual bool IsDeclaredScope(const std::string & /*base*/, bool & /*isInlined*/) = 0;
116 virtual bool GetPartiallyDesugaredNameWithScopeHandling(const std::string & /*tname*/,
117 std::string & /*result*/,
118 bool /* dropstd */ = true) = 0;
119 virtual void ShuttingDownSignal() = 0;
120 };
121
122 struct TSplitType {
123
124 const char *fName; // Original spelling of the name.
125 std::vector<std::string> fElements;
126 int fNestedLocation; // Stores the location of the tail (nested names) in nestedLoc (0 indicates no tail).
127
128 TSplitType(const char *type2split, EModType mode = TClassEdit::kNone);
129
130 int IsSTLCont(int testAlloc=0) const;
131 ROOT::ESTLType IsInSTL() const;
132 void ShortType(std::string &answer, int mode);
133 bool IsTemplate();
134
135 private:
136 TSplitType(const TSplitType&) = delete;
137 TSplitType &operator=(const TSplitType &) = delete;
138 };
139
140 /// A RAII helper to remove and readd enclosing _Atomic()
141 /// It expects no spaces at the beginning or end of the string
143 public:
144 enum class EBehavior : short {
145 kDetectStripReadd, // Detect if the _Atomic specifier is used, if yes, strip it and re-add it in the dtor
146 kDetectStrip, // Detect if the _Atomic specifier is used, if yes, strip it
147 kReadd, // Re-add the _Atomic specifier in the dtor
148 kNoOp // Do nothing
149 };
150
151 private:
152 std::string &fTypeName;
153 bool fIsAtomic = false;
155 const char *fgPrefix = "_Atomic(";
156
157 public:
159 : fTypeName(typeName), fBehavior(behavior)
160 {
162 return;
163 if (0 == fTypeName.find(fgPrefix)) {
164 fIsAtomic = true;
166 fTypeName.erase(0, 8);
167 if (0 == fTypeName.size() || ')' != fTypeName[fTypeName.size() - 1]) {
168 throw std::runtime_error("Cannot remove substring \")\" at the end of typename \"" + typeName + "\"");
169 }
170 fTypeName.pop_back();
171 }
172 }
173 }
174
175 bool IsAtomic() { return fIsAtomic; }
176
183 };
184
186
187 std::string CleanType (const char *typeDesc,int mode = 0,const char **tail = nullptr);
188 inline bool IsArtificial(std::string_view name) { return name.find('@') != name.npos; }
189 bool IsDefAlloc(const char *alloc, const char *classname);
190 bool IsDefAlloc(const char *alloc, const char *keyclassname, const char *valueclassname);
191 bool IsDefComp (const char *comp , const char *classname);
192 bool IsDefPred(const char *predname, const char *classname);
193 bool IsDefHash(const char *hashname, const char *classname);
194 bool IsInterpreterDetail(const char *type);
195 bool IsSTLBitset(const char *type);
196 ROOT::ESTLType UnderlyingIsSTLCont(std::string_view type);
197 ROOT::ESTLType IsSTLCont (std::string_view type);
198 int IsSTLCont (const char *type,int testAlloc);
199 bool IsStdClass(const char *type);
200 bool IsVectorBool(const char *name);
201 void GetNormalizedName(std::string &norm_name, std::string_view name);
202 std::string GetLong64_Name(const char *original);
203 std::string GetLong64_Name(const std::string& original);
204 int GetSplit (const char *type, std::vector<std::string> &output, int &nestedLoc, EModType mode = TClassEdit::kNone);
205 ROOT::ESTLType STLKind(std::string_view type); //Kind of stl container
206 int STLArgs (int kind); //Min number of arguments without allocator
207 std::string ResolveTypedef(const char *tname, bool resolveAll = false);
208 std::string ShortType (const char *typeDesc, int mode);
209 std::string InsertStd(const char *tname);
210 const char* GetUnqualifiedName(const char*name);
211 inline bool IsUniquePtr(std::string_view name) {return 0 == name.compare(0, 11, "unique_ptr<");}
212 inline bool IsStdArray(std::string_view name) {return 0 == name.compare(0, 6, "array<");}
213 inline bool IsStdPair(std::string_view name)
214 {
215 return 0 == name.compare(0, 10, "std::pair<") || 0 == name.compare(0, 5, "pair<");
216 }
217 inline bool IsStdPairBase(std::string_view name)
218 {
219 return 0 == name.compare(0, 17, "std::__pair_base<") || 0 == name.compare(0, 12, "__pair_base<");
220 }
221 inline std::string R__DEPRECATED(6, 46, "not needed anymore") GetUniquePtrType(std::string_view name)
222 {
223 // Find the first template parameter
224 std::vector<std::string> v;
225 int i;
226 GetSplit(std::string(name).c_str(), v, i);
227 return v[1];
228 }
229 std::string GetNameForIO(const std::string& templateInstanceName,
231 bool* hasChanged = nullptr);
232 bool GetStdArrayProperties(const char* typeName,
233 std::string& typeNameBuf,
234 std::array<int, 5>& maxIndices,
235 int& ndim);
236
237 inline char* DemangleName(const char* mangled_name, int& errorCode)
238 {
239 // Demangle in a portable way the name.
240 // IMPORTANT: The caller is responsible for freeing the returned const char*
241
242 errorCode=0;
243#ifdef R__WIN32
244 char *demangled_name = __unDName(0, mangled_name, 0, malloc, free, UNDNAME_COMPLETE);
245 if (!demangled_name) {
246 errorCode = -1;
247 return nullptr;
248 }
249 std::string demangledName = demangled_name;
250 if (demangledName.compare(0, 6, "class ") == 0)
251 demangledName.erase(0, 6);
252 else if (demangledName.compare(0, 7, "struct ") == 0)
253 demangledName.erase(0, 7);
254 strcpy(demangled_name, demangledName.c_str());
255#else
256 char *demangled_name = abi::__cxa_demangle(mangled_name, nullptr, nullptr, &errorCode);
257 if (!demangled_name || errorCode) {
258 free(demangled_name);
259 return nullptr;
260 }
261#endif
262 return demangled_name;
263 }
264 char* DemangleTypeIdName(const std::type_info& ti, int& errorCode);
265
266
267 /// Result of splitting a function declaration into
268 /// fReturnType fScopeName::fFunctionName<fFunctionTemplateArguments>(fFunctionParameters)
270 /// Return type of the function, might be empty if the function declaration string did not provide it.
271 std::string fReturnType;
272
273 /// Name of the scope qualification of the function, possibly empty
274 std::string fScopeName;
275
276 /// Name of the function
277 std::string fFunctionName;
278
279 /// Template arguments of the function template specialization, if any; will contain one element "" for
280 /// `function<>()`
281 std::vector<std::string> fFunctionTemplateArguments;
282
283 /// Function parameters.
284 std::vector<std::string> fFunctionParameters;
285 };
286
287 /// Split a function declaration into its different parts.
288 bool SplitFunction(std::string_view decl, FunctionSplitInfo &result);
289}
290
291#endif
free(fBuffer)
#define R__DEPRECATED(MAJOR, MINOR, REASON)
Definition RConfig.hxx:486
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 GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char mode
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
A RAII helper to remove and readd enclosing _Atomic() It expects no spaces at the beginning or end of...
Definition TClassEdit.h:142
AtomicTypeNameHandlerRAII(std::string &typeName, EBehavior behavior=EBehavior::kDetectStripReadd)
Definition TClassEdit.h:158
virtual bool CheckInClassTable(const std::string &, std::string &)=0
virtual bool GetPartiallyDesugaredNameWithScopeHandling(const std::string &, std::string &, bool=true)=0
virtual bool IsAlreadyPartiallyDesugaredName(const std::string &, const std::string &)=0
virtual bool IsDeclaredScope(const std::string &, bool &)=0
virtual void GetPartiallyDesugaredName(std::string &)=0
virtual bool ExistingTypeCheck(const std::string &, std::string &)=0
ESTLType
Definition ESTLType.h:28
ROOT::ESTLType STLKind(std::string_view type)
Converts STL container name to number.
bool IsStdPairBase(std::string_view name)
Definition TClassEdit.h:217
bool IsDefComp(const char *comp, const char *classname)
return whether or not 'compare' is the STL default comparator for type 'classname'
std::string ResolveTypedef(const char *tname, bool resolveAll=false)
bool IsStdArray(std::string_view name)
Definition TClassEdit.h:212
bool IsStdClass(const char *type)
return true if the class belongs to the std namespace
bool IsDefHash(const char *hashname, const char *classname)
return whether or not 'hashname' is the STL default hash for type 'classname'
bool IsStdPair(std::string_view name)
Definition TClassEdit.h:213
bool IsInterpreterDetail(const char *type)
Return true if the type is one the interpreter details which are only forward declared (ClassInfo_t e...
std::string InsertStd(const char *tname)
bool SplitFunction(std::string_view decl, FunctionSplitInfo &result)
Split a function declaration into its different parts.
std::string GetLong64_Name(const char *original)
Replace 'long long' and 'unsigned long long' by 'Long64_t' and 'ULong64_t'.
bool IsDefPred(const char *predname, const char *classname)
return whether or not 'predname' is the STL default predicate for type 'classname'
char * DemangleTypeIdName(const std::type_info &ti, int &errorCode)
Demangle in a portable way the type id name.
const char * GetUnqualifiedName(const char *name)
Return the start of the unqualified name include in 'original'.
bool IsVectorBool(const char *name)
void Init(TClassEdit::TInterpreterLookupHelper *helper)
ROOT::ESTLType IsSTLCont(std::string_view type)
type : type name: vector<list<classA,allocator>,allocator> result: 0 : not stl container code of cont...
std::string CleanType(const char *typeDesc, int mode=0, const char **tail=nullptr)
Cleanup type description, redundant blanks removed and redundant tail ignored return *tail = pointer ...
std::string ShortType(const char *typeDesc, int mode)
Return the absolute type of typeDesc.
char * DemangleName(const char *mangled_name, int &errorCode)
Definition TClassEdit.h:237
bool IsArtificial(std::string_view name)
Definition TClassEdit.h:188
bool GetStdArrayProperties(const char *typeName, std::string &typeNameBuf, std::array< int, 5 > &maxIndices, int &ndim)
std::string GetNameForIO(const std::string &templateInstanceName, TClassEdit::EModType mode=TClassEdit::kNone, bool *hasChanged=nullptr)
int STLArgs(int kind)
Return number of arguments for STL container before allocator.
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.
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.
bool IsDefAlloc(const char *alloc, const char *classname)
return whether or not 'allocname' is the STL default allocator for type 'classname'
bool IsUniquePtr(std::string_view name)
Definition TClassEdit.h:211
@ kDropDefaultAlloc
Definition TClassEdit.h:79
@ kResolveTypedef
Definition TClassEdit.h:89
@ kInnedMostClass
Definition TClassEdit.h:82
@ kDropComparator
Definition TClassEdit.h:84
@ kDropAllDefault
Definition TClassEdit.h:85
@ kKeepOuterConst
Definition TClassEdit.h:88
@ kDropStlDefault
Definition TClassEdit.h:83
bool IsSTLBitset(const char *type)
Return true is the name is std::bitset<number> or bitset<number>
ROOT::ESTLType UnderlyingIsSTLCont(std::string_view type)
Return the type of STL collection, if any, that is the underlying type of the given type.
EComplexType GetComplexType(const char *)
Result of splitting a function declaration into fReturnType fScopeName::fFunctionName<fFunctionTempla...
Definition TClassEdit.h:269
std::string fFunctionName
Name of the function.
Definition TClassEdit.h:277
std::vector< std::string > fFunctionTemplateArguments
Template arguments of the function template specialization, if any; will contain one element "" for f...
Definition TClassEdit.h:281
std::string fScopeName
Name of the scope qualification of the function, possibly empty.
Definition TClassEdit.h:274
std::vector< std::string > fFunctionParameters
Function parameters.
Definition TClassEdit.h:284
std::string fReturnType
Return type of the function, might be empty if the function declaration string did not provide it.
Definition TClassEdit.h:271
TSplitType(const TSplitType &)=delete
bool IsTemplate()
Check if the type is a template.
int IsSTLCont(int testAlloc=0) const
type : type name: vector<list<classA,allocator>,allocator> testAlloc: if true, we test allocator,...
TSplitType(const char *type2split, EModType mode=TClassEdit::kNone)
default constructor
std::vector< std::string > fElements
Definition TClassEdit.h:125
ROOT::ESTLType IsInSTL() const
type : type name: vector<list<classA,allocator>,allocator>[::iterator] result: 0 : not stl container ...
TSplitType & operator=(const TSplitType &)=delete
void ShortType(std::string &answer, int mode)
Return the absolute type of typeDesc into the string answ.