Search in sources :

Example 11 with FunctionOrValue

use of com.redhat.ceylon.model.typechecker.model.FunctionOrValue in project ceylon-compiler by ceylon.

the class MethodDefinitionBuilder method parameter.

public MethodDefinitionBuilder parameter(Parameter param, TypedReference typedRef, List<JCAnnotation> userAnnotations, int flags, WideningRules wideningRules) {
    String paramName = param.getName();
    String aliasedName = Naming.getAliasedParameterName(param);
    FunctionOrValue mov = CodegenUtil.findMethodOrValueForParam(param);
    if (typedRef == null)
        typedRef = gen.getTypedReference(mov);
    int mods = 0;
    if (!Decl.isNonTransientValue(mov) || !mov.isVariable() || mov.isCaptured()) {
        mods |= FINAL;
    }
    NonWideningParam nonWideningParam = getNonWideningParam(typedRef, wideningRules);
    flags |= nonWideningParam.flags;
    return parameter(mods, param.getModel().getAnnotations(), userAnnotations, paramName, aliasedName, param, nonWideningParam.nonWideningDecl, nonWideningParam.nonWideningType, flags);
}
Also used : FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 12 with FunctionOrValue

use of com.redhat.ceylon.model.typechecker.model.FunctionOrValue in project ceylon-compiler by ceylon.

the class ModelLoaderTests method functionalParameterParameterNames.

@Test
public void functionalParameterParameterNames() {
    compile("FunctionalParameterParameterNames.ceylon");
    try {
        verifyCompilerClassLoading("functionalparameterparameternamestest.ceylon", new RunnableTest() {

            @Override
            public void test(ModelLoader loader) {
                Module mod = loader.getLoadedModule(moduleForJavaModelLoading(), moduleVersionForJavaModelLoading());
                Assert.assertNotNull(mod);
                Package p = mod.getDirectPackage(packageForJavaModelLoading());
                Assert.assertNotNull(p);
                Declaration fpClass = p.getDirectMember("FunctionalParameterParameterNames", Collections.<Type>emptyList(), false);
                Assert.assertNotNull(fpClass);
                {
                    // functionalParameter
                    Function fp = (Function) fpClass.getDirectMember("functionalParameter", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(String))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertTrue(paramF.isDeclaredAnything());
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertTrue(modelF.isDeclaredVoid());
                    Assert.assertEquals("Anything(String)", typeName(modelF));
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String", typeName(modelS));
                }
                {
                    // callableValueParameter
                    Function fp = (Function) fpClass.getDirectMember("callableValueParameter", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(String))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertFalse(paramF.isDeclaredAnything());
                    Assert.assertTrue(paramF.getModel() instanceof Value);
                    Value modelF = (Value) paramF.getModel();
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals("Anything(String)", typeName(modelF));
                }
                {
                    // functionalParameterNested
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterNested", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(Anything(String)))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.isDeclaredAnything());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertEquals("Anything(Anything(String))", typeName(modelF));
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertTrue(modelF.isDeclaredVoid());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramF2 = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertTrue(paramF2.isDeclaredAnything());
                    Assert.assertEquals("f2", paramF2.getName());
                    Assert.assertTrue(paramF2.getModel() instanceof Function);
                    Function modelF2 = (Function) paramF2.getModel();
                    Assert.assertEquals("Anything(String)", typeName(modelF2));
                    Assert.assertEquals("f2", modelF2.getName());
                    Assert.assertTrue(modelF2.isDeclaredVoid());
                    Assert.assertEquals(1, modelF2.getParameterLists().size());
                    Assert.assertEquals(1, modelF2.getParameterLists().get(0).getParameters().size());
                    Parameter paramS = modelF2.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String", typeName(modelS));
                }
                {
                    // functionalParameterNested2
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterNested2", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(Anything(String, Anything(Boolean, Integer))))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals("Anything(Anything(String, Anything(Boolean, Integer)))", typeName(modelF));
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramF2 = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f2", paramF2.getName());
                    Assert.assertTrue(paramF2.getModel() instanceof Function);
                    Function modelF2 = (Function) paramF2.getModel();
                    Assert.assertEquals("Anything(String, Anything(Boolean, Integer))", typeName(modelF2));
                    Assert.assertEquals("f2", modelF2.getName());
                    Assert.assertEquals(1, modelF2.getParameterLists().size());
                    Assert.assertEquals(2, modelF2.getParameterLists().get(0).getParameters().size());
                    Parameter paramS = modelF2.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("String", typeName(modelS));
                    Assert.assertEquals("s", modelS.getName());
                    Parameter paramF3 = modelF2.getParameterLists().get(0).getParameters().get(1);
                    Assert.assertEquals("f3", paramF3.getName());
                    Assert.assertTrue(paramF3.getModel() instanceof Function);
                    Function modelF3 = (Function) paramF3.getModel();
                    Assert.assertEquals("Anything(Boolean, Integer)", typeName(modelF3));
                    Assert.assertEquals("f3", modelF3.getName());
                    Assert.assertEquals(1, modelF3.getParameterLists().size());
                    Assert.assertEquals(2, modelF3.getParameterLists().get(0).getParameters().size());
                    Parameter paramB1 = modelF3.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("b1", paramB1.getName());
                    Assert.assertTrue(paramB1.getModel() instanceof Value);
                    Value modelB1 = (Value) paramB1.getModel();
                    Assert.assertEquals("Boolean", typeName(modelB1));
                    Assert.assertEquals("b1", modelB1.getName());
                    Parameter paramI2 = modelF3.getParameterLists().get(0).getParameters().get(1);
                    Assert.assertEquals("i2", paramI2.getName());
                    Assert.assertTrue(paramI2.getModel() instanceof Value);
                    Value modelI2 = (Value) paramI2.getModel();
                    Assert.assertEquals("i2", modelI2.getName());
                    Assert.assertEquals("Integer", typeName(modelI2));
                }
                {
                    // functionalParameterMpl
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterMpl", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(Integer)(String))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertTrue(paramF.isDeclaredAnything());
                    Assert.assertEquals("mpl", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertTrue(modelF.isDeclaredVoid());
                    Assert.assertEquals("Anything(Integer)(String)", typeName(modelF));
                    Assert.assertEquals("mpl", modelF.getName());
                    Assert.assertEquals(2, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(1).getParameters().size());
                    Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String", typeName(modelS));
                    Parameter paramS2 = modelF.getParameterLists().get(1).getParameters().get(0);
                    Assert.assertEquals("i2", paramS2.getName());
                    Assert.assertTrue(paramS2.getModel() instanceof Value);
                    Value modelS2 = (Value) paramS2.getModel();
                    Assert.assertEquals("i2", modelS2.getName());
                    Assert.assertEquals("Integer", typeName(modelS2));
                }
                {
                    // functionalParameterMpl2
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterMpl2", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Parameter paramMpl = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("mpl", paramMpl.getName());
                    Assert.assertTrue(paramMpl.getModel() instanceof Function);
                    Function modelMpl = (Function) paramMpl.getModel();
                    Assert.assertEquals("mpl", modelMpl.getName());
                    Assert.assertEquals(2, modelMpl.getParameterLists().size());
                    Assert.assertEquals(1, modelMpl.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals(1, modelMpl.getParameterLists().get(1).getParameters().size());
                    Parameter paramS = modelMpl.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String", typeName(modelS));
                    Parameter paramF = modelMpl.getParameterLists().get(1).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(2, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramB1 = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("b1", paramB1.getName());
                    Assert.assertTrue(paramB1.getModel() instanceof Value);
                    Value modelB1 = (Value) paramB1.getModel();
                    Assert.assertEquals("b1", modelB1.getName());
                    Assert.assertEquals("Boolean", typeName(modelB1));
                    Parameter paramI2 = modelF.getParameterLists().get(0).getParameters().get(1);
                    Assert.assertEquals("i2", paramI2.getName());
                    Assert.assertTrue(paramI2.getModel() instanceof Value);
                    Value modelI2 = (Value) paramI2.getModel();
                    Assert.assertEquals("i2", modelI2.getName());
                    Assert.assertEquals("Integer", typeName(modelI2));
                }
                {
                    // functionalParameterMpl3
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterMpl3", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Parameter paramMpl = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("mpl", paramMpl.getName());
                    Assert.assertTrue(paramMpl.getModel() instanceof Function);
                    Function modelMpl = (Function) paramMpl.getModel();
                    Assert.assertEquals("mpl", modelMpl.getName());
                    Assert.assertEquals(2, modelMpl.getParameterLists().size());
                    Assert.assertEquals(1, modelMpl.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals(1, modelMpl.getParameterLists().get(1).getParameters().size());
                    Parameter paramF = modelMpl.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(2, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramB1 = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("b1", paramB1.getName());
                    Assert.assertTrue(paramB1.getModel() instanceof Value);
                    Value modelB1 = (Value) paramB1.getModel();
                    Assert.assertEquals("b1", modelB1.getName());
                    Assert.assertEquals("Boolean", typeName(modelB1));
                    Parameter paramI2 = modelF.getParameterLists().get(0).getParameters().get(1);
                    Assert.assertEquals("i2", paramI2.getName());
                    Assert.assertTrue(paramI2.getModel() instanceof Value);
                    Value modelI2 = (Value) paramI2.getModel();
                    Assert.assertEquals("i2", modelI2.getName());
                    Assert.assertEquals("Integer", typeName(modelI2));
                    Parameter paramS = modelMpl.getParameterLists().get(1).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String", typeName(modelS));
                }
                {
                    // functionalParameterReturningCallable
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterReturningCallable", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals("Anything()", modelF.getType().asString());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String", modelS.getType().asString());
                }
                {
                    // functionalParameterReturningCallable
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterTakingCallable", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals("Anything", modelF.getType().asString());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramF2 = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("f2", paramF2.getName());
                    Assert.assertTrue(paramF2.getModel() instanceof Value);
                    Value modelF2 = (Value) paramF2.getModel();
                    Assert.assertEquals("f2", modelF2.getName());
                    Assert.assertEquals("Anything(String)", modelF2.getType().asString());
                }
                {
                    // functionalParameterVariadicStar
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterVariadicStar", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(String*))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertTrue(paramF.isDeclaredAnything());
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertTrue(modelF.isDeclaredVoid());
                    Assert.assertEquals("Anything(String*)", typeName(modelF));
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.isSequenced());
                    Assert.assertFalse(paramS.isAtLeastOne());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("String[]", typeName(modelS));
                }
                {
                    // functionalParameterVariadicPlus
                    Function fp = (Function) fpClass.getDirectMember("functionalParameterVariadicPlus", null, false);
                    Assert.assertNotNull(fp);
                    Assert.assertEquals(1, fp.getParameterLists().size());
                    Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
                    Assert.assertEquals("Anything(Anything(String+))", typeName(fp));
                    Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertTrue(paramF.isDeclaredAnything());
                    Assert.assertEquals("f", paramF.getName());
                    Assert.assertTrue(paramF.getModel() instanceof Function);
                    Function modelF = (Function) paramF.getModel();
                    Assert.assertTrue(modelF.isDeclaredVoid());
                    Assert.assertEquals("Anything(String+)", typeName(modelF));
                    Assert.assertEquals("f", modelF.getName());
                    Assert.assertEquals(1, modelF.getParameterLists().size());
                    Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
                    Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
                    Assert.assertEquals("s", paramS.getName());
                    Assert.assertTrue(paramS.isSequenced());
                    Assert.assertTrue(paramS.isAtLeastOne());
                    Assert.assertTrue(paramS.getModel() instanceof Value);
                    Value modelS = (Value) paramS.getModel();
                    Assert.assertEquals("s", modelS.getName());
                    Assert.assertEquals("[String+]", typeName(modelS));
                }
            }

            private String typeName(FunctionOrValue fp) {
                if (fp instanceof Function) {
                    return fp.appliedTypedReference(null, Collections.<Type>emptyList()).getFullType().asString();
                } else if (fp instanceof Value) {
                    return fp.getType().asString();
                }
                return null;
            }
        });
    } catch (RuntimeException e) {
        if (e.getCause() instanceof org.junit.ComparisonFailure) {
            throw (org.junit.ComparisonFailure) e.getCause();
        } else if (e.getCause() instanceof java.lang.AssertionError) {
            throw (java.lang.AssertionError) e.getCause();
        }
        throw e;
    }
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) AbstractModelLoader(com.redhat.ceylon.model.loader.AbstractModelLoader) ModelLoader(com.redhat.ceylon.model.loader.ModelLoader) RuntimeModelLoader(com.redhat.ceylon.compiler.java.runtime.model.RuntimeModelLoader) CeylonModelLoader(com.redhat.ceylon.compiler.java.loader.CeylonModelLoader) DeclarationType(com.redhat.ceylon.model.loader.ModelLoader.DeclarationType) Type(com.redhat.ceylon.model.typechecker.model.Type) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Package(com.redhat.ceylon.model.typechecker.model.Package) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Module(com.redhat.ceylon.model.typechecker.model.Module) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Test(org.junit.Test)

Example 13 with FunctionOrValue

use of com.redhat.ceylon.model.typechecker.model.FunctionOrValue in project ceylon-compiler by ceylon.

the class CallableBuilder method getParameterTypesFromParameterModels.

private java.util.List<Type> getParameterTypesFromParameterModels() {
    java.util.List<Type> parameterTypes = new ArrayList<Type>(numParams);
    // get them from our declaration
    for (Parameter p : paramLists.getParameters()) {
        Type pt;
        FunctionOrValue pm = p.getModel();
        if (pm instanceof Function && ((Function) pm).isParameter())
            pt = gen.getTypeForFunctionalParameter((Function) pm);
        else
            pt = p.getType();
        parameterTypes.add(pt);
    }
    return parameterTypes;
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) ArrayList(java.util.ArrayList) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 14 with FunctionOrValue

use of com.redhat.ceylon.model.typechecker.model.FunctionOrValue in project ceylon-compiler by ceylon.

the class BoxingDeclarationVisitor method setBoxingState.

private void setBoxingState(TypedDeclaration declaration, TypedDeclaration refinedDeclaration, Node that) {
    Type type = declaration.getType();
    if (type == null) {
        // an error must have already been reported
        return;
    }
    // fetch the real refined declaration if required
    if (Decl.equal(declaration, refinedDeclaration) && declaration instanceof FunctionOrValue && ((FunctionOrValue) declaration).isParameter() && declaration.getContainer() instanceof Class) {
        // maybe it is really inherited from a field?
        FunctionOrValue methodOrValueForParam = (FunctionOrValue) declaration;
        if (methodOrValueForParam != null) {
            // make sure we get the refined version of that member
            refinedDeclaration = (TypedDeclaration) methodOrValueForParam.getRefinedDeclaration();
        }
    }
    // inherit underlying type constraints
    if (!Decl.equal(refinedDeclaration, declaration) && type.getUnderlyingType() == null && refinedDeclaration.getType() != null)
        type.setUnderlyingType(refinedDeclaration.getType().getUnderlyingType());
    // abort if our boxing state has already been set
    if (declaration.getUnboxed() != null)
        return;
    // functional parameter return values are always boxed if we're not creating a method for them
    if (declaration instanceof Function && ((Function) declaration).isParameter() && !JvmBackendUtil.createMethod((Function) declaration)) {
        declaration.setUnboxed(false);
        return;
    }
    if (!Decl.equal(refinedDeclaration, declaration)) {
        // make sure refined declarations have already been set
        if (refinedDeclaration.getUnboxed() == null)
            setBoxingState(refinedDeclaration, refinedDeclaration, that);
        // inherit
        declaration.setUnboxed(refinedDeclaration.getUnboxed());
    } else if (declaration instanceof Function && CodegenUtil.isVoid(declaration.getType()) && Strategy.useBoxedVoid((Function) declaration) && !(refinedDeclaration.getTypeDeclaration() instanceof TypeParameter) && !CodegenUtil.isContainerFunctionalParameter(refinedDeclaration) && !(refinedDeclaration instanceof Functional && Decl.isMpl((Functional) refinedDeclaration))) {
        declaration.setUnboxed(false);
    } else if ((isCeylonBasicType(type) || Decl.isUnboxedVoid(declaration)) && !(refinedDeclaration.getTypeDeclaration() instanceof TypeParameter) && (refinedDeclaration.getContainer() instanceof Declaration == false || !CodegenUtil.isContainerFunctionalParameter(refinedDeclaration)) && !(refinedDeclaration instanceof Functional && Decl.isMpl((Functional) refinedDeclaration))) {
        boolean unbox = !forceBoxedLocals || !(declaration instanceof Value) || !Decl.isLocal(declaration) || Decl.isParameter(declaration) || Decl.isTransient(declaration);
        declaration.setUnboxed(unbox);
    } else if (Decl.isValueParameter(declaration) && CodegenUtil.isContainerFunctionalParameter(declaration) && JvmBackendUtil.createMethod((FunctionOrValue) declaration.getContainer())) {
        Function functionalParameter = (Function) declaration.getContainer();
        TypedDeclaration refinedFrom = (TypedDeclaration) CodegenUtil.getTopmostRefinedDeclaration(functionalParameter, optimisedMethodSpecifiersToMethods);
        if (Decl.equal(refinedFrom, functionalParameter)) {
            // not a method return type (where void would be considered unboxed).
            if (declaration.getUnit().getAnythingType().isExactly(declaration.getType()) || declaration.getUnit().isOptionalType(declaration.getType())) {
                declaration.setUnboxed(false);
            } else {
                declaration.setUnboxed(true);
            }
        } else {
            // make sure refined declarations have already been set
            if (refinedFrom.getUnboxed() == null)
                setBoxingState(refinedFrom, refinedFrom, that);
            // inherit
            declaration.setUnboxed(refinedFrom.getUnboxed());
        }
    } else {
        declaration.setUnboxed(false);
    }
    // Any "@boxed" or "@unboxed" compiler annotation overrides
    boxFromAnnotation(declaration, that);
}
Also used : Functional(com.redhat.ceylon.model.typechecker.model.Functional) Function(com.redhat.ceylon.model.typechecker.model.Function) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Type(com.redhat.ceylon.model.typechecker.model.Type) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) FunctionalParameterDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.FunctionalParameterDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 15 with FunctionOrValue

use of com.redhat.ceylon.model.typechecker.model.FunctionOrValue in project ceylon-compiler by ceylon.

the class ClassTransformer method refineClass.

private Class refineClass(Scope container, Reference pr, ClassOrInterface classModel, Class formalClass, Unit unit) {
    Class refined = new Class();
    refined.setActual(true);
    refined.setShared(formalClass.isShared());
    refined.setContainer(container);
    refined.setExtendedType(formalClass.getType());
    refined.setDeprecated(formalClass.isDeprecated());
    refined.setName(formalClass.getName());
    refined.setRefinedDeclaration(formalClass.getRefinedDeclaration());
    refined.setScope(container);
    //refined.setType(pr.getType());
    refined.setUnit(unit);
    for (ParameterList formalPl : formalClass.getParameterLists()) {
        ParameterList refinedPl = new ParameterList();
        for (Parameter formalP : formalPl.getParameters()) {
            Parameter refinedP = new Parameter();
            refinedP.setAtLeastOne(formalP.isAtLeastOne());
            refinedP.setDeclaration(refined);
            refinedP.setDefaulted(formalP.isDefaulted());
            refinedP.setDeclaredAnything(formalP.isDeclaredAnything());
            refinedP.setHidden(formalP.isHidden());
            refinedP.setSequenced(formalP.isSequenced());
            refinedP.setName(formalP.getName());
            final TypedReference typedParameter = pr.getTypedParameter(formalP);
            FunctionOrValue paramModel;
            if (formalP.getModel() instanceof Value) {
                Value paramValueModel = refineValue((Value) formalP.getModel(), typedParameter, refined, classModel.getUnit());
                paramValueModel.setInitializerParameter(refinedP);
                paramModel = paramValueModel;
            } else {
                Function paramFunctionModel = refineMethod(refined, typedParameter, classModel, (Function) formalP.getModel(), unit);
                paramFunctionModel.setInitializerParameter(refinedP);
                paramModel = paramFunctionModel;
            }
            refinedP.setModel(paramModel);
            refinedPl.getParameters().add(refinedP);
        }
        refined.addParameterList(refinedPl);
    }
    return refined;
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Aggregations

FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)18 Value (com.redhat.ceylon.model.typechecker.model.Value)10 Function (com.redhat.ceylon.model.typechecker.model.Function)9 Type (com.redhat.ceylon.model.typechecker.model.Type)7 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)6 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)6 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)5 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)5 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)5 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)5 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 ArrayList (java.util.ArrayList)4 Class (com.redhat.ceylon.model.typechecker.model.Class)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 Statement (com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement)2 JavaBeanValue (com.redhat.ceylon.model.loader.model.JavaBeanValue)2 Functional (com.redhat.ceylon.model.typechecker.model.Functional)2 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)2 Scope (com.redhat.ceylon.model.typechecker.model.Scope)2 JCTree (com.sun.tools.javac.tree.JCTree)2