Skip to content

Commit b829c00

Browse files
committed
Small fixes
1 parent 67a6ec4 commit b829c00

18 files changed

+88
-79
lines changed

ir/Enum.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Enum.h"
2-
#include <utility>
2+
#include <sstream>
3+
#include <cassert>
34

45
Enumerator::Enumerator(std::string name, uint64_t value)
56
: name(std::move(name)), value(value) {}
@@ -16,7 +17,7 @@ bool Enum::isAnonymous() const {
1617
}
1718

1819
TypeDef Enum::generateTypeDef() const {
19-
assert (!name.empty()); // called after isAnonymous()
20+
assert (!isAnonymous());
2021
return TypeDef("enum_" + name, "native.CInt");
2122
}
2223

@@ -29,7 +30,7 @@ std::ostream &operator<<(std::ostream &s, const Enum &e) {
2930
} else {
3031
enumeratorName = "enum_" + enumerator.getName();
3132
}
32-
s << " final val " << enumeratorName << " = " << std::to_string(i++) + "\n";
33+
s << " final val " << enumeratorName << " = " << std::to_string(i++) << std::endl;
3334
}
3435
return s;
3536
}

ir/Enum.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44

55
#include <string>
6-
#include <clang/AST/Type.h>
7-
#include <stdint-gcc.h>
6+
#include <vector>
87
#include "TypeDef.h"
98

109
class Enumerator {

ir/Function.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "Function.h"
22
#include "../Utils.h"
3-
#include <utility>
43

54

65
Parameter::Parameter(std::string name, std::string type)

ir/Function.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <string>
66
#include <vector>
7-
#include <clang/AST/Type.h>
87
#include "TypeAndName.h"
98

109
class Parameter : public TypeAndName {

ir/IR.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include <sstream>
2-
#include <utility>
3-
#include <llvm/Support/raw_ostream.h>
42
#include "IR.h"
53
#include "../Utils.h"
64

@@ -41,17 +39,17 @@ std::string IR::generate() {
4139
generateDecl();
4240
std::stringstream s;
4341
if (!libObjEmpty() || !enums.empty()) {
44-
s << "import scala.scalanative._\n"
45-
<< "import scala.scalanative.native._\n"
46-
<< "import scala.scalanative.native.Nat._\n\n";
42+
s << "import scala.scalanative._" << std::endl
43+
<< "import scala.scalanative.native._" << std::endl
44+
<< "import scala.scalanative.native.Nat._" << std::endl << std::endl;
4745
}
4846

4947
std::string libObjName = handleReservedWords(libName);
5048

5149
if (!libObjEmpty()) {
52-
s << "@native.link(\"" << libName << "\")\n"
53-
<< "@native.extern\n"
54-
<< "object " << libObjName << " {\n";
50+
s << "@native.link(\"" << libName << "\")" << std::endl
51+
<< "@native.extern" << std::endl
52+
<< "object " << libObjName << " {" << std::endl;
5553

5654
for (const auto &typeDef : typeDefs) {
5755
s << typeDef;
@@ -61,38 +59,38 @@ std::string IR::generate() {
6159
s << func;
6260
}
6361

64-
s << "}\n\n";
62+
s << "}" << std::endl << std::endl;
6563
}
6664

6765
if (!enums.empty() || hasHelperMethods()) {
68-
s << "import " << libObjName << "._\n\n";
66+
s << "import " << libObjName << "._" << std::endl << std::endl;
6967
}
7068

7169
if (!enums.empty()) {
72-
s << "object " << libName << "Enums {\n";
70+
s << "object " << libName << "Enums {" << std::endl;
7371

7472
for (const auto &e : enums) {
7573
s << e
76-
<< "\n"; // space between groups of enums
74+
<< std::endl; // space between groups of enums
7775
}
7876

79-
s << "}\n\n";
77+
s << "}" << std::endl << std::endl;
8078
}
8179

8280
if (hasHelperMethods()) {
83-
s << "object " << libName << "Helpers {\n";
81+
s << "object " << libName << "Helpers {" << std::endl;
8482

8583
for (const auto &st : structs) {
86-
s << "\n";
87-
s << st.generateHelperClass();
84+
s << std::endl
85+
<< st.generateHelperClass();
8886
}
8987

9088
for (const auto &u : unions) {
91-
s << "\n";
92-
s << u.generateHelperClass();
89+
s << std::endl
90+
<< u.generateHelperClass();
9391
}
9492

95-
s << "}\n";
93+
s << "}" << std::endl << std::endl;
9694
}
9795

9896
return s.str();
@@ -132,3 +130,7 @@ bool IR::hasHelperMethods() const {
132130
}
133131
return false;
134132
}
133+
134+
bool IR::hasEnums() const {
135+
return enums.size() != 0;
136+
}

ir/IR.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class IR {
3232
*/
3333
bool libObjEmpty() const;
3434

35+
bool hasEnums() const;
36+
3537
std::string generate();
3638

3739
private:
@@ -54,7 +56,7 @@ class IR {
5456
std::vector<Struct> structs;
5557
std::vector<Union> unions;
5658
std::vector<Enum> enums;
57-
bool generated = false; // generate type defs only ones
59+
bool generated = false; // generate type defs only once
5860
};
5961

6062

ir/Struct.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "Struct.h"
22
#include "../Utils.h"
3-
#include <utility>
43
#include <sstream>
54

65
Field::Field(std::string name, std::string type)
@@ -18,7 +17,7 @@ TypeDef Struct::generateTypeDef() const {
1817
"native.CStruct" + std::to_string(fields.size()) + "[" + getFieldsTypes() + "]");
1918
} else {
2019
// There is no easy way to represent it as a struct in scala native, have to represent it as an array and then
21-
// Add helpers to help with it's manipulation
20+
// Add helpers to help with its manipulation
2221
return TypeDef("struct_" + name, "native.CArray[Byte, " + uint64ToScalaNat(typeSize) + "]");
2322
}
2423
}
@@ -42,22 +41,24 @@ std::string Struct::generateHelperClass() const {
4241
}
4342
std::stringstream s;
4443
std::string newName = "struct_" + name;
45-
s << " implicit class " << newName << "_ops(val p: native.Ptr[struct_" << name << "]) extends AnyVal {\n";
44+
s << " implicit class " << newName << "_ops(val p: native.Ptr[struct_" << name << "])"
45+
<< " extends AnyVal {" << std::endl;
4646
int fieldIndex = 0;
4747
for (const auto &field : fields) {
4848
if (!field.getName().empty()) {
4949
std::string fname = handleReservedWords(field.getName());
5050
std::string ftype = field.getType();
51-
s << " def " << fname << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << "\n";
52-
s << " def " << fname << "_=(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
53-
<< " = value\n";
51+
s << " def " << fname << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << std::endl
52+
<< " def " << fname << "_=(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
53+
<< " = value" << std::endl;
5454
}
5555
fieldIndex++;
5656
}
57-
s << " }\n\n";
57+
s << " }" << std::endl << std::endl;
5858

5959
/* makes struct instantiation easier */
60-
s << " def " << newName + "()(implicit z: native.Zone): native.Ptr[" + newName + "] = native.alloc[" + newName + "]\n";
60+
s << " def " << newName + "()(implicit z: native.Zone): native.Ptr[" + newName + "]"
61+
<< " = native.alloc[" + newName + "]" << std::endl;
6162

6263
return s.str();
6364
}
@@ -77,18 +78,18 @@ TypeDef Union::generateTypeDef() const {
7778
std::string Union::generateHelperClass() const {
7879
std::stringstream s;
7980
s << " implicit class union_" << name << "_pos"
80-
<< "(val p: native.Ptr[native.CArray[Byte, " << uint64ToScalaNat(maxSize) << "]]) extends AnyVal {\n";
81+
<< "(val p: native.Ptr[union_" << name << "]) extends AnyVal {" << std::endl;
8182
for (const auto &field : fields) {
8283
if (!field.getName().empty()) {
8384
std::string fname = handleReservedWords(field.getName());
8485
std::string ftype = field.getType();
8586
s << " def " << fname
86-
<< ": native.Ptr[" << ftype << "] = p.cast[native.Ptr[" << ftype << "]]\n";
87+
<< ": native.Ptr[" << ftype << "] = p.cast[native.Ptr[" << ftype << "]]" << std::endl;
8788

8889
s << " def " << fname
89-
<< "_=(value: " << ftype << "): Unit = !(p.cast[native.Ptr[" << ftype << "]]) = value\n";
90+
<< "_=(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value" << std::endl;
9091
}
9192
}
92-
s << " }\n";
93+
s << " }" << std::endl;
9394
return s.str();
9495
}

ir/TypeAndName.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
#include <string>
6-
#include <clang/AST/Type.h>
76

87
/**
98
* Base class for function parameters, struct fields

ir/TypeDef.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "TypeDef.h"
22
#include "../Utils.h"
3-
#include <utility>
43

54
TypeDef::TypeDef(std::string name, std::string type)
65
: TypeAndName(std::move(name), std::move(type)) {}

ir/TypeDef.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
#include <string>
6-
#include <clang/AST/Type.h>
76
#include "TypeAndName.h"
87

98

0 commit comments

Comments
 (0)