mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-09-24 01:06:40 +00:00
Add Tests for ZCG cpp
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
#include "ArrayDeclarationModifier.h"
|
||||
|
||||
DeclarationModifierType ArrayDeclarationModifier::GetType() const
|
||||
{
|
||||
return DeclarationModifierType::ARRAY;
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "DeclarationModifier.h"
|
||||
#include "Domain/Evaluation/IEvaluation.h"
|
||||
|
||||
class ArrayDeclarationModifier final : public DeclarationModifier
|
||||
{
|
||||
public:
|
||||
int m_size;
|
||||
|
||||
/**
|
||||
* \brief The array size is not actually given by \c m_size but instead is dynamic.
|
||||
*/
|
||||
std::unique_ptr<IEvaluation> m_dynamic_size_evaluation;
|
||||
|
||||
/**
|
||||
* \brief The array has a size that is given by \c m_size but only a certain dynamic amount is handled by generated count.
|
||||
*/
|
||||
std::unique_ptr<IEvaluation> m_dynamic_count_evaluation;
|
||||
|
||||
DeclarationModifierType GetType() const override;
|
||||
};
|
@@ -0,0 +1,75 @@
|
||||
#include "BaseTypeDefinition.h"
|
||||
|
||||
BaseTypeDefinition::BaseTypeDefinition(std::string name, const unsigned size)
|
||||
: DataDefinition("", std::move(name)),
|
||||
m_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
DataDefinitionType BaseTypeDefinition::GetType() const
|
||||
{
|
||||
return DataDefinitionType::BASE_TYPE;
|
||||
}
|
||||
|
||||
unsigned BaseTypeDefinition::GetAlignment()
|
||||
{
|
||||
// Since this type has no members the alignment is always equal to the size.
|
||||
return m_size;
|
||||
}
|
||||
|
||||
unsigned BaseTypeDefinition::GetAlignment() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
bool BaseTypeDefinition::GetForceAlignment()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BaseTypeDefinition::GetForceAlignment() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned BaseTypeDefinition::GetSize()
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
unsigned BaseTypeDefinition::GetSize() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::FLOAT = new BaseTypeDefinition("float", 4);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::DOUBLE = new BaseTypeDefinition("double", 8);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::BOOL = new BaseTypeDefinition("bool", 1);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::CHAR = new BaseTypeDefinition("char", 1);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::UNSIGNED_CHAR = new BaseTypeDefinition("unsigned char", 1);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::SHORT = new BaseTypeDefinition("short", 2);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::UNSIGNED_SHORT = new BaseTypeDefinition("unsigned short", 2);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::INT = new BaseTypeDefinition("int", 4);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::UNSIGNED_INT = new BaseTypeDefinition("unsigned int", 4);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::LONG = new BaseTypeDefinition("long", 4);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::UNSIGNED_LONG = new BaseTypeDefinition("unsigned long", 4);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::LONG_LONG = new BaseTypeDefinition("long long", 8);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::UNSIGNED_LONG_LONG = new BaseTypeDefinition("unsigned long long", 8);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::VOID = new BaseTypeDefinition("void", 0);
|
||||
const BaseTypeDefinition* const BaseTypeDefinition::ALL_BASE_TYPES[]
|
||||
{
|
||||
FLOAT,
|
||||
DOUBLE,
|
||||
BOOL,
|
||||
CHAR,
|
||||
UNSIGNED_CHAR,
|
||||
SHORT,
|
||||
UNSIGNED_SHORT,
|
||||
INT,
|
||||
UNSIGNED_INT,
|
||||
LONG,
|
||||
UNSIGNED_LONG,
|
||||
LONG_LONG,
|
||||
UNSIGNED_LONG_LONG,
|
||||
VOID
|
||||
};
|
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include "DataDefinition.h"
|
||||
|
||||
class BaseTypeDefinition final : public DataDefinition
|
||||
{
|
||||
public:
|
||||
const unsigned m_size;
|
||||
|
||||
private:
|
||||
BaseTypeDefinition(std::string name, unsigned size);
|
||||
|
||||
public:
|
||||
_NODISCARD DataDefinitionType GetType() const override;
|
||||
_NODISCARD unsigned GetAlignment() override;
|
||||
_NODISCARD unsigned GetAlignment() const;
|
||||
_NODISCARD bool GetForceAlignment() override;
|
||||
_NODISCARD bool GetForceAlignment() const;
|
||||
_NODISCARD unsigned GetSize() override;
|
||||
_NODISCARD unsigned GetSize() const;
|
||||
|
||||
static const BaseTypeDefinition* const FLOAT;
|
||||
static const BaseTypeDefinition* const DOUBLE;
|
||||
static const BaseTypeDefinition* const BOOL;
|
||||
static const BaseTypeDefinition* const CHAR;
|
||||
static const BaseTypeDefinition* const UNSIGNED_CHAR;
|
||||
static const BaseTypeDefinition* const SHORT;
|
||||
static const BaseTypeDefinition* const UNSIGNED_SHORT;
|
||||
static const BaseTypeDefinition* const INT;
|
||||
static const BaseTypeDefinition* const UNSIGNED_INT;
|
||||
static const BaseTypeDefinition* const LONG;
|
||||
static const BaseTypeDefinition* const UNSIGNED_LONG;
|
||||
static const BaseTypeDefinition* const LONG_LONG;
|
||||
static const BaseTypeDefinition* const UNSIGNED_LONG_LONG;
|
||||
static const BaseTypeDefinition* const VOID;
|
||||
|
||||
static const BaseTypeDefinition* const ALL_BASE_TYPES[];
|
||||
};
|
@@ -0,0 +1,19 @@
|
||||
#include "DataDefinition.h"
|
||||
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
|
||||
#include "Utils/NamespaceBuilder.h"
|
||||
|
||||
DataDefinition::DataDefinition(std::string _namespace, std::string name)
|
||||
: m_namespace(std::move(_namespace)),
|
||||
m_name(std::move(name))
|
||||
{
|
||||
assert(!m_name.empty());
|
||||
}
|
||||
|
||||
std::string DataDefinition::GetFullName() const
|
||||
{
|
||||
return NamespaceBuilder::Combine(m_namespace, m_name);
|
||||
}
|
37
src/ZoneCodeGeneratorLib/Domain/Definition/DataDefinition.h
Normal file
37
src/ZoneCodeGeneratorLib/Domain/Definition/DataDefinition.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
|
||||
enum class DataDefinitionType
|
||||
{
|
||||
STRUCT,
|
||||
UNION,
|
||||
ENUM,
|
||||
TYPEDEF,
|
||||
BASE_TYPE,
|
||||
FORWARD_DECLARATION
|
||||
};
|
||||
|
||||
class DataDefinition
|
||||
{
|
||||
public:
|
||||
DataDefinition() = default;
|
||||
DataDefinition(std::string _namespace, std::string name);
|
||||
|
||||
virtual ~DataDefinition() = default;
|
||||
DataDefinition(const DataDefinition& other) = default;
|
||||
DataDefinition(DataDefinition&& other) noexcept = default;
|
||||
DataDefinition& operator=(const DataDefinition& other) = default;
|
||||
DataDefinition& operator=(DataDefinition&& other) noexcept = default;
|
||||
|
||||
std::string m_namespace;
|
||||
std::string m_name;
|
||||
|
||||
_NODISCARD virtual DataDefinitionType GetType() const = 0;
|
||||
_NODISCARD virtual unsigned GetAlignment() = 0;
|
||||
_NODISCARD virtual bool GetForceAlignment() = 0;
|
||||
_NODISCARD virtual unsigned GetSize() = 0;
|
||||
|
||||
_NODISCARD std::string GetFullName() const;
|
||||
};
|
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
|
||||
enum class DeclarationModifierType
|
||||
{
|
||||
POINTER,
|
||||
ARRAY
|
||||
};
|
||||
|
||||
class DeclarationModifier
|
||||
{
|
||||
public:
|
||||
DeclarationModifier() = default;
|
||||
virtual ~DeclarationModifier() = default;
|
||||
|
||||
_NODISCARD virtual DeclarationModifierType GetType() const = 0;
|
||||
};
|
@@ -0,0 +1,57 @@
|
||||
#include "DefinitionWithMembers.h"
|
||||
|
||||
DefinitionWithMembers::DefinitionWithMembers(std::string _namespace, std::string name, const unsigned pack)
|
||||
: DataDefinition(std::move(_namespace), std::move(name)),
|
||||
m_flags(0),
|
||||
m_size(0),
|
||||
m_alignment(0),
|
||||
m_has_alignment_override(false),
|
||||
m_anonymous(false),
|
||||
m_pack(pack),
|
||||
m_alignment_override(0)
|
||||
{
|
||||
}
|
||||
|
||||
void DefinitionWithMembers::CalculateAlignment()
|
||||
{
|
||||
if (m_has_alignment_override)
|
||||
{
|
||||
m_flags |= FLAG_ALIGNMENT_FORCED;
|
||||
m_alignment = m_alignment_override;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_alignment = 0;
|
||||
for (const auto& member : m_members)
|
||||
{
|
||||
const auto memberAlignment = member->GetAlignment();
|
||||
if (memberAlignment > m_alignment)
|
||||
m_alignment = memberAlignment;
|
||||
}
|
||||
}
|
||||
m_flags |= FLAG_ALIGNMENT_CALCULATED;
|
||||
}
|
||||
|
||||
unsigned DefinitionWithMembers::GetAlignment()
|
||||
{
|
||||
if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();
|
||||
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
bool DefinitionWithMembers::GetForceAlignment()
|
||||
{
|
||||
if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();
|
||||
|
||||
return m_flags & FLAG_ALIGNMENT_FORCED;
|
||||
}
|
||||
|
||||
unsigned DefinitionWithMembers::GetSize()
|
||||
{
|
||||
if ((m_flags & FLAG_SIZE_CALCULATED) == 0)
|
||||
CalculateSize();
|
||||
|
||||
return m_size;
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "DataDefinition.h"
|
||||
#include "Variable.h"
|
||||
|
||||
class DefinitionWithMembers : public DataDefinition
|
||||
{
|
||||
protected:
|
||||
static constexpr int FLAG_SIZE_CALCULATED = 1 << 0;
|
||||
static constexpr int FLAG_ALIGNMENT_CALCULATED = 1 << 1;
|
||||
static constexpr int FLAG_ALIGNMENT_FORCED = 1 << 2;
|
||||
|
||||
unsigned m_flags;
|
||||
unsigned m_size;
|
||||
unsigned m_alignment;
|
||||
|
||||
virtual void CalculateSize() = 0;
|
||||
void CalculateAlignment();
|
||||
|
||||
public:
|
||||
bool m_has_alignment_override;
|
||||
bool m_anonymous;
|
||||
|
||||
const unsigned m_pack;
|
||||
unsigned m_alignment_override;
|
||||
|
||||
std::vector<std::unique_ptr<Variable>> m_members;
|
||||
|
||||
DefinitionWithMembers(std::string _namespace, std::string name, unsigned pack);
|
||||
|
||||
_NODISCARD unsigned GetAlignment() override;
|
||||
_NODISCARD bool GetForceAlignment() override;
|
||||
_NODISCARD unsigned GetSize() override;
|
||||
};
|
@@ -0,0 +1,35 @@
|
||||
#include "EnumDefinition.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
EnumDefinition::EnumDefinition(std::string _namespace, std::string name, const BaseTypeDefinition* parentType)
|
||||
: DataDefinition(std::move(_namespace), std::move(name)),
|
||||
m_parent_type(parentType)
|
||||
{
|
||||
assert(parentType != nullptr);
|
||||
}
|
||||
|
||||
DataDefinitionType EnumDefinition::GetType() const
|
||||
{
|
||||
return DataDefinitionType::ENUM;
|
||||
}
|
||||
|
||||
unsigned EnumDefinition::GetAlignment()
|
||||
{
|
||||
return m_parent_type->GetAlignment();
|
||||
}
|
||||
|
||||
bool EnumDefinition::GetForceAlignment()
|
||||
{
|
||||
return m_parent_type->GetForceAlignment();
|
||||
}
|
||||
|
||||
unsigned EnumDefinition::GetSize()
|
||||
{
|
||||
return m_parent_type->GetSize();
|
||||
}
|
||||
|
||||
void EnumDefinition::AddEnumMember(EnumMember enumMember)
|
||||
{
|
||||
m_members.emplace_back(std::make_unique<EnumMember>(std::move(enumMember)));
|
||||
}
|
24
src/ZoneCodeGeneratorLib/Domain/Definition/EnumDefinition.h
Normal file
24
src/ZoneCodeGeneratorLib/Domain/Definition/EnumDefinition.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "BaseTypeDefinition.h"
|
||||
#include "DataDefinition.h"
|
||||
#include "EnumMember.h"
|
||||
|
||||
class EnumDefinition final : public DataDefinition
|
||||
{
|
||||
public:
|
||||
const BaseTypeDefinition* m_parent_type;
|
||||
std::vector<std::unique_ptr<EnumMember>> m_members;
|
||||
|
||||
EnumDefinition(std::string _namespace, std::string name, const BaseTypeDefinition* parentType);
|
||||
|
||||
_NODISCARD DataDefinitionType GetType() const override;
|
||||
_NODISCARD unsigned GetAlignment() override;
|
||||
_NODISCARD bool GetForceAlignment() override;
|
||||
_NODISCARD unsigned GetSize() override;
|
||||
|
||||
void AddEnumMember(EnumMember enumMember);
|
||||
};
|
12
src/ZoneCodeGeneratorLib/Domain/Definition/EnumMember.cpp
Normal file
12
src/ZoneCodeGeneratorLib/Domain/Definition/EnumMember.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "EnumMember.h"
|
||||
|
||||
EnumMember::EnumMember()
|
||||
: m_value(0)
|
||||
{
|
||||
}
|
||||
|
||||
EnumMember::EnumMember(std::string name, const long long value)
|
||||
: m_name(std::move(name)),
|
||||
m_value(value)
|
||||
{
|
||||
}
|
13
src/ZoneCodeGeneratorLib/Domain/Definition/EnumMember.h
Normal file
13
src/ZoneCodeGeneratorLib/Domain/Definition/EnumMember.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class EnumMember
|
||||
{
|
||||
public:
|
||||
std::string m_name;
|
||||
long long m_value;
|
||||
|
||||
EnumMember();
|
||||
EnumMember(std::string name, long long value);
|
||||
};
|
@@ -0,0 +1,28 @@
|
||||
#include "ForwardDeclaration.h"
|
||||
|
||||
ForwardDeclaration::ForwardDeclaration(std::string _namespace, std::string name, DataDefinitionType type)
|
||||
: DataDefinition(std::move(_namespace), std::move(name)),
|
||||
m_forwarded_type(type),
|
||||
m_definition(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
DataDefinitionType ForwardDeclaration::GetType() const
|
||||
{
|
||||
return DataDefinitionType::FORWARD_DECLARATION;
|
||||
}
|
||||
|
||||
unsigned ForwardDeclaration::GetAlignment()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ForwardDeclaration::GetForceAlignment()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned ForwardDeclaration::GetSize()
|
||||
{
|
||||
return 0;
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "DataDefinition.h"
|
||||
|
||||
class ForwardDeclaration final : public DataDefinition
|
||||
{
|
||||
public:
|
||||
const DataDefinitionType m_forwarded_type;
|
||||
DataDefinition* m_definition;
|
||||
|
||||
ForwardDeclaration(std::string _namespace, std::string name, DataDefinitionType type);
|
||||
|
||||
_NODISCARD DataDefinitionType GetType() const override;
|
||||
_NODISCARD unsigned GetAlignment() override;
|
||||
_NODISCARD bool GetForceAlignment() override;
|
||||
_NODISCARD unsigned GetSize() override;
|
||||
};
|
@@ -0,0 +1,72 @@
|
||||
#include "PointerDeclarationModifier.h"
|
||||
|
||||
#include "Domain/Evaluation/OperandStatic.h"
|
||||
|
||||
const IEvaluation* const PointerDeclarationModifier::DEFAULT_COUNT = new OperandStatic(1);
|
||||
|
||||
DeclarationModifierType PointerDeclarationModifier::GetType() const
|
||||
{
|
||||
return DeclarationModifierType::POINTER;
|
||||
}
|
||||
|
||||
const IEvaluation* PointerDeclarationModifier::GetCountEvaluation() const
|
||||
{
|
||||
if (m_count_evaluation)
|
||||
return m_count_evaluation.get();
|
||||
|
||||
return DEFAULT_COUNT;
|
||||
}
|
||||
|
||||
const IEvaluation* PointerDeclarationModifier::GetCountEvaluationForArrayIndex(const int index)
|
||||
{
|
||||
if (index >= 0 && m_count_evaluation_by_array_index.size() > static_cast<unsigned>(index))
|
||||
{
|
||||
return m_count_evaluation_by_array_index[index].get();
|
||||
}
|
||||
|
||||
if (m_count_evaluation)
|
||||
{
|
||||
return m_count_evaluation.get();
|
||||
}
|
||||
|
||||
return DEFAULT_COUNT;
|
||||
}
|
||||
|
||||
bool PointerDeclarationModifier::EvaluationIsArray(const IEvaluation* evaluation)
|
||||
{
|
||||
return !evaluation->IsStatic() || evaluation->EvaluateNumeric() > 1;
|
||||
}
|
||||
|
||||
bool PointerDeclarationModifier::CountEvaluationIsArray() const
|
||||
{
|
||||
if (m_count_evaluation)
|
||||
{
|
||||
return EvaluationIsArray(m_count_evaluation.get());
|
||||
}
|
||||
|
||||
return EvaluationIsArray(DEFAULT_COUNT);
|
||||
}
|
||||
|
||||
bool PointerDeclarationModifier::CountEvaluationIsArray(const int index) const
|
||||
{
|
||||
if(index >= 0 && m_count_evaluation_by_array_index.size() > static_cast<unsigned>(index))
|
||||
{
|
||||
return EvaluationIsArray(m_count_evaluation_by_array_index[index].get());
|
||||
}
|
||||
|
||||
return CountEvaluationIsArray();
|
||||
}
|
||||
|
||||
bool PointerDeclarationModifier::AnyCountEvaluationIsArray() const
|
||||
{
|
||||
if (m_count_evaluation && EvaluationIsArray(m_count_evaluation.get()))
|
||||
return true;
|
||||
|
||||
for(const auto& arrayCountEvaluation : m_count_evaluation_by_array_index)
|
||||
{
|
||||
if (EvaluationIsArray(arrayCountEvaluation.get()))
|
||||
return true;
|
||||
}
|
||||
|
||||
return EvaluationIsArray(DEFAULT_COUNT);
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "DeclarationModifier.h"
|
||||
#include "Domain/Evaluation/IEvaluation.h"
|
||||
|
||||
class PointerDeclarationModifier final : public DeclarationModifier
|
||||
{
|
||||
static const IEvaluation* const DEFAULT_COUNT;
|
||||
|
||||
static bool EvaluationIsArray(const IEvaluation* evaluation);
|
||||
|
||||
public:
|
||||
std::unique_ptr<IEvaluation> m_count_evaluation;
|
||||
std::vector<std::unique_ptr<IEvaluation>> m_count_evaluation_by_array_index;
|
||||
|
||||
_NODISCARD DeclarationModifierType GetType() const override;
|
||||
_NODISCARD const IEvaluation* GetCountEvaluation() const;
|
||||
_NODISCARD const IEvaluation* GetCountEvaluationForArrayIndex(int index);
|
||||
|
||||
_NODISCARD bool CountEvaluationIsArray() const;
|
||||
_NODISCARD bool CountEvaluationIsArray(int index) const;
|
||||
_NODISCARD bool AnyCountEvaluationIsArray() const;
|
||||
};
|
@@ -0,0 +1,49 @@
|
||||
#include "StructDefinition.h"
|
||||
|
||||
#include "Utils/AlignmentUtils.h"
|
||||
|
||||
void StructDefinition::CalculateSize()
|
||||
{
|
||||
m_size = 0;
|
||||
auto currentBitOffset = 0u;
|
||||
|
||||
for(const auto& member : m_members)
|
||||
{
|
||||
if(member->m_type_declaration->m_has_custom_bit_size)
|
||||
{
|
||||
currentBitOffset += member->m_type_declaration->m_custom_bit_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentBitOffset > 0)
|
||||
{
|
||||
currentBitOffset = AlignmentUtils::Align(currentBitOffset, 8u);
|
||||
m_size += currentBitOffset / 8;
|
||||
currentBitOffset = 0;
|
||||
}
|
||||
|
||||
m_size = AlignmentUtils::Align(m_size, member->GetForceAlignment() ? member->GetAlignment() : std::min(member->GetAlignment(), m_pack));
|
||||
m_size += member->m_type_declaration->GetSize();
|
||||
}
|
||||
}
|
||||
|
||||
if (currentBitOffset > 0)
|
||||
{
|
||||
currentBitOffset = AlignmentUtils::Align(currentBitOffset, 8u);
|
||||
m_size += currentBitOffset / 8;
|
||||
}
|
||||
|
||||
m_size = AlignmentUtils::Align(m_size, GetAlignment());
|
||||
|
||||
m_flags |= FLAG_SIZE_CALCULATED;
|
||||
}
|
||||
|
||||
StructDefinition::StructDefinition(std::string _namespace, std::string name, const int pack)
|
||||
: DefinitionWithMembers(std::move(_namespace), std::move(name), pack)
|
||||
{
|
||||
}
|
||||
|
||||
DataDefinitionType StructDefinition::GetType() const
|
||||
{
|
||||
return DataDefinitionType::STRUCT;
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "DefinitionWithMembers.h"
|
||||
|
||||
class StructDefinition final : public DefinitionWithMembers
|
||||
{
|
||||
protected:
|
||||
void CalculateSize() override;
|
||||
|
||||
public:
|
||||
StructDefinition(std::string _namespace, std::string name, int pack);
|
||||
|
||||
_NODISCARD DataDefinitionType GetType() const override;
|
||||
};
|
@@ -0,0 +1,90 @@
|
||||
#include "TypeDeclaration.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "ArrayDeclarationModifier.h"
|
||||
|
||||
TypeDeclaration::TypeDeclaration(DataDefinition* type)
|
||||
: m_flags(0),
|
||||
m_size(0),
|
||||
m_alignment(0),
|
||||
m_is_const(false),
|
||||
m_has_custom_bit_size(false),
|
||||
m_type(type),
|
||||
m_custom_bit_size(0)
|
||||
{
|
||||
assert(m_type != nullptr);
|
||||
}
|
||||
|
||||
void TypeDeclaration::CalculateSize()
|
||||
{
|
||||
auto currentSize = m_type->GetSize();
|
||||
|
||||
for(auto i = m_declaration_modifiers.size(); i > 0; i--)
|
||||
{
|
||||
const auto& declarationModifier = m_declaration_modifiers[i - 1];
|
||||
|
||||
switch(declarationModifier->GetType())
|
||||
{
|
||||
case DeclarationModifierType::POINTER:
|
||||
currentSize = POINTER_SIZE;
|
||||
break;
|
||||
|
||||
case DeclarationModifierType::ARRAY:
|
||||
currentSize *= dynamic_cast<ArrayDeclarationModifier*>(declarationModifier.get())->m_size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_flags |= FLAG_SIZE_CALCULATED;
|
||||
}
|
||||
|
||||
void TypeDeclaration::CalculateAlignment()
|
||||
{
|
||||
auto hasPointerModifier = false;
|
||||
for (const auto& declarationModifier : m_declaration_modifiers)
|
||||
{
|
||||
if (declarationModifier->GetType() == DeclarationModifierType::POINTER)
|
||||
{
|
||||
hasPointerModifier = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasPointerModifier)
|
||||
{
|
||||
m_alignment = POINTER_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_alignment = m_type->GetAlignment();
|
||||
if (m_type->GetForceAlignment())
|
||||
m_flags |= FLAG_ALIGNMENT_FORCED;
|
||||
}
|
||||
|
||||
m_flags |= FLAG_ALIGNMENT_CALCULATED;
|
||||
}
|
||||
|
||||
unsigned TypeDeclaration::GetSize()
|
||||
{
|
||||
if ((m_flags & FLAG_SIZE_CALCULATED) == 0)
|
||||
CalculateSize();
|
||||
|
||||
return m_size;
|
||||
}
|
||||
|
||||
unsigned TypeDeclaration::GetAlignment()
|
||||
{
|
||||
if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();
|
||||
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
bool TypeDeclaration::GetForceAlignment()
|
||||
{
|
||||
if ((m_flags & FLAG_ALIGNMENT_CALCULATED) == 0)
|
||||
CalculateAlignment();
|
||||
|
||||
return m_flags & FLAG_ALIGNMENT_FORCED;
|
||||
}
|
37
src/ZoneCodeGeneratorLib/Domain/Definition/TypeDeclaration.h
Normal file
37
src/ZoneCodeGeneratorLib/Domain/Definition/TypeDeclaration.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include "DataDefinition.h"
|
||||
#include "DeclarationModifier.h"
|
||||
|
||||
class TypeDeclaration
|
||||
{
|
||||
static constexpr unsigned POINTER_SIZE = sizeof(uint32_t); // TODO: Change this to support 64bit
|
||||
static constexpr int FLAG_SIZE_CALCULATED = 1 << 0;
|
||||
static constexpr int FLAG_ALIGNMENT_CALCULATED = 1 << 1;
|
||||
static constexpr int FLAG_ALIGNMENT_FORCED = 1 << 2;
|
||||
|
||||
unsigned m_flags;
|
||||
unsigned m_size;
|
||||
unsigned m_alignment;
|
||||
|
||||
void CalculateSize();
|
||||
void CalculateAlignment();
|
||||
|
||||
public:
|
||||
explicit TypeDeclaration(DataDefinition* type);
|
||||
|
||||
bool m_is_const;
|
||||
bool m_has_custom_bit_size;
|
||||
|
||||
DataDefinition* m_type;
|
||||
unsigned m_custom_bit_size;
|
||||
|
||||
std::vector<std::unique_ptr<DeclarationModifier>> m_declaration_modifiers;
|
||||
|
||||
unsigned GetSize();
|
||||
unsigned GetAlignment();
|
||||
bool GetForceAlignment();
|
||||
};
|
@@ -0,0 +1,33 @@
|
||||
#include "TypedefDefinition.h"
|
||||
|
||||
TypedefDefinition::TypedefDefinition(std::string _namespace, std::string name, std::unique_ptr<TypeDeclaration> typeDeclaration)
|
||||
: DataDefinition(std::move(_namespace), std::move(name)),
|
||||
m_has_alignment_override(false),
|
||||
m_alignment_override(0),
|
||||
m_type_declaration(std::move(typeDeclaration))
|
||||
{
|
||||
}
|
||||
|
||||
DataDefinitionType TypedefDefinition::GetType() const
|
||||
{
|
||||
return DataDefinitionType::TYPEDEF;
|
||||
}
|
||||
|
||||
unsigned TypedefDefinition::GetAlignment()
|
||||
{
|
||||
if (m_has_alignment_override)
|
||||
{
|
||||
return m_alignment_override;
|
||||
}
|
||||
return m_type_declaration->GetAlignment();
|
||||
}
|
||||
|
||||
bool TypedefDefinition::GetForceAlignment()
|
||||
{
|
||||
return m_has_alignment_override || m_type_declaration->GetForceAlignment();
|
||||
}
|
||||
|
||||
unsigned TypedefDefinition::GetSize()
|
||||
{
|
||||
return m_type_declaration->GetSize();
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "DataDefinition.h"
|
||||
#include "TypeDeclaration.h"
|
||||
|
||||
class TypedefDefinition final : public DataDefinition
|
||||
{
|
||||
public:
|
||||
bool m_has_alignment_override;
|
||||
unsigned m_alignment_override;
|
||||
std::unique_ptr<TypeDeclaration> m_type_declaration;
|
||||
|
||||
TypedefDefinition(std::string _namespace, std::string name, std::unique_ptr<TypeDeclaration> typeDeclaration);
|
||||
|
||||
_NODISCARD DataDefinitionType GetType() const override;
|
||||
_NODISCARD unsigned GetAlignment() override;
|
||||
_NODISCARD bool GetForceAlignment() override;
|
||||
_NODISCARD unsigned GetSize() override;
|
||||
};
|
@@ -0,0 +1,29 @@
|
||||
#include "UnionDefinition.h"
|
||||
|
||||
#include "Utils/AlignmentUtils.h"
|
||||
|
||||
void UnionDefinition::CalculateSize()
|
||||
{
|
||||
m_size = 0;
|
||||
|
||||
for(const auto& member : m_members)
|
||||
{
|
||||
const auto memberSize = member->m_type_declaration->GetSize();
|
||||
if (memberSize > m_size)
|
||||
m_size = memberSize;
|
||||
}
|
||||
|
||||
m_size = AlignmentUtils::Align(m_size, GetAlignment());
|
||||
|
||||
m_flags |= FLAG_SIZE_CALCULATED;
|
||||
}
|
||||
|
||||
UnionDefinition::UnionDefinition(std::string _namespace, std::string name, const int pack)
|
||||
: DefinitionWithMembers(std::move(_namespace), std::move(name), pack)
|
||||
{
|
||||
}
|
||||
|
||||
DataDefinitionType UnionDefinition::GetType() const
|
||||
{
|
||||
return DataDefinitionType::UNION;
|
||||
}
|
14
src/ZoneCodeGeneratorLib/Domain/Definition/UnionDefinition.h
Normal file
14
src/ZoneCodeGeneratorLib/Domain/Definition/UnionDefinition.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "DefinitionWithMembers.h"
|
||||
|
||||
class UnionDefinition final : public DefinitionWithMembers
|
||||
{
|
||||
protected:
|
||||
void CalculateSize() override;
|
||||
|
||||
public:
|
||||
UnionDefinition(std::string _namespace, std::string name, int pack);
|
||||
|
||||
_NODISCARD DataDefinitionType GetType() const override;
|
||||
};
|
14
src/ZoneCodeGeneratorLib/Domain/Definition/Variable.cpp
Normal file
14
src/ZoneCodeGeneratorLib/Domain/Definition/Variable.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "Variable.h"
|
||||
|
||||
unsigned Variable::GetAlignment()
|
||||
{
|
||||
if (m_has_alignment_override)
|
||||
return m_alignment_override;
|
||||
|
||||
return m_type_declaration->GetAlignment();
|
||||
}
|
||||
|
||||
bool Variable::GetForceAlignment()
|
||||
{
|
||||
return m_has_alignment_override || m_type_declaration->GetForceAlignment();
|
||||
}
|
18
src/ZoneCodeGeneratorLib/Domain/Definition/Variable.h
Normal file
18
src/ZoneCodeGeneratorLib/Domain/Definition/Variable.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "TypeDeclaration.h"
|
||||
|
||||
class Variable
|
||||
{
|
||||
public:
|
||||
std::string m_name;
|
||||
bool m_has_alignment_override;
|
||||
unsigned m_alignment_override;
|
||||
std::unique_ptr<TypeDeclaration> m_type_declaration;
|
||||
|
||||
unsigned GetAlignment();
|
||||
bool GetForceAlignment();
|
||||
};
|
Reference in New Issue
Block a user