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 )
34 import GlaExts ( (+#) )
37 %************************************************************************
39 \subsection[SrcLoc-SrcLocations]{Source-location information}
41 %************************************************************************
43 We keep information about the {\em definition} point for each entity;
44 this is the obvious stuff:
47 = SrcLoc FastString -- A precise location (file name)
50 | UnhelpfulSrcLoc FastString -- Just a general indication
55 Note that an entity might be imported via more than one route, and
56 there could be more than one ``definition point'' --- in two or more
57 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
60 %************************************************************************
62 \subsection[SrcLoc-access-fns]{Access functions for names}
64 %************************************************************************
68 mkSrcLoc x y = SrcLoc x (iUnbox y)
70 importedSrcLoc = UnhelpfulSrcLoc FSLIT("<imported>")
71 builtinSrcLoc = UnhelpfulSrcLoc FSLIT("<built-into-the-compiler>")
72 generatedSrcLoc = UnhelpfulSrcLoc FSLIT("<compiler-generated-code>")
74 isGoodSrcLoc (SrcLoc _ _) = True
75 isGoodSrcLoc other = False
77 srcLocFile :: SrcLoc -> FastString
78 srcLocFile (SrcLoc fname _) = fname
80 srcLocLine :: SrcLoc -> FastInt
81 srcLocLine (SrcLoc _ l) = l
83 incSrcLine :: SrcLoc -> SrcLoc
84 incSrcLine (SrcLoc s l) = SrcLoc s (l +# 1#)
87 replaceSrcLine :: SrcLoc -> FastInt -> SrcLoc
88 replaceSrcLine (SrcLoc s _) l = SrcLoc s l
91 %************************************************************************
93 \subsection[SrcLoc-instances]{Instance declarations for various names}
95 %************************************************************************
98 -- SrcLoc is an instance of Ord so that we can sort error messages easily
99 instance Eq SrcLoc where
100 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
104 instance Ord SrcLoc where
107 cmpSrcLoc NoSrcLoc NoSrcLoc = EQ
108 cmpSrcLoc NoSrcLoc other = LT
110 cmpSrcLoc (UnhelpfulSrcLoc s1) (UnhelpfulSrcLoc s2) = s1 `compare` s2
111 cmpSrcLoc (UnhelpfulSrcLoc s1) other = GT
113 cmpSrcLoc (SrcLoc s1 l1) NoSrcLoc = GT
114 cmpSrcLoc (SrcLoc s1 l1) (UnhelpfulSrcLoc _) = LT
115 cmpSrcLoc (SrcLoc s1 l1) (SrcLoc s2 l2) = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2)
117 l1 `cmpline` l2 | l1 <# l2 = LT
121 instance Outputable SrcLoc where
122 ppr (SrcLoc src_path src_line)
123 = getPprStyle $ \ sty ->
124 if userStyle sty || debugStyle sty then
125 hcat [ ftext src_path, char ':', int (iBox src_line) ]
127 hcat [text "{-# LINE ", int (iBox src_line), space,
128 char '\"', ftext src_path, text " #-}"]
130 src_file = unpackFS src_path -- Leave the directory prefix intact,
131 -- so emacs can find the file
133 ppr (UnhelpfulSrcLoc s) = ftext s
134 ppr NoSrcLoc = ptext SLIT("<No locn>")