2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %************************************************************************
6 \section[SrcLoc]{The @SrcLoc@ type}
8 %************************************************************************
15 noSrcLoc, isNoSrcLoc, -- "I'm sorry, I haven't a clue"
17 mkIfaceSrcLoc, -- Unknown place in an interface
18 -- (this one can die eventually ToDo)
20 mkBuiltinSrcLoc, -- Something wired into the compiler
22 mkGeneratedSrcLoc, -- Code generated within the compiler
24 incSrcLine, replaceSrcLine,
26 srcLocFile, -- return the file name part.
27 srcLocLine -- return the line part.
30 #include "HsVersions.h"
32 import Util ( thenCmp )
34 import FastString ( unpackFS )
36 import GlaExts ( Int(..), (+#) )
39 %************************************************************************
41 \subsection[SrcLoc-SrcLocations]{Source-location information}
43 %************************************************************************
45 We keep information about the {\em definition} point for each entity;
46 this is the obvious stuff:
51 | SrcLoc FAST_STRING -- A precise location (file name)
54 | UnhelpfulSrcLoc FAST_STRING -- Just a general indication
57 Note that an entity might be imported via more than one route, and
58 there could be more than one ``definition point'' --- in two or more
59 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
62 %************************************************************************
64 \subsection[SrcLoc-access-fns]{Access functions for names}
66 %************************************************************************
71 mkSrcLoc x y = SrcLoc x (iUnbox y)
73 mkIfaceSrcLoc = UnhelpfulSrcLoc SLIT("<an interface file>")
74 mkBuiltinSrcLoc = UnhelpfulSrcLoc SLIT("<built-into-the-compiler>")
75 mkGeneratedSrcLoc = UnhelpfulSrcLoc SLIT("<compiler-generated-code>")
77 isNoSrcLoc NoSrcLoc = True
78 isNoSrcLoc other = False
80 srcLocFile :: SrcLoc -> FAST_STRING
81 srcLocFile (SrcLoc fname _) = fname
83 srcLocLine :: SrcLoc -> FastInt
84 srcLocLine (SrcLoc _ l) = l
86 incSrcLine :: SrcLoc -> SrcLoc
87 incSrcLine (SrcLoc s l) = SrcLoc s (l +# 1#)
90 replaceSrcLine :: SrcLoc -> FastInt -> SrcLoc
91 replaceSrcLine (SrcLoc s _) l = SrcLoc s l
94 %************************************************************************
96 \subsection[SrcLoc-instances]{Instance declarations for various names}
98 %************************************************************************
101 -- SrcLoc is an instance of Ord so that we can sort error messages easily
102 instance Eq SrcLoc where
103 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
107 instance Ord SrcLoc where
110 cmpSrcLoc NoSrcLoc NoSrcLoc = EQ
111 cmpSrcLoc NoSrcLoc other = LT
113 cmpSrcLoc (UnhelpfulSrcLoc s1) (UnhelpfulSrcLoc s2) = s1 `compare` s2
114 cmpSrcLoc (UnhelpfulSrcLoc s1) other = GT
116 cmpSrcLoc (SrcLoc s1 l1) NoSrcLoc = GT
117 cmpSrcLoc (SrcLoc s1 l1) (UnhelpfulSrcLoc _) = LT
118 cmpSrcLoc (SrcLoc s1 l1) (SrcLoc s2 l2) = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2)
120 l1 `cmpline` l2 | l1 <# l2 = LT
124 instance Outputable SrcLoc where
125 ppr (SrcLoc src_path src_line)
126 = getPprStyle $ \ sty ->
127 if userStyle sty then
128 hcat [ text src_file, char ':', int (iBox src_line) ]
130 if debugStyle sty then
131 hcat [ ptext src_path, char ':', int (iBox src_line) ]
133 hcat [text "{-# LINE ", int (iBox src_line), space,
134 char '\"', ptext src_path, text " #-}"]
136 src_file = unpackFS src_path -- Leave the directory prefix intact,
137 -- so emacs can find the file
139 ppr (UnhelpfulSrcLoc s) = ptext s
141 ppr NoSrcLoc = text "<NoSrcLoc>"