"""Representing the artifacts of a project."""from__future__importannotationsfromitertoolsimportcountimportloggingfromtypingimportTYPE_CHECKING,TypeVarfrom.knowledge_plugins.pluginimportdefault_plugins,KnowledgeBasePluginifTYPE_CHECKING:from.projectimportProjectfrom.knowledge_pluginsimportFunctionManagerfrom.knowledge_pluginsimportVariableManagerfrom.knowledge_pluginsimportKeyDefinitionManagerfrom.knowledge_pluginsimportCFGManagerfrom.knowledge_pluginsimportStructuredCodeManagerfrom.knowledge_pluginsimportTypesStorefrom.knowledge_pluginsimportPropagationManagerfrom.knowledge_pluginsimportXRefManagerl=logging.getLogger(name=__name__)kb_ctr=count(0,1)
[docs]classKnowledgeBase:"""Represents a "model" of knowledge about an artifact. Contains things like a CFG, data references, etc. """functions:FunctionManagervariables:VariableManagerdefs:KeyDefinitionManagercfgs:CFGManager_project:Projecttypes:TypesStorepropagations:PropagationManagerxrefs:XRefManagerdecompilations:StructuredCodeManager
[docs]def__init__(self,project,obj=None,name=None):ifobjisnotNone:l.warning("The obj parameter in KnowledgeBase.__init__() has been deprecated.")object.__setattr__(self,"_project",project)object.__setattr__(self,"_plugins",{})self.name=nameifnameelsef"kb_{next(kb_ctr)}"
[docs]defget_knowledge(self,requested_plugin_cls:type[K])->K|None:""" Type inference safe method to request a knowledge base plugin Explicitly passing the type of the requested plugin achieves two things: 1. Every location using this plugin can be easily found with an IDE by searching explicit references to the type 2. Basic type inference can deduce the result type and properly type check usages of it If there isn't already an instance of this class None will be returned to make it clear to the caller that there is no existing knowledge of this type yet. The code that initially creates this knowledge should use the `register_plugin` method to register the initial knowledge state :param requested_plugin_cls: :return: Instance of the requested plugin class or null if it is not a known plugin """# Get first plugin of this type already registered, or default to Nonereturnnext((pluginforplugininself._plugins.values()ifisinstance(plugin,requested_plugin_cls)),None)