Debian Codegraph @

c2hs/c/CPretty.hs

raw symbols SCIP
1 {-# LANGUAGE CPP #-}
2 -- C->Haskell Compiler: pretty printing of C abstract syntax
3 --
4 -- Author : Manuel M T Chakravarty
5 -- Created: 25 August 1
6 --
7 -- Version $Revision: 1.3 $ from $Date: 2005/06/22 16:01:21 $
8 --
9 -- Copyright (c) [2001..2004] Manuel M T Chakravarty
10 --
11 -- This file is free software; you can redistribute it and/or modify
12 -- it under the terms of the GNU General Public License as published by
13 -- the Free Software Foundation; either version 2 of the License, or
14 -- (at your option) any later version.
15 --
16 -- This file is distributed in the hope that it will be useful,
17 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
18 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 -- GNU General Public License for more details.
20 --
21 --- DESCRIPTION ---------------------------------------------------------------
22 --
23 -- Pretty printing support for abstract C trees.
24 --
25 --- DOCU ----------------------------------------------------------------------
26 --
27 -- language: Haskell 98
28 --
29 --- TODO ----------------------------------------------------------------------
30 --
31 -- * So far, only covers a small fraction of the abstract tree definition
32 --
33
34 module CPretty (
35 -- we are just providing instances to the class `Pretty'
36 ) where
37
38 #if MIN_VERSION_base(4,11,0)
39 import Prelude hiding ((<>))
40 #endif
41 import Idents (Ident, identToLexeme)
42 import Text.PrettyPrint.HughesPJ
43
44 import CAST
45
46
47 -- pretty printing of AST nodes
48 -- ----------------------------
49
50 instance Show CDecl where
51 showsPrec _ = showString . render . pretty
52
53 -- overloaded pretty-printing function (EXPORTED)
54 --
55 class Pretty a where
56 pretty :: a -> Doc
57 prettyPrec :: Int -> a -> Doc
58
59 pretty = prettyPrec 0
60 prettyPrec _ = pretty
61
62
63 -- actual structure tree traversals
64 -- --------------------------------
65
66 instance Pretty CDecl where
67 pretty (CDecl specs declrs _) =
68 hsep (map pretty specs) `hang` 2 $
69 hsep (punctuate comma (map prettyDeclr declrs)) <> semi
70
71 instance Pretty CDeclSpec where
72 pretty (CStorageSpec sspec) = pretty sspec
73 pretty (CTypeSpec tspec) = pretty tspec
74 pretty (CTypeQual qspec) = pretty qspec
75
76 instance Pretty CStorageSpec where
77 pretty (CAuto _) = text "auto"
78 pretty (CRegister _) = text "register"
79 pretty (CStatic _) = text "static"
80 pretty (CExtern _) = text "extern"
81 pretty (CTypedef _) = text "typedef"
82
83 instance Pretty CTypeSpec where
84 pretty (CVoidType _) = text "void"
85 pretty (CCharType _) = text "char"
86 pretty (CShortType _) = text "short"
87 pretty (CIntType _) = text "int"
88 pretty (CLongType _) = text "long"
89 pretty (CFloatType _) = text "float"
90 pretty (CFloat128Type _) = text "__float128"
91 pretty (CDoubleType _) = text "double"
92 pretty (CSignedType _) = text "signed"
93 pretty (CUnsigType _) = text "unsigned"
94 pretty (CSUType struct _) = text "<<CPretty: CSUType not yet implemented!>>"
95 pretty (CEnumType enum _) = text "<<CPretty: CEnumType not yet implemented!>>"
96 pretty (CTypeDef ide _) = ident ide
97
98 instance Pretty CTypeQual where
99 pretty (CConstQual _) = text "const"
100 pretty (CVolatQual _) = text "volatile"
101 pretty (CRestrQual _) = text "restrict"
102
103 prettyDeclr :: (Maybe CDeclr, Maybe CInit, Maybe CExpr) -> Doc
104 prettyDeclr (odeclr, oinit, oexpr) =
105 maybe empty pretty odeclr
106 <+> maybe empty ((text "=" <+>) . pretty) oinit
107 <+> maybe empty ((text ":" <+>) . pretty) oexpr
108
109 instance Pretty CDeclr where
110 pretty (CVarDeclr oide _) = maybe empty ident oide
111 pretty (CPtrDeclr inds declr _) =
112 let
113 oneLevel ind = parens . (hsep (map pretty ind) <+>) . (text "*" <>)
114 in
115 oneLevel inds (pretty declr)
116 pretty (CArrDeclr declr _ oexpr _) =
117 pretty declr <> brackets (maybe empty pretty oexpr)
118 pretty (CFunDeclr declr decls isVariadic _) =
119 let
120 varDoc = if isVariadic then text ", ..." else empty
121 in
122 pretty declr
123 <+> parens (hsep (punctuate comma (map pretty decls)) <> varDoc)
124
125 instance Pretty CInit where
126 pretty _ = text "<<CPretty: CInit not yet implemented!>>"
127
128 instance Pretty CExpr where
129 pretty _ = text "<<CPretty: CExpr not yet implemented!>>"
130
131
132 -- auxiliary functions
133 -- --------------------
134
135 ident :: Ident -> Doc
136 ident = text . identToLexeme