| 1 | -- C -> Haskell Compiler: Abstract Syntax for Header Files |
| 2 | -- |
| 3 | -- Author : Manuel M T Chakravarty |
| 4 | -- Created: 7 March 99 |
| 5 | -- |
| 6 | -- Version $Revision: 1.10 $ from $Date: 2004/06/11 07:10:16 $ |
| 7 | -- |
| 8 | -- Copyright (c) [1999..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 | -- Abstract syntax of C header files. |
| 23 | -- |
| 24 | --- DOCU ---------------------------------------------------------------------- |
| 25 | -- |
| 26 | -- language: Haskell 98 |
| 27 | -- |
| 28 | -- The tree structure corresponds to the grammar in Appendix A of K&R. This |
| 29 | -- abstract syntax simplifies the concrete syntax by merging similar concrete |
| 30 | -- constructs into a single type of abstract tree structure: declarations are |
| 31 | -- merged with structure declarations, parameter declarations and type names, |
| 32 | -- and declarators are merged with abstract declarators. |
| 33 | -- |
| 34 | -- With K&R we refer to ``The C Programming Language'', second edition, Brain |
| 35 | -- W. Kernighan and Dennis M. Ritchie, Prentice Hall, 1988. This module |
| 36 | -- supports the C99 `restrict' extension |
| 37 | -- <http://www.lysator.liu.se/c/restrict.html>, `inline' functions, and also |
| 38 | -- the GNU C `alignof' extension. |
| 39 | -- |
| 40 | --- TODO ---------------------------------------------------------------------- |
| 41 | -- |
| 42 | |
| 43 | module CAST (CHeader(..), CExtDecl(..), CFunDef(..), CStat(..), CBlockItem(..), |
| 44 | CDecl(..), CDeclSpec(..), CStorageSpec(..), CTypeSpec(..), |
| 45 | CTypeQual(..), CStructUnion(..), CStructTag(..), CEnum(..), |
| 46 | CDeclr(..), CInit(..), CInitList, CDesignator(..), CExpr(..), |
| 47 | CAssignOp(..), CBinaryOp(..), CUnaryOp(..), CConst (..)) |
| 48 | where |
| 49 | |
| 50 | import Position (Position, Pos(posOf), nopos) |
| 51 | import Idents (Ident) |
| 52 | import Attributes (Attrs) |
| 53 | import Binary (Binary(..), putByte, getByte) |
| 54 | |
| 55 | |
| 56 | -- a complete C header file (K&R A10) (EXPORTED) |
| 57 | -- |
| 58 | data CHeader = CHeader [CExtDecl] |
| 59 | Attrs |
| 60 | |
| 61 | instance Pos CHeader where |
| 62 | posOf (CHeader _ at) = posOf at |
| 63 | |
| 64 | instance Eq CHeader where |
| 65 | (CHeader _ at1) == (CHeader _ at2) = at1 == at2 |
| 66 | |
| 67 | -- external C declaration (K&R A10) (EXPORTED) |
| 68 | -- |
| 69 | data CExtDecl = CDeclExt CDecl |
| 70 | | CFDefExt CFunDef |
| 71 | | CAsmExt Attrs -- a chunk of assembly code (which is |
| 72 | -- not itself recorded) |
| 73 | |
| 74 | instance Pos CExtDecl where |
| 75 | posOf (CDeclExt decl) = posOf decl |
| 76 | posOf (CFDefExt fdef) = posOf fdef |
| 77 | posOf (CAsmExt at) = posOf at |
| 78 | |
| 79 | instance Eq CExtDecl where |
| 80 | CDeclExt decl1 == CDeclExt decl2 = decl1 == decl2 |
| 81 | CFDefExt fdef1 == CFDefExt fdef2 = fdef1 == fdef2 |
| 82 | CAsmExt at1 == CAsmExt at2 = at1 == at2 |
| 83 | |
| 84 | -- C function definition (K&R A10.1) (EXPORTED) |
| 85 | -- |
| 86 | -- * The only type specifiers allowed are `extern' and `static'. |
| 87 | -- |
| 88 | -- * The declarator must specify explicitly that the declared identifier has |
| 89 | -- function type. |
| 90 | -- |
| 91 | -- * The optional declaration list is for old-style function declarations. |
| 92 | -- |
| 93 | -- * The statement must be a compound statement. |
| 94 | -- |
| 95 | data CFunDef = CFunDef [CDeclSpec] -- type specifier and qualifier |
| 96 | CDeclr -- declarator |
| 97 | [CDecl] -- optional declaration list |
| 98 | CStat -- compound statement |
| 99 | Attrs |
| 100 | |
| 101 | instance Pos CFunDef where |
| 102 | posOf (CFunDef _ _ _ _ at) = posOf at |
| 103 | |
| 104 | instance Eq CFunDef where |
| 105 | CFunDef _ _ _ _ at1 == CFunDef _ _ _ _ at2 = at1 == at2 |
| 106 | |
| 107 | -- C statement (A9) (EXPORTED) |
| 108 | -- |
| 109 | data CStat = CLabel Ident -- label |
| 110 | CStat |
| 111 | Attrs |
| 112 | | CCase CExpr -- constant expression |
| 113 | CStat |
| 114 | Attrs |
| 115 | | CCases CExpr -- case range |
| 116 | CExpr -- `case lower .. upper :' |
| 117 | CStat |
| 118 | Attrs |
| 119 | | CDefault CStat -- default case |
| 120 | Attrs |
| 121 | | CExpr (Maybe CExpr) -- expression statement, maybe empty |
| 122 | Attrs |
| 123 | | CCompound [CBlockItem] -- list of declarations and statements |
| 124 | Attrs |
| 125 | | CIf CExpr -- conditional expression |
| 126 | CStat |
| 127 | (Maybe CStat) -- optional "else" case |
| 128 | Attrs |
| 129 | | CSwitch CExpr -- selector |
| 130 | CStat |
| 131 | Attrs |
| 132 | | CWhile CExpr |
| 133 | CStat |
| 134 | Bool -- `True' implies "do-while" statement |
| 135 | Attrs |
| 136 | | CFor (Either (Maybe CExpr) |
| 137 | CDecl) |
| 138 | (Maybe CExpr) |
| 139 | (Maybe CExpr) |
| 140 | CStat |
| 141 | Attrs |
| 142 | | CGoto Ident -- label |
| 143 | Attrs |
| 144 | | CGotoPtr CExpr -- computed address |
| 145 | Attrs |
| 146 | | CCont Attrs -- continue statement |
| 147 | | CBreak Attrs -- break statement |
| 148 | | CReturn (Maybe CExpr) |
| 149 | Attrs |
| 150 | | CAsm Attrs -- a chunk of assembly code (which is |
| 151 | -- not itself recorded) |
| 152 | |
| 153 | instance Pos CStat where |
| 154 | posOf (CLabel _ _ at) = posOf at |
| 155 | posOf (CCase _ _ at) = posOf at |
| 156 | posOf (CCases _ _ _ at) = posOf at |
| 157 | posOf (CDefault _ at) = posOf at |
| 158 | posOf (CExpr _ at) = posOf at |
| 159 | posOf (CCompound _ at) = posOf at |
| 160 | posOf (CIf _ _ _ at) = posOf at |
| 161 | posOf (CSwitch _ _ at) = posOf at |
| 162 | posOf (CWhile _ _ _ at) = posOf at |
| 163 | posOf (CFor _ _ _ _ at) = posOf at |
| 164 | posOf (CGoto _ at) = posOf at |
| 165 | posOf (CGotoPtr _ at) = posOf at |
| 166 | posOf (CCont at) = posOf at |
| 167 | posOf (CBreak at) = posOf at |
| 168 | posOf (CReturn _ at) = posOf at |
| 169 | posOf (CAsm at) = posOf at |
| 170 | |
| 171 | instance Eq CStat where |
| 172 | (CLabel _ _ at1) == (CLabel _ _ at2) = at1 == at2 |
| 173 | (CCase _ _ at1) == (CCase _ _ at2) = at1 == at2 |
| 174 | (CCases _ _ _ at1) == (CCases _ _ _ at2) = at1 == at2 |
| 175 | (CDefault _ at1) == (CDefault _ at2) = at1 == at2 |
| 176 | (CExpr _ at1) == (CExpr _ at2) = at1 == at2 |
| 177 | (CCompound _ at1) == (CCompound _ at2) = at1 == at2 |
| 178 | (CIf _ _ _ at1) == (CIf _ _ _ at2) = at1 == at2 |
| 179 | (CSwitch _ _ at1) == (CSwitch _ _ at2) = at1 == at2 |
| 180 | (CWhile _ _ _ at1) == (CWhile _ _ _ at2) = at1 == at2 |
| 181 | (CFor _ _ _ _ at1) == (CFor _ _ _ _ at2) = at1 == at2 |
| 182 | (CGoto _ at1) == (CGoto _ at2) = at1 == at2 |
| 183 | (CGotoPtr _ at1) == (CGotoPtr _ at2) = at1 == at2 |
| 184 | (CCont at1) == (CCont at2) = at1 == at2 |
| 185 | (CBreak at1) == (CBreak at2) = at1 == at2 |
| 186 | (CReturn _ at1) == (CReturn _ at2) = at1 == at2 |
| 187 | (CAsm at1) == (CAsm at2) = at1 == at2 |
| 188 | |
| 189 | -- C99 Block items, things that may appear in compound statements |
| 190 | data CBlockItem = CBlockStmt CStat |
| 191 | | CBlockDecl CDecl |
| 192 | | CNestedFunDef CFunDef -- GNU C has nested functions |
| 193 | |
| 194 | instance Pos CBlockItem where |
| 195 | posOf (CBlockStmt stmt) = posOf stmt |
| 196 | posOf (CBlockDecl decl) = posOf decl |
| 197 | posOf (CNestedFunDef fdef) = posOf fdef |
| 198 | |
| 199 | instance Eq CBlockItem where |
| 200 | CBlockStmt stmt1 == CBlockStmt stmt2 = stmt1 == stmt2 |
| 201 | CBlockDecl decl1 == CBlockDecl decl2 = decl1 == decl2 |
| 202 | CNestedFunDef fdef1 == CNestedFunDef fdef2 = fdef1 == fdef2 |
| 203 | |
| 204 | |
| 205 | -- C declaration (K&R A8), structure declaration (K&R A8.3), parameter |
| 206 | -- declaration (K&R A8.6.3), and type name (K&R A8.8) (EXPORTED) |
| 207 | -- |
| 208 | -- * Toplevel declarations (K&R A8): |
| 209 | -- |
| 210 | -- - they require that the type specifier and qualifier list is not empty, |
| 211 | -- but gcc allows it and just issues a warning; for the time being, we |
| 212 | -- also allow it; |
| 213 | -- - at most one storage class specifier is allowed per declaration; |
| 214 | -- - declarators must be present and size expressions are not allowed, ie, |
| 215 | -- the elements of K&R's init-declarator-list are represented by triples |
| 216 | -- of the form `(Just declr, oinit, Nothing)', where `oinit' maybe |
| 217 | -- `Nothing' or `Just init'; and |
| 218 | -- - abstract declarators are not allowed. |
| 219 | -- |
| 220 | -- * Structure declarations (K&R A8.3): |
| 221 | -- |
| 222 | -- - do not allow storage specifiers; |
| 223 | -- - do not allow initializers; |
| 224 | -- - require a non-empty declarator-triple list, where abstract declarators |
| 225 | -- are not allowed; and |
| 226 | -- - each of the declarator-triples has to contain either a declarator or a |
| 227 | -- size expression, or both, ie, it has the form `(Just decl, Nothing, |
| 228 | -- Nothing)', `(Nothing, Nothing, Just size)', or `(Just decl, Nothing, |
| 229 | -- Just size)'. |
| 230 | -- |
| 231 | -- * Parameter declarations (K&R A8.6.3): |
| 232 | -- |
| 233 | -- - allow neither initializers nor size expressions; |
| 234 | -- - allow at most one declarator triple of the form `(Just declr, Nothing, |
| 235 | -- Nothing)' (in case of an empty declarator, the list must be empty); and |
| 236 | -- - allow abstract declarators. |
| 237 | -- |
| 238 | -- * Type names (A8.8): |
| 239 | -- |
| 240 | -- - do not allow storage specifiers; |
| 241 | -- - allow neither initializers nor size expressions; and |
| 242 | -- - allow at most one declarator triple of the form `(Just declr, Nothing, |
| 243 | -- Nothing)' (in case of an empty declarator, the list must be empty), |
| 244 | -- where the declarator must be abstract, ie, must not contain a declared |
| 245 | -- identifier. |
| 246 | -- |
| 247 | data CDecl = CDecl [CDeclSpec] -- type specifier and qualifier |
| 248 | [(Maybe CDeclr, -- declarator (may be omitted) |
| 249 | Maybe CInit, -- optional initializer |
| 250 | Maybe CExpr)] -- optional size (const expr) |
| 251 | Attrs |
| 252 | |
| 253 | instance Pos CDecl where |
| 254 | posOf (CDecl _ _ at) = posOf at |
| 255 | |
| 256 | instance Eq CDecl where |
| 257 | (CDecl _ _ at1) == (CDecl _ _ at2) = at1 == at2 |
| 258 | |
| 259 | -- C declaration specifiers and qualifiers (EXPORTED) |
| 260 | -- |
| 261 | data CDeclSpec = CStorageSpec CStorageSpec |
| 262 | | CTypeSpec CTypeSpec |
| 263 | | CTypeQual CTypeQual |
| 264 | deriving (Eq) |
| 265 | |
| 266 | instance Pos CDeclSpec where |
| 267 | posOf (CStorageSpec sspec) = posOf sspec |
| 268 | posOf (CTypeSpec tspec) = posOf tspec |
| 269 | posOf (CTypeQual tqual) = posOf tqual |
| 270 | |
| 271 | -- C storage class specifier (K&R A8.1) (EXPORTED) |
| 272 | -- |
| 273 | data CStorageSpec = CAuto Attrs |
| 274 | | CRegister Attrs |
| 275 | | CStatic Attrs |
| 276 | | CExtern Attrs |
| 277 | | CTypedef Attrs -- syntactic awkwardness of C |
| 278 | | CThread Attrs -- GNUC thread local storage |
| 279 | |
| 280 | instance Pos CStorageSpec where |
| 281 | posOf (CAuto at) = posOf at |
| 282 | posOf (CRegister at) = posOf at |
| 283 | posOf (CStatic at) = posOf at |
| 284 | posOf (CExtern at) = posOf at |
| 285 | posOf (CTypedef at) = posOf at |
| 286 | posOf (CThread at) = posOf at |
| 287 | |
| 288 | instance Eq CStorageSpec where |
| 289 | (CAuto at1) == (CAuto at2) = at1 == at2 |
| 290 | (CRegister at1) == (CRegister at2) = at1 == at2 |
| 291 | (CStatic at1) == (CStatic at2) = at1 == at2 |
| 292 | (CExtern at1) == (CExtern at2) = at1 == at2 |
| 293 | (CTypedef at1) == (CTypedef at2) = at1 == at2 |
| 294 | (CThread at1) == (CThread at2) = at1 == at2 |
| 295 | |
| 296 | -- C type specifier (K&R A8.2) (EXPORTED) |
| 297 | -- |
| 298 | data CTypeSpec = CVoidType Attrs |
| 299 | | CCharType Attrs |
| 300 | | CShortType Attrs |
| 301 | | CIntType Attrs |
| 302 | | CLongType Attrs |
| 303 | | CFloatType Attrs |
| 304 | | CFloat128Type Attrs |
| 305 | | CDoubleType Attrs |
| 306 | | CSignedType Attrs |
| 307 | | CUnsigType Attrs |
| 308 | | CBoolType Attrs |
| 309 | | CComplexType Attrs |
| 310 | | CSUType CStructUnion |
| 311 | Attrs |
| 312 | | CEnumType CEnum |
| 313 | Attrs |
| 314 | | CTypeDef Ident -- typedef name |
| 315 | Attrs |
| 316 | | CTypeOfExpr CExpr |
| 317 | Attrs |
| 318 | | CTypeOfType CDecl |
| 319 | Attrs |
| 320 | |
| 321 | instance Pos CTypeSpec where |
| 322 | posOf (CVoidType at) = posOf at |
| 323 | posOf (CCharType at) = posOf at |
| 324 | posOf (CShortType at) = posOf at |
| 325 | posOf (CIntType at) = posOf at |
| 326 | posOf (CLongType at) = posOf at |
| 327 | posOf (CFloatType at) = posOf at |
| 328 | posOf (CFloat128Type at) = posOf at |
| 329 | posOf (CDoubleType at) = posOf at |
| 330 | posOf (CSignedType at) = posOf at |
| 331 | posOf (CUnsigType at) = posOf at |
| 332 | posOf (CBoolType at) = posOf at |
| 333 | posOf (CComplexType at) = posOf at |
| 334 | posOf (CSUType _ at) = posOf at |
| 335 | posOf (CEnumType _ at) = posOf at |
| 336 | posOf (CTypeDef _ at) = posOf at |
| 337 | posOf (CTypeOfExpr _ at) = posOf at |
| 338 | posOf (CTypeOfType _ at) = posOf at |
| 339 | |
| 340 | instance Eq CTypeSpec where |
| 341 | (CVoidType at1) == (CVoidType at2) = at1 == at2 |
| 342 | (CCharType at1) == (CCharType at2) = at1 == at2 |
| 343 | (CShortType at1) == (CShortType at2) = at1 == at2 |
| 344 | (CIntType at1) == (CIntType at2) = at1 == at2 |
| 345 | (CLongType at1) == (CLongType at2) = at1 == at2 |
| 346 | (CFloatType at1) == (CFloatType at2) = at1 == at2 |
| 347 | (CFloat128Type at1) == (CFloat128Type at2) = at1 == at2 |
| 348 | (CDoubleType at1) == (CDoubleType at2) = at1 == at2 |
| 349 | (CSignedType at1) == (CSignedType at2) = at1 == at2 |
| 350 | (CUnsigType at1) == (CUnsigType at2) = at1 == at2 |
| 351 | (CBoolType at1) == (CBoolType at2) = at1 == at2 |
| 352 | (CComplexType at1) == (CComplexType at2) = at1 == at2 |
| 353 | (CSUType _ at1) == (CSUType _ at2) = at1 == at2 |
| 354 | (CEnumType _ at1) == (CEnumType _ at2) = at1 == at2 |
| 355 | (CTypeDef _ at1) == (CTypeDef _ at2) = at1 == at2 |
| 356 | (CTypeOfExpr _ at1) == (CTypeOfExpr _ at2) = at1 == at2 |
| 357 | (CTypeOfType _ at1) == (CTypeOfType _ at2) = at1 == at2 |
| 358 | |
| 359 | -- C type qualifier (K&R A8.2) (EXPORTED) |
| 360 | -- |
| 361 | -- * plus `restrict' from C99 and `inline' |
| 362 | -- |
| 363 | data CTypeQual = CConstQual Attrs |
| 364 | | CVolatQual Attrs |
| 365 | | CRestrQual Attrs |
| 366 | | CInlinQual Attrs |
| 367 | |
| 368 | instance Pos CTypeQual where |
| 369 | posOf (CConstQual at) = posOf at |
| 370 | posOf (CVolatQual at) = posOf at |
| 371 | posOf (CRestrQual at) = posOf at |
| 372 | posOf (CInlinQual at) = posOf at |
| 373 | |
| 374 | instance Eq CTypeQual where |
| 375 | (CConstQual at1) == (CConstQual at2) = at1 == at2 |
| 376 | (CVolatQual at1) == (CVolatQual at2) = at1 == at2 |
| 377 | (CRestrQual at1) == (CRestrQual at2) = at1 == at2 |
| 378 | (CInlinQual at1) == (CInlinQual at2) = at1 == at2 |
| 379 | |
| 380 | -- C structure of union declaration (K&R A8.3) (EXPORTED) |
| 381 | -- |
| 382 | -- * in both case, either the identifier is present or the list must be |
| 383 | -- non-empty |
| 384 | -- |
| 385 | data CStructUnion = CStruct CStructTag |
| 386 | (Maybe Ident) |
| 387 | [CDecl] -- *structure* declaration |
| 388 | Attrs |
| 389 | |
| 390 | instance Pos CStructUnion where |
| 391 | posOf (CStruct _ _ _ at) = posOf at |
| 392 | |
| 393 | instance Eq CStructUnion where |
| 394 | (CStruct _ _ _ at1) == (CStruct _ _ _ at2) = at1 == at2 |
| 395 | |
| 396 | -- (EXPORTED) |
| 397 | -- |
| 398 | data CStructTag = CStructTag |
| 399 | | CUnionTag |
| 400 | deriving (Eq) |
| 401 | |
| 402 | -- C enumeration declaration (K&R A8.4) (EXPORTED) |
| 403 | -- |
| 404 | data CEnum = CEnum (Maybe Ident) |
| 405 | [(Ident, -- variant name |
| 406 | Maybe CExpr)] -- explicit variant value |
| 407 | Attrs |
| 408 | |
| 409 | instance Pos CEnum where |
| 410 | posOf (CEnum _ _ at) = posOf at |
| 411 | |
| 412 | instance Eq CEnum where |
| 413 | (CEnum _ _ at1) == (CEnum _ _ at2) = at1 == at2 |
| 414 | |
| 415 | -- C declarator (K&R A8.5) and abstract declarator (K&R A8.8) (EXPORTED) |
| 416 | -- |
| 417 | -- * We have one type qualifier list `[CTypeQual]' for each indirection (ie, |
| 418 | -- each occurrence of `*' in the concrete syntax). |
| 419 | -- |
| 420 | -- * We unfold K&R's direct-declarators nonterminal into declarators. Note |
| 421 | -- that `*(*x)' is equivalent to `**x'. |
| 422 | -- |
| 423 | -- * Declarators (A8.5) and abstract declarators (A8.8) are represented in the |
| 424 | -- same structure. In the case of a declarator, the identifier in |
| 425 | -- `CVarDeclr' must be present; in an abstract declarator it misses. |
| 426 | -- `CVarDeclr Nothing ...' on its own is meaningless, it may only occur as |
| 427 | -- part of a larger type (ie, there must be a pointer, an array, or function |
| 428 | -- declarator around). |
| 429 | -- |
| 430 | -- * The qualifiers list in a `CPtrDeclr' may not be empty. |
| 431 | -- |
| 432 | -- * Old and new style function definitions are merged into a single case |
| 433 | -- `CFunDeclr'. In case of an old style definition, the parameter list is |
| 434 | -- empty and the variadic flag is `False' (ie, the parameter names are not |
| 435 | -- stored in the tree). Remember, a new style definition with no parameters |
| 436 | -- requires a single `void' in the argument list (according to the standard). |
| 437 | -- |
| 438 | -- * We unfold K&R's parameter-type-list nonterminal into the declarator |
| 439 | -- variant for functions. |
| 440 | -- |
| 441 | data CDeclr = CVarDeclr (Maybe Ident) -- declared identifier |
| 442 | Attrs |
| 443 | | CPtrDeclr [CTypeQual] -- indirections |
| 444 | CDeclr |
| 445 | Attrs |
| 446 | | CArrDeclr CDeclr |
| 447 | [CTypeQual] |
| 448 | (Maybe CExpr) -- array size |
| 449 | Attrs |
| 450 | | CFunDeclr CDeclr |
| 451 | [CDecl] -- *parameter* declarations |
| 452 | Bool -- is variadic? |
| 453 | Attrs |
| 454 | |
| 455 | instance Pos CDeclr where |
| 456 | posOf (CVarDeclr _ at) = posOf at |
| 457 | posOf (CPtrDeclr _ _ at) = posOf at |
| 458 | posOf (CArrDeclr _ _ _ at) = posOf at |
| 459 | posOf (CFunDeclr _ _ _ at) = posOf at |
| 460 | |
| 461 | instance Eq CDeclr where |
| 462 | (CVarDeclr _ at1) == (CVarDeclr _ at2) = at1 == at2 |
| 463 | (CPtrDeclr _ _ at1) == (CPtrDeclr _ _ at2) = at1 == at2 |
| 464 | (CArrDeclr _ _ _ at1) == (CArrDeclr _ _ _ at2) = at1 == at2 |
| 465 | (CFunDeclr _ _ _ at1) == (CFunDeclr _ _ _ at2) = at1 == at2 |
| 466 | |
| 467 | -- C initializer (K&R A8.7) (EXPORTED) |
| 468 | -- |
| 469 | data CInit = CInitExpr CExpr |
| 470 | Attrs -- assignment expression |
| 471 | | CInitList CInitList |
| 472 | Attrs |
| 473 | |
| 474 | type CInitList = [([CDesignator], CInit)] |
| 475 | |
| 476 | instance Pos CInit where |
| 477 | posOf (CInitExpr _ at) = posOf at |
| 478 | posOf (CInitList _ at) = posOf at |
| 479 | |
| 480 | instance Eq CInit where |
| 481 | (CInitExpr _ at1) == (CInitExpr _ at2) = at1 == at2 |
| 482 | (CInitList _ at1) == (CInitList _ at2) = at1 == at2 |
| 483 | |
| 484 | -- C initializer designator (EXPORTED) |
| 485 | -- |
| 486 | data CDesignator = CArrDesig CExpr |
| 487 | Attrs |
| 488 | | CMemberDesig Ident |
| 489 | Attrs |
| 490 | | CRangeDesig CExpr -- GNUC array range designator |
| 491 | CExpr |
| 492 | Attrs |
| 493 | |
| 494 | instance Pos CDesignator where |
| 495 | posOf (CArrDesig _ at) = posOf at |
| 496 | posOf (CMemberDesig _ at) = posOf at |
| 497 | posOf (CRangeDesig _ _ at) = posOf at |
| 498 | |
| 499 | instance Eq CDesignator where |
| 500 | (CArrDesig _ at1) == (CArrDesig _ at2) = at1 == at2 |
| 501 | (CMemberDesig _ at1) == (CMemberDesig _ at2) = at1 == at2 |
| 502 | (CRangeDesig _ _ at1) == (CRangeDesig _ _ at2) = at1 == at2 |
| 503 | |
| 504 | -- C expression (K&R A7) (EXPORTED) |
| 505 | -- |
| 506 | -- * these can be arbitrary expression, as the argument of `sizeof' can be |
| 507 | -- arbitrary, even if appearing in a constant expression |
| 508 | -- |
| 509 | -- * GNU C extension: `alignof' |
| 510 | -- |
| 511 | data CExpr = CComma [CExpr] -- comma expression list, n >= 2 |
| 512 | Attrs |
| 513 | | CAssign CAssignOp -- assignment operator |
| 514 | CExpr -- l-value |
| 515 | CExpr -- r-value |
| 516 | Attrs |
| 517 | | CCond CExpr -- conditional |
| 518 | (Maybe CExpr) -- true-expression (GNU allows omitting) |
| 519 | CExpr -- false-expression |
| 520 | Attrs |
| 521 | | CBinary CBinaryOp -- binary operator |
| 522 | CExpr -- lhs |
| 523 | CExpr -- rhs |
| 524 | Attrs |
| 525 | | CCast CDecl -- type name |
| 526 | CExpr |
| 527 | Attrs |
| 528 | | CUnary CUnaryOp -- unary operator |
| 529 | CExpr |
| 530 | Attrs |
| 531 | | CSizeofExpr CExpr |
| 532 | Attrs |
| 533 | | CSizeofType CDecl -- type name |
| 534 | Attrs |
| 535 | | CAlignofExpr CExpr |
| 536 | Attrs |
| 537 | | CAlignofType CDecl -- type name |
| 538 | Attrs |
| 539 | | CIndex CExpr -- array |
| 540 | CExpr -- index |
| 541 | Attrs |
| 542 | | CCall CExpr -- function |
| 543 | [CExpr] -- arguments |
| 544 | Attrs |
| 545 | | CMember CExpr -- structure |
| 546 | Ident -- member name |
| 547 | Bool -- deref structure? (True for `->') |
| 548 | Attrs |
| 549 | | CVar Ident -- identifier (incl. enumeration const) |
| 550 | Attrs |
| 551 | | CConst CConst -- includes strings |
| 552 | Attrs |
| 553 | | CCompoundLit CDecl -- C99 compound literal |
| 554 | CInitList -- type name & initialiser list |
| 555 | Attrs |
| 556 | | CStatExpr CStat -- GNUC compound statement as expr |
| 557 | Attrs |
| 558 | | CLabAddrExpr Ident -- GNUC address of label |
| 559 | Attrs |
| 560 | | CBuiltinExpr Attrs -- place holder for GNUC builtin exprs |
| 561 | |
| 562 | instance Pos CExpr where |
| 563 | posOf (CComma _ at) = posOf at |
| 564 | posOf (CAssign _ _ _ at) = posOf at |
| 565 | posOf (CCond _ _ _ at) = posOf at |
| 566 | posOf (CBinary _ _ _ at) = posOf at |
| 567 | posOf (CCast _ _ at) = posOf at |
| 568 | posOf (CUnary _ _ at) = posOf at |
| 569 | posOf (CSizeofExpr _ at) = posOf at |
| 570 | posOf (CSizeofType _ at) = posOf at |
| 571 | posOf (CAlignofExpr _ at) = posOf at |
| 572 | posOf (CAlignofType _ at) = posOf at |
| 573 | posOf (CIndex _ _ at) = posOf at |
| 574 | posOf (CCall _ _ at) = posOf at |
| 575 | posOf (CMember _ _ _ at) = posOf at |
| 576 | posOf (CVar _ at) = posOf at |
| 577 | posOf (CConst _ at) = posOf at |
| 578 | posOf (CCompoundLit _ _ at) = posOf at |
| 579 | posOf (CStatExpr _ at) = posOf at |
| 580 | posOf (CLabAddrExpr _ at) = posOf at |
| 581 | posOf (CBuiltinExpr at) = posOf at |
| 582 | |
| 583 | instance Eq CExpr where |
| 584 | (CComma _ at1) == (CComma _ at2) = at1 == at2 |
| 585 | (CAssign _ _ _ at1) == (CAssign _ _ _ at2) = at1 == at2 |
| 586 | (CCond _ _ _ at1) == (CCond _ _ _ at2) = at1 == at2 |
| 587 | (CBinary _ _ _ at1) == (CBinary _ _ _ at2) = at1 == at2 |
| 588 | (CCast _ _ at1) == (CCast _ _ at2) = at1 == at2 |
| 589 | (CUnary _ _ at1) == (CUnary _ _ at2) = at1 == at2 |
| 590 | (CSizeofExpr _ at1) == (CSizeofExpr _ at2) = at1 == at2 |
| 591 | (CSizeofType _ at1) == (CSizeofType _ at2) = at1 == at2 |
| 592 | (CAlignofExpr _ at1) == (CAlignofExpr _ at2) = at1 == at2 |
| 593 | (CAlignofType _ at1) == (CAlignofType _ at2) = at1 == at2 |
| 594 | (CIndex _ _ at1) == (CIndex _ _ at2) = at1 == at2 |
| 595 | (CCall _ _ at1) == (CCall _ _ at2) = at1 == at2 |
| 596 | (CMember _ _ _ at1) == (CMember _ _ _ at2) = at1 == at2 |
| 597 | (CVar _ at1) == (CVar _ at2) = at1 == at2 |
| 598 | (CConst _ at1) == (CConst _ at2) = at1 == at2 |
| 599 | (CCompoundLit _ _ at1) == (CCompoundLit _ _ at2) = at1 == at2 |
| 600 | (CStatExpr _ at1) == (CStatExpr _ at2) = at1 == at2 |
| 601 | (CLabAddrExpr _ at1) == (CLabAddrExpr _ at2) = at1 == at2 |
| 602 | (CBuiltinExpr at1) == (CBuiltinExpr at2) = at1 == at2 |
| 603 | |
| 604 | -- C assignment operators (K&R A7.17) (EXPORTED) |
| 605 | -- |
| 606 | data CAssignOp = CAssignOp |
| 607 | | CMulAssOp |
| 608 | | CDivAssOp |
| 609 | | CRmdAssOp -- remainder and assignment |
| 610 | | CAddAssOp |
| 611 | | CSubAssOp |
| 612 | | CShlAssOp |
| 613 | | CShrAssOp |
| 614 | | CAndAssOp |
| 615 | | CXorAssOp |
| 616 | | COrAssOp |
| 617 | deriving (Eq) |
| 618 | |
| 619 | -- C binary operators (K&R A7.6-15) (EXPORTED) |
| 620 | -- |
| 621 | data CBinaryOp = CMulOp |
| 622 | | CDivOp |
| 623 | | CRmdOp -- remainder of division |
| 624 | | CAddOp |
| 625 | | CSubOp |
| 626 | | CShlOp -- shift left |
| 627 | | CShrOp -- shift right |
| 628 | | CLeOp -- less |
| 629 | | CGrOp -- greater |
| 630 | | CLeqOp -- less or equal |
| 631 | | CGeqOp -- greater or equal |
| 632 | | CEqOp -- equal |
| 633 | | CNeqOp -- not equal |
| 634 | | CAndOp -- bitwise and |
| 635 | | CXorOp -- exclusive bitwise or |
| 636 | | COrOp -- inclusive bitwise or |
| 637 | | CLndOp -- logical and |
| 638 | | CLorOp -- logical or |
| 639 | deriving (Eq) |
| 640 | |
| 641 | -- C unary operator (K&R A7.3-4) (EXPORTED) |
| 642 | -- |
| 643 | data CUnaryOp = CPreIncOp -- prefix increment operator |
| 644 | | CPreDecOp -- prefix decrement operator |
| 645 | | CPostIncOp -- postfix increment operator |
| 646 | | CPostDecOp -- postfix decrement operator |
| 647 | | CAdrOp -- address operator |
| 648 | | CIndOp -- indirection operator |
| 649 | | CPlusOp -- prefix plus |
| 650 | | CMinOp -- prefix minus |
| 651 | | CCompOp -- one's complement |
| 652 | | CNegOp -- logical negation |
| 653 | deriving (Eq) |
| 654 | |
| 655 | -- C constant (K&R A2.5 & A7.2) (EXPORTED) |
| 656 | -- |
| 657 | -- * we do not list enumeration constants here, as they are identifiers |
| 658 | -- |
| 659 | data CConst = CIntConst Integer |
| 660 | Attrs |
| 661 | | CCharConst Char |
| 662 | Attrs |
| 663 | | CFloatConst String |
| 664 | Attrs |
| 665 | | CStrConst String |
| 666 | Attrs |
| 667 | |
| 668 | instance Pos CConst where |
| 669 | posOf (CIntConst _ at) = posOf at |
| 670 | posOf (CCharConst _ at) = posOf at |
| 671 | posOf (CFloatConst _ at) = posOf at |
| 672 | posOf (CStrConst _ at) = posOf at |
| 673 | |
| 674 | instance Eq CConst where |
| 675 | (CIntConst _ at1) == (CIntConst _ at2) = at1 == at2 |
| 676 | (CCharConst _ at1) == (CCharConst _ at2) = at1 == at2 |
| 677 | (CFloatConst _ at1) == (CFloatConst _ at2) = at1 == at2 |
| 678 | (CStrConst _ at1) == (CStrConst _ at2) = at1 == at2 |
| 679 | |
| 680 | |
| 681 | {-! for CDecl derive : GhcBinary !-} |
| 682 | {-! for CEnum derive : GhcBinary !-} |
| 683 | {-! for CStructUnion derive : GhcBinary !-} |
| 684 | {-! for CStructTag derive : GhcBinary !-} |
| 685 | {-! for CExpr derive : GhcBinary !-} |
| 686 | {-! for CInit derive : GhcBinary !-} |
| 687 | {-! for CDeclr derive : GhcBinary !-} |
| 688 | {-! for CDeclSpec derive : GhcBinary !-} |
| 689 | {-! for CTypeSpec derive : GhcBinary !-} |
| 690 | {-! for CStorageSpec derive : GhcBinary !-} |
| 691 | {-! for CTypeQual derive : GhcBinary !-} |
| 692 | {-! for CConst derive : GhcBinary !-} |
| 693 | {-! for CUnaryOp derive : GhcBinary !-} |
| 694 | {-! for CBinaryOp derive : GhcBinary !-} |
| 695 | {-! for CAssignOp derive : GhcBinary !-} |
| 696 | {-* Generated by DrIFT : Look, but Don't Touch. *-} |
| 697 | instance Binary CDecl where |
| 698 | put_ bh (CDecl aa ab ac) = do |
| 699 | put_ bh aa |
| 700 | put_ bh ab |
| 701 | put_ bh ac |
| 702 | get bh = do |
| 703 | aa <- get bh |
| 704 | ab <- get bh |
| 705 | ac <- get bh |
| 706 | return (CDecl aa ab ac) |
| 707 | |
| 708 | instance Binary CEnum where |
| 709 | put_ bh (CEnum aa ab ac) = do |
| 710 | put_ bh aa |
| 711 | put_ bh ab |
| 712 | put_ bh ac |
| 713 | get bh = do |
| 714 | aa <- get bh |
| 715 | ab <- get bh |
| 716 | ac <- get bh |
| 717 | return (CEnum aa ab ac) |
| 718 | |
| 719 | instance Binary CStructUnion where |
| 720 | put_ bh (CStruct aa ab ac ad) = do |
| 721 | put_ bh aa |
| 722 | put_ bh ab |
| 723 | put_ bh ac |
| 724 | put_ bh ad |
| 725 | get bh = do |
| 726 | aa <- get bh |
| 727 | ab <- get bh |
| 728 | ac <- get bh |
| 729 | ad <- get bh |
| 730 | return (CStruct aa ab ac ad) |
| 731 | |
| 732 | instance Binary CStructTag where |
| 733 | put_ bh CStructTag = do |
| 734 | putByte bh 0 |
| 735 | put_ bh CUnionTag = do |
| 736 | putByte bh 1 |
| 737 | get bh = do |
| 738 | h <- getByte bh |
| 739 | case h of |
| 740 | 0 -> do |
| 741 | return CStructTag |
| 742 | 1 -> do |
| 743 | return CUnionTag |
| 744 | |
| 745 | instance Binary CExpr where |
| 746 | put_ bh (CComma aa ab) = do |
| 747 | putByte bh 0 |
| 748 | put_ bh aa |
| 749 | put_ bh ab |
| 750 | put_ bh (CAssign ac ad ae af) = do |
| 751 | putByte bh 1 |
| 752 | put_ bh ac |
| 753 | put_ bh ad |
| 754 | put_ bh ae |
| 755 | put_ bh af |
| 756 | put_ bh (CCond ag ah ai aj) = do |
| 757 | putByte bh 2 |
| 758 | put_ bh ag |
| 759 | put_ bh ah |
| 760 | put_ bh ai |
| 761 | put_ bh aj |
| 762 | put_ bh (CBinary ak al am an) = do |
| 763 | putByte bh 3 |
| 764 | put_ bh ak |
| 765 | put_ bh al |
| 766 | put_ bh am |
| 767 | put_ bh an |
| 768 | put_ bh (CCast ao ap aq) = do |
| 769 | putByte bh 4 |
| 770 | put_ bh ao |
| 771 | put_ bh ap |
| 772 | put_ bh aq |
| 773 | put_ bh (CUnary ar as at) = do |
| 774 | putByte bh 5 |
| 775 | put_ bh ar |
| 776 | put_ bh as |
| 777 | put_ bh at |
| 778 | put_ bh (CSizeofExpr au av) = do |
| 779 | putByte bh 6 |
| 780 | put_ bh au |
| 781 | put_ bh av |
| 782 | put_ bh (CSizeofType aw ax) = do |
| 783 | putByte bh 7 |
| 784 | put_ bh aw |
| 785 | put_ bh ax |
| 786 | put_ bh (CAlignofExpr ay az) = do |
| 787 | putByte bh 8 |
| 788 | put_ bh ay |
| 789 | put_ bh az |
| 790 | put_ bh (CAlignofType aA aB) = do |
| 791 | putByte bh 9 |
| 792 | put_ bh aA |
| 793 | put_ bh aB |
| 794 | put_ bh (CIndex aC aD aE) = do |
| 795 | putByte bh 10 |
| 796 | put_ bh aC |
| 797 | put_ bh aD |
| 798 | put_ bh aE |
| 799 | put_ bh (CCall aF aG aH) = do |
| 800 | putByte bh 11 |
| 801 | put_ bh aF |
| 802 | put_ bh aG |
| 803 | put_ bh aH |
| 804 | put_ bh (CMember aI aJ aK aL) = do |
| 805 | putByte bh 12 |
| 806 | put_ bh aI |
| 807 | put_ bh aJ |
| 808 | put_ bh aK |
| 809 | put_ bh aL |
| 810 | put_ bh (CVar aM aN) = do |
| 811 | putByte bh 13 |
| 812 | put_ bh aM |
| 813 | put_ bh aN |
| 814 | put_ bh (CConst aO aP) = do |
| 815 | putByte bh 14 |
| 816 | put_ bh aO |
| 817 | put_ bh aP |
| 818 | get bh = do |
| 819 | h <- getByte bh |
| 820 | case h of |
| 821 | 0 -> do |
| 822 | aa <- get bh |
| 823 | ab <- get bh |
| 824 | return (CComma aa ab) |
| 825 | 1 -> do |
| 826 | ac <- get bh |
| 827 | ad <- get bh |
| 828 | ae <- get bh |
| 829 | af <- get bh |
| 830 | return (CAssign ac ad ae af) |
| 831 | 2 -> do |
| 832 | ag <- get bh |
| 833 | ah <- get bh |
| 834 | ai <- get bh |
| 835 | aj <- get bh |
| 836 | return (CCond ag ah ai aj) |
| 837 | 3 -> do |
| 838 | ak <- get bh |
| 839 | al <- get bh |
| 840 | am <- get bh |
| 841 | an <- get bh |
| 842 | return (CBinary ak al am an) |
| 843 | 4 -> do |
| 844 | ao <- get bh |
| 845 | ap <- get bh |
| 846 | aq <- get bh |
| 847 | return (CCast ao ap aq) |
| 848 | 5 -> do |
| 849 | ar <- get bh |
| 850 | as <- get bh |
| 851 | at <- get bh |
| 852 | return (CUnary ar as at) |
| 853 | 6 -> do |
| 854 | au <- get bh |
| 855 | av <- get bh |
| 856 | return (CSizeofExpr au av) |
| 857 | 7 -> do |
| 858 | aw <- get bh |
| 859 | ax <- get bh |
| 860 | return (CSizeofType aw ax) |
| 861 | 8 -> do |
| 862 | ay <- get bh |
| 863 | az <- get bh |
| 864 | return (CAlignofExpr ay az) |
| 865 | 9 -> do |
| 866 | aA <- get bh |
| 867 | aB <- get bh |
| 868 | return (CAlignofType aA aB) |
| 869 | 10 -> do |
| 870 | aC <- get bh |
| 871 | aD <- get bh |
| 872 | aE <- get bh |
| 873 | return (CIndex aC aD aE) |
| 874 | 11 -> do |
| 875 | aF <- get bh |
| 876 | aG <- get bh |
| 877 | aH <- get bh |
| 878 | return (CCall aF aG aH) |
| 879 | 12 -> do |
| 880 | aI <- get bh |
| 881 | aJ <- get bh |
| 882 | aK <- get bh |
| 883 | aL <- get bh |
| 884 | return (CMember aI aJ aK aL) |
| 885 | 13 -> do |
| 886 | aM <- get bh |
| 887 | aN <- get bh |
| 888 | return (CVar aM aN) |
| 889 | 14 -> do |
| 890 | aO <- get bh |
| 891 | aP <- get bh |
| 892 | return (CConst aO aP) |
| 893 | |
| 894 | instance Binary CInit where |
| 895 | put_ bh (CInitExpr aa ab) = do |
| 896 | putByte bh 0 |
| 897 | put_ bh aa |
| 898 | put_ bh ab |
| 899 | put_ bh (CInitList ac ad) = do |
| 900 | putByte bh 1 |
| 901 | put_ bh ac |
| 902 | put_ bh ad |
| 903 | get bh = do |
| 904 | h <- getByte bh |
| 905 | case h of |
| 906 | 0 -> do |
| 907 | aa <- get bh |
| 908 | ab <- get bh |
| 909 | return (CInitExpr aa ab) |
| 910 | 1 -> do |
| 911 | ac <- get bh |
| 912 | ad <- get bh |
| 913 | return (CInitList ac ad) |
| 914 | |
| 915 | instance Binary CDesignator where |
| 916 | put_ bh (CArrDesig aa ab) = do |
| 917 | putByte bh 0 |
| 918 | put_ bh aa |
| 919 | put_ bh ab |
| 920 | put_ bh (CMemberDesig ac ad) = do |
| 921 | putByte bh 1 |
| 922 | put_ bh ac |
| 923 | put_ bh ad |
| 924 | put_ bh (CRangeDesig ae af ag) = do |
| 925 | putByte bh 2 |
| 926 | put_ bh ae |
| 927 | put_ bh af |
| 928 | put_ bh ag |
| 929 | get bh = do |
| 930 | h <- getByte bh |
| 931 | case h of |
| 932 | 0 -> do |
| 933 | aa <- get bh |
| 934 | ab <- get bh |
| 935 | return (CArrDesig aa ab) |
| 936 | 1 -> do |
| 937 | ac <- get bh |
| 938 | ad <- get bh |
| 939 | return (CMemberDesig ac ad) |
| 940 | 2 -> do |
| 941 | ae <- get bh |
| 942 | af <- get bh |
| 943 | ag <- get bh |
| 944 | return (CRangeDesig ae af ag) |
| 945 | |
| 946 | instance Binary CDeclr where |
| 947 | put_ bh (CVarDeclr aa ab) = do |
| 948 | putByte bh 0 |
| 949 | put_ bh aa |
| 950 | put_ bh ab |
| 951 | put_ bh (CPtrDeclr ac ad ae) = do |
| 952 | putByte bh 1 |
| 953 | put_ bh ac |
| 954 | put_ bh ad |
| 955 | put_ bh ae |
| 956 | put_ bh (CArrDeclr af ag ah ai) = do |
| 957 | putByte bh 2 |
| 958 | put_ bh af |
| 959 | put_ bh ag |
| 960 | put_ bh ah |
| 961 | put_ bh ai |
| 962 | put_ bh (CFunDeclr ai aj ak al) = do |
| 963 | putByte bh 3 |
| 964 | put_ bh ai |
| 965 | put_ bh aj |
| 966 | put_ bh ak |
| 967 | put_ bh al |
| 968 | get bh = do |
| 969 | h <- getByte bh |
| 970 | case h of |
| 971 | 0 -> do |
| 972 | aa <- get bh |
| 973 | ab <- get bh |
| 974 | return (CVarDeclr aa ab) |
| 975 | 1 -> do |
| 976 | ac <- get bh |
| 977 | ad <- get bh |
| 978 | ae <- get bh |
| 979 | return (CPtrDeclr ac ad ae) |
| 980 | 2 -> do |
| 981 | af <- get bh |
| 982 | ag <- get bh |
| 983 | ah <- get bh |
| 984 | ai <- get bh |
| 985 | return (CArrDeclr af ag ah ai) |
| 986 | 3 -> do |
| 987 | ai <- get bh |
| 988 | aj <- get bh |
| 989 | ak <- get bh |
| 990 | al <- get bh |
| 991 | return (CFunDeclr ai aj ak al) |
| 992 | |
| 993 | instance Binary CDeclSpec where |
| 994 | put_ bh (CStorageSpec aa) = do |
| 995 | putByte bh 0 |
| 996 | put_ bh aa |
| 997 | put_ bh (CTypeSpec ab) = do |
| 998 | putByte bh 1 |
| 999 | put_ bh ab |
| 1000 | put_ bh (CTypeQual ac) = do |
| 1001 | putByte bh 2 |
| 1002 | put_ bh ac |
| 1003 | get bh = do |
| 1004 | h <- getByte bh |
| 1005 | case h of |
| 1006 | 0 -> do |
| 1007 | aa <- get bh |
| 1008 | return (CStorageSpec aa) |
| 1009 | 1 -> do |
| 1010 | ab <- get bh |
| 1011 | return (CTypeSpec ab) |
| 1012 | 2 -> do |
| 1013 | ac <- get bh |
| 1014 | return (CTypeQual ac) |
| 1015 | |
| 1016 | instance Binary CTypeSpec where |
| 1017 | put_ bh (CVoidType aa) = do |
| 1018 | putByte bh 0 |
| 1019 | put_ bh aa |
| 1020 | put_ bh (CCharType ab) = do |
| 1021 | putByte bh 1 |
| 1022 | put_ bh ab |
| 1023 | put_ bh (CShortType ac) = do |
| 1024 | putByte bh 2 |
| 1025 | put_ bh ac |
| 1026 | put_ bh (CIntType ad) = do |
| 1027 | putByte bh 3 |
| 1028 | put_ bh ad |
| 1029 | put_ bh (CLongType ae) = do |
| 1030 | putByte bh 4 |
| 1031 | put_ bh ae |
| 1032 | put_ bh (CFloatType af) = do |
| 1033 | putByte bh 5 |
| 1034 | put_ bh af |
| 1035 | put_ bh (CDoubleType ag) = do |
| 1036 | putByte bh 6 |
| 1037 | put_ bh ag |
| 1038 | put_ bh (CSignedType ah) = do |
| 1039 | putByte bh 7 |
| 1040 | put_ bh ah |
| 1041 | put_ bh (CUnsigType ai) = do |
| 1042 | putByte bh 8 |
| 1043 | put_ bh ai |
| 1044 | put_ bh (CSUType aj ak) = do |
| 1045 | putByte bh 9 |
| 1046 | put_ bh aj |
| 1047 | put_ bh ak |
| 1048 | put_ bh (CEnumType al am) = do |
| 1049 | putByte bh 10 |
| 1050 | put_ bh al |
| 1051 | put_ bh am |
| 1052 | put_ bh (CTypeDef an ao) = do |
| 1053 | putByte bh 11 |
| 1054 | put_ bh an |
| 1055 | put_ bh ao |
| 1056 | put_ bh (CTypeOfExpr ap aq) = do |
| 1057 | putByte bh 12 |
| 1058 | put_ bh ap |
| 1059 | put_ bh aq |
| 1060 | put_ bh (CTypeOfType ar as) = do |
| 1061 | putByte bh 13 |
| 1062 | put_ bh ar |
| 1063 | put_ bh as |
| 1064 | put_ bh (CFloat128Type at) = do |
| 1065 | putByte bh 14 |
| 1066 | put_ bh at |
| 1067 | put_ bh (CBoolType at) = do |
| 1068 | putByte bh 15 |
| 1069 | put_ bh at |
| 1070 | get bh = do |
| 1071 | h <- getByte bh |
| 1072 | case h of |
| 1073 | 0 -> do |
| 1074 | aa <- get bh |
| 1075 | return (CVoidType aa) |
| 1076 | 1 -> do |
| 1077 | ab <- get bh |
| 1078 | return (CCharType ab) |
| 1079 | 2 -> do |
| 1080 | ac <- get bh |
| 1081 | return (CShortType ac) |
| 1082 | 3 -> do |
| 1083 | ad <- get bh |
| 1084 | return (CIntType ad) |
| 1085 | 4 -> do |
| 1086 | ae <- get bh |
| 1087 | return (CLongType ae) |
| 1088 | 5 -> do |
| 1089 | af <- get bh |
| 1090 | return (CFloatType af) |
| 1091 | 6 -> do |
| 1092 | ag <- get bh |
| 1093 | return (CDoubleType ag) |
| 1094 | 7 -> do |
| 1095 | ah <- get bh |
| 1096 | return (CSignedType ah) |
| 1097 | 8 -> do |
| 1098 | ai <- get bh |
| 1099 | return (CUnsigType ai) |
| 1100 | 9 -> do |
| 1101 | aj <- get bh |
| 1102 | ak <- get bh |
| 1103 | return (CSUType aj ak) |
| 1104 | 10 -> do |
| 1105 | al <- get bh |
| 1106 | am <- get bh |
| 1107 | return (CEnumType al am) |
| 1108 | 11 -> do |
| 1109 | an <- get bh |
| 1110 | ao <- get bh |
| 1111 | return (CTypeDef an ao) |
| 1112 | 12 -> do |
| 1113 | ap <- get bh |
| 1114 | aq <- get bh |
| 1115 | return (CTypeOfExpr ap aq) |
| 1116 | 13 -> do |
| 1117 | ar <- get bh |
| 1118 | as <- get bh |
| 1119 | return (CTypeOfType ar as) |
| 1120 | 14 -> do |
| 1121 | at <- get bh |
| 1122 | return (CFloat128Type at) |
| 1123 | 15 -> do |
| 1124 | at <- get bh |
| 1125 | return (CBoolType at) |
| 1126 | |
| 1127 | instance Binary CStorageSpec where |
| 1128 | put_ bh (CAuto aa) = do |
| 1129 | putByte bh 0 |
| 1130 | put_ bh aa |
| 1131 | put_ bh (CRegister ab) = do |
| 1132 | putByte bh 1 |
| 1133 | put_ bh ab |
| 1134 | put_ bh (CStatic ac) = do |
| 1135 | putByte bh 2 |
| 1136 | put_ bh ac |
| 1137 | put_ bh (CExtern ad) = do |
| 1138 | putByte bh 3 |
| 1139 | put_ bh ad |
| 1140 | put_ bh (CTypedef ae) = do |
| 1141 | putByte bh 4 |
| 1142 | put_ bh ae |
| 1143 | get bh = do |
| 1144 | h <- getByte bh |
| 1145 | case h of |
| 1146 | 0 -> do |
| 1147 | aa <- get bh |
| 1148 | return (CAuto aa) |
| 1149 | 1 -> do |
| 1150 | ab <- get bh |
| 1151 | return (CRegister ab) |
| 1152 | 2 -> do |
| 1153 | ac <- get bh |
| 1154 | return (CStatic ac) |
| 1155 | 3 -> do |
| 1156 | ad <- get bh |
| 1157 | return (CExtern ad) |
| 1158 | 4 -> do |
| 1159 | ae <- get bh |
| 1160 | return (CTypedef ae) |
| 1161 | |
| 1162 | instance Binary CTypeQual where |
| 1163 | put_ bh (CConstQual aa) = do |
| 1164 | putByte bh 0 |
| 1165 | put_ bh aa |
| 1166 | put_ bh (CVolatQual ab) = do |
| 1167 | putByte bh 1 |
| 1168 | put_ bh ab |
| 1169 | put_ bh (CRestrQual ac) = do |
| 1170 | putByte bh 2 |
| 1171 | put_ bh ac |
| 1172 | put_ bh (CInlinQual ad) = do |
| 1173 | putByte bh 3 |
| 1174 | put_ bh ad |
| 1175 | get bh = do |
| 1176 | h <- getByte bh |
| 1177 | case h of |
| 1178 | 0 -> do |
| 1179 | aa <- get bh |
| 1180 | return (CConstQual aa) |
| 1181 | 1 -> do |
| 1182 | ab <- get bh |
| 1183 | return (CVolatQual ab) |
| 1184 | 2 -> do |
| 1185 | ac <- get bh |
| 1186 | return (CRestrQual ac) |
| 1187 | 3 -> do |
| 1188 | ad <- get bh |
| 1189 | return (CInlinQual ad) |
| 1190 | |
| 1191 | instance Binary CConst where |
| 1192 | put_ bh (CIntConst aa ab) = do |
| 1193 | putByte bh 0 |
| 1194 | put_ bh aa |
| 1195 | put_ bh ab |
| 1196 | put_ bh (CCharConst ac ad) = do |
| 1197 | putByte bh 1 |
| 1198 | put_ bh ac |
| 1199 | put_ bh ad |
| 1200 | put_ bh (CFloatConst ae af) = do |
| 1201 | putByte bh 2 |
| 1202 | put_ bh ae |
| 1203 | put_ bh af |
| 1204 | put_ bh (CStrConst ag ah) = do |
| 1205 | putByte bh 3 |
| 1206 | put_ bh ag |
| 1207 | put_ bh ah |
| 1208 | get bh = do |
| 1209 | h <- getByte bh |
| 1210 | case h of |
| 1211 | 0 -> do |
| 1212 | aa <- get bh |
| 1213 | ab <- get bh |
| 1214 | return (CIntConst aa ab) |
| 1215 | 1 -> do |
| 1216 | ac <- get bh |
| 1217 | ad <- get bh |
| 1218 | return (CCharConst ac ad) |
| 1219 | 2 -> do |
| 1220 | ae <- get bh |
| 1221 | af <- get bh |
| 1222 | return (CFloatConst ae af) |
| 1223 | 3 -> do |
| 1224 | ag <- get bh |
| 1225 | ah <- get bh |
| 1226 | return (CStrConst ag ah) |
| 1227 | |
| 1228 | instance Binary CUnaryOp where |
| 1229 | put_ bh CPreIncOp = putByte bh 0 |
| 1230 | put_ bh CPreDecOp = putByte bh 1 |
| 1231 | put_ bh CPostIncOp = putByte bh 2 |
| 1232 | put_ bh CPostDecOp = putByte bh 3 |
| 1233 | put_ bh CAdrOp = putByte bh 4 |
| 1234 | put_ bh CIndOp = putByte bh 5 |
| 1235 | put_ bh CPlusOp = putByte bh 6 |
| 1236 | put_ bh CMinOp = putByte bh 7 |
| 1237 | put_ bh CCompOp = putByte bh 8 |
| 1238 | put_ bh CNegOp = putByte bh 9 |
| 1239 | get bh = do |
| 1240 | h <- getByte bh |
| 1241 | case h of |
| 1242 | 0 -> return CPreIncOp |
| 1243 | 1 -> return CPreDecOp |
| 1244 | 2 -> return CPostIncOp |
| 1245 | 3 -> return CPostDecOp |
| 1246 | 4 -> return CAdrOp |
| 1247 | 5 -> return CIndOp |
| 1248 | 6 -> return CPlusOp |
| 1249 | 7 -> return CMinOp |
| 1250 | 8 -> return CCompOp |
| 1251 | 9 -> return CNegOp |
| 1252 | |
| 1253 | instance Binary CBinaryOp where |
| 1254 | put_ bh CMulOp = putByte bh 0 |
| 1255 | put_ bh CDivOp = putByte bh 1 |
| 1256 | put_ bh CRmdOp = putByte bh 2 |
| 1257 | put_ bh CAddOp = putByte bh 3 |
| 1258 | put_ bh CSubOp = putByte bh 4 |
| 1259 | put_ bh CShlOp = putByte bh 5 |
| 1260 | put_ bh CShrOp = putByte bh 6 |
| 1261 | put_ bh CLeOp = putByte bh 7 |
| 1262 | put_ bh CGrOp = putByte bh 8 |
| 1263 | put_ bh CLeqOp = putByte bh 9 |
| 1264 | put_ bh CGeqOp = putByte bh 10 |
| 1265 | put_ bh CEqOp = putByte bh 11 |
| 1266 | put_ bh CNeqOp = putByte bh 12 |
| 1267 | put_ bh CAndOp = putByte bh 13 |
| 1268 | put_ bh CXorOp = putByte bh 14 |
| 1269 | put_ bh COrOp = putByte bh 15 |
| 1270 | put_ bh CLndOp = putByte bh 16 |
| 1271 | put_ bh CLorOp = putByte bh 17 |
| 1272 | get bh = do |
| 1273 | h <- getByte bh |
| 1274 | case h of |
| 1275 | 0 -> return CMulOp |
| 1276 | 1 -> return CDivOp |
| 1277 | 2 -> return CRmdOp |
| 1278 | 3 -> return CAddOp |
| 1279 | 4 -> return CSubOp |
| 1280 | 5 -> return CShlOp |
| 1281 | 6 -> return CShrOp |
| 1282 | 7 -> return CLeOp |
| 1283 | 8 -> return CGrOp |
| 1284 | 9 -> return CLeqOp |
| 1285 | 10 -> return CGeqOp |
| 1286 | 11 -> return CEqOp |
| 1287 | 12 -> return CNeqOp |
| 1288 | 13 -> return CAndOp |
| 1289 | 14 -> return CXorOp |
| 1290 | 15 -> return COrOp |
| 1291 | 16 -> return CLndOp |
| 1292 | 17 -> return CLorOp |
| 1293 | |
| 1294 | instance Binary CAssignOp where |
| 1295 | put_ bh CAssignOp = putByte bh 0 |
| 1296 | put_ bh CMulAssOp = putByte bh 1 |
| 1297 | put_ bh CDivAssOp = putByte bh 2 |
| 1298 | put_ bh CRmdAssOp = putByte bh 3 |
| 1299 | put_ bh CAddAssOp = putByte bh 4 |
| 1300 | put_ bh CSubAssOp = putByte bh 5 |
| 1301 | put_ bh CShlAssOp = putByte bh 6 |
| 1302 | put_ bh CShrAssOp = putByte bh 7 |
| 1303 | put_ bh CAndAssOp = putByte bh 8 |
| 1304 | put_ bh CXorAssOp = putByte bh 9 |
| 1305 | put_ bh COrAssOp = putByte bh 10 |
| 1306 | get bh = do |
| 1307 | h <- getByte bh |
| 1308 | case h of |
| 1309 | 0 -> return CAssignOp |
| 1310 | 1 -> return CMulAssOp |
| 1311 | 2 -> return CDivAssOp |
| 1312 | 3 -> return CRmdAssOp |
| 1313 | 4 -> return CAddAssOp |
| 1314 | 5 -> return CSubAssOp |
| 1315 | 6 -> return CShlAssOp |
| 1316 | 7 -> return CShrAssOp |
| 1317 | 8 -> return CAndAssOp |
| 1318 | 9 -> return CXorAssOp |
| 1319 | 10 -> return COrAssOp |