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