nested.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 https://www.swig.org/legal.html.
  8. *
  9. * nested.cxx
  10. *
  11. * Nested structs support
  12. * ----------------------------------------------------------------------------- */
  13. #include "swigmod.h"
  14. #include "cparse.h"
  15. // Nested classes processing section
  16. static Hash *classhash = 0;
  17. static String *make_name(Node *n, String *name, SwigType *decl) {
  18. int destructor = name && (*(Char(name)) == '~');
  19. if (String *yyrename = Getattr(n, "class_rename")) {
  20. String *s = NewString(yyrename);
  21. Delattr(n, "class_rename");
  22. if (destructor && (*(Char(s)) != '~')) {
  23. Insert(s, 0, "~");
  24. }
  25. return s;
  26. }
  27. if (!name)
  28. return 0;
  29. return Swig_name_make(n, 0, name, decl, 0);
  30. }
  31. // C version of add_symbols()
  32. static void add_symbols_c(Node *n) {
  33. String *decl;
  34. String *wrn = 0;
  35. String *symname = 0;
  36. int iscdecl = Cmp(nodeType(n), "cdecl") == 0;
  37. Setattr(n, "ismember", "1");
  38. Setattr(n, "access", "public");
  39. if (Getattr(n, "sym:name"))
  40. return;
  41. decl = Getattr(n, "decl");
  42. if (!SwigType_isfunction(decl)) {
  43. String *name = Getattr(n, "name");
  44. String *makename = Getattr(n, "parser:makename");
  45. if (iscdecl) {
  46. String *storage = Getattr(n, "storage");
  47. if (Cmp(storage, "typedef") == 0) {
  48. Setattr(n, "kind", "typedef");
  49. } else {
  50. SwigType *type = Getattr(n, "type");
  51. String *value = Getattr(n, "value");
  52. Setattr(n, "kind", "variable");
  53. if (value && Len(value)) {
  54. Setattr(n, "hasvalue", "1");
  55. }
  56. if (type) {
  57. SwigType *ty;
  58. SwigType *tmp = 0;
  59. if (decl) {
  60. ty = tmp = Copy(type);
  61. SwigType_push(ty, decl);
  62. } else {
  63. ty = type;
  64. }
  65. if (!SwigType_ismutable(ty)) {
  66. SetFlag(n, "hasconsttype");
  67. SetFlag(n, "feature:immutable");
  68. }
  69. if (tmp)
  70. Delete(tmp);
  71. }
  72. if (!type) {
  73. Printf(stderr, "notype name %s\n", name);
  74. }
  75. }
  76. }
  77. Swig_features_get(Swig_cparse_features(), 0, name, 0, n);
  78. if (makename) {
  79. symname = make_name(n, makename, 0);
  80. Delattr(n, "parser:makename"); /* temporary information, don't leave it hanging around */
  81. } else {
  82. makename = name;
  83. symname = make_name(n, makename, 0);
  84. }
  85. if (!symname) {
  86. symname = Copy(Getattr(n, "unnamed"));
  87. }
  88. if (symname) {
  89. wrn = Swig_name_warning(n, 0, symname, 0);
  90. }
  91. } else {
  92. String *name = Getattr(n, "name");
  93. SwigType *fdecl = Copy(decl);
  94. SwigType *fun = SwigType_pop_function(fdecl);
  95. if (iscdecl) {
  96. Setattr(n, "kind", "function");
  97. }
  98. Swig_features_get(Swig_cparse_features(), 0, name, fun, n);
  99. symname = make_name(n, name, fun);
  100. wrn = Swig_name_warning(n, 0, symname, fun);
  101. Delete(fdecl);
  102. Delete(fun);
  103. }
  104. if (!symname)
  105. return;
  106. if (GetFlag(n, "feature:ignore")) {
  107. /* Only add to C symbol table and continue */
  108. Swig_symbol_add(0, n);
  109. } else if (strncmp(Char(symname), "$ignore", 7) == 0) {
  110. char *c = Char(symname) + 7;
  111. SetFlag(n, "feature:ignore");
  112. if (strlen(c)) {
  113. SWIG_WARN_NODE_BEGIN(n);
  114. Swig_warning(0, Getfile(n), Getline(n), "%s\n", c + 1);
  115. SWIG_WARN_NODE_END(n);
  116. }
  117. Swig_symbol_add(0, n);
  118. } else {
  119. Node *c;
  120. if ((wrn) && (Len(wrn))) {
  121. String *metaname = symname;
  122. if (!Getmeta(metaname, "already_warned")) {
  123. SWIG_WARN_NODE_BEGIN(n);
  124. Swig_warning(0, Getfile(n), Getline(n), "%s\n", wrn);
  125. SWIG_WARN_NODE_END(n);
  126. Setmeta(metaname, "already_warned", "1");
  127. }
  128. }
  129. c = Swig_symbol_add(symname, n);
  130. if (c != n) {
  131. /* symbol conflict attempting to add in the new symbol */
  132. if (Getattr(n, "sym:weak")) {
  133. Setattr(n, "sym:name", symname);
  134. } else {
  135. String *e = NewStringEmpty();
  136. String *en = NewStringEmpty();
  137. String *ec = NewStringEmpty();
  138. int redefined = Swig_need_redefined_warn(n, c, true);
  139. if (redefined) {
  140. Printf(en, "Identifier '%s' redefined (ignored)", symname);
  141. Printf(ec, "previous definition of '%s'", symname);
  142. } else {
  143. Printf(en, "Redundant redeclaration of '%s'", symname);
  144. Printf(ec, "previous declaration of '%s'", symname);
  145. }
  146. if (Cmp(symname, Getattr(n, "name"))) {
  147. Printf(en, " (Renamed from '%s')", SwigType_namestr(Getattr(n, "name")));
  148. }
  149. Printf(en, ",");
  150. if (Cmp(symname, Getattr(c, "name"))) {
  151. Printf(ec, " (Renamed from '%s')", SwigType_namestr(Getattr(c, "name")));
  152. }
  153. Printf(ec, ".");
  154. SWIG_WARN_NODE_BEGIN(n);
  155. if (redefined) {
  156. Swig_warning(WARN_PARSE_REDEFINED, Getfile(n), Getline(n), "%s\n", en);
  157. Swig_warning(WARN_PARSE_REDEFINED, Getfile(c), Getline(c), "%s\n", ec);
  158. } else {
  159. Swig_warning(WARN_PARSE_REDUNDANT, Getfile(n), Getline(n), "%s\n", en);
  160. Swig_warning(WARN_PARSE_REDUNDANT, Getfile(c), Getline(c), "%s\n", ec);
  161. }
  162. SWIG_WARN_NODE_END(n);
  163. Printf(e, "%s:%d:%s\n%s:%d:%s\n", Getfile(n), Getline(n), en, Getfile(c), Getline(c), ec);
  164. Setattr(n, "error", e);
  165. Delete(e);
  166. Delete(en);
  167. Delete(ec);
  168. }
  169. }
  170. }
  171. Delete(symname);
  172. }
  173. /* Strips C-style and C++-style comments from string in-place. */
  174. static void strip_comments(char *string) {
  175. int state = 0;
  176. /*
  177. * 0 - not in comment
  178. * 1 - in c-style comment
  179. * 2 - in c++-style comment
  180. * 3 - in string
  181. * 4 - after reading / not in comments
  182. * 5 - after reading * in c-style comments
  183. * 6 - after reading \ in strings
  184. */
  185. char *c = string;
  186. while (*c) {
  187. switch (state) {
  188. case 0:
  189. if (*c == '\"')
  190. state = 3;
  191. else if (*c == '/')
  192. state = 4;
  193. break;
  194. case 1:
  195. if (*c == '*')
  196. state = 5;
  197. *c = ' ';
  198. break;
  199. case 2:
  200. if (*c == '\n')
  201. state = 0;
  202. else
  203. *c = ' ';
  204. break;
  205. case 3:
  206. if (*c == '\"')
  207. state = 0;
  208. else if (*c == '\\')
  209. state = 6;
  210. break;
  211. case 4:
  212. if (*c == '/') {
  213. *(c - 1) = ' ';
  214. *c = ' ';
  215. state = 2;
  216. } else if (*c == '*') {
  217. *(c - 1) = ' ';
  218. *c = ' ';
  219. state = 1;
  220. } else
  221. state = 0;
  222. break;
  223. case 5:
  224. if (*c == '/')
  225. state = 0;
  226. else
  227. state = 1;
  228. *c = ' ';
  229. break;
  230. case 6:
  231. state = 3;
  232. break;
  233. }
  234. ++c;
  235. }
  236. }
  237. // Create a %insert with a typedef to make a new name visible to C
  238. static Node *create_insert(Node *n, bool noTypedef = false) {
  239. // format a typedef
  240. String *ccode = Getattr(n, "code");
  241. Push(ccode, " ");
  242. if (noTypedef) {
  243. Push(ccode, Getattr(n, "name"));
  244. Push(ccode, " ");
  245. Push(ccode, Getattr(n, "kind"));
  246. } else {
  247. Push(ccode, Getattr(n, "kind"));
  248. Push(ccode, "typedef ");
  249. Append(ccode, " ");
  250. Append(ccode, Getattr(n, "tdname"));
  251. }
  252. Append(ccode, ";");
  253. /* Strip comments - further code may break in presence of comments. */
  254. strip_comments(Char(ccode));
  255. /* Make all SWIG created typedef structs/unions/classes unnamed else
  256. redefinition errors occur - nasty hack alert. */
  257. if (!noTypedef) {
  258. const char *types_array[3] = { "struct", "union", "class" };
  259. for (int i = 0; i < 3; i++) {
  260. char *code_ptr = Char(ccode);
  261. while (code_ptr) {
  262. /* Replace struct name (as in 'struct name {...}' ) with whitespace
  263. name will be between struct and opening brace */
  264. code_ptr = strstr(code_ptr, types_array[i]);
  265. if (code_ptr) {
  266. char *open_bracket_pos;
  267. code_ptr += strlen(types_array[i]);
  268. open_bracket_pos = strchr(code_ptr, '{');
  269. if (open_bracket_pos) {
  270. /* Make sure we don't have something like struct A a; */
  271. char *semi_colon_pos = strchr(code_ptr, ';');
  272. if (!(semi_colon_pos && (semi_colon_pos < open_bracket_pos)))
  273. while (code_ptr < open_bracket_pos)
  274. *code_ptr++ = ' ';
  275. }
  276. }
  277. }
  278. }
  279. }
  280. {
  281. /* Remove SWIG directive %constant which may be left in the SWIG created typedefs */
  282. char *code_ptr = Char(ccode);
  283. while (code_ptr) {
  284. code_ptr = strstr(code_ptr, "%constant");
  285. if (code_ptr) {
  286. char *directive_end_pos = strchr(code_ptr, ';');
  287. if (directive_end_pos) {
  288. while (code_ptr <= directive_end_pos)
  289. *code_ptr++ = ' ';
  290. }
  291. }
  292. }
  293. }
  294. Node *newnode = NewHash();
  295. set_nodeType(newnode, "insert");
  296. Setfile(newnode, Getfile(n));
  297. Setline(newnode, Getline(n));
  298. String *code = NewStringEmpty();
  299. Wrapper_pretty_print(ccode, code);
  300. Setattr(newnode, "code", code);
  301. Delete(code);
  302. Delattr(n, "code");
  303. return newnode;
  304. }
  305. static void insertNodeAfter(Node *n, Node *c) {
  306. Node *g = parentNode(n);
  307. set_parentNode(c, g);
  308. Node *ns = nextSibling(n);
  309. if (Node *outer = Getattr(c, "nested:outer")) {
  310. while (ns && outer == Getattr(ns, "nested:outer")) {
  311. n = ns;
  312. ns = nextSibling(n);
  313. }
  314. }
  315. if (!ns) {
  316. set_lastChild(g, c);
  317. } else {
  318. set_nextSibling(c, ns);
  319. set_previousSibling(ns, c);
  320. }
  321. set_nextSibling(n, c);
  322. set_previousSibling(c, n);
  323. }
  324. void Swig_nested_name_unnamed_c_structs(Node *n) {
  325. if (!n)
  326. return;
  327. if (!classhash)
  328. classhash = Getattr(n, "classes");
  329. Node *c = firstChild(n);
  330. while (c) {
  331. Node *next = nextSibling(c);
  332. if (String *declName = Getattr(c, "nested:unnamed")) {
  333. if (Node *outer = Getattr(c, "nested:outer")) {
  334. // generate a name
  335. String *name = NewStringf("%s_%s", Getattr(outer, "name"), declName);
  336. Delattr(c, "nested:unnamed");
  337. // set the name to the class and symbol table
  338. Setattr(c, "tdname", name);
  339. Setattr(c, "name", name);
  340. Swig_symbol_setscope(Getattr(c, "symtab"));
  341. Swig_symbol_setscopename(name);
  342. // now that we have a name - gather base symbols
  343. if (List *publicBases = Getattr(c, "baselist")) {
  344. List *bases = Swig_make_inherit_list(name, publicBases, 0);
  345. Swig_inherit_base_symbols(bases);
  346. Delete(bases);
  347. }
  348. Setattr(classhash, name, c);
  349. // Merge the extension into the symbol table
  350. if (Node *am = Getattr(Swig_extend_hash(), name)) {
  351. Swig_extend_merge(c, am);
  352. Swig_extend_append_previous(c, am);
  353. Delattr(Swig_extend_hash(), name);
  354. }
  355. Swig_symbol_popscope();
  356. // process declarations following this type (assign correct new type)
  357. SwigType *ty = Copy(name);
  358. Node *decl = nextSibling(c);
  359. List *declList = NewList();
  360. while (decl && Getattr(decl, "nested:unnamedtype") == c) {
  361. Setattr(decl, "type", ty);
  362. Append(declList, decl);
  363. Delattr(decl, "nested:unnamedtype");
  364. SetFlag(decl, "feature:immutable");
  365. add_symbols_c(decl);
  366. decl = nextSibling(decl);
  367. }
  368. Delete(ty);
  369. Swig_symbol_setscope(Swig_symbol_global_scope());
  370. add_symbols_c(c);
  371. Node *ins = create_insert(c);
  372. insertNodeAfter(c, ins);
  373. removeNode(c);
  374. insertNodeAfter(n, c);
  375. Delete(ins);
  376. Delattr(c, "nested:outer");
  377. } else {
  378. // global unnamed struct - ignore it and its instances
  379. SetFlag(c, "feature:ignore");
  380. while (next && Getattr(next, "nested:unnamedtype") == c) {
  381. SetFlag(next, "feature:ignore");
  382. next = nextSibling(next);
  383. }
  384. c = next;
  385. continue;
  386. }
  387. } else if (cparse_cplusplusout) {
  388. if (Getattr(c, "nested:outer")) {
  389. Node *ins = create_insert(c, true);
  390. insertNodeAfter(c, ins);
  391. Delete(ins);
  392. Delattr(c, "nested:outer");
  393. }
  394. }
  395. // process children
  396. Swig_nested_name_unnamed_c_structs(c);
  397. c = next;
  398. }
  399. }
  400. static void remove_outer_class_reference(Node *n) {
  401. for (Node *c = firstChild(n); c; c = nextSibling(c)) {
  402. if (GetFlag(c, "feature:flatnested") || Language::instance()->nestedClassesSupport() == Language::NCS_None) {
  403. Delattr(c, "nested:outer");
  404. remove_outer_class_reference(c);
  405. }
  406. }
  407. }
  408. void Swig_nested_process_classes(Node *n) {
  409. if (!n)
  410. return;
  411. Node *c = firstChild(n);
  412. while (c) {
  413. Node *next = nextSibling(c);
  414. if (!Getattr(c, "templatetype")) {
  415. if (GetFlag(c, "nested") && (GetFlag(c, "feature:flatnested") || Language::instance()->nestedClassesSupport() == Language::NCS_None)) {
  416. removeNode(c);
  417. if (!checkAttribute(c, "access", "public"))
  418. SetFlag(c, "feature:ignore");
  419. else if (Strcmp(nodeType(n),"extend") == 0 && Strcmp(nodeType(parentNode(n)),"class") == 0)
  420. insertNodeAfter(parentNode(n), c);
  421. else
  422. insertNodeAfter(n, c);
  423. }
  424. Swig_nested_process_classes(c);
  425. }
  426. c = next;
  427. }
  428. remove_outer_class_reference(n);
  429. }