Smarter HsType pretty-print for promoted datacons
[ghc.git] / compiler / basicTypes / FieldLabel.hs
1 {-
2 %
3 % (c) Adam Gundry 2013-2015
4 %
5
6 This module defines the representation of FieldLabels as stored in
7 TyCons. As well as a selector name, these have some extra structure
8 to support the DuplicateRecordFields extension.
9
10 In the normal case (with NoDuplicateRecordFields), a datatype like
11
12 data T = MkT { foo :: Int }
13
14 has
15
16 FieldLabel { flLabel = "foo"
17 , flIsOverloaded = False
18 , flSelector = foo }.
19
20 In particular, the Name of the selector has the same string
21 representation as the label. If DuplicateRecordFields
22 is enabled, however, the same declaration instead gives
23
24 FieldLabel { flLabel = "foo"
25 , flIsOverloaded = True
26 , flSelector = $sel:foo:MkT }.
27
28 Now the name of the selector ($sel:foo:MkT) does not match the label of
29 the field (foo). We must be careful not to show the selector name to
30 the user! The point of mangling the selector name is to allow a
31 module to define the same field label in different datatypes:
32
33 data T = MkT { foo :: Int }
34 data U = MkU { foo :: Bool }
35
36 Now there will be two FieldLabel values for 'foo', one in T and one in
37 U. They share the same label (FieldLabelString), but the selector
38 functions differ.
39
40 See also Note [Representing fields in AvailInfo] in Avail.
41
42 Note [Why selector names include data constructors]
43 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
45 As explained above, a selector name includes the name of the first
46 data constructor in the type, so that the same label can appear
47 multiple times in the same module. (This is irrespective of whether
48 the first constructor has that field, for simplicity.)
49
50 We use a data constructor name, rather than the type constructor name,
51 because data family instances do not have a representation type
52 constructor name generated until relatively late in the typechecking
53 process.
54
55 Of course, datatypes with no constructors cannot have any fields.
56
57 -}
58
59 {-# LANGUAGE DeriveDataTypeable #-}
60 {-# LANGUAGE DeriveFunctor #-}
61 {-# LANGUAGE DeriveFoldable #-}
62 {-# LANGUAGE DeriveTraversable #-}
63 {-# LANGUAGE StandaloneDeriving #-}
64
65 module FieldLabel ( FieldLabelString
66 , FieldLabelEnv
67 , FieldLbl(..)
68 , FieldLabel
69 , mkFieldLabelOccs
70 ) where
71
72 import GhcPrelude
73
74 import OccName
75 import Name
76
77 import FastString
78 import FastStringEnv
79 import Outputable
80 import Binary
81
82 import Data.Data
83
84 -- | Field labels are just represented as strings;
85 -- they are not necessarily unique (even within a module)
86 type FieldLabelString = FastString
87
88 -- | A map from labels to all the auxiliary information
89 type FieldLabelEnv = DFastStringEnv FieldLabel
90
91
92 type FieldLabel = FieldLbl Name
93
94 -- | Fields in an algebraic record type
95 data FieldLbl a = FieldLabel {
96 flLabel :: FieldLabelString, -- ^ User-visible label of the field
97 flIsOverloaded :: Bool, -- ^ Was DuplicateRecordFields on
98 -- in the defining module for this datatype?
99 flSelector :: a -- ^ Record selector function
100 }
101 deriving (Eq, Functor, Foldable, Traversable)
102 deriving instance Data a => Data (FieldLbl a)
103
104 instance Outputable a => Outputable (FieldLbl a) where
105 ppr fl = ppr (flLabel fl) <> braces (ppr (flSelector fl))
106
107 instance Binary a => Binary (FieldLbl a) where
108 put_ bh (FieldLabel aa ab ac) = do
109 put_ bh aa
110 put_ bh ab
111 put_ bh ac
112 get bh = do
113 ab <- get bh
114 ac <- get bh
115 ad <- get bh
116 return (FieldLabel ab ac ad)
117
118
119 -- | Record selector OccNames are built from the underlying field name
120 -- and the name of the first data constructor of the type, to support
121 -- duplicate record field names.
122 -- See Note [Why selector names include data constructors].
123 mkFieldLabelOccs :: FieldLabelString -> OccName -> Bool -> FieldLbl OccName
124 mkFieldLabelOccs lbl dc is_overloaded
125 = FieldLabel { flLabel = lbl, flIsOverloaded = is_overloaded
126 , flSelector = sel_occ }
127 where
128 str = ":" ++ unpackFS lbl ++ ":" ++ occNameString dc
129 sel_occ | is_overloaded = mkRecFldSelOcc str
130 | otherwise = mkVarOccFS lbl