2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Id]{@Ids@: Value and constructor identifiers}
9 GenId(..), -- *naughtily* used in some places (e.g., TcHsSyn)
13 DataCon, DictFun, DictVar,
19 mkIdWithNewUniq, mkIdWithNewName, mkIdWithNewType,
33 -- DESTRUCTION (excluding pragmatic info)
49 recordSelectorFieldLabel,
55 cmpId_withSpecDataCon,
58 idWantsToBeINLINEd, getInlinePragma,
59 idMustBeINLINEd, idMustNotBeINLINEd,
61 isDataCon, isAlgCon, isNewCon,
63 isDefaultMethodId_maybe,
70 isSuperDictSelId_maybe,
76 unfoldingUnfriendlyId,
82 -- PRINTING and RENUMBERING
91 -- UNFOLDING, ARITY, UPDATE, AND STRICTNESS STUFF (etc)
104 replaceIdInfo, replacePragmaInfo,
105 addInlinePragma, nukeNoInlinePragma, addNoInlinePragma,
108 IdEnv, GenIdSet, IdSet,
129 modifyIdEnv_Directly,
138 #include "HsVersions.h"
140 import {-# SOURCE #-} CoreUnfold ( Unfolding )
141 import {-# SOURCE #-} StdIdInfo ( addStandardIdInfo )
143 import CmdLineOpts ( opt_PprStyle_All )
144 import SpecEnv ( SpecEnv )
146 import Class ( Class )
147 import BasicTypes ( Arity )
149 import Maybes ( maybeToBool )
150 import Name ( nameUnique, mkLocalName, mkSysLocalName, isLocalName,
152 isLocallyDefinedName, occNameString, modAndOcc,
153 isLocallyDefined, changeUnique, isWiredInName,
154 nameString, getOccString, setNameVisibility,
155 isExported, ExportFlag(..), Provenance,
156 OccName(..), Name, Module,
159 import PrimOp ( PrimOp )
160 import PrelMods ( pREL_TUP, pREL_BASE )
161 import Lex ( mkTupNameStr )
162 import FieldLabel ( fieldLabelName, FieldLabel(..){-instances-} )
163 import PragmaInfo ( PragmaInfo(..) )
164 import SrcLoc ( mkBuiltinSrcLoc )
165 import TysWiredIn ( tupleTyCon )
166 import TyCon ( TyCon, tyConDataCons, isDataTyCon, isNewTyCon, mkSpecTyCon )
167 import Type ( mkSigmaTy, mkTyVarTys, mkFunTys, splitSigmaTy,
168 mkTyConApp, instantiateTy, mkForAllTys,
169 tyVarsOfType, instantiateTy, typePrimRep,
171 GenType, ThetaType, TauType, Type
173 import TyVar ( TyVar, alphaTyVars, isEmptyTyVarSet,
174 TyVarEnv, zipTyVarEnv, mkTyVarEnv
177 import UniqSet -- practically all of it
178 import Unique ( getBuiltinUniques, pprUnique, Unique, Uniquable(..) )
180 import SrcLoc ( SrcLoc )
181 import Util ( mapAccumL, nOfThem, zipEqual, assoc )
182 import GlaExts ( Int# )
185 Here are the @Id@ and @IdDetails@ datatypes; also see the notes that
188 Every @Id@ has a @Unique@, to uniquify it and for fast comparison, a
189 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
190 strictness). The essential info about different kinds of @Ids@ is
193 ToDo: possibly cache other stuff in the single-constructor @Id@ type.
197 Unique -- Key for fast comparison
199 ty -- Id's type; used all the time;
200 IdDetails -- Stuff about individual kinds of Ids.
201 PragmaInfo -- Properties of this Id requested by programmer
202 -- eg specialise-me, inline-me
203 IdInfo -- Properties of this Id deduced by compiler
207 data StrictnessMark = MarkedStrict | NotMarkedStrict
211 ---------------- Local values
213 = LocalId Bool -- Local name; mentioned by the user
214 -- True <=> no free type vars
216 | SysLocalId Bool -- Local name; made up by the compiler
219 | PrimitiveId PrimOp -- The Id for a primitive operation
221 | SpecPragmaId -- Local name; introduced by the compiler
222 (Maybe Id) -- for explicit specid in pragma
223 Bool -- as for LocalId
225 ---------------- Global values
227 | ImportedId -- Global name (Imported or Implicit); Id imported from an interface
229 ---------------- Data constructors
231 | AlgConId -- Used for both data and newtype constructors.
232 -- You can tell the difference by looking at the TyCon
234 [StrictnessMark] -- Strict args; length = arity
235 [FieldLabel] -- Field labels for this constructor;
236 --length = 0 (not a record) or arity
238 [TyVar] ThetaType -- Type vars and context for the data type decl
239 [TyVar] ThetaType -- Ditto for the context of the constructor,
240 -- the existentially quantified stuff
241 [Type] TyCon -- Args and result tycon
243 -- forall tyvars1 ++ tyvars2. theta1 ++ theta2 =>
244 -- unitype_1 -> ... -> unitype_n -> tycon tyvars
246 | TupleConId Int -- Its arity
248 | RecordSelId FieldLabel
250 ---------------- Things to do with overloading
252 | SuperDictSelId -- Selector for superclass dictionary
253 Class -- The class (input dict)
254 Class -- The superclass (result dict)
256 | MethodSelId Class -- An overloaded class operation, with
257 -- a fully polymorphic type. Its code
258 -- just selects a method from the
261 -- NB: The IdInfo for a MethodSelId has all the info about its
262 -- related "constant method Ids", which are just
263 -- specialisations of this general one.
265 | DefaultMethodId -- Default method for a particular class op
266 Class -- same class, <blah-blah> info as MethodSelId
269 | DictFunId Class -- A DictFun is uniquely identified
270 [Type] -- by its class and type; this type has free type vars,
271 -- whose identity is irrelevant. Eg Class = Eq
273 -- The "a" is irrelevant. As it is too painful to
274 -- actually do comparisons that way, we kindly supply
275 -- a Unique for that purpose.
277 | SpecId -- A specialisation of another Id
278 Id -- Id of which this is a specialisation
279 [Maybe Type] -- Types at which it is specialised;
280 -- A "Nothing" says this type ain't relevant.
281 Bool -- True <=> no free type vars; it's not enough
282 -- to know about the unspec version, because
283 -- we may specialise to a type w/ free tyvars
284 -- (i.e., in one of the "Maybe Type" dudes).
292 DictFunIds are generated from instance decls.
297 instance Foo a => Foo [a] where
300 generates the dict fun id decl
302 dfun.Foo.[*] = \d -> ...
304 The dfun id is uniquely named by the (class, type) pair. Notice, it
305 isn't a (class,tycon) pair any more, because we may get manually or
306 automatically generated specialisations of the instance decl:
308 instance Foo [Int] where
315 The type variables in the name are irrelevant; we print them as stars.
318 Constant method ids are generated from instance decls where
319 there is no context; that is, no dictionaries are needed to
320 construct the method. Example
322 instance Foo Int where
325 Then we get a constant method
330 It is possible, albeit unusual, to have a constant method
331 for an instance decl which has type vars:
333 instance Foo [a] where
337 We get the constant method
341 So a constant method is identified by a class/op/type triple.
342 The type variables in the type are irrelevant.
345 For Ids whose names must be known/deducible in other modules, we have
346 to conjure up their worker's names (and their worker's worker's
347 names... etc) in a known systematic way.
350 %************************************************************************
352 \subsection[Id-documentation]{Documentation}
354 %************************************************************************
358 The @Id@ datatype describes {\em values}. The basic things we want to
359 know: (1)~a value's {\em type} (@idType@ is a very common
360 operation in the compiler); and (2)~what ``flavour'' of value it might
361 be---for example, it can be terribly useful to know that a value is a
365 %----------------------------------------------------------------------
366 \item[@AlgConId@:] For the data constructors declared by a @data@
367 declaration. Their type is kept in {\em two} forms---as a regular
368 @Type@ (in the usual place), and also in its constituent pieces (in
369 the ``details''). We are frequently interested in those pieces.
371 %----------------------------------------------------------------------
372 \item[@TupleConId@:] This is just a special shorthand for @DataCons@ for
373 the infinite family of tuples.
375 %----------------------------------------------------------------------
376 \item[@ImportedId@:] These are values defined outside this module.
377 {\em Everything} we want to know about them must be stored here (or in
380 %----------------------------------------------------------------------
381 \item[@MethodSelId@:] A selector from a dictionary; it may select either
382 a method or a dictionary for one of the class's superclasses.
384 %----------------------------------------------------------------------
387 @mkDictFunId [a,b..] theta C T@ is the function derived from the
390 instance theta => C (T a b ..) where
393 It builds function @Id@ which maps dictionaries for theta,
394 to a dictionary for C (T a b ..).
396 *Note* that with the ``Mark Jones optimisation'', the theta may
397 include dictionaries for the immediate superclasses of C at the type
400 %----------------------------------------------------------------------
403 %----------------------------------------------------------------------
404 \item[@LocalId@:] A purely-local value, e.g., a function argument,
405 something defined in a @where@ clauses, ... --- but which appears in
406 the original program text.
408 %----------------------------------------------------------------------
409 \item[@SysLocalId@:] Same as a @LocalId@, except does {\em not} appear in
410 the original program text; these are introduced by the compiler in
413 %----------------------------------------------------------------------
414 \item[@SpecPragmaId@:] Introduced by the compiler to record
415 Specialisation pragmas. It is dead code which MUST NOT be removed
416 before specialisation.
421 %----------------------------------------------------------------------
424 @DataCons@ @TupleCons@, @Importeds@, @SuperDictSelIds@,
425 @MethodSelIds@, @DictFunIds@, and @DefaultMethodIds@ have the following
429 They have no free type variables, so if you are making a
430 type-variable substitution you don't need to look inside them.
432 They are constants, so they are not free variables. (When the STG
433 machine makes a closure, it puts all the free variables in the
434 closure; the above are not required.)
436 Note that @Locals@ and @SysLocals@ {\em may} have the above
437 properties, but they may not.
440 %************************************************************************
442 \subsection[Id-general-funs]{General @Id@-related functions}
444 %************************************************************************
447 -- isDataCon returns False for @newtype@ constructors
448 isDataCon (Id _ _ _ (AlgConId _ _ _ _ _ _ _ _ tc) _ _) = isDataTyCon tc
449 isDataCon (Id _ _ _ (TupleConId _) _ _) = True
450 isDataCon (Id _ _ _ (SpecId unspec _ _) _ _) = isDataCon unspec
451 isDataCon other = False
453 isNewCon (Id _ _ _ (AlgConId _ _ _ _ _ _ _ _ tc) _ _) = isNewTyCon tc
454 isNewCon other = False
456 -- isAlgCon returns True for @data@ or @newtype@ constructors
457 isAlgCon (Id _ _ _ (AlgConId _ _ _ _ _ _ _ _ _) _ _) = True
458 isAlgCon (Id _ _ _ (TupleConId _) _ _) = True
459 isAlgCon (Id _ _ _ (SpecId unspec _ _) _ _) = isAlgCon unspec
460 isAlgCon other = False
462 isTupleCon (Id _ _ _ (TupleConId _) _ _) = True
463 isTupleCon (Id _ _ _ (SpecId unspec _ _) _ _) = isTupleCon unspec
464 isTupleCon other = False
467 @toplevelishId@ tells whether an @Id@ {\em may} be defined in a nested
468 @let(rec)@ (returns @False@), or whether it is {\em sure} to be
469 defined at top level (returns @True@). This is used to decide whether
470 the @Id@ is a candidate free variable. NB: you are only {\em sure}
471 about something if it returns @True@!
474 toplevelishId :: Id -> Bool
475 idHasNoFreeTyVars :: Id -> Bool
477 toplevelishId (Id _ _ _ details _ _)
480 chk (AlgConId _ __ _ _ _ _ _ _) = True
481 chk (TupleConId _) = True
482 chk (RecordSelId _) = True
483 chk ImportedId = True
484 chk (SuperDictSelId _ _) = True
485 chk (MethodSelId _) = True
486 chk (DefaultMethodId _) = True
487 chk (DictFunId _ _) = True
488 chk (SpecId unspec _ _) = toplevelishId unspec
489 -- depends what the unspecialised thing is
490 chk (LocalId _) = False
491 chk (SysLocalId _) = False
492 chk (SpecPragmaId _ _) = False
493 chk (PrimitiveId _) = True
495 idHasNoFreeTyVars (Id _ _ _ details _ info)
498 chk (AlgConId _ _ _ _ _ _ _ _ _) = True
499 chk (TupleConId _) = True
500 chk (RecordSelId _) = True
501 chk ImportedId = True
502 chk (SuperDictSelId _ _) = True
503 chk (MethodSelId _) = True
504 chk (DefaultMethodId _) = True
505 chk (DictFunId _ _) = True
506 chk (SpecId _ _ no_free_tvs) = no_free_tvs
507 chk (LocalId no_free_tvs) = no_free_tvs
508 chk (SysLocalId no_free_tvs) = no_free_tvs
509 chk (SpecPragmaId _ no_free_tvs) = no_free_tvs
510 chk (PrimitiveId _) = True
512 -- omitIfaceSigForId tells whether an Id's info is implied by other declarations,
513 -- so we don't need to put its signature in an interface file, even if it's mentioned
514 -- in some other interface unfolding.
520 omitIfaceSigForId (Id _ name _ details _ _)
526 ImportedId -> True -- Never put imports in interface file
527 (PrimitiveId _) -> True -- Ditto, for primitives
529 -- This group is Ids that are implied by their type or class decl;
530 -- remember that all type and class decls appear in the interface file.
531 -- The dfun id must *not* be omitted, because it carries version info for
533 (AlgConId _ _ _ _ _ _ _ _ _) -> True
534 (TupleConId _) -> True
535 (RecordSelId _) -> True
536 (SuperDictSelId _ _) -> True
537 (MethodSelId _) -> True
539 other -> False -- Don't omit!
540 -- NB DefaultMethodIds are not omitted
544 isImportedId (Id _ _ _ ImportedId _ _) = True
545 isImportedId other = False
547 isBottomingId (Id _ _ _ _ _ info) = bottomIsGuaranteed (strictnessInfo info)
549 isSysLocalId (Id _ _ _ (SysLocalId _) _ _) = True
550 isSysLocalId other = False
552 isSpecPragmaId (Id _ _ _ (SpecPragmaId _ _) _ _) = True
553 isSpecPragmaId other = False
555 isSpecId_maybe (Id _ _ _ (SpecId unspec ty_maybes _) _ _)
556 = ASSERT(not (maybeToBool (isSpecId_maybe unspec)))
557 Just (unspec, ty_maybes)
558 isSpecId_maybe other_id
561 isMethodSelId_maybe (Id _ _ _ (MethodSelId cls) _ _) = Just cls
562 isMethodSelId_maybe _ = Nothing
564 isDefaultMethodId (Id _ _ _ (DefaultMethodId _) _ _) = True
565 isDefaultMethodId other = False
567 isDefaultMethodId_maybe (Id _ _ _ (DefaultMethodId cls) _ _)
569 isDefaultMethodId_maybe other = Nothing
571 isDictFunId (Id _ _ _ (DictFunId _ _) _ _) = True
572 isDictFunId other = False
574 isSuperDictSelId_maybe (Id _ _ _ (SuperDictSelId c sc) _ _) = Just (c, sc)
575 isSuperDictSelId_maybe other_id = Nothing
577 isWrapperId id = workerExists (getIdStrictness id)
579 isPrimitiveId_maybe (Id _ _ _ (PrimitiveId primop) _ _) = Just primop
580 isPrimitiveId_maybe other = Nothing
584 unfoldingUnfriendlyId -- return True iff it is definitely a bad
585 :: Id -- idea to export an unfolding that
586 -> Bool -- mentions this Id. Reason: it cannot
587 -- possibly be seen in another module.
589 unfoldingUnfriendlyId id = not (externallyVisibleId id)
592 @externallyVisibleId@: is it true that another module might be
593 able to ``see'' this Id in a code generation sense. That
594 is, another .o file might refer to this Id.
596 In tidyCorePgm (SimplCore.lhs) we carefully set each top level thing's
597 local-ness precisely so that the test here would be easy
600 externallyVisibleId :: Id -> Bool
601 externallyVisibleId id@(Id _ name _ _ _ _) = not (isLocalName name)
602 -- not local => global => externally visible
605 CLAIM (not ASSERTed) for @applyTypeEnvToId@ and @applySubstToId@:
606 `Top-levelish Ids'' cannot have any free type variables, so applying
607 the type-env cannot have any effect. (NB: checked in CoreLint?)
610 type TypeEnv = TyVarEnv Type
612 applyTypeEnvToId :: TypeEnv -> Id -> Id
613 applyTypeEnvToId type_env id@(Id _ _ ty _ _ _)
614 = apply_to_Id ( \ ty ->
615 instantiateTy type_env ty
620 apply_to_Id :: (Type -> Type) -> Id -> Id
622 apply_to_Id ty_fn id@(Id u n ty details prag info)
623 | idHasNoFreeTyVars id
626 = Id u n (ty_fn ty) (apply_to_details details) prag (apply_to_IdInfo ty_fn info)
628 apply_to_details (SpecId unspec ty_maybes no_ftvs)
630 new_unspec = apply_to_Id ty_fn unspec
631 new_maybes = map apply_to_maybe ty_maybes
633 SpecId new_unspec new_maybes (no_free_tvs ty)
634 -- ToDo: gratuitous recalc no_ftvs????
636 apply_to_maybe Nothing = Nothing
637 apply_to_maybe (Just ty) = Just (ty_fn ty)
639 apply_to_details other = other
643 %************************************************************************
645 \subsection[Id-type-funs]{Type-related @Id@ functions}
647 %************************************************************************
650 idName :: GenId ty -> Name
651 idName (Id _ n _ _ _ _) = n
653 idType :: GenId ty -> ty
654 idType (Id _ _ ty _ _ _) = ty
656 idPrimRep i = typePrimRep (idType i)
659 %************************************************************************
661 \subsection[Id-overloading]{Functions related to overloading}
663 %************************************************************************
666 mkSuperDictSelId u clas sc ty
667 = addStandardIdInfo $
668 Id u name ty details NoPragmaInfo noIdInfo
670 name = mkCompoundName name_fn u (getName clas)
671 details = SuperDictSelId clas sc
672 name_fn clas_str = SLIT("scsel_") _APPEND_ clas_str _APPEND_ mod _APPEND_ occNameString occ
673 (mod,occ) = modAndOcc sc
675 -- For method selectors the clean thing to do is
676 -- to give the method selector the same name as the class op itself.
677 mkMethodSelId op_name rec_c ty
678 = addStandardIdInfo $
679 Id (uniqueOf op_name) op_name ty (MethodSelId rec_c) NoPragmaInfo noIdInfo
681 mkDefaultMethodId dm_name rec_c ty
682 = Id (uniqueOf dm_name) dm_name ty (DefaultMethodId rec_c) NoPragmaInfo noIdInfo
684 mkDictFunId dfun_name full_ty clas itys
685 = Id (nameUnique dfun_name) dfun_name full_ty details NoPragmaInfo noIdInfo
687 details = DictFunId clas itys
689 mkWorkerId u unwrkr ty info
690 = Id u name ty details NoPragmaInfo info
692 details = LocalId (no_free_tvs ty)
693 name = mkCompoundName name_fn u (getName unwrkr)
694 name_fn wkr_str = SLIT("$w") _APPEND_ wkr_str
697 %************************************************************************
699 \subsection[local-funs]{@LocalId@-related functions}
701 %************************************************************************
704 mkImported n ty info = Id (nameUnique n) n ty ImportedId NoPragmaInfo info
706 mkPrimitiveId n ty primop
707 = addStandardIdInfo $
708 Id (nameUnique n) n ty (PrimitiveId primop) IMustBeINLINEd noIdInfo
709 -- The pragma @IMustBeINLINEd@ says that this Id absolutely must be inlined.
710 -- It's only true for primitives, because we don't want to make a closure for each of them.
715 no_free_tvs ty = isEmptyTyVarSet (tyVarsOfType ty)
717 -- SysLocal: for an Id being created by the compiler out of thin air...
718 -- UserLocal: an Id with a name the user might recognize...
719 mkSysLocal :: FAST_STRING -> Unique -> GenType flexi -> SrcLoc -> GenId (GenType flexi)
720 mkUserLocal :: OccName -> Unique -> GenType flexi -> SrcLoc -> GenId (GenType flexi)
722 mkSysLocal str uniq ty loc
723 = Id uniq (mkSysLocalName uniq str loc) ty (SysLocalId (no_free_tvs ty)) NoPragmaInfo noIdInfo
725 mkUserLocal occ uniq ty loc
726 = Id uniq (mkLocalName uniq occ loc) ty (LocalId (no_free_tvs ty)) NoPragmaInfo noIdInfo
728 mkUserId :: Name -> GenType flexi -> PragmaInfo -> GenId (GenType flexi)
729 mkUserId name ty pragma_info
730 = Id (nameUnique name) name ty (LocalId (no_free_tvs ty)) pragma_info noIdInfo
734 -- See notes with setNameVisibility (Name.lhs)
735 setIdVisibility :: Maybe Module -> Unique -> Id -> Id
736 setIdVisibility maybe_mod u (Id uniq name ty details prag info)
737 = Id uniq (setNameVisibility maybe_mod u name) ty details prag info
739 mkIdWithNewUniq :: Id -> Unique -> Id
740 mkIdWithNewUniq (Id _ n ty details prag info) u
741 = Id u (changeUnique n u) ty details prag info
743 mkIdWithNewName :: Id -> Name -> Id
744 mkIdWithNewName (Id _ _ ty details prag info) new_name
745 = Id (uniqueOf new_name) new_name ty details prag info
747 mkIdWithNewType :: Id -> Type -> Id
748 mkIdWithNewType (Id u name _ details pragma info) ty
749 = Id u name ty details pragma info
752 -- Specialised version of constructor: only used in STG and code generation
753 -- Note: The specialsied Id has the same unique as the unspeced Id
755 mkSameSpecCon ty_maybes unspec@(Id u name ty details pragma info)
756 = ASSERT(isDataCon unspec)
757 ASSERT(not (maybeToBool (isSpecId_maybe unspec)))
758 Id u name new_ty (SpecId unspec ty_maybes (no_free_tvs new_ty)) pragma info
760 new_ty = specialiseTy ty ty_maybes 0
762 -- pprTrace "SameSpecCon:Unique:"
763 -- (ppSep (ppr unspec: [pprMaybeTy ty | ty <- ty_maybes]))
767 Make some local @Ids@ for a template @CoreExpr@. These have bogus
768 @Uniques@, but that's OK because the templates are supposed to be
769 instantiated before use.
771 mkTemplateLocals :: [Type] -> [Id]
773 = zipWith (\ u -> \ ty -> mkSysLocal SLIT("tpl") u ty mkBuiltinSrcLoc)
774 (getBuiltinUniques (length tys))
779 getIdInfo :: GenId ty -> IdInfo
780 getPragmaInfo :: GenId ty -> PragmaInfo
782 getIdInfo (Id _ _ _ _ _ info) = info
783 getPragmaInfo (Id _ _ _ _ info _) = info
785 replaceIdInfo :: Id -> IdInfo -> Id
786 replaceIdInfo (Id u n ty details pinfo _) info = Id u n ty details pinfo info
788 replacePragmaInfo :: GenId ty -> PragmaInfo -> GenId ty
789 replacePragmaInfo (Id u sn ty details _ info) prag = Id u sn ty details prag info
792 %************************************************************************
794 \subsection[Id-arities]{Arity-related functions}
796 %************************************************************************
798 For locally-defined Ids, the code generator maintains its own notion
799 of their arities; so it should not be asking... (but other things
800 besides the code-generator need arity info!)
803 getIdArity :: Id -> ArityInfo
804 getIdArity id@(Id _ _ _ _ _ id_info)
807 addIdArity :: Id -> ArityInfo -> Id
808 addIdArity (Id u n ty details pinfo info) arity
809 = Id u n ty details pinfo (info `addArityInfo` arity)
812 %************************************************************************
814 \subsection[constructor-funs]{@DataCon@-related functions (incl.~tuples)}
816 %************************************************************************
820 -> [StrictnessMark] -> [FieldLabel]
821 -> [TyVar] -> ThetaType
822 -> [TyVar] -> ThetaType
823 -> [TauType] -> TyCon
825 -- can get the tag and all the pieces of the type from the Type
827 mkDataCon n stricts fields tvs ctxt con_tvs con_ctxt args_tys tycon
828 = ASSERT(length stricts == length args_tys)
829 addStandardIdInfo data_con
831 -- NB: data_con self-recursion; should be OK as tags are not
832 -- looked at until late in the game.
837 (AlgConId data_con_tag stricts fields tvs ctxt con_tvs con_ctxt args_tys tycon)
838 IWantToBeINLINEd -- Always inline constructors if possible
841 data_con_tag = assoc "mkDataCon" (data_con_family `zip` [fIRST_TAG..]) data_con
842 data_con_family = tyConDataCons tycon
845 = mkSigmaTy (tvs++con_tvs) (ctxt++con_ctxt)
846 (mkFunTys args_tys (mkTyConApp tycon (mkTyVarTys tvs)))
849 mkTupleCon :: Arity -> Name -> Type -> Id
850 mkTupleCon arity name ty
851 = addStandardIdInfo tuple_id
853 tuple_id = Id (nameUnique name) name ty
855 IWantToBeINLINEd -- Always inline constructors if possible
859 fIRST_TAG = 1 -- Tags allocated from here for real constructors
862 dataConNumFields gives the number of actual fields in the
863 {\em representation} of the data constructor. This may be more than appear
864 in the source code; the extra ones are the existentially quantified
869 = ASSERT( if (isDataCon id) then True else
870 pprTrace "dataConNumFields" (ppr id) False )
871 case (dataConSig id) of { (_, _, _, con_theta, arg_tys, _) ->
872 length con_theta + length arg_tys }
874 isNullaryDataCon con = dataConNumFields con == 0 -- function of convenience
880 dataConTag :: DataCon -> ConTag -- will panic if not a DataCon
881 dataConTag (Id _ _ _ (AlgConId tag _ _ _ _ _ _ _ _) _ _) = tag
882 dataConTag (Id _ _ _ (TupleConId _) _ _) = fIRST_TAG
883 dataConTag (Id _ _ _ (SpecId unspec _ _) _ _) = dataConTag unspec
885 dataConTyCon :: DataCon -> TyCon -- will panic if not a DataCon
886 dataConTyCon (Id _ _ _ (AlgConId _ _ _ _ _ _ _ _ tycon) _ _) = tycon
887 dataConTyCon (Id _ _ _ (TupleConId a) _ _) = tupleTyCon a
889 dataConSig :: DataCon -> ([TyVar], ThetaType, [TyVar], ThetaType, [TauType], TyCon)
890 -- will panic if not a DataCon
892 dataConSig (Id _ _ _ (AlgConId _ _ _ tyvars theta con_tyvars con_theta arg_tys tycon) _ _)
893 = (tyvars, theta, con_tyvars, con_theta, arg_tys, tycon)
895 dataConSig (Id _ _ _ (TupleConId arity) _ _)
896 = (tyvars, [], [], [], tyvar_tys, tupleTyCon arity)
898 tyvars = take arity alphaTyVars
899 tyvar_tys = mkTyVarTys tyvars
901 dataConSig (Id _ _ _ (SpecId unspec ty_maybes _) _ _)
902 = (spec_tyvars, spec_theta_ty, spec_con_tyvars, spec_con_theta, spec_arg_tys, spec_tycon)
904 (tyvars, theta_ty, con_tyvars, con_theta, arg_tys, tycon) = dataConSig unspec
906 ty_env = tyvars `zip` ty_maybes
908 spec_tyvars = [tyvar | (tyvar, Nothing) <- ty_env]
909 spec_con_tyvars = [tyvar | (tyvar, Nothing) <- con_tyvars `zip` ty_maybes] -- Hmm..
911 spec_env = mkTyVarEnv [(tyvar, ty) | (tyvar, Just ty) <- ty_env]
912 spec_arg_tys = map (instantiateTauTy spec_env) arg_tys
914 spec_theta_ty = if null theta_ty then []
915 else panic "dataConSig:ThetaTy:SpecDataCon1"
916 spec_con_theta = if null con_theta then []
917 else panic "dataConSig:ThetaTy:SpecDataCon2"
918 spec_tycon = mkSpecTyCon tycon ty_maybes
921 -- dataConRepType returns the type of the representation of a contructor
922 -- This may differ from the type of the contructor Id itself for two reasons:
923 -- a) the constructor Id may be overloaded, but the dictionary isn't stored
924 -- e.g. data Eq a => T a = MkT a a
926 -- b) the constructor may store an unboxed version of a strict field.
928 -- Here's an example illustrating both:
929 -- data Ord a => T a = MkT Int! a
931 -- T :: Ord a => Int -> a -> T a
932 -- but the rep type is
933 -- Trep :: Int# -> a -> T a
934 -- Actually, the unboxed part isn't implemented yet!
936 dataConRepType :: Id -> Type
937 dataConRepType (Id _ _ _ (AlgConId _ _ _ tyvars theta con_tyvars con_theta arg_tys tycon) _ _)
938 = mkForAllTys (tyvars++con_tyvars)
939 (mkFunTys arg_tys (mkTyConApp tycon (mkTyVarTys tyvars)))
940 dataConRepType other_id
941 = ASSERT( isDataCon other_id )
944 dataConFieldLabels :: DataCon -> [FieldLabel]
945 dataConFieldLabels (Id _ _ _ (AlgConId _ _ fields _ _ _ _ _ _) _ _) = fields
946 dataConFieldLabels (Id _ _ _ (TupleConId _) _ _) = []
948 dataConFieldLabels x@(Id _ _ _ idt _ _) =
949 panic ("dataConFieldLabel: " ++
954 SpecPragmaId _ _ -> "sp"
957 SuperDictSelId _ _ -> "sc"
959 DefaultMethodId _ -> "d"
960 DictFunId _ _ -> "di"
961 SpecId _ _ _ -> "spec"))
964 dataConStrictMarks :: DataCon -> [StrictnessMark]
965 dataConStrictMarks (Id _ _ _ (AlgConId _ stricts _ _ _ _ _ _ _) _ _) = stricts
966 dataConStrictMarks (Id _ _ _ (TupleConId arity) _ _)
967 = nOfThem arity NotMarkedStrict
969 dataConRawArgTys :: DataCon -> [TauType] -- a function of convenience
970 dataConRawArgTys con = case (dataConSig con) of { (_,_, _, _, arg_tys,_) -> arg_tys }
972 dataConArgTys :: DataCon
973 -> [Type] -- Instantiated at these types
974 -> [Type] -- Needs arguments of these types
975 dataConArgTys con_id inst_tys
976 = map (instantiateTy tenv) arg_tys
978 (tyvars, _, _, _, arg_tys, _) = dataConSig con_id
979 tenv = zipTyVarEnv tyvars inst_tys
983 mkRecordSelId field_label selector_ty
984 = addStandardIdInfo $ -- Record selectors have a standard unfolding
988 (RecordSelId field_label)
992 name = fieldLabelName field_label
994 recordSelectorFieldLabel :: Id -> FieldLabel
995 recordSelectorFieldLabel (Id _ _ _ (RecordSelId lbl) _ _) = lbl
997 isRecordSelector (Id _ _ _ (RecordSelId lbl) _ _) = True
998 isRecordSelector other = False
1002 Data type declarations are of the form:
1004 data Foo a b = C1 ... | C2 ... | ... | Cn ...
1006 For each constructor @Ci@, we want to generate a curried function; so, e.g., for
1007 @C1 x y z@, we want a function binding:
1009 fun_C1 = /\ a -> /\ b -> \ [x, y, z] -> Con C1 [a, b] [x, y, z]
1011 Notice the ``big lambdas'' and type arguments to @Con@---we are producing
1012 2nd-order polymorphic lambda calculus with explicit types.
1014 %************************************************************************
1016 \subsection[unfolding-Ids]{Functions related to @Ids@' unfoldings}
1018 %************************************************************************
1021 getIdUnfolding :: Id -> Unfolding
1023 getIdUnfolding (Id _ _ _ _ _ info) = unfoldInfo info
1025 addIdUnfolding :: Id -> Unfolding -> Id
1026 addIdUnfolding id@(Id u n ty details prag info) unfolding
1027 = Id u n ty details prag (info `addUnfoldInfo` unfolding)
1030 The inline pragma tells us to be very keen to inline this Id, but it's still
1031 OK not to if optimisation is switched off.
1034 getInlinePragma :: Id -> PragmaInfo
1035 getInlinePragma (Id _ _ _ _ prag _) = prag
1037 idWantsToBeINLINEd :: Id -> Bool
1039 idWantsToBeINLINEd (Id _ _ _ _ IWantToBeINLINEd _) = True
1040 idWantsToBeINLINEd (Id _ _ _ _ IMustBeINLINEd _) = True
1041 idWantsToBeINLINEd _ = False
1043 idMustNotBeINLINEd (Id _ _ _ _ IMustNotBeINLINEd _) = True
1044 idMustNotBeINLINEd _ = False
1046 idMustBeINLINEd (Id _ _ _ _ IMustBeINLINEd _) = True
1047 idMustBeINLINEd _ = False
1049 addInlinePragma :: Id -> Id
1050 addInlinePragma (Id u sn ty details _ info)
1051 = Id u sn ty details IWantToBeINLINEd info
1053 nukeNoInlinePragma :: Id -> Id
1054 nukeNoInlinePragma id@(Id u sn ty details IMustNotBeINLINEd info)
1055 = Id u sn ty details NoPragmaInfo info
1056 nukeNoInlinePragma id@(Id u sn ty details _ info) = id -- Otherwise no-op
1058 addNoInlinePragma :: Id -> Id
1059 addNoInlinePragma id@(Id u sn ty details _ info)
1060 = Id u sn ty details IMustNotBeINLINEd info
1065 %************************************************************************
1067 \subsection[IdInfo-funs]{Functions related to @Ids@' @IdInfos@}
1069 %************************************************************************
1072 getIdDemandInfo :: Id -> DemandInfo
1073 getIdDemandInfo (Id _ _ _ _ _ info) = demandInfo info
1075 addIdDemandInfo :: Id -> DemandInfo -> Id
1076 addIdDemandInfo (Id u n ty details prags info) demand_info
1077 = Id u n ty details prags (info `addDemandInfo` demand_info)
1081 getIdUpdateInfo :: Id -> UpdateInfo
1082 getIdUpdateInfo (Id _ _ _ _ _ info) = updateInfo info
1084 addIdUpdateInfo :: Id -> UpdateInfo -> Id
1085 addIdUpdateInfo (Id u n ty details prags info) upd_info
1086 = Id u n ty details prags (info `addUpdateInfo` upd_info)
1091 getIdArgUsageInfo :: Id -> ArgUsageInfo
1092 getIdArgUsageInfo (Id u n ty info details) = argUsageInfo info
1094 addIdArgUsageInfo :: Id -> ArgUsageInfo -> Id
1095 addIdArgUsageInfo (Id u n ty info details) au_info
1096 = Id u n ty (info `addArgusageInfo` au_info) details
1102 getIdFBTypeInfo :: Id -> FBTypeInfo
1103 getIdFBTypeInfo (Id u n ty info details) = fbTypeInfo info
1105 addIdFBTypeInfo :: Id -> FBTypeInfo -> Id
1106 addIdFBTypeInfo (Id u n ty info details) upd_info
1107 = Id u n ty (info `addFBTypeInfo` upd_info) details
1112 getIdSpecialisation :: Id -> IdSpecEnv
1113 getIdSpecialisation (Id _ _ _ _ _ info) = specInfo info
1115 addIdSpecialisation :: Id -> IdSpecEnv -> Id
1116 addIdSpecialisation (Id u n ty details prags info) spec_info
1117 = Id u n ty details prags (info `addSpecInfo` spec_info)
1120 Strictness: we snaffle the info out of the IdInfo.
1123 getIdStrictness :: Id -> StrictnessInfo
1125 getIdStrictness (Id _ _ _ _ _ info) = strictnessInfo info
1127 addIdStrictness :: Id -> StrictnessInfo -> Id
1128 addIdStrictness (Id u n ty details prags info) strict_info
1129 = Id u n ty details prags (info `addStrictnessInfo` strict_info)
1132 %************************************************************************
1134 \subsection[Id-comparison]{Comparison functions for @Id@s}
1136 %************************************************************************
1138 Comparison: equality and ordering---this stuff gets {\em hammered}.
1141 cmpId (Id u1 _ _ _ _ _) (Id u2 _ _ _ _ _) = compare u1 u2
1142 -- short and very sweet
1146 instance Eq (GenId ty) where
1147 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
1148 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
1150 instance Ord (GenId ty) where
1151 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
1152 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
1153 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
1154 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
1155 compare a b = cmpId a b
1158 @cmpId_withSpecDataCon@ ensures that any spectys are taken into
1159 account when comparing two data constructors. We need to do this
1160 because a specialised data constructor has the same Unique as its
1161 unspecialised counterpart.
1164 cmpId_withSpecDataCon :: Id -> Id -> Ordering
1166 cmpId_withSpecDataCon id1 id2
1167 | eq_ids && isDataCon id1 && isDataCon id2
1168 = cmpEqDataCon id1 id2
1173 cmp_ids = cmpId id1 id2
1174 eq_ids = case cmp_ids of { EQ -> True; other -> False }
1176 cmpEqDataCon (Id _ _ _ (SpecId _ mtys1 _) _ _) (Id _ _ _ (SpecId _ mtys2 _) _ _)
1177 = panic "Id.cmpEqDataCon:cmpUniTypeMaybeList mtys1 mtys2"
1179 cmpEqDataCon _ (Id _ _ _ (SpecId _ _ _) _ _) = LT
1180 cmpEqDataCon (Id _ _ _ (SpecId _ _ _) _ _) _ = GT
1181 cmpEqDataCon _ _ = EQ
1184 %************************************************************************
1186 \subsection[Id-other-instances]{Other instance declarations for @Id@s}
1188 %************************************************************************
1191 instance Outputable ty => Outputable (GenId ty) where
1194 showId :: Id -> String
1195 showId id = showSDoc (pprId id)
1198 Default printing code (not used for interfaces):
1200 pprId :: Outputable ty => GenId ty -> SDoc
1202 pprId (Id u n _ _ prags _)
1203 = hcat [ppr n, pp_prags]
1205 pp_prags | opt_PprStyle_All = case prags of
1206 IMustNotBeINLINEd -> text "{n}"
1207 IWantToBeINLINEd -> text "{i}"
1208 IMustBeINLINEd -> text "{I}"
1212 -- WDP 96/05/06: We can re-elaborate this as we go along...
1216 idUnique (Id u _ _ _ _ _) = u
1218 instance Uniquable (GenId ty) where
1221 instance NamedThing (GenId ty) where
1222 getName this_id@(Id u n _ details _ _) = n
1225 Note: The code generator doesn't carry a @UniqueSupply@, so it uses
1226 the @Uniques@ out of local @Ids@ given to it.
1228 %************************************************************************
1230 \subsection{@IdEnv@s and @IdSet@s}
1232 %************************************************************************
1235 type IdEnv elt = UniqFM elt
1237 nullIdEnv :: IdEnv a
1239 mkIdEnv :: [(GenId ty, a)] -> IdEnv a
1240 unitIdEnv :: GenId ty -> a -> IdEnv a
1241 addOneToIdEnv :: IdEnv a -> GenId ty -> a -> IdEnv a
1242 growIdEnv :: IdEnv a -> IdEnv a -> IdEnv a
1243 growIdEnvList :: IdEnv a -> [(GenId ty, a)] -> IdEnv a
1245 delManyFromIdEnv :: IdEnv a -> [GenId ty] -> IdEnv a
1246 delOneFromIdEnv :: IdEnv a -> GenId ty -> IdEnv a
1247 combineIdEnvs :: (a -> a -> a) -> IdEnv a -> IdEnv a -> IdEnv a
1248 mapIdEnv :: (a -> b) -> IdEnv a -> IdEnv b
1249 modifyIdEnv :: (a -> a) -> IdEnv a -> GenId ty -> IdEnv a
1250 rngIdEnv :: IdEnv a -> [a]
1252 isNullIdEnv :: IdEnv a -> Bool
1253 lookupIdEnv :: IdEnv a -> GenId ty -> Maybe a
1254 lookupNoFailIdEnv :: IdEnv a -> GenId ty -> a
1258 addOneToIdEnv = addToUFM
1259 combineIdEnvs = plusUFM_C
1260 delManyFromIdEnv = delListFromUFM
1261 delOneFromIdEnv = delFromUFM
1263 lookupIdEnv = lookupUFM
1266 nullIdEnv = emptyUFM
1270 growIdEnvList env pairs = plusUFM env (listToUFM pairs)
1271 isNullIdEnv env = sizeUFM env == 0
1272 lookupNoFailIdEnv env id = case (lookupIdEnv env id) of { Just xx -> xx }
1274 -- modifyIdEnv: Look up a thing in the IdEnv, then mash it with the
1275 -- modify function, and put it back.
1277 modifyIdEnv mangle_fn env key
1278 = case (lookupIdEnv env key) of
1280 Just xx -> addOneToIdEnv env key (mangle_fn xx)
1282 modifyIdEnv_Directly mangle_fn env key
1283 = case (lookupUFM_Directly env key) of
1285 Just xx -> addToUFM_Directly env key (mangle_fn xx)
1289 type GenIdSet ty = UniqSet (GenId ty)
1290 type IdSet = UniqSet (GenId Type)
1292 emptyIdSet :: GenIdSet ty
1293 intersectIdSets :: GenIdSet ty -> GenIdSet ty -> GenIdSet ty
1294 unionIdSets :: GenIdSet ty -> GenIdSet ty -> GenIdSet ty
1295 unionManyIdSets :: [GenIdSet ty] -> GenIdSet ty
1296 idSetToList :: GenIdSet ty -> [GenId ty]
1297 unitIdSet :: GenId ty -> GenIdSet ty
1298 addOneToIdSet :: GenIdSet ty -> GenId ty -> GenIdSet ty
1299 elementOfIdSet :: GenId ty -> GenIdSet ty -> Bool
1300 minusIdSet :: GenIdSet ty -> GenIdSet ty -> GenIdSet ty
1301 isEmptyIdSet :: GenIdSet ty -> Bool
1302 mkIdSet :: [GenId ty] -> GenIdSet ty
1304 emptyIdSet = emptyUniqSet
1305 unitIdSet = unitUniqSet
1306 addOneToIdSet = addOneToUniqSet
1307 intersectIdSets = intersectUniqSets
1308 unionIdSets = unionUniqSets
1309 unionManyIdSets = unionManyUniqSets
1310 idSetToList = uniqSetToList
1311 elementOfIdSet = elementOfUniqSet
1312 minusIdSet = minusUniqSet
1313 isEmptyIdSet = isEmptyUniqSet