Do not access TypeNamespace during build
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractTypeStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.math.BigDecimal;
15 import java.util.Collection;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.Empty;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
37 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
42 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
52 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
53 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
54 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
55 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
56 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
57 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
58 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
59 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
60 import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException;
61 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
62 import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
63 import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
64 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
65 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
66 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
67 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
68 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
74 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
75 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
76 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
77 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
78 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
79 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
80 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
81 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84
85 abstract class AbstractTypeStatementSupport
86         extends BaseStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
87     private static final Logger LOG = LoggerFactory.getLogger(AbstractTypeStatementSupport.class);
88
89     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
90         YangStmtMapping.TYPE)
91         .addOptional(YangStmtMapping.BASE)
92         .addAny(YangStmtMapping.BIT)
93         .addAny(YangStmtMapping.ENUM)
94         .addOptional(YangStmtMapping.FRACTION_DIGITS)
95         .addOptional(YangStmtMapping.LENGTH)
96         .addOptional(YangStmtMapping.PATH)
97         .addAny(YangStmtMapping.PATTERN)
98         .addOptional(YangStmtMapping.RANGE)
99         .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
100         .addAny(YangStmtMapping.TYPE)
101         .build();
102
103     static final String BINARY = "binary";
104     static final String BITS = "bits";
105     static final String BOOLEAN = "boolean";
106     static final String DECIMAL64 = "decimal64";
107     static final String EMPTY = "empty";
108     static final String ENUMERATION = "enumeration";
109     static final String IDENTITY_REF = "identityref";
110     static final String INSTANCE_IDENTIFIER = "instance-identifier";
111     static final String INT8 = "int8";
112     static final String INT16 = "int16";
113     static final String INT32 = "int32";
114     static final String INT64 = "int64";
115     static final String LEAF_REF = "leafref";
116     static final String STRING = "string";
117     static final String UINT8 = "uint8";
118     static final String UINT16 = "uint16";
119     static final String UINT32 = "uint32";
120     static final String UINT64 = "uint64";
121     static final String UNION = "union";
122
123     private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
124         .put(BINARY, BINARY)
125         .put(BITS, BITS)
126         .put(BOOLEAN, BOOLEAN)
127         .put(DECIMAL64, DECIMAL64)
128         .put(EMPTY, EMPTY)
129         .put(ENUMERATION, ENUMERATION)
130         .put(IDENTITY_REF,IDENTITY_REF)
131         .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
132         .put(INT8, INT8)
133         .put(INT16, INT16)
134         .put(INT32, INT32)
135         .put(INT64, INT64)
136         .put(LEAF_REF, LEAF_REF)
137         .put(STRING, STRING)
138         .put(UINT8, UINT8)
139         .put(UINT16, UINT16)
140         .put(UINT32, UINT32)
141         .put(UINT64, UINT64)
142         .put(UNION, UNION)
143         .build();
144
145     private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
146             ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
147             .put(BITS, new BitsSpecificationSupport())
148             .put(DECIMAL64, new Decimal64SpecificationSupport())
149             .put(ENUMERATION, new EnumSpecificationSupport())
150             .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
151             .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
152             .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
153             .put(UNION, new UnionSpecificationSupport())
154             .build();
155
156     AbstractTypeStatementSupport() {
157         super(YangStmtMapping.TYPE);
158     }
159
160     @Override
161     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
162         return value;
163     }
164
165     @Override
166     public final void onFullDefinitionDeclared(
167             final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
168         super.onFullDefinitionDeclared(stmt);
169
170         // if it is yang built-in type, no prerequisite is needed, so simply return
171         if (BUILT_IN_TYPES.containsKey(stmt.argument())) {
172             // FIXME: consider populating BaseTypeNamespace here, which could be done quite efficiently, moving the
173             //        logic from resolveType()
174             return;
175         }
176
177         final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.argument());
178         final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
179         final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
180                 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
181         typeAction.mutatesEffectiveCtx(stmt.getParentContext());
182
183         /*
184          * If the type does not exist, throw an InferenceException.
185          * If the type exists, store a reference to it in BaseTypeNamespace.
186          */
187         typeAction.apply(new InferenceAction() {
188             @Override
189             public void apply(final InferenceContext ctx) {
190                 stmt.addToNs(BaseTypeNamespace.class, Empty.getInstance(), typePrereq.resolve(ctx));
191             }
192
193             @Override
194             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
195                 InferenceException.throwIf(failed.contains(typePrereq), stmt, "Type [%s] was not found.", typeQName);
196             }
197         });
198     }
199
200     @Override
201     public final String internArgument(final String rawArgument) {
202         final String found;
203         return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
204     }
205
206     @Override
207     public boolean hasArgumentSpecificSupports() {
208         return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
209     }
210
211     @Override
212     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
213         return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
214     }
215
216     @Override
217     protected final SubstatementValidator getSubstatementValidator() {
218         return SUBSTATEMENT_VALIDATOR;
219     }
220
221     @Override
222     protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
223             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
224         return new RegularTypeStatement(ctx.getRawArgument(), substatements);
225     }
226
227     @Override
228     protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
229         final TypeStatement builtin;
230         return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin
231             : new EmptyTypeStatement(ctx.getRawArgument());
232     }
233
234     @Override
235     protected EffectiveStatement<String, TypeStatement> createEffective(final Current<String, TypeStatement> stmt,
236             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
237         // First look up the proper base type
238         final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(stmt);
239         if (substatements.isEmpty()) {
240             return typeStmt;
241         }
242
243         // Now instantiate the proper effective statement for that type
244         final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
245         final TypeStatement declared = stmt.declared();
246         if (baseType instanceof BinaryTypeDefinition) {
247             return createBinary(stmt, (BinaryTypeDefinition) baseType, declared, substatements);
248         } else if (baseType instanceof BitsTypeDefinition) {
249             return createBits(stmt, (BitsTypeDefinition) baseType, declared, substatements);
250         } else if (baseType instanceof BooleanTypeDefinition) {
251             return createBoolean(stmt, (BooleanTypeDefinition) baseType, declared, substatements);
252         } else if (baseType instanceof DecimalTypeDefinition) {
253             return createDecimal(stmt, (DecimalTypeDefinition) baseType, declared, substatements);
254         } else if (baseType instanceof EmptyTypeDefinition) {
255             return createEmpty(stmt, (EmptyTypeDefinition) baseType, declared, substatements);
256         } else if (baseType instanceof EnumTypeDefinition) {
257             return createEnum(stmt, (EnumTypeDefinition) baseType, declared, substatements);
258         } else if (baseType instanceof IdentityrefTypeDefinition) {
259             return createIdentityref(stmt, (IdentityrefTypeDefinition) baseType, declared, substatements);
260         } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
261             return createInstanceIdentifier(stmt, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
262         } else if (baseType instanceof Int8TypeDefinition) {
263             return createIntegral(stmt, declared, substatements,
264                 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
265         } else if (baseType instanceof Int16TypeDefinition) {
266             return createIntegral(stmt, declared, substatements,
267                 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
268         } else if (baseType instanceof Int32TypeDefinition) {
269             return createIntegral(stmt, declared, substatements,
270                 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
271         } else if (baseType instanceof Int64TypeDefinition) {
272             return createIntegral(stmt, declared, substatements,
273                 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
274         } else if (baseType instanceof LeafrefTypeDefinition) {
275             return createLeafref(stmt, (LeafrefTypeDefinition) baseType, declared, substatements);
276         } else if (baseType instanceof StringTypeDefinition) {
277             return createString(stmt, (StringTypeDefinition) baseType, declared, substatements);
278         } else if (baseType instanceof Uint8TypeDefinition) {
279             return createIntegral(stmt, declared, substatements,
280                 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
281         } else if (baseType instanceof Uint16TypeDefinition) {
282             return createIntegral(stmt, declared, substatements,
283                 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
284         } else if (baseType instanceof Uint32TypeDefinition) {
285             return createIntegral(stmt, declared, substatements,
286                 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
287         } else if (baseType instanceof Uint64TypeDefinition) {
288             return createIntegral(stmt, declared, substatements,
289                 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
290         } else if (baseType instanceof UnionTypeDefinition) {
291             return createUnion(stmt, (UnionTypeDefinition) baseType, declared, substatements);
292         } else {
293             throw new IllegalStateException("Unhandled base type " + baseType);
294         }
295     }
296
297     static final SchemaPath typeEffectiveSchemaPath(final Current<?, ?> stmt) {
298         final SchemaPath path = stmt.getSchemaPath();
299         final SchemaPath parent = path.getParent();
300         final QName parentQName = parent.getLastComponent();
301         checkArgument(parentQName != null, "Path %s has an empty parent", path);
302
303         final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
304         return parent.createChild(qname);
305     }
306
307     /**
308      * Resolve type reference, as pointed to by the context's argument.
309      *
310      * @param ctx Statement context
311      * @return Resolved type
312      * @throws SourceException if the target type cannot be found
313      */
314     private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final Current<String, ?> ctx) {
315         final StmtContext<?, ?, ?> baseType = ctx.namespaceItem(BaseTypeNamespace.class, Empty.getInstance());
316         if (baseType != null) {
317             return ((TypedefEffectiveStatement) baseType.buildEffective()).asTypeEffectiveStatement();
318         }
319
320         final String argument = ctx.getArgument();
321         switch (argument) {
322             case BINARY:
323                 return BuiltinEffectiveStatement.BINARY;
324             case BOOLEAN:
325                 return BuiltinEffectiveStatement.BOOLEAN;
326             case EMPTY:
327                 return BuiltinEffectiveStatement.EMPTY;
328             case INSTANCE_IDENTIFIER:
329                 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
330             case INT8:
331                 return BuiltinEffectiveStatement.INT8;
332             case INT16:
333                 return BuiltinEffectiveStatement.INT16;
334             case INT32:
335                 return BuiltinEffectiveStatement.INT32;
336             case INT64:
337                 return BuiltinEffectiveStatement.INT64;
338             case STRING:
339                 return BuiltinEffectiveStatement.STRING;
340             case UINT8:
341                 return BuiltinEffectiveStatement.UINT8;
342             case UINT16:
343                 return BuiltinEffectiveStatement.UINT16;
344             case UINT32:
345                 return BuiltinEffectiveStatement.UINT32;
346             case UINT64:
347                 return BuiltinEffectiveStatement.UINT64;
348             default:
349                 throw new IllegalStateException("Unhandled type argument " + argument);
350         }
351     }
352
353     private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final Current<?, ?> ctx,
354             final BinaryTypeDefinition baseType, final TypeStatement declared,
355             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
356         final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
357                 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
358
359         for (EffectiveStatement<?, ?> stmt : substatements) {
360             if (stmt instanceof LengthEffectiveStatement) {
361                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
362
363                 try {
364                     builder.setLengthConstraint(length, length.argument());
365                 } catch (IllegalStateException e) {
366                     throw new SourceException(ctx, e, "Multiple length constraints encountered");
367                 } catch (InvalidLengthConstraintException e) {
368                     throw new SourceException(ctx, e, "Invalid length constraint %s", length.argument());
369                 }
370             }
371         }
372
373         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
374     }
375
376     private @NonNull TypeEffectiveStatement<TypeStatement> createBits(final Current<?, ?> ctx,
377             final BitsTypeDefinition baseType, final TypeStatement declared,
378             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
379         final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath());
380
381         for (final EffectiveStatement<?, ?> stmt : substatements) {
382             if (stmt instanceof BitEffectiveStatement) {
383                 builder.addBit(addRestrictedBit(ctx, baseType, (BitEffectiveStatement) stmt));
384             }
385         }
386
387         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
388     }
389
390     abstract @NonNull Bit addRestrictedBit(@NonNull EffectiveStmtCtx stmt, @NonNull BitsTypeDefinition base,
391         @NonNull BitEffectiveStatement bit);
392
393     private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final Current<?, ?> ctx,
394             final BooleanTypeDefinition baseType, final TypeStatement declared,
395             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
396         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
397             typeEffectiveSchemaPath(ctx)));
398     }
399
400     private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final Current<?, ?> ctx,
401             final DecimalTypeDefinition baseType, final TypeStatement declared,
402             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
403         final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
404                 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
405
406         for (EffectiveStatement<?, ?> stmt : substatements) {
407             if (stmt instanceof RangeEffectiveStatement) {
408                 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
409                 builder.setRangeConstraint(range, range.argument());
410             }
411             if (stmt instanceof FractionDigitsEffectiveStatement) {
412                 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
413                 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx,
414                     "Cannot override fraction-digits from base type %s to %s", baseType, digits);
415             }
416         }
417
418         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
419     }
420
421     private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final Current<?, ?> ctx,
422             final EmptyTypeDefinition baseType, final TypeStatement declared,
423             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
424         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
425             typeEffectiveSchemaPath(ctx)));
426     }
427
428     private @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final Current<?, ?> ctx,
429             final EnumTypeDefinition baseType, final TypeStatement declared,
430             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
431         final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType, ctx.getSchemaPath());
432
433         for (final EffectiveStatement<?, ?> stmt : substatements) {
434             if (stmt instanceof EnumEffectiveStatement) {
435                 builder.addEnum(addRestrictedEnum(ctx, baseType, (EnumEffectiveStatement) stmt));
436             }
437         }
438
439         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
440     }
441
442     abstract @NonNull EnumPair addRestrictedEnum(@NonNull EffectiveStmtCtx stmt, @NonNull EnumTypeDefinition base,
443         @NonNull EnumEffectiveStatement enumStmt);
444
445     private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final Current<?, ?> ctx,
446             final IdentityrefTypeDefinition baseType, final TypeStatement declared,
447             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
448         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
449             typeEffectiveSchemaPath(ctx)));
450     }
451
452     private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
453             final Current<?, ?> ctx, final InstanceIdentifierTypeDefinition baseType, final TypeStatement declared,
454             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
455         final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
456                     typeEffectiveSchemaPath(ctx));
457
458         for (EffectiveStatement<?, ?> stmt : substatements) {
459             if (stmt instanceof RequireInstanceEffectiveStatement) {
460                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
461             }
462         }
463
464         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
465     }
466
467     private static <T extends RangeRestrictedTypeDefinition<T, N>, N extends Number & Comparable<N>>
468         @NonNull TypeEffectiveStatement<TypeStatement> createIntegral(final Current<?, ?> ctx,
469                 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
470                 final RangeRestrictedTypeBuilder<T, N> builder) {
471         for (EffectiveStatement<?, ?> stmt : substatements) {
472             if (stmt instanceof RangeEffectiveStatement) {
473                 final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt;
474                 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
475             }
476         }
477
478         try {
479             return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
480         } catch (InvalidRangeConstraintException e) {
481             throw new SourceException(ctx, e, "Invalid range constraint: %s", e.getOffendingRanges());
482         }
483     }
484
485     private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final Current<?, ?> ctx,
486             final LeafrefTypeDefinition baseType, final TypeStatement declared,
487             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
488         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
489                 RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
490
491         for (final EffectiveStatement<?, ?> stmt : substatements) {
492             if (stmt instanceof RequireInstanceEffectiveStatement) {
493                 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
494             }
495         }
496         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
497     }
498
499     private static @NonNull TypeEffectiveStatement<TypeStatement> createString(final Current<?, ?> ctx,
500             final StringTypeDefinition baseType, final TypeStatement declared,
501             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
502         final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
503             AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
504
505         for (EffectiveStatement<?, ?> stmt : substatements) {
506             if (stmt instanceof LengthEffectiveStatement) {
507                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
508
509                 try {
510                     builder.setLengthConstraint(length, length.argument());
511                 } catch (IllegalStateException e) {
512                     throw new SourceException(ctx, e, "Multiple length constraints encountered");
513                 } catch (InvalidLengthConstraintException e) {
514                     throw new SourceException(ctx, e, "Invalid length constraint %s", length.argument());
515                 }
516             }
517             if (stmt instanceof PatternEffectiveStatement) {
518                 builder.addPatternConstraint((PatternEffectiveStatement) stmt);
519             }
520         }
521
522         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
523     }
524
525     private static @NonNull TypeEffectiveStatement<TypeStatement> createUnion(final Current<?, ?> ctx,
526             final UnionTypeDefinition baseType, final TypeStatement declared,
527             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
528         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newUnionBuilder(baseType,
529             typeEffectiveSchemaPath(ctx)));
530     }
531 }