|
1 | 1 | #include "IR.h" |
2 | 2 | #include "../Utils.h" |
3 | 3 |
|
4 | | -IR::IR(std::string libName) : libName(std::move(libName)) {} |
| 4 | +IR::IR(std::string libName) : libName(std::move(libName)) { |
| 5 | + if (this->libName == "native") { |
| 6 | + /* there are at most 3 objects in the file. |
| 7 | + * All of them will have distinct names. */ |
| 8 | + libObjectName = "nativeLib"; |
| 9 | + } else { |
| 10 | + libObjectName = this->libName; |
| 11 | + } |
| 12 | +} |
5 | 13 |
|
6 | 14 | void IR::addFunction(std::string name, std::vector<Parameter> parameters, |
7 | 15 | std::string retType, bool isVariadic) { |
@@ -46,7 +54,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) { |
46 | 54 | << "import scala.scalanative.native._\n\n"; |
47 | 55 | } |
48 | 56 |
|
49 | | - std::string libObjName = handleReservedWords(ir.libName); |
| 57 | + std::string libObjName = handleReservedWords(ir.libObjectName); |
50 | 58 |
|
51 | 59 | if (!ir.libObjEmpty()) { |
52 | 60 | s << "@native.link(\"" << ir.libName << "\")\n" |
@@ -116,6 +124,7 @@ void IR::generateTypeDefs() { |
116 | 124 |
|
117 | 125 | void IR::generate(const std::string &excludePrefix) { |
118 | 126 | if (!generated) { |
| 127 | + setScalaNames(); |
119 | 128 | filterDeclarations(excludePrefix); |
120 | 129 | generateTypeDefs(); |
121 | 130 | generated = true; |
@@ -199,5 +208,30 @@ bool IR::typeIsUsedOnlyInTypeDefs(std::string type) { |
199 | 208 | } |
200 | 209 |
|
201 | 210 | void IR::setPackageName(std::string packageName) { |
202 | | - this->packageName = packageName; |
| 211 | + this->packageName = std::move(packageName); |
| 212 | +} |
| 213 | + |
| 214 | +void IR::setScalaNames() { |
| 215 | + /* Renaming according to Scala naming conventions |
| 216 | + * should happen here */ |
| 217 | + |
| 218 | + for (auto &function : functions) { |
| 219 | + if (function.getName() == "native") { |
| 220 | + std::string scalaName = "nativeFunc"; |
| 221 | + int i = 0; |
| 222 | + while (existsFunctionWithName(scalaName)) { |
| 223 | + scalaName = "nativeFunc" + std::to_string(i++); |
| 224 | + } |
| 225 | + function.setScalaName(scalaName); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +bool IR::existsFunctionWithName(std::string functionName) { |
| 231 | + for (const auto &function : functions) { |
| 232 | + if (function.getName() == functionName) { |
| 233 | + return true; |
| 234 | + } |
| 235 | + } |
| 236 | + return false; |
203 | 237 | } |
0 commit comments