From ba2c8cff409890fa38aec7d0f4b4fa2e82a167bf Mon Sep 17 00:00:00 2001 From: Matevz Jekovec Date: Mon, 3 Aug 2009 15:03:41 +0000 Subject: [PATCH] Added syntax for lambda expressions and closures introduced in C++0x. Added testcase cpp0x_lambda_functions.i. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-matevz@11491 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/common.mk | 1 + Examples/test-suite/cpp0x_lambda_functions.i | 34 ++++++++++++++++++++ Source/CParse/parser.y | 14 +++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Examples/test-suite/cpp0x_lambda_functions.i diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 1e27b54b0..22bf646c3 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -412,6 +412,7 @@ CPP0X_TEST_CASES = \ cpp0x_variadic_templates \ cpp0x_alternate_function_syntax # cpp0x_hash_types # not fully implemented yet +# cpp0x_lambda_functions # not supported by GCC or MSVC yet # cpp0x_null_pointer_constant # not supported by any compilers yet # cpp0x_unrestricted_unions # not supported by any compilers yet # cpp0x_smart_pointers # not supported by standard library yet diff --git a/Examples/test-suite/cpp0x_lambda_functions.i b/Examples/test-suite/cpp0x_lambda_functions.i new file mode 100644 index 000000000..7719b59f4 --- /dev/null +++ b/Examples/test-suite/cpp0x_lambda_functions.i @@ -0,0 +1,34 @@ +/* This testcase checks whether Swig correctly parses the lambda expressions + and closure syntax introduced in C++0x. + Swig supports only lambda syntax and doesn't produce any wrapper code for + this. +*/ +%module cpp0x_lambda_functions + +%inline %{ +struct A { + /* Defined lambda function with return value. */ + auto lambda1 = [](int x, int y) -> int { return x+y; }; + + /* Defined lambda function without return value. + Return value is calculated by compiler, if the function contains a + single statement "return expr;". */ + auto lambda2 = [](int x, int y) { return x+y; }; +}; + +int runLambda1() { + A myA; + return myA.lambda1(5,6); +} + +int runLambda2() { + A myA; + return myA.lambda2(5,6); +} + +/* Inline defined lambda function. */ +int runLambda3() { + auto myLambda = [](int x, int y) { return x+y; }; + return myLambda(5,6); +} +%} diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 01ed40350..6335b03f9 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -1536,7 +1536,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_keyword c_enum_inherit c_enum_decl c_enum_forward_decl c_constructor_decl ; +%type c_declaration c_decl c_decl_tail c_enum_keyword c_enum_inherit c_enum_decl c_enum_forward_decl c_constructor_decl c_lambda_decl c_lambda_decl_front ; %type enumlist edecl; /* C++ declarations */ @@ -2941,6 +2941,7 @@ c_declaration : c_decl { appendChild($$,firstChild($5)); } } + | c_lambda_decl { Swig_warning("Swig doesn't produce wrapper code for lambda expressions and closures yet.") $$ = $1; } ; /* ------------------------------------------------------------ @@ -3129,6 +3130,17 @@ cpp_alternate_rettype : primitive_type { $$ = $1; } | idcolon { $$ = $1; } ; +/* Lambda function syntax introduced in C++0x. + auto myFunc = [](int x, int y) -> int { return x+y; } + OR + auto myFunc = [](int x, int y) { return x+y; } +*/ +c_lambda_decl : c_lambda_decl_front LPAREN parms RPAREN LBRACE { skip_balanced('{','}'); } SEMI { $$ = 0; } + | c_lambda_decl_front LPAREN parms RPAREN ARROW type LBRACE { skip_balanced('{','}'); } SEMI { $$ = 0; } + +c_lambda_decl_front : storage_class AUTO idcolon EQUAL LBRACKET { skip_balanced('[',']'); $$ = 0; } + + /* ------------------------------------------------------------ enum or