2 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
4 \section[BasicTypes]{Miscellanous types}
6 This module defines a miscellaneously collection of very simple
10 \item have no other obvious home
11 \item don't depend on any other complicated types
12 \item are used in more than one "part" of the compiler
17 Version, bumpVersion, initialVersion,
23 Fixity(..), FixityDirection(..),
24 defaultFixity, maxPrecedence,
25 negateFixity, funTyFixity,
28 IPName(..), ipNameName, mapIPName,
30 RecFlag(..), isRec, isNonRec, boolToRecFlag,
32 TopLevelFlag(..), isTopLevel, isNotTopLevel,
38 TupCon(..), tupleParens,
40 OccInfo(..), seqOccInfo, isFragileOcc, isOneOcc,
41 isDeadOcc, isLoopBreaker, isNonRuleLoopBreaker, isNoOcc,
43 InsideLam, insideLam, notInsideLam,
44 OneBranch, oneBranch, notOneBranch,
49 StrictnessMark(..), isMarkedUnboxed, isMarkedStrict,
52 Activation(..), isActive, isNeverActive, isAlwaysActive,
53 InlineSpec(..), defaultInlineSpec, alwaysInlineSpec, neverInlineSpec,
55 SuccessFlag(..), succeeded, failed, successIf
58 #include "HsVersions.h"
60 import FastString( FastString )
64 %************************************************************************
66 \subsection[Arity]{Arity}
68 %************************************************************************
75 %************************************************************************
77 \subsection[Version]{Module and identifier version numbers}
79 %************************************************************************
84 bumpVersion :: Version -> Version
87 initialVersion :: Version
91 %************************************************************************
95 %************************************************************************
99 type DeprecTxt = FastString -- reason/explanation for deprecation
102 %************************************************************************
104 \subsection{Implicit parameter identity}
106 %************************************************************************
108 The @IPName@ type is here because it is used in TypeRep (i.e. very
109 early in the hierarchy), but also in HsSyn.
112 newtype IPName name = IPName name -- ?x
113 deriving( Eq, Ord ) -- Ord is used in the IP name cache finite map
114 -- (used in HscTypes.OrigIParamCache)
116 ipNameName :: IPName name -> name
117 ipNameName (IPName n) = n
119 mapIPName :: (a->b) -> IPName a -> IPName b
120 mapIPName f (IPName n) = IPName (f n)
122 instance Outputable name => Outputable (IPName name) where
123 ppr (IPName n) = char '?' <> ppr n -- Ordinary implicit parameters
127 %************************************************************************
129 \subsection[Fixity]{Fixity info}
131 %************************************************************************
134 ------------------------
135 data Fixity = Fixity Int FixityDirection
137 instance Outputable Fixity where
138 ppr (Fixity prec dir) = hcat [ppr dir, space, int prec]
140 instance Eq Fixity where -- Used to determine if two fixities conflict
141 (Fixity p1 dir1) == (Fixity p2 dir2) = p1==p2 && dir1 == dir2
143 ------------------------
144 data FixityDirection = InfixL | InfixR | InfixN
147 instance Outputable FixityDirection where
148 ppr InfixL = ptext SLIT("infixl")
149 ppr InfixR = ptext SLIT("infixr")
150 ppr InfixN = ptext SLIT("infix")
152 ------------------------
153 maxPrecedence = (9::Int)
154 defaultFixity = Fixity maxPrecedence InfixL
156 negateFixity, funTyFixity :: Fixity
158 negateFixity = Fixity 6 InfixL -- Fixity of unary negate
159 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
167 @(compareFixity op1 op2)@ tells which way to arrange appication, or
168 whether there's an error.
171 compareFixity :: Fixity -> Fixity
172 -> (Bool, -- Error please
173 Bool) -- Associate to the right: a op1 (b op2 c)
174 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
175 = case prec1 `compare` prec2 of
178 EQ -> case (dir1, dir2) of
179 (InfixR, InfixR) -> right
180 (InfixL, InfixL) -> left
183 right = (False, True)
184 left = (False, False)
185 error_please = (True, False)
189 %************************************************************************
191 \subsection[Top-level/local]{Top-level/not-top level flag}
193 %************************************************************************
200 isTopLevel, isNotTopLevel :: TopLevelFlag -> Bool
202 isNotTopLevel NotTopLevel = True
203 isNotTopLevel TopLevel = False
205 isTopLevel TopLevel = True
206 isTopLevel NotTopLevel = False
208 instance Outputable TopLevelFlag where
209 ppr TopLevel = ptext SLIT("<TopLevel>")
210 ppr NotTopLevel = ptext SLIT("<NotTopLevel>")
214 %************************************************************************
216 Top-level/not-top level flag
218 %************************************************************************
226 isBoxed :: Boxity -> Bool
228 isBoxed Unboxed = False
232 %************************************************************************
234 Recursive/Non-Recursive flag
236 %************************************************************************
239 data RecFlag = Recursive
243 isRec :: RecFlag -> Bool
244 isRec Recursive = True
245 isRec NonRecursive = False
247 isNonRec :: RecFlag -> Bool
248 isNonRec Recursive = False
249 isNonRec NonRecursive = True
251 boolToRecFlag :: Bool -> RecFlag
252 boolToRecFlag True = Recursive
253 boolToRecFlag False = NonRecursive
255 instance Outputable RecFlag where
256 ppr Recursive = ptext SLIT("Recursive")
257 ppr NonRecursive = ptext SLIT("NonRecursive")
260 %************************************************************************
262 Instance overlap flag
264 %************************************************************************
268 = NoOverlap -- This instance must not overlap another
270 | OverlapOk -- Silently ignore this instance if you find a
271 -- more specific one that matches the constraint
272 -- you are trying to resolve
274 -- Example: constraint (Foo [Int])
275 -- instances (Foo [Int])
276 -- (Foo [a]) OverlapOk
277 -- Since the second instance has the OverlapOk flag,
278 -- the first instance will be chosen (otherwise
279 -- its ambiguous which to choose)
281 | Incoherent -- Like OverlapOk, but also ignore this instance
282 -- if it doesn't match the constraint you are
283 -- trying to resolve, but could match if the type variables
284 -- in the constraint were instantiated
286 -- Example: constraint (Foo [b])
287 -- instances (Foo [Int]) Incoherent
289 -- Without the Incoherent flag, we'd complain that
290 -- instantiating 'b' would change which instance
294 instance Outputable OverlapFlag where
295 ppr NoOverlap = empty
296 ppr OverlapOk = ptext SLIT("[overlap ok]")
297 ppr Incoherent = ptext SLIT("[incoherent]")
301 %************************************************************************
305 %************************************************************************
308 data TupCon = TupCon Boxity Arity
310 instance Eq TupCon where
311 (TupCon b1 a1) == (TupCon b2 a2) = b1==b2 && a1==a2
313 tupleParens :: Boxity -> SDoc -> SDoc
314 tupleParens Boxed p = parens p
315 tupleParens Unboxed p = ptext SLIT("(#") <+> p <+> ptext SLIT("#)")
318 %************************************************************************
320 \subsection[Generic]{Generic flag}
322 %************************************************************************
324 This is the "Embedding-Projection pair" datatype, it contains
325 two pieces of code (normally either RenamedExpr's or Id's)
326 If we have a such a pair (EP from to), the idea is that 'from' and 'to'
327 represents functions of type
336 T and Tring are arbitrary, but typically T is the 'main' type while
337 Tring is the 'representation' type. (This just helps us remember
338 whether to use 'from' or 'to'.
341 data EP a = EP { fromEP :: a, -- :: T -> Tring
342 toEP :: a } -- :: Tring -> T
345 Embedding-projection pairs are used in several places:
347 First of all, each type constructor has an EP associated with it, the
348 code in EP converts (datatype T) from T to Tring and back again.
350 Secondly, when we are filling in Generic methods (in the typechecker,
351 tcMethodBinds), we are constructing bimaps by induction on the structure
352 of the type of the method signature.
355 %************************************************************************
357 \subsection{Occurrence information}
359 %************************************************************************
361 This data type is used exclusively by the simplifier, but it appears in a
362 SubstResult, which is currently defined in VarEnv, which is pretty near
363 the base of the module hierarchy. So it seemed simpler to put the
364 defn of OccInfo here, safely at the bottom
368 = NoOccInfo -- Many occurrences, or unknown
370 | RulesOnly -- Occurs only in the RHS of one or more rules
372 | IAmDead -- Marks unused variables. Sometimes useful for
373 -- lambda and case-bound variables.
375 | OneOcc -- Occurs exactly once, not inside a rule
380 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
381 -- in a group of recursive definitions
382 !Bool -- True <=> This loop breaker occurs only the RHS of a RULE
387 The RulesOnly constructor records if an Id occurs only in the RHS of a Rule.
388 Similarly, the boolean in IAmLoopbreaker True if the only reason the Id is a
389 loop-breaker only because of recursion through a RULE. In that case,
390 we can ignore the loop-breaker-ness for inlining purposes. Example
393 eftInt :: Int# -> Int# -> [Int]
394 eftInt x y = ...(non-recursive)...
396 {-# INLINE [0] eftIntFB #-}
397 eftIntFB :: (Int -> r -> r) -> r -> Int# -> Int# -> r
398 eftIntFB c n x y = ...(non-recursive)...
401 "eftInt" [~1] forall x y. eftInt x y = build (\ c n -> eftIntFB c n x y)
402 "eftIntList" [1] eftIntFB (:) [] = eftInt
405 The two look mutually recursive only because of their RULES;
406 we don't want that to inhibit inlining!
409 isNoOcc :: OccInfo -> Bool
410 isNoOcc NoOccInfo = True
411 isNoOcc other = False
413 seqOccInfo :: OccInfo -> ()
414 seqOccInfo occ = occ `seq` ()
417 type InterestingCxt = Bool -- True <=> Function: is applied
418 -- Data value: scrutinised by a case with
419 -- at least one non-DEFAULT branch
422 type InsideLam = Bool -- True <=> Occurs inside a non-linear lambda
423 -- Substituting a redex for this occurrence is
424 -- dangerous because it might duplicate work.
429 type OneBranch = Bool -- True <=> Occurs in only one case branch
430 -- so no code-duplication issue to worry about
434 isLoopBreaker :: OccInfo -> Bool
435 isLoopBreaker (IAmALoopBreaker _) = True
436 isLoopBreaker other = False
438 isNonRuleLoopBreaker :: OccInfo -> Bool
439 isNonRuleLoopBreaker (IAmALoopBreaker False) = True -- Loop-breaker that breaks a non-rule cycle
440 isNonRuleLoopBreaker other = False
442 isDeadOcc :: OccInfo -> Bool
443 isDeadOcc IAmDead = True
444 isDeadOcc other = False
446 isOneOcc (OneOcc _ _ _) = True
447 isOneOcc other = False
449 isFragileOcc :: OccInfo -> Bool
450 isFragileOcc (OneOcc _ _ _) = True
451 isFragileOcc other = False
455 instance Outputable OccInfo where
456 -- only used for debugging; never parsed. KSW 1999-07
457 ppr NoOccInfo = empty
458 ppr RulesOnly = ptext SLIT("RulesOnly")
459 ppr (IAmALoopBreaker ro) = ptext SLIT("LoopBreaker") <> if ro then char '!' else empty
460 ppr IAmDead = ptext SLIT("Dead")
461 ppr (OneOcc inside_lam one_branch int_cxt)
462 = ptext SLIT("Once") <> pp_lam <> pp_br <> pp_args
464 pp_lam | inside_lam = char 'L'
466 pp_br | one_branch = empty
467 | otherwise = char '*'
468 pp_args | int_cxt = char '!'
471 instance Show OccInfo where
472 showsPrec p occ = showsPrecSDoc p (ppr occ)
475 %************************************************************************
477 \subsection{Strictness indication}
479 %************************************************************************
481 The strictness annotations on types in data type declarations
482 e.g. data T = MkT !Int !(Bool,Bool)
485 data StrictnessMark -- Used in interface decls only
491 isMarkedUnboxed MarkedUnboxed = True
492 isMarkedUnboxed other = False
494 isMarkedStrict NotMarkedStrict = False
495 isMarkedStrict other = True -- All others are strict
497 instance Outputable StrictnessMark where
498 ppr MarkedStrict = ptext SLIT("!")
499 ppr MarkedUnboxed = ptext SLIT("!!")
500 ppr NotMarkedStrict = ptext SLIT("_")
504 %************************************************************************
506 \subsection{Success flag}
508 %************************************************************************
511 data SuccessFlag = Succeeded | Failed
513 successIf :: Bool -> SuccessFlag
514 successIf True = Succeeded
515 successIf False = Failed
517 succeeded, failed :: SuccessFlag -> Bool
518 succeeded Succeeded = True
519 succeeded Failed = False
521 failed Succeeded = False
526 %************************************************************************
528 \subsection{Activation}
530 %************************************************************************
532 When a rule or inlining is active
535 type CompilerPhase = Int -- Compilation phase
536 -- Phases decrease towards zero
537 -- Zero is the last phase
539 data Activation = NeverActive
541 | ActiveBefore CompilerPhase -- Active only *before* this phase
542 | ActiveAfter CompilerPhase -- Active in this phase and later
543 deriving( Eq ) -- Eq used in comparing rules in HsDecls
547 Activation -- Says during which phases inlining is allowed
548 Bool -- True <=> make the RHS look small, so that when inlining
549 -- is enabled, it will definitely actually happen
552 defaultInlineSpec = Inline AlwaysActive False -- Inlining is OK, but not forced
553 alwaysInlineSpec = Inline AlwaysActive True -- INLINE always
554 neverInlineSpec = Inline NeverActive False -- NOINLINE
556 instance Outputable Activation where
557 ppr AlwaysActive = empty -- The default
558 ppr (ActiveBefore n) = brackets (char '~' <> int n)
559 ppr (ActiveAfter n) = brackets (int n)
560 ppr NeverActive = ptext SLIT("NEVER")
562 instance Outputable InlineSpec where
563 ppr (Inline act True) = ptext SLIT("INLINE") <> ppr act
564 ppr (Inline act False) = ptext SLIT("NOINLINE") <> ppr act
566 isActive :: CompilerPhase -> Activation -> Bool
567 isActive p NeverActive = False
568 isActive p AlwaysActive = True
569 isActive p (ActiveAfter n) = p <= n
570 isActive p (ActiveBefore n) = p > n
572 isNeverActive, isAlwaysActive :: Activation -> Bool
573 isNeverActive NeverActive = True
574 isNeverActive act = False
576 isAlwaysActive AlwaysActive = True
577 isAlwaysActive other = False