-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitial_pass.h
More file actions
169 lines (132 loc) · 6.94 KB
/
initial_pass.h
File metadata and controls
169 lines (132 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef KSLICER_INITIAL_PASS_H
#define KSLICER_INITIAL_PASS_H
#include "kslicer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Parse/ParseAST.h"
#include <unordered_set>
namespace kslicer
{
std::string ClearTypeName(const std::string& a_typeName);
struct MainFuncNodeInfo
{
std::string funcName;
std::string className;
const clang::CXXMethodDecl* astNode = nullptr;
};
struct ClassInfo
{
ClassInfo(){}
ClassInfo(const std::string& a_name) : name(a_name) {}
std::string name;
const clang::CXXRecordDecl* astNode = nullptr;
std::unordered_map<std::string, KernelInfo> funKernels;
std::unordered_map<std::string, const clang::CXXMethodDecl*> funMembers;
std::unordered_map<std::string, MainFuncNodeInfo> funControls;
std::unordered_map<std::string, DataMemberInfo> dataMembers;
std::unordered_map<std::string, const clang::CXXMethodDecl*> m_setters;
std::vector<const clang::CXXConstructorDecl* > ctors;
int baseClassOrder = 0;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::string PerformClassComposition(ClassInfo& mainClassInfo, const ClassInfo& apiClassInfo, const ClassInfo& implClassInfo);
void PerformInheritanceMerge(kslicer::ClassInfo& mainClassInfo, const kslicer::ClassInfo& baseClassInfo);
using namespace clang;
// RecursiveASTVisitor is the big-kahuna visitor that traverses everything in the AST.
//
class InitialPassRecursiveASTVisitor : public RecursiveASTVisitor<InitialPassRecursiveASTVisitor>
{
public:
std::string MAIN_CLASS_NAME;
std::string MAIN_FILE_INCLUDE;
InitialPassRecursiveASTVisitor(std::vector<std::string>& a_mainFunctionNames,
std::string main_class,
std::vector<std::string> compos_classes,
CompilerInstance& a_compiler, MainClassInfo& a_codeInfo) :
MAIN_CLASS_NAME(main_class), m_compiler(a_compiler), m_astContext(a_compiler.getASTContext()), m_sourceManager(a_compiler.getSourceManager()), m_codeInfo(a_codeInfo)
{
m_mainFuncts.reserve(a_mainFunctionNames.size());
for(const auto& name : a_mainFunctionNames)
m_mainFuncts.insert(name);
for(const auto& name : compos_classes)
m_composedClassInfo[name] = ClassInfo(name);
mci.name = MAIN_CLASS_NAME;
}
bool VisitCXXMethodDecl(CXXMethodDecl* f);
bool VisitFieldDecl (FieldDecl* var);
bool VisitCXXRecordDecl(CXXRecordDecl* record);
bool VisitTypeDecl (TypeDecl* record);
bool VisitVarDecl (VarDecl* pTargetVar);
ClassInfo mci; // main class info
std::unordered_map<std::string, ClassInfo> m_composedClassInfo;
std::unordered_map<std::string, ClassInfo> m_baseClassInfo;
std::vector<const clang::CXXRecordDecl*> m_classList;
std::vector<kslicer::DeclInClass> GetExtractedDecls();
const std::unordered_map<std::string, kslicer::DeclInClass>& GetOtherTypeDecls() const { return m_storedDecl;}
private:
bool ProcessKernelDef(const CXXMethodDecl *f, std::unordered_map<std::string, KernelInfo>& a_funcList, const std::string& a_className);
CompilerInstance& m_compiler;
const ASTContext& m_astContext;
clang::SourceManager& m_sourceManager;
std::unordered_set<std::string> m_mainFuncts;
MainClassInfo& m_codeInfo;
uint32_t m_currId = 0;
std::unordered_map<std::string, kslicer::DeclInClass> m_transferredDecl;
std::unordered_map<std::string, kslicer::DeclInClass> m_storedDecl;
};
class InitialPassASTConsumer : public ASTConsumer
{
public:
InitialPassASTConsumer (std::vector<std::string>& a_mainFunctionNames,
std::string main_class,
std::vector<std::string> compos_classes,
CompilerInstance& a_compiler, MainClassInfo& a_codeInfo) :
rv(a_mainFunctionNames, main_class, compos_classes, a_compiler, a_codeInfo)
//rv0(main_class, a_compiler, a_codeInfo, rv.m_composedClassInfo)
{ }
bool HandleTopLevelDecl(DeclGroupRef d) override;
InitialPassRecursiveASTVisitor rv;
//ZeroPassRecursiveASTVisitor rv0;
};
std::string ClearTypeName(const std::string& a_typeName);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class HeaderLister : public clang::PPCallbacks
{
public:
HeaderLister(kslicer::MainClassInfo* a_pInfo) : m_pGlobInfo(a_pInfo) {}
/*void InclusionDirective(clang::SourceLocation HashLoc,
const clang::Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
clang::CharSourceRange FilenameRange,
const clang::FileEntry *File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath,
const clang::Module *Imported,
clang::SrcMgr::CharacteristicKind FileType) override */
void InclusionDirective (clang::SourceLocation HashLoc,
const clang::Token &IncludeTok,
clang::StringRef FileName,
bool IsAngled,
clang::CharSourceRange FilenameRange,
clang::OptionalFileEntryRef File,
clang::StringRef SearchPath,
clang::StringRef RelativePath,
const clang::Module *Imported,
clang::SrcMgr::CharacteristicKind FileType)
{
if(!IsAngled && File != nullptr)
{
assert(File != nullptr);
std::string filename = std::string(RelativePath.begin(), RelativePath.end());
m_pGlobInfo->allIncludeFiles[filename] = false;
}
}
private:
kslicer::MainClassInfo* m_pGlobInfo;
};
#endif