[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsCore.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
7 %*                                                                      *
8 %************************************************************************
9
10 We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
11 @TyVars@ as well.  Currently trying the former... MEGA SIGH.
12
13 \begin{code}
14 module HsCore (
15         UfExpr(..), UfAlt, UfBinder(..), UfNote(..),
16         UfBinding(..), UfCon(..)
17     ) where
18
19 #include "HsVersions.h"
20
21 -- friends:
22 import HsTypes          ( HsType, pprParendHsType )
23
24 -- others:
25 import Const            ( Literal )
26 import Type             ( Kind )
27 import CostCentre
28 import Outputable
29 \end{code}
30
31 %************************************************************************
32 %*                                                                      *
33 \subsection[HsCore-types]{Types for read/written Core unfoldings}
34 %*                                                                      *
35 %************************************************************************
36
37 \begin{code}
38 data UfExpr name
39   = UfVar       name
40   | UfType      (HsType name)
41   | UfCon       (UfCon name) [UfExpr name]
42   | UfTuple     name [UfExpr name]              -- Type arguments omitted
43   | UfLam       (UfBinder name)   (UfExpr name)
44   | UfApp       (UfExpr name) (UfExpr name)
45   | UfCase      (UfExpr name) name [UfAlt name]
46   | UfLet       (UfBinding name)  (UfExpr name)
47   | UfNote      (UfNote name) (UfExpr name)
48
49 data UfNote name = UfSCC CostCentre
50                  | UfCoerce (HsType name)
51                  | UfInlineCall
52
53 type UfAlt name = (UfCon name, [name], UfExpr name)
54
55 data UfCon name = UfDefault
56                 | UfDataCon name
57                 | UfLitCon Literal
58                 | UfLitLitCon FAST_STRING (HsType name)
59                 | UfPrimOp name
60                 | UfCCallOp FAST_STRING    -- callee
61                             Bool           -- True <=> casm, rather than ccall
62                             Bool           -- True <=> might cause GC
63
64 data UfBinding name
65   = UfNonRec    (UfBinder name)
66                 (UfExpr name)
67   | UfRec       [(UfBinder name, UfExpr name)]
68
69 data UfBinder name
70   = UfValBinder name (HsType name)
71   | UfTyBinder  name Kind
72 \end{code}
73
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection[HsCore-print]{Printing Core unfoldings}
78 %*                                                                      *
79 %************************************************************************
80
81 \begin{code}
82 instance Outputable name => Outputable (UfExpr name) where
83     ppr (UfVar v) = ppr v
84     ppr (UfType ty) = char '@' <+> pprParendHsType ty
85
86     ppr (UfCon c as)
87       = hsep [text "UfCon", ppr c, ppr as]
88
89     ppr (UfTuple c as) = parens (hsep (punctuate comma (map ppr as)))
90
91     ppr (UfLam b body)
92       = hsep [char '\\', ppr b, ptext SLIT("->"), ppr body]
93
94     ppr (UfApp fun arg) = ppr fun <+> ppr arg 
95
96     ppr (UfCase scrut bndr alts)
97       = hsep [ptext SLIT("case"), ppr scrut, ptext SLIT("of"), ppr bndr,
98               braces (hsep (punctuate semi (map pp_alt alts)))]
99       where
100         pp_alt (c,bs,rhs) = hsep [ppr c, ppr bs, ppr_arrow, ppr rhs]
101
102         ppr_arrow = ptext SLIT("->")
103
104     ppr (UfLet (UfNonRec b rhs) body)
105       = hsep [ptext SLIT("let"), ppr b, equals, ppr rhs, ptext SLIT("in"), ppr body]
106     ppr (UfLet (UfRec pairs) body)
107       = hsep [ptext SLIT("letrec"), braces (hsep (punctuate semi (map pp_pair pairs))), ptext SLIT("in"), ppr body]
108       where
109         pp_pair (b,rhs) = hsep [ppr b, equals, ppr rhs]
110
111     ppr (UfNote note body)
112       = hsep [ptext SLIT("_NOTE_ [ToDo]>"), ppr body]
113
114 instance Outputable name => Outputable (UfCon name) where
115     ppr UfDefault       = text "DEFAULT"
116     ppr (UfDataCon d)   = ppr d
117     ppr (UfPrimOp p)    = ppr p
118     ppr (UfCCallOp str is_casm can_gc)
119       = hcat [before, ptext str, after]
120       where
121             before = ptext (if is_casm then SLIT("_casm_ ``") else SLIT("_ccall_ "))
122             after  = if is_casm then text "'' " else space
123
124 instance Outputable name => Outputable (UfBinder name) where
125     ppr (UfValBinder name ty)  = hsep [ppr name, ptext SLIT("::"), ppr ty]
126     ppr (UfTyBinder name kind) = hsep [ppr name, ptext SLIT("::"), ppr kind]
127 \end{code}
128