https://gcc.gnu.org/g:5e675dbc6c064c8eabadd627bbc49cbccda67495
commit r17-6031-g5e675dbc6c064c8eabadd627bbc49cbccda67495 Author: Jose E. Marchesi <[email protected]> Date: Sat Oct 31 09:49:11 2026 +0100 a68: fix handling of standard pow operators in find_operator The Algol 68 Revised Report defines the set of standard operators in its chapter 10. There are a lot of standard operators defined for SIZETY int, SIZETY real, SIZETY compl, etc. This includes "cross-term" operators that mix SIZETY int with SIZETY real. For example, + is defined for intxint->int and realxreal->real, but also for intxreal->real and realxint->real. Rather than explicitly enumerating all the possibilities, the report uses an abbreviated notation for these "cross-term" operators, like: a) op P = (L real a, L int b) L real: a P L real(b); b) op P = (L int a, L real b) L real: L real(a) P b; c) op R = (L real a, L int b) bool: a R L real(b); d) op R = (L int a, L real b) bool: L real(a) R b; g) op <^,**,UP> = (L real a, int b) L real: ...; where P is one of +, -, etc, and R is one of +, -, etc. Marcel's parser is clever and handles the cases above by "inlining" rather than explicilty defining all cross-term operators. This is done by the find_operator function in a68-parser-moids-check.cc. For example, consider this program text: int i, real r; r := r + i; Where `r + i' is a formula consisting of a dyadic operator '+' with real and int operators. In our standard prelude we have defined the following standard operators: int + int -> int real + real -> real But applying the report's rule a) above, we would need to also define op + = (real a, int b) real: a + real(b) {*} Instead, the find_operator does "inlining" by looking inserting a strong coercion from int to real: r + real(i) effectively inlining the body of the operator at {*} above, rather than requiring it being defined. Note that this extra inserted strong coercion doesn't change the fact that the syntactic position of an operand in a formula is firm, where widening is not allowed. A problem with the existing inlining code, however, is that it doesn't apply to the standard pow operator, because that operator is covered by its own rule, g) above, which _is_ explicitly defined in our standard prelude: real ^ int -> real long real ^ int -> long real long long real ^ int -> long long real but long real ^ long int -> long real long long real ^ long long int -> long long real are _not_ defined, and inlining should not happen. This patch fixes this. Signed-off-by: Jose E. Marchesi <[email protected]> gcc/algol68/ChangeLog * a68-parser-moids-check.cc (find_operator): Fix handling of standard pow operators. gcc/testsuite/ChangeLog * algol68/compile/cross-term-1.a68: New test. * algol68/compile/plusab-real-int-1.a68: Expanded. Diff: --- gcc/algol68/a68-parser-moids-check.cc | 78 +++++++++++++++++----- gcc/testsuite/algol68/compile/cross-term-1.a68 | 41 ++++++++++++ .../algol68/compile/plusab-real-int-1.a68 | 15 ++++- 3 files changed, 116 insertions(+), 18 deletions(-) diff --git a/gcc/algol68/a68-parser-moids-check.cc b/gcc/algol68/a68-parser-moids-check.cc index 7f61cec97829..92a86ff5db14 100644 --- a/gcc/algol68/a68-parser-moids-check.cc +++ b/gcc/algol68/a68-parser-moids-check.cc @@ -977,7 +977,7 @@ find_operator (TABLE_T *s, const char *n, MOID_T *x, MOID_T *y) else if (y != NO_MOID && a68_is_mode_isnt_well (y)) return A68_PARSER (error_tag); - /* (B) MONADs. */ + /* MONADs. */ if (x != NO_MOID && y == NO_MOID) { z = search_table_chain_for_operator (s, n, x, NO_MOID); @@ -988,18 +988,21 @@ find_operator (TABLE_T *s, const char *n, MOID_T *x, MOID_T *y) /* (B.2) A little trick to allow - (0, 1) or ABS (1, long pi). */ if (a68_is_coercible (x, M_COMPLEX, STRONG, SAFE_DEFLEXING)) { - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_COMPLEX, NO_MOID); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_COMPLEX, NO_MOID); if (z != NO_TAG) return z; } if (a68_is_coercible (x, M_LONG_COMPLEX, STRONG, SAFE_DEFLEXING)) { - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_LONG_COMPLEX, NO_MOID); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_LONG_COMPLEX, NO_MOID); if (z != NO_TAG) return z; } if (a68_is_coercible (x, M_LONG_LONG_COMPLEX, STRONG, SAFE_DEFLEXING)) - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_LONG_LONG_COMPLEX, NO_MOID); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_LONG_LONG_COMPLEX, NO_MOID); if (a68_is_coercible (x, M_WORD_COMPLEX, STRONG, SAFE_DEFLEXING)) { z= search_table_for_operator (OPERATORS (A68_STANDENV), n, @@ -1010,16 +1013,12 @@ find_operator (TABLE_T *s, const char *n, MOID_T *x, MOID_T *y) } return NO_TAG; } - /* (C) DYADs. */ + + /* DYADs. */ z = search_table_chain_for_operator (s, n, x, y); if (z != NO_TAG) return z; - /* The checks below apply to operators covered by a) and b) in 10.2.3.5 but - not to the pow operator, which is covered by g) */ - if (strcmp (n, "**") == 0 || strcmp (n, "^") == 0) - return z; - /* (C.2) Vector and matrix "strong coercions" in standard environ. */ u = DEFLEX (a68_depref_completely (x)); v = DEFLEX (a68_depref_completely (y)); @@ -1060,49 +1059,96 @@ find_operator (TABLE_T *s, const char *n, MOID_T *x, MOID_T *y) } } - /* (C.3) Look in standenv for an appropriate cross-term. */ + /* The RR abbreviated rules in RR 10.2.3.5: + + a) op P = (L real a, L int b) L real: a P L real(b); + b) op P = (L int a, L real b) L real: L real(a) P b; + c) op R = (L real a, L int b) bool: a R L real(b); + d) op R = (L int a, L real b) bool: L real(a) R b; + g) op <^,**,UP> = (L real a, int b) L real: ...; + + where P is one of +, -, ... + R is one of +, -, ... + + define a full set of "cross-term" operators that combine int and real + operators. Rather than enumerating all possible combinations in + stand_prelude, we resolve these here by "inlining" the corresponding `L + real(a) P b' expressions. + + Likewise for "and become" operators in RR 10.2.3.11: + + n) OP Q = (REF L REAL a, L INT b) REF L REAL: a Q L REAL (b); + o) OP Q = (REF L COMPL a, L INT b) REF L COMPL: a Q L COMPL (b); + p) OP Q = (REF L COMPL a, L REAL b) REF L COMPL: a Q L COMPL (b); + s) OP <PLUSAB,+:=> = (REF STRING a, CHAR b) REF STRING: a +:= STRING (b); + t) OP <PLUSTO,+=:> = (CHAR a, REF STRING b) REF STRING: STRING (a) +=: b; + + With Q being -:=, +:=, *:= or /:= or their synonyms. */ + + if (strcmp (n, "**") == 0 || strcmp (n, "^") == 0) + return z; + u = a68_make_series_from_moids (x, y); u = a68_make_united_mode (u); v = a68_get_balanced_mode (u, STRONG, A68_NO_DEPREF, SAFE_DEFLEXING); z = search_table_for_operator (OPERATORS (A68_STANDENV), n, v, v); if (z != NO_TAG) return z; + if (a68_is_coercible_series (u, M_REAL, STRONG, SAFE_DEFLEXING)) { z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_REAL, M_REAL); if (z != NO_TAG) return z; } + if (a68_is_coercible_series (u, M_LONG_REAL, STRONG, SAFE_DEFLEXING)) { - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_LONG_REAL, M_LONG_REAL); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_LONG_REAL, M_LONG_REAL); if (z != NO_TAG) return z; } + if (a68_is_coercible_series (u, M_LONG_LONG_REAL, STRONG, SAFE_DEFLEXING)) { - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_LONG_LONG_REAL, M_LONG_LONG_REAL); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_LONG_LONG_REAL, M_LONG_LONG_REAL); if (z != NO_TAG) return z; } + + if (a68_is_coercible_series (u, M_WORD_REAL, STRONG, SAFE_DEFLEXING)) + { + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_WORD_REAL, M_WORD_REAL); + if (z != NO_TAG) + return z; + } + if (a68_is_coercible_series (u, M_COMPLEX, STRONG, SAFE_DEFLEXING)) { z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_COMPLEX, M_COMPLEX); if (z != NO_TAG) return z; } + if (a68_is_coercible_series (u, M_LONG_COMPLEX, STRONG, SAFE_DEFLEXING)) { - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_LONG_COMPLEX, M_LONG_COMPLEX); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_LONG_COMPLEX, M_LONG_COMPLEX); if (z != NO_TAG) return z; } + if (a68_is_coercible_series (u, M_LONG_LONG_COMPLEX, STRONG, SAFE_DEFLEXING)) { - z = search_table_for_operator (OPERATORS (A68_STANDENV), n, M_LONG_LONG_COMPLEX, M_LONG_LONG_COMPLEX); + z = search_table_for_operator (OPERATORS (A68_STANDENV), n, + M_LONG_LONG_COMPLEX, M_LONG_LONG_COMPLEX); if (z != NO_TAG) return z; } + if (a68_is_coercible_series (u, M_WORD_COMPLEX, STRONG, SAFE_DEFLEXING)) { z = search_table_for_operator (OPERATORS (A68_STANDENV), n, @@ -1111,7 +1157,7 @@ find_operator (TABLE_T *s, const char *n, MOID_T *x, MOID_T *y) return z; } - /* (C.4) Now allow for depreffing for REF REAL +:= INT and alike. */ + /* Now allow for depreffing for REF REAL +:= INT and alike. */ v = a68_get_balanced_mode (u, STRONG, A68_DEPREF, SAFE_DEFLEXING); z = search_table_for_operator (OPERATORS (A68_STANDENV), n, v, v); if (z != NO_TAG) diff --git a/gcc/testsuite/algol68/compile/cross-term-1.a68 b/gcc/testsuite/algol68/compile/cross-term-1.a68 new file mode 100644 index 000000000000..0170bd82a667 --- /dev/null +++ b/gcc/testsuite/algol68/compile/cross-term-1.a68 @@ -0,0 +1,41 @@ +{ This test checks for the presence of "cross-check" operators in the + standard prelude. These are implemented by "inlining" in + a68-parser-moids-check.cc:find_operator } + +begin real r; long real lr; long long real llr; word real wr; + compl c; long compl lc; long long compl llc; word compl wc; + bool b; + { 10.2.3.5.a } + r := r + 10; + lr := lr + long 10; + llr := llr + long long 10; + wr := wr + word 10; + { 10.2.3.5.b } + r := 10 + r; + lr := long 10 + lr; + llr := long long 10 + llr; + wr := word 10 + wr; + { 10.2.3.5.c } + b := 3.14 > 2; + b := long 3.14 > long 2; + b := word 3.14 > word 2; + { 10.2.3.5.d } + b := 2 < 3.14; + b := long 2 < long 3.14; + b := word 2 < word 3.14; + { 10.2.3.11.n }r +:= 10; + lr +:= long 10; + llr +:= long long 10; + wr +:= word 10; + { 10.2.3.11.o } + c +:= 20; + lc +:= long 20; + llc +:= long long 20; + wc +:= word 20; + { 10.2.3.11.p } + c +:= 20.0; + lc +:= long 20.0; + llc +:= long long 20.0; + wc +:= word 20.0; + skip +end diff --git a/gcc/testsuite/algol68/compile/plusab-real-int-1.a68 b/gcc/testsuite/algol68/compile/plusab-real-int-1.a68 index 39e601167c53..f7b0d060549e 100644 --- a/gcc/testsuite/algol68/compile/plusab-real-int-1.a68 +++ b/gcc/testsuite/algol68/compile/plusab-real-int-1.a68 @@ -1,3 +1,14 @@ -begin real r; - r +:= 10 +begin { 10.2.3.11.n } + real r; r +:= 10; + long real lr; lr +:= long 10; + long long real llr; llr +:= long long 10; + { 10.2.3.11.o } + compl c; c +:= 20; + long compl lc; lc +:= long 20; + long long compl llc; llc +:= long long 20; + { 10.2.3.11.p } + c +:= 20.0; + lc +:= long 20.0; + llc +:= long long 20.0; + skip end
