]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/parms.cxx
1 /*******************************************************************************
2 * Simplified Wrapper and Interface Generator (SWIG)
4 * Author : David Beazley
6 * Department of Computer Science
7 * University of Chicago
10 * beazley@cs.uchicago.edu
12 * Please read the file LICENSE for the copyright and terms by which SWIG
13 * can be used and distributed.
14 *******************************************************************************/
16 /* ------------------------------------------------------------------------
21 This file is used to manage function parameters and parameter lists.
22 Rewritten (10/27) to solve a bunch of problems with memory management
23 and proper cleanup of things.
24 ------------------------------------------------------------------------ */
28 // ------------------------------------------------------------------------
29 // Parm::Parm(DataType *type, char *n)
31 // Create a new parameter from datatype 'type' and name 'n'.
32 // Copies will be made of type and n, unless they aren't specified.
33 // ------------------------------------------------------------------------
35 Parm::Parm(DataType
*type
, char *n
) {
37 t
= new DataType(type
);
41 name
= copy_string(n
);
48 // ------------------------------------------------------------------------
49 // Parm::Parm(Parm *p)
51 // Make a copy of a parameter
52 // ------------------------------------------------------------------------
55 if (p
->t
) t
= new DataType(p
->t
);
56 name
= copy_string(p
->name
);
57 call_type
= p
->call_type
;
58 defvalue
= copy_string(p
->defvalue
);
60 objc_separator
= copy_string(p
->objc_separator
);
63 // ------------------------------------------------------------------------
66 // Destroy a parameter
67 // ------------------------------------------------------------------------
71 if (name
) delete name
;
72 if (defvalue
) delete defvalue
;
73 if (objc_separator
) delete objc_separator
;
76 /********************************************************************
79 These functions are used to manipulate lists of parameters
80 ********************************************************************/
82 // ------------------------------------------------------------------
83 // ParmList::ParmList()
85 // Create a new (empty) parameter list
86 // ------------------------------------------------------------------
88 ParmList::ParmList() {
92 parms
= new Parm
*[maxparms
]; // Create an array of parms
93 for (int i
= 0; i
< MAXPARMS
; i
++)
94 parms
[i
] = (Parm
*) 0;
97 // ------------------------------------------------------------------
98 // ParmList::ParmList(ParmList *l)
100 // Make a copy of parameter list
101 // ------------------------------------------------------------------
103 ParmList::ParmList(ParmList
*l
) {
108 maxparms
= l
->maxparms
;
109 parms
= new Parm
*[maxparms
];
111 for (i
= 0; i
< maxparms
; i
++) {
113 parms
[i
] = new Parm(l
->parms
[i
]);
121 parms
= new Parm
*[maxparms
]; // Create an array of parms
123 for (i
= 0; i
< MAXPARMS
; i
++)
124 parms
[i
] = (Parm
*) 0;
128 // ------------------------------------------------------------------
129 // ParmList::~ParmList()
131 // Delete a parameter list
132 // ------------------------------------------------------------------
134 ParmList::~ParmList() {
135 for (int i
= 0; i
< maxparms
; i
++) {
136 if (parms
[i
]) delete parms
[i
];
141 // ------------------------------------------------------------------
142 // void ParmList::moreparms() (PRIVATE)
144 // Doubles the amount of parameter memory available.
145 // ------------------------------------------------------------------
147 void ParmList::moreparms() {
151 newparms
= new Parm
*[maxparms
*2];
152 for (i
= 0; i
< 2*maxparms
; i
++)
153 newparms
[i
] = (Parm
*) 0;
154 for (i
= 0; i
< maxparms
; i
++) {
155 newparms
[i
] = parms
[i
];
157 maxparms
= 2*maxparms
;
162 // ------------------------------------------------------------------
163 // void ParmList::append(Parm *p)
165 // Add a new parameter to the end of a parameter list
166 // ------------------------------------------------------------------
168 void ParmList::append(Parm
*p
) {
170 if (nparms
== maxparms
) moreparms();
172 // Add parm onto the end
174 parms
[nparms
] = new Parm(p
);
178 // ------------------------------------------------------------------
179 // void ParmList::insert(Parm *p, int pos)
181 // Inserts a parameter at position pos. Parameters are inserted
182 // *before* any existing parameter at position pos.
183 // ------------------------------------------------------------------
185 void ParmList::insert(Parm
*p
, int pos
) {
187 // If pos is out of range, we'd better fix it
189 if (pos
< 0) pos
= 0;
190 if (pos
> nparms
) pos
= nparms
;
192 // If insertion is going to need more memory, take care of that now
194 if (nparms
>= maxparms
) moreparms();
196 // Now shift all of the existing parms to the right
198 for (int i
= nparms
; i
> pos
; i
--) {
199 parms
[i
] = parms
[i
-1];
204 parms
[pos
] = new Parm(p
);
209 // ------------------------------------------------------------------
210 // void ParmList::del(int pos)
212 // Deletes the parameter at position pos.
213 // ------------------------------------------------------------------
215 void ParmList::del(int pos
) {
217 if (nparms
<= 0) return;
218 if (pos
< 0) pos
= 0;
219 if (pos
>= nparms
) pos
= nparms
-1;
221 // Delete the parameter (if it exists)
223 if (parms
[pos
]) delete parms
[pos
];
225 // Now slide all of the parameters to the left
227 for (int i
= pos
; i
< nparms
-1; i
++) {
228 parms
[i
] = parms
[i
+1];
234 // ------------------------------------------------------------------
235 // Parm *ParmList::get(int pos)
237 // Gets the parameter at location pos. Returns 0 if invalid
239 // ------------------------------------------------------------------
241 Parm
*ParmList::get(int pos
) {
243 if ((pos
< 0) || (pos
>= nparms
)) return 0;
247 // ------------------------------------------------------------------
248 // int ParmList::numopt()
250 // Gets the number of optional arguments.
251 // ------------------------------------------------------------------
252 int ParmList::numopt() {
256 for (int i
= 0; i
< nparms
; i
++) {
257 if (parms
[i
]->defvalue
) {
260 } else if (typemap_check("default",typemap_lang
,parms
[i
]->t
,parms
[i
]->name
)) {
263 } else if (typemap_check("ignore",typemap_lang
,parms
[i
]->t
,parms
[i
]->name
)) {
265 } else if (typemap_check("build",typemap_lang
,parms
[i
]->t
,parms
[i
]->name
)) {
269 fprintf(stderr
,"%s : Line %d. Argument %d must have a default value!\n", input_file
,line_number
,i
+1);
276 // ------------------------------------------------------------------
277 // int ParmList::numarg()
279 // Gets the number of arguments
280 // ------------------------------------------------------------------
281 int ParmList::numarg() {
284 for (int i
= 0; i
< nparms
; i
++) {
285 if (!parms
[i
]->ignore
)
291 // ------------------------------------------------------------------
292 // Parm &ParmList::operator[](int n)
294 // Returns parameter n in the parameter list. May generate
295 // an error if that parameter is out of range.
296 // ------------------------------------------------------------------
298 Parm
&ParmList::operator[](int n
) {
300 if ((n
< 0) || (n
>= nparms
)) {
301 fprintf(stderr
,"ParmList : Fatal error. subscript out of range in ParmList.operator[]\n");
308 // ---------------------------------------------------------------------
309 // Parm * ParmList::get_first()
311 // Returns the first item on a parameter list.
312 // ---------------------------------------------------------------------
314 Parm
*ParmList::get_first() {
316 if (nparms
> 0) return parms
[current_parm
++];
317 else return (Parm
*) 0;
320 // ----------------------------------------------------------------------
321 // Parm *ParmList::get_next()
323 // Returns the next item on the parameter list.
324 // ----------------------------------------------------------------------
326 Parm
* ParmList::get_next() {
327 if (current_parm
>= nparms
) return 0;
328 else return parms
[current_parm
++];
331 // ---------------------------------------------------------------------
332 // void ParmList::print_types(FILE *f)
334 // Prints a comma separated list of all of the parameter types.
335 // This is for generating valid C prototypes. Has to do some
336 // manipulation of pointer types depending on how the call_type
337 // variable has been set.
338 // ----------------------------------------------------------------------
340 void ParmList::print_types(FILE *f
) {
346 is_pointer
= parms
[pn
]->t
->is_pointer
;
347 if (parms
[pn
]->t
->is_reference
) {
348 if (parms
[pn
]->t
->is_pointer
) {
349 parms
[pn
]->t
->is_pointer
--;
350 fprintf(f
,"%s&", parms
[pn
]->t
->print_real());
351 parms
[pn
]->t
->is_pointer
++;
353 fprintf(f
,"%s&", parms
[pn
]->t
->print_real());
356 if (parms
[pn
]->call_type
& CALL_VALUE
) parms
[pn
]->t
->is_pointer
++;
357 if (parms
[pn
]->call_type
& CALL_REFERENCE
) parms
[pn
]->t
->is_pointer
--;
358 fprintf(f
,"%s", parms
[pn
]->t
->print_real());
359 parms
[pn
]->t
->is_pointer
= is_pointer
;
368 // ---------------------------------------------------------------------
369 // void ParmList::print_types(String &f)
371 // Generates a comma separated list of function types. Is used in
372 // C++ code generation when generating hash keys and for function overloading.
373 // ----------------------------------------------------------------------
375 void ParmList::print_types(String
&f
) {
381 is_pointer
= parms
[pn
]->t
->is_pointer
;
382 if (parms
[pn
]->t
->is_reference
) {
383 if (parms
[pn
]->t
->is_pointer
) {
384 parms
[pn
]->t
->is_pointer
--;
385 f
<< parms
[pn
]->t
->print_real() << "&";
386 parms
[pn
]->t
->is_pointer
++;
388 f
<< parms
[pn
]->t
->print_real() << "&";
391 if (parms
[pn
]->call_type
& CALL_VALUE
) parms
[pn
]->t
->is_pointer
++;
392 if (parms
[pn
]->call_type
& CALL_REFERENCE
) parms
[pn
]->t
->is_pointer
--;
393 f
<< parms
[pn
]->t
->print_real();
394 parms
[pn
]->t
->is_pointer
= is_pointer
;
403 // ---------------------------------------------------------------------
404 // void ParmList::print_args(FILE *f)
406 // Prints a comma separated list of all of the parameter arguments.
407 // ----------------------------------------------------------------------
409 void ParmList::print_args(FILE *f
) {
415 is_pointer
= parms
[pn
]->t
->is_pointer
;
416 if (parms
[pn
]->t
->is_reference
) {
417 if (parms
[pn
]->t
->is_pointer
) {
418 parms
[pn
]->t
->is_pointer
--;
419 fprintf(f
,"%s&", parms
[pn
]->t
->print_full());
420 parms
[pn
]->t
->is_pointer
++;
422 fprintf(f
,"%s&", parms
[pn
]->t
->print_full());
425 if (parms
[pn
]->call_type
& CALL_VALUE
) parms
[pn
]->t
->is_pointer
++;
426 if (parms
[pn
]->call_type
& CALL_REFERENCE
) parms
[pn
]->t
->is_pointer
--;
427 fprintf(f
,"%s", parms
[pn
]->t
->print_full());
428 parms
[pn
]->t
->is_pointer
= is_pointer
;
430 fprintf(f
,"%s",parms
[pn
]->name
);
437 // -------------------------------------------------------------------
438 // int check_defined()
440 // Checks to see if all of the datatypes are defined.
441 // -------------------------------------------------------------------
443 int ParmList::check_defined() {
446 for (i
= 0; i
< nparms
; i
++) {
448 a
+=parms
[i
]->t
->check_defined();
456 // -------------------------------------------------------------------
457 // void ParmList::sub_parmnames(String &s)
459 // Given a string, this function substitutes all of the parameter
460 // names with their internal representation. Used in very special
461 // kinds of typemaps.
462 // -------------------------------------------------------------------
464 void ParmList::sub_parmnames(String
&s
) {
466 extern char *emit_local(int i
);
467 for (int i
= 0; i
< nparms
; i
++) {
469 if (strlen(p
->name
) > 0) {
470 s
.replaceid(p
->name
, emit_local(i
));