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 ( IfaceFlavour(..), Fixity(..), FixityDirection(..),
13 NewOrData(..), Version
15 import HsPragmas ( noDataPragmas, noClassPragmas )
16 import Type ( Kind, mkArrowKind, boxedTypeKind, openTypeKind )
17 import IdInfo ( ArityInfo, exactArity )
20 import RnEnv ( ifaceUnqualTC, ifaceUnqualVar, ifaceUnqualTv, ifaceQualVar, ifaceQualTC )
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 Name ( OccName, isTCOcc, Provenance, Module,
27 varOcc, tcOcc, mkModuleFS
29 import PrelMods ( mkTupNameStr, mkUbxTupNameStr )
30 import PrelInfo ( mkTupConRdrName, mkUbxTupConRdrName )
31 import SrcLoc ( SrcLoc )
43 %tokentype { IfaceToken }
44 %monad { IfM }{ thenIf }{ returnIf }
45 %lexer { lexIface } { ITeof }
48 'case' { ITcase } -- Haskell keywords
51 'default' { ITdefault }
52 'deriving' { ITderiving }
61 'instance' { ITinstance }
64 'newtype' { ITnewtype }
70 'qualified' { ITqualified }
73 '__interface' { ITinterface } -- GHC-extension keywords
74 '__export' { ITexport }
75 '__instimport' { ITinstimport }
76 '__forall' { ITforall }
77 '__letrec' { ITletrec }
78 '__coerce' { ITcoerce }
79 '__inline' { ITinline }
80 '__DEFAULT' { ITdefaultbranch }
82 '__integer' { ITinteger_lit }
83 '__float' { ITfloat_lit }
84 '__rational' { ITrational_lit }
85 '__addr' { ITaddr_lit }
86 '__litlit' { ITlit_lit }
87 '__string' { ITstring_lit }
88 '__ccall' { ITccall $$ }
93 '__P' { ITspecialise }
98 '..' { ITdotdot } -- reserved symbols
111 '/\\' { ITbiglam } -- GHC-extension symbols
113 '{' { ITocurly } -- special symbols
124 VARID { ITvarid $$ } -- identifiers
126 VARSYM { ITvarsym $$ }
127 CONSYM { ITconsym $$ }
128 QVARID { ITqvarid $$ }
129 QCONID { ITqconid $$ }
130 QVARSYM { ITqvarsym $$ }
131 QCONSYM { ITqconsym $$ }
133 PRAGMA { ITpragma $$ }
136 STRING { ITstring $$ }
137 INTEGER { ITinteger $$ }
138 RATIONAL { ITrational $$ }
140 UNKNOWN { ITunknown $$ }
143 -- iface_stuff is the main production.
144 -- It recognises (a) a whole interface file
145 -- (b) a type (so that type sigs can be parsed lazily)
146 -- (c) the IdInfo part of a signature (same reason)
148 iface_stuff :: { IfaceStuff }
149 iface_stuff : iface { PIface $1 }
151 | id_info { PIdInfo $1 }
154 iface :: { ParsedIface }
155 iface : '__interface' mod_name INTEGER checkVersion 'where'
163 (fromInteger $3) -- Module version
164 (reverse $6) -- Usages
165 (reverse $8) -- Exports
166 (reverse $7) -- Instance import modules
167 (reverse $10) -- Decls
168 (reverse $9) -- Local instances
171 --------------------------------------------------------------------------
173 import_part :: { [ImportVersion OccName] }
175 | import_part import_decl { $2 : $1 }
177 import_decl :: { ImportVersion OccName }
178 import_decl : 'import' mod_name opt_bang INTEGER '::' whats_imported ';'
179 { ($2, $3, fromInteger $4, $6) }
181 whats_imported :: { WhatsImported OccName }
182 whats_imported : { Everything }
183 | name_version_pair name_version_pairs { Specifically ($1:$2) }
185 name_version_pairs :: { [LocalVersion OccName] }
186 name_version_pairs : { [] }
187 | name_version_pair name_version_pairs { $1 : $2 }
189 name_version_pair :: { LocalVersion OccName }
190 name_version_pair : entity_occ INTEGER { ($1, fromInteger $2) }
192 instance_import_part :: { [Module] }
193 instance_import_part : { [] }
194 | instance_import_part '__instimport' mod_name ';'
197 --------------------------------------------------------------------------
199 exports_part :: { [ExportItem] }
200 exports_part : { [] }
201 | exports_part '__export' opt_bang mod_name entities ';'
204 opt_bang :: { IfaceFlavour }
205 opt_bang : { HiFile }
208 entities :: { [RdrAvailInfo] }
210 | entity entities { $1 : $2 }
212 entity :: { RdrAvailInfo }
213 entity : entity_occ { if isTCOcc $1
216 | entity_occ stuff_inside { AvailTC $1 ($1:$2) }
217 | entity_occ '|' stuff_inside { AvailTC $1 $3 }
219 stuff_inside :: { [OccName] }
220 stuff_inside : '{' val_occs '}' { $2 }
222 --------------------------------------------------------------------------
224 fixity :: { FixityDirection }
225 fixity : 'infixl' { InfixL }
226 | 'infixr' { InfixR }
230 mb_fix : {-nothing-} { 9 }
231 | INTEGER { (fromInteger $1) }
233 -----------------------------------------------------------------------------
235 csigs :: { [RdrNameSig] }
237 | 'where' '{' csigs1 '}' { $3 }
239 csigs1 :: { [RdrNameSig] }
240 csigs1 : csig { [$1] }
241 | csig ';' csigs1 { $1 : $3 }
243 csig :: { RdrNameSig }
244 csig : src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
245 | src_loc var_name '=' '::' type
247 (Just (error "Un-filled-in default method"))
250 --------------------------------------------------------------------------
252 instance_decl_part :: { [RdrNameInstDecl] }
253 instance_decl_part : {- empty -} { [] }
254 | instance_decl_part inst_decl { $2 : $1 }
256 inst_decl :: { RdrNameInstDecl }
257 inst_decl : src_loc 'instance' type '=' var_name ';'
259 EmptyMonoBinds {- No bindings -}
260 [] {- No user pragmas -}
261 (Just $5) {- Dfun id -}
265 --------------------------------------------------------------------------
267 decls_part :: { [(Version, RdrNameHsDecl)] }
270 | decls_part version decl ';' { ($2,$3):$1 }
272 decl :: { RdrNameHsDecl }
273 decl : src_loc var_name '::' type maybe_idinfo
274 { SigD (IfaceSig $2 $4 ($5 $2) $1) }
275 | src_loc 'type' tc_name tv_bndrs '=' type
276 { TyClD (TySynonym $3 $4 $6 $1) }
277 | src_loc 'data' decl_context data_fs tv_bndrs constrs
278 { TyClD (TyData DataType $3 (ifaceUnqualTC $4) $5 $6 Nothing noDataPragmas $1) }
279 | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
280 { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
281 | src_loc 'class' decl_context tc_name tv_bndrs csigs
282 { TyClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds
284 | src_loc fixity mb_fix val_occ
285 { FixD (FixitySig (Unqual $4) (Fixity $3 $2) $1) }
287 maybe_idinfo :: { RdrName -> [HsIdInfo RdrName] }
288 maybe_idinfo : {- empty -} { \_ -> [] }
289 | src_loc PRAGMA { \x ->
290 case parseIface $2 $1 of
291 Succeeded (PIdInfo id_info) -> id_info
292 Failed err -> pprPanic "IdInfo parse failed"
296 -----------------------------------------------------------------------------
298 version :: { Version }
299 version : INTEGER { fromInteger $1 }
301 decl_context :: { RdrNameContext }
302 decl_context : { [] }
303 | '{' context_list1 '}' '=>' { $2 }
305 ----------------------------------------------------------------
307 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
309 | '=' constrs1 { $2 }
311 constrs1 :: { [RdrNameConDecl] }
312 constrs1 : constr { [$1] }
313 | constr '|' constrs1 { $1 : $3 }
315 constr :: { RdrNameConDecl }
316 constr : src_loc ex_stuff data_fs batypes { mkConDecl (ifaceUnqualVar $3) $2 (VanillaCon $4) $1 }
317 | src_loc ex_stuff data_fs '{' fields1 '}' { mkConDecl (ifaceUnqualVar $3) $2 (RecCon $5) $1 }
318 -- We use "data_fs" so as to include ()
320 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
321 newtype_constr : { [] }
322 | src_loc '=' ex_stuff data_name atype { [mkConDecl $4 $3 (NewCon $5 Nothing) $1] }
323 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
324 { [mkConDecl $4 $3 (NewCon $8 (Just $6)) $1] }
326 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
327 ex_stuff : { ([],[]) }
328 | '__forall' forall context '=>' { ($2,$3) }
330 batypes :: { [RdrNameBangType] }
332 | batype batypes { $1 : $2 }
334 batype :: { RdrNameBangType }
335 batype : atype { Unbanged $1 }
336 | '!' atype { Banged $2 }
338 fields1 :: { [([RdrName], RdrNameBangType)] }
339 fields1 : field { [$1] }
340 | field ',' fields1 { $1 : $3 }
342 field :: { ([RdrName], RdrNameBangType) }
343 field : var_names1 '::' type { ($1, Unbanged $3) }
344 | var_names1 '::' '!' type { ($1, Banged $4) }
345 --------------------------------------------------------------------------
347 type :: { RdrNameHsType }
348 type : '__forall' forall context '=>' type
349 { mkHsForAllTy $2 $3 $5 }
350 | btype '->' type { MonoFunTy $1 $3 }
353 forall :: { [HsTyVar RdrName] }
354 forall : '[' tv_bndrs ']' { $2 }
356 context :: { RdrNameContext }
358 | '{' context_list1 '}' { $2 }
360 context_list1 :: { RdrNameContext }
361 context_list1 : class { [$1] }
362 | class ',' context_list1 { $1 : $3 }
364 class :: { (RdrName, [RdrNameHsType]) }
365 class : qtc_name atypes { ($1, $2) }
367 types2 :: { [RdrNameHsType] {- Two or more -} }
368 types2 : type ',' type { [$1,$3] }
369 | type ',' types2 { $1 : $3 }
371 btype :: { RdrNameHsType }
373 | btype atype { MonoTyApp $1 $2 }
375 atype :: { RdrNameHsType }
376 atype : qtc_name { MonoTyVar $1 }
377 | tv_name { MonoTyVar $1 }
378 | '(' ')' { MonoTupleTy [] True }
379 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
380 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
381 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
382 | '[' type ']' { MonoListTy $2 }
383 | '{' qtc_name atypes '}' { MonoDictTy $2 $3 }
384 | '(' type ')' { $2 }
386 atypes :: { [RdrNameHsType] {- Zero or more -} }
388 | atype atypes { $1 : $2 }
389 ---------------------------------------------------------------------
391 mod_name :: { Module }
392 : CONID { mkModuleFS $1 }
394 var_fs :: { FAST_STRING }
400 data_fs :: { FAST_STRING }
403 | '->' { SLIT("->") }
404 | '(' ')' { SLIT("()") }
405 | '(' commas ')' { snd (mkTupNameStr $2) }
406 | '[' ']' { SLIT("[]") }
410 | commas ',' { $1 + 1 }
412 val_occ :: { OccName }
413 : var_fs { varOcc $1 }
414 | data_fs { varOcc $1 }
416 val_occs :: { [OccName] }
418 | val_occ val_occs { $1 : $2 }
420 entity_occ :: { OccName }
421 : var_fs { varOcc $1 }
422 | data_fs { tcOcc $1 }
424 var_name :: { RdrName }
425 var_name : var_fs { ifaceUnqualVar $1 }
427 qvar_name :: { RdrName }
428 qvar_name : var_name { $1 }
429 | QVARID { ifaceQualVar $1 }
430 | QVARSYM { ifaceQualVar $1 }
432 var_names :: { [RdrName] }
434 | var_name var_names { $1 : $2 }
436 var_names1 :: { [RdrName] }
437 var_names1 : var_name var_names { $1 : $2 }
439 data_name :: { RdrName }
440 : CONID { ifaceUnqualVar $1 }
441 | CONSYM { ifaceUnqualVar $1 }
442 | '(' commas ')' { ifaceUnqualVar (snd (mkTupNameStr $2)) }
443 | '[' ']' { ifaceUnqualVar SLIT("[]") }
445 qdata_name :: { RdrName }
446 qdata_name : data_name { $1 }
447 | QCONID { ifaceQualVar $1 }
448 | QCONSYM { ifaceQualVar $1 }
450 qdata_names :: { [RdrName] }
452 | qdata_name qdata_names { $1 : $2 }
454 tc_name :: { RdrName }
455 tc_name : CONID { ifaceUnqualTC $1 }
456 | CONSYM { ifaceUnqualTC $1 }
457 | '(' '->' ')' { ifaceUnqualTC SLIT("->") }
458 | '(' commas ')' { ifaceUnqualTC (snd (mkTupNameStr $2)) }
459 | '[' ']' { ifaceUnqualTC SLIT("[]") }
461 qtc_name :: { RdrName }
462 qtc_name : tc_name { $1 }
463 | QCONID { ifaceQualTC $1 }
464 | QCONSYM { ifaceQualTC $1 }
466 tv_name :: { RdrName }
467 tv_name : VARID { ifaceUnqualTv $1 }
468 | VARSYM { ifaceUnqualTv $1 {- Allow t2 as a tyvar -} }
470 tv_bndr :: { HsTyVar RdrName }
471 tv_bndr : tv_name '::' akind { IfaceTyVar $1 $3 }
472 | tv_name { IfaceTyVar $1 boxedTypeKind }
474 tv_bndrs :: { [HsTyVar RdrName] }
476 | tv_bndr tv_bndrs { $1 : $2 }
480 | akind '->' kind { mkArrowKind $1 $3 }
483 : VARSYM { if $1 == SLIT("*") then
485 else if $1 == SLIT("?") then
487 else panic "ParseInterface: akind"
489 | '(' kind ')' { $2 }
491 --------------------------------------------------------------------------
493 id_info :: { [HsIdInfo RdrName] }
495 | id_info_item id_info { $1 : $2 }
497 id_info_item :: { HsIdInfo RdrName }
498 id_info_item : '__A' arity_info { HsArity $2 }
499 | strict_info { HsStrictness $1 }
500 | '__U' core_expr { HsUnfold $1 (Just $2) }
501 | '__U' { HsUnfold $1 Nothing }
503 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
504 | '__C' { HsNoCafRefs }
507 spec_tvs :: { [HsTyVar RdrName] }
508 spec_tvs : '[' tv_bndrs ']' { $2 }
511 arity_info :: { ArityInfo }
512 arity_info : INTEGER { exactArity (fromInteger $1) }
514 strict_info :: { HsStrictnessInfo RdrName }
515 strict_info : '__S' qvar_name '{' qdata_names '}'
516 { HsStrictnessInfo $1 (Just ($2,$4)) }
517 | '__S' qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
518 | '__S' { HsStrictnessInfo $1 Nothing }
520 -------------------------------------------------------
521 core_expr :: { UfExpr RdrName }
522 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
523 | 'case' core_expr 'of' var_name
524 '{' core_alts '}' { UfCase $2 $4 $6 }
526 | 'let' '{' core_val_bndr '=' core_expr
527 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
528 | '__letrec' '{' rec_binds '}'
529 'in' core_expr { UfLet (UfRec $3) $6 }
531 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
532 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
534 | '__inline' core_expr { UfNote UfInlineCall $2 }
535 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
536 | '__scc' core_expr { UfNote (UfSCC $1) $2 }
539 fexpr :: { UfExpr RdrName }
540 fexpr : fexpr core_arg { UfApp $1 $2 }
543 core_arg :: { UfExpr RdrName }
544 : '__a' atype { UfType $2 }
547 core_args :: { [UfExpr RdrName] }
549 | core_arg core_args { $1 : $2 }
551 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
552 core_aexpr : qvar_name { UfVar $1 }
554 | qdata_name { UfVar $1 }
555 -- This one means that e.g. "True" will parse as
556 -- (UfVar True_Id) rather than (UfCon True_Con []).
557 -- No big deal; it'll be inlined in a jiffy. I tried
558 -- parsing it to (Con con []) directly, but got bitten
559 -- when a real constructor Id showed up in an interface
560 -- file. As usual, a hack bites you in the end.
561 -- If you want to get a UfCon, then use the
562 -- curly-bracket notation (True {}).
564 | core_lit { UfCon (UfLitCon $1) [] }
565 | '(' core_expr ')' { $2 }
566 | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
567 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
568 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
569 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
571 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
572 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
573 | core_expr ',' comma_exprs2 { $1 : $3 }
575 con_or_primop :: { UfCon RdrName }
576 con_or_primop : qdata_name { UfDataCon $1 }
577 | qvar_name { UfPrimOp $1 }
578 | '__ccall' ccall_string { let
579 (is_dyn, is_casm, may_gc) = $1
581 UfCCallOp $2 is_dyn is_casm may_gc
584 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
586 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
588 core_alts :: { [UfAlt RdrName] }
590 | core_alt ';' core_alts { $1 : $3 }
592 core_alt :: { UfAlt RdrName }
593 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
595 core_pat :: { (UfCon RdrName, [RdrName]) }
596 core_pat : core_lit { (UfLitCon $1, []) }
597 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
598 | qdata_name var_names { (UfDataCon $1, $2) }
599 | '(' comma_var_names ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
600 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
601 | '__DEFAULT' { (UfDefault, []) }
602 | '(' core_pat ')' { $2 }
605 comma_var_names :: { [RdrName] } -- Zero, or two or more
606 comma_var_names : { [] }
607 | var_name ',' comma_var_names1 { $1 : $3 }
609 comma_var_names1 :: { [RdrName] } -- One or more
610 comma_var_names1 : var_name { [$1] }
611 | var_name ',' comma_var_names1 { $1 : $3 }
613 core_lit :: { Literal }
614 core_lit : INTEGER { mkMachInt_safe $1 }
615 | CHAR { MachChar $1 }
616 | STRING { MachStr $1 }
617 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
618 | RATIONAL { MachDouble $1 }
619 | '__float' RATIONAL { MachFloat $2 }
621 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
622 -- The type checker will add the types
625 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
626 (panic "NoRepRational type")
627 -- The type checker will add the type
630 | '__addr' INTEGER { MachAddr $2 }
632 core_bndr :: { UfBinder RdrName }
633 core_bndr : core_val_bndr { $1 }
634 | core_tv_bndr { $1 }
636 core_bndrs :: { [UfBinder RdrName] }
638 | core_bndr core_bndrs { $1 : $2 }
640 core_val_bndr :: { UfBinder RdrName }
641 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
643 core_tv_bndr :: { UfBinder RdrName }
644 core_tv_bndr : '__a' tv_name '::' akind { UfTyBinder $2 $4 }
645 | '__a' tv_name { UfTyBinder $2 boxedTypeKind }
647 ccall_string :: { FAST_STRING }
652 -------------------------------------------------------------------
654 src_loc :: { SrcLoc }
655 src_loc : {% getSrcLocIf }
657 checkVersion :: { () }
658 : {-empty-} {% checkVersion Nothing }
659 | INTEGER {% checkVersion (Just (fromInteger $1)) }
661 -------------------------------------------------------------------
666 data IfaceStuff = PIface ParsedIface
667 | PIdInfo [HsIdInfo RdrName]
668 | PType RdrNameHsType
670 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc