[project @ 2000-03-27 08:50: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                 | 'ccall'               { SLIT("ccall") }
531                 | 'stdcall'             { SLIT("stdcall") }
532
533 qvar_fs         :: { (EncodedFS, EncodedFS) }
534                 :  QVARID               { $1 }
535                 |  QVARSYM              { $1 }
536
537 var_occ         :: { OccName }
538                 :  var_fs               { mkSysOccFS varName $1 }
539
540 var_name        :: { RdrName }
541 var_name        :  var_occ              { mkRdrUnqual $1 }
542
543 qvar_name       :: { RdrName }
544 qvar_name       :  var_name             { $1 }
545                 |  qvar_fs              { mkSysQual varName $1 }
546
547 ipvar_name      :: { RdrName }
548                 :  IPVARID              { mkSysUnqual ipName (tailFS $1) }
549
550 var_names       :: { [RdrName] }
551 var_names       :                       { [] }
552                 | var_name var_names    { $1 : $2 }
553
554 var_names1      :: { [RdrName] }
555 var_names1      : var_name var_names    { $1 : $2 }
556
557 ---------------------------------------------------
558 -- For some bizarre reason, 
559 --      (,,,)      is dealt with by the parser
560 --      Foo.(,,,)  is dealt with by the lexer
561 -- Sigh
562
563 data_fs         :: { EncodedFS }
564                 :  CONID                { $1 }
565                 |  CONSYM               { $1 }
566
567 qdata_fs        :: { (EncodedFS, EncodedFS) }
568                 :  QCONID               { $1 }
569                 |  QCONSYM              { $1 }
570
571 data_occ        :: { OccName }
572                 :  data_fs              { mkSysOccFS dataName $1 }
573
574 data_name       :: { RdrName }
575                 :  data_occ             { mkRdrUnqual $1 }
576
577 qdata_name      :: { RdrName }
578 qdata_name      :  data_name            { $1 }
579                 |  qdata_fs             { mkSysQual dataName $1 }
580                                 
581 qdata_names     :: { [RdrName] }
582 qdata_names     :                               { [] }
583                 | qdata_name qdata_names        { $1 : $2 }
584
585 var_or_data_name :: { RdrName }
586                   : var_name                    { $1 }
587                   | data_name                   { $1 }
588
589 ---------------------------------------------------
590 tc_fs           :: { EncodedFS }
591                 :  data_fs              { $1 }
592
593 tc_occ          :: { OccName }
594                 :  tc_fs                { mkSysOccFS tcName $1 }
595
596 tc_name         :: { RdrName }
597                 :  tc_occ               { mkRdrUnqual $1 }
598
599 qtc_name        :: { RdrName }
600                 : tc_name               { $1 }
601                 | qdata_fs              { mkSysQual tcName $1 }
602
603 ---------------------------------------------------
604 cls_name        :: { RdrName }
605                 :  data_fs              { mkSysUnqual clsName $1 }
606
607 qcls_name       :: { RdrName }
608                 : cls_name              { $1 }
609                 | qdata_fs              { mkSysQual clsName $1 }
610
611 ---------------------------------------------------
612 uv_name         :: { RdrName }
613                 :  VARID                { mkSysUnqual uvName $1 }
614
615 uv_bndr         :: { RdrName }
616                 :  uv_name              { $1 }
617
618 uv_bndrs        :: { [RdrName] }
619                 :                       { [] }
620                 | uv_bndr uv_bndrs      { $1 : $2 }
621
622 ---------------------------------------------------
623 tv_name         :: { RdrName }
624                 :  VARID                { mkSysUnqual tvName $1 }
625                 |  VARSYM               { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
626
627 tv_bndr         :: { HsTyVar RdrName }
628                 :  tv_name '::' akind   { IfaceTyVar $1 $3 }
629                 |  tv_name              { IfaceTyVar $1 boxedTypeKind }
630
631 tv_bndrs        :: { [HsTyVar RdrName] }
632                 :                       { [] }
633                 | tv_bndr tv_bndrs      { $1 : $2 }
634
635 ---------------------------------------------------
636 fds :: { [([RdrName], [RdrName])] }
637         : {- empty -}                   { [] }
638         | '|' fds1                      { reverse $2 }
639
640 fds1 :: { [([RdrName], [RdrName])] }
641         : fds1 ',' fd                   { $3 : $1 }
642         | fd                            { [$1] }
643
644 fd :: { ([RdrName], [RdrName]) }
645         : varids0 '->' varids0          { (reverse $1, reverse $3) }
646
647 varids0 :: { [RdrName] }
648         : {- empty -}                   { [] }
649         | varids0 tv_name               { $2 : $1 }
650
651 ---------------------------------------------------
652 kind            :: { Kind }
653                 : akind                 { $1 }
654                 | akind '->' kind       { mkArrowKind $1 $3 }
655
656 akind           :: { Kind }
657                 : VARSYM                { if $1 == SLIT("*") then
658                                                 boxedTypeKind
659                                           else if $1 == SLIT("?") then
660                                                 openTypeKind
661                                           else panic "ParseInterface: akind"
662                                         }
663                 | '(' kind ')'  { $2 }
664
665 --------------------------------------------------------------------------
666
667 id_info         :: { [HsIdInfo RdrName] }
668                 :                               { [] }
669                 | id_info_item id_info          { $1 : $2 }
670
671 id_info_item    :: { HsIdInfo RdrName }
672                 : '__A' INTEGER                 { HsArity (exactArity (fromInteger $2)) }
673                 | '__U' inline_prag core_expr   { HsUnfold $2 $3 }
674                 | '__M'                         { HsCprInfo }
675                 | '__S'                         { HsStrictness (HsStrictnessInfo $1) }
676                 | '__C'                         { HsNoCafRefs }
677                 | '__P' qvar_name               { HsWorker $2 }
678
679 inline_prag     :: { InlinePragInfo }
680                 :  {- empty -}                  { NoInlinePragInfo }
681                 | '[' INTEGER ']'               { IMustNotBeINLINEd True  (Just (fromInteger $2)) } -- INLINE n
682                 | '[' '!' INTEGER ']'           { IMustNotBeINLINEd False (Just (fromInteger $3)) } -- NOINLINE n
683
684 -------------------------------------------------------
685 core_expr       :: { UfExpr RdrName }
686 core_expr       : '\\' core_bndrs '->' core_expr        { foldr UfLam $4 $2 }
687                 | 'case' core_expr 'of' var_name
688                   '{' core_alts '}'                     { UfCase $2 $4 $6 }
689
690                 | 'let' '{' core_val_bndr '=' core_expr
691                       '}' 'in' core_expr                { UfLet (UfNonRec $3 $5) $8 }
692                 | '__letrec' '{' rec_binds '}'          
693                   'in' core_expr                        { UfLet (UfRec $3) $6 }
694
695                 | '__litlit' STRING atype               { UfLitLit $2 $3 }
696
697                 | '__inline_me' core_expr               { UfNote UfInlineMe $2 }
698                 | '__inline_call' core_expr             { UfNote UfInlineCall $2 }
699                 | '__coerce' atype core_expr            { UfNote (UfCoerce $2) $3 }
700                 | scc core_expr                         { UfNote (UfSCC $1) $2  }
701                 | fexpr                                 { $1 }
702
703 fexpr   :: { UfExpr RdrName }
704 fexpr   : fexpr core_arg                                { UfApp $1 $2 }
705         | core_aexpr                                    { $1 }
706
707 core_arg        :: { UfExpr RdrName }
708                 : '@' atype                                     { UfType $2 }
709                 | core_aexpr                                    { $1 }
710
711 core_args       :: { [UfExpr RdrName] }
712                 :                                               { [] }
713                 | core_arg core_args                            { $1 : $2 }
714
715 core_aexpr      :: { UfExpr RdrName }              -- Atomic expressions
716 core_aexpr      : qvar_name                                     { UfVar $1 }
717                 | qdata_name                                    { UfVar $1 }
718                         -- This one means that e.g. "True" will parse as 
719                         -- (UfVar True_Id) rather than (UfCon True_Con []).
720                         -- No big deal; it'll be inlined in a jiffy.  I tried 
721                         -- parsing it to (Con con []) directly, but got bitten 
722                         -- when a real constructor Id showed up in an interface
723                         -- file.  As usual, a hack bites you in the end.
724                         -- If you want to get a UfCon, then use the
725                         -- curly-bracket notation (True {}).
726
727 -- This one is dealt with by qdata_name: see above comments
728 --              | '('  ')'               { UfTuple (mkTupConRdrName 0) [] }
729
730                 | core_lit               { UfLit $1 }
731                 | '(' core_expr ')'      { $2 }
732
733                         -- Tuple construtors are for the *worker* of the tuple
734                         -- Going direct saves needless messing about 
735                 | '(' comma_exprs2 ')'   { UfTuple (mkRdrNameWkr (mkTupConRdrName (length $2))) $2 }
736                 | '(#' comma_exprs0 '#)' { UfTuple (mkRdrNameWkr (mkUbxTupConRdrName (length $2))) $2 }
737
738                 | '{' '__ccall' ccall_string type '}'       
739                            { let
740                                  (is_dyn, is_casm, may_gc) = $2
741
742                                  target | is_dyn    = DynamicTarget (error "CCall dyn target bogus unique")
743                                         | otherwise = StaticTarget $3
744
745                                  ccall = CCall target is_casm may_gc cCallConv
746                              in
747                              UfCCall ccall $4
748                            }
749
750
751 comma_exprs0    :: { [UfExpr RdrName] } -- Zero or more
752 comma_exprs0    : {- empty -}                   { [ ] }
753                 | core_expr                     { [ $1 ] }
754                 | comma_exprs2                  { $1 }
755
756 comma_exprs2    :: { [UfExpr RdrName] } -- Two or more
757 comma_exprs2    : core_expr ',' core_expr                       { [$1,$3] }
758                 | core_expr ',' comma_exprs2                    { $1 : $3 }
759
760 rec_binds       :: { [(UfBinder RdrName, UfExpr RdrName)] }
761                 :                                               { [] }
762                 | core_val_bndr '=' core_expr ';' rec_binds     { ($1,$3) : $5 }
763
764 core_alts       :: { [UfAlt RdrName] }
765                 : core_alt                                      { [$1] }
766                 | core_alt ';' core_alts                        { $1 : $3 }
767
768 core_alt        :: { UfAlt RdrName }
769 core_alt        : core_pat '->' core_expr       { (fst $1, snd $1, $3) }
770
771 core_pat        :: { (UfConAlt RdrName, [RdrName]) }
772 core_pat        : core_lit                      { (UfLitAlt  $1, []) }
773                 | '__litlit' STRING atype       { (UfLitLitAlt $2 $3, []) }
774                 | qdata_name core_pat_names     { (UfDataAlt $1, $2) }
775                 | '(' comma_var_names1 ')'      { (UfDataAlt (mkTupConRdrName (length $2)), $2) }
776                 | '(#' comma_var_names1 '#)'    { (UfDataAlt (mkUbxTupConRdrName (length $2)), $2) }
777                 | '__DEFAULT'                   { (UfDefault, []) }
778                 | '(' core_pat ')'              { $2 }
779
780 core_pat_names :: { [RdrName] }
781 core_pat_names :                                { [] }
782                 | core_pat_name core_pat_names  { $1 : $2 }
783
784 -- Tyvar names and variable names live in different name spaces
785 -- so they need to be signalled separately.  But we don't record 
786 -- types or kinds in a pattern; we work that out from the type 
787 -- of the case scrutinee
788 core_pat_name   :: { RdrName }
789 core_pat_name   : var_name                      { $1 }
790                 | '@' tv_name                   { $2 }
791         
792 comma_var_names1 :: { [RdrName] }       -- One or more
793 comma_var_names1 : var_name                                     { [$1] }
794                  | var_name ',' comma_var_names1                { $1 : $3 }
795
796 core_lit        :: { Literal }
797 core_lit        : integer                       { mkMachInt $1 }
798                 | CHAR                          { MachChar $1 }
799                 | STRING                        { MachStr $1 }
800                 | rational                      { MachDouble $1 }
801                 | '__word' integer              { mkMachWord $2 }
802                 | '__word64' integer            { mkMachWord64 $2 }
803                 | '__int64' integer             { mkMachInt64 $2 }
804                 | '__float' rational            { MachFloat $2 }
805                 | '__addr' integer              { MachAddr $2 }
806
807 integer         :: { Integer }
808                 : INTEGER                       { $1 }
809                 | '-' INTEGER                   { (-$2) }
810
811 rational        :: { Rational }
812                 : RATIONAL                      { $1 }
813                 | '-' RATIONAL                  { (-$2) }
814
815 core_bndr       :: { UfBinder RdrName }
816 core_bndr       : core_val_bndr                                 { $1 }
817                 | core_tv_bndr                                  { $1 }
818
819 core_bndrs      :: { [UfBinder RdrName] }
820 core_bndrs      :                                               { [] }
821                 | core_bndr core_bndrs                          { $1 : $2 }
822
823 core_val_bndr   :: { UfBinder RdrName }
824 core_val_bndr   : var_name '::' atype                           { UfValBinder $1 $3 }
825
826 core_tv_bndr    :: { UfBinder RdrName }
827 core_tv_bndr    :  '@' tv_name '::' akind               { UfTyBinder $2 $4 }
828                 |  '@' tv_name                          { UfTyBinder $2 boxedTypeKind }
829
830 ccall_string    :: { FAST_STRING }
831                 : STRING                                        { $1 }
832                 | CLITLIT                                       { $1 }
833                 | VARID                                         { $1 }
834                 | CONID                                         { $1 }
835
836 ------------------------------------------------------------------------
837 scc     :: { CostCentre }
838         :  '__sccC' '{' mod_name '}'                      { AllCafsCC $3 }
839         |  '__scc' '{' cc_name mod_name cc_dup cc_caf '}'
840                              { NormalCC { cc_name = $3, cc_mod = $4,
841                                           cc_is_dupd = $5, cc_is_caf = $6 } }
842
843 cc_name :: { EncodedFS }
844         : CONID                 { $1 }
845         | var_fs                { $1 }
846   
847 cc_dup  :: { IsDupdCC }
848 cc_dup  :                       { OriginalCC }
849         | '!'                   { DupdCC }
850
851 cc_caf  :: { IsCafCC }
852         :                       { NotCafCC }
853         | '__C'                 { CafCC }
854
855 -------------------------------------------------------------------
856
857 src_loc :: { SrcLoc }
858 src_loc :                               {% getSrcLocP }
859
860 checkVersion :: { () }
861            : {-empty-}                  {% checkVersion Nothing }
862            | INTEGER                    {% checkVersion (Just (fromInteger $1)) }
863
864 ------------------------------------------------------------------- 
865
866 --                      Haskell code 
867 {
868 happyError :: P a
869 happyError buf PState{ loc = loc } = PFailed (ifaceParseErr buf loc)
870
871 data IfaceStuff = PIface        EncodedFS{-.hi module name-} ParsedIface
872                 | PIdInfo       [HsIdInfo RdrName]
873                 | PType         RdrNameHsType
874                 | PRules        [RdrNameRuleDecl]
875                 | PDeprecs      [RdrNameDeprecation]
876
877 mk_con_decl name (ex_tvs, ex_ctxt) details loc = mkConDecl name ex_tvs ex_ctxt details loc
878 }