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