Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TClingUtils.cxx
Go to the documentation of this file.
1// @(#)root/metautils:$Id$
2// Author: Paul Russo, 2009-10-06
3
4/*************************************************************************
5 * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//______________________________________________________________________________
13// //
14// ROOT::TMetaUtils provides utility wrappers around //
15// cling, the LLVM-based interpreter. It's an internal set of tools //
16// used by TCling and rootcling. //
17// //
18//______________________________________________________________________________
19#include <algorithm>
20#include <iostream>
21#include <sstream>
22#include <stdlib.h>
23#include <stdio.h>
24#include <unordered_set>
25
26#include "RConfigure.h"
27#include <ROOT/RConfig.hxx>
29#include "Rtypes.h"
30#include "strlcpy.h"
31
32#include "RStl.h"
33
34#include "clang/AST/ASTContext.h"
35#include "clang/AST/Attr.h"
36#include "clang/AST/CXXInheritance.h"
37#include "clang/AST/Decl.h"
38#include "clang/AST/DeclTemplate.h"
39#include "clang/AST/Mangle.h"
40#include "clang/AST/Type.h"
41#include "clang/AST/TypeVisitor.h"
42#include "clang/Frontend/CompilerInstance.h"
43#include "clang/Lex/HeaderSearch.h"
44#include "clang/Lex/ModuleMap.h"
45#include "clang/Lex/Preprocessor.h"
46#include "clang/Lex/PreprocessorOptions.h"
47
48#include "clang/Sema/Lookup.h"
49#include "clang/Sema/Sema.h"
50#include "clang/Sema/SemaDiagnostic.h"
51
52#include "cling/Interpreter/LookupHelper.h"
53#include "cling/Interpreter/Transaction.h"
54#include "cling/Interpreter/Interpreter.h"
55#include "cling/Utils/AST.h"
56
57#include "llvm/Support/Path.h"
58#include "llvm/Support/FileSystem.h"
59
60#include "TClingUtils.h"
61
62#ifdef _WIN32
63#define strncasecmp _strnicmp
64#include <io.h>
65#else
66#include <unistd.h>
67#endif // _WIN32
68
69namespace ROOT {
70namespace TMetaUtils {
71
72std::string GetRealPath(const std::string &path)
73{
74 llvm::SmallString<256> result_path;
75 llvm::sys::fs::real_path(path, result_path, /*expandTilde*/true);
76 return result_path.str().str();
77}
78
79
80////////////////////////////////////////////////////////////////////////////////
81
83 using DeclsCont_t = TNormalizedCtxt::Config_t::SkipCollection;
87private:
91public:
92 TNormalizedCtxtImpl(const cling::LookupHelper &lh);
93
94 const Config_t &GetConfig() const { return fConfig; }
96 void AddTemplAndNargsToKeep(const clang::ClassTemplateDecl* templ, unsigned int i);
97 int GetNargsToKeep(const clang::ClassTemplateDecl* templ) const;
99 void keepTypedef(const cling::LookupHelper &lh, const char* name,
100 bool replace = false);
101};
102}
103}
104
105namespace {
106
107////////////////////////////////////////////////////////////////////////////////
108/// Add default parameter to the scope if needed.
109
110static clang::NestedNameSpecifier *AddDefaultParametersNNS(const clang::ASTContext& Ctx,
111 clang::NestedNameSpecifier* scope,
112 const cling::Interpreter &interpreter,
113 const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) {
114 if (!scope) return nullptr;
115
116 const clang::Type* scope_type = scope->getAsType();
117 if (scope_type) {
118 // this is not a namespace, so we might need to desugar
119 clang::NestedNameSpecifier* outer_scope = scope->getPrefix();
120 if (outer_scope) {
121 outer_scope = AddDefaultParametersNNS(Ctx, outer_scope, interpreter, normCtxt);
122 }
123
124 clang::QualType addDefault =
125 ROOT::TMetaUtils::AddDefaultParameters(clang::QualType(scope_type,0), interpreter, normCtxt );
126 // NOTE: Should check whether the type has changed or not.
127 if (addDefault.getTypePtr() != scope_type)
128 return clang::NestedNameSpecifier::Create(Ctx,outer_scope,
129 false /* template keyword wanted */,
130 addDefault.getTypePtr());
131 }
132 return scope;
133}
134
135////////////////////////////////////////////////////////////////////////////////
136
137static bool CheckDefinition(const clang::CXXRecordDecl *cl, const clang::CXXRecordDecl *context)
138{
139 if (!cl->hasDefinition()) {
140 if (context) {
141 ROOT::TMetaUtils::Error("CheckDefinition",
142 "Missing definition for class %s, please #include its header in the header of %s\n",
143 cl->getName().str().c_str(), context->getName().str().c_str());
144 } else {
145 ROOT::TMetaUtils::Error("CheckDefinition",
146 "Missing definition for class %s\n",
147 cl->getName().str().c_str());
148 }
149 return false;
150 }
151 return true;
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Check if 'scope' or any of its template parameter was substituted when
156/// instantiating the class template instance and replace it with the
157/// partially sugared types we have from 'instance'.
158
159static clang::NestedNameSpecifier *ReSubstTemplateArgNNS(const clang::ASTContext &Ctxt,
160 clang::NestedNameSpecifier *scope,
161 const clang::Type *instance)
162{
163 if (!scope) return nullptr;
164
165 const clang::Type* scope_type = scope->getAsType();
166 if (scope_type) {
167 clang::NestedNameSpecifier* outer_scope = scope->getPrefix();
168 if (outer_scope) {
169 outer_scope = ReSubstTemplateArgNNS(Ctxt, outer_scope, instance);
170 }
171 clang::QualType substScope =
172 ROOT::TMetaUtils::ReSubstTemplateArg(clang::QualType(scope_type,0), instance);
173 // NOTE: Should check whether the type has changed or not.
174 scope = clang::NestedNameSpecifier::Create(Ctxt,outer_scope,
175 false /* template keyword wanted */,
176 substScope.getTypePtr());
177 }
178 return scope;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182
183static bool IsTypeInt(const clang::Type *type)
184{
185 const clang::BuiltinType * builtin = llvm::dyn_cast<clang::BuiltinType>(type->getCanonicalTypeInternal().getTypePtr());
186 if (builtin) {
187 return builtin->isInteger(); // builtin->getKind() == clang::BuiltinType::Int;
188 } else {
189 return false;
190 }
191}
192
193////////////////////////////////////////////////////////////////////////////////
194
195static bool IsFieldDeclInt(const clang::FieldDecl *field)
196{
197 return IsTypeInt(field->getType().getTypePtr());
198}
199
200////////////////////////////////////////////////////////////////////////////////
201/// Return a data member name 'what' in the class described by 'cl' if any.
202
203static const clang::FieldDecl *GetDataMemberFromAll(const clang::CXXRecordDecl &cl, llvm::StringRef what)
204{
205 clang::ASTContext &C = cl.getASTContext();
206 clang::DeclarationName DName = &C.Idents.get(what);
207 auto R = cl.lookup(DName);
208 for (const clang::NamedDecl *D : R)
209 if (auto FD = llvm::dyn_cast<const clang::FieldDecl>(D))
210 return FD;
211 return nullptr;
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Return a data member name 'what' in any of the base classes of the class described by 'cl' if any.
216
217static const clang::FieldDecl *GetDataMemberFromAllParents(clang::Sema &SemaR, const clang::CXXRecordDecl &cl, const char *what)
218{
219 clang::DeclarationName DName = &SemaR.Context.Idents.get(what);
220 clang::LookupResult R(SemaR, DName, clang::SourceLocation(),
221 clang::Sema::LookupOrdinaryName,
222 clang::Sema::ForExternalRedeclaration);
223 SemaR.LookupInSuper(R, &const_cast<clang::CXXRecordDecl&>(cl));
224 if (R.empty())
225 return nullptr;
226 return llvm::dyn_cast<const clang::FieldDecl>(R.getFoundDecl());
227}
228
229static
230cling::LookupHelper::DiagSetting ToLHDS(bool wantDiags) {
231 return wantDiags
232 ? cling::LookupHelper::WithDiagnostics
233 : cling::LookupHelper::NoDiagnostics;
234}
235
236} // end of anonymous namespace
237
238
239namespace ROOT {
240namespace TMetaUtils {
241
242////////////////////////////////////////////////////////////////////////////////
243/// Add to the internal map the pointer of a template as key and the number of
244/// template arguments to keep as value.
245
246void TNormalizedCtxtImpl::AddTemplAndNargsToKeep(const clang::ClassTemplateDecl* templ,
247 unsigned int i){
248 if (!templ){
249 Error("TNormalizedCtxt::AddTemplAndNargsToKeep",
250 "Tring to specify a number of template arguments to keep for a null pointer. Exiting without assigning any value.\n");
251 return;
252 }
253
254 const clang::ClassTemplateDecl* canTempl = templ->getCanonicalDecl();
255
256 if(fTemplatePtrArgsToKeepMap.count(canTempl)==1 &&
257 fTemplatePtrArgsToKeepMap[canTempl]!=(int)i){
258 const std::string templateName (canTempl->getNameAsString());
259 const std::string i_str (std::to_string(i));
260 const std::string previousArgsToKeep(std::to_string(fTemplatePtrArgsToKeepMap[canTempl]));
261 Error("TNormalizedCtxt::AddTemplAndNargsToKeep",
262 "Tring to specify for template %s %s arguments to keep, while before this number was %s\n",
263 canTempl->getNameAsString().c_str(),
264 i_str.c_str(),
265 previousArgsToKeep.c_str());
266 }
267
268 fTemplatePtrArgsToKeepMap[canTempl]=i;
269}
270////////////////////////////////////////////////////////////////////////////////
271/// Get from the map the number of arguments to keep.
272/// It uses the canonical decl of the template as key.
273/// If not present, returns -1.
274
275int TNormalizedCtxtImpl::GetNargsToKeep(const clang::ClassTemplateDecl* templ) const{
276 const clang::ClassTemplateDecl* constTempl = templ->getCanonicalDecl();
277 auto thePairPtr = fTemplatePtrArgsToKeepMap.find(constTempl);
278 int nArgsToKeep = (thePairPtr != fTemplatePtrArgsToKeepMap.end() ) ? thePairPtr->second : -1;
279 return nArgsToKeep;
280}
281
282
283////////////////////////////////////////////////////////////////////////////////
284
285TNormalizedCtxt::TNormalizedCtxt(const cling::LookupHelper &lh):
286 fImpl(new TNormalizedCtxtImpl(lh))
287{}
288
290 fImpl(new TNormalizedCtxtImpl(*other.fImpl))
291{}
292
294 delete fImpl;
295}
297 return fImpl->GetConfig();
298}
301}
302void TNormalizedCtxt::AddTemplAndNargsToKeep(const clang::ClassTemplateDecl* templ, unsigned int i)
303{
304 return fImpl->AddTemplAndNargsToKeep(templ, i);
305}
306int TNormalizedCtxt::GetNargsToKeep(const clang::ClassTemplateDecl* templ) const
307{
308 return fImpl->GetNargsToKeep(templ);
309}
312}
313void TNormalizedCtxt::keepTypedef(const cling::LookupHelper &lh, const char* name,
314 bool replace /*= false*/)
315{
316 return fImpl->keepTypedef(lh, name, replace);
317}
318
319std::string AnnotatedRecordDecl::BuildDemangledTypeInfo(const clang::RecordDecl *rDecl,
320 const std::string &normalizedName)
321{
322 // Types with strong typedefs must not be findable through demangled type names, or else
323 // the demangled name will resolve to both sinblings double / Double32_t.
324 if (normalizedName.find("Double32_t") != std::string::npos
325 || normalizedName.find("Float16_t") != std::string::npos)
326 return {};
327 std::unique_ptr<clang::MangleContext> mangleCtx(rDecl->getASTContext().createMangleContext());
328 std::string mangledName;
329 {
330 llvm::raw_string_ostream sstr(mangledName);
331 if (const clang::TypeDecl* TD = llvm::dyn_cast<clang::TypeDecl>(rDecl)) {
332 mangleCtx->mangleCXXRTTI(clang::QualType(TD->getTypeForDecl(), 0), sstr);
333 }
334 }
335 if (!mangledName.empty()) {
336 int errDemangle = 0;
337#ifdef WIN32
338 if (mangledName[0] == '\01')
339 mangledName.erase(0, 1);
340 char *demangledTIName = TClassEdit::DemangleName(mangledName.c_str(), errDemangle);
341 if (!errDemangle && demangledTIName) {
342 static const char typeinfoNameFor[] = " `RTTI Type Descriptor'";
343 if (strstr(demangledTIName, typeinfoNameFor)) {
344 std::string demangledName = demangledTIName;
345 demangledName.erase(demangledName.end() - strlen(typeinfoNameFor), demangledName.end());
346#else
347 char* demangledTIName = TClassEdit::DemangleName(mangledName.c_str(), errDemangle);
348 if (!errDemangle && demangledTIName) {
349 static const char typeinfoNameFor[] = "typeinfo for ";
350 if (!strncmp(demangledTIName, typeinfoNameFor, strlen(typeinfoNameFor))) {
351 std::string demangledName = demangledTIName + strlen(typeinfoNameFor);
352#endif
353 free(demangledTIName);
354 return demangledName;
355 } else {
356#ifdef WIN32
357 ROOT::TMetaUtils::Error("AnnotatedRecordDecl::BuildDemangledTypeInfo",
358 "Demangled typeinfo name '%s' does not contain `RTTI Type Descriptor'\n",
359 demangledTIName);
360#else
361 ROOT::TMetaUtils::Error("AnnotatedRecordDecl::BuildDemangledTypeInfo",
362 "Demangled typeinfo name '%s' does not start with 'typeinfo for'\n",
363 demangledTIName);
364#endif
365 } // if demangled type_info starts with "typeinfo for "
366 } // if demangling worked
367 free(demangledTIName);
368 } // if mangling worked
369 return {};
370}
371
372
373////////////////////////////////////////////////////////////////////////////////
374/// There is no requested type name.
375/// Still let's normalized the actual name.
376
378 const clang::RecordDecl *decl,
379 bool rStreamerInfo,
380 bool rNoStreamer,
381 bool rRequestNoInputOperator,
382 bool rRequestOnlyTClass,
383 int rRequestedVersionNumber,
384 const cling::Interpreter &interpreter,
385 const TNormalizedCtxt &normCtxt) :
386 fRuleIndex(index), fDecl(decl), fRequestStreamerInfo(rStreamerInfo), fRequestNoStreamer(rNoStreamer),
387 fRequestNoInputOperator(rRequestNoInputOperator), fRequestOnlyTClass(rRequestOnlyTClass), fRequestedVersionNumber(rRequestedVersionNumber)
388{
389 TMetaUtils::GetNormalizedName(fNormalizedName, decl->getASTContext().getTypeDeclType(decl), interpreter,normCtxt);
391}
392
393////////////////////////////////////////////////////////////////////////////////
394/// Normalize the requested type name.
395
397 const clang::Type *requestedType,
398 const clang::RecordDecl *decl,
399 const char *requestName,
400 unsigned int nTemplateArgsToSkip,
401 bool rStreamerInfo,
402 bool rNoStreamer,
403 bool rRequestNoInputOperator,
404 bool rRequestOnlyTClass,
405 int rRequestVersionNumber,
406 const cling::Interpreter &interpreter,
407 const TNormalizedCtxt &normCtxt) :
408 fRuleIndex(index), fDecl(decl), fRequestedName(""), fRequestStreamerInfo(rStreamerInfo), fRequestNoStreamer(rNoStreamer),
409 fRequestNoInputOperator(rRequestNoInputOperator), fRequestOnlyTClass(rRequestOnlyTClass), fRequestedVersionNumber(rRequestVersionNumber)
410{
411 // For comparison purposes.
413 splitname1.ShortType(fRequestedName, 0);
414
415 TMetaUtils::GetNormalizedName( fNormalizedName, clang::QualType(requestedType,0), interpreter, normCtxt);
416 if ( 0!=TMetaUtils::RemoveTemplateArgsFromName( fNormalizedName, nTemplateArgsToSkip) ){
417 ROOT::TMetaUtils::Warning("AnnotatedRecordDecl",
418 "Could not remove the requested template arguments.\n");
419 }
421}
422
423////////////////////////////////////////////////////////////////////////////////
424/// Normalize the requested type name.
425
427 const clang::Type *requestedType,
428 const clang::RecordDecl *decl,
429 const char *requestName,
430 bool rStreamerInfo,
431 bool rNoStreamer,
432 bool rRequestNoInputOperator,
433 bool rRequestOnlyTClass,
434 int rRequestVersionNumber,
435 const cling::Interpreter &interpreter,
436 const TNormalizedCtxt &normCtxt) :
437 fRuleIndex(index), fDecl(decl), fRequestedName(""), fRequestStreamerInfo(rStreamerInfo), fRequestNoStreamer(rNoStreamer),
438 fRequestNoInputOperator(rRequestNoInputOperator), fRequestOnlyTClass(rRequestOnlyTClass), fRequestedVersionNumber(rRequestVersionNumber)
439{
440 // For comparison purposes.
442 splitname1.ShortType(fRequestedName, 0);
443
444 TMetaUtils::GetNormalizedName( fNormalizedName, clang::QualType(requestedType,0), interpreter, normCtxt);
446}
447
448////////////////////////////////////////////////////////////////////////////////
449/// Normalize the requested name.
450
452 const clang::RecordDecl *decl,
453 const char *requestName,
454 bool rStreamerInfo,
455 bool rNoStreamer,
456 bool rRequestNoInputOperator,
457 bool rRequestOnlyTClass,
458 int rRequestVersionNumber,
459 const cling::Interpreter &interpreter,
460 const TNormalizedCtxt &normCtxt) :
461 fRuleIndex(index), fDecl(decl), fRequestedName(""), fRequestStreamerInfo(rStreamerInfo), fRequestNoStreamer(rNoStreamer), fRequestNoInputOperator(rRequestNoInputOperator), fRequestOnlyTClass(rRequestOnlyTClass), fRequestedVersionNumber(rRequestVersionNumber)
462{
463 // const clang::ClassTemplateSpecializationDecl *tmplt_specialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl> (decl);
464 // if (tmplt_specialization) {
465 // tmplt_specialization->getTemplateArgs ().data()->print(decl->getASTContext().getPrintingPolicy(),llvm::outs());
466 // llvm::outs() << "\n";
467 // }
468 // const char *current = requestName;
469 // Strips spaces and std::
470 if (requestName && requestName[0]) {
473
475 } else {
476 TMetaUtils::GetNormalizedName( fNormalizedName, decl->getASTContext().getTypeDeclType(decl),interpreter,normCtxt);
477 }
479}
480
481////////////////////////////////////////////////////////////////////////////////
482
483TClingLookupHelper::TClingLookupHelper(cling::Interpreter &interpreter,
484 TNormalizedCtxt &normCtxt,
485 ExistingTypeCheck_t existingTypeCheck,
486 AutoParse_t autoParse,
487 bool *shuttingDownPtr,
488 const int* pgDebug /*= 0*/):
489 fInterpreter(&interpreter),fNormalizedCtxt(&normCtxt),
490 fExistingTypeCheck(existingTypeCheck),
491 fAutoParse(autoParse),
492 fInterpreterIsShuttingDownPtr(shuttingDownPtr),
493 fPDebug(pgDebug)
494{
495}
496
497////////////////////////////////////////////////////////////////////////////////
498/// Helper routine to ry hard to avoid looking up in the Cling database as
499/// this could enduce an unwanted autoparsing.
500
501bool TClingLookupHelper::ExistingTypeCheck(const std::string &tname,
502 std::string &result)
503{
504 if (tname.empty()) return false;
505
507 else return false;
508}
509
510////////////////////////////////////////////////////////////////////////////////
511
513{
514 const cling::LookupHelper& lh = fInterpreter->getLookupHelper();
515 clang::QualType t = lh.findType(nameLong, ToLHDS(WantDiags()));
516 if (!t.isNull()) {
517 clang::QualType dest = cling::utils::Transform::GetPartiallyDesugaredType(fInterpreter->getCI()->getASTContext(), t, fNormalizedCtxt->GetConfig(), true /* fully qualify */);
518 if (!dest.isNull() && (dest != t)) {
519 // getAsStringInternal() appends.
520 nameLong.clear();
521 dest.getAsStringInternal(nameLong, fInterpreter->getCI()->getASTContext().getPrintingPolicy());
522 }
523 }
524}
525
526////////////////////////////////////////////////////////////////////////////////
527
529 const std::string &nameLong)
530{
531 const cling::LookupHelper& lh = fInterpreter->getLookupHelper();
532 clang::QualType t = lh.findType(nondef.c_str(), ToLHDS(WantDiags()));
533 if (!t.isNull()) {
534 clang::QualType dest = cling::utils::Transform::GetPartiallyDesugaredType(fInterpreter->getCI()->getASTContext(), t, fNormalizedCtxt->GetConfig(), true /* fully qualify */);
535 if (!dest.isNull() && (dest != t) &&
536 nameLong == t.getAsString(fInterpreter->getCI()->getASTContext().getPrintingPolicy()))
537 return true;
538 }
539 return false;
540}
541
542////////////////////////////////////////////////////////////////////////////////
543
544bool TClingLookupHelper::IsDeclaredScope(const std::string &base, bool &isInlined)
545{
546 const cling::LookupHelper& lh = fInterpreter->getLookupHelper();
547 const clang::Decl *scope = lh.findScope(base.c_str(), ToLHDS(WantDiags()), nullptr);
548
549 if (!scope) {
550 // the nesting namespace is not declared
551 isInlined = false;
552 return false;
553 }
554 const clang::NamespaceDecl *nsdecl = llvm::dyn_cast<clang::NamespaceDecl>(scope);
555 isInlined = nsdecl && nsdecl->isInline();
556 return true;
557}
558
559////////////////////////////////////////////////////////////////////////////////
560/// We assume that we have a simple type:
561/// [const] typename[*&][const]
562
564 std::string &result,
565 bool dropstd /* = true */)
566{
567 if (tname.empty()) return false;
568
569 // Try hard to avoid looking up in the Cling database as this could enduce
570 // an unwanted autoparsing.
571 // Note: this is always done by the callers and thus is redundant.
572 // Maybe replace with
573 assert(! (fExistingTypeCheck && fExistingTypeCheck(tname,result)) );
575 return ! result.empty();
576 }
577
578 if (fAutoParse) fAutoParse(tname.c_str());
579
580 // Since we already check via other means (TClassTable which is populated by
581 // the dictonary loading, and the gROOT list of classes and enums, which are
582 // populated via TProtoClass/Enum), we should be able to disable the autoloading
583 // ... which requires access to libCore or libCling ...
584 const cling::LookupHelper& lh = fInterpreter->getLookupHelper();
585 clang::QualType t = lh.findType(tname.c_str(), ToLHDS(WantDiags()));
586 // Technically we ought to try:
587 // if (t.isNull()) t = lh.findType(TClassEdit::InsertStd(tname), ToLHDS(WantDiags()));
588 // at least until the 'normalized name' contains the std:: prefix.
589
590 if (!t.isNull()) {
592 if (!dest.isNull() && dest != t) {
593 // Since our input is not a template instance name, rather than going through the full
594 // TMetaUtils::GetNormalizedName, we just do the 'strip leading std' and fix
595 // white space.
596 clang::PrintingPolicy policy(fInterpreter->getCI()->getASTContext().getPrintingPolicy());
597 policy.SuppressTagKeyword = true; // Never get the class or struct keyword
598 policy.SuppressScope = true; // Force the scope to be coming from a clang::ElaboratedType.
599 // The scope suppression is required for getting rid of the anonymous part of the name of a class defined in an anonymous namespace.
600 // This gives us more control vs not using the clang::ElaboratedType and relying on the Policy.SuppressUnwrittenScope which would
601 // strip both the anonymous and the inline namespace names (and we probably do not want the later to be suppressed).
602 // getAsStringInternal() appends.
603 result.clear();
604 dest.getAsStringInternal(result, policy);
605 // Strip the std::
606 unsigned long offset = 0;
607 if (strncmp(result.c_str(), "const ", 6) == 0) {
608 offset = 6;
609 }
610 if (dropstd && strncmp(result.c_str()+offset, "std::", 5) == 0) {
611 result.erase(offset,5);
612 }
613 for(unsigned int i = 1; i<result.length(); ++i) {
614 if (result[i]=='s') {
615 if (result[i-1]=='<' || result[i-1]==',' || result[i-1]==' ') {
616 if (dropstd && result.compare(i,5,"std::",5) == 0) {
617 result.erase(i,5);
618 }
619 }
620 }
621 if (result[i]==' ') {
622 if (result[i-1] == ',') {
623 result.erase(i,1);
624 --i;
625 } else if ( (i+1) < result.length() &&
626 (result[i+1]=='*' || result[i+1]=='&' || result[i+1]=='[') ) {
627 result.erase(i,1);
628 --i;
629 }
630 }
631 }
632
633// std::string alt;
634// TMetaUtils::GetNormalizedName(alt, dest, *fInterpreter, *fNormalizedCtxt);
635// if (alt != result) fprintf(stderr,"norm: %s vs result=%s\n",alt.c_str(),result.c_str());
636
637 return true;
638 }
639 }
640 return false;
641}
642
643////////////////////////////////////////////////////////////////////////////////
644// TClassEdit will call this routine as soon as any of its static variable (used
645// for caching) is destroyed.
647{
650}
651
652 } // end namespace ROOT
653} // end namespace TMetaUtils
654
655
656////////////////////////////////////////////////////////////////////////////////
657/// Insert the type with name into the collection of typedefs to keep.
658/// if replace, replace occurrences of the canonical type by name.
659
660void ROOT::TMetaUtils::TNormalizedCtxtImpl::keepTypedef(const cling::LookupHelper &lh,
661 const char* name,
662 bool replace /*=false*/) {
663 clang::QualType toSkip = lh.findType(name, cling::LookupHelper::WithDiagnostics);
664 if (const clang::Type* T = toSkip.getTypePtr()) {
665 const clang::TypedefType *tt = llvm::dyn_cast<clang::TypedefType>(T);
666 if (!tt) return;
667 clang::Decl* D = tt->getDecl();
668 fConfig.m_toSkip.insert(D);
669 if (replace) {
670 clang::QualType canon = toSkip->getCanonicalTypeInternal();
671 fConfig.m_toReplace.insert(std::make_pair(canon.getTypePtr(),T));
672 } else {
673 fTypeWithAlternative.insert(T);
674 }
675 }
676}
677
678////////////////////////////////////////////////////////////////////////////////
679/// Initialize the list of typedef to keep (i.e. make them opaque for normalization)
680/// and the list of typedef whose semantic is different from their underlying type
681/// (Double32_t and Float16_t).
682/// This might be specific to an interpreter.
683
685{
686 keepTypedef(lh, "Double32_t");
687 keepTypedef(lh, "Float16_t");
688 keepTypedef(lh, "Long64_t", true);
689 keepTypedef(lh, "ULong64_t", true);
690
691 clang::QualType toSkip = lh.findType("string", cling::LookupHelper::WithDiagnostics);
692 if (!toSkip.isNull()) {
693 if (const clang::TypedefType* TT
694 = llvm::dyn_cast_or_null<clang::TypedefType>(toSkip.getTypePtr()))
695 fConfig.m_toSkip.insert(TT->getDecl());
696 }
697 toSkip = lh.findType("std::string", cling::LookupHelper::WithDiagnostics);
698 if (!toSkip.isNull()) {
699 if (const clang::TypedefType* TT
700 = llvm::dyn_cast_or_null<clang::TypedefType>(toSkip.getTypePtr()))
701 fConfig.m_toSkip.insert(TT->getDecl());
702
703 clang::QualType canon = toSkip->getCanonicalTypeInternal();
704 fConfig.m_toReplace.insert(std::make_pair(canon.getTypePtr(),toSkip.getTypePtr()));
705 }
706}
707
710
711////////////////////////////////////////////////////////////////////////////////
712
713inline bool IsTemplate(const clang::Decl &cl)
714{
715 return (cl.getKind() == clang::Decl::ClassTemplatePartialSpecialization
716 || cl.getKind() == clang::Decl::ClassTemplateSpecialization);
717}
718
719
720////////////////////////////////////////////////////////////////////////////////
721
722const clang::FunctionDecl* ROOT::TMetaUtils::ClassInfo__HasMethod(const clang::DeclContext *cl, const char* name,
723 const cling::Interpreter& interp)
724{
725 clang::Sema* S = &interp.getSema();
726 const clang::NamedDecl* ND = cling::utils::Lookup::Named(S, name, cl);
727 if (ND == (clang::NamedDecl*)-1)
728 return (clang::FunctionDecl*)-1;
729 return llvm::dyn_cast_or_null<clang::FunctionDecl>(ND);
730}
731
732////////////////////////////////////////////////////////////////////////////////
733/// Return the scope corresponding to 'name' or std::'name'
734
735const clang::CXXRecordDecl *
736ROOT::TMetaUtils::ScopeSearch(const char *name, const cling::Interpreter &interp,
737 bool /*diagnose*/, const clang::Type** resultType)
738{
739 const cling::LookupHelper& lh = interp.getLookupHelper();
740 // We have many bogus diagnostics if we allow diagnostics here. Suppress.
741 // FIXME: silence them in the callers.
742 const clang::CXXRecordDecl *result
743 = llvm::dyn_cast_or_null<clang::CXXRecordDecl>
744 (lh.findScope(name, cling::LookupHelper::NoDiagnostics, resultType));
745 if (!result) {
746 std::string std_name("std::");
747 std_name += name;
748 // We have many bogus diagnostics if we allow diagnostics here. Suppress.
749 // FIXME: silence them in the callers.
750 result = llvm::dyn_cast_or_null<clang::CXXRecordDecl>
751 (lh.findScope(std_name, cling::LookupHelper::NoDiagnostics, resultType));
752 }
753 return result;
754}
755
756
757////////////////////////////////////////////////////////////////////////////////
758
759bool ROOT::TMetaUtils::RequireCompleteType(const cling::Interpreter &interp, const clang::CXXRecordDecl *cl)
760{
761 clang::QualType qType(cl->getTypeForDecl(),0);
762 return RequireCompleteType(interp,cl->getLocation(),qType);
763}
764
765////////////////////////////////////////////////////////////////////////////////
766
767bool ROOT::TMetaUtils::RequireCompleteType(const cling::Interpreter &interp, clang::SourceLocation Loc, clang::QualType Type)
768{
769 clang::Sema& S = interp.getCI()->getSema();
770 // Here we might not have an active transaction to handle
771 // the caused instantiation decl.
772 cling::Interpreter::PushTransactionRAII RAII(const_cast<cling::Interpreter*>(&interp));
773 return S.RequireCompleteType(Loc, Type, clang::diag::err_incomplete_type);
774}
775
776////////////////////////////////////////////////////////////////////////////////
777
778bool ROOT::TMetaUtils::IsBase(const clang::CXXRecordDecl *cl, const clang::CXXRecordDecl *base,
779 const clang::CXXRecordDecl *context, const cling::Interpreter &interp)
780{
781 if (!cl || !base) {
782 return false;
783 }
784
785 if (!cl->getDefinition() || !cl->isCompleteDefinition()) {
786 RequireCompleteType(interp,cl);
787 }
788
789 if (!CheckDefinition(cl, context) || !CheckDefinition(base, context)) {
790 return false;
791 }
792
793 if (!base->hasDefinition()) {
794 ROOT::TMetaUtils::Error("IsBase", "Missing definition for class %s\n", base->getName().str().c_str());
795 return false;
796 }
797 return cl->isDerivedFrom(base);
798}
799
800////////////////////////////////////////////////////////////////////////////////
801
802bool ROOT::TMetaUtils::IsBase(const clang::FieldDecl &m, const char* basename, const cling::Interpreter &interp)
803{
804 const clang::CXXRecordDecl* CRD = llvm::dyn_cast<clang::CXXRecordDecl>(ROOT::TMetaUtils::GetUnderlyingRecordDecl(m.getType()));
805 if (!CRD) {
806 return false;
807 }
808
809 const clang::NamedDecl *base
810 = ScopeSearch(basename, interp, true /*diagnose*/, nullptr);
811
812 if (base) {
813 return IsBase(CRD, llvm::dyn_cast<clang::CXXRecordDecl>( base ),
814 llvm::dyn_cast<clang::CXXRecordDecl>(m.getDeclContext()),interp);
815 }
816 return false;
817}
818
819////////////////////////////////////////////////////////////////////////////////
820
821int ROOT::TMetaUtils::ElementStreamer(std::ostream& finalString,
822 const clang::NamedDecl &forcontext,
823 const clang::QualType &qti,
824 const char *R__t,int rwmode,
825 const cling::Interpreter &interp,
826 const char *tcl)
827{
828 static const clang::CXXRecordDecl *TObject_decl
829 = ROOT::TMetaUtils::ScopeSearch("TObject", interp, true /*diag*/, nullptr);
830 enum {
831 kBIT_ISTOBJECT = 0x10000000,
832 kBIT_HASSTREAMER = 0x20000000,
833 kBIT_ISSTRING = 0x40000000,
834
835 kBIT_ISPOINTER = 0x00001000,
836 kBIT_ISFUNDAMENTAL = 0x00000020,
837 kBIT_ISENUM = 0x00000008
838 };
839
840 const clang::Type &ti( * qti.getTypePtr() );
841 std::string tiName;
842 ROOT::TMetaUtils::GetQualifiedName(tiName, clang::QualType(&ti,0), forcontext);
843
844 std::string objType(ROOT::TMetaUtils::ShortTypeName(tiName.c_str()));
845
846 const clang::Type *rawtype = ROOT::TMetaUtils::GetUnderlyingType(clang::QualType(&ti,0));
847 std::string rawname;
848 ROOT::TMetaUtils::GetQualifiedName(rawname, clang::QualType(rawtype,0), forcontext);
849
850 clang::CXXRecordDecl *cxxtype = rawtype->getAsCXXRecordDecl() ;
851 int isStre = cxxtype && ROOT::TMetaUtils::ClassInfo__HasMethod(cxxtype,"Streamer",interp);
852 int isTObj = cxxtype && (IsBase(cxxtype,TObject_decl,nullptr,interp) || rawname == "TObject");
853
854 long kase = 0;
855
856 if (ti.isPointerType()) kase |= kBIT_ISPOINTER;
857 if (rawtype->isFundamentalType()) kase |= kBIT_ISFUNDAMENTAL;
858 if (rawtype->isEnumeralType()) kase |= kBIT_ISENUM;
859
860
861 if (isTObj) kase |= kBIT_ISTOBJECT;
862 if (isStre) kase |= kBIT_HASSTREAMER;
863 if (tiName == "string") kase |= kBIT_ISSTRING;
864 if (tiName == "string*") kase |= kBIT_ISSTRING;
865
866
867 if (!tcl)
868 tcl = " internal error in rootcling ";
869 // if (strcmp(objType,"string")==0) RStl::Instance().GenerateTClassFor( "string", interp, normCtxt );
870
871 if (rwmode == 0) { //Read mode
872
873 if (R__t) finalString << " " << tiName << " " << R__t << ";" << std::endl;
874 switch (kase) {
875
876 case kBIT_ISFUNDAMENTAL:
877 if (!R__t) return 0;
878 finalString << " R__b >> " << R__t << ";" << std::endl;
879 break;
880
881 case kBIT_ISPOINTER|kBIT_ISTOBJECT|kBIT_HASSTREAMER:
882 if (!R__t) return 1;
883 finalString << " " << R__t << " = (" << tiName << ")R__b.ReadObjectAny(" << tcl << ");" << std::endl;
884 break;
885
886 case kBIT_ISENUM:
887 if (!R__t) return 0;
888 // fprintf(fp, " R__b >> (Int_t&)%s;\n",R__t);
889 // On some platforms enums and not 'Int_t' and casting to a reference to Int_t
890 // induces the silent creation of a temporary which is 'filled' __instead of__
891 // the desired enum. So we need to take it one step at a time.
892 finalString << " Int_t readtemp;" << std::endl
893 << " R__b >> readtemp;" << std::endl
894 << " " << R__t << " = static_cast<" << tiName << ">(readtemp);" << std::endl;
895 break;
896
897 case kBIT_HASSTREAMER:
898 case kBIT_HASSTREAMER|kBIT_ISTOBJECT:
899 if (!R__t) return 0;
900 finalString << " " << R__t << ".Streamer(R__b);" << std::endl;
901 break;
902
903 case kBIT_HASSTREAMER|kBIT_ISPOINTER:
904 if (!R__t) return 1;
905 //fprintf(fp, " fprintf(stderr,\"info is %%p %%d\\n\",R__b.GetInfo(),R__b.GetInfo()?R__b.GetInfo()->GetOldVersion():-1);\n");
906 finalString << " if (R__b.GetInfo() && R__b.GetInfo()->GetOldVersion()<=3) {" << std::endl;
907 if (cxxtype && cxxtype->isAbstract()) {
908 finalString << " R__ASSERT(0);// " << objType << " is abstract. We assume that older file could not be produced using this streaming method." << std::endl;
909 } else {
910 finalString << " " << R__t << " = new " << objType << ";" << std::endl
911 << " " << R__t << "->Streamer(R__b);" << std::endl;
912 }
913 finalString << " } else {" << std::endl
914 << " " << R__t << " = (" << tiName << ")R__b.ReadObjectAny(" << tcl << ");" << std::endl
915 << " }" << std::endl;
916 break;
917
918 case kBIT_ISSTRING:
919 if (!R__t) return 0;
920 finalString << " {TString R__str;" << std::endl
921 << " R__str.Streamer(R__b);" << std::endl
922 << " " << R__t << " = R__str.Data();}" << std::endl;
923 break;
924
925 case kBIT_ISSTRING|kBIT_ISPOINTER:
926 if (!R__t) return 0;
927 finalString << " {TString R__str;" << std::endl
928 << " R__str.Streamer(R__b);" << std::endl
929 << " " << R__t << " = new string(R__str.Data());}" << std::endl;
930 break;
931
932 case kBIT_ISPOINTER:
933 if (!R__t) return 1;
934 finalString << " " << R__t << " = (" << tiName << ")R__b.ReadObjectAny(" << tcl << ");" << std::endl;
935 break;
936
937 default:
938 if (!R__t) return 1;
939 finalString << " R__b.StreamObject(&" << R__t << "," << tcl << ");" << std::endl;
940 break;
941 }
942
943 } else { //Write case
944
945 switch (kase) {
946
947 case kBIT_ISFUNDAMENTAL:
948 case kBIT_ISPOINTER|kBIT_ISTOBJECT|kBIT_HASSTREAMER:
949 if (!R__t) return 0;
950 finalString << " R__b << " << R__t << ";" << std::endl;
951 break;
952
953 case kBIT_ISENUM:
954 if (!R__t) return 0;
955 finalString << " { void *ptr_enum = (void*)&" << R__t << ";\n";
956 finalString << " R__b >> *reinterpret_cast<Int_t*>(ptr_enum); }" << std::endl;
957 break;
958
959 case kBIT_HASSTREAMER:
960 case kBIT_HASSTREAMER|kBIT_ISTOBJECT:
961 if (!R__t) return 0;
962 finalString << " ((" << objType << "&)" << R__t << ").Streamer(R__b);" << std::endl;
963 break;
964
965 case kBIT_HASSTREAMER|kBIT_ISPOINTER:
966 if (!R__t) return 1;
967 finalString << " R__b.WriteObjectAny(" << R__t << "," << tcl << ");" << std::endl;
968 break;
969
970 case kBIT_ISSTRING:
971 if (!R__t) return 0;
972 finalString << " {TString R__str(" << R__t << ".c_str());" << std::endl
973 << " R__str.Streamer(R__b);};" << std::endl;
974 break;
975
976 case kBIT_ISSTRING|kBIT_ISPOINTER:
977 if (!R__t) return 0;
978 finalString << " {TString R__str(" << R__t << "->c_str());" << std::endl
979 << " R__str.Streamer(R__b);}" << std::endl;
980 break;
981
982 case kBIT_ISPOINTER:
983 if (!R__t) return 1;
984 finalString << " R__b.WriteObjectAny(" << R__t << "," << tcl <<");" << std::endl;
985 break;
986
987 default:
988 if (!R__t) return 1;
989 finalString << " R__b.StreamObject((" << objType << "*)&" << R__t << "," << tcl << ");" << std::endl;
990 break;
991 }
992 }
993 return 0;
994}
995
996////////////////////////////////////////////////////////////////////////////////
997/// Checks if default constructor exists and accessible
998
999bool ROOT::TMetaUtils::CheckDefaultConstructor(const clang::CXXRecordDecl* cl, const cling::Interpreter& interpreter)
1000{
1001 clang::CXXRecordDecl* ncCl = const_cast<clang::CXXRecordDecl*>(cl);
1002
1003 // We may induce template instantiation
1004 cling::Interpreter::PushTransactionRAII clingRAII(const_cast<cling::Interpreter*>(&interpreter));
1005
1006 if (auto* Ctor = interpreter.getCI()->getSema().LookupDefaultConstructor(ncCl)) {
1007 if (Ctor->getAccess() == clang::AS_public && !Ctor->isDeleted()) {
1008 return true;
1009 }
1010 }
1011
1012 return false;
1013}
1014
1015
1016////////////////////////////////////////////////////////////////////////////////
1017/// Checks IO constructor - must be public and with specified argument
1018
1020 const char *typeOfArg,
1021 const clang::CXXRecordDecl *expectedArgType,
1022 const cling::Interpreter& interpreter)
1023{
1024 if (typeOfArg && !expectedArgType) {
1025 const cling::LookupHelper& lh = interpreter.getLookupHelper();
1026 // We can not use findScope since the type we are given are usually,
1027 // only forward declared (and findScope explicitly reject them).
1028 clang::QualType instanceType = lh.findType(typeOfArg, cling::LookupHelper::WithDiagnostics);
1029 if (!instanceType.isNull())
1030 expectedArgType = instanceType->getAsCXXRecordDecl();
1031 }
1032
1033 if (!expectedArgType)
1034 return EIOCtorCategory::kAbsent;
1035
1036 // FIXME: We should not iterate here. That costs memory!
1037 cling::Interpreter::PushTransactionRAII clingRAII(const_cast<cling::Interpreter*>(&interpreter));
1038 for (auto iter = cl->ctor_begin(), end = cl->ctor_end(); iter != end; ++iter)
1039 {
1040 if ((iter->getAccess() != clang::AS_public) || (iter->getNumParams() != 1))
1041 continue;
1042
1043 // We can reach this constructor.
1044 clang::QualType argType((*iter->param_begin())->getType());
1045 argType = argType.getDesugaredType(cl->getASTContext());
1046 // Deal with pointers and references: ROOT-7723
1047 auto ioCtorCategory = EIOCtorCategory::kAbsent;
1048 if (argType->isPointerType()) {
1049 ioCtorCategory = EIOCtorCategory::kIOPtrType;
1050 argType = argType->getPointeeType();
1051 } else if (argType->isReferenceType()) {
1052 ioCtorCategory = EIOCtorCategory::kIORefType;
1053 argType = argType.getNonReferenceType();
1054 } else
1055 continue;
1056
1057 argType = argType.getDesugaredType(cl->getASTContext());
1058 const clang::CXXRecordDecl *argDecl = argType->getAsCXXRecordDecl();
1059 if (argDecl) {
1060 if (argDecl->getCanonicalDecl() == expectedArgType->getCanonicalDecl()) {
1061 return ioCtorCategory;
1062 }
1063 } else {
1064 std::string realArg = argType.getAsString();
1065 std::string clarg("class ");
1066 clarg += typeOfArg;
1067 if (realArg == clarg)
1068 return ioCtorCategory;
1069 }
1070 } // for each constructor
1071
1072 return EIOCtorCategory::kAbsent;
1073}
1074
1075
1076////////////////////////////////////////////////////////////////////////////////
1077/// Check if class has constructor of provided type - either default or with single argument
1078
1080 const RConstructorType &ioctortype,
1081 const cling::Interpreter& interpreter)
1082{
1083 const char *arg = ioctortype.GetName();
1084
1085 if (!ioctortype.GetType() && (!arg || !arg[0])) {
1086 // We are looking for a constructor with zero non-default arguments.
1087
1088 return CheckDefaultConstructor(cl, interpreter) ? EIOCtorCategory::kDefault : EIOCtorCategory::kAbsent;
1089 }
1090
1091 return CheckIOConstructor(cl, arg, ioctortype.GetType(), interpreter);
1092}
1093
1094
1095////////////////////////////////////////////////////////////////////////////////
1096
1097const clang::CXXMethodDecl *GetMethodWithProto(const clang::Decl* cinfo,
1098 const char *method, const char *proto,
1099 const cling::Interpreter &interp,
1100 bool diagnose)
1101{
1102 const clang::FunctionDecl* funcD
1103 = interp.getLookupHelper().findFunctionProto(cinfo, method, proto,
1104 diagnose ? cling::LookupHelper::WithDiagnostics
1105 : cling::LookupHelper::NoDiagnostics);
1106 if (funcD)
1107 return llvm::dyn_cast<const clang::CXXMethodDecl>(funcD);
1108
1109 return nullptr;
1110}
1111
1112
1113////////////////////////////////////////////////////////////////////////////////
1114
1115namespace ROOT {
1116 namespace TMetaUtils {
1117 RConstructorType::RConstructorType(const char *type_of_arg, const cling::Interpreter &interp) : fArgTypeName(type_of_arg),fArgType(nullptr)
1118 {
1119 const cling::LookupHelper& lh = interp.getLookupHelper();
1120 // We can not use findScope since the type we are given are usually,
1121 // only forward declared (and findScope explicitly reject them).
1122 clang::QualType instanceType = lh.findType(type_of_arg, cling::LookupHelper::WithDiagnostics);
1123 if (!instanceType.isNull())
1124 fArgType = instanceType->getAsCXXRecordDecl();
1125 }
1126 const char *RConstructorType::GetName() const { return fArgTypeName.c_str(); }
1127 const clang::CXXRecordDecl *RConstructorType::GetType() const { return fArgType; }
1128 }
1129}
1130
1131////////////////////////////////////////////////////////////////////////////////
1132/// return true if we can find an constructor calleable without any arguments
1133/// or with one the IOCtor special types.
1134
1135bool ROOT::TMetaUtils::HasIOConstructor(const clang::CXXRecordDecl *cl,
1136 std::string& arg,
1137 const RConstructorTypes& ctorTypes,
1138 const cling::Interpreter &interp)
1139{
1140 if (cl->isAbstract()) return false;
1141
1142 for (auto & ctorType : ctorTypes) {
1143
1144 auto ioCtorCat = ROOT::TMetaUtils::CheckConstructor(cl, ctorType, interp);
1145
1146 if (EIOCtorCategory::kAbsent == ioCtorCat)
1147 continue;
1148
1149 std::string proto( ctorType.GetName() );
1150 bool defaultCtor = proto.empty();
1151 if (defaultCtor) {
1152 arg.clear();
1153 } else {
1154 // I/O constructors can take pointers or references to ctorTypes
1155 proto += " *";
1156 if (EIOCtorCategory::kIOPtrType == ioCtorCat) {
1157 arg = "( ("; //(MyType*)nullptr
1158 } else if (EIOCtorCategory::kIORefType == ioCtorCat) {
1159 arg = "( *("; //*(MyType*)nullptr
1160 }
1161 arg += proto;
1162 arg += ")nullptr )";
1163 }
1164 // Check for private operator new
1165 const clang::CXXMethodDecl *method
1166 = GetMethodWithProto(cl, "operator new", "size_t", interp,
1167 cling::LookupHelper::NoDiagnostics);
1168 if (method && method->getAccess() != clang::AS_public) {
1169 // The non-public op new is not going to improve for other c'tors.
1170 return false;
1171 }
1172
1173 // This one looks good!
1174 return true;
1175 }
1176 return false;
1177}
1178
1179////////////////////////////////////////////////////////////////////////////////
1180
1181bool ROOT::TMetaUtils::NeedDestructor(const clang::CXXRecordDecl *cl,
1182 const cling::Interpreter& interp)
1183{
1184 if (!cl) return false;
1185
1186 if (cl->hasUserDeclaredDestructor()) {
1187
1188 cling::Interpreter::PushTransactionRAII clingRAII(const_cast<cling::Interpreter*>(&interp));
1189 clang::CXXDestructorDecl *dest = cl->getDestructor();
1190 if (dest) {
1191 return (dest->getAccess() == clang::AS_public);
1192 } else {
1193 return true; // no destructor, so let's assume it means default?
1194 }
1195 }
1196 return true;
1197}
1198
1199////////////////////////////////////////////////////////////////////////////////
1200/// Return true, if the function (defined by the name and prototype) exists and is public
1201
1202bool ROOT::TMetaUtils::CheckPublicFuncWithProto(const clang::CXXRecordDecl *cl,
1203 const char *methodname,
1204 const char *proto,
1205 const cling::Interpreter &interp,
1206 bool diagnose)
1207{
1208 const clang::CXXMethodDecl *method
1209 = GetMethodWithProto(cl,methodname,proto, interp,
1210 diagnose ? cling::LookupHelper::WithDiagnostics
1211 : cling::LookupHelper::NoDiagnostics);
1212 return (method && method->getAccess() == clang::AS_public);
1213}
1214
1215////////////////////////////////////////////////////////////////////////////////
1216/// Return true if the class has a method DirectoryAutoAdd(TDirectory *)
1217
1218bool ROOT::TMetaUtils::HasDirectoryAutoAdd(const clang::CXXRecordDecl *cl, const cling::Interpreter &interp)
1219{
1220 // Detect if the class has a DirectoryAutoAdd
1221
1222 // Detect if the class or one of its parent has a DirectoryAutoAdd
1223 const char *proto = "TDirectory*";
1224 const char *name = "DirectoryAutoAdd";
1225
1226 return CheckPublicFuncWithProto(cl,name,proto,interp, false /*diags*/);
1227}
1228
1229
1230////////////////////////////////////////////////////////////////////////////////
1231/// Return true if the class has a method Merge(TCollection*,TFileMergeInfo*)
1232
1233bool ROOT::TMetaUtils::HasNewMerge(const clang::CXXRecordDecl *cl, const cling::Interpreter &interp)
1234{
1235 // Detect if the class has a 'new' Merge function.
1236
1237 // Detect if the class or one of its parent has a DirectoryAutoAdd
1238 const char *proto = "TCollection*,TFileMergeInfo*";
1239 const char *name = "Merge";
1240
1241 return CheckPublicFuncWithProto(cl,name,proto,interp, false /*diags*/);
1242}
1243
1244////////////////////////////////////////////////////////////////////////////////
1245/// Return true if the class has a method Merge(TCollection*)
1246
1247bool ROOT::TMetaUtils::HasOldMerge(const clang::CXXRecordDecl *cl, const cling::Interpreter &interp)
1248{
1249 // Detect if the class has an old fashion Merge function.
1250
1251 // Detect if the class or one of its parent has a DirectoryAutoAdd
1252 const char *proto = "TCollection*";
1253 const char *name = "Merge";
1254
1255 return CheckPublicFuncWithProto(cl,name,proto, interp, false /*diags*/);
1256}
1257
1258
1259////////////////////////////////////////////////////////////////////////////////
1260/// Return true if the class has a method ResetAfterMerge(TFileMergeInfo *)
1261
1262bool ROOT::TMetaUtils::HasResetAfterMerge(const clang::CXXRecordDecl *cl, const cling::Interpreter &interp)
1263{
1264 // Detect if the class has a 'new' Merge function.
1265 // bool hasMethod = cl.HasMethod("DirectoryAutoAdd");
1266
1267 // Detect if the class or one of its parent has a DirectoryAutoAdd
1268 const char *proto = "TFileMergeInfo*";
1269 const char *name = "ResetAfterMerge";
1270
1271 return CheckPublicFuncWithProto(cl,name,proto, interp, false /*diags*/);
1272}
1273
1274
1275////////////////////////////////////////////////////////////////////////////////
1276/// Return true if the class has a custom member function streamer.
1277
1279 const clang::CXXRecordDecl* clxx,
1280 const cling::Interpreter &interp,
1281 const TNormalizedCtxt &normCtxt)
1282{
1283 static const char *proto = "TBuffer&";
1284
1285 const clang::CXXMethodDecl *method
1286 = GetMethodWithProto(clxx,"Streamer",proto, interp,
1287 cling::LookupHelper::NoDiagnostics);
1288 const clang::DeclContext *clxx_as_context = llvm::dyn_cast<clang::DeclContext>(clxx);
1289
1290 return (method && method->getDeclContext() == clxx_as_context
1291 && ( cl.RequestNoStreamer() || !cl.RequestStreamerInfo()));
1292}
1293
1294////////////////////////////////////////////////////////////////////////////////
1295/// Return true if the class has a custom member function streamer.
1296
1298 const clang::CXXRecordDecl* clxx,
1299 const cling::Interpreter &interp,
1300 const TNormalizedCtxt &normCtxt)
1301{
1302 static const char *proto = "TBuffer&,TClass*";
1303
1304 const clang::CXXMethodDecl *method
1305 = GetMethodWithProto(clxx,"Streamer",proto, interp,
1306 cling::LookupHelper::NoDiagnostics);
1307 const clang::DeclContext *clxx_as_context = llvm::dyn_cast<clang::DeclContext>(clxx);
1308
1309 return (method && method->getDeclContext() == clxx_as_context
1310 && ( cl.RequestNoStreamer() || !cl.RequestStreamerInfo()));
1311}
1312
1313
1314////////////////////////////////////////////////////////////////////////////////
1315/// Main implementation relying on GetFullyQualifiedTypeName
1316/// All other GetQualifiedName functions leverage this one except the
1317/// one for namespaces.
1318
1319void ROOT::TMetaUtils::GetQualifiedName(std::string &qual_name, const clang::QualType &type, const clang::NamedDecl &forcontext)
1320{
1321 ROOT::TMetaUtils::GetFullyQualifiedTypeName(qual_name, type, forcontext.getASTContext());
1322}
1323
1324//----
1325std::string ROOT::TMetaUtils::GetQualifiedName(const clang::QualType &type, const clang::NamedDecl &forcontext)
1326{
1327 std::string result;
1329 type,
1330 forcontext);
1331 return result;
1332}
1333
1334
1335////////////////////////////////////////////////////////////////////////////////
1336
1337void ROOT::TMetaUtils::GetQualifiedName(std::string& qual_type, const clang::Type &type, const clang::NamedDecl &forcontext)
1338{
1339 clang::QualType qualType(&type,0);
1341 qualType,
1342 forcontext);
1343}
1344
1345//---
1346std::string ROOT::TMetaUtils::GetQualifiedName(const clang::Type &type, const clang::NamedDecl &forcontext)
1347{
1348 std::string result;
1350 type,
1351 forcontext);
1352 return result;
1353}
1354
1355// //______________________________________________________________________________
1356// void ROOT::TMetaUtils::GetQualifiedName(std::string &qual_name, const clang::NamespaceDecl &cl)
1357// {
1358// GetQualifiedName(qual_name,cl);
1359// }
1360//
1361// //----
1362// std::string ROOT::TMetaUtils::GetQualifiedName(const clang::NamespaceDecl &cl){
1363// return GetQualifiedName(cl);
1364// }
1365
1366////////////////////////////////////////////////////////////////////////////////
1367/// This implementation does not rely on GetFullyQualifiedTypeName
1368
1369void ROOT::TMetaUtils::GetQualifiedName(std::string &qual_name, const clang::NamedDecl &cl)
1370{
1371 llvm::raw_string_ostream stream(qual_name);
1372 clang::PrintingPolicy policy( cl.getASTContext().getPrintingPolicy() );
1373 policy.SuppressTagKeyword = true; // Never get the class or struct keyword
1374 policy.SuppressUnwrittenScope = true; // Don't write the inline or anonymous namespace names.
1375
1376 cl.getNameForDiagnostic(stream,policy,true);
1377 stream.flush(); // flush to string.
1378
1379 if ( qual_name == "(anonymous " || qual_name == "(unnamed" ) {
1380 size_t pos = qual_name.find(':');
1381 qual_name.erase(0,pos+2);
1382 }
1383}
1384
1385//----
1386std::string ROOT::TMetaUtils::GetQualifiedName(const clang::NamedDecl &cl){
1387 std::string result;
1389 return result;
1390}
1391
1392
1393////////////////////////////////////////////////////////////////////////////////
1394
1395void ROOT::TMetaUtils::GetQualifiedName(std::string &qual_name, const clang::RecordDecl &recordDecl)
1396{
1397 const clang::Type* declType ( recordDecl.getTypeForDecl() );
1398 clang::QualType qualType(declType,0);
1400 qualType,
1401 recordDecl);
1402}
1403
1404//----
1405std::string ROOT::TMetaUtils::GetQualifiedName(const clang::RecordDecl &recordDecl)
1406{
1407 std::string result;
1409 return result;
1410}
1411
1412////////////////////////////////////////////////////////////////////////////////
1413
1415{
1416 ROOT::TMetaUtils::GetQualifiedName(qual_name, *annotated.GetRecordDecl());
1417}
1418
1419//----
1421{
1422 std::string result;
1424 return result;
1425}
1426
1427////////////////////////////////////////////////////////////////////////////////
1428/// Create the data member name-type map for given class
1429
1430static void CreateNameTypeMap(const clang::CXXRecordDecl &cl, ROOT::MembersTypeMap_t& nameType)
1431{
1432 std::stringstream dims;
1433 std::string typenameStr;
1434
1435 const clang::ASTContext& astContext = cl.getASTContext();
1436
1437 // Loop over the non static data member.
1438 for(clang::RecordDecl::field_iterator field_iter = cl.field_begin(), end = cl.field_end();
1439 field_iter != end;
1440 ++field_iter){
1441 // The CINT based code was filtering away static variables (they are not part of
1442 // the list starting with field_begin in clang), and const enums (which should
1443 // also not be part of this list).
1444 // It was also filtering out the 'G__virtualinfo' artificial member.
1445
1446 typenameStr.clear();
1447 dims.str("");
1448 dims.clear();
1449
1450 clang::QualType fieldType(field_iter->getType());
1451 if (fieldType->isConstantArrayType()) {
1452 const clang::ConstantArrayType *arrayType = llvm::dyn_cast<clang::ConstantArrayType>(fieldType.getTypePtr());
1453 while (arrayType) {
1454 dims << "[" << arrayType->getSize().getLimitedValue() << "]";
1455 fieldType = arrayType->getElementType();
1456 arrayType = llvm::dyn_cast<clang::ConstantArrayType>(arrayType->getArrayElementTypeNoTypeQual());
1457 }
1458 }
1459
1460 ROOT::TMetaUtils::GetFullyQualifiedTypeName(typenameStr, fieldType, astContext);
1461 nameType[field_iter->getName().str()] = ROOT::Internal::TSchemaType(typenameStr.c_str(),dims.str().c_str());
1462 }
1463
1464 // And now the base classes
1465 // We also need to look at the base classes.
1466 for(clang::CXXRecordDecl::base_class_const_iterator iter = cl.bases_begin(), end = cl.bases_end();
1467 iter != end;
1468 ++iter){
1469 std::string basename( iter->getType()->getAsCXXRecordDecl()->getNameAsString() ); // Intentionally using only the unqualified name.
1470 nameType[basename] = ROOT::Internal::TSchemaType(basename.c_str(),"");
1471 }
1472}
1473
1474////////////////////////////////////////////////////////////////////////////////
1475
1476const clang::FunctionDecl *ROOT::TMetaUtils::GetFuncWithProto(const clang::Decl* cinfo,
1477 const char *method,
1478 const char *proto,
1479 const cling::Interpreter &interp,
1480 bool diagnose)
1481{
1482 return interp.getLookupHelper().findFunctionProto(cinfo, method, proto,
1483 diagnose ? cling::LookupHelper::WithDiagnostics
1484 : cling::LookupHelper::NoDiagnostics);
1485}
1486
1487////////////////////////////////////////////////////////////////////////////////
1488/// It looks like the template specialization decl actually contains _less_ information
1489/// on the location of the code than the decl (in case where there is forward declaration,
1490/// that is what the specialization points to.
1491///
1492/// const clang::CXXRecordDecl* clxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
1493/// if (clxx) {
1494/// switch(clxx->getTemplateSpecializationKind()) {
1495/// case clang::TSK_Undeclared:
1496/// // We want the default behavior
1497/// break;
1498/// case clang::TSK_ExplicitInstantiationDeclaration:
1499/// case clang::TSK_ExplicitInstantiationDefinition:
1500/// case clang::TSK_ImplicitInstantiation: {
1501/// // We want the location of the template declaration:
1502/// const clang::ClassTemplateSpecializationDecl *tmplt_specialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl> (clxx);
1503/// if (tmplt_specialization) {
1504/// return GetLineNumber(const_cast< clang::ClassTemplateSpecializationDecl *>(tmplt_specialization)->getSpecializedTemplate());
1505/// }
1506/// break;
1507/// }
1508/// case clang::TSK_ExplicitSpecialization:
1509/// // We want the default behavior
1510/// break;
1511/// default:
1512/// break;
1513/// }
1514/// }
1515
1516long ROOT::TMetaUtils::GetLineNumber(const clang::Decl *decl)
1517{
1518 clang::SourceLocation sourceLocation = decl->getLocation();
1519 clang::SourceManager& sourceManager = decl->getASTContext().getSourceManager();
1520
1521 if (!sourceLocation.isValid() ) {
1522 return -1;
1523 }
1524
1525 if (!sourceLocation.isFileID()) {
1526 sourceLocation = sourceManager.getExpansionRange(sourceLocation).getEnd();
1527 }
1528
1529 if (sourceLocation.isValid() && sourceLocation.isFileID()) {
1530 return sourceManager.getLineNumber(sourceManager.getFileID(sourceLocation),sourceManager.getFileOffset(sourceLocation));
1531 }
1532 else {
1533 return -1;
1534 }
1535}
1536
1537////////////////////////////////////////////////////////////////////////////////
1538/// Return true if the type is a Double32_t or Float16_t or
1539/// is a instance template that depends on Double32_t or Float16_t.
1540
1541bool ROOT::TMetaUtils::hasOpaqueTypedef(clang::QualType instanceType, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
1542{
1543 while (llvm::isa<clang::PointerType>(instanceType.getTypePtr())
1544 || llvm::isa<clang::ReferenceType>(instanceType.getTypePtr()))
1545 {
1546 instanceType = instanceType->getPointeeType();
1547 }
1548
1549 const clang::ElaboratedType* etype
1550 = llvm::dyn_cast<clang::ElaboratedType>(instanceType.getTypePtr());
1551 if (etype) {
1552 instanceType = clang::QualType(etype->getNamedType().getTypePtr(),0);
1553 }
1554
1555 // There is no typedef to worried about, except for the opaque ones.
1556
1557 // Technically we should probably used our own list with just
1558 // Double32_t and Float16_t
1559 if (normCtxt.GetTypeWithAlternative().count(instanceType.getTypePtr())) {
1560 return true;
1561 }
1562
1563
1564 bool result = false;
1565 const clang::CXXRecordDecl* clxx = instanceType->getAsCXXRecordDecl();
1566 if (clxx && clxx->getTemplateSpecializationKind() != clang::TSK_Undeclared) {
1567 // do the template thing.
1568 const clang::TemplateSpecializationType* TST
1569 = llvm::dyn_cast<const clang::TemplateSpecializationType>(instanceType.getTypePtr());
1570 if (!TST) {
1571 // std::string type_name;
1572 // type_name = GetQualifiedName( instanceType, *clxx );
1573 // fprintf(stderr,"ERROR: Could not findS TST for %s\n",type_name.c_str());
1574 return false;
1575 }
1576 for (const clang::TemplateArgument &TA : TST->template_arguments()) {
1577 if (TA.getKind() == clang::TemplateArgument::Type) {
1578 result |= ROOT::TMetaUtils::hasOpaqueTypedef(TA.getAsType(), normCtxt);
1579 }
1580 }
1581 }
1582 return result;
1583}
1584
1585////////////////////////////////////////////////////////////////////////////////
1586/// Return true if any of the argument is or contains a double32.
1587
1589 const cling::Interpreter &interp,
1590 const TNormalizedCtxt &normCtxt)
1591{
1592 const clang::CXXRecordDecl *clxx = llvm::dyn_cast<clang::CXXRecordDecl>(cl.GetRecordDecl());
1593 if (!clxx || clxx->getTemplateSpecializationKind() == clang::TSK_Undeclared) return false;
1594
1595 clang::QualType instanceType = interp.getLookupHelper().findType(cl.GetNormalizedName(),
1596 cling::LookupHelper::WithDiagnostics);
1597 if (instanceType.isNull()) {
1598 //Error(0,"Could not find the clang::Type for %s\n",cl.GetNormalizedName());
1599 return false;
1600 }
1601
1602 return ROOT::TMetaUtils::hasOpaqueTypedef(instanceType, normCtxt);
1603}
1604
1605////////////////////////////////////////////////////////////////////////////////
1606/// Extract attr string
1607
1608int ROOT::TMetaUtils::extractAttrString(clang::Attr* attribute, std::string& attrString)
1609{
1610 clang::AnnotateAttr* annAttr = clang::dyn_cast<clang::AnnotateAttr>(attribute);
1611 if (!annAttr) {
1612 //TMetaUtils::Error(0,"Could not cast Attribute to AnnotatedAttribute\n");
1613 return 1;
1614 }
1615 attrString = annAttr->getAnnotation().str();
1616 return 0;
1617}
1618
1619////////////////////////////////////////////////////////////////////////////////
1620
1621int ROOT::TMetaUtils::extractPropertyNameValFromString(const std::string attributeStr,std::string& attrName, std::string& attrValue)
1622{
1623 // if separator found, extract name and value
1624 size_t substrFound (attributeStr.find(propNames::separator));
1625 if (substrFound==std::string::npos) {
1626 //TMetaUtils::Error(0,"Could not find property name-value separator (%s)\n",ROOT::TMetaUtils::PropertyNameValSeparator.c_str());
1627 return 1;
1628 }
1629 size_t EndPart1 = attributeStr.find_first_of(propNames::separator) ;
1630 attrName = attributeStr.substr(0, EndPart1);
1631 const int separatorLength(propNames::separator.size());
1632 attrValue = attributeStr.substr(EndPart1 + separatorLength);
1633 return 0;
1634}
1635
1636////////////////////////////////////////////////////////////////////////////////
1637
1638int ROOT::TMetaUtils::extractPropertyNameVal(clang::Attr* attribute, std::string& attrName, std::string& attrValue)
1639{
1640 std::string attrString;
1641 int ret = extractAttrString(attribute, attrString);
1642 if (0!=ret) return ret;
1643 return extractPropertyNameValFromString(attrString, attrName,attrValue);
1644}
1645
1646////////////////////////////////////////////////////////////////////////////////
1647/// This routine counts on the "propName<separator>propValue" format
1648
1650 const std::string& propName,
1651 std::string& propValue)
1652{
1653 for (clang::Decl::attr_iterator attrIt = decl.attr_begin();
1654 attrIt!=decl.attr_end();++attrIt){
1655 clang::AnnotateAttr* annAttr = clang::dyn_cast<clang::AnnotateAttr>(*attrIt);
1656 if (!annAttr) continue;
1657
1658 llvm::StringRef attribute = annAttr->getAnnotation();
1659 std::pair<llvm::StringRef,llvm::StringRef> split = attribute.split(propNames::separator.c_str());
1660 if (split.first != propName.c_str()) continue;
1661 else {
1662 propValue = split.second.str();
1663 return true;
1664 }
1665 }
1666 return false;
1667}
1668
1669////////////////////////////////////////////////////////////////////////////////
1670/// This routine counts on the "propName<separator>propValue" format
1671
1673 const std::string& propName,
1674 int& propValue)
1675{
1676 for (clang::Decl::attr_iterator attrIt = decl.attr_begin();
1677 attrIt!=decl.attr_end();++attrIt){
1678 clang::AnnotateAttr* annAttr = clang::dyn_cast<clang::AnnotateAttr>(*attrIt);
1679 if (!annAttr) continue;
1680
1681 llvm::StringRef attribute = annAttr->getAnnotation();
1682 std::pair<llvm::StringRef,llvm::StringRef> split = attribute.split(propNames::separator.c_str());
1683 if (split.first != propName.c_str()) continue;
1684 else {
1685 return split.second.getAsInteger(10,propValue);
1686 }
1687 }
1688 return false;
1689}
1690
1691////////////////////////////////////////////////////////////////////////////////
1692/// FIXME: a function of 450+ lines!
1693
1694void ROOT::TMetaUtils::WriteClassInit(std::ostream& finalString,
1695 const AnnotatedRecordDecl &cl,
1696 const clang::CXXRecordDecl *decl,
1697 const cling::Interpreter &interp,
1698 const TNormalizedCtxt &normCtxt,
1699 const RConstructorTypes& ctorTypes,
1700 bool& needCollectionProxy)
1701{
1702 std::string classname = TClassEdit::GetLong64_Name(cl.GetNormalizedName());
1703
1704 std::string mappedname;
1705 ROOT::TMetaUtils::GetCppName(mappedname,classname.c_str());
1706 std::string csymbol = classname;
1707 std::string args;
1708
1709 if ( ! TClassEdit::IsStdClass( classname.c_str() ) ) {
1710
1711 // Prefix the full class name with '::' except for the STL
1712 // containers and std::string. This is to request the
1713 // real class instead of the class in the namespace ROOT::Shadow
1714 csymbol.insert(0,"::");
1715 }
1716
1717 int stl = TClassEdit::IsSTLCont(classname);
1718 bool bset = TClassEdit::IsSTLBitset(classname.c_str());
1719
1720 bool isStd = TMetaUtils::IsStdClass(*decl);
1721 const cling::LookupHelper& lh = interp.getLookupHelper();
1722 bool isString = TMetaUtils::IsOfType(*decl,"std::string",lh);
1723
1724 bool isStdNotString = isStd && !isString;
1725
1726 finalString << "namespace ROOT {" << "\n";
1727
1728 if (!ClassInfo__HasMethod(decl,"Dictionary",interp) || IsTemplate(*decl))
1729 {
1730 finalString << " static TClass *" << mappedname.c_str() << "_Dictionary();\n"
1731 << " static void " << mappedname.c_str() << "_TClassManip(TClass*);\n";
1732
1733
1734 }
1735
1736 if (HasIOConstructor(decl, args, ctorTypes, interp)) {
1737 finalString << " static void *new_" << mappedname.c_str() << "(void *p = nullptr);" << "\n";
1738
1739 if (args.size()==0 && NeedDestructor(decl, interp))
1740 {
1741 finalString << " static void *newArray_";
1742 finalString << mappedname.c_str();
1743 finalString << "(Long_t size, void *p);";
1744 finalString << "\n";
1745 }
1746 }
1747
1748 if (NeedDestructor(decl, interp)) {
1749 finalString << " static void delete_" << mappedname.c_str() << "(void *p);" << "\n" << " static void deleteArray_" << mappedname.c_str() << "(void *p);" << "\n" << " static void destruct_" << mappedname.c_str() << "(void *p);" << "\n";
1750 }
1751 if (HasDirectoryAutoAdd(decl, interp)) {
1752 finalString << " static void directoryAutoAdd_" << mappedname.c_str() << "(void *obj, TDirectory *dir);" << "\n";
1753 }
1754 if (HasCustomStreamerMemberFunction(cl, decl, interp, normCtxt)) {
1755 finalString << " static void streamer_" << mappedname.c_str() << "(TBuffer &buf, void *obj);" << "\n";
1756 }
1757 if (HasCustomConvStreamerMemberFunction(cl, decl, interp, normCtxt)) {
1758 finalString << " static void conv_streamer_" << mappedname.c_str() << "(TBuffer &buf, void *obj, const TClass*);" << "\n";
1759 }
1760 if (HasNewMerge(decl, interp) || HasOldMerge(decl, interp)) {
1761 finalString << " static Long64_t merge_" << mappedname.c_str() << "(void *obj, TCollection *coll,TFileMergeInfo *info);" << "\n";
1762 }
1763 if (HasResetAfterMerge(decl, interp)) {
1764 finalString << " static void reset_" << mappedname.c_str() << "(void *obj, TFileMergeInfo *info);" << "\n";
1765 }
1766
1767 //--------------------------------------------------------------------------
1768 // Check if we have any schema evolution rules for this class
1769 /////////////////////////////////////////////////////////////////////////////
1770
1771 ROOT::SchemaRuleClassMap_t::iterator rulesIt1 = ROOT::gReadRules.find( classname.c_str() );
1772 ROOT::SchemaRuleClassMap_t::iterator rulesIt2 = ROOT::gReadRawRules.find( classname.c_str() );
1773
1774 ROOT::MembersTypeMap_t nameTypeMap;
1775 CreateNameTypeMap( *decl, nameTypeMap ); // here types for schema evo are written
1776
1777 //--------------------------------------------------------------------------
1778 // Process the read rules
1779 /////////////////////////////////////////////////////////////////////////////
1780
1781 if( rulesIt1 != ROOT::gReadRules.end() ) {
1782 int i = 0;
1783 finalString << "\n // Schema evolution read functions\n";
1784 std::list<ROOT::SchemaRuleMap_t>::iterator rIt = rulesIt1->second.begin();
1785 while( rIt != rulesIt1->second.end() ) {
1786
1787 //--------------------------------------------------------------------
1788 // Check if the rules refer to valid data members
1789 ///////////////////////////////////////////////////////////////////////
1790
1791 std::string error_string;
1792 if( !HasValidDataMembers( *rIt, nameTypeMap, error_string ) ) {
1793 Warning(nullptr, "%s", error_string.c_str());
1794 rIt = rulesIt1->second.erase(rIt);
1795 continue;
1796 }
1797
1798 //---------------------------------------------------------------------
1799 // Write the conversion function if necessary
1800 ///////////////////////////////////////////////////////////////////////
1801
1802 if( rIt->find( "code" ) != rIt->end() ) {
1803 WriteReadRuleFunc( *rIt, i++, mappedname, nameTypeMap, finalString );
1804 }
1805 ++rIt;
1806 }
1807 }
1808
1809
1810
1811
1812 //--------------------------------------------------------------------------
1813 // Process the read raw rules
1814 /////////////////////////////////////////////////////////////////////////////
1815
1816 if( rulesIt2 != ROOT::gReadRawRules.end() ) {
1817 int i = 0;
1818 finalString << "\n // Schema evolution read raw functions\n";
1819 std::list<ROOT::SchemaRuleMap_t>::iterator rIt = rulesIt2->second.begin();
1820 while( rIt != rulesIt2->second.end() ) {
1821
1822 //--------------------------------------------------------------------
1823 // Check if the rules refer to valid data members
1824 ///////////////////////////////////////////////////////////////////////
1825
1826 std::string error_string;
1827 if( !HasValidDataMembers( *rIt, nameTypeMap, error_string ) ) {
1828 Warning(nullptr, "%s", error_string.c_str());
1829 rIt = rulesIt2->second.erase(rIt);
1830 continue;
1831 }
1832
1833 //---------------------------------------------------------------------
1834 // Write the conversion function
1835 ///////////////////////////////////////////////////////////////////////
1836
1837 if( rIt->find( "code" ) == rIt->end() )
1838 continue;
1839
1840 WriteReadRawRuleFunc( *rIt, i++, mappedname, nameTypeMap, finalString );
1841 ++rIt;
1842 }
1843 }
1844
1845 finalString << "\n" << " // Function generating the singleton type initializer" << "\n";
1846
1847 finalString << " static TGenericClassInfo *GenerateInitInstanceLocal(const " << csymbol << "*)" << "\n" << " {" << "\n";
1848
1849 finalString << " " << csymbol << " *ptr = nullptr;" << "\n";
1850
1851 //fprintf(fp, " static ::ROOT::ClassInfo< %s > \n",classname.c_str());
1852 if (ClassInfo__HasMethod(decl,"IsA",interp) ) {
1853 finalString << " static ::TVirtualIsAProxy* isa_proxy = new ::TInstrumentedIsAProxy< " << csymbol << " >(nullptr);" << "\n";
1854 }
1855 else {
1856 finalString << " static ::TVirtualIsAProxy* isa_proxy = new ::TIsAProxy(typeid(" << csymbol << "));" << "\n";
1857 }
1858 finalString << " static ::ROOT::TGenericClassInfo " << "\n" << " instance(\"" << classname.c_str() << "\", ";
1859
1860 if (ClassInfo__HasMethod(decl,"Class_Version",interp)) {
1861 finalString << csymbol << "::Class_Version(), ";
1862 } else if (bset) {
1863 finalString << "2, "; // bitset 'version number'
1864 } else if (stl) {
1865 finalString << "-2, "; // "::TStreamerInfo::Class_Version(), ";
1866 } else if( cl.HasClassVersion() ) {
1867 finalString << cl.RequestedVersionNumber() << ", ";
1868 } else { // if (cl_input.RequestStreamerInfo()) {
1869
1870 // Need to find out if the operator>> is actually defined for this class.
1871 static const char *versionFunc = "GetClassVersion";
1872 // int ncha = strlen(classname.c_str())+strlen(versionFunc)+5;
1873 // char *funcname= new char[ncha];
1874 // snprintf(funcname,ncha,"%s<%s >",versionFunc,classname.c_str());
1875 std::string proto = classname + "*";
1876 const clang::Decl* ctxt = llvm::dyn_cast<clang::Decl>((*cl).getDeclContext());
1877 const clang::FunctionDecl *methodinfo
1878 = ROOT::TMetaUtils::GetFuncWithProto(ctxt, versionFunc, proto.c_str(),
1879 interp, cling::LookupHelper::NoDiagnostics);
1880 // delete [] funcname;
1881
1882 if (methodinfo &&
1883 ROOT::TMetaUtils::GetFileName(*methodinfo, interp).find("Rtypes.h") == llvm::StringRef::npos) {
1884
1885 // GetClassVersion was defined in the header file.
1886 //fprintf(fp, "GetClassVersion((%s *)0x0), ",classname.c_str());
1887 finalString << "GetClassVersion< ";
1888 finalString << classname.c_str();
1889 finalString << " >(), ";
1890 }
1891 //static char temporary[1024];
1892 //sprintf(temporary,"GetClassVersion<%s>( (%s *) 0x0 )",classname.c_str(),classname.c_str());
1893 //fprintf(stderr,"DEBUG: %s has value %d\n",classname.c_str(),(int)G__int(G__calc(temporary)));
1894 }
1895
1896 std::string filename = ROOT::TMetaUtils::GetFileName(*cl, interp);
1897 if (filename.length() > 0) {
1898 for (unsigned int i=0; i<filename.length(); i++) {
1899 if (filename[i]=='\\') filename[i]='/';
1900 }
1901 }
1902 finalString << "\"" << filename << "\", " << ROOT::TMetaUtils::GetLineNumber(cl)
1903 << "," << "\n" << " typeid(" << csymbol
1904 << "), ::ROOT::Internal::DefineBehavior(ptr, ptr)," << "\n" << " ";
1905
1906 if (ClassInfo__HasMethod(decl,"Dictionary",interp) && !IsTemplate(*decl)) {
1907 finalString << "&" << csymbol << "::Dictionary, ";
1908 } else {
1909 finalString << "&" << mappedname << "_Dictionary, ";
1910 }
1911
1912 enum {
1913 TClassTable__kHasCustomStreamerMember = 0x10 // See TClassTable.h
1914 };
1915
1916 Int_t rootflag = cl.RootFlag();
1917 if (HasCustomStreamerMemberFunction(cl, decl, interp, normCtxt)) {
1918 rootflag = rootflag | TClassTable__kHasCustomStreamerMember;
1919 }
1920 finalString << "isa_proxy, " << rootflag << "," << "\n" << " sizeof(" << csymbol << ") );" << "\n";
1921 if (HasIOConstructor(decl, args, ctorTypes, interp)) {
1922 finalString << " instance.SetNew(&new_" << mappedname.c_str() << ");" << "\n";
1923 if (args.size()==0 && NeedDestructor(decl, interp))
1924 finalString << " instance.SetNewArray(&newArray_" << mappedname.c_str() << ");" << "\n";
1925 }
1926 if (NeedDestructor(decl, interp)) {
1927 finalString << " instance.SetDelete(&delete_" << mappedname.c_str() << ");" << "\n" << " instance.SetDeleteArray(&deleteArray_" << mappedname.c_str() << ");" << "\n" << " instance.SetDestructor(&destruct_" << mappedname.c_str() << ");" << "\n";
1928 }
1929 if (HasDirectoryAutoAdd(decl, interp)) {
1930 finalString << " instance.SetDirectoryAutoAdd(&directoryAutoAdd_" << mappedname.c_str() << ");" << "\n";
1931 }
1932 if (HasCustomStreamerMemberFunction(cl, decl, interp, normCtxt)) {
1933 // We have a custom member function streamer or an older (not StreamerInfo based) automatic streamer.
1934 finalString << " instance.SetStreamerFunc(&streamer_" << mappedname.c_str() << ");" << "\n";
1935 }
1936 if (HasCustomConvStreamerMemberFunction(cl, decl, interp, normCtxt)) {
1937 // We have a custom member function streamer or an older (not StreamerInfo based) automatic streamer.
1938 finalString << " instance.SetConvStreamerFunc(&conv_streamer_" << mappedname.c_str() << ");" << "\n";
1939 }
1940 if (HasNewMerge(decl, interp) || HasOldMerge(decl, interp)) {
1941 finalString << " instance.SetMerge(&merge_" << mappedname.c_str() << ");" << "\n";
1942 }
1943 if (HasResetAfterMerge(decl, interp)) {
1944 finalString << " instance.SetResetAfterMerge(&reset_" << mappedname.c_str() << ");" << "\n";
1945 }
1946 if (bset) {
1947 finalString << " instance.AdoptCollectionProxyInfo(TCollectionProxyInfo::Generate(TCollectionProxyInfo::" << "Pushback" << "<Internal::TStdBitsetHelper< " << classname.c_str() << " > >()));" << "\n";
1948
1949 needCollectionProxy = true;
1950 } else if (stl != 0 &&
1951 ((stl > 0 && stl<ROOT::kSTLend) || (stl < 0 && stl>-ROOT::kSTLend)) && // is an stl container
1952 (stl != ROOT::kSTLbitset && stl !=-ROOT::kSTLbitset) ){ // is no bitset
1953 int idx = classname.find("<");
1954 int stlType = (idx!=(int)std::string::npos) ? TClassEdit::STLKind(classname.substr(0,idx)) : 0;
1955 const char* methodTCP = nullptr;
1956 switch(stlType) {
1957 case ROOT::kSTLvector:
1958 case ROOT::kSTLlist:
1959 case ROOT::kSTLdeque:
1960 case ROOT::kROOTRVec:
1961 methodTCP="Pushback";
1962 break;
1964 methodTCP="Pushfront";
1965 break;
1966 case ROOT::kSTLmap:
1967 case ROOT::kSTLmultimap:
1970 methodTCP="MapInsert";
1971 break;
1972 case ROOT::kSTLset:
1973 case ROOT::kSTLmultiset:
1976 methodTCP="Insert";
1977 break;
1978 }
1979 // FIXME Workaround: for the moment we do not generate coll proxies with unique ptrs since
1980 // they imply copies and therefore do not compile.
1981 auto classNameForIO = TClassEdit::GetNameForIO(classname);
1982 finalString << " instance.AdoptCollectionProxyInfo(TCollectionProxyInfo::Generate(TCollectionProxyInfo::" << methodTCP << "< " << classNameForIO.c_str() << " >()));" << "\n";
1983
1984 needCollectionProxy = true;
1985 }
1986
1987 //---------------------------------------------------------------------------
1988 // Register Alternate spelling of the class name.
1989 /////////////////////////////////////////////////////////////////////////////
1990
1991 if (cl.GetRequestedName()[0] && classname != cl.GetRequestedName()) {
1992 finalString << "\n" << " instance.AdoptAlternate(::ROOT::AddClassAlternate(\""
1993 << classname << "\",\"" << cl.GetRequestedName() << "\"));\n";
1994 }
1995
1996 if (!cl.GetDemangledTypeInfo().empty()
1997 && cl.GetDemangledTypeInfo() != classname
1998 && cl.GetDemangledTypeInfo() != cl.GetRequestedName()) {
1999 finalString << "\n" << " instance.AdoptAlternate(::ROOT::AddClassAlternate(\""
2000 << classname << "\",\"" << cl.GetDemangledTypeInfo() << "\"));\n";
2001
2002 }
2003
2004 //---------------------------------------------------------------------------
2005 // Pass the schema evolution rules to TGenericClassInfo
2006 /////////////////////////////////////////////////////////////////////////////
2007
2008 if( (rulesIt1 != ROOT::gReadRules.end() && rulesIt1->second.size()>0) || (rulesIt2 != ROOT::gReadRawRules.end() && rulesIt2->second.size()>0) ) {
2009 finalString << "\n" << " ::ROOT::Internal::TSchemaHelper* rule;" << "\n";
2010 }
2011
2012 if( rulesIt1 != ROOT::gReadRules.end() ) {
2013 finalString << "\n" << " // the io read rules" << "\n" << " std::vector<::ROOT::Internal::TSchemaHelper> readrules(" << rulesIt1->second.size() << ");" << "\n";
2014 ROOT::WriteSchemaList( rulesIt1->second, "readrules", finalString );
2015 finalString << " instance.SetReadRules( readrules );" << "\n";
2016 }
2017
2018 if( rulesIt2 != ROOT::gReadRawRules.end() ) {
2019 finalString << "\n" << " // the io read raw rules" << "\n" << " std::vector<::ROOT::Internal::TSchemaHelper> readrawrules(" << rulesIt2->second.size() << ");" << "\n";
2020 ROOT::WriteSchemaList( rulesIt2->second, "readrawrules", finalString );
2021 finalString << " instance.SetReadRawRules( readrawrules );" << "\n";
2022 }
2023
2024 finalString << " return &instance;" << "\n" << " }" << "\n";
2025
2026 if (!isStdNotString && !ROOT::TMetaUtils::hasOpaqueTypedef(cl, interp, normCtxt)) {
2027 // The GenerateInitInstance for STL are not unique and should not be externally accessible
2028 finalString << " TGenericClassInfo *GenerateInitInstance(const " << csymbol << "*)" << "\n" << " {\n return GenerateInitInstanceLocal(static_cast<" << csymbol << "*>(nullptr));\n }" << "\n";
2029 }
2030
2031 finalString << " // Static variable to force the class initialization" << "\n";
2032 // must be one long line otherwise UseDummy does not work
2033
2034
2035 finalString << " static ::ROOT::TGenericClassInfo *_R__UNIQUE_DICT_(Init) = GenerateInitInstanceLocal(static_cast<const " << csymbol << "*>(nullptr)); R__UseDummy(_R__UNIQUE_DICT_(Init));" << "\n";
2036
2037 if (!ClassInfo__HasMethod(decl,"Dictionary",interp) || IsTemplate(*decl)) {
2038 finalString << "\n" << " // Dictionary for non-ClassDef classes" << "\n"
2039 << " static TClass *" << mappedname << "_Dictionary() {\n"
2040 << " TClass* theClass ="
2041 << "::ROOT::GenerateInitInstanceLocal(static_cast<const " << csymbol << "*>(nullptr))->GetClass();\n"
2042 << " " << mappedname << "_TClassManip(theClass);\n";
2043 finalString << " return theClass;\n";
2044 finalString << " }\n\n";
2045
2046 // Now manipulate tclass in order to percolate the properties expressed as
2047 // annotations of the decls.
2048 std::string manipString;
2049 std::string attribute_s;
2050 std::string attrName, attrValue;
2051 // Class properties
2052 bool attrMapExtracted = false;
2053 if (decl->hasAttrs()){
2054 // Loop on the attributes
2055 for (clang::Decl::attr_iterator attrIt = decl->attr_begin();
2056 attrIt!=decl->attr_end();++attrIt){
2057 if ( 0!=ROOT::TMetaUtils::extractAttrString(*attrIt,attribute_s)){
2058 continue;
2059 }
2060 if (0!=ROOT::TMetaUtils::extractPropertyNameValFromString(attribute_s, attrName, attrValue)){
2061 continue;
2062 }
2063 if (attrName == "name" ||
2064 attrName == "pattern" ||
2065 attrName == "rootmap") continue;
2066 // A general property
2067 // 1) We need to create the property map (in the gen code)
2068 // 2) we need to take out the map (in the gen code)
2069 // 3) We need to bookkep the fact that the map is created and out (in this source)
2070 // 4) We fill the map (in the gen code)
2071 if (!attrMapExtracted){
2072 manipString+=" theClass->CreateAttributeMap();\n";
2073 manipString+=" TDictAttributeMap* attrMap( theClass->GetAttributeMap() );\n";
2074 attrMapExtracted=true;
2075 }
2076 manipString+=" attrMap->AddProperty(\""+attrName +"\",\""+attrValue+"\");\n";
2077 }
2078 } // end of class has properties
2079
2080 // Member properties
2081 // Loop on declarations inside the class, including data members
2082 for(clang::CXXRecordDecl::decl_iterator internalDeclIt = decl->decls_begin();
2083 internalDeclIt != decl->decls_end(); ++internalDeclIt){
2084 if (!(!(*internalDeclIt)->isImplicit()
2085 && (clang::isa<clang::FieldDecl>(*internalDeclIt) ||
2086 clang::isa<clang::VarDecl>(*internalDeclIt)))) continue; // Check if it's a var or a field
2087
2088 // Now let's check the attributes of the var/field
2089 if (!internalDeclIt->hasAttrs()) continue;
2090
2091 attrMapExtracted = false;
2092 bool memberPtrCreated = false;
2093
2094 for (clang::Decl::attr_iterator attrIt = internalDeclIt->attr_begin();
2095 attrIt!=internalDeclIt->attr_end();++attrIt){
2096
2097 // Get the attribute as string
2098 if ( 0!=ROOT::TMetaUtils::extractAttrString(*attrIt,attribute_s)){
2099 continue;
2100 }
2101
2102 // Check the name of the decl
2103 clang::NamedDecl* namedInternalDecl = clang::dyn_cast<clang::NamedDecl> (*internalDeclIt);
2104 if (!namedInternalDecl) {
2105 TMetaUtils::Error(nullptr, "Cannot convert field declaration to clang::NamedDecl");
2106 continue;
2107 }
2108 const std::string memberName(namedInternalDecl->getName());
2109 const std::string cppMemberName = "theMember_"+memberName;
2110
2111 // Prepare a string to get the data member, it can be used later.
2112 const std::string dataMemberCreation= " TDataMember* "+cppMemberName+" = theClass->GetDataMember(\""+memberName+"\");\n";
2113
2114 // Let's now attack regular properties
2115
2116 if (0 != ROOT::TMetaUtils::extractPropertyNameValFromString(attribute_s, attrName, attrValue)) {
2117 continue;
2118 }
2119
2120 // Skip these
2121 if (attrName == propNames::comment ||
2122 attrName == propNames::iotype ||
2123 attrName == propNames::ioname ) continue;
2124
2125 if (!memberPtrCreated){
2126 manipString+=dataMemberCreation;
2127 memberPtrCreated=true;
2128 }
2129
2130 if (!attrMapExtracted){
2131 manipString+=" "+cppMemberName+"->CreateAttributeMap();\n";
2132 manipString+=" TDictAttributeMap* memberAttrMap_"+memberName+"( theMember_"+memberName+"->GetAttributeMap() );\n";
2133 attrMapExtracted=true;
2134 }
2135
2136 manipString+=" memberAttrMap_"+memberName+"->AddProperty(\""+attrName +"\",\""+attrValue+"\");\n";
2137
2138
2139 } // End loop on attributes
2140 } // End loop on internal declarations
2141
2142
2143 finalString << " static void " << mappedname << "_TClassManip(TClass* " << (manipString.empty() ? "":"theClass") << "){\n"
2144 << manipString
2145 << " }\n\n";
2146 } // End of !ClassInfo__HasMethod(decl,"Dictionary") || IsTemplate(*decl))
2147
2148 finalString << "} // end of namespace ROOT" << "\n" << "\n";
2149}
2150
2151////////////////////////////////////////////////////////////////////////////////
2152/// Return true if one of the class' enclosing scope is a namespace and
2153/// set fullname to the fully qualified name,
2154/// clsname to the name within a namespace
2155/// and nsname to the namespace fully qualified name.
2156
2158 std::string &clsname,
2159 std::string &nsname,
2160 const clang::CXXRecordDecl *cl)
2161{
2162 fullname.clear();
2163 nsname.clear();
2164
2166 clsname = fullname;
2167
2168 // Inline namespace are stripped from the normalized name, we need to
2169 // strip it from the prefix we want to remove.
2170 auto ctxt = cl->getEnclosingNamespaceContext();
2171 while(ctxt && ctxt!=cl && ctxt->isInlineNamespace()) {
2172 ctxt = ctxt->getParent();
2173 }
2174 if (ctxt) {
2175 const clang::NamedDecl *namedCtxt = llvm::dyn_cast<clang::NamedDecl>(ctxt);
2176 if (namedCtxt && namedCtxt!=cl) {
2177 const clang::NamespaceDecl *nsdecl = llvm::dyn_cast<clang::NamespaceDecl>(namedCtxt);
2178 if (nsdecl && !nsdecl->isAnonymousNamespace()) {
2179 ROOT::TMetaUtils::GetQualifiedName(nsname,*nsdecl);
2180 clsname.erase (0, nsname.size() + 2);
2181 return true;
2182 }
2183 }
2184 }
2185 return false;
2186}
2187
2188////////////////////////////////////////////////////////////////////////////////
2189
2190const clang::DeclContext *GetEnclosingSpace(const clang::RecordDecl &cl)
2191{
2192 const clang::DeclContext *ctxt = cl.getDeclContext();
2193 while(ctxt && !ctxt->isNamespace()) {
2194 ctxt = ctxt->getParent();
2195 }
2196 return ctxt;
2197}
2198
2199////////////////////////////////////////////////////////////////////////////////
2200/// Write all the necessary opening part of the namespace and
2201/// return the number of closing brackets needed
2202/// For example for Space1::Space2
2203/// we write: namespace Space1 { namespace Space2 {
2204/// and return 2.
2205
2206int ROOT::TMetaUtils::WriteNamespaceHeader(std::ostream &out, const clang::DeclContext *ctxt)
2207{
2208 int closing_brackets = 0;
2209
2210 //fprintf(stderr,"DEBUG: in WriteNamespaceHeader for %s with %s\n",
2211 // cl.Fullname(),namespace_obj.Fullname());
2212 if (ctxt && ctxt->isNamespace()) {
2213 closing_brackets = WriteNamespaceHeader(out,ctxt->getParent());
2214 const clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(ctxt);
2215 if (ns) {
2216 for (int indent = 0; indent < closing_brackets; ++indent)
2217 out << " ";
2218 if (ns->isInline())
2219 out << "inline ";
2220 out << "namespace " << ns->getNameAsString() << " {" << std::endl;
2221 closing_brackets++;
2222 }
2223 }
2224
2225 return closing_brackets;
2226}
2227
2228////////////////////////////////////////////////////////////////////////////////
2229
2230int ROOT::TMetaUtils::WriteNamespaceHeader(std::ostream &out, const clang::RecordDecl *cl)
2231{
2232 return WriteNamespaceHeader(out, GetEnclosingSpace(*cl));
2233}
2234
2235////////////////////////////////////////////////////////////////////////////////
2236
2237bool ROOT::TMetaUtils::NeedTemplateKeyword(const clang::CXXRecordDecl *cl)
2238{
2239 clang::TemplateSpecializationKind kind = cl->getTemplateSpecializationKind();
2240 if (kind == clang::TSK_Undeclared ) {
2241 // Note a template;
2242 return false;
2243 } else if (kind == clang::TSK_ExplicitSpecialization) {
2244 // This is a specialized templated class
2245 return false;
2246 } else {
2247 // This is an automatically or explicitly instantiated templated class.
2248 return true;
2249 }
2250}
2251
2252////////////////////////////////////////////////////////////////////////////////
2253/// return true if we can find a custom operator new with placement
2254
2255bool ROOT::TMetaUtils::HasCustomOperatorNewPlacement(const char *which, const clang::RecordDecl &cl, const cling::Interpreter &interp)
2256{
2257 const char *name = which;
2258 const char *proto = "size_t";
2259 const char *protoPlacement = "size_t,void*";
2260
2261 // First search in the enclosing namespaces
2262 const clang::FunctionDecl *operatornew
2263 = ROOT::TMetaUtils::GetFuncWithProto(llvm::dyn_cast<clang::Decl>(cl.getDeclContext()),
2264 name, proto, interp,
2265 cling::LookupHelper::NoDiagnostics);
2266 const clang::FunctionDecl *operatornewPlacement
2267 = ROOT::TMetaUtils::GetFuncWithProto(llvm::dyn_cast<clang::Decl>(cl.getDeclContext()),
2268 name, protoPlacement, interp,
2269 cling::LookupHelper::NoDiagnostics);
2270
2271 const clang::DeclContext *ctxtnew = nullptr;
2272 const clang::DeclContext *ctxtnewPlacement = nullptr;
2273
2274 if (operatornew) {
2275 ctxtnew = operatornew->getParent();
2276 }
2277 if (operatornewPlacement) {
2278 ctxtnewPlacement = operatornewPlacement->getParent();
2279 }
2280
2281 // Then in the class and base classes
2282 operatornew = ROOT::TMetaUtils::GetFuncWithProto(&cl, name, proto, interp,
2283 false /*diags*/);
2284 operatornewPlacement
2285 = ROOT::TMetaUtils::GetFuncWithProto(&cl, name, protoPlacement, interp,
2286 false /*diags*/);
2287
2288 if (operatornew) {
2289 ctxtnew = operatornew->getParent();
2290 }
2291 if (operatornewPlacement) {
2292 ctxtnewPlacement = operatornewPlacement->getParent();
2293 }
2294
2295 if (!ctxtnewPlacement) {
2296 return false;
2297 }
2298 if (!ctxtnew) {
2299 // Only a new with placement, no hiding
2300 return true;
2301 }
2302 // Both are non zero
2303 if (ctxtnew == ctxtnewPlacement) {
2304 // Same declaration ctxt, no hiding
2305 return true;
2306 }
2307 const clang::CXXRecordDecl* clnew = llvm::dyn_cast<clang::CXXRecordDecl>(ctxtnew);
2308 const clang::CXXRecordDecl* clnewPlacement = llvm::dyn_cast<clang::CXXRecordDecl>(ctxtnewPlacement);
2309 if (!clnew && !clnewPlacement) {
2310 // They are both in different namespaces, I am not sure of the rules.
2311 // we probably ought to find which one is closest ... for now bail
2312 // (because rootcling was also bailing on that).
2313 return true;
2314 }
2315 if (clnew && !clnewPlacement) {
2316 // operator new is class method hiding the outer scope operator new with placement.
2317 return false;
2318 }
2319 if (!clnew && clnewPlacement) {
2320 // operator new is a not class method and can not hide new with placement which is a method
2321 return true;
2322 }
2323 // Both are class methods
2324 if (clnew->isDerivedFrom(clnewPlacement)) {
2325 // operator new is in a more derived part of the hierarchy, it is hiding operator new with placement.
2326 return false;
2327 }
2328 // operator new with placement is in a more derived part of the hierarchy, it can't be hidden by operator new.
2329 return true;
2330}
2331
2332////////////////////////////////////////////////////////////////////////////////
2333/// return true if we can find a custom operator new with placement
2334
2335bool ROOT::TMetaUtils::HasCustomOperatorNewPlacement(const clang::RecordDecl &cl, const cling::Interpreter &interp)
2336{
2337 return HasCustomOperatorNewPlacement("operator new",cl, interp);
2338}
2339
2340////////////////////////////////////////////////////////////////////////////////
2341/// return true if we can find a custom operator new with placement
2342
2343bool ROOT::TMetaUtils::HasCustomOperatorNewArrayPlacement(const clang::RecordDecl &cl, const cling::Interpreter &interp)
2344{
2345 return HasCustomOperatorNewPlacement("operator new[]",cl, interp);
2346}
2347
2348////////////////////////////////////////////////////////////////////////////////
2349/// std::string NormalizedName;
2350/// GetNormalizedName(NormalizedName, decl->getASTContext().getTypeDeclType(decl), interp, normCtxt);
2351
2352void ROOT::TMetaUtils::WriteAuxFunctions(std::ostream& finalString,
2353 const AnnotatedRecordDecl &cl,
2354 const clang::CXXRecordDecl *decl,
2355 const cling::Interpreter &interp,
2356 const RConstructorTypes& ctorTypes,
2357 const TNormalizedCtxt &normCtxt)
2358{
2359 std::string classname = TClassEdit::GetLong64_Name(cl.GetNormalizedName());
2360
2361 std::string mappedname;
2362 ROOT::TMetaUtils::GetCppName(mappedname,classname.c_str());
2363
2364 // Write the functions that are need for the TGenericClassInfo.
2365 // This includes
2366 // IsA
2367 // operator new
2368 // operator new[]
2369 // operator delete
2370 // operator delete[]
2371
2372 ROOT::TMetaUtils::GetCppName(mappedname,classname.c_str());
2373
2374 if ( ! TClassEdit::IsStdClass( classname.c_str() ) ) {
2375
2376 // Prefix the full class name with '::' except for the STL
2377 // containers and std::string. This is to request the
2378 // real class instead of the class in the namespace ROOT::Shadow
2379 classname.insert(0,"::");
2380 }
2381
2382 finalString << "namespace ROOT {" << "\n";
2383
2384 std::string args;
2385 if (HasIOConstructor(decl, args, ctorTypes, interp)) {
2386 // write the constructor wrapper only for concrete classes
2387 finalString << " // Wrappers around operator new" << "\n";
2388 finalString << " static void *new_" << mappedname.c_str() << "(void *p) {" << "\n" << " return p ? ";
2389 if (HasCustomOperatorNewPlacement(*decl, interp)) {
2390 finalString << "new(p) ";
2391 finalString << classname.c_str();
2392 finalString << args;
2393 finalString << " : ";
2394 } else {
2395 finalString << "::new(static_cast<::ROOT::Internal::TOperatorNewHelper*>(p)) ";
2396 finalString << classname.c_str();
2397 finalString << args;
2398 finalString << " : ";
2399 }
2400 finalString << "new " << classname.c_str() << args << ";" << "\n";
2401 finalString << " }" << "\n";
2402
2403 if (args.size()==0 && NeedDestructor(decl, interp)) {
2404 // Can not can newArray if the destructor is not public.
2405 finalString << " static void *newArray_";
2406 finalString << mappedname.c_str();
2407 finalString << "(Long_t nElements, void *p) {";
2408 finalString << "\n";
2409 finalString << " return p ? ";
2410 if (HasCustomOperatorNewArrayPlacement(*decl, interp)) {
2411 finalString << "new(p) ";
2412 finalString << classname.c_str();
2413 finalString << "[nElements] : ";
2414 } else {
2415 finalString << "::new(static_cast<::ROOT::Internal::TOperatorNewHelper*>(p)) ";
2416 finalString << classname.c_str();
2417 finalString << "[nElements] : ";
2418 }
2419 finalString << "new ";
2420 finalString << classname.c_str();
2421 finalString << "[nElements];";
2422 finalString << "\n";
2423 finalString << " }";
2424 finalString << "\n";
2425 }
2426 }
2427
2428 if (NeedDestructor(decl, interp)) {
2429 finalString << " // Wrapper around operator delete" << "\n" << " static void delete_" << mappedname.c_str() << "(void *p) {" << "\n" << " delete (static_cast<" << classname.c_str() << "*>(p));" << "\n" << " }" << "\n" << " static void deleteArray_" << mappedname.c_str() << "(void *p) {" << "\n" << " delete [] (static_cast<" << classname.c_str() << "*>(p));" << "\n" << " }" << "\n" << " static void destruct_" << mappedname.c_str() << "(void *p) {" << "\n" << " typedef " << classname.c_str() << " current_t;" << "\n" << " (static_cast<current_t*>(p))->~current_t();" << "\n" << " }" << "\n";
2430 }
2431
2432 if (HasDirectoryAutoAdd(decl, interp)) {
2433 finalString << " // Wrapper around the directory auto add." << "\n" << " static void directoryAutoAdd_" << mappedname.c_str() << "(void *p, TDirectory *dir) {" << "\n" << " ((" << classname.c_str() << "*)p)->DirectoryAutoAdd(dir);" << "\n" << " }" << "\n";
2434 }
2435
2436 if (HasCustomStreamerMemberFunction(cl, decl, interp, normCtxt)) {
2437 finalString << " // Wrapper around a custom streamer member function." << "\n" << " static void streamer_" << mappedname.c_str() << "(TBuffer &buf, void *obj) {" << "\n" << " ((" << classname.c_str() << "*)obj)->" << classname.c_str() << "::Streamer(buf);" << "\n" << " }" << "\n";
2438 }
2439
2440 if (HasCustomConvStreamerMemberFunction(cl, decl, interp, normCtxt)) {
2441 finalString << " // Wrapper around a custom streamer member function." << "\n" << " static void conv_streamer_" << mappedname.c_str() << "(TBuffer &buf, void *obj, const TClass *onfile_class) {" << "\n" << " ((" << classname.c_str() << "*)obj)->" << classname.c_str() << "::Streamer(buf,onfile_class);" << "\n" << " }" << "\n";
2442 }
2443
2444 if (HasNewMerge(decl, interp)) {
2445 finalString << " // Wrapper around the merge function." << "\n" << " static Long64_t merge_" << mappedname.c_str() << "(void *obj,TCollection *coll,TFileMergeInfo *info) {" << "\n" << " return ((" << classname.c_str() << "*)obj)->Merge(coll,info);" << "\n" << " }" << "\n";
2446 } else if (HasOldMerge(decl, interp)) {
2447 finalString << " // Wrapper around the merge function." << "\n" << " static Long64_t merge_" << mappedname.c_str() << "(void *obj,TCollection *coll,TFileMergeInfo *) {" << "\n" << " return ((" << classname.c_str() << "*)obj)->Merge(coll);" << "\n" << " }" << "\n";
2448 }
2449
2450 if (HasResetAfterMerge(decl, interp)) {
2451 finalString << " // Wrapper around the Reset function." << "\n" << " static void reset_" << mappedname.c_str() << "(void *obj,TFileMergeInfo *info) {" << "\n" << " ((" << classname.c_str() << "*)obj)->ResetAfterMerge(info);" << "\n" << " }" << "\n";
2452 }
2453 finalString << "} // end of namespace ROOT for class " << classname.c_str() << "\n" << "\n";
2454}
2455
2456////////////////////////////////////////////////////////////////////////////////
2457/// Write interface function for STL members
2458
2460 const cling::Interpreter &interp,
2461 const TNormalizedCtxt &normCtxt)
2462{
2463 std::string a;
2464 std::string clName;
2465 TMetaUtils::GetCppName(clName, ROOT::TMetaUtils::GetFileName(*cl.GetRecordDecl(), interp).c_str());
2466 int version = ROOT::TMetaUtils::GetClassVersion(cl.GetRecordDecl(),interp);
2467 if (version == 0) return;
2468 if (version < 0 && !(cl.RequestStreamerInfo()) ) return;
2469
2470
2471 const clang::CXXRecordDecl *clxx = llvm::dyn_cast<clang::CXXRecordDecl>(cl.GetRecordDecl());
2472 if (!clxx) return;
2473
2474 // We also need to look at the base classes.
2475 for(clang::CXXRecordDecl::base_class_const_iterator iter = clxx->bases_begin(), end = clxx->bases_end();
2476 iter != end;
2477 ++iter)
2478 {
2479 int k = ROOT::TMetaUtils::IsSTLContainer(*iter);
2480 if (k!=0) {
2481 Internal::RStl::Instance().GenerateTClassFor( iter->getType(), interp, normCtxt);
2482 }
2483 }
2484
2485 // Loop over the non static data member.
2486 for(clang::RecordDecl::field_iterator field_iter = clxx->field_begin(), end = clxx->field_end();
2487 field_iter != end;
2488 ++field_iter)
2489 {
2490 std::string mTypename;
2491 ROOT::TMetaUtils::GetQualifiedName(mTypename, field_iter->getType(), *clxx);
2492
2493 //member is a string
2494 {
2495 const char*shortTypeName = ROOT::TMetaUtils::ShortTypeName(mTypename.c_str());
2496 if (!strcmp(shortTypeName, "string")) {
2497 continue;
2498 }
2499 }
2500
2501 if (!ROOT::TMetaUtils::IsStreamableObject(**field_iter, interp)) continue;
2502
2503 int k = ROOT::TMetaUtils::IsSTLContainer( **field_iter );
2504 if (k!=0) {
2505 // fprintf(stderr,"Add %s which is also",m.Type()->Name());
2506 // fprintf(stderr," %s\n",R__TrueName(**field_iter) );
2507 clang::QualType utype(ROOT::TMetaUtils::GetUnderlyingType(field_iter->getType()),0);
2508 Internal::RStl::Instance().GenerateTClassFor(utype, interp, normCtxt);
2509 }
2510 }
2511}
2512
2513////////////////////////////////////////////////////////////////////////////////
2514/// TrueName strips the typedefs and array dimensions.
2515
2516std::string ROOT::TMetaUtils::TrueName(const clang::FieldDecl &m)
2517{
2518 const clang::Type *rawtype = m.getType()->getCanonicalTypeInternal().getTypePtr();
2519 if (rawtype->isArrayType()) {
2520 rawtype = rawtype->getBaseElementTypeUnsafe ();
2521 }
2522
2523 std::string result;
2524 ROOT::TMetaUtils::GetQualifiedName(result, clang::QualType(rawtype,0), m);
2525 return result;
2526}
2527
2528////////////////////////////////////////////////////////////////////////////////
2529/// Return the version number of the class or -1
2530/// if the function Class_Version does not exist.
2531
2532int ROOT::TMetaUtils::GetClassVersion(const clang::RecordDecl *cl, const cling::Interpreter& interp)
2533{
2534 const clang::CXXRecordDecl* CRD = llvm::dyn_cast<clang::CXXRecordDecl>(cl);
2535 if (!CRD) {
2536 // Must be an enum or namespace.
2537 // FIXME: Make it work for a namespace!
2538 return -1;
2539 }
2540 const clang::FunctionDecl* funcCV = ROOT::TMetaUtils::ClassInfo__HasMethod(CRD,"Class_Version",interp);
2541
2542 // if we have no Class_Info() return -1.
2543 if (!funcCV) return -1;
2544
2545 // if we have many Class_Info() (?!) return 1.
2546 if (funcCV == (clang::FunctionDecl*)-1) return 1;
2547
2548 return GetTrivialIntegralReturnValue(funcCV, interp).second;
2549}
2550
2551////////////////////////////////////////////////////////////////////////////////
2552/// If the function contains 'just': return SomeValue;
2553/// this routine will extract this value and return it.
2554/// The first element is set to true we have the body of the function and it
2555/// is indeed a trivial function with just a return of a value.
2556/// The second element contains the value (or -1 is case of failure)
2557
2558std::pair<bool, int>
2559ROOT::TMetaUtils::GetTrivialIntegralReturnValue(const clang::FunctionDecl *funcCV, const cling::Interpreter &interp)
2560{
2561 using res_t = std::pair<bool, int>;
2562
2563 const clang::CompoundStmt* FuncBody
2564 = llvm::dyn_cast_or_null<clang::CompoundStmt>(funcCV->getBody());
2565 if (!FuncBody)
2566 return res_t{false, -1};
2567 if (FuncBody->size() != 1) {
2568 // This is a non-ClassDef(), complex function - it might depend on state
2569 // and thus we'll need the runtime and cannot determine the result
2570 // statically.
2571 return res_t{false, -1};
2572 }
2573 const clang::ReturnStmt* RetStmt
2574 = llvm::dyn_cast<clang::ReturnStmt>(FuncBody->body_back());
2575 if (!RetStmt)
2576 return res_t{false, -1};
2577 const clang::Expr* RetExpr = RetStmt->getRetValue();
2578 // ClassDef controls the content of Class_Version() but not the return
2579 // expression which is CPP expanded from what the user provided as second
2580 // ClassDef argument. It's usually just be an integer literal but it could
2581 // also be an enum or a variable template for all we know.
2582 // Go through ICE to be more general.
2583 if (auto RetRes = RetExpr->getIntegerConstantExpr(funcCV->getASTContext())) {
2584 if (RetRes->isSigned())
2585 return res_t{true, (Version_t)RetRes->getSExtValue()};
2586 return res_t{true, (Version_t)RetRes->getZExtValue()};
2587 }
2588 return res_t{false, -1};
2589}
2590
2591////////////////////////////////////////////////////////////////////////////////
2592/// Is this an STL container.
2593
2595{
2596 return TMetaUtils::IsSTLCont(*annotated.GetRecordDecl());
2597}
2598
2599////////////////////////////////////////////////////////////////////////////////
2600/// Is this an STL container?
2601
2603{
2604 clang::QualType type = m.getType();
2605 clang::RecordDecl *decl = ROOT::TMetaUtils::GetUnderlyingRecordDecl(type);
2606
2607 if (decl) return TMetaUtils::IsSTLCont(*decl);
2608 else return ROOT::kNotSTL;
2609}
2610
2611////////////////////////////////////////////////////////////////////////////////
2612/// Is this an STL container?
2613
2614int ROOT::TMetaUtils::IsSTLContainer(const clang::CXXBaseSpecifier &base)
2615{
2616 clang::QualType type = base.getType();
2617 clang::RecordDecl *decl = ROOT::TMetaUtils::GetUnderlyingRecordDecl(type);
2618
2619 if (decl) return TMetaUtils::IsSTLCont(*decl);
2620 else return ROOT::kNotSTL;
2621}
2622
2623////////////////////////////////////////////////////////////////////////////////
2624/// Calls the given lambda on every header in the given module.
2625/// includeDirectlyUsedModules designates if the foreach should also loop over
2626/// the headers in all modules that are directly used via a `use` declaration
2627/// in the modulemap.
2628void ROOT::TMetaUtils::foreachHeaderInModule(const clang::Module &module,
2629 const std::function<void(const clang::Module::Header &)> &closure,
2630 bool includeDirectlyUsedModules)
2631{
2632 // Iterates over all headers in a module and calls the closure on each.
2633
2634 // FIXME: We currently have to hardcode '4' to do this. Maybe we
2635 // will have a nicer way to do this in the future.
2636 // NOTE: This is on purpose '4', not '5' which is the size of the
2637 // vector. The last element is the list of excluded headers which we
2638 // obviously don't want to check here.
2639 const std::size_t publicHeaderIndex = 4;
2640
2641 // Integrity check in case this array changes its size at some point.
2642 const std::size_t maxArrayLength = ((sizeof module.Headers) / (sizeof *module.Headers));
2643 static_assert(publicHeaderIndex + 1 == maxArrayLength,
2644 "'Headers' has changed it's size, we need to update publicHeaderIndex");
2645
2646 // Make a list of modules and submodules that we can check for headers.
2647 // We use a SetVector to prevent an infinite loop in unlikely case the
2648 // modules somehow are messed up and don't form a tree...
2649 llvm::SetVector<const clang::Module *> modules;
2650 modules.insert(&module);
2651 for (size_t i = 0; i < modules.size(); ++i) {
2652 const clang::Module *M = modules[i];
2653 for (const clang::Module *subModule : M->submodules())
2654 modules.insert(subModule);
2655 }
2656
2657 for (const clang::Module *m : modules) {
2658 if (includeDirectlyUsedModules) {
2659 for (clang::Module *used : m->DirectUses) {
2660 foreachHeaderInModule(*used, closure, true);
2661 }
2662 }
2663
2664 for (std::size_t i = 0; i < publicHeaderIndex; i++) {
2665 auto &headerList = m->Headers[i];
2666 for (const clang::Module::Header &moduleHeader : headerList) {
2667 closure(moduleHeader);
2668 }
2669 }
2670 }
2671}
2672
2673////////////////////////////////////////////////////////////////////////////////
2674/// Return the absolute type of typeDesc.
2675/// E.g.: typeDesc = "class TNamed**", returns "TNamed".
2676/// we remove * and const keywords. (we do not want to remove & ).
2677/// You need to use the result immediately before it is being overwritten.
2678
2679const char *ROOT::TMetaUtils::ShortTypeName(const char *typeDesc)
2680{
2681 static char t[4096];
2682 static const char* constwd = "const ";
2683 static const char* constwdend = "const";
2684
2685 const char *s;
2686 char *p=t;
2687 int lev=0;
2688 for (s=typeDesc;*s;s++) {
2689 if (*s=='<') lev++;
2690 if (*s=='>') lev--;
2691 if (lev==0 && *s=='*') continue;
2692 if (lev==0 && (strncmp(constwd,s,strlen(constwd))==0
2693 ||strcmp(constwdend,s)==0 ) ) {
2694 s+=strlen(constwd)-1; // -1 because the loop adds 1
2695 continue;
2696 }
2697 if (lev==0 && *s==' ' && *(s+1)!='*') { p = t; continue;}
2698 if (p - t > (long)sizeof(t)) {
2699 printf("ERROR (rootcling): type name too long for StortTypeName: %s\n",
2700 typeDesc);
2701 p[0] = 0;
2702 return t;
2703 }
2704 *p++ = *s;
2705 }
2706 p[0]=0;
2707
2708 return t;
2709}
2710
2711bool ROOT::TMetaUtils::IsStreamableObject(const clang::FieldDecl &m,
2712 const cling::Interpreter& interp)
2713{
2714 auto comment = ROOT::TMetaUtils::GetComment( m );
2715
2716 // Transient
2717 if (!comment.empty() && comment[0] == '!')
2718 return false;
2719
2720 clang::QualType type = m.getType();
2721
2722 if (type->isReferenceType()) {
2723 // Reference can not be streamed.
2724 return false;
2725 }
2726
2727 std::string mTypeName = type.getAsString(m.getASTContext().getPrintingPolicy());
2728 if (!strcmp(mTypeName.c_str(), "string") || !strcmp(mTypeName.c_str(), "string*")) {
2729 return true;
2730 }
2731 if (!strcmp(mTypeName.c_str(), "std::string") || !strcmp(mTypeName.c_str(), "std::string*")) {
2732 return true;
2733 }
2734
2736 return true;
2737 }
2738
2739 const clang::Type *rawtype = type.getTypePtr()->getBaseElementTypeUnsafe ();
2740
2741 if (rawtype->isPointerType()) {
2742 //Get to the 'raw' type.
2743 clang::QualType pointee;
2744 while ( (pointee = rawtype->getPointeeType()) , pointee.getTypePtrOrNull() && pointee.getTypePtr() != rawtype)
2745 {
2746 rawtype = pointee.getTypePtr();
2747 }
2748 }
2749
2750 if (rawtype->isFundamentalType() || rawtype->isEnumeralType()) {
2751 // not an ojbect.
2752 return false;
2753 }
2754
2755 const clang::CXXRecordDecl *cxxdecl = rawtype->getAsCXXRecordDecl();
2756 if (cxxdecl && ROOT::TMetaUtils::ClassInfo__HasMethod(cxxdecl,"Streamer", interp)) {
2757 if (!(ROOT::TMetaUtils::ClassInfo__HasMethod(cxxdecl,"Class_Version", interp))) return true;
2758 int version = ROOT::TMetaUtils::GetClassVersion(cxxdecl,interp);
2759 if (version > 0) return true;
2760 }
2761 return false;
2762}
2763
2764////////////////////////////////////////////////////////////////////////////////
2765/// Return the absolute type of typeDesc.
2766/// E.g.: typeDesc = "class TNamed**", returns "TNamed".
2767/// we remove * and const keywords. (we do not want to remove & ).
2768/// You need to use the result immediately before it is being overwritten.
2769
2770std::string ROOT::TMetaUtils::ShortTypeName(const clang::FieldDecl &m)
2771{
2772 const clang::Type *rawtype = m.getType().getTypePtr();
2773
2774 //Get to the 'raw' type.
2775 clang::QualType pointee;
2776 while ( rawtype->isPointerType() && ((pointee = rawtype->getPointeeType()) , pointee.getTypePtrOrNull()) && pointee.getTypePtr() != rawtype)
2777 {
2778 rawtype = pointee.getTypePtr();
2779 }
2780
2781 std::string result;
2782 ROOT::TMetaUtils::GetQualifiedName(result, clang::QualType(rawtype,0), m);
2783 return result;
2784}
2785
2786////////////////////////////////////////////////////////////////////////////////
2787
2788clang::RecordDecl *ROOT::TMetaUtils::GetUnderlyingRecordDecl(clang::QualType type)
2789{
2790 const clang::Type *rawtype = ROOT::TMetaUtils::GetUnderlyingType(type);
2791
2792 if (rawtype->isFundamentalType() || rawtype->isEnumeralType()) {
2793 // not an object.
2794 return nullptr;
2795 }
2796 return rawtype->getAsCXXRecordDecl();
2797}
2798
2799////////////////////////////////////////////////////////////////////////////////
2800/// Generate the code of the class
2801/// If the requestor is genreflex, request the new streamer format
2802
2803void ROOT::TMetaUtils::WriteClassCode(CallWriteStreamer_t WriteStreamerFunc,
2804 const AnnotatedRecordDecl &cl,
2805 const cling::Interpreter &interp,
2806 const TNormalizedCtxt &normCtxt,
2807 std::ostream& dictStream,
2808 const RConstructorTypes& ctorTypes,
2809 bool isGenreflex=false)
2810{
2811 const clang::CXXRecordDecl* decl = llvm::dyn_cast<clang::CXXRecordDecl>(cl.GetRecordDecl());
2812
2813 if (!decl || !decl->isCompleteDefinition()) {
2814 return;
2815 }
2816
2817 std::string fullname;
2819 if (TClassEdit::IsSTLCont(fullname) ) {
2820 Internal::RStl::Instance().GenerateTClassFor(cl.GetNormalizedName(), llvm::dyn_cast<clang::CXXRecordDecl>(cl.GetRecordDecl()), interp, normCtxt);
2821 return;
2822 }
2823
2824 if (ROOT::TMetaUtils::ClassInfo__HasMethod(cl,"Streamer",interp)) {
2825 // The !genreflex is there to prevent genreflex to select collections which are data members
2826 // This is to maintain the behaviour of ROOT5 and ROOT6 up to 6.07 included.
2827 if (cl.RootFlag() && !isGenreflex) ROOT::TMetaUtils::WritePointersSTL(cl, interp, normCtxt); // In particular this detect if the class has a version number.
2828 if (!(cl.RequestNoStreamer())) {
2829 (*WriteStreamerFunc)(cl, interp, normCtxt, dictStream, isGenreflex || cl.RequestStreamerInfo());
2830 } else
2831 ROOT::TMetaUtils::Info(nullptr, "Class %s: Do not generate Streamer() [*** custom streamer ***]\n",fullname.c_str());
2832 } else {
2833 ROOT::TMetaUtils::Info(nullptr, "Class %s: Streamer() not declared\n", fullname.c_str());
2834
2835 // See comment above about the !isGenreflex
2836 if (cl.RequestStreamerInfo() && !isGenreflex) ROOT::TMetaUtils::WritePointersSTL(cl, interp, normCtxt);
2837 }
2838 ROOT::TMetaUtils::WriteAuxFunctions(dictStream, cl, decl, interp, ctorTypes, normCtxt);
2839}
2840
2841////////////////////////////////////////////////////////////////////////////////
2842/// Add any unspecified template parameters to the class template instance,
2843/// mentioned anywhere in the type.
2844///
2845/// Note: this does not strip any typedef but could be merged with cling::utils::Transform::GetPartiallyDesugaredType
2846/// if we can safely replace TClassEdit::IsStd with a test on the declaring scope
2847/// and if we can resolve the fact that the added parameter do not take into account possible use/dependences on Double32_t
2848/// and if we decide that adding the default is the right long term solution or not.
2849/// Whether it is or not depend on the I/O on whether the default template argument might change or not
2850/// and whether they (should) affect the on disk layout (for STL containers, we do know they do not).
2851
2852clang::QualType ROOT::TMetaUtils::AddDefaultParameters(clang::QualType instanceType,
2853 const cling::Interpreter &interpreter,
2854 const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
2855{
2856 const clang::ASTContext& Ctx = interpreter.getCI()->getASTContext();
2857
2858 clang::QualType originalType = instanceType;
2859
2860 // In case of name* we need to strip the pointer first, add the default and attach
2861 // the pointer once again.
2862 if (llvm::isa<clang::PointerType>(instanceType.getTypePtr())) {
2863 // Get the qualifiers.
2864 clang::Qualifiers quals = instanceType.getQualifiers();
2865 clang::QualType newPointee = AddDefaultParameters(instanceType->getPointeeType(), interpreter, normCtxt);
2866 if (newPointee != instanceType->getPointeeType()) {
2867 instanceType = Ctx.getPointerType(newPointee);
2868 // Add back the qualifiers.
2869 instanceType = Ctx.getQualifiedType(instanceType, quals);
2870 }
2871 return instanceType;
2872 }
2873
2874 // In case of Int_t& we need to strip the pointer first, desugar and attach
2875 // the pointer once again.
2876 if (llvm::isa<clang::ReferenceType>(instanceType.getTypePtr())) {
2877 // Get the qualifiers.
2878 bool isLValueRefTy = llvm::isa<clang::LValueReferenceType>(instanceType.getTypePtr());
2879 clang::Qualifiers quals = instanceType.getQualifiers();
2880 clang::QualType newPointee = AddDefaultParameters(instanceType->getPointeeType(), interpreter, normCtxt);
2881
2882 if (newPointee != instanceType->getPointeeType()) {
2883 // Add the r- or l- value reference type back to the desugared one
2884 if (isLValueRefTy)
2885 instanceType = Ctx.getLValueReferenceType(newPointee);
2886 else
2887 instanceType = Ctx.getRValueReferenceType(newPointee);
2888 // Add back the qualifiers.
2889 instanceType = Ctx.getQualifiedType(instanceType, quals);
2890 }
2891 return instanceType;
2892 }
2893
2894 // Treat the Scope.
2895 bool prefix_changed = false;
2896 clang::NestedNameSpecifier *prefix = nullptr;
2897 clang::Qualifiers prefix_qualifiers = instanceType.getLocalQualifiers();
2898 const clang::ElaboratedType* etype
2899 = llvm::dyn_cast<clang::ElaboratedType>(instanceType.getTypePtr());
2900 if (etype) {
2901 // We have to also handle the prefix.
2902 prefix = AddDefaultParametersNNS(Ctx, etype->getQualifier(), interpreter, normCtxt);
2903 prefix_changed = prefix != etype->getQualifier();
2904 instanceType = clang::QualType(etype->getNamedType().getTypePtr(),0);
2905 }
2906
2907 // In case of template specializations iterate over the arguments and
2908 // add unspecified default parameter.
2909
2910 const clang::TemplateSpecializationType* TST
2911 = llvm::dyn_cast<const clang::TemplateSpecializationType>(instanceType.getTypePtr());
2912
2913 const clang::ClassTemplateSpecializationDecl* TSTdecl
2914 = llvm::dyn_cast_or_null<const clang::ClassTemplateSpecializationDecl>(instanceType.getTypePtr()->getAsCXXRecordDecl());
2915
2916 // Don't add the default paramater onto std classes.
2917 // We really need this for __shared_ptr which add a enum constant value which
2918 // is spelled in its 'numeral' form and thus the resulting type name is
2919 // incorrect. We also can used this for any of the STL collections where we
2920 // know we don't want the default argument. For the other members of the
2921 // std namespace this is dubious (because TMetaUtils::GetNormalizedName would
2922 // not drop those defaults). [I.e. the real test ought to be is std and
2923 // name is __shared_ptr or vector or list or set or etc.]
2924 bool isStdDropDefault = TSTdecl && IsStdDropDefaultClass(*TSTdecl);
2925
2926 bool mightHaveChanged = false;
2927 if (TST && TSTdecl) {
2928
2929 clang::Sema& S = interpreter.getCI()->getSema();
2930 clang::TemplateDecl *Template = TSTdecl->getSpecializedTemplate()->getMostRecentDecl();
2931 clang::TemplateParameterList *Params = Template->getTemplateParameters();
2932 clang::TemplateParameterList::iterator Param = Params->begin(); // , ParamEnd = Params->end();
2933 //llvm::SmallVectorImpl<TemplateArgument> Converted; // Need to contains the other arguments.
2934 // Converted seems to be the same as our 'desArgs'
2935
2936 unsigned int dropDefault = normCtxt.GetConfig().DropDefaultArg(*Template);
2937
2938 llvm::SmallVector<clang::TemplateArgument, 4> desArgs;
2939 llvm::SmallVector<clang::TemplateArgument, 4> canonArgs;
2940 llvm::ArrayRef<clang::TemplateArgument> template_arguments = TST->template_arguments();
2941 unsigned int Idecl = 0, Edecl = TSTdecl->getTemplateArgs().size();
2942 unsigned int maxAddArg = TSTdecl->getTemplateArgs().size() - dropDefault;
2943 for (const clang::TemplateArgument *I = template_arguments.begin(), *E = template_arguments.end(); Idecl != Edecl;
2944 I != E ? ++I : nullptr, ++Idecl, ++Param) {
2945
2946 if (I != E) {
2947
2948 if (I->getKind() == clang::TemplateArgument::Template) {
2949 clang::TemplateName templateName = I->getAsTemplate();
2950 clang::TemplateDecl* templateDecl = templateName.getAsTemplateDecl();
2951 if (templateDecl) {
2952 clang::DeclContext* declCtxt = templateDecl->getDeclContext();
2953
2954 if (declCtxt && !templateName.getAsQualifiedTemplateName()){
2955 clang::NamespaceDecl* ns = clang::dyn_cast<clang::NamespaceDecl>(declCtxt);
2956 clang::NestedNameSpecifier* nns;
2957 if (ns) {
2958 nns = cling::utils::TypeName::CreateNestedNameSpecifier(Ctx, ns);
2959 } else if (clang::TagDecl* TD = llvm::dyn_cast<clang::TagDecl>(declCtxt)) {
2960 nns = cling::utils::TypeName::CreateNestedNameSpecifier(Ctx,TD, false /*FullyQualified*/);
2961 } else {
2962 // TU scope
2963 desArgs.push_back(*I);
2964 continue;
2965 }
2966 clang::TemplateName UnderlyingTN(templateDecl);
2967 if (clang::UsingShadowDecl *USD = templateName.getAsUsingShadowDecl())
2968 UnderlyingTN = clang::TemplateName(USD);
2969 clang::TemplateName templateNameWithNSS ( Ctx.getQualifiedTemplateName(nns, false, UnderlyingTN) );
2970 desArgs.push_back(clang::TemplateArgument(templateNameWithNSS));
2971 mightHaveChanged = true;
2972 continue;
2973 }
2974 }
2975 }
2976
2977 if (I->getKind() != clang::TemplateArgument::Type) {
2978 desArgs.push_back(*I);
2979 continue;
2980 }
2981
2982 clang::QualType SubTy = I->getAsType();
2983
2984 // Check if the type needs more desugaring and recurse.
2985 // (Originally this was limited to elaborated and templated type,
2986 // but we also need to do it for pointer and reference type
2987 // and who knows what, so do it always)
2988 clang::QualType newSubTy = AddDefaultParameters(SubTy,
2989 interpreter,
2990 normCtxt);
2991 if (SubTy != newSubTy) {
2992 mightHaveChanged = true;
2993 desArgs.push_back(clang::TemplateArgument(newSubTy));
2994 } else {
2995 desArgs.push_back(*I);
2996 }
2997 // Converted.push_back(TemplateArgument(ArgTypeForTemplate));
2998 } else if (!isStdDropDefault && Idecl < maxAddArg) {
2999
3000 mightHaveChanged = true;
3001
3002 const clang::TemplateArgument& templateArg
3003 = TSTdecl->getTemplateArgs().get(Idecl);
3004 if (templateArg.getKind() != clang::TemplateArgument::Type) {
3005 desArgs.push_back(templateArg);
3006 continue;
3007 }
3008 clang::QualType SubTy = templateArg.getAsType();
3009
3010 clang::SourceLocation TemplateLoc = Template->getSourceRange ().getBegin(); //NOTE: not sure that this is the 'right' location.
3011 clang::SourceLocation RAngleLoc = TSTdecl->getSourceRange().getBegin(); // NOTE: most likely wrong, I think this is expecting the location of right angle
3012
3013 clang::TemplateTypeParmDecl *TTP = llvm::dyn_cast<clang::TemplateTypeParmDecl>(*Param);
3014 {
3015 // We may induce template instantiation
3016 cling::Interpreter::PushTransactionRAII clingRAII(const_cast<cling::Interpreter*>(&interpreter));
3017 bool HasDefaultArgs;
3018 clang::TemplateArgumentLoc ArgType = S.SubstDefaultTemplateArgumentIfAvailable(
3019 Template,
3020 TemplateLoc,
3021 RAngleLoc,
3022 TTP,
3023 desArgs,
3024 canonArgs,
3025 HasDefaultArgs);
3026 // The substition can fail, in which case there would have been compilation
3027 // error printed on the screen.
3028 if (ArgType.getArgument().isNull()
3029 || ArgType.getArgument().getKind() != clang::TemplateArgument::Type) {
3030 ROOT::TMetaUtils::Error("ROOT::TMetaUtils::AddDefaultParameters",
3031 "Template parameter substitution failed for %s around %s\n",
3032 instanceType.getAsString().c_str(), SubTy.getAsString().c_str());
3033 break;
3034 }
3035 clang::QualType BetterSubTy = ArgType.getArgument().getAsType();
3036 SubTy = cling::utils::Transform::GetPartiallyDesugaredType(Ctx,BetterSubTy,normCtxt.GetConfig(),/*fullyQualified=*/ true);
3037 }
3038 SubTy = AddDefaultParameters(SubTy,interpreter,normCtxt);
3039 desArgs.push_back(clang::TemplateArgument(SubTy));
3040 } else {
3041 // We are past the end of the list of specified arguements and we
3042 // do not want to add the default, no need to continue.
3043 break;
3044 }
3045 }
3046
3047 // If we added default parameter, allocate new type in the AST.
3048 if (mightHaveChanged) {
3049 instanceType = Ctx.getTemplateSpecializationType(TST->getTemplateName(),
3050 desArgs,
3051 TST->getCanonicalTypeInternal());
3052 }
3053 }
3054
3055 if (!prefix_changed && !mightHaveChanged) return originalType;
3056 if (prefix) {
3057 instanceType = Ctx.getElaboratedType(clang::ETK_None,prefix,instanceType);
3058 instanceType = Ctx.getQualifiedType(instanceType,prefix_qualifiers);
3059 }
3060 return instanceType;
3061}
3062
3063////////////////////////////////////////////////////////////////////////////////
3064/// ValidArrayIndex return a static string (so use it or copy it immediatly, do not
3065/// call GrabIndex twice in the same expression) containing the size of the
3066/// array data member.
3067/// In case of error, or if the size is not specified, GrabIndex returns 0.
3068/// If errnum is not null, *errnum updated with the error number:
3069/// Cint::G__DataMemberInfo::G__VALID : valid array index
3070/// Cint::G__DataMemberInfo::G__NOT_INT : array index is not an int
3071/// Cint::G__DataMemberInfo::G__NOT_DEF : index not defined before array
3072/// (this IS an error for streaming to disk)
3073/// Cint::G__DataMemberInfo::G__IS_PRIVATE: index exist in a parent class but is private
3074/// Cint::G__DataMemberInfo::G__UNKNOWN : index is not known
3075/// If errstr is not null, *errstr is updated with the address of a static
3076/// string containing the part of the index with is invalid.
3077
3078llvm::StringRef ROOT::TMetaUtils::DataMemberInfo__ValidArrayIndex(const cling::Interpreter &interp, const clang::DeclaratorDecl &m, int *errnum, llvm::StringRef *errstr)
3079{
3080 llvm::StringRef title;
3081
3082 // Try to get the comment either from the annotation or the header file if present
3083 if (clang::AnnotateAttr *A = m.getAttr<clang::AnnotateAttr>())
3084 title = A->getAnnotation();
3085 else
3086 // Try to get the comment from the header file if present
3088
3089 // Let's see if the user provided us with some information
3090 // with the format: //[dimension] this is the dim of the array
3091 // dimension can be an arithmetical expression containing, literal integer,
3092 // the operator *,+ and - and data member of integral type. In addition the
3093 // data members used for the size of the array need to be defined prior to
3094 // the array.
3095
3096 if (errnum) *errnum = VALID;
3097
3098 if (title.size() == 0 || (title[0] != '[')) return llvm::StringRef();
3099 size_t rightbracket = title.find(']');
3100 if (rightbracket == llvm::StringRef::npos) return llvm::StringRef();
3101
3102 std::string working;
3103 llvm::StringRef indexvar(title.data()+1,rightbracket-1);
3104
3105 // now we should have indexvar=dimension
3106 // Let's see if this is legal.
3107 // which means a combination of data member and digit separated by '*','+','-'
3108 // First we remove white spaces.
3109 unsigned int i;
3110 size_t indexvarlen = indexvar.size();
3111 for ( i=0; i<indexvarlen; i++) {
3112 if (!isspace(indexvar[i])) {
3113 working += indexvar[i];
3114 }
3115 }
3116
3117 // Now we go through all indentifiers
3118 const char *tokenlist = "*+-";
3119 char *current = const_cast<char*>(working.c_str());
3120 current = strtok(current,tokenlist); // this method does not need to be reentrant
3121
3122 while (current) {
3123 // Check the token
3124 if (isdigit(current[0])) {
3125 for(i=0;i<strlen(current);i++) {
3126 if (!isdigit(current[i])) {
3127 // Error we only access integer.
3128 //NOTE: *** Need to print an error;
3129 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) is not an interger\n",
3130 // member.MemberOf()->Name(), member.Name(), current);
3131 if (errstr) *errstr = current;
3132 if (errnum) *errnum = NOT_INT;
3133 return llvm::StringRef();
3134 }
3135 }
3136 } else { // current token is not a digit
3137 // first let's see if it is a data member:
3138 const clang::CXXRecordDecl *parent_clxx = llvm::dyn_cast<clang::CXXRecordDecl>(m.getDeclContext());
3139 const clang::FieldDecl *index1 = nullptr;
3140 if (parent_clxx)
3141 index1 = GetDataMemberFromAll(*parent_clxx, current );
3142 if ( index1 ) {
3143 if ( IsFieldDeclInt(index1) ) {
3144 // Let's see if it has already been written down in the
3145 // Streamer.
3146 // Let's see if we already wrote it down in the
3147 // streamer.
3148 for(clang::RecordDecl::field_iterator field_iter = parent_clxx->field_begin(), end = parent_clxx->field_end();
3149 field_iter != end;
3150 ++field_iter)
3151 {
3152 if ( field_iter->getNameAsString() == m.getNameAsString() ) {
3153 // we reached the current data member before
3154 // reaching the index so we have not written it yet!
3155 //NOTE: *** Need to print an error;
3156 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) has not been defined before the array \n",
3157 // member.MemberOf()->Name(), member.Name(), current);
3158 if (errstr) *errstr = current;
3159 if (errnum) *errnum = NOT_DEF;
3160 return llvm::StringRef();
3161 }
3162 if ( field_iter->getNameAsString() == index1->getNameAsString() ) {
3163 break;
3164 }
3165 } // end of while (m_local.Next())
3166 } else {
3167 //NOTE: *** Need to print an error;
3168 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) is not int \n",
3169 // member.MemberOf()->Name(), member.Name(), current);
3170 if (errstr) *errstr = current;
3171 if (errnum) *errnum = NOT_INT;
3172 return llvm::StringRef();
3173 }
3174 } else {
3175 // There is no variable by this name in this class, let see
3176 // the base classes!:
3177 int found = 0;
3178 if (parent_clxx) {
3179 clang::Sema& SemaR = const_cast<cling::Interpreter&>(interp).getSema();
3180 index1 = GetDataMemberFromAllParents(SemaR, *parent_clxx, current);
3181 }
3182 if ( index1 ) {
3183 if ( IsFieldDeclInt(index1) ) {
3184 found = 1;
3185 } else {
3186 // We found a data member but it is the wrong type
3187 //NOTE: *** Need to print an error;
3188 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) is not int \n",
3189 // member.MemberOf()->Name(), member.Name(), current);
3190 if (errnum) *errnum = NOT_INT;
3191 if (errstr) *errstr = current;
3192 //NOTE: *** Need to print an error;
3193 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) is not int \n",
3194 // member.MemberOf()->Name(), member.Name(), current);
3195 if (errnum) *errnum = NOT_INT;
3196 if (errstr) *errstr = current;
3197 return llvm::StringRef();
3198 }
3199 if ( found && (index1->getAccess() == clang::AS_private) ) {
3200 //NOTE: *** Need to print an error;
3201 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) is a private member of %s \n",
3202 if (errstr) *errstr = current;
3203 if (errnum) *errnum = IS_PRIVATE;
3204 return llvm::StringRef();
3205 }
3206 }
3207 if (!found) {
3208 //NOTE: *** Need to print an error;
3209 //fprintf(stderr,"*** Datamember %s::%s: size of array (%s) is not known \n",
3210 // member.MemberOf()->Name(), member.Name(), indexvar);
3211 if (errstr) *errstr = indexvar;
3212 if (errnum) *errnum = UNKNOWN;
3213 return llvm::StringRef();
3214 } // end of if not found
3215 } // end of if is a data member of the class
3216 } // end of if isdigit
3217
3218 current = strtok(nullptr, tokenlist);
3219 } // end of while loop on tokens
3220
3221 return indexvar;
3222
3223}
3224
3225////////////////////////////////////////////////////////////////////////////////
3226/// Return (in the argument 'output') a mangled version of the C++ symbol/type (pass as 'input')
3227/// that can be used in C++ as a variable name.
3228
3229void ROOT::TMetaUtils::GetCppName(std::string &out, const char *in)
3230{
3231 unsigned int i = 0;
3232 char c;
3233 out.clear();
3234 while((c = in[i++])) {
3235 const char *repl = nullptr;
3236 switch(c) {
3237 case '+': repl = "pL"; break;
3238 case '-': repl = "mI"; break;
3239 case '*': repl = "mU"; break;
3240 case '/': repl = "dI"; break;
3241 case '&': repl = "aN"; break;
3242 case '%': repl = "pE"; break;
3243 case '|': repl = "oR"; break;
3244 case '^': repl = "hA"; break;
3245 case '>': repl = "gR"; break;
3246 case '<': repl = "lE"; break;
3247 case '=': repl = "eQ"; break;
3248 case '~': repl = "wA"; break;
3249 case '.': repl = "dO"; break;
3250 case '(': repl = "oP"; break;
3251 case ')': repl = "cP"; break;
3252 case '[': repl = "oB"; break;
3253 case ']': repl = "cB"; break;
3254 case '!': repl = "nO"; break;
3255 case ',': repl = "cO"; break;
3256 case '$': repl = "dA"; break;
3257 case ' ': repl = "sP"; break;
3258 case ':': repl = "cL"; break;
3259 case '"': repl = "dQ"; break;
3260 case '@': repl = "aT"; break;
3261 case '\'': repl = "sQ"; break;
3262 case '\\': repl = "fI"; break;
3263 }
3264 if (repl)
3265 out.append(repl);
3266 else
3267 out.push_back(c);
3268 }
3269
3270 // Remove initial numbers if any
3271 auto firstNonNumber = out.find_first_not_of("0123456789");
3272 if (firstNonNumber != std::string::npos)
3273 out.replace(0,firstNonNumber,"");
3274}
3275
3276static clang::SourceLocation
3277getFinalSpellingLoc(clang::SourceManager& sourceManager,
3278 clang::SourceLocation sourceLoc) {
3279 // Follow macro expansion until we hit a source file.
3280 if (!sourceLoc.isFileID()) {
3281 return sourceManager.getExpansionRange(sourceLoc).getEnd();
3282 }
3283 return sourceLoc;
3284}
3285
3286////////////////////////////////////////////////////////////////////////////////
3287/// Return the header file to be included to declare the Decl.
3288
3289std::string ROOT::TMetaUtils::GetFileName(const clang::Decl& decl,
3290 const cling::Interpreter& interp)
3291{
3292 // It looks like the template specialization decl actually contains _less_ information
3293 // on the location of the code than the decl (in case where there is forward declaration,
3294 // that is what the specialization points to).
3295 //
3296 // const clang::CXXRecordDecl* clxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
3297 // if (clxx) {
3298 // switch(clxx->getTemplateSpecializationKind()) {
3299 // case clang::TSK_Undeclared:
3300 // // We want the default behavior
3301 // break;
3302 // case clang::TSK_ExplicitInstantiationDeclaration:
3303 // case clang::TSK_ExplicitInstantiationDefinition:
3304 // case clang::TSK_ImplicitInstantiation: {
3305 // // We want the location of the template declaration:
3306 // const clang::ClassTemplateSpecializationDecl *tmplt_specialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl> (clxx);
3307 // if (tmplt_specialization) {
3308 // // return GetFileName(const_cast< clang::ClassTemplateSpecializationDecl *>(tmplt_specialization)->getSpecializedTemplate());
3309 // }
3310 // break;
3311 // }
3312 // case clang::TSK_ExplicitSpecialization:
3313 // // We want the default behavior
3314 // break;
3315 // default:
3316 // break;
3317 // }
3318 // }
3319
3320 using namespace clang;
3321 SourceLocation headerLoc = decl.getLocation();
3322
3323 static const char invalidFilename[] = "";
3324 if (!headerLoc.isValid()) return invalidFilename;
3325
3326 HeaderSearch& HdrSearch = interp.getCI()->getPreprocessor().getHeaderSearchInfo();
3327
3328 SourceManager& sourceManager = decl.getASTContext().getSourceManager();
3329 headerLoc = getFinalSpellingLoc(sourceManager, headerLoc);
3330 FileID headerFID = sourceManager.getFileID(headerLoc);
3331 SourceLocation includeLoc
3332 = getFinalSpellingLoc(sourceManager,
3333 sourceManager.getIncludeLoc(headerFID));
3334
3335 const FileEntry *headerFE = sourceManager.getFileEntryForID(headerFID);
3336 while (includeLoc.isValid() && sourceManager.isInSystemHeader(includeLoc)) {
3337 ConstSearchDirIterator *foundDir = nullptr;
3338 // use HeaderSearch on the basename, to make sure it takes a header from
3339 // the include path (e.g. not from /usr/include/bits/)
3340 assert(headerFE && "Couldn't find FileEntry from FID!");
3341 auto FEhdr
3342 = HdrSearch.LookupFile(llvm::sys::path::filename(headerFE->getName()),
3343 SourceLocation(),
3344 true /*isAngled*/, nullptr/*FromDir*/, foundDir,
3345 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>>(),
3346 nullptr/*Searchpath*/, nullptr/*RelPath*/,
3347 nullptr/*SuggestedModule*/, nullptr/*RequestingModule*/,
3348 nullptr/*IsMapped*/, nullptr /*IsFrameworkFound*/,
3349 false /*SkipCache*/,
3350 false /*BuildSystemModule*/,
3351 false /*OpenFile*/, true /*CacheFailures*/);
3352 if (FEhdr) break;
3353 headerFID = sourceManager.getFileID(includeLoc);
3354 headerFE = sourceManager.getFileEntryForID(headerFID);
3355 // If we have a system header in a module we can't just trace back the
3356 // original include with the preprocessor. But it should be enough if
3357 // we trace it back to the top-level system header that includes this
3358 // declaration.
3359 if (interp.getCI()->getLangOpts().Modules && !headerFE) {
3360 assert(decl.isFirstDecl() && "Couldn't trace back include from a decl"
3361 " that is not from an AST file");
3362 assert(StringRef(includeLoc.printToString(sourceManager)).startswith("<module-includes>"));
3363 break;
3364 }
3365 includeLoc = getFinalSpellingLoc(sourceManager,
3366 sourceManager.getIncludeLoc(headerFID));
3367 }
3368
3369 if (!headerFE) return invalidFilename;
3370
3371 llvm::SmallString<256> headerFileName(headerFE->getName());
3372 // Remove double ../ from the path so that the search below finds a valid
3373 // longest match and does not result in growing paths.
3374 llvm::sys::path::remove_dots(headerFileName, /*remove_dot_dot=*/true);
3375
3376 // Now headerFID references the last valid system header or the original
3377 // user file.
3378 // Find out how to include it by matching file name to include paths.
3379 // We assume that the file "/A/B/C/D.h" can at some level be included as
3380 // "C/D.h". Be we cannot know whether that happens to be a different file
3381 // with the same name. Thus we first find the longest stem that can be
3382 // reached, say B/C/D.h. Then we find the shortest one, say C/D.h, that
3383 // points to the same file as the long version. If such a short version
3384 // exists it will be returned. If it doesn't the long version is returned.
3385 bool isAbsolute = llvm::sys::path::is_absolute(headerFileName);
3386 clang::OptionalFileEntryRef FELong;
3387 // Find the longest available match.
3388 for (llvm::sys::path::const_iterator
3389 IDir = llvm::sys::path::begin(headerFileName),
3390 EDir = llvm::sys::path::end(headerFileName);
3391 !FELong && IDir != EDir; ++IDir) {
3392 if (isAbsolute) {
3393 // skip "/" part
3394 isAbsolute = false;
3395 continue;
3396 }
3397 size_t lenTrailing = headerFileName.size() - (IDir->data() - headerFileName.data());
3398 llvm::StringRef trailingPart(IDir->data(), lenTrailing);
3399 assert(trailingPart.data() + trailingPart.size()
3400 == headerFileName.data() + headerFileName.size()
3401 && "Mismatched partitioning of file name!");
3402 ConstSearchDirIterator* FoundDir = nullptr;
3403 FELong = HdrSearch.LookupFile(trailingPart, SourceLocation(),
3404 true /*isAngled*/, nullptr/*FromDir*/, FoundDir,
3405 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>>(),
3406 nullptr/*Searchpath*/, nullptr/*RelPath*/,
3407 nullptr/*SuggestedModule*/, nullptr/*RequestingModule*/,
3408 nullptr/*IsMapped*/, nullptr /*IsFrameworkFound*/);
3409 }
3410
3411 if (!FELong) {
3412 // We did not find any file part in any search path.
3413 return invalidFilename;
3414 }
3415
3416 // Iterates through path *parts* "C"; we need trailing parts "C/D.h"
3417 for (llvm::sys::path::reverse_iterator
3418 IDir = llvm::sys::path::rbegin(headerFileName),
3419 EDir = llvm::sys::path::rend(headerFileName);
3420 IDir != EDir; ++IDir) {
3421 size_t lenTrailing = headerFileName.size() - (IDir->data() - headerFileName.data());
3422 llvm::StringRef trailingPart(IDir->data(), lenTrailing);
3423 assert(trailingPart.data() + trailingPart.size()
3424 == headerFileName.data() + headerFileName.size()
3425 && "Mismatched partitioning of file name!");
3426 ConstSearchDirIterator* FoundDir = nullptr;
3427 // Can we find it, and is it the same file as the long version?
3428 // (or are we back to the previously found spelling, which is fine, too)
3429 if (HdrSearch.LookupFile(trailingPart, SourceLocation(),
3430 true /*isAngled*/, nullptr/*FromDir*/, FoundDir,
3431 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>>(),
3432 nullptr/*Searchpath*/, nullptr/*RelPath*/,
3433 nullptr/*SuggestedModule*/, nullptr/*RequestingModule*/,
3434 nullptr/*IsMapped*/, nullptr /*IsFrameworkFound*/) == FELong) {
3435 return trailingPart.str();
3436 }
3437 }
3438
3439 return invalidFilename;
3440}
3441
3442////////////////////////////////////////////////////////////////////////////////
3443
3445 const clang::QualType &qtype,
3446 const clang::ASTContext &astContext)
3447{
3448 std::string fqname = cling::utils::TypeName::GetFullyQualifiedName(qtype, astContext);
3449 TClassEdit::TSplitType splitname(fqname.c_str(),
3452}
3453
3454////////////////////////////////////////////////////////////////////////////////
3455
3457 const clang::QualType &qtype,
3458 const cling::Interpreter &interpreter)
3459{
3460 // We need this because GetFullyQualifiedTypeName is triggering deserialization
3461 // This calling the same name function GetFullyQualifiedTypeName, but this should stay here because
3462 // callee doesn't have an interpreter pointer
3463 cling::Interpreter::PushTransactionRAII RAII(const_cast<cling::Interpreter*>(&interpreter));
3464
3465 GetFullyQualifiedTypeName(typenamestr,
3466 qtype,
3467 interpreter.getCI()->getASTContext());
3468}
3469
3470////////////////////////////////////////////////////////////////////////////////
3471/// Get the template specialisation decl and template decl behind the qualtype
3472/// Returns true if successfully found, false otherwise
3473
3474bool ROOT::TMetaUtils::QualType2Template(const clang::QualType& qt,
3475 clang::ClassTemplateDecl*& ctd,
3476 clang::ClassTemplateSpecializationDecl*& ctsd)
3477{
3478 using namespace clang;
3479 const Type* theType = qt.getTypePtr();
3480 if (!theType){
3481 ctd=nullptr;
3482 ctsd=nullptr;
3483 return false;
3484 }
3485
3486 if (theType->isPointerType()) {
3487 return QualType2Template(theType->getPointeeType(), ctd, ctsd);
3488 }
3489
3490 if (const RecordType* rType = llvm::dyn_cast<RecordType>(theType)) {
3491 ctsd = llvm::dyn_cast_or_null<ClassTemplateSpecializationDecl>(rType->getDecl());
3492 if (ctsd) {
3493 ctd = ctsd->getSpecializedTemplate();
3494 return true;
3495 }
3496 }
3497
3498 if (const SubstTemplateTypeParmType* sttpType = llvm::dyn_cast<SubstTemplateTypeParmType>(theType)){
3499 return QualType2Template(sttpType->getReplacementType(), ctd, ctsd);
3500 }
3501
3502
3503 ctsd = llvm::dyn_cast_or_null<ClassTemplateSpecializationDecl>(qt->getAsCXXRecordDecl());
3504 if(ctsd){
3505 ctd = ctsd->getSpecializedTemplate();
3506 return true;
3507 }
3508
3509 ctd=nullptr;
3510 ctsd=nullptr;
3511 return false;
3512}
3513
3514////////////////////////////////////////////////////////////////////////////////
3515/// Extract from a qualtype the class template if this makes sense.
3516/// Retuns the ClassTemplateDecl or nullptr otherwise.
3517
3518clang::ClassTemplateDecl* ROOT::TMetaUtils::QualType2ClassTemplateDecl(const clang::QualType& qt)
3519{
3520 using namespace clang;
3521 ClassTemplateSpecializationDecl* ctsd;
3522 ClassTemplateDecl* ctd;
3523 QualType2Template(qt,ctd,ctsd);
3524 return ctd;
3525}
3526
3527////////////////////////////////////////////////////////////////////////////////
3528/// These manipulations are necessary because a template specialisation type
3529/// does not inherit from a record type (there is an asymmetry between
3530/// the decls and the types in the clang interface).
3531/// We may need therefore to step into the "Decl dimension" to then get back
3532/// to the "type dimension".
3533
3534clang::TemplateName ROOT::TMetaUtils::ExtractTemplateNameFromQualType(const clang::QualType& qt)
3535{
3536 using namespace clang;
3537 TemplateName theTemplateName;
3538
3539 const Type* theType = qt.getTypePtr();
3540
3541 if (const TemplateSpecializationType* tst = llvm::dyn_cast_or_null<const TemplateSpecializationType>(theType)) {
3542 theTemplateName = tst->getTemplateName();
3543 } // We step into the decl dimension
3544 else if (ClassTemplateDecl* ctd = QualType2ClassTemplateDecl(qt)) {
3545 theTemplateName = TemplateName(ctd);
3546 }
3547
3548 return theTemplateName;
3549}
3550
3551////////////////////////////////////////////////////////////////////////////////
3552
3553static bool areEqualTypes(const clang::TemplateArgument& tArg,
3554 llvm::SmallVectorImpl<clang::TemplateArgument>& preceedingTArgs,
3555 const clang::NamedDecl& tPar,
3556 const cling::Interpreter& interp,
3557 const ROOT::TMetaUtils::TNormalizedCtxt& normCtxt)
3558{
3559 using namespace ROOT::TMetaUtils;
3560 using namespace clang;
3561
3562 // Check if this is a type for security
3563 TemplateTypeParmDecl* ttpdPtr = const_cast<TemplateTypeParmDecl*>(llvm::dyn_cast<TemplateTypeParmDecl>(&tPar));
3564 if (!ttpdPtr) return false;
3565 if (!ttpdPtr->hasDefaultArgument()) return false; // we should not be here in this case, but we protect us.
3566
3567 // Try the fast solution
3568 QualType tParQualType = ttpdPtr->getDefaultArgument();
3569 const QualType tArgQualType = tArg.getAsType();
3570
3571 // Now the equality tests for non template specialisations.
3572
3573 // The easy cases:
3574 // template <class T=double> class A; or
3575 // template <class T=A<float>> class B;
3576 if (tParQualType.getTypePtr() == tArgQualType.getTypePtr()) return true;
3577
3578 // Here the difficulty comes. We have to check if the argument is equal to its
3579 // default. We can do that bootstrapping an argument which has the default value
3580 // based on the preceeding arguments.
3581 // Basically we ask sema to give us the value of the argument given the template
3582 // of behind the parameter and the all the arguments.
3583 // So:
3584
3585 // Take the template out of the parameter
3586
3587 const clang::ElaboratedType* etype
3588 = llvm::dyn_cast<clang::ElaboratedType>(tParQualType.getTypePtr());
3589 while (etype) {
3590 tParQualType = clang::QualType(etype->getNamedType().getTypePtr(),0);
3591 etype = llvm::dyn_cast<clang::ElaboratedType>(tParQualType.getTypePtr());
3592 }
3593
3594 const TemplateSpecializationType* tst =
3595 llvm::dyn_cast<TemplateSpecializationType>(tParQualType.getTypePtr());
3596
3597 if(!tst) // nothing more to be tried. They are different indeed.
3598 return false;
3599
3600 ClassTemplateSpecializationDecl* TSTdecl
3601 = llvm::dyn_cast_or_null<ClassTemplateSpecializationDecl>(tArgQualType->getAsCXXRecordDecl());
3602
3603 if(!TSTdecl) // nothing more to be tried. They are different indeed.
3604 return false;
3605
3606 TemplateDecl *Template = tst->getTemplateName().getAsTemplateDecl();
3607
3608 // Take the template location
3609 SourceLocation TemplateLoc = Template->getSourceRange ().getBegin();
3610
3611 // Get the position of the "<" (LA) of the specializaion
3612 SourceLocation LAngleLoc = TSTdecl->getSourceRange().getBegin();
3613
3614
3615 // Enclose in a scope for the RAII
3616 bool isEqual=false;
3617 TemplateArgument newArg = tArg;
3618 {
3619 clang::Sema& S = interp.getCI()->getSema();
3620 cling::Interpreter::PushTransactionRAII clingRAII(const_cast<cling::Interpreter*>(&interp));
3621 llvm::SmallVector<clang::TemplateArgument, 4> canonArgs;
3622 bool HasDefaultArgs;
3623 TemplateArgumentLoc defTArgLoc = S.SubstDefaultTemplateArgumentIfAvailable(Template,
3624 TemplateLoc,
3625 LAngleLoc,
3626 ttpdPtr,
3627 preceedingTArgs,
3628 canonArgs,
3629 HasDefaultArgs);
3630 // The substition can fail, in which case there would have been compilation
3631 // error printed on the screen.
3632 newArg = defTArgLoc.getArgument();
3633 if (newArg.isNull() ||
3634 newArg.getKind() != clang::TemplateArgument::Type) {
3635 ROOT::TMetaUtils::Error("areEqualTypes",
3636 "Template parameter substitution failed!");
3637 }
3638
3639 ClassTemplateSpecializationDecl* nTSTdecl
3640 = llvm::dyn_cast_or_null<ClassTemplateSpecializationDecl>(newArg.getAsType()->getAsCXXRecordDecl());
3641// std::cout << "nSTdecl is " << nTSTdecl << std::endl;
3642
3643 isEqual = (nTSTdecl && nTSTdecl->getMostRecentDecl() == TSTdecl->getMostRecentDecl()) ||
3644 (tParQualType.getTypePtr() == newArg.getAsType().getTypePtr());
3645 }
3646
3647
3648 return isEqual;
3649}
3650
3651
3652////////////////////////////////////////////////////////////////////////////////
3653/// std::cout << "Are equal values?\n";
3654
3655static bool areEqualValues(const clang::TemplateArgument& tArg,
3656 const clang::NamedDecl& tPar)
3657{
3658 using namespace clang;
3659 const NonTypeTemplateParmDecl* nttpdPtr = llvm::dyn_cast<NonTypeTemplateParmDecl>(&tPar);
3660 if (!nttpdPtr) return false;
3661 const NonTypeTemplateParmDecl& nttpd = *nttpdPtr;
3662
3663 if (!nttpd.hasDefaultArgument())
3664 return false;
3665
3666 // 64 bits wide and signed (non unsigned, that is why "false")
3667 llvm::APSInt defaultValueAPSInt(64, false);
3668 if (Expr* defArgExpr = nttpd.getDefaultArgument()) {
3669 const ASTContext& astCtxt = nttpdPtr->getASTContext();
3670 if (auto Value = defArgExpr->getIntegerConstantExpr(astCtxt))
3671 defaultValueAPSInt = *Value;
3672 }
3673
3674 const int value = tArg.getAsIntegral().getLimitedValue();
3675
3676 // std::cout << (value == defaultValueAPSInt ? "yes!":"no") << std::endl;
3677 return value == defaultValueAPSInt;
3678}
3679
3680////////////////////////////////////////////////////////////////////////////////
3681/// Check if this NamedDecl is a template parameter with a default argument.
3682/// This is a single interface to treat both integral and type parameters.
3683/// Returns true if this is the case, false otherwise
3684
3685static bool isTypeWithDefault(const clang::NamedDecl* nDecl)
3686{
3687 using namespace clang;
3688 if (!nDecl) return false;
3689 if (const TemplateTypeParmDecl* ttpd = llvm::dyn_cast<TemplateTypeParmDecl>(nDecl))
3690 return ttpd->hasDefaultArgument();
3691 if (const NonTypeTemplateParmDecl* nttpd = llvm::dyn_cast<NonTypeTemplateParmDecl>(nDecl))
3692 return nttpd->hasDefaultArgument();
3693 return false;
3694
3695}
3696
3697static void KeepNParams(clang::QualType& normalizedType,
3698 const clang::QualType& vanillaType,
3699 const cling::Interpreter& interp,
3700 const ROOT::TMetaUtils::TNormalizedCtxt& normCtxt);
3701
3702// Returns true if normTArg might have changed.
3703static bool RecurseKeepNParams(clang::TemplateArgument &normTArg,
3704 const clang::TemplateArgument &tArg,
3705 const cling::Interpreter& interp,
3706 const ROOT::TMetaUtils::TNormalizedCtxt& normCtxt,
3707 const clang::ASTContext& astCtxt)
3708{
3709 using namespace ROOT::TMetaUtils;
3710 using namespace clang;
3711
3712 // Once we know there is no more default parameter, we can run through to the end
3713 // and/or recurse in the template parameter packs.
3714
3715 // If this is a type,
3716 // we need first of all to recurse: this argument may need to be manipulated
3717 if (tArg.getKind() == clang::TemplateArgument::Type) {
3718 QualType thisNormQualType = normTArg.getAsType();
3719 QualType thisArgQualType = tArg.getAsType();
3720 KeepNParams(thisNormQualType,
3721 thisArgQualType,
3722 interp,
3723 normCtxt);
3724 normTArg = TemplateArgument(thisNormQualType);
3725 return (thisNormQualType != thisArgQualType);
3726 } else if (normTArg.getKind() == clang::TemplateArgument::Pack) {
3727 assert( tArg.getKind() == clang::TemplateArgument::Pack );
3728
3729 SmallVector<TemplateArgument, 2> desArgs;
3730 bool mightHaveChanged = true;
3731 for (auto I = normTArg.pack_begin(), E = normTArg.pack_end(),
3732 FI = tArg.pack_begin(), FE = tArg.pack_end();
3733 I != E && FI != FE; ++I, ++FI)
3734 {
3735 TemplateArgument pack_arg(*I);
3736 mightHaveChanged |= RecurseKeepNParams(pack_arg, *FI, interp, normCtxt, astCtxt);
3737 desArgs.push_back(pack_arg);
3738 }
3739 if (mightHaveChanged) {
3740 ASTContext &mutableCtx( const_cast<ASTContext&>(astCtxt) );
3741 normTArg = TemplateArgument::CreatePackCopy(mutableCtx, desArgs);
3742 }
3743 return mightHaveChanged;
3744 }
3745 return false;
3746}
3747
3748
3749////////////////////////////////////////////////////////////////////////////////
3750/// This function allows to manipulate the number of arguments in the type
3751/// of a template specialisation.
3752
3753static void KeepNParams(clang::QualType& normalizedType,
3754 const clang::QualType& vanillaType,
3755 const cling::Interpreter& interp,
3756 const ROOT::TMetaUtils::TNormalizedCtxt& normCtxt)
3757{
3758 using namespace ROOT::TMetaUtils;
3759 using namespace clang;
3760
3761 // If this type has no template specialisation behind, we don't need to do
3762 // anything
3763 ClassTemplateSpecializationDecl* ctsd;
3764 ClassTemplateDecl* ctd;
3765 if (! QualType2Template(vanillaType, ctd, ctsd)) return ;
3766
3767 // Even if this is a template, if we don't keep any argument, return
3768 const int nArgsToKeep = normCtxt.GetNargsToKeep(ctd);
3769
3770 // Important in case of early return: we must restore the original qualtype
3771 QualType originalNormalizedType = normalizedType;
3772
3773 const ASTContext& astCtxt = ctsd->getASTContext();
3774
3775
3776 // In case of name* we need to strip the pointer first, add the default and attach
3777 // the pointer once again.
3778 if (llvm::isa<clang::PointerType>(normalizedType.getTypePtr())) {
3779 // Get the qualifiers.
3780 clang::Qualifiers quals = normalizedType.getQualifiers();
3781 auto valNormalizedType = normalizedType->getPointeeType();
3782 KeepNParams(valNormalizedType,vanillaType, interp, normCtxt);
3783 normalizedType = astCtxt.getPointerType(valNormalizedType);
3784 // Add back the qualifiers.
3785 normalizedType = astCtxt.getQualifiedType(normalizedType, quals);
3786 return;
3787 }
3788
3789 // In case of Int_t& we need to strip the pointer first, desugar and attach
3790 // the pointer once again.
3791 if (llvm::isa<clang::ReferenceType>(normalizedType.getTypePtr())) {
3792 // Get the qualifiers.
3793 bool isLValueRefTy = llvm::isa<clang::LValueReferenceType>(normalizedType.getTypePtr());
3794 clang::Qualifiers quals = normalizedType.getQualifiers();
3795 auto valNormType = normalizedType->getPointeeType();
3796 KeepNParams(valNormType, vanillaType, interp, normCtxt);
3797
3798 // Add the r- or l- value reference type back to the desugared one
3799 if (isLValueRefTy)
3800 normalizedType = astCtxt.getLValueReferenceType(valNormType);
3801 else
3802 normalizedType = astCtxt.getRValueReferenceType(valNormType);
3803 // Add back the qualifiers.
3804 normalizedType = astCtxt.getQualifiedType(normalizedType, quals);
3805 return;
3806 }
3807
3808 // Treat the Scope (factorise the code out to reuse it in AddDefaultParameters)
3809 bool prefix_changed = false;
3810 clang::NestedNameSpecifier* prefix = nullptr;
3811 clang::Qualifiers prefix_qualifiers = normalizedType.getLocalQualifiers();
3812 const clang::ElaboratedType* etype
3813 = llvm::dyn_cast<clang::ElaboratedType>(normalizedType.getTypePtr());
3814 if (etype) {
3815 // We have to also handle the prefix.
3816 // TODO: we ought to be running KeepNParams
3817 prefix = AddDefaultParametersNNS(astCtxt, etype->getQualifier(), interp, normCtxt);
3818 prefix_changed = prefix != etype->getQualifier();
3819 normalizedType = clang::QualType(etype->getNamedType().getTypePtr(),0);
3820 }
3821
3822 // The canonical decl does not necessarily have the template default arguments.
3823 // Need to walk through the redecl chain to find it (we know there will be no
3824 // inconsistencies, at least)
3825 const clang::ClassTemplateDecl* ctdWithDefaultArgs = ctd;
3826 for (const RedeclarableTemplateDecl* rd: ctdWithDefaultArgs->redecls()) {
3827 clang::TemplateParameterList* tpl = rd->getTemplateParameters();
3828 if (tpl->getMinRequiredArguments () < tpl->size()) {
3829 ctdWithDefaultArgs = llvm::dyn_cast<clang::ClassTemplateDecl>(rd);
3830 break;
3831 }
3832 }
3833
3834 if (!ctdWithDefaultArgs) {
3835 Error("KeepNParams", "Not found template default arguments\n");
3836 normalizedType=originalNormalizedType;
3837 return;
3838 }
3839
3840 TemplateParameterList* tParsPtr = ctdWithDefaultArgs->getTemplateParameters();
3841 const TemplateParameterList& tPars = *tParsPtr;
3842 const TemplateArgumentList& tArgs = ctsd->getTemplateArgs();
3843
3844 // We extract the template name from the type
3845 TemplateName theTemplateName = ExtractTemplateNameFromQualType(normalizedType);
3846 if (theTemplateName.isNull()) {
3847 normalizedType=originalNormalizedType;
3848 return;
3849 }
3850
3851 const TemplateSpecializationType* normalizedTst =
3852 llvm::dyn_cast<TemplateSpecializationType>(normalizedType.getTypePtr());
3853 if (!normalizedTst) {
3854 normalizedType=originalNormalizedType;
3855 return;
3856 }
3857
3858 const clang::ClassTemplateSpecializationDecl* TSTdecl
3859 = llvm::dyn_cast_or_null<const clang::ClassTemplateSpecializationDecl>(normalizedType.getTypePtr()->getAsCXXRecordDecl());
3860 bool isStdDropDefault = TSTdecl && IsStdDropDefaultClass(*TSTdecl);
3861
3862 // Loop over the template parameters and arguments recursively.
3863 // We go down the two lanes: the one of template parameters (decls) and the
3864 // one of template arguments (QualTypes) in parallel. The former are a
3865 // property of the template, independent of its instantiations.
3866 // The latter are a property of the instance itself.
3867 llvm::SmallVector<TemplateArgument, 4> argsToKeep;
3868
3869 const int nArgs = tArgs.size();
3870 const auto &normArgs = normalizedTst->template_arguments();
3871 const int nNormArgs = normArgs.size();
3872
3873 bool mightHaveChanged = false;
3874
3875 // becomes true when a parameter has a value equal to its default
3876 for (int formal = 0, inst = 0; formal != nArgs; ++formal, ++inst) {
3877 const NamedDecl* tParPtr = tPars.getParam(formal);
3878 if (!tParPtr) {
3879 Error("KeepNParams", "The parameter number %s is null.\n", formal);
3880 continue;
3881 }
3882
3883 // Stop if the normalized TemplateSpecializationType has less arguments than
3884 // the one index is pointing at.
3885 // We piggy back on the AddDefaultParameters routine basically.
3886 if (formal == nNormArgs || inst == nNormArgs) break;
3887
3888 const TemplateArgument& tArg = tArgs.get(formal);
3889 TemplateArgument normTArg(normArgs[inst]);
3890
3891 bool shouldKeepArg = nArgsToKeep < 0 || inst < nArgsToKeep;
3892 if (isStdDropDefault) shouldKeepArg = false;
3893
3894 // Nothing to do here: either this parameter has no default, or we have to keep it.
3895 // FIXME: Temporary measure to get Atlas started with this.
3896 // We put a hard cut on the number of template arguments to keep, w/o checking if
3897 // they are non default. This makes this feature UNUSABLE for cases like std::vector,
3898 // where 2 different entities would have the same name if an allocator different from
3899 // the default one is by chance used.
3900 if (!isTypeWithDefault(tParPtr) || shouldKeepArg) {
3901 if ( tParPtr->isTemplateParameterPack() ) {
3902 // This is the last template parameter in the template declaration
3903 // but it is signaling that there can be an arbitrary number of arguments
3904 // in the template instance. So to avoid inadvertenly dropping those
3905 // arguments we just process all remaining argument and exit the main loop.
3906 for( ; inst != nNormArgs; ++inst) {
3907 normTArg = normArgs[inst];
3908 mightHaveChanged |= RecurseKeepNParams(normTArg, tArg, interp, normCtxt, astCtxt);
3909 argsToKeep.push_back(normTArg);
3910 }
3911 // Done.
3912 break;
3913 }
3914 mightHaveChanged |= RecurseKeepNParams(normTArg, tArg, interp, normCtxt, astCtxt);
3915 argsToKeep.push_back(normTArg);
3916 continue;
3917 } else {
3918 if (!isStdDropDefault) {
3919 // Here we should not break but rather check if the value is the default one.
3920 mightHaveChanged = true;
3921 break;
3922 }
3923 // For std, we want to check the default args values.
3924 }
3925
3926 // Now, we keep it only if it not is equal to its default, expressed in the arg
3927 // Some gymnastic is needed to decide how to check for equality according to the
3928 // flavour of Type: templateType or Integer
3929 bool equal=false;
3930 auto argKind = tArg.getKind();
3931 if (argKind == clang::TemplateArgument::Type){
3932 // we need all the info
3933 equal = areEqualTypes(tArg, argsToKeep, *tParPtr, interp, normCtxt);
3934 } else if (argKind == clang::TemplateArgument::Integral){
3935 equal = areEqualValues(tArg, *tParPtr);
3936 }
3937 if (!equal) {
3938 mightHaveChanged |= RecurseKeepNParams(normTArg, tArg, interp, normCtxt, astCtxt);
3939 argsToKeep.push_back(normTArg);
3940 } else {
3941 mightHaveChanged = true;
3942 }
3943
3944
3945 } // of loop over parameters and arguments
3946
3947 if (!prefix_changed && !mightHaveChanged) {
3948 normalizedType = originalNormalizedType;
3949 return;
3950 }
3951
3952 // now, let's remanipulate our Qualtype
3953 if (mightHaveChanged) {
3954 Qualifiers qualifiers = normalizedType.getLocalQualifiers();
3955 normalizedType = astCtxt.getTemplateSpecializationType(theTemplateName,
3956 argsToKeep,
3957 normalizedType.getTypePtr()->getCanonicalTypeInternal());
3958 normalizedType = astCtxt.getQualifiedType(normalizedType, qualifiers);
3959 }
3960
3961 // Here we have (prefix_changed==true || mightHaveChanged), in both case
3962 // we need to reconstruct the type.
3963 if (prefix) {
3964 normalizedType = astCtxt.getElaboratedType(clang::ETK_None,prefix,normalizedType);
3965 normalizedType = astCtxt.getQualifiedType(normalizedType,prefix_qualifiers);
3966 }
3967
3968}
3969
3970////////////////////////////////////////////////////////////////////////////////
3971/// Return the type normalized for ROOT,
3972/// keeping only the ROOT opaque typedef (Double32_t, etc.) and
3973/// adding default template argument for all types except those explicitly
3974/// requested to be drop by the user.
3975/// Default template for STL collections are not yet removed by this routine.
3976
3977clang::QualType ROOT::TMetaUtils::GetNormalizedType(const clang::QualType &type, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt)
3978{
3979 clang::ASTContext &ctxt = interpreter.getCI()->getASTContext();
3980
3981 // Modules can trigger deserialization.
3982 cling::Interpreter::PushTransactionRAII RAII(const_cast<cling::Interpreter*>(&interpreter));
3983 clang::QualType normalizedType = cling::utils::Transform::GetPartiallyDesugaredType(ctxt, type, normCtxt.GetConfig(), true /* fully qualify */);
3984
3985 // Readd missing default template parameters
3986 normalizedType = ROOT::TMetaUtils::AddDefaultParameters(normalizedType, interpreter, normCtxt);
3987
3988 // Get the number of arguments to keep in case they are not default.
3989 KeepNParams(normalizedType,type,interpreter,normCtxt);
3990
3991 return normalizedType;
3992}
3993
3994////////////////////////////////////////////////////////////////////////////////
3995/// Return the type name normalized for ROOT,
3996/// keeping only the ROOT opaque typedef (Double32_t, etc.) and
3997/// adding default template argument for all types except the STL collections
3998/// where we remove the default template argument if any.
3999///
4000/// This routine might actually belong in the interpreter because
4001/// cache the clang::Type might be intepreter specific.
4002
4003void ROOT::TMetaUtils::GetNormalizedName(std::string &norm_name, const clang::QualType &type, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt)
4004{
4005 if (type.isNull()) {
4006 norm_name = "";
4007 return;
4008 }
4009
4010 clang::QualType normalizedType = GetNormalizedType(type,interpreter,normCtxt);
4011
4012 clang::ASTContext &ctxt = interpreter.getCI()->getASTContext();
4013 clang::PrintingPolicy policy(ctxt.getPrintingPolicy());
4014 policy.SuppressTagKeyword = true; // Never get the class or struct keyword
4015 policy.SuppressScope = true; // Force the scope to be coming from a clang::ElaboratedType.
4016 policy.AnonymousTagLocations = false; // Do not extract file name + line number for anonymous types.
4017 // The scope suppression is required for getting rid of the anonymous part of the name of a class defined in an anonymous namespace.
4018 // This gives us more control vs not using the clang::ElaboratedType and relying on the Policy.SuppressUnwrittenScope which would
4019 // strip both the anonymous and the inline namespace names (and we probably do not want the later to be suppressed).
4020
4021 std::string normalizedNameStep1;
4022
4023 // getAsStringInternal can trigger deserialization
4024 cling::Interpreter::PushTransactionRAII clingRAII(const_cast<cling::Interpreter*>(&interpreter));
4025 normalizedType.getAsStringInternal(normalizedNameStep1,policy);
4026
4027 // Still remove the std:: and default template argument for STL container and
4028 // normalize the location and amount of white spaces.
4031
4032 // The result of this routine is by definition a fully qualified name. There is an implicit starting '::' at the beginning of the name.
4033 // Depending on how the user typed their code, in particular typedef declarations, we may end up with an explicit '::' being
4034 // part of the result string. For consistency, we must remove it.
4035 if (norm_name.length()>2 && norm_name[0]==':' && norm_name[1]==':') {
4036 norm_name.erase(0,2);
4037 }
4038
4039}
4040
4041////////////////////////////////////////////////////////////////////////////////
4042
4043void ROOT::TMetaUtils::GetNormalizedName(std::string &norm_name,
4044 const clang::TypeDecl* typeDecl,
4045 const cling::Interpreter &interpreter)
4046{
4047 ROOT::TMetaUtils::TNormalizedCtxt tNormCtxt(interpreter.getLookupHelper());
4048 const clang::Sema &sema = interpreter.getSema();
4049 clang::ASTContext& astCtxt = sema.getASTContext();
4050 clang::QualType qualType = astCtxt.getTypeDeclType(typeDecl);
4051
4053 qualType,
4054 interpreter,
4055 tNormCtxt);
4056}
4057
4058////////////////////////////////////////////////////////////////////////////////
4059std::pair<std::string,clang::QualType>
4060ROOT::TMetaUtils::GetNameTypeForIO(const clang::QualType& thisType,
4061 const cling::Interpreter &interpreter,
4062 const TNormalizedCtxt &normCtxt,
4064{
4065 std::string thisTypeName;
4066 GetNormalizedName(thisTypeName, thisType, interpreter, normCtxt );
4067 bool hasChanged;
4068 auto thisTypeNameForIO = TClassEdit::GetNameForIO(thisTypeName, mode, &hasChanged);
4069 if (!hasChanged) return std::make_pair(thisTypeName,thisType);
4070
4072 ROOT::TMetaUtils::Info("ROOT::TMetaUtils::GetTypeForIO",
4073 "Name changed from %s to %s\n", thisTypeName.c_str(), thisTypeNameForIO.c_str());
4074 }
4075
4076 auto& lookupHelper = interpreter.getLookupHelper();
4077
4078 const clang::Type* typePtrForIO;
4079 lookupHelper.findScope(thisTypeNameForIO,
4080 cling::LookupHelper::DiagSetting::NoDiagnostics,
4081 &typePtrForIO);
4082
4083 // This should never happen
4084 if (!typePtrForIO) {
4085 ROOT::TMetaUtils::Fatal("ROOT::TMetaUtils::GetTypeForIO",
4086 "Type not found: %s.",thisTypeNameForIO.c_str());
4087 }
4088
4089 clang::QualType typeForIO(typePtrForIO,0);
4090
4091 // Check if this is a class. Indeed it could well be a POD
4092 if (!typeForIO->isRecordType()) {
4093 return std::make_pair(thisTypeNameForIO,typeForIO);
4094 }
4095
4096 auto thisDeclForIO = typeForIO->getAsCXXRecordDecl();
4097 if (!thisDeclForIO) {
4098 ROOT::TMetaUtils::Error("ROOT::TMetaUtils::GetTypeForIO",
4099 "The type for IO corresponding to %s is %s and it could not be found in the AST as class.\n", thisTypeName.c_str(), thisTypeNameForIO.c_str());
4100 return std::make_pair(thisTypeName,thisType);
4101 }
4102
4103 return std::make_pair(thisTypeNameForIO,typeForIO);
4104}
4105
4106////////////////////////////////////////////////////////////////////////////////
4107
4108clang::QualType ROOT::TMetaUtils::GetTypeForIO(const clang::QualType& thisType,
4109 const cling::Interpreter &interpreter,
4110 const TNormalizedCtxt &normCtxt,
4112{
4113 return GetNameTypeForIO(thisType, interpreter, normCtxt, mode).second;
4114}
4115
4116////////////////////////////////////////////////////////////////////////////////
4117/// Return the dictionary file name for a module
4118
4119std::string ROOT::TMetaUtils::GetModuleFileName(const char* moduleName)
4120{
4121 std::string dictFileName(moduleName);
4122 dictFileName += "_rdict.pcm";
4123 return dictFileName;
4124}
4125
4126int dumpDeclForAssert(const clang::Decl& D, const char* commentStart) {
4127 llvm::errs() << llvm::StringRef(commentStart, 80) << '\n';
4128 D.dump();
4129 return 0;
4130}
4131
4132////////////////////////////////////////////////////////////////////////////////
4133/// Returns the comment (// striped away), annotating declaration in a meaningful
4134/// for ROOT IO way.
4135/// Takes optional out parameter clang::SourceLocation returning the source
4136/// location of the comment.
4137///
4138/// CXXMethodDecls, FieldDecls and TagDecls are annotated.
4139/// CXXMethodDecls declarations and FieldDecls are annotated as follows:
4140/// Eg. void f(); // comment1
4141/// int member; // comment2
4142/// Inline definitions of CXXMethodDecls after the closing } \n. Eg:
4143/// void f()
4144/// {...} // comment3
4145/// TagDecls are annotated in the end of the ClassDef macro. Eg.
4146/// class MyClass {
4147/// ...
4148/// ClassDef(MyClass, 1) // comment4
4149///
4150
4151llvm::StringRef ROOT::TMetaUtils::GetComment(const clang::Decl &decl, clang::SourceLocation *loc)
4152{
4153 clang::SourceManager& sourceManager = decl.getASTContext().getSourceManager();
4154 clang::SourceLocation sourceLocation = decl.getEndLoc();
4155
4156 // If the location is a macro get the expansion location.
4157 sourceLocation = sourceManager.getExpansionRange(sourceLocation).getEnd();
4158 // FIXME: We should optimize this routine instead making it do the wrong thing
4159 // returning an empty comment if the decl came from the AST.
4160 // In order to do that we need to: check if the decl has an attribute and
4161 // return the attribute content (including walking the redecl chain) and if
4162 // this is not the case we should try finding it in the header file.
4163 // This will allow us to move the implementation of TCling*Info::Title() in
4164 // TClingDeclInfo.
4165 if (!decl.hasOwningModule() && sourceManager.isLoadedSourceLocation(sourceLocation)) {
4166 // Do not touch disk for nodes coming from the PCH.
4167 return "";
4168 }
4169
4170 bool invalid;
4171 const char *commentStart = sourceManager.getCharacterData(sourceLocation, &invalid);
4172 if (invalid)
4173 return "";
4174
4175 bool skipToSemi = true;
4176 if (const clang::FunctionDecl* FD = clang::dyn_cast<clang::FunctionDecl>(&decl)) {
4177 if (FD->isImplicit()) {
4178 // Compiler generated function.
4179 return "";
4180 }
4181 if (FD->isExplicitlyDefaulted() || FD->isDeletedAsWritten()) {
4182 // ctorOrFunc() = xyz; with commentStart pointing somewhere into
4183 // ctorOrFunc.
4184 // We have to skipToSemi
4185 } else if (FD->doesThisDeclarationHaveABody()) {
4186 // commentStart is at body's '}'
4187 // But we might end up e.g. at the ')' of a CPP macro
4188 assert((decl.getEndLoc() != sourceLocation || *commentStart == '}'
4189 || dumpDeclForAssert(*FD, commentStart))
4190 && "Expected macro or end of body at '}'");
4191 if (*commentStart) ++commentStart;
4192
4193 // We might still have a ';'; skip the spaces and check.
4194 while (*commentStart && isspace(*commentStart)
4195 && *commentStart != '\n' && *commentStart != '\r') {
4196 ++commentStart;
4197 }
4198 if (*commentStart == ';') ++commentStart;
4199
4200 skipToSemi = false;
4201 }
4202 } else if (const clang::EnumConstantDecl* ECD
4203 = clang::dyn_cast<clang::EnumConstantDecl>(&decl)) {
4204 // either "konstant = 12, //COMMENT" or "lastkonstant // COMMENT"
4205 if (ECD->getNextDeclInContext())
4206 while (*commentStart && *commentStart != ',' && *commentStart != '\r' && *commentStart != '\n')
4207 ++commentStart;
4208 // else commentStart already points to the end.
4209
4210 skipToSemi = false;
4211 }
4212
4213 if (skipToSemi) {
4214 while (*commentStart && *commentStart != ';' && *commentStart != '\r' && *commentStart != '\n')
4215 ++commentStart;
4216 if (*commentStart == ';') ++commentStart;
4217 }
4218
4219 // Now skip the spaces until beginning of comments or EOL.
4220 while ( *commentStart && isspace(*commentStart)
4221 && *commentStart != '\n' && *commentStart != '\r') {
4222 ++commentStart;
4223 }
4224
4225 if (commentStart[0] != '/' ||
4226 (commentStart[1] != '/' && commentStart[1] != '*')) {
4227 // not a comment
4228 return "";
4229 }
4230
4231 // Treat by default c++ comments (+2) but also Doxygen comments (+4)
4232 // Int_t fPx; ///< Some doxygen comment for persistent data.
4233 // Int_t fPy; //!< Some doxygen comment for persistent data.
4234 // Int_t fPz; /*!< Some doxygen comment for persistent data. */
4235 // Int_t fPa; /**< Some doxygen comment for persistent data. */
4236 unsigned int skipChars = 2;
4237 if (commentStart[0] == '/' &&
4238 commentStart[1] == '/' &&
4239 (commentStart[2] == '/' || commentStart[2] == '!') &&
4240 commentStart[3] == '<') {
4241 skipChars = 4;
4242 } else if (commentStart[0] == '/' &&
4243 commentStart[1] == '*' &&
4244 (commentStart[2] == '*' || commentStart[2] == '!') &&
4245 commentStart[3] == '<') {
4246 skipChars = 4;
4247 }
4248
4249 commentStart += skipChars;
4250
4251 // Now skip the spaces after comment start until EOL.
4252 while ( *commentStart && isspace(*commentStart)
4253 && *commentStart != '\n' && *commentStart != '\r') {
4254 ++commentStart;
4255 }
4256 const char* commentEnd = commentStart;
4257 // Even for /* comments we only take the first line into account.
4258 while (*commentEnd && *commentEnd != '\n' && *commentEnd != '\r') {
4259 ++commentEnd;
4260 }
4261
4262 // "Skip" (don't include) trailing space.
4263 // *commentEnd points behind comment end thus check commentEnd[-1]
4264 while (commentEnd > commentStart && isspace(commentEnd[-1])) {
4265 --commentEnd;
4266 }
4267
4268 if (loc) {
4269 // Find the true beginning of a comment.
4270 unsigned offset = commentStart - sourceManager.getCharacterData(sourceLocation);
4271 *loc = sourceLocation.getLocWithOffset(offset - 1);
4272 }
4273
4274 return llvm::StringRef(commentStart, commentEnd - commentStart);
4275}
4276
4277////////////////////////////////////////////////////////////////////////////////
4278/// Return true if class has any of class declarations like ClassDef, ClassDefNV, ClassDefOverride
4279
4280bool ROOT::TMetaUtils::HasClassDefMacro(const clang::Decl *decl, const cling::Interpreter &interpreter)
4281{
4282 if (!decl) return false;
4283
4284 auto& sema = interpreter.getCI()->getSema();
4285 auto maybeMacroLoc = decl->getLocation();
4286
4287 if (!maybeMacroLoc.isMacroID()) return false;
4288
4289 static const std::vector<std::string> signatures =
4290 { "ClassDef", "ClassDefOverride", "ClassDefNV", "ClassDefInline", "ClassDefInlineOverride", "ClassDefInlineNV" };
4291
4292 for (auto &name : signatures)
4293 if (sema.findMacroSpelling(maybeMacroLoc, name))
4294 return true;
4295
4296 return false;
4297}
4298
4299////////////////////////////////////////////////////////////////////////////////
4300/// Return the class comment after the ClassDef:
4301/// class MyClass {
4302/// ...
4303/// ClassDef(MyClass, 1) // class comment
4304///
4305
4306llvm::StringRef ROOT::TMetaUtils::GetClassComment(const clang::CXXRecordDecl &decl,
4307 clang::SourceLocation *loc,
4308 const cling::Interpreter &interpreter)
4309{
4310 using namespace clang;
4311
4312 const Decl* DeclFileLineDecl
4313 = interpreter.getLookupHelper().findFunctionProto(&decl, "DeclFileLine", "",
4314 cling::LookupHelper::NoDiagnostics);
4315
4316 // For now we allow only a special macro (ClassDef) to have meaningful comments
4317 if (HasClassDefMacro(DeclFileLineDecl, interpreter)) {
4318 SourceLocation commentSLoc;
4319 llvm::StringRef comment = ROOT::TMetaUtils::GetComment(*DeclFileLineDecl, &commentSLoc);
4320 if (comment.size()) {
4321 if (loc) {
4322 *loc = commentSLoc;
4323 }
4324 return comment;
4325 }
4326 }
4327 return llvm::StringRef();
4328}
4329
4330////////////////////////////////////////////////////////////////////////////////
4331/// Return the base/underlying type of a chain of array or pointers type.
4332/// Does not yet support the array and pointer part being intermixed.
4333
4334const clang::Type *ROOT::TMetaUtils::GetUnderlyingType(clang::QualType type)
4335{
4336 const clang::Type *rawtype = type.getTypePtr();
4337
4338 // NOTE: We probably meant isa<clang::ElaboratedType>
4339 if (rawtype->isElaboratedTypeSpecifier() ) {
4340 rawtype = rawtype->getCanonicalTypeInternal().getTypePtr();
4341 }
4342 if (rawtype->isArrayType()) {
4343 rawtype = type.getTypePtr()->getBaseElementTypeUnsafe ();
4344 }
4345 if (rawtype->isPointerType() || rawtype->isReferenceType() ) {
4346 //Get to the 'raw' type.
4347 clang::QualType pointee;
4348 while ( (pointee = rawtype->getPointeeType()) , pointee.getTypePtrOrNull() && pointee.getTypePtr() != rawtype)
4349 {
4350 rawtype = pointee.getTypePtr();
4351
4352 if (rawtype->isElaboratedTypeSpecifier() ) {
4353 rawtype = rawtype->getCanonicalTypeInternal().getTypePtr();
4354 }
4355 if (rawtype->isArrayType()) {
4356 rawtype = rawtype->getBaseElementTypeUnsafe ();
4357 }
4358 }
4359 }
4360 if (rawtype->isArrayType()) {
4361 rawtype = rawtype->getBaseElementTypeUnsafe ();
4362 }
4363 return rawtype;
4364}
4365
4366////////////////////////////////////////////////////////////////////////////////
4367/// Return true, if the decl is part of the std namespace.
4368
4369bool ROOT::TMetaUtils::IsStdClass(const clang::RecordDecl &cl)
4370{
4371 return cling::utils::Analyze::IsStdClass(cl);
4372}
4373
4374////////////////////////////////////////////////////////////////////////////////
4375/// Return true, if the decl is part of the std namespace and we want
4376/// its default parameter dropped.
4377
4378bool ROOT::TMetaUtils::IsStdDropDefaultClass(const clang::RecordDecl &cl)
4379{
4380 // Might need to reduce it to shared_ptr and STL collection.s
4381 if (cling::utils::Analyze::IsStdClass(cl)) {
4382 static const char *names[] =
4383 { "shared_ptr", "__shared_ptr",
4384 "vector", "list", "deque", "map", "multimap", "set", "multiset", "bitset"};
4385 llvm::StringRef clname(cl.getName());
4386 for(auto &&name : names) {
4387 if (clname == name) return true;
4388 }
4389 }
4390 return false;
4391}
4392
4393////////////////////////////////////////////////////////////////////////////////
4394/// This is a recursive function
4395
4396bool ROOT::TMetaUtils::MatchWithDeclOrAnyOfPrevious(const clang::CXXRecordDecl &cl,
4397 const clang::CXXRecordDecl &currentCl)
4398{
4399 // We found it: let's return true
4400 if (&cl == &currentCl) return true;
4401
4402 const clang::CXXRecordDecl* previous = currentCl.getPreviousDecl();
4403
4404 // There is no previous decl, so we cannot possibly find it
4405 if (nullptr == previous){
4406 return false;
4407 }
4408
4409 // We try to find it in the previous
4411
4412}
4413
4414//______________________________________________________________________________
4415
4416bool ROOT::TMetaUtils::IsOfType(const clang::CXXRecordDecl &cl, const std::string& typ, const cling::LookupHelper& lh)
4417{
4418 // Return true if the decl is of type.
4419 // A proper hashtable for caching results would be the ideal solution
4420 // 1) Only one lookup per type
4421 // 2) No string comparison
4422 // We may use a map which becomes an unordered map if c++11 is enabled?
4423
4424 const clang::CXXRecordDecl *thisDecl =
4425 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(lh.findScope(typ, cling::LookupHelper::WithDiagnostics));
4426
4427 // this would be probably an assert given that this state is not reachable unless a mistake is somewhere
4428 if (! thisDecl){
4429 Error("IsOfType","Record decl of type %s not found in the AST.", typ.c_str());
4430 return false;
4431 }
4432
4433 // Now loop on all previous decls to seek a match
4434 const clang::CXXRecordDecl *mostRecentDecl = thisDecl->getMostRecentDecl();
4435 bool matchFound = MatchWithDeclOrAnyOfPrevious (cl,*mostRecentDecl);
4436
4437 return matchFound;
4438}
4439
4440////////////////////////////////////////////////////////////////////////////////
4441/// type : type name: vector<list<classA,allocator>,allocator>
4442/// result: 0 : not stl container
4443/// abs(result): code of container 1=vector,2=list,3=deque,4=map
4444/// 5=multimap,6=set,7=multiset
4445
4447{
4448 // This routine could be enhanced to also support:
4449 //
4450 // testAlloc: if true, we test allocator, if it is not default result is negative
4451 // result: 0 : not stl container
4452 // abs(result): code of container 1=vector,2=list,3=deque,4=map
4453 // 5=multimap,6=set,7=multiset
4454 // positive val: we have a vector or list with default allocator to any depth
4455 // like vector<list<vector<int>>>
4456 // negative val: STL container other than vector or list, or non default allocator
4457 // For example: vector<deque<int>> has answer -1
4458
4459 if (!IsStdClass(cl)) {
4460 auto *nsDecl = llvm::dyn_cast<clang::NamespaceDecl>(cl.getDeclContext());
4461 if (cl.getName() != "RVec" || nsDecl == nullptr || nsDecl->getName() != "VecOps")
4462 return ROOT::kNotSTL;
4463
4464 auto *parentNsDecl = llvm::dyn_cast<clang::NamespaceDecl>(cl.getDeclContext()->getParent());
4465 if (parentNsDecl == nullptr || parentNsDecl->getName() != "ROOT")
4466 return ROOT::kNotSTL;
4467 }
4468
4469 return STLKind(cl.getName());
4470}
4471
4472static bool hasSomeTypedefSomewhere(const clang::Type* T) {
4473 using namespace clang;
4474 struct SearchTypedef: public TypeVisitor<SearchTypedef, bool> {
4475 bool VisitTypedefType(const TypedefType* TD) {
4476 return true;
4477 }
4478 bool VisitArrayType(const ArrayType* AT) {
4479 return Visit(AT->getElementType().getTypePtr());
4480 }
4481 bool VisitDecltypeType(const DecltypeType* DT) {
4482 return Visit(DT->getUnderlyingType().getTypePtr());
4483 }
4484 bool VisitPointerType(const PointerType* PT) {
4485 return Visit(PT->getPointeeType().getTypePtr());
4486 }
4487 bool VisitReferenceType(const ReferenceType* RT) {
4488 return Visit(RT->getPointeeType().getTypePtr());
4489 }
4490 bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType* STST) {
4491 return Visit(STST->getReplacementType().getTypePtr());
4492 }
4493 bool VisitTemplateSpecializationType(const TemplateSpecializationType* TST) {
4494 for (const TemplateArgument &TA : TST->template_arguments()) {
4495 if (TA.getKind() == TemplateArgument::Type && Visit(TA.getAsType().getTypePtr()))
4496 return true;
4497 }
4498 return false;
4499 }
4500 bool VisitTemplateTypeParmType(const TemplateTypeParmType* TTPT) {
4501 return false; // shrug...
4502 }
4503 bool VisitTypeOfType(const TypeOfType* TOT) {
4504 return TOT->getUnmodifiedType().getTypePtr();
4505 }
4506 bool VisitElaboratedType(const ElaboratedType* ET) {
4507 NestedNameSpecifier* NNS = ET->getQualifier();
4508 while (NNS) {
4509 if (NNS->getKind() == NestedNameSpecifier::TypeSpec) {
4510 if (Visit(NNS->getAsType()))
4511 return true;
4512 }
4513 NNS = NNS->getPrefix();
4514 }
4515 return Visit(ET->getNamedType().getTypePtr());
4516 }
4517 };
4518
4519 SearchTypedef ST;
4520 return ST.Visit(T);
4521}
4522
4523////////////////////////////////////////////////////////////////////////////////
4524/// Check if 'input' or any of its template parameter was substituted when
4525/// instantiating the class template instance and replace it with the
4526/// partially sugared types we have from 'instance'.
4527
4528clang::QualType ROOT::TMetaUtils::ReSubstTemplateArg(clang::QualType input, const clang::Type *instance)
4529{
4530 if (!instance) return input;
4531 // if there is no typedef in instance then there is nothing guiding any
4532 // template parameter typedef replacement.
4534 return input;
4535
4536 using namespace llvm;
4537 using namespace clang;
4538 const clang::ASTContext &Ctxt = instance->getAsCXXRecordDecl()->getASTContext();
4539
4540 // Treat scope (clang::ElaboratedType) if any.
4541 const clang::ElaboratedType* etype
4542 = llvm::dyn_cast<clang::ElaboratedType>(input.getTypePtr());
4543 if (etype) {
4544 // We have to also handle the prefix.
4545
4546 clang::Qualifiers scope_qualifiers = input.getLocalQualifiers();
4547 assert(instance->getAsCXXRecordDecl() != nullptr && "ReSubstTemplateArg only makes sense with a type representing a class.");
4548
4549 clang::NestedNameSpecifier *scope = ReSubstTemplateArgNNS(Ctxt,etype->getQualifier(),instance);
4550 clang::QualType subTy = ReSubstTemplateArg(clang::QualType(etype->getNamedType().getTypePtr(),0),instance);
4551
4552 if (scope) subTy = Ctxt.getElaboratedType(clang::ETK_None,scope,subTy);
4553 subTy = Ctxt.getQualifiedType(subTy,scope_qualifiers);
4554 return subTy;
4555 }
4556
4557 QualType QT = input;
4558
4559 // In case of Int_t* we need to strip the pointer first, ReSubst and attach
4560 // the pointer once again.
4561 if (isa<clang::PointerType>(QT.getTypePtr())) {
4562 // Get the qualifiers.
4563 Qualifiers quals = QT.getQualifiers();
4564 QualType nQT;
4565 nQT = ReSubstTemplateArg(QT->getPointeeType(),instance);
4566 if (nQT == QT->getPointeeType()) return QT;
4567
4568 QT = Ctxt.getPointerType(nQT);
4569 // Add back the qualifiers.
4570 QT = Ctxt.getQualifiedType(QT, quals);
4571 return QT;
4572 }
4573
4574 // In case of Int_t& we need to strip the pointer first, ReSubst and attach
4575 // the reference once again.
4576 if (isa<ReferenceType>(QT.getTypePtr())) {
4577 // Get the qualifiers.
4578 bool isLValueRefTy = isa<LValueReferenceType>(QT.getTypePtr());
4579 Qualifiers quals = QT.getQualifiers();
4580 QualType nQT;
4581 nQT = ReSubstTemplateArg(QT->getPointeeType(),instance);
4582 if (nQT == QT->getPointeeType()) return QT;
4583
4584 // Add the r- or l-value reference type back to the desugared one.
4585 if (isLValueRefTy)
4586 QT = Ctxt.getLValueReferenceType(nQT);
4587 else
4588 QT = Ctxt.getRValueReferenceType(nQT);
4589 // Add back the qualifiers.
4590 QT = Ctxt.getQualifiedType(QT, quals);
4591 return QT;
4592 }
4593
4594 // In case of Int_t[2] we need to strip the array first, ReSubst and attach
4595 // the array once again.
4596 if (isa<clang::ArrayType>(QT.getTypePtr())) {
4597 // Get the qualifiers.
4598 Qualifiers quals = QT.getQualifiers();
4599
4600 if (const auto arr = dyn_cast<ConstantArrayType>(QT.getTypePtr())) {
4601 QualType newQT= ReSubstTemplateArg(arr->getElementType(),instance);
4602
4603 if (newQT == arr->getElementType()) return QT;
4604 QT = Ctxt.getConstantArrayType(newQT,
4605 arr->getSize(),
4606 arr->getSizeExpr(),
4607 arr->getSizeModifier(),
4608 arr->getIndexTypeCVRQualifiers());
4609
4610 } else if (const auto arr = dyn_cast<DependentSizedArrayType>(QT.getTypePtr())) {
4611 QualType newQT = ReSubstTemplateArg(arr->getElementType(),instance);
4612
4613 if (newQT == QT) return QT;
4614 QT = Ctxt.getDependentSizedArrayType (newQT,
4615 arr->getSizeExpr(),
4616 arr->getSizeModifier(),
4617 arr->getIndexTypeCVRQualifiers(),
4618 arr->getBracketsRange());
4619
4620 } else if (const auto arr = dyn_cast<IncompleteArrayType>(QT.getTypePtr())) {
4621 QualType newQT = ReSubstTemplateArg(arr->getElementType(),instance);
4622
4623 if (newQT == arr->getElementType()) return QT;
4624 QT = Ctxt.getIncompleteArrayType (newQT,
4625 arr->getSizeModifier(),
4626 arr->getIndexTypeCVRQualifiers());
4627
4628 } else if (const auto arr = dyn_cast<VariableArrayType>(QT.getTypePtr())) {
4629 QualType newQT = ReSubstTemplateArg(arr->getElementType(),instance);
4630
4631 if (newQT == arr->getElementType()) return QT;
4632 QT = Ctxt.getVariableArrayType (newQT,
4633 arr->getSizeExpr(),
4634 arr->getSizeModifier(),
4635 arr->getIndexTypeCVRQualifiers(),
4636 arr->getBracketsRange());
4637 }
4638
4639 // Add back the qualifiers.
4640 QT = Ctxt.getQualifiedType(QT, quals);
4641 return QT;
4642 }
4643
4644 // If the instance is also an elaborated type, we need to skip
4645 etype = llvm::dyn_cast<clang::ElaboratedType>(instance);
4646 if (etype) {
4647 instance = etype->getNamedType().getTypePtr();
4648 if (!instance) return input;
4649 }
4650
4651 const clang::TemplateSpecializationType* TST
4652 = llvm::dyn_cast<const clang::TemplateSpecializationType>(instance);
4653
4654 if (!TST) return input;
4655
4656 const clang::ClassTemplateSpecializationDecl* TSTdecl
4657 = llvm::dyn_cast_or_null<const clang::ClassTemplateSpecializationDecl>(instance->getAsCXXRecordDecl());
4658
4659 if (!TSTdecl) return input;
4660
4661 const clang::SubstTemplateTypeParmType *substType
4662 = llvm::dyn_cast<clang::SubstTemplateTypeParmType>(input.getTypePtr());
4663
4664 if (substType) {
4665 // Make sure it got replaced from this template
4666 const clang::ClassTemplateDecl *replacedCtxt = nullptr;
4667
4668 const clang::DeclContext *replacedDeclCtxt = substType->getReplacedParameter()->getDeclContext();
4669 const clang::CXXRecordDecl *decl = llvm::dyn_cast<clang::CXXRecordDecl>(replacedDeclCtxt);
4670 unsigned int index = substType->getReplacedParameter()->getIndex();
4671 if (decl) {
4672
4673 if (decl->getKind() == clang::Decl::ClassTemplatePartialSpecialization) {
4674 const clang::ClassTemplatePartialSpecializationDecl *spec = llvm::dyn_cast<clang::ClassTemplatePartialSpecializationDecl>(decl);
4675
4676 unsigned int depth = substType->getReplacedParameter()->getDepth();
4677
4678 const TemplateArgument *instanceArgs = spec->getTemplateArgs().data();
4679 unsigned int instanceNArgs = spec->getTemplateArgs().size();
4680
4681 // Search for the 'right' replacement.
4682
4683 for(unsigned int A = 0; A < instanceNArgs; ++A) {
4684 if (instanceArgs[A].getKind() == clang::TemplateArgument::Type) {
4685 clang::QualType argQualType = instanceArgs[A].getAsType();
4686
4687 const clang::TemplateTypeParmType *replacementType;
4688
4689 replacementType = llvm::dyn_cast<clang::TemplateTypeParmType>(argQualType);
4690
4691 if (!replacementType) {
4692 const clang::SubstTemplateTypeParmType *argType
4693 = llvm::dyn_cast<clang::SubstTemplateTypeParmType>(argQualType);
4694 if (argType) {
4695 clang::QualType replacementQT = argType->getReplacementType();
4696 replacementType = llvm::dyn_cast<clang::TemplateTypeParmType>(replacementQT);
4697 }
4698 }
4699 if (replacementType &&
4700 depth == replacementType->getDepth() &&
4701 index == replacementType->getIndex() )
4702 {
4703 index = A;
4704 break;
4705 }
4706 }
4707 }
4708 replacedCtxt = spec->getSpecializedTemplate();
4709 } else {
4710 replacedCtxt = decl->getDescribedClassTemplate();
4711 }
4712 } else if (auto const declguide = llvm::dyn_cast<clang::CXXDeductionGuideDecl>(replacedDeclCtxt)) {
4713 replacedCtxt = llvm::dyn_cast<clang::ClassTemplateDecl>(declguide->getDeducedTemplate());
4714 } else if (auto const ctdecl = llvm::dyn_cast<clang::ClassTemplateDecl>(replacedDeclCtxt)) {
4715 replacedCtxt = ctdecl;
4716 } else {
4717 std::string astDump;
4718 llvm::raw_string_ostream ostream(astDump);
4719 instance->dump(ostream, Ctxt);
4720 ostream.flush();
4721 ROOT::TMetaUtils::Warning("ReSubstTemplateArg","Unexpected type of declaration context for template parameter: %s.\n\tThe responsible class is:\n\t%s\n",
4722 replacedDeclCtxt->getDeclKindName(), astDump.c_str());
4723 replacedCtxt = nullptr;
4724 }
4725
4726 if ((replacedCtxt && replacedCtxt->getCanonicalDecl() == TSTdecl->getSpecializedTemplate()->getCanonicalDecl())
4727 || /* the following is likely just redundant */
4728 substType->getReplacedParameter()
4729 == TSTdecl->getSpecializedTemplate ()->getTemplateParameters()->getParam(index))
4730 {
4731 const auto &TAs = TST->template_arguments();
4732 if (index >= TAs.size()) {
4733 // The argument replaced was a default template argument that is
4734 // being listed as part of the instance ...
4735 // so we probably don't really know how to spell it ... we would need to recreate it
4736 // (See AddDefaultParameters).
4737 return input;
4738 } else if (TAs[index].getKind() == clang::TemplateArgument::Type) {
4739 return TAs[index].getAsType();
4740 } else {
4741 // The argument is (likely) a value or expression and there is nothing for us
4742 // to change
4743 return input;
4744 }
4745 }
4746 }
4747 // Maybe a class template instance, recurse and rebuild
4748 const clang::TemplateSpecializationType* inputTST
4749 = llvm::dyn_cast<const clang::TemplateSpecializationType>(input.getTypePtr());
4750 const clang::ASTContext& astCtxt = TSTdecl->getASTContext();
4751
4752 if (inputTST) {
4753 bool mightHaveChanged = false;
4754 llvm::SmallVector<clang::TemplateArgument, 4> desArgs;
4755 for (const clang::TemplateArgument &TA : inputTST->template_arguments()) {
4756 if (TA.getKind() != clang::TemplateArgument::Type) {
4757 desArgs.push_back(TA);
4758 continue;
4759 }
4760
4761 clang::QualType SubTy = TA.getAsType();
4762 // Check if the type needs more desugaring and recurse.
4763 if (llvm::isa<clang::ElaboratedType>(SubTy)
4764 || llvm::isa<clang::SubstTemplateTypeParmType>(SubTy)
4765 || llvm::isa<clang::TemplateSpecializationType>(SubTy)) {
4766 clang::QualType newSubTy = ReSubstTemplateArg(SubTy,instance);
4767 mightHaveChanged = SubTy != newSubTy;
4768 if (!newSubTy.isNull()) {
4769 desArgs.push_back(clang::TemplateArgument(newSubTy));
4770 }
4771 } else
4772 desArgs.push_back(TA);
4773 }
4774
4775 // If desugaring happened allocate new type in the AST.
4776 if (mightHaveChanged) {
4777 clang::Qualifiers qualifiers = input.getLocalQualifiers();
4778 input = astCtxt.getTemplateSpecializationType(inputTST->getTemplateName(),
4779 desArgs,
4780 inputTST->getCanonicalTypeInternal());
4781 input = astCtxt.getQualifiedType(input, qualifiers);
4782 }
4783 }
4784
4785 return input;
4786}
4787
4788////////////////////////////////////////////////////////////////////////////////
4789/// Remove the last n template arguments from the name
4790
4791int ROOT::TMetaUtils::RemoveTemplateArgsFromName(std::string& name, unsigned int nArgsToRemove)
4792{
4793 if ( nArgsToRemove == 0 || name == "")
4794 return 0;
4795
4796 // We proceed from the right to the left, counting commas which are not
4797 // enclosed by < >.
4798 const unsigned int length = name.length();
4799 unsigned int cur=0; // let's start beyond the first > from the right
4800 unsigned int nArgsRemoved=0;
4801 unsigned int nBraces=0;
4802 char c='@';
4803 while (nArgsRemoved!=nArgsToRemove && cur<length){
4804 c = name[cur];
4805 if (c == '<') nBraces++;
4806 if (c == '>') nBraces--;
4807 if (c == ',' && nBraces==1 /*So we are not in a sub-template*/) nArgsRemoved++;
4808 cur++;
4809 }
4810 cur--;
4811 name = name.substr(0,cur)+">";
4812 return 0;
4813
4814}
4815
4816////////////////////////////////////////////////////////////////////////////////
4817/// Converts STL container name to number. vector -> 1, etc..
4818
4820{
4821 static const char *stls[] = //container names
4822 {"any","vector","list", "deque","map","multimap","set","multiset","bitset",
4823 "forward_list","unordered_set","unordered_multiset","unordered_map","unordered_multimap", "RVec", nullptr};
4824 static const ROOT::ESTLType values[] =
4835 };
4836 // kind of stl container
4837 for(int k=1;stls[k];k++) {if (type.equals(stls[k])) return values[k];}
4838 return ROOT::kNotSTL;
4839}
4840
4841////////////////////////////////////////////////////////////////////////////////
4842
4843const clang::TypedefNameDecl *ROOT::TMetaUtils::GetAnnotatedRedeclarable(const clang::TypedefNameDecl *TND)
4844{
4845 if (!TND)
4846 return nullptr;
4847
4848 TND = TND->getMostRecentDecl();
4849 while (TND && !(TND->hasAttrs()))
4850 TND = TND->getPreviousDecl();
4851
4852 return TND;
4853}
4854
4855////////////////////////////////////////////////////////////////////////////////
4856
4857const clang::TagDecl *ROOT::TMetaUtils::GetAnnotatedRedeclarable(const clang::TagDecl *TD)
4858{
4859 if (!TD)
4860 return nullptr;
4861
4862 TD = TD->getMostRecentDecl();
4863 while (TD && !(TD->hasAttrs() && TD->isThisDeclarationADefinition()))
4864 TD = TD->getPreviousDecl();
4865
4866 return TD;
4867}
4868
4869////////////////////////////////////////////////////////////////////////////////
4870/// Extract the immediately outer namespace and then launch the recursion
4871
4873 std::list<std::pair<std::string,bool> >& enclosingNamespaces)
4874{
4875 const clang::DeclContext* enclosingNamespaceDeclCtxt = decl.getDeclContext();
4876 if (!enclosingNamespaceDeclCtxt) return;
4877
4878 const clang::NamespaceDecl* enclosingNamespace =
4879 clang::dyn_cast<clang::NamespaceDecl>(enclosingNamespaceDeclCtxt);
4880 if (!enclosingNamespace) return;
4881
4882 enclosingNamespaces.push_back(std::make_pair(enclosingNamespace->getNameAsString(),
4883 enclosingNamespace->isInline()));
4884
4885 ExtractCtxtEnclosingNameSpaces(*enclosingNamespace, enclosingNamespaces);
4886
4887}
4888
4889////////////////////////////////////////////////////////////////////////////////
4890/// Extract enclosing namespaces recursively
4891
4892void ROOT::TMetaUtils::ExtractCtxtEnclosingNameSpaces(const clang::DeclContext& ctxt,
4893 std::list<std::pair<std::string,bool> >& enclosingNamespaces)
4894{
4895 const clang::DeclContext* enclosingNamespaceDeclCtxt = ctxt.getParent ();
4896
4897 // If no parent is found, nothing more to be done
4898 if (!enclosingNamespaceDeclCtxt) {
4899 return;
4900 }
4901
4902 // Check if the parent is a namespace (it could be a class for example)
4903 // if not, nothing to be done here
4904 const clang::NamespaceDecl* enclosingNamespace = clang::dyn_cast<clang::NamespaceDecl>(enclosingNamespaceDeclCtxt);
4905 if (!enclosingNamespace) return;
4906
4907 // Add to the list of parent namespaces
4908 enclosingNamespaces.push_back(std::make_pair(enclosingNamespace->getNameAsString(),
4909 enclosingNamespace->isInline()));
4910
4911 // here the recursion
4912 ExtractEnclosingNameSpaces(*enclosingNamespace, enclosingNamespaces);
4913}
4914
4915////////////////////////////////////////////////////////////////////////////////
4916/// Extract the names and types of containing scopes.
4917/// Stop if a class is met and return its pointer.
4918
4919const clang::RecordDecl *ROOT::TMetaUtils::ExtractEnclosingScopes(const clang::Decl& decl,
4920 std::list<std::pair<std::string,unsigned int> >& enclosingSc)
4921{
4922 const clang::DeclContext* enclosingDeclCtxt = decl.getDeclContext();
4923 if (!enclosingDeclCtxt) return nullptr;
4924
4925 unsigned int scopeType;
4926
4927 if (auto enclosingNamespacePtr =
4928 clang::dyn_cast<clang::NamespaceDecl>(enclosingDeclCtxt)){
4929 scopeType= enclosingNamespacePtr->isInline() ? 1 : 0; // inline or simple namespace
4930 enclosingSc.push_back(std::make_pair(enclosingNamespacePtr->getNameAsString(),scopeType));
4931 return ExtractEnclosingScopes(*enclosingNamespacePtr, enclosingSc);
4932 }
4933
4934 if (auto enclosingClassPtr =
4935 clang::dyn_cast<clang::RecordDecl>(enclosingDeclCtxt)){
4936 return enclosingClassPtr;
4937 }
4938
4939 return nullptr;
4940}
4941
4942////////////////////////////////////////////////////////////////////////////////
4943/// Reimplementation of TSystem::ExpandPathName() that cannot be
4944/// used from TMetaUtils.
4945
4946static void replaceEnvVars(const char* varname, std::string& txt)
4947{
4948 std::string::size_type beginVar = 0;
4949 std::string::size_type endVar = 0;
4950 while ((beginVar = txt.find('$', beginVar)) != std::string::npos
4951 && beginVar + 1 < txt.length()) {
4952 std::string::size_type beginVarName = beginVar + 1;
4953 std::string::size_type endVarName = std::string::npos;
4954 if (txt[beginVarName] == '(') {
4955 // "$(VARNAME)" style.
4956 endVarName = txt.find(')', beginVarName);
4957 ++beginVarName;
4958 if (endVarName == std::string::npos) {
4959 ROOT::TMetaUtils::Error(nullptr, "Missing ')' for '$(' in $%s at %s\n",
4960 varname, txt.c_str() + beginVar);
4961 return;
4962 }
4963 endVar = endVarName + 1;
4964 } else {
4965 // "$VARNAME/..." style.
4966 beginVarName = beginVar + 1;
4967 endVarName = beginVarName;
4968 while (isalnum(txt[endVarName]) || txt[endVarName] == '_')
4969 ++endVarName;
4970 endVar = endVarName;
4971 }
4972
4973 const char* val = getenv(txt.substr(beginVarName,
4974 endVarName - beginVarName).c_str());
4975 if (!val) val = "";
4976
4977 txt.replace(beginVar, endVar - beginVar, val);
4978 int lenval = strlen(val);
4979 int delta = lenval - (endVar - beginVar); // these many extra chars,
4980 endVar += delta; // advance the end marker accordingly.
4981
4982 // Look for the next one
4983 beginVar = endVar + 1;
4984 }
4985}
4986
4987////////////////////////////////////////////////////////////////////////////////
4988/// Organise the parameters for cling in order to guarantee relocatability
4989/// It treats the gcc toolchain and the root include path
4990/// FIXME: enables relocatability for experiments' framework headers until PCMs
4991/// are available.
4992
4993void ROOT::TMetaUtils::SetPathsForRelocatability(std::vector<std::string>& clingArgs )
4994{
4995 const char* envInclPath = getenv("ROOT_INCLUDE_PATH");
4996
4997 if (!envInclPath)
4998 return;
4999 std::istringstream envInclPathsStream(envInclPath);
5000 std::string inclPath;
5001 while (std::getline(envInclPathsStream, inclPath, ':')) {
5002 // Can't use TSystem in here; re-implement TSystem::ExpandPathName().
5003 replaceEnvVars("ROOT_INCLUDE_PATH", inclPath);
5004 if (!inclPath.empty()) {
5005 clingArgs.push_back("-I");
5006 clingArgs.push_back(inclPath);
5007 }
5008 }
5009}
5010
5011////////////////////////////////////////////////////////////////////////////////
5012
5013void ROOT::TMetaUtils::ReplaceAll(std::string& str, const std::string& from, const std::string& to,bool recurse)
5014{
5015 if(from.empty())
5016 return;
5017 size_t start_pos = 0;
5018 bool changed=true;
5019 while (changed){
5020 changed=false;
5021 start_pos = 0;
5022 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
5023 str.replace(start_pos, from.length(), to);
5024 start_pos += to.length();
5025 if (recurse) changed = true;
5026 }
5027 }
5028}
5029
5030////////////////////////////////////////////////////////////////////////////////
5031/// Return the separator suitable for this platform.
5033{
5035}
5036
5037////////////////////////////////////////////////////////////////////////////////
5038
5039bool ROOT::TMetaUtils::EndsWith(const std::string &theString, const std::string &theSubstring)
5040{
5041 if (theString.size() < theSubstring.size()) return false;
5042 const unsigned int theSubstringSize = theSubstring.size();
5043 return 0 == theString.compare(theString.size() - theSubstringSize,
5044 theSubstringSize,
5045 theSubstring);
5046}
5047
5048////////////////////////////////////////////////////////////////////////////////
5049
5050bool ROOT::TMetaUtils::BeginsWith(const std::string &theString, const std::string &theSubstring)
5051{
5052 if (theString.size() < theSubstring.size()) return false;
5053 const unsigned int theSubstringSize = theSubstring.size();
5054 return 0 == theString.compare(0,
5055 theSubstringSize,
5056 theSubstring);
5057}
5058
5059
5060
5061////////////////////////////////////////////////////////////////////////////////
5062
5064{
5065 // Note, should change this into take llvm::StringRef.
5066
5067 if ((strstr(filename, "LinkDef") || strstr(filename, "Linkdef") ||
5068 strstr(filename, "linkdef")) && strstr(filename, ".h")) {
5069 return true;
5070 }
5071 size_t len = strlen(filename);
5072 size_t linkdeflen = 9; /* strlen("linkdef.h") */
5073 if (len >= 9) {
5074 if (0 == strncasecmp(filename + (len - linkdeflen), "linkdef", linkdeflen - 2)
5075 && 0 == strcmp(filename + (len - 2), ".h")
5076 ) {
5077 return true;
5078 } else {
5079 return false;
5080 }
5081 } else {
5082 return false;
5083 }
5084}
5085
5086////////////////////////////////////////////////////////////////////////////////
5087
5089{
5090 return llvm::sys::path::extension(filename) == ".h" ||
5091 llvm::sys::path::extension(filename) == ".hh" ||
5092 llvm::sys::path::extension(filename) == ".hpp" ||
5093 llvm::sys::path::extension(filename) == ".H" ||
5094 llvm::sys::path::extension(filename) == ".h++" ||
5095 llvm::sys::path::extension(filename) == "hxx" ||
5096 llvm::sys::path::extension(filename) == "Hxx" ||
5097 llvm::sys::path::extension(filename) == "HXX";
5098}
5099
5100////////////////////////////////////////////////////////////////////////////////
5101
5102const std::string ROOT::TMetaUtils::AST2SourceTools::Decls2FwdDecls(const std::vector<const clang::Decl *> &decls,
5103 cling::Interpreter::IgnoreFilesFunc_t ignoreFiles,
5104 const cling::Interpreter &interp,
5105 std::string *logs)
5106{
5107 clang::Sema &sema = interp.getSema();
5108 cling::Transaction theTransaction(sema);
5109 std::set<clang::Decl *> addedDecls;
5110 for (auto decl : decls) {
5111 // again waiting for cling
5112 clang::Decl *ncDecl = const_cast<clang::Decl *>(decl);
5113 theTransaction.append(ncDecl);
5114 }
5115 std::string newFwdDecl;
5116 llvm::raw_string_ostream llvmOstr(newFwdDecl);
5117
5118 std::string locallogs;
5119 llvm::raw_string_ostream llvmLogStr(locallogs);
5120 interp.forwardDeclare(theTransaction, sema.getPreprocessor(), sema.getASTContext(), llvmOstr, true,
5121 logs ? &llvmLogStr : nullptr, ignoreFiles);
5122 llvmOstr.flush();
5123 llvmLogStr.flush();
5124 if (logs)
5125 logs->swap(locallogs);
5126 return newFwdDecl;
5127}
5128
5129////////////////////////////////////////////////////////////////////////////////
5130/// Take the namespaces which enclose the decl and put them around the
5131/// definition string.
5132/// For example, if the definition string is "myClass" which is enclosed by
5133/// the namespaces ns1 and ns2, one would get:
5134/// namespace ns2{ namespace ns1 { class myClass; } }
5135
5137 std::string& defString)
5138{
5139 auto rcd = EncloseInScopes(decl, defString);
5140 return rcd ? 1:0;
5141}
5142
5143////////////////////////////////////////////////////////////////////////////////
5144/// Take the scopes which enclose the decl and put them around the
5145/// definition string.
5146/// If a class is encountered, bail out.
5147
5148const clang::RecordDecl* ROOT::TMetaUtils::AST2SourceTools::EncloseInScopes(const clang::Decl& decl,
5149 std::string& defString)
5150{
5151 std::list<std::pair<std::string,unsigned int> > enclosingNamespaces;
5152 auto rcdPtr = ROOT::TMetaUtils::ExtractEnclosingScopes(decl,enclosingNamespaces);
5153
5154 if (rcdPtr) return rcdPtr;
5155
5156 // Check if we have enclosing namespaces
5157 static const std::string scopeType [] = {"namespace ", "inline namespace ", "class "};
5158
5159 std::string scopeName;
5160 std::string scopeContent;
5161 unsigned int scopeIndex;
5162 for (auto const & encScope : enclosingNamespaces){
5163 scopeIndex = encScope.second;
5164 scopeName = encScope.first;
5165 scopeContent = " { " + defString + " }";
5166 defString = scopeType[scopeIndex] +
5167 scopeName +
5168 scopeContent;
5169 }
5170 return nullptr;
5171}
5172
5173////////////////////////////////////////////////////////////////////////////////
5174/// Loop over the template parameters and build a string for template arguments
5175/// using the fully qualified name
5176/// There are different cases:
5177/// Case 1: a simple template parameter
5178/// E.g. `template<typename T> class A;`
5179/// Case 2: a non-type: either an integer or an enum
5180/// E.g. `template<int I, Foo > class A;` where `Foo` is `enum Foo {red, blue};`
5181/// 2 sub cases here:
5182/// SubCase 2.a: the parameter is an enum: bail out, cannot be treated.
5183/// SubCase 2.b: use the fully qualified name
5184/// Case 3: a TemplateTemplate argument
5185/// E.g. `template <template <typename> class T> class container { };`
5186
5188 const clang::TemplateParameterList& tmplParamList,
5189 const cling::Interpreter& interpreter)
5190{
5191 templateArgs="<";
5192 for (auto prmIt = tmplParamList.begin();
5193 prmIt != tmplParamList.end(); prmIt++){
5194
5195 if (prmIt != tmplParamList.begin())
5196 templateArgs += ", ";
5197
5198 auto nDecl = *prmIt;
5199 std::string typeName;
5200
5201 // Case 1
5202 if (llvm::isa<clang::TemplateTypeParmDecl>(nDecl)){
5203 typeName = "typename ";
5204 if (nDecl->isParameterPack())
5205 typeName += "... ";
5206 typeName += (*prmIt)->getNameAsString();
5207 }
5208 // Case 2
5209 else if (auto nttpd = llvm::dyn_cast<clang::NonTypeTemplateParmDecl>(nDecl)){
5210 auto theType = nttpd->getType();
5211 // If this is an enum, use int as it is impossible to fwd declare and
5212 // this makes sense since it is not a type...
5213 if (theType.getAsString().find("enum") != std::string::npos){
5214 std::string astDump;
5215 llvm::raw_string_ostream ostream(astDump);
5216 nttpd->dump(ostream);
5217 ostream.flush();
5218 ROOT::TMetaUtils::Warning(nullptr,"Forward declarations of templates with enums as template parameters. The responsible class is: %s\n", astDump.c_str());
5219 return 1;
5220 } else {
5222 theType,
5223 interpreter);
5224 }
5225 }
5226 // Case 3: TemplateTemplate argument
5227 else if (auto ttpd = llvm::dyn_cast<clang::TemplateTemplateParmDecl>(nDecl)){
5228 int retCode = FwdDeclFromTmplDecl(*ttpd,interpreter,typeName);
5229 if (retCode!=0){
5230 std::string astDump;
5231 llvm::raw_string_ostream ostream(astDump);
5232 ttpd->dump(ostream);
5233 ostream.flush();
5234 ROOT::TMetaUtils::Error(nullptr,"Cannot reconstruct template template parameter forward declaration for %s\n", astDump.c_str());
5235 return 1;
5236 }
5237 }
5238
5239 templateArgs += typeName;
5240 }
5241
5242 templateArgs+=">";
5243 return 0;
5244}
5245
5246////////////////////////////////////////////////////////////////////////////////
5247/// Convert a tmplt decl to its fwd decl
5248
5249int ROOT::TMetaUtils::AST2SourceTools::FwdDeclFromTmplDecl(const clang::TemplateDecl& templDecl,
5250 const cling::Interpreter& interpreter,
5251 std::string& defString)
5252{
5253 std::string templatePrefixString;
5254 auto tmplParamList= templDecl.getTemplateParameters();
5255 if (!tmplParamList){ // Should never happen
5256 Error(nullptr,
5257 "Cannot extract template parameter list for %s",
5258 templDecl.getNameAsString().c_str());
5259 return 1;
5260 }
5261
5262 int retCode = PrepareArgsForFwdDecl(templatePrefixString,*tmplParamList,interpreter);
5263 if (retCode!=0){
5264 Warning(nullptr,
5265 "Problems with arguments for forward declaration of class %s\n",
5266 templDecl.getNameAsString().c_str());
5267 return retCode;
5268 }
5269 templatePrefixString = "template " + templatePrefixString + " ";
5270
5271 defString = templatePrefixString + "class ";
5272 if (templDecl.isParameterPack())
5273 defString += "... ";
5274 defString += templDecl.getNameAsString();
5275 if (llvm::isa<clang::TemplateTemplateParmDecl>(&templDecl)) {
5276 // When fwd declaring the template template arg of
5277 // namespace N { template <template <class T> class C> class X; }
5278 // we don't need to put it into any namespace, and we want no trailing
5279 // ';'
5280 return 0;
5281 }
5282 defString += ';';
5283 return EncloseInNamespaces(templDecl, defString);
5284}
5285
5286////////////////////////////////////////////////////////////////////////////////
5287
5288static int TreatSingleTemplateArg(const clang::TemplateArgument& arg,
5289 std::string& argFwdDecl,
5290 const cling::Interpreter& interpreter,
5291 bool acceptStl=false)
5292{
5293 using namespace ROOT::TMetaUtils::AST2SourceTools;
5294
5295 // We do nothing in presence of ints, bools, templates.
5296 // We should probably in presence of templates though...
5297 if (clang::TemplateArgument::Type != arg.getKind()) return 0;
5298
5299 auto argQualType = arg.getAsType();
5300
5301 // Recursively remove all *
5302 while (llvm::isa<clang::PointerType>(argQualType.getTypePtr())) argQualType = argQualType->getPointeeType();
5303
5304 auto argTypePtr = argQualType.getTypePtr();
5305
5306 // Bail out on enums
5307 if (llvm::isa<clang::EnumType>(argTypePtr)){
5308 return 1;
5309 }
5310
5311 // If this is a built-in, just return: fwd decl not necessary.
5312 if (llvm::isa<clang::BuiltinType>(argTypePtr)){
5313 return 0;
5314 }
5315
5316 // Treat typedefs which are arguments
5317 if (auto tdTypePtr = llvm::dyn_cast<clang::TypedefType>(argTypePtr)) {
5318 FwdDeclFromTypeDefNameDecl(*tdTypePtr->getDecl(), interpreter, argFwdDecl);
5319 return 0;
5320 }
5321
5322 if (auto argRecTypePtr = llvm::dyn_cast<clang::RecordType>(argTypePtr)){
5323 // Now we cannot but have a RecordType
5324 if (auto argRecDeclPtr = argRecTypePtr->getDecl()){
5325 FwdDeclFromRcdDecl(*argRecDeclPtr,interpreter,argFwdDecl,acceptStl);
5326 }
5327 return 0;
5328 }
5329
5330 return 1;
5331}
5332
5333////////////////////////////////////////////////////////////////////////////////
5334/// Convert a tmplt decl to its fwd decl
5335
5336int ROOT::TMetaUtils::AST2SourceTools::FwdDeclIfTmplSpec(const clang::RecordDecl& recordDecl,
5337 const cling::Interpreter& interpreter,
5338 std::string& defString,
5339 const std::string &normalizedName)
5340{
5341 // If this is an explicit specialization, inject it into cling, too, such that it can have
5342 // externalLexicalStorage, see TCling.cxx's ExtVisibleStorageAdder::VisitClassTemplateSpecializationDecl.
5343 if (auto tmplSpecDeclPtr = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(&recordDecl)) {
5344 if (const auto *specDef = tmplSpecDeclPtr->getDefinition()) {
5345 if (specDef->getTemplateSpecializationKind() != clang::TSK_ExplicitSpecialization)
5346 return 0;
5347 // normalizedName contains scope, no need to enclose in namespace!
5349 std::cout << " Forward declaring template spec " << normalizedName << ":\n";
5350 for (auto arg : tmplSpecDeclPtr->getTemplateArgs().asArray()) {
5351 std::string argFwdDecl;
5352 int retCode = TreatSingleTemplateArg(arg, argFwdDecl, interpreter, /*acceptStl=*/false);
5354 std::cout << " o Template argument ";
5355 if (retCode == 0) {
5356 std::cout << "successfully treated. Arg fwd decl: " << argFwdDecl << std::endl;
5357 } else {
5358 std::cout << "could not be treated. Abort fwd declaration generation.\n";
5359 }
5360 }
5361
5362 if (retCode != 0) { // A sign we must bail out
5363 return retCode;
5364 }
5365 defString += argFwdDecl + '\n';
5366 }
5367 defString += "template <> class " + normalizedName + ';';
5368 return 0;
5369 }
5370 }
5371
5372 return 0;
5373}
5374
5375////////////////////////////////////////////////////////////////////////////////
5376/// Convert a rcd decl to its fwd decl
5377/// If this is a template specialisation, treat in the proper way.
5378/// If it is contained in a class, just fwd declare the class.
5379
5380int ROOT::TMetaUtils::AST2SourceTools::FwdDeclFromRcdDecl(const clang::RecordDecl& recordDecl,
5381 const cling::Interpreter& interpreter,
5382 std::string& defString,
5383 bool acceptStl)
5384{
5385 // Do not fwd declare the templates in the stl.
5386 if (ROOT::TMetaUtils::IsStdClass(recordDecl) && !acceptStl)
5387 return 0;
5388
5389 // Do not fwd declare unnamed decls.
5390 if (!recordDecl.getIdentifier())
5391 return 0;
5392
5393 // We may need to fwd declare the arguments of the template
5394 std::string argsFwdDecl;
5395
5396 if (auto tmplSpecDeclPtr = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(&recordDecl)){
5397 std::string argFwdDecl;
5399 std::cout << "Class " << recordDecl.getNameAsString()
5400 << " is a template specialisation. Treating its arguments.\n";
5401 for(auto arg : tmplSpecDeclPtr->getTemplateArgs().asArray()){
5402 int retCode = TreatSingleTemplateArg(arg, argFwdDecl, interpreter, acceptStl);
5404 std::cout << " o Template argument ";
5405 if (retCode==0){
5406 std::cout << "successfully treated. Arg fwd decl: " << argFwdDecl << std::endl;
5407 } else {
5408 std::cout << "could not be treated. Abort fwd declaration generation.\n";
5409 }
5410 }
5411
5412 if (retCode!=0){ // A sign we must bail out
5413 return retCode;
5414 }
5415 argsFwdDecl+=argFwdDecl;
5416 }
5417
5418 if (acceptStl){
5419 defString=argsFwdDecl;
5420 return 0;
5421 }
5422
5423 int retCode=0;
5424 if (auto tmplDeclPtr = tmplSpecDeclPtr->getSpecializedTemplate()){
5425 retCode = FwdDeclFromTmplDecl(*tmplDeclPtr,interpreter,defString);
5426 }
5427 defString = argsFwdDecl + "\n" + defString;
5428 return retCode;
5429
5430 }
5431
5432 defString = "class " + recordDecl.getNameAsString() + ";";
5433 const clang::RecordDecl* rcd = EncloseInScopes(recordDecl, defString);
5434
5435 if (rcd){
5436 FwdDeclFromRcdDecl(*rcd, interpreter,defString);
5437 }
5438 // Add a \n here to avoid long lines which contain duplications, for example (from MathCore):
5439 // namespace ROOT { namespace Math { class IBaseFunctionMultiDim; } }namespace ROOT { namespace Fit { template <typename FunType> class Chi2FCN; } }
5440 // namespace ROOT { namespace Math { class IGradientFunctionMultiDim; } }namespace ROOT { namespace Fit { template <typename FunType> class Chi2FCN; } }
5441 defString = argsFwdDecl + "\n" + defString;
5442
5443 return 0;
5444}
5445
5446////////////////////////////////////////////////////////////////////////////////
5447/// Extract "forward declaration" of a typedef.
5448/// If the typedef is contained in a class, just fwd declare the class.
5449/// If not, fwd declare the typedef and all the dependent typedefs and types if necessary.
5450
5452 const cling::Interpreter& interpreter,
5453 std::string& fwdDeclString,
5454 std::unordered_set<std::string>* fwdDeclSetPtr)
5455{
5456 std::string buffer = tdnDecl.getNameAsString();
5457 std::string underlyingName;
5458 auto underlyingType = tdnDecl.getUnderlyingType().getCanonicalType();
5459 if (const clang::TagType* TT
5460 = llvm::dyn_cast<clang::TagType>(underlyingType.getTypePtr())) {
5461 if (clang::NamedDecl* ND = TT->getDecl()) {
5462 if (!ND->getIdentifier()) {
5463 // No fwd decl for unnamed underlying entities.
5464 return 0;
5465 }
5466 }
5467 }
5468
5469 TNormalizedCtxt nCtxt(interpreter.getLookupHelper());
5471 underlyingType,
5472 interpreter,
5473 nCtxt);
5474
5475 // Heuristic: avoid entities like myclass<myType1, myType2::xyz>
5476 if (underlyingName.find(">::") != std::string::npos)
5477 return 0;
5478
5479 buffer="typedef "+underlyingName+" "+buffer+";";
5480 const clang::RecordDecl* rcd=EncloseInScopes(tdnDecl,buffer);
5481 if (rcd) {
5482 // We do not need the whole series of scopes, just the class.
5483 // It is enough to trigger an uncomplete type autoload/parse callback
5484 // for example: MyClass::blabla::otherNs::myTypedef
5485 return FwdDeclFromRcdDecl(*rcd, interpreter,fwdDeclString,fwdDeclSetPtr);
5486 }
5487
5488 // Start Recursion if the underlying type is a TypedefNameDecl
5489 // Note: the simple cast w/o the getSingleStepDesugaredType call
5490 // does not work in case the typedef is in a namespace.
5491 auto& ctxt = tdnDecl.getASTContext();
5492 auto immediatelyUnderlyingType = underlyingType.getSingleStepDesugaredType(ctxt);
5493
5494 if (auto underlyingTdnTypePtr = llvm::dyn_cast<clang::TypedefType>(immediatelyUnderlyingType.getTypePtr())){
5495 std::string tdnFwdDecl;
5496 auto underlyingTdnDeclPtr = underlyingTdnTypePtr->getDecl();
5497 FwdDeclFromTypeDefNameDecl(*underlyingTdnDeclPtr,
5498 interpreter,
5499 tdnFwdDecl,
5500 fwdDeclSetPtr);
5501 if (!fwdDeclSetPtr || fwdDeclSetPtr->insert(tdnFwdDecl).second)
5502 fwdDeclString+=tdnFwdDecl;
5503 } else if (auto CXXRcdDeclPtr = immediatelyUnderlyingType->getAsCXXRecordDecl()){
5504 std::string classFwdDecl;
5506 std::cout << "Typedef " << tdnDecl.getNameAsString() << " hides a class: "
5507 << CXXRcdDeclPtr->getNameAsString() << std::endl;
5508 int retCode = FwdDeclFromRcdDecl(*CXXRcdDeclPtr,
5509 interpreter,
5510 classFwdDecl,
5511 true /* acceptStl*/);
5512 if (retCode!=0){ // bail out
5513 return 0;
5514 }
5515
5516 if (!fwdDeclSetPtr || fwdDeclSetPtr->insert(classFwdDecl).second)
5517 fwdDeclString+=classFwdDecl;
5518 }
5519
5520 fwdDeclString+=buffer;
5521
5522 return 0;
5523}
5524
5525////////////////////////////////////////////////////////////////////////////////
5526/// Get the default value as string.
5527/// Limited at the moment to:
5528/// - Integers
5529/// - Booleans
5530
5531int ROOT::TMetaUtils::AST2SourceTools::GetDefArg(const clang::ParmVarDecl& par,
5532 std::string& valAsString,
5533 const clang::PrintingPolicy& ppolicy)
5534{
5535 auto defArgExprPtr = par.getDefaultArg();
5536 auto& ctxt = par.getASTContext();
5537 if(!defArgExprPtr->isEvaluatable(ctxt)){
5538 return -1;
5539 }
5540
5541 auto defArgType = par.getType();
5542
5543 // The value is a boolean
5544 if (defArgType->isBooleanType()){
5545 bool result;
5546 defArgExprPtr->EvaluateAsBooleanCondition (result,ctxt);
5547 valAsString=std::to_string(result);
5548 return 0;
5549 }
5550
5551 // The value is an integer
5552 if (defArgType->isIntegerType()){
5553 clang::Expr::EvalResult evalResult;
5554 defArgExprPtr->EvaluateAsInt(evalResult, ctxt);
5555 llvm::APSInt result = evalResult.Val.getInt();
5556 auto uintVal = *result.getRawData();
5557 if (result.isNegative()){
5558 long long int intVal=uintVal*-1;
5559 valAsString=std::to_string(intVal);
5560 } else {
5561 valAsString=std::to_string(uintVal);
5562 }
5563
5564 return 0;
5565 }
5566
5567 // The value is something else. We go for the generalised printer
5568 llvm::raw_string_ostream rso(valAsString);
5569 defArgExprPtr->printPretty(rso,nullptr,ppolicy);
5570 valAsString = rso.str();
5571 // We can be in presence of a string. Let's escape the characters properly.
5572 ROOT::TMetaUtils::ReplaceAll(valAsString,"\\\"","__TEMP__VAL__");
5573 ROOT::TMetaUtils::ReplaceAll(valAsString,"\"","\\\"");
5574 ROOT::TMetaUtils::ReplaceAll(valAsString,"__TEMP__VAL__","\\\"");
5575
5576 return 0;
5577}
5578
The file contains utilities which are foundational and could be used across the core component of ROO...
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
static Roo_reg_AGKInteg1D instance
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
short Version_t
Definition RtypesCore.h:65
static void indent(ostringstream &buf, int indent_level)
static bool RecurseKeepNParams(clang::TemplateArgument &normTArg, const clang::TemplateArgument &tArg, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt, const clang::ASTContext &astCtxt)
static clang::SourceLocation getFinalSpellingLoc(clang::SourceManager &sourceManager, clang::SourceLocation sourceLoc)
const clang::DeclContext * GetEnclosingSpace(const clang::RecordDecl &cl)
bool IsTemplate(const clang::Decl &cl)
static void KeepNParams(clang::QualType &normalizedType, const clang::QualType &vanillaType, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
This function allows to manipulate the number of arguments in the type of a template specialisation.
static void CreateNameTypeMap(const clang::CXXRecordDecl &cl, ROOT::MembersTypeMap_t &nameType)
Create the data member name-type map for given class.
const clang::CXXMethodDecl * GetMethodWithProto(const clang::Decl *cinfo, const char *method, const char *proto, const cling::Interpreter &interp, bool diagnose)
int dumpDeclForAssert(const clang::Decl &D, const char *commentStart)
static void replaceEnvVars(const char *varname, std::string &txt)
Reimplementation of TSystem::ExpandPathName() that cannot be used from TMetaUtils.
static bool areEqualValues(const clang::TemplateArgument &tArg, const clang::NamedDecl &tPar)
std::cout << "Are equal values?\n";
static bool isTypeWithDefault(const clang::NamedDecl *nDecl)
Check if this NamedDecl is a template parameter with a default argument.
static int TreatSingleTemplateArg(const clang::TemplateArgument &arg, std::string &argFwdDecl, const cling::Interpreter &interpreter, bool acceptStl=false)
static bool areEqualTypes(const clang::TemplateArgument &tArg, llvm::SmallVectorImpl< clang::TemplateArgument > &preceedingTArgs, const clang::NamedDecl &tPar, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
static bool hasSomeTypedefSomewhere(const clang::Type *T)
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
const char * proto
Definition civetweb.c:17536
#define free
Definition civetweb.c:1539
const clang::RecordDecl * GetRecordDecl() const
const clang::RecordDecl * fDecl
const char * GetRequestedName() const
static std::string BuildDemangledTypeInfo(const clang::RecordDecl *rDecl, const std::string &normalizedName)
AnnotatedRecordDecl(long index, const clang::RecordDecl *decl, bool rStreamerInfo, bool rNoStreamer, bool rRequestNoInputOperator, bool rRequestOnlyTClass, int rRequestedVersionNumber, const cling::Interpreter &interpret, const TNormalizedCtxt &normCtxt)
There is no requested type name.
const std::string & GetDemangledTypeInfo() const
const char * GetNormalizedName() const
const clang::CXXRecordDecl * fArgType
const clang::CXXRecordDecl * GetType() const
RConstructorType(const char *type_of_arg, const cling::Interpreter &)
bool IsDeclaredScope(const std::string &base, bool &isInlined) override
bool IsAlreadyPartiallyDesugaredName(const std::string &nondef, const std::string &nameLong) override
ExistingTypeCheck_t fExistingTypeCheck
TClingLookupHelper(cling::Interpreter &interpreter, TNormalizedCtxt &normCtxt, ExistingTypeCheck_t existingTypeCheck, AutoParse_t autoParse, bool *shuttingDownPtr, const int *pgDebug=nullptr)
bool GetPartiallyDesugaredNameWithScopeHandling(const std::string &tname, std::string &result, bool dropstd=true) override
We assume that we have a simple type: [const] typename[*&][const].
void GetPartiallyDesugaredName(std::string &nameLong) override
bool ExistingTypeCheck(const std::string &tname, std::string &result) override
Helper routine to ry hard to avoid looking up in the Cling database as this could enduce an unwanted ...
const Config_t & GetConfig() const
TNormalizedCtxt::Config_t::SkipCollection DeclsCont_t
TNormalizedCtxt::TemplPtrIntMap_t TemplPtrIntMap_t
int GetNargsToKeep(const clang::ClassTemplateDecl *templ) const
Get from the map the number of arguments to keep.
TNormalizedCtxt::Config_t Config_t
TNormalizedCtxtImpl(const cling::LookupHelper &lh)
Initialize the list of typedef to keep (i.e.
TNormalizedCtxt::TypesCont_t TypesCont_t
void AddTemplAndNargsToKeep(const clang::ClassTemplateDecl *templ, unsigned int i)
Add to the internal map the pointer of a template as key and the number of template arguments to keep...
void keepTypedef(const cling::LookupHelper &lh, const char *name, bool replace=false)
Insert the type with name into the collection of typedefs to keep.
const TypesCont_t & GetTypeWithAlternative() const
const TemplPtrIntMap_t GetTemplNargsToKeepMap() const
static TemplPtrIntMap_t fTemplatePtrArgsToKeepMap
void AddTemplAndNargsToKeep(const clang::ClassTemplateDecl *templ, unsigned int i)
void keepTypedef(const cling::LookupHelper &lh, const char *name, bool replace=false)
cling::utils::Transform::Config Config_t
std::map< const clang::ClassTemplateDecl *, int > TemplPtrIntMap_t
TNormalizedCtxt(const cling::LookupHelper &lh)
TNormalizedCtxtImpl * fImpl
const TypesCont_t & GetTypeWithAlternative() const
std::set< const clang::Type * > TypesCont_t
int GetNargsToKeep(const clang::ClassTemplateDecl *templ) const
const Config_t & GetConfig() const
const TemplPtrIntMap_t GetTemplNargsToKeepMap() const
TIterator begin()
#define I(x, y, z)
const std::string & GetPathSeparator()
int EncloseInNamespaces(const clang::Decl &decl, std::string &defString)
Take the namespaces which enclose the decl and put them around the definition string.
int FwdDeclFromTypeDefNameDecl(const clang::TypedefNameDecl &tdnDecl, const cling::Interpreter &interpreter, std::string &fwdDeclString, std::unordered_set< std::string > *fwdDeclSet=nullptr)
Extract "forward declaration" of a typedef.
int PrepareArgsForFwdDecl(std::string &templateArgs, const clang::TemplateParameterList &tmplParamList, const cling::Interpreter &interpreter)
Loop over the template parameters and build a string for template arguments using the fully qualified...
int FwdDeclFromTmplDecl(const clang::TemplateDecl &tmplDecl, const cling::Interpreter &interpreter, std::string &defString)
Convert a tmplt decl to its fwd decl.
const clang::RecordDecl * EncloseInScopes(const clang::Decl &decl, std::string &defString)
Take the scopes which enclose the decl and put them around the definition string.
int FwdDeclFromRcdDecl(const clang::RecordDecl &recordDecl, const cling::Interpreter &interpreter, std::string &defString, bool acceptStl=false)
Convert a rcd decl to its fwd decl If this is a template specialisation, treat in the proper way.
int GetDefArg(const clang::ParmVarDecl &par, std::string &valAsString, const clang::PrintingPolicy &pp)
Get the default value as string.
int FwdDeclIfTmplSpec(const clang::RecordDecl &recordDecl, const cling::Interpreter &interpreter, std::string &defString, const std::string &normalizedName)
Convert a tmplt decl to its fwd decl.
const std::string Decls2FwdDecls(const std::vector< const clang::Decl * > &decls, bool(*ignoreFiles)(const clang::PresumedLoc &), const cling::Interpreter &interp, std::string *logs)
bool HasClassDefMacro(const clang::Decl *decl, const cling::Interpreter &interpreter)
Return true if class has any of class declarations like ClassDef, ClassDefNV, ClassDefOverride.
llvm::StringRef GetClassComment(const clang::CXXRecordDecl &decl, clang::SourceLocation *loc, const cling::Interpreter &interpreter)
Return the class comment after the ClassDef: class MyClass { ... ClassDef(MyClass,...
const T * GetAnnotatedRedeclarable(const T *Redecl)
int extractPropertyNameValFromString(const std::string attributeStr, std::string &attrName, std::string &attrValue)
bool hasOpaqueTypedef(clang::QualType instanceType, const TNormalizedCtxt &normCtxt)
Return true if the type is a Double32_t or Float16_t or is a instance template that depends on Double...
EIOCtorCategory CheckConstructor(const clang::CXXRecordDecl *, const RConstructorType &, const cling::Interpreter &interp)
Check if class has constructor of provided type - either default or with single argument.
clang::RecordDecl * GetUnderlyingRecordDecl(clang::QualType type)
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
const clang::FunctionDecl * ClassInfo__HasMethod(const clang::DeclContext *cl, char const *, const cling::Interpreter &interp)
bool GetNameWithinNamespace(std::string &, std::string &, std::string &, clang::CXXRecordDecl const *)
Return true if one of the class' enclosing scope is a namespace and set fullname to the fully qualifi...
const clang::RecordDecl * ExtractEnclosingScopes(const clang::Decl &decl, std::list< std::pair< std::string, unsigned int > > &enclosingSc)
Extract the names and types of containing scopes.
bool HasCustomOperatorNewArrayPlacement(clang::RecordDecl const &, const cling::Interpreter &interp)
return true if we can find a custom operator new with placement
void Error(const char *location, const char *fmt,...)
void WriteClassInit(std::ostream &finalString, const AnnotatedRecordDecl &cl, const clang::CXXRecordDecl *decl, const cling::Interpreter &interp, const TNormalizedCtxt &normCtxt, const RConstructorTypes &ctorTypes, bool &needCollectionProxy)
FIXME: a function of 450+ lines!
void Info(const char *location, const char *fmt,...)
int WriteNamespaceHeader(std::ostream &, const clang::RecordDecl *)
int GetClassVersion(const clang::RecordDecl *cl, const cling::Interpreter &interp)
Return the version number of the class or -1 if the function Class_Version does not exist.
clang::QualType GetTypeForIO(const clang::QualType &templateInstanceType, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt, TClassEdit::EModType mode=TClassEdit::kNone)
int extractAttrString(clang::Attr *attribute, std::string &attrString)
Extract attr string.
void GetNormalizedName(std::string &norm_name, const clang::QualType &type, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt)
Return the type name normalized for ROOT, keeping only the ROOT opaque typedef (Double32_t,...
void WritePointersSTL(const AnnotatedRecordDecl &cl, const cling::Interpreter &interp, const TNormalizedCtxt &normCtxt)
Write interface function for STL members.
bool HasNewMerge(clang::CXXRecordDecl const *, const cling::Interpreter &)
Return true if the class has a method Merge(TCollection*,TFileMergeInfo*)
bool CheckPublicFuncWithProto(clang::CXXRecordDecl const *, char const *, char const *, const cling::Interpreter &, bool diagnose)
Return true, if the function (defined by the name and prototype) exists and is public.
std::string GetFileName(const clang::Decl &decl, const cling::Interpreter &interp)
Return the header file to be included to declare the Decl.
clang::ClassTemplateDecl * QualType2ClassTemplateDecl(const clang::QualType &qt)
Extract from a qualtype the class template if this makes sense.
int IsSTLContainer(const AnnotatedRecordDecl &annotated)
Is this an STL container.
void Fatal(const char *location, const char *fmt,...)
std::list< RConstructorType > RConstructorTypes
int extractPropertyNameVal(clang::Attr *attribute, std::string &attrName, std::string &attrValue)
std::string GetModuleFileName(const char *moduleName)
Return the dictionary file name for a module.
clang::QualType ReSubstTemplateArg(clang::QualType input, const clang::Type *instance)
Check if 'input' or any of its template parameter was substituted when instantiating the class templa...
bool NeedDestructor(clang::CXXRecordDecl const *, const cling::Interpreter &)
bool EndsWith(const std::string &theString, const std::string &theSubstring)
void GetFullyQualifiedTypeName(std::string &name, const clang::QualType &type, const cling::Interpreter &interpreter)
bool NeedTemplateKeyword(clang::CXXRecordDecl const *)
bool HasCustomConvStreamerMemberFunction(const AnnotatedRecordDecl &cl, const clang::CXXRecordDecl *clxx, const cling::Interpreter &interp, const TNormalizedCtxt &normCtxt)
Return true if the class has a custom member function streamer.
bool HasDirectoryAutoAdd(clang::CXXRecordDecl const *, const cling::Interpreter &)
Return true if the class has a method DirectoryAutoAdd(TDirectory *)
const clang::FunctionDecl * GetFuncWithProto(const clang::Decl *cinfo, const char *method, const char *proto, const cling::Interpreter &gInterp, bool diagnose)
int ElementStreamer(std::ostream &finalString, const clang::NamedDecl &forcontext, const clang::QualType &qti, const char *t, int rwmode, const cling::Interpreter &interp, const char *tcl=nullptr)
bool MatchWithDeclOrAnyOfPrevious(const clang::CXXRecordDecl &cl, const clang::CXXRecordDecl &currentCl)
This is a recursive function.
clang::QualType GetNormalizedType(const clang::QualType &type, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt)
Return the type normalized for ROOT, keeping only the ROOT opaque typedef (Double32_t,...
const char * ShortTypeName(const char *typeDesc)
Return the absolute type of typeDesc.
void GetCppName(std::string &output, const char *input)
Return (in the argument 'output') a mangled version of the C++ symbol/type (pass as 'input') that can...
bool HasCustomOperatorNewPlacement(char const *, clang::RecordDecl const &, const cling::Interpreter &)
return true if we can find a custom operator new with placement
bool IsStdClass(const clang::RecordDecl &cl)
Return true, if the decl is part of the std namespace.
bool HasResetAfterMerge(clang::CXXRecordDecl const *, const cling::Interpreter &)
Return true if the class has a method ResetAfterMerge(TFileMergeInfo *)
ROOT::ESTLType STLKind(const llvm::StringRef type)
Converts STL container name to number. vector -> 1, etc..
void WriteClassCode(CallWriteStreamer_t WriteStreamerFunc, const AnnotatedRecordDecl &cl, const cling::Interpreter &interp, const TNormalizedCtxt &normCtxt, std::ostream &finalString, const RConstructorTypes &ctorTypes, bool isGenreflex)
Generate the code of the class If the requestor is genreflex, request the new streamer format.
bool HasOldMerge(clang::CXXRecordDecl const *, const cling::Interpreter &)
Return true if the class has a method Merge(TCollection*)
clang::TemplateName ExtractTemplateNameFromQualType(const clang::QualType &qt)
These manipulations are necessary because a template specialisation type does not inherit from a reco...
int RemoveTemplateArgsFromName(std::string &name, unsigned int)
Remove the last n template arguments from the name.
long GetLineNumber(clang::Decl const *)
It looks like the template specialization decl actually contains less information on the location of ...
void WriteAuxFunctions(std::ostream &finalString, const AnnotatedRecordDecl &cl, const clang::CXXRecordDecl *decl, const cling::Interpreter &interp, const RConstructorTypes &ctorTypes, const TNormalizedCtxt &normCtxt)
std::string NormalizedName; GetNormalizedName(NormalizedName, decl->getASTContext()....
void foreachHeaderInModule(const clang::Module &module, const std::function< void(const clang::Module::Header &)> &closure, bool includeDirectlyUsedModules=true)
Calls the given lambda on every header in the given module.
bool IsBase(const clang::CXXRecordDecl *cl, const clang::CXXRecordDecl *base, const clang::CXXRecordDecl *context, const cling::Interpreter &interp)
void ExtractCtxtEnclosingNameSpaces(const clang::DeclContext &, std::list< std::pair< std::string, bool > > &)
Extract enclosing namespaces recursively.
std::pair< bool, int > GetTrivialIntegralReturnValue(const clang::FunctionDecl *funcCV, const cling::Interpreter &interp)
If the function contains 'just': return SomeValue; this routine will extract this value and return it...
bool HasCustomStreamerMemberFunction(const AnnotatedRecordDecl &cl, const clang::CXXRecordDecl *clxx, const cling::Interpreter &interp, const TNormalizedCtxt &normCtxt)
Return true if the class has a custom member function streamer.
std::string GetRealPath(const std::string &path)
clang::QualType AddDefaultParameters(clang::QualType instanceType, const cling::Interpreter &interpret, const TNormalizedCtxt &normCtxt)
Add any unspecified template parameters to the class template instance, mentioned anywhere in the typ...
std::pair< std::string, clang::QualType > GetNameTypeForIO(const clang::QualType &templateInstanceType, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt, TClassEdit::EModType mode=TClassEdit::kNone)
void GetQualifiedName(std::string &qual_name, const clang::QualType &type, const clang::NamedDecl &forcontext)
Main implementation relying on GetFullyQualifiedTypeName All other GetQualifiedName functions leverag...
bool ExtractAttrIntPropertyFromName(const clang::Decl &decl, const std::string &propName, int &propValue)
This routine counts on the "propName<separator>propValue" format.
bool IsLinkdefFile(const char *filename)
llvm::StringRef GetComment(const clang::Decl &decl, clang::SourceLocation *loc=nullptr)
Returns the comment (// striped away), annotating declaration in a meaningful for ROOT IO way.
void SetPathsForRelocatability(std::vector< std::string > &clingArgs)
Organise the parameters for cling in order to guarantee relocatability It treats the gcc toolchain an...
bool IsStreamableObject(const clang::FieldDecl &m, const cling::Interpreter &interp)
void ReplaceAll(std::string &str, const std::string &from, const std::string &to, bool recurse=false)
bool QualType2Template(const clang::QualType &qt, clang::ClassTemplateDecl *&ctd, clang::ClassTemplateSpecializationDecl *&ctsd)
Get the template specialisation decl and template decl behind the qualtype Returns true if successful...
std::string TrueName(const clang::FieldDecl &m)
TrueName strips the typedefs and array dimensions.
const clang::Type * GetUnderlyingType(clang::QualType type)
Return the base/underlying type of a chain of array or pointers type.
ROOT::ESTLType IsSTLCont(const clang::RecordDecl &cl)
type : type name: vector<list<classA,allocator>,allocator> result: 0 : not stl container abs(result):...
bool IsStdDropDefaultClass(const clang::RecordDecl &cl)
Return true, if the decl is part of the std namespace and we want its default parameter dropped.
bool RequireCompleteType(const cling::Interpreter &interp, const clang::CXXRecordDecl *cl)
bool IsHeaderName(const std::string &filename)
void Warning(const char *location, const char *fmt,...)
bool IsOfType(const clang::CXXRecordDecl &cl, const std::string &type, const cling::LookupHelper &lh)
const std::string & GetPathSeparator()
Return the separator suitable for this platform.
bool CheckDefaultConstructor(const clang::CXXRecordDecl *, const cling::Interpreter &interp)
Checks if default constructor exists and accessible.
EIOCtorCategory CheckIOConstructor(const clang::CXXRecordDecl *, const char *, const clang::CXXRecordDecl *, const cling::Interpreter &interp)
Checks IO constructor - must be public and with specified argument.
bool ExtractAttrPropertyFromName(const clang::Decl &decl, const std::string &propName, std::string &propValue)
This routine counts on the "propName<separator>propValue" format.
const clang::CXXRecordDecl * ScopeSearch(const char *name, const cling::Interpreter &gInterp, bool diagnose, const clang::Type **resultType)
Return the scope corresponding to 'name' or std::'name'.
int & GetErrorIgnoreLevel()
void ExtractEnclosingNameSpaces(const clang::Decl &, std::list< std::pair< std::string, bool > > &)
Extract the immediately outer namespace and then launch the recursion.
bool HasIOConstructor(clang::CXXRecordDecl const *, std::string &, const RConstructorTypes &, const cling::Interpreter &)
return true if we can find an constructor calleable without any arguments or with one the IOCtor spec...
llvm::StringRef DataMemberInfo__ValidArrayIndex(const cling::Interpreter &interp, const clang::DeclaratorDecl &m, int *errnum=nullptr, llvm::StringRef *errstr=nullptr)
ValidArrayIndex return a static string (so use it or copy it immediatly, do not call GrabIndex twice ...
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void WriteSchemaList(std::list< SchemaRuleMap_t > &rules, const std::string &listName, std::ostream &output)
Write schema rules.
std::map< std::string, ROOT::Internal::TSchemaType > MembersTypeMap_t
ESTLType
Definition ESTLType.h:28
@ kSTLbitset
Definition ESTLType.h:37
@ kSTLmap
Definition ESTLType.h:33
@ kSTLunorderedmultiset
Definition ESTLType.h:43
@ kROOTRVec
Definition ESTLType.h:46
@ kSTLend
Definition ESTLType.h:47
@ kSTLset
Definition ESTLType.h:35
@ kSTLmultiset
Definition ESTLType.h:36
@ kSTLdeque
Definition ESTLType.h:32
@ kSTLvector
Definition ESTLType.h:30
@ kSTLunorderedmultimap
Definition ESTLType.h:45
@ kSTLunorderedset
Definition ESTLType.h:42
@ kSTLlist
Definition ESTLType.h:31
@ kSTLforwardlist
Definition ESTLType.h:41
@ kSTLunorderedmap
Definition ESTLType.h:44
@ kNotSTL
Definition ESTLType.h:29
@ kSTLmultimap
Definition ESTLType.h:34
void WriteReadRuleFunc(SchemaRuleMap_t &rule, int index, std::string &mappedName, MembersTypeMap_t &members, std::ostream &output)
Write the conversion function for Read rule, the function name is being written to rule["funcname"].
R__EXTERN SchemaRuleClassMap_t gReadRules
bool HasValidDataMembers(SchemaRuleMap_t &rule, MembersTypeMap_t &members, std::string &error_string)
Check if given rule contains references to valid data members.
void WriteReadRawRuleFunc(SchemaRuleMap_t &rule, int index, std::string &mappedName, MembersTypeMap_t &members, std::ostream &output)
Write the conversion function for ReadRaw rule, the function name is being written to rule["funcname"...
R__EXTERN SchemaRuleClassMap_t gReadRawRules
ROOT::ESTLType STLKind(std::string_view type)
Converts STL container name to number.
bool IsStdClass(const char *type)
return true if the class belongs to the std namespace
std::string GetLong64_Name(const char *original)
Replace 'long long' and 'unsigned long long' by 'Long64_t' and 'ULong64_t'.
ROOT::ESTLType IsSTLCont(std::string_view type)
type : type name: vector<list<classA,allocator>,allocator> result: 0 : not stl container code of cont...
char * DemangleName(const char *mangled_name, int &errorCode)
Definition TClassEdit.h:208
std::string GetNameForIO(const std::string &templateInstanceName, TClassEdit::EModType mode=TClassEdit::kNone, bool *hasChanged=nullptr)
@ kKeepOuterConst
Definition TClassEdit.h:87
@ kDropStlDefault
Definition TClassEdit.h:82
bool IsSTLBitset(const char *type)
Return true is the name is std::bitset<number> or bitset<number>
constexpr Double_t C()
Velocity of light in .
Definition TMath.h:114
static const char * what
Definition stlLoader.cc:5
void ShortType(std::string &answer, int mode)
Return the absolute type of typeDesc into the string answ.
TMarker m
Definition textangle.C:8
auto * tt
Definition textangle.C:16