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