Skip to content
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

Allow shadowing of external values #86

Merged
merged 3 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 13 additions & 13 deletions src/renamer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ module private RenamerImpl =
let l = env.reusableNames |> List.filter (fun x -> x.[0] <> newName.[0])
{env with varRenames = Map.add id newName env.varRenames; reusableNames = l}, newName

let dontRename env name =
let names = env.reusableNames |> List.filter ((<>) name)
{env with varRenames = env.varRenames.Add(name, name); reusableNames = names}

let dontRenameList env names =
let mutable env = env
for name in names do env <- dontRename env name
env

let renFunction env nbArgs id =
if List.exists ((=) id) options.noRenamingList then env, id // don't rename "main"
else
Expand Down Expand Up @@ -200,7 +209,7 @@ module private RenamerImpl =
| None -> false
if isTopLevel && (ext || options.hlsl || options.preserveAllGlobals) then
if options.preserveExternals then
{env with reusableNames = List.filter ((<>)decl.name) env.reusableNames}, decl.name
dontRename env decl.name, decl.name
else
let env, newName = env.newName env decl.name
Formatter.export "" decl.name newName // TODO: first argument seems now useless
Expand Down Expand Up @@ -231,9 +240,9 @@ module private RenamerImpl =
| e -> e
mapStmt (mapEnv collect id) block |> ignore
let set = HashSet(Seq.choose env.varRenames.TryFind d)
let varRenames, availableNames = Map.partition (fun _ id -> set.Contains id) env.varRenames
let varRenames, availableNames = env.varRenames |> Map.partition (fun _ id -> id.Length > 2 || set.Contains id)
let availableNames = availableNames |> Seq.filter (fun x -> not (List.exists ((=) x.Value) options.noRenamingList))
let merged = [for i in availableNames -> i.Value] @ env.reusableNames |> Seq.distinct |> Seq.toList // |> List.sort
let merged = [for i in availableNames -> i.Value] @ env.reusableNames |> Seq.distinct |> Seq.toList
{env with varRenames=varRenames; reusableNames=merged}

let rec renStmt env =
Expand Down Expand Up @@ -286,18 +295,9 @@ module private RenamerImpl =
Function({fct with args=args}, body)
| e -> e

// Remove the values from the env
// so that the functions are not overloaded
let rec doNotOverload env = function
| [] -> env
| name::li ->
let names = env.reusableNames |> List.filter ((<>) name)
let env = {env with varRenames = Map.add name name env.varRenames; reusableNames = names}
doNotOverload env li

let renameTopLevel li env =
// Rename top-level values first
let env = doNotOverload env options.noRenamingList
let env = dontRenameList env options.noRenamingList
let env, li = renList env renTopLevelName li

// Then, rename local values
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/externals.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef EXTERNALS_EXPECTED_
# define EXTERNALS_EXPECTED_
# define VAR_E "t"
# define VAR_I "i"
# define VAR_N "n"

const char *externals_frag =
"uniform int i,n,t;"
"int r()"
"{"
"int n,t,r,u,e;"
"return i;"
"}"
"int u()"
"{"
"int i,t,r,u,e;"
"return n;"
"}"
"int e()"
"{"
"int i,r,u,n,e;"
"return t;"
"}";

#endif // EXTERNALS_EXPECTED_
16 changes: 16 additions & 0 deletions tests/unit/externals.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
uniform int i, n, e;

int f() {
int var1, var2, var3, var4, var5;
return i;
}

int g() {
int var1, var2, var3, var4, var5;
return n;
}

int h() {
int var1, var2, var3, var4, var5;
return e;
}
25 changes: 25 additions & 0 deletions tests/unit/externals.preserved.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef EXTERNALS_PRESERVED_EXPECTED_
# define EXTERNALS_PRESERVED_EXPECTED_

const char *externals_frag =
"uniform int i,n,e;"
"int t()"
"{"
"int n,e,t,r,u;"
"return i;"
"}"
"int r()"
"{"
"int i,e,t,r,u;"
"return n;"
"}"
"int u()"
"{"
"int i,t,r,n,u;"
"return e;"
"}";

#endif // EXTERNALS_PRESERVED_EXPECTED_
41 changes: 41 additions & 0 deletions tests/unit/function_overload.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef FUNCTION_OVERLOAD_EXPECTED_
# define FUNCTION_OVERLOAD_EXPECTED_

const char *function_overload_frag =
"int t()"
"{"
"return 0;"
"}"
"int t(int n)"
"{"
"return t()+n;"
"}"
"int t(int i,int n)"
"{"
"return t(i)+n;"
"}"
"int t(int i,int n,int r)"
"{"
"return t(i,n)+r;"
"}"
"int t(int i,int n,int r,int u)"
"{"
"return t(i,n,r)+u;"
"}"
"int n(int n)"
"{"
"return n;"
"}"
"int n(int i,int r)"
"{"
"return n(i)*r;"
"}"
"int n(int i,int r,int u)"
"{"
"return n(i,r)*u;"
"}";

#endif // FUNCTION_OVERLOAD_EXPECTED_
15 changes: 15 additions & 0 deletions tests/unit/function_overload.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
int sum0() { return 0; }

int sum1(int x) { return sum0() + x; }

int sum2(int x, int y) { return sum1(x) + y; }

int sum3(int x, int y, int z) { return sum2(x, y) + z; }

int sum4(int x, int y, int z, int w) { return sum3(x, y, z) + w; }

int mult1(int x) { return x; }

int mult2(int x, int y) { return mult1(x) * y; }

int mult3(int x, int y, int z) { return mult2(x, y) * z; }