db9e968df09bfdd9f4c7e1abe977036ef8604079
[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 OccName
73 import Name
74
75 import FastString
76 import Outputable
77 import Binary
78
79 import Data.Data
80
81 -- | Field labels are just represented as strings;
82 -- they are not necessarily unique (even within a module)
83 type FieldLabelString = FastString
84
85 -- | A map from labels to all the auxiliary information
86 type FieldLabelEnv = FastStringEnv FieldLabel
87
88
89 type FieldLabel = FieldLbl Name
90
91 -- | Fields in an algebraic record type
92 data FieldLbl a = FieldLabel {
93 flLabel :: FieldLabelString, -- ^ User-visible label of the field
94 flIsOverloaded :: Bool, -- ^ Was DuplicateRecordFields on
95 -- in the defining module for this datatype?
96 flSelector :: a -- ^ Record selector function
97 }
98 deriving (Eq, Functor, Foldable, Traversable)
99 deriving instance Data a => Data (FieldLbl a)
100
101 instance Outputable a => Outputable (FieldLbl a) where
102 ppr fl = ppr (flLabel fl) <> braces (ppr (flSelector fl))
103
104 instance Binary a => Binary (FieldLbl a) where
105 put_ bh (FieldLabel aa ab ac) = do
106 put_ bh aa
107 put_ bh ab
108 put_ bh ac
109 get bh = do
110 ab <- get bh
111 ac <- get bh
112 ad <- get bh
113 return (FieldLabel ab ac ad)
114
115
116 -- | Record selector OccNames are built from the underlying field name
117 -- and the name of the first data constructor of the type, to support
118 -- duplicate record field names.
119 -- See Note [Why selector names include data constructors].
120 mkFieldLabelOccs :: FieldLabelString -> OccName -> Bool -> FieldLbl OccName
121 mkFieldLabelOccs lbl dc is_overloaded
122 = FieldLabel { flLabel = lbl, flIsOverloaded = is_overloaded
123 , flSelector = sel_occ }
124 where
125 str = ":" ++ unpackFS lbl ++ ":" ++ occNameString dc
126 sel_occ | is_overloaded = mkRecFldSelOcc str
127 | otherwise = mkVarOccFS lbl