[project @ 2002-01-29 09:58:14 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 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  '~'            { ITtilde }
159  '=>'           { ITdarrow }
160  '-'            { ITminus }
161  '!'            { ITbang }
162
163  '{'            { ITocurly }                    -- special symbols
164  '}'            { ITccurly }
165  '{|'           { ITocurlybar }                         -- special symbols
166  '|}'           { ITccurlybar }                         -- special symbols
167  '['            { ITobrack }
168  ']'            { ITcbrack }
169  '('            { IToparen }
170  ')'            { ITcparen }
171  '(#'           { IToubxparen }
172  '#)'           { ITcubxparen }
173  ';'            { ITsemi }
174  ','            { ITcomma }
175  '.'            { ITdot }
176
177  VARID          { ITvarid    $$ }               -- identifiers
178  CONID          { ITconid    $$ }
179  VARSYM         { ITvarsym   $$ }
180  CONSYM         { ITconsym   $$ }
181  QVARID         { ITqvarid   $$ }
182  QCONID         { ITqconid   $$ }
183  QVARSYM        { ITqvarsym  $$ }
184  QCONSYM        { ITqconsym  $$ }
185
186  IPDUPVARID     { ITdupipvarid   $$ }           -- GHC extension
187  IPSPLITVARID   { ITsplitipvarid $$ }           -- GHC extension
188
189  PRAGMA         { ITpragma   $$ }
190
191  CHAR           { ITchar     $$ }
192  STRING         { ITstring   $$ }
193  INTEGER        { ITinteger  $$ }
194  RATIONAL       { ITrational $$ }
195  CLITLIT        { ITlitlit   $$ }
196
197  UNKNOWN        { ITunknown  $$ }
198 %%
199
200 iface           :: { ParsedIface }
201 iface           : '__interface' package mod_name 
202                         version sub_versions
203                         orphans checkVersion 'where'
204                   exports_part
205                   import_part
206                   fix_decl_part
207                   instance_decl_part
208                   decls_part
209                   rules_and_deprecs_part
210                   { let (rules,deprecs) = $14 () in
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,rules),    -- Rules 
221                         pi_deprecs = deprecs            -- 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 'foreign' 'type' qtc_name                    
356                         { ForeignType $4 Nothing DNType $1 }
357         | src_loc 'data' opt_decl_context qtc_name tv_bndrs constrs            
358                         { mkTyData DataType $3 $4 $5 $6 (length $6) Nothing $1 }
359         | src_loc 'newtype' opt_decl_context qtc_name tv_bndrs newtype_constr
360                         { mkTyData NewType $3 $4 $5 $6 1 Nothing $1 }
361         | src_loc 'class' opt_decl_context qtc_name tv_bndrs fds csigs
362                         { mkClassDecl $3 $4 $5 $6 $7 Nothing $1 }
363
364 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
365 maybe_idinfo  : {- empty -}     { \_ -> [] }
366               | pragma          { \x -> if opt_IgnoreIfacePragmas then [] 
367                                         else case $1 of
368                                                 Just (POk _ id_info) -> id_info
369                                                 Just (PFailed err) -> pprPanic "IdInfo parse failed" 
370                                                                         (vcat [ppr x, err])
371                                 }
372     {-
373       If a signature decl is being loaded, and opt_IgnoreIfacePragmas is on,
374       we toss away unfolding information.
375
376       Also, if the signature is loaded from a module we're importing from source,
377       we do the same. This is to avoid situations when compiling a pair of mutually
378       recursive modules, peering at unfolding info in the interface file of the other, 
379       e.g., you compile A, it looks at B's interface file and may as a result change
380       its interface file. Hence, B is recompiled, maybe changing its interface file,
381       which will the unfolding info used in A to become invalid. Simple way out is to
382       just ignore unfolding info.
383
384       [Jan 99: I junked the second test above.  If we're importing from an hi-boot
385        file there isn't going to *be* any pragma info.  The above comment
386        dates from a time where we picked up a .hi file first if it existed.]
387     -}
388
389 pragma  :: { Maybe (ParseResult [HsIdInfo RdrName]) }
390 pragma  : src_loc PRAGMA        { Just (parseIdInfo $2 PState{ bol = 0#, atbol = 1#,
391                                                         context = [],
392                                                         glasgow_exts = 1#,
393                                                         loc = $1 })
394                                 }
395
396 -----------------------------------------------------------------------------
397
398 -- This production is lifted so that it doesn't get eagerly parsed when we
399 -- use happy --strict.
400 rules_and_deprecs_part :: { () -> ([RdrNameRuleDecl], IfaceDeprecs) }
401 rules_and_deprecs_part
402   : {- empty -}         { \_ -> ([], Nothing) }
403   | src_loc PRAGMA      { \_ -> case parseRules $2 PState{ bol = 0#, atbol = 1#,
404                                                            context = [],
405                                                            glasgow_exts = 1#,
406                                                            loc = $1 } of
407                                         POk _ rds   -> rds
408                                         PFailed err -> pprPanic "Rules/Deprecations parse failed" err
409                         }
410
411 rules_and_deprecs :: { ([RdrNameRuleDecl], IfaceDeprecs) }
412 rules_and_deprecs : rule_prag deprec_prag       { ($1, $2) }
413
414
415 -----------------------------------------------------------------------------
416
417 rule_prag :: { [RdrNameRuleDecl] }
418 rule_prag : {- empty -}                 { [] }
419           | '__R' rules                 { $2 }
420
421 rules      :: { [RdrNameRuleDecl] }
422            : {- empty -}        { [] }
423            | rule ';' rules     { $1:$3 }
424
425 rule       :: { RdrNameRuleDecl }
426 rule       : src_loc STRING activation rule_forall qvar_name 
427              core_args '=' core_expr    { IfaceRule $2 $3 $4 $5 $6 $8 $1 } 
428
429 activation :: { Activation }
430 activation : {- empty -}                { AlwaysActive }
431            | '[' INTEGER ']'            { ActiveAfter (fromInteger $2) }
432            | '[' '~' INTEGER ']'        { ActiveBefore (fromInteger $3) }
433
434 rule_forall     :: { [UfBinder RdrName] }
435 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
436                   
437 -----------------------------------------------------------------------------
438
439 deprec_prag     :: { IfaceDeprecs }
440 deprec_prag     : {- empty -}           { Nothing }
441                 | '__D' deprecs         { Just $2 } 
442
443 deprecs         :: { Either DeprecTxt [(RdrName,DeprecTxt)] }
444 deprecs         : STRING                { Left $1 }
445                 | deprec_list           { Right $1 }
446
447 deprec_list     :: { [(RdrName,DeprecTxt)] }
448 deprec_list     : deprec                        { [$1] }
449                 | deprec ';' deprec_list        { $1 : $3 }
450
451 deprec          :: { (RdrName,DeprecTxt) }
452 deprec          : deprec_name STRING    { ($1, $2) }
453
454 deprec_name     :: { RdrName }
455                 : qvar_name             { $1 }
456                 | qtc_name              { $1 }
457
458 -----------------------------------------------------------------------------
459
460 version         :: { Version }
461 version         :  INTEGER                      { fromInteger $1 }
462
463 opt_version     :: { Version }
464 opt_version     : version                       { $1 }
465                 | {- empty -}                   { initialVersion }
466         
467 opt_decl_context  :: { RdrNameContext }
468 opt_decl_context  :                             { [] }
469                   | context '=>'                { $1 }
470
471 ----------------------------------------------------------------------------
472
473 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
474                 :                       { [] }
475                 | '=' constrs1          { $2 }
476
477 constrs1        :: { [RdrNameConDecl] }
478 constrs1        :  constr               { [$1] }
479                 |  constr '|' constrs1  { $1 : $3 }
480
481 constr          :: { RdrNameConDecl }
482 constr          :  src_loc ex_stuff qdata_name batypes          { mk_con_decl $3 $2 (VanillaCon $4) $1 }
483                 |  src_loc ex_stuff qdata_name '{' fields1 '}'  { mk_con_decl $3 $2 (RecCon $5)     $1 }
484                 -- We use "data_fs" so as to include ()
485
486 newtype_constr  :: { [RdrNameConDecl] {- Not allowed to be empty -} }
487 newtype_constr  : src_loc '=' ex_stuff qdata_name atype { [mk_con_decl $4 $3 (VanillaCon [unbangedType $5]) $1] }
488                 | src_loc '=' ex_stuff qdata_name '{' qvar_name '::' atype '}'
489                                                         { [mk_con_decl $4 $3 (RecCon [([$6], unbangedType $8)]) $1] }
490
491 ex_stuff :: { ([HsTyVarBndr RdrName], RdrNameContext) }
492 ex_stuff        :                                       { ([],[]) }
493                 | '__forall' tv_bndrs opt_context '=>'  { ($2,$3) }
494
495 batypes         :: { [RdrNameBangType] }
496 batypes         :                                       { [] }
497                 |  batype batypes                       { $1 : $2 }
498
499 batype          :: { RdrNameBangType }
500 batype          :  tatype                               { unbangedType $1 }
501                 |  '!' tatype                           { BangType MarkedStrict    $2 }
502                 |  '!' '!' tatype                       { BangType MarkedUnboxed   $3 }
503
504 fields1         :: { [([RdrName], RdrNameBangType)] }
505 fields1         : field                                 { [$1] }
506                 | field ',' fields1                     { $1 : $3 }
507
508 field           :: { ([RdrName], RdrNameBangType) }
509 field           :  qvar_names1 '::' ttype               { ($1, unbangedType $3) }
510                 |  qvar_names1 '::' '!' ttype           { ($1, BangType MarkedStrict    $4) }
511                 |  qvar_names1 '::' '!' '!' ttype       { ($1, BangType MarkedUnboxed   $5) }
512
513 --------------------------------------------------------------------------
514
515 type            :: { RdrNameHsType }
516 type            : '__forall' tv_bndrs 
517                         opt_context '=>' type   { mkHsForAllTy (Just $2) $3 $5 }
518                 | btype '->' type               { HsFunTy $1 $3 }
519                 | btype                         { $1 }
520
521 opt_context     :: { RdrNameContext }
522 opt_context     :                                       { [] }
523                 | context                               { $1 }
524
525 context         :: { RdrNameContext }
526 context         : '(' context_list1 ')'                 { $2 }
527                 | '{' context_list1 '}'                 { $2 }  -- Backward compatibility
528
529 context_list1   :: { RdrNameContext }
530 context_list1   : class                                 { [$1] }
531                 | class ',' context_list1               { $1 : $3 }
532
533 class           :: { HsPred RdrName }
534 class           :  qcls_name atypes                     { (HsClassP $1 $2) }
535                 |  ipvar_name '::' type                 { (HsIParam $1 $3) }
536
537 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
538 types0          :  {- empty -}                          { [ ] }
539                 |  type                                 { [ $1 ] }
540                 |  types2                               { $1 }
541
542 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
543 types2          :  type ',' type                        { [$1,$3] }
544                 |  type ',' types2                      { $1 : $3 }
545
546 btype           :: { RdrNameHsType }
547 btype           :  atype                                { $1 }
548                 |  btype atype                          { HsAppTy $1 $2 }
549                 |  '__u' atype atype                    { HsUsageTy $2 $3 }
550
551 atype           :: { RdrNameHsType }
552 atype           :  qtc_name                             { HsTyVar $1 }
553                 |  tv_name                              { HsTyVar $1 }
554                 |  '.'                                  { hsUsOnce }
555                 |  '!'                                  { hsUsMany }
556                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
557                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
558                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
559                 |  '[' type ']'                         { HsListTy  $2 }
560                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
561                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
562                 |  '(' type ')'                         { $2 }
563
564 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
565 atypes          :                                       { [] }
566                 |  atype atypes                         { $1 : $2 }
567 --------------------------------------------------------------------------
568
569 -- versions of type/btype/atype that cant begin with '!' (or '.')
570 -- for use where the kind is definitely known NOT to be '$'
571
572 ttype           :: { RdrNameHsType }
573 ttype           : '__forall' tv_bndrs 
574                         opt_context '=>' type           { mkHsForAllTy (Just $2) $3 $5 }
575                 | tbtype '->' type                      { HsFunTy $1 $3 }
576                 | tbtype                                { $1 }
577
578 tbtype          :: { RdrNameHsType }
579 tbtype          :  tatype                               { $1 }
580                 |  tbtype atype                         { HsAppTy $1 $2 }
581                 |  '__u' atype atype                    { HsUsageTy $2 $3 }
582
583 tatype          :: { RdrNameHsType }
584 tatype          :  qtc_name                             { HsTyVar $1 }
585                 |  tv_name                              { HsTyVar $1 }
586                 |  '(' ')'                              { HsTupleTy (mkHsTupCon tcName Boxed   []) [] }
587                 |  '(' types2 ')'                       { HsTupleTy (mkHsTupCon tcName Boxed   $2) $2 }
588                 |  '(#' types0 '#)'                     { HsTupleTy (mkHsTupCon tcName Unboxed $2) $2 }
589                 |  '[' type ']'                         { HsListTy  $2 }
590                 |  '{' qcls_name atypes '}'             { mkHsDictTy $2 $3 }
591                 |  '{' ipvar_name '::' type '}'         { mkHsIParamTy $2 $4 }
592                 |  '(' type ')'                         { $2 }
593 ---------------------------------------------------------------------
594
595 package         :: { PackageName }
596                 :  STRING               { $1 }
597                 | {- empty -}           { opt_InPackage }
598                                 -- Useful for .hi-boot files,
599                                 -- which can omit the package Id
600                                 -- Module loops are always within a package
601
602 mod_name        :: { ModuleName }
603                 :  CONID                { mkSysModuleNameFS $1 }
604
605
606 ---------------------------------------------------
607 var_fs          :: { EncodedFS }
608                 : VARID                 { $1 }
609                 | 'as'                  { SLIT("as") }
610                 | 'qualified'           { SLIT("qualified") }
611                 | 'hiding'              { SLIT("hiding") }
612                 | 'forall'              { SLIT("forall") }
613                 | 'foreign'             { SLIT("foreign") }
614                 | 'export'              { SLIT("export") }
615                 | 'label'               { SLIT("label") }
616                 | 'dynamic'             { SLIT("dynamic") }
617                 | 'unsafe'              { SLIT("unsafe") }
618                 | 'with'                { SLIT("with") }
619                 | 'ccall'               { SLIT("ccall") }
620                 | 'stdcall'             { SLIT("stdcall") }
621
622 var_occ         :: { OccName }
623                 :  var_fs               { mkSysOccFS varName $1 }
624
625 var_name        :: { RdrName }
626 var_name        :  var_occ              { mkRdrUnqual $1 }
627
628 qvar_name       :: { RdrName }
629 qvar_name       :  var_name             { $1 }
630                 |  QVARID               { mkIfaceOrig varName $1 }
631
632 ipvar_name      :: { IPName RdrName }
633                 : IPDUPVARID            { Dupable (mkRdrUnqual (mkSysOccFS varName $1)) }
634                 | IPSPLITVARID          { Linear  (mkRdrUnqual (mkSysOccFS varName $1)) }
635
636 qvar_names1     :: { [RdrName] }
637 qvar_names1     : qvar_name             { [$1] }
638                 | qvar_name qvar_names1 { $1 : $2 }
639
640 var_names       :: { [RdrName] }
641 var_names       :                       { [] }
642                 | var_name var_names    { $1 : $2 }
643
644 var_names1      :: { [RdrName] }
645 var_names1      : var_name var_names    { $1 : $2 }
646
647 ---------------------------------------------------
648
649 data_occ        :: { OccName }
650                 :  CONID                { mkSysOccFS dataName $1 }
651
652 qdata_name      :: { RdrName }
653                 :  data_occ             { mkRdrUnqual $1 }
654                 |  QCONID               { mkIfaceOrig dataName $1 }
655                                 
656 var_or_data_name :: { RdrName }
657                   : qvar_name           { $1 }
658                   | qdata_name          { $1 }
659
660 ---------------------------------------------------
661 tc_occ          :: { OccName }
662                 :  CONID                { mkSysOccFS tcName $1 }
663
664 qtc_name        :: { RdrName }
665                 : tc_occ                { mkRdrUnqual $1 }
666                 | QCONID                { mkIfaceOrig tcName $1 }
667
668 ---------------------------------------------------
669 qcls_name       :: { RdrName }
670                 : CONID                 { mkRdrUnqual (mkSysOccFS clsName $1) }
671                 | QCONID                { mkIfaceOrig clsName $1 }
672
673 ---------------------------------------------------
674 tv_name         :: { RdrName }
675                 :  var_fs               { mkRdrUnqual (mkSysOccFS tvName $1) }
676
677 tv_bndr         :: { HsTyVarBndr RdrName }
678                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
679                 |  tv_name              { IfaceTyVar $1 liftedTypeKind }
680
681 tv_bndrs        :: { [HsTyVarBndr RdrName] }
682                 : tv_bndrs1             { $1 }
683                 | '[' tv_bndrs1 ']'     { $2 }  -- Backward compatibility
684
685 tv_bndrs1       :: { [HsTyVarBndr RdrName] }
686                 :                       { [] }
687                 | tv_bndr tv_bndrs1     { $1 : $2 }
688
689 ---------------------------------------------------
690 fds :: { [([RdrName], [RdrName])] }
691         : {- empty -}                   { [] }
692         | '|' fds1                      { reverse $2 }
693
694 fds1 :: { [([RdrName], [RdrName])] }
695         : fds1 ',' fd                   { $3 : $1 }
696         | fd                            { [$1] }
697
698 fd :: { ([RdrName], [RdrName]) }
699         : varids0 '->' varids0          { (reverse $1, reverse $3) }
700
701 varids0 :: { [RdrName] }
702         : {- empty -}                   { [] }
703         | varids0 tv_name               { $2 : $1 }
704
705 ---------------------------------------------------
706 kind            :: { Kind }
707                 : akind                 { $1 }
708                 | akind '->' kind       { mkArrowKind $1 $3 }
709
710 akind           :: { Kind }
711                 : VARSYM                { if $1 == SLIT("*") then
712                                                 liftedTypeKind
713                                           else if $1 == SLIT("?") then
714                                                 openTypeKind
715                                           else if $1 == SLIT("\36") then
716                                                 usageTypeKind  -- dollar
717                                           else panic "ParseInterface: akind"
718                                         }
719                 | '(' kind ')'  { $2 }
720
721 --------------------------------------------------------------------------
722
723 id_info         :: { [HsIdInfo RdrName] }
724                 : id_info_item                  { [$1] }
725                 | id_info_item id_info          { $1 : $2 }
726
727 id_info_item    :: { HsIdInfo RdrName }
728                 : '__A' INTEGER                 { HsArity (fromInteger $2) }
729                 | '__U' activation core_expr    { HsUnfold $2 $3 }
730                 | '__S'                         { HsStrictness $1 }
731                 | '__C'                         { HsNoCafRefs }
732                 | '__P' qvar_name INTEGER       { HsWorker $2 (fromInteger $3) }
733
734 -------------------------------------------------------
735 core_expr       :: { UfExpr RdrName }
736 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
737                 | 'case' core_expr 'of' var_name
738                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
739
740                 | 'let' '{' core_val_bndr '=' core_expr
741                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
742                 | '__letrec' '{' rec_binds '}'          
743                   'in' core_expr                        { UfLet (UfRec $3) $6 }
744
745                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
746
747                 | fexpr                                 { $1 }
748
749 fexpr   :: { UfExpr RdrName }
750 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
751         | scc core_aexpr                                { UfNote (UfSCC $1) $2  }
752         | '__inline_me' core_aexpr                      { UfNote UfInlineMe $2 }
753         | '__inline_call' core_aexpr                    { UfNote UfInlineCall $2 }
754         | '__coerce' atype core_aexpr                   { UfNote (UfCoerce $2) $3 }
755         | core_aexpr                                    { $1 }
756
757 core_arg        :: { UfExpr RdrName }
758                 : '@' atype                                     { UfType $2 }
759                 | core_aexpr                                    { $1 }
760
761 core_args       :: { [UfExpr RdrName] }
762                 :                                               { [] }
763                 | core_arg core_args                            { $1 : $2 }
764
765 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
766 core_aexpr      : qvar_name                                     { UfVar $1 }
767                 | qdata_name                                    { UfVar $1 }
768
769                 | core_lit               { UfLit $1 }
770                 | '(' core_expr ')'      { $2 }
771
772                 | '('  ')'               { UfTuple (mkHsTupCon dataName Boxed [])   [] }
773                 | '(' comma_exprs2 ')'   { UfTuple (mkHsTupCon dataName Boxed $2)   $2 }
774                 | '(#' comma_exprs0 '#)' { UfTuple (mkHsTupCon dataName Unboxed $2) $2 }
775
776                 | '{' '__ccall' ccall_string type '}'       
777                            { let
778                                  (is_dyn, is_casm, may_gc) = $2
779
780                                  target | is_dyn    = DynamicTarget
781                                         | is_casm   = CasmTarget $3
782                                         | otherwise = StaticTarget $3
783
784                                  ccall = CCallSpec target CCallConv may_gc
785                              in
786                              UfFCall (CCall ccall) $4
787                            }
788
789
790 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
791 comma_exprs0    : {- empty -}                   { [ ] }
792                 | core_expr                     { [ $1 ] }
793                 | comma_exprs2                  { $1 }
794
795 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
796 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
797                 | core_expr ',' comma_exprs2                    { $1 : $3 }
798
799 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
800                 :                                               { [] }
801                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
802
803 core_alts       :: { [UfAlt RdrName] }
804                 :                                               { [] }
805                 | core_alt ';' core_alts                        { $1 : $3 }
806
807 core_alt        :: { UfAlt RdrName }
808 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
809
810 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
811 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
812                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
813                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
814                 | '('  ')'                      { (UfTupleAlt (mkHsTupCon dataName Boxed []),   []) }
815                 | '(' comma_var_names1 ')'      { (UfTupleAlt (mkHsTupCon dataName Boxed $2),   $2) }
816                 | '(#' comma_var_names1 '#)'    { (UfTupleAlt (mkHsTupCon dataName Unboxed $2), $2) }
817                 | '__DEFAULT'                   { (UfDefault, []) }
818                 | '(' core_pat ')'              { $2 }
819
820 core_pat_names :: { [RdrName] }
821 core_pat_names :                                { [] }
822                 | core_pat_name core_pat_names  { $1 : $2 }
823
824 -- Tyvar names and variable names live in different name spaces
825 -- so they need to be signalled separately.  But we don't record 
826 -- types or kinds in a pattern; we work that out from the type 
827 -- of the case scrutinee
828 core_pat_name   :: { RdrName }
829 core_pat_name   : var_name                      { $1 }
830                 | '@' tv_name                   { $2 }
831         
832 comma_var_names1 :: { [RdrName] }       -- One or more
833 comma_var_names1 : var_name                                     { [$1] }
834                  | var_name ',' comma_var_names1                { $1 : $3 }
835
836 core_lit        :: { Literal }
837 core_lit        : integer                       { mkMachInt $1 }
838                 | CHAR                          { MachChar $1 }
839                 | STRING                        { MachStr $1 }
840                 | rational                      { MachDouble $1 }
841                 | '__word' integer              { mkMachWord $2 }
842                 | '__word64' integer            { mkMachWord64 $2 }
843                 | '__int64' integer             { mkMachInt64 $2 }
844                 | '__float' rational            { MachFloat $2 }
845                 | '__addr' integer              { MachAddr $2 }
846                 | '__label' STRING              { MachLabel $2 }
847
848 integer         :: { Integer }
849                 : INTEGER                       { $1 }
850                 | '-' INTEGER                   { (-$2) }
851
852 rational        :: { Rational }
853                 : RATIONAL                      { $1 }
854                 | '-' RATIONAL                  { (-$2) }
855
856 core_bndr       :: { UfBinder RdrName }
857 core_bndr       : core_val_bndr                                 { $1 }
858                 | core_tv_bndr                                  { $1 }
859
860 core_bndrs      :: { [UfBinder RdrName] }
861 core_bndrs      :                                               { [] }
862                 | core_bndr core_bndrs                          { $1 : $2 }
863
864 core_val_bndr   :: { UfBinder RdrName }
865 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
866
867 core_tv_bndr    :: { UfBinder RdrName }
868 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
869                 |  '@' tv_name                          { UfTyBinder $2 liftedTypeKind }
870
871 ccall_string    :: { FAST_STRING }
872                 : STRING                                        { $1 }
873                 | CLITLIT                                       { $1 }
874                 | VARID                                         { $1 }
875                 | CONID                                         { $1 }
876
877 ------------------------------------------------------------------------
878 scc     :: { CostCentre }
879         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
880         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
881                              { NormalCC { cc_name = $3, cc_mod = $4,
882                                           cc_is_dupd = $5, cc_is_caf = $6 } }
883
884 cc_name :: { EncodedFS }
885         : CONID                 { $1 }
886         | var_fs                { $1 }
887   
888 cc_dup  :: { IsDupdCC }
889 cc_dup  :                       { OriginalCC }
890         | '!'                   { DupdCC }
891
892 cc_caf  :: { IsCafCC }
893         :                       { NotCafCC }
894         | '__C'                 { CafCC }
895
896 -------------------------------------------------------------------
897
898 src_loc :: { SrcLoc }
899 src_loc :                               {% getSrcLocP }
900
901 -- Check the project version: this makes sure
902 -- that the project version (e.g. 407) in the interface
903 -- file is the same as that for the compiler that's reading it
904 checkVersion :: { () }
905            : {-empty-}                  {% checkVersion Nothing }
906            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
907
908 ------------------------------------------------------------------- 
909
910 --                      Haskell code 
911 {
912 happyError :: P a
913 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
914
915 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
916 }