Search in sources :

Example 1 with Expression

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

the class MsgCompiler method partToPartExpression.

/**
 * Returns an {@link Expression} that evaluates to an equivalent SoyMsgPart as the argument.
 */
private Expression partToPartExpression(SoyMsgPart part) {
    if (part instanceof SoyMsgPlaceholderPart) {
        return SOY_MSG_PLACEHOLDER_PART.construct(constant(((SoyMsgPlaceholderPart) part).getPlaceholderName()), constantNull(STRING_TYPE));
    } else if (part instanceof SoyMsgPluralPart) {
        SoyMsgPluralPart pluralPart = (SoyMsgPluralPart) part;
        List<Expression> caseExprs = new ArrayList<>(pluralPart.getCases().size());
        for (Case<SoyMsgPluralCaseSpec> item : pluralPart.getCases()) {
            Expression spec;
            if (item.spec().getType() == Type.EXPLICIT) {
                spec = SOY_MSG_PLURAL_CASE_SPEC_LONG.construct(constant(item.spec().getExplicitValue()));
            } else {
                spec = SOY_MSG_PLURAL_CASE_SPEC_TYPE.construct(FieldRef.enumReference(item.spec().getType()).accessor());
            }
            caseExprs.add(CASE_CREATE.invoke(spec, partsToPartsList(item.parts())));
        }
        return SOY_MSG_PURAL_PART.construct(constant(pluralPart.getPluralVarName()), constant(pluralPart.getOffset()), BytecodeUtils.asList(caseExprs));
    } else if (part instanceof SoyMsgPluralRemainderPart) {
        return SOY_MSG_PLURAL_REMAINDER_PART.construct(constant(((SoyMsgPluralRemainderPart) part).getPluralVarName()));
    } else if (part instanceof SoyMsgRawTextPart) {
        return SOY_MSG_RAW_TEXT_PART_OF.invoke(constant(((SoyMsgRawTextPart) part).getRawText(), variables));
    } else if (part instanceof SoyMsgSelectPart) {
        SoyMsgSelectPart selectPart = (SoyMsgSelectPart) part;
        List<Expression> caseExprs = new ArrayList<>(selectPart.getCases().size());
        for (Case<String> item : selectPart.getCases()) {
            caseExprs.add(CASE_CREATE.invoke(item.spec() == null ? constantNull(STRING_TYPE) : constant(item.spec()), partsToPartsList(item.parts())));
        }
        return SOY_MSG_SELECT_PART.construct(constant(selectPart.getSelectVarName()), BytecodeUtils.asList(caseExprs));
    } else {
        throw new AssertionError("unrecognized part: " + part);
    }
}
Also used : SoyMsgPluralRemainderPart(com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) ArrayList(java.util.ArrayList) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) Case(com.google.template.soy.msgs.restricted.SoyMsgPart.Case) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 2 with Expression

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

the class MsgCompiler method putPluralPartIntoMap.

private void putPluralPartIntoMap(Expression mapExpression, MsgNode originalMsg, Map<String, Statement> placeholderNameToPutStatement, SoyMsgPluralPart plural) {
    MsgPluralNode repPluralNode = originalMsg.getRepPluralNode(plural.getPluralVarName());
    if (!placeholderNameToPutStatement.containsKey(plural.getPluralVarName())) {
        Label reattachPoint = new Label();
        Expression value = soyNodeCompiler.compileToInt(repPluralNode.getExpr(), reattachPoint);
        placeholderNameToPutStatement.put(plural.getPluralVarName(), putToMap(mapExpression, plural.getPluralVarName(), value).labelStart(reattachPoint).withSourceLocation(repPluralNode.getSourceLocation()));
    }
    // Recursively visit plural cases
    for (Case<SoyMsgPluralCaseSpec> caseOrDefault : plural.getCases()) {
        putPlaceholdersIntoMap(mapExpression, originalMsg, caseOrDefault.parts(), placeholderNameToPutStatement);
    }
}
Also used : SoyMsgPluralCaseSpec(com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) Label(org.objectweb.asm.Label) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode)

Example 3 with Expression

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

the class LazyClosureCompiler method compileLazyExpression.

Expression compileLazyExpression(String namePrefix, SoyNode declaringNode, String varName, ExprNode exprNode) {
    Optional<Expression> asSoyValueProvider = expressionToSoyValueProviderCompiler.compileAvoidingDetaches(exprNode);
    if (asSoyValueProvider.isPresent()) {
        return asSoyValueProvider.get();
    }
    TypeInfo type = innerClasses.registerInnerClassWithGeneratedName(getProposedName(namePrefix, varName), LAZY_CLOSURE_ACCESS);
    SoyClassWriter writer = SoyClassWriter.builder(type).setAccess(LAZY_CLOSURE_ACCESS).extending(DETACHABLE_VALUE_PROVIDER_TYPE).sourceFileName(declaringNode.getSourceLocation().getFileName()).build();
    Expression expr = new CompilationUnit(writer, type, DETACHABLE_VALUE_PROVIDER_TYPE, declaringNode).compileExpression(exprNode);
    innerClasses.registerAsInnerClass(writer, type);
    writer.visitEnd();
    innerClasses.add(writer.toClassData());
    return expr;
}
Also used : Statement.returnExpression(com.google.template.soy.jbcsrc.restricted.Statement.returnExpression) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyClassWriter(com.google.template.soy.jbcsrc.internal.SoyClassWriter) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 4 with Expression

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

the class SoyNodeCompiler method visitCallBasicNode.

@Override
protected Statement visitCallBasicNode(CallBasicNode node) {
    // Basic nodes are basic! We can just call the node directly.
    CompiledTemplateMetadata callee = registry.getTemplateInfoByTemplateName(node.getCalleeName());
    Label reattachPoint = new Label();
    Expression calleeExpression = callee.constructor().construct(prepareParamsHelper(node, reattachPoint), parameterLookup.getIjRecord());
    return renderCallNode(reattachPoint, node, calleeExpression);
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) Label(org.objectweb.asm.Label)

Example 5 with Expression

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

the class SoyNodeCompiler method visitLoggingFunction.

private Statement visitLoggingFunction(PrintNode node, FunctionNode fn, LoggingFunction loggingFunction) {
    List<Expression> printDirectives = new ArrayList<>(node.numChildren());
    for (PrintDirectiveNode child : node.getChildren()) {
        // sanity
        checkState(child.getArgs().isEmpty());
        printDirectives.add(parameterLookup.getRenderContext().getEscapingDirectiveAsFunction(child.getName()));
    }
    Label reattachPoint = new Label();
    return appendableExpression.appendLoggingFunctionInvocation(loggingFunction.getName(), loggingFunction.getPlaceholder(), exprCompiler.asBasicCompiler(reattachPoint).compileToList(fn.getChildren()), printDirectives).labelStart(reattachPoint).toStatement();
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label)

Aggregations

Expression (com.google.template.soy.jbcsrc.restricted.Expression)32 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)26 Statement (com.google.template.soy.jbcsrc.restricted.Statement)15 Label (org.objectweb.asm.Label)15 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)8 ArrayList (java.util.ArrayList)8 Statement.returnExpression (com.google.template.soy.jbcsrc.restricted.Statement.returnExpression)5 Scope (com.google.template.soy.jbcsrc.TemplateVariableManager.Scope)4 Variable (com.google.template.soy.jbcsrc.TemplateVariableManager.Variable)4 LocalVariable (com.google.template.soy.jbcsrc.restricted.LocalVariable)4 AppendableAndOptions (com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective.Streamable.AppendableAndOptions)4 ImmutableList (com.google.common.collect.ImmutableList)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 SoyClassWriter (com.google.template.soy.jbcsrc.internal.SoyClassWriter)2 FieldRef (com.google.template.soy.jbcsrc.restricted.FieldRef)2 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)2 RangeArgs (com.google.template.soy.shared.RangeArgs)2 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)2 ForNonemptyNode (com.google.template.soy.soytree.ForNonemptyNode)2 LinkedHashMap (java.util.LinkedHashMap)2