From 12c5cf9976f7c4993db5b930bf8bce0b64c428a1 Mon Sep 17 00:00:00 2001 From: rustanleino Date: Thu, 11 Mar 2010 07:08:51 +0000 Subject: Dafny: * Enforce ghost vs. non-ghost separation * Allow ghost parameters and ghost locals * Functions are ghost, but allow the non-ghost "function method" --- Source/Dafny/Dafny.atg | 53 +++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 20 deletions(-) (limited to 'Source/Dafny/Dafny.atg') diff --git a/Source/Dafny/Dafny.atg b/Source/Dafny/Dafny.atg index d6f3aeae..069c88c3 100644 --- a/Source/Dafny/Dafny.atg +++ b/Source/Dafny/Dafny.atg @@ -36,10 +36,11 @@ struct MemberModifiers { // helper routine for parsing call statements private static void RecordCallLhs(IdentifierExpr! e, List! lhs, - List! newVars) { + List! newVars) { + int index = lhs.Count; lhs.Add(e); if (parseVarScope.Find(e.Name) == null) { - VarDecl d = new VarDecl(e.tok, e.Name, new InferredTypeProxy(), null); + AutoVarDecl d = new AutoVarDecl(e.tok, e.Name, new InferredTypeProxy(), index); newVars.Add(d); parseVarScope.Push(e.Name, e.Name); } @@ -207,6 +208,14 @@ FieldDecl! mm> ";" . +GIdentType +/* isGhost always returns as false if allowGhost is false */ += (. isGhost = false; .) + [ "ghost" (. if (allowGhost) { isGhost = true; } else { SemErr(token, "formal cannot be declared 'ghost' in this context"); } .) + ] + IdentType + . + IdentType = Ident ":" @@ -276,9 +285,9 @@ MethodDecl Ident [ GenericParameters ] (. parseVarScope.PushMarker(); .) - Formals + Formals [ "returns" - Formals + Formals ] ( ";" { MethodSpec } @@ -307,12 +316,12 @@ MethodSpec! req, List! mod, List! formals> -= (. Token! id; Type! ty; .) +Formals! formals> += (. Token! id; Type! ty; bool isGhost; .) "(" [ - IdentType (. formals.Add(new Formal(id, id.val, ty, incoming)); parseVarScope.Push(id.val, id.val); .) - { "," IdentType (. formals.Add(new Formal(id, id.val, ty, incoming)); parseVarScope.Push(id.val, id.val); .) + GIdentType (. formals.Add(new Formal(id, id.val, ty, incoming, isGhost)); parseVarScope.Push(id.val, id.val); .) + { "," GIdentType (. formals.Add(new Formal(id, id.val, ty, incoming, isGhost)); parseVarScope.Push(id.val, id.val); .) } ] ")" @@ -322,8 +331,8 @@ FormalsOptionalIds! formals> = (. Token! id; Type! ty; string! name; .) "(" [ - TypeIdentOptional (. formals.Add(new Formal(id, name, ty, true)); parseVarScope.Push(name, name); .) - { "," TypeIdentOptional (. formals.Add(new Formal(id, name, ty, true)); parseVarScope.Push(name, name); .) + TypeIdentOptional (. formals.Add(new Formal(id, name, ty, true, false)); parseVarScope.Push(name, name); .) + { "," TypeIdentOptional (. formals.Add(new Formal(id, name, ty, true, false)); parseVarScope.Push(name, name); .) } ] ")" @@ -390,15 +399,18 @@ FunctionDecl List reads = new List(); List decreases = new List(); Expression! bb; Expression body = null; + bool isFunctionMethod = false; .) "function" - (. if (mmod.IsGhost) { SemErr(token, "functions cannot be declared 'ghost'"); } + [ "method" (. isFunctionMethod = true; .) + ] + (. if (mmod.IsGhost) { SemErr(token, "functions cannot be declared 'ghost' (they are ghost by default)"); } .) { Attribute } Ident [ GenericParameters ] (. parseVarScope.PushMarker(); .) - Formals + Formals ":" Type ( ";" @@ -407,7 +419,7 @@ FunctionDecl FunctionBody (. body = bb; .) ) (. parseVarScope.PopMarker(); - f = new Function(id, id.val, mmod.IsClass, mmod.IsUse, typeArgs, formals, returnType, reqs, reads, decreases, body, attrs); + f = new Function(id, id.val, mmod.IsClass, !isFunctionMethod, mmod.IsUse, typeArgs, formals, returnType, reqs, reads, decreases, body, attrs); .) . @@ -580,16 +592,17 @@ LhsExpr . VarDeclStmts! ss> -= (. VarDecl! d; .) - [ "ghost" ] += (. VarDecl! d; bool isGhost = false; .) + [ "ghost" (. isGhost = true; .) + ] "var" - IdentTypeRhs (. ss.Add(d); parseVarScope.Push(d.Name, d.Name); .) - { "," IdentTypeRhs (. ss.Add(d); parseVarScope.Push(d.Name, d.Name); .) + IdentTypeRhs (. ss.Add(d); parseVarScope.Push(d.Name, d.Name); .) + { "," IdentTypeRhs (. ss.Add(d); parseVarScope.Push(d.Name, d.Name); .) } ";" . -IdentTypeRhs +IdentTypeRhs = (. Token! id; Type! ty; Expression! e; Expression rhs = null; Type newType = null; Type optionalType = null; DeterminedAssignmentRhs optionalRhs = null; @@ -608,7 +621,7 @@ IdentTypeRhs } else if (optionalType == null) { optionalType = new InferredTypeProxy(); } - d = new VarDecl(id, id.val, optionalType, optionalRhs); + d = new VarDecl(id, id.val, optionalType, isGhost, optionalRhs); .) . @@ -664,7 +677,7 @@ CallStmt = (. Token! x, id; Expression! e; List lhs = new List(); - List newVars = new List(); + List newVars = new List(); .) "call" (. x = token; .) CallStmtSubExpr -- cgit v1.2.3