orig_named = t.getFieldsByName(); final MutableMap<FieldName, RecordFieldDeclValue<IdentifierType, TType>> fields_named = Maps.mutable.empty(); final ImmutableList<RecordFieldDeclType<IdentifierType, Untyped>> orig_ordered = t.getFieldsInDeclarationOrder(); final MutableList<RecordFieldDeclType<IdentifierType, TType>> fields_ordered = Lists.mutable.empty(); TRecord.newBuilder(this.package_ctx, t.getIdentifier(), t.getName()); orig_named.containsKey(k), "Names must contain %s", k)); return new TypeDeclRecord<>( t.getIdentifier(), type, fields_named.toImmutable(), t.getName(), fields_ordered.toImmutable());
private TypeDeclType<IdentifierType, Untyped> resolveTypeDeclarationRecord( final TypeDeclRecord<Unresolved, Untyped> t) throws JPRACompilerResolverException { final ImmutableList<RecordFieldDeclType<Unresolved, Untyped>> o = t.getFieldsInDeclarationOrder(); final MutableList<RecordFieldDeclType<IdentifierType, Untyped>> by_order = Lists.mutable.empty(); for (int index = 0; index < o.size(); ++index) { by_order.add(this.resolveTypeDeclRecordField(o.get(index))); } final TypeDeclRecord<IdentifierType, Untyped> rv = new TypeDeclRecord<>( this.context.getFreshIdentifier(), Untyped.get(), this.current_record_fields.toImmutable(), t.getName(), by_order.toImmutable()); this.current_record_fields.clear(); return rv; }
@Test public final void testRecord_OK1() throws Exception { final SExpressionType e = this.newFileSExpr( "t-record-1.jpr"); final JPRAParserType p = this.newParser(); final StatementType<Unresolved, Untyped> s = p.parseStatement(e); final TypeDeclRecord<Unresolved, Untyped> d = TypeDeclRecord.class.cast(s); final ImmutableList<RecordFieldDeclType<Unresolved, Untyped>> field_order = d.getFieldsInDeclarationOrder(); final ImmutableMap<FieldName, RecordFieldDeclValue<Unresolved, Untyped>> field_names = d.getFieldsByName(); Assert.assertEquals(1L, (long) field_order.size()); Assert.assertEquals(0L, (long) field_names.size()); final RecordFieldDeclType<Unresolved, Untyped> f1 = field_order.get(0); final RecordFieldDeclPaddingOctets<Unresolved, Untyped> fp = RecordFieldDeclPaddingOctets.class.cast(f1); final SizeExprConstant<?, ?> size = SizeExprConstant.class.cast(fp.getSizeExpression()); Assert.assertEquals(BigInteger.valueOf(4L), size.getValue()); }
return new TypeDeclRecord<>( Unresolved.get(), Untyped.get(),
@Test public final void testTypeDeclRecordPadding_0() throws Exception { final JPRAParserType p = this.newParser(); final GlobalContextType c = GlobalContexts.newContext(new AlwaysEmptyLoader()); final JPRAResolverType r = this.newResolver(c); final JPRACheckerType ch = this.newChecker( c, JPRACheckerStandardCapabilities.newCapabilities()); ch.checkPackageBegin( r.resolvePackageBegin( (StatementPackageBegin<Unresolved, Untyped>) p.parseStatement( this.newStringSExpr("(package-begin x.y)")))); final TypeDeclType<IdentifierType, TType> td = ch.checkTypeDeclaration( r.resolveTypeDeclaration( (TypeDeclType<Unresolved, Untyped>) p.parseStatement( this.newStringSExpr("(record T [(padding-octets 8)])")))); final TypeDeclRecord<IdentifierType, TType> tr = TypeDeclRecord.class.cast(td); final TRecord tt = TRecord.class.cast(tr.getType()); this.checkRecordInvariants(tr, tt); final TRecord.FieldPaddingOctets f = TRecord.FieldPaddingOctets.class.cast( tt.getFieldsInDeclarationOrder().get(0)); Assert.assertEquals( BigInteger.valueOf(8L * 8L), f.getSizeInBits().getValue()); }
private TypeDeclType<IdentifierType, Untyped> resolveTypeDeclarationRecord( final TypeDeclRecord<Unresolved, Untyped> t) throws JPRACompilerResolverException { final ImmutableList<RecordFieldDeclType<Unresolved, Untyped>> o = t.getFieldsInDeclarationOrder(); final MutableList<RecordFieldDeclType<IdentifierType, Untyped>> by_order = Lists.mutable.empty(); for (int index = 0; index < o.size(); ++index) { by_order.add(this.resolveTypeDeclRecordField(o.get(index))); } final TypeDeclRecord<IdentifierType, Untyped> rv = new TypeDeclRecord<>( this.context.getFreshIdentifier(), Untyped.get(), this.current_record_fields.toImmutable(), t.getName(), by_order.toImmutable()); this.current_record_fields.clear(); return rv; }
@Test public final void testRecord_OK0() throws Exception { final SExpressionType e = this.newFileSExpr( "t-record-0.jpr"); final JPRAParserType p = this.newParser(); final StatementType<Unresolved, Untyped> s = p.parseStatement(e); final TypeDeclRecord<Unresolved, Untyped> d = TypeDeclRecord.class.cast(s); final ImmutableList<RecordFieldDeclType<Unresolved, Untyped>> field_order = d.getFieldsInDeclarationOrder(); final ImmutableMap<FieldName, RecordFieldDeclValue<Unresolved, Untyped>> field_names = d.getFieldsByName(); Assert.assertEquals(1L, (long) field_order.size()); Assert.assertEquals(1L, (long) field_names.size()); final FieldName f_name = new FieldName(Optional.empty(), "f0"); final RecordFieldDeclValue<Unresolved, Untyped> f0 = field_names.get(f_name); final RecordFieldDeclType<Unresolved, Untyped> f1 = field_order.get(0); Assert.assertSame(f0, f1); Assert.assertEquals(f_name, f0.getName()); }
return new TypeDeclRecord<>( Unresolved.get(), Untyped.get(),
@Test public final void testTypeDeclRecordField_0() throws Exception { final JPRAParserType p = this.newParser(); final GlobalContextType c = GlobalContexts.newContext(new AlwaysEmptyLoader()); final JPRAResolverType r = this.newResolver(c); final JPRACheckerType ch = this.newChecker( c, JPRACheckerStandardCapabilities.newCapabilities()); ch.checkPackageBegin( r.resolvePackageBegin( (StatementPackageBegin<Unresolved, Untyped>) p.parseStatement( this.newStringSExpr("(package-begin x.y)")))); final TypeDeclType<IdentifierType, TType> td = ch.checkTypeDeclaration( r.resolveTypeDeclaration( (TypeDeclType<Unresolved, Untyped>) p.parseStatement( this.newStringSExpr("(record T [(field x [integer signed 32])])")))); final TypeDeclRecord<IdentifierType, TType> tr = TypeDeclRecord.class.cast(td); final TRecord tt = TRecord.class.cast(tr.getType()); this.checkRecordInvariants(tr, tt); }
orig_named = t.getFieldsByName(); final MutableMap<FieldName, RecordFieldDeclValue<IdentifierType, TType>> fields_named = Maps.mutable.empty(); final ImmutableList<RecordFieldDeclType<IdentifierType, Untyped>> orig_ordered = t.getFieldsInDeclarationOrder(); final MutableList<RecordFieldDeclType<IdentifierType, TType>> fields_ordered = Lists.mutable.empty(); TRecord.newBuilder(this.package_ctx, t.getIdentifier(), t.getName()); k -> Assertive.require(orig_named.containsKey(k))); return new TypeDeclRecord<>( t.getIdentifier(), type, fields_named.toImmutable(), t.getName(), fields_ordered.toImmutable());
private void checkRecordInvariants( final TypeDeclRecord<IdentifierType, TType> tr, final TRecord tt) { Assert.assertEquals(tr.getName(), tt.getName()); Assert.assertEquals(tr.getIdentifier(), tt.getIdentifier()); final ImmutableMap<FieldName, RecordFieldDeclValue<IdentifierType, TType>> tr_named = tr.getFieldsByName(); final ImmutableMap<FieldName, TRecord.FieldValue> tt_named = tt.getFieldsByName(); final ImmutableList<RecordFieldDeclType<IdentifierType, TType>> tr_order = tr.getFieldsInDeclarationOrder(); final ImmutableList<TRecord.FieldType> tt_order = tt.getFieldsInDeclarationOrder(); Assert.assertEquals((long) tt_named.size(), (long) tr_named.size()); Assert.assertEquals((long) tt_order.size(), (long) tr_order.size()); for (int index = 0; index < tt_order.size(); ++index) { final TRecord.FieldType f = tt_order.get(index); Assert.assertEquals(f.getOwner(), tt); } tt_named.forEachKey( k -> Assert.assertTrue("Map contains " + k, tr_named.containsKey(k))); }