Skip to content

Commit e803796

Browse files
davidcokdavidcok
and
davidcok
authored
Adding utility functions; tightening types (#3973)
These changes are in support of dafny doc, but are generally useful. <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small> Co-authored-by: davidcok <[email protected]>
1 parent e48820a commit e803796

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

Source/DafnyCore/AST/DafnyAst.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ void ObjectInvariant() {
4949
// Contains the definitions to be used for compilation.
5050

5151
public Method MainMethod; // Method to be used as main if compiled
52-
public readonly ModuleDecl DefaultModule;
52+
public readonly LiteralModuleDecl DefaultModule;
5353
public readonly DefaultModuleDefinition DefaultModuleDef;
5454
public readonly BuiltIns BuiltIns;
5555
public DafnyOptions Options => Reporter.Options;
5656
public ErrorReporter Reporter { get; set; }
5757

58-
public Program(string name, [Captured] ModuleDecl module, [Captured] BuiltIns builtIns, ErrorReporter reporter) {
58+
public Program(string name, [Captured] LiteralModuleDecl module, [Captured] BuiltIns builtIns, ErrorReporter reporter) {
5959
Contract.Requires(name != null);
6060
Contract.Requires(module != null);
61-
Contract.Requires(module is LiteralModuleDecl);
6261
Contract.Requires(reporter != null);
6362
FullName = name;
6463
DefaultModule = module;
@@ -193,6 +192,16 @@ public Attributes(string name, [Captured] List<Expression> args, Attributes prev
193192
Prev = prev;
194193
}
195194

195+
public override string ToString() {
196+
string result = Prev?.ToString() + "{:" + Name;
197+
if (Args == null || Args.Count() == 0) {
198+
return result + "}";
199+
} else {
200+
var exprs = String.Join(", ", Args.Select(e => e.ToString()));
201+
return result + " " + exprs + "}";
202+
}
203+
}
204+
196205
public static IEnumerable<Expression> SubExpressions(Attributes attrs) {
197206
return attrs.AsEnumerable().SelectMany(aa => aa.Args);
198207
}

Source/DafnyCore/AST/MemberDecls.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ public virtual bool IsStatic {
1919
protected readonly bool isGhost;
2020
public bool IsGhost { get { return isGhost; } }
2121

22+
public string ModifiersAsString() {
23+
string result = "";
24+
if (IsGhost) {
25+
result += "ghost ";
26+
}
27+
if (IsStatic) {
28+
result += "static ";
29+
}
30+
if (IsOpaque) {
31+
result += "opaque ";
32+
}
33+
return result;
34+
}
35+
2236
/// <summary>
2337
/// The term "instance independent" can be confusing. It means that the constant does not get its value in
2438
/// a constructor. (But the RHS of the const's declaration may mention "this".)
@@ -712,4 +726,4 @@ public GreatestLemma(RangeToken rangeToken, Name name,
712726
Contract.Requires(cce.NonNullElements(ens));
713727
Contract.Requires(decreases != null);
714728
}
715-
}
729+
}

Source/DafnyCore/AST/TopLevelDeclarations.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ public override string SanitizedName {
201201
/// Contravariant - - contra-variant
202202
/// </summary>
203203
public enum TPVarianceSyntax { NonVariant_Strict, NonVariant_Permissive, Covariant_Strict, Covariant_Permissive, Contravariance }
204+
public static string VarianceString(TPVarianceSyntax varianceSyntax) {
205+
switch (varianceSyntax) {
206+
case TPVarianceSyntax.NonVariant_Strict: return "";
207+
case TPVarianceSyntax.NonVariant_Permissive: return "!";
208+
case TPVarianceSyntax.Covariant_Strict: return "+";
209+
case TPVarianceSyntax.Covariant_Permissive: return "*";
210+
case TPVarianceSyntax.Contravariance: return "-";
211+
}
212+
return ""; // Should not happen, but compiler complains
213+
}
204214
public enum TPVariance { Co, Non, Contra }
205215
public static TPVariance Negate(TPVariance v) {
206216
switch (v) {
@@ -275,6 +285,25 @@ public TypeParameterCharacteristics(EqualitySupportValue eqSupport, Type.AutoIni
275285
AutoInit = autoInit;
276286
ContainsNoReferenceTypes = containsNoReferenceTypes;
277287
}
288+
public override string ToString() {
289+
string result = "";
290+
if (EqualitySupport == EqualitySupportValue.Required) {
291+
result += ",==";
292+
}
293+
if (HasCompiledValue) {
294+
result += ",0";
295+
}
296+
if (AutoInit == Type.AutoInitInfo.Nonempty) {
297+
result += ",00";
298+
}
299+
if (ContainsNoReferenceTypes) {
300+
result += ",!new";
301+
}
302+
if (result.Length != 0) {
303+
result = "(" + result.Substring(1) + ")";
304+
}
305+
return result;
306+
}
278307
}
279308
public TypeParameterCharacteristics Characteristics;
280309
public bool SupportsEquality {

Source/DafnyCore/DafnyMain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public static string Parse(IList<DafnyFile> files, string programName, DafnyOpti
269269

270270
if (options.PrintIncludesMode == DafnyOptions.IncludesModes.Immediate) {
271271
DependencyMap dmap = new DependencyMap();
272-
dmap.AddIncludes(((LiteralModuleDecl)module).ModuleDef.Includes);
272+
dmap.AddIncludes(module.ModuleDef.Includes);
273273
dmap.PrintMap();
274274
}
275275

Source/DafnyPipeline.Test/FormatterBaseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected void FormatterWorksFor(string testCase, string? expectedProgramString
5454
var uri = new Uri("virtual:virtual");
5555
var outerModule = new DefaultModuleDefinition(new List<Uri>() { uri });
5656
BatchErrorReporter reporter = new BatchErrorReporter(options, outerModule);
57-
ModuleDecl module = new LiteralModuleDecl(outerModule, null);
57+
var module = new LiteralModuleDecl(outerModule, null);
5858
Microsoft.Dafny.Type.ResetScopes();
5959
BuiltIns builtIns = new BuiltIns(options);
6060
Parser.Parse(programNotIndented, uri, module, builtIns, reporter);

Source/DafnyServer/DafnyHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public bool Verify() {
4040
private bool Parse() {
4141
var uri = new Uri("transcript:///" + fname);
4242
var defaultModuleDefinition = new DefaultModuleDefinition(new List<Uri>() { uri });
43-
ModuleDecl module = new LiteralModuleDecl(defaultModuleDefinition, null);
43+
var module = new LiteralModuleDecl(defaultModuleDefinition, null);
4444
reporter = new ConsoleErrorReporter(options, defaultModuleDefinition);
4545
BuiltIns builtIns = new BuiltIns(options);
4646
var success = (Parser.Parse(source, uri, module, builtIns, new Errors(reporter)) == 0 &&
@@ -152,4 +152,4 @@ private static string ConvertToJson<T>(T data) {
152152
}
153153
}
154154
}
155-
}
155+
}

Source/DafnyTestGeneration/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static Type CopyWithReplacements(Type type, List<string> from, List<Type>
7676
public static Program/*?*/ Parse(DafnyOptions options, string source, bool resolve = true, Uri uri = null) {
7777
uri ??= new Uri(Path.GetTempPath());
7878
var defaultModuleDefinition = new DefaultModuleDefinition(new List<Uri>() { uri });
79-
ModuleDecl module = new LiteralModuleDecl(defaultModuleDefinition, null);
79+
var module = new LiteralModuleDecl(defaultModuleDefinition, null);
8080
var builtIns = new BuiltIns(options);
8181
var reporter = new BatchErrorReporter(options, defaultModuleDefinition);
8282
var success = Parser.Parse(source, uri, module, builtIns,

0 commit comments

Comments
 (0)