[project @ 2000-10-27 16:43:24 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, mkSysQual, mkSysUnqual )
57 import Name             ( OccName )
58 import OccName          ( mkSysOccFS,
59                           tcName, varName, ipName, dataName, clsName, tvName, uvName,
60                           EncodedFS 
61                         )
62 import Module           ( ModuleName, PackageName, mkSysModuleNameFS, mkModule )
63 import SrcLoc           ( SrcLoc )
64 import CmdLineOpts      ( opt_InPackage, 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              { mkRdrUnqual $1 }
610
611 qvar_name       :: { RdrName }
612 qvar_name       :  var_name             { $1 }
613                 |  qvar_fs              { mkSysQual varName $1 }
614
615 ipvar_name      :: { RdrName }
616                 :  IPVARID              { mkSysUnqual 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             { mkRdrUnqual $1 }
644
645 qdata_name      :: { RdrName }
646 qdata_name      :  data_name            { $1 }
647                 |  qdata_fs             { mkSysQual dataName $1 }
648                                 
649 var_or_data_name :: { RdrName }
650                   : var_name                    { $1 }
651                   | data_name                   { $1 }
652
653 ---------------------------------------------------
654 tc_fs           :: { EncodedFS }
655                 :  data_fs              { $1 }
656
657 tc_occ          :: { OccName }
658                 :  tc_fs                { mkSysOccFS tcName $1 }
659
660 tc_name         :: { RdrName }
661                 :  tc_occ               { mkRdrUnqual $1 }
662
663 qtc_name        :: { RdrName }
664                 : tc_name               { $1 }
665                 | qdata_fs              { mkSysQual tcName $1 }
666
667 ---------------------------------------------------
668 cls_name        :: { RdrName }
669                 :  data_fs              { mkSysUnqual clsName $1 }
670
671 qcls_name       :: { RdrName }
672                 : cls_name              { $1 }
673                 | qdata_fs              { mkSysQual clsName $1 }
674
675 ---------------------------------------------------
676 uv_name         :: { RdrName }
677                 :  VARID                { mkSysUnqual uvName $1 }
678
679 uv_bndr         :: { RdrName }
680                 :  uv_name              { $1 }
681
682 uv_bndrs        :: { [RdrName] }
683                 :                       { [] }
684                 | uv_bndr uv_bndrs      { $1 : $2 }
685
686 ---------------------------------------------------
687 tv_name         :: { RdrName }
688                 :  VARID                { mkSysUnqual tvName $1 }
689                 |  VARSYM               { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
690
691 tv_bndr         :: { HsTyVarBndr RdrName }
692                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
693                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
694
695 tv_bndrs        :: { [HsTyVarBndr RdrName] }
696 tv_bndrs        : tv_bndrs1             { $1 }
697                 | '[' tv_bndrs1 ']'     { $2 }  -- Backward compatibility
698
699 tv_bndrs1       :: { [HsTyVarBndr RdrName] }
700                 :                       { [] }
701                 | tv_bndr tv_bndrs1     { $1 : $2 }
702
703 ---------------------------------------------------
704 fds :: { [([RdrName], [RdrName])] }
705         : {- empty -}                   { [] }
706         | '|' fds1                      { reverse $2 }
707
708 fds1 :: { [([RdrName], [RdrName])] }
709         : fds1 ',' fd                   { $3 : $1 }
710         | fd                            { [$1] }
711
712 fd :: { ([RdrName], [RdrName]) }
713         : varids0 '->' varids0          { (reverse $1, reverse $3) }
714
715 varids0 :: { [RdrName] }
716         : {- empty -}                   { [] }
717         | varids0 tv_name               { $2 : $1 }
718
719 ---------------------------------------------------
720 kind            :: { Kind }
721                 : akind                 { $1 }
722                 | akind '->' kind       { mkArrowKind $1 $3 }
723
724 akind           :: { Kind }
725                 : VARSYM                { if $1 == SLIT("*") then
726                                                 boxedTypeKind
727                                           else if $1 == SLIT("?") then
728                                                 openTypeKind
729                                           else panic "ParseInterface: akind"
730                                         }
731                 | '(' kind ')'  { $2 }
732
733 --------------------------------------------------------------------------
734
735 id_info         :: { [HsIdInfo RdrName] }
736                 : id_info_item                  { [$1] }
737                 | id_info_item id_info          { $1 : $2 }
738
739 id_info_item    :: { HsIdInfo RdrName }
740                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
741                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
742                 | '__M'                         { HsCprInfo }
743                 | '__S'                         { HsStrictness (mkStrictnessInfo $1) }
744                 | '__C'                         { HsNoCafRefs }
745                 | '__P' qvar_name               { HsWorker $2 }
746
747 inline_prag     :: { InlinePragInfo }
748                 :  {- empty -}                  { NoInlinePragInfo }
749                 | '[' from_prag phase ']'       { IMustNotBeINLINEd $2 $3 }
750
751 from_prag       :: { Bool }
752                 : {- empty -}                   { True }
753                 | '!'                           { False }
754
755 phase           :: { Maybe Int }
756                 : {- empty -}                   { Nothing }
757                 | INTEGER                       { Just (fromInteger $1) }
758
759 -------------------------------------------------------
760 core_expr       :: { UfExpr RdrName }
761 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
762                 | 'case' core_expr 'of' var_name
763                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
764
765                 | 'let' '{' core_val_bndr '=' core_expr
766                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
767                 | '__letrec' '{' rec_binds '}'          
768                   'in' core_expr                        { UfLet (UfRec $3) $6 }
769
770                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
771
772                 | fexpr                                 { $1 }
773
774 fexpr   :: { UfExpr RdrName }
775 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
776         | scc core_aexpr                                { UfNote (UfSCC $1) $2  }
777         | '__inline_me' core_aexpr                      { UfNote UfInlineMe $2 }
778         | '__inline_call' core_aexpr                    { UfNote UfInlineCall $2 }
779         | '__coerce' atype core_aexpr                   { UfNote (UfCoerce $2) $3 }
780         | core_aexpr                                    { $1 }
781
782 core_arg        :: { UfExpr RdrName }
783                 : '@' atype                                     { UfType $2 }
784                 | core_aexpr                                    { $1 }
785
786 core_args       :: { [UfExpr RdrName] }
787                 :                                               { [] }
788                 | core_arg core_args                            { $1 : $2 }
789
790 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
791 core_aexpr      : qvar_name                                     { UfVar $1 }
792                 | qdata_name                                    { UfVar $1 }
793
794                 | core_lit               { UfLit $1 }
795                 | '(' core_expr ')'      { $2 }
796
797                 | '('  ')'               { UfTuple (mkHsTupCon dataName Boxed [])   [] }
798                 | '(' comma_exprs2 ')'   { UfTuple (mkHsTupCon dataName Boxed $2)   $2 }
799                 | '(#' comma_exprs0 '#)' { UfTuple (mkHsTupCon dataName Unboxed $2) $2 }
800
801                 | '{' '__ccall' ccall_string type '}'       
802                            { let
803                                  (is_dyn, is_casm, may_gc) = $2
804
805                                  target | is_dyn    = DynamicTarget (error "CCall dyn target bogus unique")
806                                         | otherwise = StaticTarget $3
807
808                                  ccall = CCall target is_casm may_gc cCallConv
809                              in
810                              UfCCall ccall $4
811                            }
812
813
814 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
815 comma_exprs0    : {- empty -}                   { [ ] }
816                 | core_expr                     { [ $1 ] }
817                 | comma_exprs2                  { $1 }
818
819 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
820 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
821                 | core_expr ',' comma_exprs2                    { $1 : $3 }
822
823 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
824                 :                                               { [] }
825                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
826
827 core_alts       :: { [UfAlt RdrName] }
828                 :                                               { [] }
829                 | core_alt ';' core_alts                        { $1 : $3 }
830
831 core_alt        :: { UfAlt RdrName }
832 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
833
834 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
835 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
836                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
837                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
838                 | '('  ')'                      { (UfTupleAlt (mkHsTupCon dataName Boxed []),   []) }
839                 | '(' comma_var_names1 ')'      { (UfTupleAlt (mkHsTupCon dataName Boxed $2),   $2) }
840                 | '(#' comma_var_names1 '#)'    { (UfTupleAlt (mkHsTupCon dataName Unboxed $2), $2) }
841                 | '__DEFAULT'                   { (UfDefault, []) }
842                 | '(' core_pat ')'              { $2 }
843
844 core_pat_names :: { [RdrName] }
845 core_pat_names :                                { [] }
846                 | core_pat_name core_pat_names  { $1 : $2 }
847
848 -- Tyvar names and variable names live in different name spaces
849 -- so they need to be signalled separately.  But we don't record 
850 -- types or kinds in a pattern; we work that out from the type 
851 -- of the case scrutinee
852 core_pat_name   :: { RdrName }
853 core_pat_name   : var_name                      { $1 }
854                 | '@' tv_name                   { $2 }
855         
856 comma_var_names1 :: { [RdrName] }       -- One or more
857 comma_var_names1 : var_name                                     { [$1] }
858                  | var_name ',' comma_var_names1                { $1 : $3 }
859
860 core_lit        :: { Literal }
861 core_lit        : integer                       { mkMachInt $1 }
862                 | CHAR                          { MachChar $1 }
863                 | STRING                        { MachStr $1 }
864                 | rational                      { MachDouble $1 }
865                 | '__word' integer              { mkMachWord $2 }
866                 | '__word64' integer            { mkMachWord64 $2 }
867                 | '__int64' integer             { mkMachInt64 $2 }
868                 | '__float' rational            { MachFloat $2 }
869                 | '__addr' integer              { MachAddr $2 }
870                 | '__label' STRING              { MachLabel $2 }
871
872 integer         :: { Integer }
873                 : INTEGER                       { $1 }
874                 | '-' INTEGER                   { (-$2) }
875
876 rational        :: { Rational }
877                 : RATIONAL                      { $1 }
878                 | '-' RATIONAL                  { (-$2) }
879
880 core_bndr       :: { UfBinder RdrName }
881 core_bndr       : core_val_bndr                                 { $1 }
882                 | core_tv_bndr                                  { $1 }
883
884 core_bndrs      :: { [UfBinder RdrName] }
885 core_bndrs      :                                               { [] }
886                 | core_bndr core_bndrs                          { $1 : $2 }
887
888 core_val_bndr   :: { UfBinder RdrName }
889 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
890
891 core_tv_bndr    :: { UfBinder RdrName }
892 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
893                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
894
895 ccall_string    :: { FAST_STRING }
896                 : STRING                                        { $1 }
897                 | CLITLIT                                       { $1 }
898                 | VARID                                         { $1 }
899                 | CONID                                         { $1 }
900
901 ------------------------------------------------------------------------
902 scc     :: { CostCentre }
903         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
904         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
905                              { NormalCC { cc_name = $3, cc_mod = $4,
906                                           cc_is_dupd = $5, cc_is_caf = $6 } }
907
908 cc_name :: { EncodedFS }
909         : CONID                 { $1 }
910         | var_fs                { $1 }
911   
912 cc_dup  :: { IsDupdCC }
913 cc_dup  :                       { OriginalCC }
914         | '!'                   { DupdCC }
915
916 cc_caf  :: { IsCafCC }
917         :                       { NotCafCC }
918         | '__C'                 { CafCC }
919
920 -------------------------------------------------------------------
921
922 src_loc :: { SrcLoc }
923 src_loc :                               {% getSrcLocP }
924
925 -- Check the project version: this makes sure
926 -- that the project version (e.g. 407) in the interface
927 -- file is the same as that for the compiler that's reading it
928 checkVersion :: { () }
929            : {-empty-}                  {% checkVersion Nothing }
930            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
931
932 ------------------------------------------------------------------- 
933
934 --                      Haskell code 
935 {
936 happyError :: P a
937 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
938
939 data IfaceStuff = PIface           ParsedIface
940                 | PIdInfo          [HsIdInfo RdrName]
941                 | PType            RdrNameHsType
942                 | PRulesAndDeprecs ([RdrNameRuleDecl], IfaceDeprecs)
943
944 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
945 }