[cig-commits] r6933 - cs/babel/trunk

leif at geodynamics.org leif at geodynamics.org
Mon May 21 11:46:21 PDT 2007


Author: leif
Date: 2007-05-21 11:46:20 -0700 (Mon, 21 May 2007)
New Revision: 6933

Modified:
   cs/babel/trunk/babel.cc
Log:
Generate 'tp_dealloc' method (call C++ destructor).


Modified: cs/babel/trunk/babel.cc
===================================================================
--- cs/babel/trunk/babel.cc	2007-05-19 22:38:21 UTC (rev 6932)
+++ cs/babel/trunk/babel.cc	2007-05-21 18:46:20 UTC (rev 6933)
@@ -50,11 +50,12 @@
     void generatePostamble();
     void generateModInitFunc();
 
-    virtual bool visitExpression(Expression *obj);    virtual bool visitDeclarator(Declarator *obj);
+    virtual bool visitDeclarator(Declarator *obj);
     virtual bool visitFunction(Function *f);
     
     bool visitFunctionVariable(Variable *var);
     void writePyMethod(Variable *var);
+    void writeDeallocMethod(CompoundType *ct);
     void scanFunctionParams(Variable *var,
                             std::string &format,
                             std::string &argnames,
@@ -67,7 +68,11 @@
     virtual void postvisitTypeSpecifier(TypeSpecifier *ts);
     void generateTypeObject(CompoundType *ct);
     bool isDiscardedFunction(Variable *var);
-    std::string mangledMemberFuncName(Variable *var);
+
+    std::string tpName(std::string funcName, CompoundType *ct);
+    std::string mangledMemberFuncName(Variable *var, std::string ns = "m");
+    std::string mangledMemberFuncName(Variable *var, std::string funcName, std::string ns = "m");
+    std::string mangledMemberFuncName(CompoundType *ct, std::string funcName, std::string ns);
     
 };
 
@@ -175,23 +180,6 @@
 }
 
 
-bool PyExtModuleGenerator::visitExpression(Expression *obj)
-{
-#if 0
-    ASTSWITCH(Expression, obj) {
-        ASTCASE(E_variable, e) {
-            //tryHit(e->var, e->name->loc, "use as variable");
-        }
-        ASTNEXT(E_fieldAcc, e) {
-            //tryHit(e->field, e->fieldName->loc, "use as field");
-        }
-        ASTENDCASED
-            }
-#endif
-    return true;
-}
-
-
 bool PyExtModuleGenerator::visitDeclarator(Declarator *obj)
 {
     Variable *var = obj->var;
@@ -293,6 +281,18 @@
 }
 
 
+void PyExtModuleGenerator::writeDeallocMethod(CompoundType *ct)
+{
+    std::string dealloc = tpName("dealloc", ct);
+    cout << "static void " << dealloc << "(PyObject *self) {" << endl
+         << "    " << ct->toCString() << " *_this = ((struct __babel_obj_" << ct->name << " *)self)->_this;" << endl
+         << "    delete _this;" << endl
+         << "    (*self->ob_type->tp_free)(self);" << endl
+         << "}" << endl
+         << endl;
+}
+
+
 void PyExtModuleGenerator::scanFunctionParams(Variable *var,
                                               std::string &format,
                                               std::string &argnames,
@@ -530,6 +530,7 @@
             cout << "    {0, 0, 0, 0}" << endl
                  << "};" << endl
                  << endl;
+            this->writeDeallocMethod(ct);
             this->generateTypeObject(ct);
             break;
         default:
@@ -545,6 +546,7 @@
 
 void PyExtModuleGenerator::generateTypeObject(CompoundType *ct)
 {
+    std::string dealloc = tpName("dealloc", ct);
     cout <<
         "PyTypeObject __babel_type_" << ct->name << " = {\n"
         "    PyObject_HEAD_INIT(0)\n"
@@ -552,7 +554,7 @@
         "    \"" << name << "." << ct->name << "\", /*tp_name*/\n"
         "    sizeof(struct __babel_obj_" << ct->name << "), /*tp_basicsize*/\n"
         "    0, /*tp_itemsize*/\n"
-        "    0,\n" //__babel_tp_dealloc_" << ct->name << ", /*tp_dealloc*/\n"
+        "    " << dealloc << ", /*tp_dealloc*/\n"
         "    0, /*tp_print*/\n"
         "    0, /*tp_getattr*/\n"
         "    0, /*tp_setattr*/\n"
@@ -613,12 +615,29 @@
 }
 
 
-std::string PyExtModuleGenerator::mangledMemberFuncName(Variable *var)
+std::string PyExtModuleGenerator::tpName(std::string funcName, CompoundType *ct)
 {
-    std::string className = var->scope->curCompound->typedefVar->fullyQualifiedName().c_str();
-    std::string funcName = var->name;
+    return this->mangledMemberFuncName(ct, funcName, "tp");
+}
+
+
+std::string PyExtModuleGenerator::mangledMemberFuncName(Variable *var, std::string ns)
+{
+    return this->mangledMemberFuncName(var, var->name, ns);
+}
+
+
+std::string PyExtModuleGenerator::mangledMemberFuncName(Variable *var, std::string funcName, std::string ns)
+{
+    return this->mangledMemberFuncName(var->scope->curCompound, funcName, ns);
+}
+
+
+std::string PyExtModuleGenerator::mangledMemberFuncName(CompoundType *ct, std::string funcName, std::string ns)
+{
+    std::string className = ct->typedefVar->fullyQualifiedName().c_str();
     std::stringstream s;
-    s << "__babel_m_" << className.length();
+    s << "__babel_" << ns << "_" << className.length();
     for (std::string::const_iterator i = className.begin(); i != className.end(); ++i) {
         char c = *i;
         if (c == ':') {
@@ -739,4 +758,3 @@
 
 
 // end of file
-



More information about the cig-commits mailing list