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.
7 * This file defines a variety of wrapping rules for C/C++ handling including
8 * the naming of local variables, calling conventions, and so forth.
9 * ----------------------------------------------------------------------------- */
11 char cvsroot_cwrap_c[] = "$Id: cwrap.c 11312 2009-06-24 17:20:17Z wsfulton $";
15 extern int cparse_cplusplus;
17 static Parm *nonvoid_parms(Parm *p) {
19 SwigType *t = Getattr(p, "type");
20 if (SwigType_type(t) == T_VOID)
26 /* -----------------------------------------------------------------------------
29 * Generates a name for the ith argument in an argument list
30 * ----------------------------------------------------------------------------- */
32 String *Swig_cparm_name(Parm *p, int i) {
33 String *name = NewStringf("arg%d", i + 1);
35 Setattr(p, "lname", name);
41 /* -----------------------------------------------------------------------------
44 * Creates a string that declares a C local variable type. Converts references
45 * and user defined types to pointers.
46 * ----------------------------------------------------------------------------- */
48 static String *Swig_clocal(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr value) {
51 decl = NewStringEmpty();
53 switch (SwigType_type(t)) {
56 String *lstrname = SwigType_lstr(t, name);
57 String *lstr = SwigType_lstr(t, 0);
58 Printf(decl, "%s = (%s) &%s_defvalue", lstrname, lstr, name);
62 String *lstrname = SwigType_lstr(t, name);
63 Printf(decl, "%s = 0", lstrname);
70 Printf(decl, "void *%s = 0", name);
75 String *lcaststr = SwigType_lcaststr(t, value);
76 String *lstr = SwigType_lstr(t, 0);
77 String *lstrn = SwigType_lstr(t, name);
78 Printf(decl, "%s = (%s) %s", lstrn, lstr, lcaststr);
83 String *lstrname = SwigType_lstr(t, name);
84 Append(decl, lstrname);
91 /* -----------------------------------------------------------------------------
92 * Swig_wrapped_var_convert()
94 * Converts a member variable for use in the get and set wrapper methods.
95 * This function only converts user defined types to pointers.
96 * ----------------------------------------------------------------------------- */
98 String *Swig_wrapped_var_type(SwigType *t, int varcref) {
101 if (!Strstr(t, "enum $unnamed")) {
104 /* Change the type for unnamed enum instance variables */
105 ty = NewString("int");
108 if (SwigType_isclass(t)) {
110 if (cparse_cplusplus) {
111 if (!SwigType_isconst(ty))
112 SwigType_add_qualifier(ty, "const");
113 SwigType_add_reference(ty);
118 SwigType_add_pointer(ty);
124 String *Swig_wrapped_member_var_type(SwigType *t, int varcref) {
127 if (!Strstr(t, "enum $unnamed")) {
130 /* Change the type for unnamed enum instance variables */
131 ty = NewString("int");
133 if (SwigType_isclass(t)) {
135 if (cparse_cplusplus) {
136 if (!SwigType_isconst(ty))
137 SwigType_add_qualifier(ty, "const");
138 SwigType_add_reference(ty);
143 SwigType_add_pointer(ty);
150 static String *Swig_wrapped_var_deref(SwigType *t, const_String_or_char_ptr name, int varcref) {
151 if (SwigType_isclass(t)) {
153 if (cparse_cplusplus) {
154 return NewStringf("*%s", name);
156 return NewStringf("%s", name);
159 return NewStringf("*%s", name);
162 return SwigType_rcaststr(t, name);
166 static String *Swig_wrapped_var_assign(SwigType *t, const_String_or_char_ptr name, int varcref) {
167 if (SwigType_isclass(t)) {
169 return NewStringf("%s", name);
171 return NewStringf("&%s", name);
174 return SwigType_lcaststr(t, name);
178 /* -----------------------------------------------------------------------------
181 * Emit all of the local variables for a list of parameters. Returns the
182 * number of parameters.
183 * Default values for the local variables are only emitted if the compact default
184 * argument behaviour is required.
185 * ----------------------------------------------------------------------------- */
186 int Swig_cargs(Wrapper *w, ParmList *p) {
188 int compactdefargs = ParmList_is_compactdefargs(p);
191 String *lname = Swig_cparm_name(p, i);
192 SwigType *pt = Getattr(p, "type");
193 if ((SwigType_type(pt) != T_VOID)) {
195 String *type = Getattr(p, "type");
196 /* default values only emitted if in compact default args mode */
197 String *pvalue = (compactdefargs) ? Getattr(p, "value") : 0;
199 /* When using compactdefaultargs, the code generated initialises a variable via a constructor call that accepts the
200 * default value as a parameter. The default constructor is not called and therefore SwigValueWrapper is not needed. */
201 SwigType *altty = pvalue ? 0 : SwigType_alttype(type, 0);
203 int tycode = SwigType_type(type);
204 if (tycode == T_REFERENCE) {
207 String *defname, *defvalue, *rvalue, *qvalue;
208 rvalue = SwigType_typedef_resolve_all(pvalue);
209 qvalue = SwigType_typedef_qualified(rvalue);
210 defname = NewStringf("%s_defvalue", lname);
212 SwigType_del_reference(tvalue);
213 tycode = SwigType_type(tvalue);
214 if (tycode != T_USER) {
215 /* plain primitive type, we copy the the def value */
216 String *lstr = SwigType_lstr(tvalue, defname);
217 defvalue = NewStringf("%s = %s", lstr, qvalue);
220 /* user type, we copy the reference value */
221 String *str = SwigType_str(type, defname);
222 defvalue = NewStringf("%s = %s", str, qvalue);
225 Wrapper_add_localv(w, defname, defvalue, NIL);
232 } else if (!pvalue && ((tycode == T_POINTER) || (tycode == T_STRING))) {
233 pvalue = (String *) "0";
236 local = Swig_clocal(pt, lname, pvalue);
238 local = Swig_clocal(altty, lname, pvalue);
241 Wrapper_add_localv(w, lname, local, NIL);
251 /* -----------------------------------------------------------------------------
254 * This function generates the C code needed to set the result of a C
256 * ----------------------------------------------------------------------------- */
258 String *Swig_cresult(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr decl) {
261 fcall = NewStringEmpty();
262 switch (SwigType_type(t)) {
267 String *lstr = SwigType_lstr(t, 0);
268 Printf(fcall, "%s = (%s) &", name, lstr);
273 Printf(fcall, "%s = ", name);
277 /* Normal return value */
279 String *lstr = SwigType_lstr(t, 0);
280 Printf(fcall, "%s = (%s)", name, lstr);
286 /* Now print out function call */
291 char *c = Char(decl) + Len(decl) - 1;
292 if (!((*c == ';') || (*c == '}')))
299 /* -----------------------------------------------------------------------------
300 * Swig_cfunction_call()
302 * Creates a string that calls a C function using the local variable rules
305 * name(arg0, arg1, arg2, ... argn)
307 * ----------------------------------------------------------------------------- */
309 String *Swig_cfunction_call(const_String_or_char_ptr name, ParmList *parms) {
316 func = NewStringEmpty();
317 nname = SwigType_namestr(name);
320 SWIGTEMPLATEDISAMBIGUATOR is compiler dependent (swiglabels.swg),
321 - SUN Studio 9 requires 'template',
322 - gcc-3.4 forbids the use of 'template'.
323 the rest seems not caring very much,
325 if (SwigType_istemplate(name)) {
326 String *prefix = Swig_scopename_prefix(nname);
327 if (!prefix || Len(prefix) == 0) {
328 Printf(func, "%s(", nname);
330 String *last = Swig_scopename_last(nname);
331 Printf(func, "%s::SWIGTEMPLATEDISAMBIGUATOR %s(", prefix, last);
336 Printf(func, "%s(", nname);
341 SwigType *pt = Getattr(p, "type");
342 if ((SwigType_type(pt) != T_VOID)) {
343 SwigType *rpt = SwigType_typedef_resolve_all(pt);
344 String *pname = Swig_cparm_name(p, i);
345 String *rcaststr = SwigType_rcaststr(rpt, pname);
348 Printv(func, ",", rcaststr, NIL);
350 Append(func, rcaststr);
364 /* -----------------------------------------------------------------------------
365 * Swig_cmethod_call()
367 * Generates a string that calls a C++ method from a list of parameters.
369 * arg0->name(arg1, arg2, arg3, ..., argn)
371 * self is an argument that defines how to handle the first argument. Normally,
372 * it should be set to "this->". With C++ proxy classes enabled, it could be
373 * set to "(*this)->" or some similar sequence.
374 * ----------------------------------------------------------------------------- */
376 static String *Swig_cmethod_call(const_String_or_char_ptr name, ParmList *parms, const_String_or_char_ptr self, String *explicit_qualifier, SwigType *director_type) {
377 String *func, *nname;
383 func = NewStringEmpty();
388 self = (char *) "(this)->";
391 if (SwigType_istemplate(name) && (strncmp(Char(name), "operator ", 9) == 0)) {
392 /* fix for template + operators and compilers like gcc 3.3.5 */
393 String *tprefix = SwigType_templateprefix(name);
396 nname = SwigType_namestr(name);
400 const char *pname = "darg";
401 String *rcaststr = SwigType_rcaststr(director_type, pname);
402 Replaceall(func, "this", rcaststr);
405 pt = Getattr(p, "type");
407 /* If the method is invoked through a dereferenced pointer, we don't add any casts
408 (needed for smart pointers). Otherwise, we cast to the appropriate type */
410 if (Strstr(func, "*this")) {
411 String *pname = Swig_cparm_name(p, 0);
412 Replaceall(func, "this", pname);
415 String *pname = Swig_cparm_name(p, 0);
416 String *rcaststr = SwigType_rcaststr(pt, pname);
417 Replaceall(func, "this", rcaststr);
423 SWIGTEMPLATEDESIMBUAGATOR is compiler dependent (swiglabels.swg),
424 - SUN Studio 9 requires 'template',
425 - gcc-3.4 forbids the use of 'template' (correctly implementing the ISO C++ standard)
426 the others don't seem to care,
428 if (SwigType_istemplate(name))
429 Printf(func, "SWIGTEMPLATEDISAMBIGUATOR ");
431 if (explicit_qualifier) {
432 Printv(func, explicit_qualifier, "::", NIL);
436 Printf(func, "%s(", nname);
441 pt = Getattr(p, "type");
442 if ((SwigType_type(pt) != T_VOID)) {
443 String *pname = Swig_cparm_name(p, i);
444 String *rcaststr = SwigType_rcaststr(pt, pname);
447 Append(func, rcaststr);
460 /* -----------------------------------------------------------------------------
461 * Swig_cconstructor_call()
463 * Creates a string that calls a C constructor function.
465 * calloc(1,sizeof(name));
466 * ----------------------------------------------------------------------------- */
468 String *Swig_cconstructor_call(const_String_or_char_ptr name) {
471 func = NewStringEmpty();
472 Printf(func, "calloc(1, sizeof(%s))", name);
477 /* -----------------------------------------------------------------------------
478 * Swig_cppconstructor_call()
480 * Creates a string that calls a C function using the local variable rules
483 * name(arg0, arg1, arg2, ... argn)
485 * ----------------------------------------------------------------------------- */
487 String *Swig_cppconstructor_base_call(const_String_or_char_ptr name, ParmList *parms, int skip_self) {
499 nname = SwigType_namestr(name);
500 func = NewStringEmpty();
501 Printf(func, "new %s(", nname);
503 pt = Getattr(p, "type");
504 if ((SwigType_type(pt) != T_VOID)) {
505 String *rcaststr = 0;
509 if (!Getattr(p, "arg:byname")) {
510 pname = Swig_cparm_name(p, i);
513 pname = Getattr(p, "value");
517 pname = Copy(Getattr(p, "name"));
519 rcaststr = SwigType_rcaststr(pt, pname);
520 Append(func, rcaststr);
532 String *Swig_cppconstructor_call(const_String_or_char_ptr name, ParmList *parms) {
533 return Swig_cppconstructor_base_call(name, parms, 0);
536 String *Swig_cppconstructor_nodirector_call(const_String_or_char_ptr name, ParmList *parms) {
537 return Swig_cppconstructor_base_call(name, parms, 1);
540 String *Swig_cppconstructor_director_call(const_String_or_char_ptr name, ParmList *parms) {
541 return Swig_cppconstructor_base_call(name, parms, 0);
544 /* -----------------------------------------------------------------------------
545 * Swig_rflag_search()
547 * This function searches for the class attribute 'attr' in the class
548 * 'n' or recursively in its bases.
550 * If you define SWIG_FAST_REC_SEARCH, the method will set the found
551 * 'attr' in the target class 'n'. If not, the method will set the
552 * 'noattr' one. This prevents of having to navigate the entire
553 * hierarchy tree everytime, so, it is an O(1) method... or something
554 * like that. However, it populates all the parsed classes with the
555 * 'attr' and/or 'noattr' attributes.
557 * If you undefine the SWIG_FAST_REC_SEARCH no attribute will be set
558 * while searching. This could be slower for large projects with very
559 * large hierarchy trees... or maybe not. But it will be cleaner.
561 * Maybe later a swig option can be added to switch at runtime.
563 * ----------------------------------------------------------------------------- */
565 /* #define SWIG_FAST_REC_SEARCH 1 */
566 String *Swig_rflag_search(Node *n, const String *attr, const String *noattr) {
568 n = Swig_methodclass(n);
569 if (GetFlag(n, noattr)) {
572 f = GetFlagAttr(n, attr);
576 List *bl = Getattr(n, "bases");
579 for (bi = First(bl); bi.item; bi = Next(bi)) {
580 f = Swig_rflag_search(bi.item, attr, noattr);
582 #ifdef SWIG_FAST_REC_SEARCH
583 SetFlagAttr(n, attr, f);
590 #ifdef SWIG_FAST_REC_SEARCH
596 /* -----------------------------------------------------------------------------
599 * find the unref call, if any.
600 * ----------------------------------------------------------------------------- */
602 String *Swig_unref_call(Node *n) {
603 Node *cn = Swig_methodclass(n);
604 String *unref = Swig_rflag_search(cn, "feature:unref", "feature:nounref");
606 String *pname = Swig_cparm_name(0, 0);
607 unref = NewString(unref);
608 Replaceall(unref, "$this", pname);
609 Replaceall(unref, "$self", pname);
615 /* -----------------------------------------------------------------------------
618 * find the ref call, if any.
619 * ----------------------------------------------------------------------------- */
621 String *Swig_ref_call(Node *n, const String *lname) {
622 Node *cn = Swig_methodclass(n);
623 String *ref = Swig_rflag_search(cn, "feature:ref", "feature:noref");
625 ref = NewString(ref);
626 Replaceall(ref, "$this", lname);
627 Replaceall(ref, "$self", lname);
632 /* -----------------------------------------------------------------------------
633 * Swig_cdestructor_call()
635 * Creates a string that calls a C destructor function.
637 * free((char *) arg0);
638 * ----------------------------------------------------------------------------- */
640 String *Swig_cdestructor_call(Node *n) {
641 String *unref = Swig_unref_call(n);
646 String *pname = Swig_cparm_name(0, 0);
647 String *call = NewStringf("free((char *) %s);", pname);
654 /* -----------------------------------------------------------------------------
655 * Swig_cppdestructor_call()
657 * Creates a string that calls a C destructor function.
660 * ----------------------------------------------------------------------------- */
662 String *Swig_cppdestructor_call(Node *n) {
663 String *unref = Swig_unref_call(n);
667 String *pname = Swig_cparm_name(0, 0);
668 String *call = NewStringf("delete %s;", pname);
674 /* -----------------------------------------------------------------------------
675 * Swig_cmemberset_call()
677 * Generates a string that sets the name of a member in a C++ class or C struct.
681 * ----------------------------------------------------------------------------- */
683 String *Swig_cmemberset_call(const_String_or_char_ptr name, SwigType *type, String *self, int varcref) {
685 String *pname0 = Swig_cparm_name(0, 0);
686 String *pname1 = Swig_cparm_name(0, 1);
687 func = NewStringEmpty();
689 self = NewString("(this)->");
691 self = NewString(self);
692 Replaceall(self, "this", pname0);
693 if (SwigType_type(type) != T_ARRAY) {
694 if (!Strstr(type, "enum $unnamed")) {
695 String *dref = Swig_wrapped_var_deref(type, pname1, varcref);
696 Printf(func, "if (%s) %s%s = %s", pname0, self, name, dref);
699 Printf(func, "if (%s && sizeof(int) == sizeof(%s%s)) *(int*)(void*)&(%s%s) = %s", pname0, self, name, self, name, pname1);
709 /* -----------------------------------------------------------------------------
710 * Swig_cmemberget_call()
712 * Generates a string that sets the name of a member in a C++ class or C struct.
716 * ----------------------------------------------------------------------------- */
718 String *Swig_cmemberget_call(const_String_or_char_ptr name, SwigType *t, String *self, int varcref) {
721 String *pname0 = Swig_cparm_name(0, 0);
723 self = NewString("(this)->");
725 self = NewString(self);
726 Replaceall(self, "this", pname0);
727 func = NewStringEmpty();
728 call = Swig_wrapped_var_assign(t, "", varcref);
729 Printf(func, "%s (%s%s)", call, self, name);
736 /* -----------------------------------------------------------------------------
739 * Generates an extension function (a function defined in %extend)
741 * return_type function_name(parms) code
743 * ----------------------------------------------------------------------------- */
744 static String *extension_code(const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self) {
745 String *parms_str = cplusplus ? ParmList_str_defaultargs(parms) : ParmList_str(parms);
746 String *sig = NewStringf("%s(%s)", function_name, parms_str);
747 String *rt_sig = SwigType_str(return_type, sig);
748 String *body = NewStringf("SWIGINTERN %s", rt_sig);
749 Printv(body, code, "\n", NIL);
751 Replaceall(body, "$self", self);
758 /* -----------------------------------------------------------------------------
759 * Swig_add_extension_code()
761 * Generates an extension function (a function defined in %extend) and
762 * adds it to the "wrap:code" attribute of a node
764 * See also extension_code()
766 * ----------------------------------------------------------------------------- */
767 int Swig_add_extension_code(Node *n, const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self) {
768 String *body = extension_code(function_name, parms, return_type, code, cplusplus, self);
769 Setattr(n, "wrap:code", body);
775 /* -----------------------------------------------------------------------------
776 * Swig_MethodToFunction(Node *n)
778 * Converts a C++ method node to a function accessor function.
779 * ----------------------------------------------------------------------------- */
781 int Swig_MethodToFunction(Node *n, String *classname, int flags, SwigType *director_type, int is_director) {
782 String *name, *qualifier;
788 /* If smart pointer, change self dereferencing */
789 if (flags & CWRAP_SMART_POINTER) {
790 self = NewString("(*this)->");
793 /* If node is a member template expansion, we don't allow added code */
794 if (Getattr(n, "templatetype"))
795 flags &= ~(CWRAP_EXTEND);
797 name = Getattr(n, "name");
798 qualifier = Getattr(n, "qualifier");
799 parms = CopyParmList(nonvoid_parms(Getattr(n, "parms")));
801 type = NewString(classname);
803 SwigType_push(type, qualifier);
805 SwigType_add_pointer(type);
806 p = NewParm(type, "self");
807 Setattr(p, "self", "1");
808 Setattr(p, "hidden","1");
810 Disable the 'this' ownership in 'self' to manage inplace
813 A& A::operator+=(int i) { ...; return *this;}
815 Here the 'self' parameter ownership needs to be disabled since
816 there could be two objects sharing the same 'this' pointer: the
817 input and the result one. And worse, the pointer could be deleted
818 in one of the objects (input), leaving the other (output) with
819 just a seg. fault to happen.
821 To avoid the previous problem, use
823 %feature("self:disown") *::operator+=;
824 %feature("new") *::operator+=;
826 These two lines just transfer the ownership of the 'this' pointer
827 from the input to the output wrapping object.
829 This happens in python, but may also happens in other target
832 if (GetFlag(n, "feature:self:disown")) {
833 Setattr(p, "wrap:disown", "1");
835 set_nextSibling(p, parms);
838 /* Generate action code for the access */
839 if (!(flags & CWRAP_EXTEND)) {
840 String *explicit_qualifier = 0;
843 String *explicitcall_name = 0;
844 int pure_virtual = !(Cmp(Getattr(n, "storage"), "virtual")) && !(Cmp(Getattr(n, "value"), "0"));
846 /* Call the explicit method rather than allow for a polymorphic call */
847 if ((flags & CWRAP_DIRECTOR_TWO_CALLS) || (flags & CWRAP_DIRECTOR_ONE_CALL)) {
848 String *access = Getattr(n, "access");
849 if (access && (Cmp(access, "protected") == 0)) {
850 /* If protected access (can only be if a director method) then call the extra public accessor method (language module must provide this) */
851 String *explicit_qualifier_tmp = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
852 explicitcall_name = NewStringf("%sSwigPublic", name);
853 explicit_qualifier = NewStringf("SwigDirector_%s", explicit_qualifier_tmp);
854 Delete(explicit_qualifier_tmp);
856 explicit_qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
860 call = Swig_cmethod_call(explicitcall_name ? explicitcall_name : name, p, self, explicit_qualifier, director_type);
861 cres = Swig_cresult(Getattr(n, "type"), "result", call);
863 if (pure_virtual && is_director && (flags & CWRAP_DIRECTOR_TWO_CALLS)) {
864 String *qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
866 cres = NewStringf("Swig::DirectorPureVirtualException::raise(\"%s::%s\");", qualifier, name);
870 if (flags & CWRAP_DIRECTOR_TWO_CALLS) {
871 /* Create two method calls, one to call the explicit method, the other a normal polymorphic function call */
872 String *cres_both_calls = NewStringf("");
873 String *call_extra = Swig_cmethod_call(name, p, self, 0, director_type);
874 String *cres_extra = Swig_cresult(Getattr(n, "type"), "result", call_extra);
875 Printv(cres_both_calls, "if (upcall) {\n", cres, "\n", "} else {", cres_extra, "\n}", NIL);
876 Setattr(n, "wrap:action", cres_both_calls);
879 Delete(cres_both_calls);
881 Setattr(n, "wrap:action", cres);
884 Delete(explicitcall_name);
887 Delete(explicit_qualifier);
889 /* Methods with default arguments are wrapped with additional methods for each default argument,
890 * however, only one extra %extend method is generated. */
892 String *defaultargs = Getattr(n, "defaultargs");
893 String *code = Getattr(n, "code");
894 String *cname = Getattr(n, "classname") ? Getattr(n, "classname") : classname;
895 String *membername = Swig_name_member(cname, name);
896 String *mangled = Swig_name_mangle(membername);
897 int is_smart_pointer = flags & CWRAP_SMART_POINTER;
899 type = Getattr(n, "type");
901 /* Check if the method is overloaded. If so, and it has code attached, we append an extra suffix
902 to avoid a name-clash in the generated wrappers. This allows overloaded methods to be defined
904 if (Getattr(n, "sym:overloaded") && code) {
905 Append(mangled, Getattr(defaultargs ? defaultargs : n, "sym:overname"));
908 /* See if there is any code that we need to emit */
909 if (!defaultargs && code && !is_smart_pointer) {
910 Swig_add_extension_code(n, mangled, p, type, code, cparse_cplusplus, "self");
912 if (is_smart_pointer) {
915 String *func = NewStringf("%s(", mangled);
918 if (Cmp(Getattr(n, "storage"), "static") != 0) {
919 String *pname = Swig_cparm_name(pp, i);
920 String *ctname = SwigType_namestr(cname);
921 String *fadd = NewStringf("(%s*)(%s)->operator ->()", ctname, pname);
926 pp = nextSibling(pp);
930 pp = nextSibling(pp);
934 SwigType *pt = Getattr(pp, "type");
935 if ((SwigType_type(pt) != T_VOID)) {
936 String *pname = Swig_cparm_name(pp, i++);
937 String *rcaststr = SwigType_rcaststr(pt, pname);
938 Append(func, rcaststr);
941 pp = nextSibling(pp);
947 cres = Swig_cresult(Getattr(n, "type"), "result", func);
948 Setattr(n, "wrap:action", cres);
951 String *call = Swig_cfunction_call(mangled, p);
952 String *cres = Swig_cresult(Getattr(n, "type"), "result", call);
953 Setattr(n, "wrap:action", cres);
961 Setattr(n, "parms", p);
968 /* -----------------------------------------------------------------------------
971 * This function returns the class node for a given method or class.
972 * ----------------------------------------------------------------------------- */
974 Node *Swig_methodclass(Node *n) {
975 Node *nodetype = nodeType(n);
976 if (Cmp(nodetype, "class") == 0)
978 return GetFlag(n, "feature:extend") ? parentNode(parentNode(n)) : parentNode(n);
981 int Swig_directorclass(Node *n) {
982 Node *classNode = Swig_methodclass(n);
983 assert(classNode != 0);
984 return (Getattr(classNode, "vtable") != 0);
987 Node *Swig_directormap(Node *module, String *type) {
988 int is_void = !Cmp(type, "void");
989 if (!is_void && module) {
990 /* ?? follow the inheritance hierarchy? */
992 String *base = SwigType_base(type);
994 Node *directormap = Getattr(module, "wrap:directormap");
996 return Getattr(directormap, base);
1002 /* -----------------------------------------------------------------------------
1003 * Swig_ConstructorToFunction()
1005 * This function creates a C wrapper for a C constructor function.
1006 * ----------------------------------------------------------------------------- */
1008 int Swig_ConstructorToFunction(Node *n, String *classname, String *none_comparison, String *director_ctor, int cplus, int flags) {
1012 ParmList *directorparms;
1017 classNode = Swig_methodclass(n);
1018 use_director = Swig_directorclass(n);
1020 parms = CopyParmList(nonvoid_parms(Getattr(n, "parms")));
1022 /* Prepend the list of prefix_args (if any) */
1023 prefix_args = Getattr(n, "director:prefix_args");
1024 if (prefix_args != NIL) {
1027 directorparms = CopyParmList(prefix_args);
1028 for (p = directorparms; nextSibling(p); p = nextSibling(p));
1029 for (p2 = parms; p2; p2 = nextSibling(p2)) {
1031 set_nextSibling(p, p3);
1036 directorparms = parms;
1038 type = NewString(classname);
1039 SwigType_add_pointer(type);
1041 if (flags & CWRAP_EXTEND) {
1042 /* Constructors with default arguments are wrapped with additional constructor methods for each default argument,
1043 * however, only one extra %extend method is generated. */
1046 String *defaultargs = Getattr(n, "defaultargs");
1047 String *code = Getattr(n, "code");
1048 String *membername = Swig_name_construct(classname);
1049 String *mangled = Swig_name_mangle(membername);
1051 /* Check if the constructor is overloaded. If so, and it has code attached, we append an extra suffix
1052 to avoid a name-clash in the generated wrappers. This allows overloaded constructors to be defined
1054 if (Getattr(n, "sym:overloaded") && code) {
1055 Append(mangled, Getattr(defaultargs ? defaultargs : n, "sym:overname"));
1058 /* See if there is any code that we need to emit */
1059 if (!defaultargs && code) {
1060 Swig_add_extension_code(n, mangled, parms, type, code, cparse_cplusplus, "self");
1063 call = Swig_cfunction_call(mangled, parms);
1064 cres = Swig_cresult(type, "result", call);
1065 Setattr(n, "wrap:action", cres);
1072 /* if a C++ director class exists, create it rather than the original class */
1074 Node *parent = Swig_methodclass(n);
1075 int abstract = Getattr(parent, "abstract") != 0;
1076 String *name = Getattr(parent, "sym:name");
1077 String *directorname = NewStringf("SwigDirector_%s", name);
1078 String *action = NewStringEmpty();
1079 String *tmp_none_comparison = Copy(none_comparison);
1080 String *director_call;
1081 String *nodirector_call;
1083 Replaceall(tmp_none_comparison, "$arg", "arg1");
1085 director_call = Swig_cppconstructor_director_call(directorname, directorparms);
1086 nodirector_call = Swig_cppconstructor_nodirector_call(classname, parms);
1089 /* whether or not the abstract class has been subclassed in python,
1090 * create a director instance (there's no way to create a normal
1091 * instance). if any of the pure virtual methods haven't been
1092 * implemented in the target language, calls to those methods will
1093 * generate Swig::DirectorPureVirtualException exceptions.
1095 String *cres = Swig_cresult(type, "result", director_call);
1096 Append(action, cres);
1099 /* (scottm): The code for creating a new director is now a string
1100 template that gets passed in via the director_ctor argument.
1102 $comparison : an 'if' comparison from none_comparison
1103 $director_new: Call new for director class
1104 $nondirector_new: Call new for non-director class
1107 Append(action, director_ctor);
1108 Replaceall(action, "$comparison", tmp_none_comparison);
1110 cres = Swig_cresult(type, "result", director_call);
1111 Replaceall(action, "$director_new", cres);
1114 cres = Swig_cresult(type, "result", nodirector_call);
1115 Replaceall(action, "$nondirector_new", cres);
1118 Setattr(n, "wrap:action", action);
1119 Delete(tmp_none_comparison);
1121 Delete(directorname);
1123 String *call = Swig_cppconstructor_call(classname, parms);
1124 String *cres = Swig_cresult(type, "result", call);
1125 Setattr(n, "wrap:action", cres);
1130 String *call = Swig_cconstructor_call(classname);
1131 String *cres = Swig_cresult(type, "result", call);
1132 Setattr(n, "wrap:action", cres);
1137 Setattr(n, "type", type);
1138 Setattr(n, "parms", parms);
1140 if (directorparms != parms)
1141 Delete(directorparms);
1146 /* -----------------------------------------------------------------------------
1147 * Swig_DestructorToFunction()
1149 * This function creates a C wrapper for a destructor function.
1150 * ----------------------------------------------------------------------------- */
1152 int Swig_DestructorToFunction(Node *n, String *classname, int cplus, int flags) {
1156 type = NewString(classname);
1157 SwigType_add_pointer(type);
1158 p = NewParm(type, "self");
1159 Setattr(p, "self", "1");
1160 Setattr(p, "hidden", "1");
1161 Setattr(p, "wrap:disown", "1");
1163 type = NewString("void");
1165 if (flags & CWRAP_EXTEND) {
1168 String *membername, *mangled, *code;
1169 membername = Swig_name_destroy(classname);
1170 mangled = Swig_name_mangle(membername);
1171 code = Getattr(n, "code");
1173 Swig_add_extension_code(n, mangled, p, type, code, cparse_cplusplus, "self");
1175 call = Swig_cfunction_call(mangled, p);
1176 cres = NewStringf("%s;", call);
1177 Setattr(n, "wrap:action", cres);
1184 String *call = Swig_cppdestructor_call(n);
1185 String *cres = NewStringf("%s", call);
1186 Setattr(n, "wrap:action", cres);
1190 String *call = Swig_cdestructor_call(n);
1191 String *cres = NewStringf("%s", call);
1192 Setattr(n, "wrap:action", cres);
1197 Setattr(n, "type", type);
1198 Setattr(n, "parms", p);
1204 /* -----------------------------------------------------------------------------
1205 * Swig_MembersetToFunction()
1207 * This function creates a C wrapper for setting a structure member.
1208 * ----------------------------------------------------------------------------- */
1210 int Swig_MembersetToFunction(Node *n, String *classname, int flags, String **call) {
1217 SwigType *void_type = NewString("void");
1223 int varcref = flags & CWRAP_NATURAL_VAR;
1225 if (flags & CWRAP_SMART_POINTER) {
1226 self = NewString("(*this)->");
1228 if (flags & CWRAP_ALL_PROTECTED_ACCESS) {
1229 self = NewStringf("darg->");
1232 name = Getattr(n, "name");
1233 type = Getattr(n, "type");
1235 sname = Swig_name_set(name);
1236 membername = Swig_name_member(classname, sname);
1237 mangled = Swig_name_mangle(membername);
1239 t = NewString(classname);
1240 SwigType_add_pointer(t);
1241 parms = NewParm(t, "self");
1242 Setattr(parms, "self", "1");
1243 Setattr(parms, "hidden","1");
1246 ty = Swig_wrapped_member_var_type(type, varcref);
1247 p = NewParm(ty, name);
1248 Setattr(parms, "hidden", "1");
1249 set_nextSibling(parms, p);
1251 /* If the type is a pointer or reference. We mark it with a special wrap:disown attribute */
1252 if (SwigType_check_decl(type, "p.")) {
1253 Setattr(p, "wrap:disown", "1");
1257 if (flags & CWRAP_EXTEND) {
1259 String *code = Getattr(n, "code");
1261 /* I don't think this ever gets run - WSF */
1262 Swig_add_extension_code(n, mangled, parms, void_type, code, cparse_cplusplus, "self");
1264 *call = Swig_cfunction_call(mangled, parms);
1265 cres = NewStringf("%s;", *call);
1266 Setattr(n, "wrap:action", cres);
1270 *call = Swig_cmemberset_call(name, type, self, varcref);
1271 cres = NewStringf("%s;", *call);
1272 Setattr(n, "wrap:action", cres);
1275 Setattr(n, "type", void_type);
1276 Setattr(n, "parms", parms);
1287 /* -----------------------------------------------------------------------------
1288 * Swig_MembergetToFunction()
1290 * This function creates a C wrapper for getting a structure member.
1291 * ----------------------------------------------------------------------------- */
1293 int Swig_MembergetToFunction(Node *n, String *classname, int flags) {
1304 int varcref = flags & CWRAP_NATURAL_VAR;
1306 if (flags & CWRAP_SMART_POINTER) {
1307 if (checkAttribute(n, "storage", "static")) {
1308 Node *sn = Getattr(n, "cplus:staticbase");
1309 String *base = Getattr(sn, "name");
1310 self = NewStringf("%s::", base);
1312 self = NewString("(*this)->");
1315 if (flags & CWRAP_ALL_PROTECTED_ACCESS) {
1316 self = NewStringf("darg->");
1319 name = Getattr(n, "name");
1320 type = Getattr(n, "type");
1322 gname = Swig_name_get(name);
1323 membername = Swig_name_member(classname, gname);
1324 mangled = Swig_name_mangle(membername);
1326 t = NewString(classname);
1327 SwigType_add_pointer(t);
1328 parms = NewParm(t, "self");
1329 Setattr(parms, "self", "1");
1330 Setattr(parms, "hidden","1");
1333 ty = Swig_wrapped_member_var_type(type, varcref);
1334 if (flags & CWRAP_EXTEND) {
1338 String *code = Getattr(n, "code");
1340 /* I don't think this ever gets run - WSF */
1341 Swig_add_extension_code(n, mangled, parms, ty, code, cparse_cplusplus, "self");
1343 call = Swig_cfunction_call(mangled, parms);
1344 cres = Swig_cresult(ty, "result", call);
1345 Setattr(n, "wrap:action", cres);
1349 String *call = Swig_cmemberget_call(name, type, self, varcref);
1350 String *cres = Swig_cresult(ty, "result", call);
1351 Setattr(n, "wrap:action", cres);
1355 Setattr(n, "type", ty);
1356 Setattr(n, "parms", parms);
1366 /* -----------------------------------------------------------------------------
1367 * Swig_VarsetToFunction()
1369 * This function creates a C wrapper for setting a global variable or static member
1371 * ----------------------------------------------------------------------------- */
1373 int Swig_VarsetToFunction(Node *n, int flags) {
1374 String *name, *nname;
1376 SwigType *type, *ty;
1378 int varcref = flags & CWRAP_NATURAL_VAR;
1380 name = Getattr(n, "name");
1381 type = Getattr(n, "type");
1382 nname = SwigType_namestr(name);
1383 ty = Swig_wrapped_var_type(type, varcref);
1384 parms = NewParm(ty, name);
1386 if (flags & CWRAP_EXTEND) {
1387 String *sname = Swig_name_set(name);
1388 String *mangled = Swig_name_mangle(sname);
1389 String *call = Swig_cfunction_call(mangled, parms);
1390 String *cres = NewStringf("%s;", call);
1391 Setattr(n, "wrap:action", cres);
1397 if (!Strstr(type, "enum $unnamed")) {
1398 String *pname = Swig_cparm_name(0, 0);
1399 String *dref = Swig_wrapped_var_deref(type, pname, varcref);
1400 String *call = NewStringf("%s = %s;", nname, dref);
1401 Setattr(n, "wrap:action", call);
1406 String *pname = Swig_cparm_name(0, 0);
1407 String *call = NewStringf("if (sizeof(int) == sizeof(%s)) *(int*)(void*)&(%s) = %s;", nname, nname, pname);
1408 Setattr(n, "wrap:action", call);
1413 Setattr(n, "type", "void");
1414 Setattr(n, "parms", parms);
1421 /* -----------------------------------------------------------------------------
1422 * Swig_VargetToFunction()
1424 * This function creates a C wrapper for getting a global variable or static member
1426 * ----------------------------------------------------------------------------- */
1428 int Swig_VargetToFunction(Node *n, int flags) {
1429 String *cres, *call;
1434 int varcref = flags & CWRAP_NATURAL_VAR;
1436 name = Getattr(n, "name");
1437 type = Getattr(n, "type");
1438 ty = Swig_wrapped_var_type(type, varcref);
1440 if (flags & CWRAP_EXTEND) {
1441 String *sname = Swig_name_get(name);
1442 String *mangled = Swig_name_mangle(sname);
1443 call = Swig_cfunction_call(mangled, 0);
1444 cres = Swig_cresult(ty, "result", call);
1445 Setattr(n, "wrap:action", cres);
1449 String *nname = SwigType_namestr(name);
1450 call = Swig_wrapped_var_assign(type, nname, varcref);
1451 cres = Swig_cresult(ty, "result", call);
1452 Setattr(n, "wrap:action", cres);
1456 Setattr(n, "type", ty);
1457 Delattr(n, "parms");