Skip to content

Commit

Permalink
Error on redefinition of existing function with matching signatures.
Browse files Browse the repository at this point in the history
Remove duplicate color2 pow functions which this exposed.
  • Loading branch information
marsupial committed Dec 29, 2017
1 parent be57edf commit d5ad478
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ TESTSUITE ( and-or-not-synonyms aastep arithmetic array array-derivs array-range
oslc-comma oslc-D
oslc-err-arrayindex oslc-err-closuremul oslc-err-field
oslc-err-format oslc-err-intoverflow
oslc-err-noreturn oslc-err-notfunc
oslc-err-names oslc-err-noreturn oslc-err-notfunc
oslc-err-outputparamvararray oslc-err-paramdefault
oslc-err-struct-array-init oslc-err-struct-ctr
oslc-err-struct-dup
Expand Down
35 changes: 30 additions & 5 deletions src/liboslcomp/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,11 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
f = NULL;
}

// FIXME -- allow multiple function declarations, but only if they
// aren't the same polymorphic type.

if (name[0] == '_' && name[1] == '_' && name[2] == '_') {
error ("\"%s\" : sorry, can't start with three underscores",
name.c_str());
}

m_sym = new FunctionSymbol (name, type, this);
func()->nextpoly ((FunctionSymbol *)f);
std::string argcodes = oslcompiler->code_from_type (m_typespec);
for (ASTNode *arg = form; arg; arg = arg->nextptr()) {
const TypeSpec &t (arg->typespec());
Expand All @@ -341,6 +336,36 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
v->error ("function parameter '%s' may not have a default initializer.",
v->name().c_str());
}

if (stmts && f && f->symtype() == SymTypeFunction) {
for (FunctionSymbol* poly = static_cast<FunctionSymbol*>(f); poly;
poly = poly->nextpoly ()) {
// If the argcodes match, only one should have statements.
// If there is no ASTNode for the poly, must be a builtin, and has
// 'implicit' statements.
auto other = static_cast<ASTfunction_declaration*>(poly->node());
if ((!other || other->statements()) && poly->argcodes() == argcodes) {
ASSERT (argcodes.size() > 0);
error ("redefinition of %sfunction: %s %s (%s).%s",
other ? "" : "builtin ",
type_c_str(type), m_name,
argcodes.size() <= 1 ? "" :
m_compiler->typelist_from_code(argcodes.c_str()+1),
other ? Strutil::format (
"\n\t\tprevious declaration was at %s:%d",
OIIO::Filesystem::filename(other->sourcefile().string()),
other->sourceline())
: std::string());
// Set m_sym to the function that is being overwritten.
m_sym = poly;
return;
}
}
}

m_sym = new FunctionSymbol (name, type, this);
func()->nextpoly ((FunctionSymbol *)f);

func()->argcodes (ustring (argcodes));
oslcompiler->symtab().insert (m_sym);

Expand Down
12 changes: 0 additions & 12 deletions src/shaders/color2.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,6 @@ color2 clamp(color2 c, float minval, float maxval)
return clamp(c, color2(minval, minval), color2(maxval, maxval));
}

color2 pow(color2 base, color2 power)
{
return color2(pow(base.r, power.r),
pow(base.a, power.a));
}

color2 pow(color2 base, float power)
{
return color2(pow(base.r, power),
pow(base.a, power));
}

color2 max(color2 a, color2 b)
{
return color2(max(a.r, b.r),
Expand Down
6 changes: 6 additions & 0 deletions testsuite/oslc-err-names/ref/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test.osl:5: error: redefinition of function: int redclfunc (int).
previous declaration was at test.osl:4
test.osl:10: error: redefinition of builtin function: string concat (string, string).
test.osl:13: error: redefinition of function: void redclfunc ().
previous declaration was at test.osl:12
FAILED test.osl
6 changes: 6 additions & 0 deletions testsuite/oslc-err-names/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

# command = oslc("test.osl")
# don't even need that -- it's automatic
failureok = 1 # this test is expected to have oslc errors

19 changes: 19 additions & 0 deletions testsuite/oslc-err-names/test.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

int redclfunc (int f); // ok
int redclfunc (int f); // ok
int redclfunc (int f) { return 0; } // ok
int redclfunc (int f) { return 1; } // fail
int redclfunc (int f); // ok

float redclfunc (float f) { return 0.0; } // ok

string concat (string a, string b) { return ""; } // fail

void redclfunc () { int a = 10; }
void redclfunc () { int a = 10; }

shader test ()
{
redclfunc(5);
concat("a", "b");
}

0 comments on commit d5ad478

Please sign in to comment.