| ... |
... |
@@ -42,6 +42,7 @@ module GHC.Unit.Home.PackageTable |
|
42
|
42
|
, addHomeModInfoToHpt
|
|
43
|
43
|
, addHomeModInfosToHpt
|
|
44
|
44
|
, addOnDemandHomeModule
|
|
|
45
|
+ , hptAddOwnBootTypes
|
|
45
|
46
|
|
|
46
|
47
|
-- * Restrict the HPT
|
|
47
|
48
|
, restrictHpt
|
| ... |
... |
@@ -54,14 +55,13 @@ module GHC.Unit.Home.PackageTable |
|
54
|
55
|
, hptAllAnnotations
|
|
55
|
56
|
, hptSetBootInfo
|
|
56
|
57
|
, hptLoadedIsBoot
|
|
57
|
|
- , hptLoadedTypeEnv
|
|
|
58
|
+ , hptLookupLoadedType
|
|
58
|
59
|
, hptLoadedInstEnv
|
|
59
|
60
|
, hptLoadedFamInstEnv
|
|
60
|
|
- , hptLoadedModFamInstEnv
|
|
|
61
|
+ , hptLoadedModFamInsts
|
|
61
|
62
|
, hptLoadedRuleBase
|
|
62
|
63
|
, hptLoadedAnnEnv
|
|
63
|
64
|
, hptLoadedCompleteMatches
|
|
64
|
|
- , hptLoadedDefaults
|
|
65
|
65
|
, hptLoadedByteCode
|
|
66
|
66
|
, HptStats(..)
|
|
67
|
67
|
, hptLoadedStats
|
| ... |
... |
@@ -121,8 +121,6 @@ import GHC.Unit.Module.ModIface |
|
121
|
121
|
import GHC.Utils.Outputable
|
|
122
|
122
|
import GHC.Types.Name
|
|
123
|
123
|
import GHC.Types.Name.Env (extendNameEnvList)
|
|
124
|
|
-import GHC.Types.DefaultEnv (DefaultEnv)
|
|
125
|
|
-import GHC.Core
|
|
126
|
124
|
|
|
127
|
125
|
-- | Helps us find information about modules in the home package
|
|
128
|
126
|
data HomePackageTable
|
| ... |
... |
@@ -157,39 +155,75 @@ emptyOnDemandHomePackageTable = do |
|
157
|
155
|
|
|
158
|
156
|
data HomeLoaderState = HomeLoaderState
|
|
159
|
157
|
{ hls_table :: DModuleNameEnv HomeModInfo
|
|
|
158
|
+ , hls_indices :: !LoaderIndices
|
|
160
|
159
|
, hls_is_boot :: InstalledModuleEnv ModuleNameWithIsBoot
|
|
161
|
|
- , hls_type_env :: !TypeEnv
|
|
|
160
|
+ , hls_boot_type_env :: !TypeEnv
|
|
162
|
161
|
, hls_iface_bytecode :: !(ModuleEnv (IO Linkable))
|
|
163
|
162
|
-- ^ Actions that compile the Core bindings stored in a loaded interface
|
|
164
|
163
|
-- to bytecode, for modules whose interface has Core bindings.
|
|
165
|
164
|
-- See Note [Interface Files with Core Definitions]
|
|
166
|
|
- , hls_inst_env :: !InstEnv
|
|
167
|
|
- , hls_fam_inst_env :: !FamInstEnv
|
|
168
|
|
- , hls_mod_fam_inst_env :: !(ModuleEnv FamInstEnv)
|
|
169
|
|
- , hls_rule_base :: !RuleBase
|
|
170
|
|
- , hls_ann_env :: !AnnEnv
|
|
171
|
|
- , hls_complete_matches :: !CompleteMatches
|
|
172
|
|
- , hls_defaults :: ModuleEnv DefaultEnv
|
|
173
|
165
|
, hls_stats :: !HptStats
|
|
174
|
166
|
}
|
|
175
|
167
|
|
|
|
168
|
+data LoaderIndices = LoaderIndices
|
|
|
169
|
+ { li_inst_env :: !InstEnv
|
|
|
170
|
+ , li_fam_inst_env :: !FamInstEnv
|
|
|
171
|
+ , li_rule_base :: !RuleBase
|
|
|
172
|
+ , li_ann_env :: !AnnEnv
|
|
|
173
|
+ , li_complete_matches :: !CompleteMatches
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+emptyLoaderIndices :: LoaderIndices
|
|
|
177
|
+emptyLoaderIndices = LoaderIndices
|
|
|
178
|
+ { li_inst_env = emptyInstEnv
|
|
|
179
|
+ , li_fam_inst_env = emptyFamInstEnv
|
|
|
180
|
+ , li_rule_base = emptyRuleBase
|
|
|
181
|
+ , li_ann_env = emptyAnnEnv
|
|
|
182
|
+ , li_complete_matches = []
|
|
|
183
|
+ }
|
|
|
184
|
+
|
|
|
185
|
+extendLoaderIndices :: LoaderIndices -> HomeModInfo -> LoaderIndices
|
|
|
186
|
+extendLoaderIndices li hmi = LoaderIndices
|
|
|
187
|
+ { li_inst_env = extendInstEnvList (li_inst_env li) (instEnvElts (md_insts details))
|
|
|
188
|
+ , li_fam_inst_env = extendFamInstEnvList (li_fam_inst_env li) (md_fam_insts details)
|
|
|
189
|
+ , li_rule_base = extendRuleBaseList (li_rule_base li) (md_rules details)
|
|
|
190
|
+ , li_ann_env = extendAnnEnvList (li_ann_env li) (md_anns details)
|
|
|
191
|
+ , li_complete_matches = li_complete_matches li ++ md_complete_matches details
|
|
|
192
|
+ }
|
|
|
193
|
+ where
|
|
|
194
|
+ details = hm_details hmi
|
|
|
195
|
+
|
|
176
|
196
|
emptyHomeLoaderState :: HomeLoaderState
|
|
177
|
197
|
emptyHomeLoaderState =
|
|
178
|
198
|
HomeLoaderState
|
|
179
|
199
|
{ hls_table = emptyUDFM
|
|
|
200
|
+ , hls_indices = emptyLoaderIndices
|
|
180
|
201
|
, hls_is_boot = emptyInstalledModuleEnv
|
|
181
|
|
- , hls_type_env = emptyTypeEnv
|
|
|
202
|
+ , hls_boot_type_env = emptyTypeEnv
|
|
182
|
203
|
, hls_iface_bytecode = emptyModuleEnv
|
|
183
|
|
- , hls_inst_env = emptyInstEnv
|
|
184
|
|
- , hls_fam_inst_env = emptyFamInstEnv
|
|
185
|
|
- , hls_mod_fam_inst_env = emptyModuleEnv
|
|
186
|
|
- , hls_rule_base = emptyRuleBase
|
|
187
|
|
- , hls_ann_env = emptyAnnEnv
|
|
188
|
|
- , hls_complete_matches = []
|
|
189
|
|
- , hls_defaults = emptyModuleEnv
|
|
190
|
204
|
, hls_stats = emptyHptStats
|
|
191
|
205
|
}
|
|
192
|
206
|
|
|
|
207
|
+insertLoadedHomeModInfo :: ModuleName -> HomeModInfo -> HomeLoaderState -> HomeLoaderState
|
|
|
208
|
+insertLoadedHomeModInfo mn hmi hls
|
|
|
209
|
+ | mn `elemUDFM` hls_table hls
|
|
|
210
|
+ = hls { hls_table = table'
|
|
|
211
|
+ , hls_indices = foldl' extendLoaderIndices emptyLoaderIndices (eltsUDFM table')
|
|
|
212
|
+ , hls_stats = stats'
|
|
|
213
|
+ }
|
|
|
214
|
+ | otherwise
|
|
|
215
|
+ = hls { hls_table = table'
|
|
|
216
|
+ , hls_indices = extendLoaderIndices (hls_indices hls) hmi
|
|
|
217
|
+ , hls_stats = stats'
|
|
|
218
|
+ }
|
|
|
219
|
+ where
|
|
|
220
|
+ table' = addToUDFM (hls_table hls) mn hmi
|
|
|
221
|
+ details = hm_details hmi
|
|
|
222
|
+ stats' = addHptInStats (hls_stats hls)
|
|
|
223
|
+ (length (typeEnvElts (md_types details)))
|
|
|
224
|
+ (length (instEnvElts (md_insts details)))
|
|
|
225
|
+ (length (md_rules details))
|
|
|
226
|
+
|
|
193
|
227
|
--------------------------------------------------------------------------------
|
|
194
|
228
|
-- * Lookups in the HPT
|
|
195
|
229
|
--------------------------------------------------------------------------------
|
| ... |
... |
@@ -219,29 +253,36 @@ queryOnDemandHpt a f hpt = |
|
219
|
253
|
hptLoadedIsBoot :: HomePackageTable -> IO (InstalledModuleEnv ModuleNameWithIsBoot)
|
|
220
|
254
|
hptLoadedIsBoot = queryOnDemandHpt emptyInstalledModuleEnv hls_is_boot
|
|
221
|
255
|
|
|
222
|
|
-hptLoadedTypeEnv :: HomePackageTable -> IO TypeEnv
|
|
223
|
|
-hptLoadedTypeEnv = queryOnDemandHpt emptyTypeEnv hls_type_env
|
|
|
256
|
+hptLookupLoadedType :: HomePackageTable -> Name -> IO (Maybe TyThing)
|
|
|
257
|
+hptLookupLoadedType HPT_Make{} _ = pure Nothing
|
|
|
258
|
+hptLookupLoadedType hpt@HPT_OneShot{loader} name = do
|
|
|
259
|
+ mb_hmi <- case nameModule_maybe name of
|
|
|
260
|
+ Just modl -> lookupHptByModule hpt modl
|
|
|
261
|
+ Nothing -> pure Nothing
|
|
|
262
|
+ case mb_hmi of
|
|
|
263
|
+ Just hmi -> pure $! lookupTypeEnv (md_types (hm_details hmi)) name
|
|
|
264
|
+ Nothing -> (`lookupTypeEnv` name) . hls_boot_type_env <$> readIORef loader
|
|
|
265
|
+
|
|
|
266
|
+hptLoadedModFamInsts :: HomePackageTable -> Module -> IO (Maybe FamInstEnv)
|
|
|
267
|
+hptLoadedModFamInsts HPT_Make{} _ = pure Nothing
|
|
|
268
|
+hptLoadedModFamInsts hpt@HPT_OneShot{} modl =
|
|
|
269
|
+ fmap (extendFamInstEnvList emptyFamInstEnv . md_fam_insts . hm_details)
|
|
|
270
|
+ <$> lookupHptByModule hpt modl
|
|
224
|
271
|
|
|
225
|
272
|
hptLoadedInstEnv :: HomePackageTable -> IO InstEnv
|
|
226
|
|
-hptLoadedInstEnv = queryOnDemandHpt emptyInstEnv hls_inst_env
|
|
|
273
|
+hptLoadedInstEnv = queryOnDemandHpt emptyInstEnv (li_inst_env . hls_indices)
|
|
227
|
274
|
|
|
228
|
275
|
hptLoadedFamInstEnv :: HomePackageTable -> IO FamInstEnv
|
|
229
|
|
-hptLoadedFamInstEnv = queryOnDemandHpt emptyFamInstEnv hls_fam_inst_env
|
|
230
|
|
-
|
|
231
|
|
-hptLoadedModFamInstEnv :: HomePackageTable -> IO (ModuleEnv FamInstEnv)
|
|
232
|
|
-hptLoadedModFamInstEnv = queryOnDemandHpt emptyModuleEnv hls_mod_fam_inst_env
|
|
|
276
|
+hptLoadedFamInstEnv = queryOnDemandHpt emptyFamInstEnv (li_fam_inst_env . hls_indices)
|
|
233
|
277
|
|
|
234
|
278
|
hptLoadedRuleBase :: HomePackageTable -> IO RuleBase
|
|
235
|
|
-hptLoadedRuleBase = queryOnDemandHpt emptyRuleBase hls_rule_base
|
|
|
279
|
+hptLoadedRuleBase = queryOnDemandHpt emptyRuleBase (li_rule_base . hls_indices)
|
|
236
|
280
|
|
|
237
|
281
|
hptLoadedAnnEnv :: HomePackageTable -> IO AnnEnv
|
|
238
|
|
-hptLoadedAnnEnv = queryOnDemandHpt emptyAnnEnv hls_ann_env
|
|
|
282
|
+hptLoadedAnnEnv = queryOnDemandHpt emptyAnnEnv (li_ann_env . hls_indices)
|
|
239
|
283
|
|
|
240
|
284
|
hptLoadedCompleteMatches :: HomePackageTable -> IO CompleteMatches
|
|
241
|
|
-hptLoadedCompleteMatches = queryOnDemandHpt [] hls_complete_matches
|
|
242
|
|
-
|
|
243
|
|
-hptLoadedDefaults :: HomePackageTable -> IO (ModuleEnv DefaultEnv)
|
|
244
|
|
-hptLoadedDefaults = queryOnDemandHpt emptyModuleEnv hls_defaults
|
|
|
285
|
+hptLoadedCompleteMatches = queryOnDemandHpt [] (li_complete_matches . hls_indices)
|
|
245
|
286
|
|
|
246
|
287
|
-- | The action that compiles the Core bindings of an on-demand loaded home
|
|
247
|
288
|
-- module to bytecode, if its interface contains Core bindings.
|
| ... |
... |
@@ -249,8 +290,8 @@ hptLoadedDefaults = queryOnDemandHpt emptyModuleEnv hls_defaults |
|
249
|
290
|
--
|
|
250
|
291
|
-- See Note [Interface Files with Core Definitions]
|
|
251
|
292
|
hptLoadedByteCode :: HomePackageTable -> Module -> IO (Maybe (IO Linkable))
|
|
252
|
|
-hptLoadedByteCode hpt mod =
|
|
253
|
|
- queryOnDemandHpt Nothing (\ hls -> lookupModuleEnv (hls_iface_bytecode hls) mod) hpt
|
|
|
293
|
+hptLoadedByteCode hpt modl =
|
|
|
294
|
+ queryOnDemandHpt Nothing (\ hls -> lookupModuleEnv (hls_iface_bytecode hls) modl) hpt
|
|
254
|
295
|
|
|
255
|
296
|
hptLoadedStats :: HomePackageTable -> IO (Maybe HptStats)
|
|
256
|
297
|
hptLoadedStats = queryOnDemandHpt Nothing (Just . hls_stats)
|
| ... |
... |
@@ -326,8 +367,8 @@ addToHpt HPT_Make{table=hptr} mn hmi = do |
|
326
|
367
|
atomicModifyIORef' hptr (\hpt -> (addToUDFM hpt mn hmi, ()))
|
|
327
|
368
|
-- If the key already existed in the map, this insertion is overwriting
|
|
328
|
369
|
-- the HMI of a previously loaded module (likely in rehydration).
|
|
329
|
|
-addToHpt HPT_OneShot{loader=hptr} mn hmi = do
|
|
330
|
|
- atomicModifyIORef' hptr (\hpt -> (hpt { hls_table = addToUDFM (hls_table hpt) mn hmi }, ()))
|
|
|
370
|
+addToHpt HPT_OneShot{loader} mn hmi =
|
|
|
371
|
+ atomicModifyIORef' loader (\hls -> (insertLoadedHomeModInfo mn hmi hls, ()))
|
|
331
|
372
|
|
|
332
|
373
|
-- | 'addHomeModInfoToHpt' for multiple module infos.
|
|
333
|
374
|
addHomeModInfosToHpt :: HomePackageTable -> [HomeModInfo] -> IO ()
|
| ... |
... |
@@ -353,53 +394,23 @@ restrictHpt HPT_OneShot{} _ = pure () |
|
353
|
394
|
addListToHpt :: HomePackageTable -> [(ModuleName, HomeModInfo)] -> IO ()
|
|
354
|
395
|
addListToHpt hpt = mapM_ (uncurry (addToHpt hpt))
|
|
355
|
396
|
|
|
356
|
|
-addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> Maybe (IO Linkable) -> IO ()
|
|
357
|
|
-addOnDemandHomeModule HPT_Make{} _ _ _ _ = pure ()
|
|
358
|
|
-addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) mb_bytecode = do
|
|
|
397
|
+addOnDemandHomeModule :: HomePackageTable -> HomeModInfo -> Maybe (IO Linkable) -> IO ()
|
|
|
398
|
+addOnDemandHomeModule hpt@HPT_Make{} hmi _ = addHomeModInfoToHpt hmi hpt
|
|
|
399
|
+addOnDemandHomeModule HPT_OneShot{loader} hmi mb_bytecode =
|
|
359
|
400
|
atomicModifyIORef' loader $ \hls ->
|
|
360
|
|
- ( if bad_boot
|
|
361
|
|
- then addHomeModTypeEnv hls
|
|
362
|
|
- else addHomeModData hls
|
|
363
|
|
- , ()
|
|
364
|
|
- )
|
|
|
401
|
+ let hls' = insertLoadedHomeModInfo (moduleName modl) hmi hls
|
|
|
402
|
+ in ( case mb_bytecode of
|
|
|
403
|
+ Nothing -> hls'
|
|
|
404
|
+ Just action -> hls' { hls_iface_bytecode = extendModuleEnv (hls_iface_bytecode hls') modl action }
|
|
|
405
|
+ , () )
|
|
365
|
406
|
where
|
|
366
|
|
- addHomeModTypeEnv hls = hls
|
|
367
|
|
- { hls_type_env = extendNameEnvList (hls_type_env hls) new_decls
|
|
368
|
|
- }
|
|
369
|
|
-
|
|
370
|
|
- addHomeModData hls = hls {
|
|
371
|
|
- hls_type_env = extendNameEnvList (hls_type_env hls) new_decls,
|
|
372
|
|
- -- Don't add an entry if the iface doesn't have Core bindings, so that
|
|
373
|
|
- -- 'get_link_deps' knows that it should load object code.
|
|
374
|
|
- hls_iface_bytecode = case mb_bytecode of
|
|
375
|
|
- Nothing -> hls_iface_bytecode hls
|
|
376
|
|
- Just action -> extendModuleEnv (hls_iface_bytecode hls) mn action,
|
|
377
|
|
- hls_rule_base = extendRuleBaseList (hls_rule_base hls)
|
|
378
|
|
- new_rules,
|
|
379
|
|
- hls_complete_matches
|
|
380
|
|
- = hls_complete_matches hls ++ new_complete_matches,
|
|
381
|
|
- hls_inst_env = extendInstEnvList (hls_inst_env hls)
|
|
382
|
|
- new_insts,
|
|
383
|
|
- hls_fam_inst_env = extendFamInstEnvList (hls_fam_inst_env hls)
|
|
384
|
|
- new_fam_insts,
|
|
385
|
|
- hls_ann_env = extendAnnEnvList (hls_ann_env hls)
|
|
386
|
|
- new_anns,
|
|
387
|
|
- hls_mod_fam_inst_env
|
|
388
|
|
- =
|
|
389
|
|
- let
|
|
390
|
|
- fam_inst_env =
|
|
391
|
|
- extendFamInstEnvList emptyFamInstEnv
|
|
392
|
|
- new_fam_insts
|
|
393
|
|
- in
|
|
394
|
|
- extendModuleEnv (hls_mod_fam_inst_env hls)
|
|
395
|
|
- mn
|
|
396
|
|
- fam_inst_env,
|
|
397
|
|
- hls_defaults = extendModuleEnv (hls_defaults hls) mn new_defaults,
|
|
398
|
|
- hls_stats = addHptInStats (hls_stats hls)
|
|
399
|
|
- (length new_decls)
|
|
400
|
|
- (length new_insts)
|
|
401
|
|
- (length new_rules)
|
|
402
|
|
- }
|
|
|
407
|
+ modl = mi_module (hm_iface hmi)
|
|
|
408
|
+
|
|
|
409
|
+hptAddOwnBootTypes :: HomePackageTable -> [(Name, TyThing)] -> IO ()
|
|
|
410
|
+hptAddOwnBootTypes HPT_Make{} _ = pure ()
|
|
|
411
|
+hptAddOwnBootTypes HPT_OneShot{loader} decls =
|
|
|
412
|
+ atomicModifyIORef' loader $ \hls ->
|
|
|
413
|
+ (hls { hls_boot_type_env = extendNameEnvList (hls_boot_type_env hls) decls }, ())
|
|
403
|
414
|
|
|
404
|
415
|
hptSetBootInfo :: HomePackageTable -> InstalledModuleEnv ModuleNameWithIsBoot -> IO ()
|
|
405
|
416
|
hptSetBootInfo HPT_Make{} _ = pure ()
|
| ... |
... |
@@ -414,11 +425,15 @@ hptSetBootInfo HPT_OneShot{loader} is_boot_env = |
|
414
|
425
|
-- | Get all 'CompleteMatches' (arising from COMPLETE pragmas) present in all
|
|
415
|
426
|
-- modules from this unit's HPT.
|
|
416
|
427
|
hptCompleteSigs :: HomePackageTable -> IO CompleteMatches
|
|
417
|
|
-hptCompleteSigs = concatHpt (md_complete_matches . hm_details)
|
|
|
428
|
+hptCompleteSigs HPT_OneShot{loader} = li_complete_matches . hls_indices <$> readIORef loader
|
|
|
429
|
+hptCompleteSigs hpt@HPT_Make{} = concatHpt (md_complete_matches . hm_details) hpt
|
|
418
|
430
|
|
|
419
|
431
|
-- | Find all the instance declarations (of classes and families) from this Home Package Table
|
|
420
|
432
|
hptAllInstances :: HomePackageTable -> IO (InstEnv, [FamInst])
|
|
421
|
|
-hptAllInstances hpt = do
|
|
|
433
|
+hptAllInstances HPT_OneShot{loader} = do
|
|
|
434
|
+ li <- hls_indices <$> readIORef loader
|
|
|
435
|
+ return (li_inst_env li, famInstEnvElts (li_fam_inst_env li))
|
|
|
436
|
+hptAllInstances hpt@HPT_Make{} = do
|
|
422
|
437
|
hits <- flip concatHpt hpt $ \mod_info -> do
|
|
423
|
438
|
let details = hm_details mod_info
|
|
424
|
439
|
return (md_insts details, md_fam_insts details)
|
| ... |
... |
@@ -435,7 +450,8 @@ hptAllFamInstances = fmap mkModuleEnv . concatHpt (\hmi -> [(hmiModule hmi, hmiF |
|
435
|
450
|
|
|
436
|
451
|
-- | All annotations from the HPT
|
|
437
|
452
|
hptAllAnnotations :: HomePackageTable -> IO AnnEnv
|
|
438
|
|
-hptAllAnnotations = fmap mkAnnEnv . concatHpt (md_anns . hm_details)
|
|
|
453
|
+hptAllAnnotations HPT_OneShot{loader} = li_ann_env . hls_indices <$> readIORef loader
|
|
|
454
|
+hptAllAnnotations hpt@HPT_Make{} = mkAnnEnv <$> concatHpt (md_anns . hm_details) hpt
|
|
439
|
455
|
|
|
440
|
456
|
--------------------------------------------------------------------------------
|
|
441
|
457
|
-- * Traversal-based queries
|