Beispiel #1
0
		public TypeVariable [] ArgumentTypes;	// types of the parameters.

		public TraitFunc(TypeVariable func, int funcPtrSize, TypeVariable tRet, TypeVariable [] argumentTypes)
		{
			FuncType = func;
			FuncPointerSize = funcPtrSize;
			ReturnType = tRet;
			ArgumentTypes = argumentTypes;
		}
 public override DataType UnifyTypeVariables(TypeVariable tA, TypeVariable tB)
 {
     var dt = Unify(tA.Class.DataType, tB.Class.DataType);
     var eq = store.MergeClasses(tA, tB);
     eq.DataType = dt;
     return eq.Representative;
 }
 public override DataType UnifyTypeVariables(TypeVariable tA, TypeVariable tB)
 {
     if (++nestedCalls > 300)        //$DEBUG
         nestedCalls.ToString();
     var dt = Unify(tA.Class.DataType, tB.Class.DataType);
     var eq = store.MergeClasses(tA, tB);
     eq.DataType = dt;
     --nestedCalls;
     return eq.Representative;
 }
Beispiel #4
0
 public void DataTypeTrait(TypeVariable type, DataType dt)
 {
     if (dt == PrimitiveType.SegmentSelector)
     {
         StructureType seg = factory.CreateStructureType(null, 0);
         seg.IsSegment = true;
         Pointer ptr = factory.CreatePointer(seg, dt.Size);
         dt = ptr;
     }
     MergeIntoDataType(dt, type);
 }
Beispiel #5
0
        public DataType MergeIntoDataType(DataType dtNew, TypeVariable tv)
        {
            if (dtNew == null)
                return tv.OriginalDataType;

            DataType dtCurrent = tv.OriginalDataType;
            if (dtCurrent != null)
            {
                dtNew = unifier.Unify(dtCurrent, dtNew);
            }
            tv.OriginalDataType = dtNew;
            return dtNew;
        }
		public void Merge1()
		{
			TypeVariable tv1 = new TypeVariable(1);
			TypeVariable tv2 = new TypeVariable(2);
            StructureType s1 = new StructureType { Fields = { { 4, PrimitiveType.Pointer32 } } };
            StructureType s2 = new StructureType { Fields = { { 4, PrimitiveType.Pointer32 } } };
			EquivalenceClass c1 = new EquivalenceClass(tv1);
			EquivalenceClass c2 = new EquivalenceClass(tv2);
			c1.DataType = s1;
			c2.DataType = s2;
			StructureMerger sm = new StructureMerger(new StructureType[] { s1, s2 }, new EquivalenceClass[] { c1, c2 } );
			sm.Merge();
			Assert.AreEqual("(struct (4 ptr32 ptr0004))", c1.DataType.ToString());
		}
        public void NotMatch()
        {
            TypeVariable t1 = new TypeVariable(1);
            TypeVariable t2 = new TypeVariable(2);
            EquivalenceClass c1 = new EquivalenceClass(t1);
            EquivalenceClass c2 = new EquivalenceClass(t2);
            c1.DataType = new StructureType{ Fields = { { 4, PrimitiveType.Word16 } } };
            c2.DataType = new StructureType { Fields = { { 20, PrimitiveType.Word32 } } };
            t1.Class = c1;
            t2.Class = c2;

            UnionType u = new UnionType(null, null);
            u.Alternatives.Add(new Pointer(c1, 4));
            u.Alternatives.Add(new Pointer(PrimitiveType.Word16, 4));

            UnionPointersStructuresMatcher upsm = new UnionPointersStructuresMatcher();
            Assert.IsFalse(upsm.Match(u));
        }
Beispiel #8
0
        public override object Exec(TypeVariable a, object arg)   // this is a likely array
        {
            TypeExpression subtitutions = a.Substitution;

            if (subtitutions != null)
            {
                DynVarOptions.Instance.AssignDynamism(subtitutions, a.IsDynamic);
                return(subtitutions.AcceptOperation(this, arg));
            }
            if (this.methodAnalyzed != null)
            {
                // * A bracket constraint is added to the method analyzed
                SquareBracketConstraint bracketConstraint = new SquareBracketConstraint(a, this.index, this.location);
                this.methodAnalyzed.AddConstraint(bracketConstraint);
                // * Also a promotion constriaint of the index to IntType
                //index.Promotion(IntType.Instance, ArrayOperator.Indexer, methodAnalyzed, fileName, line, column);
                return(bracketConstraint.ReturnType);
            }
            // We are at this point the operation is invalid, report the error.?
            return(ReportError(a));
        }
        /// <summary>
        /// If there are Substitution in the first operand, it return the result of aplying the operation using the substitution
        /// as first operand. It is not the case, a constraint is added and if showErrorMessages is true an error is raise.
        /// </summary>
        /// <param name="firstOperand">A TypeVariable</param>
        /// <sumary>The type expression resulting of applying the operand and second operator.
        /// If somethis is worng and showMessages is true an error is raise.</sumary>
        public override object Exec(TypeVariable firstOperand, object arg)
        {
            if (firstOperand.Substitution != null)
            {
                DynVarOptions.Instance.AssignDynamism(firstOperand.Substitution, firstOperand.IsDynamic);
                return(firstOperand.Substitution.AcceptOperation(this, arg));
            }
            if (methodAnalyzed != null)
            {
                // * A constraint is added to the method analyzed
                ArithmeticConstraint constraint = new ArithmeticConstraint(firstOperand, secondOperand, binaryOperator, location);
                methodAnalyzed.AddConstraint(constraint);
                return(constraint.ReturnType);
            }
            if (showErrorMessage)
            {
                ErrorManager.Instance.NotifyError(new OperationNotAllowedError(binaryOperator.ToString(), firstOperand.FullName, secondOperand.FullName, location));
            }

            return(null);
        }
Beispiel #10
0
        public void ArrayOfUserDefinedStructures()
        {
            var s = new StructureType(null, 0, true);

            s.Fields.Add(0, PrimitiveType.Word32);
            s.Fields.Add(4, PrimitiveType.Real64);

            ArrayType a = new ArrayType(s, 0);

            TypeVariable tv = store.CreateTypeVariable(factory);

            tv.Class.DataType = a;
            Assert.AreEqual(1, store.UsedEquivalenceClasses.Count);

            DataType dt = tv.Class.DataType.Accept(nct);

            Assert.AreEqual(1, store.UsedEquivalenceClasses.Count);
            Assert.AreEqual(
                "(arr (struct (0 word32 dw0000) (4 real64 r0004)))",
                store.UsedEquivalenceClasses[0].DataType.ToString());
        }
Beispiel #11
0
        public void TestUninstantiated()
        {
            var var0 = new TypeVariable();
            var var1 = new TypeVariable();

            var rhs = new FunType(
                new List <DataType>()
            {
                BoolType.Instance,
                new ArrayType(new ArrayType(var1)),
                BoolType.Instance
            },
                IntType.Instance);

            var input = new Dictionary <TypeVariable, IHerbrandObject>()
            {
                { var0, rhs }
            };

            CheckThrows(input);
        }
Beispiel #12
0
        public void ArrayOfUserDefinedUnions()
        {
            var ut = new UnionType(null, null, true);

            ut.AddAlternative(PrimitiveType.Word32);
            ut.AddAlternative(PrimitiveType.Real64);

            ArrayType a = new ArrayType(ut, 0);

            TypeVariable tv = store.CreateTypeVariable(factory);

            tv.Class.DataType = a;
            Assert.AreEqual(1, store.UsedEquivalenceClasses.Count);

            DataType dt = tv.Class.DataType.Accept(nct);

            Assert.AreEqual(1, store.UsedEquivalenceClasses.Count);
            Assert.AreEqual(
                "(arr (union (real64 u1) (word32 u0)))",
                store.UsedEquivalenceClasses[0].DataType.ToString());
        }
        public void Setup()
        {
            mr = new MockRepository();
            arch = mr.Stub<IProcessorArchitecture>();
            arch.Stub(a => a.CreateImageReader(null, 0u)).IgnoreArguments().Do(new Func<LoadedImage, ulong, ImageReader>((i, o) => i.CreateLeReader(o)));
            arch.Replay();
            globalStruct = new StructureType
            {
            };
            globals_t = new TypeVariable("globals_t", 1) { DataType = globalStruct };
            globals = new Identifier("globals", PrimitiveType.Pointer32, null);

            eqLink = new EquivalenceClass(new TypeVariable(2));
            StructureType str = new StructureType
            {
                Fields = {
                    { 0, new Pointer(eqLink, 4)},
                    { 4, PrimitiveType.Int32 }
                }
            };
            eqLink.DataType = str;
        }
Beispiel #14
0
 public void AddTypeBinder(TypeVariable td)
 {
     Contract.Requires(td != null);
     if (CheckBvNameClashes(td, td.Name))
     {
         return;
     }
     if (types.ContainsKey(td.Name))
     {
         Error(td, "name is already reserved for type constructor: {0}", td.Name);
         return;
     }
     for (int i = 0; i < typeBinders.Count; i++)
     {
         if (typeBinders[i].Name == td.Name)
         {
             Error(td, "more than one declaration of type variable: {0}", td.Name);
             return;
         }
     }
     typeBinders.Add(td);
 }
Beispiel #15
0
        public HashSet <TypeExpression> getTypesFromUnion(UnionType union)
        {
            HashSet <TypeExpression> types = new HashSet <TypeExpression>();

            foreach (TypeExpression type in union.TypeSet)
            {
                TypeVariable   varType          = type as TypeVariable;
                TypeExpression substitutionType = varType != null ? varType.Substitution : null;
                if (substitutionType == null)
                {
                    continue;
                }

                if (substitutionType is UnionType)
                {
                    types.UnionWith(getTypesFromUnion(substitutionType as UnionType));
                    continue;
                }
                types.Add(substitutionType);
            }
            return(types);
        }
Beispiel #16
0
        public void TestBacktrack()
        {
            var xTypeVar = new TypeVariable();
            var yTypeVar = new TypeVariable();

            var intVar  = IntType.Instance;
            var boolVar = BoolType.Instance;

            var clauses = new List <Clause>
            {
                new Clause
                {
                    Alternatives = new List <List <(IHerbrandObject, IHerbrandObject)> >
                    {
                        new List <(IHerbrandObject, IHerbrandObject)> {
                            (xTypeVar, intVar)
                        },
                        new List <(IHerbrandObject, IHerbrandObject)> {
                            (xTypeVar, boolVar)
                        }
                    }
                },
Beispiel #17
0
        public void TestNested()
        {
            var var0 = new TypeVariable();
            var var1 = new TypeVariable();
            var var2 = new TypeVariable();

            var input = new Dictionary <TypeVariable, IHerbrandObject>()
            {
                { var0, new FunType(new List <DataType>(), new FunType(new List <DataType>(), var1)) },
                { var1, new ArrayType(new ArrayType(new ArrayType(var2))) },
                { var2, IntType.Instance }
            };

            var output2 = IntType.Instance;
            var output1 = new ArrayType(new ArrayType(new ArrayType(output2)));
            var output0 = new FunType(new List <DataType>(), new FunType(new List <DataType>(), output1));

            var output = new Dictionary <TypeVariable, IHerbrandObject>()
            {
                { var0, output0 }, { var1, output1 }, { var2, output2 }
            };

            CheckOutput(input, output);
        }
Beispiel #18
0
        public Expression RewriteArrayAccess(TypeVariable typeVariable, Expression arr, Expression idx)
        {
            var ter = new TypedExpressionRewriter(prog);

            basePointer = null;
            dtResult    = new Pointer(typeVariable.DataType, platform.PointerType.Size);
            var dtElement     = Dereference(arr.TypeVariable.DataType);
            var dtElementOrig = Dereference(arr.TypeVariable.OriginalDataType);

            arr = arr.Accept(ter);
            idx = idx.Accept(ter);
            idx = RescaleIndex(idx, dtElement);
            var ceb = new ComplexExpressionBuilder(
                dtResult,
                dtElement,
                dtElementOrig,
                basePointer,
                new ArrayAccess(dtElement, arr, idx),
                null,
                0);

            ceb.Dereferenced = true;
            return(ceb.BuildComplex());
        }
        /// <summary>
        /// Returns a value that indicates a promotion level.
        /// </summary>
        /// <param name="firstOperand">WriteType to promotion.</param>
        /// <returns>Returns a promotion value.</returns>
        public override object Exec(BoolType firstOperand, object arg)
        {
            // * Bool type and type variable
            if (TypeExpression.As <BoolType>(this.secondOperand) != null)
            {
                return(0);
            }

            // * WriteType variable
            TypeVariable typeVariable = this.secondOperand as TypeVariable;

            if (typeVariable != null && typeVariable.Substitution == null)
            {
                // * A free variable is complete promotion
                return(0);
            }

            // * Union type
            UnionType unionType = TypeExpression.As <UnionType>(this.secondOperand);

            if (unionType != null)
            {
                return(unionType.SuperType(firstOperand));
            }

            // * Field type and bounded type variable
            FieldType fieldType = TypeExpression.As <FieldType>(this.secondOperand);

            if (fieldType != null)
            {
                return(firstOperand.AcceptOperation(new PromotionLevelOperation(fieldType.FieldTypeExpression), arg));
            }

            // * Use the BCL object oriented approach
            return(firstOperand.AsClassType().AcceptOperation(this, arg));
        }
        public override Type VisitMapTypeProxy(MapTypeProxy node)
        {
            //Contract.Requires(node != null);
            Contract.Ensures(Contract.Result <Type>() != null);
            if (node.ProxyFor != null)
            {
                return(base.VisitMapTypeProxy(node));
            }

            List <TypeVariable> /*!*/
            typeParams = new List <TypeVariable>();
            List <Type>         /*!*/
            arguments = new List <Type>();

            for (int i = 0; i < node.Arity; ++i)
            {
                TypeVariable /*!*/
                    param = new TypeVariable(Token.NoToken, "arg" + i);
                Contract.Assert(param != null);
                typeParams.Add(param);
                arguments.Add(param);
            }

            TypeVariable /*!*/
                result = new TypeVariable(Token.NoToken, "res");

            Contract.Assert(result != null);
            typeParams.Add(result);

            Type /*!*/
                instantiation = new MapType(Token.NoToken, typeParams, arguments, result);

            Contract.Assert(instantiation != null);

            return(Instantiate(node, instantiation));
        }
 public override Type VisitTypeVariable(TypeVariable node)
 {
     //instantiate T@U with the Boogie values means we do not need to transform the value
     ReturnResult(v => v);
     return(node);
 }
Beispiel #22
0
 public TraitPointer(TypeVariable pointee)
 {
     this.pointee = pointee;
 }
Beispiel #23
0
        public DataType VisitTypeReference(TypeReference typeref)
        {
            var tv = new TypeVariable(typeref.TypeName.Name, 0);

            return(tv);
        }
Beispiel #24
0
 public IEnumerable <WorkItem> VisitTypeVariable(TypeVariable tv)
 {
     throw new NotImplementedException();
 }
Beispiel #25
0
 public CodeFormatter VisitTypeVariable(TypeVariable tv)
 {
     throw new NotImplementedException();
 }
Beispiel #26
0
        public void WriteEntry(TypeVariable tv, Formatter writer)
        {
            writer.Write(tv.Name);
            writer.Write(":");
            WriteExpressionOf(tv, writer);
            writer.WriteLine();

            writer.Write("  Class: ");
            writer.WriteLine(tv.Class.Name);

            writer.Write("  DataType: ");
            writer.WriteLine(tv.DataType);

            writer.Write("  OrigDataType: ");
            writer.WriteLine(tv.OriginalDataType);
        }
Beispiel #27
0
 public Expression VisitTypeVariable(TypeVariable tv)
 {
     throw new NotImplementedException();
 }
Beispiel #28
0
 public EquivalenceClass MergeClasses(TypeVariable tv1, TypeVariable tv2)
 {
     EquivalenceClass class1 = tv1.Class;
     EquivalenceClass class2 = tv2.Class;
     usedClasses.Remove(class1.Number);
     usedClasses.Remove(class2.Number);
     EquivalenceClass merged = EquivalenceClass.Merge(class1, class2);
     usedClasses.Add(merged.Number, merged);
     tv1.Class = merged;
     tv2.Class = merged;
     return merged;
 }
Beispiel #29
0
		public DataType FunctionTrait(Expression function, int funcPtrSize, TypeVariable ret, params TypeVariable [] actuals)
		{
            Identifier[] adt = actuals.Select(a => new Identifier("", a, null)).ToArray();
			var fn = factory.CreateFunctionType(new Identifier("", ret, null), adt);
			var pfn = factory.CreatePointer(fn, funcPtrSize);
			return MergeIntoDataType(function, pfn);
		}
Beispiel #30
0
 public void VisitTypeVariable(TypeVariable tv)
 {
     throw new NotImplementedException();
 }
Beispiel #31
0
 private void AddDebugSource(TypeVariable tv, Expression e)
 {
     if (e != null)
         tvSources.Add(tv, e);
 }
Beispiel #32
0
 public void SetTypeVariableExpression(TypeVariable typeVariable, BinaryExpression binExp)
 {
     tvSources[typeVariable] = binExp;
 }
Beispiel #33
0
 private void WriteEntry(TypeVariable tv, DataType dt, Formatter writer)
 {
     writer.Write("{0}: ", tv);
     if (dt != null)
     {
         dt.Accept(new TypeGraphWriter(writer));
         WriteExpressionOf(tv, writer);
     }
     writer.WriteLine();
 }
Beispiel #34
0
 public int VisitTypeVariable(TypeVariable tv)
 {
     throw new NotImplementedException();
 }
Beispiel #35
0
 public void AddTypeBinder(TypeVariable td)
 {
     Contract.Requires(td != null);
       if (CheckBvNameClashes(td, td.Name)) {
     return;
       }
       if (types.ContainsKey(td.Name)) {
     Error(td, "name is already reserved for type constructor: {0}", td.Name);
     return;
       }
       for (int i = 0; i < typeBinders.Count; i++) {
     if (typeBinders[i].Name == td.Name) {
       Error(td, "more than one declaration of type variable: {0}", td.Name);
       return;
     }
       }
       typeBinders.Add(td);
 }
Beispiel #36
0
        public void ArrayTrait(TypeVariable tArray, int elementSize, int length)
		{
			DataType elem = factory.CreateStructureType(null, elementSize);
			Pointer ptr = factory.CreatePointer(factory.CreateArrayType(elem, length), 0);
			MergeIntoDataType(ptr, tArray);
		}
        public override object Exec(InterfaceType firstOperand, object arg)
        {
            int aux, less = -1;

            // * Equivalent types
            if ((bool)firstOperand.AcceptOperation(new EquivalentOperation(this.secondOperand), arg))
            {
                less = 0;
            }

            // * WriteType variable
            TypeVariable typeVariable = this.secondOperand as TypeVariable;

            if (typeVariable != null)
            {
                if (typeVariable.Substitution != null)
                {
                    // * If the variable is bounded, the promotion is the one of its substitution
                    return(firstOperand.AcceptOperation(new PromotionLevelOperation(typeVariable.EquivalenceClass.Substitution), arg));
                }
                // * A free variable is complete promotion
                return(0);
            }

            // * Field type and bounded type variable
            FieldType fieldType = TypeExpression.As <FieldType>(this.secondOperand);

            if (fieldType != null)
            {
                return(firstOperand.AcceptOperation(new PromotionLevelOperation(fieldType.FieldTypeExpression), arg));
            }

            // * Interface List
            if (firstOperand.InterfaceList.Count != 0)
            {
                for (int i = 0; i < firstOperand.InterfaceList.Count; i++)
                {
                    if ((bool)firstOperand.InterfaceList[i].AcceptOperation(new EquivalentOperation(this.secondOperand), arg))
                    {
                        if ((less > 1) || (less == -1))
                        {
                            less = 1;
                        }
                    }
                    else
                    {
                        aux = (int)firstOperand.InterfaceList[i].AcceptOperation(this, arg);
                        if (aux != -1)
                        {
                            if (less > aux + 1 || less == -1)
                            {
                                less = aux + 1;
                            }
                        }
                    }
                }
            }
            if (less != -1)
            {
                return(less);
            }

            // * Union type
            UnionType unionType = TypeExpression.As <UnionType>(this.secondOperand);

            if (unionType != null)
            {
                return(unionType.SuperType(firstOperand));
            }

            // * No promotion
            return(-1);
        }
 public void ArrayTrait(TypeVariable tArray, int elementSize, int length)
 {
     Traits.AddTrait(tArray, new TraitArray(elementSize, length));
 }
Beispiel #39
0
		public override DataType VisitTypeVariable(TypeVariable tv)
		{
			throw new TypeInferenceException("Type variables mustn't occur at this stage.");
		}
Beispiel #40
0
 public SerializedType VisitTypeVariable(TypeVariable tv)
 {
     throw new NotImplementedException();
 }
Beispiel #41
0
 private DataType PointerTo(TypeVariable tv)
 {
     return(new Pointer(tv, platform.PointerType.Size));
 }
Beispiel #42
0
 public Expression ExpressionOf(TypeVariable tv)
 {
     Expression e;
     if (tvSources.TryGetValue(tv, out e))
         return e;
     else
         return null;
 }
Beispiel #43
0
 public override DataType VisitTypeVariable(TypeVariable tv)
 {
     throw new TypeInferenceException("Type variables mustn't occur at this stage.");
 }
		public override DataType VisitTypeVariable(TypeVariable tv)
		{
			return tv.Class;
		}
Beispiel #45
0
 public TraitMemArray(TypeVariable basePtr, int structPtrSize, int offset, int elementSize, int length, TypeVariable tAccess)
 {
     this.BasePointer = basePtr;
     this.Offset      = offset;
     this.ElementSize = elementSize;
     this.Length      = length;
     this.AccessType  = tAccess;
     if (tAccess == null)
     {
         throw new ArgumentNullException("tAccess");
     }
 }
Beispiel #46
0
 public override Type VisitTypeVariable(TypeVariable node)
 {
     Contract.Ensures(Contract.Result<Type>() == node);
     return this.VisitType(node);
 }
Beispiel #47
0
 public TraitEqual(TypeVariable typeOther)
 {
     this.TypeOther = typeOther;
 }
Beispiel #48
0
 public virtual Type VisitTypeVariable(TypeVariable node) {
   Contract.Requires(node != null);
   Contract.Ensures(Contract.Result<Type>() != null);
   return this.VisitType(node);
 }
Beispiel #49
0
//        public virtual DataType         visitType                (DataType        t       ) { return t; }
        public virtual TypeVariable visitTypeVariable(TypeVariable t)
        {
            return(t);
        }
        private VCExpr BestTypeVarExtractor(TypeVariable/*!*/ var, List<Type/*!*/>/*!*/ types,
            List<VCExprVar/*!*/>/*!*/ concreteTypeSources)
        {
            Contract.Requires(var != null);
              Contract.Requires(cce.NonNullElements(types));
              Contract.Requires(cce.NonNullElements(concreteTypeSources));
              List<VCExpr/*!*/> allExtractors = TypeVarExtractors(var, types, concreteTypeSources);
              Contract.Assert(cce.NonNullElements(allExtractors));
              if (allExtractors.Count == 0)
            return null;

              VCExpr bestExtractor = allExtractors[0];
              int bestExtractorSize = SizeComputingVisitor.ComputeSize(bestExtractor);
              for (int i = 1; i < allExtractors.Count; ++i) {
            int newSize = SizeComputingVisitor.ComputeSize(allExtractors[i]);
            if (newSize < bestExtractorSize) {
              bestExtractor = allExtractors[i];
              bestExtractorSize = newSize;
            }
              }

              return bestExtractor;
        }
Beispiel #51
0
 public DataType VisitTypeVariable(TypeVariable tv)
 {
     return(tv);
 }
        /*!*/
        private List<VCExpr/*!*/> TypeVarExtractors(TypeVariable/*!*/ var, List<Type/*!*/>/*!*/ types,
            List<VCExprVar/*!*/>/*!*/ concreteTypeSources)
        {
            Contract.Requires(var != null);
              Contract.Requires(cce.NonNullElements(types));
              Contract.Requires(cce.NonNullElements(concreteTypeSources));
              Contract.Requires((types.Count == concreteTypeSources.Count));
              Contract.Ensures(cce.NonNullElements(Contract.Result<List<VCExpr>>()));
              List<VCExpr/*!*/>/*!*/ res = new List<VCExpr/*!*/>();
              for (int i = 0; i < types.Count; ++i)
            TypeVarExtractors(var, types[i], TypeOf(concreteTypeSources[i]), res);

              return res;
        }
Beispiel #53
0
    ////////////////////////////////////////////////////////////////////////////

    // Generate the normal axioms for a partial order relation
    public void Setup() {
      TypeVariable alpha = new TypeVariable(Token.NoToken, "alpha");
      Contract.Assert(alpha != null);
      List<TypeVariable> typeParams = new List<TypeVariable>();
      typeParams.Add(alpha);

      List<VCTrigger> triggers = new List<VCTrigger>();

      VCExprVar x = Gen.Variable("x", alpha);
      Contract.Assert(x != null);
      VCExprVar y = Gen.Variable("y", alpha);
      Contract.Assert(y != null);
      VCExprVar z = Gen.Variable("z", alpha);
      Contract.Assert(z != null);

      List<VCExprVar> boundVars = new List<VCExprVar>();

      // reflexivity
      boundVars.Add(x);
      AddAxiom(Gen.Forall(typeParams, boundVars, triggers,
                          new VCQuantifierInfos("bg:subtype-refl", -1, false, null),
                          Gen.AtMost(x, x)));

      // transitivity
      boundVars = new List<VCExprVar>();
      boundVars.Add(x);
      boundVars.Add(y);
      boundVars.Add(z);
      triggers = new List<VCTrigger>();
      triggers.Add(Gen.Trigger(true, Gen.AtMost(x, y), Gen.AtMost(y, z)));
      VCExpr body = Gen.Implies(Gen.And(Gen.AtMost(x, y), Gen.AtMost(y, z)),
                                 Gen.AtMost(x, z));
      Contract.Assert(body != null);
      AddAxiom(Gen.Forall(typeParams, boundVars, triggers,
                          new VCQuantifierInfos("bg:subtype-trans", -1, false, null),
                          body));

      // anti-symmetry
      boundVars = new List<VCExprVar>();
      boundVars.Add(x);
      boundVars.Add(y);
      triggers = new List<VCTrigger>();
      triggers.Add(Gen.Trigger(true, Gen.AtMost(x, y), Gen.AtMost(y, x)));
      body = Gen.Implies(Gen.And(Gen.AtMost(x, y), Gen.AtMost(y, x)),
                         Gen.Eq(x, y));
      AddAxiom(Gen.Forall(typeParams, boundVars, triggers,
                          new VCQuantifierInfos("bg:subtype-antisymm", -1, false, null),
                          body));
    }
 private void TypeVarExtractors(TypeVariable var, Type completeType, VCExpr innerTerm, List<VCExpr/*!*/>/*!*/ extractors)
 {
     Contract.Requires(innerTerm != null);
       Contract.Requires(completeType != null);
       Contract.Requires(var != null);
       Contract.Requires(cce.NonNullElements(extractors));
       if (completeType.IsVariable) {
     if (var.Equals(completeType)) {
       extractors.Add(innerTerm);
     }  // else nothing
       } else if (completeType.IsBasic) {
     // nothing
       } else if (completeType.IsCtor) {
     CtorType/*!*/ ctorType = completeType.AsCtor;
     if (ctorType.Arguments.Count > 0) {
       // otherwise there are no chances of extracting any
       // instantiations from this type
       TypeCtorRepr repr = GetTypeCtorReprStruct(ctorType.Decl);
       for (int i = 0; i < ctorType.Arguments.Count; ++i) {
     VCExpr/*!*/ newInnerTerm = Gen.Function(repr.Dtors[i], innerTerm);
     Contract.Assert(newInnerTerm != null);
     TypeVarExtractors(var, ctorType.Arguments[i], newInnerTerm, extractors);
       }
     }
       } else if (completeType.IsMap) {
     TypeVarExtractors(var, MapTypeAbstracter.AbstractMapType(completeType.AsMap),
                   innerTerm, extractors);
       } else {
     System.Diagnostics.Debug.Fail("Don't know how to handle this type: " + completeType);
       }
 }
Beispiel #55
0
        /*
         * We need to handle the case when seeing
         * 1) ptr(mem(a)) = ptr(mem(a)) + C and
         * 2) ptr(mem(a)) = ptr(mem(b)) + C expressions.
         * In the first instance, we may be seeing the increment of an array pointer, where the stride is C. However
         * if mem(a) has a size > C, then this is actually an expression of the type:
         * a = &a->fC, where C is the offset of the field fC. This is equivalent to case 2.
         *
         * If we see t = x and later t = x + 4, then
         * [[t]] = [[x]] and [[t]] = [[x + 4]]. If [[t]] is a ptr(mem(Q)),
         * then we have a problem!
         *
         * This analysis is probably best done after TraitCollection, since by then we have discovered max sizes of mems.
         */
        public DataType VisitBinaryExpression(BinaryExpression binExp)
        {
            binExp.Left.Accept(this);
            var ivLeft = ivCur;

            binExp.Right.Accept(this);

            ivCur = null;
            if (ivLeft != null)
            {
                if (binExp.Operator == Operator.SMul || binExp.Operator == Operator.UMul || binExp.Operator == Operator.IMul || binExp.Operator == Operator.Shl)
                {
                    ivCur = MergeInductionVariableConstant(ivLeft, binExp.Operator, binExp.Right as Constant);
                }
            }

            TypeVariable tvExp = binExp.TypeVariable;

            //$BUGBUG: This needs to be redone because the domain of the operation is now in the OPERATOR, not the operands.
            if (binExp.Operator == Operator.IAdd ||
                binExp.Operator == Operator.ISub ||
                binExp.Operator == Operator.And ||
                binExp.Operator == Operator.Or ||
                binExp.Operator == Operator.Xor)
            {
                return(handler.DataTypeTrait(binExp, binExp.DataType));
            }
            else if (binExp.Operator == Operator.SMul ||
                     binExp.Operator == Operator.SDiv)
            {
                handler.DataTypeTrait(binExp, MakeNonPointer(binExp.DataType));
                var dt = handler.DataTypeTrait(binExp, binExp.DataType);
                handler.DataTypeTrait(binExp.Left, PrimitiveType.Create(DomainOf(binExp.DataType), binExp.Left.DataType.BitSize));
                handler.DataTypeTrait(binExp.Right, PrimitiveType.Create(DomainOf(binExp.DataType), binExp.Right.DataType.BitSize));
                return(dt);
            }
            else if (binExp.Operator == Operator.UMul ||
                     binExp.Operator == Operator.UDiv ||
                     binExp.Operator == Operator.Shr)
            {
                handler.DataTypeTrait(binExp, MakeNonPointer(binExp.DataType));
                var dt = handler.DataTypeTrait(binExp, MakeUnsigned(binExp.DataType));
                handler.DataTypeTrait(binExp.Left, MakeUnsigned(binExp.Left.DataType));
                handler.DataTypeTrait(binExp.Right, MakeUnsigned(binExp.Right.DataType));
                return(dt);
            }
            else if (binExp.Operator == Operator.IMul)
            {
                handler.DataTypeTrait(binExp.Left, MakeIntegral(binExp.Left.DataType));
                handler.DataTypeTrait(binExp.Right, MakeIntegral(binExp.Right.DataType));
                return(handler.DataTypeTrait(binExp, MakeIntegral(binExp.DataType)));
            }
            else if (binExp.Operator == Operator.Sar)
            {
                var dt = handler.DataTypeTrait(binExp, MakeSigned(binExp.DataType));
                handler.DataTypeTrait(binExp.Left, MakeSigned(binExp.Left.DataType));
                handler.DataTypeTrait(binExp.Right, MakeUnsigned(binExp.Right.DataType));
                return(dt);
            }
            else if (binExp.Operator == Operator.Shl)
            {
                return(handler.DataTypeTrait(binExp, MakeIntegral(binExp.DataType)));
            }
            else if (binExp.Operator == Operator.IMod)
            {
                var dt = handler.DataTypeTrait(binExp, binExp.DataType);
                handler.DataTypeTrait(binExp.Left, binExp.Left.DataType);
                handler.DataTypeTrait(binExp.Right, binExp.Right.DataType);
                return(dt);
            }
            else if (binExp.Operator == Operator.Eq ||
                     binExp.Operator == Operator.Ne)
            {
                handler.EqualTrait(binExp.Left, binExp.Right);
                return(handler.DataTypeTrait(binExp, PrimitiveType.Bool));
            }
            else if (binExp.Operator == Operator.Ge ||
                     binExp.Operator == Operator.Gt ||
                     binExp.Operator == Operator.Le ||
                     binExp.Operator == Operator.Lt)
            {
                handler.EqualTrait(binExp.Left, binExp.Right);
                var dt = handler.DataTypeTrait(binExp, PrimitiveType.Bool);
                handler.DataTypeTrait(binExp.Left, MakeSigned(binExp.Left.DataType));
                handler.DataTypeTrait(binExp.Right, MakeSigned(binExp.Right.DataType));
                return(dt);
            }
            else if (binExp.Operator is RealConditionalOperator)
            {
                handler.EqualTrait(binExp.Left, binExp.Right);
                var dt = handler.DataTypeTrait(binExp, PrimitiveType.Bool);
                handler.DataTypeTrait(binExp.Left, PrimitiveType.Create(Domain.Real, binExp.Left.DataType.BitSize));
                handler.DataTypeTrait(binExp.Right, PrimitiveType.Create(Domain.Real, binExp.Right.DataType.BitSize));
                return(dt);
            }
            else if (binExp.Operator == Operator.Uge ||
                     binExp.Operator == Operator.Ugt ||
                     binExp.Operator == Operator.Ule ||
                     binExp.Operator == Operator.Ult)
            {
                handler.EqualTrait(binExp.Left, binExp.Right);
                var dt = handler.DataTypeTrait(binExp, PrimitiveType.Bool);
                handler.DataTypeTrait(binExp.Left, MakeNotSigned(binExp.Left.DataType));
                handler.DataTypeTrait(binExp.Right, MakeNotSigned(binExp.Right.DataType));
                return(dt);
            }
            else if (binExp.Operator == Operator.FAdd ||
                     binExp.Operator == Operator.FSub ||
                     binExp.Operator == Operator.FMul ||
                     binExp.Operator == Operator.FDiv)
            {
                var dt = PrimitiveType.Create(Domain.Real, binExp.DataType.BitSize);
                handler.DataTypeTrait(binExp, dt);
                handler.DataTypeTrait(binExp.Left, dt);
                handler.DataTypeTrait(binExp.Right, dt);
                return(dt);
            }
            else if (binExp.Operator == Operator.Cand ||
                     binExp.Operator == Operator.Cor)
            {
                var dt = PrimitiveType.Bool;
                handler.DataTypeTrait(binExp, dt);
                handler.DataTypeTrait(binExp.Left, dt);
                handler.DataTypeTrait(binExp.Right, dt);
                return(dt);
            }
            throw new NotImplementedException("NYI: " + binExp.Operator + " in " + binExp);
        }
Beispiel #56
0
 public override Microsoft.Boogie.Type VisitTypeVariable(TypeVariable node)
 {
     add(node);
     return(base.VisitTypeVariable(node));
 }
 public DataType FunctionTrait(Expression function, int funcPtrSize, TypeVariable ret, params TypeVariable[] actuals)
 {
     return Traits.AddTrait(function.TypeVariable, new TraitFunc(function.TypeVariable, funcPtrSize, ret, actuals));
 }
Beispiel #58
0
 public void WriteExpressionOf(TypeVariable tvMember, Formatter writer)
 {
     Expression e;
     if (tvSources.TryGetValue(tvMember, out e) && e != null)
     {
         writer.Write(" (in {0}", e);
         if (e.DataType != null)
         {
             writer.Write(" : ");
             writer.Write(e.DataType);
         }
         writer.Write(")");
     }
 }
 public TypeDependencyVisitor(Graph <TypeVariable> typeVariableDependencyGraph,
                              HashSet <Tuple <TypeVariable, TypeVariable> > strongDependencyEdges, TypeVariable formal)
 {
     this.typeVariableDependencyGraph = typeVariableDependencyGraph;
     this.strongDependencyEdges       = strongDependencyEdges;
     this.formal = formal;
     this.insideContructedType = 0;
 }
Beispiel #60
0
		public void Add(TypeVariable var)
		{
			InnerList[var] = var;
		}