2 % (c) The GRASP Project, Glasgow University, 1992-2002
6 TcRnIf, TcRn, TcM, RnM, IfM, IfL, IfG, -- The monad is opaque outside this module
9 -- The environment types
11 TcGblEnv(..), TcLclEnv(..),
12 IfGblEnv(..), IfLclEnv(..),
15 EntityUsage, emptyUsages, ErrCtxt,
16 ImportAvails(..), emptyImportAvails, plusImportAvails,
17 plusAvail, pruneAvails,
18 AvailEnv, emptyAvailEnv, unitAvailEnv, plusAvailEnv,
19 mkAvailEnv, lookupAvailEnv, lookupAvailEnv_maybe, availEnvElts, addAvail,
20 WhereFrom(..), mkModDeps,
26 ThStage(..), topStage, topSpliceStage,
27 ThLevel, impLevel, topLevel,
30 ArrowCtxt(..), topArrowCtxt, ProcLevel, topProcLevel,
33 Inst(..), InstOrigin(..), InstLoc(..), pprInstLoc,
34 instLocSrcLoc, instLocSrcSpan,
35 LIE, emptyLIE, unitLIE, plusLIE, consLIE,
36 plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE,
42 #include "HsVersions.h"
44 import HsSyn ( PendingSplice, HsOverLit, LHsBind, LRuleDecl, LForeignDecl,
46 import HscTypes ( FixityEnv,
47 HscEnv, TypeEnv, TyThing,
48 Avails, GenAvailInfo(..), AvailInfo,
49 availName, IsBootInterface, Deprecations )
50 import Packages ( PackageName )
51 import TcType ( TcTyVarSet, TcType, TcTauType, TcThetaType,
52 TcPredType, TcKind, tcCmpPred, tcCmpType, tcCmpTypes )
53 import InstEnv ( DFunId, InstEnv )
55 import RdrName ( GlobalRdrEnv, LocalRdrEnv )
58 import NameSet ( NameSet, emptyNameSet, DefUses )
59 import OccName ( OccEnv )
61 import Class ( Class )
62 import Var ( Id, TyVar )
63 import VarEnv ( TidyEnv )
65 import SrcLoc ( SrcSpan, SrcLoc, srcSpanStart )
66 import VarSet ( IdSet )
67 import ErrUtils ( Messages, Message )
68 import UniqSupply ( UniqSupply )
69 import BasicTypes ( IPName )
70 import Util ( thenCmp )
73 import Maybe ( mapMaybe )
74 import ListSetOps ( unionLists )
78 %************************************************************************
80 Standard monad definition for TcRn
81 All the combinators for the monad can be found in TcRnMonad
83 %************************************************************************
85 The monad itself has to be defined here, because it is mentioned by ErrCtxt
88 type TcRef a = IORef a
89 type TcId = Id -- Type may be a TcType
92 type TcRnIf a b c = IOEnv (Env a b) c
93 type IfM lcl a = TcRnIf IfGblEnv lcl a -- Iface stuff
94 type IfG a = IfM () a -- Top level
95 type IfL a = IfM IfLclEnv a -- Nested
96 type TcRn a = TcRnIf TcGblEnv TcLclEnv a
97 type RnM a = TcRn a -- Historical
98 type TcM a = TcRn a -- Historical
102 %************************************************************************
104 The main environment types
106 %************************************************************************
109 data Env gbl lcl -- Changes as we move into an expression
111 env_top :: HscEnv, -- Top-level stuff that never changes
112 -- Includes all info about imported things
114 env_us :: TcRef UniqSupply, -- Unique supply for local varibles
116 env_gbl :: gbl, -- Info about things defined at the top level
117 -- of the module being compiled
119 env_lcl :: lcl -- Nested stuff -- changes as we go into
123 -- TcGblEnv describes the top-level of the module at the
124 -- point at which the typechecker is finished work.
125 -- It is this structure that is handed on to the desugarer
129 tcg_mod :: Module, -- Module being compiled
130 tcg_rdr_env :: GlobalRdrEnv, -- Top level envt; used during renaming
131 tcg_default :: Maybe [Type], -- Types used for defaulting
132 -- Nothing => no 'default' decl
134 tcg_fix_env :: FixityEnv, -- Just for things in this module
136 tcg_type_env :: TypeEnv, -- Global type env for the module we are compiling now
137 -- All TyCons and Classes (for this module) end up in here right away,
138 -- along with their derived constructors, selectors.
140 -- (Ids defined in this module start in the local envt,
141 -- though they move to the global envt during zonking)
143 tcg_type_env_var :: TcRef TypeEnv,
144 -- Used only to initialise the interface-file
145 -- typechecker in initIfaceTcRn, so that it can see stuff
146 -- bound in this module when dealing with hi-boot recursions
147 -- Updated at intervals (e.g. after dealing with types and classes)
149 tcg_inst_env :: InstEnv, -- Instance envt for *home-package* modules
150 -- Includes the dfuns in tcg_insts
151 tcg_inst_uses :: TcRef NameSet, -- Home-package Dfuns actually used
152 -- Used to generate version dependencies
153 -- This records usages, rather like tcg_dus, but it has to
154 -- be a mutable variable so it can be augmented
155 -- when we look up an instance. These uses of dfuns are
156 -- rather like the free variables of the program, but
157 -- are implicit instead of explicit.
159 tcg_th_used :: TcRef Bool, -- True <=> Template Haskell syntax used
160 -- We need this so that we can generate a dependency on the
161 -- Template Haskell package, becuase the desugarer is going to
162 -- emit loads of references to TH symbols. It's rather like
163 -- tcg_inst_uses; the reference is implicit rather than explicit,
164 -- so we have to zap a mutable variable.
166 -- Now a bunch of things about this module that are simply
167 -- accumulated, but never consulted until the end.
168 -- Nevertheless, it's convenient to accumulate them along
169 -- with the rest of the info from this module.
170 tcg_exports :: NameSet, -- What is exported
171 tcg_imports :: ImportAvails, -- Information about what was imported
172 -- from where, including things bound
175 tcg_dus :: DefUses, -- What is defined in this module and what is used.
176 -- The latter is used to generate
177 -- (a) version tracking; no need to recompile if these
178 -- things have not changed version stamp
179 -- (b) unused-import info
181 tcg_keep :: NameSet, -- Set of names to keep alive, and to expose in the
182 -- interface file (but not to export to the user).
183 -- These are typically extra definitions generated from
184 -- data type declarations which would otherwise be
185 -- dropped as dead code.
187 -- The next fields accumulate the payload of the module
188 -- The binds, rules and foreign-decl fiels are collected
189 -- initially in un-zonked form and are finally zonked in tcRnSrcDecls
190 tcg_binds :: Bag (LHsBind Id), -- Value bindings in this module
191 tcg_deprecs :: Deprecations, -- ...Deprecations
192 tcg_insts :: [DFunId], -- ...Instances
193 tcg_rules :: [LRuleDecl Id], -- ...Rules
194 tcg_fords :: [LForeignDecl Id] -- ...Foreign import & exports
198 %************************************************************************
200 The interface environments
201 Used when dealing with IfaceDecls
203 %************************************************************************
208 -- The type environment for the module being compiled,
209 -- in case the interface refers back to it via a reference that
210 -- was originally a hi-boot file.
211 -- We need the module name so we can test when it's appropriate
212 -- to look in this env.
213 if_rec_types :: Maybe (Module, IfG TypeEnv),
214 -- Allows a read effect, so it can be in a mutable
215 -- variable; c.f. handling the external package type env
216 -- Nothing => interactive stuff, no loops possible
218 if_is_boot :: ModuleEnv (ModuleName, IsBootInterface)
219 -- Tells what we know about boot interface files
220 -- When we're importing a module we know absolutely
221 -- nothing about, so we assume it's from
222 -- another package, where we aren't doing
223 -- dependency tracking. So it won't be a hi-boot file.
228 -- The module for the current IfaceDecl
229 -- So if we see f = \x -> x
230 -- it means M.f = \x -> x, where M is the if_mod
231 if_mod :: ModuleName,
233 if_tv_env :: OccEnv TyVar, -- Nested tyvar bindings
234 if_id_env :: OccEnv Id -- Nested id binding
239 %************************************************************************
241 The local typechecker environment
243 %************************************************************************
245 The Global-Env/Local-Env story
246 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247 During type checking, we keep in the tcg_type_env
248 * All types and classes
249 * All Ids derived from types and classes (constructors, selectors)
251 At the end of type checking, we zonk the local bindings,
252 and as we do so we add to the tcg_type_env
253 * Locally defined top-level Ids
255 Why? Because they are now Ids not TcIds. This final GlobalEnv is
256 a) fed back (via the knot) to typechecking the
257 unfoldings of interface signatures
258 b) used in the ModDetails of this module
261 data TcLclEnv -- Changes as we move inside an expression
262 -- Discarded after typecheck/rename; not passed on to desugarer
264 tcl_loc :: SrcSpan, -- Source span
265 tcl_ctxt :: ErrCtxt, -- Error context
266 tcl_errs :: TcRef Messages, -- Place to accumulate errors
268 tcl_th_ctxt :: ThStage, -- Template Haskell context
269 tcl_arrow_ctxt :: ArrowCtxt, -- Arrow-notation context
271 tcl_rdr :: LocalRdrEnv, -- Local name envt
272 -- Maintained during renaming, of course, but also during
273 -- type checking, solely so that when renaming a Template-Haskell
274 -- splice we have the right environment for the renamer.
276 -- Does *not* include global name envt; may shadow it
277 -- Includes both ordinary variables and type variables;
278 -- they are kept distinct because tyvar have a different
279 -- occurrence contructor (Name.TvOcc)
280 -- We still need the unsullied global name env so that
281 -- we can look up record field names
283 tcl_env :: NameEnv TcTyThing, -- The local type environment: Ids and TyVars
284 -- defined in this module
286 tcl_tyvars :: TcRef TcTyVarSet, -- The "global tyvars"
287 -- Namely, the in-scope TyVars bound in tcl_lenv,
288 -- plus the tyvars mentioned in the types of Ids bound in tcl_lenv
289 -- Why mutable? see notes with tcGetGlobalTyVars
291 tcl_lie :: TcRef LIE -- Place to accumulate type constraints
294 ---------------------------
295 -- Template Haskell levels
296 ---------------------------
298 type ThLevel = Int -- Always >= 0
301 = Comp -- Ordinary compiling, at level topLevel
302 | Splice ThLevel -- Inside a splice
303 | Brack ThLevel -- Inside brackets;
304 (TcRef [PendingSplice]) -- accumulate pending splices here
305 (TcRef LIE) -- and type constraints here
306 topStage, topSpliceStage :: ThStage
308 topSpliceStage = Splice (topLevel - 1) -- Stage for the body of a top-level splice
311 impLevel, topLevel :: ThLevel
312 topLevel = 1 -- Things defined at top level of this module
313 impLevel = 0 -- Imported things; they can be used inside a top level splice
317 -- g1 = $(map ...) is OK
318 -- g2 = $(f ...) is not OK; because we havn't compiled f yet
321 ---------------------------
322 -- Arrow-notation stages
323 ---------------------------
325 -- In arrow notation, a variable bound by a proc (or enclosed let/kappa)
326 -- is not in scope to the left of an arrow tail (-<). For example
328 -- proc x -> (e1 -< e2)
330 -- Here, x is not in scope in e1, but it is in scope in e2. This can get
331 -- a bit complicated:
334 -- prox y -> (proc z -> e1) -< e2
336 -- Here, x and z are in scope in e1, but y is not. Here's how we track this:
337 -- a) Assign an "proc level" to each proc, being the number of
338 -- lexically-enclosing procs + 1.
339 -- b) Assign to each local variable the proc-level of its lexically
341 -- c) Keep a list of out-of-scope procs. When moving to the left of
342 -- an arrow-tail, add the proc-level of the immediately enclosing
344 -- d) When looking up a variable, complain if its proc-level is in
347 type ProcLevel = Int -- Always >= 0
348 topProcLevel = 0 -- Not inside any proc
350 data ArrowCtxt = ArrCtxt { proc_level :: ProcLevel, -- Current level
351 proc_banned :: [ProcLevel] } -- Out of scope proc-levels
353 topArrowCtxt = ArrCtxt { proc_level = topProcLevel, proc_banned = [] }
355 ---------------------------
357 ---------------------------
360 = AGlobal TyThing -- Used only in the return type of a lookup
361 | ATcId TcId ThLevel ProcLevel -- Ids defined in this module; may not be fully zonked
362 | ATyVar TyVar -- Type variables
363 | AThing TcKind -- Used temporarily, during kind checking, for the
364 -- tycons and clases in this recursive group
366 instance Outputable TcTyThing where -- Debugging only
367 ppr (AGlobal g) = text "AGlobal" <+> ppr g
368 ppr (ATcId g tl pl) = text "ATcId" <+> ppr g <+> ppr tl <+> ppr pl
369 ppr (ATyVar t) = text "ATyVar" <+> ppr t
370 ppr (AThing k) = text "AThing" <+> ppr k
374 type ErrCtxt = [TidyEnv -> TcM (TidyEnv, Message)]
375 -- Innermost first. Monadic so that we have a chance
376 -- to deal with bound type variables just before error
377 -- message construction
381 %************************************************************************
385 %************************************************************************
387 EntityUsage tells what things are actually need in order to compile this
388 module. It is used for generating the usage-version field of the ModIface.
390 Note that we do not record version info for entities from
391 other (non-home) packages. If the package changes, GHC doesn't help.
394 type EntityUsage = NameSet
395 -- The Names are all the (a) home-package
396 -- (b) "big" (i.e. no data cons, class ops)
397 -- (c) non-locally-defined
399 -- names that have been slurped in so far.
400 -- This is used to generate the "usage" information for this module.
402 emptyUsages :: EntityUsage
403 emptyUsages = emptyNameSet
407 %************************************************************************
409 Operations over ImportAvails
411 %************************************************************************
413 ImportAvails summarises what was imported from where, irrespective
414 of whether the imported things are actually used or not
415 It is used * when processing the export list
416 * when constructing usage info for the inteface file
417 * to identify the list of directly imported modules
418 for initialisation purposes
419 * when figuring out what things are really unused
425 -- All the things that are available from the import
426 -- Its domain is all the "main" things;
427 -- i.e. *excluding* class ops and constructors
428 -- (which appear inside their parent AvailTC)
430 imp_qual :: ModuleEnv AvailEnv,
431 -- Used to figure out "module M" export specifiers
432 -- (see 1.4 Report Section 5.1.1). Ultimately, we want to find
433 -- everything that is unambiguously in scope as 'M.x'
434 -- and where plain 'x' is (perhaps ambiguously) in scope.
435 -- So the starting point is all things that are in scope as 'M.x',
436 -- which is what this field tells us.
438 -- Domain is the *module qualifier* for imports.
439 -- e.g. import List as Foo
440 -- would add a binding Foo |-> ...stuff from List...
442 -- We keep the stuff as an AvailEnv so that it's easy to
443 -- combine stuff coming from different (unqualified)
444 -- imports of the same module
446 imp_mods :: ModuleEnv (Module, Maybe Bool, SrcSpan),
447 -- Domain is all directly-imported modules
448 -- Maybe value answers the question "is the import restricted?"
449 -- Nothing => unrestricted import (e.g., "import Foo")
450 -- Just True => restricted import, at least one entity (e.g., "import Foo(x)")
451 -- Just False => fully restricted import (e.g., "import Foo ()")
453 -- A distinction is made between the first and the third in order
454 -- to more precisely emit warnings about unused imports.
456 -- We need the Module in the range because we can't get
457 -- the keys of a ModuleEnv
459 -- (a) to help construct the usage information in
460 -- the interface file; if we import everything we
461 -- need to recompile if the module version changes
462 -- (b) to specify what child modules to initialise
464 imp_dep_mods :: ModuleEnv (ModuleName, IsBootInterface),
465 -- Home-package modules needed by the module being compiled
467 -- It doesn't matter whether any of these dependencies are actually
468 -- *used* when compiling the module; they are listed if they are below
469 -- it at all. For example, suppose M imports A which imports X. Then
470 -- compiling M might not need to consult X.hi, but X is still listed
471 -- in M's dependencies.
473 imp_dep_pkgs :: [PackageName],
474 -- Packages needed by the module being compiled, whether
475 -- directly, or via other modules in this package, or via
476 -- modules imported from other packages.
478 imp_orphs :: [ModuleName]
479 -- Orphan modules below us in the import tree
482 mkModDeps :: [(ModuleName, IsBootInterface)]
483 -> ModuleEnv (ModuleName, IsBootInterface)
484 mkModDeps deps = foldl add emptyModuleEnv deps
486 add env elt@(m,_) = extendModuleEnvByName env m elt
488 emptyImportAvails :: ImportAvails
489 emptyImportAvails = ImportAvails { imp_env = emptyAvailEnv,
490 imp_qual = emptyModuleEnv,
491 imp_mods = emptyModuleEnv,
492 imp_dep_mods = emptyModuleEnv,
496 plusImportAvails :: ImportAvails -> ImportAvails -> ImportAvails
498 (ImportAvails { imp_env = env1, imp_qual = unqual1, imp_mods = mods1,
499 imp_dep_mods = dmods1, imp_dep_pkgs = dpkgs1, imp_orphs = orphs1 })
500 (ImportAvails { imp_env = env2, imp_qual = unqual2, imp_mods = mods2,
501 imp_dep_mods = dmods2, imp_dep_pkgs = dpkgs2, imp_orphs = orphs2 })
502 = ImportAvails { imp_env = env1 `plusAvailEnv` env2,
503 imp_qual = plusModuleEnv_C plusAvailEnv unqual1 unqual2,
504 imp_mods = mods1 `plusModuleEnv` mods2,
505 imp_dep_mods = plusModuleEnv_C plus_mod_dep dmods1 dmods2,
506 imp_dep_pkgs = dpkgs1 `unionLists` dpkgs2,
507 imp_orphs = orphs1 `unionLists` orphs2 }
509 plus_mod_dep (m1, boot1) (m2, boot2)
510 = WARN( not (m1 == m2), (ppr m1 <+> ppr m2) $$ (ppr boot1 <+> ppr boot2) )
511 -- Check mod-names match
512 (m1, boot1 && boot2) -- If either side can "see" a non-hi-boot interface, use that
515 %************************************************************************
517 Avails, AvailEnv, etc
519 v%************************************************************************
522 plusAvail (Avail n1) (Avail n2) = Avail n1
523 plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n2 (ns1 `unionLists` ns2)
526 plusAvail a1 a2 = pprPanic "RnEnv.plusAvail" (hsep [ppr a1,ppr a2])
529 -------------------------
530 pruneAvails :: (Name -> Bool) -- Keep if this is True
533 pruneAvails keep avails
534 = mapMaybe del avails
536 del :: AvailInfo -> Maybe AvailInfo -- Nothing => nothing left!
537 del (Avail n) | keep n = Just (Avail n)
538 | otherwise = Nothing
539 del (AvailTC n ns) | null ns' = Nothing
540 | otherwise = Just (AvailTC n ns')
545 ---------------------------------------
547 ---------------------------------------
550 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
552 emptyAvailEnv :: AvailEnv
553 emptyAvailEnv = emptyNameEnv
555 unitAvailEnv :: AvailInfo -> AvailEnv
556 unitAvailEnv a = unitNameEnv (availName a) a
558 plusAvailEnv :: AvailEnv -> AvailEnv -> AvailEnv
559 plusAvailEnv = plusNameEnv_C plusAvail
561 lookupAvailEnv_maybe :: AvailEnv -> Name -> Maybe AvailInfo
562 lookupAvailEnv_maybe = lookupNameEnv
564 lookupAvailEnv :: AvailEnv -> Name -> AvailInfo
565 lookupAvailEnv env n = case lookupNameEnv env n of
567 Nothing -> pprPanic "lookupAvailEnv" (ppr n)
569 availEnvElts = nameEnvElts
571 addAvail :: AvailEnv -> AvailInfo -> AvailEnv
572 addAvail avails avail = extendNameEnv_C plusAvail avails (availName avail) avail
574 mkAvailEnv :: [AvailInfo] -> AvailEnv
575 -- 'avails' may have several items with the same availName
576 -- E.g import Ix( Ix(..), index )
577 -- will give Ix(Ix,index,range) and Ix(index)
578 -- We want to combine these; addAvail does that
579 mkAvailEnv avails = foldl addAvail emptyAvailEnv avails
582 %************************************************************************
584 \subsection{Where from}
586 %************************************************************************
588 The @WhereFrom@ type controls where the renamer looks for an interface file
592 = ImportByUser IsBootInterface -- Ordinary user import (perhaps {-# SOURCE #-})
593 | ImportBySystem -- Non user import.
595 instance Outputable WhereFrom where
596 ppr (ImportByUser is_boot) | is_boot = ptext SLIT("{- SOURCE -}")
598 ppr ImportBySystem = ptext SLIT("{- SYSTEM -}")
602 %************************************************************************
604 \subsection[Inst-types]{@Inst@ types}
606 v%************************************************************************
608 An @Inst@ is either a dictionary, an instance of an overloaded
609 literal, or an instance of an overloaded value. We call the latter a
610 ``method'' even though it may not correspond to a class operation.
611 For example, we might have an instance of the @double@ function at
612 type Int, represented by
614 Method 34 doubleId [Int] origin
626 TcId -- The overloaded function
627 -- This function will be a global, local, or ClassOpId;
628 -- inside instance decls (only) it can also be an InstId!
629 -- The id needn't be completely polymorphic.
630 -- You'll probably find its name (for documentation purposes)
631 -- inside the InstOrigin
633 [TcType] -- The types to which its polymorphic tyvars
634 -- should be instantiated.
635 -- These types must saturate the Id's foralls.
637 TcThetaType -- The (types of the) dictionaries to which the function
638 -- must be applied to get the method
640 TcTauType -- The tau-type of the method
644 -- INVARIANT 1: in (Method u f tys theta tau loc)
645 -- type of (f tys dicts(from theta)) = tau
647 -- INVARIANT 2: tau must not be of form (Pred -> Tau)
648 -- Reason: two methods are considerd equal if the
649 -- base Id matches, and the instantiating types
650 -- match. The TcThetaType should then match too.
651 -- This only bites in the call to tcInstClassOp in TcClassDcl.mkMethodBind
655 HsOverLit -- The literal from the occurrence site
656 -- INVARIANT: never a rebindable-syntax literal
657 -- Reason: tcSyntaxName does unification, and we
658 -- don't want to deal with that during tcSimplify
659 TcType -- The type at which the literal is used
663 @Insts@ are ordered by their class/type info, rather than by their
664 unique. This allows the context-reduction mechanism to use standard finite
665 maps to do their stuff.
668 instance Ord Inst where
671 instance Eq Inst where
672 (==) i1 i2 = case i1 `cmpInst` i2 of
676 cmpInst (Dict _ pred1 _) (Dict _ pred2 _) = pred1 `tcCmpPred` pred2
677 cmpInst (Dict _ _ _) other = LT
679 cmpInst (Method _ _ _ _ _ _) (Dict _ _ _) = GT
680 cmpInst (Method _ id1 tys1 _ _ _) (Method _ id2 tys2 _ _ _) = (id1 `compare` id2) `thenCmp` (tys1 `tcCmpTypes` tys2)
681 cmpInst (Method _ _ _ _ _ _) other = LT
683 cmpInst (LitInst _ _ _ _) (Dict _ _ _) = GT
684 cmpInst (LitInst _ _ _ _) (Method _ _ _ _ _ _) = GT
685 cmpInst (LitInst _ lit1 ty1 _) (LitInst _ lit2 ty2 _) = (lit1 `compare` lit2) `thenCmp` (ty1 `tcCmpType` ty2)
689 %************************************************************************
691 \subsection[Inst-collections]{LIE: a collection of Insts}
693 %************************************************************************
698 isEmptyLIE = isEmptyBag
700 unitLIE inst = unitBag inst
701 mkLIE insts = listToBag insts
702 plusLIE lie1 lie2 = lie1 `unionBags` lie2
703 consLIE inst lie = inst `consBag` lie
704 plusLIEs lies = unionManyBags lies
705 lieToList = bagToList
706 listToLIE = listToBag
710 %************************************************************************
712 \subsection[Inst-origin]{The @InstOrigin@ type}
714 %************************************************************************
716 The @InstOrigin@ type gives information about where a dictionary came from.
717 This is important for decent error message reporting because dictionaries
718 don't appear in the original source code. Doubtless this type will evolve...
720 It appears in TcMonad because there are a couple of error-message-generation
721 functions that deal with it.
724 data InstLoc = InstLoc InstOrigin SrcSpan ErrCtxt
726 instLocSrcLoc :: InstLoc -> SrcLoc
727 instLocSrcLoc (InstLoc _ src_span _) = srcSpanStart src_span
729 instLocSrcSpan :: InstLoc -> SrcSpan
730 instLocSrcSpan (InstLoc _ src_span _) = src_span
733 = OccurrenceOf Name -- Occurrence of an overloaded identifier
735 | IPOccOrigin (IPName Name) -- Occurrence of an implicit parameter
736 | IPBindOrigin (IPName Name) -- Binding site of an implicit parameter
740 | DataDeclOrigin -- Typechecking a data declaration
742 | InstanceDeclOrigin -- Typechecking an instance decl
744 | LiteralOrigin HsOverLit -- Occurrence of a literal
746 | PatOrigin (Pat Name)
748 | ArithSeqOrigin (ArithSeqInfo Name) -- [x..], [x..y] etc
749 | PArrSeqOrigin (ArithSeqInfo Name) -- [:x..y:] and [:x,y..z:]
751 | SignatureOrigin -- A dict created from a type signature
752 | Rank2Origin -- A dict created when typechecking the argument
753 -- of a rank-2 typed function
755 | DoOrigin -- The monad for a do expression
756 | ProcOrigin -- A proc expression
758 | ClassDeclOrigin -- Manufactured during a class decl
760 | InstanceSpecOrigin Class -- in a SPECIALIZE instance pragma
763 -- When specialising instances the instance info attached to
764 -- each class is not yet ready, so we record it inside the
765 -- origin information. This is a bit of a hack, but it works
766 -- fine. (Patrick is to blame [WDP].)
768 | ValSpecOrigin Name -- in a SPECIALIZE pragma for a value
770 -- Argument or result of a ccall
771 -- Dictionaries with this origin aren't actually mentioned in the
772 -- translated term, and so need not be bound. Nor should they
773 -- be abstracted over.
775 | UnknownOrigin -- Help! I give up...
779 pprInstLoc :: InstLoc -> SDoc
780 pprInstLoc (InstLoc orig locn ctxt)
781 = hsep [text "arising from", pp_orig orig, text "at", ppr locn]
783 pp_orig (OccurrenceOf name)
784 = hsep [ptext SLIT("use of"), quotes (ppr name)]
785 pp_orig (IPOccOrigin name)
786 = hsep [ptext SLIT("use of implicit parameter"), quotes (ppr name)]
787 pp_orig (IPBindOrigin name)
788 = hsep [ptext SLIT("binding for implicit parameter"), quotes (ppr name)]
789 pp_orig RecordUpdOrigin
790 = ptext SLIT("a record update")
791 pp_orig DataDeclOrigin
792 = ptext SLIT("the data type declaration")
793 pp_orig InstanceDeclOrigin
794 = ptext SLIT("the instance declaration")
795 pp_orig (LiteralOrigin lit)
796 = hsep [ptext SLIT("the literal"), quotes (ppr lit)]
797 pp_orig (PatOrigin pat)
798 = hsep [ptext SLIT("the pattern"), quotes (ppr pat)]
799 pp_orig (ArithSeqOrigin seq)
800 = hsep [ptext SLIT("the arithmetic sequence"), quotes (ppr seq)]
801 pp_orig (PArrSeqOrigin seq)
802 = hsep [ptext SLIT("the parallel array sequence"), quotes (ppr seq)]
803 pp_orig (SignatureOrigin)
804 = ptext SLIT("a type signature")
805 pp_orig (Rank2Origin)
806 = ptext SLIT("a function with an overloaded argument type")
808 = ptext SLIT("a do statement")
810 = ptext SLIT("a proc expression")
811 pp_orig (ClassDeclOrigin)
812 = ptext SLIT("a class declaration")
813 pp_orig (InstanceSpecOrigin clas ty)
814 = hsep [text "a SPECIALIZE instance pragma; class",
815 quotes (ppr clas), text "type:", ppr ty]
816 pp_orig (ValSpecOrigin name)
817 = hsep [ptext SLIT("a SPECIALIZE user-pragma for"), quotes (ppr name)]
818 pp_orig (UnknownOrigin)
819 = ptext SLIT("...oops -- I don't know where the overloading came from!")