Search in sources :

Example 1 with IMethod

use of dyvilx.tools.compiler.ast.method.IMethod in project Dyvil by Dyvil.

the class CompleteCommand method findExtensions.

private static void findExtensions(IContext context, IType type, IValue value, Set<IMethod> methods, String start, boolean statics) {
    final MatchList<IMethod> matchList = new MatchList<>(context, true);
    type.getMethodMatches(matchList, value, null, null);
    context.getMethodMatches(matchList, value, null, null);
    Types.BASE_CONTEXT.getMethodMatches(matchList, value, null, null);
    for (Candidate<IMethod> candidate : matchList) {
        final IMethod member = candidate.getMember();
        if (member.hasModifier(Modifiers.INFIX) || member.getEnclosingClass() != type.getTheClass()) {
            checkMember(methods, member, start, true);
        }
    }
}
Also used : MatchList(dyvilx.tools.compiler.ast.method.MatchList) IMethod(dyvilx.tools.compiler.ast.method.IMethod)

Example 2 with IMethod

use of dyvilx.tools.compiler.ast.method.IMethod in project Dyvil by Dyvil.

the class MethodsCommand method execute.

@Override
public void execute(DyvilREPL repl, String args) {
    final List<IMethod> methodList = repl.getContext().getMethods();
    if (methodList.isEmpty()) {
        repl.getOutput().println(I18n.get("command.methods.none"));
        return;
    }
    final IMethod[] methods = methodList.toArray(IMethod.class);
    Arrays.sort(methods, MemberSorter.METHOD_COMPARATOR);
    for (IMethod method : methods) {
        repl.getOutput().println(Util.methodSignatureToString(method, null));
    }
}
Also used : IMethod(dyvilx.tools.compiler.ast.method.IMethod)

Example 3 with IMethod

use of dyvilx.tools.compiler.ast.method.IMethod in project Dyvil by Dyvil.

the class LambdaExpr method isType.

@Override
public boolean isType(IType type) {
    if (this.type != null && Types.isSuperType(type, this.type)) {
        return true;
    }
    final IClass interfaceClass = type.getTheClass();
    if (interfaceClass == null) {
        return false;
    }
    if (interfaceClass == Types.OBJECT_CLASS) {
        return true;
    }
    final IMethod method = interfaceClass.getFunctionalMethod();
    if (method == null) {
        return false;
    }
    final ParameterList methodParameters = method.getParameters();
    final int parameterCount = this.parameters.size();
    if (parameterCount != methodParameters.size()) {
        return false;
    }
    for (int i = 0; i < parameterCount; i++) {
        final IType lambdaParameterType = this.parameters.get(i).getCovariantType();
        if (lambdaParameterType.isUninferred()) {
            continue;
        }
        final IType methodParameterType = methodParameters.get(i).getCovariantType();
        if (!Types.isSuperType(methodParameterType, lambdaParameterType)) {
            return false;
        }
    }
    return true;
}
Also used : IClass(dyvilx.tools.compiler.ast.classes.IClass) IMethod(dyvilx.tools.compiler.ast.method.IMethod) IType(dyvilx.tools.compiler.ast.type.IType)

Example 4 with IMethod

use of dyvilx.tools.compiler.ast.method.IMethod in project Dyvil by Dyvil.

the class LambdaExpr method cleanup.

@Override
public IValue cleanup(ICompilableList compilableList, IClassCompilableList classCompilableList) {
    this.parameters.cleanup(compilableList, classCompilableList);
    if (this.returnType != null && (this.flags & EXPLICIT_RETURN) != 0) {
        this.returnType.cleanup(compilableList, classCompilableList);
    }
    this.value = this.value.cleanup(compilableList, classCompilableList);
    if (this.captureHelper == null || !this.captureHelper.hasCaptures()) {
        // Check if we can use a direct method reference
        if (this.value instanceof AbstractCall) {
            final AbstractCall call = (AbstractCall) this.value;
            final IMethod method = call.getMethod();
            if (method != null && this.checkCall(call.getReceiver(), call.getArguments(), method)) {
                this.setHandleType(ClassFormat.insnToHandle(method.getInvokeOpcode()));
                this.name = method.getInternalName();
                this.owner = method.getEnclosingClass().getInternalName();
                this.descriptor = method.getDescriptor();
                return this;
            }
        } else // To avoid trouble with anonymous classes
        if (this.value.getClass() == ConstructorCall.class) {
            final ConstructorCall call = (ConstructorCall) this.value;
            final IConstructor constructor = call.getConstructor();
            if (constructor != null && this.checkCall(null, call.getArguments(), constructor)) {
                this.setHandleType(ClassFormat.H_NEWINVOKESPECIAL);
                this.name = constructor.getInternalName();
                this.owner = constructor.getEnclosingClass().getInternalName();
                this.descriptor = constructor.getDescriptor();
                return this;
            }
        }
    }
    this.owner = classCompilableList.getInternalName();
    this.name = "lambda$" + classCompilableList.classCompilableCount();
    classCompilableList.addClassCompilable(this);
    return this;
}
Also used : IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) IMethod(dyvilx.tools.compiler.ast.method.IMethod) AbstractCall(dyvilx.tools.compiler.ast.expression.access.AbstractCall) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall)

Example 5 with IMethod

use of dyvilx.tools.compiler.ast.method.IMethod in project Dyvil by Dyvil.

the class Field method resolve.

@Override
public void resolve(MarkerList markers, IContext context) {
    super.resolve(markers, context);
    if (this.value != null) {
        final IContext context1 = new CombiningContext(this, context);
        this.value = this.value.resolve(markers, context1);
        boolean inferType = false;
        if (this.type == Types.UNKNOWN) {
            inferType = true;
            this.type = this.value.getType();
        }
        final TypeChecker.MarkerSupplier markerSupplier = TypeChecker.markerSupplier("field.type.incompatible", "field.type", "value.type", this.name);
        this.value = TypeChecker.convertValue(this.value, this.type, this.type, markers, context1, markerSupplier);
        if (inferType) {
            this.type = this.value.getType();
            if (this.type == Types.UNKNOWN && this.value.isResolved()) {
                markers.add(Markers.semantic(this.position, "field.type.infer", this.name.unqualified));
                this.type = Types.ANY;
            }
        }
    } else if (this.type == Types.UNKNOWN) {
        markers.add(Markers.semantic(this.position, "field.type.infer.novalue", this.name.unqualified));
        this.type = Types.ANY;
    }
    if (this.property == null) {
        return;
    }
    this.property.setType(this.type);
    final IMethod getter = this.property.getGetter();
    final IMethod setter = this.property.getSetter();
    final IValue receiver = this.hasModifier(Modifiers.STATIC) ? null : new ThisExpr(this.enclosingClass.getThisType(), VariableThis.DEFAULT);
    if (getter != null) {
        getter.setType(this.type);
        if (getter.getValue() == null) {
            // get: this.FIELD_NAME
            getter.setValue(new FieldAccess(getter.getPosition(), receiver, this));
        }
    }
    if (setter != null) {
        final IParameter setterParameter = setter.getParameters().get(0);
        setterParameter.setType(this.type);
        if (setter.getValue() == null) {
            final SourcePosition setterPosition = setter.getPosition();
            if (this.hasModifier(Modifiers.FINAL)) {
                markers.add(Markers.semanticError(setterPosition, "field.property.setter.final", this.name));
                // avoid abstract method error
                setter.setValue(new VoidValue(setterPosition));
            } else {
                // set: this.FIELD_NAME = newValue
                setter.setValue(new FieldAssignment(setterPosition, receiver, this, new FieldAccess(setterPosition, null, setterParameter)));
            }
        }
    }
    this.property.resolve(markers, context);
}
Also used : VoidValue(dyvilx.tools.compiler.ast.expression.constant.VoidValue) IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IContext(dyvilx.tools.compiler.ast.context.IContext) TypeChecker(dyvilx.tools.compiler.transform.TypeChecker) CombiningContext(dyvilx.tools.compiler.ast.context.CombiningContext) FieldAssignment(dyvilx.tools.compiler.ast.expression.access.FieldAssignment) IValue(dyvilx.tools.compiler.ast.expression.IValue) SourcePosition(dyvil.source.position.SourcePosition) IMethod(dyvilx.tools.compiler.ast.method.IMethod) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ThisExpr(dyvilx.tools.compiler.ast.expression.ThisExpr)

Aggregations

IMethod (dyvilx.tools.compiler.ast.method.IMethod)31 IValue (dyvilx.tools.compiler.ast.expression.IValue)6 IField (dyvilx.tools.compiler.ast.field.IField)4 IProperty (dyvilx.tools.compiler.ast.field.IProperty)4 IType (dyvilx.tools.compiler.ast.type.IType)3 TreeSet (dyvil.collection.mutable.TreeSet)2 Name (dyvil.lang.Name)2 SourcePosition (dyvil.source.position.SourcePosition)2 IContext (dyvilx.tools.compiler.ast.context.IContext)2 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)2 IImportContext (dyvilx.tools.compiler.ast.imports.IImportContext)2 ImportDeclaration (dyvilx.tools.compiler.ast.imports.ImportDeclaration)2 MatchList (dyvilx.tools.compiler.ast.method.MatchList)2 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)2 PrintStream (java.io.PrintStream)2 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 IClass (dyvilx.tools.compiler.ast.classes.IClass)1 IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)1 CombiningContext (dyvilx.tools.compiler.ast.context.CombiningContext)1 IImplicitContext (dyvilx.tools.compiler.ast.context.IImplicitContext)1