Search in sources :

Example 56 with Value

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

the class TypeUtils method convertTupleToParameters.

/**
 * Turns a Tuple type into a parameter list.
 */
public static List<Parameter> convertTupleToParameters(Type _tuple) {
    final ArrayList<Parameter> rval = new ArrayList<>();
    int pos = 0;
    final Unit unit = getUnit(_tuple);
    final Type empty = unit.getEmptyType();
    while (_tuple != null && !(_tuple.isSubtypeOf(empty) || _tuple.isTypeParameter())) {
        Parameter _p = null;
        if (isTuple(_tuple)) {
            _p = new Parameter();
            _p.setModel(new Value());
            if (_tuple.isUnion()) {
                // Handle union types for defaulted parameters
                for (Type mt : _tuple.getCaseTypes()) {
                    if (mt.isTuple()) {
                        _p.getModel().setType(mt.getTypeArgumentList().get(1));
                        _tuple = mt.getTypeArgumentList().get(2);
                        break;
                    }
                }
                _p.setDefaulted(true);
            } else {
                _p.getModel().setType(_tuple.getTypeArgumentList().get(1));
                _tuple = _tuple.getTypeArgumentList().get(2);
            }
        } else if (unit.isSequentialType(_tuple)) {
            // Handle Sequence, for nonempty variadic parameters
            _p = new Parameter();
            _p.setModel(new Value());
            _p.getModel().setType(_tuple.getTypeArgumentList().get(0));
            _p.setSequenced(true);
            _tuple = null;
        } else {
            if (pos > 100) {
                return rval;
            }
        }
        if (_p != null) {
            _p.setName("arg" + pos);
            rval.add(_p);
        }
        pos++;
    }
    return rval;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) ArrayList(java.util.ArrayList) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Unit(org.eclipse.ceylon.model.typechecker.model.Unit)

Example 57 with Value

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

the class JsOutput method publishUnsharedDeclarations.

public void publishUnsharedDeclarations(JsIdentifierNames names) {
    // #489 lists with unshared toplevel members of each package
    for (org.eclipse.ceylon.model.typechecker.model.Package pkg : module.getPackages()) {
        ArrayList<Declaration> unsharedDecls = new ArrayList<>(pkg.getMembers().size());
        for (Declaration d : pkg.getMembers()) {
            if (!d.isShared() && !(d.isAnonymous() && d.getName() != null && d.getName().startsWith("anonymous#")) && (!d.isNative() || d.getNativeBackends().supports(Backend.JavaScript))) {
                unsharedDecls.add(d);
            }
        }
        if (!unsharedDecls.isEmpty()) {
            out("ex$.$pkgunsh$", pkg.getNameAsString().replace('.', '$'), "={");
            boolean first = true;
            for (Declaration d : unsharedDecls) {
                if (d.getName() == null)
                    continue;
                // TODO only use quotes when absolutely necessary
                if (d.isAnonymous()) {
                // Don't generate anything for anonymous types
                } else if (d instanceof Setter) {
                    // ignore
                    if (((Setter) d).getGetter() == null) {
                        if (first)
                            first = false;
                        else
                            out(",");
                        out("'", d.getName(), "':", names.setter(d));
                    }
                } else if (d instanceof Value) {
                    if (first)
                        first = false;
                    else
                        out(",");
                    out("'", d.getName(), "':", names.getter(d, true));
                } else {
                    if (first)
                        first = false;
                    else
                        out(",");
                    out("'", d.getName(), "':", names.name(d));
                }
            }
            out("};\n");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) Value(org.eclipse.ceylon.model.typechecker.model.Value) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 58 with Value

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

the class TestBitAnnotations method testEncode.

@Test
public void testEncode() {
    final Value v = new Value();
    int b = MetamodelGenerator.encodeAnnotations(v.getAnnotations(), v, null);
    Assert.assertEquals(0, b);
    v.getAnnotations().add(new Annotation("shared"));
    b = MetamodelGenerator.encodeAnnotations(v.getAnnotations(), v, null);
    Assert.assertEquals(1, b);
    v.getAnnotations().add(new Annotation("actual"));
    b = MetamodelGenerator.encodeAnnotations(v.getAnnotations(), v, null);
    Assert.assertEquals(3, b);
    v.getAnnotations().add(new Annotation("default"));
    b = MetamodelGenerator.encodeAnnotations(v.getAnnotations(), v, null);
    Assert.assertEquals(11, b);
}
Also used : Value(org.eclipse.ceylon.model.typechecker.model.Value) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation) Test(org.junit.Test)

Example 59 with Value

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

the class RefinementVisitor method visit.

@Override
public void visit(Tree.Declaration that) {
    super.visit(that);
    Declaration dec = that.getDeclarationModel();
    if (dec != null) {
        boolean mayBeShared = dec.isToplevel() || dec.isClassOrInterfaceMember();
        if (dec.isShared() && !mayBeShared) {
            that.addError("shared declaration is not a member of a class, interface, or package: " + message(dec) + " may not be annotated 'shared'", 1200);
        }
        boolean mayBeRefined = dec instanceof Value || dec instanceof Function || dec instanceof Class;
        if (!mayBeRefined) {
            checkNonrefinableDeclaration(that, dec);
        }
        boolean member = dec.isClassOrInterfaceMember() && dec.isShared() && !isConstructor(dec) && // TODO: what about nested interfaces and abstract classes?!
        !(dec instanceof TypeParameter);
        if (member) {
            checkMember(that, dec);
        } else {
            checkNonMember(that, dec);
        }
        if (dec.isNativeImplementation() || isNativeMember(dec)) {
            checkNative(that, dec);
        }
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 60 with Value

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

the class RefinementVisitor method visit.

@Override
public void visit(Tree.SpecifierStatement that) {
    super.visit(that);
    List<Type> sig = new ArrayList<Type>();
    Tree.Term term = that.getBaseMemberExpression();
    while (term instanceof Tree.ParameterizedExpression) {
        sig.clear();
        Tree.ParameterizedExpression pe = (Tree.ParameterizedExpression) term;
        Tree.TypeParameterList typeParameterList = pe.getTypeParameterList();
        if (typeParameterList != null) {
            // TODO: remove this for #1329
            typeParameterList.addError("specification statements may not have type parameters");
        }
        Tree.ParameterList pl = pe.getParameterLists().get(0);
        for (Tree.Parameter p : pl.getParameters()) {
            if (p == null) {
                sig.add(null);
            } else {
                Parameter model = p.getParameterModel();
                if (model != null) {
                    sig.add(model.getType());
                } else {
                    sig.add(null);
                }
            }
        }
        term = pe.getPrimary();
    }
    if (term instanceof Tree.BaseMemberExpression) {
        Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) term;
        Unit unit = that.getUnit();
        TypedDeclaration td = getTypedDeclaration(bme.getScope(), name(bme.getIdentifier()), sig, false, unit);
        if (td != null) {
            that.setDeclaration(td);
            Scope scope = that.getScope();
            Scope container = scope.getContainer();
            Scope realScope = getRealScope(container);
            if (realScope instanceof ClassOrInterface) {
                ClassOrInterface ci = (ClassOrInterface) realScope;
                Scope tdcontainer = td.getContainer();
                if (td.isClassOrInterfaceMember()) {
                    ClassOrInterface tdci = (ClassOrInterface) tdcontainer;
                    if (!tdcontainer.equals(realScope) && ci.inherits(tdci)) {
                        boolean lazy = that.getSpecifierExpression() instanceof Tree.LazySpecifierExpression;
                        if (!lazy && td.isVariable() && td.isJava()) {
                        // allow assignment to variable
                        // member of Java supertype
                        } else // refinement of an inherited member
                        if (tdcontainer == scope) {
                            that.addError("parameter declaration hides refining member: '" + td.getName(unit) + "' (rename parameter)");
                        } else if (td instanceof Value) {
                            refineAttribute((Value) td, bme, that, ci);
                        } else if (td instanceof Function) {
                            refineMethod((Function) td, bme, that, ci);
                        } else {
                            // TODO!
                            bme.addError("not a reference to a formal attribute: '" + td.getName(unit) + "'");
                        }
                    }
                }
            }
        }
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ArrayList(java.util.ArrayList) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Function(org.eclipse.ceylon.model.typechecker.model.Function) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Aggregations

Value (org.eclipse.ceylon.model.typechecker.model.Value)190 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)135 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)77 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)74 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)70 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)69 Type (org.eclipse.ceylon.model.typechecker.model.Type)68 Function (org.eclipse.ceylon.model.typechecker.model.Function)50 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)47 Class (org.eclipse.ceylon.model.typechecker.model.Class)46 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)42 JavaBeanValue (org.eclipse.ceylon.model.loader.model.JavaBeanValue)30 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)30 ArrayList (java.util.ArrayList)29 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)29 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)28 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)27 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)26 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)24 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)23