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 )
39 %tokentype { IfaceToken }
40 %monad { IfM }{ thenIf }{ returnIf }
41 %lexer { lexIface } { ITeof }
44 'case' { ITcase } -- Haskell keywords
47 'default' { ITdefault }
48 'deriving' { ITderiving }
57 'instance' { ITinstance }
60 'newtype' { ITnewtype }
66 'qualified' { ITqualified }
69 '__interface' { ITinterface } -- GHC-extension keywords
70 '__export' { ITexport }
71 '__instimport' { ITinstimport }
72 '__forall' { ITforall }
73 '__letrec' { ITletrec }
74 '__coerce' { ITcoerce }
75 '__inline' { ITinline }
76 '__DEFAULT' { ITdefaultbranch }
78 '__integer' { ITinteger_lit }
79 '__float' { ITfloat_lit }
80 '__rational' { ITrational_lit }
81 '__addr' { ITaddr_lit }
82 '__litlit' { ITlit_lit }
83 '__string' { ITstring_lit }
84 '__ccall' { ITccall $$ }
89 '__P' { ITspecialise }
94 '..' { ITdotdot } -- reserved symbols
107 '/\\' { ITbiglam } -- GHC-extension symbols
109 '{' { ITocurly } -- special symbols
120 VARID { ITvarid $$ } -- identifiers
122 VARSYM { ITvarsym $$ }
123 CONSYM { ITconsym $$ }
124 QVARID { ITqvarid $$ }
125 QCONID { ITqconid $$ }
126 QVARSYM { ITqvarsym $$ }
127 QCONSYM { ITqconsym $$ }
129 PRAGMA { ITpragma $$ }
132 STRING { ITstring $$ }
133 INTEGER { ITinteger $$ }
134 RATIONAL { ITrational $$ }
136 UNKNOWN { ITunknown $$ }
139 -- iface_stuff is the main production.
140 -- It recognises (a) a whole interface file
141 -- (b) a type (so that type sigs can be parsed lazily)
142 -- (c) the IdInfo part of a signature (same reason)
144 iface_stuff :: { IfaceStuff }
145 iface_stuff : iface { PIface $1 }
147 | id_info { PIdInfo $1 }
150 iface :: { ParsedIface }
151 iface : '__interface' mod_name INTEGER checkVersion 'where'
159 (fromInteger $3) -- Module version
160 (reverse $6) -- Usages
161 (reverse $8) -- Exports
162 (reverse $7) -- Instance import modules
163 (reverse $10) -- Decls
164 (reverse $9) -- Local instances
167 --------------------------------------------------------------------------
169 import_part :: { [ImportVersion OccName] }
171 | import_part import_decl { $2 : $1 }
173 import_decl :: { ImportVersion OccName }
174 import_decl : 'import' mod_name opt_bang INTEGER '::' whats_imported ';'
175 { ($2, $3, fromInteger $4, $6) }
177 whats_imported :: { WhatsImported OccName }
178 whats_imported : { Everything }
179 | name_version_pair name_version_pairs { Specifically ($1:$2) }
181 name_version_pairs :: { [LocalVersion OccName] }
182 name_version_pairs : { [] }
183 | name_version_pair name_version_pairs { $1 : $2 }
185 name_version_pair :: { LocalVersion OccName }
186 name_version_pair : entity_occ INTEGER { ($1, fromInteger $2) }
188 instance_import_part :: { [Module] }
189 instance_import_part : { [] }
190 | instance_import_part '__instimport' mod_name ';'
193 --------------------------------------------------------------------------
195 exports_part :: { [ExportItem] }
196 exports_part : { [] }
197 | exports_part '__export' opt_bang mod_name entities ';'
200 opt_bang :: { IfaceFlavour }
201 opt_bang : { HiFile }
204 entities :: { [RdrAvailInfo] }
206 | entity entities { $1 : $2 }
208 entity :: { RdrAvailInfo }
209 entity : entity_occ { if isTCOcc $1
212 | entity_occ stuff_inside { AvailTC $1 ($1:$2) }
213 | entity_occ '|' stuff_inside { AvailTC $1 $3 }
215 stuff_inside :: { [OccName] }
216 stuff_inside : '{' val_occs '}' { $2 }
218 --------------------------------------------------------------------------
220 fixity :: { FixityDirection }
221 fixity : 'infixl' { InfixL }
222 | 'infixr' { InfixR }
226 mb_fix : {-nothing-} { 9 }
227 | INTEGER { (fromInteger $1) }
229 -----------------------------------------------------------------------------
231 csigs :: { [RdrNameSig] }
233 | 'where' '{' csigs1 '}' { $3 }
235 csigs1 :: { [RdrNameSig] }
236 csigs1 : csig { [$1] }
237 | csig ';' csigs1 { $1 : $3 }
239 csig :: { RdrNameSig }
240 csig : src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
241 | src_loc var_name '=' '::' type
243 (Just (error "Un-filled-in default method"))
246 --------------------------------------------------------------------------
248 instance_decl_part :: { [RdrNameInstDecl] }
249 instance_decl_part : {- empty -} { [] }
250 | instance_decl_part inst_decl { $2 : $1 }
252 inst_decl :: { RdrNameInstDecl }
253 inst_decl : src_loc 'instance' type '=' var_name ';'
255 EmptyMonoBinds {- No bindings -}
256 [] {- No user pragmas -}
257 (Just $5) {- Dfun id -}
261 --------------------------------------------------------------------------
263 decls_part :: { [(Version, RdrNameHsDecl)] }
266 | decls_part version decl ';' { ($2,$3):$1 }
268 decl :: { RdrNameHsDecl }
269 decl : src_loc var_name '::' type maybe_idinfo
270 { SigD (IfaceSig $2 $4 ($5 $2) $1) }
271 | src_loc 'type' tc_name tv_bndrs '=' type
272 { TyClD (TySynonym $3 $4 $6 $1) }
273 | src_loc 'data' decl_context data_fs tv_bndrs constrs
274 { TyClD (TyData DataType $3 (ifaceUnqualTC $4) $5 $6 Nothing noDataPragmas $1) }
275 | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
276 { TyClD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
277 | src_loc 'class' decl_context tc_name tv_bndrs csigs
278 { TyClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds
280 | src_loc fixity mb_fix val_occ
281 { FixD (FixitySig (Unqual $4) (Fixity $3 $2) $1) }
283 maybe_idinfo :: { RdrName -> [HsIdInfo RdrName] }
284 maybe_idinfo : {- empty -} { \_ -> [] }
285 | src_loc PRAGMA { \x ->
286 case parseIface $2 $1 of
287 Succeeded (PIdInfo id_info) -> id_info
288 Failed err -> pprPanic "IdInfo parse failed"
292 -----------------------------------------------------------------------------
294 version :: { Version }
295 version : INTEGER { fromInteger $1 }
297 decl_context :: { RdrNameContext }
298 decl_context : { [] }
299 | '{' context_list1 '}' '=>' { $2 }
301 ----------------------------------------------------------------
303 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
305 | '=' constrs1 { $2 }
307 constrs1 :: { [RdrNameConDecl] }
308 constrs1 : constr { [$1] }
309 | constr '|' constrs1 { $1 : $3 }
311 constr :: { RdrNameConDecl }
312 constr : src_loc ex_stuff data_fs batypes { mkConDecl (ifaceUnqualVar $3) $2 (VanillaCon $4) $1 }
313 | src_loc ex_stuff data_fs '{' fields1 '}' { mkConDecl (ifaceUnqualVar $3) $2 (RecCon $5) $1 }
314 -- We use "data_fs" so as to include ()
316 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
317 newtype_constr : { [] }
318 | src_loc '=' ex_stuff data_name atype { [mkConDecl $4 $3 (NewCon $5 Nothing) $1] }
319 | src_loc '=' ex_stuff data_name '{' var_name '::' atype '}'
320 { [mkConDecl $4 $3 (NewCon $8 (Just $6)) $1] }
322 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
323 ex_stuff : { ([],[]) }
324 | '__forall' forall context '=>' { ($2,$3) }
326 batypes :: { [RdrNameBangType] }
328 | batype batypes { $1 : $2 }
330 batype :: { RdrNameBangType }
331 batype : atype { Unbanged $1 }
332 | '!' atype { Banged $2 }
334 fields1 :: { [([RdrName], RdrNameBangType)] }
335 fields1 : field { [$1] }
336 | field ',' fields1 { $1 : $3 }
338 field :: { ([RdrName], RdrNameBangType) }
339 field : var_names1 '::' type { ($1, Unbanged $3) }
340 | var_names1 '::' '!' type { ($1, Banged $4) }
341 --------------------------------------------------------------------------
343 type :: { RdrNameHsType }
344 type : '__forall' forall context '=>' type
345 { mkHsForAllTy $2 $3 $5 }
346 | btype '->' type { MonoFunTy $1 $3 }
349 forall :: { [HsTyVar RdrName] }
350 forall : '[' tv_bndrs ']' { $2 }
352 context :: { RdrNameContext }
354 | '{' context_list1 '}' { $2 }
356 context_list1 :: { RdrNameContext }
357 context_list1 : class { [$1] }
358 | class ',' context_list1 { $1 : $3 }
360 class :: { (RdrName, [RdrNameHsType]) }
361 class : qtc_name atypes { ($1, $2) }
363 types2 :: { [RdrNameHsType] {- Two or more -} }
364 types2 : type ',' type { [$1,$3] }
365 | type ',' types2 { $1 : $3 }
367 btype :: { RdrNameHsType }
369 | btype atype { MonoTyApp $1 $2 }
371 atype :: { RdrNameHsType }
372 atype : qtc_name { MonoTyVar $1 }
373 | tv_name { MonoTyVar $1 }
374 | '(' ')' { MonoTupleTy [] True }
375 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
376 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
377 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
378 | '[' type ']' { MonoListTy $2 }
379 | '{' qtc_name atypes '}' { MonoDictTy $2 $3 }
380 | '(' type ')' { $2 }
382 atypes :: { [RdrNameHsType] {- Zero or more -} }
384 | atype atypes { $1 : $2 }
385 ---------------------------------------------------------------------
387 mod_name :: { Module }
388 : CONID { mkModuleFS $1 }
390 var_fs :: { FAST_STRING }
396 data_fs :: { FAST_STRING }
399 | '->' { SLIT("->") }
400 | '(' ')' { SLIT("()") }
401 | '(' commas ')' { snd (mkTupNameStr $2) }
402 | '[' ']' { SLIT("[]") }
406 | commas ',' { $1 + 1 }
408 val_occ :: { OccName }
409 : var_fs { varOcc $1 }
410 | data_fs { varOcc $1 }
412 val_occs :: { [OccName] }
414 | val_occ val_occs { $1 : $2 }
416 entity_occ :: { OccName }
417 : var_fs { varOcc $1 }
418 | data_fs { tcOcc $1 }
420 var_name :: { RdrName }
421 var_name : var_fs { ifaceUnqualVar $1 }
423 qvar_name :: { RdrName }
424 qvar_name : var_name { $1 }
425 | QVARID { ifaceQualVar $1 }
426 | QVARSYM { ifaceQualVar $1 }
428 var_names :: { [RdrName] }
430 | var_name var_names { $1 : $2 }
432 var_names1 :: { [RdrName] }
433 var_names1 : var_name var_names { $1 : $2 }
435 data_name :: { RdrName }
436 : CONID { ifaceUnqualVar $1 }
437 | CONSYM { ifaceUnqualVar $1 }
438 | '(' commas ')' { ifaceUnqualVar (snd (mkTupNameStr $2)) }
439 | '[' ']' { ifaceUnqualVar SLIT("[]") }
441 qdata_name :: { RdrName }
442 qdata_name : data_name { $1 }
443 | QCONID { ifaceQualVar $1 }
444 | QCONSYM { ifaceQualVar $1 }
446 qdata_names :: { [RdrName] }
448 | qdata_name qdata_names { $1 : $2 }
450 tc_name :: { RdrName }
451 tc_name : CONID { ifaceUnqualTC $1 }
452 | CONSYM { ifaceUnqualTC $1 }
453 | '(' '->' ')' { ifaceUnqualTC SLIT("->") }
454 | '(' commas ')' { ifaceUnqualTC (snd (mkTupNameStr $2)) }
455 | '[' ']' { ifaceUnqualTC SLIT("[]") }
457 qtc_name :: { RdrName }
458 qtc_name : tc_name { $1 }
459 | QCONID { ifaceQualTC $1 }
460 | QCONSYM { ifaceQualTC $1 }
462 tv_name :: { RdrName }
463 tv_name : VARID { ifaceUnqualTv $1 }
464 | VARSYM { ifaceUnqualTv $1 {- Allow t2 as a tyvar -} }
466 tv_bndr :: { HsTyVar RdrName }
467 tv_bndr : tv_name '::' akind { IfaceTyVar $1 $3 }
468 | tv_name { IfaceTyVar $1 boxedTypeKind }
470 tv_bndrs :: { [HsTyVar RdrName] }
472 | tv_bndr tv_bndrs { $1 : $2 }
476 | akind '->' kind { mkArrowKind $1 $3 }
479 : VARSYM { if $1 == SLIT("*") then
481 else if $1 == SLIT("?") then
483 else panic "ParseInterface: akind"
485 | '(' kind ')' { $2 }
487 --------------------------------------------------------------------------
489 id_info :: { [HsIdInfo RdrName] }
491 | id_info_item id_info { $1 : $2 }
493 id_info_item :: { HsIdInfo RdrName }
494 id_info_item : '__A' arity_info { HsArity $2 }
495 | strict_info { HsStrictness $1 }
496 | '__U' core_expr { HsUnfold $1 (Just $2) }
497 | '__U' { HsUnfold $1 Nothing }
499 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
500 | '__C' { HsNoCafRefs }
503 spec_tvs :: { [HsTyVar RdrName] }
504 spec_tvs : '[' tv_bndrs ']' { $2 }
507 arity_info :: { ArityInfo }
508 arity_info : INTEGER { exactArity (fromInteger $1) }
510 strict_info :: { HsStrictnessInfo RdrName }
511 strict_info : '__S' qvar_name '{' qdata_names '}'
512 { HsStrictnessInfo $1 (Just ($2,$4)) }
513 | '__S' qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
514 | '__S' { HsStrictnessInfo $1 Nothing }
516 -------------------------------------------------------
517 core_expr :: { UfExpr RdrName }
518 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
519 | 'case' core_expr 'of' var_name
520 '{' core_alts '}' { UfCase $2 $4 $6 }
522 | 'let' '{' core_val_bndr '=' core_expr
523 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
524 | '__letrec' '{' rec_binds '}'
525 'in' core_expr { UfLet (UfRec $3) $6 }
527 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
528 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
530 | '__inline' core_expr { UfNote UfInlineCall $2 }
531 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
532 | '__scc' core_expr { UfNote (UfSCC $1) $2 }
535 fexpr :: { UfExpr RdrName }
536 fexpr : fexpr core_arg { UfApp $1 $2 }
539 core_arg :: { UfExpr RdrName }
540 : '__a' atype { UfType $2 }
543 core_args :: { [UfExpr RdrName] }
545 | core_arg core_args { $1 : $2 }
547 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
548 core_aexpr : qvar_name { UfVar $1 }
550 | qdata_name { UfVar $1 }
551 -- This one means that e.g. "True" will parse as
552 -- (UfVar True_Id) rather than (UfCon True_Con []).
553 -- No big deal; it'll be inlined in a jiffy. I tried
554 -- parsing it to (Con con []) directly, but got bitten
555 -- when a real constructor Id showed up in an interface
556 -- file. As usual, a hack bites you in the end.
557 -- If you want to get a UfCon, then use the
558 -- curly-bracket notation (True {}).
560 | core_lit { UfCon (UfLitCon $1) [] }
561 | '(' core_expr ')' { $2 }
562 | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
563 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
564 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
565 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
567 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
568 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
569 | core_expr ',' comma_exprs2 { $1 : $3 }
571 con_or_primop :: { UfCon RdrName }
572 con_or_primop : qdata_name { UfDataCon $1 }
573 | qvar_name { UfPrimOp $1 }
574 | '__ccall' ccall_string { let
575 (is_dyn, is_casm, may_gc) = $1
577 UfCCallOp $2 is_dyn is_casm may_gc
580 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
582 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
584 core_alts :: { [UfAlt RdrName] }
586 | core_alt ';' core_alts { $1 : $3 }
588 core_alt :: { UfAlt RdrName }
589 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
591 core_pat :: { (UfCon RdrName, [RdrName]) }
592 core_pat : core_lit { (UfLitCon $1, []) }
593 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
594 | qdata_name var_names { (UfDataCon $1, $2) }
595 | '(' comma_var_names ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
596 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
597 | '__DEFAULT' { (UfDefault, []) }
598 | '(' core_pat ')' { $2 }
601 comma_var_names :: { [RdrName] } -- Zero, or two or more
602 comma_var_names : { [] }
603 | var_name ',' comma_var_names1 { $1 : $3 }
605 comma_var_names1 :: { [RdrName] } -- One or more
606 comma_var_names1 : var_name { [$1] }
607 | var_name ',' comma_var_names1 { $1 : $3 }
609 core_lit :: { Literal }
610 core_lit : INTEGER { mkMachInt_safe $1 }
611 | CHAR { MachChar $1 }
612 | STRING { MachStr $1 }
613 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
614 | RATIONAL { MachDouble $1 }
615 | '__float' RATIONAL { MachFloat $2 }
617 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
618 -- The type checker will add the types
621 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
622 (panic "NoRepRational type")
623 -- The type checker will add the type
626 | '__addr' INTEGER { MachAddr $2 }
628 core_bndr :: { UfBinder RdrName }
629 core_bndr : core_val_bndr { $1 }
630 | core_tv_bndr { $1 }
632 core_bndrs :: { [UfBinder RdrName] }
634 | core_bndr core_bndrs { $1 : $2 }
636 core_val_bndr :: { UfBinder RdrName }
637 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
639 core_tv_bndr :: { UfBinder RdrName }
640 core_tv_bndr : '__a' tv_name '::' akind { UfTyBinder $2 $4 }
641 | '__a' tv_name { UfTyBinder $2 boxedTypeKind }
643 ccall_string :: { FAST_STRING }
648 -------------------------------------------------------------------
650 src_loc :: { SrcLoc }
651 src_loc : {% getSrcLocIf }
653 checkVersion :: { () }
654 : {-empty-} {% checkVersion Nothing }
655 | INTEGER {% checkVersion (Just (fromInteger $1)) }
657 -------------------------------------------------------------------
662 data IfaceStuff = PIface ParsedIface
663 | PIdInfo [HsIdInfo RdrName]
664 | PType RdrNameHsType
666 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc