José Fonseca | 7ad4026 | 2009-09-30 17:17:12 +0100 | [diff] [blame] | 1 | ########################################################################## |
José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 2 | # |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 3 | # Copyright 2008-2010 VMware, Inc. |
José Fonseca | 7ad4026 | 2009-09-30 17:17:12 +0100 | [diff] [blame] | 4 | # All Rights Reserved. |
José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 5 | # |
José Fonseca | 7ad4026 | 2009-09-30 17:17:12 +0100 | [diff] [blame] | 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | # of this software and associated documentation files (the "Software"), to deal |
| 8 | # in the Software without restriction, including without limitation the rights |
| 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | # copies of the Software, and to permit persons to whom the Software is |
| 11 | # furnished to do so, subject to the following conditions: |
José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 12 | # |
José Fonseca | 7ad4026 | 2009-09-30 17:17:12 +0100 | [diff] [blame] | 13 | # The above copyright notice and this permission notice shall be included in |
| 14 | # all copies or substantial portions of the Software. |
José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 15 | # |
José Fonseca | 7ad4026 | 2009-09-30 17:17:12 +0100 | [diff] [blame] | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | # THE SOFTWARE. |
José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 23 | # |
José Fonseca | 7ad4026 | 2009-09-30 17:17:12 +0100 | [diff] [blame] | 24 | ##########################################################################/ |
José Fonseca | 9544244 | 2008-07-08 10:32:53 +0900 | [diff] [blame] | 25 | |
José Fonseca | d626cf4 | 2008-07-07 07:43:16 +0900 | [diff] [blame] | 26 | """C basic types""" |
| 27 | |
José Fonseca | 8a56d14 | 2008-07-09 12:18:08 +0900 | [diff] [blame] | 28 | |
| 29 | import debug |
| 30 | |
| 31 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 32 | class Type: |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 33 | """Base class for all types.""" |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 34 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 35 | __tags = set() |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 36 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 37 | def __init__(self, expr, tag = None): |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 38 | self.expr = expr |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 39 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 40 | # Generate a default tag, used when naming functions that will operate |
| 41 | # on this type, so it should preferrably be something representative of |
| 42 | # the type. |
| 43 | if tag is None: |
José Fonseca | 5b6fb75 | 2012-04-14 14:56:45 +0100 | [diff] [blame] | 44 | if expr is not None: |
| 45 | tag = ''.join([c for c in expr if c.isalnum() or c in '_']) |
| 46 | else: |
| 47 | tag = 'anonynoums' |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 48 | else: |
| 49 | for c in tag: |
| 50 | assert c.isalnum() or c in '_' |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 51 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 52 | # Ensure it is unique. |
| 53 | if tag in Type.__tags: |
| 54 | suffix = 1 |
| 55 | while tag + str(suffix) in Type.__tags: |
| 56 | suffix += 1 |
| 57 | tag += str(suffix) |
| 58 | |
| 59 | assert tag not in Type.__tags |
| 60 | Type.__tags.add(tag) |
| 61 | |
| 62 | self.tag = tag |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 63 | |
| 64 | def __str__(self): |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 65 | """Return the C/C++ type expression for this type.""" |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 66 | return self.expr |
| 67 | |
| 68 | def visit(self, visitor, *args, **kwargs): |
| 69 | raise NotImplementedError |
| 70 | |
José Fonseca | 0075f15 | 2012-04-14 20:25:52 +0100 | [diff] [blame] | 71 | def mutable(self): |
| 72 | '''Return a mutable version of this type. |
| 73 | |
| 74 | Convenience wrapper around MutableRebuilder.''' |
| 75 | visitor = MutableRebuilder() |
| 76 | return visitor.visit(self) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class _Void(Type): |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 80 | """Singleton void type.""" |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 81 | |
| 82 | def __init__(self): |
| 83 | Type.__init__(self, "void") |
| 84 | |
| 85 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 86 | return visitor.visitVoid(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 87 | |
| 88 | Void = _Void() |
| 89 | |
| 90 | |
| 91 | class Literal(Type): |
José Fonseca | 2f2ea48 | 2011-10-15 15:10:06 +0100 | [diff] [blame] | 92 | """Class to describe literal types. |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 93 | |
José Fonseca | 2f2ea48 | 2011-10-15 15:10:06 +0100 | [diff] [blame] | 94 | Types which are not defined in terms of other types, such as integers and |
| 95 | floats.""" |
| 96 | |
| 97 | def __init__(self, expr, kind): |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 98 | Type.__init__(self, expr) |
José Fonseca | 2f2ea48 | 2011-10-15 15:10:06 +0100 | [diff] [blame] | 99 | self.kind = kind |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 100 | |
| 101 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 102 | return visitor.visitLiteral(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 103 | |
| 104 | |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 105 | Bool = Literal("bool", "Bool") |
| 106 | SChar = Literal("signed char", "SInt") |
| 107 | UChar = Literal("unsigned char", "UInt") |
| 108 | Short = Literal("short", "SInt") |
| 109 | Int = Literal("int", "SInt") |
| 110 | Long = Literal("long", "SInt") |
| 111 | LongLong = Literal("long long", "SInt") |
| 112 | UShort = Literal("unsigned short", "UInt") |
| 113 | UInt = Literal("unsigned int", "UInt") |
| 114 | ULong = Literal("unsigned long", "UInt") |
| 115 | ULongLong = Literal("unsigned long long", "UInt") |
| 116 | Float = Literal("float", "Float") |
| 117 | Double = Literal("double", "Double") |
| 118 | SizeT = Literal("size_t", "UInt") |
| 119 | |
| 120 | Char = Literal("char", "SInt") |
| 121 | WChar = Literal("wchar_t", "SInt") |
| 122 | |
| 123 | Int8 = Literal("int8_t", "SInt") |
| 124 | UInt8 = Literal("uint8_t", "UInt") |
| 125 | Int16 = Literal("int16_t", "SInt") |
| 126 | UInt16 = Literal("uint16_t", "UInt") |
| 127 | Int32 = Literal("int32_t", "SInt") |
| 128 | UInt32 = Literal("uint32_t", "UInt") |
| 129 | Int64 = Literal("int64_t", "SInt") |
| 130 | UInt64 = Literal("uint64_t", "UInt") |
| 131 | |
José Fonseca | cb9d2e0 | 2012-10-19 14:51:48 +0100 | [diff] [blame^] | 132 | IntPtr = Literal("intptr_t", "SInt") |
| 133 | UIntPtr = Literal("uintptr_t", "UInt") |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 134 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 135 | class Const(Type): |
| 136 | |
| 137 | def __init__(self, type): |
José Fonseca | 903c2ca | 2011-09-23 09:43:05 +0100 | [diff] [blame] | 138 | # While "const foo" and "foo const" are synonymous, "const foo *" and |
| 139 | # "foo * const" are not quite the same, and some compilers do enforce |
| 140 | # strict const correctness. |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 141 | if type.expr.startswith("const ") or '*' in type.expr: |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 142 | expr = type.expr + " const" |
| 143 | else: |
José Fonseca | 903c2ca | 2011-09-23 09:43:05 +0100 | [diff] [blame] | 144 | # The most legible |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 145 | expr = "const " + type.expr |
| 146 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 147 | Type.__init__(self, expr, 'C' + type.tag) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 148 | |
| 149 | self.type = type |
| 150 | |
| 151 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 152 | return visitor.visitConst(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | class Pointer(Type): |
| 156 | |
| 157 | def __init__(self, type): |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 158 | Type.__init__(self, type.expr + " *", 'P' + type.tag) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 159 | self.type = type |
| 160 | |
| 161 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 162 | return visitor.visitPointer(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 163 | |
| 164 | |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 165 | class IntPointer(Type): |
| 166 | '''Integer encoded as a pointer.''' |
| 167 | |
| 168 | def visit(self, visitor, *args, **kwargs): |
| 169 | return visitor.visitIntPointer(self, *args, **kwargs) |
| 170 | |
| 171 | |
José Fonseca | fbcf683 | 2012-04-05 07:10:30 +0100 | [diff] [blame] | 172 | class ObjPointer(Type): |
| 173 | '''Pointer to an object.''' |
| 174 | |
| 175 | def __init__(self, type): |
| 176 | Type.__init__(self, type.expr + " *", 'P' + type.tag) |
| 177 | self.type = type |
| 178 | |
| 179 | def visit(self, visitor, *args, **kwargs): |
| 180 | return visitor.visitObjPointer(self, *args, **kwargs) |
| 181 | |
| 182 | |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 183 | class LinearPointer(Type): |
José Fonseca | fbcf683 | 2012-04-05 07:10:30 +0100 | [diff] [blame] | 184 | '''Pointer to a linear range of memory.''' |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 185 | |
| 186 | def __init__(self, type, size = None): |
| 187 | Type.__init__(self, type.expr + " *", 'P' + type.tag) |
| 188 | self.type = type |
| 189 | self.size = size |
| 190 | |
| 191 | def visit(self, visitor, *args, **kwargs): |
| 192 | return visitor.visitLinearPointer(self, *args, **kwargs) |
| 193 | |
| 194 | |
José Fonseca | b89c593 | 2012-04-01 22:47:11 +0200 | [diff] [blame] | 195 | class Reference(Type): |
| 196 | '''C++ references.''' |
| 197 | |
| 198 | def __init__(self, type): |
| 199 | Type.__init__(self, type.expr + " &", 'R' + type.tag) |
| 200 | self.type = type |
| 201 | |
| 202 | def visit(self, visitor, *args, **kwargs): |
| 203 | return visitor.visitReference(self, *args, **kwargs) |
| 204 | |
| 205 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 206 | class Handle(Type): |
| 207 | |
José Fonseca | 8a844ae | 2010-12-06 18:50:52 +0000 | [diff] [blame] | 208 | def __init__(self, name, type, range=None, key=None): |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 209 | Type.__init__(self, type.expr, 'P' + type.tag) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 210 | self.name = name |
| 211 | self.type = type |
| 212 | self.range = range |
José Fonseca | 8a844ae | 2010-12-06 18:50:52 +0000 | [diff] [blame] | 213 | self.key = key |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 214 | |
| 215 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 216 | return visitor.visitHandle(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | def ConstPointer(type): |
| 220 | return Pointer(Const(type)) |
| 221 | |
| 222 | |
| 223 | class Enum(Type): |
| 224 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 225 | __id = 0 |
| 226 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 227 | def __init__(self, name, values): |
| 228 | Type.__init__(self, name) |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 229 | |
| 230 | self.id = Enum.__id |
| 231 | Enum.__id += 1 |
| 232 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 233 | self.values = list(values) |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 234 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 235 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 236 | return visitor.visitEnum(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def FakeEnum(type, values): |
| 240 | return Enum(type.expr, values) |
| 241 | |
| 242 | |
| 243 | class Bitmask(Type): |
| 244 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 245 | __id = 0 |
| 246 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 247 | def __init__(self, type, values): |
| 248 | Type.__init__(self, type.expr) |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 249 | |
| 250 | self.id = Bitmask.__id |
| 251 | Bitmask.__id += 1 |
| 252 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 253 | self.type = type |
| 254 | self.values = values |
| 255 | |
| 256 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 257 | return visitor.visitBitmask(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 258 | |
| 259 | Flags = Bitmask |
| 260 | |
| 261 | |
| 262 | class Array(Type): |
| 263 | |
| 264 | def __init__(self, type, length): |
| 265 | Type.__init__(self, type.expr + " *") |
| 266 | self.type = type |
| 267 | self.length = length |
| 268 | |
| 269 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 270 | return visitor.visitArray(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | class Blob(Type): |
| 274 | |
| 275 | def __init__(self, type, size): |
| 276 | Type.__init__(self, type.expr + ' *') |
| 277 | self.type = type |
| 278 | self.size = size |
| 279 | |
| 280 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 281 | return visitor.visitBlob(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 282 | |
| 283 | |
| 284 | class Struct(Type): |
| 285 | |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 286 | __id = 0 |
| 287 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 288 | def __init__(self, name, members): |
| 289 | Type.__init__(self, name) |
José Fonseca | 02c2500 | 2011-10-15 13:17:26 +0100 | [diff] [blame] | 290 | |
| 291 | self.id = Struct.__id |
| 292 | Struct.__id += 1 |
| 293 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 294 | self.name = name |
José Fonseca | 5b6fb75 | 2012-04-14 14:56:45 +0100 | [diff] [blame] | 295 | self.members = [] |
| 296 | |
| 297 | # Eliminate anonymous unions |
| 298 | for type, name in members: |
| 299 | if name is not None: |
| 300 | self.members.append((type, name)) |
| 301 | else: |
| 302 | assert isinstance(type, Union) |
| 303 | assert type.name is None |
| 304 | self.members.extend(type.members) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 305 | |
| 306 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 307 | return visitor.visitStruct(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 308 | |
| 309 | |
José Fonseca | 5b6fb75 | 2012-04-14 14:56:45 +0100 | [diff] [blame] | 310 | class Union(Type): |
| 311 | |
| 312 | __id = 0 |
| 313 | |
| 314 | def __init__(self, name, members): |
| 315 | Type.__init__(self, name) |
| 316 | |
| 317 | self.id = Union.__id |
| 318 | Union.__id += 1 |
| 319 | |
| 320 | self.name = name |
| 321 | self.members = members |
| 322 | |
| 323 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 324 | class Alias(Type): |
| 325 | |
| 326 | def __init__(self, expr, type): |
| 327 | Type.__init__(self, expr) |
| 328 | self.type = type |
| 329 | |
| 330 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 331 | return visitor.visitAlias(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 332 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 333 | class Arg: |
| 334 | |
José Fonseca | 9dd8f70 | 2012-04-07 10:42:50 +0100 | [diff] [blame] | 335 | def __init__(self, type, name, input=True, output=False): |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 336 | self.type = type |
| 337 | self.name = name |
José Fonseca | 9dd8f70 | 2012-04-07 10:42:50 +0100 | [diff] [blame] | 338 | self.input = input |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 339 | self.output = output |
| 340 | self.index = None |
| 341 | |
| 342 | def __str__(self): |
| 343 | return '%s %s' % (self.type, self.name) |
| 344 | |
| 345 | |
José Fonseca | 9dd8f70 | 2012-04-07 10:42:50 +0100 | [diff] [blame] | 346 | def In(type, name): |
| 347 | return Arg(type, name, input=True, output=False) |
| 348 | |
| 349 | def Out(type, name): |
| 350 | return Arg(type, name, input=False, output=True) |
| 351 | |
| 352 | def InOut(type, name): |
| 353 | return Arg(type, name, input=True, output=True) |
| 354 | |
| 355 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 356 | class Function: |
| 357 | |
José Fonseca | 46a4839 | 2011-10-14 11:34:27 +0100 | [diff] [blame] | 358 | # 0-3 are reserved to memcpy, malloc, free, and realloc |
| 359 | __id = 4 |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 360 | |
José Fonseca | 84cea3b | 2012-05-09 21:12:30 +0100 | [diff] [blame] | 361 | def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False): |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 362 | self.id = Function.__id |
| 363 | Function.__id += 1 |
| 364 | |
| 365 | self.type = type |
| 366 | self.name = name |
| 367 | |
| 368 | self.args = [] |
| 369 | index = 0 |
| 370 | for arg in args: |
José Fonseca | 8384ccb | 2011-05-25 10:12:02 +0100 | [diff] [blame] | 371 | if not isinstance(arg, Arg): |
| 372 | if isinstance(arg, tuple): |
| 373 | arg_type, arg_name = arg |
| 374 | else: |
| 375 | arg_type = arg |
| 376 | arg_name = "arg%u" % index |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 377 | arg = Arg(arg_type, arg_name) |
| 378 | arg.index = index |
| 379 | index += 1 |
| 380 | self.args.append(arg) |
| 381 | |
| 382 | self.call = call |
| 383 | self.fail = fail |
| 384 | self.sideeffects = sideeffects |
José Fonseca | 84cea3b | 2012-05-09 21:12:30 +0100 | [diff] [blame] | 385 | self.internal = internal |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 386 | |
| 387 | def prototype(self, name=None): |
| 388 | if name is not None: |
| 389 | name = name.strip() |
| 390 | else: |
| 391 | name = self.name |
| 392 | s = name |
| 393 | if self.call: |
| 394 | s = self.call + ' ' + s |
| 395 | if name.startswith('*'): |
| 396 | s = '(' + s + ')' |
| 397 | s = self.type.expr + ' ' + s |
| 398 | s += "(" |
| 399 | if self.args: |
| 400 | s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args]) |
| 401 | else: |
| 402 | s += "void" |
| 403 | s += ")" |
| 404 | return s |
| 405 | |
José Fonseca | 568ecc2 | 2012-01-15 13:57:03 +0000 | [diff] [blame] | 406 | def argNames(self): |
| 407 | return [arg.name for arg in self.args] |
| 408 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 409 | |
| 410 | def StdFunction(*args, **kwargs): |
| 411 | kwargs.setdefault('call', '__stdcall') |
| 412 | return Function(*args, **kwargs) |
| 413 | |
| 414 | |
| 415 | def FunctionPointer(type, name, args, **kwargs): |
| 416 | # XXX: We should probably treat function pointers (callbacks or not) in a generic fashion |
| 417 | return Opaque(name) |
| 418 | |
| 419 | |
| 420 | class Interface(Type): |
| 421 | |
| 422 | def __init__(self, name, base=None): |
| 423 | Type.__init__(self, name) |
| 424 | self.name = name |
| 425 | self.base = base |
| 426 | self.methods = [] |
| 427 | |
| 428 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 429 | return visitor.visitInterface(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 430 | |
José Fonseca | bd08634 | 2012-04-18 19:58:32 +0100 | [diff] [blame] | 431 | def getMethodByName(self, name): |
José Fonseca | d275d0a | 2012-04-30 23:18:05 +0100 | [diff] [blame] | 432 | for method in self.iterMethods(): |
| 433 | if method.name == name: |
| 434 | return method |
José Fonseca | bd08634 | 2012-04-18 19:58:32 +0100 | [diff] [blame] | 435 | return None |
| 436 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 437 | def iterMethods(self): |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 438 | if self.base is not None: |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 439 | for method in self.base.iterMethods(): |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 440 | yield method |
| 441 | for method in self.methods: |
| 442 | yield method |
| 443 | raise StopIteration |
| 444 | |
José Fonseca | 143e925 | 2012-04-15 09:31:18 +0100 | [diff] [blame] | 445 | def iterBases(self): |
| 446 | iface = self |
| 447 | while iface is not None: |
| 448 | yield iface |
| 449 | iface = iface.base |
| 450 | raise StopIteration |
| 451 | |
José Fonseca | 4220b1b | 2012-02-03 19:05:29 +0000 | [diff] [blame] | 452 | def iterBaseMethods(self): |
| 453 | if self.base is not None: |
| 454 | for iface, method in self.base.iterBaseMethods(): |
| 455 | yield iface, method |
| 456 | for method in self.methods: |
| 457 | yield self, method |
| 458 | raise StopIteration |
| 459 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 460 | |
| 461 | class Method(Function): |
| 462 | |
José Fonseca | 5b6fb75 | 2012-04-14 14:56:45 +0100 | [diff] [blame] | 463 | def __init__(self, type, name, args, call = '__stdcall', const=False, sideeffects=True): |
| 464 | Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 465 | for index in range(len(self.args)): |
| 466 | self.args[index].index = index + 1 |
José Fonseca | 9dbeda6 | 2012-02-03 19:05:54 +0000 | [diff] [blame] | 467 | self.const = const |
| 468 | |
| 469 | def prototype(self, name=None): |
| 470 | s = Function.prototype(self, name) |
| 471 | if self.const: |
| 472 | s += ' const' |
| 473 | return s |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 474 | |
José Fonseca | bcb26b2 | 2012-04-15 08:42:25 +0100 | [diff] [blame] | 475 | |
José Fonseca | 5b6fb75 | 2012-04-14 14:56:45 +0100 | [diff] [blame] | 476 | def StdMethod(*args, **kwargs): |
| 477 | kwargs.setdefault('call', '__stdcall') |
| 478 | return Method(*args, **kwargs) |
| 479 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 480 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 481 | class String(Type): |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 482 | '''Human-legible character string.''' |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 483 | |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 484 | def __init__(self, type = Char, length = None, wide = False): |
| 485 | assert isinstance(type, Type) |
| 486 | Type.__init__(self, type.expr + ' *') |
| 487 | self.type = type |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 488 | self.length = length |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 489 | self.wide = wide |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 490 | |
| 491 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 492 | return visitor.visitString(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 493 | |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 494 | |
| 495 | class Opaque(Type): |
| 496 | '''Opaque pointer.''' |
| 497 | |
| 498 | def __init__(self, expr): |
| 499 | Type.__init__(self, expr) |
| 500 | |
| 501 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 502 | return visitor.visitOpaque(self, *args, **kwargs) |
José Fonseca | 6fac5ae | 2010-11-29 16:09:13 +0000 | [diff] [blame] | 503 | |
| 504 | |
| 505 | def OpaquePointer(type, *args): |
| 506 | return Opaque(type.expr + ' *') |
| 507 | |
| 508 | def OpaqueArray(type, size): |
| 509 | return Opaque(type.expr + ' *') |
| 510 | |
| 511 | def OpaqueBlob(type, size): |
| 512 | return Opaque(type.expr + ' *') |
José Fonseca | 8a56d14 | 2008-07-09 12:18:08 +0900 | [diff] [blame] | 513 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 514 | |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 515 | class Polymorphic(Type): |
| 516 | |
José Fonseca | b95e372 | 2012-04-16 14:01:15 +0100 | [diff] [blame] | 517 | def __init__(self, switchExpr, switchTypes, defaultType, contextLess=True): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 518 | Type.__init__(self, defaultType.expr) |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 519 | self.switchExpr = switchExpr |
| 520 | self.switchTypes = switchTypes |
José Fonseca | b95e372 | 2012-04-16 14:01:15 +0100 | [diff] [blame] | 521 | self.defaultType = defaultType |
| 522 | self.contextLess = contextLess |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 523 | |
| 524 | def visit(self, visitor, *args, **kwargs): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 525 | return visitor.visitPolymorphic(self, *args, **kwargs) |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 526 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 527 | def iterSwitch(self): |
José Fonseca | 4616111 | 2011-10-14 10:04:55 +0100 | [diff] [blame] | 528 | cases = [['default']] |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 529 | types = [self.defaultType] |
José Fonseca | 4616111 | 2011-10-14 10:04:55 +0100 | [diff] [blame] | 530 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 531 | for expr, type in self.switchTypes: |
José Fonseca | 4616111 | 2011-10-14 10:04:55 +0100 | [diff] [blame] | 532 | case = 'case %s' % expr |
| 533 | try: |
| 534 | i = types.index(type) |
| 535 | except ValueError: |
| 536 | cases.append([case]) |
| 537 | types.append(type) |
| 538 | else: |
| 539 | cases[i].append(case) |
| 540 | |
| 541 | return zip(cases, types) |
| 542 | |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 543 | |
José Fonseca | b95e372 | 2012-04-16 14:01:15 +0100 | [diff] [blame] | 544 | def EnumPolymorphic(enumName, switchExpr, switchTypes, defaultType, contextLess=True): |
| 545 | enumValues = [expr for expr, type in switchTypes] |
| 546 | enum = Enum(enumName, enumValues) |
| 547 | polymorphic = Polymorphic(switchExpr, switchTypes, defaultType, contextLess) |
| 548 | return enum, polymorphic |
| 549 | |
| 550 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 551 | class Visitor: |
José Fonseca | 9c4a257 | 2012-01-13 23:21:10 +0000 | [diff] [blame] | 552 | '''Abstract visitor for the type hierarchy.''' |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 553 | |
| 554 | def visit(self, type, *args, **kwargs): |
| 555 | return type.visit(self, *args, **kwargs) |
| 556 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 557 | def visitVoid(self, void, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 558 | raise NotImplementedError |
| 559 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 560 | def visitLiteral(self, literal, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 561 | raise NotImplementedError |
| 562 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 563 | def visitString(self, string, *args, **kwargs): |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 564 | raise NotImplementedError |
| 565 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 566 | def visitConst(self, const, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 567 | raise NotImplementedError |
| 568 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 569 | def visitStruct(self, struct, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 570 | raise NotImplementedError |
| 571 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 572 | def visitArray(self, array, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 573 | raise NotImplementedError |
| 574 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 575 | def visitBlob(self, blob, *args, **kwargs): |
José Fonseca | 885f265 | 2010-11-20 11:22:25 +0000 | [diff] [blame] | 576 | raise NotImplementedError |
| 577 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 578 | def visitEnum(self, enum, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 579 | raise NotImplementedError |
| 580 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 581 | def visitBitmask(self, bitmask, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 582 | raise NotImplementedError |
| 583 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 584 | def visitPointer(self, pointer, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 585 | raise NotImplementedError |
| 586 | |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 587 | def visitIntPointer(self, pointer, *args, **kwargs): |
| 588 | raise NotImplementedError |
| 589 | |
José Fonseca | fbcf683 | 2012-04-05 07:10:30 +0100 | [diff] [blame] | 590 | def visitObjPointer(self, pointer, *args, **kwargs): |
| 591 | raise NotImplementedError |
| 592 | |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 593 | def visitLinearPointer(self, pointer, *args, **kwargs): |
| 594 | raise NotImplementedError |
| 595 | |
José Fonseca | b89c593 | 2012-04-01 22:47:11 +0200 | [diff] [blame] | 596 | def visitReference(self, reference, *args, **kwargs): |
| 597 | raise NotImplementedError |
| 598 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 599 | def visitHandle(self, handle, *args, **kwargs): |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame] | 600 | raise NotImplementedError |
| 601 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 602 | def visitAlias(self, alias, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 603 | raise NotImplementedError |
| 604 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 605 | def visitOpaque(self, opaque, *args, **kwargs): |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 606 | raise NotImplementedError |
| 607 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 608 | def visitInterface(self, interface, *args, **kwargs): |
José Fonseca | c356d6a | 2010-11-23 14:27:25 +0000 | [diff] [blame] | 609 | raise NotImplementedError |
| 610 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 611 | def visitPolymorphic(self, polymorphic, *args, **kwargs): |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 612 | raise NotImplementedError |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 613 | #return self.visit(polymorphic.defaultType, *args, **kwargs) |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 614 | |
José Fonseca | c356d6a | 2010-11-23 14:27:25 +0000 | [diff] [blame] | 615 | |
| 616 | class OnceVisitor(Visitor): |
José Fonseca | 9c4a257 | 2012-01-13 23:21:10 +0000 | [diff] [blame] | 617 | '''Visitor that guarantees that each type is visited only once.''' |
José Fonseca | c356d6a | 2010-11-23 14:27:25 +0000 | [diff] [blame] | 618 | |
| 619 | def __init__(self): |
| 620 | self.__visited = set() |
| 621 | |
| 622 | def visit(self, type, *args, **kwargs): |
| 623 | if type not in self.__visited: |
| 624 | self.__visited.add(type) |
| 625 | return type.visit(self, *args, **kwargs) |
| 626 | return None |
| 627 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 628 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 629 | class Rebuilder(Visitor): |
José Fonseca | 9c4a257 | 2012-01-13 23:21:10 +0000 | [diff] [blame] | 630 | '''Visitor which rebuild types as it visits them. |
| 631 | |
| 632 | By itself it is a no-op -- it is intended to be overwritten. |
| 633 | ''' |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 634 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 635 | def visitVoid(self, void): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 636 | return void |
| 637 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 638 | def visitLiteral(self, literal): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 639 | return literal |
| 640 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 641 | def visitString(self, string): |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 642 | string_type = self.visit(string.type) |
| 643 | if string_type is string.type: |
| 644 | return string |
| 645 | else: |
| 646 | return String(string_type, string.length, string.wide) |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 647 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 648 | def visitConst(self, const): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 649 | const_type = self.visit(const.type) |
| 650 | if const_type is const.type: |
| 651 | return const |
| 652 | else: |
| 653 | return Const(const_type) |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 654 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 655 | def visitStruct(self, struct): |
José Fonseca | 06aa284 | 2011-05-05 07:55:54 +0100 | [diff] [blame] | 656 | members = [(self.visit(type), name) for type, name in struct.members] |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 657 | return Struct(struct.name, members) |
| 658 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 659 | def visitArray(self, array): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 660 | type = self.visit(array.type) |
| 661 | return Array(type, array.length) |
| 662 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 663 | def visitBlob(self, blob): |
José Fonseca | 885f265 | 2010-11-20 11:22:25 +0000 | [diff] [blame] | 664 | type = self.visit(blob.type) |
| 665 | return Blob(type, blob.size) |
| 666 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 667 | def visitEnum(self, enum): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 668 | return enum |
| 669 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 670 | def visitBitmask(self, bitmask): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 671 | type = self.visit(bitmask.type) |
| 672 | return Bitmask(type, bitmask.values) |
| 673 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 674 | def visitPointer(self, pointer): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 675 | pointer_type = self.visit(pointer.type) |
| 676 | if pointer_type is pointer.type: |
| 677 | return pointer |
| 678 | else: |
| 679 | return Pointer(pointer_type) |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 680 | |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 681 | def visitIntPointer(self, pointer): |
| 682 | return pointer |
| 683 | |
José Fonseca | fbcf683 | 2012-04-05 07:10:30 +0100 | [diff] [blame] | 684 | def visitObjPointer(self, pointer): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 685 | pointer_type = self.visit(pointer.type) |
| 686 | if pointer_type is pointer.type: |
| 687 | return pointer |
| 688 | else: |
| 689 | return ObjPointer(pointer_type) |
José Fonseca | fbcf683 | 2012-04-05 07:10:30 +0100 | [diff] [blame] | 690 | |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 691 | def visitLinearPointer(self, pointer): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 692 | pointer_type = self.visit(pointer.type) |
| 693 | if pointer_type is pointer.type: |
| 694 | return pointer |
| 695 | else: |
| 696 | return LinearPointer(pointer_type) |
José Fonseca | 59ee88e | 2012-01-15 14:24:10 +0000 | [diff] [blame] | 697 | |
José Fonseca | b89c593 | 2012-04-01 22:47:11 +0200 | [diff] [blame] | 698 | def visitReference(self, reference): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 699 | reference_type = self.visit(reference.type) |
| 700 | if reference_type is reference.type: |
| 701 | return reference |
| 702 | else: |
| 703 | return Reference(reference_type) |
José Fonseca | b89c593 | 2012-04-01 22:47:11 +0200 | [diff] [blame] | 704 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 705 | def visitHandle(self, handle): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 706 | handle_type = self.visit(handle.type) |
| 707 | if handle_type is handle.type: |
| 708 | return handle |
| 709 | else: |
| 710 | return Handle(handle.name, handle_type, range=handle.range, key=handle.key) |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame] | 711 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 712 | def visitAlias(self, alias): |
José Fonseca | f182eda | 2012-04-05 19:59:56 +0100 | [diff] [blame] | 713 | alias_type = self.visit(alias.type) |
| 714 | if alias_type is alias.type: |
| 715 | return alias |
| 716 | else: |
| 717 | return Alias(alias.expr, alias_type) |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 718 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 719 | def visitOpaque(self, opaque): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 720 | return opaque |
| 721 | |
José Fonseca | 7814edf | 2012-01-31 10:55:49 +0000 | [diff] [blame] | 722 | def visitInterface(self, interface, *args, **kwargs): |
| 723 | return interface |
| 724 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 725 | def visitPolymorphic(self, polymorphic): |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 726 | switchExpr = polymorphic.switchExpr |
| 727 | switchTypes = [(expr, self.visit(type)) for expr, type in polymorphic.switchTypes] |
José Fonseca | b95e372 | 2012-04-16 14:01:15 +0100 | [diff] [blame] | 728 | defaultType = self.visit(polymorphic.defaultType) |
| 729 | return Polymorphic(switchExpr, switchTypes, defaultType, polymorphic.contextLess) |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 730 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 731 | |
José Fonseca | 0075f15 | 2012-04-14 20:25:52 +0100 | [diff] [blame] | 732 | class MutableRebuilder(Rebuilder): |
| 733 | '''Type visitor which derives a mutable type.''' |
| 734 | |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 735 | def visitString(self, string): |
| 736 | return string |
| 737 | |
José Fonseca | 0075f15 | 2012-04-14 20:25:52 +0100 | [diff] [blame] | 738 | def visitConst(self, const): |
| 739 | # Strip out const qualifier |
| 740 | return const.type |
| 741 | |
| 742 | def visitAlias(self, alias): |
| 743 | # Tear the alias on type changes |
| 744 | type = self.visit(alias.type) |
| 745 | if type is alias.type: |
| 746 | return alias |
| 747 | return type |
| 748 | |
| 749 | def visitReference(self, reference): |
| 750 | # Strip out references |
| 751 | return reference.type |
| 752 | |
| 753 | |
| 754 | class Traverser(Visitor): |
| 755 | '''Visitor which all types.''' |
| 756 | |
| 757 | def visitVoid(self, void, *args, **kwargs): |
| 758 | pass |
| 759 | |
| 760 | def visitLiteral(self, literal, *args, **kwargs): |
| 761 | pass |
| 762 | |
| 763 | def visitString(self, string, *args, **kwargs): |
| 764 | pass |
| 765 | |
| 766 | def visitConst(self, const, *args, **kwargs): |
| 767 | self.visit(const.type, *args, **kwargs) |
| 768 | |
| 769 | def visitStruct(self, struct, *args, **kwargs): |
| 770 | for type, name in struct.members: |
| 771 | self.visit(type, *args, **kwargs) |
| 772 | |
| 773 | def visitArray(self, array, *args, **kwargs): |
| 774 | self.visit(array.type, *args, **kwargs) |
| 775 | |
| 776 | def visitBlob(self, array, *args, **kwargs): |
| 777 | pass |
| 778 | |
| 779 | def visitEnum(self, enum, *args, **kwargs): |
| 780 | pass |
| 781 | |
| 782 | def visitBitmask(self, bitmask, *args, **kwargs): |
| 783 | self.visit(bitmask.type, *args, **kwargs) |
| 784 | |
| 785 | def visitPointer(self, pointer, *args, **kwargs): |
| 786 | self.visit(pointer.type, *args, **kwargs) |
| 787 | |
| 788 | def visitIntPointer(self, pointer, *args, **kwargs): |
| 789 | pass |
| 790 | |
| 791 | def visitObjPointer(self, pointer, *args, **kwargs): |
| 792 | self.visit(pointer.type, *args, **kwargs) |
| 793 | |
| 794 | def visitLinearPointer(self, pointer, *args, **kwargs): |
| 795 | self.visit(pointer.type, *args, **kwargs) |
| 796 | |
| 797 | def visitReference(self, reference, *args, **kwargs): |
| 798 | self.visit(reference.type, *args, **kwargs) |
| 799 | |
| 800 | def visitHandle(self, handle, *args, **kwargs): |
| 801 | self.visit(handle.type, *args, **kwargs) |
| 802 | |
| 803 | def visitAlias(self, alias, *args, **kwargs): |
| 804 | self.visit(alias.type, *args, **kwargs) |
| 805 | |
| 806 | def visitOpaque(self, opaque, *args, **kwargs): |
| 807 | pass |
| 808 | |
| 809 | def visitInterface(self, interface, *args, **kwargs): |
| 810 | if interface.base is not None: |
| 811 | self.visit(interface.base, *args, **kwargs) |
| 812 | for method in interface.iterMethods(): |
| 813 | for arg in method.args: |
| 814 | self.visit(arg.type, *args, **kwargs) |
| 815 | self.visit(method.type, *args, **kwargs) |
| 816 | |
| 817 | def visitPolymorphic(self, polymorphic, *args, **kwargs): |
| 818 | self.visit(polymorphic.defaultType, *args, **kwargs) |
| 819 | for expr, type in polymorphic.switchTypes: |
| 820 | self.visit(type, *args, **kwargs) |
| 821 | |
| 822 | |
| 823 | class Collector(Traverser): |
José Fonseca | 9c4a257 | 2012-01-13 23:21:10 +0000 | [diff] [blame] | 824 | '''Visitor which collects all unique types as it traverses them.''' |
José Fonseca | e6a50bd | 2010-11-24 10:12:22 +0000 | [diff] [blame] | 825 | |
| 826 | def __init__(self): |
| 827 | self.__visited = set() |
| 828 | self.types = [] |
| 829 | |
| 830 | def visit(self, type): |
| 831 | if type in self.__visited: |
| 832 | return |
| 833 | self.__visited.add(type) |
| 834 | Visitor.visit(self, type) |
| 835 | self.types.append(type) |
| 836 | |
José Fonseca | 16d46dd | 2011-10-13 09:52:52 +0100 | [diff] [blame] | 837 | |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 838 | |
| 839 | class API: |
José Fonseca | 9c4a257 | 2012-01-13 23:21:10 +0000 | [diff] [blame] | 840 | '''API abstraction. |
| 841 | |
| 842 | Essentially, a collection of types, functions, and interfaces. |
| 843 | ''' |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 844 | |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 845 | def __init__(self, name = None): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 846 | self.name = name |
| 847 | self.headers = [] |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 848 | self.functions = [] |
| 849 | self.interfaces = [] |
| 850 | |
José Fonseca | 4470382 | 2012-01-31 10:48:58 +0000 | [diff] [blame] | 851 | def getAllTypes(self): |
José Fonseca | e6a50bd | 2010-11-24 10:12:22 +0000 | [diff] [blame] | 852 | collector = Collector() |
| 853 | for function in self.functions: |
| 854 | for arg in function.args: |
| 855 | collector.visit(arg.type) |
| 856 | collector.visit(function.type) |
| 857 | for interface in self.interfaces: |
| 858 | collector.visit(interface) |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 859 | for method in interface.iterMethods(): |
José Fonseca | e6a50bd | 2010-11-24 10:12:22 +0000 | [diff] [blame] | 860 | for arg in method.args: |
| 861 | collector.visit(arg.type) |
| 862 | collector.visit(method.type) |
| 863 | return collector.types |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 864 | |
José Fonseca | 2ef6d5b | 2012-01-31 10:56:38 +0000 | [diff] [blame] | 865 | def getAllInterfaces(self): |
| 866 | types = self.getAllTypes() |
| 867 | interfaces = [type for type in types if isinstance(type, Interface)] |
| 868 | for interface in self.interfaces: |
| 869 | if interface not in interfaces: |
| 870 | interfaces.append(interface) |
| 871 | return interfaces |
| 872 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 873 | def addFunction(self, function): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 874 | self.functions.append(function) |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 875 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 876 | def addFunctions(self, functions): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 877 | for function in functions: |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 878 | self.addFunction(function) |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 879 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 880 | def addInterface(self, interface): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 881 | self.interfaces.append(interface) |
| 882 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 883 | def addInterfaces(self, interfaces): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 884 | self.interfaces.extend(interfaces) |
| 885 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 886 | def addApi(self, api): |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 887 | self.headers.extend(api.headers) |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 888 | self.addFunctions(api.functions) |
| 889 | self.addInterfaces(api.interfaces) |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 890 | |
José Fonseca | 1b6c875 | 2012-04-15 14:33:00 +0100 | [diff] [blame] | 891 | def getFunctionByName(self, name): |
José Fonseca | eccec3e | 2011-02-20 09:01:25 +0000 | [diff] [blame] | 892 | for function in self.functions: |
| 893 | if function.name == name: |
| 894 | return function |
| 895 | return None |
| 896 | |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 897 | |
José Fonseca | 280a176 | 2012-01-31 15:10:13 +0000 | [diff] [blame] | 898 | # C string (i.e., zero terminated) |
José Fonseca | bcfc81b | 2012-08-07 21:07:22 +0100 | [diff] [blame] | 899 | CString = String(Char) |
| 900 | WString = String(WChar, wide=True) |
| 901 | ConstCString = String(Const(Char)) |
| 902 | ConstWString = String(Const(WChar), wide=True) |