Skip to content

Merge latest code from Marius #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ScalaFrontend.h"
#include "Utils.h"

#include <sys/types.h>

static llvm::cl::OptionCategory Category("Binding Generator");
static llvm::cl::extrahelp CommonHelp(clang::tooling::CommonOptionsParser::HelpMessage);
Expand Down Expand Up @@ -43,15 +42,19 @@ int main(int argc, char *argv[]) {
} else {
if(declarations != "" || enums != "")
llvm::outs() << "import scala.scalanative._\n"
<< "import scala.scalanative.native._\n"
<< "import scala.scalanative.native.Nat._\n\n";

if(declarations != ""){
llvm::outs() << "@native.link(\"" << lib << "\")\n"
<< "@native.extern\n"
<< "object " << lib << " {\n"
<< declarations
<< "}\n\n"
<< "import " + lib + "._\n\n";
<< "}\n\n";
}

if(enums != "" || helpers != ""){
llvm::outs() << "import " + lib + "._\n\n";
}

if(enums != ""){
Expand Down
13 changes: 13 additions & 0 deletions SimpleTypeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ TEST_CASE("struct pointer", "[Type]"){
REQUIRE(answ == Translate(code));
}

TEST_CASE("struct helper methods") {
std::string code = "struct listing { int a; int b; };";
Translate(code);
std::string answ = "\timplicit class struct_listing_ops(val p: native.Ptr[struct_listing]) extends AnyVal {\n"
"\t\tdef a: native.CInt = !p._1\n"
"\t\tdef a_=(value: native.CInt):Unit = !p._1 = value\n"
"\t\tdef b: native.CInt = !p._2\n"
"\t\tdef b_=(value: native.CInt):Unit = !p._2 = value\n"
"\t}\n\n"
"\tdef struct_listing()(implicit z: native.Zone): native.Ptr[struct_listing] = native.alloc[struct_listing]\n\n";
REQUIRE(answ == helpers);
}

TEST_CASE("func no args", "[Func]"){
std::string code = "int foo();";
std::string answ = "\tdef foo(): native.CInt = native.extern\n";
Expand Down
30 changes: 24 additions & 6 deletions TreeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ bool TreeVisitor::VisitTypedefDecl(clang::TypedefDecl *tpdef){
std::string name = tpdef->getName();
std::string tpe = typeTranslator.Translate(tpdef->getUnderlyingType());
declarations += "\ttype " + name + " = " + tpe + "\n";

cycleDetection.AddDependcy(name, tpdef->getUnderlyingType());
if(cycleDetection.isCyclic(name)){
llvm::errs() << "Error: " << name << " ic cyclic\n";
llvm::errs() << name << "\n";
for(auto& s : cycleDetection.dependencies[name]){
llvm::errs() << "\t" << s << "\n";
}
llvm::errs() << cycleDetection.isCyclic(name) << "\n";
}

return true;
}

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

return true;
return true;

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

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

//llvm::errs() << newName << "\n";
//for(auto& s : cycleDetection.dependencies[newName]){
// llvm::errs() << "\t" << s << "\n";
//}
//llvm::errs() << cycleDetection.isCyclic(newName) << "\n";


if(cycleDetection.isCyclic(newName)){
llvm::errs() << "Error: " << newName << " ic cyclic\n";
llvm::errs() << newName << "\n";
for(auto& s : cycleDetection.dependencies[newName]){
llvm::errs() << "\t" << s << "\n";
}
llvm::errs() << cycleDetection.isCyclic(newName) << "\n";
}

if(fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
declarations += "\ttype " + newName + " = " + "native.CStruct" + std::to_string(fieldCnt) + "[" + fields + "]\n";
Expand All @@ -165,6 +181,8 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
helpers += "\timplicit class " + newName + "_ops(val p: native.Ptr[struct_" + name + "]) extends AnyVal {\n";
helpers += helpersFunc;
helpers += "\t}\n\n";

helpers += "\tdef " + newName + "()(implicit z: native.Zone): native.Ptr[" + newName + "] = native.alloc[" + newName + "]\n\n";
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions TypeTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ std::string TypeTranslator::TranslateFunctionPointer(const clang::QualType& qtpe
}
}

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

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

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

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

} else if(qtpe->isConstantArrayType()){
return TranslateConstantArray(ctx->getAsConstantArrayType(qtpe), avoid);

} else if(qtpe->isArrayType()){
return TranslatePointer(ctx->getAsArrayType(qtpe)->getElementType(), avoid);
} else {

auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());
Expand Down
2 changes: 1 addition & 1 deletion TypeTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TypeTranslator {
*/
std::string Translate(const clang::QualType& tpe, const std::string* = nullptr);
std::string TranslateFunctionPointer(const clang::QualType& qtpe, const std::string* avoid );
std::string TranslatePointer(const clang::PointerType* ptr, const std::string* avoid );
std::string TranslatePointer(const clang::QualType& pointee, const std::string* avoid);
std::string TranslateStructOrUnion(const clang::QualType& qtpe);
std::string TranslateEnum(const clang::QualType& qtpe);
std::string TranslateConstantArray(const clang::ConstantArrayType* ar, const std::string* avoid );
Expand Down