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) '\t' = SrcLoc f l (tab c)
112 advanceSrcLoc (SrcLoc f l c) '\n' = SrcLoc f (l + 1) 0
113 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
114 advanceSrcLoc loc _ = loc -- Better than nothing
116 -- Advance to the next tab stop. Tabs are at column positions 0, 8, 16, etc.
118 tab c = (c `quot` 8 + 1) * 8
121 %************************************************************************
123 \subsection[SrcLoc-instances]{Instance declarations for various names}
125 %************************************************************************
128 -- SrcLoc is an instance of Ord so that we can sort error messages easily
129 instance Eq SrcLoc where
130 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
134 instance Ord SrcLoc where
137 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
138 cmpSrcLoc (UnhelpfulLoc _) other = LT
140 cmpSrcLoc (ImportedLoc _) (UnhelpfulLoc _) = GT
141 cmpSrcLoc (ImportedLoc m1) (ImportedLoc m2) = m1 `compare` m2
142 cmpSrcLoc (ImportedLoc _) other = LT
144 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
145 = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2) `thenCmp` (c1 `cmpline` c2)
147 l1 `cmpline` l2 | l1 < l2 = LT
150 cmpSrcLoc (SrcLoc _ _ _) other = GT
152 instance Outputable SrcLoc where
153 ppr (SrcLoc src_path src_line src_col)
154 = getPprStyle $ \ sty ->
155 if userStyle sty || debugStyle sty then
156 hcat [ ftext src_path, char ':',
158 char ':', int src_col
161 hcat [text "{-# LINE ", int src_line, space,
162 char '\"', ftext src_path, text " #-}"]
164 ppr (ImportedLoc mod) = ptext SLIT("Imported from") <+> quotes (text mod)
165 ppr (UnhelpfulLoc s) = ftext s
168 %************************************************************************
170 \subsection[SrcSpan]{Source Spans}
172 %************************************************************************
176 A SrcSpan delimits a portion of a text file. It could be represented
177 by a pair of (line,column) coordinates, but in fact we optimise
178 slightly by using more compact representations for single-line and
179 zero-length spans, both of which are quite common.
181 The end position is defined to be the column *after* the end of the
182 span. That is, a span of (1,1)-(1,2) is one character long, and a
183 span of (1,1)-(1,1) is zero characters long.
186 = SrcSpanOneLine -- a common case: a single line
187 { srcSpanFile :: FastString,
194 { srcSpanFile :: FastString,
195 srcSpanSLine :: !Int,
197 srcSpanELine :: !Int,
202 { srcSpanFile :: FastString,
207 | ImportedSpan String -- Module name
209 | UnhelpfulSpan FastString -- Just a general indication
210 -- also used to indicate an empty span
214 -- We want to order SrcSpans first by the start point, then by the end point.
215 instance Ord SrcSpan where
217 (srcSpanStart a `compare` srcSpanStart b) `thenCmp`
218 (srcSpanEnd a `compare` srcSpanEnd b)
220 noSrcSpan = UnhelpfulSpan FSLIT("<no location info>")
222 mkGeneralSrcSpan :: FastString -> SrcSpan
223 mkGeneralSrcSpan = UnhelpfulSpan
225 isGoodSrcSpan SrcSpanOneLine{} = True
226 isGoodSrcSpan SrcSpanMultiLine{} = True
227 isGoodSrcSpan SrcSpanPoint{} = True
228 isGoodSrcSpan _ = False
230 srcSpanStartLine SrcSpanOneLine{ srcSpanLine=l } = l
231 srcSpanStartLine SrcSpanMultiLine{ srcSpanSLine=l } = l
232 srcSpanStartLine SrcSpanPoint{ srcSpanLine=l } = l
233 srcSpanStartLine _ = panic "SrcLoc.srcSpanStartLine"
235 srcSpanEndLine SrcSpanOneLine{ srcSpanLine=l } = l
236 srcSpanEndLine SrcSpanMultiLine{ srcSpanELine=l } = l
237 srcSpanEndLine SrcSpanPoint{ srcSpanLine=l } = l
238 srcSpanEndLine _ = panic "SrcLoc.srcSpanEndLine"
240 srcSpanStartCol SrcSpanOneLine{ srcSpanSCol=l } = l
241 srcSpanStartCol SrcSpanMultiLine{ srcSpanSCol=l } = l
242 srcSpanStartCol SrcSpanPoint{ srcSpanCol=l } = l
243 srcSpanStartCol _ = panic "SrcLoc.srcSpanStartCol"
245 srcSpanEndCol SrcSpanOneLine{ srcSpanECol=c } = c
246 srcSpanEndCol SrcSpanMultiLine{ srcSpanECol=c } = c
247 srcSpanEndCol SrcSpanPoint{ srcSpanCol=c } = c
248 srcSpanEndCol _ = panic "SrcLoc.srcSpanEndCol"
250 srcSpanStart (ImportedSpan str) = ImportedLoc str
251 srcSpanStart (UnhelpfulSpan str) = UnhelpfulLoc str
253 mkSrcLoc (srcSpanFile s)
257 srcSpanEnd (ImportedSpan str) = ImportedLoc str
258 srcSpanEnd (UnhelpfulSpan str) = UnhelpfulLoc str
260 mkSrcLoc (srcSpanFile s)
264 srcLocSpan :: SrcLoc -> SrcSpan
265 srcLocSpan (ImportedLoc str) = ImportedSpan str
266 srcLocSpan (UnhelpfulLoc str) = UnhelpfulSpan str
267 srcLocSpan (SrcLoc file line col) = SrcSpanPoint file line col
269 mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
270 mkSrcSpan (ImportedLoc str) _ = ImportedSpan str
271 mkSrcSpan (UnhelpfulLoc str) _ = UnhelpfulSpan str
272 mkSrcSpan _ (ImportedLoc str) = ImportedSpan str
273 mkSrcSpan _ (UnhelpfulLoc str) = UnhelpfulSpan str
275 | line1 == line2 = if col1 == col2
276 then SrcSpanPoint file line1 col1
277 else SrcSpanOneLine file line1 col1 col2
278 | otherwise = SrcSpanMultiLine file line1 col1 line2 col2
280 line1 = srcLocLine loc1
281 line2 = srcLocLine loc2
282 col1 = srcLocCol loc1
283 col2 = srcLocCol loc2
284 file = srcLocFile loc1
286 combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
287 -- Assumes the 'file' part is the same in both
288 combineSrcSpans (ImportedSpan str) _ = ImportedSpan str
289 combineSrcSpans (UnhelpfulSpan str) r = r -- this seems more useful
290 combineSrcSpans _ (ImportedSpan str) = ImportedSpan str
291 combineSrcSpans l (UnhelpfulSpan str) = l
292 combineSrcSpans start end
293 = case line1 `compare` line2 of
294 EQ -> case col1 `compare` col2 of
295 EQ -> SrcSpanPoint file line1 col1
296 LT -> SrcSpanOneLine file line1 col1 col2
297 GT -> SrcSpanOneLine file line1 col2 col1
298 LT -> SrcSpanMultiLine file line1 col1 line2 col2
299 GT -> SrcSpanMultiLine file line2 col2 line1 col1
301 line1 = srcSpanStartLine start
302 col1 = srcSpanStartCol start
303 line2 = srcSpanEndLine end
304 col2 = srcSpanEndCol end
305 file = srcSpanFile start
307 instance Outputable SrcSpan where
309 = getPprStyle $ \ sty ->
310 if userStyle sty || debugStyle sty then
313 hcat [text "{-# LINE ", int (srcSpanStartLine span), space,
314 char '\"', ftext (srcSpanFile span), text " #-}"]
317 pprUserSpan (SrcSpanOneLine src_path line start_col end_col)
318 = hcat [ ftext src_path, char ':',
320 char ':', int start_col
322 <> if end_col - start_col <= 1
324 -- for single-character or point spans, we just output the starting
326 else char '-' <> int (end_col-1)
328 pprUserSpan (SrcSpanMultiLine src_path sline scol eline ecol)
329 = hcat [ ftext src_path, char ':',
330 parens (int sline <> char ',' <> int scol),
332 parens (int eline <> char ',' <>
333 if ecol == 0 then int ecol else int (ecol-1))
336 pprUserSpan (SrcSpanPoint src_path line col)
337 = hcat [ ftext src_path, char ':',
342 pprUserSpan (ImportedSpan mod) = ptext SLIT("Imported from") <+> quotes (text mod)
343 pprUserSpan (UnhelpfulSpan s) = ftext s
346 %************************************************************************
348 \subsection[Located]{Attaching SrcSpans to things}
350 %************************************************************************
353 -- | We attach SrcSpans to lots of things, so let's have a datatype for it.
354 data Located e = L SrcSpan e
356 unLoc :: Located e -> e
359 getLoc :: Located e -> SrcSpan
362 noLoc :: e -> Located e
363 noLoc e = L noSrcSpan e
365 combineLocs :: Located a -> Located b -> SrcSpan
366 combineLocs a b = combineSrcSpans (getLoc a) (getLoc b)
368 addCLoc :: Located a -> Located b -> c -> Located c
369 addCLoc a b c = L (combineSrcSpans (getLoc a) (getLoc b)) c
371 -- not clear whether to add a general Eq instance, but this is useful sometimes:
372 eqLocated :: Eq a => Located a -> Located a -> Bool
373 eqLocated a b = unLoc a == unLoc b
375 -- not clear whether to add a general Eq instance, but this is useful sometimes:
376 cmpLocated :: Ord a => Located a -> Located a -> Ordering
377 cmpLocated a b = unLoc a `compare` unLoc b
379 instance Functor Located where
380 fmap f (L l e) = L l (f e)
382 instance Outputable e => Outputable (Located e) where
383 ppr (L span e) = ppr e
384 -- do we want to dump the span in debugSty mode?