[project @ 2000-02-09 18:32:09 by lewie]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
1 {
2 module ParseIface ( parseIface, IfaceStuff(..) ) where
3
4 #include "HsVersions.h"
5
6 import HsSyn            -- quite a bit of stuff
7 import RdrHsSyn         -- oodles of synonyms
8 import HsTypes          ( mkHsForAllTy, mkHsUsForAllTy )
9 import HsCore
10 import Const            ( Literal(..), mkMachInt_safe )
11 import BasicTypes       ( Fixity(..), FixityDirection(..), 
12                           NewOrData(..), Version
13                         )
14 import CostCentre       ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
15 import HsPragmas        ( noDataPragmas, noClassPragmas )
16 import Type             ( Kind, mkArrowKind, boxedTypeKind, openTypeKind, UsageAnn(..) )
17 import IdInfo           ( ArityInfo, exactArity, CprInfo(..), InlinePragInfo(..) )
18 import Lex              
19
20 import RnMonad          ( ImportVersion, LocalVersion, ParsedIface(..), WhatsImported(..),
21                           RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..), WhetherHasOrphans
22                         ) 
23 import Bag              ( emptyBag, unitBag, snocBag )
24 import FiniteMap        ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
25 import RdrName          ( RdrName, mkRdrUnqual, mkSysQual, mkSysUnqual )
26 import Name             ( OccName, Provenance )
27 import OccName          ( mkSysOccFS,
28                           tcName, varName, ipName, dataName, clsName, tvName, uvName,
29                           EncodedFS 
30                         )
31 import Module           ( ModuleName, mkSysModuleFS )                   
32 import PrelMods         ( mkTupNameStr, mkUbxTupNameStr )
33 import PrelInfo         ( mkTupConRdrName, mkUbxTupConRdrName )
34 import SrcLoc           ( SrcLoc )
35 import Maybes
36 import Outputable
37
38 import GlaExts
39
40 #if __HASKELL1__ > 4
41 import Ratio ( (%) )
42 #endif
43 }
44
45 %name       parseIface
46 %tokentype  { Token }
47 %monad      { P }{ thenP }{ returnP }
48 %lexer      { lexer } { ITeof }
49
50 %token
51  'case'         { ITcase }                      -- Haskell keywords
52  'class'        { ITclass } 
53  'data'         { ITdata } 
54  'default'      { ITdefault }
55  'deriving'     { ITderiving }
56  'do'           { ITdo }
57  'else'         { ITelse }
58  'if'           { ITif }
59  'import'       { ITimport }
60  'in'           { ITin }
61  'infix'        { ITinfix }
62  'infixl'       { ITinfixl }
63  'infixr'       { ITinfixr }
64  'instance'     { ITinstance }
65  'let'          { ITlet }
66  'module'       { ITmodule }
67  'newtype'      { ITnewtype }
68  'of'           { ITof }
69  'then'         { ITthen }
70  'type'         { ITtype }
71  'where'        { ITwhere }
72  'as'           { ITas }
73  'qualified'    { ITqualified }
74  'hiding'       { IThiding }
75
76  'forall'       { ITforall }                    -- GHC extension keywords
77  'foreign'      { ITforeign }
78  'export'       { ITexport }
79  'label'        { ITlabel } 
80  'dynamic'      { ITdynamic }
81  'unsafe'       { ITunsafe }
82
83  '__interface'  { ITinterface }                 -- interface keywords
84  '__export'     { IT__export }
85  '__forall'     { IT__forall }
86  '__depends'    { ITdepends }
87  '__letrec'     { ITletrec }
88  '__coerce'     { ITcoerce }
89  '__inline_call'{ ITinlineCall }
90  '__inline_me'  { ITinlineMe }
91  '__DEFAULT'    { ITdefaultbranch }
92  '__bot'        { ITbottom }
93  '__integer'    { ITinteger_lit }
94  '__float'      { ITfloat_lit }
95  '__rational'   { ITrational_lit }
96  '__addr'       { ITaddr_lit }
97  '__litlit'     { ITlit_lit }
98  '__string'     { ITstring_lit }
99  '__ccall'      { ITccall $$ }
100  '__scc'        { ITscc }
101  '__sccC'       { ITsccAllCafs }
102
103  '__u'          { ITusage }
104  '__fuall'      { ITfuall }
105
106  '__A'          { ITarity }
107  '__P'          { ITspecialise }
108  '__C'          { ITnocaf }
109  '__U'          { ITunfold $$ }
110  '__S'          { ITstrict $$ }
111  '__R'          { ITrules }
112  '__M'          { ITcprinfo $$ }
113
114  '..'           { ITdotdot }                    -- reserved symbols
115  '::'           { ITdcolon }
116  '='            { ITequal }
117  '\\'           { ITlam }
118  '|'            { ITvbar }
119  '<-'           { ITlarrow }
120  '->'           { ITrarrow }
121  '@'            { ITat }
122  '~'            { ITtilde }
123  '=>'           { ITdarrow }
124  '-'            { ITminus }
125  '!'            { ITbang }
126
127  '/\\'          { ITbiglam }                    -- GHC-extension symbols
128
129  '{'            { ITocurly }                    -- special symbols
130  '}'            { ITccurly }
131  '['            { ITobrack }
132  ']'            { ITcbrack }
133  '('            { IToparen }
134  ')'            { ITcparen }
135  '(#'           { IToubxparen }
136  '#)'           { ITcubxparen }
137  ';'            { ITsemi }
138  ','            { ITcomma }
139
140  VARID          { ITvarid    $$ }               -- identifiers
141  CONID          { ITconid    $$ }
142  VARSYM         { ITvarsym   $$ }
143  CONSYM         { ITconsym   $$ }
144  QVARID         { ITqvarid   $$ }
145  QCONID         { ITqconid   $$ }
146  QVARSYM        { ITqvarsym  $$ }
147  QCONSYM        { ITqconsym  $$ }
148
149  IPVARID        { ITipvarid  $$ }               -- GHC extension
150
151  PRAGMA         { ITpragma   $$ }
152
153  CHAR           { ITchar     $$ }
154  STRING         { ITstring   $$ }
155  INTEGER        { ITinteger  $$ }
156  RATIONAL       { ITrational $$ }
157  CLITLIT        { ITlitlit   $$ }
158
159  UNKNOWN        { ITunknown  $$ }
160 %%
161
162 -- iface_stuff is the main production.
163 -- It recognises (a) a whole interface file
164 --               (b) a type (so that type sigs can be parsed lazily)
165 --               (c) the IdInfo part of a signature (same reason)
166
167 iface_stuff :: { IfaceStuff }
168 iface_stuff : iface             { let (nm, iff) = $1 in PIface nm iff }
169             | type              { PType   $1 }
170             | id_info           { PIdInfo $1 }
171             | '__R' rules       { PRules  $2 }
172
173
174 iface           :: { (ModuleName, ParsedIface) }
175 iface           : '__interface' mod_fs INTEGER orphans checkVersion 'where'
176                   exports_part
177                   import_part
178                   instance_decl_part
179                   decls_part
180                   rules_part
181                   { ( $2                        -- Module name
182                     , ParsedIface {
183                         pi_mod = fromInteger $3,        -- Module version
184                         pi_orphan  = $4,
185                         pi_exports = $7,        -- Exports
186                         pi_usages  = $8,        -- Usages
187                         pi_insts   = $9,        -- Local instances
188                         pi_decls   = $10,       -- Decls
189                         pi_rules   = $11        -- Rules 
190                       } ) }
191
192 --------------------------------------------------------------------------
193
194 import_part :: { [ImportVersion OccName] }
195 import_part :                                             { [] }
196             |  import_part import_decl                    { $2 : $1 }
197             
198 import_decl :: { ImportVersion OccName }
199 import_decl : 'import' mod_fs INTEGER orphans whats_imported ';'
200                         { (mkSysModuleFS $2, fromInteger $3, $4, $5) }
201         -- import Foo 3 :: a 1 b 3 c 7 ;        means import a,b,c from Foo
202         -- import Foo 3 ;                       means import all of Foo
203         -- import Foo 3 ! :: ...stuff... ;      the ! means that Foo contains orphans
204
205 orphans             :: { WhetherHasOrphans }
206 orphans             :                                           { False }
207                     | '!'                                       { True }
208
209 whats_imported      :: { WhatsImported OccName }
210 whats_imported      :                                           { Everything }
211                     | '::' name_version_pairs                   { Specifically $2 }
212
213 name_version_pairs  ::  { [LocalVersion OccName] }
214 name_version_pairs  :                                           { [] }
215                     |  name_version_pair name_version_pairs     { $1 : $2 }
216
217 name_version_pair   ::  { LocalVersion OccName }
218 name_version_pair   :  var_occ INTEGER                          { ($1, fromInteger $2) }
219                     |  tc_occ  INTEGER                          { ($1, fromInteger $2) }
220
221
222 --------------------------------------------------------------------------
223
224 exports_part    :: { [ExportItem] }
225 exports_part    :                                       { [] }
226                 | exports_part '__export' 
227                   mod_fs entities ';'                   { (mkSysModuleFS $3, $4) : $1 }
228
229 entities        :: { [RdrAvailInfo] }
230 entities        :                                       { [] }
231                 |  entity entities                      { $1 : $2 }
232
233 entity          :: { RdrAvailInfo }
234 entity          :  tc_occ                               { AvailTC $1 [$1] }
235                 |  var_occ                              { Avail $1 }
236                 |  tc_occ stuff_inside                  { AvailTC $1 ($1:$2) }
237                 |  tc_occ '|' stuff_inside              { AvailTC $1 $3 }
238
239 stuff_inside    :: { [OccName] }
240 stuff_inside    :  '{' val_occs '}'                     { $2 }
241
242 val_occ         :: { OccName }
243                 :  var_occ              { $1 }
244                 |  data_occ             { $1 }
245
246 val_occs        :: { [OccName] }
247                 :  val_occ              { [$1] }
248                 |  val_occ val_occs     { $1 : $2 }
249
250
251 --------------------------------------------------------------------------
252
253 fixity      :: { FixityDirection }
254 fixity      : 'infixl'                                  { InfixL }
255             | 'infixr'                                  { InfixR }
256             | 'infix'                                   { InfixN }
257    
258 mb_fix      :: { Int }
259 mb_fix      : {-nothing-}                               { 9 }
260             | INTEGER                                   { (fromInteger $1) }
261
262 -----------------------------------------------------------------------------
263
264 csigs           :: { [RdrNameSig] }
265 csigs           :                               { [] }
266                 | 'where' '{' csigs1 '}'        { $3 }
267
268 csigs1          :: { [RdrNameSig] }
269 csigs1          : csig                          { [$1] }
270                 | csig ';' csigs1               { $1 : $3 }
271
272 csig            :: { RdrNameSig }
273 csig            :  src_loc var_name '::' type           { mkClassOpSig False $2 $4 $1 }
274                 |  src_loc var_name '=' '::' type       { mkClassOpSig True  $2 $5 $1 }
275
276 --------------------------------------------------------------------------
277
278 instance_decl_part :: { [RdrNameInstDecl] }
279 instance_decl_part : {- empty -}                       { [] }
280                    | instance_decl_part inst_decl      { $2 : $1 }
281
282 inst_decl       :: { RdrNameInstDecl }
283 inst_decl       :  src_loc 'instance' type '=' var_name ';'
284                         { InstDecl $3
285                                    EmptyMonoBinds       {- No bindings -}
286                                    []                   {- No user pragmas -}
287                                    $5                   {- Dfun id -}
288                                    $1
289                         }
290
291 --------------------------------------------------------------------------
292
293 decls_part :: { [(Version, RdrNameHsDecl)] }
294 decls_part 
295         :  {- empty -}                          { [] }
296         |  decls_part version decl ';'          { ($2,$3):$1 }
297
298 decl    :: { RdrNameHsDecl }
299 decl    : src_loc var_name '::' type maybe_idinfo
300                          { SigD (IfaceSig $2 $4 ($5 $2) $1) }
301         | src_loc 'type' tc_name tv_bndrs '=' type                     
302                         { TyClD (TySynonym $3 $4 $6 $1) }
303         | src_loc 'data' decl_context tc_name tv_bndrs constrs         
304                         { TyClD (TyData DataType $3 $4 $5 $6 Nothing noDataPragmas $1) }
305         | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
306                         { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
307         | src_loc 'class' decl_context tc_name tv_bndrs fds csigs
308                         { TyClD (mkClassDecl $3 $4 $5 $6 $7 EmptyMonoBinds 
309                                         noClassPragmas $1) }
310         | src_loc fixity mb_fix var_or_data_name
311                         { FixD (FixitySig $4 (Fixity $3 $2) $1) }
312
313 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
314 maybe_idinfo  : {- empty -}     { \_ -> [] }
315               | src_loc PRAGMA  { \x -> 
316                                    case parseIface $2
317                                            PState{bol = 0#, atbol = 1#,
318                                                   context = [],
319                                                   glasgow_exts = 1#,
320                                                   loc = $1 } of
321                                      POk _ (PIdInfo id_info) -> id_info
322                                      PFailed err -> 
323                                         pprPanic "IdInfo parse failed" 
324                                             (vcat [ppr x, err])
325                                 }
326
327 -----------------------------------------------------------------------------
328
329 rules_part :: { [RdrNameRuleDecl] }
330 rules_part : {- empty -}        { [] }
331            | src_loc PRAGMA     { case parseIface $2 
332                                            PState{bol = 0#, atbol = 1#,
333                                                   context = [],
334                                                   glasgow_exts = 1#,
335                                                   loc = $1 }  of
336                                      POk _ (PRules rules) -> rules
337                                      PFailed err -> 
338                                           pprPanic "Rules parse failed" err
339                                 }
340
341 rules      :: { [RdrNameRuleDecl] }
342            : {- empty -}        { [] }
343            | rule ';' rules     { $1:$3 }
344
345 rule       :: { RdrNameRuleDecl }
346 rule       : src_loc STRING rule_forall qvar_name 
347              core_args '=' core_expr    { IfaceRuleDecl $4 (UfRuleBody $2 $3 $5 $7) $1 } 
348
349 rule_forall     :: { [UfBinder RdrName] }
350 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
351                   
352 -----------------------------------------------------------------------------
353
354 version         :: { Version }
355 version         :  INTEGER                              { fromInteger $1 }
356
357 decl_context    :: { RdrNameContext }
358 decl_context    :                                       { [] }
359                 | '{' context_list1 '}' '=>'    { $2 }
360
361 ----------------------------------------------------------------------------
362
363 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
364                 :                       { [] }
365                 | '=' constrs1          { $2 }
366
367 constrs1        :: { [RdrNameConDecl] }
368 constrs1        :  constr               { [$1] }
369                 |  constr '|' constrs1  { $1 : $3 }
370
371 constr          :: { RdrNameConDecl }
372 constr          :  src_loc ex_stuff data_name batypes           { mkConDecl $3 $2 (VanillaCon $4) $1 }
373                 |  src_loc ex_stuff data_name '{' fields1 '}'   { mkConDecl $3 $2 (RecCon $5)     $1 }
374                 -- We use "data_fs" so as to include ()
375
376 newtype_constr  :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
377 newtype_constr  :                                       { [] }
378                 | src_loc '=' ex_stuff data_name atype  { [mkConDecl $4 $3 (NewCon $5 Nothing) $1] }
379                 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
380                                                         { [mkConDecl $4 $3 (NewCon $8 (Just $6)) $1] }
381
382 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
383 ex_stuff        :                                       { ([],[]) }
384                 | '__forall' forall context '=>'            { ($2,$3) }
385
386 batypes         :: { [RdrNameBangType] }
387 batypes         :                                       { [] }
388                 |  batype batypes                       { $1 : $2 }
389
390 batype          :: { RdrNameBangType }
391 batype          :  atype                                { Unbanged $1 }
392                 |  '!' atype                            { Banged   $2 }
393                 |  '!' '!' atype                        { Unpacked $3 }
394
395 fields1         :: { [([RdrName], RdrNameBangType)] }
396 fields1         : field                                 { [$1] }
397                 | field ',' fields1                     { $1 : $3 }
398
399 field           :: { ([RdrName], RdrNameBangType) }
400 field           :  var_names1 '::' type         { ($1, Unbanged $3) }
401                 |  var_names1 '::' '!' type     { ($1, Banged   $4) }
402                 |  var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
403 --------------------------------------------------------------------------
404
405 type            :: { RdrNameHsType }
406 type            : '__fuall'  fuall '=>' type    { mkHsUsForAllTy $2 $4 }
407                 | '__forall' forall context '=>' type   
408                                                 { mkHsForAllTy (Just $2) $3 $5 }
409                 | btype '->' type               { MonoFunTy $1 $3 }
410                 | btype                         { $1 }
411
412 fuall           :: { [RdrName] }
413 fuall           : '[' uv_bndrs ']'                      { $2 }
414
415 forall          :: { [HsTyVar RdrName] }
416 forall          : '[' tv_bndrs ']'                      { $2 }
417
418 context         :: { RdrNameContext }
419 context         :                                       { [] }
420                 | '{' context_list1 '}'                 { $2 }
421
422 context_list1   :: { RdrNameContext }
423 context_list1   : class                                 { [$1] }
424                 | class ',' context_list1               { $1 : $3 }
425
426 class           :: { HsPred RdrName }
427 class           :  qcls_name atypes                     { (HsPClass $1 $2) }
428                 |  IPVARID '::' type                    { (HsPIParam (mkSysUnqual ipName $1) $3) }
429
430 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
431 types0          :  {- empty -}                          { [ ] }
432                 |  type                                 { [ $1 ] }
433                 |  types2                               { $1 }
434
435 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
436 types2          :  type ',' type                        { [$1,$3] }
437                 |  type ',' types2                      { $1 : $3 }
438
439 btype           :: { RdrNameHsType }
440 btype           :  atype                                { $1 }
441                 |  btype atype                          { MonoTyApp $1 $2 }
442                 |  '__u' usage atype                    { MonoUsgTy $2 $3 }
443
444 usage           :: { MonoUsageAnn RdrName }
445 usage           : '-'                                   { MonoUsOnce }
446                 | '!'                                   { MonoUsMany }
447                 | uv_name                               { MonoUsVar $1 }
448
449 atype           :: { RdrNameHsType }
450 atype           :  qtc_name                             { MonoTyVar $1 }
451                 |  tv_name                              { MonoTyVar $1 }
452                 |  '(' types2 ')'                       { MonoTupleTy $2 True{-boxed-} }
453                 |  '(#' types0 '#)'                     { MonoTupleTy $2 False{-unboxed-} }
454                 |  '[' type ']'                         { MonoListTy  $2 }
455                 |  '{' qcls_name atypes '}'             { MonoDictTy $2 $3 }
456                 |  '{' IPVARID '::' type '}'            { MonoIParamTy (mkSysUnqual ipName $2) $4 }
457                 |  '(' type ')'                         { $2 }
458
459 -- This one is dealt with via qtc_name
460 --              |  '(' ')'                              { MonoTupleTy [] True }
461
462 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
463 atypes          :                                       { [] }
464                 |  atype atypes                         { $1 : $2 }
465 ---------------------------------------------------------------------
466 mod_fs          :: { EncodedFS }
467                 :  CONID                { $1 }
468
469 mod_name        :: { ModuleName }
470                 :  mod_fs               { mkSysModuleFS $1 }
471
472
473 ---------------------------------------------------
474 var_fs          :: { EncodedFS }
475                 : VARID                 { $1 }
476                 | '!'                   { SLIT("!") }
477                 | 'as'                  { SLIT("as") }
478                 | 'qualified'           { SLIT("qualified") }
479                 | 'hiding'              { SLIT("hiding") }
480                 | 'forall'              { SLIT("forall") }
481                 | 'foreign'             { SLIT("foreign") }
482                 | 'export'              { SLIT("export") }
483                 | 'label'               { SLIT("label") }
484                 | 'dynamic'             { SLIT("dynamic") }
485                 | 'unsafe'              { SLIT("unsafe") }
486
487 qvar_fs         :: { (EncodedFS, EncodedFS) }
488                 :  QVARID               { $1 }
489                 |  QVARSYM              { $1 }
490
491 var_occ         :: { OccName }
492                 :  var_fs               { mkSysOccFS varName $1 }
493
494 var_name        :: { RdrName }
495 var_name        :  var_occ              { mkRdrUnqual $1 }
496
497 qvar_name       :: { RdrName }
498 qvar_name       :  var_name             { $1 }
499                 |  qvar_fs              { mkSysQual varName $1 }
500
501 var_names       :: { [RdrName] }
502 var_names       :                       { [] }
503                 | var_name var_names    { $1 : $2 }
504
505 var_names1      :: { [RdrName] }
506 var_names1      : var_name var_names    { $1 : $2 }
507
508 ---------------------------------------------------
509 -- For some bizarre reason, 
510 --      (,,,)      is dealt with by the parser
511 --      Foo.(,,,)  is dealt with by the lexer
512 -- Sigh
513
514 data_fs         :: { EncodedFS }
515                 :  CONID                { $1 }
516                 |  CONSYM               { $1 }
517
518 qdata_fs        :: { (EncodedFS, EncodedFS) }
519                 :  QCONID               { $1 }
520                 |  QCONSYM              { $1 }
521
522 data_occ        :: { OccName }
523                 :  data_fs              { mkSysOccFS dataName $1 }
524
525 data_name       :: { RdrName }
526                 :  data_occ             { mkRdrUnqual $1 }
527
528 qdata_name      :: { RdrName }
529 qdata_name      :  data_name            { $1 }
530                 |  qdata_fs             { mkSysQual dataName $1 }
531                                 
532 qdata_names     :: { [RdrName] }
533 qdata_names     :                               { [] }
534                 | qdata_name qdata_names        { $1 : $2 }
535
536 var_or_data_name :: { RdrName }
537                   : var_name                    { $1 }
538                   | data_name                   { $1 }
539
540 ---------------------------------------------------
541 tc_fs           :: { EncodedFS }
542                 :  data_fs              { $1 }
543
544 tc_occ          :: { OccName }
545                 :  tc_fs                { mkSysOccFS tcName $1 }
546
547 tc_name         :: { RdrName }
548                 :  tc_occ               { mkRdrUnqual $1 }
549
550 qtc_name        :: { RdrName }
551                 : tc_name               { $1 }
552                 | qdata_fs              { mkSysQual tcName $1 }
553
554 ---------------------------------------------------
555 cls_name        :: { RdrName }
556                 :  data_fs              { mkSysUnqual clsName $1 }
557
558 qcls_name       :: { RdrName }
559                 : cls_name              { $1 }
560                 | qdata_fs              { mkSysQual clsName $1 }
561
562 ---------------------------------------------------
563 uv_name         :: { RdrName }
564                 :  VARID                { mkSysUnqual uvName $1 }
565
566 uv_bndr         :: { RdrName }
567                 :  uv_name              { $1 }
568
569 uv_bndrs        :: { [RdrName] }
570                 :                       { [] }
571                 | uv_bndr uv_bndrs      { $1 : $2 }
572
573 ---------------------------------------------------
574 tv_name         :: { RdrName }
575                 :  VARID                { mkSysUnqual tvName $1 }
576                 |  VARSYM               { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
577
578 tv_bndr         :: { HsTyVar RdrName }
579                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
580                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
581
582 tv_bndrs        :: { [HsTyVar RdrName] }
583                 :                       { [] }
584                 | tv_bndr tv_bndrs      { $1 : $2 }
585
586 ---------------------------------------------------
587 fds :: { [([RdrName], [RdrName])] }
588         : {- empty -}                   { [] }
589         | '|' fds1                      { reverse $2 }
590
591 fds1 :: { [([RdrName], [RdrName])] }
592         : fds1 ',' fd                   { $3 : $1 }
593         | fd                            { [$1] }
594
595 fd :: { ([RdrName], [RdrName]) }
596         : varids0 '->' varids0          { (reverse $1, reverse $3) }
597
598 varids0 :: { [RdrName] }
599         : {- empty -}                   { [] }
600         | varids0 tv_name               { $2 : $1 }
601
602 ---------------------------------------------------
603 kind            :: { Kind }
604                 : akind                 { $1 }
605                 | akind '->' kind       { mkArrowKind $1 $3 }
606
607 akind           :: { Kind }
608                 : VARSYM                { if $1 == SLIT("*") then
609                                                 boxedTypeKind
610                                           else if $1 == SLIT("?") then
611                                                 openTypeKind
612                                           else panic "ParseInterface: akind"
613                                         }
614                 | '(' kind ')'  { $2 }
615
616 --------------------------------------------------------------------------
617
618 id_info         :: { [HsIdInfo RdrName] }
619                 :                               { [] }
620                 | id_info_item id_info          { $1 : $2 }
621
622 id_info_item    :: { HsIdInfo RdrName }
623                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
624                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
625                 | '__M'                         { HsCprInfo $1 }
626                 | '__S'                         { HsStrictness (HsStrictnessInfo $1) }
627                 | '__C'                         { HsNoCafRefs }
628                 | '__P' qvar_name               { HsWorker $2 }
629
630 inline_prag     :: { InlinePragInfo }
631                 :  {- empty -}                  { NoInlinePragInfo }
632                 | '[' INTEGER ']'               { IMustNotBeINLINEd True  (Just (fromInteger $2)) } -- INLINE n
633                 | '[' '!' INTEGER ']'           { IMustNotBeINLINEd False (Just (fromInteger $3)) } -- NOINLINE n
634
635 -------------------------------------------------------
636 core_expr       :: { UfExpr RdrName }
637 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
638                 | 'case' core_expr 'of' var_name
639                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
640
641                 | 'let' '{' core_val_bndr '=' core_expr
642                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
643                 | '__letrec' '{' rec_binds '}'          
644                   'in' core_expr                        { UfLet (UfRec $3) $6 }
645
646                 | con_or_primop '{' core_args '}'       { UfCon $1 $3 }
647                 | '__litlit' STRING atype               { UfCon (UfLitLitCon $2 $3) [] }
648
649                 | '__inline_me' core_expr               { UfNote UfInlineMe $2 }
650                 | '__inline_call' core_expr             { UfNote UfInlineCall $2 }
651                 | '__coerce' atype core_expr            { UfNote (UfCoerce $2) $3 }
652                 | scc core_expr                         { UfNote (UfSCC $1) $2  }
653                 | fexpr                                 { $1 }
654
655 fexpr   :: { UfExpr RdrName }
656 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
657         | core_aexpr                                    { $1 }
658
659 core_arg        :: { UfExpr RdrName }
660                 : '@' atype                                     { UfType $2 }
661                 | core_aexpr                                    { $1 }
662
663 core_args       :: { [UfExpr RdrName] }
664                 :                                               { [] }
665                 | core_arg core_args                            { $1 : $2 }
666
667 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
668 core_aexpr      : qvar_name                                     { UfVar $1 }
669
670                 | qdata_name                                    { UfVar $1 }
671                         -- This one means that e.g. "True" will parse as 
672                         -- (UfVar True_Id) rather than (UfCon True_Con []).
673                         -- No big deal; it'll be inlined in a jiffy.  I tried 
674                         -- parsing it to (Con con []) directly, but got bitten 
675                         -- when a real constructor Id showed up in an interface
676                         -- file.  As usual, a hack bites you in the end.
677                         -- If you want to get a UfCon, then use the
678                         -- curly-bracket notation (True {}).
679
680                 | core_lit               { UfCon (UfLitCon $1) [] }
681                 | '(' core_expr ')'      { $2 }
682                 | '(' comma_exprs2 ')'   { UfTuple (mkTupConRdrName (length $2)) $2 }
683                 | '(#' comma_exprs0 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
684
685 -- This one is dealt with by qdata_name: see above comments
686 --              | '('  ')'               { UfTuple (mkTupConRdrName 0) [] }
687
688 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
689 comma_exprs0    : {- empty -}                   { [ ] }
690                 | core_expr                     { [ $1 ] }
691                 | comma_exprs2                  { $1 }
692
693 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
694 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
695                 | core_expr ',' comma_exprs2                    { $1 : $3 }
696
697 con_or_primop   :: { UfCon RdrName }
698 con_or_primop   : qdata_name                    { UfDataCon $1 }
699                 | qvar_name                     { UfPrimOp $1 }
700                 | '__ccall' ccall_string      { let
701                                                 (is_dyn, is_casm, may_gc) = $1
702                                                 in
703                                                 UfCCallOp $2 is_dyn is_casm may_gc
704                                                 }
705
706 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
707                 :                                               { [] }
708                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
709
710 core_alts       :: { [UfAlt RdrName] }
711                 : core_alt                                      { [$1] }
712                 | core_alt ';' core_alts                        { $1 : $3 }
713
714 core_alt        :: { UfAlt RdrName }
715 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
716
717 core_pat        :: { (UfCon RdrName, [RdrName]) }
718 core_pat        : core_lit                      { (UfLitCon  $1, []) }
719                 | '__litlit' STRING atype       { (UfLitLitCon $2 $3, []) }
720                 | qdata_name core_pat_names     { (UfDataCon $1, $2) }
721                 | '(' comma_var_names1 ')'      { (UfDataCon (mkTupConRdrName (length $2)), $2) }
722                 | '(#' comma_var_names1 '#)'    { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
723                 | '__DEFAULT'                   { (UfDefault, []) }
724                 | '(' core_pat ')'              { $2 }
725
726 core_pat_names :: { [RdrName] }
727 core_pat_names :                                { [] }
728                 | core_pat_name core_pat_names  { $1 : $2 }
729
730 -- Tyvar names and variable names live in different name spaces
731 -- so they need to be signalled separately.  But we don't record 
732 -- types or kinds in a pattern; we work that out from the type 
733 -- of the case scrutinee
734 core_pat_name   :: { RdrName }
735 core_pat_name   : var_name                      { $1 }
736                 | '@' tv_name                   { $2 }
737         
738 comma_var_names1 :: { [RdrName] }       -- One or more
739 comma_var_names1 : var_name                                     { [$1] }
740                  | var_name ',' comma_var_names1                { $1 : $3 }
741
742 core_lit        :: { Literal }
743 core_lit        : integer                       { mkMachInt_safe $1 }
744                 | CHAR                          { MachChar $1 }
745                 | STRING                        { MachStr $1 }
746                 | '__string' STRING             { NoRepStr $2 (panic "NoRepStr type") }
747                 | rational                      { MachDouble $1 }
748                 | '__float' rational            { MachFloat $2 }
749
750                 | '__integer' integer           { NoRepInteger  $2 (panic "NoRepInteger type") 
751                                                         -- The type checker will add the types
752                                                 }
753
754                 | '__rational' integer integer  { NoRepRational ($2 % $3) 
755                                                    (panic "NoRepRational type")
756                                                         -- The type checker will add the type
757                                                 }
758
759                 | '__addr' integer              { MachAddr $2 }
760
761 integer         :: { Integer }
762                 : INTEGER                       { $1 }
763                 | '-' INTEGER                   { (-$2) }
764
765 rational        :: { Rational }
766                 : RATIONAL                      { $1 }
767                 | '-' RATIONAL                  { (-$2) }
768
769 core_bndr       :: { UfBinder RdrName }
770 core_bndr       : core_val_bndr                                 { $1 }
771                 | core_tv_bndr                                  { $1 }
772
773 core_bndrs      :: { [UfBinder RdrName] }
774 core_bndrs      :                                               { [] }
775                 | core_bndr core_bndrs                          { $1 : $2 }
776
777 core_val_bndr   :: { UfBinder RdrName }
778 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
779
780 core_tv_bndr    :: { UfBinder RdrName }
781 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
782                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
783
784 ccall_string    :: { FAST_STRING }
785                 : STRING                                        { $1 }
786                 | CLITLIT                                       { $1 }
787                 | VARID                                         { $1 }
788                 | CONID                                         { $1 }
789
790 ------------------------------------------------------------------------
791 scc     :: { CostCentre }
792         :  '__sccC' '{' mod_name STRING '}'                      { AllCafsCC $3 $4 }
793         |  '__scc' '{' cc_name mod_name STRING cc_dup cc_caf '}'
794                              { NormalCC { cc_name = $3, cc_mod = $4, cc_grp = $5,
795                                           cc_is_dupd = $6, cc_is_caf = $7 } }
796
797 cc_name :: { EncodedFS }
798         : CONID                 { $1 }
799         | var_fs                { $1 }
800   
801 cc_dup  :: { IsDupdCC }
802 cc_dup  :                       { OriginalCC }
803         | '!'                   { DupdCC }
804
805 cc_caf  :: { IsCafCC }
806         :                       { NotCafCC }
807         | '__C'                 { CafCC }
808
809 -------------------------------------------------------------------
810
811 src_loc :: { SrcLoc }
812 src_loc :                               {% getSrcLocP }
813
814 checkVersion :: { () }
815            : {-empty-}                  {% checkVersion Nothing }
816            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
817
818 ------------------------------------------------------------------- 
819
820 --                      Haskell code 
821 {
822 happyError :: P a
823 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
824
825 data IfaceStuff = PIface        EncodedFS{-.hi module name-} ParsedIface
826                 | PIdInfo       [HsIdInfo RdrName]
827                 | PType         RdrNameHsType
828                 | PRules        [RdrNameRuleDecl]
829
830 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc
831 }