-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AST annotations #276
base: main
Are you sure you want to change the base?
Add AST annotations #276
Conversation
d759246
to
9d52a1f
Compare
After discussing this with @TravisCardwell , we decided to go a slightly different route, which would make the "infrastructure" (such as it is) more generally applicable to all phases in -- | All passes in hs-bindgen (from parsing C to final generation)
data Pass = ...
-- closed!
type family Ann (p :: Pass) (a :: Star) where
Ann Parsed a = AnnParsed a
Ann p a = () -- All annotations of all types default to ()
type family AnnParsed a -- could be open or closed, doesn't matter
deriving stock instance (forall p. Show (Ann "Foo" p)) => Show (Foo p) |
2145d6a
to
7fde632
Compare
I am experimenting with the above new design. I am using the following terminology. (Let me know if different terminology would be better.)
Details-- | Annotation passes
--
-- @hs-bindgen@ generates Haskell bindings from C headers via a number of
-- abstract syntax tree (AST) data types. Each AST represents a different
-- /phase/, and each phase may be implemented using multiple /passes/. This sum
-- type defines the passes used across all phases. Each pass should only be
-- used within a single phase.
--
-- Phases:
--
-- 1. __C AST__ ("HsBindgen.C.AST"), created from the low-level @libclang@ types
-- that are parsed from the C source
-- * Prefix: @C@
-- 2. __C IR__ ("HsBindgen.IR.AST")
-- * Prefix: @Ir@
-- 3. __Haskell AST__ ("HsBindgen.Hs.AST")
-- * Prefix: @Hs@
-- 4. __Backend Common__ ("HsBindgen.Backend.Common")
-- * Prefix: @Be@
-- 5. __Template Haskell Backend__ ("HsBindgen.Backend.TH")
-- * Prefix: @Th@
-- 6. __Preprocessor Backend__ ("HsBindgen.Backend.PP")
-- * Prefix: @Pp@
data Pass =
-- | Haskell AST placeholder pass, to be removed when real passes are
-- implemented (in any phase)
HsPlaceholder I think that pass annotation type families (like The
I have to define the following instances for it to work, and that default definition can be removed. type instance AnnHsPlaceholder "Newtype" = ()
type instance AnnHsPlaceholder "NewtypeField" = () Without the default definition, there is no need for the I am force-pushing the
I am also pushing an I do not mean to be contrarian, but I think that defining annotations for each phase (when needed, separately) is a better design.
In this case, neither of these branches should be merged, but the |
7fde632
to
3e85990
Compare
I rebased the branches to work with the de Bruijn merge. Note that I have not yet updated the documentation for |
After discussing this once more, we realized that the one module HaskellAST where
-- | Passes specific to the "Haskell AST" phase
data Pass = P1 | ..
type family Ann (p :: Pass) (s :: Symbol) where
Ann P1 s = AnnP1 s
Ann P2 s = AnnP2 s
type family AnnP1 (s :: Symbol) where
AnnP1 s = ()
data T1 (p :: Pass) = MkT1 (Ann p "MkT1") ..
deriving stock instance ValidPass p => Show (T1 p)
class (forall s. Show (Ann p s)) => ValidPass p
instance (forall s. Show (Ann p s)) => ValidPass p
-- could consider (not sure if necessary)
data SPass :: Pass -> STar where
SP1 :: SPass P1
..
class KnownPass (p :: Pass) where
knownPass :: Proxy p -> SPass p
-- and then add KnownPass as a superclass constraint on ValidPass |
3e85990
to
85c8163
Compare
I get errors like the following:
I worked on it for a while, but I have not been able to find a way to get around this. I pushed the building code using |
85c8163
to
a260ffd
Compare
Haskell Unfolder Episode 2: quantified constraints teaches the workaround to the issue that had me stumped. Thank you! Here is how I documented it in the commit: -- Class alias to work around GHC limitation that type family synonym
-- applications cannot be used in quantified constraints
class Show (Ann pass s) => ShowAnn pass s
instance Show (Ann pass s) => ShowAnn pass s
-- All annotations must have a 'Show' instance (quantified constraint)
class (forall s. ShowAnn pass s) => AllAnnShow pass
instance (forall s. ShowAnn pass s) => AllAnnShow pass |
a260ffd
to
7b7ebfd
Compare
Issue: #256
This WIP implementation has one pass named
Parsed
and unit annotations. We probably do not want to merge without implementing realistic passes/annotations, so this PR is created as a draft.Note that many tests currently fail because the fixtures have not been updated to include the annotations.