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