From 03db5b499151aa9c544cd97f5cbe3a3027045a8a Mon Sep 17 00:00:00 2001 From: Matevz Jekovec Date: Fri, 17 Jul 2009 11:17:01 +0000 Subject: [PATCH] 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 --- Examples/test-suite/common.mk | 3 +- .../cpp0x_alternate_function_syntax.i | 11 +++++ Source/CParse/cscanner.c | 6 +++ Source/CParse/parser.y | 43 ++++++++++++------- Source/Swig/stype.c | 3 ++ Source/Swig/swig.h | 1 + Source/Swig/typesys.c | 2 + 7 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 Examples/test-suite/cpp0x_alternate_function_syntax.i diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index f9f0de807..4ab20575a 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -406,7 +406,8 @@ CPP0X_TEST_CASES = \ cpp0x_static_assert \ cpp0x_template_explicit \ cpp0x_thread_local \ - cpp0x_uniform_initialization + cpp0x_uniform_initialization \ + cpp0x_alternate_function_syntax # cpp0x_smart_pointers # not supported by standard library yet # cpp0x_constexpr # not supported by any compilers yet diff --git a/Examples/test-suite/cpp0x_alternate_function_syntax.i b/Examples/test-suite/cpp0x_alternate_function_syntax.i new file mode 100644 index 000000000..ed3cb6729 --- /dev/null +++ b/Examples/test-suite/cpp0x_alternate_function_syntax.i @@ -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; +} +%} diff --git a/Source/CParse/cscanner.c b/Source/CParse/cscanner.c index 6ad3b2c5a..1fe0735db 100644 --- a/Source/CParse/cscanner.c +++ b/Source/CParse/cscanner.c @@ -340,6 +340,8 @@ int yylook(void) { return GREATERTHANOREQUALTO; case SWIG_TOKEN_RSHIFT: return RSHIFT; + case SWIG_TOKEN_ARROW: + return ARROW; case SWIG_TOKEN_PERIOD: return PERIOD; case SWIG_TOKEN_MODULO: @@ -593,6 +595,10 @@ int yylex(void) { yylval.type = NewSwigType(T_BOOL); return (TYPE_BOOL); } + if (strcmp(yytext, "auto") == 0) { + yylval.type = NewSwigType(T_AUTO); + return (TYPE_AUTO); + } /* Non ISO (Windows) C extensions */ if (strcmp(yytext, "__int8") == 0) { diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 35b625b0c..093c4d315 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -1486,7 +1486,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) { %token CHARCONST %token NUM_INT NUM_FLOAT NUM_UNSIGNED NUM_LONG NUM_ULONG NUM_LONGLONG NUM_ULONGLONG %token TYPEDEF -%token 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_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 CONST_QUAL VOLATILE REGISTER STRUCT UNION EQUAL SIZEOF MODULE LBRACKET RBRACKET %token ILLEGAL CONSTANT @@ -1501,6 +1501,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) { %token WARN %token LESSTHAN GREATERTHAN MODULO DELETE_KW %token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO +%token ARROW %token QUESTIONMARK %token TYPES PARMS %token NONID DSTAR DCNOT @@ -1534,7 +1535,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) { %type types_directive template_directive warn_directive ; /* C declarations */ -%type c_declaration c_decl c_decl_tail c_enum_decl c_enum_forward_decl c_constructor_decl ; +%type c_declaration c_decl c_decl_tail c_enum_decl c_enum_forward_decl c_constructor_decl c_rettype ; %type enumlist edecl; /* C++ declarations */ @@ -2939,25 +2940,25 @@ c_declaration : c_decl { 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"); - if ($4.qualifier) SwigType_push($3.type,$4.qualifier); + if ($5.qualifier) SwigType_push($3.type,$5.qualifier); Setattr($$,"type",$2); Setattr($$,"storage",$1); Setattr($$,"name",$3.id); Setattr($$,"decl",$3.type); Setattr($$,"parms",$3.parms); - Setattr($$,"value",$4.val); - Setattr($$,"throws",$4.throws); - Setattr($$,"throw",$4.throwf); - if (!$5) { + Setattr($$,"value",$5.val); + Setattr($$,"throws",$5.throws); + Setattr($$,"throw",$5.throwf); + if (!$6) { if (Len(scanner_ccode)) { String *code = Copy(scanner_ccode); Setattr($$,"code",code); Delete(code); } } else { - Node *n = $5; + Node *n = $6; /* Inherit attributes */ while (n) { String *type = Copy($2); @@ -2967,8 +2968,8 @@ c_decl : storage_class type declarator initializer c_decl_tail { Delete(type); } } - if ($4.bitfield) { - Setattr($$,"bitfield", $4.bitfield); + if ($5.bitfield) { + Setattr($$,"bitfield", $5.bitfield); } /* 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); Setattr($$,"name",lstr); Delete(lstr); - set_nextSibling($$,$5); + set_nextSibling($$,$6); } else { Delete($$); - $$ = $5; + $$ = $6; } Delete(p); } else { Delete($$); - $$ = $5; + $$ = $6; } } 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 */ c_decl_tail : SEMI { @@ -5209,6 +5221,7 @@ type_right : primitive_type { $$ = $1; } | TYPE_BOOL { $$ = $1; } | TYPE_VOID { $$ = $1; } + | TYPE_AUTO { $$ = $1; } | TYPE_TYPEDEF template_decl { $$ = NewStringf("%s%s",$1,$2); } | ENUM idcolon { $$ = NewStringf("enum %s", $2); } | TYPE_RAW { $$ = $1; } diff --git a/Source/Swig/stype.c b/Source/Swig/stype.c index 8a7700bec..3b56e98cb 100644 --- a/Source/Swig/stype.c +++ b/Source/Swig/stype.c @@ -141,6 +141,9 @@ SwigType *NewSwigType(int t) { case T_VOID: return NewString("void"); break; + case T_AUTO: + return NewString("auto"); + break; default: break; } diff --git a/Source/Swig/swig.h b/Source/Swig/swig.h index 2b2c797c9..7eb9ef210 100644 --- a/Source/Swig/swig.h +++ b/Source/Swig/swig.h @@ -73,6 +73,7 @@ extern "C" { #define T_FLTCPLX 23 #define T_DBLCPLX 24 #define T_NUMERIC 25 +#define T_AUTO 26 #define T_COMPLEX T_DBLCPLX diff --git a/Source/Swig/typesys.c b/Source/Swig/typesys.c index 2562e12f8..9b11a08e5 100644 --- a/Source/Swig/typesys.c +++ b/Source/Swig/typesys.c @@ -1257,6 +1257,8 @@ int SwigType_type(SwigType *t) { return T_ULONGLONG; if (strncmp(c, "enum ", 5) == 0) return T_INT; + if (strcmp(c, "auto") == 0) + return T_AUTO; if (strcmp(c, "v(...)") == 0) return T_VARARGS;