PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-2.0.0/Source/Swig/naming.c

#
C | 1681 lines | 1238 code | 161 blank | 282 comment | 370 complexity | 4dc0115f9eaee81103435c1163d0feda MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * This file is part of SWIG, which is licensed as a whole under version 3
  3. * (or any later version) of the GNU General Public License. Some additional
  4. * terms also apply to certain portions of SWIG. The full details of the SWIG
  5. * license and copyrights can be found in the LICENSE and COPYRIGHT files
  6. * included with the SWIG source code as distributed by the SWIG developers
  7. * and at http://www.swig.org/legal.html.
  8. *
  9. * naming.c
  10. *
  11. * Functions for generating various kinds of names during code generation.
  12. *
  13. * Swig_name_register is used to register a format string for generating names.
  14. * The format string makes use of the following format specifiers:
  15. *
  16. * %c - class name is substituted
  17. * %f - function name is substituted
  18. * %m - member name is substituted
  19. * %n - namespace is substituted
  20. * %v - variable name is substituted
  21. * ----------------------------------------------------------------------------- */
  22. char cvsroot_naming_c[] = "$Id: naming.c 11896 2010-03-04 21:27:23Z wsfulton $";
  23. #include "swig.h"
  24. #include "cparse.h"
  25. #include <ctype.h>
  26. /* Hash table containing naming data */
  27. static Hash *naming_hash = 0;
  28. #if 0
  29. #define SWIG_DEBUG
  30. #endif
  31. /* -----------------------------------------------------------------------------
  32. * Swig_name_register()
  33. *
  34. * Register a new naming format.
  35. * ----------------------------------------------------------------------------- */
  36. void Swig_name_register(const_String_or_char_ptr method, const_String_or_char_ptr format) {
  37. if (!naming_hash)
  38. naming_hash = NewHash();
  39. Setattr(naming_hash, method, format);
  40. }
  41. void Swig_name_unregister(const_String_or_char_ptr method) {
  42. if (naming_hash) {
  43. Delattr(naming_hash, method);
  44. }
  45. }
  46. static int name_mangle(String *r) {
  47. char *c;
  48. int special;
  49. special = 0;
  50. Replaceall(r, "::", "_");
  51. c = Char(r);
  52. while (*c) {
  53. if (!isalnum((int) *c) && (*c != '_')) {
  54. special = 1;
  55. switch (*c) {
  56. case '+':
  57. *c = 'a';
  58. break;
  59. case '-':
  60. *c = 's';
  61. break;
  62. case '*':
  63. *c = 'm';
  64. break;
  65. case '/':
  66. *c = 'd';
  67. break;
  68. case '<':
  69. *c = 'l';
  70. break;
  71. case '>':
  72. *c = 'g';
  73. break;
  74. case '=':
  75. *c = 'e';
  76. break;
  77. case ',':
  78. *c = 'c';
  79. break;
  80. case '(':
  81. *c = 'p';
  82. break;
  83. case ')':
  84. *c = 'P';
  85. break;
  86. case '[':
  87. *c = 'b';
  88. break;
  89. case ']':
  90. *c = 'B';
  91. break;
  92. case '^':
  93. *c = 'x';
  94. break;
  95. case '&':
  96. *c = 'A';
  97. break;
  98. case '|':
  99. *c = 'o';
  100. break;
  101. case '~':
  102. *c = 'n';
  103. break;
  104. case '!':
  105. *c = 'N';
  106. break;
  107. case '%':
  108. *c = 'M';
  109. break;
  110. case '.':
  111. *c = 'f';
  112. break;
  113. case '?':
  114. *c = 'q';
  115. break;
  116. default:
  117. *c = '_';
  118. break;
  119. }
  120. }
  121. c++;
  122. }
  123. if (special)
  124. Append(r, "___");
  125. return special;
  126. }
  127. /* -----------------------------------------------------------------------------
  128. * replace_nspace()
  129. *
  130. * Mangles in the namespace from nspace by replacing %n in name if nspace feature required.
  131. * ----------------------------------------------------------------------------- */
  132. static void replace_nspace(String *name, const_String_or_char_ptr nspace) {
  133. if (nspace) {
  134. String *namspace = NewStringf("%s_", nspace);
  135. Replaceall(namspace, NSPACE_SEPARATOR, "_");
  136. Replace(name, "%n", namspace, DOH_REPLACE_ANY);
  137. Delete(namspace);
  138. } else {
  139. Replace(name, "%n", "", DOH_REPLACE_ANY);
  140. }
  141. }
  142. /* -----------------------------------------------------------------------------
  143. * Swig_name_mangle()
  144. *
  145. * Converts all of the non-identifier characters of a string to underscores.
  146. * ----------------------------------------------------------------------------- */
  147. String *Swig_name_mangle(const_String_or_char_ptr s) {
  148. #if 0
  149. String *r = NewString(s);
  150. name_mangle(r);
  151. return r;
  152. #else
  153. return Swig_string_mangle(s);
  154. #endif
  155. }
  156. /* -----------------------------------------------------------------------------
  157. * Swig_name_wrapper()
  158. *
  159. * Returns the name of a wrapper function.
  160. * ----------------------------------------------------------------------------- */
  161. String *Swig_name_wrapper(const_String_or_char_ptr fname) {
  162. String *r;
  163. String *f;
  164. r = NewStringEmpty();
  165. if (!naming_hash)
  166. naming_hash = NewHash();
  167. f = Getattr(naming_hash, "wrapper");
  168. if (!f) {
  169. Append(r, "_wrap_%f");
  170. } else {
  171. Append(r, f);
  172. }
  173. Replace(r, "%f", fname, DOH_REPLACE_ANY);
  174. name_mangle(r);
  175. return r;
  176. }
  177. /* -----------------------------------------------------------------------------
  178. * Swig_name_member()
  179. *
  180. * Returns the name of a class method.
  181. * ----------------------------------------------------------------------------- */
  182. String *Swig_name_member(const_String_or_char_ptr nspace, const_String_or_char_ptr classname, const_String_or_char_ptr membername) {
  183. String *r;
  184. String *f;
  185. String *rclassname;
  186. char *cname;
  187. rclassname = SwigType_namestr(classname);
  188. r = NewStringEmpty();
  189. if (!naming_hash)
  190. naming_hash = NewHash();
  191. f = Getattr(naming_hash, "member");
  192. if (!f) {
  193. Append(r, "%n%c_%m");
  194. } else {
  195. Append(r, f);
  196. }
  197. cname = Char(rclassname);
  198. if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
  199. cname = strchr(cname, ' ') + 1;
  200. }
  201. replace_nspace(r, nspace);
  202. Replace(r, "%c", cname, DOH_REPLACE_ANY);
  203. Replace(r, "%m", membername, DOH_REPLACE_ANY);
  204. /* name_mangle(r); */
  205. Delete(rclassname);
  206. return r;
  207. }
  208. /* -----------------------------------------------------------------------------
  209. * Swig_name_get()
  210. *
  211. * Returns the name of the accessor function used to get a variable.
  212. * ----------------------------------------------------------------------------- */
  213. String *Swig_name_get(const_String_or_char_ptr nspace, const_String_or_char_ptr vname) {
  214. String *r;
  215. String *f;
  216. #ifdef SWIG_DEBUG
  217. Printf(stdout, "Swig_name_get: '%s'\n", vname);
  218. #endif
  219. r = NewStringEmpty();
  220. if (!naming_hash)
  221. naming_hash = NewHash();
  222. f = Getattr(naming_hash, "get");
  223. if (!f) {
  224. Append(r, "%n%v_get");
  225. } else {
  226. Append(r, f);
  227. }
  228. replace_nspace(r, nspace);
  229. Replace(r, "%v", vname, DOH_REPLACE_ANY);
  230. /* name_mangle(r); */
  231. return r;
  232. }
  233. /* -----------------------------------------------------------------------------
  234. * Swig_name_set()
  235. *
  236. * Returns the name of the accessor function used to set a variable.
  237. * ----------------------------------------------------------------------------- */
  238. String *Swig_name_set(const_String_or_char_ptr nspace, const_String_or_char_ptr vname) {
  239. String *r;
  240. String *f;
  241. r = NewStringEmpty();
  242. if (!naming_hash)
  243. naming_hash = NewHash();
  244. f = Getattr(naming_hash, "set");
  245. if (!f) {
  246. Append(r, "%n%v_set");
  247. } else {
  248. Append(r, f);
  249. }
  250. replace_nspace(r, nspace);
  251. Replace(r, "%v", vname, DOH_REPLACE_ANY);
  252. /* name_mangle(r); */
  253. return r;
  254. }
  255. /* -----------------------------------------------------------------------------
  256. * Swig_name_construct()
  257. *
  258. * Returns the name of the accessor function used to create an object.
  259. * ----------------------------------------------------------------------------- */
  260. String *Swig_name_construct(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
  261. String *r;
  262. String *f;
  263. String *rclassname;
  264. char *cname;
  265. rclassname = SwigType_namestr(classname);
  266. r = NewStringEmpty();
  267. if (!naming_hash)
  268. naming_hash = NewHash();
  269. f = Getattr(naming_hash, "construct");
  270. if (!f) {
  271. Append(r, "new_%n%c");
  272. } else {
  273. Append(r, f);
  274. }
  275. cname = Char(rclassname);
  276. if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
  277. cname = strchr(cname, ' ') + 1;
  278. }
  279. replace_nspace(r, nspace);
  280. Replace(r, "%c", cname, DOH_REPLACE_ANY);
  281. Delete(rclassname);
  282. return r;
  283. }
  284. /* -----------------------------------------------------------------------------
  285. * Swig_name_copyconstructor()
  286. *
  287. * Returns the name of the accessor function used to copy an object.
  288. * ----------------------------------------------------------------------------- */
  289. String *Swig_name_copyconstructor(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
  290. String *r;
  291. String *f;
  292. String *rclassname;
  293. char *cname;
  294. rclassname = SwigType_namestr(classname);
  295. r = NewStringEmpty();
  296. if (!naming_hash)
  297. naming_hash = NewHash();
  298. f = Getattr(naming_hash, "copy");
  299. if (!f) {
  300. Append(r, "copy_%n%c");
  301. } else {
  302. Append(r, f);
  303. }
  304. cname = Char(rclassname);
  305. if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
  306. cname = strchr(cname, ' ') + 1;
  307. }
  308. replace_nspace(r, nspace);
  309. Replace(r, "%c", cname, DOH_REPLACE_ANY);
  310. Delete(rclassname);
  311. return r;
  312. }
  313. /* -----------------------------------------------------------------------------
  314. * Swig_name_destroy()
  315. *
  316. * Returns the name of the accessor function used to destroy an object.
  317. * ----------------------------------------------------------------------------- */
  318. String *Swig_name_destroy(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
  319. String *r;
  320. String *f;
  321. String *rclassname;
  322. char *cname;
  323. rclassname = SwigType_namestr(classname);
  324. r = NewStringEmpty();
  325. if (!naming_hash)
  326. naming_hash = NewHash();
  327. f = Getattr(naming_hash, "destroy");
  328. if (!f) {
  329. Append(r, "delete_%n%c");
  330. } else {
  331. Append(r, f);
  332. }
  333. cname = Char(rclassname);
  334. if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
  335. cname = strchr(cname, ' ') + 1;
  336. }
  337. replace_nspace(r, nspace);
  338. Replace(r, "%c", cname, DOH_REPLACE_ANY);
  339. Delete(rclassname);
  340. return r;
  341. }
  342. /* -----------------------------------------------------------------------------
  343. * Swig_name_disown()
  344. *
  345. * Returns the name of the accessor function used to disown an object.
  346. * ----------------------------------------------------------------------------- */
  347. String *Swig_name_disown(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
  348. String *r;
  349. String *f;
  350. String *rclassname;
  351. char *cname;
  352. rclassname = SwigType_namestr(classname);
  353. r = NewStringEmpty();
  354. if (!naming_hash)
  355. naming_hash = NewHash();
  356. f = Getattr(naming_hash, "disown");
  357. if (!f) {
  358. Append(r, "disown_%n%c");
  359. } else {
  360. Append(r, f);
  361. }
  362. cname = Char(rclassname);
  363. if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
  364. cname = strchr(cname, ' ') + 1;
  365. }
  366. replace_nspace(r, nspace);
  367. Replace(r, "%c", cname, DOH_REPLACE_ANY);
  368. Delete(rclassname);
  369. return r;
  370. }
  371. /* -----------------------------------------------------------------------------
  372. * Swig_name_object_set()
  373. *
  374. * Sets an object associated with a name and optional declarators.
  375. * ----------------------------------------------------------------------------- */
  376. void Swig_name_object_set(Hash *namehash, String *name, SwigType *decl, DOH *object) {
  377. DOH *n;
  378. #ifdef SWIG_DEBUG
  379. Printf(stdout, "Swig_name_object_set: '%s', '%s'\n", name, decl);
  380. #endif
  381. n = Getattr(namehash, name);
  382. if (!n) {
  383. n = NewHash();
  384. Setattr(namehash, name, n);
  385. Delete(n);
  386. }
  387. /* Add an object based on the declarator value */
  388. if (!decl) {
  389. Setattr(n, "start", object);
  390. } else {
  391. SwigType *cd = Copy(decl);
  392. Setattr(n, cd, object);
  393. Delete(cd);
  394. }
  395. }
  396. /* -----------------------------------------------------------------------------
  397. * Swig_name_object_get()
  398. *
  399. * Return an object associated with an optional class prefix, name, and
  400. * declarator. This function operates according to name matching rules
  401. * described for the %rename directive in the SWIG manual.
  402. * ----------------------------------------------------------------------------- */
  403. static DOH *get_object(Hash *n, String *decl) {
  404. DOH *rn = 0;
  405. if (!n)
  406. return 0;
  407. if (decl) {
  408. rn = Getattr(n, decl);
  409. } else {
  410. rn = Getattr(n, "start");
  411. }
  412. return rn;
  413. }
  414. static
  415. DOH *name_object_get(Hash *namehash, String *tname, SwigType *decl, SwigType *ncdecl) {
  416. DOH *rn = 0;
  417. Hash *n = Getattr(namehash, tname);
  418. if (n) {
  419. rn = get_object(n, decl);
  420. if ((!rn) && ncdecl)
  421. rn = get_object(n, ncdecl);
  422. if (!rn)
  423. rn = get_object(n, 0);
  424. }
  425. return rn;
  426. }
  427. DOH *Swig_name_object_get(Hash *namehash, String *prefix, String *name, SwigType *decl) {
  428. String *tname = NewStringEmpty();
  429. DOH *rn = 0;
  430. char *ncdecl = 0;
  431. if (!namehash)
  432. return 0;
  433. /* DB: This removed to more tightly control feature/name matching */
  434. /* if ((decl) && (SwigType_isqualifier(decl))) {
  435. ncdecl = strchr(Char(decl),'.');
  436. ncdecl++;
  437. }
  438. */
  439. #ifdef SWIG_DEBUG
  440. Printf(stdout, "Swig_name_object_get: '%s' '%s', '%s'\n", prefix, name, decl);
  441. #endif
  442. /* Perform a class-based lookup (if class prefix supplied) */
  443. if (prefix) {
  444. if (Len(prefix)) {
  445. Printf(tname, "%s::%s", prefix, name);
  446. rn = name_object_get(namehash, tname, decl, ncdecl);
  447. if (!rn) {
  448. String *cls = Swig_scopename_last(prefix);
  449. if (!Equal(cls, prefix)) {
  450. Clear(tname);
  451. Printf(tname, "*::%s::%s", cls, name);
  452. rn = name_object_get(namehash, tname, decl, ncdecl);
  453. }
  454. Delete(cls);
  455. }
  456. /* A template-based class lookup, check name first */
  457. if (!rn) {
  458. String *t_name = SwigType_istemplate_templateprefix(name);
  459. if (t_name)
  460. rn = Swig_name_object_get(namehash, prefix, t_name, decl);
  461. Delete(t_name);
  462. }
  463. }
  464. /* A wildcard-based class lookup */
  465. if (!rn) {
  466. Clear(tname);
  467. Printf(tname, "*::%s", name);
  468. rn = name_object_get(namehash, tname, decl, ncdecl);
  469. }
  470. } else {
  471. /* Lookup in the global namespace only */
  472. Clear(tname);
  473. Printf(tname, "::%s", name);
  474. rn = name_object_get(namehash, tname, decl, ncdecl);
  475. }
  476. /* Catch-all */
  477. if (!rn) {
  478. rn = name_object_get(namehash, name, decl, ncdecl);
  479. }
  480. if (!rn && Swig_scopename_check(name)) {
  481. String *nprefix = NewStringEmpty();
  482. String *nlast = NewStringEmpty();
  483. Swig_scopename_split(name, &nprefix, &nlast);
  484. rn = name_object_get(namehash, nlast, decl, ncdecl);
  485. Delete(nlast);
  486. Delete(nprefix);
  487. }
  488. Delete(tname);
  489. #ifdef SWIG_DEBUG
  490. Printf(stdout, "Swig_name_object_get: found %d\n", rn ? 1 : 0);
  491. #endif
  492. return rn;
  493. }
  494. /* -----------------------------------------------------------------------------
  495. * Swig_name_object_inherit()
  496. *
  497. * Implements name-based inheritance scheme.
  498. * ----------------------------------------------------------------------------- */
  499. void Swig_name_object_inherit(Hash *namehash, String *base, String *derived) {
  500. Iterator ki;
  501. String *bprefix;
  502. String *dprefix;
  503. char *cbprefix;
  504. int plen;
  505. if (!namehash)
  506. return;
  507. bprefix = NewStringf("%s::", base);
  508. dprefix = NewStringf("%s::", derived);
  509. cbprefix = Char(bprefix);
  510. plen = strlen(cbprefix);
  511. for (ki = First(namehash); ki.key; ki = Next(ki)) {
  512. char *k = Char(ki.key);
  513. if (strncmp(k, cbprefix, plen) == 0) {
  514. Iterator oi;
  515. String *nkey = NewStringf("%s%s", dprefix, k + plen);
  516. Hash *n = ki.item;
  517. Hash *newh = Getattr(namehash, nkey);
  518. if (!newh) {
  519. newh = NewHash();
  520. Setattr(namehash, nkey, newh);
  521. Delete(newh);
  522. }
  523. for (oi = First(n); oi.key; oi = Next(oi)) {
  524. if (!Getattr(newh, oi.key)) {
  525. String *ci = Copy(oi.item);
  526. Setattr(newh, oi.key, ci);
  527. Delete(ci);
  528. }
  529. }
  530. Delete(nkey);
  531. }
  532. }
  533. Delete(bprefix);
  534. Delete(dprefix);
  535. }
  536. /* -----------------------------------------------------------------------------
  537. * merge_features()
  538. *
  539. * Given a hash, this function merges the features in the hash into the node.
  540. * ----------------------------------------------------------------------------- */
  541. static void merge_features(Hash *features, Node *n) {
  542. Iterator ki;
  543. if (!features)
  544. return;
  545. for (ki = First(features); ki.key; ki = Next(ki)) {
  546. String *ci = Copy(ki.item);
  547. Setattr(n, ki.key, ci);
  548. Delete(ci);
  549. }
  550. }
  551. /* -----------------------------------------------------------------------------
  552. * Swig_features_get()
  553. *
  554. * Attaches any features in the features hash to the node that matches
  555. * the declaration, decl.
  556. * ----------------------------------------------------------------------------- */
  557. static
  558. void features_get(Hash *features, const String *tname, SwigType *decl, SwigType *ncdecl, Node *node) {
  559. Node *n = Getattr(features, tname);
  560. #ifdef SWIG_DEBUG
  561. Printf(stdout, " features_get: %s\n", tname);
  562. #endif
  563. if (n) {
  564. merge_features(get_object(n, 0), node);
  565. if (ncdecl)
  566. merge_features(get_object(n, ncdecl), node);
  567. merge_features(get_object(n, decl), node);
  568. }
  569. }
  570. void Swig_features_get(Hash *features, String *prefix, String *name, SwigType *decl, Node *node) {
  571. char *ncdecl = 0;
  572. String *rdecl = 0;
  573. String *rname = 0;
  574. if (!features)
  575. return;
  576. /* MM: This removed to more tightly control feature/name matching */
  577. /*
  578. if ((decl) && (SwigType_isqualifier(decl))) {
  579. ncdecl = strchr(Char(decl),'.');
  580. ncdecl++;
  581. }
  582. */
  583. /* very specific hack for template constructors/destructors */
  584. if (name && SwigType_istemplate(name)) {
  585. String *nodetype = nodeType(node);
  586. if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
  587. String *nprefix = NewStringEmpty();
  588. String *nlast = NewStringEmpty();
  589. String *tprefix;
  590. Swig_scopename_split(name, &nprefix, &nlast);
  591. tprefix = SwigType_templateprefix(nlast);
  592. Delete(nlast);
  593. if (Len(nprefix)) {
  594. Append(nprefix, "::");
  595. Append(nprefix, tprefix);
  596. Delete(tprefix);
  597. rname = nprefix;
  598. } else {
  599. rname = tprefix;
  600. Delete(nprefix);
  601. }
  602. rdecl = Copy(decl);
  603. Replaceall(rdecl, name, rname);
  604. decl = rdecl;
  605. name = rname;
  606. }
  607. }
  608. #ifdef SWIG_DEBUG
  609. Printf(stdout, "Swig_features_get: '%s' '%s' '%s'\n", prefix, name, decl);
  610. #endif
  611. /* Global features */
  612. features_get(features, "", 0, 0, node);
  613. if (name) {
  614. String *tname = NewStringEmpty();
  615. /* add features for 'root' template */
  616. String *dname = SwigType_istemplate_templateprefix(name);
  617. if (dname) {
  618. features_get(features, dname, decl, ncdecl, node);
  619. }
  620. /* Catch-all */
  621. features_get(features, name, decl, ncdecl, node);
  622. /* Perform a class-based lookup (if class prefix supplied) */
  623. if (prefix) {
  624. /* A class-generic feature */
  625. if (Len(prefix)) {
  626. Printf(tname, "%s::", prefix);
  627. features_get(features, tname, decl, ncdecl, node);
  628. }
  629. /* A wildcard-based class lookup */
  630. Clear(tname);
  631. Printf(tname, "*::%s", name);
  632. features_get(features, tname, decl, ncdecl, node);
  633. /* A specific class lookup */
  634. if (Len(prefix)) {
  635. /* A template-based class lookup */
  636. String *tprefix = SwigType_istemplate_templateprefix(prefix);
  637. if (tprefix) {
  638. Clear(tname);
  639. Printf(tname, "%s::%s", tprefix, name);
  640. features_get(features, tname, decl, ncdecl, node);
  641. }
  642. Clear(tname);
  643. Printf(tname, "%s::%s", prefix, name);
  644. features_get(features, tname, decl, ncdecl, node);
  645. Delete(tprefix);
  646. }
  647. } else {
  648. /* Lookup in the global namespace only */
  649. Clear(tname);
  650. Printf(tname, "::%s", name);
  651. features_get(features, tname, decl, ncdecl, node);
  652. }
  653. Delete(tname);
  654. Delete(dname);
  655. }
  656. if (name && SwigType_istemplate(name)) {
  657. /* add features for complete template type */
  658. String *dname = Swig_symbol_template_deftype(name, 0);
  659. if (!Equal(dname, name)) {
  660. Swig_features_get(features, prefix, dname, decl, node);
  661. }
  662. Delete(dname);
  663. }
  664. if (rname)
  665. Delete(rname);
  666. if (rdecl)
  667. Delete(rdecl);
  668. }
  669. /* -----------------------------------------------------------------------------
  670. * Swig_feature_set()
  671. *
  672. * Sets a feature name and value. Also sets optional feature attributes as
  673. * passed in by featureattribs. Optional feature attributes are given a full name
  674. * concatenating the feature name plus ':' plus the attribute name.
  675. * ----------------------------------------------------------------------------- */
  676. void Swig_feature_set(Hash *features, const_String_or_char_ptr name, SwigType *decl, const_String_or_char_ptr featurename, String *value, Hash *featureattribs) {
  677. Hash *n;
  678. Hash *fhash;
  679. #ifdef SWIG_DEBUG
  680. Printf(stdout, "Swig_feature_set: '%s' '%s' '%s' '%s'\n", name, decl, featurename, value);
  681. #endif
  682. n = Getattr(features, name);
  683. if (!n) {
  684. n = NewHash();
  685. Setattr(features, name, n);
  686. Delete(n);
  687. }
  688. if (!decl) {
  689. fhash = Getattr(n, "start");
  690. if (!fhash) {
  691. fhash = NewHash();
  692. Setattr(n, "start", fhash);
  693. Delete(fhash);
  694. }
  695. } else {
  696. fhash = Getattr(n, decl);
  697. if (!fhash) {
  698. String *cdecl_ = Copy(decl);
  699. fhash = NewHash();
  700. Setattr(n, cdecl_, fhash);
  701. Delete(cdecl_);
  702. Delete(fhash);
  703. }
  704. }
  705. if (value) {
  706. Setattr(fhash, featurename, value);
  707. } else {
  708. Delattr(fhash, featurename);
  709. }
  710. {
  711. /* Add in the optional feature attributes */
  712. Hash *attribs = featureattribs;
  713. while (attribs) {
  714. String *attribname = Getattr(attribs, "name");
  715. String *featureattribname = NewStringf("%s:%s", featurename, attribname);
  716. if (value) {
  717. String *attribvalue = Getattr(attribs, "value");
  718. Setattr(fhash, featureattribname, attribvalue);
  719. } else {
  720. Delattr(fhash, featureattribname);
  721. }
  722. attribs = nextSibling(attribs);
  723. Delete(featureattribname);
  724. }
  725. }
  726. if (name && SwigType_istemplate(name)) {
  727. String *dname = Swig_symbol_template_deftype(name, 0);
  728. if (Strcmp(dname, name)) {
  729. Swig_feature_set(features, dname, decl, featurename, value, featureattribs);
  730. }
  731. Delete(dname);
  732. }
  733. }
  734. /* -----------------------------------------------------------------------------
  735. * The rename/namewarn engine
  736. *
  737. * Code below was in parser.y for a while
  738. * ----------------------------------------------------------------------------- */
  739. static Hash *namewarn_hash = 0;
  740. Hash *Swig_name_namewarn_hash() {
  741. if (!namewarn_hash)
  742. namewarn_hash = NewHash();
  743. return namewarn_hash;
  744. }
  745. static Hash *rename_hash = 0;
  746. Hash *Swig_name_rename_hash() {
  747. if (!rename_hash)
  748. rename_hash = NewHash();
  749. return rename_hash;
  750. }
  751. static List *namewarn_list = 0;
  752. List *Swig_name_namewarn_list() {
  753. if (!namewarn_list)
  754. namewarn_list = NewList();
  755. return namewarn_list;
  756. }
  757. static List *rename_list = 0;
  758. List *Swig_name_rename_list() {
  759. if (!rename_list)
  760. rename_list = NewList();
  761. return rename_list;
  762. }
  763. /* -----------------------------------------------------------------------------
  764. * int Swig_need_name_warning(Node *n)
  765. *
  766. * Detects if a node needs name warnings
  767. *
  768. * ----------------------------------------------------------------------------- */
  769. int Swig_need_name_warning(Node *n) {
  770. int need = 1;
  771. /*
  772. we don't use name warnings for:
  773. - class forwards, no symbol is generated at the target language.
  774. - template declarations, only for real instances using %template(name).
  775. - typedefs, they have no effect at the target language.
  776. */
  777. if (checkAttribute(n, "nodeType", "classforward")) {
  778. need = 0;
  779. } else if (checkAttribute(n, "storage", "typedef")) {
  780. need = 0;
  781. } else if (Getattr(n, "hidden")) {
  782. need = 0;
  783. } else if (Getattr(n, "ignore")) {
  784. need = 0;
  785. } else if (Getattr(n, "templatetype")) {
  786. need = 0;
  787. }
  788. return need;
  789. }
  790. /* -----------------------------------------------------------------------------
  791. * int Swig_need_redefined_warn()
  792. *
  793. * Detects when a redefined object needs a warning
  794. *
  795. * ----------------------------------------------------------------------------- */
  796. static int nodes_are_equivalent(Node *a, Node *b, int a_inclass) {
  797. /* they must have the same type */
  798. String *ta = nodeType(a);
  799. String *tb = nodeType(b);
  800. if (Cmp(ta, tb) != 0)
  801. return 0;
  802. /* cdecl case */
  803. if (Cmp(ta, "cdecl") == 0) {
  804. /* typedef */
  805. String *a_storage = Getattr(a, "storage");
  806. String *b_storage = Getattr(b, "storage");
  807. if ((Cmp(a_storage, "typedef") == 0)
  808. || (Cmp(b_storage, "typedef") == 0)) {
  809. if (Cmp(a_storage, b_storage) == 0) {
  810. String *a_type = (Getattr(a, "type"));
  811. String *b_type = (Getattr(b, "type"));
  812. if (Cmp(a_type, b_type) == 0)
  813. return 1;
  814. }
  815. return 0;
  816. }
  817. /* static functions */
  818. if ((Cmp(a_storage, "static") == 0)
  819. || (Cmp(b_storage, "static") == 0)) {
  820. if (Cmp(a_storage, b_storage) != 0)
  821. return 0;
  822. }
  823. /* friend methods */
  824. if (!a_inclass || (Cmp(a_storage, "friend") == 0)) {
  825. /* check declaration */
  826. String *a_decl = (Getattr(a, "decl"));
  827. String *b_decl = (Getattr(b, "decl"));
  828. if (Cmp(a_decl, b_decl) == 0) {
  829. /* check return type */
  830. String *a_type = (Getattr(a, "type"));
  831. String *b_type = (Getattr(b, "type"));
  832. if (Cmp(a_type, b_type) == 0) {
  833. /* check parameters */
  834. Parm *ap = (Getattr(a, "parms"));
  835. Parm *bp = (Getattr(b, "parms"));
  836. while (ap && bp) {
  837. SwigType *at = Getattr(ap, "type");
  838. SwigType *bt = Getattr(bp, "type");
  839. if (Cmp(at, bt) != 0)
  840. return 0;
  841. ap = nextSibling(ap);
  842. bp = nextSibling(bp);
  843. }
  844. if (ap || bp) {
  845. return 0;
  846. } else {
  847. Node *a_template = Getattr(a, "template");
  848. Node *b_template = Getattr(b, "template");
  849. /* Not equivalent if one is a template instantiation (via %template) and the other is a non-templated function */
  850. if ((a_template && !b_template) || (!a_template && b_template))
  851. return 0;
  852. }
  853. return 1;
  854. }
  855. }
  856. }
  857. } else {
  858. /* %constant case */
  859. String *a_storage = Getattr(a, "storage");
  860. String *b_storage = Getattr(b, "storage");
  861. if ((Cmp(a_storage, "%constant") == 0)
  862. || (Cmp(b_storage, "%constant") == 0)) {
  863. if (Cmp(a_storage, b_storage) == 0) {
  864. String *a_type = (Getattr(a, "type"));
  865. String *b_type = (Getattr(b, "type"));
  866. if ((Cmp(a_type, b_type) == 0)
  867. && (Cmp(Getattr(a, "value"), Getattr(b, "value")) == 0))
  868. return 1;
  869. }
  870. return 0;
  871. }
  872. }
  873. return 0;
  874. }
  875. int Swig_need_redefined_warn(Node *a, Node *b, int InClass) {
  876. String *a_name = Getattr(a, "name");
  877. String *b_name = Getattr(b, "name");
  878. String *a_symname = Getattr(a, "sym:name");
  879. String *b_symname = Getattr(b, "sym:name");
  880. /* always send a warning if a 'rename' is involved */
  881. if ((a_symname && !Equal(a_symname, a_name))
  882. || (b_symname && !Equal(b_symname, b_name))) {
  883. if (!Equal(a_name, b_name)) {
  884. return 1;
  885. }
  886. }
  887. return !nodes_are_equivalent(a, b, InClass);
  888. }
  889. /* -----------------------------------------------------------------------------
  890. * int Swig_need_protected(Node* n)
  891. *
  892. * Detects when we need to fully register the protected member.
  893. * This is basically any protected members when the allprotected mode is set.
  894. * Otherwise we take just the protected virtual methods and non-static methods
  895. * (potentially virtual methods) as well as constructors/destructors.
  896. *
  897. * ----------------------------------------------------------------------------- */
  898. int Swig_need_protected(Node *n) {
  899. String *nodetype = nodeType(n);
  900. if (checkAttribute(n, "access", "protected")) {
  901. if ((Equal(nodetype, "cdecl"))) {
  902. if (Swig_director_mode() && Swig_director_protected_mode() && Swig_all_protected_mode()) {
  903. return 1;
  904. }
  905. if (SwigType_isfunction(Getattr(n, "decl"))) {
  906. String *storage = Getattr(n, "storage");
  907. /* The function is declared virtual, or it has no storage. This eliminates typedef, static etc. */
  908. return !storage || Equal(storage, "virtual");
  909. }
  910. } else if (Equal(nodetype, "constructor") || Equal(nodetype, "destructor")) {
  911. return 1;
  912. }
  913. }
  914. return 0;
  915. }
  916. /* -----------------------------------------------------------------------------
  917. * void Swig_name_nameobj_add()
  918. *
  919. * Add nameobj (rename/namewarn)
  920. *
  921. * ----------------------------------------------------------------------------- */
  922. static List *Swig_make_attrlist(const char *ckey) {
  923. List *list = NewList();
  924. const char *cattr = strchr(ckey, '$');
  925. if (cattr) {
  926. String *nattr;
  927. const char *rattr = strchr(++cattr, '$');
  928. while (rattr) {
  929. nattr = NewStringWithSize(cattr, rattr - cattr);
  930. Append(list, nattr);
  931. Delete(nattr);
  932. cattr = rattr + 1;
  933. rattr = strchr(cattr, '$');
  934. }
  935. nattr = NewString(cattr);
  936. Append(list, nattr);
  937. Delete(nattr);
  938. } else {
  939. Append(list, "nodeType");
  940. }
  941. return list;
  942. }
  943. static void Swig_name_object_attach_keys(const char *keys[], Hash *nameobj) {
  944. Node *kw = nextSibling(nameobj);
  945. List *matchlist = 0;
  946. while (kw) {
  947. Node *next = nextSibling(kw);
  948. String *kname = Getattr(kw, "name");
  949. char *ckey = kname ? Char(kname) : 0;
  950. if (ckey) {
  951. const char **rkey;
  952. int isnotmatch = 0;
  953. int isrxsmatch = 0;
  954. if ((strncmp(ckey, "match", 5) == 0)
  955. || (isnotmatch = (strncmp(ckey, "notmatch", 8) == 0))
  956. || (isrxsmatch = (strncmp(ckey, "rxsmatch", 8) == 0))
  957. || (isnotmatch = isrxsmatch = (strncmp(ckey, "notrxsmatch", 11) == 0))) {
  958. Hash *mi = NewHash();
  959. List *attrlist = Swig_make_attrlist(ckey);
  960. if (!matchlist)
  961. matchlist = NewList();
  962. Setattr(mi, "value", Getattr(kw, "value"));
  963. Setattr(mi, "attrlist", attrlist);
  964. #ifdef SWIG_DEBUG
  965. if (isrxsmatch)
  966. Printf(stdout, "rxsmatch to use: %s %s %s\n", ckey, Getattr(kw, "value"), attrlist);
  967. #endif
  968. if (isnotmatch)
  969. SetFlag(mi, "notmatch");
  970. if (isrxsmatch)
  971. SetFlag(mi, "rxsmatch");
  972. Delete(attrlist);
  973. Append(matchlist, mi);
  974. Delete(mi);
  975. removeNode(kw);
  976. } else {
  977. for (rkey = keys; *rkey != 0; ++rkey) {
  978. if (strcmp(ckey, *rkey) == 0) {
  979. Setattr(nameobj, *rkey, Getattr(kw, "value"));
  980. removeNode(kw);
  981. }
  982. }
  983. }
  984. }
  985. kw = next;
  986. }
  987. if (matchlist) {
  988. Setattr(nameobj, "matchlist", matchlist);
  989. Delete(matchlist);
  990. }
  991. }
  992. void Swig_name_nameobj_add(Hash *name_hash, List *name_list, String *prefix, String *name, SwigType *decl, Hash *nameobj) {
  993. String *nname = 0;
  994. if (name && Len(name)) {
  995. String *target_fmt = Getattr(nameobj, "targetfmt");
  996. nname = prefix ? NewStringf("%s::%s", prefix, name) : NewString(name);
  997. if (target_fmt) {
  998. String *tmp = NewStringf(target_fmt, nname);
  999. Delete(nname);
  1000. nname = tmp;
  1001. }
  1002. }
  1003. if (!nname || !Len(nname) || Getattr(nameobj, "fullname") || /* any of these options trigger a 'list' nameobj */
  1004. Getattr(nameobj, "sourcefmt") || Getattr(nameobj, "matchlist")) {
  1005. if (decl)
  1006. Setattr(nameobj, "decl", decl);
  1007. if (nname && Len(nname))
  1008. Setattr(nameobj, "targetname", nname);
  1009. /* put the new nameobj at the beginnig of the list, such that the
  1010. last inserted rule take precedence */
  1011. Insert(name_list, 0, nameobj);
  1012. } else {
  1013. /* here we add an old 'hash' nameobj, simple and fast */
  1014. Swig_name_object_set(name_hash, nname, decl, nameobj);
  1015. }
  1016. Delete(nname);
  1017. }
  1018. /* -----------------------------------------------------------------------------
  1019. * int Swig_name_match_nameobj()
  1020. *
  1021. * Apply and check the nameobj's math list to the node
  1022. *
  1023. * ----------------------------------------------------------------------------- */
  1024. static DOH *Swig_get_lattr(Node *n, List *lattr) {
  1025. DOH *res = 0;
  1026. int ilen = Len(lattr);
  1027. int i;
  1028. for (i = 0; n && (i < ilen); ++i) {
  1029. String *nattr = Getitem(lattr, i);
  1030. res = Getattr(n, nattr);
  1031. #ifdef SWIG_DEBUG
  1032. if (!res) {
  1033. Printf(stdout, "missing %s %s %s\n", nattr, Getattr(n, "name"), Getattr(n, "member"));
  1034. } else {
  1035. Printf(stdout, "lattr %d %s %s\n", i, nattr, DohIsString(res) ? res : Getattr(res, "name"));
  1036. }
  1037. #endif
  1038. n = res;
  1039. }
  1040. return res;
  1041. }
  1042. #if defined(HAVE_RXSPENCER)
  1043. #include <sys/types.h>
  1044. #include <rxspencer/regex.h>
  1045. #define USE_RXSPENCER
  1046. #endif
  1047. #if defined(USE_RXSPENCER)
  1048. int Swig_name_rxsmatch_value(String *mvalue, String *value) {
  1049. int match = 0;
  1050. char *cvalue = Char(value);
  1051. char *cmvalue = Char(mvalue);
  1052. regex_t compiled;
  1053. int retval = regcomp(&compiled, cmvalue, REG_EXTENDED | REG_NOSUB);
  1054. if (retval != 0)
  1055. return 0;
  1056. retval = regexec(&compiled, cvalue, 0, 0, 0);
  1057. match = (retval == REG_NOMATCH) ? 0 : 1;
  1058. #ifdef SWIG_DEBUG
  1059. Printf(stdout, "rxsmatch_value: %s %s %d\n", cvalue, cmvalue, match);
  1060. #endif
  1061. regfree(&compiled);
  1062. return match;
  1063. }
  1064. #else
  1065. int Swig_name_rxsmatch_value(String *mvalue, String *value) {
  1066. (void) mvalue;
  1067. (void) value;
  1068. return 0;
  1069. }
  1070. #endif
  1071. int Swig_name_match_value(String *mvalue, String *value) {
  1072. #if defined(SWIG_USE_SIMPLE_MATCHOR)
  1073. int match = 0;
  1074. char *cvalue = Char(value);
  1075. char *cmvalue = Char(mvalue);
  1076. char *sep = strchr(cmvalue, '|');
  1077. while (sep && !match) {
  1078. match = strncmp(cvalue, cmvalue, sep - cmvalue) == 0;
  1079. #ifdef SWIG_DEBUG
  1080. Printf(stdout, "match_value: %s %s %d\n", cvalue, cmvalue, match);
  1081. #endif
  1082. cmvalue = sep + 1;
  1083. sep = strchr(cmvalue, '|');
  1084. }
  1085. if (!match) {
  1086. match = strcmp(cvalue, cmvalue) == 0;
  1087. #ifdef SWIG_DEBUG
  1088. Printf(stdout, "match_value: %s %s %d\n", cvalue, cmvalue, match);
  1089. #endif
  1090. }
  1091. return match;
  1092. #else
  1093. return Equal(mvalue, value);
  1094. #endif
  1095. }
  1096. int Swig_name_match_nameobj(Hash *rn, Node *n) {
  1097. int match = 1;
  1098. List *matchlist = Getattr(rn, "matchlist");
  1099. #ifdef SWIG_DEBUG
  1100. Printf(stdout, "Swig_name_match_nameobj: %s\n", Getattr(n, "name"));
  1101. #endif
  1102. if (matchlist) {
  1103. int ilen = Len(matchlist);
  1104. int i;
  1105. for (i = 0; match && (i < ilen); ++i) {
  1106. Node *mi = Getitem(matchlist, i);
  1107. List *lattr = Getattr(mi, "attrlist");
  1108. String *nval = Swig_get_lattr(n, lattr);
  1109. int notmatch = GetFlag(mi, "notmatch");
  1110. int rxsmatch = GetFlag(mi, "rxsmatch");
  1111. #ifdef SWIG_DEBUG
  1112. Printf(stdout, "mi %d %s re %d not %d \n", i, nval, notmatch, rxsmatch);
  1113. if (rxsmatch) {
  1114. Printf(stdout, "rxsmatch %s\n", lattr);
  1115. }
  1116. #endif
  1117. match = 0;
  1118. if (nval) {
  1119. String *kwval = Getattr(mi, "value");
  1120. match = rxsmatch ? Swig_name_rxsmatch_value(kwval, nval)
  1121. : Swig_name_match_value(kwval, nval);
  1122. #ifdef SWIG_DEBUG
  1123. Printf(stdout, "val %s %s %d %d \n", nval, kwval, match, ilen);
  1124. #endif
  1125. }
  1126. if (notmatch)
  1127. match = !match;
  1128. }
  1129. }
  1130. #ifdef SWIG_DEBUG
  1131. Printf(stdout, "Swig_name_match_nameobj: %d\n", match);
  1132. #endif
  1133. return match;
  1134. }
  1135. /* -----------------------------------------------------------------------------
  1136. * Hash *Swig_name_nameobj_lget()
  1137. *
  1138. * Get a nameobj (rename/namewarn) from the list of filters
  1139. *
  1140. * ----------------------------------------------------------------------------- */
  1141. Hash *Swig_name_nameobj_lget(List *namelist, Node *n, String *prefix, String *name, String *decl) {
  1142. Hash *res = 0;
  1143. if (namelist) {
  1144. int len = Len(namelist);
  1145. int i;
  1146. int match = 0;
  1147. for (i = 0; !match && (i < len); i++) {
  1148. Hash *rn = Getitem(namelist, i);
  1149. String *rdecl = Getattr(rn, "decl");
  1150. if (rdecl && (!decl || !Equal(rdecl, decl))) {
  1151. continue;
  1152. } else if (Swig_name_match_nameobj(rn, n)) {
  1153. String *tname = Getattr(rn, "targetname");
  1154. if (tname) {
  1155. String *sfmt = Getattr(rn, "sourcefmt");
  1156. String *sname = 0;
  1157. int fullname = GetFlag(rn, "fullname");
  1158. int rxstarget = GetFlag(rn, "rxstarget");
  1159. if (sfmt) {
  1160. if (fullname && prefix) {
  1161. String *pname = NewStringf("%s::%s", prefix, name);
  1162. sname = NewStringf(sfmt, pname);
  1163. Delete(pname);
  1164. } else {
  1165. sname = NewStringf(sfmt, name);
  1166. }
  1167. } else {
  1168. if (fullname && prefix) {
  1169. sname = NewStringf("%s::%s", prefix, name);
  1170. } else {
  1171. sname = name;
  1172. DohIncref(name);
  1173. }
  1174. }
  1175. match = rxstarget ? Swig_name_rxsmatch_value(tname, sname) : Swig_name_match_value(tname, sname);
  1176. Delete(sname);
  1177. } else {
  1178. match = 1;
  1179. }
  1180. }
  1181. if (match) {
  1182. res = rn;
  1183. break;
  1184. }
  1185. }
  1186. }
  1187. return res;
  1188. }
  1189. /* -----------------------------------------------------------------------------
  1190. * Swig_name_namewarn_add
  1191. *
  1192. * Add a namewarn objects
  1193. *
  1194. * ----------------------------------------------------------------------------- */
  1195. void Swig_name_namewarn_add(String *prefix, String *name, SwigType *decl, Hash *namewrn) {
  1196. const char *namewrn_keys[] = { "rename", "error", "fullname", "sourcefmt", "targetfmt", 0 };
  1197. Swig_name_object_attach_keys(namewrn_keys, namewrn);
  1198. Swig_name_nameobj_add(Swig_name_namewarn_hash(), Swig_name_namewarn_list(), prefix, name, decl, namewrn);
  1199. }
  1200. /* -----------------------------------------------------------------------------
  1201. * Hash *Swig_name_namewarn_get()
  1202. *
  1203. * Return the namewarn object, if there is one.
  1204. *
  1205. * ----------------------------------------------------------------------------- */
  1206. Hash *Swig_name_namewarn_get(Node *n, String *prefix, String *name, SwigType *decl) {
  1207. if (!namewarn_hash && !namewarn_list)
  1208. return 0;
  1209. if (n) {
  1210. /* Return in the obvious cases */
  1211. if (!name || !Swig_need_name_warning(n)) {
  1212. return 0;
  1213. } else {
  1214. String *access = Getattr(n, "access");
  1215. int is_public = !access || Equal(access, "public");
  1216. if (!is_public && !Swig_need_protected(n)) {
  1217. return 0;
  1218. }
  1219. }
  1220. }
  1221. if (name) {
  1222. /* Check to see if the name is in the hash */
  1223. Hash *wrn = Swig_name_object_get(Swig_name_namewarn_hash(), prefix, name, decl);
  1224. if (wrn && !Swig_name_match_nameobj(wrn, n))
  1225. wrn = 0;
  1226. if (!wrn) {
  1227. wrn = Swig_name_nameobj_lget(Swig_name_namewarn_list(), n, prefix, name, decl);
  1228. }
  1229. if (wrn && Getattr(wrn, "error")) {
  1230. if (n) {
  1231. Swig_error(Getfile(n), Getline(n), "%s\n", Getattr(wrn, "name"));
  1232. } else {
  1233. Swig_error(cparse_file, cparse_line, "%s\n", Getattr(wrn, "name"));
  1234. }
  1235. }
  1236. return wrn;
  1237. } else {
  1238. return 0;
  1239. }
  1240. }
  1241. /* -----------------------------------------------------------------------------
  1242. * String *Swig_name_warning()
  1243. *
  1244. * Return the name warning, if there is one.
  1245. *
  1246. * ----------------------------------------------------------------------------- */
  1247. String *Swig_name_warning(Node *n, String *prefix, String *name, SwigType *decl) {
  1248. Hash *wrn = Swig_name_namewarn_get(n, prefix, name, decl);
  1249. return (name && wrn) ? Getattr(wrn, "name") : 0;
  1250. }
  1251. /* -----------------------------------------------------------------------------
  1252. * Swig_name_rename_add()
  1253. *
  1254. * Manage the rename objects
  1255. *
  1256. * ----------------------------------------------------------------------------- */
  1257. static void single_rename_add(String *prefix, String *name, SwigType *decl, Hash *newname) {
  1258. Swig_name_nameobj_add(Swig_name_rename_hash(), Swig_name_rename_list(), prefix, name, decl, newname);
  1259. }
  1260. /* Add a new rename. Works much like new_feature including default argument handling. */
  1261. void Swig_name_rename_add(String *prefix, String *name, SwigType *decl, Hash *newname, ParmList *declaratorparms) {
  1262. ParmList *declparms = declaratorparms;
  1263. const char *rename_keys[] = { "fullname", "sourcefmt", "targetfmt", "continue", "rxstarget", 0 };
  1264. Swig_name_object_attach_keys(rename_keys, newname);
  1265. /* Add the name */
  1266. single_rename_add(prefix, name, decl, newname);
  1267. /* Add extra names if there are default parameters in the parameter list */
  1268. if (decl) {
  1269. int constqualifier = SwigType_isconst(decl);
  1270. while (declparms) {
  1271. if (ParmList_has_defaultargs(declparms)) {
  1272. /* Create a parameter list for the new rename by copying all
  1273. but the last (defaulted) parameter */
  1274. ParmList *newparms = CopyParmListMax(declparms,ParmList_len(declparms)-1);
  1275. /* Create new declaration - with the last parameter removed */
  1276. SwigType *newdecl = Copy(decl);
  1277. Delete(SwigType_pop_function(newdecl)); /* remove the old parameter list from newdecl */
  1278. SwigType_add_function(newdecl, newparms);
  1279. if (constqualifier)
  1280. SwigType_add_qualifier(newdecl, "const");
  1281. single_rename_add(prefix, name, newdecl, newname);
  1282. declparms = newparms;
  1283. Delete(newdecl);
  1284. } else {
  1285. declparms = 0;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. /* Create a name applying rename/namewarn if needed */
  1291. static String *apply_rename(String *newname, int fullname, String *prefix, String *name) {
  1292. String *result = 0;
  1293. if (newname && Len(newname)) {
  1294. if (Strcmp(newname, "$ignore") == 0) {
  1295. result = Copy(newname);
  1296. } else {
  1297. char *cnewname = Char(newname);
  1298. if (cnewname) {
  1299. int destructor = name && (*(Char(name)) == '~');
  1300. String *fmt = newname;
  1301. /* use name as a fmt, but avoid C++ "%" and "%=" operators */
  1302. if (Len(newname) > 1 && strchr(cnewname, '%') && !(strcmp(cnewname, "%=") == 0)) {
  1303. if (fullname && prefix) {
  1304. result = NewStringf(fmt, prefix, name);
  1305. } else {
  1306. result = NewStringf(fmt, name);
  1307. }
  1308. } else {
  1309. result = Copy(newname);
  1310. }
  1311. if (destructor && result && (*(Char(result)) != '~')) {
  1312. Insert(result, 0, "~");
  1313. }
  1314. }
  1315. }
  1316. }
  1317. return result;
  1318. }
  1319. /* -----------------------------------------------------------------------------
  1320. * String *Swig_name_make()
  1321. *
  1322. * Make a name after applying all the rename/namewarn objects
  1323. *
  1324. * ----------------------------------------------------------------------------- */
  1325. String *Swig_name_make(Node *n, String *prefix, const_String_or_char_ptr cname, SwigType *decl, String *oldname) {
  1326. String *nname = 0;
  1327. String *result = 0;
  1328. String *name = NewString(cname);
  1329. Hash *wrn = 0;
  1330. String *rdecl = 0;
  1331. String *rname = 0;
  1332. /* very specific hack for template constructors/destructors */
  1333. #ifdef SWIG_DEBUG
  1334. Printf(stdout, "Swig_name_make: looking for %s %s %s %s\n", prefix, name, decl, oldname);
  1335. #endif
  1336. if (name && n && SwigType_istemplate(name)) {
  1337. String *nodetype = nodeType(n);
  1338. if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
  1339. String *nprefix = NewStringEmpty();
  1340. String *nlast = NewStringEmpty();
  1341. String *tprefix;
  1342. Swig_scopename_split(name, &nprefix, &nlast);
  1343. tprefix = SwigType_templateprefix(nlast);
  1344. Delete(nlast);
  1345. if (Len(nprefix)) {
  1346. Append(nprefix, "::");
  1347. Append(nprefix, tprefix);
  1348. Delete(tprefix);
  1349. rname = nprefix;
  1350. } else {
  1351. rname = tprefix;
  1352. Delete(nprefix);
  1353. }
  1354. rdecl = Copy(decl);
  1355. Replaceall(rdecl, name, rname);
  1356. #ifdef SWIG_DEBUG
  1357. Printf(stdout, "SWIG_name_make: use new name %s %s : %s %s\n", name, decl, rname, rdecl);
  1358. #endif
  1359. decl = rdecl;
  1360. Delete(name);
  1361. name = rname;
  1362. }
  1363. }
  1364. if (rename_hash || rename_list || namewarn_hash || namewarn_list) {
  1365. Hash *rn = Swig_name_object_get(Swig_name_rename_hash(), prefix, name, decl);
  1366. if (!rn || !Swig_name_match_nameobj(rn, n)) {
  1367. rn = Swig_name_nameobj_lget(Swig_name_rename_list(), n, prefix, name, decl);
  1368. if (rn) {
  1369. String *sfmt = Getattr(rn, "sourcefmt");
  1370. int fullname = GetFlag(rn, "fullname");
  1371. if (fullname && prefix) {
  1372. String *sname = NewStringf("%s::%s", prefix, name);
  1373. Delete(name);
  1374. name = sname;
  1375. prefix = 0;
  1376. }
  1377. if (sfmt) {
  1378. String *sname = NewStringf(sfmt, name);
  1379. Delete(name);
  1380. name = sname;
  1381. }
  1382. }
  1383. }
  1384. if (rn) {
  1385. String *newname = Getattr(rn, "name");
  1386. int fullname = GetFlag(rn, "fullname");
  1387. result = apply_rename(newname, fullname, prefix, name);
  1388. }
  1389. if (result && !Equal(result, name)) {
  1390. /* operators in C++ allow aliases, we look for them */
  1391. char *cresult = Char(result);
  1392. if (cresult && (strncmp(cresult, "operator ", 9) == 0)) {
  1393. String *nresult = Swig_name_make(n, prefix, result, decl, oldname);
  1394. if (!Equal(nresult, result)) {
  1395. Delete(result);
  1396. result = nresult;
  1397. } else {
  1398. Delete(nresult);
  1399. }
  1400. }
  1401. }
  1402. nname = result ? result : name;
  1403. wrn = Swig_name_namewarn_get(n, prefix, nname, decl);
  1404. if (wrn) {
  1405. String *rename = Getattr(wrn, "rename");
  1406. if (rename) {
  1407. String *msg = Getattr(wrn, "name");
  1408. int fullname = GetFlag(wrn, "fullname");
  1409. if (result)
  1410. Delete(result);
  1411. result = apply_rename(rename, fullname, prefix, name);
  1412. if ((msg) && (Len(msg))) {
  1413. if (!Getmeta(nname, "already_warned")) {
  1414. if (n) {
  1415. SWIG_WARN_NODE_BEGIN(n);
  1416. Swig_warning(0, Getfile(n), Getline(n), "%s\n", msg);
  1417. SWIG_WARN_NODE_END(n);
  1418. } else {
  1419. Swig_warning(0, Getfile(name), Getline(name), "%s\n", msg);
  1420. }
  1421. Setmeta(nname, "already_warned", "1");
  1422. }
  1423. }
  1424. }
  1425. }
  1426. }
  1427. if (!result || !Len(result)) {
  1428. if (result)
  1429. Delete(result);
  1430. if (oldname) {
  1431. result = NewString(oldname);
  1432. } else {
  1433. result = NewString(cname);
  1434. }
  1435. }
  1436. Delete(name);
  1437. #ifdef SWIG_DEBUG
  1438. Printf(stdout, "Swig_name_make: result '%s' '%s'\n", cname, result);
  1439. #endif
  1440. return result;
  1441. }
  1442. /* -----------------------------------------------------------------------------
  1443. * void Swig_name_inherit()
  1444. *
  1445. * Inherit namewarn,rename, and feature objects
  1446. *
  1447. * ----------------------------------------------------------------------------- */
  1448. void Swig_name_inherit(String *base, String *derived) {
  1449. /* Printf(stdout,"base = '%s', derived = '%s'\n", base, derived); */
  1450. Swig_name_object_inherit(Swig_name_rename_hash(), base, derived);
  1451. Swig_name_object_inherit(Swig_name_namewarn_hash(), base, derived);
  1452. Swig_name_object_inherit(Swig_cparse_features(), base, derived);
  1453. }
  1454. /* -----------------------------------------------------------------------------
  1455. * void Swig_name_decl()
  1456. *
  1457. * Return a stringified version of a C/C++ declaration without the return type.
  1458. * The node passed in is expected to be a function. Some example return values:
  1459. * "MyNameSpace::MyTemplate<MyNameSpace::ABC >::~MyTemplate()"
  1460. * "MyNameSpace::ABC::ABC(int,double)"
  1461. * "MyNameSpace::ABC::constmethod(int) const"
  1462. *
  1463. * ----------------------------------------------------------------------------- */
  1464. String *Swig_name_decl(Node *n) {
  1465. String *qname;
  1466. String *decl;
  1467. String *qualifier = Swig_symbol_qualified(n);
  1468. String *name = Swig_scopename_last(Getattr(n, "name"));
  1469. if (qualifier)
  1470. qualifier = SwigType_namestr(qualifier);
  1471. /* Very specific hack for template constructors/destructors */
  1472. if (SwigType_istemplate(name)) {
  1473. String *nodetype = nodeType(n);
  1474. if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
  1475. String *nprefix = NewStringEmpty();
  1476. String *nlast = NewStringEmpty();
  1477. String *tprefix;
  1478. Swig_scopename_split(name, &nprefix, &nlast);
  1479. tprefix = SwigType_templateprefix(nlast);
  1480. Delete(nlast);
  1481. Delete(name);
  1482. name = tprefix;
  1483. }
  1484. }
  1485. qname = NewString("");
  1486. if (qualifier && Len(qualifier) > 0)
  1487. Printf(qname, "%s::", qualifier);
  1488. Printf(qname, "%s", SwigType_str(name, 0));
  1489. decl = NewStringf("%s(%s)%s", qname, ParmList_errorstr(Getattr(n, "parms")), SwigType_isconst(Getattr(n, "decl")) ? " const" : "");
  1490. Delete(name);
  1491. Delete(qualifier);
  1492. Delete(qname);
  1493. return decl;
  1494. }
  1495. /* -----------------------------------------------------------------------------
  1496. * void Swig_name_fulldecl()
  1497. *
  1498. * Return a stringified version of a C/C++ declaration including the return type.
  1499. * The node passed in is expected to be a function. Some example return values:
  1500. * "MyNameSpace::MyTemplate<MyNameSpace::ABC >::~MyTemplate()"
  1501. * "MyNameSpace::ABC::ABC(int,double)"
  1502. * "int * MyNameSpace::ABC::constmethod(int) const"
  1503. *
  1504. * ----------------------------------------------------------------------------- */
  1505. String *Swig_name_fulldecl(Node *n) {
  1506. String *decl = Swig_name_decl(n);
  1507. String *type = Getattr(n, "type");
  1508. String *nodetype = nodeType(n);
  1509. String *fulldecl;
  1510. /* add on the return type */
  1511. if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
  1512. fulldecl = decl;
  1513. } else {
  1514. String *t = SwigType_str(type, 0);
  1515. fulldecl = NewStringf("%s %s", t, decl);
  1516. Delete(decl);
  1517. Delete(t);
  1518. }
  1519. return fulldecl;
  1520. }