Revert "Generate Typeable info at definition sites"
[ghc.git] / compiler / main / DriverPhases.hs
1 {-# LANGUAGE CPP #-}
2
3 -----------------------------------------------------------------------------
4 -- $Id: DriverPhases.hs,v 1.38 2005/05/17 11:01:59 simonmar Exp $
5 --
6 -- GHC Driver
7 --
8 -- (c) The University of Glasgow 2002
9 --
10 -----------------------------------------------------------------------------
11
12 module DriverPhases (
13 HscSource(..), isHsBoot, hscSourceString,
14 Phase(..),
15 happensBefore, eqPhase, anyHsc, isStopLn,
16 startPhase,
17 phaseInputExt,
18
19 isHaskellishSuffix,
20 isHaskellSrcSuffix,
21 isObjectSuffix,
22 isCishSuffix,
23 isDynLibSuffix,
24 isHaskellUserSrcSuffix,
25 isSourceSuffix,
26
27 isHaskellishFilename,
28 isHaskellSrcFilename,
29 isObjectFilename,
30 isCishFilename,
31 isDynLibFilename,
32 isHaskellUserSrcFilename,
33 isSourceFilename
34 ) where
35
36 #include "HsVersions.h"
37
38 import {-# SOURCE #-} DynFlags
39 import Outputable
40 import Platform
41 import System.FilePath
42 import Binary
43
44 -----------------------------------------------------------------------------
45 -- Phases
46
47 {-
48 Phase of the | Suffix saying | Flag saying | (suffix of)
49 compilation system | ``start here''| ``stop after''| output file
50
51 literate pre-processor | .lhs | - | -
52 C pre-processor (opt.) | - | -E | -
53 Haskell compiler | .hs | -C, -S | .hc, .s
54 C compiler (opt.) | .hc or .c | -S | .s
55 assembler | .s or .S | -c | .o
56 linker | other | - | a.out
57 -}
58
59 -- Note [HscSource types]
60 -- ~~~~~~~~~~~~~~~~~~~~~~
61 -- There are two types of source file for user-written Haskell code:
62 --
63 -- * HsSrcFile is an ordinary hs file which contains code,
64 --
65 -- * HsBootFile is an hs-boot file. Within a unit, it can
66 -- be used to break recursive module imports, in which case there's an
67 -- HsSrcFile associated with it. However, externally, it can
68 -- also be used to specify the *requirements* of a package,
69 -- in which case there is an HsBootMerge associated with it.
70 --
71 -- An HsBootMerge is a "fake" source file, which is constructed
72 -- by collecting up non-recursive HsBootFiles into a single interface.
73 -- HsBootMerges get an hi and o file, and are treated as "non-boot"
74 -- sources.
75
76 data HscSource
77 = HsSrcFile | HsBootFile | HsBootMerge
78 deriving( Eq, Ord, Show )
79 -- Ord needed for the finite maps we build in CompManager
80
81 instance Outputable HscSource where
82 ppr HsSrcFile = text "HsSrcFile"
83 ppr HsBootFile = text "HsBootFile"
84 ppr HsBootMerge = text "HsBootMerge"
85
86 instance Binary HscSource where
87 put_ bh HsSrcFile = putByte bh 0
88 put_ bh HsBootFile = putByte bh 1
89 put_ bh HsBootMerge = putByte bh 2
90 get bh = do
91 h <- getByte bh
92 case h of
93 0 -> return HsSrcFile
94 1 -> return HsBootFile
95 _ -> return HsBootMerge
96
97 hscSourceString :: HscSource -> String
98 hscSourceString HsSrcFile = ""
99 hscSourceString HsBootFile = "[boot]"
100 hscSourceString HsBootMerge = "[merge]"
101
102 isHsBoot :: HscSource -> Bool
103 isHsBoot HsBootFile = True
104 isHsBoot HsSrcFile = False
105 isHsBoot HsBootMerge = False
106
107 data Phase
108 = Unlit HscSource
109 | Cpp HscSource
110 | HsPp HscSource
111 | Hsc HscSource
112 | Ccxx -- Compile C++
113 | Cc -- Compile C
114 | Cobjc -- Compile Objective-C
115 | Cobjcxx -- Compile Objective-C++
116 | HCc -- Haskellised C (as opposed to vanilla C) compilation
117 | Splitter -- Assembly file splitter (part of '-split-objs')
118 | SplitAs -- Assembler for split assembly files (part of '-split-objs')
119 | As Bool -- Assembler for regular assembly files (Bool: with-cpp)
120 | LlvmOpt -- Run LLVM opt tool over llvm assembly
121 | LlvmLlc -- LLVM bitcode to native assembly
122 | LlvmMangle -- Fix up TNTC by processing assembly produced by LLVM
123 | CmmCpp -- pre-process Cmm source
124 | Cmm -- parse & compile Cmm code
125 | MergeStub -- merge in the stub object file
126
127 -- The final phase is a pseudo-phase that tells the pipeline to stop.
128 -- There is no runPhase case for it.
129 | StopLn -- Stop, but linking will follow, so generate .o file
130 deriving (Eq, Show)
131
132 instance Outputable Phase where
133 ppr p = text (show p)
134
135 anyHsc :: Phase
136 anyHsc = Hsc (panic "anyHsc")
137
138 isStopLn :: Phase -> Bool
139 isStopLn StopLn = True
140 isStopLn _ = False
141
142 eqPhase :: Phase -> Phase -> Bool
143 -- Equality of constructors, ignoring the HscSource field
144 -- NB: the HscSource field can be 'bot'; see anyHsc above
145 eqPhase (Unlit _) (Unlit _) = True
146 eqPhase (Cpp _) (Cpp _) = True
147 eqPhase (HsPp _) (HsPp _) = True
148 eqPhase (Hsc _) (Hsc _) = True
149 eqPhase Cc Cc = True
150 eqPhase Cobjc Cobjc = True
151 eqPhase HCc HCc = True
152 eqPhase Splitter Splitter = True
153 eqPhase SplitAs SplitAs = True
154 eqPhase (As x) (As y) = x == y
155 eqPhase LlvmOpt LlvmOpt = True
156 eqPhase LlvmLlc LlvmLlc = True
157 eqPhase LlvmMangle LlvmMangle = True
158 eqPhase CmmCpp CmmCpp = True
159 eqPhase Cmm Cmm = True
160 eqPhase MergeStub MergeStub = True
161 eqPhase StopLn StopLn = True
162 eqPhase Ccxx Ccxx = True
163 eqPhase Cobjcxx Cobjcxx = True
164 eqPhase _ _ = False
165
166 {- Note [Partial ordering on phases]
167
168 We want to know which phases will occur before which others. This is used for
169 sanity checking, to ensure that the pipeline will stop at some point (see
170 DriverPipeline.runPipeline).
171
172 A < B iff A occurs before B in a normal compilation pipeline.
173
174 There is explicitly not a total ordering on phases, because in registerised
175 builds, the phase `HsC` doesn't happen before nor after any other phase.
176
177 Although we check that a normal user doesn't set the stop_phase to HsC through
178 use of -C with registerised builds (in Main.checkOptions), it is still
179 possible for a ghc-api user to do so. So be careful when using the function
180 happensBefore, and don't think that `not (a <= b)` implies `b < a`.
181 -}
182 happensBefore :: DynFlags -> Phase -> Phase -> Bool
183 happensBefore dflags p1 p2 = p1 `happensBefore'` p2
184 where StopLn `happensBefore'` _ = False
185 x `happensBefore'` y = after_x `eqPhase` y
186 || after_x `happensBefore'` y
187 where after_x = nextPhase dflags x
188
189 nextPhase :: DynFlags -> Phase -> Phase
190 nextPhase dflags p
191 -- A conservative approximation to the next phase, used in happensBefore
192 = case p of
193 Unlit sf -> Cpp sf
194 Cpp sf -> HsPp sf
195 HsPp sf -> Hsc sf
196 Hsc _ -> maybeHCc
197 Splitter -> SplitAs
198 LlvmOpt -> LlvmLlc
199 LlvmLlc -> LlvmMangle
200 LlvmMangle -> As False
201 SplitAs -> MergeStub
202 As _ -> MergeStub
203 Ccxx -> As False
204 Cc -> As False
205 Cobjc -> As False
206 Cobjcxx -> As False
207 CmmCpp -> Cmm
208 Cmm -> maybeHCc
209 HCc -> As False
210 MergeStub -> StopLn
211 StopLn -> panic "nextPhase: nothing after StopLn"
212 where maybeHCc = if platformUnregisterised (targetPlatform dflags)
213 then HCc
214 else As False
215
216 -- the first compilation phase for a given file is determined
217 -- by its suffix.
218 startPhase :: String -> Phase
219 startPhase "lhs" = Unlit HsSrcFile
220 startPhase "lhs-boot" = Unlit HsBootFile
221 startPhase "hs" = Cpp HsSrcFile
222 startPhase "hs-boot" = Cpp HsBootFile
223 startPhase "hscpp" = HsPp HsSrcFile
224 startPhase "hspp" = Hsc HsSrcFile
225 startPhase "hc" = HCc
226 startPhase "c" = Cc
227 startPhase "cpp" = Ccxx
228 startPhase "C" = Cc
229 startPhase "m" = Cobjc
230 startPhase "M" = Cobjcxx
231 startPhase "mm" = Cobjcxx
232 startPhase "cc" = Ccxx
233 startPhase "cxx" = Ccxx
234 startPhase "split_s" = Splitter
235 startPhase "s" = As False
236 startPhase "S" = As True
237 startPhase "ll" = LlvmOpt
238 startPhase "bc" = LlvmLlc
239 startPhase "lm_s" = LlvmMangle
240 startPhase "o" = StopLn
241 startPhase "cmm" = CmmCpp
242 startPhase "cmmcpp" = Cmm
243 startPhase _ = StopLn -- all unknown file types
244
245 -- This is used to determine the extension for the output from the
246 -- current phase (if it generates a new file). The extension depends
247 -- on the next phase in the pipeline.
248 phaseInputExt :: Phase -> String
249 phaseInputExt (Unlit HsSrcFile) = "lhs"
250 phaseInputExt (Unlit HsBootFile) = "lhs-boot"
251 phaseInputExt (Unlit HsBootMerge) = panic "phaseInputExt: Unlit HsBootMerge"
252 -- You can't Unlit an HsBootMerge, because there's no source
253 -- file to Unlit!
254 phaseInputExt (Cpp _) = "lpp" -- intermediate only
255 phaseInputExt (HsPp _) = "hscpp" -- intermediate only
256 phaseInputExt (Hsc _) = "hspp" -- intermediate only
257 -- NB: as things stand, phaseInputExt (Hsc x) must not evaluate x
258 -- because runPipeline uses the StopBefore phase to pick the
259 -- output filename. That could be fixed, but watch out.
260 phaseInputExt HCc = "hc"
261 phaseInputExt Ccxx = "cpp"
262 phaseInputExt Cobjc = "m"
263 phaseInputExt Cobjcxx = "mm"
264 phaseInputExt Cc = "c"
265 phaseInputExt Splitter = "split_s"
266 phaseInputExt (As True) = "S"
267 phaseInputExt (As False) = "s"
268 phaseInputExt LlvmOpt = "ll"
269 phaseInputExt LlvmLlc = "bc"
270 phaseInputExt LlvmMangle = "lm_s"
271 phaseInputExt SplitAs = "split_s"
272 phaseInputExt CmmCpp = "cmm"
273 phaseInputExt Cmm = "cmmcpp"
274 phaseInputExt MergeStub = "o"
275 phaseInputExt StopLn = "o"
276
277 haskellish_src_suffixes, haskellish_suffixes, cish_suffixes,
278 haskellish_user_src_suffixes
279 :: [String]
280 -- When a file with an extension in the haskellish_src_suffixes group is
281 -- loaded in --make mode, its imports will be loaded too.
282 haskellish_src_suffixes = haskellish_user_src_suffixes ++
283 [ "hspp", "hscpp" ]
284 haskellish_suffixes = haskellish_src_suffixes ++
285 [ "hc", "cmm", "cmmcpp" ]
286 cish_suffixes = [ "c", "cpp", "C", "cc", "cxx", "s", "S", "ll", "bc", "lm_s", "m", "M", "mm" ]
287
288 -- Will not be deleted as temp files:
289 haskellish_user_src_suffixes = [ "hs", "lhs", "hs-boot", "lhs-boot" ]
290
291 objish_suffixes :: Platform -> [String]
292 -- Use the appropriate suffix for the system on which
293 -- the GHC-compiled code will run
294 objish_suffixes platform = case platformOS platform of
295 OSMinGW32 -> [ "o", "O", "obj", "OBJ" ]
296 _ -> [ "o" ]
297
298 dynlib_suffixes :: Platform -> [String]
299 dynlib_suffixes platform = case platformOS platform of
300 OSMinGW32 -> ["dll", "DLL"]
301 OSDarwin -> ["dylib", "so"]
302 _ -> ["so"]
303
304 isHaskellishSuffix, isHaskellSrcSuffix, isCishSuffix,
305 isHaskellUserSrcSuffix
306 :: String -> Bool
307 isHaskellishSuffix s = s `elem` haskellish_suffixes
308 isHaskellSrcSuffix s = s `elem` haskellish_src_suffixes
309 isCishSuffix s = s `elem` cish_suffixes
310 isHaskellUserSrcSuffix s = s `elem` haskellish_user_src_suffixes
311
312 isObjectSuffix, isDynLibSuffix :: Platform -> String -> Bool
313 isObjectSuffix platform s = s `elem` objish_suffixes platform
314 isDynLibSuffix platform s = s `elem` dynlib_suffixes platform
315
316 isSourceSuffix :: String -> Bool
317 isSourceSuffix suff = isHaskellishSuffix suff || isCishSuffix suff
318
319 isHaskellishFilename, isHaskellSrcFilename, isCishFilename,
320 isHaskellUserSrcFilename, isSourceFilename
321 :: FilePath -> Bool
322 -- takeExtension return .foo, so we drop 1 to get rid of the .
323 isHaskellishFilename f = isHaskellishSuffix (drop 1 $ takeExtension f)
324 isHaskellSrcFilename f = isHaskellSrcSuffix (drop 1 $ takeExtension f)
325 isCishFilename f = isCishSuffix (drop 1 $ takeExtension f)
326 isHaskellUserSrcFilename f = isHaskellUserSrcSuffix (drop 1 $ takeExtension f)
327 isSourceFilename f = isSourceSuffix (drop 1 $ takeExtension f)
328
329 isObjectFilename, isDynLibFilename :: Platform -> FilePath -> Bool
330 isObjectFilename platform f = isObjectSuffix platform (drop 1 $ takeExtension f)
331 isDynLibFilename platform f = isDynLibSuffix platform (drop 1 $ takeExtension f)
332