88-- > import HsBindgen.Frontend.Analysis.DeclIndex qualified as DeclIndex
99module HsBindgen.Frontend.Analysis.DeclIndex (
1010 DeclIndex -- opaque
11- , getNotAttempted
12- , getFailed
11+ , getParseOmissions
12+ , getParseFailures
1313 -- * Construction
1414 , DeclIndexError (.. )
1515 , fromParseResults
1616 -- * Query
1717 , lookup
1818 , (!)
1919 , lookupDelayedParseMsgs
20- , lookupNotAttempted
20+ , lookupMissing
2121 , getDecls
2222 ) where
2323
2424import Prelude hiding (lookup )
2525
2626import Control.Monad.State
2727import Data.Function
28+ import Data.List.NonEmpty ((<|) )
29+ import Data.List.NonEmpty qualified as NonEmpty
2830import Data.Map.Strict qualified as Map
29- import Optics.Core (_1 , over , set , view , (%) )
31+ import Optics.Core (over , set , (%) )
3032import Text.SimplePrettyPrint (hcat , showToCtxDoc )
3133
3234import Clang.HighLevel.Types
@@ -44,27 +46,21 @@ import HsBindgen.Util.Tracer
4446
4547-- | Index of all declarations
4648data DeclIndex = DeclIndex {
47- succeeded ::
48- ! (Map C. QualPrelimDeclId
49- (C. Decl Parse , [DelayedParseMsg ]))
50- , notAttempted ::
51- ! (Map C. QualPrelimDeclId
52- (SingleLoc , C. Availability , ParseOmissionReason ))
53- , failed ::
54- ! (Map C. QualPrelimDeclId
55- (SingleLoc , C. Availability , NonEmpty DelayedParseMsg ))
49+ succeeded :: ! (Map C. QualPrelimDeclId ParseSuccess )
50+ , omitted :: ! (Map C. QualPrelimDeclId (NonEmpty ParseOmission ))
51+ , failed :: ! (Map C. QualPrelimDeclId (NonEmpty ParseFailure ))
5652 }
5753 deriving stock (Show , Generic )
5854
5955emptyIndex :: DeclIndex
6056emptyIndex = DeclIndex Map. empty Map. empty Map. empty
6157
62- getNotAttempted ::
63- DeclIndex -> Map C. QualPrelimDeclId (SingleLoc , C. Availability , ParseOmissionReason )
64- getNotAttempted = notAttempted
58+ getParseOmissions ::
59+ DeclIndex -> Map C. QualPrelimDeclId (NonEmpty ParseOmission )
60+ getParseOmissions = omitted
6561
66- getFailed :: DeclIndex -> Map C. QualPrelimDeclId (SingleLoc , C. Availability , NonEmpty DelayedParseMsg )
67- getFailed = failed
62+ getParseFailures :: DeclIndex -> Map C. QualPrelimDeclId (NonEmpty ParseFailure )
63+ getParseFailures = failed
6864
6965{- ------------------------------------------------------------------------------
7066 Construction
@@ -93,10 +89,10 @@ fromParseResults results =
9389 -- Ignore further definitions of the same ID after an error
9490 oldIndex
9591 else case parse of
96- ParseSucceeded { .. } ->
92+ ParseSucceeded x ->
9793 let (succeeded', mErr) = flip runState Nothing $
9894 Map. alterF
99- (insert psDecl psDelayedMsgs )
95+ (insert x )
10096 qualPrelimDeclId
10197 index. succeeded
10298 in PartialIndex {
@@ -105,48 +101,42 @@ fromParseResults results =
105101 Nothing -> errors
106102 Just err -> Map. insert qualPrelimDeclId err errors
107103 }
108- -- TODO_PR: On failures, we simply insert the value into the
109- -- respective index maps, preferring existing, previously inserted
110- -- key-value pairs.
111- ParseNotAttempted {.. } ->
112- let val = (pnaSingleLoc, pnaAvailability, pnaParseOmissionReason)
113- in over
114- ( # index % # notAttempted )
115- ( insertPreferOld qualPrelimDeclId val )
116- oldIndex
117- ParseFailed {.. } ->
118- let val = (pfSingleLoc, pfAvailability, pfDelayedParseMsgs)
119- in over
120- ( # index % # failed )
121- ( insertPreferOld qualPrelimDeclId val )
122- oldIndex
104+ ParseOmitted x ->
105+ over
106+ ( # index % # omitted )
107+ ( alter qualPrelimDeclId x )
108+ oldIndex
109+ ParseFailed x ->
110+ over
111+ ( # index % # failed )
112+ ( alter qualPrelimDeclId x )
113+ oldIndex
123114 where
124115 qualPrelimDeclId :: C. QualPrelimDeclId
125116 qualPrelimDeclId = getQualPrelimDeclId parse
126117
127- insertPreferOld :: Ord k => k -> a -> Map k a -> Map k a
128- insertPreferOld key new =
118+ alter :: Ord k => k -> a -> Map k ( NonEmpty a ) -> Map k ( NonEmpty a )
119+ alter key x =
129120 Map. alter (\ case
130- Nothing -> Just new
131- Just old -> Just old ) key
121+ Nothing -> Just $ x :| []
122+ Just xs -> Just $ x <| xs ) key
132123
133124 insert ::
134- C. Decl Parse
135- -> [DelayedParseMsg ]
136- -> Maybe (C. Decl Parse , [DelayedParseMsg ])
137- -> State (Maybe DeclIndexError ) (Maybe (C. Decl Parse , [DelayedParseMsg ]))
138- insert new newMsgs mOld = state $ \ _ ->
125+ ParseSuccess
126+ -> Maybe ParseSuccess
127+ -> State (Maybe DeclIndexError ) (Maybe ParseSuccess )
128+ insert new mOld = state $ \ _ ->
139129 case mOld of
140130 Nothing ->
141131 -- The normal case: no previous declaration exists
142- success ( new, newMsgs)
132+ success new
143133
144- Just ( old, oldMsgs)
145- | sameDefinition ( C. declKind new) ( C. declKind old) ->
134+ Just old
135+ | sameDefinition new. psDecl . declKind old. psDecl . declKind ->
146136 -- Redeclaration but with the same definition. This can happen,
147137 -- for example for opaque structs. We stick with the first but
148138 -- add the parse messages of the second.
149- success (old, oldMsgs ++ newMsgs)
139+ success $ over # psDelayedMsgs ( ++ new . psDelayedMsgs) old
150140
151141 | otherwise ->
152142 -- Redeclaration with a /different/ value. This is only legal
@@ -165,9 +155,9 @@ fromParseResults results =
165155 --
166156 -- See issue #1155.
167157 failure $ Redeclaration {
168- redeclarationId = C. declQualPrelimDeclId new
169- , redeclarationOld = C. declLoc $ C . declInfo old
170- , redeclarationNew = C. declLoc $ C . declInfo new
158+ redeclarationId = C. declQualPrelimDeclId $ new. psDecl
159+ , redeclarationOld = old . psDecl . declInfo. declLoc
160+ , redeclarationNew = new . psDecl . declInfo. declLoc
171161 }
172162 where
173163 -- No errors; set (or replace) value in the map
@@ -225,8 +215,7 @@ instance IsTrace Level DeclIndexError where
225215
226216lookup :: C. QualPrelimDeclId -> DeclIndex -> Maybe (C. Decl Parse )
227217lookup qualPrelimDeclId =
228- fmap fst . Map. lookup qualPrelimDeclId . succeeded
229-
218+ fmap psDecl . Map. lookup qualPrelimDeclId . succeeded
230219
231220(!) :: HasCallStack => DeclIndex -> C. QualPrelimDeclId -> C. Decl Parse
232221(!) declIndex qualPrelimDeclId =
@@ -235,11 +224,17 @@ lookup qualPrelimDeclId =
235224
236225lookupDelayedParseMsgs :: C. QualPrelimDeclId -> DeclIndex -> [DelayedParseMsg ]
237226lookupDelayedParseMsgs qualPrelimDeclId =
238- maybe [] snd . Map. lookup qualPrelimDeclId . succeeded
239-
240- lookupNotAttempted :: C. QualPrelimDeclId -> DeclIndex -> Maybe SingleLoc
241- lookupNotAttempted qualPrelimDeclId =
242- fmap (view _1) . Map. lookup qualPrelimDeclId . notAttempted
227+ maybe [] psDelayedMsgs . Map. lookup qualPrelimDeclId . succeeded
228+
229+ -- | For a given declaration ID, look up the source locations of omitted or
230+ -- failed parses.
231+ lookupMissing :: C. QualPrelimDeclId -> DeclIndex -> [SingleLoc ]
232+ lookupMissing qualPrelimDeclId index =
233+ (maybe [] (map poSingleLoc . NonEmpty. toList) $
234+ Map. lookup qualPrelimDeclId $ index. omitted)
235+ ++
236+ (maybe [] (map pfSingleLoc . NonEmpty. toList) $
237+ Map. lookup qualPrelimDeclId $ index. failed)
243238
244239getDecls :: DeclIndex -> [C. Decl Parse ]
245- getDecls = map fst . Map. elems . succeeded
240+ getDecls = map psDecl . Map. elems . succeeded
0 commit comments