diff --git a/code_generation/code_generation.pri b/code_generation/code_generation.pri index b33a7cd..da060f4 100644 --- a/code_generation/code_generation.pri +++ b/code_generation/code_generation.pri @@ -11,6 +11,7 @@ HEADERS += \ $$PWD/namer.h \ $$PWD/printer.h \ $$PWD/statemachine.h \ + $$PWD/struct.h \ $$PWD/style.h \ $$PWD/typedef.h \ $$PWD/variable.h @@ -27,6 +28,7 @@ SOURCES += \ $$PWD/namer.cpp \ $$PWD/printer.cpp \ $$PWD/statemachine.cpp \ + $$PWD/struct.cpp \ $$PWD/style.cpp \ $$PWD/typedef.cpp \ $$PWD/variable.cpp diff --git a/code_generation/file.cpp b/code_generation/file.cpp index c12cb45..ea90bb7 100644 --- a/code_generation/file.cpp +++ b/code_generation/file.cpp @@ -41,6 +41,7 @@ class File::Private Class::List mClasses; Variable::List mFileVariables; Function::List mFileFunctions; + Struct::List mStructs; Enum::List mFileEnums; QStringList mExternCDeclarations; Code mFileCode; @@ -245,6 +246,16 @@ Enum::List File::fileEnums() const return d->mFileEnums; } +void File::addStruct(const Struct &structValue) +{ + d->mStructs.append(structValue); +} + +Struct::List File::structs() const +{ + return d->mStructs; +} + void File::addExternCDeclaration(const QString &externalCDeclaration) { d->mExternCDeclarations.append(externalCDeclaration); diff --git a/code_generation/file.h b/code_generation/file.h index 34a0cdb..f6a521e 100644 --- a/code_generation/file.h +++ b/code_generation/file.h @@ -22,6 +22,7 @@ #define KODE_FILE_H #include "class.h" +#include "struct.h" #include "code.h" #include "license.h" #include "variable.h" @@ -207,6 +208,16 @@ class KODE_EXPORT File */ Enum::List fileEnums() const; + /** + * Adds a file struct to the file. + */ + void addStruct(const KODE::Struct &structValue); + + /** + * Returns the list of all file enums. + */ + KODE::Struct::List structs() const; + /** * Adds an external C declaration to the file. */ diff --git a/code_generation/printer.cpp b/code_generation/printer.cpp index 2369907..34571d8 100644 --- a/code_generation/printer.cpp +++ b/code_generation/printer.cpp @@ -735,6 +735,13 @@ void Printer::printHeader(const File &file) (*enumIt).printDeclaration(out); } + // Create structs + Struct::List structs = file.structs(); + Struct::List::ConstIterator structIt; + for (structIt = structs.constBegin(); structIt != structs.constEnd(); ++structIt) { + (*structIt).printDeclaration(out); + } + // Create forward declarations QSet processed; Class::List::ConstIterator it; diff --git a/code_generation/struct.cpp b/code_generation/struct.cpp new file mode 100644 index 0000000..515a9c7 --- /dev/null +++ b/code_generation/struct.cpp @@ -0,0 +1,81 @@ +/* + This file is part of libkode + + Copyright (c) 2025 Miklos Marton + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#include "struct.h" + +#include "code.h" + +using namespace KODE; + +class Struct::Private +{ +public: + QString mName; + bool mTypedef = false; + Variable::List mMembers; +}; + +Struct::Struct() : d(new Private) {} + +void Struct::setTypedef(bool typeDef) +{ + d->mTypedef = typeDef; +} + +void Struct::setName(const QString &name) +{ + d->mName = name; +} + +QString Struct::name() const +{ + return d->mName; +} + +void Struct::addMemberVariable(const Variable &variable) +{ + d->mMembers.append(variable); +} + +Variable::List Struct::memberVariables() const +{ + return d->mMembers; +} + +void Struct::printDeclaration(KODE::Code &code) const +{ + if (d->mTypedef) + code.addLine(QStringLiteral("typedef struct {")); + else + code.addLine(QStringLiteral("struct %1 {").arg(d->mName)); + code.indent(); + + for (const auto &member : d->mMembers) { + code.addLine(QString("%1 %2 %3;") + .arg(member.isStatic() ? "static" : "", member.type(), member.name())); + } + code.unindent(); + if (d->mTypedef) + code.addLine(QString("} %1;").arg(d->mName)); + else + code.addLine("};"); + + code.newLine(); +} diff --git a/code_generation/struct.h b/code_generation/struct.h new file mode 100644 index 0000000..3e44d0c --- /dev/null +++ b/code_generation/struct.h @@ -0,0 +1,69 @@ +/* + This file is part of libkode + +Copyright (c) 2025 Miklos Marton + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public License +along with this library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ +#pragma once + +#include +#include "variable.h" +#include "code.h" + +#include + +namespace KODE { + +class KODE_EXPORT Struct +{ +public: + typedef QList List; + + Struct(); + + void setTypedef(bool typeDef = true); + /** + * Sets the @param name of the class object. + */ + void setName(const QString &name); + + /** + * Returns the name of the class object. + */ + QString name() const; + + /** + * Adds a member @param variable to the class object. + */ + void addMemberVariable(const KODE::Variable &variable); + + /** + * Returns the list of all member variables. + */ + KODE::Variable::List memberVariables() const; + + /** + * Prints the declaration of the enum to the code + */ + void printDeclaration(KODE::Code &code) const; + +private: + class Private; + Private *d; +}; + +}