Convert type checking in AbstractTypeStatementSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractTypeStatementSupport.java
index 2330fedf670f9a5b13f051e1c24b15ae1d0c5ed7..e463a450b83426355887bf94a652c3b2fe997628 100644 (file)
@@ -9,24 +9,36 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import java.math.BigDecimal;
 import java.util.Collection;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
@@ -34,15 +46,27 @@ import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
+import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException;
+import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
+import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
@@ -55,9 +79,13 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 abstract class AbstractTypeStatementSupport
-        extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
+        extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeStatementSupport.class);
+
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.TYPE)
         .addOptional(YangStmtMapping.BASE)
@@ -134,76 +162,6 @@ abstract class AbstractTypeStatementSupport
         return value;
     }
 
-    @Override
-    public final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
-        return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
-    }
-
-    @Override
-    public final TypeEffectiveStatement<TypeStatement> createEffective(
-            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
-
-        // First look up the proper base type
-        final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(ctx);
-
-        if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
-            return typeStmt;
-        }
-
-        // Now instantiate the proper effective statement for that type
-        final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
-        if (baseType instanceof BinaryTypeDefinition) {
-            return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
-        } else if (baseType instanceof BitsTypeDefinition) {
-            return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
-        } else if (baseType instanceof BooleanTypeDefinition) {
-            return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
-        } else if (baseType instanceof DecimalTypeDefinition) {
-            return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
-        } else if (baseType instanceof EmptyTypeDefinition) {
-            return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
-        } else if (baseType instanceof EnumTypeDefinition) {
-            return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
-        } else if (baseType instanceof IdentityrefTypeDefinition) {
-            return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
-        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
-            return new InstanceIdentifierTypeEffectiveStatementImpl(ctx,
-                (InstanceIdentifierTypeDefinition) baseType);
-        } else if (baseType instanceof Int8TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof Int16TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof Int32TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof Int64TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof LeafrefTypeDefinition) {
-            return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
-        } else if (baseType instanceof StringTypeDefinition) {
-            return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
-        } else if (baseType instanceof Uint8TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof Uint16TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof Uint32TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof Uint64TypeDefinition) {
-            return new IntegralTypeEffectiveStatementImpl<>(ctx,
-                    RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
-        } else if (baseType instanceof UnionTypeDefinition) {
-            return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
-        } else {
-            throw new IllegalStateException("Unhandled base type " + baseType);
-        }
-    }
-
     @Override
     public final void onFullDefinitionDeclared(
             final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
@@ -238,11 +196,6 @@ abstract class AbstractTypeStatementSupport
         });
     }
 
-    @Override
-    protected final SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
-    }
-
     @Override
     public final String internArgument(final String rawArgument) {
         final String found;
@@ -259,8 +212,89 @@ abstract class AbstractTypeStatementSupport
         return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
     }
 
-    static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
-        final SchemaPath path = stmtCtx.getSchemaPath().get();
+    @Override
+    protected final SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
+    }
+
+    @Override
+    protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularTypeStatement(ctx.coerceRawStatementArgument(), substatements);
+    }
+
+    @Override
+    protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
+        final TypeStatement builtin;
+        return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin
+            : new EmptyTypeStatement(ctx.coerceRawStatementArgument());
+    }
+
+    @Override
+    protected EffectiveStatement<String, TypeStatement> createEffective(final Current<String, TypeStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        // First look up the proper base type
+        final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(stmt);
+        if (substatements.isEmpty()) {
+            return typeStmt;
+        }
+
+        // Now instantiate the proper effective statement for that type
+        final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
+        final TypeStatement declared = stmt.declared();
+        if (baseType instanceof BinaryTypeDefinition) {
+            return createBinary(stmt, (BinaryTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof BitsTypeDefinition) {
+            return createBits(stmt, (BitsTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof BooleanTypeDefinition) {
+            return createBoolean(stmt, (BooleanTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof DecimalTypeDefinition) {
+            return createDecimal(stmt, (DecimalTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof EmptyTypeDefinition) {
+            return createEmpty(stmt, (EmptyTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof EnumTypeDefinition) {
+            return createEnum(stmt, (EnumTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof IdentityrefTypeDefinition) {
+            return createIdentityref(stmt, (IdentityrefTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
+            return createInstanceIdentifier(stmt, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof Int8TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof Int16TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof Int32TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof Int64TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof LeafrefTypeDefinition) {
+            return createLeafref(stmt, (LeafrefTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof StringTypeDefinition) {
+            return createString(stmt, (StringTypeDefinition) baseType, declared, substatements);
+        } else if (baseType instanceof Uint8TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof Uint16TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof Uint32TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof Uint64TypeDefinition) {
+            return createIntegral(stmt, declared, substatements,
+                RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
+        } else if (baseType instanceof UnionTypeDefinition) {
+            return createUnion(stmt, (UnionTypeDefinition) baseType, declared, substatements);
+        } else {
+            throw new IllegalStateException("Unhandled base type " + baseType);
+        }
+    }
+
+    static final SchemaPath typeEffectiveSchemaPath(final Current<?, ?> stmt) {
+        final SchemaPath path = stmt.getSchemaPath();
         final SchemaPath parent = path.getParent();
         final QName parentQName = parent.getLastComponent();
         checkArgument(parentQName != null, "Path %s has an empty parent", path);
@@ -276,8 +310,8 @@ abstract class AbstractTypeStatementSupport
      * @return Resolved type
      * @throws SourceException if the target type cannot be found
      */
-    private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final StmtContext<String, ?, ?> ctx) {
-        final String argument = ctx.coerceStatementArgument();
+    private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final Current<String, ?> ctx) {
+        final String argument = ctx.coerceArgument();
         switch (argument) {
             case BINARY:
                 return BuiltinEffectiveStatement.BINARY;
@@ -306,11 +340,193 @@ abstract class AbstractTypeStatementSupport
             case UINT64:
                 return BuiltinEffectiveStatement.UINT64;
             default:
-                final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, argument);
+                final QName qname = StmtContextUtils.parseNodeIdentifier(ctx.caerbannog(), argument);
                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
                         SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
-                            ctx.getStatementSourceReference(), "Type '%s' not found", qname);
+                            ctx.sourceReference(), "Type '%s' not found", qname);
                 return typedef.buildEffective().asTypeEffectiveStatement();
         }
     }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final Current<?, ?> ctx,
+            final BinaryTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
+                RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
+
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof LengthEffectiveStatement) {
+                final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
+
+                try {
+                    builder.setLengthConstraint(length, length.argument());
+                } catch (IllegalStateException e) {
+                    throw new SourceException(ctx.sourceReference(), e, "Multiple length constraints encountered");
+                } catch (InvalidLengthConstraintException e) {
+                    throw new SourceException(ctx.sourceReference(), e, "Invalid length constraint %s",
+                        length.argument());
+                }
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    private @NonNull TypeEffectiveStatement<TypeStatement> createBits(final Current<?, ?> ctx,
+            final BitsTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath());
+
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof BitEffectiveStatement) {
+                builder.addBit(addRestrictedBit(ctx, baseType, (BitEffectiveStatement) stmt));
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    abstract @NonNull Bit addRestrictedBit(@NonNull EffectiveStmtCtx stmt, @NonNull BitsTypeDefinition base,
+        @NonNull BitEffectiveStatement bit);
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final Current<?, ?> ctx,
+            final BooleanTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
+            typeEffectiveSchemaPath(ctx)));
+    }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final Current<?, ?> ctx,
+            final DecimalTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
+                RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
+
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof RangeEffectiveStatement) {
+                final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
+                builder.setRangeConstraint(range, range.argument());
+            }
+            if (stmt instanceof FractionDigitsEffectiveStatement) {
+                final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
+                SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.sourceReference(),
+                    "Cannot override fraction-digits from base type %s to %s", baseType, digits);
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final Current<?, ?> ctx,
+            final EmptyTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
+            typeEffectiveSchemaPath(ctx)));
+    }
+
+    private @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final Current<?, ?> ctx,
+            final EnumTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType, ctx.getSchemaPath());
+
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof EnumEffectiveStatement) {
+                builder.addEnum(addRestrictedEnum(ctx, baseType, (EnumEffectiveStatement) stmt));
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    abstract @NonNull EnumPair addRestrictedEnum(@NonNull EffectiveStmtCtx stmt, @NonNull EnumTypeDefinition base,
+        @NonNull EnumEffectiveStatement enumStmt);
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final Current<?, ?> ctx,
+            final IdentityrefTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
+            typeEffectiveSchemaPath(ctx)));
+    }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
+            final Current<?, ?> ctx, final InstanceIdentifierTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
+                    typeEffectiveSchemaPath(ctx));
+
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof RequireInstanceEffectiveStatement) {
+                builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    private static <T extends RangeRestrictedTypeDefinition<T, N>, N extends Number & Comparable<N>>
+        @NonNull TypeEffectiveStatement<TypeStatement> createIntegral(final Current<?, ?> ctx,
+                final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
+                final RangeRestrictedTypeBuilder<T, N> builder) {
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof RangeEffectiveStatement) {
+                final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt;
+                builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
+            }
+        }
+
+        try {
+            return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+        } catch (InvalidRangeConstraintException e) {
+            throw new SourceException(ctx.sourceReference(), e, "Invalid range constraint: %s",
+                e.getOffendingRanges());
+        }
+    }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final Current<?, ?> ctx,
+            final LeafrefTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
+                RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
+
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof RequireInstanceEffectiveStatement) {
+                builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
+            }
+        }
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createString(final Current<?, ?> ctx,
+            final StringTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
+            AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
+
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof LengthEffectiveStatement) {
+                final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
+
+                try {
+                    builder.setLengthConstraint(length, length.argument());
+                } catch (IllegalStateException e) {
+                    throw new SourceException(ctx.sourceReference(), e, "Multiple length constraints encountered");
+                } catch (InvalidLengthConstraintException e) {
+                    throw new SourceException(ctx.sourceReference(), e, "Invalid length constraint %s",
+                        length.argument());
+                }
+            }
+            if (stmt instanceof PatternEffectiveStatement) {
+                builder.addPatternConstraint((PatternEffectiveStatement) stmt);
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final Current<?, ?> ctx,
+            final UnionTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
+            typeEffectiveSchemaPath(ctx)));
+    }
 }
\ No newline at end of file