Search in sources :

Example 11 with SoyType

use of com.google.template.soy.types.SoyType in project closure-templates by google.

the class ResolveExpressionTypesVisitor method addTypeSubstitutions.

// Given a map of type subsitutions, add all the entries to the current set of
// active substitutions.
private void addTypeSubstitutions(Map<Wrapper<ExprNode>, SoyType> substitutionsToAdd) {
    for (Map.Entry<Wrapper<ExprNode>, SoyType> entry : substitutionsToAdd.entrySet()) {
        ExprNode expr = entry.getKey().get();
        // Get the existing type
        SoyType previousType = expr.getType();
        for (TypeSubstitution subst = substitutions; subst != null; subst = subst.parent) {
            if (ExprEquivalence.get().equivalent(subst.expression, expr)) {
                previousType = subst.type;
                break;
            }
        }
        // If the new type is different than the current type, then add a new type substitution.
        if (!entry.getValue().equals(previousType)) {
            substitutions = new TypeSubstitution(substitutions, expr, entry.getValue());
        }
    }
}
Also used : AbstractParentExprNode(com.google.template.soy.exprtree.AbstractParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) Wrapper(com.google.common.base.Equivalence.Wrapper) SoyType(com.google.template.soy.types.SoyType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 12 with SoyType

use of com.google.template.soy.types.SoyType in project closure-templates by google.

the class RewriteGlobalsPass method resolveGlobal.

private void resolveGlobal(GlobalNode global) {
    // First check to see if this global matches a proto enum.  We do this because the enums from
    // the type registry have better type information and for applications with legacy globals
    // configs there is often overlap, so the order in which we check is actually important.
    // proto enums are dotted identifiers
    String name = global.getName();
    int lastDot = name.lastIndexOf('.');
    if (lastDot > 0) {
        String enumTypeName = name.substring(0, lastDot);
        SoyType type = typeRegistry.getType(enumTypeName);
        if (type != null && type.getKind() == SoyType.Kind.PROTO_ENUM) {
            SoyProtoEnumType enumType = (SoyProtoEnumType) type;
            String enumMemberName = name.substring(lastDot + 1);
            Integer enumValue = enumType.getValue(enumMemberName);
            if (enumValue != null) {
                // TODO(lukes): consider introducing a new PrimitiveNode for enums
                global.resolve(enumType, new IntegerNode(enumValue, global.getSourceLocation()));
            } else {
                // If we found the type definition but not the value, then that's an error
                // regardless of whether we're allowing unbound globals or not.
                errorReporter.report(global.getSourceLocation(), ENUM_MEMBERSHIP_ERROR, enumMemberName, enumTypeName);
            }
            // TODO(lukes): issue a warning if a registered global also matches
            return;
        }
    }
    // if that doesn't work, see if it was registered in the globals file.
    PrimitiveData value = compileTimeGlobals.get(global.getName());
    if (value != null) {
        PrimitiveNode expr = InternalValueUtils.convertPrimitiveDataToExpr(value, global.getSourceLocation());
        global.resolve(expr.getType(), expr);
    }
}
Also used : PrimitiveNode(com.google.template.soy.exprtree.ExprNode.PrimitiveNode) PrimitiveData(com.google.template.soy.data.restricted.PrimitiveData) IntegerNode(com.google.template.soy.exprtree.IntegerNode) SoyType(com.google.template.soy.types.SoyType) SoyProtoEnumType(com.google.template.soy.types.SoyProtoEnumType)

Example 13 with SoyType

use of com.google.template.soy.types.SoyType in project closure-templates by google.

the class ValidateAliasesPass method run.

@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
    if (file.getSoyFileKind() != SoyFileKind.SRC) {
        return;
    }
    for (AliasDeclaration alias : file.getAliasDeclarations()) {
        if (options.getCompileTimeGlobals().containsKey(alias.alias().identifier())) {
            errorReporter.report(alias.alias().location(), ALIAS_CONFLICTS_WITH_GLOBAL, alias.alias());
        }
        SoyType type = registry.getType(alias.alias().identifier());
        // When running with a dummy type provider that parses all types as unknown, ignore that.
        if (type != null && type.getKind() != SoyType.Kind.UNKNOWN) {
            errorReporter.report(alias.alias().location(), ALIAS_CONFLICTS_WITH_TYPE_NAME, alias.alias());
        }
        String conflictingNamespacedType = registry.findTypeWithMatchingNamespace(alias.alias().identifier());
        if (conflictingNamespacedType != null) {
            errorReporter.report(alias.alias().location(), ALIAS_CONFLICTS_WITH_TYPE_PREFIX, alias.alias(), conflictingNamespacedType);
        }
        String prefix = alias.alias().identifier() + ".";
        for (String global : options.getCompileTimeGlobals().keySet()) {
            if (global.startsWith(prefix)) {
                errorReporter.report(alias.alias().location(), ALIAS_CONFLICTS_WITH_GLOBAL_PREFIX, alias.alias(), global);
            }
        }
    }
}
Also used : SoyType(com.google.template.soy.types.SoyType) AliasDeclaration(com.google.template.soy.soytree.AliasDeclaration)

Example 14 with SoyType

use of com.google.template.soy.types.SoyType in project closure-templates by google.

the class LetContentNode method forVariable.

/**
 * Creates a LetContentNode for a compiler-generated variable. Use this in passes that rewrite the
 * tree and introduce local temporary variables.
 */
// TODO(user): Delete.
public static LetContentNode forVariable(int id, SourceLocation sourceLocation, String varName, @Nullable SanitizedContentKind contentKind) {
    LetContentNode node = new LetContentNode(id, sourceLocation, varName, contentKind);
    SoyType type = (contentKind != null) ? SanitizedType.getTypeForContentKind(contentKind) : StringType.getInstance();
    node.getVar().setType(type);
    return node;
}
Also used : SoyType(com.google.template.soy.types.SoyType)

Example 15 with SoyType

use of com.google.template.soy.types.SoyType in project closure-templates by google.

the class MapKeysFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression soyExpression = args.get(0);
    SoyType argType = soyExpression.soyType();
    SoyType keyType = ((MapType) argType).getKeyType();
    return SoyExpression.forList(keyType == null ? ListType.EMPTY_LIST : ListType.of(keyType), JbcSrcMethods.MAP_KEYS_FN.invoke(soyExpression.box().checkedCast(SoyMap.class)));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) SoyType(com.google.template.soy.types.SoyType) MapType(com.google.template.soy.types.MapType)

Aggregations

SoyType (com.google.template.soy.types.SoyType)26 MapType (com.google.template.soy.types.MapType)5 ExprNode (com.google.template.soy.exprtree.ExprNode)4 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)4 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 LegacyObjectMapType (com.google.template.soy.types.LegacyObjectMapType)4 ListType (com.google.template.soy.types.ListType)4 LinkedHashMap (java.util.LinkedHashMap)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)3 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)3 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)3 SoyProtoEnumType (com.google.template.soy.types.SoyProtoEnumType)3 SoyProtoType (com.google.template.soy.types.SoyProtoType)3 UnionType (com.google.template.soy.types.UnionType)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 FunctionNode (com.google.template.soy.exprtree.FunctionNode)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 RecordType (com.google.template.soy.types.RecordType)2