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, mkGenOcc1, mkGenOcc2 )
32 import TyCon ( TyCon, DataConDetails(..), visibleDataCons,
33 tyConTyVars, tyConName )
34 import VarSet ( intersectVarSet, isEmptyVarSet )
35 import Generics ( mkTyConGenInfo )
36 import CmdLineOpts ( DynFlag(..) )
40 %************************************************************************
42 \subsection{Type checking}
44 %************************************************************************
47 tcTyDecl :: RenamedTyClDecl -> TcM (Name, TyThingDetails)
48 tcTyDecl (TySynonym {tcdName = tycon_name, tcdSynRhs = rhs})
49 = tcLookupTyCon tycon_name `thenM` \ tycon ->
50 tcExtendTyVarEnv (tyConTyVars tycon) $
51 tcHsType rhs `thenM` \ rhs_ty ->
52 returnM (tycon_name, SynTyDetails rhs_ty)
54 tcTyDecl (TyData {tcdND = new_or_data, tcdCtxt = context,
55 tcdName = tycon_name, tcdCons = con_decls,
56 tcdGeneric = generic})
57 = tcLookupTyCon tycon_name `thenM` \ tycon ->
59 tyvars = tyConTyVars tycon
61 tcExtendTyVarEnv tyvars $
62 tcHsTheta context `thenM` \ ctxt ->
63 tcConDecls new_or_data tycon tyvars ctxt con_decls `thenM` \ data_cons ->
65 sel_ids = mkRecordSelectors tycon data_cons
67 tcGenericInfo tycon generic `thenM` \ gen_info ->
68 returnM (tycon_name, DataTyDetails ctxt data_cons sel_ids gen_info)
70 tcTyDecl (ForeignType {tcdName = tycon_name})
71 = returnM (tycon_name, ForeignTyDetails)
74 tcGenericInfo tycon generics -- Source code decl: consult the flag
75 = do_we_want generics `thenM` \ want_generics ->
77 mapM (lookupSysName (tyConName tycon))
78 [mkGenOcc1, mkGenOcc2] `thenM` \ gen_sys_names ->
79 returnM (mkTyConGenInfo tycon gen_sys_names)
83 do_we_want (Just g) = returnM g -- Interface file decl
85 do_we_want Nothing = doptM Opt_Generics -- Source code decl
88 mkRecordSelectors tycon data_cons
89 = -- We'll check later that fields with the same name
90 -- from different constructors have the same type.
91 [ mkRecordSelId tycon field
92 | field <- nubBy eq_name fields ]
94 fields = [ field | con <- visibleDataCons data_cons,
95 field <- dataConFieldLabels con ]
96 eq_name field1 field2 = fieldLabelName field1 == fieldLabelName field2
100 %************************************************************************
102 \subsection{Kind and type check constructors}
104 %************************************************************************
107 kcConDetails :: NewOrData -> RenamedContext
108 -> HsConDetails Name (BangType Name) -> TcM ()
109 kcConDetails new_or_data ex_ctxt details
110 = kcHsContext ex_ctxt `thenM_`
111 mappM_ kc_sig_type (conDetailsTys details)
113 kc_sig_type = case new_or_data of
114 DataType -> kcHsSigType
115 NewType -> kcHsLiftedSigType
116 -- Can't allow an unlifted type here, because we're effectively
117 -- going to remove the constructor while coercing it to a lifted type.
120 tcConDecls :: NewOrData -> TyCon -> [TyVar] -> ThetaType
121 -> DataConDetails RenamedConDecl -> TcM (DataConDetails DataCon)
123 tcConDecls new_or_data tycon tyvars ctxt con_decls
125 Unknown -> returnM Unknown
126 HasCons n -> returnM (HasCons n)
127 DataCons cs -> mappM tc_con_decl cs `thenM` \ data_cons ->
128 returnM (DataCons data_cons)
130 tc_con_decl (ConDecl name ex_tvs ex_ctxt details src_loc)
131 = addSrcLoc src_loc $
132 tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details) $ \ ex_tyvars ->
133 tcHsTheta ex_ctxt `thenM` \ ex_theta ->
135 PrefixCon btys -> tc_datacon ex_tyvars ex_theta btys
136 InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
137 RecCon fields -> tc_rec_con ex_tyvars ex_theta fields
140 tc_datacon ex_tyvars ex_theta btys
141 = mappM tcHsType (map getBangType btys) `thenM` \ arg_tys ->
143 (map getBangStrictness btys)
144 [{- No field labels -}]
145 tyvars ctxt ex_tyvars ex_theta
148 tc_rec_con ex_tyvars ex_theta fields
149 = checkTc (null ex_tyvars) (exRecConErr name) `thenM_`
150 mappM tc_field (fields `zip` allFieldLabelTags) `thenM` \ field_labels ->
152 arg_stricts = [getBangStrictness bty | (n, bty) <- fields]
153 arg_tys = map fieldLabelType field_labels
155 tcMkDataCon name arg_stricts field_labels
156 tyvars ctxt ex_tyvars ex_theta
159 tc_field ((field_label_name, bty), tag)
160 = tcHsType (getBangType bty) `thenM` \ field_ty ->
161 returnM (mkFieldLabel field_label_name tycon field_ty tag)
164 -> [StrictnessMark] -> [FieldLabel]
165 -> [TyVar] -> ThetaType
166 -> [TyVar] -> ThetaType
169 -- A wrapper for DataCon.mkDataCon that
170 -- a) makes the worker Id
171 -- b) makes the wrapper Id if necessary, including
172 -- allocating its unique (hence monadic)
173 tcMkDataCon src_name arg_stricts fields
174 tyvars ctxt ex_tyvars ex_theta
176 = lookupSysName src_name mkDataConWrapperOcc `thenM` \ wrap_name ->
177 lookupSysName src_name mkDataConWorkerOcc `thenM` \ work_name ->
178 -- This last one takes the name of the data constructor in the source
179 -- code, which (for Haskell source anyway) will be in the SrcDataName name
180 -- space, and makes it into a "real data constructor name"
182 data_con = mkDataCon src_name arg_stricts fields
183 tyvars (thinContext arg_tys ctxt)
186 data_con_work_id data_con_wrap_id
187 data_con_work_id = mkDataConWorkId work_name data_con
188 data_con_wrap_id = mkDataConWrapId wrap_name data_con
192 -- The context for a data constructor should be limited to
193 -- the type variables mentioned in the arg_tys
194 thinContext arg_tys ctxt
195 = filter in_arg_tys ctxt
197 arg_tyvars = tyVarsOfTypes arg_tys
198 in_arg_tys pred = not $ isEmptyVarSet $
199 tyVarsOfPred pred `intersectVarSet` arg_tyvars
203 %************************************************************************
205 \subsection{Errors and contexts}
207 %************************************************************************
212 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
214 (ptext SLIT("In the declaration of data constructor") <+> ppr name)