Skip to content

Commit d5ad478

Browse files
committed
Error on redefinition of existing function with matching signatures.
Remove duplicate color2 pow functions which this exposed.
1 parent be57edf commit d5ad478

File tree

6 files changed

+62
-18
lines changed

6 files changed

+62
-18
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ TESTSUITE ( and-or-not-synonyms aastep arithmetic array array-derivs array-range
267267
oslc-comma oslc-D
268268
oslc-err-arrayindex oslc-err-closuremul oslc-err-field
269269
oslc-err-format oslc-err-intoverflow
270-
oslc-err-noreturn oslc-err-notfunc
270+
oslc-err-names oslc-err-noreturn oslc-err-notfunc
271271
oslc-err-outputparamvararray oslc-err-paramdefault
272272
oslc-err-struct-array-init oslc-err-struct-ctr
273273
oslc-err-struct-dup

src/liboslcomp/ast.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,11 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
317317
f = NULL;
318318
}
319319

320-
// FIXME -- allow multiple function declarations, but only if they
321-
// aren't the same polymorphic type.
322-
323320
if (name[0] == '_' && name[1] == '_' && name[2] == '_') {
324321
error ("\"%s\" : sorry, can't start with three underscores",
325322
name.c_str());
326323
}
327324

328-
m_sym = new FunctionSymbol (name, type, this);
329-
func()->nextpoly ((FunctionSymbol *)f);
330325
std::string argcodes = oslcompiler->code_from_type (m_typespec);
331326
for (ASTNode *arg = form; arg; arg = arg->nextptr()) {
332327
const TypeSpec &t (arg->typespec());
@@ -341,6 +336,36 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
341336
v->error ("function parameter '%s' may not have a default initializer.",
342337
v->name().c_str());
343338
}
339+
340+
if (stmts && f && f->symtype() == SymTypeFunction) {
341+
for (FunctionSymbol* poly = static_cast<FunctionSymbol*>(f); poly;
342+
poly = poly->nextpoly ()) {
343+
// If the argcodes match, only one should have statements.
344+
// If there is no ASTNode for the poly, must be a builtin, and has
345+
// 'implicit' statements.
346+
auto other = static_cast<ASTfunction_declaration*>(poly->node());
347+
if ((!other || other->statements()) && poly->argcodes() == argcodes) {
348+
ASSERT (argcodes.size() > 0);
349+
error ("redefinition of %sfunction: %s %s (%s).%s",
350+
other ? "" : "builtin ",
351+
type_c_str(type), m_name,
352+
argcodes.size() <= 1 ? "" :
353+
m_compiler->typelist_from_code(argcodes.c_str()+1),
354+
other ? Strutil::format (
355+
"\n\t\tprevious declaration was at %s:%d",
356+
OIIO::Filesystem::filename(other->sourcefile().string()),
357+
other->sourceline())
358+
: std::string());
359+
// Set m_sym to the function that is being overwritten.
360+
m_sym = poly;
361+
return;
362+
}
363+
}
364+
}
365+
366+
m_sym = new FunctionSymbol (name, type, this);
367+
func()->nextpoly ((FunctionSymbol *)f);
368+
344369
func()->argcodes (ustring (argcodes));
345370
oslcompiler->symtab().insert (m_sym);
346371

src/shaders/color2.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,6 @@ color2 clamp(color2 c, float minval, float maxval)
181181
return clamp(c, color2(minval, minval), color2(maxval, maxval));
182182
}
183183

184-
color2 pow(color2 base, color2 power)
185-
{
186-
return color2(pow(base.r, power.r),
187-
pow(base.a, power.a));
188-
}
189-
190-
color2 pow(color2 base, float power)
191-
{
192-
return color2(pow(base.r, power),
193-
pow(base.a, power));
194-
}
195-
196184
color2 max(color2 a, color2 b)
197185
{
198186
return color2(max(a.r, b.r),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test.osl:5: error: redefinition of function: int redclfunc (int).
2+
previous declaration was at test.osl:4
3+
test.osl:10: error: redefinition of builtin function: string concat (string, string).
4+
test.osl:13: error: redefinition of function: void redclfunc ().
5+
previous declaration was at test.osl:12
6+
FAILED test.osl

testsuite/oslc-err-names/run.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
3+
# command = oslc("test.osl")
4+
# don't even need that -- it's automatic
5+
failureok = 1 # this test is expected to have oslc errors
6+

testsuite/oslc-err-names/test.osl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
int redclfunc (int f); // ok
3+
int redclfunc (int f); // ok
4+
int redclfunc (int f) { return 0; } // ok
5+
int redclfunc (int f) { return 1; } // fail
6+
int redclfunc (int f); // ok
7+
8+
float redclfunc (float f) { return 0.0; } // ok
9+
10+
string concat (string a, string b) { return ""; } // fail
11+
12+
void redclfunc () { int a = 10; }
13+
void redclfunc () { int a = 10; }
14+
15+
shader test ()
16+
{
17+
redclfunc(5);
18+
concat("a", "b");
19+
}

0 commit comments

Comments
 (0)