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
73 -- A precise source file span
74 | SrcSpan FastString -- file name
75 FastInt -- beginning line
76 FastInt -- beginning column
80 | UnhelpfulSrcSpan FastString -- Just a general indication
84 Note that an entity might be imported via more than one route, and
85 there could be more than one ``definition point'' --- in two or more
86 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
89 %************************************************************************
91 \subsection[SrcLoc-access-fns]{Access functions for names}
93 %************************************************************************
97 mkSrcLoc x line col = SrcLoc x line col
98 noSrcLoc = UnhelpfulLoc FSLIT("<no location info>")
99 generatedSrcLoc = UnhelpfulLoc FSLIT("<compiler-generated code>")
100 wiredInSrcLoc = UnhelpfulLoc FSLIT("<wired into compiler>")
101 interactiveSrcLoc = UnhelpfulLoc FSLIT("<interactive session>")
103 mkGeneralSrcLoc :: FastString -> SrcLoc
104 mkGeneralSrcLoc = UnhelpfulLoc
106 importedSrcLoc :: String -> SrcLoc
107 importedSrcLoc mod_name = ImportedLoc mod_name
109 isGoodSrcLoc (SrcLoc _ _ _) = True
110 isGoodSrcLoc other = False
112 srcLocFile :: SrcLoc -> FastString
113 srcLocFile (SrcLoc fname _ _) = fname
114 srcLocFile other = FSLIT("<unknown file")
116 srcLocLine :: SrcLoc -> Int
117 srcLocLine (SrcLoc _ l c) = l
118 srcLocLine other = panic "srcLocLine: unknown line"
120 srcLocCol :: SrcLoc -> Int
121 srcLocCol (SrcLoc _ l c) = c
122 srcLocCol other = panic "srcLocCol: unknown col"
124 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
125 advanceSrcLoc (SrcLoc f l c) '\t' = SrcLoc f l (tab c)
126 advanceSrcLoc (SrcLoc f l c) '\n' = SrcLoc f (l + 1) 0
127 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
128 advanceSrcLoc loc _ = loc -- Better than nothing
130 -- Advance to the next tab stop. Tabs are at column positions 0, 8, 16, etc.
132 tab c = (c `quot` 8 + 1) * 8
135 %************************************************************************
137 \subsection[SrcLoc-instances]{Instance declarations for various names}
139 %************************************************************************
142 -- SrcLoc is an instance of Ord so that we can sort error messages easily
143 instance Eq SrcLoc where
144 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
148 instance Ord SrcLoc where
151 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
152 cmpSrcLoc (UnhelpfulLoc _) other = LT
154 cmpSrcLoc (ImportedLoc _) (UnhelpfulLoc _) = GT
155 cmpSrcLoc (ImportedLoc m1) (ImportedLoc m2) = m1 `compare` m2
156 cmpSrcLoc (ImportedLoc _) other = LT
158 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
159 = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2) `thenCmp` (c1 `cmpline` c2)
161 l1 `cmpline` l2 | l1 < l2 = LT
164 cmpSrcLoc (SrcLoc _ _ _) other = GT
166 instance Outputable SrcLoc where
167 ppr (SrcLoc src_path src_line src_col)
168 = getPprStyle $ \ sty ->
169 if userStyle sty || debugStyle sty then
170 hcat [ ftext src_path, char ':',
172 char ':', int src_col
175 hcat [text "{-# LINE ", int src_line, space,
176 char '\"', ftext src_path, text " #-}"]
178 ppr (ImportedLoc mod) = ptext SLIT("Imported from") <+> quotes (text mod)
179 ppr (UnhelpfulLoc s) = ftext s
182 %************************************************************************
184 \subsection[SrcSpan]{Source Spans}
186 %************************************************************************
190 A SrcSpan delimits a portion of a text file. It could be represented
191 by a pair of (line,column) coordinates, but in fact we optimise
192 slightly by using more compact representations for single-line and
193 zero-length spans, both of which are quite common.
195 The end position is defined to be the column *after* the end of the
196 span. That is, a span of (1,1)-(1,2) is one character long, and a
197 span of (1,1)-(1,1) is zero characters long.
200 = SrcSpanOneLine -- a common case: a single line
201 { srcSpanFile :: FastString,
208 { srcSpanFile :: FastString,
209 srcSpanSLine :: !Int,
211 srcSpanELine :: !Int,
216 { srcSpanFile :: FastString,
221 | ImportedSpan String -- Module name
223 | UnhelpfulSpan FastString -- Just a general indication
224 -- also used to indicate an empty span
228 -- We want to order SrcSpans first by the start point, then by the end point.
229 instance Ord SrcSpan where
231 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
232 (srcSpanEnd a `compare` srcSpanEnd b)
234 noSrcSpan = UnhelpfulSpan FSLIT("<no location info>")
236 mkGeneralSrcSpan :: FastString -> SrcSpan
237 mkGeneralSrcSpan = UnhelpfulSpan
239 isGoodSrcSpan SrcSpanOneLine{} = True
240 isGoodSrcSpan SrcSpanMultiLine{} = True
241 isGoodSrcSpan SrcSpanPoint{} = True
242 isGoodSrcSpan _ = False
244 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
245 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
246 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
247 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
249 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
250 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
251 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
252 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
254 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
255 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
256 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
257 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
259 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
260 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
261 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
262 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
264 srcSpanStart (ImportedSpan str) = ImportedLoc str
265 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
267 mkSrcLoc (srcSpanFile s)
271 srcSpanEnd (ImportedSpan str) = ImportedLoc str
272 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
274 mkSrcLoc (srcSpanFile s)
278 srcLocSpan :: SrcLoc -> SrcSpan
279 srcLocSpan (ImportedLoc str) = ImportedSpan str
280 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
281 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
283 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
284 mkSrcSpan (ImportedLoc str) _ = ImportedSpan str
285 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
286 mkSrcSpan _ (ImportedLoc str) = ImportedSpan str
287 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
289 | line1 == line2 = if col1 == col2
290 then SrcSpanPoint file line1 col1
291 else SrcSpanOneLine file line1 col1 col2
292 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
294 line1 = srcLocLine loc1
295 line2 = srcLocLine loc2
296 col1 = srcLocCol loc1
297 col2 = srcLocCol loc2
298 file = srcLocFile loc1
300 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
301 combineSrcSpans (ImportedSpan str) _ = ImportedSpan str
302 combineSrcSpans (UnhelpfulSpan str) r = r -- this seems more useful
303 combineSrcSpans _ (ImportedSpan str) = ImportedSpan str
304 combineSrcSpans l (UnhelpfulSpan str) = l
305 combineSrcSpans start end
306 | line1 == line2 = if col1 == col2
307 then SrcSpanPoint file line1 col1
308 else SrcSpanOneLine file line1 col1 col2
309 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
311 line1 = srcSpanStartLine start
312 line2 = srcSpanEndLine end
313 col1 = srcSpanStartCol start
314 col2 = srcSpanEndCol end
315 file = srcSpanFile start
317 instance Outputable SrcSpan where
319 = getPprStyle $ \ sty ->
320 if userStyle sty || debugStyle sty then
323 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
324 char '\"', ftext (srcSpanFile span), text " #-}"]
327 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
328 = hcat [ ftext src_path, char ':',
330 char ':', int start_col
332 <> if end_col - start_col <= 1
334 -- for single-character or point spans, we just output the starting
336 else char '-' <> int (end_col-1)
338 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
339 = hcat [ ftext src_path, char ':',
340 parens (int sline <> char ',' <> int scol),
342 parens (int eline <> char ',' <>
343 if ecol == 0 then int ecol else int (ecol-1))
346 pprUserSpan (SrcSpanPoint src_path line col)
347 = hcat [ ftext src_path, char ':',
352 pprUserSpan (ImportedSpan mod) = ptext SLIT("Imported from") <+> quotes (text mod)
353 pprUserSpan (UnhelpfulSpan s) = ftext s
356 %************************************************************************
358 \subsection[Located]{Attaching SrcSpans to things}
360 %************************************************************************
363 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
364 data Located e = L SrcSpan e
366 unLoc :: Located e -> e
369 getLoc :: Located e -> SrcSpan
372 noLoc :: e -> Located e
373 noLoc e = L noSrcSpan e
375 combineLocs :: Located a -> Located b -> SrcSpan
376 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
378 addCLoc :: Located a -> Located b -> c -> Located c
379 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
381 -- not clear whether to add a general Eq instance, but this is useful sometimes:
382 eqLocated :: Eq a => Located a -> Located a -> Bool
383 eqLocated a b = unLoc a == unLoc b
385 -- not clear whether to add a general Eq instance, but this is useful sometimes:
386 cmpLocated :: Ord a => Located a -> Located a -> Ordering
387 cmpLocated a b = unLoc a `compare` unLoc b
389 instance Functor Located where
390 fmap f (L l e) = L l (f e)
392 instance Outputable e => Outputable (Located e) where
393 ppr (L span e) = ppr e
394 -- do we want to dump the span in debugSty mode?