Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RStl.cxx
Go to the documentation of this file.
1// @(#)root/utils:$Id$
2// Author: Philippe Canal 27/08/2003
3
4/*************************************************************************
5 * Copyright (C) 1995-2003, Rene Brun, Fons Rademakers, and al. *
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#include "RConfigure.h"
13#include <ROOT/RConfig.hxx>
14
15#include "RStl.h"
16#include "TClassEdit.h"
17#include "TClingUtils.h"
18using namespace TClassEdit;
19
20#include <stdio.h>
21
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclTemplate.h"
24
25#include "cling/Interpreter/Interpreter.h"
26#include "cling/Interpreter/LookupHelper.h"
27
28#include "clang/Sema/Sema.h"
29#include "clang/Sema/Template.h"
30#include "clang/Frontend/CompilerInstance.h"
31
32#include "Varargs.h"
33
34//
35// ROOT::Internal::RStl is the rootcint STL handling class.
36//
37
38static int fgCount = 0;
39
41{
42 // Return the singleton ROOT::Internal::RStl.
43
44 static ROOT::Internal::RStl instance;
45 return instance;
46
47}
48
49void ROOT::Internal::RStl::GenerateTClassFor(const clang::QualType &type, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
50{
51 // Force the generation of the TClass for the given class.
52
53 clang::QualType thisType = type;
54
55 auto typePtr = thisType.getTypePtr();
56 const clang::CXXRecordDecl *stlclass = typePtr->getAsCXXRecordDecl();
57 if (!stlclass) {
58 return;
59 }
60
61 // Transform the type to the corresponding one for IO
62 auto typeForIO = ROOT::TMetaUtils::GetTypeForIO(thisType, interp, normCtxt);
63 if (typeForIO.getTypePtr() != typePtr)
64 stlclass = typeForIO->getAsCXXRecordDecl();
65 if (!stlclass) {
66 return;
67 }
68 thisType = typeForIO;
69
70 const clang::ClassTemplateSpecializationDecl *templateCl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(stlclass);
71
72 if (!templateCl) {
73 ROOT::TMetaUtils::Error("RStl::GenerateTClassFor","%s not in a template",
74 ROOT::TMetaUtils::GetQualifiedName(*stlclass).c_str());
75 }
76
77 if ( TClassEdit::STLKind( stlclass->getName().str() ) == ROOT::kSTLvector ) {
78 const clang::TemplateArgument &arg( templateCl->getTemplateArgs().get(0) );
79 if (arg.getKind() == clang::TemplateArgument::Type) {
80 const clang::NamedDecl *decl = arg.getAsType().getTypePtr()->getAsCXXRecordDecl();
81 if (decl) {
82 // NOTE: we should just compare the decl to the bool builtin!
83 llvm::StringRef argname = decl->getName();
84 if ( (argname.str() == "bool") || (argname.str() == "Bool_t") ) {
85 ROOT::TMetaUtils::Warning("std::vector<bool>", " is not fully supported yet!\nUse std::vector<char> or std::deque<bool> instead.\n");
86 }
87 }
88 }
89 }
90
92 thisType.getTypePtr(),
93 stlclass,
94 "",
95 false /* for backward compatibility rather than 'true' .. neither really make a difference */,
96 false,
97 false,
98 false,
99 -1,
100 interp,
101 normCtxt) );
102
103 // fprintf(stderr,"Registered the STL class %s as needing a dictionary\n",R__GetQualifiedName(*stlclass).c_str());
104
105 for(unsigned int i=0; i < templateCl->getTemplateArgs().size(); ++i) {
106 const clang::TemplateArgument &arg( templateCl->getTemplateArgs().get(i) );
107 if (arg.getKind() == clang::TemplateArgument::Type) {
108 const clang::NamedDecl *decl = arg.getAsType().getTypePtr()->getAsCXXRecordDecl();
109
110 if (decl && TClassEdit::STLKind( decl->getName().str() ) != 0 )
111 {
112 const clang::CXXRecordDecl *clxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
113 if (clxx) {
114 if (!clxx->isCompleteDefinition()) {
115 /* bool result = */ ROOT::TMetaUtils::RequireCompleteType(interp, clxx->getLocation(), arg.getAsType());
116 }
117 // Do we need to strip the qualifier?
118 GenerateTClassFor(arg.getAsType(),interp,normCtxt);
119 }
120 }
121 }
122 }
123}
124
125void ROOT::Internal::RStl::GenerateTClassFor(const char *requestedName, const clang::CXXRecordDecl *stlclass, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
126{
127 // Force the generation of the TClass for the given class.
128 const clang::ClassTemplateSpecializationDecl *templateCl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(stlclass);
129
130 if (templateCl == 0) {
131 ROOT::TMetaUtils::Error("RStl::GenerateTClassFor","%s not in a template",
132 ROOT::TMetaUtils::GetQualifiedName(*stlclass).c_str());
133 }
134
135
136 if ( TClassEdit::STLKind( stlclass->getName().str() ) == ROOT::kSTLvector ) {
137 const clang::TemplateArgument &arg( templateCl->getTemplateArgs().get(0) );
138 if (arg.getKind() == clang::TemplateArgument::Type) {
139 const clang::NamedDecl *decl = arg.getAsType().getTypePtr()->getAsCXXRecordDecl();
140 if (decl) {
141 // NOTE: we should just compare the decl to the bool builtin!
142 llvm::StringRef argname = decl->getName();
143 if ( (argname.str() == "bool") || (argname.str() == "Bool_t") ) {
144 ROOT::TMetaUtils::Warning("std::vector<bool>", " is not fully supported yet!\nUse std::vector<char> or std::deque<bool> instead.\n");
145 }
146 }
147 }
148 }
149
150 fList.insert( ROOT::TMetaUtils::AnnotatedRecordDecl(++fgCount,stlclass,requestedName,true,false,false,false,-1, interp,normCtxt) );
151
153 for(unsigned int i=0; i < templateCl->getTemplateArgs().size(); ++i) {
154 const clang::TemplateArgument &arg( templateCl->getTemplateArgs().get(i) );
155 if (arg.getKind() == clang::TemplateArgument::Type) {
156 const clang::NamedDecl *decl = arg.getAsType().getTypePtr()->getAsCXXRecordDecl();
157
158 if (decl && TClassEdit::STLKind( decl->getName().str() ) != 0 )
159 {
160 const clang::CXXRecordDecl *clxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
161 if (clxx) {
162 if (!clxx->isCompleteDefinition()) {
163 /* bool result = */ ROOT::TMetaUtils::RequireCompleteType(interp, clxx->getLocation (), arg.getAsType());
164 clxx = arg.getAsType().getTypePtr()->getAsCXXRecordDecl();
165 }
166 if (!splitType.fElements.empty()) {
167 GenerateTClassFor( splitType.fElements[i+1].c_str(), clxx, interp, normCtxt);
168 } else {
169 GenerateTClassFor( "", clxx, interp, normCtxt );
170 }
171 }
172 }
173 }
174 }
175
176}
177
179{
180 // Print the content of the object
181 fprintf(stderr,"ROOT::Internal::RStl singleton\n");
182 list_t::iterator iter;
183 for(iter = fList.begin(); iter != fList.end(); ++iter) {
184 fprintf(stderr, "need TClass for %s\n", ROOT::TMetaUtils::GetQualifiedName(*(*iter)).c_str());
185 }
186}
187
189 const cling::Interpreter &interp,
190 const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt,
192 bool &needCollectionProxy,
193 void (*emitStreamerInfo)(const char*) )
194{
195 // This function writes the TGeneraticClassInfo initialiser
196 // and the auxiliary functions (new and delete wrappers) for
197 // each of the STL containers that have been registered
198
199 list_t::iterator iter;
200 for(iter = fList.begin(); iter != fList.end(); ++iter) {
201 const clang::CXXRecordDecl* result;
202
203 if (!iter->GetRecordDecl()->getDefinition()) {
204
205 // We do not have a complete definition, we need to force the instantiation
206 // and findScope can do that.
207 const cling::LookupHelper& lh = interp.getLookupHelper();
208 result = llvm::dyn_cast_or_null<clang::CXXRecordDecl>(lh.findScope(iter->GetNormalizedName(),
209 cling::LookupHelper::NoDiagnostics,
210 0)
211 );
212
213 if (!result || !iter->GetRecordDecl()->getDefinition()) {
214 fprintf(stderr,"Error: incomplete definition for %s\n",iter->GetNormalizedName());
215 continue;
216 }
217 }
218 else
219 {
220 result = llvm::dyn_cast<clang::CXXRecordDecl>(iter->GetRecordDecl());
221 }
222
223 ROOT::TMetaUtils::WriteClassInit(ostr, *iter, result, interp, normCtxt, ctorTypes, needCollectionProxy);
224 ROOT::TMetaUtils::WriteAuxFunctions(ostr, *iter, result, interp, ctorTypes, normCtxt);
225
226 if (emitStreamerInfo) emitStreamerInfo(iter->GetNormalizedName());
227 }
228}
static int fgCount
Definition RStl.cxx:38
int type
Definition TGX11.cxx:121
void GenerateTClassFor(const char *requestedName, const clang::CXXRecordDecl *stlClass, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt)
Definition RStl.cxx:125
static RStl & Instance()
Definition RStl.cxx:40
void WriteClassInit(std::ostream &strm, const cling::Interpreter &interp, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt, const ROOT::TMetaUtils::RConstructorTypes &, bool &needCollectionProxy, void(*emitStreamerInfo)(const char *))
Definition RStl.cxx:188
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 Error(const char *location, const char *va_(fmt),...)
clang::QualType GetTypeForIO(const clang::QualType &templateInstanceType, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt, TClassEdit::EModType mode=TClassEdit::kNone)
std::list< RConstructorType > RConstructorTypes
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 GetQualifiedName(std::string &qual_name, const clang::QualType &type, const clang::NamedDecl &forcontext)
Main implementation relying on GetFullyQualifiedTypeName All other GetQualifiedName functions leverag...
bool RequireCompleteType(const cling::Interpreter &interp, const clang::CXXRecordDecl *cl)
void Warning(const char *location, const char *va_(fmt),...)
@ kSTLvector
Definition ESTLType.h:30
ROOT::ESTLType STLKind(std::string_view type)
Converts STL container name to number.
std::vector< std::string > fElements
Definition TClassEdit.h:140