Skip to content

Commit

Permalink
Added error if missing 'optional' for custom type and if wrong struct…
Browse files Browse the repository at this point in the history
… order.
  • Loading branch information
Yurii.Blok authored and Yurii.Blok committed Apr 2, 2018
1 parent 661313f commit 2af3b85
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 36 deletions.
Binary file modified Binaries/ibc_x64
Binary file not shown.
41 changes: 39 additions & 2 deletions Compiler/inbcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ void INBCompiler::parseStruct(tokens_it &it)

optionalCount = 0;
bool isExpectType = false;
bool isExpectOptional = false;
while (*it != tokenCloseScope)
{
if (*it == tokenArray)
Expand Down Expand Up @@ -345,21 +346,57 @@ void INBCompiler::parseStruct(tokens_it &it)
optionalCount++;
fieldsList.back().isOptional = true;
fieldsList.back().isBuiltIn = false;
isExpectOptional = true;

auto fs = std::find_if(m_structs.begin(), m_structs.end(),
[&](StructDescr &sd) { return sd.name == fieldsList.back().type; });
if (fs == m_structs.end())
{
std::cout << clr::red << "Wrong structs order, must be (first)'"
<< fieldsList.back().type << "'+(second)'"
<< structName << "'" << clr::default_ << std::endl;
exit(0);
}
fs->friends.insert(structName);
}
if (!next(it))
return;
isExpectType = false;
}
else
{
if (isExpectOptional)
{
isExpectOptional = false;
if (!fieldsList.back().isArray)
{
std::cout << clr::red << "Custom types ("
<< fieldsList.back().name << ":"
<< fieldsList.back().type
<< ") should be specified as optional "
<< clr::default_ << std::endl;
exit(0);
}
}
fieldsList.emplace_back();
fieldsList.back().name = std::move(*it);
if (!next(it))
return;
isExpectType = true;
}
//if (!next(it))
// return;
}
if (isExpectOptional)
{
isExpectOptional = false;
if (!fieldsList.back().isArray && !fieldsList.back().isOptional)
{
std::cout << clr::red << "Custom types ("
<< fieldsList.back().name << ":"
<< fieldsList.back().type
<< ") should be specified as optional "
<< clr::default_ << std::endl;
exit(0);
}
}
next(it);
if (m_detailed)
Expand Down
11 changes: 6 additions & 5 deletions Compiler/inbcompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ class INBCompiler
{
std::string name;
std::string type;
bool isBuiltIn = false;
bool isOptional = false;
bool isArray = false;
bool isBuiltIn = false;
bool isOptional = false;
bool isArray = false;
};
struct StructDescr
{
std::string name;
std::set<std::string> friends;
std::deque<StructFieldType> fields;
uint32_t optionalCount = 0;
std::string name;
uint32_t optionalCount = 0;
};
std::deque<StructDescr> m_structs;
//using StructDescr = std::pair<std::deque<std::pair<std::string,
Expand Down
16 changes: 9 additions & 7 deletions Compiler/inbcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void INBCompiler::genCPP(const std::string &out)
file << " }" << std::endl;
file << " uint32_t size(const ids& id)" << std::endl;
file << " {" << std::endl;
file << " uint8_t* ptr = m_from ? m_from : &m_buffer->data()[0];" << std::endl;
file << " uint8_t* ptr = m_from ? const_cast<uint8_t*>(m_from) : &m_buffer->data()[0];" << std::endl;
file << " Table *table = reinterpret_cast<Table*>(&ptr[sizeof(_inner_::header)]);" << std::endl;
file << " const uint32_t offset = address(id, table);" << std::endl;
file << " return *reinterpret_cast<uint32_t*>(&ptr[offset]);" << std::endl;
Expand Down Expand Up @@ -505,7 +505,7 @@ void INBCompiler::genCPP(const std::string &out)
file << " h->signature1 = 'b';" << std::endl;
file << " m_table = sizeof(_inner_::header);" << std::endl;
file << " }" << std::endl;
file << " bool from(uint8_t* ptr)" << std::endl;
file << " bool from(const uint8_t* ptr)" << std::endl;
file << " {" << std::endl;
file << " if (!ptr)" << std::endl;
file << " return false;" << std::endl;
Expand Down Expand Up @@ -533,8 +533,8 @@ void INBCompiler::genCPP(const std::string &out)
file << " {" << std::endl;
file << " custom." << name << ".emplace_back();" << std::endl;
file << " custom." << name << ".back().from(ptr);" << std::endl;
file << " auto table = reinterpret_cast<" << type << "::Table*>(ptr + getTable()->__" << name << " + sizeof(uint32_t) + sizeof(" << type << "::Table) * i);" << std::endl;
file << " custom." << name << ".back().m_table = reinterpret_cast<uint8_t*>(table) - ptr;" << std::endl;
file << " auto table = reinterpret_cast<const " << type << "::Table*>(ptr + getTable()->__" << name << " + sizeof(uint32_t) + sizeof(" << type << "::Table) * i);" << std::endl;
file << " custom." << name << ".back().m_table = reinterpret_cast<const uint8_t*>(table) - ptr;" << std::endl;
file << " }" << std::endl;
file << " }" << std::endl;
}
Expand All @@ -543,7 +543,7 @@ void INBCompiler::genCPP(const std::string &out)
file << " uint8_t* to()" << std::endl;
file << " {" << std::endl;
file << " if (m_from)" << std::endl;
file << " return m_from;" << std::endl;
file << " return const_cast<uint8_t*>(m_from);" << std::endl;
file << " else" << std::endl;
file << " return m_buffer->data();" << std::endl;
file << " }" << std::endl;
Expand All @@ -555,7 +555,9 @@ void INBCompiler::genCPP(const std::string &out)
file << " return static_cast<uint32_t>(m_buffer->size());" << std::endl;
file << " }" << std::endl;
file << std::endl;
//file << "private:" << std::endl;
for (const auto &member : st.friends)
file << " friend class " << member << ";" << std::endl;
file << "private:" << std::endl;
file << " #pragma pack(push, 1)" << std::endl;
file << " struct Table" << std::endl;
file << " {" << std::endl;
Expand Down Expand Up @@ -630,7 +632,7 @@ void INBCompiler::genCPP(const std::string &out)
file << " } custom;" << std::endl;
file << std::endl;
}
file << " uint8_t* m_from = nullptr;" << std::endl;
file << " const uint8_t* m_from = nullptr;" << std::endl;
file << " std::shared_ptr<std::vector<uint8_t>> m_buffer;" << std::endl;
file << " uint32_t m_table = 0;" << std::endl;
file << "};" << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions Compiler/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <deque>
#include <cctype>
#include <algorithm>
#include <set>
#include <unordered_set>

#ifdef _WIN32
# include <Windows.h>
Expand Down
51 changes: 29 additions & 22 deletions Tests/schema_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace _inner_
static uint32_t zero = 0;
}

static const uint32_t TestConst = 43344;
enum class Color
{
RED = 0,
Expand Down Expand Up @@ -162,7 +163,7 @@ class OnlyStatic
}
uint32_t size(const ids& id)
{
uint8_t* ptr = m_from ? m_from : &m_buffer->data()[0];
uint8_t* ptr = m_from ? const_cast<uint8_t*>(m_from) : &m_buffer->data()[0];
Table *table = reinterpret_cast<Table*>(&ptr[sizeof(_inner_::header)]);
const uint32_t offset = address(id, table);
return *reinterpret_cast<uint32_t*>(&ptr[offset]);
Expand Down Expand Up @@ -293,7 +294,7 @@ class OnlyStatic
h->signature1 = 'b';
m_table = sizeof(_inner_::header);
}
bool from(uint8_t* ptr)
bool from(const uint8_t* ptr)
{
if (!ptr)
return false;
Expand All @@ -309,7 +310,7 @@ class OnlyStatic
uint8_t* to()
{
if (m_from)
return m_from;
return const_cast<uint8_t*>(m_from);
else
return m_buffer->data();
}
Expand All @@ -321,6 +322,7 @@ class OnlyStatic
return static_cast<uint32_t>(m_buffer->size());
}

private:
#pragma pack(push, 1)
struct Table
{
Expand Down Expand Up @@ -356,7 +358,7 @@ class OnlyStatic
}
}

uint8_t* m_from = nullptr;
const uint8_t* m_from = nullptr;
std::shared_ptr<std::vector<uint8_t>> m_buffer;
uint32_t m_table = 0;
};
Expand Down Expand Up @@ -465,7 +467,7 @@ class OnlyOptional
}
uint32_t size(const ids& id)
{
uint8_t* ptr = m_from ? m_from : &m_buffer->data()[0];
uint8_t* ptr = m_from ? const_cast<uint8_t*>(m_from) : &m_buffer->data()[0];
Table *table = reinterpret_cast<Table*>(&ptr[sizeof(_inner_::header)]);
const uint32_t offset = address(id, table);
return *reinterpret_cast<uint32_t*>(&ptr[offset]);
Expand Down Expand Up @@ -596,7 +598,7 @@ class OnlyOptional
h->signature1 = 'b';
m_table = sizeof(_inner_::header);
}
bool from(uint8_t* ptr)
bool from(const uint8_t* ptr)
{
if (!ptr)
return false;
Expand All @@ -612,7 +614,7 @@ class OnlyOptional
uint8_t* to()
{
if (m_from)
return m_from;
return const_cast<uint8_t*>(m_from);
else
return m_buffer->data();
}
Expand All @@ -624,6 +626,7 @@ class OnlyOptional
return static_cast<uint32_t>(m_buffer->size());
}

private:
#pragma pack(push, 1)
struct Table
{
Expand Down Expand Up @@ -665,7 +668,7 @@ class OnlyOptional
}
}

uint8_t* m_from = nullptr;
const uint8_t* m_from = nullptr;
std::shared_ptr<std::vector<uint8_t>> m_buffer;
uint32_t m_table = 0;
};
Expand Down Expand Up @@ -763,7 +766,7 @@ class Vec3f
}
uint32_t size(const ids& id)
{
uint8_t* ptr = m_from ? m_from : &m_buffer->data()[0];
uint8_t* ptr = m_from ? const_cast<uint8_t*>(m_from) : &m_buffer->data()[0];
Table *table = reinterpret_cast<Table*>(&ptr[sizeof(_inner_::header)]);
const uint32_t offset = address(id, table);
return *reinterpret_cast<uint32_t*>(&ptr[offset]);
Expand Down Expand Up @@ -837,7 +840,7 @@ class Vec3f
h->signature1 = 'b';
m_table = sizeof(_inner_::header);
}
bool from(uint8_t* ptr)
bool from(const uint8_t* ptr)
{
if (!ptr)
return false;
Expand All @@ -853,7 +856,7 @@ class Vec3f
uint8_t* to()
{
if (m_from)
return m_from;
return const_cast<uint8_t*>(m_from);
else
return m_buffer->data();
}
Expand All @@ -865,6 +868,8 @@ class Vec3f
return static_cast<uint32_t>(m_buffer->size());
}

friend class Arrays;
private:
#pragma pack(push, 1)
struct Table
{
Expand Down Expand Up @@ -897,7 +902,7 @@ class Vec3f
}
}

uint8_t* m_from = nullptr;
const uint8_t* m_from = nullptr;
std::shared_ptr<std::vector<uint8_t>> m_buffer;
uint32_t m_table = 0;
};
Expand Down Expand Up @@ -995,7 +1000,7 @@ class Arrays
}
uint32_t size(const ids& id)
{
uint8_t* ptr = m_from ? m_from : &m_buffer->data()[0];
uint8_t* ptr = m_from ? const_cast<uint8_t*>(m_from) : &m_buffer->data()[0];
Table *table = reinterpret_cast<Table*>(&ptr[sizeof(_inner_::header)]);
const uint32_t offset = address(id, table);
return *reinterpret_cast<uint32_t*>(&ptr[offset]);
Expand Down Expand Up @@ -1088,7 +1093,7 @@ class Arrays
h->signature1 = 'b';
m_table = sizeof(_inner_::header);
}
bool from(uint8_t* ptr)
bool from(const uint8_t* ptr)
{
if (!ptr)
return false;
Expand All @@ -1107,16 +1112,16 @@ class Arrays
{
custom.v.emplace_back();
custom.v.back().from(ptr);
auto table = reinterpret_cast<Vec3f::Table*>(ptr + getTable()->__v + sizeof(uint32_t) + sizeof(Vec3f::Table) * i);
custom.v.back().m_table = reinterpret_cast<uint8_t*>(table) - ptr;
auto table = reinterpret_cast<const Vec3f::Table*>(ptr + getTable()->__v + sizeof(uint32_t) + sizeof(Vec3f::Table) * i);
custom.v.back().m_table = reinterpret_cast<const uint8_t*>(table) - ptr;
}
}
return true;
}
uint8_t* to()
{
if (m_from)
return m_from;
return const_cast<uint8_t*>(m_from);
else
return m_buffer->data();
}
Expand All @@ -1128,6 +1133,7 @@ class Arrays
return static_cast<uint32_t>(m_buffer->size());
}

private:
#pragma pack(push, 1)
struct Table
{
Expand Down Expand Up @@ -1168,7 +1174,7 @@ class Arrays
std::vector<Vec3f> v;
} custom;

uint8_t* m_from = nullptr;
const uint8_t* m_from = nullptr;
std::shared_ptr<std::vector<uint8_t>> m_buffer;
uint32_t m_table = 0;
};
Expand Down Expand Up @@ -1239,7 +1245,7 @@ class Complex
}
uint32_t size(const ids& id)
{
uint8_t* ptr = m_from ? m_from : &m_buffer->data()[0];
uint8_t* ptr = m_from ? const_cast<uint8_t*>(m_from) : &m_buffer->data()[0];
Table *table = reinterpret_cast<Table*>(&ptr[sizeof(_inner_::header)]);
const uint32_t offset = address(id, table);
return *reinterpret_cast<uint32_t*>(&ptr[offset]);
Expand All @@ -1256,7 +1262,7 @@ class Complex
h->signature1 = 'b';
m_table = sizeof(_inner_::header);
}
bool from(uint8_t* ptr)
bool from(const uint8_t* ptr)
{
if (!ptr)
return false;
Expand All @@ -1272,7 +1278,7 @@ class Complex
uint8_t* to()
{
if (m_from)
return m_from;
return const_cast<uint8_t*>(m_from);
else
return m_buffer->data();
}
Expand All @@ -1284,6 +1290,7 @@ class Complex
return static_cast<uint32_t>(m_buffer->size());
}

private:
#pragma pack(push, 1)
struct Table
{
Expand Down Expand Up @@ -1313,7 +1320,7 @@ class Complex
}
}

uint8_t* m_from = nullptr;
const uint8_t* m_from = nullptr;
std::shared_ptr<std::vector<uint8_t>> m_buffer;
uint32_t m_table = 0;
};
Expand Down

0 comments on commit 2af3b85

Please sign in to comment.