Skip to content

Fixed-length array yields invalid Java code #33

@mthmulders

Description

@mthmulders

Given a small input file, sample.tt:

const MY_CONSTANT = 42;
typedef long MyLongs[MY_CONSTANT];
struct MyStructure {
     MyLongs myLongs;
};

Invoking Treetop with: bundle exec xdrgen sample.tt -o generated -l java -n generated, it generates this Java source file:

// Automatically generated by xdrgen 
// DO NOT EDIT or your changes may be overwritten

package generated;


import java.io.IOException;

// === xdr source ============================================================

//  typedef long MyLongs[MY_CONSTANT];

//  ===========================================================================
public class MyLongs  {
  private Long[] MyLongs;
  public Long[] getMyLongs() {
    return this.MyLongs;
  }
  public void setMyLongs(Long[] value) {
    this.MyLongs = value;
  }
  public static void encode(XdrDataOutputStream stream, MyLongs  encodedMyLongs) throws IOException {
  int MyLongssize = encodedMyLongs.getMyLongs().length;
  for (int i = 0; i < MyLongssize; i++) {
    stream.writeLong(encodedMyLongs.MyLongs[i]);
  }
  }
  public static MyLongs decode(XdrDataInputStream stream) throws IOException {
    MyLongs decodedMyLongs = new MyLongs();
  int MyLongssize = MY_CONSTANT;
  decodedMyLongs.MyLongs = new Long[MyLongssize];
  for (int i = 0; i < MyLongssize; i++) {
    decodedMyLongs.MyLongs[i] = stream.readLong();
  }
    return decodedMyLongs;
  }
}

This code does not compile:

generated/MyLongs.java:31: error: cannot find symbol
  int MyLongssize = MY_CONSTANT;
                    ^
  symbol:   variable MY_CONSTANT
  location: class MyLongs
1 error

I've tried changing the decode_member method in java.rb to read

out.puts "int #{member.name}size = #{member.declaration.resolved_size};"

But that yields an error:

NameError: undefined local variable or method `root' for #<Xdrgen::AST::FixedSize:0x00007fb36a3845d0>
  /path/to/xdrgen/lib/xdrgen/ast/fixed_size.rb:10:in `resolved_size'
  /path/to/xdrgen/lib/xdrgen/ast/declarations/array.rb:5:in `resolved_size'
  /path/to/xdrgen/lib/xdrgen/generators/java.rb:L413:in `decode_member'

I have no clue how to fix this. Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions