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