Search in sources :

Example 16 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class SoyNodeCompiler method visitIfNode.

@Override
protected Statement visitIfNode(IfNode node) {
    List<IfBlock> ifs = new ArrayList<>();
    Optional<Statement> elseBlock = Optional.absent();
    for (SoyNode child : node.getChildren()) {
        if (child instanceof IfCondNode) {
            IfCondNode icn = (IfCondNode) child;
            SoyExpression cond = exprCompiler.compile(icn.getExpr()).coerceToBoolean();
            Statement block = visitChildrenInNewScope(icn);
            ifs.add(IfBlock.create(cond, block));
        } else {
            IfElseNode ien = (IfElseNode) child;
            elseBlock = Optional.of(visitChildrenInNewScope(ien));
        }
    }
    return ControlFlow.ifElseChain(ifs, elseBlock);
}
Also used : IfElseNode(com.google.template.soy.soytree.IfElseNode) SoyNode(com.google.template.soy.soytree.SoyNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Statement(com.google.template.soy.jbcsrc.restricted.Statement) ArrayList(java.util.ArrayList) IfBlock(com.google.template.soy.jbcsrc.ControlFlow.IfBlock)

Example 17 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class MapToLegacyObjectMapFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression soyExpression = Iterables.getOnlyElement(args);
    SoyType originalType = soyExpression.soyRuntimeType().soyType();
    LegacyObjectMapType newType;
    if (originalType instanceof MapType) {
        newType = LegacyObjectMapType.of(((MapType) originalType).getKeyType(), ((MapType) originalType).getValueType());
    } else if (originalType instanceof UnknownType) {
        newType = LegacyObjectMapType.of(UnknownType.getInstance(), UnknownType.getInstance());
    } else {
        throw new IllegalArgumentException("mapToLegacyObjectMap() expects input to be MAP, get " + originalType.getKind());
    }
    return SoyExpression.forLegacyObjectMap(newType, JbcSrcMethods.MAP_TO_LEGACY_OBJECT_MAP.invoke(soyExpression.box().checkedCast(SoyMap.class)));
}
Also used : UnknownType(com.google.template.soy.types.UnknownType) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) SoyType(com.google.template.soy.types.SoyType) LegacyObjectMapType(com.google.template.soy.types.LegacyObjectMapType) LegacyObjectMapType(com.google.template.soy.types.LegacyObjectMapType) MapType(com.google.template.soy.types.MapType)

Example 18 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class MinFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression left = args.get(0);
    SoyExpression right = args.get(1);
    if (left.assignableToNullableInt() && right.assignableToNullableInt()) {
        return SoyExpression.forInt(JbcSrcMethods.MATH_MIN_LONG.invoke(left.unboxAs(long.class), right.unboxAs(long.class)));
    } else if (left.assignableToNullableFloat() && right.assignableToNullableFloat()) {
        return SoyExpression.forFloat(JbcSrcMethods.MATH_MIN_DOUBLE.invoke(left.unboxAs(double.class), right.unboxAs(double.class)));
    } else {
        return SoyExpression.forSoyValue(NUMBER_TYPE, JbcSrcMethods.MIN_FN.invoke(left.box(), right.box()));
    }
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression)

Example 19 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class LegacyObjectMapToMapFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression soyExpression = Iterables.getOnlyElement(args);
    SoyType originalType = soyExpression.soyRuntimeType().soyType();
    MapType newType;
    if (originalType instanceof LegacyObjectMapType) {
        newType = MapType.of(((LegacyObjectMapType) originalType).getKeyType(), ((LegacyObjectMapType) originalType).getValueType());
    } else if (originalType instanceof UnknownType) {
        newType = MapType.of(UnknownType.getInstance(), UnknownType.getInstance());
    } else {
        throw new IllegalArgumentException("legacyObjectMapToMap() expects input to be LEGACY_OBJECT_MAP, get " + originalType.getKind());
    }
    return SoyExpression.forMap(newType, JbcSrcMethods.LEGACY_OBJECT_MAP_TO_MAP.invoke(soyExpression.box().checkedCast(SoyDict.class)));
}
Also used : UnknownType(com.google.template.soy.types.UnknownType) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) SoyType(com.google.template.soy.types.SoyType) LegacyObjectMapType(com.google.template.soy.types.LegacyObjectMapType) LegacyObjectMapType(com.google.template.soy.types.LegacyObjectMapType) MapType(com.google.template.soy.types.MapType)

Example 20 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class AugmentMapFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression arg0 = args.get(0);
    SoyExpression arg1 = args.get(1);
    Expression first = arg0.checkedCast(SoyDict.class);
    Expression second = arg1.checkedCast(SoyDict.class);
    // TODO(lukes): this logic should move into the ResolveExpressionTypesVisitor
    LegacyObjectMapType mapType = LegacyObjectMapType.of(StringType.getInstance(), UnionType.of(getMapValueType(arg0.soyType()), getMapValueType(arg1.soyType())));
    return SoyExpression.forSoyValue(mapType, JbcSrcMethods.AUGMENT_MAP_FN.invoke(first, second));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) LegacyObjectMapType(com.google.template.soy.types.LegacyObjectMapType)

Aggregations

SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)21 Expression (com.google.template.soy.jbcsrc.restricted.Expression)8 Statement (com.google.template.soy.jbcsrc.restricted.Statement)5 SoyType (com.google.template.soy.types.SoyType)4 Label (org.objectweb.asm.Label)4 Scope (com.google.template.soy.jbcsrc.TemplateVariableManager.Scope)3 Variable (com.google.template.soy.jbcsrc.TemplateVariableManager.Variable)3 LegacyObjectMapType (com.google.template.soy.types.LegacyObjectMapType)3 MapType (com.google.template.soy.types.MapType)3 ArrayList (java.util.ArrayList)3 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)2 SoyNode (com.google.template.soy.soytree.SoyNode)2 UnknownType (com.google.template.soy.types.UnknownType)2 SoyString (com.google.template.soy.data.restricted.SoyString)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 BasicExpressionCompiler (com.google.template.soy.jbcsrc.ExpressionCompiler.BasicExpressionCompiler)1 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)1 MethodRef (com.google.template.soy.jbcsrc.restricted.MethodRef)1 AppendableAndOptions (com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective.Streamable.AppendableAndOptions)1