| 1 | -- Compiler Toolkit: compiler state management |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 2 November 95 |
| 5 | -- |
| 6 | -- Version $Revision: 1.1.1.1 $ from $Date: 2004/11/13 16:42:45 $ |
| 7 | -- |
| 8 | -- Copyright (c) [1995..1999] Manuel M. T. Chakravarty |
| 9 | -- |
| 10 | -- This file is free software; you can redistribute it and/or modify |
| 11 | -- it under the terms of the GNU General Public License as published by |
| 12 | -- the Free Software Foundation; either version 2 of the License, or |
| 13 | -- (at your option) any later version. |
| 14 | -- |
| 15 | -- This file is distributed in the hope that it will be useful, |
| 16 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | -- GNU General Public License for more details. |
| 19 | -- |
| 20 | --- DESCRIPTION --------------------------------------------------------------- |
| 21 | -- |
| 22 | -- This module forms the interface to the state base of the compiler. It is |
| 23 | -- used by all modules that are not directly involved in implementing the |
| 24 | -- state base. It provides a state transformer that is capable of doing I/O |
| 25 | -- and provides facilities such as error handling and compiler switch |
| 26 | -- management. |
| 27 | -- |
| 28 | --- DOCU ---------------------------------------------------------------------- |
| 29 | -- |
| 30 | -- language: Haskell 98 |
| 31 | -- |
| 32 | -- * The monad `PreCST' is reexported abstractly. |
| 33 | -- |
| 34 | -- * Errors are dumped to `stdout' to facilitate communication with other |
| 35 | -- processes (see `Interact'). |
| 36 | -- |
| 37 | --- TODO ---------------------------------------------------------------------- |
| 38 | -- |
| 39 | |
| 40 | module State (-- the PreCST monad |
| 41 | -- |
| 42 | PreCST, -- reexport ABSTRACT |
| 43 | nop, yield, (+>=), (+>), fixCST, -- reexport |
| 44 | throwExc, fatal, catchExc, fatalsHandledBy, -- reexport lifted |
| 45 | readCST, writeCST, transCST, run, runCST, |
| 46 | StateTrans.MVar, -- reexport |
| 47 | newMV, readMV, assignMV, -- reexport lifted |
| 48 | -- |
| 49 | -- reexport compiler I/O |
| 50 | -- |
| 51 | module CIO, |
| 52 | liftIO, |
| 53 | -- |
| 54 | -- identification |
| 55 | -- |
| 56 | getId, |
| 57 | -- |
| 58 | -- error management |
| 59 | -- |
| 60 | raise, raiseWarning, raiseError, raiseFatal, showErrors, |
| 61 | errorsPresent, |
| 62 | -- |
| 63 | -- extra state management |
| 64 | -- |
| 65 | readExtra, updExtra, |
| 66 | -- |
| 67 | -- name supplies |
| 68 | -- |
| 69 | getNameSupply) |
| 70 | where |
| 71 | |
| 72 | import Data.Ix |
| 73 | import Control.Monad (when) |
| 74 | import Data.List (sort) |
| 75 | |
| 76 | import BaseVersion (version, copyright, disclaimer) |
| 77 | import Config (errorLimit) |
| 78 | import Position (Position) |
| 79 | import UNames (NameSupply, |
| 80 | rootSupply, splitSupply) |
| 81 | import StateTrans (STB, |
| 82 | readBase, transBase, runSTB) |
| 83 | import qualified |
| 84 | StateTrans (interleave, throwExc, fatal, catchExc, fatalsHandledBy, |
| 85 | MVar, newMV, readMV, assignMV) |
| 86 | import StateBase (PreCST(..), ErrorState(..), BaseState(..), |
| 87 | nop, yield, (+>=), (+>), fixCST, |
| 88 | unpackCST, readCST, writeCST, transCST, |
| 89 | liftIO) |
| 90 | import CIO |
| 91 | import Errors (ErrorLvl(..), Error, makeError, errorLvl, showError) |
| 92 | |
| 93 | |
| 94 | -- state used in the whole compiler |
| 95 | -- -------------------------------- |
| 96 | |
| 97 | -- initialization |
| 98 | -- |
| 99 | -- * it gets the version information and the initial extra state as arguments |
| 100 | -- |
| 101 | initialBaseState :: (String, String, String) -> e -> BaseState e |
| 102 | initialBaseState vcd es = BaseState { |
| 103 | idTKBS = (version, copyright, disclaimer), |
| 104 | idBS = vcd, |
| 105 | errorsBS = initialErrorState, |
| 106 | suppliesBS = splitSupply rootSupply, |
| 107 | extraBS = es |
| 108 | } |
| 109 | |
| 110 | |
| 111 | -- executing state transformers |
| 112 | -- ---------------------------- |
| 113 | |
| 114 | -- initiate a complete run of the ToolKit represented by a PreCST with a void |
| 115 | -- generic component (type `()') (EXPORTED) |
| 116 | -- |
| 117 | -- * fatals errors are explicitly caught and reported (instead of letting them |
| 118 | -- through to the runtime system) |
| 119 | -- |
| 120 | run :: (String, String, String) -> e -> PreCST e () a -> IO a |
| 121 | run vcd es cst = runSTB m (initialBaseState vcd es) () |
| 122 | where |
| 123 | m = unpackCST ( |
| 124 | cst |
| 125 | `fatalsHandledBy` \err -> |
| 126 | putStrCIO ("Uncaught fatal error: " ++ show err) >> |
| 127 | exitWithCIO (ExitFailure 1) |
| 128 | ) |
| 129 | |
| 130 | -- run a PreCST in the context of another PreCST (EXPORTED) |
| 131 | -- |
| 132 | -- the generic state of the enclosing PreCST is preserved while the |
| 133 | -- computation of the PreCST passed as an argument is interleaved in the |
| 134 | -- execution of the enclosing one |
| 135 | -- |
| 136 | runCST :: PreCST e s a -> s -> PreCST e s' a |
| 137 | runCST m s = CST $ StateTrans.interleave (unpackCST m) s |
| 138 | |
| 139 | |
| 140 | -- exception handling |
| 141 | -- ------------------ |
| 142 | |
| 143 | -- throw an exception with the given tag and message (EXPORTED) |
| 144 | -- |
| 145 | throwExc :: String -> String -> PreCST e s a |
| 146 | throwExc s1 s2 = CST $ StateTrans.throwExc s1 s2 |
| 147 | |
| 148 | -- raise a fatal user-defined error (EXPORTED) |
| 149 | -- |
| 150 | -- * such an error my be caught and handled using `fatalsHandeledBy' |
| 151 | -- |
| 152 | fatal :: String -> PreCST e s a |
| 153 | fatal = CST . StateTrans.fatal |
| 154 | |
| 155 | -- the given state transformer is executed and exceptions with the given tag |
| 156 | -- are caught using the provided handler, which expects to get the exception |
| 157 | -- message (EXPORTED) |
| 158 | -- |
| 159 | -- * the state observed by the exception handler is *modified* by the failed |
| 160 | -- state transformer upto the point where the exception was thrown (this |
| 161 | -- semantics is the only reasonable when it should be possible to use |
| 162 | -- updating for maintaining the state) |
| 163 | -- |
| 164 | catchExc :: PreCST e s a |
| 165 | -> (String, String -> PreCST e s a) |
| 166 | -> PreCST e s a |
| 167 | catchExc m (s, h) = CST $ StateTrans.catchExc (unpackCST m) (s, unpackCST . h) |
| 168 | |
| 169 | -- given a state transformer that may raise fatal errors and an error handler |
| 170 | -- for fatal errors, execute the state transformer and apply the error handler |
| 171 | -- when a fatal error occurs (EXPORTED) |
| 172 | -- |
| 173 | -- * fatal errors are IO monad errors and errors raised by `fatal' as well as |
| 174 | -- uncaught exceptions |
| 175 | -- |
| 176 | -- * the base and generic state observed by the error handler is *in contrast |
| 177 | -- to `catch'* the state *before* the state transformer is applied |
| 178 | -- |
| 179 | fatalsHandledBy :: PreCST e s a -> (IOError -> PreCST e s a) -> PreCST e s a |
| 180 | fatalsHandledBy m h = CST $ StateTrans.fatalsHandledBy m' h' |
| 181 | where |
| 182 | m' = unpackCST m |
| 183 | h' = unpackCST . h |
| 184 | |
| 185 | -- mutable variables |
| 186 | -- ----------------- |
| 187 | |
| 188 | -- lifted mutable variable functions (EXPORTED) |
| 189 | -- |
| 190 | |
| 191 | newMV :: a -> PreCST e s (StateTrans.MVar a) |
| 192 | newMV = CST . StateTrans.newMV |
| 193 | |
| 194 | readMV :: StateTrans.MVar a -> PreCST e s a |
| 195 | readMV = CST . StateTrans.readMV |
| 196 | |
| 197 | assignMV :: StateTrans.MVar a -> a -> PreCST e s () |
| 198 | assignMV m a = CST $ StateTrans.assignMV m a |
| 199 | |
| 200 | -- read identification |
| 201 | -- ------------------- |
| 202 | |
| 203 | -- read identification information (EXPORT) |
| 204 | -- |
| 205 | getId :: PreCST e s (String, String, String) |
| 206 | getId = CST $ |
| 207 | readBase (idBS) |
| 208 | |
| 209 | |
| 210 | -- manipulating the error state |
| 211 | -- ---------------------------- |
| 212 | |
| 213 | -- the lowest level of errors is `WarningErr', but it is meaningless as long as |
| 214 | -- the the list of errors is empty |
| 215 | -- |
| 216 | initialErrorState :: ErrorState |
| 217 | initialErrorState = ErrorState WarningErr 0 [] |
| 218 | |
| 219 | -- raise an error (EXPORTED) |
| 220 | -- |
| 221 | -- * a fatal error is reported immediately; see `raiseFatal' |
| 222 | -- |
| 223 | raise :: Error -> PreCST e s () |
| 224 | raise err = case errorLvl err of |
| 225 | WarningErr -> raise0 err |
| 226 | ErrorErr -> raise0 err |
| 227 | FatalErr -> raiseFatal0 "Generic fatal error." err |
| 228 | |
| 229 | -- raise a warning (see `raiseErr') (EXPORTED) |
| 230 | -- |
| 231 | raiseWarning :: Position -> [String] -> PreCST e s () |
| 232 | raiseWarning pos msg = raise0 (makeError WarningErr pos msg) |
| 233 | |
| 234 | -- raise an error (see `raiseErr') (EXPORTED) |
| 235 | -- |
| 236 | raiseError :: Position -> [String] -> PreCST e s () |
| 237 | raiseError pos msg = raise0 (makeError ErrorErr pos msg) |
| 238 | |
| 239 | -- raise a fatal compilation error (EXPORTED) |
| 240 | -- |
| 241 | -- * the error is together with the up-to-now accumulated errors are reported |
| 242 | -- as part of the error message of the fatal error exception |
| 243 | -- |
| 244 | -- * the current thread of control is discarded and control is passed to the |
| 245 | -- innermost handler for fatal errors |
| 246 | -- |
| 247 | -- * the first argument must contain a short description of the error, while |
| 248 | -- the second and third argument are like the two arguments to `raise' |
| 249 | -- |
| 250 | raiseFatal :: String -> Position -> [String] -> PreCST e s a |
| 251 | raiseFatal short pos long = raiseFatal0 short (makeError FatalErr pos long) |
| 252 | |
| 253 | -- raise a fatal error; internal version that gets an abstract error |
| 254 | -- |
| 255 | raiseFatal0 :: String -> Error -> PreCST e s a |
| 256 | raiseFatal0 short err = do |
| 257 | raise0 err |
| 258 | errmsgs <- showErrors |
| 259 | fatal (short ++ "\n\n" ++ errmsgs) |
| 260 | |
| 261 | -- raise an error; internal version, doesn't check whether the error is fatal |
| 262 | -- |
| 263 | -- * the error is entered into the compiler state and a fatal error is |
| 264 | -- triggered if the `errorLimit' is reached |
| 265 | -- |
| 266 | raise0 :: Error -> PreCST e s () |
| 267 | raise0 err = do |
| 268 | noOfErrs <- CST $ transBase doRaise |
| 269 | when (noOfErrs >= errorLimit) $ do |
| 270 | errmsgs <- showErrors |
| 271 | fatal ("Error limit of " ++ show errorLimit |
| 272 | ++ " errors has been reached.\n" ++ errmsgs) |
| 273 | where |
| 274 | doRaise :: BaseState e -> (BaseState e, Int) |
| 275 | doRaise bs = let |
| 276 | lvl = errorLvl err |
| 277 | ErrorState wlvl no errs = errorsBS bs |
| 278 | wlvl' = max wlvl lvl |
| 279 | no' = no + if lvl > WarningErr |
| 280 | then 1 else 0 |
| 281 | errs' = err : errs |
| 282 | in |
| 283 | (bs {errorsBS = (ErrorState wlvl' no' errs')}, no') |
| 284 | |
| 285 | -- yield a string containing the collected error messages (EXPORTED) |
| 286 | -- |
| 287 | -- * the error state is reset in this process |
| 288 | -- |
| 289 | showErrors :: PreCST e s String |
| 290 | showErrors = CST $ do |
| 291 | ErrorState wlvl no errs <- transBase extractErrs |
| 292 | return $ foldr (.) id (map showString (errsToStrs errs)) "" |
| 293 | where |
| 294 | extractErrs :: BaseState e -> (BaseState e, ErrorState) |
| 295 | extractErrs bs = (bs {errorsBS = initialErrorState}, |
| 296 | errorsBS bs) |
| 297 | |
| 298 | errsToStrs :: [Error] -> [String] |
| 299 | errsToStrs errs = (map showError . sort) errs |
| 300 | |
| 301 | -- inquire if there was already an error of at least level `ErrorErr' raised |
| 302 | -- (EXPORTED) |
| 303 | -- |
| 304 | errorsPresent :: PreCST e s Bool |
| 305 | errorsPresent = CST $ do |
| 306 | ErrorState wlvl no _ <- readBase errorsBS |
| 307 | return $ wlvl >= ErrorErr |
| 308 | |
| 309 | |
| 310 | -- manipulating the extra state |
| 311 | -- ---------------------------- |
| 312 | |
| 313 | -- apply a reader function to the extra state and yield the reader's result |
| 314 | -- (EXPORTED) |
| 315 | -- |
| 316 | readExtra :: (e -> a) -> PreCST e s a |
| 317 | readExtra rf = CST $ readBase (\bs -> |
| 318 | (rf . extraBS) bs |
| 319 | ) |
| 320 | |
| 321 | -- apply an update function to the extra state (EXPORTED) |
| 322 | -- |
| 323 | updExtra :: (e -> e) -> PreCST e s () |
| 324 | updExtra uf = CST $ transBase (\bs -> |
| 325 | let |
| 326 | es = extraBS bs |
| 327 | in |
| 328 | (bs {extraBS = uf es}, ()) |
| 329 | ) |
| 330 | |
| 331 | |
| 332 | -- name supplies |
| 333 | -- ------------- |
| 334 | |
| 335 | -- Get a name supply out of the base state (EXPORTED) |
| 336 | -- |
| 337 | getNameSupply :: PreCST e s NameSupply |
| 338 | getNameSupply = CST $ transBase (\bs -> |
| 339 | let |
| 340 | supply : supplies = suppliesBS bs |
| 341 | in |
| 342 | (bs {suppliesBS = supplies}, supply) |
| 343 | ) |