paul@638 | 1 | #!/usr/bin/env python |
paul@638 | 2 | |
paul@638 | 3 | """ |
paul@638 | 4 | Produce syspython code from an inspected program. |
paul@638 | 5 | |
paul@638 | 6 | Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@638 | 7 | |
paul@638 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@638 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@638 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@638 | 11 | version. |
paul@638 | 12 | |
paul@638 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@638 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@638 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@638 | 16 | details. |
paul@638 | 17 | |
paul@638 | 18 | You should have received a copy of the GNU General Public License along with |
paul@638 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@638 | 20 | """ |
paul@638 | 21 | |
paul@638 | 22 | from micropython.common import * |
paul@638 | 23 | from micropython.data import * |
paul@638 | 24 | from micropython.errors import * |
paul@638 | 25 | from os.path import exists, extsep, join |
paul@638 | 26 | import compiler.ast |
paul@638 | 27 | import sys |
paul@638 | 28 | import os |
paul@638 | 29 | |
paul@638 | 30 | try: |
paul@638 | 31 | set |
paul@638 | 32 | except NameError: |
paul@638 | 33 | from sets import Set as set |
paul@638 | 34 | |
paul@638 | 35 | # Convenience definitions. |
paul@638 | 36 | |
paul@638 | 37 | module_attribute = compiler.ast.Getattr |
paul@638 | 38 | special_name = compiler.ast.Name |
paul@638 | 39 | quoted_name = compiler.ast.Const |
paul@638 | 40 | |
paul@638 | 41 | # Source code classes. |
paul@638 | 42 | |
paul@638 | 43 | class ConvertedSource(ASTVisitor): |
paul@638 | 44 | |
paul@638 | 45 | "A conversion of module source code to syspython." |
paul@638 | 46 | |
paul@638 | 47 | def __init__(self, module, program): |
paul@638 | 48 | self.visitor = self |
paul@638 | 49 | self.module = module |
paul@638 | 50 | self.program = program |
paul@638 | 51 | self.in_main = False |
paul@638 | 52 | self.units = [] |
paul@638 | 53 | |
paul@638 | 54 | def get_unit(self): |
paul@638 | 55 | return self.units[-1] |
paul@638 | 56 | |
paul@638 | 57 | def get_module(self): |
paul@638 | 58 | return self.units[0] |
paul@638 | 59 | |
paul@638 | 60 | def to_stream(self, stream): |
paul@638 | 61 | |
paul@638 | 62 | "Write the converted code to the given 'stream'." |
paul@638 | 63 | |
paul@638 | 64 | module = self.dispatch(self.module.astnode) |
paul@638 | 65 | stream.write(str(module)) |
paul@638 | 66 | |
paul@638 | 67 | def NOP(self, node): |
paul@638 | 68 | return node |
paul@638 | 69 | |
paul@638 | 70 | def visitModule(self, node): |
paul@638 | 71 | module = node.unit |
paul@638 | 72 | self.units.append(module) |
paul@638 | 73 | |
paul@638 | 74 | definitions = self._findDefinitions(node) |
paul@638 | 75 | |
paul@638 | 76 | # __globalnames__(name, ...) |
paul@638 | 77 | |
paul@638 | 78 | globalnames = module.module_attribute_names() and [ |
paul@638 | 79 | compiler.ast.CallFunc( |
paul@638 | 80 | special_name("__globalnames__"), |
paul@638 | 81 | [special_name(name) for name in module.module_attribute_names()] |
paul@638 | 82 | ) |
paul@638 | 83 | ] or [] |
paul@638 | 84 | |
paul@638 | 85 | # def __main__(): |
paul@638 | 86 | # ... |
paul@638 | 87 | |
paul@638 | 88 | self.in_main = True |
paul@638 | 89 | |
paul@638 | 90 | main = compiler.ast.Function( |
paul@638 | 91 | [], "__main__", [], [], 0, "Module initialisation.", |
paul@638 | 92 | compiler.ast.Stmt(globalnames + self.dispatch(node.node).nodes) |
paul@638 | 93 | ) |
paul@638 | 94 | |
paul@638 | 95 | self.in_main = False |
paul@638 | 96 | self.units.pop() |
paul@638 | 97 | |
paul@638 | 98 | return compiler.ast.Module(node.doc, compiler.ast.Stmt(definitions + [main])) |
paul@638 | 99 | |
paul@638 | 100 | def _findDefinitions(self, node): |
paul@638 | 101 | definitions = [] |
paul@638 | 102 | for n in node.getChildNodes(): |
paul@638 | 103 | if isinstance(n, (compiler.ast.Class, compiler.ast.Function)): |
paul@638 | 104 | definitions.append(self.dispatch(n)) |
paul@638 | 105 | else: |
paul@638 | 106 | definitions += self._findDefinitions(n) |
paul@638 | 107 | return definitions |
paul@638 | 108 | |
paul@638 | 109 | # Statements. |
paul@638 | 110 | |
paul@638 | 111 | def visitAssert(self, node): |
paul@638 | 112 | return compiler.ast.Assert(self.dispatch(node.test), node.fail and self.dispatch(node.fail)) |
paul@638 | 113 | |
paul@638 | 114 | def visitAssign(self, node): |
paul@638 | 115 | expr = self.dispatch(node.expr) |
paul@638 | 116 | return compiler.ast.Stmt([self.dispatch(n, expr) for n in node.nodes]) |
paul@638 | 117 | |
paul@638 | 118 | def visitAugAssign(self, node): |
paul@638 | 119 | |
paul@638 | 120 | # lvalue = op(lvalue, expr) |
paul@638 | 121 | # -> __fn__(lvalue, op(lvalue, expr)) |
paul@638 | 122 | |
paul@638 | 123 | op_name = operator_functions[node.op] |
paul@638 | 124 | |
paul@638 | 125 | return self.dispatch(node.node, compiler.ast.CallFunc( |
paul@638 | 126 | module_attribute("operator", op_name), |
paul@638 | 127 | [self.dispatch(node.node), self.dispatch(node.expr)] |
paul@638 | 128 | )) |
paul@638 | 129 | |
paul@638 | 130 | visitBreak = NOP |
paul@638 | 131 | |
paul@638 | 132 | def visitClass(self, node): |
paul@638 | 133 | if not used_by_unit(node): |
paul@638 | 134 | return compiler.ast.Stmt([]) |
paul@638 | 135 | |
paul@638 | 136 | self.units.append(node.unit) |
paul@638 | 137 | try: |
paul@638 | 138 | # Incorporate class body code in the main function. |
paul@638 | 139 | |
paul@638 | 140 | if self.in_main: |
paul@638 | 141 | return self.dispatch(node.code) |
paul@638 | 142 | else: |
paul@638 | 143 | return self._visitClassDefinition(node) |
paul@638 | 144 | |
paul@638 | 145 | finally: |
paul@638 | 146 | self.units.pop() |
paul@638 | 147 | |
paul@638 | 148 | def _visitClassDefinition(self, node): |
paul@638 | 149 | cls = node.unit |
paul@638 | 150 | |
paul@638 | 151 | # __instattrs__(name, ...) |
paul@638 | 152 | # __clsattrs__(name, ...) |
paul@638 | 153 | |
paul@638 | 154 | instattrs = cls.instance_attribute_names() and [ |
paul@638 | 155 | compiler.ast.CallFunc( |
paul@638 | 156 | special_name("__instattrs__"), |
paul@638 | 157 | [special_name(name) for name in cls.instance_attribute_names()] |
paul@638 | 158 | ) |
paul@638 | 159 | ] or [] |
paul@638 | 160 | |
paul@638 | 161 | clsattrs = cls.class_attribute_names() and [ |
paul@638 | 162 | compiler.ast.CallFunc( |
paul@638 | 163 | special_name("__clsattrs__"), |
paul@638 | 164 | [special_name(name) for name in cls.class_attribute_names()] |
paul@638 | 165 | ) |
paul@638 | 166 | ] or [] |
paul@638 | 167 | |
paul@638 | 168 | # __inherited__(superclass, name, ...) |
paul@638 | 169 | # ... |
paul@638 | 170 | |
paul@638 | 171 | attrs_by_cls = {} |
paul@638 | 172 | for attrname, attr in cls.all_class_attributes().items(): |
paul@638 | 173 | supercls = attr.parent |
paul@638 | 174 | if supercls is cls: |
paul@638 | 175 | continue |
paul@638 | 176 | if not attrs_by_cls.has_key(supercls): |
paul@638 | 177 | attrs_by_cls[supercls] = [] |
paul@638 | 178 | attrs_by_cls[supercls].append(attrname) |
paul@638 | 179 | |
paul@638 | 180 | inherited = [] |
paul@638 | 181 | |
paul@638 | 182 | for supercls, attrnames in attrs_by_cls.items(): |
paul@638 | 183 | inherited.append( |
paul@638 | 184 | compiler.ast.CallFunc( |
paul@638 | 185 | special_name("__inherited__"), |
paul@638 | 186 | [special_name(supercls.full_name())] + [special_name(name) for name in attrnames] |
paul@638 | 187 | )) |
paul@638 | 188 | |
paul@638 | 189 | # __descendants__(name, ...) |
paul@638 | 190 | |
paul@638 | 191 | descendants = cls.all_descendants() and [ |
paul@638 | 192 | compiler.ast.CallFunc( |
paul@638 | 193 | special_name("__descendants__"), |
paul@638 | 194 | [special_name(name) for name in cls.all_descendants().keys()] |
paul@638 | 195 | ) |
paul@638 | 196 | ] or [] |
paul@638 | 197 | |
paul@638 | 198 | # Process all the definitions defined inside the class. |
paul@638 | 199 | |
paul@638 | 200 | definitions = self._findDefinitions(node) |
paul@638 | 201 | |
paul@638 | 202 | return compiler.ast.Class(node.name, [], node.doc, |
paul@638 | 203 | compiler.ast.Stmt(instattrs + clsattrs + inherited + descendants + definitions) |
paul@638 | 204 | ) |
paul@638 | 205 | |
paul@638 | 206 | visitContinue = NOP |
paul@638 | 207 | |
paul@638 | 208 | def visitDiscard(self, node): |
paul@638 | 209 | return compiler.ast.Discard(self.dispatch(node.expr)) |
paul@638 | 210 | |
paul@638 | 211 | def visitFor(self, node): |
paul@638 | 212 | |
paul@638 | 213 | """ |
paul@638 | 214 | Convert from... |
paul@638 | 215 | |
paul@638 | 216 | for <assign> in <list>: |
paul@638 | 217 | <body> |
paul@638 | 218 | [ else: |
paul@638 | 219 | <else_> ] |
paul@638 | 220 | |
paul@638 | 221 | ...to... |
paul@638 | 222 | |
paul@638 | 223 | _it = iter(<list>) |
paul@638 | 224 | while True: |
paul@638 | 225 | try: |
paul@638 | 226 | <assign> = _it.next() |
paul@638 | 227 | except StopIteration: |
paul@638 | 228 | [ <else_> ] |
paul@638 | 229 | break |
paul@638 | 230 | else: |
paul@638 | 231 | <body> |
paul@638 | 232 | """ |
paul@638 | 233 | |
paul@638 | 234 | unit = self.get_unit() |
paul@638 | 235 | temp = quoted_name(unit.temp_usage) |
paul@638 | 236 | unit.temp_usage += 1 |
paul@638 | 237 | |
paul@638 | 238 | else_nodes = node.else_ and self.dispatch(node.else_).nodes or [] |
paul@638 | 239 | |
paul@638 | 240 | return compiler.ast.Stmt([ |
paul@638 | 241 | # __storetemp__(_it, __loadaddress__(__builtins__, iter)(<list>)) |
paul@638 | 242 | compiler.ast.CallFunc(special_name("__storetemp__"), [ |
paul@638 | 243 | temp, |
paul@638 | 244 | compiler.ast.CallFunc( |
paul@638 | 245 | compiler.ast.CallFunc(special_name("__loadaddress__"), |
paul@638 | 246 | [special_name("__builtins__"), special_name("iter")] |
paul@638 | 247 | ), |
paul@638 | 248 | [self.dispatch(node.list)] |
paul@638 | 249 | ) |
paul@638 | 250 | ]), |
paul@638 | 251 | # while True: ... |
paul@638 | 252 | compiler.ast.While( |
paul@638 | 253 | special_name("True"), |
paul@638 | 254 | # try: ... |
paul@638 | 255 | compiler.ast.TryExcept( |
paul@638 | 256 | compiler.ast.Stmt([ |
paul@638 | 257 | # <assign> = ... |
paul@638 | 258 | self.dispatch(node.assign, |
paul@638 | 259 | # _it.next() |
paul@638 | 260 | compiler.ast.CallFunc( |
paul@638 | 261 | compiler.ast.CallFunc(special_name("__loadattr__"), [ |
paul@638 | 262 | compiler.ast.CallFunc(special_name("__loadtemp__"), [temp]), |
paul@638 | 263 | special_name("next") |
paul@638 | 264 | ]), |
paul@638 | 265 | [] |
paul@638 | 266 | ) |
paul@638 | 267 | ) |
paul@638 | 268 | ]), |
paul@638 | 269 | # except StopIteration: ... |
paul@638 | 270 | [(special_name("StopIteration"), None, compiler.ast.Stmt(else_nodes + [compiler.ast.Break()]))], |
paul@638 | 271 | # else: ... |
paul@638 | 272 | self.dispatch(node.body) |
paul@638 | 273 | ), |
paul@638 | 274 | None |
paul@638 | 275 | ) |
paul@638 | 276 | ]) |
paul@638 | 277 | |
paul@638 | 278 | def visitFrom(self, node): |
paul@638 | 279 | |
paul@638 | 280 | # Generate __main__ function calls for each step in the imported module |
paul@638 | 281 | # hierarchy. |
paul@638 | 282 | |
paul@638 | 283 | statements = [] |
paul@638 | 284 | |
paul@638 | 285 | for modname in self.module.get_module_paths(node.modname): |
paul@638 | 286 | statements.append( |
paul@638 | 287 | compiler.ast.CallFunc(special_name("%s.__main__" % modname ), []) |
paul@638 | 288 | ) |
paul@638 | 289 | |
paul@638 | 290 | for name, alias in node.names: |
paul@638 | 291 | statements.append( |
paul@638 | 292 | compiler.ast.Assign( |
paul@638 | 293 | [special_name(alias or name)], |
paul@638 | 294 | compiler.ast.CallFunc( |
paul@638 | 295 | special_name("__loadattribute__"), |
paul@638 | 296 | [special_name(node.modname), special_name(name)] |
paul@638 | 297 | ) |
paul@638 | 298 | ) |
paul@638 | 299 | ) |
paul@638 | 300 | |
paul@638 | 301 | return compiler.ast.Stmt(statements) |
paul@638 | 302 | |
paul@638 | 303 | def visitFunction(self, node): |
paul@638 | 304 | if not used_by_unit(node): |
paul@638 | 305 | return compiler.ast.Stmt([]) |
paul@638 | 306 | |
paul@638 | 307 | self.units.append(node.unit) |
paul@638 | 308 | |
paul@638 | 309 | try: |
paul@638 | 310 | # Ignore functions when generating the main function. |
paul@638 | 311 | |
paul@638 | 312 | if self.in_main: |
paul@638 | 313 | return compiler.ast.Stmt([]) |
paul@638 | 314 | else: |
paul@638 | 315 | return self._visitFunctionDefinition(node) |
paul@638 | 316 | finally: |
paul@638 | 317 | self.units.pop() |
paul@638 | 318 | |
paul@638 | 319 | def _visitFunctionDefinition(self, node): |
paul@638 | 320 | fn = node.unit |
paul@638 | 321 | |
paul@638 | 322 | # __localnames__(name, ...) |
paul@638 | 323 | # __globalnames__(name, ...) |
paul@638 | 324 | |
paul@638 | 325 | localnames = fn.locals() and [ |
paul@638 | 326 | compiler.ast.CallFunc( |
paul@638 | 327 | special_name("__localnames__"), |
paul@638 | 328 | [special_name(name) for name in fn.locals().keys()] |
paul@638 | 329 | ) |
paul@638 | 330 | ] or [] |
paul@638 | 331 | |
paul@638 | 332 | globalnames = fn.globals and [ |
paul@638 | 333 | compiler.ast.CallFunc( |
paul@638 | 334 | special_name("__globalnames__"), |
paul@638 | 335 | [special_name(name) for name in fn.globals] |
paul@638 | 336 | ) |
paul@638 | 337 | ] or [] |
paul@638 | 338 | |
paul@638 | 339 | defaults = [self.dispatch(n) for n in node.defaults] |
paul@638 | 340 | |
paul@638 | 341 | code = self.dispatch(node.code) |
paul@638 | 342 | |
paul@638 | 343 | return compiler.ast.Function(node.decorators, node.name, node.argnames, defaults, node.flags, node.doc, |
paul@638 | 344 | compiler.ast.Stmt(localnames + globalnames + code.nodes)) |
paul@638 | 345 | |
paul@638 | 346 | visitGlobal = NOP |
paul@638 | 347 | |
paul@638 | 348 | def visitIf(self, node): |
paul@638 | 349 | return compiler.ast.If( |
paul@638 | 350 | [(self.dispatch(compare), self.dispatch(stmt)) for (compare, stmt) in node.tests], |
paul@638 | 351 | node.else_ and self.dispatch(node.else_) |
paul@638 | 352 | ) |
paul@638 | 353 | |
paul@638 | 354 | def visitImport(self, node): |
paul@638 | 355 | |
paul@638 | 356 | # Generate __main__ function calls for each step in the imported module |
paul@638 | 357 | # hierarchy. |
paul@638 | 358 | |
paul@638 | 359 | statements = [] |
paul@638 | 360 | |
paul@638 | 361 | for name, alias in node.names: |
paul@638 | 362 | for modname in self.module.get_module_paths(name): |
paul@638 | 363 | statements.append( |
paul@638 | 364 | compiler.ast.CallFunc(compiler.ast.Getattr(modname, "__main__"), []) |
paul@638 | 365 | ) |
paul@638 | 366 | |
paul@638 | 367 | statements.append( |
paul@638 | 368 | compiler.ast.Assign( |
paul@638 | 369 | [special_name(alias or name.split(".")[0])], |
paul@638 | 370 | compiler.ast.CallFunc( |
paul@638 | 371 | special_name("__static__"), |
paul@638 | 372 | [special_name(name)] |
paul@638 | 373 | ) |
paul@638 | 374 | ) |
paul@638 | 375 | ) |
paul@638 | 376 | |
paul@638 | 377 | return compiler.ast.Stmt(statements) |
paul@638 | 378 | |
paul@638 | 379 | visitPass = NOP |
paul@638 | 380 | |
paul@638 | 381 | def visitPrint(self, node): |
paul@638 | 382 | return compiler.ast.Print( |
paul@638 | 383 | [self.dispatch(n) for n in node.nodes], |
paul@638 | 384 | node.dest and self.dispatch(node.dest) |
paul@638 | 385 | ) |
paul@638 | 386 | |
paul@638 | 387 | def visitPrintnl(self, node): |
paul@638 | 388 | return compiler.ast.Print( |
paul@638 | 389 | [self.dispatch(n) for n in node.nodes], |
paul@638 | 390 | node.dest and self.dispatch(node.dest) |
paul@638 | 391 | ) |
paul@638 | 392 | |
paul@638 | 393 | def visitRaise(self, node): |
paul@638 | 394 | return compiler.ast.Raise( |
paul@638 | 395 | node.expr1 and self.dispatch(node.expr1), |
paul@638 | 396 | node.expr2 and self.dispatch(node.expr2), |
paul@638 | 397 | node.expr3 and self.dispatch(node.expr3) |
paul@638 | 398 | ) |
paul@638 | 399 | |
paul@638 | 400 | def visitReturn(self, node): |
paul@638 | 401 | return compiler.ast.Return(self.dispatch(node.value)) |
paul@638 | 402 | |
paul@638 | 403 | def visitStmt(self, node): |
paul@638 | 404 | return compiler.ast.Stmt([self.dispatch(n) for n in node.nodes]) |
paul@638 | 405 | |
paul@638 | 406 | def visitTryExcept(self, node): |
paul@638 | 407 | # NOTE: Need to dispatch to the assignment with the exception. |
paul@638 | 408 | return compiler.ast.TryExcept( |
paul@638 | 409 | self.dispatch(node.body), |
paul@638 | 410 | [(spec and self.dispatch(spec), assign and self.dispatch(assign), self.dispatch(statement)) |
paul@638 | 411 | for spec, assign, statement in node.handlers], |
paul@638 | 412 | node.else_ and self.dispatch(node.else_) |
paul@638 | 413 | ) |
paul@638 | 414 | |
paul@638 | 415 | def visitTryFinally(self, node): |
paul@638 | 416 | return compiler.ast.TryFinally( |
paul@638 | 417 | self.dispatch(node.body), |
paul@638 | 418 | self.dispatch(node.final) |
paul@638 | 419 | ) |
paul@638 | 420 | |
paul@638 | 421 | def visitWhile(self, node): |
paul@638 | 422 | return compiler.ast.While( |
paul@638 | 423 | self.dispatch(node.test), |
paul@638 | 424 | self.dispatch(node.body), |
paul@638 | 425 | node.else_ and self.dispatch(node.else_) |
paul@638 | 426 | ) |
paul@638 | 427 | |
paul@638 | 428 | def visitYield(self, node): |
paul@638 | 429 | return compiler.ast.Yield(self.dispatch(node.value)) |
paul@638 | 430 | |
paul@638 | 431 | # Expression-related helper methods. |
paul@638 | 432 | |
paul@638 | 433 | def _visitBitBinary(self, node): |
paul@638 | 434 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 435 | last = self.dispatch(node.nodes[0]) |
paul@638 | 436 | |
paul@638 | 437 | for n in node.nodes[1:]: |
paul@638 | 438 | last = compiler.ast.CallFunc( |
paul@638 | 439 | module_attribute("operator", op_name), |
paul@638 | 440 | [last, self.dispatch(n)] |
paul@638 | 441 | ) |
paul@638 | 442 | |
paul@638 | 443 | return last |
paul@638 | 444 | |
paul@638 | 445 | def _visitBinary(self, node): |
paul@638 | 446 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 447 | |
paul@638 | 448 | return compiler.ast.CallFunc( |
paul@638 | 449 | module_attribute("operator", op_name), |
paul@638 | 450 | [self.dispatch(node.left), self.dispatch(node.right)] |
paul@638 | 451 | ) |
paul@638 | 452 | |
paul@638 | 453 | def _visitUnary(self, node): |
paul@638 | 454 | op_name = operator_functions[node.__class__.__name__] |
paul@638 | 455 | |
paul@638 | 456 | return compiler.ast.CallFunc( |
paul@638 | 457 | module_attribute("operator", op_name), |
paul@638 | 458 | [self.dispatch(node.expr)] |
paul@638 | 459 | ) |
paul@638 | 460 | |
paul@638 | 461 | # Expressions. |
paul@638 | 462 | |
paul@638 | 463 | def visitAdd(self, node): |
paul@638 | 464 | return self._visitBinary(node) |
paul@638 | 465 | |
paul@638 | 466 | def visitAnd(self, node): |
paul@638 | 467 | return compiler.ast.And([self.dispatch(n) for n in node.nodes]) |
paul@638 | 468 | |
paul@638 | 469 | def visitAssAttr(self, node, expr): |
paul@638 | 470 | possible_types = self.possible_accessor_types(node, defining_users=0) |
paul@638 | 471 | |
paul@638 | 472 | # NOTE: Derived from Getattr support. |
paul@638 | 473 | |
paul@638 | 474 | accessor = self.dispatch(node.expr) |
paul@638 | 475 | |
paul@638 | 476 | # NOTE: Replicate the _generateAttr logic. |
paul@638 | 477 | # NOTE: Should be able to store concrete value details on generated |
paul@638 | 478 | # NOTE: nodes, such as whether an expression yields a constant. |
paul@638 | 479 | |
paul@638 | 480 | # NOTE: Known targets: |
paul@638 | 481 | # NOTE: __storeaddress__ and __storeaddresscontext__ |
paul@638 | 482 | |
paul@638 | 483 | # NOTE: Attributes of self. |
paul@638 | 484 | |
paul@638 | 485 | # Usage observations. |
paul@638 | 486 | |
paul@638 | 487 | possible_types = self.possible_accessor_types(node, defining_users=0) |
paul@638 | 488 | |
paul@638 | 489 | # Record whether types were already deduced. If not, get types using |
paul@638 | 490 | # only this attribute. |
paul@638 | 491 | |
paul@638 | 492 | if not possible_types: |
paul@638 | 493 | possible_types = self.get_possible_types(node.attrname) |
paul@638 | 494 | |
paul@638 | 495 | attributes = self.get_attributes(possible_types, node.attrname) |
paul@638 | 496 | |
paul@638 | 497 | # Generate optimisations where only a single attribute applies. |
paul@638 | 498 | |
paul@638 | 499 | if len(attributes) == 1: |
paul@638 | 500 | value, target, target_name = attributes[0] |
paul@638 | 501 | |
paul@638 | 502 | # Static attributes. |
paul@638 | 503 | |
paul@638 | 504 | if value is not None: |
paul@638 | 505 | |
paul@638 | 506 | # Static attributes may be accompanied by a different context |
paul@638 | 507 | # depending on the accessor. |
paul@638 | 508 | # NOTE: Should determine whether the context is always replaced. |
paul@638 | 509 | |
paul@638 | 510 | return compiler.ast.CallFunc( |
paul@638 | 511 | special_name("__storeaddresscontextcond__"), |
paul@638 | 512 | [accessor, special_name(node.attrname), expr] |
paul@638 | 513 | ) |
paul@638 | 514 | |
paul@638 | 515 | # Non-static attributes. |
paul@638 | 516 | |
paul@638 | 517 | return compiler.ast.CallFunc( |
paul@638 | 518 | special_name("__storeattr__"), |
paul@638 | 519 | [accessor, special_name(node.attrname), expr] |
paul@638 | 520 | ) |
paul@638 | 521 | |
paul@638 | 522 | # With no usable deductions, generate a table-based access. |
paul@638 | 523 | |
paul@638 | 524 | return compiler.ast.CallFunc( |
paul@638 | 525 | special_name("__storeattrindex__"), |
paul@638 | 526 | [accessor, special_name(node.attrname), expr] |
paul@638 | 527 | ) |
paul@638 | 528 | |
paul@638 | 529 | def visitAssList(self, node, expr): |
paul@638 | 530 | return compiler.ast.Stmt([ |
paul@638 | 531 | self.dispatch(n, compiler.ast.CallFunc( |
paul@638 | 532 | module_attribute("operator", "getitem"), |
paul@638 | 533 | [expr, i] |
paul@638 | 534 | )) |
paul@638 | 535 | for (i, n) in enumerate(node.nodes) |
paul@638 | 536 | ]) |
paul@638 | 537 | |
paul@638 | 538 | def visitAssName(self, node, expr): |
paul@638 | 539 | unit = self.get_unit() |
paul@638 | 540 | |
paul@638 | 541 | # Generate appropriate name access operation. |
paul@638 | 542 | |
paul@638 | 543 | scope = getattr(node, "_scope", None) |
paul@638 | 544 | if not scope: |
paul@638 | 545 | attr, scope, from_name = self.get_unit()._get_with_scope(node.name) |
paul@638 | 546 | |
paul@638 | 547 | if scope == "constant": |
paul@638 | 548 | return node |
paul@638 | 549 | elif scope == "local": |
paul@638 | 550 | |
paul@638 | 551 | # Function locals are stored using a function. |
paul@638 | 552 | |
paul@638 | 553 | if isinstance(unit, Function): |
paul@638 | 554 | return compiler.ast.CallFunc( |
paul@638 | 555 | special_name("__storelocal__"), |
paul@638 | 556 | [special_name(node.name), expr] |
paul@638 | 557 | ) |
paul@638 | 558 | |
paul@638 | 559 | # Class locals are class attribute references. |
paul@638 | 560 | |
paul@638 | 561 | elif isinstance(unit, Class): |
paul@638 | 562 | return compiler.ast.CallFunc( |
paul@638 | 563 | special_name("__storeaddresscontext__"), |
paul@638 | 564 | [quoted_name(unit.full_name()), special_name(node.name), expr] |
paul@638 | 565 | ) |
paul@638 | 566 | |
paul@638 | 567 | # Module locals are module attribute references. |
paul@638 | 568 | |
paul@638 | 569 | elif isinstance(unit, Module): |
paul@638 | 570 | return compiler.ast.CallFunc( |
paul@638 | 571 | special_name("__storeaddress__"), |
paul@638 | 572 | [quoted_name(unit.full_name()), special_name(node.name), expr] |
paul@638 | 573 | ) |
paul@638 | 574 | else: |
paul@638 | 575 | raise TranslateError("Program unit has no local %r." % name) |
paul@638 | 576 | |
paul@638 | 577 | elif scope == "global": |
paul@638 | 578 | |
paul@638 | 579 | # Globals are references to module attributes. |
paul@638 | 580 | |
paul@638 | 581 | return compiler.ast.CallFunc( |
paul@638 | 582 | special_name("__storeaddress__"), |
paul@638 | 583 | [quoted_name(self.get_module().full_name()), special_name(node.name), expr] |
paul@638 | 584 | ) |
paul@638 | 585 | |
paul@638 | 586 | elif scope == "builtin": |
paul@638 | 587 | |
paul@638 | 588 | # Builtins are accessed via the __builtins__ module. |
paul@638 | 589 | |
paul@638 | 590 | return compiler.ast.CallFunc( |
paul@638 | 591 | special_name("__storeaddress__"), |
paul@638 | 592 | [special_name("__builtins__"), special_name(node.name), expr] |
paul@638 | 593 | ) |
paul@638 | 594 | |
paul@638 | 595 | else: |
paul@638 | 596 | # NOTE: This may happen because a class attribute is optimised away. |
paul@638 | 597 | return compiler.ast.CallFunc( |
paul@638 | 598 | special_name("__storeunknown__"), |
paul@638 | 599 | [special_name(node.name), expr] |
paul@638 | 600 | ) |
paul@638 | 601 | |
paul@638 | 602 | visitAssTuple = visitAssList |
paul@638 | 603 | |
paul@638 | 604 | def visitBitand(self, node): |
paul@638 | 605 | self._visitBitBinary(node) |
paul@638 | 606 | |
paul@638 | 607 | def visitBitor(self, node): |
paul@638 | 608 | self._visitBitBinary(node) |
paul@638 | 609 | |
paul@638 | 610 | def visitBitxor(self, node): |
paul@638 | 611 | self._visitBitBinary(node) |
paul@638 | 612 | |
paul@638 | 613 | def visitCallFunc(self, node): |
paul@638 | 614 | return compiler.ast.CallFunc( |
paul@638 | 615 | self.dispatch(node.node), |
paul@638 | 616 | [self.dispatch(arg) for arg in node.args], |
paul@638 | 617 | node.star_args and [self.dispatch(arg) for arg in node.star_args], |
paul@638 | 618 | node.dstar_args and [self.dispatch(arg) for arg in node.dstar_args] |
paul@638 | 619 | ) |
paul@638 | 620 | |
paul@638 | 621 | def visitCompare(self, node): |
paul@638 | 622 | nodes = [] |
paul@638 | 623 | left = node.expr |
paul@638 | 624 | for op_name, right in node.ops: |
paul@638 | 625 | nodes.append( |
paul@638 | 626 | compiler.ast.CallFunc( |
paul@638 | 627 | module_attribute("operator", operator_functions.get(op_name)), |
paul@638 | 628 | [self.dispatch(left), self.dispatch(right)] |
paul@638 | 629 | ) |
paul@638 | 630 | ) |
paul@638 | 631 | left = right |
paul@638 | 632 | return compiler.ast.And(nodes) |
paul@638 | 633 | |
paul@638 | 634 | visitConst = NOP |
paul@638 | 635 | |
paul@638 | 636 | def visitDict(self, node): |
paul@638 | 637 | return compiler.ast.Dict([(self.dispatch(key), self.dispatch(value)) for (key, value) in node.items]) |
paul@638 | 638 | |
paul@638 | 639 | def visitDiv(self, node): |
paul@638 | 640 | return self._visitBinary(node) |
paul@638 | 641 | |
paul@638 | 642 | def visitFloorDiv(self, node): |
paul@638 | 643 | return self._visitBinary(node) |
paul@638 | 644 | |
paul@638 | 645 | def visitGetattr(self, node, expr=None): |
paul@638 | 646 | if expr: |
paul@638 | 647 | return self.visitAssAttr(node, expr) |
paul@638 | 648 | |
paul@638 | 649 | accessor = self.dispatch(node.expr) |
paul@638 | 650 | |
paul@638 | 651 | # NOTE: Replicate the _generateAttr logic. |
paul@638 | 652 | # NOTE: Should be able to store concrete value details on generated |
paul@638 | 653 | # NOTE: nodes, such as whether an expression yields a constant. |
paul@638 | 654 | |
paul@638 | 655 | # NOTE: Known targets: |
paul@638 | 656 | # NOTE: class.__class__ => __builtins__.type |
paul@638 | 657 | # NOTE: __loadaddress__ and __loadaddresscontext__ |
paul@638 | 658 | |
paul@638 | 659 | # NOTE: Attributes of self. |
paul@638 | 660 | |
paul@638 | 661 | # Usage observations. |
paul@638 | 662 | |
paul@638 | 663 | possible_types = self.possible_accessor_types(node, defining_users=0) |
paul@638 | 664 | |
paul@638 | 665 | # Record whether types were already deduced. If not, get types using |
paul@638 | 666 | # only this attribute. |
paul@638 | 667 | |
paul@638 | 668 | if not possible_types: |
paul@638 | 669 | possible_types = self.get_possible_types(node.attrname) |
paul@638 | 670 | |
paul@638 | 671 | attributes = self.get_attributes(possible_types, node.attrname) |
paul@638 | 672 | |
paul@638 | 673 | # Generate optimisations where only a single attribute applies. |
paul@638 | 674 | |
paul@638 | 675 | if len(attributes) == 1: |
paul@638 | 676 | value, target, target_name = attributes[0] |
paul@638 | 677 | |
paul@638 | 678 | # Static attributes. |
paul@638 | 679 | |
paul@638 | 680 | if value is not None: |
paul@638 | 681 | |
paul@638 | 682 | # class.__class__ => __builtins__.type |
paul@638 | 683 | |
paul@638 | 684 | if node.attrname == "__class__": |
paul@638 | 685 | return compiler.ast.CallFunc( |
paul@638 | 686 | special_name("__loadaddress__"), |
paul@638 | 687 | [special_name("__builtins__"), special_name("type")] |
paul@638 | 688 | ) |
paul@638 | 689 | |
paul@638 | 690 | # Static attributes may be accompanied by a different context |
paul@638 | 691 | # depending on the accessor. |
paul@638 | 692 | # NOTE: Should determine whether the context is always replaced. |
paul@638 | 693 | |
paul@638 | 694 | return compiler.ast.CallFunc( |
paul@638 | 695 | special_name("__loadaddresscontextcond__"), |
paul@638 | 696 | [accessor, special_name(node.attrname)] |
paul@638 | 697 | ) |
paul@638 | 698 | |
paul@638 | 699 | # Non-static attributes. |
paul@638 | 700 | |
paul@638 | 701 | return compiler.ast.CallFunc( |
paul@638 | 702 | special_name("__loadattr__"), |
paul@638 | 703 | [accessor, special_name(node.attrname)] |
paul@638 | 704 | ) |
paul@638 | 705 | |
paul@638 | 706 | # With no usable deductions, generate a table-based access. |
paul@638 | 707 | |
paul@638 | 708 | return compiler.ast.CallFunc( |
paul@638 | 709 | special_name("__loadattrindex__"), |
paul@638 | 710 | [accessor, special_name(node.attrname)] |
paul@638 | 711 | ) |
paul@638 | 712 | |
paul@638 | 713 | def visitGenExpr(self, node): |
paul@638 | 714 | return compiler.ast.GenExpr(self.dispatch(node.code)) |
paul@638 | 715 | |
paul@638 | 716 | def visitGenExprFor(self, node): |
paul@638 | 717 | return compiler.ast.GenExprFor( |
paul@638 | 718 | self.dispatch(node.assign), # NOTE: Needs to dispatch to AssName/AssTuple/AssList with an expression. |
paul@638 | 719 | self.dispatch(node.iter), |
paul@638 | 720 | [self.dispatch(n) for n in node.ifs] |
paul@638 | 721 | ) |
paul@638 | 722 | |
paul@638 | 723 | def visitGenExprIf(self, node): |
paul@638 | 724 | return compiler.ast.GenExprIf(self.dispatch(node.test)) |
paul@638 | 725 | |
paul@638 | 726 | def visitGenExprInner(self, node): |
paul@638 | 727 | return compiler.ast.GenExprInner( |
paul@638 | 728 | self.dispatch(node.expr), |
paul@638 | 729 | [self.dispatch(n) for n in node.quals] |
paul@638 | 730 | ) |
paul@638 | 731 | |
paul@638 | 732 | def visitIfExp(self, node): |
paul@638 | 733 | return compiler.ast.IfExp( |
paul@638 | 734 | self.dispatch(node.then), |
paul@638 | 735 | self.dispatch(node.test), |
paul@638 | 736 | self.dispatch(node.else_) |
paul@638 | 737 | ) |
paul@638 | 738 | |
paul@638 | 739 | def visitInvert(self, node): |
paul@638 | 740 | return self._visitUnary(node) |
paul@638 | 741 | |
paul@638 | 742 | def visitKeyword(self, node): |
paul@638 | 743 | return compiler.ast.Keyword( |
paul@638 | 744 | node.name, |
paul@638 | 745 | self.dispatch(node.expr) |
paul@638 | 746 | ) |
paul@638 | 747 | |
paul@638 | 748 | def visitLambda(self, node): |
paul@638 | 749 | self.units.append(node.unit) |
paul@638 | 750 | |
paul@638 | 751 | try: |
paul@638 | 752 | return compiler.ast.Lambda( |
paul@638 | 753 | node.argnames, |
paul@638 | 754 | [self.dispatch(n) for n in node.defaults], |
paul@638 | 755 | node.flags, |
paul@638 | 756 | self.dispatch(node.code) |
paul@638 | 757 | ) |
paul@638 | 758 | finally: |
paul@638 | 759 | self.units.pop() |
paul@638 | 760 | |
paul@638 | 761 | def visitLeftShift(self, node): |
paul@638 | 762 | return self._visitBinary(node) |
paul@638 | 763 | |
paul@638 | 764 | def visitList(self, node, expr=None): |
paul@638 | 765 | if expr: |
paul@638 | 766 | return self.visitAssList(node, expr) |
paul@638 | 767 | return compiler.ast.List([self.dispatch(n) for n in node.nodes]) |
paul@638 | 768 | |
paul@638 | 769 | def visitListComp(self, node): |
paul@638 | 770 | return compiler.ast.ListComp( |
paul@638 | 771 | self.dispatch(node.expr), |
paul@638 | 772 | [self.dispatch(n) for n in node.quals] |
paul@638 | 773 | ) |
paul@638 | 774 | |
paul@638 | 775 | def visitListCompFor(self, node): |
paul@638 | 776 | return compiler.ast.ListCompFor( |
paul@638 | 777 | self.dispatch(node.assign), # NOTE: Needs to dispatch to AssName/AssTuple/AssList with an expression. |
paul@638 | 778 | self.dispatch(node.list), |
paul@638 | 779 | [self.dispatch(n) for n in node.ifs] |
paul@638 | 780 | ) |
paul@638 | 781 | |
paul@638 | 782 | def visitListCompIf(self, node): |
paul@638 | 783 | return compiler.ast.ListCompIf( |
paul@638 | 784 | self.dispatch(node.test) |
paul@638 | 785 | ) |
paul@638 | 786 | |
paul@638 | 787 | def visitMod(self, node): |
paul@638 | 788 | return self._visitBinary(node) |
paul@638 | 789 | |
paul@638 | 790 | def visitMul(self, node): |
paul@638 | 791 | return self._visitBinary(node) |
paul@638 | 792 | |
paul@638 | 793 | def visitName(self, node, expr=None): |
paul@638 | 794 | if expr: |
paul@638 | 795 | return self.visitAssName(node, expr) |
paul@638 | 796 | |
paul@638 | 797 | unit = self.get_unit() |
paul@638 | 798 | |
paul@638 | 799 | # Generate appropriate name access operation. |
paul@638 | 800 | |
paul@638 | 801 | scope = getattr(node, "_scope", None) |
paul@638 | 802 | if not scope: |
paul@638 | 803 | attr, scope, from_name = self.get_unit()._get_with_scope(node.name) |
paul@638 | 804 | |
paul@638 | 805 | if scope == "constant": |
paul@638 | 806 | return node |
paul@638 | 807 | elif scope == "local": |
paul@638 | 808 | |
paul@638 | 809 | # Function locals are referenced normally. |
paul@638 | 810 | |
paul@638 | 811 | if isinstance(unit, Function): |
paul@638 | 812 | return node |
paul@638 | 813 | |
paul@638 | 814 | # Class locals are class attribute references. |
paul@638 | 815 | # Module locals are module attribute references. |
paul@638 | 816 | |
paul@638 | 817 | elif isinstance(unit, (Class, Module)): |
paul@638 | 818 | return compiler.ast.CallFunc( |
paul@638 | 819 | special_name("__loadaddress__"), |
paul@638 | 820 | [quoted_name(unit.full_name()), special_name(node.name)] |
paul@638 | 821 | ) |
paul@638 | 822 | else: |
paul@638 | 823 | raise TranslateError("Program unit has no local %r." % name) |
paul@638 | 824 | |
paul@638 | 825 | elif scope == "global": |
paul@638 | 826 | |
paul@638 | 827 | # Globals are references to module attributes. |
paul@638 | 828 | |
paul@638 | 829 | return compiler.ast.CallFunc( |
paul@638 | 830 | special_name("__loadaddress__"), |
paul@638 | 831 | [quoted_name(self.get_module().full_name()), special_name(node.name)] |
paul@638 | 832 | ) |
paul@638 | 833 | |
paul@638 | 834 | elif scope == "builtin": |
paul@638 | 835 | |
paul@638 | 836 | # Builtins are accessed via the __builtins__ module. |
paul@638 | 837 | |
paul@638 | 838 | return compiler.ast.CallFunc( |
paul@638 | 839 | special_name("__loadaddress__"), |
paul@638 | 840 | [special_name("__builtins__"), special_name(node.name)] |
paul@638 | 841 | ) |
paul@638 | 842 | |
paul@638 | 843 | else: |
paul@638 | 844 | # NOTE: This may happen because a class attribute is optimised away. |
paul@638 | 845 | return compiler.ast.CallFunc( |
paul@638 | 846 | special_name("__loadunknown__"), |
paul@638 | 847 | [special_name(node.name)] |
paul@638 | 848 | ) |
paul@638 | 849 | |
paul@638 | 850 | def visitNot(self, node): |
paul@638 | 851 | return compiler.ast.Not(self.dispatch(node.expr)) |
paul@638 | 852 | |
paul@638 | 853 | def visitOr(self, node): |
paul@638 | 854 | return compiler.ast.Or([self.dispatch(n) for n in node.nodes]) |
paul@638 | 855 | |
paul@638 | 856 | def visitPower(self, node): |
paul@638 | 857 | return self._visitBinary(node) |
paul@638 | 858 | |
paul@638 | 859 | def visitRightShift(self, node): |
paul@638 | 860 | return self._visitBinary(node) |
paul@638 | 861 | |
paul@638 | 862 | def visitSlice(self, node, expr=None): |
paul@638 | 863 | return compiler.ast.CallFunc( |
paul@638 | 864 | module_attribute("operator", expr and "setslice" or "getslice"), |
paul@638 | 865 | [self.dispatch(node.expr), node.lower and self.dispatch(node.lower), node.upper and self.dispatch(node.upper)] |
paul@638 | 866 | + (expr and [expr] or []) |
paul@638 | 867 | ) |
paul@638 | 868 | |
paul@638 | 869 | def visitSliceobj(self, node): |
paul@638 | 870 | return compiler.ast.Sliceobj([self.dispatch(n) for n in node.nodes]) |
paul@638 | 871 | |
paul@638 | 872 | def visitSub(self, node): |
paul@638 | 873 | return self._visitBinary(node) |
paul@638 | 874 | |
paul@638 | 875 | def visitSubscript(self, node, expr=None): |
paul@638 | 876 | return compiler.ast.CallFunc( |
paul@638 | 877 | module_attribute("operator", expr and "setitem" or "getitem"), |
paul@638 | 878 | [self.dispatch(node.expr), compiler.ast.Tuple([self.dispatch(sub) for sub in node.subs])] |
paul@638 | 879 | + (expr and [expr] or []) |
paul@638 | 880 | ) |
paul@638 | 881 | |
paul@638 | 882 | def visitTuple(self, node, expr=None): |
paul@638 | 883 | if expr: |
paul@638 | 884 | return self.visitAssTuple(node, expr) |
paul@638 | 885 | return compiler.ast.Tuple([self.dispatch(n) for n in node.nodes]) |
paul@638 | 886 | |
paul@638 | 887 | def visitUnaryAdd(self, node): |
paul@638 | 888 | return self._visitUnary(node) |
paul@638 | 889 | |
paul@638 | 890 | def visitUnarySub(self, node): |
paul@638 | 891 | return self._visitUnary(node) |
paul@638 | 892 | |
paul@638 | 893 | # Type-related methods. |
paul@638 | 894 | |
paul@638 | 895 | def possible_accessor_types(self, node, defining_users=1): |
paul@638 | 896 | return set([tn for (tn, st) in ASTVisitor.possible_accessor_types(self, node, defining_users)]) |
paul@638 | 897 | |
paul@638 | 898 | def get_possible_types(self, attrname): |
paul@638 | 899 | objtable = self.program.get_object_table() |
paul@638 | 900 | return objtable.any_possible_objects([attrname]) |
paul@638 | 901 | |
paul@638 | 902 | def get_attributes(self, possible_types, attrname): |
paul@638 | 903 | objtable = self.program.get_object_table() |
paul@638 | 904 | attributes = [] |
paul@638 | 905 | for target_name in possible_types: |
paul@638 | 906 | target = objtable.get_object(target_name) |
paul@638 | 907 | try: |
paul@638 | 908 | attr = objtable.access(target_name, attrname) |
paul@638 | 909 | except TableError: |
paul@638 | 910 | continue |
paul@638 | 911 | if attr.is_static_attribute(): |
paul@638 | 912 | for v in attr.get_values(): |
paul@638 | 913 | attributes.append((v, target, target_name)) |
paul@638 | 914 | else: |
paul@638 | 915 | attributes.append((None, target, target_name)) |
paul@638 | 916 | |
paul@638 | 917 | return attributes |
paul@638 | 918 | |
paul@638 | 919 | # Convenience functions. |
paul@638 | 920 | |
paul@638 | 921 | def convert(module, program, filename): |
paul@638 | 922 | stream = open(filename, "wb") |
paul@638 | 923 | try: |
paul@638 | 924 | source = ConvertedSource(module, program) |
paul@638 | 925 | source.to_stream(stream) |
paul@638 | 926 | finally: |
paul@638 | 927 | stream.close() |
paul@638 | 928 | |
paul@638 | 929 | def translate(program, directory): |
paul@638 | 930 | if not exists(directory): |
paul@638 | 931 | os.mkdir(directory) |
paul@638 | 932 | |
paul@638 | 933 | # NOTE: Add constants here. |
paul@638 | 934 | |
paul@638 | 935 | for module in program.get_importer().get_modules(): |
paul@638 | 936 | convert(module, program, join(directory, "%s%spy" % (module.full_name(), extsep))) |
paul@638 | 937 | |
paul@638 | 938 | # vim: tabstop=4 expandtab shiftwidth=4 |