2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[TcTyDecls]{Typecheck type declarations}
7 module TcTyDecls ( tcTyDecl, kcConDetails, tcMkDataCon ) where
9 #include "HsVersions.h"
11 import HsSyn ( TyClDecl(..), ConDecl(..), HsConDetails(..), BangType,
12 getBangType, getBangStrictness, conDetailsTys
14 import RnHsSyn ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
15 import BasicTypes ( NewOrData(..), StrictnessMark )
17 import TcMonoType ( tcHsTyVars, tcHsTheta, tcHsType,
18 kcHsContext, kcHsSigType, kcHsLiftedSigType
20 import TcEnv ( tcExtendTyVarEnv, tcLookupTyCon, TyThingDetails(..) )
21 import TcType ( Type, tyVarsOfTypes, tyVarsOfPred, ThetaType )
22 import RnEnv ( lookupSysName )
25 import DataCon ( DataCon, mkDataCon, dataConFieldLabels )
26 import FieldLabel ( FieldLabel, fieldLabelName, fieldLabelType, allFieldLabelTags, mkFieldLabel )
27 import MkId ( mkDataConWorkId, mkDataConWrapId, mkRecordSelId )
30 import OccName ( mkDataConWrapperOcc, mkDataConWorkerOcc,
31 mkGenOcc1, mkGenOcc2, setOccNameSpace )
33 import TyCon ( TyCon, DataConDetails(..), visibleDataCons,
34 tyConTyVars, tyConName )
35 import VarSet ( intersectVarSet, isEmptyVarSet )
36 import Generics ( mkTyConGenInfo )
37 import CmdLineOpts ( DynFlag(..) )
41 %************************************************************************
43 \subsection{Type checking}
45 %************************************************************************
48 tcTyDecl :: RenamedTyClDecl -> TcM (Name, TyThingDetails)
49 tcTyDecl (TySynonym {tcdName = tycon_name, tcdSynRhs = rhs})
50 = tcLookupTyCon tycon_name `thenM` \ tycon ->
51 tcExtendTyVarEnv (tyConTyVars tycon) $
52 tcHsType rhs `thenM` \ rhs_ty ->
53 returnM (tycon_name, SynTyDetails rhs_ty)
55 tcTyDecl (TyData {tcdND = new_or_data, tcdCtxt = context,
56 tcdName = tycon_name, tcdCons = con_decls,
57 tcdGeneric = generic})
58 = tcLookupTyCon tycon_name `thenM` \ tycon ->
60 tyvars = tyConTyVars tycon
62 tcExtendTyVarEnv tyvars $
63 tcHsTheta context `thenM` \ ctxt ->
64 tcConDecls new_or_data tycon tyvars ctxt con_decls `thenM` \ data_cons ->
66 sel_ids = mkRecordSelectors tycon data_cons
68 tcGenericInfo tycon generic `thenM` \ gen_info ->
69 returnM (tycon_name, DataTyDetails ctxt data_cons sel_ids gen_info)
71 tcTyDecl (ForeignType {tcdName = tycon_name})
72 = returnM (tycon_name, ForeignTyDetails)
75 tcGenericInfo tycon generics -- Source code decl: consult the flag
76 = do_we_want generics `thenM` \ want_generics ->
78 mapM (lookupSysName (tyConName tycon))
79 [mkGenOcc1, mkGenOcc2] `thenM` \ gen_sys_names ->
80 returnM (mkTyConGenInfo tycon gen_sys_names)
84 do_we_want (Just g) = returnM g -- Interface file decl
86 do_we_want Nothing = doptM Opt_Generics -- Source code decl
89 mkRecordSelectors tycon data_cons
90 = -- We'll check later that fields with the same name
91 -- from different constructors have the same type.
92 [ mkRecordSelId tycon field
93 | field <- nubBy eq_name fields ]
95 fields = [ field | con <- visibleDataCons data_cons,
96 field <- dataConFieldLabels con ]
97 eq_name field1 field2 = fieldLabelName field1 == fieldLabelName field2
101 %************************************************************************
103 \subsection{Kind and type check constructors}
105 %************************************************************************
108 kcConDetails :: NewOrData -> RenamedContext
109 -> HsConDetails Name (BangType Name) -> TcM ()
110 kcConDetails new_or_data ex_ctxt details
111 = kcHsContext ex_ctxt `thenM_`
112 mappM_ kc_sig_type (conDetailsTys details)
114 kc_sig_type = case new_or_data of
115 DataType -> kcHsSigType
116 NewType -> kcHsLiftedSigType
117 -- Can't allow an unlifted type here, because we're effectively
118 -- going to remove the constructor while coercing it to a lifted type.
121 tcConDecls :: NewOrData -> TyCon -> [TyVar] -> ThetaType
122 -> DataConDetails RenamedConDecl -> TcM (DataConDetails DataCon)
124 tcConDecls new_or_data tycon tyvars ctxt con_decls
126 Unknown -> returnM Unknown
127 HasCons n -> returnM (HasCons n)
128 DataCons cs -> mappM tc_con_decl cs `thenM` \ data_cons ->
129 returnM (DataCons data_cons)
131 tc_con_decl (ConDecl name ex_tvs ex_ctxt details src_loc)
132 = addSrcLoc src_loc $
133 tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details) $ \ ex_tyvars ->
134 tcHsTheta ex_ctxt `thenM` \ ex_theta ->
136 PrefixCon btys -> tc_datacon ex_tyvars ex_theta btys
137 InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
138 RecCon fields -> tc_rec_con ex_tyvars ex_theta fields
141 tc_datacon ex_tyvars ex_theta btys
142 = mappM tcHsType (map getBangType btys) `thenM` \ arg_tys ->
144 (map getBangStrictness btys)
145 [{- No field labels -}]
146 tyvars ctxt ex_tyvars ex_theta
149 tc_rec_con ex_tyvars ex_theta fields
150 = checkTc (null ex_tyvars) (exRecConErr name) `thenM_`
151 mappM tc_field (fields `zip` allFieldLabelTags) `thenM` \ field_labels ->
153 arg_stricts = [getBangStrictness bty | (n, bty) <- fields]
154 arg_tys = map fieldLabelType field_labels
156 tcMkDataCon name arg_stricts field_labels
157 tyvars ctxt ex_tyvars ex_theta
160 tc_field ((field_label_name, bty), tag)
161 = tcHsType (getBangType bty) `thenM` \ field_ty ->
162 returnM (mkFieldLabel field_label_name tycon field_ty tag)
165 -> [StrictnessMark] -> [FieldLabel]
166 -> [TyVar] -> ThetaType
167 -> [TyVar] -> ThetaType
170 -- A wrapper for DataCon.mkDataCon that
171 -- a) makes the worker Id
172 -- b) makes the wrapper Id if necessary, including
173 -- allocating its unique (hence monadic)
174 tcMkDataCon src_name arg_stricts fields
175 tyvars ctxt ex_tyvars ex_theta
177 = lookupSysName src_name mkDataConWrapperOcc `thenM` \ wrap_name ->
178 lookupSysName src_name mkDataConWorkerOcc `thenM` \ work_name ->
179 -- This last one takes the name of the data constructor in the source
180 -- code, which (for Haskell source anyway) will be in the SrcDataName name
181 -- space, and makes it into a "real data constructor name"
183 data_con = mkDataCon src_name arg_stricts fields
184 tyvars (thinContext arg_tys ctxt)
187 data_con_work_id data_con_wrap_id
188 data_con_work_id = mkDataConWorkId work_name data_con
189 data_con_wrap_id = mkDataConWrapId wrap_name data_con
193 -- The context for a data constructor should be limited to
194 -- the type variables mentioned in the arg_tys
195 thinContext arg_tys ctxt
196 = filter in_arg_tys ctxt
198 arg_tyvars = tyVarsOfTypes arg_tys
199 in_arg_tys pred = not $ isEmptyVarSet $
200 tyVarsOfPred pred `intersectVarSet` arg_tyvars
204 %************************************************************************
206 \subsection{Errors and contexts}
208 %************************************************************************
213 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
215 (ptext SLIT("In the declaration of data constructor") <+> ppr name)