diff --git a/ast/ast.fbs b/ast/ast.fbs index 53076ed84f..533d19fd19 100644 --- a/ast/ast.fbs +++ b/ast/ast.fbs @@ -2,6 +2,7 @@ namespace fbast; union MonoType { NamedType, + TvarType, ArrayType, RecordType, FunctionType, @@ -12,15 +13,20 @@ table NamedType { id:Identifier; } +table TvarType { + base_node:BaseNode; + id:Identifier; +} + table ArrayType { base_node:BaseNode; - element_type:MonoType; + element:MonoType; } table PropertyType { base_node:BaseNode; id:Identifier; - ty:MonoType; + monotype:MonoType; } table RecordType { @@ -37,15 +43,15 @@ enum ParameterKind : byte { table ParameterType { base_node:BaseNode; - name:Identifier; - ty:MonoType; + id:Identifier; + monotype:MonoType; kind:ParameterKind; } table FunctionType { base_node:BaseNode; parameters:[ParameterType]; - retn:MonoType; + monotype:MonoType; } table TypeConstraint { @@ -56,7 +62,7 @@ table TypeConstraint { table TypeExpression { base_node:BaseNode; - ty:MonoType; + monotype:MonoType; constraints:[TypeConstraint]; } diff --git a/ast/ast.go b/ast/ast.go index 727136ee0f..90acf481d1 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -181,6 +181,7 @@ type MonoType interface { } func (NamedType) monotype() {} +func (TvarType) monotype() {} func (ArrayType) monotype() {} func (RecordType) monotype() {} func (FunctionType) monotype() {} @@ -194,6 +195,15 @@ func (NamedType) Type() string { return "NamedType" } +type TvarType struct { + BaseNode + ID *Identifier +} + +func (TvarType) Type() string { + return "TvarType" +} + type ArrayType struct { BaseNode ElementType MonoType diff --git a/ast/asttest/cmpopts.go b/ast/asttest/cmpopts.go index b024614728..050e3c01ab 100644 --- a/ast/asttest/cmpopts.go +++ b/ast/asttest/cmpopts.go @@ -51,6 +51,7 @@ var IgnoreBaseNodeOptions = []cmp.Option{ cmpopts.IgnoreFields(ast.StringLiteral{}, "BaseNode"), cmpopts.IgnoreFields(ast.TestStatement{}, "BaseNode"), cmpopts.IgnoreFields(ast.TextPart{}, "BaseNode"), + cmpopts.IgnoreFields(ast.TvarType{}, "BaseNode"), cmpopts.IgnoreFields(ast.TypeConstraint{}, "BaseNode"), cmpopts.IgnoreFields(ast.TypeExpression{}, "BaseNode"), cmpopts.IgnoreFields(ast.UnaryExpression{}, "BaseNode"), diff --git a/ast/fbuffers.go b/ast/fbuffers.go index 2f68ee57b1..3f87277c6f 100644 --- a/ast/fbuffers.go +++ b/ast/fbuffers.go @@ -47,7 +47,7 @@ func (b *BaseNode) FromBuf(buf *fbast.BaseNode) { func (t TypeExpression) FromBuf(buf *fbast.TypeExpression) *TypeExpression { t.BaseNode.FromBuf(buf.BaseNode(nil)) - t.Ty = DecodeMonoType(newFBTable(buf.Ty, &t.BaseNode), buf.TyType()) + t.Ty = DecodeMonoType(newFBTable(buf.Monotype, &t.BaseNode), buf.MonotypeType()) for i := 0; i < buf.ConstraintsLength(); i++ { if c := new(fbast.TypeConstraint); !buf.Constraints(c, i) { t.BaseNode.Errors = append(t.BaseNode.Errors, Error{ @@ -81,9 +81,15 @@ func (t NamedType) FromBuf(buf *fbast.NamedType) *NamedType { return &t } +func (t TvarType) FromBuf(buf *fbast.TvarType) *TvarType { + t.BaseNode.FromBuf(buf.BaseNode(nil)) + t.ID = Identifier{}.FromBuf(buf.Id(nil)) + return &t +} + func (t ArrayType) FromBuf(buf *fbast.ArrayType) *ArrayType { t.BaseNode.FromBuf(buf.BaseNode(nil)) - t.ElementType = DecodeMonoType(newFBTable(buf.ElementType, &t.BaseNode), buf.ElementTypeType()) + t.ElementType = DecodeMonoType(newFBTable(buf.Element, &t.BaseNode), buf.ElementType()) return &t } @@ -105,13 +111,13 @@ func (t RecordType) FromBuf(buf *fbast.RecordType) *RecordType { func (p PropertyType) FromBuf(buf *fbast.PropertyType) *PropertyType { p.BaseNode.FromBuf(buf.BaseNode(nil)) p.Name = Identifier{}.FromBuf(buf.Id(nil)) - p.Ty = DecodeMonoType(newFBTable(buf.Ty, &p.BaseNode), buf.TyType()) + p.Ty = DecodeMonoType(newFBTable(buf.Monotype, &p.BaseNode), buf.MonotypeType()) return &p } func (t FunctionType) FromBuf(buf *fbast.FunctionType) *FunctionType { t.BaseNode.FromBuf(buf.BaseNode(nil)) - t.Return = DecodeMonoType(newFBTable(buf.Retn, &t.BaseNode), buf.RetnType()) + t.Return = DecodeMonoType(newFBTable(buf.Monotype, &t.BaseNode), buf.MonotypeType()) for i := 0; i < buf.ParametersLength(); i++ { if p := new(fbast.ParameterType); !buf.Parameters(p, i) { t.BaseNode.Errors = append(t.BaseNode.Errors, Error{ @@ -126,8 +132,8 @@ func (t FunctionType) FromBuf(buf *fbast.FunctionType) *FunctionType { func (p ParameterType) FromBuf(buf *fbast.ParameterType) *ParameterType { p.BaseNode.FromBuf(buf.BaseNode(nil)) - p.Name = Identifier{}.FromBuf(buf.Name(nil)) - p.Ty = DecodeMonoType(newFBTable(buf.Ty, &p.BaseNode), buf.TyType()) + p.Name = Identifier{}.FromBuf(buf.Id(nil)) + p.Ty = DecodeMonoType(newFBTable(buf.Monotype, &p.BaseNode), buf.MonotypeType()) p.Kind = paramKindMap[buf.Kind()] return &p } @@ -146,6 +152,10 @@ func DecodeMonoType(t *flatbuffers.Table, ty byte) MonoType { b := new(fbast.NamedType) b.Init(t.Bytes, t.Pos) return NamedType{}.FromBuf(b) + case fbast.MonoTypeTvarType: + b := new(fbast.TvarType) + b.Init(t.Bytes, t.Pos) + return TvarType{}.FromBuf(b) case fbast.MonoTypeArrayType: b := new(fbast.ArrayType) b.Init(t.Bytes, t.Pos) diff --git a/ast/flatbuffers_test.go b/ast/flatbuffers_test.go index 856464d084..cb453c6097 100644 --- a/ast/flatbuffers_test.go +++ b/ast/flatbuffers_test.go @@ -157,6 +157,31 @@ func TestDecodeMonoType(t *testing.T) { t.Errorf("unexpected AST -want/+got:\n%s", cmp.Diff(want, got, CompareOptions...)) } }) + t.Run("tvar", func(t *testing.T) { + b := flatbuffers.NewBuilder(1024) + + name := b.CreateString("T") + + fbast.IdentifierStart(b) + fbast.IdentifierAddName(b, name) + id := fbast.IdentifierEnd(b) + + fbast.TvarTypeStart(b) + fbast.TvarTypeAddId(b, id) + tv := fbast.TvarTypeEnd(b) + + b.Finish(tv) + fbt := fbast.GetRootAsTvarType(b.FinishedBytes(), 0) + tbl := fbt.Table() + + want := &ast.TvarType{ + ID: &ast.Identifier{Name: "T"}, + } + + if got := ast.DecodeMonoType(&tbl, fbast.MonoTypeTvarType); !cmp.Equal(want, got, CompareOptions...) { + t.Errorf("unexpected AST -want/+got:\n%s", cmp.Diff(want, got, CompareOptions...)) + } + }) t.Run("array", func(t *testing.T) { b := flatbuffers.NewBuilder(1024) @@ -171,8 +196,8 @@ func TestDecodeMonoType(t *testing.T) { el := fbast.NamedTypeEnd(b) fbast.ArrayTypeStart(b) - fbast.ArrayTypeAddElementTypeType(b, fbast.MonoTypeNamedType) - fbast.ArrayTypeAddElementType(b, el) + fbast.ArrayTypeAddElementType(b, fbast.MonoTypeNamedType) + fbast.ArrayTypeAddElement(b, el) ty := fbast.ArrayTypeEnd(b) b.Finish(ty) @@ -226,8 +251,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.PropertyTypeStart(b) fbast.PropertyTypeAddId(b, label) - fbast.PropertyTypeAddTy(b, basic) - fbast.PropertyTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.PropertyTypeAddMonotype(b, basic) + fbast.PropertyTypeAddMonotypeType(b, fbast.MonoTypeNamedType) p := fbast.PropertyTypeEnd(b) fbast.RecordTypeStartPropertiesVector(b, 1) @@ -278,8 +303,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.PropertyTypeStart(b) fbast.PropertyTypeAddId(b, label) - fbast.PropertyTypeAddTy(b, basic) - fbast.PropertyTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.PropertyTypeAddMonotype(b, basic) + fbast.PropertyTypeAddMonotypeType(b, fbast.MonoTypeNamedType) p := fbast.PropertyTypeEnd(b) fbast.RecordTypeStartPropertiesVector(b, 1) @@ -337,8 +362,8 @@ func TestDecodeMonoType(t *testing.T) { retn := fbast.NamedTypeEnd(b) fbast.FunctionTypeStart(b) - fbast.FunctionTypeAddRetn(b, retn) - fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType) + fbast.FunctionTypeAddMonotype(b, retn) + fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeNamedType) f := fbast.FunctionTypeEnd(b) b.Finish(f) @@ -370,8 +395,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.ParameterTypeStart(b) fbast.ParameterTypeAddKind(b, fbast.ParameterKindPipe) - fbast.ParameterTypeAddTy(b, ty) - fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.ParameterTypeAddMonotype(b, ty) + fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType) pipe := fbast.ParameterTypeEnd(b) fbast.FunctionTypeStartParametersVector(b, 1) @@ -380,8 +405,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.FunctionTypeStart(b) fbast.FunctionTypeAddParameters(b, params) - fbast.FunctionTypeAddRetn(b, ty) - fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType) + fbast.FunctionTypeAddMonotype(b, ty) + fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeNamedType) f := fbast.FunctionTypeEnd(b) b.Finish(f) @@ -429,9 +454,9 @@ func TestDecodeMonoType(t *testing.T) { fbast.ParameterTypeStart(b) fbast.ParameterTypeAddKind(b, fbast.ParameterKindPipe) - fbast.ParameterTypeAddName(b, pipeParam) - fbast.ParameterTypeAddTy(b, ty) - fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.ParameterTypeAddId(b, pipeParam) + fbast.ParameterTypeAddMonotype(b, ty) + fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType) pipe := fbast.ParameterTypeEnd(b) name = b.CreateString("a") @@ -442,9 +467,9 @@ func TestDecodeMonoType(t *testing.T) { fbast.ParameterTypeStart(b) fbast.ParameterTypeAddKind(b, fbast.ParameterKindRequired) - fbast.ParameterTypeAddName(b, requiredParam) - fbast.ParameterTypeAddTy(b, ty) - fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.ParameterTypeAddId(b, requiredParam) + fbast.ParameterTypeAddMonotype(b, ty) + fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType) req := fbast.ParameterTypeEnd(b) name = b.CreateString("b") @@ -455,9 +480,9 @@ func TestDecodeMonoType(t *testing.T) { fbast.ParameterTypeStart(b) fbast.ParameterTypeAddKind(b, fbast.ParameterKindOptional) - fbast.ParameterTypeAddName(b, optionalParam) - fbast.ParameterTypeAddTy(b, ty) - fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.ParameterTypeAddId(b, optionalParam) + fbast.ParameterTypeAddMonotype(b, ty) + fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeNamedType) opt := fbast.ParameterTypeEnd(b) fbast.FunctionTypeStartParametersVector(b, 3) @@ -468,8 +493,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.FunctionTypeStart(b) fbast.FunctionTypeAddParameters(b, params) - fbast.FunctionTypeAddRetn(b, ty) - fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType) + fbast.FunctionTypeAddMonotype(b, ty) + fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeNamedType) f := fbast.FunctionTypeEnd(b) b.Finish(f) @@ -530,9 +555,9 @@ func TestDecodeMonoType(t *testing.T) { fbast.IdentifierAddName(b, name) id := fbast.IdentifierEnd(b) - fbast.NamedTypeStart(b) - fbast.NamedTypeAddId(b, id) - tvar := fbast.NamedTypeEnd(b) + fbast.TvarTypeStart(b) + fbast.TvarTypeAddId(b, id) + tvar := fbast.TvarTypeEnd(b) name = b.CreateString("x") @@ -542,9 +567,9 @@ func TestDecodeMonoType(t *testing.T) { fbast.ParameterTypeStart(b) fbast.ParameterTypeAddKind(b, fbast.ParameterKindRequired) - fbast.ParameterTypeAddName(b, x) - fbast.ParameterTypeAddTy(b, tvar) - fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.ParameterTypeAddId(b, x) + fbast.ParameterTypeAddMonotype(b, tvar) + fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeTvarType) x = fbast.ParameterTypeEnd(b) name = b.CreateString("y") @@ -555,9 +580,9 @@ func TestDecodeMonoType(t *testing.T) { fbast.ParameterTypeStart(b) fbast.ParameterTypeAddKind(b, fbast.ParameterKindRequired) - fbast.ParameterTypeAddName(b, y) - fbast.ParameterTypeAddTy(b, tvar) - fbast.ParameterTypeAddTyType(b, fbast.MonoTypeNamedType) + fbast.ParameterTypeAddId(b, y) + fbast.ParameterTypeAddMonotype(b, tvar) + fbast.ParameterTypeAddMonotypeType(b, fbast.MonoTypeTvarType) y = fbast.ParameterTypeEnd(b) fbast.FunctionTypeStartParametersVector(b, 2) @@ -567,8 +592,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.FunctionTypeStart(b) fbast.FunctionTypeAddParameters(b, params) - fbast.FunctionTypeAddRetn(b, tvar) - fbast.FunctionTypeAddRetnType(b, fbast.MonoTypeNamedType) + fbast.FunctionTypeAddMonotype(b, tvar) + fbast.FunctionTypeAddMonotypeType(b, fbast.MonoTypeTvarType) f := fbast.FunctionTypeEnd(b) name = b.CreateString("Addable") @@ -599,8 +624,8 @@ func TestDecodeMonoType(t *testing.T) { fbast.TypeExpressionStart(b) fbast.TypeExpressionAddConstraints(b, constraints) - fbast.TypeExpressionAddTy(b, f) - fbast.TypeExpressionAddTyType(b, fbast.MonoTypeFunctionType) + fbast.TypeExpressionAddMonotype(b, f) + fbast.TypeExpressionAddMonotypeType(b, fbast.MonoTypeFunctionType) texpr := fbast.TypeExpressionEnd(b) b.Finish(texpr) @@ -613,7 +638,7 @@ func TestDecodeMonoType(t *testing.T) { Name: &ast.Identifier{ Name: "x", }, - Ty: &ast.NamedType{ + Ty: &ast.TvarType{ ID: &ast.Identifier{ Name: "T", }, @@ -624,7 +649,7 @@ func TestDecodeMonoType(t *testing.T) { Name: &ast.Identifier{ Name: "y", }, - Ty: &ast.NamedType{ + Ty: &ast.TvarType{ ID: &ast.Identifier{ Name: "T", }, @@ -632,7 +657,7 @@ func TestDecodeMonoType(t *testing.T) { Kind: ast.Required, }, }, - Return: &ast.NamedType{ + Return: &ast.TvarType{ ID: &ast.Identifier{ Name: "T", }, diff --git a/ast/internal/fbast/ast_generated.go b/ast/internal/fbast/ast_generated.go index 06c27b3aa1..d68b2a7acf 100644 --- a/ast/internal/fbast/ast_generated.go +++ b/ast/internal/fbast/ast_generated.go @@ -11,14 +11,16 @@ type MonoType = byte const ( MonoTypeNONE MonoType = 0 MonoTypeNamedType MonoType = 1 - MonoTypeArrayType MonoType = 2 - MonoTypeRecordType MonoType = 3 - MonoTypeFunctionType MonoType = 4 + MonoTypeTvarType MonoType = 2 + MonoTypeArrayType MonoType = 3 + MonoTypeRecordType MonoType = 4 + MonoTypeFunctionType MonoType = 5 ) var EnumNamesMonoType = map[MonoType]string{ MonoTypeNONE: "NONE", MonoTypeNamedType: "NamedType", + MonoTypeTvarType: "TvarType", MonoTypeArrayType: "ArrayType", MonoTypeRecordType: "RecordType", MonoTypeFunctionType: "FunctionType", @@ -313,6 +315,65 @@ func NamedTypeEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +type TvarType struct { + _tab flatbuffers.Table +} + +func GetRootAsTvarType(buf []byte, offset flatbuffers.UOffsetT) *TvarType { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &TvarType{} + x.Init(buf, n+offset) + return x +} + +func (rcv *TvarType) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *TvarType) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *TvarType) BaseNode(obj *BaseNode) *BaseNode { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(BaseNode) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func (rcv *TvarType) Id(obj *Identifier) *Identifier { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(Identifier) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func TvarTypeStart(builder *flatbuffers.Builder) { + builder.StartObject(2) +} +func TvarTypeAddBaseNode(builder *flatbuffers.Builder, baseNode flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(baseNode), 0) +} +func TvarTypeAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(id), 0) +} +func TvarTypeEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} + type ArrayType struct { _tab flatbuffers.Table } @@ -346,7 +407,7 @@ func (rcv *ArrayType) BaseNode(obj *BaseNode) *BaseNode { return nil } -func (rcv *ArrayType) ElementTypeType() byte { +func (rcv *ArrayType) ElementType() byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) if o != 0 { return rcv._tab.GetByte(o + rcv._tab.Pos) @@ -354,11 +415,11 @@ func (rcv *ArrayType) ElementTypeType() byte { return 0 } -func (rcv *ArrayType) MutateElementTypeType(n byte) bool { +func (rcv *ArrayType) MutateElementType(n byte) bool { return rcv._tab.MutateByteSlot(6, n) } -func (rcv *ArrayType) ElementType(obj *flatbuffers.Table) bool { +func (rcv *ArrayType) Element(obj *flatbuffers.Table) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) if o != 0 { rcv._tab.Union(obj, o) @@ -373,11 +434,11 @@ func ArrayTypeStart(builder *flatbuffers.Builder) { func ArrayTypeAddBaseNode(builder *flatbuffers.Builder, baseNode flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(baseNode), 0) } -func ArrayTypeAddElementTypeType(builder *flatbuffers.Builder, elementTypeType byte) { - builder.PrependByteSlot(1, elementTypeType, 0) +func ArrayTypeAddElementType(builder *flatbuffers.Builder, elementType byte) { + builder.PrependByteSlot(1, elementType, 0) } -func ArrayTypeAddElementType(builder *flatbuffers.Builder, elementType flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(elementType), 0) +func ArrayTypeAddElement(builder *flatbuffers.Builder, element flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(element), 0) } func ArrayTypeEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() @@ -429,7 +490,7 @@ func (rcv *PropertyType) Id(obj *Identifier) *Identifier { return nil } -func (rcv *PropertyType) TyType() byte { +func (rcv *PropertyType) MonotypeType() byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) if o != 0 { return rcv._tab.GetByte(o + rcv._tab.Pos) @@ -437,11 +498,11 @@ func (rcv *PropertyType) TyType() byte { return 0 } -func (rcv *PropertyType) MutateTyType(n byte) bool { +func (rcv *PropertyType) MutateMonotypeType(n byte) bool { return rcv._tab.MutateByteSlot(8, n) } -func (rcv *PropertyType) Ty(obj *flatbuffers.Table) bool { +func (rcv *PropertyType) Monotype(obj *flatbuffers.Table) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) if o != 0 { rcv._tab.Union(obj, o) @@ -459,11 +520,11 @@ func PropertyTypeAddBaseNode(builder *flatbuffers.Builder, baseNode flatbuffers. func PropertyTypeAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(id), 0) } -func PropertyTypeAddTyType(builder *flatbuffers.Builder, tyType byte) { - builder.PrependByteSlot(2, tyType, 0) +func PropertyTypeAddMonotypeType(builder *flatbuffers.Builder, monotypeType byte) { + builder.PrependByteSlot(2, monotypeType, 0) } -func PropertyTypeAddTy(builder *flatbuffers.Builder, ty flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(ty), 0) +func PropertyTypeAddMonotype(builder *flatbuffers.Builder, monotype flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(monotype), 0) } func PropertyTypeEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() @@ -587,7 +648,7 @@ func (rcv *ParameterType) BaseNode(obj *BaseNode) *BaseNode { return nil } -func (rcv *ParameterType) Name(obj *Identifier) *Identifier { +func (rcv *ParameterType) Id(obj *Identifier) *Identifier { o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) if o != 0 { x := rcv._tab.Indirect(o + rcv._tab.Pos) @@ -600,7 +661,7 @@ func (rcv *ParameterType) Name(obj *Identifier) *Identifier { return nil } -func (rcv *ParameterType) TyType() byte { +func (rcv *ParameterType) MonotypeType() byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) if o != 0 { return rcv._tab.GetByte(o + rcv._tab.Pos) @@ -608,11 +669,11 @@ func (rcv *ParameterType) TyType() byte { return 0 } -func (rcv *ParameterType) MutateTyType(n byte) bool { +func (rcv *ParameterType) MutateMonotypeType(n byte) bool { return rcv._tab.MutateByteSlot(8, n) } -func (rcv *ParameterType) Ty(obj *flatbuffers.Table) bool { +func (rcv *ParameterType) Monotype(obj *flatbuffers.Table) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) if o != 0 { rcv._tab.Union(obj, o) @@ -639,14 +700,14 @@ func ParameterTypeStart(builder *flatbuffers.Builder) { func ParameterTypeAddBaseNode(builder *flatbuffers.Builder, baseNode flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(baseNode), 0) } -func ParameterTypeAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(name), 0) +func ParameterTypeAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(id), 0) } -func ParameterTypeAddTyType(builder *flatbuffers.Builder, tyType byte) { - builder.PrependByteSlot(2, tyType, 0) +func ParameterTypeAddMonotypeType(builder *flatbuffers.Builder, monotypeType byte) { + builder.PrependByteSlot(2, monotypeType, 0) } -func ParameterTypeAddTy(builder *flatbuffers.Builder, ty flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(ty), 0) +func ParameterTypeAddMonotype(builder *flatbuffers.Builder, monotype flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(monotype), 0) } func ParameterTypeAddKind(builder *flatbuffers.Builder, kind int8) { builder.PrependInt8Slot(4, kind, 0) @@ -708,7 +769,7 @@ func (rcv *FunctionType) ParametersLength() int { return 0 } -func (rcv *FunctionType) RetnType() byte { +func (rcv *FunctionType) MonotypeType() byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) if o != 0 { return rcv._tab.GetByte(o + rcv._tab.Pos) @@ -716,11 +777,11 @@ func (rcv *FunctionType) RetnType() byte { return 0 } -func (rcv *FunctionType) MutateRetnType(n byte) bool { +func (rcv *FunctionType) MutateMonotypeType(n byte) bool { return rcv._tab.MutateByteSlot(8, n) } -func (rcv *FunctionType) Retn(obj *flatbuffers.Table) bool { +func (rcv *FunctionType) Monotype(obj *flatbuffers.Table) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) if o != 0 { rcv._tab.Union(obj, o) @@ -741,11 +802,11 @@ func FunctionTypeAddParameters(builder *flatbuffers.Builder, parameters flatbuff func FunctionTypeStartParametersVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 4) } -func FunctionTypeAddRetnType(builder *flatbuffers.Builder, retnType byte) { - builder.PrependByteSlot(2, retnType, 0) +func FunctionTypeAddMonotypeType(builder *flatbuffers.Builder, monotypeType byte) { + builder.PrependByteSlot(2, monotypeType, 0) } -func FunctionTypeAddRetn(builder *flatbuffers.Builder, retn flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(retn), 0) +func FunctionTypeAddMonotype(builder *flatbuffers.Builder, monotype flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(monotype), 0) } func FunctionTypeEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() @@ -869,7 +930,7 @@ func (rcv *TypeExpression) BaseNode(obj *BaseNode) *BaseNode { return nil } -func (rcv *TypeExpression) TyType() byte { +func (rcv *TypeExpression) MonotypeType() byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) if o != 0 { return rcv._tab.GetByte(o + rcv._tab.Pos) @@ -877,11 +938,11 @@ func (rcv *TypeExpression) TyType() byte { return 0 } -func (rcv *TypeExpression) MutateTyType(n byte) bool { +func (rcv *TypeExpression) MutateMonotypeType(n byte) bool { return rcv._tab.MutateByteSlot(6, n) } -func (rcv *TypeExpression) Ty(obj *flatbuffers.Table) bool { +func (rcv *TypeExpression) Monotype(obj *flatbuffers.Table) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) if o != 0 { rcv._tab.Union(obj, o) @@ -916,11 +977,11 @@ func TypeExpressionStart(builder *flatbuffers.Builder) { func TypeExpressionAddBaseNode(builder *flatbuffers.Builder, baseNode flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(baseNode), 0) } -func TypeExpressionAddTyType(builder *flatbuffers.Builder, tyType byte) { - builder.PrependByteSlot(1, tyType, 0) +func TypeExpressionAddMonotypeType(builder *flatbuffers.Builder, monotypeType byte) { + builder.PrependByteSlot(1, monotypeType, 0) } -func TypeExpressionAddTy(builder *flatbuffers.Builder, ty flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(ty), 0) +func TypeExpressionAddMonotype(builder *flatbuffers.Builder, monotype flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(monotype), 0) } func TypeExpressionAddConstraints(builder *flatbuffers.Builder, constraints flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(constraints), 0) diff --git a/libflux/go/libflux/buildinfo.gen.go b/libflux/go/libflux/buildinfo.gen.go index fb62093d19..b495e42011 100644 --- a/libflux/go/libflux/buildinfo.gen.go +++ b/libflux/go/libflux/buildinfo.gen.go @@ -19,8 +19,9 @@ var sourceHashes = map[string]string{ "libflux/src/core/Cargo.toml": "4f3093731281c0ef02151230f1bd83eb1a1f9b1a1cff0fc88ebe6b5b1fe2b6d7", "libflux/src/core/ast/check/mod.rs": "59de9596b266fd254785a81c1e1dbbd2ed18a2b66bafc9a4c9e51dba72cb8422", "libflux/src/core/ast/check/tests.rs": "9ca9abb71c9c2aa20b75b3aa1220304c5c04f5a59d8adc74c604659d940866ea", - "libflux/src/core/ast/flatbuffers/ast_generated.rs": "5c4422f2ec849d519101ab35727220d09af72b87ff82f0653786b1d7969a7b7b", - "libflux/src/core/ast/flatbuffers/mod.rs": "1f09831f45eee630101b0318e496fedd5730ce6020c3e22c18acac831259c4f3", + "libflux/src/core/ast/flatbuffers/ast_generated.rs": "50236dfa9b6d57cc24f6d967ffe93db9a618c67dc93cef9dbc9f7df9c43cbac0", + "libflux/src/core/ast/flatbuffers/mod.rs": "0fb5a31d08ee92eb090a4b0c814a2a90889b9557bc4a7062e40599e518840d7a", + "libflux/src/core/ast/flatbuffers/monotype.rs": "75a59399e8b3b5d16f0d34b49dd9db54958065dc819cc55788c47372376b9d14", "libflux/src/core/ast/flatbuffers/tests.rs": "131f266613fd39cfcbc72c41691b68f58a14ee3a994d45f011da70c263f16f26", "libflux/src/core/ast/mod.rs": "f59fdda97e8d9cdac84402b61ca98862254d8adadd4e9a49d4b3bba6124526e3", "libflux/src/core/ast/tests.rs": "16e6838a45ca4012572ff22603590fe898a9f373996112403a03e2170a92a7ab", diff --git a/libflux/src/core/ast/flatbuffers/ast_generated.rs b/libflux/src/core/ast/flatbuffers/ast_generated.rs index 948b762224..4a590b3322 100644 --- a/libflux/src/core/ast/flatbuffers/ast_generated.rs +++ b/libflux/src/core/ast/flatbuffers/ast_generated.rs @@ -21,13 +21,14 @@ pub mod fbast { pub enum MonoType { NONE = 0, NamedType = 1, - ArrayType = 2, - RecordType = 3, - FunctionType = 4, + TvarType = 2, + ArrayType = 3, + RecordType = 4, + FunctionType = 5, } const ENUM_MIN_MONO_TYPE: u8 = 0; - const ENUM_MAX_MONO_TYPE: u8 = 4; + const ENUM_MAX_MONO_TYPE: u8 = 5; impl<'a> flatbuffers::Follow<'a> for MonoType { type Inner = Self; @@ -61,18 +62,20 @@ pub mod fbast { } #[allow(non_camel_case_types)] - const ENUM_VALUES_MONO_TYPE: [MonoType; 5] = [ + const ENUM_VALUES_MONO_TYPE: [MonoType; 6] = [ MonoType::NONE, MonoType::NamedType, + MonoType::TvarType, MonoType::ArrayType, MonoType::RecordType, MonoType::FunctionType, ]; #[allow(non_camel_case_types)] - const ENUM_NAMES_MONO_TYPE: [&'static str; 5] = [ + const ENUM_NAMES_MONO_TYPE: [&'static str; 6] = [ "NONE", "NamedType", + "TvarType", "ArrayType", "RecordType", "FunctionType", @@ -931,6 +934,104 @@ pub mod fbast { } } + pub enum TvarTypeOffset {} + #[derive(Copy, Clone, Debug, PartialEq)] + + pub struct TvarType<'a> { + pub _tab: flatbuffers::Table<'a>, + } + + impl<'a> flatbuffers::Follow<'a> for TvarType<'a> { + type Inner = TvarType<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { + _tab: flatbuffers::Table { buf: buf, loc: loc }, + } + } + } + + impl<'a> TvarType<'a> { + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TvarType { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TvarTypeArgs<'args>, + ) -> flatbuffers::WIPOffset> { + let mut builder = TvarTypeBuilder::new(_fbb); + if let Some(x) = args.id { + builder.add_id(x); + } + if let Some(x) = args.base_node { + builder.add_base_node(x); + } + builder.finish() + } + + pub const VT_BASE_NODE: flatbuffers::VOffsetT = 4; + pub const VT_ID: flatbuffers::VOffsetT = 6; + + #[inline] + pub fn base_node(&self) -> Option> { + self._tab + .get::>>(TvarType::VT_BASE_NODE, None) + } + #[inline] + pub fn id(&self) -> Option> { + self._tab + .get::>>(TvarType::VT_ID, None) + } + } + + pub struct TvarTypeArgs<'a> { + pub base_node: Option>>, + pub id: Option>>, + } + impl<'a> Default for TvarTypeArgs<'a> { + #[inline] + fn default() -> Self { + TvarTypeArgs { + base_node: None, + id: None, + } + } + } + pub struct TvarTypeBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, + } + impl<'a: 'b, 'b> TvarTypeBuilder<'a, 'b> { + #[inline] + pub fn add_base_node(&mut self, base_node: flatbuffers::WIPOffset>) { + self.fbb_ + .push_slot_always::>( + TvarType::VT_BASE_NODE, + base_node, + ); + } + #[inline] + pub fn add_id(&mut self, id: flatbuffers::WIPOffset>) { + self.fbb_ + .push_slot_always::>(TvarType::VT_ID, id); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TvarTypeBuilder<'a, 'b> { + let start = _fbb.start_table(); + TvarTypeBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } + } + pub enum ArrayTypeOffset {} #[derive(Copy, Clone, Debug, PartialEq)] @@ -959,19 +1060,19 @@ pub mod fbast { args: &'args ArrayTypeArgs<'args>, ) -> flatbuffers::WIPOffset> { let mut builder = ArrayTypeBuilder::new(_fbb); - if let Some(x) = args.element_type { - builder.add_element_type(x); + if let Some(x) = args.element { + builder.add_element(x); } if let Some(x) = args.base_node { builder.add_base_node(x); } - builder.add_element_type_type(args.element_type_type); + builder.add_element_type(args.element_type); builder.finish() } pub const VT_BASE_NODE: flatbuffers::VOffsetT = 4; - pub const VT_ELEMENT_TYPE_TYPE: flatbuffers::VOffsetT = 6; - pub const VT_ELEMENT_TYPE: flatbuffers::VOffsetT = 8; + pub const VT_ELEMENT_TYPE: flatbuffers::VOffsetT = 6; + pub const VT_ELEMENT: flatbuffers::VOffsetT = 8; #[inline] pub fn base_node(&self) -> Option> { @@ -979,24 +1080,34 @@ pub mod fbast { .get::>>(ArrayType::VT_BASE_NODE, None) } #[inline] - pub fn element_type_type(&self) -> MonoType { + pub fn element_type(&self) -> MonoType { self._tab - .get::(ArrayType::VT_ELEMENT_TYPE_TYPE, Some(MonoType::NONE)) + .get::(ArrayType::VT_ELEMENT_TYPE, Some(MonoType::NONE)) .unwrap() } #[inline] - pub fn element_type(&self) -> Option> { + pub fn element(&self) -> Option> { self._tab .get::>>( - ArrayType::VT_ELEMENT_TYPE, + ArrayType::VT_ELEMENT, None, ) } #[inline] #[allow(non_snake_case)] - pub fn element_type_as_named_type(&self) -> Option> { - if self.element_type_type() == MonoType::NamedType { - self.element_type().map(|u| NamedType::init_from_table(u)) + pub fn element_as_named_type(&self) -> Option> { + if self.element_type() == MonoType::NamedType { + self.element().map(|u| NamedType::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn element_as_tvar_type(&self) -> Option> { + if self.element_type() == MonoType::TvarType { + self.element().map(|u| TvarType::init_from_table(u)) } else { None } @@ -1004,9 +1115,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn element_type_as_array_type(&self) -> Option> { - if self.element_type_type() == MonoType::ArrayType { - self.element_type().map(|u| ArrayType::init_from_table(u)) + pub fn element_as_array_type(&self) -> Option> { + if self.element_type() == MonoType::ArrayType { + self.element().map(|u| ArrayType::init_from_table(u)) } else { None } @@ -1014,9 +1125,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn element_type_as_record_type(&self) -> Option> { - if self.element_type_type() == MonoType::RecordType { - self.element_type().map(|u| RecordType::init_from_table(u)) + pub fn element_as_record_type(&self) -> Option> { + if self.element_type() == MonoType::RecordType { + self.element().map(|u| RecordType::init_from_table(u)) } else { None } @@ -1024,10 +1135,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn element_type_as_function_type(&self) -> Option> { - if self.element_type_type() == MonoType::FunctionType { - self.element_type() - .map(|u| FunctionType::init_from_table(u)) + pub fn element_as_function_type(&self) -> Option> { + if self.element_type() == MonoType::FunctionType { + self.element().map(|u| FunctionType::init_from_table(u)) } else { None } @@ -1036,16 +1146,16 @@ pub mod fbast { pub struct ArrayTypeArgs<'a> { pub base_node: Option>>, - pub element_type_type: MonoType, - pub element_type: Option>, + pub element_type: MonoType, + pub element: Option>, } impl<'a> Default for ArrayTypeArgs<'a> { #[inline] fn default() -> Self { ArrayTypeArgs { base_node: None, - element_type_type: MonoType::NONE, - element_type: None, + element_type: MonoType::NONE, + element: None, } } } @@ -1063,22 +1173,20 @@ pub mod fbast { ); } #[inline] - pub fn add_element_type_type(&mut self, element_type_type: MonoType) { + pub fn add_element_type(&mut self, element_type: MonoType) { self.fbb_.push_slot::( - ArrayType::VT_ELEMENT_TYPE_TYPE, - element_type_type, + ArrayType::VT_ELEMENT_TYPE, + element_type, MonoType::NONE, ); } #[inline] - pub fn add_element_type( + pub fn add_element( &mut self, - element_type: flatbuffers::WIPOffset, + element: flatbuffers::WIPOffset, ) { - self.fbb_.push_slot_always::>( - ArrayType::VT_ELEMENT_TYPE, - element_type, - ); + self.fbb_ + .push_slot_always::>(ArrayType::VT_ELEMENT, element); } #[inline] pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ArrayTypeBuilder<'a, 'b> { @@ -1123,8 +1231,8 @@ pub mod fbast { args: &'args PropertyTypeArgs<'args>, ) -> flatbuffers::WIPOffset> { let mut builder = PropertyTypeBuilder::new(_fbb); - if let Some(x) = args.ty { - builder.add_ty(x); + if let Some(x) = args.monotype { + builder.add_monotype(x); } if let Some(x) = args.id { builder.add_id(x); @@ -1132,14 +1240,14 @@ pub mod fbast { if let Some(x) = args.base_node { builder.add_base_node(x); } - builder.add_ty_type(args.ty_type); + builder.add_monotype_type(args.monotype_type); builder.finish() } pub const VT_BASE_NODE: flatbuffers::VOffsetT = 4; pub const VT_ID: flatbuffers::VOffsetT = 6; - pub const VT_TY_TYPE: flatbuffers::VOffsetT = 8; - pub const VT_TY: flatbuffers::VOffsetT = 10; + pub const VT_MONOTYPE_TYPE: flatbuffers::VOffsetT = 8; + pub const VT_MONOTYPE: flatbuffers::VOffsetT = 10; #[inline] pub fn base_node(&self) -> Option> { @@ -1152,24 +1260,24 @@ pub mod fbast { .get::>>(PropertyType::VT_ID, None) } #[inline] - pub fn ty_type(&self) -> MonoType { + pub fn monotype_type(&self) -> MonoType { self._tab - .get::(PropertyType::VT_TY_TYPE, Some(MonoType::NONE)) + .get::(PropertyType::VT_MONOTYPE_TYPE, Some(MonoType::NONE)) .unwrap() } #[inline] - pub fn ty(&self) -> Option> { + pub fn monotype(&self) -> Option> { self._tab .get::>>( - PropertyType::VT_TY, + PropertyType::VT_MONOTYPE, None, ) } #[inline] #[allow(non_snake_case)] - pub fn ty_as_named_type(&self) -> Option> { - if self.ty_type() == MonoType::NamedType { - self.ty().map(|u| NamedType::init_from_table(u)) + pub fn monotype_as_named_type(&self) -> Option> { + if self.monotype_type() == MonoType::NamedType { + self.monotype().map(|u| NamedType::init_from_table(u)) } else { None } @@ -1177,9 +1285,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_array_type(&self) -> Option> { - if self.ty_type() == MonoType::ArrayType { - self.ty().map(|u| ArrayType::init_from_table(u)) + pub fn monotype_as_tvar_type(&self) -> Option> { + if self.monotype_type() == MonoType::TvarType { + self.monotype().map(|u| TvarType::init_from_table(u)) } else { None } @@ -1187,9 +1295,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_record_type(&self) -> Option> { - if self.ty_type() == MonoType::RecordType { - self.ty().map(|u| RecordType::init_from_table(u)) + pub fn monotype_as_array_type(&self) -> Option> { + if self.monotype_type() == MonoType::ArrayType { + self.monotype().map(|u| ArrayType::init_from_table(u)) } else { None } @@ -1197,9 +1305,19 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_function_type(&self) -> Option> { - if self.ty_type() == MonoType::FunctionType { - self.ty().map(|u| FunctionType::init_from_table(u)) + pub fn monotype_as_record_type(&self) -> Option> { + if self.monotype_type() == MonoType::RecordType { + self.monotype().map(|u| RecordType::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn monotype_as_function_type(&self) -> Option> { + if self.monotype_type() == MonoType::FunctionType { + self.monotype().map(|u| FunctionType::init_from_table(u)) } else { None } @@ -1209,8 +1327,8 @@ pub mod fbast { pub struct PropertyTypeArgs<'a> { pub base_node: Option>>, pub id: Option>>, - pub ty_type: MonoType, - pub ty: Option>, + pub monotype_type: MonoType, + pub monotype: Option>, } impl<'a> Default for PropertyTypeArgs<'a> { #[inline] @@ -1218,8 +1336,8 @@ pub mod fbast { PropertyTypeArgs { base_node: None, id: None, - ty_type: MonoType::NONE, - ty: None, + monotype_type: MonoType::NONE, + monotype: None, } } } @@ -1242,14 +1360,20 @@ pub mod fbast { .push_slot_always::>(PropertyType::VT_ID, id); } #[inline] - pub fn add_ty_type(&mut self, ty_type: MonoType) { - self.fbb_ - .push_slot::(PropertyType::VT_TY_TYPE, ty_type, MonoType::NONE); + pub fn add_monotype_type(&mut self, monotype_type: MonoType) { + self.fbb_.push_slot::( + PropertyType::VT_MONOTYPE_TYPE, + monotype_type, + MonoType::NONE, + ); } #[inline] - pub fn add_ty(&mut self, ty: flatbuffers::WIPOffset) { + pub fn add_monotype( + &mut self, + monotype: flatbuffers::WIPOffset, + ) { self.fbb_ - .push_slot_always::>(PropertyType::VT_TY, ty); + .push_slot_always::>(PropertyType::VT_MONOTYPE, monotype); } #[inline] pub fn new( @@ -1425,24 +1549,24 @@ pub mod fbast { args: &'args ParameterTypeArgs<'args>, ) -> flatbuffers::WIPOffset> { let mut builder = ParameterTypeBuilder::new(_fbb); - if let Some(x) = args.ty { - builder.add_ty(x); + if let Some(x) = args.monotype { + builder.add_monotype(x); } - if let Some(x) = args.name { - builder.add_name(x); + if let Some(x) = args.id { + builder.add_id(x); } if let Some(x) = args.base_node { builder.add_base_node(x); } builder.add_kind(args.kind); - builder.add_ty_type(args.ty_type); + builder.add_monotype_type(args.monotype_type); builder.finish() } pub const VT_BASE_NODE: flatbuffers::VOffsetT = 4; - pub const VT_NAME: flatbuffers::VOffsetT = 6; - pub const VT_TY_TYPE: flatbuffers::VOffsetT = 8; - pub const VT_TY: flatbuffers::VOffsetT = 10; + pub const VT_ID: flatbuffers::VOffsetT = 6; + pub const VT_MONOTYPE_TYPE: flatbuffers::VOffsetT = 8; + pub const VT_MONOTYPE: flatbuffers::VOffsetT = 10; pub const VT_KIND: flatbuffers::VOffsetT = 12; #[inline] @@ -1453,21 +1577,21 @@ pub mod fbast { ) } #[inline] - pub fn name(&self) -> Option> { + pub fn id(&self) -> Option> { self._tab - .get::>>(ParameterType::VT_NAME, None) + .get::>>(ParameterType::VT_ID, None) } #[inline] - pub fn ty_type(&self) -> MonoType { + pub fn monotype_type(&self) -> MonoType { self._tab - .get::(ParameterType::VT_TY_TYPE, Some(MonoType::NONE)) + .get::(ParameterType::VT_MONOTYPE_TYPE, Some(MonoType::NONE)) .unwrap() } #[inline] - pub fn ty(&self) -> Option> { + pub fn monotype(&self) -> Option> { self._tab .get::>>( - ParameterType::VT_TY, + ParameterType::VT_MONOTYPE, None, ) } @@ -1479,9 +1603,9 @@ pub mod fbast { } #[inline] #[allow(non_snake_case)] - pub fn ty_as_named_type(&self) -> Option> { - if self.ty_type() == MonoType::NamedType { - self.ty().map(|u| NamedType::init_from_table(u)) + pub fn monotype_as_named_type(&self) -> Option> { + if self.monotype_type() == MonoType::NamedType { + self.monotype().map(|u| NamedType::init_from_table(u)) } else { None } @@ -1489,9 +1613,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_array_type(&self) -> Option> { - if self.ty_type() == MonoType::ArrayType { - self.ty().map(|u| ArrayType::init_from_table(u)) + pub fn monotype_as_tvar_type(&self) -> Option> { + if self.monotype_type() == MonoType::TvarType { + self.monotype().map(|u| TvarType::init_from_table(u)) } else { None } @@ -1499,9 +1623,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_record_type(&self) -> Option> { - if self.ty_type() == MonoType::RecordType { - self.ty().map(|u| RecordType::init_from_table(u)) + pub fn monotype_as_array_type(&self) -> Option> { + if self.monotype_type() == MonoType::ArrayType { + self.monotype().map(|u| ArrayType::init_from_table(u)) } else { None } @@ -1509,9 +1633,19 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_function_type(&self) -> Option> { - if self.ty_type() == MonoType::FunctionType { - self.ty().map(|u| FunctionType::init_from_table(u)) + pub fn monotype_as_record_type(&self) -> Option> { + if self.monotype_type() == MonoType::RecordType { + self.monotype().map(|u| RecordType::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn monotype_as_function_type(&self) -> Option> { + if self.monotype_type() == MonoType::FunctionType { + self.monotype().map(|u| FunctionType::init_from_table(u)) } else { None } @@ -1520,9 +1654,9 @@ pub mod fbast { pub struct ParameterTypeArgs<'a> { pub base_node: Option>>, - pub name: Option>>, - pub ty_type: MonoType, - pub ty: Option>, + pub id: Option>>, + pub monotype_type: MonoType, + pub monotype: Option>, pub kind: ParameterKind, } impl<'a> Default for ParameterTypeArgs<'a> { @@ -1530,9 +1664,9 @@ pub mod fbast { fn default() -> Self { ParameterTypeArgs { base_node: None, - name: None, - ty_type: MonoType::NONE, - ty: None, + id: None, + monotype_type: MonoType::NONE, + monotype: None, kind: ParameterKind::Required, } } @@ -1551,22 +1685,27 @@ pub mod fbast { ); } #[inline] - pub fn add_name(&mut self, name: flatbuffers::WIPOffset>) { + pub fn add_id(&mut self, id: flatbuffers::WIPOffset>) { self.fbb_ - .push_slot_always::>( - ParameterType::VT_NAME, - name, - ); + .push_slot_always::>(ParameterType::VT_ID, id); } #[inline] - pub fn add_ty_type(&mut self, ty_type: MonoType) { - self.fbb_ - .push_slot::(ParameterType::VT_TY_TYPE, ty_type, MonoType::NONE); + pub fn add_monotype_type(&mut self, monotype_type: MonoType) { + self.fbb_.push_slot::( + ParameterType::VT_MONOTYPE_TYPE, + monotype_type, + MonoType::NONE, + ); } #[inline] - pub fn add_ty(&mut self, ty: flatbuffers::WIPOffset) { - self.fbb_ - .push_slot_always::>(ParameterType::VT_TY, ty); + pub fn add_monotype( + &mut self, + monotype: flatbuffers::WIPOffset, + ) { + self.fbb_.push_slot_always::>( + ParameterType::VT_MONOTYPE, + monotype, + ); } #[inline] pub fn add_kind(&mut self, kind: ParameterKind) { @@ -1621,8 +1760,8 @@ pub mod fbast { args: &'args FunctionTypeArgs<'args>, ) -> flatbuffers::WIPOffset> { let mut builder = FunctionTypeBuilder::new(_fbb); - if let Some(x) = args.retn { - builder.add_retn(x); + if let Some(x) = args.monotype { + builder.add_monotype(x); } if let Some(x) = args.parameters { builder.add_parameters(x); @@ -1630,14 +1769,14 @@ pub mod fbast { if let Some(x) = args.base_node { builder.add_base_node(x); } - builder.add_retn_type(args.retn_type); + builder.add_monotype_type(args.monotype_type); builder.finish() } pub const VT_BASE_NODE: flatbuffers::VOffsetT = 4; pub const VT_PARAMETERS: flatbuffers::VOffsetT = 6; - pub const VT_RETN_TYPE: flatbuffers::VOffsetT = 8; - pub const VT_RETN: flatbuffers::VOffsetT = 10; + pub const VT_MONOTYPE_TYPE: flatbuffers::VOffsetT = 8; + pub const VT_MONOTYPE: flatbuffers::VOffsetT = 10; #[inline] pub fn base_node(&self) -> Option> { @@ -1654,24 +1793,34 @@ pub mod fbast { >>(FunctionType::VT_PARAMETERS, None) } #[inline] - pub fn retn_type(&self) -> MonoType { + pub fn monotype_type(&self) -> MonoType { self._tab - .get::(FunctionType::VT_RETN_TYPE, Some(MonoType::NONE)) + .get::(FunctionType::VT_MONOTYPE_TYPE, Some(MonoType::NONE)) .unwrap() } #[inline] - pub fn retn(&self) -> Option> { + pub fn monotype(&self) -> Option> { self._tab .get::>>( - FunctionType::VT_RETN, + FunctionType::VT_MONOTYPE, None, ) } #[inline] #[allow(non_snake_case)] - pub fn retn_as_named_type(&self) -> Option> { - if self.retn_type() == MonoType::NamedType { - self.retn().map(|u| NamedType::init_from_table(u)) + pub fn monotype_as_named_type(&self) -> Option> { + if self.monotype_type() == MonoType::NamedType { + self.monotype().map(|u| NamedType::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn monotype_as_tvar_type(&self) -> Option> { + if self.monotype_type() == MonoType::TvarType { + self.monotype().map(|u| TvarType::init_from_table(u)) } else { None } @@ -1679,9 +1828,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn retn_as_array_type(&self) -> Option> { - if self.retn_type() == MonoType::ArrayType { - self.retn().map(|u| ArrayType::init_from_table(u)) + pub fn monotype_as_array_type(&self) -> Option> { + if self.monotype_type() == MonoType::ArrayType { + self.monotype().map(|u| ArrayType::init_from_table(u)) } else { None } @@ -1689,9 +1838,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn retn_as_record_type(&self) -> Option> { - if self.retn_type() == MonoType::RecordType { - self.retn().map(|u| RecordType::init_from_table(u)) + pub fn monotype_as_record_type(&self) -> Option> { + if self.monotype_type() == MonoType::RecordType { + self.monotype().map(|u| RecordType::init_from_table(u)) } else { None } @@ -1699,9 +1848,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn retn_as_function_type(&self) -> Option> { - if self.retn_type() == MonoType::FunctionType { - self.retn().map(|u| FunctionType::init_from_table(u)) + pub fn monotype_as_function_type(&self) -> Option> { + if self.monotype_type() == MonoType::FunctionType { + self.monotype().map(|u| FunctionType::init_from_table(u)) } else { None } @@ -1715,8 +1864,8 @@ pub mod fbast { flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset>>, >, >, - pub retn_type: MonoType, - pub retn: Option>, + pub monotype_type: MonoType, + pub monotype: Option>, } impl<'a> Default for FunctionTypeArgs<'a> { #[inline] @@ -1724,8 +1873,8 @@ pub mod fbast { FunctionTypeArgs { base_node: None, parameters: None, - retn_type: MonoType::NONE, - retn: None, + monotype_type: MonoType::NONE, + monotype: None, } } } @@ -1755,14 +1904,20 @@ pub mod fbast { ); } #[inline] - pub fn add_retn_type(&mut self, retn_type: MonoType) { - self.fbb_ - .push_slot::(FunctionType::VT_RETN_TYPE, retn_type, MonoType::NONE); + pub fn add_monotype_type(&mut self, monotype_type: MonoType) { + self.fbb_.push_slot::( + FunctionType::VT_MONOTYPE_TYPE, + monotype_type, + MonoType::NONE, + ); } #[inline] - pub fn add_retn(&mut self, retn: flatbuffers::WIPOffset) { + pub fn add_monotype( + &mut self, + monotype: flatbuffers::WIPOffset, + ) { self.fbb_ - .push_slot_always::>(FunctionType::VT_RETN, retn); + .push_slot_always::>(FunctionType::VT_MONOTYPE, monotype); } #[inline] pub fn new( @@ -1945,19 +2100,19 @@ pub mod fbast { if let Some(x) = args.constraints { builder.add_constraints(x); } - if let Some(x) = args.ty { - builder.add_ty(x); + if let Some(x) = args.monotype { + builder.add_monotype(x); } if let Some(x) = args.base_node { builder.add_base_node(x); } - builder.add_ty_type(args.ty_type); + builder.add_monotype_type(args.monotype_type); builder.finish() } pub const VT_BASE_NODE: flatbuffers::VOffsetT = 4; - pub const VT_TY_TYPE: flatbuffers::VOffsetT = 6; - pub const VT_TY: flatbuffers::VOffsetT = 8; + pub const VT_MONOTYPE_TYPE: flatbuffers::VOffsetT = 6; + pub const VT_MONOTYPE: flatbuffers::VOffsetT = 8; pub const VT_CONSTRAINTS: flatbuffers::VOffsetT = 10; #[inline] @@ -1968,16 +2123,16 @@ pub mod fbast { ) } #[inline] - pub fn ty_type(&self) -> MonoType { + pub fn monotype_type(&self) -> MonoType { self._tab - .get::(TypeExpression::VT_TY_TYPE, Some(MonoType::NONE)) + .get::(TypeExpression::VT_MONOTYPE_TYPE, Some(MonoType::NONE)) .unwrap() } #[inline] - pub fn ty(&self) -> Option> { + pub fn monotype(&self) -> Option> { self._tab .get::>>( - TypeExpression::VT_TY, + TypeExpression::VT_MONOTYPE, None, ) } @@ -1992,9 +2147,19 @@ pub mod fbast { } #[inline] #[allow(non_snake_case)] - pub fn ty_as_named_type(&self) -> Option> { - if self.ty_type() == MonoType::NamedType { - self.ty().map(|u| NamedType::init_from_table(u)) + pub fn monotype_as_named_type(&self) -> Option> { + if self.monotype_type() == MonoType::NamedType { + self.monotype().map(|u| NamedType::init_from_table(u)) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn monotype_as_tvar_type(&self) -> Option> { + if self.monotype_type() == MonoType::TvarType { + self.monotype().map(|u| TvarType::init_from_table(u)) } else { None } @@ -2002,9 +2167,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_array_type(&self) -> Option> { - if self.ty_type() == MonoType::ArrayType { - self.ty().map(|u| ArrayType::init_from_table(u)) + pub fn monotype_as_array_type(&self) -> Option> { + if self.monotype_type() == MonoType::ArrayType { + self.monotype().map(|u| ArrayType::init_from_table(u)) } else { None } @@ -2012,9 +2177,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_record_type(&self) -> Option> { - if self.ty_type() == MonoType::RecordType { - self.ty().map(|u| RecordType::init_from_table(u)) + pub fn monotype_as_record_type(&self) -> Option> { + if self.monotype_type() == MonoType::RecordType { + self.monotype().map(|u| RecordType::init_from_table(u)) } else { None } @@ -2022,9 +2187,9 @@ pub mod fbast { #[inline] #[allow(non_snake_case)] - pub fn ty_as_function_type(&self) -> Option> { - if self.ty_type() == MonoType::FunctionType { - self.ty().map(|u| FunctionType::init_from_table(u)) + pub fn monotype_as_function_type(&self) -> Option> { + if self.monotype_type() == MonoType::FunctionType { + self.monotype().map(|u| FunctionType::init_from_table(u)) } else { None } @@ -2033,8 +2198,8 @@ pub mod fbast { pub struct TypeExpressionArgs<'a> { pub base_node: Option>>, - pub ty_type: MonoType, - pub ty: Option>, + pub monotype_type: MonoType, + pub monotype: Option>, pub constraints: Option< flatbuffers::WIPOffset< flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset>>, @@ -2046,8 +2211,8 @@ pub mod fbast { fn default() -> Self { TypeExpressionArgs { base_node: None, - ty_type: MonoType::NONE, - ty: None, + monotype_type: MonoType::NONE, + monotype: None, constraints: None, } } @@ -2066,14 +2231,22 @@ pub mod fbast { ); } #[inline] - pub fn add_ty_type(&mut self, ty_type: MonoType) { - self.fbb_ - .push_slot::(TypeExpression::VT_TY_TYPE, ty_type, MonoType::NONE); + pub fn add_monotype_type(&mut self, monotype_type: MonoType) { + self.fbb_.push_slot::( + TypeExpression::VT_MONOTYPE_TYPE, + monotype_type, + MonoType::NONE, + ); } #[inline] - pub fn add_ty(&mut self, ty: flatbuffers::WIPOffset) { - self.fbb_ - .push_slot_always::>(TypeExpression::VT_TY, ty); + pub fn add_monotype( + &mut self, + monotype: flatbuffers::WIPOffset, + ) { + self.fbb_.push_slot_always::>( + TypeExpression::VT_MONOTYPE, + monotype, + ); } #[inline] pub fn add_constraints( diff --git a/libflux/src/core/ast/flatbuffers/mod.rs b/libflux/src/core/ast/flatbuffers/mod.rs index 238dba6a89..e5da50591e 100644 --- a/libflux/src/core/ast/flatbuffers/mod.rs +++ b/libflux/src/core/ast/flatbuffers/mod.rs @@ -1,6 +1,9 @@ #[allow(non_snake_case, unused, clippy::all)] mod ast_generated; +#[cfg(test)] +mod monotype; + use std::cell::RefCell; use std::rc::Rc; diff --git a/libflux/src/core/ast/flatbuffers/monotype.rs b/libflux/src/core/ast/flatbuffers/monotype.rs new file mode 100644 index 0000000000..7ba85fa941 --- /dev/null +++ b/libflux/src/core/ast/flatbuffers/monotype.rs @@ -0,0 +1,884 @@ +//! Flatbuffer serialization for MonoType AST nodes +//! +use crate::ast::flatbuffers::ast_generated::fbast as fb; + +#[rustfmt::skip] +use crate::ast::{ + SourceLocation, + BaseNode, + TypeExpression, + TypeConstraint, + MonoType, + Identifier, + NamedType, + TvarType, + ArrayType, + PropertyType, + RecordType, + ParameterType, + FunctionType, +}; + +fn build_vec(v: Vec, b: &mut B, f: F) -> Vec +where + F: Fn(&mut B, T) -> S, +{ + let mut mapped = Vec::new(); + for t in v { + mapped.push(f(b, t)); + } + mapped +} + +fn build_base_node<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + base_node: BaseNode, +) -> flatbuffers::WIPOffset> { + let loc = Some(build_loc(builder, base_node.location)); + let errors = build_vec(base_node.errors, builder, |builder, s| { + builder.create_string(&s) + }); + let errors = Some(builder.create_vector(errors.as_slice())); + fb::BaseNode::create(builder, &fb::BaseNodeArgs { loc, errors }) +} + +fn build_loc<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + loc: SourceLocation, +) -> flatbuffers::WIPOffset> { + let file = match loc.file { + None => None, + Some(name) => Some(builder.create_string(&name)), + }; + let source = match loc.source { + None => None, + Some(src) => Some(builder.create_string(&src)), + }; + fb::SourceLocation::create( + builder, + &fb::SourceLocationArgs { + file, + start: Some(&fb::Position::new( + loc.start.line as i32, + loc.start.column as i32, + )), + end: Some(&fb::Position::new( + loc.end.line as i32, + loc.end.column as i32, + )), + source, + }, + ) +} + +fn build_type_expression<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + expr: TypeExpression, +) -> flatbuffers::WIPOffset> { + let base_node = build_base_node(builder, expr.base); + let (offset, t) = build_monotype(builder, expr.monotype); + let constraints = build_vec(expr.constraints, builder, build_type_constraint); + let constraints = builder.create_vector(constraints.as_slice()); + fb::TypeExpression::create( + builder, + &fb::TypeExpressionArgs { + base_node: Some(base_node), + monotype: Some(offset), + monotype_type: t, + constraints: Some(constraints), + }, + ) +} + +fn build_type_constraint<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + c: TypeConstraint, +) -> flatbuffers::WIPOffset> { + let base_node = build_base_node(builder, c.base); + let tvar = build_identifier(builder, c.tvar); + let kinds = build_vec(c.kinds, builder, build_identifier); + let kinds = builder.create_vector(kinds.as_slice()); + fb::TypeConstraint::create( + builder, + &fb::TypeConstraintArgs { + base_node: Some(base_node), + tvar: Some(tvar), + kinds: Some(kinds), + }, + ) +} + +fn build_monotype<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + t: MonoType, +) -> ( + flatbuffers::WIPOffset, + fb::MonoType, +) { + match t { + MonoType::Basic(t) => { + let offset = build_named_type(builder, t); + (offset.as_union_value(), fb::MonoType::NamedType) + } + MonoType::Tvar(t) => { + let offset = build_tvar_type(builder, t); + (offset.as_union_value(), fb::MonoType::TvarType) + } + MonoType::Array(t) => { + let offset = build_array_type(builder, *t); + (offset.as_union_value(), fb::MonoType::ArrayType) + } + MonoType::Record(t) => { + let offset = build_record_type(builder, t); + (offset.as_union_value(), fb::MonoType::RecordType) + } + MonoType::Function(t) => { + let offset = build_function_type(builder, *t); + (offset.as_union_value(), fb::MonoType::FunctionType) + } + } +} + +fn build_identifier<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + id: Identifier, +) -> flatbuffers::WIPOffset> { + let base_node = Some(build_base_node(builder, id.base)); + let name = Some(builder.create_string(&id.name)); + fb::Identifier::create(builder, &fb::IdentifierArgs { base_node, name }) +} + +fn build_named_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + t: NamedType, +) -> flatbuffers::WIPOffset> { + let base_node = Some(build_base_node(builder, t.base)); + let id = Some(build_identifier(builder, t.name)); + fb::NamedType::create(builder, &fb::NamedTypeArgs { base_node, id }) +} + +fn build_tvar_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + t: TvarType, +) -> flatbuffers::WIPOffset> { + let base_node = Some(build_base_node(builder, t.base)); + let id = Some(build_identifier(builder, t.name)); + fb::TvarType::create(builder, &fb::TvarTypeArgs { base_node, id }) +} + +fn build_array_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + a: ArrayType, +) -> flatbuffers::WIPOffset> { + let base_node = build_base_node(builder, a.base); + let (offset, t) = build_monotype(builder, a.element); + fb::ArrayType::create( + builder, + &fb::ArrayTypeArgs { + base_node: Some(base_node), + element: Some(offset), + element_type: t, + }, + ) +} + +fn build_record_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + r: RecordType, +) -> flatbuffers::WIPOffset> { + let base_node = Some(build_base_node(builder, r.base)); + let tvar = match r.tvar { + None => None, + Some(id) => Some(build_identifier(builder, id)), + }; + let properties = build_vec(r.properties, builder, build_property_type); + let properties = Some(builder.create_vector(properties.as_slice())); + fb::RecordType::create( + builder, + &fb::RecordTypeArgs { + base_node, + tvar, + properties, + }, + ) +} + +fn build_property_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + p: PropertyType, +) -> flatbuffers::WIPOffset> { + let base_node = build_base_node(builder, p.base); + let id = build_identifier(builder, p.name); + let (offset, t) = build_monotype(builder, p.monotype); + fb::PropertyType::create( + builder, + &fb::PropertyTypeArgs { + base_node: Some(base_node), + id: Some(id), + monotype: Some(offset), + monotype_type: t, + }, + ) +} + +fn build_function_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + f: FunctionType, +) -> flatbuffers::WIPOffset> { + let base_node = Some(build_base_node(builder, f.base)); + let parameters = build_vec(f.parameters, builder, build_parameter_type); + let parameters = Some(builder.create_vector(parameters.as_slice())); + let (offset, t) = build_monotype(builder, f.monotype); + fb::FunctionType::create( + builder, + &fb::FunctionTypeArgs { + base_node, + parameters, + monotype: Some(offset), + monotype_type: t, + }, + ) +} + +fn build_parameter_type<'a>( + builder: &mut flatbuffers::FlatBufferBuilder<'a>, + p: ParameterType, +) -> flatbuffers::WIPOffset> { + match p { + ParameterType::Required { + base, + name, + monotype, + } => { + let base_node = build_base_node(builder, base); + let id = build_identifier(builder, name); + let (offset, t) = build_monotype(builder, monotype); + fb::ParameterType::create( + builder, + &fb::ParameterTypeArgs { + base_node: Some(base_node), + id: Some(id), + monotype: Some(offset), + monotype_type: t, + kind: fb::ParameterKind::Required, + }, + ) + } + ParameterType::Optional { + base, + name, + monotype, + } => { + let base_node = build_base_node(builder, base); + let id = build_identifier(builder, name); + let (offset, t) = build_monotype(builder, monotype); + fb::ParameterType::create( + builder, + &fb::ParameterTypeArgs { + base_node: Some(base_node), + id: Some(id), + monotype: Some(offset), + monotype_type: t, + kind: fb::ParameterKind::Optional, + }, + ) + } + ParameterType::Pipe { + base, + name: Some(id), + monotype, + } => { + let base_node = build_base_node(builder, base); + let id = build_identifier(builder, id); + let (offset, t) = build_monotype(builder, monotype); + fb::ParameterType::create( + builder, + &fb::ParameterTypeArgs { + base_node: Some(base_node), + id: Some(id), + monotype: Some(offset), + monotype_type: t, + kind: fb::ParameterKind::Pipe, + }, + ) + } + ParameterType::Pipe { + base, + name: None, + monotype, + } => { + let base_node = build_base_node(builder, base); + let (offset, t) = build_monotype(builder, monotype); + fb::ParameterType::create( + builder, + &fb::ParameterTypeArgs { + base_node: Some(base_node), + id: None, + monotype: Some(offset), + monotype_type: t, + kind: fb::ParameterKind::Pipe, + }, + ) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn monotype_from_table(table: flatbuffers::Table, t: fb::MonoType) -> MonoType { + match t { + fb::MonoType::NamedType => { + MonoType::Basic(fb::NamedType::init_from_table(table).into()) + } + fb::MonoType::TvarType => MonoType::Tvar(fb::TvarType::init_from_table(table).into()), + fb::MonoType::ArrayType => { + MonoType::Array(Box::new(fb::ArrayType::init_from_table(table).into())) + } + fb::MonoType::RecordType => { + MonoType::Record(fb::RecordType::init_from_table(table).into()) + } + fb::MonoType::FunctionType => { + MonoType::Function(Box::new(fb::FunctionType::init_from_table(table).into())) + } + fb::MonoType::NONE => unimplemented!("cannot convert fb::MonoType::NONE"), + } + } + impl From> for Identifier { + fn from(id: fb::Identifier) -> Identifier { + Identifier { + base: BaseNode::default(), + name: id.name().unwrap().to_string(), + } + } + } + impl From> for NamedType { + fn from(t: fb::NamedType) -> NamedType { + NamedType { + base: BaseNode::default(), + name: t.id().unwrap().into(), + } + } + } + impl From> for TvarType { + fn from(t: fb::TvarType) -> TvarType { + TvarType { + base: BaseNode::default(), + name: t.id().unwrap().into(), + } + } + } + impl From> for ArrayType { + fn from(t: fb::ArrayType) -> ArrayType { + ArrayType { + base: BaseNode::default(), + element: monotype_from_table(t.element().unwrap(), t.element_type()), + } + } + } + impl From> for PropertyType { + fn from(p: fb::PropertyType) -> PropertyType { + PropertyType { + base: BaseNode::default(), + name: p.id().unwrap().into(), + monotype: monotype_from_table(p.monotype().unwrap(), p.monotype_type()), + } + } + } + impl From> for RecordType { + fn from(t: fb::RecordType) -> RecordType { + let mut properties = Vec::new(); + for p in t.properties().unwrap().iter() { + properties.push(p.into()); + } + RecordType { + base: BaseNode::default(), + tvar: match t.tvar() { + None => None, + Some(id) => Some(id.into()), + }, + properties, + } + } + } + impl From> for ParameterType { + fn from(p: fb::ParameterType) -> ParameterType { + match p.kind() { + fb::ParameterKind::Required => ParameterType::Required { + base: BaseNode::default(), + name: p.id().unwrap().into(), + monotype: monotype_from_table(p.monotype().unwrap(), p.monotype_type()), + }, + fb::ParameterKind::Optional => ParameterType::Optional { + base: BaseNode::default(), + name: p.id().unwrap().into(), + monotype: monotype_from_table(p.monotype().unwrap(), p.monotype_type()), + }, + fb::ParameterKind::Pipe => ParameterType::Pipe { + base: BaseNode::default(), + name: match p.id() { + None => None, + Some(id) => Some(id.into()), + }, + monotype: monotype_from_table(p.monotype().unwrap(), p.monotype_type()), + }, + } + } + } + impl From> for FunctionType { + fn from(t: fb::FunctionType) -> FunctionType { + let mut parameters = Vec::new(); + for p in t.parameters().unwrap().iter() { + parameters.push(p.into()); + } + FunctionType { + base: BaseNode::default(), + parameters, + monotype: monotype_from_table(t.monotype().unwrap(), t.monotype_type()), + } + } + } + impl From> for TypeConstraint { + fn from(c: fb::TypeConstraint) -> TypeConstraint { + let mut kinds = Vec::new(); + for id in c.kinds().unwrap().iter() { + kinds.push(id.into()); + } + TypeConstraint { + base: BaseNode::default(), + tvar: c.tvar().unwrap().into(), + kinds, + } + } + } + impl From> for TypeExpression { + fn from(t: fb::TypeExpression) -> TypeExpression { + let mut constraints = Vec::new(); + for c in t.constraints().unwrap().iter() { + constraints.push(c.into()); + } + TypeExpression { + base: BaseNode::default(), + monotype: monotype_from_table(t.monotype().unwrap(), t.monotype_type()), + constraints, + } + } + } + + fn serialize<'a, 'b, T, S, F>( + builder: &'a mut flatbuffers::FlatBufferBuilder<'b>, + t: T, + f: F, + ) -> &'a [u8] + where + F: Fn(&mut flatbuffers::FlatBufferBuilder<'b>, T) -> flatbuffers::WIPOffset, + { + let offset = f(builder, t); + builder.finish(offset, None); + builder.finished_data() + } + + #[test] + fn test_named_type() { + let want = NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_named_type + )) + .into() + ); + } + #[test] + fn test_tvar_type() { + let want = TvarType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "T".to_string(), + }, + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_tvar_type + )) + .into() + ); + } + #[test] + fn test_array_type() { + let want = ArrayType { + base: BaseNode::default(), + element: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_array_type, + )) + .into() + ); + } + #[test] + fn test_empty_record_type() { + let want = RecordType { + base: BaseNode::default(), + tvar: None, + properties: vec![], + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_record_type, + )) + .into() + ); + } + #[test] + fn test_record_type() { + let want = RecordType { + base: BaseNode::default(), + tvar: None, + properties: vec![ + PropertyType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "a".to_string(), + }, + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }, + PropertyType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "b".to_string(), + }, + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "string".to_string(), + }, + }), + }, + ], + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_record_type, + )) + .into() + ); + } + #[test] + fn test_record_extension_type() { + let want = RecordType { + base: BaseNode::default(), + tvar: Some(Identifier { + base: BaseNode::default(), + name: "T".to_string(), + }), + properties: vec![ + PropertyType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "a".to_string(), + }, + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }, + PropertyType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "b".to_string(), + }, + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "string".to_string(), + }, + }), + }, + ], + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_record_type, + )) + .into() + ); + } + #[test] + fn test_function_type_no_params() { + let want = FunctionType { + base: BaseNode::default(), + parameters: vec![], + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_function_type, + )) + .into() + ); + } + #[test] + fn test_function_type_many_params() { + let want = FunctionType { + base: BaseNode::default(), + parameters: vec![ + ParameterType::Pipe { + base: BaseNode::default(), + name: Some(Identifier { + base: BaseNode::default(), + name: "tables".to_string(), + }), + monotype: MonoType::Array(Box::new(ArrayType { + base: BaseNode::default(), + element: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + })), + }, + ParameterType::Required { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "x".to_string(), + }, + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }, + ParameterType::Optional { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "y".to_string(), + }, + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "bool".to_string(), + }, + }), + }, + ], + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_function_type, + )) + .into() + ); + } + #[test] + fn test_function_type_pipe_param() { + let want = FunctionType { + base: BaseNode::default(), + parameters: vec![ParameterType::Pipe { + base: BaseNode::default(), + name: None, + monotype: MonoType::Array(Box::new(ArrayType { + base: BaseNode::default(), + element: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + })), + }], + monotype: MonoType::Basic(NamedType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "int".to_string(), + }, + }), + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_function_type, + )) + .into() + ); + } + #[test] + fn test_type_expression() { + let want = TypeExpression { + base: BaseNode::default(), + constraints: vec![TypeConstraint { + base: BaseNode::default(), + tvar: Identifier { + base: BaseNode::default(), + name: "T".to_string(), + }, + kinds: vec![ + Identifier { + base: BaseNode::default(), + name: "Addable".to_string(), + }, + Identifier { + base: BaseNode::default(), + name: "Divisible".to_string(), + }, + ], + }], + monotype: MonoType::Function(Box::new(FunctionType { + base: BaseNode::default(), + parameters: vec![ + ParameterType::Required { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "a".to_string(), + }, + monotype: MonoType::Tvar(TvarType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "T".to_string(), + }, + }), + }, + ParameterType::Required { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "b".to_string(), + }, + monotype: MonoType::Tvar(TvarType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "T".to_string(), + }, + }), + }, + ], + monotype: MonoType::Tvar(TvarType { + base: BaseNode::default(), + name: Identifier { + base: BaseNode::default(), + name: "T".to_string(), + }, + }), + })), + }; + + let mut builder = flatbuffers::FlatBufferBuilder::new(); + + assert_eq!( + want, + flatbuffers::get_root::(serialize( + &mut builder, + want.clone(), + build_type_expression, + )) + .into() + ); + } +}