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