HsDecls.hs 74.7 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 7
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveFoldable,
             DeriveTraversable #-}
8 9 10 11 12
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
                                      -- in module PlaceHolder
{-# LANGUAGE ConstraintKinds #-}
13
{-# LANGUAGE FlexibleInstances #-}
14

15 16
-- | Abstract syntax of global declarations.
--
17
-- Definitions for: @SynDecl@ and @ConDecl@, @ClassDecl@,
18
-- @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
19
module HsDecls (
20
  -- * Toplevel declarations
21 22
  HsDecl(..), LHsDecl, HsDataDefn(..), HsDeriving,

23
  -- ** Class or type declarations
24 25
  TyClDecl(..), LTyClDecl,
  TyClGroup(..), tyClGroupConcat, mkTyClGroup,
26 27
  isClassDecl, isDataDecl, isSynDecl, tcdName,
  isFamilyDecl, isTypeFamilyDecl, isDataFamilyDecl,
28
  isOpenTypeFamilyInfo, isClosedTypeFamilyInfo,
29 30 31
  tyFamInstDeclName, tyFamInstDeclLName,
  countTyClDecls, pprTyClDeclFlavour,
  tyClDeclLName, tyClDeclTyVars,
32
  hsDeclHasCusk, famDeclHasCusk,
33
  FamilyDecl(..), LFamilyDecl,
34

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

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

  -- * Grouping
82
  HsGroup(..),  emptyRdrGroup, emptyRnGroup, appendGroups
83

84
    ) where
85 86

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

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

-- others:
102
import InstEnv
103
import Class
104
import Outputable
105 106 107
import Util
import SrcLoc

108
import Bag
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
109
import Maybes
110
import Data.Data        hiding (TyCon,Fixity)
111

Austin Seipp's avatar
Austin Seipp committed
112 113 114
{-
************************************************************************
*                                                                      *
115
\subsection[HsDecl]{Declarations}
Austin Seipp's avatar
Austin Seipp committed
116 117 118
*                                                                      *
************************************************************************
-}
119

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

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

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

147 148

-- NB: all top-level fixity decls are contained EITHER
149
-- EITHER SigDs
150 151 152
-- OR     in the ClassDecls in TyClDs
--
-- The former covers
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
153 154 155 156 157
--      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
158 159
--
-- The latter is for class methods only
160

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

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

174 175 176
        hs_instds  :: [LInstDecl id],
                -- Both class and family instance declarations in here

177
        hs_derivds :: [LDerivDecl id],
178

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

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

190
        hs_docs   :: [LDocDecl]
191 192
  } deriving (Typeable)
deriving instance (DataId id) => Data (HsGroup id)
193

194 195 196 197
emptyGroup, emptyRdrGroup, emptyRnGroup :: HsGroup a
emptyRdrGroup = emptyGroup { hs_valds = emptyValBindsIn }
emptyRnGroup  = emptyGroup { hs_valds = emptyValBindsOut }

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

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

252
instance OutputableBndr name => Outputable (HsDecl name) where
253 254 255 256 257 258 259 260
    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
261
    ppr (VectD vect)            = ppr vect
Ian Lynagh's avatar
Ian Lynagh committed
262
    ppr (WarningD wd)           = ppr wd
263
    ppr (AnnD ad)               = ppr ad
264 265
    ppr (SpliceD dd)            = ppr dd
    ppr (DocD doc)              = ppr doc
266
    ppr (RoleAnnotD ra)         = ppr ra
267 268 269

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
270 271
                   hs_tyclds = tycl_decls,
                   hs_instds = inst_decls,
272
                   hs_derivds = deriv_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
273 274 275 276 277 278 279
                   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 })
280 281
        = vcat_mb empty
            [ppr_ds fix_decls, ppr_ds default_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
282 283 284
             ppr_ds deprec_decls, ppr_ds ann_decls,
             ppr_ds rule_decls,
             ppr_ds vect_decls,
285 286
             if isEmptyValBinds val_decls
                then Nothing
287
                else Just (ppr val_decls),
288
             ppr_ds (tyClGroupConcat tycl_decls),
289
             ppr_ds inst_decls,
290
             ppr_ds deriv_decls,
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
291 292
             ppr_ds foreign_decls]
        where
293
          ppr_ds :: Outputable a => [a] -> Maybe SDoc
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
294 295
          ppr_ds [] = Nothing
          ppr_ds ds = Just (vcat (map ppr ds))
296 297

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

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

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

instance OutputableBndr name => Outputable (SpliceDecl name) where
316
   ppr (SpliceDecl (L _ e) _) = pprSplice e
317

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

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
325 326 327
                --------------------------------
                        THE NAMING STORY
                --------------------------------
328

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

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

343 344
All have occurrence names that are derived uniquely from their parent
declaration.
345 346 347 348 349 350 351

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:
352
 - Ensure they "point to" the parent data/class decl
353
   when loading that decl from an interface file
354 355 356 357 358
   (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
359

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

363 364 365 366 367 368
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
369 370
   the ClassOpSig (in HsBinds), in the DefMethInfo field.
   (DefMethInfo is defined in Class.hs)
371 372 373 374 375 376 377

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:
378

379 380 381 382 383
 - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName
   This is done by RdrHsSyn.mkClassOpSigDM

 - The renamer renames it to a Name

384
 - During typechecking, we generate a binding for each $dm for
385
   which there's a programmer-supplied default method:
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
386 387 388 389
        class Foo a where
          op1 :: <type>
          op2 :: <type>
          op1 = ...
390
   We generate a binding for $dmop1 but not for $dmop2.
391 392
   The Class for Foo has a Nothing for op2 and
                         a Just ($dm_op1, VanillaDM) for op1.
393 394 395 396 397
   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
398 399 400
        class Foo a where
          op1 = :: <type>       -- NB the '='
          op2   :: <type>
401 402 403 404 405
    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
406
    the binding for $dmop1 will be sucked in.  (See RnHsSyn.tyClDeclFVs)
407 408 409 410 411 412 413 414 415 416 417 418 419 420
    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
421 422
        instance {Eq Int} = dEqInt
        dEqInt :: {Eq Int} <pragma info>
423 424 425 426 427 428 429 430 431 432 433

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').

434
  - The occurrence name it chooses is derived from the instance decl (just for
435 436
    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
437 438
        instance Foo [Int]  where ...
        instance Foo [Bool] where ...
439 440
    These might both be dFooList

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

444
  - We can take this relaxed approach (changing the occurrence name later)
445 446 447 448 449 450 451 452 453
    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.
454

455 456
  - 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
457
-}
458

459 460
type LTyClDecl name = Located (TyClDecl name)

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

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

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

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

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

    -- For details on above see note [Api annotations] in ApiAnnotation
497
    DataDecl { tcdLName    :: Located name        -- ^ Type constructor
498
             , tcdTyVars   :: LHsQTyVars name  -- ^ Type variables; for an associated type
499
                                                  --   these include outer binders
500 501 502
                                                  -- Eg  class T a where
                                                  --       type F a :: *
                                                  --       type F a = a -> a
503
                                                  -- Here the type decl for 'f' includes 'a'
504
                                                  -- in its tcdTyVars
505
             , tcdDataDefn :: HsDataDefn name
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
506
             , tcdDataCusk :: PostRn name Bool    -- ^ does this have a CUSK?
507
             , tcdFVs      :: PostRn name NameSet }
508

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

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

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

533 534 535 536 537 538 539 540
 -- 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] }
541 542
    deriving (Typeable)
deriving instance (DataId id) => Data (TyClGroup id)
543 544 545 546 547 548 549

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

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

Jan Stolarek's avatar
Jan Stolarek committed
550 551
-- Simple classifiers for TyClDecl
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
552

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

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

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

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

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

582 583 584 585 586 587 588 589 590 591
-- | 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

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

Austin Seipp's avatar
Austin Seipp committed
597
-- Dealing with names
598

599
tyFamInstDeclName :: TyFamInstDecl name -> name
600 601
tyFamInstDeclName = unLoc . tyFamInstDeclLName

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

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

611
tcdName :: TyClDecl name -> name
612 613
tcdName = unLoc . tyClDeclLName

614
tyClDeclTyVars :: TyClDecl name -> LHsQTyVars name
615 616
tyClDeclTyVars (FamDecl { tcdFam = FamilyDecl { fdTyVars = tvs } }) = tvs
tyClDeclTyVars d = tcdTyVars d
617

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

630 631
   isNewTy DataDecl{ tcdDataDefn = HsDataDefn { dd_ND = NewType } } = True
   isNewTy _                                                      = False
632 633 634

-- | Does this declaration have a complete, user-supplied kind signature?
-- See Note [Complete user-supplied kind signatures]
635
hsDeclHasCusk :: TyClDecl Name -> Bool
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
636
hsDeclHasCusk (FamDecl { tcdFam = fam_decl }) = famDeclHasCusk Nothing fam_decl
637 638 639 640 641 642 643
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
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
644
hsDeclHasCusk (DataDecl { tcdDataCusk = cusk }) = cusk
645 646
hsDeclHasCusk (ClassDecl { tcdTyVars = tyvars }) = hsTvbAllKinded tyvars

Jan Stolarek's avatar
Jan Stolarek committed
647 648
-- Pretty-printing TyClDecl
-- ~~~~~~~~~~~~~~~~~~~~~~~~
649

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

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

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

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

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

679 680 681 682 683
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
684 685
pp_vanilla_decl_head :: OutputableBndr name
   => Located name
686
   -> LHsQTyVars name
Jan Stolarek's avatar
Jan Stolarek committed
687 688 689 690 691 692
   -> HsContext name
   -> SDoc
pp_vanilla_decl_head thing tyvars context
 = hsep [pprHsContext context, pprPrefixOcc (unLoc thing), ppr tyvars]

pprTyClDeclFlavour :: TyClDecl a -> SDoc
693 694
pprTyClDeclFlavour (ClassDecl {})   = text "class"
pprTyClDeclFlavour (SynDecl {})     = text "type"
Jan Stolarek's avatar
Jan Stolarek committed
695
pprTyClDeclFlavour (FamDecl { tcdFam = FamilyDecl { fdInfo = info }})
696
  = pprFlavour info <+> text "family"
Jan Stolarek's avatar
Jan Stolarek committed
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
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
797
  , fdTyVars         :: LHsQTyVars name              -- type variables
Jan Stolarek's avatar
Jan Stolarek committed
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
  , 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?
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
841 842 843 844 845 846
famDeclHasCusk :: Maybe Bool
                   -- ^ if associated, does the enclosing class have a CUSK?
               -> FamilyDecl name -> Bool
famDeclHasCusk _ (FamilyDecl { fdInfo      = ClosedTypeFamily _
                             , fdTyVars    = tyvars
                             , fdResultSig = L _ resultSig })
Jan Stolarek's avatar
Jan Stolarek committed
847
  = hsTvbAllKinded tyvars && hasReturnKindSignature resultSig
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
848 849
famDeclHasCusk mb_class_cusk _ = mb_class_cusk `orElse` True
        -- all un-associated open families have CUSKs!
Jan Stolarek's avatar
Jan Stolarek committed
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

-- | 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 *.
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
886 887 888 889

 - Additionally, if -XTypeInType is on, then a data definition with a top-level
   :: must explicitly bind all kind variables to the right of the ::.
   See test dependent/should_compile/KindLevels, which requires this case.
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
890 891
   (Naturally, any kind variable mentioned before the :: should not be bound
   after it.)
Jan Stolarek's avatar
Jan Stolarek committed
892 893
-}

894
instance (OutputableBndr name) => Outputable (FamilyDecl name) where
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
  ppr = pprFamilyDecl TopLevel

pprFamilyDecl :: OutputableBndr name => TopLevelFlag -> FamilyDecl name -> SDoc
pprFamilyDecl top_level (FamilyDecl { fdInfo = info, fdLName = ltycon
                                    , fdTyVars = tyvars
                                    , fdResultSig = L _ result
                                    , fdInjectivityAnn = mb_inj })
  = vcat [ pprFlavour info <+> pp_top_level <+>
           pp_vanilla_decl_head ltycon tyvars [] <+>
           pp_kind <+> pp_inj <+> pp_where
         , nest 2 $ pp_eqns ]
  where
    pp_top_level = case top_level of
                     TopLevel    -> text "family"
                     NotTopLevel -> empty

    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)) ->
917
                 hsep [ vbar, ppr lhs, text "->", hsep (map ppr rhs) ]
918 919 920
               Nothing -> empty
    (pp_where, pp_eqns) = case info of
      ClosedTypeFamily mb_eqns ->
921
        ( text "where"
922
        , case mb_eqns of
923
            Nothing   -> text ".."
924 925
            Just eqns -> vcat $ map ppr_fam_inst_eqn eqns )
      _ -> (empty, empty)
926 927

pprFlavour :: FamilyInfo name -> SDoc
928 929 930
pprFlavour DataFamily            = text "data"
pprFlavour OpenTypeFamily        = text "type"
pprFlavour (ClosedTypeFamily {}) = text "type"
931

932
instance Outputable (FamilyInfo name) where
933
  ppr info = pprFlavour info <+> text "family"
934

935

936

Jan Stolarek's avatar
Jan Stolarek committed
937
{- *********************************************************************
Austin Seipp's avatar
Austin Seipp committed
938
*                                                                      *
Jan Stolarek's avatar
Jan Stolarek committed
939
               Data types and data constructors
Austin Seipp's avatar
Austin Seipp committed
940
*                                                                      *
Jan Stolarek's avatar
Jan Stolarek committed
941
********************************************************************* -}
942 943 944 945

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
946
  = -- | Declares a data type or newtype, giving its constructors
947 948 949 950 951 952
    -- @
    --  data/newtype T a = <constrs>
    --  data/newtype instance T [a] = <constrs>
    -- @
    HsDataDefn { dd_ND     :: NewOrData,
                 dd_ctxt   :: LHsContext name,           -- ^ Context
953
                 dd_cType  :: Maybe (Located CType),
954 955