9478ed49a75e54249a47e2a3d039b2100c56d5f7
[ghc-hetmet.git] / ghc / compiler / typecheck / TcTyDecls.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[TcTyDecls]{Typecheck type declarations}
5
6 \begin{code}
7 module TcTyDecls ( tcTyDecl, kcConDetails ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( TyClDecl(..), ConDecl(..), ConDetails(..), 
12                           getBangType, getBangStrictness, conDetailsTys
13                         )
14 import RnHsSyn          ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
15 import BasicTypes       ( NewOrData(..) )
16
17 import TcMonoType       ( tcHsTyVars, tcHsTheta, tcHsType, 
18                           kcHsContext, kcHsSigType, kcHsLiftedSigType
19                         )
20 import TcEnv            ( tcExtendTyVarEnv, 
21                           tcLookupTyCon, tcLookupRecId, 
22                           TyThingDetails(..), RecTcEnv
23                         )
24 import TcType           ( tyVarsOfTypes, tyVarsOfPred, ThetaType )
25 import TcMonad
26
27 import DataCon          ( DataCon, mkDataCon, dataConFieldLabels )
28 import FieldLabel       ( fieldLabelName, fieldLabelType, allFieldLabelTags, mkFieldLabel )
29 import MkId             ( mkDataConId, mkDataConWrapId, mkRecordSelId )
30 import Var              ( TyVar )
31 import Name             ( Name, NamedThing(..) )
32 import Outputable
33 import TyCon            ( TyCon, DataConDetails(..), visibleDataCons,
34                           tyConTyVars )
35 import VarSet           ( intersectVarSet, isEmptyVarSet )
36 import PrelNames        ( unpackCStringName, unpackCStringUtf8Name )
37 import List             ( nubBy )
38 \end{code}
39
40 %************************************************************************
41 %*                                                                      *
42 \subsection{Type checking}
43 %*                                                                      *
44 %************************************************************************
45
46 \begin{code}
47 tcTyDecl :: RenamedTyClDecl -> TcM (Name, TyThingDetails)
48 tcTyDecl (TySynonym {tcdName = tycon_name, tcdSynRhs = rhs})
49   = tcLookupTyCon tycon_name                    `thenNF_Tc` \ tycon ->
50     tcExtendTyVarEnv (tyConTyVars tycon)        $
51     tcHsType rhs                                `thenTc` \ rhs_ty ->
52     returnTc (tycon_name, SynTyDetails rhs_ty)
53
54 tcTyDecl (TyData {tcdND = new_or_data, tcdCtxt = context,
55                           tcdName = tycon_name, tcdCons = con_decls})
56   = tcLookupTyCon tycon_name                    `thenNF_Tc` \ tycon ->
57     let
58         tyvars = tyConTyVars tycon
59     in
60     tcExtendTyVarEnv tyvars                             $
61     tcHsTheta context                                   `thenTc` \ ctxt ->
62     tcConDecls new_or_data tycon tyvars ctxt con_decls  `thenTc` \ data_cons ->
63     let
64         sel_ids = mkRecordSelectors tycon data_cons
65     in
66     returnTc (tycon_name, DataTyDetails ctxt data_cons sel_ids)
67
68 tcTyDecl (ForeignType {tcdName = tycon_name})
69   = returnTc (tycon_name, ForeignTyDetails)
70
71
72 mkRecordSelectors tycon data_cons
73   =     -- We'll check later that fields with the same name 
74         -- from different constructors have the same type.
75      [ mkRecordSelId tycon field 
76      | field <- nubBy eq_name fields ]
77   where
78     fields = [ field | con <- visibleDataCons data_cons, 
79                        field <- dataConFieldLabels con ]
80     eq_name field1 field2 = fieldLabelName field1 == fieldLabelName field2
81 \end{code}
82
83
84 %************************************************************************
85 %*                                                                      *
86 \subsection{Kind and type check constructors}
87 %*                                                                      *
88 %************************************************************************
89
90 \begin{code}
91 kcConDetails :: NewOrData -> RenamedContext -> ConDetails Name -> TcM ()
92 kcConDetails new_or_data ex_ctxt details
93   = kcHsContext ex_ctxt         `thenTc_`
94     mapTc_ kc_sig_type (conDetailsTys details)
95   where
96     kc_sig_type = case new_or_data of
97                     DataType -> kcHsSigType
98                     NewType  -> kcHsLiftedSigType
99             -- Can't allow an unlifted type here, because we're effectively
100             -- going to remove the constructor while coercing it to a lifted type.
101
102
103 tcConDecls :: NewOrData -> TyCon -> [TyVar] -> ThetaType 
104            -> DataConDetails RenamedConDecl -> TcM (DataConDetails DataCon)
105
106 tcConDecls new_or_data tycon tyvars ctxt con_decls
107   = case con_decls of
108         Unknown     -> returnTc Unknown
109         HasCons n   -> returnTc (HasCons n)
110         DataCons cs -> mapTc tc_con_decl cs     `thenTc` \ data_cons ->
111                        returnTc (DataCons data_cons)
112   where
113     tc_con_decl (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
114       = tcAddSrcLoc src_loc                                             $
115         tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details)    $ \ ex_tyvars ->
116         tcHsTheta ex_ctxt                                               `thenTc` \ ex_theta ->
117         case details of
118             VanillaCon btys    -> tc_datacon ex_tyvars ex_theta btys
119             InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
120             RecCon fields      -> tc_rec_con ex_tyvars ex_theta fields
121       where
122         
123         tc_datacon ex_tyvars ex_theta btys
124           = mapTc tcHsType (map getBangType btys)       `thenTc` \ arg_tys ->
125             mk_data_con ex_tyvars ex_theta (map getBangStrictness btys) arg_tys []
126     
127         tc_rec_con ex_tyvars ex_theta fields
128           = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
129             mapTc tc_field (fields `zip` allFieldLabelTags)     `thenTc` \ field_labels_s ->
130             let
131                 field_labels = concat field_labels_s
132                 arg_stricts = [str | (ns, bty) <- fields, 
133                                      let str = getBangStrictness bty, 
134                                      n <- ns    -- One for each.  E.g   x,y,z :: !Int
135                               ]
136             in
137             mk_data_con ex_tyvars ex_theta arg_stricts 
138                         (map fieldLabelType field_labels) field_labels
139     
140         tc_field ((field_label_names, bty), tag)
141           = tcHsType (getBangType bty)                  `thenTc` \ field_ty ->
142             returnTc [mkFieldLabel (getName name) tycon field_ty tag | name <- field_label_names]
143     
144         mk_data_con ex_tyvars ex_theta arg_stricts arg_tys fields
145           = let
146                data_con = mkDataCon name arg_stricts fields
147                                tyvars (thinContext arg_tys ctxt)
148                                ex_tyvars ex_theta
149                                arg_tys
150                                tycon data_con_id data_con_wrap_id
151     
152                data_con_id      = mkDataConId wkr_name data_con
153                data_con_wrap_id = mkDataConWrapId data_con
154             in
155             returnNF_Tc data_con
156
157 -- The context for a data constructor should be limited to
158 -- the type variables mentioned in the arg_tys
159 thinContext arg_tys ctxt
160   = filter in_arg_tys ctxt
161   where
162       arg_tyvars = tyVarsOfTypes arg_tys
163       in_arg_tys pred = not $ isEmptyVarSet $ 
164                         tyVarsOfPred pred `intersectVarSet` arg_tyvars
165 \end{code}
166
167
168 %************************************************************************
169 %*                                                                      *
170 \subsection{Errors and contexts}
171 %*                                                                      *
172 %************************************************************************
173
174
175 \begin{code}
176 exRecConErr name
177   = ptext SLIT("Can't combine named fields with locally-quantified type variables")
178     $$
179     (ptext SLIT("In the declaration of data constructor") <+> ppr name)
180 \end{code}