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