2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnExpr]{Renaming of expressions}
6 Basically dependency analysis.
8 Handles @Match@, @GRHSs@, @HsExpr@, and @Qualifier@ datatypes. In
9 general, all of these functions return a renamed thing, and a set of
14 rnMatch, rnGRHSs, rnPat, rnExpr, rnExprs, rnStmt,
18 #include "HsVersions.h"
20 import {-# SOURCE #-} RnBinds ( rnBinds )
27 import RnTypes ( rnHsTypeFVs, precParseErr, sectionPrecErr )
28 import CmdLineOpts ( DynFlag(..), opt_IgnoreAsserts )
29 import Literal ( inIntRange, inCharRange )
30 import BasicTypes ( Fixity(..), FixityDirection(..), IPName(..),
31 defaultFixity, negateFixity, compareFixity )
32 import PrelNames ( hasKey, assertIdKey,
33 eqClassName, foldrName, buildName, eqStringName,
34 cCallableClassName, cReturnableClassName,
35 monadClassName, enumClassName, ordClassName,
36 ratioDataConName, splitName, fstName, sndName,
37 ioDataConName, plusIntegerName, timesIntegerName,
39 replicatePName, mapPName, filterPName,
40 falseDataConName, trueDataConName, crossPName,
41 zipPName, lengthPName, indexPName, toPName,
42 enumFromToPName, enumFromThenToPName )
43 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
44 floatPrimTyCon, doublePrimTyCon )
45 import TysWiredIn ( intTyCon )
46 import Name ( NamedThing(..), mkSystemName, nameSrcLoc )
48 import UnicodeUtil ( stringToUtf8 )
49 import UniqFM ( isNullUFM )
50 import UniqSet ( emptyUniqSet )
51 import List ( intersectBy )
52 import ListSetOps ( removeDups )
58 *********************************************************
62 *********************************************************
65 rnPat :: RdrNamePat -> RnMS (RenamedPat, FreeVars)
67 rnPat WildPatIn = returnRn (WildPatIn, emptyFVs)
70 = lookupBndrRn name `thenRn` \ vname ->
71 returnRn (VarPatIn vname, emptyFVs)
73 rnPat (SigPatIn pat ty)
74 = doptRn Opt_GlasgowExts `thenRn` \ glaExts ->
77 then rnPat pat `thenRn` \ (pat', fvs1) ->
78 rnHsTypeFVs doc ty `thenRn` \ (ty', fvs2) ->
79 returnRn (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
81 else addErrRn (patSigErr ty) `thenRn_`
84 doc = text "a pattern type-signature"
86 rnPat (LitPatIn s@(HsString _))
87 = returnRn (LitPatIn s, unitFV eqStringName)
90 = litFVs lit `thenRn` \ fvs ->
91 returnRn (LitPatIn lit, fvs)
93 rnPat (NPatIn lit mb_neg)
94 = rnOverLit lit `thenRn` \ (lit', fvs1) ->
96 Nothing -> returnRn (Nothing, emptyFVs)
97 Just neg -> lookupSyntaxName neg `thenRn` \ neg' ->
98 returnRn (Just neg', unitFV neg')
99 ) `thenRn` \ (mb_neg', fvs2) ->
100 returnRn (NPatIn lit' mb_neg',
101 fvs1 `plusFV` fvs2 `addOneFV` eqClassName)
102 -- Needed to find equality on pattern
104 rnPat (NPlusKPatIn name lit minus)
105 = rnOverLit lit `thenRn` \ (lit', fvs) ->
106 lookupBndrRn name `thenRn` \ name' ->
107 lookupSyntaxName minus `thenRn` \ minus' ->
108 returnRn (NPlusKPatIn name' lit' minus',
109 fvs `addOneFV` ordClassName `addOneFV` minus')
111 rnPat (LazyPatIn pat)
112 = rnPat pat `thenRn` \ (pat', fvs) ->
113 returnRn (LazyPatIn pat', fvs)
115 rnPat (AsPatIn name pat)
116 = rnPat pat `thenRn` \ (pat', fvs) ->
117 lookupBndrRn name `thenRn` \ vname ->
118 returnRn (AsPatIn vname pat', fvs)
120 rnPat (ConPatIn con pats)
121 = lookupOccRn con `thenRn` \ con' ->
122 mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
123 returnRn (ConPatIn con' patslist, fvs `addOneFV` con')
125 rnPat (ConOpPatIn pat1 con _ pat2)
126 = rnPat pat1 `thenRn` \ (pat1', fvs1) ->
127 lookupOccRn con `thenRn` \ con' ->
128 rnPat pat2 `thenRn` \ (pat2', fvs2) ->
130 getModeRn `thenRn` \ mode ->
131 -- See comments with rnExpr (OpApp ...)
132 (if isInterfaceMode mode
133 then returnRn (ConOpPatIn pat1' con' defaultFixity pat2')
134 else lookupFixityRn con' `thenRn` \ fixity ->
135 mkConOpPatRn pat1' con' fixity pat2'
137 returnRn (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
140 = rnPat pat `thenRn` \ (pat', fvs) ->
141 returnRn (ParPatIn pat', fvs)
143 rnPat (ListPatIn pats)
144 = mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
145 returnRn (ListPatIn patslist, fvs `addOneFV` listTyCon_name)
147 rnPat (PArrPatIn pats)
148 = mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
149 returnRn (PArrPatIn patslist,
150 fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
152 implicit_fvs = mkFVs [lengthPName, indexPName]
154 rnPat (TuplePatIn pats boxed)
155 = mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
156 returnRn (TuplePatIn patslist boxed, fvs `addOneFV` tycon_name)
158 tycon_name = tupleTyCon_name boxed (length pats)
160 rnPat (RecPatIn con rpats)
161 = lookupOccRn con `thenRn` \ con' ->
162 rnRpats rpats `thenRn` \ (rpats', fvs) ->
163 returnRn (RecPatIn con' rpats', fvs `addOneFV` con')
165 rnPat (TypePatIn name)
166 = rnHsTypeFVs (text "type pattern") name `thenRn` \ (name', fvs) ->
167 returnRn (TypePatIn name', fvs)
170 ************************************************************************
174 ************************************************************************
177 rnMatch :: HsMatchContext RdrName -> RdrNameMatch -> RnMS (RenamedMatch, FreeVars)
179 rnMatch ctxt match@(Match pats maybe_rhs_sig grhss)
180 = pushSrcLocRn (getMatchLoc match) $
182 -- Bind pattern-bound type variables
184 rhs_sig_tys = case maybe_rhs_sig of
187 pat_sig_tys = collectSigTysFromPats pats
188 doc_sig = text "In a result type-signature"
189 doc_pat = pprMatchContext ctxt
191 bindPatSigTyVars (rhs_sig_tys ++ pat_sig_tys) $
193 -- Note that we do a single bindLocalsRn for all the
194 -- matches together, so that we spot the repeated variable in
196 bindLocalsFVRn doc_pat (collectPatsBinders pats) $ \ new_binders ->
198 mapFvRn rnPat pats `thenRn` \ (pats', pat_fvs) ->
199 rnGRHSs grhss `thenRn` \ (grhss', grhss_fvs) ->
200 doptRn Opt_GlasgowExts `thenRn` \ opt_GlasgowExts ->
201 (case maybe_rhs_sig of
202 Nothing -> returnRn (Nothing, emptyFVs)
203 Just ty | opt_GlasgowExts -> rnHsTypeFVs doc_sig ty `thenRn` \ (ty', ty_fvs) ->
204 returnRn (Just ty', ty_fvs)
205 | otherwise -> addErrRn (patSigErr ty) `thenRn_`
206 returnRn (Nothing, emptyFVs)
207 ) `thenRn` \ (maybe_rhs_sig', ty_fvs) ->
210 binder_set = mkNameSet new_binders
211 unused_binders = nameSetToList (binder_set `minusNameSet` grhss_fvs)
212 all_fvs = grhss_fvs `plusFV` pat_fvs `plusFV` ty_fvs
214 warnUnusedMatches unused_binders `thenRn_`
216 returnRn (Match pats' maybe_rhs_sig' grhss', all_fvs)
217 -- The bindLocals and bindTyVars will remove the bound FVs
221 %************************************************************************
223 \subsubsection{Guarded right-hand sides (GRHSs)}
225 %************************************************************************
228 rnGRHSs :: RdrNameGRHSs -> RnMS (RenamedGRHSs, FreeVars)
230 rnGRHSs (GRHSs grhss binds _)
231 = rnBinds binds $ \ binds' ->
232 mapFvRn rnGRHS grhss `thenRn` \ (grhss', fvGRHSs) ->
233 returnRn (GRHSs grhss' binds' placeHolderType, fvGRHSs)
235 rnGRHS (GRHS guarded locn)
236 = doptRn Opt_GlasgowExts `thenRn` \ opt_GlasgowExts ->
238 (if not (opt_GlasgowExts || is_standard_guard guarded) then
239 addWarnRn (nonStdGuardErr guarded)
244 rnStmts guarded `thenRn` \ ((_, guarded'), fvs) ->
245 returnRn (GRHS guarded' locn, fvs)
247 -- Standard Haskell 1.4 guards are just a single boolean
248 -- expression, rather than a list of qualifiers as in the
250 is_standard_guard [ResultStmt _ _] = True
251 is_standard_guard [ExprStmt _ _ _, ResultStmt _ _] = True
252 is_standard_guard other = False
255 %************************************************************************
257 \subsubsection{Expressions}
259 %************************************************************************
262 rnExprs :: [RdrNameHsExpr] -> RnMS ([RenamedHsExpr], FreeVars)
263 rnExprs ls = rnExprs' ls emptyUniqSet
265 rnExprs' [] acc = returnRn ([], acc)
266 rnExprs' (expr:exprs) acc
267 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
269 -- Now we do a "seq" on the free vars because typically it's small
270 -- or empty, especially in very long lists of constants
272 acc' = acc `plusFV` fvExpr
274 (grubby_seqNameSet acc' rnExprs') exprs acc' `thenRn` \ (exprs', fvExprs) ->
275 returnRn (expr':exprs', fvExprs)
277 -- Grubby little function to do "seq" on namesets; replace by proper seq when GHC can do seq
278 grubby_seqNameSet ns result | isNullUFM ns = result
282 Variables. We look up the variable and return the resulting name.
285 rnExpr :: RdrNameHsExpr -> RnMS (RenamedHsExpr, FreeVars)
288 = lookupOccRn v `thenRn` \ name ->
289 if name `hasKey` assertIdKey then
290 -- We expand it to (GHCerr.assert__ location)
294 returnRn (HsVar name, unitFV name)
297 = newIPName v `thenRn` \ name ->
300 Linear _ -> mkFVs [splitName, fstName, sndName]
301 Dupable _ -> emptyFVs
303 returnRn (HsIPVar name, fvs)
306 = litFVs lit `thenRn` \ fvs ->
307 returnRn (HsLit lit, fvs)
309 rnExpr (HsOverLit lit)
310 = rnOverLit lit `thenRn` \ (lit', fvs) ->
311 returnRn (HsOverLit lit', fvs)
314 = rnMatch LambdaExpr match `thenRn` \ (match', fvMatch) ->
315 returnRn (HsLam match', fvMatch)
317 rnExpr (HsApp fun arg)
318 = rnExpr fun `thenRn` \ (fun',fvFun) ->
319 rnExpr arg `thenRn` \ (arg',fvArg) ->
320 returnRn (HsApp fun' arg', fvFun `plusFV` fvArg)
322 rnExpr (OpApp e1 op _ e2)
323 = rnExpr e1 `thenRn` \ (e1', fv_e1) ->
324 rnExpr e2 `thenRn` \ (e2', fv_e2) ->
325 rnExpr op `thenRn` \ (op'@(HsVar op_name), fv_op) ->
328 -- When renaming code synthesised from "deriving" declarations
329 -- we're in Interface mode, and we should ignore fixity; assume
330 -- that the deriving code generator got the association correct
331 -- Don't even look up the fixity when in interface mode
332 getModeRn `thenRn` \ mode ->
333 (if isInterfaceMode mode
334 then returnRn (OpApp e1' op' defaultFixity e2')
335 else lookupFixityRn op_name `thenRn` \ fixity ->
336 mkOpAppRn e1' op' fixity e2'
337 ) `thenRn` \ final_e ->
340 fv_e1 `plusFV` fv_op `plusFV` fv_e2)
342 rnExpr (NegApp e neg_name)
343 = rnExpr e `thenRn` \ (e', fv_e) ->
344 lookupSyntaxName neg_name `thenRn` \ neg_name' ->
345 mkNegAppRn e' neg_name' `thenRn` \ final_e ->
346 returnRn (final_e, fv_e `addOneFV` neg_name')
349 = rnExpr e `thenRn` \ (e', fvs_e) ->
350 returnRn (HsPar e', fvs_e)
352 rnExpr section@(SectionL expr op)
353 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
354 rnExpr op `thenRn` \ (op', fvs_op) ->
355 checkSectionPrec InfixL section op' expr' `thenRn_`
356 returnRn (SectionL expr' op', fvs_op `plusFV` fvs_expr)
358 rnExpr section@(SectionR op expr)
359 = rnExpr op `thenRn` \ (op', fvs_op) ->
360 rnExpr expr `thenRn` \ (expr', fvs_expr) ->
361 checkSectionPrec InfixR section op' expr' `thenRn_`
362 returnRn (SectionR op' expr', fvs_op `plusFV` fvs_expr)
364 rnExpr (HsCCall fun args may_gc is_casm _)
365 -- Check out the comment on RnIfaces.getNonWiredDataDecl about ccalls
366 = lookupOrigNames [] `thenRn` \ implicit_fvs ->
367 rnExprs args `thenRn` \ (args', fvs_args) ->
368 returnRn (HsCCall fun args' may_gc is_casm placeHolderType,
369 fvs_args `plusFV` mkFVs [cCallableClassName,
370 cReturnableClassName,
373 rnExpr (HsSCC lbl expr)
374 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
375 returnRn (HsSCC lbl expr', fvs_expr)
377 rnExpr (HsCase expr ms src_loc)
378 = pushSrcLocRn src_loc $
379 rnExpr expr `thenRn` \ (new_expr, e_fvs) ->
380 mapFvRn (rnMatch CaseAlt) ms `thenRn` \ (new_ms, ms_fvs) ->
381 returnRn (HsCase new_expr new_ms src_loc, e_fvs `plusFV` ms_fvs)
383 rnExpr (HsLet binds expr)
384 = rnBinds binds $ \ binds' ->
385 rnExpr expr `thenRn` \ (expr',fvExpr) ->
386 returnRn (HsLet binds' expr', fvExpr)
388 rnExpr (HsWith expr binds is_with)
389 = warnCheckRn (not is_with) withWarning `thenRn_`
390 rnExpr expr `thenRn` \ (expr',fvExpr) ->
391 rnIPBinds binds `thenRn` \ (binds',fvBinds) ->
392 returnRn (HsWith expr' binds' is_with, fvExpr `plusFV` fvBinds)
394 rnExpr e@(HsDo do_or_lc stmts src_loc)
395 = pushSrcLocRn src_loc $
396 rnStmts stmts `thenRn` \ ((_, stmts'), fvs) ->
397 -- check the statement list ends in an expression
398 case last stmts' of {
399 ResultStmt _ _ -> returnRn () ;
400 _ -> addErrRn (doStmtListErr e)
402 returnRn (HsDo do_or_lc stmts' src_loc, fvs `plusFV` implicit_fvs)
404 implicit_fvs = case do_or_lc of
405 PArrComp -> mkFVs [replicatePName, mapPName, filterPName,
406 falseDataConName, trueDataConName, crossPName,
408 _ -> mkFVs [foldrName, buildName, monadClassName]
409 -- Monad stuff should not be necessary for a list comprehension
410 -- but the typechecker looks up the bind and return Ids anyway
413 rnExpr (ExplicitList _ exps)
414 = rnExprs exps `thenRn` \ (exps', fvs) ->
415 returnRn (ExplicitList placeHolderType exps', fvs `addOneFV` listTyCon_name)
417 rnExpr (ExplicitPArr _ exps)
418 = rnExprs exps `thenRn` \ (exps', fvs) ->
419 returnRn (ExplicitPArr placeHolderType exps',
420 fvs `addOneFV` toPName `addOneFV` parrTyCon_name)
422 rnExpr (ExplicitTuple exps boxity)
423 = rnExprs exps `thenRn` \ (exps', fvs) ->
424 returnRn (ExplicitTuple exps' boxity, fvs `addOneFV` tycon_name)
426 tycon_name = tupleTyCon_name boxity (length exps)
428 rnExpr (RecordCon con_id rbinds)
429 = lookupOccRn con_id `thenRn` \ conname ->
430 rnRbinds "construction" rbinds `thenRn` \ (rbinds', fvRbinds) ->
431 returnRn (RecordCon conname rbinds', fvRbinds `addOneFV` conname)
433 rnExpr (RecordUpd expr rbinds)
434 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
435 rnRbinds "update" rbinds `thenRn` \ (rbinds', fvRbinds) ->
436 returnRn (RecordUpd expr' rbinds', fvExpr `plusFV` fvRbinds)
438 rnExpr (ExprWithTySig expr pty)
439 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
440 rnHsTypeFVs (text "an expression type signature") pty `thenRn` \ (pty', fvTy) ->
441 returnRn (ExprWithTySig expr' pty', fvExpr `plusFV` fvTy)
443 rnExpr (HsIf p b1 b2 src_loc)
444 = pushSrcLocRn src_loc $
445 rnExpr p `thenRn` \ (p', fvP) ->
446 rnExpr b1 `thenRn` \ (b1', fvB1) ->
447 rnExpr b2 `thenRn` \ (b2', fvB2) ->
448 returnRn (HsIf p' b1' b2' src_loc, plusFVs [fvP, fvB1, fvB2])
451 = rnHsTypeFVs doc a `thenRn` \ (t, fvT) ->
452 returnRn (HsType t, fvT)
454 doc = text "in a type argument"
456 rnExpr (ArithSeqIn seq)
457 = rn_seq seq `thenRn` \ (new_seq, fvs) ->
458 returnRn (ArithSeqIn new_seq, fvs `addOneFV` enumClassName)
461 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
462 returnRn (From expr', fvExpr)
464 rn_seq (FromThen expr1 expr2)
465 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
466 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
467 returnRn (FromThen expr1' expr2', fvExpr1 `plusFV` fvExpr2)
469 rn_seq (FromTo expr1 expr2)
470 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
471 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
472 returnRn (FromTo expr1' expr2', fvExpr1 `plusFV` fvExpr2)
474 rn_seq (FromThenTo expr1 expr2 expr3)
475 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
476 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
477 rnExpr expr3 `thenRn` \ (expr3', fvExpr3) ->
478 returnRn (FromThenTo expr1' expr2' expr3',
479 plusFVs [fvExpr1, fvExpr2, fvExpr3])
481 rnExpr (PArrSeqIn seq)
482 = rn_seq seq `thenRn` \ (new_seq, fvs) ->
483 returnRn (PArrSeqIn new_seq,
484 fvs `plusFV` mkFVs [enumFromToPName, enumFromThenToPName])
487 -- the parser shouldn't generate these two
489 rn_seq (From _ ) = panic "RnExpr.rnExpr: Infinite parallel array!"
490 rn_seq (FromThen _ _) = panic "RnExpr.rnExpr: Infinite parallel array!"
492 rn_seq (FromTo expr1 expr2)
493 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
494 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
495 returnRn (FromTo expr1' expr2', fvExpr1 `plusFV` fvExpr2)
496 rn_seq (FromThenTo expr1 expr2 expr3)
497 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
498 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
499 rnExpr expr3 `thenRn` \ (expr3', fvExpr3) ->
500 returnRn (FromThenTo expr1' expr2' expr3',
501 plusFVs [fvExpr1, fvExpr2, fvExpr3])
504 These three are pattern syntax appearing in expressions.
505 Since all the symbols are reservedops we can simply reject them.
506 We return a (bogus) EWildPat in each case.
509 rnExpr e@EWildPat = addErrRn (patSynErr e) `thenRn_`
510 returnRn (EWildPat, emptyFVs)
512 rnExpr e@(EAsPat _ _) = addErrRn (patSynErr e) `thenRn_`
513 returnRn (EWildPat, emptyFVs)
515 rnExpr e@(ELazyPat _) = addErrRn (patSynErr e) `thenRn_`
516 returnRn (EWildPat, emptyFVs)
521 %************************************************************************
523 \subsubsection{@Rbinds@s and @Rpats@s: in record expressions}
525 %************************************************************************
529 = mapRn_ field_dup_err dup_fields `thenRn_`
530 mapFvRn rn_rbind rbinds `thenRn` \ (rbinds', fvRbind) ->
531 returnRn (rbinds', fvRbind)
533 (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rbinds ]
535 field_dup_err dups = addErrRn (dupFieldErr str dups)
537 rn_rbind (field, expr, pun)
538 = lookupGlobalOccRn field `thenRn` \ fieldname ->
539 rnExpr expr `thenRn` \ (expr', fvExpr) ->
540 returnRn ((fieldname, expr', pun), fvExpr `addOneFV` fieldname)
543 = mapRn_ field_dup_err dup_fields `thenRn_`
544 mapFvRn rn_rpat rpats `thenRn` \ (rpats', fvs) ->
545 returnRn (rpats', fvs)
547 (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rpats ]
549 field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
551 rn_rpat (field, pat, pun)
552 = lookupGlobalOccRn field `thenRn` \ fieldname ->
553 rnPat pat `thenRn` \ (pat', fvs) ->
554 returnRn ((fieldname, pat', pun), fvs `addOneFV` fieldname)
557 %************************************************************************
559 \subsubsection{@rnIPBinds@s: in implicit parameter bindings} *
561 %************************************************************************
564 rnIPBinds [] = returnRn ([], emptyFVs)
565 rnIPBinds ((n, expr) : binds)
566 = newIPName n `thenRn` \ name ->
567 rnExpr expr `thenRn` \ (expr',fvExpr) ->
568 rnIPBinds binds `thenRn` \ (binds',fvBinds) ->
569 returnRn ((name, expr') : binds', fvExpr `plusFV` fvBinds)
573 %************************************************************************
575 \subsubsection{@Stmt@s: in @do@ expressions}
577 %************************************************************************
579 Note that although some bound vars may appear in the free var set for
580 the first qual, these will eventually be removed by the caller. For
581 example, if we have @[p | r <- s, q <- r, p <- q]@, when doing
582 @[q <- r, p <- q]@, the free var set for @q <- r@ will
583 be @{r}@, and the free var set for the entire Quals will be @{r}@. This
584 @r@ will be removed only when we finally return from examining all the
588 rnStmts :: [RdrNameStmt]
589 -> RnMS (([Name], [RenamedStmt]), FreeVars)
592 = returnRn (([], []), emptyFVs)
595 = getLocalNameEnv `thenRn` \ name_env ->
596 rnStmt stmt $ \ stmt' ->
597 rnStmts stmts `thenRn` \ ((binders, stmts'), fvs) ->
598 returnRn ((binders, stmt' : stmts'), fvs)
600 rnStmt :: RdrNameStmt
601 -> (RenamedStmt -> RnMS (([Name], a), FreeVars))
602 -> RnMS (([Name], a), FreeVars)
603 -- The thing list of names returned is the list returned by the
604 -- thing_inside, plus the binders of the arguments stmt
606 -- Because of mutual recursion we have to pass in rnExpr.
608 rnStmt (ParStmt stmtss) thing_inside
609 = mapFvRn rnStmts stmtss `thenRn` \ (bndrstmtss, fv_stmtss) ->
610 let binderss = map fst bndrstmtss
611 checkBndrs all_bndrs bndrs
612 = checkRn (null (intersectBy eqOcc all_bndrs bndrs)) err `thenRn_`
613 returnRn (bndrs ++ all_bndrs)
614 eqOcc n1 n2 = nameOccName n1 == nameOccName n2
615 err = text "duplicate binding in parallel list comprehension"
617 foldlRn checkBndrs [] binderss `thenRn` \ new_binders ->
618 bindLocalNamesFV new_binders $
619 thing_inside (ParStmtOut bndrstmtss)`thenRn` \ ((rest_bndrs, result), fv_rest) ->
620 returnRn ((new_binders ++ rest_bndrs, result), fv_stmtss `plusFV` fv_rest)
622 rnStmt (BindStmt pat expr src_loc) thing_inside
623 = pushSrcLocRn src_loc $
624 rnExpr expr `thenRn` \ (expr', fv_expr) ->
625 bindPatSigTyVars (collectSigTysFromPat pat) $
626 bindLocalsFVRn doc (collectPatBinders pat) $ \ new_binders ->
627 rnPat pat `thenRn` \ (pat', fv_pat) ->
628 thing_inside (BindStmt pat' expr' src_loc) `thenRn` \ ((rest_binders, result), fvs) ->
629 returnRn ((new_binders ++ rest_binders, result),
630 fv_expr `plusFV` fvs `plusFV` fv_pat)
632 doc = text "In a pattern in 'do' binding"
634 rnStmt (ExprStmt expr _ src_loc) thing_inside
635 = pushSrcLocRn src_loc $
636 rnExpr expr `thenRn` \ (expr', fv_expr) ->
637 thing_inside (ExprStmt expr' placeHolderType src_loc) `thenRn` \ (result, fvs) ->
638 returnRn (result, fv_expr `plusFV` fvs)
640 rnStmt (ResultStmt expr src_loc) thing_inside
641 = pushSrcLocRn src_loc $
642 rnExpr expr `thenRn` \ (expr', fv_expr) ->
643 thing_inside (ResultStmt expr' src_loc) `thenRn` \ (result, fvs) ->
644 returnRn (result, fv_expr `plusFV` fvs)
646 rnStmt (LetStmt binds) thing_inside
647 = rnBinds binds $ \ binds' ->
648 let new_binders = collectHsBinders binds' in
649 thing_inside (LetStmt binds') `thenRn` \ ((rest_binders, result), fvs) ->
650 returnRn ((new_binders ++ rest_binders, result), fvs )
653 %************************************************************************
655 \subsubsection{Precedence Parsing}
657 %************************************************************************
659 @mkOpAppRn@ deals with operator fixities. The argument expressions
660 are assumed to be already correctly arranged. It needs the fixities
661 recorded in the OpApp nodes, because fixity info applies to the things
662 the programmer actually wrote, so you can't find it out from the Name.
664 Furthermore, the second argument is guaranteed not to be another
665 operator application. Why? Because the parser parses all
666 operator appications left-associatively, EXCEPT negation, which
667 we need to handle specially.
670 mkOpAppRn :: RenamedHsExpr -- Left operand; already rearranged
671 -> RenamedHsExpr -> Fixity -- Operator and fixity
672 -> RenamedHsExpr -- Right operand (not an OpApp, but might
674 -> RnMS RenamedHsExpr
676 ---------------------------
677 -- (e11 `op1` e12) `op2` e2
678 mkOpAppRn e1@(OpApp e11 op1 fix1 e12) op2 fix2 e2
680 = addErrRn (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenRn_`
681 returnRn (OpApp e1 op2 fix2 e2)
684 = mkOpAppRn e12 op2 fix2 e2 `thenRn` \ new_e ->
685 returnRn (OpApp e11 op1 fix1 new_e)
687 (nofix_error, associate_right) = compareFixity fix1 fix2
689 ---------------------------
690 -- (- neg_arg) `op` e2
691 mkOpAppRn e1@(NegApp neg_arg neg_name) op2 fix2 e2
693 = addErrRn (precParseErr (pp_prefix_minus,negateFixity) (ppr_op op2,fix2)) `thenRn_`
694 returnRn (OpApp e1 op2 fix2 e2)
697 = mkOpAppRn neg_arg op2 fix2 e2 `thenRn` \ new_e ->
698 returnRn (NegApp new_e neg_name)
700 (nofix_error, associate_right) = compareFixity negateFixity fix2
702 ---------------------------
704 mkOpAppRn e1 op1 fix1 e2@(NegApp neg_arg _) -- NegApp can occur on the right
705 | not associate_right -- We *want* right association
706 = addErrRn (precParseErr (ppr_op op1, fix1) (pp_prefix_minus, negateFixity)) `thenRn_`
707 returnRn (OpApp e1 op1 fix1 e2)
709 (_, associate_right) = compareFixity fix1 negateFixity
711 ---------------------------
713 mkOpAppRn e1 op fix e2 -- Default case, no rearrangment
714 = ASSERT2( right_op_ok fix e2,
715 ppr e1 $$ text "---" $$ ppr op $$ text "---" $$ ppr fix $$ text "---" $$ ppr e2
717 returnRn (OpApp e1 op fix e2)
719 -- Parser left-associates everything, but
720 -- derived instances may have correctly-associated things to
721 -- in the right operarand. So we just check that the right operand is OK
722 right_op_ok fix1 (OpApp _ _ fix2 _)
723 = not error_please && associate_right
725 (error_please, associate_right) = compareFixity fix1 fix2
726 right_op_ok fix1 other
729 -- Parser initially makes negation bind more tightly than any other operator
730 mkNegAppRn neg_arg neg_name
733 getModeRn `thenRn` \ mode ->
734 ASSERT( not_op_app mode neg_arg )
736 returnRn (NegApp neg_arg neg_name)
738 not_op_app SourceMode (OpApp _ _ _ _) = False
739 not_op_app mode other = True
743 mkConOpPatRn :: RenamedPat -> Name -> Fixity -> RenamedPat
746 mkConOpPatRn p1@(ConOpPatIn p11 op1 fix1 p12)
749 = addErrRn (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenRn_`
750 returnRn (ConOpPatIn p1 op2 fix2 p2)
753 = mkConOpPatRn p12 op2 fix2 p2 `thenRn` \ new_p ->
754 returnRn (ConOpPatIn p11 op1 fix1 new_p)
757 (nofix_error, associate_right) = compareFixity fix1 fix2
759 mkConOpPatRn p1 op fix p2 -- Default case, no rearrangment
760 = ASSERT( not_op_pat p2 )
761 returnRn (ConOpPatIn p1 op fix p2)
763 not_op_pat (ConOpPatIn _ _ _ _) = False
764 not_op_pat other = True
768 checkPrecMatch :: Bool -> Name -> RenamedMatch -> RnMS ()
770 checkPrecMatch False fn match
773 checkPrecMatch True op (Match (p1:p2:_) _ _)
774 -- True indicates an infix lhs
775 = getModeRn `thenRn` \ mode ->
776 -- See comments with rnExpr (OpApp ...)
777 if isInterfaceMode mode
779 else checkPrec op p1 False `thenRn_`
782 checkPrecMatch True op _ = panic "checkPrecMatch"
784 checkPrec op (ConOpPatIn _ op1 _ _) right
785 = lookupFixityRn op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
786 lookupFixityRn op1 `thenRn` \ op1_fix@(Fixity op1_prec op1_dir) ->
788 inf_ok = op1_prec > op_prec ||
789 (op1_prec == op_prec &&
790 (op1_dir == InfixR && op_dir == InfixR && right ||
791 op1_dir == InfixL && op_dir == InfixL && not right))
793 info = (ppr_op op, op_fix)
794 info1 = (ppr_op op1, op1_fix)
795 (infol, infor) = if right then (info, info1) else (info1, info)
797 checkRn inf_ok (precParseErr infol infor)
799 checkPrec op pat right
802 -- Check precedence of (arg op) or (op arg) respectively
803 -- If arg is itself an operator application, then either
804 -- (a) its precedence must be higher than that of op
805 -- (b) its precedency & associativity must be the same as that of op
806 checkSectionPrec direction section op arg
808 OpApp _ op fix _ -> go_for_it (ppr_op op) fix
809 NegApp _ _ -> go_for_it pp_prefix_minus negateFixity
813 go_for_it pp_arg_op arg_fix@(Fixity arg_prec assoc)
814 = lookupFixityRn op_name `thenRn` \ op_fix@(Fixity op_prec _) ->
815 checkRn (op_prec < arg_prec
816 || op_prec == arg_prec && direction == assoc)
817 (sectionPrecErr (ppr_op op_name, op_fix)
818 (pp_arg_op, arg_fix) section)
822 %************************************************************************
824 \subsubsection{Literals}
826 %************************************************************************
828 When literals occur we have to make sure
829 that the types and classes they involve
834 = checkRn (inCharRange c) (bogusCharError c) `thenRn_`
835 returnRn (unitFV charTyCon_name)
837 litFVs (HsCharPrim c) = returnRn (unitFV (getName charPrimTyCon))
838 litFVs (HsString s) = returnRn (mkFVs [listTyCon_name, charTyCon_name])
839 litFVs (HsStringPrim s) = returnRn (unitFV (getName addrPrimTyCon))
840 litFVs (HsInt i) = returnRn (unitFV (getName intTyCon))
841 litFVs (HsIntPrim i) = returnRn (unitFV (getName intPrimTyCon))
842 litFVs (HsFloatPrim f) = returnRn (unitFV (getName floatPrimTyCon))
843 litFVs (HsDoublePrim d) = returnRn (unitFV (getName doublePrimTyCon))
844 litFVs (HsLitLit l bogus_ty) = returnRn (unitFV cCallableClassName)
845 litFVs lit = pprPanic "RnExpr.litFVs" (ppr lit) -- HsInteger and HsRat only appear
846 -- in post-typechecker translations
848 rnOverLit (HsIntegral i from_integer_name)
849 = lookupSyntaxName from_integer_name `thenRn` \ from_integer_name' ->
851 returnRn (HsIntegral i from_integer_name', unitFV from_integer_name')
853 fvs = mkFVs [plusIntegerName, timesIntegerName]
854 -- Big integer literals are built, using + and *,
855 -- out of small integers (DsUtils.mkIntegerLit)
856 -- [NB: plusInteger, timesInteger aren't rebindable...
857 -- they are used to construct the argument to fromInteger,
858 -- which is the rebindable one.]
860 returnRn (HsIntegral i from_integer_name', fvs `addOneFV` from_integer_name')
862 rnOverLit (HsFractional i from_rat_name)
863 = lookupSyntaxName from_rat_name `thenRn` \ from_rat_name' ->
865 fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
866 -- We have to make sure that the Ratio type is imported with
867 -- its constructor, because literals of type Ratio t are
868 -- built with that constructor.
869 -- The Rational type is needed too, but that will come in
870 -- when fractionalClass does.
871 -- The plus/times integer operations may be needed to construct the numerator
872 -- and denominator (see DsUtils.mkIntegerLit)
874 returnRn (HsFractional i from_rat_name', fvs `addOneFV` from_rat_name')
877 %************************************************************************
879 \subsubsection{Assertion utils}
881 %************************************************************************
884 mkAssertExpr :: RnMS (RenamedHsExpr, FreeVars)
886 lookupOrigName assertErr_RDR `thenRn` \ name ->
887 getSrcLocRn `thenRn` \ sloc ->
889 -- if we're ignoring asserts, return (\ _ e -> e)
890 -- if not, return (assertError "src-loc")
892 if opt_IgnoreAsserts then
893 getUniqRn `thenRn` \ uniq ->
895 vname = mkSystemName uniq FSLIT("v")
896 expr = HsLam ignorePredMatch
897 loc = nameSrcLoc vname
898 ignorePredMatch = mkSimpleMatch [WildPatIn, VarPatIn vname] (HsVar vname) placeHolderType loc
900 returnRn (expr, unitFV name)
905 (HsLit (HsStringPrim (mkFastString (stringToUtf8 (showSDoc (ppr sloc))))))
907 returnRn (expr, unitFV name)
910 %************************************************************************
912 \subsubsection{Errors}
914 %************************************************************************
917 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
918 pp_prefix_minus = ptext SLIT("prefix `-'")
920 dupFieldErr str (dup:rest)
921 = hsep [ptext SLIT("duplicate field name"),
923 ptext SLIT("in record"), text str]
927 SLIT("accepting non-standard pattern guards (-fglasgow-exts to suppress this message)")
931 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
932 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
935 = sep [ptext SLIT("Pattern syntax in expression context:"),
939 = sep [ptext SLIT("`do' statements must end in expression:"),
943 = ptext SLIT("character literal out of range: '\\") <> int c <> char '\''
946 = sep [quotes (ptext SLIT("with")),
947 ptext SLIT("is deprecated, use"),
948 quotes (ptext SLIT("let")),
949 ptext SLIT("instead")]