2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[HsLit]{Abstract syntax: source-language literals}
8 {-# LANGUAGE DeriveDataTypeable #-}
12 #include "HsVersions.h"
14 import {-# SOURCE #-} HsExpr( SyntaxExpr, pprExpr )
15 import HsTypes (PostTcType)
24 %************************************************************************
26 \subsection[HsLit]{Literals}
28 %************************************************************************
33 = HsChar Char -- Character
34 | HsCharPrim Char -- Unboxed character
35 | HsString FastString -- String
36 | HsStringPrim FastString -- Packed string
37 | HsInt Integer -- Genuinely an Int; arises from TcGenDeriv,
38 -- and from TRANSLATION
39 | HsIntPrim Integer -- Unboxed Int
40 | HsWordPrim Integer -- Unboxed Word
41 | HsInteger Integer Type -- Genuinely an integer; arises only from TRANSLATION
42 -- (overloaded literals are done with HsOverLit)
43 | HsRat Rational Type -- Genuinely a rational; arises only from TRANSLATION
44 -- (overloaded literals are done with HsOverLit)
45 | HsFloatPrim Rational -- Unboxed Float
46 | HsDoublePrim Rational -- Unboxed Double
47 deriving (Data, Typeable)
49 instance Eq HsLit where
50 (HsChar x1) == (HsChar x2) = x1==x2
51 (HsCharPrim x1) == (HsCharPrim x2) = x1==x2
52 (HsString x1) == (HsString x2) = x1==x2
53 (HsStringPrim x1) == (HsStringPrim x2) = x1==x2
54 (HsInt x1) == (HsInt x2) = x1==x2
55 (HsIntPrim x1) == (HsIntPrim x2) = x1==x2
56 (HsWordPrim x1) == (HsWordPrim x2) = x1==x2
57 (HsInteger x1 _) == (HsInteger x2 _) = x1==x2
58 (HsRat x1 _) == (HsRat x2 _) = x1==x2
59 (HsFloatPrim x1) == (HsFloatPrim x2) = x1==x2
60 (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
63 data HsOverLit id -- An overloaded literal
66 ol_rebindable :: Bool, -- True <=> rebindable syntax
67 -- False <=> standard syntax
68 ol_witness :: SyntaxExpr id, -- Note [Overloaded literal witnesses]
69 ol_type :: PostTcType }
70 deriving (Data, Typeable)
73 = HsIntegral !Integer -- Integer-looking literals;
74 | HsFractional !Rational -- Frac-looking literals
75 | HsIsString !FastString -- String-looking literals
76 deriving (Data, Typeable)
78 overLitType :: HsOverLit a -> Type
82 Note [Overloaded literal witnesses]
83 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84 *Before* type checking, the SyntaxExpr in an HsOverLit is the
85 name of the coercion function, 'fromInteger' or 'fromRational'.
86 *After* type checking, it is a witness for the literal, such as
87 (fromInteger 3) or lit_78
88 This witness should replace the literal.
90 This dual role is unusual, because we're replacing 'fromInteger' with
91 a call to fromInteger. Reason: it allows commoning up of the fromInteger
92 calls, which wouldn't be possible if the desguarar made the application
94 The PostTcType in each branch records the type the overload literal is
98 -- Comparison operations are needed when grouping literals
99 -- for compiling pattern-matching (module MatchLit)
100 instance Eq (HsOverLit id) where
101 (OverLit {ol_val = val1}) == (OverLit {ol_val=val2}) = val1 == val2
103 instance Eq OverLitVal where
104 (HsIntegral i1) == (HsIntegral i2) = i1 == i2
105 (HsFractional f1) == (HsFractional f2) = f1 == f2
106 (HsIsString s1) == (HsIsString s2) = s1 == s2
109 instance Ord (HsOverLit id) where
110 compare (OverLit {ol_val=val1}) (OverLit {ol_val=val2}) = val1 `compare` val2
112 instance Ord OverLitVal where
113 compare (HsIntegral i1) (HsIntegral i2) = i1 `compare` i2
114 compare (HsIntegral _) (HsFractional _) = LT
115 compare (HsIntegral _) (HsIsString _) = LT
116 compare (HsFractional f1) (HsFractional f2) = f1 `compare` f2
117 compare (HsFractional _) (HsIntegral _) = GT
118 compare (HsFractional _) (HsIsString _) = LT
119 compare (HsIsString s1) (HsIsString s2) = s1 `compare` s2
120 compare (HsIsString _) (HsIntegral _) = GT
121 compare (HsIsString _) (HsFractional _) = GT
125 instance Outputable HsLit where
126 -- Use "show" because it puts in appropriate escapes
127 ppr (HsChar c) = pprHsChar c
128 ppr (HsCharPrim c) = pprHsChar c <> char '#'
129 ppr (HsString s) = pprHsString s
130 ppr (HsStringPrim s) = pprHsString s <> char '#'
131 ppr (HsInt i) = integer i
132 ppr (HsInteger i _) = integer i
133 ppr (HsRat f _) = rational f
134 ppr (HsFloatPrim f) = rational f <> char '#'
135 ppr (HsDoublePrim d) = rational d <> text "##"
136 ppr (HsIntPrim i) = integer i <> char '#'
137 ppr (HsWordPrim w) = integer w <> text "##"
139 -- in debug mode, print the expression that it's resolved to, too
140 instance OutputableBndr id => Outputable (HsOverLit id) where
141 ppr (OverLit {ol_val=val, ol_witness=witness})
142 = ppr val <+> (ifPprDebug (parens (pprExpr witness)))
144 instance Outputable OverLitVal where
145 ppr (HsIntegral i) = integer i
146 ppr (HsFractional f) = rational f
147 ppr (HsIsString s) = pprHsString s