Beispiel #1
0
    public TopLevelDecl CloneDeclaration(TopLevelDecl d, ModuleDefinition m) {
      Contract.Requires(d != null);
      Contract.Requires(m != null);

      if (d is OpaqueTypeDecl) {
        var dd = (OpaqueTypeDecl)d;
        return new OpaqueTypeDecl(Tok(dd.tok), dd.Name, m, dd.EqualitySupport, dd.TypeArgs.ConvertAll(CloneTypeParam), CloneAttributes(dd.Attributes), d);
      } else if (d is TypeSynonymDecl) {
        var dd = (TypeSynonymDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        return new TypeSynonymDecl(Tok(dd.tok), dd.Name, tps, m, CloneType(dd.Rhs), CloneAttributes(dd.Attributes), dd);
      } else if (d is NewtypeDecl) {
        var dd = (NewtypeDecl)d;
        if (dd.Var == null) {
          return new NewtypeDecl(Tok(dd.tok), dd.Name, m, CloneType(dd.BaseType), CloneAttributes(dd.Attributes), dd);
        } else {
          return new NewtypeDecl(Tok(dd.tok), dd.Name, m, CloneBoundVar(dd.Var), CloneExpr(dd.Constraint), CloneAttributes(dd.Attributes), dd);
        }
      } else if (d is TupleTypeDecl) {
        var dd = (TupleTypeDecl)d;
        return new TupleTypeDecl(dd.Dims, dd.Module);
      } else if (d is IndDatatypeDecl) {
        var dd = (IndDatatypeDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var ctors = dd.Ctors.ConvertAll(CloneCtor);
        var dt = new IndDatatypeDecl(Tok(dd.tok), dd.Name, m, tps, ctors, CloneAttributes(dd.Attributes), dd);
        return dt;
      } else if (d is CoDatatypeDecl) {
        var dd = (CoDatatypeDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var ctors = dd.Ctors.ConvertAll(CloneCtor);
        var dt = new CoDatatypeDecl(Tok(dd.tok), dd.Name, m, tps, ctors, CloneAttributes(dd.Attributes), dd);
        return dt;
      } else if (d is IteratorDecl) {
        var dd = (IteratorDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var ins = dd.Ins.ConvertAll(CloneFormal);
        var outs = dd.Outs.ConvertAll(CloneFormal);
        var reads = CloneSpecFrameExpr(dd.Reads);
        var mod = CloneSpecFrameExpr(dd.Modifies);
        var decr = CloneSpecExpr(dd.Decreases);
        var req = dd.Requires.ConvertAll(CloneMayBeFreeExpr);
        var yreq = dd.YieldRequires.ConvertAll(CloneMayBeFreeExpr);
        var ens = dd.Ensures.ConvertAll(CloneMayBeFreeExpr);
        var yens = dd.YieldEnsures.ConvertAll(CloneMayBeFreeExpr);
        var body = CloneBlockStmt(dd.Body);
        var iter = new IteratorDecl(Tok(dd.tok), dd.Name, dd.Module,
          tps, ins, outs, reads, mod, decr,
          req, ens, yreq, yens,
          body, CloneAttributes(dd.Attributes), dd.SignatureEllipsis);
        return iter;
      } else if (d is TraitDecl) {
        if (d is DefaultClassDecl) {
          var dd = (TraitDecl)d;
          var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
          var mm = dd.Members.ConvertAll(CloneMember);
          var cl = new DefaultClassDecl(m, mm);
          return cl;
        } else {
          var dd = (TraitDecl)d;
          var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
          var mm = dd.Members.ConvertAll(CloneMember);
          var cl = new TraitDecl(Tok(dd.tok), dd.Name, m, tps, mm, CloneAttributes(dd.Attributes), dd);
          return cl;
        }
      } else if (d is ClassDecl) {
        var dd = (ClassDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var mm = dd.Members.ConvertAll(CloneMember);
        if (d is DefaultClassDecl) {
          return new DefaultClassDecl(m, mm, ((DefaultClassDecl)d));
        } else {
          return new ClassDecl(Tok(dd.tok), dd.Name, m, tps, mm, CloneAttributes(dd.Attributes), dd.TraitsTyp.ConvertAll(CloneType), dd);
        }
      } else if (d is ModuleDecl) {
        if (d is LiteralModuleDecl) {
          var l = new LiteralModuleDecl(((LiteralModuleDecl)d).ModuleDef, m);
          l.Signature = ((ModuleDecl)d).Signature;
          return l;
        } else if (d is AliasModuleDecl) {
          var a = (AliasModuleDecl)d;
          var alias = new AliasModuleDecl(a.Path, a.tok, m, a.Opened);
          alias.Signature = a.Signature;
          return alias;
        } else if (d is ModuleFacadeDecl) {
          var a = (ModuleFacadeDecl)d;
          var abs = new ModuleFacadeDecl(a.Path, a.tok, m, a.CompilePath, a.Opened);
          abs.Signature = a.Signature;
          abs.OriginalSignature = a.OriginalSignature;
          return abs;
        } else if (d is ModuleExportDecl) {
          var a = (ModuleExportDecl)d;
          var export = new ModuleExportDecl(a.tok, m, a.IsDefault, a.Exports, a.Extends);
          export.Signature = a.Signature;
          return export;
        } else {
          Contract.Assert(false);  // unexpected declaration
          return null;  // to please compiler
        }
      } else {
        Contract.Assert(false);  // unexpected declaration
        return null;  // to please compiler
      }
    }
Beispiel #2
0
        void DatatypeDecl(ModuleDefinition/*!*/ module, out DatatypeDecl/*!*/ dt)
        {
            Contract.Requires(module != null);
            Contract.Ensures(Contract.ValueAtReturn(out dt)!=null);
            IToken/*!*/ id;
            Attributes attrs = null;
            List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
            List<DatatypeCtor/*!*/> ctors = new List<DatatypeCtor/*!*/>();
            IToken bodyStart = Token.NoToken;  // dummy assignment
            bool co = false;

            while (!(la.kind == 0 || la.kind == 76 || la.kind == 77)) {SynErr(142); Get();}
            if (la.kind == 76) {
            Get();
            } else if (la.kind == 77) {
            Get();
            co = true;
            } else SynErr(143);
            while (la.kind == 46) {
            Attribute(ref attrs);
            }
            NoUSIdent(out id);
            if (la.kind == 52) {
            GenericParameters(typeArgs);
            }
            Expect(67);
            bodyStart = t;
            DatatypeMemberDecl(ctors);
            while (la.kind == 23) {
            Get();
            DatatypeMemberDecl(ctors);
            }
            if (la.kind == 28) {
            while (!(la.kind == 0 || la.kind == 28)) {SynErr(144); Get();}
            Get();
            errors.Warning(t, "the semi-colon that used to terminate a (co)datatype declaration has been deprecated; in the new syntax, just leave off the semi-colon");
            }
            if (co) {
             dt = new CoDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs);
            } else {
             dt = new IndDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs);
            }
            dt.BodyStartTok = bodyStart;
            dt.BodyEndTok = t;
        }
Beispiel #3
0
        void DatatypeDecl(DeclModifierData dmod, ModuleDefinition/*!*/ module, out DatatypeDecl/*!*/ dt)
        {
            Contract.Requires(module != null);
            Contract.Ensures(Contract.ValueAtReturn(out dt)!=null);
            IToken/*!*/ id;
            Attributes attrs = null;
            List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
            List<DatatypeCtor/*!*/> ctors = new List<DatatypeCtor/*!*/>();
            IToken bodyStart = Token.NoToken;  // dummy assignment
            bool co = false;
            CheckDeclModifiers(dmod, "Datatypes or codatatypes", AllowedDeclModifiers.None);

            while (!(la.kind == 0 || la.kind == 81 || la.kind == 82)) {SynErr(156); Get();}
            if (la.kind == 81) {
            Get();
            } else if (la.kind == 82) {
            Get();
            co = true;
            } else SynErr(157);
            while (la.kind == 50) {
            Attribute(ref attrs);
            }
            NoUSIdent(out id);
            if (la.kind == 56) {
            GenericParameters(typeArgs);
            }
            Expect(74);
            bodyStart = t;
            DatatypeMemberDecl(ctors);
            while (la.kind == 24) {
            Get();
            DatatypeMemberDecl(ctors);
            }
            if (la.kind == 30) {
            while (!(la.kind == 0 || la.kind == 30)) {SynErr(158); Get();}
            Get();
            errors.Deprecated(t, "the semi-colon that used to terminate a (co)datatype declaration has been deprecated; in the new syntax, just leave off the semi-colon");
            }
            if (co) {
             dt = new CoDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs);
            } else {
             dt = new IndDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs);
            }
            dt.BodyStartTok = bodyStart;
            dt.BodyEndTok = t;
        }