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