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