2024-08-02 00:56:22 -04:00
|
|
|
import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction, LEAN_HEADER_GUARD } from './common';
|
2024-01-04 15:27:02 -05:00
|
|
|
import { Method } from './method';
|
|
|
|
import { Property, StaticProperty } from './property';
|
|
|
|
import { VTable } from './vtable';
|
|
|
|
|
|
|
|
export class Struct {
|
2024-09-20 21:30:38 -04:00
|
|
|
readonly name: string;
|
2024-01-04 15:27:02 -05:00
|
|
|
#vtable: VTable | null;
|
|
|
|
readonly #methods: Method[];
|
|
|
|
readonly #properties: Property[];
|
2024-05-03 00:46:21 -04:00
|
|
|
#size: number | null;
|
2024-01-04 15:27:02 -05:00
|
|
|
readonly #dependencies: string[];
|
|
|
|
readonly #staticProperties: StaticProperty[];
|
2024-04-03 03:18:51 -04:00
|
|
|
#directParent: string | null;
|
2024-09-20 21:30:38 -04:00
|
|
|
#isSimple: boolean;
|
2024-01-04 15:27:02 -05:00
|
|
|
|
|
|
|
// Constructor
|
|
|
|
constructor(name: string) {
|
2024-09-20 21:30:38 -04:00
|
|
|
this.name = name;
|
2024-01-04 15:27:02 -05:00
|
|
|
this.#methods = [];
|
|
|
|
this.#properties = [];
|
|
|
|
this.#vtable = null;
|
2024-05-03 00:46:21 -04:00
|
|
|
this.#size = null;
|
2024-01-04 15:27:02 -05:00
|
|
|
this.#dependencies = [];
|
|
|
|
this.#staticProperties = [];
|
2024-04-03 03:18:51 -04:00
|
|
|
this.#directParent = null;
|
2024-09-20 21:30:38 -04:00
|
|
|
this.#isSimple = false;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dependencies
|
2024-01-04 17:00:19 -05:00
|
|
|
#addDependency(dependency: string) {
|
2024-01-04 15:27:02 -05:00
|
|
|
this.#dependencies.push(dependency);
|
|
|
|
}
|
|
|
|
getDependencies() {
|
|
|
|
return this.#dependencies;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure VTable Exists
|
2024-09-20 21:30:38 -04:00
|
|
|
getVTable() {
|
2024-01-04 15:27:02 -05:00
|
|
|
if (this.#vtable === null) {
|
2024-09-20 21:30:38 -04:00
|
|
|
this.#vtable = new VTable(this.name);
|
2024-05-03 00:46:21 -04:00
|
|
|
this.addProperty(this.#vtable.property);
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
2024-09-20 21:30:38 -04:00
|
|
|
return this.#vtable;
|
2024-04-03 03:18:51 -04:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:27:02 -05:00
|
|
|
// Setters
|
2024-05-03 00:46:21 -04:00
|
|
|
setSize(size: number) {
|
|
|
|
this.#size = size;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add Method
|
|
|
|
addMethod(method: Method, isVirtual: boolean) {
|
2024-09-20 21:30:38 -04:00
|
|
|
if (method.returnType !== this.name && method.returnType in STRUCTURE_FILES) {
|
2024-05-03 22:20:03 -04:00
|
|
|
this.#addDependency(method.returnType);
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
if (isVirtual) {
|
2024-09-20 21:30:38 -04:00
|
|
|
if (method.self !== this.name) {
|
2024-05-03 22:20:03 -04:00
|
|
|
throw new Error();
|
|
|
|
}
|
2024-09-20 21:30:38 -04:00
|
|
|
this.getVTable().add(method);
|
2024-01-04 15:27:02 -05:00
|
|
|
} else {
|
2024-05-03 22:20:03 -04:00
|
|
|
if (method.isInherited) {
|
|
|
|
this.#addDependency(method.self);
|
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
this.#methods.push(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add Property
|
|
|
|
addProperty(property: Property) {
|
|
|
|
this.#properties.push(property);
|
2024-01-04 17:00:19 -05:00
|
|
|
// Add Dependency If Needed
|
2024-05-03 00:46:21 -04:00
|
|
|
const type = property.rawType();
|
2024-01-04 17:00:19 -05:00
|
|
|
if (type in STRUCTURE_FILES) {
|
|
|
|
this.#addDependency(type);
|
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
// Add Static Property
|
|
|
|
addStaticProperty(property: StaticProperty) {
|
|
|
|
this.#staticProperties.push(property);
|
|
|
|
}
|
|
|
|
|
2024-09-20 21:30:38 -04:00
|
|
|
// "Simple" Structures
|
|
|
|
markAsSimple() {
|
|
|
|
this.#isSimple = true;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
2024-09-20 21:30:38 -04:00
|
|
|
#checkSimple() {
|
|
|
|
const checks = [
|
|
|
|
['Cannot Inherit', this.#directParent !== null],
|
|
|
|
['Must Have A Defined Size', this.#size === null],
|
|
|
|
['Cannot Have A VTable', this.#vtable !== null]
|
|
|
|
];
|
|
|
|
for (const check of checks) {
|
|
|
|
if (check[1]) {
|
|
|
|
throw new Error('Simple Structures ' + check[0]);
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
2024-05-03 00:46:21 -04:00
|
|
|
// Generate Properties
|
|
|
|
#generateProperties() {
|
2024-01-04 15:27:02 -05:00
|
|
|
// Sort Properties
|
2024-05-03 00:46:21 -04:00
|
|
|
const sortedProperties = [];
|
|
|
|
sortedProperties.push(...this.#properties);
|
|
|
|
sortedProperties.sort((a, b) => a.offset - b.offset);
|
2024-01-04 15:27:02 -05:00
|
|
|
|
2024-05-03 00:46:21 -04:00
|
|
|
// Fake Property To Pad Structure Size
|
|
|
|
let sizeProperty = null;
|
|
|
|
if (this.#size) {
|
|
|
|
sizeProperty = new Property(this.#size, '', '', '');
|
|
|
|
sortedProperties.push(sizeProperty);
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
2024-05-03 00:46:21 -04:00
|
|
|
// Add Properties
|
|
|
|
let out = '';
|
|
|
|
for (let i = 0; i < sortedProperties.length; i++) {
|
|
|
|
const property = sortedProperties[i]!;
|
|
|
|
const lastProperty = sortedProperties[i - 1];
|
|
|
|
|
|
|
|
// Padding
|
|
|
|
let offsetFromLastProperty = property.offset;
|
|
|
|
if (lastProperty) {
|
|
|
|
offsetFromLastProperty -= lastProperty.offset;
|
2024-01-15 19:01:59 -05:00
|
|
|
}
|
2024-05-03 00:46:21 -04:00
|
|
|
let paddingSize = toHex(offsetFromLastProperty);
|
|
|
|
if (lastProperty) {
|
|
|
|
paddingSize += ` - sizeof(${lastProperty.type()})`;
|
|
|
|
}
|
2024-09-20 21:30:38 -04:00
|
|
|
if (!this.#isSimple) {
|
|
|
|
out += `${INDENT}uchar ${INTERNAL}padding${i}[${paddingSize}];\n`;
|
|
|
|
}
|
2024-05-03 00:46:21 -04:00
|
|
|
|
|
|
|
// The Actual Property
|
|
|
|
if (property !== sizeProperty) {
|
|
|
|
out += `${INDENT}${property.type()} ${property.name()};\n`;
|
2024-01-15 19:01:59 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 00:46:21 -04:00
|
|
|
return out;
|
2024-01-15 19:01:59 -05:00
|
|
|
}
|
|
|
|
|
2024-05-03 22:20:03 -04:00
|
|
|
// Generate C++ Method Shortcuts
|
2024-07-14 05:03:18 -04:00
|
|
|
#generateMethod(method: Method, isVirtual: boolean) {
|
|
|
|
let out = '';
|
2024-07-14 05:04:32 -04:00
|
|
|
out += INDENT;
|
2024-07-14 05:03:18 -04:00
|
|
|
if (method.isStatic) {
|
|
|
|
out += 'static ';
|
|
|
|
}
|
2024-08-01 21:57:31 -04:00
|
|
|
out += `decltype(auto) ${method.shortName}(auto&&... args) {\n`;
|
|
|
|
out += `${INDENT}${INDENT}return `;
|
|
|
|
if (isVirtual) {
|
|
|
|
out += `this->vtable->${method.shortName}`;
|
|
|
|
} else {
|
|
|
|
out += `${method.getName()}->get(false)`;
|
|
|
|
}
|
|
|
|
out += '(';
|
|
|
|
if (!method.isStatic) {
|
|
|
|
out += `(${method.self} *) this, `;
|
|
|
|
}
|
|
|
|
out += 'std::forward<decltype(args)>(args)...);\n';
|
2024-07-14 05:03:18 -04:00
|
|
|
out += `${INDENT}}\n`;
|
|
|
|
return out;
|
|
|
|
}
|
2024-05-03 22:20:03 -04:00
|
|
|
#generateMethods() {
|
|
|
|
let out = '';
|
2024-08-02 00:56:22 -04:00
|
|
|
out += LEAN_HEADER_GUARD;
|
2024-05-03 22:20:03 -04:00
|
|
|
// Normal Methods
|
|
|
|
for (const method of this.#methods) {
|
2024-07-14 05:03:18 -04:00
|
|
|
out += this.#generateMethod(method, false);
|
2024-05-03 22:20:03 -04:00
|
|
|
}
|
|
|
|
// Virtual Methods
|
|
|
|
if (this.#vtable !== null) {
|
|
|
|
const virtualMethods = this.#vtable.getMethods();
|
|
|
|
for (const method of virtualMethods) {
|
2024-07-14 05:03:18 -04:00
|
|
|
if (method) {
|
|
|
|
out += this.#generateMethod(method, true);
|
2024-05-03 22:20:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-20 21:30:38 -04:00
|
|
|
// Allocation Method
|
|
|
|
if (this.#size !== null) {
|
|
|
|
// THIS DOES NOT CONSTRUCT THE OBJECT
|
|
|
|
out += `${INDENT}static ${this.name} *allocate() {\n`;
|
|
|
|
out += `${INDENT}${INDENT}return (${this.name} *) ::operator new(sizeof(${this.name}));\n`;
|
|
|
|
out += `${INDENT}}\n`;
|
|
|
|
}
|
2024-05-03 22:20:03 -04:00
|
|
|
// Return
|
2024-08-02 00:56:22 -04:00
|
|
|
out += '#endif\n';
|
2024-05-03 22:20:03 -04:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-01-04 15:27:02 -05:00
|
|
|
// Generate Header
|
|
|
|
generate() {
|
|
|
|
let out = '';
|
|
|
|
|
2024-09-20 21:30:38 -04:00
|
|
|
// Check "Simple" Status
|
|
|
|
if (this.#isSimple) {
|
|
|
|
this.#checkSimple();
|
|
|
|
}
|
|
|
|
|
2024-01-04 15:27:02 -05:00
|
|
|
// VTable
|
|
|
|
if (this.#vtable !== null) {
|
2024-05-18 18:58:18 -04:00
|
|
|
out += this.#vtable.generate();
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
2024-07-14 05:03:18 -04:00
|
|
|
// Static Properties
|
|
|
|
for (const property of this.#staticProperties) {
|
|
|
|
out += property.typedef();
|
|
|
|
}
|
|
|
|
|
2024-05-03 00:46:21 -04:00
|
|
|
// Property Typedefs
|
|
|
|
for (const property of this.#properties) {
|
|
|
|
out += property.typedef();
|
|
|
|
}
|
|
|
|
|
2024-07-14 05:03:18 -04:00
|
|
|
// Method Wrappers
|
2024-08-02 00:56:22 -04:00
|
|
|
let typedefs = '';
|
|
|
|
let methodsOut = '';
|
2024-07-14 05:03:18 -04:00
|
|
|
for (const method of this.#methods) {
|
|
|
|
if (!method.isInherited) {
|
2024-08-02 00:56:22 -04:00
|
|
|
typedefs += method.generateTypedefs();
|
|
|
|
methodsOut += method.generate(false, false);
|
2024-07-14 05:03:18 -04:00
|
|
|
}
|
|
|
|
}
|
2024-08-02 00:56:22 -04:00
|
|
|
out += typedefs;
|
|
|
|
out += LEAN_HEADER_GUARD;
|
|
|
|
out += methodsOut;
|
|
|
|
out += '#endif\n';
|
2024-07-14 05:03:18 -04:00
|
|
|
|
2024-01-04 15:27:02 -05:00
|
|
|
// Structure
|
2024-09-20 21:30:38 -04:00
|
|
|
out += `struct ${this.name} final {\n`;
|
2024-05-03 00:46:21 -04:00
|
|
|
out += this.#generateProperties();
|
2024-05-03 22:20:03 -04:00
|
|
|
out += this.#generateMethods();
|
2024-05-17 02:52:31 -04:00
|
|
|
for (const property of this.#staticProperties) {
|
|
|
|
// Static Property References
|
|
|
|
out += `${INDENT}static ${property.referenceDefinition(false)};\n`;
|
|
|
|
}
|
2024-09-20 21:30:38 -04:00
|
|
|
if (!this.#isSimple) {
|
|
|
|
// Disable Construction Of Complex Structures
|
|
|
|
out += preventConstruction(this.name);
|
2024-05-17 00:36:06 -04:00
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
out += `};\n`;
|
|
|
|
|
|
|
|
// Sanity Check Offsets
|
|
|
|
for (let i = 0; i < this.#properties.length; i++) {
|
|
|
|
const property = this.#properties[i]!;
|
2024-05-03 00:46:21 -04:00
|
|
|
const name = property.name();
|
|
|
|
const offset = property.offset;
|
2024-09-20 21:30:38 -04:00
|
|
|
out += `static_assert(offsetof(${this.name}, ${name}) == ${toHex(offset)}, "Invalid Offset");\n`;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity Check Size
|
2024-05-03 00:46:21 -04:00
|
|
|
if (this.#size !== null) {
|
2024-09-20 21:30:38 -04:00
|
|
|
out += assertSize(this.name, this.#size);
|
2024-05-03 00:46:21 -04:00
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
|
|
|
|
// Return
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate Compiled Code
|
|
|
|
generateCode() {
|
2024-05-17 02:52:31 -04:00
|
|
|
let out = '';
|
2024-01-04 15:27:02 -05:00
|
|
|
|
|
|
|
// Static Properties
|
|
|
|
for (const property of this.#staticProperties) {
|
2024-05-17 02:52:31 -04:00
|
|
|
out += `${property.referenceDefinition(true)} = *(${property.type()} *) ${toHex(property.offset)};\n`;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
for (const method of this.#methods) {
|
2024-05-03 22:20:03 -04:00
|
|
|
if (!method.isInherited) {
|
2024-07-14 05:03:18 -04:00
|
|
|
out += method.generate(true, false);
|
2024-05-03 22:20:03 -04:00
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// VTable
|
|
|
|
if (this.#vtable !== null) {
|
2024-05-18 18:58:18 -04:00
|
|
|
const vtable = this.#vtable.generateCode(this.#directParent);
|
2024-05-17 02:52:31 -04:00
|
|
|
out += vtable;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return
|
2024-05-17 02:52:31 -04:00
|
|
|
return out;
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|
2024-04-03 03:18:51 -04:00
|
|
|
|
|
|
|
// Set Direct Parent (Used For "New Method" Testing)
|
|
|
|
setDirectParent(directParent: string) {
|
|
|
|
this.#directParent = directParent;
|
|
|
|
}
|
2024-01-04 15:27:02 -05:00
|
|
|
}
|