2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[RnExpr]{Renaming of expressions}
6 Basically dependency analysis.
8 Handles @Match@, @GRHSsAndBinds@, @HsExpr@, and @Qualifier@ datatypes. In
9 general, all of these functions return a renamed thing, and a set of
13 #include "HsVersions.h"
16 rnMatch, rnGRHSsAndBinds, rnPat,
21 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
22 IMPORT_DELOOPER(RnLoop) -- break the RnPass/RnExpr/RnBinds loops
24 import {-# SOURCE #-} RnBinds
25 import {-# SOURCE #-} RnSource ( rnHsSigType )
33 import CmdLineOpts ( opt_GlasgowExts )
34 import BasicTypes ( Fixity(..), FixityDirection(..) )
35 import PrelInfo ( numClass_RDR, fractionalClass_RDR, eqClass_RDR, ccallableClass_RDR,
36 creturnableClass_RDR, monadZeroClass_RDR, enumClass_RDR, ordClass_RDR,
37 ratioDataCon_RDR, negate_RDR
39 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
40 floatPrimTyCon, doublePrimTyCon
42 import TyCon ( TyCon )
44 import ErrUtils ( addErrLoc, addShortErrLocLine )
47 import UniqFM ( lookupUFM{-, ufmToList ToDo:rm-} )
48 import UniqSet ( emptyUniqSet, unitUniqSet,
49 unionUniqSets, unionManyUniqSets,
52 import Util ( Ord3(..), removeDups, panic, pprPanic, assertPanic )
58 *********************************************************
62 *********************************************************
65 rnPat :: RdrNamePat -> RnMS s RenamedPat
67 rnPat WildPatIn = returnRn WildPatIn
70 = lookupBndrRn name `thenRn` \ vname ->
71 returnRn (VarPatIn vname)
74 = litOccurrence lit `thenRn_`
75 lookupImplicitOccRn eqClass_RDR `thenRn_` -- Needed to find equality on pattern
76 returnRn (LitPatIn lit)
79 = rnPat pat `thenRn` \ pat' ->
80 returnRn (LazyPatIn pat')
82 rnPat (AsPatIn name pat)
83 = rnPat pat `thenRn` \ pat' ->
84 lookupBndrRn name `thenRn` \ vname ->
85 returnRn (AsPatIn vname pat')
87 rnPat (ConPatIn con pats)
88 = lookupOccRn con `thenRn` \ con' ->
89 mapRn rnPat pats `thenRn` \ patslist ->
90 returnRn (ConPatIn con' patslist)
92 rnPat (ConOpPatIn pat1 con _ pat2)
93 = rnPat pat1 `thenRn` \ pat1' ->
94 lookupOccRn con `thenRn` \ con' ->
95 lookupFixity con `thenRn` \ fixity ->
96 rnPat pat2 `thenRn` \ pat2' ->
97 mkConOpPatRn pat1' con' fixity pat2'
99 -- Negated patters can only be literals, and they are dealt with
100 -- by negating the literal at compile time, not by using the negation
101 -- operation in Num. So we don't need to make an implicit reference
103 rnPat neg@(NegPatIn pat)
104 = checkRn (valid_neg_pat pat) (negPatErr neg)
106 rnPat pat `thenRn` \ pat' ->
107 returnRn (NegPatIn pat')
109 valid_neg_pat (LitPatIn (HsInt _)) = True
110 valid_neg_pat (LitPatIn (HsFrac _)) = True
111 valid_neg_pat _ = False
114 = rnPat pat `thenRn` \ pat' ->
115 returnRn (ParPatIn pat')
117 rnPat (NPlusKPatIn name lit)
118 = litOccurrence lit `thenRn_`
119 lookupImplicitOccRn ordClass_RDR `thenRn_`
120 lookupBndrRn name `thenRn` \ name' ->
121 returnRn (NPlusKPatIn name' lit)
123 rnPat (ListPatIn pats)
124 = addImplicitOccRn listType_name `thenRn_`
125 mapRn rnPat pats `thenRn` \ patslist ->
126 returnRn (ListPatIn patslist)
128 rnPat (TuplePatIn pats)
129 = addImplicitOccRn (tupleType_name (length pats)) `thenRn_`
130 mapRn rnPat pats `thenRn` \ patslist ->
131 returnRn (TuplePatIn patslist)
133 rnPat (RecPatIn con rpats)
134 = lookupOccRn con `thenRn` \ con' ->
135 rnRpats rpats `thenRn` \ rpats' ->
136 returnRn (RecPatIn con' rpats')
139 ************************************************************************
143 ************************************************************************
146 rnMatch, rnMatch1 :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
148 -- The only tricky bit here is that we want to do a single
149 -- bindLocalsRn for all the matches together, so that we spot
150 -- the repeated variable in
154 = bindLocalsRn "pattern" (get_binders match) $ \ new_binders ->
155 rnMatch1 match `thenRn` \ (match', fvs) ->
156 returnRn (match', fvs `minusNameSet` mkNameSet new_binders)
158 get_binders (GRHSMatch _) = []
159 get_binders (PatMatch pat match) = collectPatBinders pat ++ get_binders match
161 rnMatch1 (PatMatch pat match)
162 = rnPat pat `thenRn` \ pat' ->
163 rnMatch1 match `thenRn` \ (match', fvs) ->
164 returnRn (PatMatch pat' match', fvs)
166 rnMatch1 (GRHSMatch grhss_and_binds)
167 = rnGRHSsAndBinds grhss_and_binds `thenRn` \ (grhss_and_binds', fvs) ->
168 returnRn (GRHSMatch grhss_and_binds', fvs)
171 %************************************************************************
173 \subsubsection{Guarded right-hand sides (GRHSsAndBinds)}
175 %************************************************************************
178 rnGRHSsAndBinds :: RdrNameGRHSsAndBinds -> RnMS s (RenamedGRHSsAndBinds, FreeVars)
180 rnGRHSsAndBinds (GRHSsAndBindsIn grhss binds)
181 = rnBinds binds $ \ binds' ->
182 rnGRHSs grhss `thenRn` \ (grhss', fvGRHS) ->
183 returnRn (GRHSsAndBindsIn grhss' binds', fvGRHS)
185 rnGRHSs [] = returnRn ([], emptyNameSet)
188 = rnGRHS grhs `thenRn` \ (grhs', fvs) ->
189 rnGRHSs grhss `thenRn` \ (grhss', fvss) ->
190 returnRn (grhs' : grhss', fvs `unionNameSets` fvss)
192 rnGRHS (GRHS guard expr locn)
193 = pushSrcLocRn locn $
194 (if not (opt_GlasgowExts || is_standard_guard guard) then
195 addWarnRn (nonStdGuardErr guard)
200 (rnStmts rnExpr guard $ \ guard' ->
201 -- This nested thing deals with scope and
202 -- the free vars of the guard, and knocking off the
203 -- free vars of the rhs that are bound by the guard
205 rnExpr expr `thenRn` \ (expr', fvse) ->
206 returnRn (GRHS guard' expr' locn, fvse))
208 rnGRHS (OtherwiseGRHS expr locn)
209 = pushSrcLocRn locn $
210 rnExpr expr `thenRn` \ (expr', fvs) ->
211 returnRn (GRHS [] expr' locn, fvs)
213 -- Standard Haskell 1.4 guards are just a single boolean
214 -- expression, rather than a list of qualifiers as in the
216 is_standard_guard [GuardStmt _ _] = True
217 is_standard_guard other = False
220 %************************************************************************
222 \subsubsection{Expressions}
224 %************************************************************************
227 rnExprs :: [RdrNameHsExpr] -> RnMS s ([RenamedHsExpr], FreeVars)
229 rnExprs' ls [] `thenRn` \ (exprs, fvExprs) ->
230 returnRn (exprs, unionManyNameSets fvExprs)
232 rnExprs' [] acc = returnRn ([], acc)
233 rnExprs' (expr:exprs) acc
234 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
235 rnExprs' exprs (fvExpr:acc) `thenRn` \ (exprs', fvExprs) ->
236 returnRn (expr':exprs', fvExprs)
239 Variables. We look up the variable and return the resulting name. The
240 interesting question is what the free-variable set should be. We
241 don't want to return imported or prelude things as free vars. So we
242 look at the Name returned from the lookup, and make it part of the
243 free-var set iff if it's a LocallyDefined Name.
247 rnExpr :: RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
250 = lookupOccRn v `thenRn` \ vname ->
251 returnRn (HsVar vname, if isLocallyDefined vname
252 then unitNameSet vname
256 = litOccurrence lit `thenRn_`
257 returnRn (HsLit lit, emptyNameSet)
260 = rnMatch match `thenRn` \ (match', fvMatch) ->
261 returnRn (HsLam match', fvMatch)
263 rnExpr (HsApp fun arg)
264 = rnExpr fun `thenRn` \ (fun',fvFun) ->
265 rnExpr arg `thenRn` \ (arg',fvArg) ->
266 returnRn (HsApp fun' arg', fvFun `unionNameSets` fvArg)
268 rnExpr (OpApp e1 op@(HsVar op_name) _ e2)
269 = rnExpr e1 `thenRn` \ (e1', fv_e1) ->
270 rnExpr e2 `thenRn` \ (e2', fv_e2) ->
271 rnExpr op `thenRn` \ (op', fv_op) ->
274 -- When renaming code synthesised from "deriving" declarations
275 -- we're in Interface mode, and we should ignore fixity; assume
276 -- that the deriving code generator got the association correct
277 lookupFixity op_name `thenRn` \ fixity ->
278 getModeRn `thenRn` \ mode ->
280 SourceMode -> mkOpAppRn e1' op' fixity e2'
281 InterfaceMode _ -> returnRn (OpApp e1' op' fixity e2')
282 ) `thenRn` \ final_e ->
285 fv_e1 `unionNameSets` fv_op `unionNameSets` fv_e2)
288 = rnExpr e `thenRn` \ (e', fv_e) ->
289 lookupImplicitOccRn negate_RDR `thenRn` \ neg ->
290 mkNegAppRn e' (HsVar neg) `thenRn` \ final_e ->
291 returnRn (final_e, fv_e)
294 = rnExpr e `thenRn` \ (e', fvs_e) ->
295 returnRn (HsPar e', fvs_e)
297 rnExpr (SectionL expr op)
298 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
299 rnExpr op `thenRn` \ (op', fvs_op) ->
300 returnRn (SectionL expr' op', fvs_op `unionNameSets` fvs_expr)
302 rnExpr (SectionR op expr)
303 = rnExpr op `thenRn` \ (op', fvs_op) ->
304 rnExpr expr `thenRn` \ (expr', fvs_expr) ->
305 returnRn (SectionR op' expr', fvs_op `unionNameSets` fvs_expr)
307 rnExpr (CCall fun args may_gc is_casm fake_result_ty)
308 = lookupImplicitOccRn ccallableClass_RDR `thenRn_`
309 lookupImplicitOccRn creturnableClass_RDR `thenRn_`
310 rnExprs args `thenRn` \ (args', fvs_args) ->
311 returnRn (CCall fun args' may_gc is_casm fake_result_ty, fvs_args)
313 rnExpr (HsSCC label expr)
314 = rnExpr expr `thenRn` \ (expr', fvs_expr) ->
315 returnRn (HsSCC label expr', fvs_expr)
317 rnExpr (HsCase expr ms src_loc)
318 = pushSrcLocRn src_loc $
319 rnExpr expr `thenRn` \ (new_expr, e_fvs) ->
320 mapAndUnzipRn rnMatch ms `thenRn` \ (new_ms, ms_fvs) ->
321 returnRn (HsCase new_expr new_ms src_loc, unionManyNameSets (e_fvs : ms_fvs))
323 rnExpr (HsLet binds expr)
324 = rnBinds binds $ \ binds' ->
325 rnExpr expr `thenRn` \ (expr',fvExpr) ->
326 returnRn (HsLet binds' expr', fvExpr)
328 rnExpr (HsDo do_or_lc stmts src_loc)
329 = pushSrcLocRn src_loc $
330 lookupImplicitOccRn monadZeroClass_RDR `thenRn_` -- Forces Monad to come too
331 (rnStmts rnExpr stmts $ \ stmts' ->
332 returnRn (HsDo do_or_lc stmts' src_loc, emptyNameSet))
334 rnExpr (ExplicitList exps)
335 = addImplicitOccRn listType_name `thenRn_`
336 rnExprs exps `thenRn` \ (exps', fvs) ->
337 returnRn (ExplicitList exps', fvs)
339 rnExpr (ExplicitTuple exps)
340 = addImplicitOccRn (tupleType_name (length exps)) `thenRn_`
341 rnExprs exps `thenRn` \ (exps', fvExps) ->
342 returnRn (ExplicitTuple exps', fvExps)
344 rnExpr (RecordCon (HsVar con) rbinds)
345 = lookupOccRn con `thenRn` \ conname ->
346 rnRbinds "construction" rbinds `thenRn` \ (rbinds', fvRbinds) ->
347 returnRn (RecordCon (HsVar conname) rbinds', fvRbinds)
349 rnExpr (RecordUpd expr rbinds)
350 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
351 rnRbinds "update" rbinds `thenRn` \ (rbinds', fvRbinds) ->
352 returnRn (RecordUpd expr' rbinds', fvExpr `unionNameSets` fvRbinds)
354 rnExpr (ExprWithTySig expr pty)
355 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
356 rnHsSigType (\ sty -> text "an expression") pty `thenRn` \ pty' ->
357 returnRn (ExprWithTySig expr' pty', fvExpr)
359 rnExpr (HsIf p b1 b2 src_loc)
360 = pushSrcLocRn src_loc $
361 rnExpr p `thenRn` \ (p', fvP) ->
362 rnExpr b1 `thenRn` \ (b1', fvB1) ->
363 rnExpr b2 `thenRn` \ (b2', fvB2) ->
364 returnRn (HsIf p' b1' b2' src_loc, unionManyNameSets [fvP, fvB1, fvB2])
366 rnExpr (ArithSeqIn seq)
367 = lookupImplicitOccRn enumClass_RDR `thenRn_`
368 rn_seq seq `thenRn` \ (new_seq, fvs) ->
369 returnRn (ArithSeqIn new_seq, fvs)
372 = rnExpr expr `thenRn` \ (expr', fvExpr) ->
373 returnRn (From expr', fvExpr)
375 rn_seq (FromThen expr1 expr2)
376 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
377 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
378 returnRn (FromThen expr1' expr2', fvExpr1 `unionNameSets` fvExpr2)
380 rn_seq (FromTo expr1 expr2)
381 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
382 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
383 returnRn (FromTo expr1' expr2', fvExpr1 `unionNameSets` fvExpr2)
385 rn_seq (FromThenTo expr1 expr2 expr3)
386 = rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
387 rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
388 rnExpr expr3 `thenRn` \ (expr3', fvExpr3) ->
389 returnRn (FromThenTo expr1' expr2' expr3',
390 unionManyNameSets [fvExpr1, fvExpr2, fvExpr3])
393 %************************************************************************
395 \subsubsection{@Rbinds@s and @Rpats@s: in record expressions}
397 %************************************************************************
401 = mapRn field_dup_err dup_fields `thenRn_`
402 mapAndUnzipRn rn_rbind rbinds `thenRn` \ (rbinds', fvRbind_s) ->
403 returnRn (rbinds', unionManyNameSets fvRbind_s)
405 (_, dup_fields) = removeDups cmp [ f | (f,_,_) <- rbinds ]
407 field_dup_err dups = addErrRn (dupFieldErr str dups)
409 rn_rbind (field, expr, pun)
410 = lookupGlobalOccRn field `thenRn` \ fieldname ->
411 rnExpr expr `thenRn` \ (expr', fvExpr) ->
412 returnRn ((fieldname, expr', pun), fvExpr)
415 = mapRn field_dup_err dup_fields `thenRn_`
418 (_, dup_fields) = removeDups cmp [ f | (f,_,_) <- rpats ]
420 field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
422 rn_rpat (field, pat, pun)
423 = lookupGlobalOccRn field `thenRn` \ fieldname ->
424 rnPat pat `thenRn` \ pat' ->
425 returnRn (fieldname, pat', pun)
428 %************************************************************************
430 \subsubsection{@Stmt@s: in @do@ expressions}
432 %************************************************************************
434 Note that although some bound vars may appear in the free var set for
435 the first qual, these will eventually be removed by the caller. For
436 example, if we have @[p | r <- s, q <- r, p <- q]@, when doing
437 @[q <- r, p <- q]@, the free var set for @q <- r@ will
438 be @{r}@, and the free var set for the entire Quals will be @{r}@. This
439 @r@ will be removed only when we finally return from examining all the
443 type RnExprTy s = RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
445 rnStmts :: RnExprTy s
447 -> ([RenamedStmt] -> RnMS s (a, FreeVars))
448 -> RnMS s (a, FreeVars)
450 rnStmts rn_expr [] thing_inside
453 rnStmts rn_expr (stmt:stmts) thing_inside
454 = rnStmt rn_expr stmt $ \ stmt' ->
455 rnStmts rn_expr stmts $ \ stmts' ->
456 thing_inside (stmt' : stmts')
458 rnStmt :: RnExprTy s -> RdrNameStmt -> (RenamedStmt -> RnMS s (a, FreeVars)) -> RnMS s (a, FreeVars)
459 -- Because of mutual recursion we have to pass in rnExpr.
461 rnStmt rn_expr (BindStmt pat expr src_loc) thing_inside
462 = pushSrcLocRn src_loc $
463 rn_expr expr `thenRn` \ (expr', fv_expr) ->
464 bindLocalsRn "pattern in do binding" binders $ \ new_binders ->
465 rnPat pat `thenRn` \ pat' ->
467 thing_inside (BindStmt pat' expr' src_loc) `thenRn` \ (result, fvs) ->
468 returnRn (result, fv_expr `unionNameSets` (fvs `minusNameSet` mkNameSet new_binders))
470 binders = collectPatBinders pat
472 rnStmt rn_expr (ExprStmt expr src_loc) thing_inside
473 = pushSrcLocRn src_loc $
474 rn_expr expr `thenRn` \ (expr', fv_expr) ->
475 thing_inside (ExprStmt expr' src_loc) `thenRn` \ (result, fvs) ->
476 returnRn (result, fv_expr `unionNameSets` fvs)
478 rnStmt rn_expr (GuardStmt expr src_loc) thing_inside
479 = pushSrcLocRn src_loc $
480 rn_expr expr `thenRn` \ (expr', fv_expr) ->
481 thing_inside (GuardStmt expr' src_loc) `thenRn` \ (result, fvs) ->
482 returnRn (result, fv_expr `unionNameSets` fvs)
484 rnStmt rn_expr (ReturnStmt expr) thing_inside
485 = rn_expr expr `thenRn` \ (expr', fv_expr) ->
486 thing_inside (ReturnStmt expr') `thenRn` \ (result, fvs) ->
487 returnRn (result, fv_expr `unionNameSets` fvs)
489 rnStmt rn_expr (LetStmt binds) thing_inside
490 = rnBinds binds $ \ binds' ->
491 thing_inside (LetStmt binds')
494 %************************************************************************
496 \subsubsection{Precedence Parsing}
498 %************************************************************************
500 @mkOpAppRn@ deals with operator fixities. The argument expressions
501 are assumed to be already correctly arranged. It needs the fixities
502 recorded in the OpApp nodes, because fixity info applies to the things
503 the programmer actually wrote, so you can't find it out from the Name.
505 Furthermore, the second argument is guaranteed not to be another
506 operator application. Why? Because the parser parses all
507 operator appications left-associatively.
510 mkOpAppRn :: RenamedHsExpr -> RenamedHsExpr -> Fixity -> RenamedHsExpr
511 -> RnMS s RenamedHsExpr
513 mkOpAppRn e1@(OpApp e11 op1 fix1 e12)
516 = addErrRn (precParseErr (get op1,fix1) (get op2,fix2)) `thenRn_`
517 returnRn (OpApp e1 op2 fix2 e2)
520 = mkOpAppRn e12 op2 fix2 e2 `thenRn` \ new_e ->
521 returnRn (OpApp e11 op1 fix1 new_e)
523 (nofix_error, rearrange_me) = compareFixity fix1 fix2
525 mkOpAppRn e1@(NegApp neg_arg neg_op)
527 fix2@(Fixity prec2 dir2)
530 = addErrRn (precParseErr (get neg_op,fix_neg) (get op2,fix2)) `thenRn_`
531 returnRn (OpApp e1 op2 fix2 e2)
534 = mkOpAppRn neg_arg op2 fix2 e2 `thenRn` \ new_e ->
535 returnRn (NegApp new_e neg_op)
537 fix_neg = Fixity 6 InfixL -- Precedence of unary negate is wired in as infixl 6!
538 (nofix_error, rearrange_me) = compareFixity fix_neg fix2
540 mkOpAppRn e1 op fix e2 -- Default case, no rearrangment
541 = ASSERT( right_op_ok fix e2 )
542 returnRn (OpApp e1 op fix e2)
546 -- Parser left-associates everything, but
547 -- derived instances may have correctly-associated things to
548 -- in the right operarand. So we just check that the right operand is OK
549 right_op_ok fix1 (OpApp _ _ fix2 _)
550 = not error_please && associate_right
552 (error_please, associate_right) = compareFixity fix1 fix2
553 right_op_ok fix1 other
556 -- Parser initially makes negation bind more tightly than any other operator
557 mkNegAppRn neg_arg neg_op
560 getModeRn `thenRn` \ mode ->
561 ASSERT( not_op_app mode neg_arg )
563 returnRn (NegApp neg_arg neg_op)
565 not_op_app SourceMode (OpApp _ _ _ _) = False
566 not_op_app mode other = True
570 mkConOpPatRn :: RenamedPat -> Name -> Fixity -> RenamedPat
573 mkConOpPatRn p1@(ConOpPatIn p11 op1 fix1 p12)
576 = addErrRn (precParseErr (op1,fix1) (op2,fix2)) `thenRn_`
577 returnRn (ConOpPatIn p1 op2 fix2 p2)
580 = mkConOpPatRn p12 op2 fix2 p2 `thenRn` \ new_p ->
581 returnRn (ConOpPatIn p11 op1 fix1 new_p)
584 (nofix_error, rearrange_me) = compareFixity fix1 fix2
586 mkConOpPatRn p1@(NegPatIn neg_arg)
588 fix2@(Fixity prec2 dir2)
590 | prec2 > 6 -- Precedence of unary - is wired in as 6!
591 = addErrRn (precParseNegPatErr (op2,fix2)) `thenRn_`
592 returnRn (ConOpPatIn p1 op2 fix2 p2)
594 mkConOpPatRn p1 op fix p2 -- Default case, no rearrangment
595 = ASSERT( not_op_pat p2 )
596 returnRn (ConOpPatIn p1 op fix p2)
598 not_op_pat (ConOpPatIn _ _ _ _) = False
599 not_op_pat other = True
603 checkPrecMatch :: Bool -> RdrName -> RdrNameMatch -> RnMS s ()
605 checkPrecMatch False fn match
607 checkPrecMatch True op (PatMatch p1 (PatMatch p2 (GRHSMatch _)))
608 = checkPrec op p1 False `thenRn_`
610 checkPrecMatch True op _
611 = panic "checkPrecMatch"
613 checkPrec op (ConOpPatIn _ op1 _ _) right
614 = lookupFixity op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
615 lookupFixity op1 `thenRn` \ op1_fix@(Fixity op1_prec op1_dir) ->
617 inf_ok = op1_prec > op_prec ||
618 (op1_prec == op_prec &&
619 (op1_dir == InfixR && op_dir == InfixR && right ||
620 op1_dir == InfixL && op_dir == InfixL && not right))
623 info1 = (op1,op1_fix)
624 (infol, infor) = if right then (info, info1) else (info1, info)
626 checkRn inf_ok (precParseErr infol infor)
628 checkPrec op (NegPatIn _) right
629 = lookupFixity op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
630 checkRn (op_prec <= 6) (precParseNegPatErr (op,op_fix))
632 checkPrec op pat right
639 (compareFixity op1 op2) tells which way to arrange appication, or
640 whether there's an error.
643 compareFixity :: Fixity -> Fixity
644 -> (Bool, -- Error please
645 Bool) -- Associate to the right: a op1 (b op2 c)
646 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
647 = case prec1 `cmp` prec2 of
650 EQ_ -> case (dir1, dir2) of
651 (InfixR, InfixR) -> right
652 (InfixL, InfixL) -> left
655 right = (False, True)
656 left = (False, False)
657 error_please = (True, False)
660 %************************************************************************
662 \subsubsection{Literals}
664 %************************************************************************
666 When literals occur we have to make sure that the types and classes they involve
670 litOccurrence (HsChar _)
671 = addImplicitOccRn charType_name
673 litOccurrence (HsCharPrim _)
674 = addImplicitOccRn (getName charPrimTyCon)
676 litOccurrence (HsString _)
677 = addImplicitOccRn listType_name `thenRn_`
678 addImplicitOccRn charType_name
680 litOccurrence (HsStringPrim _)
681 = addImplicitOccRn (getName addrPrimTyCon)
683 litOccurrence (HsInt _)
684 = lookupImplicitOccRn numClass_RDR -- Int and Integer are forced in by Num
686 litOccurrence (HsFrac _)
687 = lookupImplicitOccRn fractionalClass_RDR `thenRn_`
688 lookupImplicitOccRn ratioDataCon_RDR
689 -- We have to make sure that the Ratio type is imported with
690 -- its constructor, because literals of type Ratio t are
691 -- built with that constructor.
693 litOccurrence (HsIntPrim _)
694 = addImplicitOccRn (getName intPrimTyCon)
696 litOccurrence (HsFloatPrim _)
697 = addImplicitOccRn (getName floatPrimTyCon)
699 litOccurrence (HsDoublePrim _)
700 = addImplicitOccRn (getName doublePrimTyCon)
702 litOccurrence (HsLitLit _)
703 = lookupImplicitOccRn ccallableClass_RDR
707 %************************************************************************
709 \subsubsection{Errors}
711 %************************************************************************
714 dupFieldErr str (dup:rest) sty
715 = hcat [ptext SLIT("duplicate field name `"),
717 ptext SLIT("' in record "), text str]
720 = sep [ptext SLIT("prefix `-' not applied to literal in pattern"), ppr sty pat]
722 precParseNegPatErr op sty
723 = hang (ptext SLIT("precedence parsing error"))
724 4 (hcat [ptext SLIT("prefix `-' has lower precedence than "),
726 ptext SLIT(" in pattern")])
728 precParseErr op1 op2 sty
729 = hang (ptext SLIT("precedence parsing error"))
730 4 (hcat [ptext SLIT("cannot mix "), pp_op sty op1, ptext SLIT(" and "), pp_op sty op2,
731 ptext SLIT(" in the same infix expression")])
733 nonStdGuardErr guard sty
734 = hang (ptext SLIT("accepting non-standard pattern guards (-fglasgow-exts to suppress this message)"))
737 pp_op sty (op, fix) = hcat [ppr sty op, space, parens (ppr sty fix)]