2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnSource]{Main pass of renamer}
7 module RnSource ( rnDecl, rnSourceDecls, rnHsType, rnHsSigType ) where
9 #include "HsVersions.h"
14 import HsTypes ( getTyVarName, pprClassAssertion, cmpHsTypes )
15 import RdrName ( RdrName, isRdrDataCon, rdrNameOcc, isRdrTyVar )
16 import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNameConDecl,
17 extractHsTyRdrNames, extractRuleBndrsTyVars
22 import RnBinds ( rnTopBinds, rnMethodBinds, renameSigs, unknownSigErr )
23 import RnEnv ( bindTyVarsRn, lookupBndrRn, lookupOccRn,
25 bindLocalsRn, bindLocalRn, bindLocalsFVRn,
26 bindTyVarsFVRn, bindTyVarsFV2Rn, extendTyVarEnvFVRn,
27 bindCoreLocalFVRn, bindCoreLocalsFVRn,
28 checkDupOrQualNames, checkDupNames,
29 mkImportedGlobalName, mkImportedGlobalFromRdrName,
30 newDFunName, getDFunKey, newImplicitBinder,
31 FreeVars, emptyFVs, plusFV, plusFVs, unitFV, addOneFV, mapFvRn
35 import Name ( Name, OccName,
36 ExportFlag(..), Provenance(..),
37 nameOccName, NamedThing(..)
40 import OccName ( mkDefaultMethodOcc )
41 import BasicTypes ( TopLevelFlag(..) )
42 import FiniteMap ( elemFM )
43 import PrelInfo ( derivingOccurrences, numClass_RDR,
44 deRefStablePtr_NAME, makeStablePtr_NAME,
47 import Bag ( bagToList )
48 import List ( partition, nub )
50 import SrcLoc ( SrcLoc )
51 import CmdLineOpts ( opt_WarnUnusedMatches ) -- Warn of unused for-all'd tyvars
52 import UniqFM ( lookupUFM )
53 import Maybes ( maybeToBool, catMaybes )
57 @rnDecl@ `renames' declarations.
58 It simultaneously performs dependency analysis and precedence parsing.
59 It also does the following error checks:
62 Checks that tyvars are used properly. This includes checking
63 for undefined tyvars, and tyvars in contexts that are ambiguous.
65 Checks that all variable occurences are defined.
67 Checks the @(..)@ etc constraints in the export list.
71 %*********************************************************
73 \subsection{Value declarations}
75 %*********************************************************
78 rnSourceDecls :: [RdrNameHsDecl] -> RnMS ([RenamedHsDecl], FreeVars)
79 -- The decls get reversed, but that's ok
82 = go emptyFVs [] decls
84 -- Fixity decls have been dealt with already; ignore them
85 go fvs ds' [] = returnRn (ds', fvs)
86 go fvs ds' (FixD _:ds) = go fvs ds' ds
87 go fvs ds' (d:ds) = rnDecl d `thenRn` \(d', fvs') ->
88 go (fvs `plusFV` fvs') (d':ds') ds
92 %*********************************************************
94 \subsection{Value declarations}
96 %*********************************************************
99 -- rnDecl does all the work
100 rnDecl :: RdrNameHsDecl -> RnMS (RenamedHsDecl, FreeVars)
102 rnDecl (ValD binds) = rnTopBinds binds `thenRn` \ (new_binds, fvs) ->
103 returnRn (ValD new_binds, fvs)
106 rnDecl (SigD (IfaceSig name ty id_infos loc))
108 lookupBndrRn name `thenRn` \ name' ->
109 rnHsType doc_str ty `thenRn` \ (ty',fvs1) ->
110 mapFvRn rnIdInfo id_infos `thenRn` \ (id_infos', fvs2) ->
111 returnRn (SigD (IfaceSig name' ty' id_infos' loc), fvs1 `plusFV` fvs2)
113 doc_str = text "the interface signature for" <+> quotes (ppr name)
116 %*********************************************************
118 \subsection{Type declarations}
120 %*********************************************************
122 @rnTyDecl@ uses the `global name function' to create a new type
123 declaration in which local names have been replaced by their original
124 names, reporting any unknown names.
126 Renaming type variables is a pain. Because they now contain uniques,
127 it is necessary to pass in an association list which maps a parsed
128 tyvar to its @Name@ representation.
129 In some cases (type signatures of values),
130 it is even necessary to go over the type first
131 in order to get the set of tyvars used by it, make an assoc list,
132 and then go over it again to rename the tyvars!
133 However, we can also do some scoping checks at the same time.
136 rnDecl (TyClD (TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc))
137 = pushSrcLocRn src_loc $
138 lookupBndrRn tycon `thenRn` \ tycon' ->
139 bindTyVarsFVRn data_doc tyvars $ \ tyvars' ->
140 rnContext data_doc context `thenRn` \ (context', cxt_fvs) ->
141 checkDupOrQualNames data_doc con_names `thenRn_`
142 mapFvRn rnConDecl condecls `thenRn` \ (condecls', con_fvs) ->
143 rnDerivs derivings `thenRn` \ (derivings', deriv_fvs) ->
144 ASSERT(isNoDataPragmas pragmas)
145 returnRn (TyClD (TyData new_or_data context' tycon' tyvars' condecls'
146 derivings' noDataPragmas src_loc),
147 cxt_fvs `plusFV` con_fvs `plusFV` deriv_fvs)
149 data_doc = text "the data type declaration for" <+> quotes (ppr tycon)
150 con_names = map conDeclName condecls
152 rnDecl (TyClD (TySynonym name tyvars ty src_loc))
153 = pushSrcLocRn src_loc $
154 lookupBndrRn name `thenRn` \ name' ->
155 bindTyVarsFVRn syn_doc tyvars $ \ tyvars' ->
156 rnHsType syn_doc ty `thenRn` \ (ty', ty_fvs) ->
157 returnRn (TyClD (TySynonym name' tyvars' ty' src_loc), ty_fvs)
159 syn_doc = text "the declaration for type synonym" <+> quotes (ppr name)
161 rnDecl (TyClD (ClassDecl context cname tyvars sigs mbinds pragmas
162 tname dname snames src_loc))
163 = pushSrcLocRn src_loc $
165 lookupBndrRn cname `thenRn` \ cname' ->
167 -- Deal with the implicit tycon and datacon name
168 -- They aren't in scope (because they aren't visible to the user)
169 -- and what we want to do is simply look them up in the cache;
170 -- we jolly well ought to get a 'hit' there!
171 -- So the 'Imported' part of this call is not relevant.
172 -- Unclean; but since these two are the only place this happens
173 -- I can't work up the energy to do it more beautifully
174 mkImportedGlobalFromRdrName tname `thenRn` \ tname' ->
175 mkImportedGlobalFromRdrName dname `thenRn` \ dname' ->
176 mapRn mkImportedGlobalFromRdrName snames `thenRn` \ snames' ->
178 -- Tyvars scope over bindings and context
179 bindTyVarsFV2Rn cls_doc tyvars ( \ clas_tyvar_names tyvars' ->
181 -- Check the superclasses
182 rnContext cls_doc context `thenRn` \ (context', cxt_fvs) ->
184 -- Check the signatures
186 -- First process the class op sigs, then the fixity sigs.
187 (op_sigs, non_op_sigs) = partition isClassOpSig sigs
188 (fix_sigs, non_sigs) = partition isFixitySig non_op_sigs
190 checkDupOrQualNames sig_doc sig_rdr_names_w_locs `thenRn_`
191 mapFvRn (rn_op cname' clas_tyvar_names) op_sigs
192 `thenRn` \ (sigs', sig_fvs) ->
193 mapRn_ (unknownSigErr) non_sigs `thenRn_`
195 binders = mkNameSet [ nm | (ClassOpSig nm _ _ _) <- sigs' ]
197 renameSigs False binders lookupOccRn fix_sigs
198 `thenRn` \ (fixs', fix_fvs) ->
201 checkDupOrQualNames meth_doc meth_rdr_names_w_locs `thenRn_`
203 `thenRn` \ (mbinds', meth_fvs) ->
205 -- Typechecker is responsible for checking that we only
206 -- give default-method bindings for things in this class.
207 -- The renamer *could* check this for class decls, but can't
208 -- for instance decls.
210 ASSERT(isNoClassPragmas pragmas)
211 returnRn (TyClD (ClassDecl context' cname' tyvars' (fixs' ++ sigs') mbinds'
212 NoClassPragmas tname' dname' snames' src_loc),
220 cls_doc = text "the declaration for class" <+> ppr cname
221 sig_doc = text "the signatures for class" <+> ppr cname
222 meth_doc = text "the default-methods for class" <+> ppr cname
224 sig_rdr_names_w_locs = [(op,locn) | ClassOpSig op _ _ locn <- sigs]
225 meth_rdr_names_w_locs = bagToList (collectMonoBinders mbinds)
226 meth_rdr_names = map fst meth_rdr_names_w_locs
228 rn_op clas clas_tyvars sig@(ClassOpSig op maybe_dm ty locn)
229 = pushSrcLocRn locn $
230 lookupBndrRn op `thenRn` \ op_name ->
232 -- Check the signature
233 rnHsSigType (quotes (ppr op)) ty `thenRn` \ (new_ty, op_ty_fvs) ->
235 check_in_op_ty clas_tyvar =
236 checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
237 (classTyVarNotInOpTyErr clas_tyvar sig)
239 mapRn_ check_in_op_ty clas_tyvars `thenRn_`
241 -- Make the default-method name
242 getModeRn `thenRn` \ mode ->
243 (case (mode, maybe_dm) of
245 | op `elem` meth_rdr_names
246 -> -- Source class decl with an explicit method decl
247 newImplicitBinder (mkDefaultMethodOcc (rdrNameOcc op)) locn
248 `thenRn` \ dm_name ->
249 returnRn (Just dm_name, emptyFVs)
252 -> -- Source class dec, no explicit method decl
253 returnRn (Nothing, emptyFVs)
255 (InterfaceMode, Just dm_rdr_name)
256 -> -- Imported class that has a default method decl
257 -- See comments with tname, snames, above
258 lookupImplicitOccRn dm_rdr_name `thenRn` \ dm_name ->
259 returnRn (Just dm_name, unitFV dm_name)
260 -- An imported class decl mentions, rather than defines,
261 -- the default method, so we must arrange to pull it in
263 (InterfaceMode, Nothing)
264 -- Imported class with no default metho
265 -> returnRn (Nothing, emptyFVs)
266 ) `thenRn` \ (maybe_dm_name, dm_fvs) ->
268 returnRn (ClassOpSig op_name maybe_dm_name new_ty locn, op_ty_fvs `plusFV` dm_fvs)
272 %*********************************************************
274 \subsection{Instance declarations}
276 %*********************************************************
279 rnDecl (InstD (InstDecl inst_ty mbinds uprags dfun_rdr_name src_loc))
280 = pushSrcLocRn src_loc $
281 rnHsSigType (text "an instance decl") inst_ty `thenRn` \ (inst_ty', inst_fvs) ->
283 inst_tyvars = case inst_ty' of
284 HsForAllTy (Just inst_tyvars) _ _ -> inst_tyvars
286 -- (Slightly strangely) the forall-d tyvars scope over
287 -- the method bindings too
290 -- Rename the bindings
291 -- NB meth_names can be qualified!
292 checkDupNames meth_doc meth_names `thenRn_`
293 extendTyVarEnvFVRn inst_tyvars (
295 ) `thenRn` \ (mbinds', meth_fvs) ->
297 binders = mkNameSet (map fst (bagToList (collectMonoBinders mbinds')))
299 -- Delete sigs (&report) sigs that aren't allowed inside an
305 (ok_sigs, not_ok_idecl_sigs) = partition okInInstDecl uprags
307 okInInstDecl (FixSig _) = False
308 okInInstDecl (Sig _ _ _) = False
309 okInInstDecl _ = True
312 -- You can't have fixity decls & type signatures
313 -- within an instance declaration.
314 mapRn_ unknownSigErr not_ok_idecl_sigs `thenRn_`
316 -- Rename the prags and signatures.
317 -- Note that the type variables are not in scope here,
318 -- so that instance Eq a => Eq (T a) where
319 -- {-# SPECIALISE instance Eq a => Eq (T [a]) #-}
321 renameSigs False binders lookupOccRn ok_sigs `thenRn` \ (new_uprags, prag_fvs) ->
323 getModeRn `thenRn` \ mode ->
325 InterfaceMode -> lookupImplicitOccRn dfun_rdr_name `thenRn` \ dfun_name ->
326 returnRn (dfun_name, unitFV dfun_name)
327 SourceMode -> newDFunName (getDFunKey inst_ty') src_loc
328 `thenRn` \ dfun_name ->
329 returnRn (dfun_name, emptyFVs)
331 `thenRn` \ (dfun_name, dfun_fv) ->
333 -- The typechecker checks that all the bindings are for the right class.
334 returnRn (InstD (InstDecl inst_ty' mbinds' new_uprags dfun_name src_loc),
335 inst_fvs `plusFV` meth_fvs `plusFV` prag_fvs `plusFV` dfun_fv)
337 meth_doc = text "the bindings in an instance declaration"
338 meth_names = bagToList (collectMonoBinders mbinds)
341 %*********************************************************
343 \subsection{Default declarations}
345 %*********************************************************
348 rnDecl (DefD (DefaultDecl tys src_loc))
349 = pushSrcLocRn src_loc $
350 rnHsTypes doc_str tys `thenRn` \ (tys', fvs) ->
351 lookupImplicitOccRn numClass_RDR `thenRn` \ num ->
352 returnRn (DefD (DefaultDecl tys' src_loc), fvs `addOneFV` num)
354 doc_str = text "a `default' declaration"
357 %*********************************************************
359 \subsection{Foreign declarations}
361 %*********************************************************
364 rnDecl (ForD (ForeignDecl name imp_exp ty ext_nm cconv src_loc))
365 = pushSrcLocRn src_loc $
366 lookupBndrRn name `thenRn` \ name' ->
368 fvs1 = case imp_exp of
369 FoImport _ | not isDyn -> emptyFVs
371 FoExport | isDyn -> mkNameSet [makeStablePtr_NAME,
376 rnHsSigType fo_decl_msg ty `thenRn` \ (ty', fvs2) ->
377 returnRn (ForD (ForeignDecl name' imp_exp ty' ext_nm cconv src_loc),
380 fo_decl_msg = ptext SLIT("a foreign declaration")
381 isDyn = isDynamic ext_nm
384 %*********************************************************
388 %*********************************************************
391 rnDecl (RuleD (IfaceRuleDecl var body src_loc))
392 = pushSrcLocRn src_loc $
393 lookupOccRn var `thenRn` \ var' ->
394 rnRuleBody body `thenRn` \ (body', fvs) ->
395 returnRn (RuleD (IfaceRuleDecl var' body' src_loc), fvs `addOneFV` var')
397 rnDecl (RuleD (RuleDecl rule_name tvs vars lhs rhs src_loc))
399 pushSrcLocRn src_loc $
401 bindTyVarsFV2Rn doc (map UserTyVar sig_tvs) $ \ sig_tvs' _ ->
402 bindLocalsFVRn doc (map get_var vars) $ \ ids ->
403 mapFvRn rn_var (vars `zip` ids) `thenRn` \ (vars', fv_vars) ->
405 rnExpr lhs `thenRn` \ (lhs', fv_lhs) ->
406 rnExpr rhs `thenRn` \ (rhs', fv_rhs) ->
407 checkRn (validRuleLhs ids lhs')
408 (badRuleLhsErr rule_name lhs') `thenRn_`
410 bad_vars = [var | var <- ids, not (var `elemNameSet` fv_lhs)]
412 mapRn (addErrRn . badRuleVar rule_name) bad_vars `thenRn_`
413 returnRn (RuleD (RuleDecl rule_name sig_tvs' vars' lhs' rhs' src_loc),
414 fv_vars `plusFV` fv_lhs `plusFV` fv_rhs)
416 doc = text "the transformation rule" <+> ptext rule_name
417 sig_tvs = extractRuleBndrsTyVars vars
419 get_var (RuleBndr v) = v
420 get_var (RuleBndrSig v _) = v
422 rn_var (RuleBndr v, id) = returnRn (RuleBndr id, emptyFVs)
423 rn_var (RuleBndrSig v t, id) = rnHsType doc t `thenRn` \ (t', fvs) ->
424 returnRn (RuleBndrSig id t', fvs)
428 %*********************************************************
430 \subsection{Support code for type/data declarations}
432 %*********************************************************
435 rnDerivs :: Maybe [RdrName] -> RnMS (Maybe [Name], FreeVars)
437 rnDerivs Nothing -- derivs not specified
438 = returnRn (Nothing, emptyFVs)
441 = mapFvRn rn_deriv ds `thenRn` \ (derivs, fvs) ->
442 returnRn (Just derivs, fvs)
445 = lookupOccRn clas `thenRn` \ clas_name ->
447 -- Now add extra "occurrences" for things that
448 -- the deriving mechanism will later need in order to
449 -- generate code for this class.
450 case lookupUFM derivingOccurrences clas_name of
451 Nothing -> addErrRn (derivingNonStdClassErr clas_name) `thenRn_`
452 returnRn (clas_name, unitFV clas_name)
454 Just occs -> mapRn lookupImplicitOccRn occs `thenRn` \ names ->
455 returnRn (clas_name, mkNameSet (clas_name : names))
459 conDeclName :: RdrNameConDecl -> (RdrName, SrcLoc)
460 conDeclName (ConDecl n _ _ _ l) = (n,l)
462 rnConDecl :: RdrNameConDecl -> RnMS (RenamedConDecl, FreeVars)
463 rnConDecl (ConDecl name tvs cxt details locn)
464 = pushSrcLocRn locn $
465 checkConName name `thenRn_`
466 lookupBndrRn name `thenRn` \ new_name ->
467 bindTyVarsFVRn doc tvs $ \ new_tyvars ->
468 rnContext doc cxt `thenRn` \ (new_context, cxt_fvs) ->
469 rnConDetails doc locn details `thenRn` \ (new_details, det_fvs) ->
470 returnRn (ConDecl new_name new_tyvars new_context new_details locn,
471 cxt_fvs `plusFV` det_fvs)
473 doc = text "the definition of data constructor" <+> quotes (ppr name)
475 rnConDetails doc locn (VanillaCon tys)
476 = mapFvRn (rnBangTy doc) tys `thenRn` \ (new_tys, fvs) ->
477 returnRn (VanillaCon new_tys, fvs)
479 rnConDetails doc locn (InfixCon ty1 ty2)
480 = rnBangTy doc ty1 `thenRn` \ (new_ty1, fvs1) ->
481 rnBangTy doc ty2 `thenRn` \ (new_ty2, fvs2) ->
482 returnRn (InfixCon new_ty1 new_ty2, fvs1 `plusFV` fvs2)
484 rnConDetails doc locn (NewCon ty mb_field)
485 = rnHsType doc ty `thenRn` \ (new_ty, fvs) ->
486 rn_field mb_field `thenRn` \ new_mb_field ->
487 returnRn (NewCon new_ty new_mb_field, fvs)
489 rn_field Nothing = returnRn Nothing
491 lookupBndrRn f `thenRn` \ new_f ->
492 returnRn (Just new_f)
494 rnConDetails doc locn (RecCon fields)
495 = checkDupOrQualNames doc field_names `thenRn_`
496 mapFvRn (rnField doc) fields `thenRn` \ (new_fields, fvs) ->
497 returnRn (RecCon new_fields, fvs)
499 field_names = [(fld, locn) | (flds, _) <- fields, fld <- flds]
501 rnField doc (names, ty)
502 = mapRn lookupBndrRn names `thenRn` \ new_names ->
503 rnBangTy doc ty `thenRn` \ (new_ty, fvs) ->
504 returnRn ((new_names, new_ty), fvs)
506 rnBangTy doc (Banged ty)
507 = rnHsType doc ty `thenRn` \ (new_ty, fvs) ->
508 returnRn (Banged new_ty, fvs)
510 rnBangTy doc (Unbanged ty)
511 = rnHsType doc ty `thenRn` \ (new_ty, fvs) ->
512 returnRn (Unbanged new_ty, fvs)
514 rnBangTy doc (Unpacked ty)
515 = rnHsType doc ty `thenRn` \ (new_ty, fvs) ->
516 returnRn (Unpacked new_ty, fvs)
518 -- This data decl will parse OK
520 -- treating "a" as the constructor.
521 -- It is really hard to make the parser spot this malformation.
522 -- So the renamer has to check that the constructor is legal
524 -- We can get an operator as the constructor, even in the prefix form:
525 -- data T = :% Int Int
526 -- from interface files, which always print in prefix form
529 = checkRn (isRdrDataCon name)
534 %*********************************************************
536 \subsection{Support code to rename types}
538 %*********************************************************
541 rnHsSigType :: SDoc -> RdrNameHsType -> RnMS (RenamedHsType, FreeVars)
542 -- rnHsSigType is used for source-language type signatures,
543 -- which use *implicit* universal quantification.
544 rnHsSigType doc_str ty
545 = rnHsType (text "the type signature for" <+> doc_str) ty
547 rnForAll doc forall_tyvars ctxt ty
548 = bindTyVarsFVRn doc forall_tyvars $ \ new_tyvars ->
549 rnContext doc ctxt `thenRn` \ (new_ctxt, cxt_fvs) ->
550 rnHsType doc ty `thenRn` \ (new_ty, ty_fvs) ->
551 returnRn (mkHsForAllTy new_tyvars new_ctxt new_ty,
552 cxt_fvs `plusFV` ty_fvs)
554 -- Check that each constraint mentions at least one of the forall'd type variables
555 -- Since the forall'd type variables are a subset of the free tyvars
556 -- of the tau-type part, this guarantees that every constraint mentions
557 -- at least one of the free tyvars in ty
558 checkConstraints explicit_forall doc forall_tyvars ctxt ty
559 = mapRn check ctxt `thenRn` \ maybe_ctxt' ->
560 returnRn (catMaybes maybe_ctxt')
561 -- Remove problem ones, to avoid duplicate error message.
564 | forall_mentioned = returnRn (Just ct)
565 | otherwise = addErrRn (ctxtErr explicit_forall doc forall_tyvars ct ty)
566 `thenRn_` returnRn Nothing
568 forall_mentioned = foldr ((||) . any (`elem` forall_tyvars) . extractHsTyRdrNames)
572 freeRdrTyVars :: RdrNameHsType -> [RdrName]
573 freeRdrTyVars ty = filter isRdrTyVar (extractHsTyRdrNames ty)
575 rnHsType :: SDoc -> RdrNameHsType -> RnMS (RenamedHsType, FreeVars)
577 rnHsType doc (HsForAllTy Nothing ctxt ty)
578 -- From source code (no kinds on tyvars)
579 -- Given the signature C => T we universally quantify
580 -- over FV(T) \ {in-scope-tyvars}
581 = getLocalNameEnv `thenRn` \ name_env ->
583 mentioned_in_tau = freeRdrTyVars ty
584 forall_tyvars = filter (not . (`elemFM` name_env)) mentioned_in_tau
586 checkConstraints False doc forall_tyvars ctxt ty `thenRn` \ ctxt' ->
587 rnForAll doc (map UserTyVar forall_tyvars) ctxt' ty
589 rnHsType doc (HsForAllTy (Just forall_tyvars) ctxt tau)
590 -- Explicit quantification.
591 -- Check that the forall'd tyvars are a subset of the
592 -- free tyvars in the tau-type part
593 -- That's only a warning... unless the tyvar is constrained by a
594 -- context in which case it's an error
596 mentioned_in_tau = freeRdrTyVars tau
597 mentioned_in_ctxt = nub [tv | (_,tys) <- ctxt,
599 tv <- freeRdrTyVars ty]
601 dubious_guys = filter (`notElem` mentioned_in_tau) forall_tyvar_names
602 -- dubious = explicitly quantified but not mentioned in tau type
604 (bad_guys, warn_guys) = partition (`elem` mentioned_in_ctxt) dubious_guys
605 -- bad = explicitly quantified and constrained, but not mentioned in tau
606 -- warn = explicitly quantified but not mentioned in ctxt or tau
608 forall_tyvar_names = map getTyVarName forall_tyvars
610 mapRn_ (forAllErr doc tau) bad_guys `thenRn_`
611 mapRn_ (forAllWarn doc tau) warn_guys `thenRn_`
612 checkConstraints True doc forall_tyvar_names ctxt tau `thenRn` \ ctxt' ->
613 rnForAll doc forall_tyvars ctxt' tau
615 rnHsType doc (MonoTyVar tyvar)
616 = lookupOccRn tyvar `thenRn` \ tyvar' ->
617 returnRn (MonoTyVar tyvar', unitFV tyvar')
619 rnHsType doc (MonoFunTy ty1 ty2)
620 = rnHsType doc ty1 `thenRn` \ (ty1', fvs1) ->
621 rnHsType doc ty2 `thenRn` \ (ty2', fvs2) ->
622 returnRn (MonoFunTy ty1' ty2', fvs1 `plusFV` fvs2)
624 rnHsType doc (MonoListTy ty)
625 = rnHsType doc ty `thenRn` \ (ty', fvs) ->
626 returnRn (MonoListTy ty', fvs `addOneFV` listTyCon_name)
628 rnHsType doc (MonoTupleTy tys boxed)
629 = rnHsTypes doc tys `thenRn` \ (tys', fvs) ->
630 returnRn (MonoTupleTy tys' boxed, fvs `addOneFV` tup_con_name)
632 tup_con_name = tupleTyCon_name boxed (length tys)
634 rnHsType doc (MonoTyApp ty1 ty2)
635 = rnHsType doc ty1 `thenRn` \ (ty1', fvs1) ->
636 rnHsType doc ty2 `thenRn` \ (ty2', fvs2) ->
637 returnRn (MonoTyApp ty1' ty2', fvs1 `plusFV` fvs2)
639 rnHsType doc (MonoDictTy clas tys)
640 = lookupOccRn clas `thenRn` \ clas' ->
641 rnHsTypes doc tys `thenRn` \ (tys', fvs) ->
642 returnRn (MonoDictTy clas' tys', fvs `addOneFV` clas')
644 rnHsType doc (MonoUsgTy usg ty)
645 = rnHsType doc ty `thenRn` \ (ty', fvs) ->
646 returnRn (MonoUsgTy usg ty', fvs)
648 rnHsTypes doc tys = mapFvRn (rnHsType doc) tys
653 rnContext :: SDoc -> RdrNameContext -> RnMS (RenamedContext, FreeVars)
656 = mapAndUnzipRn rn_ctxt ctxt `thenRn` \ (theta, fvs_s) ->
658 (_, dup_asserts) = removeDups cmp_assert theta
660 -- Check for duplicate assertions
661 -- If this isn't an error, then it ought to be:
662 mapRn_ (addWarnRn . dupClassAssertWarn theta) dup_asserts `thenRn_`
664 returnRn (theta, plusFVs fvs_s)
667 = lookupOccRn clas `thenRn` \ clas_name ->
668 rnHsTypes doc tys `thenRn` \ (tys', fvs) ->
669 returnRn ((clas_name, tys'), fvs `addOneFV` clas_name)
671 cmp_assert (c1,tys1) (c2,tys2)
672 = (c1 `compare` c2) `thenCmp` (cmpHsTypes compare tys1 tys2)
676 %*********************************************************
680 %*********************************************************
683 rnIdInfo (HsStrictness str) = returnRn (HsStrictness str, emptyFVs)
685 rnIdInfo (HsWorker worker)
686 = lookupOccRn worker `thenRn` \ worker' ->
687 returnRn (HsWorker worker', unitFV worker')
689 rnIdInfo (HsUnfold inline (Just expr)) = rnCoreExpr expr `thenRn` \ (expr', fvs) ->
690 returnRn (HsUnfold inline (Just expr'), fvs)
691 rnIdInfo (HsUnfold inline Nothing) = returnRn (HsUnfold inline Nothing, emptyFVs)
692 rnIdInfo (HsArity arity) = returnRn (HsArity arity, emptyFVs)
693 rnIdInfo (HsUpdate update) = returnRn (HsUpdate update, emptyFVs)
694 rnIdInfo (HsNoCafRefs) = returnRn (HsNoCafRefs, emptyFVs)
695 rnIdInfo (HsCprInfo cpr_info) = returnRn (HsCprInfo cpr_info, emptyFVs)
696 rnIdInfo (HsSpecialise rule_body) = rnRuleBody rule_body
697 `thenRn` \ (rule_body', fvs) ->
698 returnRn (HsSpecialise rule_body', fvs)
700 rnRuleBody (UfRuleBody str vars args rhs)
701 = rnCoreBndrs vars $ \ vars' ->
702 mapFvRn rnCoreExpr args `thenRn` \ (args', fvs1) ->
703 rnCoreExpr rhs `thenRn` \ (rhs', fvs2) ->
704 returnRn (UfRuleBody str vars' args' rhs', fvs1 `plusFV` fvs2)
707 @UfCore@ expressions.
710 rnCoreExpr (UfType ty)
711 = rnHsType (text "unfolding type") ty `thenRn` \ (ty', fvs) ->
712 returnRn (UfType ty', fvs)
715 = lookupOccRn v `thenRn` \ v' ->
716 returnRn (UfVar v', unitFV v')
718 rnCoreExpr (UfCon con args)
719 = rnUfCon con `thenRn` \ (con', fvs1) ->
720 mapFvRn rnCoreExpr args `thenRn` \ (args', fvs2) ->
721 returnRn (UfCon con' args', fvs1 `plusFV` fvs2)
723 rnCoreExpr (UfTuple con args)
724 = lookupOccRn con `thenRn` \ con' ->
725 mapFvRn rnCoreExpr args `thenRn` \ (args', fvs) ->
726 returnRn (UfTuple con' args', fvs `addOneFV` con')
728 rnCoreExpr (UfApp fun arg)
729 = rnCoreExpr fun `thenRn` \ (fun', fv1) ->
730 rnCoreExpr arg `thenRn` \ (arg', fv2) ->
731 returnRn (UfApp fun' arg', fv1 `plusFV` fv2)
733 rnCoreExpr (UfCase scrut bndr alts)
734 = rnCoreExpr scrut `thenRn` \ (scrut', fvs1) ->
735 bindCoreLocalFVRn bndr ( \ bndr' ->
736 mapFvRn rnCoreAlt alts `thenRn` \ (alts', fvs2) ->
737 returnRn (UfCase scrut' bndr' alts', fvs2)
738 ) `thenRn` \ (case', fvs3) ->
739 returnRn (case', fvs1 `plusFV` fvs3)
741 rnCoreExpr (UfNote note expr)
742 = rnNote note `thenRn` \ (note', fvs1) ->
743 rnCoreExpr expr `thenRn` \ (expr', fvs2) ->
744 returnRn (UfNote note' expr', fvs1 `plusFV` fvs2)
746 rnCoreExpr (UfLam bndr body)
747 = rnCoreBndr bndr $ \ bndr' ->
748 rnCoreExpr body `thenRn` \ (body', fvs) ->
749 returnRn (UfLam bndr' body', fvs)
751 rnCoreExpr (UfLet (UfNonRec bndr rhs) body)
752 = rnCoreExpr rhs `thenRn` \ (rhs', fvs1) ->
753 rnCoreBndr bndr ( \ bndr' ->
754 rnCoreExpr body `thenRn` \ (body', fvs2) ->
755 returnRn (UfLet (UfNonRec bndr' rhs') body', fvs2)
756 ) `thenRn` \ (result, fvs3) ->
757 returnRn (result, fvs1 `plusFV` fvs3)
759 rnCoreExpr (UfLet (UfRec pairs) body)
760 = rnCoreBndrs bndrs $ \ bndrs' ->
761 mapFvRn rnCoreExpr rhss `thenRn` \ (rhss', fvs1) ->
762 rnCoreExpr body `thenRn` \ (body', fvs2) ->
763 returnRn (UfLet (UfRec (bndrs' `zip` rhss')) body', fvs1 `plusFV` fvs2)
765 (bndrs, rhss) = unzip pairs
769 rnCoreBndr (UfValBinder name ty) thing_inside
770 = rnHsType doc ty `thenRn` \ (ty', fvs1) ->
771 bindCoreLocalFVRn name ( \ name' ->
772 thing_inside (UfValBinder name' ty')
773 ) `thenRn` \ (result, fvs2) ->
774 returnRn (result, fvs1 `plusFV` fvs2)
776 doc = text "unfolding id"
778 rnCoreBndr (UfTyBinder name kind) thing_inside
779 = bindCoreLocalFVRn name $ \ name' ->
780 thing_inside (UfTyBinder name' kind)
782 rnCoreBndrs [] thing_inside = thing_inside []
783 rnCoreBndrs (b:bs) thing_inside = rnCoreBndr b $ \ name' ->
784 rnCoreBndrs bs $ \ names' ->
785 thing_inside (name':names')
789 rnCoreAlt (con, bndrs, rhs)
790 = rnUfCon con `thenRn` \ (con', fvs1) ->
791 bindCoreLocalsFVRn bndrs ( \ bndrs' ->
792 rnCoreExpr rhs `thenRn` \ (rhs', fvs2) ->
793 returnRn ((con', bndrs', rhs'), fvs2)
794 ) `thenRn` \ (result, fvs3) ->
795 returnRn (result, fvs1 `plusFV` fvs3)
798 = rnHsType (text "unfolding coerce") ty `thenRn` \ (ty', fvs) ->
799 returnRn (UfCoerce ty', fvs)
801 rnNote (UfSCC cc) = returnRn (UfSCC cc, emptyFVs)
802 rnNote UfInlineCall = returnRn (UfInlineCall, emptyFVs)
803 rnNote UfInlineMe = returnRn (UfInlineMe, emptyFVs)
807 = returnRn (UfDefault, emptyFVs)
809 rnUfCon (UfDataCon con)
810 = lookupOccRn con `thenRn` \ con' ->
811 returnRn (UfDataCon con', unitFV con')
813 rnUfCon (UfLitCon lit)
814 = returnRn (UfLitCon lit, emptyFVs)
816 rnUfCon (UfLitLitCon lit ty)
817 = rnHsType (text "litlit") ty `thenRn` \ (ty', fvs) ->
818 returnRn (UfLitLitCon lit ty', fvs)
820 rnUfCon (UfPrimOp op)
821 = lookupOccRn op `thenRn` \ op' ->
822 returnRn (UfPrimOp op', emptyFVs)
824 rnUfCon (UfCCallOp str is_dyn casm gc)
825 = returnRn (UfCCallOp str is_dyn casm gc, emptyFVs)
828 %*********************************************************
830 \subsection{Rule shapes}
832 %*********************************************************
834 Check the shape of a transformation rule LHS. Currently
835 we only allow LHSs of the form @(f e1 .. en)@, where @f@ is
836 not one of the @forall@'d variables.
839 validRuleLhs foralls lhs
842 check (HsApp e1 e2) = check e1
843 check (HsVar v) | v `notElem` foralls = True
848 %*********************************************************
852 %*********************************************************
855 derivingNonStdClassErr clas
856 = hsep [ptext SLIT("non-standard class"), ppr clas, ptext SLIT("in deriving clause")]
858 classTyVarNotInOpTyErr clas_tyvar sig
859 = hang (hsep [ptext SLIT("Class type variable"),
860 quotes (ppr clas_tyvar),
861 ptext SLIT("does not appear in method signature")])
864 dupClassAssertWarn ctxt (assertion : dups)
865 = sep [hsep [ptext SLIT("Duplicate class assertion"),
866 quotes (pprClassAssertion assertion),
867 ptext SLIT("in the context:")],
868 nest 4 (pprContext ctxt <+> ptext SLIT("..."))]
871 = hsep [ptext SLIT("Illegal data constructor name"), quotes (ppr name)]
873 forAllWarn doc ty tyvar
874 | not opt_WarnUnusedMatches = returnRn ()
876 = getModeRn `thenRn` \ mode ->
879 InterfaceMode -> returnRn () ; -- Don't warn of unused tyvars in interface files
880 -- unless DEBUG is on, in which case it is slightly
881 -- informative. They can arise from mkRhsTyLam,
882 #endif -- leading to (say) f :: forall a b. [b] -> [b]
886 sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
887 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
889 (ptext SLIT("In") <+> doc))
892 forAllErr doc ty tyvar
894 sep [ptext SLIT("The constrained type variable") <+> quotes (ppr tyvar),
895 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
897 (ptext SLIT("In") <+> doc))
899 ctxtErr explicit_forall doc tyvars constraint ty
900 = sep [ptext SLIT("None of the type variable(s) in the constraint")
901 <+> quotes (pprClassAssertion constraint),
902 if explicit_forall then
903 nest 4 (ptext SLIT("is universally quantified (i.e. bound by the forall)"))
905 nest 4 (ptext SLIT("appears in the type") <+> quotes (ppr ty))
908 (ptext SLIT("In") <+> doc)
910 badRuleLhsErr name lhs
911 = sep [ptext SLIT("Rule") <+> ptext name <> colon,
912 nest 4 (ptext SLIT("Illegal left-hand side:") <+> ppr lhs)]
914 ptext SLIT("LHS must be of form (f e1 .. en) where f is not forall'd")
917 = sep [ptext SLIT("Rule") <+> ptext name <> colon,
918 ptext SLIT("Forall'd variable") <+> quotes (ppr var) <+>
919 ptext SLIT("does not appear on left hand side")]