PageRenderTime 66ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/Doc/Manual/Extending.html

https://github.com/sunaku/swig-ruby-ffi
HTML | 4034 lines | 3273 code | 755 blank | 6 comment | 0 complexity | 2ef4bba18c35dc72606eabd75981c999 MD5 | raw file
Possible License(s): 0BSD, GPL-2.0, LGPL-2.1
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Extending SWIG to support new languages</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. </head>
  7. <body bgcolor="#ffffff">
  8. <H1><a name="Extending"></a>35 Extending SWIG to support new languages</H1>
  9. <!-- INDEX -->
  10. <div class="sectiontoc">
  11. <ul>
  12. <li><a href="#Extending_nn2">Introduction</a>
  13. <li><a href="#Extending_nn3">Prerequisites</a>
  14. <li><a href="#Extending_nn4">The Big Picture</a>
  15. <li><a href="#Extending_nn5">Execution Model</a>
  16. <ul>
  17. <li><a href="#Extending_nn6">Preprocessing</a>
  18. <li><a href="#Extending_nn7">Parsing</a>
  19. <li><a href="#Extending_nn8">Parse Trees</a>
  20. <li><a href="#Extending_nn9">Attribute namespaces</a>
  21. <li><a href="#Extending_nn10">Symbol Tables</a>
  22. <li><a href="#Extending_nn11">The %feature directive</a>
  23. <li><a href="#Extending_nn12">Code Generation</a>
  24. <li><a href="#Extending_nn13">SWIG and XML</a>
  25. </ul>
  26. <li><a href="#Extending_nn14">Primitive Data Structures</a>
  27. <ul>
  28. <li><a href="#Extending_nn15">Strings</a>
  29. <li><a href="#Extending_nn16">Hashes</a>
  30. <li><a href="#Extending_nn17">Lists</a>
  31. <li><a href="#Extending_nn18">Common operations</a>
  32. <li><a href="#Extending_nn19">Iterating over Lists and Hashes</a>
  33. <li><a href="#Extending_nn20">I/O</a>
  34. </ul>
  35. <li><a href="#Extending_nn21">Navigating and manipulating parse trees</a>
  36. <li><a href="#Extending_nn22">Working with attributes</a>
  37. <li><a href="#Extending_nn23">Type system</a>
  38. <ul>
  39. <li><a href="#Extending_nn24">String encoding of types</a>
  40. <li><a href="#Extending_nn25">Type construction</a>
  41. <li><a href="#Extending_nn26">Type tests</a>
  42. <li><a href="#Extending_nn27">Typedef and inheritance</a>
  43. <li><a href="#Extending_nn28">Lvalues</a>
  44. <li><a href="#Extending_nn29">Output functions</a>
  45. </ul>
  46. <li><a href="#Extending_nn30">Parameters</a>
  47. <li><a href="#Extending_nn31">Writing a Language Module</a>
  48. <ul>
  49. <li><a href="#Extending_nn32">Execution model</a>
  50. <li><a href="#Extending_starting_out">Starting out</a>
  51. <li><a href="#Extending_nn34">Command line options</a>
  52. <li><a href="#Extending_nn35">Configuration and preprocessing</a>
  53. <li><a href="#Extending_nn36">Entry point to code generation</a>
  54. <li><a href="#Extending_nn37">Module I/O and wrapper skeleton</a>
  55. <li><a href="#Extending_nn38">Low-level code generators</a>
  56. <li><a href="#Extending_nn39">Configuration files</a>
  57. <li><a href="#Extending_nn40">Runtime support</a>
  58. <li><a href="#Extending_nn41">Standard library files</a>
  59. <li><a href="#Extending_nn42">User examples</a>
  60. <li><a href="#Extending_test_suite">Test driven development and the test-suite</a>
  61. <ul>
  62. <li><a href="#Extending_running_test_suite">Running the test-suite</a>
  63. </ul>
  64. <li><a href="#Extending_nn43">Documentation</a>
  65. <li><a href="#Extending_prerequisites">Prerequisites for adding a new language module to the SWIG distribution</a>
  66. <li><a href="#Extending_coding_style_guidelines">Coding style guidelines</a>
  67. </ul>
  68. <li><a href="#Extending_debugging_options">Debugging Options</a>
  69. <li><a href="#Extending_nn46">Guide to parse tree nodes</a>
  70. <li><a href="#Extending_further_info">Further Development Information</a>
  71. </ul>
  72. </div>
  73. <!-- INDEX -->
  74. <H2><a name="Extending_nn2"></a>35.1 Introduction</H2>
  75. <p>
  76. This chapter describes SWIG's internal organization and the process by which
  77. new target languages can be developed. First, a brief word of warning---SWIG
  78. is continually evolving.
  79. The information in this chapter is mostly up to
  80. date, but changes are ongoing. Expect a few inconsistencies.
  81. </p>
  82. <p>
  83. Also, this chapter is not meant to be a hand-holding tutorial. As a starting point,
  84. you should probably look at one of SWIG's existing modules.
  85. </p>
  86. <H2><a name="Extending_nn3"></a>35.2 Prerequisites</H2>
  87. <p>
  88. In order to extend SWIG, it is useful to have the following background:
  89. </p>
  90. <ul>
  91. <li>An understanding of the C API for the target language.
  92. <li>A good grasp of the C++ type system.
  93. <li>An understanding of typemaps and some of SWIG's advanced features.
  94. <li>Some familiarity with writing C++ (language modules are currently written in C++).
  95. </ul>
  96. <p>
  97. Since SWIG is essentially a specialized C++ compiler, it may be useful
  98. to have some prior experience with compiler design (perhaps even a
  99. compilers course) to better understand certain parts of the system. A
  100. number of books will also be useful. For example, "The C Programming
  101. Language" by Kernighan and Ritchie (a.k.a, "K&amp;R") and the C++ standard,
  102. "ISO/IEC 14882 Programming Languages - C++" will be of great use.
  103. </p>
  104. <p>
  105. Also, it is useful to keep in mind that SWIG primarily operates as an
  106. extension of the C++ <em>type</em> system. At first glance, this might not be
  107. obvious, but almost all SWIG directives as well as the low-level generation of
  108. wrapper code are driven by C++ datatypes.
  109. </p>
  110. <H2><a name="Extending_nn4"></a>35.3 The Big Picture</H2>
  111. <p>
  112. SWIG is a special purpose compiler that parses C++ declarations to
  113. generate wrapper code. To make this conversion possible, SWIG makes
  114. three fundamental extensions to the C++ language:
  115. </p>
  116. <ul>
  117. <li><b>Typemaps</b>. Typemaps are used to define the
  118. conversion/marshalling behavior of specific C++ datatypes. All type conversion in SWIG is
  119. based on typemaps. Furthermore, the association of typemaps to datatypes utilizes an advanced pattern matching
  120. mechanism that is fully integrated with the C++ type system.
  121. </li>
  122. <li><b>Declaration Annotation</b>. To customize wrapper code
  123. generation, most declarations can be annotated with special features.
  124. For example, you can make a variable read-only, you can ignore a
  125. declaration, you can rename a member function, you can add exception
  126. handling, and so forth. Virtually all of these customizations are built on top of a low-level
  127. declaration annotator that can attach arbitrary attributes to any declaration.
  128. Code generation modules can look for these attributes to guide the wrapping process.
  129. </li>
  130. <li><b>Class extension</b>. SWIG allows classes and structures to be extended with new
  131. methods and attributes (the <tt>%extend</tt> directive). This has the effect of altering
  132. the API in the target language and can be used to generate OO interfaces to C libraries.
  133. </ul>
  134. <p>
  135. It is important to emphasize that virtually all SWIG features reduce to one of these three
  136. fundamental concepts. The type system and pattern matching rules also play a critical
  137. role in making the system work. For example, both typemaps and declaration annotation are
  138. based on pattern matching and interact heavily with the underlying type system.
  139. </p>
  140. <H2><a name="Extending_nn5"></a>35.4 Execution Model</H2>
  141. <p>
  142. When you run SWIG on an interface, processing is handled in stages by a series of system components:
  143. </p>
  144. <ul>
  145. <li>An integrated C preprocessor reads a collection of configuration
  146. files and the specified interface file into memory. The preprocessor
  147. performs the usual functions including macro expansion and file
  148. inclusion. However, the preprocessor also performs some transformations of the
  149. interface. For instance, <tt>#define</tt> statements are sometimes transformed into
  150. <tt>%constant</tt> declarations. In addition, information related to file/line number
  151. tracking is inserted.
  152. </li>
  153. <li>A C/C++ parser reads the preprocessed input and generates a full
  154. parse tree of all of the SWIG directives and C declarations found.
  155. The parser is responsible for many aspects of the system including
  156. renaming, declaration annotation, and template expansion. However, the parser
  157. does not produce any output nor does it interact with the target
  158. language module as it runs. SWIG is not a one-pass compiler.
  159. </li>
  160. <li>A type-checking pass is made. This adjusts all of the C++ typenames to properly
  161. handle namespaces, typedefs, nested classes, and other issues related to type scoping.
  162. </li>
  163. <li>A semantic pass is made on the parse tree to collect information
  164. related to properties of the C++ interface. For example, this pass
  165. would determine whether or not a class allows a default constructor.
  166. </li>
  167. <li>A code generation pass is made using a specific target language
  168. module. This phase is responsible for generating the actual wrapper
  169. code. All of SWIG's user-defined modules are invoked during this
  170. latter stage of compilation.
  171. </li>
  172. </ul>
  173. <p>
  174. The next few sections briefly describe some of these stages.
  175. </p>
  176. <H3><a name="Extending_nn6"></a>35.4.1 Preprocessing</H3>
  177. <p>
  178. The preprocessor plays a critical role in the SWIG implementation. This is because a lot
  179. of SWIG's processing and internal configuration is managed not by code written in C, but
  180. by configuration files in the SWIG library. In fact, when you
  181. run SWIG, parsing starts with a small interface file like this (note: this explains
  182. the cryptic error messages that new users sometimes get when SWIG is misconfigured or installed
  183. incorrectly):
  184. </p>
  185. <div class="code">
  186. <pre>
  187. %include "swig.swg" // Global SWIG configuration
  188. %include "<em>langconfig.swg</em>" // Language specific configuration
  189. %include "yourinterface.i" // Your interface file
  190. </pre>
  191. </div>
  192. <p>
  193. The <tt>swig.swg</tt> file contains global configuration information. In addition, this file
  194. defines many of SWIG's standard directives as macros. For instance, part of
  195. of <tt>swig.swg</tt> looks like this:
  196. </p>
  197. <div class="code">
  198. <pre>
  199. ...
  200. /* Code insertion directives such as %wrapper %{ ... %} */
  201. #define %begin %insert("begin")
  202. #define %runtime %insert("runtime")
  203. #define %header %insert("header")
  204. #define %wrapper %insert("wrapper")
  205. #define %init %insert("init")
  206. /* Access control directives */
  207. #define %immutable %feature("immutable","1")
  208. #define %mutable %feature("immutable")
  209. /* Directives for callback functions */
  210. #define %callback(x) %feature("callback") `x`;
  211. #define %nocallback %feature("callback");
  212. /* %ignore directive */
  213. #define %ignore %rename($ignore)
  214. #define %ignorewarn(x) %rename("$ignore:" x)
  215. ...
  216. </pre>
  217. </div>
  218. <p>
  219. The fact that most of the standard SWIG directives are macros is
  220. intended to simplify the implementation of the internals. For instance,
  221. rather than having to support dozens of special directives, it is
  222. easier to have a few basic primitives such as <tt>%feature</tt> or
  223. <tt>%insert</tt>.
  224. </p>
  225. <p>
  226. The <em><tt>langconfig.swg</tt></em> file is supplied by the target
  227. language. This file contains language-specific configuration
  228. information. More often than not, this file provides run-time wrapper
  229. support code (e.g., the type-checker) as well as a collection of
  230. typemaps that define the default wrapping behavior. Note: the name of this
  231. file depends on the target language and is usually something like <tt>python.swg</tt>
  232. or <tt>perl5.swg</tt>.
  233. </p>
  234. <p>
  235. As a debugging aide, the text that SWIG feeds to its C++ parser can be
  236. obtained by running <tt>swig -E interface.i</tt>. This output
  237. probably isn't too useful in general, but it will show how macros have
  238. been expanded as well as everything else that goes into the low-level
  239. construction of the wrapper code.
  240. </p>
  241. <H3><a name="Extending_nn7"></a>35.4.2 Parsing</H3>
  242. <p>
  243. The current C++ parser handles a subset of C++. Most incompatibilities with C are due to
  244. subtle aspects of how SWIG parses declarations. Specifically, SWIG expects all C/C++ declarations to follow this general form:
  245. </p>
  246. <div class="diagram">
  247. <pre>
  248. <em>storage</em> <em>type</em> <em>declarator</em> <em>initializer</em>;
  249. </pre>
  250. </div>
  251. <p>
  252. <tt><em>storage</em></tt> is a keyword such as <tt>extern</tt>,
  253. <tt>static</tt>, <tt>typedef</tt>, or <tt>virtual</tt>. <tt><em>type</em></tt> is a primitive
  254. datatype such as <tt>int</tt> or <tt>void</tt>. <tt><em>type</em></tt> may be optionally
  255. qualified with a qualifier such as <tt>const</tt> or <tt>volatile</tt>. <tt><em>declarator</em></tt>
  256. is a name with additional type-construction modifiers attached to it (pointers, arrays, references,
  257. functions, etc.). Examples of declarators include <tt>*x</tt>, <tt>**x</tt>, <tt>x[20]</tt>, and
  258. <tt>(*x)(int,double)</tt>. The <tt><em>initializer</em></tt> may be a value assigned using <tt>=</tt> or
  259. body of code enclosed in braces <tt>{ ... }</tt>.
  260. </p>
  261. <p>
  262. This declaration format covers most common C++ declarations. However, the C++ standard
  263. is somewhat more flexible in the placement of the parts. For example, it is technically legal, although
  264. uncommon to write something like <tt>int typedef const a</tt> in your program. SWIG simply
  265. doesn't bother to deal with this case.
  266. </p>
  267. <p>
  268. The other significant difference between C++ and SWIG is in the
  269. treatment of typenames. In C++, if you have a declaration like this,
  270. </p>
  271. <div class="code">
  272. <pre>
  273. int blah(Foo *x, Bar *y);
  274. </pre>
  275. </div>
  276. <p>
  277. it won't parse correctly unless <tt>Foo</tt> and <tt>Bar</tt> have
  278. been previously defined as types either using a <tt>class</tt>
  279. definition or a <tt>typedef</tt>. The reasons for this are subtle,
  280. but this treatment of typenames is normally integrated at the level of the C
  281. tokenizer---when a typename appears, a different token is returned to the parser
  282. instead of an identifier.
  283. </p>
  284. <p>
  285. SWIG does not operate in this manner--any legal identifier can be used
  286. as a type name. The reason for this is primarily motivated by the use
  287. of SWIG with partially defined data. Specifically,
  288. SWIG is supposed to be easy to use on interfaces with missing type information.
  289. </p>
  290. <p>
  291. Because of the different treatment of typenames, the most serious
  292. limitation of the SWIG parser is that it can't process type declarations where
  293. an extra (and unnecessary) grouping operator is used. For example:
  294. </p>
  295. <div class="code">
  296. <pre>
  297. int (x); /* A variable x */
  298. int (y)(int); /* A function y */
  299. </pre>
  300. </div>
  301. <p>
  302. The placing of extra parentheses in type declarations like this is
  303. already recognized by the C++ community as a potential source of
  304. strange programming errors. For example, Scott Meyers "Effective STL"
  305. discusses this problem in a section on avoiding C++'s "most vexing
  306. parse."
  307. </p>
  308. <p>
  309. The parser is also unable to handle declarations with no return type or bare argument names.
  310. For example, in an old C program, you might see things like this:
  311. </p>
  312. <div class="code">
  313. <pre>
  314. foo(a,b) {
  315. ...
  316. }
  317. </pre>
  318. </div>
  319. <p>
  320. In this case, the return type as well as the types of the arguments
  321. are taken by the C compiler to be an <tt>int</tt>. However, SWIG
  322. interprets the above code as an abstract declarator for a function
  323. returning a <tt>foo</tt> and taking types <tt>a</tt> and <tt>b</tt> as
  324. arguments).
  325. </p>
  326. <H3><a name="Extending_nn8"></a>35.4.3 Parse Trees</H3>
  327. <p>
  328. The SWIG parser produces a complete parse tree of the input file before any wrapper code
  329. is actually generated. Each item in the tree is known as a "Node". Each node is identified
  330. by a symbolic tag. Furthermore, a node may have an arbitrary number of children.
  331. The parse tree structure and tag names of an interface can be displayed using <tt>swig -debug-tags</tt>.
  332. For example:
  333. </p>
  334. <div class="shell">
  335. <pre>
  336. $ <b>swig -c++ -python -debug-tags example.i</b>
  337. . top (example.i:1)
  338. . top . include (example.i:1)
  339. . top . include . typemap (/r0/beazley/Projects/lib/swig1.3/swig.swg:71)
  340. . top . include . typemap . typemapitem (/r0/beazley/Projects/lib/swig1.3/swig.swg:71)
  341. . top . include . typemap (/r0/beazley/Projects/lib/swig1.3/swig.swg:83)
  342. . top . include . typemap . typemapitem (/r0/beazley/Projects/lib/swig1.3/swig.swg:83)
  343. . top . include (example.i:4)
  344. . top . include . insert (/r0/beazley/Projects/lib/swig1.3/python/python.swg:7)
  345. . top . include . insert (/r0/beazley/Projects/lib/swig1.3/python/python.swg:8)
  346. . top . include . typemap (/r0/beazley/Projects/lib/swig1.3/python/python.swg:19)
  347. ...
  348. . top . include (example.i:6)
  349. . top . include . module (example.i:2)
  350. . top . include . insert (example.i:6)
  351. . top . include . include (example.i:9)
  352. . top . include . include . class (example.h:3)
  353. . top . include . include . class . access (example.h:4)
  354. . top . include . include . class . constructor (example.h:7)
  355. . top . include . include . class . destructor (example.h:10)
  356. . top . include . include . class . cdecl (example.h:11)
  357. . top . include . include . class . cdecl (example.h:11)
  358. . top . include . include . class . cdecl (example.h:12)
  359. . top . include . include . class . cdecl (example.h:13)
  360. . top . include . include . class . cdecl (example.h:14)
  361. . top . include . include . class . cdecl (example.h:15)
  362. . top . include . include . class (example.h:18)
  363. . top . include . include . class . access (example.h:19)
  364. . top . include . include . class . cdecl (example.h:20)
  365. . top . include . include . class . access (example.h:21)
  366. . top . include . include . class . constructor (example.h:22)
  367. . top . include . include . class . cdecl (example.h:23)
  368. . top . include . include . class . cdecl (example.h:24)
  369. . top . include . include . class (example.h:27)
  370. . top . include . include . class . access (example.h:28)
  371. . top . include . include . class . cdecl (example.h:29)
  372. . top . include . include . class . access (example.h:30)
  373. . top . include . include . class . constructor (example.h:31)
  374. . top . include . include . class . cdecl (example.h:32)
  375. . top . include . include . class . cdecl (example.h:33)
  376. </pre>
  377. </div>
  378. <p>
  379. Even for the most simple interface, the parse tree structure is larger than you might expect. For example, in the
  380. above output, a substantial number of nodes are actually generated by the <tt>python.swg</tt> configuration file
  381. which defines typemaps and other directives. The contents of the user-supplied input file don't appear until the end
  382. of the output.
  383. </p>
  384. <p>
  385. The contents of each parse tree node consist of a collection of attribute/value
  386. pairs. Internally, the nodes are simply represented by hash tables. A display of
  387. the entire parse-tree structure can be obtained using <tt>swig -debug-top &lt;n&gt;</tt>, where <tt>n</tt> is
  388. the stage being processed.
  389. There are a number of other parse tree display options, for example, <tt>swig -debug-module &lt;n&gt;</tt> will
  390. avoid displaying system parse information and only display the parse tree pertaining to the user's module at
  391. stage <tt>n</tt> of processing.
  392. </p>
  393. <div class="shell">
  394. <pre>
  395. $ swig -c++ -python -debug-module 4 example.i
  396. +++ include ----------------------------------------
  397. | name - "example.i"
  398. +++ module ----------------------------------------
  399. | name - "example"
  400. |
  401. +++ insert ----------------------------------------
  402. | code - "\n#include \"example.h\"\n"
  403. |
  404. +++ include ----------------------------------------
  405. | name - "example.h"
  406. +++ class ----------------------------------------
  407. | abstract - "1"
  408. | sym:name - "Shape"
  409. | name - "Shape"
  410. | kind - "class"
  411. | symtab - 0x40194140
  412. | sym:symtab - 0x40191078
  413. +++ access ----------------------------------------
  414. | kind - "public"
  415. |
  416. +++ constructor ----------------------------------------
  417. | sym:name - "Shape"
  418. | name - "Shape"
  419. | decl - "f()."
  420. | code - "{\n nshapes++;\n }"
  421. | sym:symtab - 0x40194140
  422. |
  423. +++ destructor ----------------------------------------
  424. | sym:name - "~Shape"
  425. | name - "~Shape"
  426. | storage - "virtual"
  427. | code - "{\n nshapes--;\n }"
  428. | sym:symtab - 0x40194140
  429. |
  430. +++ cdecl ----------------------------------------
  431. | sym:name - "x"
  432. | name - "x"
  433. | decl - ""
  434. | type - "double"
  435. | sym:symtab - 0x40194140
  436. |
  437. +++ cdecl ----------------------------------------
  438. | sym:name - "y"
  439. | name - "y"
  440. | decl - ""
  441. | type - "double"
  442. | sym:symtab - 0x40194140
  443. |
  444. +++ cdecl ----------------------------------------
  445. | sym:name - "move"
  446. | name - "move"
  447. | decl - "f(double,double)."
  448. | parms - double ,double
  449. | type - "void"
  450. | sym:symtab - 0x40194140
  451. |
  452. +++ cdecl ----------------------------------------
  453. | sym:name - "area"
  454. | name - "area"
  455. | decl - "f(void)."
  456. | parms - void
  457. | storage - "virtual"
  458. | value - "0"
  459. | type - "double"
  460. | sym:symtab - 0x40194140
  461. |
  462. +++ cdecl ----------------------------------------
  463. | sym:name - "perimeter"
  464. | name - "perimeter"
  465. | decl - "f(void)."
  466. | parms - void
  467. | storage - "virtual"
  468. | value - "0"
  469. | type - "double"
  470. | sym:symtab - 0x40194140
  471. |
  472. +++ cdecl ----------------------------------------
  473. | sym:name - "nshapes"
  474. | name - "nshapes"
  475. | decl - ""
  476. | storage - "static"
  477. | type - "int"
  478. | sym:symtab - 0x40194140
  479. |
  480. +++ class ----------------------------------------
  481. | sym:name - "Circle"
  482. | name - "Circle"
  483. | kind - "class"
  484. | bases - 0x40194510
  485. | symtab - 0x40194538
  486. | sym:symtab - 0x40191078
  487. +++ access ----------------------------------------
  488. | kind - "private"
  489. |
  490. +++ cdecl ----------------------------------------
  491. | name - "radius"
  492. | decl - ""
  493. | type - "double"
  494. |
  495. +++ access ----------------------------------------
  496. | kind - "public"
  497. |
  498. +++ constructor ----------------------------------------
  499. | sym:name - "Circle"
  500. | name - "Circle"
  501. | parms - double
  502. | decl - "f(double)."
  503. | code - "{ }"
  504. | sym:symtab - 0x40194538
  505. |
  506. +++ cdecl ----------------------------------------
  507. | sym:name - "area"
  508. | name - "area"
  509. | decl - "f(void)."
  510. | parms - void
  511. | storage - "virtual"
  512. | type - "double"
  513. | sym:symtab - 0x40194538
  514. |
  515. +++ cdecl ----------------------------------------
  516. | sym:name - "perimeter"
  517. | name - "perimeter"
  518. | decl - "f(void)."
  519. | parms - void
  520. | storage - "virtual"
  521. | type - "double"
  522. | sym:symtab - 0x40194538
  523. |
  524. +++ class ----------------------------------------
  525. | sym:name - "Square"
  526. | name - "Square"
  527. | kind - "class"
  528. | bases - 0x40194760
  529. | symtab - 0x40194788
  530. | sym:symtab - 0x40191078
  531. +++ access ----------------------------------------
  532. | kind - "private"
  533. |
  534. +++ cdecl ----------------------------------------
  535. | name - "width"
  536. | decl - ""
  537. | type - "double"
  538. |
  539. +++ access ----------------------------------------
  540. | kind - "public"
  541. |
  542. +++ constructor ----------------------------------------
  543. | sym:name - "Square"
  544. | name - "Square"
  545. | parms - double
  546. | decl - "f(double)."
  547. | code - "{ }"
  548. | sym:symtab - 0x40194788
  549. |
  550. +++ cdecl ----------------------------------------
  551. | sym:name - "area"
  552. | name - "area"
  553. | decl - "f(void)."
  554. | parms - void
  555. | storage - "virtual"
  556. | type - "double"
  557. | sym:symtab - 0x40194788
  558. |
  559. +++ cdecl ----------------------------------------
  560. | sym:name - "perimeter"
  561. | name - "perimeter"
  562. | decl - "f(void)."
  563. | parms - void
  564. | storage - "virtual"
  565. | type - "double"
  566. | sym:symtab - 0x40194788
  567. </pre>
  568. </div>
  569. <H3><a name="Extending_nn9"></a>35.4.4 Attribute namespaces</H3>
  570. <p>
  571. Attributes of parse tree nodes are often prepended with a namespace qualifier.
  572. For example, the attributes
  573. <tt>sym:name</tt> and <tt>sym:symtab</tt> are attributes related to
  574. symbol table management and are prefixed with <tt>sym:</tt>. As a
  575. general rule, only those attributes which are directly related to the raw declaration
  576. appear without a prefix (type, name, declarator, etc.).
  577. </p>
  578. <p>
  579. Target language modules may add additional attributes to nodes to assist the generation
  580. of wrapper code. The convention for doing this is to place these attributes in a namespace
  581. that matches the name of the target language. For example, <tt>python:foo</tt> or
  582. <tt>perl:foo</tt>.
  583. </p>
  584. <H3><a name="Extending_nn10"></a>35.4.5 Symbol Tables</H3>
  585. <p>
  586. During parsing, all symbols are managed in the space of the target
  587. language. The <tt>sym:name</tt> attribute of each node contains the symbol name
  588. selected by the parser. Normally, <tt>sym:name</tt> and <tt>name</tt>
  589. are the same. However, the <tt>%rename</tt> directive can be used to
  590. change the value of <tt>sym:name</tt>. You can see the effect of
  591. <tt>%rename</tt> by trying it on a simple interface and dumping the
  592. parse tree. For example:
  593. </p>
  594. <div class="code">
  595. <pre>
  596. %rename(foo_i) foo(int);
  597. %rename(foo_d) foo(double);
  598. void foo(int);
  599. void foo(double);
  600. void foo(Bar *b);
  601. </pre>
  602. </div>
  603. <p>
  604. There are various <tt>debug-</tt> options that can be useful for debugging and analysing the parse tree.
  605. For example, the <tt>debug-top &lt;n&gt;</tt> or <tt>debug-module &lt;n&gt;</tt> options will
  606. dump the entire/top of the parse tree or the module subtree at one of the four <tt>n</tt> stages of processing.
  607. The parse tree can be viewed after the final stage of processing by running SWIG:
  608. </p>
  609. <div class="shell">
  610. <pre>
  611. $ swig -debug-top 4 example.i
  612. ...
  613. +++ cdecl ----------------------------------------
  614. | sym:name - "foo_i"
  615. | name - "foo"
  616. | decl - "f(int)."
  617. | parms - int
  618. | type - "void"
  619. | sym:symtab - 0x40165078
  620. |
  621. +++ cdecl ----------------------------------------
  622. | sym:name - "foo_d"
  623. | name - "foo"
  624. | decl - "f(double)."
  625. | parms - double
  626. | type - "void"
  627. | sym:symtab - 0x40165078
  628. |
  629. +++ cdecl ----------------------------------------
  630. | sym:name - "foo"
  631. | name - "foo"
  632. | decl - "f(p.Bar)."
  633. | parms - Bar *
  634. | type - "void"
  635. | sym:symtab - 0x40165078
  636. </pre>
  637. </div>
  638. <p>
  639. All symbol-related conflicts and complaints about overloading are based on <tt>sym:name</tt> values.
  640. For instance, the following example uses <tt>%rename</tt> in reverse to generate a name clash.
  641. </p>
  642. <div class="code">
  643. <pre>
  644. %rename(foo) foo_i(int);
  645. %rename(foo) foo_d(double;
  646. void foo_i(int);
  647. void foo_d(double);
  648. void foo(Bar *b);
  649. </pre>
  650. </div>
  651. <p>
  652. When you run SWIG on this you now get:
  653. </p>
  654. <div class="shell">
  655. <pre>
  656. $ ./swig example.i
  657. example.i:6. Overloaded declaration ignored. foo_d(double )
  658. example.i:5. Previous declaration is foo_i(int )
  659. example.i:7. Overloaded declaration ignored. foo(Bar *)
  660. example.i:5. Previous declaration is foo_i(int )
  661. </pre>
  662. </div>
  663. <H3><a name="Extending_nn11"></a>35.4.6 The %feature directive</H3>
  664. <p>
  665. A number of SWIG directives such as <tt>%exception</tt> are implemented using the
  666. low-level <tt>%feature</tt> directive. For example:
  667. </p>
  668. <div class="code">
  669. <pre>
  670. %feature("except") getitem(int) {
  671. try {
  672. $action
  673. } catch (badindex) {
  674. ...
  675. }
  676. }
  677. ...
  678. class Foo {
  679. public:
  680. Object *getitem(int index) throws(badindex);
  681. ...
  682. };
  683. </pre>
  684. </div>
  685. <p>
  686. The behavior of <tt>%feature</tt> is very easy to describe--it simply
  687. attaches a new attribute to any parse tree node that matches the
  688. given prototype. When a feature is added, it shows up as an attribute in the <tt>feature:</tt> namespace.
  689. You can see this when running with the <tt>-debug-top 4</tt> option. For example:
  690. </p>
  691. <div class="shell">
  692. <pre>
  693. +++ cdecl ----------------------------------------
  694. | sym:name - "getitem"
  695. | name - "getitem"
  696. | decl - "f(int).p."
  697. | parms - int
  698. | type - "Object"
  699. | feature:except - "{\n try {\n $action\n } catc..."
  700. | sym:symtab - 0x40168ac8
  701. |
  702. </pre>
  703. </div>
  704. <p>
  705. Feature names are completely arbitrary and a target language module can be
  706. programmed to respond to any feature name that it wants to recognize. The
  707. data stored in a feature attribute is usually just a raw unparsed string.
  708. For example, the exception code above is simply
  709. stored without any modifications.
  710. </p>
  711. <H3><a name="Extending_nn12"></a>35.4.7 Code Generation</H3>
  712. <p>
  713. Language modules work by defining handler functions that know how to respond to
  714. different types of parse-tree nodes. These handlers simply look at the
  715. attributes of each node in order to produce low-level code.
  716. </p>
  717. <p>
  718. In reality, the generation of code is somewhat more subtle than simply
  719. invoking handler functions. This is because parse-tree nodes might be
  720. transformed. For example, suppose you are wrapping a class like this:
  721. </p>
  722. <div class="code">
  723. <pre>
  724. class Foo {
  725. public:
  726. virtual int *bar(int x);
  727. };
  728. </pre>
  729. </div>
  730. <p>
  731. When the parser constructs a node for the member <tt>bar</tt>, it creates a raw "cdecl" node with the following
  732. attributes:
  733. </p>
  734. <div class="diagram">
  735. <pre>
  736. nodeType : cdecl
  737. name : bar
  738. type : int
  739. decl : f(int).p
  740. parms : int x
  741. storage : virtual
  742. sym:name : bar
  743. </pre>
  744. </div>
  745. <p>
  746. To produce wrapper code, this "cdecl" node undergoes a number of transformations. First, the node is recognized as a function declaration. This adjusts some of the type information--specifically, the declarator is joined with the base datatype to produce this:
  747. </p>
  748. <div class="diagram">
  749. <pre>
  750. nodeType : cdecl
  751. name : bar
  752. type : p.int &lt;-- Notice change in return type
  753. decl : f(int).p
  754. parms : int x
  755. storage : virtual
  756. sym:name : bar
  757. </pre>
  758. </div>
  759. <p>
  760. Next, the context of the node indicates that the node is really a
  761. member function. This produces a transformation to a low-level
  762. accessor function like this:
  763. </p>
  764. <div class="diagram">
  765. <pre>
  766. nodeType : cdecl
  767. name : bar
  768. type : int.p
  769. decl : f(int).p
  770. parms : Foo *self, int x &lt;-- Added parameter
  771. storage : virtual
  772. wrap:action : result = (arg1)-&gt;bar(arg2) &lt;-- Action code added
  773. sym:name : Foo_bar &lt;-- Symbol name changed
  774. </pre>
  775. </div>
  776. <p>
  777. In this transformation, notice how an additional parameter was added
  778. to the parameter list and how the symbol name of the node has suddenly
  779. changed into an accessor using the naming scheme described in the
  780. "SWIG Basics" chapter. A small fragment of "action" code has also
  781. been generated--notice how the <tt>wrap:action</tt> attribute defines
  782. the access to the underlying method. The data in this transformed
  783. node is then used to generate a wrapper.
  784. </p>
  785. <p>
  786. Language modules work by registering handler functions for dealing with
  787. various types of nodes at different stages of transformation. This is done by
  788. inheriting from a special <tt>Language</tt> class and defining a collection
  789. of virtual methods. For example, the Python module defines a class as
  790. follows:
  791. </p>
  792. <div class="code">
  793. <pre>
  794. class PYTHON : public Language {
  795. protected:
  796. public :
  797. virtual void main(int, char *argv[]);
  798. virtual int top(Node *);
  799. virtual int functionWrapper(Node *);
  800. virtual int constantWrapper(Node *);
  801. virtual int variableWrapper(Node *);
  802. virtual int nativeWrapper(Node *);
  803. virtual int membervariableHandler(Node *);
  804. virtual int memberconstantHandler(Node *);
  805. virtual int memberfunctionHandler(Node *);
  806. virtual int constructorHandler(Node *);
  807. virtual int destructorHandler(Node *);
  808. virtual int classHandler(Node *);
  809. virtual int classforwardDeclaration(Node *);
  810. virtual int insertDirective(Node *);
  811. virtual int importDirective(Node *);
  812. };
  813. </pre>
  814. </div>
  815. <p>
  816. The role of these functions is described shortly.
  817. </p>
  818. <H3><a name="Extending_nn13"></a>35.4.8 SWIG and XML</H3>
  819. <p>
  820. Much of SWIG's current parser design was originally motivated by
  821. interest in using XML to represent SWIG parse trees. Although XML is
  822. not currently used in any direct manner, the parse tree structure, use
  823. of node tags, attributes, and attribute namespaces are all influenced
  824. by aspects of XML parsing. Therefore, in trying to understand SWIG's
  825. internal data structures, it may be useful to keep XML in the back of
  826. your mind as a model.
  827. </p>
  828. <H2><a name="Extending_nn14"></a>35.5 Primitive Data Structures</H2>
  829. <p>
  830. Most of SWIG is constructed using three basic data structures:
  831. strings, hashes, and lists. These data structures are dynamic in same way as
  832. similar structures found in many scripting languages. For instance,
  833. you can have containers (lists and hash tables) of mixed types and
  834. certain operations are polymorphic.
  835. </p>
  836. <p>
  837. This section briefly describes the basic structures so that later
  838. sections of this chapter make more sense.
  839. </p>
  840. <p>
  841. When describing the low-level API, the following type name conventions are
  842. used:
  843. </p>
  844. <ul>
  845. <li><tt>String</tt>. A string object.
  846. <li><tt>Hash</tt>. A hash object.
  847. <li><tt>List</tt>. A list object.
  848. <li><tt>String_or_char</tt>. A string object or a <tt>char *</tt>.
  849. <li><tt>Object_or_char</tt>. An object or a <tt>char *</tt>.
  850. <li><tt>Object</tt>. Any object (string, hash, list, etc.)
  851. </ul>
  852. <p>
  853. In most cases, other typenames in the source are aliases for one of these
  854. primitive types. Specifically:
  855. </p>
  856. <div class="code">
  857. <pre>
  858. typedef String SwigType;
  859. typedef Hash Parm;
  860. typedef Hash ParmList;
  861. typedef Hash Node;
  862. typedef Hash Symtab;
  863. typedef Hash Typetab;
  864. </pre>
  865. </div>
  866. <H3><a name="Extending_nn15"></a>35.5.1 Strings</H3>
  867. <p>
  868. <b><tt>String *NewString(const String_or_char *val)</tt></b>
  869. </p>
  870. <div class="indent">
  871. Creates a new string with initial value <tt>val</tt>. <tt>val</tt> may
  872. be a <tt>char *</tt> or another <tt>String</tt> object. If you want
  873. to create an empty string, use "" for val.
  874. </div>
  875. <p>
  876. <b><tt>String *NewStringf(const char *fmt, ...)</tt></b>
  877. </p>
  878. <div class="indent">
  879. Creates a new string whose initial value is set according to a C <tt>printf</tt> style
  880. format string in <tt>fmt</tt>. Additional arguments follow depending
  881. on <tt>fmt</tt>.
  882. </div>
  883. <p>
  884. <b><tt>String *Copy(String *s)</tt></b>
  885. </p>
  886. <div class="indent">
  887. Make a copy of the string <tt>s</tt>.
  888. </div>
  889. <p>
  890. <b><tt>void Delete(String *s)</tt></b>
  891. </p>
  892. <div class="indent">
  893. Deletes <tt>s</tt>.
  894. </div>
  895. <p>
  896. <b><tt>int Len(const String_or_char *s)</tt></b>
  897. </p>
  898. <div class="indent">
  899. Returns the length of the string.
  900. </div>
  901. <p>
  902. <b><tt>char *Char(const String_or_char *s)</tt></b>
  903. </p>
  904. <div class="indent">
  905. Returns a pointer to the first character in a string.
  906. </div>
  907. <p>
  908. <b><tt>void Append(String *s, const String_or_char *t)</tt></b>
  909. </p>
  910. <div class="indent">
  911. Appends <tt>t</tt> to the end of string <tt>s</tt>.
  912. </div>
  913. <p>
  914. <b><tt>void Insert(String *s, int pos, const String_or_char *t)</tt></b>
  915. </p>
  916. <div class="indent">
  917. Inserts <tt>t</tt> into <tt>s</tt> at position <tt>pos</tt>. The contents
  918. of <tt>s</tt> are shifted accordingly. The special value <tt>DOH_END</tt>
  919. can be used for <tt>pos</tt> to indicate insertion at the end of the string (appending).
  920. </div>
  921. <p>
  922. <b><tt>int Strcmp(const String_or_char *s, const String_or_char *t)</tt></b>
  923. </p>
  924. <div class="indent">
  925. Compare strings <tt>s</tt> and <tt>t</tt>. Same as the C <tt>strcmp()</tt>
  926. function.
  927. </div>
  928. <p>
  929. <b><tt>int Strncmp(const String_or_char *s, const String_or_char *t, int len)</tt></b>
  930. </p>
  931. <div class="indent">
  932. Compare the first <tt>len</tt> characters of strings <tt>s</tt> and <tt>t</tt>. Same as the C <tt>strncmp()</tt>
  933. function.
  934. </div>
  935. <p>
  936. <b><tt>char *Strstr(const String_or_char *s, const String_or_char *pat)</tt></b>
  937. </p>
  938. <div class="indent">
  939. Returns a pointer to the first occurrence of <tt>pat</tt> in <tt>s</tt>.
  940. Same as the C <tt>strstr()</tt> function.
  941. </div>
  942. <p>
  943. <b><tt>char *Strchr(const String_or_char *s, char ch)</tt></b>
  944. </p>
  945. <div class="indent">
  946. Returns a pointer to the first occurrence of character <tt>ch</tt> in <tt>s</tt>.
  947. Same as the C <tt>strchr()</tt> function.
  948. </div>
  949. <p>
  950. <b><tt>void Chop(String *s)</tt></b>
  951. </p>
  952. <div class="indent">
  953. Chops trailing whitespace off the end of <tt>s</tt>.
  954. </div>
  955. <p>
  956. <b><tt>int Replace(String *s, const String_or_char *pat, const String_or_char *rep, int flags)</tt></b>
  957. </p>
  958. <div class="indent">
  959. <p>
  960. Replaces the pattern <tt>pat</tt> with <tt>rep</tt> in string <tt>s</tt>.
  961. <tt>flags</tt> is a combination of the following flags:</p>
  962. <div class="code">
  963. <pre>
  964. DOH_REPLACE_ANY - Replace all occurrences
  965. DOH_REPLACE_ID - Valid C identifiers only
  966. DOH_REPLACE_NOQUOTE - Don't replace in quoted strings
  967. DOH_REPLACE_FIRST - Replace first occurrence only.
  968. </pre>
  969. </div>
  970. <p>
  971. Returns the number of replacements made (if any).
  972. </p>
  973. </div>
  974. <H3><a name="Extending_nn16"></a>35.5.2 Hashes</H3>
  975. <p>
  976. <b><tt>Hash *NewHash()</tt></b>
  977. </p>
  978. <div class="indent">
  979. Creates a new empty hash table.
  980. </div>
  981. <p>
  982. <b><tt>Hash *Copy(Hash *h)</tt></b>
  983. </p>
  984. <div class="indent">
  985. Make a shallow copy of the hash <tt>h</tt>.
  986. </div>
  987. <p>
  988. <b><tt>void Delete(Hash *h)</tt></b>
  989. </p>
  990. <div class="indent">
  991. Deletes <tt>h</tt>.
  992. </div>
  993. <p>
  994. <b><tt>int Len(Hash *h)</tt></b>
  995. </p>
  996. <div class="indent">
  997. Returns the number of items in <tt>h</tt>.
  998. </div>
  999. <p>
  1000. <b><tt>Object *Getattr(Hash *h, const String_or_char *key)</tt></b>
  1001. </p>
  1002. <div class="indent">
  1003. Gets an object from <tt>h</tt>. <tt>key</tt> may be a string or
  1004. a simple <tt>char *</tt> string. Returns NULL if not found.
  1005. </div>
  1006. <p>
  1007. <b><tt>int Setattr(Hash *h, const String_or_char *key, const Object_or_char *val)</tt></b>
  1008. </p>
  1009. <div class="indent">
  1010. Stores <tt>val</tt> in <tt>h</tt>. <tt>key</tt> may be a string or
  1011. a simple <tt>char *</tt>. If <tt>val</tt> is not a standard
  1012. object (String, Hash, or List) it is assumed to be a <tt>char *</tt> in which
  1013. case it is used to construct a <tt>String</tt> that is stored in the hash.
  1014. If <tt>val</tt> is NULL, the object is deleted. Increases the reference count
  1015. of <tt>val</tt>. Returns 1 if this operation replaced an existing hash entry,
  1016. 0 otherwise.
  1017. </div>
  1018. <p>
  1019. <b><tt>int Delattr(Hash *h, const String_or_char *key)</tt></b>
  1020. </p>
  1021. <div class="indent">
  1022. Deletes the hash item referenced by <tt>key</tt>. Decreases the
  1023. reference count on the corresponding object (if any). Returns 1
  1024. if an object was removed, 0 otherwise.
  1025. </div>
  1026. <p>
  1027. <b><tt>List *Keys(Hash *h)</tt></b>
  1028. </p>
  1029. <div class="indent">
  1030. Returns the list of hash table keys.
  1031. </div>
  1032. <H3><a name="Extending_nn17"></a>35.5.3 Lists</H3>
  1033. <p>
  1034. <b><tt>List *NewList()</tt></b>
  1035. </p>
  1036. <div class="indent">
  1037. Creates a new empty list.
  1038. </div>
  1039. <p>
  1040. <b><tt>List *Copy(List *x)</tt></b>
  1041. </p>
  1042. <div class="indent">
  1043. Make a shallow copy of the List <tt>x</tt>.
  1044. </div>
  1045. <p>
  1046. <b><tt>void Delete(List *x)</tt></b>
  1047. </p>
  1048. <div class="indent">
  1049. Deletes <tt>x</tt>.
  1050. </div>
  1051. <p>
  1052. <b><tt>int Len(List *x)</tt></b>
  1053. </p>
  1054. <div class="indent">
  1055. Returns the number of items in <tt>x</tt>.
  1056. </div>
  1057. <p>
  1058. <b><tt>Object *Getitem(List *x, int n)</tt></b>
  1059. </p>
  1060. <div class="indent">
  1061. Returns an object from <tt>x</tt> with index <tt>n</tt>. If <tt>n</tt> is
  1062. beyond the end of the list, the last item is returned. If <tt>n</tt> is
  1063. negative, the first item is returned.
  1064. </div>
  1065. <p>
  1066. <b><tt>int *Setitem(List *x, int n, const Object_or_char *val)</tt></b>
  1067. </p>
  1068. <div class="indent">
  1069. Stores <tt>val</tt> in <tt>x</tt>.
  1070. If <tt>val</tt> is not a standard
  1071. object (String, Hash, or List) it is assumed to be a <tt>char *</tt> in which
  1072. case it is used to construct a <tt>String</tt> that is stored in the list.
  1073. <tt>n</tt> must be in range. Otherwise, an assertion will be raised.
  1074. </div>
  1075. <p>
  1076. <b><tt>int *Delitem(List *x, int n)</tt></b>
  1077. </p>
  1078. <div class="indent">
  1079. Deletes item <tt>n</tt> from the list, shifting items down if necessary.
  1080. To delete the last item in the list, use the special value <tt>DOH_END</tt>
  1081. for <tt>n</tt>.
  1082. </div>
  1083. <p>
  1084. <b><tt>void Append(List *x, const Object_or_char *t)</tt></b>
  1085. </p>
  1086. <div class="indent">
  1087. Appends <tt>t</tt> to the end of <tt>x</tt>. If <tt>t</tt> is not
  1088. a standard object, it is assumed to be a <tt>char *</tt> and is
  1089. used to create a String object.
  1090. </div>
  1091. <p>
  1092. <b><tt>void Insert(String *s, int pos, const Object_or_char *t)</tt></b>
  1093. </p>
  1094. <div class="indent">
  1095. Inserts <tt>t</tt> into <tt>s</tt> at position <tt>pos</tt>. The contents
  1096. of <tt>s</tt> are shifted accordingly. The special value <tt>DOH_END</tt>
  1097. can be used for <tt>pos</tt> to indicate insertion at the end of the list (appending).
  1098. If <tt>t</tt> is not a standard object, it is assumed to be a <tt>char *</tt>
  1099. and is used to create a String object.
  1100. </div>
  1101. <H3><a name="Extending_nn18"></a>35.5.4 Common operations</H3>
  1102. The following operations are applicable to all datatypes.
  1103. <p>
  1104. <b><tt>Object *Copy(Object *x)</tt></b>
  1105. </p>
  1106. <div class="indent">
  1107. Make a copy of the object <tt>x</tt>.
  1108. </div>
  1109. <p>
  1110. <b><tt>void Delete(Object *x)</tt></b>
  1111. </p>
  1112. <div class="indent">
  1113. Deletes <tt>x</tt>.
  1114. </div>
  1115. <p>
  1116. <b><tt>void Setfile(Object *x, String_or_char *f)</tt></b>
  1117. </p>
  1118. <div class="indent">
  1119. Sets the filename associated with <tt>x</tt>. Used to track
  1120. objects and report errors.
  1121. </div>
  1122. <p>
  1123. <b><tt>String *Getfile(Object *x)</tt></b>
  1124. </p>
  1125. <div class="indent">
  1126. Gets the filename associated with <tt>x</tt>.
  1127. </div>
  1128. <p>
  1129. <b><tt>void Setline(Object *x, int n)</tt></b>
  1130. </p>
  1131. <div class="indent">
  1132. Sets the line number associated with <tt>x</tt>. Used to track
  1133. objects and report errors.
  1134. </div>
  1135. <p>
  1136. <b><tt>int Getline(Object *x)</tt></b>
  1137. </p>
  1138. <div class="indent">
  1139. Gets the line number associated with <tt>x</tt>.
  1140. </div>
  1141. <H3><a name="Extending_nn19"></a>35.5.5 Iterating over Lists and Hashes</H3>
  1142. To iterate over the elements of a list or a hash table, the following functions are used:
  1143. <p>
  1144. <b><tt>Iterator First(Object *x)</tt></b>
  1145. </p>
  1146. <div class="indent">
  1147. Returns an iterator object that points to the first item in a list or hash table. The
  1148. <tt>item</tt> attribute of the Iterator object is a pointer to the item. For hash tables, the <tt>key</tt> attribute
  1149. of the Iterator object additionally points to the corresponding Hash table key. The <tt>item</tt> and <tt>key</tt> attributes
  1150. are NULL if the object contains no items or if there are no more items.
  1151. </div>
  1152. <p>
  1153. <b><tt>Iterator Next(Iterator i)</tt></b>
  1154. </p>
  1155. <div class="indent">
  1156. <p>Returns an iterator that points to the next item in a list or hash table.
  1157. Here are two examples of iteration:</p>
  1158. <div class="code">
  1159. <pre>
  1160. List *l = (some list);
  1161. Iterator i;
  1162. for (i = First(l); i.item; i = Next(i)) {
  1163. Printf(stdout,"%s\n", i.item);
  1164. }
  1165. Hash *h = (some hash);
  1166. Iterator j;
  1167. for (j = First(j); j.item; j= Next(j)) {
  1168. Printf(stdout,"%s : %s\n", j.key, j.item);
  1169. }
  1170. </pre>
  1171. </div>
  1172. </div>
  1173. <H3><a name="Extending_nn20"></a>35.5.6 I/O</H3>
  1174. Special I/O functions are used for all internal I/O. These operations
  1175. work on C <tt>FILE *</tt> objects, String objects, and special <tt>File</tt> objects
  1176. (which are merely a wrapper around <tt>FILE *</tt>).
  1177. <p>
  1178. <b><tt>int Printf(String_or_FILE *f, const char *fmt, ...)</tt></b>
  1179. </p>
  1180. <div class="indent">
  1181. Formatted I/O. Same as the C <tt>fprintf()</tt> function except that output
  1182. can also be directed to a string object. Note: the <tt>%s</tt> format
  1183. specifier works with both strings and <tt>char *</tt>. All other format
  1184. operators have the same meaning.
  1185. </div>
  1186. <p>
  1187. <b><tt>int Printv(String_or_FILE *f, String_or_char *arg1,..., NULL)</tt></b>
  1188. </p>
  1189. <div class="indent">
  1190. Prints a variable number of strings arguments to the output. The last
  1191. argument to this function must be NULL. The other arguments can either
  1192. be <tt>char *</tt> or string objects.
  1193. </div>
  1194. <p>
  1195. <b><tt>int Putc(int ch, String_or_FILE *f)</tt></b>
  1196. </p>
  1197. <div class="indent">
  1198. Same as the C <tt>fputc()</tt> function.
  1199. </div>
  1200. <p>
  1201. <b><tt>int Write(String_or_FILE *f, void *buf, int len)</tt></b>
  1202. </p>
  1203. <div class="indent">
  1204. Same as the C <tt>write()</tt> function.
  1205. </div>
  1206. <p>
  1207. <b><tt>int Read(String_or_FILE *f, void *buf, int maxlen)</tt></b>
  1208. </p>
  1209. <div class="indent">
  1210. Same as the C <tt>read()</tt> function.
  1211. </div>
  1212. <p>
  1213. <b><tt>int Getc(String_or_FILE *f)</tt></b>
  1214. </p>
  1215. <div class="indent">
  1216. Same as the C <tt>fgetc()</tt> function.
  1217. </div>
  1218. <p>
  1219. <b><tt>int Ungetc(int ch, String_or_FILE *f)</tt></b>
  1220. </p>
  1221. <div class="indent">
  1222. Same as the C <tt>ungetc()</tt> function.
  1223. </div>
  1224. <p>
  1225. <b><tt>int Seek(String_or_FILE *f, int offset, int whence)</tt></b>
  1226. </p>
  1227. <div class="indent">
  1228. Same as the C <tt>seek()</tt> function. <tt>offset</tt> is the number
  1229. of bytes. <tt>whence</tt> is one of <tt>SEEK_SET</tt>,<tt>SEEK_CUR</tt>,
  1230. or <tt>SEEK_END</tt>..
  1231. </div>
  1232. <p>
  1233. <b><tt>long Tell(String_or_FILE *f)</tt></b>
  1234. </p>
  1235. <div class="indent">
  1236. Same as the C <tt>tell()</tt> function.
  1237. </div>
  1238. <p>
  1239. <b><tt>File *NewFile(const char *filename, const char *mode, List *newfiles)</tt></b>
  1240. </p>
  1241. <div class="indent">
  1242. Create a File object using the <tt>fopen()</tt> library call. This
  1243. file differs from <tt>FILE *</tt> in that it can be placed in the standard
  1244. SWIG containers (lists, hashes, etc.). The <tt>filename</tt> is added to the
  1245. <tt>newfiles</tt> list if <tt>newfiles</tt> is non-zero and the file was created successfully.
  1246. </div>
  1247. <p>
  1248. <b><tt>File *NewFileFromFile(FILE *f)</tt></b>
  1249. </p>
  1250. <div class="indent">
  1251. Create a File object wrapper around an existing <tt>FILE *</tt> object.
  1252. </div>
  1253. <p>
  1254. <b><tt>int Close(String_or_FILE *f)</tt></b>
  1255. </p>
  1256. <div class="indent">
  1257. <p>Closes a file. Has no effect on strings.</p>
  1258. <p>
  1259. The use of the above I/O functions and strings play a critical role in SWIG. It is
  1260. common to see small code fragments of code generated using code like this:
  1261. </p>
  1262. <div class="code">
  1263. <pre>
  1264. /* Print into a string */
  1265. String *s = NewString("");
  1266. Printf(s,"Hello\n");
  1267. for (i = 0; i &lt; 10; i++) {
  1268. Printf(s,"%d\n", i);
  1269. }
  1270. ...
  1271. /* Print string into a file */
  1272. Printf(f, "%s\n", s);
  1273. </pre>
  1274. </div>
  1275. <p>
  1276. Similarly, the preprocessor and parser all operate on string-files.
  1277. </p>
  1278. </div>
  1279. <H2><a name="Extending_nn21"></a>35.6 Navigating and manipulating parse trees</H2>
  1280. Parse trees are built as collections of hash tables. Each node is a hash table in which
  1281. arbitrary attributes can be stored. Certain attributes in the hash table provide links to
  1282. other parse tree nodes. The following macros can be used to move around the parse tree.
  1283. <p>
  1284. <b><tt>String *nodeType(Node *n)</tt></b>
  1285. </p>
  1286. <div class="indent">
  1287. Returns the node type tag as a string. The returned string indicates the type of parse
  1288. tree node.
  1289. </div>
  1290. <p>
  1291. <b><tt>Node *nextSibling(Node *n)</tt></b>
  1292. </p>
  1293. <div class="indent">
  1294. Returns the next node in the parse tree. For example, the next C declaration.
  1295. </div>
  1296. <p>
  1297. <b><tt>Node *previousSibling(Node *n)</tt></b>
  1298. </p>
  1299. <div class="indent">
  1300. Returns the previous node in the parse tree. For example, the previous C declaration.
  1301. </div>
  1302. <p>
  1303. <b><tt>Node *firstChild(Node *n)</tt></b>
  1304. </p>
  1305. <div class="indent">
  1306. Returns the first child node. For example, if <tt>n</tt> was a C++ class node, this would
  1307. return the node for the first class member.
  1308. </div>
  1309. <p>
  1310. <b><tt>Node *lastChild(Node *n)</tt></b>
  1311. </p>
  1312. <div class="indent">
  1313. Returns the last child node. You might use this if you wanted to append a new
  1314. node to the of a class.
  1315. </div>
  1316. <p>
  1317. <b><tt>Node *parentNode(Node *n)</tt></b>
  1318. </p>
  1319. <div class="indent">
  1320. Returns the parent of node <tt>n</tt>. Use this to move up the pass tree.
  1321. </div>
  1322. <p>
  1323. The following macros can be used to change all of the above attributes.
  1324. Normally, these functions are only used by the parser. Changing them without
  1325. knowing what you are doing is likely to be dangerous.
  1326. </p>
  1327. <p>
  1328. <b><tt>void set_nodeType(Node *n, const String_or_char)</tt></b>
  1329. </p>
  1330. <div class="indent">
  1331. Change the node type.
  1332. tree node.
  1333. </div>
  1334. <p>
  1335. <b><tt>void set_nextSibling(Node *n, Node *s)</tt></b>
  1336. </p>
  1337. <div class="indent">
  1338. Set the next sibling.
  1339. </div>
  1340. <p>
  1341. <b><tt>void set_previousSibling(Node *n, Node *s)</tt></b>
  1342. </p>
  1343. <div class="indent">
  1344. Set the previous sibling.
  1345. </div>
  1346. <p>
  1347. <b><tt>void set_firstChild(Node *n, Node *c)</tt></b>
  1348. </p>
  1349. <div class="indent">
  1350. Set the first child node.
  1351. </div>
  1352. <p>
  1353. <b><tt>void set_lastChild(Node *n, Node *c)</tt></b>
  1354. </p>
  1355. <div class="indent">
  1356. Set the last child node.
  1357. </div>
  1358. <p>
  1359. <b><tt>void set_parentNode(Node *n, Node *p)</tt></b>
  1360. </p>
  1361. <div class="indent">
  1362. Set the parent node.
  1363. </div>
  1364. <p>
  1365. The following utility functions are used to alter the parse tree (at your own risk)
  1366. </p>
  1367. <p>
  1368. <b><tt>void appendChild(Node *parent, Node *child)</tt></b>
  1369. </p>
  1370. <div class="indent">
  1371. Append a child to <tt>parent</tt>. The appended node becomes the last child.
  1372. </div>
  1373. <p>
  1374. <b><tt>void deleteNode(Node *node)</tt></b>
  1375. </p>
  1376. <div class="indent">
  1377. Deletes a node from the parse tree. Deletion reconnects siblings and properly updates
  1378. the parent so that sibling nodes are unaffected.
  1379. </div>
  1380. <H2><a name="Extending_nn22"></a>35.7 Working with attributes</H2>
  1381. <p>
  1382. Since parse tree nodes are just hash tables, attributes are accessed using the <tt>Getattr()</tt>,
  1383. <tt>Setattr()</tt>, and <tt>Delattr()</tt> operations. For example:
  1384. </p>
  1385. <div class="code">
  1386. <pre>
  1387. int functionHandler(Node *n) {
  1388. String *name = Getattr(n,"name");
  1389. String *symname = Getattr(n,"sym:name");
  1390. SwigType *type = Getattr(n,"type");
  1391. ...
  1392. }
  1393. </pre>
  1394. </div>
  1395. <p>
  1396. New attributes can be freely attached to a node as needed. However, when new attributes
  1397. are attached during code generation, they should be prepended with a namespace prefix.
  1398. For example:
  1399. </p>
  1400. <div class="code">
  1401. <pre>
  1402. ...
  1403. Setattr(n,"python:docstring", doc); /* Store docstring */
  1404. ...
  1405. </pre>
  1406. </div>
  1407. <p>
  1408. A quick way to check the value of an attribute is to use the <tt>checkAttribute()</tt> function like this:
  1409. </p>
  1410. <div class="code">
  1411. <pre>
  1412. if (checkAttribute(n,"storage","virtual")) {
  1413. /* n is virtual */
  1414. ...
  1415. }
  1416. </pre>
  1417. </div>
  1418. <p>
  1419. Changing the values of existing attributes is allowed and is sometimes done to implement
  1420. node transformations. However, if a function/method modifies a node, it is required to restore
  1421. modified attributes to their original values. To simplify the task of saving/restoring attributes,
  1422. the following functions are used:
  1423. </p>
  1424. <p>
  1425. <b><tt>int Swig_save(const char *ns, Node *n, const char *name1, const char *name2, ..., NIL)</tt></b>
  1426. </p>
  1427. <div class="indent">
  1428. Saves a copy of attributes <tt>name1</tt>, <tt>name2</tt>, etc. from node <tt>n</tt>.
  1429. Copies of the attributes are actually resaved in the node in a different namespace which is
  1430. set by the <tt>ns</tt> argument. For example, if you call <tt>Swig_save("foo",n,"type",NIL)</tt>,
  1431. then the "type" attribute will be copied and saved as "foo:type". The namespace name itself is stored in
  1432. the "view" attribute of the node. If necessary, this can be examined to find out where previous
  1433. values of attributes might have been saved.
  1434. </div>
  1435. <p>
  1436. <b><tt>int Swig_restore(Node *n)</tt></b>
  1437. </p>
  1438. <div class="indent">
  1439. <p>
  1440. Restores the attributes saved by the previous call to <tt>Swig_save()</tt>. Those
  1441. attributes that were supplied to <tt>Swig_save()</tt> will be restored to their
  1442. original values.
  1443. </p>
  1444. <p>
  1445. The <tt>Swig_save()</tt> and <tt>Swig_restore()</tt> functions must always be used as a pair.
  1446. That is, every call to <tt>Swig_save()</tt> must have a matching call to <tt>Swig_restore()</tt>.
  1447. Calls can be nested if necessary. Here is an example that shows how the functions might be used:
  1448. </p>
  1449. <div class="code">
  1450. <pre>
  1451. int variableHandler(Node *n) {
  1452. Swig_save("variableHandler",n,"type","sym:name",NIL);
  1453. String *symname = Getattr(n,"sym:name");
  1454. SwigType *type = Getattr(n,"type");
  1455. ...
  1456. Append(symname,"_global"); // Change symbol name
  1457. SwigType_add_pointer(type); // Add pointer
  1458. ...
  1459. generate wrappers
  1460. ...
  1461. Swig_restore(n); // Restore original values
  1462. return SWIG_OK;
  1463. }
  1464. </pre>
  1465. </div>
  1466. </div>
  1467. <p>
  1468. <b><tt>int Swig_require(const char *ns, Node *n, const char *name1, const char *name2, ..., NIL)</tt></b>
  1469. </p>
  1470. <div class="indent">
  1471. This is an enhanced version of <tt>Swig_save()</tt> that adds error checking. If an attribute
  1472. name is not present in <tt>n</tt>, a failed assertion results and SWIG terminates with a fatal
  1473. error. Optionally, if an attribute name is specified as "*<em>name</em>", a copy of the
  1474. attribute is saved as with <tt>Swig_save()</tt>. If an attribute is specified as "?<em>name</em>",
  1475. the attribute is optional. <tt>Swig_restore()</tt> must always be called after using this
  1476. function.
  1477. </div>
  1478. <H2><a name="Extending_nn23"></a>35.8 Type system</H2>
  1479. <p>
  1480. SWIG implements the complete C++ type system including typedef, inheritance,
  1481. pointers, references, and pointers to members. A detailed discussion of
  1482. type theory is impossible here. However, let's cover the highlights.
  1483. </p>
  1484. <H3><a name="Extending_nn24"></a>35.8.1 String encoding of types</H3>
  1485. <p>
  1486. All types in SWIG consist of a base datatype and a collection of type
  1487. operators that are applied to the base. A base datatype is almost
  1488. always some kind of primitive type such as <tt>int</tt> or <tt>double</tt>.
  1489. The operators consist of things like pointers, references, arrays, and so forth.
  1490. Internally, types are represented as strings that are constructed in a very
  1491. precise manner. Here are some examples:
  1492. </p>
  1493. <div class="diagram">
  1494. <pre>
  1495. C datatype SWIG encoding (strings)
  1496. ----------------------------- --------------------------
  1497. int "int"
  1498. int * "p.int"
  1499. const int * "p.q(const).int"
  1500. int (*x)(int,double) "p.f(int,double).int"
  1501. int [20][30] "a(20).a(30).int"
  1502. int (F::*)(int) "m(F).f(int).int"
  1503. vector&lt;int&gt; * "p.vector&lt;(int)&gt;"
  1504. </pre>
  1505. </div>
  1506. <p>
  1507. Reading the SWIG encoding is often easier than figuring out the C code---just
  1508. read it from left to right. For a type of "p.f(int,double).int" is
  1509. a "pointer to a function(int,double) that returns int".
  1510. </p>
  1511. <p>
  1512. The following operator encodings are used in type strings:
  1513. </p>
  1514. <div class="diagram">
  1515. <pre>
  1516. Operator Meaning
  1517. ------------------- -------------------------------
  1518. p. Pointer to
  1519. a(n). Array of dimension n
  1520. r. C++ reference
  1521. m(class). Member pointer to class
  1522. f(args). Function.
  1523. q(qlist). Qualifiers
  1524. </pre>
  1525. </div>
  1526. <p>
  1527. In addition, type names may be parameterized by templates. This is
  1528. represented by enclosing the template parameters in <tt>&lt;(
  1529. ... )&gt;</tt>. Variable length arguments are represented by the
  1530. special base type of <tt>v(...)</tt>.
  1531. </p>
  1532. <p>
  1533. If you want to experiment with type encodings, the raw type strings can
  1534. be inserted into an interface file using backticks `` wherever a type
  1535. is expected. For instance, here is
  1536. an extremely perverted example:
  1537. </p>
  1538. <div class="diagram">
  1539. <pre>
  1540. `p.a(10).p.f(int,p.f(int).int)` foo(int, int (*x)(int));
  1541. </pre>
  1542. </div>
  1543. <p>
  1544. This corresponds to the immediately obvious C declaration:
  1545. </p>
  1546. <div class="diagram">
  1547. <pre>
  1548. (*(*foo(int,int (*)(int)))[10])(int,int (*)(int));
  1549. </pre>
  1550. </div>
  1551. <p>
  1552. Aside from the potential use of this declaration on a C programming quiz,
  1553. it motivates the use of the special SWIG encoding of types. The SWIG
  1554. encoding is much easier to work with because types can be easily examined,
  1555. modified, and constructed using simple string operations (comparison,
  1556. substrings, concatenation, etc.). For example, in the parser, a declaration
  1557. like this
  1558. </p>
  1559. <div class="code">
  1560. <pre>
  1561. int *a[30];
  1562. </pre>
  1563. </div>
  1564. <p>
  1565. is processed in a few pieces. In this case, you have the base type
  1566. "<tt>int</tt>" and the declarator of type "<tt>a(30).p.</tt>". To
  1567. make the final type, the two parts are just joined together using
  1568. string concatenation.
  1569. </p>
  1570. <H3><a name="Extending_nn25"></a>35.8.2 Type construction</H3>
  1571. <p>
  1572. The following functions are used to construct types. You should use
  1573. these functions instead of trying to build the type strings yourself.
  1574. </p>
  1575. <p>
  1576. <b><tt>void SwigType_add_pointer(SwigType *ty)</tt></b>
  1577. </p>
  1578. <div class="indent">
  1579. Adds a pointer to <tt>ty</tt>.
  1580. </div>
  1581. <p>
  1582. <b><tt>void SwigType_del_pointer(SwigType *ty)</tt></b>
  1583. </p>
  1584. <div class="indent">
  1585. Removes a single pointer from <tt>ty</tt>.
  1586. </div>
  1587. <p>
  1588. <b><tt>void SwigType_add_reference(SwigType *ty)</tt></b>
  1589. </p>
  1590. <div class="indent">
  1591. Adds a reference to <tt>ty</tt>.
  1592. </div>
  1593. <p>
  1594. <b><tt>void SwigType_add_array(SwigType *ty, const String_or_char *size)</tt></b>
  1595. </p>
  1596. <div class="indent">
  1597. Adds an array with dimension <tt>dim</tt> to <tt>ty</tt>.
  1598. </div>
  1599. <p>
  1600. <b><tt>void SwigType_del_array(SwigType *ty)</tt></b>
  1601. </p>
  1602. <div class="indent">
  1603. Removes a single array dimension from <tt>ty</tt>.
  1604. </div>
  1605. <p>
  1606. <b><tt>int SwigType_array_ndim(SwigType *ty)</tt></b>
  1607. </p>
  1608. <div class="indent">
  1609. Returns number of array dimensions of <tt>ty</tt>.
  1610. </div>
  1611. <p>
  1612. <b><tt>String* SwigType_array_getdim(SwigType *ty,int n)</tt></b>
  1613. </p>
  1614. <div class="indent">
  1615. Returns <tt>n</tt>th array dimension of <tt>ty</tt>.
  1616. </div>
  1617. <p>
  1618. <b><tt>void SwigType_array_setdim(SwigType *ty, int n, const String_or_char *rep)</tt></b>
  1619. </p>
  1620. <div class="indent">
  1621. Sets <tt>n</tt>th array dimensions of <tt>ty</tt> to <tt>rep</tt>.
  1622. </div>
  1623. <p>
  1624. <b><tt>void SwigType_add_qualifier(SwigType *ty, const String_or_char *q)</tt></b>
  1625. </p>
  1626. <div class="indent">
  1627. Adds a type qualifier <tt>q</tt> to <tt>ty</tt>. <tt>q</tt> is typically
  1628. <tt>"const"</tt> or <tt>"volatile"</tt>.
  1629. </div>
  1630. <p>
  1631. <b><tt>void SwigType_add_memberpointer(SwigType *ty, const String_or_char *cls)</tt></b>
  1632. </p>
  1633. <div class="indent">
  1634. Adds a pointer to a member of class <tt>cls</tt> to <tt>ty</tt>.
  1635. </div>
  1636. <p>
  1637. <b><tt>void SwigType_add_function(SwigType *ty, ParmList *p)</tt></b>
  1638. </p>
  1639. <div class="indent">
  1640. Adds a function to <tt>ty</tt>. <tt>p</tt> is a linked-list of parameter
  1641. nodes as generated by the parser. See the section on parameter lists
  1642. for details about the representation.
  1643. </div>
  1644. <p>
  1645. <b><tt>void SwigType_add_template(SwigType *ty, ParmList *p)</tt></b>
  1646. </p>
  1647. <div class="indent">
  1648. Adds a template to <tt>ty</tt>. <tt>p</tt> is a linked-list of parameter
  1649. nodes as generated by the parser. See the section on parameter lists
  1650. for details about the representation.
  1651. </div>
  1652. <p>
  1653. <b><tt>SwigType *SwigType_pop(SwigType *ty)</tt></b>
  1654. </p>
  1655. <div class="indent">
  1656. Removes the last type constructor from <tt>ty</tt> and returns it.
  1657. <tt>ty</tt> is modified.
  1658. </div>
  1659. <p>
  1660. <b><tt>void SwigType_push(SwigType *ty, SwigType *op)</tt></b>
  1661. </p>
  1662. <div class="indent">
  1663. Pushes the type operators in <tt>op</tt> onto type <tt>ty</tt>. The
  1664. opposite of <tt>SwigType_pop()</tt>.
  1665. </div>
  1666. <p>
  1667. <b><tt>SwigType *SwigType_pop_arrays(SwigType *ty)</tt></b>
  1668. </p>
  1669. <div class="indent">
  1670. Removes all leading array operators from <tt>ty</tt> and returns them.
  1671. <tt>ty</tt> is modified. For example, if <tt>ty</tt> is <tt>"a(20).a(10).p.int"</tt>,
  1672. then this function would return <tt>"a(20).a(10)."</tt> and modify <tt>ty</tt>
  1673. so that it has the value <tt>"p.int"</tt>.
  1674. </div>
  1675. <p>
  1676. <b><tt>SwigType *SwigType_pop_function(SwigType *ty)</tt></b>
  1677. </p>
  1678. <div class="indent">
  1679. Removes a function operator from <tt>ty</tt> including any qualification.
  1680. <tt>ty</tt> is modified. For example, if <tt>ty</tt> is <tt>"f(int).int"</tt>,
  1681. then this function would return <tt>"f(int)."</tt> and modify <tt>ty</tt>
  1682. so that it has the value <tt>"int"</tt>.
  1683. </div>
  1684. <p>
  1685. <b><tt>SwigType *SwigType_base(SwigType *ty)</tt></b>
  1686. </p>
  1687. <div class="indent">
  1688. Returns the base type of a type. For example, if <tt>ty</tt> is
  1689. <tt>"p.a(20).int"</tt>, this function would return <tt>"int"</tt>.
  1690. <tt>ty</tt> is unmodified.
  1691. </div>
  1692. <p>
  1693. <b><tt>SwigType *SwigType_prefix(SwigType *ty)</tt></b>
  1694. </p>
  1695. <div class="indent">
  1696. Returns the prefix of a type. For example, if <tt>ty</tt> is
  1697. <tt>"p.a(20).int"</tt>, this function would return <tt>"p.a(20)."</tt>.
  1698. <tt>ty</tt> is unmodified.
  1699. </div>
  1700. <H3><a name="Extending_nn26"></a>35.8.3 Type tests</H3>
  1701. <p>
  1702. The following functions can be used to test properties of a datatype.
  1703. </p>
  1704. <p>
  1705. <b><tt>int SwigType_ispointer(SwigType *ty)</tt></b>
  1706. </p>
  1707. <div class="indent">
  1708. Checks if <tt>ty</tt> is a standard pointer.
  1709. </div>
  1710. <p>
  1711. <b><tt>int SwigType_ismemberpointer(SwigType *ty)</tt></b>
  1712. </p>
  1713. <div class="indent">
  1714. Checks if <tt>ty</tt> is a member pointer.
  1715. </div>
  1716. <p>
  1717. <b><tt>int SwigType_isreference(SwigType *ty)</tt></b>
  1718. </p>
  1719. <div class="indent">
  1720. Checks if <tt>ty</tt> is a C++ reference.
  1721. </div>
  1722. <p>
  1723. <b><tt>int SwigType_isarray(SwigType *ty)</tt></b>
  1724. </p>
  1725. <div class="indent">
  1726. Checks if <tt>ty</tt> is an array.
  1727. </div>
  1728. <p>
  1729. <b><tt>int SwigType_isfunction(SwigType *ty)</tt></b>
  1730. </p>
  1731. <div class="indent">
  1732. Checks if <tt>ty</tt> is a function.
  1733. </div>
  1734. <p>
  1735. <b><tt>int SwigType_isqualifier(SwigType *ty)</tt></b>
  1736. </p>
  1737. <div class="indent">
  1738. Checks if <tt>ty</tt> is a qualifier.
  1739. </div>
  1740. <p>
  1741. <b><tt>int SwigType_issimple(SwigType *ty)</tt></b>
  1742. </p>
  1743. <div class="indent">
  1744. Checks if <tt>ty</tt> is a simple type. No operators applied.
  1745. </div>
  1746. <p>
  1747. <b><tt>int SwigType_isconst(SwigType *ty)</tt></b>
  1748. </p>
  1749. <div class="indent">
  1750. Checks if <tt>ty</tt> is a const type.
  1751. </div>
  1752. <p>
  1753. <b><tt>int SwigType_isvarargs(SwigType *ty)</tt></b>
  1754. </p>
  1755. <div class="indent">
  1756. Checks if <tt>ty</tt> is a varargs type.
  1757. </div>
  1758. <p>
  1759. <b><tt>int SwigType_istemplate(SwigType *ty)</tt></b>
  1760. </p>
  1761. <div class="indent">
  1762. Checks if <tt>ty</tt> is a templatized type.
  1763. </div>
  1764. <H3><a name="Extending_nn27"></a>35.8.4 Typedef and inheritance</H3>
  1765. <p>
  1766. The behavior of <tt>typedef</tt> declaration is to introduce a type alias.
  1767. For instance, <tt>typedef int Integer</tt> makes the identifier
  1768. <tt>Integer</tt> an alias for <tt>int</tt>. The treatment of typedef in
  1769. SWIG is somewhat complicated due to the pattern matching rules that get applied
  1770. in typemaps and the fact that SWIG prefers to generate wrapper code
  1771. that closely matches the input to simplify debugging (a user will see the
  1772. typedef names used in their program instead of the low-level primitive C
  1773. datatypes).
  1774. </p>
  1775. <p>
  1776. To handle <tt>typedef</tt>, SWIG builds a collection of trees containing typedef relations. For example,
  1777. </p>
  1778. <div class="code">
  1779. <pre>
  1780. typedef int Integer;
  1781. typedef Integer *IntegerPtr;
  1782. typedef int Number;
  1783. typedef int Size;
  1784. </pre>
  1785. </div>
  1786. <p>
  1787. produces two trees like this:
  1788. </p>
  1789. <div class="diagram">
  1790. <pre>
  1791. int p.Integer
  1792. ^ ^ ^ ^
  1793. / | \ |
  1794. / | \ |
  1795. Integer Size Number IntegerPtr
  1796. </pre>
  1797. </div>
  1798. <p>
  1799. To resolve a single typedef relationship, the following function is used:
  1800. </p>
  1801. <p>
  1802. <b><tt>SwigType *SwigType_typedef_resolve(SwigType *ty)</tt></b>
  1803. </p>
  1804. <div class="indent">
  1805. Checks if <tt>ty</tt> can be reduced to a new type via typedef. If so,
  1806. returns the new type. If not, returns NULL.
  1807. </div>
  1808. <p>
  1809. Typedefs are only resolved in simple typenames that appear in a type.
  1810. For example, the type base name and in function parameters. When
  1811. resolving types, the process starts in the leaf nodes and moves up
  1812. the tree towards the root. Here are a few examples that show how it works:
  1813. </p>
  1814. <div class="diagram">
  1815. <pre>
  1816. Original type After typedef_resolve()
  1817. ------------------------ -----------------------
  1818. Integer int
  1819. a(30).Integer int
  1820. p.IntegerPtr p.p.Integer
  1821. p.p.Integer p.p.int
  1822. </pre>
  1823. </div>
  1824. <p>
  1825. For complicated types, the process can be quite involved. Here is the
  1826. reduction of a function pointer:
  1827. </p>
  1828. <div class="diagram">
  1829. <pre>
  1830. p.f(Integer, p.IntegerPtr, Size).Integer : Start
  1831. p.f(Integer, p.IntegerPtr, Size).int
  1832. p.f(int, p.IntegerPtr, Size).int
  1833. p.f(int, p.p.Integer, Size).int
  1834. p.f(int, p.p.int, Size).int
  1835. p.f(int, p.p.int, int).int : End
  1836. </pre>
  1837. </div>
  1838. <p>
  1839. Two types are equivalent if their full type reductions are the same.
  1840. The following function will fully reduce a datatype:
  1841. </p>
  1842. <p>
  1843. <b><tt>SwigType *SwigType_typedef_resolve_all(SwigType *ty)</tt></b>
  1844. </p>
  1845. <div class="indent">
  1846. Fully reduces <tt>ty</tt> according to typedef rules. Resulting datatype
  1847. will consist only of primitive typenames.
  1848. </div>
  1849. <H3><a name="Extending_nn28"></a>35.8.5 Lvalues</H3>
  1850. <p>
  1851. When generating wrapper code, it is necessary to emit datatypes that can
  1852. be used on the left-hand side of an assignment operator (an lvalue). However,
  1853. not all C datatypes can be used in this way---especially arrays and
  1854. const-qualified types. To generate a type that can be used as an lvalue,
  1855. use the following function:
  1856. </p>
  1857. <p>
  1858. <b><tt>SwigType *SwigType_ltype(SwigType *ty)</tt></b>
  1859. </p>
  1860. <div class="indent">
  1861. Converts type <tt>ty</tt> to a type that can be used as an lvalue in
  1862. assignment. The resulting type is stripped of qualifiers and arrays are
  1863. converted to a pointers.
  1864. </div>
  1865. <p>
  1866. The creation of lvalues is fully aware of typedef and other aspects
  1867. of the type system. Therefore, the creation of an lvalue may result in
  1868. unexpected results. Here are a few examples:
  1869. </p>
  1870. <div class="code">
  1871. <pre>
  1872. typedef double Matrix4[4][4];
  1873. Matrix4 x; // type = 'Matrix4', ltype='p.a(4).double'
  1874. typedef const char * Literal;
  1875. Literal y; // type = 'Literal', ltype='p.char'
  1876. </pre>
  1877. </div>
  1878. <H3><a name="Extending_nn29"></a>35.8.6 Output functions</H3>
  1879. <p>
  1880. The following functions produce strings that are suitable for output.
  1881. </p>
  1882. <p>
  1883. <b><tt>String *SwigType_str(SwigType *ty, const String_or_char *id = 0)</tt></b>
  1884. </p>
  1885. <div class="indent">
  1886. Generates a C string for a datatype. <tt>id</tt> is an optional declarator.
  1887. For example, if <tt>ty</tt> is "p.f(int).int" and <tt>id</tt> is "foo", then
  1888. this function produces "<tt>int (*foo)(int)</tt>". This function is
  1889. used to convert string-encoded types back into a form that is valid C syntax.
  1890. </div>
  1891. <p>
  1892. <b><tt>String *SwigType_lstr(SwigType *ty, const String_or_char *id = 0)</tt></b>
  1893. </p>
  1894. <div class="indent">
  1895. This is the same as <tt>SwigType_str()</tt> except that the result
  1896. is generated from the type's lvalue (as generated from SwigType_ltype).
  1897. </div>
  1898. <p>
  1899. <b><tt>String *SwigType_lcaststr(SwigType *ty, const String_or_char *id = 0)</tt></b>
  1900. </p>
  1901. <div class="indent">
  1902. Generates a casting operation that converts from type <tt>ty</tt> to its
  1903. lvalue. <tt>id</tt> is an optional name to include in the cast. For example,
  1904. if <tt>ty</tt> is "<tt>q(const).p.char</tt>" and <tt>id</tt> is "<tt>foo</tt>",
  1905. this function produces the string "<tt>(char *) foo</tt>".
  1906. </div>
  1907. <p>
  1908. <b><tt>String *SwigType_rcaststr(SwigType *ty, const String_or_char *id = 0)</tt></b>
  1909. </p>
  1910. <div class="indent">
  1911. Generates a casting operation that converts from a type's lvalue to a
  1912. type equivalent to <tt>ty</tt>. <tt>id</tt> is an optional name to
  1913. include in the cast. For example, if <tt>ty</tt> is
  1914. "<tt>q(const).p.char</tt>" and <tt>id</tt> is "<tt>foo</tt>", this
  1915. function produces the string "<tt>(const char *) foo</tt>".
  1916. </div>
  1917. <p>
  1918. <b><tt>String *SwigType_manglestr(SwigType *ty)</tt></b>
  1919. </p>
  1920. <div class="indent">
  1921. Generates a mangled string encoding of type <tt>ty</tt>. The
  1922. mangled string only contains characters that are part of a valid
  1923. C identifier. The resulting string is used in various parts of
  1924. SWIG, but is most commonly associated with type-descriptor objects
  1925. that appear in wrappers (e.g., <tt>SWIGTYPE_p_double</tt>).
  1926. </div>
  1927. <H2><a name="Extending_nn30"></a>35.9 Parameters</H2>
  1928. <p>
  1929. Several type-related functions involve parameter lists. These include
  1930. functions and templates. Parameter list are represented as a list of
  1931. nodes with the following attributes:
  1932. </p>
  1933. <div class="diagram">
  1934. <pre>
  1935. "type" - Parameter type (required)
  1936. "name" - Parameter name (optional)
  1937. "value" - Initializer (optional)
  1938. </pre>
  1939. </div>
  1940. <p>
  1941. Typically parameters are denoted in the source by using a typename of
  1942. <tt>Parm *</tt> or <tt>ParmList *</tt>. To walk a parameter list, simply use
  1943. code like this:
  1944. </p>
  1945. <div class="diagram">
  1946. <pre>
  1947. Parm *parms;
  1948. Parm *p;
  1949. for (p = parms; p; p = nextSibling(p)) {
  1950. SwigType *type = Getattr(p,"type");
  1951. String *name = Getattr(p,"name");
  1952. String *value = Getattr(p,"value");
  1953. ...
  1954. }
  1955. </pre>
  1956. </div>
  1957. <p>
  1958. Note: this code is exactly the same as what you would use to walk parse tree nodes.
  1959. </p>
  1960. <p>
  1961. An empty list of parameters is denoted by a NULL pointer.
  1962. </p>
  1963. <p>
  1964. Since parameter lists are fairly common, the following utility functions are provided
  1965. to manipulate them:
  1966. </p>
  1967. <p>
  1968. <b><tt>Parm *CopyParm(Parm *p);</tt></b>
  1969. </p>
  1970. <div class="indent">
  1971. Copies a single parameter.
  1972. </div>
  1973. <p>
  1974. <b><tt>ParmList *CopyParmList(ParmList *p);</tt></b>
  1975. </p>
  1976. <div class="indent">
  1977. Copies an entire list of parameters.
  1978. </div>
  1979. <p>
  1980. <b><tt>int ParmList_len(ParmList *p);</tt></b>
  1981. </p>
  1982. <div class="indent">
  1983. Returns the number of parameters in a parameter list.
  1984. </div>
  1985. <p>
  1986. <b><tt>String *ParmList_str(ParmList *p);</tt></b>
  1987. </p>
  1988. <div class="indent">
  1989. Converts a parameter list into a C string. For example,
  1990. produces a string like "<tt>(int *p, int n, double x);</tt>".
  1991. </div>
  1992. <p>
  1993. <b><tt>String *ParmList_protostr(ParmList *p);</tt></b>
  1994. </p>
  1995. <div class="indent">
  1996. The same as <tt>ParmList_str()</tt> except that parameter names are not
  1997. included. Used to emit prototypes.
  1998. </div>
  1999. <p>
  2000. <b><tt>int ParmList_numrequired(ParmList *p);</tt></b>
  2001. </p>
  2002. <div class="indent">
  2003. Returns the number of required (non-optional) arguments in <tt>p</tt>.
  2004. </div>
  2005. <H2><a name="Extending_nn31"></a>35.10 Writing a Language Module</H2>
  2006. <p>
  2007. One of the easiest routes to supporting a new language module is to copy an already
  2008. supported language module implementation and modify it.
  2009. Be sure to choose a language that is similar in nature to the new language.
  2010. All language modules follow a similar structure and
  2011. this section briefly outlines the steps needed to create a bare-bones
  2012. language module from scratch.
  2013. Since the code is relatively easy to read, this section
  2014. describes the creation of a minimal Python module. You should be able to extrapolate
  2015. this to other languages.
  2016. </p>
  2017. <H3><a name="Extending_nn32"></a>35.10.1 Execution model</H3>
  2018. <p>
  2019. Code generation modules are defined by inheriting from the <tt>Language</tt> class,
  2020. currently defined in the <tt>Source/Modules</tt> directory of SWIG. Starting from
  2021. the parsing of command line options, all aspects of code generation are controlled by
  2022. different methods of the <tt>Language</tt> that must be defined by your module.
  2023. </p>
  2024. <H3><a name="Extending_starting_out"></a>35.10.2 Starting out</H3>
  2025. <p>
  2026. To define a new language module, first create a minimal implementation using
  2027. this example as a guide:
  2028. </p>
  2029. <div class="code">
  2030. <pre>
  2031. #include "swigmod.h"
  2032. class PYTHON : public Language {
  2033. public:
  2034. virtual void main(int argc, char *argv[]) {
  2035. printf("I'm the Python module.\n");
  2036. }
  2037. virtual int top(Node *n) {
  2038. printf("Generating code.\n");
  2039. return SWIG_OK;
  2040. }
  2041. };
  2042. extern "C" Language *
  2043. swig_python(void) {
  2044. return new PYTHON();
  2045. }
  2046. </pre>
  2047. </div>
  2048. <p>
  2049. The "swigmod.h" header file contains, among other things, the declaration
  2050. of the <tt>Language</tt> base class and so you should include it at the top
  2051. of your language module's source file. Similarly, the "swigconfig.h" header
  2052. file contains some other useful definitions that you may need. Note that you
  2053. should <em>not</em> include any header files that are installed with the
  2054. target language. That is to say, the implementation of the SWIG Python module
  2055. shouldn't have any dependencies on the Python header files. The wrapper code
  2056. generated by SWIG will almost always depend on some language-specific C/C++
  2057. header files, but SWIG itself does not.
  2058. </p>
  2059. <p>
  2060. Give your language class a reasonable name, usually the same as the target language.
  2061. By convention, these class names are all uppercase (e.g. "PYTHON" for the Python
  2062. language module) but this is not a requirement. This class will ultimately consist
  2063. of a number of overrides of the virtual functions declared in the <tt>Language</tt>
  2064. base class, in addition to any language-specific member functions and data you
  2065. need. For now, just use the dummy implementations shown above.
  2066. </p>
  2067. <p>
  2068. The language module ends with a factory function, <tt>swig_python()</tt>, that simply
  2069. returns a new instance of the language class. As shown, it should be declared with the
  2070. <tt>extern "C"</tt> storage qualifier so that it can be called from C code. It should
  2071. also return a pointer to the base class (<tt>Language</tt>) so that only the interface
  2072. (and not the implementation) of your language module is exposed to the rest of SWIG.
  2073. </p>
  2074. <p>
  2075. Save the code for your language module in a file named "<tt>python.cxx</tt>" and.
  2076. place this file in the <tt>Source/Modules</tt> directory of the SWIG distribution.
  2077. To ensure that your module is compiled into SWIG along with the other language modules,
  2078. modify the file <tt>Source/Modules/Makefile.am</tt> to include the additional source
  2079. files. In addition, modify the file <tt>Source/Modules/swigmain.cxx</tt>
  2080. with an additional command line option that activates the module. Read the source---it's straightforward.
  2081. </p>
  2082. <p>
  2083. Next, at the top level of the SWIG distribution, re-run the <tt>autogen.sh</tt> script
  2084. to regenerate the various build files:
  2085. </p>
  2086. <div class="shell">
  2087. <pre>
  2088. $ <b>./autogen.sh</b>
  2089. </pre>
  2090. </div>
  2091. <p>
  2092. Next re-run <tt>configure</tt> to regenerate all of the Makefiles:
  2093. </p>
  2094. <div class="shell">
  2095. <pre>
  2096. $ <b>./configure</b>
  2097. </pre>
  2098. </div>
  2099. <p>
  2100. Finally, rebuild SWIG with your module added:
  2101. </p>
  2102. <div class="shell">
  2103. <pre>
  2104. $ <b>make</b>
  2105. </pre>
  2106. </div>
  2107. <p>
  2108. Once it finishes compiling, try running SWIG with the command-line option
  2109. that activates your module. For example, <tt>swig -python foo.i</tt>. The
  2110. messages from your new module should appear.
  2111. </p>
  2112. <H3><a name="Extending_nn34"></a>35.10.3 Command line options</H3>
  2113. <p>
  2114. When SWIG starts, the command line options are passed to your language module. This occurs
  2115. before any other processing occurs (preprocessing, parsing, etc.). To capture the
  2116. command line options, simply use code similar to this:
  2117. </p>
  2118. <div class="code">
  2119. <pre>
  2120. void Language::main(int argc, char *argv[]) {
  2121. for (int i = 1; i &lt; argc; i++) {
  2122. if (argv[i]) {
  2123. if(strcmp(argv[i],"-interface") == 0) {
  2124. if (argv[i+1]) {
  2125. interface = NewString(argv[i+1]);
  2126. Swig_mark_arg(i);
  2127. Swig_mark_arg(i+1);
  2128. i++;
  2129. } else {
  2130. Swig_arg_error();
  2131. }
  2132. } else if (strcmp(argv[i],"-globals") == 0) {
  2133. if (argv[i+1]) {
  2134. global_name = NewString(argv[i+1]);
  2135. Swig_mark_arg(i);
  2136. Swig_mark_arg(i+1);
  2137. i++;
  2138. } else {
  2139. Swig_arg_error();
  2140. }
  2141. } else if ( (strcmp(argv[i],"-proxy") == 0)) {
  2142. proxy_flag = 1;
  2143. Swig_mark_arg(i);
  2144. } else if (strcmp(argv[i],"-keyword") == 0) {
  2145. use_kw = 1;
  2146. Swig_mark_arg(i);
  2147. } else if (strcmp(argv[i],"-help") == 0) {
  2148. fputs(usage,stderr);
  2149. }
  2150. ...
  2151. }
  2152. }
  2153. }
  2154. </pre>
  2155. </div>
  2156. <p>
  2157. The exact set of options depends on what you want to do in your module. Generally,
  2158. you would use the options to change code generation modes or to print diagnostic information.
  2159. </p>
  2160. <p>
  2161. If a module recognizes an option, it should always call <tt>Swig_mark_arg()</tt>
  2162. to mark the option as valid. If you forget to do this, SWIG will terminate with an
  2163. unrecognized command line option error.
  2164. </p>
  2165. <H3><a name="Extending_nn35"></a>35.10.4 Configuration and preprocessing</H3>
  2166. <p>
  2167. In addition to looking at command line options, the <tt>main()</tt> method is responsible
  2168. for some initial configuration of the SWIG library and preprocessor. To do this,
  2169. insert some code like this:
  2170. </p>
  2171. <div class="code">
  2172. <pre>
  2173. void main(int argc, char *argv[]) {
  2174. ... command line options ...
  2175. /* Set language-specific subdirectory in SWIG library */
  2176. SWIG_library_directory("python");
  2177. /* Set language-specific preprocessing symbol */
  2178. Preprocessor_define("SWIGPYTHON 1", 0);
  2179. /* Set language-specific configuration file */
  2180. SWIG_config_file("python.swg");
  2181. /* Set typemap language (historical) */
  2182. SWIG_typemap_lang("python");
  2183. }
  2184. </pre>
  2185. </div>
  2186. <p>
  2187. The above code does several things--it registers the name of the
  2188. language module with the core, it supplies some preprocessor macro definitions
  2189. for use in input files (so that they can determine the target language), and
  2190. it registers a start-up file. In this case, the file <tt>python.swg</tt> will
  2191. be parsed before any part of the user-supplied input file.
  2192. </p>
  2193. <p>
  2194. Before proceeding any further, create a directory for your module in the SWIG
  2195. library (The <tt>Lib</tt> directory). Now, create a configuration file in the
  2196. directory. For example, <tt>python.swg</tt>.
  2197. </p>
  2198. <p>
  2199. Just to review, your language module should now consist of two files--
  2200. an implementation file <tt>python.cxx</tt> and a configuration file
  2201. <tt>python.swg</tt>.
  2202. </p>
  2203. <H3><a name="Extending_nn36"></a>35.10.5 Entry point to code generation</H3>
  2204. <p>
  2205. SWIG is a multi-pass compiler. Once the <tt>main()</tt> method has
  2206. been invoked, the language module does not execute again until
  2207. preprocessing, parsing, and a variety of semantic analysis passes have
  2208. been performed. When the core is ready to start generating wrappers,
  2209. it invokes the <tt>top()</tt> method of your language class. The
  2210. argument to <tt>top</tt> is a single parse tree node that corresponds to
  2211. the top of the entire parse tree.
  2212. </p>
  2213. <p>
  2214. To get the code generation process started, the <tt>top()</tt> procedure needs
  2215. to do several things:
  2216. </p>
  2217. <ul>
  2218. <li>Initialize the wrapper code output.
  2219. <li>Set the module name.
  2220. <li>Emit common initialization code.
  2221. <li>Emit code for all of the child nodes.
  2222. <li>Finalize the wrapper module and cleanup.
  2223. </ul>
  2224. <p>
  2225. An outline of <tt>top()</tt> might be as follows:
  2226. </p>
  2227. <div class="code">
  2228. <pre>
  2229. int Python::top(Node *n) {
  2230. /* Get the module name */
  2231. String *module = Getattr(n,"name");
  2232. /* Get the output file name */
  2233. String *outfile = Getattr(n,"outfile");
  2234. /* Initialize I/O (see next section) */
  2235. ...
  2236. /* Output module initialization code */
  2237. ...
  2238. /* Emit code for children */
  2239. Language::top(n);
  2240. ...
  2241. /* Cleanup files */
  2242. ...
  2243. return SWIG_OK;
  2244. }
  2245. </pre>
  2246. </div>
  2247. <H3><a name="Extending_nn37"></a>35.10.6 Module I/O and wrapper skeleton</H3>
  2248. <!-- please report bugs in this section to mgossage -->
  2249. <p>
  2250. Within SWIG wrappers, there are four main sections. These are (in order)
  2251. </p>
  2252. <ul>
  2253. <li>runtime: This section has most of the common SWIG runtime code
  2254. <li>header: This section holds declarations and inclusions from the .i file
  2255. <li>wrapper: This section holds all the wrappering code
  2256. <li>init: This section holds the module initalisation function
  2257. (the entry point for the interpreter)
  2258. </ul>
  2259. <p>
  2260. Different parts of the SWIG code will fill different sections,
  2261. then upon completion of the wrappering all the sections will be saved
  2262. to the wrapper file.
  2263. </p>
  2264. <p>
  2265. To perform this will require several additions to the code in various places,
  2266. such as:
  2267. </p>
  2268. <div class="code">
  2269. <pre>
  2270. class PYTHON : public Language {
  2271. protected:
  2272. /* General DOH objects used for holding the strings */
  2273. File *f_begin;
  2274. File *f_runtime;
  2275. File *f_header;
  2276. File *f_wrappers;
  2277. File *f_init;
  2278. public:
  2279. ...
  2280. };
  2281. int Python::top(Node *n) {
  2282. ...
  2283. /* Initialize I/O */
  2284. f_begin = NewFile(outfile, "w", SWIG_output_files());
  2285. if (!f_begin) {
  2286. FileErrorDisplay(outfile);
  2287. SWIG_exit(EXIT_FAILURE);
  2288. }
  2289. f_runtime = NewString("");
  2290. f_init = NewString("");
  2291. f_header = NewString("");
  2292. f_wrappers = NewString("");
  2293. /* Register file targets with the SWIG file handler */
  2294. Swig_register_filebyname("begin", f_begin);
  2295. Swig_register_filebyname("header", f_header);
  2296. Swig_register_filebyname("wrapper", f_wrappers);
  2297. Swig_register_filebyname("runtime", f_runtime);
  2298. Swig_register_filebyname("init", f_init);
  2299. /* Output module initialization code */
  2300. Swig_banner(f_begin);
  2301. ...
  2302. /* Emit code for children */
  2303. Language::top(n);
  2304. ...
  2305. /* Write all to the file */
  2306. Dump(f_runtime, f_begin);
  2307. Dump(f_header, f_begin);
  2308. Dump(f_wrappers, f_begin);
  2309. Wrapper_pretty_print(f_init, f_begin);
  2310. /* Cleanup files */
  2311. Delete(f_runtime);
  2312. Delete(f_header);
  2313. Delete(f_wrappers);
  2314. Delete(f_init);
  2315. Close(f_begin);
  2316. Delete(f_begin);
  2317. return SWIG_OK;
  2318. }
  2319. </pre>
  2320. </div>
  2321. <p>
  2322. Using this to process a file will generate a wrapper file, however the
  2323. wrapper will only consist of the common SWIG code as well as any inline
  2324. code which was written in the .i file. It does not contain any wrappers for
  2325. any of the functions or classes.
  2326. </p>
  2327. <p>
  2328. The code to generate the wrappers are the various member functions, which
  2329. currently have not been touched. We will look at <tt>functionWrapper()</tt> as this
  2330. is the most commonly used function. In fact many of the other wrapper routines
  2331. will call this to do their work.
  2332. </p>
  2333. <p>
  2334. A simple modification to write some basic details to the wrapper looks like this:
  2335. </p>
  2336. <div class="code">
  2337. <pre>
  2338. int Python::functionWrapper(Node *n) {
  2339. /* Get some useful attributes of this function */
  2340. String *name = Getattr(n,"sym:name");
  2341. SwigType *type = Getattr(n,"type");
  2342. ParmList *parms = Getattr(n,"parms");
  2343. String *parmstr= ParmList_str_defaultargs(parms); // to string
  2344. String *func = SwigType_str(type, NewStringf("%s(%s)", name, parmstr));
  2345. String *action = Getattr(n,"wrap:action");
  2346. Printf(f_wrappers,"functionWrapper : %s\n", func);
  2347. Printf(f_wrappers," action : %s\n", action);
  2348. return SWIG_OK;
  2349. }
  2350. </pre>
  2351. </div>
  2352. <p>
  2353. This will now produce some useful information within your wrapper file.
  2354. </p>
  2355. <div class="shell">
  2356. <pre>
  2357. functionWrapper : void delete_Shape(Shape *self)
  2358. action : delete arg1;
  2359. functionWrapper : void Shape_x_set(Shape *self,double x)
  2360. action : if (arg1) (arg1)-&gt;x = arg2;
  2361. functionWrapper : double Shape_x_get(Shape *self)
  2362. action : result = (double) ((arg1)-&gt;x);
  2363. functionWrapper : void Shape_y_set(Shape *self,double y)
  2364. action : if (arg1) (arg1)-&gt;y = arg2;
  2365. ...
  2366. </pre>
  2367. </div>
  2368. <H3><a name="Extending_nn38"></a>35.10.7 Low-level code generators</H3>
  2369. <!-- please report bugs in this section to mgossage -->
  2370. <p>
  2371. As ingenious as SWIG is, and despite all its capabilities and the power of
  2372. its parser, the Low-level code generation takes a lot of work to write
  2373. properly. Mainly because every language insists on its own manner of
  2374. interfacing to C/C++. To write the code generators you will need a good
  2375. understanding of how to manually write an interface to your chosen
  2376. language, so make sure you have your documentation handy.
  2377. </p>
  2378. <p>
  2379. At this point it is also probably a good idea to take a very simple file
  2380. (just one function), and try letting SWIG generate wrappers for many
  2381. different languages. Take a look at all of the wrappers generated, and decide
  2382. which one looks closest to the language you are trying to wrap.
  2383. This may help you to decide which code to look at.
  2384. </p>
  2385. <p>
  2386. In general most language wrappers look a little like this:
  2387. </p>
  2388. <div class="code">
  2389. <pre>
  2390. /* wrapper for TYPE3 some_function(TYPE1,TYPE2); */
  2391. RETURN_TYPE _wrap_some_function(ARGS){
  2392. TYPE1 arg1;
  2393. TYPE2 arg2;
  2394. TYPE3 result;
  2395. if(ARG1 is not of TYPE1) goto fail;
  2396. arg1=(convert ARG1);
  2397. if(ARG2 is not of TYPE2) goto fail;
  2398. arg2=(convert ARG2);
  2399. result=some_function(arg1,arg2);
  2400. convert 'result' to whatever the language wants;
  2401. do any tidy up;
  2402. return ALL_OK;
  2403. fail:
  2404. do any tidy up;
  2405. return ERROR;
  2406. }
  2407. </pre>
  2408. </div>
  2409. <p>
  2410. Yes, it is rather vague and not very clear. But each language works differently
  2411. so this will have to do for now.
  2412. </p>
  2413. <p>
  2414. Tackling this problem will be done in two stages:
  2415. </p>
  2416. <ul>
  2417. <li>The skeleton: the function wrapper, and call, but without the conversion
  2418. <li>The conversion: converting the arguments to-from what the language wants
  2419. </ul>
  2420. <p>
  2421. The first step will be done in the code, the second will be done in typemaps.
  2422. </p>
  2423. <p>
  2424. Our first step will be to write the code for <tt>functionWrapper()</tt>. What is
  2425. shown below is <b>NOT</b> the solution, merely a step in the right direction.
  2426. There are a lot of issues to address.
  2427. </p>
  2428. <ul>
  2429. <li>Variable length and default parameters
  2430. <li>Typechecking and number of argument checks
  2431. <li>Overloaded functions
  2432. <li>Inout and Output only arguments
  2433. </ul>
  2434. <div class="code">
  2435. <pre>
  2436. virtual int functionWrapper(Node *n) {
  2437. /* get useful atributes */
  2438. String *name = Getattr(n,"sym:name");
  2439. SwigType *type = Getattr(n,"type");
  2440. ParmList *parms = Getattr(n,"parms");
  2441. ...
  2442. /* create the wrapper object */
  2443. Wrapper *wrapper = NewWrapper();
  2444. /* create the functions wrappered name */
  2445. String *wname = Swig_name_wrapper(iname);
  2446. /* deal with overloading */
  2447. ....
  2448. /* write the wrapper function definition */
  2449. Printv(wrapper-&gt;def,"RETURN_TYPE ", wname, "(ARGS) {",NIL);
  2450. /* if any additional local variable needed, add them now */
  2451. ...
  2452. /* write the list of locals/arguments required */
  2453. emit_args(type, parms, wrapper);
  2454. /* check arguments */
  2455. ...
  2456. /* write typemaps(in) */
  2457. ....
  2458. /* write constriants */
  2459. ....
  2460. /* Emit the function call */
  2461. emit_action(n,wrapper);
  2462. /* return value if necessary */
  2463. ....
  2464. /* write typemaps(out) */
  2465. ....
  2466. /* add cleanup code */
  2467. ....
  2468. /* Close the function(ok) */
  2469. Printv(wrapper-&gt;code, "return ALL_OK;\n", NIL);
  2470. /* add the failure cleanup code */
  2471. ...
  2472. /* Close the function(error) */
  2473. Printv(wrapper-&gt;code, "return ERROR;\n", "}\n", NIL);
  2474. /* final substititions if applicable */
  2475. ...
  2476. /* Dump the function out */
  2477. Wrapper_print(wrapper,f_wrappers);
  2478. /* tidy up */
  2479. Delete(wname);
  2480. DelWrapper(wrapper);
  2481. return SWIG_OK;
  2482. }
  2483. </pre>
  2484. </div>
  2485. <p>
  2486. Executing this code will produce wrappers which have our basic skeleton
  2487. but without the typemaps, there is still work to do.
  2488. </p>
  2489. <H3><a name="Extending_nn39"></a>35.10.8 Configuration files</H3>
  2490. <!-- please report bugs in this section to ttn -->
  2491. <p>
  2492. At the time of this writing, SWIG supports nearly twenty languages,
  2493. which means that for continued sanity in maintaining the configuration
  2494. files, the language modules need to follow some conventions. These are
  2495. outlined here along with the admission that, yes it is ok to violate
  2496. these conventions in minor ways, as long as you know where to apply the
  2497. proper kludge to keep the overall system regular and running.
  2498. Engineering is the art of compromise, see...
  2499. </p>
  2500. <p>
  2501. Much of the maintenance regularity depends on choosing a suitable
  2502. nickname for your language module (and then using it in a controlled
  2503. way). Nicknames should be all lower case letters with an optional
  2504. numeric suffix (no underscores, no dashes, no spaces). Some examples
  2505. are: <TT>foo</TT>, <TT>bar</TT>, <TT>qux99</TT>.
  2506. </p>
  2507. <p>
  2508. The numeric suffix variant, as in the last example, is somewhat tricky
  2509. to work with because sometimes people expect to refer to the language
  2510. without this number but sometimes that number is extremely relevant
  2511. (especially when it corresponds to language implementation versions with
  2512. incompatible interfaces). New language modules that unavoidably require
  2513. a numeric suffix in their nickname should include that number in all
  2514. uses, or be prepared to kludge.
  2515. </p>
  2516. <p>
  2517. The nickname is used in four places:
  2518. </p>
  2519. <TABLE summary="nickname table">
  2520. <TR><TD><B>usage</B></TD><TD><B>transform</B></TD></TR>
  2521. <TR><TD>"skip" tag</TD><TD>(none)</TD></TR>
  2522. <TR><TD>Examples/ subdir name</TD><TD>(none)</TD></TR>
  2523. <TR><TD>Examples/GIFPlot/ subdir name</TD>
  2524. <TD>capitalize (upcase first letter)</TD></TR>
  2525. <TR><TD>Examples/test-suite/ subdir name</TD><TD>(none)</TD></TR>
  2526. <!-- add more uses here (remember to adjust header) -->
  2527. </TABLE>
  2528. <p>
  2529. As you can see, most usages are direct.
  2530. </p>
  2531. <dl>
  2532. <dt> <b>configure.in</b>
  2533. <dd> This file is processed by
  2534. <p>
  2535. <A HREF="http://www.gnu.org/software/autoconf/">autoconf</A>
  2536. to generate the <TT>configure</TT> script. This is where you
  2537. need to add shell script fragments and autoconf macros to detect the
  2538. presence of whatever development support your language module requires,
  2539. typically directories where headers and libraries can be found, and/or
  2540. utility programs useful for integrating the generated wrapper code.
  2541. </p>
  2542. <p>
  2543. Use the <TT>AC_ARG_WITH</TT>, <TT>AC_MSG_CHECKING</TT>, <TT>AC_SUBST</TT>
  2544. macros and so forth (see other languages for examples). Avoid using the
  2545. <TT>[</TT> and <TT>]</TT> character in shell script fragments. The
  2546. variable names passed to <TT>AC_SUBST</TT> should begin with the nickname,
  2547. entirely upcased.
  2548. </p>
  2549. <p>
  2550. At the end of the new section is the place to put the aforementioned
  2551. nickname kludges (should they be needed). See Perl5 for
  2552. examples of what to do. [If this is still unclear after you've read
  2553. the code, ping me and I'll expand on this further. --ttn]
  2554. </p>
  2555. <dt> <b>Makefile.in</b>
  2556. <dd>
  2557. <p>
  2558. Some of the variables AC_SUBSTituted are essential to the
  2559. support of your language module. Fashion these into a shell script
  2560. "test" clause and assign that to a skip tag using "-z" and "-o":
  2561. </p>
  2562. <div class="code"><tt>
  2563. skip-qux99 = [ -z "@QUX99INCLUDE@" -o -z "@QUX99LIBS" ]
  2564. </tt></div>
  2565. <p>
  2566. This means if those vars should ever be empty, qux99 support should
  2567. be considered absent and so it would be a good idea to skip actions that
  2568. might rely on it.
  2569. </p>
  2570. <p>
  2571. Here is where you may also define an alias (but then you'll need to
  2572. kludge --- don't do this):
  2573. </p>
  2574. <div class="code"><tt>
  2575. skip-qux = $(skip-qux99)
  2576. </tt></div>
  2577. <p>
  2578. Lastly, you need to modify each of <TT>check-aliveness</TT>,
  2579. <TT>check-examples</TT>, <TT>check-test-suite</TT>,
  2580. <TT>check-gifplot</TT> (all targets) and <TT>lib-languages</TT> (var).
  2581. Use the nickname for these, not the alias.
  2582. Note that you can do this even before you have any tests or examples
  2583. set up; the Makefile rules do some sanity checking and skip around
  2584. these kinds of problems.
  2585. </p>
  2586. <dt> <b>Examples/Makefile.in</b>
  2587. <dd> Nothing special here; see comments at top the of this file
  2588. and look to the existing languages for examples.
  2589. <dt> <b>Examples/qux99/check.list</b>
  2590. <dd> Do <TT>cp ../python/check.list .</TT> and modify to taste.
  2591. One subdir per line.
  2592. <dt> <b>Examples/GIFPlot/Qux99/check.list</b>
  2593. <dd> Do <TT>cp ../Python/check.list .</TT> and modify to taste.
  2594. One subdir per line.
  2595. <dt> <b>Lib/qux99/extra-install.list</b>
  2596. <dd> If you add your language to the top-level Makefile.in var
  2597. <TT>lib-languages</TT>, then <TT>make install</TT> will install
  2598. all <TT>*.i</TT> and <TT>*.swg</TT> files from the language-specific
  2599. subdirectory of <TT>Lib</TT>. Use (optional) file
  2600. <TT>extra-install.list</TT> in that directory to name
  2601. additional files to install (see ruby for example).
  2602. <dt> <b>Source/Modules/Makefile.am</b>
  2603. <dd> Add appropriate files to this Automake file. That's it!
  2604. <p>
  2605. When you have modified these files, please make sure that the new language module is completely
  2606. ignored if it is not installed and detected on a box, that is, <tt>make check-examples</tt> and <tt>make check-test-suite</tt>
  2607. politely displays the ignoring language message.
  2608. </p>
  2609. </dl>
  2610. <H3><a name="Extending_nn40"></a>35.10.9 Runtime support</H3>
  2611. <p>
  2612. Discuss the kinds of functions typically needed for SWIG runtime support (e.g.
  2613. <tt>SWIG_ConvertPtr()</tt> and <tt>SWIG_NewPointerObj()</tt>) and the names of
  2614. the SWIG files that implement those functions.
  2615. </p>
  2616. <H3><a name="Extending_nn41"></a>35.10.10 Standard library files</H3>
  2617. <p>
  2618. The standard library files that most languages supply keeps growing as SWIG matures.
  2619. The following are the minimum that are usually supported:
  2620. </p>
  2621. <ul>
  2622. <li> typemaps.i </li>
  2623. <li> std_string.i </li>
  2624. <li> std_vector.i </li>
  2625. <li> stl.i </li>
  2626. </ul>
  2627. <p>
  2628. Please copy these and modify for any new language.
  2629. </p>
  2630. <H3><a name="Extending_nn42"></a>35.10.11 User examples</H3>
  2631. <p>
  2632. Each of the language modules provides one or more examples. These examples
  2633. are used to demonstrate different features of the language module to SWIG
  2634. end-users, but you'll find that they're useful during development and testing
  2635. of your language module as well. You can use examples from the existing SWIG
  2636. language modules for inspiration.
  2637. </p>
  2638. <p>
  2639. Each example is self-contained and consists of (at least) a <tt>Makefile</tt>,
  2640. a SWIG interface file for the example module, and a 'runme' script that demonstrates
  2641. the functionality for that module. All of these files are stored in the same
  2642. subdirectory under the <tt>Examples/[lang]</tt> directory.
  2643. There are two classic examples which should be the first to convert to a new
  2644. language module. These are the "simple" C example and the "class" C++ example.
  2645. These can be found, for example for Python, in
  2646. <tt>Examples/python/simple</tt> and <tt>Examples/python/class</tt>.
  2647. </p>
  2648. <p>
  2649. By default, all of the examples are built and run when the user types
  2650. <tt>make check</tt>. To ensure that your examples are automatically run
  2651. during this process, see the section on <a href="#n37a">configuration
  2652. files</a>.
  2653. </p>
  2654. <H3><a name="Extending_test_suite"></a>35.10.12 Test driven development and the test-suite</H3>
  2655. <p>
  2656. A test driven development approach is central to the improvement and development of SWIG.
  2657. Most modifications to SWIG are accompanied by additional regression tests and checking all
  2658. tests to ensure that no regressions have been introduced.
  2659. </p>
  2660. <p>
  2661. The regression testing is carried out by the SWIG <i>test-suite</i>.
  2662. The test-suite consists of numerous testcase interface files in the <tt>Examples/test-suite</tt> directory
  2663. as well as target language specific runtime tests in the <tt>Examples/test-suite/[lang]</tt> directory.
  2664. When a testcase is run, it will execute the following steps for each testcase:
  2665. </p>
  2666. <ol>
  2667. <li>Execute SWIG passing it the testcase interface file.</li>
  2668. <li>Compile the resulting generated C/C++ code with either the C or C++ compiler into object files.</li>
  2669. <li>Link the object files into a dynamic library (dll/shared object).</li>
  2670. <li>Compile any generated and any runtime test target language code with the target language compiler, if the target language supports compilation. This step thus does not apply to the interpreted languages.</li>
  2671. <li>Execute a runtime test if one exists.</li>
  2672. </ol>
  2673. <p>
  2674. For example, the <i>ret_by_value</i> testcase consists of two components.
  2675. The first component is the <tt>Examples/test-suite/ret_by_value.i</tt> interface file.
  2676. The name of the SWIG module <b>must</b> always be the name of the testcase, so the <tt>ret_by_value.i</tt> interface file thus begins with:
  2677. </p>
  2678. <div class="code">
  2679. <pre>
  2680. %module ret_by_value
  2681. </pre>
  2682. </div>
  2683. <p>
  2684. The testcase code will then follow the module declaration,
  2685. usually within a <tt>%inline %{ ... %}</tt> section for the majority of the tests.
  2686. </p>
  2687. <p>
  2688. The second component is the optional runtime tests.
  2689. Any runtime tests are named using the following convention: <tt>[testcase]_runme.[ext]</tt>,
  2690. where <tt>[testcase]</tt> is the testcase name and <tt>[ext]</tt> is the normal extension for the target language file.
  2691. In this case, the Java and Python target languages implement a runtime test, so their files are respectively,
  2692. <tt>Examples/test-suite/java/ret_by_value_runme.java</tt> and
  2693. <tt>Examples/test-suite/python/ret_by_value_runme.py</tt>.
  2694. </p>
  2695. <p>
  2696. The goal of the test-suite is to test as much as possible in a <b>silent</b> manner.
  2697. This way any SWIG or compiler errors or warnings are easily visible.
  2698. Should there be any warnings, changes must be made to either fix them (preferably) or suppress them.
  2699. Compilation or runtime errors result in a testcase failure and will be immediately visible.
  2700. It is therefore essential that the runtime tests are written in a manner that displays nothing to stdout/stderr on success
  2701. but error/exception out with an error message on stderr on failure.
  2702. </p>
  2703. <H4><a name="Extending_running_test_suite"></a>35.10.12.1 Running the test-suite</H4>
  2704. <p>
  2705. In order for the test-suite to work for a particular target language, the language must be correctly detected
  2706. and configured during the configure stage so that the correct Makefiles are generated.
  2707. Most development occurs on Linux, so usually it is a matter of installing the development packages for the target language
  2708. and simply configuring as outlined <a href="#Extending_starting_out">earlier</a>.
  2709. </p>
  2710. <p>
  2711. If when running the test-suite commands that follow, you get a message that the test was skipped, it indicates that the
  2712. configure stage is missing information in order to compile and run everything for that language.
  2713. </p>
  2714. <p>
  2715. The test-suite can be run in a number of ways.
  2716. The first group of commands are for running multiple testcases in one run and should be executed in the top level directory.
  2717. To run the entire test-suite (can take a long time):
  2718. </p>
  2719. <div class="shell"><pre>
  2720. make -k check-test-suite
  2721. </pre></div>
  2722. <p>
  2723. To run the test-suite just for target language [lang], replace [lang] with one of csharp, java, perl5, python, ruby, tcl etc:
  2724. </p>
  2725. <div class="shell"><pre>
  2726. make check-[lang]-test-suite
  2727. </pre></div>
  2728. <p>
  2729. Note that if a runtime test is available, a message "(with run test)" is displayed when run. For example:
  2730. </p>
  2731. <div class="shell"><pre>
  2732. $ make check-python-test-suite
  2733. checking python test-suite
  2734. checking testcase argcargvtest (with run test) under python
  2735. checking testcase python_autodoc under python
  2736. checking testcase python_append (with run test) under python
  2737. checking testcase callback (with run test) under python
  2738. </pre></div>
  2739. <p>
  2740. The files generated on a previous run can be deleted using the clean targets, either the whole test-suite or for a particular language:
  2741. </p>
  2742. <div class="shell"><pre>
  2743. make clean-test-suite
  2744. make clean-[lang]-test-suite
  2745. </pre></div>
  2746. <p>
  2747. The test-suite can be run in a <i>partialcheck</i> mode where just SWIG is executed, that is, the compile,
  2748. link and running of the testcases is not performed.
  2749. Note that the partialcheck does not require the target language to be correctly configured and detected and unlike the other test-suite make targets, is never skipped. Once again, either all the languages can be executed or just a chosen language:
  2750. </p>
  2751. <div class="shell"><pre>
  2752. make partialcheck-test-suite
  2753. make partialcheck-[lang]-test-suite
  2754. </pre></div>
  2755. <p>
  2756. If your computer has more than one CPU, you are strongly advised to use parallel make to speed up the execution speed.
  2757. This can be done with any of the make targets that execute more than one testcase.
  2758. For example, a dual core processor can efficiently use 2 parallel jobs:
  2759. </p>
  2760. <div class="shell"><pre>
  2761. make -j2 check-test-suite
  2762. make -j2 check-python-test-suite
  2763. make -j2 partialcheck-java-test-suite
  2764. </pre></div>
  2765. <p>
  2766. The second group of commands are for running individual testcases and should be executed in the appropriate
  2767. target language directory, <tt>Examples/test-suite/[lang]</tt>.
  2768. Testcases can contain either C or C++ code and when one is written, a decision must be made as to which of these input
  2769. languages is to be used.
  2770. Replace <tt>[testcase]</tt> in the commands below with the name of the testcase.
  2771. </p>
  2772. <p>
  2773. For a C language testcase, add the testcase under the C_TEST_CASES list in <tt>Examples/test-suite/common.mk</tt> and
  2774. execute individually as:
  2775. </p>
  2776. <div class="shell"><pre>
  2777. make -s [testcase].ctest
  2778. </pre></div>
  2779. <p>
  2780. For a C++ language testcase, add the testcase under the CPP_TEST_CASES list in <tt>Examples/test-suite/common.mk</tt> and
  2781. execute individually as:
  2782. </p>
  2783. <div class="shell"><pre>
  2784. make -s [testcase].cpptest
  2785. </pre></div>
  2786. <p>
  2787. A third category of tests are C++ language testcases testing multiple modules (the %import directive).
  2788. These require more than one shared library (dll/shared object) to be built and so are separated out from the normal C++ testcases.
  2789. Add the testcase under the MULTI_CPP_TEST_CASES list in <tt>Examples/test-suite/common.mk</tt> and
  2790. execute individually as:
  2791. </p>
  2792. <div class="shell"><pre>
  2793. make -s [testcase].multicpptest
  2794. </pre></div>
  2795. <p>
  2796. To delete the generated files, execute:
  2797. </p>
  2798. <div class="shell"><pre>
  2799. make -s [testcase].clean
  2800. </pre></div>
  2801. <p>
  2802. If you would like to see the exact commands being executed, drop the -s option:
  2803. </p>
  2804. <div class="shell"><pre>
  2805. make [testcase].ctest
  2806. make [testcase].cpptest
  2807. make [testcase].multicpptest
  2808. </pre></div>
  2809. <p>
  2810. Some real examples of each:
  2811. </p>
  2812. <div class="shell"><pre>
  2813. make -s ret_by_value.clean
  2814. make -s ret_by_value.ctest
  2815. make -s bools.cpptest
  2816. make -s imports.multicpptest
  2817. </pre></div>
  2818. <p>
  2819. Advanced usage of the test-suite facilitates running tools on some of the five stages.
  2820. The make variables <tt>SWIGTOOL</tt> and <tt>RUNTOOL</tt> are used to specify a tool to respectively, invoke SWIG
  2821. and the execution of the runtime test.
  2822. You are advised to view the <tt>Examples/test-suite/common.mk</tt> file for details but for a short summary,
  2823. the classic usage is to use <a href="http://valgrind.org/">Valgrind</a> for memory checking.
  2824. For example, checking for memory leaks when running the runtime test in the target language interpreter:
  2825. </p>
  2826. <div class="shell"><pre>
  2827. make ret_by_value.ctest RUNTOOL="valgrind --leak-check=full"
  2828. </pre></div>
  2829. <p>
  2830. This will probably make more sense if you look at the output of the above as it will show the exact commands being executed.
  2831. SWIG can be analyzed for bad memory accesses using:
  2832. </p>
  2833. <div class="shell"><pre>
  2834. make ret_by_value.ctest SWIGTOOL="valgrind --tool=memcheck --trace-children=yes"
  2835. </pre></div>
  2836. <H3><a name="Extending_nn43"></a>35.10.13 Documentation</H3>
  2837. <p>
  2838. Don't forget to write end-user documentation for your language module. Currently,
  2839. each language module has a dedicated chapter
  2840. You shouldn't rehash things that are already covered in sufficient
  2841. detail in the <a href="SWIG.html#SWIG">SWIG Basics</a> and <a href="SWIGPlus.html#SWIGPlus">SWIG
  2842. and C++</a> chapters. There is no fixed format for <em>what</em>, exactly, you should
  2843. document about your language module, but you'll obviously want to cover issues that
  2844. are unique to your language.
  2845. </p>
  2846. <p>
  2847. Some topics that you'll want to be sure to address include:
  2848. </p>
  2849. <ul>
  2850. <li> Command line options unique to your language module.
  2851. <li> Non-obvious mappings between C/C++ and target language concepts.
  2852. For example, if your target language provides a single floating
  2853. point type, it should be no big surprise to find that C/C++
  2854. <tt>float</tt> and <tt>double</tt> types are mapped to it. On the other
  2855. hand, if your target language doesn't provide support for "classes"
  2856. or something similar, you'd want to discuss how C++ classes are handled.
  2857. <li> How to compile the SWIG-generated wrapper code into shared libraries
  2858. that can actually be used. For some languages, there are well-defined
  2859. procedures for doing this, but for others it's an ad hoc process.
  2860. Provide as much detail as appropriate, and links to other resources
  2861. if available.
  2862. </ul>
  2863. <H3><a name="Extending_prerequisites"></a>35.10.14 Prerequisites for adding a new language module to the SWIG distribution</H3>
  2864. <p>
  2865. If you wish for a new language module to be distributed with SWIG,
  2866. which we encourage for all popular languages, there are a few requirements.
  2867. While we appreciate that getting all aspects of a new language working
  2868. won't happen at the outset, there are a set of minimum requirements before
  2869. a module can be committed into the SVN repository for distribution with future
  2870. versions of SWIG. The following are really a summary of this whole section with
  2871. details being outlined earlier on.
  2872. </p>
  2873. <ol>
  2874. <li>
  2875. Demonstrate basic C code working by porting the "simple" example including
  2876. a runtime test, see for example <tt>Examples/python/simple</tt>.
  2877. </li>
  2878. <li>
  2879. Demonstrate basic C++ code working by porting the "class" example including
  2880. a runtime test, see for example <tt>Examples/python/class</tt>.
  2881. </li>
  2882. <li>
  2883. Modify <tt>configure.in</tt>, <tt>Makefile.in</tt> and <tt>Examples/Makefile.in</tt> to run
  2884. these examples. Please make sure that if the new language is not
  2885. installed properly on a box, <tt>make -k check</tt> should still work by
  2886. skipping the tests and examples for the new language module.
  2887. </li>
  2888. <li>
  2889. Get the test-suite running for the new language (<tt>make check-[lang]-test-suite</tt>).
  2890. While the test-suite tests many corner cases,
  2891. we'd expect the majority of it to work by compiling the generated code
  2892. correctly as most of the corner cases are covered in the SWIG core. Get
  2893. at least one C and one C++ runtime test running in the test-suite.
  2894. </li>
  2895. <li>
  2896. Provide a chapter in the html documentation on the basics of using
  2897. the language module.
  2898. </li>
  2899. <li>
  2900. Ensure your source code is formatted according to the <a href="#Extending_coding_style_guidelines">coding style guidelines</a>.
  2901. </li>
  2902. <li>
  2903. Finally, email the SWIG developers with a patch and a demonstration of
  2904. commitment to maintaining the language module,
  2905. certainly in the short term and ideally long term.
  2906. </li>
  2907. </ol>
  2908. <p>
  2909. Once accepted into SVN, development efforts should concentrate on
  2910. getting the entire test-suite to work with plenty of runtime tests.
  2911. Runtime tests should be for existing testcases and new test cases
  2912. should be added should there be an area not already covered by
  2913. the existing tests.
  2914. </p>
  2915. <H3><a name="Extending_coding_style_guidelines"></a>35.10.15 Coding style guidelines</H3>
  2916. <p>
  2917. The coding guidelines for the C/C++ source code are pretty much K&amp;R C style.
  2918. The style can be inferred from the existing code base and is
  2919. largely dictated by the <tt>indent</tt> code beautifier tool set to K&amp;R style.
  2920. The code can formatted using the make targets in the Source directory.
  2921. Below is an example of how to format the emit.cxx file:
  2922. </p>
  2923. <blockquote>
  2924. <pre>
  2925. $ cd Source
  2926. $ make beautify-file INDENTFILE=Modules/emit.cxx
  2927. </pre>
  2928. </blockquote>
  2929. <p>
  2930. Of particular note is indentation is set to 2 spaces and a tab is used instead of 8 spaces.
  2931. The generated C/C++ code should also follow this style as close as possible. However, tabs
  2932. should be avoided as unlike the SWIG developers, users will never have consistent tab settings.
  2933. </p>
  2934. <H2><a name="Extending_debugging_options"></a>35.11 Debugging Options</H2>
  2935. <p>
  2936. There are various command line options which can aid debugging a SWIG interface as well as debugging the development of a language module. These are as follows:
  2937. </p>
  2938. <div class="shell"><pre>
  2939. -debug-classes - Display information about the classes found in the interface
  2940. -debug-module &lt;n&gt; - Display module parse tree at stages 1-4, &lt;n&gt; is a csv list of stages
  2941. -debug-symtabs - Display symbol tables information
  2942. -debug-symbols - Display target language symbols in the symbol tables
  2943. -debug-csymbols - Display C symbols in the symbol tables
  2944. -debug-tags - Display information about the tags found in the interface
  2945. -debug-template - Display information for debugging templates
  2946. -debug-top &lt;n&gt; - Display entire parse tree at stages 1-4, &lt;n&gt; is a csv list of stages
  2947. -debug-typedef - Display information about the types and typedefs in the interface
  2948. -debug-typemap - Display information for debugging typemaps
  2949. </pre></div>
  2950. <p>
  2951. The complete list of command line options for SWIG are available by running <tt>swig -help</tt>.
  2952. </p>
  2953. <H2><a name="Extending_nn46"></a>35.12 Guide to parse tree nodes</H2>
  2954. <p>
  2955. This section describes the different parse tree nodes and their attributes.
  2956. </p>
  2957. <p>
  2958. <b>cdecl</b>
  2959. </p>
  2960. <p>
  2961. Describes general C declarations including variables, functions, and typedefs.
  2962. A declaration is parsed as "storage T D" where storage is a storage class, T is a base type,
  2963. and D is a declarator.
  2964. </p>
  2965. <div class="diagram">
  2966. <pre>
  2967. "name" - Declarator name
  2968. "type" - Base type T
  2969. "decl" - Declarator type (abstract)
  2970. "storage" - Storage class (static, extern, typedef, etc.)
  2971. "parms" - Function parameters (if a function)
  2972. "code" - Function body code (if supplied)
  2973. "value" - Default value (if supplied)
  2974. </pre>
  2975. </div>
  2976. <p>
  2977. <b>constructor</b>
  2978. </p>
  2979. <p>
  2980. C++ constructor declaration.
  2981. </p>
  2982. <div class="diagram">
  2983. <pre>
  2984. "name" - Name of constructor
  2985. "parms" - Parameters
  2986. "decl" - Declarator (function with parameters)
  2987. "code" - Function body code (if any)
  2988. "feature:new" - Set to indicate return of new object.
  2989. </pre>
  2990. </div>
  2991. <p>
  2992. <b>destructor</b>
  2993. </p>
  2994. <p>
  2995. C++ destructor declaration.
  2996. </p>
  2997. <div class="diagram">
  2998. <pre>
  2999. "name" - Name of destructor
  3000. "code" - Function body code (if any)
  3001. "storage" - Storage class (set if virtual)
  3002. "value" - Default value (set if pure virtual).
  3003. </pre>
  3004. </div>
  3005. <p>
  3006. <b>access</b>
  3007. </p>
  3008. <p>
  3009. C++ access change.
  3010. </p>
  3011. <div class="diagram">
  3012. <pre>
  3013. "kind" - public, protected, private
  3014. </pre>
  3015. </div>
  3016. <p>
  3017. <b>constant</b>
  3018. </p>
  3019. <p>
  3020. Constant created by %constant or #define.
  3021. </p>
  3022. <div class="diagram">
  3023. <pre>
  3024. "name" - Name of constant.
  3025. "type" - Base type.
  3026. "value" - Value.
  3027. "storage" - Set to %constant
  3028. "feature:immutable" - Set to indicate read-only
  3029. </pre>
  3030. </div>
  3031. <p>
  3032. <b>class</b>
  3033. </p>
  3034. <p>
  3035. C++ class definition or C structure definition.
  3036. </p>
  3037. <div class="diagram">
  3038. <pre>
  3039. "name" - Name of the class.
  3040. "kind" - Class kind ("struct", "union", "class")
  3041. "symtab" - Enclosing symbol table.
  3042. "tdname" - Typedef name. Use for typedef struct { ... } A.
  3043. "abstract" - Set if class has pure virtual methods.
  3044. "baselist" - List of base class names.
  3045. "storage" - Storage class (if any)
  3046. "unnamed" - Set if class is unnamed.
  3047. </pre>
  3048. </div>
  3049. <p>
  3050. <b>enum</b>
  3051. </p>
  3052. <p>
  3053. Enumeration.
  3054. </p>
  3055. <div class="diagram">
  3056. <pre>
  3057. "name" - Name of the enum (if supplied).
  3058. "storage" - Storage class (if any)
  3059. "tdname" - Typedef name (typedef enum { ... } name).
  3060. "unnamed" - Set if enum is unnamed.
  3061. </pre>
  3062. </div>
  3063. <p>
  3064. <b>enumitem</b>
  3065. </p>
  3066. <p>
  3067. Enumeration value.
  3068. </p>
  3069. <div class="diagram">
  3070. <pre>
  3071. "name" - Name of the enum value.
  3072. "type" - Type (integer or char)
  3073. "value" - Enum value (if given)
  3074. "feature:immutable" - Set to indicate read-only
  3075. </pre>
  3076. </div>
  3077. <p>
  3078. <b>namespace</b>
  3079. </p>
  3080. <p>
  3081. C++ namespace.
  3082. </p>
  3083. <div class="diagram">
  3084. <pre>
  3085. "name" - Name of the namespace.
  3086. "symtab" - Symbol table for enclosed scope.
  3087. "unnamed" - Set if unnamed namespace
  3088. "alias" - Alias name. Set for namespace A = B;
  3089. </pre>
  3090. </div>
  3091. <p>
  3092. <b>using</b>
  3093. </p>
  3094. <p>
  3095. C++ using directive.
  3096. </p>
  3097. <div class="diagram">
  3098. <pre>
  3099. "name" - Name of the object being referred to.
  3100. "uname" - Qualified name actually given to using.
  3101. "node" - Node being referenced.
  3102. "namespace" - Namespace name being reference (using namespace name)
  3103. </pre>
  3104. </div>
  3105. <p>
  3106. <b>classforward</b>
  3107. </p>
  3108. <p>
  3109. A forward C++ class declaration.
  3110. </p>
  3111. <div class="diagram">
  3112. <pre>
  3113. "name" - Name of the class.
  3114. "kind" - Class kind ("union", "struct", "class")
  3115. </pre>
  3116. </div>
  3117. <p>
  3118. <b>insert</b>
  3119. </p>
  3120. <p>
  3121. Code insertion directive. For example, %{ ... %} or
  3122. %insert(section).
  3123. </p>
  3124. <div class="diagram">
  3125. <pre>
  3126. "code" - Inserted code
  3127. "section" - Section name ("header", "wrapper", etc.)
  3128. </pre>
  3129. </div>
  3130. <p>
  3131. <b>top</b>
  3132. </p>
  3133. <p>
  3134. Top of the parse tree.
  3135. </p>
  3136. <div class="diagram">
  3137. <pre>
  3138. "module" - Module name
  3139. </pre>
  3140. </div>
  3141. <p>
  3142. <b>extend</b>
  3143. </p>
  3144. <p>
  3145. %extend directive.
  3146. </p>
  3147. <div class="diagram">
  3148. <pre>
  3149. "name" - Module name
  3150. "symtab" - Symbol table of enclosed scope.
  3151. </pre>
  3152. </div>
  3153. <p>
  3154. <b>apply</b>
  3155. </p>
  3156. <p>
  3157. %apply pattern { patternlist }.
  3158. </p>
  3159. <div class="diagram">
  3160. <pre>
  3161. "pattern" - Source pattern.
  3162. "symtab" - Symbol table of enclosed scope.
  3163. </pre>
  3164. </div>
  3165. <p>
  3166. <b>clear</b>
  3167. </p>
  3168. <p>
  3169. %clear patternlist;
  3170. </p>
  3171. <div class="diagram">
  3172. <pre>
  3173. "firstChild" - Patterns to clear
  3174. </pre>
  3175. </div>
  3176. <p>
  3177. <b>include</b>
  3178. </p>
  3179. <p>
  3180. %include directive.
  3181. </p>
  3182. <div class="diagram">
  3183. <pre>
  3184. "name" - Filename
  3185. "firstChild" - Children
  3186. </pre>
  3187. </div>
  3188. <p>
  3189. <b>import</b>
  3190. </p>
  3191. <p>
  3192. %import directive.
  3193. </p>
  3194. <div class="diagram">
  3195. <pre>
  3196. "name" - Filename
  3197. "firstChild" - Children
  3198. </pre>
  3199. </div>
  3200. <p>
  3201. <b>module</b>
  3202. </p>
  3203. <p>
  3204. %module directive.
  3205. </p>
  3206. <div class="diagram">
  3207. <pre>
  3208. "name" - Name of the module
  3209. </pre>
  3210. </div>
  3211. <p>
  3212. <b>typemap</b>
  3213. </p>
  3214. <p>
  3215. %typemap directive.
  3216. </p>
  3217. <div class="diagram">
  3218. <pre>
  3219. "method" - Typemap method name.
  3220. "code" - Typemap code.
  3221. "kwargs" - Keyword arguments (if any)
  3222. "firstChild" - Typemap patterns
  3223. </pre>
  3224. </div>
  3225. <p>
  3226. <b>typemapcopy</b>
  3227. </p>
  3228. <p>
  3229. %typemap directive with copy.
  3230. </p>
  3231. <div class="diagram">
  3232. <pre>
  3233. "method" - Typemap method name.
  3234. "pattern" - Typemap source pattern.
  3235. "firstChild" - Typemap patterns
  3236. </pre>
  3237. </div>
  3238. <p>
  3239. <b>typemapitem</b>
  3240. </p>
  3241. <p>
  3242. %typemap pattern. Used with %apply, %clear, %typemap.
  3243. </p>
  3244. <div class="diagram">
  3245. <pre>
  3246. "pattern" - Typemap pattern (a parameter list)
  3247. "parms" - Typemap parameters.
  3248. </pre>
  3249. </div>
  3250. <p>
  3251. <b>types</b>
  3252. </p>
  3253. <p>
  3254. %types directive.
  3255. </p>
  3256. <div class="diagram">
  3257. <pre>
  3258. "parms" - List of parameter types.
  3259. "convcode" - Code which replaces the default casting / conversion code
  3260. </pre>
  3261. </div>
  3262. <p>
  3263. <b>extern</b>
  3264. </p>
  3265. <p>
  3266. extern "X" { ... } declaration.
  3267. </p>
  3268. <div class="diagram">
  3269. <pre>
  3270. "name" - Name "C", "Fortran", etc.
  3271. </pre>
  3272. </div>
  3273. <H2><a name="Extending_further_info"></a>35.13 Further Development Information</H2>
  3274. <p>
  3275. There is further documentation available on the internals of SWIG, API documentation and debugging information.
  3276. This is shipped with SWIG in the <tt>Doc/Devel</tt> directory.
  3277. </P>
  3278. </body>
  3279. </html>