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