Debian Codegraph @

c2hs/c/CAttrs.hs

raw symbols SCIP
1 -- C->Haskell Compiler: C attribute definitions and manipulation routines
2 --
3 -- Author : Manuel M. T. Chakravarty
4 -- Created: 12 August 99
5 --
6 -- Version $Revision: 1.1 $ from $Date: 2004/11/21 21:05:27 $
7 --
8 -- Copyright (c) [1999..2001] 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 provides the attributed version of the C structure tree.
23 --
24 -- * C has several name spaces of which two are represented in this module:
25 -- - `CObj' in `defObjsAC': The name space of objects, functions, typedef
26 -- names, and enum constants.
27 -- - `CTag' in `defTagsAC': The name space of tags of structures, unions,
28 -- and enumerations.
29 --
30 -- * The final state of the names spaces are preserved in the attributed
31 -- structure tree. This allows further fast lookups for globally defined
32 -- identifiers after the name anaysis is over.
33 --
34 -- * In addition to the name spaces, the attribute structure tree contains
35 -- a ident-definition table, which for attribute handles of identifiers
36 -- refers to the identifiers definition. These are only used in usage
37 -- occurrences, except for one exception: The tag identifiers in forward
38 -- definitions of structures or enums get a reference to the corresponding
39 -- full definition - see `CTrav' for full details.
40 --
41 -- * We maintain a shadow definition table, it can be populated with aliases
42 -- to other objects and maps identifiers to identifiers. It is populated by
43 -- using the `applyPrefix' function. When looksup performed via the shadow
44 -- variant of a lookup function, shadow aliases are also considered, but
45 -- they are used only if no normal entry for the identifiers is present.
46 --
47 -- * Only ranges delimited by a block open a new range for tags (see
48 -- `enterNewObjRangeC' and `leaveObjRangeC').
49 --
50 --- DOCU ----------------------------------------------------------------------
51 --
52 -- language: Haskell 98
53 --
54 --- TODO ----------------------------------------------------------------------
55 --
56
57 module CAttrs (-- attributed C
58 --
59 AttrC, attrC, getCHeader, enterNewRangeC, enterNewObjRangeC,
60 leaveRangeC, leaveObjRangeC, addDefObjC, lookupDefObjC,
61 lookupDefObjCShadow, addDefTagC, lookupDefTagC,
62 lookupDefTagCShadow, applyPrefix, getDefOfIdentC,
63 setDefOfIdentC, updDefOfIdentC, freezeDefOfIdentsAttrC,
64 softenDefOfIdentsAttrC,
65 --
66 -- C objects
67 --
68 CObj(..), CTag(..), CDef(..))
69 where
70
71 import Data.Char (toUpper)
72 import Data.List (isPrefixOf)
73 import Data.Maybe (mapMaybe)
74
75 import Position (Position, Pos(posOf), nopos, dontCarePos, builtinPos)
76 import Errors (interr)
77 import Idents (Ident, getIdentAttrs, identToLexeme, onlyPosIdent)
78 import Attributes (Attr(..), AttrTable, getAttr, setAttr, updAttr,
79 newAttrTable, freezeAttrTable, softenAttrTable)
80 import NameSpaces (NameSpace, nameSpace, enterNewRange, leaveRange, defLocal,
81 defGlobal, find, nameSpaceToList)
82 import Binary (Binary(..), putByte, getByte)
83
84 import CAST
85
86
87 -- attributed C structure tree
88 -- ---------------------------
89
90 -- C unit together with the attributes relevant to the outside world
91 -- (EXPORTED ABSTRACT)
92 --
93 data AttrC = AttrC {
94 headerAC :: CHeader, -- raw header
95 defObjsAC :: CObjNS, -- defined objects
96 defTagsAC :: CTagNS, -- defined tags
97 shadowsAC :: CShadowNS, -- shadow definitions (prefix)
98 defsAC :: CDefTable -- ident-def associations
99 }
100
101 -- make an attribute structure tree from a raw one (EXPORTED)
102 --
103 attrC :: CHeader -> AttrC
104 attrC header = AttrC {
105 headerAC = header,
106 defObjsAC = cObjNS,
107 defTagsAC = cTagNS,
108 shadowsAC = cShadowNS,
109 defsAC = cDefTable
110 }
111
112 -- extract the raw structure tree from an attributes one (EXPORTED)
113 --
114 getCHeader :: AttrC -> CHeader
115 getCHeader = headerAC
116
117
118 -- the name space operations
119 --
120
121 -- enter a new range (EXPORTED)
122 --
123 enterNewRangeC :: AttrC -> AttrC
124 enterNewRangeC ac = ac {
125 defObjsAC = enterNewRange . defObjsAC $ ac,
126 defTagsAC = enterNewRange . defTagsAC $ ac
127 }
128
129 -- enter a new range, only for objects (EXPORTED)
130 --
131 enterNewObjRangeC :: AttrC -> AttrC
132 enterNewObjRangeC ac = ac {
133 defObjsAC = enterNewRange . defObjsAC $ ac
134 }
135
136 -- leave the current range (EXPORTED)
137 --
138 leaveRangeC :: AttrC -> AttrC
139 leaveRangeC ac = ac {
140 defObjsAC = fst . leaveRange . defObjsAC $ ac,
141 defTagsAC = fst . leaveRange . defTagsAC $ ac
142 }
143
144 -- leave the current range, only for objects (EXPORTED)
145 --
146 leaveObjRangeC :: AttrC -> AttrC
147 leaveObjRangeC ac = ac {
148 defObjsAC = fst . leaveRange . defObjsAC $ ac
149 }
150
151 -- add another definitions to the object name space (EXPORTED)
152 --
153 -- * if a definition of the same name was already present, it is returned
154 --
155 addDefObjC :: AttrC -> Ident -> CObj -> (AttrC, Maybe CObj)
156 addDefObjC ac ide obj = let om = defObjsAC ac
157 (ac', obj') = defLocal om ide obj
158 in
159 (ac {defObjsAC = ac'}, obj')
160
161 -- lookup an identifier in the object name space (EXPORTED)
162 --
163 lookupDefObjC :: AttrC -> Ident -> Maybe CObj
164 lookupDefObjC ac ide = find (defObjsAC ac) ide
165
166 -- lookup an identifier in the object name space; if nothing found, try
167 -- whether there is a shadow identifier that matches (EXPORTED)
168 --
169 -- * the returned identifier is the _real_ identifier of the object
170 --
171 lookupDefObjCShadow :: AttrC -> Ident -> Maybe (CObj, Ident)
172 lookupDefObjCShadow ac ide =
173 case lookupDefObjC ac ide of
174 Just obj -> Just (obj, ide)
175 Nothing -> case find (shadowsAC ac) ide of
176 Nothing -> Nothing
177 Just ide' -> case lookupDefObjC ac ide' of
178 Just obj -> Just (obj, ide')
179 Nothing -> Nothing
180
181 -- add another definition to the tag name space (EXPORTED)
182 --
183 -- * if a definition of the same name was already present, it is returned
184 --
185 addDefTagC :: AttrC -> Ident -> CTag -> (AttrC, Maybe CTag)
186 addDefTagC ac ide obj = let tm = defTagsAC ac
187 (ac', obj') = defLocal tm ide obj
188 in
189 (ac {defTagsAC = ac'}, obj')
190
191 -- lookup an identifier in the tag name space (EXPORTED)
192 --
193 lookupDefTagC :: AttrC -> Ident -> Maybe CTag
194 lookupDefTagC ac ide = find (defTagsAC ac) ide
195
196 -- lookup an identifier in the tag name space; if nothing found, try
197 -- whether there is a shadow identifier that matches (EXPORTED)
198 --
199 -- * the returned identifier is the _real_ identifier of the tag
200 --
201 lookupDefTagCShadow :: AttrC -> Ident -> Maybe (CTag, Ident)
202 lookupDefTagCShadow ac ide =
203 case lookupDefTagC ac ide of
204 Just tag -> Just (tag, ide)
205 Nothing -> case find (shadowsAC ac) ide of
206 Nothing -> Nothing
207 Just ide' -> case lookupDefTagC ac ide' of
208 Just tag -> Just (tag, ide')
209 Nothing -> Nothing
210
211 -- enrich the shadow name space with identifiers obtained by dropping
212 -- the given prefix from the identifiers already in the object or tag name
213 -- space (EXPORTED)
214 --
215 -- * in case of a collisions, a random entry is selected
216 --
217 -- * case is not relevant in the prefix and underscores between the prefix and
218 -- the stem of an identifier are also dropped
219 --
220 applyPrefix :: AttrC -> String -> AttrC
221 applyPrefix ac prefix =
222 let
223 shadows = shadowsAC ac
224 names = map fst (nameSpaceToList (defObjsAC ac))
225 ++ map fst (nameSpaceToList (defTagsAC ac))
226 newShadows = mapMaybe (strip prefix) names
227 in
228 ac {shadowsAC = foldl define shadows newShadows}
229 where
230 strip prefix ide = case eat prefix (identToLexeme ide) of
231 Nothing -> Nothing
232 Just "" -> Nothing
233 Just newName -> Just
234 (onlyPosIdent (posOf ide) newName,
235 ide)
236 --
237 eat [] ('_':cs) = eat [] cs
238 eat [] cs = Just cs
239 eat (p:prefix) (c:cs) | toUpper p == toUpper c = eat prefix cs
240 | otherwise = Nothing
241 eat _ _ = Nothing
242 --
243 define ns (ide, def) = fst (defGlobal ns ide def)
244
245
246 -- the attribute table operations on the attributes
247 --
248
249 -- get the definition associated with the given identifier (EXPORTED)
250 --
251 getDefOfIdentC :: AttrC -> Ident -> CDef
252 getDefOfIdentC ac = getAttr (defsAC ac) . getIdentAttrs
253
254 setDefOfIdentC :: AttrC -> Ident -> CDef -> AttrC
255 setDefOfIdentC ac id def =
256 let tot' = setAttr (defsAC ac) (getIdentAttrs id) def
257 in
258 ac {defsAC = tot'}
259
260 updDefOfIdentC :: AttrC -> Ident -> CDef -> AttrC
261 updDefOfIdentC ac id def =
262 let tot' = updAttr (defsAC ac) (getIdentAttrs id) def
263 in
264 ac {defsAC = tot'}
265
266 freezeDefOfIdentsAttrC :: AttrC -> AttrC
267 freezeDefOfIdentsAttrC ac = ac {defsAC = freezeAttrTable (defsAC ac)}
268
269 softenDefOfIdentsAttrC :: AttrC -> AttrC
270 softenDefOfIdentsAttrC ac = ac {defsAC = softenAttrTable (defsAC ac)}
271
272
273 -- C objects including operations
274 -- ------------------------------
275
276 -- C objects data definition (EXPORTED)
277 --
278 data CObj = TypeCO CDecl -- typedef declaration
279 | ObjCO CDecl -- object or function declaration
280 | EnumCO Ident CEnum -- enumerator
281 | BuiltinCO -- builtin object
282
283 -- two C objects are equal iff they are defined by the same structure
284 -- tree node (i.e., the two nodes referenced have the same attribute
285 -- identifier)
286 --
287 instance Eq CObj where
288 (TypeCO decl1 ) == (TypeCO decl2 ) = decl1 == decl2
289 (ObjCO decl1 ) == (ObjCO decl2 ) = decl1 == decl2
290 (EnumCO ide1 enum1) == (EnumCO ide2 enum2) = ide1 == ide2 && enum1 == enum2
291 _ == _ = False
292
293 instance Pos CObj where
294 posOf (TypeCO def ) = posOf def
295 posOf (ObjCO def ) = posOf def
296 posOf (EnumCO ide _) = posOf ide
297 posOf (BuiltinCO ) = builtinPos
298
299
300 -- C tagged objects including operations
301 -- -------------------------------------
302
303 -- C tagged objects data definition (EXPORTED)
304 --
305 data CTag = StructUnionCT CStructUnion -- toplevel struct-union declaration
306 | EnumCT CEnum -- toplevel enum declaration
307
308 -- two C tag objects are equal iff they are defined by the same structure
309 -- tree node (i.e., the two nodes referenced have the same attribute
310 -- identifier)
311 --
312 instance Eq CTag where
313 (StructUnionCT struct1) == (StructUnionCT struct2) = struct1 == struct2
314 (EnumCT enum1 ) == (EnumCT enum2 ) = enum1 == enum2
315 _ == _ = False
316
317 instance Pos CTag where
318 posOf (StructUnionCT def) = posOf def
319 posOf (EnumCT def) = posOf def
320
321
322 -- C general definition
323 -- --------------------
324
325 -- C general definition (EXPORTED)
326 --
327 data CDef = UndefCD -- undefined object
328 | DontCareCD -- don't care object
329 | ObjCD CObj -- C object
330 | TagCD CTag -- C tag
331
332 -- two C definitions are equal iff they are defined by the same structure
333 -- tree node (i.e., the two nodes referenced have the same attribute
334 -- identifier), but don't care objects are equal to everything and undefined
335 -- objects may not be compared
336 --
337 instance Eq CDef where
338 (ObjCD obj1) == (ObjCD obj2) = obj1 == obj2
339 (TagCD tag1) == (TagCD tag2) = tag1 == tag2
340 DontCareCD == _ = True
341 _ == DontCareCD = True
342 UndefCD == _ =
343 interr "CAttrs: Attempt to compare an undefined C definition!"
344 _ == UndefCD =
345 interr "CAttrs: Attempt to compare an undefined C definition!"
346 _ == _ = False
347
348 instance Attr CDef where
349 undef = UndefCD
350 dontCare = DontCareCD
351
352 isUndef UndefCD = True
353 isUndef _ = False
354
355 isDontCare DontCareCD = True
356 isDontCare _ = False
357
358 instance Pos CDef where
359 posOf UndefCD = nopos
360 posOf DontCareCD = dontCarePos
361 posOf (ObjCD obj) = posOf obj
362 posOf (TagCD tag) = posOf tag
363
364
365 -- object tables (internal use only)
366 -- ---------------------------------
367
368 -- the object name space
369 --
370 type CObjNS = NameSpace CObj
371
372 -- creating a new object name space
373 --
374 cObjNS :: CObjNS
375 cObjNS = nameSpace
376
377 -- the tag name space
378 --
379 type CTagNS = NameSpace CTag
380
381 -- creating a new tag name space
382 --
383 cTagNS :: CTagNS
384 cTagNS = nameSpace
385
386 -- the shadow name space
387 --
388 type CShadowNS = NameSpace Ident
389
390 -- creating a shadow name space
391 --
392 cShadowNS :: CShadowNS
393 cShadowNS = nameSpace
394
395 -- the general definition table
396 --
397 type CDefTable = AttrTable CDef
398
399 -- creating a new definition table
400 --
401 cDefTable :: CDefTable
402 cDefTable = newAttrTable "C General Definition Table for Idents"
403
404
405 {-! for AttrC derive : GhcBinary !-}
406 {-! for CObj derive : GhcBinary !-}
407 {-! for CTag derive : GhcBinary !-}
408 {-! for CDef derive : GhcBinary !-}
409 {-* Generated by DrIFT : Look, but Don't Touch. *-}
410 instance Binary AttrC where
411 put_ bh (AttrC aa ab ac ad ae) = do
412 -- put_ bh aa
413 put_ bh ab
414 put_ bh ac
415 put_ bh ad
416 put_ bh ae
417 get bh = do
418 -- aa <- get bh
419 ab <- get bh
420 ac <- get bh
421 ad <- get bh
422 ae <- get bh
423 return (AttrC (error "AttrC.headerAC should not be needed") ab ac ad ae)
424
425 instance Binary CObj where
426 put_ bh (TypeCO aa) = do
427 putByte bh 0
428 put_ bh aa
429 put_ bh (ObjCO ab) = do
430 putByte bh 1
431 put_ bh ab
432 put_ bh (EnumCO ac ad) = do
433 putByte bh 2
434 put_ bh ac
435 put_ bh ad
436 put_ bh BuiltinCO = do
437 putByte bh 3
438 get bh = do
439 h <- getByte bh
440 case h of
441 0 -> do
442 aa <- get bh
443 return (TypeCO aa)
444 1 -> do
445 ab <- get bh
446 return (ObjCO ab)
447 2 -> do
448 ac <- get bh
449 ad <- get bh
450 return (EnumCO ac ad)
451 3 -> do
452 return BuiltinCO
453
454 instance Binary CTag where
455 put_ bh (StructUnionCT aa) = do
456 putByte bh 0
457 put_ bh aa
458 put_ bh (EnumCT ab) = do
459 putByte bh 1
460 put_ bh ab
461 get bh = do
462 h <- getByte bh
463 case h of
464 0 -> do
465 aa <- get bh
466 return (StructUnionCT aa)
467 1 -> do
468 ab <- get bh
469 return (EnumCT ab)
470
471 instance Binary CDef where
472 put_ bh UndefCD = do
473 putByte bh 0
474 put_ bh DontCareCD = do
475 putByte bh 1
476 put_ bh (ObjCD aa) = do
477 putByte bh 2
478 put_ bh aa
479 put_ bh (TagCD ab) = do
480 putByte bh 3
481 put_ bh ab
482 get bh = do
483 h <- getByte bh
484 case h of
485 0 -> do
486 return UndefCD
487 1 -> do
488 return DontCareCD
489 2 -> do
490 aa <- get bh
491 return (ObjCD aa)
492 3 -> do
493 ab <- get bh
494 return (TagCD ab)