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