def test_InitializeGHDL(self) -> None: """Initialization: set options and then load libaries.""" libghdl.initialize() # Print error messages on the console. errorout_console.Install_Handler() # Set options. This must be done before analyze_init() libghdl.set_option("--std=08") # Finish initialization. This will load the standard package. if libghdl.analyze_init_status() != 0: self.fail("libghdl initialization error") # Load the file file_id = name_table.Get_Identifier(str(self._filename)) sfe = files_map.Read_Source_File(name_table.Null_Identifier, file_id) if sfe == files_map.No_Source_File_Entry: self.fail("Cannot read file '{!s}'".format(self._filename)) # Parse file = sem_lib.Load_File(sfe) # Display all design units designUnit = nodes.Get_First_Design_Unit(file) while designUnit != nodes.Null_Iir: libraryUnit = nodes.Get_Library_Unit(designUnit) if nodes.Get_Kind( libraryUnit) == nodes.Iir_Kind.Entity_Declaration: entityName = self.getIdentifier(libraryUnit) self.assertEqual( entityName, "entity_1", "expected entity name 'e1', got '{}'".format(entityName), ) elif nodes.Get_Kind( libraryUnit) == nodes.Iir_Kind.Architecture_Body: architectureName = self.getIdentifier(libraryUnit) self.assertEqual( architectureName, "behav", "expected architecture name 'behav', got '{}'".format( architectureName), ) else: self.fail("Unknown unit.") designUnit = nodes.Get_Chain(designUnit)
def parse(self): unit = nodes.Get_First_Design_Unit(self.__ghdlFile) while unit != nodes.Null_Iir: libraryUnit = nodes.Get_Library_Unit(unit) nodeKind = nodes.Get_Kind(libraryUnit) if (nodeKind == nodes.Iir_Kind.Entity_Declaration): entity = Entity.parse(libraryUnit) self.Entities.append(entity) elif (nodeKind == nodes.Iir_Kind.Architecture_Body): architecture = Architecture.parse(libraryUnit) self.Architectures.append(architecture) elif (nodeKind == nodes.Iir_Kind.Package_Declaration): package = Package.parse(libraryUnit) self.Packages.append(package) elif (nodeKind == nodes.Iir_Kind.Package_Body): packageBody = PackageBody.parse(libraryUnit) self.PackageBodies.append(packageBody) elif (nodeKind == nodes.Iir_Kind.Context_Declaration): context = Context.parse(libraryUnit) self.Contexts.append(context) elif (nodeKind == nodes.Iir_Kind.Configuration_Declaration): configuration = Configuration.parse(libraryUnit) self.Configurations.append(configuration) else: raise GHDLException("Unknown design unit kind.") unit = nodes.Get_Chain(unit)
def concurrent_stmts_iter(n) -> Generator[Any, None, None]: """Iterate concurrent statements in node :obj:`n`.""" k = nodes.Get_Kind(n) if k == nodes.Iir_Kind.Design_File: for n1 in chain_iter(nodes.Get_First_Design_Unit(n)): for n2 in concurrent_stmts_iter(n1): yield n2 elif k == nodes.Iir_Kind.Design_Unit: for n1 in concurrent_stmts_iter(nodes.Get_Library_Unit(n)): yield n1 elif k in ( nodes.Iir_Kind.Entity_Declaration, nodes.Iir_Kind.Architecture_Body, nodes.Iir_Kind.Block_Statement, ): for n1 in chain_iter(nodes.Get_Concurrent_Statement_Chain(n)): yield n1 for n2 in concurrent_stmts_iter(n1): yield n2 elif k == nodes.Iir_Kind.For_Generate_Statement: for n1 in concurrent_stmts_iter(nodes.Get_Generate_Statement_Body(n)): yield n1 elif k == nodes.Iir_Kind.If_Generate_Statement: while n != nodes.Null_Iir: for n1 in concurrent_stmts_iter(nodes.Get_Generate_Statement_Body(n)): yield n1 n = nodes.Get_Generate_Else_Clause(n) elif k == nodes.Iir_Kind.Case_Generate_Statement: alt = nodes.Get_Case_Statement_Alternative_Chain(n) for n1 in chain_iter(alt): blk = nodes.Get_Associated_Block(n1) if blk != nodes.Null_Iir: for n2 in concurrent_stmts_iter(nodes.Get_Generate_Statement_Body(n)): yield n2
def GetIirKindOfNode(node: Iir) -> nodes.Iir_Kind: """Return the kind of a node in the IIR tree.""" if node == Null_Iir: raise ValueError("GetIirKindOfNode: Parameter 'node' must not be 'Null_iir'.") kind: int = nodes.Get_Kind(node) return nodes.Iir_Kind(kind)
def compute_anti_dependences(self): """Return a dictionnary of anti dependencies for design unit""" res = {} lib = libraries.Get_Libraries_Chain() while lib != nodes.Null_Iir: files = nodes.Get_Design_File_Chain(lib) while files != nodes.Null_Iir: units = nodes.Get_First_Design_Unit(files) while units != nodes.Null_Iir: if nodes.Get_Date_State(units) == nodes.Date_State.Analyze: # The unit has been analyzed, so the dependencies are know. deps = nodes.Get_Dependence_List(units) assert deps != nodes.Null_Iir_List deps_it = lists.Iterate(deps) while lists.Is_Valid(byref(deps_it)): el = lists.Get_Element(byref(deps_it)) if nodes.Get_Kind( el) == nodes.Iir_Kind.Design_Unit: if res.get(el, None): res[el].append(units) else: res[el] = [units] else: assert False lists.Next(byref(deps_it)) units = nodes.Get_Chain(units) files = nodes.Get_Chain(files) lib = nodes.Get_Chain(lib) return res
def fields_iter(n) -> Generator[Any, None, None]: """Iterate on fields of node :obj:`n`.""" if n == nodes.Null_Iir: return k = nodes.Get_Kind(n) first = nodes_meta.get_fields_first(k) last = nodes_meta.get_fields_last(k) for i in range(first, last + 1): yield nodes_meta.get_field_by_index(i)
def leftest_location(n): while True: if n == nodes.Null_Iir: return files_map.No_Location k = nodes.Get_Kind(n) if k == nodes.Iir_Kind.Array_Subtype_Definition: n = nodes.Get_Subtype_Type_Mark(n) else: return nodes.Get_Location(n)
def sequential_iter(n) -> Generator[Any, None, None]: """ Iterate sequential statements. The first node must be either a process or a subprogram body. """ if n == nodes.Null_Iir: return k = nodes.Get_Kind(n) if k in ( nodes.Iir_Kind.Process_Statement, nodes.Iir_Kind.Sensitized_Process_Statement, nodes.Iir_Kind.Function_Body, nodes.Iir_Kind.Procedure_Body, ): for n1 in chain_iter(nodes.Get_Sequential_Statement_Chain(n)): yield n1 for n2 in sequential_iter(n1): yield n2 elif k == nodes.Iir_Kind.If_Statement: while True: n = nodes.Get_Chain(n) if n == nodes.Null_Iir: break yield n for n1 in sequential_iter(n): yield n1 elif k == nodes.Iir_Kind.Case_Statement: for ch in chain_iter(nodes.Get_Case_Statement_Alternative_Chain(n)): stmt = nodes.Get_Associated_Chain(ch) if stmt != nodes.Null_Iir: for n1 in chain_iter(stmt): yield n1 for n2 in sequential_iter(n1): yield n2 elif k in [ nodes.Iir_Kind.For_Loop_Statement, nodes.Iir_Kind.While_Loop_Statement ]: for n1 in chain_iter(nodes.Get_Sequential_Statement_Chain(n)): yield n1 for n2 in sequential_iter(n1): yield n2 elif k in ( nodes.Iir_Kind.Assertion_Statement, nodes.Iir_Kind.Wait_Statement, nodes.Iir_Kind.Null_Statement, nodes.Iir_Kind.Exit_Statement, nodes.Iir_Kind.Next_Statement, nodes.Iir_Kind.Return_Statement, nodes.Iir_Kind.Variable_Assignment_Statement, nodes.Iir_Kind.Simple_Signal_Assignment_Statement, nodes.Iir_Kind.Procedure_Call_Statement, ): return else: raise Exception("Unknown node of kind {}".format(kind_image(k)))
def goto_definition(self, doc_uri, position): decl = self._docs[doc_uri].goto_definition(position) if decl is None: return None decl_loc = self.declaration_to_location(decl) if decl_loc is None: return None res = [decl_loc] if nodes.Get_Kind(decl) == nodes.Iir_Kind.Component_Declaration: ent = libraries.Find_Entity_For_Component( nodes.Get_Identifier(decl)) if ent != nodes.Null_Iir: res.append( self.declaration_to_location(nodes.Get_Library_Unit(ent))) return res
def obsolete_doc(self, doc): if doc._tree == nodes.Null_Iir: return # Free old tree assert nodes.Get_Kind(doc._tree) == nodes.Iir_Kind.Design_File if self._last_linted_doc == doc: antideps = None else: antideps = self.compute_anti_dependences() unit = nodes.Get_First_Design_Unit(doc._tree) while unit != nodes.Null_Iir: if antideps is not None: self.obsolete_dependent_units(unit, antideps) # FIXME: free unit; it is not referenced. unit = nodes.Get_Chain(unit) libraries.Purge_Design_File(doc._tree) doc._tree = nodes.Null_Iir
def x_get_all_entities(self): res = [] lib = libraries.Get_Libraries_Chain() while lib != nodes.Null_Iir: files = nodes.Get_Design_File_Chain(lib) ents = [] while files != nodes.Null_Iir: units = nodes.Get_First_Design_Unit(files) while units != nodes.Null_Iir: unitlib = nodes.Get_Library_Unit(units) if nodes.Get_Kind( unitlib) == nodes.Iir_Kind.Entity_Declaration: ents.append(unitlib) units = nodes.Get_Chain(units) files = nodes.Get_Chain(files) ents = [pyutils.name_image(nodes.Get_Identifier(e)) for e in ents] lib_name = pyutils.name_image(nodes.Get_Identifier(lib)) res.extend([{"name": n, "library": lib_name} for n in ents]) lib = nodes.Get_Chain(lib) return res
def find_def(n, loc): "Return the node at location :param loc:, or None if not under :param n:" if n == nodes.Null_Iir: return None k = nodes.Get_Kind(n) if k in [ nodes.Iir_Kind.Simple_Name, nodes.Iir_Kind.Character_Literal, nodes.Iir_Kind.Operator_Symbol, nodes.Iir_Kind.Selected_Name, nodes.Iir_Kind.Attribute_Name, nodes.Iir_Kind.Selected_Element, ]: n_loc = nodes.Get_Location(n) if loc >= n_loc: ident = nodes.Get_Identifier(n) id_len = name_table.Get_Name_Length(ident) if loc < n_loc + id_len: return n if k == nodes.Iir_Kind.Simple_Name: return None elif k == nodes.Iir_Kind.Design_File: return find_def_chain(nodes.Get_First_Design_Unit(n), loc) elif k == nodes.Iir_Kind.Design_Unit: # if loc > elocations.Get_End_Location(unit): # return None res = find_def_chain(nodes.Get_Context_Items(n), loc) if res is not None: return res unit = nodes.Get_Library_Unit(n) return find_def(unit, loc) # This is *much* faster than using node_iter! for f in pyutils.fields_iter(n): typ = nodes_meta.get_field_type(f) if typ == nodes_meta.types.Iir: attr = nodes_meta.get_field_attribute(f) if attr == nodes_meta.Attr.ANone: res = find_def(nodes_meta.Get_Iir(n, f), loc) if res is not None: return res elif attr == nodes_meta.Attr.Chain: res = find_def_chain(nodes_meta.Get_Iir(n, f), loc) if res is not None: return res elif attr == nodes_meta.Attr.Maybe_Ref: if not nodes.Get_Is_Ref(n): res = find_def(nodes_meta.Get_Iir(n, f), loc) if res is not None: return res elif typ == nodes_meta.types.Iir_List: attr = nodes_meta.get_field_attribute(f) if attr == nodes_meta.Attr.ANone: for n1 in pyutils.list_iter(nodes_meta.Get_Iir_List(n, f)): res = find_def(n1, loc) if res is not None: return res elif typ == nodes_meta.types.Iir_Flist: attr = nodes_meta.get_field_attribute(f) if attr == nodes_meta.Attr.ANone: for n1 in pyutils.flist_iter(nodes_meta.Get_Iir_Flist(n, f)): res = find_def(n1, loc) if res is not None: return res return None
def constructs_iter(n) -> Generator[Any, None, None]: """ Iterate library units, concurrent statements and declarations that appear directly within a declarative part. """ if n == nodes.Null_Iir: return k = nodes.Get_Kind(n) if k == nodes.Iir_Kind.Design_File: for n1 in chain_iter(nodes.Get_First_Design_Unit(n)): for n2 in constructs_iter(n1): yield n2 elif k == nodes.Iir_Kind.Design_Unit: n1 = nodes.Get_Library_Unit(n) yield n1 for n2 in constructs_iter(n1): yield n2 elif k in ( nodes.Iir_Kind.Entity_Declaration, nodes.Iir_Kind.Architecture_Body, nodes.Iir_Kind.Block_Statement, nodes.Iir_Kind.Generate_Statement_Body, ): for n1 in chain_iter(nodes.Get_Declaration_Chain(n)): yield n1 for n2 in constructs_iter(n1): yield n2 for n1 in chain_iter(nodes.Get_Concurrent_Statement_Chain(n)): yield n1 for n2 in constructs_iter(n1): yield n2 elif k in ( nodes.Iir_Kind.Configuration_Declaration, nodes.Iir_Kind.Package_Declaration, nodes.Iir_Kind.Package_Body, nodes.Iir_Kind.Function_Body, nodes.Iir_Kind.Procedure_Body, nodes.Iir_Kind.Protected_Type_Declaration, nodes.Iir_Kind.Protected_Type_Body, nodes.Iir_Kind.Process_Statement, nodes.Iir_Kind.Sensitized_Process_Statement, ): for n1 in chain_iter(nodes.Get_Declaration_Chain(n)): yield n1 for n2 in constructs_iter(n1): yield n2 elif k == nodes.Iir_Kind.For_Generate_Statement: n1 = nodes.Get_Generate_Statement_Body(n) yield n1 for n2 in constructs_iter(n1): yield n2 elif k == nodes.Iir_Kind.If_Generate_Statement: while n != nodes.Null_Iir: n1 = nodes.Get_Generate_Statement_Body(n) yield n1 for n2 in constructs_iter(n1): yield n2 n = nodes.Get_Generate_Else_Clause(n) elif k == nodes.Iir_Kind.Case_Generate_Statement: alt = nodes.Get_Case_Statement_Alternative_Chain(n) for n1 in chain_iter(alt): blk = nodes.Get_Associated_Block(n1) if blk != nodes.Null_Iir: n2 = nodes.Get_Generate_Statement_Body(blk) yield n2 for n3 in constructs_iter(n2): yield n3
def declarations_iter(n) -> Generator[Any, None, None]: """Iterate all declarations in node :obj:`n`.""" k = nodes.Get_Kind(n) if nodes_meta.Has_Generic_Chain(k): for n1 in chain_iter(nodes.Get_Generic_Chain(n)): yield n1 if nodes_meta.Has_Port_Chain(k): for n1 in chain_iter(nodes.Get_Port_Chain(n)): yield n1 if nodes_meta.Has_Interface_Declaration_Chain(k): for n1 in chain_iter(nodes.Get_Interface_Declaration_Chain(n)): yield n1 if nodes_meta.Has_Declaration_Chain(k): for n1 in chain_iter(nodes.Get_Declaration_Chain(n)): k1 = nodes.Get_Kind(n1) if k1 in nodes.Iir_Kinds.Specification or k1 == nodes.Iir_Kind.Use_Clause: # Not a declaration pass elif k1 == nodes.Iir_Kind.Signal_Attribute_Declaration: # Not a declaration pass elif k1 in ( nodes.Iir_Kind.Type_Declaration, nodes.Iir_Kind.Anonymous_Type_Declaration, ): yield n1 # Handle nested declarations: record elements, physical units, # enumeration literals... typ = nodes.Get_Type_Definition(n1) for n2 in declarations_iter(n1): yield n2 else: yield n1 # There can be nested declarations (subprograms) for n2 in declarations_iter(n1): yield n2 if nodes_meta.Has_Concurrent_Statement_Chain(k): for n1 in chain_iter(nodes.Get_Concurrent_Statement_Chain(n)): for n2 in declarations_iter(n1): yield n2 if nodes_meta.Has_Sequential_Statement_Chain(k): for n1 in chain_iter(nodes.Get_Sequential_Statement_Chain(n)): for n2 in declarations_iter(n1): yield n2 if nodes_meta.Has_Parameter_Specification(k): yield nodes.Get_Parameter_Specification(n) if nodes_meta.Has_Generate_Statement_Body(k): for n1 in declarations_iter(nodes.Get_Generate_Statement_Body(n)): yield n1 if nodes_meta.Has_Else_Clause(k): n1 = nodes.Get_Else_Clause(n) if n1 != nodes.Null_Iir: for n2 in declarations_iter(n1): yield n2 if nodes_meta.Has_Generate_Else_Clause(k): n1 = nodes.Get_Generate_Else_Clause(n) if n1 != nodes.Null_Iir: for n2 in declarations_iter(n1): yield n2 if nodes_meta.Has_Block_Header(k): n1 = nodes.Get_Block_Header(n) if n1 != nodes.Null_Iir: for n2 in declarations_iter(n1): yield n2 # All these nodes are handled: if k in ( nodes.Iir_Kind.Entity_Declaration, nodes.Iir_Kind.Architecture_Body, nodes.Iir_Kind.Package_Declaration, nodes.Iir_Kind.Package_Body, nodes.Iir_Kind.Process_Statement, nodes.Iir_Kind.Sensitized_Process_Statement, nodes.Iir_Kind.Concurrent_Assertion_Statement, nodes.Iir_Kind.Concurrent_Simple_Signal_Assignment, nodes.Iir_Kind.Concurrent_Selected_Signal_Assignment, nodes.Iir_Kind.Concurrent_Conditional_Signal_Assignment, nodes.Iir_Kind.Concurrent_Procedure_Call_Statement, nodes.Iir_Kind.Block_Statement, nodes.Iir_Kind.Block_Header, nodes.Iir_Kind.For_Generate_Statement, nodes.Iir_Kind.If_Generate_Statement, nodes.Iir_Kind.Generate_Statement_Body, nodes.Iir_Kind.Assertion_Statement, nodes.Iir_Kind.Wait_Statement, nodes.Iir_Kind.Simple_Signal_Assignment_Statement, nodes.Iir_Kind.Variable_Assignment_Statement, nodes.Iir_Kind.For_Loop_Statement, nodes.Iir_Kind.While_Loop_Statement, nodes.Iir_Kind.Case_Statement, nodes.Iir_Kind.Null_Statement, nodes.Iir_Kind.Exit_Statement, nodes.Iir_Kind.Next_Statement, nodes.Iir_Kind.Procedure_Call_Statement, nodes.Iir_Kind.Signal_Declaration, nodes.Iir_Kind.Constant_Declaration, nodes.Iir_Kind.Variable_Declaration, nodes.Iir_Kind.File_Declaration, nodes.Iir_Kind.Object_Alias_Declaration, nodes.Iir_Kind.Attribute_Declaration, nodes.Iir_Kind.Component_Declaration, nodes.Iir_Kind.Use_Clause, nodes.Iir_Kind.If_Statement, nodes.Iir_Kind.Elsif, nodes.Iir_Kind.Return_Statement, nodes.Iir_Kind.Type_Declaration, nodes.Iir_Kind.Anonymous_Type_Declaration, nodes.Iir_Kind.Subtype_Declaration, nodes.Iir_Kind.Function_Declaration, nodes.Iir_Kind.Function_Body, nodes.Iir_Kind.Procedure_Declaration, nodes.Iir_Kind.Procedure_Body, nodes.Iir_Kind.Component_Instantiation_Statement, ): return raise Exception("Unknown node of kind {}".format(kind_image(k)))
def get_symbols(fe, n): if n == nodes.Null_Iir: return None k = nodes.Get_Kind(n) if k == nodes.Iir_Kind.Design_Unit: return get_symbols(fe, nodes.Get_Library_Unit(n)) m = SYMBOLS_MAP.get(k, None) if m is None: raise AssertionError("get_symbol: unhandled {}".format( pyutils.kind_image(k))) kind = m["kind"] if kind is None: return None if k in [ nodes.Iir_Kind.Procedure_Declaration, nodes.Iir_Kind.Function_Declaration ]: # Discard implicit declarations. if nodes.Get_Implicit_Definition(n) < nodes.Iir_Predefined.PNone: return None if nodes.Get_Has_Body(n): # Use the body instead. # FIXME: but get interface from the spec! return None res = {"kind": kind} detail = m.get("detail") if detail is not None: res["detail"] = detail # Get the name if k in [nodes.Iir_Kind.Function_Body, nodes.Iir_Kind.Procedure_Body]: nid = nodes.Get_Identifier(nodes.Get_Subprogram_Specification(n)) else: nid = nodes.Get_Identifier(n) if nid == name_table.Null_Identifier: name = None else: name = pyutils.name_image(nid) # Get the range. Use elocations when possible. if k in ( nodes.Iir_Kind.Architecture_Body, nodes.Iir_Kind.Entity_Declaration, nodes.Iir_Kind.Package_Declaration, nodes.Iir_Kind.Package_Body, nodes.Iir_Kind.Component_Declaration, nodes.Iir_Kind.Process_Statement, nodes.Iir_Kind.Sensitized_Process_Statement, nodes.Iir_Kind.If_Generate_Statement, nodes.Iir_Kind.For_Generate_Statement, ): start_loc = elocations.Get_Start_Location(n) end_loc = elocations.Get_End_Location(n) if end_loc == files_map.No_Location: # Can happen in case of parse error end_loc = start_loc else: start_loc = nodes.Get_Location(n) end_loc = start_loc + name_table.Get_Name_Length(nid) res["range"] = { "start": location_to_position(fe, start_loc), "end": location_to_position(fe, end_loc), } # Gather children. # FIXME: should we use a list of fields to inspect ? children = [] # if nodes_meta.Has_Generic_Chain(k): # children.extend(get_symbols_chain(fe, nodes.Get_Generic_Chain(n))) # if nodes_meta.Has_Port_Chain(k): # children.extend(get_symbols_chain(fe, nodes.Get_Port_Chain(n))) # if nodes_meta.Has_Interface_Declaration_Chain(k): # children.extend(get_symbols_chain(fe, nodes.Get_Interface_Declaration_Chain(n))) if k in (nodes.Iir_Kind.Package_Declaration, nodes.Iir_Kind.Package_Body): children.extend(get_symbols_chain(fe, nodes.Get_Declaration_Chain(n))) if nodes_meta.Has_Concurrent_Statement_Chain(k): children.extend( get_symbols_chain(fe, nodes.Get_Concurrent_Statement_Chain(n))) if nodes_meta.Has_Generate_Statement_Body(k): children.extend( get_symbols_chain( fe, nodes.Get_Concurrent_Statement_Chain( nodes.Get_Generate_Statement_Body(n)), )) if children: res["children"] = children else: # Discard anonymous symbols without children. if name is None: return None res["name"] = name if name is not None else "<anon>" return res