//Build Memory Map for Array types, private void ConstructArrayMemoryMap(ArrayTypeName array, HashSet <KeyValuePair <BoogieType, BoogieType> > generatedTypes) { BoogieType boogieKeyType = BoogieType.Int; BoogieType boogieValueType = null; if (array.BaseType is ArrayTypeName subarray) { boogieValueType = BoogieType.Ref; ConstructArrayMemoryMap(subarray, generatedTypes); //construct array memory map with sub array } else if (array.BaseType is Mapping mapping) { boogieValueType = BoogieType.Ref; ConstructMappingMemoryArray(mapping, generatedTypes); // construct array memory map using mapping type } else { boogieValueType = Conversion_Utility_Tool.GetBoogieTypeFromSolidityTypeName(array.BaseType); //type cannot be resolve, request type declaration } KeyValuePair <BoogieType, BoogieType> pair = new KeyValuePair <BoogieType, BoogieType>(boogieKeyType, boogieValueType); if (generatedTypes.Contains(pair) == false) { generatedTypes.Add(pair); BuildSingleMapMemoryName(boogieKeyType, boogieValueType); } }
internal static Type ResolveTypeName(ITypeName typeName) { Type reflectionType = typeName.GetReflectionType(); if (reflectionType != null) { return(reflectionType); } GenericTypeName name = typeName as GenericTypeName; if (name != null) { Type genericType = name.GetGenericType(ResolveTypeName(name.TypeName)); Type[] typeArguments = (from arg in name.GenericArguments select ResolveTypeName(arg)).ToArray <Type>(); try { if ((genericType != null) && genericType.ContainsGenericParameters) { genericType.MakeGenericType(typeArguments); } } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); throw InterpreterError.NewInterpreterException(typeName, typeof(RuntimeException), null, "TypeNotFoundWithMessage", ParserStrings.TypeNotFoundWithMessage, new object[] { typeName.FullName, exception.Message }); } } ArrayTypeName name2 = typeName as ArrayTypeName; if (name2 != null) { ResolveTypeName(name2.ElementType); } throw InterpreterError.NewInterpreterException(typeName, typeof(RuntimeException), null, "TypeNotFound", ParserStrings.TypeNotFound, new object[] { typeName.FullName }); }
public void ShouldDerivePredefinedFromPredefined() { var actual = ArrayTypeName.From(new PredefinedTypeName("p:int"), 1); var expected = new PredefinedTypeName("p:int[]"); Assert.AreEqual(expected, actual); }
private void GenerateMemoryVariables() { HashSet <KeyValuePair <BoogieType, BoogieType> > generatedTypes = new HashSet <KeyValuePair <BoogieType, BoogieType> >(); // mappings foreach (ContractDefinition contract in context.ContractToMappingsMap.Keys) { foreach (VariableDeclaration varDecl in context.ContractToMappingsMap[contract]) { Debug.Assert(varDecl.TypeName is Mapping); Mapping mapping = varDecl.TypeName as Mapping; GenerateMemoryVariablesForMapping(mapping, generatedTypes); } } // arrays foreach (ContractDefinition contract in context.ContractToArraysMap.Keys) { foreach (VariableDeclaration varDecl in context.ContractToArraysMap[contract]) { Debug.Assert(varDecl.TypeName is ArrayTypeName); ArrayTypeName array = varDecl.TypeName as ArrayTypeName; GenerateMemoryVariablesForArray(array, generatedTypes); } } }
public void CanDeriveArrays() { var actual = Names.ArrayType(2, new TypeName("T, P")); var expected = new ArrayTypeName("T[,], P"); Assert.AreEqual(expected, actual); }
//Function to construct memory variables for ArrayMap.keys and Mapping Frame private void ConstructMemoryVariables() { HashSet <KeyValuePair <BoogieType, BoogieType> > generatedTypes = new HashSet <KeyValuePair <BoogieType, BoogieType> >(); // mappings foreach (ContractDefinition contract in context.MappingFrame.Keys) { foreach (VariableDeclaration variableDeclaration in context.MappingFrame[contract]) { //Iteratve over MappingFrame and generate keys Debug.Assert(variableDeclaration.TypeName is Mapping); Mapping mapping = variableDeclaration.TypeName as Mapping; ConstructMappingMemoryArray(mapping, generatedTypes); } } //Iterate over array maps and construct appropriate memory maps from this. foreach (ContractDefinition contract in context.constructArrayMaps.Keys) { foreach (VariableDeclaration variableDeclaration in context.constructArrayMaps[contract]) { Debug.Assert(variableDeclaration.TypeName is ArrayTypeName); ArrayTypeName array = variableDeclaration.TypeName as ArrayTypeName; ConstructArrayMemoryMap(array, generatedTypes); } } }
public void ShouldDeriveTypeParameterFromTypeParameter() { var actual = ArrayTypeName.From(new TypeParameterName("T"), 1); var expected = new TypeParameterName("T[]"); Assert.AreEqual(expected, actual); }
public void ShouldDeriveUnknownArray() { var actual = ArrayTypeName.From(new TypeName(), 1); var expected = new ArrayTypeName("?[]"); Assert.AreEqual(expected, actual); }
private void GenerateMemoryVariablesForArray(ArrayTypeName array, HashSet <KeyValuePair <BoogieType, BoogieType> > generatedTypes) { BoogieType boogieKeyType = BoogieType.Int; BoogieType boogieValType = null; if (array.BaseType is ArrayTypeName subarray) { boogieValType = BoogieType.Ref; GenerateMemoryVariablesForArray(subarray, generatedTypes); } else if (array.BaseType is Mapping mapping) { boogieValType = BoogieType.Ref; GenerateMemoryVariablesForMapping(mapping, generatedTypes); } else { boogieValType = TransUtils.GetBoogieTypeFromSolidityTypeName(array.BaseType); } KeyValuePair <BoogieType, BoogieType> pair = new KeyValuePair <BoogieType, BoogieType>(boogieKeyType, boogieValType); if (!generatedTypes.Contains(pair)) { generatedTypes.Add(pair); GenerateSingleMemoryVariable(boogieKeyType, boogieValType); } }
public void IsArrayName(string baseTypeId, string expected1DId, string expected2DId) { Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(baseTypeId)); foreach (var arrId in new[] { expected1DId, expected2DId }) { if (TypeParameterName.IsTypeParameterNameIdentifier(baseTypeId)) { Assert.IsTrue(TypeParameterName.IsTypeParameterNameIdentifier(arrId)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(arrId)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(arrId)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(arrId)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(arrId)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(arrId)); } else if (PredefinedTypeName.IsPredefinedTypeNameIdentifier(baseTypeId)) { Assert.IsTrue(PredefinedTypeName.IsPredefinedTypeNameIdentifier(arrId)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(arrId)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(arrId)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(arrId)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(arrId)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(arrId)); } else { Assert.IsTrue(ArrayTypeName.IsArrayTypeNameIdentifier(arrId)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(arrId)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(arrId)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(arrId)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(arrId)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(arrId)); } } }
public void TypeParameterParsingIsCached() { var sut = new ArrayTypeName("T[],P"); var a = sut.TypeParameters; var b = sut.TypeParameters; Assert.AreSame(a, b); }
public void ShouldRecognizeUnknownArrays() { Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier("?")); Assert.IsTrue(ArrayTypeName.IsArrayTypeNameIdentifier("?[]")); Assert.IsTrue(ArrayTypeName.IsArrayTypeNameIdentifier("?[,]")); // unknown array is nested somewhere Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier("d:[?] [?].([?[]] p)")); }
public void ShouldDerive1To2DArray(string baseTypeId, string expected1DId, string expected2DId) { var expected1D = TypeUtils.CreateTypeName(expected1DId); var expected2D = TypeUtils.CreateTypeName(expected2DId); var actual1To2D = ArrayTypeName.From(expected1D, 1); Assert.AreEqual(expected2D, actual1To2D); }
public void ShouldIdentifyValidTypeParameterNames(string typeParameter, string shortName, string boundType) { Assert.IsTrue(TypeParameterName.IsTypeParameterNameIdentifier(typeParameter)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(typeParameter)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(typeParameter)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(typeParameter)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(typeParameter)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(typeParameter)); }
public void ShouldRecognizeDelegateNames(string delegateId, string delegateTypeId) { Assert.IsTrue(DelegateTypeName.IsDelegateTypeNameIdentifier(delegateId)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(delegateId)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(delegateId)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(delegateId)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(delegateId)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(delegateId)); }
public void ShouldRecognizeIdentifier(string shortName, string fullName, string id) { Assert.IsTrue(PredefinedTypeName.IsPredefinedTypeNameIdentifier(id)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(id)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(id)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(id)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(id)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(id)); }
public void ShouldNotCrashForInvalidNames(string invalidId) { Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(invalidId)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(invalidId)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(invalidId)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(invalidId)); Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(invalidId)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(invalidId)); }
public void FromType_convert_array_type() { TypeName name = TypeName.FromType(typeof(void).MakeArrayType(2)); Assert.True(name.IsArray); ArrayTypeName array = (ArrayTypeName)name; Assert.Equal(TypeName.Void, array.ElementType); Assert.Equal(2, array.Dimensions.Count); Assert.Equal("Void[,]", array.Name); }
TypeReference ResolveTypeName(AssemblyDefinition assembly, TypeName typeName) { if (typeName is AssemblyQualifiedTypeName assemblyQualifiedTypeName) { // In this case we ignore the assembly parameter since the type name has assembly in it var assemblyFromName = _context.TryResolve(assemblyQualifiedTypeName.AssemblyName.Name); return(ResolveTypeName(assemblyFromName, assemblyQualifiedTypeName.TypeName)); } if (assembly == null || typeName == null) { return(null); } if (typeName is ConstructedGenericTypeName constructedGenericTypeName) { var genericTypeRef = ResolveTypeName(assembly, constructedGenericTypeName.GenericType); if (genericTypeRef == null) { return(null); } TypeDefinition genericType = genericTypeRef.Resolve(); var genericInstanceType = new GenericInstanceType(genericType); foreach (var arg in constructedGenericTypeName.GenericArguments) { var genericArgument = ResolveTypeName(assembly, arg); if (genericArgument == null) { return(null); } genericInstanceType.GenericArguments.Add(genericArgument); } return(genericInstanceType); } else if (typeName is HasElementTypeName elementTypeName) { var elementType = ResolveTypeName(assembly, elementTypeName.ElementTypeName); if (elementType == null) { return(null); } return(typeName switch { ArrayTypeName _ => new ArrayType(elementType), MultiDimArrayTypeName multiDimArrayTypeName => new ArrayType(elementType, multiDimArrayTypeName.Rank), ByRefTypeName _ => new ByReferenceType(elementType), PointerTypeName _ => new PointerType(elementType), _ => elementType });
public void ShouldRecognizeUnknownType() { foreach (var id in new[] { null, "", "?" }) { Assert.IsTrue(TypeUtils.IsUnknownTypeIdentifier(id)); Assert.IsFalse(TypeName.IsTypeNameIdentifier(id)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(id)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(id)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(id)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(id)); } }
/// <summary> /// Constructs a new variable name parse tree. /// </summary> /// <param name="name">The name.</param> /// <param name="arrayType">The array modifier, if any.</param> /// <param name="span">The location of the parse tree.</param> public VariableName(SimpleName name, ArrayTypeName arrayType, Span span) : base(TreeType.VariableName, span) { if (name == null) { throw new ArgumentNullException("name"); } SetParent(name); SetParent(arrayType); _Name = name; _ArrayType = arrayType; }
public void ShouldRecognizeRegularTypes(string typeId, string assemblyId, string namespaceId, string fullName, string name) { Assert.IsFalse(TypeUtils.IsUnknownTypeIdentifier(typeId)); Assert.IsFalse(ArrayTypeName.IsArrayTypeNameIdentifier(typeId)); Assert.IsFalse(DelegateTypeName.IsDelegateTypeNameIdentifier(typeId)); Assert.IsFalse(TypeParameterName.IsTypeParameterNameIdentifier(typeId)); Assert.IsFalse(PredefinedTypeName.IsPredefinedTypeNameIdentifier(typeId)); Assert.IsTrue(TypeName.IsTypeNameIdentifier(typeId)); }
protected internal override void CheckSemantics(AstHelper astHelper) { base.CheckSemantics(astHelper); ArrayTypeName.CheckSemantics(astHelper); // type must be an array: type XX = array of ... astHelper.Errors.Check(new NotAnArrayTypeError(Type, Start)); OriginalType = Type.GetElementType(); Lenght.CheckSemantics(astHelper); astHelper.Errors.Check(new TypeMismatchedError(Lenght.Type, typeof(int), Start)); }
public static IEnumerable <string> TypesSource() { ISet <string> typeIds = new HashSet <string>(); foreach (var baseTypeId in NonArrayTypeSource()) { var baseType = TypeUtils.CreateTypeName(baseTypeId); Asserts.Not(baseType.IsArray); typeIds.Add(baseTypeId); typeIds.Add(ArrayTypeName.From(baseType, 1).Identifier); typeIds.Add(ArrayTypeName.From(baseType, 2).Identifier); } return(typeIds); }
public static string HandleOldTypeNames(string input) { ITypeName typeName = TypeUtils.CreateTypeName(input); string result = input; if (typeName.IsDelegateType) { return("d:" + HandleOldMethodNames(input.Substring(2, input.Length - 2))); } if (typeName.IsArray) { ArrayTypeName name = (ArrayTypeName)typeName; return("arr(" + name.Rank + "):" + HandleOldTypeNames(name.ArrayBaseType.Identifier)); } if (typeName.HasTypeParameters) { var paras = typeName.TypeParameters; var n = typeName.Namespace.Identifier; var a = typeName.Assembly.Identifier; if (!n.Equals("")) { n += "."; } if (!a.Contains("+")) { a = ", " + a; } result = n + GetTypeNameIdentifier(typeName) + typeName.Name + "'" + paras.Count + "["; for (var i = 0; i < paras.Count; i++) { result += "[" + ((TypeParameterName)paras[i]).TypeParameterShortName + " -> " + HandleOldTypeNames(((TypeParameterName)paras[i]).TypeParameterType.Identifier) + "]" + (i < paras.Count - 1 ? "," : "") ; } result += "]" + a; } if ((typeName.IsEnumType || (typeName.IsStructType && !typeName.IsSimpleType && !typeName.IsVoidType) || typeName.IsInterfaceType) && !typeName.Namespace.Identifier.Equals("")) { result = HandleTypeIdentifier(result); } if (typeName.IsNestedType || typeName.Identifier.Contains("+")) { result = HandleNestedTypeNames(result); } return(result); }
private void GenerateMemoryVariables() { HashSet <String> generatedMaps = new HashSet <String>(); // mappings foreach (ContractDefinition contract in context.ContractToMappingsMap.Keys) { foreach (VariableDeclaration varDecl in context.ContractToMappingsMap[contract]) { Debug.Assert(varDecl.TypeName is Mapping); Mapping mapping = varDecl.TypeName as Mapping; GenerateMemoryVariablesForMapping(varDecl, mapping, generatedMaps, 0); if (context.TranslateFlags.InstrumentSums) { String sumName = mapHelper.GetSumName(varDecl); if (!generatedMaps.Contains(sumName)) { generatedMaps.Add(sumName); BoogieType sumType = new BoogieMapType(BoogieType.Ref, BoogieType.Int); context.Program.AddDeclaration(new BoogieGlobalVariable(new BoogieTypedIdent(sumName, sumType))); } } } } // arrays foreach (ContractDefinition contract in context.ContractToArraysMap.Keys) { foreach (VariableDeclaration varDecl in context.ContractToArraysMap[contract]) { Debug.Assert(varDecl.TypeName is ArrayTypeName); ArrayTypeName array = varDecl.TypeName as ArrayTypeName; GenerateMemoryVariablesForArray(varDecl, array, generatedMaps, 0); if (context.TranslateFlags.InstrumentSums) { String sumName = mapHelper.GetSumName(varDecl); if (!generatedMaps.Contains(sumName)) { generatedMaps.Add(sumName); BoogieType sumType = new BoogieMapType(BoogieType.Ref, BoogieType.Int); context.Program.AddDeclaration(new BoogieGlobalVariable(new BoogieTypedIdent(sumName, sumType))); } } } } }
/// <summary> /// The constructor for a New array expression parse tree. /// </summary> /// <param name="target">The target array type to create.</param> /// <param name="initializer">The initializer for the array.</param> /// <param name="span">The location of the parse tree.</param> public NewAggregateExpression(ArrayTypeName target, AggregateInitializer initializer, Span span) : base(TreeType.NewAggregateExpression, span) { if (target == null) { throw new ArgumentNullException("target"); } if (initializer == null) { throw new ArgumentNullException("initializer"); } SetParent(target); SetParent(initializer); _Target = target; _Initializer = initializer; }
private void GenerateDeriveArrayTest(int counter, string baseTypeId) { Asserts.Not(ArrayTypeName.IsArrayTypeNameIdentifier(baseTypeId)); var type = TypeUtils.CreateTypeName(baseTypeId); var arr1 = ArrayTypeName.From(type, 1); var arr2 = ArrayTypeName.From(type, 2); _sb.OpenTest("DeriveArrayTest_{0}".FormatEx(counter)); _sb.AppendLine("String baseId = \"{0}\";".FormatEx(baseTypeId)); _sb.AppendLine("String arr1Id = \"{0}\";".FormatEx(arr1.Identifier)); _sb.AppendLine("String arr2Id = \"{0}\";".FormatEx(arr2.Identifier)); _sb.AppendLine("ITypeName base = TypeUtils.createTypeName(baseId);"); _sb.AppendLine("ITypeName arr1 = ArrayTypeName.from(base, 1);"); _sb.AppendLine("assertTrue(arr1 instanceof {0});".FormatEx(arr1.GetType().Name)); _sb.AppendLine("assertEquals(arr1Id, arr1.getIdentifier());"); _sb.AppendLine("ITypeName arr2 = ArrayTypeName.from(base, 2);"); _sb.AppendLine("assertTrue(arr2 instanceof {0});".FormatEx(arr2.GetType().Name)); _sb.AppendLine("assertEquals(arr2Id, arr2.getIdentifier());"); _sb.CloseTest(); }
internal static TypeName FindTypeNameToComplete(ITypeName type, IScriptPosition cursor) { TypeName name = type as TypeName; if (name != null) { if ((cursor.Offset > type.Extent.StartOffset) && (cursor.Offset <= type.Extent.EndOffset)) { return(name); } return(null); } GenericTypeName name2 = type as GenericTypeName; if (name2 != null) { name = FindTypeNameToComplete(name2.TypeName, cursor); if (name != null) { return(name); } foreach (ITypeName name3 in name2.GenericArguments) { name = FindTypeNameToComplete(name3, cursor); if (name != null) { return(name); } } return(null); } ArrayTypeName name4 = type as ArrayTypeName; if (name4 == null) { return(null); } return((TypeName)(FindTypeNameToComplete(name4.ElementType, cursor) ?? null)); }
private void GenerateMemoryVariablesForArray(VariableDeclaration decl, ArrayTypeName array, HashSet <String> generatedMaps, int lvl) { BoogieType boogieKeyType = BoogieType.Int; BoogieType boogieValType = null; if (array.BaseType is ArrayTypeName subarray) { boogieValType = BoogieType.Ref; GenerateMemoryVariablesForArray(decl, subarray, generatedMaps, lvl + 1); // The last level gets initialized all at once if (context.TranslateFlags.LazyAllocNoMod) { BoogieMapType mapType = new BoogieMapType(BoogieType.Ref, new BoogieMapType(boogieKeyType, BoogieType.Bool)); context.Program.AddDeclaration(new BoogieGlobalVariable(new BoogieTypedIdent(mapHelper.GetNestedAllocName(decl, lvl), mapType))); } } else if (array.BaseType is Mapping mapping) { boogieValType = BoogieType.Ref; GenerateMemoryVariablesForMapping(decl, mapping, generatedMaps, lvl + 1); if (context.TranslateFlags.LazyAllocNoMod) { BoogieMapType mapType = new BoogieMapType(BoogieType.Ref, new BoogieMapType(boogieKeyType, BoogieType.Bool)); context.Program.AddDeclaration(new BoogieGlobalVariable(new BoogieTypedIdent(mapHelper.GetNestedAllocName(decl, lvl), mapType))); } } else { boogieValType = TransUtils.GetBoogieTypeFromSolidityTypeName(array.BaseType); } KeyValuePair <BoogieType, BoogieType> pair = new KeyValuePair <BoogieType, BoogieType>(boogieKeyType, boogieValType); GenerateSingleMemoryVariable(decl, boogieKeyType, boogieValType, generatedMaps); }