[project @ 2000-11-01 17:15:28 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
1 {-      Notes about the syntax of interface files
2         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 The header
4 ~~~~~~~~~~
5   interface "edison" M 4 6 2 ! 406      Module M, version 4, from package 'edison',
6                                         Fixities version 6, rules version 2
7                                         Interface syntax version 406
8                                         ! means M contains orphans
9
10 Import declarations
11 ~~~~~~~~~~~~~~~~~~~
12   import Foo ;                          To compile M I used nothing from Foo, but it's 
13                                         below me in the hierarchy
14
15   import Foo ! @ ;                      Ditto, but the ! means that Foo contains orphans
16                                         and        the @ means that Foo is a boot interface
17
18   import Foo :: 3 ;                     To compile M I used everything from Foo, which has 
19                                         module version 3
20
21   import Foo :: 3 2 6 a 1 b 3 c 7 ;     To compile M I used Foo.  It had 
22                                                 module version 3
23                                                 fixity version 2
24                                                 rules  version 6
25                                         and some specific things besides.
26
27 -}
28
29
30 {
31 module ParseIface ( parseIface, 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 Type             ( Kind, mkArrowKind, boxedTypeKind, openTypeKind )
47 import IdInfo           ( exactArity, InlinePragInfo(..) )
48 import PrimOp           ( CCall(..), CCallTarget(..) )
49 import Lex              
50
51 import RnMonad          ( ParsedIface(..), ExportItem, IfaceDeprecs ) 
52 import HscTypes         ( WhetherHasOrphans, IsBootInterface, GenAvailInfo(..), 
53                           ImportVersion, WhatsImported(..),
54                           RdrAvailInfo )
55
56 import RdrName          ( RdrName, mkRdrUnqual, mkIfaceOrig )
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, opt_IgnoreIfacePragmas )
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             | rules_and_deprecs { PRulesAndDeprecs $1 }
210
211 iface           :: { ParsedIface }
212 iface           : '__interface' package mod_name 
213                         version sub_versions
214                         orphans checkVersion 'where'
215                   exports_part
216                   import_part
217                   fix_decl_part
218                   instance_decl_part
219                   decls_part
220                   rules_and_deprecs_part
221                   { ParsedIface {
222                         pi_mod  = mkModule $3 $2,       -- Module itself
223                         pi_vers = $4,                   -- Module version
224                         pi_orphan  = $6,
225                         pi_exports = (fst $5, $9),      -- Exports
226                         pi_usages  = $10,               -- Usages
227                         pi_fixity  = $11,               -- Fixies
228                         pi_insts   = $12,               -- Local instances
229                         pi_decls   = $13,               -- Decls
230                         pi_rules   = (snd $5,fst $14),  -- Rules 
231                         pi_deprecs = snd $14            -- Deprecations 
232                    } }
233
234 -- Versions for exports and rules (optional)
235 sub_versions :: { (Version,Version) }
236         : '[' version version ']'               { ($2,$3) }
237         | {- empty -}                           { (initialVersion, initialVersion) }
238
239 --------------------------------------------------------------------------
240
241 import_part :: { [ImportVersion OccName] }
242 import_part :                                             { [] }
243             |  import_decl import_part                    { $1 : $2 }
244             
245 import_decl :: { ImportVersion OccName }
246 import_decl : 'import' mod_name orphans is_boot whats_imported ';'
247                         { ({-mkSysModuleNameFS-} $2, $3, $4, $5) }
248
249 orphans             :: { WhetherHasOrphans }
250 orphans             :                                           { False }
251                     | '!'                                       { True }
252
253 is_boot             :: { IsBootInterface }
254 is_boot             :                                           { False }
255                     | '@'                                       { True }
256
257 whats_imported      :: { WhatsImported OccName }
258 whats_imported      :                                                   { NothingAtAll }
259                     | '::' version                                      { Everything $2 }
260                     | '::' version version version name_version_pairs   { Specifically $2 (Just $3) $5 $4 }
261
262 name_version_pairs  ::  { [(OccName, Version)] }
263 name_version_pairs  :                                           { [] }
264                     |  name_version_pair name_version_pairs     { $1 : $2 }
265
266 name_version_pair   ::  { (OccName, Version) }
267 name_version_pair   :  var_occ version                          { ($1, $2) }
268                     |  tc_occ  version                          { ($1, $2) }
269
270
271 --------------------------------------------------------------------------
272
273 exports_part    :: { [ExportItem] }
274 exports_part    :                                       { [] }
275                 | '__export' mod_name entities ';'
276                         exports_part                    { ({-mkSysModuleNameFS-} $2, $3) : $5 }
277
278 entities        :: { [RdrAvailInfo] }
279 entities        :                                       { [] }
280                 |  entity entities                      { $1 : $2 }
281
282 entity          :: { RdrAvailInfo }
283 entity          :  var_occ                              { Avail $1 }
284                 |  tc_occ                               { AvailTC $1 [$1] }
285                 |  tc_occ '|' stuff_inside              { AvailTC $1 $3 }
286                 |  tc_occ stuff_inside                  { AvailTC $1 ($1:$2) }
287                 -- Note that the "main name" comes at the beginning
288
289 stuff_inside    :: { [OccName] }
290 stuff_inside    :  '{' val_occs '}'                     { $2 }
291
292 val_occ         :: { OccName }
293                 :  var_occ              { $1 }
294                 |  data_occ             { $1 }
295
296 val_occs        :: { [OccName] }
297                 :  val_occ              { [$1] }
298                 |  val_occ val_occs     { $1 : $2 }
299
300
301 --------------------------------------------------------------------------
302
303 fix_decl_part :: { [RdrNameFixitySig] }
304 fix_decl_part : {- empty -}                             { [] }
305               | fix_decls ';'                           { $1 }
306
307 fix_decls     :: { [RdrNameFixitySig] }
308 fix_decls     :                                         { [] }
309               | fix_decl fix_decls                      { $1 : $2 }
310
311 fix_decl :: { RdrNameFixitySig }
312 fix_decl : src_loc fixity prec var_or_data_name         { FixitySig $4 (Fixity $3 $2) $1 }
313
314 fixity      :: { FixityDirection }
315 fixity      : 'infixl'                                  { InfixL }
316             | 'infixr'                                  { InfixR }
317             | 'infix'                                   { InfixN }
318    
319 prec        :: { Int }
320 prec        : INTEGER                                   { fromInteger $1 }
321
322 -----------------------------------------------------------------------------
323
324 csigs           :: { [RdrNameSig] }
325 csigs           :                               { [] }
326                 | 'where' '{' csigs1 '}'        { $3 }
327
328 csigs1          :: { [RdrNameSig] }
329 csigs1          :                               { [] }
330                 | csig ';' csigs1               { $1 : $3 }
331
332 csig            :: { RdrNameSig }
333 csig            :  src_loc qvar_name '::' type          { mkClassOpSig NoDefMeth $2 $4 $1 }
334                 |  src_loc qvar_name '=' '::' type      { mkClassOpSig (DefMeth (error "DefMeth") )
335                                                                 $2 $5 $1 }
336                 |  src_loc qvar_name ';' '::' type      { mkClassOpSig GenDefMeth  $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 '=' qvar_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, RdrNameTyClDecl)] }
356 decls_part 
357         :  {- empty -}                          { [] }
358         |  opt_version decl ';' decls_part              { ($1,$2):$4 }
359
360 decl    :: { RdrNameTyClDecl }
361 decl    : src_loc qvar_name '::' type maybe_idinfo
362                         { IfaceSig $2 $4 ($5 $2) $1 }
363         | src_loc 'type' qtc_name tv_bndrs '=' type                    
364                         { TySynonym $3 $4 $6 $1 }
365         | src_loc 'data' opt_decl_context qtc_name tv_bndrs constrs            
366                         { mkTyData DataType $3 $4 $5 $6 (length $6) Nothing $1 }
367         | src_loc 'newtype' opt_decl_context qtc_name tv_bndrs newtype_constr
368                         { mkTyData NewType $3 $4 $5 $6 1 Nothing $1 }
369         | src_loc 'class' opt_decl_context qtc_name tv_bndrs fds csigs
370                         { mkClassDecl $3 $4 $5 $6 $7 EmptyMonoBinds $1 }
371
372 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
373 maybe_idinfo  : {- empty -}     { \_ -> [] }
374               | pragma          { \x -> if opt_IgnoreIfacePragmas then [] 
375                                         else case $1 of
376                                                 POk _ (PIdInfo id_info) -> id_info
377                                                 PFailed err -> pprPanic "IdInfo parse failed" 
378                                                                         (vcat [ppr x, err])
379                                 }
380     {-
381       If a signature decl is being loaded, and opt_IgnoreIfacePragmas is on,
382       we toss away unfolding information.
383
384       Also, if the signature is loaded from a module we're importing from source,
385       we do the same. This is to avoid situations when compiling a pair of mutually
386       recursive modules, peering at unfolding info in the interface file of the other, 
387       e.g., you compile A, it looks at B's interface file and may as a result change
388       its interface file. Hence, B is recompiled, maybe changing its interface file,
389       which will the unfolding info used in A to become invalid. Simple way out is to
390       just ignore unfolding info.
391
392       [Jan 99: I junked the second test above.  If we're importing from an hi-boot
393        file there isn't going to *be* any pragma info.  The above comment
394        dates from a time where we picked up a .hi file first if it existed.]
395     -}
396
397 pragma  :: { ParseResult IfaceStuff }
398 pragma  : src_loc PRAGMA        { parseIface $2 PState{ bol = 0#, atbol = 1#,
399                                                         context = [],
400                                                         glasgow_exts = 1#,
401                                                         loc = $1 }
402                                 }
403
404 -----------------------------------------------------------------------------
405
406 rules_and_deprecs_part :: { ([RdrNameRuleDecl], IfaceDeprecs) }
407 rules_and_deprecs_part : {- empty -}    { ([], Nothing) }
408                        | pragma         { case $1 of
409                                              POk _ (PRulesAndDeprecs 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 rule_forall qvar_name 
429              core_args '=' core_expr    { IfaceRule $2 $3 $4 $5 $7 $1 } 
430
431 rule_forall     :: { [UfBinder RdrName] }
432 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
433                   
434 -----------------------------------------------------------------------------
435
436 deprec_prag     :: { IfaceDeprecs }
437 deprec_prag     : {- empty -}           { Nothing }
438                 | '__D' deprecs         { Just $2 } 
439
440 deprecs         :: { Either DeprecTxt [(RdrName,DeprecTxt)] }
441 deprecs         : STRING                { Left $1 }
442                 | deprec_list           { Right $1 }
443
444 deprec_list     :: { [(RdrName,DeprecTxt)] }
445 deprec_list     : deprec                        { [$1] }
446                 | deprec ';' deprec_list        { $1 : $3 }
447
448 deprec          :: { (RdrName,DeprecTxt) }
449 deprec          : deprec_name STRING    { ($1, $2) }
450
451 deprec_name     :: { RdrName }
452                 : qvar_name             { $1 }
453                 | qtc_name              { $1 }
454
455 -----------------------------------------------------------------------------
456
457 version         :: { Version }
458 version         :  INTEGER                      { fromInteger $1 }
459
460 opt_version     :: { Version }
461 opt_version     : version                       { $1 }
462                 | {- empty -}                   { initialVersion }
463         
464 opt_decl_context  :: { RdrNameContext }
465 opt_decl_context  :                             { [] }
466                   | context '=>'                { $1 }
467
468 ----------------------------------------------------------------------------
469
470 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
471                 :                       { [] }
472                 | '=' constrs1          { $2 }
473
474 constrs1        :: { [RdrNameConDecl] }
475 constrs1        :  constr               { [$1] }
476                 |  constr '|' constrs1  { $1 : $3 }
477
478 constr          :: { RdrNameConDecl }
479 constr          :  src_loc ex_stuff qdata_name batypes          { mk_con_decl $3 $2 (VanillaCon $4) $1 }
480                 |  src_loc ex_stuff qdata_name '{' fields1 '}'  { mk_con_decl $3 $2 (RecCon $5)     $1 }
481                 -- We use "data_fs" so as to include ()
482
483 newtype_constr  :: { [RdrNameConDecl] {- Not allowed to be empty -} }
484 newtype_constr  : src_loc '=' ex_stuff qdata_name atype { [mk_con_decl $4 $3 (VanillaCon [Unbanged $5]) $1] }
485                 | src_loc '=' ex_stuff qdata_name '{' qvar_name '::' atype '}'
486                                                         { [mk_con_decl $4 $3 (RecCon [([$6], Unbanged $8)]) $1] }
487
488 ex_stuff :: { ([HsTyVarBndr RdrName], RdrNameContext) }
489 ex_stuff        :                                       { ([],[]) }
490                 | '__forall' tv_bndrs opt_context '=>'  { ($2,$3) }
491
492 batypes         :: { [RdrNameBangType] }
493 batypes         :                                       { [] }
494                 |  batype batypes                       { $1 : $2 }
495
496 batype          :: { RdrNameBangType }
497 batype          :  atype                                { Unbanged $1 }
498                 |  '!' atype                            { Banged   $2 }
499                 |  '!' '!' atype                        { Unpacked $3 }
500
501 fields1         :: { [([RdrName], RdrNameBangType)] }
502 fields1         : field                                 { [$1] }
503                 | field ',' fields1                     { $1 : $3 }
504
505 field           :: { ([RdrName], RdrNameBangType) }
506 field           :  qvar_names1 '::' type                { ($1, Unbanged $3) }
507                 |  qvar_names1 '::' '!' type            { ($1, Banged   $4) }
508                 |  qvar_names1 '::' '!' '!' type        { ($1, Unpacked $5) }
509 --------------------------------------------------------------------------
510
511 type            :: { RdrNameHsType }
512 type            : '__fuall'  fuall '=>' type    { mkHsUsForAllTy $2 $4 }
513                 | '__forall' tv_bndrs 
514                         opt_context '=>' type   { mkHsForAllTy (Just $2) $3 $5 }
515                 | btype '->' type               { HsFunTy $1 $3 }
516                 | btype                         { $1 }
517
518 fuall           :: { [RdrName] }
519 fuall           : '[' uv_bndrs ']'                      { $2 }
520
521 opt_context     :: { RdrNameContext }
522 opt_context     :                                       { [] }
523                 | context                               { $1 }
524
525 context         :: { RdrNameContext }
526 context         : '(' context_list1 ')'                 { $2 }
527                 | '{' context_list1 '}'                 { $2 }  -- Backward compatibility
528
529 context_list1   :: { RdrNameContext }
530 context_list1   : class                                 { [$1] }
531                 | class ',' context_list1               { $1 : $3 }
532
533 class           :: { HsPred RdrName }
534 class           :  qcls_name atypes                     { (HsPClass $1 $2) }
535                 |  ipvar_name '::' type                 { (HsPIParam $1 $3) }
536
537 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
538 types0          :  {- empty -}                          { [ ] }
539                 |  type                                 { [ $1 ] }
540                 |  types2                               { $1 }
541
542 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
543 types2          :  type ',' type                        { [$1,$3] }
544                 |  type ',' types2                      { $1 : $3 }
545
546 btype           :: { RdrNameHsType }
547 btype           :  atype                                { $1 }
548                 |  btype atype                          { HsAppTy $1 $2 }
549                 |  '__u' usage atype                    { HsUsgTy $2 $3 }
550
551 usage           :: { HsUsageAnn RdrName }
552 usage           : '-'                                   { HsUsOnce }
553                 | '!'                                   { HsUsMany }
554                 | uv_name                               { HsUsVar $1 }
555
556 atype           :: { RdrNameHsType }
557 atype           :  qtc_name                             { HsTyVar $1 }
558                 |  tv_name                              { HsTyVar $1 }
559                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
560                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
561                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
562                 |  '[' type ']'                         { HsListTy  $2 }
563                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
564                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
565                 |  '(' type ')'                         { $2 }
566
567 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
568 atypes          :                                       { [] }
569                 |  atype atypes                         { $1 : $2 }
570 ---------------------------------------------------------------------
571 package         :: { PackageName }
572                 :  STRING               { $1 }
573                 | {- empty -}           { opt_InPackage }       -- Useful for .hi-boot files,
574                                                                 -- which can omit the package Id
575                                                                 -- Module loops are always within a package
576
577 mod_name        :: { ModuleName }
578                 :  CONID                { mkSysModuleNameFS $1 }
579
580
581 ---------------------------------------------------
582 var_fs          :: { EncodedFS }
583                 : VARID                 { $1 }
584                 | '!'                   { SLIT("!") }
585                 | 'as'                  { SLIT("as") }
586                 | 'qualified'           { SLIT("qualified") }
587                 | 'hiding'              { SLIT("hiding") }
588                 | 'forall'              { SLIT("forall") }
589                 | 'foreign'             { SLIT("foreign") }
590                 | 'export'              { SLIT("export") }
591                 | 'label'               { SLIT("label") }
592                 | 'dynamic'             { SLIT("dynamic") }
593                 | 'unsafe'              { SLIT("unsafe") }
594                 | 'with'                { SLIT("with") }
595                 | 'ccall'               { SLIT("ccall") }
596                 | 'stdcall'             { SLIT("stdcall") }
597
598 qvar_fs         :: { (EncodedFS, EncodedFS) }
599                 :  QVARID               { $1 }
600                 |  QVARSYM              { $1 }
601
602 var_occ         :: { OccName }
603                 :  var_fs               { mkSysOccFS varName $1 }
604
605 var_name        :: { RdrName }
606 var_name        :  var_occ              { mkRdrUnqual $1 }
607
608 qvar_name       :: { RdrName }
609 qvar_name       :  var_name             { $1 }
610                 |  qvar_fs              { mkIfaceOrig varName $1 }
611
612 ipvar_name      :: { RdrName }
613                 :  IPVARID              { mkRdrUnqual (mkSysOccFS ipName (tailFS $1)) }
614
615 qvar_names1     :: { [RdrName] }
616 qvar_names1     : qvar_name             { [$1] }
617                 | qvar_name qvar_names1 { $1 : $2 }
618
619 var_names       :: { [RdrName] }
620 var_names       :                       { [] }
621                 | var_name var_names    { $1 : $2 }
622
623 var_names1      :: { [RdrName] }
624 var_names1      : var_name var_names    { $1 : $2 }
625
626 ---------------------------------------------------
627 -- For some bizarre reason, 
628 --      (,,,)      is dealt with by the parser
629 --      Foo.(,,,)  is dealt with by the lexer
630 -- Sigh
631
632 data_fs         :: { EncodedFS }
633                 :  CONID                { $1 }
634                 |  CONSYM               { $1 }
635
636 qdata_fs        :: { (EncodedFS, EncodedFS) }
637                 :  QCONID               { $1 }
638                 |  QCONSYM              { $1 }
639
640 data_occ        :: { OccName }
641                 :  data_fs              { mkSysOccFS dataName $1 }
642
643 data_name       :: { RdrName }
644                 :  data_occ             { mkRdrUnqual $1 }
645
646 qdata_name      :: { RdrName }
647 qdata_name      :  data_name            { $1 }
648                 |  qdata_fs             { mkIfaceOrig dataName $1 }
649                                 
650 var_or_data_name :: { RdrName }
651                   : qvar_name                    { $1 }
652                   | qdata_name                   { $1 }
653
654 ---------------------------------------------------
655 tc_occ          :: { OccName }
656                 :  data_fs              { mkSysOccFS tcName $1 }
657
658 tc_name         :: { RdrName }
659                 :  tc_occ               { mkRdrUnqual $1 }
660
661 qtc_name        :: { RdrName }
662                 : tc_name               { $1 }
663                 | qdata_fs              { mkIfaceOrig tcName $1 }
664
665 ---------------------------------------------------
666 cls_name        :: { RdrName }
667                 :  data_fs              { mkRdrUnqual (mkSysOccFS clsName $1) }
668
669 qcls_name       :: { RdrName }
670                 : cls_name              { $1 }
671                 | qdata_fs              { mkIfaceOrig clsName $1 }
672
673 ---------------------------------------------------
674 uv_name         :: { RdrName }
675                 :  VARID                { mkRdrUnqual (mkSysOccFS uvName $1) }
676
677 uv_bndr         :: { RdrName }
678                 :  uv_name              { $1 }
679
680 uv_bndrs        :: { [RdrName] }
681                 :                       { [] }
682                 | uv_bndr uv_bndrs      { $1 : $2 }
683
684 ---------------------------------------------------
685 tv_name         :: { RdrName }
686                 :  VARID                { mkRdrUnqual (mkSysOccFS tvName $1) }
687                 |  VARSYM               { mkRdrUnqual (mkSysOccFS tvName $1) {- Allow t2 as a tyvar -} }
688
689 tv_bndr         :: { HsTyVarBndr RdrName }
690                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
691                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
692
693 tv_bndrs        :: { [HsTyVarBndr RdrName] }
694 tv_bndrs        : tv_bndrs1             { $1 }
695                 | '[' tv_bndrs1 ']'     { $2 }  -- Backward compatibility
696
697 tv_bndrs1       :: { [HsTyVarBndr RdrName] }
698                 :                       { [] }
699                 | tv_bndr tv_bndrs1     { $1 : $2 }
700
701 ---------------------------------------------------
702 fds :: { [([RdrName], [RdrName])] }
703         : {- empty -}                   { [] }
704         | '|' fds1                      { reverse $2 }
705
706 fds1 :: { [([RdrName], [RdrName])] }
707         : fds1 ',' fd                   { $3 : $1 }
708         | fd                            { [$1] }
709
710 fd :: { ([RdrName], [RdrName]) }
711         : varids0 '->' varids0          { (reverse $1, reverse $3) }
712
713 varids0 :: { [RdrName] }
714         : {- empty -}                   { [] }
715         | varids0 tv_name               { $2 : $1 }
716
717 ---------------------------------------------------
718 kind            :: { Kind }
719                 : akind                 { $1 }
720                 | akind '->' kind       { mkArrowKind $1 $3 }
721
722 akind           :: { Kind }
723                 : VARSYM                { if $1 == SLIT("*") then
724                                                 boxedTypeKind
725                                           else if $1 == SLIT("?") then
726                                                 openTypeKind
727                                           else panic "ParseInterface: akind"
728                                         }
729                 | '(' kind ')'  { $2 }
730
731 --------------------------------------------------------------------------
732
733 id_info         :: { [HsIdInfo RdrName] }
734                 : id_info_item                  { [$1] }
735                 | id_info_item id_info          { $1 : $2 }
736
737 id_info_item    :: { HsIdInfo RdrName }
738                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
739                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
740                 | '__M'                         { HsCprInfo }
741                 | '__S'                         { HsStrictness (mkStrictnessInfo $1) }
742                 | '__C'                         { HsNoCafRefs }
743                 | '__P' qvar_name               { HsWorker $2 }
744
745 inline_prag     :: { InlinePragInfo }
746                 :  {- empty -}                  { NoInlinePragInfo }
747                 | '[' from_prag phase ']'       { IMustNotBeINLINEd $2 $3 }
748
749 from_prag       :: { Bool }
750                 : {- empty -}                   { True }
751                 | '!'                           { False }
752
753 phase           :: { Maybe Int }
754                 : {- empty -}                   { Nothing }
755                 | INTEGER                       { Just (fromInteger $1) }
756
757 -------------------------------------------------------
758 core_expr       :: { UfExpr RdrName }
759 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
760                 | 'case' core_expr 'of' var_name
761                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
762
763                 | 'let' '{' core_val_bndr '=' core_expr
764                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
765                 | '__letrec' '{' rec_binds '}'          
766                   'in' core_expr                        { UfLet (UfRec $3) $6 }
767
768                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
769
770                 | fexpr                                 { $1 }
771
772 fexpr   :: { UfExpr RdrName }
773 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
774         | scc core_aexpr                                { UfNote (UfSCC $1) $2  }
775         | '__inline_me' core_aexpr                      { UfNote UfInlineMe $2 }
776         | '__inline_call' core_aexpr                    { UfNote UfInlineCall $2 }
777         | '__coerce' atype core_aexpr                   { UfNote (UfCoerce $2) $3 }
778         | core_aexpr                                    { $1 }
779
780 core_arg        :: { UfExpr RdrName }
781                 : '@' atype                                     { UfType $2 }
782                 | core_aexpr                                    { $1 }
783
784 core_args       :: { [UfExpr RdrName] }
785                 :                                               { [] }
786                 | core_arg core_args                            { $1 : $2 }
787
788 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
789 core_aexpr      : qvar_name                                     { UfVar $1 }
790                 | qdata_name                                    { UfVar $1 }
791
792                 | core_lit               { UfLit $1 }
793                 | '(' core_expr ')'      { $2 }
794
795                 | '('  ')'               { UfTuple (mkHsTupCon dataName Boxed [])   [] }
796                 | '(' comma_exprs2 ')'   { UfTuple (mkHsTupCon dataName Boxed $2)   $2 }
797                 | '(#' comma_exprs0 '#)' { UfTuple (mkHsTupCon dataName Unboxed $2) $2 }
798
799                 | '{' '__ccall' ccall_string type '}'       
800                            { let
801                                  (is_dyn, is_casm, may_gc) = $2
802
803                                  target | is_dyn    = DynamicTarget (error "CCall dyn target bogus unique")
804                                         | otherwise = StaticTarget $3
805
806                                  ccall = CCall target is_casm may_gc cCallConv
807                              in
808                              UfCCall ccall $4
809                            }
810
811
812 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
813 comma_exprs0    : {- empty -}                   { [ ] }
814                 | core_expr                     { [ $1 ] }
815                 | comma_exprs2                  { $1 }
816
817 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
818 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
819                 | core_expr ',' comma_exprs2                    { $1 : $3 }
820
821 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
822                 :                                               { [] }
823                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
824
825 core_alts       :: { [UfAlt RdrName] }
826                 :                                               { [] }
827                 | core_alt ';' core_alts                        { $1 : $3 }
828
829 core_alt        :: { UfAlt RdrName }
830 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
831
832 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
833 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
834                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
835                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
836                 | '('  ')'                      { (UfTupleAlt (mkHsTupCon dataName Boxed []),   []) }
837                 | '(' comma_var_names1 ')'      { (UfTupleAlt (mkHsTupCon dataName Boxed $2),   $2) }
838                 | '(#' comma_var_names1 '#)'    { (UfTupleAlt (mkHsTupCon dataName Unboxed $2), $2) }
839                 | '__DEFAULT'                   { (UfDefault, []) }
840                 | '(' core_pat ')'              { $2 }
841
842 core_pat_names :: { [RdrName] }
843 core_pat_names :                                { [] }
844                 | core_pat_name core_pat_names  { $1 : $2 }
845
846 -- Tyvar names and variable names live in different name spaces
847 -- so they need to be signalled separately.  But we don't record 
848 -- types or kinds in a pattern; we work that out from the type 
849 -- of the case scrutinee
850 core_pat_name   :: { RdrName }
851 core_pat_name   : var_name                      { $1 }
852                 | '@' tv_name                   { $2 }
853         
854 comma_var_names1 :: { [RdrName] }       -- One or more
855 comma_var_names1 : var_name                                     { [$1] }
856                  | var_name ',' comma_var_names1                { $1 : $3 }
857
858 core_lit        :: { Literal }
859 core_lit        : integer                       { mkMachInt $1 }
860                 | CHAR                          { MachChar $1 }
861                 | STRING                        { MachStr $1 }
862                 | rational                      { MachDouble $1 }
863                 | '__word' integer              { mkMachWord $2 }
864                 | '__word64' integer            { mkMachWord64 $2 }
865                 | '__int64' integer             { mkMachInt64 $2 }
866                 | '__float' rational            { MachFloat $2 }
867                 | '__addr' integer              { MachAddr $2 }
868                 | '__label' STRING              { MachLabel $2 }
869
870 integer         :: { Integer }
871                 : INTEGER                       { $1 }
872                 | '-' INTEGER                   { (-$2) }
873
874 rational        :: { Rational }
875                 : RATIONAL                      { $1 }
876                 | '-' RATIONAL                  { (-$2) }
877
878 core_bndr       :: { UfBinder RdrName }
879 core_bndr       : core_val_bndr                                 { $1 }
880                 | core_tv_bndr                                  { $1 }
881
882 core_bndrs      :: { [UfBinder RdrName] }
883 core_bndrs      :                                               { [] }
884                 | core_bndr core_bndrs                          { $1 : $2 }
885
886 core_val_bndr   :: { UfBinder RdrName }
887 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
888
889 core_tv_bndr    :: { UfBinder RdrName }
890 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
891                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
892
893 ccall_string    :: { FAST_STRING }
894                 : STRING                                        { $1 }
895                 | CLITLIT                                       { $1 }
896                 | VARID                                         { $1 }
897                 | CONID                                         { $1 }
898
899 ------------------------------------------------------------------------
900 scc     :: { CostCentre }
901         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
902         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
903                              { NormalCC { cc_name = $3, cc_mod = $4,
904                                           cc_is_dupd = $5, cc_is_caf = $6 } }
905
906 cc_name :: { EncodedFS }
907         : CONID                 { $1 }
908         | var_fs                { $1 }
909   
910 cc_dup  :: { IsDupdCC }
911 cc_dup  :                       { OriginalCC }
912         | '!'                   { DupdCC }
913
914 cc_caf  :: { IsCafCC }
915         :                       { NotCafCC }
916         | '__C'                 { CafCC }
917
918 -------------------------------------------------------------------
919
920 src_loc :: { SrcLoc }
921 src_loc :                               {% getSrcLocP }
922
923 -- Check the project version: this makes sure
924 -- that the project version (e.g. 407) in the interface
925 -- file is the same as that for the compiler that's reading it
926 checkVersion :: { () }
927            : {-empty-}                  {% checkVersion Nothing }
928            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
929
930 ------------------------------------------------------------------- 
931
932 --                      Haskell code 
933 {
934 happyError :: P a
935 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
936
937 data IfaceStuff = PIface           ParsedIface
938                 | PIdInfo          [HsIdInfo RdrName]
939                 | PType            RdrNameHsType
940                 | PRulesAndDeprecs ([RdrNameRuleDecl], IfaceDeprecs)
941
942 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
943 }