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
54 dcType :: Type, -- Type of the constructor (see notes below)
56 dcTyVars :: [TyVar], -- Type vars and context for the data type decl
59 dcExTyVars :: [TyVar], -- Ditto for the context of the constructor,
60 dcExTheta :: ThetaType, -- the existentially quantified stuff
62 dcArgTys :: [Type], -- Argument types
63 dcTyCon :: TyCon, -- Result tycon
65 dcStricts :: [StrictnessMark], -- Strict args, in the same order as the argument types;
66 -- length = dataConNumFields dataCon
68 dcFields :: [FieldLabel], -- Field labels for this constructor, in the
69 -- same order as the argument types;
70 -- length = 0 (if not a record) or dataConSourceArity.
72 dcId :: Id -- The corresponding Id
78 fIRST_TAG = 1 -- Tags allocated from here for real constructors
81 The dcType field contains the type of the representation of a contructor
82 This may differ from the type of the contructor *Id* (built
83 by MkId.mkDataConId) for two reasons:
84 a) the constructor Id may be overloaded, but the dictionary isn't stored
85 e.g. data Eq a => T a = MkT a a
87 b) the constructor may store an unboxed version of a strict field.
89 Here's an example illustrating both:
90 data Ord a => T a = MkT Int! a
92 T :: Ord a => Int -> a -> T a
94 Trep :: Int# -> a -> T a
95 Actually, the unboxed part isn't implemented yet!
99 instance Eq DataCon where
100 a == b = getUnique a == getUnique b
101 a /= b = getUnique a /= getUnique b
103 instance Ord DataCon where
104 a <= b = getUnique a <= getUnique b
105 a < b = getUnique a < getUnique b
106 a >= b = getUnique a >= getUnique b
107 a > b = getUnique a > getUnique b
108 compare a b = getUnique a `compare` getUnique b
110 instance Uniquable DataCon where
113 instance NamedThing DataCon where
116 instance Outputable DataCon where
117 ppr con = ppr (dataConName con)
119 instance Show DataCon where
120 showsPrec p con = showsPrecSDoc p (ppr con)
125 -> [StrictnessMark] -> [FieldLabel]
126 -> [TyVar] -> ThetaType
127 -> [TyVar] -> ThetaType
128 -> [TauType] -> TyCon
131 -- Can get the tag from the TyCon
133 mkDataCon name arg_stricts fields tyvars theta ex_tyvars ex_theta arg_tys tycon id
134 = ASSERT(length arg_stricts == length arg_tys)
135 -- The 'stricts' passed to mkDataCon are simply those for the
136 -- source-language arguments. We add extra ones for the
137 -- dictionary arguments right here.
140 con = MkData {dcName = name, dcUnique = nameUnique name,
141 dcTyVars = tyvars, dcTheta = theta, dcArgTys = arg_tys,
142 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
143 dcStricts = all_stricts, dcFields = fields,
144 dcTag = tag, dcTyCon = tycon, dcType = ty,
147 all_stricts = (map mk_dict_strict_mark ex_theta) ++ arg_stricts
148 -- Add a strictness flag for the existential dictionary arguments
150 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
151 ty = mkSigmaTy (tyvars ++ ex_tyvars)
153 (mkFunTys arg_tys (mkTyConApp tycon (mkTyVarTys tyvars)))
155 mk_dict_strict_mark (clas,tys)
157 isDataTyCon (classTyCon clas) = MarkedStrict -- Don't mark newtype things as strict!
158 | otherwise = NotMarkedStrict
163 dataConName :: DataCon -> Name
166 dataConTag :: DataCon -> ConTag
169 dataConTyCon :: DataCon -> TyCon
170 dataConTyCon = dcTyCon
172 dataConType :: DataCon -> Type
175 dataConId :: DataCon -> Id
179 dataConFieldLabels :: DataCon -> [FieldLabel]
180 dataConFieldLabels = dcFields
182 dataConStrictMarks :: DataCon -> [StrictnessMark]
183 dataConStrictMarks = dcStricts
185 dataConRawArgTys :: DataCon -> [TauType] -- a function of convenience
186 dataConRawArgTys = dcArgTys
188 dataConSourceArity :: DataCon -> Arity
189 -- Source-level arity of the data constructor
190 dataConSourceArity dc = length (dcArgTys dc)
192 dataConSig :: DataCon -> ([TyVar], ThetaType,
196 dataConSig (MkData {dcTyVars = tyvars, dcTheta = theta,
197 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
198 dcArgTys = arg_tys, dcTyCon = tycon})
199 = (tyvars, theta, ex_tyvars, ex_theta, arg_tys, tycon)
201 dataConArgTys :: DataCon
202 -> [Type] -- Instantiated at these types
203 -- NB: these INCLUDE the existentially quantified arg types
204 -> [Type] -- Needs arguments of these types
205 -- NB: these INCLUDE the existentially quantified dict args
206 -- but EXCLUDE the data-decl context which is discarded
208 dataConArgTys (MkData {dcArgTys = arg_tys, dcTyVars = tyvars,
209 dcExTyVars = ex_tyvars, dcExTheta = ex_theta}) inst_tys
210 = map (substTy (zipVarEnv (tyvars ++ ex_tyvars) inst_tys))
211 ([mkDictTy cls tys | (cls,tys) <- ex_theta] ++ arg_tys)
214 dataConNumFields gives the number of actual fields in the
215 {\em representation} of the data constructor. This may be more than appear
216 in the source code; the extra ones are the existentially quantified
220 -- Number of type-instantiation arguments
221 -- All the remaining arguments of the DataCon are (notionally)
222 -- stored in the DataCon, and are matched in a case expression
223 dataConNumInstArgs (MkData {dcTyVars = tyvars}) = length tyvars
225 dataConNumFields (MkData {dcExTheta = theta, dcArgTys = arg_tys})
226 = length theta + length arg_tys
229 = dataConNumFields con == 0 -- function of convenience
231 isTupleCon :: DataCon -> Bool
232 isTupleCon (MkData {dcTyCon = tc}) = isTupleTyCon tc
234 isUnboxedTupleCon :: DataCon -> Bool
235 isUnboxedTupleCon (MkData {dcTyCon = tc}) = isUnboxedTupleTyCon tc
237 isExistentialDataCon :: DataCon -> Bool
238 isExistentialDataCon (MkData {dcExTyVars = tvs}) = not (null tvs)