Skip to content

Generate new name if varArgs parameter already exist #42

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 1 commit into from
Jun 8, 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
20 changes: 19 additions & 1 deletion ir/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
}
if (func.isVariadic) {
/* the C Iso require at least one argument in a variadic function, so the comma is fine */
s << ", varArgs: native.CVararg*";
s << ", " << func.getVarargsParameterName() << ": native.CVararg*";
}
s << "): "
<< func.retType
Expand All @@ -46,3 +46,21 @@ bool Function::usesType(const std::string &type) const {
std::string Function::getName() const {
return name;
}

std::string Function::getVarargsParameterName() const {
std::string parameterName = "varArgs";
int i = 0;
while (existParameterWithName(parameterName)) {
parameterName = "varArgs" + std::to_string(i++);
}
return parameterName;
}

bool Function::existParameterWithName(const std::string &parameterName) const {
for (const auto &parameter : parameters) {
if (parameter.getName() == parameterName) {
return true;
}
}
return false;
}
5 changes: 5 additions & 0 deletions ir/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Function {
std::string getName() const;

private:

std::string getVarargsParameterName() const;

bool existParameterWithName(const std::string &parameterName) const;

std::string name;
std::vector<Parameter> parameters;
std::string retType;
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ int void_arg(void);
void one_arg(int a);
void *two_args(float a, int b);
void anonymous_args(float, int);
double variadic_args(double a, void* b, ...);
double variadic_args(double a, void* varArgs, ...);
2 changes: 1 addition & 1 deletion tests/samples/Function.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ object Function {
def one_arg(a: native.CInt): Unit = native.extern
def two_args(a: native.CFloat, b: native.CInt): native.Ptr[Byte] = native.extern
def anonymous_args(anonymous0: native.CFloat, anonymous1: native.CInt): Unit = native.extern
def variadic_args(a: native.CDouble, b: native.Ptr[Byte], varArgs: native.CVararg*): native.CDouble = native.extern
def variadic_args(a: native.CDouble, varArgs: native.Ptr[Byte], varArgs0: native.CVararg*): native.CDouble = native.extern
}