2 module ParserCore ( parseCore ) where
10 import Kind( Kind(..) )
11 import Name( nameOccName, nameModuleName )
13 import ParserCoreUtils
18 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
19 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
20 import TyCon ( TyCon, tyConName )
25 #include "../HsVersions.h"
33 '%module' { TKmodule }
35 '%newtype' { TKnewtype }
36 '%forall' { TKforall }
42 '%coerce' { TKcoerce }
44 '%external' { TKexternal }
62 INTEGER { TKinteger $$ }
63 RATIONAL { TKrational $$ }
64 STRING { TKstring $$ }
67 %monad { P } { thenP } { returnP }
68 %lexer { lexer } { TKEOF }
72 module :: { HsExtCore RdrName }
73 : '%module' modid tdefs vdefgs
74 { HsExtCore (mkHomeModule $2) $3 $4 }
76 modid :: { ModuleName }
77 : CNAME { mkSysModuleNameFS (mkFastString $1) }
79 -------------------------------------------------------------
80 -- Type and newtype declarations are in HsSyn syntax
82 tdefs :: { [TyClDecl RdrName] }
84 | tdef ';' tdefs {$1:$3}
86 tdef :: { TyClDecl RdrName }
87 : '%data' q_tc_name tv_bndrs '=' '{' cons1 '}'
88 { mkTyData DataType (noLoc [], noLoc (ifaceExtRdrName $2), map toHsTvBndr $3) $6 Nothing }
89 | '%newtype' q_tc_name tv_bndrs trep
90 { let tc_rdr = ifaceExtRdrName $2 in
91 mkTyData NewType (noLoc [], noLoc tc_rdr, map toHsTvBndr $3) ($4 (rdrNameOcc tc_rdr)) Nothing }
93 -- For a newtype we have to invent a fake data constructor name
94 -- It doesn't matter what it is, because it won't be used
95 trep :: { OccName -> [LConDecl RdrName] }
96 : {- empty -} { (\ tc_occ -> []) }
97 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
98 con_info = PrefixCon [unbangedType (toHsType $2)] }
99 in [noLoc $ ConDecl (noLoc dc_name) []
100 (noLoc []) con_info]) }
102 cons1 :: { [LConDecl RdrName] }
104 | con ';' cons1 { $1:$3 }
106 con :: { LConDecl RdrName }
107 : d_pat_occ attv_bndrs hs_atys
108 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon (map unbangedType $3))}
110 attv_bndrs :: { [LHsTyVarBndr RdrName] }
112 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
114 hs_atys :: { [LHsType RdrName] }
115 : atys { map toHsType $1 }
118 ---------------------------------------
120 ---------------------------------------
122 atys :: { [IfaceType] }
127 : tv_occ { IfaceTyVar $1 }
128 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
132 : tv_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
133 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
137 | bty '->' ty { IfaceFunTy $1 $3 }
138 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
140 ----------------------------------------------
141 -- Bindings are in Iface syntax
143 vdefgs :: { [IfaceBinding] }
145 | let_bind ';' vdefgs { $1 : $3 }
147 let_bind :: { IfaceBinding }
148 : '%rec' '{' vdefs1 '}' { IfaceRec $3 }
149 | vdef { let (b,r) = $1
152 vdefs1 :: { [(IfaceIdBndr, IfaceExpr)] }
154 | vdef ';' vdefs1 { $1:$3 }
156 vdef :: { (IfaceIdBndr, IfaceExpr) }
157 : qd_occ '::' ty '=' exp { (($1, $3), $5) }
158 -- NB: qd_occ includes data constructors, because
159 -- we allow data-constructor wrappers at top level
160 -- But we discard the module name, because it must be the
161 -- same as the module being compiled, and Iface syntax only
162 -- has OccNames in binding positions
164 qd_occ :: { OccName }
168 ---------------------------------------
170 bndr :: { IfaceBndr }
171 : '@' tv_bndr { IfaceTvBndr $2 }
172 | id_bndr { IfaceIdBndr $1 }
174 bndrs :: { [IfaceBndr] }
176 | bndr bndrs { $1:$2 }
178 id_bndr :: { IfaceIdBndr }
179 : '(' var_occ '::' ty ')' { ($2,$4) }
181 id_bndrs :: { [IfaceIdBndr] }
183 | id_bndr id_bndrs { $1:$2 }
185 tv_bndr :: { IfaceTvBndr }
186 : tv_occ { ($1, LiftedTypeKind) }
187 | '(' tv_occ '::' akind ')' { ($2, $4) }
189 tv_bndrs :: { [IfaceTvBndr] }
191 | tv_bndr tv_bndrs { $1:$2 }
193 akind :: { IfaceKind }
194 : '*' { LiftedTypeKind }
195 | '#' { UnliftedTypeKind }
196 | '?' { OpenTypeKind }
197 | '(' kind ')' { $2 }
199 kind :: { IfaceKind }
201 | akind '->' kind { FunKind $1 $3 }
203 -----------------------------------------
206 aexp :: { IfaceExpr }
207 : var_occ { IfaceLcl $1 }
208 | modid '.' qd_occ { IfaceExt (ExtPkg $1 $3) }
209 | lit { IfaceLit $1 }
212 fexp :: { IfaceExpr }
213 : fexp aexp { IfaceApp $1 $2 }
214 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
219 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
220 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
221 | '%case' aexp '%of' id_bndr
222 '{' alts1 '}' { IfaceCase $2 (fst $4) $6 }
223 | '%coerce' aty exp { IfaceNote (IfaceCoerce $2) $3 }
226 --"SCC" -> IfaceNote (IfaceSCC "scc") $3
227 "InlineCall" -> IfaceNote IfaceInlineCall $3
228 "InlineMe" -> IfaceNote IfaceInlineMe $3
230 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
231 (CCallSpec (StaticTarget (mkFastString $2))
232 CCallConv (PlaySafe False)))
235 alts1 :: { [IfaceAlt] }
237 | alt ';' alts1 { $1:$3 }
240 : modid '.' d_pat_occ bndrs '->' exp
241 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) }
242 -- The external syntax currently includes the types of the
243 -- the args, but they aren't needed internally
244 -- Nor is the module qualifier
246 { (IfaceLitAlt $1, [], $3) }
248 { (IfaceDefault, [], $3) }
251 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
252 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
253 | '(' CHAR '::' aty ')' { MachChar $2 }
254 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
256 tv_occ :: { OccName }
257 : NAME { mkSysOcc tvName $1 }
259 var_occ :: { OccName }
260 : NAME { mkSysOcc varName $1 }
264 q_tc_name :: { IfaceExtName }
265 : modid '.' CNAME { ExtPkg $1 (mkSysOcc tcName $3) }
267 -- Data constructor in a pattern or data type declaration; use the dataName,
268 -- because that's what we expect in Core case patterns
269 d_pat_occ :: { OccName }
270 : CNAME { mkSysOcc dataName $1 }
272 -- Data constructor occurrence in an expression;
273 -- use the varName because that's the worker Id
275 : CNAME { mkSysOcc varName $1 }
279 ifaceBndrName (IfaceIdBndr (n,_)) = n
280 ifaceBndrName (IfaceTvBndr (n,_)) = n
282 convIntLit :: Integer -> IfaceType -> Literal
283 convIntLit i (IfaceTyConApp tc [])
284 | tc `eqTc` intPrimTyCon = MachInt i
285 | tc `eqTc` wordPrimTyCon = MachWord i
286 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
287 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
289 = pprPanic "Unknown integer literal type" (ppr aty)
291 convRatLit :: Rational -> IfaceType -> Literal
292 convRatLit r (IfaceTyConApp tc [])
293 | tc `eqTc` floatPrimTyCon = MachFloat r
294 | tc `eqTc` doublePrimTyCon = MachDouble r
296 = pprPanic "Unknown rational literal type" (ppr aty)
298 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
299 eqTc (IfaceTc (ExtPkg mod occ)) tycon
300 = mod == nameModuleName nm && occ == nameOccName nm
304 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
305 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
306 -- and convert to HsTypes here. But the IfaceTypes we can see here
307 -- are very limited (see the productions for 'ty', so the translation
309 toHsType :: IfaceType -> LHsType RdrName
310 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual v)
311 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
312 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
313 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
314 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
316 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
317 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
319 ifaceExtRdrName :: IfaceExtName -> RdrName
320 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
321 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
323 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
324 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
326 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
329 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l