symbol-processor/src/method.ts

71 lines
2.6 KiB
TypeScript

import { INDENT, formatType, getArgNames, prependArg } from './common';
export class Method {
readonly self: string;
readonly shortName: string;
readonly returnType: string;
readonly args: string;
readonly address: number;
readonly isInherited: boolean;
readonly hasVarargs: boolean;
readonly isStatic: boolean;
// Constructor
constructor(self: string, name: string, returnType: string, args: string, address: number, isInherited: boolean, isStatic: boolean) {
this.self = self;
this.shortName = name;
this.returnType = returnType;
this.args = args;
this.address = address;
this.isInherited = isInherited;
this.hasVarargs = this.args.includes('...');
this.isStatic = isStatic;
}
// Get Type
getNameWithCustomSelf(self: string) {
return `${self}_${this.shortName}`;
}
getName() {
return this.getNameWithCustomSelf(this.self);
}
getType() {
return `${this.getName()}_t`;
}
// Generate Type Definition
generateTypedef() {
let out = '';
// Normal Definition
const returnType = formatType(this.returnType);
out += `typedef ${returnType}(*${this.getType()})${this.args};\n`;
// Fancy Overwrite Does Not Support Varargs
if (!this.hasVarargs) {
// Add Overwrite Typedef
const type = this.getType();
const overwriteType = `__overwrite_${type}`;
out += `typedef ${returnType}(*${overwriteType})${prependArg(this.args, type + ' original')};\n`;
// Overwrite Helper
const helperName = '__create_overwrite_helper_for_' + this.getName();
out += `template <int>\n`;
out += `static ${type} ${helperName}(${overwriteType} method, ${type} original) {\n`;
out += `${INDENT}static ${overwriteType} stored_method = method;\n`;
out += `${INDENT}static ${type} stored_original = original;\n`;
out += `${INDENT}return []${this.args} { \\\n`;
out += `${INDENT}${INDENT}${returnType.trim() === 'void' ? '' : 'return '}stored_method(${['stored_original'].concat(getArgNames(this.args)).join(', ')}); \\\n`;
out += `${INDENT}};\n`;
out += `}\n`;
out += `#define ${helperName} ${helperName}<__COUNTER__>\n`;
}
// Return
return out;
}
// Generate Variable Definition
generateDefinition(nameSuffix?: string) {
return `${this.getType()} ${this.getName()}${nameSuffix !== undefined ? nameSuffix : ''}`;
}
}