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