Skip to content

Commit

Permalink
Add Field type
Browse files Browse the repository at this point in the history
We were using a tuple, but that does not scale well when more data is
added.  I ran into this when working on annotations (#256, PR #276).  I
ran into it again when adding source information to support test
generation (#22).

The `Field` types are used by *both* `Struct`/`Record` and `Newtype`
types.
  • Loading branch information
TravisCardwell committed Dec 18, 2024
1 parent 2a16d32 commit ef97506
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 159 deletions.
20 changes: 10 additions & 10 deletions hs-bindgen/fixtures/anonymous.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
DeclData (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = ("cS1c_a",HsPrimType HsPrimCInt) ::: ("cS1c_b",HsPrimType HsPrimCInt) ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = ("cS1c_a",HsPrimType HsPrimCInt) ::: ("cS1c_b",HsPrimType HsPrimCInt) ::: VNil}) (StorableInstance {storableSizeOf = 8, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = ("cS1c_a",HsPrimType HsPrimCInt) ::: ("cS1c_b",HsPrimType HsPrimCInt) ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 4]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = ("cS1c_a",HsPrimType HsPrimCInt) ::: ("cS1c_b",HsPrimType HsPrimCInt) ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 4 1])))}))
DeclData (Struct {structName = "CS1", structConstr = "MkCS1", structFields = ("cS1_c",HsTypRef "CS1c") ::: ("cS1_d",HsPrimType HsPrimCInt) ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS1", structConstr = "MkCS1", structFields = ("cS1_c",HsTypRef "CS1c") ::: ("cS1_d",HsPrimType HsPrimCInt) ::: VNil}) (StorableInstance {storableSizeOf = 12, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS1", structConstr = "MkCS1", structFields = ("cS1_c",HsTypRef "CS1c") ::: ("cS1_d",HsPrimType HsPrimCInt) ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 8]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS1", structConstr = "MkCS1", structFields = ("cS1_c",HsTypRef "CS1c") ::: ("cS1_d",HsPrimType HsPrimCInt) ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 8 1])))}))
DeclData (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = ("cS2innerdeep_b",HsPrimType HsPrimCInt) ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = ("cS2innerdeep_b",HsPrimType HsPrimCInt) ::: VNil}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = ("cS2innerdeep_b",HsPrimType HsPrimCInt) ::: VNil})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = ("cS2innerdeep_b",HsPrimType HsPrimCInt) ::: VNil}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclData (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = ("cS2inner_a",HsPrimType HsPrimCInt) ::: ("cS2inner_deep",HsTypRef "CS2innerdeep") ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = ("cS2inner_a",HsPrimType HsPrimCInt) ::: ("cS2inner_deep",HsTypRef "CS2innerdeep") ::: VNil}) (StorableInstance {storableSizeOf = 8, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = ("cS2inner_a",HsPrimType HsPrimCInt) ::: ("cS2inner_deep",HsTypRef "CS2innerdeep") ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 4]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = ("cS2inner_a",HsPrimType HsPrimCInt) ::: ("cS2inner_deep",HsTypRef "CS2innerdeep") ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 4 1])))}))
DeclData (Struct {structName = "CS2", structConstr = "MkCS2", structFields = ("cS2_inner",HsTypRef "CS2inner") ::: ("cS2_d",HsPrimType HsPrimCInt) ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS2", structConstr = "MkCS2", structFields = ("cS2_inner",HsTypRef "CS2inner") ::: ("cS2_d",HsPrimType HsPrimCInt) ::: VNil}) (StorableInstance {storableSizeOf = 12, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS2", structConstr = "MkCS2", structFields = ("cS2_inner",HsTypRef "CS2inner") ::: ("cS2_d",HsPrimType HsPrimCInt) ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 8]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS2", structConstr = "MkCS2", structFields = ("cS2_inner",HsTypRef "CS2inner") ::: ("cS2_d",HsPrimType HsPrimCInt) ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 8 1])))}))
DeclData (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = Field {fieldName = "cS1c_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS1c_b", fieldType = HsPrimType HsPrimCInt} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = Field {fieldName = "cS1c_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS1c_b", fieldType = HsPrimType HsPrimCInt} ::: VNil}) (StorableInstance {storableSizeOf = 8, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = Field {fieldName = "cS1c_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS1c_b", fieldType = HsPrimType HsPrimCInt} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 4]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS1c", structConstr = "MkCS1c", structFields = Field {fieldName = "cS1c_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS1c_b", fieldType = HsPrimType HsPrimCInt} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 4 1])))}))
DeclData (Struct {structName = "CS1", structConstr = "MkCS1", structFields = Field {fieldName = "cS1_c", fieldType = HsTypRef "CS1c"} ::: Field {fieldName = "cS1_d", fieldType = HsPrimType HsPrimCInt} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS1", structConstr = "MkCS1", structFields = Field {fieldName = "cS1_c", fieldType = HsTypRef "CS1c"} ::: Field {fieldName = "cS1_d", fieldType = HsPrimType HsPrimCInt} ::: VNil}) (StorableInstance {storableSizeOf = 12, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS1", structConstr = "MkCS1", structFields = Field {fieldName = "cS1_c", fieldType = HsTypRef "CS1c"} ::: Field {fieldName = "cS1_d", fieldType = HsPrimType HsPrimCInt} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 8]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS1", structConstr = "MkCS1", structFields = Field {fieldName = "cS1_c", fieldType = HsTypRef "CS1c"} ::: Field {fieldName = "cS1_d", fieldType = HsPrimType HsPrimCInt} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 8 1])))}))
DeclData (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = Field {fieldName = "cS2innerdeep_b", fieldType = HsPrimType HsPrimCInt} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = Field {fieldName = "cS2innerdeep_b", fieldType = HsPrimType HsPrimCInt} ::: VNil}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = Field {fieldName = "cS2innerdeep_b", fieldType = HsPrimType HsPrimCInt} ::: VNil})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS2innerdeep", structConstr = "MkCS2innerdeep", structFields = Field {fieldName = "cS2innerdeep_b", fieldType = HsPrimType HsPrimCInt} ::: VNil}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclData (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = Field {fieldName = "cS2inner_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS2inner_deep", fieldType = HsTypRef "CS2innerdeep"} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = Field {fieldName = "cS2inner_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS2inner_deep", fieldType = HsTypRef "CS2innerdeep"} ::: VNil}) (StorableInstance {storableSizeOf = 8, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = Field {fieldName = "cS2inner_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS2inner_deep", fieldType = HsTypRef "CS2innerdeep"} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 4]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS2inner", structConstr = "MkCS2inner", structFields = Field {fieldName = "cS2inner_a", fieldType = HsPrimType HsPrimCInt} ::: Field {fieldName = "cS2inner_deep", fieldType = HsTypRef "CS2innerdeep"} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 4 1])))}))
DeclData (Struct {structName = "CS2", structConstr = "MkCS2", structFields = Field {fieldName = "cS2_inner", fieldType = HsTypRef "CS2inner"} ::: Field {fieldName = "cS2_d", fieldType = HsPrimType HsPrimCInt} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CS2", structConstr = "MkCS2", structFields = Field {fieldName = "cS2_inner", fieldType = HsTypRef "CS2inner"} ::: Field {fieldName = "cS2_d", fieldType = HsPrimType HsPrimCInt} ::: VNil}) (StorableInstance {storableSizeOf = 12, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CS2", structConstr = "MkCS2", structFields = Field {fieldName = "cS2_inner", fieldType = HsTypRef "CS2inner"} ::: Field {fieldName = "cS2_d", fieldType = HsPrimType HsPrimCInt} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 8]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CS2", structConstr = "MkCS2", structFields = Field {fieldName = "cS2_inner", fieldType = HsTypRef "CS2inner"} ::: Field {fieldName = "cS2_d", fieldType = HsPrimType HsPrimCInt} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 8 1])))}))
14 changes: 7 additions & 7 deletions hs-bindgen/fixtures/bool.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DeclNewtype (Newtype {newtypeName = "CBOOL", newtypeConstr = "MkCBOOL", newtypeField = "unCBOOL", newtypeType = HsPrimType HsPrimCBool})
DeclData (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = ("cBools1_x",HsPrimType HsPrimCBool) ::: ("cBools1_y",HsPrimType HsPrimCBool) ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = ("cBools1_x",HsPrimType HsPrimCBool) ::: ("cBools1_y",HsPrimType HsPrimCBool) ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = ("cBools1_x",HsPrimType HsPrimCBool) ::: ("cBools1_y",HsPrimType HsPrimCBool) ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = ("cBools1_x",HsPrimType HsPrimCBool) ::: ("cBools1_y",HsPrimType HsPrimCBool) ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = ("cBools2_x",HsPrimType HsPrimCBool) ::: ("cBools2_y",HsPrimType HsPrimCBool) ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = ("cBools2_x",HsPrimType HsPrimCBool) ::: ("cBools2_y",HsPrimType HsPrimCBool) ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = ("cBools2_x",HsPrimType HsPrimCBool) ::: ("cBools2_y",HsPrimType HsPrimCBool) ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = ("cBools2_x",HsPrimType HsPrimCBool) ::: ("cBools2_y",HsPrimType HsPrimCBool) ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = ("cBools3_x",HsTypRef "CBOOL") ::: ("cBools3_y",HsTypRef "CBOOL") ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = ("cBools3_x",HsTypRef "CBOOL") ::: ("cBools3_y",HsTypRef "CBOOL") ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = ("cBools3_x",HsTypRef "CBOOL") ::: ("cBools3_y",HsTypRef "CBOOL") ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = ("cBools3_x",HsTypRef "CBOOL") ::: ("cBools3_y",HsTypRef "CBOOL") ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclNewtype (Newtype {newtypeName = "CBOOL", newtypeConstr = "MkCBOOL", newtypeField = Field {fieldName = "unCBOOL", fieldType = HsPrimType HsPrimCBool}})
DeclData (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimCBool} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimCBool} ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimCBool} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimCBool} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools2_y", fieldType = HsPrimType HsPrimCBool} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools2_y", fieldType = HsPrimType HsPrimCBool} ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools2_y", fieldType = HsPrimType HsPrimCBool} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsPrimType HsPrimCBool} ::: Field {fieldName = "cBools2_y", fieldType = HsPrimType HsPrimCBool} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = Field {fieldName = "cBools3_x", fieldType = HsTypRef "CBOOL"} ::: Field {fieldName = "cBools3_y", fieldType = HsTypRef "CBOOL"} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = Field {fieldName = "cBools3_x", fieldType = HsTypRef "CBOOL"} ::: Field {fieldName = "cBools3_y", fieldType = HsTypRef "CBOOL"} ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = Field {fieldName = "cBools3_x", fieldType = HsTypRef "CBOOL"} ::: Field {fieldName = "cBools3_y", fieldType = HsTypRef "CBOOL"} ::: VNil})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools3", structConstr = "MkCBools3", structFields = Field {fieldName = "cBools3_x", fieldType = HsTypRef "CBOOL"} ::: Field {fieldName = "cBools3_y", fieldType = HsTypRef "CBOOL"} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
Loading

0 comments on commit ef97506

Please sign in to comment.