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
18 Version, bumpVersion, initialVersion,
24 Fixity(..), FixityDirection(..),
25 defaultFixity, maxPrecedence,
26 negateFixity, funTyFixity,
29 IPName(..), ipNameName, mapIPName,
31 RecFlag(..), isRec, isNonRec, boolToRecFlag,
33 TopLevelFlag(..), isTopLevel, isNotTopLevel,
39 TupCon(..), tupleParens,
41 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
42 isDeadOcc, isLoopBreaker, isNonRuleLoopBreaker, isNoOcc,
44 InsideLam, insideLam, notInsideLam,
45 OneBranch, oneBranch, notOneBranch,
50 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
53 Activation(..), isActive, isNeverActive, isAlwaysActive,
54 InlineSpec(..), defaultInlineSpec, alwaysInlineSpec, neverInlineSpec,
56 SuccessFlag(..), succeeded, failed, successIf
59 #include "HsVersions.h"
65 %************************************************************************
67 \subsection[Arity]{Arity}
69 %************************************************************************
76 %************************************************************************
78 \subsection[Version]{Module and identifier version numbers}
80 %************************************************************************
85 bumpVersion :: Version -> Version
88 initialVersion :: Version
92 %************************************************************************
96 %************************************************************************
100 type DeprecTxt = FastString -- reason/explanation for deprecation
103 %************************************************************************
105 \subsection{Implicit parameter identity}
107 %************************************************************************
109 The @IPName@ type is here because it is used in TypeRep (i.e. very
110 early in the hierarchy), but also in HsSyn.
113 newtype IPName name = IPName name -- ?x
114 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
115 -- (used in HscTypes.OrigIParamCache)
117 ipNameName :: IPName name -> name
118 ipNameName (IPName n) = n
120 mapIPName :: (a->b) -> IPName a -> IPName b
121 mapIPName f (IPName n) = IPName (f n)
123 instance Outputable name => Outputable (IPName name) where
124 ppr (IPName n) = char '?' <> ppr n -- Ordinary implicit parameters
128 %************************************************************************
130 \subsection[Fixity]{Fixity info}
132 %************************************************************************
135 ------------------------
136 data Fixity = Fixity Int FixityDirection
138 instance Outputable Fixity where
139 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
141 instance Eq Fixity where -- Used to determine if two fixities conflict
142 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
144 ------------------------
145 data FixityDirection = InfixL | InfixR | InfixN
148 instance Outputable FixityDirection where
149 ppr InfixL = ptext SLIT("infixl")
150 ppr InfixR = ptext SLIT("infixr")
151 ppr InfixN = ptext SLIT("infix")
153 ------------------------
156 defaultFixity :: Fixity
157 defaultFixity = Fixity maxPrecedence InfixL
159 negateFixity, funTyFixity :: Fixity
161 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
162 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
170 @(compareFixity op1 op2)@ tells which way to arrange appication, or
171 whether there's an error.
174 compareFixity :: Fixity -> Fixity
175 -> (Bool, -- Error please
176 Bool) -- Associate to the right: a op1 (b op2 c)
177 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
178 = case prec1 `compare` prec2 of
181 EQ -> case (dir1, dir2) of
182 (InfixR, InfixR) -> right
183 (InfixL, InfixL) -> left
186 right = (False, True)
187 left = (False, False)
188 error_please = (True, False)
192 %************************************************************************
194 \subsection[Top-level/local]{Top-level/not-top level flag}
196 %************************************************************************
203 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
205 isNotTopLevel NotTopLevel = True
206 isNotTopLevel TopLevel = False
208 isTopLevel TopLevel = True
209 isTopLevel NotTopLevel = False
211 instance Outputable TopLevelFlag where
212 ppr TopLevel = ptext SLIT("<TopLevel>")
213 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
217 %************************************************************************
219 Top-level/not-top level flag
221 %************************************************************************
229 isBoxed :: Boxity -> Bool
231 isBoxed Unboxed = False
235 %************************************************************************
237 Recursive/Non-Recursive flag
239 %************************************************************************
242 data RecFlag = Recursive
246 isRec :: RecFlag -> Bool
247 isRec Recursive = True
248 isRec NonRecursive = False
250 isNonRec :: RecFlag -> Bool
251 isNonRec Recursive = False
252 isNonRec NonRecursive = True
254 boolToRecFlag :: Bool -> RecFlag
255 boolToRecFlag True = Recursive
256 boolToRecFlag False = NonRecursive
258 instance Outputable RecFlag where
259 ppr Recursive = ptext SLIT("Recursive")
260 ppr NonRecursive = ptext SLIT("NonRecursive")
263 %************************************************************************
265 Instance overlap flag
267 %************************************************************************
271 = NoOverlap -- This instance must not overlap another
273 | OverlapOk -- Silently ignore this instance if you find a
274 -- more specific one that matches the constraint
275 -- you are trying to resolve
277 -- Example: constraint (Foo [Int])
278 -- instances (Foo [Int])
279 -- (Foo [a]) OverlapOk
280 -- Since the second instance has the OverlapOk flag,
281 -- the first instance will be chosen (otherwise
282 -- its ambiguous which to choose)
284 | Incoherent -- Like OverlapOk, but also ignore this instance
285 -- if it doesn't match the constraint you are
286 -- trying to resolve, but could match if the type variables
287 -- in the constraint were instantiated
289 -- Example: constraint (Foo [b])
290 -- instances (Foo [Int]) Incoherent
292 -- Without the Incoherent flag, we'd complain that
293 -- instantiating 'b' would change which instance
297 instance Outputable OverlapFlag where
298 ppr NoOverlap = empty
299 ppr OverlapOk = ptext SLIT("[overlap ok]")
300 ppr Incoherent = ptext SLIT("[incoherent]")
304 %************************************************************************
308 %************************************************************************
311 data TupCon = TupCon Boxity Arity
313 instance Eq TupCon where
314 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
316 tupleParens :: Boxity -> SDoc -> SDoc
317 tupleParens Boxed p = parens p
318 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
321 %************************************************************************
323 \subsection[Generic]{Generic flag}
325 %************************************************************************
327 This is the "Embedding-Projection pair" datatype, it contains
328 two pieces of code (normally either RenamedExpr's or Id's)
329 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
330 represents functions of type
339 T and Tring are arbitrary, but typically T is the 'main' type while
340 Tring is the 'representation' type. (This just helps us remember
341 whether to use 'from' or 'to'.
344 data EP a = EP { fromEP :: a, -- :: T -> Tring
345 toEP :: a } -- :: Tring -> T
348 Embedding-projection pairs are used in several places:
350 First of all, each type constructor has an EP associated with it, the
351 code in EP converts (datatype T) from T to Tring and back again.
353 Secondly, when we are filling in Generic methods (in the typechecker,
354 tcMethodBinds), we are constructing bimaps by induction on the structure
355 of the type of the method signature.
358 %************************************************************************
360 \subsection{Occurrence information}
362 %************************************************************************
364 This data type is used exclusively by the simplifier, but it appears in a
365 SubstResult, which is currently defined in VarEnv, which is pretty near
366 the base of the module hierarchy. So it seemed simpler to put the
367 defn of OccInfo here, safely at the bottom
371 = NoOccInfo -- Many occurrences, or unknown
373 | IAmDead -- Marks unused variables. Sometimes useful for
374 -- lambda and case-bound variables.
376 | OneOcc -- Occurs exactly once, not inside a rule
381 | IAmALoopBreaker -- Note [LoopBreaker OccInfo]
382 !RulesOnly -- True <=> This is a weak or rules-only loop breaker
383 -- See OccurAnal Note [Weak loop breakers]
385 type RulesOnly = Bool
388 Note [LoopBreaker OccInfo]
389 ~~~~~~~~~~~~~~~~~~~~~~~~~~
390 An OccInfo of (IAmLoopBreaker False) is used by the occurrence
391 analyser in two ways:
392 (a) to mark loop-breakers in a group of recursive
393 definitions (hence the name)
394 (b) to mark binders that must not be inlined in this phase
395 (perhaps it has a NOINLINE pragma)
396 Things with (IAmLoopBreaker False) do not get an unfolding
397 pinned on to them, so they are completely opaque.
399 See OccurAnal Note [Weak loop breakers] for (IAmLoopBreaker True).
403 isNoOcc :: OccInfo -> Bool
404 isNoOcc NoOccInfo = True
407 seqOccInfo :: OccInfo -> ()
408 seqOccInfo occ = occ `seq` ()
411 type InterestingCxt = Bool -- True <=> Function: is applied
412 -- Data value: scrutinised by a case with
413 -- at least one non-DEFAULT branch
416 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
417 -- Substituting a redex for this occurrence is
418 -- dangerous because it might duplicate work.
419 insideLam, notInsideLam :: InsideLam
424 type OneBranch = Bool -- True <=> Occurs in only one case branch
425 -- so no code-duplication issue to worry about
426 oneBranch, notOneBranch :: OneBranch
430 isLoopBreaker :: OccInfo -> Bool
431 isLoopBreaker (IAmALoopBreaker _) = True
432 isLoopBreaker _ = False
434 isNonRuleLoopBreaker :: OccInfo -> Bool
435 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
436 isNonRuleLoopBreaker _ = False
438 isDeadOcc :: OccInfo -> Bool
439 isDeadOcc IAmDead = True
442 isOneOcc :: OccInfo -> Bool
443 isOneOcc (OneOcc _ _ _) = True
446 isFragileOcc :: OccInfo -> Bool
447 isFragileOcc (OneOcc _ _ _) = True
448 isFragileOcc _ = False
452 instance Outputable OccInfo where
453 -- only used for debugging; never parsed. KSW 1999-07
454 ppr NoOccInfo = empty
455 ppr (IAmALoopBreaker ro) = ptext SLIT("LoopBreaker") <> if ro then char '!' else empty
456 ppr IAmDead = ptext SLIT("Dead")
457 ppr (OneOcc inside_lam one_branch int_cxt)
458 = ptext SLIT("Once") <> pp_lam <> pp_br <> pp_args
460 pp_lam | inside_lam = char 'L'
462 pp_br | one_branch = empty
463 | otherwise = char '*'
464 pp_args | int_cxt = char '!'
467 instance Show OccInfo where
468 showsPrec p occ = showsPrecSDoc p (ppr occ)
471 %************************************************************************
473 \subsection{Strictness indication}
475 %************************************************************************
477 The strictness annotations on types in data type declarations
478 e.g. data T = MkT !Int !(Bool,Bool)
481 data StrictnessMark -- Used in interface decls only
487 isMarkedUnboxed :: StrictnessMark -> Bool
488 isMarkedUnboxed MarkedUnboxed = True
489 isMarkedUnboxed _ = False
491 isMarkedStrict :: StrictnessMark -> Bool
492 isMarkedStrict NotMarkedStrict = False
493 isMarkedStrict _ = True -- All others are strict
495 instance Outputable StrictnessMark where
496 ppr MarkedStrict = ptext SLIT("!")
497 ppr MarkedUnboxed = ptext SLIT("!!")
498 ppr NotMarkedStrict = ptext SLIT("_")
502 %************************************************************************
504 \subsection{Success flag}
506 %************************************************************************
509 data SuccessFlag = Succeeded | Failed
511 instance Outputable SuccessFlag where
512 ppr Succeeded = ptext SLIT("Succeeded")
513 ppr Failed = ptext SLIT("Failed")
515 successIf :: Bool -> SuccessFlag
516 successIf True = Succeeded
517 successIf False = Failed
519 succeeded, failed :: SuccessFlag -> Bool
520 succeeded Succeeded = True
521 succeeded Failed = False
523 failed Succeeded = False
528 %************************************************************************
530 \subsection{Activation}
532 %************************************************************************
534 When a rule or inlining is active
537 type CompilerPhase = Int -- Compilation phase
538 -- Phases decrease towards zero
539 -- Zero is the last phase
541 data Activation = NeverActive
543 | ActiveBefore CompilerPhase -- Active only *before* this phase
544 | ActiveAfter CompilerPhase -- Active in this phase and later
545 deriving( Eq ) -- Eq used in comparing rules in HsDecls
549 Activation -- Says during which phases inlining is allowed
550 Bool -- True <=> make the RHS look small, so that when inlining
551 -- is enabled, it will definitely actually happen
554 defaultInlineSpec, alwaysInlineSpec, neverInlineSpec :: InlineSpec
556 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
557 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
558 neverInlineSpec = Inline NeverActive False -- NOINLINE
560 instance Outputable Activation where
561 ppr NeverActive = ptext SLIT("NEVER")
562 ppr AlwaysActive = ptext SLIT("ALWAYS")
563 ppr (ActiveBefore n) = brackets (char '~' <> int n)
564 ppr (ActiveAfter n) = brackets (int n)
566 instance Outputable InlineSpec where
567 ppr (Inline act is_inline)
568 | is_inline = ptext SLIT("INLINE")
570 AlwaysActive -> empty
572 | otherwise = ptext SLIT("NOINLINE")
577 isActive :: CompilerPhase -> Activation -> Bool
578 isActive _ NeverActive = False
579 isActive _ AlwaysActive = True
580 isActive p (ActiveAfter n) = p <= n
581 isActive p (ActiveBefore n) = p > n
583 isNeverActive, isAlwaysActive :: Activation -> Bool
584 isNeverActive NeverActive = True
585 isNeverActive _ = False
587 isAlwaysActive AlwaysActive = True
588 isAlwaysActive _ = False