symbol-processor/src/property.ts

142 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-01-04 20:27:02 +00:00
import { POINTER_SIZE } from './common';
import { getStructure } from './map';
export interface Property {
propertyOffset(): number;
propertySize(): number;
propertyType(): string;
propertyName(): string;
propertyAlignment(): number;
}
export class SimpleProperty implements Property {
readonly #offset: number;
readonly #type: string;
readonly #name: string;
// Constructor
constructor(offset: number, type: string, name: string) {
this.#offset = offset;
this.#type = type;
this.#name = name;
}
// Getters
propertyOffset() {
return this.#offset;
}
propertyType() {
return this.#type;
}
propertyName() {
return this.#name;
}
// Size
propertySize() {
if (this.#type.endsWith('*')) {
// Pointer
return POINTER_SIZE;
} else if (this.#type === 'int' || this.#type === 'uint') {
// Integer
return 4;
} else if (this.#type === 'float') {
// Float
return 4;
} else if (this.#type === 'bool') {
// Boolean
return 1;
} else if (this.#type === 'std::string') {
// C++ String
return 4;
} else if (this.#type === 'char' || this.#type === 'uchar') {
// Character
return 1;
2024-01-21 23:19:13 +00:00
} else if (this.#type === 'short' || this.#type === 'ushort') {
// Short
return 2;
2024-01-04 20:27:02 +00:00
} else if (this.#type.startsWith('std::vector<')) {
// C++ Vector
return 12;
} else {
// Structure
const structure = getStructure(this.#type);
2024-01-06 10:53:27 +00:00
return structure.getSize(true);
2024-01-04 20:27:02 +00:00
}
}
// Alignment
propertyAlignment() {
if (this.#type.endsWith('*')) {
// Pointer
return POINTER_SIZE;
} else if (this.#type === 'int' || this.#type === 'uint') {
// Integer
return 4;
} else if (this.#type === 'float') {
// Float
return 4;
} else if (this.#type === 'bool') {
// Boolean
return 1;
} else if (this.#type === 'std::string') {
// C++ String
return 4;
} else if (this.#type === 'char' || this.#type === 'uchar') {
// Character
return 1;
2024-01-21 23:19:13 +00:00
} else if (this.#type === 'short' || this.#type === 'ushort') {
// Short
return 1;
2024-01-04 20:27:02 +00:00
} else if (this.#type.startsWith('std::vector<')) {
// C++ Vector
return 4;
} else {
// Structure
const structure = getStructure(this.#type);
return structure.getAlignment();
}
}
}
export class StaticProperty {
readonly address: number;
readonly #type: string;
readonly #name: string;
readonly #self: string;
2024-01-07 07:41:08 +00:00
readonly #isArray: boolean;
2024-01-04 20:27:02 +00:00
// Constructor
2024-01-07 07:41:08 +00:00
constructor(address: number, type: string, name: string, self: string, isArray: boolean) {
2024-01-04 20:27:02 +00:00
this.address = address;
this.#type = type;
this.#name = name;
this.#self = self;
2024-01-07 07:41:08 +00:00
this.#isArray = isArray;
2024-01-04 20:27:02 +00:00
}
// Name And Type
getName() {
return `${this.#self}_${this.#name}`;
}
2024-01-07 07:41:08 +00:00
getPointerType() {
2024-01-04 20:27:02 +00:00
let type = this.#type;
2024-01-07 07:41:08 +00:00
// Convert Type To Pointer
2024-01-04 20:27:02 +00:00
if (!type.endsWith('*')) {
type += ' ';
}
type += '*';
// Return
return type;
}
// Generate Variable Definition
generateDefinition() {
2024-01-07 07:41:08 +00:00
return `${this.getPointerType()}${this.getName()}_pointer;\n`;
}
// Generate Macro
generateMacro() {
return `#define ${this.getName()} (${this.#isArray ? '' : '*'}${this.getName()}_pointer)\n`;
2024-01-04 20:27:02 +00:00
}
}