1 // Copyright 2020 The Tint Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_WRITER_WGSL_GENERATOR_IMPL_H_ 16 #define SRC_WRITER_WGSL_GENERATOR_IMPL_H_ 17 18 #include <string> 19 20 #include "src/ast/assignment_statement.h" 21 #include "src/ast/binary_expression.h" 22 #include "src/ast/bitcast_expression.h" 23 #include "src/ast/break_statement.h" 24 #include "src/ast/continue_statement.h" 25 #include "src/ast/discard_statement.h" 26 #include "src/ast/fallthrough_statement.h" 27 #include "src/ast/for_loop_statement.h" 28 #include "src/ast/if_statement.h" 29 #include "src/ast/index_accessor_expression.h" 30 #include "src/ast/loop_statement.h" 31 #include "src/ast/member_accessor_expression.h" 32 #include "src/ast/return_statement.h" 33 #include "src/ast/switch_statement.h" 34 #include "src/ast/unary_op_expression.h" 35 #include "src/program.h" 36 #include "src/sem/storage_texture_type.h" 37 #include "src/sem/struct.h" 38 #include "src/writer/text_generator.h" 39 40 namespace tint { 41 namespace writer { 42 namespace wgsl { 43 44 /// Implementation class for WGSL generator 45 class GeneratorImpl : public TextGenerator { 46 public: 47 /// Constructor 48 /// @param program the program 49 explicit GeneratorImpl(const Program* program); 50 ~GeneratorImpl(); 51 52 /// Generates the result data 53 /// @returns true on successful generation; false otherwise 54 bool Generate(); 55 56 /// Handles generating a declared type 57 /// @param ty the declared type to generate 58 /// @returns true if the declared type was emitted 59 bool EmitTypeDecl(const ast::TypeDecl* ty); 60 /// Handles an index accessor expression 61 /// @param out the output of the expression stream 62 /// @param expr the expression to emit 63 /// @returns true if the index accessor was emitted 64 bool EmitIndexAccessor(std::ostream& out, 65 const ast::IndexAccessorExpression* expr); 66 /// Handles an assignment statement 67 /// @param stmt the statement to emit 68 /// @returns true if the statement was emitted successfully 69 bool EmitAssign(const ast::AssignmentStatement* stmt); 70 /// Handles generating a binary expression 71 /// @param out the output of the expression stream 72 /// @param expr the binary expression 73 /// @returns true if the expression was emitted, false otherwise 74 bool EmitBinary(std::ostream& out, const ast::BinaryExpression* expr); 75 /// Handles generating a bitcast expression 76 /// @param out the output of the expression stream 77 /// @param expr the bitcast expression 78 /// @returns true if the bitcast was emitted 79 bool EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr); 80 /// Handles a block statement 81 /// @param stmt the statement to emit 82 /// @returns true if the statement was emitted successfully 83 bool EmitBlock(const ast::BlockStatement* stmt); 84 /// Handles a break statement 85 /// @param stmt the statement to emit 86 /// @returns true if the statement was emitted successfully 87 bool EmitBreak(const ast::BreakStatement* stmt); 88 /// Handles generating a call expression 89 /// @param out the output of the expression stream 90 /// @param expr the call expression 91 /// @returns true if the call expression is emitted 92 bool EmitCall(std::ostream& out, const ast::CallExpression* expr); 93 /// Handles a case statement 94 /// @param stmt the statement 95 /// @returns true if the statment was emitted successfully 96 bool EmitCase(const ast::CaseStatement* stmt); 97 /// Handles generating a literal expression 98 /// @param out the output of the expression stream 99 /// @param expr the literal expression expression 100 /// @returns true if the literal expression is emitted 101 bool EmitLiteral(std::ostream& out, const ast::LiteralExpression* expr); 102 /// Handles a continue statement 103 /// @param stmt the statement to emit 104 /// @returns true if the statement was emitted successfully 105 bool EmitContinue(const ast::ContinueStatement* stmt); 106 /// Handles generate an Expression 107 /// @param out the output of the expression stream 108 /// @param expr the expression 109 /// @returns true if the expression was emitted 110 bool EmitExpression(std::ostream& out, const ast::Expression* expr); 111 /// Handles generating a fallthrough statement 112 /// @param stmt the fallthrough statement 113 /// @returns true if the statement was successfully emitted 114 bool EmitFallthrough(const ast::FallthroughStatement* stmt); 115 /// Handles generating a function 116 /// @param func the function to generate 117 /// @returns true if the function was emitted 118 bool EmitFunction(const ast::Function* func); 119 /// Handles generating an identifier expression 120 /// @param out the output of the expression stream 121 /// @param expr the identifier expression 122 /// @returns true if the identifier was emitted 123 bool EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr); 124 /// Handles an if statement 125 /// @param stmt the statement to emit 126 /// @returns true if the statement was successfully emitted 127 bool EmitIf(const ast::IfStatement* stmt); 128 /// Handles generating a discard statement 129 /// @param stmt the discard statement 130 /// @returns true if the statement was successfully emitted 131 bool EmitDiscard(const ast::DiscardStatement* stmt); 132 /// Handles a loop statement 133 /// @param stmt the statement to emit 134 /// @returns true if the statement was emtited 135 bool EmitLoop(const ast::LoopStatement* stmt); 136 /// Handles a for-loop statement 137 /// @param stmt the statement to emit 138 /// @returns true if the statement was emtited 139 bool EmitForLoop(const ast::ForLoopStatement* stmt); 140 /// Handles a member accessor expression 141 /// @param out the output of the expression stream 142 /// @param expr the member accessor expression 143 /// @returns true if the member accessor was emitted 144 bool EmitMemberAccessor(std::ostream& out, 145 const ast::MemberAccessorExpression* expr); 146 /// Handles return statements 147 /// @param stmt the statement to emit 148 /// @returns true if the statement was successfully emitted 149 bool EmitReturn(const ast::ReturnStatement* stmt); 150 /// Handles statement 151 /// @param stmt the statement to emit 152 /// @returns true if the statement was emitted 153 bool EmitStatement(const ast::Statement* stmt); 154 /// Handles a statement list 155 /// @param stmts the statements to emit 156 /// @returns true if the statements were emitted 157 bool EmitStatements(const ast::StatementList& stmts); 158 /// Handles a statement list with an increased indentation 159 /// @param stmts the statements to emit 160 /// @returns true if the statements were emitted 161 bool EmitStatementsWithIndent(const ast::StatementList& stmts); 162 /// Handles generating a switch statement 163 /// @param stmt the statement to emit 164 /// @returns true if the statement was emitted 165 bool EmitSwitch(const ast::SwitchStatement* stmt); 166 /// Handles generating type 167 /// @param out the output of the expression stream 168 /// @param type the type to generate 169 /// @returns true if the type is emitted 170 bool EmitType(std::ostream& out, const ast::Type* type); 171 /// Handles generating a struct declaration 172 /// @param str the struct 173 /// @returns true if the struct is emitted 174 bool EmitStructType(const ast::Struct* str); 175 /// Handles emitting an image format 176 /// @param out the output of the expression stream 177 /// @param fmt the format to generate 178 /// @returns true if the format is emitted 179 bool EmitImageFormat(std::ostream& out, const ast::ImageFormat fmt); 180 /// Handles emitting an access control 181 /// @param out the output of the expression stream 182 /// @param access the access to generate 183 /// @returns true if the access is emitted 184 bool EmitAccess(std::ostream& out, const ast::Access access); 185 /// Handles a unary op expression 186 /// @param out the output of the expression stream 187 /// @param expr the expression to emit 188 /// @returns true if the expression was emitted 189 bool EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr); 190 /// Handles generating a variable 191 /// @param out the output of the expression stream 192 /// @param var the variable to generate 193 /// @returns true if the variable was emitted 194 bool EmitVariable(std::ostream& out, const ast::Variable* var); 195 /// Handles generating a decoration list 196 /// @param out the output of the expression stream 197 /// @param decos the decoration list 198 /// @returns true if the decorations were emitted 199 bool EmitDecorations(std::ostream& out, const ast::DecorationList& decos); 200 }; 201 202 } // namespace wgsl 203 } // namespace writer 204 } // namespace tint 205 206 #endif // SRC_WRITER_WGSL_GENERATOR_IMPL_H_ 207