Package com.redhat.ceylon.compiler.java.codegen.Naming

Examples of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName


                loopVarType = variable.getDeclarationModel().getType();
                loopVarInit = at(stmt).Apply(null, makeSelect(castElem, Naming.getGetterName("key")), List.<JCExpression> nil());
            }
            JCVariableDecl itemOrKeyDecl = at(stmt).VarDef(make().Modifiers(FINAL, annots), loopVarName.asName(), makeJavaType(loopVarType),
                    boxUnboxIfNecessary(loopVarInit, true, loopVarType, CodegenUtil.getBoxingStrategy(variable.getDeclarationModel())));
            final SyntheticName iteratorVarName = loopVarName.suffixedBy(Suffix.$iterator$).alias();
            List<JCStatement> itemDecls = List.<JCStatement> of(itemOrKeyDecl);

            if (valueVariable != null) {
                // final V n = $elem$X.getElement();
                ProducedType valueVarType = valueVariable.getDeclarationModel().getType();
View Full Code Here


        protected ListBuffer<JCStatement> transformForClause() {
            ListBuffer<JCStatement> result = ListBuffer.<JCStatement>lb();
           
            // Note: Must invoke lhs, rhs and increment in the correct order!
            // long start = <lhs>
            SyntheticName start = naming.temp("start");
            result.append(make().VarDef(make().Modifiers(FINAL), start.asName(), makeType(),
                    expressionGen().transformExpression(lhs, BoxingStrategy.UNBOXED, getType())));
            // long end = <rhs>
            SyntheticName end = naming.temp("end");
            result.append(make().VarDef(make().Modifiers(FINAL), end.asName(), makeType(),
                    expressionGen().transformExpression(rhs, BoxingStrategy.UNBOXED, getType())));
           
            final SyntheticName by;
            if (increment != null) {
                by = naming.temp("by");
                // by = increment;
                result.append(make().VarDef(make().Modifiers(FINAL), by.asName(), makeType(),
                        expressionGen().transformExpression(increment, BoxingStrategy.UNBOXED, getType())));
                // if (by <= 0) throw Exception("step size must be greater than zero");
                result.append(make().If(
                        make().Binary(JCTree.LE, by.makeIdent(), make().Literal(0)),
                        makeThrowAssertionException(
                                new AssertionExceptionMessageBuilder(null)
                                    .appendViolatedCondition("step > 0")
                                    .prependAssertionDoc("step size must be greater than zero")
                                    .build()),
                        null));
            } else {
                by = null;
            }
           
            SyntheticName increasing = naming.temp("increasing");
            // boolean increasing = start < end;
            result.append(make().VarDef(make().Modifiers(FINAL), increasing.asName(), make().Type(syms().booleanType),
                    make().Binary(JCTree.LE, start.makeIdent(), end.makeIdent())));
           
            SyntheticName incr = naming.temp("incr");
           
            result.append(make().VarDef(make().Modifiers(FINAL), incr.asName(), makeType(),
                    make().Conditional(
                            increasing.makeIdent(),
                            makeIncreasingIncrement(by), makeDecreasingIncrement(by))));
           
            SyntheticName varname = naming.alias(getVariable().getIdentifier().getText());
            JCVariableDecl init = make().VarDef(make().Modifiers(0), varname.asName(), makeType(), start.makeIdent());
            Tree.ControlClause prevForclause = currentForClause;
            currentForClause = stmt.getForClause();
            List<JCStatement> blockStatements = transformBlock(getBlock());
            currentForClause = prevForclause;
            blockStatements = blockStatements.prepend(make().VarDef(make().Modifiers(FINAL),
                    names().fromString(Naming.getVariableName(getVariable())),
                    makeType(),
                    varname.makeIdent()));
           
            // for (long i = start; (increasing ? i -end <= 0 : i -end >= 0); i+=inc) {
            JCConditional cond = make().Conditional(increasing.makeIdent(),
                    make().Binary(JCTree.LE, make().Binary(JCTree.MINUS, varname.makeIdent(), end.makeIdent()), makeZero()),
                    make().Binary(JCTree.GE, make().Binary(JCTree.MINUS, varname.makeIdent(), end.makeIdent()), makeZero()));
            List<JCExpressionStatement> step = List.<JCExpressionStatement>of(make().Exec(make().Assignop(JCTree.PLUS_ASG, varname.makeIdent(), incr.makeIdent())));
            result.append(make().Labelled(this.label, make().ForLoop(
                    List.<JCStatement>of(init),
                    cond,
                    step,
                    make().Block(0, blockStatements))));
View Full Code Here

            // about which are more common
            java.util.List<CaseClause> list = super.getCaseClauses(stmt);
            java.util.ArrayList<CaseClause> cheap = new ArrayList<CaseClause>(list.size());
            int lastCheap = 0;
            // The dummy isn't actually used for anything, it just has to be non-null
            SyntheticName dummy = naming.synthetic(Naming.Unfix.$annotationSequence$);
            for (Tree.CaseClause clause : list) {
                Tree.CaseItem item = clause.getCaseItem();
                boolean isCheap;
                if (item instanceof Tree.IsCase) {
                    isCheap = isTypeTestCheap(null, dummy,
View Full Code Here

        // TODO Only when the iterable *could be* an array (e.g. if static type is Iterable, but not if static type is Sequence)
        // TODO Need to use naming.Infix for the hidden members of Array
        boolean optForArray = allowArrayOpt && typeFact().getArrayType(iteratedType).isSubtypeOf(iterableType);
        boolean optForTuple = allowArraySeqOpt && typeFact().getTupleType(Collections.singletonList(iteratedType), true, false, -1).isSubtypeOf(iterableType);
       
        SyntheticName iterableName = optForArray || optForTuple ? naming.alias("iterable") : null;
        SyntheticName isArrayName = optForArray ? naming.alias("isArray") : null;
        SyntheticName isTupleName = optForTuple ? naming.alias("isTuple") : null;
        SyntheticName arrayName = optForArray || optForTuple ? naming.alias("array") : null;
        SyntheticName arrayIndex = optForArray || optForTuple ? naming.alias("i") : null;
        SyntheticName arrayLength = optForArray || optForTuple ? naming.alias("length") : null;
        if (optForArray || optForTuple) {
            result.append(makeVar(FINAL, iterableName, makeJavaType(typeFact().getIterableType(iteratorElementType)), iterableExpr));
        }
        if (optForArray) {
            result.append(makeVar(FINAL, isArrayName,
                    make().Type(syms().booleanType),
                    make().TypeTest(iterableName.makeIdent(),
                            makeJavaType(typeFact().getArrayType(iteratorElementType), JT_RAW))));
        }
        if (optForTuple) {
            result.append(makeVar(FINAL, isTupleName,
                    make().Type(syms().booleanType),
                    make().Binary(JCTree.AND,
                            make().TypeTest(iterableName.makeIdent(),
                                    make().QualIdent(syms().ceylonTupleType.tsym)),
                            make().Binary(JCTree.NE,
                                    make().Apply(null,
                                            naming.makeQualIdent(
                                                    make().TypeCast(make().QualIdent(syms().ceylonTupleType.tsym), iterableName.makeIdent()),
                                                    Unfix.$getArray$.toString()),
                                                List.<JCExpression>nil()),
                                    makeNull()))));
        }
       
        // java.lang.Object ELEM_NAME;
        JCVariableDecl elemDecl = makeVar(iterationVarName, make().Type(syms().objectType), optForArray || optForTuple ? makeNull() : null);
        result.append(elemDecl);
       
        SyntheticName iterName = iteratorVarName;
       
        ProducedType iteratorType = typeFact().getIteratorType(iteratorElementType);
        JCExpression iteratorTypeExpr = makeJavaType(iteratorType, CeylonTransformer.JT_TYPE_ARGUMENT);
       
        // ceylon.language.Iterator<T> LOOP_VAR_NAME$iter$X = ITERABLE.getIterator();
        // We don't need to unerase here as anything remotely a sequence will be erased to Iterable, which has getIterator()
        JCExpression getIter;
        if (optForArray || optForTuple) {
            at(node);
            result.append(makeVar(FINAL, arrayName, make().Type(syms().objectType), null));
            result.append(makeVar(arrayIndex, make().Type(syms().intType), make().Literal(0)));
            result.append(makeVar(FINAL, arrayLength, make().Type(syms().intType), null));
            ListBuffer<JCStatement> whenTuple = ListBuffer.<JCTree.JCStatement>lb();
            whenTuple.append(make().Exec(make().Assign(
                    arrayName.makeIdent(),
                    make().Apply(null,
                            naming.makeQualIdent(
                                    make().TypeCast(make().QualIdent(syms().ceylonTupleType.tsym), iterableName.makeIdent()),
                                    Unfix.$getArray$.toString()),
                            List.<JCExpression>nil()))));
            whenTuple.append(make().Exec(make().Assign(
                    arrayIndex.makeIdent(),
                    make().Apply(null,
                            naming.makeQualIdent(
                                    make().TypeCast(make().QualIdent(syms().ceylonTupleType.tsym), iterableName.makeIdent()),
                                    Unfix.$getFirst$.toString()),
                            List.<JCExpression>nil()))));
            whenTuple.append(make().Exec(make().Assign(
                    arrayLength.makeIdent(),
                    make().Binary(JCTree.PLUS, arrayIndex.makeIdent(), make().Apply(null,
                            naming.makeQualIdent(
                                    make().TypeCast(make().QualIdent(syms().ceylonTupleType.tsym), iterableName.makeIdent()),
                                    Unfix.$getLength$.toString()),
                            List.<JCExpression>nil())))));
           
            ListBuffer<JCStatement> whenArray = ListBuffer.<JCTree.JCStatement>lb();
            whenArray.append(make().Exec(make().Assign(
                    arrayName.makeIdent(),
                    make().Apply(null,
                            naming.makeQualIdent(
                                    make().TypeCast(makeJavaType(typeFact().getArrayType(typeFact().getAnythingDeclaration().getType()), JT_RAW), iterableName.makeIdent()),
                                    "toArray"),
                            List.<JCExpression>nil()))));
            whenArray.append(make().Exec(make().Assign(
                    arrayLength.makeIdent(),
                    make().Apply(null,
                            naming.makeQuotedFQIdent("com.redhat.ceylon.compiler.java.Util.arrayLength"),
                            List.<JCExpression>of(arrayName.makeIdent())))));
            ListBuffer<JCStatement> whenIterable = ListBuffer.<JCTree.JCStatement>lb();
            whenIterable.append(make().Exec(make().Assign(
                    arrayName.makeIdent(),
                    makeNull())));
            whenIterable.append(make().Exec(make().Assign(
                    arrayLength.makeIdent(),
                    make().Literal(0))));
            if (optForArray && optForTuple) {
                result.append(make().If(isTupleName.makeIdent(),
                        make().Block(0, whenTuple.toList()),
                        make().If(isArrayName.makeIdent(),
                            make().Block(0, whenArray.toList()),
                            make().Block(0, whenIterable.toList()))));
            } else {
                result.append(make().If((optForArray ? isArrayName : isTupleName).makeIdent(),
                        make().Block(0, (optForArray ? whenArray : whenTuple).toList()),
                        make().Block(0, whenIterable.toList())));
            }
           
            getIter = make().Conditional(
                    optForArray && optForTuple ? make().Binary(JCTree.OR, isTupleName.makeIdent(), isArrayName.makeIdent()): optForArray ? isArrayName.makeIdent() : isTupleName.makeIdent(),
                    makeNull(),
                    make().Apply(null, makeSelect(iterableName.makeIdent(), "iterator"), List.<JCExpression> nil()));
        } else {
            getIter = at(node).Apply(null, makeSelect(iterableExpr, "iterator"), List.<JCExpression> nil());
        }
        getIter = gen().expressionGen().applyErasureAndBoxing(getIter, iteratorType, true, BoxingStrategy.BOXED, iteratorType);
        JCVariableDecl iteratorDecl = at(node).VarDef(make().Modifiers(0), iterName.asName(), iteratorTypeExpr, getIter);
        // .ceylon.language.Iterator<T> LOOP_VAR_NAME$iter$X = ITERABLE.getIterator();
        result.append(iteratorDecl);
       
        ListBuffer<JCStatement> loopBody = ListBuffer.<JCStatement>lb();
       
        if(optForArray || optForTuple) {
            JCExpression cond;
            if (optForArray && optForTuple) {
                cond = make().Binary(JCTree.OR, isTupleName.makeIdent(), isArrayName.makeIdent());
            } else if (optForArray) {
                cond = isArrayName.makeIdent();
            } else {
                cond = isTupleName.makeIdent();
            }
            loopBody.append(make().If(cond,
                    make().Exec(make().Assign(iterationVarName.makeIdent(),
                            make().Apply(null,
                                    naming.makeQuotedFQIdent("com.redhat.ceylon.compiler.java.Util.getObjectArray"),
                                    List.<JCExpression>of(arrayName.makeIdent(),
                                            make().Unary(JCTree.POSTINC, arrayIndex.makeIdent()))))),
                    null));
        }
       
        if (itemDecls != null) {
            loopBody.appendList(itemDecls);
        }

        // The user-supplied contents of the loop
        loopBody.appendList(bodyStmts);
       
        // ELEM_NAME = LOOP_VAR_NAME$iter$X.next()
        JCExpression iter_elem = make().Apply(null, makeSelect(iterName.makeIdent(), "next"), List.<JCExpression> nil());
        JCExpression elem_assign = make().Assign(iterationVarName.makeIdent(), iter_elem);
        // !((ELEM_NAME = LOOP_VAR_NAME$iter$X.next()) instanceof Finished)
        JCExpression instof = make().TypeTest(elem_assign, makeIdent(syms().ceylonFinishedType));
        JCExpression loopCond = make().Unary(JCTree.NOT, instof);
        if (optForArray || optForTuple) {
View Full Code Here

     */
    private List<JCCatch> transformCatchesIfElseIf(
            java.util.List<Tree.CatchClause> catchClauses) {
        ProducedType supertype = intersectionOfCatchClauseTypes(catchClauses);
        JCExpression exceptionType = makeJavaType(supertype, JT_CATCH | JT_RAW);
        SyntheticName exceptionVar = naming.alias("exception");
        JCVariableDecl param = make().VarDef(
                make().Modifiers(Flags.FINAL),
                exceptionVar.asName(),
                exceptionType, null);
       
        ArrayList<Tree.CatchClause> reversed = new ArrayList<Tree.CatchClause>(catchClauses);
        Collections.reverse(reversed);
       
        JCStatement elsePart = make().Throw(exceptionVar.makeIdent());
       
        for (Tree.CatchClause catchClause : reversed) {
            Tree.Variable caughtVar = catchClause.getCatchVariable().getVariable();
            ProducedType caughtType = caughtVar.getType().getTypeModel();
            List<JCStatement> catchBlock = transformBlock(catchClause.getBlock());
            catchBlock = catchBlock.prepend(
                    makeVar(FINAL,
                        caughtVar.getIdentifier().getText(),
                        makeJavaType(caughtType),
                        expressionGen().applyErasureAndBoxing(exceptionVar.makeIdent(),
                                supertype, true, true, BoxingStrategy.BOXED, caughtType, 0)));
            elsePart = make().If(makeOptimizedTypeTest(null, exceptionVar, caughtType, caughtType),
                    make().Block(0, catchBlock),
                    elsePart);
        }
View Full Code Here

        }
       
        protected ListBuffer<JCStatement> transformForClause() {
            ListBuffer<JCStatement> stmts = ListBuffer.<JCStatement>lb();
           
            SyntheticName stringName = naming.alias("s");
            stmts.append(makeVar(stringName, make().Type(syms().stringType),
                    expressionGen().transformExpression(baseIterable, BoxingStrategy.UNBOXED, baseIterable.getTypeModel())));
           
            SyntheticName lengthName = naming.alias("length");
            stmts.append(makeVar(lengthName, make().Type(syms().intType),
               
                                make().Apply(null,
                                        makeQualIdent(stringName.makeIdent(), "length"),
                                        List.<JCExpression>nil())));
           
            SyntheticName indexName = naming.alias("index");
           
            List<JCStatement> transformedBlock = transformBlock(getBlock());
            transformedBlock = transformedBlock.prepend(make().Exec(
                    make().Assignop(JCTree.PLUS_ASG, indexName.makeIdent(),
                        make().Apply(null,
                                naming.makeQualIdent(make().Type(syms().characterObjectType), "charCount"),
                                List.<JCExpression>of(naming.makeQuotedIdent(Naming.getVariableName(getElementOrKeyVariable())))))));
            transformedBlock = transformedBlock.prepend(makeVar(FINAL,
                    Naming.getVariableName(getElementOrKeyVariable()),
                    make().Type(syms().intType),
                    make().Apply(null,
                            naming.makeQualIdent(stringName.makeIdent(), "codePointAt"),
                            List.<JCExpression>of(indexName.makeIdent()))));
           
            JCStatement block = make().Block(0, transformedBlock);
           
           
            JCForLoop loop = make().ForLoop(
                    List.<JCStatement>of(makeVar(indexName, make().Type(syms().intType), make().Literal(0))),
                    make().Binary(JCTree.LT, indexName.makeIdent(), lengthName.makeIdent()),
                    List.<JCExpressionStatement>nil(),
                    block);
            stmts.add(make().Labelled(this.label, loop));
           
            return stmts;
View Full Code Here

            result.add(makeVar(FINAL, indexableName,
                    makeIndexableType(),
                    makeIndexable()));
           
            // int step = ...
            final SyntheticName stepName;
            if (this.step != null) {
                JCExpression stepExpr = makeStepExpr();
                stepName = naming.alias("step");
                result.add(makeVar(FINAL, stepName,
                        makeIndexType(),
                        stepExpr));
                result.add(
                make().If(
                        make().Binary(JCTree.LE, stepName.makeIdent(), make().Literal(0)),
                        makeThrowAssertionException(
                                new AssertionExceptionMessageBuilder(null)
                                    .appendViolatedCondition("step > 0")
                                    .prependAssertionDoc("step size must be greater than zero")
                                    .build()),
                        null));
               
            } else {
                stepName = null;
            }
           
            // int length = java.lang.reflect.Array.getLength(array);
            JCExpression lengthExpr = makeLengthExpr();
            if (lengthExpr != null) {
                result.add(makeVar(FINAL, lengthName,
                        makeIndexType(),
                        lengthExpr));
            }
           
            // int i = 0;
            JCStatement iVar = makeVar(indexName,
                    makeIndexType(),
                    makeIndexInit());
            // i < length;
            JCExpression iCond = makeCondition();
            // i++
            JCExpression iIncr = makeIncrement(stepName);
           
            Tree.ControlClause prevControlClause = currentForClause;
            currentForClause = stmt.getForClause();
            List<JCStatement> transformedBlock = transformBlock(getBlock());
            currentForClause = prevControlClause;
           
            // FOO element = java.lang.reflect.Array.get(array, i);
            JCExpression elementGet = makeIndexedAccess();
           
            Tree.ForIterator forIterator = getForIterator();
            if (forIterator instanceof Tree.ValueIterator) {
                String varName = Naming.getVariableName(((Tree.ValueIterator)forIterator).getVariable());
                JCStatement variable = makeVar(FINAL,
                        varName,
                        makeJavaType(elementType),
                        elementGet);
                // Prepend to the block
                transformedBlock = transformedBlock.prepend(variable);
            } else if (forIterator instanceof Tree.KeyValueIterator) {
                SyntheticName entryName = naming.alias("entry");
                JCStatement entryVariable = makeVar(FINAL, entryName,
                        makeJavaType(typeFact().getEntryType(typeFact().getAnythingDeclaration().getType(), typeFact().getAnythingDeclaration().getType()), JT_RAW),
                        elementGet);
                ProducedType entryType = elementType.getSupertype(typeFact().getEntryDeclaration());
                ProducedType keyType = entryType.getTypeArgumentList().get(0);
                String keyName = Naming.getVariableName(((Tree.KeyValueIterator)forIterator).getKeyVariable());
                JCStatement keyVariable = makeVar(FINAL,
                        keyName,
                        makeJavaType(keyType),
                        expressionGen().applyErasureAndBoxing(
                                make().Apply(null, naming.makeQualIdent(entryName.makeIdent(), "getKey"), List.<JCExpression>nil()),
                                typeFact().getAnythingDeclaration().getType(), true, BoxingStrategy.UNBOXED, keyType));
                ProducedType valueType = entryType.getTypeArgumentList().get(1);
                String valueName = Naming.getVariableName(((Tree.KeyValueIterator)forIterator).getValueVariable());
                JCStatement valueVariable = makeVar(FINAL,
                        valueName,
                        makeJavaType(valueType),
                        expressionGen().applyErasureAndBoxing(
                                make().Apply(null, naming.makeQualIdent(entryName.makeIdent(), "getItem"), List.<JCExpression>nil()),
                                typeFact().getAnythingDeclaration().getType(), true, BoxingStrategy.UNBOXED, valueType));
                // Prepend to the block
                transformedBlock = transformedBlock.prepend(valueVariable);
                transformedBlock = transformedBlock.prepend(keyVariable);
                transformedBlock = transformedBlock.prepend(entryVariable);
View Full Code Here

                Cond transformedCond = statementGen().transformCondition(condition, null);
                // The innermost condition's test should be transformed before
                // variable substitution
               
                JCExpression test = transformedCond.makeTest();
                SyntheticName resultVarName = addVarSubs(transformedCond);
                return transformCommon(transformedCond,
                        test,
                        insideCheck,
                        resultVarName);
            }
View Full Code Here

            }
           
            protected List<JCStatement> transformIntermediate(Tree.Condition condition, java.util.List<Tree.Condition> rest) {
                Cond transformedCond = statementGen().transformCondition(condition, null);
                JCExpression test = transformedCond.makeTest();
                SyntheticName resultVarName = addVarSubs(transformedCond);
                return transformCommon(transformedCond, test, transformList(rest), resultVarName);
            }
View Full Code Here

            }

            private SyntheticName addVarSubs(Cond transformedCond) {
                if (transformedCond.hasResultDecl()) {
                    Tree.Variable var = transformedCond.getVariable();
                    SyntheticName resultVarName = naming.alias(transformedCond.getVariableName().getName());
                    fieldSubst.add(naming.addVariableSubst(var.getDeclarationModel(), resultVarName.getName()));
                    return resultVarName;
                }
                return null;
            }
View Full Code Here

TOP

Related Classes of com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.