final PackageContextType tp = t.getPackageContext(); final String tn = this.getRecordInterfaceWritableName(t.getName()); jcb.addSuperinterface(JPRAValueType.class); for (final TRecord.FieldType f : t.getFieldsInDeclarationOrder()) { f.matchField( new TRecord.FieldMatcherType<Unit, UnreachableCodeException>()
final PackageContextType tp = t.getPackageContext(); final TypeName t_name = t.getName(); final String tn = JPRAGeneratedNames.getRecordImplementationByteBufferedName(t_name); generateRecordFactoryMethods( imp_name, int_name, ptr_class, jcb); generateSizeMethods(jcb, t.getSizeInOctets()); generateRecordByteOffsetMethod(jcb); generateRecordFieldMethods(t, jcb);
@Override public Unit matchRecord(final TRecord t) { this.recordOrPackedField(t.getName(), t.getPackageContext()); return Unit.unit(); }
TRecord.newBuilder(this.package_ctx, t.getIdentifier(), t.getName()); type.getFieldsInDeclarationOrder().size() == orig_ordered.size(), "%d == %d", Integer.valueOf(type.getFieldsInDeclarationOrder().size()), Integer.valueOf(orig_ordered.size())); Preconditions.checkPreconditionV( type.getFieldsByName().size() == orig_named.size(), "%d == %d", Integer.valueOf(type.getFieldsByName().size()), Integer.valueOf(orig_named.size())); type.getFieldsByName().forEachKey( k -> Preconditions.checkPreconditionV( orig_named.containsKey(k), "Names must contain %s", k));
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))); }
@Test public final void testRecordFieldRecord() throws Exception { final JPRAJavaGeneratorType g = this.getJavaGenerator(); final GlobalContextType gc = GlobalContexts.newContext(new AlwaysEmptyLoader()); final PackageContextType pc = gc.loadPackage( new PackageNameQualified( Lists.immutable.of( PackageNameUnqualified.of("x"), PackageNameUnqualified.of("y"), PackageNameUnqualified.of("z")))); final IdentifierType id = gc.getFreshIdentifier(); final Optional<ImmutableLexicalPositionType<Path>> no_lex = Optional.empty(); final Path path = JPRAJavaGeneratorContract.createTemporaryDir(); final TypeName t_name = new TypeName(no_lex, "Empty"); final TRecordBuilderType teb = TRecord.newBuilder(pc, id, t_name); final TRecord te = teb.build(); final TypeName tr_name = new TypeName(no_lex, "RecordRecord"); final TRecordBuilderType rb = TRecord.newBuilder(pc, id, tr_name); rb.addField(new FieldName(no_lex, "r"), gc.getFreshIdentifier(), te); final TRecord r = rb.build(); JPRAJavaGeneratorContract.compileRecords( path, g, Lists.immutable.of(te, r)); }
jmb.addStatement("this.$N = $N", "base_offset", "in_base_offset"); for (final TRecord.FieldType f : t.getFieldsInDeclarationOrder()) { f.matchField( new TRecord.FieldMatcherType<Unit, UnreachableCodeException>()
(Procedure<TRecord>) r -> { try { final TypeName t_name = r.getName();
TRecord.newBuilder(this.package_ctx, t.getIdentifier(), t.getName()); type.getFieldsInDeclarationOrder().size() == orig_ordered.size()); Assertive.require(type.getFieldsByName().size() == orig_named.size()); type.getFieldsByName().forEachKey( k -> Assertive.require(orig_named.containsKey(k)));
@Test public final void testRecordFieldRecordTwice() throws Exception { final JPRAJavaGeneratorType g = this.getJavaGenerator(); final GlobalContextType gc = GlobalContexts.newContext(new AlwaysEmptyLoader()); final PackageContextType pc = gc.loadPackage( new PackageNameQualified( Lists.immutable.of( PackageNameUnqualified.of("x"), PackageNameUnqualified.of("y"), PackageNameUnqualified.of("z")))); final IdentifierType id = gc.getFreshIdentifier(); final Optional<ImmutableLexicalPositionType<Path>> no_lex = Optional.empty(); final Path path = JPRAJavaGeneratorContract.createTemporaryDir(); final TypeName t_name = new TypeName(no_lex, "Empty"); final TRecordBuilderType teb = TRecord.newBuilder(pc, id, t_name); final TRecord te = teb.build(); final TypeName tr_name = new TypeName(no_lex, "RecordRecordTwice"); final TRecordBuilderType rb = TRecord.newBuilder(pc, id, tr_name); rb.addField(new FieldName(no_lex, "r0"), gc.getFreshIdentifier(), te); rb.addField(new FieldName(no_lex, "r1"), gc.getFreshIdentifier(), te); final TRecord r = rb.build(); JPRAJavaGeneratorContract.compileRecords( path, g, Lists.immutable.of(te, r)); }
@Override public Unit matchRecord(final TRecord t) { this.recordOrPackedMethods(t.getName(), t.getPackageContext()); return Unit.unit(); }
private static void generateRecordFieldMethods( final TRecord t, final TypeSpec.Builder jcb) { BigInteger offset = BigInteger.ZERO; for (final TRecord.FieldType f : t.getFieldsInDeclarationOrder()) { final BigInteger o = offset; f.matchField( new TRecord.FieldMatcherType<Unit, UnreachableCodeException>() { @Override public Unit matchFieldValue( final TRecord.FieldValue f) { final TType t = f.getType(); return t.matchType( new RecordFieldImplementationProcessor(f, o, jcb)); } @Override public Unit matchFieldPaddingOctets( final TRecord.FieldPaddingOctets f) { return Unit.unit(); } }); offset = offset.add(f.getSizeInOctets().getValue()); } }
final PackageContextType tp = t.getPackageContext(); final TypeName tn = t.getName(); final String name = this.getRecordInterfaceReadableName(tn); final String pack_name = tp.getName().toString(); jcb.addSuperinterface(JPRAValueType.class); for (final TRecord.FieldType f : t.getFieldsInDeclarationOrder()) { f.matchField( new TRecord.FieldMatcherType<Unit, UnreachableCodeException>()
@Test public final void testRecordEmpty() throws Exception { final JPRAJavaGeneratorType g = this.getJavaGenerator(); final GlobalContextType gc = GlobalContexts.newContext(new AlwaysEmptyLoader()); final PackageContextType pc = gc.loadPackage( new PackageNameQualified( Lists.immutable.of( PackageNameUnqualified.of("x"), PackageNameUnqualified.of("y"), PackageNameUnqualified.of("z")))); final IdentifierType id = gc.getFreshIdentifier(); final Optional<ImmutableLexicalPositionType<Path>> no_lex = Optional.empty(); final TypeName t_name = new TypeName(no_lex, "Empty"); final TRecordBuilderType rb = TRecord.newBuilder(pc, id, t_name); final TRecord r = rb.build(); JPRAJavaGeneratorContract.compileRecords( JPRAJavaGeneratorContract.createTemporaryDir(), g, Lists.immutable.of(r)); }
@Override public Unit matchRecord( final TRecord t) { this.generateFieldOffsetConstant(); this.recordOrPackedMethods(t.getName(), t.getPackageContext()); return Unit.unit(); }
@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()); }
@Test public final void testRecordPaddingExhaustive() throws Exception { final JPRAJavaGeneratorType g = this.getJavaGenerator(); final GlobalContextType gc = GlobalContexts.newContext(new AlwaysEmptyLoader()); final PackageContextType pc = gc.loadPackage( new PackageNameQualified( Lists.immutable.of( PackageNameUnqualified.of("x"), PackageNameUnqualified.of("y"), PackageNameUnqualified.of("z")))); final IdentifierType id = gc.getFreshIdentifier(); final Optional<ImmutableLexicalPositionType<Path>> no_lex = Optional.empty(); final TypeName t_name = new TypeName(no_lex, "RecordPadding"); final TRecordBuilderType rb = TRecord.newBuilder(pc, id, t_name); rb.addPaddingOctets(no_lex, Size.valueOf(100L)); final TRecord r = rb.build(); JPRAJavaGeneratorContract.compileRecords( JPRAJavaGeneratorContract.createTemporaryDir(), g, Lists.immutable.of(r)); }
@Override public void generateRecordInterface( final TRecord t, final OutputStream os) throws IOException { NullCheck.notNull(t, "t"); NullCheck.notNull(os, "os"); try (final OutputStreamWriter out = new OutputStreamWriter(os)) { final PackageContextType tp = t.getPackageContext(); final TypeName t_name = t.getName(); final String pack_name = tp.getName().toString(); final String wtn = JPRAGeneratedNames.getRecordInterfaceWritableName(t_name); final String rtn = JPRAGeneratedNames.getRecordInterfaceReadableName(t_name); final String tn = JPRAGeneratedNames.getRecordInterfaceName(t_name); final TypeSpec.Builder jcb = TypeSpec.interfaceBuilder(tn); jcb.addJavadoc( "The interface to values of the {@code $L} record type.", t_name); jcb.addModifiers(Modifier.PUBLIC); jcb.addSuperinterface(ClassName.get(pack_name, wtn)); jcb.addSuperinterface(ClassName.get(pack_name, rtn)); jcb.addSuperinterface(JPRAValueType.class); final TypeSpec jc = jcb.build(); final JavaFile.Builder jfb = JavaFile.builder(pack_name, jc); final JavaFile jf = jfb.build(); jf.writeTo(out); } }
Optional.empty(); final TypeName t_name = new TypeName(no_lex, "RecordFloat"); final TRecordBuilderType rb = TRecord.newBuilder(pc, id, t_name);
Optional.empty(); final TypeName t_name = new TypeName(no_lex, "RecordBooleanSet"); final TRecordBuilderType rb = TRecord.newBuilder(pc, id, t_name);