1# =============================================================================
2#               ____ _   _ ____  _       _ _ _           _         _ _
3#  _ __  _   _ / ___| | | |  _ \| |     | (_) |__   __ _| |__   __| | |
4# | '_ \| | | | |  _| |_| | | | | |     | | | '_ \ / _` | '_ \ / _` | |
5# | |_) | |_| | |_| |  _  | |_| | |___ _| | | |_) | (_| | | | | (_| | |
6# | .__/ \__, |\____|_| |_|____/|_____(_)_|_|_.__/ \__, |_| |_|\__,_|_|
7# |_|    |___/                                     |___/
8# =============================================================================
9#  Authors:
10#    Tristan Gingold
11#    Patrick Lehmann
12#
13# Package module:   Python binding and low-level API for shared library 'libghdl'.
14#
15# License:
16# ============================================================================
17#  Copyright (C) 2019-2021 Tristan Gingold
18#
19#  This program is free software: you can redistribute it and/or modify
20#  it under the terms of the GNU General Public License as published by
21#  the Free Software Foundation, either version 2 of the License, or
22#  (at your option) any later version.
23#
24#  This program is distributed in the hope that it will be useful,
25#  but WITHOUT ANY WARRANTY; without even the implied warranty of
26#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27#  GNU General Public License for more details.
28#
29#  You should have received a copy of the GNU General Public License
30#  along with this program.  If not, see <gnu.org/licenses>.
31#
32# SPDX-License-Identifier: GPL-2.0-or-later
33# ============================================================================
34
35from ctypes import c_int32
36
37from pydecor import export
38
39from pyGHDL.libghdl import libghdl
40
41__all__ = [
42	'Library_Location',
43	'Work_Library'
44]
45
46from pyGHDL.libghdl._types import NameId
47
48
49Library_Location = c_int32.in_dll(libghdl, "libraries__library_location")   #: A location for library declarations (such as library WORK). Type ``Location_Type``. Use ``.value`` to access this variable inside libghdl
50Work_Library = c_int32.in_dll(libghdl, "libraries__work_library")           #: Library declaration for the work library. Note: the identifier of the work_library is ``work_library_name``, which may be different from 'WORK'. Type: ``Iir_Library_Declaration``. Use ``.value`` to access this variable inside libghdl
51
52
53@export
54def Get_Libraries_Chain():
55	"""
56	Get the chain of libraries.  Can be used only to read (it mustn't be modified).
57
58	:return: Type ``Iir_Library_Declaration``
59	"""
60	return libghdl.libraries__get_libraries_chain()
61
62
63@export
64def Add_Design_Unit_Into_Library(Unit, Keep_Obsolete: bool = False) -> None:
65	"""
66	Add or replace an design unit in the work library. DECL must not have a chain
67	(because it may be modified).
68
69	If the design_file of UNIT is not already in the library, a new one is created.
70
71	Units are always appended to the design_file. Therefore, the order is kept.
72
73	:param Unit:          Type: ``Iir_Design_Unit``
74	:param Keep_Obsolete: If :obj:`Keep_Obsolete` is True, obsoleted units are
75	                      kept in the library.
76
77	                      This is used when a whole design file has to be added
78	                      in the library and then processed (without that feature,
79	                      redefined units would disappear).
80	"""
81	libghdl.libraries__add_design_unit_into_library(Unit, Keep_Obsolete)
82
83
84@export
85def Purge_Design_File(Design_File) -> None:
86	"""
87	Remove the same file as DESIGN_FILE from work library and all of its units.
88
89	:param Design_File: Type: ``Iir_Design_File``
90	"""
91	libghdl.libraries__purge_design_file(Design_File)
92
93
94@export
95def Find_Entity_For_Component(Name: NameId):
96	"""
97	Find an entity whose name is :obj:`Name` in any library. |br|
98	If there is no such entity, return :attr:`~pyGHDL.libghdl.vhdl.nodes.Null_Iir`. |br|
99	If there are several entities, return :attr:`~pyGHDL.libghdl.vhdl.nodes.Null_Iir`;
100
101	:param Name: Entity name to search for.
102	:return:     Type: ``Iir_Design_Unit``
103	"""
104	return libghdl.libraries__find_entity_for_component(Name)
105
106
107@export
108def Get_Library_No_Create(Ident: NameId):
109	"""
110	Get the library named :obj:`Ident`.
111
112	:param Ident: Libryr to look for.
113	:return:      Return :attr:`~pyGHDL.libghdl.vhdl.nodes.Null_Iir` if it doesn't exist. Type ``Iir_Library_Declaration``
114	"""
115	return libghdl.libraries__get_library_no_create(Ident)
116
117
118@export
119def Find_Primary_Unit(Library, Name: NameId):
120	"""
121	Just return the design_unit for :obj:`Name`, or ``NULL`` if not found.
122
123	:param Library: Library to look in. Type: ``Iir_Library_Declaration``
124	:param Name:    Primary unit to search for.
125	:return:        Type: ``Iir_Design_Unit``
126	"""
127	return libghdl.libraries__find_primary_unit(Library, Name)
128