File tree Expand file tree Collapse file tree 4 files changed +67
-3
lines changed Expand file tree Collapse file tree 4 files changed +67
-3
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ add_clang_executable(scalaBindgen
12
12
TypeTranslator.cpp
13
13
HeaderManager.h
14
14
HeaderManager.cpp
15
+ CycleDetection.h
15
16
Utils.h
16
17
catch/catch.hpp
17
18
SimpleTypeTests.cpp
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include " TypeTranslator.h"
4
+
5
+ #include < string>
6
+ #include < map>
7
+ #include < set>
8
+
9
+ class CycleDetection {
10
+ private:
11
+ TypeTranslator& tpeTransl;
12
+
13
+ bool contains (std::string& k){
14
+ return !!dependencies.count (k);
15
+ }
16
+
17
+ public:
18
+ std::map<std::string, std::set<std::string> > dependencies;
19
+ CycleDetection (TypeTranslator& tpeTransl_) : tpeTransl(tpeTransl_), dependencies{} {}
20
+
21
+
22
+ void AddDependcy (std::string name, const clang::QualType& qtpe){
23
+
24
+ // TODO: function pointer
25
+
26
+ if (qtpe->isPointerType ()){
27
+ const clang::PointerType* ptr = qtpe.getTypePtr ()->getAs <clang::PointerType>();
28
+ AddDependcy (name, ptr->getPointeeType ());
29
+ return ;
30
+ }
31
+
32
+ std::string qtpeString = tpeTransl.Translate (qtpe);
33
+
34
+ // Add the dependence of qtpe
35
+ if (contains (qtpeString)){
36
+ dependencies[name].insert (dependencies[qtpeString].begin (), dependencies[qtpeString].end ());
37
+ }
38
+
39
+ dependencies[name].insert (qtpeString);
40
+ }
41
+
42
+
43
+ bool isCyclic (std::string& name){
44
+ if (contains (name)){
45
+ if (dependencies[name].count (name) != 0 ){
46
+ return true ;
47
+ }
48
+ }
49
+ return false ;
50
+ }
51
+ };
Original file line number Diff line number Diff line change @@ -128,12 +128,15 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
128
128
for (const clang::FieldDecl* field : record->fields ()){
129
129
std::string fname = handleReservedWords (field->getNameAsString ());
130
130
std::string ftype = typeTranslator.Translate (field->getType (), &name);
131
-
132
131
fields += ftype + " , " ;
133
- if (name != " " ){
132
+
133
+ if (fname != " " ){
134
134
helpersFunc += " \t\t def " + fname + " : " + ftype + " = !p._" + std::to_string (fieldCnt + 1 ) + " \n " ;
135
135
helpersFunc += " \t\t def " + fname + " _=(value: " + ftype + " ):Unit = !p._" + std::to_string (fieldCnt + 1 ) + " = value\n " ;
136
136
}
137
+
138
+ cycleDetection.AddDependcy (newName, field->getType ());
139
+
137
140
fieldCnt++;
138
141
}
139
142
@@ -142,6 +145,12 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
142
145
fields = fields.substr (0 , fields.size ()-2 );
143
146
}
144
147
148
+ // llvm::errs() << newName << "\n";
149
+ // for(auto& s : cycleDetection.dependencies[newName]){
150
+ // llvm::errs() << "\t" << s << "\n";
151
+ // }
152
+ // llvm::errs() << cycleDetection.isCyclic(newName) << "\n";
153
+
145
154
if (fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
146
155
declarations += " \t type " + newName + " = " + " native.CStruct" + std::to_string (fieldCnt) + " [" + fields + " ]\n " ;
147
156
} else {
Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
3
#include " HeaderManager.h"
4
+ #include " CycleDetection.h"
4
5
#include " TypeTranslator.h"
5
6
6
7
#include " clang/Driver/Options.h"
@@ -23,9 +24,11 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
23
24
private:
24
25
clang::ASTContext* astContext;
25
26
TypeTranslator typeTranslator;
27
+ CycleDetection cycleDetection;
28
+
26
29
27
30
public:
28
- explicit TreeVisitor (clang::CompilerInstance *CI) : astContext(&(CI->getASTContext ())), typeTranslator(astContext) {}
31
+ explicit TreeVisitor (clang::CompilerInstance *CI) : astContext(&(CI->getASTContext ())), typeTranslator(astContext), cycleDetection(typeTranslator) {}
29
32
30
33
virtual bool VisitFunctionDecl (clang::FunctionDecl *func);
31
34
virtual bool VisitTypedefDecl (clang::TypedefDecl *tpdef);
You can’t perform that action at this time.
0 commit comments