| // Copyright 2020 The Tint Authors. |
| // |
| // 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 SRC_WRITER_TEXT_GENERATOR_H_ |
| #define SRC_WRITER_TEXT_GENERATOR_H_ |
| |
| #include <sstream> |
| #include <string> |
| #include <utility> |
| |
| #include "src/diagnostic/diagnostic.h" |
| #include "src/program_builder.h" |
| |
| namespace tint { |
| namespace writer { |
| |
| /// Helper methods for generators which are creating text output |
| class TextGenerator { |
| public: |
| /// Constructor |
| /// @param program the program used by the generator |
| explicit TextGenerator(const Program* program); |
| ~TextGenerator(); |
| |
| /// Increment the emitter indent level |
| void increment_indent() { indent_ += 2; } |
| /// Decrement the emiter indent level |
| void decrement_indent() { |
| if (indent_ < 2) { |
| indent_ = 0; |
| return; |
| } |
| indent_ -= 2; |
| } |
| |
| /// Writes the current indent to the output stream |
| void make_indent(); |
| |
| /// Writes the current indent to `out` |
| /// @param out the stream to write the indent to |
| void make_indent(std::ostream& out) const; |
| |
| /// @returns the result data |
| std::string result() const { return out_.str(); } |
| |
| /// @returns the list of diagnostics raised by the generator. |
| const diag::List& Diagnostics() const { return diagnostics_; } |
| |
| /// @returns the error |
| std::string error() const { return diagnostics_.str(); } |
| |
| /// @return a new, unique identifier with the given prefix. |
| /// @param prefix optional prefix to apply to the generated identifier. If |
| /// empty "tint_symbol" will be used. |
| std::string UniqueIdentifier(const std::string& prefix = ""); |
| |
| protected: |
| /// LineWriter is a helper that acts as a string buffer, who's content is |
| /// emitted to the TextGenerator as a single line on destruction. |
| struct LineWriter { |
| public: |
| /// Constructor |
| /// @param generator the TextGenerator that the LineWriter will append its |
| /// content to on destruction |
| explicit LineWriter(TextGenerator* generator); |
| /// Move constructor |
| /// @param rhs the LineWriter to move |
| LineWriter(LineWriter&& rhs); |
| /// Destructor |
| ~LineWriter(); |
| |
| /// @returns the ostringstream |
| operator std::ostream&() { return os; } |
| |
| /// @param rhs the value to write to the line |
| /// @returns the ostream so calls can be chained |
| template <typename T> |
| std::ostream& operator<<(T&& rhs) { |
| return os << std::forward<T>(rhs); |
| } |
| |
| private: |
| LineWriter(const LineWriter&) = delete; |
| LineWriter& operator=(const LineWriter&) = delete; |
| |
| std::ostringstream os; |
| TextGenerator* gen; |
| }; |
| |
| /// Helper for writing a '(' on construction and a ')' destruction. |
| struct ScopedParen { |
| /// Constructor |
| /// @param stream the std::ostream that will be written to |
| explicit ScopedParen(std::ostream& stream); |
| /// Destructor |
| ~ScopedParen(); |
| |
| private: |
| ScopedParen(ScopedParen&& rhs) = delete; |
| ScopedParen(const ScopedParen&) = delete; |
| ScopedParen& operator=(const ScopedParen&) = delete; |
| std::ostream& s; |
| }; |
| |
| /// Helper for incrementing indentation on construction and decrementing |
| /// indentation on destruction. |
| struct ScopedIndent { |
| /// Constructor |
| /// @param generator the TextGenerator that the ScopedIndent will indent |
| explicit ScopedIndent(TextGenerator* generator); |
| /// Destructor |
| ~ScopedIndent(); |
| |
| private: |
| ScopedIndent(ScopedIndent&& rhs) = delete; |
| ScopedIndent(const ScopedIndent&) = delete; |
| ScopedIndent& operator=(const ScopedIndent&) = delete; |
| TextGenerator* gen; |
| }; |
| |
| /// @returns the resolved type of the ast::Expression `expr` |
| /// @param expr the expression |
| sem::Type* TypeOf(ast::Expression* expr) const { |
| return builder_.TypeOf(expr); |
| } |
| |
| /// @returns the resolved type of the ast::Type `type` |
| /// @param type the type |
| const sem::Type* TypeOf(const ast::Type* type) const { |
| return builder_.TypeOf(type); |
| } |
| |
| /// @returns the resolved type of the ast::TypeDecl `type_decl` |
| /// @param type_decl the type |
| const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const { |
| return builder_.TypeOf(type_decl); |
| } |
| |
| /// @returns a new LineWriter, used for buffering and writing a line to out_ |
| LineWriter line() { return LineWriter(this); } |
| |
| /// The program |
| Program const* const program_; |
| /// A ProgramBuilder that thinly wraps program_ |
| ProgramBuilder builder_; |
| /// The text output stream |
| std::ostringstream out_; |
| /// Diagnostics generated by the generator |
| diag::List diagnostics_; |
| |
| private: |
| size_t indent_ = 0; |
| }; |
| |
| } // namespace writer |
| } // namespace tint |
| |
| #endif // SRC_WRITER_TEXT_GENERATOR_H_ |