| 1 |
-- C->Haskell Compiler: custom header generator |
| 2 |
-- |
| 3 |
-- Author : Manuel M T Chakravarty |
| 4 |
-- Created: 5 February 2003 |
| 5 |
-- |
| 6 |
-- Version $Revision: 1.1 $ |
| 7 |
-- |
| 8 |
-- Copyright (c) 2004 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 implements the generation of a custom header from a binding |
| 23 |
-- module. |
| 24 |
-- |
| 25 |
--- DOCU ---------------------------------------------------------------------- |
| 26 |
-- |
| 27 |
-- language: Haskell 98 |
| 28 |
-- |
| 29 |
-- Computing CPP Conditionals |
| 30 |
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 31 |
-- We obtain information about which branches of CPP conditions are taken |
| 32 |
-- during pre-processing of the custom header file by introducing new |
| 33 |
-- struct declarations. Specifically, after each #if[[n]def] or #elif, |
| 34 |
-- we place a declaration of the form |
| 35 |
-- |
| 36 |
-- struct C2HS_COND_SENTRY<unique number>; |
| 37 |
-- |
| 38 |
-- We can, then, determine which branch of a conditional has been taken by |
| 39 |
-- checking whether the struct corresponding to that conditional has been |
| 40 |
-- declared. |
| 41 |
-- |
| 42 |
--- TODO ---------------------------------------------------------------------- |
| 43 |
-- |
| 44 |
-- * Ideally, `ghFrag[s]' should be tail recursive |
| 45 |
|
| 46 |
module GenHeader ( |
| 47 |
genHeader |
| 48 |
) where |
| 49 |
|
| 50 |
-- standard libraries |
| 51 |
import Control.Monad (when) |
| 52 |
|
| 53 |
-- Compiler Toolkit |
| 54 |
import Position (Position, Pos(..), nopos) |
| 55 |
import DLists (DList, openDL, closeDL, zeroDL, unitDL, joinDL, snocDL) |
| 56 |
import Errors (interr) |
| 57 |
import Idents (onlyPosIdent) |
| 58 |
import UNames (NameSupply, Name, names) |
| 59 |
|
| 60 |
-- C->Haskell |
| 61 |
import C2HSState (CST, getNameSupply, runCST, transCST, raiseError, catchExc, |
| 62 |
throwExc, errorsPresent, showErrors, fatal) |
| 63 |
|
| 64 |
-- friends |
| 65 |
import CHS (CHSModule(..), CHSFrag(..)) |
| 66 |
|
| 67 |
|
| 68 |
-- The header generation monad |
| 69 |
-- |
| 70 |
type GH a = CST [Name] a |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
genHeader :: CHSModule -> CST s ([String], CHSModule, String) |
| 80 |
genHeader mod = |
| 81 |
do |
| 82 |
supply <- getNameSupply |
| 83 |
(header, mod) <- runCST (ghModule mod) (names supply) |
| 84 |
`ifGHExc` return ([], CHSModule []) |
| 85 |
|
| 86 |
-- check for errors and finalise |
| 87 |
-- |
| 88 |
errs <- errorsPresent |
| 89 |
if errs |
| 90 |
then do |
| 91 |
errmsgs <- showErrors |
| 92 |
fatal ("Errors during generation of C header:\n\n" -- fatal error |
| 93 |
++ errmsgs) |
| 94 |
else do |
| 95 |
warnmsgs <- showErrors |
| 96 |
return (header, mod, warnmsgs) |
| 97 |
|
| 98 |
-- Obtain a new base name that may be used, in C, to encode the result of a |
| 99 |
-- preprocessor conditionl. |
| 100 |
-- |
| 101 |
newName :: CST [Name] String |
| 102 |
newName = transCST $ |
| 103 |
\supply -> (tail supply, "C2HS_COND_SENTRY_" ++ show (head supply)) |
| 104 |
|
| 105 |
-- Various forms of processed fragments |
| 106 |
-- |
| 107 |
data FragElem = Frag CHSFrag |
| 108 |
| Elif String Position |
| 109 |
| Else Position |
| 110 |
| Endif Position |
| 111 |
| EOF |
| 112 |
|
| 113 |
instance Pos FragElem where |
| 114 |
posOf (Frag frag ) = posOf frag |
| 115 |
posOf (Elif _ pos) = pos |
| 116 |
posOf (Else pos) = pos |
| 117 |
posOf (Endif pos) = pos |
| 118 |
posOf EOF = nopos |
| 119 |
|
| 120 |
-- check for end of file |
| 121 |
-- |
| 122 |
isEOF :: FragElem -> Bool |
| 123 |
isEOF EOF = True |
| 124 |
isEOF _ = False |
| 125 |
|
| 126 |
-- Generate the C header for an entire .chs module. |
| 127 |
-- |
| 128 |
-- * This works more or less like a recursive decent parser for a statement |
| 129 |
-- sequence that may contain conditionals, where `ghFrag' implements most of |
| 130 |
-- the state transition system of the associated automaton |
| 131 |
-- |
| 132 |
ghModule :: CHSModule -> GH ([String], CHSModule) |
| 133 |
ghModule (CHSModule frags) = |
| 134 |
do |
| 135 |
(header, frags, last, rest) <- ghFrags frags |
| 136 |
when (not . isEOF $ last) $ |
| 137 |
notOpenCondErr (posOf last) |
| 138 |
return (closeDL header, CHSModule frags) |
| 139 |
|
| 140 |
-- Collect header and fragments up to eof or a CPP directive that is part of a |
| 141 |
-- conditional |
| 142 |
-- |
| 143 |
-- * We collect the header (ie, CPP directives and inline-C) using a |
| 144 |
-- difference list to avoid worst case O(n^2) complexity due to |
| 145 |
-- concatenation of lines that go into the header. |
| 146 |
-- |
| 147 |
ghFrags :: [CHSFrag] -> GH (DList String, [CHSFrag], FragElem, [CHSFrag]) |
| 148 |
ghFrags [] = return (zeroDL, [], EOF, []) |
| 149 |
ghFrags frags = |
| 150 |
do |
| 151 |
(header, frag, rest) <- ghFrag frags |
| 152 |
case frag of |
| 153 |
Frag aFrag -> do |
| 154 |
(header2, frags', frag', rest) <- ghFrags rest |
| 155 |
-- FIXME: Not tail rec |
| 156 |
return (header `joinDL` header2, aFrag:frags', |
| 157 |
frag', rest) |
| 158 |
_ -> return (header, [], frag, rest) |
| 159 |
|
| 160 |
-- Process a single fragment *structure*; i.e., if the first fragment |
| 161 |
-- introduces a conditional, process the whole conditional; otherwise, process |
| 162 |
-- the first fragment |
| 163 |
-- |
| 164 |
ghFrag :: [CHSFrag] -> GH (DList String, -- partial header file |
| 165 |
FragElem, -- processed fragment |
| 166 |
[CHSFrag]) -- not yet processed fragments |
| 167 |
ghFrag [] = |
| 168 |
return (zeroDL, EOF, []) |
| 169 |
ghFrag (frag@(CHSVerb _ _ ) : frags) = |
| 170 |
return (zeroDL, Frag frag, frags) |
| 171 |
ghFrag (frag@(CHSHook _ ) : frags) = |
| 172 |
return (zeroDL, Frag frag, frags) |
| 173 |
ghFrag (frag@(CHSLine _ ) : frags) = |
| 174 |
return (zeroDL, Frag frag, frags) |
| 175 |
ghFrag (frag@(CHSLang _ _ ) : frags) = |
| 176 |
return (zeroDL, Frag frag, frags) |
| 177 |
ghFrag ( (CHSC s _ ) : frags) = |
| 178 |
do |
| 179 |
(header, frag, frags' ) <- ghFrag frags -- scan for next CHS fragment |
| 180 |
return (unitDL s `joinDL` header, frag, frags') |
| 181 |
-- FIXME: this is not tail recursive... |
| 182 |
ghFrag ( (CHSCond _ _ ) : frags) = |
| 183 |
interr "GenHeader.ghFrags: There can't be a structured conditional yet!" |
| 184 |
ghFrag (frag@(CHSCPP s pos) : frags) = |
| 185 |
let |
| 186 |
(directive, _) = break (`elem` " \t") |
| 187 |
. dropWhile (`elem` " \t") |
| 188 |
$ s |
| 189 |
in |
| 190 |
case directive of |
| 191 |
"if" -> openIf s pos frags |
| 192 |
"ifdef" -> openIf s pos frags |
| 193 |
"ifndef" -> openIf s pos frags |
| 194 |
"else" -> return (zeroDL , Else pos , frags) |
| 195 |
"elif" -> return (zeroDL , Elif s pos , frags) |
| 196 |
"endif" -> return (zeroDL , Endif pos , frags) |
| 197 |
_ -> return (openDL ['#':s, "\n"], Frag (CHSVerb "" nopos), frags) |
| 198 |
where |
| 199 |
-- enter a new conditional (may be an #if[[n]def] or #elif) |
| 200 |
-- |
| 201 |
-- * Arguments are the lexeme of the directive `s', the position of that |
| 202 |
-- directive `pos', and the fragments following the directive `frags' |
| 203 |
-- |
| 204 |
openIf s pos frags = |
| 205 |
do |
| 206 |
(headerTh, fragsTh, last, rest) <- ghFrags frags |
| 207 |
case last of |
| 208 |
Else pos -> do |
| 209 |
(headerEl, fragsEl, last, rest) <- ghFrags rest |
| 210 |
case last of |
| 211 |
Else pos -> notOpenCondErr pos |
| 212 |
Elif _ pos -> notOpenCondErr pos |
| 213 |
Endif pos -> closeIf |
| 214 |
((headerTh |
| 215 |
`snocDL` "#else\n") |
| 216 |
`joinDL` |
| 217 |
(headerEl |
| 218 |
`snocDL` "#endif\n")) |
| 219 |
(s, fragsTh) |
| 220 |
[] |
| 221 |
(Just fragsEl) |
| 222 |
rest |
| 223 |
EOF -> notClosedCondErr pos |
| 224 |
Elif s' pos -> do |
| 225 |
(headerEl, condFrag, rest) <- openIf s' pos rest |
| 226 |
case condFrag of |
| 227 |
Frag (CHSCond alts dft) -> |
| 228 |
closeIf (headerTh `joinDL` headerEl) |
| 229 |
(s, fragsTh) |
| 230 |
alts |
| 231 |
dft |
| 232 |
rest |
| 233 |
_ -> |
| 234 |
interr "GenHeader.ghFrag: Expected CHSCond!" |
| 235 |
Endif pos -> closeIf (headerTh `snocDL` "#endif\n") |
| 236 |
(s, fragsTh) |
| 237 |
[] |
| 238 |
(Just []) |
| 239 |
rest |
| 240 |
EOF -> notClosedCondErr pos |
| 241 |
-- |
| 242 |
-- turn a completed conditional into a `CHSCond' fragment |
| 243 |
-- |
| 244 |
-- * `(s, fragsTh)' is the CPP directive `s' containing the condition under |
| 245 |
-- which `fragTh' should be executed; `alts' are alternative branches |
| 246 |
-- (with conditions); and `oelse' is an optional else-branch |
| 247 |
-- |
| 248 |
closeIf headerTail (s, fragsTh) alts oelse rest = |
| 249 |
do |
| 250 |
sentryName <- newName |
| 251 |
let sentry = onlyPosIdent nopos sentryName |
| 252 |
-- don't use an internal ident, as we need to test for |
| 253 |
-- equality with identifiers read from the .i file |
| 254 |
-- during binding hook expansion |
| 255 |
header = openDL ['#':s, "\n", |
| 256 |
"struct ", sentryName, ";\n"] |
| 257 |
`joinDL` headerTail |
| 258 |
return (header, Frag (CHSCond ((sentry, fragsTh):alts) oelse), rest) |
| 259 |
|
| 260 |
|
| 261 |
-- exception handling |
| 262 |
-- ------------------ |
| 263 |
|
| 264 |
-- exception identifier |
| 265 |
-- |
| 266 |
ghExc :: String |
| 267 |
ghExc = "ghExc" |
| 268 |
|
| 269 |
-- throw an exception |
| 270 |
-- |
| 271 |
throwGHExc :: GH a |
| 272 |
throwGHExc = throwExc ghExc "Error during C header generation" |
| 273 |
|
| 274 |
-- catch a `ghExc' |
| 275 |
-- |
| 276 |
ifGHExc :: CST s a -> CST s a -> CST s a |
| 277 |
ifGHExc m handler = m `catchExc` (ghExc, const handler) |
| 278 |
|
| 279 |
-- raise an error followed by throwing a GH exception |
| 280 |
-- |
| 281 |
raiseErrorGHExc :: Position -> [String] -> GH a |
| 282 |
raiseErrorGHExc pos errs = raiseError pos errs >> throwGHExc |
| 283 |
|
| 284 |
|
| 285 |
-- error messages |
| 286 |
-- -------------- |
| 287 |
|
| 288 |
notClosedCondErr :: Position -> GH a |
| 289 |
notClosedCondErr pos = |
| 290 |
raiseErrorGHExc pos |
| 291 |
["Unexpected end of file!", |
| 292 |
"File ended while the conditional block starting here was not closed \ |
| 293 |
\properly."] |
| 294 |
|
| 295 |
notOpenCondErr :: Position -> GH a |
| 296 |
notOpenCondErr pos = |
| 297 |
raiseErrorGHExc pos |
| 298 |
["Missing #if[[n]def]!", |
| 299 |
"There is a #else, #elif, or #endif without an #if, #ifdef, or #ifndef."] |