2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
5 \section[BasicTypes]{Miscellanous types}
7 This module defines a miscellaneously collection of very simple
11 \item have no other obvious home
12 \item don't depend on any other complicated types
13 \item are used in more than one "part" of the compiler
17 {-# LANGUAGE DeriveDataTypeable #-}
20 Version, bumpVersion, initialVersion,
28 Fixity(..), FixityDirection(..),
29 defaultFixity, maxPrecedence,
30 negateFixity, funTyFixity,
33 IPName(..), ipNameName, mapIPName,
35 RecFlag(..), isRec, isNonRec, boolToRecFlag,
39 TopLevelFlag(..), isTopLevel, isNotTopLevel,
45 TupCon(..), tupleParens,
47 OccInfo(..), seqOccInfo, zapFragileOcc, isOneOcc,
48 isDeadOcc, isLoopBreaker, isNonRuleLoopBreaker, isNoOcc,
51 InsideLam, insideLam, notInsideLam,
52 OneBranch, oneBranch, notOneBranch,
57 HsBang(..), isBanged, isMarkedUnboxed,
58 StrictnessMark(..), isMarkedStrict,
63 Activation(..), isActive, isNeverActive, isAlwaysActive, isEarlyActive,
64 RuleMatchInfo(..), isConLike, isFunLike,
65 InlinePragma(..), defaultInlinePragma, alwaysInlinePragma, neverInlinePragma, dfunInlinePragma,
66 isDefaultInlinePragma, isInlinePragma, inlinePragmaSat,
67 inlinePragmaActivation, inlinePragmaRuleMatchInfo,
68 setInlinePragmaActivation, setInlinePragmaRuleMatchInfo,
70 SuccessFlag(..), succeeded, failed, successIf
76 import Data.Data hiding (Fixity)
79 %************************************************************************
81 \subsection[Arity]{Arity}
83 %************************************************************************
89 %************************************************************************
91 \subsection[FunctionOrData]{FunctionOrData}
93 %************************************************************************
96 data FunctionOrData = IsFunction | IsData
97 deriving (Eq, Ord, Data, Typeable)
99 instance Outputable FunctionOrData where
100 ppr IsFunction = text "(function)"
101 ppr IsData = text "(data)"
105 %************************************************************************
107 \subsection[Version]{Module and identifier version numbers}
109 %************************************************************************
114 bumpVersion :: Version -> Version
117 initialVersion :: Version
121 %************************************************************************
125 %************************************************************************
129 -- reason/explanation from a WARNING or DEPRECATED pragma
130 data WarningTxt = WarningTxt [FastString]
131 | DeprecatedTxt [FastString]
132 deriving (Eq, Data, Typeable)
134 instance Outputable WarningTxt where
135 ppr (WarningTxt ws) = doubleQuotes (vcat (map ftext ws))
136 ppr (DeprecatedTxt ds) = text "Deprecated:" <+>
137 doubleQuotes (vcat (map ftext ds))
140 %************************************************************************
142 \subsection{Implicit parameter identity}
144 %************************************************************************
146 The @IPName@ type is here because it is used in TypeRep (i.e. very
147 early in the hierarchy), but also in HsSyn.
150 newtype IPName name = IPName name -- ?x
151 deriving( Eq, Ord, Data, Typeable )
152 -- Ord is used in the IP name cache finite map
153 -- (used in HscTypes.OrigIParamCache)
155 ipNameName :: IPName name -> name
156 ipNameName (IPName n) = n
158 mapIPName :: (a->b) -> IPName a -> IPName b
159 mapIPName f (IPName n) = IPName (f n)
161 instance Outputable name => Outputable (IPName name) where
162 ppr (IPName n) = char '?' <> ppr n -- Ordinary implicit parameters
165 %************************************************************************
169 %************************************************************************
172 type RuleName = FastString
175 %************************************************************************
177 \subsection[Fixity]{Fixity info}
179 %************************************************************************
182 ------------------------
183 data Fixity = Fixity Int FixityDirection
184 deriving (Data, Typeable)
186 instance Outputable Fixity where
187 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
189 instance Eq Fixity where -- Used to determine if two fixities conflict
190 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
192 ------------------------
193 data FixityDirection = InfixL | InfixR | InfixN
194 deriving (Eq, Data, Typeable)
196 instance Outputable FixityDirection where
197 ppr InfixL = ptext (sLit "infixl")
198 ppr InfixR = ptext (sLit "infixr")
199 ppr InfixN = ptext (sLit "infix")
201 ------------------------
204 defaultFixity :: Fixity
205 defaultFixity = Fixity maxPrecedence InfixL
207 negateFixity, funTyFixity :: Fixity
209 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
210 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
218 @(compareFixity op1 op2)@ tells which way to arrange appication, or
219 whether there's an error.
222 compareFixity :: Fixity -> Fixity
223 -> (Bool, -- Error please
224 Bool) -- Associate to the right: a op1 (b op2 c)
225 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
226 = case prec1 `compare` prec2 of
229 EQ -> case (dir1, dir2) of
230 (InfixR, InfixR) -> right
231 (InfixL, InfixL) -> left
234 right = (False, True)
235 left = (False, False)
236 error_please = (True, False)
240 %************************************************************************
242 \subsection[Top-level/local]{Top-level/not-top level flag}
244 %************************************************************************
251 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
253 isNotTopLevel NotTopLevel = True
254 isNotTopLevel TopLevel = False
256 isTopLevel TopLevel = True
257 isTopLevel NotTopLevel = False
259 instance Outputable TopLevelFlag where
260 ppr TopLevel = ptext (sLit "<TopLevel>")
261 ppr NotTopLevel = ptext (sLit "<NotTopLevel>")
265 %************************************************************************
267 Top-level/not-top level flag
269 %************************************************************************
275 deriving( Eq, Data, Typeable )
277 isBoxed :: Boxity -> Bool
279 isBoxed Unboxed = False
283 %************************************************************************
285 Recursive/Non-Recursive flag
287 %************************************************************************
290 data RecFlag = Recursive
292 deriving( Eq, Data, Typeable )
294 isRec :: RecFlag -> Bool
295 isRec Recursive = True
296 isRec NonRecursive = False
298 isNonRec :: RecFlag -> Bool
299 isNonRec Recursive = False
300 isNonRec NonRecursive = True
302 boolToRecFlag :: Bool -> RecFlag
303 boolToRecFlag True = Recursive
304 boolToRecFlag False = NonRecursive
306 instance Outputable RecFlag where
307 ppr Recursive = ptext (sLit "Recursive")
308 ppr NonRecursive = ptext (sLit "NonRecursive")
311 %************************************************************************
313 Instance overlap flag
315 %************************************************************************
319 = NoOverlap -- This instance must not overlap another
321 | OverlapOk -- Silently ignore this instance if you find a
322 -- more specific one that matches the constraint
323 -- you are trying to resolve
325 -- Example: constraint (Foo [Int])
326 -- instances (Foo [Int])
327 -- (Foo [a]) OverlapOk
328 -- Since the second instance has the OverlapOk flag,
329 -- the first instance will be chosen (otherwise
330 -- its ambiguous which to choose)
332 | Incoherent -- Like OverlapOk, but also ignore this instance
333 -- if it doesn't match the constraint you are
334 -- trying to resolve, but could match if the type variables
335 -- in the constraint were instantiated
337 -- Example: constraint (Foo [b])
338 -- instances (Foo [Int]) Incoherent
340 -- Without the Incoherent flag, we'd complain that
341 -- instantiating 'b' would change which instance
345 instance Outputable OverlapFlag where
346 ppr NoOverlap = empty
347 ppr OverlapOk = ptext (sLit "[overlap ok]")
348 ppr Incoherent = ptext (sLit "[incoherent]")
352 %************************************************************************
356 %************************************************************************
359 data TupCon = TupCon Boxity Arity
361 instance Eq TupCon where
362 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
364 tupleParens :: Boxity -> SDoc -> SDoc
365 tupleParens Boxed p = parens p
366 tupleParens Unboxed p = ptext (sLit "(#") <+> p <+> ptext (sLit "#)")
369 %************************************************************************
371 \subsection[Generic]{Generic flag}
373 %************************************************************************
375 This is the "Embedding-Projection pair" datatype, it contains
376 two pieces of code (normally either RenamedExpr's or Id's)
377 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
378 represents functions of type
387 T and Tring are arbitrary, but typically T is the 'main' type while
388 Tring is the 'representation' type. (This just helps us remember
389 whether to use 'from' or 'to'.
392 data EP a = EP { fromEP :: a, -- :: T -> Tring
393 toEP :: a } -- :: Tring -> T
396 Embedding-projection pairs are used in several places:
398 First of all, each type constructor has an EP associated with it, the
399 code in EP converts (datatype T) from T to Tring and back again.
401 Secondly, when we are filling in Generic methods (in the typechecker,
402 tcMethodBinds), we are constructing bimaps by induction on the structure
403 of the type of the method signature.
406 %************************************************************************
408 \subsection{Occurrence information}
410 %************************************************************************
412 This data type is used exclusively by the simplifier, but it appears in a
413 SubstResult, which is currently defined in VarEnv, which is pretty near
414 the base of the module hierarchy. So it seemed simpler to put the
415 defn of OccInfo here, safely at the bottom
418 -- | Identifier occurrence information
420 = NoOccInfo -- ^ There are many occurrences, or unknown occurences
422 | IAmDead -- ^ Marks unused variables. Sometimes useful for
423 -- lambda and case-bound variables.
428 !InterestingCxt -- ^ Occurs exactly once, not inside a rule
430 -- | This identifier breaks a loop of mutually recursive functions. The field
431 -- marks whether it is only a loop breaker due to a reference in a rule
432 | IAmALoopBreaker -- Note [LoopBreaker OccInfo]
433 !RulesOnly -- True <=> This is a weak or rules-only loop breaker
434 -- See OccurAnal Note [Weak loop breakers]
436 type RulesOnly = Bool
439 Note [LoopBreaker OccInfo]
440 ~~~~~~~~~~~~~~~~~~~~~~~~~~
441 An OccInfo of (IAmLoopBreaker False) is used by the occurrence
442 analyser in two ways:
443 (a) to mark loop-breakers in a group of recursive
444 definitions (hence the name)
445 (b) to mark binders that must not be inlined in this phase
446 (perhaps it has a NOINLINE pragma)
447 Things with (IAmLoopBreaker False) do not get an unfolding
448 pinned on to them, so they are completely opaque.
450 See OccurAnal Note [Weak loop breakers] for (IAmLoopBreaker True).
454 isNoOcc :: OccInfo -> Bool
455 isNoOcc NoOccInfo = True
458 seqOccInfo :: OccInfo -> ()
459 seqOccInfo occ = occ `seq` ()
462 type InterestingCxt = Bool -- True <=> Function: is applied
463 -- Data value: scrutinised by a case with
464 -- at least one non-DEFAULT branch
467 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
468 -- Substituting a redex for this occurrence is
469 -- dangerous because it might duplicate work.
470 insideLam, notInsideLam :: InsideLam
475 type OneBranch = Bool -- True <=> Occurs in only one case branch
476 -- so no code-duplication issue to worry about
477 oneBranch, notOneBranch :: OneBranch
481 isLoopBreaker :: OccInfo -> Bool
482 isLoopBreaker (IAmALoopBreaker _) = True
483 isLoopBreaker _ = False
485 isNonRuleLoopBreaker :: OccInfo -> Bool
486 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
487 isNonRuleLoopBreaker _ = False
489 nonRuleLoopBreaker :: OccInfo
490 nonRuleLoopBreaker = IAmALoopBreaker False
492 isDeadOcc :: OccInfo -> Bool
493 isDeadOcc IAmDead = True
496 isOneOcc :: OccInfo -> Bool
497 isOneOcc (OneOcc {}) = True
500 zapFragileOcc :: OccInfo -> OccInfo
501 zapFragileOcc (OneOcc {}) = NoOccInfo
502 zapFragileOcc occ = occ
506 instance Outputable OccInfo where
507 -- only used for debugging; never parsed. KSW 1999-07
508 ppr NoOccInfo = empty
509 ppr (IAmALoopBreaker ro) = ptext (sLit "LoopBreaker") <> if ro then char '!' else empty
510 ppr IAmDead = ptext (sLit "Dead")
511 ppr (OneOcc inside_lam one_branch int_cxt)
512 = ptext (sLit "Once") <> pp_lam <> pp_br <> pp_args
514 pp_lam | inside_lam = char 'L'
516 pp_br | one_branch = empty
517 | otherwise = char '*'
518 pp_args | int_cxt = char '!'
521 instance Show OccInfo where
522 showsPrec p occ = showsPrecSDoc p (ppr occ)
525 %************************************************************************
527 Strictness indication
529 %************************************************************************
531 The strictness annotations on types in data type declarations
532 e.g. data T = MkT !Int !(Bool,Bool)
535 -------------------------
536 -- HsBang describes what the *programmer* wrote
537 -- This info is retained in the DataCon.dcStrictMarks field
538 data HsBang = HsNoBang
542 | HsUnpack -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
544 | HsUnpackFailed -- An UNPACK pragma that we could not make
545 -- use of, because the type isn't unboxable;
546 -- equivalant to HsStrict except for checkValidDataCon
547 deriving (Eq, Data, Typeable)
549 instance Outputable HsBang where
551 ppr HsStrict = char '!'
552 ppr HsUnpack = ptext (sLit "{-# UNPACK #-} !")
553 ppr HsUnpackFailed = ptext (sLit "{-# UNPACK (failed) #-} !")
555 isBanged :: HsBang -> Bool
556 isBanged HsNoBang = False
559 isMarkedUnboxed :: HsBang -> Bool
560 isMarkedUnboxed HsUnpack = True
561 isMarkedUnboxed _ = False
563 -------------------------
564 -- StrictnessMark is internal only, used to indicate strictness
565 -- of the DataCon *worker* fields
566 data StrictnessMark = MarkedStrict | NotMarkedStrict
568 instance Outputable StrictnessMark where
569 ppr MarkedStrict = ptext (sLit "!")
570 ppr NotMarkedStrict = empty
572 isMarkedStrict :: StrictnessMark -> Bool
573 isMarkedStrict NotMarkedStrict = False
574 isMarkedStrict _ = True -- All others are strict
578 %************************************************************************
580 Default method specfication
582 %************************************************************************
584 The DefMethSpec enumeration just indicates what sort of default method
585 is used for a class. It is generated from source code, and present in
586 interface files; it is converted to Class.DefMeth before begin put in a
590 data DefMethSpec = NoDM -- No default method
591 | VanillaDM -- Default method given with polymorphic code
592 | GenericDM -- Default method given with generic code
594 instance Outputable DefMethSpec where
596 ppr VanillaDM = ptext (sLit "{- Has default method -}")
597 ppr GenericDM = ptext (sLit "{- Has generic default method -}")
600 %************************************************************************
602 \subsection{Success flag}
604 %************************************************************************
607 data SuccessFlag = Succeeded | Failed
609 instance Outputable SuccessFlag where
610 ppr Succeeded = ptext (sLit "Succeeded")
611 ppr Failed = ptext (sLit "Failed")
613 successIf :: Bool -> SuccessFlag
614 successIf True = Succeeded
615 successIf False = Failed
617 succeeded, failed :: SuccessFlag -> Bool
618 succeeded Succeeded = True
619 succeeded Failed = False
621 failed Succeeded = False
626 %************************************************************************
628 \subsection{Activation}
630 %************************************************************************
632 When a rule or inlining is active
635 type CompilerPhase = Int -- Compilation phase
636 -- Phases decrease towards zero
637 -- Zero is the last phase
639 data Activation = NeverActive
641 | ActiveBefore CompilerPhase -- Active only *before* this phase
642 | ActiveAfter CompilerPhase -- Active in this phase and later
643 deriving( Eq, Data, Typeable ) -- Eq used in comparing rules in HsDecls
645 data RuleMatchInfo = ConLike -- See Note [CONLIKE pragma]
647 deriving( Eq, Data, Typeable )
649 data InlinePragma -- Note [InlinePragma]
651 { inl_inline :: Bool -- True <=> INLINE,
652 -- False <=> no pragma at all, or NOINLINE
654 , inl_sat :: Maybe Arity -- Just n <=> Inline only when applied to n
655 -- explicit (non-type, non-dictionary) args
656 -- That is, inl_sat describes the number of *source-code*
657 -- arguments the thing must be applied to. We add on the
658 -- number of implicit, dictionary arguments when making
659 -- the InlineRule, and don't look at inl_sat further
661 , inl_act :: Activation -- Says during which phases inlining is allowed
663 , inl_rule :: RuleMatchInfo -- Should the function be treated like a constructor?
664 } deriving( Eq, Data, Typeable )
669 This data type mirrors what you can write in an INLINE or NOINLINE pragma in
672 If you write nothing at all, you get defaultInlinePragma:
674 inl_act = AlwaysActive
677 It's not possible to get that combination by *writing* something, so
678 if an Id has defaultInlinePragma it means the user didn't specify anything.
680 If inl_inline = True, then the Id should have an InlineRule unfolding.
682 Note [CONLIKE pragma]
683 ~~~~~~~~~~~~~~~~~~~~~
684 The ConLike constructor of a RuleMatchInfo is aimed at the following.
686 {-# RULE "r/cons" forall a as. r (a:as) = f (a+1) #-}
687 g b bs = let x = b:bs in ..x...x...(r x)...
688 Now, the rule applies to the (r x) term, because GHC "looks through"
689 the definition of 'x' to see that it is (b:bs).
692 {-# RULE "r/f" forall v. r (f v) = f (v+1) #-}
693 g v = let x = f v in ..x...x...(r x)...
694 Normally the (r x) would *not* match the rule, because GHC would be
695 scared about duplicating the redex (f v), so it does not "look
696 through" the bindings.
698 However the CONLIKE modifier says to treat 'f' like a constructor in
699 this situation, and "look through" the unfolding for x. So (r x)
700 fires, yielding (f (v+1)).
702 This is all controlled with a user-visible pragma:
703 {-# NOINLINE CONLIKE [1] f #-}
705 The main effects of CONLIKE are:
707 - The occurrence analyser (OccAnal) and simplifier (Simplify) treat
708 CONLIKE thing like constructors, by ANF-ing them
710 - New function coreUtils.exprIsExpandable is like exprIsCheap, but
711 additionally spots applications of CONLIKE functions
713 - A CoreUnfolding has a field that caches exprIsExpandable
715 - The rule matcher consults this field. See
716 Note [Expanding variables] in Rules.lhs.
719 isConLike :: RuleMatchInfo -> Bool
720 isConLike ConLike = True
723 isFunLike :: RuleMatchInfo -> Bool
724 isFunLike FunLike = True
727 defaultInlinePragma, alwaysInlinePragma, neverInlinePragma, dfunInlinePragma
729 defaultInlinePragma = InlinePragma { inl_act = AlwaysActive
732 , inl_sat = Nothing }
734 alwaysInlinePragma = defaultInlinePragma { inl_inline = True }
735 neverInlinePragma = defaultInlinePragma { inl_act = NeverActive }
737 -- A DFun has an always-active inline activation so that
738 -- exprIsConApp_maybe can "see" its unfolding
739 -- (However, its actual Unfolding is a DFunUnfolding, which is
740 -- never inlined other than via exprIsConApp_maybe.)
741 dfunInlinePragma = defaultInlinePragma { inl_act = AlwaysActive
742 , inl_rule = ConLike }
744 isDefaultInlinePragma :: InlinePragma -> Bool
745 isDefaultInlinePragma (InlinePragma { inl_act = activation
746 , inl_rule = match_info
747 , inl_inline = inline })
748 = not inline && isAlwaysActive activation && isFunLike match_info
750 isInlinePragma :: InlinePragma -> Bool
751 isInlinePragma prag = inl_inline prag
753 inlinePragmaSat :: InlinePragma -> Maybe Arity
754 inlinePragmaSat = inl_sat
756 inlinePragmaActivation :: InlinePragma -> Activation
757 inlinePragmaActivation (InlinePragma { inl_act = activation }) = activation
759 inlinePragmaRuleMatchInfo :: InlinePragma -> RuleMatchInfo
760 inlinePragmaRuleMatchInfo (InlinePragma { inl_rule = info }) = info
762 setInlinePragmaActivation :: InlinePragma -> Activation -> InlinePragma
763 setInlinePragmaActivation prag activation = prag { inl_act = activation }
765 setInlinePragmaRuleMatchInfo :: InlinePragma -> RuleMatchInfo -> InlinePragma
766 setInlinePragmaRuleMatchInfo prag info = prag { inl_rule = info }
768 instance Outputable Activation where
769 ppr AlwaysActive = brackets (ptext (sLit "ALWAYS"))
770 ppr NeverActive = brackets (ptext (sLit "NEVER"))
771 ppr (ActiveBefore n) = brackets (char '~' <> int n)
772 ppr (ActiveAfter n) = brackets (int n)
774 instance Outputable RuleMatchInfo where
775 ppr ConLike = ptext (sLit "CONLIKE")
776 ppr FunLike = ptext (sLit "FUNLIKE")
778 instance Outputable InlinePragma where
779 ppr (InlinePragma { inl_inline = inline, inl_act = activation
780 , inl_rule = info, inl_sat = mb_arity })
781 = pp_inl_act (inline, activation) <+> pp_sat <+> pp_info
783 pp_inl_act (False, AlwaysActive) = empty -- defaultInlinePragma
784 pp_inl_act (False, NeverActive) = ptext (sLit "NOINLINE")
785 pp_inl_act (False, act) = ptext (sLit "NOINLINE") <> ppr act
786 pp_inl_act (True, AlwaysActive) = ptext (sLit "INLINE")
787 pp_inl_act (True, act) = ptext (sLit "INLINE") <> ppr act
789 pp_sat | Just ar <- mb_arity = parens (ptext (sLit "sat-args=") <> int ar)
791 pp_info | isFunLike info = empty
792 | otherwise = ppr info
794 isActive :: CompilerPhase -> Activation -> Bool
795 isActive _ NeverActive = False
796 isActive _ AlwaysActive = True
797 isActive p (ActiveAfter n) = p <= n
798 isActive p (ActiveBefore n) = p > n
800 isNeverActive, isAlwaysActive, isEarlyActive :: Activation -> Bool
801 isNeverActive NeverActive = True
802 isNeverActive _ = False
804 isAlwaysActive AlwaysActive = True
805 isAlwaysActive _ = False
807 isEarlyActive AlwaysActive = True
808 isEarlyActive (ActiveBefore {}) = True
809 isEarlyActive _ = False