use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.
the class Property method initSetter.
@Override
public IMethod initSetter() {
if (this.setter != null) {
return this.setter;
}
final Name name = Name.from(this.name.unqualified + "_=", this.name.qualified + "_$eq");
this.setter = new CodeMethod(this.enclosingClass, name, Types.VOID);
this.setter.setPosition(this.position);
this.setterParameter = new CodeParameter(this.setter, this.position, Names.newValue, this.type);
this.setter.getParameters().add(this.setterParameter);
return this.setter;
}
use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.
the class Property method initGetter.
@Override
public IMethod initGetter() {
if (this.getter != null) {
return this.getter;
}
final CodeMethod getter = new CodeMethod(this.enclosingClass, this.name, this.type);
getter.setPosition(this.position);
return this.getter = getter;
}
use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.
the class Template method parse.
@Override
public void parse() {
// class NAME { }
final CodeClass theClass = new CodeClass(null, this.name, AttributeList.of(Modifiers.PUBLIC));
final ClassBody classBody = new ClassBody(theClass);
theClass.setBody(classBody);
this.addClass(theClass);
this.templateClass = theClass;
// func generate(spec, writer) -> void = { ... }
final CodeMethod genMethod = new CodeMethod(theClass, Name.fromRaw("generate"), Types.VOID, AttributeList.of(Modifiers.PUBLIC | Modifiers.OVERRIDE));
final StatementList directives = new StatementList();
genMethod.setValue(directives);
classBody.addMethod(genMethod);
this.genMethod = genMethod;
// func main(args) -> void
final CodeMethod mainMethod = new CodeMethod(theClass, Name.fromRaw("main"), Types.VOID, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
classBody.addMethod(mainMethod);
this.mainMethod = mainMethod;
// Parse
new ParserManager(DyvilSymbols.INSTANCE, this.tokens.iterator(), this.markers).parse(new BlockParser(this, directives));
}
use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.
the class CaseClassMetadata method createUnapplyMethod.
private CodeMethod createUnapplyMethod() {
// static final func unapply<TypeParams...>(value: This) -> (T...)
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = this.getUnapplyReturnType();
final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
unapply.setPosition(position);
unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, this.theClass.getThisType());
unapply.getParameters().add(parameter);
// = (value.classParams...)
final TupleExpr tupleExpr = new TupleExpr(position);
final ArgumentList arguments = tupleExpr.getValues();
for (IParameter param : this.theClass.getParameters()) {
// value
final FieldAccess thisAccess = new FieldAccess(position, null, parameter);
// value.classParam
final IValue fieldAccess;
if (param.isOverride()) {
// if the class parameter is marked as 'override', we have to resolve it from a super-class
// the easiest way to do this is by name
fieldAccess = new FieldAccess(position, thisAccess, param.getName());
} else {
fieldAccess = new FieldAccess(position, thisAccess, param);
}
arguments.add(fieldAccess);
}
unapply.setValue(tupleExpr);
return unapply;
}
use of dyvilx.tools.compiler.ast.method.CodeMethod in project Dyvil by Dyvil.
the class EnumClassMetadata method createFromNameMethod.
private CodeMethod createFromNameMethod() {
// @BytecodeName("valueOf")
// public static func from(name: String) -> EnumType
final SourcePosition position = this.theClass.position();
final IType type = this.theClass.getClassType();
final CodeMethod method = new CodeMethod(this.theClass, Name.fromRaw("from"), type, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
method.setPosition(position);
method.setInternalName("valueOf");
final CodeParameter parameter = new CodeParameter(Name.fromRaw("name"), Types.STRING);
method.getParameters().add(parameter);
// = Enum.valueOf(class<EnumType>, name)
final MethodCall valueOfCall = new MethodCall(position, new ClassAccess(Types.ENUM), Name.fromRaw("valueOf"), new ArgumentList(new ClassOperator(this.theClass.getClassType()), new FieldAccess(parameter)));
method.setValue(valueOfCall);
return method;
}
Aggregations