Merge MatchFixity and HsMatchContext
[ghc.git] / compiler / hsSyn / HsLit.hs
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
8 {-# LANGUAGE CPP, DeriveDataTypeable #-}
9 {-# LANGUAGE TypeSynonymInstances #-}
10 {-# LANGUAGE StandaloneDeriving #-}
11 {-# LANGUAGE FlexibleInstances #-}
12 {-# LANGUAGE FlexibleContexts #-}
13 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
14 -- in module PlaceHolder
15 {-# LANGUAGE ConstraintKinds #-}
16
17 module HsLit where
18
19 #include "HsVersions.h"
20
21 import {-# SOURCE #-} HsExpr( HsExpr, pprExpr )
22 import BasicTypes ( FractionalLit(..),SourceText )
23 import Type ( Type )
24 import Outputable
25 import FastString
26 import PlaceHolder ( PostTc,PostRn,DataId,OutputableBndrId )
27
28 import Data.ByteString (ByteString)
29 import Data.Data hiding ( Fixity )
30
31 {-
32 ************************************************************************
33 * *
34 \subsection[HsLit]{Literals}
35 * *
36 ************************************************************************
37 -}
38
39 -- Note [Literal source text] in BasicTypes for SourceText fields in
40 -- the following
41 data HsLit
42 = HsChar SourceText Char -- Character
43 | HsCharPrim SourceText Char -- Unboxed character
44 | HsString SourceText FastString -- String
45 | HsStringPrim SourceText ByteString -- Packed bytes
46 | HsInt SourceText Integer -- Genuinely an Int; arises from
47 -- TcGenDeriv, and from TRANSLATION
48 | HsIntPrim SourceText Integer -- literal Int#
49 | HsWordPrim SourceText Integer -- literal Word#
50 | HsInt64Prim SourceText Integer -- literal Int64#
51 | HsWord64Prim SourceText Integer -- literal Word64#
52 | HsInteger SourceText Integer Type -- Genuinely an integer; arises only
53 -- from TRANSLATION (overloaded
54 -- literals are done with HsOverLit)
55 | HsRat FractionalLit Type -- Genuinely a rational; arises only from
56 -- TRANSLATION (overloaded literals are
57 -- done with HsOverLit)
58 | HsFloatPrim FractionalLit -- Unboxed Float
59 | HsDoublePrim FractionalLit -- Unboxed Double
60 deriving Data
61
62 instance Eq HsLit where
63 (HsChar _ x1) == (HsChar _ x2) = x1==x2
64 (HsCharPrim _ x1) == (HsCharPrim _ x2) = x1==x2
65 (HsString _ x1) == (HsString _ x2) = x1==x2
66 (HsStringPrim _ x1) == (HsStringPrim _ x2) = x1==x2
67 (HsInt _ x1) == (HsInt _ x2) = x1==x2
68 (HsIntPrim _ x1) == (HsIntPrim _ x2) = x1==x2
69 (HsWordPrim _ x1) == (HsWordPrim _ x2) = x1==x2
70 (HsInt64Prim _ x1) == (HsInt64Prim _ x2) = x1==x2
71 (HsWord64Prim _ x1) == (HsWord64Prim _ x2) = x1==x2
72 (HsInteger _ x1 _) == (HsInteger _ x2 _) = x1==x2
73 (HsRat x1 _) == (HsRat x2 _) = x1==x2
74 (HsFloatPrim x1) == (HsFloatPrim x2) = x1==x2
75 (HsDoublePrim x1) == (HsDoublePrim x2) = x1==x2
76 _ == _ = False
77
78 data HsOverLit id -- An overloaded literal
79 = OverLit {
80 ol_val :: OverLitVal,
81 ol_rebindable :: PostRn id Bool, -- Note [ol_rebindable]
82 ol_witness :: HsExpr id, -- Note [Overloaded literal witnesses]
83 ol_type :: PostTc id Type }
84 deriving instance (DataId id) => Data (HsOverLit id)
85
86 -- Note [Literal source text] in BasicTypes for SourceText fields in
87 -- the following
88 data OverLitVal
89 = HsIntegral !SourceText !Integer -- Integer-looking literals;
90 | HsFractional !FractionalLit -- Frac-looking literals
91 | HsIsString !SourceText !FastString -- String-looking literals
92 deriving Data
93
94 overLitType :: HsOverLit a -> PostTc a Type
95 overLitType = ol_type
96
97 {-
98 Note [ol_rebindable]
99 ~~~~~~~~~~~~~~~~~~~~
100 The ol_rebindable field is True if this literal is actually
101 using rebindable syntax. Specifically:
102
103 False iff ol_witness is the standard one
104 True iff ol_witness is non-standard
105
106 Equivalently it's True if
107 a) RebindableSyntax is on
108 b) the witness for fromInteger/fromRational/fromString
109 that happens to be in scope isn't the standard one
110
111 Note [Overloaded literal witnesses]
112 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113 *Before* type checking, the HsExpr in an HsOverLit is the
114 name of the coercion function, 'fromInteger' or 'fromRational'.
115 *After* type checking, it is a witness for the literal, such as
116 (fromInteger 3) or lit_78
117 This witness should replace the literal.
118
119 This dual role is unusual, because we're replacing 'fromInteger' with
120 a call to fromInteger. Reason: it allows commoning up of the fromInteger
121 calls, which wouldn't be possible if the desguarar made the application.
122
123 The PostTcType in each branch records the type the overload literal is
124 found to have.
125 -}
126
127 -- Comparison operations are needed when grouping literals
128 -- for compiling pattern-matching (module MatchLit)
129 instance Eq (HsOverLit id) where
130 (OverLit {ol_val = val1}) == (OverLit {ol_val=val2}) = val1 == val2
131
132 instance Eq OverLitVal where
133 (HsIntegral _ i1) == (HsIntegral _ i2) = i1 == i2
134 (HsFractional f1) == (HsFractional f2) = f1 == f2
135 (HsIsString _ s1) == (HsIsString _ s2) = s1 == s2
136 _ == _ = False
137
138 instance Ord (HsOverLit id) where
139 compare (OverLit {ol_val=val1}) (OverLit {ol_val=val2}) = val1 `compare` val2
140
141 instance Ord OverLitVal where
142 compare (HsIntegral _ i1) (HsIntegral _ i2) = i1 `compare` i2
143 compare (HsIntegral _ _) (HsFractional _) = LT
144 compare (HsIntegral _ _) (HsIsString _ _) = LT
145 compare (HsFractional f1) (HsFractional f2) = f1 `compare` f2
146 compare (HsFractional _) (HsIntegral _ _) = GT
147 compare (HsFractional _) (HsIsString _ _) = LT
148 compare (HsIsString _ s1) (HsIsString _ s2) = s1 `compare` s2
149 compare (HsIsString _ _) (HsIntegral _ _) = GT
150 compare (HsIsString _ _) (HsFractional _) = GT
151
152 instance Outputable HsLit where
153 ppr (HsChar _ c) = pprHsChar c
154 ppr (HsCharPrim _ c) = pprPrimChar c
155 ppr (HsString _ s) = pprHsString s
156 ppr (HsStringPrim _ s) = pprHsBytes s
157 ppr (HsInt _ i) = integer i
158 ppr (HsInteger _ i _) = integer i
159 ppr (HsRat f _) = ppr f
160 ppr (HsFloatPrim f) = ppr f <> primFloatSuffix
161 ppr (HsDoublePrim d) = ppr d <> primDoubleSuffix
162 ppr (HsIntPrim _ i) = pprPrimInt i
163 ppr (HsWordPrim _ w) = pprPrimWord w
164 ppr (HsInt64Prim _ i) = pprPrimInt64 i
165 ppr (HsWord64Prim _ w) = pprPrimWord64 w
166
167 -- in debug mode, print the expression that it's resolved to, too
168 instance (OutputableBndrId id) => Outputable (HsOverLit id) where
169 ppr (OverLit {ol_val=val, ol_witness=witness})
170 = ppr val <+> (ifPprDebug (parens (pprExpr witness)))
171
172 instance Outputable OverLitVal where
173 ppr (HsIntegral _ i) = integer i
174 ppr (HsFractional f) = ppr f
175 ppr (HsIsString _ s) = pprHsString s
176
177 -- | pmPprHsLit pretty prints literals and is used when pretty printing pattern
178 -- match warnings. All are printed the same (i.e., without hashes if they are
179 -- primitive and not wrapped in constructors if they are boxed). This happens
180 -- mainly for too reasons:
181 -- * We do not want to expose their internal representation
182 -- * The warnings become too messy
183 pmPprHsLit :: HsLit -> SDoc
184 pmPprHsLit (HsChar _ c) = pprHsChar c
185 pmPprHsLit (HsCharPrim _ c) = pprHsChar c
186 pmPprHsLit (HsString _ s) = pprHsString s
187 pmPprHsLit (HsStringPrim _ s) = pprHsBytes s
188 pmPprHsLit (HsInt _ i) = integer i
189 pmPprHsLit (HsIntPrim _ i) = integer i
190 pmPprHsLit (HsWordPrim _ w) = integer w
191 pmPprHsLit (HsInt64Prim _ i) = integer i
192 pmPprHsLit (HsWord64Prim _ w) = integer w
193 pmPprHsLit (HsInteger _ i _) = integer i
194 pmPprHsLit (HsRat f _) = ppr f
195 pmPprHsLit (HsFloatPrim f) = ppr f
196 pmPprHsLit (HsDoublePrim d) = ppr d