[project @ 2000-08-01 09:08:25 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
1 {-      Notes about the syntax of interface files
2         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 The header
4 ~~~~~~~~~~
5   interface "edison" M 4 6 2 ! 406      Module M, version 4, from package 'edison',
6                                         Fixities version 6, rules version 2
7                                         Interface syntax version 406
8                                         ! means M contains orphans
9
10 Import declarations
11 ~~~~~~~~~~~~~~~~~~~
12   import Foo ;                          To compile M I used nothing from Foo, but it's 
13                                         below me in the hierarchy
14
15   import Foo ! @ ;                      Ditto, but the ! means that Foo contains orphans
16                                         and        the @ means that Foo is a boot interface
17
18   import Foo :: 3 ;                     To compile M I used everything from Foo, which has 
19                                         module version 3
20
21   import Foo :: 3 2 6 a 1 b 3 c 7 ;     To compile M I used Foo.  It had 
22                                                 module version 3
23                                                 fixity version 2
24                                                 rules  version 6
25                                         and some specific things besides.
26
27 -}
28
29
30 {
31 module ParseIface ( parseIface, IfaceStuff(..) ) where
32
33 #include "HsVersions.h"
34
35 import HsSyn            -- quite a bit of stuff
36 import RdrHsSyn         -- oodles of synonyms
37 import HsTypes          ( mkHsForAllTy, mkHsUsForAllTy, mkHsTupCon )
38 import HsCore
39 import Demand           ( mkStrictnessInfo )
40 import Literal          ( Literal(..), mkMachInt, mkMachInt64, mkMachWord, mkMachWord64 )
41 import BasicTypes       ( Fixity(..), FixityDirection(..), 
42                           NewOrData(..), Version, initialVersion, Boxity(..)
43                         )
44 import CostCentre       ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
45 import CallConv         ( cCallConv )
46 import HsPragmas        ( noDataPragmas, noClassPragmas )
47 import Type             ( Kind, mkArrowKind, boxedTypeKind, openTypeKind, UsageAnn(..) )
48 import IdInfo           ( ArityInfo, exactArity, CprInfo(..), InlinePragInfo(..) )
49 import PrimOp           ( CCall(..), CCallTarget(..) )
50 import Lex              
51
52 import RnMonad          ( ImportVersion, ParsedIface(..), WhatsImported(..),
53                           RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..), 
54                           WhetherHasOrphans, IsBootInterface
55                         ) 
56 import Bag              ( emptyBag, unitBag, snocBag )
57 import FiniteMap        ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
58 import RdrName          ( RdrName, mkRdrUnqual, mkSysQual, mkSysUnqual, mkRdrNameWkr )
59 import Name             ( OccName, Provenance )
60 import OccName          ( mkSysOccFS,
61                           tcName, varName, ipName, dataName, clsName, tvName, uvName,
62                           EncodedFS 
63                         )
64 import Module           ( ModuleName, PackageName, mkSysModuleFS, mkModule )                    
65 import SrcLoc           ( SrcLoc )
66 import CmdLineOpts      ( opt_InPackage )
67 import Maybes
68 import Outputable
69 import List             ( insert )
70
71 import GlaExts
72 import FastString       ( tailFS )
73
74 #if __HASKELL1__ > 4
75 import Ratio ( (%) )
76 #endif
77 }
78
79 %name       parseIface
80 %tokentype  { Token }
81 %monad      { P }{ thenP }{ returnP }
82 %lexer      { lexer } { ITeof }
83
84 %token
85  'as'           { ITas }
86  'case'         { ITcase }                      -- Haskell keywords
87  'class'        { ITclass } 
88  'data'         { ITdata } 
89  'default'      { ITdefault }
90  'deriving'     { ITderiving }
91  'do'           { ITdo }
92  'else'         { ITelse }
93  'hiding'       { IThiding }
94  'if'           { ITif }
95  'import'       { ITimport }
96  'in'           { ITin }
97  'infix'        { ITinfix }
98  'infixl'       { ITinfixl }
99  'infixr'       { ITinfixr }
100  'instance'     { ITinstance }
101  'let'          { ITlet }
102  'module'       { ITmodule }
103  'newtype'      { ITnewtype }
104  'of'           { ITof }
105  'qualified'    { ITqualified }
106  'then'         { ITthen }
107  'type'         { ITtype }
108  'where'        { ITwhere }
109
110  'forall'       { ITforall }                    -- GHC extension keywords
111  'foreign'      { ITforeign }
112  'export'       { ITexport }
113  'label'        { ITlabel } 
114  'dynamic'      { ITdynamic }
115  'unsafe'       { ITunsafe }
116  'with'         { ITwith }
117  'stdcall'      { ITstdcallconv }
118  'ccall'        { ITccallconv }
119
120  '__interface'  { ITinterface }                 -- interface keywords
121  '__export'     { IT__export }
122  '__depends'    { ITdepends }
123  '__forall'     { IT__forall }
124  '__letrec'     { ITletrec }
125  '__coerce'     { ITcoerce }
126  '__inline_me'  { ITinlineMe }
127  '__inline_call'{ ITinlineCall }
128  '__DEFAULT'    { ITdefaultbranch }
129  '__bot'        { ITbottom }
130  '__integer'    { ITinteger_lit }
131  '__float'      { ITfloat_lit }
132  '__word'       { ITword_lit }
133  '__int64'      { ITint64_lit }
134  '__word64'     { ITword64_lit }
135  '__rational'   { ITrational_lit }
136  '__addr'       { ITaddr_lit }
137  '__label'      { ITlabel_lit }
138  '__litlit'     { ITlit_lit }
139  '__string'     { ITstring_lit }
140  '__ccall'      { ITccall $$ }
141  '__scc'        { ITscc }
142  '__sccC'       { ITsccAllCafs }
143
144  '__u'          { ITusage }
145  '__fuall'      { ITfuall }
146
147  '__A'          { ITarity }
148  '__P'          { ITspecialise }
149  '__C'          { ITnocaf }
150  '__U'          { ITunfold $$ }
151  '__S'          { ITstrict $$ }
152  '__R'          { ITrules }
153  '__M'          { ITcprinfo }
154  '__D'          { ITdeprecated }
155
156  '..'           { ITdotdot }                    -- reserved symbols
157  '::'           { ITdcolon }
158  '='            { ITequal }
159  '\\'           { ITlam }
160  '|'            { ITvbar }
161  '<-'           { ITlarrow }
162  '->'           { ITrarrow }
163  '@'            { ITat }
164  '~'            { ITtilde }
165  '=>'           { ITdarrow }
166  '-'            { ITminus }
167  '!'            { ITbang }
168
169  '/\\'          { ITbiglam }                    -- GHC-extension symbols
170
171  '{'            { ITocurly }                    -- special symbols
172  '}'            { ITccurly }
173  '['            { ITobrack }
174  ']'            { ITcbrack }
175  '('            { IToparen }
176  ')'            { ITcparen }
177  '(#'           { IToubxparen }
178  '#)'           { ITcubxparen }
179  ';'            { ITsemi }
180  ','            { ITcomma }
181
182  VARID          { ITvarid    $$ }               -- identifiers
183  CONID          { ITconid    $$ }
184  VARSYM         { ITvarsym   $$ }
185  CONSYM         { ITconsym   $$ }
186  QVARID         { ITqvarid   $$ }
187  QCONID         { ITqconid   $$ }
188  QVARSYM        { ITqvarsym  $$ }
189  QCONSYM        { ITqconsym  $$ }
190
191  IPVARID        { ITipvarid  $$ }               -- GHC extension
192
193  PRAGMA         { ITpragma   $$ }
194
195  CHAR           { ITchar     $$ }
196  STRING         { ITstring   $$ }
197  INTEGER        { ITinteger  $$ }
198  RATIONAL       { ITrational $$ }
199  CLITLIT        { ITlitlit   $$ }
200
201  UNKNOWN        { ITunknown  $$ }
202 %%
203
204 -- iface_stuff is the main production.
205 -- It recognises (a) a whole interface file
206 --               (b) a type (so that type sigs can be parsed lazily)
207 --               (c) the IdInfo part of a signature (same reason)
208
209 iface_stuff :: { IfaceStuff }
210 iface_stuff : iface             { PIface   $1 }
211             | type              { PType    $1 }
212             | id_info           { PIdInfo  $1 }
213             | '__R' rules       { PRules   $2 }
214             | '__D' deprecs     { PDeprecs $2 }
215
216
217 iface           :: { ParsedIface }
218 iface           : '__interface' package mod_name 
219                         version sub_versions
220                         orphans checkVersion 'where'
221                   exports_part
222                   import_part
223                   fix_decl_part
224                   instance_decl_part
225                   decls_part
226                   rules_and_deprecs
227                   { ParsedIface {
228                         pi_mod  = mkModule $3 $2,       -- Module itself
229                         pi_vers = $4,                   -- Module version
230                         pi_orphan  = $6,
231                         pi_exports = $9,                -- Exports
232                         pi_usages  = $10,               -- Usages
233                         pi_fixity  = (fst $5,$11),      -- Fixies
234                         pi_insts   = $12,               -- Local instances
235                         pi_decls   = $13,               -- Decls
236                         pi_rules   = (snd $5,fst $14),  -- Rules 
237                         pi_deprecs = snd $14            -- Deprecations 
238                    } }
239
240 -- Versions for fixities and rules (optional)
241 sub_versions :: { (Version,Version) }
242         : '[' version version ']'               { ($2,$3) }
243         | {- empty -}                           { (initialVersion, initialVersion) }
244
245 --------------------------------------------------------------------------
246
247 import_part :: { [ImportVersion OccName] }
248 import_part :                                             { [] }
249             |  import_decl import_part                    { $1 : $2 }
250             
251 import_decl :: { ImportVersion OccName }
252 import_decl : 'import' mod_name orphans is_boot whats_imported ';'
253                         { (mkSysModuleFS $2, $3, $4, $5) }
254
255 orphans             :: { WhetherHasOrphans }
256 orphans             :                                           { False }
257                     | '!'                                       { True }
258
259 is_boot             :: { IsBootInterface }
260 is_boot             :                                           { False }
261                     | '@'                                       { True }
262
263 whats_imported      :: { WhatsImported OccName }
264 whats_imported      :                                                   { NothingAtAll }
265                     | '::' version                                      { Everything $2 }
266                     | '::' version version version name_version_pairs   { Specifically $2 $3 $4 $5 }
267
268 name_version_pairs  ::  { [(OccName, Version)] }
269 name_version_pairs  :                                           { [] }
270                     |  name_version_pair name_version_pairs     { $1 : $2 }
271
272 name_version_pair   ::  { (OccName, Version) }
273 name_version_pair   :  var_occ version                          { ($1, $2) }
274                     |  tc_occ  version                          { ($1, $2) }
275
276
277 --------------------------------------------------------------------------
278
279 exports_part    :: { [ExportItem] }
280 exports_part    :                                       { [] }
281                 | '__export' mod_name entities ';'
282                         exports_part                    { (mkSysModuleFS $2, $3) : $5 }
283
284 entities        :: { [RdrAvailInfo] }
285 entities        :                                       { [] }
286                 |  entity entities                      { $1 : $2 }
287
288 entity          :: { RdrAvailInfo }
289 entity          :  var_occ                              { Avail $1 }
290                 |  tc_occ                               { AvailTC $1 [$1] }
291                 |  tc_occ '|' stuff_inside              { AvailTC $1 $3 }
292                 |  tc_occ stuff_inside                  { AvailTC $1 (insert $1 $2) }
293                 -- The 'insert' is important.  The stuff_inside is sorted, and
294                 -- insert keeps it that way.  This is important when comparing 
295                 -- against the new interface file, which has the stuff in sorted order
296                 -- If they differ, we'll bump the module number when it's unnecessary
297
298 stuff_inside    :: { [OccName] }
299 stuff_inside    :  '{' val_occs '}'                     { $2 }
300
301 val_occ         :: { OccName }
302                 :  var_occ              { $1 }
303                 |  data_occ             { $1 }
304
305 val_occs        :: { [OccName] }
306                 :  val_occ              { [$1] }
307                 |  val_occ val_occs     { $1 : $2 }
308
309
310 --------------------------------------------------------------------------
311
312 fix_decl_part :: { [RdrNameFixitySig] }
313 fix_decl_part : {- empty -}                             { [] }
314               | fix_decls ';'                           { $1 }
315
316 fix_decls     :: { [RdrNameFixitySig] }
317 fix_decls     :                                         { [] }
318               | fix_decl fix_decls                      { $1 : $2 }
319
320 fix_decl :: { RdrNameFixitySig }
321 fix_decl : src_loc fixity prec var_or_data_name         { FixitySig $4 (Fixity $3 $2) $1 }
322
323 fixity      :: { FixityDirection }
324 fixity      : 'infixl'                                  { InfixL }
325             | 'infixr'                                  { InfixR }
326             | 'infix'                                   { InfixN }
327    
328 prec        :: { Int }
329 prec        : INTEGER                                   { fromInteger $1 }
330
331 -----------------------------------------------------------------------------
332
333 csigs           :: { [RdrNameSig] }
334 csigs           :                               { [] }
335                 | 'where' '{' csigs1 '}'        { $3 }
336
337 csigs1          :: { [RdrNameSig] }
338 csigs1          :                               { [] }
339                 | csig ';' csigs1               { $1 : $3 }
340
341 csig            :: { RdrNameSig }
342 csig            :  src_loc var_name '::' type           { mkClassOpSig False $2 $4 $1 }
343                 |  src_loc var_name '=' '::' type       { mkClassOpSig True  $2 $5 $1 }
344
345 --------------------------------------------------------------------------
346
347 instance_decl_part :: { [RdrNameInstDecl] }
348 instance_decl_part : {- empty -}                       { [] }
349                    | instance_decl_part inst_decl      { $2 : $1 }
350
351 inst_decl       :: { RdrNameInstDecl }
352 inst_decl       :  src_loc 'instance' type '=' var_name ';'
353                         { InstDecl $3
354                                    EmptyMonoBinds       {- No bindings -}
355                                    []                   {- No user pragmas -}
356                                    (Just $5)            {- Dfun id -}
357                                    $1
358                         }
359
360 --------------------------------------------------------------------------
361
362 decls_part :: { [(Version, RdrNameHsDecl)] }
363 decls_part 
364         :  {- empty -}                          { [] }
365         |  opt_version decl ';' decls_part              { ($1,$2):$4 }
366
367 decl    :: { RdrNameHsDecl }
368 decl    : src_loc var_name '::' type maybe_idinfo
369                          { SigD (IfaceSig $2 $4 ($5 $2) $1) }
370         | src_loc 'type' tc_name tv_bndrs '=' type                     
371                         { TyClD (TySynonym $3 $4 $6 $1) }
372         | src_loc 'data' opt_decl_context tc_name tv_bndrs constrs             
373                         { TyClD (TyData DataType $3 $4 $5 $6 (length $6) Nothing noDataPragmas $1) }
374         | src_loc 'newtype' opt_decl_context tc_name tv_bndrs newtype_constr
375                         { TyClD (TyData NewType $3 $4 $5 $6 1 Nothing noDataPragmas $1) }
376         | src_loc 'class' opt_decl_context tc_name tv_bndrs fds csigs
377                         { TyClD (mkClassDecl $3 $4 $5 $6 $7 EmptyMonoBinds 
378                                         noClassPragmas $1) }
379
380 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
381 maybe_idinfo  : {- empty -}     { \_ -> [] }
382               | pragma          { \x -> case $1 of
383                                      POk _ (PIdInfo id_info) -> id_info
384                                      PFailed err -> 
385                                         pprPanic "IdInfo parse failed" 
386                                             (vcat [ppr x, err])
387                                 }
388
389 pragma  :: { ParseResult IfaceStuff }
390 pragma  : src_loc PRAGMA        { parseIface $2 PState{ bol = 0#, atbol = 1#,
391                                                         context = [],
392                                                         glasgow_exts = 1#,
393                                                         loc = $1 }
394                                 }
395
396 -----------------------------------------------------------------------------
397
398 rules_and_deprecs :: { ([RdrNameRuleDecl], [RdrNameDeprecation]) }
399 rules_and_deprecs : {- empty -} { ([], []) }
400                   | rules_and_deprecs rule_or_deprec
401                                 { let
402                                      append2 (xs1,ys1) (xs2,ys2) =
403                                         (xs1 `app` xs2, ys1 `app` ys2)
404                                      xs `app` [] = xs -- performance paranoia
405                                      xs `app` ys = xs ++ ys
406                                   in append2 $1 $2
407                                 }
408
409 rule_or_deprec :: { ([RdrNameRuleDecl], [RdrNameDeprecation]) }
410 rule_or_deprec : pragma { case $1 of
411                              POk _ (PRules   rules)   -> (rules,[])
412                              POk _ (PDeprecs deprecs) -> ([],deprecs)
413                              PFailed err -> pprPanic "Rules/Deprecations parse failed" err
414                         }
415
416 -----------------------------------------------------------------------------
417
418 rules      :: { [RdrNameRuleDecl] }
419            : {- empty -}        { [] }
420            | rule ';' rules     { $1:$3 }
421
422 rule       :: { RdrNameRuleDecl }
423 rule       : src_loc STRING rule_forall qvar_name 
424              core_args '=' core_expr    { IfaceRule $2 $3 $4 $5 $7 $1 } 
425
426 rule_forall     :: { [UfBinder RdrName] }
427 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
428                   
429 -----------------------------------------------------------------------------
430
431 deprecs         :: { [RdrNameDeprecation] }
432 deprecs         : {- empty -}           { [] }
433                 | deprec ';' deprecs    { $1 : $3 }
434
435 deprec          :: { RdrNameDeprecation }
436 deprec          : src_loc STRING                { Deprecation (IEModuleContents undefined) $2 $1 }
437                 | src_loc deprec_name STRING    { Deprecation $2 $3 $1 }
438
439 -- SUP: TEMPORARY HACK
440 deprec_name     :: { RdrNameIE }
441                 : var_name              { IEVar      $1 }
442                 | data_name             { IEThingAbs $1 }
443
444 -----------------------------------------------------------------------------
445
446 version         :: { Version }
447 version         :  INTEGER                      { fromInteger $1 }
448
449 opt_version     :: { Version }
450 opt_version     : version                       { $1 }
451                 | {- empty -}                   { initialVersion }
452         
453 opt_decl_context  :: { RdrNameContext }
454 opt_decl_context  :                             { [] }
455                   | context '=>'                { $1 }
456
457 ----------------------------------------------------------------------------
458
459 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
460                 :                       { [] }
461                 | '=' constrs1          { $2 }
462
463 constrs1        :: { [RdrNameConDecl] }
464 constrs1        :  constr               { [$1] }
465                 |  constr '|' constrs1  { $1 : $3 }
466
467 constr          :: { RdrNameConDecl }
468 constr          :  src_loc ex_stuff data_name batypes           { mk_con_decl $3 $2 (VanillaCon $4) $1 }
469                 |  src_loc ex_stuff data_name '{' fields1 '}'   { mk_con_decl $3 $2 (RecCon $5)     $1 }
470                 -- We use "data_fs" so as to include ()
471
472 newtype_constr  :: { [RdrNameConDecl] {- Not allowed to be empty -} }
473 newtype_constr  : src_loc '=' ex_stuff data_name atype  { [mk_con_decl $4 $3 (NewCon $5 Nothing) $1] }
474                 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
475                                                         { [mk_con_decl $4 $3 (NewCon $8 (Just $6)) $1] }
476
477 ex_stuff :: { ([HsTyVarBndr RdrName], RdrNameContext) }
478 ex_stuff        :                                       { ([],[]) }
479                 | '__forall' tv_bndrs opt_context '=>'  { ($2,$3) }
480
481 batypes         :: { [RdrNameBangType] }
482 batypes         :                                       { [] }
483                 |  batype batypes                       { $1 : $2 }
484
485 batype          :: { RdrNameBangType }
486 batype          :  atype                                { Unbanged $1 }
487                 |  '!' atype                            { Banged   $2 }
488                 |  '!' '!' atype                        { Unpacked $3 }
489
490 fields1         :: { [([RdrName], RdrNameBangType)] }
491 fields1         : field                                 { [$1] }
492                 | field ',' fields1                     { $1 : $3 }
493
494 field           :: { ([RdrName], RdrNameBangType) }
495 field           :  var_names1 '::' type         { ($1, Unbanged $3) }
496                 |  var_names1 '::' '!' type     { ($1, Banged   $4) }
497                 |  var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
498 --------------------------------------------------------------------------
499
500 type            :: { RdrNameHsType }
501 type            : '__fuall'  fuall '=>' type    { mkHsUsForAllTy $2 $4 }
502                 | '__forall' tv_bndrs 
503                         opt_context '=>' type   { mkHsForAllTy (Just $2) $3 $5 }
504                 | btype '->' type               { HsFunTy $1 $3 }
505                 | btype                         { $1 }
506
507 fuall           :: { [RdrName] }
508 fuall           : '[' uv_bndrs ']'                      { $2 }
509
510 opt_context     :: { RdrNameContext }
511 opt_context     :                                       { [] }
512                 | context                               { $1 }
513
514 context         :: { RdrNameContext }
515 context         : '(' context_list1 ')'                 { $2 }
516                 | '{' context_list1 '}'                 { $2 }  -- Backward compatibility
517
518 context_list1   :: { RdrNameContext }
519 context_list1   : class                                 { [$1] }
520                 | class ',' context_list1               { $1 : $3 }
521
522 class           :: { HsPred RdrName }
523 class           :  qcls_name atypes                     { (HsPClass $1 $2) }
524                 |  ipvar_name '::' type                 { (HsPIParam $1 $3) }
525
526 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
527 types0          :  {- empty -}                          { [ ] }
528                 |  type                                 { [ $1 ] }
529                 |  types2                               { $1 }
530
531 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
532 types2          :  type ',' type                        { [$1,$3] }
533                 |  type ',' types2                      { $1 : $3 }
534
535 btype           :: { RdrNameHsType }
536 btype           :  atype                                { $1 }
537                 |  btype atype                          { HsAppTy $1 $2 }
538                 |  '__u' usage atype                    { HsUsgTy $2 $3 }
539
540 usage           :: { HsUsageAnn RdrName }
541 usage           : '-'                                   { HsUsOnce }
542                 | '!'                                   { HsUsMany }
543                 | uv_name                               { HsUsVar $1 }
544
545 atype           :: { RdrNameHsType }
546 atype           :  qtc_name                             { HsTyVar $1 }
547                 |  tv_name                              { HsTyVar $1 }
548                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
549                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
550                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
551                 |  '[' type ']'                         { HsListTy  $2 }
552                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
553                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
554                 |  '(' type ')'                         { $2 }
555
556 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
557 atypes          :                                       { [] }
558                 |  atype atypes                         { $1 : $2 }
559 ---------------------------------------------------------------------
560 package         :: { PackageName }
561                 :  STRING               { $1 }
562                 | {- empty -}           { opt_InPackage }       -- Useful for .hi-boot files,
563                                                                 -- which can omit the package Id
564                                                                 -- Module loops are always within a package
565
566 mod_name        :: { ModuleName }
567                 :  CONID                { mkSysModuleFS $1 }
568
569
570 ---------------------------------------------------
571 var_fs          :: { EncodedFS }
572                 : VARID                 { $1 }
573                 | '!'                   { SLIT("!") }
574                 | 'as'                  { SLIT("as") }
575                 | 'qualified'           { SLIT("qualified") }
576                 | 'hiding'              { SLIT("hiding") }
577                 | 'forall'              { SLIT("forall") }
578                 | 'foreign'             { SLIT("foreign") }
579                 | 'export'              { SLIT("export") }
580                 | 'label'               { SLIT("label") }
581                 | 'dynamic'             { SLIT("dynamic") }
582                 | 'unsafe'              { SLIT("unsafe") }
583                 | 'with'                { SLIT("with") }
584                 | 'ccall'               { SLIT("ccall") }
585                 | 'stdcall'             { SLIT("stdcall") }
586
587 qvar_fs         :: { (EncodedFS, EncodedFS) }
588                 :  QVARID               { $1 }
589                 |  QVARSYM              { $1 }
590
591 var_occ         :: { OccName }
592                 :  var_fs               { mkSysOccFS varName $1 }
593
594 var_name        :: { RdrName }
595 var_name        :  var_occ              { mkRdrUnqual $1 }
596
597 qvar_name       :: { RdrName }
598 qvar_name       :  var_name             { $1 }
599                 |  qvar_fs              { mkSysQual varName $1 }
600
601 ipvar_name      :: { RdrName }
602                 :  IPVARID              { mkSysUnqual ipName (tailFS $1) }
603
604 var_names       :: { [RdrName] }
605 var_names       :                       { [] }
606                 | var_name var_names    { $1 : $2 }
607
608 var_names1      :: { [RdrName] }
609 var_names1      : var_name var_names    { $1 : $2 }
610
611 ---------------------------------------------------
612 -- For some bizarre reason, 
613 --      (,,,)      is dealt with by the parser
614 --      Foo.(,,,)  is dealt with by the lexer
615 -- Sigh
616
617 data_fs         :: { EncodedFS }
618                 :  CONID                { $1 }
619                 |  CONSYM               { $1 }
620
621 qdata_fs        :: { (EncodedFS, EncodedFS) }
622                 :  QCONID               { $1 }
623                 |  QCONSYM              { $1 }
624
625 data_occ        :: { OccName }
626                 :  data_fs              { mkSysOccFS dataName $1 }
627
628 data_name       :: { RdrName }
629                 :  data_occ             { mkRdrUnqual $1 }
630
631 qdata_name      :: { RdrName }
632 qdata_name      :  data_name            { $1 }
633                 |  qdata_fs             { mkSysQual dataName $1 }
634                                 
635 qdata_names     :: { [RdrName] }
636 qdata_names     :                               { [] }
637                 | qdata_name qdata_names        { $1 : $2 }
638
639 var_or_data_name :: { RdrName }
640                   : var_name                    { $1 }
641                   | data_name                   { $1 }
642
643 ---------------------------------------------------
644 tc_fs           :: { EncodedFS }
645                 :  data_fs              { $1 }
646
647 tc_occ          :: { OccName }
648                 :  tc_fs                { mkSysOccFS tcName $1 }
649
650 tc_name         :: { RdrName }
651                 :  tc_occ               { mkRdrUnqual $1 }
652
653 qtc_name        :: { RdrName }
654                 : tc_name               { $1 }
655                 | qdata_fs              { mkSysQual tcName $1 }
656
657 ---------------------------------------------------
658 cls_name        :: { RdrName }
659                 :  data_fs              { mkSysUnqual clsName $1 }
660
661 qcls_name       :: { RdrName }
662                 : cls_name              { $1 }
663                 | qdata_fs              { mkSysQual clsName $1 }
664
665 ---------------------------------------------------
666 uv_name         :: { RdrName }
667                 :  VARID                { mkSysUnqual uvName $1 }
668
669 uv_bndr         :: { RdrName }
670                 :  uv_name              { $1 }
671
672 uv_bndrs        :: { [RdrName] }
673                 :                       { [] }
674                 | uv_bndr uv_bndrs      { $1 : $2 }
675
676 ---------------------------------------------------
677 tv_name         :: { RdrName }
678                 :  VARID                { mkSysUnqual tvName $1 }
679                 |  VARSYM               { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
680
681 tv_bndr         :: { HsTyVarBndr RdrName }
682                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
683                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
684
685 tv_bndrs        :: { [HsTyVarBndr RdrName] }
686 tv_bndrs        : tv_bndrs1             { $1 }
687                 | '[' tv_bndrs1 ']'     { $2 }  -- Backward compatibility
688
689 tv_bndrs1       :: { [HsTyVarBndr RdrName] }
690                 :                       { [] }
691                 | tv_bndr tv_bndrs1     { $1 : $2 }
692
693 ---------------------------------------------------
694 fds :: { [([RdrName], [RdrName])] }
695         : {- empty -}                   { [] }
696         | '|' fds1                      { reverse $2 }
697
698 fds1 :: { [([RdrName], [RdrName])] }
699         : fds1 ',' fd                   { $3 : $1 }
700         | fd                            { [$1] }
701
702 fd :: { ([RdrName], [RdrName]) }
703         : varids0 '->' varids0          { (reverse $1, reverse $3) }
704
705 varids0 :: { [RdrName] }
706         : {- empty -}                   { [] }
707         | varids0 tv_name               { $2 : $1 }
708
709 ---------------------------------------------------
710 kind            :: { Kind }
711                 : akind                 { $1 }
712                 | akind '->' kind       { mkArrowKind $1 $3 }
713
714 akind           :: { Kind }
715                 : VARSYM                { if $1 == SLIT("*") then
716                                                 boxedTypeKind
717                                           else if $1 == SLIT("?") then
718                                                 openTypeKind
719                                           else panic "ParseInterface: akind"
720                                         }
721                 | '(' kind ')'  { $2 }
722
723 --------------------------------------------------------------------------
724
725 id_info         :: { [HsIdInfo RdrName] }
726                 :                               { [] }
727                 | id_info_item id_info          { $1 : $2 }
728
729 id_info_item    :: { HsIdInfo RdrName }
730                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
731                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
732                 | '__M'                         { HsCprInfo }
733                 | '__S'                         { HsStrictness (mkStrictnessInfo $1) }
734                 | '__C'                         { HsNoCafRefs }
735                 | '__P' qvar_name               { HsWorker $2 }
736
737 inline_prag     :: { InlinePragInfo }
738                 :  {- empty -}                  { NoInlinePragInfo }
739                 | '[' from_prag phase ']'       { IMustNotBeINLINEd $2 $3 }
740
741 from_prag       :: { Bool }
742                 : {- empty -}                   { True }
743                 | '!'                           { False }
744
745 phase           :: { Maybe Int }
746                 : {- empty -}                   { Nothing }
747                 | INTEGER                       { Just (fromInteger $1) }
748
749 -------------------------------------------------------
750 core_expr       :: { UfExpr RdrName }
751 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
752                 | 'case' core_expr 'of' var_name
753                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
754
755                 | 'let' '{' core_val_bndr '=' core_expr
756                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
757                 | '__letrec' '{' rec_binds '}'          
758                   'in' core_expr                        { UfLet (UfRec $3) $6 }
759
760                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
761
762                 | fexpr                                 { $1 }
763
764 fexpr   :: { UfExpr RdrName }
765 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
766         | scc core_aexpr                                { UfNote (UfSCC $1) $2  }
767         | '__inline_me' core_aexpr                      { UfNote UfInlineMe $2 }
768         | '__inline_call' core_aexpr                    { UfNote UfInlineCall $2 }
769         | '__coerce' atype core_aexpr                   { UfNote (UfCoerce $2) $3 }
770         | core_aexpr                                    { $1 }
771
772 core_arg        :: { UfExpr RdrName }
773                 : '@' atype                                     { UfType $2 }
774                 | core_aexpr                                    { $1 }
775
776 core_args       :: { [UfExpr RdrName] }
777                 :                                               { [] }
778                 | core_arg core_args                            { $1 : $2 }
779
780 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
781 core_aexpr      : qvar_name                                     { UfVar $1 }
782                 | qdata_name                                    { UfVar $1 }
783
784                 | core_lit               { UfLit $1 }
785                 | '(' core_expr ')'      { $2 }
786
787                 | '('  ')'               { UfTuple (mkHsTupCon dataName Boxed [])   [] }
788                 | '(' comma_exprs2 ')'   { UfTuple (mkHsTupCon dataName Boxed $2)   $2 }
789                 | '(#' comma_exprs0 '#)' { UfTuple (mkHsTupCon dataName Unboxed $2) $2 }
790
791                 | '{' '__ccall' ccall_string type '}'       
792                            { let
793                                  (is_dyn, is_casm, may_gc) = $2
794
795                                  target | is_dyn    = DynamicTarget (error "CCall dyn target bogus unique")
796                                         | otherwise = StaticTarget $3
797
798                                  ccall = CCall target is_casm may_gc cCallConv
799                              in
800                              UfCCall ccall $4
801                            }
802
803
804 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
805 comma_exprs0    : {- empty -}                   { [ ] }
806                 | core_expr                     { [ $1 ] }
807                 | comma_exprs2                  { $1 }
808
809 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
810 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
811                 | core_expr ',' comma_exprs2                    { $1 : $3 }
812
813 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
814                 :                                               { [] }
815                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
816
817 core_alts       :: { [UfAlt RdrName] }
818                 :                                               { [] }
819                 | core_alt ';' core_alts                        { $1 : $3 }
820
821 core_alt        :: { UfAlt RdrName }
822 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
823
824 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
825 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
826                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
827                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
828                 | '('  ')'                      { (UfTupleAlt (mkHsTupCon dataName Boxed []),   []) }
829                 | '(' comma_var_names1 ')'      { (UfTupleAlt (mkHsTupCon dataName Boxed $2),   $2) }
830                 | '(#' comma_var_names1 '#)'    { (UfTupleAlt (mkHsTupCon dataName Unboxed $2), $2) }
831                 | '__DEFAULT'                   { (UfDefault, []) }
832                 | '(' core_pat ')'              { $2 }
833
834 core_pat_names :: { [RdrName] }
835 core_pat_names :                                { [] }
836                 | core_pat_name core_pat_names  { $1 : $2 }
837
838 -- Tyvar names and variable names live in different name spaces
839 -- so they need to be signalled separately.  But we don't record 
840 -- types or kinds in a pattern; we work that out from the type 
841 -- of the case scrutinee
842 core_pat_name   :: { RdrName }
843 core_pat_name   : var_name                      { $1 }
844                 | '@' tv_name                   { $2 }
845         
846 comma_var_names1 :: { [RdrName] }       -- One or more
847 comma_var_names1 : var_name                                     { [$1] }
848                  | var_name ',' comma_var_names1                { $1 : $3 }
849
850 core_lit        :: { Literal }
851 core_lit        : integer                       { mkMachInt $1 }
852                 | CHAR                          { MachChar $1 }
853                 | STRING                        { MachStr $1 }
854                 | rational                      { MachDouble $1 }
855                 | '__word' integer              { mkMachWord $2 }
856                 | '__word64' integer            { mkMachWord64 $2 }
857                 | '__int64' integer             { mkMachInt64 $2 }
858                 | '__float' rational            { MachFloat $2 }
859                 | '__addr' integer              { MachAddr $2 }
860                 | '__label' STRING              { MachLabel $2 }
861
862 integer         :: { Integer }
863                 : INTEGER                       { $1 }
864                 | '-' INTEGER                   { (-$2) }
865
866 rational        :: { Rational }
867                 : RATIONAL                      { $1 }
868                 | '-' RATIONAL                  { (-$2) }
869
870 core_bndr       :: { UfBinder RdrName }
871 core_bndr       : core_val_bndr                                 { $1 }
872                 | core_tv_bndr                                  { $1 }
873
874 core_bndrs      :: { [UfBinder RdrName] }
875 core_bndrs      :                                               { [] }
876                 | core_bndr core_bndrs                          { $1 : $2 }
877
878 core_val_bndr   :: { UfBinder RdrName }
879 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
880
881 core_tv_bndr    :: { UfBinder RdrName }
882 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
883                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
884
885 ccall_string    :: { FAST_STRING }
886                 : STRING                                        { $1 }
887                 | CLITLIT                                       { $1 }
888                 | VARID                                         { $1 }
889                 | CONID                                         { $1 }
890
891 ------------------------------------------------------------------------
892 scc     :: { CostCentre }
893         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
894         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
895                              { NormalCC { cc_name = $3, cc_mod = $4,
896                                           cc_is_dupd = $5, cc_is_caf = $6 } }
897
898 cc_name :: { EncodedFS }
899         : CONID                 { $1 }
900         | var_fs                { $1 }
901   
902 cc_dup  :: { IsDupdCC }
903 cc_dup  :                       { OriginalCC }
904         | '!'                   { DupdCC }
905
906 cc_caf  :: { IsCafCC }
907         :                       { NotCafCC }
908         | '__C'                 { CafCC }
909
910 -------------------------------------------------------------------
911
912 src_loc :: { SrcLoc }
913 src_loc :                               {% getSrcLocP }
914
915 -- Check the project version: this makes sure
916 -- that the project version (e.g. 407) in the interface
917 -- file is the same as that for the compiler that's reading it
918 checkVersion :: { () }
919            : {-empty-}                  {% checkVersion Nothing }
920            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
921
922 ------------------------------------------------------------------- 
923
924 --                      Haskell code 
925 {
926 happyError :: P a
927 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
928
929 data IfaceStuff = PIface        ParsedIface
930                 | PIdInfo       [HsIdInfo RdrName]
931                 | PType         RdrNameHsType
932                 | PRules        [RdrNameRuleDecl]
933                 | PDeprecs      [RdrNameDeprecation]
934
935 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
936 }