Skip to content

Commit

Permalink
vhdl: factorize component_need_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
tgingold committed Jan 13, 2024
1 parent 2567add commit 0fb4259
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
21 changes: 4 additions & 17 deletions src/vhdl/vhdl-sem.adb
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,10 @@ package body Vhdl.Sem is
-- Sem generics.
Sem_Interface_Chain (Generics, Generic_Interface_List);

-- Set macro-expanded flag.
declare
Gen : Iir;
begin
Gen := Generics;
while Gen /= Null_Iir loop
case Get_Kind (Gen) is
when Iir_Kind_Interface_Type_Declaration
| Iir_Kinds_Interface_Subprogram_Declaration =>
Set_Macro_Expand_Flag (Entity, True);
exit;
when others =>
null;
end case;
Gen := Get_Chain (Gen);
end loop;
end;
-- Set macro-expand flag.
if Component_Need_Instance (Entity, False) then
Set_Macro_Expand_Flag (Entity, True);
end if;

-- Sem ports.
Sem_Interface_Chain (Get_Port_Chain (Entity), Port_Interface_List);
Expand Down
4 changes: 4 additions & 0 deletions src/vhdl/vhdl-sem_decls.adb
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,10 @@ package body Vhdl.Sem_Decls is
Close_Declarative_Region;

Name_Visible (Component);

if Component_Need_Instance (Component, False) then
Set_Macro_Expand_Flag (Component, True);
end if;
end Sem_Component_Declaration;

procedure Sem_Object_Alias_Declaration (Alias: Iir_Object_Alias_Declaration)
Expand Down
24 changes: 1 addition & 23 deletions src/vhdl/vhdl-sem_stmts.adb
Original file line number Diff line number Diff line change
Expand Up @@ -2191,28 +2191,6 @@ package body Vhdl.Sem_Stmts is
end if;
end Sem_Instantiated_Unit;

-- If a component or an entity contains an interface package or type,
-- the ports have to be instantiated in the case they use a type of the
-- interface.
function Component_Need_Instance (Comp : Iir) return Boolean
is
Inter : Iir;
begin
Inter := Get_Generic_Chain (Comp);
while Inter /= Null_Iir loop
case Get_Kind (Inter) is
when Iir_Kind_Interface_Package_Declaration
| Iir_Kind_Interface_Type_Declaration =>
return True;
when others =>
null;
end case;
Inter := Get_Chain (Inter);
end loop;

return False;
end Component_Need_Instance;

-- Change the formal so that it refers to the original interface.
procedure Reassoc_Association_Chain (Chain : Iir)
is
Expand Down Expand Up @@ -2270,7 +2248,7 @@ package body Vhdl.Sem_Stmts is

-- The associations
Sem_Generic_Association_Chain (Decl, Stmt);
if Component_Need_Instance (Decl) then
if Component_Need_Instance (Decl, True) then
Decl_Inst := Sem_Inst.Instantiate_Component_Declaration (Decl, Stmt);
Set_Instantiated_Header (Stmt, Decl_Inst);
Sem_Port_Association_Chain (Decl_Inst, Stmt);
Expand Down
33 changes: 33 additions & 0 deletions src/vhdl/vhdl-utils.adb
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,39 @@ package body Vhdl.Utils is
end case;
end Is_Entity_Instantiation;

function Component_Need_Instance (Comp : Iir; For_Sem : Boolean)
return Boolean
is
Inter : Iir;
begin
Inter := Get_Generic_Chain (Comp);
while Inter /= Null_Iir loop
case Get_Kind (Inter) is
when Iir_Kind_Interface_Package_Declaration =>
if For_Sem then
return True;
end if;
declare
Pkg : constant Iir :=
Get_Uninstantiated_Package_Decl (Inter);
begin
if not Is_Error (Pkg) and then Get_Macro_Expand_Flag (Pkg)
then
return True;
end if;
end;
when Iir_Kinds_Interface_Subprogram_Declaration
| Iir_Kind_Interface_Type_Declaration =>
return True;
when others =>
null;
end case;
Inter := Get_Chain (Inter);
end loop;

return False;
end Component_Need_Instance;

function Get_Attribute_Name_Expression (Name : Iir) return Iir
is
Attr_Val : constant Iir := Get_Named_Entity (Name);
Expand Down
9 changes: 9 additions & 0 deletions src/vhdl/vhdl-utils.ads
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ package Vhdl.Utils is
function Is_Entity_Instantiation
(Inst : Iir_Component_Instantiation_Statement) return Boolean;

-- If a component or an entity contains an interface package or type,
-- the ports have to be instantiated in the case they use a type of the
-- interface.
-- if FOR_SEM is true, it will return True if there is an interface
-- package. This is needed to correctly match object subtypes.
-- This is used to set macro_expand_flag.
function Component_Need_Instance (Comp : Iir; For_Sem : Boolean)
return Boolean;

-- Get the expression of the attribute specification corresponding to the
-- attribute name NAME. Meaningful only for static values.
function Get_Attribute_Name_Expression (Name : Iir) return Iir;
Expand Down

0 comments on commit 0fb4259

Please sign in to comment.