-tcGroup :: TcEnv s -> InstanceMapper -> Bag RenamedHsDecl -> TcM s (TcEnv s)
-tcGroup unf_env inst_mapper decls
- = -- TIE THE KNOT
- fixTc ( \ ~(tycons,classes,_) ->
-
- -- EXTEND TYPE AND CLASS ENVIRONMENTS
- -- NB: it's important that the tycons and classes come back in just
- -- the same order from this fix as from get_binders, so that these
- -- extend-env things work properly. A bit UGH-ish.
- tcExtendTyConEnv tycon_names_w_arities tycons $
- tcExtendClassEnv class_names classes $
-
- -- DEAL WITH TYPE VARIABLES
- tcTyVarScope tyvar_names ( \ tyvars ->
-
- -- DEAL WITH THE DEFINITIONS THEMSELVES
- foldBag combine (tcDecl unf_env inst_mapper)
- (returnTc (emptyBag, emptyBag))
- decls
- ) `thenTc` \ (tycon_bag,class_bag) ->
- let
- tycons = bagToList tycon_bag
- classes = bagToList class_bag
- in
-
- -- SNAFFLE ENV TO RETURN
- tcGetEnv `thenNF_Tc` \ final_env ->
-
- returnTc (tycons, classes, final_env)
- ) `thenTc` \ (_, _, final_env) ->
-
- returnTc final_env
+tcGroup :: RecTcEnv -> SCC RenamedTyClDecl -> TcM TcEnv
+tcGroup unf_env scc
+ = getDOptsTc `thenTc` \ dflags ->
+ -- Step 1
+ mapNF_Tc getInitialKind decls `thenNF_Tc` \ initial_kinds ->
+
+ -- Step 2
+ tcExtendKindEnv initial_kinds (mapTc kcTyClDecl decls) `thenTc_`
+
+ -- Step 3
+ zonkKindEnv initial_kinds `thenNF_Tc` \ final_kinds ->
+
+ -- Tie the knot
+ fixTc ( \ ~(rec_details_list, _, _) ->
+ -- Step 4
+ let
+ kind_env = mkNameEnv final_kinds
+ rec_details = mkNameEnv rec_details_list
+
+ tyclss, all_tyclss :: [TyThing]
+ tyclss = map (buildTyConOrClass dflags is_rec kind_env
+ rec_vrcs rec_details) decls
+
+ -- Add the tycons that come from the classes
+ -- We want them in the environment because
+ -- they are mentioned in interface files
+ all_tyclss = [ ATyCon (classTyCon clas) | AClass clas <- tyclss]
+ ++ tyclss
+
+ -- Calculate variances, and (yes!) feed back into buildTyConOrClass.
+ rec_vrcs = calcTyConArgVrcs [tc | ATyCon tc <- all_tyclss]
+ in
+ -- Step 5
+ tcExtendGlobalEnv all_tyclss $
+ mapTc (tcTyClDecl1 is_rec unf_env) decls `thenTc` \ tycls_details ->
+
+ -- Return results
+ tcGetEnv `thenNF_Tc` \ env ->
+ returnTc (tycls_details, all_tyclss, env)
+ ) `thenTc` \ (_, all_tyclss, env) ->
+
+ tcSetEnv env $
+
+ -- Step 6
+ -- For a recursive group, check all the types again,
+ -- this time with the wimp flag off
+ (if isRec is_rec then
+ mapTc_ (tcTyClDecl1 NonRecursive unf_env) decls
+ else
+ returnTc ()
+ ) `thenTc_`
+
+ -- Step 7
+ -- Extend the environment with the final TyCons/Classes
+ -- and their implicit Ids
+ tcExtendGlobalValEnv (implicitTyThingIds all_tyclss) tcGetEnv