Example #1
0
        public override bool MakeHeap(Sink sink, out Heap heap, out Bpl.Program /*?*/ program)
        {
            heap      = this;
            program   = null;
            this.sink = sink;
            string prelude = this.InitialPreludeText + this.CommonText;
            var    b       = RepresentationFor.ParsePrelude(prelude, this, out program);

            if (b)
            {
                this.FieldType = new Bpl.CtorType(this.FieldTypeDecl.tok, this.FieldTypeDecl, new List <Bpl.Type>());
                this.RefType   = new Bpl.CtorType(this.RefTypeDecl.tok, this.RefTypeDecl, new List <Bpl.Type>());
                this.RealType  = new Bpl.CtorType(this.RealTypeDecl.tok, this.RealTypeDecl, new List <Bpl.Type>());
            }
            return(b);
        }
Example #2
0
    internal static bool ParsePrelude(string initialPreludeText, object instance, out Bpl.Program/*?*/ prelude) {

      prelude = null;

      var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
      var type = instance.GetType();
      FieldInfo/*?*/[] fields = type.GetFields(flags);
      RepresentationFor[] rfs = new RepresentationFor[fields.Length];
      for (int i = 0; i < fields.Length; i++) {
        var field = fields[i];
        object[] cas = field.GetCustomAttributes(typeof(RepresentationFor), false);
        if (cas == null || cas.Length == 0) { // only look at fields that have the attribute
          fields[i] = null;
        }
        else {
          foreach (var a in cas) { // should be exactly one
            RepresentationFor rf = a as RepresentationFor;
            if (rf != null) {
              rfs[i] = rf;
              break;
            }
          }
        }
      }

      #region Gather all of the Boogie declarations from the fields of this class
      var preludeText = new StringBuilder(initialPreludeText);
      for (int i = 0; i < fields.Length; i++) {
        var field = fields[i];
        if (field == null) continue;
        preludeText.AppendLine(rfs[i].declaration);
      }
      #endregion

      #region Parse the declarations
      int errorCount = Bpl.Parser.Parse(preludeText.ToString(), "foo", out prelude);
      if (prelude == null || errorCount > 0) {
        prelude = null;
        return false;
      }
      #endregion

      #region Use the compiled program to get the ASTs
      for (int i = 0; i < fields.Length; i++) {
        var field = fields[i];
        if (field == null) continue;
        if (!rfs[i].required) continue;
        var val = prelude.TopLevelDeclarations.First(d => { Bpl.NamedDeclaration nd = d as Bpl.NamedDeclaration; return nd != null && nd.Name.Equals(rfs[i].name); });
        field.SetValue(instance, val);
      }
      #endregion

      #region Check that every field in this class has been set
      for (int i = 0; i < fields.Length; i++) {
        var field = fields[i];
        if (field == null) continue;
        if (!rfs[i].required) continue;
        if (field.GetValue(instance) == null) {
          return false;
        }
      }
      #endregion Check that every field in this class has been set

      return true;
    }
Example #3
0
        internal static bool ParsePrelude(string initialPreludeText, object instance, out Bpl.Program /*?*/ prelude)
        {
            prelude = null;

            var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
            var type  = instance.GetType();

            FieldInfo /*?*/[]   fields = type.GetFields(flags);
            RepresentationFor[] rfs    = new RepresentationFor[fields.Length];
            for (int i = 0; i < fields.Length; i++)
            {
                var      field = fields[i];
                object[] cas   = field.GetCustomAttributes(typeof(RepresentationFor), false);
                if (cas == null || cas.Length == 0) // only look at fields that have the attribute
                {
                    fields[i] = null;
                }
                else
                {
                    foreach (var a in cas) // should be exactly one
                    {
                        RepresentationFor rf = a as RepresentationFor;
                        if (rf != null)
                        {
                            rfs[i] = rf;
                            break;
                        }
                    }
                }
            }

            #region Gather all of the Boogie declarations from the fields of this class
            var preludeText = new StringBuilder(initialPreludeText);
            for (int i = 0; i < fields.Length; i++)
            {
                var field = fields[i];
                if (field == null)
                {
                    continue;
                }
                preludeText.AppendLine(rfs[i].declaration);
            }
            #endregion

            #region Parse the declarations
            int errorCount = Bpl.Parser.Parse(preludeText.ToString(), "foo", out prelude);
            if (prelude == null || errorCount > 0)
            {
                prelude = null;
                return(false);
            }
            #endregion

            #region Use the compiled program to get the ASTs
            for (int i = 0; i < fields.Length; i++)
            {
                var field = fields[i];
                if (field == null)
                {
                    continue;
                }
                if (!rfs[i].required)
                {
                    continue;
                }
                var val = prelude.TopLevelDeclarations.First(d => { Bpl.NamedDeclaration nd = d as Bpl.NamedDeclaration; return(nd != null && nd.Name.Equals(rfs[i].name)); });
                field.SetValue(instance, val);
            }
            #endregion

            #region Check that every field in this class has been set
            for (int i = 0; i < fields.Length; i++)
            {
                var field = fields[i];
                if (field == null)
                {
                    continue;
                }
                if (!rfs[i].required)
                {
                    continue;
                }
                if (field.GetValue(instance) == null)
                {
                    return(false);
                }
            }
            #endregion Check that every field in this class has been set

            return(true);
        }