blob: 33c84c2c7a1ed7d55877cc6c42e87a5997cd4d8f [file] [log] [blame]
// 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_READER_SPV_PARSER_IMPL_H_
#define SRC_READER_SPV_PARSER_IMPL_H_
#include <cstdint>
#include <memory>
#include <sstream>
#include <vector>
#include "src/reader/reader.h"
#include "src/reader/spv/fail_stream.h"
namespace tint {
namespace reader {
namespace spv {
/// Parser implementation for SPIR-V.
class ParserImpl : Reader {
public:
/// Creates a new parser
/// @param input the input data to parse
explicit ParserImpl(const std::vector<uint32_t>& input);
/// Destructor
~ParserImpl() override;
/// Run the parser
/// @returns true if the parse was successful, false otherwise.
bool Parse() override;
/// @returns the module. The module in the parser will be reset after this.
ast::Module module() override;
/// Logs failure, ands return a failure stream to accumulate diagnostic
/// messages. By convention, a failure should only be logged along with
/// a non-empty string diagnostic.
/// @returns the failure stream
FailStream& Fail() {
success_ = false;
return fail_stream_;
}
/// @returns the accumulated error string
const std::string error() { return errors_.str(); }
private:
// The SPIR-V binary we're parsing
std::vector<uint32_t> spv_binary_;
// The resulting module in Tint AST form.
ast::Module ast_module_;
// Is the parse successful?
bool success_ = true;
// Collector for diagnostic messages.
std::stringstream errors_;
FailStream fail_stream_;
};
} // namespace spv
} // namespace reader
} // namespace tint
#endif // SRC_READER_SPV_PARSER_IMPL_H_