[project @ 2000-03-23 17:45:17 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
1 {
2 module ParseIface ( parseIface, IfaceStuff(..) ) where
3
4 #include "HsVersions.h"
5
6 import HsSyn            -- quite a bit of stuff
7 import RdrHsSyn         -- oodles of synonyms
8 import HsTypes          ( mkHsForAllTy, mkHsUsForAllTy )
9 import HsCore
10 import Literal          ( Literal(..), mkMachInt, mkMachInt64, mkMachWord, mkMachWord64 )
11 import BasicTypes       ( Fixity(..), FixityDirection(..), 
12                           NewOrData(..), Version
13                         )
14 import CostCentre       ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
15 import CallConv         ( cCallConv )
16 import HsPragmas        ( noDataPragmas, noClassPragmas )
17 import Type             ( Kind, mkArrowKind, boxedTypeKind, openTypeKind, UsageAnn(..) )
18 import IdInfo           ( ArityInfo, exactArity, CprInfo(..), InlinePragInfo(..) )
19 import PrimOp           ( CCall(..), CCallTarget(..) )
20 import Lex              
21
22 import RnMonad          ( ImportVersion, LocalVersion, ParsedIface(..), WhatsImported(..),
23                           RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..), 
24                           WhetherHasOrphans, IsBootInterface
25                         ) 
26 import Bag              ( emptyBag, unitBag, snocBag )
27 import FiniteMap        ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
28 import RdrName          ( RdrName, mkRdrUnqual, mkSysQual, mkSysUnqual, mkRdrNameWkr )
29 import Name             ( OccName, Provenance )
30 import OccName          ( mkSysOccFS,
31                           tcName, varName, ipName, dataName, clsName, tvName, uvName,
32                           EncodedFS 
33                         )
34 import Module           ( ModuleName, mkSysModuleFS )                   
35 import PrelInfo         ( mkTupConRdrName, mkUbxTupConRdrName )
36 import SrcLoc           ( SrcLoc )
37 import Maybes
38 import Outputable
39
40 import GlaExts
41 import FastString       ( tailFS )
42
43 #if __HASKELL1__ > 4
44 import Ratio ( (%) )
45 #endif
46 }
47
48 %name       parseIface
49 %tokentype  { Token }
50 %monad      { P }{ thenP }{ returnP }
51 %lexer      { lexer } { ITeof }
52
53 %token
54  'case'         { ITcase }                      -- Haskell keywords
55  'class'        { ITclass } 
56  'data'         { ITdata } 
57  'default'      { ITdefault }
58  'deriving'     { ITderiving }
59  'do'           { ITdo }
60  'else'         { ITelse }
61  'if'           { ITif }
62  'import'       { ITimport }
63  'in'           { ITin }
64  'infix'        { ITinfix }
65  'infixl'       { ITinfixl }
66  'infixr'       { ITinfixr }
67  'instance'     { ITinstance }
68  'let'          { ITlet }
69  'module'       { ITmodule }
70  'newtype'      { ITnewtype }
71  'of'           { ITof }
72  'then'         { ITthen }
73  'type'         { ITtype }
74  'where'        { ITwhere }
75  'as'           { ITas }
76  'qualified'    { ITqualified }
77  'hiding'       { IThiding }
78
79  'forall'       { ITforall }                    -- GHC extension keywords
80  'foreign'      { ITforeign }
81  'export'       { ITexport }
82  'label'        { ITlabel } 
83  'dynamic'      { ITdynamic }
84  'unsafe'       { ITunsafe }
85  'with'         { ITwith }
86
87  '__interface'  { ITinterface }                 -- interface keywords
88  '__export'     { IT__export }
89  '__forall'     { IT__forall }
90  '__depends'    { ITdepends }
91  '__letrec'     { ITletrec }
92  '__coerce'     { ITcoerce }
93  '__inline_call'{ ITinlineCall }
94  '__inline_me'  { ITinlineMe }
95  '__DEFAULT'    { ITdefaultbranch }
96  '__bot'        { ITbottom }
97  '__integer'    { ITinteger_lit }
98  '__float'      { ITfloat_lit }
99  '__word'       { ITword_lit }
100  '__int64'      { ITint64_lit }
101  '__word64'     { ITword64_lit }
102  '__rational'   { ITrational_lit }
103  '__addr'       { ITaddr_lit }
104  '__litlit'     { ITlit_lit }
105  '__string'     { ITstring_lit }
106  '__ccall'      { ITccall $$ }
107  '__scc'        { ITscc }
108  '__sccC'       { ITsccAllCafs }
109
110  '__u'          { ITusage }
111  '__fuall'      { ITfuall }
112
113  '__A'          { ITarity }
114  '__P'          { ITspecialise }
115  '__C'          { ITnocaf }
116  '__U'          { ITunfold $$ }
117  '__S'          { ITstrict $$ }
118  '__R'          { ITrules }
119  '__M'          { ITcprinfo }
120  '__D'          { ITdeprecated }
121
122  '..'           { ITdotdot }                    -- reserved symbols
123  '::'           { ITdcolon }
124  '='            { ITequal }
125  '\\'           { ITlam }
126  '|'            { ITvbar }
127  '<-'           { ITlarrow }
128  '->'           { ITrarrow }
129  '@'            { ITat }
130  '~'            { ITtilde }
131  '=>'           { ITdarrow }
132  '-'            { ITminus }
133  '!'            { ITbang }
134
135  '/\\'          { ITbiglam }                    -- GHC-extension symbols
136
137  '{'            { ITocurly }                    -- special symbols
138  '}'            { ITccurly }
139  '['            { ITobrack }
140  ']'            { ITcbrack }
141  '('            { IToparen }
142  ')'            { ITcparen }
143  '(#'           { IToubxparen }
144  '#)'           { ITcubxparen }
145  ';'            { ITsemi }
146  ','            { ITcomma }
147
148  VARID          { ITvarid    $$ }               -- identifiers
149  CONID          { ITconid    $$ }
150  VARSYM         { ITvarsym   $$ }
151  CONSYM         { ITconsym   $$ }
152  QVARID         { ITqvarid   $$ }
153  QCONID         { ITqconid   $$ }
154  QVARSYM        { ITqvarsym  $$ }
155  QCONSYM        { ITqconsym  $$ }
156
157  IPVARID        { ITipvarid  $$ }               -- GHC extension
158
159  PRAGMA         { ITpragma   $$ }
160
161  CHAR           { ITchar     $$ }
162  STRING         { ITstring   $$ }
163  INTEGER        { ITinteger  $$ }
164  RATIONAL       { ITrational $$ }
165  CLITLIT        { ITlitlit   $$ }
166
167  UNKNOWN        { ITunknown  $$ }
168 %%
169
170 -- iface_stuff is the main production.
171 -- It recognises (a) a whole interface file
172 --               (b) a type (so that type sigs can be parsed lazily)
173 --               (c) the IdInfo part of a signature (same reason)
174
175 iface_stuff :: { IfaceStuff }
176 iface_stuff : iface             { let (nm, iff) = $1 in PIface nm iff }
177             | type              { PType    $1 }
178             | id_info           { PIdInfo  $1 }
179             | '__R' rules       { PRules   $2 }
180             | '__D' deprecs     { PDeprecs $2 }
181
182
183 iface           :: { (ModuleName, ParsedIface) }
184 iface           : '__interface' mod_fs INTEGER orphans checkVersion 'where'
185                   exports_part
186                   import_part
187                   instance_decl_part
188                   decls_part
189                   rules_and_deprecs
190                   { ( $2                        -- Module name
191                     , ParsedIface {
192                         pi_mod = fromInteger $3,        -- Module version
193                         pi_orphan  = $4,
194                         pi_exports = $7,        -- Exports
195                         pi_usages  = $8,        -- Usages
196                         pi_insts   = $9,        -- Local instances
197                         pi_decls   = $10,       -- Decls
198                         pi_rules   = fst $11,   -- Rules 
199                         pi_deprecs = snd $11    -- Deprecations 
200                       } ) }
201
202 --------------------------------------------------------------------------
203
204 import_part :: { [ImportVersion OccName] }
205 import_part :                                             { [] }
206             |  import_part import_decl                    { $2 : $1 }
207             
208 import_decl :: { ImportVersion OccName }
209 import_decl : 'import' mod_fs INTEGER orphans is_boot whats_imported ';'
210                         { (mkSysModuleFS $2, fromInteger $3, $4, $5, $6) }
211         -- import Foo 3 :: a 1 b 3 c 7 ;        means import a,b,c from Foo
212         -- import Foo 3 ;                       means import all of Foo
213         -- import Foo 3 ! @ :: ...stuff... ;    the ! means that Foo contains orphans
214         --                                      and @ that Foo is a boot interface
215
216 orphans             :: { WhetherHasOrphans }
217 orphans             :                                           { False }
218                     | '!'                                       { True }
219
220 is_boot             :: { IsBootInterface }
221 is_boot             :                                           { False }
222                     | '@'                                       { True }
223
224 whats_imported      :: { WhatsImported OccName }
225 whats_imported      :                                           { Everything }
226                     | '::' name_version_pairs                   { Specifically $2 }
227
228 name_version_pairs  ::  { [LocalVersion OccName] }
229 name_version_pairs  :                                           { [] }
230                     |  name_version_pair name_version_pairs     { $1 : $2 }
231
232 name_version_pair   ::  { LocalVersion OccName }
233 name_version_pair   :  var_occ INTEGER                          { ($1, fromInteger $2) }
234                     |  tc_occ  INTEGER                          { ($1, fromInteger $2) }
235
236
237 --------------------------------------------------------------------------
238
239 exports_part    :: { [ExportItem] }
240 exports_part    :                                       { [] }
241                 | exports_part '__export' 
242                   mod_fs entities ';'                   { (mkSysModuleFS $3, $4) : $1 }
243
244 entities        :: { [RdrAvailInfo] }
245 entities        :                                       { [] }
246                 |  entity entities                      { $1 : $2 }
247
248 entity          :: { RdrAvailInfo }
249 entity          :  tc_occ                               { AvailTC $1 [$1] }
250                 |  var_occ                              { Avail $1 }
251                 |  tc_occ stuff_inside                  { AvailTC $1 ($1:$2) }
252                 |  tc_occ '|' stuff_inside              { AvailTC $1 $3 }
253
254 stuff_inside    :: { [OccName] }
255 stuff_inside    :  '{' val_occs '}'                     { $2 }
256
257 val_occ         :: { OccName }
258                 :  var_occ              { $1 }
259                 |  data_occ             { $1 }
260
261 val_occs        :: { [OccName] }
262                 :  val_occ              { [$1] }
263                 |  val_occ val_occs     { $1 : $2 }
264
265
266 --------------------------------------------------------------------------
267
268 fixity      :: { FixityDirection }
269 fixity      : 'infixl'                                  { InfixL }
270             | 'infixr'                                  { InfixR }
271             | 'infix'                                   { InfixN }
272    
273 mb_fix      :: { Int }
274 mb_fix      : {-nothing-}                               { 9 }
275             | INTEGER                                   { (fromInteger $1) }
276
277 -----------------------------------------------------------------------------
278
279 csigs           :: { [RdrNameSig] }
280 csigs           :                               { [] }
281                 | 'where' '{' csigs1 '}'        { $3 }
282
283 csigs1          :: { [RdrNameSig] }
284 csigs1          : csig                          { [$1] }
285                 | csig ';' csigs1               { $1 : $3 }
286
287 csig            :: { RdrNameSig }
288 csig            :  src_loc var_name '::' type           { mkClassOpSig False $2 $4 $1 }
289                 |  src_loc var_name '=' '::' type       { mkClassOpSig True  $2 $5 $1 }
290
291 --------------------------------------------------------------------------
292
293 instance_decl_part :: { [RdrNameInstDecl] }
294 instance_decl_part : {- empty -}                       { [] }
295                    | instance_decl_part inst_decl      { $2 : $1 }
296
297 inst_decl       :: { RdrNameInstDecl }
298 inst_decl       :  src_loc 'instance' type '=' var_name ';'
299                         { InstDecl $3
300                                    EmptyMonoBinds       {- No bindings -}
301                                    []                   {- No user pragmas -}
302                                    $5                   {- Dfun id -}
303                                    $1
304                         }
305
306 --------------------------------------------------------------------------
307
308 decls_part :: { [(Version, RdrNameHsDecl)] }
309 decls_part 
310         :  {- empty -}                          { [] }
311         |  decls_part version decl ';'          { ($2,$3):$1 }
312
313 decl    :: { RdrNameHsDecl }
314 decl    : src_loc var_name '::' type maybe_idinfo
315                          { SigD (IfaceSig $2 $4 ($5 $2) $1) }
316         | src_loc 'type' tc_name tv_bndrs '=' type                     
317                         { TyClD (TySynonym $3 $4 $6 $1) }
318         | src_loc 'data' decl_context tc_name tv_bndrs constrs         
319                         { TyClD (TyData DataType $3 $4 $5 $6 Nothing noDataPragmas $1) }
320         | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
321                         { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
322         | src_loc 'class' decl_context tc_name tv_bndrs fds csigs
323                         { TyClD (mkClassDecl $3 $4 $5 $6 $7 EmptyMonoBinds 
324                                         noClassPragmas $1) }
325         | src_loc fixity mb_fix var_or_data_name
326                         { FixD (FixitySig $4 (Fixity $3 $2) $1) }
327
328 maybe_idinfo  :: { RdrName -> [HsIdInfo RdrName] }
329 maybe_idinfo  : {- empty -}     { \_ -> [] }
330               | pragma          { \x -> case $1 of
331                                      POk _ (PIdInfo id_info) -> id_info
332                                      PFailed err -> 
333                                         pprPanic "IdInfo parse failed" 
334                                             (vcat [ppr x, err])
335                                 }
336
337 pragma  :: { ParseResult IfaceStuff }
338 pragma  : src_loc PRAGMA        { parseIface $2 PState{ bol = 0#, atbol = 1#,
339                                                         context = [],
340                                                         glasgow_exts = 1#,
341                                                         loc = $1 }
342                                 }
343
344 -----------------------------------------------------------------------------
345
346 rules_and_deprecs :: { ([RdrNameRuleDecl], [RdrNameDeprecation]) }
347 rules_and_deprecs : {- empty -} { ([], []) }
348                   | rules_and_deprecs rule_or_deprec
349                                 { let
350                                      append2 (xs1,ys1) (xs2,ys2) =
351                                         (xs1 `app` xs2, ys1 `app` ys2)
352                                      xs `app` [] = xs -- performance paranoia
353                                      xs `app` ys = xs ++ ys
354                                   in append2 $1 $2
355                                 }
356
357 rule_or_deprec :: { ([RdrNameRuleDecl], [RdrNameDeprecation]) }
358 rule_or_deprec : pragma { case $1 of
359                              POk _ (PRules   rules)   -> (rules,[])
360                              POk _ (PDeprecs deprecs) -> ([],deprecs)
361                              PFailed err -> pprPanic "Rules/Deprecations parse failed" err
362                         }
363
364 -----------------------------------------------------------------------------
365
366 rules      :: { [RdrNameRuleDecl] }
367            : {- empty -}        { [] }
368            | rule ';' rules     { $1:$3 }
369
370 rule       :: { RdrNameRuleDecl }
371 rule       : src_loc STRING rule_forall qvar_name 
372              core_args '=' core_expr    { IfaceRuleDecl $4 (UfRuleBody $2 $3 $5 $7) $1 } 
373
374 rule_forall     :: { [UfBinder RdrName] }
375 rule_forall     : '__forall' '{' core_bndrs '}' { $3 }
376                   
377 -----------------------------------------------------------------------------
378
379 deprecs         :: { [RdrNameDeprecation] }
380 deprecs         : {- empty -}           { [] }
381                 | deprecs deprec ';'    { $2 : $1 }
382
383 deprec          :: { RdrNameDeprecation }
384 deprec          : STRING                { Deprecation (IEModuleContents undefined) $1 }
385                 | deprec_name STRING    { Deprecation $1 $2 }
386
387 -- SUP: TEMPORARY HACK
388 deprec_name     :: { RdrNameIE }
389                 : var_name              { IEVar      $1 }
390                 | data_name             { IEThingAbs $1 }
391
392 -----------------------------------------------------------------------------
393
394 version         :: { Version }
395 version         :  INTEGER                              { fromInteger $1 }
396
397 decl_context    :: { RdrNameContext }
398 decl_context    :                                       { [] }
399                 | '{' context_list1 '}' '=>'    { $2 }
400
401 ----------------------------------------------------------------------------
402
403 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
404                 :                       { [] }
405                 | '=' constrs1          { $2 }
406
407 constrs1        :: { [RdrNameConDecl] }
408 constrs1        :  constr               { [$1] }
409                 |  constr '|' constrs1  { $1 : $3 }
410
411 constr          :: { RdrNameConDecl }
412 constr          :  src_loc ex_stuff data_name batypes           { mk_con_decl $3 $2 (VanillaCon $4) $1 }
413                 |  src_loc ex_stuff data_name '{' fields1 '}'   { mk_con_decl $3 $2 (RecCon $5)     $1 }
414                 -- We use "data_fs" so as to include ()
415
416 newtype_constr  :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
417 newtype_constr  :                                       { [] }
418                 | src_loc '=' ex_stuff data_name atype  { [mk_con_decl $4 $3 (NewCon $5 Nothing) $1] }
419                 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
420                                                         { [mk_con_decl $4 $3 (NewCon $8 (Just $6)) $1] }
421
422 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
423 ex_stuff        :                                       { ([],[]) }
424                 | '__forall' forall context '=>'            { ($2,$3) }
425
426 batypes         :: { [RdrNameBangType] }
427 batypes         :                                       { [] }
428                 |  batype batypes                       { $1 : $2 }
429
430 batype          :: { RdrNameBangType }
431 batype          :  atype                                { Unbanged $1 }
432                 |  '!' atype                            { Banged   $2 }
433                 |  '!' '!' atype                        { Unpacked $3 }
434
435 fields1         :: { [([RdrName], RdrNameBangType)] }
436 fields1         : field                                 { [$1] }
437                 | field ',' fields1                     { $1 : $3 }
438
439 field           :: { ([RdrName], RdrNameBangType) }
440 field           :  var_names1 '::' type         { ($1, Unbanged $3) }
441                 |  var_names1 '::' '!' type     { ($1, Banged   $4) }
442                 |  var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
443 --------------------------------------------------------------------------
444
445 type            :: { RdrNameHsType }
446 type            : '__fuall'  fuall '=>' type    { mkHsUsForAllTy $2 $4 }
447                 | '__forall' forall context '=>' type   
448                                                 { mkHsForAllTy (Just $2) $3 $5 }
449                 | btype '->' type               { MonoFunTy $1 $3 }
450                 | btype                         { $1 }
451
452 fuall           :: { [RdrName] }
453 fuall           : '[' uv_bndrs ']'                      { $2 }
454
455 forall          :: { [HsTyVar RdrName] }
456 forall          : '[' tv_bndrs ']'                      { $2 }
457
458 context         :: { RdrNameContext }
459 context         :                                       { [] }
460                 | '{' context_list1 '}'                 { $2 }
461
462 context_list1   :: { RdrNameContext }
463 context_list1   : class                                 { [$1] }
464                 | class ',' context_list1               { $1 : $3 }
465
466 class           :: { HsPred RdrName }
467 class           :  qcls_name atypes                     { (HsPClass $1 $2) }
468                 |  ipvar_name '::' type                 { (HsPIParam $1 $3) }
469
470 types0          :: { [RdrNameHsType]                    {- Zero or more -}  }   
471 types0          :  {- empty -}                          { [ ] }
472                 |  type                                 { [ $1 ] }
473                 |  types2                               { $1 }
474
475 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
476 types2          :  type ',' type                        { [$1,$3] }
477                 |  type ',' types2                      { $1 : $3 }
478
479 btype           :: { RdrNameHsType }
480 btype           :  atype                                { $1 }
481                 |  btype atype                          { MonoTyApp $1 $2 }
482                 |  '__u' usage atype                    { MonoUsgTy $2 $3 }
483
484 usage           :: { MonoUsageAnn RdrName }
485 usage           : '-'                                   { MonoUsOnce }
486                 | '!'                                   { MonoUsMany }
487                 | uv_name                               { MonoUsVar $1 }
488
489 atype           :: { RdrNameHsType }
490 atype           :  qtc_name                             { MonoTyVar $1 }
491                 |  tv_name                              { MonoTyVar $1 }
492                 |  '(' types2 ')'                       { MonoTupleTy $2 True{-boxed-} }
493                 |  '(#' types0 '#)'                     { MonoTupleTy $2 False{-unboxed-} }
494                 |  '[' type ']'                         { MonoListTy  $2 }
495                 |  '{' qcls_name atypes '}'             { MonoDictTy $2 $3 }
496                 |  '{' ipvar_name '::' type '}'         { MonoIParamTy $2 $4 }
497                 |  '(' type ')'                         { $2 }
498
499 -- This one is dealt with via qtc_name
500 --              |  '(' ')'                              { MonoTupleTy [] True }
501
502 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
503 atypes          :                                       { [] }
504                 |  atype atypes                         { $1 : $2 }
505 ---------------------------------------------------------------------
506 mod_fs          :: { EncodedFS }
507                 :  CONID                { $1 }
508
509 mod_name        :: { ModuleName }
510                 :  mod_fs               { mkSysModuleFS $1 }
511
512
513 ---------------------------------------------------
514 var_fs          :: { EncodedFS }
515                 : VARID                 { $1 }
516                 | '!'                   { SLIT("!") }
517                 | 'as'                  { SLIT("as") }
518                 | 'qualified'           { SLIT("qualified") }
519                 | 'hiding'              { SLIT("hiding") }
520                 | 'forall'              { SLIT("forall") }
521                 | 'foreign'             { SLIT("foreign") }
522                 | 'export'              { SLIT("export") }
523                 | 'label'               { SLIT("label") }
524                 | 'dynamic'             { SLIT("dynamic") }
525                 | 'unsafe'              { SLIT("unsafe") }
526                 | 'with'                { SLIT("with") }
527
528 qvar_fs         :: { (EncodedFS, EncodedFS) }
529                 :  QVARID               { $1 }
530                 |  QVARSYM              { $1 }
531
532 var_occ         :: { OccName }
533                 :  var_fs               { mkSysOccFS varName $1 }
534
535 var_name        :: { RdrName }
536 var_name        :  var_occ              { mkRdrUnqual $1 }
537
538 qvar_name       :: { RdrName }
539 qvar_name       :  var_name             { $1 }
540                 |  qvar_fs              { mkSysQual varName $1 }
541
542 ipvar_name      :: { RdrName }
543                 :  IPVARID              { mkSysUnqual ipName (tailFS $1) }
544
545 var_names       :: { [RdrName] }
546 var_names       :                       { [] }
547                 | var_name var_names    { $1 : $2 }
548
549 var_names1      :: { [RdrName] }
550 var_names1      : var_name var_names    { $1 : $2 }
551
552 ---------------------------------------------------
553 -- For some bizarre reason, 
554 --      (,,,)      is dealt with by the parser
555 --      Foo.(,,,)  is dealt with by the lexer
556 -- Sigh
557
558 data_fs         :: { EncodedFS }
559                 :  CONID                { $1 }
560                 |  CONSYM               { $1 }
561
562 qdata_fs        :: { (EncodedFS, EncodedFS) }
563                 :  QCONID               { $1 }
564                 |  QCONSYM              { $1 }
565
566 data_occ        :: { OccName }
567                 :  data_fs              { mkSysOccFS dataName $1 }
568
569 data_name       :: { RdrName }
570                 :  data_occ             { mkRdrUnqual $1 }
571
572 qdata_name      :: { RdrName }
573 qdata_name      :  data_name            { $1 }
574                 |  qdata_fs             { mkSysQual dataName $1 }
575                                 
576 qdata_names     :: { [RdrName] }
577 qdata_names     :                               { [] }
578                 | qdata_name qdata_names        { $1 : $2 }
579
580 var_or_data_name :: { RdrName }
581                   : var_name                    { $1 }
582                   | data_name                   { $1 }
583
584 ---------------------------------------------------
585 tc_fs           :: { EncodedFS }
586                 :  data_fs              { $1 }
587
588 tc_occ          :: { OccName }
589                 :  tc_fs                { mkSysOccFS tcName $1 }
590
591 tc_name         :: { RdrName }
592                 :  tc_occ               { mkRdrUnqual $1 }
593
594 qtc_name        :: { RdrName }
595                 : tc_name               { $1 }
596                 | qdata_fs              { mkSysQual tcName $1 }
597
598 ---------------------------------------------------
599 cls_name        :: { RdrName }
600                 :  data_fs              { mkSysUnqual clsName $1 }
601
602 qcls_name       :: { RdrName }
603                 : cls_name              { $1 }
604                 | qdata_fs              { mkSysQual clsName $1 }
605
606 ---------------------------------------------------
607 uv_name         :: { RdrName }
608                 :  VARID                { mkSysUnqual uvName $1 }
609
610 uv_bndr         :: { RdrName }
611                 :  uv_name              { $1 }
612
613 uv_bndrs        :: { [RdrName] }
614                 :                       { [] }
615                 | uv_bndr uv_bndrs      { $1 : $2 }
616
617 ---------------------------------------------------
618 tv_name         :: { RdrName }
619                 :  VARID                { mkSysUnqual tvName $1 }
620                 |  VARSYM               { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
621
622 tv_bndr         :: { HsTyVar RdrName }
623                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
624                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
625
626 tv_bndrs        :: { [HsTyVar RdrName] }
627                 :                       { [] }
628                 | tv_bndr tv_bndrs      { $1 : $2 }
629
630 ---------------------------------------------------
631 fds :: { [([RdrName], [RdrName])] }
632         : {- empty -}                   { [] }
633         | '|' fds1                      { reverse $2 }
634
635 fds1 :: { [([RdrName], [RdrName])] }
636         : fds1 ',' fd                   { $3 : $1 }
637         | fd                            { [$1] }
638
639 fd :: { ([RdrName], [RdrName]) }
640         : varids0 '->' varids0          { (reverse $1, reverse $3) }
641
642 varids0 :: { [RdrName] }
643         : {- empty -}                   { [] }
644         | varids0 tv_name               { $2 : $1 }
645
646 ---------------------------------------------------
647 kind            :: { Kind }
648                 : akind                 { $1 }
649                 | akind '->' kind       { mkArrowKind $1 $3 }
650
651 akind           :: { Kind }
652                 : VARSYM                { if $1 == SLIT("*") then
653                                                 boxedTypeKind
654                                           else if $1 == SLIT("?") then
655                                                 openTypeKind
656                                           else panic "ParseInterface: akind"
657                                         }
658                 | '(' kind ')'  { $2 }
659
660 --------------------------------------------------------------------------
661
662 id_info         :: { [HsIdInfo RdrName] }
663                 :                               { [] }
664                 | id_info_item id_info          { $1 : $2 }
665
666 id_info_item    :: { HsIdInfo RdrName }
667                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
668                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
669                 | '__M'                         { HsCprInfo }
670                 | '__S'                         { HsStrictness (HsStrictnessInfo $1) }
671                 | '__C'                         { HsNoCafRefs }
672                 | '__P' qvar_name               { HsWorker $2 }
673
674 inline_prag     :: { InlinePragInfo }
675                 :  {- empty -}                  { NoInlinePragInfo }
676                 | '[' INTEGER ']'               { IMustNotBeINLINEd True  (Just (fromInteger $2)) } -- INLINE n
677                 | '[' '!' INTEGER ']'           { IMustNotBeINLINEd False (Just (fromInteger $3)) } -- NOINLINE n
678
679 -------------------------------------------------------
680 core_expr       :: { UfExpr RdrName }
681 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
682                 | 'case' core_expr 'of' var_name
683                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
684
685                 | 'let' '{' core_val_bndr '=' core_expr
686                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
687                 | '__letrec' '{' rec_binds '}'          
688                   'in' core_expr                        { UfLet (UfRec $3) $6 }
689
690                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
691
692                 | '__inline_me' core_expr               { UfNote UfInlineMe $2 }
693                 | '__inline_call' core_expr             { UfNote UfInlineCall $2 }
694                 | '__coerce' atype core_expr            { UfNote (UfCoerce $2) $3 }
695                 | scc core_expr                         { UfNote (UfSCC $1) $2  }
696                 | fexpr                                 { $1 }
697
698 fexpr   :: { UfExpr RdrName }
699 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
700         | core_aexpr                                    { $1 }
701
702 core_arg        :: { UfExpr RdrName }
703                 : '@' atype                                     { UfType $2 }
704                 | core_aexpr                                    { $1 }
705
706 core_args       :: { [UfExpr RdrName] }
707                 :                                               { [] }
708                 | core_arg core_args                            { $1 : $2 }
709
710 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
711 core_aexpr      : qvar_name                                     { UfVar $1 }
712                 | qdata_name                                    { UfVar $1 }
713                         -- This one means that e.g. "True" will parse as 
714                         -- (UfVar True_Id) rather than (UfCon True_Con []).
715                         -- No big deal; it'll be inlined in a jiffy.  I tried 
716                         -- parsing it to (Con con []) directly, but got bitten 
717                         -- when a real constructor Id showed up in an interface
718                         -- file.  As usual, a hack bites you in the end.
719                         -- If you want to get a UfCon, then use the
720                         -- curly-bracket notation (True {}).
721
722 -- This one is dealt with by qdata_name: see above comments
723 --              | '('  ')'               { UfTuple (mkTupConRdrName 0) [] }
724
725                 | core_lit               { UfLit $1 }
726                 | '(' core_expr ')'      { $2 }
727
728                         -- Tuple construtors are for the *worker* of the tuple
729                         -- Going direct saves needless messing about 
730                 | '(' comma_exprs2 ')'   { UfTuple (mkRdrNameWkr (mkTupConRdrName (length $2))) $2 }
731                 | '(#' comma_exprs0 '#)' { UfTuple (mkRdrNameWkr (mkUbxTupConRdrName (length $2))) $2 }
732
733                 | '{' '__ccall' ccall_string type '}'       
734                            { let
735                                  (is_dyn, is_casm, may_gc) = $2
736
737                                  target | is_dyn    = DynamicTarget (error "CCall dyn target bogus unique")
738                                         | otherwise = StaticTarget $3
739
740                                  ccall = CCall target is_casm may_gc cCallConv
741                              in
742                              UfCCall ccall $4
743                            }
744
745
746 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
747 comma_exprs0    : {- empty -}                   { [ ] }
748                 | core_expr                     { [ $1 ] }
749                 | comma_exprs2                  { $1 }
750
751 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
752 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
753                 | core_expr ',' comma_exprs2                    { $1 : $3 }
754
755 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
756                 :                                               { [] }
757                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
758
759 core_alts       :: { [UfAlt RdrName] }
760                 : core_alt                                      { [$1] }
761                 | core_alt ';' core_alts                        { $1 : $3 }
762
763 core_alt        :: { UfAlt RdrName }
764 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
765
766 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
767 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
768                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
769                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
770                 | '(' comma_var_names1 ')'      { (UfDataAlt (mkTupConRdrName (length $2)), $2) }
771                 | '(#' comma_var_names1 '#)'    { (UfDataAlt (mkUbxTupConRdrName (length $2)), $2) }
772                 | '__DEFAULT'                   { (UfDefault, []) }
773                 | '(' core_pat ')'              { $2 }
774
775 core_pat_names :: { [RdrName] }
776 core_pat_names :                                { [] }
777                 | core_pat_name core_pat_names  { $1 : $2 }
778
779 -- Tyvar names and variable names live in different name spaces
780 -- so they need to be signalled separately.  But we don't record 
781 -- types or kinds in a pattern; we work that out from the type 
782 -- of the case scrutinee
783 core_pat_name   :: { RdrName }
784 core_pat_name   : var_name                      { $1 }
785                 | '@' tv_name                   { $2 }
786         
787 comma_var_names1 :: { [RdrName] }       -- One or more
788 comma_var_names1 : var_name                                     { [$1] }
789                  | var_name ',' comma_var_names1                { $1 : $3 }
790
791 core_lit        :: { Literal }
792 core_lit        : integer                       { mkMachInt $1 }
793                 | CHAR                          { MachChar $1 }
794                 | STRING                        { MachStr $1 }
795                 | rational                      { MachDouble $1 }
796                 | '__word' integer              { mkMachWord $2 }
797                 | '__word64' integer            { mkMachWord64 $2 }
798                 | '__int64' integer             { mkMachInt64 $2 }
799                 | '__float' rational            { MachFloat $2 }
800                 | '__addr' integer              { MachAddr $2 }
801
802 integer         :: { Integer }
803                 : INTEGER                       { $1 }
804                 | '-' INTEGER                   { (-$2) }
805
806 rational        :: { Rational }
807                 : RATIONAL                      { $1 }
808                 | '-' RATIONAL                  { (-$2) }
809
810 core_bndr       :: { UfBinder RdrName }
811 core_bndr       : core_val_bndr                                 { $1 }
812                 | core_tv_bndr                                  { $1 }
813
814 core_bndrs      :: { [UfBinder RdrName] }
815 core_bndrs      :                                               { [] }
816                 | core_bndr core_bndrs                          { $1 : $2 }
817
818 core_val_bndr   :: { UfBinder RdrName }
819 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
820
821 core_tv_bndr    :: { UfBinder RdrName }
822 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
823                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
824
825 ccall_string    :: { FAST_STRING }
826                 : STRING                                        { $1 }
827                 | CLITLIT                                       { $1 }
828                 | VARID                                         { $1 }
829                 | CONID                                         { $1 }
830
831 ------------------------------------------------------------------------
832 scc     :: { CostCentre }
833         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
834         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
835                              { NormalCC { cc_name = $3, cc_mod = $4,
836                                           cc_is_dupd = $5, cc_is_caf = $6 } }
837
838 cc_name :: { EncodedFS }
839         : CONID                 { $1 }
840         | var_fs                { $1 }
841   
842 cc_dup  :: { IsDupdCC }
843 cc_dup  :                       { OriginalCC }
844         | '!'                   { DupdCC }
845
846 cc_caf  :: { IsCafCC }
847         :                       { NotCafCC }
848         | '__C'                 { CafCC }
849
850 -------------------------------------------------------------------
851
852 src_loc :: { SrcLoc }
853 src_loc :                               {% getSrcLocP }
854
855 checkVersion :: { () }
856            : {-empty-}                  {% checkVersion Nothing }
857            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
858
859 ------------------------------------------------------------------- 
860
861 --                      Haskell code 
862 {
863 happyError :: P a
864 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
865
866 data IfaceStuff = PIface        EncodedFS{-.hi module name-} ParsedIface
867                 | PIdInfo       [HsIdInfo RdrName]
868                 | PType         RdrNameHsType
869                 | PRules        [RdrNameRuleDecl]
870                 | PDeprecs      [RdrNameDeprecation]
871
872 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
873 }