2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[DataCon]{@DataCon@: Data Constructors}
11 dataConType, dataConSig, dataConName, dataConTag,
12 dataConOrigArgTys, dataConArgTys, dataConRawArgTys, dataConTyCon,
13 dataConFieldLabels, dataConStrictMarks, dataConSourceArity,
14 dataConNumFields, dataConNumInstArgs, dataConId, dataConRepStrictness,
15 isNullaryDataCon, isTupleCon, isUnboxedTupleCon,
18 StrictnessMark(..), -- Representation visible to MkId only
19 markedStrict, notMarkedStrict, markedUnboxed, maybeMarkedUnboxed
22 #include "HsVersions.h"
24 import {-# SOURCE #-} Subst( substTy, mkTyVarSubst )
26 import CmdLineOpts ( opt_DictsStrict )
28 import Type ( Type, ThetaType, TauType,
29 mkSigmaTy, mkFunTys, mkTyConApp,
31 splitAlgTyConApp_maybe
34 import TyCon ( TyCon, tyConDataCons, isDataTyCon,
35 isTupleTyCon, isUnboxedTupleTyCon )
36 import Class ( classTyCon )
37 import Name ( Name, NamedThing(..), nameUnique, isLocallyDefinedName )
38 import Var ( TyVar, Id )
39 import FieldLabel ( FieldLabel )
40 import BasicTypes ( Arity )
41 import Demand ( Demand, wwStrict, wwLazy )
43 import Unique ( Unique, Uniquable(..) )
44 import CmdLineOpts ( opt_UnboxStrictFields )
51 %************************************************************************
53 \subsection{Data constructors}
55 %************************************************************************
59 = MkData { -- Used for data constructors only;
60 -- there *is* no constructor for a newtype
62 dcUnique :: Unique, -- Cached from Name
67 -- data Eq a => T a = forall b. Ord b => MkT a [b]
69 dcType :: Type, -- Type of the constructor
70 -- forall ab . Ord b => a -> [b] -> MkT a
71 -- (this is *not* of the constructor Id:
72 -- see notes after this data type declaration)
74 -- The next six fields express the type of the constructor, in pieces
80 -- dcExTheta = [Ord b]
81 -- dcOrigArgTys = [a,List b]
84 dcTyVars :: [TyVar], -- Type vars and context for the data type decl
87 dcExTyVars :: [TyVar], -- Ditto for the context of the constructor,
88 dcExTheta :: ThetaType, -- the existentially quantified stuff
90 dcOrigArgTys :: [Type], -- Original argument types
91 -- (before unboxing and flattening of
93 dcRepArgTys :: [Type], -- Constructor Argument types
94 dcTyCon :: TyCon, -- Result tycon
96 -- Now the strictness annotations and field labels of the constructor
97 dcUserStricts :: [StrictnessMark],
98 -- Strictness annotations, as placed on the data type defn,
99 -- in the same order as the argument types;
100 -- length = dataConNumFields dataCon
102 dcRealStricts :: [StrictnessMark],
103 -- Strictness annotations as deduced by the compiler. May
104 -- include some MarkedUnboxed fields that are MarkedStrict
106 -- length = dataConNumFields dataCon
108 dcFields :: [FieldLabel],
109 -- Field labels for this constructor, in the
110 -- same order as the argument types;
111 -- length = 0 (if not a record) or dataConSourceArity.
113 -- Finally, the curried function that corresponds to the constructor
114 -- mkT :: forall a b. (Eq a, Ord b) => a -> [b] -> T a
115 -- mkT = /\ab. \deq dord p qs. Con MkT [a, b, dord, p, qs]
116 -- This unfolding is built in MkId.mkDataConId
118 dcId :: Id -- The corresponding Id
124 fIRST_TAG = 1 -- Tags allocated from here for real constructors
127 The dcType field contains the type of the representation of a contructor
128 This may differ from the type of the contructor *Id* (built
129 by MkId.mkDataConId) for two reasons:
130 a) the constructor Id may be overloaded, but the dictionary isn't stored
131 e.g. data Eq a => T a = MkT a a
133 b) the constructor may store an unboxed version of a strict field.
135 Here's an example illustrating both:
136 data Ord a => T a = MkT Int! a
138 T :: Ord a => Int -> a -> T a
140 Trep :: Int# -> a -> T a
141 Actually, the unboxed part isn't implemented yet!
144 %************************************************************************
146 \subsection{Strictness indication}
148 %************************************************************************
151 data StrictnessMark = MarkedStrict
152 | MarkedUnboxed DataCon [Type]
155 markedStrict = MarkedStrict
156 notMarkedStrict = NotMarkedStrict
157 markedUnboxed = MarkedUnboxed (panic "markedUnboxed1") (panic "markedUnboxed2")
159 maybeMarkedUnboxed (MarkedUnboxed dc tys) = Just (dc,tys)
160 maybeMarkedUnboxed other = Nothing
164 %************************************************************************
166 \subsection{Instances}
168 %************************************************************************
171 instance Eq DataCon where
172 a == b = getUnique a == getUnique b
173 a /= b = getUnique a /= getUnique b
175 instance Ord DataCon where
176 a <= b = getUnique a <= getUnique b
177 a < b = getUnique a < getUnique b
178 a >= b = getUnique a >= getUnique b
179 a > b = getUnique a > getUnique b
180 compare a b = getUnique a `compare` getUnique b
182 instance Uniquable DataCon where
185 instance NamedThing DataCon where
188 instance Outputable DataCon where
189 ppr con = ppr (dataConName con)
191 instance Show DataCon where
192 showsPrec p con = showsPrecSDoc p (ppr con)
196 %************************************************************************
198 \subsection{Consruction}
200 %************************************************************************
204 -> [StrictnessMark] -> [FieldLabel]
205 -> [TyVar] -> ThetaType
206 -> [TyVar] -> ThetaType
207 -> [TauType] -> TyCon
210 -- Can get the tag from the TyCon
212 mkDataCon name arg_stricts fields tyvars theta ex_tyvars ex_theta orig_arg_tys tycon id
213 = ASSERT(length arg_stricts == length orig_arg_tys)
214 -- The 'stricts' passed to mkDataCon are simply those for the
215 -- source-language arguments. We add extra ones for the
216 -- dictionary arguments right here.
219 con = MkData {dcName = name, dcUnique = nameUnique name,
220 dcTyVars = tyvars, dcTheta = theta,
221 dcOrigArgTys = orig_arg_tys,
222 dcRepArgTys = rep_arg_tys,
223 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
224 dcRealStricts = all_stricts, dcUserStricts = user_stricts,
225 dcFields = fields, dcTag = tag, dcTyCon = tycon, dcType = ty,
228 (real_arg_stricts, strict_arg_tyss)
229 = unzip (zipWith (unbox_strict_arg_ty tycon) arg_stricts orig_arg_tys)
230 rep_arg_tys = concat strict_arg_tyss
232 ex_dict_stricts = map mk_dict_strict_mark ex_theta
233 -- Add a strictness flag for the existential dictionary arguments
234 all_stricts = ex_dict_stricts ++ real_arg_stricts
235 user_stricts = ex_dict_stricts ++ arg_stricts
237 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
238 ty = mkSigmaTy (tyvars ++ ex_tyvars)
240 (mkFunTys rep_arg_tys
241 (mkTyConApp tycon (mkTyVarTys tyvars)))
243 mk_dict_strict_mark (clas,tys)
245 -- Don't mark newtype things as strict!
246 isDataTyCon (classTyCon clas) = MarkedStrict
247 | otherwise = NotMarkedStrict
249 -- We attempt to unbox/unpack a strict field when either:
250 -- (i) The tycon is imported, and the field is marked '! !', or
251 -- (ii) The tycon is defined in this module, the field is marked '!',
252 -- and the -funbox-strict-fields flag is on.
254 -- This ensures that if we compile some modules with -funbox-strict-fields and
255 -- some without, the compiler doesn't get confused about the constructor
258 unbox_strict_arg_ty :: TyCon -> StrictnessMark -> Type -> (StrictnessMark, [Type])
259 unbox_strict_arg_ty tycon NotMarkedStrict ty
260 = (NotMarkedStrict, [ty])
261 unbox_strict_arg_ty tycon MarkedStrict ty
262 | not opt_UnboxStrictFields
263 || not (isLocallyDefinedName (getName tycon)) = (MarkedStrict, [ty])
264 unbox_strict_arg_ty tycon marked_unboxed ty
265 -- MarkedUnboxed || (MarkedStrict && opt_UnboxStrictFields && not imported)
266 = case splitAlgTyConApp_maybe ty of
268 -> panic (showSDoc (hcat [
269 text "unbox_strict_arg_ty: constructors for ",
271 text " not available."
273 Just (tycon,ty_args,[con])
274 -> case maybe_unpack_fields emptyUniqSet
275 (zip (dataConOrigArgTys con ty_args)
278 Nothing -> (MarkedStrict, [ty])
279 Just tys -> (MarkedUnboxed con tys, tys)
280 _ -> (MarkedStrict, [ty])
282 -- bail out if we encounter the same tycon twice. This avoids problems like
287 -- where no useful unpacking can be done.
289 maybe_unpack_field :: UniqSet TyCon -> Type -> StrictnessMark -> Maybe [Type]
290 maybe_unpack_field set ty NotMarkedStrict
292 maybe_unpack_field set ty MarkedStrict | not opt_UnboxStrictFields
294 maybe_unpack_field set ty strict
295 = case splitAlgTyConApp_maybe ty of
296 Just (tycon,ty_args,[con])
298 | tycon `elementOfUniqSet` set -> Nothing
299 -- don't unpack constructors with existential tyvars
300 | not (null ex_tyvars) -> Nothing
303 let set' = addOneToUniqSet set tycon in
304 maybe_unpack_fields set'
305 (zip (dataConOrigArgTys con ty_args)
307 where (_, _, ex_tyvars, _, _, _) = dataConSig con
310 maybe_unpack_fields :: UniqSet TyCon -> [(Type,StrictnessMark)] -> Maybe [Type]
311 maybe_unpack_fields set tys
312 | all isJust unpacked_fields = Just (concat (catMaybes unpacked_fields))
313 | otherwise = Nothing
314 where unpacked_fields = map (\(ty,str) -> maybe_unpack_field set ty str) tys
319 dataConName :: DataCon -> Name
322 dataConTag :: DataCon -> ConTag
325 dataConTyCon :: DataCon -> TyCon
326 dataConTyCon = dcTyCon
328 dataConType :: DataCon -> Type
331 dataConId :: DataCon -> Id
335 dataConFieldLabels :: DataCon -> [FieldLabel]
336 dataConFieldLabels = dcFields
338 dataConStrictMarks :: DataCon -> [StrictnessMark]
339 dataConStrictMarks = dcRealStricts
341 dataConRawArgTys :: DataCon -> [TauType] -- a function of convenience
342 dataConRawArgTys = dcRepArgTys
344 dataConSourceArity :: DataCon -> Arity
345 -- Source-level arity of the data constructor
346 dataConSourceArity dc = length (dcOrigArgTys dc)
348 dataConRepStrictness :: DataCon -> [Demand]
349 -- Give the demands on the arguments of a
350 -- Core constructor application (Con dc args)
351 dataConRepStrictness dc
352 = go (dcRealStricts dc)
355 go (MarkedStrict : ss) = wwStrict : go ss
356 go (NotMarkedStrict : ss) = wwLazy : go ss
357 go (MarkedUnboxed con _ : ss) = go (dcRealStricts con ++ ss)
359 dataConSig :: DataCon -> ([TyVar], ThetaType,
363 dataConSig (MkData {dcTyVars = tyvars, dcTheta = theta,
364 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
365 dcOrigArgTys = arg_tys, dcTyCon = tycon})
366 = (tyvars, theta, ex_tyvars, ex_theta, arg_tys, tycon)
368 dataConArgTys, dataConOrigArgTys :: DataCon
369 -> [Type] -- Instantiated at these types
370 -- NB: these INCLUDE the existentially quantified arg types
371 -> [Type] -- Needs arguments of these types
372 -- NB: these INCLUDE the existentially quantified dict args
373 -- but EXCLUDE the data-decl context which is discarded
375 dataConArgTys (MkData {dcRepArgTys = arg_tys, dcTyVars = tyvars,
376 dcExTyVars = ex_tyvars, dcExTheta = ex_theta}) inst_tys
377 = map (substTy (mkTyVarSubst (tyvars ++ ex_tyvars) inst_tys))
378 ([mkDictTy cls tys | (cls,tys) <- ex_theta] ++ arg_tys)
380 dataConOrigArgTys (MkData {dcOrigArgTys = arg_tys, dcTyVars = tyvars,
381 dcExTyVars = ex_tyvars, dcExTheta = ex_theta}) inst_tys
382 = map (substTy (mkTyVarSubst (tyvars ++ ex_tyvars) inst_tys))
383 ([mkDictTy cls tys | (cls,tys) <- ex_theta] ++ arg_tys)
386 dataConNumFields gives the number of actual fields in the
387 {\em representation} of the data constructor. This may be more than appear
388 in the source code; the extra ones are the existentially quantified
392 -- Number of type-instantiation arguments
393 -- All the remaining arguments of the DataCon are (notionally)
394 -- stored in the DataCon, and are matched in a case expression
395 dataConNumInstArgs (MkData {dcTyVars = tyvars}) = length tyvars
397 dataConNumFields (MkData {dcExTheta = theta, dcRepArgTys = arg_tys})
398 = length theta + length arg_tys
401 = dataConNumFields con == 0 -- function of convenience
403 isTupleCon :: DataCon -> Bool
404 isTupleCon (MkData {dcTyCon = tc}) = isTupleTyCon tc
406 isUnboxedTupleCon :: DataCon -> Bool
407 isUnboxedTupleCon (MkData {dcTyCon = tc}) = isUnboxedTupleTyCon tc
409 isExistentialDataCon :: DataCon -> Bool
410 isExistentialDataCon (MkData {dcExTyVars = tvs}) = not (null tvs)