module FreeC.IR.Syntax.TypeDecl where
import FreeC.IR.SrcSpan
import FreeC.IR.Syntax.Name
import FreeC.IR.Syntax.Type
import FreeC.IR.Syntax.TypeVarDecl
import FreeC.Pretty
data TypeDecl
= DataDecl { typeDeclSrcSpan :: SrcSpan
, typeDeclIdent :: DeclIdent
, typeDeclArgs :: [TypeVarDecl]
, dataDeclCons :: [ConDecl]
}
| TypeSynDecl { typeDeclSrcSpan :: SrcSpan
, typeDeclIdent :: DeclIdent
, typeDeclArgs :: [TypeVarDecl]
, typeSynDeclRhs :: Type
}
deriving ( Eq, Show )
instance HasDeclIdent TypeDecl where
declIdent = typeDeclIdent
typeDeclQName :: TypeDecl -> QName
typeDeclQName = declIdentName . typeDeclIdent
typeDeclName :: TypeDecl -> Name
typeDeclName = nameFromQName . typeDeclQName
instance Pretty TypeDecl where
pretty (DataDecl _ declIdent' typeVarDecls conDecls) = prettyString "data"
<+> pretty declIdent'
<+> hsep (map pretty typeVarDecls)
<+> align (vcat (zipWith prettyConDecl [0 ..] conDecls))
where
prettyConDecl :: Int -> ConDecl -> Doc
prettyConDecl i conDecl | i == 0 = equals <+> pretty conDecl
| otherwise = char '|' <+> pretty conDecl
pretty (TypeSynDecl _ declIdent' typeVarDecls typeExpr) = prettyString "type"
<+> pretty declIdent'
<+> hsep (map pretty typeVarDecls)
<+> equals
<+> pretty typeExpr
data ConDecl = ConDecl { conDeclSrcSpan :: SrcSpan
, conDeclIdent :: DeclIdent
, conDeclFields :: [Type]
}
deriving ( Eq, Show )
instance HasDeclIdent ConDecl where
declIdent = conDeclIdent
conDeclQName :: ConDecl -> QName
conDeclQName = declIdentName . conDeclIdent
conDeclName :: ConDecl -> Name
conDeclName = nameFromQName . conDeclQName
instance Pretty ConDecl where
pretty (ConDecl _ declIdent' types) = pretty declIdent'
<+> hsep (map pretty types)