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"
61 import FastString( FastString )
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 -- Used by the occurrence analyser to mark loop-breakers
382 -- in a group of recursive definitions
383 !RulesOnly -- True <=> This is a weak or rules-only loop breaker
384 -- See OccurAnal Note [Weak loop breakers]
386 type RulesOnly = Bool
391 isNoOcc :: OccInfo -> Bool
392 isNoOcc NoOccInfo = True
395 seqOccInfo :: OccInfo -> ()
396 seqOccInfo occ = occ `seq` ()
399 type InterestingCxt = Bool -- True <=> Function: is applied
400 -- Data value: scrutinised by a case with
401 -- at least one non-DEFAULT branch
404 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
405 -- Substituting a redex for this occurrence is
406 -- dangerous because it might duplicate work.
407 insideLam, notInsideLam :: InsideLam
412 type OneBranch = Bool -- True <=> Occurs in only one case branch
413 -- so no code-duplication issue to worry about
414 oneBranch, notOneBranch :: OneBranch
418 isLoopBreaker :: OccInfo -> Bool
419 isLoopBreaker (IAmALoopBreaker _) = True
420 isLoopBreaker _ = False
422 isNonRuleLoopBreaker :: OccInfo -> Bool
423 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
424 isNonRuleLoopBreaker _ = False
426 isDeadOcc :: OccInfo -> Bool
427 isDeadOcc IAmDead = True
430 isOneOcc :: OccInfo -> Bool
431 isOneOcc (OneOcc _ _ _) = True
434 isFragileOcc :: OccInfo -> Bool
435 isFragileOcc (OneOcc _ _ _) = True
436 isFragileOcc _ = False
440 instance Outputable OccInfo where
441 -- only used for debugging; never parsed. KSW 1999-07
442 ppr NoOccInfo = empty
443 ppr (IAmALoopBreaker ro) = ptext SLIT("LoopBreaker") <> if ro then char '!' else empty
444 ppr IAmDead = ptext SLIT("Dead")
445 ppr (OneOcc inside_lam one_branch int_cxt)
446 = ptext SLIT("Once") <> pp_lam <> pp_br <> pp_args
448 pp_lam | inside_lam = char 'L'
450 pp_br | one_branch = empty
451 | otherwise = char '*'
452 pp_args | int_cxt = char '!'
455 instance Show OccInfo where
456 showsPrec p occ = showsPrecSDoc p (ppr occ)
459 %************************************************************************
461 \subsection{Strictness indication}
463 %************************************************************************
465 The strictness annotations on types in data type declarations
466 e.g. data T = MkT !Int !(Bool,Bool)
469 data StrictnessMark -- Used in interface decls only
475 isMarkedUnboxed :: StrictnessMark -> Bool
476 isMarkedUnboxed MarkedUnboxed = True
477 isMarkedUnboxed _ = False
479 isMarkedStrict :: StrictnessMark -> Bool
480 isMarkedStrict NotMarkedStrict = False
481 isMarkedStrict _ = True -- All others are strict
483 instance Outputable StrictnessMark where
484 ppr MarkedStrict = ptext SLIT("!")
485 ppr MarkedUnboxed = ptext SLIT("!!")
486 ppr NotMarkedStrict = ptext SLIT("_")
490 %************************************************************************
492 \subsection{Success flag}
494 %************************************************************************
497 data SuccessFlag = Succeeded | Failed
499 instance Outputable SuccessFlag where
500 ppr Succeeded = ptext SLIT("Succeeded")
501 ppr Failed = ptext SLIT("Failed")
503 successIf :: Bool -> SuccessFlag
504 successIf True = Succeeded
505 successIf False = Failed
507 succeeded, failed :: SuccessFlag -> Bool
508 succeeded Succeeded = True
509 succeeded Failed = False
511 failed Succeeded = False
516 %************************************************************************
518 \subsection{Activation}
520 %************************************************************************
522 When a rule or inlining is active
525 type CompilerPhase = Int -- Compilation phase
526 -- Phases decrease towards zero
527 -- Zero is the last phase
529 data Activation = NeverActive
531 | ActiveBefore CompilerPhase -- Active only *before* this phase
532 | ActiveAfter CompilerPhase -- Active in this phase and later
533 deriving( Eq ) -- Eq used in comparing rules in HsDecls
537 Activation -- Says during which phases inlining is allowed
538 Bool -- True <=> make the RHS look small, so that when inlining
539 -- is enabled, it will definitely actually happen
542 defaultInlineSpec, alwaysInlineSpec, neverInlineSpec :: InlineSpec
544 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
545 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
546 neverInlineSpec = Inline NeverActive False -- NOINLINE
548 instance Outputable Activation where
549 ppr NeverActive = ptext SLIT("NEVER")
550 ppr AlwaysActive = ptext SLIT("ALWAYS")
551 ppr (ActiveBefore n) = brackets (char '~' <> int n)
552 ppr (ActiveAfter n) = brackets (int n)
554 instance Outputable InlineSpec where
555 ppr (Inline act is_inline)
556 | is_inline = ptext SLIT("INLINE")
558 AlwaysActive -> empty
560 | otherwise = ptext SLIT("NOINLINE")
565 isActive :: CompilerPhase -> Activation -> Bool
566 isActive _ NeverActive = False
567 isActive _ AlwaysActive = True
568 isActive p (ActiveAfter n) = p <= n
569 isActive p (ActiveBefore n) = p > n
571 isNeverActive, isAlwaysActive :: Activation -> Bool
572 isNeverActive NeverActive = True
573 isNeverActive _ = False
575 isAlwaysActive AlwaysActive = True
576 isAlwaysActive _ = False