Some refinement of the type system

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@512 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-07-03 04:06:04 +00:00
commit 7c984700de
3 changed files with 398 additions and 154 deletions

View file

@ -717,7 +717,7 @@ tm_tail : COMMA tm_parm tm_tail {
tm_parm : type tm_name { tm_parm : type tm_name {
$$ = NewHash(); $$ = NewHash();
if ($2.array) { if ($2.array) {
StringType_push($1,$2.array); SwigType_push($1,$2.array);
} }
Setattr($$,ATTR_TYPE,$1); Setattr($$,ATTR_TYPE,$1);
if ($2.name) if ($2.name)
@ -727,9 +727,9 @@ tm_parm : type tm_name {
} }
| type stars tm_name { | type stars tm_name {
$$ = NewHash(); $$ = NewHash();
StringType_push($1,$2); SwigType_push($1,$2);
if ($3.array) { if ($3.array) {
StringType_push($1,$3.array); SwigType_push($1,$3.array);
} }
Setattr($$,ATTR_TYPE,$1); Setattr($$,ATTR_TYPE,$1);
if ($3.name) if ($3.name)
@ -739,9 +739,9 @@ tm_parm : type tm_name {
} }
| type AND tm_name { | type AND tm_name {
$$ = NewHash(); $$ = NewHash();
StringType_add_reference($1); SwigType_add_reference($1);
if ($3.array) { if ($3.array) {
StringType_push($1,$3.array); SwigType_push($1,$3.array);
} }
Setattr($$,ATTR_TYPE,$1); Setattr($$,ATTR_TYPE,$1);
if ($3.name) if ($3.name)
@ -801,8 +801,8 @@ variable_decl : storage_spec type declaration array2 def_args stail {
DOH *o, *t; DOH *o, *t;
$$ = new_node(TAG_VARIABLE,Getfile($3.id),Getline($3.id)); $$ = new_node(TAG_VARIABLE,Getfile($3.id),Getline($3.id));
t = Copy($2); t = Copy($2);
StringType_push(t,$3.decl); SwigType_push(t,$3.decl);
StringType_push(t,$4); SwigType_push(t,$4);
Setattr($$,ATTR_NAME,$3.id); Setattr($$,ATTR_NAME,$3.id);
Setattr($$,ATTR_TYPE,t); Setattr($$,ATTR_TYPE,t);
if ($1.ivalue) { if ($1.ivalue) {
@ -816,7 +816,7 @@ variable_decl : storage_spec type declaration array2 def_args stail {
o = $6; o = $6;
while (o) { while (o) {
t = Copy($2); t = Copy($2);
StringType_push(t,Getattr(o,ATTR_TYPE)); SwigType_push(t,Getattr(o,ATTR_TYPE));
Setattr(o,ATTR_TYPE,t); Setattr(o,ATTR_TYPE,t);
if ($1.ivalue) { if ($1.ivalue) {
Setattr(o,ATTR_STORAGE,$1.text); Setattr(o,ATTR_STORAGE,$1.text);
@ -841,7 +841,7 @@ variable_decl : storage_spec type declaration array2 def_args stail {
function_decl : storage_spec type declaration LPAREN parms RPAREN cpp_const stail { function_decl : storage_spec type declaration LPAREN parms RPAREN cpp_const stail {
DOH *o, *t; DOH *o, *t;
t = Copy($2); t = Copy($2);
StringType_push(t,$3.decl); SwigType_push(t,$3.decl);
$$ = new_node(TAG_FUNCTION,Getfile($3.id),Getline($3.id)); $$ = new_node(TAG_FUNCTION,Getfile($3.id),Getline($3.id));
Setattr($$,ATTR_NAME,$3.id); Setattr($$,ATTR_NAME,$3.id);
Setattr($$,ATTR_TYPE,t); Setattr($$,ATTR_TYPE,t);
@ -854,7 +854,7 @@ function_decl : storage_spec type declaration LPAREN parms RPAREN cpp_const sta
o = $8; o = $8;
while (o) { while (o) {
t = Copy($2); t = Copy($2);
StringType_push(t,Getattr(o,ATTR_TYPE)); SwigType_push(t,Getattr(o,ATTR_TYPE));
Setattr(o,ATTR_TYPE,t); Setattr(o,ATTR_TYPE,t);
if ($1.ivalue) { if ($1.ivalue) {
Setattr(o,ATTR_STORAGE,$1.text); Setattr(o,ATTR_STORAGE,$1.text);
@ -867,7 +867,7 @@ function_decl : storage_spec type declaration LPAREN parms RPAREN cpp_const sta
/* A function declaration with code after it */ /* A function declaration with code after it */
| storage_spec type declaration LPAREN parms RPAREN cpp_end { | storage_spec type declaration LPAREN parms RPAREN cpp_end {
StringType_push($2,$3.decl); SwigType_push($2,$3.decl);
$$ = new_node(TAG_FUNCTION,Getfile($3.id),Getline($3.id)); $$ = new_node(TAG_FUNCTION,Getfile($3.id),Getline($3.id));
Setattr($$,ATTR_NAME,$3.id); Setattr($$,ATTR_NAME,$3.id);
Setattr($$,ATTR_TYPE,$2); Setattr($$,ATTR_TYPE,$2);
@ -914,8 +914,8 @@ function_decl : storage_spec type declaration LPAREN parms RPAREN cpp_const sta
stail : SEMI { $$ = 0; } stail : SEMI { $$ = 0; }
| COMMA declaration array2 def_args stail { | COMMA declaration array2 def_args stail {
DOH *t = NewString(""); DOH *t = NewString("");
StringType_push(t,$2.decl); SwigType_push(t,$2.decl);
StringType_push(t,$3); SwigType_push(t,$3);
$$ = new_node(TAG_VARIABLE, Getfile($2.id),Getline($2.id)); $$ = new_node(TAG_VARIABLE, Getfile($2.id),Getline($2.id));
Setattr($$,ATTR_NAME,$2.id); Setattr($$,ATTR_NAME,$2.id);
Setattr($$,ATTR_TYPE,t); Setattr($$,ATTR_TYPE,t);
@ -926,7 +926,7 @@ stail : SEMI { $$ = 0; }
} }
| COMMA declaration LPAREN parms RPAREN stail { | COMMA declaration LPAREN parms RPAREN stail {
DOH *t = NewString(""); DOH *t = NewString("");
StringType_push(t,$2.decl); SwigType_push(t,$2.decl);
$$ = new_node(TAG_FUNCTION, Getfile($2.id), Getline($2.id)); $$ = new_node(TAG_FUNCTION, Getfile($2.id), Getline($2.id));
Setattr($$,ATTR_NAME,$2.id); Setattr($$,ATTR_NAME,$2.id);
Setattr($$,ATTR_PARMS,$4); Setattr($$,ATTR_PARMS,$4);
@ -1035,8 +1035,8 @@ typedef_decl : TYPEDEF type declaration array2 typedeflist SEMI {
int i; int i;
$$ = new_node("typedef", $1.filename,$1.line); $$ = new_node("typedef", $1.filename,$1.line);
t = Copy($2); t = Copy($2);
StringType_push($2,$3.decl); SwigType_push($2,$3.decl);
if ($4) StringType_push($2,$4); if ($4) SwigType_push($2,$4);
Setattr($$,ATTR_NAME,$3.id); Setattr($$,ATTR_NAME,$3.id);
Setattr($$,ATTR_TYPE,$2); Setattr($$,ATTR_TYPE,$2);
/* Go create more typedefs */ /* Go create more typedefs */
@ -1046,8 +1046,8 @@ typedef_decl : TYPEDEF type declaration array2 typedeflist SEMI {
d = Getitem($5,i); d = Getitem($5,i);
o = new_node("typedef",$1.filename,$1.line); o = new_node("typedef",$1.filename,$1.line);
ty = Copy(t); ty = Copy(t);
StringType_push(ty,Getattr(d,"decl")); SwigType_push(ty,Getattr(d,"decl"));
StringType_push(ty,Getattr(d,"array")); SwigType_push(ty,Getattr(d,"array"));
Setattr(o,ATTR_TYPE,ty); Setattr(o,ATTR_TYPE,ty);
Setattr(o,ATTR_NAME,Getattr(d,ATTR_NAME)); Setattr(o,ATTR_NAME,Getattr(d,ATTR_NAME));
Setattr(prev,ATTR_SIBLING,o); Setattr(prev,ATTR_SIBLING,o);
@ -1060,10 +1060,10 @@ typedef_decl : TYPEDEF type declaration array2 typedeflist SEMI {
| TYPEDEF type LPAREN stars pname RPAREN LPAREN parms RPAREN SEMI { | TYPEDEF type LPAREN stars pname RPAREN LPAREN parms RPAREN SEMI {
$$ = new_node("typedef", $1.filename,$1.line); $$ = new_node("typedef", $1.filename,$1.line);
StringType_push($2,parmstotype($8)); SwigType_push($2,parmstotype($8));
StringType_push($2,$4); SwigType_push($2,$4);
if ($5.array) if ($5.array)
StringType_push($2,$5.array); SwigType_push($2,$5.array);
Setattr($$,ATTR_NAME,$5.name); Setattr($$,ATTR_NAME,$5.name);
Setattr($$,ATTR_TYPE,$2); Setattr($$,ATTR_TYPE,$2);
} }
@ -1072,11 +1072,11 @@ typedef_decl : TYPEDEF type declaration array2 typedeflist SEMI {
| TYPEDEF type stars LPAREN stars pname RPAREN LPAREN parms RPAREN SEMI { | TYPEDEF type stars LPAREN stars pname RPAREN LPAREN parms RPAREN SEMI {
$$ = new_node("typedef", $1.filename,$1.line); $$ = new_node("typedef", $1.filename,$1.line);
StringType_push($2,$3); SwigType_push($2,$3);
StringType_push($2,parmstotype($9)); SwigType_push($2,parmstotype($9));
StringType_push($2,$5); SwigType_push($2,$5);
if ($6.array) if ($6.array)
StringType_push($2,$6.array); SwigType_push($2,$6.array);
Setattr($$,ATTR_NAME,$6.name); Setattr($$,ATTR_NAME,$6.name);
Setattr($$,ATTR_TYPE,$2); Setattr($$,ATTR_TYPE,$2);
} }
@ -1160,7 +1160,7 @@ cpp_class : storage_spec cpptype ID inherit LBRACE interface RBRACE opt_id S
o = new_node("typedef",$3.filename,$3.line); o = new_node("typedef",$3.filename,$3.line);
Setattr(o,ATTR_NAME,$8.id); Setattr(o,ATTR_NAME,$8.id);
t = Copy($3.text); t = Copy($3.text);
StringType_push(t,$8.decl); SwigType_push(t,$8.decl);
Setattr(o,ATTR_TYPE,t); Setattr(o,ATTR_TYPE,t);
Setattr($$,ATTR_SIBLING,o); Setattr($$,ATTR_SIBLING,o);
prev = o; prev = o;
@ -1168,8 +1168,8 @@ cpp_class : storage_spec cpptype ID inherit LBRACE interface RBRACE opt_id S
d = Getitem($9,i); d = Getitem($9,i);
o = new_node("typedef",$3.filename,$3.line); o = new_node("typedef",$3.filename,$3.line);
t = Copy($3.text); t = Copy($3.text);
StringType_push(t,Getattr(d,"decl")); SwigType_push(t,Getattr(d,"decl"));
StringType_push(t,Getattr(d,"array")); SwigType_push(t,Getattr(d,"array"));
Setattr(o,ATTR_TYPE,t); Setattr(o,ATTR_TYPE,t);
Setattr(o,ATTR_NAME,Getattr(d,ATTR_NAME)); Setattr(o,ATTR_NAME,Getattr(d,ATTR_NAME));
Setattr(prev,ATTR_SIBLING,o); Setattr(prev,ATTR_SIBLING,o);
@ -1336,7 +1336,7 @@ ptail : COMMA parm ptail {
parm : type pname { parm : type pname {
$$ = new_node("parm",Getfile($2.name),Getline($2.name)); $$ = new_node("parm",Getfile($2.name),Getline($2.name));
Setattr($$,ATTR_NAME,$2.name); Setattr($$,ATTR_NAME,$2.name);
StringType_push($1,$2.array); SwigType_push($1,$2.array);
if ($2.value) if ($2.value)
Setattr($$,ATTR_VALUE,$2.value); Setattr($$,ATTR_VALUE,$2.value);
Setattr($$,ATTR_TYPE,$1); Setattr($$,ATTR_TYPE,$1);
@ -1344,8 +1344,8 @@ parm : type pname {
| type stars pname { | type stars pname {
$$ = new_node("parm",Getfile($3.name),Getline($3.name)); $$ = new_node("parm",Getfile($3.name),Getline($3.name));
Setattr($$,ATTR_NAME,$3.name); Setattr($$,ATTR_NAME,$3.name);
StringType_push($1,$2); SwigType_push($1,$2);
StringType_push($1,$3.array); SwigType_push($1,$3.array);
if ($3.value) { if ($3.value) {
Setattr($$,ATTR_VALUE,$3.value); Setattr($$,ATTR_VALUE,$3.value);
} }
@ -1353,8 +1353,8 @@ parm : type pname {
} }
| type AND pname { | type AND pname {
$$ = new_node("parm",Getfile($3.name),Getline($3.name)); $$ = new_node("parm",Getfile($3.name),Getline($3.name));
StringType_add_reference($1); SwigType_add_reference($1);
StringType_push($1,$3.array); SwigType_push($1,$3.array);
Setattr($$,ATTR_NAME,$3.name); Setattr($$,ATTR_NAME,$3.name);
if ($3.value) { if ($3.value) {
Setattr($$,ATTR_VALUE,$3.value); Setattr($$,ATTR_VALUE,$3.value);
@ -1363,10 +1363,10 @@ parm : type pname {
} }
| type LPAREN stars pname RPAREN LPAREN parms RPAREN { | type LPAREN stars pname RPAREN LPAREN parms RPAREN {
$$ = new_node("parm",$2.filename, $2.line); $$ = new_node("parm",$2.filename, $2.line);
StringType_push($1,parmstotype($7)); SwigType_push($1,parmstotype($7));
StringType_push($1,$3); SwigType_push($1,$3);
if ($4.array) if ($4.array)
StringType_push($1,$4.array); SwigType_push($1,$4.array);
Setattr($$,ATTR_NAME,$4.name); Setattr($$,ATTR_NAME,$4.name);
if ($4.value) if ($4.value)
Setattr($$,ATTR_VALUE,$4.value); Setattr($$,ATTR_VALUE,$4.value);
@ -1374,11 +1374,11 @@ parm : type pname {
} }
| type stars LPAREN stars pname RPAREN LPAREN parms RPAREN { | type stars LPAREN stars pname RPAREN LPAREN parms RPAREN {
$$ = new_node("parm",$3.filename, $3.line); $$ = new_node("parm",$3.filename, $3.line);
StringType_push($1,$2); SwigType_push($1,$2);
StringType_push($1,parmstotype($8)); SwigType_push($1,parmstotype($8));
StringType_push($1,$4); SwigType_push($1,$4);
if ($5.array) if ($5.array)
StringType_push($1,$5.array); SwigType_push($1,$5.array);
Setattr($$,ATTR_NAME,$5.name); Setattr($$,ATTR_NAME,$5.name);
if ($5.value) if ($5.value)
Setattr($$,ATTR_VALUE,$5.value); Setattr($$,ATTR_VALUE,$5.value);
@ -1447,32 +1447,32 @@ declaration : ID {
| AND ID { | AND ID {
$$.id = $2.text; $$.id = $2.text;
$$.decl = NewString(""); $$.decl = NewString("");
StringType_add_reference($$.decl); SwigType_add_reference($$.decl);
} }
| AND stars ID { | AND stars ID {
$$.id = $3.text; $$.id = $3.text;
$$.decl = $2; $$.decl = $2;
StringType_add_reference($$.decl); SwigType_add_reference($$.decl);
} }
; ;
stars : STAR empty { stars : STAR empty {
$$ = NewString(""); $$ = NewString("");
StringType_add_pointer($$); SwigType_add_pointer($$);
} }
| STAR stars { | STAR stars {
$$ = $2; $$ = $2;
StringType_add_pointer($$); SwigType_add_pointer($$);
} }
; ;
array : LBRACKET RBRACKET array2 { array : LBRACKET RBRACKET array2 {
$$ = $3; $$ = $3;
StringType_add_array($$,""); SwigType_add_array($$,"");
} }
| LBRACKET expr RBRACKET array2 { | LBRACKET expr RBRACKET array2 {
$$ = $4; $$ = $4;
StringType_add_array($$,$2.text); SwigType_add_array($$,$2.text);
} }
; ;
array2 : array { array2 : array {
@ -1509,7 +1509,7 @@ type : TYPE_INT { $$ = NewString("int"); }
$$ = NewString($1.text); $$ = NewString($1.text);
} }
| CONST type { | CONST type {
StringType_add_qualifier($2,"const"); SwigType_add_qualifier($2,"const");
$$ = $2; $$ = $2;
} }
| cpptype ID { | cpptype ID {
@ -1557,7 +1557,7 @@ strict_type : TYPE_INT { $$ = NewString("int"); }
} }
| CONST type { | CONST type {
$$ = $2; $$ = $2;
StringType_add_qualifier($$,"const"); SwigType_add_qualifier($$,"const");
} }
| cpptype ID { | cpptype ID {
$$ = NewStringf("%s %s", $1.text, $2.text); $$ = NewStringf("%s %s", $1.text, $2.text);

View file

@ -40,6 +40,7 @@
* 'a(n).' = Array of size n * 'a(n).' = Array of size n
* 'f(..,..).' = Function with arguments * 'f(..,..).' = Function with arguments
* 'q(str).' = Qualifier (such as const or volatile) * 'q(str).' = Qualifier (such as const or volatile)
* 't(...).' = Template specifier???
* *
* The encoding follows the order that you might describe a type in words. * The encoding follows the order that you might describe a type in words.
* For example "p.a(200).int" is "A pointer to array of int's" and * For example "p.a(200).int" is "A pointer to array of int's" and
@ -58,8 +59,8 @@
* *
* For the most part, this module tries to minimize the use of special * For the most part, this module tries to minimize the use of special
* characters (*, [, <, etc...) in its type encoding. One reason for this * characters (*, [, <, etc...) in its type encoding. One reason for this
* is that SWIG might be extended to interact with data encodings such as * is that SWIG might be extended to encode data in formats such as XML
* XML in which case a type could be provided as follows: * where we might want to do things like this:
* *
* <function> * <function>
* <type>p.p.int</type> * <type>p.p.int</type>
@ -70,67 +71,66 @@
* *
* <function type="p.p.int" ...>blah</function> * <function type="p.p.int" ...>blah</function>
* *
* In either case, it's probably best to avoid characters such as '&' * In either case, it's probably best to avoid characters such as '&', '*', or '<'.
* or '<'.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_add_pointer() * SwigType_add_pointer()
* *
* Adds a pointer constructor to a type * Adds a pointer constructor to a type
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
void void
StringType_add_pointer(DOHString *t) { SwigType_add_pointer(DOHString *t) {
Insert(t,0,"p."); Insert(t,0,"p.");
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_add_array() * SwigType_add_array()
* *
* Adds an array constructor to a type * Adds an array constructor to a type
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
void void
StringType_add_array(DOHString *t, DOHString *size) { SwigType_add_array(DOHString *t, DOHString *size) {
char temp[256]; char temp[256];
sprintf(temp,"a(%s).", Char(size)); sprintf(temp,"a(%s).", Char(size));
Insert(t,0,temp); Insert(t,0,temp);
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_add_reference() * SwigType_add_reference()
* *
* Adds a reference constructor to a type. * Adds a reference constructor to a type.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
void void
StringType_add_reference(DOHString *t) { SwigType_add_reference(DOHString *t) {
Insert(t,0,"r."); Insert(t,0,"r.");
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_add_qualifier() * SwigType_add_qualifier()
* *
* Adds a qualifier to a type * Adds a qualifier to a type
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
void void
StringType_add_qualifier(DOHString *t, DOHString *qual) { SwigType_add_qualifier(DOHString *t, DOHString *qual) {
char temp[256]; char temp[256];
sprintf(temp,"q(%s).",Char(qual)); sprintf(temp,"q(%s).",Char(qual));
Insert(t,0,temp); Insert(t,0,temp);
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_add_function() * SwigType_add_function()
* *
* Adds a function to a type. Accepts a list of abstract types as parameters. * Adds a function to a type. Accepts a list of abstract types as parameters.
* These abstract types should be passed as a list of type-strings. * These abstract types should be passed as a list of type-strings.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
void void
StringType_add_function(DOHString *t, DOHList *parms) { SwigType_add_function(DOHString *t, DOHList *parms) {
DOHString *pstr; DOHString *pstr;
int i,l; int i,l;
@ -156,7 +156,10 @@ static DOHString *
isolate_element(char *c) { isolate_element(char *c) {
DOHString *result = NewString(""); DOHString *result = NewString("");
while (*c) { while (*c) {
if (*c == '.') return result; if (*c == '.') {
Putc(*c,result);
return result;
}
else if (*c == '(') { else if (*c == '(') {
int nparen = 1; int nparen = 1;
Putc(*c,result); Putc(*c,result);
@ -170,19 +173,6 @@ isolate_element(char *c) {
} }
c++; c++;
} }
} else if (*c == '{') {
int nbrace = 1;
Putc(*c,result);
c++;
while(*c) {
Putc(*c,result);
if (*c == '{') nbrace++;
if (*c == '}') {
nbrace--;
if (nbrace == 0) break;
}
c++;
}
} else { } else {
Putc(*c,result); Putc(*c,result);
} }
@ -190,13 +180,14 @@ isolate_element(char *c) {
} }
return result; return result;
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_split(DOH *t) * SwigType_split(DOH *t)
* *
* Splits a type into it's component parts and returns a list of string. * Splits a type into it's component parts and returns a list of string.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
DOHList *StringType_split(DOHString_or_char *t) { DOHList *SwigType_split(DOHString_or_char *t) {
DOH *item; DOH *item;
DOHList *list; DOHList *list;
char *c; char *c;
@ -221,12 +212,12 @@ DOHList *StringType_split(DOHString_or_char *t) {
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_pop() * SwigType_pop()
* *
* Pop off the first type-constructor object and update the type * Pop off the first type-constructor object and update the type
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
DOHString *StringType_pop(DOH *t) DOHString *SwigType_pop(DOH *t)
{ {
DOHString *result; DOHString *result;
char *c; char *c;
@ -243,12 +234,12 @@ DOHString *StringType_pop(DOH *t)
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_push() * SwigType_push()
* *
* Push a type constructor onto the type * Push a type constructor onto the type
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
void StringType_push(DOHString *t, DOHString *cons) void SwigType_push(DOHString *t, DOHString *cons)
{ {
if (!cons) return; if (!cons) return;
if (!Len(cons)) return; if (!Len(cons)) return;
@ -262,18 +253,19 @@ void StringType_push(DOHString *t, DOHString *cons)
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_split_parms() * SwigType_parmlist()
* *
* Splits a comma separated list of components into strings. * Splits a comma separated list of components into strings.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
DOHList *StringType_split_parms(DOHString *p) { DOHList *SwigType_parmlist(DOHString *p) {
DOH *item; DOH *item;
DOHList *list; DOHList *list;
char *c; char *c;
c = Char(p); c = Char(p);
assert(*c == '('); while (*c && (*c != '(') && (*c != '.')) c++;
if (!*c || (*c == '.')) return 0;
c++; c++;
list = NewList(); list = NewList();
item = NewString(""); item = NewString("");
@ -297,19 +289,6 @@ DOHList *StringType_split_parms(DOHString *p) {
} }
} else if (*c == ')') { } else if (*c == ')') {
break; break;
} else if (*c == '{') {
int nbraces = 1;
Putc(*c,item);
c++;
while (*c) {
Putc(*c,item);
if (*c == '{') nbraces++;
if (*c == '}') {
nbraces--;
if (nbraces == 0) break;
}
c++;
}
} else { } else {
Putc(*c,item); Putc(*c,item);
} }
@ -322,67 +301,87 @@ DOHList *StringType_split_parms(DOHString *p) {
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_ispointer() * SwigType_parm()
* StringType_isarray() *
* StringType_isreference() * Returns the parameter of an operator as a string
* StringType_isfunction() * ----------------------------------------------------------------------------- */
* StringType_isqualifier()
DOHString *SwigType_parm(DOHString *t) {
DOHString *result;
char *c;
int nparens = 0;
c = Char(t);
while (*c && (*c != '(') && (*c != '.')) c++;
if (!*c || (*c == '.')) return 0;
c++;
result = NewString("");
while (*c) {
if (*c == ')') {
if (nparens == 0) return result;
nparens--;
} else if (*c == '(') {
nparens++;
}
Putc(*c,result);
c++;
}
return result;
}
/* -----------------------------------------------------------------------------
* SwigType_ispointer()
* SwigType_isarray()
* SwigType_isreference()
* SwigType_isfunction()
* SwigType_isqualifier()
* *
* Testing functions for querying a datatype * Testing functions for querying a datatype
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
int StringType_ispointer(DOHString *t) { int SwigType_ispointer(DOHString_or_char *t) {
char *c; char *c;
assert(DohIsString(t));
c = Char(t); c = Char(t);
if (*c == '*') return 1; if (strncmp(c,"p.",2) == 0) return 1;
return 0; return 0;
} }
int StringType_isreference(DOHString *t) { int SwigType_isreference(DOHString_or_char *t) {
char *c; char *c;
assert(DohIsString(t));
c = Char(t); c = Char(t);
if (*c == '&') return 1; if (strncmp(c,"r.",2) == 0) return 1;
return 0; return 0;
} }
int StringType_isarray(DOHString *t) { int SwigType_isarray(DOHString_or_char *t) {
char *c; char *c;
assert(DohIsString(t));
c = Char(t); c = Char(t);
if (*c == '[') return 1; if (strncmp(c,"a(",2) == 0) return 1;
return 0; return 0;
} }
int StringType_isfunction(DOHString *t) { int SwigType_isfunction(DOHString_or_char *t) {
char *c; char *c;
assert(DohIsString(t));
c = Char(t); c = Char(t);
if (*c == '(') return 1; if (strncmp(c,"f(",2) == 0) return 1;
return 0; return 0;
} }
int StringType_isqualifier(DOHString *t) { int SwigType_isqualifier(DOHString_or_char *t) {
char *c; char *c;
assert(DohIsString(t));
c = Char(t); c = Char(t);
if (*c == '+') return 1; if (strncmp(c,"q(",2) == 0) return 1;
return 0; return 0;
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_base() * SwigType_base()
* *
* Returns the base of a datatype. * Returns the base of a datatype.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
DOHString *StringType_base(DOHString *t) { DOHString *SwigType_base(DOHString *t) {
char *c, *d; char *c, *d;
assert(DohIsString(t)); assert(DohIsString(t));
@ -396,13 +395,38 @@ DOHString *StringType_base(DOHString *t) {
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* StringType_cstr(DOH *s, DOH *id) * SwigType_prefix()
*
* Returns the prefix of a datatype
* ----------------------------------------------------------------------------- */
DOHString *SwigType_prefix(DOHString *t) {
char *c, *d;
DOHString *r = 0;
c = Char(t);
d = c + strlen(c);
while (d > c) {
d--;
if (*d == '.') {
char t = *(d+1);
*(d+1) = 0;
r = NewString(c);
*(d+1) = t;
return r;
}
}
return NewString("");
}
/* -----------------------------------------------------------------------------
* SwigType_cstr(DOH *s, DOH *id)
* *
* Create a C string representation of a datatype. * Create a C string representation of a datatype.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
DOHString * DOHString *
StringType_cstr(DOHString *s, DOHString_or_char *id) SwigType_cstr(DOHString *s, DOHString_or_char *id)
{ {
DOHString *result; DOHString *result;
DOHString *element, *nextelement; DOHString *element, *nextelement;
@ -416,8 +440,8 @@ StringType_cstr(DOHString *s, DOHString_or_char *id)
result = NewString(""); result = NewString("");
} }
elements = StringType_split(s); elements = SwigType_split(s);
Printf(stdout,"%s\n",elements); /* Printf(stdout,"%s\n",elements); */
nelements = Len(elements); nelements = Len(elements);
if (nelements > 0) { if (nelements > 0) {
@ -430,32 +454,41 @@ StringType_cstr(DOHString *s, DOHString_or_char *id)
} else { } else {
nextelement = 0; nextelement = 0;
} }
if (StringType_ispointer(element)) { if (SwigType_ispointer(element)) {
Insert(result,0,"*"); Insert(result,0,"*");
if ((nextelement) && ((StringType_isfunction(nextelement) || (StringType_isarray(nextelement))))) { if ((nextelement) && ((SwigType_isfunction(nextelement) || (SwigType_isarray(nextelement))))) {
Insert(result,0,"("); Insert(result,0,"(");
Append(result,")"); Append(result,")");
} }
} }
else if (StringType_isreference(element)) Insert(result,0,"&"); else if (SwigType_isreference(element)) Insert(result,0,"&");
else if (StringType_isarray(element)) Append(result,element); else if (SwigType_isarray(element)) {
else if (StringType_isfunction(element)) { DOH *size;
Append(result,"[");
size = SwigType_parm(element);
Append(result,size);
Append(result,"]");
Delete(size);
} else if (SwigType_isfunction(element)) {
DOH *parms, *p; DOH *parms, *p;
int j, plen; int j, plen;
Append(result,"("); Append(result,"(");
parms = StringType_split_parms(element); parms = SwigType_parmlist(element);
plen = Len(parms); plen = Len(parms);
for (j = 0; j < plen; j++) { for (j = 0; j < plen; j++) {
p = StringType_cstr(Getitem(parms,j),0); p = SwigType_cstr(Getitem(parms,j),0);
Append(result,p); Append(result,p);
if (j < (plen-1)) Append(result,","); if (j < (plen-1)) Append(result,",");
Delete(p); Delete(p);
} }
Append(result,")"); Append(result,")");
Delete(parms); Delete(parms);
} else if (StringType_isqualifier(element)) { } else if (SwigType_isqualifier(element)) {
DOH *q = 0;
q = SwigType_parm(element);
Insert(result,0," "); Insert(result,0," ");
Insert(result,0,Char(element)+1); Insert(result,0,q);
Delete(q);
} else { } else {
Insert(result,0," "); Insert(result,0," ");
Insert(result,0,element); Insert(result,0,element);
@ -464,3 +497,203 @@ StringType_cstr(DOHString *s, DOHString_or_char *id)
} }
return result; return result;
} }
/* -----------------------------------------------------------------------------
* Scope handling
*
* These functions are used to manipulate typedefs and scopes.
* ----------------------------------------------------------------------------- */
#define MAX_SCOPE 8
static DOHHash *type_scopes = 0; /* Hash table of scope names */
static DOHHash *scopes[MAX_SCOPE]; /* List representing the current scope */
static DOHString *scopenames[MAX_SCOPE]; /* Names of the various scopes */
static int scope_level = 0;
static init_scopes() {
if (type_scopes) return;
type_scopes = NewHash();
scopes[scope_level] = NewHash();
scopenames[scope_level] = NewString("::");
Setattr(type_scopes,"::",scopes[scope_level]);
}
/* -----------------------------------------------------------------------------
* SwigType_typedef()
*
* Defines a new typedef. Returns -1 if the type name is already defined.
* ----------------------------------------------------------------------------- */
int SwigType_typedef(DOHString_or_char *type, DOHString_or_char *name) {
init_scopes();
if (Getattr(scopes[scope_level],name)) return -1;
Setattr(scopes[scope_level],name,type);
return 0;
}
/* -----------------------------------------------------------------------------
* SwigType_new_scope()
*
* Creates a new scope
* ----------------------------------------------------------------------------- */
void SwigType_new_scope() {
init_scopes();
scope_level++;
scopes[scope_level] = NewHash();
scopenames[scope_level] = NewString("");
}
/* -----------------------------------------------------------------------------
* SwigType_reset_scopes()
*
* Reset the scope system
* ----------------------------------------------------------------------------- */
void SwigType_reset_scopes() {
Delete(type_scopes);
type_scopes = 0;
init_scopes();
}
/* -----------------------------------------------------------------------------
* SwigType_set_scope_name()
*
* Set the name of the current scope. Note: this will create an entry in the
* type_scopes hash.
* ----------------------------------------------------------------------------- */
void SwigType_set_scope_name(DOHString_or_char *name) {
DOHString *key;
int i;
init_scopes();
scopenames[scope_level] = NewString(Char(name));
key = NewString("");
for (i = 1; i <= scope_level; i++) {
Append(key,scopenames[scope_level]);
if (i < scope_level) Append(key,"::");
}
Setattr(type_scopes,key,scopes[scope_level]);
}
/* -----------------------------------------------------------------------------
* SwigType_merge_scope()
*
* Merges the contents of one scope into the current scope.
* ----------------------------------------------------------------------------- */
void SwigType_merge_scope(DOHHash *scope, DOHString_or_char *prefix) {
DOHString *name;
DOHString *key;
DOHString *type;
init_scopes();
key = Firstkey(scope);
while (key) {
type = Getattr(scope,key);
if (prefix) {
name = NewStringf("%s::%s",prefix,key);
} else {
name = NewString(key);
}
Setattr(scopes[scope_level],name,type);
key = Nextkey(scope);
}
}
/* -----------------------------------------------------------------------------
* SwigType_pop_scope()
*
* Pop off the last scope and perform a merge operation. Returns the hash
* table for the scope that was popped off.
* ----------------------------------------------------------------------------- */
DOHHash *SwigType_pop_scope() {
DOHHash *s;
DOHString *prefix;
init_scopes();
if (scope_level == 0) return 0;
prefix = scopenames[scope_level];
s = scopes[scope_level--];
SwigType_merge_scope(s,prefix);
return s;
}
/* -----------------------------------------------------------------------------
* SwigType_typedef_resolve()
*
* Resolves a typedef and returns a new type string. Returns 0 if there is no
* typedef mapping.
* ----------------------------------------------------------------------------- */
DOHString *SwigType_typedef_resolve(DOHString_or_char *t) {
DOHString *base;
DOHString *type;
DOHString *r;
int level;
base = SwigType_base(t);
level = scope_level;
while (level >= 0) {
/* See if we know about this type */
type = Getattr(scopes[scope_level],base);
if (type) break;
level--;
}
if (level < 0) {
Delete(base);
return 0;
}
r = SwigType_prefix(t);
Append(r,type);
return r;
}
/* -----------------------------------------------------------------------------
* SwigType_istypedef()
*
* Checks a typename to see if it is a typedef.
* ----------------------------------------------------------------------------- */
int SwigType_istypedef(DOHString_or_char *t) {
DOHString *base, *type;
int level;
base = SwigType_base(t);
level = scope_level;
while (level >= 0) {
/* See if we know about this type */
type = Getattr(scopes[scope_level],base);
if (type) {
Delete(base);
return 1;
}
level--;
}
Delete(base);
return 0;
}
#ifdef DEBUG
int main() {
DOHString *a,*b,*c,*d;
a = NewString("int");
SwigType_add_pointer(a);
SwigType_typedef(a,"IntPtr");
b = NewString("IntPtr");
SwigType_add_array(b,"1000");
Printf(stdout,"b = '%s'\n", b);
c = SwigType_typedef_resolve(b);
Printf(stdout,"c = '%s'\n", c);
}
#endif

View file

@ -118,22 +118,33 @@ extern void SwigScanner_idstart(SwigScanner *, char *idchar);
/* --- Functions for manipulating the string-based type encoding --- */ /* --- Functions for manipulating the string-based type encoding --- */
extern void StringType_add_pointer(DOHString *t); extern void SwigType_add_pointer(DOHString *t);
extern void StringType_add_array(DOHString *t, DOHString *size); extern void SwigType_add_array(DOHString *t, DOHString_or_char *size);
extern void StringType_add_reference(DOHString *t); extern void SwigType_add_reference(DOHString *t);
extern void StringType_add_qualifier(DOHString *t, DOHString *qual); extern void SwigType_add_qualifier(DOHString *t, DOHString_or_char *qual);
extern void StringType_add_function(DOHString *t, DOHString *parms); extern void SwigType_add_function(DOHString *t, DOHList *parms);
extern DOHList *StringType_split(DOHString *t); extern DOHList *SwigType_split(DOHString *t);
extern DOHString *StringType_pop(DOHString *t); extern DOHString *SwigType_pop(DOHString *t);
extern void StringType_push(DOHString *t, DOHString *s); extern void SwigType_push(DOHString *t, DOHString *s);
extern DOHList *StringType_split_parms(DOHString *p); extern DOHList *SwigType_parmlist(DOHString *p);
extern DOHString *StringType_cstr(DOHString *s, DOHString_or_char *id); extern DOHString *SwigType_parm(DOHString *p);
extern int StringType_ispointer(DOHString *t); extern DOHString *SwigType_cstr(DOHString *s, DOHString_or_char *id);
extern int StringType_isreference(DOHString *t); extern int SwigType_ispointer(DOHString_or_char *t);
extern int StringType_isarray(DOHString *t); extern int SwigType_isreference(DOHString_or_char *t);
extern int StringType_isfunction(DOHString *t); extern int SwigType_isarray(DOHString_or_char *t);
extern int StringType_isqualifier(DOHString *t); extern int SwigType_isfunction(DOHString_or_char *t);
extern DOHString *StringType_base(DOHString *t); extern int SwigType_isqualifier(DOHString_or_char *t);
extern DOHString *SwigType_base(DOHString_or_char *t);
extern DOHString *SwigType_prefix(DOHString_or_char *t);
extern int SwigType_typedef(DOHString_or_char *type, DOHString_or_char *name);
extern void SwigType_new_scope();
extern void SwigType_reset_scopes();
extern void SwigType_set_scope_name(DOHString_or_char *name);
extern void SwigType_merge_scope(DOHHash *scope, DOHString_or_char *prefix);
extern DOHHash *SwigType_pop_scope();
extern DOHString *SwigType_typedef_resolve(DOHString_or_char *t);
extern int SwigType_istypedef(DOHString_or_char *t);
/* --- Parse tree support --- */ /* --- Parse tree support --- */