| 1 | -- C->Haskell Compiler: interface to C processing routines |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 12 August 99 |
| 5 | -- |
| 6 | -- Version $Revision: 1.3 $ from $Date: 2005/06/22 16:01:20 $ |
| 7 | -- |
| 8 | -- Copyright (c) 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 modules provides access to the C processing routines for the rest of |
| 23 | -- the compiler. |
| 24 | -- |
| 25 | --- DOCU ---------------------------------------------------------------------- |
| 26 | -- |
| 27 | -- language: Haskell 98 |
| 28 | -- |
| 29 | --- TODO ---------------------------------------------------------------------- |
| 30 | -- |
| 31 | -- |
| 32 | |
| 33 | module C (-- interface to KL for all non-KL modules |
| 34 | -- |
| 35 | -- stuff from `Common' (reexported) |
| 36 | -- |
| 37 | Pos(posOf), |
| 38 | -- |
| 39 | -- structure tree |
| 40 | -- |
| 41 | module CAST, |
| 42 | -- |
| 43 | -- attributed structure tree with operations (reexported from |
| 44 | -- `CAttrs') |
| 45 | -- |
| 46 | AttrC, getCHeader, |
| 47 | CObj(..), CTag(..), CDef(..), lookupDefObjC, lookupDefTagC, |
| 48 | getDefOfIdentC, |
| 49 | -- |
| 50 | -- support for C structure tree traversals |
| 51 | -- |
| 52 | module CTrav, |
| 53 | -- |
| 54 | -- support for pretty printing C abstract trees |
| 55 | -- |
| 56 | module CPretty, |
| 57 | -- |
| 58 | loadAttrC, -- locally defined |
| 59 | -- |
| 60 | -- misc. reexported stuff |
| 61 | -- |
| 62 | Ident, Attrs, Attr(..), |
| 63 | -- |
| 64 | -- misc. own stuff |
| 65 | -- |
| 66 | csuffix, hsuffix, isuffix) |
| 67 | where |
| 68 | |
| 69 | import Position (Position(..), Pos(posOf)) |
| 70 | import Idents (Ident, lexemeToIdent) |
| 71 | import Attributes (Attrs, Attr(..)) |
| 72 | |
| 73 | import C2HSState (CST, IOMode(..), |
| 74 | readCST, transCST, runCST, nop, |
| 75 | readFileCIO, writeFileCIO, openFileCIO, hCloseCIO, |
| 76 | fatal, errorsPresent, showErrors, |
| 77 | Traces(..), putTraceStr) |
| 78 | import CAST |
| 79 | import CParser (parseC) |
| 80 | import CPretty |
| 81 | import CAttrs (AttrC, attrC, getCHeader, |
| 82 | CObj(..), CTag(..), CDef(..), lookupDefObjC, lookupDefTagC, |
| 83 | getDefOfIdentC) |
| 84 | import CNames (nameAnalysis) |
| 85 | import CTrav |
| 86 | |
| 87 | |
| 88 | -- suffix for files containing C (EXPORTED) |
| 89 | -- |
| 90 | csuffix, hsuffix, isuffix :: String |
| 91 | csuffix = ".c" |
| 92 | hsuffix = ".h" |
| 93 | isuffix = ".i" |
| 94 | |
| 95 | -- given a file name (with suffix), parse that file as a C header and do the |
| 96 | -- static analysis (collect defined names) (EXPORTED) |
| 97 | -- |
| 98 | -- * currently, lexical and syntactical errors are reported immediately and |
| 99 | -- abort the program; others are reported as part of the fatal error message; |
| 100 | -- warnings are returned together with the read unit |
| 101 | -- |
| 102 | loadAttrC :: String -> CST s (AttrC, String) |
| 103 | loadAttrC fname = do |
| 104 | -- read file |
| 105 | -- |
| 106 | traceInfoRead fname |
| 107 | contents <- readFileCIO fname |
| 108 | |
| 109 | -- parse |
| 110 | -- |
| 111 | traceInfoParse |
| 112 | rawHeader <- parseC contents (Position fname 1 1) |
| 113 | let header = attrC rawHeader |
| 114 | |
| 115 | -- name analysis |
| 116 | -- |
| 117 | traceInfoNA |
| 118 | headerWithAttrs <- nameAnalysis header |
| 119 | |
| 120 | -- check for errors and finalize |
| 121 | -- |
| 122 | errs <- errorsPresent |
| 123 | if errs |
| 124 | then do |
| 125 | traceInfoErr |
| 126 | errmsgs <- showErrors |
| 127 | fatal ("C header contains \ |
| 128 | \errors:\n\n" ++ errmsgs) -- fatal error |
| 129 | else do |
| 130 | traceInfoOK |
| 131 | warnmsgs <- showErrors |
| 132 | return (headerWithAttrs, warnmsgs) |
| 133 | where |
| 134 | traceInfoRead fname = putTraceStr tracePhasesSW |
| 135 | ("Attempting to read file `" |
| 136 | ++ fname ++ "'...\n") |
| 137 | traceInfoParse = putTraceStr tracePhasesSW |
| 138 | ("...parsing `" |
| 139 | ++ fname ++ "'...\n") |
| 140 | traceInfoNA = putTraceStr tracePhasesSW |
| 141 | ("...name analysis of `" |
| 142 | ++ fname ++ "'...\n") |
| 143 | traceInfoErr = putTraceStr tracePhasesSW |
| 144 | ("...error(s) detected in `" |
| 145 | ++ fname ++ "'.\n") |
| 146 | traceInfoOK = putTraceStr tracePhasesSW |
| 147 | ("...successfully loaded `" |
| 148 | ++ fname ++ "'.\n") |