void Swig_extend_merge(Node *cls, Node *am) { Node *n; Node *csym; n = firstChild(am); while (n) { String *symname; if (Strcmp(nodeType(n),"constructor") == 0) { symname = Getattr(n,"sym:name"); if (symname) { if (Strcmp(symname,Getattr(n,"name")) == 0) { /* If the name and the sym:name of a constructor are the same, then it hasn't been renamed. However---the name of the class itself might have been renamed so we need to do a consistency check here */ if (Getattr(cls,"sym:name")) { Setattr(n,"sym:name", Getattr(cls,"sym:name")); } } } } symname = Getattr(n,"sym:name"); DohIncref(symname); if ((symname) && (!Getattr(n,"error"))) { /* Remove node from its symbol table */ Swig_symbol_remove(n); csym = Swig_symbol_add(symname,n); if (csym != n) { /* Conflict with previous definition. Nuke previous definition */ String *e = NewStringEmpty(); String *en = NewStringEmpty(); String *ec = NewStringEmpty(); Printf(ec,"Identifier '%s' redefined by %%extend (ignored),",symname); Printf(en,"%%extend definition of '%s'.",symname); SWIG_WARN_NODE_BEGIN(n); Swig_warning(WARN_PARSE_REDEFINED,Getfile(csym),Getline(csym),"%s\n",ec); Swig_warning(WARN_PARSE_REDEFINED,Getfile(n),Getline(n),"%s\n",en); SWIG_WARN_NODE_END(n); Printf(e,"%s:%d:%s\n%s:%d:%s\n",Getfile(csym),Getline(csym),ec, Getfile(n),Getline(n),en); Setattr(csym,"error",e); Delete(e); Delete(en); Delete(ec); Swig_symbol_remove(csym); /* Remove class definition */ Swig_symbol_add(symname,n); /* Insert extend definition */ } } n = nextSibling(n); } }
void Swig_extend_unused_check(void) { Iterator ki; if (!extendhash) return; for (ki = First(extendhash); ki.key; ki = Next(ki)) { if (!Strchr(ki.key,'<')) { SWIG_WARN_NODE_BEGIN(ki.item); Swig_warning(WARN_PARSE_EXTEND_UNDEF,Getfile(ki.item), Getline(ki.item), "%%extend defined for an undeclared class %s.\n", SwigType_namestr(ki.key)); SWIG_WARN_NODE_END(ki.item); } } }
int yylex(void) { int l; char *yytext; if (!scan_init) { scanner_init(); } if (next_token) { l = next_token; next_token = 0; return l; } l = yylook(); /* Printf(stdout, "%s:%d:::%d: '%s'\n", cparse_file, cparse_line, l, Scanner_text(scan)); */ if (l == NONID) { last_id = 1; } else { last_id = 0; } /* We got some sort of non-white space object. We set the start_line variable unless it has already been set */ if (!cparse_start_line) { cparse_start_line = cparse_line; } /* Copy the lexene */ switch (l) { case NUM_INT: case NUM_FLOAT: case NUM_ULONG: case NUM_LONG: case NUM_UNSIGNED: case NUM_LONGLONG: case NUM_ULONGLONG: case NUM_BOOL: if (l == NUM_INT) yylval.dtype.type = T_INT; if (l == NUM_FLOAT) yylval.dtype.type = T_DOUBLE; if (l == NUM_ULONG) yylval.dtype.type = T_ULONG; if (l == NUM_LONG) yylval.dtype.type = T_LONG; if (l == NUM_UNSIGNED) yylval.dtype.type = T_UINT; if (l == NUM_LONGLONG) yylval.dtype.type = T_LONGLONG; if (l == NUM_ULONGLONG) yylval.dtype.type = T_ULONGLONG; if (l == NUM_BOOL) yylval.dtype.type = T_BOOL; yylval.dtype.val = NewString(Scanner_text(scan)); yylval.dtype.bitfield = 0; yylval.dtype.throws = 0; return (l); case ID: yytext = Char(Scanner_text(scan)); if (yytext[0] != '%') { /* Look for keywords now */ if (strcmp(yytext, "int") == 0) { yylval.type = NewSwigType(T_INT); return (TYPE_INT); } if (strcmp(yytext, "double") == 0) { yylval.type = NewSwigType(T_DOUBLE); return (TYPE_DOUBLE); } if (strcmp(yytext, "void") == 0) { yylval.type = NewSwigType(T_VOID); return (TYPE_VOID); } if (strcmp(yytext, "char") == 0) { yylval.type = NewSwigType(T_CHAR); return (TYPE_CHAR); } if (strcmp(yytext, "wchar_t") == 0) { yylval.type = NewSwigType(T_WCHAR); return (TYPE_WCHAR); } if (strcmp(yytext, "short") == 0) { yylval.type = NewSwigType(T_SHORT); return (TYPE_SHORT); } if (strcmp(yytext, "long") == 0) { yylval.type = NewSwigType(T_LONG); return (TYPE_LONG); } if (strcmp(yytext, "float") == 0) { yylval.type = NewSwigType(T_FLOAT); return (TYPE_FLOAT); } if (strcmp(yytext, "signed") == 0) { yylval.type = NewSwigType(T_INT); return (TYPE_SIGNED); } if (strcmp(yytext, "unsigned") == 0) { yylval.type = NewSwigType(T_UINT); return (TYPE_UNSIGNED); } if (strcmp(yytext, "bool") == 0) { yylval.type = NewSwigType(T_BOOL); return (TYPE_BOOL); } /* Non ISO (Windows) C extensions */ if (strcmp(yytext, "__int8") == 0) { yylval.type = NewString(yytext); return (TYPE_NON_ISO_INT8); } if (strcmp(yytext, "__int16") == 0) { yylval.type = NewString(yytext); return (TYPE_NON_ISO_INT16); } if (strcmp(yytext, "__int32") == 0) { yylval.type = NewString(yytext); return (TYPE_NON_ISO_INT32); } if (strcmp(yytext, "__int64") == 0) { yylval.type = NewString(yytext); return (TYPE_NON_ISO_INT64); } /* C++ keywords */ if (cparse_cplusplus) { if (strcmp(yytext, "and") == 0) return (LAND); if (strcmp(yytext, "or") == 0) return (LOR); if (strcmp(yytext, "not") == 0) return (LNOT); if (strcmp(yytext, "class") == 0) return (CLASS); if (strcmp(yytext, "private") == 0) return (PRIVATE); if (strcmp(yytext, "public") == 0) return (PUBLIC); if (strcmp(yytext, "protected") == 0) return (PROTECTED); if (strcmp(yytext, "friend") == 0) return (FRIEND); if (strcmp(yytext, "virtual") == 0) return (VIRTUAL); if (strcmp(yytext, "operator") == 0) { int nexttok; String *s = NewString("operator "); /* If we have an operator, we have to collect the operator symbol and attach it to the operator identifier. To do this, we need to scan ahead by several tokens. Cases include: (1) If the next token is an operator as determined by Scanner_isoperator(), it means that the operator applies to one of the standard C++ mathematical, assignment, or logical operator symbols (e.g., '+','<=','==','&', etc.) In this case, we merely append the symbol text to the operator string above. (2) If the next token is (, we look for ). This is operator (). (3) If the next token is [, we look for ]. This is operator []. (4) If the next token is an identifier. The operator is possibly a conversion operator. (a) Must check for special case new[] and delete[] Error handling is somewhat tricky here. We'll try to back out gracefully if we can. */ nexttok = Scanner_token(scan); if (Scanner_isoperator(nexttok)) { /* One of the standard C/C++ symbolic operators */ Append(s,Scanner_text(scan)); yylval.str = s; return OPERATOR; } else if (nexttok == SWIG_TOKEN_LPAREN) { /* Function call operator. The next token MUST be a RPAREN */ nexttok = Scanner_token(scan); if (nexttok != SWIG_TOKEN_RPAREN) { Swig_error(Scanner_file(scan),Scanner_line(scan),"Syntax error. Bad operator name.\n"); } else { Append(s,"()"); yylval.str = s; return OPERATOR; } } else if (nexttok == SWIG_TOKEN_LBRACKET) { /* Array access operator. The next token MUST be a RBRACKET */ nexttok = Scanner_token(scan); if (nexttok != SWIG_TOKEN_RBRACKET) { Swig_error(Scanner_file(scan),Scanner_line(scan),"Syntax error. Bad operator name.\n"); } else { 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 going to read tokens until we encounter a ( or ;. Some care is needed for formatting. */ int needspace = 1; int termtoken = 0; const char *termvalue = 0; Append(s,Scanner_text(scan)); while (1) { nexttok = Scanner_token(scan); if (nexttok <= 0) { Swig_error(Scanner_file(scan),Scanner_line(scan),"Syntax error. Bad operator name.\n"); } if (nexttok == SWIG_TOKEN_LPAREN) { termtoken = SWIG_TOKEN_LPAREN; termvalue = "("; break; } else if (nexttok == SWIG_TOKEN_CODEBLOCK) { termtoken = SWIG_TOKEN_CODEBLOCK; termvalue = Char(Scanner_text(scan)); break; } else if (nexttok == SWIG_TOKEN_LBRACE) { termtoken = SWIG_TOKEN_LBRACE; termvalue = "{"; break; } else if (nexttok == SWIG_TOKEN_SEMI) { termtoken = SWIG_TOKEN_SEMI; termvalue = ";"; break; } else if (nexttok == SWIG_TOKEN_STRING) { termtoken = SWIG_TOKEN_STRING; termvalue = Swig_copy_string(Char(Scanner_text(scan))); break; } else if (nexttok == SWIG_TOKEN_ID) { if (needspace) { Append(s," "); } Append(s,Scanner_text(scan)); } else { Append(s,Scanner_text(scan)); needspace = 0; } } yylval.str = s; if (!rename_active) { String *cs; char *t = Char(s) + 9; if (!((strcmp(t, "new") == 0) || (strcmp(t, "delete") == 0) || (strcmp(t, "new[]") == 0) || (strcmp(t, "delete[]") == 0) || (strcmp(t, "and") == 0) || (strcmp(t, "and_eq") == 0) || (strcmp(t, "bitand") == 0) || (strcmp(t, "bitor") == 0) || (strcmp(t, "compl") == 0) || (strcmp(t, "not") == 0) || (strcmp(t, "not_eq") == 0) || (strcmp(t, "or") == 0) || (strcmp(t, "or_eq") == 0) || (strcmp(t, "xor") == 0) || (strcmp(t, "xor_eq") == 0) )) { /* retract(strlen(t)); */ /* The operator is a conversion operator. In order to deal with this, we need to feed the type information back into the parser. For now this is a hack. Needs to be cleaned up later. */ cs = NewString(t); if (termtoken) Append(cs,termvalue); Seek(cs,0,SEEK_SET); Setline(cs,cparse_line); Setfile(cs,cparse_file); Scanner_push(scan,cs); Delete(cs); return COPERATOR; } } if (termtoken) Scanner_pushtoken(scan, termtoken, termvalue); return (OPERATOR); } } if (strcmp(yytext, "throw") == 0) return (THROW); if (strcmp(yytext, "try") == 0) return (yylex()); if (strcmp(yytext, "catch") == 0) return (CATCH); if (strcmp(yytext, "inline") == 0) return (yylex()); if (strcmp(yytext, "mutable") == 0) return (yylex()); if (strcmp(yytext, "explicit") == 0) return (EXPLICIT); if (strcmp(yytext, "export") == 0) return (yylex()); if (strcmp(yytext, "typename") == 0) return (TYPENAME); if (strcmp(yytext, "template") == 0) { yylval.ivalue = cparse_line; return (TEMPLATE); } if (strcmp(yytext, "delete") == 0) { return (DELETE_KW); } if (strcmp(yytext, "using") == 0) { return (USING); } if (strcmp(yytext, "namespace") == 0) { return (NAMESPACE); } } else { if (strcmp(yytext, "class") == 0) { Swig_warning(WARN_PARSE_CLASS_KEYWORD, cparse_file, cparse_line, "class keyword used, but not in C++ mode.\n"); } if (strcmp(yytext, "complex") == 0) { yylval.type = NewSwigType(T_COMPLEX); return (TYPE_COMPLEX); } if (strcmp(yytext, "restrict") == 0) return (yylex()); } /* Misc keywords */ if (strcmp(yytext, "extern") == 0) return (EXTERN); if (strcmp(yytext, "const") == 0) return (CONST_QUAL); if (strcmp(yytext, "static") == 0) return (STATIC); if (strcmp(yytext, "struct") == 0) return (STRUCT); if (strcmp(yytext, "union") == 0) return (UNION); if (strcmp(yytext, "enum") == 0) return (ENUM); if (strcmp(yytext, "sizeof") == 0) return (SIZEOF); if (strcmp(yytext, "typedef") == 0) { yylval.ivalue = 0; return (TYPEDEF); } /* Ignored keywords */ if (strcmp(yytext, "volatile") == 0) return (VOLATILE); if (strcmp(yytext, "register") == 0) return (REGISTER); if (strcmp(yytext, "inline") == 0) return (yylex()); /* SWIG directives */ } else { if (strcmp(yytext, "%module") == 0) return (MODULE); if (strcmp(yytext, "%insert") == 0) return (INSERT); if (strcmp(yytext, "%name") == 0) return (NAME); if (strcmp(yytext, "%rename") == 0) { rename_active = 1; return (RENAME); } if (strcmp(yytext, "%namewarn") == 0) { rename_active = 1; return (NAMEWARN); } if (strcmp(yytext, "%includefile") == 0) return (INCLUDE); if (strcmp(yytext, "%val") == 0) { Swig_warning(WARN_DEPRECATED_VAL, cparse_file, cparse_line, "%%val directive deprecated (ignored).\n"); return (yylex()); } if (strcmp(yytext, "%out") == 0) { Swig_warning(WARN_DEPRECATED_OUT, cparse_file, cparse_line, "%%out directive deprecated (ignored).\n"); return (yylex()); } if (strcmp(yytext, "%constant") == 0) return (CONSTANT); if (strcmp(yytext, "%typedef") == 0) { yylval.ivalue = 1; return (TYPEDEF); } if (strcmp(yytext, "%native") == 0) return (NATIVE); if (strcmp(yytext, "%pragma") == 0) return (PRAGMA); if (strcmp(yytext, "%extend") == 0) return (EXTEND); if (strcmp(yytext, "%fragment") == 0) return (FRAGMENT); if (strcmp(yytext, "%inline") == 0) return (INLINE); if (strcmp(yytext, "%typemap") == 0) return (TYPEMAP); if (strcmp(yytext, "%feature") == 0) { /* The rename_active indicates we don't need the information of the * following function's return type. This applied for %rename, so do * %feature. */ rename_active = 1; return (FEATURE); } if (strcmp(yytext, "%except") == 0) return (EXCEPT); if (strcmp(yytext, "%importfile") == 0) return (IMPORT); if (strcmp(yytext, "%echo") == 0) return (ECHO); if (strcmp(yytext, "%apply") == 0) return (APPLY); if (strcmp(yytext, "%clear") == 0) return (CLEAR); if (strcmp(yytext, "%types") == 0) return (TYPES); if (strcmp(yytext, "%parms") == 0) return (PARMS); if (strcmp(yytext, "%varargs") == 0) return (VARARGS); if (strcmp(yytext, "%template") == 0) { return (SWIGTEMPLATE); } if (strcmp(yytext, "%warn") == 0) return (WARN); } /* Have an unknown identifier, as a last step, we'll do a typedef lookup on it. */ /* Need to fix this */ if (check_typedef) { if (SwigType_istypedef(yytext)) { yylval.type = NewString(yytext); return (TYPE_TYPEDEF); } } yylval.id = Swig_copy_string(yytext); last_id = 1; return (ID); case POUND: return yylex(); default: return (l); } }
void Swig_fragment_emit(Node *n) { String *code; char *pc, *tok; String *t; String *mangle = 0; String *name = 0; String *type = 0; if (!fragments) { Swig_warning(WARN_FRAGMENT_NOT_FOUND, Getfile(n), Getline(n), "Fragment '%s' not found.\n", name); return; } name = Getattr(n,k_value); if (!name) { name = n; } type = Getattr(n,k_type); if (type) { mangle = Swig_string_mangle(type); } if (debug) Printf(stdout,"looking fragment %s %s\n",name, type); t = Copy(name); tok = Char(t); pc = char_index(tok,','); if (pc) *pc = 0; while (tok) { String *name = NewString(tok); if (mangle) Append(name,mangle); if (looking_fragments && Getattr(looking_fragments,name)) { return; } code = Getattr(fragments,name); if (debug) Printf(stdout,"looking subfragment %s\n", name); if (code && (Strcmp(code,k_ignore) != 0)) { String *section = Getmeta(code,k_section); Hash *nn = Getmeta(code,k_kwargs); if (!looking_fragments) looking_fragments = NewHash(); Setattr(looking_fragments,name,"1"); while (nn) { if (Equal(Getattr(nn,k_name),k_fragment)) { if (debug) Printf(stdout,"emitting fragment %s %s\n",nn, type); Setfile(nn, Getfile(n)); Setline(nn, Getline(n)); Swig_fragment_emit(nn); } nn = nextSibling(nn); } if (section) { File *f = Swig_filebyname(section); if (!f) { Swig_error(Getfile(code),Getline(code), "Bad section '%s' for code fragment '%s'\n", section,name); } else { if (debug) Printf(stdout,"emitting subfragment %s %s\n",name, section); if (debug) Printf(f,"/* begin fragment %s */\n",name); Printf(f,"%s\n",code); if (debug) Printf(f,"/* end fragment %s */\n\n",name); Setattr(fragments,name,k_ignore); Delattr(looking_fragments,name); } } } else if (!code && type) { SwigType *rtype = SwigType_typedef_resolve_all(type); if (!Equal(type,rtype)) { String *name = Copy(Getattr(n,k_value)); String *mangle = Swig_string_mangle(type); Append(name,mangle); Setfile(name, Getfile(n)); Setline(name, Getline(n)); Swig_fragment_emit(name); Delete(mangle); Delete(name); } Delete(rtype); } if (!code) { Swig_warning(WARN_FRAGMENT_NOT_FOUND, Getfile(n), Getline(n), "Fragment '%s' not found.\n", name); } tok = pc ? pc + 1 : 0; if (tok) { pc = char_index(tok,','); if (pc) *pc = 0; } Delete(name); } Delete(t); }
static Node *template_locate(String *name, Parm *tparms, Symtab *tscope) { Node *n; String *tname, *rname = 0; Node *templ; List *mpartials = 0; Parm *p; Parm *parms; Parm *targs; ParmList *expandedparms; tname = Copy(name); parms = CopyParmList(tparms); /* Search for generic template */ templ = Swig_symbol_clookup(name, 0); /* Add default values from generic template */ if (templ) { Symtab *tsdecl = Getattr(templ, "sym:symtab"); targs = Getattr(templ, "templateparms"); expandedparms = Swig_symbol_template_defargs(parms, targs, tscope, tsdecl); } else { expandedparms = parms; } /* reduce the typedef */ p = expandedparms; while (p) { SwigType *ty = Getattr(p, "type"); if (ty) { SwigType *nt = Swig_symbol_type_qualify(ty, tscope); Setattr(p, "type", nt); Delete(nt); } p = nextSibling(p); } SwigType_add_template(tname, expandedparms); if (template_debug) { Printf(stdout, "\n%s:%d: template_debug: Searching for %s\n", cparse_file, cparse_line, tname); } /* Search for an exact specialization. Example: template<> class name<int> { ... } */ { if (template_debug) { Printf(stdout, " searching: '%s' (exact specialization)\n", tname); } n = Swig_symbol_clookup_local(tname, 0); if (!n) { SwigType *rname = Swig_symbol_typedef_reduce(tname, tscope); if (!Equal(rname, tname)) { if (template_debug) { Printf(stdout, " searching: '%s' (exact specialization)\n", rname); } n = Swig_symbol_clookup_local(rname, 0); } Delete(rname); } if (n) { Node *tn; String *nodeType = nodeType(n); if (Equal(nodeType, "template")) goto success; tn = Getattr(n, "template"); if (tn) { n = tn; goto success; /* Previously wrapped by a template return that */ } Swig_error(cparse_file, cparse_line, "'%s' is not defined as a template. (%s)\n", name, nodeType(n)); Delete(tname); Delete(parms); return 0; /* Found a match, but it's not a template of any kind. */ } } /* Search for partial specialization. Example: template<typename T> class name<T *> { ... } */ /* Generate reduced template name (stripped of extraneous pointers, etc.) */ rname = NewStringf("%s<(", name); p = parms; while (p) { String *t; t = Getattr(p, "type"); if (!t) t = Getattr(p, "value"); if (t) { String *ty = Swig_symbol_typedef_reduce(t, tscope); String *tb = SwigType_base(ty); String *td = SwigType_default(ty); Replaceid(td, "enum SWIGTYPE", tb); Replaceid(td, "SWIGTYPE", tb); Append(rname, td); Delete(tb); Delete(ty); Delete(td); } p = nextSibling(p); if (p) { Append(rname, ","); } } Append(rname, ")>"); mpartials = NewList(); if (templ) { /* First, we search using an exact type prototype */ Parm *p; char tmp[32]; int i; List *partials; String *ss; Iterator pi; partials = Getattr(templ, "partials"); if (partials) { for (pi = First(partials); pi.item; pi = Next(pi)) { ss = Copy(pi.item); p = parms; i = 1; while (p) { String *t, *tn; sprintf(tmp, "$%d", i); t = Getattr(p, "type"); if (!t) t = Getattr(p, "value"); if (t) { String *ty = Swig_symbol_typedef_reduce(t, tscope); tn = SwigType_base(ty); Replaceid(ss, tmp, tn); Delete(tn); Delete(ty); } i++; p = nextSibling(p); } if (template_debug) { Printf(stdout, " searching: '%s' (partial specialization - %s)\n", ss, pi.item); } if ((Equal(ss, tname)) || (Equal(ss, rname))) { Append(mpartials, pi.item); } Delete(ss); } } } if (template_debug) { Printf(stdout, " Matched partials: %s\n", mpartials); } if (Len(mpartials)) { String *s = Getitem(mpartials, 0); n = Swig_symbol_clookup_local(s, 0); if (Len(mpartials) > 1) { if (n) { Swig_warning(WARN_PARSE_TEMPLATE_AMBIG, cparse_file, cparse_line, "Instantiation of template '%s' is ambiguous,\n", SwigType_namestr(tname)); Swig_warning(WARN_PARSE_TEMPLATE_AMBIG, Getfile(n), Getline(n), " instantiation '%s' is used.\n", SwigType_namestr(Getattr(n, "name"))); } } } if (!n) { n = templ; } if (!n) { Swig_error(cparse_file, cparse_line, "Template '%s' undefined.\n", name); } else if (n) { String *nodeType = nodeType(n); if (!Equal(nodeType, "template")) { Swig_error(cparse_file, cparse_line, "'%s' is not defined as a template. (%s)\n", name, nodeType); n = 0; } } success: Delete(tname); Delete(rname); Delete(mpartials); if ((template_debug) && (n)) { Printf(stdout, "Node: %p\n", n); Swig_print_node(n); } Delete(parms); return n; }
static Node *template_locate(String *name, Parm *tparms, Symtab *tscope) { Node *n = 0; String *tname = 0; Node *templ; Symtab *primary_scope = 0; List *possiblepartials = 0; Parm *p; Parm *parms = 0; Parm *targs; ParmList *expandedparms; int *priorities_matrix = 0; int max_possible_partials = 0; int posslen = 0; /* Search for primary (unspecialized) template */ templ = Swig_symbol_clookup(name, 0); if (template_debug) { tname = Copy(name); SwigType_add_template(tname, tparms); Printf(stdout, "\n"); Swig_diagnostic(cparse_file, cparse_line, "template_debug: Searching for match to: '%s'\n", tname); Delete(tname); tname = 0; } if (templ) { tname = Copy(name); parms = CopyParmList(tparms); /* All template specializations must be in the primary template's scope, store the symbol table for this scope for specialization lookups */ primary_scope = Getattr(templ, "sym:symtab"); /* Add default values from primary template */ targs = Getattr(templ, "templateparms"); expandedparms = Swig_symbol_template_defargs(parms, targs, tscope, primary_scope); /* reduce the typedef */ p = expandedparms; while (p) { SwigType *ty = Getattr(p, "type"); if (ty) { SwigType *nt = Swig_symbol_type_qualify(ty, tscope); Setattr(p, "type", nt); Delete(nt); } p = nextSibling(p); } SwigType_add_template(tname, expandedparms); /* Search for an explicit (exact) specialization. Example: template<> class name<int> { ... } */ { if (template_debug) { Printf(stdout, " searching for : '%s' (explicit specialization)\n", tname); } n = Swig_symbol_clookup_local(tname, primary_scope); if (!n) { SwigType *rname = Swig_symbol_typedef_reduce(tname, tscope); if (!Equal(rname, tname)) { if (template_debug) { Printf(stdout, " searching for : '%s' (explicit specialization with typedef reduction)\n", rname); } n = Swig_symbol_clookup_local(rname, primary_scope); } Delete(rname); } if (n) { Node *tn; String *nodeType = nodeType(n); if (Equal(nodeType, "template")) { if (template_debug) { Printf(stdout, " explicit specialization found: '%s'\n", Getattr(n, "name")); } goto success; } tn = Getattr(n, "template"); if (tn) { if (template_debug) { Printf(stdout, " previous instantiation found: '%s'\n", Getattr(n, "name")); } n = tn; goto success; /* Previously wrapped by a template instantiation */ } Swig_error(cparse_file, cparse_line, "'%s' is not defined as a template. (%s)\n", name, nodeType(n)); Delete(tname); Delete(parms); return 0; /* Found a match, but it's not a template of any kind. */ } } /* Search for partial specializations. * Example: template<typename T> class name<T *> { ... } * There are 3 types of template arguments: * (1) Template type arguments * (2) Template non type arguments * (3) Template template arguments * only (1) is really supported for partial specializations */ /* Rank each template parameter against the desired template parameters then build a matrix of best matches */ possiblepartials = NewList(); { char tmp[32]; List *partials; partials = Getattr(templ, "partials"); /* note that these partial specializations do not include explicit specializations */ if (partials) { Iterator pi; int parms_len = ParmList_len(parms); int *priorities_row; max_possible_partials = Len(partials); priorities_matrix = (int *)malloc(sizeof(int) * max_possible_partials * parms_len); /* slightly wasteful allocation for max possible matches */ priorities_row = priorities_matrix; for (pi = First(partials); pi.item; pi = Next(pi)) { Parm *p = parms; int all_parameters_match = 1; int i = 1; Parm *partialparms = Getattr(pi.item, "partialparms"); Parm *pp = partialparms; String *templcsymname = Getattr(pi.item, "templcsymname"); if (template_debug) { Printf(stdout, " checking match: '%s' (partial specialization)\n", templcsymname); } if (ParmList_len(partialparms) == parms_len) { while (p && pp) { SwigType *t; sprintf(tmp, "$%d", i); t = Getattr(p, "type"); if (!t) t = Getattr(p, "value"); if (t) { EMatch match = does_parm_match(t, Getattr(pp, "type"), tmp, tscope, priorities_row + i - 1); if (match < (int)PartiallySpecializedMatch) { all_parameters_match = 0; break; } } i++; p = nextSibling(p); pp = nextSibling(pp); } if (all_parameters_match) { Append(possiblepartials, pi.item); priorities_row += parms_len; } } } } } posslen = Len(possiblepartials); if (template_debug) { int i; if (posslen == 0) Printf(stdout, " matched partials: NONE\n"); else if (posslen == 1) Printf(stdout, " chosen partial: '%s'\n", Getattr(Getitem(possiblepartials, 0), "templcsymname")); else { Printf(stdout, " possibly matched partials:\n"); for (i = 0; i < posslen; i++) { Printf(stdout, " '%s'\n", Getattr(Getitem(possiblepartials, i), "templcsymname")); } } } if (posslen > 1) { /* Now go through all the possibly matched partial specialization templates and look for a non-ambiguous match. * Exact matches rank the highest and deduced parameters are ranked by how specialized they are, eg looking for * a match to const int *, the following rank (highest to lowest): * const int * (exact match) * const T * * T * * T * * An ambiguous example when attempting to match as either specialization could match: %template() X<int *, double *>; * template<typename T1, typename T2> X class {}; // primary template * template<typename T1> X<T1, double *> class {}; // specialization (1) * template<typename T2> X<int *, T2> class {}; // specialization (2) */ if (template_debug) { int row, col; int parms_len = ParmList_len(parms); Printf(stdout, " parameter priorities matrix (%d parms):\n", parms_len); for (row = 0; row < posslen; row++) { int *priorities_row = priorities_matrix + row*parms_len; Printf(stdout, " "); for (col = 0; col < parms_len; col++) { Printf(stdout, "%5d ", priorities_row[col]); } Printf(stdout, "\n"); } } { int row, col; int parms_len = ParmList_len(parms); /* Printf(stdout, " parameter priorities inverse matrix (%d parms):\n", parms_len); */ for (col = 0; col < parms_len; col++) { int *priorities_col = priorities_matrix + col; int maxpriority = -1; /* Printf(stdout, "max_possible_partials: %d col:%d\n", max_possible_partials, col); Printf(stdout, " "); */ /* determine the highest rank for this nth parameter */ for (row = 0; row < posslen; row++) { int *element_ptr = priorities_col + row*parms_len; int priority = *element_ptr; if (priority > maxpriority) maxpriority = priority; /* Printf(stdout, "%5d ", priority); */ } /* Printf(stdout, "\n"); */ /* flag all the parameters which equal the highest rank */ for (row = 0; row < posslen; row++) { int *element_ptr = priorities_col + row*parms_len; int priority = *element_ptr; *element_ptr = (priority >= maxpriority) ? 1 : 0; } } } { int row, col; int parms_len = ParmList_len(parms); Iterator pi = First(possiblepartials); Node *chosenpartials = NewList(); if (template_debug) Printf(stdout, " priority flags matrix:\n"); for (row = 0; row < posslen; row++) { int *priorities_row = priorities_matrix + row*parms_len; int highest_count = 0; /* count of highest priority parameters */ for (col = 0; col < parms_len; col++) { highest_count += priorities_row[col]; } if (template_debug) { Printf(stdout, " "); for (col = 0; col < parms_len; col++) { Printf(stdout, "%5d ", priorities_row[col]); } Printf(stdout, "\n"); } if (highest_count == parms_len) { Append(chosenpartials, pi.item); } pi = Next(pi); } if (Len(chosenpartials) > 0) { /* one or more best match found */ Delete(possiblepartials); possiblepartials = chosenpartials; posslen = Len(possiblepartials); } else { /* no best match found */ Delete(chosenpartials); } } } if (posslen > 0) { String *s = Getattr(Getitem(possiblepartials, 0), "templcsymname"); n = Swig_symbol_clookup_local(s, primary_scope); if (posslen > 1) { int i; if (n) { Swig_warning(WARN_PARSE_TEMPLATE_AMBIG, cparse_file, cparse_line, "Instantiation of template '%s' is ambiguous,\n", SwigType_namestr(tname)); Swig_warning(WARN_PARSE_TEMPLATE_AMBIG, Getfile(n), Getline(n), " instantiation '%s' used,\n", SwigType_namestr(Getattr(n, "name"))); } for (i = 1; i < posslen; i++) { String *templcsymname = Getattr(Getitem(possiblepartials, i), "templcsymname"); Node *ignored_node = Swig_symbol_clookup_local(templcsymname, primary_scope); assert(ignored_node); Swig_warning(WARN_PARSE_TEMPLATE_AMBIG, Getfile(ignored_node), Getline(ignored_node), " instantiation '%s' ignored.\n", SwigType_namestr(Getattr(ignored_node, "name"))); } } } if (!n) { if (template_debug) { Printf(stdout, " chosen primary template: '%s'\n", Getattr(templ, "name")); } n = templ; } } else { if (template_debug) { Printf(stdout, " primary template not found\n"); } /* Give up if primary (unspecialized) template not found as specializations will only exist if there is a primary template */ n = 0; } if (!n) { Swig_error(cparse_file, cparse_line, "Template '%s' undefined.\n", name); } else if (n) { String *nodeType = nodeType(n); if (!Equal(nodeType, "template")) { Swig_error(cparse_file, cparse_line, "'%s' is not defined as a template. (%s)\n", name, nodeType); n = 0; } } success: Delete(tname); Delete(possiblepartials); if ((template_debug) && (n)) { /* Printf(stdout, "Node: %p\n", n); Swig_print_node(n); */ Printf(stdout, " chosen template:'%s'\n", Getattr(n, "name")); } Delete(parms); free(priorities_matrix); return n; }
int yylex(void) { int l; if (!scan_init) { scanner_init(); } if (next_token) { l = next_token; next_token = 0; return l; } /* Printf(stdout,"%d\n", last_id);*/ l = yylook(); if (l == NONID) { last_id = 1; } else { last_id = 0; } /* yytext[yylen]= 0; Printf(stdout,"%d '%s' %d\n", l, yytext, last_id); */ /* We got some sort of non-white space object. We set the start_line variable unless it has already been set */ if (!cparse_start_line) { cparse_start_line = cparse_line; } /* Copy the lexene */ yytext[yylen] = 0; switch(l) { case NUM_INT: case NUM_FLOAT: case NUM_ULONG: case NUM_LONG: case NUM_UNSIGNED: case NUM_LONGLONG: case NUM_ULONGLONG: if (l == NUM_INT) yylval.dtype.type = T_INT; if (l == NUM_FLOAT) yylval.dtype.type = T_DOUBLE; if (l == NUM_ULONG) yylval.dtype.type = T_ULONG; if (l == NUM_LONG) yylval.dtype.type = T_LONG; if (l == NUM_UNSIGNED) yylval.dtype.type = T_UINT; if (l == NUM_LONGLONG) yylval.dtype.type = T_LONGLONG; if (l == NUM_ULONGLONG) yylval.dtype.type = T_ULONGLONG; yylval.dtype.val = NewString(yytext); yylval.dtype.bitfield = 0; yylval.dtype.throws = 0; return(l); break; case ID: if (yytext[0] != '%') { /* Look for keywords now */ if (strcmp(yytext,"int") == 0) { yylval.type = NewSwigType(T_INT); return(TYPE_INT); } if (strcmp(yytext,"double") == 0) { yylval.type = NewSwigType(T_DOUBLE); return(TYPE_DOUBLE); } if (strcmp(yytext,"void") == 0) { yylval.type = NewSwigType(T_VOID); return(TYPE_VOID); } if (strcmp(yytext,"char") == 0) { yylval.type = NewSwigType(T_CHAR); return(TYPE_CHAR); } if (strcmp(yytext,"short") == 0) { yylval.type = NewSwigType(T_SHORT); return(TYPE_SHORT); } if (strcmp(yytext,"long") == 0) { yylval.type = NewSwigType(T_LONG); return(TYPE_LONG); } if (strcmp(yytext,"float") == 0) { yylval.type = NewSwigType(T_FLOAT); return(TYPE_FLOAT); } if (strcmp(yytext,"signed") == 0) { yylval.type = NewSwigType(T_INT); return(TYPE_SIGNED); } if (strcmp(yytext,"unsigned") == 0) { yylval.type = NewSwigType(T_UINT); return(TYPE_UNSIGNED); } if (strcmp(yytext,"bool") == 0) { yylval.type = NewSwigType(T_BOOL); return(TYPE_BOOL); } /* C++ keywords */ if (cparse_cplusplus) { if (strcmp(yytext,"class") == 0) return(CLASS); if (strcmp(yytext,"private") == 0) return(PRIVATE); if (strcmp(yytext,"public") == 0) return(PUBLIC); if (strcmp(yytext,"protected") == 0) return(PROTECTED); if (strcmp(yytext,"friend") == 0) return(FRIEND); if (strcmp(yytext,"virtual") == 0) return(VIRTUAL); if (strcmp(yytext,"operator") == 0) { String *s = NewString("operator"); int c; int state = 0; int sticky = 0; int isconversion = 0; int count = 0; while ((c = nextchar())) { if (((c == '(') || (c == ';')) && state) { retract(1); break; } count++; if (!isspace(c)) { if ((!state) && (isalpha(c))) isconversion = 1; if (!state && !sticky) Putc(' ',s); Putc(c,s); sticky = 0; state = 1; } else { if (!sticky) Putc(' ',s); sticky = 1; } } Chop(s); yylval.str = s; while(Replaceall(s,"[ ", "[")); if (isconversion) { String *ns = Swig_symbol_string_qualify(s,0); yylval.str = ns; } if (isconversion && !rename_active) { char *t = Char(s) + 9; if (!((strcmp(t,"new") == 0) || (strcmp(t,"delete") == 0) || (strcmp(t,"new[]") == 0) || (strcmp(t,"delete[]") == 0))) { /* retract(strlen(t));*/ retract(count); return COPERATOR; } } return(OPERATOR); } if (strcmp(yytext,"throw") == 0) return(THROW); if (strcmp(yytext,"try") == 0) return (yylex()); if (strcmp(yytext,"catch") == 0) return (CATCH); if (strcmp(yytext,"inline") == 0) return(yylex()); if (strcmp(yytext,"mutable") == 0) return(yylex()); if (strcmp(yytext,"explicit") == 0) return(yylex()); if (strcmp(yytext,"export") == 0) return(yylex()); if (strcmp(yytext,"typename") == 0) return (TYPENAME); if (strcmp(yytext,"template") == 0) { yylval.ivalue = cparse_line; return(TEMPLATE); } if (strcmp(yytext,"delete") == 0) { return(DELETE); } if (strcmp(yytext,"using") == 0) { return(USING); } if (strcmp(yytext,"namespace") == 0) { return(NAMESPACE); } } else { if (strcmp(yytext,"class") == 0) { Swig_warning(WARN_PARSE_CLASS_KEYWORD,cparse_file,cparse_line, "class keyword used, but not in C++ mode.\n"); } } /* Objective-C keywords */ #ifdef OBJECTIVEC if ((ObjC) && (yytext[0] == '@')) { if (strcmp(yytext,"@interface") == 0) return (OC_INTERFACE); if (strcmp(yytext,"@end") == 0) return (OC_END); if (strcmp(yytext,"@public") == 0) return (OC_PUBLIC); if (strcmp(yytext,"@private") == 0) return (OC_PRIVATE); if (strcmp(yytext,"@protected") == 0) return (OC_PROTECTED); if (strcmp(yytext,"@class") == 0) return(OC_CLASS); if (strcmp(yytext,"@implementation") == 0) return(OC_IMPLEMENT); if (strcmp(yytext,"@protocol") == 0) return(OC_PROTOCOL); } #endif /* Misc keywords */ if (strcmp(yytext,"extern") == 0) return(EXTERN); if (strcmp(yytext,"const") == 0) return(CONST); if (strcmp(yytext,"static") == 0) return(STATIC); if (strcmp(yytext,"struct") == 0) return(STRUCT); if (strcmp(yytext,"union") == 0) return(UNION); if (strcmp(yytext,"enum") == 0) return(ENUM); if (strcmp(yytext,"sizeof") == 0) return(SIZEOF); if (strcmp(yytext,"typedef") == 0) { yylval.ivalue = 0; return(TYPEDEF); } /* Ignored keywords */ if (strcmp(yytext,"volatile") == 0) return(VOLATILE); /* SWIG directives */ } else { if (strcmp(yytext,"%module") == 0) return(MODULE); if (strcmp(yytext,"%insert") == 0) return(INSERT); if (strcmp(yytext,"%name") == 0) return(NAME); if (strcmp(yytext,"%rename") == 0) { rename_active = 1; return(RENAME); } if (strcmp(yytext,"%namewarn") == 0) { rename_active = 1; return (NAMEWARN); } if (strcmp(yytext,"%includefile") == 0) return(INCLUDE); if (strcmp(yytext,"%val") == 0) { Swig_warning(WARN_DEPRECATED_VAL, cparse_file, cparse_line, "%%val directive deprecated (ignored).\n"); return (yylex()); } if (strcmp(yytext,"%out") == 0) { Swig_warning(WARN_DEPRECATED_OUT, cparse_file, cparse_line, "%%out directive deprecated (ignored).\n"); return(yylex()); } if (strcmp(yytext,"%constant") == 0) return(CONSTANT); if (strcmp(yytext,"%typedef") == 0) { yylval.ivalue = 1; return(TYPEDEF); } if (strcmp(yytext,"%native") == 0) return(NATIVE); if (strcmp(yytext,"%pragma") == 0) return(PRAGMA); if (strcmp(yytext,"%extend") == 0) return(EXTEND); if (strcmp(yytext,"%fragment") == 0) return(FRAGMENT); if (strcmp(yytext,"%inline") == 0) return(INLINE); if (strcmp(yytext,"%typemap") == 0) return(TYPEMAP); if (strcmp(yytext,"%feature") == 0) return(FEATURE); if (strcmp(yytext,"%except") == 0) return(EXCEPT); if (strcmp(yytext,"%importfile") == 0) return(IMPORT); if (strcmp(yytext,"%echo") == 0) return(ECHO); if (strcmp(yytext,"%apply") == 0) return(APPLY); if (strcmp(yytext,"%clear") == 0) return(CLEAR); if (strcmp(yytext,"%types") == 0) return(TYPES); if (strcmp(yytext,"%parms") == 0) return(PARMS); if (strcmp(yytext,"%varargs") == 0) return(VARARGS); if (strcmp(yytext,"%template") == 0) return (SWIGTEMPLATE); if (strcmp(yytext,"%warn") == 0) return(WARN); } /* Have an unknown identifier, as a last step, we'll do a typedef lookup on it. */ /* Need to fix this */ if (check_typedef) { if (SwigType_istypedef(yytext)) { yylval.type = NewString(yytext); return(TYPE_TYPEDEF); } } yylval.id = Swig_copy_string(yytext); last_id = 1; return(ID); case POUND: return yylex(); default: return(l); } }
String *Swig_name_make(Node *n, String *prefix, const_String_or_char_ptr cname, SwigType *decl, String *oldname) { String *nname = 0; String *result = 0; String *name = NewString(cname); Hash *wrn = 0; String *rdecl = 0; String *rname = 0; /* very specific hack for template constructors/destructors */ #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_make: looking for %s %s %s %s\n", prefix, name, decl, oldname); #endif if (name && n && SwigType_istemplate(name)) { String *nodetype = nodeType(n); if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) { String *nprefix = NewStringEmpty(); String *nlast = NewStringEmpty(); String *tprefix; Swig_scopename_split(name, &nprefix, &nlast); tprefix = SwigType_templateprefix(nlast); Delete(nlast); if (Len(nprefix)) { Append(nprefix, "::"); Append(nprefix, tprefix); Delete(tprefix); rname = nprefix; } else { rname = tprefix; Delete(nprefix); } rdecl = Copy(decl); Replaceall(rdecl, name, rname); #ifdef SWIG_DEBUG Printf(stdout, "SWIG_name_make: use new name %s %s : %s %s\n", name, decl, rname, rdecl); #endif decl = rdecl; Delete(name); name = rname; } } if (rename_hash || rename_list || namewarn_hash || namewarn_list) { Hash *rn = Swig_name_object_get(Swig_name_rename_hash(), prefix, name, decl); if (!rn || !Swig_name_match_nameobj(rn, n)) { rn = Swig_name_nameobj_lget(Swig_name_rename_list(), n, prefix, name, decl); if (rn) { String *sfmt = Getattr(rn, "sourcefmt"); int fullname = GetFlag(rn, "fullname"); if (fullname && prefix) { String *sname = NewStringf("%s::%s", prefix, name); Delete(name); name = sname; prefix = 0; } if (sfmt) { String *sname = NewStringf(sfmt, name); Delete(name); name = sname; } } } if (rn) { String *newname = Getattr(rn, "name"); int fullname = GetFlag(rn, "fullname"); result = apply_rename(newname, fullname, prefix, name); } if (result && !Equal(result, name)) { /* operators in C++ allow aliases, we look for them */ char *cresult = Char(result); if (cresult && (strncmp(cresult, "operator ", 9) == 0)) { String *nresult = Swig_name_make(n, prefix, result, decl, oldname); if (!Equal(nresult, result)) { Delete(result); result = nresult; } else { Delete(nresult); } } } nname = result ? result : name; wrn = Swig_name_namewarn_get(n, prefix, nname, decl); if (wrn) { String *rename = Getattr(wrn, "rename"); if (rename) { String *msg = Getattr(wrn, "name"); int fullname = GetFlag(wrn, "fullname"); if (result) Delete(result); result = apply_rename(rename, fullname, prefix, name); if ((msg) && (Len(msg))) { if (!Getmeta(nname, "already_warned")) { if (n) { SWIG_WARN_NODE_BEGIN(n); Swig_warning(0, Getfile(n), Getline(n), "%s\n", msg); SWIG_WARN_NODE_END(n); } else { Swig_warning(0, Getfile(name), Getline(name), "%s\n", msg); } Setmeta(nname, "already_warned", "1"); } } } } } if (!result || !Len(result)) { if (result) Delete(result); if (oldname) { result = NewString(oldname); } else { result = NewString(cname); } } Delete(name); #ifdef SWIG_DEBUG Printf(stdout, "Swig_name_make: result '%s' '%s'\n", cname, result); #endif return result; }