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