+newNamedKindVar name = newKindVar `thenNF_Tc` \ kind ->
+ returnNF_Tc (name, kind)
+
+---------------------------
+kcBoxedType :: RenamedHsType -> TcM ()
+ -- The type ty must be a *boxed* *type*
+kcBoxedType ty
+ = kcHsType ty `thenTc` \ kind ->
+ tcAddErrCtxt (typeKindCtxt ty) $
+ unifyKind boxedTypeKind kind
+
+---------------------------
+kcTypeType :: RenamedHsType -> TcM ()
+ -- The type ty must be a *type*, but it can be boxed or unboxed.
+kcTypeType ty
+ = kcHsType ty `thenTc` \ kind ->
+ tcAddErrCtxt (typeKindCtxt ty) $
+ unifyOpenTypeKind kind
+
+---------------------------
+kcHsSigType, kcHsBoxedSigType :: RenamedHsType -> TcM ()
+ -- Used for type signatures
+kcHsSigType = kcTypeType
+kcHsBoxedSigType = kcBoxedType
+
+---------------------------
+kcHsType :: RenamedHsType -> TcM TcKind
+kcHsType (HsTyVar name) = kcTyVar name
+kcHsType (HsUsgTy _ ty) = kcHsType ty
+kcHsType (HsUsgForAllTy _ ty) = kcHsType ty
+
+kcHsType (HsListTy ty)
+ = kcBoxedType ty `thenTc` \ tau_ty ->
+ returnTc boxedTypeKind
+
+kcHsType (HsTupleTy (HsTupCon _ Boxed) tys)
+ = mapTc kcBoxedType tys `thenTc_`
+ returnTc boxedTypeKind
+
+kcHsType ty@(HsTupleTy (HsTupCon _ Unboxed) tys)
+ = failWithTc (unboxedTupleErr ty)
+ -- Unboxed tuples are illegal everywhere except
+ -- just after a function arrow (see kcFunResType)
+
+kcHsType (HsFunTy ty1 ty2)
+ = kcTypeType ty1 `thenTc_`
+ kcFunResType ty2 `thenTc_`
+ returnTc boxedTypeKind
+
+kcHsType ty@(HsOpTy ty1 op ty2)
+ = kcTyVar op `thenTc` \ op_kind ->
+ kcHsType ty1 `thenTc` \ ty1_kind ->
+ kcHsType ty2 `thenTc` \ ty2_kind ->
+ tcAddErrCtxt (appKindCtxt (ppr ty)) $
+ kcAppKind op_kind ty1_kind `thenTc` \ op_kind' ->
+ kcAppKind op_kind' ty2_kind
+
+kcHsType (HsPredTy pred)
+ = kcHsPred pred `thenTc_`
+ returnTc boxedTypeKind
+
+kcHsType ty@(HsAppTy ty1 ty2)
+ = kcHsType ty1 `thenTc` \ tc_kind ->
+ kcHsType ty2 `thenTc` \ arg_kind ->
+ tcAddErrCtxt (appKindCtxt (ppr ty)) $
+ kcAppKind tc_kind arg_kind
+
+kcHsType (HsForAllTy (Just tv_names) context ty)
+ = kcHsTyVars tv_names `thenNF_Tc` \ kind_env ->
+ tcExtendKindEnv kind_env $
+ kcHsContext context `thenTc_`
+
+ -- Context behaves like a function type
+ -- This matters. Return-unboxed-tuple analysis can
+ -- give overloaded functions like
+ -- f :: forall a. Num a => (# a->a, a->a #)
+ -- And we want these to get through the type checker
+ if null context then
+ kcHsType ty
+ else
+ kcFunResType ty `thenTc_`
+ returnTc boxedTypeKind
+
+---------------------------
+kcFunResType :: RenamedHsType -> TcM TcKind
+-- The only place an unboxed tuple type is allowed
+-- is at the right hand end of an arrow
+kcFunResType (HsTupleTy (HsTupCon _ Unboxed) tys)
+ = mapTc kcTypeType tys `thenTc_`
+ returnTc unboxedTypeKind
+
+kcFunResType ty = kcHsType ty
+
+---------------------------
+kcAppKind fun_kind arg_kind
+ = case splitFunTy_maybe fun_kind of
+ Just (arg_kind', res_kind)
+ -> unifyKind arg_kind arg_kind' `thenTc_`
+ returnTc res_kind
+
+ Nothing -> newKindVar `thenNF_Tc` \ res_kind ->
+ unifyKind fun_kind (mkArrowKind arg_kind res_kind) `thenTc_`
+ returnTc res_kind
+
+
+---------------------------
+kcHsContext ctxt = mapTc_ kcHsPred ctxt
+
+kcHsPred :: RenamedHsPred -> TcM ()
+kcHsPred pred@(HsPIParam name ty)
+ = tcAddErrCtxt (appKindCtxt (ppr pred)) $
+ kcBoxedType ty
+
+kcHsPred pred@(HsPClass cls tys)
+ = tcAddErrCtxt (appKindCtxt (ppr pred)) $
+ kcClass cls `thenTc` \ kind ->
+ mapTc kcHsType tys `thenTc` \ arg_kinds ->
+ unifyKind kind (mkArrowKinds arg_kinds boxedTypeKind)
+
+---------------------------
+kcTyVar name -- Could be a tyvar or a tycon
+ = tcLookup name `thenTc` \ thing ->
+ case thing of
+ AThing kind -> returnTc kind
+ ATyVar tv -> returnTc (tyVarKind tv)
+ AGlobal (ATyCon tc) -> returnTc (tyConKind tc)
+ other -> failWithTc (wrongThingErr "type" thing name)
+
+kcClass cls -- Must be a class
+ = tcLookup cls `thenNF_Tc` \ thing ->
+ case thing of
+ AThing kind -> returnTc kind
+ AGlobal (AClass cls) -> returnTc (tyConKind (classTyCon cls))
+ other -> failWithTc (wrongThingErr "class" thing cls)
+\end{code}