import HsTypes ( getTyVarName, pprClassAssertion, cmpHsTypes )
import RdrName ( RdrName, isRdrDataCon, rdrNameOcc, isRdrTyVar )
import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNameConDecl,
- extractHsTyRdrNames, extractRuleBndrsTyVars
+ extractRuleBndrsTyVars, extractHsTyRdrTyVars
)
import RnHsSyn
import HsCore
import RnBinds ( rnTopBinds, rnMethodBinds, renameSigs, unknownSigErr )
import RnEnv ( bindTyVarsRn, lookupBndrRn, lookupOccRn,
lookupImplicitOccRn,
- bindLocalsRn, bindLocalRn, bindLocalsFVRn,
+ bindLocalsRn, bindLocalRn, bindLocalsFVRn, bindUVarRn,
bindTyVarsFVRn, bindTyVarsFV2Rn, extendTyVarEnvFVRn,
bindCoreLocalFVRn, bindCoreLocalsFVRn,
checkDupOrQualNames, checkDupNames,
import OccName ( mkDefaultMethodOcc )
import BasicTypes ( TopLevelFlag(..) )
import FiniteMap ( elemFM )
-import PrelInfo ( derivingOccurrences, numClass_RDR,
- deRefStablePtr_NAME, makeStablePtr_NAME,
- bindIO_NAME
+import PrelInfo ( derivableClassKeys,
+ deRefStablePtr_NAME, makeStablePtr_NAME, bindIO_NAME
)
import Bag ( bagToList )
import List ( partition, nub )
import Outputable
import SrcLoc ( SrcLoc )
import CmdLineOpts ( opt_WarnUnusedMatches ) -- Warn of unused for-all'd tyvars
+import Unique ( Uniquable(..) )
import UniqFM ( lookupUFM )
import Maybes ( maybeToBool, catMaybes )
import Util
\end{code}
-rnDecl `renames' declarations.
+@rnDecl@ `renames' declarations.
It simultaneously performs dependency analysis and precedence parsing.
It also does the following error checks:
\begin{enumerate}
\item
Checks that all variable occurences are defined.
\item
-Checks the (..) etc constraints in the export list.
+Checks the @(..)@ etc constraints in the export list.
\end{enumerate}
Renaming type variables is a pain. Because they now contain uniques,
it is necessary to pass in an association list which maps a parsed
-tyvar to its Name representation. In some cases (type signatures of
-values), it is even necessary to go over the type first in order to
-get the set of tyvars used by it, make an assoc list, and then go over
-it again to rename the tyvars! However, we can also do some scoping
-checks at the same time.
+tyvar to its @Name@ representation.
+In some cases (type signatures of values),
+it is even necessary to go over the type first
+in order to get the set of tyvars used by it, make an assoc list,
+and then go over it again to rename the tyvars!
+However, we can also do some scoping checks at the same time.
\begin{code}
rnDecl (TyClD (TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc))
= pushSrcLocRn src_loc $
- lookupBndrRn tycon `thenRn` \ tycon' ->
- bindTyVarsFVRn data_doc tyvars $ \ tyvars' ->
- rnContext data_doc context `thenRn` \ (context', cxt_fvs) ->
- checkDupOrQualNames data_doc con_names `thenRn_`
- mapFvRn rnConDecl condecls `thenRn` \ (condecls', con_fvs) ->
- rnDerivs derivings `thenRn` \ (derivings', deriv_fvs) ->
+ lookupBndrRn tycon `thenRn` \ tycon' ->
+ bindTyVarsFVRn data_doc tyvars $ \ tyvars' ->
+ rnContext data_doc context `thenRn` \ (context', cxt_fvs) ->
+ checkDupOrQualNames data_doc con_names `thenRn_`
+ mapFvRn rnConDecl condecls `thenRn` \ (condecls', con_fvs) ->
+ rnDerivs derivings `thenRn` \ (derivings', deriv_fvs) ->
ASSERT(isNoDataPragmas pragmas)
- returnRn (TyClD (TyData new_or_data context' tycon' tyvars' condecls' derivings' noDataPragmas src_loc),
+ returnRn (TyClD (TyData new_or_data context' tycon' tyvars' condecls'
+ derivings' noDataPragmas src_loc),
cxt_fvs `plusFV` con_fvs `plusFV` deriv_fvs)
where
data_doc = text "the data type declaration for" <+> quotes (ppr tycon)
where
syn_doc = text "the declaration for type synonym" <+> quotes (ppr name)
-rnDecl (TyClD (ClassDecl context cname tyvars sigs mbinds pragmas tname dname snames src_loc))
+rnDecl (TyClD (ClassDecl context cname tyvars sigs mbinds pragmas
+ tname dname snames src_loc))
= pushSrcLocRn src_loc $
lookupBndrRn cname `thenRn` \ cname' ->
mapRn mkImportedGlobalFromRdrName snames `thenRn` \ snames' ->
-- Tyvars scope over bindings and context
- bindTyVarsFV2Rn cls_doc tyvars ( \ clas_tyvar_names tyvars' ->
+ bindTyVarsFV2Rn cls_doc tyvars ( \ clas_tyvar_names tyvars' ->
-- Check the superclasses
- rnContext cls_doc context `thenRn` \ (context', cxt_fvs) ->
+ rnContext cls_doc context `thenRn` \ (context', cxt_fvs) ->
-- Check the signatures
let
(fix_sigs, non_sigs) = partition isFixitySig non_op_sigs
in
checkDupOrQualNames sig_doc sig_rdr_names_w_locs `thenRn_`
- mapFvRn (rn_op cname' clas_tyvar_names) op_sigs `thenRn` \ (sigs', sig_fvs) ->
+ mapFvRn (rn_op cname' clas_tyvar_names) op_sigs
+ `thenRn` \ (sigs', sig_fvs) ->
mapRn_ (unknownSigErr) non_sigs `thenRn_`
let
binders = mkNameSet [ nm | (ClassOpSig nm _ _ _) <- sigs' ]
in
- renameSigs False binders lookupOccRn fix_sigs `thenRn` \ (fixs', fix_fvs) ->
+ renameSigs False binders lookupOccRn fix_sigs
+ `thenRn` \ (fixs', fix_fvs) ->
-- Check the methods
checkDupOrQualNames meth_doc meth_rdr_names_w_locs `thenRn_`
- rnMethodBinds mbinds `thenRn` \ (mbinds', meth_fvs) ->
+ rnMethodBinds mbinds
+ `thenRn` \ (mbinds', meth_fvs) ->
-- Typechecker is responsible for checking that we only
-- give default-method bindings for things in this class.
-- for instance decls.
ASSERT(isNoClassPragmas pragmas)
- returnRn (TyClD (ClassDecl context' cname' tyvars' (fixs' ++ sigs')
- mbinds' NoClassPragmas tname' dname' snames' src_loc),
+ returnRn (TyClD (ClassDecl context' cname' tyvars' (fixs' ++ sigs') mbinds'
+ NoClassPragmas tname' dname' snames' src_loc),
sig_fvs `plusFV`
fix_fvs `plusFV`
cxt_fvs `plusFV`
-- Check the signature
rnHsSigType (quotes (ppr op)) ty `thenRn` \ (new_ty, op_ty_fvs) ->
let
- check_in_op_ty clas_tyvar = checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
- (classTyVarNotInOpTyErr clas_tyvar sig)
+ check_in_op_ty clas_tyvar =
+ checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
+ (classTyVarNotInOpTyErr clas_tyvar sig)
in
mapRn_ check_in_op_ty clas_tyvars `thenRn_`
(case (mode, maybe_dm) of
(SourceMode, _)
| op `elem` meth_rdr_names
- -> -- Source class decl with an explicit method decl
- newImplicitBinder (mkDefaultMethodOcc (rdrNameOcc op)) locn `thenRn` \ dm_name ->
- returnRn (Just dm_name, emptyFVs)
+ -> -- Source class decl with an explicit method decl
+ newImplicitBinder (mkDefaultMethodOcc (rdrNameOcc op)) locn
+ `thenRn` \ dm_name ->
+ returnRn (Just dm_name, emptyFVs)
| otherwise
-> -- Source class dec, no explicit method decl
(InterfaceMode, Just dm_rdr_name)
-> -- Imported class that has a default method decl
-- See comments with tname, snames, above
- lookupImplicitOccRn dm_rdr_name `thenRn` \ dm_name ->
+ lookupImplicitOccRn dm_rdr_name `thenRn` \ dm_name ->
returnRn (Just dm_name, unitFV dm_name)
-- An imported class decl mentions, rather than defines,
-- the default method, so we must arrange to pull it in
\begin{code}
rnDecl (InstD (InstDecl inst_ty mbinds uprags dfun_rdr_name src_loc))
= pushSrcLocRn src_loc $
- rnHsSigType (text "an instance decl") inst_ty `thenRn` \ (inst_ty', inst_fvs) ->
+ rnHsSigType (text "an instance decl") inst_ty `thenRn` \ (inst_ty', inst_fvs) ->
let
inst_tyvars = case inst_ty' of
HsForAllTy (Just inst_tyvars) _ _ -> inst_tyvars
getModeRn `thenRn` \ mode ->
(case mode of
- InterfaceMode -> lookupImplicitOccRn dfun_rdr_name `thenRn` \ dfun_name ->
+ InterfaceMode -> lookupImplicitOccRn dfun_rdr_name `thenRn` \ dfun_name ->
returnRn (dfun_name, unitFV dfun_name)
- SourceMode -> newDFunName (getDFunKey inst_ty') src_loc `thenRn` \ dfun_name ->
+ SourceMode -> newDFunName (getDFunKey inst_ty') src_loc
+ `thenRn` \ dfun_name ->
returnRn (dfun_name, emptyFVs)
- ) `thenRn` \ (dfun_name, dfun_fv) ->
+ )
+ `thenRn` \ (dfun_name, dfun_fv) ->
- -- The typechecker checks that all the bindings are for the right class.
+ -- The typechecker checks that all the bindings are for the right class.
returnRn (InstD (InstDecl inst_ty' mbinds' new_uprags dfun_name src_loc),
inst_fvs `plusFV` meth_fvs `plusFV` prag_fvs `plusFV` dfun_fv)
where
rnDecl (DefD (DefaultDecl tys src_loc))
= pushSrcLocRn src_loc $
rnHsTypes doc_str tys `thenRn` \ (tys', fvs) ->
- lookupImplicitOccRn numClass_RDR `thenRn` \ num ->
- returnRn (DefD (DefaultDecl tys' src_loc), fvs `addOneFV` num)
+ returnRn (DefD (DefaultDecl tys' src_loc), fvs)
where
doc_str = text "a `default' declaration"
\end{code}
\begin{code}
rnDecl (ForD (ForeignDecl name imp_exp ty ext_nm cconv src_loc))
= pushSrcLocRn src_loc $
- lookupBndrRn name `thenRn` \ name' ->
+ lookupOccRn name `thenRn` \ name' ->
let
fvs1 = case imp_exp of
FoImport _ | not isDyn -> emptyFVs
FoExport | isDyn -> mkNameSet [makeStablePtr_NAME,
deRefStablePtr_NAME,
bindIO_NAME]
+ | otherwise -> mkNameSet [name']
_ -> emptyFVs
in
rnHsSigType fo_decl_msg ty `thenRn` \ (ty', fvs2) ->
rnDerivs Nothing -- derivs not specified
= returnRn (Nothing, emptyFVs)
-rnDerivs (Just ds)
- = mapFvRn rn_deriv ds `thenRn` \ (derivs, fvs) ->
- returnRn (Just derivs, fvs)
+rnDerivs (Just clss)
+ = mapRn do_one clss `thenRn` \ clss' ->
+ returnRn (Just clss', mkNameSet clss')
where
- rn_deriv clas
- = lookupOccRn clas `thenRn` \ clas_name ->
-
- -- Now add extra "occurrences" for things that
- -- the deriving mechanism will later need in order to
- -- generate code for this class.
- case lookupUFM derivingOccurrences clas_name of
- Nothing -> addErrRn (derivingNonStdClassErr clas_name) `thenRn_`
- returnRn (clas_name, unitFV clas_name)
-
- Just occs -> mapRn lookupImplicitOccRn occs `thenRn` \ names ->
- returnRn (clas_name, mkNameSet (clas_name : names))
+ do_one cls = lookupOccRn cls `thenRn` \ clas_name ->
+ checkRn (getUnique clas_name `elem` derivableClassKeys)
+ (derivingNonStdClassErr clas_name) `thenRn_`
+ returnRn clas_name
\end{code}
\begin{code}
= rnHsType (text "the type signature for" <+> doc_str) ty
rnForAll doc forall_tyvars ctxt ty
- = bindTyVarsFVRn doc forall_tyvars $ \ new_tyvars ->
- rnContext doc ctxt `thenRn` \ (new_ctxt, cxt_fvs) ->
- rnHsType doc ty `thenRn` \ (new_ty, ty_fvs) ->
+ = bindTyVarsFVRn doc forall_tyvars $ \ new_tyvars ->
+ rnContext doc ctxt `thenRn` \ (new_ctxt, cxt_fvs) ->
+ rnHsType doc ty `thenRn` \ (new_ty, ty_fvs) ->
returnRn (mkHsForAllTy new_tyvars new_ctxt new_ty,
cxt_fvs `plusFV` ty_fvs)
where
check ct@(_,tys)
| forall_mentioned = returnRn (Just ct)
- | otherwise = addErrRn (ctxtErr explicit_forall doc forall_tyvars ct ty) `thenRn_`
- returnRn Nothing
+ | otherwise = addErrRn (ctxtErr explicit_forall doc forall_tyvars ct ty)
+ `thenRn_` returnRn Nothing
where
- forall_mentioned = foldr ((||) . any (`elem` forall_tyvars) . extractHsTyRdrNames)
+ forall_mentioned = foldr ((||) . any (`elem` forall_tyvars) . extractHsTyRdrTyVars)
False
tys
-freeRdrTyVars :: RdrNameHsType -> [RdrName]
-freeRdrTyVars ty = filter isRdrTyVar (extractHsTyRdrNames ty)
-
rnHsType :: SDoc -> RdrNameHsType -> RnMS (RenamedHsType, FreeVars)
rnHsType doc (HsForAllTy Nothing ctxt ty)
-- over FV(T) \ {in-scope-tyvars}
= getLocalNameEnv `thenRn` \ name_env ->
let
- mentioned_in_tau = freeRdrTyVars ty
+ mentioned_in_tau = extractHsTyRdrTyVars ty
forall_tyvars = filter (not . (`elemFM` name_env)) mentioned_in_tau
in
checkConstraints False doc forall_tyvars ctxt ty `thenRn` \ ctxt' ->
-- That's only a warning... unless the tyvar is constrained by a
-- context in which case it's an error
= let
- mentioned_in_tau = freeRdrTyVars tau
+ mentioned_in_tau = extractHsTyRdrTyVars tau
mentioned_in_ctxt = nub [tv | (_,tys) <- ctxt,
ty <- tys,
- tv <- freeRdrTyVars ty]
+ tv <- extractHsTyRdrTyVars ty]
dubious_guys = filter (`notElem` mentioned_in_tau) forall_tyvar_names
-- dubious = explicitly quantified but not mentioned in tau type
rnHsTypes doc tys `thenRn` \ (tys', fvs) ->
returnRn (MonoDictTy clas' tys', fvs `addOneFV` clas')
+rnHsType doc (MonoUsgForAllTy uv_rdr ty)
+ = bindUVarRn doc uv_rdr $ \ uv_name ->
+ rnHsType doc ty `thenRn` \ (ty', fvs) ->
+ returnRn (MonoUsgForAllTy uv_name ty',
+ fvs )
+
rnHsType doc (MonoUsgTy usg ty)
- = rnHsType doc ty `thenRn` \ (ty', fvs) ->
- returnRn (MonoUsgTy usg ty', fvs)
+ = newUsg usg `thenRn` \ (usg', usg_fvs) ->
+ rnHsType doc ty `thenRn` \ (ty', ty_fvs) ->
+ returnRn (MonoUsgTy usg' ty',
+ usg_fvs `plusFV` ty_fvs)
+ where
+ newUsg usg = case usg of
+ MonoUsOnce -> returnRn (MonoUsOnce, emptyFVs)
+ MonoUsMany -> returnRn (MonoUsMany, emptyFVs)
+ MonoUsVar uv_rdr -> lookupOccRn uv_rdr `thenRn` \ uv_name ->
+ returnRn (MonoUsVar uv_name, emptyFVs)
rnHsTypes doc tys = mapFvRn (rnHsType doc) tys
\end{code}
%*********************************************************
-%* *
+%* *
\subsection{IdInfo}
-%* *
+%* *
%*********************************************************
\begin{code}
= lookupOccRn worker `thenRn` \ worker' ->
returnRn (HsWorker worker', unitFV worker')
-rnIdInfo (HsUnfold inline (Just expr)) = rnCoreExpr expr `thenRn` \ (expr', fvs) ->
+rnIdInfo (HsUnfold inline (Just expr)) = rnCoreExpr expr `thenRn` \ (expr', fvs) ->
returnRn (HsUnfold inline (Just expr'), fvs)
rnIdInfo (HsUnfold inline Nothing) = returnRn (HsUnfold inline Nothing, emptyFVs)
rnIdInfo (HsArity arity) = returnRn (HsArity arity, emptyFVs)
rnIdInfo (HsUpdate update) = returnRn (HsUpdate update, emptyFVs)
rnIdInfo (HsNoCafRefs) = returnRn (HsNoCafRefs, emptyFVs)
rnIdInfo (HsCprInfo cpr_info) = returnRn (HsCprInfo cpr_info, emptyFVs)
-rnIdInfo (HsSpecialise rule_body) = rnRuleBody rule_body `thenRn` \ (rule_body', fvs) ->
+rnIdInfo (HsSpecialise rule_body) = rnRuleBody rule_body
+ `thenRn` \ (rule_body', fvs) ->
returnRn (HsSpecialise rule_body', fvs)
rnRuleBody (UfRuleBody str vars args rhs)
returnRn (UfRuleBody str vars' args' rhs', fvs1 `plusFV` fvs2)
\end{code}
-UfCore expressions.
+@UfCore@ expressions.
\begin{code}
rnCoreExpr (UfType ty)
\end{code}
%*********************************************************
-%* *
+%* *
\subsection{Rule shapes}
-%* *
+%* *
%*********************************************************
Check the shape of a transformation rule LHS. Currently
-we only allow LHSs of the form (f e1 .. en), where f is
-not one of the forall'd variables.
+we only allow LHSs of the form @(f e1 .. en)@, where @f@ is
+not one of the @forall@'d variables.
\begin{code}
validRuleLhs foralls lhs
%*********************************************************
-%* *
+%* *
\subsection{Errors}
-%* *
+%* *
%*********************************************************
\begin{code}
(ptext SLIT("In") <+> doc))
ctxtErr explicit_forall doc tyvars constraint ty
- = sep [ptext SLIT("None of the type variable(s) in the constraint") <+> quotes (pprClassAssertion constraint),
+ = sep [ptext SLIT("None of the type variable(s) in the constraint")
+ <+> quotes (pprClassAssertion constraint),
if explicit_forall then
nest 4 (ptext SLIT("is universally quantified (i.e. bound by the forall)"))
else