Added support for type alias

This commit is contained in:
Lior Goldberg 2016-06-19 22:37:29 +03:00
parent 86fd5c9858
commit c363a93d69
5 changed files with 121 additions and 53 deletions

View File

@ -29,7 +29,7 @@
<li><a href="#CPlusPlus11_strongly_typed_enumerations">Strongly typed enumerations</a>
<li><a href="#CPlusPlus11_double_angle_brackets">Double angle brackets</a>
<li><a href="#CPlusPlus11_explicit_conversion_operators">Explicit conversion operators</a>
<li><a href="#CPlusPlus11_alias_templates">Alias templates</a>
<li><a href="#CPlusPlus11_alias_templates">Type alias and alias templates</a>
<li><a href="#CPlusPlus11_unrestricted_unions">Unrestricted unions</a>
<li><a href="#CPlusPlus11_variadic_templates">Variadic templates</a>
<li><a href="#CPlusPlus11_new_string_literals">New string literals</a>
@ -603,8 +603,27 @@ Conversion operators either with or without <tt>explicit</tt> need renaming to a
them available as a normal proxy method.
</p>
<H3><a name="CPlusPlus11_alias_templates">7.2.16 Alias templates</a></H3>
<H3><a name="CPlusPlus11_alias_templates">7.2.16 Type alias and alias templates</a></H3>
<p>
A type alias is a statement of the form:
</p>
<div class="code"><pre>
using PFD = void (*)(double); // New introduced syntax
</pre></div>
<p>
which is equivalent to the old style typedef:
</p>
<div class="code"><pre>
typedef void (*PFD)(double); // The old style
</pre></div>
<p>
SWIG supports this kind of statements.
</p>
<p>
The following is an example of an alias template:
@ -632,31 +651,6 @@ example.i:13: Warning 342: The 'using' keyword in template aliasing is not fully
</pre>
</div>
<p>
Similarly for non-template type aliasing:
</p>
<div class="code"><pre>
using PFD = void (*)(double); // New introduced syntax
</pre></div>
<p>
A warning will be issued:
</p>
<div class="shell">
<pre>
example.i:17: Warning 341: The 'using' keyword in type aliasing is not fully supported yet.
</pre>
</div>
<p>The equivalent old style typedefs can be used as a workaround:</p>
<div class="code"><pre>
typedef void (*PFD)(double); // The old style
</pre></div>
<H3><a name="CPlusPlus11_unrestricted_unions">7.2.17 Unrestricted unions</a></H3>

View File

@ -2,15 +2,6 @@
// Type aliasing seg fault : Github issue #424
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) Target;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) Int;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntRef;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntPtrRef;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntRValueRef;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntArray;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) HalideTargetPtr1;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) HalideTargetPtr2;
%inline %{
namespace Halide {
@ -53,14 +44,69 @@ public:
%inline %{
using Int = int;
using IntRef = int&;
using IntPtrRef = int*&;
using IntRValueRef = int&&;
using IntArray = int[];
using HalideTargetPtr1 = Halide::Target*;
namespace Halide {
using HalideTargetPtr2 = Target*;
}
%}
// Define some types
%inline %{
using Int = int;
using IntPtr = Int*;
using IntRef = Int&;
using IntPtrRef = Int*&;
using IntRValueRef = Int&&;
using IntArray = Int[];
%}
// Test that SWIG understands these new types
%callback("%s_cb");
Int mult2(Int x);
%nocallback;
%inline %{
Int mult2(Int x) { return x * 2; }
IntPtr allocate_int() { return new Int(12); }
void free_int(int* ptr) { delete ptr; }
void inplace_mult2(IntRef x) { x *= 2; }
Int read_int(IntPtr ptr) { return *ptr; }
template <typename T> class Pair {
public:
using data_t = T;
data_t a, b;
Pair() : a(), b() { }
Pair(data_t a, data_t b) : a(a), b(b) { }
data_t first() { return a; }
data_t second() { return b; }
};
%}
%template(int_pair) Pair<Int>;
%inline %{
using PairInt = Pair<Int>;
class PairSubclass : public PairInt {
public:
PairSubclass(data_t a, data_t b) : PairInt(a, b) { }
using const_ref_data_t = const data_t&;
};
PairSubclass::data_t plus1(PairSubclass::const_ref_data_t x) { return x + 1; }
%}
// Test function pointers
%inline %{
using callback_t = int(*)(int);
callback_t get_callback() { return mult2; }
int call(callback_t func, int param) { return func(param); }
%}

View File

@ -1,3 +0,0 @@
cpp_using_type_aliasing.i:8: Warning 341: The 'using' keyword in type aliasing is not fully supported yet.
cpp_using_type_aliasing.i:8: Warning 315: Nothing known about 'Okay< int >'.
cpp_using_type_aliasing.i:8: Warning 315: Nothing known about 'Okay< int >'.

View File

@ -0,0 +1,32 @@
from cpp11_type_aliasing import *
if get_host_target().bits != 32:
raise RuntimeError("get_host_target().bits should return 32")
if mult2(10) != 20:
raise RuntimeError("mult2(10) should return 20")
int_ptr = allocate_int()
inplace_mult2(int_ptr)
if read_int(int_ptr) != 24:
raise RuntimeError("read_int should return 24")
free_int(int_ptr)
pair = PairSubclass(3, 4)
if pair.first() != 3:
raise RuntimeError("pair.first() should return 3")
if pair.second() != 4:
raise RuntimeError("pair.second() should return 4")
if pair.a != 3:
raise RuntimeError("pair.a should be 3")
if plus1(5) != 6:
raise RuntimeError("plus1(5) should return 6")
if call(mult2_cb, 7) != 14:
raise RuntimeError("call(mult2_cb, 7) should return 14")
if call(get_callback(), 7) != 14:
raise RuntimeError("call(get_callback(), 7) should return 14")

View File

@ -2899,16 +2899,15 @@ c_declaration : c_decl {
SWIG_WARN_NODE_END($$);
}
| USING idcolon EQUAL type plain_declarator SEMI {
$$ = new_node("using");
Setattr($$,"name",$2);
/* Convert using statement to a typedef statement */
$$ = new_node("cdecl");
SwigType_push($4,$5.type);
Setattr($$,"uname",$4);
Setattr($$,"type",$4);
Setattr($$,"storage","typedef");
Setattr($$,"name",$2);
Setattr($$,"decl","");
SetFlag($$,"typealias");
add_symbols($$);
SWIG_WARN_NODE_BEGIN($$);
Swig_warning(WARN_CPP11_ALIAS_DECLARATION, cparse_file, cparse_line, "The 'using' keyword in type aliasing is not fully supported yet.\n");
SWIG_WARN_NODE_END($$);
$$ = 0; /* TODO - ignored for now */
}
| TEMPLATE LESSTHAN template_parms GREATERTHAN USING idcolon EQUAL type plain_declarator SEMI {
$$ = new_node("using");