72a884
From 9e2a12416cf6ce7b926829aff353fe2d9019f135 Mon Sep 17 00:00:00 2001
72a884
From: William S Fulton <wsf@fultondesigns.co.uk>
72a884
Date: Mon, 18 Sep 2017 07:06:27 +0100
72a884
Subject: [PATCH 2/2] Fix type promotion wrapping some non-trivial constant
72a884
 expressions
72a884
72a884
This was previously an integral type instead of a floating point type:
72a884
---
72a884
 CHANGES.current                                         | 5 +++++
72a884
 Examples/test-suite/csharp/preproc_constants_c_runme.cs | 1 +
72a884
 Examples/test-suite/csharp/preproc_constants_runme.cs   | 1 +
72a884
 Examples/test-suite/d/preproc_constants_c_runme.1.d     | 1 +
72a884
 Examples/test-suite/d/preproc_constants_c_runme.2.d     | 1 +
72a884
 Examples/test-suite/d/preproc_constants_runme.1.d       | 1 +
72a884
 Examples/test-suite/d/preproc_constants_runme.2.d       | 1 +
72a884
 Examples/test-suite/php/preproc_constants_c_runme.php   | 1 +
72a884
 Examples/test-suite/php/preproc_constants_runme.php     | 1 +
72a884
 Examples/test-suite/php5/preproc_constants_c_runme.php  | 1 +
72a884
 Examples/test-suite/php5/preproc_constants_runme.php    | 1 +
72a884
 Examples/test-suite/preproc_constants.i                 | 1 +
72a884
 Source/CParse/parser.y                                  | 1 +
72a884
 13 files changed, 17 insertions(+)
72a884
72a884
#diff --git a/CHANGES.current b/CHANGES.current
72a884
#index b455a9f..f76e5a5 100644
72a884
#--- a/CHANGES.current
72a884
#+++ b/CHANGES.current
72a884
#@@ -7,6 +7,11 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
72a884
# Version 4.0.0 (in progress)
72a884
# ===========================
72a884
# 
72a884
#+2017-09-18: wsfulton
72a884
#+            Fix type promotion wrapping constant expressions of the form:
72a884
#+              # define EXPR_MIXED1    (0x80 + 11.1) - 1
72a884
#+            This was previously an integral type instead of a floating point type.
72a884
#+
72a884
# 2017-09-17: wsfulton
72a884
#             Fix generated code for constant expressions containing wchar_t L literals such as:
72a884
#               # define __WCHAR_MAX    (0x7fffffff + L'\0')
72a884
diff --git a/Examples/test-suite/csharp/preproc_constants_c_runme.cs b/Examples/test-suite/csharp/preproc_constants_c_runme.cs
72a884
index 1c28e49..7f40ce4 100644
72a884
--- a/Examples/test-suite/csharp/preproc_constants_c_runme.cs
72a884
+++ b/Examples/test-suite/csharp/preproc_constants_c_runme.cs
72a884
@@ -61,6 +61,7 @@ public class runme {
72a884
     assert( typeof(int) == preproc_constants_c.EXPR_LAND.GetType() );
72a884
     assert( typeof(int) == preproc_constants_c.EXPR_LOR.GetType() );
72a884
     assert( typeof(double) == preproc_constants_c.EXPR_CONDITIONAL.GetType() );
72a884
+    assert( typeof(double) == preproc_constants_c.EXPR_MIXED1.GetType() );
72a884
     assert( typeof(int) == preproc_constants_c.EXPR_WCHAR_MAX.GetType() );
72a884
     assert( typeof(int) == preproc_constants_c.EXPR_WCHAR_MIN.GetType() );
72a884
   }
72a884
diff --git a/Examples/test-suite/csharp/preproc_constants_runme.cs b/Examples/test-suite/csharp/preproc_constants_runme.cs
72a884
index 6b02e30..0d8981f 100644
72a884
--- a/Examples/test-suite/csharp/preproc_constants_runme.cs
72a884
+++ b/Examples/test-suite/csharp/preproc_constants_runme.cs
72a884
@@ -60,6 +60,7 @@ public class runme {
72a884
     assert( typeof(bool) == preproc_constants.EXPR_LAND.GetType() );
72a884
     assert( typeof(bool) == preproc_constants.EXPR_LOR.GetType() );
72a884
     assert( typeof(double) == preproc_constants.EXPR_CONDITIONAL.GetType() );
72a884
+    assert( typeof(double) == preproc_constants.EXPR_MIXED1.GetType() );
72a884
     assert( typeof(int) == preproc_constants.EXPR_WCHAR_MAX.GetType() );
72a884
     assert( typeof(int) == preproc_constants.EXPR_WCHAR_MIN.GetType() );
72a884
 
72a884
diff --git a/Examples/test-suite/d/preproc_constants_c_runme.1.d b/Examples/test-suite/d/preproc_constants_c_runme.1.d
72a884
index 2b349af..b79ee3b 100644
72a884
--- a/Examples/test-suite/d/preproc_constants_c_runme.1.d
72a884
+++ b/Examples/test-suite/d/preproc_constants_c_runme.1.d
72a884
@@ -61,6 +61,7 @@ void main() {
72a884
   static assert(is(int == typeof(EXPR_LAND())));
72a884
   static assert(is(int == typeof(EXPR_LOR())));
72a884
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
72a884
+  static assert(is(double == typeof(EXPR_MIXED1())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MAX())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MIN())));
72a884
 }
72a884
diff --git a/Examples/test-suite/d/preproc_constants_c_runme.2.d b/Examples/test-suite/d/preproc_constants_c_runme.2.d
72a884
index 1bac525..260bf8d 100644
72a884
--- a/Examples/test-suite/d/preproc_constants_c_runme.2.d
72a884
+++ b/Examples/test-suite/d/preproc_constants_c_runme.2.d
72a884
@@ -61,6 +61,7 @@ void main() {
72a884
   static assert(is(int == typeof(EXPR_LAND())));
72a884
   static assert(is(int == typeof(EXPR_LOR())));
72a884
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
72a884
+  static assert(is(double == typeof(EXPR_MIXED1())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MAX())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MIN())));
72a884
 }
72a884
diff --git a/Examples/test-suite/d/preproc_constants_runme.1.d b/Examples/test-suite/d/preproc_constants_runme.1.d
72a884
index f743f48..84a99c8 100644
72a884
--- a/Examples/test-suite/d/preproc_constants_runme.1.d
72a884
+++ b/Examples/test-suite/d/preproc_constants_runme.1.d
72a884
@@ -60,6 +60,7 @@ void main() {
72a884
   static assert(is(bool == typeof(EXPR_LAND())));
72a884
   static assert(is(bool == typeof(EXPR_LOR())));
72a884
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
72a884
+  static assert(is(double == typeof(EXPR_MIXED1())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MAX())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MIN())));
72a884
 }
72a884
diff --git a/Examples/test-suite/d/preproc_constants_runme.2.d b/Examples/test-suite/d/preproc_constants_runme.2.d
72a884
index 0d96c37..f6638f4 100644
72a884
--- a/Examples/test-suite/d/preproc_constants_runme.2.d
72a884
+++ b/Examples/test-suite/d/preproc_constants_runme.2.d
72a884
@@ -60,6 +60,7 @@ void main() {
72a884
   static assert(is(bool == typeof(EXPR_LAND())));
72a884
   static assert(is(bool == typeof(EXPR_LOR())));
72a884
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
72a884
+  static assert(is(double == typeof(EXPR_MIXED1())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MAX())));
72a884
   static assert(is(int == typeof(EXPR_WCHAR_MIN())));
72a884
 }
72a884
diff --git a/Examples/test-suite/php/preproc_constants_c_runme.php b/Examples/test-suite/php/preproc_constants_c_runme.php
72a884
index e59fe18..20868dc 100644
72a884
--- a/Examples/test-suite/php/preproc_constants_c_runme.php
72a884
+++ b/Examples/test-suite/php/preproc_constants_c_runme.php
72a884
@@ -62,6 +62,7 @@ check::equal(gettype(preproc_constants_c::EXPR_OR), "integer", "preproc_constant
72a884
 check::equal(gettype(preproc_constants_c::EXPR_LAND), "integer", "preproc_constants.EXPR_LAND has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_LOR), "integer", "preproc_constants.EXPR_LOR has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
72a884
+check::equal(gettype(preproc_constants_c::EXPR_MIXED1), "double", "preproc_constants.EXPR_MIXED1 has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
72a884
 
72a884
diff --git a/Examples/test-suite/php/preproc_constants_runme.php b/Examples/test-suite/php/preproc_constants_runme.php
72a884
index 8e117ea..ef32867 100644
72a884
--- a/Examples/test-suite/php/preproc_constants_runme.php
72a884
+++ b/Examples/test-suite/php/preproc_constants_runme.php
72a884
@@ -63,5 +63,6 @@ check::equal(gettype(preproc_constants::EXPR_LOR), "boolean", "preproc_constants
72a884
 check::equal(gettype(preproc_constants::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
72a884
 check::equal(gettype(preproc_constants::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
72a884
 check::equal(gettype(preproc_constants::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
72a884
+check::equal(gettype(preproc_constants::EXPR_MIXED1), "integer", "preproc_constants.EXPR_MIXED1 has unexpected type");
72a884
 
72a884
 ?>
72a884
diff --git a/Examples/test-suite/php5/preproc_constants_c_runme.php b/Examples/test-suite/php5/preproc_constants_c_runme.php
72a884
index d978fab..d55d423 100644
72a884
--- a/Examples/test-suite/php5/preproc_constants_c_runme.php
72a884
+++ b/Examples/test-suite/php5/preproc_constants_c_runme.php
72a884
@@ -62,6 +62,7 @@ check::equal(gettype(preproc_constants_c::EXPR_OR), "integer", "preproc_constant
72a884
 check::equal(gettype(preproc_constants_c::EXPR_LAND), "integer", "preproc_constants.EXPR_LAND has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_LOR), "integer", "preproc_constants.EXPR_LOR has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
72a884
+check::equal(gettype(preproc_constants_c::EXPR_MIXED1), "double", "preproc_constants.EXPR_MIXED1 has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
72a884
 check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
72a884
 
72a884
diff --git a/Examples/test-suite/php5/preproc_constants_runme.php b/Examples/test-suite/php5/preproc_constants_runme.php
72a884
index 7527026..01137b0 100644
72a884
--- a/Examples/test-suite/php5/preproc_constants_runme.php
72a884
+++ b/Examples/test-suite/php5/preproc_constants_runme.php
72a884
@@ -70,6 +70,7 @@ check::equal(gettype(preproc_constants::EXPR_LAND), "integer", "preproc_constant
72a884
 check::equal(gettype(preproc_constants::EXPR_LOR), "integer", "preproc_constants.EXPR_LOR has unexpected type");
72a884
 
72a884
 check::equal(gettype(preproc_constants::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
72a884
+check::equal(gettype(preproc_constants::EXPR_MIXED1), "double", "preproc_constants.EXPR_MIXED1 has unexpected type");
72a884
 check::equal(gettype(preproc_constants::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
72a884
 check::equal(gettype(preproc_constants::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
72a884
 
72a884
diff --git a/Examples/test-suite/preproc_constants.i b/Examples/test-suite/preproc_constants.i
72a884
index 16b44c9..628cae1 100644
72a884
--- a/Examples/test-suite/preproc_constants.i
72a884
+++ b/Examples/test-suite/preproc_constants.i
72a884
@@ -86,6 +86,7 @@
72a884
 #define EXPR_LAND        0xFF && 1
72a884
 #define EXPR_LOR         0xFF || 1
72a884
 #define EXPR_CONDITIONAL true ? 2 : 2.2
72a884
+#define EXPR_MIXED1      (0x80 + 11.1) - 1
72a884
 
72a884
 #define EXPR_WCHAR_MAX   (0x7fffffff + L'\0')
72a884
 #define EXPR_WCHAR_MIN   (-EXPR_WCHAR_MAX - 1)
72a884
diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y
72a884
index 273dadb..3df9896 100644
72a884
--- a/Source/CParse/parser.y
72a884
+++ b/Source/CParse/parser.y
72a884
@@ -6338,6 +6338,7 @@ valexpr        : exprnum { $$ = $1; }
72a884
 		       break;
72a884
 		   }
72a884
 		 }
72a884
+		 $$.type = promote($2.type, $4.type);
72a884
  	       }
72a884
                | LPAREN expr pointer RPAREN expr %prec CAST {
72a884
                  $$ = $5;
72a884
-- 
72a884
2.9.5
72a884