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