2 % (c) The University of Glasgow, 1992-2006
7 -- The above warning supression flag is a temporary kludge.
8 -- While working on this module you are encouraged to remove it and fix
9 -- any warnings in the module. See
10 -- http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
16 mkSrcLoc, isGoodSrcLoc, mkGeneralSrcLoc,
17 noSrcLoc, -- "I'm sorry, I haven't a clue"
20 importedSrcLoc, -- Unknown place in an interface
21 generatedSrcLoc, -- Code generated within the compiler
22 interactiveSrcLoc, -- Code from an interactive session
24 srcLocFile, -- return the file name part
25 srcLocLine, -- return the line part
26 srcLocCol, -- return the column part
31 wiredInSrcSpan, -- Something wired into the compiler
32 importedSrcSpan, -- Unknown place in an interface
34 isGoodSrcSpan, isOneLineSpan,
35 mkSrcSpan, srcLocSpan,
37 srcSpanStart, srcSpanEnd,
40 -- These are dubious exports, because they crash on some inputs,
41 -- used only in Lexer.x where we are sure what the Span looks like
43 srcSpanStartLine, srcSpanEndLine,
44 srcSpanStartCol, srcSpanEndCol,
46 Located(..), getLoc, unLoc, noLoc, eqLocated, cmpLocated, combineLocs, addCLoc,
47 leftmost_smallest, leftmost_largest, rightmost, spans, isSubspanOf
50 #include "HsVersions.h"
57 %************************************************************************
59 \subsection[SrcLoc-SrcLocations]{Source-location information}
61 %************************************************************************
63 We keep information about the {\em definition} point for each entity;
64 this is the obvious stuff:
67 = SrcLoc FastString -- A precise location (file name)
68 !Int -- line number, begins at 1
69 !Int -- column number, begins at 0
70 -- Don't ask me why lines start at 1 and columns start at
71 -- zero. That's just the way it is, so there. --SDM
73 | ImportedLoc FastString -- Module name
75 | UnhelpfulLoc FastString -- Just a general indication
78 Note that an entity might be imported via more than one route, and
79 there could be more than one ``definition point'' --- in two or more
80 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
83 %************************************************************************
85 \subsection[SrcLoc-access-fns]{Access functions for names}
87 %************************************************************************
91 mkSrcLoc x line col = SrcLoc x line col
92 noSrcLoc = UnhelpfulLoc FSLIT("<no location info>")
93 generatedSrcLoc = UnhelpfulLoc FSLIT("<compiler-generated code>")
94 interactiveSrcLoc = UnhelpfulLoc FSLIT("<interactive session>")
96 mkGeneralSrcLoc :: FastString -> SrcLoc
97 mkGeneralSrcLoc = UnhelpfulLoc
99 importedSrcLoc :: FastString -> SrcLoc
100 importedSrcLoc mod_name = ImportedLoc mod_name
102 isGoodSrcLoc (SrcLoc _ _ _) = True
103 isGoodSrcLoc other = False
105 srcLocFile :: SrcLoc -> FastString
106 srcLocFile (SrcLoc fname _ _) = fname
107 srcLocFile other = FSLIT("<unknown file")
109 srcLocLine :: SrcLoc -> Int
110 srcLocLine (SrcLoc _ l c) = l
111 srcLocLine other = panic "srcLocLine: unknown line"
113 srcLocCol :: SrcLoc -> Int
114 srcLocCol (SrcLoc _ l c) = c
115 srcLocCol other = panic "srcLocCol: unknown col"
117 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
118 advanceSrcLoc (SrcLoc f l c) '\n' = SrcLoc f (l + 1) 0
119 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
120 advanceSrcLoc loc _ = loc -- Better than nothing
123 %************************************************************************
125 \subsection[SrcLoc-instances]{Instance declarations for various names}
127 %************************************************************************
130 -- SrcLoc is an instance of Ord so that we can sort error messages easily
131 instance Eq SrcLoc where
132 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
136 instance Ord SrcLoc where
139 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
140 cmpSrcLoc (UnhelpfulLoc _) other = LT
142 cmpSrcLoc (ImportedLoc _) (UnhelpfulLoc _) = GT
143 cmpSrcLoc (ImportedLoc m1) (ImportedLoc m2) = m1 `compare` m2
144 cmpSrcLoc (ImportedLoc _) other = LT
146 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
147 = (s1 `compare` s2) `thenCmp` (l1 `compare` l2) `thenCmp` (c1 `compare` c2)
148 cmpSrcLoc (SrcLoc _ _ _) other = GT
150 instance Outputable SrcLoc where
151 ppr (SrcLoc src_path src_line src_col)
152 = getPprStyle $ \ sty ->
153 if userStyle sty || debugStyle sty then
154 hcat [ ftext src_path, char ':',
156 char ':', int src_col
159 hcat [text "{-# LINE ", int src_line, space,
160 char '\"', ftext src_path, text " #-}"]
162 ppr (ImportedLoc mod) = ptext SLIT("Defined in") <+> ftext mod
163 ppr (UnhelpfulLoc s) = ftext s
166 %************************************************************************
168 \subsection[SrcSpan]{Source Spans}
170 %************************************************************************
174 A SrcSpan delimits a portion of a text file. It could be represented
175 by a pair of (line,column) coordinates, but in fact we optimise
176 slightly by using more compact representations for single-line and
177 zero-length spans, both of which are quite common.
179 The end position is defined to be the column *after* the end of the
180 span. That is, a span of (1,1)-(1,2) is one character long, and a
181 span of (1,1)-(1,1) is zero characters long.
184 = SrcSpanOneLine -- a common case: a single line
185 { srcSpanFile :: FastString,
192 { srcSpanFile :: FastString,
193 srcSpanSLine :: !Int,
195 srcSpanELine :: !Int,
200 { srcSpanFile :: FastString,
205 | ImportedSpan FastString -- Module name
207 | UnhelpfulSpan FastString -- Just a general indication
208 -- also used to indicate an empty span
212 -- We want to order SrcSpans first by the start point, then by the end point.
213 instance Ord SrcSpan where
215 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
216 (srcSpanEnd a `compare` srcSpanEnd b)
218 noSrcSpan = UnhelpfulSpan FSLIT("<no location info>")
219 wiredInSrcSpan = UnhelpfulSpan FSLIT("<wired into compiler>")
220 importedSrcSpan = ImportedSpan
222 mkGeneralSrcSpan :: FastString -> SrcSpan
223 mkGeneralSrcSpan = UnhelpfulSpan
225 isGoodSrcSpan SrcSpanOneLine{} = True
226 isGoodSrcSpan SrcSpanMultiLine{} = True
227 isGoodSrcSpan SrcSpanPoint{} = True
228 isGoodSrcSpan _ = False
230 optSrcSpanFileName :: SrcSpan -> Maybe FastString
231 optSrcSpanFileName (SrcSpanOneLine { srcSpanFile = nm }) = Just nm
232 optSrcSpanFileName (SrcSpanMultiLine { srcSpanFile = nm }) = Just nm
233 optSrcSpanFileName (SrcSpanPoint { srcSpanFile = nm}) = Just nm
234 optSrcSpanFileName _ = Nothing
236 isOneLineSpan :: SrcSpan -> Bool
237 -- True if the span is known to straddle more than one line
238 -- By default, it returns False
240 | isGoodSrcSpan s = srcSpanStartLine s == srcSpanEndLine s
243 --------------------------------------------------------
244 -- Don't export these four;
245 -- they panic on Imported, Unhelpful.
246 -- They are for internal use only
247 -- Urk! Some are needed for Lexer.x; see comment in export list
249 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
250 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
251 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
252 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
254 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
255 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
256 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
257 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
259 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
260 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
261 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
262 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
264 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
265 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
266 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
267 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
268 --------------------------------------------------------
270 srcSpanStart (ImportedSpan str) = ImportedLoc str
271 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
272 srcSpanStart s = mkSrcLoc (srcSpanFile s)
276 srcSpanEnd (ImportedSpan str) = ImportedLoc str
277 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
279 mkSrcLoc (srcSpanFile s)
283 srcLocSpan :: SrcLoc -> SrcSpan
284 srcLocSpan (ImportedLoc str) = ImportedSpan str
285 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
286 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
288 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
289 mkSrcSpan (ImportedLoc str) _ = ImportedSpan str
290 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
291 mkSrcSpan _ (ImportedLoc str) = ImportedSpan str
292 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
294 | line1 == line2 = if col1 == col2
295 then SrcSpanPoint file line1 col1
296 else SrcSpanOneLine file line1 col1 col2
297 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
299 line1 = srcLocLine loc1
300 line2 = srcLocLine loc2
301 col1 = srcLocCol loc1
302 col2 = srcLocCol loc2
303 file = srcLocFile loc1
305 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
306 -- Assumes the 'file' part is the same in both
307 combineSrcSpans (ImportedSpan str) _ = ImportedSpan str
308 combineSrcSpans (UnhelpfulSpan str) r = r -- this seems more useful
309 combineSrcSpans _ (ImportedSpan str) = ImportedSpan str
310 combineSrcSpans l (UnhelpfulSpan str) = l
311 combineSrcSpans start end
312 = case line1 `compare` line2 of
313 EQ -> case col1 `compare` col2 of
314 EQ -> SrcSpanPoint file line1 col1
315 LT -> SrcSpanOneLine file line1 col1 col2
316 GT -> SrcSpanOneLine file line1 col2 col1
317 LT -> SrcSpanMultiLine file line1 col1 line2 col2
318 GT -> SrcSpanMultiLine file line2 col2 line1 col1
320 line1 = srcSpanStartLine start
321 col1 = srcSpanStartCol start
322 line2 = srcSpanEndLine end
323 col2 = srcSpanEndCol end
324 file = srcSpanFile start
326 pprDefnLoc :: SrcSpan -> SDoc
327 -- "defined at ..." or "imported from ..."
329 | isGoodSrcSpan loc = ptext SLIT("Defined at") <+> ppr loc
330 | otherwise = ppr loc
332 instance Outputable SrcSpan where
334 = getPprStyle $ \ sty ->
335 if userStyle sty || debugStyle sty then
338 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
339 char '\"', ftext (srcSpanFile span), text " #-}"]
342 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
343 = hcat [ ftext src_path, char ':',
345 char ':', int start_col
347 <> if end_col - start_col <= 1
349 -- for single-character or point spans, we just output the starting
351 else char '-' <> int (end_col-1)
353 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
354 = hcat [ ftext src_path, char ':',
355 parens (int sline <> char ',' <> int scol),
357 parens (int eline <> char ',' <>
358 if ecol == 0 then int ecol else int (ecol-1))
361 pprUserSpan (SrcSpanPoint src_path line col)
362 = hcat [ ftext src_path, char ':',
367 pprUserSpan (ImportedSpan mod) = ptext SLIT("Defined in") <+> ftext mod
368 pprUserSpan (UnhelpfulSpan s) = ftext s
371 %************************************************************************
373 \subsection[Located]{Attaching SrcSpans to things}
375 %************************************************************************
378 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
379 data Located e = L SrcSpan e
381 unLoc :: Located e -> e
384 getLoc :: Located e -> SrcSpan
387 noLoc :: e -> Located e
388 noLoc e = L noSrcSpan e
390 combineLocs :: Located a -> Located b -> SrcSpan
391 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
393 addCLoc :: Located a -> Located b -> c -> Located c
394 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
396 -- not clear whether to add a general Eq instance, but this is useful sometimes:
397 eqLocated :: Eq a => Located a -> Located a -> Bool
398 eqLocated a b = unLoc a == unLoc b
400 -- not clear whether to add a general Eq instance, but this is useful sometimes:
401 cmpLocated :: Ord a => Located a -> Located a -> Ordering
402 cmpLocated a b = unLoc a `compare` unLoc b
404 instance Functor Located where
405 fmap f (L l e) = L l (f e)
407 instance Outputable e => Outputable (Located e) where
408 ppr (L span e) = ppr e
409 -- do we want to dump the span in debugSty mode?
413 %************************************************************************
415 \subsection{Manipulating SrcSpans}
417 %************************************************************************
420 leftmost_smallest, leftmost_largest, rightmost :: SrcSpan -> SrcSpan -> Ordering
421 rightmost = flip compare
422 leftmost_smallest = compare
423 leftmost_largest a b = (srcSpanStart a `compare` srcSpanStart b)
425 (srcSpanEnd b `compare` srcSpanEnd a)
428 spans :: SrcSpan -> (Int,Int) -> Bool
429 spans span (l,c) = srcSpanStart span <= loc && loc <= srcSpanEnd span
430 where loc = mkSrcLoc (srcSpanFile span) l c
432 isSubspanOf :: SrcSpan -> SrcSpan -> Bool
433 isSubspanOf src parent
434 | optSrcSpanFileName parent /= optSrcSpanFileName src = False
435 | otherwise = srcSpanStart parent <= srcSpanStart src &&
436 srcSpanEnd parent >= srcSpanEnd src