-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BoundedInts to DafnyStdLibs #4713
Changes from all commits
fddf31f
e67f203
501df4f
9cd59e4
d1a66e1
e5313e7
0c9b812
a56eb7e
3067c61
4bad250
4aa7f0b
c33b8ef
6f032e3
5038892
6522a7e
a6a4fa5
ef5cd0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ string FormatDefaultTypeParameterValue(TopLevelDecl tp) { | |
return $"_default_{tp.GetCompileName(Options)}"; | ||
} | ||
|
||
private readonly List<Import> Imports = new List<Import>(StandardImports); | ||
private readonly List<Import> Imports = new(StandardImports); | ||
private string ModuleName; | ||
private ConcreteSyntaxTree RootImportWriter; | ||
private ConcreteSyntaxTree RootImportDummyWriter; | ||
|
@@ -52,7 +52,7 @@ string FormatDefaultTypeParameterValue(TopLevelDecl tp) { | |
|
||
private struct Import { | ||
public string Name, Path; | ||
public bool SuppressDummy; | ||
public ModuleDefinition ExternModule; | ||
} | ||
|
||
protected override void EmitHeader(Program program, ConcreteSyntaxTree wr) { | ||
|
@@ -134,7 +134,7 @@ protected override ConcreteSyntaxTree CreateStaticMain(IClassWriter cw, string a | |
return wr.NewNamedBlock("func (_this * {0}) Main({1} _dafny.Sequence)", FormatCompanionTypeName(((GoCompiler.ClassWriter)cw).ClassName), argsParameterName); | ||
} | ||
|
||
private Import CreateImport(string moduleName, bool isDefault, bool isExtern, string /*?*/ libraryName) { | ||
private Import CreateImport(string moduleName, bool isDefault, ModuleDefinition externModule, string /*?*/ libraryName) { | ||
string pkgName; | ||
if (libraryName != null) { | ||
pkgName = libraryName; | ||
|
@@ -151,24 +151,18 @@ private Import CreateImport(string moduleName, bool isDefault, bool isExtern, st | |
} | ||
} | ||
|
||
var import = new Import { Name = moduleName, Path = pkgName }; | ||
if (isExtern) { | ||
// Allow the library name to be "" to import built-in things like the error type | ||
if (pkgName != "") { | ||
import.SuppressDummy = true; | ||
} | ||
} | ||
|
||
return import; | ||
return new Import { Name = moduleName, Path = pkgName, ExternModule = externModule }; | ||
} | ||
|
||
protected override ConcreteSyntaxTree CreateModule(string moduleName, bool isDefault, bool isExtern, string/*?*/ libraryName, ConcreteSyntaxTree wr) { | ||
protected override ConcreteSyntaxTree CreateModule(string moduleName, bool isDefault, | ||
ModuleDefinition externModule, | ||
string libraryName /*?*/, ConcreteSyntaxTree wr) { | ||
if (isDefault) { | ||
// Fold the default module into the main module | ||
return wr; | ||
} | ||
|
||
var import = CreateImport(moduleName, isDefault, isExtern, libraryName); | ||
var import = CreateImport(moduleName, isDefault, externModule, libraryName); | ||
|
||
var filename = string.Format("{0}/{0}.go", import.Path); | ||
var w = wr.NewFile(filename); | ||
|
@@ -180,8 +174,9 @@ protected override ConcreteSyntaxTree CreateModule(string moduleName, bool isDef | |
return w; | ||
} | ||
|
||
protected override void DependOnModule(string moduleName, bool isDefault, bool isExtern, string libraryName) { | ||
var import = CreateImport(moduleName, isDefault, isExtern, libraryName); | ||
protected override void DependOnModule(string moduleName, bool isDefault, ModuleDefinition externModule, | ||
string libraryName) { | ||
var import = CreateImport(moduleName, isDefault, externModule, libraryName); | ||
AddImport(import); | ||
} | ||
|
||
|
@@ -202,11 +197,32 @@ private void EmitImport(Import import, ConcreteSyntaxTree importWriter, Concrete | |
|
||
importWriter.WriteLine("{0} \"{1}\"", id, path); | ||
|
||
if (!import.SuppressDummy) { | ||
if (id == "os") { | ||
importDummyWriter.WriteLine("var _ = os.Args"); | ||
bool isType; | ||
string memberName = null; | ||
if (id == "os") { | ||
memberName = "Args"; | ||
isType = false; | ||
} else { | ||
isType = true; | ||
if (import.ExternModule != null) { | ||
var attributes = Attributes.Find(import.ExternModule.Attributes, "dummyImportMember"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely glad we're not documenting this officially, but it might be worth adding a note on this attribute to https://github.com/dafny-lang/dafny/blob/master/Source/DafnyStandardLibraries/CONTRIBUTING.md, just so anyone that might need to use it is at least aware of how it works. |
||
if (attributes != null && attributes.Args.Count == 2) { | ||
if (attributes.Args[0] is LiteralExpr expr1 && expr1.Value is string isNameValue && | ||
attributes.Args[1] is LiteralExpr expr2 && expr2.Value is bool isTypeValue) { | ||
memberName = isNameValue; | ||
isType = isTypeValue; | ||
} | ||
} | ||
} else { | ||
memberName = DummyTypeName; | ||
} | ||
} | ||
|
||
if (memberName != null) { | ||
if (isType) { | ||
importDummyWriter.WriteLine("var _ {0}.{1}", id, memberName); | ||
} else { | ||
importDummyWriter.WriteLine("var _ {0}.{1}", id, DummyTypeName); | ||
importDummyWriter.WriteLine("var _ = {0}.{1}", id, memberName); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,12 +167,14 @@ protected void CheckSystemModulePopulatedToCommonLimits(SystemModuleManager syst | |
/// call to the instance Main method in the enclosing class. | ||
/// </summary> | ||
protected abstract ConcreteSyntaxTree CreateStaticMain(IClassWriter wr, string argsParameterName); | ||
protected abstract ConcreteSyntaxTree CreateModule(string moduleName, bool isDefault, bool isExtern, string/*?*/ libraryName, ConcreteSyntaxTree wr); | ||
protected abstract ConcreteSyntaxTree CreateModule(string moduleName, bool isDefault, ModuleDefinition externModule, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely feels a little weird to have a parameter that is "the module being created, but only passed if it's an extern". Consider keeping |
||
string libraryName /*?*/, ConcreteSyntaxTree wr); | ||
/// <summary> | ||
/// Indicates the current program depends on the given module without creating it. | ||
/// Called when a module is out of scope for compilation, such as when using --library. | ||
/// </summary> | ||
protected virtual void DependOnModule(string moduleName, bool isDefault, bool isExtern, string/*?*/ libraryName) { } | ||
protected virtual void DependOnModule(string moduleName, bool isDefault, ModuleDefinition externModule, | ||
string libraryName /*?*/) { } | ||
protected abstract string GetHelperModuleName(); | ||
protected interface IClassWriter { | ||
ConcreteSyntaxTree/*?*/ CreateMethod(Method m, List<TypeArgumentInstantiation> typeArgs, bool createBody, bool forBodyInheritance, bool lookasideBody); | ||
|
@@ -1449,7 +1451,7 @@ private void EmitModule(Program program, ConcreteSyntaxTree programNode, ModuleD | |
|
||
DetectAndMarkCapitalizationConflicts(module); | ||
|
||
var moduleIsExtern = false; | ||
ModuleDefinition externModule = null; | ||
string libraryName = null; | ||
if (!Options.DisallowExterns) { | ||
var args = Attributes.FindExpressions(module.Attributes, "extern"); | ||
|
@@ -1458,16 +1460,16 @@ private void EmitModule(Program program, ConcreteSyntaxTree programNode, ModuleD | |
libraryName = (string)(args[1] as StringLiteralExpr)?.Value; | ||
} | ||
|
||
moduleIsExtern = true; | ||
externModule = module; | ||
} | ||
} | ||
|
||
if (!module.ShouldCompile(program.Compilation)) { | ||
DependOnModule(module.GetCompileName(Options), module.IsDefaultModule, moduleIsExtern, libraryName); | ||
DependOnModule(module.GetCompileName(Options), module.IsDefaultModule, externModule, libraryName); | ||
return; | ||
} | ||
|
||
var wr = CreateModule(module.GetCompileName(Options), module.IsDefaultModule, moduleIsExtern, libraryName, programNode); | ||
var wr = CreateModule(module.GetCompileName(Options), module.IsDefaultModule, externModule, libraryName, programNode); | ||
var v = new CheckHasNoAssumes_Visitor(this, wr); | ||
Contract.Assert(enclosingModule == null); | ||
enclosingModule = module; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-rc-3-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,11 @@ update-binary: build-binary | |
# with deeper coverage of module functionality. | ||
test: | ||
$(DAFNY) build -t:lib examples/dfyconfig.toml --output:build/stdlibexamples.doo | ||
$(DAFNY) test -t:cs build/stdlibexamples.doo --output:build/stdlibexamples | ||
$(DAFNY) test -t:java build/stdlibexamples.doo --output:build/stdlibexamples | ||
$(DAFNY) test -t:go build/stdlibexamples.doo --output:build/stdlibexamples | ||
$(DAFNY) test -t:py build/stdlibexamples.doo --output:build/stdlibexamples | ||
$(DAFNY) test -t:js build/stdlibexamples.doo --output:build/stdlibexamples | ||
$(DAFNY) test -t:cs build/stdlibexamples.doo examples/BoundedInts/NonDefault.cs --output:build/stdlibexamples | ||
$(DAFNY) test -t:java build/stdlibexamples.doo examples/BoundedInts/Externs/NonDefault.java --output:build/stdlibexamples | ||
$(DAFNY) test -t:go build/stdlibexamples.doo examples/BoundedInts/NonDefault.go --output:build/stdlibexamples | ||
$(DAFNY) test -t:py build/stdlibexamples.doo examples/BoundedInts/NonDefault.py --output:build/stdlibexamples | ||
$(DAFNY) test -t:js build/stdlibexamples.doo examples/BoundedInts/NonDefault.js --output:build/stdlibexamples | ||
Comment on lines
+26
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine for this change but probably worth replacing with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or look into target specific options in the project file |
||
|
||
format: | ||
$(DAFNY) format . | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own understanding, are the AST changes unrelated cleanup or necessary somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated, yes