Search in sources :

Example 26 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class ClassMetadata method createDefaultConstructor.

private CodeConstructor createDefaultConstructor() {
    // init(classParams...)
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = this.theClass.getConstructorAttributes();
    attributes.addFlag(Modifiers.GENERATED);
    final CodeConstructor constructor = new CodeConstructor(this.theClass, attributes);
    constructor.setPosition(position);
    this.copyClassParameters(constructor);
    // : super(superParams...)
    final IType superType = this.theClass.getSuperType();
    if (superType != null) {
        // Generate the constructor body
        ArgumentList arguments = this.theClass.getSuperConstructorArguments();
        if (arguments == null) {
            arguments = ArgumentList.empty();
        }
        final InitializerCall init = new InitializerCall(this.theClass.getPosition(), true, arguments, superType);
        constructor.setInitializer(init);
    }
    // { this.classParams... = classParams... }
    final ParameterList classParams = this.theClass.getParameters();
    final StatementList ctorBody = new StatementList();
    final ParameterList ctorParams = constructor.getParameters();
    // j is the counter for class parameters, as there may be leading synthetic constructor parameters
    for (int i = 0, j = 0, count = ctorParams.size(); i < count; i++) {
        final IParameter ctorParam = ctorParams.get(i);
        if (ctorParam.hasModifier(Modifiers.SYNTHETIC)) {
            continue;
        }
        final IParameter classParam = classParams.get(j++);
        if (classParam.hasModifier(Modifiers.OVERRIDE)) {
            continue;
        }
        final IValue receiver = new ThisExpr(this.theClass);
        final FieldAccess access = new FieldAccess(ctorParam);
        final FieldAssignment assignment = new FieldAssignment(position, receiver, classParam, access);
        ctorBody.add(assignment);
    }
    constructor.setValue(ctorBody);
    return constructor;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) FieldAssignment(dyvilx.tools.compiler.ast.expression.access.FieldAssignment) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall) IType(dyvilx.tools.compiler.ast.type.IType) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeConstructor(dyvilx.tools.compiler.ast.constructor.CodeConstructor) SourcePosition(dyvil.source.position.SourcePosition) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ThisExpr(dyvilx.tools.compiler.ast.expression.ThisExpr)

Example 27 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class Annotation method resolve.

@Override
public void resolve(MarkerList markers, IContext context) {
    this.arguments.resolve(markers, context);
    final IClass theClass;
    if (this.type == null || (theClass = this.type.getTheClass()) == null) {
        return;
    }
    final ParameterList parameterList = theClass.getParameters();
    for (int i = 0, count = parameterList.size(); i < count; i++) {
        final IParameter parameter = parameterList.get(i);
        final IType parameterType = parameter.getType();
        final IValue value = this.arguments.get(parameter);
        if (value == null) {
            if (parameter.getValue() == null) {
                markers.add(Markers.semanticError(this.getPosition(), "annotation.parameter.missing", this.type, parameter.getName()));
            }
            continue;
        }
        IValue typedValue = value.withType(parameterType, parameterType, markers, context);
        if (typedValue == null) {
            markers.add(TypeChecker.typeError(value, parameterType, parameterType, "annotation.parameter.type", parameter.getName()));
            continue;
        }
        typedValue = IValue.toAnnotationConstant(typedValue, markers, context);
        if (typedValue != value) {
            this.arguments.set(i, parameter.getLabel(), typedValue);
        }
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) IClass(dyvilx.tools.compiler.ast.classes.IClass) IType(dyvilx.tools.compiler.ast.type.IType)

Example 28 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class Annotation method write.

public void write(AnnotationVisitor writer) {
    final IClass iclass = this.type.getTheClass();
    final ParameterList parameterList = iclass.getParameters();
    for (int i = 0, count = parameterList.size(); i < count; i++) {
        final IParameter parameter = parameterList.get(i);
        final IValue argument = this.arguments.get(parameter);
        if (argument != null) {
            argument.writeAnnotationValue(writer, parameter.getName().qualified);
        }
    }
    writer.visitEnd();
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) IClass(dyvilx.tools.compiler.ast.classes.IClass)

Example 29 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class AbstractMethod method checkMutating.

private void checkMutating(MarkerList markers, IValue receiver) {
    final IType receiverType = receiver.getType();
    if (receiverType.getMutability() != Mutability.IMMUTABLE) {
        return;
    }
    final Annotation mutatingAnnotation = this.getAnnotation(Types.MUTATING_CLASS);
    if (mutatingAnnotation == null) {
        return;
    }
    final IValue value = mutatingAnnotation.getArguments().get(0, Names.value);
    final String stringValue = value != null ? value.stringValue() : Mutating.DEFAULT_MESSAGE;
    StringBuilder builder = new StringBuilder(stringValue);
    int index = builder.indexOf("{method}");
    if (index >= 0) {
        builder.replace(index, index + 8, this.name.unqualified);
    }
    index = builder.indexOf("{type}");
    if (index >= 0) {
        builder.replace(index, index + 6, receiverType.toString());
    }
    markers.add(new SemanticError(receiver.getPosition(), builder.toString()));
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) SemanticError(dyvilx.tools.parsing.marker.SemanticError) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) IType(dyvilx.tools.compiler.ast.type.IType)

Example 30 with IValue

use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.

the class ArgumentList method checkVarargsMatch.

protected static // 
int checkVarargsMatch(// 
int[] matchValues, // 
IType[] matchTypes, // 
int matchStartIndex, // 
IValue[] values, // 
int startIndex, // 
int endIndex, IParameter param, IImplicitContext implicitContext) {
    final IValue argument = values[startIndex];
    final IType paramType = param.getCovariantType();
    final int matchIndex = matchStartIndex + startIndex;
    if (argument.checkVarargs(false)) {
        return checkMatch_(matchValues, matchTypes, matchIndex, argument, paramType, implicitContext) ? 0 : MISMATCH;
    }
    if (startIndex == endIndex) {
        return 0;
    }
    final int count = endIndex - startIndex;
    final ArrayExpr arrayExpr = newArrayExpr(values, startIndex, count);
    if (!checkMatch_(matchValues, matchTypes, matchIndex, arrayExpr, paramType, implicitContext)) {
        return MISMATCH;
    }
    // We fill the remaining entries that are reserved for the (now wrapped) varargs values with the match value
    // of the array expression and the element type
    final int value = matchValues[matchIndex];
    final IType type = arrayExpr.getElementType();
    for (int i = 0; i < count; i++) {
        matchValues[matchIndex + i] = value;
        matchTypes[matchIndex + i] = type;
    }
    return count;
}
Also used : ArrayExpr(dyvilx.tools.compiler.ast.expression.ArrayExpr) IValue(dyvilx.tools.compiler.ast.expression.IValue) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

IValue (dyvilx.tools.compiler.ast.expression.IValue)79 IType (dyvilx.tools.compiler.ast.type.IType)20 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)17 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)11 SourcePosition (dyvil.source.position.SourcePosition)10 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)9 ArrayExpr (dyvilx.tools.compiler.ast.expression.ArrayExpr)7 Marker (dyvilx.tools.parsing.marker.Marker)7 IMethod (dyvilx.tools.compiler.ast.method.IMethod)6 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)5 IContext (dyvilx.tools.compiler.ast.context.IContext)5 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)4 IClass (dyvilx.tools.compiler.ast.classes.IClass)4 Variable (dyvilx.tools.compiler.ast.field.Variable)4 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)4 CombiningContext (dyvilx.tools.compiler.ast.context.CombiningContext)3 IVariable (dyvilx.tools.compiler.ast.field.IVariable)3 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)3