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