Skip to content

Commit

Permalink
Add type spelling to ASTs
Browse files Browse the repository at this point in the history
This commit adds a type spelling field to the following ASTs:

* `C` phase: `Struct`, `Enu`, `Typedef`
    * Type: `Text`
* `Hs` phase: `Struct`, `Newtype`
    * Type: `Maybe Text`
* `SHs` phase: `Record`, `Newtype`
    * Type: `Maybe Text`

As suggested in #316, the string is not parsed.  This commit simply uses
`Text`, but we could implement a `newtype` wrapper if desired.

The goal is to make it easier to generate tests for structures,
enumerations, and `typedef`s of structures/enumerations.  Note that this
is also required for unions, but those are not implemented yet.  This is
the minimal change required to do this; the `Maybe` is needed because it
does *not* track the type spelling for macros.

(Cherry-picked from `source-info` for experimentation)
  • Loading branch information
TravisCardwell committed Dec 2, 2024
1 parent 3e53b8e commit 3e76979
Show file tree
Hide file tree
Showing 36 changed files with 248 additions and 163 deletions.
8 changes: 4 additions & 4 deletions hs-bindgen/fixtures/bool.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DeclData (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: 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 HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil})
DeclInstance (InstanceStorable (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: 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 = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil, structTypeSpelling = Just "struct bools1"})
DeclInstance (InstanceStorable (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil, structTypeSpelling = Just "struct bools1"}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools1", structConstr = "MkCBools1", structFields = Field {fieldName = "cBools1_x", fieldType = HsPrimType HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil, structTypeSpelling = Just "struct bools1"})) [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 HsPrimVoid} ::: Field {fieldName = "cBools1_y", fieldType = HsPrimType HsPrimVoid} ::: VNil, structTypeSpelling = Just "struct bools1"}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
DeclData (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil, structTypeSpelling = Just "struct bools2"})
DeclInstance (InstanceStorable (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil, structTypeSpelling = Just "struct bools2"}) (StorableInstance {storableSizeOf = 2, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil, structTypeSpelling = Just "struct bools2"})) [PeekByteOff 0 0,PeekByteOff 0 1]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CBools2", structConstr = "MkCBools2", structFields = Field {fieldName = "cBools2_x", fieldType = HsTypRef "CBool'"} ::: Field {fieldName = "cBools2_y", fieldType = HsTypRef "CBool'"} ::: VNil, structTypeSpelling = Just "struct bools2"}) 2 (Seq [PokeByteOff 3 0 0,PokeByteOff 3 1 1])))}))
9 changes: 6 additions & 3 deletions hs-bindgen/fixtures/bool.tree-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ WrapCHeader
StructField {
fieldName = CName "y",
fieldOffset = 8,
fieldType = TypePrim
PrimVoid}]},
fieldType = TypePrim PrimVoid}],
structTypeSpelling =
"struct bools1"},
DeclStruct
Struct {
structTag = Just
Expand All @@ -33,4 +34,6 @@ WrapCHeader
fieldName = CName "y",
fieldOffset = 8,
fieldType = TypeElaborated
(CName "bool")}]}])
(CName "bool")}],
structTypeSpelling =
"struct bools2"}])
30 changes: 15 additions & 15 deletions hs-bindgen/fixtures/distilled_lib_1.hs

Large diffs are not rendered by default.

46 changes: 32 additions & 14 deletions hs-bindgen/fixtures/distilled_lib_1.tree-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,18 @@ WrapCHeader
fieldName = CName "bar",
fieldOffset = 32,
fieldType = TypePrim
(PrimChar Nothing)}]},
(PrimChar Nothing)}],
structTypeSpelling =
"another_typedef_struct_t"},
DeclTypedef
Typedef {
typedefName = CName
"another_typedef_struct_t",
typedefType = TypeElaborated
(CName
"struct another_typedef_struct_t")},
"struct another_typedef_struct_t"),
typedefTypeSpelling =
"another_typedef_struct_t"},
DeclEnum
Enu {
enumTag = Nothing,
Expand All @@ -539,26 +543,31 @@ WrapCHeader
valueValue = 0},
EnumValue {
valueName = CName "BAR",
valueValue = 1}]},
valueValue = 1}],
enumTypeSpelling =
"another_typedef_enum_e"},
DeclTypedef
Typedef {
typedefName = CName
"another_typedef_enum_e",
typedefType = TypeElaborated
(CName
"enum another_typedef_enum_e")},
"enum another_typedef_enum_e"),
typedefTypeSpelling =
"another_typedef_enum_e"},
DeclTypedef
Typedef {
typedefName = CName "a_type_t",
typedefType = TypePrim
(PrimIntegral
(PrimInt Signed))},
(PrimIntegral (PrimInt Signed)),
typedefTypeSpelling =
"a_type_t"},
DeclTypedef
Typedef {
typedefName = CName "var_t",
typedefType = TypePrim
(PrimIntegral
(PrimInt Signed))},
(PrimIntegral (PrimInt Signed)),
typedefTypeSpelling = "var_t"},
DeclStruct
Struct {
structTag = Just
Expand Down Expand Up @@ -628,14 +637,18 @@ WrapCHeader
fieldOffset = 640,
fieldType = TypeElaborated
(CName
"another_typedef_enum_e")}]},
"another_typedef_enum_e")}],
structTypeSpelling =
"struct a_typedef_struct"},
DeclTypedef
Typedef {
typedefName = CName
"a_typedef_struct_t",
typedefType = TypeElaborated
(CName
"struct a_typedef_struct")},
"struct a_typedef_struct"),
typedefTypeSpelling =
"a_typedef_struct_t"},
DeclEnum
Enu {
enumTag = Nothing,
Expand All @@ -655,17 +668,22 @@ WrapCHeader
valueValue = 2},
EnumValue {
valueName = CName "ENUM_CASE_3",
valueValue = 3}]},
valueValue = 3}],
enumTypeSpelling =
"a_typedef_enum_e"},
DeclTypedef
Typedef {
typedefName = CName
"a_typedef_enum_e",
typedefType = TypeElaborated
(CName
"enum a_typedef_enum_e")},
(CName "enum a_typedef_enum_e"),
typedefTypeSpelling =
"a_typedef_enum_e"},
DeclTypedef
Typedef {
typedefName = CName
"callback_t",
typedefType = TypePointer
(TypePrim PrimVoid)}])
(TypePrim PrimVoid),
typedefTypeSpelling =
"callback_t"}])
16 changes: 8 additions & 8 deletions hs-bindgen/fixtures/enums.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
DeclNewtype (Newtype {newtypeName = "CFirst", newtypeConstr = "MkCFirst", newtypeField = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt}})
DeclInstance (InstanceStorable (Struct {structName = "CFirst", structConstr = "MkCFirst", structFields = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt} ::: VNil}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CFirst", structConstr = "MkCFirst", structFields = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt} ::: VNil})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CFirst", structConstr = "MkCFirst", structFields = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt} ::: VNil}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CSecond", newtypeConstr = "MkCSecond", newtypeField = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt}})
DeclInstance (InstanceStorable (Struct {structName = "CSecond", structConstr = "MkCSecond", structFields = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt} ::: VNil}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CSecond", structConstr = "MkCSecond", structFields = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt} ::: VNil})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CSecond", structConstr = "MkCSecond", structFields = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt} ::: VNil}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CSame", newtypeConstr = "MkCSame", newtypeField = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt}})
DeclInstance (InstanceStorable (Struct {structName = "CSame", structConstr = "MkCSame", structFields = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt} ::: VNil}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CSame", structConstr = "MkCSame", structFields = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt} ::: VNil})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CSame", structConstr = "MkCSame", structFields = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt} ::: VNil}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CPackad", newtypeConstr = "MkCPackad", newtypeField = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar}})
DeclInstance (InstanceStorable (Struct {structName = "CPackad", structConstr = "MkCPackad", structFields = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar} ::: VNil}) (StorableInstance {storableSizeOf = 1, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CPackad", structConstr = "MkCPackad", structFields = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar} ::: VNil})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CPackad", structConstr = "MkCPackad", structFields = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar} ::: VNil}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CFirst", newtypeConstr = "MkCFirst", newtypeField = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt}, newtypeTypeSpelling = Just "enum first"})
DeclInstance (InstanceStorable (Struct {structName = "CFirst", structConstr = "MkCFirst", structFields = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt} ::: VNil, structTypeSpelling = Just "enum first"}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CFirst", structConstr = "MkCFirst", structFields = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt} ::: VNil, structTypeSpelling = Just "enum first"})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CFirst", structConstr = "MkCFirst", structFields = Field {fieldName = "unCFirst", fieldType = HsPrimType HsPrimCUInt} ::: VNil, structTypeSpelling = Just "enum first"}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CSecond", newtypeConstr = "MkCSecond", newtypeField = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt}, newtypeTypeSpelling = Just "enum second"})
DeclInstance (InstanceStorable (Struct {structName = "CSecond", structConstr = "MkCSecond", structFields = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt} ::: VNil, structTypeSpelling = Just "enum second"}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CSecond", structConstr = "MkCSecond", structFields = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt} ::: VNil, structTypeSpelling = Just "enum second"})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CSecond", structConstr = "MkCSecond", structFields = Field {fieldName = "unCSecond", fieldType = HsPrimType HsPrimCInt} ::: VNil, structTypeSpelling = Just "enum second"}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CSame", newtypeConstr = "MkCSame", newtypeField = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt}, newtypeTypeSpelling = Just "enum same"})
DeclInstance (InstanceStorable (Struct {structName = "CSame", structConstr = "MkCSame", structFields = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt} ::: VNil, structTypeSpelling = Just "enum same"}) (StorableInstance {storableSizeOf = 4, storableAlignment = 4, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CSame", structConstr = "MkCSame", structFields = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt} ::: VNil, structTypeSpelling = Just "enum same"})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CSame", structConstr = "MkCSame", structFields = Field {fieldName = "unCSame", fieldType = HsPrimType HsPrimCUInt} ::: VNil, structTypeSpelling = Just "enum same"}) 1 (Seq [PokeByteOff 2 0 0])))}))
DeclNewtype (Newtype {newtypeName = "CPackad", newtypeConstr = "MkCPackad", newtypeField = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar}, newtypeTypeSpelling = Just "enum packad"})
DeclInstance (InstanceStorable (Struct {structName = "CPackad", structConstr = "MkCPackad", structFields = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar} ::: VNil, structTypeSpelling = Just "enum packad"}) (StorableInstance {storableSizeOf = 1, storableAlignment = 1, storablePeek = Lambda "ptr" (Ap (StructCon (Struct {structName = "CPackad", structConstr = "MkCPackad", structFields = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar} ::: VNil, structTypeSpelling = Just "enum packad"})) [PeekByteOff 0 0]), storablePoke = Lambda "ptr" (Lambda "s" (ElimStruct 0 (Struct {structName = "CPackad", structConstr = "MkCPackad", structFields = Field {fieldName = "unCPackad", fieldType = HsPrimType HsPrimCSChar} ::: VNil, structTypeSpelling = Just "enum packad"}) 1 (Seq [PokeByteOff 2 0 0])))}))
15 changes: 11 additions & 4 deletions hs-bindgen/fixtures/enums.tree-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ WrapCHeader
valueValue = 0},
EnumValue {
valueName = CName "FIRST2",
valueValue = 1}]},
valueValue = 1}],
enumTypeSpelling =
"enum first"},
DeclEnum
Enu {
enumTag = Just (CName "second"),
Expand All @@ -49,7 +51,9 @@ WrapCHeader
valueValue = 0},
EnumValue {
valueName = CName "SECOND_C",
valueValue = 1}]},
valueValue = 1}],
enumTypeSpelling =
"enum second"},
DeclEnum
Enu {
enumTag = Just (CName "same"),
Expand All @@ -64,7 +68,8 @@ WrapCHeader
valueValue = 1},
EnumValue {
valueName = CName "SAME_B",
valueValue = 1}]},
valueValue = 1}],
enumTypeSpelling = "enum same"},
DeclEnum
Enu {
enumTag = Just (CName "packad"),
Expand All @@ -81,4 +86,6 @@ WrapCHeader
valueValue = 1},
EnumValue {
valueName = CName "PACKED_C",
valueValue = 2}]}])
valueValue = 2}],
enumTypeSpelling =
"enum packad"}])
2 changes: 1 addition & 1 deletion hs-bindgen/fixtures/fixedarray.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DeclNewtype (Newtype {newtypeName = "CTriple", newtypeConstr = "MkCTriple", newtypeField = Field {fieldName = "unCTriple", fieldType = HsConstArray 3 (HsPrimType HsPrimCInt)}})
DeclNewtype (Newtype {newtypeName = "CTriple", newtypeConstr = "MkCTriple", newtypeField = Field {fieldName = "unCTriple", fieldType = HsConstArray 3 (HsPrimType HsPrimCInt)}, newtypeTypeSpelling = Just "triple"})
DeclNewtypeInstance Storable "CTriple"
4 changes: 3 additions & 1 deletion hs-bindgen/fixtures/fixedarray.tree-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ WrapCHeader
3
(TypePrim
(PrimIntegral
(PrimInt Signed)))}])
(PrimInt Signed))),
typedefTypeSpelling =
"triple"}])
Loading

0 comments on commit 3e76979

Please sign in to comment.