2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %************************************************************************
6 \section[SrcLoc]{The @SrcLoc@ type}
8 %************************************************************************
14 mkSrcLoc, isGoodSrcLoc,
15 noSrcLoc, -- "I'm sorry, I haven't a clue"
17 importedSrcLoc, -- Unknown place in an interface
18 builtinSrcLoc, -- Something wired into the compiler
19 generatedSrcLoc, -- Code generated within the compiler
21 incSrcLine, replaceSrcLine,
23 srcLocFile, -- return the file name part.
24 srcLocLine -- return the line part.
27 #include "HsVersions.h"
29 import Util ( thenCmp )
31 import FastString ( unpackFS )
35 import GLAEXTS ( (+#) )
38 %************************************************************************
40 \subsection[SrcLoc-SrcLocations]{Source-location information}
42 %************************************************************************
44 We keep information about the {\em definition} point for each entity;
45 this is the obvious stuff:
48 = SrcLoc FastString -- A precise location (file name)
51 | UnhelpfulSrcLoc FastString -- Just a general indication
56 Note that an entity might be imported via more than one route, and
57 there could be more than one ``definition point'' --- in two or more
58 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
61 %************************************************************************
63 \subsection[SrcLoc-access-fns]{Access functions for names}
65 %************************************************************************
69 mkSrcLoc x y = SrcLoc x (iUnbox y)
71 importedSrcLoc = UnhelpfulSrcLoc FSLIT("<imported>")
72 builtinSrcLoc = UnhelpfulSrcLoc FSLIT("<built-into-the-compiler>")
73 generatedSrcLoc = UnhelpfulSrcLoc FSLIT("<compiler-generated-code>")
75 isGoodSrcLoc (SrcLoc _ _) = True
76 isGoodSrcLoc other = False
78 srcLocFile :: SrcLoc -> FastString
79 srcLocFile (SrcLoc fname _) = fname
81 srcLocLine :: SrcLoc -> FastInt
82 srcLocLine (SrcLoc _ l) = l
84 incSrcLine :: SrcLoc -> SrcLoc
85 incSrcLine (SrcLoc s l) = SrcLoc s (l +# 1#)
88 replaceSrcLine :: SrcLoc -> FastInt -> SrcLoc
89 replaceSrcLine (SrcLoc s _) l = SrcLoc s l
92 %************************************************************************
94 \subsection[SrcLoc-instances]{Instance declarations for various names}
96 %************************************************************************
99 -- SrcLoc is an instance of Ord so that we can sort error messages easily
100 instance Eq SrcLoc where
101 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
105 instance Ord SrcLoc where
108 cmpSrcLoc NoSrcLoc NoSrcLoc = EQ
109 cmpSrcLoc NoSrcLoc other = LT
111 cmpSrcLoc (UnhelpfulSrcLoc s1) (UnhelpfulSrcLoc s2) = s1 `compare` s2
112 cmpSrcLoc (UnhelpfulSrcLoc s1) other = GT
114 cmpSrcLoc (SrcLoc s1 l1) NoSrcLoc = GT
115 cmpSrcLoc (SrcLoc s1 l1) (UnhelpfulSrcLoc _) = LT
116 cmpSrcLoc (SrcLoc s1 l1) (SrcLoc s2 l2) = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2)
118 l1 `cmpline` l2 | l1 <# l2 = LT
122 instance Outputable SrcLoc where
123 ppr (SrcLoc src_path src_line)
124 = getPprStyle $ \ sty ->
125 if userStyle sty || debugStyle sty then
126 hcat [ ftext src_path, char ':', int (iBox src_line) ]
128 hcat [text "{-# LINE ", int (iBox src_line), space,
129 char '\"', ftext src_path, text " #-}"]
131 src_file = unpackFS src_path -- Leave the directory prefix intact,
132 -- so emacs can find the file
134 ppr (UnhelpfulSrcLoc s) = ftext s
135 ppr NoSrcLoc = ptext SLIT("<No locn>")