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(..), IsDictCC(..), IsCafCC(..), IsDupdCC(..) )
16 import HsPragmas ( noDataPragmas, noClassPragmas )
17 import Type ( Kind, mkArrowKind, boxedTypeKind, openTypeKind )
18 import IdInfo ( ArityInfo, exactArity )
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 }
95 '__sccD' { ITsccAllDicts }
98 '__P' { ITspecialise }
100 '__U' { ITunfold $$ }
101 '__S' { ITstrict $$ }
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 }
353 fields1 :: { [([RdrName], RdrNameBangType)] }
354 fields1 : field { [$1] }
355 | field ',' fields1 { $1 : $3 }
357 field :: { ([RdrName], RdrNameBangType) }
358 field : var_names1 '::' type { ($1, Unbanged $3) }
359 | var_names1 '::' '!' type { ($1, Banged $4) }
360 --------------------------------------------------------------------------
362 type :: { RdrNameHsType }
363 type : '__forall' forall context '=>' type
364 { mkHsForAllTy $2 $3 $5 }
365 | btype '->' type { MonoFunTy $1 $3 }
368 forall :: { [HsTyVar RdrName] }
369 forall : '[' tv_bndrs ']' { $2 }
371 context :: { RdrNameContext }
373 | '{' context_list1 '}' { $2 }
375 context_list1 :: { RdrNameContext }
376 context_list1 : class { [$1] }
377 | class ',' context_list1 { $1 : $3 }
379 class :: { (RdrName, [RdrNameHsType]) }
380 class : qcls_name atypes { ($1, $2) }
382 types2 :: { [RdrNameHsType] {- Two or more -} }
383 types2 : type ',' type { [$1,$3] }
384 | type ',' types2 { $1 : $3 }
386 btype :: { RdrNameHsType }
388 | btype atype { MonoTyApp $1 $2 }
390 atype :: { RdrNameHsType }
391 atype : qtc_name { MonoTyVar $1 }
392 | tv_name { MonoTyVar $1 }
393 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
394 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
395 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
396 | '[' type ']' { MonoListTy $2 }
397 | '{' qcls_name atypes '}' { MonoDictTy $2 $3 }
398 | '(' type ')' { $2 }
400 -- This one is dealt with via qtc_name
401 -- | '(' ')' { MonoTupleTy [] True }
403 atypes :: { [RdrNameHsType] {- Zero or more -} }
405 | atype atypes { $1 : $2 }
406 ---------------------------------------------------------------------
407 mod_fs :: { EncodedFS }
410 mod_name :: { Module }
411 : mod_fs { mkSysModuleFS $1 hiFile }
414 ---------------------------------------------------
415 var_fs :: { EncodedFS }
422 qvar_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
426 var_occ :: { OccName }
427 : var_fs { mkSysOccFS varName $1 }
429 var_name :: { RdrName }
430 var_name : var_occ { mkRdrUnqual $1 }
432 qvar_name :: { RdrName }
433 qvar_name : var_name { $1 }
434 | qvar_fs { mkSysQual varName $1 }
436 var_names :: { [RdrName] }
438 | var_name var_names { $1 : $2 }
440 var_names1 :: { [RdrName] }
441 var_names1 : var_name var_names { $1 : $2 }
443 ---------------------------------------------------
444 -- For some bizarre reason,
445 -- (,,,) is dealt with by the parser
446 -- Foo.(,,,) is dealt with by the lexer
449 data_fs :: { EncodedFS }
453 qdata_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
457 data_occ :: { OccName }
458 : data_fs { mkSysOccFS dataName $1 }
460 data_name :: { RdrName }
461 : data_occ { mkRdrUnqual $1 }
463 qdata_name :: { RdrName }
464 qdata_name : data_name { $1 }
465 | qdata_fs { mkSysQual dataName $1 }
467 qdata_names :: { [RdrName] }
469 | qdata_name qdata_names { $1 : $2 }
471 var_or_data_name :: { RdrName }
475 ---------------------------------------------------
476 tc_fs :: { EncodedFS }
479 tc_occ :: { OccName }
480 : tc_fs { mkSysOccFS tcName $1 }
482 tc_name :: { RdrName }
483 : tc_occ { mkRdrUnqual $1 }
485 qtc_name :: { RdrName }
487 | qdata_fs { mkSysQual tcName $1 }
489 ---------------------------------------------------
490 cls_name :: { RdrName }
491 : data_fs { mkSysUnqual clsName $1 }
493 qcls_name :: { RdrName }
495 | qdata_fs { mkSysQual clsName $1 }
497 ---------------------------------------------------
498 tv_name :: { RdrName }
499 : VARID { mkSysUnqual tvName $1 }
500 | VARSYM { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
502 tv_bndr :: { HsTyVar RdrName }
503 : tv_name '::' akind { IfaceTyVar $1 $3 }
504 | tv_name { IfaceTyVar $1 boxedTypeKind }
506 tv_bndrs :: { [HsTyVar RdrName] }
508 | tv_bndr tv_bndrs { $1 : $2 }
510 ---------------------------------------------------
513 | akind '->' kind { mkArrowKind $1 $3 }
516 : VARSYM { if $1 == SLIT("*") then
518 else if $1 == SLIT("?") then
520 else panic "ParseInterface: akind"
522 | '(' kind ')' { $2 }
524 --------------------------------------------------------------------------
526 id_info :: { [HsIdInfo RdrName] }
528 | id_info_item id_info { $1 : $2 }
530 id_info_item :: { HsIdInfo RdrName }
531 id_info_item : '__A' arity_info { HsArity $2 }
532 | strict_info { HsStrictness $1 }
533 | '__U' core_expr { HsUnfold $1 (Just $2) }
534 | '__U' { HsUnfold $1 Nothing }
536 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
537 | '__C' { HsNoCafRefs }
540 spec_tvs :: { [HsTyVar RdrName] }
541 spec_tvs : '[' tv_bndrs ']' { $2 }
544 arity_info :: { ArityInfo }
545 arity_info : INTEGER { exactArity (fromInteger $1) }
547 strict_info :: { HsStrictnessInfo RdrName }
548 strict_info : '__S' qvar_name '{' qdata_names '}'
549 { HsStrictnessInfo $1 (Just ($2,$4)) }
550 | '__S' qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
551 | '__S' { HsStrictnessInfo $1 Nothing }
553 -------------------------------------------------------
554 core_expr :: { UfExpr RdrName }
555 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
556 | 'case' core_expr 'of' var_name
557 '{' core_alts '}' { UfCase $2 $4 $6 }
559 | 'let' '{' core_val_bndr '=' core_expr
560 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
561 | '__letrec' '{' rec_binds '}'
562 'in' core_expr { UfLet (UfRec $3) $6 }
564 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
565 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
567 | '__inline' core_expr { UfNote UfInlineCall $2 }
568 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
569 | scc core_expr { UfNote (UfSCC $1) $2 }
572 fexpr :: { UfExpr RdrName }
573 fexpr : fexpr core_arg { UfApp $1 $2 }
576 core_arg :: { UfExpr RdrName }
577 : '@' atype { UfType $2 }
580 core_args :: { [UfExpr RdrName] }
582 | core_arg core_args { $1 : $2 }
584 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
585 core_aexpr : qvar_name { UfVar $1 }
587 | qdata_name { UfVar $1 }
588 -- This one means that e.g. "True" will parse as
589 -- (UfVar True_Id) rather than (UfCon True_Con []).
590 -- No big deal; it'll be inlined in a jiffy. I tried
591 -- parsing it to (Con con []) directly, but got bitten
592 -- when a real constructor Id showed up in an interface
593 -- file. As usual, a hack bites you in the end.
594 -- If you want to get a UfCon, then use the
595 -- curly-bracket notation (True {}).
597 | core_lit { UfCon (UfLitCon $1) [] }
598 | '(' core_expr ')' { $2 }
599 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
600 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
601 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
603 -- This one is dealt with by qdata_name: see above comments
604 -- | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
606 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
607 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
608 | core_expr ',' comma_exprs2 { $1 : $3 }
610 con_or_primop :: { UfCon RdrName }
611 con_or_primop : qdata_name { UfDataCon $1 }
612 | qvar_name { UfPrimOp $1 }
613 | '__ccall' ccall_string { let
614 (is_dyn, is_casm, may_gc) = $1
616 UfCCallOp $2 is_dyn is_casm may_gc
619 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
621 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
623 core_alts :: { [UfAlt RdrName] }
625 | core_alt ';' core_alts { $1 : $3 }
627 core_alt :: { UfAlt RdrName }
628 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
630 core_pat :: { (UfCon RdrName, [RdrName]) }
631 core_pat : core_lit { (UfLitCon $1, []) }
632 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
633 | qdata_name var_names { (UfDataCon $1, $2) }
634 | '(' comma_var_names1 ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
635 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
636 | '__DEFAULT' { (UfDefault, []) }
637 | '(' core_pat ')' { $2 }
640 comma_var_names1 :: { [RdrName] } -- One or more
641 comma_var_names1 : var_name { [$1] }
642 | var_name ',' comma_var_names1 { $1 : $3 }
644 core_lit :: { Literal }
645 core_lit : INTEGER { mkMachInt_safe $1 }
646 | CHAR { MachChar $1 }
647 | STRING { MachStr $1 }
648 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
649 | RATIONAL { MachDouble $1 }
650 | '__float' RATIONAL { MachFloat $2 }
652 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
653 -- The type checker will add the types
656 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
657 (panic "NoRepRational type")
658 -- The type checker will add the type
661 | '__addr' INTEGER { MachAddr $2 }
663 core_bndr :: { UfBinder RdrName }
664 core_bndr : core_val_bndr { $1 }
665 | core_tv_bndr { $1 }
667 core_bndrs :: { [UfBinder RdrName] }
669 | core_bndr core_bndrs { $1 : $2 }
671 core_val_bndr :: { UfBinder RdrName }
672 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
674 core_tv_bndr :: { UfBinder RdrName }
675 core_tv_bndr : '@' tv_name '::' akind { UfTyBinder $2 $4 }
676 | '@' tv_name { UfTyBinder $2 boxedTypeKind }
678 ccall_string :: { FAST_STRING }
683 ------------------------------------------------------------------------
684 scc :: { CostCentre }
685 : '__sccC' '{' mod_name STRING '}' { AllCafsCC $3 $4 }
686 | '__sccD' '{' mod_name STRING cc_dup '}' { AllDictsCC $3 $4 $5 }
687 | '__scc' '(' cc_name mod_name STRING cc_dict cc_dup cc_caf '}'
688 { NormalCC { cc_name = $3, cc_mod = $4, cc_grp = $5,
689 cc_is_dict = $6, cc_is_dupd = $7, cc_is_caf = $8 } }
691 cc_name :: { EncodedFS }
695 cc_dup :: { IsDupdCC }
696 cc_dup : { OriginalCC }
699 cc_caf :: { IsCafCC }
703 cc_dict :: { IsDictCC }
708 -------------------------------------------------------------------
710 src_loc :: { SrcLoc }
711 src_loc : {% getSrcLocIf }
713 checkVersion :: { () }
714 : {-empty-} {% checkVersion Nothing }
715 | INTEGER {% checkVersion (Just (fromInteger $1)) }
717 -------------------------------------------------------------------
722 data IfaceStuff = PIface EncodedFS{-.hi module name-} ParsedIface
723 | PIdInfo [HsIdInfo RdrName]
724 | PType RdrNameHsType
726 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc