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 RnMonad ( ImportVersion, LocalVersion, ParsedIface(..), WhatsImported(..),
21 RdrNamePragma, ExportItem, RdrAvailInfo, GenAvailInfo(..)
23 import Bag ( emptyBag, unitBag, snocBag )
24 import FiniteMap ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
25 import Name ( OccName(..), isTCOcc, Provenance, Module,
26 mkTupNameStr, mkUbxTupNameStr
28 import SrcLoc ( SrcLoc )
36 %tokentype { IfaceToken }
37 %monad { IfM }{ thenIf }{ returnIf }
38 %lexer { lexIface } { ITeof }
41 'case' { ITcase } -- Haskell keywords
44 'default' { ITdefault }
45 'deriving' { ITderiving }
54 'instance' { ITinstance }
57 'newtype' { ITnewtype }
63 'qualified' { ITqualified }
66 '__interface' { ITinterface } -- GHC-extension keywords
67 '__export' { ITexport }
68 '__instimport' { ITinstimport }
69 '__forall' { ITforall }
70 '__letrec' { ITletrec }
71 '__coerce' { ITcoerce }
72 '__inline' { ITinline }
73 '__DEFAULT' { ITdefaultbranch }
75 '__integer' { ITinteger_lit }
76 '__float' { ITfloat_lit }
77 '__rational' { ITrational_lit }
78 '__addr' { ITaddr_lit }
79 '__litlit' { ITlit_lit }
80 '__string' { ITstring_lit }
81 '__ccall' { ITccall $$ }
86 '__P' { ITspecialise }
91 '..' { ITdotdot } -- reserved symbols
104 '/\\' { ITbiglam } -- GHC-extension symbols
106 '{' { ITocurly } -- special symbols
117 VARID { ITvarid $$ } -- identifiers
119 VARSYM { ITvarsym $$ }
120 CONSYM { ITconsym $$ }
121 QVARID { ITqvarid $$ }
122 QCONID { ITqconid $$ }
123 QVARSYM { ITqvarsym $$ }
124 QCONSYM { ITqconsym $$ }
126 PRAGMA { ITpragma $$ }
129 STRING { ITstring $$ }
130 INTEGER { ITinteger $$ }
131 RATIONAL { ITrational $$ }
133 UNKNOWN { ITunknown $$ }
136 -- iface_stuff is the main production.
137 -- It recognises (a) a whole interface file
138 -- (b) a type (so that type sigs can be parsed lazily)
139 -- (c) the IdInfo part of a signature (same reason)
141 iface_stuff :: { IfaceStuff }
142 iface_stuff : iface { PIface $1 }
144 | id_info { PIdInfo $1 }
147 iface :: { ParsedIface }
148 iface : '__interface' CONID INTEGER checkVersion 'where'
157 (fromInteger $3) -- Module version
158 (reverse $6) -- Usages
159 (reverse $8) -- Exports
160 (reverse $7) -- Instance import modules
161 (reverse $9) -- Fixities
162 (reverse $11) -- Decls
163 (reverse $10) -- Local instances
166 --------------------------------------------------------------------------
168 import_part :: { [ImportVersion OccName] }
170 | import_part import_decl { $2 : $1 }
172 import_decl :: { ImportVersion OccName }
173 import_decl : 'import' mod_name opt_bang INTEGER '::' whats_imported ';'
174 { ($2, $3, fromInteger $4, $6) }
176 whats_imported :: { WhatsImported OccName }
177 whats_imported : { Everything }
178 | name_version_pair name_version_pairs { Specifically ($1:$2) }
180 name_version_pairs :: { [LocalVersion OccName] }
181 name_version_pairs : { [] }
182 | name_version_pair name_version_pairs { $1 : $2 }
184 name_version_pair :: { LocalVersion OccName }
185 name_version_pair : entity_occ INTEGER { ($1, fromInteger $2) }
187 instance_import_part :: { [Module] }
188 instance_import_part : { [] }
189 | instance_import_part '__instimport' mod_name ';'
192 --------------------------------------------------------------------------
194 exports_part :: { [ExportItem] }
195 exports_part : { [] }
196 | exports_part '__export' opt_bang mod_name entities ';'
199 opt_bang :: { IfaceFlavour }
200 opt_bang : { HiFile }
203 entities :: { [RdrAvailInfo] }
205 | entity entities { $1 : $2 }
207 entity :: { RdrAvailInfo }
208 entity : entity_occ { if isTCOcc $1
211 | entity_occ stuff_inside { AvailTC $1 ($1:$2) }
212 | entity_occ '|' stuff_inside { AvailTC $1 $3 }
214 stuff_inside :: { [OccName] }
215 stuff_inside : '{' val_occs '}' { $2 }
217 --------------------------------------------------------------------------
219 fixities_part :: { [(OccName,Fixity)] }
220 fixities_part : { [] }
221 | fixities_part fixity_decl ';' { $2 : $1 }
223 fixity_decl :: { (OccName,Fixity) }
224 fixity_decl : 'infixl' mb_fix val_occ { ($3, Fixity $2 InfixL) }
225 | 'infixr' mb_fix val_occ { ($3, Fixity $2 InfixR) }
226 | 'infix' mb_fix val_occ { ($3, Fixity $2 InfixN) }
229 mb_fix : {-nothing-} { 9 }
230 | INTEGER { (fromInteger $1) }
232 -----------------------------------------------------------------------------
234 csigs :: { [RdrNameSig] }
236 | 'where' '{' csigs1 '}' { $3 }
238 csigs1 :: { [RdrNameSig] }
239 csigs1 : csig { [$1] }
240 | csig ';' csigs1 { $1 : $3 }
242 csig :: { RdrNameSig }
243 csig : src_loc var_name '::' type { ClassOpSig $2 Nothing $4 $1 }
244 | src_loc var_name '=' '::' type
246 (Just (error "Un-filled-in default method"))
249 --------------------------------------------------------------------------
251 instance_decl_part :: { [RdrNameInstDecl] }
252 instance_decl_part : {- empty -} { [] }
253 | instance_decl_part inst_decl { $2 : $1 }
255 inst_decl :: { RdrNameInstDecl }
256 inst_decl : src_loc 'instance' type '=' var_name ';'
258 EmptyMonoBinds {- No bindings -}
259 [] {- No user pragmas -}
260 (Just $5) {- Dfun id -}
264 --------------------------------------------------------------------------
266 decls_part :: { [(Version, RdrNameHsDecl)] }
269 | decls_part version decl ';' { ($2,$3):$1 }
271 decl :: { RdrNameHsDecl }
272 decl : src_loc var_name '::' type maybe_idinfo
273 { SigD (IfaceSig $2 $4 ($5 $2) $1) }
274 | src_loc 'type' tc_name tv_bndrs '=' type
275 { TyD (TySynonym $3 $4 $6 $1) }
276 | src_loc 'data' decl_context data_fs tv_bndrs constrs
277 { TyD (TyData DataType $3 (Unqual (TCOcc $4)) $5 $6 Nothing noDataPragmas $1) }
278 | src_loc 'newtype' decl_context tc_name tv_bndrs newtype_constr
279 { TyD (TyData NewType $3 $4 $5 $6 Nothing noDataPragmas $1) }
280 | src_loc 'class' decl_context tc_name tv_bndrs csigs
281 { ClD (mkClassDecl $3 $4 $5 $6 EmptyMonoBinds
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 other -> 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 (Unqual (VarOcc $3)) $2 (VanillaCon $4) $1 }
313 | src_loc ex_stuff data_fs '{' fields1 '}' { mkConDecl (Unqual (VarOcc $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) $1] }
320 ex_stuff :: { ([HsTyVar RdrName], RdrNameContext) }
321 ex_stuff : { ([],[]) }
322 | '__forall' forall context '=>' { ($2,$3) }
324 batypes :: { [RdrNameBangType] }
326 | batype batypes { $1 : $2 }
328 batype :: { RdrNameBangType }
329 batype : atype { Unbanged $1 }
330 | '!' atype { Banged $2 }
332 fields1 :: { [([RdrName], RdrNameBangType)] }
333 fields1 : field { [$1] }
334 | field ',' fields1 { $1 : $3 }
336 field :: { ([RdrName], RdrNameBangType) }
337 field : var_names1 '::' type { ($1, Unbanged $3) }
338 | var_names1 '::' '!' type { ($1, Banged $4) }
339 --------------------------------------------------------------------------
341 type :: { RdrNameHsType }
342 type : '__forall' forall context '=>' type
343 { mkHsForAllTy $2 $3 $5 }
344 | btype '->' type { MonoFunTy $1 $3 }
347 forall :: { [HsTyVar RdrName] }
348 forall : '[' tv_bndrs ']' { $2 }
350 context :: { RdrNameContext }
352 | '{' context_list1 '}' { $2 }
354 context_list1 :: { RdrNameContext }
355 context_list1 : class { [$1] }
356 | class ',' context_list1 { $1 : $3 }
358 class :: { (RdrName, [RdrNameHsType]) }
359 class : qtc_name atypes { ($1, $2) }
361 types2 :: { [RdrNameHsType] {- Two or more -} }
362 types2 : type ',' type { [$1,$3] }
363 | type ',' types2 { $1 : $3 }
365 btype :: { RdrNameHsType }
367 | btype atype { MonoTyApp $1 $2 }
369 atype :: { RdrNameHsType }
370 atype : qtc_name { MonoTyVar $1 }
371 | tv_name { MonoTyVar $1 }
372 | '(' ')' { MonoTupleTy [] True }
373 | '(' types2 ')' { MonoTupleTy $2 True{-boxed-} }
374 | '(#' type '#)' { MonoTupleTy [$2] False{-unboxed-} }
375 | '(#' types2 '#)' { MonoTupleTy $2 False{-unboxed-} }
376 | '[' type ']' { MonoListTy $2 }
377 | '{' qtc_name atypes '}' { MonoDictTy $2 $3 }
378 | '(' type ')' { $2 }
380 atypes :: { [RdrNameHsType] {- Zero or more -} }
382 | atype atypes { $1 : $2 }
383 ---------------------------------------------------------------------
385 mod_name :: { Module }
388 var_fs :: { FAST_STRING }
394 data_fs :: { FAST_STRING }
397 | '->' { SLIT("->") }
398 | '(' ')' { SLIT("()") }
399 | '(' commas ')' { snd (mkTupNameStr $2) }
400 | '[' ']' { SLIT("[]") }
404 | commas ',' { $1 + 1 }
406 val_occ :: { OccName }
407 : var_fs { VarOcc $1 }
408 | data_fs { VarOcc $1 }
410 val_occs :: { [OccName] }
412 | val_occ val_occs { $1 : $2 }
414 entity_occ :: { OccName }
415 : var_fs { VarOcc $1 }
416 | data_fs { TCOcc $1 }
418 var_name :: { RdrName }
419 var_name : var_fs { Unqual (VarOcc $1) }
421 qvar_name :: { RdrName }
422 qvar_name : var_name { $1 }
423 | QVARID { lexVarQual $1 }
424 | QVARSYM { lexVarQual $1 }
426 var_names :: { [RdrName] }
428 | var_name var_names { $1 : $2 }
430 var_names1 :: { [RdrName] }
431 var_names1 : var_name var_names { $1 : $2 }
433 data_name :: { RdrName }
434 : CONID { Unqual (VarOcc $1) }
435 | CONSYM { Unqual (VarOcc $1) }
436 | '(' commas ')' { Unqual (VarOcc (snd (mkTupNameStr $2))) }
437 | '[' ']' { Unqual (VarOcc SLIT("[]")) }
439 qdata_name :: { RdrName }
440 qdata_name : data_name { $1 }
441 | QCONID { lexVarQual $1 }
442 | QCONSYM { lexVarQual $1 }
444 qdata_names :: { [RdrName] }
446 | qdata_name qdata_names { $1 : $2 }
448 tc_name :: { RdrName }
449 tc_name : CONID { Unqual (TCOcc $1) }
450 | CONSYM { Unqual (TCOcc $1) }
451 | '(' '->' ')' { Unqual (TCOcc SLIT("->")) }
452 | '(' commas ')' { Unqual (TCOcc (snd (mkTupNameStr $2))) }
453 | '[' ']' { Unqual (TCOcc SLIT("[]")) }
455 qtc_name :: { RdrName }
456 qtc_name : tc_name { $1 }
457 | QCONID { lexTcQual $1 }
458 | QCONSYM { lexTcQual $1 }
460 tv_name :: { RdrName }
461 tv_name : VARID { Unqual (TvOcc $1) }
462 | VARSYM { Unqual (TvOcc $1) {- Allow t2 as a tyvar -} }
464 tv_bndr :: { HsTyVar RdrName }
465 tv_bndr : tv_name '::' akind { IfaceTyVar $1 $3 }
466 | tv_name { UserTyVar $1 }
468 tv_bndrs :: { [HsTyVar RdrName] }
470 | tv_bndr tv_bndrs { $1 : $2 }
474 | akind '->' kind { mkArrowKind $1 $3 }
477 : VARSYM { if $1 == SLIT("*") then
479 else if $1 == SLIT("**") then
481 else panic "ParseInterface: akind"
483 | '(' kind ')' { $2 }
485 --------------------------------------------------------------------------
487 id_info :: { [HsIdInfo RdrName] }
489 | id_info_item id_info { $1 : $2 }
491 id_info_item :: { HsIdInfo RdrName }
492 id_info_item : '__A' arity_info { HsArity $2 }
493 | strict_info { HsStrictness $1 }
494 | '__bot' { HsStrictness HsBottom }
495 | '__U' core_expr { HsUnfold $1 (Just $2) }
496 | '__U' { HsUnfold $1 Nothing }
498 atypes '=' core_expr { HsSpecialise $2 $3 $5 }
499 | '__C' { HsNoCafRefs }
502 spec_tvs :: { [HsTyVar RdrName] }
503 spec_tvs : '[' tv_bndrs ']' { $2 }
506 arity_info :: { ArityInfo }
507 arity_info : INTEGER { exactArity (fromInteger $1) }
509 strict_info :: { HsStrictnessInfo RdrName }
510 strict_info : '__S' qvar_name '{' qdata_names '}'
511 { HsStrictnessInfo $1 (Just ($2,$4)) }
512 | '__S' qvar_name { HsStrictnessInfo $1 (Just ($2,[])) }
513 | '__S' { HsStrictnessInfo $1 Nothing }
515 -------------------------------------------------------
516 core_expr :: { UfExpr RdrName }
517 core_expr : '\\' core_bndrs '->' core_expr { foldr UfLam $4 $2 }
518 | 'case' core_expr 'of' var_name
519 '{' core_alts '}' { UfCase $2 $4 $6 }
521 | 'let' '{' core_val_bndr '=' core_expr
522 '}' 'in' core_expr { UfLet (UfNonRec $3 $5) $8 }
523 | '__letrec' '{' rec_binds '}'
524 'in' core_expr { UfLet (UfRec $3) $6 }
526 | con_or_primop '{' core_args '}' { UfCon $1 $3 }
527 | '__litlit' STRING atype { UfCon (UfLitLitCon $2 $3) [] }
529 | '__inline' core_expr { UfNote UfInlineCall $2 }
530 | '__coerce' atype core_expr { UfNote (UfCoerce $2) $3 }
531 | '__scc' core_expr { UfNote (UfSCC $1) $2 }
534 fexpr :: { UfExpr RdrName }
535 fexpr : fexpr core_arg { UfApp $1 $2 }
538 core_arg :: { UfExpr RdrName }
539 : '__a' atype { UfType $2 }
542 core_args :: { [UfExpr RdrName] }
544 | core_arg core_args { $1 : $2 }
546 core_aexpr :: { UfExpr RdrName } -- Atomic expressions
547 core_aexpr : qvar_name { UfVar $1 }
549 | qdata_name { UfVar $1 }
550 -- This one means that e.g. "True" will parse as
551 -- (UfVar True_Id) rather than (UfCon True_Con []).
552 -- No big deal; it'll be inlined in a jiffy. I tried
553 -- parsing it to (Con con []) directly, but got bitten
554 -- when a real constructor Id showed up in an interface
555 -- file. As usual, a hack bites you in the end.
556 -- If you want to get a UfCon, then use the
557 -- curly-bracket notation (True {}).
559 | core_lit { UfCon (UfLitCon $1) [] }
560 | '(' core_expr ')' { $2 }
561 | '(' ')' { UfTuple (mkTupConRdrName 0) [] }
562 | '(' comma_exprs2 ')' { UfTuple (mkTupConRdrName (length $2)) $2 }
563 | '(#' core_expr '#)' { UfTuple (mkUbxTupConRdrName 1) [$2] }
564 | '(#' comma_exprs2 '#)' { UfTuple (mkUbxTupConRdrName (length $2)) $2 }
566 comma_exprs2 :: { [UfExpr RdrName] } -- Two or more
567 comma_exprs2 : core_expr ',' core_expr { [$1,$3] }
568 | core_expr ',' comma_exprs2 { $1 : $3 }
570 con_or_primop :: { UfCon RdrName }
571 con_or_primop : qdata_name { UfDataCon $1 }
572 | qvar_name { UfPrimOp $1 }
573 | '__ccall' ccall_string { let
574 (is_casm, may_gc) = $1
576 UfCCallOp $2 is_casm may_gc
579 rec_binds :: { [(UfBinder RdrName, UfExpr RdrName)] }
581 | core_val_bndr '=' core_expr ';' rec_binds { ($1,$3) : $5 }
583 core_alts :: { [UfAlt RdrName] }
585 | core_alt ';' core_alts { $1 : $3 }
587 core_alt :: { UfAlt RdrName }
588 core_alt : core_pat '->' core_expr { (fst $1, snd $1, $3) }
590 core_pat :: { (UfCon RdrName, [RdrName]) }
591 core_pat : core_lit { (UfLitCon $1, []) }
592 | '__litlit' STRING atype { (UfLitLitCon $2 $3, []) }
593 | qdata_name var_names { (UfDataCon $1, $2) }
594 | '(' comma_var_names ')' { (UfDataCon (mkTupConRdrName (length $2)), $2) }
595 | '(#' comma_var_names1 '#)' { (UfDataCon (mkUbxTupConRdrName (length $2)), $2) }
596 | '__DEFAULT' { (UfDefault, []) }
597 | '(' core_pat ')' { $2 }
600 comma_var_names :: { [RdrName] } -- Zero, or two or more
601 comma_var_names : { [] }
602 | var_name ',' comma_var_names1 { $1 : $3 }
604 comma_var_names1 :: { [RdrName] } -- One or more
605 comma_var_names1 : var_name { [$1] }
606 | var_name ',' comma_var_names1 { $1 : $3 }
608 core_lit :: { Literal }
609 core_lit : INTEGER { mkMachInt_safe $1 }
610 | CHAR { MachChar $1 }
611 | STRING { MachStr $1 }
612 | '__string' STRING { NoRepStr $2 (panic "NoRepStr type") }
613 | RATIONAL { MachDouble $1 }
614 | '__float' RATIONAL { MachFloat $2 }
616 | '__integer' INTEGER { NoRepInteger $2 (panic "NoRepInteger type")
617 -- The type checker will add the types
620 | '__rational' INTEGER INTEGER { NoRepRational ($2 % $3)
621 (panic "NoRepRational type")
622 -- The type checker will add the type
625 | '__addr' INTEGER { MachAddr $2 }
627 core_bndr :: { UfBinder RdrName }
628 core_bndr : core_val_bndr { $1 }
629 | core_tv_bndr { $1 }
631 core_bndrs :: { [UfBinder RdrName] }
633 | core_bndr core_bndrs { $1 : $2 }
635 core_val_bndr :: { UfBinder RdrName }
636 core_val_bndr : var_name '::' atype { UfValBinder $1 $3 }
638 core_tv_bndr :: { UfBinder RdrName }
639 core_tv_bndr : '__a' tv_name '::' akind { UfTyBinder $2 $4 }
640 | '__a' tv_name { UfTyBinder $2 boxedTypeKind }
642 ccall_string :: { FAST_STRING }
647 -------------------------------------------------------------------
649 src_loc :: { SrcLoc }
650 src_loc : {% getSrcLocIf }
652 checkVersion :: { () }
653 : {-empty-} {% checkVersion Nothing }
654 | INTEGER {% checkVersion (Just (fromInteger $1)) }
656 -------------------------------------------------------------------
661 data IfaceStuff = PIface ParsedIface
662 | PIdInfo [HsIdInfo RdrName]
663 | PType RdrNameHsType
665 mkConDecl name (ex_tvs, ex_ctxt) details loc = ConDecl name ex_tvs ex_ctxt details loc