Skip to content

Commit

Permalink
TypeChecker
Browse files Browse the repository at this point in the history
- Moved the reference counting mechanism into the `TypeChecker` to avoid problem of seperate `DNodeGenerator` instances having their own seperate counts
- We now check the reference count values after function definition processing so as to get a proper count

Dependency

- Use the reference counting from `TypeChecker`
- Removed reference counting code

Test cases

- Added a positive case in the form of `unused_vars.t`
- Added a negative case in the form of `unused_vars_none.t`
  • Loading branch information
deavmi committed Nov 4, 2023
1 parent 94555a3 commit 2d90d01
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 51 deletions.
62 changes: 62 additions & 0 deletions source/tlang/compiler/typecheck/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ public final class TypeChecker
}

// NOTE: Check scope guard for "exit routines" which run here


/**
* Find the variables which were declared but never used
*
* TODO: Add compiler entry check here for whether or not
* these should be printed out
*/
Variable[] unusedVariables = getUnusedVariables();
gprintln("There are "~to!(string)(unusedVariables.length)~" unused variables");
if(unusedVariables.length)
{
foreach(Variable unusedVariable; unusedVariables)
{
gprintln("Variable '"~to!(string)(unusedVariable)~"' is declared but never");
}
}
}


Expand Down Expand Up @@ -3258,6 +3275,51 @@ public final class TypeChecker
//assert()
}

/**
* Maps a given `Variable` to its reference
* count. This includes the declaration
* thereof.
*/
private uint[Variable] varRefCounts;

/**
* Increments the given variable's reference
* count
*
* Params:
* variable = the variable
*/
void touch(Variable variable)
{
// Create entry if not existing yet
if(variable !in this.varRefCounts)
{
this.varRefCounts[variable] = 0;
}

// Increment count
this.varRefCounts[variable]++;
}

/**
* Returns all variables which were declared
* but not used
*
* Returns: the array of variables
*/
public Variable[] getUnusedVariables()
{
Variable[] unused;
foreach(Variable variable; this.varRefCounts.keys())
{
if(!(this.varRefCounts[variable] > 1))
{
unused ~= variable;
}
}

return unused;
}
}


Expand Down
54 changes: 3 additions & 51 deletions source/tlang/compiler/typecheck/dependency/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ public class DNodeGenerator
Variable variable = cast(Variable)namedEntity;

/* Variable reference count must increase */
touch(variable);
tc.touch(variable);

/* Pool the node */
VariableNode varDecNode = poolT!(VariableNode, Variable)(variable);
Expand Down Expand Up @@ -1107,7 +1107,7 @@ public class DNodeGenerator
writeln("VarType: "~to!(string)(variableType));

/* Add an entry to the reference counting map */
touch(variable);
tc.touch(variable);

/* Basic type */
if(cast(Primitive)variableType)
Expand Down Expand Up @@ -1212,7 +1212,7 @@ public class DNodeGenerator
assert(variable);

/* Assinging to a variable is usage, therefore increment the reference count */
touch(variable);
tc.touch(variable);


/* Pool the variable */
Expand Down Expand Up @@ -1708,52 +1708,4 @@ public class DNodeGenerator

return classDNode;
}


/**
* Maps a given `Variable` to its reference
* count. This includes the declaration
* thereof.
*/
private uint[Variable] varRefCounts;

/**
* Increments the given variable's reference
* count
*
* Params:
* variable = the variable
*/
private void touch(Variable variable)
{
// Create entry if not existing yet
if(variable !in this.varRefCounts)
{
this.varRefCounts[variable] = 0;
}

// Increment count
this.varRefCounts[variable]++;
}

/**
* Returns all variables which were declared
* but not used
*
* Returns: the array of variables
*/
public Variable[] getUnusedVariables()
{
Variable[] unused;
foreach(Variable variable; this.varRefCounts.keys())
{
if(!(this.varRefCounts[variable] > 1))
{
unused ~= variable;
}
}

return unused;
}

}
3 changes: 3 additions & 0 deletions source/tlang/testing/unused_vars.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module unused_vars;

int j;
8 changes: 8 additions & 0 deletions source/tlang/testing/unused_vars_none.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module unused_vars;

int j;

void thing()
{
j = 1;
}

0 comments on commit 2d90d01

Please sign in to comment.