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 )
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 $$ }
102 '..' { ITdotdot } -- reserved symbols
115 '/\\' { ITbiglam } -- GHC-extension symbols
117 '{' { ITocurly } -- special symbols
128 VARID { ITvarid $$ } -- identifiers
130 VARSYM { ITvarsym $$ }
131 CONSYM { ITconsym $$ }
132 QVARID { ITqvarid $$ }
133 QCONID { ITqconid $$ }
134 QVARSYM { ITqvarsym $$ }
135 QCONSYM { ITqconsym $$ }
137 PRAGMA { ITpragma $$ }
140 STRING { ITstring $$ }
141 INTEGER { ITinteger $$ }
142 RATIONAL { ITrational $$ }
144 UNKNOWN { ITunknown $$ }
147 -- iface_stuff is the main production.
148 -- It recognises (a) a whole interface file
149 -- (b) a type (so that type sigs can be parsed lazily)
150 -- (c) the IdInfo part of a signature (same reason)
152 iface_stuff :: { IfaceStuff }
153 iface_stuff : iface { let (nm, iff) = $1 in PIface nm iff }
155 | id_info { PIdInfo $1 }
158 iface :: { (EncodedFS, ParsedIface) }
159 iface : '__interface' mod_fs INTEGER checkVersion 'where'
165 { ( $2 -- Module name
167 (fromInteger $3) -- Module version
168 (reverse $6) -- Usages
169 (reverse $8) -- Exports
170 (reverse $7) -- Instance import modules
171 (reverse $10) -- Decls
172 (reverse $9) -- Local instances
176 --------------------------------------------------------------------------
178 import_part :: { [ImportVersion OccName] }
180 | import_part import_decl { $2 : $1 }
182 import_decl :: { ImportVersion OccName }
183 import_decl : 'import' mod_fs opt_bang INTEGER '::' whats_imported ';'
184 { (mkSysModuleFS $2 $3, fromInteger $4, $6) }
186 whats_imported :: { WhatsImported OccName }
187 whats_imported : { Everything }
188 | name_version_pair name_version_pairs { Specifically ($1:$2) }
190 name_version_pairs :: { [LocalVersion OccName] }
191 name_version_pairs : { [] }
192 | name_version_pair name_version_pairs { $1 : $2 }
194 name_version_pair :: { LocalVersion OccName }
195 name_version_pair : var_occ INTEGER { ($1, fromInteger $2) }
196 | tc_occ INTEGER { ($1, fromInteger $2) }
198 instance_import_part :: { [Module] }
199 instance_import_part : { [] }
200 | instance_import_part '__instimport' mod_name ';'
203 --------------------------------------------------------------------------
205 exports_part :: { [ExportItem] }
206 exports_part : { [] }
207 | exports_part '__export' opt_bang mod_fs entities ';'
208 { (mkSysModuleFS $4 $3,$5) : $1 }
210 opt_bang :: { IfaceFlavour }
211 opt_bang : { hiFile }
214 entities :: { [RdrAvailInfo] }
216 | entity entities { $1 : $2 }
218 entity :: { RdrAvailInfo }
219 entity : tc_occ { AvailTC $1 [$1] }
220 | var_occ { Avail $1 }
221 | tc_occ stuff_inside { AvailTC $1 ($1:$2) }
222 | tc_occ '|' stuff_inside { AvailTC $1 $3 }
224 stuff_inside :: { [OccName] }
225 stuff_inside : '{' val_occs '}' { $2 }
227 val_occ :: { OccName }
231 val_occs :: { [OccName] }
233 | val_occ val_occs { $1 : $2 }
236 --------------------------------------------------------------------------
238 fixity :: { FixityDirection }
239 fixity : 'infixl' { InfixL }
240 | 'infixr' { InfixR }
244 mb_fix : {-nothing-} { 9 }
245 | INTEGER { (fromInteger $1) }
247 -----------------------------------------------------------------------------
249 csigs :: { [RdrNameSig] }
251 | 'where' '{' csigs1 '}' { $3 }
253 csigs1 :: { [RdrNameSig] }
254 csigs1 : csig { [$1] }
255 | csig ';' csigs1 { $1 : $3 }
257 csig :: { RdrNameSig }
258 csig : src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
259 | src_loc var_name '=' '::' type
261 (Just (error "Un-filled-in default method"))
264 --------------------------------------------------------------------------
266 instance_decl_part :: { [RdrNameInstDecl] }
267 instance_decl_part : {- empty -} { [] }
268 | instance_decl_part inst_decl { $2 : $1 }
270 inst_decl :: { RdrNameInstDecl }
271 inst_decl : src_loc 'instance' type '=' var_name ';'
273 EmptyMonoBinds {- No bindings -}
274 [] {- No user pragmas -}
275 (Just $5) {- Dfun id -}
279 --------------------------------------------------------------------------
281 decls_part :: { [(Version, RdrNameHsDecl)] }
284 | decls_part version decl ';' { ($2,$3):$1 }
286 decl :: { RdrNameHsDecl }
287 decl : src_loc var_name '::' type maybe_idinfo
288 { SigD (IfaceSig $2 $4 ($5 $2) $1) }
289 | src_loc 'type' tc_name tv_bndrs '=' type
290 { TyClD (TySynonym $3 $4 $6 $1) }
291 | src_loc 'data' decl_context tc_name tv_bndrs constrs
292 { TyClD (TyData DataType $3 $4 $5 $6 Nothing noDataPragmas $1) }
293 | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
294 { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
295 | src_loc 'class' decl_context tc_name tv_bndrs csigs
296 { TyClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds
298 | src_loc fixity mb_fix var_or_data_name
299 { FixD (FixitySig $4 (Fixity $3 $2) $1) }
301 maybe_idinfo :: { RdrName -> [HsIdInfo RdrName] }
302 maybe_idinfo : {- empty -} { \_ -> [] }
303 | src_loc PRAGMA { \x ->
304 case parseIface $2 $1 of
305 Succeeded (PIdInfo id_info) -> id_info
306 Failed err -> pprPanic "IdInfo parse failed"
310 -----------------------------------------------------------------------------
312 version :: { Version }
313 version : INTEGER { fromInteger $1 }
315 decl_context :: { RdrNameContext }
316 decl_context : { [] }
317 | '{' context_list1 '}' '=>' { $2 }
319 ----------------------------------------------------------------
321 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
323 | '=' constrs1 { $2 }
325 constrs1 :: { [RdrNameConDecl] }
326 constrs1 : constr { [$1] }
327 | constr '|' constrs1 { $1 : $3 }
329 constr :: { RdrNameConDecl }
330 constr : src_loc ex_stuff data_name batypes { mkConDecl $3 $2 (VanillaCon $4) $1 }
331 | src_loc ex_stuff data_name '{' fields1 '}' { mkConDecl $3 $2 (RecCon $5) $1 }
332 -- We use "data_fs" so as to include ()
334 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
335 newtype_constr : { [] }
336 | src_loc '=' ex_stuff data_name atype { [mkConDecl $4 $3 (NewCon $5 Nothing) $1] }
337 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
338 { [mkConDecl $4 $3 (NewCon $8 (Just $6)) $1] }
340 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
341 ex_stuff : { ([],[]) }
342 | '__forall' forall context '=>' { ($2,$3) }
344 batypes :: { [RdrNameBangType] }
346 | batype batypes { $1 : $2 }
348 batype :: { RdrNameBangType }
349 batype : atype { Unbanged $1 }
350 | '!' atype { Banged $2 }
352 fields1 :: { [([RdrName], RdrNameBangType)] }
353 fields1 : field { [$1] }
354 | field ',' fields1 { $1 : $3 }
356 field :: { ([RdrName], RdrNameBangType) }
357 field : var_names1 '::' type { ($1, Unbanged $3) }
358 | var_names1 '::' '!' type { ($1, Banged $4) }
359 --------------------------------------------------------------------------
361 type :: { RdrNameHsType }
362 type : '__forall' forall context '=>' type
363 { mkHsForAllTy $2 $3 $5 }
364 | btype '->' type { MonoFunTy $1 $3 }
367 forall :: { [HsTyVar RdrName] }
368 forall : '[' tv_bndrs ']' { $2 }
370 context :: { RdrNameContext }
372 | '{' context_list1 '}' { $2 }
374 context_list1 :: { RdrNameContext }
375 context_list1 : class { [$1] }
376 | class ',' context_list1 { $1 : $3 }
378 class :: { (RdrName, [RdrNameHsType]) }
379 class : qcls_name atypes { ($1, $2) }
381 types2 :: { [RdrNameHsType] {- Two or more -} }
382 types2 : type ',' type { [$1,$3] }
383 | type ',' types2 { $1 : $3 }
385 btype :: { RdrNameHsType }
387 | btype atype { MonoTyApp $1 $2 }
389 atype :: { RdrNameHsType }
390 atype : qtc_name { MonoTyVar $1 }
391 | tv_name { MonoTyVar $1 }
392 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
393 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
394 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
395 | '[' type ']' { MonoListTy $2 }
396 | '{' qcls_name atypes '}' { MonoDictTy $2 $3 }
397 | '(' type ')' { $2 }
399 -- This one is dealt with via qtc_name
400 -- | '(' ')' { MonoTupleTy [] True }
402 atypes :: { [RdrNameHsType] {- Zero or more -} }
404 | atype atypes { $1 : $2 }
405 ---------------------------------------------------------------------
406 mod_fs :: { EncodedFS }
409 mod_name :: { Module }
410 : mod_fs { mkSysModuleFS $1 hiFile }
413 ---------------------------------------------------
414 var_fs :: { EncodedFS }
421 qvar_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
425 var_occ :: { OccName }
426 : var_fs { mkSysOccFS varName $1 }
428 var_name :: { RdrName }
429 var_name : var_occ { mkRdrUnqual $1 }
431 qvar_name :: { RdrName }
432 qvar_name : var_name { $1 }
433 | qvar_fs { mkSysQual varName $1 }
435 var_names :: { [RdrName] }
437 | var_name var_names { $1 : $2 }
439 var_names1 :: { [RdrName] }
440 var_names1 : var_name var_names { $1 : $2 }
442 ---------------------------------------------------
443 -- For some bizarre reason,
444 -- (,,,) is dealt with by the parser
445 -- Foo.(,,,) is dealt with by the lexer
448 data_fs :: { EncodedFS }
452 qdata_fs :: { (EncodedFS, EncodedFS, IfaceFlavour) }
456 data_occ :: { OccName }
457 : data_fs { mkSysOccFS dataName $1 }
459 data_name :: { RdrName }
460 : data_occ { mkRdrUnqual $1 }
462 qdata_name :: { RdrName }
463 qdata_name : data_name { $1 }
464 | qdata_fs { mkSysQual dataName $1 }
466 qdata_names :: { [RdrName] }
468 | qdata_name qdata_names { $1 : $2 }
470 var_or_data_name :: { RdrName }
474 ---------------------------------------------------
475 tc_fs :: { EncodedFS }
478 tc_occ :: { OccName }
479 : tc_fs { mkSysOccFS tcName $1 }
481 tc_name :: { RdrName }
482 : tc_occ { mkRdrUnqual $1 }
484 qtc_name :: { RdrName }
486 | qdata_fs { mkSysQual tcName $1 }
488 ---------------------------------------------------
489 cls_name :: { RdrName }
490 : data_fs { mkSysUnqual clsName $1 }
492 qcls_name :: { RdrName }
494 | qdata_fs { mkSysQual clsName $1 }
496 ---------------------------------------------------
497 tv_name :: { RdrName }
498 : VARID { mkSysUnqual tvName $1 }
499 | VARSYM { mkSysUnqual tvName $1 {- Allow t2 as a tyvar -} }
501 tv_bndr :: { HsTyVar RdrName }
502 : tv_name '::' akind { IfaceTyVar $1 $3 }
503 | tv_name { IfaceTyVar $1 boxedTypeKind }
505 tv_bndrs :: { [HsTyVar RdrName] }
507 | tv_bndr tv_bndrs { $1 : $2 }
509 ---------------------------------------------------
512 | akind '->' kind { mkArrowKind $1 $3 }
515 : VARSYM { if $1 == SLIT("*") then
517 else if $1 == SLIT("?") then
519 else panic "ParseInterface: akind"
521 | '(' kind ')' { $2 }
523 --------------------------------------------------------------------------
525 id_info :: { [HsIdInfo RdrName] }
527 | id_info_item id_info { $1 : $2 }
529 id_info_item :: { HsIdInfo RdrName }
530 id_info_item : '__A' arity_info { HsArity $2 }
531 | strict_info { HsStrictness $1 }
532 | '__U' core_expr { HsUnfold $1 (Just $2) }
533 | '__U' { HsUnfold $1 Nothing }
535 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
536 | '__C' { HsNoCafRefs }
539 spec_tvs :: { [HsTyVar RdrName] }
540 spec_tvs : '[' tv_bndrs ']' { $2 }
543 arity_info :: { ArityInfo }
544 arity_info : INTEGER { exactArity (fromInteger $1) }
546 strict_info :: { HsStrictnessInfo RdrName }
547 strict_info : '__S' qvar_name '{' qdata_names '}'
548 { HsStrictnessInfo $1 (Just ($2,$4)) }
549 | '__S' qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
550 | '__S' { HsStrictnessInfo $1 Nothing }
552 -------------------------------------------------------
553 core_expr :: { UfExpr RdrName }
554 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
555 | 'case' core_expr 'of' var_name
556 '{' core_alts '}' { UfCase $2 $4 $6 }
558 | 'let' '{' core_val_bndr '=' core_expr
559 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
560 | '__letrec' '{' rec_binds '}'
561 'in' core_expr { UfLet (UfRec $3) $6 }
563 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
564 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
566 | '__inline' core_expr { UfNote UfInlineCall $2 }
567 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
568 | scc core_expr { UfNote (UfSCC $1) $2 }
571 fexpr :: { UfExpr RdrName }
572 fexpr : fexpr core_arg { UfApp $1 $2 }
575 core_arg :: { UfExpr RdrName }
576 : '@' atype { UfType $2 }
579 core_args :: { [UfExpr RdrName] }
581 | core_arg core_args { $1 : $2 }
583 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
584 core_aexpr : qvar_name { UfVar $1 }
586 | qdata_name { UfVar $1 }
587 -- This one means that e.g. "True" will parse as
588 -- (UfVar True_Id) rather than (UfCon True_Con []).
589 -- No big deal; it'll be inlined in a jiffy. I tried
590 -- parsing it to (Con con []) directly, but got bitten
591 -- when a real constructor Id showed up in an interface
592 -- file. As usual, a hack bites you in the end.
593 -- If you want to get a UfCon, then use the
594 -- curly-bracket notation (True {}).
596 | core_lit { UfCon (UfLitCon $1) [] }
597 | '(' core_expr ')' { $2 }
598 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
599 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
600 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
602 -- This one is dealt with by qdata_name: see above comments
603 -- | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
605 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
606 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
607 | core_expr ',' comma_exprs2 { $1 : $3 }
609 con_or_primop :: { UfCon RdrName }
610 con_or_primop : qdata_name { UfDataCon $1 }
611 | qvar_name { UfPrimOp $1 }
612 | '__ccall' ccall_string { let
613 (is_dyn, is_casm, may_gc) = $1
615 UfCCallOp $2 is_dyn is_casm may_gc
618 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
620 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
622 core_alts :: { [UfAlt RdrName] }
624 | core_alt ';' core_alts { $1 : $3 }
626 core_alt :: { UfAlt RdrName }
627 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
629 core_pat :: { (UfCon RdrName, [RdrName]) }
630 core_pat : core_lit { (UfLitCon $1, []) }
631 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
632 | qdata_name var_names { (UfDataCon $1, $2) }
633 | '(' comma_var_names1 ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
634 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
635 | '__DEFAULT' { (UfDefault, []) }
636 | '(' core_pat ')' { $2 }
639 comma_var_names1 :: { [RdrName] } -- One or more
640 comma_var_names1 : var_name { [$1] }
641 | var_name ',' comma_var_names1 { $1 : $3 }
643 core_lit :: { Literal }
644 core_lit : INTEGER { mkMachInt_safe $1 }
645 | CHAR { MachChar $1 }
646 | STRING { MachStr $1 }
647 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
648 | RATIONAL { MachDouble $1 }
649 | '__float' RATIONAL { MachFloat $2 }
651 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
652 -- The type checker will add the types
655 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
656 (panic "NoRepRational type")
657 -- The type checker will add the type
660 | '__addr' INTEGER { MachAddr $2 }
662 core_bndr :: { UfBinder RdrName }
663 core_bndr : core_val_bndr { $1 }
664 | core_tv_bndr { $1 }
666 core_bndrs :: { [UfBinder RdrName] }
668 | core_bndr core_bndrs { $1 : $2 }
670 core_val_bndr :: { UfBinder RdrName }
671 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
673 core_tv_bndr :: { UfBinder RdrName }
674 core_tv_bndr : '@' tv_name '::' akind { UfTyBinder $2 $4 }
675 | '@' tv_name { UfTyBinder $2 boxedTypeKind }
677 ccall_string :: { FAST_STRING }
682 ------------------------------------------------------------------------
683 scc :: { CostCentre }
684 : '__sccC' '{' mod_name STRING '}' { AllCafsCC $3 $4 }
685 | '__scc' '(' cc_name mod_name STRING cc_dup cc_caf '}'
686 { NormalCC { cc_name = $3, cc_mod = $4, cc_grp = $5,
687 cc_is_dupd = $6, cc_is_caf = $7 } }
689 cc_name :: { EncodedFS }
693 cc_dup :: { IsDupdCC }
694 cc_dup : { OriginalCC }
697 cc_caf :: { IsCafCC }
701 -------------------------------------------------------------------
703 src_loc :: { SrcLoc }
704 src_loc : {% getSrcLocIf }
706 checkVersion :: { () }
707 : {-empty-} {% checkVersion Nothing }
708 | INTEGER {% checkVersion (Just (fromInteger $1)) }
710 -------------------------------------------------------------------
715 data IfaceStuff = PIface EncodedFS{-.hi module name-} ParsedIface
716 | PIdInfo [HsIdInfo RdrName]
717 | PType RdrNameHsType
719 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc