HsDecls.hs 74.9 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, 1992-1998
-}
5

6
{-# LANGUAGE CPP #-}
7 8
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveFoldable,
             DeriveTraversable #-}
9 10 11 12 13
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
                                      -- in module PlaceHolder
{-# LANGUAGE ConstraintKinds #-}
14 15
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
16

17 18
-- | Abstract syntax of global declarations.
--
19
-- Definitions for: @SynDecl@ and @ConDecl@, @ClassDecl@,
20
-- @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
21
module HsDecls (
22
  -- * Toplevel declarations
23
  HsDecl(..), LHsDecl, HsDataDefn(..),
24
  -- ** Class or type declarations
25 26
  TyClDecl(..), LTyClDecl,
  TyClGroup(..), tyClGroupConcat, mkTyClGroup,
27 28
  isClassDecl, isDataDecl, isSynDecl, tcdName,
  isFamilyDecl, isTypeFamilyDecl, isDataFamilyDecl,
29
  isOpenTypeFamilyInfo, isClosedTypeFamilyInfo,
30 31 32
  tyFamInstDeclName, tyFamInstDeclLName,
  countTyClDecls, pprTyClDeclFlavour,
  tyClDeclLName, tyClDeclTyVars,
33
  hsDeclHasCusk, famDeclHasCusk,
34
  FamilyDecl(..), LFamilyDecl,
35

36
  -- ** Instance declarations
37
  InstDecl(..), LInstDecl, NewOrData(..), FamilyInfo(..),
38 39
  TyFamInstDecl(..), LTyFamInstDecl, instDeclDataFamInsts,
  DataFamInstDecl(..), LDataFamInstDecl, pprDataFamInstFlavour,
40
  TyFamEqn(..), TyFamInstEqn, LTyFamInstEqn, TyFamDefltEqn, LTyFamDefltEqn,
Alan Zimmerman's avatar
Alan Zimmerman committed
41
  HsTyPats,
42
  LClsInstDecl, ClsInstDecl(..),
43

44 45 46
  -- ** Standalone deriving declarations
  DerivDecl(..), LDerivDecl,
  -- ** @RULE@ declarations
Alan Zimmerman's avatar
Alan Zimmerman committed
47
  LRuleDecls,RuleDecls(..),RuleDecl(..), LRuleDecl, RuleBndr(..),LRuleBndr,
48
  collectRuleBndrSigTys,
Alan Zimmerman's avatar
Alan Zimmerman committed
49
  flattenRuleDecls,
50 51
  -- ** @VECTORISE@ declarations
  VectDecl(..), LVectDecl,
52
  lvectDeclName, lvectInstDecl,
53 54
  -- ** @default@ declarations
  DefaultDecl(..), LDefaultDecl,
55
  -- ** Template haskell declaration splice
56
  SpliceExplicitFlag(..),
57
  SpliceDecl(..), LSpliceDecl,
58 59
  -- ** Foreign function interface declarations
  ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
60
  noForeignImportCoercionYet, noForeignExportCoercionYet,
61
  CImportSpec(..),
62
  -- ** Data-constructor declarations
63 64
  ConDecl(..), LConDecl, ResType(..),
  HsConDeclDetails, hsConDeclArgTys,
65 66 67 68
  -- ** Document comments
  DocDecl(..), LDocDecl, docDeclDoc,
  -- ** Deprecations
  WarnDecl(..),  LWarnDecl,
Alan Zimmerman's avatar
Alan Zimmerman committed
69
  WarnDecls(..), LWarnDecls,
70
  -- ** Annotations
71
  AnnDecl(..), LAnnDecl,
72
  AnnProvenance(..), annProvenanceName_maybe,
73 74
  -- ** Role annotations
  RoleAnnotDecl(..), LRoleAnnotDecl, roleAnnotDeclName,
Jan Stolarek's avatar
Jan Stolarek committed
75 76 77
  -- ** Injective type families
  FamilyResultSig(..), LFamilyResultSig, InjectivityAnn(..), LInjectivityAnn,
  resultVariableName,
78 79

  -- * Grouping
80
  HsGroup(..),  emptyRdrGroup, emptyRnGroup, appendGroups
81

82
    ) where
83 84

-- friends:
85
import {-# SOURCE #-}   HsExpr( LHsExpr, HsExpr, HsSplice, pprExpr, pprSplice )
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
86
        -- Because Expr imports Decls via HsBracket
87

88 89
import HsBinds
import HsPat
90
import HsTypes
91
import HsDoc
92
import TyCon
93
import Name
94
import BasicTypes
95
import Coercion
96
import ForeignCall
97 98
import PlaceHolder ( PostTc,PostRn,PlaceHolder(..),DataId )
import NameSet
99 100

-- others:
101
import InstEnv
102
import Class
103
import Outputable
104 105
import Util
import SrcLoc
rrt's avatar
rrt committed
106
import FastString
107

108
import Bag
109
import Data.Data        hiding (TyCon,Fixity)
110 111 112 113
#if __GLASGOW_HASKELL__ < 709
import Data.Foldable ( Foldable )
import Data.Traversable ( Traversable )
#endif
114

Austin Seipp's avatar
Austin Seipp committed
115 116 117
{-
************************************************************************
*                                                                      *
118
\subsection[HsDecl]{Declarations}
Austin Seipp's avatar
Austin Seipp committed
119 120 121
*                                                                      *
************************************************************************
-}
122

123
type LHsDecl id = Located (HsDecl id)
Alan Zimmerman's avatar
Alan Zimmerman committed
124 125 126 127
        -- ^ When in a list this may have
        --
        --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSemi'
        --
128

129 130
-- For details on above see note [Api annotations] in ApiAnnotation

131
-- | A Haskell Declaration
132
data HsDecl id
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
133 134
  = TyClD       (TyClDecl id)     -- ^ A type or class declaration.
  | InstD       (InstDecl  id)    -- ^ An instance declaration.
135
  | DerivD      (DerivDecl id)
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
136 137 138
  | ValD        (HsBind id)
  | SigD        (Sig id)
  | DefD        (DefaultDecl id)
139
  | ForD        (ForeignDecl id)
Alan Zimmerman's avatar
Alan Zimmerman committed
140
  | WarningD    (WarnDecls id)
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
141
  | AnnD        (AnnDecl id)
Alan Zimmerman's avatar
Alan Zimmerman committed
142
  | RuleD       (RuleDecls id)
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
143
  | VectD       (VectDecl id)
144
  | SpliceD     (SpliceDecl id)   -- Includes quasi-quotes
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
145
  | DocD        (DocDecl)
146
  | RoleAnnotD  (RoleAnnotDecl id)
147 148
  deriving (Typeable)
deriving instance (DataId id) => Data (HsDecl id)
149

150 151

-- NB: all top-level fixity decls are contained EITHER
152
-- EITHER SigDs
153 154 155
-- OR     in the ClassDecls in TyClDs
--
-- The former covers
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
156 157 158 159 160
--      a) data constructors
--      b) class methods (but they can be also done in the
--              signatures of class decls)
--      c) imported functions (that have an IfacSig)
--      d) top level decls
161 162
--
-- The latter is for class methods only
163

164
-- | A 'HsDecl' is categorised into a 'HsGroup' before being
165 166 167
-- fed to the renamer.
data HsGroup id
  = HsGroup {
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
168
        hs_valds  :: HsValBinds id,
169
        hs_splcds :: [LSpliceDecl id],
170

171
        hs_tyclds :: [TyClGroup id],
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
172
                -- A list of mutually-recursive groups
173
                -- No family-instances here; they are in hs_instds
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
174 175
                -- Parser generates a singleton list;
                -- renamer does dependency analysis
176

177 178 179
        hs_instds  :: [LInstDecl id],
                -- Both class and family instance declarations in here

180
        hs_derivds :: [LDerivDecl id],
181

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
182 183 184
        hs_fixds  :: [LFixitySig id],
                -- Snaffled out of both top-level fixity signatures,
                -- and those in class declarations
185

186 187
        hs_defds  :: [LDefaultDecl id],
        hs_fords  :: [LForeignDecl id],
Alan Zimmerman's avatar
Alan Zimmerman committed
188
        hs_warnds :: [LWarnDecls id],
189
        hs_annds  :: [LAnnDecl id],
Alan Zimmerman's avatar
Alan Zimmerman committed
190
        hs_ruleds :: [LRuleDecls id],
191
        hs_vects  :: [LVectDecl id],
192

193
        hs_docs   :: [LDocDecl]
194 195
  } deriving (Typeable)
deriving instance (DataId id) => Data (HsGroup id)
196

197 198 199 200
emptyGroup, emptyRdrGroup, emptyRnGroup :: HsGroup a
emptyRdrGroup = emptyGroup { hs_valds = emptyValBindsIn }
emptyRnGroup  = emptyGroup { hs_valds = emptyValBindsOut }

201
emptyGroup = HsGroup { hs_tyclds = [], hs_instds = [],
202
                       hs_derivds = [],
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
203 204 205
                       hs_fixds = [], hs_defds = [], hs_annds = [],
                       hs_fords = [], hs_warnds = [], hs_ruleds = [], hs_vects = [],
                       hs_valds = error "emptyGroup hs_valds: Can't happen",
206
                       hs_splcds = [],
207
                       hs_docs = [] }
208 209

appendGroups :: HsGroup a -> HsGroup a -> HsGroup a
210 211
appendGroups
    HsGroup {
212
        hs_valds  = val_groups1,
213
        hs_splcds = spliceds1,
214
        hs_tyclds = tyclds1,
215
        hs_instds = instds1,
216
        hs_derivds = derivds1,
217
        hs_fixds  = fixds1,
218 219
        hs_defds  = defds1,
        hs_annds  = annds1,
220
        hs_fords  = fords1,
221 222 223
        hs_warnds = warnds1,
        hs_ruleds = rulds1,
        hs_vects = vects1,
224
  hs_docs   = docs1 }
225
    HsGroup {
226
        hs_valds  = val_groups2,
227
        hs_splcds = spliceds2,
228
        hs_tyclds = tyclds2,
229
        hs_instds = instds2,
230
        hs_derivds = derivds2,
231
        hs_fixds  = fixds2,
232 233
        hs_defds  = defds2,
        hs_annds  = annds2,
234
        hs_fords  = fords2,
235 236 237 238
        hs_warnds = warnds2,
        hs_ruleds = rulds2,
        hs_vects  = vects2,
        hs_docs   = docs2 }
239 240
  =
    HsGroup {
241
        hs_valds  = val_groups1 `plusHsValBinds` val_groups2,
242 243
        hs_splcds = spliceds1 ++ spliceds2,
        hs_tyclds = tyclds1 ++ tyclds2,
244
        hs_instds = instds1 ++ instds2,
245
        hs_derivds = derivds1 ++ derivds2,
246 247 248
        hs_fixds  = fixds1 ++ fixds2,
        hs_annds  = annds1 ++ annds2,
        hs_defds  = defds1 ++ defds2,
249
        hs_fords  = fords1 ++ fords2,
250 251 252 253
        hs_warnds = warnds1 ++ warnds2,
        hs_ruleds = rulds1 ++ rulds2,
        hs_vects  = vects1 ++ vects2,
        hs_docs   = docs1  ++ docs2 }
254

255
instance OutputableBndr name => Outputable (HsDecl name) where
256 257 258 259 260 261 262 263
    ppr (TyClD dcl)             = ppr dcl
    ppr (ValD binds)            = ppr binds
    ppr (DefD def)              = ppr def
    ppr (InstD inst)            = ppr inst
    ppr (DerivD deriv)          = ppr deriv
    ppr (ForD fd)               = ppr fd
    ppr (SigD sd)               = ppr sd
    ppr (RuleD rd)              = ppr rd
264
    ppr (VectD vect)            = ppr vect
Ian Lynagh's avatar
Ian Lynagh committed
265
    ppr (WarningD wd)           = ppr wd
266
    ppr (AnnD ad)               = ppr ad
267 268
    ppr (SpliceD dd)            = ppr dd
    ppr (DocD doc)              = ppr doc
269
    ppr (RoleAnnotD ra)         = ppr ra
270 271 272

instance OutputableBndr name => Outputable (HsGroup name) where
    ppr (HsGroup { hs_valds  = val_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
273 274
                   hs_tyclds = tycl_decls,
                   hs_instds = inst_decls,
275
                   hs_derivds = deriv_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
276 277 278 279 280 281 282
                   hs_fixds  = fix_decls,
                   hs_warnds = deprec_decls,
                   hs_annds  = ann_decls,
                   hs_fords  = foreign_decls,
                   hs_defds  = default_decls,
                   hs_ruleds = rule_decls,
                   hs_vects  = vect_decls })
283 284
        = vcat_mb empty
            [ppr_ds fix_decls, ppr_ds default_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
285 286 287
             ppr_ds deprec_decls, ppr_ds ann_decls,
             ppr_ds rule_decls,
             ppr_ds vect_decls,
288 289
             if isEmptyValBinds val_decls
                then Nothing
290
                else Just (ppr val_decls),
291
             ppr_ds (tyClGroupConcat tycl_decls),
292
             ppr_ds inst_decls,
293
             ppr_ds deriv_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
294 295
             ppr_ds foreign_decls]
        where
296
          ppr_ds :: Outputable a => [a] -> Maybe SDoc
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
297 298
          ppr_ds [] = Nothing
          ppr_ds ds = Just (vcat (map ppr ds))
299 300

          vcat_mb :: SDoc -> [Maybe SDoc] -> SDoc
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
301
          -- Concatenate vertically with white-space between non-blanks
302 303 304
          vcat_mb _    []             = empty
          vcat_mb gap (Nothing : ds) = vcat_mb gap ds
          vcat_mb gap (Just d  : ds) = gap $$ d $$ vcat_mb blankLine ds
305

306 307 308 309
data SpliceExplicitFlag = ExplicitSplice | -- <=> $(f x y)
                          ImplicitSplice   -- <=> f x y,  i.e. a naked top level expression
    deriving (Data, Typeable)

310
type LSpliceDecl name = Located (SpliceDecl name)
311
data SpliceDecl id
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
312
  = SpliceDecl                  -- Top level splice
313
        (Located (HsSplice id))
314
        SpliceExplicitFlag
315 316
    deriving (Typeable)
deriving instance (DataId id) => Data (SpliceDecl id)
317 318

instance OutputableBndr name => Outputable (SpliceDecl name) where
319
   ppr (SpliceDecl (L _ e) _) = pprSplice e
320

Austin Seipp's avatar
Austin Seipp committed
321 322 323
{-
************************************************************************
*                                                                      *
324
\subsection[SynDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
Austin Seipp's avatar
Austin Seipp committed
325 326
*                                                                      *
************************************************************************
327

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
328 329 330
                --------------------------------
                        THE NAMING STORY
                --------------------------------
331

332 333
Here is the story about the implicit names that go with type, class,
and instance decls.  It's a bit tricky, so pay attention!
334 335 336

"Implicit" (or "system") binders
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
337
  Each data type decl defines
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
338 339
        a worker name for each constructor
        to-T and from-T convertors
340
  Each class decl defines
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
341 342 343 344
        a tycon for the class
        a data constructor for that tycon
        the worker for that constructor
        a selector for each superclass
345

346 347
All have occurrence names that are derived uniquely from their parent
declaration.
348 349 350 351 352 353 354

None of these get separate definitions in an interface file; they are
fully defined by the data or class decl.  But they may *occur* in
interface files, of course.  Any such occurrence must haul in the
relevant type or class decl.

Plan of attack:
355
 - Ensure they "point to" the parent data/class decl
356
   when loading that decl from an interface file
357 358 359 360 361
   (See RnHiFiles.getSysBinders)

 - When typechecking the decl, we build the implicit TyCons and Ids.
   When doing so we look them up in the name cache (RnEnv.lookupSysName),
   to ensure correct module and provenance is set
362

363 364
These are the two places that we have to conjure up the magic derived
names.  (The actual magic is in OccName.mkWorkerOcc, etc.)
365

366 367 368 369 370 371 372
Default methods
~~~~~~~~~~~~~~~
 - Occurrence name is derived uniquely from the method name
   E.g. $dmmax

 - If there is a default method name at all, it's recorded in
   the ClassOpSig (in HsBinds), in the DefMeth field.
373
   (DefMeth is defined in Class.hs)
374 375 376 377 378 379 380

Source-code class decls and interface-code class decls are treated subtly
differently, which has given me a great deal of confusion over the years.
Here's the deal.  (We distinguish the two cases because source-code decls
have (Just binds) in the tcdMeths field, whereas interface decls have Nothing.

In *source-code* class declarations:
381

382 383 384 385 386
 - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName
   This is done by RdrHsSyn.mkClassOpSigDM

 - The renamer renames it to a Name

387
 - During typechecking, we generate a binding for each $dm for
388
   which there's a programmer-supplied default method:
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
389 390 391 392
        class Foo a where
          op1 :: <type>
          op2 :: <type>
          op1 = ...
393 394 395 396 397 398 399
   We generate a binding for $dmop1 but not for $dmop2.
   The Class for Foo has a NoDefMeth for op2 and a DefMeth for op1.
   The Name for $dmop2 is simply discarded.

In *interface-file* class declarations:
  - When parsing, we see if there's an explicit programmer-supplied default method
    because there's an '=' sign to indicate it:
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
400 401 402
        class Foo a where
          op1 = :: <type>       -- NB the '='
          op2   :: <type>
403 404 405 406 407
    We use this info to generate a DefMeth with a suitable RdrName for op1,
    and a NoDefMeth for op2
  - The interface file has a separate definition for $dmop1, with unfolding etc.
  - The renamer renames it to a Name.
  - The renamer treats $dmop1 as a free variable of the declaration, so that
408
    the binding for $dmop1 will be sucked in.  (See RnHsSyn.tyClDeclFVs)
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    This doesn't happen for source code class decls, because they *bind* the default method.

Dictionary functions
~~~~~~~~~~~~~~~~~~~~
Each instance declaration gives rise to one dictionary function binding.

The type checker makes up new source-code instance declarations
(e.g. from 'deriving' or generic default methods --- see
TcInstDcls.tcInstDecls1).  So we can't generate the names for
dictionary functions in advance (we don't know how many we need).

On the other hand for interface-file instance declarations, the decl
specifies the name of the dictionary function, and it has a binding elsewhere
in the interface file:
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
423 424
        instance {Eq Int} = dEqInt
        dEqInt :: {Eq Int} <pragma info>
425 426 427 428 429 430 431 432 433 434 435

So again we treat source code and interface file code slightly differently.

Source code:
  - Source code instance decls have a Nothing in the (Maybe name) field
    (see data InstDecl below)

  - The typechecker makes up a Local name for the dict fun for any source-code
    instance decl, whether it comes from a source-code instance decl, or whether
    the instance decl is derived from some other construct (e.g. 'deriving').

436
  - The occurrence name it chooses is derived from the instance decl (just for
437 438
    documentation really) --- e.g. dNumInt.  Two dict funs may share a common
    occurrence name, but will have different uniques.  E.g.
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
439 440
        instance Foo [Int]  where ...
        instance Foo [Bool] where ...
441 442
    These might both be dFooList

443
  - The CoreTidy phase externalises the name, and ensures the occurrence name is
444 445
    unique (this isn't special to dict funs).  So we'd get dFooList and dFooList1.

446
  - We can take this relaxed approach (changing the occurrence name later)
447 448 449 450 451 452 453 454 455
    because dict fun Ids are not captured in a TyCon or Class (unlike default
    methods, say).  Instead, they are kept separately in the InstEnv.  This
    makes it easy to adjust them after compiling a module.  (Once we've finished
    compiling that module, they don't change any more.)


Interface file code:
  - The instance decl gives the dict fun name, so the InstDecl has a (Just name)
    in the (Maybe name) field.
456

457 458
  - RnHsSyn.instDeclFVs treats the dict fun name as free in the decl, so that we
    suck in the dfun binding
Austin Seipp's avatar
Austin Seipp committed
459
-}
460

461 462
type LTyClDecl name = Located (TyClDecl name)

463
-- | A type or class declaration.
464
data TyClDecl name
Yuras's avatar
Yuras committed
465
  = -- | @type/data family T :: *->*@
Alan Zimmerman's avatar
Alan Zimmerman committed
466 467 468
    --
    --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnType',
    --             'ApiAnnotation.AnnData',
469
    --             'ApiAnnotation.AnnFamily','ApiAnnotation.AnnDcolon',
Jan Stolarek's avatar
Jan Stolarek committed
470 471 472 473
    --             'ApiAnnotation.AnnWhere','ApiAnnotation.AnnOpenP',
    --             'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnCloseP',
    --             'ApiAnnotation.AnnEqual','ApiAnnotation.AnnRarrow',
    --             'ApiAnnotation.AnnVbar'
Alan Zimmerman's avatar
Alan Zimmerman committed
474

475
    -- For details on above see note [Api annotations] in ApiAnnotation
476
    FamDecl { tcdFam :: FamilyDecl name }
477

478
  | -- | @type@ declaration
Alan Zimmerman's avatar
Alan Zimmerman committed
479 480 481
    --
    --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnType',
    --             'ApiAnnotation.AnnEqual',
482 483

    -- For details on above see note [Api annotations] in ApiAnnotation
484
    SynDecl { tcdLName  :: Located name            -- ^ Type constructor
485
            , tcdTyVars :: LHsTyVarBndrs name      -- ^ Type variables; for an associated type
486
                                                  --   these include outer binders
487
            , tcdRhs    :: LHsType name            -- ^ RHS of type declaration
488
            , tcdFVs    :: PostRn name NameSet }
489 490

  | -- | @data@ declaration
Alan Zimmerman's avatar
Alan Zimmerman committed
491 492 493 494
    --
    --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnData',
    --              'ApiAnnotation.AnnFamily',
    --              'ApiAnnotation.AnnNewType',
495 496
    --              'ApiAnnotation.AnnNewType','ApiAnnotation.AnnDcolon'
    --              'ApiAnnotation.AnnWhere',
497 498

    -- For details on above see note [Api annotations] in ApiAnnotation
499
    DataDecl { tcdLName    :: Located name        -- ^ Type constructor
Gabor Greif's avatar
Gabor Greif committed
500
             , tcdTyVars   :: LHsTyVarBndrs name  -- ^ Type variables; for an associated type
501
                                                  --   these include outer binders
502 503 504
                                                  -- Eg  class T a where
                                                  --       type F a :: *
                                                  --       type F a = a -> a
505
                                                  -- Here the type decl for 'f' includes 'a'
506
                                                  -- in its tcdTyVars
507
             , tcdDataDefn :: HsDataDefn name
508
             , tcdFVs      :: PostRn name NameSet }
509

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
510 511
  | ClassDecl { tcdCtxt    :: LHsContext name,          -- ^ Context...
                tcdLName   :: Located name,             -- ^ Name of the class
512
                tcdTyVars  :: LHsTyVarBndrs name,       -- ^ Class type variables
Alan Zimmerman's avatar
Alan Zimmerman committed
513 514
                tcdFDs     :: [Located (FunDep (Located name))],
                                                        -- ^ Functional deps
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
515 516
                tcdSigs    :: [LSig name],              -- ^ Methods' signatures
                tcdMeths   :: LHsBinds name,            -- ^ Default methods
Alan Zimmerman's avatar
Alan Zimmerman committed
517
                tcdATs     :: [LFamilyDecl name],       -- ^ Associated types;
518
                tcdATDefs  :: [LTyFamDefltEqn name],    -- ^ Associated type defaults
519
                tcdDocs    :: [LDocDecl],               -- ^ Haddock docs
520
                tcdFVs     :: PostRn name NameSet
521
    }
Alan Zimmerman's avatar
Alan Zimmerman committed
522 523 524 525 526 527
        -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnClass',
        --           'ApiAnnotation.AnnWhere','ApiAnnotation.AnnOpen',
        --           'ApiAnnotation.AnnClose'
        --   - The tcdFDs will have 'ApiAnnotation.AnnVbar',
        --                          'ApiAnnotation.AnnComma'
        --                          'ApiAnnotation.AnnRarrow'
528

529 530
        -- For details on above see note [Api annotations] in ApiAnnotation

531 532
  deriving (Typeable)
deriving instance (DataId id) => Data (TyClDecl id)
533

534 535 536 537 538 539 540 541
 -- This is used in TcTyClsDecls to represent
 -- strongly connected components of decls
 -- No familiy instances in here
 -- The role annotations must be grouped with their decls for the
 -- type-checker to infer roles correctly
data TyClGroup name
  = TyClGroup { group_tyclds :: [LTyClDecl name]
              , group_roles  :: [LRoleAnnotDecl name] }
542 543
    deriving (Typeable)
deriving instance (DataId id) => Data (TyClGroup id)
544 545 546 547 548 549 550

tyClGroupConcat :: [TyClGroup name] -> [LTyClDecl name]
tyClGroupConcat = concatMap group_tyclds

mkTyClGroup :: [LTyClDecl name] -> TyClGroup name
mkTyClGroup decls = TyClGroup { group_tyclds = decls, group_roles = [] }

dreixel's avatar
dreixel committed
551

Jan Stolarek's avatar
Jan Stolarek committed
552 553
-- Simple classifiers for TyClDecl
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
554

555
-- | @True@ <=> argument is a @data@\/@newtype@
556 557
-- declaration.
isDataDecl :: TyClDecl name -> Bool
558 559
isDataDecl (DataDecl {}) = True
isDataDecl _other        = False
560

561 562
-- | type or type instance declaration
isSynDecl :: TyClDecl name -> Bool
563 564
isSynDecl (SynDecl {})   = True
isSynDecl _other        = False
565

566 567
-- | type class
isClassDecl :: TyClDecl name -> Bool
568
isClassDecl (ClassDecl {}) = True
569
isClassDecl _              = False
570

571
-- | type/data family declaration
572
isFamilyDecl :: TyClDecl name -> Bool
573
isFamilyDecl (FamDecl {})  = True
574
isFamilyDecl _other        = False
575 576 577

-- | type family declaration
isTypeFamilyDecl :: TyClDecl name -> Bool
578 579 580 581 582
isTypeFamilyDecl (FamDecl (FamilyDecl { fdInfo = info })) = case info of
  OpenTypeFamily      -> True
  ClosedTypeFamily {} -> True
  _                   -> False
isTypeFamilyDecl _ = False
583

584 585 586 587 588 589 590 591 592 593
-- | open type family info
isOpenTypeFamilyInfo :: FamilyInfo name -> Bool
isOpenTypeFamilyInfo OpenTypeFamily = True
isOpenTypeFamilyInfo _              = False

-- | closed type family info
isClosedTypeFamilyInfo :: FamilyInfo name -> Bool
isClosedTypeFamilyInfo (ClosedTypeFamily {}) = True
isClosedTypeFamilyInfo _                     = False

594 595
-- | data family declaration
isDataFamilyDecl :: TyClDecl name -> Bool
596
isDataFamilyDecl (FamDecl (FamilyDecl { fdInfo = DataFamily })) = True
597
isDataFamilyDecl _other      = False
598

Austin Seipp's avatar
Austin Seipp committed
599
-- Dealing with names
600

601
tyFamInstDeclName :: TyFamInstDecl name -> name
602 603
tyFamInstDeclName = unLoc . tyFamInstDeclLName

604
tyFamInstDeclLName :: TyFamInstDecl name -> Located name
605
tyFamInstDeclLName (TyFamInstDecl { tfid_eqn =
606
                     (L _ (TyFamEqn { tfe_tycon = ln })) })
607 608 609 610 611
  = ln

tyClDeclLName :: TyClDecl name -> Located name
tyClDeclLName (FamDecl { tcdFam = FamilyDecl { fdLName = ln } }) = ln
tyClDeclLName decl = tcdLName decl
612

613
tcdName :: TyClDecl name -> name
614 615
tcdName = unLoc . tyClDeclLName

616
tyClDeclTyVars :: TyClDecl name -> LHsTyVarBndrs name
617 618
tyClDeclTyVars (FamDecl { tcdFam = FamilyDecl { fdTyVars = tvs } }) = tvs
tyClDeclTyVars d = tcdTyVars d
619

620 621
countTyClDecls :: [TyClDecl name] -> (Int, Int, Int, Int, Int)
        -- class, synonym decls, data, newtype, family decls
622
countTyClDecls decls
623 624 625 626
 = (count isClassDecl    decls,
    count isSynDecl      decls,  -- excluding...
    count isDataTy       decls,  -- ...family...
    count isNewTy        decls,  -- ...instances
627
    count isFamilyDecl   decls)
sof's avatar
sof committed
628
 where
629 630
   isDataTy DataDecl{ tcdDataDefn = HsDataDefn { dd_ND = DataType } } = True
   isDataTy _                                                       = False
631

632 633
   isNewTy DataDecl{ tcdDataDefn = HsDataDefn { dd_ND = NewType } } = True
   isNewTy _                                                      = False
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

-- | Does this declaration have a complete, user-supplied kind signature?
-- See Note [Complete user-supplied kind signatures]
hsDeclHasCusk :: TyClDecl name -> Bool
hsDeclHasCusk (FamDecl { tcdFam = fam_decl }) = famDeclHasCusk fam_decl
hsDeclHasCusk (SynDecl { tcdTyVars = tyvars, tcdRhs = rhs })
  = hsTvbAllKinded tyvars && rhs_annotated rhs
  where
    rhs_annotated (L _ ty) = case ty of
      HsParTy lty  -> rhs_annotated lty
      HsKindSig {} -> True
      _            -> False
hsDeclHasCusk (DataDecl { tcdTyVars = tyvars })  = hsTvbAllKinded tyvars
hsDeclHasCusk (ClassDecl { tcdTyVars = tyvars }) = hsTvbAllKinded tyvars

Jan Stolarek's avatar
Jan Stolarek committed
649 650
-- Pretty-printing TyClDecl
-- ~~~~~~~~~~~~~~~~~~~~~~~~
651

652
instance OutputableBndr name
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
653
              => Outputable (TyClDecl name) where
654

655 656 657 658
    ppr (FamDecl { tcdFam = decl }) = ppr decl
    ppr (SynDecl { tcdLName = ltycon, tcdTyVars = tyvars, tcdRhs = rhs })
      = hang (ptext (sLit "type") <+>
              pp_vanilla_decl_head ltycon tyvars [] <+> equals)
659
          4 (ppr rhs)
660

661 662
    ppr (DataDecl { tcdLName = ltycon, tcdTyVars = tyvars, tcdDataDefn = defn })
      = pp_data_defn (pp_vanilla_decl_head ltycon tyvars) defn
663

664
    ppr (ClassDecl {tcdCtxt = context, tcdLName = lclas, tcdTyVars = tyvars,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
665
                    tcdFDs  = fds,
666 667 668
                    tcdSigs = sigs, tcdMeths = methods,
                    tcdATs = ats, tcdATDefs = at_defs})
      | null sigs && isEmptyBag methods && null ats && null at_defs -- No "where" part
669 670
      = top_matter

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
671
      | otherwise       -- Laid out
672
      = vcat [ top_matter <+> ptext (sLit "where")
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
673
             , nest 2 $ pprDeclList (map ppr ats ++
674
                                     map ppr_fam_deflt_eqn at_defs ++
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
675
                                     pprLHsBindsForUser methods sigs) ]
676
      where
677
        top_matter = ptext (sLit "class")
678
                     <+> pp_vanilla_decl_head lclas tyvars (unLoc context)
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
679
                     <+> pprFundeps (map unLoc fds)
680

681 682 683 684 685
instance OutputableBndr name => Outputable (TyClGroup name) where
  ppr (TyClGroup { group_tyclds = tyclds, group_roles = roles })
    = ppr tyclds $$
      ppr roles

Jan Stolarek's avatar
Jan Stolarek committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
pp_vanilla_decl_head :: OutputableBndr name
   => Located name
   -> LHsTyVarBndrs name
   -> HsContext name
   -> SDoc
pp_vanilla_decl_head thing tyvars context
 = hsep [pprHsContext context, pprPrefixOcc (unLoc thing), ppr tyvars]

pprTyClDeclFlavour :: TyClDecl a -> SDoc
pprTyClDeclFlavour (ClassDecl {})   = ptext (sLit "class")
pprTyClDeclFlavour (SynDecl {})     = ptext (sLit "type")
pprTyClDeclFlavour (FamDecl { tcdFam = FamilyDecl { fdInfo = info }})
  = pprFlavour info
pprTyClDeclFlavour (DataDecl { tcdDataDefn = HsDataDefn { dd_ND = nd } })
  = ppr nd


{- *********************************************************************
*                                                                      *
               Data and type family declarations
*                                                                      *
********************************************************************* -}

-- Note [FamilyResultSig]
-- ~~~~~~~~~~~~~~~~~~~~~~
--
-- This data type represents the return signature of a type family.  Possible
-- values are:
--
--  * NoSig - the user supplied no return signature:
--       type family Id a where ...
--
--  * KindSig - the user supplied the return kind:
--       type family Id a :: * where ...
--
--  * TyVarSig - user named the result with a type variable and possibly
--    provided a kind signature for that variable:
--       type family Id a = r where ...
--       type family Id a = (r :: *) where ...
--
--    Naming result of a type family is required if we want to provide
--    injectivity annotation for a type family:
--       type family Id a = r | r -> a where ...
--
-- See also: Note [Injectivity annotation]

-- Note [Injectivity annotation]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- A user can declare a type family to be injective:
--
--    type family Id a = r | r -> a where ...
--
--  * The part after the "|" is called "injectivity annotation".
--  * "r -> a" part is called "injectivity condition"; at the moment terms
--    "injectivity annotation" and "injectivity condition" are synonymous
--    because we only allow a single injectivity condition.
--  * "r" is the "LHS of injectivity condition". LHS can only contain the
--    variable naming the result of a type family.

--  * "a" is the "RHS of injectivity condition". RHS contains space-separated
--    type and kind variables representing the arguments of a type
--    family. Variables can be omitted if a type family is not injective in
--    these arguments. Example:
--          type family Foo a b c = d | d -> a c where ...
--
-- Note that:
--  a) naming of type family result is required to provide injectivity
--     annotation
--  b) for associated types if the result was named then injectivity annotation
--     is mandatory. Otherwise result type variable is indistinguishable from
--     associated type default.
--
-- It is possible that in the future this syntax will be extended to support
-- more complicated injectivity annotations. For example we could declare that
-- if we know the result of Plus and one of its arguments we can determine the
-- other argument:
--
--    type family Plus a b = (r :: Nat) | r a -> b, r b -> a where ...
--
-- Here injectivity annotation would consist of two comma-separated injectivity
-- conditions.
--
-- See also Note [Injective type families] in TyCon

type LFamilyResultSig name = Located (FamilyResultSig name)
data FamilyResultSig name = -- see Note [FamilyResultSig]
    NoSig
  -- ^ - 'ApiAnnotation.AnnKeywordId' :

  -- For details on above see note [Api annotations] in ApiAnnotation

  | KindSig  (LHsKind name)
  -- ^ - 'ApiAnnotation.AnnKeywordId' :
  --             'ApiAnnotation.AnnOpenP','ApiAnnotation.AnnDcolon',
  --             'ApiAnnotation.AnnCloseP'

  -- For details on above see note [Api annotations] in ApiAnnotation

  | TyVarSig (LHsTyVarBndr name)
  -- ^ - 'ApiAnnotation.AnnKeywordId' :
  --             'ApiAnnotation.AnnOpenP','ApiAnnotation.AnnDcolon',
  --             'ApiAnnotation.AnnCloseP', 'ApiAnnotation.AnnEqual'

  -- For details on above see note [Api annotations] in ApiAnnotation

  deriving ( Typeable )
deriving instance (DataId name) => Data (FamilyResultSig name)

type LFamilyDecl name = Located (FamilyDecl name)
data FamilyDecl name = FamilyDecl
  { fdInfo           :: FamilyInfo name              -- type/data, closed/open
  , fdLName          :: Located name                 -- type constructor
  , fdTyVars         :: LHsTyVarBndrs name           -- type variables
  , fdResultSig      :: LFamilyResultSig name        -- result signature
  , fdInjectivityAnn :: Maybe (LInjectivityAnn name) -- optional injectivity ann
  }
  -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnType',
  --             'ApiAnnotation.AnnData', 'ApiAnnotation.AnnFamily',
  --             'ApiAnnotation.AnnWhere', 'ApiAnnotation.AnnOpenP',
  --             'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnCloseP',
  --             'ApiAnnotation.AnnEqual', 'ApiAnnotation.AnnRarrow',
  --             'ApiAnnotation.AnnVbar'

  -- For details on above see note [Api annotations] in ApiAnnotation
  deriving ( Typeable )

deriving instance (DataId id) => Data (FamilyDecl id)

type LInjectivityAnn name = Located (InjectivityAnn name)

-- | If the user supplied an injectivity annotation it is represented using
-- InjectivityAnn. At the moment this is a single injectivity condition - see
-- Note [Injectivity annotation]. `Located name` stores the LHS of injectivity
-- condition. `[Located name]` stores the RHS of injectivity condition. Example:
--
--   type family Foo a b c = r | r -> a c where ...
--
-- This will be represented as "InjectivityAnn `r` [`a`, `c`]"
data InjectivityAnn name
  = InjectivityAnn (Located name) [Located name]
  -- ^ - 'ApiAnnotation.AnnKeywordId' :
  --             'ApiAnnotation.AnnRarrow', 'ApiAnnotation.AnnVbar'

  -- For details on above see note [Api annotations] in ApiAnnotation
  deriving ( Data, Typeable )

data FamilyInfo name
  = DataFamily
  | OpenTypeFamily
     -- | 'Nothing' if we're in an hs-boot file and the user
     -- said "type family Foo x where .."
  | ClosedTypeFamily (Maybe [LTyFamInstEqn name])
  deriving( Typeable )
deriving instance (DataId name) => Data (FamilyInfo name)

-- | Does this family declaration have a complete, user-supplied kind signature?
famDeclHasCusk :: FamilyDecl name -> Bool
famDeclHasCusk (FamilyDecl { fdInfo      = ClosedTypeFamily _
                           , fdTyVars    = tyvars
                           , fdResultSig = L _ resultSig })
  = hsTvbAllKinded tyvars && hasReturnKindSignature resultSig
famDeclHasCusk _ = True  -- all open families have CUSKs!

-- | Does this family declaration have user-supplied return kind signature?
hasReturnKindSignature :: FamilyResultSig a -> Bool
hasReturnKindSignature NoSig                          = False
hasReturnKindSignature (TyVarSig (L _ (UserTyVar _))) = False
hasReturnKindSignature _                              = True

-- | Maybe return name of the result type variable
resultVariableName :: FamilyResultSig a -> Maybe a
resultVariableName (TyVarSig sig) = Just $ hsLTyVarName sig
resultVariableName _              = Nothing

{-
Note [Complete user-supplied kind signatures]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We kind-check declarations differently if they have a complete, user-supplied
kind signature (CUSK). This is because we can safely generalise a CUSKed
declaration before checking all of the others, supporting polymorphic recursion.
See ghc.haskell.org/trac/ghc/wiki/GhcKinds/KindInference#Proposednewstrategy
and #9200 for lots of discussion of how we got here.

A declaration has a CUSK if we can know its complete kind without doing any
inference, at all. Here are the rules:

 - A class or datatype is said to have a CUSK if and only if all of its type
variables are annotated. Its result kind is, by construction, Constraint or *
respectively.

 - A type synonym has a CUSK if and only if all of its type variables and its
RHS are annotated with kinds.

 - A closed type family is said to have a CUSK if and only if all of its type
variables and its return type are annotated.

 - An open type family always has a CUSK -- unannotated type variables (and
return type) default to *.
-}

887
instance (OutputableBndr name) => Outputable (FamilyDecl name) where
Jan Stolarek's avatar
Jan Stolarek committed
888 889 890 891 892
  ppr (FamilyDecl { fdInfo = info, fdLName = ltycon
                  , fdTyVars = tyvars, fdResultSig = L _ result
                  , fdInjectivityAnn = mb_inj })
      = vcat [ pprFlavour info <+> pp_vanilla_decl_head ltycon tyvars [] <+>
               pp_kind <+> pp_inj <+> pp_where
893
             , nest 2 $ pp_eqns ]
894
        where
Jan Stolarek's avatar
Jan Stolarek committed
895 896 897 898 899 900 901 902
          pp_kind = case result of
                      NoSig            -> empty
                      KindSig  kind    -> dcolon <+> ppr kind
                      TyVarSig tv_bndr -> text "=" <+> ppr tv_bndr
          pp_inj = case mb_inj of
                     Just (L _ (InjectivityAnn lhs rhs)) ->
                       hsep [ text "|", ppr lhs, text "->", hsep (map ppr rhs) ]
                     Nothing -> empty
903
          (pp_where, pp_eqns) = case info of
904 905 906 907 908 909
            ClosedTypeFamily mb_eqns ->
              ( ptext (sLit "where")
              , case mb_eqns of
                  Nothing   -> ptext (sLit "..")
                  Just eqns -> vcat $ map ppr_fam_inst_eqn eqns )
            _ -> (empty, empty)
910 911 912 913 914

pprFlavour :: FamilyInfo name -> SDoc
pprFlavour DataFamily            = ptext (sLit "data family")
pprFlavour OpenTypeFamily        = ptext (sLit "type family")
pprFlavour (ClosedTypeFamily {}) = ptext (sLit "type family")
915

916 917
instance Outputable (FamilyInfo name) where
  ppr = pprFlavour
918

919

920

Jan Stolarek's avatar
Jan Stolarek committed
921
{- *********************************************************************
Austin Seipp's avatar
Austin Seipp committed
922
*                                                                      *
Jan Stolarek's avatar
Jan Stolarek committed
923
               Data types and data constructors
Austin Seipp's avatar
Austin Seipp committed
924
*                                                                      *
Jan Stolarek's avatar
Jan Stolarek committed
925
********************************************************************* -}
926 927 928 929

data HsDataDefn name   -- The payload of a data type defn
                       -- Used *both* for vanilla data declarations,
                       --       *and* for data family instances
Gabor Greif's avatar
Gabor Greif committed
930
  = -- | Declares a data type or newtype, giving its constructors
931 932 933 934 935 936
    -- @
    --  data/newtype T a = <constrs>
    --  data/newtype instance T [a] = <constrs>
    -- @
    HsDataDefn { dd_ND     :: NewOrData,
                 dd_ctxt   :: LHsContext name,           -- ^ Context
937
                 dd_cType  :: Maybe (Located CType),
938 939 940
                 dd_kindSig:: Maybe (LHsKind name),
                     -- ^ Optional kind signature.
                     --
941
                     -- @(Just k)@ for a GADT-style @data@,
942 943 944 945 946 947 948 949 950 951 952 953
                     -- or @data instance@ decl, with explicit kind sig
                     --
                     -- Always @Nothing@ for H98-syntax decls

                 dd_cons   :: [LConDecl name],
                     -- ^ Data constructors
                     --
                     -- For @data T a = T1 | T2 a@
                     --   the 'LConDecl's all have 'ResTyH98'.
                     -- For @data T a where { T1 :: T a }@
                     --   the 'LConDecls' all have 'ResTyGADT'.

954
                 dd_derivs :: Maybe (Located [LHsType name])
955 956 957 958 959 960 961 962 963
                     -- ^ Derivings; @Nothing@ => not specified,
                     --              @Just []@ => derive exactly what is asked
                     --
                     -- These "types" must be of form
                     -- @
                     --      forall ab. C ty1 ty2
                     -- @
                     -- Typically the foralls and ty args are empty, but they
                     -- are non-empty for the newtype-deriving case
Alan Zimmerman's avatar
Alan Zimmerman committed
964 965 966 967
                     --
                     --  - 'ApiAnnotation.AnnKeywordId' :
                     --       'ApiAnnotation.AnnDeriving',
                     --       'ApiAnnotation.AnnOpen','ApiAnnotation.AnnClose'
968 969

             -- For details on above see note [Api annotations] in ApiAnnotation
Alan Zimmerman's avatar
Alan Zimmerman committed
970
   }
971 972
    deriving( Typeable )
deriving instance (DataId id) => Data (HsDataDefn id)
973 974 975 976 977 978

data NewOrData
  = NewType                     -- ^ @newtype Blah ...@
  | DataType                    -- ^ @data Blah ...@
  deriving( Eq, Data, Typeable )                -- Needed because Demand derives Eq

979
type LConDecl name = Located (ConDecl name)
Alan Zimmerman's avatar
Alan Zimmerman committed
980 981
      -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSemi' when
      --   in a GADT constructor list
982

983 984
  -- For details on above see note [Api annotations] in ApiAnnotation

Alan Zimmerman's avatar
Alan Zimmerman committed
985 986 987
-- |
--
-- @
988 989
-- data T b = forall a. Eq a => MkT a b
--   MkT :: forall b a. Eq a => MkT a b