Skip to content

Commit 7f7baa1

Browse files
committed
Clean up encoding of natural numbers used in array sizes
Constant array sizes can only be positive so remove signed conversion of integer to Scala Native natural number encoding in favour of the unsigned one.
1 parent 3e5508d commit 7f7baa1

File tree

3 files changed

+11
-30
lines changed

3 files changed

+11
-30
lines changed

TypeTranslator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ std::string TypeTranslator::TranslateEnum(const clang::QualType& qtpe){
116116
}
117117

118118
std::string TypeTranslator::TranslateConstantArray(const clang::ConstantArrayType* ar, const std::string* avoid){
119-
const llvm::APInt& size = ar->getSize();
120-
return "native.CArray[" + Translate(ar->getElementType(), avoid) + ", " + intToScalaNat((int)size.roundToDouble()) + "]";
119+
const uint64_t size = ar->getSize().getZExtValue();
120+
const std::string nat = uint64ToScalaNat(size);
121+
return "native.CArray[" + Translate(ar->getElementType(), avoid) + ", " + nat + "]";
121122
}
122123

123124
std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::string* avoid){

Utils.h

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,17 @@ inline std::string basename(const std::string& pathname) {
1515
pathname.end()};
1616
}
1717

18-
inline std::string intToScalaNat(int v, std::string accumulator = ""){
19-
20-
if(v > 0){
21-
int last_digit = v % 10;
22-
int rest = v / 10;
23-
24-
if(accumulator == ""){
25-
return intToScalaNat(rest, "_" + std::to_string(last_digit));
26-
} else{
27-
return intToScalaNat(rest, "Digit[_" + std::to_string(last_digit) + ", " + accumulator + "]");
28-
}
29-
} else{
18+
inline std::string uint64ToScalaNat(uint64_t v, std::string accumulator = "") {
19+
if (v == 0)
3020
return accumulator;
31-
}
32-
}
3321

34-
inline std::string uint64ToScalaNat(uint64_t v, std::string accumulator = ""){
22+
int last_digit = v % 10;
23+
int rest = v / 10;
3524

36-
if(v > 0){
37-
int last_digit = v % 10;
38-
int rest = v / 10;
39-
40-
if(accumulator == ""){
41-
return uint64ToScalaNat(rest, "_" + std::to_string(last_digit));
42-
} else{
43-
return uint64ToScalaNat(rest, "Digit[_" + std::to_string(last_digit) + ", " + accumulator + "]");
44-
}
45-
} else{
46-
return accumulator;
25+
if (accumulator.empty()) {
26+
return uint64ToScalaNat(rest, "_" + std::to_string(last_digit));
27+
} else {
28+
return uint64ToScalaNat(rest, "Digit[_" + std::to_string(last_digit) + ", " + accumulator + "]");
4729
}
4830
}
4931

visitor/TreeVisitor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
109109
fields.push_back(Field(fname, ftype));
110110
}
111111

112-
auto usize = intToScalaNat(static_cast<int>(maxSize));
113-
114112
ir->addUnion(name, fields, maxSize);
115113
}
116114

0 commit comments

Comments
 (0)