Repository navigation
Expand file tree
/
Copy pathType.cpp
More file actions
104 lines (87 loc) · 3.45 KB
/
Copy pathType.cpp
File metadata and controls
104 lines (87 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Type.h"
#include <algorithm>
static TypePtr voidType = std::make_shared<PrimitiveType>(TypeKind::Void);
static TypePtr charType = std::make_shared<PrimitiveType>(TypeKind::Char);
static TypePtr intType = std::make_shared<PrimitiveType>(TypeKind::Int);
TypePtr Type::getVoid() { return voidType; }
TypePtr Type::getChar() { return charType; }
TypePtr Type::getInt() { return intType; }
TypePtr Type::getPointer(TypePtr pointee) {
return std::make_shared<PointerType>(std::move(pointee));
}
TypePtr Type::getArray(TypePtr elementType, size_t numElements) {
return std::make_shared<ArrayType>(std::move(elementType), numElements);
}
TypePtr Type::getStruct(const std::string& name, const std::vector<StructMember>& members) {
return std::make_shared<StructType>(name, members);
}
TypePtr Type::getFunction(TypePtr returnType, const std::vector<TypePtr>& paramTypes) {
return std::make_shared<FunctionType>(std::move(returnType), paramTypes);
}
bool Type::equals(const Type& other) const {
return kind == other.kind;
}
bool PointerType::equals(const Type& other) const {
if (other.getKind() != TypeKind::Pointer) return false;
const auto& otherPtr = static_cast<const PointerType&>(other);
return pointee->equals(*otherPtr.pointee);
}
bool ArrayType::equals(const Type& other) const {
if (other.getKind() != TypeKind::Array) return false;
const auto& otherArr = static_cast<const ArrayType&>(other);
return numElements == otherArr.numElements && elementType->equals(*otherArr.elementType);
}
const StructMember* StructType::findMember(const std::string& memberName) const {
for (const auto& member : members) {
if (member.name == memberName) {
return &member;
}
}
return nullptr;
}
void StructType::setMembers(const std::vector<StructMember>& newMembers) {
members = newMembers;
size_t currentOffset = 0;
size_t maxAlign = 1;
for (auto& m : members) {
size_t align = m.type ? m.type->getAlignment() : 1;
if (align == 0) align = 1;
if (align > maxAlign) maxAlign = align;
// Pad to alignment boundary
if (currentOffset % align != 0) {
currentOffset += align - (currentOffset % align);
}
m.offset = currentOffset;
currentOffset += m.type ? m.type->getSize() : 0;
}
if (maxAlign > 0 && currentOffset % maxAlign != 0) {
currentOffset += maxAlign - (currentOffset % maxAlign);
}
structAlignment = maxAlign;
totalSize = currentOffset;
complete = true;
}
bool StructType::equals(const Type& other) const {
if (other.getKind() != TypeKind::Struct) return false;
const auto& otherStruct = static_cast<const StructType&>(other);
return name == otherStruct.name;
}
std::string FunctionType::toString() const {
std::string str = returnType->toString() + " (";
for (size_t i = 0; i < paramTypes.size(); ++i) {
if (i > 0) str += ", ";
str += paramTypes[i]->toString();
}
str += ")";
return str;
}
bool FunctionType::equals(const Type& other) const {
if (other.getKind() != TypeKind::Function) return false;
const auto& otherFn = static_cast<const FunctionType&>(other);
if (!returnType->equals(*otherFn.returnType)) return false;
if (paramTypes.size() != otherFn.paramTypes.size()) return false;
for (size_t i = 0; i < paramTypes.size(); ++i) {
if (!paramTypes[i]->equals(*otherFn.paramTypes[i])) return false;
}
return true;
}