Skip to content

Commit 26cfdee

Browse files
authored
Revert "Remove IncludeToken as prep for caching parsing (#3858)" (#3981)
This reverts commit eb45d23.
1 parent 298fbcb commit 26cfdee

File tree

100 files changed

+823
-840
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+823
-840
lines changed

Source/DafnyCore.Test/DooFileTest.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ public void UnknownManifestEntries() {
2929
}
3030

3131
private static Program ParseProgram(string dafnyProgramText, DafnyOptions options) {
32-
const string fullFilePath = "untitled:foo";
33-
var rootUri = new Uri(fullFilePath);
34-
var outerModule = new DefaultModuleDefinition(new List<Uri> { rootUri });
35-
var module = new LiteralModuleDecl(outerModule, null);
32+
var module = new LiteralModuleDecl(new DefaultModuleDefinition(), null);
33+
const string fullFilePath = "foo";
3634
Microsoft.Dafny.Type.ResetScopes();
3735
var builtIns = new BuiltIns(options);
38-
var errorReporter = new ConsoleErrorReporter(options, outerModule);
39-
var parseResult = Parser.Parse(dafnyProgramText, rootUri, module, builtIns, errorReporter);
36+
var errorReporter = new ConsoleErrorReporter(options);
37+
var parseResult = Parser.Parse(dafnyProgramText, fullFilePath, "foo", module, builtIns, errorReporter);
4038
Assert.Equal(0, parseResult);
4139
return new Program(fullFilePath, module, builtIns, errorReporter);
4240
}

Source/DafnyCore/AST/Cloner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public Cloner(bool cloneResolvedFields = false) {
3131

3232
public virtual ModuleDefinition CloneModuleDefinition(ModuleDefinition m, Name name) {
3333
ModuleDefinition nw;
34-
if (m is DefaultModuleDefinition defaultModuleDefinition) {
35-
nw = new DefaultModuleDefinition(defaultModuleDefinition.RootUris);
34+
if (m is DefaultModuleDefinition) {
35+
nw = new DefaultModuleDefinition();
3636
} else {
3737
nw = new ModuleDefinition(Range(m.RangeToken), name, m.PrefixIds, m.IsAbstract, m.IsFacade,
3838
m.RefinementQId, m.EnclosingModule, CloneAttributes(m.Attributes),

Source/DafnyCore/AST/DafnyAst.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ void ObjectInvariant() {
3939
Contract.Invariant(DefaultModule != null);
4040
}
4141

42-
public List<Include> Includes => DefaultModuleDef.Includes;
43-
4442
public readonly string FullName;
4543
[FilledInDuringResolution] public Dictionary<ModuleDefinition, ModuleSignature> ModuleSigs;
4644
// Resolution essentially flattens the module hierarchy, for
@@ -50,7 +48,7 @@ void ObjectInvariant() {
5048

5149
public Method MainMethod; // Method to be used as main if compiled
5250
public readonly LiteralModuleDecl DefaultModule;
53-
public readonly DefaultModuleDefinition DefaultModuleDef;
51+
public readonly ModuleDefinition DefaultModuleDef;
5452
public readonly BuiltIns BuiltIns;
5553
public DafnyOptions Options => Reporter.Options;
5654
public ErrorReporter Reporter { get; set; }
@@ -102,7 +100,7 @@ public IToken GetFirstTopLevelToken() {
102100

103101
var firstToken = DefaultModule.RootToken.Next;
104102
// We skip all included files
105-
while (firstToken is { Next: { } } && firstToken.Next.Filepath != DefaultModule.RootToken.Filepath) {
103+
while (firstToken is { Next: { } } && firstToken.Next.Filename != DefaultModule.RootToken.Filename) {
106104
firstToken = firstToken.Next;
107105
}
108106

@@ -123,13 +121,13 @@ public override IEnumerable<Assumption> Assumptions(Declaration decl) {
123121
}
124122

125123
public class Include : TokenNode, IComparable {
126-
public Uri IncluderFilename { get; }
124+
public string IncluderFilename { get; }
127125
public string IncludedFilename { get; }
128126
public string CanonicalPath { get; }
129127
public bool CompileIncludedCode { get; }
130128
public bool ErrorReported;
131129

132-
public Include(IToken tok, Uri includer, string theFilename, bool compileIncludedCode) {
130+
public Include(IToken tok, string includer, string theFilename, bool compileIncludedCode) {
133131
this.tok = tok;
134132
this.IncluderFilename = includer;
135133
this.IncludedFilename = theFilename;

Source/DafnyCore/AST/Expressions/LitPattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public LiteralExpr OptimisticallyDesugaredLit {
4343
} else {
4444
var n = (BigInteger)lit.Value;
4545
var tok = new Token(neg.tok.line, neg.tok.col) {
46-
Uri = neg.tok.Uri,
46+
Filename = neg.tok.Filename,
4747
val = "-0"
4848
};
4949
optimisticallyDesugaredLit = new LiteralExpr(tok, -n);

Source/DafnyCore/AST/Grammar/IndentationFormatter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Concurrent;
1+
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text.RegularExpressions;
@@ -24,7 +23,7 @@ public IndentationFormatter(TokenNewIndentCollector formatter) {
2423
/// by immediately processing all nodes and assigning indentations to most structural tokens
2524
/// </summary>
2625
public static IndentationFormatter ForProgram(Program program, bool reduceBlockiness = true) {
27-
var tokenNewIndentCollector = new TokenNewIndentCollector(program) {
26+
var tokenNewIndentCollector = new TokenNewIndentCollector {
2827
ReduceBlockiness = reduceBlockiness
2928
};
3029
foreach (var child in program.DefaultModuleDef.PreResolveChildren) {

Source/DafnyCore/AST/Grammar/Printer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,9 +972,9 @@ private bool PrintModeSkipFunctionOrMethod(bool IsGhost, Attributes attributes,
972972
return false;
973973
}
974974

975-
private bool PrintModeSkipGeneral(IToken tok, string fileBeingPrinted) {
975+
private bool PrintModeSkipGeneral(Bpl.IToken tok, string fileBeingPrinted) {
976976
return (printMode == PrintModes.NoIncludes || printMode == PrintModes.NoGhost)
977-
&& tok.Uri != null && fileBeingPrinted != null && tok.Uri.LocalPath != fileBeingPrinted;
977+
&& tok.filename != null && fileBeingPrinted != null && Path.GetFullPath(tok.filename) != fileBeingPrinted;
978978
}
979979

980980
public void PrintMethod(Method method, int indent, bool printSignatureOnly) {

Source/DafnyCore/AST/Grammar/TokenNewIndentCollector.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ namespace Microsoft.Dafny;
5050
* ```
5151
*/
5252
public class TokenNewIndentCollector : TopDownVisitor<int> {
53-
private readonly Program program;
54-
5553
/* If true, the indentation will be
5654
* var name := method(
5755
* x,
@@ -83,8 +81,8 @@ private Indentations PosToIndentations(int pos) {
8381
public int binOpIndent = -1;
8482
public int binOpArgIndent = -1;
8583

86-
internal TokenNewIndentCollector(Program program) {
87-
this.program = program;
84+
85+
internal TokenNewIndentCollector() {
8886
preResolve = true;
8987
}
9088

@@ -231,7 +229,7 @@ public static bool IsFollowedByNewline(IToken token) {
231229
// 'inline' is the hypothetical indentation of this token if it was on its own line
232230
// 'below' is the hypothetical indentation of a comment after that token, and of the next token if it does not have a set indentation
233231
public void SetIndentations(IToken token, int above = -1, int inline = -1, int below = -1) {
234-
if (token.WasIncluded(program) || (token.line == 0 && token.col == 0)) {
232+
if (token is IncludeToken || (token.line == 0 && token.col == 0)) {
235233
// Just ignore this token.
236234
return;
237235
}
@@ -401,7 +399,7 @@ public void SetStatementIndentation(Statement statement) {
401399
}
402400

403401
public void SetDeclIndentation(TopLevelDecl topLevelDecl, int indent) {
404-
if (topLevelDecl.tok.WasIncluded(program)) {
402+
if (topLevelDecl.tok is IncludeToken) {
405403
return;
406404
}
407405

@@ -422,7 +420,7 @@ public void SetDeclIndentation(TopLevelDecl topLevelDecl, int indent) {
422420

423421
var initialMemberIndent = declWithMembers.tok.line == 0 ? indent : indent2;
424422
foreach (var member in declWithMembers.PreResolveChildren) {
425-
if (member.Tok.WasIncluded(program)) {
423+
if (member.Tok is IncludeToken) {
426424
continue;
427425
}
428426

Source/DafnyCore/AST/IncludeHandler.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

Source/DafnyCore/AST/Tokens.cs

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics.Contracts;
5-
using System.IO;
65
using System.Text;
76

87
namespace Microsoft.Dafny;
@@ -18,14 +17,12 @@ public interface IToken : Microsoft.Boogie.IToken {
1817
string val { get; set; }
1918
bool IsValid { get; }*/
2019
string Boogie.IToken.filename {
21-
get => Uri == null ? null : Path.GetFileName(Uri.LocalPath);
22-
set => throw new NotSupportedException();
20+
get => Filename;
21+
set => Filename = value;
2322
}
2423

25-
public string ActualFilename => Uri.LocalPath;
26-
string Filepath => Uri.LocalPath;
27-
28-
Uri Uri { get; set; }
24+
public string ActualFilename { get; }
25+
string Filename { get; set; }
2926

3027
/// <summary>
3128
/// TrailingTrivia contains everything after the token,
@@ -73,9 +70,8 @@ public Token(int linenum, int colnum) {
7370

7471
public int kind { get; set; } // Used by coco, so we can't rename it to Kind
7572

76-
public string ActualFilename => Filepath;
77-
public string Filepath => Uri?.LocalPath;
78-
public Uri Uri { get; set; }
73+
public string ActualFilename => Filename;
74+
public string Filename { get; set; }
7975

8076
public int pos { get; set; } // Used by coco, so we can't rename it to Pos
8177

@@ -108,7 +104,7 @@ public IToken WithVal(string newVal) {
108104
line = line,
109105
Prev = Prev,
110106
Next = Next,
111-
Uri = Uri,
107+
Filename = Filename,
112108
kind = kind,
113109
val = newVal
114110
};
@@ -119,7 +115,7 @@ public override int GetHashCode() {
119115
}
120116

121117
public override string ToString() {
122-
return $"'{val}': {Path.GetFileName(Filepath)}@{pos} - @{line}:{col}";
118+
return $"{Filename}@{pos} - @{line}:{col}";
123119
}
124120
}
125121

@@ -140,11 +136,9 @@ public virtual int col {
140136

141137
public string ActualFilename => WrappedToken.ActualFilename;
142138

143-
public virtual string Filepath => WrappedToken.Filepath;
144-
145-
public Uri Uri {
146-
get => WrappedToken.Uri;
147-
set => WrappedToken.Uri = value;
139+
public virtual string Filename {
140+
get { return WrappedToken.Filename; }
141+
set { WrappedToken.filename = value; }
148142
}
149143

150144
public bool IsValid {
@@ -186,32 +180,6 @@ public virtual IToken Prev {
186180
}
187181

188182
public static class TokenExtensions {
189-
190-
public static string TokenToString(this Boogie.IToken tok, DafnyOptions options) {
191-
if (tok is IToken dafnyToken) {
192-
return dafnyToken.TokenToString(options);
193-
}
194-
195-
return $"{tok.filename}({tok.line},{tok.col - 1})";
196-
}
197-
198-
public static string TokenToString(this IToken tok, DafnyOptions options) {
199-
if (tok.Uri == null) {
200-
return $"({tok.line},{tok.col - 1})";
201-
}
202-
203-
var currentDirectory = Directory.GetCurrentDirectory();
204-
string filename = tok.Uri.Scheme switch {
205-
"stdin" => "<stdin>",
206-
"transcript" => Path.GetFileName(tok.Filepath),
207-
_ => options.UseBaseNameForFileName
208-
? Path.GetFileName(tok.Filepath)
209-
: (tok.Filepath.StartsWith(currentDirectory) ? Path.GetRelativePath(currentDirectory, tok.Filepath) : tok.Filepath)
210-
};
211-
212-
return $"{filename}({tok.line},{tok.col - 1})";
213-
}
214-
215183
public static RangeToken ToRange(this IToken token) {
216184
if (token is BoogieRangeToken boogieRangeToken) {
217185
return new RangeToken(boogieRangeToken.StartToken, boogieRangeToken.EndToken);
@@ -333,6 +301,54 @@ public override IToken WithVal(string newVal) {
333301
}
334302
}
335303

304+
/// <summary>
305+
/// An IncludeToken is a wrapper that indicates that the function/method was
306+
/// declared in a file that was included. Any proof obligations from such an
307+
/// included file are to be ignored.
308+
/// </summary>
309+
public class IncludeToken : TokenWrapper {
310+
public Include Include;
311+
public IncludeToken(Include include, IToken wrappedToken)
312+
: base(wrappedToken) {
313+
Contract.Requires(wrappedToken != null);
314+
this.Include = include;
315+
}
316+
317+
public override string val {
318+
get { return WrappedToken.val; }
319+
set { WrappedToken.val = value; }
320+
}
321+
322+
public override int pos {
323+
get { return WrappedToken.pos; }
324+
set { WrappedToken.pos = value; }
325+
}
326+
327+
public override int line {
328+
get { return WrappedToken.line; }
329+
set { WrappedToken.line = value; }
330+
}
331+
332+
public override int col {
333+
get { return WrappedToken.col; }
334+
set { WrappedToken.col = value; }
335+
}
336+
337+
public override IToken Prev {
338+
get { return WrappedToken.Prev; }
339+
set { WrappedToken.Prev = value; }
340+
}
341+
342+
public override IToken Next {
343+
get { return WrappedToken.Next; }
344+
set { WrappedToken.Next = value; }
345+
}
346+
347+
public override IToken WithVal(string newVal) {
348+
return new IncludeToken(Include, WrappedToken.WithVal(newVal));
349+
}
350+
}
351+
336352
/// <summary>
337353
/// A token wrapper used to produce better type checking errors
338354
/// for quantified variables. See QuantifierVar.ExtractSingleRange()

Source/DafnyCore/AST/TopLevelDeclarations.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,14 +1159,14 @@ public override IEnumerable<Assumption> Assumptions(Declaration decl) {
11591159
}
11601160

11611161
public class DefaultModuleDefinition : ModuleDefinition {
1162-
1163-
public IList<Uri> RootUris { get; }
1164-
public DefaultModuleDefinition(IList<Uri> rootUris)
1162+
public DefaultModuleDefinition()
11651163
: base(RangeToken.NoToken, new Name("_module"), new List<IToken>(), false, false, null, null, null, true, true, true) {
1166-
RootUris = rootUris;
11671164
}
1168-
1169-
public override bool IsDefaultModule => true;
1165+
public override bool IsDefaultModule {
1166+
get {
1167+
return true;
1168+
}
1169+
}
11701170
}
11711171

11721172
public abstract class TopLevelDecl : Declaration, TypeParameter.ParentType {

0 commit comments

Comments
 (0)