Skip to content
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

Make singleton datatypes more efficient in Go #1394

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Source/Dafny/Compilers/Compiler-go.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,11 +617,17 @@ private struct Import {

if (dt is IndDatatypeDecl) {
var wStruct = wr.NewNamedBlock("type {0} struct", name);
wStruct.WriteLine(dataName);
var fieldName = dataName;
if (dt.Ctors.Count == 1) {
// only field will be the struct for the only ctor
var ctor = dt.Ctors.First();
fieldName = name + "_" + ctor.CompileName;
}
wStruct.WriteLine(fieldName);

wr.WriteLine();
var wGet = wr.NewNamedBlock("func (_this {0}) Get() {1}", name, dataName);
wGet.WriteLine("return _this.{0}", dataName);
wGet.WriteLine("return _this.{0}", fieldName);
} else {
var wDt = wr.NewNamedBlock("type {0} struct", name);
wDt.WriteLine(ifaceName);
Expand Down Expand Up @@ -744,7 +750,11 @@ private struct Import {
var wDtor = wr.NewNamedBlock("func (_this {0}) {1}() {2}", name, FormatDatatypeDestructorName(arg.CompileName), TypeName(arg.Type, wr, arg.tok));
var n = dtor.EnclosingCtors.Count;
if (n == 1) {
wDtor.WriteLine("return _this.Get().({0}).{1}", structOfCtor(dtor.EnclosingCtors[0]), DatatypeFieldName(arg));
if (dt.Ctors.Count == 1) {
wDtor.WriteLine("return _this.{0}", DatatypeFieldName(arg));
} else {
wDtor.WriteLine("return _this.Get().({0}).{1}", structOfCtor(dtor.EnclosingCtors[0]), DatatypeFieldName(arg));
}
} else {
wDtor = wDtor.NewBlock("switch data := _this.Get().(type)");
for (int i = 0; i < n - 1; i++) {
Expand Down