Add bang patterns
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsTypes.lhs
index 9325d27..76076ff 100644 (file)
@@ -1,4 +1,4 @@
-]%
+%
 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
 \section[HsTypes]{Abstract syntax: user-defined types}
@@ -10,18 +10,18 @@ module HsTypes (
        HsExplicitForAll(..),
        HsContext, LHsContext,
        HsPred(..), LHsPred,
+
+       LBangType, BangType, HsBang(..), 
+        getBangType, getBangStrictness, 
        
        mkExplicitHsForAllTy, mkImplicitHsForAllTy, 
        hsTyVarName, hsTyVarNames, replaceTyVarName,
        hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
-       splitHsInstDeclTy,
+       splitHsInstDeclTy, splitHsFunType,
        
        -- Type place holder
        PostTcType, placeHolderType,
 
-       -- Name place holder
-       SyntaxName, placeHolderName,
-
        -- Printing
        pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context, pprHsTyVarBndr
     ) where
@@ -33,12 +33,9 @@ import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
 import Type            ( Type )
 import Kind            ( {- instance Outputable Kind -}, Kind,
                          pprParendKind, pprKind, isLiftedTypeKind )
-import Name            ( Name, mkInternalName )
-import OccName         ( mkVarOcc )
 import BasicTypes      ( IPName, Boxity, tupleParens )
-import PrelNames       ( unboundKey )
-import SrcLoc          ( noSrcLoc, Located(..), unLoc, noSrcSpan )
-import CmdLineOpts     ( opt_PprStyle_Debug )
+import SrcLoc          ( Located(..), unLoc, noSrcSpan )
+import StaticFlags     ( opt_PprStyle_Debug )
 import Outputable
 \end{code}
 
@@ -57,18 +54,35 @@ type PostTcType = Type              -- Used for slots in the abstract syntax
 
 placeHolderType :: PostTcType  -- Used before typechecking
 placeHolderType  = panic "Evaluated the place holder for a PostTcType"
+\end{code}
 
+%************************************************************************
+%*                                                                     *
+\subsection{Bang annotations}
+%*                                                                     *
+%************************************************************************
 
-type SyntaxName = Name         -- These names are filled in by the renamer
-                               -- Before then they are a placeHolderName (so that
-                               --      we can still print the HsSyn)
-                               -- They correspond to "rebindable syntax";
-                               -- See RnEnv.lookupSyntaxName
-
-placeHolderName :: SyntaxName
-placeHolderName = mkInternalName unboundKey 
-                       (mkVarOcc FSLIT("syntaxPlaceHolder")) 
-                       noSrcLoc
+\begin{code}
+type LBangType name = Located (BangType name)
+type BangType name  = HsType name      -- Bangs are in the HsType data type
+
+data HsBang = HsNoBang -- Only used as a return value for getBangStrictness,
+                       -- never appears on a HsBangTy
+           | HsStrict  -- ! 
+           | HsUnbox   -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
+
+instance Outputable HsBang where
+    ppr (HsNoBang) = empty
+    ppr (HsStrict) = char '!'
+    ppr (HsUnbox)  = ptext SLIT("!!")
+
+getBangType :: LHsType a -> LHsType a
+getBangType (L _ (HsBangTy _ ty)) = ty
+getBangType ty                    = ty
+
+getBangStrictness :: LHsType a -> HsBang
+getBangStrictness (L _ (HsBangTy s _)) = s
+getBangStrictness _                    = HsNoBang
 \end{code}
 
 
@@ -103,6 +117,8 @@ data HsType name
 
   | HsTyVar            name            -- Type variable or type constructor
 
+  | HsBangTy   HsBang (LHsType name)   -- Bang-style type annotations 
+
   | HsAppTy            (LHsType name)
                        (LHsType name)
 
@@ -128,10 +144,12 @@ data HsType name
 
   | HsNumTy             Integer                -- Generics only
 
-  | HsPredTy           (LHsPred name)  -- Only used in the type of an instance
+  | HsPredTy           (HsPred name)   -- Only used in the type of an instance
                                        -- declaration, eg.  Eq [a] -> Eq a
                                        --                             ^^^^
                                        --                            HsPredTy
+                                       -- Note no need for location info on the
+                                       -- enclosed HsPred; the one on the type will do
 
   | HsKindSig          (LHsType name)  -- (ty :: kind)
                        Kind            -- A type with a kind signature
@@ -172,7 +190,7 @@ type LHsTyVarBndr name = Located (HsTyVarBndr name)
 data HsTyVarBndr name
   = UserTyVar name
   | KindedTyVar name Kind
-       -- *** NOTA BENE *** A "monotype" in a pragma can have
+       --  *** NOTA BENE *** A "monotype" in a pragma can have
        -- for-alls in it, (mostly to do with dictionaries).  These
        -- must be explicitly Kinded.
 
@@ -208,36 +226,25 @@ splitHsInstDeclTy
     -> ([LHsTyVarBndr name], HsContext name, name, [LHsType name])
        -- Split up an instance decl type, returning the pieces
 
--- In interface files, the instance declaration head is created
--- by HsTypes.toHsType, which does not guarantee to produce a
--- HsForAllTy.  For example, if we had the weird decl
---     instance Foo T => Foo [T]
--- then we'd get the instance type
---     Foo T -> Foo [T]
--- So when colleting the instance context, to be on the safe side
--- we gather predicate arguments
--- 
--- For source code, the parser ensures the type will have the right shape.
--- (e.g. see ParseUtil.checkInstType)
-
 splitHsInstDeclTy inst_ty
   = case inst_ty of
-       HsForAllTy _ tvs cxt1 tau       -- The type vars should have been
-                                       -- computed by now, even if they were implicit
-             -> (tvs, unLoc cxt1 ++ cxt2, cls, tys)
-             where
-                (cxt2, cls, tys) = split_tau (unLoc tau)
-
-       other -> ([],  cxt2,  cls, tys)
-             where
-                (cxt2, cls, tys) = split_tau inst_ty
-
+       HsParTy (L _ ty)              -> splitHsInstDeclTy ty
+       HsForAllTy _ tvs cxt (L _ ty) -> split_tau tvs (unLoc cxt) ty
+       other                         -> split_tau []  []          other
+    -- The type vars should have been computed by now, even if they were implicit
+  where
+    split_tau tvs cxt (HsPredTy (HsClassP cls tys)) = (tvs, cxt, cls, tys)
+    split_tau tvs cxt (HsParTy (L _ ty))           = split_tau tvs cxt ty
+
+-- Splits HsType into the (init, last) parts
+-- Breaks up any parens in the result type: 
+--     splitHsFunType (a -> (b -> c)) = ([a,b], c)
+splitHsFunType :: LHsType name -> ([LHsType name], LHsType name)
+splitHsFunType (L l (HsFunTy x y)) = (x:args, res)
   where
-    split_tau (HsFunTy (L _ (HsPredTy p)) ty) = (p:ps, cls, tys)
-                                       where
-                                         (ps, cls, tys) = split_tau (unLoc ty)
-    split_tau (HsPredTy (L _ (HsClassP cls tys))) = ([], cls, tys)
-    split_tau other = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
+  (args, res) = splitHsFunType y
+splitHsFunType (L _ (HsParTy ty))  = splitHsFunType ty
+splitHsFunType other              = ([], other)
 \end{code}
 
 
@@ -318,6 +325,8 @@ ppr_mono_ty ctxt_prec (HsForAllTy exp tvs ctxt ty)
   = maybeParen ctxt_prec pREC_FUN $
     sep [pprHsForAll exp tvs ctxt, ppr_mono_lty pREC_TOP ty]
 
+-- gaw 2004
+ppr_mono_ty ctxt_prec (HsBangTy b ty)     = ppr b <> ppr ty
 ppr_mono_ty ctxt_prec (HsTyVar name)      = ppr name
 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)   = ppr_fun_ty ctxt_prec ty1 ty2
 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = tupleParens con (interpp'SP tys)