Logo ROOT   6.08/07
Reference Guide
Scanner.h
Go to the documentation of this file.
1 // @(#)root/utils/src:$Id$
2 // Author: Philippe Canal November 2011 ;
3 // 16/04/2010 and Velislava Spasova.
4 // originated from Zdenek Culik
5 
6 
7 /*************************************************************************
8  * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/rootcint. *
13  *************************************************************************/
14 
15 #ifndef ROOT__RSCANNER_H__
16 #define ROOT__RSCANNER_H__
17 
18 #include <stack>
19 
20 #include "clang/AST/AST.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclGroup.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/Type.h"
25 #include "clang/AST/RecursiveASTVisitor.h"
26 
27 #include "llvm/IR/Module.h"
28 
29 #include "TMetaUtils.h"
30 
31 namespace clang {
32  class ClassTemplatePartialSpecializationDecl;
33  class ClassTemplateDecl;
34  class RecordDecl;
35  class Stmt;
36 }
37 
38 namespace cling {
39  class Interpreter;
40 }
41 
42 class SelectionRules;
43 class BaseSelectionRule;
44 class ClassSelectionRule;
45 
46 class RScanner: public clang::RecursiveASTVisitor<RScanner>
47 {
48 
49 public:
51  public:
52  AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass) :
53  fDecl(decl), fRuleIndex(index), fRequestOnlyTClass(rRequestOnlyTClass){}
54  AnnotatedNamespaceDecl() { /* Nothing to do we do not own the pointer; */}
55  bool RequestOnlyTClass() const { return fRequestOnlyTClass; }
56  const clang::NamespaceDecl* GetNamespaceDecl() const { return fDecl; }
57  operator clang::NamespaceDecl const *() const { return fDecl; }
58  bool operator<(const AnnotatedNamespaceDecl& right) { return fRuleIndex < right.fRuleIndex; }
59  private:
60  const clang::NamespaceDecl *fDecl;
61  long fRuleIndex;
63  };
64 
65  typedef std::vector<AnnotatedNamespaceDecl> NamespaceColl_t;
66  typedef std::vector<ROOT::TMetaUtils::AnnotatedRecordDecl> ClassColl_t;
67  typedef std::vector<clang::TypedefNameDecl*> TypedefColl_t;
68  typedef std::vector<clang::FunctionDecl*> FunctionColl_t;
69  typedef std::vector<clang::VarDecl*> VariableColl_t;
70  typedef std::vector<clang::EnumDecl*> EnumColl_t;
71  typedef void (*DeclCallback)(const char *type);
72  typedef std::map<clang::Decl*,const BaseSelectionRule*> DeclsSelRulesMap_t;
73 
74  enum class EScanType : char {kNormal, kTwoPasses, kOnePCM};
75 
76  RScanner (SelectionRules &rules,
77  EScanType stype,
78  const cling::Interpreter &interpret,
80  unsigned int verbose = 0);
81 
82  // Configure the vistitor to also visit template instantiation.
83  bool shouldVisitTemplateInstantiations() const { return true; }
84 
85  // Don't descend into function bodies.
86  bool TraverseStmt(clang::Stmt*) { return true; }
87 
88  // Don't descend into templates partial specialization (but only instances thereof).
89  bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl*) { return true; }
90 
91  bool VisitEnumDecl(clang::EnumDecl* D); //Visitor for every EnumDecl i.e. enumeration node in the AST
92  bool VisitFieldDecl(clang::FieldDecl* D); //Visitor for e field inside a class
93  bool VisitFunctionDecl(clang::FunctionDecl* D); //Visitor for every FunctionDecl i.e. function node in the AST
94  bool VisitNamespaceDecl(clang::NamespaceDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
95  bool VisitRecordDecl(clang::RecordDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
96  bool VisitTypedefNameDecl(clang::TypedefNameDecl* D); // Visitor for every TypedefNameDecl i.e. class node in the AST
97  bool VisitVarDecl(clang::VarDecl* D); //Visitor for every VarDecl i.e. variable node in the AST
98 
99  bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl* typeDecl); //Function called by VisitTypedefNameDecl and VisitRecordDecl
100 
101  bool TraverseDeclContextHelper(clang::DeclContext *DC); // Here is the code magic :) - every Decl
102  // according to its type is processed by the corresponding Visitor method
103 
104  // Set a callback to record which are declared.
105  DeclCallback SetRecordDeclCallback(DeclCallback callback);
106 
107  // Main interface of this class.
108  void Scan(const clang::ASTContext &C);
109 
110  // Utility routines. Most belongs in TMetaUtils and should be shared with rootcling.cxx
111  bool GetDeclName(clang::Decl* D, std::string& name) const;
112  bool GetDeclQualName(const clang::Decl* D, std::string& qual_name) const;
113  bool GetFunctionPrototype(clang::Decl* D, std::string& prototype) const;
114 
115  static const char* fgClangDeclKey; // property key used for CLang declaration objects
116  static const char* fgClangFuncKey; // property key for function (demangled) names
117 
118  const DeclsSelRulesMap_t& GetDeclsSelRulesMap() const {return fDeclSelRuleMap;};
119 
120  // public for now, the list of selected classes.
121  ClassColl_t fSelectedClasses;
122  NamespaceColl_t fSelectedNamespaces;
123  TypedefColl_t fSelectedTypedefs;
124  FunctionColl_t fSelectedFunctions;
125  VariableColl_t fSelectedVariables;
126  EnumColl_t fSelectedEnums;
127 
128  virtual ~ RScanner ();
129 
130 
131 
132 private:
133 
134  void AddAnnotatedRecordDecl(const ClassSelectionRule*,
135  const clang::Type*,
136  const clang::RecordDecl*,
137  const std::string&,
138  const clang::TypedefNameDecl*,
139  unsigned int indexOffset=0);
140  std::string ConvTemplateArguments(const clang::TemplateArgumentList& list) const;
141  std::string ConvTemplateName(clang::TemplateName& N) const;
142  std::string ConvTemplateParameterList(clang::TemplateParameterList* list) const;
143  std::string ConvTemplateParams(clang::TemplateDecl* D) const;
144  void DeclInfo(clang::Decl* D) const;
145  std::string ExprToStr(clang::Expr* expr) const;
146  std::string FuncParameterList(clang::FunctionDecl* D) const;
147  std::string FuncParameters(clang::FunctionDecl* D) const;
148  std::string GetEnumName(clang::EnumDecl* D) const;
149  std::string GetLocation(clang::Decl* D) const;
150  std::string GetName(clang::Decl* D) const;
151  std::string GetSrcLocation(clang::SourceLocation L) const;
152  unsigned int FuncModifiers(clang::FunctionDecl* D) const;
153  unsigned int fVerboseLevel;
154  unsigned int VarModifiers(clang::VarDecl* D) const;
155  unsigned int Visibility(clang::Decl* D) const;
156  unsigned int VisibilityModifiers(clang::AccessSpecifier access) const;
157  void ShowError(const std::string &msg, const std::string &location = "") const;
158  void ShowInfo(const std::string &msg, const std::string &location = "") const;
159  void ShowTemplateInfo(const std::string &msg, const std::string &location = "") const;
160  void ShowWarning(const std::string &msg, const std::string &location = "") const;
161  static std::map <clang::Decl*, std::string> fgAnonymousClassMap;
162  static std::map <clang::Decl*, std::string> fgAnonymousEnumMap;
163  void UnexpectedDecl(clang::Decl* D,const std::string &txt = "") const;
164  void UnimplementedDecl(clang::Decl* D,const std::string &txt = "");
165  void UnimportantDecl(clang::Decl* D,const std::string &txt = "") const;
166  void UnknownDecl(clang::Decl* D, const std::string &txt = "") const;
167  void UnknownType(clang::QualType qual_type) const;
168  void UnsupportedDecl(clang::Decl* D,const std::string &txt = "") const;
169  void UnsupportedType(clang::QualType qual_type) const;
170 
171  const clang::SourceManager* fSourceManager;
172  const cling::Interpreter &fInterpreter;
173  static const int fgDeclLast = clang::Decl::Var;
174  static const int fgTypeLast = clang::Type::TemplateTypeParm;
175  bool fDeclTable [ fgDeclLast+1 ];
176  clang::Decl * fLastDecl;
177  DeclCallback fRecordDeclCallback;
178  bool fTypeTable [ fgTypeLast+1 ];
181  static int fgBadClassCounter;
184  std::set<clang::RecordDecl*> fselectedRecordDecls; // Set for O(logN)
185  EScanType fScanType; // Differentiate among different kind of scans
186  bool fFirstPass; // This flag allows to run twice, for example in presence of dict selection and recursive template list manipulations.
187  DeclsSelRulesMap_t fDeclSelRuleMap; // Map decls to selection rules which selected them
188 
189 };
190 
191 #endif /* ROOT__RSCANNER_H__ */
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:140
NamespaceColl_t fSelectedNamespaces
Definition: Scanner.h:122
bool TraverseStmt(clang::Stmt *)
Definition: Scanner.h:86
RooArgList L(const RooAbsArg &v1)
static std::map< clang::Decl *, std::string > fgAnonymousEnumMap
Definition: Scanner.h:162
#define N
const clang::NamespaceDecl * fDecl
Definition: Scanner.h:60
static std::map< clang::Decl *, std::string > fgAnonymousClassMap
Definition: Scanner.h:161
clang::Decl * fLastDecl
Definition: Scanner.h:176
static int fgAnonymousEnumCounter
Definition: Scanner.h:180
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
Definition: Scanner.h:182
unsigned int fVerboseLevel
Definition: Scanner.h:153
SelectionRules & fSelectionRules
Definition: Scanner.h:183
static const char * fgClangDeclKey
Definition: Scanner.h:115
VariableColl_t fSelectedVariables
Definition: Scanner.h:125
static int fgAnonymousClassCounter
Definition: Scanner.h:179
EScanType fScanType
Definition: Scanner.h:185
FunctionColl_t fSelectedFunctions
Definition: Scanner.h:124
bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl *)
Definition: Scanner.h:89
The class representing the collection of selection rules.
std::vector< clang::FunctionDecl * > FunctionColl_t
Definition: Scanner.h:68
RooCmdArg ShowError(Bool_t flag)
const cling::Interpreter & fInterpreter
Definition: Scanner.h:172
static double C[]
std::vector< clang::VarDecl * > VariableColl_t
Definition: Scanner.h:69
bool verbose
bool operator<(const AnnotatedNamespaceDecl &right)
Definition: Scanner.h:58
const clang::NamespaceDecl * GetNamespaceDecl() const
Definition: Scanner.h:56
std::vector< clang::EnumDecl * > EnumColl_t
Definition: Scanner.h:70
EScanType
Definition: Scanner.h:74
DeclsSelRulesMap_t fDeclSelRuleMap
Definition: Scanner.h:187
Type
enumeration specifying the integration types.
bool shouldVisitTemplateInstantiations() const
Definition: Scanner.h:83
bool fFirstPass
Definition: Scanner.h:186
const clang::SourceManager * fSourceManager
Definition: Scanner.h:171
Definition: TCling.h:48
std::map< clang::Decl *, const BaseSelectionRule * > DeclsSelRulesMap_t
Definition: Scanner.h:72
int type
Definition: TGX11.cxx:120
Print a TSeq at the prompt:
Definition: TDatime.h:114
std::set< clang::RecordDecl * > fselectedRecordDecls
Definition: Scanner.h:184
EnumColl_t fSelectedEnums
Definition: Scanner.h:126
bool RequestOnlyTClass() const
Definition: Scanner.h:55
AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass)
Definition: Scanner.h:52
typedef void((*Func_t)())
std::vector< ROOT::TMetaUtils::AnnotatedRecordDecl > ClassColl_t
Definition: Scanner.h:66
std::vector< AnnotatedNamespaceDecl > NamespaceColl_t
Definition: Scanner.h:65
static const char * fgClangFuncKey
Definition: Scanner.h:116
const DeclsSelRulesMap_t & GetDeclsSelRulesMap() const
Definition: Scanner.h:118
DeclCallback fRecordDeclCallback
Definition: Scanner.h:177
static int fgBadClassCounter
Definition: Scanner.h:181
std::vector< clang::TypedefNameDecl * > TypedefColl_t
Definition: Scanner.h:67
char name[80]
Definition: TGX11.cxx:109
TypedefColl_t fSelectedTypedefs
Definition: Scanner.h:123