| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import sys |
| 19 |
from typing import Any, ClassVar, IO, Optional |
| 20 |
|
| 21 |
def _repr(obj): |
| 22 |
""" |
| 23 |
Get the representation of an object, with dedicated pprint-like format for lists. |
| 24 |
""" |
| 25 |
if isinstance(obj, list): |
| 26 |
return '[' + (',\n '.join((_repr(e).replace('\n', '\n ') for e in obj))) + '\n]' |
| 27 |
else: |
| 28 |
return repr(obj) |
| 29 |
|
| 30 |
class Node: |
| 31 |
__slots__ = () |
| 32 |
""" Abstract base class for AST nodes. |
| 33 |
""" |
| 34 |
attr_names: ClassVar[tuple[str, ...]] = () |
| 35 |
coord: Optional[Any] |
| 36 |
def __repr__(self): |
| 37 |
""" Generates a python representation of the current node |
| 38 |
""" |
| 39 |
result = self.__class__.__name__ + '(' |
| 40 |
|
| 41 |
indent = '' |
| 42 |
separator = '' |
| 43 |
for name in self.__slots__[:-2]: |
| 44 |
result += separator |
| 45 |
result += indent |
| 46 |
result += name + '=' + (_repr(getattr(self, name)).replace('\n', '\n ' + (' ' * (len(name) + len(self.__class__.__name__))))) |
| 47 |
|
| 48 |
separator = ',' |
| 49 |
indent = '\n ' + (' ' * len(self.__class__.__name__)) |
| 50 |
|
| 51 |
result += indent + ')' |
| 52 |
|
| 53 |
return result |
| 54 |
|
| 55 |
def children(self): |
| 56 |
""" A sequence of all children that are Nodes |
| 57 |
""" |
| 58 |
pass |
| 59 |
|
| 60 |
def show( |
| 61 |
self, |
| 62 |
buf: IO[str] = sys.stdout, |
| 63 |
offset: int = 0, |
| 64 |
attrnames: bool = False, |
| 65 |
showemptyattrs: bool = True, |
| 66 |
nodenames: bool = False, |
| 67 |
showcoord: bool = False, |
| 68 |
_my_node_name: Optional[str] = None, |
| 69 |
): |
| 70 |
""" Pretty print the Node and all its attributes and |
| 71 |
children (recursively) to a buffer. |
| 72 |
|
| 73 |
buf: |
| 74 |
Open IO buffer into which the Node is printed. |
| 75 |
|
| 76 |
offset: |
| 77 |
Initial offset (amount of leading spaces) |
| 78 |
|
| 79 |
attrnames: |
| 80 |
True if you want to see the attribute names in |
| 81 |
name=value pairs. False to only see the values. |
| 82 |
|
| 83 |
showemptyattrs: |
| 84 |
False if you want to suppress printing empty attributes. |
| 85 |
|
| 86 |
nodenames: |
| 87 |
True if you want to see the actual node names |
| 88 |
within their parents. |
| 89 |
|
| 90 |
showcoord: |
| 91 |
Do you want the coordinates of each Node to be |
| 92 |
displayed. |
| 93 |
""" |
| 94 |
lead = ' ' * offset |
| 95 |
if nodenames and _my_node_name is not None: |
| 96 |
buf.write(lead + self.__class__.__name__+ ' <' + _my_node_name + '>: ') |
| 97 |
else: |
| 98 |
buf.write(lead + self.__class__.__name__+ ': ') |
| 99 |
|
| 100 |
if self.attr_names: |
| 101 |
def is_empty(v): |
| 102 |
v is None or (hasattr(v, '__len__') and len(v) == 0) |
| 103 |
nvlist = [(n, getattr(self,n)) for n in self.attr_names \ |
| 104 |
if showemptyattrs or not is_empty(getattr(self,n))] |
| 105 |
if attrnames: |
| 106 |
attrstr = ', '.join(f'{name}={value}' for name, value in nvlist) |
| 107 |
else: |
| 108 |
attrstr = ', '.join(f'{value}' for _, value in nvlist) |
| 109 |
buf.write(attrstr) |
| 110 |
|
| 111 |
if showcoord: |
| 112 |
buf.write(f' (at {self.coord})') |
| 113 |
buf.write('\n') |
| 114 |
|
| 115 |
for (child_name, child) in self.children(): |
| 116 |
child.show( |
| 117 |
buf, |
| 118 |
offset=offset + 2, |
| 119 |
attrnames=attrnames, |
| 120 |
showemptyattrs=showemptyattrs, |
| 121 |
nodenames=nodenames, |
| 122 |
showcoord=showcoord, |
| 123 |
_my_node_name=child_name) |
| 124 |
|
| 125 |
|
| 126 |
class NodeVisitor: |
| 127 |
""" A base NodeVisitor class for visiting c_ast nodes. |
| 128 |
Subclass it and define your own visit_XXX methods, where |
| 129 |
XXX is the class name you want to visit with these |
| 130 |
methods. |
| 131 |
|
| 132 |
For example: |
| 133 |
|
| 134 |
class ConstantVisitor(NodeVisitor): |
| 135 |
def __init__(self): |
| 136 |
self.values = [] |
| 137 |
|
| 138 |
def visit_Constant(self, node): |
| 139 |
self.values.append(node.value) |
| 140 |
|
| 141 |
Creates a list of values of all the constant nodes |
| 142 |
encountered below the given node. To use it: |
| 143 |
|
| 144 |
cv = ConstantVisitor() |
| 145 |
cv.visit(node) |
| 146 |
|
| 147 |
Notes: |
| 148 |
|
| 149 |
* generic_visit() will be called for AST nodes for which |
| 150 |
no visit_XXX method was defined. |
| 151 |
* The children of nodes for which a visit_XXX was |
| 152 |
defined will not be visited - if you need this, call |
| 153 |
generic_visit() on the node. |
| 154 |
You can use: |
| 155 |
NodeVisitor.generic_visit(self, node) |
| 156 |
* Modeled after Python's own AST visiting facilities |
| 157 |
(the ast module of Python 3.0) |
| 158 |
""" |
| 159 |
|
| 160 |
_method_cache = None |
| 161 |
|
| 162 |
def visit(self, node: Node): |
| 163 |
""" Visit a node. |
| 164 |
""" |
| 165 |
|
| 166 |
if self._method_cache is None: |
| 167 |
self._method_cache = {} |
| 168 |
|
| 169 |
visitor = self._method_cache.get(node.__class__.__name__, None) |
| 170 |
if visitor is None: |
| 171 |
method = 'visit_' + node.__class__.__name__ |
| 172 |
visitor = getattr(self, method, self.generic_visit) |
| 173 |
self._method_cache[node.__class__.__name__] = visitor |
| 174 |
|
| 175 |
return visitor(node) |
| 176 |
|
| 177 |
def generic_visit(self, node: Node): |
| 178 |
""" Called if no explicit visitor function exists for a |
| 179 |
node. Implements preorder visiting of the node. |
| 180 |
""" |
| 181 |
for _, c in node.children(): |
| 182 |
self.visit(c) |
| 183 |
|
| 184 |
class ArrayDecl(Node): |
| 185 |
__slots__ = ('type', 'dim', 'dim_quals', 'coord', '__weakref__') |
| 186 |
def __init__(self, type, dim, dim_quals, coord=None): |
| 187 |
self.type = type |
| 188 |
self.dim = dim |
| 189 |
self.dim_quals = dim_quals |
| 190 |
self.coord = coord |
| 191 |
|
| 192 |
def children(self): |
| 193 |
nodelist = [] |
| 194 |
if self.type is not None: |
| 195 |
nodelist.append(("type", self.type)) |
| 196 |
if self.dim is not None: |
| 197 |
nodelist.append(("dim", self.dim)) |
| 198 |
return tuple(nodelist) |
| 199 |
|
| 200 |
def __iter__(self): |
| 201 |
if self.type is not None: |
| 202 |
yield self.type |
| 203 |
if self.dim is not None: |
| 204 |
yield self.dim |
| 205 |
|
| 206 |
attr_names = ('dim_quals', ) |
| 207 |
|
| 208 |
class ArrayRef(Node): |
| 209 |
__slots__ = ('name', 'subscript', 'coord', '__weakref__') |
| 210 |
def __init__(self, name, subscript, coord=None): |
| 211 |
self.name = name |
| 212 |
self.subscript = subscript |
| 213 |
self.coord = coord |
| 214 |
|
| 215 |
def children(self): |
| 216 |
nodelist = [] |
| 217 |
if self.name is not None: |
| 218 |
nodelist.append(("name", self.name)) |
| 219 |
if self.subscript is not None: |
| 220 |
nodelist.append(("subscript", self.subscript)) |
| 221 |
return tuple(nodelist) |
| 222 |
|
| 223 |
def __iter__(self): |
| 224 |
if self.name is not None: |
| 225 |
yield self.name |
| 226 |
if self.subscript is not None: |
| 227 |
yield self.subscript |
| 228 |
|
| 229 |
attr_names = () |
| 230 |
|
| 231 |
class Assignment(Node): |
| 232 |
__slots__ = ('op', 'lvalue', 'rvalue', 'coord', '__weakref__') |
| 233 |
def __init__(self, op, lvalue, rvalue, coord=None): |
| 234 |
self.op = op |
| 235 |
self.lvalue = lvalue |
| 236 |
self.rvalue = rvalue |
| 237 |
self.coord = coord |
| 238 |
|
| 239 |
def children(self): |
| 240 |
nodelist = [] |
| 241 |
if self.lvalue is not None: |
| 242 |
nodelist.append(("lvalue", self.lvalue)) |
| 243 |
if self.rvalue is not None: |
| 244 |
nodelist.append(("rvalue", self.rvalue)) |
| 245 |
return tuple(nodelist) |
| 246 |
|
| 247 |
def __iter__(self): |
| 248 |
if self.lvalue is not None: |
| 249 |
yield self.lvalue |
| 250 |
if self.rvalue is not None: |
| 251 |
yield self.rvalue |
| 252 |
|
| 253 |
attr_names = ('op', ) |
| 254 |
|
| 255 |
class Alignas(Node): |
| 256 |
__slots__ = ('alignment', 'coord', '__weakref__') |
| 257 |
def __init__(self, alignment, coord=None): |
| 258 |
self.alignment = alignment |
| 259 |
self.coord = coord |
| 260 |
|
| 261 |
def children(self): |
| 262 |
nodelist = [] |
| 263 |
if self.alignment is not None: |
| 264 |
nodelist.append(("alignment", self.alignment)) |
| 265 |
return tuple(nodelist) |
| 266 |
|
| 267 |
def __iter__(self): |
| 268 |
if self.alignment is not None: |
| 269 |
yield self.alignment |
| 270 |
|
| 271 |
attr_names = () |
| 272 |
|
| 273 |
class BinaryOp(Node): |
| 274 |
__slots__ = ('op', 'left', 'right', 'coord', '__weakref__') |
| 275 |
def __init__(self, op, left, right, coord=None): |
| 276 |
self.op = op |
| 277 |
self.left = left |
| 278 |
self.right = right |
| 279 |
self.coord = coord |
| 280 |
|
| 281 |
def children(self): |
| 282 |
nodelist = [] |
| 283 |
if self.left is not None: |
| 284 |
nodelist.append(("left", self.left)) |
| 285 |
if self.right is not None: |
| 286 |
nodelist.append(("right", self.right)) |
| 287 |
return tuple(nodelist) |
| 288 |
|
| 289 |
def __iter__(self): |
| 290 |
if self.left is not None: |
| 291 |
yield self.left |
| 292 |
if self.right is not None: |
| 293 |
yield self.right |
| 294 |
|
| 295 |
attr_names = ('op', ) |
| 296 |
|
| 297 |
class Break(Node): |
| 298 |
__slots__ = ('coord', '__weakref__') |
| 299 |
def __init__(self, coord=None): |
| 300 |
self.coord = coord |
| 301 |
|
| 302 |
def children(self): |
| 303 |
return () |
| 304 |
|
| 305 |
def __iter__(self): |
| 306 |
return |
| 307 |
yield |
| 308 |
|
| 309 |
attr_names = () |
| 310 |
|
| 311 |
class Case(Node): |
| 312 |
__slots__ = ('expr', 'stmts', 'coord', '__weakref__') |
| 313 |
def __init__(self, expr, stmts, coord=None): |
| 314 |
self.expr = expr |
| 315 |
self.stmts = stmts |
| 316 |
self.coord = coord |
| 317 |
|
| 318 |
def children(self): |
| 319 |
nodelist = [] |
| 320 |
if self.expr is not None: |
| 321 |
nodelist.append(("expr", self.expr)) |
| 322 |
for i, child in enumerate(self.stmts or []): |
| 323 |
nodelist.append((f"stmts[{i}]", child)) |
| 324 |
return tuple(nodelist) |
| 325 |
|
| 326 |
def __iter__(self): |
| 327 |
if self.expr is not None: |
| 328 |
yield self.expr |
| 329 |
for child in (self.stmts or []): |
| 330 |
yield child |
| 331 |
|
| 332 |
attr_names = () |
| 333 |
|
| 334 |
class Cast(Node): |
| 335 |
__slots__ = ('to_type', 'expr', 'coord', '__weakref__') |
| 336 |
def __init__(self, to_type, expr, coord=None): |
| 337 |
self.to_type = to_type |
| 338 |
self.expr = expr |
| 339 |
self.coord = coord |
| 340 |
|
| 341 |
def children(self): |
| 342 |
nodelist = [] |
| 343 |
if self.to_type is not None: |
| 344 |
nodelist.append(("to_type", self.to_type)) |
| 345 |
if self.expr is not None: |
| 346 |
nodelist.append(("expr", self.expr)) |
| 347 |
return tuple(nodelist) |
| 348 |
|
| 349 |
def __iter__(self): |
| 350 |
if self.to_type is not None: |
| 351 |
yield self.to_type |
| 352 |
if self.expr is not None: |
| 353 |
yield self.expr |
| 354 |
|
| 355 |
attr_names = () |
| 356 |
|
| 357 |
class Compound(Node): |
| 358 |
__slots__ = ('block_items', 'coord', '__weakref__') |
| 359 |
def __init__(self, block_items, coord=None): |
| 360 |
self.block_items = block_items |
| 361 |
self.coord = coord |
| 362 |
|
| 363 |
def children(self): |
| 364 |
nodelist = [] |
| 365 |
for i, child in enumerate(self.block_items or []): |
| 366 |
nodelist.append((f"block_items[{i}]", child)) |
| 367 |
return tuple(nodelist) |
| 368 |
|
| 369 |
def __iter__(self): |
| 370 |
for child in (self.block_items or []): |
| 371 |
yield child |
| 372 |
|
| 373 |
attr_names = () |
| 374 |
|
| 375 |
class CompoundLiteral(Node): |
| 376 |
__slots__ = ('type', 'init', 'coord', '__weakref__') |
| 377 |
def __init__(self, type, init, coord=None): |
| 378 |
self.type = type |
| 379 |
self.init = init |
| 380 |
self.coord = coord |
| 381 |
|
| 382 |
def children(self): |
| 383 |
nodelist = [] |
| 384 |
if self.type is not None: |
| 385 |
nodelist.append(("type", self.type)) |
| 386 |
if self.init is not None: |
| 387 |
nodelist.append(("init", self.init)) |
| 388 |
return tuple(nodelist) |
| 389 |
|
| 390 |
def __iter__(self): |
| 391 |
if self.type is not None: |
| 392 |
yield self.type |
| 393 |
if self.init is not None: |
| 394 |
yield self.init |
| 395 |
|
| 396 |
attr_names = () |
| 397 |
|
| 398 |
class Constant(Node): |
| 399 |
__slots__ = ('type', 'value', 'coord', '__weakref__') |
| 400 |
def __init__(self, type, value, coord=None): |
| 401 |
self.type = type |
| 402 |
self.value = value |
| 403 |
self.coord = coord |
| 404 |
|
| 405 |
def children(self): |
| 406 |
nodelist = [] |
| 407 |
return tuple(nodelist) |
| 408 |
|
| 409 |
def __iter__(self): |
| 410 |
return |
| 411 |
yield |
| 412 |
|
| 413 |
attr_names = ('type', 'value', ) |
| 414 |
|
| 415 |
class Continue(Node): |
| 416 |
__slots__ = ('coord', '__weakref__') |
| 417 |
def __init__(self, coord=None): |
| 418 |
self.coord = coord |
| 419 |
|
| 420 |
def children(self): |
| 421 |
return () |
| 422 |
|
| 423 |
def __iter__(self): |
| 424 |
return |
| 425 |
yield |
| 426 |
|
| 427 |
attr_names = () |
| 428 |
|
| 429 |
class Decl(Node): |
| 430 |
__slots__ = ('name', 'quals', 'align', 'storage', 'funcspec', 'type', 'init', 'bitsize', 'coord', '__weakref__') |
| 431 |
def __init__(self, name, quals, align, storage, funcspec, type, init, bitsize, coord=None): |
| 432 |
self.name = name |
| 433 |
self.quals = quals |
| 434 |
self.align = align |
| 435 |
self.storage = storage |
| 436 |
self.funcspec = funcspec |
| 437 |
self.type = type |
| 438 |
self.init = init |
| 439 |
self.bitsize = bitsize |
| 440 |
self.coord = coord |
| 441 |
|
| 442 |
def children(self): |
| 443 |
nodelist = [] |
| 444 |
if self.type is not None: |
| 445 |
nodelist.append(("type", self.type)) |
| 446 |
if self.init is not None: |
| 447 |
nodelist.append(("init", self.init)) |
| 448 |
if self.bitsize is not None: |
| 449 |
nodelist.append(("bitsize", self.bitsize)) |
| 450 |
return tuple(nodelist) |
| 451 |
|
| 452 |
def __iter__(self): |
| 453 |
if self.type is not None: |
| 454 |
yield self.type |
| 455 |
if self.init is not None: |
| 456 |
yield self.init |
| 457 |
if self.bitsize is not None: |
| 458 |
yield self.bitsize |
| 459 |
|
| 460 |
attr_names = ('name', 'quals', 'align', 'storage', 'funcspec', ) |
| 461 |
|
| 462 |
class DeclList(Node): |
| 463 |
__slots__ = ('decls', 'coord', '__weakref__') |
| 464 |
def __init__(self, decls, coord=None): |
| 465 |
self.decls = decls |
| 466 |
self.coord = coord |
| 467 |
|
| 468 |
def children(self): |
| 469 |
nodelist = [] |
| 470 |
for i, child in enumerate(self.decls or []): |
| 471 |
nodelist.append((f"decls[{i}]", child)) |
| 472 |
return tuple(nodelist) |
| 473 |
|
| 474 |
def __iter__(self): |
| 475 |
for child in (self.decls or []): |
| 476 |
yield child |
| 477 |
|
| 478 |
attr_names = () |
| 479 |
|
| 480 |
class Default(Node): |
| 481 |
__slots__ = ('stmts', 'coord', '__weakref__') |
| 482 |
def __init__(self, stmts, coord=None): |
| 483 |
self.stmts = stmts |
| 484 |
self.coord = coord |
| 485 |
|
| 486 |
def children(self): |
| 487 |
nodelist = [] |
| 488 |
for i, child in enumerate(self.stmts or []): |
| 489 |
nodelist.append((f"stmts[{i}]", child)) |
| 490 |
return tuple(nodelist) |
| 491 |
|
| 492 |
def __iter__(self): |
| 493 |
for child in (self.stmts or []): |
| 494 |
yield child |
| 495 |
|
| 496 |
attr_names = () |
| 497 |
|
| 498 |
class DoWhile(Node): |
| 499 |
__slots__ = ('cond', 'stmt', 'coord', '__weakref__') |
| 500 |
def __init__(self, cond, stmt, coord=None): |
| 501 |
self.cond = cond |
| 502 |
self.stmt = stmt |
| 503 |
self.coord = coord |
| 504 |
|
| 505 |
def children(self): |
| 506 |
nodelist = [] |
| 507 |
if self.cond is not None: |
| 508 |
nodelist.append(("cond", self.cond)) |
| 509 |
if self.stmt is not None: |
| 510 |
nodelist.append(("stmt", self.stmt)) |
| 511 |
return tuple(nodelist) |
| 512 |
|
| 513 |
def __iter__(self): |
| 514 |
if self.cond is not None: |
| 515 |
yield self.cond |
| 516 |
if self.stmt is not None: |
| 517 |
yield self.stmt |
| 518 |
|
| 519 |
attr_names = () |
| 520 |
|
| 521 |
class EllipsisParam(Node): |
| 522 |
__slots__ = ('coord', '__weakref__') |
| 523 |
def __init__(self, coord=None): |
| 524 |
self.coord = coord |
| 525 |
|
| 526 |
def children(self): |
| 527 |
return () |
| 528 |
|
| 529 |
def __iter__(self): |
| 530 |
return |
| 531 |
yield |
| 532 |
|
| 533 |
attr_names = () |
| 534 |
|
| 535 |
class EmptyStatement(Node): |
| 536 |
__slots__ = ('coord', '__weakref__') |
| 537 |
def __init__(self, coord=None): |
| 538 |
self.coord = coord |
| 539 |
|
| 540 |
def children(self): |
| 541 |
return () |
| 542 |
|
| 543 |
def __iter__(self): |
| 544 |
return |
| 545 |
yield |
| 546 |
|
| 547 |
attr_names = () |
| 548 |
|
| 549 |
class Enum(Node): |
| 550 |
__slots__ = ('name', 'values', 'coord', '__weakref__') |
| 551 |
def __init__(self, name, values, coord=None): |
| 552 |
self.name = name |
| 553 |
self.values = values |
| 554 |
self.coord = coord |
| 555 |
|
| 556 |
def children(self): |
| 557 |
nodelist = [] |
| 558 |
if self.values is not None: |
| 559 |
nodelist.append(("values", self.values)) |
| 560 |
return tuple(nodelist) |
| 561 |
|
| 562 |
def __iter__(self): |
| 563 |
if self.values is not None: |
| 564 |
yield self.values |
| 565 |
|
| 566 |
attr_names = ('name', ) |
| 567 |
|
| 568 |
class Enumerator(Node): |
| 569 |
__slots__ = ('name', 'value', 'coord', '__weakref__') |
| 570 |
def __init__(self, name, value, coord=None): |
| 571 |
self.name = name |
| 572 |
self.value = value |
| 573 |
self.coord = coord |
| 574 |
|
| 575 |
def children(self): |
| 576 |
nodelist = [] |
| 577 |
if self.value is not None: |
| 578 |
nodelist.append(("value", self.value)) |
| 579 |
return tuple(nodelist) |
| 580 |
|
| 581 |
def __iter__(self): |
| 582 |
if self.value is not None: |
| 583 |
yield self.value |
| 584 |
|
| 585 |
attr_names = ('name', ) |
| 586 |
|
| 587 |
class EnumeratorList(Node): |
| 588 |
__slots__ = ('enumerators', 'coord', '__weakref__') |
| 589 |
def __init__(self, enumerators, coord=None): |
| 590 |
self.enumerators = enumerators |
| 591 |
self.coord = coord |
| 592 |
|
| 593 |
def children(self): |
| 594 |
nodelist = [] |
| 595 |
for i, child in enumerate(self.enumerators or []): |
| 596 |
nodelist.append((f"enumerators[{i}]", child)) |
| 597 |
return tuple(nodelist) |
| 598 |
|
| 599 |
def __iter__(self): |
| 600 |
for child in (self.enumerators or []): |
| 601 |
yield child |
| 602 |
|
| 603 |
attr_names = () |
| 604 |
|
| 605 |
class ExprList(Node): |
| 606 |
__slots__ = ('exprs', 'coord', '__weakref__') |
| 607 |
def __init__(self, exprs, coord=None): |
| 608 |
self.exprs = exprs |
| 609 |
self.coord = coord |
| 610 |
|
| 611 |
def children(self): |
| 612 |
nodelist = [] |
| 613 |
for i, child in enumerate(self.exprs or []): |
| 614 |
nodelist.append((f"exprs[{i}]", child)) |
| 615 |
return tuple(nodelist) |
| 616 |
|
| 617 |
def __iter__(self): |
| 618 |
for child in (self.exprs or []): |
| 619 |
yield child |
| 620 |
|
| 621 |
attr_names = () |
| 622 |
|
| 623 |
class FileAST(Node): |
| 624 |
__slots__ = ('ext', 'coord', '__weakref__') |
| 625 |
def __init__(self, ext, coord=None): |
| 626 |
self.ext = ext |
| 627 |
self.coord = coord |
| 628 |
|
| 629 |
def children(self): |
| 630 |
nodelist = [] |
| 631 |
for i, child in enumerate(self.ext or []): |
| 632 |
nodelist.append((f"ext[{i}]", child)) |
| 633 |
return tuple(nodelist) |
| 634 |
|
| 635 |
def __iter__(self): |
| 636 |
for child in (self.ext or []): |
| 637 |
yield child |
| 638 |
|
| 639 |
attr_names = () |
| 640 |
|
| 641 |
class For(Node): |
| 642 |
__slots__ = ('init', 'cond', 'next', 'stmt', 'coord', '__weakref__') |
| 643 |
def __init__(self, init, cond, next, stmt, coord=None): |
| 644 |
self.init = init |
| 645 |
self.cond = cond |
| 646 |
self.next = next |
| 647 |
self.stmt = stmt |
| 648 |
self.coord = coord |
| 649 |
|
| 650 |
def children(self): |
| 651 |
nodelist = [] |
| 652 |
if self.init is not None: |
| 653 |
nodelist.append(("init", self.init)) |
| 654 |
if self.cond is not None: |
| 655 |
nodelist.append(("cond", self.cond)) |
| 656 |
if self.next is not None: |
| 657 |
nodelist.append(("next", self.next)) |
| 658 |
if self.stmt is not None: |
| 659 |
nodelist.append(("stmt", self.stmt)) |
| 660 |
return tuple(nodelist) |
| 661 |
|
| 662 |
def __iter__(self): |
| 663 |
if self.init is not None: |
| 664 |
yield self.init |
| 665 |
if self.cond is not None: |
| 666 |
yield self.cond |
| 667 |
if self.next is not None: |
| 668 |
yield self.next |
| 669 |
if self.stmt is not None: |
| 670 |
yield self.stmt |
| 671 |
|
| 672 |
attr_names = () |
| 673 |
|
| 674 |
class FuncCall(Node): |
| 675 |
__slots__ = ('name', 'args', 'coord', '__weakref__') |
| 676 |
def __init__(self, name, args, coord=None): |
| 677 |
self.name = name |
| 678 |
self.args = args |
| 679 |
self.coord = coord |
| 680 |
|
| 681 |
def children(self): |
| 682 |
nodelist = [] |
| 683 |
if self.name is not None: |
| 684 |
nodelist.append(("name", self.name)) |
| 685 |
if self.args is not None: |
| 686 |
nodelist.append(("args", self.args)) |
| 687 |
return tuple(nodelist) |
| 688 |
|
| 689 |
def __iter__(self): |
| 690 |
if self.name is not None: |
| 691 |
yield self.name |
| 692 |
if self.args is not None: |
| 693 |
yield self.args |
| 694 |
|
| 695 |
attr_names = () |
| 696 |
|
| 697 |
class FuncDecl(Node): |
| 698 |
__slots__ = ('args', 'type', 'coord', '__weakref__') |
| 699 |
def __init__(self, args, type, coord=None): |
| 700 |
self.args = args |
| 701 |
self.type = type |
| 702 |
self.coord = coord |
| 703 |
|
| 704 |
def children(self): |
| 705 |
nodelist = [] |
| 706 |
if self.args is not None: |
| 707 |
nodelist.append(("args", self.args)) |
| 708 |
if self.type is not None: |
| 709 |
nodelist.append(("type", self.type)) |
| 710 |
return tuple(nodelist) |
| 711 |
|
| 712 |
def __iter__(self): |
| 713 |
if self.args is not None: |
| 714 |
yield self.args |
| 715 |
if self.type is not None: |
| 716 |
yield self.type |
| 717 |
|
| 718 |
attr_names = () |
| 719 |
|
| 720 |
class FuncDef(Node): |
| 721 |
__slots__ = ('decl', 'param_decls', 'body', 'coord', '__weakref__') |
| 722 |
def __init__(self, decl, param_decls, body, coord=None): |
| 723 |
self.decl = decl |
| 724 |
self.param_decls = param_decls |
| 725 |
self.body = body |
| 726 |
self.coord = coord |
| 727 |
|
| 728 |
def children(self): |
| 729 |
nodelist = [] |
| 730 |
if self.decl is not None: |
| 731 |
nodelist.append(("decl", self.decl)) |
| 732 |
if self.body is not None: |
| 733 |
nodelist.append(("body", self.body)) |
| 734 |
for i, child in enumerate(self.param_decls or []): |
| 735 |
nodelist.append((f"param_decls[{i}]", child)) |
| 736 |
return tuple(nodelist) |
| 737 |
|
| 738 |
def __iter__(self): |
| 739 |
if self.decl is not None: |
| 740 |
yield self.decl |
| 741 |
if self.body is not None: |
| 742 |
yield self.body |
| 743 |
for child in (self.param_decls or []): |
| 744 |
yield child |
| 745 |
|
| 746 |
attr_names = () |
| 747 |
|
| 748 |
class Goto(Node): |
| 749 |
__slots__ = ('name', 'coord', '__weakref__') |
| 750 |
def __init__(self, name, coord=None): |
| 751 |
self.name = name |
| 752 |
self.coord = coord |
| 753 |
|
| 754 |
def children(self): |
| 755 |
nodelist = [] |
| 756 |
return tuple(nodelist) |
| 757 |
|
| 758 |
def __iter__(self): |
| 759 |
return |
| 760 |
yield |
| 761 |
|
| 762 |
attr_names = ('name', ) |
| 763 |
|
| 764 |
class ID(Node): |
| 765 |
__slots__ = ('name', 'coord', '__weakref__') |
| 766 |
def __init__(self, name, coord=None): |
| 767 |
self.name = name |
| 768 |
self.coord = coord |
| 769 |
|
| 770 |
def children(self): |
| 771 |
nodelist = [] |
| 772 |
return tuple(nodelist) |
| 773 |
|
| 774 |
def __iter__(self): |
| 775 |
return |
| 776 |
yield |
| 777 |
|
| 778 |
attr_names = ('name', ) |
| 779 |
|
| 780 |
class IdentifierType(Node): |
| 781 |
__slots__ = ('names', 'coord', '__weakref__') |
| 782 |
def __init__(self, names, coord=None): |
| 783 |
self.names = names |
| 784 |
self.coord = coord |
| 785 |
|
| 786 |
def children(self): |
| 787 |
nodelist = [] |
| 788 |
return tuple(nodelist) |
| 789 |
|
| 790 |
def __iter__(self): |
| 791 |
return |
| 792 |
yield |
| 793 |
|
| 794 |
attr_names = ('names', ) |
| 795 |
|
| 796 |
class If(Node): |
| 797 |
__slots__ = ('cond', 'iftrue', 'iffalse', 'coord', '__weakref__') |
| 798 |
def __init__(self, cond, iftrue, iffalse, coord=None): |
| 799 |
self.cond = cond |
| 800 |
self.iftrue = iftrue |
| 801 |
self.iffalse = iffalse |
| 802 |
self.coord = coord |
| 803 |
|
| 804 |
def children(self): |
| 805 |
nodelist = [] |
| 806 |
if self.cond is not None: |
| 807 |
nodelist.append(("cond", self.cond)) |
| 808 |
if self.iftrue is not None: |
| 809 |
nodelist.append(("iftrue", self.iftrue)) |
| 810 |
if self.iffalse is not None: |
| 811 |
nodelist.append(("iffalse", self.iffalse)) |
| 812 |
return tuple(nodelist) |
| 813 |
|
| 814 |
def __iter__(self): |
| 815 |
if self.cond is not None: |
| 816 |
yield self.cond |
| 817 |
if self.iftrue is not None: |
| 818 |
yield self.iftrue |
| 819 |
if self.iffalse is not None: |
| 820 |
yield self.iffalse |
| 821 |
|
| 822 |
attr_names = () |
| 823 |
|
| 824 |
class InitList(Node): |
| 825 |
__slots__ = ('exprs', 'coord', '__weakref__') |
| 826 |
def __init__(self, exprs, coord=None): |
| 827 |
self.exprs = exprs |
| 828 |
self.coord = coord |
| 829 |
|
| 830 |
def children(self): |
| 831 |
nodelist = [] |
| 832 |
for i, child in enumerate(self.exprs or []): |
| 833 |
nodelist.append((f"exprs[{i}]", child)) |
| 834 |
return tuple(nodelist) |
| 835 |
|
| 836 |
def __iter__(self): |
| 837 |
for child in (self.exprs or []): |
| 838 |
yield child |
| 839 |
|
| 840 |
attr_names = () |
| 841 |
|
| 842 |
class Label(Node): |
| 843 |
__slots__ = ('name', 'stmt', 'coord', '__weakref__') |
| 844 |
def __init__(self, name, stmt, coord=None): |
| 845 |
self.name = name |
| 846 |
self.stmt = stmt |
| 847 |
self.coord = coord |
| 848 |
|
| 849 |
def children(self): |
| 850 |
nodelist = [] |
| 851 |
if self.stmt is not None: |
| 852 |
nodelist.append(("stmt", self.stmt)) |
| 853 |
return tuple(nodelist) |
| 854 |
|
| 855 |
def __iter__(self): |
| 856 |
if self.stmt is not None: |
| 857 |
yield self.stmt |
| 858 |
|
| 859 |
attr_names = ('name', ) |
| 860 |
|
| 861 |
class NamedInitializer(Node): |
| 862 |
__slots__ = ('name', 'expr', 'coord', '__weakref__') |
| 863 |
def __init__(self, name, expr, coord=None): |
| 864 |
self.name = name |
| 865 |
self.expr = expr |
| 866 |
self.coord = coord |
| 867 |
|
| 868 |
def children(self): |
| 869 |
nodelist = [] |
| 870 |
if self.expr is not None: |
| 871 |
nodelist.append(("expr", self.expr)) |
| 872 |
for i, child in enumerate(self.name or []): |
| 873 |
nodelist.append((f"name[{i}]", child)) |
| 874 |
return tuple(nodelist) |
| 875 |
|
| 876 |
def __iter__(self): |
| 877 |
if self.expr is not None: |
| 878 |
yield self.expr |
| 879 |
for child in (self.name or []): |
| 880 |
yield child |
| 881 |
|
| 882 |
attr_names = () |
| 883 |
|
| 884 |
class ParamList(Node): |
| 885 |
__slots__ = ('params', 'coord', '__weakref__') |
| 886 |
def __init__(self, params, coord=None): |
| 887 |
self.params = params |
| 888 |
self.coord = coord |
| 889 |
|
| 890 |
def children(self): |
| 891 |
nodelist = [] |
| 892 |
for i, child in enumerate(self.params or []): |
| 893 |
nodelist.append((f"params[{i}]", child)) |
| 894 |
return tuple(nodelist) |
| 895 |
|
| 896 |
def __iter__(self): |
| 897 |
for child in (self.params or []): |
| 898 |
yield child |
| 899 |
|
| 900 |
attr_names = () |
| 901 |
|
| 902 |
class PtrDecl(Node): |
| 903 |
__slots__ = ('quals', 'type', 'coord', '__weakref__') |
| 904 |
def __init__(self, quals, type, coord=None): |
| 905 |
self.quals = quals |
| 906 |
self.type = type |
| 907 |
self.coord = coord |
| 908 |
|
| 909 |
def children(self): |
| 910 |
nodelist = [] |
| 911 |
if self.type is not None: |
| 912 |
nodelist.append(("type", self.type)) |
| 913 |
return tuple(nodelist) |
| 914 |
|
| 915 |
def __iter__(self): |
| 916 |
if self.type is not None: |
| 917 |
yield self.type |
| 918 |
|
| 919 |
attr_names = ('quals', ) |
| 920 |
|
| 921 |
class Return(Node): |
| 922 |
__slots__ = ('expr', 'coord', '__weakref__') |
| 923 |
def __init__(self, expr, coord=None): |
| 924 |
self.expr = expr |
| 925 |
self.coord = coord |
| 926 |
|
| 927 |
def children(self): |
| 928 |
nodelist = [] |
| 929 |
if self.expr is not None: |
| 930 |
nodelist.append(("expr", self.expr)) |
| 931 |
return tuple(nodelist) |
| 932 |
|
| 933 |
def __iter__(self): |
| 934 |
if self.expr is not None: |
| 935 |
yield self.expr |
| 936 |
|
| 937 |
attr_names = () |
| 938 |
|
| 939 |
class StaticAssert(Node): |
| 940 |
__slots__ = ('cond', 'message', 'coord', '__weakref__') |
| 941 |
def __init__(self, cond, message, coord=None): |
| 942 |
self.cond = cond |
| 943 |
self.message = message |
| 944 |
self.coord = coord |
| 945 |
|
| 946 |
def children(self): |
| 947 |
nodelist = [] |
| 948 |
if self.cond is not None: |
| 949 |
nodelist.append(("cond", self.cond)) |
| 950 |
if self.message is not None: |
| 951 |
nodelist.append(("message", self.message)) |
| 952 |
return tuple(nodelist) |
| 953 |
|
| 954 |
def __iter__(self): |
| 955 |
if self.cond is not None: |
| 956 |
yield self.cond |
| 957 |
if self.message is not None: |
| 958 |
yield self.message |
| 959 |
|
| 960 |
attr_names = () |
| 961 |
|
| 962 |
class Struct(Node): |
| 963 |
__slots__ = ('name', 'decls', 'coord', '__weakref__') |
| 964 |
def __init__(self, name, decls, coord=None): |
| 965 |
self.name = name |
| 966 |
self.decls = decls |
| 967 |
self.coord = coord |
| 968 |
|
| 969 |
def children(self): |
| 970 |
nodelist = [] |
| 971 |
for i, child in enumerate(self.decls or []): |
| 972 |
nodelist.append((f"decls[{i}]", child)) |
| 973 |
return tuple(nodelist) |
| 974 |
|
| 975 |
def __iter__(self): |
| 976 |
for child in (self.decls or []): |
| 977 |
yield child |
| 978 |
|
| 979 |
attr_names = ('name', ) |
| 980 |
|
| 981 |
class StructRef(Node): |
| 982 |
__slots__ = ('name', 'type', 'field', 'coord', '__weakref__') |
| 983 |
def __init__(self, name, type, field, coord=None): |
| 984 |
self.name = name |
| 985 |
self.type = type |
| 986 |
self.field = field |
| 987 |
self.coord = coord |
| 988 |
|
| 989 |
def children(self): |
| 990 |
nodelist = [] |
| 991 |
if self.name is not None: |
| 992 |
nodelist.append(("name", self.name)) |
| 993 |
if self.field is not None: |
| 994 |
nodelist.append(("field", self.field)) |
| 995 |
return tuple(nodelist) |
| 996 |
|
| 997 |
def __iter__(self): |
| 998 |
if self.name is not None: |
| 999 |
yield self.name |
| 1000 |
if self.field is not None: |
| 1001 |
yield self.field |
| 1002 |
|
| 1003 |
attr_names = ('type', ) |
| 1004 |
|
| 1005 |
class Switch(Node): |
| 1006 |
__slots__ = ('cond', 'stmt', 'coord', '__weakref__') |
| 1007 |
def __init__(self, cond, stmt, coord=None): |
| 1008 |
self.cond = cond |
| 1009 |
self.stmt = stmt |
| 1010 |
self.coord = coord |
| 1011 |
|
| 1012 |
def children(self): |
| 1013 |
nodelist = [] |
| 1014 |
if self.cond is not None: |
| 1015 |
nodelist.append(("cond", self.cond)) |
| 1016 |
if self.stmt is not None: |
| 1017 |
nodelist.append(("stmt", self.stmt)) |
| 1018 |
return tuple(nodelist) |
| 1019 |
|
| 1020 |
def __iter__(self): |
| 1021 |
if self.cond is not None: |
| 1022 |
yield self.cond |
| 1023 |
if self.stmt is not None: |
| 1024 |
yield self.stmt |
| 1025 |
|
| 1026 |
attr_names = () |
| 1027 |
|
| 1028 |
class TernaryOp(Node): |
| 1029 |
__slots__ = ('cond', 'iftrue', 'iffalse', 'coord', '__weakref__') |
| 1030 |
def __init__(self, cond, iftrue, iffalse, coord=None): |
| 1031 |
self.cond = cond |
| 1032 |
self.iftrue = iftrue |
| 1033 |
self.iffalse = iffalse |
| 1034 |
self.coord = coord |
| 1035 |
|
| 1036 |
def children(self): |
| 1037 |
nodelist = [] |
| 1038 |
if self.cond is not None: |
| 1039 |
nodelist.append(("cond", self.cond)) |
| 1040 |
if self.iftrue is not None: |
| 1041 |
nodelist.append(("iftrue", self.iftrue)) |
| 1042 |
if self.iffalse is not None: |
| 1043 |
nodelist.append(("iffalse", self.iffalse)) |
| 1044 |
return tuple(nodelist) |
| 1045 |
|
| 1046 |
def __iter__(self): |
| 1047 |
if self.cond is not None: |
| 1048 |
yield self.cond |
| 1049 |
if self.iftrue is not None: |
| 1050 |
yield self.iftrue |
| 1051 |
if self.iffalse is not None: |
| 1052 |
yield self.iffalse |
| 1053 |
|
| 1054 |
attr_names = () |
| 1055 |
|
| 1056 |
class TypeDecl(Node): |
| 1057 |
__slots__ = ('declname', 'quals', 'align', 'type', 'coord', '__weakref__') |
| 1058 |
def __init__(self, declname, quals, align, type, coord=None): |
| 1059 |
self.declname = declname |
| 1060 |
self.quals = quals |
| 1061 |
self.align = align |
| 1062 |
self.type = type |
| 1063 |
self.coord = coord |
| 1064 |
|
| 1065 |
def children(self): |
| 1066 |
nodelist = [] |
| 1067 |
if self.type is not None: |
| 1068 |
nodelist.append(("type", self.type)) |
| 1069 |
return tuple(nodelist) |
| 1070 |
|
| 1071 |
def __iter__(self): |
| 1072 |
if self.type is not None: |
| 1073 |
yield self.type |
| 1074 |
|
| 1075 |
attr_names = ('declname', 'quals', 'align', ) |
| 1076 |
|
| 1077 |
class Typedef(Node): |
| 1078 |
__slots__ = ('name', 'quals', 'storage', 'type', 'coord', '__weakref__') |
| 1079 |
def __init__(self, name, quals, storage, type, coord=None): |
| 1080 |
self.name = name |
| 1081 |
self.quals = quals |
| 1082 |
self.storage = storage |
| 1083 |
self.type = type |
| 1084 |
self.coord = coord |
| 1085 |
|
| 1086 |
def children(self): |
| 1087 |
nodelist = [] |
| 1088 |
if self.type is not None: |
| 1089 |
nodelist.append(("type", self.type)) |
| 1090 |
return tuple(nodelist) |
| 1091 |
|
| 1092 |
def __iter__(self): |
| 1093 |
if self.type is not None: |
| 1094 |
yield self.type |
| 1095 |
|
| 1096 |
attr_names = ('name', 'quals', 'storage', ) |
| 1097 |
|
| 1098 |
class Typename(Node): |
| 1099 |
__slots__ = ('name', 'quals', 'align', 'type', 'coord', '__weakref__') |
| 1100 |
def __init__(self, name, quals, align, type, coord=None): |
| 1101 |
self.name = name |
| 1102 |
self.quals = quals |
| 1103 |
self.align = align |
| 1104 |
self.type = type |
| 1105 |
self.coord = coord |
| 1106 |
|
| 1107 |
def children(self): |
| 1108 |
nodelist = [] |
| 1109 |
if self.type is not None: |
| 1110 |
nodelist.append(("type", self.type)) |
| 1111 |
return tuple(nodelist) |
| 1112 |
|
| 1113 |
def __iter__(self): |
| 1114 |
if self.type is not None: |
| 1115 |
yield self.type |
| 1116 |
|
| 1117 |
attr_names = ('name', 'quals', 'align', ) |
| 1118 |
|
| 1119 |
class UnaryOp(Node): |
| 1120 |
__slots__ = ('op', 'expr', 'coord', '__weakref__') |
| 1121 |
def __init__(self, op, expr, coord=None): |
| 1122 |
self.op = op |
| 1123 |
self.expr = expr |
| 1124 |
self.coord = coord |
| 1125 |
|
| 1126 |
def children(self): |
| 1127 |
nodelist = [] |
| 1128 |
if self.expr is not None: |
| 1129 |
nodelist.append(("expr", self.expr)) |
| 1130 |
return tuple(nodelist) |
| 1131 |
|
| 1132 |
def __iter__(self): |
| 1133 |
if self.expr is not None: |
| 1134 |
yield self.expr |
| 1135 |
|
| 1136 |
attr_names = ('op', ) |
| 1137 |
|
| 1138 |
class Union(Node): |
| 1139 |
__slots__ = ('name', 'decls', 'coord', '__weakref__') |
| 1140 |
def __init__(self, name, decls, coord=None): |
| 1141 |
self.name = name |
| 1142 |
self.decls = decls |
| 1143 |
self.coord = coord |
| 1144 |
|
| 1145 |
def children(self): |
| 1146 |
nodelist = [] |
| 1147 |
for i, child in enumerate(self.decls or []): |
| 1148 |
nodelist.append((f"decls[{i}]", child)) |
| 1149 |
return tuple(nodelist) |
| 1150 |
|
| 1151 |
def __iter__(self): |
| 1152 |
for child in (self.decls or []): |
| 1153 |
yield child |
| 1154 |
|
| 1155 |
attr_names = ('name', ) |
| 1156 |
|
| 1157 |
class While(Node): |
| 1158 |
__slots__ = ('cond', 'stmt', 'coord', '__weakref__') |
| 1159 |
def __init__(self, cond, stmt, coord=None): |
| 1160 |
self.cond = cond |
| 1161 |
self.stmt = stmt |
| 1162 |
self.coord = coord |
| 1163 |
|
| 1164 |
def children(self): |
| 1165 |
nodelist = [] |
| 1166 |
if self.cond is not None: |
| 1167 |
nodelist.append(("cond", self.cond)) |
| 1168 |
if self.stmt is not None: |
| 1169 |
nodelist.append(("stmt", self.stmt)) |
| 1170 |
return tuple(nodelist) |
| 1171 |
|
| 1172 |
def __iter__(self): |
| 1173 |
if self.cond is not None: |
| 1174 |
yield self.cond |
| 1175 |
if self.stmt is not None: |
| 1176 |
yield self.stmt |
| 1177 |
|
| 1178 |
attr_names = () |
| 1179 |
|
| 1180 |
class Pragma(Node): |
| 1181 |
__slots__ = ('string', 'coord', '__weakref__') |
| 1182 |
def __init__(self, string, coord=None): |
| 1183 |
self.string = string |
| 1184 |
self.coord = coord |
| 1185 |
|
| 1186 |
def children(self): |
| 1187 |
nodelist = [] |
| 1188 |
return tuple(nodelist) |
| 1189 |
|
| 1190 |
def __iter__(self): |
| 1191 |
return |
| 1192 |
yield |
| 1193 |
|
| 1194 |
attr_names = ('string', ) |
| 1195 |
|