PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Doc/Devel/engineering.html

#
HTML | 479 lines | 387 code | 92 blank | 0 comment | 0 complexity | 0a573f9532bc8dc3bf85f30bd8d063d9 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <html>
  2. <head>
  3. <title>SWIG Engineering Manual</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <center>
  7. <h1>SWIG Engineering Manual</h1>
  8. <b>David Beazley <br>
  9. Department of Computer Science <br>
  10. University of Chicago <br>
  11. Chicago, IL 60637 <br>
  12. beazley@cs.uchicago.edu <br>
  13. </b>
  14. </center>
  15. <p>
  16. <b>$Header$</b>
  17. <p>
  18. (Note : This is a work in progress.)
  19. <h2>Table of Contents</h2>
  20. <ul>
  21. <li><a name="i1" href="#1">1. Introduction</a>
  22. <li><a name="i2" href="#2">2. Programming Languages and Libraries</a>
  23. <li><a name="i3" href="#3">3. The Source Directory and Module Names</a>
  24. <li><a name="i4" href="#4">4. Include Files</a>
  25. <li><a name="i5" href="#5">5. File Structure</a>
  26. <li><a name="i6" href="#6">6. Bottom-Up Design</a>
  27. <li><a name="i7" href="#7">7. Functions</a>
  28. <li><a name="i8" href="#8">8. Naming Conventions</a>
  29. <li><a name="i9" href="#9">9. Visibility</a>
  30. <li><a name="i10" href="#10">10. Miscellaneous Coding Guidelines</a>
  31. <li><a name="i11" href="#11">11. CVS Tagging Conventions</a>
  32. </ul>
  33. <a name="1" href="#i1">
  34. <h2>1. Introduction</h2>
  35. </a>
  36. The purpose of this document is to describe various coding conventions
  37. and organizational aspects for SWIG developers. The idea for this
  38. document is largely borrowed from John Ousterhout's Tcl/Tk Engineering
  39. Manual. It is not my intent to overly managerial about matters--rather I'm
  40. hoping to make life a little less chaotic for everyone.
  41. <p>
  42. First a little background: SWIG was started in 1995 as a one-person
  43. project and continued in this mode of operation until about 1998.
  44. Most of this development was driven by ideas submitted by early SWIG
  45. users as opposed to being motivated by a grand design. As a result,
  46. the code ended up being a pretty horrible C++ coding disaster. A
  47. mostly working disaster perhaps, but a disaster nonetheless.
  48. <p>
  49. With that said, the primary goal of future SWIG development is to
  50. reengineer the original system, fix most of its inherent design flaws,
  51. and to produce what I hope will become a highly extensible and modular
  52. interface compiler framework. To this do this, there are a few
  53. critical areas of work. First, I want to restructure SWIG as a
  54. collection of loosely coupled modules written in either ANSI C or an
  55. scripting language. Second, I want the system to be minimalistic in
  56. its use of data structures and interconnections. The primary reason
  57. for this is that the fewer data structures there are, the less users
  58. will have to remember. This will also make the system more accessible
  59. to non-experts. Finally, I want to reevaluate the whole idea of a
  60. SWIG module is and expand the definition to include just about
  61. anything from parsers, preprocessors, optimizers, interface editors,
  62. and code generators.
  63. <p>
  64. The rest of this document outlines a few general rules of how code
  65. should be developed within the SWIG project. These rules are
  66. primarily drawn from my own experience developing software and
  67. observing the practices of other successful projects.
  68. <a name="2" href="#i2">
  69. <h2>2. Programming Languages and Libraries </h2>
  70. </a>
  71. All SWIG modules must be written in either ANSI C or one of the
  72. scripting languages for which SWIG can generate an interface (e.g.,
  73. Perl, Python, or Tcl). C++ is currently being used to write
  74. SWIG modules, but it is only being utilized to avoid working with
  75. a lot of pointers to functions. <b>Advanced C++ features like namespaces, templates,
  76. and overloading should not be used.</b>.
  77. <p>
  78. Module writers should make every attempt to use only those functions
  79. described in the POSIX.1 standard. This includes most of the
  80. functions contained the Kernighan and Ritchie C programming book. Use
  81. of operating system dependent functionality such as socket libraries
  82. should always be included inside a conditional compilation block so
  83. that it can be omitted on problematic platforms. If you are unsure
  84. about a library call, check the man page or contact Dave.
  85. <a name="3" href="#i3">
  86. <h2>3. The Source Directory and Module Names</h2>
  87. </a>
  88. All SWIG modules are contained within the "Source" directory. Within
  89. this directory, each module is placed into its own subdirectory. The
  90. name of this subdirectory should exactly match the name of the module.
  91. For example, if you are creating a module called "Tcl", all of your
  92. files should be placed in a directory "Tcl".
  93. <p>
  94. When choosing a module name, please pick a name that is not
  95. currently in use. As a general convention, the first letter of a
  96. module name is capitalized such as "Perl". Alternatives such as
  97. "perl" or "PERL" should be avoided. In certain instances, the first
  98. two letters may be capitalized as in "CParse." The exact usage of
  99. this is somewhat inconsistent and isn't terribly important--just make
  100. sure the first letter is capitalized. Also, module names should not
  101. start with numbers, include underscores or any other special
  102. non-alphanumeric characters.
  103. <a name="4" href="#i4">
  104. <h2>4. Include Files </h2>
  105. </a>
  106. All modules should include a header file that defines the public interface.
  107. The name of this header file should be of the form "swigmodule.h" where
  108. "module" is the name of your module. For example, if you created a
  109. module "Perl", the header file should be named "swigperl.h". This scheme
  110. should prevent header-file naming conflicts both within SWIG and when linking
  111. parts of SWIG to the outside world.
  112. <p>
  113. All header files should include a short description, author information, copyright message,
  114. CVS version, include guards, and be C++ aware. For example:
  115. <blockquote>
  116. <pre>
  117. /* -------------------------------------------------------------------------
  118. * swigperl.h
  119. *
  120. * All of the externally visible functions in the Perl module.
  121. *
  122. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  123. *
  124. * Copyright (C) 1999-2000, The University of Chicago.
  125. * See the file LICENSE for information on usage and redistribution.
  126. *
  127. * $Header$
  128. * ------------------------------------------------------------------------- */
  129. #ifndef _SWIGPERL_H
  130. #define _SWIGPERL_H 1
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134. /* Your declarations here */
  135. ...
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139. #endif /* _SWIGPERL_H */
  140. </pre>
  141. </blockquote>
  142. <p>
  143. To minimize compilation time, please include as few other header files as possible.
  144. <a name="5" href="#i5">
  145. <h2>5. File Structure </h2>
  146. </a>
  147. Each file in a module should be given a filename that is all lowercase letters
  148. such as "parser.c", not "Parser.c" or "PARSER.c". Please note that filenames
  149. are case-insensitive on Windows so this convention will prevent you from inadvertantly
  150. creating two files that differ in case-only.
  151. <p>
  152. Each file should include a short abstract, author information, copyright information, and
  153. a CVS revision tag like this:
  154. <blockquote>
  155. <pre>
  156. /* -----------------------------------------------------------------------------
  157. * include.c
  158. *
  159. * This file implements the functions used to locate and include files in
  160. * the SWIG library. Functions for maintaining the library search path are
  161. * also located here.
  162. *
  163. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  164. *
  165. * Copyright (C) 1999-2000, The University of Chicago.
  166. * See the file LICENSE for information on usage and redistribution.
  167. * ----------------------------------------------------------------------------- */
  168. static char cvsroot[] = "$Header$";
  169. #include "swig.h"
  170. /* Declarations */
  171. typedef struct {
  172. int x, y;
  173. } Foo;
  174. ...
  175. /* Private Declarations (used only in this file) */
  176. static int avariable;
  177. ...
  178. /* Functions */
  179. ...
  180. </pre>
  181. </blockquote>
  182. The CVS revision tag should be placed into a static string as shown
  183. above. This adds the revision information to the SWIG executable and
  184. makes it possible to extract version information from a raw binary
  185. (sometimes useful in debugging).
  186. <p>
  187. As a general rule, files start to get unmanagable once they exceed
  188. about 2000 lines. Files larger than this should be broken up into
  189. multiple files. Similarly, you should avoid the temptation to create
  190. many small files as this increases compilation time and makes the
  191. directory structure too complicated.
  192. <a name="6" href="#i6">
  193. <h2>6. Bottom-Up Design </h2>
  194. </a>
  195. Within each source file, the preferred organization is to use what is
  196. known as "bottom-up" design. Under this scheme, lower-level functions
  197. appear first and the highest level function appears last. The easy
  198. way to remember is that the "main" function of your module should
  199. always appear last in the source file. For example:
  200. <blockquote>
  201. <pre>
  202. /* Simple bottom-up program */
  203. #include &lt;stdio.h&gt;
  204. int foo(int x, int y) {
  205. /* Implement foo */
  206. ...
  207. }
  208. int bar() {
  209. ...
  210. foo(i,j);
  211. ...
  212. }
  213. ...
  214. int main(int argc, char **argv) {
  215. ...
  216. bar();
  217. ...
  218. }
  219. </pre>
  220. </blockquote>
  221. This choice of design is somewhat arbitrary however it has a number of
  222. benefits particular to C. In particular, a bottom-up design generally
  223. eliminates the need to include forward references--resulting in
  224. cleaner code and fewer compilation errors.
  225. <a name="7" href="#i7">
  226. <h2>7. Functions</h2>
  227. </a>
  228. All functions should have a function header that gives the function name
  229. and a short description like this:
  230. <blockquote>
  231. <pre>
  232. /* -------------------------------------------------------------------------
  233. * Swig_add_directory()
  234. *
  235. * Adds a directory to the SWIG search path.
  236. * ------------------------------------------------------------------------- */
  237. void
  238. Swig_add_directory(DOH *dirname) {
  239. ...
  240. }
  241. </pre>
  242. </blockquote>
  243. In the function declaration, the return type and any specifiers
  244. (extern or static) should appear on a separate line followed by the
  245. function name and arguments as shown above. The left curly brace
  246. should appear on the same line as the function name.
  247. <p>
  248. Function declarations should <b>NOT</b> use the pre-ANSI function
  249. declaration syntax. The ANSI standard has been around long enough for
  250. this to be a non-issue.
  251. <a name="8" href="#i8">
  252. <h2>8. Naming Conventions</h2>
  253. </a>
  254. The following conventions are used to name various objects throughout SWIG.
  255. <h4>Functions</h4>
  256. Functions should consist of the module name and the function name separated by an underscore like this:
  257. <blockquote>
  258. <pre>
  259. Preprocessor_define()
  260. Swig_add_directory()
  261. </pre>
  262. </blockquote>
  263. In general, the module name should match the name of the module
  264. subdirectory and the function name should be in all lowercase with
  265. words separated by underscores.
  266. <h4>Structures and Types</h4>
  267. If your module defines new structures, the structure name should include the name of the
  268. module and the name of the structure appended together like this:
  269. <blockquote>
  270. <pre>
  271. typedef struct SwigScanner {
  272. ...
  273. } SwigScanner;
  274. typedef struct LParseType {
  275. ...
  276. } LParseType;
  277. </pre>
  278. </blockquote>
  279. In this case, both the name of the module and the type should be capitalized. Also, whenever
  280. possible, you should use the "typedef struct Name { ... } Name" form when defining new
  281. data structures.
  282. <h4>Global Variables</h4>
  283. Global variables should be avoided if at all possible. However, if you must use a global
  284. variable, please prepend the module name and use the same naming scheme as for functions.
  285. <h4>Constants</h4>
  286. Constants should be created using #define and should be in all caps like this:
  287. <blockquote>
  288. <pre>
  289. #define SWIG_TOKEN_LPAREN 1
  290. </pre>
  291. </blockquote>
  292. Separate words in a constant should be separated by underscores as with functions.
  293. <h4>Structure members</h4>
  294. Structure members should be in all lower-case and follow the same word-separation convention
  295. as for function names. However, the module name does not have to be included.
  296. For example:
  297. <blockquote>
  298. <pre>
  299. typedef struct SwigScanner {
  300. DOH *text; /* Current token value */
  301. DOH *scanobjs; /* Objects being scanned */
  302. DOH *str; /* Current object being scanned */
  303. char *idstart; /* Optional identifier start characters */
  304. int next_token; /* Next token to be returned */
  305. int start_line; /* Starting line of certain declarations */
  306. int yylen; /* Length of text pushed into text */
  307. DOH *file; /* Current file name */
  308. } SwigScanner;
  309. </pre>
  310. </blockquote>
  311. <h4>Static Functions and Variables </h4>
  312. Static declarations are free to use any naming convention that is appropriate. However, most
  313. existing parts of SWIG use lower-case names and follow the same convention as described for functions.
  314. <a name="9" href="#i9">
  315. <h2>9. Visibility</h2>
  316. </a>
  317. Modules should keep the following rules in mind when exposing their internals:
  318. <ul>
  319. <li>Only publicly accessible functions should be included in the module header file.
  320. <li>All non-static declarations must be prepended with some form of the module name
  321. to avoid potential linker namespace conflicts with other modules.
  322. <li>Modules should not expose global variables or use global variables in their
  323. public interface.
  324. <li>Similarly, modules should discourage the direct manipulation of data contained
  325. within data structures in favor of using function calls instead. For example,
  326. instead of providing a user with a structure like this:
  327. <blockquote>
  328. <pre>
  329. typedef struct Foo {
  330. int line;
  331. } Foo;
  332. </pre>
  333. </blockquote>
  334. It is better to hide the implementation of Foo and provide an
  335. function-call interface like this:
  336. <blockquote>
  337. <pre>
  338. typedef struct Foo Foo;
  339. extern int Foo_getline(Foo *f);
  340. extern void Foo_setline(Foo *f, int line);
  341. </pre>
  342. </blockquote>
  343. Although this results in worse performance, there are many practical
  344. reasons for doing this. The most important reason is that it allows
  345. you to change the internal representation of Foo without breaking all
  346. of the other modules or having to recompile the entire universe after
  347. making your changes.
  348. </ul>
  349. <a name="10" href="#i10">
  350. <h2>10. Miscellaneous Coding Guidelines</h2>
  351. </a>
  352. <ul>
  353. <li> Do not use the ternary ?: operator. It is unnecessarily error prone,
  354. hard for people to read, and hard to maintain code that uses it.
  355. [I don't agree w/ this guideline. ?: operator can be abused
  356. just like everything else, but it can also be used cleanly. In some styles of
  357. programming, it is the best tool for the job. --ttn]
  358. </ul>
  359. <a name="11" href="#i11">
  360. <h2>11. CVS Tagging Conventions</h2>
  361. </a>
  362. Use <tt>cvs tag</tt> to declare some set of file revisions as related in some
  363. symbolic way. This eases reference, retrieval and manipulation of these files
  364. later. At the moment (2001/01/16 14:02:53), the conventions are very simple;
  365. let's hope they stay that way!
  366. <p>
  367. There are two types of tags, internal (aka personal) and external.
  368. Internal tags are used by SWIG developers primarily, whereas external
  369. tags are used when communicating with people w/ anonymous cvs access.
  370. <ul>
  371. <li> Internal tags should start with the developer name and a hyphen.
  372. <li> External tags should start with "v-".
  373. </ul>
  374. That's all there is to it. Some example tags:
  375. <ul>
  376. <li> ttn-pre-xml-patch
  377. <li> ttn-post-xml-patch
  378. <li> ttn-going-on-vacation-so-dutifully-tagging-now
  379. <li> v-1-3-a37-fixes-bug-2432
  380. <li> v-1-3-a37-fixes-bug-2433
  381. <li> v-1-3-a37-fixes-bug-2432-again
  382. <li> v-1-3-a37-release
  383. </ul>
  384. <hr>
  385. Copyright (C) 1999-2004 SWIG Development Team.
  386. </body>
  387. </html>