Fix syntax error when the template keyword is used in types

For example:
  std::template vector<int> v;
This commit is contained in:
William S Fulton 2015-07-02 07:40:29 +01:00
commit 05397cf6a2
5 changed files with 93 additions and 18 deletions

View file

@ -5,6 +5,11 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.6 (in progress)
===========================
2015-07-02: wsfulton
Fix syntax error when the template keyword is used in types, eg:
std::template vector<int> v;
2015-07-02: ngladitz
[Lua] Push characters as unformatted 1-character strings to avoid
unprintable characters such as (char)127 being converted to

View file

@ -412,6 +412,7 @@ CPP_TEST_CASES += \
template_inherit \
template_inherit_abstract \
template_int_const \
template_keyword_in_type \
template_methods \
template_namespace_forward_declaration \
template_using_directive_and_declaration_forward \

View file

@ -5,6 +5,8 @@
%warnfilter(SWIGWARN_CPP11_ALIAS_TEMPLATE) TypedefNamePtr;
%warnfilter(SWIGWARN_CPP11_ALIAS_TEMPLATE) MyIntKeyClass;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) PF;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) BucketAllocator1;
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) BucketAllocator2;
// This warning should go away when type aliasing is supported
#pragma SWIG nowarn=SWIGWARN_PARSE_USING_UNDEF // Nothing known about 'p.SomeType< char *,T2,4 >'.
@ -39,3 +41,16 @@ TypedefName<int> alias1(TypedefName<int> a) { return a; }
TypedefNamePtr<int> alias1(TypedefNamePtr<int> a = nullptr) { return a; }
%}
%inline %{
typedef double Val;
template<typename T> struct ListBucket {
};
namespace Alloc {
template<typename T> struct rebind {
typedef int other;
};
}
using BucketAllocator1 = typename Alloc::template rebind<ListBucket<Val>>::other;
using BucketAllocator2 = typename Alloc::template rebind<::template ListBucket<double>>::other;
%}

View file

@ -0,0 +1,38 @@
%module template_keyword_in_type
%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) YYY;
%inline %{
template<typename T> struct XXX;
template<typename T> struct XXX {
template<typename TT> struct YYY {
typedef TT type;
};
int xxx(int h) { return h; }
};
#if defined(SWIG) || defined(__clang__)
// gcc doesn't parse this (tested with gcc-4.8)
void testXXX1(::template XXX<int>::template YYY<int>::type xx) {}
#else
void testXXX1(:: XXX<int>::template YYY<int>::type xx) {}
#endif
void testXXX2(XXX<int>::YYY<int>::type xx) {}
typedef ::XXX<int>::template YYY<int>::type templatetyped;
%}
%inline %{
typedef double Val;
template<typename T> struct ListBucket {
};
namespace Alloc {
template<typename T> struct rebind {
typedef int other;
};
}
void other1(typename Alloc::template rebind<ListBucket<Val> >::other) {}
void other2(typename Alloc::template rebind< ::template ListBucket<Val> >::other) {}
void other3(Alloc::template rebind<int>) {}
%}

View file

@ -1430,7 +1430,7 @@ static void mark_nodes_as_extend(Node *n) {
%type <dtype> definetype def_args etype default_delete deleted_definition explicit_default;
%type <dtype> expr exprnum exprcompound valexpr;
%type <id> ename ;
%type <id> template_decl;
%type <id> less_valparms_greater;
%type <str> type_qualifier ;
%type <id> type_qualifier_raw;
%type <id> idstring idstringopt;
@ -1441,7 +1441,7 @@ static void mark_nodes_as_extend(Node *n) {
%type <decl> declarator direct_declarator notso_direct_declarator parameter_declarator plain_declarator;
%type <decl> abstract_declarator direct_abstract_declarator ctor_end;
%type <tmap> typemap_type;
%type <str> idcolon idcolontail idcolonnt idcolontailnt idtemplate stringbrace stringbracesemi;
%type <str> idcolon idcolontail idcolonnt idcolontailnt idtemplate idtemplatetemplate stringbrace stringbracesemi;
%type <id> string stringnum wstring;
%type <tparms> template_parms;
%type <dtype> cpp_end cpp_vend;
@ -3073,7 +3073,9 @@ initializer : def_args {
cpp_alternate_rettype : primitive_type { $$ = $1; }
| TYPE_BOOL { $$ = $1; }
| TYPE_VOID { $$ = $1; }
/*
| TYPE_TYPEDEF template_decl { $$ = NewStringf("%s%s",$1,$2); }
*/
| TYPE_RAW { $$ = $1; }
| idcolon { $$ = $1; }
| decltype { $$ = $1; }
@ -4646,7 +4648,9 @@ anon_bitfield_type : primitive_type { $$ = $1;
}
| TYPE_BOOL { $$ = $1; }
| TYPE_VOID { $$ = $1; }
/*
| TYPE_TYPEDEF template_decl { $$ = NewStringf("%s%s",$1,$2); }
*/
| TYPE_RAW { $$ = $1; }
| idcolon {
@ -5593,7 +5597,9 @@ type_right : primitive_type { $$ = $1;
}
| TYPE_BOOL { $$ = $1; }
| TYPE_VOID { $$ = $1; }
/*
| TYPE_TYPEDEF template_decl { $$ = NewStringf("%s%s",$1,$2); }
*/
| c_enum_key idcolon { $$ = NewStringf("enum %s", $2); }
| TYPE_RAW { $$ = $1; }
@ -6419,14 +6425,13 @@ mem_initializer : idcolon LPAREN {
}
;
template_decl : LESSTHAN valparms GREATERTHAN {
less_valparms_greater : LESSTHAN valparms GREATERTHAN {
String *s = NewStringEmpty();
SwigType_add_template(s,$2);
$$ = Char(s);
scanner_last_id(1);
}
| empty { $$ = (char*)""; }
;
}
;
/* Identifiers including the C++11 identifiers with special meaning */
identifier : ID { $$ = $1; }
@ -6448,29 +6453,32 @@ idcolon : idtemplate idcolontail {
if (!$$) $$ = NewStringf("%s%s", $1,$2);
Delete($2);
}
| NONID DCOLON idtemplate idcolontail {
| NONID DCOLON idtemplatetemplate idcolontail {
$$ = NewStringf("::%s%s",$3,$4);
Delete($4);
}
| idtemplate {
$$ = NewString($1);
}
| NONID DCOLON idtemplate {
| NONID DCOLON idtemplatetemplate {
$$ = NewStringf("::%s",$3);
}
| OPERATOR template_decl {
$$ = NewStringf("%s%s",$1,$2);
| OPERATOR {
$$ = NewStringf("%s", $1);
}
| OPERATOR less_valparms_greater {
$$ = NewStringf("%s%s", $1, $2);
}
| NONID DCOLON OPERATOR {
$$ = NewStringf("::%s",$3);
}
;
idcolontail : DCOLON idtemplate idcolontail {
idcolontail : DCOLON idtemplatetemplate idcolontail {
$$ = NewStringf("::%s%s",$2,$3);
Delete($3);
}
| DCOLON idtemplate {
| DCOLON idtemplatetemplate {
$$ = NewStringf("::%s",$2);
}
| DCOLON OPERATOR {
@ -6486,12 +6494,20 @@ idcolontail : DCOLON idtemplate idcolontail {
;
idtemplate : identifier template_decl {
$$ = NewStringf("%s%s",$1,$2);
/* if (Len($2)) {
scanner_last_id(1);
} */
}
idtemplate : identifier {
$$ = NewStringf("%s", $1);
}
| identifier less_valparms_greater {
$$ = NewStringf("%s%s", $1, $2);
}
;
idtemplatetemplate : idtemplate {
$$ = $1;
}
| TEMPLATE identifier less_valparms_greater {
$$ = NewStringf("%s%s", $2, $3);
}
;
/* Identifier, but no templates */