Merge branch 'spaceship'

This commit is contained in:
Olly Betts 2022-07-27 09:12:47 +12:00
commit 4c14dc7010
14 changed files with 107 additions and 4 deletions

View file

@ -204,6 +204,7 @@ SWIG_TOKEN_LESSTHAN <
SWIG_TOKEN_GREATERTHAN >
SWIG_TOKEN_LTEQUAL <=
SWIG_TOKEN_GTEQUAL >=
SWIG_TOKEN_LTEQUALGT <=>
SWIG_TOKEN_NOT ~
SWIG_TOKEN_LNOT !
SWIG_TOKEN_LBRACKET [

View file

@ -34,6 +34,21 @@ Work has only just begun on adding C++20 support.
<H2><a name="CPlusPlus20_core_language_changes">10.2 Core language changes</a></H2>
<H3><a name="CPlusPlus20_spaceship_operator">10.2.1 Spaceship operator</a></H3>
<p>
SWIG supports the spaceship operator <tt>&lt;=&gt;</tt> in constant
expressions. To simplifying handling the return value is currently
treated as an integer rather than <tt>std::strong_ordering</tt>, etc.
In practice we think that should do the right thing in most cases.
</p>
<p>
SWIG also recognises <tt>operator&lt;=&gt;</tt> which can be wrapped
if renamed. There's not currently any default renaming for the operator
or attempts to automatically map it to a three-way comparison operator
in any target languages that have one.
</p>
<H2><a name="CPlusPlus20_standard_library_changes">10.3 Standard library changes</a></H2>

View file

@ -649,6 +649,7 @@ CPP17_TEST_BROKEN = \
# C++20 test cases.
CPP20_TEST_CASES += \
cpp20_spaceship_operator \
# Broken C++20 test cases.
CPP20_TEST_BROKEN = \

View file

@ -0,0 +1,28 @@
%module cpp20_spaceship_operator
%rename(spaceship) operator<=>;
%inline %{
#include <compare>
int v = (-1 <=> 1 > 0) ? 7 : 42;
// We use !(a >= b) here due to limited support for (a < b) in SWIG's parser.
#define ALIEN !(0 <=> 1 >= 0)
const int SPACE = 3 <=> 3 == 0;
struct A {
int v;
explicit A(int v_) : v(v_) { }
};
int operator<=>(const A& a, const A& b) {
return a.v - b.v;
}
int f(int v = (-1 <=> 1 > 0) ? 7 : 42) { return v; }
%}
%constant int COMET = (4 <=> 2 > 0);

View file

@ -66,3 +66,7 @@
/* Unary + was a no-op and so this didn't give an error in SWIG < 4.1.0. */
#if "1" == +"1"
#endif
/* Spaceship operator doesn't seem to be allowed in preprocessor expressions. */
#if (4 <=> 2) < 0
#endif

View file

@ -31,3 +31,5 @@ pp_expressions_bad.i:64: Warning 202: Could not evaluate expression '"1" == ~"1"
pp_expressions_bad.i:64: Warning 202: Syntax error: attempt to apply unary operator to string
pp_expressions_bad.i:67: Warning 202: Could not evaluate expression '"1" == +"1"'
pp_expressions_bad.i:67: Warning 202: Syntax error: attempt to apply unary operator to string
pp_expressions_bad.i:71: Warning 202: Could not evaluate expression '(4 <=> 2) < 0'
pp_expressions_bad.i:71: Warning 202: Spaceship operator (<=>) not allowed in preprocessor expression

View file

@ -0,0 +1,23 @@
<?php
require "tests.php";
check::functions(array('f', 'spaceship'));
check::classes(array('cpp20_spaceship_operator','A'));
check::globals(array('v', 'SPACE'));
//check::equal(ALIEN, true);
check::equal(SPACE_get(), 1);
check::equal(COMET, 1);
check::equal(v_get(), 42);
$x = new A(1);
$y = new A(2);
check::equal(spaceship($x, $y) < 0, true);
check::equal(spaceship($x, $x), 0);
check::equal(spaceship($y, $x) > 0, true);
check::equal(f(), 42);
check::done();

View file

@ -108,6 +108,7 @@
%define SWIGWARN_IGNORE_OPERATOR_NEWARR_MSG "394:operator new[] ignored" %enddef
%define SWIGWARN_IGNORE_OPERATOR_DELARR_MSG "395:operator delete[] ignored" %enddef
%define SWIGWARN_IGNORE_OPERATOR_REF_MSG "396:operator*() ignored" %enddef
%define SWIGWARN_IGNORE_OPERATOR_LTEQUALGT_MSG "397:operator<=> ignored" %enddef
#define %ignoreoperator(Oper) %ignorewarn(SWIGWARN_IGNORE_OPERATOR_##Oper##_MSG)

View file

@ -331,6 +331,8 @@ static int yylook(void) {
return COLON;
case SWIG_TOKEN_DCOLONSTAR:
return DSTAR;
case SWIG_TOKEN_LTEQUALGT:
return LESSEQUALGREATER;
case SWIG_TOKEN_DCOLON:
{

View file

@ -1626,7 +1626,7 @@ static String *add_qualifier_to_declarator(SwigType *type, SwigType *qualifier)
%token TYPEMAP EXCEPT ECHO APPLY CLEAR SWIGTEMPLATE FRAGMENT
%token WARN
%token LESSTHAN GREATERTHAN DELETE_KW DEFAULT
%token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO
%token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO LESSEQUALGREATER
%token ARROW
%token QUESTIONMARK
%token TYPES PARMS
@ -1648,6 +1648,7 @@ static String *add_qualifier_to_declarator(SwigType *type, SwigType *qualifier)
%left AND
%left EQUALTO NOTEQUALTO
%left GREATERTHAN LESSTHAN GREATERTHANOREQUALTO LESSTHANOREQUALTO
%left LESSEQUALGREATER
%left LSHIFT RSHIFT
%left PLUS MINUS
%left STAR SLASH MODULO
@ -6844,6 +6845,15 @@ exprcompound : expr PLUS expr {
$$.val = NewStringf("%s <= %s", COMPOUND_EXPR_VAL($1), COMPOUND_EXPR_VAL($3));
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr LESSEQUALGREATER expr {
$$.val = NewStringf("%s <=> %s", COMPOUND_EXPR_VAL($1), COMPOUND_EXPR_VAL($3));
// Really `<=>` returns one of `std::strong_ordering`,
// `std::partial_ordering` or `std::weak_ordering`, but we
// fake it by treating the return value as `int`. The main
// thing to do with the return value in this context is to
// compare it with 0, for which `int` does the job.
$$.type = T_INT;
}
| expr QUESTIONMARK expr COLON expr %prec QUESTIONMARK {
$$.val = NewStringf("%s?%s:%s", COMPOUND_EXPR_VAL($1), COMPOUND_EXPR_VAL($3), COMPOUND_EXPR_VAL($5));
/* This may not be exactly right, but is probably good enough

View file

@ -147,6 +147,7 @@
#define WARN_IGNORE_OPERATOR_NEWARR 394 /* new [] */
#define WARN_IGNORE_OPERATOR_DELARR 395 /* delete [] */
#define WARN_IGNORE_OPERATOR_REF 396 /* operator *() */
#define WARN_IGNORE_OPERATOR_LTEQUALGT 397 /* <=> */
/* please leave 350-399 free for WARN_IGNORE_OPERATOR_* */

View file

@ -445,6 +445,8 @@ int Preprocessor_expr(DOH *s, int *error) {
stack[sp - 1].svalue = stack[sp].svalue;
sp--;
break;
case SWIG_TOKEN_LTEQUALGT:
goto spaceship_not_allowed;
default:
goto syntax_error_expected_operator;
break;
@ -476,6 +478,11 @@ extra_rparen:
errmsg = "Extra \')\'";
*error = 1;
return 0;
spaceship_not_allowed:
errmsg = "Spaceship operator (<=>) not allowed in preprocessor expression";
*error = 1;
return 0;
}
/* -----------------------------------------------------------------------------

View file

@ -893,9 +893,16 @@ static int look(Scanner *s) {
}
if (c == '<')
state = 240;
else if (c == '=')
return SWIG_TOKEN_LTEQUAL;
else {
else if (c == '=') {
if ((c = nextchar(s)) == 0) {
return SWIG_TOKEN_LTEQUAL;
} else if (c == '>') { /* Spaceship operator */
return SWIG_TOKEN_LTEQUALGT;
} else {
retract(s, 1);
return SWIG_TOKEN_LTEQUAL;
}
} else {
retract(s, 1);
brackets_increment(s);
return SWIG_TOKEN_LESSTHAN;

View file

@ -116,3 +116,4 @@ extern void Scanner_locator(Scanner *, String *loc);
#define SWIG_TOKEN_MODEQUAL 134 /* %= */
#define SWIG_TOKEN_ARROW 135 /* -> */
#define SWIG_TOKEN_ARROWSTAR 136 /* ->* */
#define SWIG_TOKEN_LTEQUALGT 137 /* <=> */