~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Source/Modules/php4.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-11-10 16:29:56 UTC
  • mfrom: (1.2.8 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20081110162956-xue6itkuqhbza87s
Tags: 1.3.36-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop pike and libchicken-dev from the build-depends 
    (both are universe)
  - Use python2.5 instead of python2.4.
  - use php5
  - Clean Runtime/ as well.
  - debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.
  - drop "--without-mzscheme", we don't have it in our build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 * (may need to add more WARN_PHP4_xxx codes...)
43
43
 */
44
44
 
45
 
char cvsroot_php4_cxx[] = "$Id: php4.cxx 10351 2008-04-05 20:53:23Z wsfulton $";
 
45
char cvsroot_php4_cxx[] = "$Id: php4.cxx 10567 2008-06-24 00:41:07Z olly $";
46
46
 
47
47
#include "swigmod.h"
48
48
 
150
150
  ki = First(zend_types);
151
151
  if (ki.key)
152
152
    Printf(s_oinit, "\n/* Register resource destructors for pointer types */\n");
153
 
  while (ki.key)
154
 
    if (1 /* is pointer type */ ) {
155
 
      DOH *key = ki.key;
156
 
      Node *class_node = ki.item;
157
 
      String *human_name = key;
158
 
 
159
 
      // Write out destructor function header
160
 
      Printf(s_wrappers, "/* NEW Destructor style */\nstatic ZEND_RSRC_DTOR_FUNC(_wrap_destroy%s) {\n", key);
161
 
 
162
 
      // write out body
163
 
      if ((class_node != NOTCLASS)) {
164
 
        String *destructor = Getattr(class_node, "destructor");
165
 
        human_name = Getattr(class_node, "sym:name");
166
 
        if (!human_name)
167
 
          human_name = Getattr(class_node, "name");
168
 
        // Do we have a known destructor for this type?
169
 
        if (destructor) {
170
 
          Printf(s_wrappers, "  %s(rsrc, SWIGTYPE%s->name TSRMLS_CC);\n", destructor, key);
171
 
        } else {
172
 
          Printf(s_wrappers, "  /* No destructor for class %s */\n", human_name);
173
 
        }
 
153
  while (ki.key) {
 
154
    DOH *key = ki.key;
 
155
    Node *class_node = ki.item;
 
156
    String *human_name = key;
 
157
 
 
158
    // Write out destructor function header
 
159
    Printf(s_wrappers, "/* NEW Destructor style */\nstatic ZEND_RSRC_DTOR_FUNC(_wrap_destroy%s) {\n", key);
 
160
 
 
161
    // write out body
 
162
    if ((class_node != NOTCLASS)) {
 
163
      String *destructor = Getattr(class_node, "destructor");
 
164
      human_name = Getattr(class_node, "sym:name");
 
165
      if (!human_name)
 
166
        human_name = Getattr(class_node, "name");
 
167
      // Do we have a known destructor for this type?
 
168
      if (destructor) {
 
169
        Printf(s_wrappers, "  %s(rsrc, SWIGTYPE%s->name TSRMLS_CC);\n", destructor, key);
174
170
      } else {
175
 
        Printf(s_wrappers, "  /* No destructor for simple type %s */\n", key);
 
171
        Printf(s_wrappers, "  /* No destructor for class %s */\n", human_name);
176
172
      }
177
 
 
178
 
      // close function
179
 
      Printf(s_wrappers, "}\n");
180
 
 
181
 
      // declare le_swig_<mangled> to store php registration
182
 
      Printf(s_vdecl, "static int le_swig_%s=0; /* handle for %s */\n", key, human_name);
183
 
 
184
 
      // register with php
185
 
      Printf(s_oinit, "le_swig_%s=zend_register_list_destructors_ex" "(_wrap_destroy%s,NULL,(char *)(SWIGTYPE%s->name),module_number);\n", key, key, key);
186
 
 
187
 
      // store php type in class struct
188
 
      Printf(s_oinit, "SWIG_TypeClientData(SWIGTYPE%s,&le_swig_%s);\n", key, key);
189
 
 
190
 
      ki = Next(ki);
 
173
    } else {
 
174
      Printf(s_wrappers, "  /* No destructor for simple type %s */\n", key);
191
175
    }
 
176
 
 
177
    // close function
 
178
    Printf(s_wrappers, "}\n");
 
179
 
 
180
    // declare le_swig_<mangled> to store php registration
 
181
    Printf(s_vdecl, "static int le_swig_%s=0; /* handle for %s */\n", key, human_name);
 
182
 
 
183
    // register with php
 
184
    Printf(s_oinit, "le_swig_%s=zend_register_list_destructors_ex" "(_wrap_destroy%s,NULL,(char *)(SWIGTYPE%s->name),module_number);\n", key, key, key);
 
185
 
 
186
    // store php type in class struct
 
187
    Printf(s_oinit, "SWIG_TypeClientData(SWIGTYPE%s,&le_swig_%s);\n", key, key);
 
188
 
 
189
    ki = Next(ki);
 
190
  }
192
191
}
193
192
 
194
193
class PHP:public Language {
368
367
      SWIG_exit(EXIT_FAILURE);
369
368
    }
370
369
 
371
 
    Printf(f_extra, "# $Id: php4.cxx 10351 2008-04-05 20:53:23Z wsfulton $\n\n" "LTLIBRARY_NAME          = %s.la\n", module);
 
370
    Printf(f_extra, "# $Id: php4.cxx 10567 2008-06-24 00:41:07Z olly $\n\n" "LTLIBRARY_NAME          = %s.la\n", module);
372
371
 
373
372
    // C++ has more and different entries to C in Makefile.in
374
373
    if (!CPlusPlus) {
415
414
      SWIG_exit(EXIT_FAILURE);
416
415
    }
417
416
 
418
 
    Printf(f_extra, "dnl $Id: php4.cxx 10351 2008-04-05 20:53:23Z wsfulton $\n");
 
417
    Printf(f_extra, "dnl $Id: php4.cxx 10567 2008-06-24 00:41:07Z olly $\n");
419
418
    Printf(f_extra, "dnl ***********************************************************************\n");
420
419
    Printf(f_extra, "dnl ** THIS config.m4 is provided for PHPIZE and PHP's consumption NOT\n");
421
420
    Printf(f_extra, "dnl ** for any part of the rest of the %s build system\n", module);
1007
1006
      Printv(f->def, "ZEND_NAMED_FUNCTION(", wname, ") {\n", NIL);
1008
1007
    }
1009
1008
 
1010
 
    emit_args(d, l, f);
 
1009
    emit_parameter_variables(l, f);
1011
1010
    /* Attach standard typemaps */
1012
1011
 
1013
1012
    emit_attach_parmmaps(l, f);
1175
1174
    Setattr(n, "wrap:name", wname);
1176
1175
 
1177
1176
    /* emit function call */
1178
 
    emit_action(n, f);
 
1177
    String *actioncode = emit_action(n);
1179
1178
 
1180
 
    if ((tm = Swig_typemap_lookup_new("out", n, "result", 0))) {
 
1179
    if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) {
1181
1180
      Replaceall(tm, "$input", "result");
1182
1181
      Replaceall(tm, "$source", "result");
1183
1182
      Replaceall(tm, "$target", "return_value");
1195
1194
        if (native_constructor == NATIVE_CONSTRUCTOR) {
1196
1195
          Printf(f->code, "add_property_zval(this_ptr,\"" SWIG_PTR "\",_cPtr);\n");
1197
1196
        } else {
1198
 
          String *shadowrettype = SwigToPhpType(d, iname, true);
 
1197
          String *shadowrettype = GetShadowReturnType(n);
1199
1198
          Printf(f->code, "object_init_ex(return_value,ptr_ce_swig_%s);\n", shadowrettype);
1200
1199
          Delete(shadowrettype);
1201
1200
          Printf(f->code, "add_property_zval(return_value,\"" SWIG_PTR "\",_cPtr);\n");
1205
1204
    } else {
1206
1205
      Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), name);
1207
1206
    }
 
1207
    emit_return_variable(n, d, f);
1208
1208
 
1209
1209
    if (outarg) {
1210
1210
      Printv(f->code, outarg, NIL);
1216
1216
 
1217
1217
    /* Look to see if there is any newfree cleanup code */
1218
1218
    if (GetFlag(n, "feature:new")) {
1219
 
      if ((tm = Swig_typemap_lookup_new("newfree", n, "result", 0))) {
 
1219
      if ((tm = Swig_typemap_lookup("newfree", n, "result", 0))) {
1220
1220
        Printf(f->code, "%s\n", tm);
1221
1221
        Delete(tm);
1222
1222
      }
1223
1223
    }
1224
1224
 
1225
1225
    /* See if there is any return cleanup code */
1226
 
    if ((tm = Swig_typemap_lookup_new("ret", n, "result", 0))) {
 
1226
    if ((tm = Swig_typemap_lookup("ret", n, "result", 0))) {
1227
1227
      Printf(f->code, "%s\n", tm);
1228
1228
      Delete(tm);
1229
1229
    }
1439
1439
        if (wrapperType == memberfn)
1440
1440
          p = nextSibling(p);
1441
1441
        while (p) {
 
1442
          if (GetInt(p, "tmap:in:numinputs") == 0) {
 
1443
            p = nextSibling(p);
 
1444
            continue;
 
1445
          }
1442
1446
          assert(0 <= argno && argno < max_num_of_arguments);
1443
1447
          String *&pname = arg_names[argno];
1444
1448
          const char *pname_cstr = GetChar(p, "name");
1749
1753
      }
1750
1754
 
1751
1755
      Printf(output, "\n");
1752
 
      if (wrapperType == memberfn || newobject) {
 
1756
      // If it's a member function or a class constructor...
 
1757
      if (wrapperType == memberfn || (newobject && current_class)) {
1753
1758
        Printf(output, "\tfunction %s(%s) {\n", methodname, args);
1754
1759
        // We don't need this code if the wrapped class has a copy ctor
1755
1760
        // since the flat function new_CLASSNAME will handle it for us.
1857
1862
 
1858
1863
    /* First link C variables to PHP */
1859
1864
 
1860
 
    tm = Swig_typemap_lookup_new("varinit", n, name, 0);
 
1865
    tm = Swig_typemap_lookup("varinit", n, name, 0);
1861
1866
    if (tm) {
1862
1867
      Replaceall(tm, "$target", name);
1863
1868
      Printf(s_vinit, "%s\n", tm);
1867
1872
 
1868
1873
    /* Now generate PHP -> C sync blocks */
1869
1874
    /*
1870
 
       tm = Swig_typemap_lookup_new("varin", n, name, 0);
 
1875
       tm = Swig_typemap_lookup("varin", n, name, 0);
1871
1876
       if(tm) {
1872
1877
       Replaceall(tm, "$symname", iname);
1873
1878
       Printf(f_c->code, "%s\n", tm);
1880
1885
    /*
1881
1886
       if(!GetFlag(n,"feature:immutable")) {
1882
1887
 
1883
 
       tm = Swig_typemap_lookup_new("varout", n, name, 0);
 
1888
       tm = Swig_typemap_lookup("varout", n, name, 0);
1884
1889
       if(tm) {
1885
1890
       Replaceall(tm, "$symname", iname);
1886
1891
       Printf(f_php->code, "%s\n", tm);
1910
1915
 
1911
1916
    SwigType_remember(type);
1912
1917
 
1913
 
    if ((tm = Swig_typemap_lookup_new("consttab", n, name, 0))) {
 
1918
    if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) {
1914
1919
      Replaceall(tm, "$source", value);
1915
1920
      Replaceall(tm, "$target", name);
1916
1921
      Replaceall(tm, "$value", value);
1947
1952
      } else {
1948
1953
        if (!s_fakeoowrappers)
1949
1954
          s_fakeoowrappers = NewStringEmpty();
1950
 
        Printf(s_fakeoowrappers, "\n\tconst %s = %s;\n", name, set_to);
 
1955
        Printf(s_fakeoowrappers, "\n\tconst %s = %s;\n", iname, set_to);
1951
1956
      }
1952
1957
    }
1953
1958
 
2321
2326
        base.item = NULL;
2322
2327
      }
2323
2328
 
2324
 
      if (Getattr(n, "abstract")) {
 
2329
      if (Getattr(n, "abstract") && !GetFlag(n, "feature:notabstract")) {
2325
2330
        Printf(s_phpclasses, "abstract ");
2326
2331
      }
2327
2332
 
2495
2500
    // is made.
2496
2501
    int assignable = is_assignable(n);
2497
2502
    if (assignable) {
2498
 
      String *tm = Swig_typemap_lookup_new("globalin", n, name, 0);
 
2503
      String *tm = Swig_typemap_lookup("globalin", n, name, 0);
2499
2504
      if (!tm && SwigType_isarray(type)) {
2500
2505
        assignable = 0;
2501
2506
      }
2554
2559
    return SWIG_OK;
2555
2560
  }
2556
2561
 
2557
 
 
2558
 
  String * SwigToPhpType(SwigType *t, String_or_char *pname, int shadow_flag) {
2559
 
    String *ptype = 0;
2560
 
 
2561
 
    if (shadow_flag) {
2562
 
      ptype = PhpTypeFromTypemap((char *) "pstype", t, pname, (char *) "");
2563
 
    }
2564
 
    if (!ptype) {
2565
 
      ptype = PhpTypeFromTypemap((char *) "ptype", t, pname, (char *) "");
2566
 
    }
2567
 
 
2568
 
    if (ptype) return ptype;
 
2562
  String * GetShadowReturnType(Node *n) {
 
2563
    SwigType *t = Getattr(n, "type");
2569
2564
 
2570
2565
    /* Map type here */
2571
2566
    switch (SwigType_type(t)) {
2587
2582
      case T_POINTER:
2588
2583
      case T_REFERENCE:
2589
2584
      case T_USER:
2590
 
        if (shadow_flag && is_shadow(t)) {
 
2585
        if (is_shadow(t)) {
2591
2586
          return NewString(Char(is_shadow(t)));
2592
2587
        }
2593
2588
        break;
2595
2590
        /* TODO */
2596
2591
        break;
2597
2592
      default:
2598
 
        Printf(stderr, "SwigToPhpType: unhandled data type: %s\n", SwigType_str(t, 0));
 
2593
        Printf(stderr, "GetShadowReturnType: unhandled data type: %s\n", SwigType_str(t, 0));
2599
2594
        break;
2600
2595
    }
2601
2596
 
2602
2597
    return NewStringEmpty();
2603
2598
  }
2604
2599
 
2605
 
  String *PhpTypeFromTypemap(char *op, SwigType *t, String_or_char *pname, String_or_char *lname) {
2606
 
    String *tms;
2607
 
    tms = Swig_typemap_lookup(op, t, pname, lname, (char *) "", (char *) "", NULL);
2608
 
    if (!tms) {
2609
 
      return NULL;
2610
 
    }
2611
 
 
2612
 
    char *start = Char(tms);
2613
 
    while (isspace(*start) || *start == '{') {
2614
 
      start++;
2615
 
    }
2616
 
    char *end = start;
2617
 
    while (*end && *end != '}') {
2618
 
      end++;
2619
 
    }
2620
 
    return NewStringWithSize(start, end - start);
 
2600
  String *PhpTypeFromTypemap(char *op, Node *n, String_or_char *lname) {
 
2601
    String *tms = Swig_typemap_lookup(op, n, lname, 0);
 
2602
    if (!tms)
 
2603
      return 0;
 
2604
    else
 
2605
      return NewStringf("%s", tms);
2621
2606
  }
2622
2607
 
2623
2608
  int abstractConstructorHandler(Node *n) {
2679
2664
  int CreateZendListDestructor(Node *n) {
2680
2665
    String *name = GetChar(Swig_methodclass(n), "name");
2681
2666
    String *iname = GetChar(n, "sym:name");
2682
 
    SwigType *d = Getattr(n, "type");
2683
2667
    ParmList *l = Getattr(n, "parms");
2684
2668
 
2685
2669
    String *destructorname = NewStringEmpty();
2686
2670
    Printf(destructorname, "_%s", Swig_name_wrapper(iname));
2687
2671
    Setattr(classnode, "destructor", destructorname);
2688
2672
 
2689
 
    Wrapper *df = NewWrapper();
2690
 
    Printf(df->def, "/* This function is designed to be called by the zend list destructors */\n");
2691
 
    Printf(df->def, "/* to typecast and do the actual destruction */\n");
2692
 
    Printf(df->def, "static void %s(zend_rsrc_list_entry *rsrc, const char *type_name TSRMLS_DC) {\n", destructorname);
2693
 
 
2694
 
    Wrapper_add_localv(df, "value", "swig_object_wrapper *value=(swig_object_wrapper *) rsrc->ptr", NIL);
2695
 
    Wrapper_add_localv(df, "ptr", "void *ptr=value->ptr", NIL);
2696
 
    Wrapper_add_localv(df, "newobject", "int newobject=value->newobject", NIL);
2697
 
 
2698
 
    emit_args(d, l, df);
2699
 
    emit_attach_parmmaps(l, df);
 
2673
    Wrapper *f = NewWrapper();
 
2674
    Printf(f->def, "/* This function is designed to be called by the zend list destructors */\n");
 
2675
    Printf(f->def, "/* to typecast and do the actual destruction */\n");
 
2676
    Printf(f->def, "static void %s(zend_rsrc_list_entry *rsrc, const char *type_name TSRMLS_DC) {\n", destructorname);
 
2677
 
 
2678
    Wrapper_add_localv(f, "value", "swig_object_wrapper *value=(swig_object_wrapper *) rsrc->ptr", NIL);
 
2679
    Wrapper_add_localv(f, "ptr", "void *ptr=value->ptr", NIL);
 
2680
    Wrapper_add_localv(f, "newobject", "int newobject=value->newobject", NIL);
 
2681
 
 
2682
    emit_parameter_variables(l, f);
 
2683
    emit_attach_parmmaps(l, f);
2700
2684
 
2701
2685
    // Get type of first arg, thing to be destructed
2702
2686
    // Skip ignored arguments
2707
2691
    }
2708
2692
    SwigType *pt = Getattr(p, "type");
2709
2693
 
2710
 
    Printf(df->code, "  efree(value);\n");
2711
 
    Printf(df->code, "  if (! newobject) return; /* can't delete it! */\n");
2712
 
    Printf(df->code, "  arg1 = (%s)SWIG_ZTS_ConvertResourceData(ptr,type_name,SWIGTYPE%s TSRMLS_CC);\n", SwigType_lstr(pt, 0), SwigType_manglestr(pt));
2713
 
    Printf(df->code, "  if (! arg1) zend_error(E_ERROR, \"%s resource already free'd\");\n", Char(name));
 
2694
    Printf(f->code, "  efree(value);\n");
 
2695
    Printf(f->code, "  if (! newobject) return; /* can't delete it! */\n");
 
2696
    Printf(f->code, "  arg1 = (%s)SWIG_ZTS_ConvertResourceData(ptr,type_name,SWIGTYPE%s TSRMLS_CC);\n", SwigType_lstr(pt, 0), SwigType_manglestr(pt));
 
2697
    Printf(f->code, "  if (! arg1) zend_error(E_ERROR, \"%s resource already free'd\");\n", Char(name));
2714
2698
 
2715
2699
    Setattr(n, "wrap:name", destructorname);
2716
2700
 
2717
 
    emit_action(n, df);
2718
 
 
2719
 
    Printf(df->code, "}\n");
2720
 
 
2721
 
    Wrapper_print(df, s_wrappers);
 
2701
    String *actioncode = emit_action(n);
 
2702
    Append(f->code, actioncode);
 
2703
    Delete(actioncode);
 
2704
 
 
2705
    Printf(f->code, "}\n");
 
2706
 
 
2707
    Wrapper_print(f, s_wrappers);
2722
2708
 
2723
2709
    return SWIG_OK;
2724
2710