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 )
11 import Const ( Literal(..), mkMachInt_safe )
12 import BasicTypes ( Fixity(..), FixityDirection(..),
13 NewOrData(..), Version
15 import CostCentre ( CostCentre(..), IsCafCC(..), IsDupdCC(..) )
16 import HsPragmas ( noDataPragmas, noClassPragmas )
17 import Type ( Kind, mkArrowKind, boxedTypeKind, openTypeKind )
18 import IdInfo ( ArityInfo, exactArity, CprInfo(..) )
21 import RnMonad ( ImportVersion, LocalVersion, ParsedIface(..), WhatsImported(..),
22 RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..)
24 import Bag ( emptyBag, unitBag, snocBag )
25 import FiniteMap ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
26 import RdrName ( RdrName, mkRdrUnqual, mkSysQual, mkSysUnqual )
27 import Name ( OccName, Provenance )
28 import OccName ( mkSysOccFS,
29 tcName, varName, dataName, clsName, tvName,
32 import Module ( Module, mkSysModuleFS, IfaceFlavour, hiFile, hiBootFile )
33 import PrelMods ( mkTupNameStr, mkUbxTupNameStr )
34 import PrelInfo ( mkTupConRdrName, mkUbxTupConRdrName )
35 import SrcLoc ( SrcLoc )
47 %tokentype { IfaceToken }
48 %monad { IfM }{ thenIf }{ returnIf }
49 %lexer { lexIface } { ITeof }
52 'case' { ITcase } -- Haskell keywords
55 'default' { ITdefault }
56 'deriving' { ITderiving }
65 'instance' { ITinstance }
68 'newtype' { ITnewtype }
74 'qualified' { ITqualified }
77 '__interface' { ITinterface } -- GHC-extension keywords
78 '__export' { ITexport }
79 '__instimport' { ITinstimport }
80 '__forall' { ITforall }
81 '__letrec' { ITletrec }
82 '__coerce' { ITcoerce }
83 '__inline' { ITinline }
84 '__DEFAULT' { ITdefaultbranch }
86 '__integer' { ITinteger_lit }
87 '__float' { ITfloat_lit }
88 '__rational' { ITrational_lit }
89 '__addr' { ITaddr_lit }
90 '__litlit' { ITlit_lit }
91 '__string' { ITstring_lit }
92 '__ccall' { ITccall $$ }
94 '__sccC' { ITsccAllCafs }
97 '__P' { ITspecialise }
100 '__S' { ITstrict $$ }
101 '__M' { ITcprinfo $$ }
103 '..' { ITdotdot } -- reserved symbols
116 '/\\' { ITbiglam } -- GHC-extension symbols
118 '{' { ITocurly } -- special symbols
129 VARID { ITvarid $$ } -- identifiers
131 VARSYM { ITvarsym $$ }
132 CONSYM { ITconsym $$ }
133 QVARID { ITqvarid $$ }
134 QCONID { ITqconid $$ }
135 QVARSYM { ITqvarsym $$ }
136 QCONSYM { ITqconsym $$ }
138 PRAGMA { ITpragma $$ }
141 STRING { ITstring $$ }
142 INTEGER { ITinteger $$ }
143 RATIONAL { ITrational $$ }
145 UNKNOWN { ITunknown $$ }
148 -- iface_stuff is the main production.
149 -- It recognises (a) a whole interface file
150 -- (b) a type (so that type sigs can be parsed lazily)
151 -- (c) the IdInfo part of a signature (same reason)
153 iface_stuff :: { IfaceStuff }
154 iface_stuff : iface { let (nm, iff) = $1 in PIface nm iff }
156 | id_info { PIdInfo $1 }
159 iface :: { (EncodedFS, ParsedIface) }
160 iface : '__interface' mod_fs INTEGER checkVersion 'where'
166 { ( $2 -- Module name
168 (fromInteger $3) -- Module version
169 (reverse $6) -- Usages
170 (reverse $8) -- Exports
171 (reverse $7) -- Instance import modules
172 (reverse $10) -- Decls
173 (reverse $9) -- Local instances
177 --------------------------------------------------------------------------
179 import_part :: { [ImportVersion OccName] }
181 | import_part import_decl { $2 : $1 }
183 import_decl :: { ImportVersion OccName }
184 import_decl : 'import' mod_fs opt_bang INTEGER '::' whats_imported ';'
185 { (mkSysModuleFS $2 $3, fromInteger $4, $6) }
187 whats_imported :: { WhatsImported OccName }
188 whats_imported : { Everything }
189 | name_version_pair name_version_pairs { Specifically ($1:$2) }
191 name_version_pairs :: { [LocalVersion OccName] }
192 name_version_pairs : { [] }
193 | name_version_pair name_version_pairs { $1 : $2 }
195 name_version_pair :: { LocalVersion OccName }
196 name_version_pair : var_occ INTEGER { ($1, fromInteger $2) }
197 | tc_occ INTEGER { ($1, fromInteger $2) }
199 instance_import_part :: { [Module] }
200 instance_import_part : { [] }
201 | instance_import_part '__instimport' mod_name ';'
204 --------------------------------------------------------------------------
206 exports_part :: { [ExportItem] }
207 exports_part : { [] }
208 | exports_part '__export' opt_bang mod_fs entities ';'
209 { (mkSysModuleFS $4 $3,$5) : $1 }
211 opt_bang :: { IfaceFlavour }
212 opt_bang : { hiFile }
215 entities :: { [RdrAvailInfo] }
217 | entity entities { $1 : $2 }
219 entity :: { RdrAvailInfo }
220 entity : tc_occ { AvailTC $1 [$1] }
221 | var_occ { Avail $1 }
222 | tc_occ stuff_inside { AvailTC $1 ($1:$2) }
223 | tc_occ '|' stuff_inside { AvailTC $1 $3 }
225 stuff_inside :: { [OccName] }
226 stuff_inside : '{' val_occs '}' { $2 }
228 val_occ :: { OccName }
232 val_occs :: { [OccName] }
234 | val_occ val_occs { $1 : $2 }
237 --------------------------------------------------------------------------
239 fixity :: { FixityDirection }
240 fixity : 'infixl' { InfixL }
241 | 'infixr' { InfixR }
245 mb_fix : {-nothing-} { 9 }
246 | INTEGER { (fromInteger $1) }
248 -----------------------------------------------------------------------------
250 csigs :: { [RdrNameSig] }
252 | 'where' '{' csigs1 '}' { $3 }
254 csigs1 :: { [RdrNameSig] }
255 csigs1 : csig { [$1] }
256 | csig ';' csigs1 { $1 : $3 }
258 csig :: { RdrNameSig }
259 csig : src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
260 | src_loc var_name '=' '::' type
262 (Just (error "Un-filled-in default method"))
265 --------------------------------------------------------------------------
267 instance_decl_part :: { [RdrNameInstDecl] }
268 instance_decl_part : {- empty -} { [] }
269 | instance_decl_part inst_decl { $2 : $1 }
271 inst_decl :: { RdrNameInstDecl }
272 inst_decl : src_loc 'instance' type '=' var_name ';'
274 EmptyMonoBinds {- No bindings -}
275 [] {- No user pragmas -}
276 (Just $5) {- Dfun id -}
280 --------------------------------------------------------------------------
282 decls_part :: { [(Version, RdrNameHsDecl)] }
285 | decls_part version decl ';' { ($2,$3):$1 }
287 decl :: { RdrNameHsDecl }
288 decl : src_loc var_name '::' type maybe_idinfo
289 { SigD (IfaceSig $2 $4 ($5 $2) $1) }
290 | src_loc 'type' tc_name tv_bndrs '=' type
291 { TyClD (TySynonym $3 $4 $6 $1) }
292 | src_loc 'data' decl_context tc_name tv_bndrs constrs
293 { TyClD (TyData DataType $3 $4 $5 $6 Nothing noDataPragmas $1) }
294 | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
295 { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
296 | src_loc 'class' decl_context tc_name tv_bndrs csigs
297 { TyClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds
299 | src_loc fixity mb_fix var_or_data_name
300 { FixD (FixitySig $4 (Fixity $3 $2) $1) }
302 maybe_idinfo :: { RdrName -> [HsIdInfo RdrName] }
303 maybe_idinfo : {- empty -} { \_ -> [] }
304 | src_loc PRAGMA { \x ->
305 case parseIface $2 $1 of
306 Succeeded (PIdInfo id_info) -> id_info
307 Failed err -> pprPanic "IdInfo parse failed"
311 -----------------------------------------------------------------------------
313 version :: { Version }
314 version : INTEGER { fromInteger $1 }
316 decl_context :: { RdrNameContext }
317 decl_context : { [] }
318 | '{' context_list1 '}' '=>' { $2 }
320 ----------------------------------------------------------------
322 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
324 | '=' constrs1 { $2 }
326 constrs1 :: { [RdrNameConDecl] }
327 constrs1 : constr { [$1] }
328 | constr '|' constrs1 { $1 : $3 }
330 constr :: { RdrNameConDecl }
331 constr : src_loc ex_stuff data_name batypes { mkConDecl $3 $2 (VanillaCon $4) $1 }
332 | src_loc ex_stuff data_name '{' fields1 '}' { mkConDecl $3 $2 (RecCon $5) $1 }
333 -- We use "data_fs" so as to include ()
335 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
336 newtype_constr : { [] }
337 | src_loc '=' ex_stuff data_name atype { [mkConDecl $4 $3 (NewCon $5 Nothing) $1] }
338 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
339 { [mkConDecl $4 $3 (NewCon $8 (Just $6)) $1] }
341 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
342 ex_stuff : { ([],[]) }
343 | '__forall' forall context '=>' { ($2,$3) }
345 batypes :: { [RdrNameBangType] }
347 | batype batypes { $1 : $2 }
349 batype :: { RdrNameBangType }
350 batype : atype { Unbanged $1 }
351 | '!' atype { Banged $2 }
352 | '!' '!' atype { Unpacked $3 }
354 fields1 :: { [([RdrName], RdrNameBangType)] }
355 fields1 : field { [$1] }
356 | field ',' fields1 { $1 : $3 }
358 field :: { ([RdrName], RdrNameBangType) }
359 field : var_names1 '::' type { ($1, Unbanged $3) }
360 | var_names1 '::' '!' type { ($1, Banged $4) }
361 | var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
362 --------------------------------------------------------------------------
364 type :: { RdrNameHsType }
365 type : '__forall' forall context '=>' type
366 { mkHsForAllTy $2 $3 $5 }
367 | btype '->' type { MonoFunTy $1 $3 }
370 forall :: { [HsTyVar RdrName] }
371 forall : '[' tv_bndrs ']' { $2 }
373 context :: { RdrNameContext }
375 | '{' context_list1 '}' { $2 }
377 context_list1 :: { RdrNameContext }
378 context_list1 : class { [$1] }
379 | class ',' context_list1 { $1 : $3 }
381 class :: { (RdrName, [RdrNameHsType]) }
382 class : qcls_name atypes { ($1, $2) }
384 types2 :: { [RdrNameHsType] {- Two or more -} }
385 types2 : type ',' type { [$1,$3] }
386 | type ',' types2 { $1 : $3 }
388 btype :: { RdrNameHsType }
390 | btype atype { MonoTyApp $1 $2 }
392 atype :: { RdrNameHsType }
393 atype : qtc_name { MonoTyVar $1 }
394 | tv_name { MonoTyVar $1 }
395 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
396 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
397 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
398 | '[' type ']' { MonoListTy $2 }
399 | '{' qcls_name atypes '}' { MonoDictTy $2 $3 }
400 | '(' type ')' { $2 }
402 -- This one is dealt with via qtc_name
403 -- | '(' ')' { MonoTupleTy [] True }
405 atypes :: { [RdrNameHsType] {- Zero or more -} }
407 | atype atypes { $1 : $2 }
408 ---------------------------------------------------------------------
409 mod_fs :: { EncodedFS }
412 mod_name :: { Module }
413 : mod_fs { mkSysModuleFS $1 hiFile }
416 ---------------------------------------------------
417 var_fs :: { EncodedFS }
424 qvar_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
428 var_occ :: { OccName }
429 : var_fs { mkSysOccFS varName $1 }
431 var_name :: { RdrName }
432 var_name : var_occ { mkRdrUnqual $1 }
434 qvar_name :: { RdrName }
435 qvar_name : var_name { $1 }
436 | qvar_fs { mkSysQual varName $1 }
438 var_names :: { [RdrName] }
440 | var_name var_names { $1 : $2 }
442 var_names1 :: { [RdrName] }
443 var_names1 : var_name var_names { $1 : $2 }
445 ---------------------------------------------------
446 -- For some bizarre reason,
447 -- (,,,) is dealt with by the parser
448 -- Foo.(,,,) is dealt with by the lexer
451 data_fs :: { EncodedFS }
455 qdata_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
459 data_occ :: { OccName }
460 : data_fs { mkSysOccFS dataName $1 }
462 data_name :: { RdrName }
463 : data_occ { mkRdrUnqual $1 }
465 qdata_name :: { RdrName }
466 qdata_name : data_name { $1 }
467 | qdata_fs { mkSysQual dataName $1 }
469 qdata_names :: { [RdrName] }
471 | qdata_name qdata_names { $1 : $2 }
473 var_or_data_name :: { RdrName }
477 ---------------------------------------------------
478 tc_fs :: { EncodedFS }
481 tc_occ :: { OccName }
482 : tc_fs { mkSysOccFS tcName $1 }
484 tc_name :: { RdrName }
485 : tc_occ { mkRdrUnqual $1 }
487 qtc_name :: { RdrName }
489 | qdata_fs { mkSysQual tcName $1 }
491 ---------------------------------------------------
492 cls_name :: { RdrName }
493 : data_fs { mkSysUnqual clsName $1 }
495 qcls_name :: { RdrName }
497 | qdata_fs { mkSysQual clsName $1 }
499 ---------------------------------------------------
500 tv_name :: { RdrName }
501 : VARID { mkSysUnqual tvName $1 }
502 | VARSYM { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
504 tv_bndr :: { HsTyVar RdrName }
505 : tv_name '::' akind { IfaceTyVar $1 $3 }
506 | tv_name { IfaceTyVar $1 boxedTypeKind }
508 tv_bndrs :: { [HsTyVar RdrName] }
510 | tv_bndr tv_bndrs { $1 : $2 }
512 ---------------------------------------------------
515 | akind '->' kind { mkArrowKind $1 $3 }
518 : VARSYM { if $1 == SLIT("*") then
520 else if $1 == SLIT("?") then
522 else panic "ParseInterface: akind"
524 | '(' kind ')' { $2 }
526 --------------------------------------------------------------------------
528 id_info :: { [HsIdInfo RdrName] }
530 | id_info_item id_info { $1 : $2 }
532 id_info_item :: { HsIdInfo RdrName }
533 id_info_item : '__A' arity_info { HsArity $2 }
534 | strict_info { HsStrictness $1 }
535 | '__M' { HsCprInfo $1 }
536 | '__U' core_expr { HsUnfold $1 (Just $2) }
537 | '__U' { HsUnfold $1 Nothing }
539 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
540 | '__C' { HsNoCafRefs }
543 spec_tvs :: { [HsTyVar RdrName] }
544 spec_tvs : '[' tv_bndrs ']' { $2 }
547 arity_info :: { ArityInfo }
548 arity_info : INTEGER { exactArity (fromInteger $1) }
550 strict_info :: { HsStrictnessInfo RdrName }
551 strict_info : '__S' qvar_name '{' qdata_names '}'
552 { HsStrictnessInfo $1 (Just ($2,$4)) }
553 | '__S' qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
554 | '__S' { HsStrictnessInfo $1 Nothing }
556 -------------------------------------------------------
557 core_expr :: { UfExpr RdrName }
558 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
559 | 'case' core_expr 'of' var_name
560 '{' core_alts '}' { UfCase $2 $4 $6 }
562 | 'let' '{' core_val_bndr '=' core_expr
563 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
564 | '__letrec' '{' rec_binds '}'
565 'in' core_expr { UfLet (UfRec $3) $6 }
567 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
568 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
570 | '__inline' core_expr { UfNote UfInlineCall $2 }
571 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
572 | scc core_expr { UfNote (UfSCC $1) $2 }
575 fexpr :: { UfExpr RdrName }
576 fexpr : fexpr core_arg { UfApp $1 $2 }
579 core_arg :: { UfExpr RdrName }
580 : '@' atype { UfType $2 }
583 core_args :: { [UfExpr RdrName] }
585 | core_arg core_args { $1 : $2 }
587 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
588 core_aexpr : qvar_name { UfVar $1 }
590 | qdata_name { UfVar $1 }
591 -- This one means that e.g. "True" will parse as
592 -- (UfVar True_Id) rather than (UfCon True_Con []).
593 -- No big deal; it'll be inlined in a jiffy. I tried
594 -- parsing it to (Con con []) directly, but got bitten
595 -- when a real constructor Id showed up in an interface
596 -- file. As usual, a hack bites you in the end.
597 -- If you want to get a UfCon, then use the
598 -- curly-bracket notation (True {}).
600 | core_lit { UfCon (UfLitCon $1) [] }
601 | '(' core_expr ')' { $2 }
602 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
603 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
604 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
606 -- This one is dealt with by qdata_name: see above comments
607 -- | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
609 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
610 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
611 | core_expr ',' comma_exprs2 { $1 : $3 }
613 con_or_primop :: { UfCon RdrName }
614 con_or_primop : qdata_name { UfDataCon $1 }
615 | qvar_name { UfPrimOp $1 }
616 | '__ccall' ccall_string { let
617 (is_dyn, is_casm, may_gc) = $1
619 UfCCallOp $2 is_dyn is_casm may_gc
622 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
624 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
626 core_alts :: { [UfAlt RdrName] }
628 | core_alt ';' core_alts { $1 : $3 }
630 core_alt :: { UfAlt RdrName }
631 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
633 core_pat :: { (UfCon RdrName, [RdrName]) }
634 core_pat : core_lit { (UfLitCon $1, []) }
635 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
636 | qdata_name var_names { (UfDataCon $1, $2) }
637 | '(' comma_var_names1 ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
638 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
639 | '__DEFAULT' { (UfDefault, []) }
640 | '(' core_pat ')' { $2 }
643 comma_var_names1 :: { [RdrName] } -- One or more
644 comma_var_names1 : var_name { [$1] }
645 | var_name ',' comma_var_names1 { $1 : $3 }
647 core_lit :: { Literal }
648 core_lit : INTEGER { mkMachInt_safe $1 }
649 | CHAR { MachChar $1 }
650 | STRING { MachStr $1 }
651 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
652 | RATIONAL { MachDouble $1 }
653 | '__float' RATIONAL { MachFloat $2 }
655 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
656 -- The type checker will add the types
659 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
660 (panic "NoRepRational type")
661 -- The type checker will add the type
664 | '__addr' INTEGER { MachAddr $2 }
666 core_bndr :: { UfBinder RdrName }
667 core_bndr : core_val_bndr { $1 }
668 | core_tv_bndr { $1 }
670 core_bndrs :: { [UfBinder RdrName] }
672 | core_bndr core_bndrs { $1 : $2 }
674 core_val_bndr :: { UfBinder RdrName }
675 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
677 core_tv_bndr :: { UfBinder RdrName }
678 core_tv_bndr : '@' tv_name '::' akind { UfTyBinder $2 $4 }
679 | '@' tv_name { UfTyBinder $2 boxedTypeKind }
681 ccall_string :: { FAST_STRING }
686 ------------------------------------------------------------------------
687 scc :: { CostCentre }
688 : '__sccC' '{' mod_name STRING '}' { AllCafsCC $3 $4 }
689 | '__scc' '{' cc_name mod_name STRING cc_dup cc_caf '}'
690 { NormalCC { cc_name = $3, cc_mod = $4, cc_grp = $5,
691 cc_is_dupd = $6, cc_is_caf = $7 } }
693 cc_name :: { EncodedFS }
697 cc_dup :: { IsDupdCC }
698 cc_dup : { OriginalCC }
701 cc_caf :: { IsCafCC }
705 -------------------------------------------------------------------
707 src_loc :: { SrcLoc }
708 src_loc : {% getSrcLocIf }
710 checkVersion :: { () }
711 : {-empty-} {% checkVersion Nothing }
712 | INTEGER {% checkVersion (Just (fromInteger $1)) }
714 -------------------------------------------------------------------
719 data IfaceStuff = PIface EncodedFS{-.hi module name-} ParsedIface
720 | PIdInfo [HsIdInfo RdrName]
721 | PType RdrNameHsType
723 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc