805
void typemap_replace_vars(String *s, ParmList *locals, SwigType *type, SwigType *rtype, String *pname, String *lname, int index) {
805
int typemap_replace_vars(String *s, ParmList *locals, SwigType *type, SwigType *rtype, String *pname, String *lname, int index) {
809
int bare_substitution_count = 0;
810
811
Replaceall(s, "$typemap", "$TYPEMAP");
1160
1162
/* -----------------------------------------------------------------------------
1161
1163
* Swig_typemap_lookup()
1163
* Perform a typemap lookup (ala SWIG1.1)
1164
* ----------------------------------------------------------------------------- */
1166
String *Swig_typemap_lookup(const String_or_char *op, SwigType *type, String_or_char *pname,
1167
String_or_char *lname, String_or_char *source, String_or_char *target, Wrapper *f) {
1170
SwigType *mtype = 0;
1172
tm = Swig_typemap_search(op, type, pname, &mtype);
1176
s = Getattr(tm, "code");
1185
if (Cmp(s, "pass") == 0) {
1190
s = Copy(s); /* Make a local copy of the typemap code */
1192
locals = Getattr(tm, "locals");
1194
locals = CopyParmList(locals);
1196
/* This is wrong. It replaces locals in place. Need to fix this */
1197
if (mtype && SwigType_isarray(mtype)) {
1198
typemap_replace_vars(s, locals, mtype, type, pname, lname, 1);
1200
typemap_replace_vars(s, locals, type, type, pname, lname, 1);
1204
typemap_locals(s, locals, f, -1);
1207
replace_embedded_typemap(s);
1209
/* Now perform character replacements */
1210
Replace(s, "$source", source, DOH_REPLACE_ANY);
1211
Replace(s, "$target", target, DOH_REPLACE_ANY);
1214
String *tmname = Getattr(tm,"typemap");
1215
if (tmname) Replace(s,"$typemap",tmname, DOH_REPLACE_ANY);
1219
Replace(s, "$parmname", pname, DOH_REPLACE_ANY);
1220
/* Replace(s,"$name",pname,DOH_REPLACE_ANY); */
1227
/* -----------------------------------------------------------------------------
1228
* Swig_typemap_lookup_new()
1230
* Attach one or more typemaps to a node
1231
* op - typemap name, eg "out", "newfree"
1232
* node - the node to attach the typemaps to
1235
* ----------------------------------------------------------------------------- */
1237
String *Swig_typemap_lookup_new(const String_or_char *op, Node *node, const String_or_char *lname, Wrapper *f) {
1165
* Attach one or more typemaps to a node and optionally generate the typemap contents
1167
* op - typemap name, eg "out", "newfree"
1168
* node - the node to attach the typemaps to
1169
* lname - name of variable to substitute $1, $2 etc for
1170
* f - wrapper code to generate into if non null
1171
* actioncode - code to generate into f before the out typemap code, unless
1172
* the optimal attribute is set in the out typemap in which case
1173
* $1 in the out typemap will be replaced by the code in actioncode.
1174
* ----------------------------------------------------------------------------- */
1176
static String *Swig_typemap_lookup_impl(const String_or_char *op, Node *node, const String_or_char *lname, Wrapper *f, String *actioncode) {
1238
1177
SwigType *type;
1239
1178
SwigType *mtype = 0;
1298
1241
s = Copy(s); /* Make a local copy of the typemap code */
1243
/* Attach kwargs - ie the typemap attributes */
1244
kw = Getattr(tm, "kwargs");
1246
String *value = Copy(Getattr(kw, "value"));
1247
String *type = Getattr(kw, "type");
1248
char *ckwname = Char(Getattr(kw, "name"));
1250
String *mangle = Swig_string_mangle(type);
1251
Append(value, mangle);
1254
sprintf(temp, "%s:%s", cop, ckwname);
1255
Setattr(node, tmop_name(temp), value);
1256
if (Cmp(temp, "out:optimal") == 0)
1257
optimal_attribute = (Cmp(value, "0") != 0) ? 1 : 0;
1259
kw = nextSibling(kw);
1262
if (optimal_attribute) {
1263
/* Note: "out" typemap is the only typemap that will have the "optimal" attribute set.
1264
* If f and actioncode are NULL, then the caller is just looking to attach the "out" attributes
1265
* ie, not use the typemap code, otherwise both f and actioncode must be non null. */
1267
clname = Copy(actioncode);
1268
/* check that the code in the typemap can be used in this optimal way.
1269
* The code should be in the form "result = ...;\n". We need to extract
1270
* the "..." part. This may not be possible for various reasons, eg
1271
* code added by %exception. This optimal code generation is bit of a
1272
* hack and circumvents the normal requirement for a temporary variable
1273
* to hold the result returned from a wrapped function call.
1275
if (Strncmp(clname, "result = ", 9) == 0) {
1276
int numreplacements = Replace(clname, "result = ", "", DOH_REPLACE_ID_BEGIN);
1277
if (numreplacements == 1) {
1278
numreplacements = Replace(clname, ";\n", "", DOH_REPLACE_ID_END);
1279
if (numreplacements == 1) {
1280
if (Strchr(clname, ';') == 0) {
1283
optimal_substitution = 1;
1288
if (!optimal_substitution) {
1289
Swig_warning(WARN_TYPEMAP_OUT_OPTIMAL_IGNORED, Getfile(node), Getline(node), "Method %s usage of the optimal attribute in the out typemap at %s:%d ignored as the following cannot be used to generate optimal code: %s\n", Swig_name_decl(node), Getfile(s), Getline(s), clname);
1290
Delattr(node, "tmap:out:optimal");
1298
Append(f->code, actioncode);
1301
/* emit local variables declared in typemap, eg emit declarations for aa and bb in:
1302
* %typemap(in) foo (int aa, int bb) "..." */
1300
1303
locals = Getattr(tm, "locals");
1302
1305
locals = CopyParmList(locals);
1315
1318
if (mtype && SwigType_isarray(mtype)) {
1316
typemap_replace_vars(s, locals, mtype, type, pname, (char *) lname, 1);
1319
num_substitutions = typemap_replace_vars(s, locals, mtype, type, pname, (char *) lname, 1);
1318
typemap_replace_vars(s, locals, type, type, pname, (char *) lname, 1);
1321
num_substitutions = typemap_replace_vars(s, locals, type, type, pname, (char *) lname, 1);
1323
if (optimal_substitution && num_substitutions > 1)
1324
Swig_warning(WARN_TYPEMAP_OUT_OPTIMAL_MULTIPLE, Getfile(node), Getline(node), "Multiple calls to %s might be generated due to optimal attribute usage in the out typemap at %s:%d.\n", Swig_name_decl(node), Getfile(s), Getline(s));
1321
1326
if (locals && f) {
1322
1327
typemap_locals(s, locals, f, -1);
1324
1329
replace_embedded_typemap(s);
1326
String *tmname = Getattr(tm,"typemap");
1327
if (tmname) Replace(s,"$typemap",tmname, DOH_REPLACE_ANY);
1330
1331
Replace(s, "$name", pname, DOH_REPLACE_ANY);
1346
1347
Setattr(node, tmop_name(temp), "1");
1350
kw = Getattr(tm, "kwargs");
1352
String *value = Copy(Getattr(kw, "value"));
1353
String *type = Getattr(kw, "type");
1354
char *ckwname = Char(Getattr(kw, "name"));
1356
String *mangle = Swig_string_mangle(type);
1357
Append(value, mangle);
1360
sprintf(temp, "%s:%s", cop, ckwname);
1361
Setattr(node, tmop_name(temp), value);
1363
kw = nextSibling(kw);
1366
1350
/* Look for warnings */
1396
1377
if (sdef) { /* put 'ref' and 'newfree' codes together */
1397
1378
String *p = NewStringf("%s\n%s", sdef, s);
1387
String *Swig_typemap_lookup_out(const String_or_char *op, Node *node, const String_or_char *lname, Wrapper *f, String *actioncode) {
1389
assert(Cmp(op, "out") == 0);
1390
return Swig_typemap_lookup_impl(op, node, lname, f, actioncode);
1393
String *Swig_typemap_lookup(const String_or_char *op, Node *node, const String_or_char *lname, Wrapper *f) {
1394
return Swig_typemap_lookup_impl(op, node, lname, f, 0);
1405
1398
/* -----------------------------------------------------------------------------
1406
1399
* Swig_typemap_attach_kwargs()