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] Implement static predicates for string/real types


This implements static predicates for string and real types,
as defined in the RM. There is one exception, which is that
the RM allows X > "ABC" as being predicate static, but since
"ABC" > "ABA" is not static, that's peculiar, so we assume
that this is a mistake in the RM, and that string comparisons
should not be permitted as predicate-static.

The following test program shows various uses of static
predicates of all types with a range of legality tests
and tests for compile time evaluation of static predicates.

     1. package TestSP is
     2.    subtype F1 is Float with -- OK
     3.    Static_Predicate => F1 > 0.0 and 4.7 > F1;
     4.
     5.    subtype F1a is F1 with -- OK
     6.      Static_Predicate => F1a > 2.4;
     7.
     8.    subtype F2 is Float with -- ERROR
     9.      Static_Predicate => (F2 + 1.0) > 0.0 and 4.7 > F2;
                                  |
        >>> expression is not predicate-static (RM 4.3.2(16-22))

    10.    subtype F3 is Float with -- OK
    11.      Dynamic_Predicate => (F3 + 1.0) > 0.0 and 4.7 > F3;
    12.    subtype F4 is Float with -- OK
    13.      Predicate => (F4 + 1.0) > 0.0 and 4.7 > F4;
    14.
    15.    subtype S0 is String with -- ERROR
    16.      Static_Predicate => S0 > "ABC" and then "DEF" >= S0;
                                 |
        >>> expression is not predicate-static (RM 4.3.2(16-22))

    17.    subtype S1 is String with -- OK
    18.      Static_Predicate => S1 in "ABC" | "DEF";
    19.
    20.    subtype S2 is String with -- ERROR
    21.      Static_Predicate => S2'First = 1 and then S2(1) = 'A';
                                 |
        >>> expression is not predicate-static (RM 4.3.2(16-22))

    22.    subtype S3 is String with -- OK
    23.      Dynamic_Predicate => S3'First = 1 and then S3(1) = 'A';
    24.    subtype S4 is String with -- OK
    25.      Predicate => S4'First = 1 and then S4(1) = 'A';
    26.    subtype S5 is String with -- OK
    27.      Predicate => S5 in "ABC" | "DEF";
    28.    subtype S6 is String with -- OK
    29.      Dynamic_Predicate => S6 in "ABC" | "DEF";
    30.
    31.    subtype I1 is Integer with -- OK
    32.      Static_Predicate => I1 > 0 and 4 > I1;
    33.    subtype I1a is I1 with -- OK
    34.      Static_Predicate => I1a > 2;
    35.
    36.    subtype I2 is Integer with -- ERROR
    37.      Static_Predicate => (I2 + 1) > 0 and 4 > I2;
                                  |
        >>> expression is not predicate-static (RM 4.3.2(16-22))

    38.    subtype I3 is Integer with -- OK
    39.      Dynamic_Predicate => (I3 + 1) > 0 and 4 > I3;
    40.    subtype I4 is Integer with -- OK
    41.      Predicate => (I4 + 1) > 0 and 4 > I4;
    42.
    43.    subtype I5 is Integer with -- ERROR
    44.      Static_Predicate => Boolean'(I5 > 0);
                                 |
        >>> expression is not predicate-static (RM 4.3.2(16-22))

    45.
    46.    XF1 : constant F1 := 10.0; -- WARN
                                |
        >>> warning: static expression fails static predicate
            check on "F1", expression is no longer considered
            static

    47.    XF2 : constant F1 := 3.0;  -- OK
    48.
    49.    XF3 : constant := XF1;     -- ERROR
                             |
        >>> non-static expression used in number declaration
        >>> "XF1" is not a static constant (RM 4.9(5))

    50.    XF4 : constant := XF2;     -- OK
    51.
    52.    XF1a : constant F1a := 1.3; -- WARN;
                                  |
        >>> warning: static expression fails static predicate
            check on "F1a", expression is no longer considered
            static

    53.    XF1b : constant F1a := 5.3; -- WARN;
                                  |
        >>> warning: static expression fails static predicate
            check on "F1a", expression is no longer considered
            static

    54.    XF1c : constant F1a := 3.7; -- OK
    55.
    56.    XI1 : constant I1 := 10; -- WARN
                                |
        >>> warning: static expression fails static predicate
            check on "I1", expression is no longer considered
            static

    57.    XI2 : constant I1 := 3;  -- OK
    58.
    59.    XI3 : constant := XI1;   -- ERROR
                             |
        >>> non-static expression used in number declaration
        >>> "XI1" is not a static constant (RM 4.9(5))

    60.    XI4 : constant := XI2;   -- OK
    61.
    62.    XI1a : constant I1a := 2; -- WARN
                                  |
        >>> warning: static expression fails static predicate
            check on "I1a", expression is no longer considered
            static

    63.    XI1b : constant I1a := 7; -- WARN
                                  |
        >>> warning: static expression fails static predicate
            check on "I1a", expression is no longer considered
            static

    64.    XI1c : constant I1a := 3; -- OK
    65.
    66.    XSa : constant S1 := "ABC"; -- OK
    67.
    68.    Xsb : constant S1 := "DQR"; -- WARN
                                |
        >>> warning: static expression fails static predicate
            check on "S1", expression is no longer considered
            static

    69.    XSc : constant S5 := "ABC"; -- OK
    70.
    71.    Xsd : constant S5 := "DQR"; -- WARN
                                |
        >>> warning: static expression fails static predicate
            check on "S5", expression is no longer considered
            static

    72.    Xse : constant S6 := "ABC"; -- OK
    73.
    74.    Xsf : constant S6 := "DQR"; -- WARN
                                |
        >>> warning: expression fails predicate check on "S6"

    75. end;

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

2014-07-29  Robert Dewar  <dewar@adacore.com>

	* einfo.ads, einfo.adb (Static_Real_Or_String_Predicate): New function
	(Set_Static_Real_Or_String_Predicate): New procedure
	* sem_ch13.adb (Build_Predicate_Functions): Accomodate static
	string predicates (Is_Predicate_Static): Handle static string
	predicates.
	* sem_eval.adb (Real_Or_String_Static_Predicate_Matches):
	New procedure (Check_Expression_Against_Static_Predicate):
	Deal with static string predicates, now fully implemented
	(Eval_Relational_Op): Allow string equality/inequality as static
	if not comes from source.

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]