Wrapper *NewWrapper(void) { Wrapper *w; w = (Wrapper *) malloc(sizeof(Wrapper)); w->localh = NewHash(); w->locals = NewStringEmpty(); w->code = NewStringEmpty(); w->def = NewStringEmpty(); return w; }
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); } }
String *Swig_name_disown(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) { String *r; String *f; String *rclassname; char *cname; rclassname = SwigType_namestr(classname); r = NewStringEmpty(); if (!naming_hash) naming_hash = NewHash(); f = Getattr(naming_hash, "disown"); if (!f) { Append(r, "disown_%n%c"); } else { Append(r, f); } cname = Char(rclassname); if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) { cname = strchr(cname, ' ') + 1; } replace_nspace(r, nspace); Replace(r, "%c", cname, DOH_REPLACE_ANY); Delete(rclassname); return r; }
/* ----------------------------------------------------------------------------- * Swig_string_regex() * * Executes a regular expression substitution. For example: * * Printf(stderr,"gsl%(regex:/GSL_.*_/\\1/)s","GSL_Hello_") -> gslHello * ----------------------------------------------------------------------------- */ String *Swig_string_regex(String *s) { const int pcre_options = 0; String *res = 0; pcre *compiled_pat = 0; const char *pcre_error, *input; int pcre_errorpos; String *pattern = 0, *subst = 0; int captures[30]; if (split_regex_pattern_subst(s, &pattern, &subst, &input)) { int rc; compiled_pat = pcre_compile( Char(pattern), pcre_options, &pcre_error, &pcre_errorpos, NULL); if (!compiled_pat) { Swig_error("SWIG", Getline(s), "PCRE compilation failed: '%s' in '%s':%i.\n", pcre_error, Char(pattern), pcre_errorpos); exit(1); } rc = pcre_exec(compiled_pat, NULL, input, (int)strlen(input), 0, 0, captures, 30); if (rc >= 0) { res = replace_captures(rc, input, subst, captures, pattern, s); } else if (rc != PCRE_ERROR_NOMATCH) { Swig_error("SWIG", Getline(s), "PCRE execution failed: error %d while matching \"%s\" using \"%s\".\n", rc, Char(pattern), input); exit(1); } } DohDelete(pattern); DohDelete(subst); pcre_free(compiled_pat); return res ? res : NewStringEmpty(); }
String *SwigType_lcaststr(const SwigType *s, const_String_or_char_ptr name) { String *result; result = NewStringEmpty(); if (SwigType_isarray(s)) { String *lstr = SwigType_lstr(s, 0); Printf(result, "(%s)%s", lstr, name); Delete(lstr); } else if (SwigType_isreference(s)) { String *str = SwigType_str(s, 0); Printf(result, "(%s)", str); Delete(str); if (name) Append(result, name); } else if (SwigType_isqualifier(s)) { String *lstr = SwigType_lstr(s, 0); Printf(result, "(%s)%s", lstr, name); Delete(lstr); } else { if (name) Append(result, name); } return result; }
String *Swig_string_escape(String *s) { String *ns; int c; ns = NewStringEmpty(); while ((c = Getc(s)) != EOF) { if (c == '\n') { Printf(ns, "\\n"); } else if (c == '\r') { Printf(ns, "\\r"); } else if (c == '\t') { Printf(ns, "\\t"); } else if (c == '\\') { Printf(ns, "\\\\"); } else if (c == '\'') { Printf(ns, "\\'"); } else if (c == '\"') { Printf(ns, "\\\""); } else if (c == ' ') { Putc(c, ns); } else if (!isgraph(c)) { if (c < 0) c += UCHAR_MAX + 1; Printf(ns, "\\%o", c); } else { Putc(c, ns); } } return ns; }
String *replace_captures(int num_captures, const char *input, String *subst, int captures[], String *pattern, String *s) { String *result = NewStringEmpty(); const char *p = Char(subst); while (*p) { /* Copy part without substitutions */ const char *q = strchr(p, '\\'); if (!q) { Write(result, p, strlen(p)); break; } Write(result, p, q - p); p = q + 1; /* Handle substitution */ if (*p == '\0') { Putc('\\', result); } else if (isdigit((unsigned char)*p)) { int group = *p++ - '0'; if (group < num_captures) { int l = captures[group*2], r = captures[group*2 + 1]; if (l != -1) { Write(result, input + l, r - l); } } else { Swig_error("SWIG", Getline(s), "PCRE capture replacement failed while matching \"%s\" using \"%s\" - request for group %d is greater than the number of captures %d.\n", Char(pattern), input, group, num_captures-1); } } } return result; }
SwigType *SwigType_strip_qualifiers(SwigType *t) { static Hash *memoize_stripped = 0; SwigType *r; List *l; Iterator ei; if (!memoize_stripped) memoize_stripped = NewHash(); r = Getattr(memoize_stripped, t); if (r) return Copy(r); l = SwigType_split(t); r = NewStringEmpty(); for (ei = First(l); ei.item; ei = Next(ei)) { if (SwigType_isqualifier(ei.item)) continue; Append(r, ei.item); } Delete(l); { String *key, *value; key = Copy(t); value = Copy(r); Setattr(memoize_stripped, key, value); Delete(key); Delete(value); } return r; }
String *Swig_cconstructor_call(String_or_char *name) { DOH *func; func = NewStringEmpty(); Printf(func, "(%s *) calloc(1, sizeof(%s))", name, name); return func; }
void DohSetDouble(DOH *obj, const DOH *name, double value) { DOH *temp; temp = NewStringEmpty(); Printf(temp,"%0.17f",value); Setattr(obj,(DOH *) name,temp); }
void DohSetInt(DOH *obj, const DOH *name, int value) { DOH *temp; temp = NewStringEmpty(); Printf(temp,"%d",value); Setattr(obj,(DOH *) name,temp); }
String *replace_captures(const char *input, String *subst, int captures[]) { String *result = NewStringEmpty(); const char *p = Char(subst); while (*p) { /* Copy part without substitutions */ const char *q = strchr(p, '\\'); if (!q) { Write(result, p, strlen(p)); break; } Write(result, p, q - p); p = q + 1; /* Handle substitution */ if (*p == '\0') { Putc('\\', result); } else if (isdigit(*p)) { int group = *p++ - '0'; int l = captures[group*2], r = captures[group*2 + 1]; if (l != -1) { Write(result, input + l, r - l); } } } return result; }
String *Swig_stringify_with_location(DOH *object) { String *str = NewStringEmpty(); if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT); if (object) { int line = Getline(object); String *formatted_filename = format_filename(Getfile(object)); if (line > 0) { Printf(str, diag_line_fmt, formatted_filename, line); } else { Printf(str, diag_eof_fmt, formatted_filename); } if (Len(object) == 0) { Printf(str, "[EMPTY]"); } else { Printf(str, "[%s]", object); } Delete(formatted_filename); } else { Printf(str, "[NULL]"); } return str; }
String *Swig_cconstructor_call(const_String_or_char_ptr name) { DOH *func; func = NewStringEmpty(); Printf(func, "calloc(1, sizeof(%s))", name); return func; }
DOHString *DohNewStringf(const DOHString_or_char *fmt, ...) { va_list ap; DOH *r; va_start(ap, fmt); r = NewStringEmpty(); DohvPrintf(r, Char(fmt), ap); va_end(ap); return (DOHString *) r; }
String *Swig_string_first_lower(String *s) { String *ns = NewStringEmpty(); char *cs = Char(s); if (cs && cs[0] != 0) { Putc(tolower((int)cs[0]), ns); Append(ns, cs + 1); } return ns; }
String *Swig_string_typecode(String *s) { String *ns; int c; String *tc; ns = NewStringEmpty(); while ((c = Getc(s)) != EOF) { if (c == '`') { String *str = 0; tc = NewStringEmpty(); while ((c = Getc(s)) != EOF) { if (c == '`') break; Putc(c, tc); } str = SwigType_str(tc, 0); Append(ns, str); Delete(str); } else { Putc(c, ns); if (c == '\'') { while ((c = Getc(s)) != EOF) { Putc(c, ns); if (c == '\'') break; if (c == '\\') { c = Getc(s); Putc(c, ns); } } } else if (c == '\"') { while ((c = Getc(s)) != EOF) { Putc(c, ns); if (c == '\"') break; if (c == '\\') { c = Getc(s); Putc(c, ns); } } } } } return ns; }
void Swig_warning(int wnum, const String_or_char *filename, int line, const char *fmt, ...) { String *out; char *msg; int wrn = 1; va_list ap; if (silence) return; if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT); va_start(ap, fmt); out = NewStringEmpty(); vPrintf(out, fmt, ap); msg = Char(out); if (isdigit((unsigned char) *msg)) { unsigned long result = strtoul(msg, &msg, 10); if (msg != Char(out)) { msg++; wnum = result; } } /* Check in the warning filter */ if (filter) { char temp[32]; char *c; char *f = Char(filter); sprintf(temp, "%d", wnum); while (*f != '\0' && (c = strstr(f, temp))) { if (*(c - 1) == '-') { wrn = 0; /* Warning disabled */ break; } if (*(c - 1) == '+') { wrn = 1; /* Warning enabled */ break; } f += strlen(temp); } } if (warnall || wrn) { String *formatted_filename = format_filename(filename); if (wnum) { Printf(stderr, wrn_wnum_fmt, formatted_filename, line, wnum); } else { Printf(stderr, wrn_nnum_fmt, formatted_filename, line); } Printf(stderr, "%s", msg); nwarning++; Delete(formatted_filename); } Delete(out); va_end(ap); }
SwigType *SwigType_add_qualifier(SwigType *t, const_String_or_char_ptr qual) { List *qlist; String *allq, *newq; int i, sz; const char *cqprev = 0; const char *c = Char(t); const char *cqual = Char(qual); /* if 't' has no qualifiers and 'qual' is a single qualifier, simply add it */ if ((strncmp(c, "q(", 2) != 0) && (strstr(cqual, " ") == 0)) { String *temp = NewStringf("q(%s).", cqual); Insert(t, 0, temp); Delete(temp); return t; } /* create string of all qualifiers */ if (strncmp(c, "q(", 2) == 0) { allq = SwigType_parm(t); Append(allq, " "); SwigType_del_element(t); /* delete old qualifier list from 't' */ } else { allq = NewStringEmpty(); } Append(allq, qual); /* create list of all qualifiers from string */ qlist = Split(allq, ' ', INT_MAX); Delete(allq); /* sort in alphabetical order */ SortList(qlist, Strcmp); /* create new qualifier string from unique elements of list */ sz = Len(qlist); newq = NewString("q("); for (i = 0; i < sz; ++i) { String *q = Getitem(qlist, i); const char *cq = Char(q); if (cqprev == 0 || strcmp(cqprev, cq) != 0) { if (i > 0) { Append(newq, " "); } Append(newq, q); cqprev = cq; } } Append(newq, ")."); Delete(qlist); /* replace qualifier string with new one */ Insert(t, 0, newq); Delete(newq); return t; }
String *Swig_cfunction_call(String_or_char *name, ParmList *parms) { String *func; int i = 0; int comma = 0; Parm *p = parms; String *nname; func = NewStringEmpty(); nname = SwigType_namestr(name); /* SWIGTEMPLATEDISAMBIGUATOR is compiler dependent (swiglabels.swg), - SUN Studio 9 requires 'template', - gcc-3.4 forbids the use of 'template'. the rest seems not caring very much, */ if (SwigType_istemplate(name)) { String *prefix = Swig_scopename_prefix(nname); if (!prefix || Len(prefix) == 0) { Printf(func, "%s(", nname); } else { String *last = Swig_scopename_last(nname); Printf(func, "%s::SWIGTEMPLATEDISAMBIGUATOR %s(", prefix, last); Delete(last); } Delete(prefix); } else { Printf(func, "%s(", nname); } Delete(nname); while (p) { SwigType *pt = Getattr(p, k_type); if ((SwigType_type(pt) != T_VOID)) { SwigType *rpt = SwigType_typedef_resolve_all(pt); String *pname = Swig_cparm_name(p, i); String *rcaststr = SwigType_rcaststr(rpt, pname); if (comma) { Printv(func, ",", rcaststr, NIL); } else { Append(func, rcaststr); } Delete(rpt); Delete(pname); Delete(rcaststr); comma = 1; i++; } p = nextSibling(p); } Append(func, ")"); return func; }
/* Remove all arrays */ SwigType *SwigType_pop_arrays(SwigType *t) { String *ta; assert(SwigType_isarray(t)); ta = NewStringEmpty(); while (SwigType_isarray(t)) { SwigType *td = SwigType_pop(t); Append(ta, td); Delete(td); } return ta; }
String *Swig_string_lower(String *s) { String *ns; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { Putc(tolower(c), ns); } return ns; }
String *Swig_string_title(String *s) { String *ns; int first = 1; int c; ns = NewStringEmpty(); Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { Putc(first ? toupper(c) : tolower(c), ns); first = 0; } return ns; }
String *ParmList_protostr(ParmList *p) { String *out = NewStringEmpty(); while (p) { String *pstr = SwigType_str(Getattr(p, "type"), 0); Append(out, pstr); p = nextSibling(p); if (p) { Append(out, ","); } Delete(pstr); } return out; }
String *ParmList_str(ParmList *p) { String *out = NewStringEmpty(); while (p) { String *type = Getattr(p, "type"); String *pstr = SwigType_str(type ? type : get_empty_type(), Getattr(p, "name")); Append(out, pstr); p = nextSibling(p); if (p) { Append(out, ","); } Delete(pstr); } return out; }
void Wrapper_print(Wrapper *w, File *f) { String *str; str = NewStringEmpty(); Printf(str, "%s\n", w->def); Printf(str, "%s\n", w->locals); Printf(str, "%s\n", w->code); if (Compact_mode == 1) Wrapper_compact_print(str, f); else Wrapper_pretty_print(str, f); Delete(str); }
String *Swig_name_decl(Node *n) { String *qname; String *decl; String *qualifier = Swig_symbol_qualified(n); String *name = Swig_scopename_last(Getattr(n, "name")); if (qualifier) qualifier = SwigType_namestr(qualifier); /* Very specific hack for template constructors/destructors */ if (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); Delete(name); name = tprefix; } } qname = NewString(""); if (qualifier && Len(qualifier) > 0) Printf(qname, "%s::", qualifier); Printf(qname, "%s", SwigType_str(name, 0)); decl = NewStringf("%s(%s)%s", qname, ParmList_errorstr(Getattr(n, "parms")), SwigType_isconst(Getattr(n, "decl")) ? " const" : ""); Delete(name); Delete(qualifier); Delete(qname); return decl; }
Scanner *NewScanner(void) { Scanner *s; s = (Scanner *) malloc(sizeof(Scanner)); s->line = 1; s->file = 0; s->nexttoken = -1; s->start_line = 1; s->yylen = 0; s->idstart = NULL; s->scanobjs = NewList(); s->text = NewStringEmpty(); s->str = 0; s->error = 0; s->freeze_line = 0; return s; }
String *Swig_cresult(SwigType *t, const String_or_char *name, const String_or_char *decl) { String *fcall; fcall = NewStringEmpty(); switch (SwigType_type(t)) { case T_VOID: break; case T_REFERENCE: { String *str = SwigType_str(t, "_result_ref"); Printf(fcall, "{\n"); Printf(fcall, "%s = ", str); Delete(str); } break; case T_USER: Printf(fcall, "%s = ", name); break; default: /* Normal return value */ { String *lstr = SwigType_lstr(t, 0); Printf(fcall, "%s = (%s)", name, lstr); Delete(lstr); } break; } /* Now print out function call */ Append(fcall, decl); /* A sick hack */ { char *c = Char(decl) + Len(decl) - 1; if (!((*c == ';') || (*c == '}'))) Append(fcall, ";"); } if (SwigType_type(t) == T_REFERENCE) { String *lstr = SwigType_lstr(t, 0); Printf(fcall, "\n%s = (%s) &_result_ref;\n", name, lstr); Append(fcall, "}"); Delete(lstr); } return fcall; }
String *SwigType_prefix(const SwigType *t) { char *c, *d; String *r = 0; c = Char(t); d = c + strlen(c); /* Check for a type constructor */ if ((d > c) && (*(d - 1) == '.')) d--; while (d > c) { d--; if (*d == '>') { int nest = 1; d--; while ((d > c) && (nest)) { if (*d == '>') nest++; if (*d == '<') nest--; d--; } } if (*d == ')') { /* Skip over params */ int nparen = 1; d--; while ((d > c) && (nparen)) { if (*d == ')') nparen++; if (*d == '(') nparen--; d--; } } if (*d == '.') { char t = *(d + 1); *(d + 1) = 0; r = NewString(c); *(d + 1) = t; return r; } } return NewStringEmpty(); }