[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / basicTypes / SrcLoc.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[SrcLoc]{The @SrcLoc@ type}
7 %*                                                                      *
8 %************************************************************************
9
10 \begin{code}
11 #include "HsVersions.h"
12
13 module SrcLoc (
14         SrcLoc,                 -- abstract
15
16         mkSrcLoc, mkSrcLoc2,    -- the usual
17         mkUnknownSrcLoc,        -- "I'm sorry, I haven't a clue"
18         mkBuiltinSrcLoc,        -- something wired into the compiler
19         mkGeneratedSrcLoc,      -- code generated within the compiler
20         unpackSrcLoc
21     ) where
22
23 import Outputable
24 import Pretty
25 import Util
26 \end{code}
27
28 %************************************************************************
29 %*                                                                      *
30 \subsection[SrcLoc-SrcLocations]{Source-location information}
31 %*                                                                      *
32 %************************************************************************
33
34 We keep information about the {\em definition} point for each entity;
35 this is the obvious stuff:
36 \begin{code}
37 data SrcLoc
38   = SrcLoc      FAST_STRING     -- source file name
39                 FAST_STRING     -- line number in source file
40   | SrcLoc2     FAST_STRING     -- same, but w/ an Int line#
41                 FAST_INT
42 \end{code}
43
44 Note that an entity might be imported via more than one route, and
45 there could be more than one ``definition point'' --- in two or more
46 \tr{.hi} files.  We deemed it probably-unworthwhile to cater for this
47 rare case.
48
49 %************************************************************************
50 %*                                                                      *
51 \subsection[SrcLoc-access-fns]{Access functions for names}
52 %*                                                                      *
53 %************************************************************************
54
55 Things to make 'em:
56 \begin{code}
57 mkSrcLoc            = SrcLoc
58 mkSrcLoc2 x IBOX(y) = SrcLoc2 x y
59 mkUnknownSrcLoc     = SrcLoc SLIT("<unknown>") SLIT("<unknown>")
60 mkBuiltinSrcLoc     = SrcLoc SLIT("<built-into-the-compiler>") SLIT("<none>")
61 mkGeneratedSrcLoc   = SrcLoc SLIT("<compiler-generated-code>") SLIT("<none>")
62
63 unpackSrcLoc (SrcLoc  src_file src_line) = (src_file, src_line)
64 unpackSrcLoc (SrcLoc2 src_file src_line) = (src_file, _PK_ (show IBOX(src_line)))
65 \end{code}
66
67 %************************************************************************
68 %*                                                                      *
69 \subsection[SrcLoc-instances]{Instance declarations for various names}
70 %*                                                                      *
71 %************************************************************************
72
73 \begin{code}
74 instance Outputable SrcLoc where
75     ppr PprForUser (SrcLoc src_file src_line)
76       = ppBesides [ ppChar '"', ppPStr src_file, ppPStr SLIT("\", line "), ppPStr src_line ]
77
78     ppr sty (SrcLoc src_file src_line)
79       = ppBesides [ppPStr SLIT("{-# LINE "), ppPStr src_line, ppSP,
80                    ppChar '"', ppPStr src_file, ppPStr SLIT("\" #-}")]
81
82     ppr sty (SrcLoc2 src_file src_line)
83       = ppr sty (SrcLoc src_file (_PK_ (show IBOX(src_line))))
84 \end{code}