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