3 -- The above warning supression flag is a temporary kludge.
4 -- While working on this module you are encouraged to remove it and fix
5 -- any warnings in the module. See
6 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
9 module ParserCore ( parseCore ) where
18 liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
19 argTypeKindTyCon, ubxTupleKindTyCon, mkArrowKind, mkTyConApp
21 import Name( Name, nameOccName, nameModule, mkExternalName )
23 import PackageConfig ( mainPackageId, stringToPackageId )
24 import ParserCoreUtils
28 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
29 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
30 import TyCon ( TyCon, tyConName )
36 #include "../HsVersions.h"
44 '%module' { TKmodule }
46 '%newtype' { TKnewtype }
47 '%forall' { TKforall }
55 '%external' { TKexternal }
66 ':=:' { TKcoloneqcolon }
76 INTEGER { TKinteger $$ }
77 RATIONAL { TKrational $$ }
78 STRING { TKstring $$ }
81 %monad { P } { thenP } { returnP }
82 %lexer { lexer } { TKEOF }
86 module :: { HsExtCore RdrName }
87 -- : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
88 : '%module' modid tdefs vdefgs { HsExtCore $2 [] [] }
91 -------------------------------------------------------------
92 -- Names: the trickiest bit in here
94 -- A name of the form A.B.C could be:
96 -- dcon C in module A.B
97 -- tcon C in module A.B
99 : NAME ':' mparts { undefined }
101 q_dc_name :: { Name }
102 : NAME ':' mparts { undefined }
104 q_tc_name :: { Name }
105 : NAME ':' mparts { undefined }
107 q_var_occ :: { Name }
108 : NAME ':' vparts { undefined }
110 mparts :: { [String] }
112 | CNAME '.' mparts { $1:$3 }
114 vparts :: { [String] }
116 | CNAME '.' vparts { $1:$3 }
118 -------------------------------------------------------------
119 -- Type and newtype declarations are in HsSyn syntax
121 tdefs :: { [TyClDecl RdrName] }
125 tdef :: { TyClDecl RdrName }
126 : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
127 { mkTyData DataType ( noLoc []
128 , noLoc (ifaceExtRdrName $2)
131 ) Nothing $6 Nothing }
132 | '%newtype' q_tc_name tv_bndrs trep ';'
133 { let tc_rdr = ifaceExtRdrName $2 in
134 mkTyData NewType ( noLoc []
138 ) Nothing ($4 (rdrNameOcc tc_rdr)) Nothing }
140 -- For a newtype we have to invent a fake data constructor name
141 -- It doesn't matter what it is, because it won't be used
142 trep :: { OccName -> [LConDecl RdrName] }
143 : {- empty -} { (\ tc_occ -> []) }
144 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
145 con_info = PrefixCon [toHsType $2] }
146 in [noLoc $ ConDecl (noLoc dc_name) Explicit []
147 (noLoc []) con_info ResTyH98 Nothing]) }
149 cons :: { [LConDecl RdrName] }
150 : {- empty -} { [] } -- 20060420 Empty data types allowed. jds
152 | con ';' cons { $1:$3 }
154 con :: { LConDecl RdrName }
155 : d_pat_occ attv_bndrs hs_atys
156 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit $2 (noLoc []) (PrefixCon $3) ResTyH98 Nothing }
158 -- XXX - audreyt - $3 needs to be split into argument and return types!
159 -- also not sure whether the [] below (quantified vars) appears.
160 -- also the "PrefixCon []" is wrong.
161 -- also we want to munge $3 somehow.
162 -- extractWhatEver to unpack ty into the parts to ConDecl
163 -- XXX - define it somewhere in RdrHsSyn
164 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit [] (noLoc []) (PrefixCon []) (undefined $3) Nothing }
166 attv_bndrs :: { [LHsTyVarBndr RdrName] }
168 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
170 hs_atys :: { [LHsType RdrName] }
171 : atys { map toHsType $1 }
174 ---------------------------------------
176 ---------------------------------------
178 atys :: { [IfaceType] }
183 : fs_var_occ { IfaceTyVar $1 }
184 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
188 : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
189 | q_var_occ atys { undefined }
190 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
195 | bty '->' ty { IfaceFunTy $1 $3 }
196 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
198 ----------------------------------------------
199 -- Bindings are in Iface syntax
201 vdefgs :: { [IfaceBinding] }
203 | let_bind ';' vdefgs { $1 : $3 }
205 let_bind :: { IfaceBinding }
206 : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
207 | vdef { let (b,r) = $1
210 vdefs1 :: { [(IfaceLetBndr, IfaceExpr)] }
212 | vdef ';' vdefs1 { $1:$3 }
214 vdef :: { (IfaceLetBndr, IfaceExpr) }
215 : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
216 | '%local' vdef { $2 }
218 -- NB: qd_occ includes data constructors, because
219 -- we allow data-constructor wrappers at top level
220 -- But we discard the module name, because it must be the
221 -- same as the module being compiled, and Iface syntax only
222 -- has OccNames in binding positions. Ah, but it has Names now!
224 ---------------------------------------
226 bndr :: { IfaceBndr }
227 : '@' tv_bndr { IfaceTvBndr $2 }
228 | id_bndr { IfaceIdBndr $1 }
230 bndrs :: { [IfaceBndr] }
232 | bndr bndrs { $1:$2 }
234 id_bndr :: { IfaceIdBndr }
235 : '(' fs_var_occ '::' ty ')' { ($2,$4) }
237 tv_bndr :: { IfaceTvBndr }
238 : fs_var_occ { ($1, ifaceLiftedTypeKind) }
239 | '(' fs_var_occ '::' akind ')' { ($2, $4) }
241 tv_bndrs :: { [IfaceTvBndr] }
243 | tv_bndr tv_bndrs { $1:$2 }
245 akind :: { IfaceKind }
246 : '*' { ifaceLiftedTypeKind }
247 | '#' { ifaceUnliftedTypeKind }
248 | '?' { ifaceOpenTypeKind }
249 | '(' kind ')' { $2 }
251 kind :: { IfaceKind }
253 | akind '->' kind { ifaceArrow $1 $3 }
254 | ty ':=:' ty { ifaceEq $1 $3 }
256 -----------------------------------------
259 aexp :: { IfaceExpr }
260 : fs_var_occ { IfaceLcl $1 }
261 | q_var_occ { IfaceExt $1 }
262 | q_dc_name { IfaceExt $1 }
263 | lit { IfaceLit $1 }
266 fexp :: { IfaceExpr }
267 : fexp aexp { IfaceApp $1 $2 }
268 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
273 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
274 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
276 | '%case' '(' ty ')' aexp '%of' id_bndr
277 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
278 | '%cast' aexp aty { IfaceCast $2 $3 }
281 --"SCC" -> IfaceNote (IfaceSCC "scc") $3
282 "InlineMe" -> IfaceNote IfaceInlineMe $3
284 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
285 (CCallSpec (StaticTarget (mkFastString $2))
286 CCallConv (PlaySafe False)))
289 alts1 :: { [IfaceAlt] }
291 | alt ';' alts1 { $1:$3 }
294 : q_dc_name bndrs '->' exp
295 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) }
296 -- The external syntax currently includes the types of the
297 -- the args, but they aren't needed internally
298 -- Nor is the module qualifier
300 { (IfaceDataAlt $1, [], $3) }
302 { (IfaceLitAlt $1, [], $3) }
304 { (IfaceDefault, [], $3) }
307 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
308 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
309 | '(' CHAR '::' aty ')' { MachChar $2 }
310 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
312 fs_var_occ :: { FastString }
313 : NAME { mkFastString $1 }
315 var_occ :: { String }
319 -- Data constructor in a pattern or data type declaration; use the dataName,
320 -- because that's what we expect in Core case patterns
321 d_pat_occ :: { OccName }
322 : CNAME { mkOccName dataName $1 }
326 ifaceKind kc = IfaceTyConApp kc []
328 ifaceBndrName (IfaceIdBndr (n,_)) = n
329 ifaceBndrName (IfaceTvBndr (n,_)) = n
331 convIntLit :: Integer -> IfaceType -> Literal
332 convIntLit i (IfaceTyConApp tc [])
333 | tc `eqTc` intPrimTyCon = MachInt i
334 | tc `eqTc` wordPrimTyCon = MachWord i
335 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
336 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
338 = pprPanic "Unknown integer literal type" (ppr aty)
340 convRatLit :: Rational -> IfaceType -> Literal
341 convRatLit r (IfaceTyConApp tc [])
342 | tc `eqTc` floatPrimTyCon = MachFloat r
343 | tc `eqTc` doublePrimTyCon = MachDouble r
345 = pprPanic "Unknown rational literal type" (ppr aty)
347 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
348 eqTc (IfaceTc name) tycon = name == tyConName tycon
350 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
351 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
352 -- and convert to HsTypes here. But the IfaceTypes we can see here
353 -- are very limited (see the productions for 'ty', so the translation
355 toHsType :: IfaceType -> LHsType RdrName
356 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOcc v))
357 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
358 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
359 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
360 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
362 -- We also need to convert IfaceKinds to Kinds (now that they are different).
363 -- Only a limited form of kind will be encountered... hopefully
364 toKind :: IfaceKind -> Kind
365 toKind (IfaceFunTy ifK1 ifK2) = mkArrowKind (toKind ifK1) (toKind ifK2)
366 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
367 toKind other = pprPanic "toKind" (ppr other)
369 toKindTc :: IfaceTyCon -> TyCon
370 toKindTc IfaceLiftedTypeKindTc = liftedTypeKindTyCon
371 toKindTc IfaceOpenTypeKindTc = openTypeKindTyCon
372 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
373 toKindTc IfaceUbxTupleKindTc = ubxTupleKindTyCon
374 toKindTc IfaceArgTypeKindTc = argTypeKindTyCon
375 toKindTc other = pprPanic "toKindTc" (ppr other)
377 ifaceTcType ifTc = IfaceTyConApp ifTc []
379 ifaceLiftedTypeKind = ifaceTcType IfaceLiftedTypeKindTc
380 ifaceOpenTypeKind = ifaceTcType IfaceOpenTypeKindTc
381 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
383 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
385 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
387 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
388 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOcc tv)) (toKind k)
390 ifaceExtRdrName :: Name -> RdrName
391 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
392 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
394 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
395 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
397 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
400 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l