aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/python/units01/show_units.py
blob: 9207bde8cf841bd0a345771ec55247f688cefcdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import libghdl
from libghdl.thin import name_table
from libghdl.thin import files_map
from libghdl.thin.vhdl import nodes
from libghdl.thin.vhdl import sem_lib
from libghdl.thin import errorout_console


def init():
    """Initialization: set options and then load libaries"""
    # Print error messages on the console
    errorout_console.Install_Handler()
    # Set options.  This must be done before analyze_init()
    libghdl.set_option(b"--std=08")
    # Finish initialization. This will load the standard package
    libghdl.analyze_init()


def get_identifier_ptr(n):
    """Return the python string from node :param n: identifier"""
    return name_table.Get_Name_Ptr(nodes.Get_Identifier(n)).decode("utf-8")


def list_units(filename):
    # Load the file
    file_id = name_table.Get_Identifier(filename.encode("utf_8"))
    sfe = files_map.Read_Source_File(name_table.Null_Identifier, file_id)
    if sfe == files_map.No_Source_File_Entry:
        print("cannot open file '%s'" % filename)
        return

    # Parse
    file = sem_lib.Load_File(sfe)

    # Display all design units
    unit = nodes.Get_First_Design_Unit(file)
    while unit != nodes.Null_Iir:
        lib_unit = nodes.Get_Library_Unit(unit)
        if nodes.Get_Kind(lib_unit) == nodes.Iir_Kind.Entity_Declaration:
            print("entity %s" % get_identifier_ptr(lib_unit))
        elif nodes.Get_Kind(lib_unit) == nodes.Iir_Kind.Architecture_Body:
            print("architecture %s" % get_identifier_ptr(lib_unit))
        else:
            print("unknown unit!")
        unit = nodes.Get_Chain(unit)


def main():
    init()
    list_units("demo.vhdl")


if __name__ == "__main__":
    main()