2 % (c) The University of Glasgow, 1992-2003
4 %************************************************************************
6 \section[SrcLoc]{The @SrcLoc@ type}
8 %************************************************************************
14 mkSrcLoc, isGoodSrcLoc, mkGeneralSrcLoc,
15 noSrcLoc, -- "I'm sorry, I haven't a clue"
18 importedSrcLoc, -- Unknown place in an interface
19 wiredInSrcLoc, -- Something wired into the compiler
20 generatedSrcLoc, -- Code generated within the compiler
21 interactiveSrcLoc, -- Code from an interactive session
23 srcLocFile, -- return the file name part
24 srcLocLine, -- return the line part
25 srcLocCol, -- return the column part
32 mkSrcSpan, srcLocSpan,
35 srcSpanStartLine, srcSpanEndLine,
36 srcSpanStartCol, srcSpanEndCol,
37 srcSpanStart, srcSpanEnd,
39 Located(..), getLoc, unLoc, noLoc, eqLocated, cmpLocated, combineLocs, addCLoc
42 #include "HsVersions.h"
44 import Util ( thenCmp )
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 String -- 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 wiredInSrcLoc = UnhelpfulLoc FSLIT("<wired into compiler>")
87 interactiveSrcLoc = UnhelpfulLoc FSLIT("<interactive session>")
89 mkGeneralSrcLoc :: FastString -> SrcLoc
90 mkGeneralSrcLoc = UnhelpfulLoc
92 importedSrcLoc :: String -> SrcLoc
93 importedSrcLoc mod_name = ImportedLoc mod_name
95 isGoodSrcLoc (SrcLoc _ _ _) = True
96 isGoodSrcLoc other = False
98 srcLocFile :: SrcLoc -> FastString
99 srcLocFile (SrcLoc fname _ _) = fname
100 srcLocFile other = FSLIT("<unknown file")
102 srcLocLine :: SrcLoc -> Int
103 srcLocLine (SrcLoc _ l c) = l
104 srcLocLine other = panic "srcLocLine: unknown line"
106 srcLocCol :: SrcLoc -> Int
107 srcLocCol (SrcLoc _ l c) = c
108 srcLocCol other = panic "srcLocCol: unknown col"
110 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
111 advanceSrcLoc (SrcLoc f l c) '\n' = SrcLoc f (l + 1) 0
112 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
113 advanceSrcLoc loc _ = loc -- Better than nothing
116 %************************************************************************
118 \subsection[SrcLoc-instances]{Instance declarations for various names}
120 %************************************************************************
123 -- SrcLoc is an instance of Ord so that we can sort error messages easily
124 instance Eq SrcLoc where
125 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
129 instance Ord SrcLoc where
132 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
133 cmpSrcLoc (UnhelpfulLoc _) other = LT
135 cmpSrcLoc (ImportedLoc _) (UnhelpfulLoc _) = GT
136 cmpSrcLoc (ImportedLoc m1) (ImportedLoc m2) = m1 `compare` m2
137 cmpSrcLoc (ImportedLoc _) other = LT
139 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
140 = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2) `thenCmp` (c1 `cmpline` c2)
142 l1 `cmpline` l2 | l1 < l2 = LT
145 cmpSrcLoc (SrcLoc _ _ _) other = GT
147 instance Outputable SrcLoc where
148 ppr (SrcLoc src_path src_line src_col)
149 = getPprStyle $ \ sty ->
150 if userStyle sty || debugStyle sty then
151 hcat [ ftext src_path, char ':',
153 char ':', int src_col
156 hcat [text "{-# LINE ", int src_line, space,
157 char '\"', ftext src_path, text " #-}"]
159 ppr (ImportedLoc mod) = ptext SLIT("Imported from") <+> text mod
160 ppr (UnhelpfulLoc s) = ftext s
163 %************************************************************************
165 \subsection[SrcSpan]{Source Spans}
167 %************************************************************************
171 A SrcSpan delimits a portion of a text file. It could be represented
172 by a pair of (line,column) coordinates, but in fact we optimise
173 slightly by using more compact representations for single-line and
174 zero-length spans, both of which are quite common.
176 The end position is defined to be the column *after* the end of the
177 span. That is, a span of (1,1)-(1,2) is one character long, and a
178 span of (1,1)-(1,1) is zero characters long.
181 = SrcSpanOneLine -- a common case: a single line
182 { srcSpanFile :: FastString,
189 { srcSpanFile :: FastString,
190 srcSpanSLine :: !Int,
192 srcSpanELine :: !Int,
197 { srcSpanFile :: FastString,
202 | ImportedSpan String -- Module name
204 | UnhelpfulSpan FastString -- Just a general indication
205 -- also used to indicate an empty span
209 -- We want to order SrcSpans first by the start point, then by the end point.
210 instance Ord SrcSpan where
212 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
213 (srcSpanEnd a `compare` srcSpanEnd b)
215 noSrcSpan = UnhelpfulSpan FSLIT("<no location info>")
217 mkGeneralSrcSpan :: FastString -> SrcSpan
218 mkGeneralSrcSpan = UnhelpfulSpan
220 isGoodSrcSpan SrcSpanOneLine{} = True
221 isGoodSrcSpan SrcSpanMultiLine{} = True
222 isGoodSrcSpan SrcSpanPoint{} = True
223 isGoodSrcSpan _ = False
225 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
226 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
227 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
228 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
230 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
231 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
232 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
233 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
235 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
236 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
237 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
238 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
240 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
241 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
242 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
243 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
245 srcSpanStart (ImportedSpan str) = ImportedLoc str
246 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
248 mkSrcLoc (srcSpanFile s)
252 srcSpanEnd (ImportedSpan str) = ImportedLoc str
253 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
255 mkSrcLoc (srcSpanFile s)
259 srcLocSpan :: SrcLoc -> SrcSpan
260 srcLocSpan (ImportedLoc str) = ImportedSpan str
261 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
262 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
264 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
265 mkSrcSpan (ImportedLoc str) _ = ImportedSpan str
266 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
267 mkSrcSpan _ (ImportedLoc str) = ImportedSpan str
268 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
270 | line1 == line2 = if col1 == col2
271 then SrcSpanPoint file line1 col1
272 else SrcSpanOneLine file line1 col1 col2
273 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
275 line1 = srcLocLine loc1
276 line2 = srcLocLine loc2
277 col1 = srcLocCol loc1
278 col2 = srcLocCol loc2
279 file = srcLocFile loc1
281 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
282 -- Assumes the 'file' part is the same in both
283 combineSrcSpans (ImportedSpan str) _ = ImportedSpan str
284 combineSrcSpans (UnhelpfulSpan str) r = r -- this seems more useful
285 combineSrcSpans _ (ImportedSpan str) = ImportedSpan str
286 combineSrcSpans l (UnhelpfulSpan str) = l
287 combineSrcSpans start end
288 = case line1 `compare` line2 of
289 EQ -> case col1 `compare` col2 of
290 EQ -> SrcSpanPoint file line1 col1
291 LT -> SrcSpanOneLine file line1 col1 col2
292 GT -> SrcSpanOneLine file line1 col2 col1
293 LT -> SrcSpanMultiLine file line1 col1 line2 col2
294 GT -> SrcSpanMultiLine file line2 col2 line1 col1
296 line1 = srcSpanStartLine start
297 col1 = srcSpanStartCol start
298 line2 = srcSpanEndLine end
299 col2 = srcSpanEndCol end
300 file = srcSpanFile start
302 pprDefnLoc :: SrcLoc -> SDoc
303 -- "defined at ..." or "imported from ..."
305 | isGoodSrcLoc loc = ptext SLIT("Defined at") <+> ppr loc
306 | otherwise = ppr loc
308 instance Outputable SrcSpan where
310 = getPprStyle $ \ sty ->
311 if userStyle sty || debugStyle sty then
314 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
315 char '\"', ftext (srcSpanFile span), text " #-}"]
318 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
319 = hcat [ ftext src_path, char ':',
321 char ':', int start_col
323 <> if end_col - start_col <= 1
325 -- for single-character or point spans, we just output the starting
327 else char '-' <> int (end_col-1)
329 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
330 = hcat [ ftext src_path, char ':',
331 parens (int sline <> char ',' <> int scol),
333 parens (int eline <> char ',' <>
334 if ecol == 0 then int ecol else int (ecol-1))
337 pprUserSpan (SrcSpanPoint src_path line col)
338 = hcat [ ftext src_path, char ':',
343 pprUserSpan (ImportedSpan mod) = ptext SLIT("Imported from") <+> quotes (text mod)
344 pprUserSpan (UnhelpfulSpan s) = ftext s
347 %************************************************************************
349 \subsection[Located]{Attaching SrcSpans to things}
351 %************************************************************************
354 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
355 data Located e = L SrcSpan e
357 unLoc :: Located e -> e
360 getLoc :: Located e -> SrcSpan
363 noLoc :: e -> Located e
364 noLoc e = L noSrcSpan e
366 combineLocs :: Located a -> Located b -> SrcSpan
367 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
369 addCLoc :: Located a -> Located b -> c -> Located c
370 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
372 -- not clear whether to add a general Eq instance, but this is useful sometimes:
373 eqLocated :: Eq a => Located a -> Located a -> Bool
374 eqLocated a b = unLoc a == unLoc b
376 -- not clear whether to add a general Eq instance, but this is useful sometimes:
377 cmpLocated :: Ord a => Located a -> Located a -> Ordering
378 cmpLocated a b = unLoc a `compare` unLoc b
380 instance Functor Located where
381 fmap f (L l e) = L l (f e)
383 instance Outputable e => Outputable (Located e) where
384 ppr (L span e) = ppr e
385 -- do we want to dump the span in debugSty mode?