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, UsageAnn(..) )
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 }
100 '__P' { ITspecialise }
102 '__U' { ITunfold $$ }
103 '__S' { ITstrict $$ }
104 '__M' { ITcprinfo $$ }
106 '..' { ITdotdot } -- reserved symbols
119 '/\\' { ITbiglam } -- GHC-extension symbols
121 '{' { ITocurly } -- special symbols
132 VARID { ITvarid $$ } -- identifiers
134 VARSYM { ITvarsym $$ }
135 CONSYM { ITconsym $$ }
136 QVARID { ITqvarid $$ }
137 QCONID { ITqconid $$ }
138 QVARSYM { ITqvarsym $$ }
139 QCONSYM { ITqconsym $$ }
141 PRAGMA { ITpragma $$ }
144 STRING { ITstring $$ }
145 INTEGER { ITinteger $$ }
146 RATIONAL { ITrational $$ }
148 UNKNOWN { ITunknown $$ }
151 -- iface_stuff is the main production.
152 -- It recognises (a) a whole interface file
153 -- (b) a type (so that type sigs can be parsed lazily)
154 -- (c) the IdInfo part of a signature (same reason)
156 iface_stuff :: { IfaceStuff }
157 iface_stuff : iface { let (nm, iff) = $1 in PIface nm iff }
159 | id_info { PIdInfo $1 }
162 iface :: { (EncodedFS, ParsedIface) }
163 iface : '__interface' mod_fs INTEGER checkVersion 'where'
169 { ( $2 -- Module name
171 (fromInteger $3) -- Module version
172 (reverse $6) -- Usages
173 (reverse $8) -- Exports
174 (reverse $7) -- Instance import modules
175 (reverse $10) -- Decls
176 (reverse $9) -- Local instances
180 --------------------------------------------------------------------------
182 import_part :: { [ImportVersion OccName] }
184 | import_part import_decl { $2 : $1 }
186 import_decl :: { ImportVersion OccName }
187 import_decl : 'import' mod_fs opt_bang INTEGER '::' whats_imported ';'
188 { (mkSysModuleFS $2 $3, fromInteger $4, $6) }
190 whats_imported :: { WhatsImported OccName }
191 whats_imported : { Everything }
192 | name_version_pair name_version_pairs { Specifically ($1:$2) }
194 name_version_pairs :: { [LocalVersion OccName] }
195 name_version_pairs : { [] }
196 | name_version_pair name_version_pairs { $1 : $2 }
198 name_version_pair :: { LocalVersion OccName }
199 name_version_pair : var_occ INTEGER { ($1, fromInteger $2) }
200 | tc_occ INTEGER { ($1, fromInteger $2) }
202 instance_import_part :: { [Module] }
203 instance_import_part : { [] }
204 | instance_import_part '__instimport' mod_name ';'
207 --------------------------------------------------------------------------
209 exports_part :: { [ExportItem] }
210 exports_part : { [] }
211 | exports_part '__export' opt_bang mod_fs entities ';'
212 { (mkSysModuleFS $4 $3,$5) : $1 }
214 opt_bang :: { IfaceFlavour }
215 opt_bang : { hiFile }
218 entities :: { [RdrAvailInfo] }
220 | entity entities { $1 : $2 }
222 entity :: { RdrAvailInfo }
223 entity : tc_occ { AvailTC $1 [$1] }
224 | var_occ { Avail $1 }
225 | tc_occ stuff_inside { AvailTC $1 ($1:$2) }
226 | tc_occ '|' stuff_inside { AvailTC $1 $3 }
228 stuff_inside :: { [OccName] }
229 stuff_inside : '{' val_occs '}' { $2 }
231 val_occ :: { OccName }
235 val_occs :: { [OccName] }
237 | val_occ val_occs { $1 : $2 }
240 --------------------------------------------------------------------------
242 fixity :: { FixityDirection }
243 fixity : 'infixl' { InfixL }
244 | 'infixr' { InfixR }
248 mb_fix : {-nothing-} { 9 }
249 | INTEGER { (fromInteger $1) }
251 -----------------------------------------------------------------------------
253 csigs :: { [RdrNameSig] }
255 | 'where' '{' csigs1 '}' { $3 }
257 csigs1 :: { [RdrNameSig] }
258 csigs1 : csig { [$1] }
259 | csig ';' csigs1 { $1 : $3 }
261 csig :: { RdrNameSig }
262 csig : src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
263 | src_loc var_name '=' '::' type
265 (Just (error "Un-filled-in default method"))
268 --------------------------------------------------------------------------
270 instance_decl_part :: { [RdrNameInstDecl] }
271 instance_decl_part : {- empty -} { [] }
272 | instance_decl_part inst_decl { $2 : $1 }
274 inst_decl :: { RdrNameInstDecl }
275 inst_decl : src_loc 'instance' type '=' var_name ';'
277 EmptyMonoBinds {- No bindings -}
278 [] {- No user pragmas -}
279 (Just $5) {- Dfun id -}
283 --------------------------------------------------------------------------
285 decls_part :: { [(Version, RdrNameHsDecl)] }
288 | decls_part version decl ';' { ($2,$3):$1 }
290 decl :: { RdrNameHsDecl }
291 decl : src_loc var_name '::' type maybe_idinfo
292 { SigD (IfaceSig $2 $4 ($5 $2) $1) }
293 | src_loc 'type' tc_name tv_bndrs '=' type
294 { TyClD (TySynonym $3 $4 $6 $1) }
295 | src_loc 'data' decl_context tc_name tv_bndrs constrs
296 { TyClD (TyData DataType $3 $4 $5 $6 Nothing noDataPragmas $1) }
297 | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
298 { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
299 | src_loc 'class' decl_context tc_name tv_bndrs csigs
300 { TyClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds
302 | src_loc fixity mb_fix var_or_data_name
303 { FixD (FixitySig $4 (Fixity $3 $2) $1) }
305 maybe_idinfo :: { RdrName -> [HsIdInfo RdrName] }
306 maybe_idinfo : {- empty -} { \_ -> [] }
307 | src_loc PRAGMA { \x ->
308 case parseIface $2 $1 of
309 Succeeded (PIdInfo id_info) -> id_info
310 Failed err -> pprPanic "IdInfo parse failed"
314 -----------------------------------------------------------------------------
316 version :: { Version }
317 version : INTEGER { fromInteger $1 }
319 decl_context :: { RdrNameContext }
320 decl_context : { [] }
321 | '{' context_list1 '}' '=>' { $2 }
323 ----------------------------------------------------------------
325 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
327 | '=' constrs1 { $2 }
329 constrs1 :: { [RdrNameConDecl] }
330 constrs1 : constr { [$1] }
331 | constr '|' constrs1 { $1 : $3 }
333 constr :: { RdrNameConDecl }
334 constr : src_loc ex_stuff data_name batypes { mkConDecl $3 $2 (VanillaCon $4) $1 }
335 | src_loc ex_stuff data_name '{' fields1 '}' { mkConDecl $3 $2 (RecCon $5) $1 }
336 -- We use "data_fs" so as to include ()
338 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
339 newtype_constr : { [] }
340 | src_loc '=' ex_stuff data_name atype { [mkConDecl $4 $3 (NewCon $5 Nothing) $1] }
341 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
342 { [mkConDecl $4 $3 (NewCon $8 (Just $6)) $1] }
344 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
345 ex_stuff : { ([],[]) }
346 | '__forall' forall context '=>' { ($2,$3) }
348 batypes :: { [RdrNameBangType] }
350 | batype batypes { $1 : $2 }
352 batype :: { RdrNameBangType }
353 batype : atype { Unbanged $1 }
354 | '!' atype { Banged $2 }
355 | '!' '!' atype { Unpacked $3 }
357 fields1 :: { [([RdrName], RdrNameBangType)] }
358 fields1 : field { [$1] }
359 | field ',' fields1 { $1 : $3 }
361 field :: { ([RdrName], RdrNameBangType) }
362 field : var_names1 '::' type { ($1, Unbanged $3) }
363 | var_names1 '::' '!' type { ($1, Banged $4) }
364 | var_names1 '::' '!' '!' type { ($1, Unpacked $5) }
365 --------------------------------------------------------------------------
367 type :: { RdrNameHsType }
368 type : '__forall' forall context '=>' type
369 { mkHsForAllTy $2 $3 $5 }
370 | btype '->' type { MonoFunTy $1 $3 }
373 forall :: { [HsTyVar RdrName] }
374 forall : '[' tv_bndrs ']' { $2 }
376 context :: { RdrNameContext }
378 | '{' context_list1 '}' { $2 }
380 context_list1 :: { RdrNameContext }
381 context_list1 : class { [$1] }
382 | class ',' context_list1 { $1 : $3 }
384 class :: { (RdrName, [RdrNameHsType]) }
385 class : qcls_name atypes { ($1, $2) }
387 types2 :: { [RdrNameHsType] {- Two or more -} }
388 types2 : type ',' type { [$1,$3] }
389 | type ',' types2 { $1 : $3 }
391 btype :: { RdrNameHsType }
393 | btype atype { MonoTyApp $1 $2 }
394 | '__o' atype { MonoUsgTy UsOnce $2 }
395 | '__m' atype { MonoUsgTy UsMany $2 }
397 atype :: { RdrNameHsType }
398 atype : qtc_name { MonoTyVar $1 }
399 | tv_name { MonoTyVar $1 }
400 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
401 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
402 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
403 | '[' type ']' { MonoListTy $2 }
404 | '{' qcls_name atypes '}' { MonoDictTy $2 $3 }
405 | '(' type ')' { $2 }
407 -- This one is dealt with via qtc_name
408 -- | '(' ')' { MonoTupleTy [] True }
410 atypes :: { [RdrNameHsType] {- Zero or more -} }
412 | atype atypes { $1 : $2 }
413 ---------------------------------------------------------------------
414 mod_fs :: { EncodedFS }
417 mod_name :: { Module }
418 : mod_fs { mkSysModuleFS $1 hiFile }
421 ---------------------------------------------------
422 var_fs :: { EncodedFS }
429 qvar_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
433 var_occ :: { OccName }
434 : var_fs { mkSysOccFS varName $1 }
436 var_name :: { RdrName }
437 var_name : var_occ { mkRdrUnqual $1 }
439 qvar_name :: { RdrName }
440 qvar_name : var_name { $1 }
441 | qvar_fs { mkSysQual varName $1 }
443 var_names :: { [RdrName] }
445 | var_name var_names { $1 : $2 }
447 var_names1 :: { [RdrName] }
448 var_names1 : var_name var_names { $1 : $2 }
450 ---------------------------------------------------
451 -- For some bizarre reason,
452 -- (,,,) is dealt with by the parser
453 -- Foo.(,,,) is dealt with by the lexer
456 data_fs :: { EncodedFS }
460 qdata_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
464 data_occ :: { OccName }
465 : data_fs { mkSysOccFS dataName $1 }
467 data_name :: { RdrName }
468 : data_occ { mkRdrUnqual $1 }
470 qdata_name :: { RdrName }
471 qdata_name : data_name { $1 }
472 | qdata_fs { mkSysQual dataName $1 }
474 qdata_names :: { [RdrName] }
476 | qdata_name qdata_names { $1 : $2 }
478 var_or_data_name :: { RdrName }
482 ---------------------------------------------------
483 tc_fs :: { EncodedFS }
486 tc_occ :: { OccName }
487 : tc_fs { mkSysOccFS tcName $1 }
489 tc_name :: { RdrName }
490 : tc_occ { mkRdrUnqual $1 }
492 qtc_name :: { RdrName }
494 | qdata_fs { mkSysQual tcName $1 }
496 ---------------------------------------------------
497 cls_name :: { RdrName }
498 : data_fs { mkSysUnqual clsName $1 }
500 qcls_name :: { RdrName }
502 | qdata_fs { mkSysQual clsName $1 }
504 ---------------------------------------------------
505 tv_name :: { RdrName }
506 : VARID { mkSysUnqual tvName $1 }
507 | VARSYM { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
509 tv_bndr :: { HsTyVar RdrName }
510 : tv_name '::' akind { IfaceTyVar $1 $3 }
511 | tv_name { IfaceTyVar $1 boxedTypeKind }
513 tv_bndrs :: { [HsTyVar RdrName] }
515 | tv_bndr tv_bndrs { $1 : $2 }
517 ---------------------------------------------------
520 | akind '->' kind { mkArrowKind $1 $3 }
523 : VARSYM { if $1 == SLIT("*") then
525 else if $1 == SLIT("?") then
527 else panic "ParseInterface: akind"
529 | '(' kind ')' { $2 }
531 --------------------------------------------------------------------------
533 id_info :: { [HsIdInfo RdrName] }
535 | id_info_item id_info { $1 : $2 }
536 | strict_info id_info { $1 ++ $2 }
538 id_info_item :: { HsIdInfo RdrName }
539 : '__A' arity_info { HsArity $2 }
540 | '__U' core_expr { HsUnfold $1 (Just $2) }
541 | '__U' { HsUnfold $1 Nothing }
543 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
544 | '__C' { HsNoCafRefs }
547 strict_info :: { [HsIdInfo RdrName] }
548 : cpr worker { ($1:$2) }
549 | strict worker { ($1:$2) }
550 | cpr strict worker { ($1:$2:$3) }
552 cpr :: { HsIdInfo RdrName }
553 : '__M' { HsCprInfo $1 }
555 strict :: { HsIdInfo RdrName }
556 : '__S' { HsStrictness (HsStrictnessInfo $1) }
558 worker :: { [HsIdInfo RdrName] }
559 : qvar_name '{' qdata_names '}' { [HsWorker $1 $3] }
560 | qvar_name { [HsWorker $1 []] }
561 | {- nothing -} { [] }
563 spec_tvs :: { [HsTyVar RdrName] }
564 : '[' tv_bndrs ']' { $2 }
567 arity_info :: { ArityInfo }
568 : INTEGER { exactArity (fromInteger $1) }
570 -------------------------------------------------------
571 core_expr :: { UfExpr RdrName }
572 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
573 | 'case' core_expr 'of' var_name
574 '{' core_alts '}' { UfCase $2 $4 $6 }
576 | 'let' '{' core_val_bndr '=' core_expr
577 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
578 | '__letrec' '{' rec_binds '}'
579 'in' core_expr { UfLet (UfRec $3) $6 }
581 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
582 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
584 | '__inline' core_expr { UfNote UfInlineCall $2 }
585 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
586 | scc core_expr { UfNote (UfSCC $1) $2 }
589 fexpr :: { UfExpr RdrName }
590 fexpr : fexpr core_arg { UfApp $1 $2 }
593 core_arg :: { UfExpr RdrName }
594 : '@' atype { UfType $2 }
597 core_args :: { [UfExpr RdrName] }
599 | core_arg core_args { $1 : $2 }
601 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
602 core_aexpr : qvar_name { UfVar $1 }
604 | qdata_name { UfVar $1 }
605 -- This one means that e.g. "True" will parse as
606 -- (UfVar True_Id) rather than (UfCon True_Con []).
607 -- No big deal; it'll be inlined in a jiffy. I tried
608 -- parsing it to (Con con []) directly, but got bitten
609 -- when a real constructor Id showed up in an interface
610 -- file. As usual, a hack bites you in the end.
611 -- If you want to get a UfCon, then use the
612 -- curly-bracket notation (True {}).
614 | core_lit { UfCon (UfLitCon $1) [] }
615 | '(' core_expr ')' { $2 }
616 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
617 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
618 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
620 -- This one is dealt with by qdata_name: see above comments
621 -- | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
623 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
624 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
625 | core_expr ',' comma_exprs2 { $1 : $3 }
627 con_or_primop :: { UfCon RdrName }
628 con_or_primop : qdata_name { UfDataCon $1 }
629 | qvar_name { UfPrimOp $1 }
630 | '__ccall' ccall_string { let
631 (is_dyn, is_casm, may_gc) = $1
633 UfCCallOp $2 is_dyn is_casm may_gc
636 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
638 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
640 core_alts :: { [UfAlt RdrName] }
642 | core_alt ';' core_alts { $1 : $3 }
644 core_alt :: { UfAlt RdrName }
645 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
647 core_pat :: { (UfCon RdrName, [RdrName]) }
648 core_pat : core_lit { (UfLitCon $1, []) }
649 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
650 | qdata_name var_names { (UfDataCon $1, $2) }
651 | '(' comma_var_names1 ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
652 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
653 | '__DEFAULT' { (UfDefault, []) }
654 | '(' core_pat ')' { $2 }
657 comma_var_names1 :: { [RdrName] } -- One or more
658 comma_var_names1 : var_name { [$1] }
659 | var_name ',' comma_var_names1 { $1 : $3 }
661 core_lit :: { Literal }
662 core_lit : INTEGER { mkMachInt_safe $1 }
663 | CHAR { MachChar $1 }
664 | STRING { MachStr $1 }
665 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
666 | RATIONAL { MachDouble $1 }
667 | '__float' RATIONAL { MachFloat $2 }
669 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
670 -- The type checker will add the types
673 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
674 (panic "NoRepRational type")
675 -- The type checker will add the type
678 | '__addr' INTEGER { MachAddr $2 }
680 core_bndr :: { UfBinder RdrName }
681 core_bndr : core_val_bndr { $1 }
682 | core_tv_bndr { $1 }
684 core_bndrs :: { [UfBinder RdrName] }
686 | core_bndr core_bndrs { $1 : $2 }
688 core_val_bndr :: { UfBinder RdrName }
689 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
691 core_tv_bndr :: { UfBinder RdrName }
692 core_tv_bndr : '@' tv_name '::' akind { UfTyBinder $2 $4 }
693 | '@' tv_name { UfTyBinder $2 boxedTypeKind }
695 ccall_string :: { FAST_STRING }
700 ------------------------------------------------------------------------
701 scc :: { CostCentre }
702 : '__sccC' '{' mod_name STRING '}' { AllCafsCC $3 $4 }
703 | '__scc' '{' cc_name mod_name STRING cc_dup cc_caf '}'
704 { NormalCC { cc_name = $3, cc_mod = $4, cc_grp = $5,
705 cc_is_dupd = $6, cc_is_caf = $7 } }
707 cc_name :: { EncodedFS }
711 cc_dup :: { IsDupdCC }
712 cc_dup : { OriginalCC }
715 cc_caf :: { IsCafCC }
719 -------------------------------------------------------------------
721 src_loc :: { SrcLoc }
722 src_loc : {% getSrcLocIf }
724 checkVersion :: { () }
725 : {-empty-} {% checkVersion Nothing }
726 | INTEGER {% checkVersion (Just (fromInteger $1)) }
728 -------------------------------------------------------------------
733 data IfaceStuff = PIface EncodedFS{-.hi module name-} ParsedIface
734 | PIdInfo [HsIdInfo RdrName]
735 | PType RdrNameHsType
737 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc