import source from 1.3.40
[external/swig.git] / Source / CParse / util.c
1 /* ----------------------------------------------------------------------------- 
2  * See the LICENSE file for information on copyright, usage and redistribution
3  * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4  *
5  * util.c
6  *
7  * Parsing utilities.
8  * ----------------------------------------------------------------------------- */
9
10 char cvsroot_util_c[] = "$Id: util.c 9632 2007-01-03 20:58:19Z beazley $";
11
12 #include "swig.h"
13 #include "cparse.h"
14
15 /* -----------------------------------------------------------------------------
16  * Swig_cparse_replace_descriptor()
17  *
18  * Replaces type descriptor string $descriptor() with the SWIG type descriptor
19  * string.
20  * ----------------------------------------------------------------------------- */
21
22 void Swig_cparse_replace_descriptor(String *s) {
23   char tmp[512];
24   String *arg = 0;
25   SwigType *t;
26   char *c = 0;
27
28   while ((c = strstr(Char(s), "$descriptor("))) {
29     char *d = tmp;
30     int level = 0;
31     while (*c) {
32       if (*c == '(')
33         level++;
34       if (*c == ')') {
35         level--;
36         if (level == 0) {
37           break;
38         }
39       }
40       *d = *c;
41       d++;
42       c++;
43     }
44     *d = 0;
45     arg = NewString(tmp + 12);
46     t = Swig_cparse_type(arg);
47     Delete(arg);
48     arg = 0;
49
50     if (t) {
51       String *mangle;
52       String *descriptor;
53
54       mangle = SwigType_manglestr(t);
55       descriptor = NewStringf("SWIGTYPE%s", mangle);
56       SwigType_remember(t);
57       *d = ')';
58       d++;
59       *d = 0;
60       Replace(s, tmp, descriptor, DOH_REPLACE_ANY);
61       Delete(mangle);
62       Delete(descriptor);
63       Delete(t);
64     } else {
65       Swig_error(Getfile(s), Getline(s), "Bad $descriptor() macro.\n");
66       break;
67     }
68   }
69 }
70
71 /* -----------------------------------------------------------------------------
72  * cparse_normalize_void()
73  *
74  * This function is used to replace arguments of the form (void) with empty
75  * arguments in C++
76  * ----------------------------------------------------------------------------- */
77
78 void cparse_normalize_void(Node *n) {
79   String *decl = Getattr(n, "decl");
80   Parm *parms = Getattr(n, "parms");
81
82   if (SwigType_isfunction(decl)) {
83     if ((ParmList_len(parms) == 1) && (SwigType_type(Getattr(parms, "type")) == T_VOID)) {
84       Replaceall(decl, "f(void).", "f().");
85       Delattr(n, "parms");
86     }
87   }
88 }