From 1d566ee1a598f2c80b0928c9b29a799590d8822b Mon Sep 17 00:00:00 2001
From: Tristan Gingold <tgingold@free.fr>
Date: Tue, 20 Nov 2018 17:23:32 +0100
Subject: Handle assignment of aggregate to an unbounded variable.

Fix #610
---
 src/vhdl/translate/trans-chap8.adb | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/src/vhdl/translate/trans-chap8.adb b/src/vhdl/translate/trans-chap8.adb
index d6dde2b23..490a104bc 100644
--- a/src/vhdl/translate/trans-chap8.adb
+++ b/src/vhdl/translate/trans-chap8.adb
@@ -988,12 +988,37 @@ package body Trans.Chap8 is
       else
          Targ_Node := Chap6.Translate_Name (Target, Mode_Value);
          if Get_Kind (Expr) = Iir_Kind_Aggregate then
-            declare
-               E : O_Enode;
-            begin
-               E := Chap7.Translate_Expression (Expr, Targ_Type);
-               Chap3.Translate_Object_Copy (Targ_Node, E, Targ_Type);
-            end;
+            if Get_Constraint_State (Get_Type (Expr)) /= Fully_Constrained then
+               declare
+                  Expr_Type : constant Iir := Get_Type (Expr);
+                  Expr_Tinfo : constant Type_Info_Acc := Get_Info (Expr_Type);
+                  Val : Mnode;
+               begin
+                  --  Create a temp.
+                  Val := Create_Temp (Expr_Tinfo);
+                  --  Set bounds from target
+                  Stabilize (Targ_Node);
+                  New_Assign_Stmt
+                    (M2Lp (Chap3.Get_Composite_Bounds (Val)),
+                     M2Addr (Chap3.Get_Composite_Bounds (Targ_Node)));
+                  --  Allocate target
+                  Chap3.Allocate_Unbounded_Composite_Base
+                    (Alloc_Stack, Val, Get_Base_Type (Expr_Type));
+                  --  Translate aggregate
+                  Chap7.Translate_Aggregate (Val, Targ_Type, Expr);
+                  --  Assign
+                  Chap3.Translate_Object_Copy
+                    (Targ_Node, M2Addr (Val), Targ_Type);
+               end;
+            else
+               --  In case of overlap: be sure to use an intermediate variable.
+               declare
+                  E : O_Enode;
+               begin
+                  E := Chap7.Translate_Expression (Expr, Targ_Type);
+                  Chap3.Translate_Object_Copy (Targ_Node, E, Targ_Type);
+               end;
+            end if;
          else
             Chap7.Translate_Assign (Targ_Node, Expr, Targ_Type);
          end if;
-- 
cgit v1.2.3