Refactor (again) the handling of default methods
[ghc-hetmet.git] / compiler / rename / RnPat.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnPat]{Renaming of patterns}
5
6 Basically dependency analysis.
7
8 Handles @Match@, @GRHSs@, @HsExpr@, and @Qualifier@ datatypes.  In
9 general, all of these functions return a renamed thing, and a set of
10 free variables.
11
12 \begin{code}
13 module RnPat (-- main entry points
14               rnPat, rnPats, rnBindPat,
15
16               NameMaker, applyNameMaker,     -- a utility for making names:
17               localRecNameMaker, topRecNameMaker,  --   sometimes we want to make local names,
18                                              --   sometimes we want to make top (qualified) names.
19
20               rnHsRecFields1, HsRecFieldContext(..),
21
22               -- Literals
23               rnLit, rnOverLit,     
24
25              -- Pattern Error messages that are also used elsewhere
26              checkTupSize, patSigErr
27              ) where
28
29 -- ENH: thin imports to only what is necessary for patterns
30
31 import {-# SOURCE #-} RnExpr ( rnLExpr )
32 #ifdef GHCI
33 import {-# SOURCE #-} TcSplice ( runQuasiQuotePat )
34 #endif  /* GHCI */
35
36 #include "HsVersions.h"
37
38 import HsSyn            
39 import TcRnMonad
40 import TcHsSyn          ( hsOverLitName )
41 import RnEnv
42 import RnTypes
43 import DynFlags         ( DynFlag(..) )
44 import PrelNames
45 import Constants        ( mAX_TUPLE_SIZE )
46 import Name
47 import NameSet
48 import RdrName
49 import BasicTypes
50 import ListSetOps       ( removeDups, minusList )
51 import Outputable
52 import SrcLoc
53 import FastString
54 import Literal          ( inCharRange )
55 import Control.Monad    ( when )
56 \end{code}
57
58
59 %*********************************************************
60 %*                                                      *
61         The CpsRn Monad
62 %*                                                      *
63 %*********************************************************
64
65 Note [CpsRn monad]
66 ~~~~~~~~~~~~~~~~~~
67 The CpsRn monad uses continuation-passing style to support this
68 style of programming:
69
70         do { ...
71            ; ns <- bindNames rs
72            ; ...blah... }
73
74    where rs::[RdrName], ns::[Name]
75
76 The idea is that '...blah...' 
77   a) sees the bindings of ns
78   b) returns the free variables it mentions
79      so that bindNames can report unused ones
80
81 In particular, 
82     mapM rnPatAndThen [p1, p2, p3]
83 has a *left-to-right* scoping: it makes the binders in 
84 p1 scope over p2,p3.
85
86 \begin{code}
87 newtype CpsRn b = CpsRn { unCpsRn :: forall r. (b -> RnM (r, FreeVars))
88                                             -> RnM (r, FreeVars) }
89         -- See Note [CpsRn monad]
90
91 instance Monad CpsRn where
92   return x = CpsRn (\k -> k x)
93   (CpsRn m) >>= mk = CpsRn (\k -> m (\v -> unCpsRn (mk v) k))
94
95 runCps :: CpsRn a -> RnM (a, FreeVars)
96 runCps (CpsRn m) = m (\r -> return (r, emptyFVs))
97
98 liftCps :: RnM a -> CpsRn a
99 liftCps rn_thing = CpsRn (\k -> rn_thing >>= k)
100
101 liftCpsFV :: RnM (a, FreeVars) -> CpsRn a
102 liftCpsFV rn_thing = CpsRn (\k -> do { (v,fvs1) <- rn_thing
103                                      ; (r,fvs2) <- k v
104                                      ; return (r, fvs1 `plusFV` fvs2) })
105
106 wrapSrcSpanCps :: (a -> CpsRn b) -> Located a -> CpsRn (Located b)
107 -- Set the location, and also wrap it around the value returned
108 wrapSrcSpanCps fn (L loc a)
109   = CpsRn (\k -> setSrcSpan loc $ 
110                  unCpsRn (fn a) $ \v -> 
111                  k (L loc v))
112
113 lookupConCps :: Located RdrName -> CpsRn (Located Name)
114 lookupConCps con_rdr 
115   = CpsRn (\k -> do { con_name <- lookupLocatedOccRn con_rdr
116                     ; (r, fvs) <- k con_name
117                     ; return (r, fvs `plusFV` unitFV (unLoc con_name)) })
118 \end{code}
119
120 %*********************************************************
121 %*                                                      *
122         Name makers
123 %*                                                      *
124 %*********************************************************
125
126 Externally abstract type of name makers,
127 which is how you go from a RdrName to a Name
128
129 \begin{code}
130 data NameMaker 
131   = LamMk       -- Lambdas 
132       Bool      -- True <=> report unused bindings
133                 --   (even if True, the warning only comes out 
134                 --    if -fwarn-unused-matches is on)
135
136   | LetMk       -- Let bindings, incl top level
137                 -- Do *not* check for unused bindings
138       TopLevelFlag
139       MiniFixityEnv
140
141 topRecNameMaker :: MiniFixityEnv -> NameMaker
142 topRecNameMaker fix_env = LetMk TopLevel fix_env
143
144 localRecNameMaker :: MiniFixityEnv -> NameMaker
145 localRecNameMaker fix_env = LetMk NotTopLevel fix_env 
146
147 matchNameMaker :: HsMatchContext a -> NameMaker
148 matchNameMaker ctxt = LamMk report_unused
149   where
150     -- Do not report unused names in interactive contexts
151     -- i.e. when you type 'x <- e' at the GHCi prompt
152     report_unused = case ctxt of
153                       StmtCtxt GhciStmt -> False
154                       _                 -> True
155
156 newName :: NameMaker -> Located RdrName -> CpsRn Name
157 newName (LamMk report_unused) rdr_name
158   = CpsRn (\ thing_inside -> 
159         do { name <- newLocalBndrRn rdr_name
160            ; (res, fvs) <- bindLocalName name (thing_inside name)
161            ; when report_unused $ warnUnusedMatches [name] fvs
162            ; return (res, name `delFV` fvs) })
163
164 newName (LetMk is_top fix_env) rdr_name
165   = CpsRn (\ thing_inside -> 
166         do { name <- case is_top of
167                        NotTopLevel -> newLocalBndrRn rdr_name
168                        TopLevel    -> newTopSrcBinder rdr_name
169            ; bindLocalName name $       -- Do *not* use bindLocalNameFV here
170                                         -- See Note [View pattern usage]
171              addLocalFixities fix_env [name] $
172              thing_inside name })
173                           
174     -- Note: the bindLocalName is somewhat suspicious
175     --       because it binds a top-level name as a local name.
176     --       however, this binding seems to work, and it only exists for
177     --       the duration of the patterns and the continuation;
178     --       then the top-level name is added to the global env
179     --       before going on to the RHSes (see RnSource.lhs).
180 \end{code}
181
182 Note [View pattern usage]
183 ~~~~~~~~~~~~~~~~~~~~~~~~~
184 Consider
185   let (r, (r -> x)) = x in ...
186 Here the pattern binds 'r', and then uses it *only* in the view pattern.
187 We want to "see" this use, and in let-bindings we collect all uses and
188 report unused variables at the binding level. So we must use bindLocalName
189 here, *not* bindLocalNameFV.  Trac #3943.
190
191 %*********************************************************
192 %*                                                      *
193         External entry points
194 %*                                                      *
195 %*********************************************************
196
197 There are various entry points to renaming patterns, depending on
198  (1) whether the names created should be top-level names or local names
199  (2) whether the scope of the names is entirely given in a continuation
200      (e.g., in a case or lambda, but not in a let or at the top-level,
201       because of the way mutually recursive bindings are handled)
202  (3) whether the a type signature in the pattern can bind 
203         lexically-scoped type variables (for unpacking existential 
204         type vars in data constructors)
205  (4) whether we do duplicate and unused variable checking
206  (5) whether there are fixity declarations associated with the names
207      bound by the patterns that need to be brought into scope with them.
208      
209  Rather than burdening the clients of this module with all of these choices,
210  we export the three points in this design space that we actually need:
211
212 \begin{code}
213 -- ----------- Entry point 1: rnPats -------------------
214 -- Binds local names; the scope of the bindings is entirely in the thing_inside
215 --   * allows type sigs to bind type vars
216 --   * local namemaker
217 --   * unused and duplicate checking
218 --   * no fixities
219 rnPats :: HsMatchContext Name -- for error messages
220        -> [LPat RdrName] 
221        -> ([LPat Name] -> RnM (a, FreeVars))
222        -> RnM (a, FreeVars)
223 rnPats ctxt pats thing_inside
224   = do  { envs_before <- getRdrEnvs
225
226           -- (0) bring into scope all of the type variables bound by the patterns
227           -- (1) rename the patterns, bringing into scope all of the term variables
228           -- (2) then do the thing inside.
229         ; bindPatSigTyVarsFV (collectSigTysFromPats pats)     $ 
230           unCpsRn (rnLPatsAndThen (matchNameMaker ctxt) pats) $ \ pats' -> do
231         { -- Check for duplicated and shadowed names 
232                  -- Because we don't bind the vars all at once, we can't
233                  --     check incrementally for duplicates; 
234                  -- Nor can we check incrementally for shadowing, else we'll
235                  --     complain *twice* about duplicates e.g. f (x,x) = ...
236         ; let names = collectPatsBinders pats'
237         ; addErrCtxt doc_pat $ checkDupAndShadowedNames envs_before names
238         ; thing_inside pats' } }
239   where
240     doc_pat = ptext (sLit "In") <+> pprMatchContext ctxt
241
242 rnPat :: HsMatchContext Name -- for error messages
243       -> LPat RdrName 
244       -> (LPat Name -> RnM (a, FreeVars))
245       -> RnM (a, FreeVars)     -- Variables bound by pattern do not 
246                                -- appear in the result FreeVars 
247 rnPat ctxt pat thing_inside 
248   = rnPats ctxt [pat] (\[pat'] -> thing_inside pat')
249
250 applyNameMaker :: NameMaker -> Located RdrName -> RnM Name
251 applyNameMaker mk rdr = do { (n, _fvs) <- runCps (newName mk rdr); return n }
252
253 -- ----------- Entry point 2: rnBindPat -------------------
254 -- Binds local names; in a recursive scope that involves other bound vars
255 --      e.g let { (x, Just y) = e1; ... } in ...
256 --   * does NOT allows type sig to bind type vars
257 --   * local namemaker
258 --   * no unused and duplicate checking
259 --   * fixities might be coming in
260 rnBindPat :: NameMaker
261           -> LPat RdrName
262           -> RnM (LPat Name, FreeVars)
263    -- Returned FreeVars are the free variables of the pattern,
264    -- of course excluding variables bound by this pattern 
265
266 rnBindPat name_maker pat = runCps (rnLPatAndThen name_maker pat)
267 \end{code}
268
269
270 %*********************************************************
271 %*                                                      *
272         The main event
273 %*                                                      *
274 %*********************************************************
275
276 \begin{code}
277 -- ----------- Entry point 3: rnLPatAndThen -------------------
278 -- General version: parametrized by how you make new names
279
280 rnLPatsAndThen :: NameMaker -> [LPat RdrName] -> CpsRn [LPat Name]
281 rnLPatsAndThen mk = mapM (rnLPatAndThen mk)
282   -- Despite the map, the monad ensures that each pattern binds
283   -- variables that may be mentioned in subsequent patterns in the list
284
285 --------------------
286 -- The workhorse
287 rnLPatAndThen :: NameMaker -> LPat RdrName -> CpsRn (LPat Name)
288 rnLPatAndThen nm lpat = wrapSrcSpanCps (rnPatAndThen nm) lpat
289
290 rnPatAndThen :: NameMaker -> Pat RdrName -> CpsRn (Pat Name)
291 rnPatAndThen _  (WildPat _)   = return (WildPat placeHolderType)
292 rnPatAndThen mk (ParPat pat)  = do { pat' <- rnLPatAndThen mk pat; return (ParPat pat') }
293 rnPatAndThen mk (LazyPat pat) = do { pat' <- rnLPatAndThen mk pat; return (LazyPat pat') }
294 rnPatAndThen mk (BangPat pat) = do { pat' <- rnLPatAndThen mk pat; return (BangPat pat') }
295 rnPatAndThen mk (VarPat rdr)  = do { loc <- liftCps getSrcSpanM
296                                    ; name <- newName mk (L loc rdr)
297                                    ; return (VarPat name) }
298      -- we need to bind pattern variables for view pattern expressions
299      -- (e.g. in the pattern (x, x -> y) x needs to be bound in the rhs of the tuple)
300                                      
301 rnPatAndThen mk (SigPatIn pat ty)
302   = do { patsigs <- liftCps (doptM Opt_ScopedTypeVariables)
303        ; if patsigs
304          then do { pat' <- rnLPatAndThen mk pat
305                  ; ty' <- liftCpsFV (rnHsTypeFVs tvdoc ty)
306                  ; return (SigPatIn pat' ty') }
307          else do { liftCps (addErr (patSigErr ty))
308                  ; rnPatAndThen mk (unLoc pat) } }
309   where
310     tvdoc = text "In a pattern type-signature"
311        
312 rnPatAndThen mk (LitPat lit)
313   | HsString s <- lit
314   = do { ovlStr <- liftCps (doptM Opt_OverloadedStrings)
315        ; if ovlStr 
316          then rnPatAndThen mk (mkNPat (mkHsIsString s placeHolderType) Nothing)
317          else normal_lit }
318   | otherwise = normal_lit
319   where
320     normal_lit = do { liftCps (rnLit lit); return (LitPat lit) }
321
322 rnPatAndThen _ (NPat lit mb_neg _eq)
323   = do { lit'    <- liftCpsFV $ rnOverLit lit
324        ; mb_neg' <- liftCpsFV $ case mb_neg of
325                       Nothing -> return (Nothing, emptyFVs)
326                       Just _  -> do { (neg, fvs) <- lookupSyntaxName negateName
327                                     ; return (Just neg, fvs) }
328        ; eq' <- liftCpsFV $ lookupSyntaxName eqName
329        ; return (NPat lit' mb_neg' eq') }
330
331 rnPatAndThen mk (NPlusKPat rdr lit _ _)
332   = do { new_name <- newName mk rdr
333        ; lit'  <- liftCpsFV $ rnOverLit lit
334        ; minus <- liftCpsFV $ lookupSyntaxName minusName
335        ; ge    <- liftCpsFV $ lookupSyntaxName geName
336        ; return (NPlusKPat (L (nameSrcSpan new_name) new_name) lit' ge minus) }
337                 -- The Report says that n+k patterns must be in Integral
338
339 rnPatAndThen mk (AsPat rdr pat)
340   = do { new_name <- newName mk rdr
341        ; pat' <- rnLPatAndThen mk pat
342        ; return (AsPat (L (nameSrcSpan new_name) new_name) pat') }
343
344 rnPatAndThen mk p@(ViewPat expr pat ty)
345   = do { liftCps $ do { vp_flag <- doptM Opt_ViewPatterns
346                       ; checkErr vp_flag (badViewPat p) }
347          -- Because of the way we're arranging the recursive calls,
348          -- this will be in the right context 
349        ; expr' <- liftCpsFV $ rnLExpr expr 
350        ; pat' <- rnLPatAndThen mk pat
351        ; return (ViewPat expr' pat' ty) }
352
353 rnPatAndThen mk (ConPatIn con stuff)
354    -- rnConPatAndThen takes care of reconstructing the pattern
355   = rnConPatAndThen mk con stuff
356
357 rnPatAndThen mk (ListPat pats _)
358   = do { pats' <- rnLPatsAndThen mk pats
359        ; return (ListPat pats' placeHolderType) }
360
361 rnPatAndThen mk (PArrPat pats _)
362   = do { pats' <- rnLPatsAndThen mk pats
363        ; return (PArrPat pats' placeHolderType) }
364
365 rnPatAndThen mk (TuplePat pats boxed _)
366   = do { liftCps $ checkTupSize (length pats)
367        ; pats' <- rnLPatsAndThen mk pats
368        ; return (TuplePat pats' boxed placeHolderType) }
369
370 rnPatAndThen _ (TypePat ty)
371   = do { ty' <- liftCpsFV $ rnHsTypeFVs (text "In a type pattern") ty
372        ; return (TypePat ty') }
373
374 #ifndef GHCI
375 rnPatAndThen _ p@(QuasiQuotePat {}) 
376   = pprPanic "Can't do QuasiQuotePat without GHCi" (ppr p)
377 #else
378 rnPatAndThen mk (QuasiQuotePat qq)
379   = do { pat <- liftCps $ runQuasiQuotePat qq
380        ; L _ pat' <- rnLPatAndThen mk pat
381        ; return pat' }
382 #endif  /* GHCI */
383
384 rnPatAndThen _ pat = pprPanic "rnLPatAndThen" (ppr pat)
385
386
387 --------------------
388 rnConPatAndThen :: NameMaker
389                 -> Located RdrName          -- the constructor
390                 -> HsConPatDetails RdrName 
391                 -> CpsRn (Pat Name)
392
393 rnConPatAndThen mk con (PrefixCon pats)
394   = do  { con' <- lookupConCps con
395         ; pats' <- rnLPatsAndThen mk pats
396         ; return (ConPatIn con' (PrefixCon pats')) }
397
398 rnConPatAndThen mk con (InfixCon pat1 pat2)
399   = do  { con' <- lookupConCps con
400         ; pat1' <- rnLPatAndThen mk pat1
401         ; pat2' <- rnLPatAndThen mk pat2
402         ; fixity <- liftCps $ lookupFixityRn (unLoc con')
403         ; liftCps $ mkConOpPatRn con' fixity pat1' pat2' }
404
405 rnConPatAndThen mk con (RecCon rpats)
406   = do  { con' <- lookupConCps con
407         ; rpats' <- rnHsRecPatsAndThen mk con' rpats
408         ; return (ConPatIn con' (RecCon rpats')) }
409
410 --------------------
411 rnHsRecPatsAndThen :: NameMaker
412                    -> Located Name      -- Constructor
413                    -> HsRecFields RdrName (LPat RdrName)
414                    -> CpsRn (HsRecFields Name (LPat Name))
415 rnHsRecPatsAndThen mk (L _ con) hs_rec_fields@(HsRecFields { rec_dotdot = dd })
416   = do { flds <- liftCpsFV $ rnHsRecFields1 (HsRecFieldPat con) VarPat hs_rec_fields
417        ; flds' <- mapM rn_field (flds `zip` [1..])
418        ; return (HsRecFields { rec_flds = flds', rec_dotdot = dd }) }
419   where 
420     rn_field (fld, n') = do { arg' <- rnLPatAndThen (nested_mk dd mk n') 
421                                                     (hsRecFieldArg fld)
422                             ; return (fld { hsRecFieldArg = arg' }) }
423
424         -- Suppress unused-match reporting for fields introduced by ".."
425     nested_mk Nothing  mk                    _  = mk
426     nested_mk (Just _) mk@(LetMk {})         _  = mk
427     nested_mk (Just n) (LamMk report_unused) n' = LamMk (report_unused && (n' <= n))
428 \end{code}
429
430
431 %************************************************************************
432 %*                                                                      *
433         Record fields
434 %*                                                                      *
435 %************************************************************************
436
437 \begin{code}
438 data HsRecFieldContext 
439   = HsRecFieldCon Name
440   | HsRecFieldPat Name
441   | HsRecFieldUpd
442
443 rnHsRecFields1 
444     :: HsRecFieldContext
445     -> (RdrName -> arg) -- When punning, use this to build a new field
446     -> HsRecFields RdrName (Located arg)
447     -> RnM ([HsRecField Name (Located arg)], FreeVars)
448
449 -- This supprisingly complicated pass
450 --   a) looks up the field name (possibly using disambiguation)
451 --   b) fills in puns and dot-dot stuff
452 -- When we we've finished, we've renamed the LHS, but not the RHS,
453 -- of each x=e binding
454
455 rnHsRecFields1 ctxt mk_arg (HsRecFields { rec_flds = flds, rec_dotdot = dotdot })
456   = do { pun_ok      <- doptM Opt_RecordPuns
457        ; disambig_ok <- doptM Opt_DisambiguateRecordFields
458        ; parent <- check_disambiguation disambig_ok mb_con
459        ; flds1 <- mapM (rn_fld pun_ok parent) flds
460        ; mapM_ (addErr . dupFieldErr ctxt) dup_flds
461        ; flds2 <- rn_dotdot dotdot mb_con flds1
462        ; return (flds2, mkFVs (getFieldIds flds2)) }
463   where
464     mb_con = case ctxt of
465                 HsRecFieldUpd     -> Nothing
466                 HsRecFieldCon con -> Just con
467                 HsRecFieldPat con -> Just con
468     doc = case mb_con of
469             Nothing  -> ptext (sLit "constructor field name")
470             Just con -> ptext (sLit "field of constructor") <+> quotes (ppr con)
471
472     name_to_arg (L loc n) = L loc (mk_arg (mkRdrUnqual (nameOccName n)))
473
474     rn_fld pun_ok parent (HsRecField { hsRecFieldId = fld
475                                      , hsRecFieldArg = arg
476                                      , hsRecPun = pun })
477       = do { fld' <- wrapLocM (lookupSubBndr parent doc) fld
478            ; arg' <- if pun 
479                      then do { checkErr pun_ok (badPun fld)
480                              ; return (name_to_arg fld') }
481                      else return arg
482            ; return (HsRecField { hsRecFieldId = fld'
483                                 , hsRecFieldArg = arg'
484                                 , hsRecPun = pun }) }
485
486     rn_dotdot Nothing _mb_con flds     -- No ".." at all
487       = return flds
488     rn_dotdot (Just {}) Nothing flds   -- ".." on record update
489       = do { addErr (badDotDot ctxt); return flds }
490     rn_dotdot (Just n) (Just con) flds -- ".." on record con/pat
491       = ASSERT( n == length flds )
492         do { loc <- getSrcSpanM -- Rather approximate
493            ; dd_flag <- doptM Opt_RecordWildCards
494            ; checkErr dd_flag (needFlagDotDot ctxt)
495
496            ; con_fields <- lookupConstructorFields con
497            ; let present_flds = getFieldIds flds
498                  absent_flds  = con_fields `minusList` present_flds
499                  extras = [ HsRecField
500                               { hsRecFieldId = L loc f
501                               , hsRecFieldArg = name_to_arg (L loc f)
502                               , hsRecPun = False }
503                           | f <- absent_flds ]
504
505            ; return (flds ++ extras) }
506
507     check_disambiguation :: Bool -> Maybe Name -> RnM Parent
508     -- When disambiguation is on, return the parent *type constructor*
509     -- That is, the parent of the data constructor.  That's the parent
510     -- to use for looking up record fields.
511     check_disambiguation disambig_ok mb_con
512       | disambig_ok, Just con <- mb_con
513       = do { env <- getGlobalRdrEnv
514            ; return (case lookupGRE_Name env con of
515                        [gre] -> gre_par gre
516                        gres  -> WARN( True, ppr con <+> ppr gres ) NoParent) }
517       | otherwise = return NoParent
518  
519     dup_flds :: [[RdrName]]
520         -- Each list represents a RdrName that occurred more than once
521         -- (the list contains all occurrences)
522         -- Each list in dup_fields is non-empty
523     (_, dup_flds) = removeDups compare (getFieldIds flds)
524
525 getFieldIds :: [HsRecField id arg] -> [id]
526 getFieldIds flds = map (unLoc . hsRecFieldId) flds
527
528 needFlagDotDot :: HsRecFieldContext -> SDoc
529 needFlagDotDot ctxt = vcat [ptext (sLit "Illegal `..' in record") <+> pprRFC ctxt,
530                             ptext (sLit "Use -XRecordWildCards to permit this")]
531
532 badDotDot :: HsRecFieldContext -> SDoc
533 badDotDot ctxt = ptext (sLit "You cannot use `..' in a record") <+> pprRFC ctxt
534
535 badPun :: Located RdrName -> SDoc
536 badPun fld = vcat [ptext (sLit "Illegal use of punning for field") <+> quotes (ppr fld),
537                    ptext (sLit "Use -XNamedFieldPuns to permit this")]
538
539 dupFieldErr :: HsRecFieldContext -> [RdrName] -> SDoc
540 dupFieldErr ctxt dups
541   = hsep [ptext (sLit "duplicate field name"), 
542           quotes (ppr (head dups)),
543           ptext (sLit "in record"), pprRFC ctxt]
544
545 pprRFC :: HsRecFieldContext -> SDoc
546 pprRFC (HsRecFieldCon {}) = ptext (sLit "construction")
547 pprRFC (HsRecFieldPat {}) = ptext (sLit "pattern")
548 pprRFC (HsRecFieldUpd {}) = ptext (sLit "update")
549 \end{code}
550
551
552 %************************************************************************
553 %*                                                                      *
554 \subsubsection{Literals}
555 %*                                                                      *
556 %************************************************************************
557
558 When literals occur we have to make sure
559 that the types and classes they involve
560 are made available.
561
562 \begin{code}
563 rnLit :: HsLit -> RnM ()
564 rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
565 rnLit _ = return ()
566
567 rnOverLit :: HsOverLit t -> RnM (HsOverLit Name, FreeVars)
568 rnOverLit lit@(OverLit {ol_val=val})
569   = do  { let std_name = hsOverLitName val
570         ; (from_thing_name, fvs) <- lookupSyntaxName std_name
571         ; let rebindable = case from_thing_name of
572                                 HsVar v -> v /= std_name
573                                 _       -> panic "rnOverLit"
574         ; return (lit { ol_witness = from_thing_name
575                       , ol_rebindable = rebindable }, fvs) }
576 \end{code}
577
578 %************************************************************************
579 %*                                                                      *
580 \subsubsection{Errors}
581 %*                                                                      *
582 %************************************************************************
583
584 \begin{code}
585 checkTupSize :: Int -> RnM ()
586 checkTupSize tup_size
587   | tup_size <= mAX_TUPLE_SIZE 
588   = return ()
589   | otherwise                  
590   = addErr (sep [ptext (sLit "A") <+> int tup_size <> ptext (sLit "-tuple is too large for GHC"),
591                  nest 2 (parens (ptext (sLit "max size is") <+> int mAX_TUPLE_SIZE)),
592                  nest 2 (ptext (sLit "Workaround: use nested tuples or define a data type"))])
593
594 patSigErr :: Outputable a => a -> SDoc
595 patSigErr ty
596   =  (ptext (sLit "Illegal signature in pattern:") <+> ppr ty)
597         $$ nest 4 (ptext (sLit "Use -XScopedTypeVariables to permit it"))
598
599 bogusCharError :: Char -> SDoc
600 bogusCharError c
601   = ptext (sLit "character literal out of range: '\\") <> char c  <> char '\''
602
603 badViewPat :: Pat RdrName -> SDoc
604 badViewPat pat = vcat [ptext (sLit "Illegal view pattern: ") <+> ppr pat,
605                        ptext (sLit "Use -XViewPatterns to enable view patterns")]
606 \end{code}