Added support for user-defined string literals.

Added testcase cpp0x_userdefined_literals.i


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-matevz@11494 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Matevz Jekovec 2009-08-04 00:51:21 +00:00
commit 8a4efbc6d3
4 changed files with 53 additions and 2 deletions

View file

@ -410,7 +410,8 @@ CPP0X_TEST_CASES = \
cpp0x_strongly_typed_enumerations \
cpp0x_rvalue_reference \
cpp0x_variadic_templates \
cpp0x_alternate_function_syntax
cpp0x_alternate_function_syntax \
cpp0x_userdefined_literals
# 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

View file

@ -0,0 +1,24 @@
/* This testcase checks whether Swig correctly parses the user-defined literals
for the string introduced in C++0x. */
%module cpp0x_userdefined_literals
%inline %{
#include <iostream>
struct OutputType {
int val;
OutputType(int v) { v=val; }
};
struct TT {
OutputType operator << (const char * string_values, size_t num_chars) { return OutputType(100); }
OutputType operator "" (const char * string_values, size_t num_chars) { return OutputType(100); }
OutputType operator "" _mySuffix1(const char * string_values, size_t num_chars) { return OutputType(100); }
OutputType operator "" _mySuffix2(const wchar_t * string_values, size_t num_chars) { return OutputType(200); }
OutputType operator "" _mySuffix3(const char16_t * string_values, size_t num_chars) { return OutputType(300); }
OutputType operator "" _mySuffix4(const char32_t * string_values, size_t num_chars) { return OutputType(400); }
OutputType operator "" _mySuffix5(int value) /* cooked version - ie. atoi() of string */ { return OutputType(500); }
};
%}

View file

@ -688,6 +688,11 @@ int yylex(void) {
yylval.str = s;
return OPERATOR;
}
} else if (nexttok == SWIG_TOKEN_STRING) {
/* Operator "" or user-defined string literal ""_suffix */
Append(s,"\"\"");
yylval.str = s;
return OPERATOR;
} else if (nexttok == SWIG_TOKEN_ID) {
/* We have an identifier. This could be any number of things. It could be a named version of
an operator (e.g., 'and_eq') or it could be a conversion operator. To deal with this, we're

View file

@ -5145,7 +5145,7 @@ direct_declarator : idcolon {
$$.parms = 0;
$$.have_parms = 0;
}
| NOT idcolon {
$$.id = Char(NewStringf("~%s",$2));
$$.type = 0;
@ -5227,6 +5227,27 @@ direct_declarator : idcolon {
Delete($$.type);
$$.type = t;
}
}
/* User-defined string literals. eg.
int operator""_mySuffix(const char* val, int length) {...} */
/* This produces one S/R conflict. */
| OPERATOR ID LPAREN parms RPAREN {
SwigType *t;
Append($1, Char($2));
$$.id = Char($1);
t = NewStringEmpty();
SwigType_add_function(t,$4);
if (!$$.have_parms) {
$$.parms = $4;
$$.have_parms = 1;
}
if (!$$.type) {
$$.type = t;
} else {
SwigType_push(t, $$.type);
Delete($$.type);
$$.type = t;
}
}
;