\begin{code}
module HsDecls (
HsDecl(..), LHsDecl, TyClDecl(..), LTyClDecl,
- InstDecl(..), LInstDecl,
+ 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(..), LConDecl,
DeprecDecl(..), LDeprecDecl,
+ HsGroup(..), emptyGroup, appendGroups,
tcdName, tyClDeclNames, tyClDeclTyVars,
isClassDecl, isSynDecl, isDataDecl,
countTyClDecls,
import {-# SOURCE #-} HsExpr( HsExpr, pprExpr )
-- Because Expr imports Decls via HsBracket
-import HsBinds ( HsBindGroup, HsBind, LHsBinds,
- Sig(..), LSig, LFixitySig )
+import HsBinds ( HsBindGroup(..), HsBind, LHsBinds,
+ Sig(..), LSig, LFixitySig, pprLHsBinds )
import HsPat ( HsConDetails(..), hsConArgs )
import HsImpExp ( pprHsVar )
import HsTypes
import HscTypes ( DeprecTxt )
import CoreSyn ( RuleName )
-import BasicTypes ( NewOrData(..), Activation(..) )
+import Kind ( Kind, pprKind )
+import BasicTypes ( Activation(..), RecFlag(..) )
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 Bag ( emptyBag )
import SrcLoc ( Located(..), unLoc )
import FastString
\end{code}
hs_depds :: [LDeprecDecl id],
hs_ruleds :: [LRuleDecl id]
}
+
+emptyGroup = HsGroup { hs_valds = [],
+ hs_tyclds = [], hs_instds = [],
+ hs_fixds = [], hs_defds = [], hs_fords = [],
+ hs_depds = [] ,hs_ruleds = [] }
+
+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 ++ 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}
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 are all ConDecls
+ -- For data T a where { T1 :: T a } the LConDecls are all GadtDecls
+
+ 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
tcdSigs :: [LSig name], -- Methods' signatures
tcdMeths :: LHsBinds name -- Default methods
}
+
+data NewOrData
+ = NewType -- "newtype Blah ..."
+ | DataType -- "data Blah ..."
+ deriving( Eq ) -- Needed because Demand derives Eq
\end{code}
Simple classifiers
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})
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 _ (GadtDecl _ _) : _) -- 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
+ ppr NewType = ptext SLIT("newtype")
+ ppr DataType = ptext SLIT("data")
\end{code}
[LHsTyVarBndr name] -- Existentially quantified type variables
(LHsContext name) -- ...and context
-- If both are empty then there are no existentials
-
(HsConDetails name (LBangType name))
+
+ | GadtDecl (Located name) -- Constructor name; this is used for the
+ -- DataCon itself, and for the user-callable wrapper Id
+ (LHsType name) -- Constructor type; it may have HsBangs on the
+ -- argument types
\end{code}
\begin{code}
do_one (flds_seen, acc) (ConDecl lname _ _ _)
= (flds_seen, lname:acc)
+-- gaw 2004
+ do_one (flds_seen, acc) (GadtDecl lname _)
+ = (flds_seen, lname: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 -- !! (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 (GadtDecl con ty)
+ = ppr con <+> dcolon <+> ppr ty
ppr_con_details con (InfixCon ty1 ty2)
- = hsep [ppr ty1, ppr con, ppr ty2]
+ = hsep [ppr ty1, pprHsVar con, ppr ty2]
-- ConDecls generated by MkIface.ifaceTyThing always have a PrefixCon, even
-- if the constructor is an infix one. This is because in an interface file
where
ppr_field (n, ty) = ppr n <+> dcolon <+> ppr 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}
-
%************************************************************************
%* *
\subsection[InstDecl]{An instance declaration
ppr (InstDecl inst_ty binds uprags)
= vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
nest 4 (ppr uprags),
- nest 4 (ppr binds) ]
+ nest 4 (pprLHsBinds binds) ]
\end{code}
%************************************************************************
-- 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)
--
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