Search in sources :

Example 1 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter 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;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) Name(dyvil.lang.Name)

Example 2 with CodeParameter

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

the class ICall method toLambda.

default IValue toLambda(MarkerList markers, IContext context, int wildcards) {
    SourcePosition position = this.getPosition();
    final IParameter[] parameters = new IParameter[wildcards];
    for (int i = 0; i < wildcards; i++) {
        parameters[i] = new CodeParameter(null, position, Name.fromRaw("wildcard$" + i), Types.UNKNOWN, new AttributeList());
    }
    int parIndex = 0;
    final IValue receiver = this.getReceiver();
    if (receiver != null && receiver.isPartialWildcard()) {
        this.setReceiver(receiver.withLambdaParameter(parameters[parIndex++]));
    }
    final ArgumentList arguments = this.getArguments();
    for (int i = 0, size = arguments.size(); i < size; i++) {
        final IValue argument = arguments.get(i, null);
        if (argument.isPartialWildcard()) {
            arguments.set(i, null, argument.withLambdaParameter(parameters[parIndex++]));
        }
    }
    final LambdaExpr lambdaExpr = new LambdaExpr(position, parameters, wildcards);
    lambdaExpr.setImplicitParameters(true);
    lambdaExpr.setValue(this);
    return lambdaExpr.resolve(markers, context);
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) SourcePosition(dyvil.source.position.SourcePosition) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter)

Example 3 with CodeParameter

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

the class Template method makeMainMethod.

private void makeMainMethod() {
    // func main(args: [String]) -> void = new TemplateName().mainImpl(args)
    final ParameterList params = this.mainMethod.getParameters();
    final CodeParameter argsParam = new CodeParameter(this.mainMethod, null, Name.fromRaw("args"), new ArrayType(Types.STRING));
    params.add(argsParam);
    final IValue newTemplate = new ConstructorCall(null, this.templateClass.getClassType(), ArgumentList.EMPTY);
    this.mainMethod.setValue(new MethodCall(null, newTemplate, Name.fromRaw("mainImpl"), new ArgumentList(new FieldAccess(argsParam))));
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IValue(dyvilx.tools.compiler.ast.expression.IValue) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall)

Example 4 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter 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;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) TupleExpr(dyvilx.tools.compiler.ast.expression.TupleExpr) IType(dyvilx.tools.compiler.ast.type.IType)

Example 5 with CodeParameter

use of dyvilx.tools.compiler.ast.parameter.CodeParameter 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;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) ClassOperator(dyvilx.tools.compiler.ast.expression.ClassOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

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