Logo ROOT  
Reference Guide
TClassEdit.cxx
Go to the documentation of this file.
1 // @(#)root/metautils:$Id$
2 /// \file TClassEdit.cxx
3 /// \ingroup Base
4 /// \author Victor Perev
5 /// \author Philippe Canal
6 /// \date 04/10/2003
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cassert>
19 #include <cstring>
20 #include "TClassEdit.h"
21 #include <cctype>
22 #include "Rstrstream.h"
23 #include <set>
24 #include <stack>
25 // for shared_ptr
26 #include <memory>
27 #include "ROOT/RStringView.hxx"
28 #include <algorithm>
29 
30 using namespace std;
31 
32 namespace {
33  static TClassEdit::TInterpreterLookupHelper *gInterpreterHelper = nullptr;
34 
35  template <typename T>
36  struct ShuttingDownSignaler : public T {
37  using T::T;
38 
39  ~ShuttingDownSignaler()
40  {
41  if (gInterpreterHelper)
42  gInterpreterHelper->ShuttingDownSignal();
43  }
44  };
45 }
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Return the length, if any, taken by std:: and any
49 /// potential inline namespace (well compiler detail namespace).
50 
51 static size_t StdLen(const std::string_view name)
52 {
53  size_t len = 0;
54  if (name.compare(0,5,"std::")==0) {
55  len = 5;
56 
57  // TODO: This is likely to induce unwanted autoparsing, those are reduced
58  // by the caching of the result.
59  if (gInterpreterHelper) {
60  for(size_t i = 5; i < name.length(); ++i) {
61  if (name[i] == '<') break;
62  if (name[i] == ':') {
63  bool isInlined;
64  std::string scope(name.data(),i);
65  std::string scoperesult;
66  // We assume that we are called in already serialized code.
67  // Note: should we also cache the negative answers?
68  static ShuttingDownSignaler<std::set<std::string>> gInlined;
69 
70  if (gInlined.find(scope) != gInlined.end()) {
71  len = i;
72  if (i+1<name.length() && name[i+1]==':') {
73  len += 2;
74  }
75  }
76  if (!gInterpreterHelper->ExistingTypeCheck(scope, scoperesult)
77  && gInterpreterHelper->IsDeclaredScope(scope,isInlined)) {
78  if (isInlined) {
79  gInlined.insert(scope);
80  len = i;
81  if (i+1<name.length() && name[i+1]==':') {
82  len += 2;
83  }
84  }
85  }
86  }
87  }
88  }
89  }
90 
91  return len;
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Remove std:: and any potential inline namespace (well compiler detail
96 /// namespace.
97 
98 static void RemoveStd(std::string &name, size_t pos = 0)
99 {
100  size_t len = StdLen({name.data()+pos,name.length()-pos});
101  if (len) {
102  name.erase(pos,len);
103  }
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Remove std:: and any potential inline namespace (well compiler detail
108 /// namespace.
109 
111 {
112  size_t len = StdLen(name);
113  if (len) {
114  name.remove_prefix(len);
115  }
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 
121 {
122  if (0 == strncmp(clName, "complex<", 8)) {
123  const char *clNamePlus8 = clName + 8;
124  if (0 == strcmp("float>", clNamePlus8)) {
125  return EComplexType::kFloat;
126  }
127  if (0 == strcmp("double>", clNamePlus8)) {
128  return EComplexType::kDouble;
129  }
130  if (0 == strcmp("int>", clNamePlus8)) {
131  return EComplexType::kInt;
132  }
133  if (0 == strcmp("long>", clNamePlus8)) {
134  return EComplexType::kLong;
135  }
136  }
137  return EComplexType::kNone;
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
142 {
143  // Already too late to call this->ShuttingDownSignal
144  // the virtual table has already lost (on some platform) the
145  // address of the derived function that we would need to call.
146  // But at least forget about this instance!
147 
148  if (this == gInterpreterHelper)
149  gInterpreterHelper = nullptr;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 
155 {
156  gInterpreterHelper = helper;
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// default constructor
161 
162 TClassEdit::TSplitType::TSplitType(const char *type2split, EModType mode) : fName(type2split), fNestedLocation(0)
163 {
164  TClassEdit::GetSplit(type2split, fElements, fNestedLocation, mode);
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// type : type name: vector<list<classA,allocator>,allocator>[::iterator]
169 /// result: 0 : not stl container and not declared inside an stl container.
170 /// result: code of container that the type or is the scope of the type
171 
173 {
174  if (fElements[0].empty()) return ROOT::kNotSTL;
175  return STLKind(fElements[0]);
176 }
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// type : type name: vector<list<classA,allocator>,allocator>
180 /// testAlloc: if true, we test allocator, if it is not default result is negative
181 /// result: 0 : not stl container
182 /// abs(result): code of container 1=vector,2=list,3=deque,4=map
183 /// 5=multimap,6=set,7=multiset
184 /// positive val: we have a vector or list with default allocator to any depth
185 /// like vector<list<vector<int>>>
186 /// negative val: STL container other than vector or list, or non default allocator
187 /// For example: vector<deque<int>> has answer -1
188 
189 int TClassEdit::TSplitType::IsSTLCont(int testAlloc) const
190 {
191 
192  if (fElements[0].empty()) return 0;
193  int numb = fElements.size();
194  if (!fElements[numb-1].empty() && fElements[numb-1][0]=='*') --numb;
195 
196  if ( fNestedLocation ) {
197  // The type has been defined inside another namespace and/or class
198  // this couldn't possibly be an STL container
199  return 0;
200  }
201 
202  int kind = STLKind(fElements[0]);
203 
204  if (kind==ROOT::kSTLvector || kind==ROOT::kSTLlist || kind==ROOT::kSTLforwardlist) {
205 
206  int nargs = STLArgs(kind);
207  if (testAlloc && (numb-1 > nargs) && !IsDefAlloc(fElements[numb-1].c_str(),fElements[1].c_str())) {
208 
209  // We have a non default allocator,
210  // let's return a negative value.
211 
212  kind = -kind;
213 
214  } else {
215 
216  // We has a default allocator, let's continue to
217  // look inside the argument list.
218  int k = TClassEdit::IsSTLCont(fElements[1].c_str(),testAlloc);
219  if (k<0) kind = -kind;
220 
221  }
222  }
223 
224  // We return a negative value for anything which is not a vector or a list.
225  if(kind>2) kind = - kind;
226  return kind;
227 }
228 
229 ////////////////////////////////////////////////////////////////////////////////
230 //////////////////////////////////////////////////////////////////////////////
231 /// Return the absolute type of typeDesc into the string answ.
232 
233 void TClassEdit::TSplitType::ShortType(std::string &answ, int mode)
234 {
235  // E.g.: typeDesc = "class const volatile TNamed**", returns "TNamed**".
236  // if (mode&1) remove last "*"s returns "TNamed"
237  // if (mode&2) remove default allocators from STL containers
238  // if (mode&4) remove all allocators from STL containers
239  // if (mode&8) return inner class of stl container. list<innerClass>
240  // if (mode&16) return deepest class of stl container. vector<list<deepest>>
241  // if (mode&kDropAllDefault) remove default template arguments
242  /////////////////////////////////////////////////////////////////////////////
243 
244  answ.clear();
245  int narg = fElements.size();
246  int tailLoc = 0;
247 
248  if (narg == 0) {
249  answ = fName;
250  return ;
251  }
252  // fprintf(stderr,"calling ShortType %d for %s with narg %d\n",mode,typeDesc,narg);
253  // {for (int i=0;i<narg;i++) fprintf(stderr,"calling ShortType %d for %s with %d %s \n",
254  // mode,typeDesc,i,arglist[i].c_str());
255  // }
256  if (fElements[narg-1].empty() == false &&
257  (fElements[narg-1][0]=='*'
258  || fElements[narg-1][0]=='&'
259  || fElements[narg-1][0]=='['
260  || 0 == fElements[narg-1].compare(0,6,"const*")
261  || 0 == fElements[narg-1].compare(0,6,"const&")
262  || 0 == fElements[narg-1].compare(0,6,"const[")
263  || 0 == fElements[narg-1].compare("const")
264  )
265  ) {
266  if ((mode&1)==0) tailLoc = narg-1;
267  }
268  else { assert(fElements[narg-1].empty()); };
269  narg--;
270  mode &= (~1);
271 
272  if (fNestedLocation) narg--;
273 
274  // fprintf(stderr,"calling ShortType %d for %s with narg %d tail %d\n",imode,typeDesc,narg,tailLoc);
275 
276  //kind of stl container
277  const int kind = STLKind(fElements[0]);
278  const int iall = STLArgs(kind);
279 
280  // Only class is needed
281  if (mode&(8|16)) {
282  while(narg-1>iall) { fElements.pop_back(); narg--;}
283  if (!fElements[0].empty() && tailLoc) {
284  tailLoc = 0;
285  }
286  fElements[0].clear();
287  mode&=(~8);
288  }
289 
290  if (mode & kDropAllDefault) mode |= kDropStlDefault;
291  if (mode & kDropStlDefault) mode |= kDropDefaultAlloc;
292 
293  if (kind) {
294  bool allocRemoved = false;
295 
296  if ( mode & (kDropDefaultAlloc|kDropAlloc) ) {
297  // remove allocators
298 
299 
300  if (narg-1 == iall+1) {
301  // has an allocator specified
302  bool dropAlloc = false;
303  if (mode & kDropAlloc) {
304 
305  dropAlloc = true;
306 
307  } else if (mode & kDropDefaultAlloc) {
308  switch (kind) {
309  case ROOT::kSTLvector:
310  case ROOT::kSTLlist:
312  case ROOT::kSTLdeque:
313  case ROOT::kSTLset:
314  case ROOT::kSTLmultiset:
317  dropAlloc = IsDefAlloc(fElements[iall+1].c_str(),fElements[1].c_str());
318  break;
319  case ROOT::kSTLmap:
320  case ROOT::kSTLmultimap:
323  dropAlloc = IsDefAlloc(fElements[iall+1].c_str(),fElements[1].c_str(),fElements[2].c_str());
324  break;
325  default:
326  dropAlloc = false;
327  }
328 
329  }
330  if (dropAlloc) {
331  narg--;
332  allocRemoved = true;
333  }
334  } else {
335  // has no allocator specified (hence it is already removed!)
336  allocRemoved = true;
337  }
338  }
339 
340  if ( allocRemoved && (mode & kDropStlDefault) && narg-1 == iall) { // remove default comparator
341  if ( IsDefComp( fElements[iall].c_str(), fElements[1].c_str() ) ) {
342  narg--;
343  }
344  } else if ( mode & kDropComparator ) {
345 
346  switch (kind) {
347  case ROOT::kSTLvector:
348  case ROOT::kSTLlist:
350  case ROOT::kSTLdeque:
351  break;
352  case ROOT::kSTLset:
353  case ROOT::kSTLmultiset:
354  case ROOT::kSTLmap:
355  case ROOT::kSTLmultimap:
356  if (!allocRemoved && narg-1 == iall+1) {
357  narg--;
358  allocRemoved = true;
359  }
360  if (narg-1 == iall) narg--;
361  break;
362  default:
363  break;
364  }
365  }
366 
367  // Treat now Pred and Hash for unordered set/map containers. Signature is:
368  // template < class Key,
369  // class Hash = hash<Key>,
370  // class Pred = equal_to<Key>,
371  // class Alloc = allocator<Key>
372  // > class unordered_{set,multiset}
373  // template < class Key,
374  // class Val,
375  // class Hash = hash<Key>,
376  // class Pred = equal_to<Key>,
377  // class Alloc = allocator<Key>
378  // > class unordered_{map,multimap}
379 
380 
382 
383  bool predRemoved = false;
384 
385  if ( allocRemoved && (mode & kDropStlDefault) && narg-1 == iall) { // remove default predicate
386  if ( IsDefPred( fElements[iall].c_str(), fElements[1].c_str() ) ) {
387  predRemoved=true;
388  narg--;
389  }
390  }
391 
392  if ( predRemoved && (mode & kDropStlDefault) && narg == iall) { // remove default hash
393  if ( IsDefHash( fElements[iall-1].c_str(), fElements[1].c_str() ) ) {
394  narg--;
395  }
396  }
397  }
398  } // End of treatment of stl containers
399  else {
400  if ( (mode & kDropStlDefault) && (narg >= 3)) {
401  unsigned int offset = (0==strncmp("const ",fElements[0].c_str(),6)) ? 6 : 0;
402  offset += (0==strncmp("std::",fElements[0].c_str()+offset,5)) ? 5 : 0;
403  if (0 == strcmp(fElements[0].c_str()+offset,"__shared_ptr"))
404  {
405 #ifdef _CONCURRENCE_H
406  static const std::string sharedPtrDef = std::to_string(__gnu_cxx::__default_lock_policy); // to_string is C++11
407 #else
408  static const std::string sharedPtrDef = std::to_string(2); // to_string is C++11
409 #endif
410  if (fElements[2] == sharedPtrDef) {
411  narg--;
412  }
413  }
414  }
415  }
416 
417  // do the same for all inside
418  for (int i=1;i<narg; i++) {
419  if (strchr(fElements[i].c_str(),'<')==0) {
420  if (mode&kDropStd) {
421  unsigned int offset = (0==strncmp("const ",fElements[i].c_str(),6)) ? 6 : 0;
422  RemoveStd( fElements[i], offset );
423  }
424  if (mode&kResolveTypedef) {
425  fElements[i] = ResolveTypedef(fElements[i].c_str(),true);
426  }
427  continue;
428  }
429  fElements[i] = TClassEdit::ShortType(fElements[i].c_str(),mode | TClassEdit::kKeepOuterConst);
430  if (mode&kResolveTypedef) {
431  // We 'just' need to check whether the outer type is a typedef or not;
432  // this also will add the default template parameter if any needs to
433  // be added.
434  string typeresult;
435  if (gInterpreterHelper &&
436  (gInterpreterHelper->ExistingTypeCheck(fElements[i], typeresult)
437  || gInterpreterHelper->GetPartiallyDesugaredNameWithScopeHandling(fElements[i], typeresult))) {
438  if (!typeresult.empty()) fElements[i] = typeresult;
439  }
440  }
441  }
442 
443  unsigned int tailOffset = 0;
444  if (tailLoc && fElements[tailLoc].compare(0,5,"const") == 0) {
445  if (mode & kKeepOuterConst) answ += "const ";
446  tailOffset = 5;
447  }
448  if (!fElements[0].empty()) {answ += fElements[0]; answ +="<";}
449 
450 #if 0
451  // This code is no longer use, the moral equivalent would be to get
452  // the 'fixed' number of argument the user told us to ignore and drop those.
453  // However, the name we get here might be (usually) normalized enough that
454  // this is not necessary (at the very least nothing break in roottest without
455  // the aforementioned new code or this old code).
456  if (mode & kDropAllDefault) {
457  int nargNonDefault = 0;
458  std::string nonDefName = answ;
459  // "superlong" because tLong might turn fName into an even longer name
460  std::string nameSuperLong = fName;
461  if (gInterpreterHelper)
462  gInterpreterHelper->GetPartiallyDesugaredName(nameSuperLong);
463  while (++nargNonDefault < narg) {
464  // If T<a> is a "typedef" (aka default template params)
465  // to T<a,b> then we can strip the "b".
466  const char* closeTemplate = " >";
467  if (nonDefName[nonDefName.length() - 1] != '>')
468  ++closeTemplate;
469  string nondef = nonDefName + closeTemplate;
470  if (gInterpreterHelper &&
471  gInterpreterHelper->IsAlreadyPartiallyDesugaredName(nondef, nameSuperLong))
472  break;
473  if (nargNonDefault>1) nonDefName += ",";
474  nonDefName += fElements[nargNonDefault];
475  }
476  if (nargNonDefault < narg)
477  narg = nargNonDefault;
478  }
479 #endif
480 
481  { for (int i=1;i<narg-1; i++) { answ += fElements[i]; answ+=",";} }
482  if (narg>1) { answ += fElements[narg-1]; }
483 
484  if (!fElements[0].empty()) {
485  if ( answ.at(answ.size()-1) == '>') {
486  answ += " >";
487  } else {
488  answ += '>';
489  }
490  }
491  if (fNestedLocation) {
492  // Treat X pf A<B>::X
493  fElements[fNestedLocation] = TClassEdit::ShortType(fElements[fNestedLocation].c_str(),mode);
494  answ += fElements[fNestedLocation];
495  }
496  // tail is not a type name, just [2], &, * etc.
497  if (tailLoc) answ += fElements[tailLoc].c_str()+tailOffset;
498 }
499 
500 ////////////////////////////////////////////////////////////////////////////////
501 /// Check if the type is a template
503 {
504  return !fElements[0].empty();
505 }
506 
507 ////////////////////////////////////////////////////////////////////////////////
508 /// Converts STL container name to number. vector -> 1, etc..
509 /// If len is greater than 0, only look at that many characters in the string.
510 
512 {
513  size_t offset = 0;
514  if (type.compare(0,6,"const ")==0) { offset += 6; }
515  offset += StdLen(type.substr(offset));
516 
517  //container names
518  static const char *stls[] =
519  { "any", "vector", "list", "deque", "map", "multimap", "set", "multiset", "bitset",
520  "forward_list", "unordered_set", "unordered_multiset", "unordered_map", "unordered_multimap", 0};
521  static const size_t stllen[] =
522  { 3, 6, 4, 5, 3, 8, 3, 8, 6,
523  12, 13, 18, 13, 18, 0};
524  static const ROOT::ESTLType values[] =
530  // New C++11
535  };
536 
537  // kind of stl container
538  auto len = type.length();
539  if (len) {
540  len -= offset;
541  for(int k=1;stls[k];k++) {
542  if (len == stllen[k]) {
543  if (type.compare(offset,len,stls[k])==0) return values[k];
544  }
545  }
546  } else {
547  for(int k=1;stls[k];k++) {if (type.compare(offset,len,stls[k])==0) return values[k];}
548  }
549  return ROOT::kNotSTL;
550 }
551 
552 ////////////////////////////////////////////////////////////////////////////////
553 /// Return number of arguments for STL container before allocator
554 
555 int TClassEdit::STLArgs(int kind)
556 {
557  static const char stln[] =// min number of container arguments
558  // vector, list, deque, map, multimap, set, multiset, bitset,
559  { 1, 1, 1, 1, 3, 3, 2, 2, 1,
560  // forward_list, unordered_set, unordered_multiset, unordered_map, unordered_multimap
561  1, 3, 3, 4, 4};
562 
563  return stln[kind];
564 }
565 
566 ////////////////////////////////////////////////////////////////////////////////
567 
568 static size_t findNameEnd(const std::string_view full)
569 {
570  int level = 0;
571  for(size_t i = 0; i < full.length(); ++i) {
572  switch(full[i]) {
573  case '<': { ++level; break; }
574  case '>': {
575  if (level == 0) return i;
576  else --level;
577  break;
578  }
579  case ',': {
580  if (level == 0) return i;
581  break;
582  }
583  default: break;
584  }
585  }
586  return full.length();
587 }
588 
589 ////////////////////////////////////////////////////////////////////////////////
590 
591 static size_t findNameEnd(const std::string &full, size_t pos)
592 {
593  return pos + findNameEnd( {full.data()+pos,full.length()-pos} );
594 }
595 
596 ////////////////////////////////////////////////////////////////////////////////
597 /// return whether or not 'allocname' is the STL default allocator for type
598 /// 'classname'
599 
600 bool TClassEdit::IsDefAlloc(const char *allocname, const char *classname)
601 {
602  string_view a( allocname );
603  RemoveStd(a);
604 
605  if (a=="alloc") return true;
606  if (a=="__default_alloc_template<true,0>") return true;
607  if (a=="__malloc_alloc_template<0>") return true;
608 
609  const static int alloclen = strlen("allocator<");
610  if (a.compare(0,alloclen,"allocator<") != 0) {
611  return false;
612  }
613  a.remove_prefix(alloclen);
614 
615  RemoveStd(a);
616 
617  string_view k = classname;
618  RemoveStd(k);
619 
620  if (a.compare(0,k.length(),k) != 0) {
621  // Now we need to compare the normalized name.
622  size_t end = findNameEnd(a);
623 
624  std::string valuepart;
625  GetNormalizedName(valuepart,std::string_view(a.data(),end));
626 
627  std::string norm_value;
628  GetNormalizedName(norm_value,k);
629 
630  if (valuepart != norm_value) {
631  return false;
632  }
633  a.remove_prefix(end);
634  } else {
635  a.remove_prefix(k.length());
636  }
637 
638  if (a.compare(0,1,">")!=0 && a.compare(0,2," >")!=0) {
639  return false;
640  }
641 
642  return true;
643 }
644 
645 ////////////////////////////////////////////////////////////////////////////////
646 /// return whether or not 'allocname' is the STL default allocator for a key
647 /// of type 'keyclassname' and a value of type 'valueclassname'
648 
649 bool TClassEdit::IsDefAlloc(const char *allocname,
650  const char *keyclassname,
651  const char *valueclassname)
652 {
653  if (IsDefAlloc(allocname,keyclassname)) return true;
654 
655  string_view a( allocname );
656  RemoveStd(a);
657 
658  const static int alloclen = strlen("allocator<");
659  if (a.compare(0,alloclen,"allocator<") != 0) {
660  return false;
661  }
662  a.remove_prefix(alloclen);
663 
664  RemoveStd(a);
665 
666  const static int pairlen = strlen("pair<");
667  if (a.compare(0,pairlen,"pair<") != 0) {
668  return false;
669  }
670  a.remove_prefix(pairlen);
671 
672  const static int constlen = strlen("const");
673  if (a.compare(0,constlen+1,"const ") == 0) {
674  a.remove_prefix(constlen+1);
675  }
676 
677  RemoveStd(a);
678 
679  string_view k = keyclassname;
680  RemoveStd(k);
681  if (k.compare(0,constlen+1,"const ") == 0) {
682  k.remove_prefix(constlen+1);
683  }
684 
685  if (a.compare(0,k.length(),k) != 0) {
686  // Now we need to compare the normalized name.
687  size_t end = findNameEnd(a);
688 
689  std::string alloc_keypart;
690  GetNormalizedName(alloc_keypart,std::string_view(a.data(),end));
691 
692  std::string norm_key;
693  GetNormalizedName(norm_key,k);
694 
695  if (alloc_keypart != norm_key) {
696  if ( norm_key[norm_key.length()-1] == '*' ) {
697  // also check with a trailing 'const'.
698  norm_key += "const";
699  } else {
700  norm_key += " const";
701  }
702  if (alloc_keypart != norm_key) {
703  return false;
704  }
705  }
706  a.remove_prefix(end);
707  } else {
708  size_t end = k.length();
709  if ( (a[end-1] == '*') || a[end]==' ' ) {
710  size_t skipSpace = (a[end] == ' ');
711  if (a.compare(end+skipSpace,constlen,"const") == 0) {
712  end += constlen+skipSpace;
713  }
714  }
715  a.remove_prefix(end);
716  }
717 
718  if (a[0] != ',') {
719  return false;
720  }
721  a.remove_prefix(1);
722  RemoveStd(a);
723 
724  string_view v = valueclassname;
725  RemoveStd(v);
726 
727  if (a.compare(0,v.length(),v) != 0) {
728  // Now we need to compare the normalized name.
729  size_t end = findNameEnd(a);
730 
731  std::string valuepart;
732  GetNormalizedName(valuepart,std::string_view(a.data(),end));
733 
734  std::string norm_value;
735  GetNormalizedName(norm_value,v);
736 
737  if (valuepart != norm_value) {
738  return false;
739  }
740  a.remove_prefix(end);
741  } else {
742  a.remove_prefix(v.length());
743  }
744 
745  if (a.compare(0,1,">")!=0 && a.compare(0,2," >")!=0) {
746  return false;
747  }
748 
749  return true;
750 }
751 
752 ////////////////////////////////////////////////////////////////////////////////
753 /// return whether or not 'elementName' is the STL default Element for type
754 /// 'classname'
755 
756 static bool IsDefElement(const char *elementName, const char* defaultElementName, const char *classname)
757 {
758  string c = elementName;
759 
760  size_t pos = StdLen(c);
761 
762  const int elementlen = strlen(defaultElementName);
763  if (c.compare(pos,elementlen,defaultElementName) != 0) {
764  return false;
765  }
766  pos += elementlen;
767 
768  string k = classname;
769  if (c.compare(pos,k.length(),k) != 0) {
770  // Now we need to compare the normalized name.
771  size_t end = findNameEnd(c,pos);
772 
773  std::string keypart;
774  if (pos != end) { // i.e. elementName != "std::less<>", see ROOT-11000.
775  TClassEdit::GetNormalizedName(keypart,std::string_view(c.c_str()+pos,end-pos));
776  }
777 
778  std::string norm_key;
779  TClassEdit::GetNormalizedName(norm_key,k);
780 
781  if (keypart != norm_key) {
782  return false;
783  }
784  pos = end;
785  } else {
786  pos += k.length();
787  }
788 
789  if (c.compare(pos,1,">")!=0 && c.compare(pos,2," >")!=0) {
790  return false;
791  }
792 
793  return true;
794 }
795 
796 ////////////////////////////////////////////////////////////////////////////////
797 /// return whether or not 'compare' is the STL default comparator for type
798 /// 'classname'
799 
800 bool TClassEdit::IsDefComp(const char *compname, const char *classname)
801 {
802  return IsDefElement(compname, "less<", classname);
803 }
804 
805 ////////////////////////////////////////////////////////////////////////////////
806 /// return whether or not 'predname' is the STL default predicate for type
807 /// 'classname'
808 
809 bool TClassEdit::IsDefPred(const char *predname, const char *classname)
810 {
811  return IsDefElement(predname, "equal_to<", classname);
812 }
813 
814 ////////////////////////////////////////////////////////////////////////////////
815 /// return whether or not 'hashname' is the STL default hash for type
816 /// 'classname'
817 
818 bool TClassEdit::IsDefHash(const char *hashname, const char *classname)
819 {
820  return IsDefElement(hashname, "hash<", classname);
821 }
822 
823 ////////////////////////////////////////////////////////////////////////////////
824 /// Return the normalized name. See TMetaUtils::GetNormalizedName.
825 ///
826 /// Return the type name normalized for ROOT,
827 /// keeping only the ROOT opaque typedef (Double32_t, etc.) and
828 /// removing the STL collections default parameter if any.
829 ///
830 /// Compare to TMetaUtils::GetNormalizedName, this routines does not
831 /// and can not add default template parameters.
832 
834 {
835  if (name.empty()) {
836  norm_name.clear();
837  return;
838  }
839 
840  norm_name = std::string(name); // NOTE: Is that the shortest version?
841  // Remove the std:: and default template argument and insert the Long64_t and change basic_string to string.
844 
845  if (splitname.fElements.size() == 3 && (splitname.fElements[0] == "std::pair" || splitname.fElements[0] == "pair")) {
846  // We don't want to lookup the std::pair itself.
847  std::string first, second;
848  GetNormalizedName(first, splitname.fElements[1]);
849  GetNormalizedName(second, splitname.fElements[2]);
850  norm_name = "pair<" + first + "," + second;
851  if (!second.empty() && second.back() == '>')
852  norm_name += " >";
853  else
854  norm_name += ">";
855  return;
856  }
857 
858  // Depending on how the user typed their code, in particular typedef
859  // declarations, we may end up with an explicit '::' being
860  // part of the result string. For consistency, we must remove it.
861  if (norm_name.length()>2 && norm_name[0]==':' && norm_name[1]==':') {
862  norm_name.erase(0,2);
863  }
864 
865  if (gInterpreterHelper) {
866  // See if the expanded name itself is a typedef.
867  std::string typeresult;
868  if (gInterpreterHelper->ExistingTypeCheck(norm_name, typeresult)
869  || gInterpreterHelper->GetPartiallyDesugaredNameWithScopeHandling(norm_name, typeresult)) {
870 
871  if (!typeresult.empty()) norm_name = typeresult;
872  }
873  }
874 }
875 
876 ////////////////////////////////////////////////////////////////////////////////
877 /// Replace 'long long' and 'unsigned long long' by 'Long64_t' and 'ULong64_t'
878 
879 string TClassEdit::GetLong64_Name(const char* original)
880 {
881  if (original==0)
882  return "";
883  else
884  return GetLong64_Name(string(original));
885 }
886 
887 ////////////////////////////////////////////////////////////////////////////////
888 /// Replace 'long long' and 'unsigned long long' by 'Long64_t' and 'ULong64_t'
889 
890 string TClassEdit::GetLong64_Name(const string& original)
891 {
892  static const char* longlong_s = "long long";
893  static const char* ulonglong_s = "unsigned long long";
894  static const unsigned int longlong_len = strlen(longlong_s);
895  static const unsigned int ulonglong_len = strlen(ulonglong_s);
896 
897  string result = original;
898 
899  int pos = 0;
900  while( (pos = result.find(ulonglong_s,pos) ) >=0 ) {
901  result.replace(pos, ulonglong_len, "ULong64_t");
902  }
903  pos = 0;
904  while( (pos = result.find(longlong_s,pos) ) >=0 ) {
905  result.replace(pos, longlong_len, "Long64_t");
906  }
907  return result;
908 }
909 
910 ////////////////////////////////////////////////////////////////////////////////
911 /// Return the start of the unqualified name include in 'original'.
912 
913 const char *TClassEdit::GetUnqualifiedName(const char *original)
914 {
915  const char *lastPos = original;
916  {
917  long depth = 0;
918  for(auto cursor = original; *cursor != '\0'; ++cursor) {
919  if ( *cursor == '<' || *cursor == '(') ++depth;
920  else if ( *cursor == '>' || *cursor == ')' ) --depth;
921  else if ( *cursor == ':' ) {
922  if (depth==0 && *(cursor+1) == ':' && *(cursor+2) != '\0') {
923  lastPos = cursor+2;
924  }
925  }
926  }
927  }
928  return lastPos;
929 }
930 
931 ////////////////////////////////////////////////////////////////////////////////
932 
933 static void R__FindTrailing(std::string &full, /*modified*/
934  std::string &stars /* the literal output */
935  )
936 {
937  const char *t = full.c_str();
938  const unsigned int tlen( full.size() );
939 
940  const char *starloc = t + tlen - 1;
941  bool hasconst = false;
942  if ( (*starloc)=='t'
943  && (starloc-t) > 4 && 0 == strncmp((starloc-4),"const",5)
944  && ( (*(starloc-5)) == ' ' || (*(starloc-5)) == '*' || (*(starloc-5)) == '&'
945  || (*(starloc-5)) == '>' || (*(starloc-5)) == ']') ) {
946  // we are ending on a const.
947  starloc -= 4;
948  if ((*starloc-1)==' ') {
949  // Take the space too.
950  starloc--;
951  }
952  hasconst = true;
953  }
954  if ( hasconst || (*starloc)=='*' || (*starloc)=='&' || (*starloc)==']' ) {
955  bool isArray = ( (*starloc)==']' );
956  while( t<=(starloc-1) && ((*(starloc-1))=='*' || (*(starloc-1))=='&' || (*(starloc-1))=='t' || isArray)) {
957  if (isArray) {
958  starloc--;
959  isArray = ! ( (*starloc)=='[' );
960  } else if ( (*(starloc-1))=='t' ) {
961  if ( (starloc-1-t) > 5 && 0 == strncmp((starloc-5),"const",5)
962  && ( (*(starloc-6)) == ' ' || (*(starloc-6)) == '*' || (*(starloc-6)) == '&'
963  || (*(starloc-6)) == '>' || (*(starloc-6)) == ']')) {
964  // we have a const.
965  starloc -= 5;
966  } else {
967  break;
968  }
969  } else {
970  starloc--;
971  }
972  }
973  stars = starloc;
974  if ((*(starloc-1))==' ') {
975  // erase the space too.
976  starloc--;
977  }
978 
979  const unsigned int starlen = strlen(starloc);
980  full.erase(tlen-starlen,starlen);
981  } else if (hasconst) {
982  stars = starloc;
983  const unsigned int starlen = strlen(starloc);
984  full.erase(tlen-starlen,starlen);
985  }
986 
987 }
988 
989 ////////////////////////////////////////////////////////////////////////////////
990 ////////////////////////////////////////////////////////////////////////////
991 /// Stores in output (after emptying it) the split type.
992 /// Stores the location of the tail (nested names) in nestedLoc (0 indicates no tail).
993 /// Return the number of elements stored.
994 ///
995 /// First in list is the template name or is empty
996 /// "vector<list<int>,alloc>**" to "vector" "list<int>" "alloc" "**"
997 /// or "TNamed*" to "" "TNamed" "*"
998 ////////////////////////////////////////////////////////////////////////////
999 
1000 int TClassEdit::GetSplit(const char *type, vector<string>& output, int &nestedLoc, EModType mode)
1001 {
1002  nestedLoc = 0;
1003  output.clear();
1004  if (strlen(type)==0) return 0;
1005 
1006  int cleantypeMode = 1 /* keepInnerConst */;
1007  if (mode & kKeepOuterConst) {
1008  cleantypeMode = 0; /* remove only the outer class keyword */
1009  }
1010  string full( mode & kLong64 ? TClassEdit::GetLong64_Name( CleanType(type, cleantypeMode) )
1011  : CleanType(type, cleantypeMode) );
1012 
1013  // We need to replace basic_string with string.
1014  {
1015  unsigned int const_offset = (0==strncmp("const ",full.c_str(),6)) ? 6 : 0;
1016  bool isString = false;
1017  bool isStdString = false;
1018  size_t std_offset = const_offset;
1019  static const char* basic_string_std = "std::basic_string<char";
1020  static const unsigned int basic_string_std_len = strlen(basic_string_std);
1021 
1022  if (full.compare(const_offset,basic_string_std_len,basic_string_std) == 0
1023  && full.size() > basic_string_std_len) {
1024  isString = true;
1025  isStdString = true;
1026  std_offset += 5;
1027  } else if (full.compare(const_offset,basic_string_std_len-5,basic_string_std+5) == 0
1028  && full.size() > (basic_string_std_len-5)) {
1029  // no std.
1030  isString = true;
1031  } else if (full.find("basic_string") != std::string::npos) {
1032  size_t len = StdLen(full.c_str() + const_offset);
1033  if (len && len != 5 && full.compare(const_offset + len, basic_string_std_len-5, basic_string_std+5) == 0) {
1034  isString = true;
1035  isStdString = true;
1036  std_offset += len;
1037  }
1038  }
1039  if (isString) {
1040  size_t offset = basic_string_std_len - 5;
1041  offset += std_offset; // std_offset includs both the size of std prefix and const prefix.
1042  if ( full[offset] == '>' ) {
1043  // done.
1044  } else if (full[offset] == ',') {
1045  ++offset;
1046  if (full.compare(offset, 5, "std::") == 0) {
1047  offset += 5;
1048  }
1049  static const char* char_traits_s = "char_traits<char>";
1050  static const unsigned int char_traits_len = strlen(char_traits_s);
1051  if (full.compare(offset, char_traits_len, char_traits_s) == 0) {
1052  offset += char_traits_len;
1053  if ( full[offset] == '>') {
1054  // done.
1055  } else if (full[offset] == ' ' && full[offset+1] == '>') {
1056  ++offset;
1057  // done.
1058  } else if (full[offset] == ',') {
1059  ++offset;
1060  if (full.compare(offset, 5, "std::") == 0) {
1061  offset += 5;
1062  }
1063  static const char* allocator_s = "allocator<char>";
1064  static const unsigned int allocator_len = strlen(allocator_s);
1065  if (full.compare(offset, allocator_len, allocator_s) == 0) {
1066  offset += allocator_len;
1067  if ( full[offset] == '>') {
1068  // done.
1069  } else if (full[offset] == ' ' && full[offset+1] == '>') {
1070  ++offset;
1071  // done.
1072  } else {
1073  // Not std::string
1074  isString = false;
1075  }
1076  }
1077  } else {
1078  // Not std::string
1079  isString = false;
1080  }
1081  } else {
1082  // Not std::string.
1083  isString = false;
1084  }
1085  } else {
1086  // Not std::string.
1087  isString = false;
1088  }
1089  if (isString) {
1090  output.push_back(string());
1091  if (const_offset && (mode & kKeepOuterConst)) {
1092  if (isStdString && !(mode & kDropStd)) {
1093  output.push_back("const std::string");
1094  } else {
1095  output.push_back("const string");
1096  }
1097  } else {
1098  if (isStdString && !(mode & kDropStd)) {
1099  output.push_back("std::string");
1100  } else {
1101  output.push_back("string");
1102  }
1103  }
1104  if (offset < full.length()) {
1105  // Copy the trailing text.
1106  // keep the '>' inside right for R__FindTrailing to work
1107  string right( full.substr(offset) );
1108  string stars;
1109  R__FindTrailing(right, stars);
1110  output.back().append(right.c_str()+1); // skip the '>'
1111  output.push_back(stars);
1112  } else {
1113  output.push_back("");
1114  }
1115  return output.size();
1116  }
1117  }
1118  }
1119 
1120  if ( mode & kDropStd) {
1121  unsigned int offset = (0==strncmp("const ",full.c_str(),6)) ? 6 : 0;
1122  RemoveStd( full, offset );
1123  }
1124 
1125  string stars;
1126  if ( !full.empty() ) {
1127  R__FindTrailing(full, stars);
1128  }
1129 
1130  const char *c = strchr(full.c_str(),'<');
1131  if (c) {
1132  //we have 'something<'
1133  output.push_back(string(full,0,c - full.c_str()));
1134 
1135  const char *cursor;
1136  int level = 0;
1137  int parenthesis = 0;
1138  for(cursor = c + 1; *cursor != '\0' && !(level==0 && *cursor == '>'); ++cursor) {
1139  if (*cursor == '(') {
1140  ++parenthesis;
1141  continue;
1142  } else if (*cursor == ')') {
1143  --parenthesis;
1144  continue;
1145  }
1146  if (parenthesis)
1147  continue;
1148  switch (*cursor) {
1149  case '<': ++level; break;
1150  case '>': --level; break;
1151  case ',':
1152  if (level == 0) {
1153  output.push_back(std::string(c+1,cursor));
1154  c = cursor;
1155  }
1156  break;
1157  }
1158  }
1159  if (*cursor=='>') {
1160  if (*(cursor-1) == ' ') {
1161  output.push_back(std::string(c+1,cursor-1));
1162  } else {
1163  output.push_back(std::string(c+1,cursor));
1164  }
1165  // See what's next!
1166  if (*(cursor+1)==':') {
1167  // we have a name specified inside the class/namespace
1168  // For now we keep it in one piece
1169  nestedLoc = output.size();
1170  output.push_back((cursor+1));
1171  }
1172  } else if (level >= 0) {
1173  // Unterminated template
1174  output.push_back(std::string(c+1,cursor));
1175  }
1176  } else {
1177  //empty
1178  output.push_back(string());
1179  output.push_back(full);
1180  }
1181 
1182  if (!output.empty()) output.push_back(stars);
1183  return output.size();
1184 }
1185 
1186 
1187 ////////////////////////////////////////////////////////////////////////////////
1188 ////////////////////////////////////////////////////////////////////////////
1189 /// Cleanup type description, redundant blanks removed
1190 /// and redundant tail ignored
1191 /// return *tail = pointer to last used character
1192 /// if (mode==0) keep keywords
1193 /// if (mode==1) remove keywords outside the template params
1194 /// if (mode>=2) remove the keywords everywhere.
1195 /// if (tail!=0) cut before the trailing *
1196 ///
1197 /// The keywords currently are: "const" , "volatile" removed
1198 ///
1199 ///
1200 /// CleanType(" A<B, C< D, E> > *,F,G>") returns "A<B,C<D,E> >*"
1201 ////////////////////////////////////////////////////////////////////////////
1202 
1203 string TClassEdit::CleanType(const char *typeDesc, int mode, const char **tail)
1204 {
1205  static const char* remove[] = {"class","const","volatile",0};
1206  static bool isinit = false;
1207  static std::vector<size_t> lengths;
1208  if (!isinit) {
1209  for (int k=0; remove[k]; ++k) {
1210  lengths.push_back(strlen(remove[k]));
1211  }
1212  isinit = true;
1213  }
1214 
1215  string result;
1216  result.reserve(strlen(typeDesc)*2);
1217  int lev=0,kbl=1;
1218  const char* c;
1219 
1220  for(c=typeDesc;*c;c++) {
1221  if (c[0]==' ') {
1222  if (kbl) continue;
1223  if (!isalnum(c[ 1]) && c[ 1] !='_') continue;
1224  }
1225  if (kbl && (mode>=2 || lev==0)) { //remove "const' etc...
1226  int done = 0;
1227  int n = (mode) ? 999 : 1;
1228 
1229  // loop on all the keywords we want to remove
1230  for (int k=0; k<n && remove[k]; k++) {
1231  int rlen = lengths[k];
1232 
1233  // Do we have a match
1234  if (strncmp(remove[k],c,rlen)) continue;
1235 
1236  // make sure that the 'keyword' is not part of a longer indentifier
1237  if (isalnum(c[rlen]) || c[rlen]=='_' || c[rlen]=='$') continue;
1238 
1239  c+=rlen-1; done = 1; break;
1240  }
1241  if (done) continue;
1242  }
1243 
1244  kbl = (!isalnum(c[ 0]) && c[ 0]!='_' && c[ 0]!='$' && c[0]!='[' && c[0]!=']' && c[0]!='-' && c[0]!='@');
1245  // '@' is special character used only the artifical class name used by ROOT to implement the
1246  // I/O customization rules that requires caching of the input data.
1247 
1248  if (*c == '<' || *c == '(') lev++;
1249  if (lev==0 && !isalnum(*c)) {
1250  if (!strchr("*&:._$ []-@",*c)) break;
1251  // '.' is used as a module/namespace separator by PyROOT, see
1252  // TPyClassGenerator::GetClass.
1253  }
1254  if (c[0]=='>' && result.size() && result[result.size()-1]=='>') result+=" ";
1255 
1256  result += c[0];
1257 
1258  if (*c == '>' || *c == ')') lev--;
1259  }
1260  if(tail) *tail=c;
1261  return result;
1262 }
1263 
1264 ////////////////////////////////////////////////////////////////////////////////
1265 //////////////////////////////////////////////////////////////////////////////
1266 /// Return the absolute type of typeDesc.
1267 /// E.g.: typeDesc = "class const volatile TNamed**", returns "TNamed**".
1268 /// if (mode&1) remove last "*"s returns "TNamed"
1269 /// if (mode&2) remove default allocators from STL containers
1270 /// if (mode&4) remove all allocators from STL containers
1271 /// if (mode&8) return inner class of stl container. list<innerClass>
1272 /// if (mode&16) return deapest class of stl container. vector<list<deapest>>
1273 /// if (mode&kDropAllDefault) remove default template arguments
1274 //////////////////////////////////////////////////////////////////////////////
1275 
1276 string TClassEdit::ShortType(const char *typeDesc, int mode)
1277 {
1278  string answer;
1279 
1280  // get list of all arguments
1281  if (typeDesc) {
1282  TSplitType arglist(typeDesc, (EModType) mode);
1283  arglist.ShortType(answer, mode);
1284  }
1285 
1286  return answer;
1287 }
1288 
1289 ////////////////////////////////////////////////////////////////////////////////
1290 /// Return true if the type is one the interpreter details which are
1291 /// only forward declared (ClassInfo_t etc..)
1292 
1294 {
1295  size_t len = strlen(type);
1296  if (len < 2 || strncmp(type+len-2,"_t",2) != 0) return false;
1297 
1298  unsigned char offset = 0;
1299  if (strncmp(type,"const ",6)==0) { offset += 6; }
1300  static const char *names[] = { "CallFunc_t","ClassInfo_t","BaseClassInfo_t",
1301  "DataMemberInfo_t","FuncTempInfo_t","MethodInfo_t","MethodArgInfo_t",
1302  "TypeInfo_t","TypedefInfo_t",0};
1303 
1304  for(int k=1;names[k];k++) {if (strcmp(type+offset,names[k])==0) return true;}
1305  return false;
1306 }
1307 
1308 ////////////////////////////////////////////////////////////////////////////////
1309 /// Return true is the name is std::bitset<number> or bitset<number>
1310 
1311 bool TClassEdit::IsSTLBitset(const char *classname)
1312 {
1313  size_t offset = StdLen(classname);
1314  if ( strncmp(classname+offset,"bitset<",strlen("bitset<"))==0) return true;
1315  return false;
1316 }
1317 
1318 ////////////////////////////////////////////////////////////////////////////////
1319 /// Return the type of STL collection, if any, that is the underlying type
1320 /// of the given type. Namely return the value of IsSTLCont after stripping
1321 /// pointer, reference and constness from the type.
1322 /// UnderlyingIsSTLCont("vector<int>*") == IsSTLCont("vector<int>")
1323 /// See TClassEdit::IsSTLCont
1324 ///
1325 /// type : type name: vector<list<classA,allocator>,allocator>*
1326 /// result: 0 : not stl container
1327 /// code of container 1=vector,2=list,3=deque,4=map
1328 /// 5=multimap,6=set,7=multiset
1329 
1331 {
1332  if (type.compare(0,6,"const ",6) == 0)
1333  type.remove_prefix(6);
1334 
1335  while(type[type.length()-1]=='*' ||
1336  type[type.length()-1]=='&' ||
1337  type[type.length()-1]==' ') {
1338  type.remove_suffix(1);
1339  }
1340  return IsSTLCont(type);
1341 }
1342 
1343 ////////////////////////////////////////////////////////////////////////////////
1344 /// type : type name: vector<list<classA,allocator>,allocator>
1345 /// result: 0 : not stl container
1346 /// code of container 1=vector,2=list,3=deque,4=map
1347 /// 5=multimap,6=set,7=multiset
1348 
1350 {
1351  auto pos = type.find('<');
1352  if (pos==std::string_view::npos) return ROOT::kNotSTL;
1353 
1354  auto c = pos+1;
1355  for (decltype(type.length()) level = 1; c < type.length(); ++c) {
1356  if (type[c] == '<') ++level;
1357  if (type[c] == '>') --level;
1358  if (level == 0) break;
1359  }
1360  if (c != (type.length()-1) ) {
1361  return ROOT::kNotSTL;
1362  }
1363 
1364  return STLKind(type.substr(0,pos));
1365 }
1366 
1367 ////////////////////////////////////////////////////////////////////////////////
1368 /// type : type name: vector<list<classA,allocator>,allocator>
1369 /// testAlloc: if true, we test allocator, if it is not default result is negative
1370 /// result: 0 : not stl container
1371 /// abs(result): code of container 1=vector,2=list,3=deque,4=map
1372 /// 5=multimap,6=set,7=multiset
1373 /// positive val: we have a vector or list with default allocator to any depth
1374 /// like vector<list<vector<int>>>
1375 /// negative val: STL container other than vector or list, or non default allocator
1376 /// For example: vector<deque<int>> has answer -1
1377 
1378 int TClassEdit::IsSTLCont(const char *type, int testAlloc)
1379 {
1380  if (strchr(type,'<')==0) return 0;
1381 
1382  TSplitType arglist( type );
1383  return arglist.IsSTLCont(testAlloc);
1384 }
1385 
1386 ////////////////////////////////////////////////////////////////////////////////
1387 /// return true if the class belongs to the std namespace
1388 
1389 bool TClassEdit::IsStdClass(const char *classname)
1390 {
1391  classname += StdLen( classname );
1392  if ( strcmp(classname,"string")==0 ) return true;
1393  if ( strncmp(classname,"bitset<",strlen("bitset<"))==0) return true;
1394  if ( strncmp(classname,"pair<",strlen("pair<"))==0) return true;
1395  if ( strcmp(classname,"allocator")==0) return true;
1396  if ( strncmp(classname,"allocator<",strlen("allocator<"))==0) return true;
1397  if ( strncmp(classname,"greater<",strlen("greater<"))==0) return true;
1398  if ( strncmp(classname,"less<",strlen("less<"))==0) return true;
1399  if ( strncmp(classname,"equal_to<",strlen("equal_to<"))==0) return true;
1400  if ( strncmp(classname,"hash<",strlen("hash<"))==0) return true;
1401  if ( strncmp(classname,"auto_ptr<",strlen("auto_ptr<"))==0) return true;
1402 
1403  if ( strncmp(classname,"vector<",strlen("vector<"))==0) return true;
1404  if ( strncmp(classname,"list<",strlen("list<"))==0) return true;
1405  if ( strncmp(classname,"forward_list<",strlen("forward_list<"))==0) return true;
1406  if ( strncmp(classname,"deque<",strlen("deque<"))==0) return true;
1407  if ( strncmp(classname,"map<",strlen("map<"))==0) return true;
1408  if ( strncmp(classname,"multimap<",strlen("multimap<"))==0) return true;
1409  if ( strncmp(classname,"set<",strlen("set<"))==0) return true;
1410  if ( strncmp(classname,"multiset<",strlen("multiset<"))==0) return true;
1411  if ( strncmp(classname,"unordered_set<",strlen("unordered_set<"))==0) return true;
1412  if ( strncmp(classname,"unordered_multiset<",strlen("unordered_multiset<"))==0) return true;
1413  if ( strncmp(classname,"unordered_map<",strlen("unordered_map<"))==0) return true;
1414  if ( strncmp(classname,"unordered_multimap<",strlen("unordered_multimap<"))==0) return true;
1415  if ( strncmp(classname,"bitset<",strlen("bitset<"))==0) return true;
1416 
1417  return false;
1418 }
1419 
1420 
1421 ////////////////////////////////////////////////////////////////////////////////
1422 
1423 bool TClassEdit::IsVectorBool(const char *name) {
1424  TSplitType splitname( name );
1425 
1426  return ( TClassEdit::STLKind( splitname.fElements[0] ) == ROOT::kSTLvector)
1427  && ( splitname.fElements[1] == "bool" || splitname.fElements[1]=="Bool_t");
1428 }
1429 
1430 ////////////////////////////////////////////////////////////////////////////////
1431 
1432 static void ResolveTypedefProcessType(const char *tname,
1433  unsigned int /* len */,
1434  unsigned int cursor,
1435  bool constprefix,
1436  unsigned int start_of_type,
1437  unsigned int end_of_type,
1438  unsigned int mod_start_of_type,
1439  bool &modified,
1440  std::string &result)
1441 {
1442  std::string type(modified && (mod_start_of_type < result.length()) ?
1443  result.substr(mod_start_of_type, string::npos)
1444  : string(tname, start_of_type, end_of_type == 0 ? cursor - start_of_type : end_of_type - start_of_type)); // we need to try to avoid this copy
1445  string typeresult;
1446  if (gInterpreterHelper->ExistingTypeCheck(type, typeresult)
1447  || gInterpreterHelper->GetPartiallyDesugaredNameWithScopeHandling(type, typeresult, false)) {
1448  // it is a known type
1449  if (!typeresult.empty()) {
1450  // and it is a typedef, we need to replace it in the output.
1451  if (modified) {
1452  result.replace(mod_start_of_type, string::npos,
1453  typeresult);
1454  }
1455  else {
1456  modified = true;
1457  mod_start_of_type = start_of_type;
1458  result += string(tname,0,start_of_type);
1459  if (constprefix && typeresult.compare(0,6,"const ",6) == 0) {
1460  result += typeresult.substr(6,string::npos);
1461  } else {
1462  result += typeresult;
1463  }
1464  }
1465  } else if (modified) {
1466  result.replace(mod_start_of_type, string::npos,
1467  type);
1468  }
1469  if (modified) {
1470  if (end_of_type != 0 && end_of_type!=cursor) {
1471  result += std::string(tname,end_of_type,cursor-end_of_type);
1472  }
1473  }
1474  } else {
1475  // no change needed.
1476  if (modified) {
1477  // result += type;
1478  if (end_of_type != 0 && end_of_type!=cursor) {
1479  result += std::string(tname,end_of_type,cursor-end_of_type);
1480  }
1481  }
1482  }
1483 }
1484 
1485 ////////////////////////////////////////////////////////////////////////////////
1486 
1487 static void ResolveTypedefImpl(const char *tname,
1488  unsigned int len,
1489  unsigned int &cursor,
1490  bool &modified,
1491  std::string &result)
1492 {
1493  // Need to parse and deal with
1494  // A::B::C< D, E::F, G::H<I,J>::K::L >::M
1495  // where E might be replace by N<O,P>
1496  // and G::H<I,J>::K or G might be a typedef.
1497 
1498  bool constprefix = false;
1499 
1500  if (tname[cursor]==' ') {
1501  if (!modified) {
1502  modified = true;
1503  result += string(tname,0,cursor);
1504  }
1505  while (tname[cursor]==' ') ++cursor;
1506  }
1507 
1508  if (tname[cursor]=='c' && (cursor+6<len)) {
1509  if (strncmp(tname+cursor,"const ",6) == 0) {
1510  cursor += 6;
1511  if (modified) result += "const ";
1512  }
1513  constprefix = true;
1514 
1515  }
1516 
1517  if (len > 2 && strncmp(tname+cursor,"::",2) == 0) {
1518  cursor += 2;
1519  }
1520 
1521  unsigned int start_of_type = cursor;
1522  unsigned int end_of_type = 0;
1523  unsigned int mod_start_of_type = result.length();
1524  unsigned int prevScope = cursor;
1525  for ( ; cursor<len; ++cursor) {
1526  switch (tname[cursor]) {
1527  case ':': {
1528  if ((cursor+1)>=len || tname[cursor+1]!=':') {
1529  // we expected another ':', malformed, give up.
1530  if (modified) result += (tname+prevScope);
1531  return;
1532  }
1533  string scope;
1534  if (modified) {
1535  scope = result.substr(mod_start_of_type, string::npos);
1536  scope += std::string(tname+prevScope,cursor-prevScope);
1537  } else {
1538  scope = std::string(tname, start_of_type, cursor - start_of_type); // we need to try to avoid this copy
1539  }
1540  std::string scoperesult;
1541  bool isInlined = false;
1542  if (gInterpreterHelper->ExistingTypeCheck(scope, scoperesult)
1543  ||gInterpreterHelper->GetPartiallyDesugaredNameWithScopeHandling(scope, scoperesult)) {
1544  // it is a known type
1545  if (!scoperesult.empty()) {
1546  // and it is a typedef
1547  if (modified) {
1548  if (constprefix && scoperesult.compare(0,6,"const ",6) != 0) mod_start_of_type -= 6;
1549  result.replace(mod_start_of_type, string::npos,
1550  scoperesult);
1551  result += "::";
1552  } else {
1553  modified = true;
1554  mod_start_of_type = start_of_type;
1555  result += string(tname,0,start_of_type);
1556  //if (constprefix) result += "const ";
1557  result += scoperesult;
1558  result += "::";
1559  }
1560  } else if (modified) {
1561  result += std::string(tname+prevScope,cursor+2-prevScope);
1562  }
1563  } else if (!gInterpreterHelper->IsDeclaredScope(scope,isInlined)) {
1564  // the nesting namespace is not declared, just ignore it and move on
1565  if (modified) result += std::string(tname+prevScope,cursor+2-prevScope);
1566  } else if (isInlined) {
1567  // humm ... just skip it.
1568  if (!modified) {
1569  modified = true;
1570  mod_start_of_type = start_of_type;
1571  result += string(tname,0,start_of_type);
1572  //if (constprefix) result += "const ";
1573  result += string(tname,start_of_type,prevScope - start_of_type);
1574  }
1575  } else if (modified) {
1576  result += std::string(tname+prevScope,cursor+2-prevScope);
1577  }
1578  // Consume the 1st semi colon, the 2nd will be consume by the for loop.
1579  ++cursor;
1580  prevScope = cursor+1;
1581  break;
1582  }
1583  case '<': {
1584  // push information on stack
1585  if (modified) {
1586  result += std::string(tname+prevScope,cursor+1-prevScope);
1587  // above includes the '<' .... result += '<';
1588  }
1589  do {
1590  ++cursor;
1591  ResolveTypedefImpl(tname,len,cursor,modified,result);
1592  } while( cursor<len && tname[cursor] == ',' );
1593 
1594  while (cursor<len && tname[cursor+1]==' ') ++cursor;
1595 
1596  // Since we already checked the type, skip the next section
1597  // (respective the scope section and final type processing section)
1598  // as they would re-do the same job.
1599  if (cursor+2<len && tname[cursor+1]==':' && tname[cursor+2]==':') {
1600  if (modified) result += "::";
1601  cursor += 2;
1602  prevScope = cursor+1;
1603  }
1604  if ( (cursor+1)<len && tname[cursor+1] == ',') {
1605  ++cursor;
1606  if (modified) result += ',';
1607  return;
1608  }
1609  if ( (cursor+1)<len && tname[cursor+1] == '>') {
1610  ++cursor;
1611  if (modified) result += " >";
1612  return;
1613  }
1614  if ( (cursor+1) >= len) {
1615  return;
1616  }
1617  if (tname[cursor] != ' ') break;
1618  if (modified) prevScope = cursor+1;
1619  // If the 'current' character is a space we need to treat it,
1620  // since this the next case statement, we can just fall through,
1621  // otherwise we should need to do:
1622  // --cursor; break;
1623  }
1624  case ' ': {
1625  end_of_type = cursor;
1626  // let's see if we have 'long long' or 'unsigned int' or 'signed char' or what not.
1627  while ((cursor+1)<len && tname[cursor+1] == ' ') ++cursor;
1628 
1629  auto next = cursor+1;
1630  if (strncmp(tname+next,"const",5) == 0 && ((next+5)==len || tname[next+5] == ' ' || tname[next+5] == '*' || tname[next+5] == '&' || tname[next+5] == ',' || tname[next+5] == '>' || tname[next+5] == ']'))
1631  {
1632  // A first const after the type needs to be move in the front.
1633  if (!modified) {
1634  modified = true;
1635  result += string(tname,0,start_of_type);
1636  result += "const ";
1637  mod_start_of_type = start_of_type + 6;
1638  result += string(tname,start_of_type,end_of_type-start_of_type);
1639  } else if (mod_start_of_type < result.length()) {
1640  result.insert(mod_start_of_type,"const ");
1641  mod_start_of_type += 6;
1642  } else {
1643  result += "const ";
1644  mod_start_of_type += 6;
1645  result += string(tname,start_of_type,end_of_type-start_of_type);
1646  }
1647  cursor += 5;
1648  end_of_type = cursor+1;
1649  prevScope = end_of_type;
1650  if ((next+5)==len || tname[next+5] == ',' || tname[next+5] == '>' || tname[next+5] == '[') {
1651  break;
1652  }
1653  } else if (next!=len && tname[next] != '*' && tname[next] != '&') {
1654  // the type is not ended yet.
1655  end_of_type = 0;
1656  break;
1657  }
1658  ++cursor;
1659  // Intentional fall through;
1660  }
1661  case '*':
1662  case '&': {
1663  if (tname[cursor] != ' ') end_of_type = cursor;
1664  // check and skip const (followed by *,&, ,) ... what about followed by ':','['?
1665  auto next = cursor+1;
1666  if (strncmp(tname+next,"const",5) == 0) {
1667  if ((next+5)==len || tname[next+5] == ' ' || tname[next+5] == '*' || tname[next+5] == '&' || tname[next+5] == ',' || tname[next+5] == '>' || tname[next+5] == '[') {
1668  next += 5;
1669  }
1670  }
1671  while (next<len &&
1672  (tname[next] == ' ' || tname[next] == '*' || tname[next] == '&')) {
1673  ++next;
1674  // check and skip const (followed by *,&, ,) ... what about followed by ':','['?
1675  if (strncmp(tname+next,"const",5) == 0) {
1676  if ((next+5)==len || tname[next+5] == ' ' || tname[next+5] == '*' || tname[next+5] == '&' || tname[next+5] == ',' || tname[next+5] == '>' || tname[next+5] == '[') {
1677  next += 5;
1678  }
1679  }
1680  }
1681  cursor = next-1;
1682 // if (modified && mod_start_of_type < result.length()) {
1683 // result += string(tname,end_of_type,cursor-end_of_type);
1684 // }
1685  break;
1686  }
1687  case ',': {
1688  if (modified && prevScope) {
1689  result += std::string(tname+prevScope,(end_of_type == 0 ? cursor : end_of_type)-prevScope);
1690  }
1691  ResolveTypedefProcessType(tname,len,cursor,constprefix,start_of_type,end_of_type,mod_start_of_type,
1692  modified, result);
1693  if (modified) result += ',';
1694  return;
1695  }
1696  case '>': {
1697  if (modified && prevScope) {
1698  result += std::string(tname+prevScope,(end_of_type == 0 ? cursor : end_of_type)-prevScope);
1699  }
1700  ResolveTypedefProcessType(tname,len,cursor,constprefix,start_of_type,end_of_type,mod_start_of_type,
1701  modified, result);
1702  if (modified) result += '>';
1703  return;
1704  }
1705  default:
1706  end_of_type = 0;
1707  }
1708  }
1709 
1710  if (prevScope && modified) result += std::string(tname+prevScope,(end_of_type == 0 ? cursor : end_of_type)-prevScope);
1711 
1712  ResolveTypedefProcessType(tname,len,cursor,constprefix,start_of_type,end_of_type,mod_start_of_type,
1713  modified, result);
1714 }
1715 
1716 
1717 ////////////////////////////////////////////////////////////////////////////////
1718 
1719 string TClassEdit::ResolveTypedef(const char *tname, bool /* resolveAll */)
1720 {
1721  // Return the name of type 'tname' with all its typedef components replaced
1722  // by the actual type its points to
1723  // For example for "typedef MyObj MyObjTypedef;"
1724  // vector<MyObjTypedef> return vector<MyObj>
1725  //
1726 
1727  if (tname == 0 || tname[0] == 0)
1728  return "";
1729  if (!gInterpreterHelper)
1730  return tname;
1731 
1732  std::string result;
1733 
1734  // Check if we already know it is a normalized typename or a registered
1735  // typedef (i.e. known to gROOT).
1736  if (gInterpreterHelper->ExistingTypeCheck(tname, result))
1737  {
1738  if (result.empty()) return tname;
1739  else return result;
1740  }
1741 
1742  unsigned int len = strlen(tname);
1743 
1744  unsigned int cursor = 0;
1745  bool modified = false;
1746  ResolveTypedefImpl(tname,len,cursor,modified,result);
1747 
1748  if (!modified) return tname;
1749  else return result;
1750 }
1751 
1752 
1753 ////////////////////////////////////////////////////////////////////////////////
1754 
1755 string TClassEdit::InsertStd(const char *tname)
1756 {
1757  // Return the name of type 'tname' with all STL classes prepended by "std::".
1758  // For example for "vector<set<auto_ptr<int*> > >" it returns
1759  // "std::vector<std::set<std::auto_ptr<int*> > >"
1760  //
1761 
1762  static const char* sSTLtypes[] = {
1763  "allocator",
1764  "auto_ptr",
1765  "bad_alloc",
1766  "bad_cast",
1767  "bad_exception",
1768  "bad_typeid",
1769  "basic_filebuf",
1770  "basic_fstream",
1771  "basic_ifstream",
1772  "basic_ios",
1773  "basic_iostream",
1774  "basic_istream",
1775  "basic_istringstream",
1776  "basic_ofstream",
1777  "basic_ostream",
1778  "basic_ostringstream",
1779  "basic_streambuf",
1780  "basic_string",
1781  "basic_stringbuf",
1782  "basic_stringstream",
1783  "binary_function",
1784  "binary_negate",
1785  "bitset",
1786  "char_traits",
1787  "codecvt_byname",
1788  "codecvt",
1789  "collate",
1790  "collate_byname",
1791  "compare",
1792  "complex",
1793  "ctype_byname",
1794  "ctype",
1795  "deque",
1796  "divides",
1797  "domain_error",
1798  "equal_to",
1799  "exception",
1800  "forward_list",
1801  "fpos",
1802  "greater_equal",
1803  "greater",
1804  "gslice_array",
1805  "gslice",
1806  "hash",
1807  "indirect_array",
1808  "invalid_argument",
1809  "ios_base",
1810  "istream_iterator",
1811  "istreambuf_iterator",
1812  "istrstream",
1813  "iterator_traits",
1814  "iterator",
1815  "length_error",
1816  "less_equal",
1817  "less",
1818  "list",
1819  "locale",
1820  "localedef utility",
1821  "locale utility",
1822  "logic_error",
1823  "logical_and",
1824  "logical_not",
1825  "logical_or",
1826  "map",
1827  "mask_array",
1828  "mem_fun",
1829  "mem_fun_ref",
1830  "messages",
1831  "messages_byname",
1832  "minus",
1833  "modulus",
1834  "money_get",
1835  "money_put",
1836  "moneypunct",
1837  "moneypunct_byname",
1838  "multimap",
1839  "multiplies",
1840  "multiset",
1841  "negate",
1842  "not_equal_to",
1843  "num_get",
1844  "num_put",
1845  "numeric_limits",
1846  "numpunct",
1847  "numpunct_byname",
1848  "ostream_iterator",
1849  "ostreambuf_iterator",
1850  "ostrstream",
1851  "out_of_range",
1852  "overflow_error",
1853  "pair",
1854  "plus",
1855  "pointer_to_binary_function",
1856  "pointer_to_unary_function",
1857  "priority_queue",
1858  "queue",
1859  "range_error",
1860  "raw_storage_iterator",
1861  "reverse_iterator",
1862  "runtime_error",
1863  "set",
1864  "slice_array",
1865  "slice",
1866  "stack",
1867  "string",
1868  "strstream",
1869  "strstreambuf",
1870  "time_get_byname",
1871  "time_get",
1872  "time_put_byname",
1873  "time_put",
1874  "unary_function",
1875  "unary_negate",
1876  "unique_pointer",
1877  "underflow_error",
1878  "unordered_map",
1879  "unordered_multimap",
1880  "unordered_multiset",
1881  "unordered_set",
1882  "valarray",
1883  "vector",
1884  "wstring"
1885  };
1886  static ShuttingDownSignaler<set<string>> sSetSTLtypes;
1887 
1888  if (tname==0 || tname[0]==0) return "";
1889 
1890  if (sSetSTLtypes.empty()) {
1891  // set up static set
1892  const size_t nSTLtypes = sizeof(sSTLtypes) / sizeof(const char*);
1893  for (size_t i = 0; i < nSTLtypes; ++i)
1894  sSetSTLtypes.insert(sSTLtypes[i]);
1895  }
1896 
1897  size_t b = 0;
1898  size_t len = strlen(tname);
1899  string ret;
1900  ret.reserve(len + 20); // expect up to 4 extra "std::" to insert
1901  string id;
1902  while (b < len) {
1903  // find beginning of next identifier
1904  bool precScope = false; // whether the identifier was preceded by "::"
1905  while (!(isalnum(tname[b]) || tname[b] == '_') && b < len) {
1906  precScope = (b < len - 2) && (tname[b] == ':') && (tname[b + 1] == ':');
1907  if (precScope) {
1908  ret += "::";
1909  b += 2;
1910  } else
1911  ret += tname[b++];
1912  }
1913 
1914  // now b is at the beginning of an identifier or len
1915  size_t e = b;
1916  // find end of identifier
1917  id.clear();
1918  while (e < len && (isalnum(tname[e]) || tname[e] == '_'))
1919  id += tname[e++];
1920  if (!id.empty()) {
1921  if (!precScope) {
1922  set<string>::const_iterator iSTLtype = sSetSTLtypes.find(id);
1923  if (iSTLtype != sSetSTLtypes.end())
1924  ret += "std::";
1925  }
1926 
1927  ret += id;
1928  b = e;
1929  }
1930  }
1931  return ret;
1932 }
1933 
1934 ////////////////////////////////////////////////////////////////////////////////
1935 /// An helper class to dismount the name and remount it changed whenever
1936 /// necessary
1937 
1938 class NameCleanerForIO {
1939  std::string fName;
1940  std::vector<std::unique_ptr<NameCleanerForIO>> fArgumentNodes = {};
1941  NameCleanerForIO* fMother;
1942  bool fHasChanged = false;
1943  bool AreAncestorsSTLContOrArray()
1944  {
1945  NameCleanerForIO* mother = fMother;
1946  if (!mother) return false;
1947  bool isSTLContOrArray = true;
1948  while (nullptr != mother){
1949  auto stlType = TClassEdit::IsSTLCont(mother->fName+"<>");
1950  isSTLContOrArray &= ROOT::kNotSTL != stlType || TClassEdit::IsStdArray(mother->fName+"<");
1951  mother = mother->fMother;
1952  }
1953 
1954  return isSTLContOrArray;
1955  }
1956 
1957 public:
1958  NameCleanerForIO(const std::string& clName = "",
1960  NameCleanerForIO* mother = nullptr):fMother(mother)
1961  {
1962  if (clName.back() != '>') {
1963  fName = clName;
1964  return;
1965  }
1966 
1967  std::vector<std::string> v;
1968  int dummy=0;
1969  TClassEdit::GetSplit(clName.c_str(), v, dummy, mode);
1970 
1971  // We could be in presence of templates such as A1<T1>::A2<T2>::A3<T3>
1972  auto argsEnd = v.end();
1973  auto argsBeginPlusOne = ++v.begin();
1974  auto argPos = std::find_if(argsBeginPlusOne, argsEnd,
1975  [](std::string& arg){return (!arg.empty() && arg.front() == ':');});
1976  if (argPos != argsEnd) {
1977  const int lenght = clName.size();
1978  int wedgeBalance = 0;
1979  int lastOpenWedge = 0;
1980  for (int i=lenght-1;i>-1;i--) {
1981  auto& c = clName.at(i);
1982  if (c == '<') {
1983  wedgeBalance++;
1984  lastOpenWedge = i;
1985  } else if (c == '>') {
1986  wedgeBalance--;
1987  } else if (c == ':' && 0 == wedgeBalance) {
1988  // This would be A1<T1>::A2<T2>
1989  auto nameToClean = clName.substr(0,i-1);
1990  NameCleanerForIO node(nameToClean, mode);
1991  auto cleanName = node.ToString();
1992  fHasChanged = node.HasChanged();
1993  // We got A1<T1>::A2<T2> cleaned
1994 
1995  // We build the changed A1<T1>::A2<T2>::A3
1996  cleanName += "::";
1997  // Now we get A3 and append it
1998  cleanName += clName.substr(i+1,lastOpenWedge-i-1);
1999 
2000  // We now get the args of what in our case is A1<T1>::A2<T2>::A3
2001  auto lastTemplate = &clName.data()[i+1];
2002 
2003  // We split it
2004  TClassEdit::GetSplit(lastTemplate, v, dummy, mode);
2005  // We now replace the name of the template
2006  v[0] = cleanName;
2007  break;
2008  }
2009  }
2010  }
2011 
2012  fName = v.front();
2013  unsigned int nargs = v.size() - 2;
2014  for (unsigned int i=0;i<nargs;++i) {
2015  fArgumentNodes.emplace_back(new NameCleanerForIO(v[i+1],mode,this));
2016  }
2017  }
2018 
2019  bool HasChanged() const {return fHasChanged;}
2020 
2021  std::string ToString()
2022  {
2023  std::string name(fName);
2024 
2025  if (fArgumentNodes.empty()) return name;
2026 
2027  // We have in hands a case like unique_ptr< ... >
2028  // Perhaps we could treat atomics as well like this?
2029  if (!fMother && TClassEdit::IsUniquePtr(fName+"<")) {
2030  name = fArgumentNodes.front()->ToString();
2031  // ROOT-9933: we remove const if present.
2032  TClassEdit::TSplitType tst(name.c_str());
2033  tst.ShortType(name, 1);
2034  fHasChanged = true;
2035  return name;
2036  }
2037 
2038  // Now we treat the case of the collections of unique ptrs
2039  auto stlContType = AreAncestorsSTLContOrArray();
2040  if (stlContType != ROOT::kNotSTL && TClassEdit::IsUniquePtr(fName+"<")) {
2041  name = fArgumentNodes.front()->ToString();
2042  name += "*";
2043  fHasChanged = true;
2044  return name;
2045  }
2046 
2047  name += "<";
2048  for (auto& node : fArgumentNodes) {
2049  name += node->ToString() + ",";
2050  fHasChanged |= node->HasChanged();
2051  }
2052  name.pop_back(); // Remove the last comma.
2053  name += name.back() == '>' ? " >" : ">"; // Respect name normalisation
2054  return name;
2055  }
2056 
2057  const std::string& GetName() {return fName;}
2058  const std::vector<std::unique_ptr<NameCleanerForIO>>* GetChildNodes() const {return &fArgumentNodes;}
2059 };
2060 
2061 ////////////////////////////////////////////////////////////////////////////////
2062 
2063 std::string TClassEdit::GetNameForIO(const std::string& templateInstanceName,
2064  TClassEdit::EModType mode,
2065  bool* hasChanged)
2066 {
2067  // Decompose template name into pieces and remount it applying the necessary
2068  // transformations necessary for the ROOT IO subsystem, namely:
2069  // - Transform std::unique_ptr<T> into T (for selections) (also nested)
2070  // - Transform std::unique_ptr<const T> into T (for selections) (also nested)
2071  // - Transform std::COLL<std::unique_ptr<T>> into std::COLL<T*> (also nested)
2072  // Name normalisation is respected (e.g. spaces).
2073  // The implementation uses an internal class defined in the cxx file.
2074  NameCleanerForIO node(templateInstanceName, mode);
2075  auto nameForIO = node.ToString();
2076  if (hasChanged) {
2077  *hasChanged = node.HasChanged();
2078  }
2079  return nameForIO;
2080 }
2081 
2082 ////////////////////////////////////////////////////////////////////////////////
2083 // We could introduce a tuple as return type, but we be consistent with the rest
2084 // of the code.
2085 bool TClassEdit::GetStdArrayProperties(const char* typeName,
2086  std::string& typeNameBuf,
2087  std::array<int, 5>& maxIndices,
2088  int& ndim)
2089 {
2090  if (!IsStdArray(typeName)) return false;
2091 
2092  // We have an array, it's worth continuing
2093  NameCleanerForIO node(typeName);
2094 
2095  // We now recurse updating the data according to what we find
2096  auto childNodes = node.GetChildNodes();
2097  for (ndim = 1;ndim <=5 ; ndim++) {
2098  maxIndices[ndim-1] = std::atoi(childNodes->back()->GetName().c_str());
2099  auto& frontNode = childNodes->front();
2100  typeNameBuf = frontNode->GetName();
2101  if (! IsStdArray(typeNameBuf+"<")) {
2102  typeNameBuf = frontNode->ToString();
2103  return true;
2104  }
2105  childNodes = frontNode->GetChildNodes();
2106  }
2107 
2108  return true;
2109 }
2110 
2111 
2112 ////////////////////////////////////////////////////////////////////////////////
2113 /// Demangle in a portable way the type id name.
2114 /// IMPORTANT: The caller is responsible for freeing the returned const char*
2115 
2116 char* TClassEdit::DemangleTypeIdName(const std::type_info& ti, int& errorCode)
2117 {
2118  const char* mangled_name = ti.name();
2119  return DemangleName(mangled_name, errorCode);
2120 }
2121 /*
2122 /// Result of splitting a function declaration into
2123 /// fReturnType fScopeName::fFunctionName<fFunctionTemplateArguments>(fFunctionParameters)
2124 struct FunctionSplitInfo {
2125  /// Return type of the function, might be empty if the function declaration string did not provide it.
2126  std::string fReturnType;
2127 
2128  /// Name of the scope qualification of the function, possibly empty
2129  std::string fScopeName;
2130 
2131  /// Name of the function
2132  std::string fFunctionName;
2133 
2134  /// Template arguments of the function template specialization, if any; will contain one element "" for
2135  /// `function<>()`
2136  std::vector<std::string> fFunctionTemplateArguments;
2137 
2138  /// Function parameters.
2139  std::vector<std::string> fFunctionParameters;
2140 };
2141 */
2142 
2143 namespace {
2144  /// Find the first occurrence of any of needle's characters in haystack that
2145  /// is not nested in a <>, () or [] pair.
2146  std::size_t FindNonNestedNeedles(std::string_view haystack, string_view needles)
2147  {
2148  std::stack<char> expected;
2149  for (std::size_t pos = 0, end = haystack.length(); pos < end; ++pos) {
2150  char c = haystack[pos];
2151  if (expected.empty()) {
2152  if (needles.find(c) != std::string_view::npos)
2153  return pos;
2154  } else {
2155  if (c == expected.top()) {
2156  expected.pop();
2157  continue;
2158  }
2159  }
2160  switch (c) {
2161  case '<': expected.emplace('>'); break;
2162  case '(': expected.emplace(')'); break;
2163  case '[': expected.emplace(']'); break;
2164  }
2165  }
2166  return std::string_view::npos;
2167  }
2168 
2169  /// Find the first occurrence of `::` that is not nested in a <>, () or [] pair.
2170  std::size_t FindNonNestedDoubleColons(std::string_view haystack)
2171  {
2172  std::size_t lenHaystack = haystack.length();
2173  std::size_t prevAfterColumn = 0;
2174  while (true) {
2175  std::size_t posColumn = FindNonNestedNeedles(haystack.substr(prevAfterColumn), ":");
2176  if (posColumn == std::string_view::npos)
2177  return std::string_view::npos;
2178  prevAfterColumn += posColumn;
2179  // prevAfterColumn must have "::", i.e. two characters:
2180  if (prevAfterColumn + 1 >= lenHaystack)
2181  return std::string_view::npos;
2182 
2183  ++prevAfterColumn; // done with first (or only) ':'
2184  if (haystack[prevAfterColumn] == ':')
2185  return prevAfterColumn - 1;
2186  ++prevAfterColumn; // That was not a ':'.
2187  }
2188 
2189  return std::string_view::npos;
2190  }
2191 
2192  std::string_view StripSurroundingSpace(std::string_view str)
2193  {
2194  while (!str.empty() && std::isspace(str[0]))
2195  str.remove_prefix(1);
2196  while (!str.empty() && std::isspace(str.back()))
2197  str.remove_suffix(1);
2198  return str;
2199  }
2200 
2201  std::string ToString(std::string_view sv)
2202  {
2203  // ROOT's string_view backport doesn't add the new std::string contructor and assignment;
2204  // convert to std::string instead and assign that.
2205  return std::string(sv.data(), sv.length());
2206  }
2207 } // unnamed namespace
2208 
2209 /// Split a function declaration into its different parts.
2211 {
2212  // General structure:
2213  // `...` last-space `...` (`...`)
2214  // The first `...` is the return type.
2215  // The second `...` is the (possibly scoped) function name.
2216  // The third `...` are the parameters.
2217  // The function name can be of the form `...`<`...`>
2218  std::size_t posArgs = FindNonNestedNeedles(decl, "(");
2219  std::string_view declNoArgs = decl.substr(0, posArgs);
2220 
2221  std::size_t prevAfterWhiteSpace = 0;
2222  static const char whitespace[] = " \t\n";
2223  while (declNoArgs.length() > prevAfterWhiteSpace) {
2224  std::size_t posWS = FindNonNestedNeedles(declNoArgs.substr(prevAfterWhiteSpace), whitespace);
2225  if (posWS == std::string_view::npos)
2226  break;
2227  prevAfterWhiteSpace += posWS + 1;
2228  while (declNoArgs.length() > prevAfterWhiteSpace
2229  && strchr(whitespace, declNoArgs[prevAfterWhiteSpace]))
2230  ++prevAfterWhiteSpace;
2231  }
2232 
2233  /// Include any '&*' in the return type:
2234  std::size_t endReturn = prevAfterWhiteSpace;
2235  while (declNoArgs.length() > endReturn
2236  && strchr("&* \t \n", declNoArgs[endReturn]))
2237  ++endReturn;
2238 
2239  result.fReturnType = ToString(StripSurroundingSpace(declNoArgs.substr(0, endReturn)));
2240 
2241  /// scope::anotherscope::functionName<tmplt>:
2242  std::string_view scopeFunctionTmplt = declNoArgs.substr(endReturn);
2243  std::size_t prevAtScope = FindNonNestedDoubleColons(scopeFunctionTmplt);
2244  while (prevAtScope != std::string_view::npos
2245  && scopeFunctionTmplt.length() > prevAtScope + 2) {
2246  std::size_t posScope = FindNonNestedDoubleColons(scopeFunctionTmplt.substr(prevAtScope + 2));
2247  if (posScope == std::string_view::npos)
2248  break;
2249  prevAtScope += posScope + 2;
2250  }
2251 
2252  std::size_t afterScope = prevAtScope + 2;
2253  if (prevAtScope == std::string_view::npos) {
2254  afterScope = 0;
2255  prevAtScope = 0;
2256  }
2257 
2258  result.fScopeName = ToString(StripSurroundingSpace(scopeFunctionTmplt.substr(0, prevAtScope)));
2259  std::string_view funcNameTmplArgs = scopeFunctionTmplt.substr(afterScope);
2260 
2261  result.fFunctionTemplateArguments.clear();
2262  std::size_t posTmpltOpen = FindNonNestedNeedles(funcNameTmplArgs, "<");
2263  if (posTmpltOpen != std::string_view::npos) {
2264  result.fFunctionName = ToString(StripSurroundingSpace(funcNameTmplArgs.substr(0, posTmpltOpen)));
2265 
2266  // Parse template parameters:
2267  std::string_view tmpltArgs = funcNameTmplArgs.substr(posTmpltOpen + 1);
2268  std::size_t posTmpltClose = FindNonNestedNeedles(tmpltArgs, ">");
2269  if (posTmpltClose != std::string_view::npos) {
2270  tmpltArgs = tmpltArgs.substr(0, posTmpltClose);
2271  std::size_t prevAfterArg = 0;
2272  while (tmpltArgs.length() > prevAfterArg) {
2273  std::size_t posComma = FindNonNestedNeedles(tmpltArgs.substr(prevAfterArg), ",");
2274  if (posComma == std::string_view::npos) {
2275  break;
2276  }
2277  result.fFunctionTemplateArguments.emplace_back(ToString(StripSurroundingSpace(tmpltArgs.substr(prevAfterArg, posComma))));
2278  prevAfterArg += posComma + 1;
2279  }
2280  // Add the trailing arg.
2281  result.fFunctionTemplateArguments.emplace_back(ToString(StripSurroundingSpace(tmpltArgs.substr(prevAfterArg))));
2282  }
2283  } else {
2284  result.fFunctionName = ToString(StripSurroundingSpace(funcNameTmplArgs));
2285  }
2286 
2287  result.fFunctionParameters.clear();
2288  if (posArgs != std::string_view::npos) {
2289  /// (params)
2290  std::string_view params = decl.substr(posArgs + 1);
2291  std::size_t posEndArgs = FindNonNestedNeedles(params, ")");
2292  if (posEndArgs != std::string_view::npos) {
2293  params = params.substr(0, posEndArgs);
2294  std::size_t prevAfterArg = 0;
2295  while (params.length() > prevAfterArg) {
2296  std::size_t posComma = FindNonNestedNeedles(params.substr(prevAfterArg), ",");
2297  if (posComma == std::string_view::npos) {
2298  result.fFunctionParameters.emplace_back(ToString(StripSurroundingSpace(params.substr(prevAfterArg))));
2299  break;
2300  }
2301  result.fFunctionParameters.emplace_back(ToString(StripSurroundingSpace(params.substr(prevAfterArg, posComma))));
2302  prevAfterArg += posComma + 1; // skip ','
2303  }
2304  }
2305  }
2306 
2307  return true;
2308 }
TClassEdit::FunctionSplitInfo
Result of splitting a function declaration into fReturnType fScopeName::fFunctionName<fFunctionTempla...
Definition: TClassEdit.h:237
c
#define c(i)
Definition: RSha256.hxx:101
n
const Int_t n
Definition: legend1.C:16
ROOT::kSTLdeque
@ kSTLdeque
Definition: ESTLType.h:32
first
Definition: first.py:1
ROOT::kSTLvector
@ kSTLvector
Definition: ESTLType.h:30
ROOT::kSTLset
@ kSTLset
Definition: ESTLType.h:35
Rstrstream.h
e
#define e(i)
Definition: RSha256.hxx:103
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::GetPartiallyDesugaredNameWithScopeHandling
virtual bool GetPartiallyDesugaredNameWithScopeHandling(const std::string &, std::string &, bool=true)=0
TClassEdit::FunctionSplitInfo::fFunctionName
std::string fFunctionName
Name of the function.
Definition: TClassEdit.h:245
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
TClassEdit::kDropAlloc
@ kDropAlloc
Definition: TClassEdit.h:78
TClassEdit::IsStdClass
bool IsStdClass(const char *type)
return true if the class belongs to the std namespace
Definition: TClassEdit.cxx:1389
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::kDropDefaultAlloc
@ kDropDefaultAlloc
Definition: TClassEdit.h:77
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
b
#define b(i)
Definition: RSha256.hxx:100
TClassEdit::kResolveTypedef
@ kResolveTypedef
Definition: TClassEdit.h:87
TClassEdit::TInterpreterLookupHelper::ShuttingDownSignal
virtual void ShuttingDownSignal()=0
IsDefElement
static bool IsDefElement(const char *elementName, const char *defaultElementName, const char *classname)
return whether or not 'elementName' is the STL default Element for type 'classname'
Definition: TClassEdit.cxx:756
ROOT::kSTLmap
@ kSTLmap
Definition: ESTLType.h:33
id
XFontStruct * id
Definition: TGX11.cxx:109
TClassEdit::TSplitType::fElements
std::vector< std::string > fElements
Definition: TClassEdit.h:140
ResolveTypedefImpl
static void ResolveTypedefImpl(const char *tname, unsigned int len, unsigned int &cursor, bool &modified, std::string &result)
Definition: TClassEdit.cxx:1487
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
kNone
const Handle_t kNone
Definition: GuiTypes.h:87
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
ROOT::kSTLforwardlist
@ kSTLforwardlist
Definition: ESTLType.h:41
TClassEdit::EComplexType
EComplexType
Definition: TClassEdit.h:110
StdLen
static size_t StdLen(const std::string_view name)
Return the length, if any, taken by std:: and any potential inline namespace (well compiler detail na...
Definition: TClassEdit.cxx:51
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
a
auto * a
Definition: textangle.C:12
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
ROOT::Math::Util::ToString
std::string ToString(const T &val)
Utility function for conversion to strings.
Definition: Util.h:50
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
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::InsertStd
std::string InsertStd(const char *tname)
Definition: TClassEdit.cxx:1755
RemoveStd
static void RemoveStd(std::string &name, size_t pos=0)
Remove std:: and any potential inline namespace (well compiler detail namespace.
Definition: TClassEdit.cxx:98
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::TInterpreterLookupHelper::GetPartiallyDesugaredName
virtual void GetPartiallyDesugaredName(std::string &)=0
TClassEdit.h
dummy
static RooMathCoreReg dummy
Definition: RooMathCoreReg.cxx:27
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
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::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
findNameEnd
static size_t findNameEnd(const std::string_view full)
Definition: TClassEdit.cxx:568
type
int type
Definition: TGX11.cxx:121
TClassEdit::FunctionSplitInfo::fFunctionParameters
std::vector< std::string > fFunctionParameters
Function parameters.
Definition: TClassEdit.h:252
TClassEdit::kKeepOuterConst
@ kKeepOuterConst
Definition: TClassEdit.h:86
ResolveTypedefProcessType
static void ResolveTypedefProcessType(const char *tname, unsigned int, unsigned int cursor, bool constprefix, unsigned int start_of_type, unsigned int end_of_type, unsigned int mod_start_of_type, bool &modified, std::string &result)
Definition: TClassEdit.cxx:1432
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
TGeant4Unit::second
static constexpr double second
Definition: TGeant4SystemOfUnits.h:151
TClassEdit::GetComplexType
EComplexType GetComplexType(const char *)
Definition: TClassEdit.cxx:120
TClassEdit::kDropStlDefault
@ kDropStlDefault
Definition: TClassEdit.h:81
R__FindTrailing
static void R__FindTrailing(std::string &full, std::string &stars)
Definition: TClassEdit.cxx:933
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