1
1
#include " TreeVisitor.h"
2
- #include " clang/AST/RecordLayout.h"
3
2
4
3
bool TreeVisitor::VisitFunctionDecl (clang::FunctionDecl *func) {
5
4
if (!astContext->getSourceManager ().isInMainFile (func->getLocation ())) {
@@ -37,7 +36,7 @@ bool TreeVisitor::VisitTypedefDecl(clang::TypedefDecl *tpdef) {
37
36
std::shared_ptr<Type> type =
38
37
typeTranslator.translate (tpdef->getUnderlyingType ());
39
38
if (type) {
40
- ir.addTypeDef (name, type, getLocation (tpdef));
39
+ ir.addTypeDef (name, type, typeTranslator. getLocation (tpdef));
41
40
}
42
41
return true ;
43
42
}
@@ -59,7 +58,8 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {
59
58
std::string scalaType = typeTranslator.getTypeFromTypeMap (
60
59
enumdecl->getIntegerType ().getUnqualifiedType ().getAsString ());
61
60
62
- ir.addEnum (name, scalaType, std::move (enumerators), getLocation (enumdecl));
61
+ ir.addEnum (name, scalaType, std::move (enumerators),
62
+ typeTranslator.getLocation (enumdecl));
63
63
64
64
return true ;
65
65
}
@@ -76,68 +76,17 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record) {
76
76
77
77
if (record->isUnion () && record->isThisDeclarationADefinition () &&
78
78
!record->isAnonymousStructOrUnion () && !name.empty ()) {
79
- handleUnion (record, name);
79
+ typeTranslator. addUnionDefinition (record, name);
80
80
return true ;
81
81
82
82
} else if (record->isStruct () && record->isThisDeclarationADefinition () &&
83
83
!record->isAnonymousStructOrUnion () && !name.empty ()) {
84
- handleStruct (record, name);
84
+ typeTranslator. addStructDefinition (record, name);
85
85
return true ;
86
86
}
87
87
return false ;
88
88
}
89
89
90
- void TreeVisitor::handleUnion (clang::RecordDecl *record, std::string name) {
91
- std::vector<std::shared_ptr<Field>> fields;
92
-
93
- for (const clang::FieldDecl *field : record->fields ()) {
94
- std::string fname = field->getNameAsString ();
95
- std::shared_ptr<Type> ftype =
96
- typeTranslator.translate (field->getType ());
97
-
98
- fields.push_back (std::make_shared<Field>(fname, ftype));
99
- }
100
-
101
- uint64_t sizeInBits = astContext->getTypeSize (record->getTypeForDecl ());
102
- assert (sizeInBits % 8 == 0 );
103
-
104
- ir.addUnion (name, std::move (fields), sizeInBits / 8 , getLocation (record));
105
- }
106
-
107
- void TreeVisitor::handleStruct (clang::RecordDecl *record, std::string name) {
108
- std::string newName = " struct_" + name;
109
-
110
- if (record->hasAttr <clang::PackedAttr>()) {
111
- llvm::errs () << " Warning: struct " << name << " is packed. "
112
- << " Packed structs are not supported by Scala Native. "
113
- << " Access to fields will not work correctly.\n " ;
114
- llvm::errs ().flush ();
115
- }
116
-
117
- std::vector<std::shared_ptr<Field>> fields;
118
- const clang::ASTRecordLayout &recordLayout =
119
- astContext->getASTRecordLayout (record);
120
-
121
- bool isBitFieldStruct = false ;
122
- for (const clang::FieldDecl *field : record->fields ()) {
123
- if (field->isBitField ()) {
124
- isBitFieldStruct = true ;
125
- }
126
- std::shared_ptr<Type> ftype =
127
- typeTranslator.translate (field->getType ());
128
- uint64_t recordOffsetInBits =
129
- recordLayout.getFieldOffset (field->getFieldIndex ());
130
- fields.push_back (std::make_shared<Field>(field->getNameAsString (),
131
- ftype, recordOffsetInBits));
132
- }
133
-
134
- uint64_t sizeInBits = astContext->getTypeSize (record->getTypeForDecl ());
135
- assert (sizeInBits % 8 == 0 );
136
-
137
- ir.addStruct (name, std::move (fields), sizeInBits / 8 , getLocation (record),
138
- record->hasAttr <clang::PackedAttr>(), isBitFieldStruct);
139
- }
140
-
141
90
bool TreeVisitor::VisitVarDecl (clang::VarDecl *varDecl) {
142
91
if (!astContext->getSourceManager ().isInMainFile (varDecl->getLocation ())) {
143
92
/* include variables only from the original header */
@@ -157,20 +106,3 @@ bool TreeVisitor::VisitVarDecl(clang::VarDecl *varDecl) {
157
106
}
158
107
return true ;
159
108
}
160
-
161
- std::shared_ptr<Location> TreeVisitor::getLocation (clang::Decl *decl) {
162
- clang::SourceManager &sm = astContext->getSourceManager ();
163
- std::string filename = std::string (sm.getFilename (decl->getLocation ()));
164
- char *p = realpath (filename.c_str (), nullptr );
165
- std::string path;
166
- if (p) {
167
- path = p;
168
- delete[] p;
169
- } else {
170
- // TODO: check when it happens
171
- path = " " ;
172
- }
173
-
174
- unsigned lineNumber = sm.getSpellingLineNumber (decl->getLocation ());
175
- return std::make_shared<Location>(path, lineNumber);
176
- }
0 commit comments