def declaration_to_location(self, decl): "Convert declaration :param decl: to an LSP Location" decl_loc = nodes.Get_Location(decl) if decl_loc == std_package.Std_Location.value: # There is no real file for the std.standard package. return None if decl_loc == libraries.Library_Location.value: # Libraries declaration are virtual. return None fe = files_map.Location_To_File(decl_loc) doc = self.sfe_to_document(fe) res = {"uri": doc.uri} nid = nodes.Get_Identifier(decl) res["range"] = { "start": symbols.location_to_position(fe, decl_loc), "end": symbols.location_to_position( fe, decl_loc + name_table.Get_Name_Length(nid)), } 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 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