Skip to content

Commit b320f3b

Browse files
committed
Review comments
1 parent ca65500 commit b320f3b

File tree

6 files changed

+171
-137
lines changed

6 files changed

+171
-137
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module HsBindgen.Frontend
44
, FrontendMsg(..)
55
) where
66

7+
import Optics.Core (_2, view)
8+
79
import Clang.Enum.Bitfield
810
import Clang.LowLevel.Core
911
import Clang.Paths
@@ -143,21 +145,27 @@ frontend tracer FrontendConfig{..} BootArtefact{..} = do
143145
afterNameAnon <- nameAnonPass
144146
extlSpec <- bootExternalBindingSpec
145147
presSpec <- bootPrescriptiveBindingSpec
146-
let (afterResolveBindingSpecs, omitTypes, msgsResolveBindingSpecs) =
148+
let ( afterResolveBindingSpecs
149+
, omitTypes
150+
, declsWithExternalBindingSpecs
151+
, msgsResolveBindingSpecs
152+
) =
147153
resolveBindingSpecs
148154
extlSpec
149155
presSpec
150156
afterNameAnon
151157
forM_ msgsResolveBindingSpecs $ traceWith tracer . FrontendResolveBindingSpecs
152-
pure (afterResolveBindingSpecs, omitTypes)
158+
pure (afterResolveBindingSpecs, omitTypes, declsWithExternalBindingSpecs)
153159

154160
selectPass <- cache "select" $ do
155161
(_, _, isMainHeader, isInMainHeaderDir, _) <- parsePass
156-
(afterResolveBindingSpecs, _) <- resolveBindingSpecsPass
162+
(afterResolveBindingSpecs, _, declsWithExternalBindingSpecs) <-
163+
resolveBindingSpecsPass
157164
let (afterSelect, msgsSelect) =
158165
selectDecls
159166
isMainHeader
160167
isInMainHeaderDir
168+
declsWithExternalBindingSpecs
161169
selectConfig
162170
afterResolveBindingSpecs
163171
forM_ msgsSelect $ traceWith tracer . FrontendSelect
@@ -204,7 +212,7 @@ frontend tracer FrontendConfig{..} BootArtefact{..} = do
204212

205213
-- Omitted types
206214
frontendOmitTypes <- cache "frontendOmitTypes" $
207-
snd <$> resolveBindingSpecsPass
215+
view _2 <$> resolveBindingSpecsPass
208216

209217
-- Declarations.
210218
frontendCDecls <- cache "frontendDecls" $

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

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@
88
-- > import HsBindgen.Frontend.Analysis.DeclIndex qualified as DeclIndex
99
module 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

2424
import Prelude hiding (lookup)
2525

2626
import Control.Monad.State
2727
import Data.Function
28+
import Data.List.NonEmpty ((<|))
29+
import Data.List.NonEmpty qualified as NonEmpty
2830
import Data.Map.Strict qualified as Map
29-
import Optics.Core (_1, over, set, view, (%))
31+
import Optics.Core (over, set, (%))
3032
import Text.SimplePrettyPrint (hcat, showToCtxDoc)
3133

3234
import Clang.HighLevel.Types
@@ -44,27 +46,21 @@ import HsBindgen.Util.Tracer
4446

4547
-- | Index of all declarations
4648
data 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

5955
emptyIndex :: DeclIndex
6056
emptyIndex = 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

226216
lookup :: C.QualPrelimDeclId -> DeclIndex -> Maybe (C.Decl Parse)
227217
lookup 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

236225
lookupDelayedParseMsgs :: C.QualPrelimDeclId -> DeclIndex -> [DelayedParseMsg]
237226
lookupDelayedParseMsgs 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

244239
getDecls :: DeclIndex -> [C.Decl Parse]
245-
getDecls = map fst . Map.elems . succeeded
240+
getDecls = map psDecl . Map.elems . succeeded

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ macroDefinition info = \curr -> do
201201
, declAnn = NoAnn
202202
}
203203
decl <- mkDecl <$> getUnparsedMacro unit curr
204-
foldContinueWith [ParseSucceeded decl []]
204+
foldContinueWith [ParseSucceeded $ ParseSuccess decl []]
205205

206206
structDecl :: C.DeclInfo Parse -> Parser
207207
structDecl info = \curr -> do
@@ -260,7 +260,7 @@ structDecl info = \curr -> do
260260
, declKind = C.DeclOpaque (C.NameKindTagged C.TagKindStruct)
261261
, declAnn = NoAnn
262262
}
263-
in foldContinueWith [ParseSucceeded decl []]
263+
in foldContinueWith [ParseSucceeded $ ParseSuccess decl []]
264264
DefinitionElsewhere _ ->
265265
foldContinue
266266

@@ -507,7 +507,7 @@ functionDecl info = \curr -> do
507507
]
508508
funDecl = mkDecl purity
509509
in map parseSucceed otherDecls ++
510-
[ ParseSucceeded funDecl $
510+
[ ParseSucceeded $ ParseSuccess funDecl $
511511
nonPublicVisibility ++ potentialDuplicate
512512
]
513513
where
@@ -631,11 +631,11 @@ varDecl info = \curr -> do
631631

632632
in case cls of
633633
VarGlobal ->
634-
singleton $
635-
ParseSucceeded (mkDecl $ C.DeclGlobal typ) msgs
634+
singleton $ ParseSucceeded $
635+
ParseSuccess (mkDecl $ C.DeclGlobal typ) msgs
636636
VarConst ->
637-
singleton $
638-
ParseSucceeded (mkDecl $ C.DeclGlobal typ) msgs
637+
singleton $ ParseSucceeded $
638+
ParseSuccess (mkDecl $ C.DeclGlobal typ) msgs
639639
VarThreadLocal ->
640640
singleton $
641641
parseFailWith' $ ParseUnsupportedTLS :| msgs

0 commit comments

Comments
 (0)