Search in sources :

Example 6 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter in project Dyvil by Dyvil.

the class CaseClassMetadata method createUnapplyAnyMethod.

private CodeMethod createUnapplyAnyMethod() {
    // static final func unapply<TypeParams...>(value: any) -> (T...)?
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = NullableType.apply(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, Types.NULLABLE_ANY);
    unapply.getParameters().add(parameter);
    // = (param is This) ? unapply(param as This) : null
    final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
    final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
    final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
    final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
    unapply.setValue(ifStatement);
    return unapply;
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) CastOperator(dyvilx.tools.compiler.ast.expression.CastOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall) IType(dyvilx.tools.compiler.ast.type.IType) InstanceOfOperator(dyvilx.tools.compiler.ast.expression.InstanceOfOperator) IfStatement(dyvilx.tools.compiler.ast.statement.IfStatement) IValue(dyvilx.tools.compiler.ast.expression.IValue) NullValue(dyvilx.tools.compiler.ast.expression.constant.NullValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

Example 7 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter in project Dyvil by Dyvil.

the class EnumClassMetadata method updateConstructor.

private void updateConstructor(IConstructor constructor) {
    constructor.getAttributes().addFlag(Modifiers.PRIVATE_PROTECTED);
    // Prepend parameters and set super initializer
    final CodeParameter nameParam = new CodeParameter(constructor, null, Names.name, new ImplicitNullableType(Types.STRING), AttributeList.of(Modifiers.SYNTHETIC));
    final CodeParameter ordParam = new CodeParameter(constructor, null, Names.ordinal, Types.INT, AttributeList.of(Modifiers.SYNTHETIC));
    constructor.getParameters().insert(0, nameParam);
    constructor.getParameters().insert(1, ordParam);
    final IConstructor enumConstructor = Types.ENUM_CLASS.getBody().getConstructor(0);
    final InitializerCall initializer = constructor.getInitializer();
    if (initializer == null) {
        final ArgumentList arguments = new ArgumentList(new FieldAccess(nameParam), new FieldAccess(ordParam));
        final SourcePosition position = constructor.position();
        final InitializerCall init = new InitializerCall(position, true, arguments, this.theClass.getSuperType(), enumConstructor);
        constructor.setInitializer(init);
        return;
    }
    // Prepend access to new parameters
    final ArgumentList arguments = initializer.getArguments();
    arguments.insert(0, new FieldAccess(nameParam));
    arguments.insert(1, new FieldAccess(ordParam));
}
Also used : IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) SourcePosition(dyvil.source.position.SourcePosition) ImplicitNullableType(dyvilx.tools.compiler.ast.type.compound.ImplicitNullableType) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter)

Example 8 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter in project Dyvil by Dyvil.

the class EnumClassMetadata method createFromOrdMethod.

private CodeMethod createFromOrdMethod() {
    // @BytecodeName("valueOf")
    // public static func from(ordinal: int) -> 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("ordinal"), Types.INT);
    method.getParameters().add(parameter);
    // = $VALUES[ordinal]
    final FieldAccess valuesField = new FieldAccess(position, null, Names.$VALUES);
    final SubscriptAccess getCall = new SubscriptAccess(position, valuesField, new ArgumentList(new FieldAccess(parameter)));
    method.setValue(getCall);
    return method;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Example 9 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter in project Dyvil by Dyvil.

the class Template method makeGenerateSpecMethod.

private void makeGenerateSpecMethod() {
    // func generate(spec: Specialization, writer: java.io.Writer) throws IOException -> void = { ... }
    final ParameterList params = this.genMethod.getParameters();
    final CodeParameter specParam = new CodeParameter(this.genMethod, null, Name.fromRaw("spec"), Template.LazyTypes.Specialization);
    final CodeParameter writerParam = new CodeParameter(this.genMethod, null, Name.fromRaw("writer"), Template.LazyTypes.Writer);
    params.add(specParam);
    params.add(writerParam);
    this.genMethod.getExceptions().add(Template.LazyTypes.IOException);
}
Also used : ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter)

Example 10 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter in project Dyvil by Dyvil.

the class CodeTypeParameter method computeReifiedKind.

@Override
protected void computeReifiedKind() {
    super.computeReifiedKind();
    final IType type;
    final Reified.Type reifiedKind = this.getReifiedKind();
    if (reifiedKind == Reified.Type.TYPE) {
        type = TypeOperator.LazyFields.TYPE;
    } else if (reifiedKind != null) {
        type = ClassOperator.LazyFields.CLASS;
    } else {
        return;
    }
    if (this.getReifiedKind() != null) {
        final AttributeList attributes = AttributeList.of(Modifiers.MANDATED | Modifiers.SYNTHETIC | Modifiers.FINAL);
        final Name name = Name.apply("reify_" + this.getName().qualified);
        final CodeParameter parameter = new CodeParameter(null, this.getPosition(), name, type, attributes);
        this.setReifyParameter(parameter);
    }
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) Reified(dyvil.annotation.Reified) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType) Name(dyvil.lang.Name)

Aggregations

CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)11 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)7 SourcePosition (dyvil.source.position.SourcePosition)6 IValue (dyvilx.tools.compiler.ast.expression.IValue)5 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)5 IType (dyvilx.tools.compiler.ast.type.IType)5 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)4 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)4 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)3 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)3 Name (dyvil.lang.Name)2 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)2 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)2 Reified (dyvil.annotation.Reified)1 IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)1 CastOperator (dyvilx.tools.compiler.ast.expression.CastOperator)1 ClassOperator (dyvilx.tools.compiler.ast.expression.ClassOperator)1 InstanceOfOperator (dyvilx.tools.compiler.ast.expression.InstanceOfOperator)1 TupleExpr (dyvilx.tools.compiler.ast.expression.TupleExpr)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1