+tcSynDecls :: (Name -> ArgVrcs) -> [LTyClDecl Name] -> TcM [TyThing]
+tcSynDecls calc_vrcs [] = return []
+tcSynDecls calc_vrcs (decl : decls)
+ = do { syn_tc <- addLocM (tcSynDecl calc_vrcs) decl
+ ; syn_tcs <- tcExtendGlobalEnv [syn_tc] (tcSynDecls calc_vrcs decls)
+ ; return (syn_tc : syn_tcs) }
+
+tcSynDecl calc_vrcs
+ (TySynonym {tcdLName = L _ tc_name, tcdTyVars = tvs, tcdSynRhs = rhs_ty})
+ = tcTyVarBndrs tvs $ \ tvs' -> do
+ { traceTc (text "tcd1" <+> ppr tc_name)
+ ; rhs_ty' <- tcHsKindedType rhs_ty
+ ; return (ATyCon (buildSynTyCon tc_name tvs' rhs_ty' (calc_vrcs tc_name))) }
+
+--------------------
+tcTyClDecl :: (Name -> ArgVrcs) -> (Name -> RecFlag)
+ -> TyClDecl Name -> TcM TyThing
+
+tcTyClDecl calc_vrcs calc_isrec decl
+ = tcAddDeclCtxt decl (tcTyClDecl1 calc_vrcs calc_isrec decl)
+
+tcTyClDecl1 calc_vrcs calc_isrec
+ (TyData {tcdND = new_or_data, tcdCtxt = ctxt, tcdTyVars = tvs,
+ tcdLName = L _ tc_name, tcdKindSig = mb_ksig, tcdCons = cons})
+ = tcTyVarBndrs tvs $ \ tvs' -> do
+ { extra_tvs <- tcDataKindSig mb_ksig
+ ; let final_tvs = tvs' ++ extra_tvs
+ ; stupid_theta <- tcStupidTheta ctxt cons
+ ; want_generic <- doptM Opt_Generics
+ ; tycon <- fixM (\ tycon -> do
+ { unbox_strict <- doptM Opt_UnboxStrictFields
+ ; gla_exts <- doptM Opt_GlasgowExts
+ ; checkTc (gla_exts || h98_syntax) (badGadtDecl tc_name)
+
+ ; data_cons <- mappM (addLocM (tcConDecl unbox_strict new_or_data tycon final_tvs)) cons
+ ; let tc_rhs = case new_or_data of
+ DataType -> mkDataTyConRhs stupid_theta data_cons
+ NewType -> ASSERT( isSingleton data_cons )
+ mkNewTyConRhs tycon (head data_cons)
+ ; buildAlgTyCon tc_name final_tvs tc_rhs arg_vrcs is_rec
+ (want_generic && canDoGenerics data_cons)
+ })
+ ; return (ATyCon tycon)
+ }
+ where
+ arg_vrcs = calc_vrcs tc_name
+ is_rec = calc_isrec tc_name
+ h98_syntax = case cons of -- All constructors have same shape
+ L _ (GadtDecl {}) : _ -> False
+ other -> True
+
+tcTyClDecl1 calc_vrcs calc_isrec
+ (ClassDecl {tcdLName = L _ class_name, tcdTyVars = tvs,
+ tcdCtxt = ctxt, tcdMeths = meths,
+ tcdFDs = fundeps, tcdSigs = sigs} )
+ = tcTyVarBndrs tvs $ \ tvs' -> do
+ { ctxt' <- tcHsKindedContext ctxt
+ ; fds' <- mappM (addLocM tc_fundep) fundeps
+ ; sig_stuff <- tcClassSigs class_name sigs meths
+ ; clas <- fixM (\ clas ->
+ let -- This little knot is just so we can get
+ -- hold of the name of the class TyCon, which we
+ -- need to look up its recursiveness and variance
+ tycon_name = tyConName (classTyCon clas)
+ tc_isrec = calc_isrec tycon_name
+ tc_vrcs = calc_vrcs tycon_name
+ in
+ buildClass class_name tvs' ctxt' fds'
+ sig_stuff tc_isrec tc_vrcs)
+ ; return (AClass clas) }
+ where
+ tc_fundep (tvs1, tvs2) = do { tvs1' <- mappM tcLookupTyVar tvs1 ;
+ ; tvs2' <- mappM tcLookupTyVar tvs2 ;
+ ; return (tvs1', tvs2') }
+
+
+tcTyClDecl1 calc_vrcs calc_isrec
+ (ForeignType {tcdLName = L _ tc_name, tcdExtName = tc_ext_name})
+ = returnM (ATyCon (mkForeignTyCon tc_name tc_ext_name liftedTypeKind 0 []))
+
+-----------------------------------
+tcConDecl :: Bool -- True <=> -funbox-strict_fields
+ -> NewOrData -> TyCon -> [TyVar]
+ -> ConDecl Name -> TcM DataCon
+
+tcConDecl unbox_strict NewType tycon tc_tvs -- Newtypes
+ (ConDecl name ex_tvs ex_ctxt details)
+ = ASSERT( null ex_tvs && null (unLoc ex_ctxt) )
+ do { let tc_datacon field_lbls arg_ty
+ = do { arg_ty' <- tcHsKindedType arg_ty -- No bang on newtype
+ ; buildDataCon (unLoc name) False {- Prefix -}
+ True {- Vanilla -} [NotMarkedStrict]
+ (map unLoc field_lbls)
+ tc_tvs [] [arg_ty']
+ tycon (mkTyVarTys tc_tvs) }
+ ; case details of
+ PrefixCon [arg_ty] -> tc_datacon [] arg_ty
+ RecCon [(field_lbl, arg_ty)] -> tc_datacon [field_lbl] arg_ty }
+
+tcConDecl unbox_strict DataType tycon tc_tvs -- Ordinary data types
+ (ConDecl name ex_tvs ex_ctxt details)
+ = tcTyVarBndrs ex_tvs $ \ ex_tvs' -> do
+ { ex_ctxt' <- tcHsKindedContext ex_ctxt
+ ; let
+ is_vanilla = null ex_tvs && null (unLoc ex_ctxt)
+ -- Vanilla iff no ex_tvs and no context
+ -- Must check the context too because of
+ -- implicit params; e.g.
+ -- data T = (?x::Int) => MkT Int
+
+ tc_datacon is_infix field_lbls btys
+ = do { let { bangs = map getBangStrictness btys }
+ ; arg_tys <- mappM tcHsBangType btys
+ ; buildDataCon (unLoc name) is_infix is_vanilla
+ (argStrictness unbox_strict tycon bangs arg_tys)
+ (map unLoc field_lbls)
+ (tc_tvs ++ ex_tvs')
+ ex_ctxt'
+ arg_tys
+ tycon (mkTyVarTys tc_tvs) }
+ ; case details of
+ PrefixCon btys -> tc_datacon False [] btys
+ InfixCon bty1 bty2 -> tc_datacon True [] [bty1,bty2]
+ RecCon fields -> do { checkTc (null ex_tvs) (exRecConErr name)
+ -- It's ok to have an implicit-parameter context
+ -- for the data constructor, provided it binds
+ -- no type variables
+ ; let { (field_names, btys) = unzip fields }
+ ; tc_datacon False field_names btys } }
+
+tcConDecl unbox_strict DataType tycon tc_tvs -- GADTs
+ decl@(GadtDecl name con_ty)
+ = do { traceTc (text "tcConDecl" <+> ppr name)
+ ; (tvs, theta, bangs, arg_tys, tc, res_tys) <- tcLHsConSig con_ty
+
+ ; traceTc (text "tcConDecl1" <+> ppr name)
+ ; let -- Now dis-assemble the type, and check its form
+ is_vanilla = null theta && mkTyVarTys tvs `tcEqTypes` res_tys
+
+ -- Vanilla datacons guarantee to use the same
+ -- type variables as the parent tycon
+ (tvs', arg_tys', res_tys')
+ | is_vanilla = (tc_tvs, substTys subst arg_tys, substTys subst res_tys)
+ | otherwise = (tvs, arg_tys, res_tys)
+ subst = zipTopTvSubst tvs (mkTyVarTys tc_tvs)
+
+ ; traceTc (text "tcConDecl3" <+> ppr name)
+ ; buildDataCon (unLoc name) False {- Not infix -} is_vanilla
+ (argStrictness unbox_strict tycon bangs arg_tys)
+ [{- No field labels -}]
+ tvs' theta arg_tys' tycon res_tys' }
+
+-------------------
+tcStupidTheta :: LHsContext Name -> [LConDecl Name] -> TcM (Maybe ThetaType)
+-- For GADTs we don't allow a context on the data declaration
+-- whereas for standard Haskell style data declarations, we do
+tcStupidTheta ctxt (L _ (ConDecl _ _ _ _) : _)
+ = do { theta <- tcHsKindedContext ctxt; return (Just theta) }
+tcStupidTheta ctxt other -- Includes an empty constructor list
+ = ASSERT( null (unLoc ctxt) ) return Nothing
+
+-------------------
+argStrictness :: Bool -- True <=> -funbox-strict_fields
+ -> TyCon -> [HsBang]
+ -> [TcType] -> [StrictnessMark]
+argStrictness unbox_strict tycon bangs arg_tys
+ = ASSERT( length bangs == length arg_tys )
+ zipWith (chooseBoxingStrategy unbox_strict tycon) arg_tys bangs
+
+-- We attempt to unbox/unpack a strict field when either:
+-- (i) The field is marked '!!', or
+-- (ii) The field is marked '!', and the -funbox-strict-fields flag is on.
+
+chooseBoxingStrategy :: Bool -> TyCon -> TcType -> HsBang -> StrictnessMark
+chooseBoxingStrategy unbox_strict_fields tycon arg_ty bang
+ = case bang of
+ HsNoBang -> NotMarkedStrict
+ HsStrict | unbox_strict_fields && can_unbox -> MarkedUnboxed
+ HsUnbox | can_unbox -> MarkedUnboxed
+ other -> MarkedStrict