2 module ParseIface ( parseIface, IfaceStuff(..) ) where
4 #include "HsVersions.h"
6 import HsSyn -- quite a bit of stuff
7 import RdrHsSyn -- oodles of synonyms
8 import HsDecls ( HsIdInfo(..), HsStrictnessInfo(..) )
9 import HsTypes ( mkHsForAllTy )
12 import BasicTypes ( IfaceFlavour(..), Fixity(..), FixityDirection(..), NewOrData(..), Version )
13 import HsPragmas ( noDataPragmas, noClassPragmas )
14 import Kind ( Kind, mkArrowKind, mkBoxedTypeKind, mkTypeKind )
15 import IdInfo ( ArgUsageInfo, FBTypeInfo, ArityInfo, exactArity )
16 import PrimRep ( decodePrimRep )
19 import RnMonad ( ImportVersion, LocalVersion, ParsedIface(..), WhatsImported(..),
20 RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..)
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 ( SrcLoc )
32 %tokentype { IfaceToken }
33 %monad { IfM }{ thenIf }{ returnIf }
34 %lexer { lexIface } { ITeof }
37 INTERFACE { ITinterface }
38 USAGES_PART { ITusages }
39 EXPORTS_PART { ITexports }
40 INSTANCE_MODULES_PART { ITinstance_modules }
41 INSTANCES_PART { ITinstances }
42 FIXITIES_PART { ITfixities }
43 DECLARATIONS_PART { ITdeclarations }
49 INSTANCE { ITinstance }
71 VARSYM { ITvarsym $$ }
72 CONSYM { ITconsym $$ }
73 QVARID { ITqvarid $$ }
74 QCONID { ITqconid $$ }
75 QVARSYM { ITqvarsym $$ }
76 QCONSYM { ITqconsym $$ }
78 STRICT_PART { ITstrict $$ }
79 TYPE_PART { ITtysig _ _ }
80 ARITY_PART { ITarity }
81 UNFOLD_PART { ITunfold $$ }
82 SPECIALISE { ITspecialise }
87 PRIM_CASE { ITprim_case }
96 INLINE_CALL { ITinline }
99 STRING { ITstring $$ }
100 INTEGER { ITinteger $$ }
101 RATIONAL { ITrational $$ }
103 INTEGER_LIT { ITinteger_lit }
104 FLOAT_LIT { ITfloat_lit }
105 RATIONAL_LIT { ITrational_lit }
106 ADDR_LIT { ITaddr_lit }
107 LIT_LIT { ITlit_lit }
108 STRING_LIT { ITstring_lit }
110 UNKNOWN { ITunknown $$ }
113 -- iface_stuff is the main production.
114 -- It recognises (a) a whole interface file
115 -- (b) a type (so that type sigs can be parsed lazily)
116 -- (c) the IdInfo part of a signature (same reason)
118 iface_stuff :: { IfaceStuff }
119 iface_stuff : iface { PIface $1 }
121 | id_info { PIdInfo $1 }
124 iface :: { ParsedIface }
125 iface : INTERFACE CONID INTEGER checkVersion
128 exports_part fixities_part
133 (fromInteger $3) -- Module version
136 $5 -- Instance modules
139 $9 -- Local instances
143 usages_part :: { [ImportVersion OccName] }
144 usages_part : USAGES_PART module_stuff_pairs { $2 }
147 module_stuff_pairs :: { [ImportVersion OccName] }
148 module_stuff_pairs : { [] }
149 | module_stuff_pair module_stuff_pairs { $1 : $2 }
151 module_stuff_pair :: { ImportVersion OccName }
152 module_stuff_pair : mod_name opt_bang INTEGER DCOLON whats_imported SEMI
153 { ($1, $2, fromInteger $3, $5) }
155 whats_imported :: { WhatsImported OccName }
156 whats_imported : { Everything }
157 | name_version_pair name_version_pairs { Specifically ($1:$2) }
159 name_version_pairs :: { [LocalVersion OccName] }
160 name_version_pairs : { [] }
161 | name_version_pair name_version_pairs { $1 : $2 }
163 name_version_pair :: { LocalVersion OccName }
164 name_version_pair : entity_occ INTEGER { ($1, fromInteger $2)
165 --------------------------------------------------------------------------
168 exports_part :: { [ExportItem] }
169 exports_part : EXPORTS_PART export_items { $2 }
172 export_items :: { [ExportItem] }
173 export_items : { [] }
174 | opt_bang mod_name entities SEMI export_items { ($2,$1,$3) : $5 }
176 opt_bang :: { IfaceFlavour }
177 opt_bang : { HiFile }
178 | BANG { HiBootFile }
180 entities :: { [RdrAvailInfo] }
182 | entity entities { $1 : $2 }
184 entity :: { RdrAvailInfo }
185 entity : entity_occ { if isTCOcc $1
188 | entity_occ stuff_inside { AvailTC $1 ($1:$2) }
189 | entity_occ VBAR stuff_inside { AvailTC $1 $3 }
191 stuff_inside :: { [OccName] }
192 stuff_inside : OPAREN val_occs CPAREN { $2
193 --------------------------------------------------------------------------
196 inst_modules_part :: { [Module] }
197 inst_modules_part : { [] }
198 | INSTANCE_MODULES_PART mod_list { $2 }
200 mod_list :: { [Module] }
202 | mod_name mod_list { $1 : $2
203 --------------------------------------------------------------------------
206 fixities_part :: { [(OccName,Fixity)] }
207 fixities_part : { [] }
208 | FIXITIES_PART fixes { $2 }
210 fixes :: { [(OccName,Fixity)] }
212 | fix fixes { $1 : $2 }
214 fix :: { (OccName, Fixity) }
215 fix : INFIXL INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixL) }
216 | INFIXR INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixR) }
217 | INFIX INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixN)
218 --------------------------------------------------------------------------
221 decls_part :: { [(Version, RdrNameHsDecl)] }
223 | DECLARATIONS_PART topdecls { $2 }
225 topdecls :: { [(Version, RdrNameHsDecl)] }
227 | version topdecl topdecls { ($1,$2) : $3 }
229 version :: { Version }
230 version : INTEGER { fromInteger $1 }
232 topdecl :: { RdrNameHsDecl }
233 topdecl : src_loc TYPE tc_name tv_bndrs EQUAL type SEMI
234 { TyD (TySynonym $3 $4 $6 $1) }
235 | src_loc DATA decl_context tc_name tv_bndrs constrs SEMI
236 { TyD (TyData DataType $3 $4 $5 $6 Nothing noDataPragmas $1) }
237 | src_loc NEWTYPE decl_context tc_name tv_bndrs newtype_constr SEMI
238 { TyD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
239 | src_loc CLASS decl_context tc_name tv_bndrs csigs SEMI
240 { ClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds noClassPragmas $1) }
241 | src_loc var_name TYPE_PART
244 ITtysig sig idinfo_part -> -- Parse type and idinfo lazily
248 Just s -> case parseIface s $1 of
249 Succeeded (PIdInfo id_info) -> id_info
250 other -> pprPanic "IdInfo parse failed"
253 tp = case parseIface sig $1 of
254 Succeeded (PType tp) -> tp
255 other -> pprPanic "Id type parse failed"
258 SigD (IfaceSig $2 tp info $1) }
260 decl_context :: { RdrNameContext }
261 decl_context : { [] }
262 | OCURLY context_list1 CCURLY DARROW { $2 }
265 csigs :: { [RdrNameSig] }
267 | WHERE OCURLY csigs1 CCURLY { $3 }
269 csigs1 :: { [RdrNameSig] }
270 csigs1 : csig { [$1] }
271 | csig SEMI csigs1 { $1 : $3 }
273 csig :: { RdrNameSig }
274 csig : src_loc var_name DCOLON type { ClassOpSig $2 Nothing $4 $1 }
275 | src_loc var_name EQUAL DCOLON type { ClassOpSig $2
276 (Just (error "Un-filled-in default method"))
278 ----------------------------------------------------------------
281 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
283 | EQUAL constrs1 { $2 }
285 constrs1 :: { [RdrNameConDecl] }
286 constrs1 : constr { [$1] }
287 | constr VBAR constrs1 { $1 : $3 }
289 constr :: { RdrNameConDecl }
290 constr : src_loc data_name batypes { ConDecl $2 [] (VanillaCon $3) $1 }
291 | src_loc data_name OCURLY fields1 CCURLY { ConDecl $2 [] (RecCon $4) $1 }
293 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
294 newtype_constr : { [] }
295 | src_loc EQUAL data_name atype { [ConDecl $3 [] (NewCon $4) $1] }
297 batypes :: { [RdrNameBangType] }
299 | batype batypes { $1 : $2 }
301 batype :: { RdrNameBangType }
302 batype : atype { Unbanged $1 }
303 | BANG atype { Banged $2 }
305 fields1 :: { [([RdrName], RdrNameBangType)] }
306 fields1 : field { [$1] }
307 | field COMMA fields1 { $1 : $3 }
309 field :: { ([RdrName], RdrNameBangType) }
310 field : var_names1 DCOLON type { ($1, Unbanged $3) }
311 | var_names1 DCOLON BANG type { ($1, Banged $4) }
312 --------------------------------------------------------------------------
314 type :: { RdrNameHsType }
315 type : FORALL forall context DARROW type { mkHsForAllTy $2 $3 $5 }
316 | btype RARROW type { MonoFunTy $1 $3 }
319 forall :: { [HsTyVar RdrName] }
320 forall : OBRACK tv_bndrs CBRACK { $2 }
322 context :: { RdrNameContext }
324 | OCURLY context_list1 CCURLY { $2 }
326 context_list1 :: { RdrNameContext }
327 context_list1 : class { [$1] }
328 | class COMMA context_list1 { $1 : $3 }
330 class :: { (RdrName, [RdrNameHsType]) }
331 class : tc_name atypes { ($1, $2) }
333 types2 :: { [RdrNameHsType] {- Two or more -} }
334 types2 : type COMMA type { [$1,$3] }
335 | type COMMA types2 { $1 : $3 }
337 btype :: { RdrNameHsType }
339 | btype atype { MonoTyApp $1 $2 }
341 atype :: { RdrNameHsType }
342 atype : tc_name { MonoTyVar $1 }
343 | tv_name { MonoTyVar $1 }
344 | OPAREN types2 CPAREN { MonoTupleTy dummyRdrTcName $2 }
345 | OBRACK type CBRACK { MonoListTy dummyRdrTcName $2 }
346 | OCURLY tc_name atypes CCURLY { MonoDictTy $2 $3 }
347 | OPAREN type CPAREN { $2 }
349 atypes :: { [RdrNameHsType] {- Zero or more -} }
351 | atype atypes { $1 : $2 }
352 ---------------------------------------------------------------------
354 mod_name :: { Module }
357 var_occ :: { OccName }
358 var_occ : VARID { VarOcc $1 }
359 | VARSYM { VarOcc $1 }
360 | BANG { VarOcc SLIT("!") {-sigh, double-sigh-} }
362 tc_occ :: { OccName }
363 tc_occ : CONID { TCOcc $1 }
364 | CONSYM { TCOcc $1 }
365 | OPAREN RARROW CPAREN { TCOcc SLIT("->") }
367 entity_occ :: { OccName }
368 entity_occ : var_occ { $1 }
370 | RARROW { TCOcc SLIT("->") {- Allow un-paren'd arrow -} }
372 val_occ :: { OccName }
373 val_occ : var_occ { $1 }
374 | CONID { VarOcc $1 }
375 | CONSYM { VarOcc $1 }
377 val_occs :: { [OccName] }
379 | val_occ val_occs { $1 : $2 }
382 var_name :: { RdrName }
383 var_name : var_occ { Unqual $1 }
385 qvar_name :: { RdrName }
386 qvar_name : var_name { $1 }
387 | QVARID { lexVarQual $1 }
388 | QVARSYM { lexVarQual $1 }
390 var_names :: { [RdrName] }
392 | var_name var_names { $1 : $2 }
394 var_names1 :: { [RdrName] }
395 var_names1 : var_name var_names { $1 : $2 }
397 data_name :: { RdrName }
398 data_name : CONID { Unqual (VarOcc $1) }
399 | CONSYM { Unqual (VarOcc $1) }
401 qdata_name :: { RdrName }
402 qdata_name : data_name { $1 }
403 | QCONID { lexVarQual $1 }
404 | QCONSYM { lexVarQual $1 }
406 qdata_names :: { [RdrName] }
408 | qdata_name qdata_names { $1 : $2 }
410 tc_name :: { RdrName }
411 tc_name : tc_occ { Unqual $1 }
412 | QCONID { lexTcQual $1 }
413 | QCONSYM { lexTcQual $1 }
415 tc_names1 :: { [RdrName] }
417 | tc_name COMMA tc_names1 { $1 : $3 }
419 tv_name :: { RdrName }
420 tv_name : VARID { Unqual (TvOcc $1) }
421 | VARSYM { Unqual (TvOcc $1) {- Allow t2 as a tyvar -} }
423 tv_bndr :: { HsTyVar RdrName }
424 tv_bndr : tv_name DCOLON akind { IfaceTyVar $1 $3 }
425 | tv_name { UserTyVar $1 }
427 tv_bndrs :: { [HsTyVar RdrName] }
429 | tv_bndr tv_bndrs { $1 : $2 }
433 | akind RARROW kind { mkArrowKind $1 $3 }
436 : VARSYM { if $1 == SLIT("*") then
438 else if $1 == SLIT("**") then
440 else panic "ParseInterface: akind"
442 | OPAREN kind CPAREN { $2 }
443 --------------------------------------------------------------------------
446 instances_part :: { [RdrNameInstDecl] }
447 instances_part : INSTANCES_PART instdecls { $2 }
450 instdecls :: { [RdrNameInstDecl] }
452 | instd instdecls { $1 : $2 }
454 instd :: { RdrNameInstDecl }
455 instd : src_loc INSTANCE type EQUAL var_name SEMI
457 EmptyMonoBinds {- No bindings -}
458 [] {- No user pragmas -}
459 (Just $5) {- Dfun id -}
462 --------------------------------------------------------------------------
464 id_info :: { [HsIdInfo RdrName] }
466 | id_info_item id_info { $1 : $2 }
468 id_info_item :: { HsIdInfo RdrName }
469 id_info_item : ARITY_PART arity_info { HsArity $2 }
470 | strict_info { HsStrictness $1 }
471 | BOTTOM { HsStrictness HsBottom }
472 | UNFOLD_PART core_expr { HsUnfold $1 $2 }
473 | SPECIALISE spec_tvs
474 atypes EQUAL core_expr { HsSpecialise $2 $3 $5 }
477 spec_tvs :: { [HsTyVar RdrName] }
478 spec_tvs : OBRACK tv_bndrs CBRACK { $2 }
481 arity_info :: { ArityInfo }
482 arity_info : INTEGER { exactArity (fromInteger $1) }
484 strict_info :: { HsStrictnessInfo RdrName }
485 strict_info : STRICT_PART qvar_name OCURLY qdata_names CCURLY { HsStrictnessInfo $1 (Just ($2,$4)) }
486 | STRICT_PART qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
487 | STRICT_PART { HsStrictnessInfo $1 Nothing }
489 core_expr :: { UfExpr RdrName }
490 : LAM core_val_bndrs RARROW core_expr { foldr UfLam $4 $2 }
491 | BIGLAM core_tv_bndrs RARROW core_expr { foldr UfLam $4 $2 }
493 OCURLY alg_alts core_default CCURLY { UfCase $2 (UfAlgAlts $5 $6) }
494 | PRIM_CASE core_expr OF
495 OCURLY prim_alts core_default CCURLY { UfCase $2 (UfPrimAlts $5 $6) }
497 | LET OCURLY core_val_bndr EQUAL core_expr CCURLY
498 IN core_expr { UfLet (UfNonRec $3 $5) $8 }
499 | LETREC OCURLY rec_binds CCURLY
500 IN core_expr { UfLet (UfRec $3) $6 }
503 OBRACK atype atypes CBRACK core_args { let
504 (is_casm, may_gc) = $1
506 UfPrim (UfCCallOp $2 is_casm may_gc $5 $4)
510 | INLINE_CALL core_expr { UfNote UfInlineCall $2 }
511 | COERCE atype core_expr { UfNote (UfCoerce $2) $3 }
512 | SCC core_expr { UfNote (UfSCC $1) $2 }
515 fexpr :: { UfExpr RdrName }
516 fexpr : fexpr core_arg { UfApp $1 $2 }
517 | fexpr ATSIGN atype { UfApp $1 (UfTyArg $3) }
520 aexpr :: { UfExpr RdrName }
521 aexpr : qvar_name { UfVar $1 }
522 | qdata_name { UfVar $1 }
523 | core_lit { UfLit $1 }
524 | OPAREN core_expr CPAREN { $2 }
525 | qdata_name OCURLY data_args CCURLY { UfCon $1 $3 }
528 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
530 | core_val_bndr EQUAL core_expr SEMI rec_binds { ($1,$3) : $5 }
532 prim_alts :: { [(Literal,UfExpr RdrName)] }
534 | core_lit RARROW core_expr SEMI prim_alts { ($1,$3) : $5 }
536 alg_alts :: { [(RdrName, [RdrName], UfExpr RdrName)] }
538 | qdata_name var_names RARROW
539 core_expr SEMI alg_alts { ($1,$2,$4) : $6 }
541 core_default :: { UfDefault RdrName }
543 | var_name RARROW core_expr SEMI { UfBindDefault $1 $3 }
545 core_arg :: { UfArg RdrName }
546 : qvar_name { UfVarArg $1 }
547 | qdata_name { UfVarArg $1 }
548 | core_lit { UfLitArg $1 }
550 core_args :: { [UfArg RdrName] }
552 | core_arg core_args { $1 : $2 }
554 data_args :: { [UfArg RdrName] }
556 | core_arg data_args { $1 : $2 }
557 | ATSIGN atype data_args { UfTyArg $2 : $3 }
559 core_lit :: { Literal }
560 core_lit : INTEGER { mkMachInt_safe $1 }
561 | CHAR { MachChar $1 }
562 | STRING { MachStr $1 }
563 | STRING_LIT STRING { NoRepStr $2 }
564 | RATIONAL { MachDouble $1 }
565 | FLOAT_LIT RATIONAL { MachFloat $2 }
567 | INTEGER_LIT INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
568 -- The type checker will add the types
571 | RATIONAL_LIT INTEGER INTEGER { NoRepRational ($2 % $3)
572 (panic "NoRepRational type")
573 -- The type checker will add the type
576 | ADDR_LIT INTEGER { MachAddr $2 }
577 | LIT_LIT prim_rep STRING { MachLitLit $3 (decodePrimRep $2) }
579 core_val_bndr :: { UfBinder RdrName }
580 core_val_bndr : var_name DCOLON atype { UfValBinder $1 $3 }
582 core_val_bndrs :: { [UfBinder RdrName] }
583 core_val_bndrs : { [] }
584 | core_val_bndr core_val_bndrs { $1 : $2 }
586 core_tv_bndr :: { UfBinder RdrName }
587 core_tv_bndr : tv_name DCOLON akind { UfTyBinder $1 $3 }
588 | tv_name { UfTyBinder $1 mkBoxedTypeKind }
590 core_tv_bndrs :: { [UfBinder RdrName] }
591 core_tv_bndrs : { [] }
592 | core_tv_bndr core_tv_bndrs { $1 : $2 }
594 ccall_string :: { FAST_STRING }
600 : VARID { head (_UNPK_ $1) }
601 | CONID { head (_UNPK_ $1) }
603 -------------------------------------------------------------------
605 src_loc :: { SrcLoc }
606 src_loc : {% getSrcLocIf }
608 checkVersion :: { () }
609 : {-empty-} {% checkVersion Nothing }
610 | INTEGER {% checkVersion (Just (fromInteger $1)) }
612 -------------------------------------------------------------------
617 data IfaceStuff = PIface ParsedIface
618 | PIdInfo [HsIdInfo RdrName]
619 | PType RdrNameHsType