Ejemplo n.º 1
0
	void DeclModifier(ref DeclModifierData dmod) {
		if (la.kind == 64) {
			Get();
			dmod.IsAbstract = true;  CheckAndSetToken(ref dmod.AbstractToken); 
		} else if (la.kind == 65) {
			Get();
			dmod.IsGhost = true;  CheckAndSetToken(ref dmod.GhostToken); 
		} else if (la.kind == 66) {
			Get();
			dmod.IsStatic = true; CheckAndSetToken(ref dmod.StaticToken); 
		} else if (la.kind == 67) {
			Get();
			dmod.IsProtected = true; CheckAndSetToken(ref dmod.ProtectedToken); 
		} else if (la.kind == 68) {
			Get();
			dmod.IsExtern = true; CheckAndSetToken(ref dmod.ExternToken); 
			if (la.kind == 20) {
				Get();
				bool isVerbatimString;
				string s = Util.RemoveParsedStringQuotes(t.val, out isVerbatimString);
				dmod.ExternName = new StringLiteralExpr(t, s, isVerbatimString);
				
			}
		} else SynErr(152);
	}
Ejemplo n.º 2
0
	void SubModuleDecl(DeclModifierData dmod, ModuleDefinition parent, out ModuleDecl submodule) {
		Attributes attrs = null;  IToken/*!*/ id;
		List<MemberDecl/*!*/> namedModuleDefaultClassMembers = new List<MemberDecl>();;
		List<IToken> idRefined = null, idPath = null, idAssignment = null;
		ModuleDefinition module;
		submodule = null; // appease compiler
		bool isAbstract = dmod.IsAbstract;
		bool isExclusively = false;
		bool opened = false;
		CheckDeclModifiers(dmod, "Modules", AllowedDeclModifiers.Abstract | AllowedDeclModifiers.Extern);
		
		if (la.kind == 69) {
			Get();
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			NoUSIdent(out id);
			EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false); 
			if (la.kind == 70 || la.kind == 71) {
				if (la.kind == 70) {
					Get();
					Expect(71);
					QualifiedModuleName(out idRefined);
					isExclusively = true; 
				} else {
					Get();
					QualifiedModuleName(out idRefined);
					isExclusively = false; 
				}
			}
			module = new ModuleDefinition(id, id.val, isAbstract, false, isExclusively, idRefined == null ? null : idRefined, parent, attrs, false, this); 
			Expect(46);
			module.BodyStartTok = t; 
			while (StartOf(1)) {
				TopDecl(module, namedModuleDefaultClassMembers, /* isTopLevel */ false, isAbstract);
			}
			Expect(47);
			module.BodyEndTok = t;
			module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers));
			submodule = new LiteralModuleDecl(module, parent); 
		} else if (la.kind == 72) {
			Get();
			if (la.kind == 73) {
				Get();
				opened = true;
			}
			NoUSIdent(out id);
			EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false); 
			if (StartOf(3)) {
				if (la.kind == 74) {
					Get();
					QualifiedModuleName(out idPath);
					submodule = new AliasModuleDecl(idPath, id, parent, opened); 
				} else if (la.kind == 75) {
					Get();
					QualifiedModuleName(out idPath);
					if (IsDefaultImport()) {
						Expect(76);
						QualifiedModuleName(out idAssignment);
					}
					submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened); 
					errors.Warning(t, "\"import A as B\" has been deprecated; in the new syntax, it is \"import A:B\"");
					
				} else if (la.kind == 21) {
					Get();
					QualifiedModuleName(out idPath);
					submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened); 
				} else {
					Get();
					QualifiedModuleName(out idPath);
					idPath.Insert(0, id);
					submodule = new AliasModuleDecl(idPath, id, parent, opened); 
					
				}
			}
			if (la.kind == 28) {
				while (!(la.kind == 0 || la.kind == 28)) {SynErr(153); Get();}
				Get();
				errors.Warning(t, "the semi-colon that used to terminate a sub-module declaration has been deprecated; in the new syntax, just leave off the semi-colon"); 
			}
			if (submodule == null) {
			 idPath = new List<IToken>();
			 idPath.Add(id);
			 submodule = new AliasModuleDecl(idPath, id, parent, opened);
			}
			
		} else if (la.kind == 76 || la.kind == 77) {
			bool isDefault = false;
			bool includeBody;
			IToken exportId;
			List<ExportSignature> exports = new List<ExportSignature>();;
			List<string> extends = new List<string>(); 
			
			if (la.kind == 76) {
				Get();
				isDefault = true; 
			}
			Expect(77);
			NoUSIdent(out exportId);
			if (la.kind == 78) {
				Get();
				NoUSIdent(out id);
				extends.Add(id.val); 
				while (la.kind == 22) {
					Get();
					NoUSIdent(out id);
					extends.Add(id.val); 
				}
			}
			Expect(46);
			NoUSIdent(out id);
			includeBody = false; 
			if (la.kind == 79) {
				Get();
				includeBody = true; 
			}
			exports.Add(new ExportSignature(id, includeBody)); 
			while (la.kind == 22) {
				Get();
				NoUSIdent(out id);
				includeBody = false; 
				if (la.kind == 79) {
					Get();
					includeBody = true; 
				}
				exports.Add(new ExportSignature(id, includeBody)); 
			}
			Expect(47);
			submodule = new ModuleExportDecl(exportId, parent, isDefault, exports, extends);
			
		} else SynErr(154);
	}
Ejemplo n.º 3
0
/// <summary>
/// Encode an 'extern' declaration modifier as an {:extern name} attribute.
///
/// We also include an {:axiom} attribute since the specification of an
/// external entity is assumed to hold, but only for methods or functions.
///</summary>
static void EncodeExternAsAttribute(DeclModifierData dmod, ref Attributes attrs, IToken/*!*/ id, bool needAxiom) {
  if (dmod.IsExtern) {
    StringLiteralExpr name = dmod.ExternName;
    if (name == null) {
      bool isVerbatimString = false;
      name = new StringLiteralExpr(id, id.val, isVerbatimString);
    }
    var args = new List<Expression>();
    args.Add(name);
    attrs = new Attributes("extern", args, attrs);

    // Also 'extern' implies 'axiom' for methods or functions.
    if (needAxiom) {
      attrs = new Attributes("axiom", new List<Expression>(), attrs);
    }
  }
}
Ejemplo n.º 4
0
	void TopDecl(ModuleDefinition module, List<MemberDecl/*!*/> membersDefaultClass, bool isTopLevel, bool isAbstract ) {
		DeclModifierData dmod = new DeclModifierData(); ModuleDecl submodule;
		ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; TopLevelDecl td; IteratorDecl iter;
		TraitDecl/*!*/ trait;
		
		while (StartOf(2)) {
			DeclModifier(ref dmod);
		}
		switch (la.kind) {
		case 69: case 72: case 76: case 77: {
			SubModuleDecl(dmod, module, out submodule);
			module.TopLevelDecls.Add(submodule); 
			break;
		}
		case 80: {
			ClassDecl(dmod, module, out c);
			module.TopLevelDecls.Add(c); 
			break;
		}
		case 82: case 83: {
			DatatypeDecl(dmod, module, out dt);
			module.TopLevelDecls.Add(dt); 
			break;
		}
		case 84: {
			NewtypeDecl(dmod, module, out td);
			module.TopLevelDecls.Add(td); 
			break;
		}
		case 85: {
			OtherTypeDecl(dmod, module, out td);
			module.TopLevelDecls.Add(td); 
			break;
		}
		case 86: {
			IteratorDecl(dmod, module, out iter);
			module.TopLevelDecls.Add(iter); 
			break;
		}
		case 81: {
			TraitDecl(dmod, module, out trait);
			module.TopLevelDecls.Add(trait); 
			break;
		}
		case 38: case 39: case 40: case 41: case 42: case 60: case 61: case 89: case 90: case 91: case 92: {
			ClassMemberDecl(dmod, membersDefaultClass, false, !DafnyOptions.O.AllowGlobals, 
!isTopLevel && DafnyOptions.O.IronDafny && isAbstract);
			break;
		}
		default: SynErr(151); break;
		}
	}
Ejemplo n.º 5
0
	void MethodDecl(DeclModifierData dmod, bool allowConstructor, bool isWithinAbstractModule, out Method/*!*/ m) {
		Contract.Ensures(Contract.ValueAtReturn(out m) !=null);
		IToken/*!*/ id = Token.NoToken;
		bool hasName = false;  IToken keywordToken;
		Attributes attrs = null;
		List<TypeParameter/*!*/>/*!*/ typeArgs = new List<TypeParameter/*!*/>();
		List<Formal/*!*/> ins = new List<Formal/*!*/>();
		List<Formal/*!*/> outs = new List<Formal/*!*/>();
		List<MaybeFreeExpression/*!*/> req = new List<MaybeFreeExpression/*!*/>();
		List<FrameExpression/*!*/> mod = new List<FrameExpression/*!*/>();
		List<MaybeFreeExpression/*!*/> ens = new List<MaybeFreeExpression/*!*/>();
		List<Expression/*!*/> dec = new List<Expression/*!*/>();
		Attributes decAttrs = null;
		Attributes modAttrs = null;
		BlockStmt body = null;
		bool isLemma = false;
		bool isConstructor = false;
		bool isIndLemma = false;
		bool isCoLemma = false;
		bool isTactic = false;
		IToken signatureEllipsis = null;
		IToken bodyStart = Token.NoToken;
		IToken bodyEnd = Token.NoToken;
		AllowedDeclModifiers allowed = AllowedDeclModifiers.None;
		string caption = "";
		
		while (!(StartOf(12))) {SynErr(172); Get();}
		switch (la.kind) {
		case 89: {
			Get();
			caption = "Methods";
			allowed = AllowedDeclModifiers.Ghost | AllowedDeclModifiers.Static 
			 | AllowedDeclModifiers.Extern; 
			break;
		}
		case 60: {
			Get();
			isTactic = true; caption = "Tactics"; 
			allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static 
			| AllowedDeclModifiers.Protected; 
			break;
		}
		case 41: {
			Get();
			isLemma = true; caption = "Lemmas";
			allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static 
			 | AllowedDeclModifiers.Protected; 
			break;
		}
		case 90: {
			Get();
			isCoLemma = true; caption = "Colemmas";
			allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static 
			 | AllowedDeclModifiers.Protected; 
			break;
		}
		case 91: {
			Get();
			isCoLemma = true; caption = "Comethods";
			allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static 
			 | AllowedDeclModifiers.Protected;
			errors.Warning(t, "the 'comethod' keyword has been deprecated; it has been renamed to 'colemma'");
			
			break;
		}
		case 40: {
			Get();
			Expect(41);
			isIndLemma = true;  caption = "Inductive lemmas";
			allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static;
			break;
		}
		case 92: {
			Get();
			if (allowConstructor) {
			 isConstructor = true;
			} else {
			 SemErr(t, "constructors are allowed only in classes");
			} 
			caption = "Constructors";
			allowed = AllowedDeclModifiers.None;
			
			break;
		}
		default: SynErr(173); break;
		}
		keywordToken = t; 
		CheckDeclModifiers(dmod, caption, allowed); 
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		if (la.kind == 1) {
			NoUSIdent(out id);
			hasName = true; 
		}
		if (!hasName) {
		 id = keywordToken;
		 if (!isConstructor) {
		   SemErr(la, "a method must be given a name (expecting identifier)");
		 }
		}
		EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ true);
		
		if (la.kind == 50 || la.kind == 52) {
			if (la.kind == 52) {
				GenericParameters(typeArgs);
			}
			Formals(true, !dmod.IsGhost, ins);
			if (la.kind == 88) {
				Get();
				if (isConstructor) { SemErr(t, "constructors cannot have out-parameters"); } 
				Formals(false, !dmod.IsGhost, outs);
			}
		} else if (la.kind == 59) {
			Get();
			signatureEllipsis = t; 
		} else SynErr(174);
		while (StartOf(13)) {
			MethodSpec(req, mod, ens, dec, ref decAttrs, ref modAttrs);
		}
		if (la.kind == 46) {
			BlockStmt(out body, out bodyStart, out bodyEnd);
		}
		if (!isWithinAbstractModule && DafnyOptions.O.DisallowSoundnessCheating && body == null && ens.Count > 0 && !Attributes.Contains(attrs, "axiom") && !Attributes.Contains(attrs, "imported") && !Attributes.Contains(attrs, "decl") && theVerifyThisFile) {
		  SemErr(t, "a method with an ensures clause must have a body, unless given the :axiom attribute");
		}
		
		IToken tok = theVerifyThisFile ? id : new IncludeToken(id);
		if (isConstructor) {
		 m = new Constructor(tok, hasName ? id.val : "_ctor", typeArgs, ins,
		                     req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
		} else if (isIndLemma) {
		 m = new InductiveLemma(tok, id.val, dmod.IsStatic, typeArgs, ins, outs,
		                        req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
		} else if (isCoLemma) {
		 m = new CoLemma(tok, id.val, dmod.IsStatic, typeArgs, ins, outs,
		                 req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
		} else if (isLemma) {
		 m = new Lemma(tok, id.val, dmod.IsStatic, typeArgs, ins, outs,
		               req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
		} else if(isTactic) {
		 m = new Tactic(tok, id.val, dmod.IsStatic, typeArgs, ins, outs,
		               req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
		
		} else {
		 m = new Method(tok, id.val, dmod.IsStatic, dmod.IsGhost, typeArgs, ins, outs,
		                req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
		}
		m.BodyStartTok = bodyStart;
		m.BodyEndTok = bodyEnd;
		
	}
Ejemplo n.º 6
0
/// <summary>
/// Check the declaration modifiers against those that are allowed.
///
/// The 'allowed' parameter specifies which declaratio modifiers are allowed.
/// The 'declCaption' parameter should be a string describing the kind of declaration. 
/// It is used in error messages.
/// Any declaration modifiers that are present but not allowed are cleared.
///</summary>
void CheckDeclModifiers(DeclModifierData dmod, string declCaption, AllowedDeclModifiers allowed)
{
  if (dmod.IsAbstract && ((allowed & AllowedDeclModifiers.Abstract) == 0)) {
    SemErr(dmod.AbstractToken, declCaption + " cannot be declared 'abstract'.");
    dmod.IsAbstract = false;
  }
  if (dmod.IsGhost) {
    if ((allowed & AllowedDeclModifiers.AlreadyGhost) != 0) {
      SemErr(dmod.GhostToken, declCaption + " cannot be declared ghost (they are 'ghost' by default).");
      dmod.IsGhost = false;
    } else if ((allowed & AllowedDeclModifiers.Ghost) == 0) {
      SemErr(dmod.GhostToken, declCaption + " cannot be declared 'ghost'.");
      dmod.IsGhost = false;
    }
  }
  if (dmod.IsStatic && ((allowed & AllowedDeclModifiers.Static) == 0)) {
    SemErr(dmod.StaticToken, declCaption + " cannot be declared 'static'.");
    dmod.IsStatic = false;
  }
  if (dmod.IsProtected && ((allowed & AllowedDeclModifiers.Protected) == 0)) {
    SemErr(dmod.ProtectedToken, declCaption + " cannot be declared 'protected'.");
    dmod.IsProtected = false;
  }
  if (dmod.IsExtern && ((allowed & AllowedDeclModifiers.Extern) == 0)) {
    SemErr(dmod.ExternToken, declCaption + " cannot be declared 'extern'.");
    dmod.IsExtern = false;
  }
}
Ejemplo n.º 7
0
	void FieldDecl(DeclModifierData dmod, List<MemberDecl/*!*/>/*!*/ mm) {
		Contract.Requires(cce.NonNullElements(mm));
		Attributes attrs = null;
		IToken/*!*/ id;  Type/*!*/ ty;
		CheckDeclModifiers(dmod, "Fields", AllowedDeclModifiers.Ghost);
		
		while (!(la.kind == 0 || la.kind == 61)) {SynErr(166); Get();}
		Expect(61);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		FIdentType(out id, out ty);
		mm.Add(new Field(id, id.val, dmod.IsGhost, ty, attrs)); 
		while (la.kind == 22) {
			Get();
			FIdentType(out id, out ty);
			mm.Add(new Field(id, id.val, dmod.IsGhost, ty, attrs)); 
		}
		OldSemi();
	}
Ejemplo n.º 8
0
        void SubModuleDecl(DeclModifierData dmod, ModuleDefinition parent, out ModuleDecl submodule)
        {
            Attributes attrs = null;  IToken/*!*/ id;
            List<MemberDecl/*!*/> namedModuleDefaultClassMembers = new List<MemberDecl>();;
            List<IToken> idPath, idExports;
            IToken idRefined = null;
            ModuleDefinition module;
            submodule = null; // appease compiler
            bool isAbstract = dmod.IsAbstract;
            bool isExclusively = false;
            bool isProtected = dmod.IsProtected;
            bool opened = false;
            CheckDeclModifiers(dmod, "Modules", AllowedDeclModifiers.Abstract | AllowedDeclModifiers.Extern | AllowedDeclModifiers.Protected);

            if (la.kind == 70) {
            Get();
            while (la.kind == 50) {
                Attribute(ref attrs);
            }
            NoUSIdent(out id);
            EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false);
            if (la.kind == 71) {
                Get();
                ModuleName(out idRefined);
                isExclusively = false;
            }
            module = new ModuleDefinition(id, id.val, isAbstract, isProtected, false, isExclusively, idRefined, parent, attrs, false); module.IsToBeVerified = theVerifyThisFile;
            Expect(50);
            module.BodyStartTok = t;
            while (StartOf(1)) {
                TopDecl(module, namedModuleDefaultClassMembers, /* isTopLevel */ false, isAbstract);
            }
            Expect(51);
            module.BodyEndTok = t;
            module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers));
            submodule = new LiteralModuleDecl(module, parent);
            } else if (la.kind == 72) {
            Get();
            if (la.kind == 73) {
                Get();
                opened = true;
            }
            ModuleName(out id);
            EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ false);
            if (StartOf(3)) {
                idPath = new List<IToken>(); idExports = new List<IToken>();
                if (la.kind == 28 || la.kind == 29) {
                    QualifiedModuleExportSuffix(idPath, idExports);
                }
                if (idPath.Count > 0)
                 SemErr(idPath[0], "Qualified imports must be given a name.");
                idPath.Insert(0, id);
                submodule = new AliasModuleDecl(idPath, id, parent, opened, idExports);

            } else if (la.kind == 74) {
                Get();
                QualifiedModuleExport(out idPath, out idExports);
                submodule = new AliasModuleDecl(idPath, id, parent, opened, idExports);
            } else if (la.kind == 22) {
                Get();
                QualifiedModuleExport(out idPath, out idExports);
                submodule = new ModuleFacadeDecl(idPath, id, parent, opened, idExports);
            } else SynErr(150);
            if (la.kind == 30) {
                while (!(la.kind == 0 || la.kind == 30)) {SynErr(151); Get();}
                Get();
                errors.Deprecated(t, "the semi-colon that used to terminate a sub-module declaration has been deprecated; in the new syntax, just leave off the semi-colon");
            }
            } else if (la.kind == 75) {
            IToken exportId;
            List<ExportSignature> exports = new List<ExportSignature>();;
              List<string> extends = new List<string>();
              bool provideAll = false;
              bool revealAll = false;
              bool isDefault = false;
              ExportSignature exsig;

            Get();
            exportId = t;
            if (StartOf(4)) {
                ModuleExport(out exportId);
            }
            while (la.kind == 76 || la.kind == 77 || la.kind == 78) {
                if (la.kind == 76) {
                    Get();
                    if (la.kind == 1) {
                        ModuleExportSignature(true, out exsig);
                        exports.Add(exsig);
                        while (la.kind == 23) {
                            Get();
                            ModuleExportSignature(true, out exsig);
                            exports.Add(exsig);
                        }
                    } else if (la.kind == 61) {
                        Get();
                        provideAll = true;
                    } else SynErr(152);
                } else if (la.kind == 77) {
                    Get();
                    if (la.kind == 1) {
                        ModuleExportSignature(false, out exsig);
                        exports.Add(exsig);
                        while (la.kind == 23) {
                            Get();
                            ModuleExportSignature(false, out exsig);
                            exports.Add(exsig);
                        }
                    } else if (la.kind == 61) {
                        Get();
                        revealAll = true;
                    } else SynErr(153);
                } else {
                    Get();
                    ModuleExport(out id);
                    extends.Add(id.val);
                    while (la.kind == 23) {
                        Get();
                        ModuleExport(out id);
                        extends.Add(id.val);
                    }
                }
            }
            if (exportId.val == "export" || exportId.val == parent.Name) {
             isDefault = true;
            }
            submodule = new ModuleExportDecl(exportId, parent, exports, extends, provideAll, revealAll, isDefault);

            } else SynErr(154);
        }
Ejemplo n.º 9
0
	void TraitDecl(DeclModifierData dmodIn, ModuleDefinition/*!*/ module, out TraitDecl/*!*/ trait) {
		Contract.Requires(module != null);     
		Contract.Ensures(Contract.ValueAtReturn(out trait) != null);
		CheckDeclModifiers(dmodIn, "Traits", AllowedDeclModifiers.None);
		IToken/*!*/ id;
		Attributes attrs = null;
		List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>(); //traits should not support type parameters at the moment
		List<MemberDecl/*!*/> members = new List<MemberDecl/*!*/>();
		IToken bodyStart;
		DeclModifierData dmod; 
		
		while (!(la.kind == 0 || la.kind == 81)) {SynErr(164); Get();}
		Expect(81);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		NoUSIdent(out id);
		if (la.kind == 52) {
			GenericParameters(typeArgs);
		}
		Expect(46);
		bodyStart = t; 
		while (StartOf(4)) {
			dmod  = new DeclModifierData(); 
			while (StartOf(2)) {
				DeclModifier(ref dmod);
			}
			ClassMemberDecl(dmod, members, true, false, false);
		}
		Expect(47);
		trait = new TraitDecl(id, id.val, module, typeArgs, members, attrs);
		trait.BodyStartTok = bodyStart;
		trait.BodyEndTok = t;
		
	}
Ejemplo n.º 10
0
	void ClassMemberDecl(DeclModifierData dmod, List<MemberDecl> mm, bool allowConstructors, bool moduleLevelDecl, bool isWithinAbstractModule) {
		Contract.Requires(cce.NonNullElements(mm));
		Method/*!*/ m;
		Function/*!*/ f;
		
		if (la.kind == 61) {
			if (moduleLevelDecl) {
			 SemErr(la, "fields are not allowed to be declared at the module level; instead, wrap the field in a 'class' declaration");
			 dmod.IsStatic = false;
			}
			
			FieldDecl(dmod, mm);
		} else if (IsFunctionDecl()) {
			if (moduleLevelDecl && dmod.StaticToken != null) {
			 errors.Warning(dmod.StaticToken, "module-level functions are always non-instance, so the 'static' keyword is not allowed here");
			 dmod.IsStatic = false;
			}
			
			FunctionDecl(dmod, isWithinAbstractModule, out f);
			mm.Add(f); 
		} else if (StartOf(8)) {
			if (moduleLevelDecl && dmod.StaticToken != null) {
			 errors.Warning(dmod.StaticToken, "module-level methods are always non-instance, so the 'static' keyword is not allowed here");
			 dmod.IsStatic = false;
			}
			
			MethodDecl(dmod, allowConstructors, isWithinAbstractModule, out m);
			mm.Add(m); 
		} else SynErr(165);
	}
Ejemplo n.º 11
0
	void IteratorDecl(DeclModifierData dmod, ModuleDefinition module, out IteratorDecl/*!*/ iter) {
		Contract.Ensures(Contract.ValueAtReturn(out iter) != null);
		IToken/*!*/ id;
		Attributes attrs = null;
		List<TypeParameter/*!*/>/*!*/ typeArgs = new List<TypeParameter/*!*/>();
		List<Formal/*!*/> ins = new List<Formal/*!*/>();
		List<Formal/*!*/> outs = new List<Formal/*!*/>();
		List<FrameExpression/*!*/> reads = new List<FrameExpression/*!*/>();
		List<FrameExpression/*!*/> mod = new List<FrameExpression/*!*/>();
		List<Expression/*!*/> decreases = new List<Expression>();
		List<MaybeFreeExpression/*!*/> req = new List<MaybeFreeExpression/*!*/>();
		List<MaybeFreeExpression/*!*/> ens = new List<MaybeFreeExpression/*!*/>();
		List<MaybeFreeExpression/*!*/> yieldReq = new List<MaybeFreeExpression/*!*/>();
		List<MaybeFreeExpression/*!*/> yieldEns = new List<MaybeFreeExpression/*!*/>();
		List<Expression/*!*/> dec = new List<Expression/*!*/>();
		Attributes readsAttrs = null;
		Attributes modAttrs = null;
		Attributes decrAttrs = null;
		BlockStmt body = null;
		IToken signatureEllipsis = null;
		IToken bodyStart = Token.NoToken;
		IToken bodyEnd = Token.NoToken;
		CheckDeclModifiers(dmod, "Iterators", AllowedDeclModifiers.None);
		
		while (!(la.kind == 0 || la.kind == 86)) {SynErr(162); Get();}
		Expect(86);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		NoUSIdent(out id);
		if (la.kind == 50 || la.kind == 52) {
			if (la.kind == 52) {
				GenericParameters(typeArgs);
			}
			Formals(true, true, ins);
			if (la.kind == 87 || la.kind == 88) {
				if (la.kind == 87) {
					Get();
				} else {
					Get();
					SemErr(t, "iterators don't have a 'returns' clause; did you mean 'yields'?"); 
				}
				Formals(false, true, outs);
			}
		} else if (la.kind == 59) {
			Get();
			signatureEllipsis = t; 
		} else SynErr(163);
		while (StartOf(7)) {
			IteratorSpec(reads, mod, decreases, req, ens, yieldReq, yieldEns, ref readsAttrs, ref modAttrs, ref decrAttrs);
		}
		if (la.kind == 46) {
			BlockStmt(out body, out bodyStart, out bodyEnd);
		}
		iter = new IteratorDecl(id, id.val, module, typeArgs, ins, outs,
		                       new Specification<FrameExpression>(reads, readsAttrs),
		                       new Specification<FrameExpression>(mod, modAttrs),
		                       new Specification<Expression>(decreases, decrAttrs),
		                       req, ens, yieldReq, yieldEns,
		                       body, attrs, signatureEllipsis);
		iter.BodyStartTok = bodyStart;
		iter.BodyEndTok = bodyEnd;
		
	}
Ejemplo n.º 12
0
	void OtherTypeDecl(DeclModifierData dmod, ModuleDefinition module, out TopLevelDecl td) {
		IToken id;
		Attributes attrs = null;
		var eqSupport = TypeParameter.EqualitySupportValue.Unspecified;
		var typeArgs = new List<TypeParameter>();
		td = null;
		Type ty;
		CheckDeclModifiers(dmod, "Type aliases", AllowedDeclModifiers.None);
		
		Expect(85);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		NoUSIdent(out id);
		if (la.kind == 50) {
			Get();
			Expect(54);
			Expect(51);
			eqSupport = TypeParameter.EqualitySupportValue.Required; 
			if (la.kind == 52) {
				GenericParameters(typeArgs);
			}
		} else if (StartOf(6)) {
			if (la.kind == 52) {
				GenericParameters(typeArgs);
			}
			if (la.kind == 74) {
				Get();
				Type(out ty);
				td = new TypeSynonymDecl(id, id.val, typeArgs, module, ty, attrs); 
			}
		} else SynErr(160);
		if (td == null) {
		 td = new OpaqueTypeDecl(id, id.val, module, eqSupport, typeArgs, attrs);
		}
		
		if (la.kind == 28) {
			while (!(la.kind == 0 || la.kind == 28)) {SynErr(161); Get();}
			Get();
			errors.Warning(t, "the semi-colon that used to terminate an opaque-type declaration has been deprecated; in the new syntax, just leave off the semi-colon"); 
		}
	}
Ejemplo n.º 13
0
	void NewtypeDecl(DeclModifierData dmod, ModuleDefinition module, out TopLevelDecl td) {
		IToken id, bvId;
		Attributes attrs = null;
		td = null;
		Type baseType = null;
		Expression wh;
		CheckDeclModifiers(dmod, "Newtypes", AllowedDeclModifiers.None);
		
		Expect(84);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		NoUSIdent(out id);
		Expect(74);
		if (IsIdentColonOrBar()) {
			NoUSIdent(out bvId);
			if (la.kind == 21) {
				Get();
				Type(out baseType);
			}
			if (baseType == null) { baseType = new OperationTypeProxy(true, true, false, false, false, false); } 
			Expect(23);
			Expression(out wh, false, true);
			td = new NewtypeDecl(theVerifyThisFile ? id : new IncludeToken(id), id.val, module, new BoundVar(bvId, bvId.val, baseType), wh, attrs); 
		} else if (StartOf(5)) {
			Type(out baseType);
			td = new NewtypeDecl(theVerifyThisFile ? id : new IncludeToken(id), id.val, module, baseType, attrs); 
		} else SynErr(159);
	}
Ejemplo n.º 14
0
	void ClassDecl(DeclModifierData dmodClass, ModuleDefinition/*!*/ module, out ClassDecl/*!*/ c) {
		Contract.Requires(module != null);
		Contract.Ensures(Contract.ValueAtReturn(out c) != null);
		IToken/*!*/ id;
		Type trait = null;
		List<Type>/*!*/ traits = new List<Type>();
		Attributes attrs = null;
		List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
		List<MemberDecl/*!*/> members = new List<MemberDecl/*!*/>();
		IToken bodyStart;
		CheckDeclModifiers(dmodClass, "Classes", AllowedDeclModifiers.Extern);
		DeclModifierData dmod; 
		
		while (!(la.kind == 0 || la.kind == 80)) {SynErr(155); Get();}
		Expect(80);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		NoUSIdent(out id);
		EncodeExternAsAttribute(dmodClass, ref attrs, id, /* needAxiom */ false); 
		if (la.kind == 52) {
			GenericParameters(typeArgs);
		}
		if (la.kind == 78) {
			Get();
			Type(out trait);
			traits.Add(trait); 
			while (la.kind == 22) {
				Get();
				Type(out trait);
				traits.Add(trait); 
			}
		}
		Expect(46);
		bodyStart = t;  
		while (StartOf(4)) {
			dmod = new DeclModifierData(); 
			while (StartOf(2)) {
				DeclModifier(ref dmod);
			}
			ClassMemberDecl(dmod, members, true, false, false);
		}
		Expect(47);
		c = new ClassDecl(id, id.val, module, typeArgs, members, attrs, traits);
		c.BodyStartTok = bodyStart;
		c.BodyEndTok = t;
		
	}
Ejemplo n.º 15
0
	void FunctionDecl(DeclModifierData dmod, bool isWithinAbstractModule, out Function/*!*/ f) {
		Contract.Ensures(Contract.ValueAtReturn(out f)!=null);
		Attributes attrs = null;
		IToken/*!*/ id = Token.NoToken;  // to please compiler
		List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
		List<Formal/*!*/> formals = new List<Formal/*!*/>();
		Type/*!*/ returnType = new BoolType();
		List<Expression/*!*/> reqs = new List<Expression/*!*/>();
		List<Expression/*!*/> ens = new List<Expression/*!*/>();
		List<FrameExpression/*!*/> reads = new List<FrameExpression/*!*/>();
		List<Expression/*!*/> decreases;
		Expression body = null;
		bool isPredicate = false; bool isIndPredicate = false; bool isCoPredicate = false;
		bool isFunctionMethod = false;
		bool isFunctionTactic = false;
		IToken bodyStart = Token.NoToken;
		IToken bodyEnd = Token.NoToken;
		IToken signatureEllipsis = null;
		bool missingOpenParen;
		
		if (la.kind == 38) {
			Get();
			if (la.kind == 60 || la.kind == 89) {
				if (la.kind == 89) {
					Get();
					isFunctionMethod = true; 
				} else {
					Get();
					isFunctionTactic = true; 
				}
			}
			AllowedDeclModifiers allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static | AllowedDeclModifiers.Protected;
			string caption = "Functions";
			if (isFunctionMethod) { 
			 allowed |= AllowedDeclModifiers.Extern; 
			 caption = "Function methods";
			}
			CheckDeclModifiers(dmod, caption, allowed);
			
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			NoUSIdent(out id);
			if (la.kind == 50 || la.kind == 52) {
				if (la.kind == 52) {
					GenericParameters(typeArgs);
				}
				Formals(true, isFunctionMethod, formals);
				Expect(21);
				Type(out returnType);
			} else if (la.kind == 59) {
				Get();
				signatureEllipsis = t; 
			} else SynErr(167);
		} else if (la.kind == 39) {
			Get();
			isPredicate = true; 
			if (la.kind == 89) {
				Get();
				isFunctionMethod = true; 
			}
			AllowedDeclModifiers allowed = AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static | AllowedDeclModifiers.Protected;
			string caption = "Predicates";
			if (isFunctionMethod) { 
			 allowed |= AllowedDeclModifiers.Extern; 
			 caption = "Predicate methods";
			}
			CheckDeclModifiers(dmod, caption, allowed);
			
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			NoUSIdent(out id);
			if (StartOf(10)) {
				if (la.kind == 52) {
					GenericParameters(typeArgs);
				}
				missingOpenParen = true; 
				if (la.kind == 50) {
					Formals(true, isFunctionMethod, formals);
					missingOpenParen = false; 
				}
				if (missingOpenParen) { errors.Warning(t, "with the new support of higher-order functions in Dafny, parentheses-less predicates are no longer supported; in the new syntax, parentheses are required for the declaration and uses of predicates, even if the predicate takes no additional arguments"); } 
				if (la.kind == 21) {
					Get();
					SemErr(t, "predicates do not have an explicitly declared return type; it is always bool"); 
				}
			} else if (la.kind == 59) {
				Get();
				signatureEllipsis = t; 
			} else SynErr(168);
		} else if (la.kind == 40) {
			Get();
			Expect(39);
			isIndPredicate = true; 
			CheckDeclModifiers(dmod, "Inductive predicates",
			 AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static | AllowedDeclModifiers.Protected);
			
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			NoUSIdent(out id);
			if (la.kind == 50 || la.kind == 52) {
				if (la.kind == 52) {
					GenericParameters(typeArgs);
				}
				Formals(true, isFunctionMethod, formals);
				if (la.kind == 21) {
					Get();
					SemErr(t, "inductive predicates do not have an explicitly declared return type; it is always bool"); 
				}
			} else if (la.kind == 59) {
				Get();
				signatureEllipsis = t; 
			} else SynErr(169);
		} else if (la.kind == 42) {
			Get();
			isCoPredicate = true; 
			CheckDeclModifiers(dmod, "Copredicates",
			 AllowedDeclModifiers.AlreadyGhost | AllowedDeclModifiers.Static | AllowedDeclModifiers.Protected);
			
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			NoUSIdent(out id);
			if (la.kind == 50 || la.kind == 52) {
				if (la.kind == 52) {
					GenericParameters(typeArgs);
				}
				Formals(true, isFunctionMethod, formals);
				if (la.kind == 21) {
					Get();
					SemErr(t, "copredicates do not have an explicitly declared return type; it is always bool"); 
				}
			} else if (la.kind == 59) {
				Get();
				signatureEllipsis = t; 
			} else SynErr(170);
		} else SynErr(171);
		decreases = isIndPredicate || isCoPredicate ? null : new List<Expression/*!*/>(); 
		while (StartOf(11)) {
			FunctionSpec(reqs, reads, ens, decreases);
		}
		if (la.kind == 46) {
			FunctionBody(out body, out bodyStart, out bodyEnd);
		}
		if (!isWithinAbstractModule && DafnyOptions.O.DisallowSoundnessCheating && body == null && ens.Count > 0 &&
		   !Attributes.Contains(attrs, "axiom") && !Attributes.Contains(attrs, "imported")) {
		  SemErr(t, "a function with an ensures clause must have a body, unless given the :axiom attribute");
		}
		EncodeExternAsAttribute(dmod, ref attrs, id, /* needAxiom */ true);
		IToken tok = theVerifyThisFile ? id : new IncludeToken(id);
		if (isPredicate) {
		  f = new Predicate(tok, id.val, dmod.IsStatic, dmod.IsProtected, !isFunctionMethod, typeArgs, formals,
		                    reqs, reads, ens, new Specification<Expression>(decreases, null), body, Predicate.BodyOriginKind.OriginalOrInherited, attrs, signatureEllipsis);
		} else if (isIndPredicate) {
		  f = new InductivePredicate(tok, id.val, dmod.IsStatic, dmod.IsProtected, typeArgs, formals,
		                             reqs, reads, ens, body, attrs, signatureEllipsis);
		} else if (isCoPredicate) {
		  f = new CoPredicate(tok, id.val, dmod.IsStatic, dmod.IsProtected, typeArgs, formals,
		                      reqs, reads, ens, body, attrs, signatureEllipsis);
		} else if(isFunctionTactic) {
		  f = new TacticFunction(tok, id.val, dmod.IsStatic, dmod.IsProtected, !isFunctionMethod, typeArgs, formals, returnType,
		                   reqs, reads, ens, new Specification<Expression>(decreases, null), body, attrs, signatureEllipsis);
		} else {
		  f = new Function(tok, id.val, dmod.IsStatic, dmod.IsProtected, !isFunctionMethod, typeArgs, formals, returnType,
		                   reqs, reads, ens, new Specification<Expression>(decreases, null), body, attrs, signatureEllipsis);
		}
		f.BodyStartTok = bodyStart;
		f.BodyEndTok = bodyEnd;
		theBuiltIns.CreateArrowTypeDecl(formals.Count);
		if (isIndPredicate || isCoPredicate) {
		 // also create an arrow type for the corresponding prefix predicate
		 theBuiltIns.CreateArrowTypeDecl(formals.Count + 1);
		}
		
	}
Ejemplo n.º 16
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 == 82 || la.kind == 83)) {SynErr(156); Get();}
		if (la.kind == 82) {
			Get();
		} else if (la.kind == 83) {
			Get();
			co = true; 
		} else SynErr(157);
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		NoUSIdent(out id);
		if (la.kind == 52) {
			GenericParameters(typeArgs);
		}
		Expect(74);
		bodyStart = t; 
		DatatypeMemberDecl(ctors);
		while (la.kind == 23) {
			Get();
			DatatypeMemberDecl(ctors);
		}
		if (la.kind == 28) {
			while (!(la.kind == 0 || la.kind == 28)) {SynErr(158); 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;
		
	}
Ejemplo n.º 17
0
        void OtherTypeDecl(DeclModifierData dmod, ModuleDefinition module, out TopLevelDecl td)
        {
            IToken id, bvId;
            Attributes attrs = null;
            var eqSupport = TypeParameter.EqualitySupportValue.Unspecified;
            var typeArgs = new List<TypeParameter>();
            td = null;
            Type ty = null;
            Expression constraint;
            var kind = "Opaque type";

            Expect(85);
            while (la.kind == 50) {
            Attribute(ref attrs);
            }
            NoUSIdent(out id);
            if (la.kind == 54) {
            Get();
            Expect(58);
            Expect(55);
            eqSupport = TypeParameter.EqualitySupportValue.Required;
            if (la.kind == 56) {
                GenericParameters(typeArgs);
            }
            } else if (StartOf(7)) {
            if (la.kind == 56) {
                GenericParameters(typeArgs);
            }
            if (la.kind == 74) {
                Get();
                if (IsIdentColonOrBar()) {
                    NoUSIdent(out bvId);
                    if (la.kind == 22) {
                        Get();
                        Type(out ty);
                    }
                    if (ty == null) { ty = new InferredTypeProxy(); }
                    Expect(24);
                    Expression(out constraint, false, true);
                    td = new SubsetTypeDecl(id, id.val, typeArgs, module, new BoundVar(bvId, bvId.val, ty), constraint, attrs);
                    kind = "Subset type";

                } else if (StartOf(6)) {
                    Type(out ty);
                    td = new TypeSynonymDecl(id, id.val, typeArgs, module, ty, attrs);
                    kind = "Type synonym";

                } else SynErr(160);
            }
            } else SynErr(161);
            if (td == null) {
             td = new OpaqueTypeDecl(id, id.val, module, eqSupport, typeArgs, attrs);
            }

            CheckDeclModifiers(dmod, kind, AllowedDeclModifiers.None);
            if (la.kind == 30) {
            while (!(la.kind == 0 || la.kind == 30)) {SynErr(162); Get();}
            Get();
            errors.Deprecated(t, "the semi-colon that used to terminate an opaque-type declaration has been deprecated; in the new syntax, just leave off the semi-colon");
            }
        }