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,
18 #include "HsVersions.h"
20 import {-# SOURCE #-} RnBinds ( rnBinds )
21 import {-# SOURCE #-} RnSource ( rnHsSigType, rnHsType )
28 import CmdLineOpts ( opt_GlasgowExts )
29 import BasicTypes ( Fixity(..), FixityDirection(..) )
30 import PrelInfo ( numClass_RDR, fractionalClass_RDR, eqClass_RDR,
31 ccallableClass_RDR, creturnableClass_RDR,
32 monadClass_RDR, enumClass_RDR, ordClass_RDR,
33 ratioDataCon_RDR, negate_RDR, assertErr_RDR,
36 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
37 floatPrimTyCon, doublePrimTyCon
39 import Name ( nameUnique, isLocallyDefined, NamedThing(..) )
41 import UniqFM ( isNullUFM )
42 import FiniteMap ( elemFM )
43 import UniqSet ( emptyUniqSet, UniqSet )
44 import Unique ( assertIdKey )
45 import Util ( removeDups )
46 import ListSetOps ( unionLists )
47 import Maybes ( maybeToBool )
52 *********************************************************
56 *********************************************************
59 rnPat :: RdrNamePat -> RnMS s (RenamedPat, FreeVars)
61 rnPat WildPatIn = returnRn (WildPatIn, emptyFVs)
64 = lookupBndrRn name `thenRn` \ vname ->
65 returnRn (VarPatIn vname, emptyFVs)
67 rnPat (SigPatIn pat ty)
69 = rnPat pat `thenRn` \ (pat', fvs1) ->
70 rnHsType doc ty `thenRn` \ (ty', fvs2) ->
71 returnRn (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
74 = addErrRn (patSigErr ty) `thenRn_`
77 doc = text "a pattern type-signature"
80 = litOccurrence lit `thenRn_`
81 lookupImplicitOccRn eqClass_RDR `thenRn_` -- Needed to find equality on pattern
82 returnRn (LitPatIn lit, emptyFVs)
85 = rnPat pat `thenRn` \ (pat', fvs) ->
86 returnRn (LazyPatIn pat', fvs)
88 rnPat (AsPatIn name pat)
89 = rnPat pat `thenRn` \ (pat', fvs) ->
90 lookupBndrRn name `thenRn` \ vname ->
91 returnRn (AsPatIn vname pat', fvs)
93 rnPat (ConPatIn con pats)
94 = lookupOccRn con `thenRn` \ con' ->
95 mapAndUnzipRn rnPat pats `thenRn` \ (patslist, fvs_s) ->
96 returnRn (ConPatIn con' patslist, plusFVs fvs_s `addOneFV` con')
98 rnPat (ConOpPatIn pat1 con _ pat2)
99 = rnPat pat1 `thenRn` \ (pat1', fvs1) ->
100 lookupOccRn con `thenRn` \ con' ->
101 lookupFixity con' `thenRn` \ fixity ->
102 rnPat pat2 `thenRn` \ (pat2', fvs2) ->
103 mkConOpPatRn pat1' con' fixity pat2' `thenRn` \ pat' ->
104 returnRn (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
106 -- Negated patters can only be literals, and they are dealt with
107 -- by negating the literal at compile time, not by using the negation
108 -- operation in Num. So we don't need to make an implicit reference
110 rnPat neg@(NegPatIn pat)
111 = checkRn (valid_neg_pat pat) (negPatErr neg)
113 rnPat pat `thenRn` \ (pat', fvs) ->
114 returnRn (NegPatIn pat', fvs)
116 valid_neg_pat (LitPatIn (HsInt _)) = True
117 valid_neg_pat (LitPatIn (HsFrac _)) = True
118 valid_neg_pat _ = False
121 = rnPat pat `thenRn` \ (pat', fvs) ->
122 returnRn (ParPatIn pat', fvs)
124 rnPat (NPlusKPatIn name lit)
125 = litOccurrence lit `thenRn_`
126 lookupImplicitOccRn ordClass_RDR `thenRn_`
127 lookupBndrRn name `thenRn` \ name' ->
128 returnRn (NPlusKPatIn name' lit, emptyFVs)
130 rnPat (ListPatIn pats)
131 = addImplicitOccRn listTyCon_name `thenRn_`
132 mapAndUnzipRn rnPat pats `thenRn` \ (patslist, fvs_s) ->
133 returnRn (ListPatIn patslist, plusFVs fvs_s)
135 rnPat (TuplePatIn pats boxed)
136 = addImplicitOccRn (tupleTyCon_name boxed (length pats)) `thenRn_`
137 mapAndUnzipRn rnPat pats `thenRn` \ (patslist, fvs_s) ->
138 returnRn (TuplePatIn patslist boxed, plusFVs fvs_s)
140 rnPat (RecPatIn con rpats)
141 = lookupOccRn con `thenRn` \ con' ->
142 rnRpats rpats `thenRn` \ (rpats', fvs) ->
143 returnRn (RecPatIn con' rpats', fvs `addOneFV` con')
146 ************************************************************************
150 ************************************************************************
153 rnMatch :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
155 rnMatch match@(Match _ pats maybe_rhs_sig grhss)
156 = pushSrcLocRn (getMatchLoc match) $
158 -- Find the universally quantified type variables
159 -- in the pattern type signatures
160 getLocalNameEnv `thenRn` \ name_env ->
162 tyvars_in_sigs = rhs_sig_tyvars `unionLists` tyvars_in_pats
163 rhs_sig_tyvars = case maybe_rhs_sig of
165 Just ty -> extractHsTyVars ty
166 tyvars_in_pats = extractPatsTyVars pats
167 forall_tyvars = filter (not . (`elemFM` name_env)) tyvars_in_sigs
168 doc = text "a pattern type-signature"
170 bindTyVarsFVRn doc (map UserTyVar forall_tyvars) $ \ sig_tyvars ->
172 -- Note that we do a single bindLocalsRn for all the
173 -- matches together, so that we spot the repeated variable in
175 bindLocalsFVRn "a pattern" (collectPatsBinders pats) $ \ new_binders ->
177 mapAndUnzipRn rnPat pats `thenRn` \ (pats', pat_fvs_s) ->
178 rnGRHSs grhss `thenRn` \ (grhss', grhss_fvs) ->
179 (case maybe_rhs_sig of
180 Nothing -> returnRn (Nothing, emptyFVs)
181 Just ty | opt_GlasgowExts -> rnHsType doc ty `thenRn` \ (ty', ty_fvs) ->
182 returnRn (Just ty', ty_fvs)
183 | otherwise -> addErrRn (patSigErr ty) `thenRn_`
184 returnRn (Nothing, emptyFVs)
185 ) `thenRn` \ (maybe_rhs_sig', ty_fvs) ->
188 binder_set = mkNameSet new_binders
189 unused_binders = nameSetToList (binder_set `minusNameSet` grhss_fvs)
190 all_fvs = grhss_fvs `plusFV` plusFVs pat_fvs_s `plusFV` ty_fvs
192 warnUnusedMatches unused_binders `thenRn_`
194 returnRn (Match sig_tyvars pats' maybe_rhs_sig' grhss', all_fvs)
195 -- The bindLocals and bindTyVars will remove the bound FVs
198 %************************************************************************
200 \subsubsection{Guarded right-hand sides (GRHSs)}
202 %************************************************************************
205 rnGRHSs :: RdrNameGRHSs -> RnMS s (RenamedGRHSs, FreeVars)
207 rnGRHSs (GRHSs grhss binds maybe_ty)
208 = ASSERT( not (maybeToBool maybe_ty) )
209 rnBinds binds $ \ binds' ->
210 mapAndUnzipRn rnGRHS grhss `thenRn` \ (grhss', fvGRHSs) ->
211 returnRn (GRHSs grhss' binds' Nothing, plusFVs fvGRHSs)
213 rnGRHS (GRHS guarded locn)
214 = pushSrcLocRn locn $
215 (if not (opt_GlasgowExts || is_standard_guard guarded) then
216 addWarnRn (nonStdGuardErr guarded)
221 rnStmts rnExpr guarded `thenRn` \ (guarded', fvs) ->
222 returnRn (GRHS guarded' locn, fvs)
224 -- Standard Haskell 1.4 guards are just a single boolean
225 -- expression, rather than a list of qualifiers as in the
227 is_standard_guard [ExprStmt _ _] = True
228 is_standard_guard [GuardStmt _ _, ExprStmt _ _] = True
229 is_standard_guard other = False
232 %************************************************************************
234 \subsubsection{Expressions}
236 %************************************************************************
239 rnExprs :: [RdrNameHsExpr] -> RnMS s ([RenamedHsExpr], FreeVars)
240 rnExprs ls = rnExprs' ls emptyUniqSet
242 rnExprs' [] acc = returnRn ([], acc)
243 rnExprs' (expr:exprs) acc
244 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
246 -- Now we do a "seq" on the free vars because typically it's small
247 -- or empty, especially in very long lists of constants
249 acc' = acc `plusFV` fvExpr
251 (grubby_seqNameSet acc' rnExprs') exprs acc' `thenRn` \ (exprs', fvExprs) ->
252 returnRn (expr':exprs', fvExprs)
254 -- Grubby little function to do "seq" on namesets; replace by proper seq when GHC can do seq
255 grubby_seqNameSet ns result | isNullUFM ns = result
259 Variables. We look up the variable and return the resulting name.
262 rnExpr :: RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
265 = lookupOccRn v `thenRn` \ name ->
266 if nameUnique name == assertIdKey then
267 -- We expand it to (GHCerr.assert__ location)
268 mkAssertExpr `thenRn` \ expr ->
269 returnRn (expr, emptyUniqSet)
272 returnRn (HsVar name, unitFV name)
275 = litOccurrence lit `thenRn_`
276 returnRn (HsLit lit, emptyFVs)
279 = rnMatch match `thenRn` \ (match', fvMatch) ->
280 returnRn (HsLam match', fvMatch)
282 rnExpr (HsApp fun arg)
283 = rnExpr fun `thenRn` \ (fun',fvFun) ->
284 rnExpr arg `thenRn` \ (arg',fvArg) ->
285 returnRn (HsApp fun' arg', fvFun `plusFV` fvArg)
287 rnExpr (OpApp e1 op _ e2)
288 = rnExpr e1 `thenRn` \ (e1', fv_e1) ->
289 rnExpr e2 `thenRn` \ (e2', fv_e2) ->
290 rnExpr op `thenRn` \ (op'@(HsVar op_name), fv_op) ->
293 -- When renaming code synthesised from "deriving" declarations
294 -- we're in Interface mode, and we should ignore fixity; assume
295 -- that the deriving code generator got the association correct
296 lookupFixity op_name `thenRn` \ fixity ->
297 getModeRn `thenRn` \ mode ->
299 SourceMode -> mkOpAppRn e1' op' fixity e2'
300 InterfaceMode _ -> returnRn (OpApp e1' op' fixity e2')
301 ) `thenRn` \ final_e ->
304 fv_e1 `plusFV` fv_op `plusFV` fv_e2)
307 = rnExpr e `thenRn` \ (e', fv_e) ->
308 lookupImplicitOccRn negate_RDR `thenRn` \ neg ->
309 mkNegAppRn e' (HsVar neg) `thenRn` \ final_e ->
310 returnRn (final_e, fv_e)
313 = rnExpr e `thenRn` \ (e', fvs_e) ->
314 returnRn (HsPar e', fvs_e)
316 rnExpr (SectionL expr op)
317 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
318 rnExpr op `thenRn` \ (op', fvs_op) ->
319 returnRn (SectionL expr' op', fvs_op `plusFV` fvs_expr)
321 rnExpr (SectionR op expr)
322 = rnExpr op `thenRn` \ (op', fvs_op) ->
323 rnExpr expr `thenRn` \ (expr', fvs_expr) ->
324 returnRn (SectionR op' expr', fvs_op `plusFV` fvs_expr)
326 rnExpr (CCall fun args may_gc is_casm fake_result_ty)
327 -- Check out the comment on RnIfaces.getNonWiredDataDecl about ccalls
328 = lookupImplicitOccRn ccallableClass_RDR `thenRn_`
329 lookupImplicitOccRn creturnableClass_RDR `thenRn_`
330 lookupImplicitOccRn ioDataCon_RDR `thenRn_`
331 rnExprs args `thenRn` \ (args', fvs_args) ->
332 returnRn (CCall fun args' may_gc is_casm fake_result_ty, fvs_args)
334 rnExpr (HsSCC label expr)
335 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
336 returnRn (HsSCC label expr', fvs_expr)
338 rnExpr (HsCase expr ms src_loc)
339 = pushSrcLocRn src_loc $
340 rnExpr expr `thenRn` \ (new_expr, e_fvs) ->
341 mapAndUnzipRn rnMatch ms `thenRn` \ (new_ms, ms_fvs) ->
342 returnRn (HsCase new_expr new_ms src_loc, plusFVs (e_fvs : ms_fvs))
344 rnExpr (HsLet binds expr)
345 = rnBinds binds $ \ binds' ->
346 rnExpr expr `thenRn` \ (expr',fvExpr) ->
347 returnRn (HsLet binds' expr', fvExpr)
349 rnExpr (HsDo do_or_lc stmts src_loc)
350 = pushSrcLocRn src_loc $
351 lookupImplicitOccRn monadClass_RDR `thenRn_`
352 rnStmts rnExpr stmts `thenRn` \ (stmts', fvs) ->
353 returnRn (HsDo do_or_lc stmts' src_loc, fvs)
355 rnExpr (ExplicitList exps)
356 = addImplicitOccRn listTyCon_name `thenRn_`
357 rnExprs exps `thenRn` \ (exps', fvs) ->
358 returnRn (ExplicitList exps', fvs)
360 rnExpr (ExplicitTuple exps boxed)
361 = addImplicitOccRn (tupleTyCon_name boxed (length exps)) `thenRn_`
362 rnExprs exps `thenRn` \ (exps', fvExps) ->
363 returnRn (ExplicitTuple exps' boxed, fvExps)
365 rnExpr (RecordCon con_id rbinds)
366 = lookupOccRn con_id `thenRn` \ conname ->
367 rnRbinds "construction" rbinds `thenRn` \ (rbinds', fvRbinds) ->
368 returnRn (RecordCon conname rbinds', fvRbinds)
370 rnExpr (RecordUpd expr rbinds)
371 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
372 rnRbinds "update" rbinds `thenRn` \ (rbinds', fvRbinds) ->
373 returnRn (RecordUpd expr' rbinds', fvExpr `plusFV` fvRbinds)
375 rnExpr (ExprWithTySig expr pty)
376 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
377 rnHsSigType (text "an expression") pty `thenRn` \ (pty', fvTy) ->
378 returnRn (ExprWithTySig expr' pty', fvExpr `plusFV` fvTy)
380 rnExpr (HsIf p b1 b2 src_loc)
381 = pushSrcLocRn src_loc $
382 rnExpr p `thenRn` \ (p', fvP) ->
383 rnExpr b1 `thenRn` \ (b1', fvB1) ->
384 rnExpr b2 `thenRn` \ (b2', fvB2) ->
385 returnRn (HsIf p' b1' b2' src_loc, plusFVs [fvP, fvB1, fvB2])
387 rnExpr (ArithSeqIn seq)
388 = lookupImplicitOccRn enumClass_RDR `thenRn_`
389 rn_seq seq `thenRn` \ (new_seq, fvs) ->
390 returnRn (ArithSeqIn new_seq, fvs)
393 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
394 returnRn (From expr', fvExpr)
396 rn_seq (FromThen expr1 expr2)
397 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
398 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
399 returnRn (FromThen expr1' expr2', fvExpr1 `plusFV` fvExpr2)
401 rn_seq (FromTo expr1 expr2)
402 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
403 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
404 returnRn (FromTo expr1' expr2', fvExpr1 `plusFV` fvExpr2)
406 rn_seq (FromThenTo expr1 expr2 expr3)
407 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
408 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
409 rnExpr expr3 `thenRn` \ (expr3', fvExpr3) ->
410 returnRn (FromThenTo expr1' expr2' expr3',
411 plusFVs [fvExpr1, fvExpr2, fvExpr3])
414 %************************************************************************
416 \subsubsection{@Rbinds@s and @Rpats@s: in record expressions}
418 %************************************************************************
422 = mapRn field_dup_err dup_fields `thenRn_`
423 mapAndUnzipRn rn_rbind rbinds `thenRn` \ (rbinds', fvRbind_s) ->
424 returnRn (rbinds', plusFVs fvRbind_s)
426 (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rbinds ]
428 field_dup_err dups = addErrRn (dupFieldErr str dups)
430 rn_rbind (field, expr, pun)
431 = lookupGlobalOccRn field `thenRn` \ fieldname ->
432 rnExpr expr `thenRn` \ (expr', fvExpr) ->
433 returnRn ((fieldname, expr', pun), fvExpr `addOneFV` fieldname)
436 = mapRn field_dup_err dup_fields `thenRn_`
437 mapAndUnzipRn rn_rpat rpats `thenRn` \ (rpats', fvs_s) ->
438 returnRn (rpats', plusFVs fvs_s)
440 (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rpats ]
442 field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
444 rn_rpat (field, pat, pun)
445 = lookupGlobalOccRn field `thenRn` \ fieldname ->
446 rnPat pat `thenRn` \ (pat', fvs) ->
447 returnRn ((fieldname, pat', pun), fvs `addOneFV` fieldname)
450 %************************************************************************
452 \subsubsection{@Stmt@s: in @do@ expressions}
454 %************************************************************************
456 Note that although some bound vars may appear in the free var set for
457 the first qual, these will eventually be removed by the caller. For
458 example, if we have @[p | r <- s, q <- r, p <- q]@, when doing
459 @[q <- r, p <- q]@, the free var set for @q <- r@ will
460 be @{r}@, and the free var set for the entire Quals will be @{r}@. This
461 @r@ will be removed only when we finally return from examining all the
465 type RnExprTy s = RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
467 rnStmts :: RnExprTy s
469 -> RnMS s ([RenamedStmt], FreeVars)
472 = returnRn ([], emptyFVs)
474 rnStmts rn_expr (stmt:stmts)
475 = rnStmt rn_expr stmt $ \ stmt' ->
476 rnStmts rn_expr stmts `thenRn` \ (stmts', fvs) ->
477 returnRn (stmt' : stmts', fvs)
479 rnStmt :: RnExprTy s -> RdrNameStmt
480 -> (RenamedStmt -> RnMS s (a, FreeVars))
481 -> RnMS s (a, FreeVars)
482 -- Because of mutual recursion we have to pass in rnExpr.
484 rnStmt rn_expr (BindStmt pat expr src_loc) thing_inside
485 = pushSrcLocRn src_loc $
486 rn_expr expr `thenRn` \ (expr', fv_expr) ->
487 bindLocalsFVRn "a pattern in do binding" binders $ \ new_binders ->
488 rnPat pat `thenRn` \ (pat', fv_pat) ->
489 thing_inside (BindStmt pat' expr' src_loc) `thenRn` \ (result, fvs) ->
490 returnRn (result, fv_expr `plusFV` fvs `plusFV` fv_pat)
492 binders = collectPatBinders pat
494 rnStmt rn_expr (ExprStmt expr src_loc) thing_inside
495 = pushSrcLocRn src_loc $
496 rn_expr expr `thenRn` \ (expr', fv_expr) ->
497 thing_inside (ExprStmt expr' src_loc) `thenRn` \ (result, fvs) ->
498 returnRn (result, fv_expr `plusFV` fvs)
500 rnStmt rn_expr (GuardStmt expr src_loc) thing_inside
501 = pushSrcLocRn src_loc $
502 rn_expr expr `thenRn` \ (expr', fv_expr) ->
503 thing_inside (GuardStmt expr' src_loc) `thenRn` \ (result, fvs) ->
504 returnRn (result, fv_expr `plusFV` fvs)
506 rnStmt rn_expr (ReturnStmt expr) thing_inside
507 = rn_expr expr `thenRn` \ (expr', fv_expr) ->
508 thing_inside (ReturnStmt expr') `thenRn` \ (result, fvs) ->
509 returnRn (result, fv_expr `plusFV` fvs)
511 rnStmt rn_expr (LetStmt binds) thing_inside
512 = rnBinds binds $ \ binds' ->
513 thing_inside (LetStmt binds')
516 %************************************************************************
518 \subsubsection{Precedence Parsing}
520 %************************************************************************
522 @mkOpAppRn@ deals with operator fixities. The argument expressions
523 are assumed to be already correctly arranged. It needs the fixities
524 recorded in the OpApp nodes, because fixity info applies to the things
525 the programmer actually wrote, so you can't find it out from the Name.
527 Furthermore, the second argument is guaranteed not to be another
528 operator application. Why? Because the parser parses all
529 operator appications left-associatively.
532 mkOpAppRn :: RenamedHsExpr -> RenamedHsExpr -> Fixity -> RenamedHsExpr
533 -> RnMS s RenamedHsExpr
535 mkOpAppRn e1@(OpApp e11 op1 fix1 e12)
538 = addErrRn (precParseErr (get op1,fix1) (get op2,fix2)) `thenRn_`
539 returnRn (OpApp e1 op2 fix2 e2)
542 = mkOpAppRn e12 op2 fix2 e2 `thenRn` \ new_e ->
543 returnRn (OpApp e11 op1 fix1 new_e)
545 (nofix_error, rearrange_me) = compareFixity fix1 fix2
547 mkOpAppRn e1@(NegApp neg_arg neg_op)
549 fix2@(Fixity prec2 dir2)
552 = addErrRn (precParseErr (get neg_op,fix_neg) (get op2,fix2)) `thenRn_`
553 returnRn (OpApp e1 op2 fix2 e2)
556 = mkOpAppRn neg_arg op2 fix2 e2 `thenRn` \ new_e ->
557 returnRn (NegApp new_e neg_op)
559 fix_neg = Fixity 6 InfixL -- Precedence of unary negate is wired in as infixl 6!
560 (nofix_error, rearrange_me) = compareFixity fix_neg fix2
562 mkOpAppRn e1 op fix e2 -- Default case, no rearrangment
563 = ASSERT( if right_op_ok fix e2 then True
564 else pprPanic "mkOpAppRn" (vcat [ppr e1, text "---", ppr op,
565 text "---", ppr fix, text "---", ppr e2])
567 returnRn (OpApp e1 op fix e2)
571 -- Parser left-associates everything, but
572 -- derived instances may have correctly-associated things to
573 -- in the right operarand. So we just check that the right operand is OK
574 right_op_ok fix1 (OpApp _ _ fix2 _)
575 = not error_please && associate_right
577 (error_please, associate_right) = compareFixity fix1 fix2
578 right_op_ok fix1 other
581 -- Parser initially makes negation bind more tightly than any other operator
582 mkNegAppRn neg_arg neg_op
585 getModeRn `thenRn` \ mode ->
586 ASSERT( not_op_app mode neg_arg )
588 returnRn (NegApp neg_arg neg_op)
590 not_op_app SourceMode (OpApp _ _ _ _) = False
591 not_op_app mode other = True
595 mkConOpPatRn :: RenamedPat -> Name -> Fixity -> RenamedPat
598 mkConOpPatRn p1@(ConOpPatIn p11 op1 fix1 p12)
601 = addErrRn (precParseErr (op1,fix1) (op2,fix2)) `thenRn_`
602 returnRn (ConOpPatIn p1 op2 fix2 p2)
605 = mkConOpPatRn p12 op2 fix2 p2 `thenRn` \ new_p ->
606 returnRn (ConOpPatIn p11 op1 fix1 new_p)
609 (nofix_error, rearrange_me) = compareFixity fix1 fix2
611 mkConOpPatRn p1@(NegPatIn neg_arg)
613 fix2@(Fixity prec2 dir2)
615 | prec2 > 6 -- Precedence of unary - is wired in as 6!
616 = addErrRn (precParseNegPatErr (op2,fix2)) `thenRn_`
617 returnRn (ConOpPatIn p1 op2 fix2 p2)
619 mkConOpPatRn p1 op fix p2 -- Default case, no rearrangment
620 = ASSERT( not_op_pat p2 )
621 returnRn (ConOpPatIn p1 op fix p2)
623 not_op_pat (ConOpPatIn _ _ _ _) = False
624 not_op_pat other = True
628 checkPrecMatch :: Bool -> Name -> RenamedMatch -> RnMS s ()
630 checkPrecMatch False fn match
632 checkPrecMatch True op (Match _ [p1,p2] _ _)
633 = checkPrec op p1 False `thenRn_`
635 checkPrecMatch True op _ = panic "checkPrecMatch"
637 checkPrec op (ConOpPatIn _ op1 _ _) right
638 = lookupFixity op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
639 lookupFixity op1 `thenRn` \ op1_fix@(Fixity op1_prec op1_dir) ->
641 inf_ok = op1_prec > op_prec ||
642 (op1_prec == op_prec &&
643 (op1_dir == InfixR && op_dir == InfixR && right ||
644 op1_dir == InfixL && op_dir == InfixL && not right))
647 info1 = (op1,op1_fix)
648 (infol, infor) = if right then (info, info1) else (info1, info)
650 checkRn inf_ok (precParseErr infol infor)
652 checkPrec op (NegPatIn _) right
653 = lookupFixity op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
654 checkRn (op_prec <= 6) (precParseNegPatErr (op,op_fix))
656 checkPrec op pat right
663 (compareFixity op1 op2) tells which way to arrange appication, or
664 whether there's an error.
667 compareFixity :: Fixity -> Fixity
668 -> (Bool, -- Error please
669 Bool) -- Associate to the right: a op1 (b op2 c)
670 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
671 = case prec1 `compare` prec2 of
674 EQ -> case (dir1, dir2) of
675 (InfixR, InfixR) -> right
676 (InfixL, InfixL) -> left
679 right = (False, True)
680 left = (False, False)
681 error_please = (True, False)
684 %************************************************************************
686 \subsubsection{Literals}
688 %************************************************************************
690 When literals occur we have to make sure that the types and classes they involve
694 litOccurrence (HsChar _)
695 = addImplicitOccRn charTyCon_name
697 litOccurrence (HsCharPrim _)
698 = addImplicitOccRn (getName charPrimTyCon)
700 litOccurrence (HsString _)
701 = addImplicitOccRn listTyCon_name `thenRn_`
702 addImplicitOccRn charTyCon_name
704 litOccurrence (HsStringPrim _)
705 = addImplicitOccRn (getName addrPrimTyCon)
707 litOccurrence (HsInt _)
708 = lookupImplicitOccRn numClass_RDR -- Int and Integer are forced in by Num
710 litOccurrence (HsFrac _)
711 = lookupImplicitOccRn fractionalClass_RDR `thenRn_`
712 lookupImplicitOccRn ratioDataCon_RDR
713 -- We have to make sure that the Ratio type is imported with
714 -- its constructor, because literals of type Ratio t are
715 -- built with that constructor.
716 -- The Rational type is needed too, but that will come in
717 -- when fractionalClass does.
719 litOccurrence (HsIntPrim _)
720 = addImplicitOccRn (getName intPrimTyCon)
722 litOccurrence (HsFloatPrim _)
723 = addImplicitOccRn (getName floatPrimTyCon)
725 litOccurrence (HsDoublePrim _)
726 = addImplicitOccRn (getName doublePrimTyCon)
728 litOccurrence (HsLitLit _)
729 = lookupImplicitOccRn ccallableClass_RDR
732 %************************************************************************
734 \subsubsection{Assertion utils}
736 %************************************************************************
739 mkAssertExpr :: RnMS s RenamedHsExpr
741 newImportedGlobalFromRdrName assertErr_RDR `thenRn` \ name ->
742 addOccurrenceName name `thenRn_`
743 getSrcLocRn `thenRn` \ sloc ->
745 expr = HsApp (HsVar name)
746 (HsLit (HsString (_PK_ (showSDoc (ppr sloc)))))
751 %************************************************************************
753 \subsubsection{Errors}
755 %************************************************************************
758 dupFieldErr str (dup:rest)
759 = hsep [ptext SLIT("duplicate field name"),
761 ptext SLIT("in record"), text str]
764 = sep [ptext SLIT("prefix `-' not applied to literal in pattern"), quotes (ppr pat)]
766 precParseNegPatErr op
767 = hang (ptext SLIT("precedence parsing error"))
768 4 (hsep [ptext SLIT("prefix `-' has lower precedence than"),
770 ptext SLIT("in pattern")])
773 = hang (ptext SLIT("precedence parsing error"))
774 4 (hsep [ptext SLIT("cannot mix"), quotes (pp_op op1), ptext SLIT("and"),
776 ptext SLIT("in the same infix expression")])
779 = hang (ptext SLIT("accepting non-standard pattern guards (-fglasgow-exts to suppress this message)"))
783 = hang (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
784 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
786 pp_op (op, fix) = hcat [ppr op, space, parens (ppr fix)]