/// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="bplObject"></param>
        /// <returns>true if defining a new name, false if replacing</returns>
        public bool setBoogieObjectForName(string name, Bpl.NamedDeclaration bplObject)
        {
            bool ret = true;

            if (boogieObjects.ContainsKey(name))
            {
                ret = false;
            }

            boogieObjects[name] = bplObject;
            return(ret);
        }
Example #2
0
        public void AddType(NamedDeclaration td)
        {
            Contract.Requires(td != null);
              Contract.Requires((td is TypeCtorDecl) || (td is TypeSynonymDecl));
              Contract.Requires(td.Name != null);

              string name = td.Name;
              if (CheckBvNameClashes(td, name))
            return;  // error has already been reported

              var previous = (NamedDeclaration)types[name];
              if (previous == null) {
            types.Add(name, td);
              } else {
            var r = (NamedDeclaration)SelectNonExtern(td, previous);
            if (r == null) {
              Error(td, "more than one declaration of type name: {0}", name);
            } else {
              types[name] = r;
            }
              }
        }
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);
        }