[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/bindings/smokegenerator
From:       Arno Rehn <kde () arnorehn ! de>
Date:       2009-06-30 21:22:21
Message-ID: 1246396941.736841.21073.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 989765 by arnorehn:

add overloads for methods with default parameters


 M  +2 -0      generators/smoke/globals.h  
 M  +38 -1     generators/smoke/helpers.cpp  
 M  +12 -0     generators/smoke/writeClasses.cpp  
 M  +8 -3      generatorvisitor.cpp  
 M  +8 -5      type.h  


--- trunk/playground/bindings/smokegenerator/generators/smoke/globals.h \
#989764:989765 @@ -89,6 +89,7 @@
 {
     static QHash<QString, QString> typeMap;
     static QHash<const Method*, const Function*> globalFunctionMap;
+    static QHash<const Method*, QStringList> defaultParameterValues;
     
     static QList<const Class*> superClassList(const Class* klass);
     static QList<const Class*> descendantsList(const Class* klass);
@@ -103,6 +104,7 @@
     static void addDefaultConstructor(Class* klass);
     static void addCopyConstructor(Class* klass);
     static void addDestructor(Class* klass);
+    static void addOverloads(const Method& meth);
 
     static QString mungedName(const Method&);
     
--- trunk/playground/bindings/smokegenerator/generators/smoke/helpers.cpp \
#989764:989765 @@ -26,6 +26,7 @@
 
 QHash<QString, QString> Util::typeMap;
 QHash<const Method*, const Function*> Util::globalFunctionMap;
+QHash<const Method*, QStringList> Util::defaultParameterValues;
 
 QList<const Class*> Util::superClassList(const Class* klass)
 {
@@ -68,11 +69,17 @@
     // add all functions as methods to a class called 'QGlobalSpace'
     for (QHash<QString, Function>::const_iterator it = functions.constBegin(); it != \
functions.constEnd(); it++) {  const Function& fn = it.value();
+        
+        // gcc doesn't like this function... for whatever reason
+        if (fn.name() == "_IO_ftrylockfile")
+            continue;
+        
         Method meth = Method(&globalSpace, fn.name(), fn.type(), Access_public, \
fn.parameters());  meth.setFlag(Method::Static);
         globalSpace.appendMethod(meth);
         // map this method to the function, so we can later retrieve the header it \
was defined in  globalFunctionMap[&globalSpace.methods().last()] = &fn;
+        
         (*usedTypes) << meth.type();
         foreach (const Parameter& param, meth.parameters())
             (*usedTypes) << param.type();
@@ -95,6 +102,7 @@
         foreach (const Method& m, klass.methods()) {
             if (m.access() == Access_private)
                 continue;
+            addOverloads(m);
             (*usedTypes) << m.type();
             foreach (const Parameter& param, m.parameters())
                 (*usedTypes) << param.type();
@@ -273,7 +281,7 @@
         if (type->pointerDepth() > 1) {
             // reference to array or hash
             ret += "?";
-        } else if (type->isIntegral()|| type->getEnum()) {
+        } else if (type->isIntegral() || type->getEnum()) {
             // plain scalar
             ret += "$";
         } else if (type->getClass()) {
@@ -364,6 +372,35 @@
     return true;
 }
 
+void Util::addOverloads(const Method& meth)
+{
+    ParameterList params;
+    Class* klass = meth.getClass();
+    
+    for (int i = 0; i < meth.parameters().count(); i++) {
+        const Parameter& param = meth.parameters()[i];
+        if (!param.isDefault()) {
+            params << param;
+            continue;
+        }
+        Method overload = meth;
+        overload.setParameterList(params);
+        klass->appendMethod(overload);
+        
+        QStringList remainingDefaultValues;
+        for (int j = i; j < meth.parameters().count(); j++) {
+            const Parameter defParam = meth.parameters()[j];
+            QString cast = "(";
+            cast += defParam.type()->toString() + ')';
+            cast += defParam.defaultValue();
+            remainingDefaultValues << cast;
+        }
+        defaultParameterValues[&klass->methods().last()] = remainingDefaultValues;
+        
+        params << param;
+    }
+}
+
 // checks if method meth is overriden in class klass or any of its superclasses
 const Method* Util::isVirtualOverriden(const Method& meth, const Class* klass)
 {
--- trunk/playground/bindings/smokegenerator/generators/smoke/writeClasses.cpp \
#989764:989765 @@ -92,6 +92,7 @@
     out << QString("void x_%1(Smoke::Stack x) {\n").arg(index);
     out << "        // " << meth.toString() << "\n";
     out << "        ";
+    
     if (meth.isConstructor()) {
         out << smokeClassName << "* xret = new " << smokeClassName << "(";
     } else {
@@ -118,6 +119,7 @@
         }
         out << meth.name() << "(";
     }
+    
     for (int j = 0; j < meth.parameters().count(); j++) {
         const Parameter& param = meth.parameters()[j];
         
@@ -137,12 +139,22 @@
         if (param.type()->isRef()) typeName.replace('&', "");
         out << "(" << typeName << ")" << "x[" << j + 1 << "]." << field;
     }
+    
+    // if the method has any other default parameters, append them here as values, \
so  +    QStringList defaultParams = Util::defaultParameterValues[&meth];
+    if (!defaultParams.isEmpty()) {
+        if (meth.parameters().count() > 0)
+            out << "," ;
+        out << defaultParams.join(",");
+    }
+    
     out << ");\n";
     if (meth.type() != Type::Void) {
         out << "        x[0]." << Util::stackItemField(meth.type()) << " = " << \
Util::assignmentString(meth.type(), "xret") << ";\n";  } else {
         out << "        (void)x; // noop (for compiler warning)\n";
     }
+    
     out << "    }\n";
     if (meth.isConstructor()) {
         out << "    explicit " << smokeClassName << '(';
--- trunk/playground/bindings/smokegenerator/generatorvisitor.cpp #989764:989765
@@ -374,11 +374,16 @@
     if (currentTypeRef == Type::Void)
         return;
     
-    bool defaultParameter = node->expression;
+    QString defaultValue;
+    if (node->expression) {
+        // this parameter has a default value
+        for (int i = node->expression->start_token; i < node->expression->end_token; \
i++) +            defaultValue.append(token(i).symbolByteArray());
+    }
     if (inClass)
-        currentMethod.appendParameter(Parameter(name, currentTypeRef, \
defaultParameter)); +        currentMethod.appendParameter(Parameter(name, \
currentTypeRef, defaultValue));  else
-        currentFunction.appendParameter(Parameter(name, currentTypeRef, \
defaultParameter)); +        currentFunction.appendParameter(Parameter(name, \
currentTypeRef, defaultValue));  }
 
 void GeneratorVisitor::visitSimpleDeclaration(SimpleDeclarationAST* node)
--- trunk/playground/bindings/smokegenerator/type.h #989764:989765
@@ -207,8 +207,8 @@
 class Parameter
 {
 public:
-    Parameter(const QString& name = QString(), Type* type = 0, bool isDefault = \
                false)
-        : m_name(name), m_type(type), m_default(isDefault) {}
+    Parameter(const QString& name = QString(), Type* type = 0, const QString& \
defaultValue = QString()) +        : m_name(name), m_type(type), \
m_defaultValue(defaultValue) {}  virtual ~Parameter() {}
 
     bool isValid() const { return m_type; }
@@ -219,15 +219,17 @@
     void setType(Type* type) { m_type = type; }
     Type* type() const { return m_type; }
 
-    void setIsDefault(bool isDefault) { m_default = isDefault; }
-    bool isDefault() const { return m_default; }
+    bool isDefault() const { return !m_defaultValue.isEmpty(); }
 
+    QString defaultValue() const { return m_defaultValue; }
+    void setDefaultValue(const QString& value) { m_defaultValue = value; }
+
     QString toString() const;
 
 protected:
     QString m_name;
     Type* m_type;
-    bool m_default;
+    QString m_defaultValue;
 };
 
 typedef QList<Parameter> ParameterList;
@@ -241,6 +243,7 @@
 
     const ParameterList& parameters() const { return m_params; }
     void appendParameter(const Parameter& param) { m_params.append(param); }
+    void setParameterList(const ParameterList& params) { m_params = params; }
 
     void setIsConstructor(bool isCtor) { m_isConstructor = isCtor; }
     bool isConstructor() const { return m_isConstructor; }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic