[project @ 1999-06-01 16:40:41 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / SrcLoc.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[SrcLoc]{The @SrcLoc@ type}
7 %*                                                                      *
8 %************************************************************************
9
10 \begin{code}
11 module SrcLoc (
12         SrcLoc,                 -- Abstract
13
14         mkSrcLoc,
15         noSrcLoc, isNoSrcLoc,   -- "I'm sorry, I haven't a clue"
16
17         mkIfaceSrcLoc,          -- Unknown place in an interface
18                                 -- (this one can die eventually ToDo)
19
20         mkBuiltinSrcLoc,        -- Something wired into the compiler
21
22         mkGeneratedSrcLoc,      -- Code generated within the compiler
23
24         incSrcLine, replaceSrcLine,
25         
26         srcLocFile,             -- return the file name part.
27         srcLocLine              -- return the line part.
28     ) where
29
30 #include "HsVersions.h"
31
32 import Util             ( thenCmp )
33 import Outputable
34 import FastString       ( unpackFS )
35 import GlaExts          ( Int(..), (+#) )
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection[SrcLoc-SrcLocations]{Source-location information}
41 %*                                                                      *
42 %************************************************************************
43
44 We keep information about the {\em definition} point for each entity;
45 this is the obvious stuff:
46 \begin{code}
47 data SrcLoc
48   = NoSrcLoc
49
50   | SrcLoc      FAST_STRING     -- A precise location (file name)
51                 FAST_INT
52
53   | UnhelpfulSrcLoc FAST_STRING -- Just a general indication
54 \end{code}
55
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
59 rare case.
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection[SrcLoc-access-fns]{Access functions for names}
64 %*                                                                      *
65 %************************************************************************
66
67 Things to make 'em:
68 \begin{code}
69 noSrcLoc            = NoSrcLoc
70 mkSrcLoc x IBOX(y)  = SrcLoc x y
71
72 mkIfaceSrcLoc       = UnhelpfulSrcLoc SLIT("<an interface file>")
73 mkBuiltinSrcLoc     = UnhelpfulSrcLoc SLIT("<built-into-the-compiler>")
74 mkGeneratedSrcLoc   = UnhelpfulSrcLoc SLIT("<compiler-generated-code>")
75
76 isNoSrcLoc NoSrcLoc = True
77 isNoSrcLoc other    = False
78
79 srcLocFile :: SrcLoc -> FAST_STRING
80 srcLocFile (SrcLoc fname _) = fname
81
82 srcLocLine :: SrcLoc -> FAST_INT
83 srcLocLine (SrcLoc _ l) = l
84
85 incSrcLine :: SrcLoc -> SrcLoc
86 incSrcLine (SrcLoc s l) = SrcLoc s (l +# 1#)
87 incSrcLine loc          = loc
88
89 replaceSrcLine :: SrcLoc -> FAST_INT -> SrcLoc
90 replaceSrcLine (SrcLoc s _) l = SrcLoc s l
91 \end{code}
92
93 %************************************************************************
94 %*                                                                      *
95 \subsection[SrcLoc-instances]{Instance declarations for various names}
96 %*                                                                      *
97 %************************************************************************
98
99 \begin{code}
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
103                    EQ    -> True
104                    other -> False
105
106 instance Ord SrcLoc where
107   compare = cmpSrcLoc
108
109 cmpSrcLoc NoSrcLoc NoSrcLoc = EQ
110 cmpSrcLoc NoSrcLoc other    = LT
111
112 cmpSrcLoc (UnhelpfulSrcLoc s1) (UnhelpfulSrcLoc s2) = s1 `compare` s2
113 cmpSrcLoc (UnhelpfulSrcLoc s1) other                = GT
114
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)
118                                              where
119                                                 l1 `cmpline` l2 | l1 <#  l2 = LT
120                                                                 | l1 ==# l2 = EQ
121                                                                 | otherwise = GT 
122                                           
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) ]
128         else
129         if debugStyle sty then
130            hcat [ ptext src_path, char ':', int IBOX(src_line) ]
131         else
132            hcat [text "{-# LINE ", int IBOX(src_line), space,
133                  char '\"', ptext src_path, text " #-}"]
134       where
135         src_file = remove_directory_prefix (unpackFS src_path)
136
137         remove_directory_prefix path = case break (== '/') path of
138                                           (filename, [])           -> filename
139                                           (prefix,   slash : rest) -> ASSERT( slash == '/' )
140                                                                       remove_directory_prefix rest
141
142     ppr (UnhelpfulSrcLoc s) = ptext s
143
144     ppr NoSrcLoc = text "<NoSrcLoc>"
145 \end{code}