[project @ 1997-06-05 20:32:25 by sof]
[ghc-hetmet.git] / ghc / compiler / rename / ParseIface.y
1 {
2 #include "HsVersions.h"
3 module ParseIface ( parseIface ) where
4
5 IMP_Ubiq(){-uitous-}
6
7 import HsSyn            -- quite a bit of stuff
8 import RdrHsSyn         -- oodles of synonyms
9 import HsDecls          ( HsIdInfo(..) )
10 import HsTypes          ( mkHsForAllTy )
11 import HsCore
12 import Literal
13 import BasicTypes       ( Fixity(..), FixityDirection(..), NewOrData(..), Version(..) )
14 import HsPragmas        ( noDataPragmas, noClassPragmas )
15 import Kind             ( Kind, mkArrowKind, mkBoxedTypeKind )
16 import IdInfo           ( ArgUsageInfo, FBTypeInfo )
17 import Lex              
18
19 import RnMonad          ( SYN_IE(ImportVersion), SYN_IE(LocalVersion), ParsedIface(..),
20                           SYN_IE(RdrNamePragma), SYN_IE(ExportItem)
21                         ) 
22 import Bag              ( emptyBag, unitBag, snocBag )
23 import FiniteMap        ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
24 import Name             ( OccName(..), isTCOcc, Provenance, SYN_IE(Module) )
25 import SrcLoc           ( mkIfaceSrcLoc )
26 --import Util           ( panic{-, pprPanic ToDo:rm-} )
27 import ParseType        ( parseType )
28 import ParseUnfolding   ( parseUnfolding )
29 import Maybes
30
31 -----------------------------------------------------------------
32
33 parseIface ls = parseIToks (lexIface ls)
34
35 -----------------------------------------------------------------
36 }
37
38 %name       parseIToks
39 %tokentype  { IfaceToken }
40 %monad      { IfM }{ thenIf }{ returnIf }
41
42 %token
43         INTERFACE           { ITinterface }
44         USAGES_PART         { ITusages }
45         VERSIONS_PART       { ITversions }
46         EXPORTS_PART        { ITexports }
47         INSTANCE_MODULES_PART { ITinstance_modules }
48         INSTANCES_PART      { ITinstances }
49         FIXITIES_PART       { ITfixities }
50         DECLARATIONS_PART   { ITdeclarations }
51         PRAGMAS_PART        { ITpragmas }
52         DATA                { ITdata }
53         TYPE                { ITtype }
54         NEWTYPE             { ITnewtype }
55         DERIVING            { ITderiving }
56         CLASS               { ITclass }
57         WHERE               { ITwhere }
58         INSTANCE            { ITinstance }
59         INFIXL              { ITinfixl }
60         INFIXR              { ITinfixr }
61         INFIX               { ITinfix }
62         FORALL              { ITforall }
63         BANG                { ITbang }
64         VBAR                { ITvbar }
65         DCOLON              { ITdcolon }
66         COMMA               { ITcomma }
67         DARROW              { ITdarrow }
68         DOTDOT              { ITdotdot }
69         EQUAL               { ITequal }
70         OCURLY              { ITocurly }
71         OBRACK              { ITobrack }
72         OPAREN              { IToparen }
73         RARROW              { ITrarrow }
74         CCURLY              { ITccurly }
75         CBRACK              { ITcbrack }
76         CPAREN              { ITcparen }
77         SEMI                { ITsemi }
78
79         VARID               { ITvarid    $$ }
80         CONID               { ITconid    $$ }
81         VARSYM              { ITvarsym   $$ }
82         CONSYM              { ITconsym   $$ }
83         QVARID              { ITqvarid   $$ }
84         QCONID              { ITqconid   $$ }
85         QVARSYM             { ITqvarsym  $$ }
86         QCONSYM             { ITqconsym  $$ }
87
88         IDINFO_PART     { ITidinfo $$ }
89         TYPE_PART       { ITtysig $$ }
90         ARITY_PART      { ITarity }
91         STRICT_PART     { ITstrict }
92         UNFOLD_PART     { ITunfold $$ }
93         DEMAND          { ITdemand $$ }
94         BOTTOM          { ITbottom }
95         LAM             { ITlam }
96         BIGLAM          { ITbiglam }
97         CASE            { ITcase }
98         PRIM_CASE       { ITprim_case }
99         LET             { ITlet }
100         LETREC          { ITletrec }
101         IN              { ITin }
102         OF              { ITof }
103         COERCE_IN       { ITcoerce_in }
104         COERCE_OUT      { ITcoerce_out }
105         ATSIGN          { ITatsign }
106         CCALL           { ITccall $$ }
107         SCC             { ITscc $$ }
108
109         CHAR            { ITchar $$ }
110         STRING          { ITstring $$ } 
111         INTEGER         { ITinteger  $$ }
112         DOUBLE          { ITdouble $$ }
113
114         INTEGER_LIT     { ITinteger_lit }
115         FLOAT_LIT       { ITfloat_lit }
116         RATIONAL_LIT    { ITrational_lit }
117         ADDR_LIT        { ITaddr_lit }
118         LIT_LIT         { ITlit_lit }
119         STRING_LIT      { ITstring_lit }
120
121         UNKNOWN         { ITunknown $$ }
122 %%
123
124 iface           :: { ParsedIface }
125 iface           : INTERFACE CONID INTEGER
126                   inst_modules_part 
127                   usages_part
128                   exports_part fixities_part
129                   instances_part
130                   decls_part
131                   { ParsedIface 
132                         $2                      -- Module name
133                         (fromInteger $3)        -- Module version
134                         $5                      -- Usages
135                         $6                      -- Exports
136                         $4                      -- Instance modules
137                         $7                      -- Fixities
138                         $9                      -- Decls
139                         $8                      -- Local instances
140                     }
141
142
143 usages_part         :: { [ImportVersion OccName] }
144 usages_part         :  USAGES_PART module_stuff_pairs           { $2 }
145                     |                                           { [] }
146
147 module_stuff_pairs  :: { [ImportVersion OccName] }
148 module_stuff_pairs  :                                           { [] }
149                     |  module_stuff_pair module_stuff_pairs     { $1 : $2 }
150
151 module_stuff_pair   ::  { ImportVersion OccName }
152 module_stuff_pair   :  mod_name INTEGER DCOLON name_version_pairs SEMI
153                         { ($1, fromInteger $2, $4) }
154
155 versions_part       :: { [LocalVersion OccName] }
156 versions_part       :  VERSIONS_PART name_version_pairs         { $2 }
157                     |                                           { [] }
158
159 name_version_pairs  ::  { [LocalVersion OccName] }
160 name_version_pairs  :                                           { [] }
161                     |  name_version_pair name_version_pairs     { $1 : $2 }
162
163 name_version_pair   ::  { LocalVersion OccName }
164 name_version_pair   :  entity_occ INTEGER                       { ($1, fromInteger $2)
165 --------------------------------------------------------------------------
166                                                                 }
167
168 exports_part    :: { [ExportItem] }
169 exports_part    :  EXPORTS_PART export_items                    { $2 }
170                 |                                               { [] }
171
172 export_items    :: { [ExportItem] }
173 export_items    :                                               { [] }
174                 |  mod_name entities SEMI export_items          { ($1,$2) : $4 }
175
176 entities        :: { [(OccName, [OccName])] }
177 entities        :                                               { [] }
178                 |  entity entities                              { $1 : $2 }
179
180 entity          :: { (OccName, [OccName]) }
181 entity          :  entity_occ                                   { ($1, if isTCOcc $1 
182                                                                        then [$1]  {- AvailTC -}
183                                                                        else [])   {- Avail -} }
184                 |  entity_occ stuff_inside                      { ($1, ($1 : $2)) {- TyCls exported too -} }
185                 |  entity_occ BANG stuff_inside                 { ($1, $3)        {- TyCls not exported -} }
186
187 stuff_inside    :: { [OccName] }
188 stuff_inside    :  OPAREN val_occs1 CPAREN                      { $2
189 --------------------------------------------------------------------------
190                                                                 }
191
192 inst_modules_part :: { [Module] }
193 inst_modules_part :                                             { [] }
194                   |  INSTANCE_MODULES_PART mod_list             { $2 }
195
196 mod_list        :: { [Module] }
197 mod_list        :                                               { [] }
198                 |  mod_name mod_list                            { $1 : $2
199 --------------------------------------------------------------------------
200                                                                   }
201
202 fixities_part   :: { [(OccName,Fixity)] }
203 fixities_part   :                                               { [] }
204                 |  FIXITIES_PART fixes                          { $2 }
205
206 fixes           :: { [(OccName,Fixity)] }
207 fixes           :                                               { []  }
208                 |  fix fixes                                    { $1 : $2 }
209
210 fix             :: { (OccName, Fixity) }
211 fix             :  INFIXL INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixL) }
212                 |  INFIXR INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixR) }
213                 |  INFIX  INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixN)
214 --------------------------------------------------------------------------
215                                                                                       }
216
217 decls_part      :: { [(Version, RdrNameHsDecl)] }
218 decls_part      :                                       { [] }
219                 |       DECLARATIONS_PART topdecls      { $2 }
220
221 topdecls        :: { [(Version, RdrNameHsDecl)] }
222 topdecls        :                                       { [] }
223                 |  version topdecl topdecls             { ($1,$2) : $3 }
224
225 version         :: { Version }
226 version         :  INTEGER                              { fromInteger $1 }
227
228 topdecl         :: { RdrNameHsDecl }
229 topdecl         :  TYPE  tc_name tv_bndrs EQUAL type SEMI
230                         { TyD (TySynonym $2 $3 $5 mkIfaceSrcLoc) }
231                 |  DATA decl_context tc_name tv_bndrs constrs deriving SEMI
232                         { TyD (TyData DataType $2 $3 $4 $5 $6 noDataPragmas mkIfaceSrcLoc) }
233                 |  NEWTYPE decl_context tc_name tv_bndrs newtype_constr deriving SEMI
234                         { TyD (TyData NewType $2 $3 $4 $5 $6 noDataPragmas mkIfaceSrcLoc) }
235                 |  CLASS decl_context tc_name tv_bndr csigs SEMI
236                         { ClD (ClassDecl $2 $3 $4 $5 EmptyMonoBinds noClassPragmas mkIfaceSrcLoc) }
237                 |  var_name TYPE_PART id_info
238                         {
239                          let
240                           (Succeeded tp) = parseType $2
241                          in
242                          SigD (IfaceSig $1 tp $3 mkIfaceSrcLoc) }
243
244 id_info         :: { [HsIdInfo RdrName] }
245 id_info         :                               { [] }
246                 | IDINFO_PART   { let { (Succeeded id_info) = parseUnfolding $1 } in id_info}
247
248 decl_context    :: { RdrNameContext }
249 decl_context    :                                       { [] }
250                 | OCURLY context_list1 CCURLY DARROW    { $2 }
251
252
253 csigs           :: { [RdrNameSig] }
254 csigs           :                               { [] }
255                 | WHERE OCURLY csigs1 CCURLY    { $3 }
256
257 csigs1          :: { [RdrNameSig] }
258 csigs1          : csig                          { [$1] }
259                 | csig SEMI csigs1              { $1 : $3 }
260
261 csig            :: { RdrNameSig }
262 csig            :  var_name DCOLON type         { ClassOpSig $1 $1 $3 mkIfaceSrcLoc
263 ----------------------------------------------------------------
264                                                  }
265
266 constrs         :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
267                 :                               { [] }
268                 | EQUAL constrs1                { $2 }
269
270 constrs1        :: { [RdrNameConDecl] }
271 constrs1        :  constr               { [$1] }
272                 |  constr VBAR constrs1 { $1 : $3 }
273
274 constr          :: { RdrNameConDecl }
275 constr          :  data_name batypes                    { ConDecl $1 [] (VanillaCon $2) mkIfaceSrcLoc }
276                 |  data_name OCURLY fields1 CCURLY      { ConDecl $1 [] (RecCon $3)     mkIfaceSrcLoc }
277
278 newtype_constr  :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
279 newtype_constr  :                               { [] }
280                 | EQUAL data_name atype         { [ConDecl $2 [] (NewCon $3) mkIfaceSrcLoc] }
281
282 deriving        :: { Maybe [RdrName] }
283                 :                                       { Nothing }
284                 | DERIVING OPAREN tc_names1 CPAREN      { Just $3 }
285
286 batypes         :: { [RdrNameBangType] }
287 batypes         :                                       { [] }
288                 |  batype batypes                       { $1 : $2 }
289
290 batype          :: { RdrNameBangType }
291 batype          :  atype                                { Unbanged $1 }
292                 |  BANG atype                           { Banged   $2 }
293
294 fields1         :: { [([RdrName], RdrNameBangType)] }
295 fields1         : field                                 { [$1] }
296                 | field COMMA fields1                   { $1 : $3 }
297
298 field           :: { ([RdrName], RdrNameBangType) }
299 field           :  var_names1 DCOLON type               { ($1, Unbanged $3) }
300                 |  var_names1 DCOLON BANG type          { ($1, Banged   $4)
301 --------------------------------------------------------------------------
302                                                         }
303
304 forall          :: { [HsTyVar RdrName] }
305 forall          : OBRACK tv_bndrs CBRACK                { $2 }
306
307 context         :: { RdrNameContext }
308 context         :                                       { [] }
309                 | OCURLY context_list1 CCURLY           { $2 }
310
311 context_list1   :: { RdrNameContext }
312 context_list1   : class                                 { [$1] }
313                 | class COMMA context_list1             { $1 : $3 }
314
315 class           :: { (RdrName, RdrNameHsType) }
316 class           :  tc_name atype                        { ($1, $2) }
317
318 type            :: { RdrNameHsType }
319 type            : FORALL forall context DARROW type     { mkHsForAllTy $2 $3 $5 }
320                 |  btype RARROW type                    { MonoFunTy $1 $3 }
321                 |  btype                                { $1 }
322
323 types2          :: { [RdrNameHsType]                    {- Two or more -}  }    
324 types2          :  type COMMA type                      { [$1,$3] }
325                 |  type COMMA types2                    { $1 : $3 }
326
327 btype           :: { RdrNameHsType }
328 btype           :  atype                                { $1 }
329                 |  btype atype                          { MonoTyApp $1 $2 }
330
331 atype           :: { RdrNameHsType }
332 atype           :  tc_name                              { MonoTyVar $1 }
333                 |  tv_name                              { MonoTyVar $1 }
334                 |  OPAREN types2 CPAREN                 { MonoTupleTy dummyRdrTcName $2 }
335                 |  OBRACK type CBRACK                   { MonoListTy  dummyRdrTcName $2 }
336                 |  OCURLY tc_name atype CCURLY          { MonoDictTy $2 $3 }
337                 |  OPAREN type CPAREN                   { $2 }
338
339 atypes          :: { [RdrNameHsType]    {-  Zero or more -} }
340 atypes          :                                       { [] }
341                 |  atype atypes                         { $1 : $2
342 ---------------------------------------------------------------------
343                                                         }
344
345 mod_name        :: { Module }
346                 :  CONID                { $1 }
347
348 var_occ         :: { OccName }
349 var_occ         : VARID                 { VarOcc $1 }
350                 | VARSYM                { VarOcc $1 }
351                 | BANG                  { VarOcc SLIT("!") {-sigh, double-sigh-} }
352
353 tc_occ          :: { OccName }
354 tc_occ          :  CONID                { TCOcc $1 }
355                 |  CONSYM               { TCOcc $1 }
356                 |  OPAREN RARROW CPAREN { TCOcc SLIT("->") }
357
358 entity_occ      :: { OccName }
359 entity_occ      :  var_occ              { $1 }
360                 |  tc_occ               { $1 }
361                 |  RARROW               { TCOcc SLIT("->") {- Allow un-paren'd arrow -} }
362
363 val_occ         :: { OccName }
364 val_occ         :  var_occ              { $1 }
365                 |  CONID                { VarOcc $1 }
366                 |  CONSYM               { VarOcc $1 }
367
368 val_occs1       :: { [OccName] }
369                 :  val_occ              { [$1] }
370                 |  val_occ val_occs1    { $1 : $2 }
371
372
373 qvar_name       :: { RdrName }
374                 :  QVARID               { varQual $1 }
375                 |  QVARSYM              { varQual $1 }
376
377 var_name        :: { RdrName }
378 var_name        :  var_occ              { Unqual $1 }
379
380 var_names1      :: { [RdrName] }
381 var_names1      : var_name              { [$1] }
382                 | var_name var_names1   { $1 : $2 }
383
384 any_var_name    :: {RdrName}
385 any_var_name    :  var_name             { $1 }
386                 |  qvar_name            { $1 }
387
388 qdata_name      :: { RdrName }
389 qdata_name      :  QCONID               { varQual $1 }
390                 |  QCONSYM              { varQual $1 }
391
392 data_name       :: { RdrName }
393 data_name       :  CONID                { Unqual (VarOcc $1) }
394                 |  CONSYM               { Unqual (VarOcc $1) }
395
396
397 tc_names1       :: { [RdrName] }
398                 : tc_name                       { [$1] }
399                 | tc_name COMMA tc_names1       { $1 : $3 }
400
401 tc_name         :: { RdrName }
402 tc_name         : tc_occ                        { Unqual $1 }
403                 | QCONID                        { tcQual $1 }
404
405 tv_name         :: { RdrName }
406 tv_name         :  VARID                { Unqual (TvOcc $1) }
407
408 tv_names        :: { [RdrName] }
409                 :                       { [] }
410                 | tv_name tv_names      { $1 : $2 }
411
412 tv_bndr         :: { HsTyVar RdrName }
413 tv_bndr         :  tv_name DCOLON akind { IfaceTyVar $1 $3 }
414                 |  tv_name              { UserTyVar $1 }
415
416 tv_bndrs        :: { [HsTyVar RdrName] }
417                 :                       { [] }
418                 | tv_bndr tv_bndrs      { $1 : $2 }
419
420 kind            :: { Kind }
421                 : akind                 { $1 }
422                 | akind RARROW kind     { mkArrowKind $1 $3 }
423
424 akind           :: { Kind }
425                 : VARSYM                { mkBoxedTypeKind {- ToDo: check that it's "*" -} }
426                 | OPAREN kind CPAREN    { $2
427 --------------------------------------------------------------------------
428                                         }
429
430
431 instances_part  :: { [RdrNameInstDecl] }
432 instances_part  :  INSTANCES_PART instdecls { $2 }
433                 |                           { [] }
434
435 instdecls       :: { [RdrNameInstDecl] }
436 instdecls       :                           { [] }
437                 |  instd instdecls          { $1 : $2 }
438
439 instd           :: { RdrNameInstDecl }
440 instd           :  INSTANCE type EQUAL var_name SEMI 
441                         { InstDecl $2
442                                    EmptyMonoBinds       {- No bindings -}
443                                    []                   {- No user pragmas -}
444                                    (Just $4)            {- Dfun id -}
445                                    mkIfaceSrcLoc 
446 --------------------------------------------------------------------------
447                     }