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);
}
}
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);
}
}
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;
}
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);
}
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();
}
Aggregations