Syntax.hs 101 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1
2
3
4
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1993-1998
-}
5

6

7
{-# LANGUAGE LambdaCase #-}
Ian Lynagh's avatar
Ian Lynagh committed
8

9
10
module GHC.Iface.Syntax (
        module GHC.Iface.Type,
11

12
        IfaceDecl(..), IfaceFamTyConFlav(..), IfaceClassOp(..), IfaceAT(..),
13
        IfaceConDecl(..), IfaceConDecls(..), IfaceEqSpec,
14
        IfaceExpr(..), IfaceAlt(..), IfaceLetBndr(..), IfaceJoinInfo(..),
dterei's avatar
dterei committed
15
        IfaceBinding(..), IfaceConAlt(..),
16
        IfaceIdInfo, IfaceIdDetails(..), IfaceUnfolding(..),
dterei's avatar
dterei committed
17
        IfaceInfoItem(..), IfaceRule(..), IfaceAnnotation(..), IfaceAnnTarget,
18
        IfaceClsInst(..), IfaceFamInst(..), IfaceTickish(..),
19
        IfaceClassBody(..),
20
21
22
        IfaceBang(..),
        IfaceSrcBang(..), SrcUnpackedness(..), SrcStrictness(..),
        IfaceAxBranch(..),
23
        IfaceTyConParent(..),
24
        IfaceCompleteMatch(..),
25
        IfaceLFInfo(..),
26

27
28
29
30
        -- * Binding names
        IfaceTopBndr,
        putIfaceTopBndr, getIfaceTopBndr,

dterei's avatar
dterei committed
31
        -- Misc
32
        ifaceDeclImplicitBndrs, visibleIfConDecls,
33
        ifaceDeclFingerprints,
34

35
        -- Free Names
36
        freeNamesIfDecl, freeNamesIfRule, freeNamesIfFamInst,
37

dterei's avatar
dterei committed
38
        -- Pretty printing
39
40
        pprIfaceExpr,
        pprIfaceDecl,
41
        AltPpr(..), ShowSub(..), ShowHowMuch(..), showToIface, showToHeader
42
43
    ) where

44
import GHC.Prelude
45

46
47
import GHC.Builtin.Names ( unrestrictedFunTyConKey, liftedTypeKindTyConKey )
import GHC.Types.Unique ( hasKey )
48
import GHC.Iface.Type
Sylvain Henry's avatar
Sylvain Henry committed
49
import GHC.Iface.Recomp.Binary
Sylvain Henry's avatar
Sylvain Henry committed
50
import GHC.Core( IsOrphan, isOrphan )
Sylvain Henry's avatar
Sylvain Henry committed
51
52
import GHC.Types.Demand
import GHC.Types.Cpr
Sylvain Henry's avatar
Sylvain Henry committed
53
import GHC.Core.Class
Sylvain Henry's avatar
Sylvain Henry committed
54
55
import GHC.Types.FieldLabel
import GHC.Types.Name.Set
Sylvain Henry's avatar
Sylvain Henry committed
56
import GHC.Core.Coercion.Axiom ( BranchIndex )
Sylvain Henry's avatar
Sylvain Henry committed
57
58
59
60
61
62
import GHC.Types.Name
import GHC.Types.CostCentre
import GHC.Types.Literal
import GHC.Types.ForeignCall
import GHC.Types.Annotations( AnnPayload, AnnTarget )
import GHC.Types.Basic
Sylvain Henry's avatar
Sylvain Henry committed
63
import GHC.Unit.Module
Sylvain Henry's avatar
Sylvain Henry committed
64
import GHC.Types.SrcLoc
65
import GHC.Data.BooleanFormula ( BooleanFormula, pprBooleanFormula, isTrue )
Gert-Jan Bottu's avatar
Gert-Jan Bottu committed
66
import GHC.Types.Var( VarBndr(..), binderVar, tyVarSpecToBinders )
Sylvain Henry's avatar
Sylvain Henry committed
67
68
import GHC.Core.TyCon ( Role (..), Injectivity(..), tyConBndrVisArgFlag )
import GHC.Core.DataCon (SrcStrictness(..), SrcUnpackedness(..))
Sylvain Henry's avatar
Sylvain Henry committed
69
import GHC.Builtin.Types ( constraintKindTyConName )
Andreas Klebinger's avatar
Andreas Klebinger committed
70
import GHC.Stg.InferTags.TagSig
71

72
73
74
import GHC.Utils.Lexeme (isLexSym)
import GHC.Utils.Fingerprint
import GHC.Utils.Binary
Sylvain Henry's avatar
Sylvain Henry committed
75
import GHC.Utils.Binary.Typeable ()
76
77
import GHC.Utils.Outputable as Outputable
import GHC.Utils.Panic
78
import GHC.Utils.Misc( dropList, filterByList, notNull, unzipWith,
79
80
                       seqList, zipWithEqual )

81
import Control.Monad
82
import System.IO.Unsafe
83
import Control.DeepSeq
84

85
86
infixl 3 &&&

Austin Seipp's avatar
Austin Seipp committed
87
88
89
{-
************************************************************************
*                                                                      *
90
                    Declarations
Austin Seipp's avatar
Austin Seipp committed
91
92
93
*                                                                      *
************************************************************************
-}
94

95
96
97
-- | A binding top-level 'Name' in an interface file (e.g. the name of an
-- 'IfaceDecl').
type IfaceTopBndr = Name
98
99
  -- It's convenient to have a Name in the Iface syntax, although in each
  -- case the namespace is implied by the context. However, having a
100
101
102
103
  -- Name makes things like ifaceDeclImplicitBndrs and ifaceDeclFingerprints
  -- very convenient. Moreover, having the key of the binder means that
  -- we can encode known-key things cleverly in the symbol table. See Note
  -- [Symbol table representation of Names]
104
105
  --
  -- We don't serialise the namespace onto the disk though; rather we
106
107
  -- drop it when serialising and add it back in when deserialising.

108
109
110
111
112
113
114
115
116
117
getIfaceTopBndr :: BinHandle -> IO IfaceTopBndr
getIfaceTopBndr bh = get bh

putIfaceTopBndr :: BinHandle -> IfaceTopBndr -> IO ()
putIfaceTopBndr bh name =
    case getUserData bh of
      UserData{ ud_put_binding_name = put_binding_name } ->
          --pprTrace "putIfaceTopBndr" (ppr name) $
          put_binding_name bh name

dterei's avatar
dterei committed
118
data IfaceDecl
119
  = IfaceId { ifName      :: IfaceTopBndr,
dterei's avatar
dterei committed
120
121
              ifType      :: IfaceType,
              ifIdDetails :: IfaceIdDetails,
122
123
              ifIdInfo    :: IfaceIdInfo
              }
dterei's avatar
dterei committed
124

125
  | IfaceData { ifName       :: IfaceTopBndr,   -- Type constructor
126
127
                ifBinders    :: [IfaceTyConBinder],
                ifResKind    :: IfaceType,      -- Result kind of type constructor
128
                ifCType      :: Maybe CType,    -- C type for CAPI FFI
129
                ifRoles      :: [Role],         -- Roles
dterei's avatar
dterei committed
130
                ifCtxt       :: IfaceContext,   -- The "stupid theta"
131
                ifCons       :: IfaceConDecls,  -- Includes new/data/data family info
dterei's avatar
dterei committed
132
133
                ifGadtSyntax :: Bool,           -- True <=> declared using
                                                -- GADT syntax
134
135
                ifParent     :: IfaceTyConParent -- The axiom, for a newtype,
                                                 -- or data/newtype family instance
136
    }
137

138
139
  | IfaceSynonym { ifName    :: IfaceTopBndr,      -- Type constructor
                   ifRoles   :: [Role],            -- Roles
140
141
                   ifBinders :: [IfaceTyConBinder],
                   ifResKind :: IfaceKind,         -- Kind of the *result*
142
143
144
                   ifSynRhs  :: IfaceType }

  | IfaceFamily  { ifName    :: IfaceTopBndr,      -- Type constructor
Jan Stolarek's avatar
Jan Stolarek committed
145
146
147
                   ifResVar  :: Maybe IfLclName,   -- Result variable name, used
                                                   -- only for pretty-printing
                                                   -- with --show-iface
148
149
                   ifBinders :: [IfaceTyConBinder],
                   ifResKind :: IfaceKind,         -- Kind of the *tycon*
Jan Stolarek's avatar
Jan Stolarek committed
150
151
                   ifFamFlav :: IfaceFamTyConFlav,
                   ifFamInj  :: Injectivity }      -- injectivity information
152

153
  | IfaceClass { ifName    :: IfaceTopBndr,             -- Name of the class TyCon
154
                 ifRoles   :: [Role],                   -- Roles
155
                 ifBinders :: [IfaceTyConBinder],
156
157
                 ifFDs     :: [FunDep IfLclName],       -- Functional dependencies
                 ifBody    :: IfaceClassBody            -- Methods, superclasses, ATs
158
159
    }

160
  | IfaceAxiom { ifName       :: IfaceTopBndr,        -- Axiom name
161
                 ifTyCon      :: IfaceTyCon,     -- LHS TyCon
162
                 ifRole       :: Role,           -- Role of axiom
163
164
                 ifAxBranches :: [IfaceAxBranch] -- Branches
    }
165

166
  | IfacePatSyn { ifName          :: IfaceTopBndr,           -- Name of the pattern synonym
Gergő Érdi's avatar
Gergő Érdi committed
167
                  ifPatIsInfix    :: Bool,
168
169
                  ifPatMatcher    :: (IfExtName, Bool),
                  ifPatBuilder    :: Maybe (IfExtName, Bool),
170
171
                  -- Everything below is redundant,
                  -- but needed to implement pprIfaceDecl
Gert-Jan Bottu's avatar
Gert-Jan Bottu committed
172
173
                  ifPatUnivBndrs  :: [IfaceForAllSpecBndr],
                  ifPatExBndrs    :: [IfaceForAllSpecBndr],
Gergő Érdi's avatar
Gergő Érdi committed
174
175
                  ifPatProvCtxt   :: IfaceContext,
                  ifPatReqCtxt    :: IfaceContext,
176
                  ifPatArgs       :: [IfaceType],
Matthew Pickering's avatar
Matthew Pickering committed
177
178
                  ifPatTy         :: IfaceType,
                  ifFieldLabels   :: [FieldLabel] }
Gergő Érdi's avatar
Gergő Érdi committed
179

180
181
182
183
184
185
186
187
188
189
190
-- See also 'ClassBody'
data IfaceClassBody
  -- Abstract classes don't specify their body; they only occur in @hs-boot@ and
  -- @hsig@ files.
  = IfAbstractClass
  | IfConcreteClass {
     ifClassCtxt :: IfaceContext,             -- Super classes
     ifATs       :: [IfaceAT],                -- Associated type families
     ifSigs      :: [IfaceClassOp],           -- Method signatures
     ifMinDef    :: BooleanFormula IfLclName  -- Minimal complete definition
    }
Gergő Érdi's avatar
Gergő Érdi committed
191

192
193
data IfaceTyConParent
  = IfNoParent
194
195
  | IfDataInstance
       IfExtName     -- Axiom name
196
       IfaceTyCon    -- Family TyCon (pretty-printing only, not used in GHC.IfaceToCore)
Sylvain Henry's avatar
Sylvain Henry committed
197
                     -- see Note [Pretty printing via Iface syntax] in GHC.Types.TyThing.Ppr
198
       IfaceAppArgs  -- Arguments of the family TyCon
199

200
data IfaceFamTyConFlav
201
202
  = IfaceDataFamilyTyCon                      -- Data family
  | IfaceOpenSynFamilyTyCon
203
204
205
  | IfaceClosedSynFamilyTyCon (Maybe (IfExtName, [IfaceAxBranch]))
    -- ^ Name of associated axiom and branches for pretty printing purposes,
    -- or 'Nothing' for an empty closed family without an axiom
Sylvain Henry's avatar
Sylvain Henry committed
206
    -- See Note [Pretty printing via Iface syntax] in "GHC.Types.TyThing.Ppr"
207
  | IfaceAbstractClosedSynFamilyTyCon
208
  | IfaceBuiltInSynFamTyCon -- for pretty printing purposes only
209

210
211
212
213
214
data IfaceClassOp
  = IfaceClassOp IfaceTopBndr
                 IfaceType                         -- Class op type
                 (Maybe (DefMethSpec IfaceType))   -- Default method
                 -- The types of both the class op itself,
Gabor Greif's avatar
Gabor Greif committed
215
                 -- and the default method, are *not* quantified
216
                 -- over the class variables
217

218
data IfaceAT = IfaceAT  -- See GHC.Core.Class.ClassATItem
219
220
221
                  IfaceDecl          -- The associated type declaration
                  (Maybe IfaceType)  -- Default associated type instance, if any

222

223
-- This is just like CoAxBranch
224
225
226
227
228
229
230
data IfaceAxBranch = IfaceAxBranch { ifaxbTyVars    :: [IfaceTvBndr]
                                   , ifaxbEtaTyVars :: [IfaceTvBndr]
                                   , ifaxbCoVars    :: [IfaceIdBndr]
                                   , ifaxbLHS       :: IfaceAppArgs
                                   , ifaxbRoles     :: [Role]
                                   , ifaxbRHS       :: IfaceType
                                   , ifaxbIncomps   :: [BranchIndex] }
Sylvain Henry's avatar
Sylvain Henry committed
231
                                     -- See Note [Storing compatibility] in GHC.Core.Coercion.Axiom
232

233
data IfaceConDecls
234
  = IfAbstractTyCon -- c.f TyCon.AbstractTyCon
235
236
  | IfDataTyCon [IfaceConDecl] -- Data type decls
  | IfNewTyCon  IfaceConDecl   -- Newtype decls
Adam Gundry's avatar
Adam Gundry committed
237
238
239

-- For IfDataTyCon and IfNewTyCon we store:
--  * the data constructor(s);
240
241
242
-- The field labels are stored individually in the IfaceConDecl
-- (there is some redundancy here, because a field label may occur
-- in multiple IfaceConDecls and represent the same field label)
243

dterei's avatar
dterei committed
244
data IfaceConDecl
245
  = IfCon {
246
        ifConName    :: IfaceTopBndr,                -- Constructor name
dterei's avatar
dterei committed
247
248
        ifConWrapper :: Bool,                   -- True <=> has a wrapper
        ifConInfix   :: Bool,                   -- True <=> declared infix
249
250
251
252
253
254
255

        -- The universal type variables are precisely those
        -- of the type constructor of this data constructor
        -- This is *easy* to guarantee when creating the IfCon
        -- but it's not so easy for the original TyCon/DataCon
        -- So this guarantee holds for IfaceConDecl, but *not* for DataCon

Ningning Xie's avatar
Ningning Xie committed
256
        ifConExTCvs   :: [IfaceBndr],  -- Existential ty/covars
Gert-Jan Bottu's avatar
Gert-Jan Bottu committed
257
        ifConUserTvBinders :: [IfaceForAllSpecBndr],
258
259
          -- The tyvars, in the order the user wrote them
          -- INVARIANT: the set of tyvars in ifConUserTvBinders is exactly the
Ningning Xie's avatar
Ningning Xie committed
260
261
262
          --            set of tyvars (*not* covars) of ifConExTCvs, unioned
          --            with the set of ifBinders (from the parent IfaceDecl)
          --            whose tyvars do not appear in ifConEqSpec
Sylvain Henry's avatar
Sylvain Henry committed
263
          -- See Note [DataCon user type variable binders] in GHC.Core.DataCon
264
265
        ifConEqSpec  :: IfaceEqSpec,        -- Equality constraints
        ifConCtxt    :: IfaceContext,       -- Non-stupid context
Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
266
        ifConArgTys  :: [(IfaceMult, IfaceType)],-- Arg types
267
        ifConFields  :: [FieldLabel],  -- ...ditto... (field labels)
268
269
270
        ifConStricts :: [IfaceBang],
          -- Empty (meaning all lazy),
          -- or 1-1 corresp with arg tys
Sylvain Henry's avatar
Sylvain Henry committed
271
          -- See Note [Bangs on imported data constructors] in GHC.Types.Id.Make
272
        ifConSrcStricts :: [IfaceSrcBang] } -- empty meaning no src stricts
dterei's avatar
dterei committed
273

274
type IfaceEqSpec = [(IfLclName,IfaceType)]
275

276
277
278
-- | This corresponds to an HsImplBang; that is, the final
-- implementation decision about the data constructor arg
data IfaceBang
279
280
  = IfNoBang | IfStrict | IfUnpack | IfUnpackCo IfaceCoercion

281
282
283
284
-- | This corresponds to HsSrcBang
data IfaceSrcBang
  = IfSrcBang SrcUnpackedness SrcStrictness

285
286
287
288
289
data IfaceClsInst
  = IfaceClsInst { ifInstCls  :: IfExtName,                -- See comments with
                   ifInstTys  :: [Maybe IfaceTyCon],       -- the defn of ClsInst
                   ifDFun     :: IfExtName,                -- The dfun
                   ifOFlag    :: OverlapFlag,              -- Overlap flag
Sylvain Henry's avatar
Sylvain Henry committed
290
                   ifInstOrph :: IsOrphan }                -- See Note [Orphans] in GHC.Core.InstEnv
dterei's avatar
dterei committed
291
292
293
294
295
296
        -- There's always a separate IfaceDecl for the DFun, which gives
        -- its IdInfo with its full type and version number.
        -- The instance declarations taken together have a version number,
        -- and we don't want that to wobble gratuitously
        -- If this instance decl is *used*, we'll record a usage on the dfun;
        -- and if the head does not change it won't be used if it wasn't before
297

298
-- The ifFamInstTys field of IfaceFamInst contains a list of the rough
299
-- match types
300
data IfaceFamInst
301
  = IfaceFamInst { ifFamInstFam      :: IfExtName            -- Family name
302
                 , ifFamInstTys      :: [Maybe IfaceTyCon]   -- See above
303
                 , ifFamInstAxiom    :: IfExtName            -- The axiom
304
                 , ifFamInstOrph     :: IsOrphan             -- Just like IfaceClsInst
dterei's avatar
dterei committed
305
                 }
306

307
data IfaceRule
dterei's avatar
dterei committed
308
309
310
311
312
313
314
315
  = IfaceRule {
        ifRuleName   :: RuleName,
        ifActivation :: Activation,
        ifRuleBndrs  :: [IfaceBndr],    -- Tyvars and term vars
        ifRuleHead   :: IfExtName,      -- Head of lhs
        ifRuleArgs   :: [IfaceExpr],    -- Args of LHS
        ifRuleRhs    :: IfaceExpr,
        ifRuleAuto   :: Bool,
316
        ifRuleOrph   :: IsOrphan   -- Just like IfaceClsInst
317
318
    }

319
320
321
data IfaceAnnotation
  = IfaceAnnotation {
        ifAnnotatedTarget :: IfaceAnnTarget,
322
        ifAnnotatedValue  :: AnnPayload
323
324
325
326
  }

type IfaceAnnTarget = AnnTarget OccName

327
data IfaceCompleteMatch = IfaceCompleteMatch [IfExtName] (Maybe IfaceTyCon)
328

329
instance Outputable IfaceCompleteMatch where
330
331
332
  ppr (IfaceCompleteMatch cls mtc) = text "COMPLETE" <> colon <+> ppr cls <+> case mtc of
    Nothing -> empty
    Just tc -> dcolon <+> ppr tc
333

334
-- Here's a tricky case:
335
336
--   * Compile with -O module A, and B which imports A.f
--   * Change function f in A, and recompile without -O
337
--   * When we read in old A.hi we read in its IdInfo (as a thunk)
dterei's avatar
dterei committed
338
339
340
--      (In earlier GHCs we used to drop IdInfo immediately on reading,
--       but we do not do that now.  Instead it's discarded when the
--       ModIface is read into the various decl pools.)
Gabor Greif's avatar
typo    
Gabor Greif committed
341
--   * The version comparison sees that new (=NoInfo) differs from old (=HasInfo *)
dterei's avatar
dterei committed
342
--      and so gives a new version.
343

344
type IfaceIdInfo = [IfaceInfoItem]
345

346
data IfaceInfoItem
347
  = HsArity         Arity
348
349
  | HsDmdSig        DmdSig
  | HsCprSig        CprSig
350
351
352
  | HsInline        InlinePragma
  | HsUnfold        Bool             -- True <=> isStrongLoopBreaker is true
                    IfaceUnfolding   -- See Note [Expose recursive functions]
353
  | HsNoCafRefs
354
  | HsLFInfo        IfaceLFInfo
Andreas Klebinger's avatar
Andreas Klebinger committed
355
  | HsTagSig        TagSig
356

357
358
359
-- NB: Specialisations and rules come in separately and are
-- only later attached to the Id.  Partial reason: some are orphans.

dterei's avatar
dterei committed
360
data IfaceUnfolding
361
  = IfCoreUnfold Bool IfaceExpr -- True <=> INLINABLE, False <=> regular unfolding
362
363
                                -- Possibly could eliminate the Bool here, the information
                                -- is also in the InlinePragma.
364

365
366
  | IfCompulsory IfaceExpr      -- default methods and unsafeCoerce#
                                -- for more about unsafeCoerce#, see
367
                                -- Note [Wiring in unsafeCoerce#] in "GHC.HsToCore"
368

369
  | IfInlineRule Arity          -- INLINE pragmas
dterei's avatar
dterei committed
370
371
372
                 Bool           -- OK to inline even if *un*-saturated
                 Bool           -- OK to inline even if context is boring
                 IfaceExpr
373

374
  | IfDFunUnfold [IfaceBndr] [IfaceExpr]
375

376

377
378
-- We only serialise the IdDetails of top-level Ids, and even then
-- we only need a very limited selection.  Notably, none of the
379
-- implicit ones are needed here, because they are not put in
380
-- interface files
381

382
383
data IfaceIdDetails
  = IfVanillaId
Andreas Klebinger's avatar
Andreas Klebinger committed
384
  | IfStrictWorkerId [CbvMark]
Matthew Pickering's avatar
Matthew Pickering committed
385
  | IfRecSelId (Either IfaceTyCon IfaceDecl) Bool
386
  | IfDFunId
387

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
-- | Iface type for LambdaFormInfo. Fields not relevant for imported Ids are
-- omitted in this type.
data IfaceLFInfo
  = IfLFReEntrant !RepArity
  | IfLFThunk
      !Bool -- True <=> updatable
      !Bool -- True <=> might be a function type
  | IfLFCon !Name
  | IfLFUnknown !Bool
  | IfLFUnlifted

instance Outputable IfaceLFInfo where
    ppr (IfLFReEntrant arity) =
      text "LFReEntrant" <+> ppr arity

    ppr (IfLFThunk updatable mb_fun) =
      text "LFThunk" <+> parens
        (text "updatable=" <> ppr updatable <+>
         text "might_be_function=" <+> ppr mb_fun)

    ppr (IfLFCon con) =
      text "LFCon" <> brackets (ppr con)

    ppr IfLFUnlifted =
      text "LFUnlifted"

    ppr (IfLFUnknown fun_flag) =
      text "LFUnknown" <+> ppr fun_flag

instance Binary IfaceLFInfo where
    put_ bh (IfLFReEntrant arity) = do
        putByte bh 0
        put_ bh arity
    put_ bh (IfLFThunk updatable mb_fun) = do
        putByte bh 1
        put_ bh updatable
        put_ bh mb_fun
    put_ bh (IfLFCon con_name) = do
        putByte bh 2
        put_ bh con_name
    put_ bh (IfLFUnknown fun_flag) = do
        putByte bh 3
        put_ bh fun_flag
    put_ bh IfLFUnlifted =
        putByte bh 4
    get bh = do
        tag <- getByte bh
        case tag of
            0 -> IfLFReEntrant <$> get bh
            1 -> IfLFThunk <$> get bh <*> get bh
            2 -> IfLFCon <$> get bh
            3 -> IfLFUnknown <$> get bh
            4 -> pure IfLFUnlifted
            _ -> panic "Invalid byte"

Austin Seipp's avatar
Austin Seipp committed
443
{-
444
445
Note [Versioning of instances]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
446
See [https://gitlab.haskell.org/ghc/ghc/wikis/commentary/compiler/recompilation-avoidance#instances]
447

448

Austin Seipp's avatar
Austin Seipp committed
449
450
************************************************************************
*                                                                      *
451
                Functions over declarations
Austin Seipp's avatar
Austin Seipp committed
452
453
454
*                                                                      *
************************************************************************
-}
455
456

visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
457
458
459
visibleIfConDecls (IfAbstractTyCon {}) = []
visibleIfConDecls (IfDataTyCon cs)     = cs
visibleIfConDecls (IfNewTyCon c)       = [c]
460

461
ifaceDeclImplicitBndrs :: IfaceDecl -> [OccName]
462
463
464
--  *Excludes* the 'main' name, but *includes* the implicitly-bound names
-- Deeply revolting, because it has to predict what gets bound,
-- especially the question of whether there's a wrapper for a datacon
Sylvain Henry's avatar
Sylvain Henry committed
465
-- See Note [Implicit TyThings] in GHC.Driver.Env
466

467
-- N.B. the set of names returned here *must* match the set of
Sylvain Henry's avatar
Sylvain Henry committed
468
-- TyThings returned by GHC.Driver.Env.implicitTyThings, in the sense that
469
-- TyThing.getOccName should define a bijection between the two lists.
Sylvain Henry's avatar
Sylvain Henry committed
470
471
-- This invariant is used in GHC.IfaceToCore.tc_iface_decl_fingerprint (see note
-- [Tricky iface loop])
472
-- The order of the list does not matter.
473

474
ifaceDeclImplicitBndrs (IfaceData {ifName = tc_name, ifCons = cons })
475
  = case cons of
476
477
478
      IfAbstractTyCon {} -> []
      IfNewTyCon  cd     -> mkNewTyCoOcc (occName tc_name) : ifaceConDeclImplicitBndrs cd
      IfDataTyCon cds    -> concatMap ifaceConDeclImplicitBndrs cds
479

480
481
482
483
484
485
486
487
488
ifaceDeclImplicitBndrs (IfaceClass { ifBody = IfAbstractClass })
  = []

ifaceDeclImplicitBndrs (IfaceClass { ifName = cls_tc_name
                                   , ifBody = IfConcreteClass {
                                        ifClassCtxt = sc_ctxt,
                                        ifSigs      = sigs,
                                        ifATs       = ats
                                     }})
batterseapower's avatar
batterseapower committed
489
  = --   (possibly) newtype coercion
490
491
492
493
494
495
    co_occs ++
    --    data constructor (DataCon namespace)
    --    data worker (Id namespace)
    --    no wrapper (class dictionaries never have a wrapper)
    [dc_occ, dcww_occ] ++
    -- associated types
496
    [occName (ifName at) | IfaceAT at _ <- ats ] ++
497
    -- superclass selectors
batterseapower's avatar
batterseapower committed
498
    [mkSuperDictSelOcc n cls_tc_occ | n <- [1..n_ctxt]] ++
499
    -- operation selectors
500
    [occName op | IfaceClassOp op  _ _ <- sigs]
501
  where
502
    cls_tc_occ = occName cls_tc_name
503
504
    n_ctxt = length sc_ctxt
    n_sigs = length sigs
batterseapower's avatar
batterseapower committed
505
    co_occs | is_newtype = [mkNewTyCoOcc cls_tc_occ]
dterei's avatar
dterei committed
506
            | otherwise  = []
507
    dcww_occ = mkDataConWorkerOcc dc_occ
batterseapower's avatar
batterseapower committed
508
    dc_occ = mkClassDataConOcc cls_tc_occ
509
    is_newtype = n_sigs + n_ctxt == 1 -- Sigh (keep this synced with buildClass)
510

511
ifaceDeclImplicitBndrs _ = []
512

513
ifaceConDeclImplicitBndrs :: IfaceConDecl -> [OccName]
514
515
ifaceConDeclImplicitBndrs (IfCon {
        ifConWrapper = has_wrapper, ifConName = con_name })
516
  = [occName con_name, work_occ] ++ wrap_occs
517
  where
518
    con_occ = occName con_name
519
520
521
522
    work_occ  = mkDataConWorkerOcc con_occ                   -- Id namespace
    wrap_occs | has_wrapper = [mkDataConWrapperOcc con_occ]  -- Id namespace
              | otherwise   = []

523
524
525
526
527
528
529
530
531
-- -----------------------------------------------------------------------------
-- The fingerprints of an IfaceDecl

       -- We better give each name bound by the declaration a
       -- different fingerprint!  So we calculate the fingerprint of
       -- each binder by combining the fingerprint of the whole
       -- declaration with the name of the binder. (#5614, #7215)
ifaceDeclFingerprints :: Fingerprint -> IfaceDecl -> [(OccName,Fingerprint)]
ifaceDeclFingerprints hash decl
532
  = (getOccName decl, hash) :
533
534
535
536
537
538
    [ (occ, computeFingerprint' (hash,occ))
    | occ <- ifaceDeclImplicitBndrs decl ]
  where
     computeFingerprint' =
       unsafeDupablePerformIO
        . computeFingerprint (panic "ifaceDeclFingerprints")
539

Austin Seipp's avatar
Austin Seipp committed
540
541
542
{-
************************************************************************
*                                                                      *
543
                Expressions
Austin Seipp's avatar
Austin Seipp committed
544
545
546
*                                                                      *
************************************************************************
-}
547
548
549
550
551
552

data IfaceExpr
  = IfaceLcl    IfLclName
  | IfaceExt    IfExtName
  | IfaceType   IfaceType
  | IfaceCo     IfaceCoercion
553
  | IfaceTuple  TupleSort [IfaceExpr]   -- Saturated; type arguments omitted
554
  | IfaceLam    IfaceLamBndr IfaceExpr
555
556
  | IfaceApp    IfaceExpr IfaceExpr
  | IfaceCase   IfaceExpr IfLclName [IfaceAlt]
557
  | IfaceECase  IfaceExpr IfaceType     -- See Note [Empty case alternatives]
558
  | IfaceLet    IfaceBinding  IfaceExpr
559
560
  | IfaceCast   IfaceExpr IfaceCoercion
  | IfaceLit    Literal
Simon Peyton Jones's avatar
Simon Peyton Jones committed
561
562
  | IfaceLitRubbish IfaceType -- See GHC.Types.Literal
                              --   Note [Rubbish literals] item (6)
563
564
565
566
567
568
  | IfaceFCall  ForeignCall IfaceType
  | IfaceTick   IfaceTickish IfaceExpr    -- from Tick tickish E

data IfaceTickish
  = IfaceHpcTick Module Int                -- from HpcTick x
  | IfaceSCC     CostCentre Bool Bool      -- from ProfNote
Peter Wortmann's avatar
Peter Wortmann committed
569
  | IfaceSource  RealSrcSpan String        -- from SourceNote
570
571
  -- no breakpoints: we never export these into interface files

572
data IfaceAlt = IfaceAlt IfaceConAlt [IfLclName] IfaceExpr
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
        -- Note: IfLclName, not IfaceBndr (and same with the case binder)
        -- We reconstruct the kind/type of the thing from the context
        -- thus saving bulk in interface files

data IfaceConAlt = IfaceDefault
                 | IfaceDataAlt IfExtName
                 | IfaceLitAlt Literal

data IfaceBinding
  = IfaceNonRec IfaceLetBndr IfaceExpr
  | IfaceRec    [(IfaceLetBndr, IfaceExpr)]

-- IfaceLetBndr is like IfaceIdBndr, but has IdInfo too
-- It's used for *non-top-level* let/rec binders
-- See Note [IdInfo on nested let-bindings]
lukemaurer's avatar
lukemaurer committed
588
589
590
591
data IfaceLetBndr = IfLetBndr IfLclName IfaceType IfaceIdInfo IfaceJoinInfo

data IfaceJoinInfo = IfaceNotJoinPoint
                   | IfaceJoinPoint JoinArity
592

Austin Seipp's avatar
Austin Seipp committed
593
{-
594
595
Note [Empty case alternatives]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
596
597
In Iface syntax an IfaceCase does not record the types of the alternatives,
unlike Core syntax Case. But we need this type if the alternatives are empty.
Sylvain Henry's avatar
Sylvain Henry committed
598
Hence IfaceECase. See Note [Empty case alternatives] in GHC.Core.
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613

Note [Expose recursive functions]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For supercompilation we want to put *all* unfoldings in the interface
file, even for functions that are recursive (or big).  So we need to
know when an unfolding belongs to a loop-breaker so that we can refrain
from inlining it (except during supercompilation).

Note [IdInfo on nested let-bindings]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Occasionally we want to preserve IdInfo on nested let bindings. The one
that came up was a NOINLINE pragma on a let-binding inside an INLINE
function.  The user (Duncan Coutts) really wanted the NOINLINE control
to cross the separate compilation boundary.

Sylvain Henry's avatar
Sylvain Henry committed
614
In general we retain all info that is left by GHC.Core.Tidy.tidyLetBndr, since
615
616
that is what is seen by importing module with --make

617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
Note [Displaying axiom incompatibilities]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
With -fprint-axiom-incomps we display which closed type family equations
are incompatible with which. This information is sometimes necessary
because GHC doesn't try equations in order: any equation can be used when
all preceding equations that are incompatible with it do not apply.

For example, the last "a && a = a" equation in Data.Type.Bool.&& is
actually compatible with all previous equations, and can reduce at any
time.

This is displayed as:
Prelude> :i Data.Type.Equality.==
type family (==) (a :: k) (b :: k) :: Bool
  where
632
633
634
635
636
    {- #0 -} (==) (f a) (g b) = (f == g) && (a == b)
    {- #1 -} (==) a a = 'True
          -- incompatible with: #0
    {- #2 -} (==) _1 _2 = 'False
          -- incompatible with: #1, #0
637
638
The comment after an equation refers to all previous equations (0-indexed)
that are incompatible with it.
639

Austin Seipp's avatar
Austin Seipp committed
640
641
************************************************************************
*                                                                      *
642
              Printing IfaceDecl
Austin Seipp's avatar
Austin Seipp committed
643
644
645
*                                                                      *
************************************************************************
-}
646

647
pprAxBranch :: SDoc -> BranchIndex -> IfaceAxBranch -> SDoc
648
649
650
-- The TyCon might be local (just an OccName), or this might
-- be a branch for an imported TyCon, so it would be an ExtName
-- So it's easier to take an SDoc here
651
652
653
654
655
656
--
-- This function is used
--    to print interface files,
--    in debug messages
--    in :info F for GHCi, which goes via toConToIfaceDecl on the family tycon
-- For user error messages we use Coercion.pprCoAxiom and friends
657
658
659
660
661
pprAxBranch pp_tc idx (IfaceAxBranch { ifaxbTyVars = tvs
                                     , ifaxbCoVars = _cvs
                                     , ifaxbLHS = pat_tys
                                     , ifaxbRHS = rhs
                                     , ifaxbIncomps = incomps })
662
  = assertPpr (null _cvs) (pp_tc $$ ppr _cvs) $
663
    hang ppr_binders 2 (hang pp_lhs 2 (equals <+> ppr rhs))
664
    $+$
665
    nest 4 maybe_incomps
666
  where
667
    -- See Note [Printing foralls in type family instances] in GHC.Iface.Type
668
669
    ppr_binders = maybe_index <+>
      pprUserIfaceForAll (map (mkIfaceForAllTvBndr Specified) tvs)
670
    pp_lhs = hang pp_tc 2 (pprParendIfaceAppArgs pat_tys)
671
672

    -- See Note [Displaying axiom incompatibilities]
673
    maybe_index
Sylvain Henry's avatar
Sylvain Henry committed
674
      = ppWhenOption sdocPrintAxiomIncomps $
675
          text "{-" <+> (text "#" <> ppr idx) <+> text "-}"
676
    maybe_incomps
Sylvain Henry's avatar
Sylvain Henry committed
677
678
679
680
      = ppWhenOption sdocPrintAxiomIncomps $
          ppWhen (notNull incomps) $
            text "--" <+> text "incompatible with:"
            <+> pprWithCommas (\incomp -> text "#" <> ppr incomp) incomps
681
682
683
684

instance Outputable IfaceAnnotation where
  ppr (IfaceAnnotation target value) = ppr target <+> colon <+> ppr value

685
686
687
instance NamedThing IfaceClassOp where
  getName (IfaceClassOp n _ _) = n

688
instance HasOccName IfaceClassOp where
689
690
691
692
  occName = getOccName

instance NamedThing IfaceConDecl where
  getName = ifConName
693

694
instance HasOccName IfaceConDecl where
695
696
697
698
  occName = getOccName

instance NamedThing IfaceDecl where
  getName = ifName
699

700
instance HasOccName IfaceDecl where
701
  occName = getOccName
702

703
instance Outputable IfaceDecl where
704
  ppr = pprIfaceDecl showToIface
705

706
{-
707
708
Note [Minimal complete definition]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
709
710
711
712
713
714
715
The minimal complete definition should only be included if a complete
class definition is shown. Since the minimal complete definition is
anonymous we can't reuse the same mechanism that is used for the
filtering of method signatures. Instead we just check if anything at all is
filtered and hide it in that case.
-}

716
717
data ShowSub
  = ShowSub
718
719
720
721
722
723
      { ss_how_much :: ShowHowMuch
      , ss_forall :: ShowForAllFlag }

-- See Note [Printing IfaceDecl binders]
-- The alternative pretty printer referred to in the note.
newtype AltPpr = AltPpr (Maybe (OccName -> SDoc))
724
725

data ShowHowMuch
726
727
728
729
  = ShowHeader AltPpr -- ^Header information only, not rhs
  | ShowSome [OccName] AltPpr
  -- ^ Show only some sub-components. Specifically,
  --
730
  -- [@\[\]@] Print all sub-components.
731
732
733
734
735
736
737
738
739
740
741
  -- [@(n:ns)@] Print sub-component @n@ with @ShowSub = ns@;
  -- elide other sub-components to @...@
  -- May 14: the list is max 1 element long at the moment
  | ShowIface
  -- ^Everything including GHC-internal information (used in --show-iface)

{-
Note [Printing IfaceDecl binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The binders in an IfaceDecl are just OccNames, so we don't know what module they
come from.  But when we pretty-print a TyThing by converting to an IfaceDecl
Sylvain Henry's avatar
Sylvain Henry committed
742
(see GHC.Types.TyThing.Ppr), the TyThing may come from some other module so we really need
743
744
745
746
747
748
the module qualifier.  We solve this by passing in a pretty-printer for the
binders.

When printing an interface file (--show-iface), we want to print
everything unqualified, so we can just print the OccName directly.
-}
749

750
instance Outputable ShowHowMuch where
751
752
753
754
755
756
757
  ppr (ShowHeader _)    = text "ShowHeader"
  ppr ShowIface         = text "ShowIface"
  ppr (ShowSome occs _) = text "ShowSome" <+> ppr occs

showToHeader :: ShowSub
showToHeader = ShowSub { ss_how_much = ShowHeader $ AltPpr Nothing
                       , ss_forall = ShowForAllWhen }
758

759
760
761
showToIface :: ShowSub
showToIface = ShowSub { ss_how_much = ShowIface
                      , ss_forall = ShowForAllWhen }
762
763
764

ppShowIface :: ShowSub -> SDoc -> SDoc
ppShowIface (ShowSub { ss_how_much = ShowIface }) doc = doc
765
ppShowIface _                                     _   = Outputable.empty
766

767
-- show if all sub-components or the complete interface is shown
768
ppShowAllSubs :: ShowSub -> SDoc -> SDoc -- See Note [Minimal complete definition]
769
770
771
ppShowAllSubs (ShowSub { ss_how_much = ShowSome [] _ }) doc = doc
ppShowAllSubs (ShowSub { ss_how_much = ShowIface })     doc = doc
ppShowAllSubs _                                         _   = Outputable.empty
772

773
ppShowRhs :: ShowSub -> SDoc -> SDoc
774
775
ppShowRhs (ShowSub { ss_how_much = ShowHeader _ }) _   = Outputable.empty
ppShowRhs _                                        doc = doc
776
777

showSub :: HasOccName n => ShowSub -> n -> Bool
778
779
showSub (ShowSub { ss_how_much = ShowHeader _ })     _     = False
showSub (ShowSub { ss_how_much = ShowSome (n:_) _ }) thing = n == occName thing
780
showSub (ShowSub { ss_how_much = _ })              _     = True
781

782
783
784
785
786
787
788
ppr_trim :: [Maybe SDoc] -> [SDoc]
-- Collapse a group of Nothings to a single "..."
ppr_trim xs
  = snd (foldr go (False, []) xs)
  where
    go (Just doc) (_,     so_far) = (False, doc : so_far)
    go Nothing    (True,  so_far) = (True, so_far)
789
    go Nothing    (False, so_far) = (True, text "..." : so_far)
790
791
792
793
794

isIfaceDataInstance :: IfaceTyConParent -> Bool
isIfaceDataInstance IfNoParent = False
isIfaceDataInstance _          = True

795
796
797
798
799
800
801
pprClassRoles :: ShowSub -> IfaceTopBndr -> [IfaceTyConBinder] -> [Role] -> SDoc
pprClassRoles ss clas binders roles =
    pprRoles (== Nominal)
             (pprPrefixIfDeclBndr (ss_how_much ss) (occName clas))
             binders
             roles

802
803
804
805
806
807
pprClassStandaloneKindSig :: ShowSub -> IfaceTopBndr -> IfaceKind -> SDoc
pprClassStandaloneKindSig ss clas =
  pprStandaloneKindSig (pprPrefixIfDeclBndr (ss_how_much ss) (occName clas))

constraintIfaceKind :: IfaceKind
constraintIfaceKind =
808
  IfaceTyConApp (IfaceTyCon constraintKindTyConName (mkIfaceTyConInfo NotPromoted IfaceNormalTyCon)) IA_Nil
809

810
pprIfaceDecl :: ShowSub -> IfaceDecl -> SDoc
811
-- NB: pprIfaceDecl is also used for pretty-printing TyThings in GHCi
812
--     See Note [Pretty printing via Iface syntax] in GHC.Types.TyThing.Ppr
813
pprIfaceDecl ss (IfaceData { ifName = tycon, ifCType = ctype,
Edward Z. Yang's avatar
Edward Z. Yang committed
814
                             ifCtxt = context, ifResKind = kind,
815
                             ifRoles = roles, ifCons = condecls,
Edward Z. Yang's avatar
Edward Z. Yang committed
816
                             ifParent = parent,
817
                             ifGadtSyntax = gadt,
818
                             ifBinders = binders })
819

820
  | gadt      = vcat [ pp_roles
821
                     , pp_ki_sig
Edward Z. Yang's avatar
Edward Z. Yang committed
822
                     , pp_nd <+> pp_lhs <+> pp_kind <+> pp_where
823
824
825
                     , nest 2 (vcat pp_cons)
                     , nest 2 $ ppShowIface ss pp_extra ]
  | otherwise = vcat [ pp_roles
826
                     , pp_ki_sig
827
                     , hang (pp_nd <+> pp_lhs) 2 (add_bars pp_cons)
828
                     , nest 2 $ ppShowIface ss pp_extra ]
829
830
  where
    is_data_instance = isIfaceDataInstance parent
831
    -- See Note [Printing foralls in type family instances] in GHC.Iface.Type
832
833
834
835
836
    pp_data_inst_forall :: SDoc
    pp_data_inst_forall = pprUserIfaceForAll forall_bndrs

    forall_bndrs :: [IfaceForAllBndr]
    forall_bndrs = [Bndr (binderVar tc_bndr) Specified | tc_bndr <- binders]
837
838

    cons       = visibleIfConDecls condecls
839
    pp_where   = ppWhen (gadt && not (null cons)) $ text "where"
840
    pp_cons    = ppr_trim (map show_con cons) :: [SDoc]
841
842
843
844
    pp_kind    = ppUnless (if ki_sig_printable
                              then isIfaceTauType kind
                                      -- Even in the presence of a standalone kind signature, a non-tau
                                      -- result kind annotation cannot be discarded as it determines the arity.
Sylvain Henry's avatar
Sylvain Henry committed
845
                                      -- See Note [Arity inference in kcCheckDeclHeader_sig] in GHC.Tc.Gen.HsType
846
847
                              else isIfaceLiftedTypeKind kind)
                          (dcolon <+> ppr kind)
848
849

    pp_lhs = case parent of
850
               IfNoParent -> pprIfaceDeclHead suppress_bndr_sig context ss tycon binders
851
852
853
               IfDataInstance{}
                          -> text "instance" <+> pp_data_inst_forall
                                             <+> pprIfaceTyConParent parent
854
855

    pp_roles
856
      | is_data_instance = empty
857
      | otherwise        = pprRoles (== Representational) name_doc binders roles
858
            -- Don't display roles for data family instances (yet)
859
            -- See discussion on #8672.
860

861
862
863
864
865
866
867
868
869
870
871
872
873
874
    ki_sig_printable =
      -- If we print a standalone kind signature for a data instance, we leak
      -- the internal constructor name:
      --
      --    type T15827.R:Dka :: forall k. k -> *
      --    data instance forall k (a :: k). D a = MkD (Proxy a)
      --
      -- This T15827.R:Dka is a compiler-generated type constructor for the
      -- data instance.
      not is_data_instance

    pp_ki_sig = ppWhen ki_sig_printable $
                pprStandaloneKindSig name_doc (mkIfaceTyConKind binders kind)

875
    -- See Note [Suppressing binder signatures] in GHC.Iface.Type
876
877
878
879
    suppress_bndr_sig = SuppressBndrSig ki_sig_printable

    name_doc = pprPrefixIfDeclBndr (ss_how_much ss) (occName tycon)

880
    add_bars []     = Outputable.empty
881
    add_bars (c:cs) = sep ((equals <+> c) : map (vbar <+>) cs)
882

883
    ok_con dc = showSub ss dc || any (showSub ss . flSelector) (ifConFields dc)
884
885

    show_con dc
886
      | ok_con dc = Just $ pprIfaceConDecl ss gadt tycon binders parent dc
887
      | otherwise = Nothing
888

889
    pp_nd = case condecls of
890
              IfAbstractTyCon{} -> text "data"
891
892
              IfDataTyCon{}     -> text "data"
              IfNewTyCon{}      -> text "newtype"
893

Edward Z. Yang's avatar
Edward Z. Yang committed
894
    pp_extra = vcat [pprCType ctype]
895

896
pprIfaceDecl ss (IfaceClass { ifName  = clas
897
                            , ifRoles = roles
898
899
900
901
                            , ifFDs    = fds
                            , ifBinders = binders
                            , ifBody = IfAbstractClass })
  = vcat [ pprClassRoles ss clas binders roles
902
903
904
         , pprClassStandaloneKindSig ss clas (mkIfaceTyConKind binders constraintIfaceKind)
         , text "class" <+> pprIfaceDeclHead suppress_bndr_sig [] ss clas binders <+> pprFundeps fds ]
  where
905
    -- See Note [Suppressing binder signatures] in GHC.Iface.Type
906
    suppress_bndr_sig = SuppressBndrSig True
907
908
909
910
911
912
913
914
915
916
917
918

pprIfaceDecl ss (IfaceClass { ifName  = clas
                            , ifRoles = roles
                            , ifFDs    = fds
                            , ifBinders = binders
                            , ifBody = IfConcreteClass {
                                ifATs = ats,
                                ifSigs = sigs,
                                ifClassCtxt = context,
                                ifMinDef = minDef
                              }})
  = vcat [ pprClassRoles ss clas binders roles
919
920
         , pprClassStandaloneKindSig ss clas (mkIfaceTyConKind binders constraintIfaceKind)
         , text "class" <+> pprIfaceDeclHead suppress_bndr_sig context ss clas binders <+> pprFundeps fds <+> pp_where
Edward Z. Yang's avatar
Edward Z. Yang committed
921
         , nest 2 (vcat [ vcat asocs, vcat dsigs
922
                        , ppShowAllSubs ss (pprMinDef minDef)])]
923
    where
924
      pp_where = ppShowRhs ss $ ppUnless (null sigs && null ats) (text "where")
925
926
927
928
929
930
931
932
933
934
935
936
937
938

      asocs = ppr_trim $ map maybeShowAssoc ats
      dsigs = ppr_trim $ map maybeShowSig sigs

      maybeShowAssoc :: IfaceAT -> Maybe SDoc
      maybeShowAssoc asc@(IfaceAT d _)
        | showSub ss d = Just $ pprIfaceAT ss asc
        | otherwise    = Nothing

      maybeShowSig :: IfaceClassOp -> Maybe SDoc
      maybeShowSig sg
        | showSub ss sg = Just $  pprIfaceClassOp ss sg
        | otherwise     = Nothing

939
940
      pprMinDef :: BooleanFormula IfLclName -> SDoc
      pprMinDef minDef = ppUnless (isTrue minDef) $ -- hide empty definitions
941
        text "{-# MINIMAL" <+>
942
943
        pprBooleanFormula
          (\_ def -> cparen (isLexSym def) (ppr def)) 0 minDef <+>
944
        text "#-}"
945

946
      -- See Note [Suppressing binder signatures] in GHC.Iface.Type
947
948
      suppress_bndr_sig = SuppressBndrSig True

949
pprIfaceDecl ss (IfaceSynonym { ifName    = tc
950
                              , ifBinders = binders
951
                              , ifSynRhs  = mono_ty
952
                              , ifResKind = res_kind})
953
954
  = vcat [ pprStandaloneKindSig name_doc (mkIfaceTyConKind binders res_kind)
         , hang (text "type" <+> pprIfaceDeclHead suppress_bndr_sig [] ss tc binders <+> equals)
955
           2 (sep [ pprIfaceForAll tvs, pprIfaceContextArr theta, ppr_tau
956
957
                  , ppUnless (isIfaceLiftedTypeKind res_kind) (dcolon <+> ppr res_kind) ])
         ]
958
  where
959
    (tvs, theta, tau) = splitIfaceSigmaTy mono_ty
960
961
    name_doc = pprPrefixIfDeclBndr (ss_how_much ss) (occName tc)