remove empty dir
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsDecls.lhs
index 930dcdc..8ff3985 100644 (file)
@@ -11,13 +11,12 @@ module HsDecls (
        HsDecl(..), LHsDecl, TyClDecl(..), LTyClDecl, 
        InstDecl(..), LInstDecl, NewOrData(..),
        RuleDecl(..), LRuleDecl, RuleBndr(..),
-       DefaultDecl(..), LDefaultDecl, HsGroup(..), SpliceDecl(..),
+       DefaultDecl(..), LDefaultDecl, SpliceDecl(..),
        ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
        CImportSpec(..), FoType(..),
-       ConDecl(..), LConDecl,
-       LBangType, BangType(..), HsBang(..), 
-       getBangType, getBangStrictness, unbangedType, 
+       ConDecl(..), ResType(..), LConDecl,     
        DeprecDecl(..),  LDeprecDecl,
+       HsGroup(..),  emptyRdrGroup, emptyRnGroup, appendGroups,
        tcdName, tyClDeclNames, tyClDeclTyVars,
        isClassDecl, isSynDecl, isDataDecl, 
        countTyClDecls,
@@ -31,24 +30,26 @@ module HsDecls (
 import {-# SOURCE #-}  HsExpr( HsExpr, pprExpr )
        -- Because Expr imports Decls via HsBracket
 
-import HsBinds         ( HsBindGroup, HsBind, LHsBinds, 
-                         Sig(..), LSig, LFixitySig, pprLHsBinds )
+import HsBinds         ( HsValBinds(..), HsBind, LHsBinds, plusHsValBinds,
+                         Sig(..), LSig, LFixitySig, pprLHsBinds,
+                         emptyValBindsIn, emptyValBindsOut )
 import HsPat           ( HsConDetails(..), hsConArgs )
 import HsImpExp                ( pprHsVar )
 import HsTypes
+import NameSet          ( NameSet )
 import HscTypes                ( DeprecTxt )
 import CoreSyn         ( RuleName )
+import Kind            ( Kind, pprKind )
 import BasicTypes      ( Activation(..) )
 import ForeignCall     ( CCallTarget(..), DNCallSpec, CCallConv, Safety,
-                         CExportSpec(..)) 
+                         CExportSpec(..), CLabelString ) 
 
 -- others:
 import FunDeps         ( pprFundeps )
 import Class           ( FunDep )
-import CStrings                ( CLabelString )
 import Outputable      
 import Util            ( count )
-import SrcLoc          ( Located(..), unLoc )
+import SrcLoc          ( Located(..), unLoc, noLoc )
 import FastString
 \end{code}
 
@@ -90,12 +91,7 @@ data HsDecl id
 -- fed to the renamer.
 data HsGroup id
   = HsGroup {
-       hs_valds  :: [HsBindGroup id],
-               -- Before the renamer, this is a single big HsBindGroup,
-               -- with all the bindings, and all the signatures.
-               -- The renamer does dependency analysis, splitting it up
-               -- into several HsBindGroups.
-
+       hs_valds  :: HsValBinds id,
        hs_tyclds :: [LTyClDecl id],
        hs_instds :: [LInstDecl id],
 
@@ -108,6 +104,46 @@ data HsGroup id
        hs_depds  :: [LDeprecDecl id],
        hs_ruleds :: [LRuleDecl id]
   }
+
+emptyGroup, emptyRdrGroup, emptyRnGroup :: HsGroup a
+emptyRdrGroup = emptyGroup { hs_valds = emptyValBindsIn }
+emptyRnGroup  = emptyGroup { hs_valds = emptyValBindsOut }
+
+emptyGroup = HsGroup { hs_tyclds = [], hs_instds = [],
+                      hs_fixds = [], hs_defds = [], hs_fords = [], 
+                      hs_depds = [], hs_ruleds = [],
+                      hs_valds = error "emptyGroup hs_valds: Can't happen" }
+
+appendGroups :: HsGroup a -> HsGroup a -> HsGroup a
+appendGroups 
+    HsGroup { 
+       hs_valds  = val_groups1,
+       hs_tyclds = tyclds1, 
+       hs_instds = instds1,
+       hs_fixds  = fixds1, 
+       hs_defds  = defds1,
+       hs_fords  = fords1, 
+       hs_depds  = depds1,
+       hs_ruleds = rulds1 }
+    HsGroup { 
+       hs_valds  = val_groups2,
+       hs_tyclds = tyclds2, 
+       hs_instds = instds2,
+       hs_fixds  = fixds2, 
+       hs_defds  = defds2,
+       hs_fords  = fords2, 
+       hs_depds  = depds2,
+       hs_ruleds = rulds2 }
+  = 
+    HsGroup { 
+       hs_valds  = val_groups1 `plusHsValBinds` val_groups2,
+       hs_tyclds = tyclds1 ++ tyclds2, 
+       hs_instds = instds1 ++ instds2,
+       hs_fixds  = fixds1 ++ fixds2, 
+       hs_defds  = defds1 ++ defds2,
+       hs_fords  = fords1 ++ fords2, 
+       hs_depds  = depds1 ++ depds2,
+       hs_ruleds = rulds1 ++ rulds2 }
 \end{code}
 
 \begin{code}
@@ -305,10 +341,20 @@ data TyClDecl name
                tcdCtxt   :: LHsContext name,           -- Context
                tcdLName  :: Located name,              -- Type constructor
                tcdTyVars :: [LHsTyVarBndr name],       -- Type variables
+               tcdKindSig :: Maybe Kind,               -- Optional kind sig; 
+                                                       -- (only for the 'where' form)
+
                tcdCons   :: [LConDecl name],           -- Data constructors
-               tcdDerivs :: Maybe (LHsContext name)    
+                       -- For data T a = T1 | T2 a          the LConDecls all have ResTyH98
+                       -- For data T a where { T1 :: T a }  the LConDecls all have ResTyGADT
+
+               tcdDerivs :: Maybe [LHsType name]
                        -- Derivings; Nothing => not specified
                        --            Just [] => derive exactly what is asked
+                       -- These "types" must be of form
+                       --      forall ab. C ty1 ty2
+                       -- Typically the foralls and ty args are empty, but they
+                       -- are non-empty for the newtype-deriving case
     }
 
   | TySynonym {        tcdLName  :: Located name,              -- type constructor
@@ -361,7 +407,7 @@ tyClDeclNames (TySynonym   {tcdLName = name})  = [name]
 tyClDeclNames (ForeignType {tcdLName = name})  = [name]
 
 tyClDeclNames (ClassDecl {tcdLName = cls_name, tcdSigs = sigs})
-  = cls_name : [n | L _ (Sig n _) <- sigs]
+  = cls_name : [n | L _ (TypeSig n _) <- sigs]
 
 tyClDeclNames (TyData {tcdLName = tc_name, tcdCons = cons})
   = tc_name : conDeclsNames (map unLoc cons)
@@ -400,11 +446,14 @@ instance OutputableBndr name
             4 (ppr mono_ty)
 
     ppr (TyData {tcdND = new_or_data, tcdCtxt = context, tcdLName = ltycon,
-                tcdTyVars = tyvars, tcdCons = condecls, 
+                tcdTyVars = tyvars, tcdKindSig = mb_sig, tcdCons = condecls, 
                 tcdDerivs = derivings})
-      = pp_tydecl (ppr new_or_data <+> pp_decl_head (unLoc context) ltycon tyvars)
+      = pp_tydecl (ppr new_or_data <+> pp_decl_head (unLoc context) ltycon tyvars <+> ppr_sig mb_sig)
                  (pp_condecls condecls)
                  derivings
+      where
+       ppr_sig Nothing = empty
+       ppr_sig (Just kind) = dcolon <+> pprKind kind
 
     ppr (ClassDecl {tcdCtxt = context, tcdLName = lclas, tcdTyVars = tyvars, tcdFDs = fds,
                    tcdSigs = sigs, tcdMeths = methods})
@@ -425,16 +474,17 @@ pp_decl_head :: OutputableBndr name
    -> SDoc
 pp_decl_head context thing tyvars
   = hsep [pprHsContext context, ppr thing, interppSP tyvars]
-
-pp_condecls cs = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
+pp_condecls cs@(L _ ConDecl{ con_res = ResTyGADT _ } : _) -- In GADT syntax
+  = hang (ptext SLIT("where")) 2 (vcat (map ppr cs))
+pp_condecls cs                           -- In H98 syntax
+  = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
 
 pp_tydecl pp_head pp_decl_rhs derivings
   = hang pp_head 4 (sep [
        pp_decl_rhs,
        case derivings of
          Nothing          -> empty
-         Just ds          -> hsep [ptext SLIT("deriving"), 
-                                       ppr_hs_context (unLoc ds)]
+         Just ds          -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
     ])
 
 instance Outputable NewOrData where
@@ -452,15 +502,40 @@ instance Outputable NewOrData where
 \begin{code}
 type LConDecl name = Located (ConDecl name)
 
+-- data T b = forall a. Eq a => MkT a b
+--   MkT :: forall b a. Eq a => MkT a b
+
+-- data T b where
+--     MkT1 :: Int -> T Int
+
+-- data T = Int `MkT` Int
+--       | MkT2
+
+-- data T a where
+--     Int `MkT` Int :: T Int
+
 data ConDecl name
-  = ConDecl    (Located name)          -- Constructor name; this is used for the
-                                       -- DataCon itself, and for the user-callable wrapper Id
+  = ConDecl
+    { con_name      :: Located name        -- Constructor name; this is used for the
+                                            -- DataCon itself, and for the user-callable wrapper Id
+
+    , con_explicit  :: HsExplicitForAll     -- Is there an user-written forall? (cf. HStypes.HsForAllTy)
+
+    , con_qvars     :: [LHsTyVarBndr name]  -- ResTyH98: the constructor's existential type variables
+                                           -- ResTyGADT:    all the constructor's quantified type variables
+
+    , con_cxt       :: LHsContext name      -- The context.  This *does not* include the
+                                           -- "stupid theta" which lives only in the TyData decl
 
-               [LHsTyVarBndr name]     -- Existentially quantified type variables
-               (LHsContext name)       -- ...and context
-                                       -- If both are empty then there are no existentials
+    , con_details   :: HsConDetails name (LBangType name)      -- The main payload
 
-               (HsConDetails name (LBangType name))
+    , con_res       :: ResType name         -- Result type of the constructor
+    }
+
+data ResType name
+   = ResTyH98          -- Constructor was declared using Haskell 98 syntax
+   | ResTyGADT (LHsType name)  -- Constructor was declared using GADT-style syntax,
+                               --      and here is its result type
 \end{code}
 
 \begin{code}
@@ -471,63 +546,40 @@ conDeclsNames :: Eq name => [ConDecl name] -> [Located name]
 conDeclsNames cons
   = snd (foldl do_one ([], []) cons)
   where
-    do_one (flds_seen, acc) (ConDecl lname _ _ (RecCon flds))
+    do_one (flds_seen, acc) (ConDecl { con_name = lname, con_details = RecCon flds })
        = (map unLoc new_flds ++ flds_seen, lname : [f | f <- new_flds] ++ acc)
        where
          new_flds = [ f | (f,_) <- flds, not (unLoc f `elem` flds_seen) ]
 
-    do_one (flds_seen, acc) (ConDecl lname _ _ _)
-       = (flds_seen, lname:acc)
+    do_one (flds_seen, acc) c
+       = (flds_seen, (con_name c):acc)
 
 conDetailsTys details = map getBangType (hsConArgs details)
 \end{code}
   
-\begin{code}
-type LBangType name = Located (BangType name)
-
-data BangType name = BangType HsBang (LHsType name)
-
-data HsBang = HsNoBang
-           | HsStrict  -- ! 
-           | HsUnbox   -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
-
-getBangType       (BangType _ ty) = ty
-getBangStrictness (BangType s _)  = s
-
-unbangedType :: LHsType id -> LBangType id
-unbangedType ty@(L loc _) = L loc (BangType HsNoBang ty)
-\end{code}
 
 \begin{code}
 instance (OutputableBndr name) => Outputable (ConDecl name) where
-    ppr (ConDecl con tvs cxt con_details)
-      = sep [pprHsForAll Explicit tvs cxt, ppr_con_details con con_details]
-
-ppr_con_details con (InfixCon ty1 ty2)
-  = hsep [ppr ty1, ppr con, ppr ty2]
+    ppr = pprConDecl
 
--- ConDecls generated by MkIface.ifaceTyThing always have a PrefixCon, even
--- if the constructor is an infix one.  This is because in an interface file
--- we don't distinguish between the two.  Hence when printing these for the
--- user, we need to parenthesise infix constructor names.
-ppr_con_details con (PrefixCon tys)
-  = hsep (pprHsVar con : map ppr tys)
+pprConDecl (ConDecl con expl tvs cxt details ResTyH98)
+  = sep [pprHsForAll expl tvs cxt, ppr_details con details]
+  where
+    ppr_details con (InfixCon t1 t2) = hsep [ppr t1, pprHsVar con, ppr t2]
+    ppr_details con (PrefixCon tys)  = hsep (pprHsVar con : map ppr tys)
+    ppr_details con (RecCon fields)  = ppr con <+> ppr_fields fields
 
-ppr_con_details con (RecCon fields)
-  = ppr con <+> braces (sep (punctuate comma (map ppr_field fields)))
+pprConDecl (ConDecl con expl tvs cxt details (ResTyGADT res_ty))
+  = sep [pprHsForAll expl tvs cxt, ppr con <+> ppr_details details]
   where
-    ppr_field (n, ty) = ppr n <+> dcolon <+> ppr ty
+    ppr_details (PrefixCon arg_tys) = dcolon <+> ppr (foldr mk_fun_ty res_ty arg_tys)
+    ppr_details (RecCon fields)     = ppr fields <+> dcolon <+> ppr res_ty
 
-instance OutputableBndr name => Outputable (BangType name) where
-    ppr (BangType is_strict ty) 
-       = bang <> pprParendHsType (unLoc ty)
-       where
-         bang = case is_strict of
-                       HsNoBang -> empty
-                       HsStrict -> char '!'
-                       HsUnbox  -> ptext SLIT("!!")
-\end{code}
+    mk_fun_ty a b = noLoc (HsFunTy a b)
 
+ppr_fields fields = braces (sep (punctuate comma (map ppr_field fields)))
+ppr_field (n, ty) = ppr n <+> dcolon <+> ppr ty
+\end{code}
 
 %************************************************************************
 %*                                                                     *
@@ -587,7 +639,7 @@ instance (OutputableBndr name)
 -- foreign declarations are distinguished as to whether they define or use a
 -- Haskell name
 --
--- * the Boolean value indicates whether the pre-standard deprecated syntax
+--  * the Boolean value indicates whether the pre-standard deprecated syntax
 --   has been used
 --
 type LForeignDecl name = Located (ForeignDecl name)
@@ -601,17 +653,17 @@ data ForeignDecl name
 --
 data ForeignImport = -- import of a C entity
                     --
-                     -- * the two strings specifying a header file or library
+                     --  * the two strings specifying a header file or library
                      --   may be empty, which indicates the absence of a
                      --   header or object specification (both are not used
                      --   in the case of `CWrapper' and when `CFunction'
                      --   has a dynamic target)
                     --
-                    -- * the calling convention is irrelevant for code
+                    --  * the calling convention is irrelevant for code
                     --   generation in the case of `CLabel', but is needed
                     --   for pretty printing 
                     --
-                    -- * `Safety' is irrelevant for `CLabel' and `CWrapper'
+                    --  * `Safety' is irrelevant for `CLabel' and `CWrapper'
                     --
                     CImport  CCallConv       -- ccall or stdcall
                              Safety          -- safe or unsafe
@@ -670,8 +722,8 @@ instance Outputable ForeignImport where
         ptext SLIT("dynamic")
       pprCEntity _      _   (CWrapper) = ptext SLIT("wrapper")
       --
-      pprLib lib | nullFastString lib = empty
-                | otherwise          = char '[' <> ppr lib <> char ']'
+      pprLib lib | nullFS lib = empty
+                | otherwise  = char '[' <> ppr lib <> char ']'
 
 instance Outputable ForeignExport where
   ppr (CExport  (CExportStatic lbl cconv)) = 
@@ -699,7 +751,9 @@ data RuleDecl name
        Activation
        [RuleBndr name]         -- Forall'd vars; after typechecking this includes tyvars
        (Located (HsExpr name)) -- LHS
+        NameSet                 -- Free-vars from the LHS
        (Located (HsExpr name)) -- RHS
+        NameSet                 -- Free-vars from the RHS
 
 data RuleBndr name
   = RuleBndr (Located name)
@@ -709,7 +763,7 @@ collectRuleBndrSigTys :: [RuleBndr name] -> [LHsType name]
 collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ ty <- bndrs]
 
 instance OutputableBndr name => Outputable (RuleDecl name) where
-  ppr (HsRule name act ns lhs rhs)
+  ppr (HsRule name act ns lhs fv_lhs rhs fv_rhs)
        = sep [text "{-# RULES" <+> doubleQuotes (ftext name) <+> ppr act,
               nest 4 (pp_forall <+> pprExpr (unLoc lhs)), 
               nest 4 (equals <+> pprExpr (unLoc rhs) <+> text "#-}") ]