1 // Copyright 2021 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 #include "src/reader/spirv/parser_impl_test_helper.h"
16 #include "src/writer/wgsl/generator_impl.h"
17
18 namespace tint {
19 namespace reader {
20 namespace spirv {
21 namespace test {
22
23 // Default to not dumping the SPIR-V assembly.
24 bool ParserImplWrapperForTest::dump_successfully_converted_spirv_ = false;
25
ParserImplWrapperForTest(const std::vector<uint32_t> & input)26 ParserImplWrapperForTest::ParserImplWrapperForTest(
27 const std::vector<uint32_t>& input)
28 : impl_(input) {}
29
~ParserImplWrapperForTest()30 ParserImplWrapperForTest::~ParserImplWrapperForTest() {
31 if (dump_successfully_converted_spirv_ && !skip_dumping_spirv_ &&
32 !impl_.spv_binary().empty() && impl_.success()) {
33 std::string disassembly = Disassemble(impl_.spv_binary());
34 std::cout << "BEGIN ConvertedOk:\n"
35 << disassembly << "\nEND ConvertedOk" << std::endl;
36 }
37 }
38
ToString(const Program & program)39 std::string ToString(const Program& program) {
40 writer::wgsl::GeneratorImpl writer(&program);
41 if (!writer.Generate()) {
42 return "WGSL writer error: " + writer.error();
43 }
44 return writer.result();
45 }
46
ToString(const Program & program,const ast::StatementList & stmts)47 std::string ToString(const Program& program, const ast::StatementList& stmts) {
48 writer::wgsl::GeneratorImpl writer(&program);
49 for (const auto* stmt : stmts) {
50 if (!writer.EmitStatement(stmt)) {
51 return "WGSL writer error: " + writer.error();
52 }
53 }
54 return writer.result();
55 }
56
ToString(const Program & program,const ast::Node * node)57 std::string ToString(const Program& program, const ast::Node* node) {
58 writer::wgsl::GeneratorImpl writer(&program);
59 if (auto* expr = node->As<ast::Expression>()) {
60 std::stringstream out;
61 if (!writer.EmitExpression(out, expr)) {
62 return "WGSL writer error: " + writer.error();
63 }
64 return out.str();
65 } else if (auto* stmt = node->As<ast::Statement>()) {
66 if (!writer.EmitStatement(stmt)) {
67 return "WGSL writer error: " + writer.error();
68 }
69 } else if (auto* ty = node->As<ast::Type>()) {
70 std::stringstream out;
71 if (!writer.EmitType(out, ty)) {
72 return "WGSL writer error: " + writer.error();
73 }
74 return out.str();
75 } else {
76 return "<unhandled AST node type " + std::string(node->TypeInfo().name) +
77 ">";
78 }
79 return writer.result();
80 }
81
82 } // namespace test
83 } // namespace spirv
84 } // namespace reader
85 } // namespace tint
86