Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions code_generation/code_generation.pri
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 11 additions & 0 deletions code_generation/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions code_generation/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define KODE_FILE_H

#include "class.h"
#include "struct.h"
#include "code.h"
#include "license.h"
#include "variable.h"
Expand Down Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/enums/structs/ ?

*/
KODE::Struct::List structs() const;

/**
* Adds an external C declaration to the file.
*/
Expand Down
7 changes: 7 additions & 0 deletions code_generation/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString> processed;
Class::List::ConstIterator it;
Expand Down
81 changes: 81 additions & 0 deletions code_generation/struct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
This file is part of libkode

Copyright (c) 2025 Miklos Marton <martonmiklosqdev@gmail.com>

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();
}
69 changes: 69 additions & 0 deletions code_generation/struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
This file is part of libkode

Copyright (c) 2025 Miklos Marton <martonmiklosqdev@gmail.com>

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 <kode_export.h>
#include "variable.h"
#include "code.h"

#include <QString>

namespace KODE {

class KODE_EXPORT Struct
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to flag a Class as being a struct, to avoid the duplication between Class and Struct?
In C++ all the features of a class are available in structs, including inheritance, visibility....

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to flag a Class as being a struct, to avoid the duplication between Class and Struct?

Makes sense, let me rework the code in that way.

{
public:
typedef QList<Struct> 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;
};

}