2 % (c) The University of Glasgow, 1992-2006
9 mkSrcLoc, isGoodSrcLoc, mkGeneralSrcLoc,
10 noSrcLoc, -- "I'm sorry, I haven't a clue"
13 generatedSrcLoc, -- Code generated within the compiler
14 interactiveSrcLoc, -- Code from an interactive session
16 srcLocFile, -- return the file name part
17 srcLocLine, -- return the line part
18 srcLocCol, -- return the column part
23 wiredInSrcSpan, -- Something wired into the compiler
25 isGoodSrcSpan, isOneLineSpan,
26 mkSrcSpan, srcLocSpan,
28 srcSpanStart, srcSpanEnd,
31 -- These are dubious exports, because they crash on some inputs,
32 -- used only in Lexer.x where we are sure what the Span looks like
34 srcSpanStartLine, srcSpanEndLine,
35 srcSpanStartCol, srcSpanEndCol,
37 Located(..), getLoc, unLoc, noLoc, eqLocated, cmpLocated, combineLocs, addCLoc,
38 leftmost_smallest, leftmost_largest, rightmost, spans, isSubspanOf
41 #include "HsVersions.h"
48 %************************************************************************
50 \subsection[SrcLoc-SrcLocations]{Source-location information}
52 %************************************************************************
54 We keep information about the {\em definition} point for each entity;
55 this is the obvious stuff:
58 = SrcLoc FastString -- A precise location (file name)
59 !Int -- line number, begins at 1
60 !Int -- column number, begins at 0
61 -- Don't ask me why lines start at 1 and columns start at
62 -- zero. That's just the way it is, so there. --SDM
64 | UnhelpfulLoc FastString -- Just a general indication
67 %************************************************************************
69 \subsection[SrcLoc-access-fns]{Access functions for names}
71 %************************************************************************
75 mkSrcLoc :: FastString -> Int -> Int -> SrcLoc
76 mkSrcLoc x line col = SrcLoc x line col
78 noSrcLoc, generatedSrcLoc, interactiveSrcLoc :: SrcLoc
79 noSrcLoc = UnhelpfulLoc FSLIT("<no location info>")
80 generatedSrcLoc = UnhelpfulLoc FSLIT("<compiler-generated code>")
81 interactiveSrcLoc = UnhelpfulLoc FSLIT("<interactive session>")
83 mkGeneralSrcLoc :: FastString -> SrcLoc
84 mkGeneralSrcLoc = UnhelpfulLoc
86 isGoodSrcLoc :: SrcLoc -> Bool
87 isGoodSrcLoc (SrcLoc _ _ _) = True
88 isGoodSrcLoc _other = False
90 srcLocFile :: SrcLoc -> FastString
91 srcLocFile (SrcLoc fname _ _) = fname
92 srcLocFile _other = FSLIT("<unknown file")
94 srcLocLine :: SrcLoc -> Int
95 srcLocLine (SrcLoc _ l _) = l
96 srcLocLine _other = panic "srcLocLine: unknown line"
98 srcLocCol :: SrcLoc -> Int
99 srcLocCol (SrcLoc _ _ c) = c
100 srcLocCol _other = panic "srcLocCol: unknown col"
102 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
103 advanceSrcLoc (SrcLoc f l _) '\n' = SrcLoc f (l + 1) 0
104 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
105 advanceSrcLoc loc _ = loc -- Better than nothing
108 %************************************************************************
110 \subsection[SrcLoc-instances]{Instance declarations for various names}
112 %************************************************************************
115 -- SrcLoc is an instance of Ord so that we can sort error messages easily
116 instance Eq SrcLoc where
117 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
121 instance Ord SrcLoc where
124 cmpSrcLoc :: SrcLoc -> SrcLoc -> Ordering
125 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
126 cmpSrcLoc (UnhelpfulLoc _) _other = LT
128 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
129 = (s1 `compare` s2) `thenCmp` (l1 `compare` l2) `thenCmp` (c1 `compare` c2)
130 cmpSrcLoc (SrcLoc _ _ _) _other = GT
132 instance Outputable SrcLoc where
133 ppr (SrcLoc src_path src_line src_col)
134 = getPprStyle $ \ sty ->
135 if userStyle sty || debugStyle sty then
136 hcat [ ftext src_path, char ':',
138 char ':', int src_col
141 hcat [text "{-# LINE ", int src_line, space,
142 char '\"', ftext src_path, text " #-}"]
144 ppr (UnhelpfulLoc s) = ftext s
147 %************************************************************************
149 \subsection[SrcSpan]{Source Spans}
151 %************************************************************************
155 A SrcSpan delimits a portion of a text file. It could be represented
156 by a pair of (line,column) coordinates, but in fact we optimise
157 slightly by using more compact representations for single-line and
158 zero-length spans, both of which are quite common.
160 The end position is defined to be the column *after* the end of the
161 span. That is, a span of (1,1)-(1,2) is one character long, and a
162 span of (1,1)-(1,1) is zero characters long.
165 = SrcSpanOneLine -- a common case: a single line
166 { srcSpanFile :: FastString,
173 { srcSpanFile :: FastString,
174 srcSpanSLine :: !Int,
176 srcSpanELine :: !Int,
181 { srcSpanFile :: FastString,
186 | UnhelpfulSpan FastString -- Just a general indication
187 -- also used to indicate an empty span
191 -- We want to order SrcSpans first by the start point, then by the end point.
192 instance Ord SrcSpan where
194 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
195 (srcSpanEnd a `compare` srcSpanEnd b)
197 noSrcSpan, wiredInSrcSpan :: SrcSpan
198 noSrcSpan = UnhelpfulSpan FSLIT("<no location info>")
199 wiredInSrcSpan = UnhelpfulSpan FSLIT("<wired into compiler>")
201 mkGeneralSrcSpan :: FastString -> SrcSpan
202 mkGeneralSrcSpan = UnhelpfulSpan
204 isGoodSrcSpan :: SrcSpan -> Bool
205 isGoodSrcSpan SrcSpanOneLine{} = True
206 isGoodSrcSpan SrcSpanMultiLine{} = True
207 isGoodSrcSpan SrcSpanPoint{} = True
208 isGoodSrcSpan _ = False
210 optSrcSpanFileName :: SrcSpan -> Maybe FastString
211 optSrcSpanFileName (SrcSpanOneLine { srcSpanFile = nm }) = Just nm
212 optSrcSpanFileName (SrcSpanMultiLine { srcSpanFile = nm }) = Just nm
213 optSrcSpanFileName (SrcSpanPoint { srcSpanFile = nm}) = Just nm
214 optSrcSpanFileName _ = Nothing
216 isOneLineSpan :: SrcSpan -> Bool
217 -- True if the span is known to straddle more than one line
218 -- By default, it returns False
220 | isGoodSrcSpan s = srcSpanStartLine s == srcSpanEndLine s
223 --------------------------------------------------------
224 -- Don't export these four;
225 -- they panic on Unhelpful.
226 -- They are for internal use only
227 -- Urk! Some are needed for Lexer.x; see comment in export list
229 srcSpanStartLine, srcSpanEndLine, srcSpanStartCol, srcSpanEndCol
232 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
233 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
234 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
235 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
237 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
238 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
239 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
240 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
242 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
243 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
244 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
245 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
247 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
248 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
249 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
250 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
251 --------------------------------------------------------
253 srcSpanStart, srcSpanEnd :: SrcSpan -> SrcLoc
255 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
256 srcSpanStart s = mkSrcLoc (srcSpanFile s)
260 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
262 mkSrcLoc (srcSpanFile s)
266 srcLocSpan :: SrcLoc -> SrcSpan
267 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
268 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
270 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
271 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
272 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
274 | line1 == line2 = if col1 == col2
275 then SrcSpanPoint file line1 col1
276 else SrcSpanOneLine file line1 col1 col2
277 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
279 line1 = srcLocLine loc1
280 line2 = srcLocLine loc2
281 col1 = srcLocCol loc1
282 col2 = srcLocCol loc2
283 file = srcLocFile loc1
285 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
286 -- Assumes the 'file' part is the same in both
287 combineSrcSpans (UnhelpfulSpan _) r = r -- this seems more useful
288 combineSrcSpans l (UnhelpfulSpan _) = l
289 combineSrcSpans start end
290 = case line1 `compare` line2 of
291 EQ -> case col1 `compare` col2 of
292 EQ -> SrcSpanPoint file line1 col1
293 LT -> SrcSpanOneLine file line1 col1 col2
294 GT -> SrcSpanOneLine file line1 col2 col1
295 LT -> SrcSpanMultiLine file line1 col1 line2 col2
296 GT -> SrcSpanMultiLine file line2 col2 line1 col1
298 line1 = srcSpanStartLine start
299 col1 = srcSpanStartCol start
300 line2 = srcSpanEndLine end
301 col2 = srcSpanEndCol end
302 file = srcSpanFile start
304 pprDefnLoc :: SrcSpan -> SDoc
307 | isGoodSrcSpan loc = ptext SLIT("Defined at") <+> ppr loc
308 | otherwise = ppr loc
310 instance Outputable SrcSpan where
312 = getPprStyle $ \ sty ->
313 if userStyle sty || debugStyle sty then
316 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
317 char '\"', ftext (srcSpanFile span), text " #-}"]
320 pprUserSpan :: SrcSpan -> SDoc
321 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
322 = hcat [ ftext src_path, char ':',
324 char ':', int start_col
326 <> if end_col - start_col <= 1
328 -- for single-character or point spans, we just output the starting
330 else char '-' <> int (end_col-1)
332 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
333 = hcat [ ftext src_path, char ':',
334 parens (int sline <> char ',' <> int scol),
336 parens (int eline <> char ',' <>
337 if ecol == 0 then int ecol else int (ecol-1))
340 pprUserSpan (SrcSpanPoint src_path line col)
341 = hcat [ ftext src_path, char ':',
346 pprUserSpan (UnhelpfulSpan s) = ftext s
349 %************************************************************************
351 \subsection[Located]{Attaching SrcSpans to things}
353 %************************************************************************
356 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
357 data Located e = L SrcSpan e
359 unLoc :: Located e -> e
362 getLoc :: Located e -> SrcSpan
365 noLoc :: e -> Located e
366 noLoc e = L noSrcSpan e
368 combineLocs :: Located a -> Located b -> SrcSpan
369 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
371 addCLoc :: Located a -> Located b -> c -> Located c
372 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
374 -- not clear whether to add a general Eq instance, but this is useful sometimes:
375 eqLocated :: Eq a => Located a -> Located a -> Bool
376 eqLocated a b = unLoc a == unLoc b
378 -- not clear whether to add a general Eq instance, but this is useful sometimes:
379 cmpLocated :: Ord a => Located a -> Located a -> Ordering
380 cmpLocated a b = unLoc a `compare` unLoc b
382 instance Functor Located where
383 fmap f (L l e) = L l (f e)
385 instance Outputable e => Outputable (Located e) where
387 -- do we want to dump the span in debugSty mode?
391 %************************************************************************
393 \subsection{Manipulating SrcSpans}
395 %************************************************************************
398 leftmost_smallest, leftmost_largest, rightmost :: SrcSpan -> SrcSpan -> Ordering
399 rightmost = flip compare
400 leftmost_smallest = compare
401 leftmost_largest a b = (srcSpanStart a `compare` srcSpanStart b)
403 (srcSpanEnd b `compare` srcSpanEnd a)
406 spans :: SrcSpan -> (Int,Int) -> Bool
407 spans span (l,c) = srcSpanStart span <= loc && loc <= srcSpanEnd span
408 where loc = mkSrcLoc (srcSpanFile span) l c
410 isSubspanOf :: SrcSpan -> SrcSpan -> Bool
411 isSubspanOf src parent
412 | optSrcSpanFileName parent /= optSrcSpanFileName src = False
413 | otherwise = srcSpanStart parent <= srcSpanStart src &&
414 srcSpanEnd parent >= srcSpanEnd src