Search in sources :

Example 1 with TypeAlias

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

the class ExpressionTransformer method transform.

public JCTree transform(Tree.TypeLiteral expr) {
    at(expr);
    if (!expr.getWantsDeclaration()) {
        if (expr.getDeclaration() instanceof Constructor) {
            JCExpression classLiteral = makeTypeLiteralCall(expr.getType().getTypeModel().getQualifyingType(), false, expr.getTypeModel());
            TypeDeclaration classModelDeclaration = (TypeDeclaration) typeFact().getLanguageModuleModelDeclaration(expr.getType().getTypeModel().getQualifyingType().getDeclaration().isMember() ? "MemberClass" : "Class");
            JCTypeCast typeCast = make().TypeCast(makeJavaType(classModelDeclaration.appliedType(null, List.of(expr.getType().getTypeModel().getQualifyingType(), typeFact().getNothingType()))), classLiteral);
            Type callableType = expr.getTypeModel().getFullType();
            JCExpression reifiedArgumentsExpr = makeReifiedTypeArgument(typeFact().getCallableTuple(callableType));
            return make().Apply(null, naming.makeQualIdent(typeCast, "getConstructor"), List.<JCExpression>of(reifiedArgumentsExpr, make().Literal(expr.getDeclaration().getName())));
        } else {
            return makeTypeLiteralCall(expr.getType().getTypeModel(), true, expr.getTypeModel());
        }
    } else if (expr.getDeclaration() instanceof TypeParameter) {
        // we must get it from its container
        TypeParameter declaration = (TypeParameter) expr.getDeclaration();
        Node node = expr;
        return makeTypeParameterDeclaration(node, declaration);
    } else if (expr.getDeclaration() instanceof Constructor || expr instanceof Tree.NewLiteral) {
        Constructor ctor;
        if (expr.getDeclaration() instanceof Constructor) {
            ctor = (Constructor) expr.getDeclaration();
        } else {
            ctor = Decl.getDefaultConstructor((Class) expr.getDeclaration());
        }
        JCExpression metamodelCall = makeTypeDeclarationLiteral(Decl.getConstructedClass(ctor));
        metamodelCall = make().TypeCast(makeJavaType(typeFact().getClassDeclarationType(), JT_RAW), metamodelCall);
        metamodelCall = make().Apply(null, naming.makeQualIdent(metamodelCall, "getConstructorDeclaration"), List.<JCExpression>of(make().Literal(ctor.getName() == null ? "" : ctor.getName())));
        if (Decl.isEnumeratedConstructor(ctor)) {
            metamodelCall = make().TypeCast(makeJavaType(typeFact().getValueConstructorDeclarationType(), JT_RAW), metamodelCall);
        } else /*else if (Decl.isDefaultConstructor(ctor)){
                metamodelCall = make().TypeCast(
                        makeJavaType(typeFact().getDefaultConstructorDeclarationType(), JT_RAW), metamodelCall);
            } */
        {
            metamodelCall = make().TypeCast(makeJavaType(typeFact().getCallableConstructorDeclarationType(), JT_RAW), metamodelCall);
        }
        return metamodelCall;
    } else if (expr.getDeclaration() instanceof ClassOrInterface || expr.getDeclaration() instanceof TypeAlias) {
        // use the generated class to get to the declaration literal
        JCExpression metamodelCall = makeTypeDeclarationLiteral((TypeDeclaration) expr.getDeclaration());
        Type exprType = expr.getTypeModel().resolveAliases();
        // now cast if required
        if (!exprType.isExactly(((TypeDeclaration) typeFact().getLanguageModuleDeclarationDeclaration("NestableDeclaration")).getType())) {
            JCExpression type = makeJavaType(exprType, JT_NO_PRIMITIVES);
            return make().TypeCast(type, metamodelCall);
        }
        return metamodelCall;
    } else {
        return makeErroneous(expr, "compiler bug: " + expr.getDeclaration() + " is an unsupported declaration type");
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTypeCast(com.sun.tools.javac.tree.JCTree.JCTypeCast) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 2 with TypeAlias

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

the class ExpressionTransformer method appendDeclarationLiteralForAnnotation.

/**
 * Appends into the given builder a String representation of the given
 * declaration, suitable for parsing my the DeclarationParser.
 */
private static void appendDeclarationLiteralForAnnotation(Declaration decl, StringBuilder sb) {
    Scope container = decl.getContainer();
    while (true) {
        if (container instanceof Declaration) {
            appendDeclarationLiteralForAnnotation((Declaration) container, sb);
            sb.append(".");
            break;
        } else if (container instanceof Package) {
            appendDeclarationLiteralForAnnotation((Package) container, sb);
            sb.append(":");
            break;
        }
        container = container.getContainer();
    }
    if (decl instanceof Class) {
        sb.append("C").append(decl.getName());
    } else if (decl instanceof Interface) {
        sb.append("I").append(decl.getName());
    } else if (decl instanceof TypeAlias) {
        sb.append("A").append(decl.getName());
    } else if (decl instanceof Value) {
        sb.append("V").append(decl.getName());
    } else if (decl instanceof Function) {
        sb.append("F").append(decl.getName());
    } else if (decl instanceof TypeParameter) {
        sb.append("P").append(decl.getName());
    } else if (decl instanceof Constructor) {
        sb.append("c").append(decl.getName());
    } else {
        throw BugException.unhandledDeclarationCase(decl);
    }
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) FieldValue(com.redhat.ceylon.model.loader.model.FieldValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Package(com.redhat.ceylon.model.typechecker.model.Package) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Example 3 with TypeAlias

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

the class ExpressionTransformer method makeTypeParameterDeclaration.

/**
 * Makes an expression equivalent to the result of {@code `given T`}
 * @param node
 * @param declaration
 * @return
 */
JCExpression makeTypeParameterDeclaration(Node node, TypeParameter declaration) {
    Scope container = declaration.getContainer();
    if (container instanceof Declaration) {
        JCExpression containerExpr;
        Declaration containerDeclaration = (Declaration) container;
        if (containerDeclaration instanceof ClassOrInterface || containerDeclaration instanceof TypeAlias) {
            JCExpression metamodelCall = makeTypeDeclarationLiteral((TypeDeclaration) containerDeclaration);
            JCExpression metamodelCast = makeJavaType(typeFact().getLanguageModuleDeclarationTypeDeclaration("GenericDeclaration").getType(), JT_NO_PRIMITIVES);
            containerExpr = make().TypeCast(metamodelCast, metamodelCall);
        } else if (containerDeclaration.isToplevel()) {
            containerExpr = makeTopLevelValueOrFunctionDeclarationLiteral(containerDeclaration);
        } else {
            containerExpr = makeMemberValueOrFunctionDeclarationLiteral(node, containerDeclaration);
        }
        // now it must be a ClassOrInterfaceDeclaration or a FunctionDeclaration, both of which have the method we need
        return at(node).Apply(null, makeSelect(containerExpr, "getTypeParameterDeclaration"), List.of(ceylonLiteral(declaration.getName())));
    } else {
        return makeErroneous(node, "compiler bug: " + container + " is not a supported type parameter container");
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 4 with TypeAlias

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

the class Naming method appendTypeDeclaration.

private void appendTypeDeclaration(final TypeDeclaration decl, EnumSet<DeclNameFlag> flags, TypeDeclarationBuilder<?> typeDeclarationBuilder, Scope scope, final boolean last) {
    if (scope instanceof Class || scope instanceof TypeAlias || (scope instanceof Constructor && (scope.equals(decl) || !Decl.isLocalNotInitializerScope(scope)))) {
        TypeDeclaration klass = (TypeDeclaration) scope;
        if (klass.isAnonymous() && !klass.isNamed())
            typeDeclarationBuilder.clear();
        typeDeclarationBuilder.append(escapeClassName(klass.getName() != null ? klass.getName() : ""));
        if (Decl.isCeylon(klass)) {
            if (flags.contains(DeclNameFlag.COMPANION) && Decl.isLocalNotInitializer(klass) && last) {
                typeDeclarationBuilder.append(IMPL_POSTFIX);
            } else if (flags.contains(DeclNameFlag.ANNOTATION) && last) {
                typeDeclarationBuilder.append(ANNO_POSTFIX);
            } else if (flags.contains(DeclNameFlag.ANNOTATIONS) && last) {
                typeDeclarationBuilder.append(ANNOS_POSTFIX);
            } else if (flags.contains(DeclNameFlag.DELEGATION) && last) {
                typeDeclarationBuilder.append(DELEGATION_POSTFIX);
            }
        }
    } else if (scope instanceof Interface) {
        Interface iface = (Interface) scope;
        typeDeclarationBuilder.append(iface.getName());
        if (Decl.isCeylon(iface) && ((decl instanceof Class || decl instanceof Constructor || decl instanceof TypeAlias || scope instanceof Constructor) || flags.contains(DeclNameFlag.COMPANION))) {
            typeDeclarationBuilder.append(IMPL_POSTFIX);
        }
    } else if (Decl.isLocalNotInitializerScope(scope)) {
        if (flags.contains(DeclNameFlag.COMPANION) || !(decl instanceof Interface)) {
            typeDeclarationBuilder.clear();
        } else if (flags.contains(DeclNameFlag.QUALIFIED) || (decl instanceof Interface)) {
            Scope nonLocal = scope;
            while (!(nonLocal instanceof Declaration)) {
                nonLocal = nonLocal.getContainer();
            }
            typeDeclarationBuilder.append(((Declaration) nonLocal).getPrefixedName());
            if (!Decl.equalScopes(scope, nonLocal)) {
                typeDeclarationBuilder.append('$');
                typeDeclarationBuilder.append(getLocalId(scope));
            }
            if (decl instanceof Interface) {
                typeDeclarationBuilder.append('$');
            } else {
                if (flags.contains(DeclNameFlag.QUALIFIED)) {
                    typeDeclarationBuilder.selectAppended();
                } else {
                    typeDeclarationBuilder.clear();
                }
            }
        }
        return;
    } else if (scope instanceof TypedDeclaration && ((Declaration) scope).isToplevel()) {
    // nothing? that's just weird
    }
    if (!last) {
        if (decl instanceof Interface && Decl.isCeylon((TypeDeclaration) decl) && !flags.contains(DeclNameFlag.COMPANION)) {
            typeDeclarationBuilder.append('$');
        } else if (decl instanceof Constructor && ((Class) decl.getContainer()).isMember() && decl.getContainer().equals(scope)) {
            typeDeclarationBuilder.append('$');
        } else {
            if (flags.contains(DeclNameFlag.QUALIFIED)) {
                typeDeclarationBuilder.selectAppended();
            } else {
                typeDeclarationBuilder.clear();
            }
        }
    } else {
        typeDeclarationBuilder.selectAppended();
    }
    return;
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Class(com.redhat.ceylon.model.typechecker.model.Class) LazyClass(com.redhat.ceylon.model.loader.model.LazyClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Example 5 with TypeAlias

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

the class CeylonDoc method getIcons.

protected final List<String> getIcons(Object obj) {
    List<String> icons = new ArrayList<String>();
    if (obj instanceof Declaration) {
        Declaration decl = (Declaration) obj;
        Annotation deprecated = Util.findAnnotation(decl, "deprecated");
        if (deprecated != null) {
            icons.add("icon-decoration-deprecated");
        }
        if (decl instanceof ClassOrInterface || decl instanceof Constructor) {
            if (decl instanceof Interface) {
                icons.add("icon-interface");
                if (Util.isEnumerated((ClassOrInterface) decl)) {
                    icons.add("icon-decoration-enumerated");
                }
            }
            if (decl instanceof Class) {
                Class klass = (Class) decl;
                if (klass.isAnonymous()) {
                    icons.add("icon-object");
                } else {
                    icons.add("icon-class");
                }
                if (klass.isAbstract()) {
                    icons.add("icon-decoration-abstract");
                }
                if (klass.isFinal() && !klass.isAnonymous() && !klass.isAnnotation()) {
                    icons.add("icon-decoration-final");
                }
                if (Util.isEnumerated(klass)) {
                    icons.add("icon-decoration-enumerated");
                }
            }
            if (decl instanceof Constructor) {
                icons.add("icon-class");
            }
            if (!decl.isShared()) {
                icons.add("icon-decoration-local");
            }
        }
        if (decl instanceof TypedDeclaration) {
            if (decl.isShared()) {
                icons.add("icon-shared-member");
            } else {
                icons.add("icon-local-member");
            }
            if (decl.isFormal()) {
                icons.add("icon-decoration-formal");
            }
            if (decl.isActual()) {
                Declaration refinedDeclaration = decl.getRefinedDeclaration();
                if (refinedDeclaration != null) {
                    if (refinedDeclaration.isFormal()) {
                        icons.add("icon-decoration-impl");
                    }
                    if (refinedDeclaration.isDefault()) {
                        icons.add("icon-decoration-over");
                    }
                }
            }
            if (((TypedDeclaration) decl).isVariable()) {
                icons.add("icon-decoration-variable");
            }
        }
        if (decl instanceof TypeAlias || decl instanceof NothingType) {
            icons.add("icon-type-alias");
        }
        if (decl.isAnnotation()) {
            icons.add("icon-decoration-annotation");
        }
    }
    if (obj instanceof Package) {
        Package pkg = (Package) obj;
        icons.add("icon-package");
        if (!pkg.isShared()) {
            icons.add("icon-decoration-local");
        }
    }
    if (obj instanceof ModuleImport) {
        ModuleImport moduleImport = (ModuleImport) obj;
        icons.add("icon-module");
        if (moduleImport.isExport()) {
            icons.add("icon-module-exported-decoration");
        }
        if (moduleImport.isOptional()) {
            icons.add("icon-module-optional-decoration");
        }
    }
    if (obj instanceof Module) {
        icons.add("icon-module");
    }
    return icons;
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) Annotation(com.redhat.ceylon.model.typechecker.model.Annotation) ModuleImport(com.redhat.ceylon.model.typechecker.model.ModuleImport) Class(com.redhat.ceylon.model.typechecker.model.Class) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) NothingType(com.redhat.ceylon.model.typechecker.model.NothingType)

Aggregations

TypeAlias (com.redhat.ceylon.model.typechecker.model.TypeAlias)11 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)10 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)9 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)8 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)8 Class (com.redhat.ceylon.model.typechecker.model.Class)7 Interface (com.redhat.ceylon.model.typechecker.model.Interface)7 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)6 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Function (com.redhat.ceylon.model.typechecker.model.Function)4 Value (com.redhat.ceylon.model.typechecker.model.Value)4 JCTree (com.sun.tools.javac.tree.JCTree)4 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)4 LazyInterface (com.redhat.ceylon.model.loader.model.LazyInterface)3 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)3 Package (com.redhat.ceylon.model.typechecker.model.Package)3 Scope (com.redhat.ceylon.model.typechecker.model.Scope)3 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)2