[project @ 2000-10-12 15:23:21 by sewardj]
[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, IfaceStuff(..) ) 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, mkHsUsForAllTy, mkHsTupCon )
38 import HsCore
39 import Demand           ( mkStrictnessInfo )
40 import Literal          ( Literal(..), mkMachInt, mkMachInt64, mkMachWord, mkMachWord64 )
41 import BasicTypes       ( Fixity(..), FixityDirection(..), 
42                           NewOrData(..), Version, initialVersion, Boxity(..)
43                         )
44 import CostCentre       ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
45 import CallConv         ( cCallConv )
46 import HsPragmas        ( noDataPragmas, noClassPragmas )
47 import Type             ( Kind, mkArrowKind, boxedTypeKind, openTypeKind )
48 import IdInfo           ( exactArity, InlinePragInfo(..) )
49 import PrimOp           ( CCall(..), CCallTarget(..) )
50 import Lex              
51
52 import RnMonad          ( ImportVersion, ParsedIface(..), WhatsImported(..),
53                           ExportItem, RdrAvailInfo, GenAvailInfo(..), 
54                           WhetherHasOrphans, IsBootInterface
55                         ) 
56 import RdrName          ( RdrName, mkRdrUnqual, mkSysQual, mkSysUnqual )
57 import Name             ( OccName )
58 import OccName          ( mkSysOccFS,
59                           tcName, varName, ipName, dataName, clsName, tvName, uvName,
60                           EncodedFS 
61                         )
62 import Module           ( ModuleName, PackageName, mkSysModuleNameFS, mkModule )
63 import SrcLoc           ( SrcLoc )
64 import CmdLineOpts      ( opt_InPackage )
65 import Outputable
66 import List             ( insert )
67 import Class            ( DefMeth (..) )
68
69 import GlaExts
70 import FastString       ( tailFS )
71 }
72
73 %name       parseIface
74 %tokentype  { Token }
75 %monad      { P }{ thenP }{ returnP }
76 %lexer      { lexer } { ITeof }
77
78 %token
79  'as'           { ITas }
80  'case'         { ITcase }                      -- Haskell keywords
81  'class'        { ITclass } 
82  'data'         { ITdata } 
83  'default'      { ITdefault }
84  'deriving'     { ITderiving }
85  'do'           { ITdo }
86  'else'         { ITelse }
87  'hiding'       { IThiding }
88  'if'           { ITif }
89  'import'       { ITimport }
90  'in'           { ITin }
91  'infix'        { ITinfix }
92  'infixl'       { ITinfixl }
93  'infixr'       { ITinfixr }
94  'instance'     { ITinstance }
95  'let'          { ITlet }
96  'module'       { ITmodule }
97  'newtype'      { ITnewtype }
98  'of'           { ITof }
99  'qualified'    { ITqualified }
100  'then'         { ITthen }
101  'type'         { ITtype }
102  'where'        { ITwhere }
103
104  'forall'       { ITforall }                    -- GHC extension keywords
105  'foreign'      { ITforeign }
106  'export'       { ITexport }
107  'label'        { ITlabel } 
108  'dynamic'      { ITdynamic }
109  'unsafe'       { ITunsafe }
110  'with'         { ITwith }
111  'stdcall'      { ITstdcallconv }
112  'ccall'        { ITccallconv }
113
114  '__interface'  { ITinterface }                 -- interface keywords
115  '__export'     { IT__export }
116  '__depends'    { ITdepends }
117  '__forall'     { IT__forall }
118  '__letrec'     { ITletrec }
119  '__coerce'     { ITcoerce }
120  '__inline_me'  { ITinlineMe }
121  '__inline_call'{ ITinlineCall }
122  '__DEFAULT'    { ITdefaultbranch }
123  '__bot'        { ITbottom }
124  '__integer'    { ITinteger_lit }
125  '__float'      { ITfloat_lit }
126  '__word'       { ITword_lit }
127  '__int64'      { ITint64_lit }
128  '__word64'     { ITword64_lit }
129  '__rational'   { ITrational_lit }
130  '__addr'       { ITaddr_lit }
131  '__label'      { ITlabel_lit }
132  '__litlit'     { ITlit_lit }
133  '__string'     { ITstring_lit }
134  '__ccall'      { ITccall $$ }
135  '__scc'        { ITscc }
136  '__sccC'       { ITsccAllCafs }
137
138  '__u'          { ITusage }
139  '__fuall'      { ITfuall }
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  '/\\'          { ITbiglam }                    -- GHC-extension symbols
164
165  '{'            { ITocurly }                    -- special symbols
166  '}'            { ITccurly }
167  '{|'           { ITocurlybar }                         -- special symbols
168  '|}'           { ITccurlybar }                         -- special symbols
169  '['            { ITobrack }
170  ']'            { ITcbrack }
171  '('            { IToparen }
172  ')'            { ITcparen }
173  '(#'           { IToubxparen }
174  '#)'           { ITcubxparen }
175  ';'            { ITsemi }
176  ','            { ITcomma }
177
178  VARID          { ITvarid    $$ }               -- identifiers
179  CONID          { ITconid    $$ }
180  VARSYM         { ITvarsym   $$ }
181  CONSYM         { ITconsym   $$ }
182  QVARID         { ITqvarid   $$ }
183  QCONID         { ITqconid   $$ }
184  QVARSYM        { ITqvarsym  $$ }
185  QCONSYM        { ITqconsym  $$ }
186
187  IPVARID        { ITipvarid  $$ }               -- GHC extension
188
189  PRAGMA         { ITpragma   $$ }
190
191  CHAR           { ITchar     $$ }
192  STRING         { ITstring   $$ }
193  INTEGER        { ITinteger  $$ }
194  RATIONAL       { ITrational $$ }
195  CLITLIT        { ITlitlit   $$ }
196
197  UNKNOWN        { ITunknown  $$ }
198 %%
199
200 -- iface_stuff is the main production.
201 -- It recognises (a) a whole interface file
202 --               (b) a type (so that type sigs can be parsed lazily)
203 --               (c) the IdInfo part of a signature (same reason)
204
205 iface_stuff :: { IfaceStuff }
206 iface_stuff : iface             { PIface   $1 }
207             | type              { PType    $1 }
208             | id_info           { PIdInfo  $1 }
209             | '__R' rules       { PRules   $2 }
210             | '__D' deprecs     { PDeprecs $2 }
211
212
213 iface           :: { ParsedIface }
214 iface           : '__interface' package mod_name 
215                         version sub_versions
216                         orphans checkVersion 'where'
217                   exports_part
218                   import_part
219                   fix_decl_part
220                   instance_decl_part
221                   decls_part
222                   rules_and_deprecs
223                   { ParsedIface {
224                         pi_mod  = mkModule $3 $2,       -- Module itself
225                         pi_vers = $4,                   -- Module version
226                         pi_orphan  = $6,
227                         pi_exports = $9,                -- Exports
228                         pi_usages  = $10,               -- Usages
229                         pi_fixity  = (fst $5,$11),      -- Fixies
230                         pi_insts   = $12,               -- Local instances
231                         pi_decls   = $13,               -- Decls
232                         pi_rules   = (snd $5,fst $14),  -- Rules 
233                         pi_deprecs = snd $14            -- Deprecations 
234                    } }
235
236 -- Versions for fixities and rules (optional)
237 sub_versions :: { (Version,Version) }
238         : '[' version version ']'               { ($2,$3) }
239         | {- empty -}                           { (initialVersion, initialVersion) }
240
241 --------------------------------------------------------------------------
242
243 import_part :: { [ImportVersion OccName] }
244 import_part :                                             { [] }
245             |  import_decl import_part                    { $1 : $2 }
246             
247 import_decl :: { ImportVersion OccName }
248 import_decl : 'import' mod_name orphans is_boot whats_imported ';'
249                         { (mkSysModuleNameFS $2, $3, $4, $5) }
250
251 orphans             :: { WhetherHasOrphans }
252 orphans             :                                           { False }
253                     | '!'                                       { True }
254
255 is_boot             :: { IsBootInterface }
256 is_boot             :                                           { False }
257                     | '@'                                       { True }
258
259 whats_imported      :: { WhatsImported OccName }
260 whats_imported      :                                                   { NothingAtAll }
261                     | '::' version                                      { Everything $2 }
262                     | '::' version version version name_version_pairs   { Specifically $2 $3 $4 $5 }
263
264 name_version_pairs  ::  { [(OccName, Version)] }
265 name_version_pairs  :                                           { [] }
266                     |  name_version_pair name_version_pairs     { $1 : $2 }
267
268 name_version_pair   ::  { (OccName, Version) }
269 name_version_pair   :  var_occ version                          { ($1, $2) }
270                     |  tc_occ  version                          { ($1, $2) }
271
272
273 --------------------------------------------------------------------------
274
275 exports_part    :: { [ExportItem] }
276 exports_part    :                                       { [] }
277                 | '__export' mod_name entities ';'
278                         exports_part                    { (mkSysModuleNameFS $2, $3) : $5 }
279
280 entities        :: { [RdrAvailInfo] }
281 entities        :                                       { [] }
282                 |  entity entities                      { $1 : $2 }
283
284 entity          :: { RdrAvailInfo }
285 entity          :  var_occ                              { Avail $1 }
286                 |  tc_occ                               { AvailTC $1 [$1] }
287                 |  tc_occ '|' stuff_inside              { AvailTC $1 $3 }
288                 |  tc_occ stuff_inside                  { AvailTC $1 (insert $1 $2) }
289                 -- The 'insert' is important.  The stuff_inside is sorted, and
290                 -- insert keeps it that way.  This is important when comparing 
291                 -- against the new interface file, which has the stuff in sorted order
292                 -- If they differ, we'll bump the module number when it's unnecessary
293
294 stuff_inside    :: { [OccName] }
295 stuff_inside    :  '{' val_occs '}'                     { $2 }
296
297 val_occ         :: { OccName }
298                 :  var_occ              { $1 }
299                 |  data_occ             { $1 }
300
301 val_occs        :: { [OccName] }
302                 :  val_occ              { [$1] }
303                 |  val_occ val_occs     { $1 : $2 }
304
305
306 --------------------------------------------------------------------------
307
308 fix_decl_part :: { [RdrNameFixitySig] }
309 fix_decl_part : {- empty -}                             { [] }
310               | fix_decls ';'                           { $1 }
311
312 fix_decls     :: { [RdrNameFixitySig] }
313 fix_decls     :                                         { [] }
314               | fix_decl fix_decls                      { $1 : $2 }
315
316 fix_decl :: { RdrNameFixitySig }
317 fix_decl : src_loc fixity prec var_or_data_name         { FixitySig $4 (Fixity $3 $2) $1 }
318
319 fixity      :: { FixityDirection }
320 fixity      : 'infixl'                                  { InfixL }
321             | 'infixr'                                  { InfixR }
322             | 'infix'                                   { InfixN }
323    
324 prec        :: { Int }
325 prec        : INTEGER                                   { fromInteger $1 }
326
327 -----------------------------------------------------------------------------
328
329 csigs           :: { [RdrNameSig] }
330 csigs           :                               { [] }
331                 | 'where' '{' csigs1 '}'        { $3 }
332
333 csigs1          :: { [RdrNameSig] }
334 csigs1          :                               { [] }
335                 | csig ';' csigs1               { $1 : $3 }
336
337 csig            :: { RdrNameSig }
338 csig            :  src_loc var_name '::' type           { mkClassOpSig NoDefMeth $2 $4 $1 }
339                 |  src_loc var_name '=' '::' type       { mkClassOpSig (DefMeth (error "DefMeth") )
340                                                                 $2 $5 $1 }
341                 |  src_loc var_name ';' '::' type       { mkClassOpSig GenDefMeth  $2 $5 $1 }           
342
343 --------------------------------------------------------------------------
344
345 instance_decl_part :: { [RdrNameInstDecl] }
346 instance_decl_part : {- empty -}                       { [] }
347                    | instance_decl_part inst_decl      { $2 : $1 }
348
349 inst_decl       :: { RdrNameInstDecl }
350 inst_decl       :  src_loc 'instance' type '=' var_name ';'
351                         { InstDecl $3
352                                    EmptyMonoBinds       {- No bindings -}
353                                    []                   {- No user pragmas -}
354                                    (Just $5)            {- Dfun id -}
355                                    $1
356                         }
357
358 --------------------------------------------------------------------------
359
360 decls_part :: { [(Version, RdrNameHsDecl)] }
361 decls_part 
362         :  {- empty -}                          { [] }
363         |  opt_version decl ';' decls_part              { ($1,$2):$4 }
364
365 decl    :: { RdrNameHsDecl }
366 decl    : src_loc var_name '::' type maybe_idinfo
367                          { SigD (IfaceSig $2 $4 ($5 $2) $1) }
368         | src_loc 'type' tc_name tv_bndrs '=' type                     
369                         { TyClD (TySynonym $3 $4 $6 $1) }
370         | src_loc 'data' opt_decl_context tc_name tv_bndrs constrs             
371                         { TyClD (mkTyData DataType $3 $4 $5 $6 (length $6) Nothing noDataPragmas $1) }
372         | src_loc 'newtype' opt_decl_context tc_name tv_bndrs newtype_constr
373                         { TyClD (mkTyData NewType $3 $4 $5 $6 1 Nothing noDataPragmas $1) }
374         | src_loc 'class' opt_decl_context tc_name tv_bndrs fds csigs
375                         { TyClD (mkClassDecl $3 $4 $5 $6 $7 EmptyMonoBinds 
376                                         noClassPragmas $1) }
377
378 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
379 maybe_idinfo  : {- empty -}     { \_ -> [] }
380               | pragma          { \x -> case $1 of
381                                      POk _ (PIdInfo id_info) -> id_info
382                                      PFailed err -> 
383                                         pprPanic "IdInfo parse failed" 
384                                             (vcat [ppr x, err])
385                                 }
386
387 pragma  :: { ParseResult IfaceStuff }
388 pragma  : src_loc PRAGMA        { parseIface $2 PState{ bol = 0#, atbol = 1#,
389                                                         context = [],
390                                                         glasgow_exts = 1#,
391                                                         loc = $1 }
392                                 }
393
394 -----------------------------------------------------------------------------
395
396 rules_and_deprecs :: { ([RdrNameRuleDecl], [RdrNameDeprecation]) }
397 rules_and_deprecs : {- empty -} { ([], []) }
398                   | rules_and_deprecs rule_or_deprec
399                                 { let
400                                      append2 (xs1,ys1) (xs2,ys2) =
401                                         (xs1 `app` xs2, ys1 `app` ys2)
402                                      xs `app` [] = xs -- performance paranoia
403                                      xs `app` ys = xs ++ ys
404                                   in append2 $1 $2
405                                 }
406
407 rule_or_deprec :: { ([RdrNameRuleDecl], [RdrNameDeprecation]) }
408 rule_or_deprec : pragma { case $1 of
409                              POk _ (PRules   rules)   -> (rules,[])
410                              POk _ (PDeprecs deprecs) -> ([],deprecs)
411                              PFailed err -> pprPanic "Rules/Deprecations parse failed" err
412                         }
413
414 -----------------------------------------------------------------------------
415
416 rules      :: { [RdrNameRuleDecl] }
417            : {- empty -}        { [] }
418            | rule ';' rules     { $1:$3 }
419
420 rule       :: { RdrNameRuleDecl }
421 rule       : src_loc STRING rule_forall qvar_name 
422              core_args '=' core_expr    { IfaceRule $2 $3 $4 $5 $7 $1 } 
423
424 rule_forall     :: { [UfBinder RdrName] }
425 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
426                   
427 -----------------------------------------------------------------------------
428
429 deprecs         :: { [RdrNameDeprecation] }
430 deprecs         : {- empty -}           { [] }
431                 | deprec ';' deprecs    { $1 : $3 }
432
433 deprec          :: { RdrNameDeprecation }
434 deprec          : src_loc STRING                { Deprecation (IEModuleContents undefined) $2 $1 }
435                 | src_loc deprec_name STRING    { Deprecation $2 $3 $1 }
436
437 -- SUP: TEMPORARY HACK
438 deprec_name     :: { RdrNameIE }
439                 : var_name              { IEVar      $1 }
440                 | data_name             { IEThingAbs $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 opt_decl_context  :: { RdrNameContext }
452 opt_decl_context  :                             { [] }
453                   | context '=>'                { $1 }
454
455 ----------------------------------------------------------------------------
456
457 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
458                 :                       { [] }
459                 | '=' constrs1          { $2 }
460
461 constrs1        :: { [RdrNameConDecl] }
462 constrs1        :  constr               { [$1] }
463                 |  constr '|' constrs1  { $1 : $3 }
464
465 constr          :: { RdrNameConDecl }
466 constr          :  src_loc ex_stuff data_name batypes           { mk_con_decl $3 $2 (VanillaCon $4) $1 }
467                 |  src_loc ex_stuff data_name '{' fields1 '}'   { mk_con_decl $3 $2 (RecCon $5)     $1 }
468                 -- We use "data_fs" so as to include ()
469
470 newtype_constr  :: { [RdrNameConDecl] {- Not allowed to be empty -} }
471 newtype_constr  : src_loc '=' ex_stuff data_name atype  { [mk_con_decl $4 $3 (VanillaCon [Unbanged $5]) $1] }
472                 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
473                                                         { [mk_con_decl $4 $3 (RecCon [([$6], Unbanged $8)]) $1] }
474
475 ex_stuff :: { ([HsTyVarBndr RdrName], RdrNameContext) }
476 ex_stuff        :                                       { ([],[]) }
477                 | '__forall' tv_bndrs opt_context '=>'  { ($2,$3) }
478
479 batypes         :: { [RdrNameBangType] }
480 batypes         :                                       { [] }
481                 |  batype batypes                       { $1 : $2 }
482
483 batype          :: { RdrNameBangType }
484 batype          :  atype                                { Unbanged $1 }
485                 |  '!' atype                            { Banged   $2 }
486                 |  '!' '!' atype                        { Unpacked $3 }
487
488 fields1         :: { [([RdrName], RdrNameBangType)] }
489 fields1         : field                                 { [$1] }
490                 | field ',' fields1                     { $1 : $3 }
491
492 field           :: { ([RdrName], RdrNameBangType) }
493 field           :  var_names1 '::' type         { ($1, Unbanged $3) }
494                 |  var_names1 '::' '!' type     { ($1, Banged   $4) }
495                 |  var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
496 --------------------------------------------------------------------------
497
498 type            :: { RdrNameHsType }
499 type            : '__fuall'  fuall '=>' type    { mkHsUsForAllTy $2 $4 }
500                 | '__forall' tv_bndrs 
501                         opt_context '=>' type   { mkHsForAllTy (Just $2) $3 $5 }
502                 | btype '->' type               { HsFunTy $1 $3 }
503                 | btype                         { $1 }
504
505 fuall           :: { [RdrName] }
506 fuall           : '[' uv_bndrs ']'                      { $2 }
507
508 opt_context     :: { RdrNameContext }
509 opt_context     :                                       { [] }
510                 | context                               { $1 }
511
512 context         :: { RdrNameContext }
513 context         : '(' context_list1 ')'                 { $2 }
514                 | '{' context_list1 '}'                 { $2 }  -- Backward compatibility
515
516 context_list1   :: { RdrNameContext }
517 context_list1   : class                                 { [$1] }
518                 | class ',' context_list1               { $1 : $3 }
519
520 class           :: { HsPred RdrName }
521 class           :  qcls_name atypes                     { (HsPClass $1 $2) }
522                 |  ipvar_name '::' type                 { (HsPIParam $1 $3) }
523
524 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
525 types0          :  {- empty -}                          { [ ] }
526                 |  type                                 { [ $1 ] }
527                 |  types2                               { $1 }
528
529 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
530 types2          :  type ',' type                        { [$1,$3] }
531                 |  type ',' types2                      { $1 : $3 }
532
533 btype           :: { RdrNameHsType }
534 btype           :  atype                                { $1 }
535                 |  btype atype                          { HsAppTy $1 $2 }
536                 |  '__u' usage atype                    { HsUsgTy $2 $3 }
537
538 usage           :: { HsUsageAnn RdrName }
539 usage           : '-'                                   { HsUsOnce }
540                 | '!'                                   { HsUsMany }
541                 | uv_name                               { HsUsVar $1 }
542
543 atype           :: { RdrNameHsType }
544 atype           :  qtc_name                             { HsTyVar $1 }
545                 |  tv_name                              { HsTyVar $1 }
546                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
547                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
548                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
549                 |  '[' type ']'                         { HsListTy  $2 }
550                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
551                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
552                 |  '(' type ')'                         { $2 }
553
554 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
555 atypes          :                                       { [] }
556                 |  atype atypes                         { $1 : $2 }
557 ---------------------------------------------------------------------
558 package         :: { PackageName }
559                 :  STRING               { $1 }
560                 | {- empty -}           { opt_InPackage }       -- Useful for .hi-boot files,
561                                                                 -- which can omit the package Id
562                                                                 -- Module loops are always within a package
563
564 mod_name        :: { ModuleName }
565                 :  CONID                { mkSysModuleNameFS $1 }
566
567
568 ---------------------------------------------------
569 var_fs          :: { EncodedFS }
570                 : VARID                 { $1 }
571                 | '!'                   { SLIT("!") }
572                 | 'as'                  { SLIT("as") }
573                 | 'qualified'           { SLIT("qualified") }
574                 | 'hiding'              { SLIT("hiding") }
575                 | 'forall'              { SLIT("forall") }
576                 | 'foreign'             { SLIT("foreign") }
577                 | 'export'              { SLIT("export") }
578                 | 'label'               { SLIT("label") }
579                 | 'dynamic'             { SLIT("dynamic") }
580                 | 'unsafe'              { SLIT("unsafe") }
581                 | 'with'                { SLIT("with") }
582                 | 'ccall'               { SLIT("ccall") }
583                 | 'stdcall'             { SLIT("stdcall") }
584
585 qvar_fs         :: { (EncodedFS, EncodedFS) }
586                 :  QVARID               { $1 }
587                 |  QVARSYM              { $1 }
588
589 var_occ         :: { OccName }
590                 :  var_fs               { mkSysOccFS varName $1 }
591
592 var_name        :: { RdrName }
593 var_name        :  var_occ              { mkRdrUnqual $1 }
594
595 qvar_name       :: { RdrName }
596 qvar_name       :  var_name             { $1 }
597                 |  qvar_fs              { mkSysQual varName $1 }
598
599 ipvar_name      :: { RdrName }
600                 :  IPVARID              { mkSysUnqual ipName (tailFS $1) }
601
602 var_names       :: { [RdrName] }
603 var_names       :                       { [] }
604                 | var_name var_names    { $1 : $2 }
605
606 var_names1      :: { [RdrName] }
607 var_names1      : var_name var_names    { $1 : $2 }
608
609 ---------------------------------------------------
610 -- For some bizarre reason, 
611 --      (,,,)      is dealt with by the parser
612 --      Foo.(,,,)  is dealt with by the lexer
613 -- Sigh
614
615 data_fs         :: { EncodedFS }
616                 :  CONID                { $1 }
617                 |  CONSYM               { $1 }
618
619 qdata_fs        :: { (EncodedFS, EncodedFS) }
620                 :  QCONID               { $1 }
621                 |  QCONSYM              { $1 }
622
623 data_occ        :: { OccName }
624                 :  data_fs              { mkSysOccFS dataName $1 }
625
626 data_name       :: { RdrName }
627                 :  data_occ             { mkRdrUnqual $1 }
628
629 qdata_name      :: { RdrName }
630 qdata_name      :  data_name            { $1 }
631                 |  qdata_fs             { mkSysQual dataName $1 }
632                                 
633 qdata_names     :: { [RdrName] }
634 qdata_names     :                               { [] }
635                 | qdata_name qdata_names        { $1 : $2 }
636
637 var_or_data_name :: { RdrName }
638                   : var_name                    { $1 }
639                   | data_name                   { $1 }
640
641 ---------------------------------------------------
642 tc_fs           :: { EncodedFS }
643                 :  data_fs              { $1 }
644
645 tc_occ          :: { OccName }
646                 :  tc_fs                { mkSysOccFS tcName $1 }
647
648 tc_name         :: { RdrName }
649                 :  tc_occ               { mkRdrUnqual $1 }
650
651 qtc_name        :: { RdrName }
652                 : tc_name               { $1 }
653                 | qdata_fs              { mkSysQual tcName $1 }
654
655 ---------------------------------------------------
656 cls_name        :: { RdrName }
657                 :  data_fs              { mkSysUnqual clsName $1 }
658
659 qcls_name       :: { RdrName }
660                 : cls_name              { $1 }
661                 | qdata_fs              { mkSysQual clsName $1 }
662
663 ---------------------------------------------------
664 uv_name         :: { RdrName }
665                 :  VARID                { mkSysUnqual uvName $1 }
666
667 uv_bndr         :: { RdrName }
668                 :  uv_name              { $1 }
669
670 uv_bndrs        :: { [RdrName] }
671                 :                       { [] }
672                 | uv_bndr uv_bndrs      { $1 : $2 }
673
674 ---------------------------------------------------
675 tv_name         :: { RdrName }
676                 :  VARID                { mkSysUnqual tvName $1 }
677                 |  VARSYM               { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
678
679 tv_bndr         :: { HsTyVarBndr RdrName }
680                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
681                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
682
683 tv_bndrs        :: { [HsTyVarBndr RdrName] }
684 tv_bndrs        : 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                                                 boxedTypeKind
715                                           else if $1 == SLIT("?") then
716                                                 openTypeKind
717                                           else panic "ParseInterface: akind"
718                                         }
719                 | '(' kind ')'  { $2 }
720
721 --------------------------------------------------------------------------
722
723 id_info         :: { [HsIdInfo RdrName] }
724                 :                               { [] }
725                 | id_info_item id_info          { $1 : $2 }
726
727 id_info_item    :: { HsIdInfo RdrName }
728                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
729                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
730                 | '__M'                         { HsCprInfo }
731                 | '__S'                         { HsStrictness (mkStrictnessInfo $1) }
732                 | '__C'                         { HsNoCafRefs }
733                 | '__P' qvar_name               { HsWorker $2 }
734
735 inline_prag     :: { InlinePragInfo }
736                 :  {- empty -}                  { NoInlinePragInfo }
737                 | '[' from_prag phase ']'       { IMustNotBeINLINEd $2 $3 }
738
739 from_prag       :: { Bool }
740                 : {- empty -}                   { True }
741                 | '!'                           { False }
742
743 phase           :: { Maybe Int }
744                 : {- empty -}                   { Nothing }
745                 | INTEGER                       { Just (fromInteger $1) }
746
747 -------------------------------------------------------
748 core_expr       :: { UfExpr RdrName }
749 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
750                 | 'case' core_expr 'of' var_name
751                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
752
753                 | 'let' '{' core_val_bndr '=' core_expr
754                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
755                 | '__letrec' '{' rec_binds '}'          
756                   'in' core_expr                        { UfLet (UfRec $3) $6 }
757
758                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
759
760                 | fexpr                                 { $1 }
761
762 fexpr   :: { UfExpr RdrName }
763 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
764         | scc core_aexpr                                { UfNote (UfSCC $1) $2  }
765         | '__inline_me' core_aexpr                      { UfNote UfInlineMe $2 }
766         | '__inline_call' core_aexpr                    { UfNote UfInlineCall $2 }
767         | '__coerce' atype core_aexpr                   { UfNote (UfCoerce $2) $3 }
768         | core_aexpr                                    { $1 }
769
770 core_arg        :: { UfExpr RdrName }
771                 : '@' atype                                     { UfType $2 }
772                 | core_aexpr                                    { $1 }
773
774 core_args       :: { [UfExpr RdrName] }
775                 :                                               { [] }
776                 | core_arg core_args                            { $1 : $2 }
777
778 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
779 core_aexpr      : qvar_name                                     { UfVar $1 }
780                 | qdata_name                                    { UfVar $1 }
781
782                 | core_lit               { UfLit $1 }
783                 | '(' core_expr ')'      { $2 }
784
785                 | '('  ')'               { UfTuple (mkHsTupCon dataName Boxed [])   [] }
786                 | '(' comma_exprs2 ')'   { UfTuple (mkHsTupCon dataName Boxed $2)   $2 }
787                 | '(#' comma_exprs0 '#)' { UfTuple (mkHsTupCon dataName Unboxed $2) $2 }
788
789                 | '{' '__ccall' ccall_string type '}'       
790                            { let
791                                  (is_dyn, is_casm, may_gc) = $2
792
793                                  target | is_dyn    = DynamicTarget (error "CCall dyn target bogus unique")
794                                         | otherwise = StaticTarget $3
795
796                                  ccall = CCall target is_casm may_gc cCallConv
797                              in
798                              UfCCall ccall $4
799                            }
800
801
802 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
803 comma_exprs0    : {- empty -}                   { [ ] }
804                 | core_expr                     { [ $1 ] }
805                 | comma_exprs2                  { $1 }
806
807 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
808 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
809                 | core_expr ',' comma_exprs2                    { $1 : $3 }
810
811 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
812                 :                                               { [] }
813                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
814
815 core_alts       :: { [UfAlt RdrName] }
816                 :                                               { [] }
817                 | core_alt ';' core_alts                        { $1 : $3 }
818
819 core_alt        :: { UfAlt RdrName }
820 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
821
822 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
823 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
824                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
825                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
826                 | '('  ')'                      { (UfTupleAlt (mkHsTupCon dataName Boxed []),   []) }
827                 | '(' comma_var_names1 ')'      { (UfTupleAlt (mkHsTupCon dataName Boxed $2),   $2) }
828                 | '(#' comma_var_names1 '#)'    { (UfTupleAlt (mkHsTupCon dataName Unboxed $2), $2) }
829                 | '__DEFAULT'                   { (UfDefault, []) }
830                 | '(' core_pat ')'              { $2 }
831
832 core_pat_names :: { [RdrName] }
833 core_pat_names :                                { [] }
834                 | core_pat_name core_pat_names  { $1 : $2 }
835
836 -- Tyvar names and variable names live in different name spaces
837 -- so they need to be signalled separately.  But we don't record 
838 -- types or kinds in a pattern; we work that out from the type 
839 -- of the case scrutinee
840 core_pat_name   :: { RdrName }
841 core_pat_name   : var_name                      { $1 }
842                 | '@' tv_name                   { $2 }
843         
844 comma_var_names1 :: { [RdrName] }       -- One or more
845 comma_var_names1 : var_name                                     { [$1] }
846                  | var_name ',' comma_var_names1                { $1 : $3 }
847
848 core_lit        :: { Literal }
849 core_lit        : integer                       { mkMachInt $1 }
850                 | CHAR                          { MachChar $1 }
851                 | STRING                        { MachStr $1 }
852                 | rational                      { MachDouble $1 }
853                 | '__word' integer              { mkMachWord $2 }
854                 | '__word64' integer            { mkMachWord64 $2 }
855                 | '__int64' integer             { mkMachInt64 $2 }
856                 | '__float' rational            { MachFloat $2 }
857                 | '__addr' integer              { MachAddr $2 }
858                 | '__label' STRING              { MachLabel $2 }
859
860 integer         :: { Integer }
861                 : INTEGER                       { $1 }
862                 | '-' INTEGER                   { (-$2) }
863
864 rational        :: { Rational }
865                 : RATIONAL                      { $1 }
866                 | '-' RATIONAL                  { (-$2) }
867
868 core_bndr       :: { UfBinder RdrName }
869 core_bndr       : core_val_bndr                                 { $1 }
870                 | core_tv_bndr                                  { $1 }
871
872 core_bndrs      :: { [UfBinder RdrName] }
873 core_bndrs      :                                               { [] }
874                 | core_bndr core_bndrs                          { $1 : $2 }
875
876 core_val_bndr   :: { UfBinder RdrName }
877 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
878
879 core_tv_bndr    :: { UfBinder RdrName }
880 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
881                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
882
883 ccall_string    :: { FAST_STRING }
884                 : STRING                                        { $1 }
885                 | CLITLIT                                       { $1 }
886                 | VARID                                         { $1 }
887                 | CONID                                         { $1 }
888
889 ------------------------------------------------------------------------
890 scc     :: { CostCentre }
891         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
892         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
893                              { NormalCC { cc_name = $3, cc_mod = $4,
894                                           cc_is_dupd = $5, cc_is_caf = $6 } }
895
896 cc_name :: { EncodedFS }
897         : CONID                 { $1 }
898         | var_fs                { $1 }
899   
900 cc_dup  :: { IsDupdCC }
901 cc_dup  :                       { OriginalCC }
902         | '!'                   { DupdCC }
903
904 cc_caf  :: { IsCafCC }
905         :                       { NotCafCC }
906         | '__C'                 { CafCC }
907
908 -------------------------------------------------------------------
909
910 src_loc :: { SrcLoc }
911 src_loc :                               {% getSrcLocP }
912
913 -- Check the project version: this makes sure
914 -- that the project version (e.g. 407) in the interface
915 -- file is the same as that for the compiler that's reading it
916 checkVersion :: { () }
917            : {-empty-}                  {% checkVersion Nothing }
918            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
919
920 ------------------------------------------------------------------- 
921
922 --                      Haskell code 
923 {
924 happyError :: P a
925 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
926
927 data IfaceStuff = PIface        ParsedIface
928                 | PIdInfo       [HsIdInfo RdrName]
929                 | PType         RdrNameHsType
930                 | PRules        [RdrNameRuleDecl]
931                 | PDeprecs      [RdrNameDeprecation]
932
933 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
934 }