{- (c) The University of Glasgow 2006 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 \section{@Vars@: Variables} -}{-# LANGUAGE CPP, FlexibleContexts, MultiWayIf, FlexibleInstances, DeriveDataTypeable #-}-- |-- #name_types#-- GHC uses several kinds of name internally:---- * 'OccName.OccName': see "OccName#name_types"---- * 'RdrName.RdrName': see "RdrName#name_types"---- * 'Name.Name': see "Name#name_types"---- * 'Id.Id': see "Id#name_types"---- * 'Var.Var' is a synonym for the 'Id.Id' type but it may additionally-- potentially contain type variables, which have a 'TyCoRep.Kind'-- rather than a 'TyCoRep.Type' and only contain some extra-- details during typechecking.---- These 'Var.Var' names may either be global or local, see "Var#globalvslocal"---- #globalvslocal#-- Global 'Id's and 'Var's are those that are imported or correspond-- to a data constructor, primitive operation, or record selectors.-- Local 'Id's and 'Var's are those bound within an expression-- (e.g. by a lambda) or at the top level of the module being compiled.moduleVar(-- * The main data type and synonymsVar ,CoVar ,Id ,NcId ,DictId ,DFunId ,EvVar ,EqVar ,EvId ,IpId ,JoinId ,TyVar ,TcTyVar ,TypeVar ,KindVar ,TKVar ,TyCoVar ,-- * In and Out variantsInVar ,InCoVar ,InId ,InTyVar ,OutVar ,OutCoVar ,OutId ,OutTyVar ,-- ** Taking 'Var's apartvarName ,varUnique ,varType ,-- ** Modifying 'Var'ssetVarName ,setVarUnique ,setVarType ,updateVarType ,updateVarTypeM ,-- ** Constructing, taking apart, modifying 'Id'smkGlobalVar ,mkLocalVar ,mkExportedLocalVar ,mkCoVar ,idInfo ,idDetails ,lazySetIdInfo ,setIdDetails ,globaliseId ,setIdExported ,setIdNotExported ,-- ** PredicatesisId ,isTyVar ,isTcTyVar ,isLocalVar ,isLocalId ,isCoVar ,isNonCoVarId ,isTyCoVar ,isGlobalId ,isExportedId ,mustHaveLocalBinding ,-- * TyVar'sVarBndr (..),ArgFlag (..),TyCoVarBinder ,TyVarBinder ,binderVar ,binderVars ,binderArgFlag ,binderType ,isVisibleArgFlag ,isInvisibleArgFlag ,sameVis ,mkTyCoVarBinder ,mkTyCoVarBinders ,mkTyVarBinder ,mkTyVarBinders ,isTyVarBinder ,-- ** Constructing TyVar'smkTyVar ,mkTcTyVar ,-- ** Taking 'TyVar's aparttyVarName ,tyVarKind ,tcTyVarDetails ,setTcTyVarDetails ,-- ** Modifying 'TyVar'ssetTyVarName ,setTyVarUnique ,setTyVarKind ,updateTyVarKind ,updateTyVarKindM ,nonDetCmpVar )where#include "HsVersions.h" importGhcPrelude import{-# SOURCE#-}TyCoRep (Type ,Kind ,pprKind )import{-# SOURCE#-}TcType (TcTyVarDetails ,pprTcTyVarDetails ,vanillaSkolemTv )import{-# SOURCE#-}IdInfo (IdDetails ,IdInfo ,coVarDetails ,isCoVarDetails ,vanillaIdInfo ,pprIdDetails )importName hiding(varName )importUnique (Uniquable ,Unique ,getKey ,getUnique ,mkUniqueGrimily ,nonDetCmpUnique )importUtil importBinary importDynFlags importOutputable importData.Data{- ************************************************************************ * * Synonyms * * ************************************************************************ -- These synonyms are here and not in Id because otherwise we need a very -- large number of SOURCE imports of Id.hs :-( -}-- | IdentifiertypeId =Var -- A term-level identifier-- predicate: isId-- | Coercion VariabletypeCoVar =Id -- See Note [Evidence: EvIds and CoVars]-- predicate: isCoVar-- |typeNcId =Id -- A term-level (value) variable that is-- /not/ an (unlifted) coercion-- predicate: isNonCoVarId-- | Type or kind VariabletypeTyVar =Var -- Type *or* kind variable (historical)-- | Type or Kind VariabletypeTKVar =Var -- Type *or* kind variable (historical)-- | Type variable that might be a metavariabletypeTcTyVar =Var -- | Type VariabletypeTypeVar =Var -- Definitely a type variable-- | Kind VariabletypeKindVar =Var -- Definitely a kind variable-- See Note [Kind and type variables]-- See Note [Evidence: EvIds and CoVars]-- | Evidence IdentifiertypeEvId =Id -- Term-level evidence: DictId, IpId, or EqVar-- | Evidence VariabletypeEvVar =EvId -- ...historical name for EvId-- | Dictionary Function IdentifiertypeDFunId =Id -- A dictionary function-- | Dictionary IdentifiertypeDictId =EvId -- A dictionary variable-- | Implicit parameter IdentifiertypeIpId =EvId -- A term-level implicit parameter-- | Equality VariabletypeEqVar =EvId -- Boxed equality evidencetypeJoinId =Id -- A join variable-- | Type or Coercion VariabletypeTyCoVar =Id -- Type, *or* coercion variable-- predicate: isTyCoVar{- Many passes apply a substitution, and it's very handy to have type synonyms to remind us whether or not the substitution has been applied -}typeInVar =Var typeInTyVar =TyVar typeInCoVar =CoVar typeInId =Id typeOutVar =Var typeOutTyVar =TyVar typeOutCoVar =CoVar typeOutId =Id {- Note [Evidence: EvIds and CoVars] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * An EvId (evidence Id) is a term-level evidence variable (dictionary, implicit parameter, or equality). Could be boxed or unboxed. * DictId, IpId, and EqVar are synonyms when we know what kind of evidence we are talking about. For example, an EqVar has type (t1 ~ t2). * A CoVar is always an un-lifted coercion, of type (t1 ~# t2) or (t1 ~R# t2) Note [Kind and type variables] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Before kind polymorphism, TyVar were used to mean type variables. Now they are used to mean kind *or* type variables. KindVar is used when we know for sure that it is a kind variable. In future, we might want to go over the whole compiler code to use: - TKVar to mean kind or type variables - TypeVar to mean type variables only - KindVar to mean kind variables ************************************************************************ * * \subsection{The main data type declarations} * * ************************************************************************ Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a @Type@, and an @IdInfo@ (non-essential info about it, e.g., strictness). The essential info about different kinds of @Vars@ is in its @VarDetails@. -}-- | Variable---- Essentially a typed 'Name', that may also contain some additional information-- about the 'Var' and its use sites.dataVar =TyVar {-- Type and kind variables-- see Note [Kind and type variables]varName ::!Name ,realUnique ::{-# UNPACK#-}!Int,-- ^ Key for fast comparison-- Identical to the Unique in the name,-- cached here for speedvarType ::Kind -- ^ The type or kind of the 'Var' in question}|TcTyVar {-- Used only during type inference-- Used for kind variables during-- inference, as wellvarName ::!Name ,realUnique ::{-# UNPACK#-}!Int,varType ::Kind ,tc_tv_details ::TcTyVarDetails }|Id {varName ::!Name ,realUnique ::{-# UNPACK#-}!Int,varType ::Type ,idScope ::IdScope ,id_details ::IdDetails ,-- Stable, doesn't changeid_info ::IdInfo }-- Unstable, updated by simplifier-- | Identifier ScopedataIdScope -- See Note [GlobalId/LocalId]=GlobalId |LocalId ExportFlag dataExportFlag -- See Note [ExportFlag on binders]=NotExported -- ^ Not exported: may be discarded as dead code.|Exported -- ^ Exported: kept alive{- Note [ExportFlag on binders] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ An ExportFlag of "Exported" on a top-level binder says "keep this binding alive; do not drop it as dead code". This transitively keeps alive all the other top-level bindings that this binding refers to. This property is persisted all the way down the pipeline, so that the binding will be compiled all the way to object code, and its symbols will appear in the linker symbol table. However, note that this use of "exported" is quite different to the export list on a Haskell module. Setting the ExportFlag on an Id does /not/ mean that if you import the module (in Haskell source code) you will see this Id. Of course, things that appear in the export list of the source Haskell module do indeed have their ExportFlag set. But many other things, such as dictionary functions, are kept alive by having their ExportFlag set, even though they are not exported in the source-code sense. We should probably use a different term for ExportFlag, like KeepAlive. Note [GlobalId/LocalId] ~~~~~~~~~~~~~~~~~~~~~~~ A GlobalId is * always a constant (top-level) * imported, or data constructor, or primop, or record selector * has a Unique that is globally unique across the whole GHC invocation (a single invocation may compile multiple modules) * never treated as a candidate by the free-variable finder; it's a constant! A LocalId is * bound within an expression (lambda, case, local let(rec)) * or defined at top level in the module being compiled * always treated as a candidate by the free-variable finder After CoreTidy, top-level LocalIds are turned into GlobalIds -}instanceOutputable Var whereppr var =sdocWithDynFlags $\dflags ->getPprStyle $\ppr_style ->if|debugStyle ppr_style &&(not(gopt Opt_SuppressVarKinds dflags ))->parens (ppr (varNamevar )<+> ppr_debug var ppr_style <+> dcolon <+> pprKind (tyVarKind var ))|otherwise->ppr (varNamevar )<> ppr_debug var ppr_style ppr_debug::Var ->PprStyle ->SDoc ppr_debug (TyVar {})sty |debugStyle sty =brackets (text "tv")ppr_debug(TcTyVar {tc_tv_details=d })sty |dumpStyle sty ||debugStyle sty =brackets (pprTcTyVarDetails d )ppr_debug(Id {idScope=s ,id_details=d })sty |debugStyle sty =brackets (ppr_id_scope s <> pprIdDetails d )ppr_debug__=empty ppr_id_scope::IdScope ->SDoc ppr_id_scope GlobalId =text "gid"ppr_id_scope(LocalId Exported )=text "lidx"ppr_id_scope(LocalId NotExported )=text "lid"instanceNamedThing Var wheregetName =varNameinstanceUniquable Var wheregetUnique =varUnique instanceEqVar wherea == b =realUniquea ==realUniqueb instanceOrdVar wherea <= b =realUniquea <=realUniqueb a < b =realUniquea <realUniqueb a >= b =realUniquea >=realUniqueb a > b =realUniquea >realUniqueb a `compare `b =a `nonDetCmpVar `b -- | Compare Vars by their Uniques.-- This is what Ord Var does, provided here to make it explicit at the-- call-site that it can introduce non-determinism.-- See Note [Unique Determinism]nonDetCmpVar::Var ->Var ->OrderingnonDetCmpVar a b =varUnique a `nonDetCmpUnique `varUnique b instanceDataVar where-- don't traverse?toConstr _=abstractConstr "Var"gunfold __=error"gunfold"dataTypeOf _=mkNoRepType"Var"instanceHasOccName Var whereoccName =nameOccName .varNamevarUnique::Var ->Unique varUnique var =mkUniqueGrimily (realUniquevar )setVarUnique::Var ->Unique ->Var setVarUnique var uniq =var {realUnique=getKey uniq ,varName=setNameUnique (varNamevar )uniq }setVarName::Var ->Name ->Var setVarName var new_name =var {realUnique=getKey (getUnique new_name ),varName=new_name }setVarType::Id ->Type ->Id setVarType id ty =id {varType=ty }updateVarType::(Type ->Type )->Id ->Id updateVarType f id =id {varType=f (varTypeid )}updateVarTypeM::Monadm =>(Type ->m Type )->Id ->m Id updateVarTypeM f id =do{ty' <-f (varTypeid );return(id {varType=ty' })}{- ********************************************************************* * * * ArgFlag * * ********************************************************************* -}-- | Argument Flag---- Is something required to appear in source Haskell ('Required'),-- permitted by request ('Specified') (visible type application), or-- prohibited entirely from appearing in source Haskell ('Inferred')?-- See Note [VarBndrs, TyCoVarBinders, TyConBinders, and visibility] in TyCoRepdataArgFlag =Inferred |Specified |Required deriving(Eq,Ord,Data)-- (<) on ArgFlag means "is less visible than"-- | Does this 'ArgFlag' classify an argument that is written in Haskell?isVisibleArgFlag::ArgFlag ->BoolisVisibleArgFlag Required =TrueisVisibleArgFlag_=False-- | Does this 'ArgFlag' classify an argument that is not written in Haskell?isInvisibleArgFlag::ArgFlag ->BoolisInvisibleArgFlag =not.isVisibleArgFlag -- | Do these denote the same level of visibility? 'Required'-- arguments are visible, others are not. So this function-- equates 'Specified' and 'Inferred'. Used for printing.sameVis::ArgFlag ->ArgFlag ->BoolsameVis Required Required =TruesameVisRequired _=FalsesameVis_Required =FalsesameVis__=TrueinstanceOutputable ArgFlag whereppr Required =text "[req]"pprSpecified =text "[spec]"pprInferred =text "[infrd]"instanceBinary ArgFlag whereput_ bh Required =putByte bh 0put_bh Specified =putByte bh 1put_bh Inferred =putByte bh 2get bh =doh <-getByte bh caseh of0->returnRequired 1->returnSpecified _->returnInferred {- ********************************************************************* * * * VarBndr, TyCoVarBinder * * ********************************************************************* -}-- Variable Binder---- VarBndr is polymorphic in both var and visibility fields.-- Currently there are six different uses of 'VarBndr':-- * Var.TyVarBinder = VarBndr TyVar ArgFlag-- * Var.TyCoVarBinder = VarBndr TyCoVar ArgFlag-- * TyCon.TyConBinder = VarBndr TyVar TyConBndrVis-- * TyCon.TyConTyCoBinder = VarBndr TyCoVar TyConBndrVis-- * IfaceType.IfaceForAllBndr = VarBndr IfaceBndr ArgFlag-- * IfaceType.IfaceTyConBinder = VarBndr IfaceBndr TyConBndrVisdataVarBndr var argf =Bndr var argf deriving(Data)-- | Variable Binder---- A 'TyCoVarBinder' is the binder of a ForAllTy-- It's convenient to define this synonym here rather its natural-- home in TyCoRep, because it's used in DataCon.hs-boot---- A 'TyVarBinder' is a binder with only TyVartypeTyCoVarBinder =VarBndr TyCoVar ArgFlag typeTyVarBinder =VarBndr TyVar ArgFlag binderVar::VarBndr tv argf ->tv binderVar (Bndr v _)=v binderVars::[VarBndr tv argf ]->[tv ]binderVars tvbs =mapbinderVar tvbs binderArgFlag::VarBndr tv argf ->argf binderArgFlag (Bndr _argf )=argf binderType::VarBndr TyCoVar argf ->Type binderType (Bndr tv _)=varTypetv -- | Make a named bindermkTyCoVarBinder::ArgFlag ->TyCoVar ->TyCoVarBinder mkTyCoVarBinder vis var =Bndr var vis -- | Make a named binder-- 'var' should be a type variablemkTyVarBinder::ArgFlag ->TyVar ->TyVarBinder mkTyVarBinder vis var =ASSERT(isTyVarvar)Bndr var vis -- | Make many named bindersmkTyCoVarBinders::ArgFlag ->[TyCoVar ]->[TyCoVarBinder ]mkTyCoVarBinders vis =map(mkTyCoVarBinder vis )-- | Make many named binders-- Input vars should be type variablesmkTyVarBinders::ArgFlag ->[TyVar ]->[TyVarBinder ]mkTyVarBinders vis =map(mkTyVarBinder vis )isTyVarBinder::TyCoVarBinder ->BoolisTyVarBinder (Bndr v _)=isTyVar v instanceOutputable tv =>Outputable (VarBndr tv ArgFlag )whereppr (Bndr v Required )=ppr v ppr(Bndr v Specified )=char '@'<> ppr v ppr(Bndr v Inferred )=braces (ppr v )instance(Binary tv ,Binary vis )=>Binary (VarBndr tv vis )whereput_ bh (Bndr tv vis )=do{put_ bh tv ;put_ bh vis }get bh =do{tv <-get bh ;vis <-get bh ;return(Bndr tv vis )}instanceNamedThing tv =>NamedThing (VarBndr tv flag )wheregetName (Bndr tv _)=getName tv {- ************************************************************************ * * * Type and kind variables * * * ************************************************************************ -}tyVarName::TyVar ->Name tyVarName =varNametyVarKind::TyVar ->Kind tyVarKind =varTypesetTyVarUnique::TyVar ->Unique ->TyVar setTyVarUnique =setVarUnique setTyVarName::TyVar ->Name ->TyVar setTyVarName =setVarName setTyVarKind::TyVar ->Kind ->TyVar setTyVarKind tv k =tv {varType=k }updateTyVarKind::(Kind ->Kind )->TyVar ->TyVar updateTyVarKind update tv =tv {varType=update (tyVarKind tv )}updateTyVarKindM::(Monadm )=>(Kind ->m Kind )->TyVar ->m TyVar updateTyVarKindM update tv =do{k' <-update (tyVarKind tv );return$tv {varType=k' }}mkTyVar::Name ->Kind ->TyVar mkTyVar name kind =TyVar {varName=name ,realUnique=getKey (nameUnique name ),varType=kind }mkTcTyVar::Name ->Kind ->TcTyVarDetails ->TyVar mkTcTyVar name kind details =-- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'TcTyVar {varName=name ,realUnique=getKey (nameUnique name ),varType=kind ,tc_tv_details=details }tcTyVarDetails::TyVar ->TcTyVarDetails -- See Note [TcTyVars in the typechecker] in TcTypetcTyVarDetails (TcTyVar {tc_tv_details=details })=details tcTyVarDetails(TyVar {})=vanillaSkolemTv tcTyVarDetailsvar =pprPanic "tcTyVarDetails"(ppr var <+> dcolon <+> pprKind (tyVarKind var ))setTcTyVarDetails::TyVar ->TcTyVarDetails ->TyVar setTcTyVarDetails tv details =tv {tc_tv_details=details }{- %************************************************************************ %* * \subsection{Ids} * * ************************************************************************ -}idInfo::HasDebugCallStack =>Id ->IdInfo idInfo (Id {id_info=info })=info idInfoother =pprPanic "idInfo"(ppr other )idDetails::Id ->IdDetails idDetails (Id {id_details=details })=details idDetailsother =pprPanic "idDetails"(ppr other )-- The next three have a 'Var' suffix even though they always build-- Ids, because Id.hs uses 'mkGlobalId' etc with different typesmkGlobalVar::IdDetails ->Name ->Type ->IdInfo ->Id mkGlobalVar details name ty info =mk_id name ty GlobalId details info mkLocalVar::IdDetails ->Name ->Type ->IdInfo ->Id mkLocalVar details name ty info =mk_id name ty (LocalId NotExported )details info mkCoVar::Name ->Type ->CoVar -- Coercion variables have no IdInfomkCoVar name ty =mk_id name ty (LocalId NotExported )coVarDetails vanillaIdInfo -- | Exported 'Var's will not be removed as dead codemkExportedLocalVar::IdDetails ->Name ->Type ->IdInfo ->Id mkExportedLocalVar details name ty info =mk_id name ty (LocalId Exported )details info mk_id::Name ->Type ->IdScope ->IdDetails ->IdInfo ->Id mk_id name ty scope details info =Id {varName=name ,realUnique=getKey (nameUnique name ),varType=ty ,idScope=scope ,id_details=details ,id_info=info }-------------------lazySetIdInfo::Id ->IdInfo ->Var lazySetIdInfo id info =id {id_info=info }setIdDetails::Id ->IdDetails ->Id setIdDetails id details =id {id_details=details }globaliseId::Id ->Id -- ^ If it's a local, make it globalglobaliseId id =id {idScope=GlobalId }setIdExported::Id ->Id -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors-- and class operations, which are born as global 'Id's and automatically exportedsetIdExported id @(Id {idScope=LocalId {}})=id {idScope=LocalId Exported }setIdExportedid @(Id {idScope=GlobalId })=id setIdExportedtv =pprPanic "setIdExported"(ppr tv )setIdNotExported::Id ->Id -- ^ We can only do this to LocalIdssetIdNotExported id =ASSERT(isLocalIdid)id {idScope=LocalId NotExported }{- ************************************************************************ * * \subsection{Predicates over variables} * * ************************************************************************ -}isTyVar::Var ->Bool-- True of both TyVar and TcTyVarisTyVar (TyVar {})=TrueisTyVar(TcTyVar {})=TrueisTyVar_=FalseisTcTyVar::Var ->Bool-- True of TcTyVar onlyisTcTyVar (TcTyVar {})=TrueisTcTyVar_=FalseisTyCoVar::Var ->BoolisTyCoVar v =isTyVar v ||isCoVar v isId::Var ->BoolisId (Id {})=TrueisId_=FalseisCoVar::Var ->Bool-- A coercion variableisCoVar (Id {id_details=details })=isCoVarDetails details isCoVar_=FalseisNonCoVarId::Var ->Bool-- A term variable (Id) that is /not/ a coercion variableisNonCoVarId (Id {id_details=details })=not(isCoVarDetails details )isNonCoVarId_=FalseisLocalId::Var ->BoolisLocalId (Id {idScope=LocalId _})=TrueisLocalId_=False-- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's-- These are the variables that we need to pay attention to when finding free-- variables, or doing dependency analysis.isLocalVar::Var ->BoolisLocalVar v =not(isGlobalId v )isGlobalId::Var ->BoolisGlobalId (Id {idScope=GlobalId })=TrueisGlobalId_=False-- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's-- that must have a binding in this module. The converse-- is not quite right: there are some global 'Id's that must have-- bindings, such as record selectors. But that doesn't matter,-- because it's only used for assertionsmustHaveLocalBinding::Var ->BoolmustHaveLocalBinding var =isLocalVar var -- | 'isExportedIdVar' means \"don't throw this away\"isExportedId::Var ->BoolisExportedId (Id {idScope=GlobalId })=TrueisExportedId(Id {idScope=LocalId Exported })=TrueisExportedId_=False