Skip to content

Utility classes for writing Java source code from a C++ process - #14094

Merged
drpngx merged 4 commits into
tensorflow:masterfrom
karllessard:java-source-writer
Dec 31, 2017
Merged

drpngx merged 4 commits into
tensorflow:masterfrom
karllessard:java-source-writer

Conversation

@karllessard

@karllessard karllessard commented Oct 30, 2017 •

Copy link
Copy Markdown
Contributor

This PR sets up a few tools for writing Java source files from the op generator (or any future generator of Java code written in C++).

They are handling most of the tricky cases known to be encountered when we'll generate the ops, including generics, wildcards, optional inner classes, etc. Note that they focus on simplicity rather than performance or memory optimitizations since they will only be used at compile time.

As stated in a previous PR, java_writer and source_writer are being kept separate since the later might be a good candidate to be move to the core::io layer if required some day.

@tensorflow-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@karllessard

Copy link
Copy Markdown
Contributor Author

@asimshankar , noticing that you have not been assigned this PR yet, in case you missed it... thanks

@asimshankar asimshankar self-assigned this Nov 8, 2017
@karllessard

Copy link
Copy Markdown
Contributor Author

Just a little poke for taking a look at this PR please, thanks!

@sb2nov
sb2nov requested a review from asimshankar November 29, 2017 15:59
@sb2nov sb2nov added awaiting review Pull request awaiting review kokoro:run labels Nov 29, 2017
@sb2nov

sb2nov commented Nov 29, 2017

Copy link
Copy Markdown
Contributor

@asimshankar PTAL

@asimshankar asimshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Haven't looked thorough all of it yet but figured I could send out an initial round of comments/questions.

The PR is pretty big (even conceptually). It might make sense to break it into smaller ones so that we can iterate more quickly. For example:

  • One to add all the JavaDefs
  • One for SourceWriter
  • One for JavaWriter
  • One that will hook this up into op_generator

Thoughts?

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
namespace java {

/// \brief An enumeration of different modifiers commonly used in Java
enum JavaModifier {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: All symbols are already in the 'java' namespace, so why have the Java prefix as well?
How about: tensorflow::java::Modifier, tensorflow::java::Annotation etc. instead of tensorflow::java::JavaModifier, tensorflow::java::JavaAnnot etc.?

Or, alternatively, do away with the java namespace? So it will be tensorflow::JavaDoc etc.?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I juggled a bit with that idea too but I ended up using both the java namespace and the Java* prefix where the former classifies any CC code written under the //tensorflow/java package and the later identifies classes that are directly related to the Java language. Does that make sense to you?

Comment thread tensorflow/java/BUILD Outdated

cc_library(
name = "java_op_gen_lib",
srcs = glob(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using glob and then a blacklist with exclude, maybe we can just list out all the files here explicitly?

Comment thread tensorflow/java/BUILD Outdated
)

tf_cc_tests(
name = "java_op_gen_cc_tests",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just java_op_gen_tests (keeping in line with your naming convention of java_op_gen_lib, java_op_gen_resources etc.?

Comment thread tensorflow/java/BUILD Outdated
tf_cc_tests(
name = "java_op_gen_cc_tests",
size = "small",
srcs = glob(["src/gen/cc/*_test.cc"]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's list out all the files instead of using glob

/// compile-time.
///
/// Source writers are language-agnostic and therefore only expose generic
/// methods common to most languages. Use a decorator class to implement

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a "decorator class"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A specific pattern for a wrapper class... I'll clarify this part.

}

void WriteType(const JavaType& type, SourceWriter* src_writer) {
src_writer->Write(Java::IsWildcard(type) ? "?" : type.name());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense of type.name() to return "?" when it is a GENERIC with no name instead of having callers do an IsWildcard check?

Then you might not even need the IsWildcard method?

src_writer->Write(Java::IsWildcard(type) ? "?" : type.name());
if (!type.params().empty()) {
src_writer->Write("<");
std::vector<JavaType>::const_iterator it;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Might be more readable as:

bool first = true;
src_writer->Writer("<");
for (const JavaType& t : type.params()) {
  if (!first) {
    src_writer->Write(", ");
 }
 first = false;
 WriteType(t, src_writer);
}

If you agree, perhaps change the other places as well?

if (!type.params().empty()) {
src_writer->Write("<");
std::vector<JavaType>::const_iterator it;
for (it = type.params().cbegin(); it != type.params().cend(); ++it) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the loop here, could we invoke: WriteGenerics(type.params(), src_writer)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is that WriteGenerics(...) is used to declare a generic (with all its supertypes in present) while here, we just want to name them. For example, if your have generic T extending Number, the WriteGenerics(...) method will output <T extends Number> while here, you just want to output <T>, like in List<T>.

src_writer->Write((*it)->name());
if (!(*it)->supertypes().empty()) {
src_writer->Write(" extends ");
WriteType((*it)->supertypes().front(), src_writer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the first supertype is listed here?Should we be listing all? (extends A & B & C etc. - https://docs.oracle.com/javase/tutorial/java/generics/bounded.html)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, sounds good!


void WriteAnnotations(const std::vector<JavaAnnot>& annotations,
SourceWriter* src_writer) {
std::vector<JavaAnnot>::const_iterator it;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use range-based loop here too?

for (const JavaAnnot& a : annotations) {
  src_writer->Write("@" + a.type().name());
  if (!a.attrs().empty()) {
    src_writer->Write("(");
    src_writer->Write(a.attrs());
    src_writer->Write(")");
  }
  src_writer->EndOfLine();
}

@karllessard

Copy link
Copy Markdown
Contributor Author

Hi @asimshankar , sorry for the delay, I was very busy lately. So as you suggested, I split this PR into smaller ones, starting from the Java definitions. I also incorporated your previous suggestions related to them, please take a look when you'll have a minute, thanks!

@asimshankar asimshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this a smaller PR, was much easier to follow (and nitpick on :)

Some minor things, but this should be good to go soon.

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
const string& name() const { return name_; }
const Type& ret_type() const { return ret_type_; }
const string& descr() const { return descr_; }
Method& descr(const string& descr) { descr_ = descr; return *this; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all these "builder" methods, that return the same object - would you mind changing them to return pointers instead of references? The arguments in https://google.github.io/styleguide/cppguide.html#Reference_Arguments apply here I think (that returning non-const references can be confusing).

@karllessard karllessard Dec 20, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I thought this rule was mainly targeting methods arguments and not returned values as well. I can change this but it leads up to some strange syntax in some cases, e.g.

type.add_param(Type::Generic("T"));  // ok
type.add_param(*(Type::Generic("T").description("any type")));  // mmmh...

What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. What you have is fine (returning non-const reference). Sorry for the distraction.

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
/// type and arguments.
class Method {
public:
static Method Of(const string& name, const Type& return_type) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Of? Seems like name corresponds to the name of the method, not the name of the class this is a method of, right? So perhaps just static Method Create(const string& name, const Type& return_type);?

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
namespace tensorflow {
namespace java {

/// \brief An enumeration of different modifiers commonly used in Java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need doxygen style comments here. The simpler style in https://google.github.io/styleguide/cppguide.html#Class_Comments should suffice (i.e., no \brief and use '//' instead of '///').

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
enum Kind {
PRIMITIVE, CLASS, INTERFACE, ENUM, GENERIC, ANNOTATION
};
struct Comparator {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call this ByName, making it clearer at the call site what the sort order is.

(Though, it seems that this isn't being used in the code so far, so I'd slightly prefer that we remove this for now and only introduce it when needed)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll remove it for now

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
return type1.name_ < type2.name_ || type1.package_ < type2.package_;
}
};
static Type Primitive(const string& name) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have methods like:

static Type Int() { return Type(Type::PRIMITIVE, "int", ""); }
static Type Float() ...

etc. instead of taking the type of the primitive as a string argument? There are only 8, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
bool IsWildcard() const {
return kind_ == GENERIC && name_.empty();
}
bool operator==(const Type& type) const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the operator overloads?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the comparator, the usage of the == operator will come in a next PR so I'll remove it for now

/// \brief A definition of a Java variable
///
/// This class defines an instance of a type, which could be documented.
class Variable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Variable" here means either an argument to a method or a member variable?

@karllessard karllessard Dec 20, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, any better name?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is fine. Perhaps add details in the comments.

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
/// This class defines an instance of a type, which could be documented.
class Variable {
public:
static Variable Of(const string& name, const Type& type) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Of" here was a little confusing to me. This creates a "variable of type type named name", right?
Perhaps call it Variable::Create(const string& name, const Type& type)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was a terrible name, I was trying to avoid Create because I didn't want to prefix all my other factory methods with Create (i.e. CreateIterableOf, etc.) which makes it too verbose. But thinking of it now, I don't think I'll have to... so yes Create it will be :)

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
const Type& ret_type() const { return ret_type_; }
const string& descr() const { return descr_; }
Method& descr(const string& descr) { descr_ = descr; return *this; }
const string& ret_descr() const { return ret_descr_; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use description instead of descr and return_description() instead of ret_descr?
(And return_type() instead of ret_type() etc.)

Comment thread tensorflow/java/src/gen/cc/java_defs.h Outdated
string data_;
};

// Templates implementation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see if we can avoid templates. Perhaps remove this from this PR for now and we can discuss options on the follow up PR that will need to introduce some sort of scanner?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok no problem

@asimshankar asimshankar added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Dec 19, 2017
@drpngx

drpngx commented Dec 27, 2017

Copy link
Copy Markdown
Contributor

@karllessard I am not sure you pushed the comment that addresses @asimshankar comments?

@karllessard

Copy link
Copy Markdown
Contributor Author

@drpngx , you are right, last comments are not pushed yet but I'll do it in the following days, thank you.

@karllessard

Copy link
Copy Markdown
Contributor Author

Ok, that's completed now.

@drpngx

drpngx commented Dec 28, 2017

Copy link
Copy Markdown
Contributor
FAIL: buildifier found errors and/or warnings in above BUILD files.
buildifier suggested the following changes:
112,113d111
<         "src/gen/cc/op_generator.h",
<         "src/gen/cc/op_generator.cc",
114a113,114
>         "src/gen/cc/op_generator.cc",
>         "src/gen/cc/op_generator.h",
Please fix manually or run buildifier <file> to auto-fix.

@karllessard

Copy link
Copy Markdown
Contributor Author

Done, sorry for that.

@drpngx

drpngx commented Dec 28, 2017

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@asimshankar asimshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looking forward to the next ones :)

@asimshankar asimshankar added awaiting testing (then merge) kokoro:force-run Tests on submitted change and removed stat:awaiting response Status - Awaiting response from author labels Dec 29, 2017
@asimshankar

Copy link
Copy Markdown
Contributor

Jenkins, test this please

@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 29, 2017
@asimshankar

Copy link
Copy Markdown
Contributor

Jenkins, test this please

(The previous test failure seems test infrastructure related)

@drpngx

drpngx commented Dec 30, 2017 via email

Copy link
Copy Markdown
Contributor

@asimshankar

Copy link
Copy Markdown
Contributor

The one test failure there is definitely unrelated, so I think this PR is ready to be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants