Migrate rawStatementArgument()/getStatementSourceReference() callers
[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.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.FractionDigitsEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
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.getStatementArgument())) {
172             return;
173         }
174
175         final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
176         final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
177         final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
178                 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
179         typeAction.mutatesEffectiveCtx(stmt.getParentContext());
180
181         /*
182          * If the type does not exist, throw new InferenceException.
183          * Otherwise perform no operation.
184          */
185         typeAction.apply(new InferenceAction() {
186             @Override
187             public void apply(final InferenceContext ctx) {
188                 // Intentional NOOP
189             }
190
191             @Override
192             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
193                 InferenceException.throwIf(failed.contains(typePrereq), stmt.sourceReference(),
194                     "Type [%s] was not found.", typeQName);
195             }
196         });
197     }
198
199     @Override
200     public final String internArgument(final String rawArgument) {
201         final String found;
202         return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
203     }
204
205     @Override
206     public boolean hasArgumentSpecificSupports() {
207         return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
208     }
209
210     @Override
211     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
212         return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
213     }
214
215     @Override
216     protected final SubstatementValidator getSubstatementValidator() {
217         return SUBSTATEMENT_VALIDATOR;
218     }
219
220     @Override
221     protected final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx,
222             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
223         return new RegularTypeStatement(ctx.coerceRawStatementArgument(), substatements);
224     }
225
226     @Override
227     protected final TypeStatement createEmptyDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
228         final TypeStatement builtin;
229         return (builtin = BuiltinTypeStatement.lookup(ctx)) != null ? builtin
230             : new EmptyTypeStatement(ctx.coerceRawStatementArgument());
231     }
232
233     @Override
234     protected EffectiveStatement<String, TypeStatement> createEffective(final Current<String, TypeStatement> stmt,
235             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
236         // First look up the proper base type
237         final TypeEffectiveStatement<TypeStatement> typeStmt = resolveType(stmt);
238         if (substatements.isEmpty()) {
239             return typeStmt;
240         }
241
242         // Now instantiate the proper effective statement for that type
243         final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
244         final TypeStatement declared = stmt.declared();
245         if (baseType instanceof BinaryTypeDefinition) {
246             return createBinary(stmt, (BinaryTypeDefinition) baseType, declared, substatements);
247         } else if (baseType instanceof BitsTypeDefinition) {
248             return createBits(stmt, (BitsTypeDefinition) baseType, declared, substatements);
249         } else if (baseType instanceof BooleanTypeDefinition) {
250             return createBoolean(stmt, (BooleanTypeDefinition) baseType, declared, substatements);
251         } else if (baseType instanceof DecimalTypeDefinition) {
252             return createDecimal(stmt, (DecimalTypeDefinition) baseType, declared, substatements);
253         } else if (baseType instanceof EmptyTypeDefinition) {
254             return createEmpty(stmt, (EmptyTypeDefinition) baseType, declared, substatements);
255         } else if (baseType instanceof EnumTypeDefinition) {
256             return createEnum(stmt, (EnumTypeDefinition) baseType, declared, substatements);
257         } else if (baseType instanceof IdentityrefTypeDefinition) {
258             return createIdentityref(stmt, (IdentityrefTypeDefinition) baseType, declared, substatements);
259         } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
260             return createInstanceIdentifier(stmt, (InstanceIdentifierTypeDefinition) baseType, declared, substatements);
261         } else if (baseType instanceof Int8TypeDefinition) {
262             return createIntegral(stmt, declared, substatements,
263                 RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
264         } else if (baseType instanceof Int16TypeDefinition) {
265             return createIntegral(stmt, declared, substatements,
266                 RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
267         } else if (baseType instanceof Int32TypeDefinition) {
268             return createIntegral(stmt, declared, substatements,
269                 RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
270         } else if (baseType instanceof Int64TypeDefinition) {
271             return createIntegral(stmt, declared, substatements,
272                 RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
273         } else if (baseType instanceof LeafrefTypeDefinition) {
274             return createLeafref(stmt, (LeafrefTypeDefinition) baseType, declared, substatements);
275         } else if (baseType instanceof StringTypeDefinition) {
276             return createString(stmt, (StringTypeDefinition) baseType, declared, substatements);
277         } else if (baseType instanceof Uint8TypeDefinition) {
278             return createIntegral(stmt, declared, substatements,
279                 RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
280         } else if (baseType instanceof Uint16TypeDefinition) {
281             return createIntegral(stmt, declared, substatements,
282                 RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
283         } else if (baseType instanceof Uint32TypeDefinition) {
284             return createIntegral(stmt, declared, substatements,
285                 RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
286         } else if (baseType instanceof Uint64TypeDefinition) {
287             return createIntegral(stmt, declared, substatements,
288                 RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(stmt)));
289         } else if (baseType instanceof UnionTypeDefinition) {
290             return createUnion(stmt, (UnionTypeDefinition) baseType, declared, substatements);
291         } else {
292             throw new IllegalStateException("Unhandled base type " + baseType);
293         }
294     }
295
296     static final SchemaPath typeEffectiveSchemaPath(final Current<?, ?> stmt) {
297         final SchemaPath path = stmt.getSchemaPath();
298         final SchemaPath parent = path.getParent();
299         final QName parentQName = parent.getLastComponent();
300         checkArgument(parentQName != null, "Path %s has an empty parent", path);
301
302         final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
303         return parent.createChild(qname);
304     }
305
306     /**
307      * Resolve type reference, as pointed to by the context's argument.
308      *
309      * @param ctx Statement context
310      * @return Resolved type
311      * @throws SourceException if the target type cannot be found
312      */
313     private static @NonNull TypeEffectiveStatement<TypeStatement> resolveType(final Current<String, ?> ctx) {
314         final String argument = ctx.getArgument();
315         switch (argument) {
316             case BINARY:
317                 return BuiltinEffectiveStatement.BINARY;
318             case BOOLEAN:
319                 return BuiltinEffectiveStatement.BOOLEAN;
320             case EMPTY:
321                 return BuiltinEffectiveStatement.EMPTY;
322             case INSTANCE_IDENTIFIER:
323                 return BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
324             case INT8:
325                 return BuiltinEffectiveStatement.INT8;
326             case INT16:
327                 return BuiltinEffectiveStatement.INT16;
328             case INT32:
329                 return BuiltinEffectiveStatement.INT32;
330             case INT64:
331                 return BuiltinEffectiveStatement.INT64;
332             case STRING:
333                 return BuiltinEffectiveStatement.STRING;
334             case UINT8:
335                 return BuiltinEffectiveStatement.UINT8;
336             case UINT16:
337                 return BuiltinEffectiveStatement.UINT16;
338             case UINT32:
339                 return BuiltinEffectiveStatement.UINT32;
340             case UINT64:
341                 return BuiltinEffectiveStatement.UINT64;
342             default:
343                 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx.caerbannog(), argument);
344                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef = SourceException.throwIfNull(
345                     ctx.getFromNamespace(TypeNamespace.class, qname), ctx.sourceReference(),
346                     "Type '%s' not found", qname);
347                 return typedef.buildEffective().asTypeEffectiveStatement();
348         }
349     }
350
351     private static @NonNull TypeEffectiveStatement<TypeStatement> createBinary(final Current<?, ?> ctx,
352             final BinaryTypeDefinition baseType, final TypeStatement declared,
353             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
354         final LengthRestrictedTypeBuilder<BinaryTypeDefinition> builder =
355                 RestrictedTypes.newBinaryBuilder(baseType, typeEffectiveSchemaPath(ctx));
356
357         for (EffectiveStatement<?, ?> stmt : substatements) {
358             if (stmt instanceof LengthEffectiveStatement) {
359                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
360
361                 try {
362                     builder.setLengthConstraint(length, length.argument());
363                 } catch (IllegalStateException e) {
364                     throw new SourceException(ctx.sourceReference(), e, "Multiple length constraints encountered");
365                 } catch (InvalidLengthConstraintException e) {
366                     throw new SourceException(ctx.sourceReference(), e, "Invalid length constraint %s",
367                         length.argument());
368                 }
369             }
370         }
371
372         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
373     }
374
375     private @NonNull TypeEffectiveStatement<TypeStatement> createBits(final Current<?, ?> ctx,
376             final BitsTypeDefinition baseType, final TypeStatement declared,
377             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
378         final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath());
379
380         for (final EffectiveStatement<?, ?> stmt : substatements) {
381             if (stmt instanceof BitEffectiveStatement) {
382                 builder.addBit(addRestrictedBit(ctx, baseType, (BitEffectiveStatement) stmt));
383             }
384         }
385
386         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
387     }
388
389     abstract @NonNull Bit addRestrictedBit(@NonNull EffectiveStmtCtx stmt, @NonNull BitsTypeDefinition base,
390         @NonNull BitEffectiveStatement bit);
391
392     private static @NonNull TypeEffectiveStatement<TypeStatement> createBoolean(final Current<?, ?> ctx,
393             final BooleanTypeDefinition baseType, final TypeStatement declared,
394             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
395         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newBooleanBuilder(baseType,
396             typeEffectiveSchemaPath(ctx)));
397     }
398
399     private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final Current<?, ?> ctx,
400             final DecimalTypeDefinition baseType, final TypeStatement declared,
401             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
402         final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
403                 RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
404
405         for (EffectiveStatement<?, ?> stmt : substatements) {
406             if (stmt instanceof RangeEffectiveStatement) {
407                 final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
408                 builder.setRangeConstraint(range, range.argument());
409             }
410             if (stmt instanceof FractionDigitsEffectiveStatement) {
411                 final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
412                 SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.sourceReference(),
413                     "Cannot override fraction-digits from base type %s to %s", baseType, digits);
414             }
415         }
416
417         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
418     }
419
420     private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final Current<?, ?> ctx,
421             final EmptyTypeDefinition baseType, final TypeStatement declared,
422             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
423         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newEmptyBuilder(baseType,
424             typeEffectiveSchemaPath(ctx)));
425     }
426
427     private @NonNull TypeEffectiveStatement<TypeStatement> createEnum(final Current<?, ?> ctx,
428             final EnumTypeDefinition baseType, final TypeStatement declared,
429             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
430         final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType, ctx.getSchemaPath());
431
432         for (final EffectiveStatement<?, ?> stmt : substatements) {
433             if (stmt instanceof EnumEffectiveStatement) {
434                 builder.addEnum(addRestrictedEnum(ctx, baseType, (EnumEffectiveStatement) stmt));
435             }
436         }
437
438         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
439     }
440
441     abstract @NonNull EnumPair addRestrictedEnum(@NonNull EffectiveStmtCtx stmt, @NonNull EnumTypeDefinition base,
442         @NonNull EnumEffectiveStatement enumStmt);
443
444     private static @NonNull TypeEffectiveStatement<TypeStatement> createIdentityref(final Current<?, ?> ctx,
445             final IdentityrefTypeDefinition baseType, final TypeStatement declared,
446             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
447         return new TypeEffectiveStatementImpl<>(declared, substatements, RestrictedTypes.newIdentityrefBuilder(baseType,
448             typeEffectiveSchemaPath(ctx)));
449     }
450
451     private static @NonNull TypeEffectiveStatement<TypeStatement> createInstanceIdentifier(
452             final Current<?, ?> ctx, final InstanceIdentifierTypeDefinition baseType, final TypeStatement declared,
453             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
454         final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(baseType,
455                     typeEffectiveSchemaPath(ctx));
456
457         for (EffectiveStatement<?, ?> stmt : substatements) {
458             if (stmt instanceof RequireInstanceEffectiveStatement) {
459                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)stmt).argument());
460             }
461         }
462
463         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
464     }
465
466     private static <T extends RangeRestrictedTypeDefinition<T, N>, N extends Number & Comparable<N>>
467         @NonNull TypeEffectiveStatement<TypeStatement> createIntegral(final Current<?, ?> ctx,
468                 final TypeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
469                 final RangeRestrictedTypeBuilder<T, N> builder) {
470         for (EffectiveStatement<?, ?> stmt : substatements) {
471             if (stmt instanceof RangeEffectiveStatement) {
472                 final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt;
473                 builder.setRangeConstraint(rangeStmt, rangeStmt.argument());
474             }
475         }
476
477         try {
478             return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
479         } catch (InvalidRangeConstraintException e) {
480             throw new SourceException(ctx.sourceReference(), e, "Invalid range constraint: %s", e.getOffendingRanges());
481         }
482     }
483
484     private static @NonNull TypeEffectiveStatement<TypeStatement> createLeafref(final Current<?, ?> ctx,
485             final LeafrefTypeDefinition baseType, final TypeStatement declared,
486             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
487         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> builder =
488                 RestrictedTypes.newLeafrefBuilder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
489
490         for (final EffectiveStatement<?, ?> stmt : substatements) {
491             if (stmt instanceof RequireInstanceEffectiveStatement) {
492                 builder.setRequireInstance(((RequireInstanceEffectiveStatement) stmt).argument());
493             }
494         }
495         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
496     }
497
498     private static @NonNull TypeEffectiveStatement<TypeStatement> createString(final Current<?, ?> ctx,
499             final StringTypeDefinition baseType, final TypeStatement declared,
500             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
501         final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
502             AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
503
504         for (EffectiveStatement<?, ?> stmt : substatements) {
505             if (stmt instanceof LengthEffectiveStatement) {
506                 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
507
508                 try {
509                     builder.setLengthConstraint(length, length.argument());
510                 } catch (IllegalStateException e) {
511                     throw new SourceException(ctx.sourceReference(), e, "Multiple length constraints encountered");
512                 } catch (InvalidLengthConstraintException e) {
513                     throw new SourceException(ctx.sourceReference(), e, "Invalid length constraint %s",
514                         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 }