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,
18 #include "HsVersions.h"
20 import {-# SOURCE #-} RnBinds ( rnBinds )
21 import {-# SOURCE #-} RnSource ( rnHsSigType, rnHsType )
28 import RnIfaces ( lookupFixityRn )
29 import CmdLineOpts ( opt_GlasgowExts, opt_IgnoreAsserts )
30 import Literal ( inIntRange )
31 import BasicTypes ( Fixity(..), FixityDirection(..), defaultFixity, negateFixity )
32 import PrelInfo ( eqClass_RDR,
33 ccallableClass_RDR, creturnableClass_RDR,
34 monadClass_RDR, enumClass_RDR, ordClass_RDR,
35 ratioDataCon_RDR, negate_RDR, assertErr_RDR,
36 ioDataCon_RDR, plusInteger_RDR, timesInteger_RDR,
39 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
40 floatPrimTyCon, doublePrimTyCon
42 import TysWiredIn ( intTyCon, integerTyCon )
43 import Name ( NamedThing(..), mkSysLocalName, nameSrcLoc )
45 import UniqFM ( isNullUFM )
46 import FiniteMap ( elemFM )
47 import UniqSet ( emptyUniqSet )
48 import Unique ( hasKey, assertIdKey )
49 import Util ( removeDups )
50 import ListSetOps ( unionLists )
51 import Maybes ( maybeToBool )
56 *********************************************************
60 *********************************************************
63 rnPat :: RdrNamePat -> RnMS (RenamedPat, FreeVars)
65 rnPat WildPatIn = returnRn (WildPatIn, emptyFVs)
68 = lookupBndrRn name `thenRn` \ vname ->
69 returnRn (VarPatIn vname, emptyFVs)
71 rnPat (SigPatIn pat ty)
73 = rnPat pat `thenRn` \ (pat', fvs1) ->
74 rnHsType doc ty `thenRn` \ (ty', fvs2) ->
75 returnRn (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
78 = addErrRn (patSigErr ty) `thenRn_`
81 doc = text "a pattern type-signature"
84 = litFVs lit `thenRn` \ fvs ->
85 returnRn (LitPatIn lit, fvs)
88 = rnOverLit lit `thenRn` \ (lit', fvs1) ->
89 lookupOrigName eqClass_RDR `thenRn` \ eq -> -- Needed to find equality on pattern
90 returnRn (NPatIn lit', fvs1 `addOneFV` eq)
92 rnPat (NPlusKPatIn name lit minus)
93 = rnOverLit lit `thenRn` \ (lit', fvs) ->
94 lookupOrigName ordClass_RDR `thenRn` \ ord ->
95 lookupBndrRn name `thenRn` \ name' ->
96 lookupOccRn minus `thenRn` \ minus' ->
97 returnRn (NPlusKPatIn name' lit' minus', fvs `addOneFV` ord `addOneFV` minus')
100 = rnPat pat `thenRn` \ (pat', fvs) ->
101 returnRn (LazyPatIn pat', fvs)
103 rnPat (AsPatIn name pat)
104 = rnPat pat `thenRn` \ (pat', fvs) ->
105 lookupBndrRn name `thenRn` \ vname ->
106 returnRn (AsPatIn vname pat', fvs)
108 rnPat (ConPatIn con pats)
109 = lookupOccRn con `thenRn` \ con' ->
110 mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
111 returnRn (ConPatIn con' patslist, fvs `addOneFV` con')
113 rnPat (ConOpPatIn pat1 con _ pat2)
114 = rnPat pat1 `thenRn` \ (pat1', fvs1) ->
115 lookupOccRn con `thenRn` \ con' ->
116 rnPat pat2 `thenRn` \ (pat2', fvs2) ->
118 getModeRn `thenRn` \ mode ->
119 -- See comments with rnExpr (OpApp ...)
121 InterfaceMode -> returnRn (ConOpPatIn pat1' con' defaultFixity pat2')
122 SourceMode -> lookupFixityRn con' `thenRn` \ fixity ->
123 mkConOpPatRn pat1' con' fixity pat2'
125 returnRn (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
128 = rnPat pat `thenRn` \ (pat', fvs) ->
129 returnRn (ParPatIn pat', fvs)
131 rnPat (ListPatIn pats)
132 = mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
133 returnRn (ListPatIn patslist, fvs `addOneFV` listTyCon_name)
135 rnPat (TuplePatIn pats boxed)
136 = mapFvRn rnPat pats `thenRn` \ (patslist, fvs) ->
137 returnRn (TuplePatIn patslist boxed, fvs `addOneFV` tycon_name)
139 tycon_name = tupleTyCon_name boxed (length pats)
141 rnPat (RecPatIn con rpats)
142 = lookupOccRn con `thenRn` \ con' ->
143 rnRpats rpats `thenRn` \ (rpats', fvs) ->
144 returnRn (RecPatIn con' rpats', fvs `addOneFV` con')
147 ************************************************************************
151 ************************************************************************
154 rnMatch :: RdrNameMatch -> RnMS (RenamedMatch, FreeVars)
156 rnMatch match@(Match _ pats maybe_rhs_sig grhss)
157 = pushSrcLocRn (getMatchLoc match) $
159 -- Find the universally quantified type variables
160 -- in the pattern type signatures
161 getLocalNameEnv `thenRn` \ name_env ->
163 tyvars_in_sigs = rhs_sig_tyvars `unionLists` tyvars_in_pats
164 rhs_sig_tyvars = case maybe_rhs_sig of
166 Just ty -> extractHsTyRdrTyVars ty
167 tyvars_in_pats = extractPatsTyVars pats
168 forall_tyvars = filter (not . (`elemFM` name_env)) tyvars_in_sigs
169 doc_sig = text "a pattern type-signature"
170 doc_pats = text "in a pattern match"
172 bindTyVarsFVRn doc_sig (map UserTyVar forall_tyvars) $ \ sig_tyvars ->
174 -- Note that we do a single bindLocalsRn for all the
175 -- matches together, so that we spot the repeated variable in
177 bindLocalsFVRn doc_pats (collectPatsBinders pats) $ \ new_binders ->
179 mapFvRn rnPat pats `thenRn` \ (pats', pat_fvs) ->
180 rnGRHSs grhss `thenRn` \ (grhss', grhss_fvs) ->
181 (case maybe_rhs_sig of
182 Nothing -> returnRn (Nothing, emptyFVs)
183 Just ty | opt_GlasgowExts -> rnHsType doc_sig ty `thenRn` \ (ty', ty_fvs) ->
184 returnRn (Just ty', ty_fvs)
185 | otherwise -> addErrRn (patSigErr ty) `thenRn_`
186 returnRn (Nothing, emptyFVs)
187 ) `thenRn` \ (maybe_rhs_sig', ty_fvs) ->
190 binder_set = mkNameSet new_binders
191 unused_binders = nameSetToList (binder_set `minusNameSet` grhss_fvs)
192 all_fvs = grhss_fvs `plusFV` pat_fvs `plusFV` ty_fvs
194 warnUnusedMatches unused_binders `thenRn_`
196 returnRn (Match sig_tyvars pats' maybe_rhs_sig' grhss', all_fvs)
197 -- The bindLocals and bindTyVars will remove the bound FVs
200 %************************************************************************
202 \subsubsection{Guarded right-hand sides (GRHSs)}
204 %************************************************************************
207 rnGRHSs :: RdrNameGRHSs -> RnMS (RenamedGRHSs, FreeVars)
209 rnGRHSs (GRHSs grhss binds maybe_ty)
210 = ASSERT( not (maybeToBool maybe_ty) )
211 rnBinds binds $ \ binds' ->
212 mapFvRn rnGRHS grhss `thenRn` \ (grhss', fvGRHSs) ->
213 returnRn (GRHSs grhss' binds' Nothing, fvGRHSs)
215 rnGRHS (GRHS guarded locn)
216 = pushSrcLocRn locn $
217 (if not (opt_GlasgowExts || is_standard_guard guarded) then
218 addWarnRn (nonStdGuardErr guarded)
223 rnStmts rnExpr guarded `thenRn` \ (guarded', fvs) ->
224 returnRn (GRHS guarded' locn, fvs)
226 -- Standard Haskell 1.4 guards are just a single boolean
227 -- expression, rather than a list of qualifiers as in the
229 is_standard_guard [ExprStmt _ _] = True
230 is_standard_guard [GuardStmt _ _, ExprStmt _ _] = True
231 is_standard_guard other = False
234 %************************************************************************
236 \subsubsection{Expressions}
238 %************************************************************************
241 rnExprs :: [RdrNameHsExpr] -> RnMS ([RenamedHsExpr], FreeVars)
242 rnExprs ls = rnExprs' ls emptyUniqSet
244 rnExprs' [] acc = returnRn ([], acc)
245 rnExprs' (expr:exprs) acc
246 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
248 -- Now we do a "seq" on the free vars because typically it's small
249 -- or empty, especially in very long lists of constants
251 acc' = acc `plusFV` fvExpr
253 (grubby_seqNameSet acc' rnExprs') exprs acc' `thenRn` \ (exprs', fvExprs) ->
254 returnRn (expr':exprs', fvExprs)
256 -- Grubby little function to do "seq" on namesets; replace by proper seq when GHC can do seq
257 grubby_seqNameSet ns result | isNullUFM ns = result
261 Variables. We look up the variable and return the resulting name.
264 rnExpr :: RdrNameHsExpr -> RnMS (RenamedHsExpr, FreeVars)
267 = lookupOccRn v `thenRn` \ name ->
268 if name `hasKey` assertIdKey then
269 -- We expand it to (GHCerr.assert__ location)
273 returnRn (HsVar name, unitFV name)
276 = newIPName v `thenRn` \ name ->
277 returnRn (HsIPVar name, emptyFVs)
280 = litFVs lit `thenRn` \ fvs ->
281 returnRn (HsLit lit, fvs)
283 rnExpr (HsOverLit lit)
284 = rnOverLit lit `thenRn` \ (lit', fvs) ->
285 returnRn (HsOverLit lit', fvs)
288 = rnMatch match `thenRn` \ (match', fvMatch) ->
289 returnRn (HsLam match', fvMatch)
291 rnExpr (HsApp fun arg)
292 = rnExpr fun `thenRn` \ (fun',fvFun) ->
293 rnExpr arg `thenRn` \ (arg',fvArg) ->
294 returnRn (HsApp fun' arg', fvFun `plusFV` fvArg)
296 rnExpr (OpApp e1 op _ e2)
297 = rnExpr e1 `thenRn` \ (e1', fv_e1) ->
298 rnExpr e2 `thenRn` \ (e2', fv_e2) ->
299 rnExpr op `thenRn` \ (op'@(HsVar op_name), fv_op) ->
302 -- When renaming code synthesised from "deriving" declarations
303 -- we're in Interface mode, and we should ignore fixity; assume
304 -- that the deriving code generator got the association correct
305 -- Don't even look up the fixity when in interface mode
306 getModeRn `thenRn` \ mode ->
308 SourceMode -> lookupFixityRn op_name `thenRn` \ fixity ->
309 mkOpAppRn e1' op' fixity e2'
310 InterfaceMode -> returnRn (OpApp e1' op' defaultFixity e2')
311 ) `thenRn` \ final_e ->
314 fv_e1 `plusFV` fv_op `plusFV` fv_e2)
317 = rnExpr e `thenRn` \ (e', fv_e) ->
318 lookupOrigName negate_RDR `thenRn` \ neg ->
319 mkNegAppRn e' neg `thenRn` \ final_e ->
320 returnRn (final_e, fv_e `addOneFV` neg)
323 = rnExpr e `thenRn` \ (e', fvs_e) ->
324 returnRn (HsPar e', fvs_e)
326 rnExpr section@(SectionL expr op)
327 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
328 rnExpr op `thenRn` \ (op', fvs_op) ->
329 checkSectionPrec "left" section op' expr' `thenRn_`
330 returnRn (SectionL expr' op', fvs_op `plusFV` fvs_expr)
332 rnExpr section@(SectionR op expr)
333 = rnExpr op `thenRn` \ (op', fvs_op) ->
334 rnExpr expr `thenRn` \ (expr', fvs_expr) ->
335 checkSectionPrec "right" section op' expr' `thenRn_`
336 returnRn (SectionR op' expr', fvs_op `plusFV` fvs_expr)
338 rnExpr (HsCCall fun args may_gc is_casm fake_result_ty)
339 -- Check out the comment on RnIfaces.getNonWiredDataDecl about ccalls
340 = lookupOrigNames [ccallableClass_RDR,
341 creturnableClass_RDR,
342 ioDataCon_RDR] `thenRn` \ implicit_fvs ->
343 rnExprs args `thenRn` \ (args', fvs_args) ->
344 returnRn (HsCCall fun args' may_gc is_casm fake_result_ty,
345 fvs_args `plusFV` implicit_fvs)
347 rnExpr (HsSCC lbl expr)
348 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
349 returnRn (HsSCC lbl expr', fvs_expr)
351 rnExpr (HsCase expr ms src_loc)
352 = pushSrcLocRn src_loc $
353 rnExpr expr `thenRn` \ (new_expr, e_fvs) ->
354 mapFvRn rnMatch ms `thenRn` \ (new_ms, ms_fvs) ->
355 returnRn (HsCase new_expr new_ms src_loc, e_fvs `plusFV` ms_fvs)
357 rnExpr (HsLet binds expr)
358 = rnBinds binds $ \ binds' ->
359 rnExpr expr `thenRn` \ (expr',fvExpr) ->
360 returnRn (HsLet binds' expr', fvExpr)
362 rnExpr (HsWith expr binds)
363 = rnExpr expr `thenRn` \ (expr',fvExpr) ->
364 rnIPBinds binds `thenRn` \ (binds',fvBinds) ->
365 returnRn (HsWith expr' binds', fvExpr `plusFV` fvBinds)
367 rnExpr e@(HsDo do_or_lc stmts src_loc)
368 = pushSrcLocRn src_loc $
369 lookupOrigNames implicit_rdr_names `thenRn` \ implicit_fvs ->
370 rnStmts rnExpr stmts `thenRn` \ (stmts', fvs) ->
371 -- check the statement list ends in an expression
372 case last stmts' of {
373 ExprStmt _ _ -> returnRn () ;
374 ReturnStmt _ -> returnRn () ; -- for list comprehensions
375 _ -> addErrRn (doStmtListErr e)
377 returnRn (HsDo do_or_lc stmts' src_loc, fvs `plusFV` implicit_fvs)
379 implicit_rdr_names = [foldr_RDR, build_RDR, monadClass_RDR]
380 -- Monad stuff should not be necessary for a list comprehension
381 -- but the typechecker looks up the bind and return Ids anyway
385 rnExpr (ExplicitList exps)
386 = rnExprs exps `thenRn` \ (exps', fvs) ->
387 returnRn (ExplicitList exps', fvs `addOneFV` listTyCon_name)
389 rnExpr (ExplicitTuple exps boxity)
390 = rnExprs exps `thenRn` \ (exps', fvs) ->
391 returnRn (ExplicitTuple exps' boxity, fvs `addOneFV` tycon_name)
393 tycon_name = tupleTyCon_name boxity (length exps)
395 rnExpr (RecordCon con_id rbinds)
396 = lookupOccRn con_id `thenRn` \ conname ->
397 rnRbinds "construction" rbinds `thenRn` \ (rbinds', fvRbinds) ->
398 returnRn (RecordCon conname rbinds', fvRbinds `addOneFV` conname)
400 rnExpr (RecordUpd expr rbinds)
401 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
402 rnRbinds "update" rbinds `thenRn` \ (rbinds', fvRbinds) ->
403 returnRn (RecordUpd expr' rbinds', fvExpr `plusFV` fvRbinds)
405 rnExpr (ExprWithTySig expr pty)
406 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
407 rnHsSigType (text "an expression") pty `thenRn` \ (pty', fvTy) ->
408 returnRn (ExprWithTySig expr' pty', fvExpr `plusFV` fvTy)
410 rnExpr (HsIf p b1 b2 src_loc)
411 = pushSrcLocRn src_loc $
412 rnExpr p `thenRn` \ (p', fvP) ->
413 rnExpr b1 `thenRn` \ (b1', fvB1) ->
414 rnExpr b2 `thenRn` \ (b2', fvB2) ->
415 returnRn (HsIf p' b1' b2' src_loc, plusFVs [fvP, fvB1, fvB2])
417 rnExpr (ArithSeqIn seq)
418 = lookupOrigName enumClass_RDR `thenRn` \ enum ->
419 rn_seq seq `thenRn` \ (new_seq, fvs) ->
420 returnRn (ArithSeqIn new_seq, fvs `addOneFV` enum)
423 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
424 returnRn (From expr', fvExpr)
426 rn_seq (FromThen expr1 expr2)
427 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
428 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
429 returnRn (FromThen expr1' expr2', fvExpr1 `plusFV` fvExpr2)
431 rn_seq (FromTo expr1 expr2)
432 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
433 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
434 returnRn (FromTo expr1' expr2', fvExpr1 `plusFV` fvExpr2)
436 rn_seq (FromThenTo expr1 expr2 expr3)
437 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
438 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
439 rnExpr expr3 `thenRn` \ (expr3', fvExpr3) ->
440 returnRn (FromThenTo expr1' expr2' expr3',
441 plusFVs [fvExpr1, fvExpr2, fvExpr3])
444 These three are pattern syntax appearing in expressions.
445 Since all the symbols are reservedops we can simply reject them.
446 We return a (bogus) EWildPat in each case.
449 rnExpr e@EWildPat = addErrRn (patSynErr e) `thenRn_`
450 returnRn (EWildPat, emptyFVs)
452 rnExpr e@(EAsPat _ _) = addErrRn (patSynErr e) `thenRn_`
453 returnRn (EWildPat, emptyFVs)
455 rnExpr e@(ELazyPat _) = addErrRn (patSynErr e) `thenRn_`
456 returnRn (EWildPat, emptyFVs)
461 %************************************************************************
463 \subsubsection{@Rbinds@s and @Rpats@s: in record expressions}
465 %************************************************************************
469 = mapRn_ field_dup_err dup_fields `thenRn_`
470 mapFvRn rn_rbind rbinds `thenRn` \ (rbinds', fvRbind) ->
471 returnRn (rbinds', fvRbind)
473 (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rbinds ]
475 field_dup_err dups = addErrRn (dupFieldErr str dups)
477 rn_rbind (field, expr, pun)
478 = lookupGlobalOccRn field `thenRn` \ fieldname ->
479 rnExpr expr `thenRn` \ (expr', fvExpr) ->
480 returnRn ((fieldname, expr', pun), fvExpr `addOneFV` fieldname)
483 = mapRn_ field_dup_err dup_fields `thenRn_`
484 mapFvRn rn_rpat rpats `thenRn` \ (rpats', fvs) ->
485 returnRn (rpats', fvs)
487 (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rpats ]
489 field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
491 rn_rpat (field, pat, pun)
492 = lookupGlobalOccRn field `thenRn` \ fieldname ->
493 rnPat pat `thenRn` \ (pat', fvs) ->
494 returnRn ((fieldname, pat', pun), fvs `addOneFV` fieldname)
497 %************************************************************************
499 \subsubsection{@rnIPBinds@s: in implicit parameter bindings} *
501 %************************************************************************
504 rnIPBinds [] = returnRn ([], emptyFVs)
505 rnIPBinds ((n, expr) : binds)
506 = newIPName n `thenRn` \ name ->
507 rnExpr expr `thenRn` \ (expr',fvExpr) ->
508 rnIPBinds binds `thenRn` \ (binds',fvBinds) ->
509 returnRn ((name, expr') : binds', fvExpr `plusFV` fvBinds)
513 %************************************************************************
515 \subsubsection{@Stmt@s: in @do@ expressions}
517 %************************************************************************
519 Note that although some bound vars may appear in the free var set for
520 the first qual, these will eventually be removed by the caller. For
521 example, if we have @[p | r <- s, q <- r, p <- q]@, when doing
522 @[q <- r, p <- q]@, the free var set for @q <- r@ will
523 be @{r}@, and the free var set for the entire Quals will be @{r}@. This
524 @r@ will be removed only when we finally return from examining all the
528 type RnExprTy = RdrNameHsExpr -> RnMS (RenamedHsExpr, FreeVars)
532 -> RnMS ([RenamedStmt], FreeVars)
535 = returnRn ([], emptyFVs)
537 rnStmts rn_expr (stmt:stmts)
538 = rnStmt rn_expr stmt $ \ stmt' ->
539 rnStmts rn_expr stmts `thenRn` \ (stmts', fvs) ->
540 returnRn (stmt' : stmts', fvs)
542 rnStmt :: RnExprTy -> RdrNameStmt
543 -> (RenamedStmt -> RnMS (a, FreeVars))
544 -> RnMS (a, FreeVars)
545 -- Because of mutual recursion we have to pass in rnExpr.
547 rnStmt rn_expr (BindStmt pat expr src_loc) thing_inside
548 = pushSrcLocRn src_loc $
549 rn_expr expr `thenRn` \ (expr', fv_expr) ->
550 bindLocalsFVRn doc binders $ \ new_binders ->
551 rnPat pat `thenRn` \ (pat', fv_pat) ->
552 thing_inside (BindStmt pat' expr' src_loc) `thenRn` \ (result, fvs) ->
553 returnRn (result, fv_expr `plusFV` fvs `plusFV` fv_pat)
555 binders = collectPatBinders pat
556 doc = text "a pattern in do binding"
558 rnStmt rn_expr (ExprStmt expr src_loc) thing_inside
559 = pushSrcLocRn src_loc $
560 rn_expr expr `thenRn` \ (expr', fv_expr) ->
561 thing_inside (ExprStmt expr' src_loc) `thenRn` \ (result, fvs) ->
562 returnRn (result, fv_expr `plusFV` fvs)
564 rnStmt rn_expr (GuardStmt expr src_loc) thing_inside
565 = pushSrcLocRn src_loc $
566 rn_expr expr `thenRn` \ (expr', fv_expr) ->
567 thing_inside (GuardStmt expr' src_loc) `thenRn` \ (result, fvs) ->
568 returnRn (result, fv_expr `plusFV` fvs)
570 rnStmt rn_expr (ReturnStmt expr) thing_inside
571 = rn_expr expr `thenRn` \ (expr', fv_expr) ->
572 thing_inside (ReturnStmt expr') `thenRn` \ (result, fvs) ->
573 returnRn (result, fv_expr `plusFV` fvs)
575 rnStmt rn_expr (LetStmt binds) thing_inside
576 = rnBinds binds $ \ binds' ->
577 thing_inside (LetStmt binds')
580 %************************************************************************
582 \subsubsection{Precedence Parsing}
584 %************************************************************************
586 @mkOpAppRn@ deals with operator fixities. The argument expressions
587 are assumed to be already correctly arranged. It needs the fixities
588 recorded in the OpApp nodes, because fixity info applies to the things
589 the programmer actually wrote, so you can't find it out from the Name.
591 Furthermore, the second argument is guaranteed not to be another
592 operator application. Why? Because the parser parses all
593 operator appications left-associatively, EXCEPT negation, which
594 we need to handle specially.
597 mkOpAppRn :: RenamedHsExpr -- Left operand; already rearranged
598 -> RenamedHsExpr -> Fixity -- Operator and fixity
599 -> RenamedHsExpr -- Right operand (not an OpApp, but might
601 -> RnMS RenamedHsExpr
603 ---------------------------
604 -- (e11 `op1` e12) `op2` e2
605 mkOpAppRn e1@(OpApp e11 op1 fix1 e12) op2 fix2 e2
607 = addErrRn (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenRn_`
608 returnRn (OpApp e1 op2 fix2 e2)
611 = mkOpAppRn e12 op2 fix2 e2 `thenRn` \ new_e ->
612 returnRn (OpApp e11 op1 fix1 new_e)
614 (nofix_error, associate_right) = compareFixity fix1 fix2
616 ---------------------------
617 -- (- neg_arg) `op` e2
618 mkOpAppRn e1@(NegApp neg_arg neg_op) op2 fix2 e2
620 = addErrRn (precParseErr (pp_prefix_minus,negateFixity) (ppr_op op2,fix2)) `thenRn_`
621 returnRn (OpApp e1 op2 fix2 e2)
624 = mkOpAppRn neg_arg op2 fix2 e2 `thenRn` \ new_e ->
625 returnRn (NegApp new_e neg_op)
627 (nofix_error, associate_right) = compareFixity negateFixity fix2
629 ---------------------------
631 mkOpAppRn e1 op1 fix1 e2@(NegApp neg_arg neg_op) -- NegApp can occur on the right
632 | not associate_right -- We *want* right association
633 = addErrRn (precParseErr (ppr_op op1, fix1) (pp_prefix_minus, negateFixity)) `thenRn_`
634 returnRn (OpApp e1 op1 fix1 e2)
636 (_, associate_right) = compareFixity fix1 negateFixity
638 ---------------------------
640 mkOpAppRn e1 op fix e2 -- Default case, no rearrangment
641 = ASSERT2( right_op_ok fix e2,
642 ppr e1 $$ text "---" $$ ppr op $$ text "---" $$ ppr fix $$ text "---" $$ ppr e2
644 returnRn (OpApp e1 op fix e2)
646 -- Parser left-associates everything, but
647 -- derived instances may have correctly-associated things to
648 -- in the right operarand. So we just check that the right operand is OK
649 right_op_ok fix1 (OpApp _ _ fix2 _)
650 = not error_please && associate_right
652 (error_please, associate_right) = compareFixity fix1 fix2
653 right_op_ok fix1 other
656 -- Parser initially makes negation bind more tightly than any other operator
657 mkNegAppRn neg_arg neg_op
660 getModeRn `thenRn` \ mode ->
661 ASSERT( not_op_app mode neg_arg )
663 returnRn (NegApp neg_arg neg_op)
665 not_op_app SourceMode (OpApp _ _ _ _) = False
666 not_op_app mode other = True
670 mkConOpPatRn :: RenamedPat -> Name -> Fixity -> RenamedPat
673 mkConOpPatRn p1@(ConOpPatIn p11 op1 fix1 p12)
676 = addErrRn (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenRn_`
677 returnRn (ConOpPatIn p1 op2 fix2 p2)
680 = mkConOpPatRn p12 op2 fix2 p2 `thenRn` \ new_p ->
681 returnRn (ConOpPatIn p11 op1 fix1 new_p)
684 (nofix_error, associate_right) = compareFixity fix1 fix2
686 mkConOpPatRn p1 op fix p2 -- Default case, no rearrangment
687 = ASSERT( not_op_pat p2 )
688 returnRn (ConOpPatIn p1 op fix p2)
690 not_op_pat (ConOpPatIn _ _ _ _) = False
691 not_op_pat other = True
695 checkPrecMatch :: Bool -> Name -> RenamedMatch -> RnMS ()
697 checkPrecMatch False fn match
700 checkPrecMatch True op (Match _ (p1:p2:_) _ _)
701 -- True indicates an infix lhs
702 = getModeRn `thenRn` \ mode ->
703 -- See comments with rnExpr (OpApp ...)
705 InterfaceMode -> returnRn ()
706 SourceMode -> checkPrec op p1 False `thenRn_`
709 checkPrecMatch True op _ = panic "checkPrecMatch"
711 checkPrec op (ConOpPatIn _ op1 _ _) right
712 = lookupFixityRn op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
713 lookupFixityRn op1 `thenRn` \ op1_fix@(Fixity op1_prec op1_dir) ->
715 inf_ok = op1_prec > op_prec ||
716 (op1_prec == op_prec &&
717 (op1_dir == InfixR && op_dir == InfixR && right ||
718 op1_dir == InfixL && op_dir == InfixL && not right))
720 info = (ppr_op op, op_fix)
721 info1 = (ppr_op op1, op1_fix)
722 (infol, infor) = if right then (info, info1) else (info1, info)
724 checkRn inf_ok (precParseErr infol infor)
726 checkPrec op pat right
729 -- Check precedence of (arg op) or (op arg) respectively
730 -- If arg is itself an operator application, its precedence should
731 -- be higher than that of op
732 checkSectionPrec left_or_right section op arg
734 OpApp _ op fix _ -> go_for_it (ppr_op op) fix
735 NegApp _ _ -> go_for_it pp_prefix_minus negateFixity
739 go_for_it pp_arg_op arg_fix@(Fixity arg_prec _)
740 = lookupFixityRn op_name `thenRn` \ op_fix@(Fixity op_prec _) ->
741 checkRn (op_prec < arg_prec)
742 (sectionPrecErr (ppr_op op_name, op_fix) (pp_arg_op, arg_fix) section)
749 @(compareFixity op1 op2)@ tells which way to arrange appication, or
750 whether there's an error.
753 compareFixity :: Fixity -> Fixity
754 -> (Bool, -- Error please
755 Bool) -- Associate to the right: a op1 (b op2 c)
756 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
757 = case prec1 `compare` prec2 of
760 EQ -> case (dir1, dir2) of
761 (InfixR, InfixR) -> right
762 (InfixL, InfixL) -> left
765 right = (False, True)
766 left = (False, False)
767 error_please = (True, False)
770 %************************************************************************
772 \subsubsection{Literals}
774 %************************************************************************
776 When literals occur we have to make sure
777 that the types and classes they involve
781 litFVs (HsChar c) = returnRn (unitFV charTyCon_name)
782 litFVs (HsCharPrim c) = returnRn (unitFV (getName charPrimTyCon))
783 litFVs (HsString s) = returnRn (mkFVs [listTyCon_name, charTyCon_name])
784 litFVs (HsStringPrim s) = returnRn (unitFV (getName addrPrimTyCon))
785 litFVs (HsInt i) = returnRn (unitFV (getName intTyCon))
786 litFVs (HsIntPrim i) = returnRn (unitFV (getName intPrimTyCon))
787 litFVs (HsFloatPrim f) = returnRn (unitFV (getName floatPrimTyCon))
788 litFVs (HsDoublePrim d) = returnRn (unitFV (getName doublePrimTyCon))
789 litFVs (HsLitLit l bogus_ty) = lookupOrigName ccallableClass_RDR `thenRn` \ cc ->
791 litFVs lit = pprPanic "RnExpr.litFVs" (ppr lit) -- HsInteger and HsRat only appear
792 -- in post-typechecker translations
794 rnOverLit (HsIntegral i from_integer)
795 = lookupOccRn from_integer `thenRn` \ from_integer' ->
796 (if inIntRange i then
799 lookupOrigNames [plusInteger_RDR, timesInteger_RDR]
801 returnRn (HsIntegral i from_integer', ns `addOneFV` from_integer')
803 rnOverLit (HsFractional i n)
804 = lookupOccRn n `thenRn` \ n' ->
805 lookupOrigNames [ratioDataCon_RDR, plusInteger_RDR, timesInteger_RDR] `thenRn` \ ns' ->
806 -- We have to make sure that the Ratio type is imported with
807 -- its constructor, because literals of type Ratio t are
808 -- built with that constructor.
809 -- The Rational type is needed too, but that will come in
810 -- when fractionalClass does.
811 -- The plus/times integer operations may be needed to construct the numerator
812 -- and denominator (see DsUtils.mkIntegerLit)
813 returnRn (HsFractional i n', ns' `addOneFV` n')
816 %************************************************************************
818 \subsubsection{Assertion utils}
820 %************************************************************************
823 mkAssertExpr :: RnMS (RenamedHsExpr, FreeVars)
825 lookupOrigName assertErr_RDR `thenRn` \ name ->
826 getSrcLocRn `thenRn` \ sloc ->
828 -- if we're ignoring asserts, return (\ _ e -> e)
829 -- if not, return (assertError "src-loc")
831 if opt_IgnoreAsserts then
832 getUniqRn `thenRn` \ uniq ->
834 vname = mkSysLocalName uniq SLIT("v")
835 expr = HsLam ignorePredMatch
836 loc = nameSrcLoc vname
837 ignorePredMatch = Match [] [WildPatIn, VarPatIn vname] Nothing
838 (GRHSs [GRHS [ExprStmt (HsVar vname) loc] loc]
841 returnRn (expr, unitFV name)
846 (HsLit (HsString (_PK_ (showSDoc (ppr sloc)))))
849 returnRn (expr, unitFV name)
853 %************************************************************************
855 \subsubsection{Errors}
857 %************************************************************************
860 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
861 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)
862 pp_prefix_minus = ptext SLIT("prefix `-'")
864 dupFieldErr str (dup:rest)
865 = hsep [ptext SLIT("duplicate field name"),
867 ptext SLIT("in record"), text str]
870 = hang (ptext SLIT("precedence parsing error"))
871 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
873 ptext SLIT("in the same infix expression")])
875 sectionPrecErr op arg_op section
876 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
877 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
878 nest 4 (ptext SLIT("In the section:") <+> quotes (ppr section))]
882 SLIT("accepting non-standard pattern guards (-fglasgow-exts to suppress this message)")
886 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
887 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
890 = sep [ptext SLIT("Pattern syntax in expression context:"),
894 = sep [ptext SLIT("`do' statements must end in expression:"),