| 1 | % |
|---|
| 2 | % (c) The University of Glasgow 2006 |
|---|
| 3 | % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 |
|---|
| 4 | % |
|---|
| 5 | |
|---|
| 6 | \begin{code} |
|---|
| 7 | {-# LANGUAGE DeriveDataTypeable #-} |
|---|
| 8 | |
|---|
| 9 | -- | Abstract syntax of global declarations. |
|---|
| 10 | -- |
|---|
| 11 | -- Definitions for: @TyDecl@ and @ConDecl@, @ClassDecl@, |
|---|
| 12 | -- @InstDecl@, @DefaultDecl@ and @ForeignDecl@. |
|---|
| 13 | module HsDecls ( |
|---|
| 14 | -- * Toplevel declarations |
|---|
| 15 | HsDecl(..), LHsDecl, HsTyDefn(..), |
|---|
| 16 | -- ** Class or type declarations |
|---|
| 17 | TyClDecl(..), LTyClDecl, TyClGroup, |
|---|
| 18 | isClassDecl, isDataDecl, isSynDecl, isFamilyDecl, |
|---|
| 19 | isHsDataDefn, isHsSynDefn, tcdName, famInstDeclName, |
|---|
| 20 | countTyClDecls, pprTyDefnFlavour, pprTyClDeclFlavour, |
|---|
| 21 | |
|---|
| 22 | -- ** Instance declarations |
|---|
| 23 | InstDecl(..), LInstDecl, NewOrData(..), FamilyFlavour(..), |
|---|
| 24 | FamInstDecl(..), LFamInstDecl, instDeclFamInsts, |
|---|
| 25 | |
|---|
| 26 | -- ** Standalone deriving declarations |
|---|
| 27 | DerivDecl(..), LDerivDecl, |
|---|
| 28 | -- ** @RULE@ declarations |
|---|
| 29 | RuleDecl(..), LRuleDecl, RuleBndr(..), |
|---|
| 30 | collectRuleBndrSigTys, |
|---|
| 31 | -- ** @VECTORISE@ declarations |
|---|
| 32 | VectDecl(..), LVectDecl, |
|---|
| 33 | lvectDeclName, lvectInstDecl, |
|---|
| 34 | -- ** @default@ declarations |
|---|
| 35 | DefaultDecl(..), LDefaultDecl, |
|---|
| 36 | -- ** Top-level template haskell splice |
|---|
| 37 | SpliceDecl(..), |
|---|
| 38 | -- ** Foreign function interface declarations |
|---|
| 39 | ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..), |
|---|
| 40 | noForeignImportCoercionYet, noForeignExportCoercionYet, |
|---|
| 41 | CImportSpec(..), |
|---|
| 42 | -- ** Data-constructor declarations |
|---|
| 43 | ConDecl(..), LConDecl, ResType(..), |
|---|
| 44 | HsConDeclDetails, hsConDeclArgTys, |
|---|
| 45 | -- ** Document comments |
|---|
| 46 | DocDecl(..), LDocDecl, docDeclDoc, |
|---|
| 47 | -- ** Deprecations |
|---|
| 48 | WarnDecl(..), LWarnDecl, |
|---|
| 49 | -- ** Annotations |
|---|
| 50 | AnnDecl(..), LAnnDecl, |
|---|
| 51 | AnnProvenance(..), annProvenanceName_maybe, modifyAnnProvenanceNameM, |
|---|
| 52 | |
|---|
| 53 | -- * Grouping |
|---|
| 54 | HsGroup(..), emptyRdrGroup, emptyRnGroup, appendGroups |
|---|
| 55 | ) where |
|---|
| 56 | |
|---|
| 57 | -- friends: |
|---|
| 58 | import {-# SOURCE #-} HsExpr( LHsExpr, HsExpr, pprExpr ) |
|---|
| 59 | -- Because Expr imports Decls via HsBracket |
|---|
| 60 | |
|---|
| 61 | import HsBinds |
|---|
| 62 | import HsPat |
|---|
| 63 | import HsTypes |
|---|
| 64 | import HsDoc |
|---|
| 65 | import TyCon |
|---|
| 66 | import NameSet |
|---|
| 67 | import Name |
|---|
| 68 | import BasicTypes |
|---|
| 69 | import Coercion |
|---|
| 70 | import ForeignCall |
|---|
| 71 | |
|---|
| 72 | -- others: |
|---|
| 73 | import InstEnv |
|---|
| 74 | import Class |
|---|
| 75 | import Outputable |
|---|
| 76 | import Util |
|---|
| 77 | import SrcLoc |
|---|
| 78 | import FastString |
|---|
| 79 | |
|---|
| 80 | import Bag |
|---|
| 81 | import Control.Monad ( liftM ) |
|---|
| 82 | import Data.Data hiding (TyCon) |
|---|
| 83 | \end{code} |
|---|
| 84 | |
|---|
| 85 | %************************************************************************ |
|---|
| 86 | %* * |
|---|
| 87 | \subsection[HsDecl]{Declarations} |
|---|
| 88 | %* * |
|---|
| 89 | %************************************************************************ |
|---|
| 90 | |
|---|
| 91 | \begin{code} |
|---|
| 92 | type LHsDecl id = Located (HsDecl id) |
|---|
| 93 | |
|---|
| 94 | -- | A Haskell Declaration |
|---|
| 95 | data HsDecl id |
|---|
| 96 | = TyClD (TyClDecl id) -- ^ A type or class declaration. |
|---|
| 97 | | InstD (InstDecl id) -- ^ An instance declaration. |
|---|
| 98 | | DerivD (DerivDecl id) |
|---|
| 99 | | ValD (HsBind id) |
|---|
| 100 | | SigD (Sig id) |
|---|
| 101 | | DefD (DefaultDecl id) |
|---|
| 102 | | ForD (ForeignDecl id) |
|---|
| 103 | | WarningD (WarnDecl id) |
|---|
| 104 | | AnnD (AnnDecl id) |
|---|
| 105 | | RuleD (RuleDecl id) |
|---|
| 106 | | VectD (VectDecl id) |
|---|
| 107 | | SpliceD (SpliceDecl id) |
|---|
| 108 | | DocD (DocDecl) |
|---|
| 109 | | QuasiQuoteD (HsQuasiQuote id) |
|---|
| 110 | deriving (Data, Typeable) |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | -- NB: all top-level fixity decls are contained EITHER |
|---|
| 114 | -- EITHER SigDs |
|---|
| 115 | -- OR in the ClassDecls in TyClDs |
|---|
| 116 | -- |
|---|
| 117 | -- The former covers |
|---|
| 118 | -- a) data constructors |
|---|
| 119 | -- b) class methods (but they can be also done in the |
|---|
| 120 | -- signatures of class decls) |
|---|
| 121 | -- c) imported functions (that have an IfacSig) |
|---|
| 122 | -- d) top level decls |
|---|
| 123 | -- |
|---|
| 124 | -- The latter is for class methods only |
|---|
| 125 | |
|---|
| 126 | -- | A 'HsDecl' is categorised into a 'HsGroup' before being |
|---|
| 127 | -- fed to the renamer. |
|---|
| 128 | data HsGroup id |
|---|
| 129 | = HsGroup { |
|---|
| 130 | hs_valds :: HsValBinds id, |
|---|
| 131 | |
|---|
| 132 | hs_tyclds :: [[LTyClDecl id]], |
|---|
| 133 | -- A list of mutually-recursive groups |
|---|
| 134 | -- No family-instances here; they are in hs_instds |
|---|
| 135 | -- Parser generates a singleton list; |
|---|
| 136 | -- renamer does dependency analysis |
|---|
| 137 | |
|---|
| 138 | hs_instds :: [LInstDecl id], |
|---|
| 139 | -- Both class and family instance declarations in here |
|---|
| 140 | |
|---|
| 141 | hs_derivds :: [LDerivDecl id], |
|---|
| 142 | |
|---|
| 143 | hs_fixds :: [LFixitySig id], |
|---|
| 144 | -- Snaffled out of both top-level fixity signatures, |
|---|
| 145 | -- and those in class declarations |
|---|
| 146 | |
|---|
| 147 | hs_defds :: [LDefaultDecl id], |
|---|
| 148 | hs_fords :: [LForeignDecl id], |
|---|
| 149 | hs_warnds :: [LWarnDecl id], |
|---|
| 150 | hs_annds :: [LAnnDecl id], |
|---|
| 151 | hs_ruleds :: [LRuleDecl id], |
|---|
| 152 | hs_vects :: [LVectDecl id], |
|---|
| 153 | |
|---|
| 154 | hs_docs :: [LDocDecl] |
|---|
| 155 | } deriving (Data, Typeable) |
|---|
| 156 | |
|---|
| 157 | emptyGroup, emptyRdrGroup, emptyRnGroup :: HsGroup a |
|---|
| 158 | emptyRdrGroup = emptyGroup { hs_valds = emptyValBindsIn } |
|---|
| 159 | emptyRnGroup = emptyGroup { hs_valds = emptyValBindsOut } |
|---|
| 160 | |
|---|
| 161 | emptyGroup = HsGroup { hs_tyclds = [], hs_instds = [], |
|---|
| 162 | hs_derivds = [], |
|---|
| 163 | hs_fixds = [], hs_defds = [], hs_annds = [], |
|---|
| 164 | hs_fords = [], hs_warnds = [], hs_ruleds = [], hs_vects = [], |
|---|
| 165 | hs_valds = error "emptyGroup hs_valds: Can't happen", |
|---|
| 166 | hs_docs = [] } |
|---|
| 167 | |
|---|
| 168 | appendGroups :: HsGroup a -> HsGroup a -> HsGroup a |
|---|
| 169 | appendGroups |
|---|
| 170 | HsGroup { |
|---|
| 171 | hs_valds = val_groups1, |
|---|
| 172 | hs_tyclds = tyclds1, |
|---|
| 173 | hs_instds = instds1, |
|---|
| 174 | hs_derivds = derivds1, |
|---|
| 175 | hs_fixds = fixds1, |
|---|
| 176 | hs_defds = defds1, |
|---|
| 177 | hs_annds = annds1, |
|---|
| 178 | hs_fords = fords1, |
|---|
| 179 | hs_warnds = warnds1, |
|---|
| 180 | hs_ruleds = rulds1, |
|---|
| 181 | hs_vects = vects1, |
|---|
| 182 | hs_docs = docs1 } |
|---|
| 183 | HsGroup { |
|---|
| 184 | hs_valds = val_groups2, |
|---|
| 185 | hs_tyclds = tyclds2, |
|---|
| 186 | hs_instds = instds2, |
|---|
| 187 | hs_derivds = derivds2, |
|---|
| 188 | hs_fixds = fixds2, |
|---|
| 189 | hs_defds = defds2, |
|---|
| 190 | hs_annds = annds2, |
|---|
| 191 | hs_fords = fords2, |
|---|
| 192 | hs_warnds = warnds2, |
|---|
| 193 | hs_ruleds = rulds2, |
|---|
| 194 | hs_vects = vects2, |
|---|
| 195 | hs_docs = docs2 } |
|---|
| 196 | = |
|---|
| 197 | HsGroup { |
|---|
| 198 | hs_valds = val_groups1 `plusHsValBinds` val_groups2, |
|---|
| 199 | hs_tyclds = tyclds1 ++ tyclds2, |
|---|
| 200 | hs_instds = instds1 ++ instds2, |
|---|
| 201 | hs_derivds = derivds1 ++ derivds2, |
|---|
| 202 | hs_fixds = fixds1 ++ fixds2, |
|---|
| 203 | hs_annds = annds1 ++ annds2, |
|---|
| 204 | hs_defds = defds1 ++ defds2, |
|---|
| 205 | hs_fords = fords1 ++ fords2, |
|---|
| 206 | hs_warnds = warnds1 ++ warnds2, |
|---|
| 207 | hs_ruleds = rulds1 ++ rulds2, |
|---|
| 208 | hs_vects = vects1 ++ vects2, |
|---|
| 209 | hs_docs = docs1 ++ docs2 } |
|---|
| 210 | \end{code} |
|---|
| 211 | |
|---|
| 212 | \begin{code} |
|---|
| 213 | instance OutputableBndr name => Outputable (HsDecl name) where |
|---|
| 214 | ppr (TyClD dcl) = ppr dcl |
|---|
| 215 | ppr (ValD binds) = ppr binds |
|---|
| 216 | ppr (DefD def) = ppr def |
|---|
| 217 | ppr (InstD inst) = ppr inst |
|---|
| 218 | ppr (DerivD deriv) = ppr deriv |
|---|
| 219 | ppr (ForD fd) = ppr fd |
|---|
| 220 | ppr (SigD sd) = ppr sd |
|---|
| 221 | ppr (RuleD rd) = ppr rd |
|---|
| 222 | ppr (VectD vect) = ppr vect |
|---|
| 223 | ppr (WarningD wd) = ppr wd |
|---|
| 224 | ppr (AnnD ad) = ppr ad |
|---|
| 225 | ppr (SpliceD dd) = ppr dd |
|---|
| 226 | ppr (DocD doc) = ppr doc |
|---|
| 227 | ppr (QuasiQuoteD qq) = ppr qq |
|---|
| 228 | |
|---|
| 229 | instance OutputableBndr name => Outputable (HsGroup name) where |
|---|
| 230 | ppr (HsGroup { hs_valds = val_decls, |
|---|
| 231 | hs_tyclds = tycl_decls, |
|---|
| 232 | hs_instds = inst_decls, |
|---|
| 233 | hs_derivds = deriv_decls, |
|---|
| 234 | hs_fixds = fix_decls, |
|---|
| 235 | hs_warnds = deprec_decls, |
|---|
| 236 | hs_annds = ann_decls, |
|---|
| 237 | hs_fords = foreign_decls, |
|---|
| 238 | hs_defds = default_decls, |
|---|
| 239 | hs_ruleds = rule_decls, |
|---|
| 240 | hs_vects = vect_decls }) |
|---|
| 241 | = vcat_mb empty |
|---|
| 242 | [ppr_ds fix_decls, ppr_ds default_decls, |
|---|
| 243 | ppr_ds deprec_decls, ppr_ds ann_decls, |
|---|
| 244 | ppr_ds rule_decls, |
|---|
| 245 | ppr_ds vect_decls, |
|---|
| 246 | if isEmptyValBinds val_decls |
|---|
| 247 | then Nothing |
|---|
| 248 | else Just (ppr val_decls), |
|---|
| 249 | ppr_ds (concat tycl_decls), |
|---|
| 250 | ppr_ds inst_decls, |
|---|
| 251 | ppr_ds deriv_decls, |
|---|
| 252 | ppr_ds foreign_decls] |
|---|
| 253 | where |
|---|
| 254 | ppr_ds :: Outputable a => [a] -> Maybe SDoc |
|---|
| 255 | ppr_ds [] = Nothing |
|---|
| 256 | ppr_ds ds = Just (vcat (map ppr ds)) |
|---|
| 257 | |
|---|
| 258 | vcat_mb :: SDoc -> [Maybe SDoc] -> SDoc |
|---|
| 259 | -- Concatenate vertically with white-space between non-blanks |
|---|
| 260 | vcat_mb _ [] = empty |
|---|
| 261 | vcat_mb gap (Nothing : ds) = vcat_mb gap ds |
|---|
| 262 | vcat_mb gap (Just d : ds) = gap $$ d $$ vcat_mb blankLine ds |
|---|
| 263 | |
|---|
| 264 | data SpliceDecl id |
|---|
| 265 | = SpliceDecl -- Top level splice |
|---|
| 266 | (Located (HsExpr id)) |
|---|
| 267 | HsExplicitFlag -- Explicit <=> $(f x y) |
|---|
| 268 | -- Implicit <=> f x y, i.e. a naked top level expression |
|---|
| 269 | deriving (Data, Typeable) |
|---|
| 270 | |
|---|
| 271 | instance OutputableBndr name => Outputable (SpliceDecl name) where |
|---|
| 272 | ppr (SpliceDecl e _) = ptext (sLit "$") <> parens (pprExpr (unLoc e)) |
|---|
| 273 | \end{code} |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | %************************************************************************ |
|---|
| 277 | %* * |
|---|
| 278 | \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration} |
|---|
| 279 | %* * |
|---|
| 280 | %************************************************************************ |
|---|
| 281 | |
|---|
| 282 | -------------------------------- |
|---|
| 283 | THE NAMING STORY |
|---|
| 284 | -------------------------------- |
|---|
| 285 | |
|---|
| 286 | Here is the story about the implicit names that go with type, class, |
|---|
| 287 | and instance decls. It's a bit tricky, so pay attention! |
|---|
| 288 | |
|---|
| 289 | "Implicit" (or "system") binders |
|---|
| 290 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 291 | Each data type decl defines |
|---|
| 292 | a worker name for each constructor |
|---|
| 293 | to-T and from-T convertors |
|---|
| 294 | Each class decl defines |
|---|
| 295 | a tycon for the class |
|---|
| 296 | a data constructor for that tycon |
|---|
| 297 | the worker for that constructor |
|---|
| 298 | a selector for each superclass |
|---|
| 299 | |
|---|
| 300 | All have occurrence names that are derived uniquely from their parent |
|---|
| 301 | declaration. |
|---|
| 302 | |
|---|
| 303 | None of these get separate definitions in an interface file; they are |
|---|
| 304 | fully defined by the data or class decl. But they may *occur* in |
|---|
| 305 | interface files, of course. Any such occurrence must haul in the |
|---|
| 306 | relevant type or class decl. |
|---|
| 307 | |
|---|
| 308 | Plan of attack: |
|---|
| 309 | - Ensure they "point to" the parent data/class decl |
|---|
| 310 | when loading that decl from an interface file |
|---|
| 311 | (See RnHiFiles.getSysBinders) |
|---|
| 312 | |
|---|
| 313 | - When typechecking the decl, we build the implicit TyCons and Ids. |
|---|
| 314 | When doing so we look them up in the name cache (RnEnv.lookupSysName), |
|---|
| 315 | to ensure correct module and provenance is set |
|---|
| 316 | |
|---|
| 317 | These are the two places that we have to conjure up the magic derived |
|---|
| 318 | names. (The actual magic is in OccName.mkWorkerOcc, etc.) |
|---|
| 319 | |
|---|
| 320 | Default methods |
|---|
| 321 | ~~~~~~~~~~~~~~~ |
|---|
| 322 | - Occurrence name is derived uniquely from the method name |
|---|
| 323 | E.g. $dmmax |
|---|
| 324 | |
|---|
| 325 | - If there is a default method name at all, it's recorded in |
|---|
| 326 | the ClassOpSig (in HsBinds), in the DefMeth field. |
|---|
| 327 | (DefMeth is defined in Class.lhs) |
|---|
| 328 | |
|---|
| 329 | Source-code class decls and interface-code class decls are treated subtly |
|---|
| 330 | differently, which has given me a great deal of confusion over the years. |
|---|
| 331 | Here's the deal. (We distinguish the two cases because source-code decls |
|---|
| 332 | have (Just binds) in the tcdMeths field, whereas interface decls have Nothing. |
|---|
| 333 | |
|---|
| 334 | In *source-code* class declarations: |
|---|
| 335 | |
|---|
| 336 | - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName |
|---|
| 337 | This is done by RdrHsSyn.mkClassOpSigDM |
|---|
| 338 | |
|---|
| 339 | - The renamer renames it to a Name |
|---|
| 340 | |
|---|
| 341 | - During typechecking, we generate a binding for each $dm for |
|---|
| 342 | which there's a programmer-supplied default method: |
|---|
| 343 | class Foo a where |
|---|
| 344 | op1 :: <type> |
|---|
| 345 | op2 :: <type> |
|---|
| 346 | op1 = ... |
|---|
| 347 | We generate a binding for $dmop1 but not for $dmop2. |
|---|
| 348 | The Class for Foo has a NoDefMeth for op2 and a DefMeth for op1. |
|---|
| 349 | The Name for $dmop2 is simply discarded. |
|---|
| 350 | |
|---|
| 351 | In *interface-file* class declarations: |
|---|
| 352 | - When parsing, we see if there's an explicit programmer-supplied default method |
|---|
| 353 | because there's an '=' sign to indicate it: |
|---|
| 354 | class Foo a where |
|---|
| 355 | op1 = :: <type> -- NB the '=' |
|---|
| 356 | op2 :: <type> |
|---|
| 357 | We use this info to generate a DefMeth with a suitable RdrName for op1, |
|---|
| 358 | and a NoDefMeth for op2 |
|---|
| 359 | - The interface file has a separate definition for $dmop1, with unfolding etc. |
|---|
| 360 | - The renamer renames it to a Name. |
|---|
| 361 | - The renamer treats $dmop1 as a free variable of the declaration, so that |
|---|
| 362 | the binding for $dmop1 will be sucked in. (See RnHsSyn.tyClDeclFVs) |
|---|
| 363 | This doesn't happen for source code class decls, because they *bind* the default method. |
|---|
| 364 | |
|---|
| 365 | Dictionary functions |
|---|
| 366 | ~~~~~~~~~~~~~~~~~~~~ |
|---|
| 367 | Each instance declaration gives rise to one dictionary function binding. |
|---|
| 368 | |
|---|
| 369 | The type checker makes up new source-code instance declarations |
|---|
| 370 | (e.g. from 'deriving' or generic default methods --- see |
|---|
| 371 | TcInstDcls.tcInstDecls1). So we can't generate the names for |
|---|
| 372 | dictionary functions in advance (we don't know how many we need). |
|---|
| 373 | |
|---|
| 374 | On the other hand for interface-file instance declarations, the decl |
|---|
| 375 | specifies the name of the dictionary function, and it has a binding elsewhere |
|---|
| 376 | in the interface file: |
|---|
| 377 | instance {Eq Int} = dEqInt |
|---|
| 378 | dEqInt :: {Eq Int} <pragma info> |
|---|
| 379 | |
|---|
| 380 | So again we treat source code and interface file code slightly differently. |
|---|
| 381 | |
|---|
| 382 | Source code: |
|---|
| 383 | - Source code instance decls have a Nothing in the (Maybe name) field |
|---|
| 384 | (see data InstDecl below) |
|---|
| 385 | |
|---|
| 386 | - The typechecker makes up a Local name for the dict fun for any source-code |
|---|
| 387 | instance decl, whether it comes from a source-code instance decl, or whether |
|---|
| 388 | the instance decl is derived from some other construct (e.g. 'deriving'). |
|---|
| 389 | |
|---|
| 390 | - The occurrence name it chooses is derived from the instance decl (just for |
|---|
| 391 | documentation really) --- e.g. dNumInt. Two dict funs may share a common |
|---|
| 392 | occurrence name, but will have different uniques. E.g. |
|---|
| 393 | instance Foo [Int] where ... |
|---|
| 394 | instance Foo [Bool] where ... |
|---|
| 395 | These might both be dFooList |
|---|
| 396 | |
|---|
| 397 | - The CoreTidy phase externalises the name, and ensures the occurrence name is |
|---|
| 398 | unique (this isn't special to dict funs). So we'd get dFooList and dFooList1. |
|---|
| 399 | |
|---|
| 400 | - We can take this relaxed approach (changing the occurrence name later) |
|---|
| 401 | because dict fun Ids are not captured in a TyCon or Class (unlike default |
|---|
| 402 | methods, say). Instead, they are kept separately in the InstEnv. This |
|---|
| 403 | makes it easy to adjust them after compiling a module. (Once we've finished |
|---|
| 404 | compiling that module, they don't change any more.) |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | Interface file code: |
|---|
| 408 | - The instance decl gives the dict fun name, so the InstDecl has a (Just name) |
|---|
| 409 | in the (Maybe name) field. |
|---|
| 410 | |
|---|
| 411 | - RnHsSyn.instDeclFVs treats the dict fun name as free in the decl, so that we |
|---|
| 412 | suck in the dfun binding |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | \begin{code} |
|---|
| 416 | type LTyClDecl name = Located (TyClDecl name) |
|---|
| 417 | type TyClGroup name = [LTyClDecl name] -- This is used in TcTyClsDecls to represent |
|---|
| 418 | -- strongly connected components of decls |
|---|
| 419 | -- No familiy instances in here |
|---|
| 420 | |
|---|
| 421 | -- | A type or class declaration. |
|---|
| 422 | data TyClDecl name |
|---|
| 423 | = ForeignType { |
|---|
| 424 | tcdLName :: Located name, |
|---|
| 425 | tcdExtName :: Maybe FastString |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | | -- | @type/data family T :: *->*@ |
|---|
| 429 | TyFamily { tcdFlavour :: FamilyFlavour, -- type or data |
|---|
| 430 | tcdLName :: Located name, -- type constructor |
|---|
| 431 | tcdTyVars :: LHsTyVarBndrs name, -- type variables |
|---|
| 432 | tcdKindSig :: Maybe (LHsKind name) -- result kind |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | | -- | @type/data declaration |
|---|
| 437 | TyDecl { tcdLName :: Located name -- ^ Type constructor |
|---|
| 438 | , tcdTyVars :: LHsTyVarBndrs name |
|---|
| 439 | , tcdTyDefn :: HsTyDefn name |
|---|
| 440 | , tcdFVs :: NameSet } |
|---|
| 441 | |
|---|
| 442 | | ClassDecl { tcdCtxt :: LHsContext name, -- ^ Context... |
|---|
| 443 | tcdLName :: Located name, -- ^ Name of the class |
|---|
| 444 | tcdTyVars :: LHsTyVarBndrs name, -- ^ Class type variables |
|---|
| 445 | tcdFDs :: [Located (FunDep name)], -- ^ Functional deps |
|---|
| 446 | tcdSigs :: [LSig name], -- ^ Methods' signatures |
|---|
| 447 | tcdMeths :: LHsBinds name, -- ^ Default methods |
|---|
| 448 | tcdATs :: [LTyClDecl name], -- ^ Associated types; ie |
|---|
| 449 | -- only 'TyFamily' |
|---|
| 450 | tcdATDefs :: [LFamInstDecl name], -- ^ Associated type defaults; ie |
|---|
| 451 | -- only 'TySynonym' |
|---|
| 452 | tcdDocs :: [LDocDecl], -- ^ Haddock docs |
|---|
| 453 | tcdFVs :: NameSet |
|---|
| 454 | } |
|---|
| 455 | deriving (Data, Typeable) |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | data HsTyDefn name -- The payload of a type synonym or data type defn |
|---|
| 459 | -- Used *both* for vanialla type/data declarations, |
|---|
| 460 | -- *and* for type/data family instances |
|---|
| 461 | = TySynonym { td_synRhs :: LHsType name } -- ^ Synonym expansion |
|---|
| 462 | |
|---|
| 463 | | -- | Declares a data type or newtype, giving its construcors |
|---|
| 464 | -- @ |
|---|
| 465 | -- data/newtype T a = <constrs> |
|---|
| 466 | -- data/newtype instance T [a] = <constrs> |
|---|
| 467 | -- @ |
|---|
| 468 | TyData { td_ND :: NewOrData, |
|---|
| 469 | td_ctxt :: LHsContext name, -- ^ Context |
|---|
| 470 | td_cType :: Maybe CType, |
|---|
| 471 | td_kindSig:: Maybe (LHsKind name), |
|---|
| 472 | -- ^ Optional kind signature. |
|---|
| 473 | -- |
|---|
| 474 | -- @(Just k)@ for a GADT-style @data@, or @data |
|---|
| 475 | -- instance@ decl with explicit kind sig |
|---|
| 476 | |
|---|
| 477 | td_cons :: [LConDecl name], |
|---|
| 478 | -- ^ Data constructors |
|---|
| 479 | -- |
|---|
| 480 | -- For @data T a = T1 | T2 a@ |
|---|
| 481 | -- the 'LConDecl's all have 'ResTyH98'. |
|---|
| 482 | -- For @data T a where { T1 :: T a }@ |
|---|
| 483 | -- the 'LConDecls' all have 'ResTyGADT'. |
|---|
| 484 | |
|---|
| 485 | td_derivs :: Maybe [LHsType name] |
|---|
| 486 | -- ^ Derivings; @Nothing@ => not specified, |
|---|
| 487 | -- @Just []@ => derive exactly what is asked |
|---|
| 488 | -- |
|---|
| 489 | -- These "types" must be of form |
|---|
| 490 | -- @ |
|---|
| 491 | -- forall ab. C ty1 ty2 |
|---|
| 492 | -- @ |
|---|
| 493 | -- Typically the foralls and ty args are empty, but they |
|---|
| 494 | -- are non-empty for the newtype-deriving case |
|---|
| 495 | } |
|---|
| 496 | deriving( Data, Typeable ) |
|---|
| 497 | |
|---|
| 498 | data NewOrData |
|---|
| 499 | = NewType -- ^ @newtype Blah ...@ |
|---|
| 500 | | DataType -- ^ @data Blah ...@ |
|---|
| 501 | deriving( Eq, Data, Typeable ) -- Needed because Demand derives Eq |
|---|
| 502 | |
|---|
| 503 | data FamilyFlavour |
|---|
| 504 | = TypeFamily -- ^ @type family ...@ |
|---|
| 505 | | DataFamily -- ^ @data family ...@ |
|---|
| 506 | deriving (Data, Typeable) |
|---|
| 507 | \end{code} |
|---|
| 508 | |
|---|
| 509 | Note [tcdTypats and HsTyPats] |
|---|
| 510 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 511 | We use TyData and TySynonym both for vanilla data/type declarations |
|---|
| 512 | type T a = Int |
|---|
| 513 | AND for data/type family instance declarations |
|---|
| 514 | type instance F [a] = (a,Int) |
|---|
| 515 | |
|---|
| 516 | tcdTyPats = HsTyDefn tvs |
|---|
| 517 | This is a vanilla data type or type synonym |
|---|
| 518 | tvs are the quantified type variables |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | ------------------------------ |
|---|
| 522 | Simple classifiers |
|---|
| 523 | |
|---|
| 524 | \begin{code} |
|---|
| 525 | isHsDataDefn, isHsSynDefn :: HsTyDefn name -> Bool |
|---|
| 526 | isHsDataDefn (TyData {}) = True |
|---|
| 527 | isHsDataDefn _ = False |
|---|
| 528 | |
|---|
| 529 | isHsSynDefn (TySynonym {}) = True |
|---|
| 530 | isHsSynDefn _ = False |
|---|
| 531 | |
|---|
| 532 | -- | @True@ <=> argument is a @data@\/@newtype@ |
|---|
| 533 | -- declaration. |
|---|
| 534 | isDataDecl :: TyClDecl name -> Bool |
|---|
| 535 | isDataDecl (TyDecl { tcdTyDefn = defn }) = isHsDataDefn defn |
|---|
| 536 | isDataDecl _other = False |
|---|
| 537 | |
|---|
| 538 | -- | type or type instance declaration |
|---|
| 539 | isSynDecl :: TyClDecl name -> Bool |
|---|
| 540 | isSynDecl (TyDecl { tcdTyDefn = defn }) = isHsSynDefn defn |
|---|
| 541 | isSynDecl _other = False |
|---|
| 542 | |
|---|
| 543 | -- | type class |
|---|
| 544 | isClassDecl :: TyClDecl name -> Bool |
|---|
| 545 | isClassDecl (ClassDecl {}) = True |
|---|
| 546 | isClassDecl _ = False |
|---|
| 547 | |
|---|
| 548 | -- | type family declaration |
|---|
| 549 | isFamilyDecl :: TyClDecl name -> Bool |
|---|
| 550 | isFamilyDecl (TyFamily {}) = True |
|---|
| 551 | isFamilyDecl _other = False |
|---|
| 552 | \end{code} |
|---|
| 553 | |
|---|
| 554 | Dealing with names |
|---|
| 555 | |
|---|
| 556 | \begin{code} |
|---|
| 557 | famInstDeclName :: LFamInstDecl a -> a |
|---|
| 558 | famInstDeclName (L _ (FamInstDecl { fid_tycon = L _ name })) = name |
|---|
| 559 | |
|---|
| 560 | tcdName :: TyClDecl name -> name |
|---|
| 561 | tcdName decl = unLoc (tcdLName decl) |
|---|
| 562 | \end{code} |
|---|
| 563 | |
|---|
| 564 | \begin{code} |
|---|
| 565 | countTyClDecls :: [TyClDecl name] -> (Int, Int, Int, Int, Int) |
|---|
| 566 | -- class, synonym decls, data, newtype, family decls |
|---|
| 567 | countTyClDecls decls |
|---|
| 568 | = (count isClassDecl decls, |
|---|
| 569 | count isSynDecl decls, -- excluding... |
|---|
| 570 | count isDataTy decls, -- ...family... |
|---|
| 571 | count isNewTy decls, -- ...instances |
|---|
| 572 | count isFamilyDecl decls) |
|---|
| 573 | where |
|---|
| 574 | isDataTy TyDecl{ tcdTyDefn = TyData { td_ND = DataType } } = True |
|---|
| 575 | isDataTy _ = False |
|---|
| 576 | |
|---|
| 577 | isNewTy TyDecl{ tcdTyDefn = TyData { td_ND = NewType } } = True |
|---|
| 578 | isNewTy _ = False |
|---|
| 579 | \end{code} |
|---|
| 580 | |
|---|
| 581 | \begin{code} |
|---|
| 582 | instance OutputableBndr name |
|---|
| 583 | => Outputable (TyClDecl name) where |
|---|
| 584 | |
|---|
| 585 | ppr (ForeignType {tcdLName = ltycon}) |
|---|
| 586 | = hsep [ptext (sLit "foreign import type dotnet"), ppr ltycon] |
|---|
| 587 | |
|---|
| 588 | ppr (TyFamily {tcdFlavour = flavour, tcdLName = ltycon, |
|---|
| 589 | tcdTyVars = tyvars, tcdKindSig = mb_kind}) |
|---|
| 590 | = pp_flavour <+> pp_vanilla_decl_head ltycon tyvars [] <+> pp_kind |
|---|
| 591 | where |
|---|
| 592 | pp_flavour = case flavour of |
|---|
| 593 | TypeFamily -> ptext (sLit "type family") |
|---|
| 594 | DataFamily -> ptext (sLit "data family") |
|---|
| 595 | |
|---|
| 596 | pp_kind = case mb_kind of |
|---|
| 597 | Nothing -> empty |
|---|
| 598 | Just kind -> dcolon <+> ppr kind |
|---|
| 599 | |
|---|
| 600 | ppr (TyDecl { tcdLName = ltycon, tcdTyVars = tyvars, tcdTyDefn = defn }) |
|---|
| 601 | = pp_ty_defn (pp_vanilla_decl_head ltycon tyvars) defn |
|---|
| 602 | |
|---|
| 603 | ppr (ClassDecl {tcdCtxt = context, tcdLName = lclas, tcdTyVars = tyvars, |
|---|
| 604 | tcdFDs = fds, |
|---|
| 605 | tcdSigs = sigs, tcdMeths = methods, |
|---|
| 606 | tcdATs = ats, tcdATDefs = at_defs}) |
|---|
| 607 | | null sigs && isEmptyBag methods && null ats && null at_defs -- No "where" part |
|---|
| 608 | = top_matter |
|---|
| 609 | |
|---|
| 610 | | otherwise -- Laid out |
|---|
| 611 | = vcat [ top_matter <+> ptext (sLit "where") |
|---|
| 612 | , nest 2 $ pprDeclList (map ppr ats ++ |
|---|
| 613 | map ppr at_defs ++ |
|---|
| 614 | pprLHsBindsForUser methods sigs) ] |
|---|
| 615 | where |
|---|
| 616 | top_matter = ptext (sLit "class") |
|---|
| 617 | <+> pp_vanilla_decl_head lclas tyvars (unLoc context) |
|---|
| 618 | <+> pprFundeps (map unLoc fds) |
|---|
| 619 | |
|---|
| 620 | pp_vanilla_decl_head :: OutputableBndr name |
|---|
| 621 | => Located name |
|---|
| 622 | -> LHsTyVarBndrs name |
|---|
| 623 | -> HsContext name |
|---|
| 624 | -> SDoc |
|---|
| 625 | pp_vanilla_decl_head thing tyvars context |
|---|
| 626 | = hsep [pprHsContext context, pprPrefixOcc (unLoc thing), ppr tyvars] |
|---|
| 627 | |
|---|
| 628 | pp_fam_inst_head :: OutputableBndr name |
|---|
| 629 | => Located name |
|---|
| 630 | -> HsWithBndrs [LHsType name] |
|---|
| 631 | -> HsContext name |
|---|
| 632 | -> SDoc |
|---|
| 633 | pp_fam_inst_head thing (HsWB { hswb_cts = typats }) context -- explicit type patterns |
|---|
| 634 | = hsep [ ptext (sLit "instance"), pprHsContext context, pprPrefixOcc (unLoc thing) |
|---|
| 635 | , hsep (map (pprParendHsType.unLoc) typats)] |
|---|
| 636 | |
|---|
| 637 | pp_condecls :: OutputableBndr name => [LConDecl name] -> SDoc |
|---|
| 638 | pp_condecls cs@(L _ ConDecl{ con_res = ResTyGADT _ } : _) -- In GADT syntax |
|---|
| 639 | = hang (ptext (sLit "where")) 2 (vcat (map ppr cs)) |
|---|
| 640 | pp_condecls cs -- In H98 syntax |
|---|
| 641 | = equals <+> sep (punctuate (ptext (sLit " |")) (map ppr cs)) |
|---|
| 642 | |
|---|
| 643 | pp_ty_defn :: OutputableBndr name |
|---|
| 644 | => (HsContext name -> SDoc) -- Printing the header |
|---|
| 645 | -> HsTyDefn name |
|---|
| 646 | -> SDoc |
|---|
| 647 | |
|---|
| 648 | pp_ty_defn pp_hdr (TySynonym { td_synRhs = rhs }) |
|---|
| 649 | = hang (ptext (sLit "type") <+> pp_hdr [] <+> equals) |
|---|
| 650 | 4 (ppr rhs) |
|---|
| 651 | |
|---|
| 652 | pp_ty_defn pp_hdr (TyData { td_ND = new_or_data, td_ctxt = L _ context |
|---|
| 653 | , td_kindSig = mb_sig |
|---|
| 654 | , td_cons = condecls, td_derivs = derivings }) |
|---|
| 655 | | null condecls |
|---|
| 656 | = ppr new_or_data <+> pp_hdr context <+> pp_sig |
|---|
| 657 | |
|---|
| 658 | | otherwise |
|---|
| 659 | = hang (ppr new_or_data <+> pp_hdr context <+> pp_sig) |
|---|
| 660 | 2 (pp_condecls condecls $$ pp_derivings) |
|---|
| 661 | where |
|---|
| 662 | pp_sig = case mb_sig of |
|---|
| 663 | Nothing -> empty |
|---|
| 664 | Just kind -> dcolon <+> ppr kind |
|---|
| 665 | pp_derivings = case derivings of |
|---|
| 666 | Nothing -> empty |
|---|
| 667 | Just ds -> hsep [ptext (sLit "deriving"), parens (interpp'SP ds)] |
|---|
| 668 | |
|---|
| 669 | instance OutputableBndr name => Outputable (HsTyDefn name) where |
|---|
| 670 | ppr d = pp_ty_defn (\_ -> ptext (sLit "Naked HsTyDefn")) d |
|---|
| 671 | |
|---|
| 672 | instance Outputable NewOrData where |
|---|
| 673 | ppr NewType = ptext (sLit "newtype") |
|---|
| 674 | ppr DataType = ptext (sLit "data") |
|---|
| 675 | |
|---|
| 676 | pprTyDefnFlavour :: HsTyDefn a -> SDoc |
|---|
| 677 | pprTyDefnFlavour (TyData { td_ND = nd }) = ppr nd |
|---|
| 678 | pprTyDefnFlavour (TySynonym {}) = ptext (sLit "type") |
|---|
| 679 | |
|---|
| 680 | pprTyClDeclFlavour :: TyClDecl a -> SDoc |
|---|
| 681 | pprTyClDeclFlavour (ClassDecl {}) = ptext (sLit "class") |
|---|
| 682 | pprTyClDeclFlavour (TyFamily {}) = ptext (sLit "family") |
|---|
| 683 | pprTyClDeclFlavour (TyDecl { tcdTyDefn = defn }) = pprTyDefnFlavour defn |
|---|
| 684 | pprTyClDeclFlavour (ForeignType {}) = ptext (sLit "foreign type") |
|---|
| 685 | \end{code} |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | %************************************************************************ |
|---|
| 689 | %* * |
|---|
| 690 | \subsection[ConDecl]{A data-constructor declaration} |
|---|
| 691 | %* * |
|---|
| 692 | %************************************************************************ |
|---|
| 693 | |
|---|
| 694 | \begin{code} |
|---|
| 695 | type LConDecl name = Located (ConDecl name) |
|---|
| 696 | |
|---|
| 697 | -- data T b = forall a. Eq a => MkT a b |
|---|
| 698 | -- MkT :: forall b a. Eq a => MkT a b |
|---|
| 699 | |
|---|
| 700 | -- data T b where |
|---|
| 701 | -- MkT1 :: Int -> T Int |
|---|
| 702 | |
|---|
| 703 | -- data T = Int `MkT` Int |
|---|
| 704 | -- | MkT2 |
|---|
| 705 | |
|---|
| 706 | -- data T a where |
|---|
| 707 | -- Int `MkT` Int :: T Int |
|---|
| 708 | |
|---|
| 709 | data ConDecl name |
|---|
| 710 | = ConDecl |
|---|
| 711 | { con_name :: Located name |
|---|
| 712 | -- ^ Constructor name. This is used for the DataCon itself, and for |
|---|
| 713 | -- the user-callable wrapper Id. |
|---|
| 714 | |
|---|
| 715 | , con_explicit :: HsExplicitFlag |
|---|
| 716 | -- ^ Is there an user-written forall? (cf. 'HsTypes.HsForAllTy') |
|---|
| 717 | |
|---|
| 718 | , con_qvars :: LHsTyVarBndrs name |
|---|
| 719 | -- ^ Type variables. Depending on 'con_res' this describes the |
|---|
| 720 | -- following entities |
|---|
| 721 | -- |
|---|
| 722 | -- - ResTyH98: the constructor's *existential* type variables |
|---|
| 723 | -- - ResTyGADT: *all* the constructor's quantified type variables |
|---|
| 724 | -- |
|---|
| 725 | -- If con_explicit is Implicit, then con_qvars is irrelevant |
|---|
| 726 | -- until after renaming. |
|---|
| 727 | |
|---|
| 728 | , con_cxt :: LHsContext name |
|---|
| 729 | -- ^ The context. This /does not/ include the \"stupid theta\" which |
|---|
| 730 | -- lives only in the 'TyData' decl. |
|---|
| 731 | |
|---|
| 732 | , con_details :: HsConDeclDetails name |
|---|
| 733 | -- ^ The main payload |
|---|
| 734 | |
|---|
| 735 | , con_res :: ResType (LHsType name) |
|---|
| 736 | -- ^ Result type of the constructor |
|---|
| 737 | |
|---|
| 738 | , con_doc :: Maybe LHsDocString |
|---|
| 739 | -- ^ A possible Haddock comment. |
|---|
| 740 | |
|---|
| 741 | , con_old_rec :: Bool |
|---|
| 742 | -- ^ TEMPORARY field; True <=> user has employed now-deprecated syntax for |
|---|
| 743 | -- GADT-style record decl C { blah } :: T a b |
|---|
| 744 | -- Remove this when we no longer parse this stuff, and hence do not |
|---|
| 745 | -- need to report decprecated use |
|---|
| 746 | } deriving (Data, Typeable) |
|---|
| 747 | |
|---|
| 748 | type HsConDeclDetails name = HsConDetails (LBangType name) [ConDeclField name] |
|---|
| 749 | |
|---|
| 750 | hsConDeclArgTys :: HsConDeclDetails name -> [LBangType name] |
|---|
| 751 | hsConDeclArgTys (PrefixCon tys) = tys |
|---|
| 752 | hsConDeclArgTys (InfixCon ty1 ty2) = [ty1,ty2] |
|---|
| 753 | hsConDeclArgTys (RecCon flds) = map cd_fld_type flds |
|---|
| 754 | |
|---|
| 755 | data ResType ty |
|---|
| 756 | = ResTyH98 -- Constructor was declared using Haskell 98 syntax |
|---|
| 757 | | ResTyGADT ty -- Constructor was declared using GADT-style syntax, |
|---|
| 758 | -- and here is its result type |
|---|
| 759 | deriving (Data, Typeable) |
|---|
| 760 | |
|---|
| 761 | instance Outputable ty => Outputable (ResType ty) where |
|---|
| 762 | -- Debugging only |
|---|
| 763 | ppr ResTyH98 = ptext (sLit "ResTyH98") |
|---|
| 764 | ppr (ResTyGADT ty) = ptext (sLit "ResTyGADT") <+> ppr ty |
|---|
| 765 | \end{code} |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | \begin{code} |
|---|
| 769 | instance (OutputableBndr name) => Outputable (ConDecl name) where |
|---|
| 770 | ppr = pprConDecl |
|---|
| 771 | |
|---|
| 772 | pprConDecl :: OutputableBndr name => ConDecl name -> SDoc |
|---|
| 773 | pprConDecl (ConDecl { con_name = con, con_explicit = expl, con_qvars = tvs |
|---|
| 774 | , con_cxt = cxt, con_details = details |
|---|
| 775 | , con_res = ResTyH98, con_doc = doc }) |
|---|
| 776 | = sep [ppr_mbDoc doc, pprHsForAll expl tvs cxt, ppr_details details] |
|---|
| 777 | where |
|---|
| 778 | ppr_details (InfixCon t1 t2) = hsep [ppr t1, pprInfixOcc (unLoc con), ppr t2] |
|---|
| 779 | ppr_details (PrefixCon tys) = hsep (pprPrefixOcc (unLoc con) : map (pprParendHsType . unLoc) tys) |
|---|
| 780 | ppr_details (RecCon fields) = ppr con <+> pprConDeclFields fields |
|---|
| 781 | |
|---|
| 782 | pprConDecl (ConDecl { con_name = con, con_explicit = expl, con_qvars = tvs |
|---|
| 783 | , con_cxt = cxt, con_details = PrefixCon arg_tys |
|---|
| 784 | , con_res = ResTyGADT res_ty }) |
|---|
| 785 | = ppr con <+> dcolon <+> |
|---|
| 786 | sep [pprHsForAll expl tvs cxt, ppr (foldr mk_fun_ty res_ty arg_tys)] |
|---|
| 787 | where |
|---|
| 788 | mk_fun_ty a b = noLoc (HsFunTy a b) |
|---|
| 789 | |
|---|
| 790 | pprConDecl (ConDecl { con_name = con, con_explicit = expl, con_qvars = tvs |
|---|
| 791 | , con_cxt = cxt, con_details = RecCon fields, con_res = ResTyGADT res_ty }) |
|---|
| 792 | = sep [ppr con <+> dcolon <+> pprHsForAll expl tvs cxt, |
|---|
| 793 | pprConDeclFields fields <+> arrow <+> ppr res_ty] |
|---|
| 794 | |
|---|
| 795 | pprConDecl (ConDecl {con_name = con, con_details = InfixCon {}, con_res = ResTyGADT {} }) |
|---|
| 796 | = pprPanic "pprConDecl" (ppr con) |
|---|
| 797 | -- In GADT syntax we don't allow infix constructors |
|---|
| 798 | \end{code} |
|---|
| 799 | |
|---|
| 800 | %************************************************************************ |
|---|
| 801 | %* * |
|---|
| 802 | \subsection[InstDecl]{An instance declaration} |
|---|
| 803 | %* * |
|---|
| 804 | %************************************************************************ |
|---|
| 805 | |
|---|
| 806 | \begin{code} |
|---|
| 807 | type LFamInstDecl name = Located (FamInstDecl name) |
|---|
| 808 | data FamInstDecl name |
|---|
| 809 | = FamInstDecl |
|---|
| 810 | { fid_tycon :: Located name |
|---|
| 811 | , fid_pats :: HsWithBndrs [LHsType name] -- ^ Type patterns (with kind and type bndrs) |
|---|
| 812 | , fid_defn :: HsTyDefn name -- Type or data family instance |
|---|
| 813 | , fid_fvs :: NameSet } |
|---|
| 814 | deriving( Typeable, Data ) |
|---|
| 815 | |
|---|
| 816 | type LInstDecl name = Located (InstDecl name) |
|---|
| 817 | data InstDecl name -- Both class and family instances |
|---|
| 818 | = ClsInstD |
|---|
| 819 | { cid_poly_ty :: LHsType name -- Context => Class Instance-type |
|---|
| 820 | -- Using a polytype means that the renamer conveniently |
|---|
| 821 | -- figures out the quantified type variables for us. |
|---|
| 822 | , cid_binds :: LHsBinds name |
|---|
| 823 | , cid_sigs :: [LSig name] -- User-supplied pragmatic info |
|---|
| 824 | , cid_fam_insts :: [LFamInstDecl name] -- Family instances for associated types |
|---|
| 825 | } |
|---|
| 826 | |
|---|
| 827 | | FamInstD -- type/data family instance |
|---|
| 828 | { lid_inst :: FamInstDecl name } |
|---|
| 829 | deriving (Data, Typeable) |
|---|
| 830 | \end{code} |
|---|
| 831 | |
|---|
| 832 | Note [Family instance declaration binders] |
|---|
| 833 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 834 | A FamInstDecl is a data/type family instance declaration |
|---|
| 835 | the fid_pats field is LHS patterns, and the tvs of the HsBSig |
|---|
| 836 | tvs are fv(pat_tys), *including* ones that are already in scope |
|---|
| 837 | |
|---|
| 838 | Eg class C s t where |
|---|
| 839 | type F t p :: * |
|---|
| 840 | instance C w (a,b) where |
|---|
| 841 | type F (a,b) x = x->a |
|---|
| 842 | The tcdTyVars of the F decl are {a,b,x}, even though the F decl |
|---|
| 843 | is nested inside the 'instance' decl. |
|---|
| 844 | |
|---|
| 845 | However after the renamer, the uniques will match up: |
|---|
| 846 | instance C w7 (a8,b9) where |
|---|
| 847 | type F (a8,b9) x10 = x10->a8 |
|---|
| 848 | so that we can compare the type patter in the 'instance' decl and |
|---|
| 849 | in the associated 'type' decl |
|---|
| 850 | |
|---|
| 851 | \begin{code} |
|---|
| 852 | instance (OutputableBndr name) => Outputable (FamInstDecl name) where |
|---|
| 853 | ppr (FamInstDecl { fid_tycon = tycon |
|---|
| 854 | , fid_pats = pats |
|---|
| 855 | , fid_defn = defn }) |
|---|
| 856 | = pp_ty_defn (pp_fam_inst_head tycon pats) defn |
|---|
| 857 | |
|---|
| 858 | instance (OutputableBndr name) => Outputable (InstDecl name) where |
|---|
| 859 | ppr (ClsInstD { cid_poly_ty = inst_ty, cid_binds = binds |
|---|
| 860 | , cid_sigs = sigs, cid_fam_insts = ats }) |
|---|
| 861 | | null sigs && null ats && isEmptyBag binds -- No "where" part |
|---|
| 862 | = top_matter |
|---|
| 863 | |
|---|
| 864 | | otherwise -- Laid out |
|---|
| 865 | = vcat [ top_matter <+> ptext (sLit "where") |
|---|
| 866 | , nest 2 $ pprDeclList (map ppr ats ++ |
|---|
| 867 | pprLHsBindsForUser binds sigs) ] |
|---|
| 868 | where |
|---|
| 869 | top_matter = ptext (sLit "instance") <+> ppr inst_ty |
|---|
| 870 | |
|---|
| 871 | ppr (FamInstD { lid_inst = decl }) = ppr decl |
|---|
| 872 | |
|---|
| 873 | -- Extract the declarations of associated types from an instance |
|---|
| 874 | |
|---|
| 875 | instDeclFamInsts :: [LInstDecl name] -> [FamInstDecl name] |
|---|
| 876 | instDeclFamInsts inst_decls |
|---|
| 877 | = concatMap do_one inst_decls |
|---|
| 878 | where |
|---|
| 879 | do_one (L _ (ClsInstD { cid_fam_insts = fam_insts })) = map unLoc fam_insts |
|---|
| 880 | do_one (L _ (FamInstD { lid_inst = fam_inst })) = [fam_inst] |
|---|
| 881 | \end{code} |
|---|
| 882 | |
|---|
| 883 | %************************************************************************ |
|---|
| 884 | %* * |
|---|
| 885 | \subsection[DerivDecl]{A stand-alone instance deriving declaration} |
|---|
| 886 | %* * |
|---|
| 887 | %************************************************************************ |
|---|
| 888 | |
|---|
| 889 | \begin{code} |
|---|
| 890 | type LDerivDecl name = Located (DerivDecl name) |
|---|
| 891 | |
|---|
| 892 | data DerivDecl name = DerivDecl { deriv_type :: LHsType name } |
|---|
| 893 | deriving (Data, Typeable) |
|---|
| 894 | |
|---|
| 895 | instance (OutputableBndr name) => Outputable (DerivDecl name) where |
|---|
| 896 | ppr (DerivDecl ty) |
|---|
| 897 | = hsep [ptext (sLit "deriving instance"), ppr ty] |
|---|
| 898 | \end{code} |
|---|
| 899 | |
|---|
| 900 | %************************************************************************ |
|---|
| 901 | %* * |
|---|
| 902 | \subsection[DefaultDecl]{A @default@ declaration} |
|---|
| 903 | %* * |
|---|
| 904 | %************************************************************************ |
|---|
| 905 | |
|---|
| 906 | There can only be one default declaration per module, but it is hard |
|---|
| 907 | for the parser to check that; we pass them all through in the abstract |
|---|
| 908 | syntax, and that restriction must be checked in the front end. |
|---|
| 909 | |
|---|
| 910 | \begin{code} |
|---|
| 911 | type LDefaultDecl name = Located (DefaultDecl name) |
|---|
| 912 | |
|---|
| 913 | data DefaultDecl name |
|---|
| 914 | = DefaultDecl [LHsType name] |
|---|
| 915 | deriving (Data, Typeable) |
|---|
| 916 | |
|---|
| 917 | instance (OutputableBndr name) |
|---|
| 918 | => Outputable (DefaultDecl name) where |
|---|
| 919 | |
|---|
| 920 | ppr (DefaultDecl tys) |
|---|
| 921 | = ptext (sLit "default") <+> parens (interpp'SP tys) |
|---|
| 922 | \end{code} |
|---|
| 923 | |
|---|
| 924 | %************************************************************************ |
|---|
| 925 | %* * |
|---|
| 926 | \subsection{Foreign function interface declaration} |
|---|
| 927 | %* * |
|---|
| 928 | %************************************************************************ |
|---|
| 929 | |
|---|
| 930 | \begin{code} |
|---|
| 931 | |
|---|
| 932 | -- foreign declarations are distinguished as to whether they define or use a |
|---|
| 933 | -- Haskell name |
|---|
| 934 | -- |
|---|
| 935 | -- * the Boolean value indicates whether the pre-standard deprecated syntax |
|---|
| 936 | -- has been used |
|---|
| 937 | -- |
|---|
| 938 | type LForeignDecl name = Located (ForeignDecl name) |
|---|
| 939 | |
|---|
| 940 | data ForeignDecl name |
|---|
| 941 | = ForeignImport (Located name) -- defines this name |
|---|
| 942 | (LHsType name) -- sig_ty |
|---|
| 943 | Coercion -- rep_ty ~ sig_ty |
|---|
| 944 | ForeignImport |
|---|
| 945 | | ForeignExport (Located name) -- uses this name |
|---|
| 946 | (LHsType name) -- sig_ty |
|---|
| 947 | Coercion -- sig_ty ~ rep_ty |
|---|
| 948 | ForeignExport |
|---|
| 949 | deriving (Data, Typeable) |
|---|
| 950 | {- |
|---|
| 951 | In both ForeignImport and ForeignExport: |
|---|
| 952 | sig_ty is the type given in the Haskell code |
|---|
| 953 | rep_ty is the representation for this type, i.e. with newtypes |
|---|
| 954 | coerced away and type functions evaluated. |
|---|
| 955 | Thus if the declaration is valid, then rep_ty will only use types |
|---|
| 956 | such as Int and IO that we know how to make foreign calls with. |
|---|
| 957 | -} |
|---|
| 958 | |
|---|
| 959 | noForeignImportCoercionYet :: Coercion |
|---|
| 960 | noForeignImportCoercionYet |
|---|
| 961 | = panic "ForeignImport coercion evaluated before typechecking" |
|---|
| 962 | |
|---|
| 963 | noForeignExportCoercionYet :: Coercion |
|---|
| 964 | noForeignExportCoercionYet |
|---|
| 965 | = panic "ForeignExport coercion evaluated before typechecking" |
|---|
| 966 | |
|---|
| 967 | -- Specification Of an imported external entity in dependence on the calling |
|---|
| 968 | -- convention |
|---|
| 969 | -- |
|---|
| 970 | data ForeignImport = -- import of a C entity |
|---|
| 971 | -- |
|---|
| 972 | -- * the two strings specifying a header file or library |
|---|
| 973 | -- may be empty, which indicates the absence of a |
|---|
| 974 | -- header or object specification (both are not used |
|---|
| 975 | -- in the case of `CWrapper' and when `CFunction' |
|---|
| 976 | -- has a dynamic target) |
|---|
| 977 | -- |
|---|
| 978 | -- * the calling convention is irrelevant for code |
|---|
| 979 | -- generation in the case of `CLabel', but is needed |
|---|
| 980 | -- for pretty printing |
|---|
| 981 | -- |
|---|
| 982 | -- * `Safety' is irrelevant for `CLabel' and `CWrapper' |
|---|
| 983 | -- |
|---|
| 984 | CImport CCallConv -- ccall or stdcall |
|---|
| 985 | Safety -- interruptible, safe or unsafe |
|---|
| 986 | (Maybe Header) -- name of C header |
|---|
| 987 | CImportSpec -- details of the C entity |
|---|
| 988 | deriving (Data, Typeable) |
|---|
| 989 | |
|---|
| 990 | -- details of an external C entity |
|---|
| 991 | -- |
|---|
| 992 | data CImportSpec = CLabel CLabelString -- import address of a C label |
|---|
| 993 | | CFunction CCallTarget -- static or dynamic function |
|---|
| 994 | | CWrapper -- wrapper to expose closures |
|---|
| 995 | -- (former f.e.d.) |
|---|
| 996 | deriving (Data, Typeable) |
|---|
| 997 | |
|---|
| 998 | -- specification of an externally exported entity in dependence on the calling |
|---|
| 999 | -- convention |
|---|
| 1000 | -- |
|---|
| 1001 | data ForeignExport = CExport CExportSpec -- contains the calling convention |
|---|
| 1002 | deriving (Data, Typeable) |
|---|
| 1003 | |
|---|
| 1004 | -- pretty printing of foreign declarations |
|---|
| 1005 | -- |
|---|
| 1006 | |
|---|
| 1007 | instance OutputableBndr name => Outputable (ForeignDecl name) where |
|---|
| 1008 | ppr (ForeignImport n ty _ fimport) = |
|---|
| 1009 | hang (ptext (sLit "foreign import") <+> ppr fimport <+> ppr n) |
|---|
| 1010 | 2 (dcolon <+> ppr ty) |
|---|
| 1011 | ppr (ForeignExport n ty _ fexport) = |
|---|
| 1012 | hang (ptext (sLit "foreign export") <+> ppr fexport <+> ppr n) |
|---|
| 1013 | 2 (dcolon <+> ppr ty) |
|---|
| 1014 | |
|---|
| 1015 | instance Outputable ForeignImport where |
|---|
| 1016 | ppr (CImport cconv safety mHeader spec) = |
|---|
| 1017 | ppr cconv <+> ppr safety <+> |
|---|
| 1018 | char '"' <> pprCEntity spec <> char '"' |
|---|
| 1019 | where |
|---|
| 1020 | pp_hdr = case mHeader of |
|---|
| 1021 | Nothing -> empty |
|---|
| 1022 | Just (Header header) -> ftext header |
|---|
| 1023 | |
|---|
| 1024 | pprCEntity (CLabel lbl) = |
|---|
| 1025 | ptext (sLit "static") <+> pp_hdr <+> char '&' <> ppr lbl |
|---|
| 1026 | pprCEntity (CFunction (StaticTarget lbl _ isFun)) = |
|---|
| 1027 | ptext (sLit "static") |
|---|
| 1028 | <+> pp_hdr |
|---|
| 1029 | <+> (if isFun then empty else ptext (sLit "value")) |
|---|
| 1030 | <+> ppr lbl |
|---|
| 1031 | pprCEntity (CFunction (DynamicTarget)) = |
|---|
| 1032 | ptext (sLit "dynamic") |
|---|
| 1033 | pprCEntity (CWrapper) = ptext (sLit "wrapper") |
|---|
| 1034 | |
|---|
| 1035 | instance Outputable ForeignExport where |
|---|
| 1036 | ppr (CExport (CExportStatic lbl cconv)) = |
|---|
| 1037 | ppr cconv <+> char '"' <> ppr lbl <> char '"' |
|---|
| 1038 | \end{code} |
|---|
| 1039 | |
|---|
| 1040 | |
|---|
| 1041 | %************************************************************************ |
|---|
| 1042 | %* * |
|---|
| 1043 | \subsection{Transformation rules} |
|---|
| 1044 | %* * |
|---|
| 1045 | %************************************************************************ |
|---|
| 1046 | |
|---|
| 1047 | \begin{code} |
|---|
| 1048 | type LRuleDecl name = Located (RuleDecl name) |
|---|
| 1049 | |
|---|
| 1050 | data RuleDecl name |
|---|
| 1051 | = HsRule -- Source rule |
|---|
| 1052 | RuleName -- Rule name |
|---|
| 1053 | Activation |
|---|
| 1054 | [RuleBndr name] -- Forall'd vars; after typechecking this includes tyvars |
|---|
| 1055 | (Located (HsExpr name)) -- LHS |
|---|
| 1056 | NameSet -- Free-vars from the LHS |
|---|
| 1057 | (Located (HsExpr name)) -- RHS |
|---|
| 1058 | NameSet -- Free-vars from the RHS |
|---|
| 1059 | deriving (Data, Typeable) |
|---|
| 1060 | |
|---|
| 1061 | data RuleBndr name |
|---|
| 1062 | = RuleBndr (Located name) |
|---|
| 1063 | | RuleBndrSig (Located name) (HsWithBndrs (LHsType name)) |
|---|
| 1064 | deriving (Data, Typeable) |
|---|
| 1065 | |
|---|
| 1066 | collectRuleBndrSigTys :: [RuleBndr name] -> [HsWithBndrs (LHsType name)] |
|---|
| 1067 | collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ ty <- bndrs] |
|---|
| 1068 | |
|---|
| 1069 | instance OutputableBndr name => Outputable (RuleDecl name) where |
|---|
| 1070 | ppr (HsRule name act ns lhs _fv_lhs rhs _fv_rhs) |
|---|
| 1071 | = sep [text "{-# RULES" <+> doubleQuotes (ftext name) <+> ppr act, |
|---|
| 1072 | nest 4 (pp_forall <+> pprExpr (unLoc lhs)), |
|---|
| 1073 | nest 4 (equals <+> pprExpr (unLoc rhs) <+> text "#-}") ] |
|---|
| 1074 | where |
|---|
| 1075 | pp_forall | null ns = empty |
|---|
| 1076 | | otherwise = text "forall" <+> fsep (map ppr ns) <> dot |
|---|
| 1077 | |
|---|
| 1078 | instance OutputableBndr name => Outputable (RuleBndr name) where |
|---|
| 1079 | ppr (RuleBndr name) = ppr name |
|---|
| 1080 | ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty |
|---|
| 1081 | \end{code} |
|---|
| 1082 | |
|---|
| 1083 | |
|---|
| 1084 | %************************************************************************ |
|---|
| 1085 | %* * |
|---|
| 1086 | \subsection{Vectorisation declarations} |
|---|
| 1087 | %* * |
|---|
| 1088 | %************************************************************************ |
|---|
| 1089 | |
|---|
| 1090 | A vectorisation pragma, one of |
|---|
| 1091 | |
|---|
| 1092 | {-# VECTORISE f = closure1 g (scalar_map g) #-} |
|---|
| 1093 | {-# VECTORISE SCALAR f #-} |
|---|
| 1094 | {-# NOVECTORISE f #-} |
|---|
| 1095 | |
|---|
| 1096 | {-# VECTORISE type T = ty #-} |
|---|
| 1097 | {-# VECTORISE SCALAR type T #-} |
|---|
| 1098 | |
|---|
| 1099 | \begin{code} |
|---|
| 1100 | type LVectDecl name = Located (VectDecl name) |
|---|
| 1101 | |
|---|
| 1102 | data VectDecl name |
|---|
| 1103 | = HsVect |
|---|
| 1104 | (Located name) |
|---|
| 1105 | (Maybe (LHsExpr name)) -- 'Nothing' => SCALAR declaration |
|---|
| 1106 | | HsNoVect |
|---|
| 1107 | (Located name) |
|---|
| 1108 | | HsVectTypeIn -- pre type-checking |
|---|
| 1109 | Bool -- 'TRUE' => SCALAR declaration |
|---|
| 1110 | (Located name) |
|---|
| 1111 | (Maybe (Located name)) -- 'Nothing' => no right-hand side |
|---|
| 1112 | | HsVectTypeOut -- post type-checking |
|---|
| 1113 | Bool -- 'TRUE' => SCALAR declaration |
|---|
| 1114 | TyCon |
|---|
| 1115 | (Maybe TyCon) -- 'Nothing' => no right-hand side |
|---|
| 1116 | | HsVectClassIn -- pre type-checking |
|---|
| 1117 | (Located name) |
|---|
| 1118 | | HsVectClassOut -- post type-checking |
|---|
| 1119 | Class |
|---|
| 1120 | | HsVectInstIn -- pre type-checking (always SCALAR) |
|---|
| 1121 | (LHsType name) |
|---|
| 1122 | | HsVectInstOut -- post type-checking (always SCALAR) |
|---|
| 1123 | ClsInst |
|---|
| 1124 | deriving (Data, Typeable) |
|---|
| 1125 | |
|---|
| 1126 | lvectDeclName :: NamedThing name => LVectDecl name -> Name |
|---|
| 1127 | lvectDeclName (L _ (HsVect (L _ name) _)) = getName name |
|---|
| 1128 | lvectDeclName (L _ (HsNoVect (L _ name))) = getName name |
|---|
| 1129 | lvectDeclName (L _ (HsVectTypeIn _ (L _ name) _)) = getName name |
|---|
| 1130 | lvectDeclName (L _ (HsVectTypeOut _ tycon _)) = getName tycon |
|---|
| 1131 | lvectDeclName (L _ (HsVectClassIn (L _ name))) = getName name |
|---|
| 1132 | lvectDeclName (L _ (HsVectClassOut cls)) = getName cls |
|---|
| 1133 | lvectDeclName (L _ (HsVectInstIn _)) = panic "HsDecls.lvectDeclName: HsVectInstIn" |
|---|
| 1134 | lvectDeclName (L _ (HsVectInstOut _)) = panic "HsDecls.lvectDeclName: HsVectInstOut" |
|---|
| 1135 | |
|---|
| 1136 | lvectInstDecl :: LVectDecl name -> Bool |
|---|
| 1137 | lvectInstDecl (L _ (HsVectInstIn _)) = True |
|---|
| 1138 | lvectInstDecl (L _ (HsVectInstOut _)) = True |
|---|
| 1139 | lvectInstDecl _ = False |
|---|
| 1140 | |
|---|
| 1141 | instance OutputableBndr name => Outputable (VectDecl name) where |
|---|
| 1142 | ppr (HsVect v Nothing) |
|---|
| 1143 | = sep [text "{-# VECTORISE SCALAR" <+> ppr v <+> text "#-}" ] |
|---|
| 1144 | ppr (HsVect v (Just rhs)) |
|---|
| 1145 | = sep [text "{-# VECTORISE" <+> ppr v, |
|---|
| 1146 | nest 4 $ |
|---|
| 1147 | pprExpr (unLoc rhs) <+> text "#-}" ] |
|---|
| 1148 | ppr (HsNoVect v) |
|---|
| 1149 | = sep [text "{-# NOVECTORISE" <+> ppr v <+> text "#-}" ] |
|---|
| 1150 | ppr (HsVectTypeIn False t Nothing) |
|---|
| 1151 | = sep [text "{-# VECTORISE type" <+> ppr t <+> text "#-}" ] |
|---|
| 1152 | ppr (HsVectTypeIn False t (Just t')) |
|---|
| 1153 | = sep [text "{-# VECTORISE type" <+> ppr t, text "=", ppr t', text "#-}" ] |
|---|
| 1154 | ppr (HsVectTypeIn True t Nothing) |
|---|
| 1155 | = sep [text "{-# VECTORISE SCALAR type" <+> ppr t <+> text "#-}" ] |
|---|
| 1156 | ppr (HsVectTypeIn True t (Just t')) |
|---|
| 1157 | = sep [text "{-# VECTORISE SCALAR type" <+> ppr t, text "=", ppr t', text "#-}" ] |
|---|
| 1158 | ppr (HsVectTypeOut False t Nothing) |
|---|
| 1159 | = sep [text "{-# VECTORISE type" <+> ppr t <+> text "#-}" ] |
|---|
| 1160 | ppr (HsVectTypeOut False t (Just t')) |
|---|
| 1161 | = sep [text "{-# VECTORISE type" <+> ppr t, text "=", ppr t', text "#-}" ] |
|---|
| 1162 | ppr (HsVectTypeOut True t Nothing) |
|---|
| 1163 | = sep [text "{-# VECTORISE SCALAR type" <+> ppr t <+> text "#-}" ] |
|---|
| 1164 | ppr (HsVectTypeOut True t (Just t')) |
|---|
| 1165 | = sep [text "{-# VECTORISE SCALAR type" <+> ppr t, text "=", ppr t', text "#-}" ] |
|---|
| 1166 | ppr (HsVectClassIn c) |
|---|
| 1167 | = sep [text "{-# VECTORISE class" <+> ppr c <+> text "#-}" ] |
|---|
| 1168 | ppr (HsVectClassOut c) |
|---|
| 1169 | = sep [text "{-# VECTORISE class" <+> ppr c <+> text "#-}" ] |
|---|
| 1170 | ppr (HsVectInstIn ty) |
|---|
| 1171 | = sep [text "{-# VECTORISE SCALAR instance" <+> ppr ty <+> text "#-}" ] |
|---|
| 1172 | ppr (HsVectInstOut i) |
|---|
| 1173 | = sep [text "{-# VECTORISE SCALAR instance" <+> ppr i <+> text "#-}" ] |
|---|
| 1174 | \end{code} |
|---|
| 1175 | |
|---|
| 1176 | %************************************************************************ |
|---|
| 1177 | %* * |
|---|
| 1178 | \subsection[DocDecl]{Document comments} |
|---|
| 1179 | %* * |
|---|
| 1180 | %************************************************************************ |
|---|
| 1181 | |
|---|
| 1182 | \begin{code} |
|---|
| 1183 | |
|---|
| 1184 | type LDocDecl = Located (DocDecl) |
|---|
| 1185 | |
|---|
| 1186 | data DocDecl |
|---|
| 1187 | = DocCommentNext HsDocString |
|---|
| 1188 | | DocCommentPrev HsDocString |
|---|
| 1189 | | DocCommentNamed String HsDocString |
|---|
| 1190 | | DocGroup Int HsDocString |
|---|
| 1191 | deriving (Data, Typeable) |
|---|
| 1192 | |
|---|
| 1193 | -- Okay, I need to reconstruct the document comments, but for now: |
|---|
| 1194 | instance Outputable DocDecl where |
|---|
| 1195 | ppr _ = text "<document comment>" |
|---|
| 1196 | |
|---|
| 1197 | docDeclDoc :: DocDecl -> HsDocString |
|---|
| 1198 | docDeclDoc (DocCommentNext d) = d |
|---|
| 1199 | docDeclDoc (DocCommentPrev d) = d |
|---|
| 1200 | docDeclDoc (DocCommentNamed _ d) = d |
|---|
| 1201 | docDeclDoc (DocGroup _ d) = d |
|---|
| 1202 | |
|---|
| 1203 | \end{code} |
|---|
| 1204 | |
|---|
| 1205 | %************************************************************************ |
|---|
| 1206 | %* * |
|---|
| 1207 | \subsection[DeprecDecl]{Deprecations} |
|---|
| 1208 | %* * |
|---|
| 1209 | %************************************************************************ |
|---|
| 1210 | |
|---|
| 1211 | We use exported entities for things to deprecate. |
|---|
| 1212 | |
|---|
| 1213 | \begin{code} |
|---|
| 1214 | type LWarnDecl name = Located (WarnDecl name) |
|---|
| 1215 | |
|---|
| 1216 | data WarnDecl name = Warning name WarningTxt |
|---|
| 1217 | deriving (Data, Typeable) |
|---|
| 1218 | |
|---|
| 1219 | instance OutputableBndr name => Outputable (WarnDecl name) where |
|---|
| 1220 | ppr (Warning thing txt) |
|---|
| 1221 | = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"] |
|---|
| 1222 | \end{code} |
|---|
| 1223 | |
|---|
| 1224 | %************************************************************************ |
|---|
| 1225 | %* * |
|---|
| 1226 | \subsection[AnnDecl]{Annotations} |
|---|
| 1227 | %* * |
|---|
| 1228 | %************************************************************************ |
|---|
| 1229 | |
|---|
| 1230 | \begin{code} |
|---|
| 1231 | type LAnnDecl name = Located (AnnDecl name) |
|---|
| 1232 | |
|---|
| 1233 | data AnnDecl name = HsAnnotation (AnnProvenance name) (Located (HsExpr name)) |
|---|
| 1234 | deriving (Data, Typeable) |
|---|
| 1235 | |
|---|
| 1236 | instance (OutputableBndr name) => Outputable (AnnDecl name) where |
|---|
| 1237 | ppr (HsAnnotation provenance expr) |
|---|
| 1238 | = hsep [text "{-#", pprAnnProvenance provenance, pprExpr (unLoc expr), text "#-}"] |
|---|
| 1239 | |
|---|
| 1240 | |
|---|
| 1241 | data AnnProvenance name = ValueAnnProvenance name |
|---|
| 1242 | | TypeAnnProvenance name |
|---|
| 1243 | | ModuleAnnProvenance |
|---|
| 1244 | deriving (Data, Typeable) |
|---|
| 1245 | |
|---|
| 1246 | annProvenanceName_maybe :: AnnProvenance name -> Maybe name |
|---|
| 1247 | annProvenanceName_maybe (ValueAnnProvenance name) = Just name |
|---|
| 1248 | annProvenanceName_maybe (TypeAnnProvenance name) = Just name |
|---|
| 1249 | annProvenanceName_maybe ModuleAnnProvenance = Nothing |
|---|
| 1250 | |
|---|
| 1251 | -- TODO: Replace with Traversable instance when GHC bootstrap version rises high enough |
|---|
| 1252 | modifyAnnProvenanceNameM :: Monad m => (before -> m after) -> AnnProvenance before -> m (AnnProvenance after) |
|---|
| 1253 | modifyAnnProvenanceNameM fm prov = |
|---|
| 1254 | case prov of |
|---|
| 1255 | ValueAnnProvenance name -> liftM ValueAnnProvenance (fm name) |
|---|
| 1256 | TypeAnnProvenance name -> liftM TypeAnnProvenance (fm name) |
|---|
| 1257 | ModuleAnnProvenance -> return ModuleAnnProvenance |
|---|
| 1258 | |
|---|
| 1259 | pprAnnProvenance :: OutputableBndr name => AnnProvenance name -> SDoc |
|---|
| 1260 | pprAnnProvenance ModuleAnnProvenance = ptext (sLit "ANN module") |
|---|
| 1261 | pprAnnProvenance (ValueAnnProvenance name) = ptext (sLit "ANN") <+> ppr name |
|---|
| 1262 | pprAnnProvenance (TypeAnnProvenance name) = ptext (sLit "ANN type") <+> ppr name |
|---|
| 1263 | \end{code} |
|---|