mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-07-01 08:41:52 +00:00
parse struct and union sequences
This commit is contained in:
@ -5,6 +5,16 @@
|
||||
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
||||
#include "Parsing/Header/Sequence/SequenceUnion.h"
|
||||
#include "Parsing/Header/Sequence/SequenceVariable.h"
|
||||
#include "Utils/NameUtils.h"
|
||||
|
||||
HeaderBlockStruct::HeaderBlockStruct(std::string name, const bool isTypedef)
|
||||
: m_type_name(std::move(name)),
|
||||
m_struct_definition(nullptr),
|
||||
m_custom_alignment(0),
|
||||
m_is_typedef(isTypedef),
|
||||
m_has_custom_align(false)
|
||||
{
|
||||
}
|
||||
|
||||
HeaderBlockType HeaderBlockStruct::GetType()
|
||||
{
|
||||
@ -26,17 +36,77 @@ const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockStruct::GetTestsForBloc
|
||||
|
||||
void HeaderBlockStruct::OnOpen(HeaderParserState* state)
|
||||
{
|
||||
m_namespace = state->m_namespace.ToString();
|
||||
|
||||
if (!m_type_name.empty())
|
||||
state->m_namespace.Push(m_type_name);
|
||||
}
|
||||
|
||||
void HeaderBlockStruct::OnClose(HeaderParserState* state)
|
||||
{
|
||||
if (!m_type_name.empty())
|
||||
state->m_namespace.Pop();
|
||||
|
||||
auto isAnonymous = false;
|
||||
auto typeName = m_type_name;
|
||||
if(typeName.empty())
|
||||
{
|
||||
isAnonymous = true;
|
||||
typeName = NameUtils::GenerateRandomName();
|
||||
}
|
||||
|
||||
auto structDefinition = std::make_unique<StructDefinition>(m_namespace, std::move(typeName), state->m_pack_value_supplier->GetCurrentPack());
|
||||
m_struct_definition = structDefinition.get();
|
||||
|
||||
if (isAnonymous)
|
||||
structDefinition->m_anonymous = true;
|
||||
|
||||
for (auto& member : m_members)
|
||||
structDefinition->m_members.emplace_back(std::move(member));
|
||||
|
||||
state->AddDataType(std::move(structDefinition));
|
||||
|
||||
if (m_is_typedef)
|
||||
state->AddDataType(std::make_unique<TypedefDefinition>(m_namespace, m_variable_name, std::make_unique<TypeDeclaration>(m_struct_definition)));
|
||||
}
|
||||
|
||||
void HeaderBlockStruct::OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block)
|
||||
{
|
||||
}
|
||||
|
||||
void HeaderBlockStruct::AddVariable(std::shared_ptr<Variable> variable)
|
||||
{
|
||||
m_members.emplace_back(std::move(variable));
|
||||
}
|
||||
|
||||
void HeaderBlockStruct::SetCustomAlignment(const int alignment)
|
||||
{
|
||||
m_has_custom_align = true;
|
||||
m_custom_alignment = alignment;
|
||||
}
|
||||
|
||||
void HeaderBlockStruct::Inherit(const StructDefinition* parentStruct)
|
||||
{
|
||||
for(const auto& parentMember : parentStruct->m_members)
|
||||
AddVariable(parentMember);
|
||||
}
|
||||
|
||||
void HeaderBlockStruct::SetBlockName(const TokenPos& nameTokenPos, std::string name)
|
||||
{
|
||||
m_variable_name = std::move(name);
|
||||
}
|
||||
|
||||
bool HeaderBlockStruct::IsDefiningVariable()
|
||||
{
|
||||
return !m_is_typedef && !m_variable_name.empty();
|
||||
}
|
||||
|
||||
DataDefinition* HeaderBlockStruct::GetVariableType()
|
||||
{
|
||||
return m_struct_definition;
|
||||
}
|
||||
|
||||
std::string HeaderBlockStruct::GetVariableName()
|
||||
{
|
||||
return m_variable_name;
|
||||
}
|
||||
|
@ -1,18 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "IHeaderBlock.h"
|
||||
#include "IHeaderBlockNameHolder.h"
|
||||
#include "IHeaderBlockVariableDefining.h"
|
||||
#include "Domain/Definition/StructDefinition.h"
|
||||
#include "Domain/Definition/Variable.h"
|
||||
|
||||
class HeaderBlockStruct final : public IHeaderBlock, public IHeaderBlockNameHolder
|
||||
class HeaderBlockStruct final : public IHeaderBlock, public IHeaderBlockNameHolder, public IHeaderBlockVariableDefining
|
||||
{
|
||||
std::string m_namespace;
|
||||
std::string m_type_name;
|
||||
std::vector<std::shared_ptr<Variable>> m_members;
|
||||
StructDefinition* m_struct_definition;
|
||||
|
||||
int m_custom_alignment;
|
||||
|
||||
bool m_is_typedef;
|
||||
bool m_has_custom_align;
|
||||
|
||||
std::string m_variable_name;
|
||||
|
||||
public:
|
||||
HeaderBlockStruct(std::string name, bool isTypedef);
|
||||
|
||||
HeaderBlockType GetType() override;
|
||||
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
||||
void OnOpen(HeaderParserState* state) override;
|
||||
void OnClose(HeaderParserState* state) override;
|
||||
void OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block) override;
|
||||
|
||||
|
||||
void AddVariable(std::shared_ptr<Variable> variable);
|
||||
void SetCustomAlignment(int alignment);
|
||||
void Inherit(const StructDefinition* parentStruct);
|
||||
|
||||
void SetBlockName(const TokenPos& nameTokenPos, std::string name) override;
|
||||
bool IsDefiningVariable() override;
|
||||
DataDefinition* GetVariableType() override;
|
||||
std::string GetVariableName() override;
|
||||
};
|
||||
|
@ -5,6 +5,16 @@
|
||||
#include "Parsing/Header/Sequence/SequenceStruct.h"
|
||||
#include "Parsing/Header/Sequence/SequenceUnion.h"
|
||||
#include "Parsing/Header/Sequence/SequenceVariable.h"
|
||||
#include "Utils/NameUtils.h"
|
||||
|
||||
HeaderBlockUnion::HeaderBlockUnion(std::string name, const bool isTypedef)
|
||||
: m_type_name(std::move(name)),
|
||||
m_union_definition(nullptr),
|
||||
m_custom_alignment(0),
|
||||
m_is_typedef(isTypedef),
|
||||
m_has_custom_align(false)
|
||||
{
|
||||
}
|
||||
|
||||
HeaderBlockType HeaderBlockUnion::GetType()
|
||||
{
|
||||
@ -26,17 +36,77 @@ const std::vector<IHeaderBlock::sequence_t*>& HeaderBlockUnion::GetTestsForBlock
|
||||
|
||||
void HeaderBlockUnion::OnOpen(HeaderParserState* state)
|
||||
{
|
||||
m_namespace = state->m_namespace.ToString();
|
||||
|
||||
if (!m_type_name.empty())
|
||||
state->m_namespace.Push(m_type_name);
|
||||
}
|
||||
|
||||
void HeaderBlockUnion::OnClose(HeaderParserState* state)
|
||||
{
|
||||
if (!m_type_name.empty())
|
||||
state->m_namespace.Pop();
|
||||
|
||||
auto isAnonymous = false;
|
||||
auto typeName = m_type_name;
|
||||
if (typeName.empty())
|
||||
{
|
||||
isAnonymous = true;
|
||||
typeName = NameUtils::GenerateRandomName();
|
||||
}
|
||||
|
||||
auto unionDefinition = std::make_unique<UnionDefinition>(m_namespace, std::move(typeName), state->m_pack_value_supplier->GetCurrentPack());
|
||||
m_union_definition = unionDefinition.get();
|
||||
|
||||
if (isAnonymous)
|
||||
unionDefinition->m_anonymous = true;
|
||||
|
||||
for (auto& member : m_members)
|
||||
unionDefinition->m_members.emplace_back(std::move(member));
|
||||
|
||||
state->AddDataType(std::move(unionDefinition));
|
||||
|
||||
if (m_is_typedef)
|
||||
state->AddDataType(std::make_unique<TypedefDefinition>(m_namespace, m_variable_name, std::make_unique<TypeDeclaration>(m_union_definition)));
|
||||
}
|
||||
|
||||
void HeaderBlockUnion::OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block)
|
||||
{
|
||||
}
|
||||
|
||||
void HeaderBlockUnion::AddVariable(std::shared_ptr<Variable> variable)
|
||||
{
|
||||
m_members.emplace_back(std::move(variable));
|
||||
}
|
||||
|
||||
void HeaderBlockUnion::SetCustomAlignment(const int alignment)
|
||||
{
|
||||
m_has_custom_align = true;
|
||||
m_custom_alignment = alignment;
|
||||
}
|
||||
|
||||
void HeaderBlockUnion::Inherit(const StructDefinition* parentStruct)
|
||||
{
|
||||
for (const auto& parentMember : parentStruct->m_members)
|
||||
AddVariable(parentMember);
|
||||
}
|
||||
|
||||
void HeaderBlockUnion::SetBlockName(const TokenPos& nameTokenPos, std::string name)
|
||||
{
|
||||
m_variable_name = std::move(name);
|
||||
}
|
||||
|
||||
bool HeaderBlockUnion::IsDefiningVariable()
|
||||
{
|
||||
return !m_is_typedef && !m_variable_name.empty();
|
||||
}
|
||||
|
||||
DataDefinition* HeaderBlockUnion::GetVariableType()
|
||||
{
|
||||
return m_union_definition;
|
||||
}
|
||||
|
||||
std::string HeaderBlockUnion::GetVariableName()
|
||||
{
|
||||
return m_variable_name;
|
||||
}
|
||||
|
@ -2,17 +2,37 @@
|
||||
|
||||
#include "IHeaderBlock.h"
|
||||
#include "IHeaderBlockNameHolder.h"
|
||||
#include "IHeaderBlockVariableDefining.h"
|
||||
|
||||
class HeaderBlockUnion final : public IHeaderBlock, public IHeaderBlockNameHolder
|
||||
class HeaderBlockUnion final : public IHeaderBlock, public IHeaderBlockNameHolder, public IHeaderBlockVariableDefining
|
||||
{
|
||||
std::string m_namespace;
|
||||
std::string m_type_name;
|
||||
std::vector<std::shared_ptr<Variable>> m_members;
|
||||
UnionDefinition* m_union_definition;
|
||||
|
||||
int m_custom_alignment;
|
||||
|
||||
bool m_is_typedef;
|
||||
bool m_has_custom_align;
|
||||
|
||||
std::string m_variable_name;
|
||||
|
||||
public:
|
||||
HeaderBlockUnion(std::string name, bool isTypedef);
|
||||
|
||||
HeaderBlockType GetType() override;
|
||||
const std::vector<sequence_t*>& GetTestsForBlock() override;
|
||||
void OnOpen(HeaderParserState* state) override;
|
||||
void OnClose(HeaderParserState* state) override;
|
||||
void OnChildBlockClose(HeaderParserState* state, IHeaderBlock* block) override;
|
||||
|
||||
void AddVariable(std::shared_ptr<Variable> variable);
|
||||
void SetCustomAlignment(int alignment);
|
||||
void Inherit(const StructDefinition* parentStruct);
|
||||
|
||||
void SetBlockName(const TokenPos& nameTokenPos, std::string name) override;
|
||||
bool IsDefiningVariable() override;
|
||||
DataDefinition* GetVariableType() override;
|
||||
std::string GetVariableName() override;
|
||||
};
|
||||
|
Reference in New Issue
Block a user