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] Fix malfunction of ASIS tools for predicated choices


The compiler was expanding choices in variant parts and case
statements and expressions prematurely, so that the ASIS tree
had these expansions, causing malfunction of ASIS tools (notably
gnatpp, the pretty printer) to malfunction. This patch moves
the expansion to the expander where it belongs so that it only
occurs if code is being generated.

If the following program is compiled with -gnatct -gnatG:

     1. procedure Predicate_ASIS is
     2.    type Color is
     3.      (Red, Orange, Yellow, Green, Blue, Indigo, Violet);
     4.
     5.    subtype S1 is Color with
     6.      Predicate => S1 in Orange .. Yellow;
     7.
     8.    subtype S2 is Color with
     9.      Predicate => S2 in Blue .. Blue;
    10.
    11.    subtype Other is Color with
    12.      Predicate => Other not in S1 | S2;
    13.
    14.    X : Color := Red;
    15.
    16. begin
    17.    case X is
    18.       when Other => null;
    19.       when S1 | S2 => null;
    20.    end case;
    21. end Predicate_ASIS;

then the -gnatG output after the begin is:

   begin
      case x is
         when other =>
            null;
         when s1 | s2 =>
            null;
      end case;
   end predicate_asis;

And the "other" choice is not expanded.

The following makes sure we are still properly diagnosing missing
cases etc in the -gnatct case:

     1. procedure Predicate_ASIS2 is
     2.    type Color is
     3.      (Red, Orange, Yellow, Green, Blue, Indigo, Violet);
     4.
     5.    subtype S1 is Color with
     6.      Predicate => S1 in Orange .. Yellow;
     7.
     8.    subtype S2 is Color with
     9.      Predicate => S2 in Blue .. Blue;
    10.
    11.    subtype Other is Color with
    12.      Predicate => Other not in S1 | S2;
    13.
    14.    X : Color := Red;
    15.
    16. begin
    17.    case X is
           |
        >>> missing case values: "Orange" .. "Yellow"

    18.       when Other => null;
    19.       when S2    => null;
    20.    end case;
    21. end Predicate_ASIS2;

Finally, this test makes sure the code generator is handling the case
of a static predicate correctly, the following is compiled without
-gnatct and bound and linked in the usual manner:

     1. with Ada.Text_IO; use Ada.Text_IO;
     2. procedure Predicate_ASIS3 is
     3.    type Color is
     4.      (Red, Orange, Yellow, Green, Blue, Indigo, Violet);
     5.
     6.    subtype S1 is Color with
     7.      Predicate => S1 in Orange .. Yellow;
     8.
     9.    subtype S2 is Color with
    10.      Predicate => S2 in Blue .. Blue;
    11.
    12.    subtype Other is Color with
    13.      Predicate => Other not in S1 | S2;
    14.
    15. begin
    16.    for X in Color loop
    17.       case X is
    18.          when Other => Put_Line (X'Img & " is in Other");
    19.          when S1    => Put_Line (X'Img & " is in S1");
    20.          when S2    => Put_Line (X'Img & " is in S2");
    21.       end case;
    22.    end loop;
    23.
    24.    New_Line;
    25.
    26.    for X in Color loop
    27.       Put_Line (X'Img &
    28.                  (case X is
    29.                     when Other => " is in Other",
    30.                     when S1    => " is in S1",
    31.                     when S2    => " is in S2"));
    32.    end loop;
    33. end Predicate_ASIS3;

The output is:

RED is in Other
ORANGE is in S1
YELLOW is in S1
GREEN is in Other
BLUE is in S2
INDIGO is in Other
VIOLET is in Other

RED is in Other
ORANGE is in S1
YELLOW is in S1
GREEN is in Other
BLUE is in S2
INDIGO is in Other
VIOLET is in Other

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

2013-10-10  Robert Dewar  <dewar@adacore.com>

	* exp_ch3.adb (Expand_N_Variant_Part): Expand statically
	predicated subtype which appears in Discrete_Choices list.
	* exp_ch5.adb (Expand_N_Case_Statement): Expand statically
	predicated subtype which appears in Discrete_Choices list of
	case statement alternative.
	* exp_util.ads, exp_util.adb (Expand_Static_Predicates_In_Choices): New
	procedure.
	* sem_case.adb: Minor reformatting (Analyze_Choices): Don't
	expand out Discrete_Choices that are names of subtypes with
	static predicates. This is now done in the analyzer so that the
	-gnatct tree is properly formed for ASIS.
	* sem_case.ads (Generic_Choices_Processing): Does not apply
	to aggregates any more, so change doc accordingly, and remove
	unneeded Get_Choices argument.
	* sem_ch3.adb (Analyze_Variant_Part): Remove no
	longer used Get_Choices argument in instantiation of
	Generic_Choices_Processing.
	* sem_ch4.adb (Analyze_Case_Expression): Remove no
	longer used Get_Choices argument in instantiation of
	Generic_Choices_Processing.
	* sem_ch5.adb (Analyze_Case_Statement): Remove no
	longer used Get_Choices argument in instantiation of
	Generic_Choices_Processing.
	* sinfo.ads: For N_Variant_Part, and N_Case_Statement_Alternative,
	document that choices that are names of statically predicated
	subtypes are expanded in the code generation tree passed to the
	back end, but not in the ASIS tree generated for -gnatct.

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]