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 ------------------------
154 maxPrecedence = (9::Int)
155 defaultFixity = Fixity maxPrecedence InfixL
157 negateFixity, funTyFixity :: Fixity
159 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
160 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
168 @(compareFixity op1 op2)@ tells which way to arrange appication, or
169 whether there's an error.
172 compareFixity :: Fixity -> Fixity
173 -> (Bool, -- Error please
174 Bool) -- Associate to the right: a op1 (b op2 c)
175 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
176 = case prec1 `compare` prec2 of
179 EQ -> case (dir1, dir2) of
180 (InfixR, InfixR) -> right
181 (InfixL, InfixL) -> left
184 right = (False, True)
185 left = (False, False)
186 error_please = (True, False)
190 %************************************************************************
192 \subsection[Top-level/local]{Top-level/not-top level flag}
194 %************************************************************************
201 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
203 isNotTopLevel NotTopLevel = True
204 isNotTopLevel TopLevel = False
206 isTopLevel TopLevel = True
207 isTopLevel NotTopLevel = False
209 instance Outputable TopLevelFlag where
210 ppr TopLevel = ptext SLIT("<TopLevel>")
211 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
215 %************************************************************************
217 Top-level/not-top level flag
219 %************************************************************************
227 isBoxed :: Boxity -> Bool
229 isBoxed Unboxed = False
233 %************************************************************************
235 Recursive/Non-Recursive flag
237 %************************************************************************
240 data RecFlag = Recursive
244 isRec :: RecFlag -> Bool
245 isRec Recursive = True
246 isRec NonRecursive = False
248 isNonRec :: RecFlag -> Bool
249 isNonRec Recursive = False
250 isNonRec NonRecursive = True
252 boolToRecFlag :: Bool -> RecFlag
253 boolToRecFlag True = Recursive
254 boolToRecFlag False = NonRecursive
256 instance Outputable RecFlag where
257 ppr Recursive = ptext SLIT("Recursive")
258 ppr NonRecursive = ptext SLIT("NonRecursive")
261 %************************************************************************
263 Instance overlap flag
265 %************************************************************************
269 = NoOverlap -- This instance must not overlap another
271 | OverlapOk -- Silently ignore this instance if you find a
272 -- more specific one that matches the constraint
273 -- you are trying to resolve
275 -- Example: constraint (Foo [Int])
276 -- instances (Foo [Int])
277 -- (Foo [a]) OverlapOk
278 -- Since the second instance has the OverlapOk flag,
279 -- the first instance will be chosen (otherwise
280 -- its ambiguous which to choose)
282 | Incoherent -- Like OverlapOk, but also ignore this instance
283 -- if it doesn't match the constraint you are
284 -- trying to resolve, but could match if the type variables
285 -- in the constraint were instantiated
287 -- Example: constraint (Foo [b])
288 -- instances (Foo [Int]) Incoherent
290 -- Without the Incoherent flag, we'd complain that
291 -- instantiating 'b' would change which instance
295 instance Outputable OverlapFlag where
296 ppr NoOverlap = empty
297 ppr OverlapOk = ptext SLIT("[overlap ok]")
298 ppr Incoherent = ptext SLIT("[incoherent]")
302 %************************************************************************
306 %************************************************************************
309 data TupCon = TupCon Boxity Arity
311 instance Eq TupCon where
312 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
314 tupleParens :: Boxity -> SDoc -> SDoc
315 tupleParens Boxed p = parens p
316 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
319 %************************************************************************
321 \subsection[Generic]{Generic flag}
323 %************************************************************************
325 This is the "Embedding-Projection pair" datatype, it contains
326 two pieces of code (normally either RenamedExpr's or Id's)
327 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
328 represents functions of type
337 T and Tring are arbitrary, but typically T is the 'main' type while
338 Tring is the 'representation' type. (This just helps us remember
339 whether to use 'from' or 'to'.
342 data EP a = EP { fromEP :: a, -- :: T -> Tring
343 toEP :: a } -- :: Tring -> T
346 Embedding-projection pairs are used in several places:
348 First of all, each type constructor has an EP associated with it, the
349 code in EP converts (datatype T) from T to Tring and back again.
351 Secondly, when we are filling in Generic methods (in the typechecker,
352 tcMethodBinds), we are constructing bimaps by induction on the structure
353 of the type of the method signature.
356 %************************************************************************
358 \subsection{Occurrence information}
360 %************************************************************************
362 This data type is used exclusively by the simplifier, but it appears in a
363 SubstResult, which is currently defined in VarEnv, which is pretty near
364 the base of the module hierarchy. So it seemed simpler to put the
365 defn of OccInfo here, safely at the bottom
369 = NoOccInfo -- Many occurrences, or unknown
371 | IAmDead -- Marks unused variables. Sometimes useful for
372 -- lambda and case-bound variables.
374 | OneOcc -- Occurs exactly once, not inside a rule
379 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
380 -- in a group of recursive definitions
381 !RulesOnly -- True <=> This loop breaker mentions the other binders
382 -- in its recursive group only in its RULES, not
384 -- See OccurAnal Note [RulesOnly]
386 type RulesOnly = Bool
391 isNoOcc :: OccInfo -> Bool
392 isNoOcc NoOccInfo = True
393 isNoOcc other = False
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.
411 type OneBranch = Bool -- True <=> Occurs in only one case branch
412 -- so no code-duplication issue to worry about
416 isLoopBreaker :: OccInfo -> Bool
417 isLoopBreaker (IAmALoopBreaker _) = True
418 isLoopBreaker other = False
420 isNonRuleLoopBreaker :: OccInfo -> Bool
421 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
422 isNonRuleLoopBreaker other = False
424 isDeadOcc :: OccInfo -> Bool
425 isDeadOcc IAmDead = True
426 isDeadOcc other = False
428 isOneOcc (OneOcc _ _ _) = True
429 isOneOcc other = False
431 isFragileOcc :: OccInfo -> Bool
432 isFragileOcc (OneOcc _ _ _) = True
433 isFragileOcc other = False
437 instance Outputable OccInfo where
438 -- only used for debugging; never parsed. KSW 1999-07
439 ppr NoOccInfo = empty
440 ppr (IAmALoopBreaker ro) = ptext SLIT("LoopBreaker") <> if ro then char '!' else empty
441 ppr IAmDead = ptext SLIT("Dead")
442 ppr (OneOcc inside_lam one_branch int_cxt)
443 = ptext SLIT("Once") <> pp_lam <> pp_br <> pp_args
445 pp_lam | inside_lam = char 'L'
447 pp_br | one_branch = empty
448 | otherwise = char '*'
449 pp_args | int_cxt = char '!'
452 instance Show OccInfo where
453 showsPrec p occ = showsPrecSDoc p (ppr occ)
456 %************************************************************************
458 \subsection{Strictness indication}
460 %************************************************************************
462 The strictness annotations on types in data type declarations
463 e.g. data T = MkT !Int !(Bool,Bool)
466 data StrictnessMark -- Used in interface decls only
472 isMarkedUnboxed MarkedUnboxed = True
473 isMarkedUnboxed other = False
475 isMarkedStrict NotMarkedStrict = False
476 isMarkedStrict other = True -- All others are strict
478 instance Outputable StrictnessMark where
479 ppr MarkedStrict = ptext SLIT("!")
480 ppr MarkedUnboxed = ptext SLIT("!!")
481 ppr NotMarkedStrict = ptext SLIT("_")
485 %************************************************************************
487 \subsection{Success flag}
489 %************************************************************************
492 data SuccessFlag = Succeeded | Failed
494 instance Outputable SuccessFlag where
495 ppr Succeeded = ptext SLIT("Succeeded")
496 ppr Failed = ptext SLIT("Failed")
498 successIf :: Bool -> SuccessFlag
499 successIf True = Succeeded
500 successIf False = Failed
502 succeeded, failed :: SuccessFlag -> Bool
503 succeeded Succeeded = True
504 succeeded Failed = False
506 failed Succeeded = False
511 %************************************************************************
513 \subsection{Activation}
515 %************************************************************************
517 When a rule or inlining is active
520 type CompilerPhase = Int -- Compilation phase
521 -- Phases decrease towards zero
522 -- Zero is the last phase
524 data Activation = NeverActive
526 | ActiveBefore CompilerPhase -- Active only *before* this phase
527 | ActiveAfter CompilerPhase -- Active in this phase and later
528 deriving( Eq ) -- Eq used in comparing rules in HsDecls
532 Activation -- Says during which phases inlining is allowed
533 Bool -- True <=> make the RHS look small, so that when inlining
534 -- is enabled, it will definitely actually happen
537 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
538 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
539 neverInlineSpec = Inline NeverActive False -- NOINLINE
541 instance Outputable Activation where
542 ppr AlwaysActive = empty -- The default
543 ppr (ActiveBefore n) = brackets (char '~' <> int n)
544 ppr (ActiveAfter n) = brackets (int n)
545 ppr NeverActive = ptext SLIT("NEVER")
547 instance Outputable InlineSpec where
548 ppr (Inline act True) = ptext SLIT("INLINE") <> ppr act
549 ppr (Inline act False) = ptext SLIT("NOINLINE") <> ppr act
551 isActive :: CompilerPhase -> Activation -> Bool
552 isActive p NeverActive = False
553 isActive p AlwaysActive = True
554 isActive p (ActiveAfter n) = p <= n
555 isActive p (ActiveBefore n) = p > n
557 isNeverActive, isAlwaysActive :: Activation -> Bool
558 isNeverActive NeverActive = True
559 isNeverActive act = False
561 isAlwaysActive AlwaysActive = True
562 isAlwaysActive other = False