TypeGraph: Add Node IDs to non-leaf types

These aren't used for anything yet, but should be useful for stable IDs
when printing nodes before and after passes and for faster cycle
detection than the current map of pointers.
This commit is contained in:
Alastair Robertson 2023-07-05 01:14:05 -07:00 committed by Alastair Robertson
parent 608880e156
commit 23efc8d2d6
19 changed files with 296 additions and 234 deletions

View File

@ -98,13 +98,12 @@ void AddPadding::addPadding(const Member& prevMember,
if (paddingBits % 8 == 0) { if (paddingBits % 8 == 0) {
// Pad with an array of bytes // Pad with an array of bytes
auto* primitive = typeGraph_.make_type<Primitive>(Primitive::Kind::Int8); auto* primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int8);
auto* paddingArray = auto* paddingArray = typeGraph_.makeType<Array>(primitive, paddingBits / 8);
typeGraph_.make_type<Array>(primitive, paddingBits / 8);
paddedMembers.emplace_back(paddingArray, MemberPrefix, prevMemberEndBits); paddedMembers.emplace_back(paddingArray, MemberPrefix, prevMemberEndBits);
} else { } else {
// Pad with a bitfield // Pad with a bitfield
auto* primitive = typeGraph_.make_type<Primitive>(Primitive::Kind::Int64); auto* primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int64);
paddedMembers.emplace_back(primitive, MemberPrefix, prevMemberEndBits, paddedMembers.emplace_back(primitive, MemberPrefix, prevMemberEndBits,
paddingBits); paddingBits);
} }

View File

@ -86,7 +86,7 @@ Type* DrgnParser::enumerateType(struct drgn_type* type) {
return it->second; return it->second;
if (!drgn_utils::isSizeComplete(type)) { if (!drgn_utils::isSizeComplete(type)) {
return make_type<Primitive>(nullptr, Primitive::Kind::Void); return makeType<Primitive>(nullptr, Primitive::Kind::Void);
} }
enum drgn_type_kind kind = drgn_type_kind(type); enum drgn_type_kind kind = drgn_type_kind(type);
@ -135,7 +135,7 @@ Container* DrgnParser::enumerateContainer(struct drgn_type* type,
VLOG(2) << "Matching container `" << containerInfo.typeName << "` from `" VLOG(2) << "Matching container `" << containerInfo.typeName << "` from `"
<< fqName << "`" << std::endl; << fqName << "`" << std::endl;
auto* c = make_type<Container>(type, containerInfo, size); auto* c = makeType<Container>(type, containerInfo, size);
enumerateClassTemplateParams(type, c->templateParams); enumerateClassTemplateParams(type, c->templateParams);
return c; return c;
} }
@ -183,8 +183,8 @@ Type* DrgnParser::enumerateClass(struct drgn_type* type) {
std::to_string(drgn_type_kind(type))}; std::to_string(drgn_type_kind(type))};
} }
auto c = make_type<Class>(type, kind, std::move(name), std::move(fqName), auto c = makeType<Class>(type, kind, std::move(name), std::move(fqName), size,
size, virtuality); virtuality);
enumerateClassTemplateParams(type, c->templateParams); enumerateClassTemplateParams(type, c->templateParams);
enumerateClassParents(type, c->parents); enumerateClassParents(type, c->parents);
@ -417,7 +417,7 @@ Enum* DrgnParser::enumerateEnum(struct drgn_type* type) {
std::string name = drgn_type_tag(type); std::string name = drgn_type_tag(type);
uint64_t size = get_drgn_type_size(type); uint64_t size = get_drgn_type_size(type);
; ;
return make_type<Enum>(type, name, size); return makeType<Enum>(type, name, size);
} }
Typedef* DrgnParser::enumerateTypedef(struct drgn_type* type) { Typedef* DrgnParser::enumerateTypedef(struct drgn_type* type) {
@ -426,13 +426,13 @@ Typedef* DrgnParser::enumerateTypedef(struct drgn_type* type) {
struct drgn_type* underlyingType = drgn_type_type(type).type; struct drgn_type* underlyingType = drgn_type_type(type).type;
auto t = enumerateType(underlyingType); auto t = enumerateType(underlyingType);
return make_type<Typedef>(type, name, t); return makeType<Typedef>(type, name, t);
} }
Type* DrgnParser::enumeratePointer(struct drgn_type* type) { Type* DrgnParser::enumeratePointer(struct drgn_type* type) {
if (!chasePointer()) { if (!chasePointer()) {
// TODO dodgy nullptr - primitives should be handled as singletons // TODO dodgy nullptr - primitives should be handled as singletons
return make_type<Primitive>(nullptr, Primitive::Kind::UIntPtr); return makeType<Primitive>(nullptr, Primitive::Kind::UIntPtr);
} }
struct drgn_type* pointeeType = drgn_type_type(type).type; struct drgn_type* pointeeType = drgn_type_type(type).type;
@ -440,14 +440,14 @@ Type* DrgnParser::enumeratePointer(struct drgn_type* type) {
// TODO why was old CodeGen following funciton pointers? // TODO why was old CodeGen following funciton pointers?
Type* t = enumerateType(pointeeType); Type* t = enumerateType(pointeeType);
return make_type<Pointer>(type, t); return makeType<Pointer>(type, t);
} }
Array* DrgnParser::enumerateArray(struct drgn_type* type) { Array* DrgnParser::enumerateArray(struct drgn_type* type) {
struct drgn_type* elementType = drgn_type_type(type).type; struct drgn_type* elementType = drgn_type_type(type).type;
uint64_t len = drgn_type_length(type); uint64_t len = drgn_type_length(type);
auto t = enumerateType(elementType); auto t = enumerateType(elementType);
return make_type<Array>(type, t, len); return makeType<Array>(type, t, len);
} }
Primitive* DrgnParser::enumeratePrimitive(struct drgn_type* type) { Primitive* DrgnParser::enumeratePrimitive(struct drgn_type* type) {
@ -469,7 +469,7 @@ Primitive* DrgnParser::enumeratePrimitive(struct drgn_type* type) {
throw DrgnParserError{"Invalid drgn type kind for primitive: " + throw DrgnParserError{"Invalid drgn type kind for primitive: " +
std::to_string(drgn_type_kind(type))}; std::to_string(drgn_type_kind(type))};
} }
return make_type<Primitive>(type, kind); return makeType<Primitive>(type, kind);
} }
bool DrgnParser::chasePointer() const { bool DrgnParser::chasePointer() const {

View File

@ -68,10 +68,8 @@ class DrgnParser {
std::unordered_map<struct drgn_type*, Type*> drgn_types_; std::unordered_map<struct drgn_type*, Type*> drgn_types_;
template <typename T, typename... Args> template <typename T, typename... Args>
T* make_type(struct drgn_type* type, Args&&... args) { T* makeType(struct drgn_type* type, Args&&... args) {
auto type_unique_ptr = std::make_unique<T>(std::forward<Args>(args)...); auto* type_raw_ptr = typeGraph_.makeType<T>(std::forward<Args>(args)...);
auto type_raw_ptr = type_unique_ptr.get();
typeGraph_.add(std::move(type_unique_ptr));
drgn_types_.insert({type, type_raw_ptr}); drgn_types_.insert({type, type_raw_ptr});
return type_raw_ptr; return type_raw_ptr;
} }

View File

@ -44,9 +44,9 @@ void RemoveIgnored::visit(Class& c) {
if (!ignoreMember(c.name(), c.members[i].name)) { if (!ignoreMember(c.name(), c.members[i].name)) {
continue; continue;
} }
auto* primitive = typeGraph_.make_type<Primitive>(Primitive::Kind::Int8); auto* primitive = typeGraph_.makeType<Primitive>(Primitive::Kind::Int8);
auto* paddingArray = auto* paddingArray =
typeGraph_.make_type<Array>(primitive, c.members[i].type->size()); typeGraph_.makeType<Array>(primitive, c.members[i].type->size());
c.members[i] = c.members[i] =
Member{paddingArray, c.members[i].name, c.members[i].bitOffset}; Member{paddingArray, c.members[i].name, c.members[i].bitOffset};
} }

View File

@ -18,7 +18,7 @@
namespace type_graph { namespace type_graph {
template <> template <>
Primitive* TypeGraph::make_type<Primitive>(Primitive::Kind kind) { Primitive* TypeGraph::makeType<Primitive>(Primitive::Kind kind) {
switch (kind) { switch (kind) {
case Primitive::Kind::Int8: case Primitive::Kind::Int8:
static Primitive pInt8{kind}; static Primitive pInt8{kind};

View File

@ -23,6 +23,11 @@
namespace type_graph { namespace type_graph {
/*
* TypeGraph
*
* Holds the nodes and metadata which form a type graph.
*/
class TypeGraph { class TypeGraph {
public: public:
size_t size() const noexcept { size_t size() const noexcept {
@ -42,24 +47,33 @@ class TypeGraph {
rootTypes_.push_back(type); rootTypes_.push_back(type);
} }
// Override of the generic make_type function that returns singleton Primitive // Override of the generic makeType function that returns singleton Primitive
// objects // objects
template <typename T> template <typename T>
Primitive* make_type(Primitive::Kind kind); Primitive* makeType(Primitive::Kind kind);
template <typename T, typename... Args> template <typename T, typename... Args>
T* make_type(Args&&... args) { T* makeType(Args&&... args) {
static_assert(!std::is_same<T, Primitive>::value, static_assert(!std::is_same<T, Primitive>::value,
"Primitive singleton override should be used"); "Primitive singleton override should be used");
if constexpr (std::is_same<T, Class>::value ||
std::is_same<T, Container>::value ||
std::is_same<T, Array>::value ||
std::is_same<T, Typedef>::value ||
std::is_same<T, Pointer>::value) {
// Node ID required
auto type_unique_ptr =
std::make_unique<T>(next_id_++, std::forward<Args>(args)...);
auto type_raw_ptr = type_unique_ptr.get();
types_.push_back(std::move(type_unique_ptr));
return type_raw_ptr;
} else {
// No Node ID
auto type_unique_ptr = std::make_unique<T>(std::forward<Args>(args)...); auto type_unique_ptr = std::make_unique<T>(std::forward<Args>(args)...);
auto type_raw_ptr = type_unique_ptr.get(); auto type_raw_ptr = type_unique_ptr.get();
types_.push_back(std::move(type_unique_ptr)); types_.push_back(std::move(type_unique_ptr));
return type_raw_ptr; return type_raw_ptr;
} }
void add(std::unique_ptr<Type> type) {
types_.push_back(std::move(type));
} }
// TODO dodgy (use a getter instead to allow returning a const vector): // TODO dodgy (use a getter instead to allow returning a const vector):
@ -69,6 +83,7 @@ class TypeGraph {
std::vector<std::reference_wrapper<Type>> rootTypes_; std::vector<std::reference_wrapper<Type>> rootTypes_;
// Store all type objects in vectors for ownership. Order is not significant. // Store all type objects in vectors for ownership. Order is not significant.
std::vector<std::unique_ptr<Type>> types_; std::vector<std::unique_ptr<Type>> types_;
NodeId next_id_ = 0;
}; };
} // namespace type_graph } // namespace type_graph

View File

@ -74,7 +74,7 @@ void TypeIdentifier::visit(Container& c) {
if (std::regex_search(paramClass->fqName(), info.matcher)) { if (std::regex_search(paramClass->fqName(), info.matcher)) {
// Create dummy containers // Create dummy containers
auto* dummy = auto* dummy =
typeGraph_.make_type<Container>(info, param.type->size()); typeGraph_.makeType<Container>(info, param.type->size());
dummy->templateParams = paramClass->templateParams; dummy->templateParams = paramClass->templateParams;
c.templateParams[i] = dummy; c.templateParams[i] = dummy;
replaced = true; replaced = true;
@ -99,11 +99,11 @@ void TypeIdentifier::visit(Container& c) {
auto* allocator = auto* allocator =
dynamic_cast<Class*>(param.type); // TODO please don't do this... dynamic_cast<Class*>(param.type); // TODO please don't do this...
Type& typeToAllocate = *allocator->templateParams.at(0).type; Type& typeToAllocate = *allocator->templateParams.at(0).type;
auto* dummy = typeGraph_.make_type<DummyAllocator>(typeToAllocate, size, auto* dummy = typeGraph_.makeType<DummyAllocator>(typeToAllocate, size,
param.type->align()); param.type->align());
c.templateParams[i] = dummy; c.templateParams[i] = dummy;
} else { } else {
auto* dummy = typeGraph_.make_type<Dummy>(size, param.type->align()); auto* dummy = typeGraph_.makeType<Dummy>(size, param.type->align());
c.templateParams[i] = dummy; c.templateParams[i] = dummy;
} }
} }

View File

@ -15,6 +15,17 @@
*/ */
#pragma once #pragma once
/*
* This file contains the definitions for the classes which represent nodes
* (types) in a type graph.
*
* Edges in the graph are represented by references held by nodes. This means
* that once created, node addresses must be stable in order for the references
* to remain valid (i.e. don't store nodes directly in vectors). It is
* recommended to use the TypeGraph class when building a complete type graph as
* this will the memory allocations safely.
*/
#include <cstddef> #include <cstddef>
#include <optional> #include <optional>
#include <string> #include <string>
@ -38,6 +49,8 @@ struct ContainerInfo;
namespace type_graph { namespace type_graph {
using NodeId = int32_t;
enum class Qualifier { enum class Qualifier {
Const, Const,
Max, Max,
@ -122,20 +135,26 @@ class Class : public Type {
Union, Union,
}; };
Class(Kind kind, Class(NodeId id,
Kind kind,
std::string name, std::string name,
std::string fqName, std::string fqName,
size_t size, size_t size,
int virtuality = 0) int virtuality = 0)
: kind_(kind), : name_(std::move(name)),
name_(std::move(name)),
fqName_(std::move(fqName)), fqName_(std::move(fqName)),
size_(size), size_(size),
virtuality_(virtuality) { kind_(kind),
virtuality_(virtuality),
id_(id) {
} }
Class(Kind kind, const std::string& name, size_t size, int virtuality = 0) Class(NodeId id,
: Class(kind, name, name, size, virtuality) { Kind kind,
const std::string& name,
size_t size,
int virtuality = 0)
: Class(id, kind, name, name, size, virtuality) {
} }
DECLARE_ACCEPT DECLARE_ACCEPT
@ -182,6 +201,10 @@ class Class : public Type {
bool isDynamic() const; bool isDynamic() const;
NodeId id() const {
return id_;
}
std::vector<TemplateParam> templateParams; std::vector<TemplateParam> templateParams;
std::vector<Parent> parents; // Sorted by offset std::vector<Parent> parents; // Sorted by offset
std::vector<Member> members; // Sorted by offset std::vector<Member> members; // Sorted by offset
@ -190,21 +213,23 @@ class Class : public Type {
children; // Only for dynamic classes children; // Only for dynamic classes
private: private:
Kind kind_;
std::string name_; std::string name_;
std::string fqName_; std::string fqName_;
size_t size_; size_t size_;
int virtuality_;
uint64_t align_ = 0; uint64_t align_ = 0;
Kind kind_;
int virtuality_;
NodeId id_ = -1;
bool packed_ = false; bool packed_ = false;
}; };
class Container : public Type { class Container : public Type {
public: public:
Container(const ContainerInfo& containerInfo, size_t size) Container(NodeId id, const ContainerInfo& containerInfo, size_t size)
: containerInfo_(containerInfo), : containerInfo_(containerInfo),
name_(containerInfo.typeName), name_(containerInfo.typeName),
size_(size) { size_(size),
id_(id) {
} }
DECLARE_ACCEPT DECLARE_ACCEPT
@ -229,12 +254,17 @@ class Container : public Type {
return 8; // TODO not needed for containers? return 8; // TODO not needed for containers?
} }
NodeId id() const {
return id_;
}
std::vector<TemplateParam> templateParams; std::vector<TemplateParam> templateParams;
const ContainerInfo& containerInfo_; const ContainerInfo& containerInfo_;
private: private:
std::string name_; std::string name_;
size_t size_; size_t size_;
NodeId id_ = -1;
}; };
class Enum : public Type { class Enum : public Type {
@ -264,7 +294,8 @@ class Enum : public Type {
class Array : public Type { class Array : public Type {
public: public:
Array(Type* elementType, size_t len) : elementType_(elementType), len_(len) { Array(NodeId id, Type* elementType, size_t len)
: elementType_(elementType), len_(len), id_(id) {
} }
DECLARE_ACCEPT DECLARE_ACCEPT
@ -290,9 +321,14 @@ class Array : public Type {
return len_; return len_;
} }
NodeId id() const {
return id_;
}
private: private:
Type* elementType_; Type* elementType_;
size_t len_; size_t len_;
NodeId id_ = -1;
}; };
class Primitive : public Type { class Primitive : public Type {
@ -334,8 +370,8 @@ class Primitive : public Type {
class Typedef : public Type { class Typedef : public Type {
public: public:
explicit Typedef(const std::string& name, Type* underlyingType) explicit Typedef(NodeId id, const std::string& name, Type* underlyingType)
: name_(name), underlyingType_(underlyingType) { : name_(name), underlyingType_(underlyingType), id_(id) {
} }
DECLARE_ACCEPT DECLARE_ACCEPT
@ -360,14 +396,20 @@ class Typedef : public Type {
return underlyingType_; return underlyingType_;
} }
NodeId id() const {
return id_;
}
private: private:
std::string name_; std::string name_;
Type* underlyingType_; Type* underlyingType_;
NodeId id_ = -1;
}; };
class Pointer : public Type { class Pointer : public Type {
public: public:
explicit Pointer(Type* pointeeType) : pointeeType_(pointeeType) { explicit Pointer(NodeId id, Type* pointeeType)
: pointeeType_(pointeeType), id_(id) {
} }
DECLARE_ACCEPT DECLARE_ACCEPT
@ -388,8 +430,13 @@ class Pointer : public Type {
return pointeeType_; return pointeeType_;
} }
NodeId id() const {
return id_;
}
private: private:
Type* pointeeType_; Type* pointeeType_;
NodeId id_ = -1;
}; };
class Dummy : public Type { class Dummy : public Type {

View File

@ -7,7 +7,7 @@
using namespace type_graph; using namespace type_graph;
TEST(AddPaddingTest, BetweenMembers) { TEST(AddPaddingTest, BetweenMembers) {
auto myclass = Class{Class::Kind::Class, "MyClass", 16}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member(&myint8, "n1", 0)); myclass.members.push_back(Member(&myint8, "n1", 0));
@ -26,7 +26,7 @@ TEST(AddPaddingTest, BetweenMembers) {
} }
TEST(AddPaddingTest, AtEnd) { TEST(AddPaddingTest, AtEnd) {
auto myclass = Class{Class::Kind::Struct, "MyStruct", 16}; auto myclass = Class{0, Class::Kind::Struct, "MyStruct", 16};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member(&myint64, "n1", 0)); myclass.members.push_back(Member(&myint64, "n1", 0));
@ -45,7 +45,7 @@ TEST(AddPaddingTest, AtEnd) {
} }
TEST(AddPaddingTest, UnionNotPadded) { TEST(AddPaddingTest, UnionNotPadded) {
auto myclass = Class{Class::Kind::Union, "MyUnion", 8}; auto myclass = Class{0, Class::Kind::Union, "MyUnion", 8};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member(&myint64, "n1", 0)); myclass.members.push_back(Member(&myint64, "n1", 0));
@ -61,7 +61,7 @@ TEST(AddPaddingTest, UnionNotPadded) {
} }
TEST(AddPaddingTest, Bitfields) { TEST(AddPaddingTest, Bitfields) {
auto myclass = Class{Class::Kind::Class, "MyClass", 16}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
auto myint16 = Primitive{Primitive::Kind::Int16}; auto myint16 = Primitive{Primitive::Kind::Int16};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};

View File

@ -6,7 +6,7 @@
using namespace type_graph; using namespace type_graph;
TEST(AlignmentCalcTest, PrimitiveMembers) { TEST(AlignmentCalcTest, PrimitiveMembers) {
auto myclass = Class{Class::Kind::Class, "MyClass", 16}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member(&myint8, "n", 0)); myclass.members.push_back(Member(&myint8, "n", 0));
@ -22,12 +22,12 @@ TEST(AlignmentCalcTest, PrimitiveMembers) {
} }
TEST(AlignmentCalcTest, StructMembers) { TEST(AlignmentCalcTest, StructMembers) {
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 8}; auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 8};
auto myint32 = Primitive{Primitive::Kind::Int32}; auto myint32 = Primitive{Primitive::Kind::Int32};
mystruct.members.push_back(Member(&myint32, "n1", 0)); mystruct.members.push_back(Member(&myint32, "n1", 0));
mystruct.members.push_back(Member(&myint32, "n2", 4 * 8)); mystruct.members.push_back(Member(&myint32, "n2", 4 * 8));
auto myclass = Class{Class::Kind::Class, "MyClass", 12}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 12};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
myclass.members.push_back(Member(&myint8, "n", 0)); myclass.members.push_back(Member(&myint8, "n", 0));
myclass.members.push_back(Member(&mystruct, "s", 4 * 8)); myclass.members.push_back(Member(&mystruct, "s", 4 * 8));
@ -46,13 +46,13 @@ TEST(AlignmentCalcTest, StructMembers) {
} }
TEST(AlignmentCalcTest, StructInContainer) { TEST(AlignmentCalcTest, StructInContainer) {
auto myclass = Class{Class::Kind::Class, "MyClass", 16}; auto myclass = Class{1, Class::Kind::Class, "MyClass", 16};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member(&myint8, "n", 0)); myclass.members.push_back(Member(&myint8, "n", 0));
myclass.members.push_back(Member(&myint64, "n", 8 * 8)); myclass.members.push_back(Member(&myint64, "n", 8 * 8));
auto mycontainer = Container{ContainerInfo{}, 8}; auto mycontainer = Container{0, ContainerInfo{}, 8};
mycontainer.templateParams.push_back(&myclass); mycontainer.templateParams.push_back(&myclass);
test(AlignmentCalc::createPass(), {mycontainer}, R"( test(AlignmentCalc::createPass(), {mycontainer}, R"(
@ -67,7 +67,7 @@ TEST(AlignmentCalcTest, StructInContainer) {
} }
TEST(AlignmentCalcTest, Packed) { TEST(AlignmentCalcTest, Packed) {
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 9}; auto mystruct = Class{0, Class::Kind::Struct, "MyStruct", 9};
auto myint8 = Primitive{Primitive::Kind::Int8}; auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64}; auto myint64 = Primitive{Primitive::Kind::Int64};
mystruct.members.push_back(Member(&myint8, "n1", 0)); mystruct.members.push_back(Member(&myint8, "n1", 0));

View File

@ -38,7 +38,7 @@ void testTransform(Type& type,
TEST(CodeGenTest, TransformContainerAllocator) { TEST(CodeGenTest, TransformContainerAllocator) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myalloc = Class{Class::Kind::Struct, "MyAlloc", 8}; auto myalloc = Class{1, Class::Kind::Struct, "MyAlloc", 8};
myalloc.templateParams.push_back(TemplateParam{&myint}); myalloc.templateParams.push_back(TemplateParam{&myint});
myalloc.functions.push_back(Function{"allocate"}); myalloc.functions.push_back(Function{"allocate"});
myalloc.functions.push_back(Function{"deallocate"}); myalloc.functions.push_back(Function{"deallocate"});
@ -76,22 +76,23 @@ TEST(CodeGenTest, TransformContainerAllocatorParamInParent) {
Primitive myint{Primitive::Kind::Int32}; Primitive myint{Primitive::Kind::Int32};
Container pair{pairInfo, 8}; Container pair{3, pairInfo, 8};
pair.templateParams.push_back(TemplateParam{&myint, {Qualifier::Const}}); pair.templateParams.push_back(TemplateParam{&myint, {Qualifier::Const}});
pair.templateParams.push_back(TemplateParam{&myint}); pair.templateParams.push_back(TemplateParam{&myint});
Class myallocBase{Class::Kind::Struct, Class myallocBase{2, Class::Kind::Struct,
"MyAllocBase<std::pair<const int, int>>", 1}; "MyAllocBase<std::pair<const int, int>>", 1};
myallocBase.templateParams.push_back(TemplateParam{&pair}); myallocBase.templateParams.push_back(TemplateParam{&pair});
myallocBase.functions.push_back(Function{"allocate"}); myallocBase.functions.push_back(Function{"allocate"});
myallocBase.functions.push_back(Function{"deallocate"}); myallocBase.functions.push_back(Function{"deallocate"});
Class myalloc{Class::Kind::Struct, "MyAlloc<std::pair<const int, int>>", 1}; Class myalloc{1, Class::Kind::Struct, "MyAlloc<std::pair<const int, int>>",
1};
myalloc.parents.push_back(Parent{&myallocBase, 0}); myalloc.parents.push_back(Parent{&myallocBase, 0});
myalloc.functions.push_back(Function{"allocate"}); myalloc.functions.push_back(Function{"allocate"});
myalloc.functions.push_back(Function{"deallocate"}); myalloc.functions.push_back(Function{"deallocate"});
Container map{mapInfo, 24}; Container map{0, mapInfo, 24};
map.templateParams.push_back(TemplateParam{&myint}); map.templateParams.push_back(TemplateParam{&myint});
map.templateParams.push_back(TemplateParam{&myint}); map.templateParams.push_back(TemplateParam{&myint});
map.templateParams.push_back(TemplateParam{&myalloc}); map.templateParams.push_back(TemplateParam{&myalloc});

View File

@ -16,8 +16,8 @@ TEST(FlattenerTest, NoParents) {
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myenum = Enum{"MyEnum", 4}; auto myenum = Enum{"MyEnum", 4};
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 4}; auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 4};
auto myclass = Class{Class::Kind::Class, "MyClass", 12}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 12};
mystruct.members.push_back(Member(&myint, "n0", 0)); mystruct.members.push_back(Member(&myint, "n0", 0));
@ -50,9 +50,9 @@ TEST(FlattenerTest, OnlyParents) {
// int c; // int c;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{0, Class::Kind::Class, "ClassA", 8};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
@ -82,9 +82,9 @@ TEST(FlattenerTest, ParentsFirst) {
// int a; // int a;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
@ -117,9 +117,9 @@ TEST(FlattenerTest, MembersFirst) {
// int c; // int c;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -154,9 +154,9 @@ TEST(FlattenerTest, MixedMembersAndParents) {
// int c; // int c;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 16}; auto classA = Class{0, Class::Kind::Class, "ClassA", 16};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -193,9 +193,9 @@ TEST(FlattenerTest, EmptyParent) {
// int a2; // int a2;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
auto classB = Class{Class::Kind::Class, "ClassB", 0}; auto classB = Class{1, Class::Kind::Class, "ClassB", 0};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -230,10 +230,10 @@ TEST(FlattenerTest, TwoDeep) {
// int a; // int a;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 16}; auto classA = Class{0, Class::Kind::Class, "ClassA", 16};
auto classB = Class{Class::Kind::Class, "ClassB", 8}; auto classB = Class{1, Class::Kind::Class, "ClassB", 8};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
auto classD = Class{Class::Kind::Class, "ClassD", 4}; auto classD = Class{3, Class::Kind::Class, "ClassD", 4};
classD.members.push_back(Member(&myint, "d", 0)); classD.members.push_back(Member(&myint, "d", 0));
@ -273,9 +273,9 @@ TEST(FlattenerTest, DiamondInheritance) {
// int a; // int a;
// }; // };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 16}; auto classA = Class{0, Class::Kind::Class, "ClassA", 16};
auto classB = Class{Class::Kind::Class, "ClassB", 8}; auto classB = Class{1, Class::Kind::Class, "ClassB", 8};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -309,9 +309,9 @@ TEST(FlattenerTest, Member) {
// class B { int c; int b; }; // class B { int c; int b; };
// Class A { int a; B b; }; // Class A { int a; B b; };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
auto classB = Class{Class::Kind::Class, "ClassB", 8}; auto classB = Class{1, Class::Kind::Class, "ClassB", 8};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -344,9 +344,9 @@ TEST(FlattenerTest, MemberOfParent) {
// class C { int c; }; // class C { int c; };
// class A { int b; C c; int a; }; // class A { int b; C c; int a; };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
auto classB = Class{Class::Kind::Class, "ClassB", 8}; auto classB = Class{1, Class::Kind::Class, "ClassB", 8};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -379,8 +379,8 @@ TEST(FlattenerTest, ContainerParam) {
// class A { int b; int a; }; // class A { int b; int a; };
// std::vector<A, int> // std::vector<A, int>
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{1, Class::Kind::Class, "ClassA", 8};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{2, Class::Kind::Class, "ClassB", 4};
auto container = getVector(); auto container = getVector();
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
@ -411,14 +411,14 @@ TEST(FlattenerTest, Array) {
// A[5] // A[5]
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{2, Class::Kind::Class, "ClassB", 4};
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{1, Class::Kind::Class, "ClassA", 8};
classA.parents.push_back(Parent(&classB, 0)); classA.parents.push_back(Parent(&classB, 0));
classA.members.push_back(Member(&myint, "a", 4 * 8)); classA.members.push_back(Member(&myint, "a", 4 * 8));
auto arrayA = Array{&classA, 5}; auto arrayA = Array{0, &classA, 5};
test(Flattener::createPass(), {arrayA}, R"( test(Flattener::createPass(), {arrayA}, R"(
[0] Array: (length: 5) [0] Array: (length: 5)
@ -436,14 +436,14 @@ TEST(FlattenerTest, Typedef) {
// class A : B { int a; }; // class A : B { int a; };
// using aliasA = A; // using aliasA = A;
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{2, Class::Kind::Class, "ClassB", 4};
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{1, Class::Kind::Class, "ClassA", 8};
classA.parents.push_back(Parent(&classB, 0)); classA.parents.push_back(Parent(&classB, 0));
classA.members.push_back(Member(&myint, "a", 4 * 8)); classA.members.push_back(Member(&myint, "a", 4 * 8));
auto aliasA = Typedef{"aliasA", &classA}; auto aliasA = Typedef{0, "aliasA", &classA};
test(Flattener::createPass(), {aliasA}, R"( test(Flattener::createPass(), {aliasA}, R"(
[0] Typedef: aliasA [0] Typedef: aliasA
@ -461,12 +461,12 @@ TEST(FlattenerTest, TypedefParent) {
// using aliasB = B; // using aliasB = B;
// class A : aliasB { int a; }; // class A : aliasB { int a; };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
auto aliasB = Typedef{"aliasB", &classB}; auto aliasB = Typedef{2, "aliasB", &classB};
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{0, Class::Kind::Class, "ClassA", 8};
classA.parents.push_back(Parent(&aliasB, 0)); classA.parents.push_back(Parent(&aliasB, 0));
classA.members.push_back(Member(&myint, "a", 4 * 8)); classA.members.push_back(Member(&myint, "a", 4 * 8));
@ -486,15 +486,15 @@ TEST(FlattenerTest, Pointer) {
// class C { A a; }; // class C { A a; };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{3, Class::Kind::Class, "ClassB", 4};
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{2, Class::Kind::Class, "ClassA", 8};
classA.parents.push_back(Parent(&classB, 0)); classA.parents.push_back(Parent(&classB, 0));
classA.members.push_back(Member(&myint, "a", 4 * 8)); classA.members.push_back(Member(&myint, "a", 4 * 8));
auto ptrA = Pointer{&classA}; auto ptrA = Pointer{1, &classA};
auto classC = Class{Class::Kind::Class, "ClassC", 8}; auto classC = Class{0, Class::Kind::Class, "ClassC", 8};
classC.members.push_back(Member(&ptrA, "a", 0)); classC.members.push_back(Member(&ptrA, "a", 0));
test(Flattener::createPass(), {classC}, R"( test(Flattener::createPass(), {classC}, R"(
@ -516,9 +516,9 @@ TEST(FlattenerTest, PointerCycle) {
// //
// Flattened: // Flattened:
// No change // No change
auto classA = Class{Class::Kind::Class, "ClassA", 69}; auto classA = Class{0, Class::Kind::Class, "ClassA", 69};
auto classB = Class{Class::Kind::Class, "ClassB", 69}; auto classB = Class{1, Class::Kind::Class, "ClassB", 69};
auto ptrA = Pointer{&classA}; auto ptrA = Pointer{2, &classA};
classA.members.push_back(Member(&classB, "b", 0)); classA.members.push_back(Member(&classB, "b", 0));
classB.members.push_back(Member(&ptrA, "a", 0)); classB.members.push_back(Member(&ptrA, "a", 0));
@ -539,9 +539,9 @@ TEST(FlattenerTest, Alignment) {
// class B { alignas(8) int b; }; // class B { alignas(8) int b; };
// class A : B, C { int a; }; // class A : B, C { int a; };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.setAlign(16); classC.setAlign(16);
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
@ -583,9 +583,9 @@ TEST(FlattenerTest, Functions) {
// class C { void funcC(); }; // class C { void funcC(); };
// class B : C { void funcB(); }; // class B : C { void funcB(); };
// class A : B { void funcA(); }; // class A : B { void funcA(); };
auto classA = Class{Class::Kind::Class, "ClassA", 0}; auto classA = Class{0, Class::Kind::Class, "ClassA", 0};
auto classB = Class{Class::Kind::Class, "ClassB", 0}; auto classB = Class{1, Class::Kind::Class, "ClassB", 0};
auto classC = Class{Class::Kind::Class, "ClassC", 0}; auto classC = Class{2, Class::Kind::Class, "ClassC", 0};
classA.parents.push_back(Parent(&classB, 0)); classA.parents.push_back(Parent(&classB, 0));
classB.parents.push_back(Parent(&classC, 0)); classB.parents.push_back(Parent(&classC, 0));
@ -608,9 +608,9 @@ TEST(FlattenerTest, Children) {
// class B { int b; }; // class B { int b; };
// class A : B, C { }; // class A : B, C { };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{1, Class::Kind::Class, "ClassA", 8};
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{0, Class::Kind::Class, "ClassB", 4};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{2, Class::Kind::Class, "ClassC", 4};
classC.members.push_back(Member(&myint, "c", 0)); classC.members.push_back(Member(&myint, "c", 0));
classB.members.push_back(Member(&myint, "b", 0)); classB.members.push_back(Member(&myint, "b", 0));
@ -641,10 +641,10 @@ TEST(FlattenerTest, ChildrenTwoDeep) {
// class B : D { int b; }; // class B : D { int b; };
// class A : B, C { int a; }; // class A : B, C { int a; };
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto classA = Class{Class::Kind::Class, "ClassA", 16}; auto classA = Class{2, Class::Kind::Class, "ClassA", 16};
auto classB = Class{Class::Kind::Class, "ClassB", 8}; auto classB = Class{1, Class::Kind::Class, "ClassB", 8};
auto classC = Class{Class::Kind::Class, "ClassC", 4}; auto classC = Class{3, Class::Kind::Class, "ClassC", 4};
auto classD = Class{Class::Kind::Class, "ClassD", 4}; auto classD = Class{0, Class::Kind::Class, "ClassD", 4};
classD.members.push_back(Member(&myint, "d", 0)); classD.members.push_back(Member(&myint, "d", 0));
@ -690,7 +690,7 @@ TEST(FlattenerTest, ParentContainer) {
auto vector = getVector(); auto vector = getVector();
vector.templateParams.push_back(TemplateParam{&myint}); vector.templateParams.push_back(TemplateParam{&myint});
auto classA = Class{Class::Kind::Class, "ClassA", 32}; auto classA = Class{0, Class::Kind::Class, "ClassA", 32};
classA.parents.push_back(Parent{&vector, 0}); classA.parents.push_back(Parent{&vector, 0});
classA.members.push_back(Member{&myint, "a", 24 * 8}); classA.members.push_back(Member{&myint, "a", 24 * 8});
@ -720,7 +720,7 @@ TEST(FlattenerTest, ParentTwoContainers) {
auto vector = getVector(); auto vector = getVector();
vector.templateParams.push_back(TemplateParam{&myint}); vector.templateParams.push_back(TemplateParam{&myint});
auto classA = Class{Class::Kind::Class, "ClassA", 48}; auto classA = Class{0, Class::Kind::Class, "ClassA", 48};
classA.parents.push_back(Parent{&vector, 0}); classA.parents.push_back(Parent{&vector, 0});
classA.parents.push_back(Parent{&vector, 24 * 8}); classA.parents.push_back(Parent{&vector, 24 * 8});
@ -750,10 +750,10 @@ TEST(FlattenerTest, ParentClassAndContainer) {
auto vector = getVector(); auto vector = getVector();
vector.templateParams.push_back(TemplateParam{&myint}); vector.templateParams.push_back(TemplateParam{&myint});
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
classB.members.push_back(Member{&myint, "b", 0}); classB.members.push_back(Member{&myint, "b", 0});
auto classA = Class{Class::Kind::Class, "ClassA", 32}; auto classA = Class{0, Class::Kind::Class, "ClassA", 32};
classA.parents.push_back(Parent{&classB, 0}); classA.parents.push_back(Parent{&classB, 0});
classA.parents.push_back(Parent{&vector, 8 * 8}); classA.parents.push_back(Parent{&vector, 8 * 8});
@ -787,22 +787,23 @@ TEST(FlattenerTest, AllocatorParamInParent) {
Primitive myint{Primitive::Kind::Int32}; Primitive myint{Primitive::Kind::Int32};
Container pair{pairInfo, 8}; Container pair{3, pairInfo, 8};
pair.templateParams.push_back(TemplateParam{&myint, {Qualifier::Const}}); pair.templateParams.push_back(TemplateParam{&myint, {Qualifier::Const}});
pair.templateParams.push_back(TemplateParam{&myint}); pair.templateParams.push_back(TemplateParam{&myint});
Class myallocBase{Class::Kind::Struct, Class myallocBase{2, Class::Kind::Struct,
"MyAllocBase<std::pair<const int, int>>", 1}; "MyAllocBase<std::pair<const int, int>>", 1};
myallocBase.templateParams.push_back(TemplateParam{&pair}); myallocBase.templateParams.push_back(TemplateParam{&pair});
myallocBase.functions.push_back(Function{"allocate"}); myallocBase.functions.push_back(Function{"allocate"});
myallocBase.functions.push_back(Function{"deallocate"}); myallocBase.functions.push_back(Function{"deallocate"});
Class myalloc{Class::Kind::Struct, "MyAlloc<std::pair<const int, int>>", 1}; Class myalloc{1, Class::Kind::Struct, "MyAlloc<std::pair<const int, int>>",
1};
myalloc.parents.push_back(Parent{&myallocBase, 0}); myalloc.parents.push_back(Parent{&myallocBase, 0});
myalloc.functions.push_back(Function{"allocate"}); myalloc.functions.push_back(Function{"allocate"});
myalloc.functions.push_back(Function{"deallocate"}); myalloc.functions.push_back(Function{"deallocate"});
Container map{mapInfo, 24}; Container map{0, mapInfo, 24};
map.templateParams.push_back(TemplateParam{&myint}); map.templateParams.push_back(TemplateParam{&myint});
map.templateParams.push_back(TemplateParam{&myint}); map.templateParams.push_back(TemplateParam{&myint});
map.templateParams.push_back(TemplateParam{&myalloc}); map.templateParams.push_back(TemplateParam{&myalloc});
@ -853,12 +854,12 @@ TEST(FlattenerTest, AllocatorParamInParent) {
TEST(FlattenerTest, ClassParam) { TEST(FlattenerTest, ClassParam) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto mychild = Class{Class::Kind::Class, "MyChild", 4}; auto mychild = Class{1, Class::Kind::Class, "MyChild", 4};
auto myparent = Class{Class::Kind::Class, "MyParent", 4}; auto myparent = Class{2, Class::Kind::Class, "MyParent", 4};
myparent.members.push_back(Member{&myint, "a", 0}); myparent.members.push_back(Member{&myint, "a", 0});
mychild.parents.push_back(Parent{&myparent, 0}); mychild.parents.push_back(Parent{&myparent, 0});
auto myclass = Class{Class::Kind::Class, "MyClass", 4}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 4};
myclass.templateParams.push_back(TemplateParam{&mychild}); myclass.templateParams.push_back(TemplateParam{&mychild});
test(Flattener::createPass(), {myclass}, R"( test(Flattener::createPass(), {myclass}, R"(

View File

@ -7,9 +7,9 @@
using namespace type_graph; using namespace type_graph;
TEST(NameGenTest, ClassParams) { TEST(NameGenTest, ClassParams) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass<MyParam, MyParam>", 13}; auto myclass = Class{2, Class::Kind::Struct, "MyClass<MyParam, MyParam>", 13};
myclass.templateParams.push_back(&myparam1); myclass.templateParams.push_back(&myparam1);
myclass.templateParams.push_back(&myparam2); myclass.templateParams.push_back(&myparam2);
@ -26,7 +26,7 @@ TEST(NameGenTest, ClassContainerParam) {
auto myparam = getVector(); auto myparam = getVector();
myparam.templateParams.push_back(&myint); myparam.templateParams.push_back(&myint);
auto myclass = Class{Class::Kind::Struct, "MyClass", 13}; auto myclass = Class{0, Class::Kind::Struct, "MyClass", 13};
myclass.templateParams.push_back(&myparam); myclass.templateParams.push_back(&myparam);
NameGen nameGen; NameGen nameGen;
@ -37,9 +37,9 @@ TEST(NameGenTest, ClassContainerParam) {
} }
TEST(NameGenTest, ClassParents) { TEST(NameGenTest, ClassParents) {
auto myparent1 = Class{Class::Kind::Struct, "MyParent", 13}; auto myparent1 = Class{0, Class::Kind::Struct, "MyParent", 13};
auto myparent2 = Class{Class::Kind::Struct, "MyParent", 13}; auto myparent2 = Class{1, Class::Kind::Struct, "MyParent", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass", 13}; auto myclass = Class{2, Class::Kind::Struct, "MyClass", 13};
myclass.parents.push_back(Parent{&myparent1, 0}); myclass.parents.push_back(Parent{&myparent1, 0});
myclass.parents.push_back(Parent{&myparent2, 0}); myclass.parents.push_back(Parent{&myparent2, 0});
@ -52,9 +52,9 @@ TEST(NameGenTest, ClassParents) {
} }
TEST(NameGenTest, ClassMembers) { TEST(NameGenTest, ClassMembers) {
auto mymember1 = Class{Class::Kind::Struct, "MyMember", 13}; auto mymember1 = Class{0, Class::Kind::Struct, "MyMember", 13};
auto mymember2 = Class{Class::Kind::Struct, "MyMember", 13}; auto mymember2 = Class{1, Class::Kind::Struct, "MyMember", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass", 13}; auto myclass = Class{2, Class::Kind::Struct, "MyClass", 13};
// A class may end up with members sharing a name after flattening // A class may end up with members sharing a name after flattening
myclass.members.push_back(Member{&mymember1, "mem", 0}); myclass.members.push_back(Member{&mymember1, "mem", 0});
@ -71,9 +71,9 @@ TEST(NameGenTest, ClassMembers) {
} }
TEST(NameGenTest, ClassChildren) { TEST(NameGenTest, ClassChildren) {
auto mychild1 = Class{Class::Kind::Struct, "MyChild", 13}; auto mychild1 = Class{0, Class::Kind::Struct, "MyChild", 13};
auto mychild2 = Class{Class::Kind::Struct, "MyChild", 13}; auto mychild2 = Class{1, Class::Kind::Struct, "MyChild", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass", 13}; auto myclass = Class{2, Class::Kind::Struct, "MyClass", 13};
myclass.children.push_back(mychild1); myclass.children.push_back(mychild1);
myclass.children.push_back(mychild2); myclass.children.push_back(mychild2);
@ -86,8 +86,8 @@ TEST(NameGenTest, ClassChildren) {
} }
TEST(NameGenTest, ContainerParams) { TEST(NameGenTest, ContainerParams) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1); mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2); mycontainer.templateParams.push_back(&myparam2);
@ -101,7 +101,7 @@ TEST(NameGenTest, ContainerParams) {
} }
TEST(NameGenTest, ContainerParamsDuplicates) { TEST(NameGenTest, ContainerParamsDuplicates) {
auto myparam = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam = Class{0, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam); mycontainer.templateParams.push_back(&myparam);
mycontainer.templateParams.push_back(&myparam); mycontainer.templateParams.push_back(&myparam);
@ -114,7 +114,7 @@ TEST(NameGenTest, ContainerParamsDuplicates) {
} }
TEST(NameGenTest, ContainerParamsDuplicatesDeep) { TEST(NameGenTest, ContainerParamsDuplicatesDeep) {
auto myparam = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam = Class{0, Class::Kind::Struct, "MyParam", 13};
auto mycontainer1 = getVector(); auto mycontainer1 = getVector();
mycontainer1.templateParams.push_back(&myparam); mycontainer1.templateParams.push_back(&myparam);
@ -133,9 +133,9 @@ TEST(NameGenTest, ContainerParamsDuplicatesDeep) {
} }
TEST(NameGenTest, ContainerParamsDuplicatesAcrossContainers) { TEST(NameGenTest, ContainerParamsDuplicatesAcrossContainers) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto myparam3 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam3 = Class{2, Class::Kind::Struct, "MyParam", 13};
auto mycontainer1 = getVector(); auto mycontainer1 = getVector();
mycontainer1.templateParams.push_back(&myparam1); mycontainer1.templateParams.push_back(&myparam1);
@ -156,8 +156,8 @@ TEST(NameGenTest, ContainerParamsDuplicatesAcrossContainers) {
} }
TEST(NameGenTest, ContainerParamsConst) { TEST(NameGenTest, ContainerParamsConst) {
auto myparam1 = Class{Class::Kind::Struct, "MyConstParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyConstParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back( mycontainer.templateParams.push_back(
@ -198,14 +198,14 @@ TEST(NameGenTest, ContainerParamsValue) {
} }
TEST(NameGenTest, Array) { TEST(NameGenTest, Array) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1); mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2); mycontainer.templateParams.push_back(&myparam2);
auto myarray = Array{&mycontainer, 5}; auto myarray = Array{2, &mycontainer, 5};
NameGen nameGen; NameGen nameGen;
nameGen.generateNames({myarray}); nameGen.generateNames({myarray});
@ -217,14 +217,14 @@ TEST(NameGenTest, Array) {
} }
TEST(NameGenTest, Typedef) { TEST(NameGenTest, Typedef) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1); mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2); mycontainer.templateParams.push_back(&myparam2);
auto mytypedef = Typedef{"MyTypedef", &mycontainer}; auto mytypedef = Typedef{2, "MyTypedef", &mycontainer};
NameGen nameGen; NameGen nameGen;
nameGen.generateNames({mytypedef}); nameGen.generateNames({mytypedef});
@ -237,7 +237,7 @@ TEST(NameGenTest, Typedef) {
TEST(NameGenTest, TypedefAliasTemplate) { TEST(NameGenTest, TypedefAliasTemplate) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto mytypedef = Typedef{"MyTypedef<ParamA, ParamB>", &myint}; auto mytypedef = Typedef{0, "MyTypedef<ParamA, ParamB>", &myint};
NameGen nameGen; NameGen nameGen;
nameGen.generateNames({mytypedef}); nameGen.generateNames({mytypedef});
@ -246,14 +246,14 @@ TEST(NameGenTest, TypedefAliasTemplate) {
} }
TEST(NameGenTest, Pointer) { TEST(NameGenTest, Pointer) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1); mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2); mycontainer.templateParams.push_back(&myparam2);
auto mypointer = Pointer{&mycontainer}; auto mypointer = Pointer{2, &mycontainer};
NameGen nameGen; NameGen nameGen;
nameGen.generateNames({mypointer}); nameGen.generateNames({mypointer});
@ -274,8 +274,8 @@ TEST(NameGenTest, Dummy) {
} }
TEST(NameGenTest, DummyAllocator) { TEST(NameGenTest, DummyAllocator) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam1 = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam2 = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1); mycontainer.templateParams.push_back(&myparam1);
@ -294,9 +294,9 @@ TEST(NameGenTest, DummyAllocator) {
} }
TEST(NameGenTest, Cycle) { TEST(NameGenTest, Cycle) {
auto classA = Class{Class::Kind::Class, "ClassA", 69}; auto classA = Class{0, Class::Kind::Class, "ClassA", 69};
auto classB = Class{Class::Kind::Class, "ClassB", 69}; auto classB = Class{1, Class::Kind::Class, "ClassB", 69};
auto ptrA = Pointer{&classA}; auto ptrA = Pointer{2, &classA};
classA.members.push_back(Member(&classB, "b", 0)); classA.members.push_back(Member(&classB, "b", 0));
classB.members.push_back(Member(&ptrA, "a", 0)); classB.members.push_back(Member(&ptrA, "a", 0));
@ -309,7 +309,7 @@ TEST(NameGenTest, Cycle) {
TEST(NameGenTest, ContainerCycle) { TEST(NameGenTest, ContainerCycle) {
auto container = getVector(); auto container = getVector();
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 69};
myclass.members.push_back(Member(&container, "c", 0)); myclass.members.push_back(Member(&container, "c", 0));
container.templateParams.push_back(TemplateParam(&myclass)); container.templateParams.push_back(TemplateParam(&myclass));

View File

@ -7,9 +7,9 @@
using namespace type_graph; using namespace type_graph;
TEST(RemoveIgnoredTest, Match) { TEST(RemoveIgnoredTest, Match) {
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
classA.members.push_back(Member(&classB, "a", 0)); classA.members.push_back(Member(&classB, "a", 0));
classA.members.push_back(Member(&classB, "b", 4 * 8)); classA.members.push_back(Member(&classB, "b", 4 * 8));
classA.members.push_back(Member(&classB, "c", 8 * 8)); classA.members.push_back(Member(&classB, "c", 8 * 8));
@ -40,9 +40,9 @@ TEST(RemoveIgnoredTest, Match) {
} }
TEST(RemoveIgnoredTest, TypeMatchMemberMiss) { TEST(RemoveIgnoredTest, TypeMatchMemberMiss) {
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
classA.members.push_back(Member(&classB, "a", 0)); classA.members.push_back(Member(&classB, "a", 0));
classA.members.push_back(Member(&classB, "b", 4 * 8)); classA.members.push_back(Member(&classB, "b", 4 * 8));
classA.members.push_back(Member(&classB, "c", 8 * 8)); classA.members.push_back(Member(&classB, "c", 8 * 8));
@ -63,9 +63,9 @@ TEST(RemoveIgnoredTest, TypeMatchMemberMiss) {
} }
TEST(RemoveIgnoredTest, MemberMatchWrongType) { TEST(RemoveIgnoredTest, MemberMatchWrongType) {
auto classB = Class{Class::Kind::Class, "ClassB", 4}; auto classB = Class{1, Class::Kind::Class, "ClassB", 4};
auto classA = Class{Class::Kind::Class, "ClassA", 12}; auto classA = Class{0, Class::Kind::Class, "ClassA", 12};
classA.members.push_back(Member(&classB, "a", 0)); classA.members.push_back(Member(&classB, "a", 0));
classA.members.push_back(Member(&classB, "b", 4 * 8)); classA.members.push_back(Member(&classB, "b", 4 * 8));
classA.members.push_back(Member(&classB, "c", 8 * 8)); classA.members.push_back(Member(&classB, "c", 8 * 8));

View File

@ -9,10 +9,10 @@ using namespace type_graph;
TEST(RemoveTopLevelPointerTest, TopLevelPointerRemoved) { TEST(RemoveTopLevelPointerTest, TopLevelPointerRemoved) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myclass = Class{Class::Kind::Class, "MyClass", 4}; auto myclass = Class{1, Class::Kind::Class, "MyClass", 4};
myclass.members.push_back(Member(&myint, "n", 0)); myclass.members.push_back(Member(&myint, "n", 0));
auto ptrA = Pointer{&myclass}; auto ptrA = Pointer{0, &myclass};
test(RemoveTopLevelPointer::createPass(), {ptrA}, R"( test(RemoveTopLevelPointer::createPass(), {ptrA}, R"(
[0] Class: MyClass (size: 4) [0] Class: MyClass (size: 4)
@ -24,7 +24,7 @@ TEST(RemoveTopLevelPointerTest, TopLevelPointerRemoved) {
TEST(RemoveTopLevelPointerTest, TopLevelClassUntouched) { TEST(RemoveTopLevelPointerTest, TopLevelClassUntouched) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myclass = Class{Class::Kind::Class, "MyClass", 4}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 4};
myclass.members.push_back(Member(&myint, "n", 0)); myclass.members.push_back(Member(&myint, "n", 0));
test(RemoveTopLevelPointer::createPass(), {myclass}, R"( test(RemoveTopLevelPointer::createPass(), {myclass}, R"(
@ -36,9 +36,9 @@ TEST(RemoveTopLevelPointerTest, TopLevelClassUntouched) {
TEST(RemoveTopLevelPointerTest, IntermediatePointerUntouched) { TEST(RemoveTopLevelPointerTest, IntermediatePointerUntouched) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto ptrInt = Pointer{&myint}; auto ptrInt = Pointer{1, &myint};
auto myclass = Class{Class::Kind::Class, "MyClass", 4}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 4};
myclass.members.push_back(Member(&ptrInt, "n", 0)); myclass.members.push_back(Member(&ptrInt, "n", 0));
test(RemoveTopLevelPointer::createPass(), {myclass}, R"( test(RemoveTopLevelPointer::createPass(), {myclass}, R"(

View File

@ -33,9 +33,9 @@ TEST(TopoSorterTest, SingleType) {
} }
TEST(TopoSorterTest, UnrelatedTypes) { TEST(TopoSorterTest, UnrelatedTypes) {
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 13}; auto mystruct = Class{0, Class::Kind::Struct, "MyStruct", 13};
auto myenum = Enum{"MyEnum", 4}; auto myenum = Enum{"MyEnum", 4};
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
// Try the same input in a few different orders and ensure they output order // Try the same input in a few different orders and ensure they output order
// matches the input order // matches the input order
@ -57,9 +57,9 @@ MyStruct
} }
TEST(TopoSorterTest, ClassMembers) { TEST(TopoSorterTest, ClassMembers) {
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 13}; auto mystruct = Class{0, Class::Kind::Struct, "MyStruct", 13};
auto myenum = Enum{"MyEnum", 4}; auto myenum = Enum{"MyEnum", 4};
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
myclass.members.push_back(Member(&mystruct, "n", 0)); myclass.members.push_back(Member(&mystruct, "n", 0));
myclass.members.push_back(Member(&myenum, "e", 4)); myclass.members.push_back(Member(&myenum, "e", 4));
@ -71,8 +71,8 @@ MyClass
} }
TEST(TopoSorterTest, Parents) { TEST(TopoSorterTest, Parents) {
auto myparent = Class{Class::Kind::Struct, "MyParent", 13}; auto myparent = Class{0, Class::Kind::Struct, "MyParent", 13};
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
myclass.parents.push_back(Parent(&myparent, 0)); myclass.parents.push_back(Parent(&myparent, 0));
test({myclass}, R"( test({myclass}, R"(
@ -82,8 +82,8 @@ MyClass
} }
TEST(TopoSorterTest, TemplateParams) { TEST(TopoSorterTest, TemplateParams) {
auto myparam = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam = Class{0, Class::Kind::Struct, "MyParam", 13};
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
myclass.templateParams.push_back(TemplateParam(&myparam)); myclass.templateParams.push_back(TemplateParam(&myparam));
test({myclass}, R"( test({myclass}, R"(
@ -93,11 +93,11 @@ MyClass
} }
TEST(TopoSorterTest, Children) { TEST(TopoSorterTest, Children) {
auto mymember = Class{Class::Kind::Struct, "MyMember", 13}; auto mymember = Class{0, Class::Kind::Struct, "MyMember", 13};
auto mychild = Class{Class::Kind::Struct, "MyChild", 13}; auto mychild = Class{1, Class::Kind::Struct, "MyChild", 13};
mychild.members.push_back(Member(&mymember, "mymember", 0)); mychild.members.push_back(Member(&mymember, "mymember", 0));
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
mychild.parents.push_back(Parent(&myclass, 0)); mychild.parents.push_back(Parent(&myclass, 0));
myclass.children.push_back(mychild); myclass.children.push_back(mychild);
@ -125,9 +125,9 @@ TEST(TopoSorterTest, ChildrenCycle) {
// class MyChild : MyParent { // class MyChild : MyParent {
// A a; // A a;
// }; // };
auto myparent = Class{Class::Kind::Class, "MyParent", 69}; auto myparent = Class{0, Class::Kind::Class, "MyParent", 69};
auto classA = Class{Class::Kind::Struct, "ClassA", 5}; auto classA = Class{1, Class::Kind::Struct, "ClassA", 5};
auto mychild = Class{Class::Kind::Struct, "MyChild", 13}; auto mychild = Class{2, Class::Kind::Struct, "MyChild", 13};
mychild.parents.push_back(Parent(&myparent, 0)); mychild.parents.push_back(Parent(&myparent, 0));
myparent.children.push_back(mychild); myparent.children.push_back(mychild);
@ -153,8 +153,8 @@ MyChild
} }
TEST(TopoSorterTest, Containers) { TEST(TopoSorterTest, Containers) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam1", 13}; auto myparam1 = Class{1, Class::Kind::Struct, "MyParam1", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam2", 13}; auto myparam2 = Class{2, Class::Kind::Struct, "MyParam2", 13};
auto mycontainer = getMap(); auto mycontainer = getMap();
mycontainer.templateParams.push_back((&myparam1)); mycontainer.templateParams.push_back((&myparam1));
mycontainer.templateParams.push_back((&myparam2)); mycontainer.templateParams.push_back((&myparam2));
@ -168,7 +168,7 @@ std::map
TEST(TopoSorterTest, ContainersVector) { TEST(TopoSorterTest, ContainersVector) {
// std::vector allows forward declared template parameters // std::vector allows forward declared template parameters
auto myparam = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector(); auto mycontainer = getVector();
mycontainer.templateParams.push_back((&myparam)); mycontainer.templateParams.push_back((&myparam));
@ -180,7 +180,7 @@ MyParam
TEST(TopoSorterTest, ContainersList) { TEST(TopoSorterTest, ContainersList) {
// std::list allows forward declared template parameters // std::list allows forward declared template parameters
auto myparam = Class{Class::Kind::Struct, "MyParam", 13}; auto myparam = Class{1, Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getList(); auto mycontainer = getList();
mycontainer.templateParams.push_back((&myparam)); mycontainer.templateParams.push_back((&myparam));
@ -191,15 +191,15 @@ MyParam
} }
TEST(TopoSorterTest, Arrays) { TEST(TopoSorterTest, Arrays) {
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 69};
auto myarray = Array{&myclass, 10}; auto myarray = Array{1, &myclass, 10};
test({myarray}, "MyClass\n"); test({myarray}, "MyClass\n");
} }
TEST(TopoSorterTest, Typedef) { TEST(TopoSorterTest, Typedef) {
auto classA = Class{Class::Kind::Class, "ClassA", 8}; auto classA = Class{0, Class::Kind::Class, "ClassA", 8};
auto aliasA = Typedef{"aliasA", &classA}; auto aliasA = Typedef{1, "aliasA", &classA};
test({aliasA}, R"( test({aliasA}, R"(
ClassA ClassA
@ -209,16 +209,16 @@ aliasA
TEST(TopoSorterTest, Pointers) { TEST(TopoSorterTest, Pointers) {
// Pointers do not require pointee types to be defined first // Pointers do not require pointee types to be defined first
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{0, Class::Kind::Class, "MyClass", 69};
auto mypointer = Pointer{&myclass}; auto mypointer = Pointer{1, &myclass};
test({mypointer}, "MyClass"); test({mypointer}, "MyClass");
} }
TEST(TopoSorterTest, PointerCycle) { TEST(TopoSorterTest, PointerCycle) {
auto classA = Class{Class::Kind::Class, "ClassA", 69}; auto classA = Class{0, Class::Kind::Class, "ClassA", 69};
auto classB = Class{Class::Kind::Class, "ClassB", 69}; auto classB = Class{1, Class::Kind::Class, "ClassB", 69};
auto ptrA = Pointer{&classA}; auto ptrA = Pointer{2, &classA};
classA.members.push_back(Member(&classB, "b", 0)); classA.members.push_back(Member(&classB, "b", 0));
classB.members.push_back(Member(&ptrA, "a", 0)); classB.members.push_back(Member(&ptrA, "a", 0));
@ -239,9 +239,9 @@ ClassA
} }
TEST(TopoSorterTest, TwoDeep) { TEST(TopoSorterTest, TwoDeep) {
auto myunion = Class{Class::Kind::Union, "MyUnion", 7}; auto myunion = Class{0, Class::Kind::Union, "MyUnion", 7};
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 13}; auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 13};
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
myclass.members.push_back(Member(&mystruct, "mystruct", 0)); myclass.members.push_back(Member(&mystruct, "mystruct", 0));
mystruct.members.push_back(Member(&myunion, "myunion", 0)); mystruct.members.push_back(Member(&myunion, "myunion", 0));
@ -253,9 +253,9 @@ MyClass
} }
TEST(TopoSorterTest, MultiplePaths) { TEST(TopoSorterTest, MultiplePaths) {
auto myunion = Class{Class::Kind::Union, "MyUnion", 7}; auto myunion = Class{0, Class::Kind::Union, "MyUnion", 7};
auto mystruct = Class{Class::Kind::Struct, "MyStruct", 13}; auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 13};
auto myclass = Class{Class::Kind::Class, "MyClass", 69}; auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
myclass.members.push_back(Member(&mystruct, "mystruct", 0)); myclass.members.push_back(Member(&mystruct, "mystruct", 0));
myclass.members.push_back(Member(&myunion, "myunion1", 0)); myclass.members.push_back(Member(&myunion, "myunion1", 0));
mystruct.members.push_back(Member(&myunion, "myunion2", 0)); mystruct.members.push_back(Member(&myunion, "myunion2", 0));

View File

@ -9,7 +9,7 @@ using namespace type_graph;
TEST(TypeIdentifierTest, StubbedParam) { TEST(TypeIdentifierTest, StubbedParam) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myparam = Class{Class::Kind::Struct, "MyParam", 4}; auto myparam = Class{1, Class::Kind::Struct, "MyParam", 4};
myparam.members.push_back(Member{&myint, "a", 0}); myparam.members.push_back(Member{&myint, "a", 0});
auto container = getVector(); auto container = getVector();
@ -42,7 +42,7 @@ TEST(TypeIdentifierTest, StubbedParam) {
TEST(TypeIdentifierTest, Allocator) { TEST(TypeIdentifierTest, Allocator) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myalloc = Class{Class::Kind::Struct, "MyAlloc", 8}; auto myalloc = Class{1, Class::Kind::Struct, "MyAlloc", 8};
myalloc.templateParams.push_back(TemplateParam{&myint}); myalloc.templateParams.push_back(TemplateParam{&myint});
myalloc.functions.push_back(Function{"allocate"}); myalloc.functions.push_back(Function{"allocate"});
myalloc.functions.push_back(Function{"deallocate"}); myalloc.functions.push_back(Function{"deallocate"});
@ -80,7 +80,7 @@ TEST(TypeIdentifierTest, Allocator) {
TEST(TypeIdentifierTest, AllocatorSize1) { TEST(TypeIdentifierTest, AllocatorSize1) {
auto myint = Primitive{Primitive::Kind::Int32}; auto myint = Primitive{Primitive::Kind::Int32};
auto myalloc = Class{Class::Kind::Struct, "MyAlloc", 1}; auto myalloc = Class{1, Class::Kind::Struct, "MyAlloc", 1};
myalloc.templateParams.push_back(TemplateParam{&myint}); myalloc.templateParams.push_back(TemplateParam{&myint});
myalloc.functions.push_back(Function{"allocate"}); myalloc.functions.push_back(Function{"allocate"});
myalloc.functions.push_back(Function{"deallocate"}); myalloc.functions.push_back(Function{"deallocate"});

View File

@ -8,6 +8,7 @@
#include "oi/type_graph/TypeGraph.h" #include "oi/type_graph/TypeGraph.h"
using type_graph::Container; using type_graph::Container;
using type_graph::NodeId;
using type_graph::Pass; using type_graph::Pass;
using type_graph::Type; using type_graph::Type;
using type_graph::TypeGraph; using type_graph::TypeGraph;
@ -54,20 +55,20 @@ void test(type_graph::Pass pass,
test(pass, rootTypes, {}, expectedAfter); test(pass, rootTypes, {}, expectedAfter);
} }
Container getVector() { Container getVector(NodeId id) {
static ContainerInfo info{"std::vector", SEQ_TYPE, "vector"}; static ContainerInfo info{"std::vector", SEQ_TYPE, "vector"};
info.stubTemplateParams = {1}; info.stubTemplateParams = {1};
return Container{info, 24}; return Container{id, info, 24};
} }
Container getMap() { Container getMap(NodeId id) {
static ContainerInfo info{"std::map", STD_MAP_TYPE, "map"}; static ContainerInfo info{"std::map", STD_MAP_TYPE, "map"};
info.stubTemplateParams = {2, 3}; info.stubTemplateParams = {2, 3};
return Container{info, 48}; return Container{id, info, 48};
} }
Container getList() { Container getList(NodeId id) {
static ContainerInfo info{"std::list", LIST_TYPE, "list"}; static ContainerInfo info{"std::list", LIST_TYPE, "list"};
info.stubTemplateParams = {1}; info.stubTemplateParams = {1};
return Container{info, 24}; return Container{id, info, 24};
} }

View File

@ -4,10 +4,10 @@
#include <string_view> #include <string_view>
#include <vector> #include <vector>
#include "oi/type_graph/Types.h"
namespace type_graph { namespace type_graph {
class Container;
class Pass; class Pass;
class Type;
} // namespace type_graph } // namespace type_graph
void check(const std::vector<std::reference_wrapper<type_graph::Type>>& types, void check(const std::vector<std::reference_wrapper<type_graph::Type>>& types,
@ -23,6 +23,6 @@ void test(type_graph::Pass pass,
std::vector<std::reference_wrapper<type_graph::Type>> rootTypes, std::vector<std::reference_wrapper<type_graph::Type>> rootTypes,
std::string_view expectedAfter); std::string_view expectedAfter);
type_graph::Container getVector(); type_graph::Container getVector(type_graph::NodeId id = 0);
type_graph::Container getMap(); type_graph::Container getMap(type_graph::NodeId id = 0);
type_graph::Container getList(); type_graph::Container getList(type_graph::NodeId id = 0);