Description.hs 4.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
{- sv2v
 - Author: Zachary Snow <zach@zachjs.com>
 - Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
 -
 - SystemVerilog top-level items (descriptions, package items)
 -}

module Language.SystemVerilog.AST.Description
    ( Description (..)
    , PackageItem (..)
    , PartKW      (..)
    , Lifetime    (..)
13 14
    , Qualifier   (..)
    , ClassItem
15 16
    ) where

17
import Data.List (intercalate)
18 19 20 21
import Text.Printf (printf)

import Language.SystemVerilog.AST.ShowHelp

22
import Language.SystemVerilog.AST.Attr (Attr)
23
import Language.SystemVerilog.AST.Decl (Decl(CommentDecl))
24 25 26 27 28
import Language.SystemVerilog.AST.Stmt (Stmt)
import Language.SystemVerilog.AST.Type (Type, Identifier)
import {-# SOURCE #-} Language.SystemVerilog.AST.ModuleItem (ModuleItem)

data Description
29
    = Part [Attr] Bool PartKW Lifetime Identifier [Identifier] [ModuleItem]
30
    | PackageItem PackageItem
31
    | Package Lifetime Identifier [PackageItem]
32
    | Class   Lifetime Identifier [Decl] [ClassItem]
33 34 35
    deriving Eq

instance Show Description where
36
    showList l _ = unlines' $ map show l
37 38 39
    show (Part attrs True  kw lifetime name _ items) =
        printf "%sextern %s %s%s %s;"
            (concatMap showPad attrs)
40
            (show kw) (showPad lifetime) name (indentedParenList itemStrs)
41
        where itemStrs = map (init . show) items
42 43 44
    show (Part attrs False kw lifetime name ports items) =
        printf "%s%s %s%s%s;\n%s\nend%s"
            (concatMap showPad attrs)
45
            (show kw) (showPad lifetime) name portsStr bodyStr (show kw)
46 47 48 49 50
        where
            portsStr = if null ports
                then ""
                else " " ++ indentedParenList ports
            bodyStr = indent $ unlines' $ map show items
51 52
    show (Package lifetime name items) =
        printf "package %s%s;\n%s\nendpackage"
53
            (showPad lifetime) name bodyStr
54 55
        where
            bodyStr = indent $ unlines' $ map show items
56
    show (Class lifetime name decls items) =
57 58
        printf "class %s%s%s;\n%s\nendclass"
            (showPad lifetime) name (showParamDecls decls) bodyStr
59
        where
60
            bodyStr = indent $ unlines' $ map showClassItem items
61 62
    show (PackageItem i) = show i

63 64
showParamDecls :: [Decl] -> String
showParamDecls [] = ""
65 66 67 68 69 70 71 72 73 74 75 76
showParamDecls decls = " #(\n\t" ++ showDecls decls ++ "\n)"

showDecls :: [Decl] -> String
showDecls =
    dropDelim . intercalate "\n\t" . map showDecl
    where
        dropDelim :: String -> String
        dropDelim [] = []
        dropDelim [x] = if x == ',' then [] else [x]
        dropDelim (x : xs) = x : dropDelim xs
        showDecl comment@CommentDecl{} = show comment
        showDecl decl = (init $ show decl) ++ ","
77

78
data PackageItem
79
    = Function Lifetime Type Identifier [Decl] [Stmt]
80
    | Task     Lifetime      Identifier [Decl] [Stmt]
81 82
    | Import Identifier Identifier
    | Export Identifier Identifier
83
    | Decl Decl
84
    | Directive String
85 86 87 88
    deriving Eq

instance Show PackageItem where
    show (Function ml t x i b) =
89 90
        printf "function %s%s%s;\n%s\nendfunction" (showPad ml) (showPad t) x
        (showBlock i b)
91
    show (Task ml x i b) =
92 93
        printf "task %s%s;\n%s\nendtask"
            (showPad ml) x (showBlock i b)
94 95
    show (Import x y) = printf "import %s::%s;" x (showWildcard y)
    show (Export x y) = printf "export %s::%s;" (showWildcard x) (showWildcard y)
96
    show (Decl decl) = show decl
97
    show (Directive str) = str
98

99 100 101 102
showWildcard :: Identifier -> String
showWildcard "" = "*"
showWildcard x = x

103 104 105 106 107 108 109 110 111 112 113 114
data PartKW
    = Module
    | Interface
    deriving Eq

instance Show PartKW where
    show Module    = "module"
    show Interface = "interface"

data Lifetime
    = Static
    | Automatic
115
    | Inherit
116
    deriving Eq
117 118 119 120

instance Show Lifetime where
    show Static    = "static"
    show Automatic = "automatic"
121
    show Inherit   = ""
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

type ClassItem = (Qualifier, PackageItem)

showClassItem :: ClassItem -> String
showClassItem (qualifier, item) = showPad qualifier ++ show item

data Qualifier
    = QNone
    | QStatic
    | QLocal
    | QProtected
    deriving Eq

instance Show Qualifier where
    show QNone      = ""
    show QStatic    = "static"
    show QLocal     = "local"
    show QProtected = "protected"