2 -- | Handy functions for creating much Core syntax
4 -- * Constructing normal syntax
6 mkCoreApp, mkCoreApps, mkCoreConApps,
7 mkCoreLams, mkWildCase, mkIfThenElse,
8 mkWildValBinder, mkWildEvBinder,
10 -- * Constructing boxed literals
11 mkWordExpr, mkWordExprWord,
12 mkIntExpr, mkIntExprInt,
14 mkFloatExpr, mkDoubleExpr,
15 mkCharExpr, mkStringExpr, mkStringExprFS,
17 -- * Constructing general big tuples
21 -- * Constructing small tuples
22 mkCoreVarTup, mkCoreVarTupTy, mkCoreTup,
24 -- * Constructing big tuples
25 mkBigCoreVarTup, mkBigCoreVarTupTy,
26 mkBigCoreTup, mkBigCoreTupTy,
28 -- * Deconstructing small tuples
29 mkSmallTupleSelector, mkSmallTupleCase,
31 -- * Deconstructing big tuples
32 mkTupleSelector, mkTupleCase,
34 -- * Constructing list expressions
35 mkNilExpr, mkConsExpr, mkListExpr,
36 mkFoldrExpr, mkBuildExpr,
39 mkRuntimeErrorApp, mkImpossibleExpr, errorIds,
40 rEC_CON_ERROR_ID, iRREFUT_PAT_ERROR_ID, rUNTIME_ERROR_ID,
41 nON_EXHAUSTIVE_GUARDS_ERROR_ID, nO_METHOD_BINDING_ERROR_ID,
42 pAT_ERROR_ID, eRROR_ID, rEC_SEL_ERROR_ID, aBSENT_ERROR_ID
45 #include "HsVersions.h"
49 import Var ( EvVar, mkWildCoVar, setTyVarUnique )
52 import CoreUtils ( exprType, needsCaseBinding, bindNonRec )
59 import TcType ( mkSigmaTy )
62 import DataCon ( DataCon, dataConWorkId )
69 import Util ( notNull, zipEqual )
72 import Data.Char ( ord )
75 infixl 4 `mkCoreApp`, `mkCoreApps`
78 %************************************************************************
80 \subsection{Basic CoreSyn construction}
82 %************************************************************************
85 -- | Bind a binding group over an expression, using a @let@ or @case@ as
86 -- appropriate (see "CoreSyn#let_app_invariant")
87 mkCoreLet :: CoreBind -> CoreExpr -> CoreExpr
88 mkCoreLet (NonRec bndr rhs) body -- See Note [CoreSyn let/app invariant]
89 | needsCaseBinding (idType bndr) rhs
90 = Case rhs bndr (exprType body) [(DEFAULT,[],body)]
94 -- | Bind a list of binding groups over an expression. The leftmost binding
95 -- group becomes the outermost group in the resulting expression
96 mkCoreLets :: [CoreBind] -> CoreExpr -> CoreExpr
97 mkCoreLets binds body = foldr mkCoreLet body binds
99 -- | Construct an expression which represents the application of one expression
101 mkCoreApp :: CoreExpr -> CoreExpr -> CoreExpr
102 -- Check the invariant that the arg of an App is ok-for-speculation if unlifted
103 -- See CoreSyn Note [CoreSyn let/app invariant]
104 mkCoreApp fun (Type ty) = App fun (Type ty)
105 mkCoreApp fun arg = ASSERT2( isFunTy fun_ty, ppr fun $$ ppr arg )
106 mk_val_app fun arg arg_ty res_ty
108 fun_ty = exprType fun
109 (arg_ty, res_ty) = splitFunTy fun_ty
111 -- | Construct an expression which represents the application of a number of
112 -- expressions to another. The leftmost expression in the list is applied first
113 mkCoreApps :: CoreExpr -> [CoreExpr] -> CoreExpr
114 -- Slightly more efficient version of (foldl mkCoreApp)
115 mkCoreApps orig_fun orig_args
116 = go orig_fun (exprType orig_fun) orig_args
119 go fun fun_ty (Type ty : args) = go (App fun (Type ty)) (applyTy fun_ty ty) args
120 go fun fun_ty (arg : args) = ASSERT2( isFunTy fun_ty, ppr fun_ty $$ ppr orig_fun $$ ppr orig_args )
121 go (mk_val_app fun arg arg_ty res_ty) res_ty args
123 (arg_ty, res_ty) = splitFunTy fun_ty
125 -- | Construct an expression which represents the application of a number of
126 -- expressions to that of a data constructor expression. The leftmost expression
127 -- in the list is applied first
128 mkCoreConApps :: DataCon -> [CoreExpr] -> CoreExpr
129 mkCoreConApps con args = mkCoreApps (Var (dataConWorkId con)) args
132 mk_val_app :: CoreExpr -> CoreExpr -> Type -> Type -> CoreExpr
133 mk_val_app fun arg arg_ty _ -- See Note [CoreSyn let/app invariant]
134 | not (needsCaseBinding arg_ty arg)
135 = App fun arg -- The vastly common case
137 mk_val_app fun arg arg_ty res_ty
138 = Case arg arg_id res_ty [(DEFAULT,[],App fun (Var arg_id))]
140 arg_id = mkWildValBinder arg_ty
141 -- Lots of shadowing, but it doesn't matter,
142 -- because 'fun ' should not have a free wild-id
144 -- This is Dangerous. But this is the only place we play this
145 -- game, mk_val_app returns an expression that does not have
146 -- have a free wild-id. So the only thing that can go wrong
147 -- is if you take apart this case expression, and pass a
148 -- fragmet of it as the fun part of a 'mk_val_app'.
150 mkWildEvBinder :: PredType -> EvVar
151 mkWildEvBinder pred@(EqPred {}) = mkWildCoVar (mkPredTy pred)
152 mkWildEvBinder pred = mkWildValBinder (mkPredTy pred)
154 -- | Make a /wildcard binder/. This is typically used when you need a binder
155 -- that you expect to use only at a *binding* site. Do not use it at
156 -- occurrence sites because it has a single, fixed unique, and it's very
157 -- easy to get into difficulties with shadowing. That's why it is used so little.
158 -- See Note [WildCard binders] in SimplEnv
159 mkWildValBinder :: Type -> Id
160 mkWildValBinder ty = mkLocalId wildCardName ty
162 mkWildCase :: CoreExpr -> Type -> Type -> [CoreAlt] -> CoreExpr
163 -- Make a case expression whose case binder is unused
164 -- The alts should not have any occurrences of WildId
165 mkWildCase scrut scrut_ty res_ty alts
166 = Case scrut (mkWildValBinder scrut_ty) res_ty alts
168 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
169 mkIfThenElse guard then_expr else_expr
170 -- Not going to be refining, so okay to take the type of the "then" clause
171 = mkWildCase guard boolTy (exprType then_expr)
172 [ (DataAlt falseDataCon, [], else_expr), -- Increasing order of tag!
173 (DataAlt trueDataCon, [], then_expr) ]
176 The functions from this point don't really do anything cleverer than
177 their counterparts in CoreSyn, but they are here for consistency
180 -- | Create a lambda where the given expression has a number of variables
181 -- bound over it. The leftmost binder is that bound by the outermost
182 -- lambda in the result
183 mkCoreLams :: [CoreBndr] -> CoreExpr -> CoreExpr
187 %************************************************************************
189 \subsection{Making literals}
191 %************************************************************************
194 -- | Create a 'CoreExpr' which will evaluate to the given @Int@
195 mkIntExpr :: Integer -> CoreExpr -- Result = I# i :: Int
196 mkIntExpr i = mkConApp intDataCon [mkIntLit i]
198 -- | Create a 'CoreExpr' which will evaluate to the given @Int@
199 mkIntExprInt :: Int -> CoreExpr -- Result = I# i :: Int
200 mkIntExprInt i = mkConApp intDataCon [mkIntLitInt i]
202 -- | Create a 'CoreExpr' which will evaluate to the a @Word@ with the given value
203 mkWordExpr :: Integer -> CoreExpr
204 mkWordExpr w = mkConApp wordDataCon [mkWordLit w]
206 -- | Create a 'CoreExpr' which will evaluate to the given @Word@
207 mkWordExprWord :: Word -> CoreExpr
208 mkWordExprWord w = mkConApp wordDataCon [mkWordLitWord w]
210 -- | Create a 'CoreExpr' which will evaluate to the given @Integer@
211 mkIntegerExpr :: MonadThings m => Integer -> m CoreExpr -- Result :: Integer
213 | inIntRange i -- Small enough, so start from an Int
214 = do integer_id <- lookupId smallIntegerName
215 return (mkSmallIntegerLit integer_id i)
217 -- Special case for integral literals with a large magnitude:
218 -- They are transformed into an expression involving only smaller
219 -- integral literals. This improves constant folding.
221 | otherwise = do -- Big, so start from a string
222 plus_id <- lookupId plusIntegerName
223 times_id <- lookupId timesIntegerName
224 integer_id <- lookupId smallIntegerName
226 lit i = mkSmallIntegerLit integer_id i
227 plus a b = Var plus_id `App` a `App` b
228 times a b = Var times_id `App` a `App` b
230 -- Transform i into (x1 + (x2 + (x3 + (...) * b) * b) * b) with abs xi <= b
231 horner :: Integer -> Integer -> CoreExpr
232 horner b i | abs q <= 1 = if r == 0 || r == i
234 else lit r `plus` lit (i-r)
235 | r == 0 = horner b q `times` lit b
236 | otherwise = lit r `plus` (horner b q `times` lit b)
238 (q,r) = i `quotRem` b
240 return (horner tARGET_MAX_INT i)
242 mkSmallIntegerLit :: Id -> Integer -> CoreExpr
243 mkSmallIntegerLit small_integer i = mkApps (Var small_integer) [mkIntLit i]
246 -- | Create a 'CoreExpr' which will evaluate to the given @Float@
247 mkFloatExpr :: Float -> CoreExpr
248 mkFloatExpr f = mkConApp floatDataCon [mkFloatLitFloat f]
250 -- | Create a 'CoreExpr' which will evaluate to the given @Double@
251 mkDoubleExpr :: Double -> CoreExpr
252 mkDoubleExpr d = mkConApp doubleDataCon [mkDoubleLitDouble d]
255 -- | Create a 'CoreExpr' which will evaluate to the given @Char@
256 mkCharExpr :: Char -> CoreExpr -- Result = C# c :: Int
257 mkCharExpr c = mkConApp charDataCon [mkCharLit c]
259 -- | Create a 'CoreExpr' which will evaluate to the given @String@
260 mkStringExpr :: MonadThings m => String -> m CoreExpr -- Result :: String
261 -- | Create a 'CoreExpr' which will evaluate to a string morally equivalent to the given @FastString@
262 mkStringExprFS :: MonadThings m => FastString -> m CoreExpr -- Result :: String
264 mkStringExpr str = mkStringExprFS (mkFastString str)
268 = return (mkNilExpr charTy)
271 = do let the_char = mkCharExpr (headFS str)
272 return (mkConsExpr charTy the_char (mkNilExpr charTy))
275 = do unpack_id <- lookupId unpackCStringName
276 return (App (Var unpack_id) (Lit (MachStr str)))
279 = do unpack_id <- lookupId unpackCStringUtf8Name
280 return (App (Var unpack_id) (Lit (MachStr str)))
284 safeChar c = ord c >= 1 && ord c <= 0x7F
287 %************************************************************************
289 \subsection{Tuple constructors}
291 %************************************************************************
298 -- GHCs built in tuples can only go up to 'mAX_TUPLE_SIZE' in arity, but
299 -- we might concievably want to build such a massive tuple as part of the
300 -- output of a desugaring stage (notably that for list comprehensions).
302 -- We call tuples above this size \"big tuples\", and emulate them by
303 -- creating and pattern matching on >nested< tuples that are expressible
306 -- Nesting policy: it's better to have a 2-tuple of 10-tuples (3 objects)
307 -- than a 10-tuple of 2-tuples (11 objects), so we want the leaves of any
308 -- construction to be big.
310 -- If you just use the 'mkBigCoreTup', 'mkBigCoreVarTupTy', 'mkTupleSelector'
311 -- and 'mkTupleCase' functions to do all your work with tuples you should be
312 -- fine, and not have to worry about the arity limitation at all.
314 -- | Lifts a \"small\" constructor into a \"big\" constructor by recursive decompositon
315 mkChunkified :: ([a] -> a) -- ^ \"Small\" constructor function, of maximum input arity 'mAX_TUPLE_SIZE'
316 -> [a] -- ^ Possible \"big\" list of things to construct from
317 -> a -- ^ Constructed thing made possible by recursive decomposition
318 mkChunkified small_tuple as = mk_big_tuple (chunkify as)
320 -- Each sub-list is short enough to fit in a tuple
321 mk_big_tuple [as] = small_tuple as
322 mk_big_tuple as_s = mk_big_tuple (chunkify (map small_tuple as_s))
324 chunkify :: [a] -> [[a]]
325 -- ^ Split a list into lists that are small enough to have a corresponding
326 -- tuple arity. The sub-lists of the result all have length <= 'mAX_TUPLE_SIZE'
327 -- But there may be more than 'mAX_TUPLE_SIZE' sub-lists
329 | n_xs <= mAX_TUPLE_SIZE = [xs]
330 | otherwise = split xs
334 split xs = take mAX_TUPLE_SIZE xs : split (drop mAX_TUPLE_SIZE xs)
338 Creating tuples and their types for Core expressions
340 @mkBigCoreVarTup@ builds a tuple; the inverse to @mkTupleSelector@.
342 * If it has only one element, it is the identity function.
344 * If there are more elements than a big tuple can have, it nests
349 -- | Build a small tuple holding the specified variables
350 mkCoreVarTup :: [Id] -> CoreExpr
351 mkCoreVarTup ids = mkCoreTup (map Var ids)
353 -- | Bulid the type of a small tuple that holds the specified variables
354 mkCoreVarTupTy :: [Id] -> Type
355 mkCoreVarTupTy ids = mkBoxedTupleTy (map idType ids)
357 -- | Build a small tuple holding the specified expressions
358 mkCoreTup :: [CoreExpr] -> CoreExpr
359 mkCoreTup [] = Var unitDataConId
361 mkCoreTup cs = mkConApp (tupleCon Boxed (length cs))
362 (map (Type . exprType) cs ++ cs)
364 -- | Build a big tuple holding the specified variables
365 mkBigCoreVarTup :: [Id] -> CoreExpr
366 mkBigCoreVarTup ids = mkBigCoreTup (map Var ids)
368 -- | Build the type of a big tuple that holds the specified variables
369 mkBigCoreVarTupTy :: [Id] -> Type
370 mkBigCoreVarTupTy ids = mkBigCoreTupTy (map idType ids)
372 -- | Build a big tuple holding the specified expressions
373 mkBigCoreTup :: [CoreExpr] -> CoreExpr
374 mkBigCoreTup = mkChunkified mkCoreTup
376 -- | Build the type of a big tuple that holds the specified type of thing
377 mkBigCoreTupTy :: [Type] -> Type
378 mkBigCoreTupTy = mkChunkified mkBoxedTupleTy
381 %************************************************************************
383 \subsection{Tuple destructors}
385 %************************************************************************
388 -- | Builds a selector which scrutises the given
389 -- expression and extracts the one name from the list given.
390 -- If you want the no-shadowing rule to apply, the caller
391 -- is responsible for making sure that none of these names
394 -- If there is just one 'Id' in the tuple, then the selector is
395 -- just the identity.
397 -- If necessary, we pattern match on a \"big\" tuple.
398 mkTupleSelector :: [Id] -- ^ The 'Id's to pattern match the tuple against
399 -> Id -- ^ The 'Id' to select
400 -> Id -- ^ A variable of the same type as the scrutinee
401 -> CoreExpr -- ^ Scrutinee
402 -> CoreExpr -- ^ Selector expression
404 -- mkTupleSelector [a,b,c,d] b v e
406 -- (p,q) -> case p of p {
408 -- We use 'tpl' vars for the p,q, since shadowing does not matter.
410 -- In fact, it's more convenient to generate it innermost first, getting
415 mkTupleSelector vars the_var scrut_var scrut
416 = mk_tup_sel (chunkify vars) the_var
418 mk_tup_sel [vars] the_var = mkSmallTupleSelector vars the_var scrut_var scrut
419 mk_tup_sel vars_s the_var = mkSmallTupleSelector group the_var tpl_v $
420 mk_tup_sel (chunkify tpl_vs) tpl_v
422 tpl_tys = [mkBoxedTupleTy (map idType gp) | gp <- vars_s]
423 tpl_vs = mkTemplateLocals tpl_tys
424 [(tpl_v, group)] = [(tpl,gp) | (tpl,gp) <- zipEqual "mkTupleSelector" tpl_vs vars_s,
429 -- | Like 'mkTupleSelector' but for tuples that are guaranteed
430 -- never to be \"big\".
432 -- > mkSmallTupleSelector [x] x v e = [| e |]
433 -- > mkSmallTupleSelector [x,y,z] x v e = [| case e of v { (x,y,z) -> x } |]
434 mkSmallTupleSelector :: [Id] -- The tuple args
435 -> Id -- The selected one
436 -> Id -- A variable of the same type as the scrutinee
437 -> CoreExpr -- Scrutinee
439 mkSmallTupleSelector [var] should_be_the_same_var _ scrut
440 = ASSERT(var == should_be_the_same_var)
442 mkSmallTupleSelector vars the_var scrut_var scrut
443 = ASSERT( notNull vars )
444 Case scrut scrut_var (idType the_var)
445 [(DataAlt (tupleCon Boxed (length vars)), vars, Var the_var)]
449 -- | A generalization of 'mkTupleSelector', allowing the body
450 -- of the case to be an arbitrary expression.
452 -- To avoid shadowing, we use uniques to invent new variables.
454 -- If necessary we pattern match on a \"big\" tuple.
455 mkTupleCase :: UniqSupply -- ^ For inventing names of intermediate variables
456 -> [Id] -- ^ The tuple identifiers to pattern match on
457 -> CoreExpr -- ^ Body of the case
458 -> Id -- ^ A variable of the same type as the scrutinee
459 -> CoreExpr -- ^ Scrutinee
461 -- ToDo: eliminate cases where none of the variables are needed.
463 -- mkTupleCase uniqs [a,b,c,d] body v e
464 -- = case e of v { (p,q) ->
465 -- case p of p { (a,b) ->
466 -- case q of q { (c,d) ->
468 mkTupleCase uniqs vars body scrut_var scrut
469 = mk_tuple_case uniqs (chunkify vars) body
471 -- This is the case where don't need any nesting
472 mk_tuple_case _ [vars] body
473 = mkSmallTupleCase vars body scrut_var scrut
475 -- This is the case where we must make nest tuples at least once
476 mk_tuple_case us vars_s body
477 = let (us', vars', body') = foldr one_tuple_case (us, [], body) vars_s
478 in mk_tuple_case us' (chunkify vars') body'
480 one_tuple_case chunk_vars (us, vs, body)
481 = let (uniq, us') = takeUniqFromSupply us
482 scrut_var = mkSysLocal (fsLit "ds") uniq
483 (mkBoxedTupleTy (map idType chunk_vars))
484 body' = mkSmallTupleCase chunk_vars body scrut_var (Var scrut_var)
485 in (us', scrut_var:vs, body')
489 -- | As 'mkTupleCase', but for a tuple that is small enough to be guaranteed
490 -- not to need nesting.
492 :: [Id] -- ^ The tuple args
493 -> CoreExpr -- ^ Body of the case
494 -> Id -- ^ A variable of the same type as the scrutinee
495 -> CoreExpr -- ^ Scrutinee
498 mkSmallTupleCase [var] body _scrut_var scrut
499 = bindNonRec var scrut body
500 mkSmallTupleCase vars body scrut_var scrut
501 -- One branch no refinement?
502 = Case scrut scrut_var (exprType body) [(DataAlt (tupleCon Boxed (length vars)), vars, body)]
505 %************************************************************************
507 \subsection{Common list manipulation expressions}
509 %************************************************************************
511 Call the constructor Ids when building explicit lists, so that they
512 interact well with rules.
515 -- | Makes a list @[]@ for lists of the specified type
516 mkNilExpr :: Type -> CoreExpr
517 mkNilExpr ty = mkConApp nilDataCon [Type ty]
519 -- | Makes a list @(:)@ for lists of the specified type
520 mkConsExpr :: Type -> CoreExpr -> CoreExpr -> CoreExpr
521 mkConsExpr ty hd tl = mkConApp consDataCon [Type ty, hd, tl]
523 -- | Make a list containing the given expressions, where the list has the given type
524 mkListExpr :: Type -> [CoreExpr] -> CoreExpr
525 mkListExpr ty xs = foldr (mkConsExpr ty) (mkNilExpr ty) xs
527 -- | Make a fully applied 'foldr' expression
528 mkFoldrExpr :: MonadThings m
529 => Type -- ^ Element type of the list
530 -> Type -- ^ Fold result type
531 -> CoreExpr -- ^ "Cons" function expression for the fold
532 -> CoreExpr -- ^ "Nil" expression for the fold
533 -> CoreExpr -- ^ List expression being folded acress
535 mkFoldrExpr elt_ty result_ty c n list = do
536 foldr_id <- lookupId foldrName
537 return (Var foldr_id `App` Type elt_ty
543 -- | Make a 'build' expression applied to a locally-bound worker function
544 mkBuildExpr :: (MonadThings m, MonadUnique m)
545 => Type -- ^ Type of list elements to be built
546 -> ((Id, Type) -> (Id, Type) -> m CoreExpr) -- ^ Function that, given information about the 'Id's
547 -- of the binders for the build worker function, returns
548 -- the body of that worker
550 mkBuildExpr elt_ty mk_build_inside = do
551 [n_tyvar] <- newTyVars [alphaTyVar]
552 let n_ty = mkTyVarTy n_tyvar
553 c_ty = mkFunTys [elt_ty, n_ty] n_ty
554 [c, n] <- sequence [mkSysLocalM (fsLit "c") c_ty, mkSysLocalM (fsLit "n") n_ty]
556 build_inside <- mk_build_inside (c, c_ty) (n, n_ty)
558 build_id <- lookupId buildName
559 return $ Var build_id `App` Type elt_ty `App` mkLams [n_tyvar, c, n] build_inside
561 newTyVars tyvar_tmpls = do
563 return (zipWith setTyVarUnique tyvar_tmpls uniqs)
567 %************************************************************************
571 %************************************************************************
575 :: Id -- Should be of type (forall a. Addr# -> a)
576 -- where Addr# points to a UTF8 encoded string
577 -> Type -- The type to instantiate 'a'
578 -> String -- The string to print
581 mkRuntimeErrorApp err_id res_ty err_msg
582 = mkApps (Var err_id) [Type res_ty, err_string]
584 err_string = Lit (mkMachString err_msg)
586 mkImpossibleExpr :: Type -> CoreExpr
587 mkImpossibleExpr res_ty
588 = mkRuntimeErrorApp rUNTIME_ERROR_ID res_ty "Impossible case alternative"
591 %************************************************************************
595 %************************************************************************
597 GHC randomly injects these into the code.
599 @patError@ is just a version of @error@ for pattern-matching
600 failures. It knows various ``codes'' which expand to longer
601 strings---this saves space!
603 @absentErr@ is a thing we put in for ``absent'' arguments. They jolly
604 well shouldn't be yanked on, but if one is, then you will get a
605 friendly message from @absentErr@ (rather than a totally random
608 @parError@ is a special version of @error@ which the compiler does
609 not know to be a bottoming Id. It is used in the @_par_@ and @_seq_@
610 templates, but we don't ever expect to generate code for it.
615 = [ eRROR_ID, -- This one isn't used anywhere else in the compiler
616 -- But we still need it in wiredInIds so that when GHC
617 -- compiles a program that mentions 'error' we don't
618 -- import its type from the interface file; we just get
619 -- the Id defined here. Which has an 'open-tyvar' type.
622 iRREFUT_PAT_ERROR_ID,
623 nON_EXHAUSTIVE_GUARDS_ERROR_ID,
624 nO_METHOD_BINDING_ERROR_ID,
630 recSelErrorName, runtimeErrorName, absentErrorName :: Name
631 irrefutPatErrorName, recConErrorName, patErrorName :: Name
632 nonExhaustiveGuardsErrorName, noMethodBindingErrorName :: Name
634 recSelErrorName = err_nm "recSelError" recSelErrorIdKey rEC_SEL_ERROR_ID
635 absentErrorName = err_nm "absentError" absentErrorIdKey aBSENT_ERROR_ID
636 runtimeErrorName = err_nm "runtimeError" runtimeErrorIdKey rUNTIME_ERROR_ID
637 irrefutPatErrorName = err_nm "irrefutPatError" irrefutPatErrorIdKey iRREFUT_PAT_ERROR_ID
638 recConErrorName = err_nm "recConError" recConErrorIdKey rEC_CON_ERROR_ID
639 patErrorName = err_nm "patError" patErrorIdKey pAT_ERROR_ID
641 noMethodBindingErrorName = err_nm "noMethodBindingError"
642 noMethodBindingErrorIdKey nO_METHOD_BINDING_ERROR_ID
643 nonExhaustiveGuardsErrorName = err_nm "nonExhaustiveGuardsError"
644 nonExhaustiveGuardsErrorIdKey nON_EXHAUSTIVE_GUARDS_ERROR_ID
646 err_nm :: String -> Unique -> Id -> Name
647 err_nm str uniq id = mkWiredInIdName cONTROL_EXCEPTION_BASE (fsLit str) uniq id
649 rEC_SEL_ERROR_ID, rUNTIME_ERROR_ID, iRREFUT_PAT_ERROR_ID, rEC_CON_ERROR_ID :: Id
650 pAT_ERROR_ID, nO_METHOD_BINDING_ERROR_ID, nON_EXHAUSTIVE_GUARDS_ERROR_ID :: Id
651 aBSENT_ERROR_ID :: Id
652 rEC_SEL_ERROR_ID = mkRuntimeErrorId recSelErrorName
653 rUNTIME_ERROR_ID = mkRuntimeErrorId runtimeErrorName
654 iRREFUT_PAT_ERROR_ID = mkRuntimeErrorId irrefutPatErrorName
655 rEC_CON_ERROR_ID = mkRuntimeErrorId recConErrorName
656 pAT_ERROR_ID = mkRuntimeErrorId patErrorName
657 nO_METHOD_BINDING_ERROR_ID = mkRuntimeErrorId noMethodBindingErrorName
658 nON_EXHAUSTIVE_GUARDS_ERROR_ID = mkRuntimeErrorId nonExhaustiveGuardsErrorName
659 aBSENT_ERROR_ID = mkRuntimeErrorId absentErrorName
661 mkRuntimeErrorId :: Name -> Id
662 mkRuntimeErrorId name = pc_bottoming_Id name runtimeErrorTy
664 runtimeErrorTy :: Type
665 -- The runtime error Ids take a UTF8-encoded string as argument
666 runtimeErrorTy = mkSigmaTy [openAlphaTyVar] [] (mkFunTy addrPrimTy openAlphaTy)
671 errorName = mkWiredInIdName gHC_ERR (fsLit "error") errorIdKey eRROR_ID
674 eRROR_ID = pc_bottoming_Id errorName errorTy
677 errorTy = mkSigmaTy [openAlphaTyVar] [] (mkFunTys [mkListTy charTy] openAlphaTy)
678 -- Notice the openAlphaTyVar. It says that "error" can be applied
679 -- to unboxed as well as boxed types. This is OK because it never
680 -- returns, so the return type is irrelevant.
684 %************************************************************************
686 \subsection{Utilities}
688 %************************************************************************
691 pc_bottoming_Id :: Name -> Type -> Id
692 -- Function of arity 1, which diverges after being given one argument
693 pc_bottoming_Id name ty
694 = mkVanillaGlobalWithInfo name ty bottoming_info
696 bottoming_info = vanillaIdInfo `setStrictnessInfo` Just strict_sig
698 -- Make arity and strictness agree
700 -- Do *not* mark them as NoCafRefs, because they can indeed have
701 -- CAF refs. For example, pAT_ERROR_ID calls GHC.Err.untangle,
702 -- which has some CAFs
703 -- In due course we may arrange that these error-y things are
704 -- regarded by the GC as permanently live, in which case we
705 -- can give them NoCaf info. As it is, any function that calls
706 -- any pc_bottoming_Id will itself have CafRefs, which bloats
709 strict_sig = mkStrictSig (mkTopDmdType [evalDmd] BotRes)
710 -- These "bottom" out, no matter what their arguments