Added support for C++0x alternate function syntax.

Added testcase.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-matevz@11414 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Matevz Jekovec 2009-07-17 11:17:01 +00:00
commit 03db5b4991
7 changed files with 53 additions and 16 deletions

View file

@ -406,7 +406,8 @@ CPP0X_TEST_CASES = \
cpp0x_static_assert \ cpp0x_static_assert \
cpp0x_template_explicit \ cpp0x_template_explicit \
cpp0x_thread_local \ cpp0x_thread_local \
cpp0x_uniform_initialization cpp0x_uniform_initialization \
cpp0x_alternate_function_syntax
# cpp0x_smart_pointers # not supported by standard library yet # cpp0x_smart_pointers # not supported by standard library yet
# cpp0x_constexpr # not supported by any compilers yet # cpp0x_constexpr # not supported by any compilers yet

View file

@ -0,0 +1,11 @@
%module cpp0x_alternate_function_syntax
%inline %{
struct SomeStruct {
auto FuncName(int x, int y) -> int;
};
auto SomeStruct::FuncName(int x, int y) -> int {
return x + y;
}
%}

View file

@ -340,6 +340,8 @@ int yylook(void) {
return GREATERTHANOREQUALTO; return GREATERTHANOREQUALTO;
case SWIG_TOKEN_RSHIFT: case SWIG_TOKEN_RSHIFT:
return RSHIFT; return RSHIFT;
case SWIG_TOKEN_ARROW:
return ARROW;
case SWIG_TOKEN_PERIOD: case SWIG_TOKEN_PERIOD:
return PERIOD; return PERIOD;
case SWIG_TOKEN_MODULO: case SWIG_TOKEN_MODULO:
@ -593,6 +595,10 @@ int yylex(void) {
yylval.type = NewSwigType(T_BOOL); yylval.type = NewSwigType(T_BOOL);
return (TYPE_BOOL); return (TYPE_BOOL);
} }
if (strcmp(yytext, "auto") == 0) {
yylval.type = NewSwigType(T_AUTO);
return (TYPE_AUTO);
}
/* Non ISO (Windows) C extensions */ /* Non ISO (Windows) C extensions */
if (strcmp(yytext, "__int8") == 0) { if (strcmp(yytext, "__int8") == 0) {

View file

@ -1486,7 +1486,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) {
%token <str> CHARCONST %token <str> CHARCONST
%token <dtype> NUM_INT NUM_FLOAT NUM_UNSIGNED NUM_LONG NUM_ULONG NUM_LONGLONG NUM_ULONGLONG %token <dtype> NUM_INT NUM_FLOAT NUM_UNSIGNED NUM_LONG NUM_ULONG NUM_LONGLONG NUM_ULONGLONG
%token <ivalue> TYPEDEF %token <ivalue> TYPEDEF
%token <type> TYPE_INT TYPE_UNSIGNED TYPE_SHORT TYPE_LONG TYPE_FLOAT TYPE_DOUBLE TYPE_CHAR TYPE_WCHAR TYPE_VOID TYPE_SIGNED TYPE_BOOL TYPE_COMPLEX TYPE_TYPEDEF TYPE_RAW TYPE_NON_ISO_INT8 TYPE_NON_ISO_INT16 TYPE_NON_ISO_INT32 TYPE_NON_ISO_INT64 %token <type> TYPE_INT TYPE_UNSIGNED TYPE_SHORT TYPE_LONG TYPE_FLOAT TYPE_DOUBLE TYPE_CHAR TYPE_WCHAR TYPE_VOID TYPE_SIGNED TYPE_BOOL TYPE_COMPLEX TYPE_TYPEDEF TYPE_RAW TYPE_NON_ISO_INT8 TYPE_NON_ISO_INT16 TYPE_NON_ISO_INT32 TYPE_NON_ISO_INT64 TYPE_AUTO
%token LPAREN RPAREN COMMA SEMI EXTERN INIT LBRACE RBRACE PERIOD %token LPAREN RPAREN COMMA SEMI EXTERN INIT LBRACE RBRACE PERIOD
%token CONST_QUAL VOLATILE REGISTER STRUCT UNION EQUAL SIZEOF MODULE LBRACKET RBRACKET %token CONST_QUAL VOLATILE REGISTER STRUCT UNION EQUAL SIZEOF MODULE LBRACKET RBRACKET
%token ILLEGAL CONSTANT %token ILLEGAL CONSTANT
@ -1501,6 +1501,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) {
%token WARN %token WARN
%token LESSTHAN GREATERTHAN MODULO DELETE_KW %token LESSTHAN GREATERTHAN MODULO DELETE_KW
%token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO %token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO
%token ARROW
%token QUESTIONMARK %token QUESTIONMARK
%token TYPES PARMS %token TYPES PARMS
%token NONID DSTAR DCNOT %token NONID DSTAR DCNOT
@ -1534,7 +1535,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) {
%type <node> types_directive template_directive warn_directive ; %type <node> types_directive template_directive warn_directive ;
/* C declarations */ /* C declarations */
%type <node> c_declaration c_decl c_decl_tail c_enum_decl c_enum_forward_decl c_constructor_decl ; %type <node> c_declaration c_decl c_decl_tail c_enum_decl c_enum_forward_decl c_constructor_decl c_rettype ;
%type <node> enumlist edecl; %type <node> enumlist edecl;
/* C++ declarations */ /* C++ declarations */
@ -2939,25 +2940,25 @@ c_declaration : c_decl {
A C global declaration of some kind (may be variable, function, typedef, etc.) A C global declaration of some kind (may be variable, function, typedef, etc.)
------------------------------------------------------------ */ ------------------------------------------------------------ */
c_decl : storage_class type declarator initializer c_decl_tail { c_decl : storage_class type declarator c_rettype initializer c_decl_tail {
$$ = new_node("cdecl"); $$ = new_node("cdecl");
if ($4.qualifier) SwigType_push($3.type,$4.qualifier); if ($5.qualifier) SwigType_push($3.type,$5.qualifier);
Setattr($$,"type",$2); Setattr($$,"type",$2);
Setattr($$,"storage",$1); Setattr($$,"storage",$1);
Setattr($$,"name",$3.id); Setattr($$,"name",$3.id);
Setattr($$,"decl",$3.type); Setattr($$,"decl",$3.type);
Setattr($$,"parms",$3.parms); Setattr($$,"parms",$3.parms);
Setattr($$,"value",$4.val); Setattr($$,"value",$5.val);
Setattr($$,"throws",$4.throws); Setattr($$,"throws",$5.throws);
Setattr($$,"throw",$4.throwf); Setattr($$,"throw",$5.throwf);
if (!$5) { if (!$6) {
if (Len(scanner_ccode)) { if (Len(scanner_ccode)) {
String *code = Copy(scanner_ccode); String *code = Copy(scanner_ccode);
Setattr($$,"code",code); Setattr($$,"code",code);
Delete(code); Delete(code);
} }
} else { } else {
Node *n = $5; Node *n = $6;
/* Inherit attributes */ /* Inherit attributes */
while (n) { while (n) {
String *type = Copy($2); String *type = Copy($2);
@ -2967,8 +2968,8 @@ c_decl : storage_class type declarator initializer c_decl_tail {
Delete(type); Delete(type);
} }
} }
if ($4.bitfield) { if ($5.bitfield) {
Setattr($$,"bitfield", $4.bitfield); Setattr($$,"bitfield", $5.bitfield);
} }
/* Look for "::" declarations (ignored) */ /* Look for "::" declarations (ignored) */
@ -2982,22 +2983,33 @@ c_decl : storage_class type declarator initializer c_decl_tail {
String *lstr = Swig_scopename_last($3.id); String *lstr = Swig_scopename_last($3.id);
Setattr($$,"name",lstr); Setattr($$,"name",lstr);
Delete(lstr); Delete(lstr);
set_nextSibling($$,$5); set_nextSibling($$,$6);
} else { } else {
Delete($$); Delete($$);
$$ = $5; $$ = $6;
} }
Delete(p); Delete(p);
} else { } else {
Delete($$); Delete($$);
$$ = $5; $$ = $6;
} }
} else { } else {
set_nextSibling($$,$5); set_nextSibling($$,$6);
} }
} }
; ;
/* Alternate function syntax:
auto funcName(int x, int y) -> int; */
c_rettype : ARROW type {
$$ = new_node("rettype");
Setattr($$,"type",$2);
}
| empty {
$$ = 0;
}
;
/* Allow lists of variables and functions to be built up */ /* Allow lists of variables and functions to be built up */
c_decl_tail : SEMI { c_decl_tail : SEMI {
@ -5209,6 +5221,7 @@ type_right : primitive_type { $$ = $1;
} }
| TYPE_BOOL { $$ = $1; } | TYPE_BOOL { $$ = $1; }
| TYPE_VOID { $$ = $1; } | TYPE_VOID { $$ = $1; }
| TYPE_AUTO { $$ = $1; }
| TYPE_TYPEDEF template_decl { $$ = NewStringf("%s%s",$1,$2); } | TYPE_TYPEDEF template_decl { $$ = NewStringf("%s%s",$1,$2); }
| ENUM idcolon { $$ = NewStringf("enum %s", $2); } | ENUM idcolon { $$ = NewStringf("enum %s", $2); }
| TYPE_RAW { $$ = $1; } | TYPE_RAW { $$ = $1; }

View file

@ -141,6 +141,9 @@ SwigType *NewSwigType(int t) {
case T_VOID: case T_VOID:
return NewString("void"); return NewString("void");
break; break;
case T_AUTO:
return NewString("auto");
break;
default: default:
break; break;
} }

View file

@ -73,6 +73,7 @@ extern "C" {
#define T_FLTCPLX 23 #define T_FLTCPLX 23
#define T_DBLCPLX 24 #define T_DBLCPLX 24
#define T_NUMERIC 25 #define T_NUMERIC 25
#define T_AUTO 26
#define T_COMPLEX T_DBLCPLX #define T_COMPLEX T_DBLCPLX

View file

@ -1257,6 +1257,8 @@ int SwigType_type(SwigType *t) {
return T_ULONGLONG; return T_ULONGLONG;
if (strncmp(c, "enum ", 5) == 0) if (strncmp(c, "enum ", 5) == 0)
return T_INT; return T_INT;
if (strcmp(c, "auto") == 0)
return T_AUTO;
if (strcmp(c, "v(...)") == 0) if (strcmp(c, "v(...)") == 0)
return T_VARARGS; return T_VARARGS;