2 % (c) The AQUA Project, Glasgow University, 1998
4 \section[StdIdInfo]{Standard unfoldings}
6 This module contains definitions for the IdInfo for things that
7 have a standard form, namely:
11 * method and superclass selectors
12 * primitive operations
16 mkDictFunId, mkDefaultMethodId,
19 mkDataConId, mkDataConWrapId,
21 mkPrimOpId, mkCCallOpId,
23 -- And some particular Ids; see below for why they are wired in
25 unsafeCoerceId, realWorldPrimId,
26 eRROR_ID, rEC_SEL_ERROR_ID, pAT_ERROR_ID, rEC_CON_ERROR_ID,
27 rEC_UPD_ERROR_ID, iRREFUT_PAT_ERROR_ID, nON_EXHAUSTIVE_GUARDS_ERROR_ID,
28 nO_METHOD_BINDING_ERROR_ID, aBSENT_ERROR_ID, pAR_ERROR_ID
31 #include "HsVersions.h"
34 import BasicTypes ( Arity )
35 import TysPrim ( openAlphaTyVars, alphaTyVar, alphaTy,
36 intPrimTy, realWorldStatePrimTy
38 import TysWiredIn ( charTy, mkListTy )
39 import PrelNames ( pREL_ERR, pREL_GHC )
40 import PrelRules ( primOpRule )
41 import Rules ( addRule )
42 import Type ( Type, ThetaType, mkDictTy, mkPredTys, mkTyConApp, mkTyVarTys,
43 mkFunTys, mkFunTy, mkSigmaTy, splitSigmaTy,
44 isUnLiftedType, mkForAllTys, mkTyVarTy, tyVarsOfType,
45 splitFunTys, splitForAllTys, mkPredTy
47 import Module ( Module )
48 import CoreUtils ( exprType, mkInlineMe )
49 import CoreUnfold ( mkTopUnfolding, mkCompulsoryUnfolding, mkOtherCon )
50 import Literal ( Literal(..) )
51 import TyCon ( TyCon, isNewTyCon, tyConTyVars, tyConDataCons,
52 tyConTheta, isProductTyCon, isDataTyCon )
53 import Class ( Class, classTyCon, classTyVars, classSelIds )
54 import Var ( Id, TyVar )
55 import VarSet ( isEmptyVarSet )
56 import Name ( mkWiredInName, mkCCallName, Name )
57 import OccName ( mkVarOcc )
58 import PrimOp ( PrimOp(DataToTagOp, CCallOp),
59 primOpSig, mkPrimOpIdName,
62 import Demand ( wwStrict, wwPrim, mkStrictnessInfo )
63 import DataCon ( DataCon, StrictnessMark(..),
64 dataConFieldLabels, dataConRepArity, dataConTyCon,
65 dataConArgTys, dataConRepType, dataConRepStrictness,
66 dataConInstOrigArgTys,
67 dataConName, dataConTheta,
68 dataConSig, dataConStrictMarks, dataConId,
69 maybeMarkedUnboxed, splitProductType_maybe
71 import Id ( idType, mkGlobalId, mkVanillaGlobal,
72 mkTemplateLocals, mkTemplateLocalsNum,
73 mkTemplateLocal, idCprInfo
75 import IdInfo ( IdInfo, noCafNoTyGenIdInfo,
76 exactArity, setUnfoldingInfo, setCprInfo,
77 setArityInfo, setSpecInfo, setCgInfo,
78 mkStrictnessInfo, setStrictnessInfo,
79 GlobalIdDetails(..), CafInfo(..), CprInfo(..),
80 CgInfo(..), setCgArity
82 import FieldLabel ( mkFieldLabel, fieldLabelName,
83 firstFieldLabelTag, allFieldLabelTags, fieldLabelType
88 import Maybe ( isJust )
90 import ListSetOps ( assoc, assocMaybe )
91 import UnicodeUtil ( stringToUtf8 )
95 %************************************************************************
97 \subsection{Wired in Ids}
99 %************************************************************************
103 = [ -- These error-y things are wired in because we don't yet have
104 -- a way to express in an interface file that the result type variable
105 -- is 'open'; that is can be unified with an unboxed type
107 -- [The interface file format now carry such information, but there's
108 -- no way yet of expressing at the definition site for these
110 -- functions that they have an 'open' result type. -- sof 1/99]
114 , iRREFUT_PAT_ERROR_ID
115 , nON_EXHAUSTIVE_GUARDS_ERROR_ID
116 , nO_METHOD_BINDING_ERROR_ID
122 -- These three can't be defined in Haskell
129 %************************************************************************
131 \subsection{Data constructors}
133 %************************************************************************
136 mkDataConId :: Name -> DataCon -> Id
137 -- Makes the *worker* for the data constructor; that is, the function
138 -- that takes the reprsentation arguments and builds the constructor.
139 mkDataConId work_name data_con
140 = mkGlobalId (DataConId data_con) work_name (dataConRepType data_con) info
142 info = noCafNoTyGenIdInfo
144 `setArityInfo` exactArity arity
145 `setStrictnessInfo` strict_info
146 `setCprInfo` cpr_info
148 arity = dataConRepArity data_con
150 strict_info = mkStrictnessInfo (dataConRepStrictness data_con, False)
152 tycon = dataConTyCon data_con
153 cpr_info | isProductTyCon tycon &&
156 arity <= mAX_CPR_SIZE = ReturnsCPR
157 | otherwise = NoCPRInfo
158 -- ReturnsCPR is only true for products that are real data types;
159 -- that is, not unboxed tuples or newtypes
161 mAX_CPR_SIZE :: Arity
163 -- We do not treat very big tuples as CPR-ish:
164 -- a) for a start we get into trouble because there aren't
165 -- "enough" unboxed tuple types (a tiresome restriction,
167 -- b) more importantly, big unboxed tuples get returned mainly
168 -- on the stack, and are often then allocated in the heap
169 -- by the caller. So doing CPR for them may in fact make
173 The wrapper for a constructor is an ordinary top-level binding that evaluates
174 any strict args, unboxes any args that are going to be flattened, and calls
177 We're going to build a constructor that looks like:
179 data (Data a, C b) => T a b = T1 !a !Int b
182 \d1::Data a, d2::C b ->
183 \p q r -> case p of { p ->
185 Con T1 [a,b] [p,q,r]}}
189 * d2 is thrown away --- a context in a data decl is used to make sure
190 one *could* construct dictionaries at the site the constructor
191 is used, but the dictionary isn't actually used.
193 * We have to check that we can construct Data dictionaries for
194 the types a and Int. Once we've done that we can throw d1 away too.
196 * We use (case p of q -> ...) to evaluate p, rather than "seq" because
197 all that matters is that the arguments are evaluated. "seq" is
198 very careful to preserve evaluation order, which we don't need
201 You might think that we could simply give constructors some strictness
202 info, like PrimOps, and let CoreToStg do the let-to-case transformation.
203 But we don't do that because in the case of primops and functions strictness
204 is a *property* not a *requirement*. In the case of constructors we need to
205 do something active to evaluate the argument.
207 Making an explicit case expression allows the simplifier to eliminate
208 it in the (common) case where the constructor arg is already evaluated.
211 mkDataConWrapId data_con
214 wrap_id = mkGlobalId (DataConWrapId data_con) (dataConName data_con) wrap_ty info
215 work_id = dataConId data_con
217 info = noCafNoTyGenIdInfo
218 `setUnfoldingInfo` mkTopUnfolding (mkInlineMe wrap_rhs)
219 `setCprInfo` cpr_info
220 -- The Cpr info can be important inside INLINE rhss, where the
221 -- wrapper constructor isn't inlined
223 -- The NoCaf-ness is set by noCafNoTyGenIdInfo
224 `setArityInfo` exactArity arity
225 -- It's important to specify the arity, so that partial
226 -- applications are treated as values
228 wrap_ty = mkForAllTys all_tyvars $
232 cpr_info = idCprInfo work_id
234 wrap_rhs | isNewTyCon tycon
235 = ASSERT( null ex_tyvars && null ex_dict_args && length orig_arg_tys == 1 )
236 -- No existentials on a newtype, but it can have a context
237 -- e.g. newtype Eq a => T a = MkT (...)
239 mkLams tyvars $ mkLams dict_args $ Lam id_arg1 $
240 Note (Coerce result_ty (head orig_arg_tys)) (Var id_arg1)
242 | null dict_args && all not_marked_strict strict_marks
243 = Var work_id -- The common case. Not only is this efficient,
244 -- but it also ensures that the wrapper is replaced
245 -- by the worker even when there are no args.
249 -- This is really important in rule matching,
250 -- (We could match on the wrappers,
251 -- but that makes it less likely that rules will match
252 -- when we bring bits of unfoldings together.)
254 -- NB: because of this special case, (map (:) ys) turns into
255 -- (map $w: ys), and thence into (map (\x xs. $w: x xs) ys)
256 -- in core-to-stg. The top-level defn for (:) is never used.
257 -- This is somewhat of a bore, but I'm currently leaving it
258 -- as is, so that there still is a top level curried (:) for
259 -- the interpreter to call.
262 = mkLams all_tyvars $ mkLams dict_args $
263 mkLams ex_dict_args $ mkLams id_args $
264 foldr mk_case con_app
265 (zip (ex_dict_args++id_args) strict_marks) i3 []
267 con_app i rep_ids = mkApps (Var work_id)
268 (map varToCoreExpr (all_tyvars ++ reverse rep_ids))
270 (tyvars, theta, ex_tyvars, ex_theta, orig_arg_tys, tycon) = dataConSig data_con
271 all_tyvars = tyvars ++ ex_tyvars
273 dict_tys = mkPredTys theta
274 ex_dict_tys = mkPredTys ex_theta
275 all_arg_tys = dict_tys ++ ex_dict_tys ++ orig_arg_tys
276 result_ty = mkTyConApp tycon (mkTyVarTys tyvars)
278 mkLocals i tys = (zipWith mkTemplateLocal [i..i+n-1] tys, i+n)
282 (dict_args, i1) = mkLocals 1 dict_tys
283 (ex_dict_args,i2) = mkLocals i1 ex_dict_tys
284 (id_args,i3) = mkLocals i2 orig_arg_tys
286 (id_arg1:_) = id_args -- Used for newtype only
288 strict_marks = dataConStrictMarks data_con
289 not_marked_strict NotMarkedStrict = True
290 not_marked_strict other = False
294 :: (Id, StrictnessMark) -- arg, strictness
295 -> (Int -> [Id] -> CoreExpr) -- body
296 -> Int -- next rep arg id
297 -> [Id] -- rep args so far
299 mk_case (arg,strict) body i rep_args
301 NotMarkedStrict -> body i (arg:rep_args)
303 | isUnLiftedType (idType arg) -> body i (arg:rep_args)
305 Case (Var arg) arg [(DEFAULT,[], body i (arg:rep_args))]
307 MarkedUnboxed con tys ->
308 Case (Var arg) arg [(DataAlt con, con_args,
309 body i' (reverse con_args++rep_args))]
311 (con_args,i') = mkLocals i tys
315 %************************************************************************
317 \subsection{Record selectors}
319 %************************************************************************
321 We're going to build a record selector unfolding that looks like this:
323 data T a b c = T1 { ..., op :: a, ...}
324 | T2 { ..., op :: a, ...}
327 sel = /\ a b c -> \ d -> case d of
332 Similarly for newtypes
334 newtype N a = MkN { unN :: a->a }
337 unN n = coerce (a->a) n
339 We need to take a little care if the field has a polymorphic type:
341 data R = R { f :: forall a. a->a }
345 f :: forall a. R -> a -> a
346 f = /\ a \ r = case r of
349 (not f :: R -> forall a. a->a, which gives the type inference mechanism
350 problems at call sites)
352 Similarly for newtypes
354 newtype N = MkN { unN :: forall a. a->a }
356 unN :: forall a. N -> a -> a
357 unN = /\a -> \n:N -> coerce (a->a) n
360 mkRecordSelId tycon field_label unpack_id unpackUtf8_id
361 -- Assumes that all fields with the same field label have the same type
363 -- Annoyingly, we have to pass in the unpackCString# Id, because
364 -- we can't conjure it up out of thin air
367 sel_id = mkGlobalId (RecordSelId field_label) (fieldLabelName field_label) selector_ty info
368 field_ty = fieldLabelType field_label
369 data_cons = tyConDataCons tycon
370 tyvars = tyConTyVars tycon -- These scope over the types in
371 -- the FieldLabels of constructors of this type
372 data_ty = mkTyConApp tycon tyvar_tys
373 tyvar_tys = mkTyVarTys tyvars
375 tycon_theta = tyConTheta tycon -- The context on the data decl
376 -- eg data (Eq a, Ord b) => T a b = ...
377 dict_tys = [mkPredTy pred | pred <- tycon_theta,
379 needed_dict pred = or [ pred `elem` (dataConTheta dc)
380 | (DataAlt dc, _, _) <- the_alts]
381 n_dict_tys = length dict_tys
383 (field_tyvars,field_theta,field_tau) = splitSigmaTy field_ty
384 field_dict_tys = map mkPredTy field_theta
385 n_field_dict_tys = length field_dict_tys
386 -- If the field has a universally quantified type we have to
387 -- be a bit careful. Suppose we have
388 -- data R = R { op :: forall a. Foo a => a -> a }
389 -- Then we can't give op the type
390 -- op :: R -> forall a. Foo a => a -> a
391 -- because the typechecker doesn't understand foralls to the
392 -- right of an arrow. The "right" type to give it is
393 -- op :: forall a. Foo a => R -> a -> a
394 -- But then we must generate the right unfolding too:
395 -- op = /\a -> \dfoo -> \ r ->
398 -- Note that this is exactly the type we'd infer from a user defn
401 -- Very tiresomely, the selectors are (unnecessarily!) overloaded over
402 -- just the dictionaries in the types of the constructors that contain
403 -- the relevant field. Urgh.
404 -- NB: this code relies on the fact that DataCons are quantified over
405 -- the identical type variables as their parent TyCon
408 selector_ty = mkForAllTys tyvars $ mkForAllTys field_tyvars $
409 mkFunTys dict_tys $ mkFunTys field_dict_tys $
410 mkFunTy data_ty field_tau
412 arity = 1 + n_dict_tys + n_field_dict_tys
413 info = noCafNoTyGenIdInfo
414 `setCgInfo` (CgInfo arity caf_info)
415 `setArityInfo` exactArity arity
416 `setUnfoldingInfo` unfolding
417 -- ToDo: consider adding further IdInfo
419 unfolding = mkTopUnfolding sel_rhs
421 -- Allocate Ids. We do it a funny way round because field_dict_tys is
422 -- almost always empty. Also note that we use length_tycon_theta
423 -- rather than n_dict_tys, because the latter gives an infinite loop:
424 -- n_dict tys depends on the_alts, which depens on arg_ids, which depends
425 -- on arity, which depends on n_dict tys. Sigh! Mega sigh!
426 field_dict_base = length tycon_theta + 1
427 dict_id_base = field_dict_base + n_field_dict_tys
428 field_base = dict_id_base + 1
429 dict_ids = mkTemplateLocalsNum 1 dict_tys
430 field_dict_ids = mkTemplateLocalsNum field_dict_base field_dict_tys
431 data_id = mkTemplateLocal dict_id_base data_ty
433 alts = map mk_maybe_alt data_cons
434 the_alts = catMaybes alts
436 no_default = all isJust alts -- No default needed
437 default_alt | no_default = []
438 | otherwise = [(DEFAULT, [], error_expr)]
440 -- the default branch may have CAF refs, because it calls recSelError etc.
441 caf_info | no_default = NoCafRefs
442 | otherwise = MayHaveCafRefs
444 sel_rhs = mkLams tyvars $ mkLams field_tyvars $
445 mkLams dict_ids $ mkLams field_dict_ids $
446 Lam data_id $ sel_body
448 sel_body | isNewTyCon tycon = Note (Coerce field_tau data_ty) (Var data_id)
449 | otherwise = Case (Var data_id) data_id (the_alts ++ default_alt)
451 mk_maybe_alt data_con
452 = case maybe_the_arg_id of
454 Just the_arg_id -> Just (DataAlt data_con, real_args, expr)
456 body = mkVarApps (mkVarApps (Var the_arg_id) field_tyvars) field_dict_ids
457 strict_marks = dataConStrictMarks data_con
458 (expr, real_args) = rebuildConArgs data_con arg_ids strict_marks body
461 arg_ids = mkTemplateLocalsNum field_base (dataConInstOrigArgTys data_con tyvar_tys)
463 unpack_base = field_base + length arg_ids
465 -- arity+1 avoids all shadowing
466 maybe_the_arg_id = assocMaybe (field_lbls `zip` arg_ids) field_label
467 field_lbls = dataConFieldLabels data_con
469 error_expr = mkApps (Var rEC_SEL_ERROR_ID) [Type field_tau, err_string]
471 | all safeChar full_msg
472 = App (Var unpack_id) (Lit (MachStr (_PK_ full_msg)))
474 = App (Var unpackUtf8_id) (Lit (MachStr (_PK_ (stringToUtf8 (map ord full_msg)))))
476 safeChar c = c >= '\1' && c <= '\xFF'
477 -- TODO: Putting this Unicode stuff here is ugly. Find a better
478 -- generic place to make string literals. This logic is repeated
480 full_msg = showSDoc (sep [text "No match in record selector", ppr sel_id])
483 -- this rather ugly function converts the unpacked data con arguments back into
484 -- their packed form. It is almost the same as the version in DsUtils, except that
485 -- we use template locals here rather than newDsId (ToDo: merge these).
488 :: DataCon -- the con we're matching on
489 -> [Id] -- the source-level args
490 -> [StrictnessMark] -- the strictness annotations (per-arg)
491 -> CoreExpr -- the body
492 -> Int -- template local
495 rebuildConArgs con [] stricts body i = (body, [])
496 rebuildConArgs con (arg:args) stricts body i | isTyVar arg
497 = let (body', args') = rebuildConArgs con args stricts body i
499 rebuildConArgs con (arg:args) (str:stricts) body i
500 = case maybeMarkedUnboxed str of
501 Just (pack_con1, _) ->
502 case splitProductType_maybe (idType arg) of
503 Just (_, tycon_args, pack_con, con_arg_tys) ->
504 ASSERT( pack_con == pack_con1 )
505 let unpacked_args = zipWith mkTemplateLocal [i..] con_arg_tys
506 (body', real_args) = rebuildConArgs con args stricts body
507 (i + length con_arg_tys)
510 Let (NonRec arg (mkConApp pack_con
511 (map Type tycon_args ++
512 map Var unpacked_args))) body',
513 unpacked_args ++ real_args
516 _ -> let (body', args') = rebuildConArgs con args stricts body i
517 in (body', arg:args')
521 %************************************************************************
523 \subsection{Dictionary selectors}
525 %************************************************************************
527 Selecting a field for a dictionary. If there is just one field, then
528 there's nothing to do.
530 ToDo: unify with mkRecordSelId.
533 mkDictSelId :: Name -> Class -> Id
534 mkDictSelId name clas
538 sel_id = mkGlobalId (RecordSelId field_lbl) name ty info
539 field_lbl = mkFieldLabel name tycon ty tag
540 tag = assoc "MkId.mkDictSelId" (classSelIds clas `zip` allFieldLabelTags) sel_id
542 info = noCafNoTyGenIdInfo
544 `setArityInfo` exactArity 1
545 `setUnfoldingInfo` unfolding
547 -- We no longer use 'must-inline' on record selectors. They'll
548 -- inline like crazy if they scrutinise a constructor
550 unfolding = mkTopUnfolding rhs
552 tyvars = classTyVars clas
554 tycon = classTyCon clas
555 [data_con] = tyConDataCons tycon
556 tyvar_tys = mkTyVarTys tyvars
557 arg_tys = dataConArgTys data_con tyvar_tys
558 the_arg_id = arg_ids !! (tag - firstFieldLabelTag)
560 dict_ty = mkDictTy clas tyvar_tys
561 (dict_id:arg_ids) = mkTemplateLocals (dict_ty : arg_tys)
563 rhs | isNewTyCon tycon = mkLams tyvars $ Lam dict_id $
564 Note (Coerce (head arg_tys) dict_ty) (Var dict_id)
565 | otherwise = mkLams tyvars $ Lam dict_id $
566 Case (Var dict_id) dict_id
567 [(DataAlt data_con, arg_ids, Var the_arg_id)]
571 %************************************************************************
573 \subsection{Primitive operations
575 %************************************************************************
578 mkPrimOpId :: PrimOp -> Id
582 (tyvars,arg_tys,res_ty, arity, strict_info) = primOpSig prim_op
583 ty = mkForAllTys tyvars (mkFunTys arg_tys res_ty)
584 name = mkPrimOpIdName prim_op
585 id = mkGlobalId (PrimOpId prim_op) name ty info
587 info = noCafNoTyGenIdInfo
590 `setArityInfo` exactArity arity
591 `setStrictnessInfo` strict_info
593 rules = maybe emptyCoreRules (addRule emptyCoreRules id)
597 -- For each ccall we manufacture a separate CCallOpId, giving it
598 -- a fresh unique, a type that is correct for this particular ccall,
599 -- and a CCall structure that gives the correct details about calling
602 -- The *name* of this Id is a local name whose OccName gives the full
603 -- details of the ccall, type and all. This means that the interface
604 -- file reader can reconstruct a suitable Id
606 mkCCallOpId :: Unique -> CCall -> Type -> Id
607 mkCCallOpId uniq ccall ty
608 = ASSERT( isEmptyVarSet (tyVarsOfType ty) )
609 -- A CCallOpId should have no free type variables;
610 -- when doing substitutions won't substitute over it
611 mkGlobalId (PrimOpId prim_op) name ty info
613 occ_str = showSDocIface (braces (pprCCallOp ccall <+> ppr ty))
614 -- The "occurrence name" of a ccall is the full info about the
615 -- ccall; it is encoded, but may have embedded spaces etc!
617 name = mkCCallName uniq occ_str
618 prim_op = CCallOp ccall
620 info = noCafNoTyGenIdInfo
622 `setArityInfo` exactArity arity
623 `setStrictnessInfo` strict_info
625 (_, tau) = splitForAllTys ty
626 (arg_tys, _) = splitFunTys tau
627 arity = length arg_tys
628 strict_info = mkStrictnessInfo (take arity (repeat wwPrim), False)
632 %************************************************************************
634 \subsection{DictFuns and default methods}
636 %************************************************************************
639 mkDefaultMethodId dm_name ty
640 = mkVanillaGlobal dm_name ty noCafNoTyGenIdInfo
642 mkDictFunId :: Name -- Name to use for the dict fun;
649 mkDictFunId dfun_name clas inst_tyvars inst_tys dfun_theta
650 = mkVanillaGlobal dfun_name dfun_ty noCafNoTyGenIdInfo
652 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_tys)
654 {- 1 dec 99: disable the Mark Jones optimisation for the sake
655 of compatibility with Hugs.
656 See `types/InstEnv' for a discussion related to this.
658 (class_tyvars, sc_theta, _, _) = classBigSig clas
659 not_const (clas, tys) = not (isEmptyVarSet (tyVarsOfTypes tys))
660 sc_theta' = substClasses (mkTopTyVarSubst class_tyvars inst_tys) sc_theta
661 dfun_theta = case inst_decl_theta of
662 [] -> [] -- If inst_decl_theta is empty, then we don't
663 -- want to have any dict arguments, so that we can
664 -- expose the constant methods.
666 other -> nub (inst_decl_theta ++ filter not_const sc_theta')
667 -- Otherwise we pass the superclass dictionaries to
668 -- the dictionary function; the Mark Jones optimisation.
670 -- NOTE the "nub". I got caught by this one:
671 -- class Monad m => MonadT t m where ...
672 -- instance Monad m => MonadT (EnvT env) m where ...
673 -- Here, the inst_decl_theta has (Monad m); but so
674 -- does the sc_theta'!
676 -- NOTE the "not_const". I got caught by this one too:
677 -- class Foo a => Baz a b where ...
678 -- instance Wob b => Baz T b where..
679 -- Now sc_theta' has Foo T
684 %************************************************************************
686 \subsection{Un-definable}
688 %************************************************************************
690 These two can't be defined in Haskell.
692 unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that
693 just gets expanded into a type coercion wherever it occurs. Hence we
694 add it as a built-in Id with an unfolding here.
696 The type variables we use here are "open" type variables: this means
697 they can unify with both unlifted and lifted types. Hence we provide
698 another gun with which to shoot yourself in the foot.
702 = pcMiscPrelId unsafeCoerceIdKey pREL_GHC SLIT("unsafeCoerce#") ty info
704 info = noCafNoTyGenIdInfo `setUnfoldingInfo` mkCompulsoryUnfolding rhs
707 ty = mkForAllTys [openAlphaTyVar,openBetaTyVar]
708 (mkFunTy openAlphaTy openBetaTy)
709 [x] = mkTemplateLocals [openAlphaTy]
710 rhs = mkLams [openAlphaTyVar,openBetaTyVar,x] $
711 Note (Coerce openBetaTy openAlphaTy) (Var x)
715 @getTag#@ is another function which can't be defined in Haskell. It needs to
716 evaluate its argument and call the dataToTag# primitive.
720 = pcMiscPrelId getTagIdKey pREL_GHC SLIT("getTag#") ty info
722 info = noCafNoTyGenIdInfo `setUnfoldingInfo` mkCompulsoryUnfolding rhs
723 -- We don't provide a defn for this; you must inline it
725 ty = mkForAllTys [alphaTyVar] (mkFunTy alphaTy intPrimTy)
726 [x,y] = mkTemplateLocals [alphaTy,alphaTy]
727 rhs = mkLams [alphaTyVar,x] $
728 Case (Var x) y [ (DEFAULT, [], mkApps (Var dataToTagId) [Type alphaTy, Var y]) ]
730 dataToTagId = mkPrimOpId DataToTagOp
733 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
734 nasty as-is, change it back to a literal (@Literal@).
737 realWorldPrimId -- :: State# RealWorld
738 = pcMiscPrelId realWorldPrimIdKey pREL_GHC SLIT("realWorld#")
740 (noCafNoTyGenIdInfo `setUnfoldingInfo` mkOtherCon [])
741 -- The mkOtherCon makes it look that realWorld# is evaluated
742 -- which in turn makes Simplify.interestingArg return True,
743 -- which in turn makes INLINE things applied to realWorld# likely
748 %************************************************************************
750 \subsection[PrelVals-error-related]{@error@ and friends; @trace@}
752 %************************************************************************
754 GHC randomly injects these into the code.
756 @patError@ is just a version of @error@ for pattern-matching
757 failures. It knows various ``codes'' which expand to longer
758 strings---this saves space!
760 @absentErr@ is a thing we put in for ``absent'' arguments. They jolly
761 well shouldn't be yanked on, but if one is, then you will get a
762 friendly message from @absentErr@ (rather than a totally random
765 @parError@ is a special version of @error@ which the compiler does
766 not know to be a bottoming Id. It is used in the @_par_@ and @_seq_@
767 templates, but we don't ever expect to generate code for it.
771 = pc_bottoming_Id errorIdKey pREL_ERR SLIT("error") errorTy
773 = generic_ERROR_ID patErrorIdKey SLIT("patError")
775 = generic_ERROR_ID recSelErrIdKey SLIT("recSelError")
777 = generic_ERROR_ID recConErrorIdKey SLIT("recConError")
779 = generic_ERROR_ID recUpdErrorIdKey SLIT("recUpdError")
781 = generic_ERROR_ID irrefutPatErrorIdKey SLIT("irrefutPatError")
782 nON_EXHAUSTIVE_GUARDS_ERROR_ID
783 = generic_ERROR_ID nonExhaustiveGuardsErrorIdKey SLIT("nonExhaustiveGuardsError")
784 nO_METHOD_BINDING_ERROR_ID
785 = generic_ERROR_ID noMethodBindingErrorIdKey SLIT("noMethodBindingError")
788 = pc_bottoming_Id absentErrorIdKey pREL_ERR SLIT("absentErr")
789 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy)
792 = pcMiscPrelId parErrorIdKey pREL_ERR SLIT("parError")
793 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy) noCafNoTyGenIdInfo
797 %************************************************************************
799 \subsection{Utilities}
801 %************************************************************************
804 pcMiscPrelId :: Unique{-IdKey-} -> Module -> FAST_STRING -> Type -> IdInfo -> Id
805 pcMiscPrelId key mod str ty info
807 name = mkWiredInName mod (mkVarOcc str) key
808 imp = mkVanillaGlobal name ty info -- the usual case...
811 -- We lie and say the thing is imported; otherwise, we get into
812 -- a mess with dependency analysis; e.g., core2stg may heave in
813 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
814 -- being compiled, then it's just a matter of luck if the definition
815 -- will be in "the right place" to be in scope.
817 pc_bottoming_Id key mod name ty
818 = pcMiscPrelId key mod name ty bottoming_info
820 bottoming_info = noCafNoTyGenIdInfo
821 `setStrictnessInfo` mkStrictnessInfo ([wwStrict], True)
823 -- these "bottom" out, no matter what their arguments
825 generic_ERROR_ID u n = pc_bottoming_Id u pREL_ERR n errorTy
827 (openAlphaTyVar:openBetaTyVar:_) = openAlphaTyVars
828 openAlphaTy = mkTyVarTy openAlphaTyVar
829 openBetaTy = mkTyVarTy openBetaTyVar
832 errorTy = mkSigmaTy [openAlphaTyVar] [] (mkFunTys [mkListTy charTy]
834 -- Notice the openAlphaTyVar. It says that "error" can be applied
835 -- to unboxed as well as boxed types. This is OK because it never
836 -- returns, so the return type is irrelevant.