5bab875b526ec3ddd99cac418a6522452d79918e
[ghc.git] / compiler / basicTypes / MkId.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The AQUA Project, Glasgow University, 1998
4
5
6 This module contains definitions for the IdInfo for things that
7 have a standard form, namely:
8
9 - data constructors
10 - record selectors
11 - method and superclass selectors
12 - primitive operations
13 -}
14
15 {-# LANGUAGE CPP #-}
16
17 module MkId (
18 mkDictFunId, mkDictFunTy, mkDictSelId, mkDictSelRhs,
19
20 mkPrimOpId, mkFCallId,
21
22 wrapNewTypeBody, unwrapNewTypeBody,
23 wrapFamInstBody, unwrapFamInstScrut,
24 wrapTypeUnbranchedFamInstBody, unwrapTypeUnbranchedFamInstScrut,
25
26 DataConBoxer(..), mkDataConRep, mkDataConWorkId,
27
28 -- And some particular Ids; see below for why they are wired in
29 wiredInIds, ghcPrimIds,
30 unsafeCoerceName, unsafeCoerceId, realWorldPrimId,
31 voidPrimId, voidArgId,
32 nullAddrId, seqId, lazyId, lazyIdKey, runRWId,
33 coercionTokenId, magicDictId, coerceId,
34 proxyHashId,
35
36 -- Re-export error Ids
37 module PrelRules
38 ) where
39
40 #include "HsVersions.h"
41
42 import Rules
43 import TysPrim
44 import TysWiredIn
45 import PrelRules
46 import Type
47 import FamInstEnv
48 import Coercion
49 import TcType
50 import MkCore
51 import CoreUtils ( exprType, mkCast )
52 import CoreUnfold
53 import Literal
54 import TyCon
55 import CoAxiom
56 import Class
57 import NameSet
58 import VarSet
59 import Name
60 import PrimOp
61 import ForeignCall
62 import DataCon
63 import Id
64 import IdInfo
65 import Demand
66 import CoreSyn
67 import Unique
68 import UniqSupply
69 import PrelNames
70 import BasicTypes hiding ( SuccessFlag(..) )
71 import Util
72 import Pair
73 import DynFlags
74 import Outputable
75 import FastString
76 import ListSetOps
77 import qualified GHC.LanguageExtensions as LangExt
78
79 import Data.Maybe ( maybeToList )
80
81 {-
82 ************************************************************************
83 * *
84 \subsection{Wired in Ids}
85 * *
86 ************************************************************************
87
88 Note [Wired-in Ids]
89 ~~~~~~~~~~~~~~~~~~~
90 There are several reasons why an Id might appear in the wiredInIds:
91
92 (1) The ghcPrimIds are wired in because they can't be defined in
93 Haskell at all, although the can be defined in Core. They have
94 compulsory unfoldings, so they are always inlined and they have
95 no definition site. Their home module is GHC.Prim, so they
96 also have a description in primops.txt.pp, where they are called
97 'pseudoops'.
98
99 (2) The 'error' function, eRROR_ID, is wired in because we don't yet have
100 a way to express in an interface file that the result type variable
101 is 'open'; that is can be unified with an unboxed type
102
103 [The interface file format now carry such information, but there's
104 no way yet of expressing at the definition site for these
105 error-reporting functions that they have an 'open'
106 result type. -- sof 1/99]
107
108 (3) Other error functions (rUNTIME_ERROR_ID) are wired in (a) because
109 the desugarer generates code that mentions them directly, and
110 (b) for the same reason as eRROR_ID
111
112 (4) lazyId is wired in because the wired-in version overrides the
113 strictness of the version defined in GHC.Base
114
115 In cases (2-4), the function has a definition in a library module, and
116 can be called; but the wired-in version means that the details are
117 never read from that module's interface file; instead, the full definition
118 is right here.
119 -}
120
121 wiredInIds :: [Id]
122 wiredInIds
123 = [lazyId, dollarId, oneShotId, runRWId]
124 ++ errorIds -- Defined in MkCore
125 ++ ghcPrimIds
126
127 -- These Ids are exported from GHC.Prim
128 ghcPrimIds :: [Id]
129 ghcPrimIds
130 = [ -- These can't be defined in Haskell, but they have
131 -- perfectly reasonable unfoldings in Core
132 realWorldPrimId,
133 voidPrimId,
134 unsafeCoerceId,
135 nullAddrId,
136 seqId,
137 magicDictId,
138 coerceId,
139 proxyHashId
140 ]
141
142 {-
143 ************************************************************************
144 * *
145 \subsection{Data constructors}
146 * *
147 ************************************************************************
148
149 The wrapper for a constructor is an ordinary top-level binding that evaluates
150 any strict args, unboxes any args that are going to be flattened, and calls
151 the worker.
152
153 We're going to build a constructor that looks like:
154
155 data (Data a, C b) => T a b = T1 !a !Int b
156
157 T1 = /\ a b ->
158 \d1::Data a, d2::C b ->
159 \p q r -> case p of { p ->
160 case q of { q ->
161 Con T1 [a,b] [p,q,r]}}
162
163 Notice that
164
165 * d2 is thrown away --- a context in a data decl is used to make sure
166 one *could* construct dictionaries at the site the constructor
167 is used, but the dictionary isn't actually used.
168
169 * We have to check that we can construct Data dictionaries for
170 the types a and Int. Once we've done that we can throw d1 away too.
171
172 * We use (case p of q -> ...) to evaluate p, rather than "seq" because
173 all that matters is that the arguments are evaluated. "seq" is
174 very careful to preserve evaluation order, which we don't need
175 to be here.
176
177 You might think that we could simply give constructors some strictness
178 info, like PrimOps, and let CoreToStg do the let-to-case transformation.
179 But we don't do that because in the case of primops and functions strictness
180 is a *property* not a *requirement*. In the case of constructors we need to
181 do something active to evaluate the argument.
182
183 Making an explicit case expression allows the simplifier to eliminate
184 it in the (common) case where the constructor arg is already evaluated.
185
186 Note [Wrappers for data instance tycons]
187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188 In the case of data instances, the wrapper also applies the coercion turning
189 the representation type into the family instance type to cast the result of
190 the wrapper. For example, consider the declarations
191
192 data family Map k :: * -> *
193 data instance Map (a, b) v = MapPair (Map a (Pair b v))
194
195 The tycon to which the datacon MapPair belongs gets a unique internal
196 name of the form :R123Map, and we call it the representation tycon.
197 In contrast, Map is the family tycon (accessible via
198 tyConFamInst_maybe). A coercion allows you to move between
199 representation and family type. It is accessible from :R123Map via
200 tyConFamilyCoercion_maybe and has kind
201
202 Co123Map a b v :: {Map (a, b) v ~ :R123Map a b v}
203
204 The wrapper and worker of MapPair get the types
205
206 -- Wrapper
207 $WMapPair :: forall a b v. Map a (Map a b v) -> Map (a, b) v
208 $WMapPair a b v = MapPair a b v `cast` sym (Co123Map a b v)
209
210 -- Worker
211 MapPair :: forall a b v. Map a (Map a b v) -> :R123Map a b v
212
213 This coercion is conditionally applied by wrapFamInstBody.
214
215 It's a bit more complicated if the data instance is a GADT as well!
216
217 data instance T [a] where
218 T1 :: forall b. b -> T [Maybe b]
219
220 Hence we translate to
221
222 -- Wrapper
223 $WT1 :: forall b. b -> T [Maybe b]
224 $WT1 b v = T1 (Maybe b) b (Maybe b) v
225 `cast` sym (Co7T (Maybe b))
226
227 -- Worker
228 T1 :: forall c b. (c ~ Maybe b) => b -> :R7T c
229
230 -- Coercion from family type to representation type
231 Co7T a :: T [a] ~ :R7T a
232
233 Note [Newtype datacons]
234 ~~~~~~~~~~~~~~~~~~~~~~~
235 The "data constructor" for a newtype should always be vanilla. At one
236 point this wasn't true, because the newtype arising from
237 class C a => D a
238 looked like
239 newtype T:D a = D:D (C a)
240 so the data constructor for T:C had a single argument, namely the
241 predicate (C a). But now we treat that as an ordinary argument, not
242 part of the theta-type, so all is well.
243
244
245 ************************************************************************
246 * *
247 \subsection{Dictionary selectors}
248 * *
249 ************************************************************************
250
251 Selecting a field for a dictionary. If there is just one field, then
252 there's nothing to do.
253
254 Dictionary selectors may get nested forall-types. Thus:
255
256 class Foo a where
257 op :: forall b. Ord b => a -> b -> b
258
259 Then the top-level type for op is
260
261 op :: forall a. Foo a =>
262 forall b. Ord b =>
263 a -> b -> b
264
265 -}
266
267 mkDictSelId :: Name -- Name of one of the *value* selectors
268 -- (dictionary superclass or method)
269 -> Class -> Id
270 mkDictSelId name clas
271 = mkGlobalId (ClassOpId clas) name sel_ty info
272 where
273 tycon = classTyCon clas
274 sel_names = map idName (classAllSelIds clas)
275 new_tycon = isNewTyCon tycon
276 [data_con] = tyConDataCons tycon
277 tyvars = dataConUnivTyVars data_con
278 tc_binders = tyConBinders tycon
279 arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
280 val_index = assoc "MkId.mkDictSelId" (sel_names `zip` [0..]) name
281
282 sel_ty = mkForAllTys (zipWith mk_binder tc_binders tyvars) $
283 mkFunTy (mkClassPred clas (mkTyVarTys tyvars)) $
284 getNth arg_tys val_index
285
286 -- copy the visibility from the tycon binders. Consider:
287 -- class C a where foo :: Proxy a
288 -- In the type of foo, `a` must be Specified but `k` must be Invisible
289 mk_binder tc_binder tyvar
290 | Invisible <- binderVisibility tc_binder
291 = mkNamedBinder Invisible tyvar
292 | otherwise
293 = mkNamedBinder Specified tyvar -- don't just copy from tc_binder, because
294 -- tc_binders can be Visible
295
296 base_info = noCafIdInfo
297 `setArityInfo` 1
298 `setStrictnessInfo` strict_sig
299
300 info | new_tycon
301 = base_info `setInlinePragInfo` alwaysInlinePragma
302 `setUnfoldingInfo` mkInlineUnfolding (Just 1) (mkDictSelRhs clas val_index)
303 -- See Note [Single-method classes] in TcInstDcls
304 -- for why alwaysInlinePragma
305
306 | otherwise
307 = base_info `setRuleInfo` mkRuleInfo [rule]
308 -- Add a magic BuiltinRule, but no unfolding
309 -- so that the rule is always available to fire.
310 -- See Note [ClassOp/DFun selection] in TcInstDcls
311
312 n_ty_args = length tyvars
313
314 -- This is the built-in rule that goes
315 -- op (dfT d1 d2) ---> opT d1 d2
316 rule = BuiltinRule { ru_name = fsLit "Class op " `appendFS`
317 occNameFS (getOccName name)
318 , ru_fn = name
319 , ru_nargs = n_ty_args + 1
320 , ru_try = dictSelRule val_index n_ty_args }
321
322 -- The strictness signature is of the form U(AAAVAAAA) -> T
323 -- where the V depends on which item we are selecting
324 -- It's worth giving one, so that absence info etc is generated
325 -- even if the selector isn't inlined
326
327 strict_sig = mkClosedStrictSig [arg_dmd] topRes
328 arg_dmd | new_tycon = evalDmd
329 | otherwise = mkManyUsedDmd $
330 mkProdDmd [ if name == sel_name then evalDmd else absDmd
331 | sel_name <- sel_names ]
332
333 mkDictSelRhs :: Class
334 -> Int -- 0-indexed selector among (superclasses ++ methods)
335 -> CoreExpr
336 mkDictSelRhs clas val_index
337 = mkLams tyvars (Lam dict_id rhs_body)
338 where
339 tycon = classTyCon clas
340 new_tycon = isNewTyCon tycon
341 [data_con] = tyConDataCons tycon
342 tyvars = dataConUnivTyVars data_con
343 arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
344
345 the_arg_id = getNth arg_ids val_index
346 pred = mkClassPred clas (mkTyVarTys tyvars)
347 dict_id = mkTemplateLocal 1 pred
348 arg_ids = mkTemplateLocalsNum 2 arg_tys
349
350 rhs_body | new_tycon = unwrapNewTypeBody tycon (mkTyVarTys tyvars) (Var dict_id)
351 | otherwise = Case (Var dict_id) dict_id (idType the_arg_id)
352 [(DataAlt data_con, arg_ids, varToCoreExpr the_arg_id)]
353 -- varToCoreExpr needed for equality superclass selectors
354 -- sel a b d = case x of { MkC _ (g:a~b) _ -> CO g }
355
356 dictSelRule :: Int -> Arity -> RuleFun
357 -- Tries to persuade the argument to look like a constructor
358 -- application, using exprIsConApp_maybe, and then selects
359 -- from it
360 -- sel_i t1..tk (D t1..tk op1 ... opm) = opi
361 --
362 dictSelRule val_index n_ty_args _ id_unf _ args
363 | (dict_arg : _) <- drop n_ty_args args
364 , Just (_, _, con_args) <- exprIsConApp_maybe id_unf dict_arg
365 = Just (getNth con_args val_index)
366 | otherwise
367 = Nothing
368
369 {-
370 ************************************************************************
371 * *
372 Data constructors
373 * *
374 ************************************************************************
375 -}
376
377 mkDataConWorkId :: Name -> DataCon -> Id
378 mkDataConWorkId wkr_name data_con
379 | isNewTyCon tycon
380 = mkGlobalId (DataConWrapId data_con) wkr_name nt_wrap_ty nt_work_info
381 | otherwise
382 = mkGlobalId (DataConWorkId data_con) wkr_name alg_wkr_ty wkr_info
383
384 where
385 tycon = dataConTyCon data_con
386
387 ----------- Workers for data types --------------
388 alg_wkr_ty = dataConRepType data_con
389 wkr_arity = dataConRepArity data_con
390 wkr_info = noCafIdInfo
391 `setArityInfo` wkr_arity
392 `setStrictnessInfo` wkr_sig
393 `setUnfoldingInfo` evaldUnfolding -- Record that it's evaluated,
394 -- even if arity = 0
395
396 wkr_sig = mkClosedStrictSig (replicate wkr_arity topDmd) (dataConCPR data_con)
397 -- Note [Data-con worker strictness]
398 -- Notice that we do *not* say the worker is strict
399 -- even if the data constructor is declared strict
400 -- e.g. data T = MkT !(Int,Int)
401 -- Why? Because the *wrapper* is strict (and its unfolding has case
402 -- expressions that do the evals) but the *worker* itself is not.
403 -- If we pretend it is strict then when we see
404 -- case x of y -> $wMkT y
405 -- the simplifier thinks that y is "sure to be evaluated" (because
406 -- $wMkT is strict) and drops the case. No, $wMkT is not strict.
407 --
408 -- When the simplifer sees a pattern
409 -- case e of MkT x -> ...
410 -- it uses the dataConRepStrictness of MkT to mark x as evaluated;
411 -- but that's fine... dataConRepStrictness comes from the data con
412 -- not from the worker Id.
413
414 ----------- Workers for newtypes --------------
415 (nt_tvs, _, nt_arg_tys, _) = dataConSig data_con
416 res_ty_args = mkTyVarTys nt_tvs
417 nt_wrap_ty = dataConUserType data_con
418 nt_work_info = noCafIdInfo -- The NoCaf-ness is set by noCafIdInfo
419 `setArityInfo` 1 -- Arity 1
420 `setInlinePragInfo` alwaysInlinePragma
421 `setUnfoldingInfo` newtype_unf
422 id_arg1 = mkTemplateLocal 1 (head nt_arg_tys)
423 newtype_unf = ASSERT2( isVanillaDataCon data_con &&
424 isSingleton nt_arg_tys, ppr data_con )
425 -- Note [Newtype datacons]
426 mkCompulsoryUnfolding $
427 mkLams nt_tvs $ Lam id_arg1 $
428 wrapNewTypeBody tycon res_ty_args (Var id_arg1)
429
430 dataConCPR :: DataCon -> DmdResult
431 dataConCPR con
432 | isDataTyCon tycon -- Real data types only; that is,
433 -- not unboxed tuples or newtypes
434 , null (dataConExTyVars con) -- No existentials
435 , wkr_arity > 0
436 , wkr_arity <= mAX_CPR_SIZE
437 = if is_prod then vanillaCprProdRes (dataConRepArity con)
438 else cprSumRes (dataConTag con)
439 | otherwise
440 = topRes
441 where
442 is_prod = isProductTyCon tycon
443 tycon = dataConTyCon con
444 wkr_arity = dataConRepArity con
445
446 mAX_CPR_SIZE :: Arity
447 mAX_CPR_SIZE = 10
448 -- We do not treat very big tuples as CPR-ish:
449 -- a) for a start we get into trouble because there aren't
450 -- "enough" unboxed tuple types (a tiresome restriction,
451 -- but hard to fix),
452 -- b) more importantly, big unboxed tuples get returned mainly
453 -- on the stack, and are often then allocated in the heap
454 -- by the caller. So doing CPR for them may in fact make
455 -- things worse.
456
457 {-
458 -------------------------------------------------
459 -- Data constructor representation
460 --
461 -- This is where we decide how to wrap/unwrap the
462 -- constructor fields
463 --
464 --------------------------------------------------
465 -}
466
467 type Unboxer = Var -> UniqSM ([Var], CoreExpr -> CoreExpr)
468 -- Unbox: bind rep vars by decomposing src var
469
470 data Boxer = UnitBox | Boxer (TCvSubst -> UniqSM ([Var], CoreExpr))
471 -- Box: build src arg using these rep vars
472
473 newtype DataConBoxer = DCB ([Type] -> [Var] -> UniqSM ([Var], [CoreBind]))
474 -- Bind these src-level vars, returning the
475 -- rep-level vars to bind in the pattern
476
477 mkDataConRep :: DynFlags
478 -> FamInstEnvs
479 -> Name
480 -> Maybe [HsImplBang]
481 -- See Note [Bangs on imported data constructors]
482 -> DataCon
483 -> UniqSM DataConRep
484 mkDataConRep dflags fam_envs wrap_name mb_bangs data_con
485 | not wrapper_reqd
486 = return NoDataConRep
487
488 | otherwise
489 = do { wrap_args <- mapM newLocal wrap_arg_tys
490 ; wrap_body <- mk_rep_app (wrap_args `zip` dropList eq_spec unboxers)
491 initial_wrap_app
492
493 ; let wrap_id = mkGlobalId (DataConWrapId data_con) wrap_name wrap_ty wrap_info
494 wrap_info = noCafIdInfo
495 `setArityInfo` wrap_arity
496 -- It's important to specify the arity, so that partial
497 -- applications are treated as values
498 `setInlinePragInfo` alwaysInlinePragma
499 `setUnfoldingInfo` wrap_unf
500 `setStrictnessInfo` wrap_sig
501 -- We need to get the CAF info right here because TidyPgm
502 -- does not tidy the IdInfo of implicit bindings (like the wrapper)
503 -- so it not make sure that the CAF info is sane
504
505 wrap_sig = mkClosedStrictSig wrap_arg_dmds (dataConCPR data_con)
506 wrap_arg_dmds = map mk_dmd arg_ibangs
507 mk_dmd str | isBanged str = evalDmd
508 | otherwise = topDmd
509 -- The Cpr info can be important inside INLINE rhss, where the
510 -- wrapper constructor isn't inlined.
511 -- And the argument strictness can be important too; we
512 -- may not inline a contructor when it is partially applied.
513 -- For example:
514 -- data W = C !Int !Int !Int
515 -- ...(let w = C x in ...(w p q)...)...
516 -- we want to see that w is strict in its two arguments
517
518 wrap_unf = mkInlineUnfolding (Just wrap_arity) wrap_rhs
519 wrap_tvs = (univ_tvs `minusList` map eqSpecTyVar eq_spec) ++ ex_tvs
520 wrap_rhs = mkLams wrap_tvs $
521 mkLams wrap_args $
522 wrapFamInstBody tycon res_ty_args $
523 wrap_body
524
525 ; return (DCR { dcr_wrap_id = wrap_id
526 , dcr_boxer = mk_boxer boxers
527 , dcr_arg_tys = rep_tys
528 , dcr_stricts = rep_strs
529 , dcr_bangs = arg_ibangs }) }
530
531 where
532 (univ_tvs, ex_tvs, eq_spec, theta, orig_arg_tys, _orig_res_ty)
533 = dataConFullSig data_con
534 res_ty_args = substTyVars (mkTvSubstPrs (map eqSpecPair eq_spec)) univ_tvs
535
536 tycon = dataConTyCon data_con -- The representation TyCon (not family)
537 wrap_ty = dataConUserType data_con
538 ev_tys = eqSpecPreds eq_spec ++ theta
539 all_arg_tys = ev_tys ++ orig_arg_tys
540 ev_ibangs = map (const HsLazy) ev_tys
541 orig_bangs = dataConSrcBangs data_con
542
543 wrap_arg_tys = theta ++ orig_arg_tys
544 wrap_arity = length wrap_arg_tys
545 -- The wrap_args are the arguments *other than* the eq_spec
546 -- Because we are going to apply the eq_spec args manually in the
547 -- wrapper
548
549 arg_ibangs =
550 case mb_bangs of
551 Nothing -> zipWith (dataConSrcToImplBang dflags fam_envs)
552 orig_arg_tys orig_bangs
553 Just bangs -> bangs
554
555 (rep_tys_w_strs, wrappers)
556 = unzip (zipWith dataConArgRep all_arg_tys (ev_ibangs ++ arg_ibangs))
557
558 (unboxers, boxers) = unzip wrappers
559 (rep_tys, rep_strs) = unzip (concat rep_tys_w_strs)
560
561 wrapper_reqd = not (isNewTyCon tycon) -- Newtypes have only a worker
562 && (any isBanged (ev_ibangs ++ arg_ibangs)
563 -- Some forcing/unboxing (includes eq_spec)
564 || isFamInstTyCon tycon -- Cast result
565 || (not $ null eq_spec)) -- GADT
566
567 initial_wrap_app = Var (dataConWorkId data_con)
568 `mkTyApps` res_ty_args
569 `mkVarApps` ex_tvs
570 `mkCoApps` map (mkReflCo Nominal . eqSpecType) eq_spec
571
572 mk_boxer :: [Boxer] -> DataConBoxer
573 mk_boxer boxers = DCB (\ ty_args src_vars ->
574 do { let (ex_vars, term_vars) = splitAtList ex_tvs src_vars
575 subst1 = zipTvSubst univ_tvs ty_args
576 subst2 = extendTvSubstList subst1 ex_tvs
577 (mkTyVarTys ex_vars)
578 ; (rep_ids, binds) <- go subst2 boxers term_vars
579 ; return (ex_vars ++ rep_ids, binds) } )
580
581 go _ [] src_vars = ASSERT2( null src_vars, ppr data_con ) return ([], [])
582 go subst (UnitBox : boxers) (src_var : src_vars)
583 = do { (rep_ids2, binds) <- go subst boxers src_vars
584 ; return (src_var : rep_ids2, binds) }
585 go subst (Boxer boxer : boxers) (src_var : src_vars)
586 = do { (rep_ids1, arg) <- boxer subst
587 ; (rep_ids2, binds) <- go subst boxers src_vars
588 ; return (rep_ids1 ++ rep_ids2, NonRec src_var arg : binds) }
589 go _ (_:_) [] = pprPanic "mk_boxer" (ppr data_con)
590
591 mk_rep_app :: [(Id,Unboxer)] -> CoreExpr -> UniqSM CoreExpr
592 mk_rep_app [] con_app
593 = return con_app
594 mk_rep_app ((wrap_arg, unboxer) : prs) con_app
595 = do { (rep_ids, unbox_fn) <- unboxer wrap_arg
596 ; expr <- mk_rep_app prs (mkVarApps con_app rep_ids)
597 ; return (unbox_fn expr) }
598
599 {-
600 Note [Bangs on imported data constructors]
601 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
602
603 We pass Maybe [HsImplBang] to mkDataConRep to make use of HsImplBangs
604 from imported modules.
605
606 - Nothing <=> use HsSrcBangs
607 - Just bangs <=> use HsImplBangs
608
609 For imported types we can't work it all out from the HsSrcBangs,
610 because we want to be very sure to follow what the original module
611 (where the data type was declared) decided, and that depends on what
612 flags were enabled when it was compiled. So we record the decisions in
613 the interface file.
614
615 The HsImplBangs passed are in 1-1 correspondence with the
616 dataConOrigArgTys of the DataCon.
617
618 -}
619
620 -------------------------
621 newLocal :: Type -> UniqSM Var
622 newLocal ty = do { uniq <- getUniqueM
623 ; return (mkSysLocalOrCoVar (fsLit "dt") uniq ty) }
624
625 -- | Unpack/Strictness decisions from source module
626 dataConSrcToImplBang
627 :: DynFlags
628 -> FamInstEnvs
629 -> Type
630 -> HsSrcBang
631 -> HsImplBang
632
633 dataConSrcToImplBang dflags fam_envs arg_ty
634 (HsSrcBang ann unpk NoSrcStrict)
635 | xopt LangExt.StrictData dflags -- StrictData => strict field
636 = dataConSrcToImplBang dflags fam_envs arg_ty
637 (HsSrcBang ann unpk SrcStrict)
638 | otherwise -- no StrictData => lazy field
639 = HsLazy
640
641 dataConSrcToImplBang _ _ _ (HsSrcBang _ _ SrcLazy)
642 = HsLazy
643
644 dataConSrcToImplBang dflags fam_envs arg_ty
645 (HsSrcBang _ unpk_prag SrcStrict)
646 | not (gopt Opt_OmitInterfacePragmas dflags) -- Don't unpack if -fomit-iface-pragmas
647 -- Don't unpack if we aren't optimising; rather arbitrarily,
648 -- we use -fomit-iface-pragmas as the indication
649 , let mb_co = topNormaliseType_maybe fam_envs arg_ty
650 -- Unwrap type families and newtypes
651 arg_ty' = case mb_co of { Just (_,ty) -> ty; Nothing -> arg_ty }
652 , isUnpackableType dflags fam_envs arg_ty'
653 , (rep_tys, _) <- dataConArgUnpack arg_ty'
654 , case unpk_prag of
655 NoSrcUnpack ->
656 gopt Opt_UnboxStrictFields dflags
657 || (gopt Opt_UnboxSmallStrictFields dflags
658 && length rep_tys <= 1) -- See Note [Unpack one-wide fields]
659 srcUnpack -> isSrcUnpacked srcUnpack
660 = case mb_co of
661 Nothing -> HsUnpack Nothing
662 Just (co,_) -> HsUnpack (Just co)
663
664 | otherwise -- Record the strict-but-no-unpack decision
665 = HsStrict
666
667
668 -- | Wrappers/Workers and representation following Unpack/Strictness
669 -- decisions
670 dataConArgRep
671 :: Type
672 -> HsImplBang
673 -> ([(Type,StrictnessMark)] -- Rep types
674 ,(Unboxer,Boxer))
675
676 dataConArgRep arg_ty HsLazy
677 = ([(arg_ty, NotMarkedStrict)], (unitUnboxer, unitBoxer))
678
679 dataConArgRep arg_ty HsStrict
680 = ([(arg_ty, MarkedStrict)], (seqUnboxer, unitBoxer))
681
682 dataConArgRep arg_ty (HsUnpack Nothing)
683 | (rep_tys, wrappers) <- dataConArgUnpack arg_ty
684 = (rep_tys, wrappers)
685
686 dataConArgRep _ (HsUnpack (Just co))
687 | let co_rep_ty = pSnd (coercionKind co)
688 , (rep_tys, wrappers) <- dataConArgUnpack co_rep_ty
689 = (rep_tys, wrapCo co co_rep_ty wrappers)
690
691
692 -------------------------
693 wrapCo :: Coercion -> Type -> (Unboxer, Boxer) -> (Unboxer, Boxer)
694 wrapCo co rep_ty (unbox_rep, box_rep) -- co :: arg_ty ~ rep_ty
695 = (unboxer, boxer)
696 where
697 unboxer arg_id = do { rep_id <- newLocal rep_ty
698 ; (rep_ids, rep_fn) <- unbox_rep rep_id
699 ; let co_bind = NonRec rep_id (Var arg_id `Cast` co)
700 ; return (rep_ids, Let co_bind . rep_fn) }
701 boxer = Boxer $ \ subst ->
702 do { (rep_ids, rep_expr)
703 <- case box_rep of
704 UnitBox -> do { rep_id <- newLocal (TcType.substTy subst rep_ty)
705 ; return ([rep_id], Var rep_id) }
706 Boxer boxer -> boxer subst
707 ; let sco = substCoUnchecked subst co
708 ; return (rep_ids, rep_expr `Cast` mkSymCo sco) }
709
710 ------------------------
711 seqUnboxer :: Unboxer
712 seqUnboxer v = return ([v], \e -> Case (Var v) v (exprType e) [(DEFAULT, [], e)])
713
714 unitUnboxer :: Unboxer
715 unitUnboxer v = return ([v], \e -> e)
716
717 unitBoxer :: Boxer
718 unitBoxer = UnitBox
719
720 -------------------------
721 dataConArgUnpack
722 :: Type
723 -> ( [(Type, StrictnessMark)] -- Rep types
724 , (Unboxer, Boxer) )
725
726 dataConArgUnpack arg_ty
727 | Just (tc, tc_args) <- splitTyConApp_maybe arg_ty
728 , Just con <- tyConSingleAlgDataCon_maybe tc
729 -- NB: check for an *algebraic* data type
730 -- A recursive newtype might mean that
731 -- 'arg_ty' is a newtype
732 , let rep_tys = dataConInstArgTys con tc_args
733 = ASSERT( isVanillaDataCon con )
734 ( rep_tys `zip` dataConRepStrictness con
735 ,( \ arg_id ->
736 do { rep_ids <- mapM newLocal rep_tys
737 ; let unbox_fn body
738 = Case (Var arg_id) arg_id (exprType body)
739 [(DataAlt con, rep_ids, body)]
740 ; return (rep_ids, unbox_fn) }
741 , Boxer $ \ subst ->
742 do { rep_ids <- mapM (newLocal . TcType.substTyUnchecked subst) rep_tys
743 ; return (rep_ids, Var (dataConWorkId con)
744 `mkTyApps` (substTysUnchecked subst tc_args)
745 `mkVarApps` rep_ids ) } ) )
746 | otherwise
747 = pprPanic "dataConArgUnpack" (ppr arg_ty)
748 -- An interface file specified Unpacked, but we couldn't unpack it
749
750 isUnpackableType :: DynFlags -> FamInstEnvs -> Type -> Bool
751 -- True if we can unpack the UNPACK the argument type
752 -- See Note [Recursive unboxing]
753 -- We look "deeply" inside rather than relying on the DataCons
754 -- we encounter on the way, because otherwise we might well
755 -- end up relying on ourselves!
756 isUnpackableType dflags fam_envs ty
757 | Just (tc, _) <- splitTyConApp_maybe ty
758 , Just con <- tyConSingleAlgDataCon_maybe tc
759 , isVanillaDataCon con
760 = ok_con_args (unitNameSet (getName tc)) con
761 | otherwise
762 = False
763 where
764 ok_arg tcs (ty, bang) = not (attempt_unpack bang) || ok_ty tcs norm_ty
765 where
766 norm_ty = topNormaliseType fam_envs ty
767 ok_ty tcs ty
768 | Just (tc, _) <- splitTyConApp_maybe ty
769 , let tc_name = getName tc
770 = not (tc_name `elemNameSet` tcs)
771 && case tyConSingleAlgDataCon_maybe tc of
772 Just con | isVanillaDataCon con
773 -> ok_con_args (tcs `extendNameSet` getName tc) con
774 _ -> True
775 | otherwise
776 = True
777
778 ok_con_args tcs con
779 = all (ok_arg tcs) (dataConOrigArgTys con `zip` dataConSrcBangs con)
780 -- NB: dataConSrcBangs gives the *user* request;
781 -- We'd get a black hole if we used dataConImplBangs
782
783 attempt_unpack (HsSrcBang _ SrcUnpack NoSrcStrict)
784 = xopt LangExt.StrictData dflags
785 attempt_unpack (HsSrcBang _ SrcUnpack SrcStrict)
786 = True
787 attempt_unpack (HsSrcBang _ NoSrcUnpack SrcStrict)
788 = True -- Be conservative
789 attempt_unpack (HsSrcBang _ NoSrcUnpack NoSrcStrict)
790 = xopt LangExt.StrictData dflags -- Be conservative
791 attempt_unpack _ = False
792
793 {-
794 Note [Unpack one-wide fields]
795 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
796 The flag UnboxSmallStrictFields ensures that any field that can
797 (safely) be unboxed to a word-sized unboxed field, should be so unboxed.
798 For example:
799
800 data A = A Int#
801 newtype B = B A
802 data C = C !B
803 data D = D !C
804 data E = E !()
805 data F = F !D
806 data G = G !F !F
807
808 All of these should have an Int# as their representation, except
809 G which should have two Int#s.
810
811 However
812
813 data T = T !(S Int)
814 data S = S !a
815
816 Here we can represent T with an Int#.
817
818 Note [Recursive unboxing]
819 ~~~~~~~~~~~~~~~~~~~~~~~~~
820 Consider
821 data R = MkR {-# UNPACK #-} !S Int
822 data S = MkS {-# UNPACK #-} !Int
823 The representation arguments of MkR are the *representation* arguments
824 of S (plus Int); the rep args of MkS are Int#. This is all fine.
825
826 But be careful not to try to unbox this!
827 data T = MkT {-# UNPACK #-} !T Int
828 Because then we'd get an infinite number of arguments.
829
830 Here is a more complicated case:
831 data S = MkS {-# UNPACK #-} !T Int
832 data T = MkT {-# UNPACK #-} !S Int
833 Each of S and T must decide independently whether to unpack
834 and they had better not both say yes. So they must both say no.
835
836 Also behave conservatively when there is no UNPACK pragma
837 data T = MkS !T Int
838 with -funbox-strict-fields or -funbox-small-strict-fields
839 we need to behave as if there was an UNPACK pragma there.
840
841 But it's the *argument* type that matters. This is fine:
842 data S = MkS S !Int
843 because Int is non-recursive.
844
845 ************************************************************************
846 * *
847 Wrapping and unwrapping newtypes and type families
848 * *
849 ************************************************************************
850 -}
851
852 wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
853 -- The wrapper for the data constructor for a newtype looks like this:
854 -- newtype T a = MkT (a,Int)
855 -- MkT :: forall a. (a,Int) -> T a
856 -- MkT = /\a. \(x:(a,Int)). x `cast` sym (CoT a)
857 -- where CoT is the coercion TyCon associated with the newtype
858 --
859 -- The call (wrapNewTypeBody T [a] e) returns the
860 -- body of the wrapper, namely
861 -- e `cast` (CoT [a])
862 --
863 -- If a coercion constructor is provided in the newtype, then we use
864 -- it, otherwise the wrap/unwrap are both no-ops
865 --
866 -- If the we are dealing with a newtype *instance*, we have a second coercion
867 -- identifying the family instance with the constructor of the newtype
868 -- instance. This coercion is applied in any case (ie, composed with the
869 -- coercion constructor of the newtype or applied by itself).
870
871 wrapNewTypeBody tycon args result_expr
872 = ASSERT( isNewTyCon tycon )
873 wrapFamInstBody tycon args $
874 mkCast result_expr (mkSymCo co)
875 where
876 co = mkUnbranchedAxInstCo Representational (newTyConCo tycon) args []
877
878 -- When unwrapping, we do *not* apply any family coercion, because this will
879 -- be done via a CoPat by the type checker. We have to do it this way as
880 -- computing the right type arguments for the coercion requires more than just
881 -- a spliting operation (cf, TcPat.tcConPat).
882
883 unwrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
884 unwrapNewTypeBody tycon args result_expr
885 = ASSERT( isNewTyCon tycon )
886 mkCast result_expr (mkUnbranchedAxInstCo Representational (newTyConCo tycon) args [])
887
888 -- If the type constructor is a representation type of a data instance, wrap
889 -- the expression into a cast adjusting the expression type, which is an
890 -- instance of the representation type, to the corresponding instance of the
891 -- family instance type.
892 -- See Note [Wrappers for data instance tycons]
893 wrapFamInstBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
894 wrapFamInstBody tycon args body
895 | Just co_con <- tyConFamilyCoercion_maybe tycon
896 = mkCast body (mkSymCo (mkUnbranchedAxInstCo Representational co_con args []))
897 | otherwise
898 = body
899
900 -- Same as `wrapFamInstBody`, but for type family instances, which are
901 -- represented by a `CoAxiom`, and not a `TyCon`
902 wrapTypeFamInstBody :: CoAxiom br -> Int -> [Type] -> [Coercion]
903 -> CoreExpr -> CoreExpr
904 wrapTypeFamInstBody axiom ind args cos body
905 = mkCast body (mkSymCo (mkAxInstCo Representational axiom ind args cos))
906
907 wrapTypeUnbranchedFamInstBody :: CoAxiom Unbranched -> [Type] -> [Coercion]
908 -> CoreExpr -> CoreExpr
909 wrapTypeUnbranchedFamInstBody axiom
910 = wrapTypeFamInstBody axiom 0
911
912 unwrapFamInstScrut :: TyCon -> [Type] -> CoreExpr -> CoreExpr
913 unwrapFamInstScrut tycon args scrut
914 | Just co_con <- tyConFamilyCoercion_maybe tycon
915 = mkCast scrut (mkUnbranchedAxInstCo Representational co_con args []) -- data instances only
916 | otherwise
917 = scrut
918
919 unwrapTypeFamInstScrut :: CoAxiom br -> Int -> [Type] -> [Coercion]
920 -> CoreExpr -> CoreExpr
921 unwrapTypeFamInstScrut axiom ind args cos scrut
922 = mkCast scrut (mkAxInstCo Representational axiom ind args cos)
923
924 unwrapTypeUnbranchedFamInstScrut :: CoAxiom Unbranched -> [Type] -> [Coercion]
925 -> CoreExpr -> CoreExpr
926 unwrapTypeUnbranchedFamInstScrut axiom
927 = unwrapTypeFamInstScrut axiom 0
928
929 {-
930 ************************************************************************
931 * *
932 \subsection{Primitive operations}
933 * *
934 ************************************************************************
935 -}
936
937 mkPrimOpId :: PrimOp -> Id
938 mkPrimOpId prim_op
939 = id
940 where
941 (tyvars,arg_tys,res_ty, arity, strict_sig) = primOpSig prim_op
942 ty = mkSpecForAllTys tyvars (mkFunTys arg_tys res_ty)
943 name = mkWiredInName gHC_PRIM (primOpOcc prim_op)
944 (mkPrimOpIdUnique (primOpTag prim_op))
945 (AnId id) UserSyntax
946 id = mkGlobalId (PrimOpId prim_op) name ty info
947
948 info = noCafIdInfo
949 `setRuleInfo` mkRuleInfo (maybeToList $ primOpRules name prim_op)
950 `setArityInfo` arity
951 `setStrictnessInfo` strict_sig
952 `setInlinePragInfo` neverInlinePragma
953 -- We give PrimOps a NOINLINE pragma so that we don't
954 -- get silly warnings from Desugar.dsRule (the inline_shadows_rule
955 -- test) about a RULE conflicting with a possible inlining
956 -- cf Trac #7287
957
958 -- For each ccall we manufacture a separate CCallOpId, giving it
959 -- a fresh unique, a type that is correct for this particular ccall,
960 -- and a CCall structure that gives the correct details about calling
961 -- convention etc.
962 --
963 -- The *name* of this Id is a local name whose OccName gives the full
964 -- details of the ccall, type and all. This means that the interface
965 -- file reader can reconstruct a suitable Id
966
967 mkFCallId :: DynFlags -> Unique -> ForeignCall -> Type -> Id
968 mkFCallId dflags uniq fcall ty
969 = ASSERT( isEmptyVarSet (tyCoVarsOfType ty) )
970 -- A CCallOpId should have no free type variables;
971 -- when doing substitutions won't substitute over it
972 mkGlobalId (FCallId fcall) name ty info
973 where
974 occ_str = showSDoc dflags (braces (ppr fcall <+> ppr ty))
975 -- The "occurrence name" of a ccall is the full info about the
976 -- ccall; it is encoded, but may have embedded spaces etc!
977
978 name = mkFCallName uniq occ_str
979
980 info = noCafIdInfo
981 `setArityInfo` arity
982 `setStrictnessInfo` strict_sig
983
984 (bndrs, _) = tcSplitPiTys ty
985 arity = count isIdLikeBinder bndrs
986
987 strict_sig = mkClosedStrictSig (replicate arity topDmd) topRes
988 -- the call does not claim to be strict in its arguments, since they
989 -- may be lifted (foreign import prim) and the called code doesn't
990 -- necessarily force them. See Trac #11076.
991 {-
992 ************************************************************************
993 * *
994 \subsection{DictFuns and default methods}
995 * *
996 ************************************************************************
997
998 Note [Dict funs and default methods]
999 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1000 Dict funs and default methods are *not* ImplicitIds. Their definition
1001 involves user-written code, so we can't figure out their strictness etc
1002 based on fixed info, as we can for constructors and record selectors (say).
1003
1004 NB: See also Note [Exported LocalIds] in Id
1005 -}
1006
1007 mkDictFunId :: Name -- Name to use for the dict fun;
1008 -> [TyVar]
1009 -> ThetaType
1010 -> Class
1011 -> [Type]
1012 -> Id
1013 -- Implements the DFun Superclass Invariant (see TcInstDcls)
1014 -- See Note [Dict funs and default methods]
1015
1016 mkDictFunId dfun_name tvs theta clas tys
1017 = mkExportedLocalId (DFunId is_nt)
1018 dfun_name
1019 dfun_ty
1020 where
1021 is_nt = isNewTyCon (classTyCon clas)
1022 dfun_ty = mkDictFunTy tvs theta clas tys
1023
1024 mkDictFunTy :: [TyVar] -> ThetaType -> Class -> [Type] -> Type
1025 mkDictFunTy tvs theta clas tys
1026 = mkSpecSigmaTy tvs theta (mkClassPred clas tys)
1027
1028 {-
1029 ************************************************************************
1030 * *
1031 \subsection{Un-definable}
1032 * *
1033 ************************************************************************
1034
1035 These Ids can't be defined in Haskell. They could be defined in
1036 unfoldings in the wired-in GHC.Prim interface file, but we'd have to
1037 ensure that they were definitely, definitely inlined, because there is
1038 no curried identifier for them. That's what mkCompulsoryUnfolding
1039 does. If we had a way to get a compulsory unfolding from an interface
1040 file, we could do that, but we don't right now.
1041
1042 unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that
1043 just gets expanded into a type coercion wherever it occurs. Hence we
1044 add it as a built-in Id with an unfolding here.
1045
1046 The type variables we use here are "open" type variables: this means
1047 they can unify with both unlifted and lifted types. Hence we provide
1048 another gun with which to shoot yourself in the foot.
1049 -}
1050
1051 lazyIdName, unsafeCoerceName, nullAddrName, seqName,
1052 realWorldName, voidPrimIdName, coercionTokenName,
1053 magicDictName, coerceName, proxyName, dollarName, oneShotName,
1054 runRWName :: Name
1055 unsafeCoerceName = mkWiredInIdName gHC_PRIM (fsLit "unsafeCoerce#") unsafeCoerceIdKey unsafeCoerceId
1056 nullAddrName = mkWiredInIdName gHC_PRIM (fsLit "nullAddr#") nullAddrIdKey nullAddrId
1057 seqName = mkWiredInIdName gHC_PRIM (fsLit "seq") seqIdKey seqId
1058 realWorldName = mkWiredInIdName gHC_PRIM (fsLit "realWorld#") realWorldPrimIdKey realWorldPrimId
1059 voidPrimIdName = mkWiredInIdName gHC_PRIM (fsLit "void#") voidPrimIdKey voidPrimId
1060 lazyIdName = mkWiredInIdName gHC_MAGIC (fsLit "lazy") lazyIdKey lazyId
1061 coercionTokenName = mkWiredInIdName gHC_PRIM (fsLit "coercionToken#") coercionTokenIdKey coercionTokenId
1062 magicDictName = mkWiredInIdName gHC_PRIM (fsLit "magicDict") magicDictKey magicDictId
1063 coerceName = mkWiredInIdName gHC_PRIM (fsLit "coerce") coerceKey coerceId
1064 proxyName = mkWiredInIdName gHC_PRIM (fsLit "proxy#") proxyHashKey proxyHashId
1065 dollarName = mkWiredInIdName gHC_BASE (fsLit "$") dollarIdKey dollarId
1066 oneShotName = mkWiredInIdName gHC_MAGIC (fsLit "oneShot") oneShotKey oneShotId
1067 runRWName = mkWiredInIdName gHC_MAGIC (fsLit "runRW#") runRWKey runRWId
1068
1069 dollarId :: Id -- Note [dollarId magic]
1070 dollarId = pcMiscPrelId dollarName ty
1071 (noCafIdInfo `setUnfoldingInfo` unf)
1072 where
1073 fun_ty = mkFunTy alphaTy openBetaTy
1074 ty = mkSpecForAllTys [runtimeRep2TyVar, alphaTyVar, openBetaTyVar] $
1075 mkFunTy fun_ty fun_ty
1076 unf = mkInlineUnfolding (Just 2) rhs
1077 [f,x] = mkTemplateLocals [fun_ty, alphaTy]
1078 rhs = mkLams [runtimeRep2TyVar, alphaTyVar, openBetaTyVar, f, x] $
1079 App (Var f) (Var x)
1080
1081 ------------------------------------------------
1082 -- proxy# :: forall a. Proxy# a
1083 proxyHashId :: Id
1084 proxyHashId
1085 = pcMiscPrelId proxyName ty
1086 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) -- Note [evaldUnfoldings]
1087 where
1088 ty = mkSpecForAllTys [kv, tv] (mkProxyPrimTy k t)
1089 kv = kKiVar
1090 k = mkTyVarTy kv
1091 [tv] = mkTemplateTyVars [k]
1092 t = mkTyVarTy tv
1093
1094 ------------------------------------------------
1095 -- unsafeCoerce# :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
1096 -- (a :: TYPE r1) (b :: TYPE r2).
1097 -- a -> b
1098 unsafeCoerceId :: Id
1099 unsafeCoerceId
1100 = pcMiscPrelId unsafeCoerceName ty info
1101 where
1102 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1103 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1104
1105 tvs = [ runtimeRep1TyVar, runtimeRep2TyVar
1106 , openAlphaTyVar, openBetaTyVar ]
1107
1108 ty = mkSpecForAllTys tvs $ mkFunTy openAlphaTy openBetaTy
1109
1110 [x] = mkTemplateLocals [openAlphaTy]
1111 rhs = mkLams (tvs ++ [x]) $
1112 Cast (Var x) (mkUnsafeCo Representational openAlphaTy openBetaTy)
1113
1114 ------------------------------------------------
1115 nullAddrId :: Id
1116 -- nullAddr# :: Addr#
1117 -- The reason is is here is because we don't provide
1118 -- a way to write this literal in Haskell.
1119 nullAddrId = pcMiscPrelId nullAddrName addrPrimTy info
1120 where
1121 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1122 `setUnfoldingInfo` mkCompulsoryUnfolding (Lit nullAddrLit)
1123
1124 ------------------------------------------------
1125 seqId :: Id -- See Note [seqId magic]
1126 seqId = pcMiscPrelId seqName ty info
1127 where
1128 info = noCafIdInfo `setInlinePragInfo` inline_prag
1129 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1130 `setRuleInfo` mkRuleInfo [seq_cast_rule]
1131
1132 inline_prag
1133 = alwaysInlinePragma `setInlinePragmaActivation` ActiveAfter "0" 0
1134 -- Make 'seq' not inline-always, so that simpleOptExpr
1135 -- (see CoreSubst.simple_app) won't inline 'seq' on the
1136 -- LHS of rules. That way we can have rules for 'seq';
1137 -- see Note [seqId magic]
1138
1139 ty = mkSpecForAllTys [alphaTyVar,betaTyVar]
1140 (mkFunTy alphaTy (mkFunTy betaTy betaTy))
1141
1142 [x,y] = mkTemplateLocals [alphaTy, betaTy]
1143 rhs = mkLams [alphaTyVar,betaTyVar,x,y] (Case (Var x) x betaTy [(DEFAULT, [], Var y)])
1144
1145 -- See Note [Built-in RULES for seq]
1146 -- NB: ru_nargs = 3, not 4, to match the code in
1147 -- Simplify.rebuildCase which tries to apply this rule
1148 seq_cast_rule = BuiltinRule { ru_name = fsLit "seq of cast"
1149 , ru_fn = seqName
1150 , ru_nargs = 3
1151 , ru_try = match_seq_of_cast }
1152
1153 match_seq_of_cast :: RuleFun
1154 -- See Note [Built-in RULES for seq]
1155 match_seq_of_cast _ _ _ [Type _, Type res_ty, Cast scrut co]
1156 = Just (fun `App` scrut)
1157 where
1158 fun = Lam x $ Lam y $
1159 Case (Var x) x res_ty [(DEFAULT,[],Var y)]
1160 -- Generate a Case directly, not a call to seq, which
1161 -- might be ill-kinded if res_ty is unboxed
1162 [x,y] = mkTemplateLocals [scrut_ty, res_ty]
1163 scrut_ty = pFst (coercionKind co)
1164
1165 match_seq_of_cast _ _ _ _ = Nothing
1166
1167 ------------------------------------------------
1168 lazyId :: Id -- See Note [lazyId magic]
1169 lazyId = pcMiscPrelId lazyIdName ty info
1170 where
1171 info = noCafIdInfo
1172 ty = mkSpecForAllTys [alphaTyVar] (mkFunTy alphaTy alphaTy)
1173
1174 oneShotId :: Id -- See Note [The oneShot function]
1175 oneShotId = pcMiscPrelId oneShotName ty info
1176 where
1177 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1178 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1179 ty = mkSpecForAllTys [ runtimeRep1TyVar, runtimeRep2TyVar
1180 , openAlphaTyVar, openBetaTyVar ]
1181 (mkFunTy fun_ty fun_ty)
1182 fun_ty = mkFunTy openAlphaTy openBetaTy
1183 [body, x] = mkTemplateLocals [fun_ty, openAlphaTy]
1184 x' = setOneShotLambda x
1185 rhs = mkLams [ runtimeRep1TyVar, runtimeRep2TyVar
1186 , openAlphaTyVar, openBetaTyVar
1187 , body, x'] $
1188 Var body `App` Var x
1189
1190 runRWId :: Id -- See Note [runRW magic] in this module
1191 runRWId = pcMiscPrelId runRWName ty info
1192 where
1193 info = noCafIdInfo `setInlinePragInfo` neverInlinePragma
1194 `setStrictnessInfo` strict_sig
1195 `setArityInfo` 1
1196 strict_sig = mkClosedStrictSig [strictApply1Dmd] topRes
1197 -- Important to express its strictness,
1198 -- since it is not inlined until CorePrep
1199 -- Also see Note [runRW arg] in CorePrep
1200
1201 -- State# RealWorld
1202 stateRW = mkTyConApp statePrimTyCon [realWorldTy]
1203 -- (# State# RealWorld, o #)
1204 ret_ty = mkTupleTy Unboxed [stateRW, openAlphaTy]
1205 -- State# RealWorld -> (# State# RealWorld, o #)
1206 arg_ty = stateRW `mkFunTy` ret_ty
1207 -- (State# RealWorld -> (# State# RealWorld, o #))
1208 -- -> (# State# RealWorld, o #)
1209 ty = mkSpecForAllTys [runtimeRep1TyVar, openAlphaTyVar] $
1210 arg_ty `mkFunTy` ret_ty
1211
1212 --------------------------------------------------------------------------------
1213 magicDictId :: Id -- See Note [magicDictId magic]
1214 magicDictId = pcMiscPrelId magicDictName ty info
1215 where
1216 info = noCafIdInfo `setInlinePragInfo` neverInlinePragma
1217 ty = mkSpecForAllTys [alphaTyVar] alphaTy
1218
1219 --------------------------------------------------------------------------------
1220
1221 coerceId :: Id
1222 coerceId = pcMiscPrelId coerceName ty info
1223 where
1224 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1225 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1226 eqRTy = mkTyConApp coercibleTyCon [ liftedTypeKind
1227 , alphaTy, betaTy ]
1228 eqRPrimTy = mkTyConApp eqReprPrimTyCon [ liftedTypeKind
1229 , liftedTypeKind
1230 , alphaTy, betaTy ]
1231 ty = mkSpecForAllTys [alphaTyVar, betaTyVar] $
1232 mkFunTys [eqRTy, alphaTy] betaTy
1233
1234 [eqR,x,eq] = mkTemplateLocals [eqRTy, alphaTy, eqRPrimTy]
1235 rhs = mkLams [alphaTyVar, betaTyVar, eqR, x] $
1236 mkWildCase (Var eqR) eqRTy betaTy $
1237 [(DataAlt coercibleDataCon, [eq], Cast (Var x) (mkCoVarCo eq))]
1238
1239 {-
1240 Note [dollarId magic]
1241 ~~~~~~~~~~~~~~~~~~~~~
1242 The only reason that ($) is wired in is so that its type can be
1243 forall (a:*, b:Open). (a->b) -> a -> b
1244 That is, the return type can be unboxed. E.g. this is OK
1245 foo $ True where foo :: Bool -> Int#
1246 because ($) doesn't inspect or move the result of the call to foo.
1247 See Trac #8739.
1248
1249 There is a special typing rule for ($) in TcExpr, so the type of ($)
1250 isn't looked at there, BUT Lint subsequently (and rightly) complains
1251 if sees ($) applied to Int# (say), unless we give it a wired-in type
1252 as we do here.
1253
1254 Note [Unsafe coerce magic]
1255 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1256 We define a *primitive*
1257 GHC.Prim.unsafeCoerce#
1258 and then in the base library we define the ordinary function
1259 Unsafe.Coerce.unsafeCoerce :: forall (a:*) (b:*). a -> b
1260 unsafeCoerce x = unsafeCoerce# x
1261
1262 Notice that unsafeCoerce has a civilized (albeit still dangerous)
1263 polymorphic type, whose type args have kind *. So you can't use it on
1264 unboxed values (unsafeCoerce 3#).
1265
1266 In contrast unsafeCoerce# is even more dangerous because you *can* use
1267 it on unboxed things, (unsafeCoerce# 3#) :: Int. Its type is
1268 forall (a:OpenKind) (b:OpenKind). a -> b
1269
1270 Note [seqId magic]
1271 ~~~~~~~~~~~~~~~~~~
1272 'GHC.Prim.seq' is special in several ways.
1273
1274 a) In source Haskell its second arg can have an unboxed type
1275 x `seq` (v +# w)
1276 But see Note [Typing rule for seq] in TcExpr, which
1277 explains why we give seq itself an ordinary type
1278 seq :: forall a b. a -> b -> b
1279 and treat it as a language construct from a typing point of view.
1280
1281 b) Its fixity is set in LoadIface.ghcPrimIface
1282
1283 c) It has quite a bit of desugaring magic.
1284 See DsUtils.hs Note [Desugaring seq (1)] and (2) and (3)
1285
1286 d) There is some special rule handing: Note [User-defined RULES for seq]
1287
1288 Note [User-defined RULES for seq]
1289 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1290 Roman found situations where he had
1291 case (f n) of _ -> e
1292 where he knew that f (which was strict in n) would terminate if n did.
1293 Notice that the result of (f n) is discarded. So it makes sense to
1294 transform to
1295 case n of _ -> e
1296
1297 Rather than attempt some general analysis to support this, I've added
1298 enough support that you can do this using a rewrite rule:
1299
1300 RULE "f/seq" forall n. seq (f n) = seq n
1301
1302 You write that rule. When GHC sees a case expression that discards
1303 its result, it mentally transforms it to a call to 'seq' and looks for
1304 a RULE. (This is done in Simplify.rebuildCase.) As usual, the
1305 correctness of the rule is up to you.
1306
1307 VERY IMPORTANT: to make this work, we give the RULE an arity of 1, not 2.
1308 If we wrote
1309 RULE "f/seq" forall n e. seq (f n) e = seq n e
1310 with rule arity 2, then two bad things would happen:
1311
1312 - The magical desugaring done in Note [seqId magic] item (c)
1313 for saturated application of 'seq' would turn the LHS into
1314 a case expression!
1315
1316 - The code in Simplify.rebuildCase would need to actually supply
1317 the value argument, which turns out to be awkward.
1318
1319 Note [Built-in RULES for seq]
1320 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1321 We also have the following built-in rule for seq
1322
1323 seq (x `cast` co) y = seq x y
1324
1325 This eliminates unnecessary casts and also allows other seq rules to
1326 match more often. Notably,
1327
1328 seq (f x `cast` co) y --> seq (f x) y
1329
1330 and now a user-defined rule for seq (see Note [User-defined RULES for seq])
1331 may fire.
1332
1333
1334 Note [lazyId magic]
1335 ~~~~~~~~~~~~~~~~~~~
1336 lazy :: forall a?. a? -> a? (i.e. works for unboxed types too)
1337
1338 'lazy' is used to make sure that a sub-expression, and its free variables,
1339 are truly used call-by-need, with no code motion. Key examples:
1340
1341 * pseq: pseq a b = a `seq` lazy b
1342 We want to make sure that the free vars of 'b' are not evaluated
1343 before 'a', even though the expression is plainly strict in 'b'.
1344
1345 * catch: catch a b = catch# (lazy a) b
1346 Again, it's clear that 'a' will be evaluated strictly (and indeed
1347 applied to a state token) but we want to make sure that any exceptions
1348 arising from the evaluation of 'a' are caught by the catch (see
1349 Trac #11555).
1350
1351 Implementing 'lazy' is a bit tricky:
1352
1353 * It must not have a strictness signature: by being a built-in Id,
1354 all the info about lazyId comes from here, not from GHC.Base.hi.
1355 This is important, because the strictness analyser will spot it as
1356 strict!
1357
1358 * It must not have an unfolding: it gets "inlined" by a HACK in
1359 CorePrep. It's very important to do this inlining *after* unfoldings
1360 are exposed in the interface file. Otherwise, the unfolding for
1361 (say) pseq in the interface file will not mention 'lazy', so if we
1362 inline 'pseq' we'll totally miss the very thing that 'lazy' was
1363 there for in the first place. See Trac #3259 for a real world
1364 example.
1365
1366 * Suppose CorePrep sees (catch# (lazy e) b). At all costs we must
1367 avoid using call by value here:
1368 case e of r -> catch# r b
1369 Avoiding that is the whole point of 'lazy'. So in CorePrep (which
1370 generate the 'case' expression for a call-by-value call) we must
1371 spot the 'lazy' on the arg (in CorePrep.cpeApp), and build a 'let'
1372 instead.
1373
1374 * lazyId is defined in GHC.Base, so we don't *have* to inline it. If it
1375 appears un-applied, we'll end up just calling it.
1376
1377 Note [runRW magic]
1378 ~~~~~~~~~~~~~~~~~~
1379 Some definitions, for instance @runST@, must have careful control over float out
1380 of the bindings in their body. Consider this use of @runST@,
1381
1382 f x = runST ( \ s -> let (a, s') = newArray# 100 [] s
1383 (_, s'') = fill_in_array_or_something a x s'
1384 in freezeArray# a s'' )
1385
1386 If we inline @runST@, we'll get:
1387
1388 f x = let (a, s') = newArray# 100 [] realWorld#{-NB-}
1389 (_, s'') = fill_in_array_or_something a x s'
1390 in freezeArray# a s''
1391
1392 And now if we allow the @newArray#@ binding to float out to become a CAF,
1393 we end up with a result that is totally and utterly wrong:
1394
1395 f = let (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
1396 in \ x ->
1397 let (_, s'') = fill_in_array_or_something a x s'
1398 in freezeArray# a s''
1399
1400 All calls to @f@ will share a {\em single} array! Clearly this is nonsense and
1401 must be prevented.
1402
1403 This is what @runRW#@ gives us: by being inlined extremely late in the
1404 optimization (right before lowering to STG, in CorePrep), we can ensure that
1405 no further floating will occur. This allows us to safely inline things like
1406 @runST@, which are otherwise needlessly expensive (see #10678 and #5916).
1407
1408 While the definition of @GHC.Magic.runRW#@, we override its type in @MkId@
1409 to be open-kinded,
1410
1411 runRW# :: forall (r1 :: RuntimeRep). (o :: TYPE r)
1412 => (State# RealWorld -> (# State# RealWorld, o #))
1413 -> (# State# RealWorld, o #)
1414
1415
1416 Note [The oneShot function]
1417 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1418 In the context of making left-folds fuse somewhat okish (see ticket #7994
1419 and Note [Left folds via right fold]) it was determined that it would be useful
1420 if library authors could explicitly tell the compiler that a certain lambda is
1421 called at most once. The oneShot function allows that.
1422
1423 'oneShot' is open kinded, i.e. the type variables can refer to unlifted
1424 types as well (Trac #10744); e.g.
1425 oneShot (\x:Int# -> x +# 1#)
1426
1427 Like most magic functions it has a compulsary unfolding, so there is no need
1428 for a real definition somewhere. We have one in GHC.Magic for the convenience
1429 of putting the documentation there.
1430
1431 It uses `setOneShotLambda` on the lambda's binder. That is the whole magic:
1432
1433 A typical call looks like
1434 oneShot (\y. e)
1435 after unfolding the definition `oneShot = \f \x[oneshot]. f x` we get
1436 (\f \x[oneshot]. f x) (\y. e)
1437 --> \x[oneshot]. ((\y.e) x)
1438 --> \x[oneshot] e[x/y]
1439 which is what we want.
1440
1441 It is only effective if the one-shot info survives as long as possible; in
1442 particular it must make it into the interface in unfoldings. See Note [Preserve
1443 OneShotInfo] in CoreTidy.
1444
1445 Also see https://ghc.haskell.org/trac/ghc/wiki/OneShot.
1446
1447
1448 Note [magicDictId magic]
1449 ~~~~~~~~~~~~~~~~~~~~~~~~~
1450 The identifier `magicDict` is just a place-holder, which is used to
1451 implement a primitve that we cannot define in Haskell but we can write
1452 in Core. It is declared with a place-holder type:
1453
1454 magicDict :: forall a. a
1455
1456 The intention is that the identifier will be used in a very specific way,
1457 to create dictionaries for classes with a single method. Consider a class
1458 like this:
1459
1460 class C a where
1461 f :: T a
1462
1463 We are going to use `magicDict`, in conjunction with a built-in Prelude
1464 rule, to cast values of type `T a` into dictionaries for `C a`. To do
1465 this, we define a function like this in the library:
1466
1467 data WrapC a b = WrapC (C a => Proxy a -> b)
1468
1469 withT :: (C a => Proxy a -> b)
1470 -> T a -> Proxy a -> b
1471 withT f x y = magicDict (WrapC f) x y
1472
1473 The purpose of `WrapC` is to avoid having `f` instantiated.
1474 Also, it avoids impredicativity, because `magicDict`'s type
1475 cannot be instantiated with a forall. The field of `WrapC` contains
1476 a `Proxy` parameter which is used to link the type of the constraint,
1477 `C a`, with the type of the `Wrap` value being made.
1478
1479 Next, we add a built-in Prelude rule (see prelude/PrelRules.hs),
1480 which will replace the RHS of this definition with the appropriate
1481 definition in Core. The rewrite rule works as follows:
1482
1483 magicDict @t (wrap @a @b f) x y
1484 ---->
1485 f (x `cast` co a) y
1486
1487 The `co` coercion is the newtype-coercion extracted from the type-class.
1488 The type class is obtain by looking at the type of wrap.
1489
1490
1491 -------------------------------------------------------------
1492 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
1493 nasty as-is, change it back to a literal (@Literal@).
1494
1495 voidArgId is a Local Id used simply as an argument in functions
1496 where we just want an arg to avoid having a thunk of unlifted type.
1497 E.g.
1498 x = \ void :: Void# -> (# p, q #)
1499
1500 This comes up in strictness analysis
1501
1502 Note [evaldUnfoldings]
1503 ~~~~~~~~~~~~~~~~~~~~~~
1504 The evaldUnfolding makes it look that some primitive value is
1505 evaluated, which in turn makes Simplify.interestingArg return True,
1506 which in turn makes INLINE things applied to said value likely to be
1507 inlined.
1508 -}
1509
1510 realWorldPrimId :: Id -- :: State# RealWorld
1511 realWorldPrimId = pcMiscPrelId realWorldName realWorldStatePrimTy
1512 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding -- Note [evaldUnfoldings]
1513 `setOneShotInfo` stateHackOneShot)
1514
1515 voidPrimId :: Id -- Global constant :: Void#
1516 voidPrimId = pcMiscPrelId voidPrimIdName voidPrimTy
1517 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) -- Note [evaldUnfoldings]
1518
1519 voidArgId :: Id -- Local lambda-bound :: Void#
1520 voidArgId = mkSysLocal (fsLit "void") voidArgIdKey voidPrimTy
1521
1522 coercionTokenId :: Id -- :: () ~ ()
1523 coercionTokenId -- Used to replace Coercion terms when we go to STG
1524 = pcMiscPrelId coercionTokenName
1525 (mkTyConApp eqPrimTyCon [liftedTypeKind, liftedTypeKind, unitTy, unitTy])
1526 noCafIdInfo
1527
1528 pcMiscPrelId :: Name -> Type -> IdInfo -> Id
1529 pcMiscPrelId name ty info
1530 = mkVanillaGlobalWithInfo name ty info
1531 -- We lie and say the thing is imported; otherwise, we get into
1532 -- a mess with dependency analysis; e.g., core2stg may heave in
1533 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
1534 -- being compiled, then it's just a matter of luck if the definition
1535 -- will be in "the right place" to be in scope.