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 mkSpecPragmaId, mkWorkerId,
18 mkDictFunId, mkDefaultMethodId,
21 mkDataConId, mkDataConWrapId,
23 mkPrimOpId, mkCCallOpId,
25 -- And some particular Ids; see below for why they are wired in
27 unsafeCoerceId, realWorldPrimId,
28 eRROR_ID, rEC_SEL_ERROR_ID, pAT_ERROR_ID, rEC_CON_ERROR_ID,
29 rEC_UPD_ERROR_ID, iRREFUT_PAT_ERROR_ID, nON_EXHAUSTIVE_GUARDS_ERROR_ID,
30 nO_METHOD_BINDING_ERROR_ID, aBSENT_ERROR_ID, pAR_ERROR_ID
33 #include "HsVersions.h"
36 import TysPrim ( openAlphaTyVars, alphaTyVar, alphaTy,
37 intPrimTy, realWorldStatePrimTy
39 import TysWiredIn ( charTy, mkListTy )
40 import PrelNames ( pREL_ERR, pREL_GHC )
41 import PrelRules ( primOpRule )
42 import Rules ( addRule )
43 import Type ( Type, ThetaType, mkDictTy, mkDictTys, mkTyConApp, mkTyVarTys,
44 mkFunTys, mkFunTy, mkSigmaTy,
45 isUnLiftedType, mkForAllTys, mkTyVarTy, tyVarsOfType,
46 splitFunTys, splitForAllTys
48 import Module ( Module )
49 import CoreUtils ( exprType, mkInlineMe )
50 import CoreUnfold ( mkTopUnfolding, mkCompulsoryUnfolding, mkOtherCon )
51 import Literal ( Literal(..) )
52 import TyCon ( TyCon, isNewTyCon, tyConTyVars, tyConDataCons,
53 tyConTheta, isProductTyCon, isUnboxedTupleTyCon )
54 import Class ( Class, classTyCon, classTyVars, classSelIds )
55 import Var ( Id, TyVar )
56 import VarSet ( isEmptyVarSet )
57 import Name ( mkWiredInName, mkLocalName,
58 mkWorkerOcc, mkCCallName,
59 Name, NamedThing(..), getSrcLoc
61 import OccName ( mkVarOcc )
62 import PrimOp ( PrimOp(DataToTagOp, CCallOp),
63 primOpSig, mkPrimOpIdName,
66 import Demand ( wwStrict, wwPrim, mkStrictnessInfo )
67 import DataCon ( DataCon, StrictnessMark(..),
68 dataConFieldLabels, dataConRepArity, dataConTyCon,
69 dataConArgTys, dataConRepType, dataConRepStrictness,
70 dataConInstOrigArgTys,
71 dataConName, dataConTheta,
72 dataConSig, dataConStrictMarks, dataConId,
73 maybeMarkedUnboxed, splitProductType_maybe
75 import Id ( idType, mkId,
76 mkVanillaId, mkTemplateLocals,
77 mkTemplateLocal, idCprInfo
79 import IdInfo ( IdInfo, constantIdInfo, mkIdInfo,
80 exactArity, setUnfoldingInfo, setCafInfo, setCprInfo,
81 setArityInfo, setSpecInfo, setTyGenInfo,
82 mkStrictnessInfo, setStrictnessInfo,
83 IdFlavour(..), CafInfo(..), CprInfo(..), TyGenInfo(..)
85 import FieldLabel ( mkFieldLabel, fieldLabelName,
86 firstFieldLabelTag, allFieldLabelTags, fieldLabelType
91 import Maybe ( isJust )
93 import ListSetOps ( assoc, assocMaybe )
94 import UnicodeUtil ( stringToUtf8 )
99 %************************************************************************
101 \subsection{Wired in Ids}
103 %************************************************************************
107 = [ -- These error-y things are wired in because we don't yet have
108 -- a way to express in an interface file that the result type variable
109 -- is 'open'; that is can be unified with an unboxed type
111 -- [The interface file format now carry such information, but there's
112 -- no way yet of expressing at the definition site for these
114 -- functions that they have an 'open' result type. -- sof 1/99]
118 , iRREFUT_PAT_ERROR_ID
119 , nON_EXHAUSTIVE_GUARDS_ERROR_ID
120 , nO_METHOD_BINDING_ERROR_ID
126 -- These two can't be defined in Haskell
133 %************************************************************************
135 \subsection{Easy ones}
137 %************************************************************************
140 mkSpecPragmaId occ uniq ty loc
141 = mkId (mkLocalName uniq occ loc) ty (mkIdInfo SpecPragmaId)
142 -- Maybe a SysLocal? But then we'd lose the location
144 mkDefaultMethodId dm_name rec_c ty
145 = mkId dm_name ty info
147 info = constantIdInfo `setTyGenInfo` TyGenNever
148 -- type is wired-in (see comment at TcClassDcl.tcClassSig), so
149 -- do not generalise it
151 mkWorkerId :: Unique -> Id -> Type -> Id
152 -- A worker gets a local name. CoreTidy will globalise it if necessary.
153 mkWorkerId uniq unwrkr ty
154 = mkVanillaId wkr_name ty
156 wkr_name = mkLocalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcLoc unwrkr)
159 %************************************************************************
161 \subsection{Data constructors}
163 %************************************************************************
166 mkDataConId :: Name -> DataCon -> Id
167 -- Makes the *worker* for the data constructor; that is, the function
168 -- that takes the reprsentation arguments and builds the constructor.
169 mkDataConId work_name data_con
170 = mkId work_name (dataConRepType data_con) info
172 info = mkIdInfo (DataConId data_con)
173 `setArityInfo` exactArity arity
174 `setStrictnessInfo` strict_info
175 `setCprInfo` cpr_info
177 arity = dataConRepArity data_con
179 strict_info = mkStrictnessInfo (dataConRepStrictness data_con, False)
181 cpr_info | isProductTyCon tycon &&
182 not (isUnboxedTupleTyCon tycon) &&
183 arity > 0 = ReturnsCPR
184 | otherwise = NoCPRInfo
186 tycon = dataConTyCon data_con
187 -- Newtypes don't have a worker at all
189 -- If we are a product with 0 args we must be void(like)
190 -- We can't create an unboxed tuple with 0 args for this
191 -- and since Void has only one, constant value it should
192 -- just mean returning a pointer to a pre-existing cell.
193 -- So we won't really gain from doing anything fancy
194 -- and we treat this case as Top.
197 The wrapper for a constructor is an ordinary top-level binding that evaluates
198 any strict args, unboxes any args that are going to be flattened, and calls
201 We're going to build a constructor that looks like:
203 data (Data a, C b) => T a b = T1 !a !Int b
206 \d1::Data a, d2::C b ->
207 \p q r -> case p of { p ->
209 Con T1 [a,b] [p,q,r]}}
213 * d2 is thrown away --- a context in a data decl is used to make sure
214 one *could* construct dictionaries at the site the constructor
215 is used, but the dictionary isn't actually used.
217 * We have to check that we can construct Data dictionaries for
218 the types a and Int. Once we've done that we can throw d1 away too.
220 * We use (case p of q -> ...) to evaluate p, rather than "seq" because
221 all that matters is that the arguments are evaluated. "seq" is
222 very careful to preserve evaluation order, which we don't need
225 You might think that we could simply give constructors some strictness
226 info, like PrimOps, and let CoreToStg do the let-to-case transformation.
227 But we don't do that because in the case of primops and functions strictness
228 is a *property* not a *requirement*. In the case of constructors we need to
229 do something active to evaluate the argument.
231 Making an explicit case expression allows the simplifier to eliminate
232 it in the (common) case where the constructor arg is already evaluated.
235 mkDataConWrapId data_con
238 wrap_id = mkId (dataConName data_con) wrap_ty info
239 work_id = dataConId data_con
241 info = mkIdInfo (DataConWrapId data_con)
242 `setUnfoldingInfo` mkTopUnfolding (mkInlineMe wrap_rhs)
243 `setCprInfo` cpr_info
244 -- The Cpr info can be important inside INLINE rhss, where the
245 -- wrapper constructor isn't inlined
246 `setArityInfo` exactArity arity
247 -- It's important to specify the arity, so that partial
248 -- applications are treated as values
249 `setCafInfo` NoCafRefs
250 -- The wrapper Id ends up in STG code as an argument,
251 -- sometimes before its definition, so we want to
252 -- signal that it has no CAFs
253 `setTyGenInfo` TyGenNever
254 -- No point generalising its type, since it gets eagerly inlined
257 wrap_ty = mkForAllTys all_tyvars $
261 cpr_info = idCprInfo work_id
263 wrap_rhs | isNewTyCon tycon
264 = ASSERT( null ex_tyvars && null ex_dict_args && length orig_arg_tys == 1 )
265 -- No existentials on a newtype, but it can have a context
266 -- e.g. newtype Eq a => T a = MkT (...)
268 mkLams tyvars $ mkLams dict_args $ Lam id_arg1 $
269 Note (Coerce result_ty (head orig_arg_tys)) (Var id_arg1)
271 | null dict_args && all not_marked_strict strict_marks
272 = Var work_id -- The common case. Not only is this efficient,
273 -- but it also ensures that the wrapper is replaced
274 -- by the worker even when there are no args.
278 -- This is really important in rule matching,
279 -- (We could match on the wrappers,
280 -- but that makes it less likely that rules will match
281 -- when we bring bits of unfoldings together.)
283 -- NB: because of this special case, (map (:) ys) turns into
284 -- (map $w: ys), and thence into (map (\x xs. $w: x xs) ys)
285 -- in core-to-stg. The top-level defn for (:) is never used.
286 -- This is somewhat of a bore, but I'm currently leaving it
287 -- as is, so that there still is a top level curried (:) for
288 -- the interpreter to call.
291 = mkLams all_tyvars $ mkLams dict_args $
292 mkLams ex_dict_args $ mkLams id_args $
293 foldr mk_case con_app
294 (zip (ex_dict_args++id_args) strict_marks) i3 []
296 con_app i rep_ids = mkApps (Var work_id)
297 (map varToCoreExpr (all_tyvars ++ reverse rep_ids))
299 (tyvars, theta, ex_tyvars, ex_theta, orig_arg_tys, tycon) = dataConSig data_con
300 all_tyvars = tyvars ++ ex_tyvars
302 dict_tys = mkDictTys theta
303 ex_dict_tys = mkDictTys ex_theta
304 all_arg_tys = dict_tys ++ ex_dict_tys ++ orig_arg_tys
305 result_ty = mkTyConApp tycon (mkTyVarTys tyvars)
307 mkLocals i tys = (zipWith mkTemplateLocal [i..i+n-1] tys, i+n)
311 (dict_args, i1) = mkLocals 1 dict_tys
312 (ex_dict_args,i2) = mkLocals i1 ex_dict_tys
313 (id_args,i3) = mkLocals i2 orig_arg_tys
315 (id_arg1:_) = id_args -- Used for newtype only
317 strict_marks = dataConStrictMarks data_con
318 not_marked_strict NotMarkedStrict = True
319 not_marked_strict other = False
323 :: (Id, StrictnessMark) -- arg, strictness
324 -> (Int -> [Id] -> CoreExpr) -- body
325 -> Int -- next rep arg id
326 -> [Id] -- rep args so far
328 mk_case (arg,strict) body i rep_args
330 NotMarkedStrict -> body i (arg:rep_args)
332 | isUnLiftedType (idType arg) -> body i (arg:rep_args)
334 Case (Var arg) arg [(DEFAULT,[], body i (arg:rep_args))]
336 MarkedUnboxed con tys ->
337 Case (Var arg) arg [(DataAlt con, con_args,
338 body i' (reverse con_args++rep_args))]
340 (con_args,i') = mkLocals i tys
344 %************************************************************************
346 \subsection{Record selectors}
348 %************************************************************************
350 We're going to build a record selector unfolding that looks like this:
352 data T a b c = T1 { ..., op :: a, ...}
353 | T2 { ..., op :: a, ...}
356 sel = /\ a b c -> \ d -> case d of
361 Similarly for newtypes
363 newtype N a = MkN { unN :: a->a }
366 unN n = coerce (a->a) n
368 We need to take a little care if the field has a polymorphic type:
370 data R = R { f :: forall a. a->a }
374 f :: forall a. R -> a -> a
375 f = /\ a \ r = case r of
378 (not f :: R -> forall a. a->a, which gives the type inference mechanism
379 problems at call sites)
381 Similarly for newtypes
383 newtype N = MkN { unN :: forall a. a->a }
385 unN :: forall a. N -> a -> a
386 unN = /\a -> \n:N -> coerce (a->a) n
389 mkRecordSelId tycon field_label unpack_id unpackUtf8_id
390 -- Assumes that all fields with the same field label have the same type
392 -- Annoyingly, we have to pass in the unpackCString# Id, because
393 -- we can't conjure it up out of thin air
396 sel_id = mkId (fieldLabelName field_label) selector_ty info
398 field_ty = fieldLabelType field_label
399 data_cons = tyConDataCons tycon
400 tyvars = tyConTyVars tycon -- These scope over the types in
401 -- the FieldLabels of constructors of this type
402 tycon_theta = tyConTheta tycon -- The context on the data decl
403 -- eg data (Eq a, Ord b) => T a b = ...
404 (field_tyvars,field_tau) = splitForAllTys field_ty
406 data_ty = mkTyConApp tycon tyvar_tys
407 tyvar_tys = mkTyVarTys tyvars
409 -- Very tiresomely, the selectors are (unnecessarily!) overloaded over
410 -- just the dictionaries in the types of the constructors that contain
411 -- the relevant field. Urgh.
412 -- NB: this code relies on the fact that DataCons are quantified over
413 -- the identical type variables as their parent TyCon
414 dict_tys = [mkDictTy cls tys | (cls, tys) <- tycon_theta, needed_dict (cls, tys)]
415 needed_dict pred = or [ pred `elem` (dataConTheta dc)
416 | (DataAlt dc, _, _) <- the_alts]
419 selector_ty = mkForAllTys tyvars $ mkForAllTys field_tyvars $
420 mkFunTys dict_tys $ mkFunTy data_ty field_tau
422 info = mkIdInfo (RecordSelId field_label)
423 `setArityInfo` exactArity (1 + length dict_tys)
424 `setUnfoldingInfo` unfolding
425 `setCafInfo` NoCafRefs
426 `setTyGenInfo` TyGenNever
427 -- ToDo: consider adding further IdInfo
429 unfolding = mkTopUnfolding sel_rhs
432 (data_id:dict_ids) = mkTemplateLocals (data_ty:dict_tys)
433 alts = map mk_maybe_alt data_cons
434 the_alts = catMaybes alts
435 default_alt | all isJust alts = [] -- No default needed
436 | otherwise = [(DEFAULT, [], error_expr)]
438 sel_rhs = mkLams tyvars $ mkLams field_tyvars $
439 mkLams dict_ids $ Lam data_id $
442 sel_body | isNewTyCon tycon = Note (Coerce field_tau data_ty) (Var data_id)
443 | otherwise = Case (Var data_id) data_id (the_alts ++ default_alt)
445 mk_maybe_alt data_con
446 = case maybe_the_arg_id of
448 Just the_arg_id -> Just (DataAlt data_con, real_args, expr)
450 body = mkVarApps (Var the_arg_id) field_tyvars
451 strict_marks = dataConStrictMarks data_con
452 (expr, real_args) = rebuildConArgs data_con arg_ids strict_marks body
455 arg_ids = mkTemplateLocals (dataConInstOrigArgTys data_con tyvar_tys)
456 -- The first one will shadow data_id, but who cares
457 maybe_the_arg_id = assocMaybe (field_lbls `zip` arg_ids) field_label
458 field_lbls = dataConFieldLabels data_con
460 error_expr = mkApps (Var rEC_SEL_ERROR_ID) [Type field_tau, err_string]
462 | all safeChar full_msg
463 = App (Var unpack_id) (Lit (MachStr (_PK_ full_msg)))
465 = App (Var unpackUtf8_id) (Lit (MachStr (_PK_ (stringToUtf8 (map ord full_msg)))))
467 safeChar c = c >= '\1' && c <= '\xFF'
468 -- TODO: Putting this Unicode stuff here is ugly. Find a better
469 -- generic place to make string literals. This logic is repeated
471 full_msg = showSDoc (sep [text "No match in record selector", ppr sel_id])
474 -- this rather ugly function converts the unpacked data con arguments back into
475 -- their packed form. It is almost the same as the version in DsUtils, except that
476 -- we use template locals here rather than newDsId (ToDo: merge these).
479 :: DataCon -- the con we're matching on
480 -> [Id] -- the source-level args
481 -> [StrictnessMark] -- the strictness annotations (per-arg)
482 -> CoreExpr -- the body
483 -> Int -- template local
486 rebuildConArgs con [] stricts body i = (body, [])
487 rebuildConArgs con (arg:args) stricts body i | isTyVar arg
488 = let (body', args') = rebuildConArgs con args stricts body i
490 rebuildConArgs con (arg:args) (str:stricts) body i
491 = case maybeMarkedUnboxed str of
492 Just (pack_con1, _) ->
493 case splitProductType_maybe (idType arg) of
494 Just (_, tycon_args, pack_con, con_arg_tys) ->
495 ASSERT( pack_con == pack_con1 )
496 let unpacked_args = zipWith mkTemplateLocal [i..] con_arg_tys
497 (body', real_args) = rebuildConArgs con args stricts body
498 (i + length con_arg_tys)
501 Let (NonRec arg (mkConApp pack_con
502 (map Type tycon_args ++
503 map Var unpacked_args))) body',
504 unpacked_args ++ real_args
507 _ -> let (body', args') = rebuildConArgs con args stricts body i
508 in (body', arg:args')
512 %************************************************************************
514 \subsection{Dictionary selectors}
516 %************************************************************************
518 Selecting a field for a dictionary. If there is just one field, then
519 there's nothing to do.
521 ToDo: unify with mkRecordSelId.
524 mkDictSelId :: Name -> Class -> Id
525 mkDictSelId name clas
529 sel_id = mkId name ty info
530 field_lbl = mkFieldLabel name tycon ty tag
531 tag = assoc "MkId.mkDictSelId" (classSelIds clas `zip` allFieldLabelTags) sel_id
533 info = mkIdInfo (RecordSelId field_lbl)
534 `setArityInfo` exactArity 1
535 `setUnfoldingInfo` unfolding
536 `setCafInfo` NoCafRefs
537 `setTyGenInfo` TyGenNever
539 -- We no longer use 'must-inline' on record selectors. They'll
540 -- inline like crazy if they scrutinise a constructor
542 unfolding = mkTopUnfolding rhs
544 tyvars = classTyVars clas
546 tycon = classTyCon clas
547 [data_con] = tyConDataCons tycon
548 tyvar_tys = mkTyVarTys tyvars
549 arg_tys = dataConArgTys data_con tyvar_tys
550 the_arg_id = arg_ids !! (tag - firstFieldLabelTag)
552 dict_ty = mkDictTy clas tyvar_tys
553 (dict_id:arg_ids) = mkTemplateLocals (dict_ty : arg_tys)
555 rhs | isNewTyCon tycon = mkLams tyvars $ Lam dict_id $
556 Note (Coerce (head arg_tys) dict_ty) (Var dict_id)
557 | otherwise = mkLams tyvars $ Lam dict_id $
558 Case (Var dict_id) dict_id
559 [(DataAlt data_con, arg_ids, Var the_arg_id)]
563 %************************************************************************
565 \subsection{Primitive operations
567 %************************************************************************
570 mkPrimOpId :: PrimOp -> Id
574 (tyvars,arg_tys,res_ty, arity, strict_info) = primOpSig prim_op
575 ty = mkForAllTys tyvars (mkFunTys arg_tys res_ty)
576 name = mkPrimOpIdName prim_op
577 id = mkId name ty info
579 info = mkIdInfo (PrimOpId prim_op)
581 `setArityInfo` exactArity arity
582 `setStrictnessInfo` strict_info
584 rules = addRule emptyCoreRules id (primOpRule prim_op)
587 -- For each ccall we manufacture a separate CCallOpId, giving it
588 -- a fresh unique, a type that is correct for this particular ccall,
589 -- and a CCall structure that gives the correct details about calling
592 -- The *name* of this Id is a local name whose OccName gives the full
593 -- details of the ccall, type and all. This means that the interface
594 -- file reader can reconstruct a suitable Id
596 mkCCallOpId :: Unique -> CCall -> Type -> Id
597 mkCCallOpId uniq ccall ty
598 = ASSERT( isEmptyVarSet (tyVarsOfType ty) )
599 -- A CCallOpId should have no free type variables;
600 -- when doing substitutions won't substitute over it
603 occ_str = showSDocIface (braces (pprCCallOp ccall <+> ppr ty))
604 -- The "occurrence name" of a ccall is the full info about the
605 -- ccall; it is encoded, but may have embedded spaces etc!
607 name = mkCCallName uniq occ_str
608 prim_op = CCallOp ccall
610 info = mkIdInfo (PrimOpId prim_op)
611 `setArityInfo` exactArity arity
612 `setStrictnessInfo` strict_info
614 (_, tau) = splitForAllTys ty
615 (arg_tys, _) = splitFunTys tau
616 arity = length arg_tys
617 strict_info = mkStrictnessInfo (take arity (repeat wwPrim), False)
621 %************************************************************************
623 \subsection{DictFuns}
625 %************************************************************************
628 mkDictFunId :: Name -- Name to use for the dict fun;
635 mkDictFunId dfun_name clas inst_tyvars inst_tys dfun_theta
636 = mkId dfun_name dfun_ty info
638 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_tys)
639 info = mkIdInfo DictFunId `setTyGenInfo` TyGenNever
640 -- type is wired-in (see comment at TcClassDcl.tcClassSig), so
641 -- do not generalise it
643 {- 1 dec 99: disable the Mark Jones optimisation for the sake
644 of compatibility with Hugs.
645 See `types/InstEnv' for a discussion related to this.
647 (class_tyvars, sc_theta, _, _) = classBigSig clas
648 not_const (clas, tys) = not (isEmptyVarSet (tyVarsOfTypes tys))
649 sc_theta' = substClasses (mkTopTyVarSubst class_tyvars inst_tys) sc_theta
650 dfun_theta = case inst_decl_theta of
651 [] -> [] -- If inst_decl_theta is empty, then we don't
652 -- want to have any dict arguments, so that we can
653 -- expose the constant methods.
655 other -> nub (inst_decl_theta ++ filter not_const sc_theta')
656 -- Otherwise we pass the superclass dictionaries to
657 -- the dictionary function; the Mark Jones optimisation.
659 -- NOTE the "nub". I got caught by this one:
660 -- class Monad m => MonadT t m where ...
661 -- instance Monad m => MonadT (EnvT env) m where ...
662 -- Here, the inst_decl_theta has (Monad m); but so
663 -- does the sc_theta'!
665 -- NOTE the "not_const". I got caught by this one too:
666 -- class Foo a => Baz a b where ...
667 -- instance Wob b => Baz T b where..
668 -- Now sc_theta' has Foo T
673 %************************************************************************
675 \subsection{Un-definable}
677 %************************************************************************
679 These two can't be defined in Haskell.
681 unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that
682 just gets expanded into a type coercion wherever it occurs. Hence we
683 add it as a built-in Id with an unfolding here.
685 The type variables we use here are "open" type variables: this means
686 they can unify with both unlifted and lifted types. Hence we provide
687 another gun with which to shoot yourself in the foot.
691 = pcMiscPrelId unsafeCoerceIdKey pREL_GHC SLIT("unsafeCoerce#") ty info
693 info = constantIdInfo `setUnfoldingInfo` mkCompulsoryUnfolding rhs
696 ty = mkForAllTys [openAlphaTyVar,openBetaTyVar]
697 (mkFunTy openAlphaTy openBetaTy)
698 [x] = mkTemplateLocals [openAlphaTy]
699 rhs = mkLams [openAlphaTyVar,openBetaTyVar,x] $
700 Note (Coerce openBetaTy openAlphaTy) (Var x)
704 @getTag#@ is another function which can't be defined in Haskell. It needs to
705 evaluate its argument and call the dataToTag# primitive.
709 = pcMiscPrelId getTagIdKey pREL_GHC SLIT("getTag#") ty info
711 info = constantIdInfo
712 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
713 -- We don't provide a defn for this; you must inline it
715 ty = mkForAllTys [alphaTyVar] (mkFunTy alphaTy intPrimTy)
716 [x,y] = mkTemplateLocals [alphaTy,alphaTy]
717 rhs = mkLams [alphaTyVar,x] $
718 Case (Var x) y [ (DEFAULT, [], mkApps (Var dataToTagId) [Type alphaTy, Var y]) ]
720 dataToTagId = mkPrimOpId DataToTagOp
723 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
724 nasty as-is, change it back to a literal (@Literal@).
727 realWorldPrimId -- :: State# RealWorld
728 = pcMiscPrelId realWorldPrimIdKey pREL_GHC SLIT("realWorld#")
730 (noCafIdInfo `setUnfoldingInfo` mkOtherCon [])
731 -- The mkOtherCon makes it look that realWorld# is evaluated
732 -- which in turn makes Simplify.interestingArg return True,
733 -- which in turn makes INLINE things applied to realWorld# likely
738 %************************************************************************
740 \subsection[PrelVals-error-related]{@error@ and friends; @trace@}
742 %************************************************************************
744 GHC randomly injects these into the code.
746 @patError@ is just a version of @error@ for pattern-matching
747 failures. It knows various ``codes'' which expand to longer
748 strings---this saves space!
750 @absentErr@ is a thing we put in for ``absent'' arguments. They jolly
751 well shouldn't be yanked on, but if one is, then you will get a
752 friendly message from @absentErr@ (rather than a totally random
755 @parError@ is a special version of @error@ which the compiler does
756 not know to be a bottoming Id. It is used in the @_par_@ and @_seq_@
757 templates, but we don't ever expect to generate code for it.
761 = pc_bottoming_Id errorIdKey pREL_ERR SLIT("error") errorTy
763 = generic_ERROR_ID patErrorIdKey SLIT("patError")
765 = generic_ERROR_ID recSelErrIdKey SLIT("recSelError")
767 = generic_ERROR_ID recConErrorIdKey SLIT("recConError")
769 = generic_ERROR_ID recUpdErrorIdKey SLIT("recUpdError")
771 = generic_ERROR_ID irrefutPatErrorIdKey SLIT("irrefutPatError")
772 nON_EXHAUSTIVE_GUARDS_ERROR_ID
773 = generic_ERROR_ID nonExhaustiveGuardsErrorIdKey SLIT("nonExhaustiveGuardsError")
774 nO_METHOD_BINDING_ERROR_ID
775 = generic_ERROR_ID noMethodBindingErrorIdKey SLIT("noMethodBindingError")
778 = pc_bottoming_Id absentErrorIdKey pREL_ERR SLIT("absentErr")
779 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy)
782 = pcMiscPrelId parErrorIdKey pREL_ERR SLIT("parError")
783 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy) noCafIdInfo
788 %************************************************************************
790 \subsection{Utilities}
792 %************************************************************************
795 pcMiscPrelId :: Unique{-IdKey-} -> Module -> FAST_STRING -> Type -> IdInfo -> Id
796 pcMiscPrelId key mod str ty info
798 name = mkWiredInName mod (mkVarOcc str) key
799 imp = mkId name ty info -- the usual case...
802 -- We lie and say the thing is imported; otherwise, we get into
803 -- a mess with dependency analysis; e.g., core2stg may heave in
804 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
805 -- being compiled, then it's just a matter of luck if the definition
806 -- will be in "the right place" to be in scope.
808 pc_bottoming_Id key mod name ty
809 = pcMiscPrelId key mod name ty bottoming_info
811 bottoming_info = noCafIdInfo
812 `setStrictnessInfo` mkStrictnessInfo ([wwStrict], True)
814 -- these "bottom" out, no matter what their arguments
816 generic_ERROR_ID u n = pc_bottoming_Id u pREL_ERR n errorTy
819 noCafIdInfo = constantIdInfo `setCafInfo` NoCafRefs
821 (openAlphaTyVar:openBetaTyVar:_) = openAlphaTyVars
822 openAlphaTy = mkTyVarTy openAlphaTyVar
823 openBetaTy = mkTyVarTy openBetaTyVar
826 errorTy = mkSigmaTy [openAlphaTyVar] [] (mkFunTys [mkListTy charTy]
828 -- Notice the openAlphaTyVar. It says that "error" can be applied
829 -- to unboxed as well as boxed types. This is OK because it never
830 -- returns, so the return type is irrelevant.