2 {-# LANGUAGE BangPatterns #-} -- required for versions of Happy before 1.18.6
3 {-# OPTIONS -Wwarn -w #-}
4 -- The above warning supression flag is a temporary kludge.
5 -- While working on this module you are encouraged to remove it and fix
6 -- any warnings in the module. See
7 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
10 module ParserCore ( parseCore ) where
19 liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
20 argTypeKindTyCon, ubxTupleKindTyCon, mkTyConApp
22 import Coercion( mkArrowKind )
23 import Name( Name, nameOccName, nameModule, mkExternalName )
25 import ParserCoreUtils
29 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
30 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
31 import TyCon ( TyCon, tyConName )
37 #include "../HsVersions.h"
46 '%module' { TKmodule }
48 '%newtype' { TKnewtype }
49 '%forall' { TKforall }
57 '%external' { TKexternal }
68 ':=:' { TKcoloneqcolon }
78 INTEGER { TKinteger $$ }
79 RATIONAL { TKrational $$ }
80 STRING { TKstring $$ }
83 %monad { P } { thenP } { returnP }
84 %lexer { lexer } { TKEOF }
88 module :: { HsExtCore RdrName }
89 -- : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
90 : '%module' modid tdefs vdefgs { HsExtCore $2 [] [] }
93 -------------------------------------------------------------
94 -- Names: the trickiest bit in here
96 -- A name of the form A.B.C could be:
98 -- dcon C in module A.B
99 -- tcon C in module A.B
101 : NAME ':' mparts { undefined }
103 q_dc_name :: { Name }
104 : NAME ':' mparts { undefined }
106 q_tc_name :: { Name }
107 : NAME ':' mparts { undefined }
109 q_var_occ :: { Name }
110 : NAME ':' vparts { undefined }
112 mparts :: { [String] }
114 | CNAME '.' mparts { $1:$3 }
116 vparts :: { [String] }
118 | CNAME '.' vparts { $1:$3 }
120 -------------------------------------------------------------
121 -- Type and newtype declarations are in HsSyn syntax
123 tdefs :: { [TyClDecl RdrName] }
127 tdef :: { TyClDecl RdrName }
128 : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
129 { TyData { tcdND = DataType, tcdCtxt = noLoc []
130 , tcdLName = noLoc (ifaceExtRdrName $2)
131 , tcdTyVars = map toHsTvBndr $3
132 , tcdTyPats = Nothing, tcdKindSig = Nothing
133 , tcdCons = $6, tcdDerivs = Nothing } }
134 | '%newtype' q_tc_name tv_bndrs trep ';'
135 { let tc_rdr = ifaceExtRdrName $2 in
136 TyData { tcdND = NewType, tcdCtxt = noLoc []
137 , tcdLName = noLoc tc_rdr
138 , tcdTyVars = map toHsTvBndr $3
139 , tcdTyPats = Nothing, tcdKindSig = Nothing
140 , tcdCons = $4 (rdrNameOcc tc_rdr), tcdDerivs = Nothing } }
142 -- For a newtype we have to invent a fake data constructor name
143 -- It doesn't matter what it is, because it won't be used
144 trep :: { OccName -> [LConDecl RdrName] }
145 : {- empty -} { (\ tc_occ -> []) }
146 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
147 con_info = PrefixCon [toHsType $2] }
148 in [noLoc $ mkSimpleConDecl (noLoc dc_name) []
149 (noLoc []) con_info]) }
151 cons :: { [LConDecl RdrName] }
152 : {- empty -} { [] } -- 20060420 Empty data types allowed. jds
154 | con ';' cons { $1:$3 }
156 con :: { LConDecl RdrName }
157 : d_pat_occ attv_bndrs hs_atys
158 { noLoc $ mkSimpleConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3) }
159 -- ToDo: parse record-style declarations
161 attv_bndrs :: { [LHsTyVarBndr RdrName] }
163 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
165 hs_atys :: { [LHsType RdrName] }
166 : atys { map toHsType $1 }
169 ---------------------------------------
171 ---------------------------------------
173 atys :: { [IfaceType] }
178 : fs_var_occ { IfaceTyVar $1 }
179 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
183 : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
184 | q_var_occ atys { undefined }
185 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
190 | bty '->' ty { IfaceFunTy $1 $3 }
191 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
193 ----------------------------------------------
194 -- Bindings are in Iface syntax
196 vdefgs :: { [IfaceBinding] }
198 | let_bind ';' vdefgs { $1 : $3 }
200 let_bind :: { IfaceBinding }
201 : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
202 | vdef { let (b,r) = $1
205 vdefs1 :: { [(IfaceLetBndr, IfaceExpr)] }
207 | vdef ';' vdefs1 { $1:$3 }
209 vdef :: { (IfaceLetBndr, IfaceExpr) }
210 : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
211 | '%local' vdef { $2 }
213 -- NB: qd_occ includes data constructors, because
214 -- we allow data-constructor wrappers at top level
215 -- But we discard the module name, because it must be the
216 -- same as the module being compiled, and Iface syntax only
217 -- has OccNames in binding positions. Ah, but it has Names now!
219 ---------------------------------------
221 bndr :: { IfaceBndr }
222 : '@' tv_bndr { IfaceTvBndr $2 }
223 | id_bndr { IfaceIdBndr $1 }
225 bndrs :: { [IfaceBndr] }
227 | bndr bndrs { $1:$2 }
229 id_bndr :: { IfaceIdBndr }
230 : '(' fs_var_occ '::' ty ')' { ($2,$4) }
232 tv_bndr :: { IfaceTvBndr }
233 : fs_var_occ { ($1, ifaceLiftedTypeKind) }
234 | '(' fs_var_occ '::' akind ')' { ($2, $4) }
236 tv_bndrs :: { [IfaceTvBndr] }
238 | tv_bndr tv_bndrs { $1:$2 }
240 akind :: { IfaceKind }
241 : '*' { ifaceLiftedTypeKind }
242 | '#' { ifaceUnliftedTypeKind }
243 | '?' { ifaceOpenTypeKind }
244 | '(' kind ')' { $2 }
246 kind :: { IfaceKind }
248 | akind '->' kind { ifaceArrow $1 $3 }
249 | ty ':=:' ty { ifaceEq $1 $3 }
251 -----------------------------------------
254 aexp :: { IfaceExpr }
255 : fs_var_occ { IfaceLcl $1 }
256 | q_var_occ { IfaceExt $1 }
257 | q_dc_name { IfaceExt $1 }
258 | lit { IfaceLit $1 }
261 fexp :: { IfaceExpr }
262 : fexp aexp { IfaceApp $1 $2 }
263 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
268 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
269 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
271 | '%case' '(' ty ')' aexp '%of' id_bndr
272 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
273 | '%cast' aexp aty { IfaceCast $2 $3 }
274 -- No InlineMe any more
275 -- | '%note' STRING exp
277 -- --"SCC" -> IfaceNote (IfaceSCC "scc") $3
278 -- "InlineMe" -> IfaceNote IfaceInlineMe $3
280 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
281 (CCallSpec (StaticTarget (mkFastString $2) Nothing)
282 CCallConv (PlaySafe False)))
285 alts1 :: { [IfaceAlt] }
287 | alt ';' alts1 { $1:$3 }
290 : q_dc_name bndrs '->' exp
291 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) }
292 -- The external syntax currently includes the types of the
293 -- the args, but they aren't needed internally
294 -- Nor is the module qualifier
296 { (IfaceDataAlt $1, [], $3) }
298 { (IfaceLitAlt $1, [], $3) }
300 { (IfaceDefault, [], $3) }
303 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
304 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
305 | '(' CHAR '::' aty ')' { MachChar $2 }
306 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
308 fs_var_occ :: { FastString }
309 : NAME { mkFastString $1 }
311 var_occ :: { String }
315 -- Data constructor in a pattern or data type declaration; use the dataName,
316 -- because that's what we expect in Core case patterns
317 d_pat_occ :: { OccName }
318 : CNAME { mkOccName dataName $1 }
322 ifaceKind kc = IfaceTyConApp kc []
324 ifaceBndrName (IfaceIdBndr (n,_)) = n
325 ifaceBndrName (IfaceTvBndr (n,_)) = n
327 convIntLit :: Integer -> IfaceType -> Literal
328 convIntLit i (IfaceTyConApp tc [])
329 | tc `eqTc` intPrimTyCon = MachInt i
330 | tc `eqTc` wordPrimTyCon = MachWord i
331 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
332 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
334 = pprPanic "Unknown integer literal type" (ppr aty)
336 convRatLit :: Rational -> IfaceType -> Literal
337 convRatLit r (IfaceTyConApp tc [])
338 | tc `eqTc` floatPrimTyCon = MachFloat r
339 | tc `eqTc` doublePrimTyCon = MachDouble r
341 = pprPanic "Unknown rational literal type" (ppr aty)
343 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
344 eqTc (IfaceTc name) tycon = name == tyConName tycon
346 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
347 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
348 -- and convert to HsTypes here. But the IfaceTypes we can see here
349 -- are very limited (see the productions for 'ty', so the translation
351 toHsType :: IfaceType -> LHsType RdrName
352 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOccFS v))
353 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
354 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
355 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
356 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
358 -- We also need to convert IfaceKinds to Kinds (now that they are different).
359 -- Only a limited form of kind will be encountered... hopefully
360 toKind :: IfaceKind -> Kind
361 toKind (IfaceFunTy ifK1 ifK2) = mkArrowKind (toKind ifK1) (toKind ifK2)
362 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
363 toKind other = pprPanic "toKind" (ppr other)
365 toKindTc :: IfaceTyCon -> TyCon
366 toKindTc IfaceLiftedTypeKindTc = liftedTypeKindTyCon
367 toKindTc IfaceOpenTypeKindTc = openTypeKindTyCon
368 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
369 toKindTc IfaceUbxTupleKindTc = ubxTupleKindTyCon
370 toKindTc IfaceArgTypeKindTc = argTypeKindTyCon
371 toKindTc other = pprPanic "toKindTc" (ppr other)
373 ifaceTcType ifTc = IfaceTyConApp ifTc []
375 ifaceLiftedTypeKind = ifaceTcType IfaceLiftedTypeKindTc
376 ifaceOpenTypeKind = ifaceTcType IfaceOpenTypeKindTc
377 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
379 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
381 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
383 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
384 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOccFS tv)) (toKind k)
386 ifaceExtRdrName :: Name -> RdrName
387 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
388 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
390 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
391 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
393 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
396 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l