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@404 | 40 | from micropython.common import ObjectSet, ProcessingError, TableError, \ |
paul@404 | 41 | TableGenerationError |
paul@17 | 42 | import micropython.ast |
paul@168 | 43 | import micropython.data |
paul@154 | 44 | import micropython.opt |
paul@0 | 45 | import micropython.inspect |
paul@7 | 46 | import micropython.table |
paul@149 | 47 | import os |
paul@228 | 48 | |
paul@0 | 49 | try: |
paul@0 | 50 | set |
paul@0 | 51 | except NameError: |
paul@0 | 52 | from sets import Set as set |
paul@0 | 53 | |
paul@175 | 54 | class Program: |
paul@0 | 55 | |
paul@175 | 56 | "This class supports the generation of a program image." |
paul@0 | 57 | |
paul@154 | 58 | supported_optimisations = micropython.opt.Optimiser.supported_optimisations |
paul@63 | 59 | |
paul@175 | 60 | def __init__(self, importer, optimisations=None): |
paul@0 | 61 | |
paul@0 | 62 | """ |
paul@175 | 63 | Initialise the program representation with an 'importer' which is able |
paul@175 | 64 | to locate and load Python modules. |
paul@35 | 65 | |
paul@167 | 66 | The optional 'optimisations' cause certain techniques to be used in |
paul@167 | 67 | reducing program size and improving program efficiency. |
paul@0 | 68 | """ |
paul@0 | 69 | |
paul@175 | 70 | self.importer = importer |
paul@175 | 71 | self.optimisations = optimisations or set() |
paul@0 | 72 | |
paul@60 | 73 | # Remember the tables once generated. |
paul@60 | 74 | |
paul@60 | 75 | self.objtable = None |
paul@60 | 76 | self.paramtable = None |
paul@60 | 77 | |
paul@93 | 78 | # Main program information. |
paul@93 | 79 | |
paul@93 | 80 | self.code = None |
paul@93 | 81 | self.code_location = None |
paul@93 | 82 | |
paul@175 | 83 | def get_importer(self): |
paul@175 | 84 | return self.importer |
paul@137 | 85 | |
paul@169 | 86 | # Access to finalised program information. |
paul@169 | 87 | |
paul@353 | 88 | def finalise(self): |
paul@353 | 89 | |
paul@353 | 90 | "Finalise the program." |
paul@353 | 91 | |
paul@353 | 92 | # Need the tables to finalise. |
paul@353 | 93 | |
paul@353 | 94 | objtable = self.get_object_table() |
paul@353 | 95 | self.get_parameter_table() |
paul@353 | 96 | |
paul@353 | 97 | self.importer.vacuum(objtable) |
paul@353 | 98 | self.importer.finalise() |
paul@353 | 99 | |
paul@353 | 100 | # Now remove unneeded things from the tables. |
paul@353 | 101 | |
paul@353 | 102 | self.get_object_table(reset=1) |
paul@353 | 103 | self.get_parameter_table(reset=1) |
paul@353 | 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@205 | 128 | suppress_builtins = not with_builtins and module.name == "__builtins__" |
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@173 | 252 | if not with_builtins and module.name == "__builtins__": |
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@60 | 268 | t.add(module.full_name(), module.module_attributes()) |
paul@135 | 269 | |
paul@135 | 270 | # Add class and instance attributes for all classes, together |
paul@135 | 271 | # with descendant information. |
paul@135 | 272 | |
paul@60 | 273 | for obj in module.all_objects: |
paul@394 | 274 | if isinstance(obj, micropython.data.Class): |
paul@243 | 275 | |
paul@243 | 276 | # Prevent ambiguous classes. |
paul@243 | 277 | |
paul@243 | 278 | full_name = obj.full_name() |
paul@243 | 279 | name = obj.name |
paul@243 | 280 | |
paul@388 | 281 | #if module.has_key(name) and module[name].defines_ambiguous_class(): |
paul@388 | 282 | # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (name, module.full_name()) |
paul@243 | 283 | |
paul@243 | 284 | # Define a table entry for the class. |
paul@243 | 285 | |
paul@243 | 286 | attributes = {full_name : obj} |
paul@135 | 287 | attributes.update(obj.all_attributes()) |
paul@135 | 288 | attributes.update(obj.all_descendants()) |
paul@243 | 289 | t.add(full_name, attributes) |
paul@60 | 290 | |
paul@60 | 291 | return self.objtable |
paul@7 | 292 | |
paul@286 | 293 | def get_parameter_table(self, reset=0): |
paul@7 | 294 | |
paul@7 | 295 | "Return a table with details of parameters for functions and methods." |
paul@7 | 296 | |
paul@60 | 297 | # Need the object table to get at class details. |
paul@60 | 298 | |
paul@286 | 299 | if self.paramtable is None or reset: |
paul@132 | 300 | t = self.paramtable = micropython.table.ParameterTable() |
paul@71 | 301 | |
paul@71 | 302 | # Visit each module, getting function and method details. |
paul@71 | 303 | |
paul@175 | 304 | for module in self.importer.get_modules(): |
paul@60 | 305 | for obj in module.all_objects: |
paul@394 | 306 | if isinstance(obj, micropython.data.Function): |
paul@60 | 307 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 308 | |
paul@60 | 309 | # Classes are callable, too. |
paul@61 | 310 | # Take details of the appropriate __init__ method to make an |
paul@61 | 311 | # entry for an instantiation function for the class. |
paul@60 | 312 | |
paul@394 | 313 | elif isinstance(obj, micropython.data.Class): |
paul@212 | 314 | t.add(obj.get_instantiator().full_name(), obj.get_instantiator().parameters()) |
paul@60 | 315 | |
paul@71 | 316 | # Filter out all parameter table entries not referenced by keyword |
paul@71 | 317 | # arguments. |
paul@71 | 318 | |
paul@71 | 319 | keyword_names = set() |
paul@71 | 320 | |
paul@175 | 321 | for module in self.importer.get_modules(): |
paul@71 | 322 | keyword_names.update(module.keyword_names) |
paul@71 | 323 | |
paul@71 | 324 | for function_name, parameters in t.table.items(): |
paul@71 | 325 | for name in parameters.keys(): |
paul@71 | 326 | if name in keyword_names: |
paul@71 | 327 | break |
paul@71 | 328 | else: |
paul@71 | 329 | del t.table[function_name] |
paul@71 | 330 | |
paul@60 | 331 | return self.paramtable |
paul@7 | 332 | |
paul@175 | 333 | class Importer: |
paul@175 | 334 | |
paul@175 | 335 | "An import machine, searching for and loading modules." |
paul@175 | 336 | |
paul@175 | 337 | predefined_constants = { |
paul@175 | 338 | "None" : None, |
paul@175 | 339 | "True" : True, |
paul@175 | 340 | "False" : False, |
paul@203 | 341 | #"Ellipsis" : Ellipsis, |
paul@175 | 342 | "NotImplemented" : NotImplemented |
paul@175 | 343 | } |
paul@175 | 344 | |
paul@280 | 345 | names_always_used = [ |
paul@338 | 346 | "bool", "__call__", "__bool__" |
paul@280 | 347 | ] |
paul@280 | 348 | |
paul@175 | 349 | def __init__(self, path=None, verbose=0, optimisations=None): |
paul@175 | 350 | |
paul@175 | 351 | """ |
paul@175 | 352 | Initialise the importer with the given search 'path' - a list of |
paul@175 | 353 | directories to search for Python modules. |
paul@175 | 354 | |
paul@175 | 355 | The optional 'verbose' parameter causes output concerning the activities |
paul@175 | 356 | of the object to be produced if set to a true value (not the default). |
paul@175 | 357 | |
paul@175 | 358 | The optional 'optimisations' cause certain techniques to be used in |
paul@175 | 359 | reducing program size and improving program efficiency. |
paul@175 | 360 | """ |
paul@175 | 361 | |
paul@175 | 362 | self.path = path or [os.getcwd()] |
paul@175 | 363 | self.verbose = verbose |
paul@175 | 364 | self.optimisations = optimisations or set() |
paul@175 | 365 | |
paul@175 | 366 | self.modules = {} |
paul@175 | 367 | self.modules_ordered = [] |
paul@175 | 368 | self.loading = set() |
paul@175 | 369 | |
paul@175 | 370 | # Constant records. |
paul@175 | 371 | |
paul@175 | 372 | self.constant_values = {} |
paul@175 | 373 | self.constant_list = None # cache for constants |
paul@175 | 374 | self.init_predefined_constants() |
paul@175 | 375 | |
paul@280 | 376 | # Attribute usage. |
paul@175 | 377 | |
paul@344 | 378 | self.attributes_used = set() |
paul@250 | 379 | self.name_references = {} |
paul@281 | 380 | self.specific_name_references = {} |
paul@403 | 381 | |
paul@403 | 382 | # Attribute coverage calculated during collection. |
paul@403 | 383 | |
paul@403 | 384 | self.inferred_name_references = {} |
paul@403 | 385 | |
paul@403 | 386 | # Attribute coverage status during collection. |
paul@403 | 387 | |
paul@289 | 388 | self.attribute_users_visited = set() |
paul@345 | 389 | self.attributes_to_visit = {} |
paul@175 | 390 | |
paul@175 | 391 | # Status information. |
paul@175 | 392 | |
paul@175 | 393 | self.vacuumed = 0 |
paul@175 | 394 | self.finalised = 0 |
paul@175 | 395 | |
paul@175 | 396 | def get_modules(self): |
paul@175 | 397 | |
paul@175 | 398 | "Return all modules known to the importer." |
paul@175 | 399 | |
paul@175 | 400 | return self.modules.values() |
paul@175 | 401 | |
paul@228 | 402 | def get_module(self, name): |
paul@228 | 403 | |
paul@228 | 404 | "Return the module with the given 'name'." |
paul@228 | 405 | |
paul@228 | 406 | return self.modules[name] |
paul@228 | 407 | |
paul@175 | 408 | # General maintenance. |
paul@175 | 409 | |
paul@280 | 410 | def vacuum(self, objtable): |
paul@175 | 411 | |
paul@175 | 412 | "Tidy up the modules." |
paul@175 | 413 | |
paul@175 | 414 | if self.vacuumed: |
paul@175 | 415 | return |
paul@175 | 416 | |
paul@280 | 417 | # Complete the list of attribute names used in the program. |
paul@280 | 418 | |
paul@280 | 419 | self.collect_attributes(objtable) |
paul@280 | 420 | |
paul@175 | 421 | for name, module in self.modules.items(): |
paul@175 | 422 | if module.loaded: |
paul@175 | 423 | module.vacuum() |
paul@175 | 424 | else: |
paul@175 | 425 | del self.modules[name] |
paul@175 | 426 | |
paul@175 | 427 | self.vacuumed = 1 |
paul@175 | 428 | |
paul@175 | 429 | def finalise(self): |
paul@175 | 430 | |
paul@280 | 431 | "Finalise the program (which should have been vacuumed first)." |
paul@175 | 432 | |
paul@175 | 433 | if self.finalised: |
paul@175 | 434 | return |
paul@175 | 435 | |
paul@287 | 436 | # Reset any previously compiled information. |
paul@287 | 437 | |
paul@287 | 438 | for module in self.get_modules(): |
paul@287 | 439 | module.unfinalise() |
paul@287 | 440 | |
paul@287 | 441 | # Prepare module information again. |
paul@287 | 442 | |
paul@175 | 443 | for module in self.get_modules(): |
paul@175 | 444 | module.finalise() |
paul@175 | 445 | |
paul@175 | 446 | self.finalised = 1 |
paul@175 | 447 | |
paul@175 | 448 | # Name accounting. |
paul@175 | 449 | |
paul@404 | 450 | def use_name(self, name, from_name, value=None): |
paul@250 | 451 | |
paul@250 | 452 | """ |
paul@250 | 453 | Register the given 'name' as being used in the program from within an |
paul@404 | 454 | object with the specified 'from_name'. If the optional 'value' is given, |
paul@404 | 455 | note an assignment. |
paul@250 | 456 | """ |
paul@250 | 457 | |
paul@250 | 458 | if not self.name_references.has_key(from_name): |
paul@250 | 459 | self.name_references[from_name] = set() |
paul@280 | 460 | |
paul@404 | 461 | attrnames = ObjectSet([name]) |
paul@382 | 462 | usage = (attrnames,) |
paul@382 | 463 | self.name_references[from_name].add((None, None, usage)) |
paul@381 | 464 | |
paul@381 | 465 | def use_names(self, user, name, usage, from_name): |
paul@280 | 466 | |
paul@280 | 467 | """ |
paul@381 | 468 | For the given attribute 'user' (which may be None if no specific user is |
paul@381 | 469 | given), register for the given 'name' the given attribute 'usage' |
paul@381 | 470 | (combinations of attribute names), noting the scope of this usage as |
paul@381 | 471 | being the program object with the specified 'from_name'. |
paul@280 | 472 | """ |
paul@280 | 473 | |
paul@280 | 474 | if not self.name_references.has_key(from_name): |
paul@280 | 475 | self.name_references[from_name] = set() |
paul@381 | 476 | |
paul@382 | 477 | self.name_references[from_name].add((user, name, usage)) |
paul@280 | 478 | |
paul@285 | 479 | def use_specific_name(self, objname, attrname, from_name): |
paul@281 | 480 | |
paul@281 | 481 | """ |
paul@285 | 482 | Register the given 'objname' (for an object) whose 'attrname' is being |
paul@285 | 483 | used in the program from within an object with the specified |
paul@285 | 484 | 'from_name'. |
paul@281 | 485 | """ |
paul@281 | 486 | |
paul@281 | 487 | if not self.specific_name_references.has_key(from_name): |
paul@281 | 488 | self.specific_name_references[from_name] = set() |
paul@285 | 489 | self.specific_name_references[from_name].add((objname, attrname)) |
paul@281 | 490 | |
paul@281 | 491 | # Name accounting products. |
paul@281 | 492 | |
paul@344 | 493 | def uses_attribute(self, objname, name): |
paul@250 | 494 | |
paul@280 | 495 | """ |
paul@344 | 496 | Return whether the attribute of the object with the given 'objname' |
paul@344 | 497 | having the given 'name' is used as an attribute in the program. |
paul@280 | 498 | """ |
paul@280 | 499 | |
paul@344 | 500 | return (objname + "." + name) in self.attributes_used |
paul@280 | 501 | |
paul@280 | 502 | def use_attribute(self, objname, name): |
paul@175 | 503 | |
paul@280 | 504 | """ |
paul@280 | 505 | Indicate that in the object with the given 'objname', the attribute of |
paul@280 | 506 | the given 'name' is used. |
paul@280 | 507 | """ |
paul@280 | 508 | |
paul@344 | 509 | self.attributes_used.add(objname + "." + name) |
paul@250 | 510 | |
paul@345 | 511 | def use_object(self, objname): |
paul@345 | 512 | |
paul@345 | 513 | "Indicate that the object with the given 'objname' is used." |
paul@345 | 514 | |
paul@345 | 515 | self.attributes_used.add(objname) |
paul@345 | 516 | |
paul@280 | 517 | def collect_attributes(self, objtable): |
paul@280 | 518 | |
paul@280 | 519 | "Collect attribute references for the entire program." |
paul@280 | 520 | |
paul@345 | 521 | # Include names which may not be explicitly used in programs. |
paul@345 | 522 | # NOTE: Potentially declare these when inspecting. |
paul@345 | 523 | |
paul@345 | 524 | for attrname in self.names_always_used: |
paul@345 | 525 | for objname in objtable.all_possible_objects([attrname]): |
paul@345 | 526 | |
paul@345 | 527 | # Record attributes of objects for potential visiting. |
paul@345 | 528 | |
paul@345 | 529 | self.add_attribute_to_visit(objname, attrname) |
paul@345 | 530 | |
paul@367 | 531 | # Visit all modules, since some may employ initialisation code which has |
paul@367 | 532 | # some kind of side-effect. |
paul@283 | 533 | |
paul@367 | 534 | for name in self.modules.keys(): |
paul@367 | 535 | self._collect_attributes(name, objtable) |
paul@283 | 536 | |
paul@345 | 537 | def add_attribute_to_visit(self, objname, attrname): |
paul@351 | 538 | |
paul@351 | 539 | """ |
paul@351 | 540 | Queue an attribute of the object with the given 'objname', having the |
paul@351 | 541 | given 'attrname', to the list for potential visiting if the specified |
paul@351 | 542 | object is actually referenced. |
paul@351 | 543 | """ |
paul@351 | 544 | |
paul@345 | 545 | if not self.attributes_to_visit.has_key(objname): |
paul@345 | 546 | self.attributes_to_visit[objname] = set() |
paul@345 | 547 | self.attributes_to_visit[objname].add(attrname) |
paul@280 | 548 | |
paul@403 | 549 | def _collect_attributes_from(self, from_name, objname, attrname, objtable): |
paul@403 | 550 | |
paul@403 | 551 | """ |
paul@403 | 552 | Record the association between 'from_name' and the attribute of |
paul@403 | 553 | 'objname' with the given 'attrname'. Then collect attributes for the |
paul@403 | 554 | referenced attribute using 'objtable'. |
paul@403 | 555 | """ |
paul@403 | 556 | |
paul@403 | 557 | if not self.inferred_name_references.has_key(from_name): |
paul@403 | 558 | self.inferred_name_references[from_name] = set() |
paul@403 | 559 | |
paul@403 | 560 | self.inferred_name_references[from_name].add((objname, attrname)) |
paul@403 | 561 | self._collect_attributes(objname + "." + attrname, objtable) |
paul@403 | 562 | |
paul@280 | 563 | def _collect_attributes(self, from_name, objtable): |
paul@250 | 564 | |
paul@280 | 565 | """ |
paul@280 | 566 | Given an object called 'from_name', find all names referenced from such |
paul@403 | 567 | an object according to the register of names, using 'objtable' to infer |
paul@403 | 568 | types. |
paul@280 | 569 | """ |
paul@280 | 570 | |
paul@289 | 571 | if from_name in self.attribute_users_visited: |
paul@280 | 572 | return |
paul@280 | 573 | |
paul@301 | 574 | self.attribute_users_visited.add(from_name) |
paul@301 | 575 | |
paul@281 | 576 | # Get name references and find possible objects which support such |
paul@281 | 577 | # combinations of attribute names. |
paul@281 | 578 | |
paul@382 | 579 | for user, name, usage in self.name_references.get(from_name, []): |
paul@381 | 580 | |
paul@381 | 581 | # Using all attribute names for a particular name, attempt to get |
paul@381 | 582 | # specific object types. |
paul@381 | 583 | |
paul@382 | 584 | all_objtypes = set() |
paul@382 | 585 | |
paul@382 | 586 | for attrnames in usage: |
paul@382 | 587 | objtypes = objtable.all_possible_objects_plus_status(attrnames) |
paul@381 | 588 | if not objtypes: |
paul@384 | 589 | print "Warning: usage in %r for %r finds no object supporting all attributes %r" % (from_name, name, attrnames) |
paul@382 | 590 | objtypes = objtable.any_possible_objects_plus_status(attrnames) |
paul@382 | 591 | if not objtypes: |
paul@384 | 592 | print "Warning: usage in %r for %r finds no object supporting any attributes %r" % (from_name, name, attrnames) |
paul@382 | 593 | |
paul@382 | 594 | all_objtypes.update(objtypes) |
paul@381 | 595 | |
paul@381 | 596 | # Record the object types for generating guards. |
paul@381 | 597 | |
paul@381 | 598 | if user is not None: |
paul@381 | 599 | if not hasattr(user, "_attrtypes"): |
paul@381 | 600 | user._attrtypes = {} |
paul@381 | 601 | |
paul@382 | 602 | user._attrtypes[name] = all_objtypes |
paul@316 | 603 | |
paul@382 | 604 | # For each suggested object type, consider each attribute given by |
paul@382 | 605 | # the names. |
paul@345 | 606 | |
paul@382 | 607 | for objname, is_static in all_objtypes: |
paul@382 | 608 | for attrnames in usage: |
paul@404 | 609 | for attrname, attrvalues in attrnames.items(): |
paul@404 | 610 | |
paul@404 | 611 | # Test for the presence of an attribute on the suggested |
paul@404 | 612 | # object type. |
paul@404 | 613 | |
paul@404 | 614 | try: |
paul@404 | 615 | attr = objtable.access(objname, attrname) |
paul@404 | 616 | except TableError: |
paul@404 | 617 | #print "Warning: object type %r does not support attribute %r" % (objname, attrname) |
paul@404 | 618 | continue |
paul@404 | 619 | |
paul@406 | 620 | # Get the real identity of the attribute in order to |
paul@406 | 621 | # properly collect usage from it. |
paul@406 | 622 | |
paul@406 | 623 | parent = attr.parent |
paul@406 | 624 | if isinstance(parent, micropython.data.Instance): |
paul@406 | 625 | parentname = objname |
paul@406 | 626 | else: |
paul@406 | 627 | parentname = parent.full_name() |
paul@406 | 628 | |
paul@404 | 629 | # Test for assignment. |
paul@404 | 630 | |
paul@404 | 631 | if attrvalues: |
paul@404 | 632 | for attrvalue in attrvalues: |
paul@404 | 633 | parent.set(attrname, attrvalue, 0) |
paul@345 | 634 | |
paul@382 | 635 | # Visit attributes of objects known to be used. |
paul@345 | 636 | |
paul@406 | 637 | if parentname in self.attributes_used: |
paul@406 | 638 | self.use_attribute(parentname, attrname) |
paul@406 | 639 | self._collect_attributes_from(from_name, parentname, attrname, objtable) |
paul@345 | 640 | |
paul@382 | 641 | # Record attributes of other objects for potential visiting. |
paul@345 | 642 | |
paul@382 | 643 | else: |
paul@406 | 644 | self.add_attribute_to_visit(parentname, attrname) |
paul@175 | 645 | |
paul@281 | 646 | # Get specific name references and visit the referenced objects. |
paul@281 | 647 | |
paul@285 | 648 | for objname, attrname in self.specific_name_references.get(from_name, []): |
paul@285 | 649 | self.use_attribute(objname, attrname) |
paul@403 | 650 | self._collect_attributes_from(from_name, objname, attrname, objtable) |
paul@281 | 651 | |
paul@332 | 652 | # Where the object has an __init__ attribute, assume that it is an |
paul@332 | 653 | # initialiser which is called at some point, and collect attributes used |
paul@332 | 654 | # in this initialiser. |
paul@332 | 655 | |
paul@332 | 656 | if "__init__" in objtable.table.get(from_name, []): |
paul@338 | 657 | self.use_attribute(from_name, "__init__") |
paul@403 | 658 | self._collect_attributes_from(from_name, from_name, "__init__", objtable) |
paul@332 | 659 | |
paul@384 | 660 | # Visit attributes on this object that were queued in case of the object |
paul@384 | 661 | # being referenced. |
paul@345 | 662 | |
paul@345 | 663 | attributes_to_visit = self.attributes_to_visit.get(from_name, []) |
paul@345 | 664 | |
paul@345 | 665 | if attributes_to_visit: |
paul@345 | 666 | del self.attributes_to_visit[from_name] |
paul@345 | 667 | |
paul@345 | 668 | for attrname in attributes_to_visit: |
paul@345 | 669 | self.use_attribute(from_name, attrname) |
paul@403 | 670 | self._collect_attributes_from(from_name, from_name, attrname, objtable) |
paul@345 | 671 | |
paul@175 | 672 | # Constant accounting. |
paul@175 | 673 | |
paul@175 | 674 | def init_predefined_constants(self): |
paul@175 | 675 | |
paul@175 | 676 | "Ensure the predefined constants." |
paul@175 | 677 | |
paul@175 | 678 | for name, value in self.predefined_constants.items(): |
paul@175 | 679 | self.make_constant(value) |
paul@175 | 680 | |
paul@175 | 681 | def get_predefined_constant(self, name): |
paul@175 | 682 | |
paul@175 | 683 | "Return the predefined constant for the given 'name'." |
paul@175 | 684 | |
paul@175 | 685 | return self.make_constant(self.predefined_constants[name]) |
paul@175 | 686 | |
paul@175 | 687 | def get_constant(self, value): |
paul@175 | 688 | |
paul@175 | 689 | "Return a constant for the given 'value'." |
paul@175 | 690 | |
paul@176 | 691 | const = micropython.data.Const(value) |
paul@176 | 692 | return self.constant_values[const] |
paul@175 | 693 | |
paul@252 | 694 | def get_constant_type_name(self, value): |
paul@252 | 695 | return value.__class__.__name__ |
paul@252 | 696 | |
paul@175 | 697 | def make_constant(self, value): |
paul@175 | 698 | |
paul@175 | 699 | "Make and return a constant for the given 'value'." |
paul@175 | 700 | |
paul@219 | 701 | # Ensure the presence of the constant's type. |
paul@219 | 702 | |
paul@252 | 703 | name = self.get_constant_type_name(value) |
paul@219 | 704 | if self.modules.has_key("__builtins__"): |
paul@219 | 705 | attr = self.modules["__builtins__"].get(name) |
paul@219 | 706 | |
paul@219 | 707 | # Make a constant object and return it. |
paul@219 | 708 | |
paul@176 | 709 | const = micropython.data.Const(value) |
paul@176 | 710 | if not self.constant_values.has_key(const): |
paul@176 | 711 | self.constant_values[const] = const |
paul@176 | 712 | return self.constant_values[const] |
paul@175 | 713 | |
paul@175 | 714 | def constants(self): |
paul@175 | 715 | |
paul@175 | 716 | "Return a list of constants." |
paul@175 | 717 | |
paul@175 | 718 | if self.constant_list is None: |
paul@175 | 719 | self.constant_list = list(self.constant_values.values()) |
paul@175 | 720 | |
paul@175 | 721 | return self.constant_list |
paul@175 | 722 | |
paul@9 | 723 | # Import methods. |
paul@9 | 724 | |
paul@0 | 725 | def find_in_path(self, name): |
paul@0 | 726 | |
paul@0 | 727 | """ |
paul@0 | 728 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 729 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 730 | otherwise. |
paul@0 | 731 | """ |
paul@0 | 732 | |
paul@0 | 733 | for d in self.path: |
paul@0 | 734 | m = self.find(d, name) |
paul@0 | 735 | if m: return m |
paul@0 | 736 | return None |
paul@0 | 737 | |
paul@0 | 738 | def find(self, d, name): |
paul@0 | 739 | |
paul@0 | 740 | """ |
paul@0 | 741 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 742 | either refer to a single file module or to a package. Return None if the |
paul@0 | 743 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 744 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 745 | """ |
paul@0 | 746 | |
paul@0 | 747 | m = self._find_package(d, name) |
paul@0 | 748 | if m: return m |
paul@0 | 749 | m = self._find_module(d, name) |
paul@0 | 750 | if m: return m |
paul@0 | 751 | return None |
paul@0 | 752 | |
paul@0 | 753 | def _find_module(self, d, name): |
paul@0 | 754 | |
paul@0 | 755 | """ |
paul@0 | 756 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 757 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 758 | None (indicating that no package directory is involved) and a filename |
paul@0 | 759 | indicating the location of the module. |
paul@0 | 760 | """ |
paul@0 | 761 | |
paul@0 | 762 | name_py = name + os.extsep + "py" |
paul@0 | 763 | filename = self._find_file(d, name_py) |
paul@0 | 764 | if filename: |
paul@0 | 765 | return None, filename |
paul@0 | 766 | return None |
paul@0 | 767 | |
paul@0 | 768 | def _find_package(self, d, name): |
paul@0 | 769 | |
paul@0 | 770 | """ |
paul@0 | 771 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 772 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 773 | a directory (indicating the location of the package directory itself) |
paul@0 | 774 | and a filename indicating the location of the __init__.py module which |
paul@0 | 775 | declares the package's top-level contents. |
paul@0 | 776 | """ |
paul@0 | 777 | |
paul@0 | 778 | filename = self._find_file(d, name) |
paul@0 | 779 | if filename: |
paul@0 | 780 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 781 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 782 | if init_py_filename: |
paul@0 | 783 | return filename, init_py_filename |
paul@0 | 784 | return None |
paul@0 | 785 | |
paul@0 | 786 | def _find_file(self, d, filename): |
paul@0 | 787 | |
paul@0 | 788 | """ |
paul@0 | 789 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 790 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 791 | """ |
paul@0 | 792 | |
paul@0 | 793 | filename = os.path.join(d, filename) |
paul@0 | 794 | if os.path.exists(filename): |
paul@0 | 795 | return filename |
paul@0 | 796 | else: |
paul@0 | 797 | return None |
paul@0 | 798 | |
paul@0 | 799 | def load(self, name, return_leaf=0): |
paul@0 | 800 | |
paul@0 | 801 | """ |
paul@0 | 802 | Load the module or package with the given 'name'. Return an object |
paul@0 | 803 | referencing the loaded module or package, or None if no such module or |
paul@0 | 804 | package exists. |
paul@0 | 805 | """ |
paul@0 | 806 | |
paul@363 | 807 | if return_leaf: |
paul@363 | 808 | name_for_return = name |
paul@363 | 809 | else: |
paul@363 | 810 | name_for_return = name.split(".")[0] |
paul@363 | 811 | |
paul@4 | 812 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@4 | 813 | #print "Cached (%s)" % name |
paul@363 | 814 | return self.modules[name_for_return] |
paul@363 | 815 | |
paul@35 | 816 | if self.verbose: |
paul@35 | 817 | print "Loading", name |
paul@0 | 818 | |
paul@0 | 819 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 820 | # the search path. |
paul@0 | 821 | |
paul@0 | 822 | path = name.split(".") |
paul@0 | 823 | m = self.find_in_path(path[0]) |
paul@0 | 824 | if not m: |
paul@35 | 825 | if self.verbose: |
paul@35 | 826 | print "Not found (%s)" % path[0] |
paul@0 | 827 | return None # NOTE: Import error. |
paul@0 | 828 | d, filename = m |
paul@0 | 829 | |
paul@0 | 830 | # Either acquire a reference to an already-imported module, or load the |
paul@0 | 831 | # module from a file. |
paul@0 | 832 | |
paul@2 | 833 | top = module = self.load_from_file(filename, path[0]) |
paul@0 | 834 | |
paul@0 | 835 | # For hierarchical names, traverse each path component... |
paul@0 | 836 | |
paul@0 | 837 | if len(path) > 1: |
paul@0 | 838 | if not d: |
paul@35 | 839 | if self.verbose: |
paul@35 | 840 | print "No package (%s)" % filename |
paul@0 | 841 | return None # NOTE: Import error (package not found). |
paul@1 | 842 | else: |
paul@1 | 843 | self.add_submodules(d, module) |
paul@0 | 844 | |
paul@0 | 845 | path_so_far = path[:1] |
paul@0 | 846 | for p in path[1:]: |
paul@0 | 847 | path_so_far.append(p) |
paul@0 | 848 | |
paul@0 | 849 | # Find the package or module concerned. |
paul@0 | 850 | |
paul@0 | 851 | m = self.find(d, p) |
paul@0 | 852 | if not m: |
paul@35 | 853 | if self.verbose: |
paul@35 | 854 | print "Not found (%s)" % p |
paul@0 | 855 | return None # NOTE: Import error. |
paul@0 | 856 | d, filename = m |
paul@0 | 857 | module_name = ".".join(path_so_far) |
paul@0 | 858 | |
paul@0 | 859 | # Either reference an imported module or load one from a file. |
paul@0 | 860 | |
paul@2 | 861 | submodule = self.load_from_file(filename, module_name) |
paul@0 | 862 | |
paul@1 | 863 | if d: |
paul@1 | 864 | self.add_submodules(d, module) |
paul@1 | 865 | |
paul@0 | 866 | # Store the submodule within its parent module. |
paul@0 | 867 | |
paul@32 | 868 | module.set_module(p, submodule) |
paul@0 | 869 | module = submodule |
paul@0 | 870 | |
paul@0 | 871 | # Return either the deepest or the uppermost module. |
paul@0 | 872 | |
paul@0 | 873 | if return_leaf: |
paul@0 | 874 | return module |
paul@0 | 875 | else: |
paul@0 | 876 | return top |
paul@0 | 877 | |
paul@0 | 878 | def load_from_file(self, name, module_name=None): |
paul@0 | 879 | |
paul@0 | 880 | """ |
paul@0 | 881 | Load the module with the given 'name' (which may be a full module path). |
paul@0 | 882 | """ |
paul@0 | 883 | |
paul@0 | 884 | if module_name is None: |
paul@0 | 885 | module_name = "__main__" |
paul@0 | 886 | |
paul@1 | 887 | module = self.add_module(module_name) |
paul@2 | 888 | if not module.loaded and module not in self.loading: |
paul@2 | 889 | self.loading.add(module) |
paul@4 | 890 | #print "Parsing", name |
paul@2 | 891 | module.parse(name) |
paul@4 | 892 | #print "Done", name |
paul@2 | 893 | self.loading.remove(module) |
paul@2 | 894 | module.loaded = 1 |
paul@0 | 895 | |
paul@0 | 896 | # Record the module. |
paul@0 | 897 | |
paul@345 | 898 | self.use_object(module.full_name()) |
paul@4 | 899 | #print "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 900 | return module |
paul@0 | 901 | |
paul@1 | 902 | def add_module(self, module_name): |
paul@1 | 903 | |
paul@1 | 904 | """ |
paul@1 | 905 | Return the module with the given 'module_name', adding a new module |
paul@1 | 906 | object if one does not already exist. |
paul@1 | 907 | """ |
paul@1 | 908 | |
paul@1 | 909 | if not self.modules.has_key(module_name): |
paul@15 | 910 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@128 | 911 | self.modules_ordered.append(module) |
paul@1 | 912 | else: |
paul@1 | 913 | module = self.modules[module_name] |
paul@1 | 914 | return module |
paul@1 | 915 | |
paul@1 | 916 | def add_submodules(self, pathname, module): |
paul@1 | 917 | |
paul@1 | 918 | """ |
paul@1 | 919 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 920 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 921 | """ |
paul@1 | 922 | |
paul@1 | 923 | for filename in os.listdir(pathname): |
paul@50 | 924 | submodule, ext = os.path.splitext(filename) |
paul@50 | 925 | if ext not in ("", ".py"): |
paul@50 | 926 | continue |
paul@32 | 927 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 928 | |
paul@0 | 929 | # vim: tabstop=4 expandtab shiftwidth=4 |