2 #include "HsVersions.h"
4 module ParseIface ( parseIface ) where
8 import HsSyn -- quite a bit of stuff
9 import RdrHsSyn -- oodles of synonyms
10 import HsDecls ( HsIdInfo(..) )
11 import HsTypes ( mkHsForAllTy )
14 import HsPragmas ( noGenPragmas, noDataPragmas, noClassPragmas, noClassOpPragmas, noInstancePragmas )
15 import IdInfo ( exactArity, mkStrictnessInfo, mkBottomStrictnessInfo,
16 ArgUsageInfo, FBTypeInfo
18 import Kind ( Kind, mkArrowKind, mkTypeKind )
21 import RnMonad ( SYN_IE(ImportVersion), SYN_IE(LocalVersion), ParsedIface(..),
22 SYN_IE(RdrNamePragma), SYN_IE(ExportItem)
24 import Bag ( emptyBag, unitBag, snocBag )
25 import FiniteMap ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
26 import Name ( OccName(..), Provenance )
27 import SrcLoc ( mkIfaceSrcLoc )
28 import Util ( panic{-, pprPanic ToDo:rm-} )
31 -----------------------------------------------------------------
33 parseIface = parseIToks . lexIface
35 -----------------------------------------------------------------
39 %tokentype { IfaceToken }
40 %monad { IfM }{ thenIf }{ returnIf }
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 }
61 DERIVING { ITderiving }
68 INSTANCE { ITinstance }
78 INTEGER { ITinteger $$ }
81 VARSYM { ITvarsym $$ }
82 CONSYM { ITconsym $$ }
83 QVARID { ITqvarid $$ }
84 QCONID { ITqconid $$ }
85 QVARSYM { ITqvarsym $$ }
86 QCONSYM { ITqconsym $$ }
88 ARITY_PART { ITarity }
89 STRICT_PART { ITstrict }
90 UNFOLD_PART { ITunfold }
91 DEMAND { ITdemand $$ }
96 PRIM_CASE { ITprim_case }
102 COERCE_IN { ITcoerce_in }
103 COERCE_OUT { ITcoerce_out }
105 STRING { ITstring $$ }
106 DOUBLE { ITdouble $$ }
107 INTEGER_LIT { ITinteger_lit }
108 STRING_LIT { ITstring_lit }
109 FLOAT_LIT { ITfloat_lit }
110 RATIONAL_LIT { ITrational_lit }
111 ADDR_LIT { ITaddr_lit }
112 LIT_LIT { ITlit_lit }
116 iface :: { ParsedIface }
117 iface : INTERFACE CONID INTEGER
120 exports_part fixities_part
125 (fromInteger $3) -- Module version
128 $4 -- Instance modules
131 $8 -- Local instances
135 usages_part :: { [ImportVersion OccName] }
136 usages_part : USAGES_PART module_stuff_pairs { $2 }
139 module_stuff_pairs :: { [ImportVersion OccName] }
140 module_stuff_pairs : { [] }
141 | module_stuff_pair module_stuff_pairs { $1 : $2 }
143 module_stuff_pair :: { ImportVersion OccName }
144 module_stuff_pair : mod_name INTEGER DCOLON name_version_pairs SEMI
145 { ($1, fromInteger $2, $4) }
147 versions_part :: { [LocalVersion OccName] }
148 versions_part : VERSIONS_PART name_version_pairs { $2 }
151 name_version_pairs :: { [LocalVersion OccName] }
152 name_version_pairs : { [] }
153 | name_version_pair name_version_pairs { $1 : $2 }
155 name_version_pair :: { LocalVersion OccName }
156 name_version_pair : entity_occ INTEGER { ($1, fromInteger $2)
157 --------------------------------------------------------------------------
160 exports_part :: { [ExportItem] }
161 exports_part : EXPORTS_PART export_items { $2 }
164 export_items :: { [ExportItem] }
165 export_items : { [] }
166 | mod_name entities SEMI export_items { ($1,$2) : $4 }
168 entities :: { [(OccName, [OccName])] }
170 | entity entities { $1 : $2 }
172 entity :: { (OccName, [OccName]) }
173 entity : entity_occ maybe_inside { ($1, $2) }
175 maybe_inside :: { [OccName] }
176 maybe_inside : { [] }
177 | OPAREN val_occs CPAREN { $2
178 --------------------------------------------------------------------------
181 inst_modules_part :: { [Module] }
182 inst_modules_part : { [] }
183 | INSTANCE_MODULES_PART mod_list { $2 }
185 mod_list :: { [Module] }
187 | mod_name mod_list { $1 : $2
188 --------------------------------------------------------------------------
191 fixities_part :: { [(OccName,Fixity)] }
192 fixities_part : { [] }
193 | FIXITIES_PART fixes { $2 }
195 fixes :: { [(OccName,Fixity)] }
197 | fix fixes { $1 : $2 }
199 fix :: { (OccName, Fixity) }
200 fix : INFIXL INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixL) }
201 | INFIXR INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixR) }
202 | INFIX INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixN)
203 --------------------------------------------------------------------------
206 decls_part :: { [(Version, RdrNameHsDecl)] }
208 | DECLARATIONS_PART topdecls { $2 }
210 topdecls :: { [(Version, RdrNameHsDecl)] }
212 | version topdecl topdecls { ($1,$2) : $3 }
214 version :: { Version }
215 version : INTEGER { fromInteger $1 }
217 topdecl :: { RdrNameHsDecl }
218 topdecl : TYPE tc_name tv_bndrs EQUAL type SEMI
219 { TyD (TySynonym $2 $3 $5 mkIfaceSrcLoc) }
220 | DATA decl_context tc_name tv_bndrs EQUAL constrs deriving SEMI
221 { TyD (TyData $2 $3 $4 $6 $7 noDataPragmas mkIfaceSrcLoc) }
222 | NEWTYPE decl_context tc_name tv_bndrs EQUAL constr1 deriving SEMI
223 { TyD (TyNew $2 $3 $4 $6 $7 noDataPragmas mkIfaceSrcLoc) }
224 | CLASS decl_context tc_name tv_bndr csigs SEMI
225 { ClD (ClassDecl $2 $3 $4 $5 EmptyMonoBinds noClassPragmas mkIfaceSrcLoc) }
226 | var_name DCOLON type id_info SEMI
227 { SigD (IfaceSig $1 $3 $4 mkIfaceSrcLoc) }
229 decl_context :: { RdrNameContext }
230 decl_context : { [] }
231 | OCURLY context_list1 CCURLY DARROW { $2 }
233 csigs :: { [RdrNameSig] }
235 | WHERE OCURLY csigs1 CCURLY { $3 }
237 csigs1 :: { [RdrNameSig] }
238 csigs1 : csig { [$1] }
239 | csig SEMI csigs1 { $1 : $3 }
241 csig :: { RdrNameSig }
242 csig : var_name DCOLON type { ClassOpSig $1 $3 noClassOpPragmas mkIfaceSrcLoc
243 ----------------------------------------------------------------
246 constrs :: { [RdrNameConDecl] }
247 constrs : constr { [$1] }
248 | constr VBAR constrs { $1 : $3 }
250 constr :: { RdrNameConDecl }
251 constr : data_name batypes { ConDecl $1 $2 mkIfaceSrcLoc }
252 | data_name OCURLY fields1 CCURLY { RecConDecl $1 $3 mkIfaceSrcLoc }
254 constr1 :: { RdrNameConDecl {- For a newtype -} }
255 constr1 : data_name atype { NewConDecl $1 $2 mkIfaceSrcLoc }
257 deriving :: { Maybe [RdrName] }
259 | DERIVING OPAREN qtc_names1 CPAREN { Just $3 }
261 batypes :: { [RdrNameBangType] }
263 | batype batypes { $1 : $2 }
265 batype :: { RdrNameBangType }
266 batype : atype { Unbanged $1 }
267 | BANG atype { Banged $2 }
269 fields1 :: { [([RdrName], RdrNameBangType)] }
270 fields1 : field { [$1] }
271 | field COMMA fields1 { $1 : $3 }
273 field :: { ([RdrName], RdrNameBangType) }
274 field : var_name DCOLON type { ([$1], Unbanged $3) }
275 | var_name DCOLON BANG type { ([$1], Banged $4)
276 --------------------------------------------------------------------------
279 forall :: { [HsTyVar RdrName] }
280 forall : OBRACK tv_bndrs CBRACK { $2 }
282 context :: { RdrNameContext }
284 | OCURLY context_list1 CCURLY { $2 }
286 context_list1 :: { RdrNameContext }
287 context_list1 : class { [$1] }
288 | class COMMA context_list1 { $1 : $3 }
290 class :: { (RdrName, RdrNameHsType) }
291 class : qtc_name atype { ($1, $2) }
293 type :: { RdrNameHsType }
294 type : FORALL forall context DARROW tautype { mkHsForAllTy $2 $3 $5 }
297 tautype :: { RdrNameHsType }
298 tautype : btype { $1 }
299 | btype RARROW tautype { MonoFunTy $1 $3 }
301 types2 :: { [RdrNameHsType] {- Two or more -} }
302 types2 : type COMMA type { [$1,$3] }
303 | type COMMA types2 { $1 : $3 }
305 btype :: { RdrNameHsType }
307 | qtc_name atype atypes { MonoTyApp $1 ($2:$3) }
308 | tv_name atype atypes { MonoTyApp $1 ($2:$3) }
310 atype :: { RdrNameHsType }
311 atype : qtc_name { MonoTyApp $1 [] }
312 | tv_name { MonoTyVar $1 }
313 | OPAREN types2 CPAREN { MonoTupleTy dummyRdrTcName $2 }
314 | OBRACK type CBRACK { MonoListTy dummyRdrTcName $2 }
315 | OCURLY qtc_name atype CCURLY { MonoDictTy $2 $3 }
316 | OPAREN type CPAREN { $2 }
318 atypes :: { [RdrNameHsType] {- Zero or more -} }
320 | atype atypes { $1 : $2
321 ---------------------------------------------------------------------
324 mod_name :: { Module }
327 var_occ :: { OccName }
328 var_occ : VARID { VarOcc $1 }
329 | VARSYM { VarOcc $1 }
330 | BANG { VarOcc SLIT("!") {-sigh, double-sigh-} }
332 entity_occ :: { OccName }
333 entity_occ : var_occ { $1 }
335 | CONSYM { TCOcc $1 }
337 val_occ :: { OccName }
338 val_occ : var_occ { $1 }
339 | CONID { VarOcc $1 }
340 | CONSYM { VarOcc $1 }
342 val_occs :: { [OccName] }
344 | val_occ val_occs { $1 : $2 }
347 qvar_name :: { RdrName }
348 : QVARID { varQual $1 }
349 | QVARSYM { varQual $1 }
351 var_name :: { RdrName }
352 var_name : var_occ { Unqual $1 }
354 any_var_name :: {RdrName}
355 any_var_name : var_name { $1 }
358 qdata_name :: { RdrName }
359 qdata_name : QCONID { varQual $1 }
360 | QCONSYM { varQual $1 }
362 data_name :: { RdrName }
363 data_name : CONID { Unqual (VarOcc $1) }
364 | CONSYM { Unqual (VarOcc $1) }
367 qtc_name :: { RdrName }
368 qtc_name : QCONID { tcQual $1 }
370 qtc_names1 :: { [RdrName] }
372 | qtc_name COMMA qtc_names1 { $1 : $3 }
374 tc_name :: { RdrName }
375 tc_name : CONID { Unqual (TCOcc $1) }
378 tv_name :: { RdrName }
379 tv_name : VARID { Unqual (TvOcc $1) }
381 tv_names :: { [RdrName] }
383 | tv_name tv_names { $1 : $2 }
385 tv_bndr :: { HsTyVar RdrName }
386 tv_bndr : tv_name DCOLON akind { IfaceTyVar $1 $3 }
387 | tv_name { UserTyVar $1 }
389 tv_bndrs :: { [HsTyVar RdrName] }
391 | tv_bndr tv_bndrs { $1 : $2 }
395 | akind RARROW kind { mkArrowKind $1 $3 }
398 : VARSYM { mkTypeKind {- ToDo: check that it's "*" -} }
399 | OPAREN kind CPAREN { $2
400 --------------------------------------------------------------------------
404 instances_part :: { [RdrNameInstDecl] }
405 instances_part : INSTANCES_PART instdecls { $2 }
408 instdecls :: { [RdrNameInstDecl] }
410 | instd instdecls { $1 : $2 }
412 instd :: { RdrNameInstDecl }
413 instd : INSTANCE type EQUAL var_name SEMI
415 EmptyMonoBinds {- No bindings -}
416 [] {- No user pragmas -}
417 (Just $4) {- Dfun id -}
419 --------------------------------------------------------------------------
422 id_info :: { [HsIdInfo RdrName] }
424 | id_info_item id_info { $1 : $2 }
426 id_info_item :: { HsIdInfo RdrName }
427 id_info_item : ARITY_PART arity_info { HsArity $2 }
428 | STRICT_PART strict_info { HsStrictness $2 }
429 | BOTTOM { HsStrictness mkBottomStrictnessInfo }
430 | UNFOLD_PART core_expr { HsUnfold $2 }
432 arity_info :: { ArityInfo }
433 arity_info : INTEGER { exactArity (fromInteger $1) }
435 strict_info :: { StrictnessInfo RdrName }
436 strict_info : DEMAND any_var_name { mkStrictnessInfo $1 (Just $2) }
437 | DEMAND { mkStrictnessInfo $1 Nothing }
439 core_expr :: { UfExpr RdrName }
440 core_expr : any_var_name { UfVar $1 }
441 | qdata_name { UfVar $1 }
442 | core_lit { UfLit $1 }
443 | OPAREN core_expr CPAREN { $2 }
445 | core_expr ATSIGN atype { UfApp $1 (UfTyArg $3) }
446 | core_expr core_arg { UfApp $1 $2 }
447 | LAM core_val_bndrs RARROW core_expr { foldr UfLam $4 $2 }
448 | BIGLAM core_tv_bndrs RARROW core_expr { foldr UfLam $4 $2 }
451 OCURLY alg_alts core_default CCURLY { UfCase $2 (UfAlgAlts $5 $6) }
452 | PRIM_CASE core_expr OF
453 OCURLY prim_alts core_default CCURLY { UfCase $2 (UfPrimAlts $5 $6) }
456 | LET OCURLY core_val_bndr EQUAL core_expr CCURLY
457 IN core_expr { UfLet (UfNonRec $3 $5) $8 }
458 | LETREC OCURLY rec_binds CCURLY
459 IN core_expr { UfLet (UfRec $3) $6 }
461 | coerce atype core_expr { UfCoerce $1 $2 $3 }
464 OBRACK atype atypes CBRACK core_args { let
465 (is_casm, may_gc) = $1
467 UfPrim (UfCCallOp $2 is_casm may_gc $5 $4)
471 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
473 | core_val_bndr EQUAL core_expr SEMI rec_binds { ($1,$3) : $5 }
475 coerce :: { UfCoercion RdrName }
476 coerce : COERCE_IN qdata_name { UfIn $2 }
477 | COERCE_OUT qdata_name { UfOut $2 }
479 prim_alts :: { [(Literal,UfExpr RdrName)] }
481 | core_lit RARROW core_expr SEMI prim_alts { ($1,$3) : $5 }
483 alg_alts :: { [(RdrName, [UfBinder RdrName], UfExpr RdrName)] }
485 | qdata_name core_val_bndrs RARROW
486 core_expr SEMI alg_alts { ($1,$2,$4) : $6 }
488 core_default :: { UfDefault RdrName }
490 | core_val_bndr RARROW core_expr SEMI { UfBindDefault $1 $3 }
492 core_arg :: { UfArg RdrName }
493 : var_name { UfVarArg $1 }
494 | qvar_name { UfVarArg $1 }
495 | qdata_name { UfVarArg $1 }
496 | core_lit { UfLitArg $1 }
498 core_args :: { [UfArg RdrName] }
500 | core_arg core_args { $1 : $2 }
502 core_lit :: { Literal }
503 core_lit : INTEGER { MachInt $1 True }
504 | CHAR { MachChar $1 }
505 | STRING { MachStr $1 }
506 | STRING_LIT STRING { NoRepStr $2 }
507 | DOUBLE { MachDouble (toRational $1) }
508 | FLOAT_LIT DOUBLE { MachFloat (toRational $2) }
510 | INTEGER_LIT INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
511 -- The type checker will add the types
514 | RATIONAL_LIT INTEGER INTEGER { NoRepRational ($2 % $3)
515 (panic "NoRepRational type")
516 -- The type checker will add the type
519 | ADDR_LIT INTEGER { MachAddr $2 }
520 | LIT_LIT STRING { MachLitLit $2 (panic "ParseIface.y: ToDo: need PrimRep on LitLits in ifaces") }
522 core_val_bndr :: { UfBinder RdrName }
523 core_val_bndr : var_name DCOLON atype { UfValBinder $1 $3 }
525 core_val_bndrs :: { [UfBinder RdrName] }
526 core_val_bndrs : { [] }
527 | core_val_bndr core_val_bndrs { $1 : $2 }
529 core_tv_bndr :: { UfBinder RdrName }
530 core_tv_bndr : tv_name DCOLON akind { UfTyBinder $1 $3 }
531 | tv_name { UfTyBinder $1 mkTypeKind }
533 core_tv_bndrs :: { [UfBinder RdrName] }
534 core_tv_bndrs : { [] }
535 | core_tv_bndr core_tv_bndrs { $1 : $2 }
537 ccall_string :: { FAST_STRING }