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