]>
Commit | Line | Data |
---|---|---|
c90f71dd RD |
1 | /******************************************************************************* |
2 | * Simplified Wrapper and Interface Generator (SWIG) | |
3 | * | |
4 | * Author : David Beazley | |
5 | * | |
6 | * Department of Computer Science | |
7 | * University of Chicago | |
8 | * 1100 E 58th Street | |
9 | * Chicago, IL 60637 | |
10 | * beazley@cs.uchicago.edu | |
11 | * | |
12 | * Please read the file LICENSE for the copyright and terms by which SWIG | |
13 | * can be used and distributed. | |
14 | *******************************************************************************/ | |
15 | ||
16 | // ------------------------------------------------------------------ | |
17 | // wrapfunc.cxx | |
18 | // | |
19 | // Created : June 22, 1996 | |
20 | // Dave Beazley | |
21 | // | |
22 | // This file defines a class for writing wrappers. Instead of worrying | |
23 | // about I/O problems, this wrapper class can be used to write functions | |
24 | // out of order. | |
25 | // | |
26 | // Defines 3 string objects. | |
27 | // def - Wrapper function definition (function name and arguments) | |
28 | // locals - Local variable definitions | |
29 | // code - The actual wrapper function code | |
30 | // | |
31 | //------------------------------------------------------------------- | |
32 | ||
33 | #include "internal.h" | |
34 | #include <ctype.h> | |
35 | ||
36 | // ------------------------------------------------------------------- | |
37 | // Print out a wrapper function. | |
38 | // | |
39 | // ------------------------------------------------------------------- | |
40 | ||
41 | void WrapperFunction::print(FILE *f) { | |
42 | fprintf(f,"%s\n",def.get()); | |
43 | fprintf(f,"%s\n",locals.get()); | |
44 | fprintf(f,"%s\n",code.get()); | |
45 | } | |
46 | ||
47 | // ------------------------------------------------------------------- | |
48 | // Print out a wrapper function. | |
49 | // | |
50 | // ------------------------------------------------------------------- | |
51 | ||
52 | void WrapperFunction::print(String &f) { | |
53 | f << def << "\n" | |
54 | << locals << "\n" | |
55 | << code << "\n"; | |
56 | } | |
57 | ||
58 | // ------------------------------------------------------------------- | |
59 | // Safely add a local variable. | |
60 | // | |
61 | // Maintains a hash table to prevent double adding. | |
62 | // ------------------------------------------------------------------- | |
63 | ||
64 | void WrapperFunction::add_local(char *type, char *name, char *defarg) { | |
65 | char *stored_type; | |
66 | char *new_type; | |
67 | char temp[256],*c,*t; | |
68 | ||
69 | new_type = new char[strlen(type)+1]; | |
70 | strcpy(new_type,type); | |
71 | ||
72 | // Figure out what the name of this variable is | |
73 | ||
74 | c = name; | |
75 | t = temp; | |
76 | while ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)) { | |
77 | *(t++) = *c; | |
78 | c++; | |
79 | } | |
80 | *t = 0; | |
81 | if (h.add(temp,new_type,WrapperFunction::del_type) == -1) { | |
82 | // Check to see if a type mismatch has occurred | |
83 | stored_type = (char *) h.lookup(temp); | |
84 | if (strcmp(type,stored_type) != 0) | |
85 | fprintf(stderr,"Error. Type %s conflicts with previously declared type of %s\n", | |
86 | type, stored_type); | |
87 | return; | |
88 | } | |
89 | ||
90 | // Successful, write some wrapper code | |
91 | ||
92 | if (!defarg) | |
93 | locals << tab4 << type << " " << name << ";\n"; | |
94 | else | |
95 | locals << tab4 << type << " " << name << " = " << defarg << ";\n"; | |
96 | } | |
97 | ||
98 | ||
99 | // ------------------------------------------------------------------- | |
100 | // char *WrapperFunction::new_local(char *type, char *name, char *defarg) { | |
101 | // | |
102 | // A safe way to add a new local variable. type and name are used as | |
103 | // a starting point, but a new local variable will be created if these | |
104 | // are already in use. | |
105 | // ------------------------------------------------------------------- | |
106 | ||
107 | char *WrapperFunction::new_local(char *type, char *name, char *defarg) { | |
108 | char *new_type; | |
109 | static String new_name; | |
110 | char *c; | |
111 | new_type = new char[strlen(type)+1]; | |
112 | ||
113 | strcpy(new_type,type); | |
114 | new_name = ""; | |
115 | c = name; | |
116 | for (c = name; ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)); c++) | |
117 | new_name << *c; | |
118 | ||
119 | // Try to add a new local variable | |
120 | if (h.add(new_name,new_type,WrapperFunction::del_type) == -1) { | |
121 | // Local variable already exists, try to generate a new name | |
122 | int i = 0; | |
123 | new_name = ""; | |
124 | // This is a little funky. We copy characters until we reach a nonvalid | |
125 | // identifier symbol, add a number, then append the rest. This is | |
126 | // needed to properly handle arrays. | |
127 | c = name; | |
128 | for (c = name; ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)); c++) | |
129 | new_name << *c; | |
130 | new_name << i; | |
131 | while (h.add(new_name,new_type,WrapperFunction::del_type) == -1) { | |
132 | i++; | |
133 | c = name; | |
134 | new_name = ""; | |
135 | for (c = name; ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)); c++) | |
136 | new_name << *c; | |
137 | new_name << i; | |
138 | } | |
139 | } | |
140 | new_name << c; | |
141 | // Successful, write some wrapper code | |
142 | if (!defarg) | |
143 | locals << tab4 << type << " " << new_name << ";\n"; | |
144 | else | |
145 | locals << tab4 << type << " " << new_name << " = " << defarg << ";\n"; | |
146 | ||
147 | // Need to strip off the array symbols now | |
148 | ||
149 | c = new_name.get(); | |
150 | while ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)) | |
151 | c++; | |
152 | *c = 0; | |
153 | return new_name; | |
154 | } | |
155 | ||
156 | // ------------------------------------------------------------------ | |
157 | // static WrapperFunction::del_type(void *obj) | |
158 | // | |
159 | // Callback function used when cleaning up the hash table. | |
160 | // ------------------------------------------------------------------ | |
161 | ||
162 | void WrapperFunction::del_type(void *obj) { | |
163 | delete (char *) obj; | |
164 | } |