- newLocalNames [(occname, locn)] `thenRn` \ [op_name] ->
- -- Make a fresh local for the bound variable; it must be different
- -- to occurrences of the same thing on the LHS, which refer to the global
- -- selectors.
+\begin{code}
+rnMethodBinds :: Name -- Class name
+ -> [Name] -- Names for generic type variables
+ -> LHsBinds RdrName
+ -> RnM (LHsBinds Name, FreeVars)
+
+rnMethodBinds cls gen_tyvars binds
+ = foldM do_one (emptyBag,emptyFVs) (bagToList binds)
+ where do_one (binds,fvs) bind = do
+ (bind', fvs_bind) <- rnMethodBind cls gen_tyvars bind
+ return (bind' `unionBags` binds, fvs_bind `plusFV` fvs)
+
+rnMethodBind cls gen_tyvars (L loc (FunBind name inf (MatchGroup matches _)))
+ = setSrcSpan loc $
+ lookupLocatedInstDeclBndr cls name `thenM` \ sel_name ->
+ let plain_name = unLoc sel_name in
+ -- We use the selector name as the binder
+
+ mapFvRn (rn_match plain_name) matches `thenM` \ (new_matches, fvs) ->
+ let
+ new_group = MatchGroup new_matches placeHolderType
+ in
+ checkPrecMatch inf plain_name new_group `thenM_`
+ returnM (unitBag (L loc (FunBind sel_name inf new_group)), fvs `addOneFV` plain_name)
+ where
+ -- Truly gruesome; bring into scope the correct members of the generic
+ -- type variables. See comments in RnSource.rnSourceDecl(ClassDecl)
+ rn_match sel_name match@(L _ (Match (L _ (TypePat ty) : _) _ _))
+ = extendTyVarEnvFVRn gen_tvs $
+ rnMatch (FunRhs sel_name) match
+ where
+ tvs = map (rdrNameOcc.unLoc) (extractHsTyRdrTyVars ty)
+ gen_tvs = [tv | tv <- gen_tyvars, nameOccName tv `elem` tvs]