Make literals in the syntax tree strict
[ghc-hetmet.git] / compiler / hsSyn / HsLit.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5 \section[HsLit]{Abstract syntax: source-language literals}
6
7 \begin{code}
8 module HsLit where
9
10 #include "HsVersions.h"
11
12 import {-# SOURCE #-} HsExpr( SyntaxExpr, pprExpr )
13 import HsTypes (PostTcType)
14 import Type     ( Type )
15 import Outputable
16 import FastString
17 import Ratio    ( Rational )
18 \end{code}
19
20
21 %************************************************************************
22 %*                                                                      *
23 \subsection[HsLit]{Literals}
24 %*                                                                      *
25 %************************************************************************
26
27
28 \begin{code}
29 data HsLit
30   = HsChar          Char                -- Character
31   | HsCharPrim      Char                -- Unboxed character
32   | HsString        FastString          -- String
33   | HsStringPrim    FastString          -- Packed string
34   | HsInt           Integer             -- Genuinely an Int; arises from TcGenDeriv, 
35                                         --      and from TRANSLATION
36   | HsIntPrim       Integer             -- Unboxed Int
37   | HsInteger       Integer  Type       -- Genuinely an integer; arises only from TRANSLATION
38                                         --      (overloaded literals are done with HsOverLit)
39   | HsRat           Rational Type       -- Genuinely a rational; arises only from TRANSLATION
40                                         --      (overloaded literals are done with HsOverLit)
41   | HsFloatPrim     Rational            -- Unboxed Float
42   | HsDoublePrim    Rational            -- Unboxed Double
43
44 instance Eq HsLit where
45   (HsChar x1)       == (HsChar x2)       = x1==x2
46   (HsCharPrim x1)   == (HsCharPrim x2)   = x1==x2
47   (HsString x1)     == (HsString x2)     = x1==x2
48   (HsStringPrim x1) == (HsStringPrim x2) = x1==x2
49   (HsInt x1)        == (HsInt x2)        = x1==x2
50   (HsIntPrim x1)    == (HsIntPrim x2)    = x1==x2
51   (HsInteger x1 _)  == (HsInteger x2 _)  = x1==x2
52   (HsRat x1 _)      == (HsRat x2 _)      = x1==x2
53   (HsFloatPrim x1)  == (HsFloatPrim x2)  = x1==x2
54   (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
55   _                 == _                 = False
56
57 data HsOverLit id       -- An overloaded literal
58   = HsIntegral   !Integer    (SyntaxExpr id)  PostTcType        -- Integer-looking literals;
59   | HsFractional !Rational   (SyntaxExpr id)  PostTcType        -- Frac-looking literals
60   | HsIsString   !FastString (SyntaxExpr id)  PostTcType        -- String-looking literals
61   -- Before type checking, the SyntaxExpr is 'fromInteger' or 'fromRational'
62   -- After type checking, it is (fromInteger 3) or lit_78; that is,
63   -- the expression that should replace the literal.
64   -- This is unusual, because we're replacing 'fromInteger' with a call 
65   -- to fromInteger.  Reason: it allows commoning up of the fromInteger
66   -- calls, which wouldn't be possible if the desguarar made the application
67   --
68   -- The PostTcType in each branch records the type the overload literal is
69   -- found to have.
70
71 overLitExpr :: HsOverLit id -> SyntaxExpr id
72 overLitExpr (HsIntegral _ e _) = e
73 overLitExpr (HsFractional _ e _) = e
74 overLitExpr (HsIsString _ e _) = e
75
76 overLitType :: HsOverLit id -> PostTcType
77 overLitType (HsIntegral _ _ t) = t
78 overLitType (HsFractional _ _ t) = t
79 overLitType (HsIsString _ _ t) = t
80
81
82 -- Comparison operations are needed when grouping literals
83 -- for compiling pattern-matching (module MatchLit)
84 instance Eq (HsOverLit id) where
85   (HsIntegral i1 _ _)   == (HsIntegral i2 _ _)   = i1 == i2
86   (HsFractional f1 _ _) == (HsFractional f2 _ _) = f1 == f2
87   (HsIsString s1 _ _)   == (HsIsString s2 _ _)   = s1 == s2
88   _                     == _                     = False
89
90 instance Ord (HsOverLit id) where
91   compare (HsIntegral i1 _ _)   (HsIntegral i2 _ _)   = i1 `compare` i2
92   compare (HsIntegral _ _ _)    (HsFractional _ _ _)  = LT
93   compare (HsIntegral _ _ _)    (HsIsString _ _ _)    = LT
94   compare (HsFractional f1 _ _) (HsFractional f2 _ _) = f1 `compare` f2
95   compare (HsFractional _ _ _)  (HsIntegral _ _ _)    = GT
96   compare (HsFractional _ _ _)  (HsIsString _ _ _)    = LT
97   compare (HsIsString s1 _ _)   (HsIsString s2 _ _)   = s1 `compare` s2
98   compare (HsIsString _ _ _)    (HsIntegral _ _ _)    = GT
99   compare (HsIsString _ _ _)    (HsFractional _ _ _)  = GT
100 \end{code}
101
102 \begin{code}
103 instance Outputable HsLit where
104         -- Use "show" because it puts in appropriate escapes
105     ppr (HsChar c)       = pprHsChar c
106     ppr (HsCharPrim c)   = pprHsChar c <> char '#'
107     ppr (HsString s)     = pprHsString s
108     ppr (HsStringPrim s) = pprHsString s <> char '#'
109     ppr (HsInt i)        = integer i
110     ppr (HsInteger i _)  = integer i
111     ppr (HsRat f _)      = rational f
112     ppr (HsFloatPrim f)  = rational f <> char '#'
113     ppr (HsDoublePrim d) = rational d <> text "##"
114     ppr (HsIntPrim i)    = integer i  <> char '#'
115
116 -- in debug mode, print the expression that it's resolved to, too
117 instance OutputableBndr id => Outputable (HsOverLit id) where
118   ppr (HsIntegral i e _)   = integer i <+> (ifPprDebug (parens (pprExpr e)))
119   ppr (HsFractional f e _) = rational f <+> (ifPprDebug (parens (pprExpr e)))
120   ppr (HsIsString s e _)   = pprHsString s <+> (ifPprDebug (parens (pprExpr e)))
121 \end{code}