Search in sources :

Example 21 with TypedReference

use of org.eclipse.ceylon.model.typechecker.model.TypedReference in project ceylon by eclipse.

the class CompilerBoxingDeclarationVisitor method getRefinedDeclarationForWideningRules.

@Override
protected TypedDeclaration getRefinedDeclarationForWideningRules(TypedDeclaration typedDeclaration) {
    TypedReference typedReference = transformer.getTypedReference(typedDeclaration);
    TypedReference refinedDeclaration = transformer.getRefinedDeclaration(typedReference, typedReference.getQualifyingType());
    if (refinedDeclaration != null)
        return refinedDeclaration.getDeclaration();
    return null;
}
Also used : TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference)

Example 22 with TypedReference

use of org.eclipse.ceylon.model.typechecker.model.TypedReference in project ceylon by eclipse.

the class ExpressionVisitor method visitGenericBaseMemberReference.

private void visitGenericBaseMemberReference(Tree.StaticMemberOrTypeExpression that, TypedDeclaration member) {
    if (member instanceof Function && member.isParameterized()) {
        Function generic = (Function) member;
        Scope scope = that.getScope();
        Type outerType = scope.getDeclaringType(member);
        TypedReference target = member.appliedTypedReference(outerType, NO_TYPE_ARGS);
        that.setTarget(target);
        Type functionType = genericFunctionType(generic, scope, member, target, unit);
        that.setTypeModel(functionType);
        checkNotJvm(that, "type functions are not supported on the JVM: '" + member.getName(unit) + "' is generic (specify explicit type arguments)");
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) NativeUtil.declarationScope(org.eclipse.ceylon.compiler.typechecker.util.NativeUtil.declarationScope) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference)

Example 23 with TypedReference

use of org.eclipse.ceylon.model.typechecker.model.TypedReference in project ceylon by eclipse.

the class TypeArgumentInference method getInferredTypeArgsForFunctionRef.

/**
 * Infer the type arguments for a reference to a
 * generic function that occurs as an argument in
 * an invocation.
 * @param receiverType
 */
List<Type> getInferredTypeArgsForFunctionRef(Tree.StaticMemberOrTypeExpression smte, Type receiverType, boolean secondList) {
    Tree.TypeArguments typeArguments = smte.getTypeArguments();
    if (typeArguments instanceof Tree.InferredTypeArguments) {
        // the model object for the function ref
        Declaration reference = smte.getDeclaration();
        List<TypeParameter> typeParameters = getTypeParametersAccountingForTypeConstructor(reference);
        if (typeParameters == null || typeParameters.isEmpty()) {
            // nothing to infer
            return NO_TYPE_ARGS;
        } else {
            // set earlier in inferParameterTypes()
            TypedReference paramTypedRef = smte.getTargetParameter();
            Type paramType = smte.getParameterType();
            // the function ref is being passed
            if (paramType == null && paramTypedRef != null) {
                paramType = paramTypedRef.getFullType();
            }
            if (paramType == null) {
                return null;
            }
            if (isArgumentToGenericParameter(paramTypedRef, paramType)) {
                return null;
            }
            Declaration paramDec;
            Declaration parameterizedDec;
            if (paramTypedRef != null) {
                paramDec = paramTypedRef.getDeclaration();
                parameterizedDec = (Declaration) paramDec.getContainer();
            } else {
                paramDec = null;
                parameterizedDec = null;
            }
            // better algorithm
            if (!smte.getStaticMethodReferencePrimary() && reference instanceof Functional && paramDec instanceof Functional && paramTypedRef != null) {
                // callable parameter
                return inferFunctionRefTypeArgs(smte, receiverType, secondList, reference, typeParameters, paramTypedRef, paramDec, parameterizedDec);
            } else {
                // improve this, including in the spec)
                if (unit.isSequentialType(paramType)) {
                    paramType = unit.getSequentialElementType(paramType);
                }
                if (unit.isCallableType(paramType)) {
                    // the parameter has type Callable
                    return inferFunctionRefTypeArgs(smte, receiverType, secondList, reference, typeParameters, paramType, parameterizedDec);
                } else if (secondList) {
                    // arguments (NOT BLESSED BY SPEC!)
                    return inferNullaryFunctionCallTypeArgs(smte, receiverType, reference, typeParameters, paramType, parameterizedDec);
                } else {
                    return null;
                }
            }
        }
    } else {
        // not inferring
        return null;
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 24 with TypedReference

use of org.eclipse.ceylon.model.typechecker.model.TypedReference in project ceylon by eclipse.

the class ExpressionVisitor method inferParameterTypes.

/**
 * Infer parameter types for an anonymous function.
 */
private boolean inferParameterTypes(Reference pr, Parameter param, Tree.Expression e, boolean variadic, boolean error) {
    FunctionOrValue model = param.getModel();
    if (e != null && model != null) {
        Tree.Term term = unwrapExpressionUntilTerm(e.getTerm());
        TypedReference tpr = pr.getTypedParameter(param);
        if (term instanceof Tree.InvocationExpression) {
            Tree.InvocationExpression ie = (Tree.InvocationExpression) term;
            Tree.PositionalArgumentList pal = ie.getPositionalArgumentList();
            Tree.NamedArgumentList nal = ie.getNamedArgumentList();
            if (pal != null && pal.getPositionalArguments().isEmpty() || nal != null && nal.getNamedArguments().isEmpty()) {
                term = ie.getPrimary();
            }
        }
        if (term instanceof Tree.FunctionArgument) {
            Tree.FunctionArgument anon = (Tree.FunctionArgument) term;
            if (model instanceof Functional) {
                // and could be removed
                return inferParameterTypesFromCallableParameter(pr, param, anon, error);
            } else {
                Type paramType = tpr.getFullType();
                if (variadic) {
                    paramType = unit.getIteratedType(paramType);
                }
                paramType = callableFromUnion(paramType);
                if (unit.isCallableType(paramType)) {
                    return inferParameterTypesFromCallableType(paramType, param, anon, error);
                }
            }
        }
    }
    return true;
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 25 with TypedReference

use of org.eclipse.ceylon.model.typechecker.model.TypedReference in project ceylon by eclipse.

the class ExpressionVisitor method setupTargetParameters.

/**
 * Set the "target parameter" for a reference, which
 * will be used later for inferring type arguments for
 * a function ref or generic function ref.
 */
private void setupTargetParameters(Reference pr, Parameter param, Tree.Expression e) {
    if (e != null) {
        Tree.Term term = unwrapExpressionUntilTerm(e.getTerm());
        TypedReference tpr = pr.getTypedParameter(param);
        if (term instanceof Tree.InvocationExpression) {
            Tree.InvocationExpression ie = (Tree.InvocationExpression) term;
            Tree.PositionalArgumentList pal = ie.getPositionalArgumentList();
            Tree.NamedArgumentList nal = ie.getNamedArgumentList();
            if (pal != null && pal.getPositionalArguments().isEmpty() || nal != null && nal.getNamedArguments().isEmpty()) {
                term = ie.getPrimary();
            }
        }
        if (term instanceof Tree.StaticMemberOrTypeExpression) {
            // the "target parameter" is used later to
            // infer type arguments for function refs
            // and generic function refs
            Tree.StaticMemberOrTypeExpression stme = (Tree.StaticMemberOrTypeExpression) term;
            if (stme instanceof Tree.QualifiedMemberOrTypeExpression && stme.getStaticMethodReference()) {
                Tree.QualifiedMemberOrTypeExpression qmte = (Tree.QualifiedMemberOrTypeExpression) stme;
                Tree.StaticMemberOrTypeExpression ote = (Tree.StaticMemberOrTypeExpression) qmte.getPrimary();
                ote.setTargetParameter(tpr);
                stme.setParameterType(tpr.getFullType());
            } else {
                stme.setTargetParameter(tpr);
                stme.setParameterType(tpr.getFullType());
            }
        }
    }
}
Also used : TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

TypedReference (org.eclipse.ceylon.model.typechecker.model.TypedReference)40 Type (org.eclipse.ceylon.model.typechecker.model.Type)35 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)17 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)17 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)16 Function (org.eclipse.ceylon.model.typechecker.model.Function)15 Value (org.eclipse.ceylon.model.typechecker.model.Value)14 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)12 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)11 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)10 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)10 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)10 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)10 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)9 ModelUtil.genericFunctionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType)8 ModelUtil.unionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType)8 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)8 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)8 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)8 ArrayList (java.util.ArrayList)7