Skip to content

Commit

Permalink
Completed Generic/Var variable support (new semantics for type/locati…
Browse files Browse the repository at this point in the history
…on inferring for variables).
  • Loading branch information
Xenomega committed Nov 15, 2018
1 parent 11080b5 commit b6297e8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Meadow.CoverageReport/Debugging/Variables/BaseVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ namespace Meadow.CoverageReport.Debugging.Variables
/// </summary>
public abstract class BaseVariable
{
#region Fields
protected bool _isUndefinedType;
#endregion

#region Properties
/// <summary>
/// The variable declaration ast node which defines this variable.
/// </summary>
public AstVariableDeclaration Declaration { get; set; }
/// <summary>
/// The type descriptions ast node for this variable, derived from <see cref="Declaration"/>'s TypeName descriptions
/// (if available), otherwise the direct type descriptions.
/// </summary>
public AstTypeDescriptions TypeDescriptions { get; set; }
/// <summary>
/// The name of this variable.
/// </summary>
public string Name { get; set; }
Expand Down Expand Up @@ -58,11 +67,14 @@ protected void Initialize(AstVariableDeclaration declaration)

protected void Initialize(string name, AstElementaryTypeName astTypeName, AstTypeDescriptions astTypeDescriptions = null)
{
// Set our generic var status.
_isUndefinedType = astTypeName?.TypeDescriptions == null;

// Override our optionally provided type descriptions with one from the ast type name, if available.
astTypeDescriptions = (astTypeName?.TypeDescriptions ?? astTypeDescriptions);
TypeDescriptions = _isUndefinedType ? astTypeDescriptions : astTypeName.TypeDescriptions;

// Initialize using whatever type provider is available
Initialize(name, astTypeDescriptions.TypeString);
Initialize(name, TypeDescriptions.TypeString);
}

protected void Initialize(string name, string typeString)
Expand Down
10 changes: 10 additions & 0 deletions src/Meadow.CoverageReport/Debugging/Variables/LocalVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace Meadow.CoverageReport.Debugging.Variables
/// </summary>
public class LocalVariable : BaseVariable
{
#region Constants
private const string MEMORY_IDENTIFIER = "memory_ptr";
#endregion

#region Properties
/// <summary>
/// Indicates if this local variable is either an input or output parameter to a function.
Expand Down Expand Up @@ -68,6 +72,12 @@ public override VarLocation VariableLocation
return VarLocation.Storage;
case AstVariableStorageLocation.Default:
default:
// Check if this is a generic variable.
if (_isUndefinedType)
{
return TypeDescriptions.TypeIdentifier.Contains(MEMORY_IDENTIFIER, StringComparison.InvariantCultureIgnoreCase) ? VarLocation.Memory : VarLocation.Storage;
}

return IsFunctionParameter ? VarLocation.Memory : VarLocation.Storage;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/Meadow.DebugExampleTests/Contracts/VarAnalysisContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ contract VarAnalysisContract
x = 7;
}

function storagePointerGenericVar1(uint[2] storageArray) internal
{
var testArray = storageArray;
testArray[0]++;
testArray[0]++;
}
function storagePointerGenericVar2(uint[2] storage storageArray) internal
{
var testArray = storageArray;
testArray[0]++;
testArray[0]++;
}

function throwWithGenericVars(uint param1, uint param2) public returns (address addr1, address addr2)
{
assert(param1 == 778899);
Expand All @@ -124,10 +137,14 @@ contract VarAnalysisContract
var gTestEnum = TestEnum.FIRST; // generic enum
gTestEnum = TestEnum.SECOND;

storagePointerGenericVar1(globalArray2);
storagePointerGenericVar2(globalArray2);

TestEnum[] memory arr3 = new TestEnum[](3);
arr3[0] = TestEnum.FIRST;
arr3[1] = TestEnum.SECOND;
arr3[2] = TestEnum.THIRD;
TestEnum[] memory arr3_copy = arr3;
var gTestEnumArray = arr3; // generic enum array
gTestEnumArray[0] = TestEnum.THIRD;
gTestEnumArray[1] = TestEnum.THIRD;
Expand Down

0 comments on commit b6297e8

Please sign in to comment.