This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Comparisons on scalar subtypes with a single value and validity checks


If the subtype of a scalar object has a single value, certain comparisons
can be handled at compile time even if an operand in the comparison is a
variable. However, this static evaluation is only justified if the operand is
known to have a valid value.

Executing the following program must print:
---
TRUE
FALSE
FALSE
---
with Ada.Text_IO;

procedure My_Test is

   type My_Enum is (A, B ,C);
   for My_Enum use (A => 1, B => 2, C => 3);

   subtype My_Sub_Enum is My_Enum range A .. A;

   ----------------------

   Foo : My_Sub_Enum;

   Foo_Overlay : My_Enum;
   for Foo_Overlay'Address use Foo'Address;

begin

   Foo_Overlay := A;
   Ada.Text_IO.Put_Line (Boolean'Image (Foo'Valid));

   Foo_Overlay := B;
   Ada.Text_IO.Put_Line (Boolean'Image (Foo'Valid));

   Foo_Overlay := C;
   Ada.Text_IO.Put_Line (Boolean'Image (Foo'Valid));

end My_Test;

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

2009-07-23  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch3.adb (Analyze_Object_Declaration): A scalar constant with a
	static expression is known valid.
	* sem_eval.adb (Compile_Time_Compare): Handle properly non-static
	operands of a subtype with a single value.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]