Repository navigation
Utility classes for writing Java source code from a C++ process - #14094
Conversation
|
Can one of the admins verify this patch? |
|
@asimshankar , noticing that you have not been assigned this PR yet, in case you missed it... thanks |
da51a5e to
ed2e27f
Compare
|
Just a little poke for taking a look at this PR please, thanks! |
|
@asimshankar PTAL |
asimshankar
left a comment
There was a problem hiding this comment.
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?
| namespace java { | ||
|
|
||
| /// \brief An enumeration of different modifiers commonly used in Java | ||
| enum JavaModifier { |
There was a problem hiding this comment.
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.?
There was a problem hiding this comment.
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?
|
|
||
| cc_library( | ||
| name = "java_op_gen_lib", | ||
| srcs = glob( |
There was a problem hiding this comment.
Instead of using glob and then a blacklist with exclude, maybe we can just list out all the files here explicitly?
| ) | ||
|
|
||
| tf_cc_tests( | ||
| name = "java_op_gen_cc_tests", |
There was a problem hiding this comment.
Just java_op_gen_tests (keeping in line with your naming convention of java_op_gen_lib, java_op_gen_resources etc.?
| tf_cc_tests( | ||
| name = "java_op_gen_cc_tests", | ||
| size = "small", | ||
| srcs = glob(["src/gen/cc/*_test.cc"]), |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
What's a "decorator class"?
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Instead of the loop here, could we invoke: WriteGenerics(type.params(), src_writer)?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Yup, sounds good!
|
|
||
| void WriteAnnotations(const std::vector<JavaAnnot>& annotations, | ||
| SourceWriter* src_writer) { | ||
| std::vector<JavaAnnot>::const_iterator it; |
There was a problem hiding this comment.
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();
}0bc3a84 to
2496455
Compare
|
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
left a comment
There was a problem hiding this comment.
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.
| 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; } |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yeah, you're right. What you have is fine (returning non-const reference). Sorry for the distraction.
| /// type and arguments. | ||
| class Method { | ||
| public: | ||
| static Method Of(const string& name, const Type& return_type) { |
There was a problem hiding this comment.
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);?
| namespace tensorflow { | ||
| namespace java { | ||
|
|
||
| /// \brief An enumeration of different modifiers commonly used in Java |
There was a problem hiding this comment.
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 '///').
| enum Kind { | ||
| PRIMITIVE, CLASS, INTERFACE, ENUM, GENERIC, ANNOTATION | ||
| }; | ||
| struct Comparator { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Ok I'll remove it for now
| return type1.name_ < type2.name_ || type1.package_ < type2.package_; | ||
| } | ||
| }; | ||
| static Type Primitive(const string& name) { |
There was a problem hiding this comment.
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?
| bool IsWildcard() const { | ||
| return kind_ == GENERIC && name_.empty(); | ||
| } | ||
| bool operator==(const Type& type) const { |
There was a problem hiding this comment.
Why do we need the operator overloads?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
"Variable" here means either an argument to a method or a member variable?
There was a problem hiding this comment.
Exactly, any better name?
There was a problem hiding this comment.
The name is fine. Perhaps add details in the comments.
| /// This class defines an instance of a type, which could be documented. | ||
| class Variable { | ||
| public: | ||
| static Variable Of(const string& name, const Type& type) { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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 :)
| 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_; } |
There was a problem hiding this comment.
Use description instead of descr and return_description() instead of ret_descr?
(And return_type() instead of ret_type() etc.)
| string data_; | ||
| }; | ||
|
|
||
| // Templates implementation |
There was a problem hiding this comment.
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?
|
@karllessard I am not sure you pushed the comment that addresses @asimshankar comments? |
|
@drpngx , you are right, last comments are not pushed yet but I'll do it in the following days, thank you. |
2496455 to
7f33ea0
Compare
|
Ok, that's completed now. |
|
|
Done, sorry for that. |
|
Jenkins, test this please. |
asimshankar
left a comment
There was a problem hiding this comment.
Thanks! Looking forward to the next ones :)
|
Jenkins, test this please |
|
Jenkins, test this please (The previous test failure seems test infrastructure related) |
|
FYI I think Jenkins might be stuck today
…On Fri, Dec 29, 2017, 10:00 PM Asim Shankar ***@***.***> wrote:
Jenkins, test this please
(The previous test failure seems test infrastructure related)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#14094 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AT_SbXSyeu-_lvzQzzJNtqBj0amtZdNcks5tFdFmgaJpZM4QLRYK>
.
|
|
The one test failure there is definitely unrelated, so I think this PR is ready to be merged. |
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.