Basically dependency analysis.
-Handles @Match@, @GRHSsAndBinds@, @HsExpr@, and @Qual@ datatypes. In
+Handles @Match@, @GRHSsAndBinds@, @HsExpr@, and @Qualifier@ datatypes. In
general, all of these functions return a renamed thing, and a set of
free variables.
\begin{code}
-#include "HsVersions.h"
-
module RnExpr (
rnMatch, rnGRHSsAndBinds, rnPat,
checkPrecMatch
) where
-import Ubiq
-import 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 ErrUtils ( addErrLoc, addShortErrLocLine )
-import Name ( isLocallyDefinedName, pprSym, Name, RdrName )
-import Pretty
-import UniqFM ( lookupUFM )
-import UniqSet ( emptyUniqSet, unitUniqSet,
- unionUniqSets, unionManyUniqSets,
- UniqSet(..) )
-import Util ( Ord3(..), removeDups, panic )
+import RnEnv
+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 Name
+import UniqFM ( isNullUFM )
+import UniqSet ( emptyUniqSet, unionManyUniqSets, UniqSet )
+import Unique ( assertIdKey )
+import Util ( removeDups )
+import Outputable
\end{code}
*********************************************************
\begin{code}
-rnPat :: RdrNamePat -> RnM_Fixes s RenamedPat
+rnPat :: RdrNamePat -> RnMS s RenamedPat
rnPat WildPatIn = returnRn WildPatIn
rnPat (VarPatIn name)
- = lookupValue name `thenRn` \ vname ->
+ = lookupBndrRn name `thenRn` \ vname ->
returnRn (VarPatIn vname)
-rnPat (LitPatIn n) = returnRn (LitPatIn n)
+rnPat (LitPatIn lit)
+ = litOccurrence lit `thenRn_`
+ lookupImplicitOccRn eqClass_RDR `thenRn_` -- Needed to find equality on pattern
+ returnRn (LitPatIn lit)
rnPat (LazyPatIn pat)
= rnPat pat `thenRn` \ pat' ->
rnPat (AsPatIn name pat)
= rnPat pat `thenRn` \ pat' ->
- lookupValue name `thenRn` \ vname ->
+ lookupBndrRn name `thenRn` \ vname ->
returnRn (AsPatIn vname pat')
rnPat (ConPatIn con pats)
- = lookupConstr con `thenRn` \ con' ->
+ = lookupOccRn con `thenRn` \ con' ->
mapRn rnPat pats `thenRn` \ patslist ->
returnRn (ConPatIn con' patslist)
-rnPat (ConOpPatIn pat1 con pat2)
- = lookupConstr con `thenRn` \ con' ->
- rnPat pat1 `thenRn` \ pat1' ->
+rnPat (ConOpPatIn pat1 con _ pat2)
+ = rnPat pat1 `thenRn` \ pat1' ->
+ lookupOccRn con `thenRn` \ con' ->
+ lookupFixity con `thenRn` \ fixity ->
rnPat pat2 `thenRn` \ pat2' ->
- precParsePat (ConOpPatIn pat1' con' pat2')
+ mkConOpPatRn pat1' con' fixity pat2'
+-- Negated patters can only be literals, and they are dealt with
+-- by negating the literal at compile time, not by using the negation
+-- operation in Num. So we don't need to make an implicit reference
+-- to negate_RDR.
rnPat neg@(NegPatIn pat)
- = getSrcLocRn `thenRn` \ src_loc ->
- addErrIfRn (not (valid_neg_pat pat)) (negPatErr neg src_loc)
+ = checkRn (valid_neg_pat pat) (negPatErr neg)
`thenRn_`
rnPat pat `thenRn` \ pat' ->
returnRn (NegPatIn pat')
= rnPat pat `thenRn` \ pat' ->
returnRn (ParPatIn pat')
+rnPat (NPlusKPatIn name lit)
+ = litOccurrence lit `thenRn_`
+ lookupImplicitOccRn ordClass_RDR `thenRn_`
+ lookupBndrRn name `thenRn` \ name' ->
+ returnRn (NPlusKPatIn name' lit)
+
rnPat (ListPatIn pats)
- = mapRn rnPat pats `thenRn` \ patslist ->
+ = addImplicitOccRn listType_name `thenRn_`
+ mapRn rnPat pats `thenRn` \ patslist ->
returnRn (ListPatIn patslist)
rnPat (TuplePatIn pats)
- = mapRn rnPat pats `thenRn` \ patslist ->
+ = addImplicitOccRn (tupleType_name (length pats)) `thenRn_`
+ mapRn rnPat pats `thenRn` \ patslist ->
returnRn (TuplePatIn patslist)
rnPat (RecPatIn con rpats)
- = lookupConstr con `thenRn` \ con' ->
+ = lookupOccRn con `thenRn` \ con' ->
rnRpats rpats `thenRn` \ rpats' ->
returnRn (RecPatIn con' rpats')
\end{code}
************************************************************************
\begin{code}
-rnMatch :: RdrNameMatch -> RnM_Fixes s (RenamedMatch, FreeVars)
+rnMatch, rnMatch1 :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
-rnMatch match
- = getSrcLocRn `thenRn` \ src_loc ->
- newLocalNames "variable in pattern"
- (binders `zip` repeat src_loc) `thenRn` \ new_binders ->
- extendSS2 new_binders (rnMatch_aux match)
- where
- binders = collect_binders match
-
- collect_binders :: RdrNameMatch -> [RdrName]
-
- collect_binders (GRHSMatch _) = []
- collect_binders (PatMatch pat match)
- = collectPatBinders pat ++ collect_binders match
-
-rnMatch_aux (PatMatch pat match)
- = rnPat pat `thenRn` \ pat' ->
- rnMatch_aux match `thenRn` \ (match', fvMatch) ->
- returnRn (PatMatch pat' match', fvMatch)
+-- 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_aux (GRHSMatch grhss_and_binds)
+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
+ get_binders (GRHSMatch _) = []
+ get_binders (PatMatch pat match) = collectPatBinders pat ++ get_binders match
+
+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}
%************************************************************************
\begin{code}
-rnGRHSsAndBinds :: RdrNameGRHSsAndBinds -> RnM_Fixes s (RenamedGRHSsAndBinds, FreeVars)
+rnGRHSsAndBinds :: RdrNameGRHSsAndBinds -> RnMS s (RenamedGRHSsAndBinds, FreeVars)
rnGRHSsAndBinds (GRHSsAndBindsIn grhss binds)
- = rnBinds binds `thenRn` \ (binds', fvBinds, scope) ->
- extendSS2 scope (rnGRHSs grhss) `thenRn` \ (grhss', fvGRHS) ->
- returnRn (GRHSsAndBindsIn grhss' binds', fvBinds `unionUniqSets` fvGRHS)
+ = rnBinds binds $ \ binds' ->
+ rnGRHSs grhss `thenRn` \ (grhss', fvGRHS) ->
+ returnRn (GRHSsAndBindsIn grhss' binds', fvGRHS)
where
- rnGRHSs [] = returnRn ([], emptyUniqSet)
+ rnGRHSs [] = returnRn ([], emptyNameSet)
rnGRHSs (grhs:grhss)
= rnGRHS grhs `thenRn` \ (grhs', fvs) ->
rnGRHSs grhss `thenRn` \ (grhss', fvss) ->
- returnRn (grhs' : grhss', fvs `unionUniqSets` fvss)
+ returnRn (grhs' : grhss', fvs `unionNameSets` fvss)
rnGRHS (GRHS guard expr locn)
= pushSrcLocRn locn $
- rnExpr guard `thenRn` \ (guard', fvsg) ->
- rnExpr expr `thenRn` \ (expr', fvse) ->
- returnRn (GRHS guard' expr' locn, fvsg `unionUniqSets` 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
- 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] -> RnM_Fixes s ([RenamedHsExpr], FreeVars)
-
-rnExprs [] = returnRn ([], emptyUniqSet)
+rnExprs :: [RdrNameHsExpr] -> RnMS s ([RenamedHsExpr], FreeVars)
+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)
-rnExprs (expr:exprs)
- = rnExpr expr `thenRn` \ (expr', fvExpr) ->
- rnExprs exprs `thenRn` \ (exprs', fvExprs) ->
- returnRn (expr':exprs', fvExpr `unionUniqSets` 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
interesting question is what the free-variable set should be. We
don't want to return imported or prelude things as free vars. So we
-look at the RnName returned from the lookup, and make it part of the
-free-var set iff if it's a LocallyDefined RnName.
-
-ToDo: what about RnClassOps ???
+look at the Name returned from the lookup, and make it part of the
+free-var set iff if it's a LocallyDefined Name.
\end{itemize}
\begin{code}
-fv_set vname@(RnName n) | isLocallyDefinedName n
- = unitUniqSet vname
-fv_set _ = emptyUniqSet
-
-
-rnExpr :: RdrNameHsExpr -> RnM_Fixes s (RenamedHsExpr, FreeVars)
+rnExpr :: RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
rnExpr (HsVar v)
- = lookupValue v `thenRn` \ vname ->
- returnRn (HsVar vname, fv_set vname)
+ = 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)
- = returnRn (HsLit lit, emptyUniqSet)
+rnExpr (HsLit lit)
+ = litOccurrence lit `thenRn_`
+ returnRn (HsLit lit, emptyNameSet)
rnExpr (HsLam match)
= rnMatch match `thenRn` \ (match', fvMatch) ->
rnExpr (HsApp fun arg)
= rnExpr fun `thenRn` \ (fun',fvFun) ->
rnExpr arg `thenRn` \ (arg',fvArg) ->
- returnRn (HsApp fun' arg', fvFun `unionUniqSets` fvArg)
-
-rnExpr (OpApp e1 op e2)
- = rnExpr e1 `thenRn` \ (e1', fvs_e1) ->
- rnExpr op `thenRn` \ (op', fvs_op) ->
- rnExpr e2 `thenRn` \ (e2', fvs_e2) ->
- precParseExpr (OpApp e1' op' e2') `thenRn` \ exp ->
- returnRn (exp, (fvs_op `unionUniqSets` fvs_e1) `unionUniqSets` fvs_e2)
+ returnRn (HsApp fun' arg', fvFun `unionNameSets` fvArg)
+
+rnExpr (OpApp e1 op@(HsVar op_name) _ e2)
+ = rnExpr e1 `thenRn` \ (e1', fv_e1) ->
+ rnExpr e2 `thenRn` \ (e2', fv_e2) ->
+ rnExpr op `thenRn` \ (op', fv_op) ->
+
+ -- 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')
+ ) `thenRn` \ final_e ->
+
+ returnRn (final_e,
+ fv_e1 `unionNameSets` fv_op `unionNameSets` fv_e2)
rnExpr (NegApp e n)
- = rnExpr e `thenRn` \ (e', fvs_e) ->
- lookupValue n `thenRn` \ nname ->
- returnRn (NegApp e' nname, fvs_e `unionUniqSets` fv_set nname)
+ = rnExpr e `thenRn` \ (e', fv_e) ->
+ lookupImplicitOccRn negate_RDR `thenRn` \ neg ->
+ mkNegAppRn e' (HsVar neg) `thenRn` \ final_e ->
+ returnRn (final_e, fv_e)
rnExpr (HsPar e)
= rnExpr e `thenRn` \ (e', fvs_e) ->
rnExpr (SectionL expr op)
= rnExpr expr `thenRn` \ (expr', fvs_expr) ->
rnExpr op `thenRn` \ (op', fvs_op) ->
- returnRn (SectionL expr' op', fvs_op `unionUniqSets` fvs_expr)
+ returnRn (SectionL expr' op', fvs_op `unionNameSets` fvs_expr)
rnExpr (SectionR op expr)
= rnExpr op `thenRn` \ (op', fvs_op) ->
rnExpr expr `thenRn` \ (expr', fvs_expr) ->
- returnRn (SectionR op' expr', fvs_op `unionUniqSets` fvs_expr)
+ returnRn (SectionR op' expr', fvs_op `unionNameSets` fvs_expr)
rnExpr (CCall fun args may_gc is_casm fake_result_ty)
- = rnExprs args `thenRn` \ (args', fvs_args) ->
+ -- 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 (HsSCC label expr)
= pushSrcLocRn src_loc $
rnExpr expr `thenRn` \ (new_expr, e_fvs) ->
mapAndUnzipRn rnMatch ms `thenRn` \ (new_ms, ms_fvs) ->
- returnRn (HsCase new_expr new_ms src_loc, unionManyUniqSets (e_fvs : ms_fvs))
+ returnRn (HsCase new_expr new_ms src_loc, unionManyNameSets (e_fvs : ms_fvs))
rnExpr (HsLet binds expr)
- = rnBinds binds `thenRn` \ (binds', fvBinds, new_binders) ->
- extendSS2 new_binders (rnExpr expr) `thenRn` \ (expr',fvExpr) ->
- returnRn (HsLet binds' expr', fvBinds `unionUniqSets` fvExpr)
+ = rnBinds binds $ \ binds' ->
+ rnExpr expr `thenRn` \ (expr',fvExpr) ->
+ returnRn (HsLet binds' expr', fvExpr)
-rnExpr (HsDo stmts src_loc)
+rnExpr (HsDo do_or_lc stmts src_loc)
= pushSrcLocRn src_loc $
- rnStmts stmts `thenRn` \ (stmts', fvStmts) ->
- returnRn (HsDo stmts' src_loc, fvStmts)
-
-rnExpr (ListComp expr quals)
- = rnQuals quals `thenRn` \ ((quals', qual_binders), fvQuals) ->
- extendSS2 qual_binders (rnExpr expr) `thenRn` \ (expr', fvExpr) ->
- returnRn (ListComp expr' quals', fvExpr `unionUniqSets` fvQuals)
+ lookupImplicitOccRn monadZeroClass_RDR `thenRn_` -- Forces Monad to come too
+ (rnStmts rnExpr stmts $ \ stmts' ->
+ returnRn (HsDo do_or_lc stmts' src_loc, emptyNameSet))
rnExpr (ExplicitList exps)
- = rnExprs exps `thenRn` \ (exps', fvs) ->
+ = addImplicitOccRn listType_name `thenRn_`
+ rnExprs exps `thenRn` \ (exps', fvs) ->
returnRn (ExplicitList exps', fvs)
rnExpr (ExplicitTuple exps)
- = rnExprs exps `thenRn` \ (exps', fvExps) ->
+ = addImplicitOccRn (tupleType_name (length exps)) `thenRn_`
+ rnExprs exps `thenRn` \ (exps', fvExps) ->
returnRn (ExplicitTuple exps', fvExps)
-rnExpr (RecordCon (HsVar con) rbinds)
- = lookupConstr 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) ->
rnRbinds "update" rbinds `thenRn` \ (rbinds', fvRbinds) ->
- returnRn (RecordUpd expr' rbinds', fvExpr `unionUniqSets` fvRbinds)
+ returnRn (RecordUpd expr' rbinds', fvExpr `unionNameSets` fvRbinds)
rnExpr (ExprWithTySig expr pty)
- = rnExpr expr `thenRn` \ (expr', fvExpr) ->
- rnPolyType nullTyVarNamesEnv 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)
rnExpr p `thenRn` \ (p', fvP) ->
rnExpr b1 `thenRn` \ (b1', fvB1) ->
rnExpr b2 `thenRn` \ (b2', fvB2) ->
- returnRn (HsIf p' b1' b2' src_loc, unionManyUniqSets [fvP, fvB1, fvB2])
+ returnRn (HsIf p' b1' b2' src_loc, unionManyNameSets [fvP, fvB1, fvB2])
rnExpr (ArithSeqIn seq)
- = rn_seq seq `thenRn` \ (new_seq, fvs) ->
+ = lookupImplicitOccRn enumClass_RDR `thenRn_`
+ rn_seq seq `thenRn` \ (new_seq, fvs) ->
returnRn (ArithSeqIn new_seq, fvs)
where
rn_seq (From expr)
rn_seq (FromThen expr1 expr2)
= rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
- returnRn (FromThen expr1' expr2', fvExpr1 `unionUniqSets` fvExpr2)
+ returnRn (FromThen expr1' expr2', fvExpr1 `unionNameSets` fvExpr2)
rn_seq (FromTo expr1 expr2)
= rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
- returnRn (FromTo expr1' expr2', fvExpr1 `unionUniqSets` fvExpr2)
+ returnRn (FromTo expr1' expr2', fvExpr1 `unionNameSets` fvExpr2)
rn_seq (FromThenTo expr1 expr2 expr3)
= rnExpr expr1 `thenRn` \ (expr1', fvExpr1) ->
rnExpr expr2 `thenRn` \ (expr2', fvExpr2) ->
rnExpr expr3 `thenRn` \ (expr3', fvExpr3) ->
returnRn (FromThenTo expr1' expr2' expr3',
- unionManyUniqSets [fvExpr1, fvExpr2, fvExpr3])
+ unionManyNameSets [fvExpr1, fvExpr2, fvExpr3])
\end{code}
%************************************************************************
rnRbinds str rbinds
= mapRn field_dup_err dup_fields `thenRn_`
mapAndUnzipRn rn_rbind rbinds `thenRn` \ (rbinds', fvRbind_s) ->
- returnRn (rbinds', unionManyUniqSets 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 = getSrcLocRn `thenRn` \ src_loc ->
- addErrRn (dupFieldErr str src_loc dups)
+ field_dup_err dups = addErrRn (dupFieldErr str dups)
rn_rbind (field, expr, pun)
- = lookupField field `thenRn` \ fieldname ->
+ = lookupGlobalOccRn field `thenRn` \ fieldname ->
rnExpr expr `thenRn` \ (expr', fvExpr) ->
returnRn ((fieldname, expr', pun), fvExpr)
= 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 = getSrcLocRn `thenRn` \ src_loc ->
- addErrRn (dupFieldErr "pattern" src_loc dups)
+ field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
rn_rpat (field, pat, pun)
- = lookupField field `thenRn` \ fieldname ->
+ = lookupGlobalOccRn field `thenRn` \ fieldname ->
rnPat pat `thenRn` \ pat' ->
returnRn (fieldname, pat', pun)
\end{code}
%************************************************************************
%* *
-\subsubsection{@Qual@s: in list comprehensions}
+\subsubsection{@Stmt@s: in @do@ expressions}
%* *
%************************************************************************
Quals.
\begin{code}
-rnQuals :: [RdrNameQual]
- -> RnM_Fixes s (([RenamedQual], -- renamed qualifiers
- [RnName]), -- qualifiers' binders
- FreeVars) -- free variables
-
-rnQuals [qual] -- must be at least one qual
- = rnQual qual `thenRn` \ ((new_qual, bs), fvs) ->
- returnRn (([new_qual], bs), fvs)
-
-rnQuals (qual: quals)
- = rnQual qual `thenRn` \ ((qual', bs1), fvQuals1) ->
- extendSS2 bs1 (rnQuals quals) `thenRn` \ ((quals', bs2), fvQuals2) ->
- returnRn
- ((qual' : quals', bs1 ++ bs2), -- The ones on the right (bs2) shadow the
- -- ones on the left (bs1)
- fvQuals1 `unionUniqSets` fvQuals2)
-
-rnQual (GeneratorQual pat expr)
- = rnExpr expr `thenRn` \ (expr', fvExpr) ->
- let
- binders = collectPatBinders pat
- in
- getSrcLocRn `thenRn` \ src_loc ->
- newLocalNames "variable in list-comprehension-generator pattern"
- (binders `zip` repeat src_loc) `thenRn` \ new_binders ->
- extendSS new_binders (rnPat pat) `thenRn` \ pat' ->
+type RnExprTy s = RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
- returnRn ((GeneratorQual pat' expr', new_binders), fvExpr)
+rnStmts :: RnExprTy s
+ -> [RdrNameStmt]
+ -> ([RenamedStmt] -> RnMS s (a, FreeVars))
+ -> RnMS s (a, FreeVars)
-rnQual (FilterQual expr)
- = rnExpr expr `thenRn` \ (expr', fvs) ->
- returnRn ((FilterQual expr', []), fvs)
+rnStmts rn_expr [] thing_inside
+ = thing_inside []
-rnQual (LetQual binds)
- = rnBinds binds `thenRn` \ (binds', binds_fvs, new_binders) ->
- returnRn ((LetQual binds', new_binders), binds_fvs)
-\end{code}
+rnStmts rn_expr (stmt:stmts) thing_inside
+ = rnStmt rn_expr stmt $ \ stmt' ->
+ rnStmts rn_expr stmts $ \ stmts' ->
+ thing_inside (stmt' : stmts')
+rnStmt :: RnExprTy s -> RdrNameStmt -> (RenamedStmt -> RnMS s (a, FreeVars)) -> RnMS s (a, FreeVars)
+-- Because of mutual recursion we have to pass in rnExpr.
-%************************************************************************
-%* *
-\subsubsection{@Stmt@s: in @do@ expressions}
-%* *
-%************************************************************************
-
-\begin{code}
-rnStmts :: [RdrNameStmt] -> RnM_Fixes s ([RenamedStmt], FreeVars)
-
-rnStmts [stmt@(ExprStmt _ _)] -- last stmt must be ExprStmt
- = rnStmt stmt `thenRn` \ ((stmt',[]), fvStmt) ->
- returnRn ([stmt'], fvStmt)
-
-rnStmts (stmt:stmts)
- = rnStmt stmt `thenRn` \ ((stmt',bs), fvStmt) ->
- extendSS2 bs (rnStmts stmts) `thenRn` \ (stmts', fvStmts) ->
- returnRn (stmt':stmts', fvStmt `unionUniqSets` fvStmts)
-
-
-rnStmt (BindStmt pat expr src_loc)
+rnStmt rn_expr (BindStmt pat expr src_loc) thing_inside
= pushSrcLocRn src_loc $
- rnExpr expr `thenRn` \ (expr', fvExpr) ->
- let
- binders = collectPatBinders pat
- in
- newLocalNames "variable in do binding"
- (binders `zip` repeat src_loc) `thenRn` \ new_binders ->
- extendSS new_binders (rnPat pat) `thenRn` \ pat' ->
+ rn_expr expr `thenRn` \ (expr', fv_expr) ->
+ bindLocalsRn "pattern in do binding" binders $ \ new_binders ->
+ rnPat pat `thenRn` \ pat' ->
- returnRn ((BindStmt pat' expr' src_loc, new_binders), fvExpr)
-
-rnStmt (ExprStmt expr src_loc)
- =
- rnExpr expr `thenRn` \ (expr', fvs) ->
- returnRn ((ExprStmt expr' src_loc, []), fvs)
+ thing_inside (BindStmt pat' expr' src_loc) `thenRn` \ (result, fvs) ->
+ returnRn (result, fv_expr `unionNameSets` (fvs `minusNameSet` mkNameSet new_binders))
+ where
+ binders = collectPatBinders pat
-rnStmt (LetStmt binds)
- = rnBinds binds `thenRn` \ (binds', binds_fvs, new_binders) ->
- returnRn ((LetStmt binds', new_binders), binds_fvs)
+rnStmt rn_expr (ExprStmt expr src_loc) thing_inside
+ = pushSrcLocRn src_loc $
+ rn_expr expr `thenRn` \ (expr', fv_expr) ->
+ thing_inside (ExprStmt expr' src_loc) `thenRn` \ (result, fvs) ->
+ returnRn (result, fv_expr `unionNameSets` fvs)
+rnStmt rn_expr (GuardStmt expr src_loc) thing_inside
+ = pushSrcLocRn src_loc $
+ rn_expr expr `thenRn` \ (expr', fv_expr) ->
+ thing_inside (GuardStmt expr' src_loc) `thenRn` \ (result, fvs) ->
+ returnRn (result, fv_expr `unionNameSets` fvs)
+
+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 rn_expr (LetStmt binds) thing_inside
+ = rnBinds binds $ \ binds' ->
+ thing_inside (LetStmt binds')
\end{code}
%************************************************************************
%* *
%************************************************************************
-\begin{code}
-precParseExpr :: RenamedHsExpr -> RnM_Fixes s RenamedHsExpr
-precParsePat :: RenamedPat -> RnM_Fixes s RenamedPat
-
-precParseExpr exp@(OpApp (NegApp e1 n) (HsVar op) e2)
- = lookupFixity op `thenRn` \ (op_fix, op_prec) ->
- if 6 < op_prec then
- -- negate precedence 6 wired in
- -- (-x)*y ==> -(x*y)
- precParseExpr (OpApp e1 (HsVar op) e2) `thenRn` \ op_app ->
- returnRn (NegApp op_app n)
- else
- returnRn exp
-
-precParseExpr exp@(OpApp (OpApp e11 (HsVar op1) e12) (HsVar op) e2)
- = lookupFixity op `thenRn` \ (op_fix, op_prec) ->
- lookupFixity op1 `thenRn` \ (op1_fix, op1_prec) ->
- case cmp op1_prec op_prec of
- LT_ -> rearrange
- EQ_ -> case (op1_fix, op_fix) of
- (INFIXR, INFIXR) -> rearrange
- (INFIXL, INFIXL) -> returnRn exp
- _ -> getSrcLocRn `thenRn` \ src_loc ->
- failButContinueRn exp
- (precParseErr (op1,op1_fix,op1_prec) (op,op_fix,op_prec) src_loc)
- GT__ -> returnRn exp
- where
- rearrange = precParseExpr (OpApp e12 (HsVar op) e2) `thenRn` \ e2' ->
- returnRn (OpApp e11 (HsVar op1) e2')
-
-precParseExpr exp = returnRn exp
+@mkOpAppRn@ deals with operator fixities. The argument expressions
+are assumed to be already correctly arranged. It needs the fixities
+recorded in the OpApp nodes, because fixity info applies to the things
+the programmer actually wrote, so you can't find it out from the Name.
+Furthermore, the second argument is guaranteed not to be another
+operator application. Why? Because the parser parses all
+operator appications left-associatively.
-precParsePat pat@(ConOpPatIn (NegPatIn e1) op e2)
- = lookupFixity op `thenRn` \ (op_fix, op_prec) ->
- if 6 < op_prec then
- -- negate precedence 6 wired in
- getSrcLocRn `thenRn` \ src_loc ->
- failButContinueRn pat (precParseNegPatErr (op,op_fix,op_prec) src_loc)
- else
- returnRn pat
-
-precParsePat pat@(ConOpPatIn (ConOpPatIn p11 op1 p12) op p2)
- = lookupFixity op `thenRn` \ (op_fix, op_prec) ->
- lookupFixity op1 `thenRn` \ (op1_fix, op1_prec) ->
- case cmp op1_prec op_prec of
- LT_ -> rearrange
- EQ_ -> case (op1_fix, op_fix) of
- (INFIXR, INFIXR) -> rearrange
- (INFIXL, INFIXL) -> returnRn pat
- _ -> getSrcLocRn `thenRn` \ src_loc ->
- failButContinueRn pat
- (precParseErr (op1,op1_fix,op1_prec) (op,op_fix,op_prec) src_loc)
- GT__ -> returnRn pat
+\begin{code}
+mkOpAppRn :: RenamedHsExpr -> RenamedHsExpr -> Fixity -> RenamedHsExpr
+ -> RnMS s RenamedHsExpr
+
+mkOpAppRn e1@(OpApp e11 op1 fix1 e12)
+ op2 fix2 e2
+ | nofix_error
+ = addErrRn (precParseErr (get op1,fix1) (get op2,fix2)) `thenRn_`
+ returnRn (OpApp e1 op2 fix2 e2)
+
+ | rearrange_me
+ = mkOpAppRn e12 op2 fix2 e2 `thenRn` \ new_e ->
+ returnRn (OpApp e11 op1 fix1 new_e)
where
- rearrange = precParsePat (ConOpPatIn p12 op p2) `thenRn` \ p2' ->
- returnRn (ConOpPatIn p11 op1 p2')
+ (nofix_error, rearrange_me) = compareFixity fix1 fix2
+
+mkOpAppRn e1@(NegApp neg_arg neg_op)
+ op2
+ fix2@(Fixity prec2 dir2)
+ e2
+ | 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_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( 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
+right_op_ok fix1 (OpApp _ _ fix2 _)
+ = not error_please && associate_right
+ where
+ (error_please, associate_right) = compareFixity fix1 fix2
+right_op_ok fix1 other
+ = True
-precParsePat pat = returnRn pat
+-- Parser initially makes negation bind more tightly than any other operator
+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
+\end{code}
+
+\begin{code}
+mkConOpPatRn :: RenamedPat -> Name -> Fixity -> RenamedPat
+ -> RnMS s RenamedPat
+mkConOpPatRn p1@(ConOpPatIn p11 op1 fix1 p12)
+ op2 fix2 p2
+ | nofix_error
+ = addErrRn (precParseErr (op1,fix1) (op2,fix2)) `thenRn_`
+ returnRn (ConOpPatIn p1 op2 fix2 p2)
-data INFIX = INFIXL | INFIXR | INFIXN deriving Eq
+ | rearrange_me
+ = mkConOpPatRn p12 op2 fix2 p2 `thenRn` \ new_p ->
+ returnRn (ConOpPatIn p11 op1 fix1 new_p)
-lookupFixity :: RnName -> RnM_Fixes s (INFIX, Int)
-lookupFixity op
- = getExtraRn `thenRn` \ fixity_fm ->
- case lookupUFM fixity_fm op of
- Nothing -> returnRn (INFIXL, 9)
- Just (InfixL _ n) -> returnRn (INFIXL, n)
- Just (InfixR _ n) -> returnRn (INFIXR, n)
- Just (InfixN _ n) -> returnRn (INFIXN, n)
+ where
+ (nofix_error, rearrange_me) = compareFixity fix1 fix2
+
+mkConOpPatRn p1@(NegPatIn neg_arg)
+ op2
+ fix2@(Fixity prec2 dir2)
+ p2
+ | prec2 > 6 -- Precedence of unary - is wired in as 6!
+ = addErrRn (precParseNegPatErr (op2,fix2)) `thenRn_`
+ returnRn (ConOpPatIn p1 op2 fix2 p2)
+
+mkConOpPatRn p1 op fix p2 -- Default case, no rearrangment
+ = ASSERT( not_op_pat p2 )
+ returnRn (ConOpPatIn p1 op fix p2)
+
+not_op_pat (ConOpPatIn _ _ _ _) = False
+not_op_pat other = True
\end{code}
\begin{code}
-checkPrecMatch :: Bool -> RnName -> RenamedMatch -> RnM_Fixes s ()
+checkPrecMatch :: Bool -> RdrName -> RdrNameMatch -> RnMS s ()
checkPrecMatch False fn match
= returnRn ()
checkPrecMatch True op _
= panic "checkPrecMatch"
-checkPrec op (ConOpPatIn _ op1 _) right
- = lookupFixity op `thenRn` \ (op_fix, op_prec) ->
- lookupFixity op1 `thenRn` \ (op1_fix, op1_prec) ->
- getSrcLocRn `thenRn` \ src_loc ->
+checkPrec op (ConOpPatIn _ op1 _ _) right
+ = lookupFixity op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
+ lookupFixity op1 `thenRn` \ op1_fix@(Fixity op1_prec op1_dir) ->
let
inf_ok = op1_prec > op_prec ||
(op1_prec == op_prec &&
- (op1_fix == INFIXR && op_fix == INFIXR && right ||
- op1_fix == INFIXL && op_fix == INFIXL && not right))
+ (op1_dir == InfixR && op_dir == InfixR && right ||
+ op1_dir == InfixL && op_dir == InfixL && not right))
- info = (op,op_fix,op_prec)
- info1 = (op1,op1_fix,op1_prec)
+ info = (op,op_fix)
+ info1 = (op1,op1_fix)
(infol, infor) = if right then (info, info1) else (info1, info)
in
- addErrIfRn (not inf_ok) (precParseErr infol infor src_loc)
+ checkRn inf_ok (precParseErr infol infor)
checkPrec op (NegPatIn _) right
- = lookupFixity op `thenRn` \ (op_fix, op_prec) ->
- getSrcLocRn `thenRn` \ src_loc ->
- addErrIfRn (6 < op_prec) (precParseNegPatErr (op,op_fix,op_prec) src_loc)
+ = lookupFixity op `thenRn` \ op_fix@(Fixity op_prec op_dir) ->
+ checkRn (op_prec <= 6) (precParseNegPatErr (op,op_fix))
checkPrec op pat right
= returnRn ()
\end{code}
+Consider
+ a `op1` b `op2` c
+
+(compareFixity op1 op2) tells which way to arrange appication, or
+whether there's an error.
+
+\begin{code}
+compareFixity :: Fixity -> Fixity
+ -> (Bool, -- Error please
+ Bool) -- Associate to the right: a op1 (b op2 c)
+compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
+ = case prec1 `compare` prec2 of
+ GT -> left
+ LT -> right
+ EQ -> case (dir1, dir2) of
+ (InfixR, InfixR) -> right
+ (InfixL, InfixL) -> left
+ _ -> error_please
+ where
+ right = (False, True)
+ left = (False, False)
+ error_please = (True, False)
+\end{code}
+
+%************************************************************************
+%* *
+\subsubsection{Literals}
+%* *
+%************************************************************************
+
+When literals occur we have to make sure that the types and classes they involve
+are made available.
+
+\begin{code}
+litOccurrence (HsChar _)
+ = addImplicitOccRn charType_name
+
+litOccurrence (HsCharPrim _)
+ = addImplicitOccRn (getName charPrimTyCon)
+
+litOccurrence (HsString _)
+ = addImplicitOccRn listType_name `thenRn_`
+ addImplicitOccRn charType_name
+
+litOccurrence (HsStringPrim _)
+ = addImplicitOccRn (getName addrPrimTyCon)
+
+litOccurrence (HsInt _)
+ = lookupImplicitOccRn numClass_RDR -- Int and Integer are forced in by Num
+
+litOccurrence (HsFrac _)
+ = 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)
+
+litOccurrence (HsFloatPrim _)
+ = addImplicitOccRn (getName floatPrimTyCon)
+
+litOccurrence (HsDoublePrim _)
+ = addImplicitOccRn (getName doublePrimTyCon)
+
+litOccurrence (HsLitLit _)
+ = 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}
+
+%************************************************************************
+%* *
+\subsubsection{Errors}
+%* *
+%************************************************************************
+
\begin{code}
-dupFieldErr str src_loc (dup:rest)
- = addShortErrLocLine src_loc (\ sty ->
- ppBesides [ppStr "duplicate field name `", ppr sty dup, ppStr "' in record ", ppStr str])
-
-negPatErr pat src_loc
- = addShortErrLocLine src_loc (\ sty ->
- ppSep [ppStr "prefix `-' not applied to literal in pattern", ppr sty pat])
-
-precParseNegPatErr op src_loc
- = addErrLoc src_loc "precedence parsing error" (\ sty ->
- ppBesides [ppStr "prefix `-' has lower precedence than ", pp_op sty op, ppStr " in pattern"])
-
-precParseErr op1 op2 src_loc
- = addErrLoc src_loc "precedence parsing error" (\ sty ->
- ppBesides [ppStr "cannot mix ", pp_op sty op1, ppStr " and ", pp_op sty op2,
- ppStr " in the same infix expression"])
-
-pp_op sty (op, fix, prec) = ppBesides [pprSym sty op, ppLparen, pp_fix fix, ppSP, ppInt prec, ppRparen]
-pp_fix INFIXL = ppStr "infixl"
-pp_fix INFIXR = ppStr "infixr"
-pp_fix INFIXN = ppStr "infix"
+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}