blob: cc153152b27178007cc80e3b700e382cc286cf30 [file] [log] [blame]
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001/*
Hans-Kristian Arntzen318c17c2019-01-04 12:38:35 +01002 * Copyright 2018-2019 Arm Limited
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SPIRV_CROSS_PARSER_HPP
18#define SPIRV_CROSS_PARSER_HPP
19
20#include "spirv_cross_parsed_ir.hpp"
21#include <stdint.h>
22#include <vector>
23
24namespace spirv_cross
25{
26class Parser
27{
28public:
29 Parser(const uint32_t *spirv_data, size_t word_count);
30 Parser(std::vector<uint32_t> spirv);
31
32 void parse();
33
34 ParsedIR &get_parsed_ir()
35 {
36 return ir;
37 }
38
39private:
40 ParsedIR ir;
41 SPIRFunction *current_function = nullptr;
42 SPIRBlock *current_block = nullptr;
43
44 void parse(const Instruction &instr);
45 const uint32_t *stream(const Instruction &instr) const;
46
47 template <typename T, typename... P>
48 T &set(uint32_t id, P &&... args)
49 {
Hans-Kristian Arntzend92de002019-01-10 09:49:33 +010050 ir.add_typed_id(static_cast<Types>(T::type), id);
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010051 auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020052 var.self = id;
53 return var;
54 }
55
56 template <typename T>
57 T &get(uint32_t id)
58 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010059 return variant_get<T>(ir.ids[id]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020060 }
61
62 template <typename T>
63 T *maybe_get(uint32_t id)
64 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010065 if (ir.ids[id].get_type() == static_cast<Types>(T::type))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020066 return &get<T>(id);
67 else
68 return nullptr;
69 }
70
71 template <typename T>
72 const T &get(uint32_t id) const
73 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010074 return variant_get<T>(ir.ids[id]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020075 }
76
77 template <typename T>
78 const T *maybe_get(uint32_t id) const
79 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010080 if (ir.ids[id].get_type() == T::type)
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020081 return &get<T>(id);
82 else
83 return nullptr;
84 }
85
86 // This must be an ordered data structure so we always pick the same type aliases.
87 std::vector<uint32_t> global_struct_cache;
88
89 bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const;
90 bool variable_storage_is_aliased(const SPIRVariable &v) const;
91 void make_constant_null(uint32_t id, uint32_t type);
92};
93} // namespace spirv_cross
94
95#endif