| 1 | -- C->Haskell Compiler: C name analysis |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 16 October 99 |
| 5 | -- |
| 6 | -- Version $Revision: 1.2 $ from $Date: 2005/07/29 01:26:56 $ |
| 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 | -- Name analysis of C header files. |
| 23 | -- |
| 24 | --- DOCU ---------------------------------------------------------------------- |
| 25 | -- |
| 26 | -- language: Haskell 98 |
| 27 | -- |
| 28 | -- * Member names are not looked up, because this requires type information |
| 29 | -- about the expressions before the `.' or `->'. |
| 30 | -- |
| 31 | --- TODO ---------------------------------------------------------------------- |
| 32 | -- |
| 33 | -- * `defObjOrErr': currently, repeated declarations are completely ignored; |
| 34 | -- eventually, the consistency of the declarations should be checked |
| 35 | -- |
| 36 | |
| 37 | module CNames (nameAnalysis) |
| 38 | where |
| 39 | |
| 40 | import Control.Monad (when, mapM_) |
| 41 | |
| 42 | import Position (Position, posOf) |
| 43 | import Idents (Ident, identToLexeme) |
| 44 | |
| 45 | import C2HSState (CST, nop) |
| 46 | import CAST |
| 47 | import CAttrs (AttrC, CObj(..), CTag(..), CDef(..)) |
| 48 | import CBuiltin (builtinTypeNames) |
| 49 | import CTrav (CT, getCHeaderCT, runCT, enter, enterObjs, leave, leaveObjs, |
| 50 | ifCTExc, raiseErrorCTExc, defObj, findTypeObj, findValueObj, |
| 51 | defTag, refersToDef, isTypedef) |
| 52 | |
| 53 | |
| 54 | -- monad and wrapper |
| 55 | -- ----------------- |
| 56 | |
| 57 | -- local instance of the C traversal monad |
| 58 | -- |
| 59 | type NA a = CT () a |
| 60 | |
| 61 | -- name analysis of C header files (EXPORTED) |
| 62 | -- |
| 63 | nameAnalysis :: AttrC -> CST s AttrC |
| 64 | nameAnalysis ac = do |
| 65 | (ac', _) <- runCT naCHeader ac () |
| 66 | return ac' |
| 67 | |
| 68 | |
| 69 | -- name analysis traversal |
| 70 | -- ---------------------- |
| 71 | |
| 72 | -- traverse a complete header file |
| 73 | -- |
| 74 | -- * in case of an error, back off the current declaration |
| 75 | -- |
| 76 | naCHeader :: NA () |
| 77 | naCHeader = do |
| 78 | -- establish definitions for builtins |
| 79 | -- |
| 80 | mapM_ (uncurry defObjOrErr) builtinTypeNames |
| 81 | -- |
| 82 | -- analyse the header |
| 83 | -- |
| 84 | CHeader decls _ <- getCHeaderCT |
| 85 | mapM_ (\decl -> naCExtDecl decl `ifCTExc` nop) decls |
| 86 | |
| 87 | -- Processing of toplevel declarations |
| 88 | -- |
| 89 | -- * We turn function definitions into prototypes, as we are not interested in |
| 90 | -- function bodies. |
| 91 | -- |
| 92 | naCExtDecl :: CExtDecl -> NA () |
| 93 | naCExtDecl (CDeclExt decl ) = naCDecl decl |
| 94 | naCExtDecl (CFDefExt (CFunDef specs declr _ _ at)) = |
| 95 | naCDecl $ CDecl specs [(Just declr, Nothing, Nothing)] at |
| 96 | naCExtDecl (CAsmExt at ) = return () |
| 97 | |
| 98 | naCDecl :: CDecl -> NA () |
| 99 | naCDecl decl@(CDecl specs decls _) = |
| 100 | do |
| 101 | mapM_ naCDeclSpec specs |
| 102 | mapM_ naTriple decls |
| 103 | where |
| 104 | naTriple (odeclr, oinit, oexpr) = |
| 105 | do |
| 106 | let obj = if isTypedef decl then TypeCO decl else ObjCO decl |
| 107 | mapMaybeM_ (naCDeclr obj) odeclr |
| 108 | mapMaybeM_ naCInit oinit |
| 109 | mapMaybeM_ naCExpr oexpr |
| 110 | |
| 111 | naCDeclSpec :: CDeclSpec -> NA () |
| 112 | naCDeclSpec (CTypeSpec tspec) = naCTypeSpec tspec |
| 113 | naCDeclSpec _ = nop |
| 114 | |
| 115 | naCTypeSpec :: CTypeSpec -> NA () |
| 116 | naCTypeSpec (CSUType su _) = naCStructUnion (StructUnionCT su) su |
| 117 | naCTypeSpec (CEnumType enum _) = naCEnum (EnumCT enum) enum |
| 118 | naCTypeSpec (CTypeDef ide _) = do |
| 119 | (obj, _) <- findTypeObj ide False |
| 120 | ide `refersToDef` ObjCD obj |
| 121 | naCTypeSpec _ = nop |
| 122 | |
| 123 | naCStructUnion :: CTag -> CStructUnion -> NA () |
| 124 | naCStructUnion tag (CStruct _ oide decls _) = |
| 125 | do |
| 126 | mapMaybeM_ (`defTagOrErr` tag) oide |
| 127 | enterObjs -- enter local struct range for objects |
| 128 | mapM_ naCDecl decls |
| 129 | leaveObjs -- leave range |
| 130 | |
| 131 | naCEnum :: CTag -> CEnum -> NA () |
| 132 | naCEnum tag enum@(CEnum oide enumrs _) = |
| 133 | do |
| 134 | mapMaybeM_ (`defTagOrErr` tag) oide |
| 135 | mapM_ naEnumr enumrs |
| 136 | where |
| 137 | naEnumr (ide, oexpr) = do |
| 138 | ide `defObjOrErr` EnumCO ide enum |
| 139 | mapMaybeM_ naCExpr oexpr |
| 140 | |
| 141 | naCDeclr :: CObj -> CDeclr -> NA () |
| 142 | naCDeclr obj (CVarDeclr oide _) = |
| 143 | mapMaybeM_ (`defObjOrErr` obj) oide |
| 144 | naCDeclr obj (CPtrDeclr _ declr _ ) = |
| 145 | naCDeclr obj declr |
| 146 | naCDeclr obj (CArrDeclr declr _ oexpr _ ) = |
| 147 | do |
| 148 | naCDeclr obj declr |
| 149 | mapMaybeM_ naCExpr oexpr |
| 150 | naCDeclr obj (CFunDeclr declr decls _ _ ) = |
| 151 | do |
| 152 | naCDeclr obj declr |
| 153 | enterObjs -- enter range of function arguments |
| 154 | mapM_ naCDecl decls |
| 155 | leaveObjs -- end of function arguments |
| 156 | |
| 157 | naCInit :: CInit -> NA () |
| 158 | naCInit (CInitExpr expr _) = naCExpr expr |
| 159 | naCInit (CInitList inits _) = mapM_ (naCInit . snd) inits |
| 160 | |
| 161 | naCExpr :: CExpr -> NA () |
| 162 | naCExpr (CComma exprs _) = mapM_ naCExpr exprs |
| 163 | naCExpr (CAssign _ expr1 expr2 _) = naCExpr expr1 >> naCExpr expr2 |
| 164 | naCExpr (CCond expr1 expr2 expr3 _) = naCExpr expr1 >> mapMaybeM_ naCExpr expr2 |
| 165 | >> naCExpr expr3 |
| 166 | naCExpr (CBinary _ expr1 expr2 _) = naCExpr expr1 >> naCExpr expr2 |
| 167 | naCExpr (CCast decl expr _) = naCDecl decl >> naCExpr expr |
| 168 | naCExpr (CUnary _ expr _) = naCExpr expr |
| 169 | naCExpr (CSizeofExpr expr _) = naCExpr expr |
| 170 | naCExpr (CSizeofType decl _) = naCDecl decl |
| 171 | naCExpr (CAlignofExpr expr _) = naCExpr expr |
| 172 | naCExpr (CAlignofType decl _) = naCDecl decl |
| 173 | naCExpr (CIndex expr1 expr2 _) = naCExpr expr1 >> naCExpr expr2 |
| 174 | naCExpr (CCall expr exprs _) = naCExpr expr >> mapM_ naCExpr exprs |
| 175 | naCExpr (CMember expr ide _ _) = naCExpr expr |
| 176 | naCExpr (CVar ide _) = do |
| 177 | (obj, _) <- findValueObj ide False |
| 178 | ide `refersToDef` ObjCD obj |
| 179 | naCExpr (CConst _ _) = nop |
| 180 | naCExpr (CCompoundLit _ inits _) = mapM_ (naCInit . snd) inits |
| 181 | |
| 182 | |
| 183 | -- auxiliary functions |
| 184 | -- -------------------- |
| 185 | |
| 186 | -- raise an error and exception if the identifier is defined twice |
| 187 | -- |
| 188 | defTagOrErr :: Ident -> CTag -> NA () |
| 189 | ide `defTagOrErr` tag = do |
| 190 | otag <- ide `defTag` tag |
| 191 | case otag of |
| 192 | Nothing -> nop |
| 193 | Just tag' -> declaredTwiceErr ide (posOf tag') |
| 194 | |
| 195 | -- associate an object with a referring identifier |
| 196 | -- |
| 197 | -- * currently, repeated declarations are completely ignored; eventually, the |
| 198 | -- consistency of the declarations should be checked |
| 199 | -- |
| 200 | defObjOrErr :: Ident -> CObj -> NA () |
| 201 | ide `defObjOrErr` obj = ide `defObj` obj >> nop |
| 202 | |
| 203 | -- maps some monad operation into a `Maybe', discarding the result |
| 204 | -- |
| 205 | mapMaybeM_ :: Monad m => (a -> m b) -> Maybe a -> m () |
| 206 | mapMaybeM_ m Nothing = return () |
| 207 | mapMaybeM_ m (Just a) = m a >> return () |
| 208 | |
| 209 | |
| 210 | -- error messages |
| 211 | -- -------------- |
| 212 | |
| 213 | declaredTwiceErr :: Ident -> Position -> NA a |
| 214 | declaredTwiceErr ide otherPos = |
| 215 | raiseErrorCTExc (posOf ide) |
| 216 | ["Identifier declared twice!", |
| 217 | "The identifier `" ++ identToLexeme ide ++ "' was already declared at " |
| 218 | ++ show otherPos ++ "."] |