Patchwork [Ada] The actual type of a constant discriminated object

login
register
mail settings
Submitter Arnaud Charlet
Date Aug. 3, 2011, 7:52 a.m.
Message ID <20110803075212.GA11358@adacore.com>
Download mbox | patch
Permalink /patch/108034/
State New
Headers show

Comments

Arnaud Charlet - Aug. 3, 2011, 7:52 a.m.
If a constant object of an unconstrained type with discriminants is initialzed
with an aggregate, the constrained subtype of the aggregate can be used  as the
type of the object, because such an object is immutable. If all components of
the aggregate are static, this allows the back-end to generate no code for the
object declaration.

Tested on x86_64-pc-linux-gnu, committed on trunk

2011-08-03  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch3.adb (Analyze_Object_Declaration): if a constant object of an
	unconstrained type with discriminants is initialized with an aggregate,
	use the constrained subtype of the aggregate as the type of the object,
	because it is immutable, and this allows the back-end to generate no
	code for the object declaration.

Patch

Index: sem_ch3.adb
===================================================================
--- sem_ch3.adb	(revision 177187)
+++ sem_ch3.adb	(working copy)
@@ -3401,6 +3401,18 @@ 
          --  It is unclear why this should make it acceptable to gcc. ???
 
          Remove_Side_Effects (E);
+
+      elsif not Is_Constrained (T)
+        and then Has_Discriminants (T)
+        and then Constant_Present (N)
+        and then not Has_Unchecked_Union (T)
+        and then Nkind (E) = N_Aggregate
+      then
+         --  If this is a constant declaration of an unconstrained type and
+         --  the initialization is an aggregate, we can use the subtype of the
+         --  aggregate for the declared entity because it is immutable.
+
+         Act_T := Etype (E);
       end if;
 
       --  Check No_Wide_Characters restriction