Allow templates to be instantiated using negative numbers and constant

expressions (which fixes SF bug #956282 and several others).


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9309 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2006-09-20 11:20:52 +00:00
commit 916a265191
3 changed files with 47 additions and 43 deletions

View file

@ -1,6 +1,14 @@
Version 1.3.30 (in progress) Version 1.3.30 (in progress)
============================ ============================
09/20/2006: olly
[ALL] Templates can now be instantiated using negative numbers and
constant expressions, e.g.:
template<int q> class x {};
%template(x_minus1) x<-1>;
%template(x_1plus2) x<1+2>;
09/20/2006: olly 09/20/2006: olly
[ALL] Treat a nested class definition as a forward declaration rather [ALL] Treat a nested class definition as a forward declaration rather
than ignoring it completely, so that we generate correct code for than ignoring it completely, so that we generate correct code for

View file

@ -23,7 +23,8 @@ class X {};
%template(X_1) X<1>; %template(X_1) X<1>;
%template(X_m1) X<-1>; %template(X_m1) X<-1>;
%template(X_1plus2) X<1+2>;
%template(X_1024div8) X<1024/8>;
// bug 1338527 // bug 1338527

View file

@ -1440,7 +1440,7 @@ static void default_arguments(Node *n) {
%type <type> type rawtype type_right ; %type <type> type rawtype type_right ;
%type <bases> base_list inherit raw_inherit; %type <bases> base_list inherit raw_inherit;
%type <dtype> definetype def_args etype; %type <dtype> definetype def_args etype;
%type <dtype> expr exprnum exprcompound ; %type <dtype> expr exprnum exprcompound valexpr;
%type <id> ename ; %type <id> ename ;
%type <id> template_decl; %type <id> template_decl;
%type <str> type_qualifier ; %type <str> type_qualifier ;
@ -4434,20 +4434,12 @@ valparm : parm {
} }
} }
| exprnum { | valexpr {
$$ = NewParm(0,0); $$ = NewParm(0,0);
Setfile($$,cparse_file); Setfile($$,cparse_file);
Setline($$,cparse_line); Setline($$,cparse_line);
Setattr($$,k_value,$1.val); Setattr($$,k_value,$1.val);
} }
| STRING {
String *v = NewString($1);
$$ = NewParm(0,0);
Setfile($$,cparse_file);
Setline($$,cparse_line);
Setattr($$,k_value,v);
Delete(v);
}
; ;
def_args : EQUAL definetype { def_args : EQUAL definetype {
@ -5272,7 +5264,27 @@ etype : expr {
this does allow us to parse many constant declarations. this does allow us to parse many constant declarations.
*/ */
expr : exprnum { $$ = $1; } expr : valexpr { $$ = $1; }
| type {
Node *n;
$$.val = $1;
$$.type = T_INT;
/* Check if value is in scope */
n = Swig_symbol_clookup($1,0);
if (n) {
/* A band-aid for enum values used in expressions. */
if (Strcmp(nodeType(n),"enumitem") == 0) {
String *q = Swig_symbol_qualified(n);
if (q) {
$$.val = NewStringf("%s::%s", q, Getattr(n,k_name));
Delete(q);
}
}
}
}
;
valexpr : exprnum { $$ = $1; }
| string { | string {
$$.val = NewString($1); $$.val = NewString($1);
$$.type = T_STRING; $$.type = T_STRING;
@ -5295,23 +5307,6 @@ expr : exprnum { $$ = $1; }
$$.throws = 0; $$.throws = 0;
$$.throwf = 0; $$.throwf = 0;
} }
| type {
Node *n;
$$.val = $1;
$$.type = T_INT;
/* Check if value is in scope */
n = Swig_symbol_clookup($1,0);
if (n) {
/* A band-aid for enum values used in expressions. */
if (Strcmp(nodeType(n),"enumitem") == 0) {
String *q = Swig_symbol_qualified(n);
if (q) {
$$.val = NewStringf("%s::%s", q, Getattr(n,k_name));
Delete(q);
}
}
}
}
/* grouping */ /* grouping */
| LPAREN expr RPAREN %prec CAST { | LPAREN expr RPAREN %prec CAST {