5a2639523d86bafd940f22dcbe30e51cf32fc124
[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 {-# OPTIONS -w #-}
14 -- The above warning supression flag is a temporary kludge.
15 -- While working on this module you are encouraged to remove it and fix
16 -- any warnings in the module. See
17 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
18 -- for details
19
20 module RnPat (-- main entry points
21               rnPatsAndThen_LocalRightwards, rnBindPat,
22
23               NameMaker, applyNameMaker,     -- a utility for making names:
24               localRecNameMaker, topRecNameMaker,  --   sometimes we want to make local names,
25                                              --   sometimes we want to make top (qualified) names.
26
27               rnHsRecFields_Con, rnHsRecFields_Update, --rename record fields in a constructor
28                                                        --and in an update
29
30               -- Literals
31               rnLit, rnOverLit,     
32
33               -- Quasiquotation
34               rnQuasiQuote,
35
36              -- Pattern Error messages that are also used elsewhere
37              checkTupSize, patSigErr
38              ) where
39
40 -- ENH: thin imports to only what is necessary for patterns
41
42 import {-# SOURCE #-} RnExpr( rnLExpr, rnStmts)
43 #ifdef GHCI
44 import {-# SOURCE #-} TcSplice( runQuasiQuotePat )
45 #endif  /* GHCI */
46
47 #include "HsVersions.h"
48
49 import HsSyn            
50 import TcRnMonad
51 import RnEnv
52 import HscTypes         ( availNames )
53 import RnNames          ( getLocalDeclBinders, extendRdrEnvRn )
54 import RnTypes          ( rnHsTypeFVs, 
55                           mkOpFormRn, mkOpAppRn, mkNegAppRn, checkSectionPrec, mkConOpPatRn
56                            )
57 import DynFlags         ( DynFlag(..) )
58 import BasicTypes       ( FixityDirection(..) )
59 import SrcLoc           ( SrcSpan )
60 import PrelNames        ( thFAKE, hasKey, assertIdKey, assertErrorName,
61                           loopAName, choiceAName, appAName, arrAName, composeAName, firstAName,
62                           negateName, thenMName, bindMName, failMName,
63                         eqClassName, integralClassName, geName, eqName,
64                           negateName, minusName, lengthPName, indexPName,
65                           plusIntegerName, fromIntegerName, timesIntegerName,
66                           ratioDataConName, fromRationalName, fromStringName, mkUnboundName )
67 import Constants        ( mAX_TUPLE_SIZE )
68 import Name             ( Name, nameOccName, nameModule_maybe, getOccName, nameSrcSpan )
69 import OccName          ( occEnvElts )
70 import NameSet
71 import UniqFM
72 import RdrName          ( RdrName, GlobalRdrElt(..), Provenance(..),
73                           extendLocalRdrEnv, lookupLocalRdrEnv, hideSomeUnquals,
74                           mkRdrUnqual, nameRdrName, gre_name, globalRdrEnvElts, isLocalGRE )
75 import LoadIface        ( loadInterfaceForName )
76 import UniqSet          ( emptyUniqSet )
77 import List             ( nub )
78 import Util             ( isSingleton )
79 import ListSetOps       ( removeDups, minusList )
80 import Maybes           ( expectJust )
81 import Outputable
82 import SrcLoc           ( Located(..), unLoc, getLoc, cmpLocated, noLoc )
83 import FastString
84 import Literal          ( inIntRange, inCharRange )
85 import List             ( unzip4 )
86 import Bag            (foldrBag)
87
88 import ErrUtils       (Message)
89 \end{code}
90
91
92 *********************************************************
93 *                                                       *
94 \subsection{Patterns}
95 *                                                       *
96 *********************************************************
97
98 \begin{code}
99 -- externally abstract type of name makers,
100 -- which is how you go from a RdrName to a Name
101 data NameMaker = NM (forall a. Located RdrName -> (Name -> RnM (a, FreeVars))
102                                                -> RnM (a, FreeVars))
103
104 matchNameMaker :: NameMaker
105 matchNameMaker
106   = NM (\ rdr_name thing_inside -> 
107         do { names@[name] <- newLocalsRn [rdr_name]
108            ; bindLocalNamesFV names $
109              warnUnusedMatches names $
110              thing_inside name })
111                           
112 topRecNameMaker, localRecNameMaker
113   :: UniqFM (Located Fixity) -- mini fixity env for the names we're about to bind
114                              -- these fixities need to be brought into scope with the names
115   -> NameMaker
116
117 -- topNameMaker and localBindMaker do not check for unused binding
118 localRecNameMaker fix_env
119   = NM (\ rdr_name thing_inside -> 
120         do { [name] <- newLocalsRn [rdr_name]
121            ; bindLocalNamesFV_WithFixities [name] fix_env $
122              thing_inside name })
123   
124 topRecNameMaker fix_env
125   = NM (\rdr_name thing_inside -> 
126         do { mod <- getModule
127            ; name <- newTopSrcBinder mod rdr_name
128            ; bindLocalNamesFV_WithFixities [name] fix_env $
129              thing_inside name })
130                 -- Note: the bindLocalNamesFV_WithFixities is somewhat suspicious 
131                 --       because it binds a top-level name as a local name.
132                 --       however, this binding seems to work, and it only exists for
133                 --       the duration of the patterns and the continuation;
134                 --       then the top-level name is added to the global env
135                 --       before going on to the RHSes (see RnSource.lhs).
136
137 applyNameMaker :: NameMaker -> Located RdrName
138                -> (Name -> RnM (a,FreeVars)) -> RnM (a, FreeVars)
139 applyNameMaker (NM f) = f
140
141
142 -- There are various entry points to renaming patterns, depending on
143 --  (1) whether the names created should be top-level names or local names
144 --  (2) whether the scope of the names is entirely given in a continuation
145 --      (e.g., in a case or lambda, but not in a let or at the top-level,
146 --       because of the way mutually recursive bindings are handled)
147 --  (3) whether the a type signature in the pattern can bind 
148 --      lexically-scoped type variables (for unpacking existential 
149 --      type vars in data constructors)
150 --  (4) whether we do duplicate and unused variable checking
151 --  (5) whether there are fixity declarations associated with the names
152 --      bound by the patterns that need to be brought into scope with them.
153 --      
154 --  Rather than burdening the clients of this module with all of these choices,
155 --  we export the three points in this design space that we actually need:
156
157 -- entry point 1:
158 -- binds local names; the scope of the bindings is entirely in the thing_inside
159 --   allows type sigs to bind type vars
160 --   local namemaker
161 --   unused and duplicate checking
162 --   no fixities
163 rnPatsAndThen_LocalRightwards :: HsMatchContext Name -- for error messages
164                               -> [LPat RdrName] 
165                               -- the continuation gets:
166                               --    the list of renamed patterns
167                               --    the (overall) free vars of all of them
168                               -> ([LPat Name] -> RnM (a, FreeVars))
169                               -> RnM (a, FreeVars)
170
171 rnPatsAndThen_LocalRightwards ctxt pats thing_inside
172   = do  { envs_before <- getRdrEnvs
173
174           -- (0) bring into scope all of the type variables bound by the patterns
175           -- (1) rename the patterns, bringing into scope all of the term variables
176           -- (2) then do the thing inside.
177         ; bindPatSigTyVarsFV (collectSigTysFromPats pats) $ 
178           rnLPatsAndThen matchNameMaker pats    $ \ pats' ->
179             do { -- Check for duplicated and shadowed names 
180                  -- Because we don't bind the vars all at once, we can't
181                  --     check incrementally for duplicates; 
182                  -- Nor can we check incrementally for shadowing, else we'll
183                  --     complain *twice* about duplicates e.g. f (x,x) = ...
184             ; let names = collectPatsBinders pats'
185             ; checkDupNames doc_pat names
186             ; checkShadowedNames doc_pat envs_before
187                                  [(nameSrcSpan name, nameOccName name) | name <- names]
188             ; thing_inside pats' } }
189   where
190     doc_pat = ptext SLIT("In") <+> pprMatchContext ctxt
191
192
193 -- entry point 2:
194 -- binds local names; in a recursive scope that involves other bound vars
195 --      e.g let { (x, Just y) = e1; ... } in ...
196 --   does NOT allows type sig to bind type vars
197 --   local namemaker
198 --   no unused and duplicate checking
199 --   fixities might be coming in
200 rnBindPat :: NameMaker
201           -> LPat RdrName
202           -> RnM (LPat Name, 
203                        -- free variables of the pattern,
204                        -- but not including variables bound by this pattern 
205                    FreeVars)
206
207 rnBindPat name_maker pat
208   = rnLPatsAndThen name_maker [pat] $ \ [pat'] ->
209     return (pat', emptyFVs)
210
211
212 -- general version: parametrized by how you make new names
213 -- invariant: what-to-do continuation only gets called with a list whose length is the same as
214 --            the part of the pattern we're currently renaming
215 rnLPatsAndThen :: NameMaker -- how to make a new variable
216                -> [LPat RdrName]   -- part of pattern we're currently renaming
217                -> ([LPat Name] -> RnM (a, FreeVars)) -- what to do afterwards
218                -> RnM (a, FreeVars) -- renaming of the whole thing
219                
220 rnLPatsAndThen var = mapFvRnCPS (rnLPatAndThen var)
221
222
223 -- the workhorse
224 rnLPatAndThen :: NameMaker
225               -> LPat RdrName   -- part of pattern we're currently renaming
226               -> (LPat Name -> RnM (a, FreeVars)) -- what to do afterwards
227               -> RnM (a, FreeVars) -- renaming of the whole thing
228 rnLPatAndThen var@(NM varf) (L loc p) cont = 
229     setSrcSpan loc $ 
230       let reloc = L loc 
231           lcont = \ unlocated -> cont (reloc unlocated)
232       in
233        case p of
234          WildPat _   -> lcont (WildPat placeHolderType)
235
236          ParPat pat  -> rnLPatAndThen var pat $ \ pat' -> lcont (ParPat pat')
237          LazyPat pat -> rnLPatAndThen var pat $ \ pat' -> lcont (LazyPat pat')
238          BangPat pat -> rnLPatAndThen var pat $ \ pat' -> lcont (BangPat pat')
239          
240          VarPat name -> 
241             varf (reloc name) $ \ newBoundName -> 
242             lcont (VarPat newBoundName)
243                -- we need to bind pattern variables for view pattern expressions
244                -- (e.g. in the pattern (x, x -> y) x needs to be bound in the rhs of the tuple)
245                                      
246          SigPatIn pat ty -> do
247              patsigs <- doptM Opt_PatternSignatures
248              if patsigs
249               then rnLPatAndThen var pat
250                       (\ pat' -> do { (ty', fvs1) <- rnHsTypeFVs tvdoc ty
251                                     ; (res, fvs2) <- lcont (SigPatIn pat' ty')
252                                     ; return (res, fvs1 `plusFV` fvs2) })
253               else do addErr (patSigErr ty)
254                       rnLPatAndThen var pat cont
255            where
256              tvdoc = text "In a pattern type-signature"
257        
258          LitPat lit@(HsString s) -> 
259              do ovlStr <- doptM Opt_OverloadedStrings
260                 if ovlStr 
261                  then rnLPatAndThen var (reloc $ mkNPat (mkHsIsString s placeHolderType) Nothing) cont
262                  else do { rnLit lit; lcont (LitPat lit) }   -- Same as below
263       
264          LitPat lit -> do { rnLit lit; lcont (LitPat lit) }
265
266          NPat lit mb_neg eq ->
267            do { (lit', fvs1) <- rnOverLit lit
268               ; (mb_neg', fvs2) <- case mb_neg of
269                                      Nothing -> return (Nothing, emptyFVs)
270                                      Just _  -> do { (neg, fvs) <- lookupSyntaxName negateName
271                                                    ; return (Just neg, fvs) }
272               ; (eq', fvs3) <- lookupSyntaxName eqName
273               ; (res, fvs4) <- lcont (NPat lit' mb_neg' eq')
274               ; return (res, fvs1 `plusFV` fvs2 `plusFV` fvs3 `plusFV` fvs4) }
275                 -- Needed to find equality on pattern
276
277          NPlusKPat name lit _ _ ->
278            varf name $ \ new_name ->
279            do { (lit', fvs1) <- rnOverLit lit
280               ; (minus, fvs2) <- lookupSyntaxName minusName
281               ; (ge, fvs3) <- lookupSyntaxName geName
282               ; (res, fvs4) <- lcont (NPlusKPat (L (nameSrcSpan new_name) new_name) lit' ge minus)
283               ; return (res, fvs1 `plusFV` fvs2 `plusFV` fvs3 `plusFV` fvs4) }
284                 -- The Report says that n+k patterns must be in Integral
285
286          AsPat name pat ->
287            varf name $ \ new_name ->
288            rnLPatAndThen var pat $ \ pat' -> 
289            lcont (AsPat (L (nameSrcSpan new_name) new_name) pat')
290
291          ViewPat expr pat ty -> 
292            do { vp_flag <- doptM Opt_ViewPatterns
293               ; checkErr vp_flag (badViewPat p)
294                 -- because of the way we're arranging the recursive calls,
295                 -- this will be in the right context 
296               ; (expr', fv_expr) <- rnLExpr expr 
297               ; (res, fvs_res) <- rnLPatAndThen var pat $ \ pat' ->
298                                   lcont (ViewPat expr' pat' ty)
299               ; return (res, fvs_res `plusFV` fv_expr) }
300
301 #ifndef GHCI
302          pat@(QuasiQuotePat _) -> pprPanic "Can't do QuasiQuotePat without GHCi" (ppr p)
303 #else
304          QuasiQuotePat qq -> do
305              (qq', _) <- rnQuasiQuote qq
306              pat' <- runQuasiQuotePat qq'
307              rnLPatAndThen var pat' $ \ (L _ pat'') ->
308                  lcont pat''
309 #endif  /* GHCI */
310
311          ConPatIn con stuff -> 
312              -- rnConPatAndThen takes care of reconstructing the pattern
313              rnConPatAndThen var con stuff cont
314
315          ListPat pats _ -> 
316            rnLPatsAndThen var pats $ \ patslist ->
317                lcont (ListPat patslist placeHolderType)
318
319          PArrPat pats _ -> 
320            do { (res, res_fvs) <- rnLPatsAndThen var pats $ \ patslist ->
321                                   lcont (PArrPat patslist placeHolderType)
322               ; return (res, res_fvs `plusFV` implicit_fvs) }
323            where
324              implicit_fvs = mkFVs [lengthPName, indexPName]
325
326          TuplePat pats boxed _ -> 
327            do { checkTupSize (length pats)
328               ; rnLPatsAndThen var pats $ \ patslist ->
329                 lcont (TuplePat patslist boxed placeHolderType) }
330
331          TypePat name -> 
332            do { (name', fvs1) <- rnHsTypeFVs (text "In a type pattern") name
333               ; (res, fvs2) <- lcont (TypePat name')
334               ; return (res, fvs1 `plusFV` fvs2) }
335
336
337 -- helper for renaming constructor patterns
338 rnConPatAndThen :: NameMaker
339                 -> Located RdrName          -- the constructor
340                 -> HsConPatDetails RdrName 
341                 -> (LPat Name -> RnM (a, FreeVars)) -- what to do afterwards
342                 -> RnM (a, FreeVars)
343
344 rnConPatAndThen var (con@(L loc _)) (PrefixCon pats) cont
345   = do  { con' <- lookupLocatedOccRn con
346         ; (res, res_fvs) <- rnLPatsAndThen var pats $ \ pats' ->
347                             cont (L loc $ ConPatIn con' (PrefixCon pats'))
348         ; return (res, res_fvs `addOneFV` unLoc con') }
349
350 rnConPatAndThen var (con@(L loc _)) (InfixCon pat1 pat2) cont
351   = do  { con' <- lookupLocatedOccRn con
352         ; (res, res_fvs) <- rnLPatAndThen var pat1 $ \ pat1' -> 
353                             rnLPatAndThen var pat2 $ \ pat2' ->
354                             do { fixity <- lookupFixityRn (unLoc con')
355                                ; pat' <- mkConOpPatRn con' fixity pat1' pat2'
356                                ; cont (L loc pat') }
357         ; return (res, res_fvs `addOneFV` unLoc con') }
358
359 rnConPatAndThen var (con@(L loc _)) (RecCon rpats) cont
360   = do  { con' <- lookupLocatedOccRn con
361         ; (res, res_fvs) <- rnHsRecFieldsAndThen_Pattern con' var rpats $ \ rpats' -> 
362                             cont (L loc $ ConPatIn con' (RecCon rpats'))
363         ; return (res, res_fvs `addOneFV` unLoc con') }
364
365 -- what kind of record expression we're doing
366 -- the first two tell the name of the datatype constructor in question
367 -- and give a way of creating a variable to fill in a ..
368 data RnHsRecFieldsChoice a = Constructor (Located Name) (RdrName -> a)
369                            | Pattern  (Located Name) (RdrName -> a)
370                            | Update
371
372 choiceToMessage (Constructor _ _) = "construction"
373 choiceToMessage (Pattern _ _) = "pattern"
374 choiceToMessage Update = "update"
375
376 doDotDot (Constructor a b) = Just (a,b)
377 doDotDot (Pattern a b) = Just (a,b)
378 doDotDot Update        = Nothing
379
380 getChoiceName (Constructor n _) = Just n
381 getChoiceName (Pattern n _) = Just n
382 getChoiceName (Update) = Nothing
383
384
385
386 -- helper for renaming record patterns;
387 -- parameterized so that it can also be used for expressions
388 rnHsRecFieldsAndThen :: RnHsRecFieldsChoice field
389                      -- how to rename the fields (CPSed)
390                      -> (Located field -> (Located field' -> RnM (c, FreeVars)) 
391                                        -> RnM (c, FreeVars)) 
392                      -- the actual fields 
393                      -> HsRecFields RdrName (Located field)  
394                      -- what to do in the scope of the field vars
395                      -> (HsRecFields Name (Located field') -> RnM (c, FreeVars)) 
396                      -> RnM (c, FreeVars)
397 -- Haddock comments for record fields are renamed to Nothing here
398 rnHsRecFieldsAndThen choice rn_thing (HsRecFields fields dd) cont = 
399     let
400
401         -- helper to collect and report duplicate record fields
402         reportDuplicateFields doingstr fields = 
403             let 
404                 -- each list represents a RdrName that occurred more than once
405                 -- (the list contains all occurrences)
406                 -- invariant: each list in dup_fields is non-empty
407                 dup_fields :: [[RdrName]]
408                 (_, dup_fields) = removeDups compare
409                                                  (map (unLoc . hsRecFieldId) fields)
410                                              
411                 -- duplicate field reporting function
412                 field_dup_err dup_group = addErr (dupFieldErr doingstr (head dup_group))
413             in
414               mapM_ field_dup_err dup_fields
415
416         -- helper to rename each field
417         rn_field pun_ok (HsRecField field inside pun) cont = do 
418           fieldname <- lookupRecordBndr (getChoiceName choice) field
419           checkErr (not pun || pun_ok) (badPun field)
420           (res, res_fvs) <- rn_thing inside $ \ inside' -> 
421                             cont (HsRecField fieldname inside' pun) 
422           return (res, res_fvs `addOneFV` unLoc fieldname)
423
424         -- Compute the extra fields to be filled in by the dot-dot notation
425         dot_dot_fields fs con mk_field cont = do 
426             con_fields <- lookupConstructorFields (unLoc con)
427             let missing_fields = con_fields `minusList` fs
428             loc <- getSrcSpanM  -- Rather approximate
429             -- it's important that we make the RdrName fields that we morally wrote
430             -- and then rename them in the usual manner
431             -- (rather than trying to make the result of renaming directly)
432             -- because, for patterns, renaming can bind vars in the continuation
433             mapFvRnCPS rn_thing 
434              (map (L loc . mk_field . mkRdrUnqual . getOccName) missing_fields) $
435               \ rhss -> 
436                   let new_fs = [ HsRecField (L loc f) r False
437                                  | (f, r) <- missing_fields `zip` rhss ]
438                   in 
439                   cont new_fs
440
441    in do
442        -- report duplicate fields
443        let doingstr = choiceToMessage choice
444        reportDuplicateFields doingstr fields
445
446        -- rename the records as written
447        -- check whether punning (implicit x=x) is allowed
448        pun_flag <- doptM Opt_RecordPuns
449        -- rename the fields
450        mapFvRnCPS (rn_field pun_flag) fields $ \ fields1 ->
451
452            -- handle ..
453            case dd of
454              Nothing -> cont (HsRecFields fields1 dd)
455              Just n  -> ASSERT( n == length fields ) do
456                           dd_flag <- doptM Opt_RecordWildCards
457                           checkErr dd_flag (needFlagDotDot doingstr)
458                           let fld_names1 = map (unLoc . hsRecFieldId) fields1
459                           case doDotDot choice of 
460                                 Nothing -> do addErr (badDotDot doingstr)
461                                               -- we return a junk value here so that error reporting goes on
462                                               cont (HsRecFields fields1 dd)
463                                 Just (con, mk_field) ->
464                                     dot_dot_fields fld_names1 con mk_field $
465                                       \ fields2 -> 
466                                           cont (HsRecFields (fields1 ++ fields2) dd)
467
468 needFlagDotDot str = vcat [ptext SLIT("Illegal `..' in record") <+> text str,
469                           ptext SLIT("Use -XRecordWildCards to permit this")]
470
471 badDotDot str = ptext SLIT("You cannot use `..' in record") <+> text str
472
473 badPun fld = vcat [ptext SLIT("Illegal use of punning for field") <+> quotes (ppr fld),
474                    ptext SLIT("Use -XRecordPuns to permit this")]
475
476
477 -- wrappers
478 rnHsRecFieldsAndThen_Pattern :: Located Name
479                              -> NameMaker -- new name maker
480                              -> HsRecFields RdrName (LPat RdrName)  
481                              -> (HsRecFields Name (LPat Name) -> RnM (c, FreeVars)) 
482                              -> RnM (c, FreeVars)
483 rnHsRecFieldsAndThen_Pattern n var
484   = rnHsRecFieldsAndThen (Pattern n VarPat) (rnLPatAndThen var)
485
486
487 -- wrapper to use rnLExpr in CPS style;
488 -- because it does not bind any vars going forward, it does not need
489 -- to be written that way
490 rnLExprAndThen :: (LHsExpr RdrName -> RnM (LHsExpr Name, FreeVars))
491                -> LHsExpr RdrName 
492                -> (LHsExpr Name -> RnM (c, FreeVars)) 
493                -> RnM (c, FreeVars) 
494 rnLExprAndThen f e cont = do { (x, fvs1) <- f e
495                              ; (res, fvs2) <- cont x
496                              ; return (res, fvs1 `plusFV` fvs2) }
497
498
499 -- non-CPSed because exprs don't leave anything bound
500 rnHsRecFields_Con :: Located Name
501                   -> (LHsExpr RdrName -> RnM (LHsExpr Name, FreeVars))
502                   -> HsRecFields RdrName (LHsExpr RdrName)  
503                   -> RnM (HsRecFields Name (LHsExpr Name), FreeVars)
504 rnHsRecFields_Con n rnLExpr fields = rnHsRecFieldsAndThen (Constructor n HsVar) 
505                                      (rnLExprAndThen rnLExpr) fields $ \ res ->
506                                      return (res, emptyFVs)
507
508 rnHsRecFields_Update :: (LHsExpr RdrName -> RnM (LHsExpr Name, FreeVars))
509                      -> HsRecFields RdrName (LHsExpr RdrName)  
510                      -> RnM (HsRecFields Name (LHsExpr Name), FreeVars)
511 rnHsRecFields_Update rnLExpr fields = rnHsRecFieldsAndThen Update
512                                       (rnLExprAndThen rnLExpr) fields $ \ res -> 
513                                       return (res, emptyFVs)
514 \end{code}
515
516
517
518 %************************************************************************
519 %*                                                                      *
520 \subsubsection{Literals}
521 %*                                                                      *
522 %************************************************************************
523
524 When literals occur we have to make sure
525 that the types and classes they involve
526 are made available.
527
528 \begin{code}
529 rnLit :: HsLit -> RnM ()
530 rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
531 rnLit other      = return ()
532
533 rnOverLit (HsIntegral i _ _) = do
534     (from_integer_name, fvs) <- lookupSyntaxName fromIntegerName
535     if inIntRange i then
536         return (HsIntegral i from_integer_name placeHolderType, fvs)
537      else let
538         extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
539         -- Big integer literals are built, using + and *, 
540         -- out of small integers (DsUtils.mkIntegerLit)
541         -- [NB: plusInteger, timesInteger aren't rebindable... 
542         --      they are used to construct the argument to fromInteger, 
543         --      which is the rebindable one.]
544         in
545         return (HsIntegral i from_integer_name placeHolderType, fvs `plusFV` extra_fvs)
546
547 rnOverLit (HsFractional i _ _) = do
548     (from_rat_name, fvs) <- lookupSyntaxName fromRationalName
549     let
550         extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
551         -- We have to make sure that the Ratio type is imported with
552         -- its constructor, because literals of type Ratio t are
553         -- built with that constructor.
554         -- The Rational type is needed too, but that will come in
555         -- as part of the type for fromRational.
556         -- The plus/times integer operations may be needed to construct the numerator
557         -- and denominator (see DsUtils.mkIntegerLit)
558     return (HsFractional i from_rat_name placeHolderType, fvs `plusFV` extra_fvs)
559
560 rnOverLit (HsIsString s _ _) = do
561     (from_string_name, fvs) <- lookupSyntaxName fromStringName
562     return (HsIsString s from_string_name placeHolderType, fvs)
563 \end{code}
564
565 %************************************************************************
566 %*                                                                      *
567 \subsubsection{Quasiquotation}
568 %*                                                                      *
569 %************************************************************************
570
571 See Note [Quasi-quote overview] in TcSplice.
572
573 \begin{code}
574 rnQuasiQuote :: HsQuasiQuote RdrName -> RnM (HsQuasiQuote Name, FreeVars)
575 rnQuasiQuote (HsQuasiQuote n quoter quoteSpan quote)
576   = do  { loc  <- getSrcSpanM
577         ; [n'] <- newLocalsRn [L loc n]
578         ; quoter' <-  (lookupOccRn quoter)
579                 -- If 'quoter' is not in scope, proceed no further
580                 -- Otherwise lookupOcc adds an error messsage and returns 
581                 -- an "unubound name", which makes the subsequent attempt to
582                 -- run the quote fail
583         ; return (HsQuasiQuote n' quoter' quoteSpan quote, unitFV quoter') }
584 \end{code}
585
586 %************************************************************************
587 %*                                                                      *
588 \subsubsection{Errors}
589 %*                                                                      *
590 %************************************************************************
591
592 \begin{code}
593 checkTupSize :: Int -> RnM ()
594 checkTupSize tup_size
595   | tup_size <= mAX_TUPLE_SIZE 
596   = return ()
597   | otherwise                  
598   = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
599                  nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
600                  nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
601
602 patSigErr ty
603   =  (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
604         $$ nest 4 (ptext SLIT("Use -XPatternSignatures to permit it"))
605
606 dupFieldErr str dup
607   = hsep [ptext SLIT("duplicate field name"), 
608           quotes (ppr dup),
609           ptext SLIT("in record"), text str]
610
611 bogusCharError c
612   = ptext SLIT("character literal out of range: '\\") <> char c  <> char '\''
613
614 badViewPat pat = vcat [ptext SLIT("Illegal view pattern: ") <+> ppr pat,
615                        ptext SLIT("Use -XViewPatterns to enalbe view patterns")]
616
617 \end{code}