[project @ 1996-03-19 08:58:34 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 Ubiq
24
25 import PprStyle         ( PprStyle(..) )
26 import Pretty
27 \end{code}
28
29 %************************************************************************
30 %*                                                                      *
31 \subsection[SrcLoc-SrcLocations]{Source-location information}
32 %*                                                                      *
33 %************************************************************************
34
35 We keep information about the {\em definition} point for each entity;
36 this is the obvious stuff:
37 \begin{code}
38 data SrcLoc
39   = SrcLoc      FAST_STRING     -- source file name
40                 FAST_STRING     -- line number in source file
41   | SrcLoc2     FAST_STRING     -- same, but w/ an Int line#
42                 FAST_INT
43 \end{code}
44
45 Note that an entity might be imported via more than one route, and
46 there could be more than one ``definition point'' --- in two or more
47 \tr{.hi} files.  We deemed it probably-unworthwhile to cater for this
48 rare case.
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection[SrcLoc-access-fns]{Access functions for names}
53 %*                                                                      *
54 %************************************************************************
55
56 Things to make 'em:
57 \begin{code}
58 mkSrcLoc            = SrcLoc
59 mkSrcLoc2 x IBOX(y) = SrcLoc2 x y
60 mkUnknownSrcLoc     = SrcLoc SLIT("<unknown>") SLIT("<unknown>")
61 mkBuiltinSrcLoc     = SrcLoc SLIT("<built-into-the-compiler>") SLIT("<none>")
62 mkGeneratedSrcLoc   = SrcLoc SLIT("<compiler-generated-code>") SLIT("<none>")
63
64 unpackSrcLoc (SrcLoc  src_file src_line) = (src_file, src_line)
65 unpackSrcLoc (SrcLoc2 src_file src_line) = (src_file, _PK_ (show IBOX(src_line)))
66 \end{code}
67
68 %************************************************************************
69 %*                                                                      *
70 \subsection[SrcLoc-instances]{Instance declarations for various names}
71 %*                                                                      *
72 %************************************************************************
73
74 \begin{code}
75 instance Outputable SrcLoc where
76     ppr PprForUser (SrcLoc src_file src_line)
77       = ppBesides [ ppChar '"', ppPStr src_file, ppPStr SLIT("\", line "), ppPStr src_line ]
78
79     ppr sty (SrcLoc src_file src_line)
80       = ppBesides [ppPStr SLIT("{-# LINE "), ppPStr src_line, ppSP,
81                    ppChar '"', ppPStr src_file, ppPStr SLIT("\" #-}")]
82
83     ppr sty (SrcLoc2 src_file src_line)
84       = ppr sty (SrcLoc src_file (_PK_ (show IBOX(src_line))))
85 \end{code}