2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
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
28 #include "HsVersions.h"
30 import Util ( thenCmp )
35 import GLAEXTS ( (+#), quotInt# )
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)
52 | ImportedLoc String -- Module name
54 | UnhelpfulLoc FastString -- Just a general indication
60 -- A precise source file span
61 | SrcSpan FastString -- file name
62 FastInt -- beginning line
63 FastInt -- beginning column
67 | UnhelpfulSrcSpan FastString -- Just a general indication
71 Note that an entity might be imported via more than one route, and
72 there could be more than one ``definition point'' --- in two or more
73 \tr{.hi} files. We deemed it probably-unworthwhile to cater for this
76 %************************************************************************
78 \subsection[SrcLoc-access-fns]{Access functions for names}
80 %************************************************************************
84 mkSrcLoc x line col = SrcLoc x (iUnbox line) (iUnbox col)
85 noSrcLoc = UnhelpfulLoc FSLIT("<no locn>")
86 generatedSrcLoc = UnhelpfulLoc FSLIT("<compiler-generated code>")
87 wiredInSrcLoc = UnhelpfulLoc FSLIT("<wired into compiler>")
88 interactiveSrcLoc = UnhelpfulLoc FSLIT("<interactive session>")
90 mkGeneralSrcLoc :: FastString -> SrcLoc
91 mkGeneralSrcLoc = UnhelpfulLoc
93 importedSrcLoc :: String -> SrcLoc
94 importedSrcLoc mod_name = ImportedLoc mod_name
96 isGoodSrcLoc (SrcLoc _ _ _) = True
97 isGoodSrcLoc other = False
99 srcLocFile :: SrcLoc -> FastString
100 srcLocFile (SrcLoc fname _ _) = fname
101 srcLocFile other = FSLIT("<unknown file")
103 srcLocLine :: SrcLoc -> Int
104 srcLocLine (SrcLoc _ l c) = iBox l
105 srcLocLine other = panic "srcLocLine: unknown line"
107 srcLocCol :: SrcLoc -> Int
108 srcLocCol (SrcLoc _ l c) = iBox c
109 srcLocCol other = panic "srcLocCol: unknown col"
111 advanceSrcLoc :: SrcLoc -> Char -> SrcLoc
112 advanceSrcLoc (SrcLoc f l c) '\t' = SrcLoc f l (tab c)
113 advanceSrcLoc (SrcLoc f l c) '\n' = SrcLoc f (l +# 1#) 0#
114 advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c +# 1#)
115 advanceSrcLoc loc _ = loc -- Better than nothing
117 -- Advance to the next tab stop. Tabs are at column positions 0, 8, 16, etc.
118 tab :: FastInt -> FastInt
119 tab c = (c `quotInt#` 8# +# 1#) *# 8#
122 %************************************************************************
124 \subsection[SrcLoc-instances]{Instance declarations for various names}
126 %************************************************************************
129 -- SrcLoc is an instance of Ord so that we can sort error messages easily
130 instance Eq SrcLoc where
131 loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
135 instance Ord SrcLoc where
138 cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
139 cmpSrcLoc (UnhelpfulLoc _) other = LT
141 cmpSrcLoc (ImportedLoc _) (UnhelpfulLoc _) = GT
142 cmpSrcLoc (ImportedLoc m1) (ImportedLoc m2) = m1 `compare` m2
143 cmpSrcLoc (ImportedLoc _) other = LT
145 cmpSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
146 = (s1 `compare` s2) `thenCmp` (l1 `cmpline` l2) `thenCmp` (c1 `cmpline` c2)
148 l1 `cmpline` l2 | l1 <# l2 = LT
151 cmpSrcLoc (SrcLoc _ _ _) other = GT
153 instance Outputable SrcLoc where
154 ppr (SrcLoc src_path src_line src_col)
155 = getPprStyle $ \ sty ->
156 if userStyle sty || debugStyle sty then
157 hcat [ ftext src_path, char ':',
159 {- TODO: char ':', int (iBox src_col) -}
162 hcat [text "{-# LINE ", int (iBox src_line), space,
163 char '\"', ftext src_path, text " #-}"]
165 ppr (ImportedLoc mod) = ptext SLIT("Imported from") <+> quotes (text mod)
166 ppr (UnhelpfulLoc s) = ftext s