+-- Rename kind signatures (signatures of indexed data types/newtypes and
+-- signatures of type functions)
+--
+-- * This function is parametrised by the routine handling the index
+-- variables. On the toplevel, these are defining occurences, whereas they
+-- are usage occurences for associated types.
+--
+rnTySig :: TyClDecl RdrName
+ -> (SDoc -> [LHsTyVarBndr RdrName] ->
+ ([LHsTyVarBndr Name] -> RnM (TyClDecl Name, FreeVars)) ->
+ RnM (TyClDecl Name, FreeVars))
+ -> RnM (TyClDecl Name, FreeVars)
+
+rnTySig (tydecl@TyData {tcdCtxt = context, tcdLName = tycon,
+ tcdTyVars = tyvars, tcdTyPats = mb_typats,
+ tcdCons = condecls, tcdKindSig = sig,
+ tcdDerivs = derivs})
+ bindIdxVars =
+ ASSERT( null condecls ) -- won't have constructors
+ ASSERT( isNothing mb_typats ) -- won't have type patterns
+ ASSERT( isNothing derivs ) -- won't have deriving
+ ASSERT( isJust sig ) -- will have kind signature
+ do { checkM (not . null $ tyvars) $ addErr needOneIdx -- #indexes >= 1
+ ; bindIdxVars (ksig_doc tycon) tyvars $ \tyvars' -> do {
+ ; tycon' <- lookupLocatedTopBndrRn tycon
+ ; context' <- rnContext (ksig_doc tycon) context
+ ; returnM (TyData {tcdND = tcdND tydecl, tcdCtxt = context',
+ tcdLName = tycon', tcdTyVars = tyvars',
+ tcdTyPats = Nothing, tcdKindSig = sig,
+ tcdCons = [], tcdDerivs = Nothing},
+ delFVs (map hsLTyVarName tyvars') $
+ extractHsCtxtTyNames context')
+ } }
+ where
+
+rnTySig (tydecl@TyFunction {tcdLName = tycon, tcdTyVars = tyvars,
+ tcdKind = sig})
+ bindIdxVars =
+ do { checkM (not . null $ tyvars) $ addErr needOneIdx -- #indexes >= 1
+ ; bindIdxVars (ksig_doc tycon) tyvars $ \tyvars' -> do {
+ ; tycon' <- lookupLocatedTopBndrRn tycon
+ ; returnM (TyFunction {tcdLName = tycon', tcdTyVars = tyvars',
+ tcdIso = tcdIso tydecl, tcdKind = sig},
+ emptyFVs)
+ } }
+
+ksig_doc tycon = text "In the kind signature for" <+> quotes (ppr tycon)
+needOneIdx = text "Kind signature requires at least one type index"
+
+-- Rename associated type declarations (in classes)
+--
+-- * This can be kind signatures and (default) type function equations.
+--
+rnATs :: [LTyClDecl RdrName] -> RnM ([LTyClDecl Name], FreeVars)
+rnATs ats = mapFvRn (wrapLocFstM rn_at) ats
+ where
+ rn_at (tydecl@TyData {}) = rnTySig tydecl lookupIdxVars
+ rn_at (tydecl@TyFunction {}) = rnTySig tydecl lookupIdxVars
+ rn_at (tydecl@TySynonym {}) =
+ do
+ checkM (isNothing (tcdTyPats tydecl)) $ addErr noPatterns
+ rnTyClDecl tydecl
+ rn_at _ = panic "RnSource.rnATs: invalid TyClDecl"
+
+ lookupIdxVars _ tyvars cont =
+ do { checkForDups tyvars;
+ ; tyvars' <- mappM lookupIdxVar tyvars
+ ; cont tyvars'
+ }
+ -- Type index variables must be class parameters, which are the only
+ -- type variables in scope at this point.
+ lookupIdxVar (L l tyvar) =
+ do
+ name' <- lookupOccRn (hsTyVarName tyvar)
+ return $ L l (replaceTyVarName tyvar name')
+
+ -- Type variable may only occur once.
+ --
+ checkForDups [] = return ()
+ checkForDups (L loc tv:ltvs) =
+ do { setSrcSpan loc $
+ when (hsTyVarName tv `ltvElem` ltvs) $
+ addErr (repeatedTyVar tv)
+ ; checkForDups ltvs
+ }
+
+ rdrName `ltvElem` [] = False
+ rdrName `ltvElem` (L _ tv:ltvs)
+ | rdrName == hsTyVarName tv = True
+ | otherwise = rdrName `ltvElem` ltvs
+
+noPatterns = text "Default definition for an associated synonym cannot have"
+ <+> text "type pattern"
+
+repeatedTyVar tv = ptext SLIT("Illegal repeated type variable") <+>
+ quotes (ppr tv)
+