- tcTopBindsAndThen
- (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
- (get_val_decls decls `ThenBinds` deriv_binds)
- ( tcGetEnv `thenNF_Tc` \ env ->
- tcGetUnique `thenNF_Tc` \ uniq ->
- returnTc ((EmptyMonoBinds, env), emptyLIE)
- ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
- tcSetEnv final_env $
-
- -- foreign export declarations next.
- tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
-
- -- Second pass over class and instance declarations,
- -- to compile the bindings themselves.
- tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
- tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
- tcRules decls `thenNF_Tc` \ (lie_rules, rules) ->
-
-
- -- Deal with constant or ambiguous InstIds. How could
- -- there be ambiguous ones? They can only arise if a
- -- top-level decl falls under the monomorphism
- -- restriction, and no subsequent decl instantiates its
- -- type. (Usually, ambiguous type variables are resolved
- -- during the generalisation step.)
- let
- lie_alldecls = lie_valdecls `plusLIE`
- lie_instdecls `plusLIE`
- lie_clasdecls `plusLIE`
- lie_fodecls `plusLIE`
- lie_rules
- in
- tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
-
- -- Check that Main defines main
- (if mod_name == mAIN_Name then
- tcLookupValueByKeyMaybe mainKey `thenNF_Tc` \ maybe_main ->
- checkTc (maybeToBool maybe_main) noMainErr
- else
- returnTc ()
- ) `thenTc_`
-
- -- Backsubstitution. This must be done last.
- -- Even tcSimplifyTop may do some unification.
- let
- all_binds = imp_data_binds `AndMonoBinds`
- imp_cls_binds `AndMonoBinds`
- val_binds `AndMonoBinds`
- inst_binds `AndMonoBinds`
- cls_dm_binds `AndMonoBinds`
- const_inst_binds `AndMonoBinds`
- foe_binds
- in
- zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
- tcSetValueEnv really_final_env $
- zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
- zonkRules rules `thenNF_Tc` \ rules' ->
-
- returnTc (really_final_env,
- (TcResults { tc_binds = all_binds',
- tc_tycons = local_tycons,
- tc_classes = local_classes,
- tc_insts = inst_info,
- tc_fords = foi_decls ++ foe_decls',
- tc_rules = rules',
- tc_env = really_final_env
- }))
-
- -- End of outer fix loop
- ) `thenTc` \ (final_env, stuff) ->
- returnTc stuff
-
-get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
+-- traceTc (text "Tc5") `thenNF_Tc_`
+ tcTopBinds (get_binds decls `ThenBinds` deriv_binds) `thenTc` \ ((val_binds, env), lie_valdecls) ->
+ tcSetEnv env $
+
+ -- Foreign export declarations next
+-- traceTc (text "Tc6") `thenNF_Tc_`
+ tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
+
+ -- Second pass over class and instance declarations,
+ -- to compile the bindings themselves.
+-- traceTc (text "Tc7") `thenNF_Tc_`
+ tcInstDecls2 local_inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
+-- traceTc (text "Tc8") `thenNF_Tc_`
+ tcClassDecls2 this_mod decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
+ tcRules (pcs_rules pcs) this_mod decls `thenNF_Tc` \ (new_pcs_rules, lie_rules, local_rules) ->
+
+ -- Deal with constant or ambiguous InstIds. How could
+ -- there be ambiguous ones? They can only arise if a
+ -- top-level decl falls under the monomorphism
+ -- restriction, and no subsequent decl instantiates its
+ -- type. (Usually, ambiguous type variables are resolved
+ -- during the generalisation step.)
+ let
+ lie_alldecls = lie_valdecls `plusLIE`
+ lie_instdecls `plusLIE`
+ lie_clasdecls `plusLIE`
+ lie_fodecls `plusLIE`
+ lie_rules
+ in
+ tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
+
+ -- Backsubstitution. This must be done last.
+ -- Even tcSimplifyTop may do some unification.
+ let
+ all_binds = data_cls_binds `AndMonoBinds`
+ val_binds `AndMonoBinds`
+ inst_binds `AndMonoBinds`
+ cls_dm_binds `AndMonoBinds`
+ const_inst_binds `AndMonoBinds`
+ foe_binds
+ in
+-- traceTc (text "Tc9") `thenNF_Tc_`
+ zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', final_env) ->
+ tcSetEnv final_env $
+ -- zonkTopBinds puts all the top-level Ids into the tcGEnv
+ zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
+ zonkRules local_rules `thenNF_Tc` \ local_rules' ->
+
+
+ let (local_things, imported_things) = partition (isLocalThing this_mod)
+ (nameEnvElts (getTcGEnv final_env))
+
+ local_type_env :: TypeEnv
+ local_type_env = mkTypeEnv local_things
+
+ new_pte :: PackageTypeEnv
+ new_pte = extendTypeEnvList (pcs_PTE pcs) imported_things
+
+ final_pcs :: PersistentCompilerState
+ final_pcs = pcs { pcs_PTE = new_pte,
+ pcs_insts = new_pcs_insts,
+ pcs_rules = new_pcs_rules
+ }
+ in
+-- traceTc (text "Tc10") `thenNF_Tc_`
+ returnTc (TcResults { tc_pcs = final_pcs,
+ tc_env = local_type_env,
+ tc_binds = all_binds',
+ tc_insts = map iDFunId local_inst_info,
+ tc_fords = foi_decls ++ foe_decls',
+ tc_rules = local_rules'
+ }
+ )
+
+get_binds decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]