aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Lima <hugo.lima@openbossa.org>2009-10-20 10:08:26 -0200
committerLuciano Miguel Wolf <luciano.wolf@indt.org.br>2009-10-21 16:34:20 -0300
commit73f6ac4faafed5c8c275ff01a22a53bfbc7fc515 (patch)
treec69bcb1929325b040b3983a24fc349577ec79ab3
parentac27d38d44bef714b08d8b675ae105ac41047bfa (diff)
Implemented a signature parser for the AddedFunction class.
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/testaddfunction.cpp80
-rw-r--r--tests/testaddfunction.h36
-rw-r--r--typesystem.cpp79
-rw-r--r--typesystem.h35
5 files changed, 213 insertions, 18 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 43a4d51e3..a9d9ddc45 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -11,3 +11,4 @@ declare_test(testabstractmetaclass)
declare_test(testabstractmetatype)
declare_test(testenum)
declare_test(testmodifydocumentation)
+declare_test(testaddfunction)
diff --git a/tests/testaddfunction.cpp b/tests/testaddfunction.cpp
new file mode 100644
index 000000000..a47cd28af
--- /dev/null
+++ b/tests/testaddfunction.cpp
@@ -0,0 +1,80 @@
+/*
+* This file is part of the API Extractor project.
+*
+* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+*
+* Contact: PySide team <contact@pyside.org>
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* version 2 as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA
+*
+*/
+
+#include "testaddfunction.h"
+#include <QtTest/QTest>
+#include "testutil.h"
+
+
+void TestAddFunction::testParsingFuncNameAndConstness()
+{
+ const char sig1[] = "func(type1, const type2, const type3* const)";
+ AddedFunction f1(sig1, "void");
+ QCOMPARE(f1.name(), QString("func"));
+ AddedFunction::TypeInfo retval = f1.returnType();
+ QCOMPARE(retval.name, QString("void"));
+ QCOMPARE(retval.indirections, 0);
+ QCOMPARE(retval.isConst, false);
+ QCOMPARE(retval.isRef, false);
+
+ const char sig2[] = " _fu__nc_ ( type1, const type2, const Abc<int& , C<char*> * > * *, const type3* const ) const ";
+ AddedFunction f2(sig2, "const Abc<int& , C<char*> * > * *");
+ QCOMPARE(f2.name(), QString("_fu__nc_"));
+ retval = f2.returnType();
+ QCOMPARE(retval.name, QString("Abc<int& , C<char*> * >"));
+ QCOMPARE(retval.indirections, 2);
+ QCOMPARE(retval.isConst, true);
+ QCOMPARE(retval.isRef, false);
+ QList< AddedFunction::TypeInfo > args = f2.arguments();
+ QCOMPARE(args.count(), 4);
+ retval = args[2];
+ QCOMPARE(retval.name, QString("Abc<int& , C<char*> * >"));
+ QCOMPARE(retval.indirections, 2);
+ QCOMPARE(retval.isConst, true);
+ QCOMPARE(retval.isRef, false);
+
+
+}
+
+void TestAddFunction::testAddFunction()
+{
+#if 0
+ const char cppCode[] = "struct A { void a(); };";
+ const char xmlCode[] = "\
+ <typesystem package=\"Foo\"> \
+ <value-type name='A'> \
+ <add-function signature='b(int, float)' return-type='int' acess='protected'>\
+ </add-function>\
+ </value-type>\
+ </typesystem>";
+ TestUtil t(cppCode, xmlCode);
+ AbstractMetaClassList classes = t.builder()->classes();
+ AbstractMetaClass* classA = classes.findClass("A");
+ QVERIFY(classA);
+ QCOMPARE(classA->functions().count(), 2); // default ctor and the added function
+#endif
+}
+
+QTEST_APPLESS_MAIN(TestAddFunction)
+
+#include "testaddfunction.moc"
diff --git a/tests/testaddfunction.h b/tests/testaddfunction.h
new file mode 100644
index 000000000..7fe07d6a4
--- /dev/null
+++ b/tests/testaddfunction.h
@@ -0,0 +1,36 @@
+/*
+* This file is part of the API Extractor project.
+*
+* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+*
+* Contact: PySide team <contact@pyside.org>
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* version 2 as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA
+*
+*/
+
+#ifndef TESTADDFUNCTION_H
+#define TESTADDFUNCTION_H
+#include <QObject>
+
+class TestAddFunction : public QObject
+{
+ Q_OBJECT
+private slots:
+ void testParsingFuncNameAndConstness();
+ void testAddFunction();
+};
+
+#endif \ No newline at end of file
diff --git a/typesystem.cpp b/typesystem.cpp
index b58c42152..e10ff380a 100644
--- a/typesystem.cpp
+++ b/typesystem.cpp
@@ -1281,7 +1281,7 @@ bool Handler::startElement(const QString &, const QString &n,
return false;
}
- AddedFunction func(signature);
+ AddedFunction func(signature, attributes["return-value"]);
m_currentSignature = signature;
QString access = attributes["access"].toLower();
@@ -2039,10 +2039,83 @@ QString FunctionModification::toString() const
return str;
}
-AddedFunction::AddedFunction(QString signature, TypeEntry* returnType)
- : m_returnType(returnType)
+static AddedFunction::TypeInfo parseType(const QString& signature, int startPos = 0, int* endPos = 0)
{
+ AddedFunction::TypeInfo result;
+ QRegExp regex("\\w");
+ int length = signature.length();
+ int start = signature.indexOf(regex, startPos);
+ if (start == -1) { // error
+ if (endPos)
+ *endPos = length;
+ return result;
+ }
+
+ int cantStop = 0;
+ QString paramString;
+ QChar c;
+ int i = start;
+ for (; i < length; ++i) {
+ c = signature[i];
+ if (c == '<')
+ cantStop++;
+ if (c == '>')
+ cantStop--;
+ if (cantStop < 0)
+ break; // FIXME: report error?
+ if ((c == ')' || c == ',') && !cantStop)
+ break;
+ paramString += signature[i];
+ }
+ if (endPos)
+ *endPos = i;
+
+ // Check default value
+ if (paramString.contains('=')) {
+ QStringList lst = paramString.split('=');
+ paramString = lst[0].trimmed();
+ result.defaultValue = lst[1].trimmed();
+ }
+
+ // check constness
+ if (paramString.startsWith("const ")) {
+ result.isConst = true;
+ paramString.remove(0, sizeof("const")/sizeof(char));
+ paramString = paramString.trimmed();
+ }
+ // check reference
+ if (paramString.endsWith("&")) {
+ result.isRef = true;
+ paramString.chop(1);
+ paramString = paramString.trimmed();
+ }
+ // check Indirections
+ while (paramString.endsWith("*")) {
+ result.indirections++;
+ paramString.chop(1);
+ paramString = paramString.trimmed();
+ }
+ result.name = paramString;
+
+ return result;
+}
+AddedFunction::AddedFunction(QString signature, QString returnType) : m_access(Public)
+{
+ m_returnType = parseType(returnType);
+ signature = signature.trimmed();
+ int endPos = signature.indexOf('(');
+ m_name = signature.left(endPos).trimmed();
+ int signatureLength = signature.length();
+ while (endPos < signatureLength) {
+ TypeInfo arg = parseType(signature, endPos, &endPos);
+ m_arguments.append(arg);
+ // end of parameters...
+ if (signature[endPos] == ')')
+ break;
+ }
+ // is const?
+ m_isConst = signature.right(signatureLength - endPos).contains("const");
}
/*
diff --git a/typesystem.h b/typesystem.h
index 8a8f7277b..7f232ac93 100644
--- a/typesystem.h
+++ b/typesystem.h
@@ -35,7 +35,6 @@ class Indentor;
class AbstractMetaType;
class QTextStream;
-class TypeEntry;
class EnumTypeEntry;
class FlagsTypeEntry;
@@ -440,22 +439,27 @@ struct FieldModification: public Modification
QString name;
};
+
typedef QList<FieldModification> FieldModificationList;
struct AddedFunction
{
-
enum Access {
Private = 0x1,
Protected = 0x2,
Public = 0x3
};
- // ArgumentPair.first: argument name
- // ArgumentPair.second: default value
- typedef QPair<QString, QString> ArgumentPair;
+ struct TypeInfo {
+ TypeInfo() : isConst(false), indirections(0), isRef(false) {}
+ QString name;
+ bool isConst;
+ int indirections;
+ bool isRef;
+ QString defaultValue;
+ };
- AddedFunction(QString signature, TypeEntry* returnType = 0);
+ AddedFunction(QString signature, QString returnType);
QString name() const
{
@@ -472,12 +476,7 @@ struct AddedFunction
return m_access;
}
- void setReturnType(TypeEntry* returnType)
- {
- m_returnType = returnType;
- }
-
- TypeEntry* returnType() const
+ TypeInfo returnType() const
{
return m_returnType;
}
@@ -497,16 +496,21 @@ struct AddedFunction
m_codeSnips << codeSnip;
}
- QList<QPair<ArgumentPair, const TypeEntry*> > arguments()
+ QList<TypeInfo> arguments() const
{
return m_arguments;
}
+ bool isConst() const
+ {
+ return m_isConst;
+ }
private:
QString m_name;
Access m_access;
- QList<QPair<ArgumentPair, const TypeEntry*> > m_arguments;
- TypeEntry* m_returnType;
+ QList<TypeInfo> m_arguments;
+ TypeInfo m_returnType;
+ bool m_isConst;
CodeSnipList m_codeSnips;
};
typedef QList<AddedFunction> AddedFunctionList;
@@ -595,6 +599,7 @@ public:
ArrayType,
TypeSystemType,
CustomType,
+ TargetLangType
};
enum CodeGeneration {