Skip to content

Commit 3db7572

Browse files
committed
Add support for all top-level declarations to GHCi
This is work mostly done by Daniel Winograd-Cort during his internship at MSR Cambridge, with some further refactoring by me. This commit adds support to GHCi for most top-level declarations that can be used in Haskell source files. Class, data, newtype, type, instance are all supported, as are Type Family-related declarations. The current set of declarations are shown by :show bindings. As with variable bindings, entities bound by newer declarations shadow earlier ones. Tests are in testsuite/tests/ghci/scripts/ghci039--ghci054. Documentation to follow.
1 parent 9de6f19 commit 3db7572

35 files changed

Lines changed: 1255 additions & 642 deletions

‎compiler/basicTypes/DataCon.lhs-boot‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ import Name( Name )
55
data DataCon
66
dataConName :: DataCon -> Name
77
isVanillaDataCon :: DataCon -> Bool
8+
instance Eq DataCon
9+
instance Ord DataCon
810
\end{code}

‎compiler/basicTypes/Name.lhs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,17 @@ instance OutputableBndr Name where
435435
pprBndr _ name = pprName name
436436
437437
pprName :: Name -> SDoc
438-
pprName (Name {n_sort = sort, n_uniq = u, n_occ = occ})
438+
pprName n@(Name {n_sort = sort, n_uniq = u, n_occ = occ})
439439
= getPprStyle $ \ sty ->
440440
case sort of
441-
WiredIn mod _ builtin -> pprExternal sty uniq mod occ True builtin
442-
External mod -> pprExternal sty uniq mod occ False UserSyntax
441+
WiredIn mod _ builtin -> pprExternal sty uniq mod occ n True builtin
442+
External mod -> pprExternal sty uniq mod occ n False UserSyntax
443443
System -> pprSystem sty uniq occ
444444
Internal -> pprInternal sty uniq occ
445445
where uniq = mkUniqueGrimily (iBox u)
446446
447-
pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
448-
pprExternal sty uniq mod occ is_wired is_builtin
447+
pprExternal :: PprStyle -> Unique -> Module -> OccName -> Name -> Bool -> BuiltInSyntax -> SDoc
448+
pprExternal sty uniq mod occ name is_wired is_builtin
449449
| codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
450450
-- In code style, always qualify
451451
-- ToDo: maybe we could print all wired-in things unqualified
@@ -455,7 +455,7 @@ pprExternal sty uniq mod occ is_wired is_builtin
455455
pprNameSpaceBrief (occNameSpace occ),
456456
pprUnique uniq])
457457
| BuiltInSyntax <- is_builtin = ppr_occ_name occ -- Never qualify builtin syntax
458-
| otherwise = pprModulePrefix sty mod occ <> ppr_occ_name occ
458+
| otherwise = pprModulePrefix sty mod name <> ppr_occ_name occ
459459
where
460460
pp_mod | opt_SuppressModulePrefixes = empty
461461
| otherwise = ppr mod <> dot
@@ -482,14 +482,14 @@ pprSystem sty uniq occ
482482
-- so print the unique
483483
484484
485-
pprModulePrefix :: PprStyle -> Module -> OccName -> SDoc
485+
pprModulePrefix :: PprStyle -> Module -> Name -> SDoc
486486
-- Print the "M." part of a name, based on whether it's in scope or not
487487
-- See Note [Printing original names] in HscTypes
488-
pprModulePrefix sty mod occ
488+
pprModulePrefix sty mod name
489489
| opt_SuppressModulePrefixes = empty
490490
491491
| otherwise
492-
= case qualName sty mod occ of -- See Outputable.QualifyName:
492+
= case qualName sty name of -- See Outputable.QualifyName:
493493
NameQual modname -> ppr modname <> dot -- Name is in scope
494494
NameNotInScope1 -> ppr mod <> dot -- Not in scope
495495
NameNotInScope2 -> ppr (modulePackageId mod) <> colon -- Module not in

‎compiler/basicTypes/Name.lhs-boot‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
\begin{code}
22
module Name where
33

4+
import {-# SOURCE #-} Module
5+
46
data Name
7+
8+
nameModule :: Name -> Module
59
\end{code}

‎compiler/basicTypes/RdrName.lhs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import Maybes
6666
import SrcLoc
6767
import FastString
6868
import Outputable
69+
import Unique
6970
import Util
7071
import StaticFlags( opt_PprStyle_Debug )
7172
@@ -247,7 +248,9 @@ instance Outputable RdrName where
247248
ppr (Exact name) = ppr name
248249
ppr (Unqual occ) = ppr occ
249250
ppr (Qual mod occ) = ppr mod <> dot <> ppr occ
250-
ppr (Orig mod occ) = getPprStyle (\sty -> pprModulePrefix sty mod occ <> ppr occ)
251+
ppr (Orig mod occ) = getPprStyle (\sty -> pprModulePrefix sty mod name <> ppr occ)
252+
where name = mkExternalName (mkUniqueGrimily 0) mod occ noSrcSpan
253+
-- Note [Outputable Orig RdrName] in HscTypes
251254
252255
instance OutputableBndr RdrName where
253256
pprBndr _ n

‎compiler/deSugar/Desugar.lhs‎

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,26 @@ deSugar :: HscEnv -> ModLocation -> TcGblEnv -> IO (Messages, Maybe ModGuts)
5656
deSugar hsc_env
5757
mod_loc
5858
tcg_env@(TcGblEnv { tcg_mod = mod,
59-
tcg_src = hsc_src,
60-
tcg_type_env = type_env,
61-
tcg_imports = imports,
62-
tcg_exports = exports,
63-
tcg_keep = keep_var,
59+
tcg_src = hsc_src,
60+
tcg_type_env = type_env,
61+
tcg_imports = imports,
62+
tcg_exports = exports,
63+
tcg_keep = keep_var,
6464
tcg_th_splice_used = tc_splice_used,
6565
tcg_rdr_env = rdr_env,
66-
tcg_fix_env = fix_env,
67-
tcg_inst_env = inst_env,
68-
tcg_fam_inst_env = fam_inst_env,
69-
tcg_warns = warns,
70-
tcg_anns = anns,
71-
tcg_binds = binds,
72-
tcg_imp_specs = imp_specs,
66+
tcg_fix_env = fix_env,
67+
tcg_inst_env = inst_env,
68+
tcg_fam_inst_env = fam_inst_env,
69+
tcg_warns = warns,
70+
tcg_anns = anns,
71+
tcg_binds = binds,
72+
tcg_imp_specs = imp_specs,
7373
tcg_ev_binds = ev_binds,
7474
tcg_fords = fords,
7575
tcg_rules = rules,
7676
tcg_vects = vects,
77+
tcg_tcs = tcs,
78+
tcg_clss = clss,
7779
tcg_insts = insts,
7880
tcg_fam_insts = fam_insts,
7981
tcg_hpc = other_hpc_info })
@@ -96,8 +98,7 @@ deSugar hsc_env
9698
<- if (opt_Hpc
9799
|| target == HscInterpreted)
98100
&& (not (isHsBoot hsc_src))
99-
then addCoverageTicksToBinds dflags mod mod_loc
100-
(typeEnvTyCons type_env) binds
101+
then addCoverageTicksToBinds dflags mod mod_loc tcs binds
101102
else return (binds, hpcInfo, emptyModBreaks)
102103
initDs hsc_env mod rdr_env type_env $ do
103104
do { ds_ev_binds <- dsEvBinds ev_binds
@@ -151,26 +152,27 @@ deSugar hsc_env
151152
; used_th <- readIORef tc_splice_used
152153
153154
; let mod_guts = ModGuts {
154-
mg_module = mod,
155-
mg_boot = isHsBoot hsc_src,
156-
mg_exports = exports,
157-
mg_deps = deps,
158-
mg_used_names = used_names,
155+
mg_module = mod,
156+
mg_boot = isHsBoot hsc_src,
157+
mg_exports = exports,
158+
mg_deps = deps,
159+
mg_used_names = used_names,
159160
mg_used_th = used_th,
160161
mg_dir_imps = imp_mods imports,
161-
mg_rdr_env = rdr_env,
162-
mg_fix_env = fix_env,
163-
mg_warns = warns,
164-
mg_anns = anns,
165-
mg_types = type_env,
166-
mg_insts = insts,
167-
mg_fam_insts = fam_insts,
168-
mg_inst_env = inst_env,
169-
mg_fam_inst_env = fam_inst_env,
170-
mg_rules = ds_rules_for_imps,
171-
mg_binds = ds_binds,
172-
mg_foreign = ds_fords,
173-
mg_hpc_info = ds_hpc_info,
162+
mg_rdr_env = rdr_env,
163+
mg_fix_env = fix_env,
164+
mg_warns = warns,
165+
mg_anns = anns,
166+
mg_tcs = tcs,
167+
mg_clss = clss,
168+
mg_insts = insts,
169+
mg_fam_insts = fam_insts,
170+
mg_inst_env = inst_env,
171+
mg_fam_inst_env = fam_inst_env,
172+
mg_rules = ds_rules_for_imps,
173+
mg_binds = ds_binds,
174+
mg_foreign = ds_fords,
175+
mg_hpc_info = ds_hpc_info,
174176
mg_modBreaks = modBreaks,
175177
mg_vect_decls = ds_vects,
176178
mg_vect_info = noVectInfo,

‎compiler/ghci/ByteCodeLink.lhs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ lookupIE ie con_nm
254254
linkFail :: String -> String -> IO a
255255
linkFail who what
256256
= ghcError (ProgramError $
257-
unlines [ ""
257+
unlines [ "",who
258258
, "During interactive linking, GHCi couldn't find the following symbol:"
259259
, ' ' : ' ' : what
260260
, "This may be due to you not asking GHCi to load extra object files,"

‎compiler/ghci/Debugger.hs‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pprintClosureCommand bindThings force str = do
8787
tidyTermTyVars :: GhcMonad m => Term -> m Term
8888
tidyTermTyVars t =
8989
withSession $ \hsc_env -> do
90-
let env_tvs = tyVarsOfTypes (map idType (ic_tmp_ids (hsc_IC hsc_env)))
90+
let env_tvs = tyThingsTyVars $ ic_tythings $ hsc_IC hsc_env
9191
my_tvs = termTyVars t
9292
tvs = env_tvs `minusVarSet` my_tvs
9393
tyvarOccName = nameOccName . tyVarName
@@ -110,7 +110,7 @@ bindSuspensions t = do
110110
let (names, tys, hvals) = unzip3 stuff
111111
let ids = [ mkVanillaGlobal name ty
112112
| (name,ty) <- zip names tys]
113-
new_ic = extendInteractiveContext ictxt ids
113+
new_ic = extendInteractiveContext ictxt (map AnId ids)
114114
liftIO $ extendLinkEnv (zip names hvals)
115115
modifySession $ \_ -> hsc_env {hsc_IC = new_ic }
116116
return t'
@@ -187,10 +187,8 @@ showTerm term = do
187187

188188
bindToFreshName hsc_env ty userName = do
189189
name <- newGrimName userName
190-
let ictxt = hsc_IC hsc_env
191-
tmp_ids = ic_tmp_ids ictxt
192-
id = mkVanillaGlobal name ty
193-
new_ic = ictxt { ic_tmp_ids = id : tmp_ids }
190+
let id = AnId $ mkVanillaGlobal name ty
191+
new_ic = extendInteractiveContext (hsc_IC hsc_env) [id]
194192
return (hsc_env {hsc_IC = new_ic }, name)
195193

196194
-- Create new uniques and give them sequentially numbered names
@@ -202,20 +200,19 @@ newGrimName userName = do
202200
name = mkInternalName unique occname noSrcSpan
203201
return name
204202

205-
pprTypeAndContents :: GhcMonad m => [Id] -> m SDoc
206-
pprTypeAndContents ids = do
203+
pprTypeAndContents :: GhcMonad m => Id -> m SDoc
204+
pprTypeAndContents id = do
207205
dflags <- GHC.getSessionDynFlags
208206
let pefas = dopt Opt_PrintExplicitForalls dflags
209207
pcontents = dopt Opt_PrintBindContents dflags
208+
pprdId = (pprTyThing pefas . AnId) id
210209
if pcontents
211210
then do
212211
let depthBound = 100
213-
terms <- mapM (GHC.obtainTermFromId depthBound False) ids
214-
docs_terms <- mapM showTerm terms
215-
return $ vcat $ zipWith (\ty cts -> ty <+> equals <+> cts)
216-
(map (pprTyThing pefas . AnId) ids)
217-
docs_terms
218-
else return $ vcat $ map (pprTyThing pefas . AnId) ids
212+
term <- GHC.obtainTermFromId depthBound False id
213+
docs_term <- showTerm term
214+
return $ pprdId <+> equals <+> docs_term
215+
else return pprdId
219216

220217
--------------------------------------------------------------
221218
-- Utils

‎compiler/ghci/Linker.lhs‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
-- -fno-cse is needed for GLOBAL_VAR's to behave properly
1313
1414
module Linker ( HValue, getHValue, showLinkerState,
15-
linkExpr, unload, withExtendedLinkEnv,
15+
linkExpr, linkDecls, unload, withExtendedLinkEnv,
1616
extendLinkEnv, deleteFromLinkEnv,
1717
extendLoadedPkgs,
1818
linkPackages,initDynLinker,linkModule,
@@ -52,6 +52,7 @@ import UniqSet
5252
import FastString
5353
import Config
5454
import SysTools
55+
import PrelNames
5556
5657
-- Standard libraries
5758
import Control.Monad
@@ -427,9 +428,9 @@ linkExpr hsc_env span root_ul_bco
427428
428429
needed_mods :: [Module]
429430
needed_mods = [ nameModule n | n <- free_names,
430-
isExternalName n, -- Names from other modules
431-
not (isWiredInName n) -- Exclude wired-in names
432-
] -- (see note below)
431+
isExternalName n, -- Names from other modules
432+
not (isWiredInName n) -- Exclude wired-in names
433+
] -- (see note below)
433434
-- Exclude wired-in names because we may not have read
434435
-- their interface files, so getLinkDeps will fail
435436
-- All wired-in names are in the base package, which we link
@@ -476,7 +477,9 @@ getLinkDeps hsc_env hpt pls maybe_normal_osuf span mods
476477
-- Find all the packages and linkables that a set of modules depends on
477478
= do {
478479
-- 1. Find the dependent home-pkg-modules/packages from each iface
479-
(mods_s, pkgs_s) <- follow_deps mods emptyUniqSet emptyUniqSet;
480+
-- (omitting iINTERACTIVE, which is already linked)
481+
(mods_s, pkgs_s) <- follow_deps (filter ((/=) iNTERACTIVE) mods)
482+
emptyUniqSet emptyUniqSet;
480483
481484
let {
482485
-- 2. Exclude ones already linked
@@ -488,7 +491,6 @@ getLinkDeps hsc_env hpt pls maybe_normal_osuf span mods
488491
(objs_loaded pls ++ bcos_loaded pls)
489492
} ;
490493
491-
-- putStrLn (showSDoc (ppr mods_s)) ;
492494
-- 3. For each dependent module, find its linkable
493495
-- This will either be in the HPT or (in the case of one-shot
494496
-- compilation) we may need to use maybe_getFileLinkable
@@ -594,6 +596,53 @@ getLinkDeps hsc_env hpt pls maybe_normal_osuf span mods
594596
adjust_ul _ _ = panic "adjust_ul"
595597
\end{code}
596598

599+
600+
%************************************************************************
601+
%* *
602+
Loading a Decls statement
603+
%* *
604+
%************************************************************************
605+
\begin{code}
606+
linkDecls :: HscEnv -> SrcSpan -> CompiledByteCode -> IO () --[HValue]
607+
linkDecls hsc_env span (ByteCode unlinkedBCOs itblEnv) = do
608+
-- Initialise the linker (if it's not been done already)
609+
let dflags = hsc_dflags hsc_env
610+
initDynLinker dflags
611+
612+
-- Take lock for the actual work.
613+
modifyPLS $ \pls0 -> do
614+
615+
-- Link the packages and modules required
616+
(pls, ok) <- linkDependencies hsc_env pls0 span needed_mods
617+
if failed ok
618+
then ghcError (ProgramError "")
619+
else do
620+
621+
-- Link the expression itself
622+
let ie = plusNameEnv (itbl_env pls) itblEnv
623+
ce = closure_env pls
624+
625+
-- Link the necessary packages and linkables
626+
(final_gce, _) <- linkSomeBCOs False ie ce unlinkedBCOs
627+
let pls2 = pls { closure_env = final_gce,
628+
itbl_env = ie }
629+
return (pls2, ()) --hvals)
630+
where
631+
free_names = concatMap (nameSetToList . bcoFreeNames) unlinkedBCOs
632+
633+
needed_mods :: [Module]
634+
needed_mods = [ nameModule n | n <- free_names,
635+
isExternalName n, -- Names from other modules
636+
not (isWiredInName n) -- Exclude wired-in names
637+
] -- (see note below)
638+
-- Exclude wired-in names because we may not have read
639+
-- their interface files, so getLinkDeps will fail
640+
-- All wired-in names are in the base package, which we link
641+
-- by default, so we can safely ignore them here.
642+
\end{code}
643+
644+
645+
597646
%************************************************************************
598647
%* *
599648
Loading a single module

0 commit comments

Comments
 (0)