2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
11 dataConType, dataConSig, dataConName, dataConTag,
12 dataConArgTys, dataConRawArgTys, dataConTyCon,
13 dataConFieldLabels, dataConStrictMarks, dataConSourceArity,
14 dataConNumFields, dataConNumInstArgs, dataConId,
15 isNullaryDataCon, isTupleCon, isUnboxedTupleCon,
19 #include "HsVersions.h"
21 import CmdLineOpts ( opt_DictsStrict )
23 import Type ( Type, ThetaType, TauType,
24 mkSigmaTy, mkFunTys, mkTyConApp,
25 mkTyVarTys, mkDictTy, substTy
27 import TyCon ( TyCon, tyConDataCons, isDataTyCon,
28 isTupleTyCon, isUnboxedTupleTyCon )
29 import Class ( classTyCon )
30 import Name ( Name, NamedThing(..), nameUnique )
31 import Var ( TyVar, Id )
33 import FieldLabel ( FieldLabel )
34 import BasicTypes ( StrictnessMark(..), Arity )
36 import Unique ( Unique, Uniquable(..) )
41 %************************************************************************
43 \subsection{Data constructors}
45 %************************************************************************
49 = MkData { -- Used for data constructors only;
50 -- there *is* no constructor for a newtype
52 dcUnique :: Unique, -- Cached from Name
57 -- data Eq a => T a = forall b. Ord b => MkT a [b]
59 dcType :: Type, -- Type of the constructor
60 -- forall ab . Ord b => a -> [b] -> MkT a
61 -- (this is *not* of the constructor Id:
62 -- see notes after this data type declaration)
64 -- The next six fields express the type of the constructor, in pieces
70 -- dcExTheta = [Ord b]
71 -- dcArgTys = [a,List b]
74 dcTyVars :: [TyVar], -- Type vars and context for the data type decl
77 dcExTyVars :: [TyVar], -- Ditto for the context of the constructor,
78 dcExTheta :: ThetaType, -- the existentially quantified stuff
80 dcArgTys :: [Type], -- Argument types
81 dcTyCon :: TyCon, -- Result tycon
83 -- Now the strictness annotations and field labels of the constructor
84 dcStricts :: [StrictnessMark], -- Strict args, in the same order as the argument types;
85 -- length = dataConNumFields dataCon
87 dcFields :: [FieldLabel], -- Field labels for this constructor, in the
88 -- same order as the argument types;
89 -- length = 0 (if not a record) or dataConSourceArity.
91 -- Finally, the curried function that corresponds to the constructor
92 -- mkT :: forall a b. (Eq a, Ord b) => a -> [b] -> T a
93 -- mkT = /\ab. \deq dord p qs. Con MkT [a, b, dord, p, qs]
94 -- This unfolding is built in MkId.mkDataConId
96 dcId :: Id -- The corresponding Id
102 fIRST_TAG = 1 -- Tags allocated from here for real constructors
105 The dcType field contains the type of the representation of a contructor
106 This may differ from the type of the contructor *Id* (built
107 by MkId.mkDataConId) for two reasons:
108 a) the constructor Id may be overloaded, but the dictionary isn't stored
109 e.g. data Eq a => T a = MkT a a
111 b) the constructor may store an unboxed version of a strict field.
113 Here's an example illustrating both:
114 data Ord a => T a = MkT Int! a
116 T :: Ord a => Int -> a -> T a
118 Trep :: Int# -> a -> T a
119 Actually, the unboxed part isn't implemented yet!
123 instance Eq DataCon where
124 a == b = getUnique a == getUnique b
125 a /= b = getUnique a /= getUnique b
127 instance Ord DataCon where
128 a <= b = getUnique a <= getUnique b
129 a < b = getUnique a < getUnique b
130 a >= b = getUnique a >= getUnique b
131 a > b = getUnique a > getUnique b
132 compare a b = getUnique a `compare` getUnique b
134 instance Uniquable DataCon where
137 instance NamedThing DataCon where
140 instance Outputable DataCon where
141 ppr con = ppr (dataConName con)
143 instance Show DataCon where
144 showsPrec p con = showsPrecSDoc p (ppr con)
149 -> [StrictnessMark] -> [FieldLabel]
150 -> [TyVar] -> ThetaType
151 -> [TyVar] -> ThetaType
152 -> [TauType] -> TyCon
155 -- Can get the tag from the TyCon
157 mkDataCon name arg_stricts fields tyvars theta ex_tyvars ex_theta arg_tys tycon id
158 = ASSERT(length arg_stricts == length arg_tys)
159 -- The 'stricts' passed to mkDataCon are simply those for the
160 -- source-language arguments. We add extra ones for the
161 -- dictionary arguments right here.
164 con = MkData {dcName = name, dcUnique = nameUnique name,
165 dcTyVars = tyvars, dcTheta = theta, dcArgTys = arg_tys,
166 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
167 dcStricts = all_stricts, dcFields = fields,
168 dcTag = tag, dcTyCon = tycon, dcType = ty,
171 all_stricts = (map mk_dict_strict_mark ex_theta) ++ arg_stricts
172 -- Add a strictness flag for the existential dictionary arguments
174 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
175 ty = mkSigmaTy (tyvars ++ ex_tyvars)
177 (mkFunTys arg_tys (mkTyConApp tycon (mkTyVarTys tyvars)))
179 mk_dict_strict_mark (clas,tys)
181 isDataTyCon (classTyCon clas) = MarkedStrict -- Don't mark newtype things as strict!
182 | otherwise = NotMarkedStrict
187 dataConName :: DataCon -> Name
190 dataConTag :: DataCon -> ConTag
193 dataConTyCon :: DataCon -> TyCon
194 dataConTyCon = dcTyCon
196 dataConType :: DataCon -> Type
199 dataConId :: DataCon -> Id
203 dataConFieldLabels :: DataCon -> [FieldLabel]
204 dataConFieldLabels = dcFields
206 dataConStrictMarks :: DataCon -> [StrictnessMark]
207 dataConStrictMarks = dcStricts
209 dataConRawArgTys :: DataCon -> [TauType] -- a function of convenience
210 dataConRawArgTys = dcArgTys
212 dataConSourceArity :: DataCon -> Arity
213 -- Source-level arity of the data constructor
214 dataConSourceArity dc = length (dcArgTys dc)
216 dataConSig :: DataCon -> ([TyVar], ThetaType,
220 dataConSig (MkData {dcTyVars = tyvars, dcTheta = theta,
221 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
222 dcArgTys = arg_tys, dcTyCon = tycon})
223 = (tyvars, theta, ex_tyvars, ex_theta, arg_tys, tycon)
225 dataConArgTys :: DataCon
226 -> [Type] -- Instantiated at these types
227 -- NB: these INCLUDE the existentially quantified arg types
228 -> [Type] -- Needs arguments of these types
229 -- NB: these INCLUDE the existentially quantified dict args
230 -- but EXCLUDE the data-decl context which is discarded
232 dataConArgTys (MkData {dcArgTys = arg_tys, dcTyVars = tyvars,
233 dcExTyVars = ex_tyvars, dcExTheta = ex_theta}) inst_tys
234 = map (substTy (zipVarEnv (tyvars ++ ex_tyvars) inst_tys))
235 ([mkDictTy cls tys | (cls,tys) <- ex_theta] ++ arg_tys)
238 dataConNumFields gives the number of actual fields in the
239 {\em representation} of the data constructor. This may be more than appear
240 in the source code; the extra ones are the existentially quantified
244 -- Number of type-instantiation arguments
245 -- All the remaining arguments of the DataCon are (notionally)
246 -- stored in the DataCon, and are matched in a case expression
247 dataConNumInstArgs (MkData {dcTyVars = tyvars}) = length tyvars
249 dataConNumFields (MkData {dcExTheta = theta, dcArgTys = arg_tys})
250 = length theta + length arg_tys
253 = dataConNumFields con == 0 -- function of convenience
255 isTupleCon :: DataCon -> Bool
256 isTupleCon (MkData {dcTyCon = tc}) = isTupleTyCon tc
258 isUnboxedTupleCon :: DataCon -> Bool
259 isUnboxedTupleCon (MkData {dcTyCon = tc}) = isUnboxedTupleTyCon tc
261 isExistentialDataCon :: DataCon -> Bool
262 isExistentialDataCon (MkData {dcExTyVars = tvs}) = not (null tvs)