free variables.
\begin{code}
-#include "HsVersions.h"
-
module RnExpr (
rnMatch, rnGRHSsAndBinds, rnPat,
checkPrecMatch
) where
-IMP_Ubiq()
-IMPORT_DELOOPER(RnLoop) -- break the RnPass/RnExpr/RnBinds loops
+#include "HsVersions.h"
+
+import {-# SOURCE #-} RnBinds
+import {-# SOURCE #-} RnSource ( rnHsSigType )
import HsSyn
import RdrHsSyn
import RnHsSyn
import RnMonad
import RnEnv
-import PrelInfo ( numClass_RDR, fractionalClass_RDR, eqClass_RDR, ccallableClass_RDR,
- creturnableClass_RDR, monadZeroClass_RDR, enumClass_RDR, ordClass_RDR,
- negate_RDR
+import CmdLineOpts ( opt_GlasgowExts )
+import BasicTypes ( Fixity(..), FixityDirection(..), IfaceFlavour(..) )
+import PrelInfo ( numClass_RDR, fractionalClass_RDR, eqClass_RDR,
+ ccallableClass_RDR, creturnableClass_RDR,
+ monadZeroClass_RDR, enumClass_RDR, ordClass_RDR,
+ ratioDataCon_RDR, negate_RDR, assertErr_RDR,
+ ioDataCon_RDR, ioOkDataCon_RDR
)
import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
floatPrimTyCon, doublePrimTyCon
)
-import TyCon ( TyCon )
-import Id ( GenId )
-import ErrUtils ( addErrLoc, addShortErrLocLine )
import Name
-import Pretty
-import Unique ( Unique, otherwiseIdKey )
-import UniqFM ( lookupUFM{-, ufmToList ToDo:rm-} )
-import UniqSet ( emptyUniqSet, unitUniqSet,
- unionUniqSets, unionManyUniqSets,
- SYN_IE(UniqSet)
- )
-import PprStyle ( PprStyle(..) )
-import Util ( Ord3(..), removeDups, panic, pprPanic, assertPanic )
+import UniqFM ( isNullUFM )
+import UniqSet ( emptyUniqSet, unionManyUniqSets, UniqSet )
+import Unique ( assertIdKey )
+import Util ( removeDups )
+import Outputable
\end{code}
************************************************************************
\begin{code}
-rnMatch :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
+rnMatch, rnMatch1 :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
-rnMatch (PatMatch pat match)
- = bindLocalsRn "pattern" binders $ \ new_binders ->
- rnPat pat `thenRn` \ pat' ->
- rnMatch match `thenRn` \ (match', fvMatch) ->
- returnRn (PatMatch pat' match', fvMatch `minusNameSet` mkNameSet new_binders)
+-- The only tricky bit here is that we want to do a single
+-- bindLocalsRn for all the matches together, so that we spot
+-- the repeated variable in
+-- f x x = 1
+
+rnMatch match
+ = pushSrcLocRn (getMatchLoc match) $
+ bindLocalsRn "pattern" (get_binders match) $ \ new_binders ->
+ rnMatch1 match `thenRn` \ (match', fvs) ->
+ let
+ binder_set = mkNameSet new_binders
+ unused_binders = binder_set `minusNameSet` fvs
+ net_fvs = fvs `minusNameSet` binder_set
+ in
+ warnUnusedMatches unused_binders `thenRn_`
+
+ returnRn (match', net_fvs)
where
- binders = collectPatBinders pat
+ get_binders (GRHSMatch _) = []
+ get_binders (PatMatch pat match) = collectPatBinders pat ++ get_binders match
-rnMatch (GRHSMatch grhss_and_binds)
+rnMatch1 (PatMatch pat match)
+ = rnPat pat `thenRn` \ pat' ->
+ rnMatch1 match `thenRn` \ (match', fvs) ->
+ returnRn (PatMatch pat' match', fvs)
+
+rnMatch1 (GRHSMatch grhss_and_binds)
= rnGRHSsAndBinds grhss_and_binds `thenRn` \ (grhss_and_binds', fvs) ->
returnRn (GRHSMatch grhss_and_binds', fvs)
\end{code}
rnGRHS (GRHS guard expr locn)
= pushSrcLocRn locn $
- rnExpr guard `thenRn` \ (guard', fvsg) ->
- rnExpr expr `thenRn` \ (expr', fvse) ->
+ (if not (opt_GlasgowExts || is_standard_guard guard) then
+ addWarnRn (nonStdGuardErr guard)
+ else
+ returnRn ()
+ ) `thenRn_`
+
+ (rnStmts rnExpr guard $ \ guard' ->
+ -- This nested thing deals with scope and
+ -- the free vars of the guard, and knocking off the
+ -- free vars of the rhs that are bound by the guard
- -- Turn an "otherwise" guard into an OtherwiseGRHS.
- -- This is the first moment that we can be sure we havn't got a shadowed binding
- -- of "otherwise".
- let grhs' = case guard' of
- HsVar v | uniqueOf v == otherwiseIdKey -> OtherwiseGRHS expr' locn
- other -> GRHS guard' expr' locn
- in
- returnRn (grhs', fvsg `unionNameSets` fvse)
-
- rnGRHS (OtherwiseGRHS expr locn)
- = pushSrcLocRn locn $
- rnExpr expr `thenRn` \ (expr', fvs) ->
- returnRn (OtherwiseGRHS expr' locn, fvs)
+ rnExpr expr `thenRn` \ (expr', fvse) ->
+ returnRn (GRHS guard' expr' locn, fvse))
+
+ -- Standard Haskell 1.4 guards are just a single boolean
+ -- expression, rather than a list of qualifiers as in the
+ -- Glasgow extension
+ is_standard_guard [] = True
+ is_standard_guard [GuardStmt _ _] = True
+ is_standard_guard other = False
\end{code}
%************************************************************************
\begin{code}
rnExprs :: [RdrNameHsExpr] -> RnMS s ([RenamedHsExpr], FreeVars)
-rnExprs ls =
- rnExprs' ls [] `thenRn` \ (exprs, fvExprs) ->
- returnRn (exprs, unionManyNameSets fvExprs)
-
-rnExprs' [] acc = returnRn ([], acc)
-rnExprs' (expr:exprs) acc
- = rnExpr expr `thenRn` \ (expr', fvExpr) ->
- rnExprs' exprs (fvExpr:acc) `thenRn` \ (exprs', fvExprs) ->
+rnExprs ls = rnExprs' ls emptyUniqSet
+ where
+ rnExprs' [] acc = returnRn ([], acc)
+ rnExprs' (expr:exprs) acc
+ = rnExpr expr `thenRn` \ (expr', fvExpr) ->
+
+ -- Now we do a "seq" on the free vars because typically it's small
+ -- or empty, especially in very long lists of constants
+ let
+ acc' = acc `unionNameSets` fvExpr
+ in
+ (grubby_seqNameSet acc' rnExprs') exprs acc' `thenRn` \ (exprs', fvExprs) ->
returnRn (expr':exprs', fvExprs)
+
+-- Grubby little function to do "seq" on namesets; replace by proper seq when GHC can do seq
+grubby_seqNameSet ns result | isNullUFM ns = result
+ | otherwise = result
\end{code}
Variables. We look up the variable and return the resulting name. The
rnExpr :: RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
rnExpr (HsVar v)
- = lookupOccRn v `thenRn` \ vname ->
- returnRn (HsVar vname, if isLocallyDefined vname
- then unitNameSet vname
- else emptyUniqSet)
+ = lookupOccRn v `thenRn` \ name ->
+ if nameUnique name == assertIdKey then
+ -- We expand it to (GHCerr.assert__ location)
+ mkAssertExpr `thenRn` \ expr ->
+ returnRn (expr, emptyUniqSet)
+ else
+ -- The normal case
+ returnRn (HsVar name, if isLocallyDefined name
+ then unitNameSet name
+ else emptyUniqSet)
rnExpr (HsLit lit)
= litOccurrence lit `thenRn_`
rnExpr e2 `thenRn` \ (e2', fv_e2) ->
rnExpr op `thenRn` \ (op', fv_op) ->
- -- Deal wth fixity
+ -- Deal with fixity
+ -- When renaming code synthesised from "deriving" declarations
+ -- we're in Interface mode, and we should ignore fixity; assume
+ -- that the deriving code generator got the association correct
lookupFixity op_name `thenRn` \ fixity ->
getModeRn `thenRn` \ mode ->
(case mode of
- SourceMode -> mkOpAppRn e1' op' fixity e2'
- InterfaceMode -> returnRn (OpApp e1' op' fixity e2')
+ SourceMode -> mkOpAppRn e1' op' fixity e2'
+ InterfaceMode _ _ -> returnRn (OpApp e1' op' fixity e2')
) `thenRn` \ final_e ->
returnRn (final_e,
rnExpr (NegApp e n)
= rnExpr e `thenRn` \ (e', fv_e) ->
lookupImplicitOccRn negate_RDR `thenRn` \ neg ->
- getModeRn `thenRn` \ mode ->
- mkNegAppRn mode e' (HsVar neg) `thenRn` \ final_e ->
+ mkNegAppRn e' (HsVar neg) `thenRn` \ final_e ->
returnRn (final_e, fv_e)
rnExpr (HsPar e)
returnRn (SectionR op' expr', fvs_op `unionNameSets` fvs_expr)
rnExpr (CCall fun args may_gc is_casm fake_result_ty)
+ -- Check out the comment on RnIfaces.getNonWiredDataDecl about ccalls
= lookupImplicitOccRn ccallableClass_RDR `thenRn_`
lookupImplicitOccRn creturnableClass_RDR `thenRn_`
+ lookupImplicitOccRn ioDataCon_RDR `thenRn_`
+ lookupImplicitOccRn ioOkDataCon_RDR `thenRn_`
rnExprs args `thenRn` \ (args', fvs_args) ->
returnRn (CCall fun args' may_gc is_casm fake_result_ty, fvs_args)
rnExpr (HsDo do_or_lc stmts src_loc)
= pushSrcLocRn src_loc $
lookupImplicitOccRn monadZeroClass_RDR `thenRn_` -- Forces Monad to come too
- rnStmts stmts `thenRn` \ (stmts', fvStmts) ->
- returnRn (HsDo do_or_lc stmts' src_loc, fvStmts)
+ (rnStmts rnExpr stmts $ \ stmts' ->
+ returnRn (HsDo do_or_lc stmts' src_loc, emptyNameSet))
rnExpr (ExplicitList exps)
= addImplicitOccRn listType_name `thenRn_`
rnExprs exps `thenRn` \ (exps', fvExps) ->
returnRn (ExplicitTuple exps', fvExps)
-rnExpr (RecordCon (HsVar con) rbinds)
- = lookupOccRn con `thenRn` \ conname ->
+rnExpr (RecordCon con_id _ rbinds)
+ = lookupOccRn con_id `thenRn` \ conname ->
rnRbinds "construction" rbinds `thenRn` \ (rbinds', fvRbinds) ->
- returnRn (RecordCon (HsVar conname) rbinds', fvRbinds)
+ returnRn (RecordCon conname (error "rnExpr:RecordCon") rbinds', fvRbinds)
rnExpr (RecordUpd expr rbinds)
= rnExpr expr `thenRn` \ (expr', fvExpr) ->
returnRn (RecordUpd expr' rbinds', fvExpr `unionNameSets` fvRbinds)
rnExpr (ExprWithTySig expr pty)
- = rnExpr expr `thenRn` \ (expr', fvExpr) ->
- rnHsType pty `thenRn` \ pty' ->
+ = rnExpr expr `thenRn` \ (expr', fvExpr) ->
+ rnHsSigType (text "an expression") pty `thenRn` \ pty' ->
returnRn (ExprWithTySig expr' pty', fvExpr)
rnExpr (HsIf p b1 b2 src_loc)
mapAndUnzipRn rn_rbind rbinds `thenRn` \ (rbinds', fvRbind_s) ->
returnRn (rbinds', unionManyNameSets fvRbind_s)
where
- (_, dup_fields) = removeDups cmp [ f | (f,_,_) <- rbinds ]
+ (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rbinds ]
field_dup_err dups = addErrRn (dupFieldErr str dups)
= mapRn field_dup_err dup_fields `thenRn_`
mapRn rn_rpat rpats
where
- (_, dup_fields) = removeDups cmp [ f | (f,_,_) <- rpats ]
+ (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rpats ]
field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
Quals.
\begin{code}
-rnStmts :: [RdrNameStmt] -> RnMS s ([RenamedStmt], FreeVars)
+type RnExprTy s = RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
-rnStmts [] = returnRn ([], emptyNameSet)
+rnStmts :: RnExprTy s
+ -> [RdrNameStmt]
+ -> ([RenamedStmt] -> RnMS s (a, FreeVars))
+ -> RnMS s (a, FreeVars)
-rnStmts (stmt:stmts)
- = rnStmt stmt $ \ stmt' ->
- rnStmts stmts `thenRn` \ (stmts', fv_stmts) ->
- returnRn (stmt':stmts', fv_stmts)
+rnStmts rn_expr [] thing_inside
+ = thing_inside []
+rnStmts rn_expr (stmt:stmts) thing_inside
+ = rnStmt rn_expr stmt $ \ stmt' ->
+ rnStmts rn_expr stmts $ \ stmts' ->
+ thing_inside (stmt' : stmts')
--- rnStmt :: RdrNameStmt -> (RenamedStmt -> RnMS s (a, FreeVars)) -> RnMS s (a, FreeVars)
--- Because of mutual recursion the actual type is a bit less general than this [Haskell 1.2]
+rnStmt :: RnExprTy s -> RdrNameStmt -> (RenamedStmt -> RnMS s (a, FreeVars)) -> RnMS s (a, FreeVars)
+-- Because of mutual recursion we have to pass in rnExpr.
-rnStmt (BindStmt pat expr src_loc) thing_inside
+rnStmt rn_expr (BindStmt pat expr src_loc) thing_inside
= pushSrcLocRn src_loc $
- rnExpr expr `thenRn` \ (expr', fv_expr) ->
+ rn_expr expr `thenRn` \ (expr', fv_expr) ->
bindLocalsRn "pattern in do binding" binders $ \ new_binders ->
rnPat pat `thenRn` \ pat' ->
where
binders = collectPatBinders pat
-rnStmt (ExprStmt expr src_loc) thing_inside
+rnStmt rn_expr (ExprStmt expr src_loc) thing_inside
= pushSrcLocRn src_loc $
- rnExpr expr `thenRn` \ (expr', fv_expr) ->
+ rn_expr expr `thenRn` \ (expr', fv_expr) ->
thing_inside (ExprStmt expr' src_loc) `thenRn` \ (result, fvs) ->
returnRn (result, fv_expr `unionNameSets` fvs)
-rnStmt (GuardStmt expr src_loc) thing_inside
+rnStmt rn_expr (GuardStmt expr src_loc) thing_inside
= pushSrcLocRn src_loc $
- rnExpr expr `thenRn` \ (expr', fv_expr) ->
+ rn_expr expr `thenRn` \ (expr', fv_expr) ->
thing_inside (GuardStmt expr' src_loc) `thenRn` \ (result, fvs) ->
returnRn (result, fv_expr `unionNameSets` fvs)
-rnStmt (ReturnStmt expr) thing_inside
- = rnExpr expr `thenRn` \ (expr', fv_expr) ->
+rnStmt rn_expr (ReturnStmt expr) thing_inside
+ = rn_expr expr `thenRn` \ (expr', fv_expr) ->
thing_inside (ReturnStmt expr') `thenRn` \ (result, fvs) ->
returnRn (result, fv_expr `unionNameSets` fvs)
-rnStmt (LetStmt binds) thing_inside
+rnStmt rn_expr (LetStmt binds) thing_inside
= rnBinds binds $ \ binds' ->
thing_inside (LetStmt binds')
\end{code}
returnRn (OpApp e11 op1 fix1 new_e)
where
(nofix_error, rearrange_me) = compareFixity fix1 fix2
- get (HsVar n) = n
-mkOpAppRn e1@(NegApp neg_arg neg_id)
+mkOpAppRn e1@(NegApp neg_arg neg_op)
op2
fix2@(Fixity prec2 dir2)
e2
- | prec2 > 6 -- Precedence of unary - is wired in as 6!
+ | nofix_error
+ = addErrRn (precParseErr (get neg_op,fix_neg) (get op2,fix2)) `thenRn_`
+ returnRn (OpApp e1 op2 fix2 e2)
+
+ | rearrange_me
= mkOpAppRn neg_arg op2 fix2 e2 `thenRn` \ new_e ->
- returnRn (NegApp new_e neg_id)
+ returnRn (NegApp new_e neg_op)
+ where
+ fix_neg = Fixity 6 InfixL -- Precedence of unary negate is wired in as infixl 6!
+ (nofix_error, rearrange_me) = compareFixity fix_neg fix2
mkOpAppRn e1 op fix e2 -- Default case, no rearrangment
- = ASSERT( right_op_ok fix e2 )
+ = ASSERT( if right_op_ok fix e2 then True
+ else pprPanic "mkOpAppRn" (vcat [ppr e1, text "---", ppr op, text "---", ppr fix, text "---", ppr e2])
+ )
returnRn (OpApp e1 op fix e2)
+get (HsVar n) = n
+
-- Parser left-associates everything, but
-- derived instances may have correctly-associated things to
-- in the right operarand. So we just check that the right operand is OK
= True
-- Parser initially makes negation bind more tightly than any other operator
-mkNegAppRn mode neg_arg neg_id
- = ASSERT( not_op_app mode neg_arg )
- returnRn (NegApp neg_arg neg_id)
+mkNegAppRn neg_arg neg_op
+ =
+#ifdef DEBUG
+ getModeRn `thenRn` \ mode ->
+ ASSERT( not_op_app mode neg_arg )
+#endif
+ returnRn (NegApp neg_arg neg_op)
not_op_app SourceMode (OpApp _ _ _ _) = False
not_op_app mode other = True
-> (Bool, -- Error please
Bool) -- Associate to the right: a op1 (b op2 c)
compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
- = case prec1 `cmp` prec2 of
- GT_ -> left
- LT_ -> right
- EQ_ -> case (dir1, dir2) of
+ = case prec1 `compare` prec2 of
+ GT -> left
+ LT -> right
+ EQ -> case (dir1, dir2) of
(InfixR, InfixR) -> right
(InfixL, InfixL) -> left
_ -> error_please
= lookupImplicitOccRn numClass_RDR -- Int and Integer are forced in by Num
litOccurrence (HsFrac _)
- = lookupImplicitOccRn fractionalClass_RDR -- ... similarly Rational
-
+ = lookupImplicitOccRn fractionalClass_RDR `thenRn_`
+ lookupImplicitOccRn ratioDataCon_RDR
+ -- We have to make sure that the Ratio type is imported with
+ -- its constructor, because literals of type Ratio t are
+ -- built with that constructor.
+ -- The Rational type is needed too, but that will come in
+ -- when fractionalClass does.
+
litOccurrence (HsIntPrim _)
= addImplicitOccRn (getName intPrimTyCon)
= lookupImplicitOccRn ccallableClass_RDR
\end{code}
+%************************************************************************
+%* *
+\subsubsection{Assertion utils}
+%* *
+%************************************************************************
+
+\begin{code}
+mkAssertExpr :: RnMS s RenamedHsExpr
+mkAssertExpr =
+ newImportedGlobalName mod occ HiFile `thenRn` \ name ->
+ addOccurrenceName name `thenRn_`
+ getSrcLocRn `thenRn` \ sloc ->
+ let
+ expr = HsApp (HsVar name)
+ (HsLit (HsString (_PK_ (showSDoc (ppr sloc)))))
+ in
+ returnRn expr
+
+ where
+ mod = rdrNameModule assertErr_RDR
+ occ = rdrNameOcc assertErr_RDR
+\end{code}
%************************************************************************
%* *
%************************************************************************
\begin{code}
-dupFieldErr str (dup:rest) sty
- = ppBesides [ppPStr SLIT("duplicate field name `"),
- ppr sty dup,
- ppPStr SLIT("' in record "), ppStr str]
-
-negPatErr pat sty
- = ppSep [ppPStr SLIT("prefix `-' not applied to literal in pattern"), ppr sty pat]
-
-precParseNegPatErr op sty
- = ppHang (ppPStr SLIT("precedence parsing error"))
- 4 (ppBesides [ppPStr SLIT("prefix `-' has lower precedence than "),
- pp_op sty op,
- ppPStr SLIT(" in pattern")])
-
-precParseErr op1 op2 sty
- = ppHang (ppPStr SLIT("precedence parsing error"))
- 4 (ppBesides [ppPStr SLIT("cannot mix "), pp_op sty op1, ppPStr SLIT(" and "), pp_op sty op2,
- ppPStr SLIT(" in the same infix expression")])
-
-pp_op sty (op, fix) = ppBesides [pprSym sty op, ppLparen, ppr sty fix, ppRparen]
+dupFieldErr str (dup:rest)
+ = hsep [ptext SLIT("duplicate field name"),
+ quotes (ppr dup),
+ ptext SLIT("in record"), text str]
+
+negPatErr pat
+ = sep [ptext SLIT("prefix `-' not applied to literal in pattern"), quotes (ppr pat)]
+
+precParseNegPatErr op
+ = hang (ptext SLIT("precedence parsing error"))
+ 4 (hsep [ptext SLIT("prefix `-' has lower precedence than"),
+ quotes (pp_op op),
+ ptext SLIT("in pattern")])
+
+precParseErr op1 op2
+ = hang (ptext SLIT("precedence parsing error"))
+ 4 (hsep [ptext SLIT("cannot mix"), quotes (pp_op op1), ptext SLIT("and"),
+ quotes (pp_op op2),
+ ptext SLIT("in the same infix expression")])
+
+nonStdGuardErr guard
+ = hang (ptext SLIT("accepting non-standard pattern guards (-fglasgow-exts to suppress this message)"))
+ 4 (ppr guard)
+
+pp_op (op, fix) = hcat [ppr op, space, parens (ppr fix)]
\end{code}