-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Implement AST annotations #256
Comments
Alternatives would be to have a different type family for each constructor, or to use an open sum type approach e.g. data ExprCon = Con1 | Con2
type Content :: Pass -> k -> Type
data family Content p con
data instance Content P Con1 = MkCon1 A B
newtype instance Content P Con2 = MkCon2 C
data WithAnnot p con = Annot { annotation :: !( Annot p con ), content :: !( Content p con ) }
type Expr p = VariantF ( WithAnnot p ) '[ Con1, Con2 ] but that seems a bit too heavy-weight. |
I am working on implementing the de# the first comment, using a symbol-indexed open type family with passes defined using a sum type. I do not yet have a clear view of what passes we might have. To start with, I just defined a single pass, tentatively called We need to decide what parts of the AST we would like to annotate. Here is what I have so far:
Should other parts of the AST be annotated? In this initial implementation, I just set all annotations to Regarding module organization, the annotation types (just type family instances since everything is currently I have not updated the tests yet. A number of them fail because the AST is now pretty-printed with annotations. I am pushing the current state to the |
Here is an overview of the data flow: graph TD
SRC@{ shape: doc, label: "C Source"}
LL("Low-level libclang types")
C("C AST types")
CIR("C IR types")
HS("Haskell AST types")
BC("Backend common types")
TH("Template Haskell types")
PP("Preprocessor types")
DST@{ shape: doc, label: "Haskell Source"}
SRC-- parsed to -->LL
LL-- translated to -->C
C-- translated to -->CIR
CIR-- translated to -->HS
HS-- translated to -->BC
BC-- translated to -->TH
BC-- translated to -->PP
PP-- rendered to -->DST
click LL "https://github.com/well-typed/hs-bindgen/blob/main/hs-bindgen-libclang/src/HsBindgen/Clang/LowLevel/Core.hs" "HsBindgen.Clang.LowLevel.Core"
click C "https://github.com/well-typed/hs-bindgen/blob/main/hs-bindgen/src/HsBindgen/C/AST.hs" "HsBindgen.C.AST"
click HS "https://github.com/well-typed/hs-bindgen/blob/main/hs-bindgen/src/HsBindgen/Hs/AST.hs" "HsBindgen.Hs.AST"
click BC "https://github.com/well-typed/hs-bindgen/blob/main/hs-bindgen/src/HsBindgen/Backend/Common.hs" "HsBindgen.Backend.Common"
click TH "https://github.com/well-typed/hs-bindgen/blob/main/hs-bindgen/src/HsBindgen/Backend/TH.hs" "HsBindgen.Backend.TH"
click PP "https://github.com/well-typed/hs-bindgen/blob/main/hs-bindgen/src/HsBindgen/Backend/PP.hs" "HsBindgen.Backend.PP"
Use of a simplified C IR is discussed in #253. From this discussion, my understanding is that the C AST will be transformed to separate C IR types. Perhaps neither C AST nor C IR types need annotations. I imagine that multi-pass processing will all occur only with the Haskell AST. I do not yet have a clear view of what passes we might have, though. The term "annotations" has a connotation of "extra" information, but I think we should be clear that it is used for including information with types that vary depending on the pass regardless of it is "extra" or not.
|
I agree with @TravisCardwell if the TL;DR is that for every need we identified so far the cleaner and simpler solution is "This [extra info] type does not vary, so this is probably best included directly in the [structure] types". And, YAGNI for any non-yet identified needs. |
I don't mind delaying until we have a concrete use case, but one such use case is the results of type inference from @sheaf 's type checker. |
We should be able handle annotations in the AST, such as recording source spans, additional information for structs (such as offsets, field widths etc provided by Clang) or type information (for type inference of macros). Perhaps the following rather simple approach would be sufficient:
The text was updated successfully, but these errors were encountered: