2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Id]{@Ids@: Value and constructor identifiers}
11 IdDetails(..), -- Exposed only to MkId
14 DataCon, DictFun, DictVar,
16 -- Construction and modification
17 mkId, mkIdWithNewUniq, mkIdWithNewName, mkIdWithNewType,
19 setIdVisibility, mkIdVisible,
22 -- DESTRUCTION (excluding pragmatic info)
28 -- Extracting pieces of particular sorts of Ids
39 recordSelectorFieldLabel,
46 idWantsToBeINLINEd, getInlinePragma,
47 idMustBeINLINEd, idMustNotBeINLINEd,
50 isDataCon, isAlgCon, isNewCon, isTupleCon,
53 isRecordSelector, isSpecPragmaId,
56 -- PRINTING and RENUMBERING
60 -- UNFOLDING, ARITY, UPDATE, AND STRICTNESS STUFF (etc)
73 addInlinePragma, nukeNoInlinePragma, addNoInlinePragma,
78 IdEnv, GenIdSet, IdSet,
92 lookupIdEnv, lookupIdSubst,
108 #include "HsVersions.h"
110 import {-# SOURCE #-} CoreUnfold ( Unfolding )
112 import CmdLineOpts ( opt_PprStyle_All )
115 import Name ( nameUnique, isLocalName, mkSysLocalName,
116 isWiredInName, setNameVisibility, mkNameVisible,
118 ExportFlag(..), Provenance,
119 OccName(..), Name, Module,
122 import PrimOp ( PrimOp )
123 import PrelMods ( pREL_TUP, pREL_BASE )
124 import FieldLabel ( fieldLabelName, FieldLabel(..) )
125 import SrcLoc ( mkBuiltinSrcLoc )
126 import TysWiredIn ( tupleTyCon )
127 import TyCon ( TyCon, isDataTyCon, isNewTyCon )
128 import Type ( mkSigmaTy, mkTyVarTys, mkFunTys,
129 mkTyConApp, instantiateTy, mkForAllTys,
130 tyVarsOfType, instantiateTy, typePrimRep,
132 ThetaType, TauType, Type, GenType
134 import TyVar ( TyVar, alphaTyVars, isEmptyTyVarSet,
135 TyVarEnv, zipTyVarEnv, mkTyVarEnv
138 import UniqSet -- practically all of it
139 import Unique ( Unique, Uniquable(..), getBuiltinUniques )
141 import SrcLoc ( SrcLoc )
142 import Util ( nOfThem, assoc )
143 import GlaExts ( Int# )
146 Here are the @Id@ and @IdDetails@ datatypes; also see the notes that
149 Every @Id@ has a @Unique@, to uniquify it and for fast comparison, a
150 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
151 strictness). The essential info about different kinds of @Ids@ is
154 ToDo: possibly cache other stuff in the single-constructor @Id@ type.
158 idUnique :: Unique, -- Key for fast comparison
160 idType :: ty, -- Id's type; used all the time;
161 idDetails :: IdDetails, -- Stuff about individual kinds of Ids.
162 idInfo :: IdInfo -- Properties of this Id deduced by compiler
167 data StrictnessMark = MarkedStrict | NotMarkedStrict
171 ---------------- Local values
173 = VanillaId Bool -- Ordinary Id
174 -- True <=> no free type vars
176 | PrimitiveId PrimOp -- The Id for a primitive operation
179 ---------------- Data constructors
181 | AlgConId -- Used for both data and newtype constructors.
182 -- You can tell the difference by looking at the TyCon
184 [StrictnessMark] -- Strict args; length = arity
185 [FieldLabel] -- Field labels for this constructor;
186 --length = 0 (not a record) or arity
188 [TyVar] ThetaType -- Type vars and context for the data type decl
189 [TyVar] ThetaType -- Ditto for the context of the constructor,
190 -- the existentially quantified stuff
191 [Type] TyCon -- Args and result tycon
193 -- forall tyvars1 ++ tyvars2. theta1 ++ theta2 =>
194 -- unitype_1 -> ... -> unitype_n -> tycon tyvars
196 | TupleConId Int -- Its arity
198 | RecordSelId FieldLabel
200 | SpecPragmaId -- This guy exists only to make Ids that are
201 -- on the *LHS* of bindings created by SPECIALISE
202 -- pragmas; eg: s = f Int d
203 -- The SpecPragmaId is never itself mentioned; it
204 -- exists solely so that the specialiser will find
205 -- the call to f, and make specialised version of it.
206 -- The SpecPragmaId binding is discarded by the specialiser
207 -- when it gathers up overloaded calls.
208 -- Meanwhile, it is not discarded as dead code.
219 %************************************************************************
221 \subsection{Construction}
223 %************************************************************************
226 mkId :: Name -> ty -> IdDetails -> IdInfo -> GenId ty
227 mkId name ty details info
228 = Id {idName = name, idUnique = nameUnique name, idType = ty,
229 idDetails = details, idInfo = info}
231 mkVanillaId :: Name -> (GenType flexi) -> IdInfo -> GenId (GenType flexi)
232 mkVanillaId name ty info
233 = Id {idName = name, idUnique = nameUnique name, idType = ty,
234 idDetails = VanillaId (isEmptyTyVarSet (tyVarsOfType ty)),
237 mkIdWithNewUniq :: Id -> Unique -> Id
238 mkIdWithNewUniq id uniq = id {idUnique = uniq, idName = changeUnique (idName id) uniq}
240 mkIdWithNewName :: Id -> Name -> Id
241 mkIdWithNewName id new_name
242 = id {idUnique = uniqueOf new_name, idName = new_name}
244 mkIdWithNewType :: GenId ty1 -> ty2 -> GenId ty2
245 mkIdWithNewType id ty = id {idType = ty}
249 Make some local @Ids@ for a template @CoreExpr@. These have bogus
250 @Uniques@, but that's OK because the templates are supposed to be
251 instantiated before use.
254 mkTemplateLocals :: [Type] -> [Id]
256 = zipWith3 mk (getBuiltinUniques (length tys)) tys [1..]
258 mk uniq ty n = mkVanillaId (mkSysLocalName uniq (_PK_ ("x"++show n)) mkBuiltinSrcLoc)
264 -- See notes with setNameVisibility (Name.lhs)
265 setIdVisibility :: Maybe Module -> Unique -> Id -> Id
266 setIdVisibility maybe_mod u id
267 = id {idName = setNameVisibility maybe_mod u (idName id)}
269 mkIdVisible :: Module -> Unique -> Id -> Id
270 mkIdVisible mod u id = id {idName = mkNameVisible mod u (idName id)}
272 replaceIdInfo :: GenId ty -> IdInfo -> GenId ty
273 replaceIdInfo id info = id {idInfo = info}
276 %************************************************************************
278 \subsection[Id-general-funs]{General @Id@-related functions}
280 %************************************************************************
284 fIRST_TAG = 1 -- Tags allocated from here for real constructors
286 -- isDataCon returns False for @newtype@ constructors
287 isDataCon (Id {idDetails = AlgConId _ _ _ _ _ _ _ _ tc}) = isDataTyCon tc
288 isDataCon (Id {idDetails = TupleConId _}) = True
289 isDataCon other = False
291 isNewCon (Id {idDetails = AlgConId _ _ _ _ _ _ _ _ tc}) = isNewTyCon tc
292 isNewCon other = False
294 -- isAlgCon returns True for @data@ or @newtype@ constructors
295 isAlgCon (Id {idDetails = AlgConId _ _ _ _ _ _ _ _ _}) = True
296 isAlgCon (Id {idDetails = TupleConId _}) = True
297 isAlgCon other = False
299 isTupleCon (Id {idDetails = TupleConId _}) = True
300 isTupleCon other = False
304 idHasNoFreeTyVars :: Id -> Bool
306 idHasNoFreeTyVars (Id {idDetails = details})
309 chk (AlgConId _ _ _ _ _ _ _ _ _) = True
310 chk (TupleConId _) = True
311 chk (RecordSelId _) = True
312 chk (VanillaId no_free_tvs) = no_free_tvs
313 chk (PrimitiveId _) = True
314 chk SpecPragmaId = False -- Play safe
316 -- omitIfaceSigForId tells whether an Id's info is implied by other declarations,
317 -- so we don't need to put its signature in an interface file, even if it's mentioned
318 -- in some other interface unfolding.
324 omitIfaceSigForId (Id {idName = name, idDetails = details})
330 (PrimitiveId _) -> True -- Ditto, for primitives
332 -- This group is Ids that are implied by their type or class decl;
333 -- remember that all type and class decls appear in the interface file.
334 -- The dfun id must *not* be omitted, because it carries version info for
336 (AlgConId _ _ _ _ _ _ _ _ _) -> True
337 (TupleConId _) -> True
338 (RecordSelId _) -> True
340 other -> False -- Don't omit!
341 -- NB DefaultMethodIds are not omitted
345 isBottomingId id = bottomIsGuaranteed (strictnessInfo (idInfo id))
347 isPrimitiveId_maybe (Id {idDetails = PrimitiveId primop}) = Just primop
348 isPrimitiveId_maybe other = Nothing
350 isSpecPragmaId (Id {idDetails = SpecPragmaId}) = True
351 isSpecPragmaId _ = False
354 @externallyVisibleId@: is it true that another module might be
355 able to ``see'' this Id in a code generation sense. That
356 is, another .o file might refer to this Id.
358 In tidyCorePgm (SimplCore.lhs) we carefully set each top level thing's
359 local-ness precisely so that the test here would be easy
362 externallyVisibleId :: Id -> Bool
363 externallyVisibleId id = not (isLocalName (idName id))
364 -- not local => global => externally visible
369 idPrimRep id = typePrimRep (idType id)
373 %************************************************************************
375 \subsection[Id-arities]{Arity-related functions}
377 %************************************************************************
379 For locally-defined Ids, the code generator maintains its own notion
380 of their arities; so it should not be asking... (but other things
381 besides the code-generator need arity info!)
384 getIdArity :: Id -> ArityInfo
385 getIdArity id = arityInfo (idInfo id)
387 addIdArity :: Id -> ArityInfo -> Id
388 addIdArity id@(Id {idInfo = info}) arity
389 = id {idInfo = arity `setArityInfo` info}
392 %************************************************************************
394 \subsection[constructor-funs]{@DataCon@-related functions (incl.~tuples)}
396 %************************************************************************
399 dataConNumFields gives the number of actual fields in the
400 {\em representation} of the data constructor. This may be more than appear
401 in the source code; the extra ones are the existentially quantified
406 = ASSERT( if (isDataCon id) then True else
407 pprTrace "dataConNumFields" (ppr id) False )
408 case (dataConSig id) of { (_, _, _, con_theta, arg_tys, _) ->
409 length con_theta + length arg_tys }
411 isNullaryDataCon con = dataConNumFields con == 0 -- function of convenience
417 dataConTag :: DataCon -> ConTag -- will panic if not a DataCon
418 dataConTag (Id {idDetails = AlgConId tag _ _ _ _ _ _ _ _}) = tag
419 dataConTag (Id {idDetails = TupleConId _}) = fIRST_TAG
421 dataConTyCon :: DataCon -> TyCon -- will panic if not a DataCon
422 dataConTyCon (Id {idDetails = AlgConId _ _ _ _ _ _ _ _ tycon}) = tycon
423 dataConTyCon (Id {idDetails = TupleConId a}) = tupleTyCon a
425 dataConSig :: DataCon -> ([TyVar], ThetaType, [TyVar], ThetaType, [TauType], TyCon)
426 -- will panic if not a DataCon
428 dataConSig (Id {idDetails = AlgConId _ _ _ tyvars theta con_tyvars con_theta arg_tys tycon})
429 = (tyvars, theta, con_tyvars, con_theta, arg_tys, tycon)
431 dataConSig (Id {idDetails = TupleConId arity})
432 = (tyvars, [], [], [], tyvar_tys, tupleTyCon arity)
434 tyvars = take arity alphaTyVars
435 tyvar_tys = mkTyVarTys tyvars
438 -- dataConRepType returns the type of the representation of a contructor
439 -- This may differ from the type of the contructor Id itself for two reasons:
440 -- a) the constructor Id may be overloaded, but the dictionary isn't stored
441 -- e.g. data Eq a => T a = MkT a a
443 -- b) the constructor may store an unboxed version of a strict field.
445 -- Here's an example illustrating both:
446 -- data Ord a => T a = MkT Int! a
448 -- T :: Ord a => Int -> a -> T a
449 -- but the rep type is
450 -- Trep :: Int# -> a -> T a
451 -- Actually, the unboxed part isn't implemented yet!
453 dataConRepType :: Id -> Type
454 dataConRepType (Id {idDetails = AlgConId _ _ _ tyvars theta con_tyvars con_theta arg_tys tycon})
455 = mkForAllTys (tyvars++con_tyvars)
456 (mkFunTys arg_tys (mkTyConApp tycon (mkTyVarTys tyvars)))
457 dataConRepType other_id
458 = ASSERT( isDataCon other_id )
461 dataConFieldLabels :: DataCon -> [FieldLabel]
462 dataConFieldLabels (Id {idDetails = AlgConId _ _ fields _ _ _ _ _ _}) = fields
463 dataConFieldLabels (Id {idDetails = TupleConId _}) = []
465 dataConFieldLabels x@(Id {idDetails = idt}) =
466 panic ("dataConFieldLabel: " ++
470 RecordSelId _ -> "r"))
473 dataConStrictMarks :: DataCon -> [StrictnessMark]
474 dataConStrictMarks (Id {idDetails = AlgConId _ stricts _ _ _ _ _ _ _}) = stricts
475 dataConStrictMarks (Id {idDetails = TupleConId arity}) = nOfThem arity NotMarkedStrict
477 dataConRawArgTys :: DataCon -> [TauType] -- a function of convenience
478 dataConRawArgTys con = case (dataConSig con) of { (_,_, _, _, arg_tys,_) -> arg_tys }
480 dataConArgTys :: DataCon
481 -> [Type] -- Instantiated at these types
482 -> [Type] -- Needs arguments of these types
483 dataConArgTys con_id inst_tys
484 = map (instantiateTy tenv) arg_tys
486 (tyvars, _, _, _, arg_tys, _) = dataConSig con_id
487 tenv = zipTyVarEnv tyvars inst_tys
491 recordSelectorFieldLabel :: Id -> FieldLabel
492 recordSelectorFieldLabel (Id {idDetails = RecordSelId lbl}) = lbl
494 isRecordSelector (Id {idDetails = RecordSelId lbl}) = True
495 isRecordSelector other = False
499 %************************************************************************
501 \subsection[unfolding-Ids]{Functions related to @Ids@' unfoldings}
503 %************************************************************************
506 getIdUnfolding :: Id -> Unfolding
508 getIdUnfolding id = unfoldingInfo (idInfo id)
510 addIdUnfolding :: Id -> Unfolding -> Id
511 addIdUnfolding id@(Id {idInfo = info}) unfolding
512 = id {idInfo = unfolding `setUnfoldingInfo` info}
515 The inline pragma tells us to be very keen to inline this Id, but it's still
516 OK not to if optimisation is switched off.
519 getInlinePragma :: Id -> InlinePragInfo
520 getInlinePragma id = inlinePragInfo (idInfo id)
522 idWantsToBeINLINEd :: Id -> Bool
524 idWantsToBeINLINEd id = case getInlinePragma id of
525 IWantToBeINLINEd -> True
526 IMustBeINLINEd -> True
529 idMustNotBeINLINEd id = case getInlinePragma id of
530 IDontWantToBeINLINEd -> True
531 IMustNotBeINLINEd -> True
534 idMustBeINLINEd id = case getInlinePragma id of
535 IMustBeINLINEd -> True
538 addInlinePragma :: Id -> Id
539 addInlinePragma id@(Id {idInfo = info})
540 = id {idInfo = setInlinePragInfo IWantToBeINLINEd info}
542 nukeNoInlinePragma :: Id -> Id
543 nukeNoInlinePragma id@(Id {idInfo = info})
544 = case inlinePragInfo info of
545 IMustNotBeINLINEd -> id {idInfo = setInlinePragInfo NoPragmaInfo info}
548 -- If the user has already marked this binding as NOINLINE, then don't
549 -- add the IMustNotBeINLINEd tag, since it will get nuked later whereas
550 -- IDontWantToBeINLINEd is permanent.
552 addNoInlinePragma :: Id -> Id
553 addNoInlinePragma id@(Id {idInfo = info})
554 = case inlinePragInfo info of
555 IDontWantToBeINLINEd -> id
556 other -> id {idInfo = IMustNotBeINLINEd `setInlinePragInfo` info}
558 mustInlineInfo = IMustBeINLINEd `setInlinePragInfo` noIdInfo
559 wantToInlineInfo = IWantToBeINLINEd `setInlinePragInfo` noIdInfo
564 %************************************************************************
566 \subsection[IdInfo-funs]{Functions related to @Ids@' @IdInfos@}
568 %************************************************************************
571 getIdDemandInfo :: Id -> DemandInfo
572 getIdDemandInfo id = demandInfo (idInfo id)
574 addIdDemandInfo :: Id -> DemandInfo -> Id
575 addIdDemandInfo id@(Id {idInfo = info}) demand_info
576 = id {idInfo = demand_info `setDemandInfo` info}
580 getIdUpdateInfo :: Id -> UpdateInfo
581 getIdUpdateInfo id = updateInfo (idInfo id)
583 addIdUpdateInfo :: Id -> UpdateInfo -> Id
584 addIdUpdateInfo id@(Id {idInfo = info}) upd_info
585 = id {idInfo = upd_info `setUpdateInfo` info}
589 getIdSpecialisation :: Id -> IdSpecEnv
590 getIdSpecialisation id = specInfo (idInfo id)
592 setIdSpecialisation :: Id -> IdSpecEnv -> Id
593 setIdSpecialisation id@(Id {idInfo = info}) spec_info
594 = id {idInfo = spec_info `setSpecInfo` info}
598 getIdStrictness :: Id -> StrictnessInfo
599 getIdStrictness id = strictnessInfo (idInfo id)
601 addIdStrictness :: Id -> StrictnessInfo -> Id
602 addIdStrictness id@(Id {idInfo = info}) strict_info
603 = id {idInfo = strict_info `setStrictnessInfo` info}
606 %************************************************************************
608 \subsection[Id-comparison]{Comparison functions for @Id@s}
610 %************************************************************************
612 Comparison: equality and ordering---this stuff gets {\em hammered}.
615 cmpId (Id {idUnique = u1}) (Id {idUnique = u2}) = compare u1 u2
619 instance Eq (GenId ty) where
620 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
621 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
623 instance Ord (GenId ty) where
624 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
625 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
626 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
627 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
628 compare a b = cmpId a b
631 %************************************************************************
633 \subsection[Id-other-instances]{Other instance declarations for @Id@s}
635 %************************************************************************
638 instance Outputable ty => Outputable (GenId ty) where
641 showId :: Id -> String
642 showId id = showSDoc (pprId id)
645 Default printing code (not used for interfaces):
647 pprId :: Outputable ty => GenId ty -> SDoc
649 pprId Id {idUnique = u, idName = n, idInfo = info}
650 = hcat [ppr n, pp_prags]
653 | opt_PprStyle_All && not (codeStyle sty)
654 = (case inlinePragInfo info of
655 IMustNotBeINLINEd -> text "{n}"
656 IWantToBeINLINEd -> text "{i}"
657 IMustBeINLINEd -> text "{I}"
666 instance Uniquable (GenId ty) where
669 instance NamedThing (GenId ty) where
673 Note: The code generator doesn't carry a @UniqueSupply@, so it uses
674 the @Uniques@ out of local @Ids@ given to it.
676 %************************************************************************
678 \subsection{@IdEnv@s and @IdSet@s}
680 %************************************************************************
683 type IdEnv elt = UniqFM elt
687 mkIdEnv :: [(GenId ty, a)] -> IdEnv a
688 unitIdEnv :: GenId ty -> a -> IdEnv a
689 addOneToIdEnv :: IdEnv a -> GenId ty -> a -> IdEnv a
690 growIdEnv :: IdEnv a -> IdEnv a -> IdEnv a
691 growIdEnvList :: IdEnv a -> [(GenId ty, a)] -> IdEnv a
693 delManyFromIdEnv :: IdEnv a -> [GenId ty] -> IdEnv a
694 delOneFromIdEnv :: IdEnv a -> GenId ty -> IdEnv a
695 combineIdEnvs :: (a -> a -> a) -> IdEnv a -> IdEnv a -> IdEnv a
696 mapIdEnv :: (a -> b) -> IdEnv a -> IdEnv b
697 modifyIdEnv :: (a -> a) -> IdEnv a -> GenId ty -> IdEnv a
698 rngIdEnv :: IdEnv a -> [a]
700 isNullIdEnv :: IdEnv a -> Bool
701 lookupIdEnv :: IdEnv a -> GenId ty -> Maybe a
702 lookupNoFailIdEnv :: IdEnv a -> GenId ty -> a
703 elemIdEnv :: Id -> IdEnv a -> Bool
708 addOneToIdEnv = addToUFM
709 combineIdEnvs = plusUFM_C
710 delManyFromIdEnv = delListFromUFM
711 delOneFromIdEnv = delFromUFM
713 lookupIdEnv = lookupUFM
719 isNullIdEnv = isNullUFM
721 growIdEnvList env pairs = plusUFM env (listToUFM pairs)
722 lookupNoFailIdEnv env id = case (lookupIdEnv env id) of { Just xx -> xx }
724 lookupIdSubst :: IdEnv Id -> Id -> Id
725 lookupIdSubst env id = case lookupIdEnv env id of
726 Just id' -> id' -- Return original if
727 Nothing -> id -- it isn't in subst
729 -- modifyIdEnv: Look up a thing in the IdEnv, then mash it with the
730 -- modify function, and put it back.
732 modifyIdEnv mangle_fn env key
733 = case (lookupIdEnv env key) of
735 Just xx -> addOneToIdEnv env key (mangle_fn xx)
737 modifyIdEnv_Directly mangle_fn env key
738 = case (lookupUFM_Directly env key) of
740 Just xx -> addToUFM_Directly env key (mangle_fn xx)
744 type GenIdSet ty = UniqSet (GenId ty)
745 type IdSet = UniqSet (GenId Type)
747 emptyIdSet :: GenIdSet ty
748 intersectIdSets :: GenIdSet ty -> GenIdSet ty -> GenIdSet ty
749 unionIdSets :: GenIdSet ty -> GenIdSet ty -> GenIdSet ty
750 unionManyIdSets :: [GenIdSet ty] -> GenIdSet ty
751 idSetToList :: GenIdSet ty -> [GenId ty]
752 unitIdSet :: GenId ty -> GenIdSet ty
753 addOneToIdSet :: GenIdSet ty -> GenId ty -> GenIdSet ty
754 elementOfIdSet :: GenId ty -> GenIdSet ty -> Bool
755 minusIdSet :: GenIdSet ty -> GenIdSet ty -> GenIdSet ty
756 isEmptyIdSet :: GenIdSet ty -> Bool
757 mkIdSet :: [GenId ty] -> GenIdSet ty
759 emptyIdSet = emptyUniqSet
760 unitIdSet = unitUniqSet
761 addOneToIdSet = addOneToUniqSet
762 intersectIdSets = intersectUniqSets
763 unionIdSets = unionUniqSets
764 unionManyIdSets = unionManyUniqSets
765 idSetToList = uniqSetToList
766 elementOfIdSet = elementOfUniqSet
767 minusIdSet = minusUniqSet
768 isEmptyIdSet = isEmptyUniqSet