static member variables retrieved using getters

This commit is contained in:
lemz1 2024-10-26 22:29:47 +02:00 committed by lemz
parent 299ccfe2bf
commit 546334a6cd

View file

@ -12,51 +12,32 @@ class PolymodMacro
{
Context.onAfterInitMacros(() -> {
var type = Context.getType('flixel.util.FlxColor');
var abst = switch (type)
switch (type)
{
case Type.TAbstract(t, _):
t.get();
buildAbstract(t.get());
default:
throw 'BRUH';
}
if (abst.impl == null)
{
return;
}
var cls = abst.impl.get();
var fields:Array<Field> = [];
for (field in cls.statics.get())
{
if (field.name == '_new')
{
continue;
}
var polymodField = createField(field);
if (polymodField == null)
{
continue;
}
fields.push(polymodField);
};
Context.defineType(
{
pos: Context.currentPos(),
pack: ['polymod', 'abstract'].concat(abst.pack),
name: abst.name,
kind: TypeDefKind.TDClass(null, [], false, false, false),
fields: fields,
}, null);
// var type = Context.getType('funkin.Paths.PathsFunction');
// switch (type)
// {
// case Type.TAbstract(t, _):
// buildAbstract(t.get());
// default:
// throw 'BRUH';
// }
});
}
public static macro function getAbstractAliases():ExprOf<Map<String, String>>
{
var abstractAliases:Map<String, String> = new Map<String, String>();
var abstractTypes = [Context.getType('flixel.util.FlxColor')];
var abstractTypes = [
Context.getType('flixel.util.FlxColor'),
// Context.getType('funkin.Paths.PathsFunction')
];
for (abstractType in abstractTypes)
{
var type = switch (abstractType)
@ -67,27 +48,93 @@ class PolymodMacro
throw 'BRUH';
}
abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstract.${type.pack.join('.')}.${type.name}');
abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstracts.${type.pack.join('.')}.${type.name}');
}
return macro $v{abstractAliases};
}
#if macro
static function createField(field:ClassField):Field
static var skipFields:Array<String> = [];
static function buildAbstract(abstractCls:AbstractType):Void
{
if (abstractCls.impl == null)
{
return;
}
skipFields = [];
var cls = abstractCls.impl.get();
// we use the functions to check whether we need to skip some fields
// that is why we sort the fields, so that functions are handled first
var sortedFields:Array<ClassField> = sortFields(cls.statics.get());
var fields:Array<Field> = [];
for (field in sortedFields)
{
if (field.name == '_new')
{
continue;
}
fields = fields.concat(createFields(abstractCls, field));
};
Context.defineType(
{
pos: Context.currentPos(),
pack: ['polymod', 'abstracts'].concat(abstractCls.pack),
name: abstractCls.name,
kind: TypeDefKind.TDClass(null, [], false, false, false),
fields: fields,
}, null);
}
static function sortFields(fields:Array<ClassField>):Array<ClassField>
{
var sortedFields:Array<ClassField> = [];
for (field in fields)
{
switch (field.type)
{
case Type.TLazy(f):
switch (f())
{
case Type.TFun(_, _):
sortedFields.insert(0, field);
default:
sortedFields.push(field);
}
case Type.TFun(_, _):
sortedFields.insert(0, field);
default:
sortedFields.push(field);
}
}
return sortedFields;
}
static function createFields(cls:AbstractType, field:ClassField):Array<Field>
{
if (skipFields.contains(field.name))
{
return [];
}
switch (field.type)
{
case Type.TLazy(f):
return _createField(field, f());
return _createFields(cls, field, f());
default:
return _createField(field, field.type);
return _createFields(cls, field, field.type);
}
}
static function _createField(field:ClassField, type:Type):Field
static function _createFields(cls:AbstractType, field:ClassField, type:Type):Array<Field>
{
var access = null;
var kind = null;
var fields = [];
switch (type)
{
@ -98,7 +145,12 @@ class PolymodMacro
{
if (arg.name == 'this')
{
return null;
var memberVariable:String = field.name.replace('get_', '').replace('set_', '');
if (memberVariable != field.name)
{
skipFields.push(memberVariable);
}
return [];
}
exprArgs.push(arg.name);
fieldArgs.push(
@ -118,38 +170,70 @@ class PolymodMacro
});
}
var strExpr = Context.parse('flixel.util.FlxColor.${field.name}(${exprArgs.join(', ')})', Context.currentPos());
var strExpr = Context.parse('${cls.pack.join('.')}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos());
access = [Access.AStatic].concat(getFieldAccess(field));
kind = FieldType.FFun(
fields.push(
{
args: fieldArgs,
ret: Context.toComplexType(ret),
expr: macro
{
return ${strExpr};
},
params: fieldParams
name: field.name,
doc: field.doc,
access: [Access.AStatic].concat(getFieldAccess(field)),
kind: FieldType.FFun(
{
args: fieldArgs,
ret: Context.toComplexType(ret),
expr: macro
{
return ${strExpr};
},
params: fieldParams
}),
pos: Context.currentPos()
});
case Type.TAbstract(t, params):
access = [Access.AStatic].concat(getFieldAccess(field));
kind = FieldType.FVar(Context.toComplexType(t.get().type), null);
fields.push(
{
name: field.name,
doc: field.doc,
access: [Access.AStatic].concat(getFieldAccess(field)),
kind: FieldType.FProp('get', 'never', Context.toComplexType(t.get().type), null),
pos: Context.currentPos()
});
var fieldParams = [];
for (param in field.params)
{
fieldParams.push(
{
name: param.name,
defaultType: Context.toComplexType(param.defaultType),
});
}
var strExpr = Context.parse('${cls.pack.join('.')}.${cls.name}.${field.name}', Context.currentPos());
fields.push(
{
name: 'get_${field.name}',
doc: field.doc,
access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate],
kind: FieldType.FFun(
{
args: [],
ret: Context.toComplexType(t.get().type),
expr: macro
{
return ${strExpr};
},
params: fieldParams
}),
pos: Context.currentPos()
});
default:
return null;
return [];
};
if (access == null || kind == null)
{
return null;
}
return {
name: field.name,
doc: field.doc,
access: access,
kind: kind,
pos: Context.currentPos()
};
return fields;
}
static function getFieldAccess(field:ClassField):Array<Access>