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