paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | The micropython package for processing Python source code. The code originates |
paul@0 | 5 | from the simplify package but has had various details related to that package |
paul@0 | 6 | removed. |
paul@0 | 7 | |
paul@394 | 8 | Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 13 | version. |
paul@0 | 14 | |
paul@0 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 18 | details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public License along with |
paul@0 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 22 | |
paul@0 | 23 | -------- |
paul@0 | 24 | |
paul@280 | 25 | To use this module, an importer should be constructed. Here, the standard path |
paul@280 | 26 | for module searching is employed: |
paul@0 | 27 | |
paul@0 | 28 | importer = Importer(sys.path) |
paul@0 | 29 | |
paul@189 | 30 | To generate programs, the above importer should be supplied in the |
paul@189 | 31 | initialisation of a program instance, and then various methods are called: |
paul@189 | 32 | |
paul@189 | 33 | program = Program(importer) |
paul@189 | 34 | image = program.get_raw_image() |
paul@189 | 35 | |
paul@189 | 36 | Such importer and program objects are the most convenient mechanism through |
paul@189 | 37 | which the functionality of the micropython package may be accessed. |
paul@0 | 38 | """ |
paul@0 | 39 | |
paul@437 | 40 | from micropython.common import * |
paul@17 | 41 | import micropython.ast |
paul@168 | 42 | import micropython.data |
paul@154 | 43 | import micropython.opt |
paul@0 | 44 | import micropython.inspect |
paul@7 | 45 | import micropython.table |
paul@149 | 46 | import os |
paul@228 | 47 | |
paul@0 | 48 | try: |
paul@0 | 49 | set |
paul@0 | 50 | except NameError: |
paul@0 | 51 | from sets import Set as set |
paul@0 | 52 | |
paul@175 | 53 | class Program: |
paul@0 | 54 | |
paul@175 | 55 | "This class supports the generation of a program image." |
paul@0 | 56 | |
paul@154 | 57 | supported_optimisations = micropython.opt.Optimiser.supported_optimisations |
paul@63 | 58 | |
paul@175 | 59 | def __init__(self, importer, optimisations=None): |
paul@0 | 60 | |
paul@0 | 61 | """ |
paul@175 | 62 | Initialise the program representation with an 'importer' which is able |
paul@175 | 63 | to locate and load Python modules. |
paul@35 | 64 | |
paul@167 | 65 | The optional 'optimisations' cause certain techniques to be used in |
paul@167 | 66 | reducing program size and improving program efficiency. |
paul@0 | 67 | """ |
paul@0 | 68 | |
paul@175 | 69 | self.importer = importer |
paul@175 | 70 | self.optimisations = optimisations or set() |
paul@0 | 71 | |
paul@60 | 72 | # Remember the tables once generated. |
paul@60 | 73 | |
paul@60 | 74 | self.objtable = None |
paul@60 | 75 | self.paramtable = None |
paul@60 | 76 | |
paul@93 | 77 | # Main program information. |
paul@93 | 78 | |
paul@93 | 79 | self.code = None |
paul@93 | 80 | self.code_location = None |
paul@93 | 81 | |
paul@175 | 82 | def get_importer(self): |
paul@175 | 83 | return self.importer |
paul@137 | 84 | |
paul@169 | 85 | # Access to finalised program information. |
paul@169 | 86 | |
paul@353 | 87 | def finalise(self): |
paul@353 | 88 | |
paul@353 | 89 | "Finalise the program." |
paul@353 | 90 | |
paul@353 | 91 | # Need the tables to finalise. |
paul@353 | 92 | |
paul@353 | 93 | objtable = self.get_object_table() |
paul@353 | 94 | self.get_parameter_table() |
paul@353 | 95 | |
paul@353 | 96 | self.importer.vacuum(objtable) |
paul@353 | 97 | |
paul@353 | 98 | # Now remove unneeded things from the tables. |
paul@353 | 99 | |
paul@437 | 100 | objtable = self.get_object_table(reset=1) |
paul@353 | 101 | self.get_parameter_table(reset=1) |
paul@353 | 102 | |
paul@437 | 103 | self.importer.finalise(objtable) |
paul@437 | 104 | |
paul@167 | 105 | def get_image(self, with_builtins=0): |
paul@9 | 106 | |
paul@265 | 107 | """ |
paul@265 | 108 | Return the program image including built-in objects if 'with_builtins' |
paul@265 | 109 | is specified and set to a true value. |
paul@265 | 110 | """ |
paul@9 | 111 | |
paul@137 | 112 | if self.code is not None: |
paul@137 | 113 | return self.code |
paul@137 | 114 | |
paul@280 | 115 | # Optimise and regenerate the object table. |
paul@280 | 116 | |
paul@353 | 117 | self.finalise() |
paul@173 | 118 | self.code = [] |
paul@18 | 119 | |
paul@82 | 120 | # Append constants to the image. |
paul@82 | 121 | |
paul@265 | 122 | for const in self.importer.constants(): |
paul@173 | 123 | self.code.append(const) |
paul@82 | 124 | |
paul@175 | 125 | last_module = self.importer.modules_ordered[-1] |
paul@139 | 126 | |
paul@175 | 127 | for module in self.importer.modules_ordered: |
paul@431 | 128 | suppress_builtins = not with_builtins and module.name in ("__builtins__", "native") |
paul@15 | 129 | |
paul@55 | 130 | # Position the module in the image and make a translation. |
paul@55 | 131 | |
paul@167 | 132 | trans = micropython.ast.Translation(module, self) |
paul@55 | 133 | |
paul@37 | 134 | # Add header details. |
paul@37 | 135 | |
paul@173 | 136 | self.code.append(module) |
paul@37 | 137 | |
paul@15 | 138 | # Append module attributes to the image. |
paul@15 | 139 | |
paul@9 | 140 | attributes = module.module_attributes() |
paul@173 | 141 | self.code += module.attributes_as_list() |
paul@9 | 142 | |
paul@15 | 143 | # Append classes and functions to the image. |
paul@15 | 144 | |
paul@9 | 145 | for obj in module.all_objects: |
paul@394 | 146 | if isinstance(obj, micropython.data.Class): |
paul@54 | 147 | |
paul@22 | 148 | # Add header details. |
paul@22 | 149 | |
paul@173 | 150 | self.code.append(obj) |
paul@15 | 151 | |
paul@15 | 152 | # Append class attributes to the image. |
paul@15 | 153 | |
paul@9 | 154 | attributes = obj.class_attributes() |
paul@173 | 155 | self.code += obj.attributes_as_list() |
paul@9 | 156 | |
paul@203 | 157 | # Omit built-in function code where requested. |
paul@203 | 158 | |
paul@253 | 159 | if suppress_builtins and obj.astnode.doc is None: |
paul@203 | 160 | continue |
paul@203 | 161 | |
paul@137 | 162 | # Generate the instantiator/initialiser. |
paul@137 | 163 | # Append the function code to the image. |
paul@15 | 164 | |
paul@137 | 165 | code = trans.get_instantiator_code(obj) |
paul@173 | 166 | self.code += code |
paul@137 | 167 | |
paul@137 | 168 | # Class-level code is generated separately at the module |
paul@137 | 169 | # level, and the code location is set within the code |
paul@137 | 170 | # generation process for the module. |
paul@9 | 171 | |
paul@394 | 172 | elif isinstance(obj, micropython.data.Function): |
paul@15 | 173 | |
paul@22 | 174 | # Add header details. |
paul@22 | 175 | |
paul@173 | 176 | self.code.append(obj) |
paul@9 | 177 | |
paul@66 | 178 | # Append any default values to the image. |
paul@331 | 179 | # Only do this for functions which are not dynamic. |
paul@66 | 180 | |
paul@331 | 181 | if not obj.is_dynamic(): |
paul@173 | 182 | self.code += obj.default_attrs |
paul@66 | 183 | |
paul@180 | 184 | # Omit built-in function code where requested. |
paul@180 | 185 | |
paul@253 | 186 | if suppress_builtins and obj.astnode.doc is None: |
paul@180 | 187 | pass |
paul@180 | 188 | |
paul@15 | 189 | # Append the function code to the image. |
paul@15 | 190 | |
paul@180 | 191 | else: |
paul@180 | 192 | code = trans.get_code(obj) |
paul@180 | 193 | self.code += code |
paul@180 | 194 | |
paul@180 | 195 | # Omit built-in module code where requested. |
paul@180 | 196 | |
paul@205 | 197 | if suppress_builtins: |
paul@180 | 198 | pass |
paul@15 | 199 | |
paul@15 | 200 | # Append the module top-level code to the image. |
paul@15 | 201 | |
paul@180 | 202 | else: |
paul@242 | 203 | code = trans.get_module_code() |
paul@180 | 204 | self.code += code |
paul@15 | 205 | |
paul@173 | 206 | return self.code |
paul@173 | 207 | |
paul@265 | 208 | def get_raw_image(self, architecture=None, with_builtins=0): |
paul@173 | 209 | |
paul@173 | 210 | "Return the raw image representation of the program." |
paul@173 | 211 | |
paul@265 | 212 | architecture = architecture or micropython.rsvp |
paul@265 | 213 | |
paul@173 | 214 | self.get_image(with_builtins) |
paul@173 | 215 | |
paul@173 | 216 | objtable = self.get_object_table() |
paul@173 | 217 | paramtable = self.get_parameter_table() |
paul@173 | 218 | |
paul@173 | 219 | # Position the objects. |
paul@173 | 220 | |
paul@173 | 221 | pos = 0 |
paul@215 | 222 | |
paul@173 | 223 | for item in self.code: |
paul@265 | 224 | arch_item = architecture.get_object(item) |
paul@180 | 225 | |
paul@265 | 226 | # Get the raw version for the architecture. |
paul@183 | 227 | |
paul@265 | 228 | if arch_item is not None: |
paul@412 | 229 | pos = arch_item.set_location(pos, objtable, with_builtins) |
paul@173 | 230 | else: |
paul@173 | 231 | pos += 1 |
paul@173 | 232 | |
paul@173 | 233 | # Generate the raw code. |
paul@173 | 234 | |
paul@173 | 235 | self.raw_code = [] |
paul@173 | 236 | |
paul@173 | 237 | for item in self.code: |
paul@265 | 238 | arch_item = architecture.get_object(item) |
paul@173 | 239 | |
paul@265 | 240 | # Get the raw version for the architecture. |
paul@139 | 241 | |
paul@265 | 242 | if arch_item is not None: |
paul@265 | 243 | self.raw_code += arch_item.as_raw(objtable, paramtable, with_builtins) |
paul@265 | 244 | arch_item.finalise_location(with_builtins) |
paul@173 | 245 | else: |
paul@173 | 246 | self.raw_code.append(item) |
paul@173 | 247 | |
paul@173 | 248 | # Fix the module locations. |
paul@173 | 249 | |
paul@175 | 250 | for module in self.importer.modules_ordered: |
paul@173 | 251 | |
paul@431 | 252 | if not with_builtins and module.name in ("__builtins__", "native"): |
paul@173 | 253 | continue |
paul@173 | 254 | |
paul@173 | 255 | module.code_location = module.blocks[0].location |
paul@173 | 256 | |
paul@175 | 257 | self.code_location = self.importer.modules["__main__"].code_location |
paul@173 | 258 | return self.raw_code |
paul@173 | 259 | |
paul@280 | 260 | def get_object_table(self, reset=0): |
paul@7 | 261 | |
paul@7 | 262 | "Return a table with details of attributes for classes and modules." |
paul@7 | 263 | |
paul@280 | 264 | if self.objtable is None or reset: |
paul@171 | 265 | |
paul@132 | 266 | t = self.objtable = micropython.table.ObjectTable() |
paul@175 | 267 | for module in self.importer.get_modules(): |
paul@418 | 268 | |
paul@418 | 269 | # Add module attributes and module identity information. |
paul@418 | 270 | |
paul@418 | 271 | full_name = module.full_name() |
paul@418 | 272 | attributes = {full_name : module} |
paul@418 | 273 | attributes.update(module.module_attributes()) |
paul@418 | 274 | t.add(full_name, attributes) |
paul@135 | 275 | |
paul@135 | 276 | # Add class and instance attributes for all classes, together |
paul@135 | 277 | # with descendant information. |
paul@135 | 278 | |
paul@60 | 279 | for obj in module.all_objects: |
paul@394 | 280 | if isinstance(obj, micropython.data.Class): |
paul@243 | 281 | |
paul@243 | 282 | # Prevent ambiguous classes. |
paul@243 | 283 | |
paul@243 | 284 | full_name = obj.full_name() |
paul@243 | 285 | |
paul@418 | 286 | #name = obj.name |
paul@388 | 287 | #if module.has_key(name) and module[name].defines_ambiguous_class(): |
paul@388 | 288 | # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (name, module.full_name()) |
paul@243 | 289 | |
paul@243 | 290 | # Define a table entry for the class. |
paul@243 | 291 | |
paul@243 | 292 | attributes = {full_name : obj} |
paul@135 | 293 | attributes.update(obj.all_attributes()) |
paul@135 | 294 | attributes.update(obj.all_descendants()) |
paul@243 | 295 | t.add(full_name, attributes) |
paul@60 | 296 | |
paul@60 | 297 | return self.objtable |
paul@7 | 298 | |
paul@286 | 299 | def get_parameter_table(self, reset=0): |
paul@7 | 300 | |
paul@7 | 301 | "Return a table with details of parameters for functions and methods." |
paul@7 | 302 | |
paul@60 | 303 | # Need the object table to get at class details. |
paul@60 | 304 | |
paul@286 | 305 | if self.paramtable is None or reset: |
paul@132 | 306 | t = self.paramtable = micropython.table.ParameterTable() |
paul@71 | 307 | |
paul@71 | 308 | # Visit each module, getting function and method details. |
paul@71 | 309 | |
paul@175 | 310 | for module in self.importer.get_modules(): |
paul@60 | 311 | for obj in module.all_objects: |
paul@394 | 312 | if isinstance(obj, micropython.data.Function): |
paul@60 | 313 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 314 | |
paul@60 | 315 | # Classes are callable, too. |
paul@61 | 316 | # Take details of the appropriate __init__ method to make an |
paul@61 | 317 | # entry for an instantiation function for the class. |
paul@60 | 318 | |
paul@394 | 319 | elif isinstance(obj, micropython.data.Class): |
paul@212 | 320 | t.add(obj.get_instantiator().full_name(), obj.get_instantiator().parameters()) |
paul@60 | 321 | |
paul@71 | 322 | # Filter out all parameter table entries not referenced by keyword |
paul@71 | 323 | # arguments. |
paul@71 | 324 | |
paul@71 | 325 | keyword_names = set() |
paul@71 | 326 | |
paul@175 | 327 | for module in self.importer.get_modules(): |
paul@71 | 328 | keyword_names.update(module.keyword_names) |
paul@71 | 329 | |
paul@71 | 330 | for function_name, parameters in t.table.items(): |
paul@71 | 331 | for name in parameters.keys(): |
paul@71 | 332 | if name in keyword_names: |
paul@71 | 333 | break |
paul@71 | 334 | else: |
paul@71 | 335 | del t.table[function_name] |
paul@71 | 336 | |
paul@60 | 337 | return self.paramtable |
paul@7 | 338 | |
paul@175 | 339 | class Importer: |
paul@175 | 340 | |
paul@175 | 341 | "An import machine, searching for and loading modules." |
paul@175 | 342 | |
paul@175 | 343 | predefined_constants = { |
paul@175 | 344 | "None" : None, |
paul@175 | 345 | "True" : True, |
paul@175 | 346 | "False" : False, |
paul@203 | 347 | #"Ellipsis" : Ellipsis, |
paul@175 | 348 | "NotImplemented" : NotImplemented |
paul@175 | 349 | } |
paul@175 | 350 | |
paul@280 | 351 | names_always_used = [ |
paul@463 | 352 | "__call__" |
paul@280 | 353 | ] |
paul@280 | 354 | |
paul@175 | 355 | def __init__(self, path=None, verbose=0, optimisations=None): |
paul@175 | 356 | |
paul@175 | 357 | """ |
paul@175 | 358 | Initialise the importer with the given search 'path' - a list of |
paul@175 | 359 | directories to search for Python modules. |
paul@175 | 360 | |
paul@175 | 361 | The optional 'verbose' parameter causes output concerning the activities |
paul@175 | 362 | of the object to be produced if set to a true value (not the default). |
paul@175 | 363 | |
paul@175 | 364 | The optional 'optimisations' cause certain techniques to be used in |
paul@175 | 365 | reducing program size and improving program efficiency. |
paul@175 | 366 | """ |
paul@175 | 367 | |
paul@175 | 368 | self.path = path or [os.getcwd()] |
paul@175 | 369 | self.verbose = verbose |
paul@175 | 370 | self.optimisations = optimisations or set() |
paul@175 | 371 | |
paul@175 | 372 | self.modules = {} |
paul@175 | 373 | self.modules_ordered = [] |
paul@175 | 374 | self.loading = set() |
paul@175 | 375 | |
paul@175 | 376 | # Constant records. |
paul@175 | 377 | |
paul@175 | 378 | self.constant_values = {} |
paul@440 | 379 | self.constants_used = set() |
paul@440 | 380 | self.constant_references = {} |
paul@175 | 381 | self.init_predefined_constants() |
paul@175 | 382 | |
paul@280 | 383 | # Attribute usage. |
paul@175 | 384 | |
paul@344 | 385 | self.attributes_used = set() |
paul@250 | 386 | self.name_references = {} |
paul@281 | 387 | self.specific_name_references = {} |
paul@403 | 388 | |
paul@403 | 389 | # Attribute coverage calculated during collection. |
paul@403 | 390 | |
paul@403 | 391 | self.inferred_name_references = {} |
paul@403 | 392 | |
paul@403 | 393 | # Attribute coverage status during collection. |
paul@403 | 394 | |
paul@289 | 395 | self.attribute_users_visited = set() |
paul@345 | 396 | self.attributes_to_visit = {} |
paul@175 | 397 | |
paul@175 | 398 | # Status information. |
paul@175 | 399 | |
paul@175 | 400 | self.vacuumed = 0 |
paul@175 | 401 | self.finalised = 0 |
paul@175 | 402 | |
paul@175 | 403 | def get_modules(self): |
paul@175 | 404 | |
paul@175 | 405 | "Return all modules known to the importer." |
paul@175 | 406 | |
paul@175 | 407 | return self.modules.values() |
paul@175 | 408 | |
paul@228 | 409 | def get_module(self, name): |
paul@228 | 410 | |
paul@228 | 411 | "Return the module with the given 'name'." |
paul@228 | 412 | |
paul@228 | 413 | return self.modules[name] |
paul@228 | 414 | |
paul@175 | 415 | # General maintenance. |
paul@175 | 416 | |
paul@280 | 417 | def vacuum(self, objtable): |
paul@175 | 418 | |
paul@175 | 419 | "Tidy up the modules." |
paul@175 | 420 | |
paul@175 | 421 | if self.vacuumed: |
paul@175 | 422 | return |
paul@175 | 423 | |
paul@280 | 424 | # Complete the list of attribute names used in the program. |
paul@280 | 425 | |
paul@280 | 426 | self.collect_attributes(objtable) |
paul@280 | 427 | |
paul@175 | 428 | for name, module in self.modules.items(): |
paul@175 | 429 | if module.loaded: |
paul@175 | 430 | module.vacuum() |
paul@175 | 431 | else: |
paul@175 | 432 | del self.modules[name] |
paul@175 | 433 | |
paul@175 | 434 | self.vacuumed = 1 |
paul@175 | 435 | |
paul@437 | 436 | def finalise(self, objtable): |
paul@175 | 437 | |
paul@280 | 438 | "Finalise the program (which should have been vacuumed first)." |
paul@175 | 439 | |
paul@175 | 440 | if self.finalised: |
paul@175 | 441 | return |
paul@175 | 442 | |
paul@287 | 443 | # Reset any previously compiled information. |
paul@287 | 444 | |
paul@287 | 445 | for module in self.get_modules(): |
paul@287 | 446 | module.unfinalise() |
paul@287 | 447 | |
paul@287 | 448 | # Prepare module information again. |
paul@287 | 449 | |
paul@175 | 450 | for module in self.get_modules(): |
paul@437 | 451 | module.finalise(objtable) |
paul@175 | 452 | |
paul@175 | 453 | self.finalised = 1 |
paul@175 | 454 | |
paul@175 | 455 | # Name accounting. |
paul@175 | 456 | |
paul@404 | 457 | def use_name(self, name, from_name, value=None): |
paul@250 | 458 | |
paul@250 | 459 | """ |
paul@250 | 460 | Register the given 'name' as being used in the program from within an |
paul@404 | 461 | object with the specified 'from_name'. If the optional 'value' is given, |
paul@404 | 462 | note an assignment. |
paul@250 | 463 | """ |
paul@250 | 464 | |
paul@250 | 465 | if not self.name_references.has_key(from_name): |
paul@250 | 466 | self.name_references[from_name] = set() |
paul@280 | 467 | |
paul@404 | 468 | attrnames = ObjectSet([name]) |
paul@382 | 469 | usage = (attrnames,) |
paul@382 | 470 | self.name_references[from_name].add((None, None, usage)) |
paul@381 | 471 | |
paul@381 | 472 | def use_names(self, user, name, usage, from_name): |
paul@280 | 473 | |
paul@280 | 474 | """ |
paul@381 | 475 | For the given attribute 'user' (which may be None if no specific user is |
paul@381 | 476 | given), register for the given 'name' the given attribute 'usage' |
paul@381 | 477 | (combinations of attribute names), noting the scope of this usage as |
paul@381 | 478 | being the program object with the specified 'from_name'. |
paul@280 | 479 | """ |
paul@280 | 480 | |
paul@280 | 481 | if not self.name_references.has_key(from_name): |
paul@280 | 482 | self.name_references[from_name] = set() |
paul@381 | 483 | |
paul@382 | 484 | self.name_references[from_name].add((user, name, usage)) |
paul@280 | 485 | |
paul@285 | 486 | def use_specific_name(self, objname, attrname, from_name): |
paul@281 | 487 | |
paul@281 | 488 | """ |
paul@285 | 489 | Register the given 'objname' (for an object) whose 'attrname' is being |
paul@285 | 490 | used in the program from within an object with the specified |
paul@285 | 491 | 'from_name'. |
paul@281 | 492 | """ |
paul@281 | 493 | |
paul@281 | 494 | if not self.specific_name_references.has_key(from_name): |
paul@281 | 495 | self.specific_name_references[from_name] = set() |
paul@285 | 496 | self.specific_name_references[from_name].add((objname, attrname)) |
paul@281 | 497 | |
paul@281 | 498 | # Name accounting products. |
paul@281 | 499 | |
paul@344 | 500 | def uses_attribute(self, objname, name): |
paul@250 | 501 | |
paul@280 | 502 | """ |
paul@344 | 503 | Return whether the attribute of the object with the given 'objname' |
paul@344 | 504 | having the given 'name' is used as an attribute in the program. |
paul@280 | 505 | """ |
paul@280 | 506 | |
paul@344 | 507 | return (objname + "." + name) in self.attributes_used |
paul@280 | 508 | |
paul@280 | 509 | def use_attribute(self, objname, name): |
paul@175 | 510 | |
paul@280 | 511 | """ |
paul@280 | 512 | Indicate that in the object with the given 'objname', the attribute of |
paul@280 | 513 | the given 'name' is used. |
paul@280 | 514 | """ |
paul@280 | 515 | |
paul@344 | 516 | self.attributes_used.add(objname + "." + name) |
paul@250 | 517 | |
paul@345 | 518 | def use_object(self, objname): |
paul@345 | 519 | |
paul@345 | 520 | "Indicate that the object with the given 'objname' is used." |
paul@345 | 521 | |
paul@345 | 522 | self.attributes_used.add(objname) |
paul@345 | 523 | |
paul@280 | 524 | def collect_attributes(self, objtable): |
paul@280 | 525 | |
paul@280 | 526 | "Collect attribute references for the entire program." |
paul@280 | 527 | |
paul@345 | 528 | # Include names which may not be explicitly used in programs. |
paul@345 | 529 | # NOTE: Potentially declare these when inspecting. |
paul@345 | 530 | |
paul@345 | 531 | for attrname in self.names_always_used: |
paul@345 | 532 | for objname in objtable.all_possible_objects([attrname]): |
paul@345 | 533 | |
paul@345 | 534 | # Record attributes of objects for potential visiting. |
paul@345 | 535 | |
paul@345 | 536 | self.add_attribute_to_visit(objname, attrname) |
paul@345 | 537 | |
paul@367 | 538 | # Visit all modules, since some may employ initialisation code which has |
paul@367 | 539 | # some kind of side-effect. |
paul@283 | 540 | |
paul@367 | 541 | for name in self.modules.keys(): |
paul@367 | 542 | self._collect_attributes(name, objtable) |
paul@283 | 543 | |
paul@345 | 544 | def add_attribute_to_visit(self, objname, attrname): |
paul@351 | 545 | |
paul@351 | 546 | """ |
paul@351 | 547 | Queue an attribute of the object with the given 'objname', having the |
paul@351 | 548 | given 'attrname', to the list for potential visiting if the specified |
paul@351 | 549 | object is actually referenced. |
paul@351 | 550 | """ |
paul@351 | 551 | |
paul@345 | 552 | if not self.attributes_to_visit.has_key(objname): |
paul@345 | 553 | self.attributes_to_visit[objname] = set() |
paul@345 | 554 | self.attributes_to_visit[objname].add(attrname) |
paul@280 | 555 | |
paul@403 | 556 | def _collect_attributes_from(self, from_name, objname, attrname, objtable): |
paul@403 | 557 | |
paul@403 | 558 | """ |
paul@403 | 559 | Record the association between 'from_name' and the attribute of |
paul@403 | 560 | 'objname' with the given 'attrname'. Then collect attributes for the |
paul@403 | 561 | referenced attribute using 'objtable'. |
paul@403 | 562 | """ |
paul@403 | 563 | |
paul@403 | 564 | if not self.inferred_name_references.has_key(from_name): |
paul@403 | 565 | self.inferred_name_references[from_name] = set() |
paul@403 | 566 | |
paul@403 | 567 | self.inferred_name_references[from_name].add((objname, attrname)) |
paul@403 | 568 | self._collect_attributes(objname + "." + attrname, objtable) |
paul@403 | 569 | |
paul@280 | 570 | def _collect_attributes(self, from_name, objtable): |
paul@250 | 571 | |
paul@280 | 572 | """ |
paul@280 | 573 | Given an object called 'from_name', find all names referenced from such |
paul@403 | 574 | an object according to the register of names, using 'objtable' to infer |
paul@403 | 575 | types. |
paul@280 | 576 | """ |
paul@280 | 577 | |
paul@289 | 578 | if from_name in self.attribute_users_visited: |
paul@280 | 579 | return |
paul@280 | 580 | |
paul@301 | 581 | self.attribute_users_visited.add(from_name) |
paul@301 | 582 | |
paul@440 | 583 | # Get constant references. |
paul@440 | 584 | |
paul@440 | 585 | for const in self.constant_references.get(from_name, []): |
paul@440 | 586 | self.constants_used.add(const) |
paul@440 | 587 | |
paul@416 | 588 | # The getattr function is a special case: it can potentially reference |
paul@416 | 589 | # any known attribute. Since accessor attributes must be known |
paul@416 | 590 | # constants, the intersection of known constants and attributes is used |
paul@416 | 591 | # to build a set of objects that might be referenced by getattr. |
paul@416 | 592 | |
paul@416 | 593 | if from_name == "__builtins__.getattr": |
paul@416 | 594 | all_attributes = set(objtable.attribute_names()) |
paul@416 | 595 | all_string_constants = set([const.get_value() for const in self.constants() if const.value_type_name() == "__builtins__.str"]) |
paul@416 | 596 | all_attribute_constants = all_attributes.intersection(all_string_constants) |
paul@416 | 597 | |
paul@416 | 598 | # Get the types supporting each attribute and visit the referenced |
paul@416 | 599 | # objects. |
paul@416 | 600 | |
paul@416 | 601 | all_objtypes = set() |
paul@416 | 602 | |
paul@416 | 603 | for attrname in all_attribute_constants: |
paul@416 | 604 | objtypes = objtable.any_possible_objects_plus_status([attrname]) |
paul@416 | 605 | all_objtypes.update(objtypes) |
paul@416 | 606 | |
paul@416 | 607 | # Attribute assignment does not take place, so an empty list of |
paul@416 | 608 | # values is given. |
paul@416 | 609 | |
paul@416 | 610 | self._collect_attributes_for_types(from_name, objtable, all_objtypes, |
paul@416 | 611 | [{attrname : []} for attrname in all_attribute_constants]) |
paul@416 | 612 | |
paul@281 | 613 | # Get name references and find possible objects which support such |
paul@281 | 614 | # combinations of attribute names. |
paul@281 | 615 | |
paul@382 | 616 | for user, name, usage in self.name_references.get(from_name, []): |
paul@381 | 617 | |
paul@381 | 618 | # Using all attribute names for a particular name, attempt to get |
paul@381 | 619 | # specific object types. |
paul@381 | 620 | |
paul@437 | 621 | all_objtypes = get_object_types_for_usage(usage, objtable, name, from_name) |
paul@382 | 622 | |
paul@437 | 623 | # Investigate the object types. |
paul@420 | 624 | |
paul@420 | 625 | self._collect_attributes_for_types(from_name, objtable, all_objtypes, usage) |
paul@175 | 626 | |
paul@281 | 627 | # Get specific name references and visit the referenced objects. |
paul@281 | 628 | |
paul@285 | 629 | for objname, attrname in self.specific_name_references.get(from_name, []): |
paul@285 | 630 | self.use_attribute(objname, attrname) |
paul@403 | 631 | self._collect_attributes_from(from_name, objname, attrname, objtable) |
paul@281 | 632 | |
paul@332 | 633 | # Where the object has an __init__ attribute, assume that it is an |
paul@332 | 634 | # initialiser which is called at some point, and collect attributes used |
paul@332 | 635 | # in this initialiser. |
paul@332 | 636 | |
paul@332 | 637 | if "__init__" in objtable.table.get(from_name, []): |
paul@338 | 638 | self.use_attribute(from_name, "__init__") |
paul@403 | 639 | self._collect_attributes_from(from_name, from_name, "__init__", objtable) |
paul@332 | 640 | |
paul@384 | 641 | # Visit attributes on this object that were queued in case of the object |
paul@384 | 642 | # being referenced. |
paul@345 | 643 | |
paul@345 | 644 | attributes_to_visit = self.attributes_to_visit.get(from_name, []) |
paul@345 | 645 | |
paul@345 | 646 | if attributes_to_visit: |
paul@345 | 647 | del self.attributes_to_visit[from_name] |
paul@345 | 648 | |
paul@345 | 649 | for attrname in attributes_to_visit: |
paul@345 | 650 | self.use_attribute(from_name, attrname) |
paul@403 | 651 | self._collect_attributes_from(from_name, from_name, attrname, objtable) |
paul@345 | 652 | |
paul@416 | 653 | def _collect_attributes_for_types(self, from_name, objtable, objtypes, usage): |
paul@416 | 654 | |
paul@416 | 655 | """ |
paul@416 | 656 | For the unit known as 'from_name' and using the 'objtable' to validate |
paul@416 | 657 | each attribute, identify and attempt to visit attributes found for each |
paul@416 | 658 | of the suggested object types given by 'objtypes' and the 'usage' |
paul@416 | 659 | provided. |
paul@416 | 660 | """ |
paul@416 | 661 | |
paul@416 | 662 | for objname, is_static in objtypes: |
paul@416 | 663 | for attrnames in usage: |
paul@416 | 664 | for attrname, attrvalues in attrnames.items(): |
paul@416 | 665 | |
paul@416 | 666 | # Test for the presence of an attribute on the suggested |
paul@416 | 667 | # object type. |
paul@416 | 668 | |
paul@416 | 669 | try: |
paul@416 | 670 | attr = objtable.access(objname, attrname) |
paul@416 | 671 | except TableError: |
paul@416 | 672 | #print "Warning: object type %r does not support attribute %r" % (objname, attrname) |
paul@416 | 673 | continue |
paul@416 | 674 | |
paul@416 | 675 | # Get the real identity of the attribute in order to |
paul@416 | 676 | # properly collect usage from it. |
paul@416 | 677 | |
paul@416 | 678 | parent = attr.parent |
paul@421 | 679 | if parent is None: |
paul@421 | 680 | continue |
paul@421 | 681 | elif isinstance(parent, micropython.data.Instance): |
paul@416 | 682 | parentname = objname |
paul@416 | 683 | else: |
paul@416 | 684 | parentname = parent.full_name() |
paul@416 | 685 | |
paul@416 | 686 | # Test for assignment. |
paul@416 | 687 | |
paul@416 | 688 | if attrvalues: |
paul@427 | 689 | |
paul@427 | 690 | # Instance-related accesses may involve any type |
paul@427 | 691 | # supporting the attribute. |
paul@427 | 692 | # NOTE: Here, an instance actually represents any kind |
paul@427 | 693 | # NOTE: of object. |
paul@427 | 694 | |
paul@427 | 695 | if isinstance(parent, micropython.data.Instance): |
paul@427 | 696 | for attrvalue in attrvalues: |
paul@427 | 697 | for name in objtable.any_possible_objects([attrname]): |
paul@427 | 698 | parent = objtable.access(name, name) |
paul@444 | 699 | if not isinstance(parent, micropython.data.Class) or \ |
paul@444 | 700 | not parent.instance_attributes().has_key(attrname): |
paul@427 | 701 | parent.set(attrname, attrvalue, 0) |
paul@427 | 702 | else: |
paul@427 | 703 | for attrvalue in attrvalues: |
paul@427 | 704 | parent.set(attrname, attrvalue, 0) |
paul@416 | 705 | |
paul@416 | 706 | # Visit attributes of objects known to be used. |
paul@416 | 707 | |
paul@416 | 708 | if parentname in self.attributes_used: |
paul@416 | 709 | self.use_attribute(parentname, attrname) |
paul@416 | 710 | self._collect_attributes_from(from_name, parentname, attrname, objtable) |
paul@416 | 711 | |
paul@416 | 712 | # Record attributes of other objects for potential visiting. |
paul@416 | 713 | |
paul@416 | 714 | else: |
paul@416 | 715 | self.add_attribute_to_visit(parentname, attrname) |
paul@416 | 716 | |
paul@175 | 717 | # Constant accounting. |
paul@175 | 718 | |
paul@440 | 719 | def use_constant(self, const, from_name): |
paul@440 | 720 | |
paul@440 | 721 | """ |
paul@440 | 722 | Register the given 'const' as being used in the program from within an |
paul@440 | 723 | object with the specified 'from_name'. |
paul@440 | 724 | """ |
paul@440 | 725 | |
paul@440 | 726 | if not self.constant_references.has_key(from_name): |
paul@440 | 727 | self.constant_references[from_name] = set() |
paul@440 | 728 | |
paul@440 | 729 | self.constant_references[from_name].add(const) |
paul@440 | 730 | |
paul@175 | 731 | def init_predefined_constants(self): |
paul@175 | 732 | |
paul@175 | 733 | "Ensure the predefined constants." |
paul@175 | 734 | |
paul@175 | 735 | for name, value in self.predefined_constants.items(): |
paul@440 | 736 | self.constants_used.add(self.make_constant(value)) |
paul@175 | 737 | |
paul@175 | 738 | def get_predefined_constant(self, name): |
paul@175 | 739 | |
paul@175 | 740 | "Return the predefined constant for the given 'name'." |
paul@175 | 741 | |
paul@175 | 742 | return self.make_constant(self.predefined_constants[name]) |
paul@175 | 743 | |
paul@175 | 744 | def get_constant(self, value): |
paul@175 | 745 | |
paul@175 | 746 | "Return a constant for the given 'value'." |
paul@175 | 747 | |
paul@176 | 748 | const = micropython.data.Const(value) |
paul@176 | 749 | return self.constant_values[const] |
paul@175 | 750 | |
paul@252 | 751 | def get_constant_type_name(self, value): |
paul@415 | 752 | |
paul@415 | 753 | "Return the type name for the given constant 'value'." |
paul@415 | 754 | |
paul@252 | 755 | return value.__class__.__name__ |
paul@252 | 756 | |
paul@175 | 757 | def make_constant(self, value): |
paul@175 | 758 | |
paul@175 | 759 | "Make and return a constant for the given 'value'." |
paul@175 | 760 | |
paul@219 | 761 | # Make a constant object and return it. |
paul@219 | 762 | |
paul@176 | 763 | const = micropython.data.Const(value) |
paul@176 | 764 | if not self.constant_values.has_key(const): |
paul@176 | 765 | self.constant_values[const] = const |
paul@176 | 766 | return self.constant_values[const] |
paul@175 | 767 | |
paul@175 | 768 | def constants(self): |
paul@175 | 769 | |
paul@175 | 770 | "Return a list of constants." |
paul@175 | 771 | |
paul@440 | 772 | return self.constants_used |
paul@175 | 773 | |
paul@9 | 774 | # Import methods. |
paul@9 | 775 | |
paul@0 | 776 | def find_in_path(self, name): |
paul@0 | 777 | |
paul@0 | 778 | """ |
paul@0 | 779 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 780 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 781 | otherwise. |
paul@0 | 782 | """ |
paul@0 | 783 | |
paul@0 | 784 | for d in self.path: |
paul@0 | 785 | m = self.find(d, name) |
paul@0 | 786 | if m: return m |
paul@0 | 787 | return None |
paul@0 | 788 | |
paul@0 | 789 | def find(self, d, name): |
paul@0 | 790 | |
paul@0 | 791 | """ |
paul@0 | 792 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 793 | either refer to a single file module or to a package. Return None if the |
paul@0 | 794 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 795 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 796 | """ |
paul@0 | 797 | |
paul@0 | 798 | m = self._find_package(d, name) |
paul@0 | 799 | if m: return m |
paul@0 | 800 | m = self._find_module(d, name) |
paul@0 | 801 | if m: return m |
paul@0 | 802 | return None |
paul@0 | 803 | |
paul@0 | 804 | def _find_module(self, d, name): |
paul@0 | 805 | |
paul@0 | 806 | """ |
paul@0 | 807 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 808 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 809 | None (indicating that no package directory is involved) and a filename |
paul@0 | 810 | indicating the location of the module. |
paul@0 | 811 | """ |
paul@0 | 812 | |
paul@0 | 813 | name_py = name + os.extsep + "py" |
paul@0 | 814 | filename = self._find_file(d, name_py) |
paul@0 | 815 | if filename: |
paul@0 | 816 | return None, filename |
paul@0 | 817 | return None |
paul@0 | 818 | |
paul@0 | 819 | def _find_package(self, d, name): |
paul@0 | 820 | |
paul@0 | 821 | """ |
paul@0 | 822 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 823 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 824 | a directory (indicating the location of the package directory itself) |
paul@0 | 825 | and a filename indicating the location of the __init__.py module which |
paul@0 | 826 | declares the package's top-level contents. |
paul@0 | 827 | """ |
paul@0 | 828 | |
paul@0 | 829 | filename = self._find_file(d, name) |
paul@0 | 830 | if filename: |
paul@0 | 831 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 832 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 833 | if init_py_filename: |
paul@0 | 834 | return filename, init_py_filename |
paul@0 | 835 | return None |
paul@0 | 836 | |
paul@0 | 837 | def _find_file(self, d, filename): |
paul@0 | 838 | |
paul@0 | 839 | """ |
paul@0 | 840 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 841 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 842 | """ |
paul@0 | 843 | |
paul@0 | 844 | filename = os.path.join(d, filename) |
paul@0 | 845 | if os.path.exists(filename): |
paul@0 | 846 | return filename |
paul@0 | 847 | else: |
paul@0 | 848 | return None |
paul@0 | 849 | |
paul@0 | 850 | def load(self, name, return_leaf=0): |
paul@0 | 851 | |
paul@0 | 852 | """ |
paul@0 | 853 | Load the module or package with the given 'name'. Return an object |
paul@0 | 854 | referencing the loaded module or package, or None if no such module or |
paul@0 | 855 | package exists. |
paul@0 | 856 | """ |
paul@0 | 857 | |
paul@363 | 858 | if return_leaf: |
paul@363 | 859 | name_for_return = name |
paul@363 | 860 | else: |
paul@363 | 861 | name_for_return = name.split(".")[0] |
paul@363 | 862 | |
paul@4 | 863 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@4 | 864 | #print "Cached (%s)" % name |
paul@363 | 865 | return self.modules[name_for_return] |
paul@363 | 866 | |
paul@35 | 867 | if self.verbose: |
paul@35 | 868 | print "Loading", name |
paul@0 | 869 | |
paul@0 | 870 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 871 | # the search path. |
paul@0 | 872 | |
paul@0 | 873 | path = name.split(".") |
paul@0 | 874 | m = self.find_in_path(path[0]) |
paul@0 | 875 | if not m: |
paul@35 | 876 | if self.verbose: |
paul@35 | 877 | print "Not found (%s)" % path[0] |
paul@0 | 878 | return None # NOTE: Import error. |
paul@0 | 879 | d, filename = m |
paul@0 | 880 | |
paul@0 | 881 | # Either acquire a reference to an already-imported module, or load the |
paul@0 | 882 | # module from a file. |
paul@0 | 883 | |
paul@2 | 884 | top = module = self.load_from_file(filename, path[0]) |
paul@0 | 885 | |
paul@0 | 886 | # For hierarchical names, traverse each path component... |
paul@0 | 887 | |
paul@0 | 888 | if len(path) > 1: |
paul@0 | 889 | if not d: |
paul@35 | 890 | if self.verbose: |
paul@35 | 891 | print "No package (%s)" % filename |
paul@0 | 892 | return None # NOTE: Import error (package not found). |
paul@1 | 893 | else: |
paul@1 | 894 | self.add_submodules(d, module) |
paul@0 | 895 | |
paul@0 | 896 | path_so_far = path[:1] |
paul@0 | 897 | for p in path[1:]: |
paul@0 | 898 | path_so_far.append(p) |
paul@0 | 899 | |
paul@0 | 900 | # Find the package or module concerned. |
paul@0 | 901 | |
paul@0 | 902 | m = self.find(d, p) |
paul@0 | 903 | if not m: |
paul@35 | 904 | if self.verbose: |
paul@35 | 905 | print "Not found (%s)" % p |
paul@0 | 906 | return None # NOTE: Import error. |
paul@0 | 907 | d, filename = m |
paul@0 | 908 | module_name = ".".join(path_so_far) |
paul@0 | 909 | |
paul@0 | 910 | # Either reference an imported module or load one from a file. |
paul@0 | 911 | |
paul@2 | 912 | submodule = self.load_from_file(filename, module_name) |
paul@0 | 913 | |
paul@1 | 914 | if d: |
paul@1 | 915 | self.add_submodules(d, module) |
paul@1 | 916 | |
paul@0 | 917 | # Store the submodule within its parent module. |
paul@0 | 918 | |
paul@32 | 919 | module.set_module(p, submodule) |
paul@0 | 920 | module = submodule |
paul@0 | 921 | |
paul@0 | 922 | # Return either the deepest or the uppermost module. |
paul@0 | 923 | |
paul@0 | 924 | if return_leaf: |
paul@0 | 925 | return module |
paul@0 | 926 | else: |
paul@0 | 927 | return top |
paul@0 | 928 | |
paul@0 | 929 | def load_from_file(self, name, module_name=None): |
paul@0 | 930 | |
paul@0 | 931 | """ |
paul@0 | 932 | Load the module with the given 'name' (which may be a full module path). |
paul@0 | 933 | """ |
paul@0 | 934 | |
paul@0 | 935 | if module_name is None: |
paul@0 | 936 | module_name = "__main__" |
paul@0 | 937 | |
paul@1 | 938 | module = self.add_module(module_name) |
paul@2 | 939 | if not module.loaded and module not in self.loading: |
paul@2 | 940 | self.loading.add(module) |
paul@4 | 941 | #print "Parsing", name |
paul@2 | 942 | module.parse(name) |
paul@4 | 943 | #print "Done", name |
paul@2 | 944 | self.loading.remove(module) |
paul@2 | 945 | module.loaded = 1 |
paul@0 | 946 | |
paul@0 | 947 | # Record the module. |
paul@0 | 948 | |
paul@345 | 949 | self.use_object(module.full_name()) |
paul@4 | 950 | #print "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 951 | return module |
paul@0 | 952 | |
paul@1 | 953 | def add_module(self, module_name): |
paul@1 | 954 | |
paul@1 | 955 | """ |
paul@1 | 956 | Return the module with the given 'module_name', adding a new module |
paul@1 | 957 | object if one does not already exist. |
paul@1 | 958 | """ |
paul@1 | 959 | |
paul@1 | 960 | if not self.modules.has_key(module_name): |
paul@15 | 961 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@128 | 962 | self.modules_ordered.append(module) |
paul@1 | 963 | else: |
paul@1 | 964 | module = self.modules[module_name] |
paul@1 | 965 | return module |
paul@1 | 966 | |
paul@1 | 967 | def add_submodules(self, pathname, module): |
paul@1 | 968 | |
paul@1 | 969 | """ |
paul@1 | 970 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 971 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 972 | """ |
paul@1 | 973 | |
paul@1 | 974 | for filename in os.listdir(pathname): |
paul@50 | 975 | submodule, ext = os.path.splitext(filename) |
paul@50 | 976 | if ext not in ("", ".py"): |
paul@50 | 977 | continue |
paul@32 | 978 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 979 | |
paul@0 | 980 | # vim: tabstop=4 expandtab shiftwidth=4 |