Skip to content

Commit d097887

Browse files
committed
Merge branch 'master' of github.com:mrRosset/scala-native-bindgen into merge-upstream
2 parents ec32313 + 8359653 commit d097887

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

Main.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#define CATCH_CONFIG_RUNNER
55
#include "catch/catch.hpp"
66

7-
#include <sys/types.h>
8-
97
static llvm::cl::OptionCategory Category("Binding Generator");
108
static llvm::cl::extrahelp CommonHelp(clang::tooling::CommonOptionsParser::HelpMessage);
119
static llvm::cl::extrahelp MoreHelp("\nProduce Bindings for scala native. Please specify lib name wit parameter name\n");
@@ -54,15 +52,19 @@ int main(int argc, char *argv[]) {
5452
} else {
5553
if(declarations != "" || enums != "")
5654
llvm::outs() << "import scala.scalanative._\n"
55+
<< "import scala.scalanative.native._\n"
5756
<< "import scala.scalanative.native.Nat._\n\n";
5857

5958
if(declarations != ""){
6059
llvm::outs() << "@native.link(\"" << lib << "\")\n"
6160
<< "@native.extern\n"
6261
<< "object " << lib << " {\n"
6362
<< declarations
64-
<< "}\n\n"
65-
<< "import " + lib + "._\n\n";
63+
<< "}\n\n";
64+
}
65+
66+
if(enums != "" || helpers != ""){
67+
llvm::outs() << "import " + lib + "._\n\n";
6668
}
6769

6870
if(enums != ""){

SimpleTypeTests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ TEST_CASE("struct pointer", "[Type]"){
7171
REQUIRE(answ == Translate(code));
7272
}
7373

74+
TEST_CASE("struct helper methods") {
75+
std::string code = "struct listing { int a; int b; };";
76+
Translate(code);
77+
std::string answ = "\timplicit class struct_listing_ops(val p: native.Ptr[struct_listing]) extends AnyVal {\n"
78+
"\t\tdef a: native.CInt = !p._1\n"
79+
"\t\tdef a_=(value: native.CInt):Unit = !p._1 = value\n"
80+
"\t\tdef b: native.CInt = !p._2\n"
81+
"\t\tdef b_=(value: native.CInt):Unit = !p._2 = value\n"
82+
"\t}\n\n"
83+
"\tdef struct_listing()(implicit z: native.Zone): native.Ptr[struct_listing] = native.alloc[struct_listing]\n\n";
84+
REQUIRE(answ == helpers);
85+
}
86+
7487
TEST_CASE("func no args", "[Func]"){
7588
std::string code = "int foo();";
7689
std::string answ = "\tdef foo(): native.CInt = native.extern\n";

TreeVisitor.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ bool TreeVisitor::VisitTypedefDecl(clang::TypedefDecl *tpdef){
4747
std::string name = tpdef->getName();
4848
std::string tpe = typeTranslator.Translate(tpdef->getUnderlyingType());
4949
declarations += "\ttype " + name + " = " + tpe + "\n";
50+
51+
cycleDetection.AddDependcy(name, tpdef->getUnderlyingType());
52+
if(cycleDetection.isCyclic(name)){
53+
llvm::errs() << "Error: " << name << " ic cyclic\n";
54+
llvm::errs() << name << "\n";
55+
for(auto& s : cycleDetection.dependencies[name]){
56+
llvm::errs() << "\t" << s << "\n";
57+
}
58+
llvm::errs() << cycleDetection.isCyclic(name) << "\n";
59+
}
60+
5061
return true;
5162
}
5263

@@ -112,7 +123,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
112123
helpers += helpersFunc;
113124
helpers += "\t}\n\n";
114125

115-
return true;
126+
return true;
116127

117128
} else if (record->isStruct() && record->isThisDeclarationADefinition() && !record->isAnonymousStructOrUnion() && name != ""){
118129

@@ -145,11 +156,16 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
145156
fields = fields.substr(0, fields.size()-2);
146157
}
147158

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";
159+
160+
161+
if(cycleDetection.isCyclic(newName)){
162+
llvm::errs() << "Error: " << newName << " ic cyclic\n";
163+
llvm::errs() << newName << "\n";
164+
for(auto& s : cycleDetection.dependencies[newName]){
165+
llvm::errs() << "\t" << s << "\n";
166+
}
167+
llvm::errs() << cycleDetection.isCyclic(newName) << "\n";
168+
}
153169

154170
if(fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
155171
declarations += "\ttype " + newName + " = " + "native.CStruct" + std::to_string(fieldCnt) + "[" + fields + "]\n";
@@ -165,6 +181,8 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
165181
helpers += "\timplicit class " + newName + "_ops(val p: native.Ptr[struct_" + name + "]) extends AnyVal {\n";
166182
helpers += helpersFunc;
167183
helpers += "\t}\n\n";
184+
185+
helpers += "\tdef " + newName + "()(implicit z: native.Zone): native.Ptr[" + newName + "] = native.alloc[" + newName + "]\n\n";
168186
}
169187

170188
return true;

TypeTranslator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ std::string TypeTranslator::TranslateFunctionPointer(const clang::QualType& qtpe
6262
}
6363
}
6464

65-
std::string TypeTranslator::TranslatePointer(const clang::PointerType* ptr, const std::string* avoid){
66-
const clang::QualType& pte = ptr->getPointeeType();
65+
std::string TypeTranslator::TranslatePointer(const clang::QualType& pte, const std::string* avoid){
6766

6867
if(pte->isBuiltinType()){
6968
const clang::BuiltinType* as = pte->getAs<clang::BuiltinType>();
@@ -147,7 +146,7 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
147146
return TranslateFunctionPointer(qtpe, avoid);
148147

149148
} else if(tpe->isPointerType()){
150-
return TranslatePointer(tpe->getAs<clang::PointerType>(), avoid);
149+
return TranslatePointer(tpe->getAs<clang::PointerType>()->getPointeeType(), avoid);
151150

152151
} else if(qtpe->isStructureType() || qtpe->isUnionType()){
153152
return TranslateStructOrUnion(qtpe);
@@ -157,7 +156,8 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
157156

158157
} else if(qtpe->isConstantArrayType()){
159158
return TranslateConstantArray(ctx->getAsConstantArrayType(qtpe), avoid);
160-
159+
} else if(qtpe->isArrayType()){
160+
return TranslatePointer(ctx->getAsArrayType(qtpe)->getElementType(), avoid);
161161
} else {
162162

163163
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());

TypeTranslator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TypeTranslator {
2121
*/
2222
std::string Translate(const clang::QualType& tpe, const std::string* = nullptr);
2323
std::string TranslateFunctionPointer(const clang::QualType& qtpe, const std::string* avoid );
24-
std::string TranslatePointer(const clang::PointerType* ptr, const std::string* avoid );
24+
std::string TranslatePointer(const clang::QualType& pointee, const std::string* avoid);
2525
std::string TranslateStructOrUnion(const clang::QualType& qtpe);
2626
std::string TranslateEnum(const clang::QualType& qtpe);
2727
std::string TranslateConstantArray(const clang::ConstantArrayType* ar, const std::string* avoid );

0 commit comments

Comments
 (0)