[project @ 2001-11-19 16:34:12 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
1 {-      Notes about the syntax of interface files
2         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 The header
4 ~~~~~~~~~~
5   interface "edison" M 4 6 2 ! 406      Module M, version 4, from package 'edison',
6                                         Fixities version 6, rules version 2
7                                         Interface syntax version 406
8                                         ! means M contains orphans
9
10 Import declarations
11 ~~~~~~~~~~~~~~~~~~~
12   import Foo ;                          To compile M I used nothing from Foo, but it's 
13                                         below me in the hierarchy
14
15   import Foo ! @ ;                      Ditto, but the ! means that Foo contains orphans
16                                         and        the @ means that Foo is a boot interface
17
18   import Foo :: 3 ;                     To compile M I used everything from Foo, which has 
19                                         module version 3
20
21   import Foo :: 3 2 6 a 1 b 3 c 7 ;     To compile M I used Foo.  It had 
22                                                 module version 3
23                                                 fixity version 2
24                                                 rules  version 6
25                                         and some specific things besides.
26
27 -}
28
29
30 {
31 module ParseIface ( parseIface, parseType, parseRules, parseIdInfo ) where
32
33 #include "HsVersions.h"
34
35 import HsSyn            -- quite a bit of stuff
36 import RdrHsSyn         -- oodles of synonyms
37 import HsTypes          ( mkHsForAllTy, mkHsTupCon )
38 import HsCore
39 import Literal          ( Literal(..), mkMachInt, mkMachInt64, mkMachWord, mkMachWord64 )
40 import BasicTypes       ( Fixity(..), FixityDirection(..), StrictnessMark(..),
41                           NewOrData(..), Version, initialVersion, Boxity(..),
42                           Activation(..)
43                         )
44 import CostCentre       ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
45 import Type             ( Kind, mkArrowKind, liftedTypeKind, openTypeKind, usageTypeKind )
46 import ForeignCall      ( ForeignCall(..), CCallConv(..), CCallSpec(..), CCallTarget(..) )
47 import Lex              
48
49 import RnMonad          ( ParsedIface(..), ExportItem, IfaceDeprecs ) 
50 import HscTypes         ( WhetherHasOrphans, IsBootInterface, GenAvailInfo(..), 
51                           ImportVersion, WhatsImported(..),
52                           RdrAvailInfo )
53
54 import RdrName          ( RdrName, mkRdrUnqual, mkIfaceOrig )
55 import Name             ( OccName )
56 import OccName          ( mkSysOccFS,
57                           tcName, varName, dataName, clsName, tvName,
58                           EncodedFS 
59                         )
60 import Module           ( ModuleName, PackageName, mkSysModuleNameFS, mkModule )
61 import SrcLoc           ( SrcLoc )
62 import CmdLineOpts      ( opt_InPackage, opt_IgnoreIfacePragmas )
63 import Outputable
64 import Class            ( DefMeth (..) )
65
66 import GlaExts
67 import FastString       ( tailFS )
68 }
69
70 %name       parseIface      iface
71 %name       parseType       type
72 %name       parseIdInfo     id_info
73 %name       parseRules      rules_and_deprecs
74
75 %tokentype  { Token }
76 %monad      { P }{ thenP }{ returnP }
77 %lexer      { lexer } { ITeof }
78
79 %token
80  'as'           { ITas }
81  'case'         { ITcase }                      -- Haskell keywords
82  'class'        { ITclass } 
83  'data'         { ITdata } 
84  'default'      { ITdefault }
85  'deriving'     { ITderiving }
86  'do'           { ITdo }
87  'else'         { ITelse }
88  'hiding'       { IThiding }
89  'if'           { ITif }
90  'import'       { ITimport }
91  'in'           { ITin }
92  'infix'        { ITinfix }
93  'infixl'       { ITinfixl }
94  'infixr'       { ITinfixr }
95  'instance'     { ITinstance }
96  'let'          { ITlet }
97  'module'       { ITmodule }
98  'newtype'      { ITnewtype }
99  'of'           { ITof }
100  'qualified'    { ITqualified }
101  'then'         { ITthen }
102  'type'         { ITtype }
103  'where'        { ITwhere }
104
105  'forall'       { ITforall }                    -- GHC extension keywords
106  'foreign'      { ITforeign }
107  'export'       { ITexport }
108  'label'        { ITlabel } 
109  'dynamic'      { ITdynamic }
110  'unsafe'       { ITunsafe }
111  'with'         { ITwith }
112  'stdcall'      { ITstdcallconv }
113  'ccall'        { ITccallconv }
114
115  '__interface'  { ITinterface }                 -- interface keywords
116  '__export'     { IT__export }
117  '__depends'    { ITdepends }
118  '__forall'     { IT__forall }
119  '__letrec'     { ITletrec }
120  '__coerce'     { ITcoerce }
121  '__inline_me'  { ITinlineMe }
122  '__inline_call'{ ITinlineCall }
123  '__DEFAULT'    { ITdefaultbranch }
124  '__bot'        { ITbottom }
125  '__integer'    { ITinteger_lit }
126  '__float'      { ITfloat_lit }
127  '__word'       { ITword_lit }
128  '__int64'      { ITint64_lit }
129  '__word64'     { ITword64_lit }
130  '__rational'   { ITrational_lit }
131  '__addr'       { ITaddr_lit }
132  '__label'      { ITlabel_lit }
133  '__litlit'     { ITlit_lit }
134  '__string'     { ITstring_lit }
135  '__ccall'      { ITccall $$ }
136  '__scc'        { ITscc }
137  '__sccC'       { ITsccAllCafs }
138
139  '__u'          { ITusage }
140
141  '__A'          { ITarity }
142  '__P'          { ITspecialise }
143  '__C'          { ITnocaf }
144  '__U'          { ITunfold }
145  '__S'          { ITstrict $$ }
146  '__R'          { ITrules }
147  '__M'          { ITcprinfo }
148  '__D'          { ITdeprecated }
149
150  '..'           { ITdotdot }                    -- reserved symbols
151  '::'           { ITdcolon }
152  '='            { ITequal }
153  '\\'           { ITlam }
154  '|'            { ITvbar }
155  '<-'           { ITlarrow }
156  '->'           { ITrarrow }
157  '@'            { ITat }
158  '=>'           { ITdarrow }
159  '-'            { ITminus }
160  '!'            { ITbang }
161
162  '{'            { ITocurly }                    -- special symbols
163  '}'            { ITccurly }
164  '{|'           { ITocurlybar }                         -- special symbols
165  '|}'           { ITccurlybar }                         -- special symbols
166  '['            { ITobrack }
167  ']'            { ITcbrack }
168  '('            { IToparen }
169  ')'            { ITcparen }
170  '(#'           { IToubxparen }
171  '#)'           { ITcubxparen }
172  ';'            { ITsemi }
173  ','            { ITcomma }
174  '.'            { ITdot }
175
176  VARID          { ITvarid    $$ }               -- identifiers
177  CONID          { ITconid    $$ }
178  VARSYM         { ITvarsym   $$ }
179  CONSYM         { ITconsym   $$ }
180  QVARID         { ITqvarid   $$ }
181  QCONID         { ITqconid   $$ }
182  QVARSYM        { ITqvarsym  $$ }
183  QCONSYM        { ITqconsym  $$ }
184
185  IPVARID        { ITipvarid  $$ }               -- GHC extension
186
187  PRAGMA         { ITpragma   $$ }
188
189  CHAR           { ITchar     $$ }
190  STRING         { ITstring   $$ }
191  INTEGER        { ITinteger  $$ }
192  RATIONAL       { ITrational $$ }
193  CLITLIT        { ITlitlit   $$ }
194
195  UNKNOWN        { ITunknown  $$ }
196 %%
197
198 iface           :: { ParsedIface }
199 iface           : '__interface' package mod_name 
200                         version sub_versions
201                         orphans checkVersion 'where'
202                   exports_part
203                   import_part
204                   fix_decl_part
205                   instance_decl_part
206                   decls_part
207                   rules_and_deprecs_part
208                   { ParsedIface {
209                         pi_mod  = mkModule $3 $2,       -- Module itself
210                         pi_vers = $4,                   -- Module version
211                         pi_orphan  = $6,
212                         pi_exports = (fst $5, $9),      -- Exports
213                         pi_usages  = $10,               -- Usages
214                         pi_fixity  = $11,               -- Fixies
215                         pi_insts   = $12,               -- Local instances
216                         pi_decls   = $13,               -- Decls
217                         pi_rules   = (snd $5,fst $14),  -- Rules 
218                         pi_deprecs = snd $14            -- Deprecations 
219                    } }
220
221 -- Versions for exports and rules (optional)
222 sub_versions :: { (Version,Version) }
223         : '[' version version ']'               { ($2,$3) }
224         | {- empty -}                           { (initialVersion, initialVersion) }
225
226 --------------------------------------------------------------------------
227
228 import_part :: { [ImportVersion OccName] }
229 import_part :                                             { [] }
230             |  import_decl import_part                    { $1 : $2 }
231             
232 import_decl :: { ImportVersion OccName }
233 import_decl : 'import' mod_name orphans is_boot whats_imported ';'
234                         { ({-mkSysModuleNameFS-} $2, $3, $4, $5) }
235
236 orphans             :: { WhetherHasOrphans }
237 orphans             :                                           { False }
238                     | '!'                                       { True }
239
240 is_boot             :: { IsBootInterface }
241 is_boot             :                                           { False }
242                     | '@'                                       { True }
243
244 whats_imported      :: { WhatsImported OccName }
245 whats_imported      :                                                   { NothingAtAll }
246                     | '::' version                                      { Everything $2 }
247                     | '::' version version version name_version_pairs   { Specifically $2 (Just $3) $5 $4 }
248                     | '::' version version name_version_pairs           { Specifically $2 Nothing $4 $3 }
249
250 name_version_pairs  ::  { [(OccName, Version)] }
251 name_version_pairs  :                                           { [] }
252                     |  name_version_pair name_version_pairs     { $1 : $2 }
253
254 name_version_pair   ::  { (OccName, Version) }
255 name_version_pair   :  var_occ version                          { ($1, $2) }
256                     |  tc_occ  version                          { ($1, $2) }
257
258
259 --------------------------------------------------------------------------
260
261 exports_part    :: { [ExportItem] }
262 exports_part    :                                       { [] }
263                 | '__export' mod_name entities ';'
264                         exports_part                    { ({-mkSysModuleNameFS-} $2, $3) : $5 }
265
266 entities        :: { [RdrAvailInfo] }
267 entities        :                                       { [] }
268                 |  entity entities                      { $1 : $2 }
269
270 entity          :: { RdrAvailInfo }
271 entity          :  var_occ                              { Avail $1 }
272                 |  tc_occ                               { AvailTC $1 [$1] }
273                 |  tc_occ '|' stuff_inside              { AvailTC $1 $3 }
274                 |  tc_occ stuff_inside                  { AvailTC $1 ($1:$2) }
275                 -- Note that the "main name" comes at the beginning
276
277 stuff_inside    :: { [OccName] }
278 stuff_inside    :  '{' val_occs '}'                     { $2 }
279
280 val_occ         :: { OccName }
281                 :  var_occ              { $1 }
282                 |  data_occ             { $1 }
283
284 val_occs        :: { [OccName] }
285                 :  val_occ              { [$1] }
286                 |  val_occ val_occs     { $1 : $2 }
287
288
289 --------------------------------------------------------------------------
290
291 fix_decl_part :: { [RdrNameFixitySig] }
292 fix_decl_part : {- empty -}                             { [] }
293               | fix_decls ';'                           { $1 }
294
295 fix_decls     :: { [RdrNameFixitySig] }
296 fix_decls     :                                         { [] }
297               | fix_decl fix_decls                      { $1 : $2 }
298
299 fix_decl :: { RdrNameFixitySig }
300 fix_decl : src_loc fixity prec var_or_data_name         { FixitySig $4 (Fixity $3 $2) $1 }
301
302 fixity      :: { FixityDirection }
303 fixity      : 'infixl'                                  { InfixL }
304             | 'infixr'                                  { InfixR }
305             | 'infix'                                   { InfixN }
306    
307 prec        :: { Int }
308 prec        : INTEGER                                   { fromInteger $1 }
309
310 -----------------------------------------------------------------------------
311
312 csigs           :: { [RdrNameSig] }
313 csigs           :                               { [] }
314                 | 'where' '{' csigs1 '}'        { $3 }
315
316 csigs1          :: { [RdrNameSig] }
317 csigs1          :                               { [] }
318                 | csig ';' csigs1               { $1 : $3 }
319
320 csig            :: { RdrNameSig }
321 csig            :  src_loc qvar_name '::' type          { ClassOpSig $2 NoDefMeth $4 $1 }
322                 |  src_loc qvar_name ';' '::' type      { ClassOpSig $2 GenDefMeth $5 $1 }              
323                 |  src_loc qvar_name '=' '::' type      { mkClassOpSigDM $2 $5 $1 }
324
325 --------------------------------------------------------------------------
326
327 instance_decl_part :: { [RdrNameInstDecl] }
328 instance_decl_part : {- empty -}                       { [] }
329                    | instance_decl_part inst_decl      { $2 : $1 }
330
331 inst_decl       :: { RdrNameInstDecl }
332 inst_decl       :  src_loc 'instance' type '=' qvar_name ';'
333                         { InstDecl $3
334                                    EmptyMonoBinds       {- No bindings -}
335                                    []                   {- No user pragmas -}
336                                    (Just $5)            {- Dfun id -}
337                                    $1
338                         }
339
340 --------------------------------------------------------------------------
341
342 decls_part :: { [(Version, RdrNameTyClDecl)] }
343 decls_part 
344         :  {- empty -}                          { [] }
345         |  opt_version decl ';' decls_part              { ($1,$2):$4 }
346
347 decl    :: { RdrNameTyClDecl }
348 decl    : src_loc qvar_name '::' type maybe_idinfo
349                         { IfaceSig $2 $4 ($5 $2) $1 }
350         | src_loc 'type' qtc_name tv_bndrs '=' type                    
351                         { TySynonym $3 $4 $6 $1 }
352         | src_loc 'foreign' 'type' qtc_name                    
353                         { ForeignType $4 Nothing DNType $1 }
354         | src_loc 'data' opt_decl_context qtc_name tv_bndrs constrs            
355                         { mkTyData DataType $3 $4 $5 $6 (length $6) Nothing $1 }
356         | src_loc 'newtype' opt_decl_context qtc_name tv_bndrs newtype_constr
357                         { mkTyData NewType $3 $4 $5 $6 1 Nothing $1 }
358         | src_loc 'class' opt_decl_context qtc_name tv_bndrs fds csigs
359                         { mkClassDecl $3 $4 $5 $6 $7 Nothing $1 }
360
361 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
362 maybe_idinfo  : {- empty -}     { \_ -> [] }
363               | pragma          { \x -> if opt_IgnoreIfacePragmas then [] 
364                                         else case $1 of
365                                                 POk _ id_info -> id_info
366                                                 PFailed err -> pprPanic "IdInfo parse failed" 
367                                                                         (vcat [ppr x, err])
368                                 }
369     {-
370       If a signature decl is being loaded, and opt_IgnoreIfacePragmas is on,
371       we toss away unfolding information.
372
373       Also, if the signature is loaded from a module we're importing from source,
374       we do the same. This is to avoid situations when compiling a pair of mutually
375       recursive modules, peering at unfolding info in the interface file of the other, 
376       e.g., you compile A, it looks at B's interface file and may as a result change
377       its interface file. Hence, B is recompiled, maybe changing its interface file,
378       which will the unfolding info used in A to become invalid. Simple way out is to
379       just ignore unfolding info.
380
381       [Jan 99: I junked the second test above.  If we're importing from an hi-boot
382        file there isn't going to *be* any pragma info.  The above comment
383        dates from a time where we picked up a .hi file first if it existed.]
384     -}
385
386 pragma  :: { ParseResult [HsIdInfo RdrName] }
387 pragma  : src_loc PRAGMA        { parseIdInfo $2 PState{ bol = 0#, atbol = 1#,
388                                                         context = [],
389                                                         glasgow_exts = 1#,
390                                                         loc = $1 }
391                                 }
392
393 -----------------------------------------------------------------------------
394
395 rules_and_deprecs_part :: { ([RdrNameRuleDecl], IfaceDeprecs) }
396 rules_and_deprecs_part : {- empty -}    { ([], Nothing) }
397                        | rules_prag     { case $1 of
398                                              POk _ rds -> rds
399                                              PFailed err -> pprPanic "Rules/Deprecations parse failed" err
400                                         }
401
402 rules_prag :: { ParseResult ([RdrNameRuleDecl], IfaceDeprecs) }
403 rules_prag : src_loc PRAGMA     { parseRules $2 PState{ bol = 0#, atbol = 1#,
404                                                         context = [],
405                                                         glasgow_exts = 1#,
406                                                         loc = $1 }
407                                 }
408
409 rules_and_deprecs :: { ([RdrNameRuleDecl], IfaceDeprecs) }
410 rules_and_deprecs : rule_prag deprec_prag       { ($1, $2) }
411
412  
413 -----------------------------------------------------------------------------
414
415 rule_prag :: { [RdrNameRuleDecl] }
416 rule_prag : {- empty -}                 { [] }
417           | '__R' rules                 { $2 }
418
419 rules      :: { [RdrNameRuleDecl] }
420            : {- empty -}        { [] }
421            | rule ';' rules     { $1:$3 }
422
423 rule       :: { RdrNameRuleDecl }
424 rule       : src_loc STRING activation rule_forall qvar_name 
425              core_args '=' core_expr    { IfaceRule $2 $3 $4 $5 $6 $8 $1 } 
426
427 activation :: { Activation }
428 activation : {- empty -}                { AlwaysActive }
429            | '[' INTEGER ']'            { ActiveAfter (fromInteger $2) }
430
431 rule_forall     :: { [UfBinder RdrName] }
432 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
433                   
434 -----------------------------------------------------------------------------
435
436 deprec_prag     :: { IfaceDeprecs }
437 deprec_prag     : {- empty -}           { Nothing }
438                 | '__D' deprecs         { Just $2 } 
439
440 deprecs         :: { Either DeprecTxt [(RdrName,DeprecTxt)] }
441 deprecs         : STRING                { Left $1 }
442                 | deprec_list           { Right $1 }
443
444 deprec_list     :: { [(RdrName,DeprecTxt)] }
445 deprec_list     : deprec                        { [$1] }
446                 | deprec ';' deprec_list        { $1 : $3 }
447
448 deprec          :: { (RdrName,DeprecTxt) }
449 deprec          : deprec_name STRING    { ($1, $2) }
450
451 deprec_name     :: { RdrName }
452                 : qvar_name             { $1 }
453                 | qtc_name              { $1 }
454
455 -----------------------------------------------------------------------------
456
457 version         :: { Version }
458 version         :  INTEGER                      { fromInteger $1 }
459
460 opt_version     :: { Version }
461 opt_version     : version                       { $1 }
462                 | {- empty -}                   { initialVersion }
463         
464 opt_decl_context  :: { RdrNameContext }
465 opt_decl_context  :                             { [] }
466                   | context '=>'                { $1 }
467
468 ----------------------------------------------------------------------------
469
470 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
471                 :                       { [] }
472                 | '=' constrs1          { $2 }
473
474 constrs1        :: { [RdrNameConDecl] }
475 constrs1        :  constr               { [$1] }
476                 |  constr '|' constrs1  { $1 : $3 }
477
478 constr          :: { RdrNameConDecl }
479 constr          :  src_loc ex_stuff qdata_name batypes          { mk_con_decl $3 $2 (VanillaCon $4) $1 }
480                 |  src_loc ex_stuff qdata_name '{' fields1 '}'  { mk_con_decl $3 $2 (RecCon $5)     $1 }
481                 -- We use "data_fs" so as to include ()
482
483 newtype_constr  :: { [RdrNameConDecl] {- Not allowed to be empty -} }
484 newtype_constr  : src_loc '=' ex_stuff qdata_name atype { [mk_con_decl $4 $3 (VanillaCon [unbangedType $5]) $1] }
485                 | src_loc '=' ex_stuff qdata_name '{' qvar_name '::' atype '}'
486                                                         { [mk_con_decl $4 $3 (RecCon [([$6], unbangedType $8)]) $1] }
487
488 ex_stuff :: { ([HsTyVarBndr RdrName], RdrNameContext) }
489 ex_stuff        :                                       { ([],[]) }
490                 | '__forall' tv_bndrs opt_context '=>'  { ($2,$3) }
491
492 batypes         :: { [RdrNameBangType] }
493 batypes         :                                       { [] }
494                 |  batype batypes                       { $1 : $2 }
495
496 batype          :: { RdrNameBangType }
497 batype          :  tatype                               { unbangedType $1 }
498                 |  '!' tatype                           { BangType MarkedStrict    $2 }
499                 |  '!' '!' tatype                       { BangType MarkedUnboxed   $3 }
500
501 fields1         :: { [([RdrName], RdrNameBangType)] }
502 fields1         : field                                 { [$1] }
503                 | field ',' fields1                     { $1 : $3 }
504
505 field           :: { ([RdrName], RdrNameBangType) }
506 field           :  qvar_names1 '::' ttype               { ($1, unbangedType $3) }
507                 |  qvar_names1 '::' '!' ttype           { ($1, BangType MarkedStrict    $4) }
508                 |  qvar_names1 '::' '!' '!' ttype       { ($1, BangType MarkedUnboxed   $5) }
509
510 --------------------------------------------------------------------------
511
512 type            :: { RdrNameHsType }
513 type            : '__forall' tv_bndrs 
514                         opt_context '=>' type   { mkHsForAllTy (Just $2) $3 $5 }
515                 | btype '->' type               { HsFunTy $1 $3 }
516                 | btype                         { $1 }
517
518 opt_context     :: { RdrNameContext }
519 opt_context     :                                       { [] }
520                 | context                               { $1 }
521
522 context         :: { RdrNameContext }
523 context         : '(' context_list1 ')'                 { $2 }
524                 | '{' context_list1 '}'                 { $2 }  -- Backward compatibility
525
526 context_list1   :: { RdrNameContext }
527 context_list1   : class                                 { [$1] }
528                 | class ',' context_list1               { $1 : $3 }
529
530 class           :: { HsPred RdrName }
531 class           :  qcls_name atypes                     { (HsClassP $1 $2) }
532                 |  ipvar_name '::' type                 { (HsIParam $1 $3) }
533
534 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
535 types0          :  {- empty -}                          { [ ] }
536                 |  type                                 { [ $1 ] }
537                 |  types2                               { $1 }
538
539 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
540 types2          :  type ',' type                        { [$1,$3] }
541                 |  type ',' types2                      { $1 : $3 }
542
543 btype           :: { RdrNameHsType }
544 btype           :  atype                                { $1 }
545                 |  btype atype                          { HsAppTy $1 $2 }
546                 |  '__u' atype atype                    { HsUsageTy $2 $3 }
547
548 atype           :: { RdrNameHsType }
549 atype           :  qtc_name                             { HsTyVar $1 }
550                 |  tv_name                              { HsTyVar $1 }
551                 |  '.'                                  { hsUsOnce }
552                 |  '!'                                  { hsUsMany }
553                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
554                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
555                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
556                 |  '[' type ']'                         { HsListTy  $2 }
557                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
558                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
559                 |  '(' type ')'                         { $2 }
560
561 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
562 atypes          :                                       { [] }
563                 |  atype atypes                         { $1 : $2 }
564 --------------------------------------------------------------------------
565
566 -- versions of type/btype/atype that cant begin with '!' (or '.')
567 -- for use where the kind is definitely known NOT to be '$'
568
569 ttype           :: { RdrNameHsType }
570 ttype           : '__forall' tv_bndrs 
571                         opt_context '=>' type           { mkHsForAllTy (Just $2) $3 $5 }
572                 | tbtype '->' type                      { HsFunTy $1 $3 }
573                 | tbtype                                { $1 }
574
575 tbtype          :: { RdrNameHsType }
576 tbtype          :  tatype                               { $1 }
577                 |  tbtype atype                         { HsAppTy $1 $2 }
578                 |  '__u' atype atype                    { HsUsageTy $2 $3 }
579
580 tatype          :: { RdrNameHsType }
581 tatype          :  qtc_name                             { HsTyVar $1 }
582                 |  tv_name                              { HsTyVar $1 }
583                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
584                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
585                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
586                 |  '[' type ']'                         { HsListTy  $2 }
587                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
588                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
589                 |  '(' type ')'                         { $2 }
590 ---------------------------------------------------------------------
591
592 package         :: { PackageName }
593                 :  STRING               { $1 }
594                 | {- empty -}           { opt_InPackage }
595                                 -- Useful for .hi-boot files,
596                                 -- which can omit the package Id
597                                 -- Module loops are always within a package
598
599 mod_name        :: { ModuleName }
600                 :  CONID                { mkSysModuleNameFS $1 }
601
602
603 ---------------------------------------------------
604 var_fs          :: { EncodedFS }
605                 : VARID                 { $1 }
606                 | 'as'                  { SLIT("as") }
607                 | 'qualified'           { SLIT("qualified") }
608                 | 'hiding'              { SLIT("hiding") }
609                 | 'forall'              { SLIT("forall") }
610                 | 'foreign'             { SLIT("foreign") }
611                 | 'export'              { SLIT("export") }
612                 | 'label'               { SLIT("label") }
613                 | 'dynamic'             { SLIT("dynamic") }
614                 | 'unsafe'              { SLIT("unsafe") }
615                 | 'with'                { SLIT("with") }
616                 | 'ccall'               { SLIT("ccall") }
617                 | 'stdcall'             { SLIT("stdcall") }
618
619 var_occ         :: { OccName }
620                 :  var_fs               { mkSysOccFS varName $1 }
621
622 var_name        :: { RdrName }
623 var_name        :  var_occ              { mkRdrUnqual $1 }
624
625 qvar_name       :: { RdrName }
626 qvar_name       :  var_name             { $1 }
627                 |  QVARID               { mkIfaceOrig varName $1 }
628
629 ipvar_name      :: { RdrName }
630                 :  IPVARID              { mkRdrUnqual (mkSysOccFS varName (tailFS $1)) }
631
632 qvar_names1     :: { [RdrName] }
633 qvar_names1     : qvar_name             { [$1] }
634                 | qvar_name qvar_names1 { $1 : $2 }
635
636 var_names       :: { [RdrName] }
637 var_names       :                       { [] }
638                 | var_name var_names    { $1 : $2 }
639
640 var_names1      :: { [RdrName] }
641 var_names1      : var_name var_names    { $1 : $2 }
642
643 ---------------------------------------------------
644
645 data_occ        :: { OccName }
646                 :  CONID                { mkSysOccFS dataName $1 }
647
648 qdata_name      :: { RdrName }
649                 :  data_occ             { mkRdrUnqual $1 }
650                 |  QCONID               { mkIfaceOrig dataName $1 }
651                                 
652 var_or_data_name :: { RdrName }
653                   : qvar_name           { $1 }
654                   | qdata_name          { $1 }
655
656 ---------------------------------------------------
657 tc_occ          :: { OccName }
658                 :  CONID                { mkSysOccFS tcName $1 }
659
660 qtc_name        :: { RdrName }
661                 : tc_occ                { mkRdrUnqual $1 }
662                 | QCONID                { mkIfaceOrig tcName $1 }
663
664 ---------------------------------------------------
665 qcls_name       :: { RdrName }
666                 : CONID                 { mkRdrUnqual (mkSysOccFS clsName $1) }
667                 | QCONID                { mkIfaceOrig clsName $1 }
668
669 ---------------------------------------------------
670 tv_name         :: { RdrName }
671                 :  var_fs               { mkRdrUnqual (mkSysOccFS tvName $1) }
672
673 tv_bndr         :: { HsTyVarBndr RdrName }
674                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
675                 |  tv_name              { IfaceTyVar $1 liftedTypeKind }
676
677 tv_bndrs        :: { [HsTyVarBndr RdrName] }
678                 : tv_bndrs1             { $1 }
679                 | '[' tv_bndrs1 ']'     { $2 }  -- Backward compatibility
680
681 tv_bndrs1       :: { [HsTyVarBndr RdrName] }
682                 :                       { [] }
683                 | tv_bndr tv_bndrs1     { $1 : $2 }
684
685 ---------------------------------------------------
686 fds :: { [([RdrName], [RdrName])] }
687         : {- empty -}                   { [] }
688         | '|' fds1                      { reverse $2 }
689
690 fds1 :: { [([RdrName], [RdrName])] }
691         : fds1 ',' fd                   { $3 : $1 }
692         | fd                            { [$1] }
693
694 fd :: { ([RdrName], [RdrName]) }
695         : varids0 '->' varids0          { (reverse $1, reverse $3) }
696
697 varids0 :: { [RdrName] }
698         : {- empty -}                   { [] }
699         | varids0 tv_name               { $2 : $1 }
700
701 ---------------------------------------------------
702 kind            :: { Kind }
703                 : akind                 { $1 }
704                 | akind '->' kind       { mkArrowKind $1 $3 }
705
706 akind           :: { Kind }
707                 : VARSYM                { if $1 == SLIT("*") then
708                                                 liftedTypeKind
709                                           else if $1 == SLIT("?") then
710                                                 openTypeKind
711                                           else if $1 == SLIT("\36") then
712                                                 usageTypeKind  -- dollar
713                                           else panic "ParseInterface: akind"
714                                         }
715                 | '(' kind ')'  { $2 }
716
717 --------------------------------------------------------------------------
718
719 id_info         :: { [HsIdInfo RdrName] }
720                 : id_info_item                  { [$1] }
721                 | id_info_item id_info          { $1 : $2 }
722
723 id_info_item    :: { HsIdInfo RdrName }
724                 : '__A' INTEGER                 { HsArity (fromInteger $2) }
725                 | '__U' activation core_expr    { HsUnfold $2 $3 }
726                 | '__S'                         { HsStrictness $1 }
727                 | '__C'                         { HsNoCafRefs }
728                 | '__P' qvar_name INTEGER       { HsWorker $2 (fromInteger $3) }
729
730 -------------------------------------------------------
731 core_expr       :: { UfExpr RdrName }
732 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
733                 | 'case' core_expr 'of' var_name
734                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
735
736                 | 'let' '{' core_val_bndr '=' core_expr
737                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
738                 | '__letrec' '{' rec_binds '}'          
739                   'in' core_expr                        { UfLet (UfRec $3) $6 }
740
741                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
742
743                 | fexpr                                 { $1 }
744
745 fexpr   :: { UfExpr RdrName }
746 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
747         | scc core_aexpr                                { UfNote (UfSCC $1) $2  }
748         | '__inline_me' core_aexpr                      { UfNote UfInlineMe $2 }
749         | '__inline_call' core_aexpr                    { UfNote UfInlineCall $2 }
750         | '__coerce' atype core_aexpr                   { UfNote (UfCoerce $2) $3 }
751         | core_aexpr                                    { $1 }
752
753 core_arg        :: { UfExpr RdrName }
754                 : '@' atype                                     { UfType $2 }
755                 | core_aexpr                                    { $1 }
756
757 core_args       :: { [UfExpr RdrName] }
758                 :                                               { [] }
759                 | core_arg core_args                            { $1 : $2 }
760
761 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
762 core_aexpr      : qvar_name                                     { UfVar $1 }
763                 | qdata_name                                    { UfVar $1 }
764
765                 | core_lit               { UfLit $1 }
766                 | '(' core_expr ')'      { $2 }
767
768                 | '('  ')'               { UfTuple (mkHsTupCon dataName Boxed [])   [] }
769                 | '(' comma_exprs2 ')'   { UfTuple (mkHsTupCon dataName Boxed $2)   $2 }
770                 | '(#' comma_exprs0 '#)' { UfTuple (mkHsTupCon dataName Unboxed $2) $2 }
771
772                 | '{' '__ccall' ccall_string type '}'       
773                            { let
774                                  (is_dyn, is_casm, may_gc) = $2
775
776                                  target | is_dyn    = DynamicTarget
777                                         | is_casm   = CasmTarget $3
778                                         | otherwise = StaticTarget $3
779
780                                  ccall = CCallSpec target CCallConv may_gc
781                              in
782                              UfFCall (CCall ccall) $4
783                            }
784
785
786 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
787 comma_exprs0    : {- empty -}                   { [ ] }
788                 | core_expr                     { [ $1 ] }
789                 | comma_exprs2                  { $1 }
790
791 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
792 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
793                 | core_expr ',' comma_exprs2                    { $1 : $3 }
794
795 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
796                 :                                               { [] }
797                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
798
799 core_alts       :: { [UfAlt RdrName] }
800                 :                                               { [] }
801                 | core_alt ';' core_alts                        { $1 : $3 }
802
803 core_alt        :: { UfAlt RdrName }
804 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
805
806 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
807 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
808                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
809                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
810                 | '('  ')'                      { (UfTupleAlt (mkHsTupCon dataName Boxed []),   []) }
811                 | '(' comma_var_names1 ')'      { (UfTupleAlt (mkHsTupCon dataName Boxed $2),   $2) }
812                 | '(#' comma_var_names1 '#)'    { (UfTupleAlt (mkHsTupCon dataName Unboxed $2), $2) }
813                 | '__DEFAULT'                   { (UfDefault, []) }
814                 | '(' core_pat ')'              { $2 }
815
816 core_pat_names :: { [RdrName] }
817 core_pat_names :                                { [] }
818                 | core_pat_name core_pat_names  { $1 : $2 }
819
820 -- Tyvar names and variable names live in different name spaces
821 -- so they need to be signalled separately.  But we don't record 
822 -- types or kinds in a pattern; we work that out from the type 
823 -- of the case scrutinee
824 core_pat_name   :: { RdrName }
825 core_pat_name   : var_name                      { $1 }
826                 | '@' tv_name                   { $2 }
827         
828 comma_var_names1 :: { [RdrName] }       -- One or more
829 comma_var_names1 : var_name                                     { [$1] }
830                  | var_name ',' comma_var_names1                { $1 : $3 }
831
832 core_lit        :: { Literal }
833 core_lit        : integer                       { mkMachInt $1 }
834                 | CHAR                          { MachChar $1 }
835                 | STRING                        { MachStr $1 }
836                 | rational                      { MachDouble $1 }
837                 | '__word' integer              { mkMachWord $2 }
838                 | '__word64' integer            { mkMachWord64 $2 }
839                 | '__int64' integer             { mkMachInt64 $2 }
840                 | '__float' rational            { MachFloat $2 }
841                 | '__addr' integer              { MachAddr $2 }
842                 | '__label' STRING              { MachLabel $2 }
843
844 integer         :: { Integer }
845                 : INTEGER                       { $1 }
846                 | '-' INTEGER                   { (-$2) }
847
848 rational        :: { Rational }
849                 : RATIONAL                      { $1 }
850                 | '-' RATIONAL                  { (-$2) }
851
852 core_bndr       :: { UfBinder RdrName }
853 core_bndr       : core_val_bndr                                 { $1 }
854                 | core_tv_bndr                                  { $1 }
855
856 core_bndrs      :: { [UfBinder RdrName] }
857 core_bndrs      :                                               { [] }
858                 | core_bndr core_bndrs                          { $1 : $2 }
859
860 core_val_bndr   :: { UfBinder RdrName }
861 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
862
863 core_tv_bndr    :: { UfBinder RdrName }
864 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
865                 |  '@' tv_name                          { UfTyBinder $2 liftedTypeKind }
866
867 ccall_string    :: { FAST_STRING }
868                 : STRING                                        { $1 }
869                 | CLITLIT                                       { $1 }
870                 | VARID                                         { $1 }
871                 | CONID                                         { $1 }
872
873 ------------------------------------------------------------------------
874 scc     :: { CostCentre }
875         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
876         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
877                              { NormalCC { cc_name = $3, cc_mod = $4,
878                                           cc_is_dupd = $5, cc_is_caf = $6 } }
879
880 cc_name :: { EncodedFS }
881         : CONID                 { $1 }
882         | var_fs                { $1 }
883   
884 cc_dup  :: { IsDupdCC }
885 cc_dup  :                       { OriginalCC }
886         | '!'                   { DupdCC }
887
888 cc_caf  :: { IsCafCC }
889         :                       { NotCafCC }
890         | '__C'                 { CafCC }
891
892 -------------------------------------------------------------------
893
894 src_loc :: { SrcLoc }
895 src_loc :                               {% getSrcLocP }
896
897 -- Check the project version: this makes sure
898 -- that the project version (e.g. 407) in the interface
899 -- file is the same as that for the compiler that's reading it
900 checkVersion :: { () }
901            : {-empty-}                  {% checkVersion Nothing }
902            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
903
904 ------------------------------------------------------------------- 
905
906 --                      Haskell code 
907 {
908 happyError :: P a
909 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
910
911 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
912 }