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 <[email protected]>
* 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.
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