C++11 alternate syntax for function declarations improvements

Functions can now be declared const/noexcept/final etc
This commit is contained in:
William S Fulton 2017-03-06 21:06:51 +00:00
commit 0ccef1424a
3 changed files with 38 additions and 27 deletions

View file

@ -6,13 +6,15 @@
struct SomeStruct {
int addNormal(int x, int y);
auto addAlternate(int x, int y) -> int;
auto addAlternateConst(int x, int y) const -> int;
auto addAlternateNoExcept(int x, int y) noexcept -> int;
auto addAlternateConstNoExcept(int x, int y) const noexcept -> int;
};
auto SomeStruct::addAlternate(int x, int y) -> int {
return x + y;
}
int SomeStruct::addNormal(int x, int y) { return x + y; }
auto SomeStruct::addAlternate(int x, int y) -> int { return x + y; }
auto SomeStruct::addAlternateConst(int x, int y) const -> int { return x + y; }
auto SomeStruct::addAlternateNoExcept(int x, int y) noexcept -> int { return x + y; }
auto SomeStruct::addAlternateConstNoExcept(int x, int y) const noexcept -> int { return x + y; }
int SomeStruct::addNormal(int x, int y) {
return x + y;
}
%}

View file

@ -4,11 +4,20 @@ a = cpp11_alternate_function_syntax.SomeStruct()
res = a.addNormal(4, 5)
if res != 9:
raise RuntimeError, ("SomeStruct::addNormal(4,5) returns ",
res, " should be 9.")
raise RuntimeError, ("SomeStruct::addNormal(4,5) returns ", res, " should be 9.")
res = a.addAlternate(4, 5)
if res != 9:
raise RuntimeError, ("SomeStruct::addAlternate(4,5) returns ",
res, " should be 9.")
raise RuntimeError, ("SomeStruct::addAlternate(4,5) returns ", res, " should be 9.")
res = a.addAlternateConst(4, 5)
if res != 9:
raise RuntimeError, ("SomeStruct::addAlternateConst(4,5) returns ", res, " should be 9.")
res = a.addAlternateNoExcept(4, 5)
if res != 9:
raise RuntimeError, ("SomeStruct::addAlternateNoExcept(4,5) returns ", res, " should be 9.")
res = a.addAlternateConstNoExcept(4, 5)
if res != 9:
raise RuntimeError, ("SomeStruct::addAlternateConstNoExcept(4,5) returns ", res, " should be 9.")

View file

@ -3067,36 +3067,36 @@ c_decl : storage_class type declarator initializer c_decl_tail {
}
/* Alternate function syntax introduced in C++11:
auto funcName(int x, int y) -> int; */
| storage_class AUTO declarator ARROW cpp_alternate_rettype initializer c_decl_tail {
| storage_class AUTO declarator cpp_const ARROW cpp_alternate_rettype initializer c_decl_tail {
$$ = new_node("cdecl");
if ($6.qualifier) SwigType_push($3.type,$6.qualifier);
Setattr($$,"type",$5);
if ($4.qualifier) SwigType_push($3.type, $4.qualifier);
Setattr($$,"type",$6);
Setattr($$,"storage",$1);
Setattr($$,"name",$3.id);
Setattr($$,"decl",$3.type);
Setattr($$,"parms",$3.parms);
Setattr($$,"value",$6.val);
Setattr($$,"throws",$6.throws);
Setattr($$,"throw",$6.throwf);
Setattr($$,"noexcept",$6.nexcept);
if (!$7) {
Setattr($$,"value",$4.val);
Setattr($$,"throws",$4.throws);
Setattr($$,"throw",$4.throwf);
Setattr($$,"noexcept",$4.nexcept);
if (!$8) {
if (Len(scanner_ccode)) {
String *code = Copy(scanner_ccode);
Setattr($$,"code",code);
Delete(code);
}
} else {
Node *n = $7;
Node *n = $8;
while (n) {
String *type = Copy($5);
String *type = Copy($6);
Setattr(n,"type",type);
Setattr(n,"storage",$1);
n = nextSibling(n);
Delete(type);
}
}
if ($6.bitfield) {
Setattr($$,"bitfield", $6.bitfield);
if ($4.bitfield) {
Setattr($$,"bitfield", $4.bitfield);
}
if (Strstr($3.id,"::")) {
@ -3107,18 +3107,18 @@ c_decl : storage_class type declarator initializer c_decl_tail {
String *lstr = Swig_scopename_last($3.id);
Setattr($$,"name",lstr);
Delete(lstr);
set_nextSibling($$,$7);
set_nextSibling($$, $8);
} else {
Delete($$);
$$ = $7;
$$ = $8;
}
Delete(p);
} else {
Delete($$);
$$ = $7;
$$ = $8;
}
} else {
set_nextSibling($$,$7);
set_nextSibling($$, $8);
}
}
;