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