aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/java/src/gen/cc/source_writer.h
blob: 637072c0df1c8dd0c21888f3ec95a259074c3182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_JAVA_SRC_GEN_CC_SOURCE_WRITER_H_
#define TENSORFLOW_JAVA_SRC_GEN_CC_SOURCE_WRITER_H_

#include <string>
#include <stack>
#include <list>
#include <set>

#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/java/src/gen/cc/java_defs.h"

namespace tensorflow {
namespace java {

// A class for writing Java source code.
class SourceWriter {
 public:
  SourceWriter();

  virtual ~SourceWriter();

  // Indents following lines with white spaces.
  //
  // Indentation is cumulative, i.e. the provided tabulation is added to the
  // current indentation value. If the tabulation is negative, the operation
  // will outdent the source code, until the indentation reaches 0 again.
  //
  // For example, calling Indent(2) twice will indent code with 4 white
  // spaces. Then calling Indent(-2) will outdent the code back to 2 white
  // spaces.
  SourceWriter& Indent(int tab);

  // Prefixes following lines with provided character(s).
  //
  // A common use case of a prefix is for commenting or documenting the code.
  //
  // The prefix is written after the indentation, For example, invoking
  // Indent(2)->Prefix("//") will result in prefixing lines with "  //".
  //
  // An empty value ("") will remove any line prefix that was previously set.
  SourceWriter& Prefix(const char* line_prefix);

  // Writes a source code snippet.
  //
  // The data might potentially contain newline characters, therefore it will
  // be scanned to ensure that each line is indented and prefixed properly,
  // making it a bit slower than Append().
  SourceWriter& Write(const StringPiece& text);

  // Writes a source code snippet read from a file.
  //
  // All lines of the file at the provided path will be read and written back
  // to the output of this writer in regard of its current attributes (e.g.
  // the indentation, prefix, etc.)
  SourceWriter& WriteFromFile(const string& fname, Env* env = Env::Default());

  // Appends a piece of source code.
  //
  // It is expected that no newline character is present in the data provided,
  // otherwise Write() must be used.
  SourceWriter& Append(const StringPiece& str);

  // Appends a type to the current line.
  //
  // The type is written in its simple form (i.e. not prefixed by its package)
  // and followed by any parameter types it has enclosed in brackets (<>).
  SourceWriter& AppendType(const Type& type);

  // Appends a newline character.
  //
  // Data written after calling this method will start on a new line, in respect
  // of the current indentation.
  SourceWriter& EndLine();

  // Begins a block of source code.
  //
  // This method appends a new opening brace to the current data and indent the
  // next lines according to Google Java Style Guide. The block can optionally
  // be preceded by an expression (e.g. Append("if(true)").BeginBlock();)
  SourceWriter& BeginBlock() {
    return Append(newline_ ? "{" : " {").EndLine().Indent(2);
  }

  // Ends the current block of source code.
  //
  // This method appends a new closing brace to the current data and outdent the
  // next lines back to the margin used before BeginBlock() was invoked.
  SourceWriter& EndBlock() {
    return Indent(-2).Append("}").EndLine();
  }

  // Begins to write a method.
  //
  // This method outputs the signature of the Java method from the data passed
  // in the 'method' parameter and starts a new block. Additionnal modifiers can
  // also be passed in parameter to define the accesses and the scope of this
  // method.
  SourceWriter& BeginMethod(const Method& method, int modifiers = 0);

  // Ends the current method.
  //
  // This method ends the block of code that has begun when invoking
  // BeginMethod() prior to this.
  SourceWriter& EndMethod();

  // Begins to write the main type of a source file.
  //
  // This method outputs the declaration of the Java type from the data passed
  // in the 'type' parameter and starts a new block. Additionnal modifiers can
  // also be passed in parameter to define the accesses and the scope of this
  // type.
  //
  // If not null, all types found in the 'dependencies' list will be imported
  // before declaring the new type.
  SourceWriter& BeginType(const Type& clazz,
      const std::list<Type>* dependencies, int modifiers = 0);

  // Begins to write a new inner type.
  //
  // This method outputs the declaration of the Java type from the data passed
  // in the 'type' parameter and starts a new block. Additionnal modifiers can
  // also be passed in parameter to define the accesses and the scope of this
  // type.
  SourceWriter& BeginInnerType(const Type& type, int modifiers = 0);

  // Ends the current type.
  //
  // This method ends the block of code that has begun when invoking
  // BeginType() or BeginInnerType() prior to this.
  SourceWriter& EndType();

  // Writes a list of variables as fields of a type.
  //
  // This method must be called within the definition of a type (see BeginType()
  // or BeginInnerType()). Additional modifiers can also be passed in parameter
  // to define the accesses and the scope of those fields.
  SourceWriter& WriteFields(const std::list<Variable>& fields,
      int modifiers = 0);

 protected:
  virtual void DoAppend(const StringPiece& str) = 0;

 private:
  // A utility base class for visiting elements of a type.
  class TypeVisitor {
   public:
    virtual ~TypeVisitor() = default;
    void Visit(const Type& type);

   protected:
    virtual void DoVisit(const Type& type) = 0;
  };

  // A utility class for keeping track of declared generics in a given scope.
  class GenericNamespace : public TypeVisitor {
   public:
    GenericNamespace() = default;
    explicit GenericNamespace(const GenericNamespace* parent)
      : generic_names_(parent->generic_names_) {}
    std::list<const Type*> declared_types() {
      return declared_types_;
    }
   protected:
    virtual void DoVisit(const Type& type);

   private:
    std::list<const Type*> declared_types_;
    std::set<string> generic_names_;
  };

  // A utility class for collecting a list of import statements to declare.
  class TypeImporter : public TypeVisitor {
   public:
    explicit TypeImporter(const string& current_package)
      : current_package_(current_package) {}
    virtual ~TypeImporter() = default;
    const std::set<string> imports() {
      return imports_;
    }
   protected:
    virtual void DoVisit(const Type& type);

   private:
    string current_package_;
    std::set<string> imports_;
  };

  string left_margin_;
  string line_prefix_;
  bool newline_ = true;
  std::stack<GenericNamespace*> generic_namespaces_;

  SourceWriter& WriteModifiers(int modifiers);
  SourceWriter& WriteDoc(const string& description,
    const string& return_description = "",
    const std::list<Variable>* parameters = nullptr);
  SourceWriter& WriteAnnotations(const std::list<Annotation>& annotations);
  SourceWriter& WriteGenerics(const std::list<const Type*>& generics);
  GenericNamespace* PushGenericNamespace(int modifiers);
  void PopGenericNamespace();
};

// A writer that outputs source code into a file.
//
// Note: the writer does not acquire the ownership of the file being passed in
// parameter.
class SourceFileWriter : public SourceWriter {
 public:
  explicit SourceFileWriter(WritableFile* file) : file_(file) {}
  virtual ~SourceFileWriter() = default;

 protected:
  void DoAppend(const StringPiece& str) override {
    TF_CHECK_OK(file_->Append(str));
  }

 private:
  WritableFile* file_;
};

// A writer that outputs source code into a string buffer.
class SourceBufferWriter : public SourceWriter {
 public:
  SourceBufferWriter() : owns_buffer_(true), buffer_(new string()) {}
  explicit SourceBufferWriter(string* buffer)
      : owns_buffer_(false), buffer_(buffer) {}
  virtual ~SourceBufferWriter() {
    if (owns_buffer_) delete buffer_;
  }
  const string& str() { return *buffer_; }

 protected:
  void DoAppend(const StringPiece& str) override {
    buffer_->append(str.begin(), str.end());
  }

 private:
  bool owns_buffer_;
  string* buffer_;
};

}  // namespace java
}  // namespace tensorflow

#endif  // TENSORFLOW_JAVA_SRC_GEN_CC_SOURCE_WRITER_H_