2 #include "HsVersions.h"
3 module ParseIface ( parseIface ) where
7 import HsSyn -- quite a bit of stuff
8 import RdrHsSyn -- oodles of synonyms
9 import HsDecls ( HsIdInfo(..), HsStrictnessInfo )
10 import HsTypes ( mkHsForAllTy )
13 import BasicTypes ( IfaceFlavour(..), Fixity(..), FixityDirection(..), NewOrData(..), Version(..) )
14 import HsPragmas ( noDataPragmas, noClassPragmas )
15 import Kind ( Kind, mkArrowKind, mkBoxedTypeKind )
16 import IdInfo ( ArgUsageInfo, FBTypeInfo )
19 import RnMonad ( SYN_IE(ImportVersion), SYN_IE(LocalVersion), ParsedIface(..),
20 SYN_IE(RdrNamePragma), SYN_IE(ExportItem), SYN_IE(RdrAvailInfo), GenAvailInfo(..)
22 import Bag ( emptyBag, unitBag, snocBag )
23 import FiniteMap ( emptyFM, unitFM, addToFM, plusFM, bagToFM, FiniteMap )
24 import Name ( OccName(..), isTCOcc, Provenance, SYN_IE(Module) )
25 import SrcLoc ( mkIfaceSrcLoc )
26 --import Util ( panic{-, pprPanic ToDo:rm-} )
27 import ParseType ( parseType )
28 import ParseUnfolding ( parseUnfolding )
31 -----------------------------------------------------------------
33 parseIface ls = parseIToks (lexIface ls)
35 -----------------------------------------------------------------
39 %tokentype { IfaceToken }
40 %monad { IfM }{ thenIf }{ returnIf }
43 INTERFACE { ITinterface }
44 USAGES_PART { ITusages }
45 VERSIONS_PART { ITversions }
46 EXPORTS_PART { ITexports }
47 INSTANCE_MODULES_PART { ITinstance_modules }
48 INSTANCES_PART { ITinstances }
49 FIXITIES_PART { ITfixities }
50 DECLARATIONS_PART { ITdeclarations }
51 PRAGMAS_PART { ITpragmas }
55 DERIVING { ITderiving }
58 INSTANCE { ITinstance }
81 VARSYM { ITvarsym $$ }
82 CONSYM { ITconsym $$ }
83 QVARID { ITqvarid $$ }
84 QCONID { ITqconid $$ }
85 QVARSYM { ITqvarsym $$ }
86 QCONSYM { ITqconsym $$ }
88 IDINFO_PART { ITidinfo $$ }
89 TYPE_PART { ITtysig $$ }
90 ARITY_PART { ITarity }
91 STRICT_PART { ITstrict }
92 UNFOLD_PART { ITunfold $$ }
93 DEMAND { ITdemand $$ }
98 PRIM_CASE { ITprim_case }
103 COERCE_IN { ITcoerce_in }
104 COERCE_OUT { ITcoerce_out }
110 STRING { ITstring $$ }
111 INTEGER { ITinteger $$ }
112 DOUBLE { ITdouble $$ }
114 INTEGER_LIT { ITinteger_lit }
115 FLOAT_LIT { ITfloat_lit }
116 RATIONAL_LIT { ITrational_lit }
117 ADDR_LIT { ITaddr_lit }
118 LIT_LIT { ITlit_lit }
119 STRING_LIT { ITstring_lit }
121 UNKNOWN { ITunknown $$ }
124 iface :: { ParsedIface }
125 iface : INTERFACE CONID INTEGER
128 exports_part fixities_part
133 (fromInteger $3) -- Module version
136 $4 -- Instance modules
139 $8 -- Local instances
143 usages_part :: { [ImportVersion OccName] }
144 usages_part : USAGES_PART module_stuff_pairs { $2 }
147 module_stuff_pairs :: { [ImportVersion OccName] }
148 module_stuff_pairs : { [] }
149 | module_stuff_pair module_stuff_pairs { $1 : $2 }
151 module_stuff_pair :: { ImportVersion OccName }
152 module_stuff_pair : mod_name opt_bang INTEGER DCOLON name_version_pairs SEMI
153 { ($1, $2, fromInteger $3, $5) }
155 versions_part :: { [LocalVersion OccName] }
156 versions_part : VERSIONS_PART name_version_pairs { $2 }
159 name_version_pairs :: { [LocalVersion OccName] }
160 name_version_pairs : { [] }
161 | name_version_pair name_version_pairs { $1 : $2 }
163 name_version_pair :: { LocalVersion OccName }
164 name_version_pair : entity_occ INTEGER { ($1, fromInteger $2)
165 --------------------------------------------------------------------------
168 exports_part :: { [ExportItem] }
169 exports_part : EXPORTS_PART export_items { $2 }
172 export_items :: { [ExportItem] }
173 export_items : { [] }
174 | opt_bang mod_name entities SEMI export_items { ($2,$1,$3) : $5 }
176 opt_bang :: { IfaceFlavour }
177 opt_bang : { HiFile }
178 | BANG { HiBootFile }
180 entities :: { [RdrAvailInfo] }
182 | entity entities { $1 : $2 }
184 entity :: { RdrAvailInfo }
185 entity : entity_occ { if isTCOcc $1
188 | entity_occ stuff_inside { AvailTC $1 ($1:$2) }
189 | entity_occ VBAR stuff_inside { AvailTC $1 $3 }
191 stuff_inside :: { [OccName] }
192 stuff_inside : OPAREN val_occs1 CPAREN { $2
193 --------------------------------------------------------------------------
196 inst_modules_part :: { [Module] }
197 inst_modules_part : { [] }
198 | INSTANCE_MODULES_PART mod_list { $2 }
200 mod_list :: { [Module] }
202 | mod_name mod_list { $1 : $2
203 --------------------------------------------------------------------------
206 fixities_part :: { [(OccName,Fixity)] }
207 fixities_part : { [] }
208 | FIXITIES_PART fixes { $2 }
210 fixes :: { [(OccName,Fixity)] }
212 | fix fixes { $1 : $2 }
214 fix :: { (OccName, Fixity) }
215 fix : INFIXL INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixL) }
216 | INFIXR INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixR) }
217 | INFIX INTEGER val_occ SEMI { ($3, Fixity (fromInteger $2) InfixN)
218 --------------------------------------------------------------------------
221 decls_part :: { [(Version, RdrNameHsDecl)] }
223 | DECLARATIONS_PART topdecls { $2 }
225 topdecls :: { [(Version, RdrNameHsDecl)] }
227 | version topdecl topdecls { ($1,$2) : $3 }
229 version :: { Version }
230 version : INTEGER { fromInteger $1 }
232 topdecl :: { RdrNameHsDecl }
233 topdecl : TYPE tc_name tv_bndrs EQUAL type SEMI
234 { TyD (TySynonym $2 $3 $5 mkIfaceSrcLoc) }
235 | DATA decl_context tc_name tv_bndrs constrs deriving SEMI
236 { TyD (TyData DataType $2 $3 $4 $5 $6 noDataPragmas mkIfaceSrcLoc) }
237 | NEWTYPE decl_context tc_name tv_bndrs newtype_constr deriving SEMI
238 { TyD (TyData NewType $2 $3 $4 $5 $6 noDataPragmas mkIfaceSrcLoc) }
239 | CLASS decl_context tc_name tv_bndr csigs SEMI
240 { ClD (ClassDecl $2 $3 $4 $5 EmptyMonoBinds noClassPragmas mkIfaceSrcLoc) }
241 | var_name TYPE_PART id_info
244 (Succeeded tp) = parseType $2
246 SigD (IfaceSig $1 tp $3 mkIfaceSrcLoc) }
248 id_info :: { [HsIdInfo RdrName] }
250 | IDINFO_PART { let { (Succeeded id_info) = parseUnfolding $1 } in id_info}
252 decl_context :: { RdrNameContext }
253 decl_context : { [] }
254 | OCURLY context_list1 CCURLY DARROW { $2 }
257 csigs :: { [RdrNameSig] }
259 | WHERE OCURLY csigs1 CCURLY { $3 }
261 csigs1 :: { [RdrNameSig] }
262 csigs1 : csig { [$1] }
263 | csig SEMI csigs1 { $1 : $3 }
265 csig :: { RdrNameSig }
266 csig : var_name DCOLON type { ClassOpSig $1 Nothing $3 mkIfaceSrcLoc }
267 | var_name EQUAL DCOLON type { ClassOpSig $1 (Just (error "Un-filled-in default method"))
269 ----------------------------------------------------------------
272 constrs :: { [RdrNameConDecl] {- empty for handwritten abstract -} }
274 | EQUAL constrs1 { $2 }
276 constrs1 :: { [RdrNameConDecl] }
277 constrs1 : constr { [$1] }
278 | constr VBAR constrs1 { $1 : $3 }
280 constr :: { RdrNameConDecl }
281 constr : data_name batypes { ConDecl $1 [] (VanillaCon $2) mkIfaceSrcLoc }
282 | data_name OCURLY fields1 CCURLY { ConDecl $1 [] (RecCon $3) mkIfaceSrcLoc }
284 newtype_constr :: { [RdrNameConDecl] {- Empty if handwritten abstract -} }
285 newtype_constr : { [] }
286 | EQUAL data_name atype { [ConDecl $2 [] (NewCon $3) mkIfaceSrcLoc] }
288 deriving :: { Maybe [RdrName] }
290 | DERIVING OPAREN tc_names1 CPAREN { Just $3 }
292 batypes :: { [RdrNameBangType] }
294 | batype batypes { $1 : $2 }
296 batype :: { RdrNameBangType }
297 batype : atype { Unbanged $1 }
298 | BANG atype { Banged $2 }
300 fields1 :: { [([RdrName], RdrNameBangType)] }
301 fields1 : field { [$1] }
302 | field COMMA fields1 { $1 : $3 }
304 field :: { ([RdrName], RdrNameBangType) }
305 field : var_names1 DCOLON type { ($1, Unbanged $3) }
306 | var_names1 DCOLON BANG type { ($1, Banged $4)
307 --------------------------------------------------------------------------
310 forall :: { [HsTyVar RdrName] }
311 forall : OBRACK tv_bndrs CBRACK { $2 }
313 context :: { RdrNameContext }
315 | OCURLY context_list1 CCURLY { $2 }
317 context_list1 :: { RdrNameContext }
318 context_list1 : class { [$1] }
319 | class COMMA context_list1 { $1 : $3 }
321 class :: { (RdrName, RdrNameHsType) }
322 class : tc_name atype { ($1, $2) }
324 type :: { RdrNameHsType }
325 type : FORALL forall context DARROW type { mkHsForAllTy $2 $3 $5 }
326 | btype RARROW type { MonoFunTy $1 $3 }
329 types2 :: { [RdrNameHsType] {- Two or more -} }
330 types2 : type COMMA type { [$1,$3] }
331 | type COMMA types2 { $1 : $3 }
333 btype :: { RdrNameHsType }
335 | btype atype { MonoTyApp $1 $2 }
337 atype :: { RdrNameHsType }
338 atype : tc_name { MonoTyVar $1 }
339 | tv_name { MonoTyVar $1 }
340 | OPAREN types2 CPAREN { MonoTupleTy dummyRdrTcName $2 }
341 | OBRACK type CBRACK { MonoListTy dummyRdrTcName $2 }
342 | OCURLY tc_name atype CCURLY { MonoDictTy $2 $3 }
343 | OPAREN type CPAREN { $2 }
345 atypes :: { [RdrNameHsType] {- Zero or more -} }
347 | atype atypes { $1 : $2
348 ---------------------------------------------------------------------
351 mod_name :: { Module }
354 var_occ :: { OccName }
355 var_occ : VARID { VarOcc $1 }
356 | VARSYM { VarOcc $1 }
357 | BANG { VarOcc SLIT("!") {-sigh, double-sigh-} }
359 tc_occ :: { OccName }
360 tc_occ : CONID { TCOcc $1 }
361 | CONSYM { TCOcc $1 }
362 | OPAREN RARROW CPAREN { TCOcc SLIT("->") }
364 entity_occ :: { OccName }
365 entity_occ : var_occ { $1 }
367 | RARROW { TCOcc SLIT("->") {- Allow un-paren'd arrow -} }
369 val_occ :: { OccName }
370 val_occ : var_occ { $1 }
371 | CONID { VarOcc $1 }
372 | CONSYM { VarOcc $1 }
374 val_occs1 :: { [OccName] }
376 | val_occ val_occs1 { $1 : $2 }
379 qvar_name :: { RdrName }
380 : QVARID { lexVarQual $1 }
381 | QVARSYM { lexVarQual $1 }
383 var_name :: { RdrName }
384 var_name : var_occ { Unqual $1 }
386 var_names1 :: { [RdrName] }
387 var_names1 : var_name { [$1] }
388 | var_name var_names1 { $1 : $2 }
390 any_var_name :: {RdrName}
391 any_var_name : var_name { $1 }
394 qdata_name :: { RdrName }
395 qdata_name : QCONID { lexVarQual $1 }
396 | QCONSYM { lexVarQual $1 }
398 data_name :: { RdrName }
399 data_name : CONID { Unqual (VarOcc $1) }
400 | CONSYM { Unqual (VarOcc $1) }
403 tc_names1 :: { [RdrName] }
405 | tc_name COMMA tc_names1 { $1 : $3 }
407 tc_name :: { RdrName }
408 tc_name : tc_occ { Unqual $1 }
409 | QCONID { lexTcQual $1 }
411 tv_name :: { RdrName }
412 tv_name : VARID { Unqual (TvOcc $1) }
413 | VARSYM { Unqual (TvOcc $1) {- Allow $t2 as a tyvar -} }
415 tv_names :: { [RdrName] }
417 | tv_name tv_names { $1 : $2 }
419 tv_bndr :: { HsTyVar RdrName }
420 tv_bndr : tv_name DCOLON akind { IfaceTyVar $1 $3 }
421 | tv_name { UserTyVar $1 }
423 tv_bndrs :: { [HsTyVar RdrName] }
425 | tv_bndr tv_bndrs { $1 : $2 }
429 | akind RARROW kind { mkArrowKind $1 $3 }
432 : VARSYM { mkBoxedTypeKind {- ToDo: check that it's "*" -} }
433 | OPAREN kind CPAREN { $2
434 --------------------------------------------------------------------------
438 instances_part :: { [RdrNameInstDecl] }
439 instances_part : INSTANCES_PART instdecls { $2 }
442 instdecls :: { [RdrNameInstDecl] }
444 | instd instdecls { $1 : $2 }
446 instd :: { RdrNameInstDecl }
447 instd : INSTANCE type EQUAL var_name SEMI
449 EmptyMonoBinds {- No bindings -}
450 [] {- No user pragmas -}
451 (Just $4) {- Dfun id -}
453 --------------------------------------------------------------------------