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 )
35 import GlaExts ( Int(..), (+#) )
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:
50 | SrcLoc FAST_STRING -- A precise location (file name)
53 | UnhelpfulSrcLoc FAST_STRING -- 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 %************************************************************************
70 mkSrcLoc x IBOX(y) = SrcLoc x y
72 mkIfaceSrcLoc = UnhelpfulSrcLoc SLIT("<an interface file>")
73 mkBuiltinSrcLoc = UnhelpfulSrcLoc SLIT("<built-into-the-compiler>")
74 mkGeneratedSrcLoc = UnhelpfulSrcLoc SLIT("<compiler-generated-code>")
76 isNoSrcLoc NoSrcLoc = True
77 isNoSrcLoc other = False
79 srcLocFile :: SrcLoc -> FAST_STRING
80 srcLocFile (SrcLoc fname _) = fname
82 srcLocLine :: SrcLoc -> FAST_INT
83 srcLocLine (SrcLoc _ l) = l
85 incSrcLine :: SrcLoc -> SrcLoc
86 incSrcLine (SrcLoc s l) = SrcLoc s (l +# 1#)
89 replaceSrcLine :: SrcLoc -> FAST_INT -> SrcLoc
90 replaceSrcLine (SrcLoc s _) l = SrcLoc s l
93 %************************************************************************
95 \subsection[SrcLoc-instances]{Instance declarations for various names}
97 %************************************************************************
100 -- SrcLoc is an instance of Ord so that we can sort error messages easily
101 instance Eq SrcLoc where
102 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
106 instance Ord SrcLoc where
109 cmpSrcLoc NoSrcLoc NoSrcLoc = EQ
110 cmpSrcLoc NoSrcLoc other = LT
112 cmpSrcLoc (UnhelpfulSrcLoc s1) (UnhelpfulSrcLoc s2) = s1 `compare` s2
113 cmpSrcLoc (UnhelpfulSrcLoc s1) other = GT
115 cmpSrcLoc (SrcLoc s1 l1) NoSrcLoc = GT
116 cmpSrcLoc (SrcLoc s1 l1) (UnhelpfulSrcLoc _) = LT
117 cmpSrcLoc (SrcLoc s1 l1) (SrcLoc s2 l2) = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2)
119 l1 `cmpline` l2 | l1 <# l2 = LT
123 instance Outputable SrcLoc where
124 ppr (SrcLoc src_path src_line)
125 = getPprStyle $ \ sty ->
126 if userStyle sty then
127 hcat [ text src_file, char ':', int IBOX(src_line) ]
129 if debugStyle sty then
130 hcat [ ptext src_path, char ':', int IBOX(src_line) ]
132 hcat [text "{-# LINE ", int IBOX(src_line), space,
133 char '\"', ptext src_path, text " #-}"]
135 src_file = unpackFS src_path -- Leave the directory prefix intact,
136 -- so emacs can find the file
138 ppr (UnhelpfulSrcLoc s) = ptext s
140 ppr NoSrcLoc = text "<NoSrcLoc>"