2 % (c) The GRASP Project, Glasgow University, 1992-2002
6 TcRn, TcM, RnM, -- The monad is opaque outside this module
8 -- Standard monadic operations
9 thenM, thenM_, returnM, failM,
11 -- Non-standard operations
12 runTcRn, fixM, tryM, ioToTcRn,
13 newMutVar, readMutVar, writeMutVar,
14 getEnv, setEnv, updEnv, unsafeInterleaveM,
16 -- The environment types
17 Env(..), TopEnv(..), TcGblEnv(..),
18 TcLclEnv(..), RnLclEnv(..),
21 RnMode(..), isInterfaceMode, isCmdLineMode,
22 EntityUsage, emptyUsages, ErrCtxt,
23 ImportAvails(..), emptyImportAvails, plusImportAvails,
24 plusAvail, pruneAvails,
25 AvailEnv, emptyAvailEnv, unitAvailEnv, plusAvailEnv,
26 mkAvailEnv, lookupAvailEnv, availEnvElts, addAvail,
33 Stage(..), topStage, topSpliceStage,
34 Level, impLevel, topLevel,
37 Inst(..), InstOrigin(..), InstLoc(..), pprInstLoc, instLocSrcLoc,
38 LIE, emptyLIE, unitLIE, plusLIE, consLIE,
39 plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE,
45 #include "HsVersions.h"
47 import HsSyn ( PendingSplice, HsOverLit, MonoBinds, RuleDecl, ForeignDecl )
48 import RnHsSyn ( RenamedHsExpr, RenamedPat, RenamedArithSeqInfo )
49 import HscTypes ( GhciMode, ExternalPackageState, HomePackageTable, NameCache,
50 GlobalRdrEnv, LocalRdrEnv, FixityEnv, TypeEnv, TyThing,
51 Avails, GenAvailInfo(..), AvailInfo, availName,
52 IsBootInterface, Deprecations )
53 import Packages ( PackageName )
54 import TcType ( TcTyVarSet, TcType, TcTauType, TcThetaType, TcPredType, TcKind,
55 tcCmpPred, tcCmpType, tcCmpTypes )
56 import InstEnv ( DFunId, InstEnv )
59 import NameSet ( NameSet, emptyNameSet )
61 import Class ( Class )
62 import Var ( Id, TyVar )
63 import VarEnv ( TidyEnv )
65 import SrcLoc ( SrcLoc )
66 import VarSet ( IdSet )
67 import ErrUtils ( Messages, Message )
68 import CmdLineOpts ( DynFlags )
69 import UniqSupply ( UniqSupply )
70 import BasicTypes ( IPName )
71 import Util ( thenCmp )
74 import DATA_IOREF ( IORef, newIORef, readIORef, writeIORef )
75 import UNSAFE_IO ( unsafeInterleaveIO )
76 import FIX_IO ( fixIO )
77 import EXCEPTION ( Exception )
78 import Maybe ( mapMaybe )
79 import ListSetOps ( unionLists )
80 import Panic ( tryMost )
85 type TcRef a = IORef a
86 type TcId = Id -- Type may be a TcType
90 %************************************************************************
92 Standard monad definition for TcRn
93 All the combinators for the monad can be found in TcRnMonad
95 %************************************************************************
97 The monad itself has to be defined here,
98 because it is mentioned by ErrCtxt
101 newtype TcRn m a = TcRn (Env m -> IO a)
104 type TcM a = TcRn TcLclEnv a
105 type RnM a = TcRn RnLclEnv a
107 returnM :: a -> TcRn m a
108 returnM a = TcRn (\ env -> return a)
110 thenM :: TcRn m a -> (a -> TcRn m b) -> TcRn m b
111 thenM (TcRn m) f = TcRn (\ env -> do { r <- m env ;
114 thenM_ :: TcRn m a -> TcRn m b -> TcRn m b
115 thenM_ (TcRn m) f = TcRn (\ env -> do { m env ; unTcRn f env })
118 failM = TcRn (\ env -> ioError (userError "TcRn failure"))
120 instance Monad (TcRn m) where
124 fail s = failM -- Ignore the string
128 %************************************************************************
130 Fundmantal combinators specific to the monad
132 %************************************************************************
137 runTcRn :: Env m -> TcRn m a -> IO a
138 runTcRn env (TcRn m) = m env
141 The fixpoint combinator
144 {-# NOINLINE fixM #-}
145 -- Aargh! Not inlining fixTc alleviates a space leak problem.
146 -- Normally fixTc is used with a lazy tuple match: if the optimiser is
147 -- shown the definition of fixTc, it occasionally transforms the code
148 -- in such a way that the code generator doesn't spot the selector
151 fixM :: (a -> TcRn m a) -> TcRn m a
152 fixM f = TcRn (\ env -> fixIO (\ r -> unTcRn (f r) env))
158 tryM :: TcRn m r -> TcRn m (Either Exception r)
159 -- Reflect exception into TcRn monad
160 tryM (TcRn thing) = TcRn (\ env -> tryMost (thing env))
166 unsafeInterleaveM :: TcRn m a -> TcRn m a
167 unsafeInterleaveM (TcRn m) = TcRn (\ env -> unsafeInterleaveIO (m env))
172 Performing arbitrary I/O, plus the read/write var (for efficiency)
175 ioToTcRn :: IO a -> TcRn m a
176 ioToTcRn io = TcRn (\ env -> io)
178 newMutVar :: a -> TcRn m (TcRef a)
179 newMutVar val = TcRn (\ env -> newIORef val)
181 writeMutVar :: TcRef a -> a -> TcRn m ()
182 writeMutVar var val = TcRn (\ env -> writeIORef var val)
184 readMutVar :: TcRef a -> TcRn m a
185 readMutVar var = TcRn (\ env -> readIORef var)
188 Getting the environment
191 getEnv :: TcRn m (Env m)
192 {-# INLINE getEnv #-}
193 getEnv = TcRn (\ env -> return env)
195 setEnv :: Env n -> TcRn n a -> TcRn m a
196 {-# INLINE setEnv #-}
197 setEnv new_env (TcRn m) = TcRn (\ env -> m new_env)
199 updEnv :: (Env m -> Env n) -> TcRn n a -> TcRn m a
200 {-# INLINE updEnv #-}
201 updEnv upd (TcRn m) = TcRn (\ env -> m (upd env))
205 %************************************************************************
207 The main environment types
209 %************************************************************************
212 data Env a -- Changes as we move into an expression
214 env_top :: TopEnv, -- Top-level stuff that never changes
215 -- Mainly a bunch of updatable refs
216 -- Includes all info about imported things
217 env_gbl :: TcGblEnv, -- Info about things defined at the top leve
218 -- of the module being compiled
220 env_lcl :: a, -- Different for the type checker
223 env_loc :: SrcLoc -- Source location
226 data TopEnv -- Built once at top level then does not change
227 -- Concerns imported stuff
228 -- Exceptions: error recovery points, meta computation points
230 top_mode :: GhciMode,
231 top_dflags :: DynFlags,
233 -- Stuff about imports
234 top_eps :: TcRef ExternalPackageState,
235 -- PIT, ImportedModuleInfo
236 -- DeclsMap, IfaceRules, IfaceInsts, InstGates
237 -- TypeEnv, InstEnv, RuleBase
238 -- Mutable, because we demand-load declarations that extend the state
240 top_hpt :: HomePackageTable,
241 -- The home package table that we've accumulated while
242 -- compiling the home package,
243 -- *excluding* the module we are compiling right now.
244 -- (In one-shot mode the current module is the only
245 -- home-package module, so tc_hpt is empty. All other
246 -- modules count as "external-package" modules.)
247 -- tc_hpt is not mutable because we only demand-load
248 -- external packages; the home package is eagerly
249 -- loaded by the compilation manager.
251 -- The global name supply
252 top_nc :: TcRef NameCache, -- Maps original names to Names
253 top_us :: TcRef UniqSupply, -- Unique supply for this module
254 top_errs :: TcRef Messages
257 -- TcGblEnv describes the top-level of the module at the
258 -- point at which the typechecker is finished work.
259 -- It is this structure that is handed on to the desugarer
263 tcg_mod :: Module, -- Module being compiled
264 tcg_usages :: TcRef EntityUsage, -- What version of what entities
265 -- have been used from other home-pkg modules
266 tcg_rdr_env :: GlobalRdrEnv, -- Top level envt; used during renaming
267 tcg_fix_env :: FixityEnv, -- Ditto
268 tcg_default :: [Type], -- Types used for defaulting
270 tcg_type_env :: TypeEnv, -- Global type env for the module we are compiling now
271 -- All TyCons and Classes (for this module) end up in here right away,
272 -- along with their derived constructors, selectors.
274 -- (Ids defined in this module start in the local envt,
275 -- though they move to the global envt during zonking)
277 tcg_inst_env :: TcRef InstEnv, -- Global instance env: a combination of
278 -- tc_pcs, tc_hpt, *and* tc_insts
279 -- This field is mutable so that it can be updated inside a
280 -- Template Haskell splice, which might suck in some new
281 -- instance declarations. This is a slightly different strategy
282 -- than for the type envt, where we look up first in tcg_type_env
283 -- and then in the mutable EPS, because the InstEnv for this module
284 -- is constructed (in principle at least) only from the modules
285 -- 'below' this one, so it's this-module-specific
287 -- Now a bunch of things about this module that are simply
288 -- accumulated, but never consulted until the end.
289 -- Nevertheless, it's convenient to accumulate them along
290 -- with the rest of the info from this module.
291 tcg_exports :: Avails, -- What is exported
292 tcg_imports :: ImportAvails, -- Information about what was imported
293 -- from where, including things bound
296 -- The next fields accumulate the payload of the module
297 -- The binds, rules and foreign-decl fiels are collected
298 -- initially in un-zonked form and are finally zonked in tcRnSrcDecls
299 tcg_binds :: MonoBinds Id, -- Value bindings in this module
300 tcg_deprecs :: Deprecations, -- ...Deprecations
301 tcg_insts :: [DFunId], -- ...Instances
302 tcg_rules :: [RuleDecl Id], -- ...Rules
303 tcg_fords :: [ForeignDecl Id] -- ...Foreign import & exports
308 %************************************************************************
310 The local typechecker environment
312 %************************************************************************
314 The Global-Env/Local-Env story
315 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
316 During type checking, we keep in the tcg_type_env
317 * All types and classes
318 * All Ids derived from types and classes (constructors, selectors)
320 At the end of type checking, we zonk the local bindings,
321 and as we do so we add to the tcg_type_env
322 * Locally defined top-level Ids
324 Why? Because they are now Ids not TcIds. This final GlobalEnv is
325 a) fed back (via the knot) to typechecking the
326 unfoldings of interface signatures
327 b) used in the ModDetails of this module
332 tcl_ctxt :: ErrCtxt, -- Error context
334 tcl_level :: Stage, -- Template Haskell context
336 tcl_env :: NameEnv TcTyThing, -- The local type environment: Ids and TyVars
337 -- defined in this module
339 tcl_tyvars :: TcRef TcTyVarSet, -- The "global tyvars"
340 -- Namely, the in-scope TyVars bound in tcl_lenv,
341 -- plus the tyvars mentioned in the types of
342 -- Ids bound in tcl_lenv
343 -- Why mutable? see notes with tcGetGlobalTyVars
345 tcl_lie :: TcRef LIE -- Place to accumulate type constraints
351 = Comp -- Ordinary compiling, at level topLevel
352 | Splice Level -- Inside a splice
353 | Brack Level -- Inside brackets;
354 (TcRef [PendingSplice]) -- accumulate pending splices here
355 (TcRef LIE) -- and type constraints here
356 topStage, topSpliceStage :: Stage
358 topSpliceStage = Splice (topLevel - 1) -- Stage for the body of a top-level splice
361 impLevel, topLevel :: Level
362 topLevel = 1 -- Things defined at top level of this module
363 impLevel = 0 -- Imported things; they can be used inside a top level splice
367 -- g1 = $(map ...) is OK
368 -- g2 = $(f ...) is not OK; because we havn't compiled f yet
371 = AGlobal TyThing -- Used only in the return type of a lookup
372 | ATcId TcId Level -- Ids defined in this module; may not be fully zonked
373 | ATyVar TyVar -- Type variables
374 | AThing TcKind -- Used temporarily, during kind checking
375 -- Here's an example of how the AThing guy is used
376 -- Suppose we are checking (forall a. T a Int):
377 -- 1. We first bind (a -> AThink kv), where kv is a kind variable.
378 -- 2. Then we kind-check the (T a Int) part.
379 -- 3. Then we zonk the kind variable.
380 -- 4. Now we know the kind for 'a', and we add (a -> ATyVar a::K) to the environment
382 instance Outputable TcTyThing where -- Debugging only
383 ppr (AGlobal g) = text "AGlobal" <+> ppr g
384 ppr (ATcId g l) = text "ATcId" <+> ppr g <+> ppr l
385 ppr (ATyVar t) = text "ATyVar" <+> ppr t
386 ppr (AThing k) = text "AThing" <+> ppr k
390 type ErrCtxt = [TidyEnv -> TcM (TidyEnv, Message)]
391 -- Innermost first. Monadic so that we have a chance
392 -- to deal with bound type variables just before error
393 -- message construction
397 %************************************************************************
399 The local renamer environment
401 %************************************************************************
407 rn_lenv :: LocalRdrEnv -- Local name envt
408 -- Does *not* include global name envt; may shadow it
409 -- Includes both ordinary variables and type variables;
410 -- they are kept distinct because tyvar have a different
411 -- occurrence contructor (Name.TvOcc)
412 -- We still need the unsullied global name env so that
413 -- we can look up record field names
416 data RnMode = SourceMode -- Renaming source code
417 | InterfaceMode Module -- Renaming interface declarations from M
418 | CmdLineMode -- Renaming a command-line expression
420 isInterfaceMode (InterfaceMode _) = True
421 isInterfaceMode _ = False
423 isCmdLineMode CmdLineMode = True
424 isCmdLineMode _ = False
428 %************************************************************************
432 %************************************************************************
434 EntityUsage tells what things are actually need in order to compile this
435 module. It is used for generating the usage-version field of the ModIface.
437 Note that we do not record version info for entities from
438 other (non-home) packages. If the package changes, GHC doesn't help.
441 type EntityUsage = NameSet
442 -- The Names are all the (a) home-package
443 -- (b) "big" (i.e. no data cons, class ops)
444 -- (c) non-locally-defined
446 -- names that have been slurped in so far.
447 -- This is used to generate the "usage" information for this module.
449 emptyUsages :: EntityUsage
450 emptyUsages = emptyNameSet
454 %************************************************************************
456 Operations over ImportAvails
458 %************************************************************************
460 ImportAvails summarises what was imported from where, irrespective
461 of whether the imported htings are actually used or not
462 It is used * when porcessing the export list
463 * when constructing usage info for the inteface file
464 * to identify the list of directly imported modules
465 for initialisation purposes
471 -- All the things that are available from the import
472 -- Its domain is all the "main" things;
473 -- i.e. *excluding* class ops and constructors
474 -- (which appear inside their parent AvailTC)
476 imp_qual :: ModuleEnv AvailEnv,
477 -- Used to figure out "module M" export specifiers
478 -- (see 1.4 Report Section 5.1.1). Ultimately, we want to find
479 -- everything that is unambiguously in scope as 'M.x'
480 -- and where plain 'x' is (perhaps ambiguously) in scope.
481 -- So the starting point is all things that are in scope as 'M.x',
482 -- which is what this field tells us.
484 -- Domain is the *module qualifier* for imports.
485 -- e.g. import List as Foo
486 -- would add a binding Foo |-> ...stuff from List...
488 -- We keep the stuff as an AvailEnv so that it's easy to
489 -- combine stuff coming from different (unqualified)
490 -- imports of the same module
492 imp_mods :: ModuleEnv (Module, Bool),
493 -- Domain is all directly-imported modules
494 -- Bool is True if there was an unrestricted import
495 -- (i.e. not a selective list)
496 -- We need the Module in the range because we can't get
497 -- the keys of a ModuleEnv
499 -- (a) to help construct the usage information in
500 -- the interface file; if we import everything we
501 -- need to recompile if the module version changes
502 -- (b) to specify what child modules to initialise
504 imp_dep_mods :: ModuleEnv (ModuleName, IsBootInterface),
505 -- Home-package modules needed by the module being compiled
507 -- It doesn't matter whether any of these dependencies are actually
508 -- *used* when compiling the module; they are listed if they are below
509 -- it at all. For example, suppose M imports A which imports X. Then
510 -- compiling M might not need to consult X.hi, but X is still listed
511 -- in M's dependencies.
513 imp_dep_pkgs :: [PackageName],
514 -- Packages needed by the module being compiled, whether
515 -- directly, or via other modules in this package, or via
516 -- modules imported from other packages.
518 imp_orphs :: [ModuleName]
519 -- Orphan modules below us in the import tree
522 emptyImportAvails :: ImportAvails
523 emptyImportAvails = ImportAvails { imp_env = emptyAvailEnv,
524 imp_qual = emptyModuleEnv,
525 imp_mods = emptyModuleEnv,
526 imp_dep_mods = emptyModuleEnv,
530 plusImportAvails :: ImportAvails -> ImportAvails -> ImportAvails
532 (ImportAvails { imp_env = env1, imp_qual = unqual1, imp_mods = mods1,
533 imp_dep_mods = dmods1, imp_dep_pkgs = dpkgs1, imp_orphs = orphs1 })
534 (ImportAvails { imp_env = env2, imp_qual = unqual2, imp_mods = mods2,
535 imp_dep_mods = dmods2, imp_dep_pkgs = dpkgs2, imp_orphs = orphs2 })
536 = ImportAvails { imp_env = env1 `plusAvailEnv` env2,
537 imp_qual = plusModuleEnv_C plusAvailEnv unqual1 unqual2,
538 imp_mods = mods1 `plusModuleEnv` mods2,
539 imp_dep_mods = plusModuleEnv_C plus_mod_dep dmods1 dmods2,
540 imp_dep_pkgs = dpkgs1 `unionLists` dpkgs2,
541 imp_orphs = orphs1 `unionLists` orphs2 }
543 plus_mod_dep (m1, boot1) (m2, boot2)
544 = WARN( not (m1 == m2), (ppr m1 <+> ppr m2) $$ (ppr boot1 <+> ppr boot2) )
545 -- Check mod-names match
546 (m1, boot1 && boot2) -- If either side can "see" a non-hi-boot interface, use that
549 %************************************************************************
551 Avails, AvailEnv, etc
553 v%************************************************************************
556 plusAvail (Avail n1) (Avail n2) = Avail n1
557 plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n2 (ns1 `unionLists` ns2)
560 plusAvail a1 a2 = pprPanic "RnEnv.plusAvail" (hsep [ppr a1,ppr a2])
563 -------------------------
564 pruneAvails :: (Name -> Bool) -- Keep if this is True
567 pruneAvails keep avails
568 = mapMaybe del avails
570 del :: AvailInfo -> Maybe AvailInfo -- Nothing => nothing left!
571 del (Avail n) | keep n = Just (Avail n)
572 | otherwise = Nothing
573 del (AvailTC n ns) | null ns' = Nothing
574 | otherwise = Just (AvailTC n ns')
579 ---------------------------------------
581 ---------------------------------------
584 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
586 emptyAvailEnv :: AvailEnv
587 emptyAvailEnv = emptyNameEnv
589 unitAvailEnv :: AvailInfo -> AvailEnv
590 unitAvailEnv a = unitNameEnv (availName a) a
592 plusAvailEnv :: AvailEnv -> AvailEnv -> AvailEnv
593 plusAvailEnv = plusNameEnv_C plusAvail
595 lookupAvailEnv = lookupNameEnv
597 availEnvElts = nameEnvElts
599 addAvail :: AvailEnv -> AvailInfo -> AvailEnv
600 addAvail avails avail = extendNameEnv_C plusAvail avails (availName avail) avail
602 mkAvailEnv :: [AvailInfo] -> AvailEnv
603 -- 'avails' may have several items with the same availName
604 -- E.g import Ix( Ix(..), index )
605 -- will give Ix(Ix,index,range) and Ix(index)
606 -- We want to combine these; addAvail does that
607 mkAvailEnv avails = foldl addAvail emptyAvailEnv avails
610 %************************************************************************
612 \subsection{Where from}
614 %************************************************************************
616 The @WhereFrom@ type controls where the renamer looks for an interface file
620 = ImportByUser IsBootInterface -- Ordinary user import (perhaps {-# SOURCE #-})
622 | ImportForUsage IsBootInterface -- Import when chasing usage info from an interaface file
623 -- Failure in this case is not an error
625 | ImportBySystem -- Non user import.
627 instance Outputable WhereFrom where
628 ppr (ImportByUser is_boot) | is_boot = ptext SLIT("{- SOURCE -}")
630 ppr (ImportForUsage is_boot) | is_boot = ptext SLIT("{- USAGE SOURCE -}")
631 | otherwise = ptext SLIT("{- USAGE -}")
632 ppr ImportBySystem = ptext SLIT("{- SYSTEM -}")
636 %************************************************************************
638 \subsection[Inst-types]{@Inst@ types}
640 v%************************************************************************
642 An @Inst@ is either a dictionary, an instance of an overloaded
643 literal, or an instance of an overloaded value. We call the latter a
644 ``method'' even though it may not correspond to a class operation.
645 For example, we might have an instance of the @double@ function at
646 type Int, represented by
648 Method 34 doubleId [Int] origin
660 TcId -- The overloaded function
661 -- This function will be a global, local, or ClassOpId;
662 -- inside instance decls (only) it can also be an InstId!
663 -- The id needn't be completely polymorphic.
664 -- You'll probably find its name (for documentation purposes)
665 -- inside the InstOrigin
667 [TcType] -- The types to which its polymorphic tyvars
668 -- should be instantiated.
669 -- These types must saturate the Id's foralls.
671 TcThetaType -- The (types of the) dictionaries to which the function
672 -- must be applied to get the method
674 TcTauType -- The tau-type of the method
678 -- INVARIANT 1: in (Method u f tys theta tau loc)
679 -- type of (f tys dicts(from theta)) = tau
681 -- INVARIANT 2: tau must not be of form (Pred -> Tau)
682 -- Reason: two methods are considerd equal if the
683 -- base Id matches, and the instantiating types
684 -- match. The TcThetaType should then match too.
685 -- This only bites in the call to tcInstClassOp in TcClassDcl.mkMethodBind
689 HsOverLit -- The literal from the occurrence site
690 -- INVARIANT: never a rebindable-syntax literal
691 -- Reason: tcSyntaxName does unification, and we
692 -- don't want to deal with that during tcSimplify
693 TcType -- The type at which the literal is used
697 @Insts@ are ordered by their class/type info, rather than by their
698 unique. This allows the context-reduction mechanism to use standard finite
699 maps to do their stuff.
702 instance Ord Inst where
705 instance Eq Inst where
706 (==) i1 i2 = case i1 `cmpInst` i2 of
710 cmpInst (Dict _ pred1 _) (Dict _ pred2 _) = pred1 `tcCmpPred` pred2
711 cmpInst (Dict _ _ _) other = LT
713 cmpInst (Method _ _ _ _ _ _) (Dict _ _ _) = GT
714 cmpInst (Method _ id1 tys1 _ _ _) (Method _ id2 tys2 _ _ _) = (id1 `compare` id2) `thenCmp` (tys1 `tcCmpTypes` tys2)
715 cmpInst (Method _ _ _ _ _ _) other = LT
717 cmpInst (LitInst _ _ _ _) (Dict _ _ _) = GT
718 cmpInst (LitInst _ _ _ _) (Method _ _ _ _ _ _) = GT
719 cmpInst (LitInst _ lit1 ty1 _) (LitInst _ lit2 ty2 _) = (lit1 `compare` lit2) `thenCmp` (ty1 `tcCmpType` ty2)
723 %************************************************************************
725 \subsection[Inst-collections]{LIE: a collection of Insts}
727 %************************************************************************
732 isEmptyLIE = isEmptyBag
734 unitLIE inst = unitBag inst
735 mkLIE insts = listToBag insts
736 plusLIE lie1 lie2 = lie1 `unionBags` lie2
737 consLIE inst lie = inst `consBag` lie
738 plusLIEs lies = unionManyBags lies
739 lieToList = bagToList
740 listToLIE = listToBag
744 %************************************************************************
746 \subsection[Inst-origin]{The @InstOrigin@ type}
748 %************************************************************************
750 The @InstOrigin@ type gives information about where a dictionary came from.
751 This is important for decent error message reporting because dictionaries
752 don't appear in the original source code. Doubtless this type will evolve...
754 It appears in TcMonad because there are a couple of error-message-generation
755 functions that deal with it.
758 data InstLoc = InstLoc InstOrigin SrcLoc ErrCtxt
760 instLocSrcLoc :: InstLoc -> SrcLoc
761 instLocSrcLoc (InstLoc _ src_loc _) = src_loc
764 = OccurrenceOf Name -- Occurrence of an overloaded identifier
766 | IPOcc (IPName Name) -- Occurrence of an implicit parameter
767 | IPBind (IPName Name) -- Binding site of an implicit parameter
771 | DataDeclOrigin -- Typechecking a data declaration
773 | InstanceDeclOrigin -- Typechecking an instance decl
775 | LiteralOrigin HsOverLit -- Occurrence of a literal
777 | PatOrigin RenamedPat
779 | ArithSeqOrigin RenamedArithSeqInfo -- [x..], [x..y] etc
780 | PArrSeqOrigin RenamedArithSeqInfo -- [:x..y:] and [:x,y..z:]
782 | SignatureOrigin -- A dict created from a type signature
783 | Rank2Origin -- A dict created when typechecking the argument
784 -- of a rank-2 typed function
786 | DoOrigin -- The monad for a do expression
788 | ClassDeclOrigin -- Manufactured during a class decl
790 | InstanceSpecOrigin Class -- in a SPECIALIZE instance pragma
793 -- When specialising instances the instance info attached to
794 -- each class is not yet ready, so we record it inside the
795 -- origin information. This is a bit of a hack, but it works
796 -- fine. (Patrick is to blame [WDP].)
798 | ValSpecOrigin Name -- in a SPECIALIZE pragma for a value
800 -- Argument or result of a ccall
801 -- Dictionaries with this origin aren't actually mentioned in the
802 -- translated term, and so need not be bound. Nor should they
803 -- be abstracted over.
805 | CCallOrigin String -- CCall label
806 (Maybe RenamedHsExpr) -- Nothing if it's the result
807 -- Just arg, for an argument
809 | LitLitOrigin String -- the litlit
811 | UnknownOrigin -- Help! I give up...
815 pprInstLoc :: InstLoc -> SDoc
816 pprInstLoc (InstLoc orig locn ctxt)
817 = hsep [text "arising from", pp_orig orig, text "at", ppr locn]
819 pp_orig (OccurrenceOf name)
820 = hsep [ptext SLIT("use of"), quotes (ppr name)]
822 = hsep [ptext SLIT("use of implicit parameter"), quotes (ppr name)]
823 pp_orig (IPBind name)
824 = hsep [ptext SLIT("binding for implicit parameter"), quotes (ppr name)]
825 pp_orig RecordUpdOrigin
826 = ptext SLIT("a record update")
827 pp_orig DataDeclOrigin
828 = ptext SLIT("the data type declaration")
829 pp_orig InstanceDeclOrigin
830 = ptext SLIT("the instance declaration")
831 pp_orig (LiteralOrigin lit)
832 = hsep [ptext SLIT("the literal"), quotes (ppr lit)]
833 pp_orig (PatOrigin pat)
834 = hsep [ptext SLIT("the pattern"), quotes (ppr pat)]
835 pp_orig (ArithSeqOrigin seq)
836 = hsep [ptext SLIT("the arithmetic sequence"), quotes (ppr seq)]
837 pp_orig (PArrSeqOrigin seq)
838 = hsep [ptext SLIT("the parallel array sequence"), quotes (ppr seq)]
839 pp_orig (SignatureOrigin)
840 = ptext SLIT("a type signature")
841 pp_orig (Rank2Origin)
842 = ptext SLIT("a function with an overloaded argument type")
844 = ptext SLIT("a do statement")
845 pp_orig (ClassDeclOrigin)
846 = ptext SLIT("a class declaration")
847 pp_orig (InstanceSpecOrigin clas ty)
848 = hsep [text "a SPECIALIZE instance pragma; class",
849 quotes (ppr clas), text "type:", ppr ty]
850 pp_orig (ValSpecOrigin name)
851 = hsep [ptext SLIT("a SPECIALIZE user-pragma for"), quotes (ppr name)]
852 pp_orig (CCallOrigin clabel Nothing{-ccall result-})
853 = hsep [ptext SLIT("the result of the _ccall_ to"), quotes (text clabel)]
854 pp_orig (CCallOrigin clabel (Just arg_expr))
855 = hsep [ptext SLIT("an argument in the _ccall_ to"), quotes (text clabel) <> comma,
856 text "namely", quotes (ppr arg_expr)]
857 pp_orig (LitLitOrigin s)
858 = hsep [ptext SLIT("the ``literal-literal''"), quotes (text s)]
859 pp_orig (UnknownOrigin)
860 = ptext SLIT("...oops -- I don't know where the overloading came from!")