Skip to content

Commit 4851c7d

Browse files
authored
Merge pull request #1326 from well-typed/edsko/declid-p
Give `DeclId` a pass `p` parameter
2 parents 25d47f8 + 2ed9ce4 commit 4851c7d

File tree

17 files changed

+142
-105
lines changed

17 files changed

+142
-105
lines changed

hs-bindgen/src-internal/HsBindgen/Frontend/AST/Coerce.hs

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
module HsBindgen.Frontend.AST.Coerce (CoercePass(..)) where
1+
module HsBindgen.Frontend.AST.Coerce (
2+
CoercePass(..)
3+
, CoercePassId(..)
4+
, CoercePassTypedefRef(..)
5+
) where
26

37
import Prelude hiding (Enum)
48

5-
import Data.Bifunctor (bimap)
6-
79
import Clang.HighLevel.Documentation qualified as CDoc
810

911
import HsBindgen.Frontend.AST.Internal
12+
import HsBindgen.Frontend.Naming
1013
import HsBindgen.Frontend.Pass
14+
import HsBindgen.Imports
15+
16+
{-------------------------------------------------------------------------------
17+
Type families
18+
-------------------------------------------------------------------------------}
19+
20+
class CoercePassId (p :: Pass) (p' :: Pass) where
21+
coercePassId :: Proxy '(p, p') -> Id p -> Id p'
22+
23+
class CoercePassTypedefRef (p :: Pass) (p' :: Pass) where
24+
coercePassTypedefRef :: Proxy '(p, p') -> TypedefRef p -> TypedefRef p'
1125

1226
{-------------------------------------------------------------------------------
1327
Coercing between passes
@@ -16,6 +30,11 @@ import HsBindgen.Frontend.Pass
1630
class CoercePass a p p' where
1731
coercePass :: a p -> a p'
1832

33+
instance CoercePass DeclId p p' where
34+
coercePass = \case
35+
DeclIdNamed name origin -> DeclIdNamed name origin
36+
DeclIdBuiltin name -> DeclIdBuiltin name
37+
1938
instance (
2039
CoercePass Decl p p'
2140
, Ann "TranslationUnit" p ~ Ann "TranslationUnit" p'
@@ -27,12 +46,12 @@ instance (
2746
}
2847

2948
instance (
30-
Id p ~ Id p'
49+
CoercePassId p p'
3150
) => CoercePass CommentRef p p' where
32-
coercePass (ById t) = ById t
51+
coercePass (ById t) = ById (coercePassId (Proxy @'(p, p')) t)
3352

3453
instance (
35-
Id p ~ Id p'
54+
CoercePassId p p'
3655
) => CoercePass CDoc.Comment (CommentRef p) (CommentRef p') where
3756
coercePass comment = fmap coercePass comment
3857

@@ -54,12 +73,14 @@ instance (
5473
}
5574

5675
instance (
57-
Id p ~ Id p'
76+
CoercePassId p p'
5877
, CoercePass Comment p p'
5978
) => CoercePass DeclInfo p p' where
60-
coercePass info = DeclInfo{ declComment = fmap coercePass declComment
61-
, ..
62-
}
79+
coercePass info = DeclInfo{
80+
declId = coercePassId (Proxy @'(p, p')) declId
81+
, declComment = fmap coercePass declComment
82+
, ..
83+
}
6384
where
6485
DeclInfo{..} = info
6586

@@ -199,24 +220,30 @@ instance (
199220
}
200221

201222
instance (
202-
Id p ~ Id p'
223+
CoercePassId p p'
203224
, ArgumentName p ~ ArgumentName p'
204-
, CoercePass TypedefRefWrapper p p'
225+
, CoercePassTypedefRef p p'
205226
, ExtBinding p ~ ExtBinding p'
206227
) => CoercePass Type p p' where
207-
coercePass (TypePrim prim) = TypePrim prim
208-
coercePass (TypeStruct uid) = TypeStruct uid
209-
coercePass (TypeUnion uid) = TypeUnion uid
210-
coercePass (TypeEnum uid) = TypeEnum uid
211-
coercePass (TypeTypedef typedef) = TypeTypedef $
212-
unTypedefRefWrapper . coercePass @_ @p @p' . TypedefRefWrapper $ typedef
213-
coercePass (TypeMacroTypedef uid) = TypeMacroTypedef uid
214-
coercePass (TypePointer typ) = TypePointer (coercePass typ)
215-
coercePass (TypeFun args res) = TypeFun (map coercePass args) (coercePass res)
216-
coercePass TypeVoid = TypeVoid
217-
coercePass (TypeConstArray n typ) = TypeConstArray n (coercePass typ)
218-
coercePass (TypeIncompleteArray typ) = TypeIncompleteArray (coercePass typ)
219-
coercePass (TypeExtBinding ext) = TypeExtBinding ext
220-
coercePass (TypeBlock typ) = TypeBlock (coercePass typ)
221-
coercePass (TypeConst typ) = TypeConst (coercePass typ)
222-
coercePass (TypeComplex prim) = TypeComplex prim
228+
coercePass = \case
229+
TypePrim prim -> TypePrim prim
230+
TypeStruct uid -> TypeStruct (goId uid)
231+
TypeUnion uid -> TypeUnion (goId uid)
232+
TypeEnum uid -> TypeEnum (goId uid)
233+
TypeTypedef typedef -> TypeTypedef (goTypedefRef typedef)
234+
TypeMacroTypedef uid -> TypeMacroTypedef (goId uid)
235+
TypePointer typ -> TypePointer (coercePass typ)
236+
TypeFun args res -> TypeFun (map coercePass args) (coercePass res)
237+
TypeVoid -> TypeVoid
238+
TypeConstArray n typ -> TypeConstArray n (coercePass typ)
239+
TypeIncompleteArray typ -> TypeIncompleteArray (coercePass typ)
240+
TypeExtBinding ext -> TypeExtBinding ext
241+
TypeBlock typ -> TypeBlock (coercePass typ)
242+
TypeConst typ -> TypeConst (coercePass typ)
243+
TypeComplex prim -> TypeComplex prim
244+
where
245+
goId :: Id p -> Id p'
246+
goId = coercePassId (Proxy @'(p, p'))
247+
248+
goTypedefRef :: TypedefRef p -> TypedefRef p'
249+
goTypedefRef = coercePassTypedefRef (Proxy @'(p, p'))

hs-bindgen/src-internal/HsBindgen/Frontend/AST/Internal.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ instance PrettyForTrace (C.Located (Id p)) => PrettyForTrace (DeclInfo p) where
554554
prettyForTrace DeclInfo{declId, declLoc} =
555555
prettyForTrace $ C.Located declLoc declId
556556

557-
instance Id p ~ C.DeclId => PrettyForTrace (Decl p) where
557+
instance Id p ~ C.DeclId p => PrettyForTrace (Decl p) where
558558
prettyForTrace decl =
559559
let qualDeclId = declQualDeclId decl
560560
in prettyForTrace $ C.Located (decl.declInfo.declLoc) qualDeclId
@@ -568,16 +568,16 @@ declQualPrelimDeclId :: HasCallStack =>
568568
declQualPrelimDeclId Decl{declInfo = DeclInfo{declId}, declKind} =
569569
C.qualPrelimDeclId declId (declKindNameKind declKind)
570570

571-
declOrigQualPrelimDeclId :: Id p ~ C.DeclId => Decl p -> C.QualPrelimDeclId
571+
declOrigQualPrelimDeclId :: Id p ~ C.DeclId p => Decl p -> C.QualPrelimDeclId
572572
declOrigQualPrelimDeclId Decl{declInfo = DeclInfo{declId}, declKind} =
573573
C.qualDeclIdToQualPrelimDeclId $
574574
C.declIdToQualDeclId declId (declKindNameKind declKind)
575575

576-
declQualDeclId :: Id p ~ C.DeclId => Decl p -> C.QualDeclId
576+
declQualDeclId :: Id p ~ C.DeclId p => Decl p -> C.QualDeclId p
577577
declQualDeclId Decl{declInfo = DeclInfo{declId}, declKind} =
578578
C.declIdToQualDeclId declId (declKindNameKind declKind)
579579

580-
declQualName :: Id p ~ C.DeclId => Decl p -> C.QualName
580+
declQualName :: Id p ~ C.DeclId p => Decl p -> C.QualName
581581
declQualName Decl{declInfo = DeclInfo{declId}, declKind} =
582582
C.QualName (C.declIdName declId) (declKindNameKind declKind)
583583

hs-bindgen/src-internal/HsBindgen/Frontend/Analysis/Typedefs.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import HsBindgen.Frontend.Analysis.DeclUseGraph qualified as DeclUseGraph
1717
import HsBindgen.Frontend.Analysis.UseDeclGraph (Usage (..), ValOrRef (..))
1818
import HsBindgen.Frontend.AST.Internal qualified as C
1919
import HsBindgen.Frontend.Naming qualified as C
20-
import HsBindgen.Frontend.Pass.HandleTypedefs.IsPass (HandleTypedefs)
2120
import HsBindgen.Frontend.Pass.Select.IsPass (Select)
2221
import HsBindgen.Imports
2322

@@ -107,12 +106,12 @@ import HsBindgen.Imports
107106
-- thing also in the case of this name clash.
108107
data TypedefAnalysis = TypedefAnalysis {
109108
-- | Declarations (structs, unions, or enums) that need to be renamed
110-
rename :: Map C.Name C.DeclId
109+
rename :: Map C.Name (C.DeclId Select)
111110

112111
-- | Typedefs that need to be squashed
113112
--
114113
-- We record what use sites of the typedef should be replaced with.
115-
, squash :: Map C.Name (C.Type HandleTypedefs)
114+
, squash :: Map C.Name (C.Type Select)
116115
}
117116
deriving stock (Show, Eq)
118117

@@ -152,7 +151,7 @@ fromDecls declUseGraph = mconcat . map aux
152151

153152
analyseTypedef ::
154153
DeclUseGraph
155-
-> C.DeclId
154+
-> C.DeclId Select
156155
-> C.Typedef Select
157156
-> TypedefAnalysis
158157
analyseTypedef declUseGraph uid typedef =
@@ -222,7 +221,7 @@ typedefOfTagged typedefName valOrRef taggedType@TaggedTypeId{..} useSites
222221
--
223222
-- If we rename a datatype with a name which was /already/ not original, we
224223
-- leave the origin information unchanged.
225-
updateOrigin :: C.DeclId -> C.NameOrigin
224+
updateOrigin :: C.DeclId Select -> C.NameOrigin
226225
updateOrigin (C.DeclIdBuiltin _name) =
227226
-- TODO: Ideally we'd have a separate pass to deal with builtin types,
228227
-- and strengthen the types in such as a way that we don't have to deal with
@@ -246,7 +245,7 @@ updateOrigin (C.DeclIdNamed oldName origin) =
246245
-- This is nearly identical to 'C.QualDeclId', except that we use 'C.TagKind'
247246
-- rather than 'C.NameKind' (in other words, we rule out 'C.NameKindOrdinary').
248247
data TaggedTypeId = TaggedTypeId {
249-
taggedTypeDeclId :: C.DeclId
248+
taggedTypeDeclId :: C.DeclId Select
250249
, taggedTypeIdKind :: C.TagKind
251250
}
252251
deriving stock (Eq, Generic, Ord, Show)
@@ -268,7 +267,7 @@ toTaggedTypeId = \case
268267
C.TypeEnum declId -> Just $ TaggedTypeId declId C.TagKindEnum
269268
_otherwise -> Nothing
270269

271-
fromTaggedTypeId :: TaggedTypeId -> C.Type HandleTypedefs
270+
fromTaggedTypeId :: TaggedTypeId -> C.Type Select
272271
fromTaggedTypeId (TaggedTypeId declId kind) =
273272
case kind of
274273
C.TagKindStruct -> C.TypeStruct declId

hs-bindgen/src-internal/HsBindgen/Frontend/Naming.hs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import Clang.HighLevel.Types
8585
import Clang.LowLevel.Core
8686

8787
import HsBindgen.Errors
88+
import HsBindgen.Frontend.Pass
8889
import HsBindgen.Imports
8990
import HsBindgen.Util.Tracer (PrettyForTrace (prettyForTrace))
9091

@@ -386,23 +387,23 @@ instance PrettyForTrace NameOrigin where
386387
--
387388
-- All declarations have names after renaming in the @NameAnon@ pass. This type
388389
-- is used until the @MangleNames@ pass.
389-
data DeclId =
390+
data DeclId (p :: Pass) =
390391
DeclIdNamed Name NameOrigin
391392
| DeclIdBuiltin Name
392393
deriving stock (Show, Eq, Ord, Generic)
393394

394-
declIdName :: DeclId -> Name
395+
declIdName :: DeclId p -> Name
395396
declIdName (DeclIdNamed name _origin) = name
396397
declIdName (DeclIdBuiltin name) = name
397398

398-
instance PrettyForTrace DeclId where
399+
instance PrettyForTrace (DeclId p) where
399400
prettyForTrace = \case
400401
DeclIdNamed name origin ->
401402
prettyForTrace name <+> PP.parens (prettyForTrace origin)
402403
DeclIdBuiltin name ->
403404
prettyForTrace name
404405

405-
instance PrettyForTrace (Located DeclId) where
406+
instance PrettyForTrace (Located (DeclId p)) where
406407
prettyForTrace (Located loc declId) =
407408
case declId of
408409
DeclIdNamed name origin -> PP.hsep [
@@ -417,13 +418,13 @@ instance PrettyForTrace (Located DeclId) where
417418
-------------------------------------------------------------------------------}
418419

419420
-- | Declaration identifier, qualified by 'NameKind'
420-
data QualDeclId = QualDeclId {
421-
qualDeclId :: DeclId
421+
data QualDeclId p = QualDeclId {
422+
qualDeclId :: DeclId p
422423
, qualDeclIdKind :: NameKind
423424
}
424425
deriving stock (Eq, Generic, Ord, Show)
425426

426-
instance PrettyForTrace (Located QualDeclId) where
427+
instance PrettyForTrace (Located (QualDeclId p)) where
427428
prettyForTrace (Located loc QualDeclId{..}) =
428429
case qualDeclId of
429430
DeclIdNamed name origin -> PP.hsep [
@@ -433,13 +434,13 @@ instance PrettyForTrace (Located QualDeclId) where
433434
DeclIdBuiltin name ->
434435
prettyForTrace (QualName name qualDeclIdKind)
435436

436-
qualDeclIdName :: QualDeclId -> Name
437+
qualDeclIdName :: QualDeclId p -> Name
437438
qualDeclIdName = declIdName . qualDeclId
438439

439-
declIdToQualDeclId :: DeclId -> NameKind -> QualDeclId
440+
declIdToQualDeclId :: DeclId p -> NameKind -> QualDeclId p
440441
declIdToQualDeclId = QualDeclId
441442

442-
qualDeclIdToQualPrelimDeclId :: HasCallStack => QualDeclId -> QualPrelimDeclId
443+
qualDeclIdToQualPrelimDeclId :: HasCallStack => QualDeclId p -> QualPrelimDeclId
443444
qualDeclIdToQualPrelimDeclId (QualDeclId declId kind) =
444445
case declId of
445446
DeclIdNamed name origin ->

hs-bindgen/src-internal/HsBindgen/Frontend/Pass.hs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module HsBindgen.Frontend.Pass (
22
Pass
33
, IsPass(..)
4-
, TypedefRefWrapper(..)
54
, NoAnn(..)
65
, NoConfig(..)
76
) where
@@ -85,10 +84,6 @@ class IsPass (p :: Pass) where
8584
-- | Trace messages possibly emitted by the pass
8685
type Msg p :: Star
8786

88-
-- | Newtype wrapper intended for class instances and constraints where
89-
-- partially applied type synonyms are not allowed.
90-
newtype TypedefRefWrapper p = TypedefRefWrapper { unTypedefRefWrapper :: TypedefRef p }
91-
9287
data NoAnn = NoAnn
9388
deriving stock (Show, Eq, Ord)
9489

hs-bindgen/src-internal/HsBindgen/Frontend/Pass/ConstructTranslationUnit/IsPass.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module HsBindgen.Frontend.Pass.ConstructTranslationUnit.IsPass (
77
import HsBindgen.Frontend.Analysis.DeclIndex
88
import HsBindgen.Frontend.Analysis.DeclUseGraph
99
import HsBindgen.Frontend.Analysis.UseDeclGraph
10-
import HsBindgen.Frontend.AST.Coerce (CoercePass (..))
10+
import HsBindgen.Frontend.AST.Coerce
1111
import HsBindgen.Frontend.AST.Internal (ValidPass)
1212
import HsBindgen.Frontend.Naming qualified as C
1313
import HsBindgen.Frontend.Pass
@@ -66,5 +66,8 @@ data ConstructTranslationUnitMsg =
6666
CoercePass
6767
-------------------------------------------------------------------------------}
6868

69-
instance CoercePass TypedefRefWrapper Parse ConstructTranslationUnit where
70-
coercePass (TypedefRefWrapper p) = TypedefRefWrapper (coercePass p)
69+
instance CoercePassId Parse ConstructTranslationUnit where
70+
coercePassId _ = id
71+
72+
instance CoercePassTypedefRef Parse ConstructTranslationUnit where
73+
coercePassTypedefRef _ = coercePass

hs-bindgen/src-internal/HsBindgen/Frontend/Pass/HandleMacros/IsPass.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module HsBindgen.Frontend.Pass.HandleMacros.IsPass (
33
, HandleMacrosReparseMsg(..)
44
) where
55

6-
import HsBindgen.Frontend.AST.Coerce (CoercePass (..))
6+
import HsBindgen.Frontend.AST.Coerce
77
import HsBindgen.Frontend.AST.Internal (CheckedMacro, ValidPass)
88
import HsBindgen.Frontend.Naming qualified as C
99
import HsBindgen.Frontend.Pass
@@ -38,5 +38,8 @@ instance IsPass HandleMacros where
3838
CoercePass
3939
-------------------------------------------------------------------------------}
4040

41-
instance CoercePass TypedefRefWrapper ConstructTranslationUnit HandleMacros where
42-
coercePass (TypedefRefWrapper ref) = TypedefRefWrapper (coercePass ref)
41+
instance CoercePassId ConstructTranslationUnit HandleMacros where
42+
coercePassId _ = id
43+
44+
instance CoercePassTypedefRef ConstructTranslationUnit HandleMacros where
45+
coercePassTypedefRef _ = coercePass

0 commit comments

Comments
 (0)