Skip to content

Commit eb7b872

Browse files
committed
Refine structure type comparison (760)
The prior method of comparing structures by examining them for identical layout failed if you wanted to have two structures that purposely had the same layout but wanted their semantics to be different (for example, by having a function that would work on only one of them). Change the struct type checking comparisons to check NAME as well as struct layout. Also need to no longer mangle the name when reading in oso. While I was there, and adding a new test for this, I renamed a test I added recently, to more closely match other related ones.
1 parent 3f6a926 commit eb7b872

9 files changed

Lines changed: 55 additions & 7 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ TESTSUITE ( and-or-not-synonyms aastep arithmetic array array-derivs array-range
237237
draw_string
238238
error-dupes exit exponential
239239
function-earlyreturn function-simple function-outputelem
240-
function-overload-struct
241240
geomath getattribute-camera getattribute-shader
242241
getsymbol-nonheap gettextureinfo
243242
group-outputs groupstring
@@ -276,8 +275,9 @@ TESTSUITE ( and-or-not-synonyms aastep arithmetic array array-derivs array-range
276275
select shortcircuit spline splineinverse spline-derivbug
277276
string
278277
struct struct-array struct-array-mixture
279-
struct-err struct-init-copy struct-layers
280-
struct-return struct-with-array
278+
struct-err struct-init-copy
279+
struct-isomorphic-overload struct-layers
280+
struct-operator-overload struct-return struct-with-array
281281
struct-nested struct-nested-assign struct-nested-deep
282282
ternary
283283
testshade-expr

src/liboslexec/osogram.y

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,7 @@ typespec
212212
}
213213
| STRUCT IDENTIFIER
214214
{
215-
// Prepend the shader name to make globally unique
216-
std::string mangled = current_shader_name + "_" + $2;
217-
current_typespec = TypeSpec (mangled.c_str(), 0);
215+
current_typespec = TypeSpec ($2, 0);
218216
$$ = 0;
219217
}
220218
;

src/liboslexec/typespec.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ equivalent (const TypeSpec &a, const TypeSpec &b)
156156
if (a == b)
157157
return true;
158158
// or if they are structs, and the structs are equivalent
159-
if (a.is_structure() || b.is_structure())
159+
if (a.is_structure() || b.is_structure()) {
160160
return a.is_structure() && b.is_structure() &&
161+
a.structspec()->name() == b.structspec()->name() &&
161162
equivalent(a.structspec(), b.structspec());
163+
}
162164
// or if the underlying simple types are equivalent
163165
return
164166
((a.is_vectriple_based() && b.is_vectriple_based()) || equivalent(a.m_simple, b.m_simple))
File renamed without changes.
File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// The point of this test is to see what happens when we overload
2+
// two structs that have the same layout. There was a bug where this
3+
// got messed up because we distinguished polymorphic args for structs
4+
// based only on layout. Now we do it on name, but still double check
5+
// the layout.
6+
7+
struct taco
8+
{
9+
float r;
10+
float g;
11+
};
12+
13+
14+
taco __operator__add__(taco c1, taco c2)
15+
{
16+
return taco(c1.r + c2.r, c1.g + c2.g);
17+
}
18+
19+
20+
21+
struct cheese
22+
{
23+
float r;
24+
float g;
25+
};
26+
27+
cheese __operator__add__(cheese c1, cheese c2)
28+
{
29+
return cheese(c1.r + c2.r, c1.g + c2.g);
30+
}
31+
32+
33+
34+
shader test (
35+
taco c2_in_1 = {.1, .1},
36+
taco c2_in_2 = {.2, .2},
37+
output taco c2_out = {0, 0},
38+
)
39+
{
40+
c2_out = c2_in_1 + c2_in_2;
41+
printf ("c2_out = %g %g\n", c2_out.r, c2_out.g);
42+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Compiled test.osl -> test.oso
2+
c2_out = 0.3 0.3
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
3+
command = testshade("test")
File renamed without changes.

0 commit comments

Comments
 (0)