2 % (c) The University of Glasgow, 1992-2006
9 mkSrcLoc, isGoodSrcLoc, mkGeneralSrcLoc,
10 noSrcLoc, -- "I'm sorry, I haven't a clue"
13 importedSrcLoc, -- Unknown place in an interface
14 generatedSrcLoc, -- Code generated within the compiler
15 interactiveSrcLoc, -- Code from an interactive session
17 srcLocFile, -- return the file name part
18 srcLocLine, -- return the line part
19 srcLocCol, -- return the column part
24 wiredInSrcSpan, -- Something wired into the compiler
25 importedSrcSpan, -- Unknown place in an interface
27 isGoodSrcSpan, isOneLineSpan,
28 mkSrcSpan, srcLocSpan,
30 srcSpanStart, srcSpanEnd,
33 -- These are dubious exports, because they crash on some inputs,
34 -- used only in Lexer.x where we are sure what the Span looks like
36 srcSpanStartLine, srcSpanEndLine,
37 srcSpanStartCol, srcSpanEndCol,
39 Located(..), getLoc, unLoc, noLoc, eqLocated, cmpLocated, combineLocs, addCLoc
42 #include "HsVersions.h"
49 %************************************************************************
51 \subsection[SrcLoc-SrcLocations]{Source-location information}
53 %************************************************************************
55 We keep information about the {\em definition} point for each entity;
56 this is the obvious stuff:
59 = SrcLoc FastString -- A precise location (file name)
60 !Int -- line number, begins at 1
61 !Int -- column number, begins at 0
62 -- Don't ask me why lines start at 1 and columns start at
63 -- zero. That's just the way it is, so there. --SDM
65 | ImportedLoc FastString -- Module name
67 | UnhelpfulLoc FastString -- Just a general indication
70 Note that an entity might be imported via more than one route, and
71 there could be more than one ``definition point'' --- in two or more
72 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
75 %************************************************************************
77 \subsection[SrcLoc-access-fns]{Access functions for names}
79 %************************************************************************
83 mkSrcLoc x line col = SrcLoc x line col
84 noSrcLoc = UnhelpfulLoc FSLIT("<no location info>")
85 generatedSrcLoc = UnhelpfulLoc FSLIT("<compiler-generated code>")
86 interactiveSrcLoc = UnhelpfulLoc FSLIT("<interactive session>")
88 mkGeneralSrcLoc :: FastString -> SrcLoc
89 mkGeneralSrcLoc = UnhelpfulLoc
91 importedSrcLoc :: FastString -> SrcLoc
92 importedSrcLoc mod_name = ImportedLoc mod_name
94 isGoodSrcLoc (SrcLoc _ _ _) = True
95 isGoodSrcLoc other = False
97 srcLocFile :: SrcLoc -> FastString
98 srcLocFile (SrcLoc fname _ _) = fname
99 srcLocFile other = FSLIT("<unknown file")
101 srcLocLine :: SrcLoc -> Int
102 srcLocLine (SrcLoc _ l c) = l
103 srcLocLine other = panic "srcLocLine: unknown line"
105 srcLocCol :: SrcLoc -> Int
106 srcLocCol (SrcLoc _ l c) = c
107 srcLocCol other = panic "srcLocCol: unknown col"
109 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
110 advanceSrcLoc (SrcLoc f l c) '\n' = SrcLoc f (l + 1) 0
111 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
112 advanceSrcLoc loc _ = loc -- Better than nothing
115 %************************************************************************
117 \subsection[SrcLoc-instances]{Instance declarations for various names}
119 %************************************************************************
122 -- SrcLoc is an instance of Ord so that we can sort error messages easily
123 instance Eq SrcLoc where
124 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
128 instance Ord SrcLoc where
131 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
132 cmpSrcLoc (UnhelpfulLoc _) other = LT
134 cmpSrcLoc (ImportedLoc _) (UnhelpfulLoc _) = GT
135 cmpSrcLoc (ImportedLoc m1) (ImportedLoc m2) = m1 `compare` m2
136 cmpSrcLoc (ImportedLoc _) other = LT
138 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
139 = (s1 `compare` s2) `thenCmp` (l1 `compare` l2) `thenCmp` (c1 `compare` c2)
140 cmpSrcLoc (SrcLoc _ _ _) other = GT
142 instance Outputable SrcLoc where
143 ppr (SrcLoc src_path src_line src_col)
144 = getPprStyle $ \ sty ->
145 if userStyle sty || debugStyle sty then
146 hcat [ ftext src_path, char ':',
148 char ':', int src_col
151 hcat [text "{-# LINE ", int src_line, space,
152 char '\"', ftext src_path, text " #-}"]
154 ppr (ImportedLoc mod) = ptext SLIT("Defined in") <+> ftext mod
155 ppr (UnhelpfulLoc s) = ftext s
158 %************************************************************************
160 \subsection[SrcSpan]{Source Spans}
162 %************************************************************************
166 A SrcSpan delimits a portion of a text file. It could be represented
167 by a pair of (line,column) coordinates, but in fact we optimise
168 slightly by using more compact representations for single-line and
169 zero-length spans, both of which are quite common.
171 The end position is defined to be the column *after* the end of the
172 span. That is, a span of (1,1)-(1,2) is one character long, and a
173 span of (1,1)-(1,1) is zero characters long.
176 = SrcSpanOneLine -- a common case: a single line
177 { srcSpanFile :: FastString,
184 { srcSpanFile :: FastString,
185 srcSpanSLine :: !Int,
187 srcSpanELine :: !Int,
192 { srcSpanFile :: FastString,
197 | ImportedSpan FastString -- Module name
199 | UnhelpfulSpan FastString -- Just a general indication
200 -- also used to indicate an empty span
204 -- We want to order SrcSpans first by the start point, then by the end point.
205 instance Ord SrcSpan where
207 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
208 (srcSpanEnd a `compare` srcSpanEnd b)
210 noSrcSpan = UnhelpfulSpan FSLIT("<no location info>")
211 wiredInSrcSpan = UnhelpfulSpan FSLIT("<wired into compiler>")
212 importedSrcSpan = ImportedSpan
214 mkGeneralSrcSpan :: FastString -> SrcSpan
215 mkGeneralSrcSpan = UnhelpfulSpan
217 isGoodSrcSpan SrcSpanOneLine{} = True
218 isGoodSrcSpan SrcSpanMultiLine{} = True
219 isGoodSrcSpan SrcSpanPoint{} = True
220 isGoodSrcSpan _ = False
222 optSrcSpanFileName :: SrcSpan -> Maybe FastString
223 optSrcSpanFileName (SrcSpanOneLine { srcSpanFile = nm }) = Just nm
224 optSrcSpanFileName (SrcSpanMultiLine { srcSpanFile = nm }) = Just nm
225 optSrcSpanFileName (SrcSpanPoint { srcSpanFile = nm}) = Just nm
226 optSrcSpanFileName _ = Nothing
228 isOneLineSpan :: SrcSpan -> Bool
229 -- True if the span is known to straddle more than one line
230 -- By default, it returns False
232 | isGoodSrcSpan s = srcSpanStartLine s == srcSpanEndLine s
235 --------------------------------------------------------
236 -- Don't export these four;
237 -- they panic on Imported, Unhelpful.
238 -- They are for internal use only
239 -- Urk! Some are needed for Lexer.x; see comment in export list
241 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
242 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
243 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
244 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
246 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
247 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
248 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
249 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
251 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
252 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
253 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
254 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
256 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
257 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
258 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
259 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
260 --------------------------------------------------------
262 srcSpanStart (ImportedSpan str) = ImportedLoc str
263 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
264 srcSpanStart s = mkSrcLoc (srcSpanFile s)
268 srcSpanEnd (ImportedSpan str) = ImportedLoc str
269 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
271 mkSrcLoc (srcSpanFile s)
275 srcLocSpan :: SrcLoc -> SrcSpan
276 srcLocSpan (ImportedLoc str) = ImportedSpan str
277 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
278 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
280 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
281 mkSrcSpan (ImportedLoc str) _ = ImportedSpan str
282 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
283 mkSrcSpan _ (ImportedLoc str) = ImportedSpan str
284 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
286 | line1 == line2 = if col1 == col2
287 then SrcSpanPoint file line1 col1
288 else SrcSpanOneLine file line1 col1 col2
289 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
291 line1 = srcLocLine loc1
292 line2 = srcLocLine loc2
293 col1 = srcLocCol loc1
294 col2 = srcLocCol loc2
295 file = srcLocFile loc1
297 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
298 -- Assumes the 'file' part is the same in both
299 combineSrcSpans (ImportedSpan str) _ = ImportedSpan str
300 combineSrcSpans (UnhelpfulSpan str) r = r -- this seems more useful
301 combineSrcSpans _ (ImportedSpan str) = ImportedSpan str
302 combineSrcSpans l (UnhelpfulSpan str) = l
303 combineSrcSpans start end
304 = case line1 `compare` line2 of
305 EQ -> case col1 `compare` col2 of
306 EQ -> SrcSpanPoint file line1 col1
307 LT -> SrcSpanOneLine file line1 col1 col2
308 GT -> SrcSpanOneLine file line1 col2 col1
309 LT -> SrcSpanMultiLine file line1 col1 line2 col2
310 GT -> SrcSpanMultiLine file line2 col2 line1 col1
312 line1 = srcSpanStartLine start
313 col1 = srcSpanStartCol start
314 line2 = srcSpanEndLine end
315 col2 = srcSpanEndCol end
316 file = srcSpanFile start
318 pprDefnLoc :: SrcSpan -> SDoc
319 -- "defined at ..." or "imported from ..."
321 | isGoodSrcSpan loc = ptext SLIT("Defined at") <+> ppr loc
322 | otherwise = ppr loc
324 instance Outputable SrcSpan where
326 = getPprStyle $ \ sty ->
327 if userStyle sty || debugStyle sty then
330 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
331 char '\"', ftext (srcSpanFile span), text " #-}"]
334 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
335 = hcat [ ftext src_path, char ':',
337 char ':', int start_col
339 <> if end_col - start_col <= 1
341 -- for single-character or point spans, we just output the starting
343 else char '-' <> int (end_col-1)
345 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
346 = hcat [ ftext src_path, char ':',
347 parens (int sline <> char ',' <> int scol),
349 parens (int eline <> char ',' <>
350 if ecol == 0 then int ecol else int (ecol-1))
353 pprUserSpan (SrcSpanPoint src_path line col)
354 = hcat [ ftext src_path, char ':',
359 pprUserSpan (ImportedSpan mod) = ptext SLIT("Defined in") <+> ftext mod
360 pprUserSpan (UnhelpfulSpan s) = ftext s
363 %************************************************************************
365 \subsection[Located]{Attaching SrcSpans to things}
367 %************************************************************************
370 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
371 data Located e = L SrcSpan e
373 unLoc :: Located e -> e
376 getLoc :: Located e -> SrcSpan
379 noLoc :: e -> Located e
380 noLoc e = L noSrcSpan e
382 combineLocs :: Located a -> Located b -> SrcSpan
383 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
385 addCLoc :: Located a -> Located b -> c -> Located c
386 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
388 -- not clear whether to add a general Eq instance, but this is useful sometimes:
389 eqLocated :: Eq a => Located a -> Located a -> Bool
390 eqLocated a b = unLoc a == unLoc b
392 -- not clear whether to add a general Eq instance, but this is useful sometimes:
393 cmpLocated :: Ord a => Located a -> Located a -> Ordering
394 cmpLocated a b = unLoc a `compare` unLoc b
396 instance Functor Located where
397 fmap f (L l e) = L l (f e)
399 instance Outputable e => Outputable (Located e) where
400 ppr (L span e) = ppr e
401 -- do we want to dump the span in debugSty mode?