2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[TcTyDecls]{Typecheck algebraic datatypes and type synonyms}
7 #include "HsVersions.h"
9 module TcTyDecls ( tcTyDecls ) where
11 import TcMonad -- typechecking monad machinery
12 import AbsSyn -- the stuff being typechecked
14 import AbsUniType ( applyTyCon, mkDataTyCon, mkSynonymTyCon,
15 getUniDataTyCon, isUnboxedDataType,
16 isTyVarTemplateTy, cmpUniTypeMaybeList,
19 import CE ( lookupCE, CE(..) )
20 import CmdLineOpts ( GlobalSwitch(..) )
21 import E ( getE_TCE, getE_CE, plusGVE, nullGVE, GVE(..), E )
22 import ErrUtils ( addShortErrLocLine )
23 import Errors ( confusedNameErr, specDataNoSpecErr, specDataUnboxedErr )
24 import FiniteMap ( FiniteMap, emptyFM, plusFM, singletonFM )
25 import IdInfo ( SpecEnv, mkSpecEnv, SpecInfo(..) )
27 import SpecTyFuns ( specialiseConstrTys )
28 import TCE -- ( nullTCE, unitTCE, lookupTCE, plusTCE, TCE(..), UniqFM )
29 import TVE ( mkTVE, TVE(..) )
30 import TcConDecls ( tcConDecls )
31 import TcMonoType ( tcMonoType )
32 import TcPragmas ( tcDataPragmas, tcTypePragmas )
36 We consult the @CE@/@TCE@ arguments {\em only} to build knots!
38 The resulting @TCE@ has info about the type constructors in it; the
39 @GVE@ has info about their data constructors.
43 -> (Name -> Bool) -- given Name, is it an abstract synonym?
44 -> (Name -> [RenamedDataTypeSig]) -- given Name, get specialisation pragmas
46 -> Baby_TcM (TCE, GVE,
47 FiniteMap TyCon [(Bool, [Maybe UniType])])
49 -- True => imported data types i.e. from interface file
50 -- False => local data types i.e. requsted by source pragmas
52 tcTyDecls e _ _ [] = returnB_Tc (nullTCE, nullGVE, emptyFM)
54 tcTyDecls e is_abs_syn get_spec_sigs (tyd: tyds)
55 = tc_decl tyd `thenB_Tc` \ (tce1, gve1, specs1) ->
56 tcTyDecls e is_abs_syn get_spec_sigs tyds
57 `thenB_Tc` \ (tce2, gve2, specs2) ->
59 tce3 = tce1 `plusTCE` tce2
60 gve3 = gve1 `plusGVE` gve2
61 specs3 = specs1 `plusFM` specs2
63 returnB_Tc (tce3, gve3, specs3)
71 We don't need to substitute here, because the @TCE@s
72 (which are at the top level) cannot contain free type variables.
76 tc_decl (TyData context name@(PreludeTyCon uniq full_name arity True{-"data"-})
77 tyvars con_decls derivings pragmas src_loc)
79 = tc_data_decl uniq name full_name arity tyvars con_decls
80 derivings pragmas src_loc
82 tc_decl (TyData context name@(OtherTyCon uniq full_name arity True{-"data"-} _)
83 tyvars con_decls derivings pragmas src_loc)
85 = tc_data_decl uniq name full_name arity tyvars con_decls
86 derivings pragmas src_loc
88 tc_decl (TyData _ bad_name _ _ _ _ src_loc)
89 = failB_Tc (confusedNameErr "Bad name on a datatype constructor (a Prelude name?)"
92 tc_decl (TySynonym name@(PreludeTyCon uniq full_name arity False{-"type"-})
93 tyvars mono_ty pragmas src_loc)
94 = tc_syn_decl uniq name full_name arity tyvars mono_ty pragmas src_loc
96 tc_decl (TySynonym name@(OtherTyCon uniq full_name arity False{-"type"-} _)
97 tyvars mono_ty pragmas src_loc)
98 = tc_syn_decl uniq name full_name arity tyvars mono_ty pragmas src_loc
100 tc_decl (TySynonym bad_name _ _ _ src_loc)
101 = failB_Tc (confusedNameErr "Bad name on a type-synonym constructor (a Prelude name?)"
105 Real work for @data@ declarations:
107 tc_data_decl uniq name full_name arity tyvars con_decls derivings pragmas src_loc
108 = addSrcLocB_Tc src_loc (
110 (tve, new_tyvars, _) = mkTVE tyvars
111 rec_tycon = lookupTCE rec_tce name
112 -- We know the lookup will succeed, because we are just
113 -- about to put it in the outgoing TCE!
115 spec_sigs = get_spec_sigs name
117 tcSpecDataSigs rec_tce spec_sigs [] `thenB_Tc` \ user_spec_infos ->
119 recoverIgnoreErrorsB_Tc ([], []) (
120 tcDataPragmas rec_tce tve rec_tycon new_tyvars pragmas
121 ) `thenB_Tc` \ (pragma_con_decls, pragma_spec_infos) ->
123 (condecls_to_use, ignore_condecl_errors_if_pragma)
124 = if null pragma_con_decls then
128 then (pragma_con_decls, recoverIgnoreErrorsB_Tc nullGVE)
129 else panic "tcTyDecls:data: user and pragma condecls!"
131 (imported_specs, specinfos_to_use)
132 = if null pragma_spec_infos then
133 (False, user_spec_infos)
135 if null user_spec_infos
136 then (True, pragma_spec_infos)
137 else panic "tcTyDecls:data: user and pragma specinfos!"
139 specenv_to_use = mkSpecEnv specinfos_to_use
141 ignore_condecl_errors_if_pragma
142 (tcConDecls rec_tce tve rec_tycon new_tyvars specenv_to_use condecls_to_use)
145 condecls = map snd gve
147 derived_classes = map (lookupCE rec_ce) derivings
151 full_name arity new_tyvars condecls
153 (null pragma_con_decls)
154 -- if constrs are from pragma we are *abstract*
157 = [(imported_specs, maybe_tys) | (SpecInfo maybe_tys _ _) <- specinfos_to_use]
160 = if null spec_list then
163 singletonFM rec_tycon spec_list
165 returnB_Tc (unitTCE uniq new_tycon, gve, spec_map)
166 -- It's OK to return pragma condecls in gve, even
167 -- though some of those names should be "invisible",
168 -- because the *renamer* is supposed to have dealt with
169 -- naming/scope issues already.
173 Real work for @type@ (synonym) declarations:
175 tc_syn_decl uniq name full_name arity tyvars mono_ty pragmas src_loc
176 = addSrcLocB_Tc src_loc (
178 let (tve, new_tyvars, _) = mkTVE tyvars
180 tcMonoType rec_ce rec_tce tve mono_ty `thenB_Tc` \ expansion ->
182 -- abstractness info either comes from the interface pragmas
183 -- (tcTypePragmas) or from a user-pragma in this module
185 abstract = tcTypePragmas pragmas
188 new_tycon = mkSynonymTyCon uniq full_name
189 arity new_tyvars expansion (not abstract)
191 returnB_Tc (unitTCE uniq new_tycon, nullGVE, emptyFM)
195 %************************************************************************
197 \subsection{Specialisation Signatures for Data Type declarations}
199 %************************************************************************
201 @tcSpecDataSigs@ checks data type specialisation signatures for
202 validity, and returns the list of specialisation requests.
205 tcSpecDataSigs :: TCE
206 -> [RenamedDataTypeSig]
207 -> [(RenamedDataTypeSig,SpecInfo)]
208 -> Baby_TcM [SpecInfo]
210 tcSpecDataSigs tce (s:ss) accum
211 = tc_sig s `thenB_Tc` \ info ->
212 tcSpecDataSigs tce ss ((s,info):accum)
214 tc_sig (SpecDataSig n ty src_loc)
215 = addSrcLocB_Tc src_loc (
217 ty_names = extractMonoTyNames (==) ty
218 (tve,_,_) = mkTVE ty_names
219 fake_CE = panic "tcSpecDataSigs:CE"
221 -- Typecheck specialising type (includes arity check)
222 tcMonoType fake_CE tce tve ty `thenB_Tc` \ tau_ty ->
224 (_,ty_args,_) = getUniDataTyCon tau_ty
225 is_unboxed_or_tyvar ty = isUnboxedDataType ty || isTyVarTemplateTy ty
227 -- Check at least one unboxed type in specialisation
228 checkB_Tc (not (any isUnboxedDataType ty_args))
229 (specDataNoSpecErr n ty_args src_loc) `thenB_Tc_`
231 -- Check all types are unboxed or tyvars
232 -- (specific boxed types are redundant)
233 checkB_Tc (not (all is_unboxed_or_tyvar ty_args))
234 (specDataUnboxedErr n ty_args src_loc) `thenB_Tc_`
237 maybe_tys = specialiseConstrTys ty_args
239 returnB_Tc (SpecInfo maybe_tys 0 (panic "SpecData:SpecInfo:SpecId"))
242 tcSpecDataSigs tce [] accum
243 = -- Remove any duplicates from accumulated specinfos
244 getSwitchCheckerB_Tc `thenB_Tc` \ sw_chkr ->
246 (if sw_chkr SpecialiseTrace && not (null duplicates) then
247 pprTrace "Duplicate SPECIALIZE data pragmas:\n"
248 (ppAboves (map specmsg sep_dups))
251 (if sw_chkr SpecialiseTrace && not (null spec_infos) then
252 pprTrace "Specialising "
253 (ppHang (ppCat [ppr PprDebug name, ppStr "at types:"])
254 4 (ppAboves (map pp_spec spec_infos)))
258 returnB_Tc (spec_infos)
261 spec_infos = map (snd . head) equiv
263 equiv = equivClasses cmp_info accum
264 duplicates = filter (not . singleton) equiv
266 cmp_info (_, SpecInfo tys1 _ _) (_, SpecInfo tys2 _ _)
267 = cmpUniTypeMaybeList tys1 tys2
272 sep_dups = tail (concat (map ((:) Nothing . map Just) duplicates))
273 specmsg (Just (SpecDataSig _ ty locn, _))
274 = addShortErrLocLine locn ( \ sty -> ppr sty ty ) PprDebug
278 ((SpecDataSig name _ _, _):_) = accum
279 pp_spec (SpecInfo tys _ _) = ppInterleave ppNil [pprMaybeTy PprDebug ty | ty <- tys]