Search in sources :

Example 1 with Unit

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

the class Strategy method inlinePowerAsMultiplication.

/**
 * Determines whether we should inline {@code x^n} as a repeated multiplication
 * ({@code x*x*...*x}) instead of calling {@code x.power(n)}.
 */
public static boolean inlinePowerAsMultiplication(Tree.PowerOp op) {
    java.lang.Long power;
    try {
        power = ExpressionTransformer.getIntegerLiteralPower(op);
        if (power != null) {
            Unit unit = op.getUnit();
            Type baseType = op.getLeftTerm().getTypeModel();
            // up bloating the code (e.g. imagine x^1_000_000_000)
            if (power > 0 && power <= 64 && baseType.isExactly(unit.getIntegerType())) {
                return true;
            } else if (power > 0 && power <= 64 && baseType.isExactly(unit.getFloatType())) {
                return true;
            }
        }
    } catch (ErroneousException e) {
        return false;
    }
    return false;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) Unit(com.redhat.ceylon.model.typechecker.model.Unit)

Example 2 with Unit

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

the class AnnotationModelVisitor method startCollection.

private CollectionLiteralAnnotationTerm startCollection(Tree.Term t) {
    Unit unit = t.getUnit();
    // Continue the visit to collect the elements
    Type iteratedType = unit.getIteratedType(parameter().getType());
    LiteralAnnotationTerm factory;
    if (iteratedType.isString()) {
        factory = StringLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isInteger()) {
        factory = IntegerLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isCharacter()) {
        factory = CharacterLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isBoolean()) {
        factory = BooleanLiteralAnnotationTerm.FACTORY;
    } else if (iteratedType.isFloat()) {
        factory = FloatLiteralAnnotationTerm.FACTORY;
    } else if (Decl.isEnumeratedTypeWithAnonCases(iteratedType)) {
        factory = ObjectLiteralAnnotationTerm.FACTORY;
    } else if (Decl.isAnnotationClass(iteratedType.getDeclaration())) {
        t.addError("compiler bug: iterables of annotation classes or annotation constructors not supported as literal " + (checkingDefaults ? "defaulted parameters" : "arguments"), Backend.Java);
        return null;
    } else if (iteratedType.isSubtypeOf(((TypeDeclaration) unit.getLanguageModuleDeclarationDeclaration("Declaration")).getType())) {
        factory = DeclarationLiteralAnnotationTerm.FACTORY;
    } else {
        throw new RuntimeException();
    }
    CollectionLiteralAnnotationTerm result = this.elements;
    this.elements = new CollectionLiteralAnnotationTerm(factory);
    return result;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) Unit(com.redhat.ceylon.model.typechecker.model.Unit) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 3 with Unit

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

the class LinkRenderer method decorateWithLinkDropdownMenu.

private String decorateWithLinkDropdownMenu(String link, Type producedType) {
    if (!printLinkDropdownMenu || !printAbbreviated || !canLinkToCeylonLanguageModule()) {
        return link;
    }
    List<Type> producedTypes = new ArrayList<Type>();
    decompose(producedType, producedTypes);
    boolean containsOptional = false;
    boolean containsSequential = false;
    boolean containsSequence = false;
    boolean containsIterable = false;
    boolean containsEntry = false;
    boolean containsCallable = false;
    boolean containsTuple = false;
    for (Type pt : producedTypes) {
        if (abbreviateOptional(pt)) {
            containsOptional = true;
        } else if (abbreviateSequential(pt) && !link.contains("'Go to ceylon.language::Sequential'")) {
            containsSequential = true;
        } else if (abbreviateSequence(pt) && !link.contains("'Go to ceylon.language::Sequence'")) {
            containsSequence = true;
        } else if (abbreviateIterable(pt) && !link.contains("'Go to ceylon.language::Iterable'")) {
            containsIterable = true;
        } else if (abbreviateEntry(pt) && !link.contains("'Go to ceylon.language::Entry'")) {
            containsEntry = true;
        } else if (abbreviateCallable(pt) && !link.contains("'Go to ceylon.language::Callable'")) {
            containsCallable = true;
        } else if (abbreviateTuple(pt) && !link.contains("'Go to ceylon.language::Tuple'")) {
            containsTuple = true;
        }
    }
    Unit unit = producedType.getDeclaration().getUnit();
    if (containsOptional || containsSequential || containsSequence || containsIterable || containsEntry || containsCallable || containsTuple) {
        StringBuilder sb = new StringBuilder();
        sb.append("<span class='link-dropdown'>");
        sb.append(link.replaceAll("class='link'", "class='link type-identifier'"));
        sb.append("<span class='dropdown'>");
        sb.append("<a class='dropdown-toggle' data-toggle='dropdown' href='#'><b title='Show more links' class='caret'></b></a>");
        sb.append("<ul class='dropdown-menu'>");
        if (containsOptional) {
            sb.append(getLinkMenuItem(unit.getNullDeclaration(), "abbreviations X? means Null|X"));
        }
        if (containsSequential) {
            sb.append(getLinkMenuItem(unit.getSequentialDeclaration(), "abbreviation X[] or [X*] means Sequential&lt;X&gt;"));
        }
        if (containsSequence) {
            sb.append(getLinkMenuItem(unit.getSequenceDeclaration(), "abbreviation [X+] means Sequence&lt;X&gt;"));
        }
        if (containsIterable) {
            sb.append(getLinkMenuItem(unit.getIterableDeclaration(), "abbreviation {X+} or {X*} means Iterable&lt;X,Nothing&gt; or Iterable&lt;X,Null&gt;"));
        }
        if (containsEntry) {
            sb.append(getLinkMenuItem(unit.getEntryDeclaration(), "abbreviation X-&gt;Y means Entry&lt;X,Y&gt;"));
        }
        if (containsCallable) {
            sb.append(getLinkMenuItem(unit.getCallableDeclaration(), "abbreviation X(Y,Z) means Callable&lt;X,[Y,Z]&gt;"));
        }
        if (containsTuple) {
            sb.append(getLinkMenuItem(unit.getTupleDeclaration(), "abbreviation [X,Y] means Tuple&lt;X|Y,X,Tuple&lt;Y,Y,[]&gt;&gt;"));
        }
        // dropdown-menu
        sb.append("</ul>");
        // dropdown
        sb.append("</span>");
        // link-dropdown
        sb.append("</span>");
        return sb.toString();
    }
    return link;
}
Also used : Util.isAbbreviatedType(com.redhat.ceylon.ceylondoc.Util.isAbbreviatedType) NothingType(com.redhat.ceylon.model.typechecker.model.NothingType) Type(com.redhat.ceylon.model.typechecker.model.Type) ArrayList(java.util.ArrayList) Unit(com.redhat.ceylon.model.typechecker.model.Unit)

Example 4 with Unit

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

the class CeylonDocTool method getSrcUrl.

/**
 * Gets a URL for the source file containing the given thing
 * @param from Where the link is relative to
 * @param modPkgOrDecl e.g. Module, Package or Declaration
 * @return A (relative) URL, or null if no source file exists (e.g. for a
 * package or a module without a descriptor)
 * @throws IOException
 */
protected String getSrcUrl(Object from, Object modPkgOrDecl) throws IOException {
    URI fromUrl = getAbsoluteObjectUrl(from);
    Module module = getModule(from);
    String filename;
    File folder;
    if (modPkgOrDecl instanceof Element) {
        Unit unit = ((Element) modPkgOrDecl).getUnit();
        filename = unit.getFilename();
        folder = getFolder(unit.getPackage());
    } else if (modPkgOrDecl instanceof Package) {
        filename = "package.ceylon";
        folder = getFolder((Package) modPkgOrDecl);
    } else if (modPkgOrDecl instanceof Module) {
        Module moduleDecl = (Module) modPkgOrDecl;
        folder = getApiOutputFolder(moduleDecl);
        filename = Constants.MODULE_DESCRIPTOR;
    } else {
        throw new RuntimeException(CeylondMessages.msg("error.unexpected", modPkgOrDecl));
    }
    File srcFile = new File(folder, filename + ".html").getCanonicalFile();
    String result;
    if (srcFile.exists()) {
        URI url = srcFile.toURI();
        result = relativize(module, fromUrl, url).toString();
    } else {
        result = null;
    }
    return result;
}
Also used : Element(com.redhat.ceylon.model.typechecker.model.Element) Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) Unit(com.redhat.ceylon.model.typechecker.model.Unit) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) URI(java.net.URI) File(java.io.File)

Example 5 with Unit

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

the class ClassOrPackageDoc method isConstantValue.

private boolean isConstantValue(Declaration d) {
    if (Decl.isValue(d)) {
        Value value = (Value) d;
        if (value.isShared() && !value.isVariable()) {
            Unit unit = value.getUnit();
            Type type = value.getType();
            if (type.isSequential()) {
                type = unit.getSequentialElementType(type);
            }
            if (type.isString() || type.isInteger() || type.isFloat() || type.isCharacter()) {
                return true;
            }
        }
    }
    return false;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) Util.isAbbreviatedType(com.redhat.ceylon.ceylondoc.Util.isAbbreviatedType) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) Unit(com.redhat.ceylon.model.typechecker.model.Unit)

Aggregations

Unit (com.redhat.ceylon.model.typechecker.model.Unit)6 Type (com.redhat.ceylon.model.typechecker.model.Type)4 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)3 Util.isAbbreviatedType (com.redhat.ceylon.ceylondoc.Util.isAbbreviatedType)2 CompilationUnit (com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit)2 AnnotationModelVisitor (com.redhat.ceylon.compiler.java.codegen.AnnotationModelVisitor)1 BoxingDeclarationVisitor (com.redhat.ceylon.compiler.java.codegen.BoxingDeclarationVisitor)1 BoxingVisitor (com.redhat.ceylon.compiler.java.codegen.BoxingVisitor)1 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)1 CompilerBoxingDeclarationVisitor (com.redhat.ceylon.compiler.java.codegen.CompilerBoxingDeclarationVisitor)1 CompilerBoxingVisitor (com.redhat.ceylon.compiler.java.codegen.CompilerBoxingVisitor)1 DeferredVisitor (com.redhat.ceylon.compiler.java.codegen.DeferredVisitor)1 DefiniteAssignmentVisitor (com.redhat.ceylon.compiler.java.codegen.DefiniteAssignmentVisitor)1 InterfaceVisitor (com.redhat.ceylon.compiler.java.codegen.InterfaceVisitor)1 JvmMissingNativeVisitor (com.redhat.ceylon.compiler.java.codegen.JvmMissingNativeVisitor)1 TypeParameterCaptureVisitor (com.redhat.ceylon.compiler.java.codegen.TypeParameterCaptureVisitor)1 UnsupportedVisitor (com.redhat.ceylon.compiler.java.codegen.UnsupportedVisitor)1 CeylonPhasedUnit (com.redhat.ceylon.compiler.java.tools.CeylonPhasedUnit)1 UsageWarning (com.redhat.ceylon.compiler.typechecker.analyzer.UsageWarning)1 Warning (com.redhat.ceylon.compiler.typechecker.analyzer.Warning)1