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