working on implementing generate_spirv_parser
[kazan.git] / src / generate_spirv_parser / ast.cpp
1 /*
2 * Copyright 2017 Jacob Lifshay
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 */
23 #include "ast.h"
24
25 namespace vulkan_cpu
26 {
27 namespace generate_spirv_parser
28 {
29 namespace ast
30 {
31 namespace
32 {
33 std::string to_hex_string(std::uint32_t v)
34 {
35 return json::ast::Number_value::append_unsigned_integer_to_string(v, "0x", 0x10, 8);
36 }
37
38 constexpr json::Location make_empty_location() noexcept
39 {
40 // use function to make empty location so it will be easy to find all occurrences if location
41 // info is added to ast
42 return {};
43 }
44 }
45
46 json::ast::Value Copyright::to_json() const
47 {
48 json::ast::Array retval;
49 retval.values.reserve(lines.size());
50 for(auto &line : lines)
51 retval.values.push_back(json::ast::Value(make_empty_location(), line));
52 return json::ast::Value(make_empty_location(), std::move(retval));
53 }
54
55 json::ast::Value Instructions::to_json() const
56 {
57 json::ast::Array retval;
58 #warning finish
59 return json::ast::Value(make_empty_location(), std::move(retval));
60 }
61
62 json::ast::Value Operand_kinds::Operand_kind::Enumerants::Enumerant::to_json() const
63 {
64 json::ast::Object retval;
65 #warning finish
66 return json::ast::Value(make_empty_location(), std::move(retval));
67 }
68
69 json::ast::Value Operand_kinds::Operand_kind::Enumerants::to_json() const
70 {
71 json::ast::Array retval;
72 retval.values.reserve(enumerants.size());
73 for(auto &enumerant : enumerants)
74 retval.values.push_back(enumerant.to_json());
75 return json::ast::Value(make_empty_location(), std::move(retval));
76 }
77
78 json::ast::Value Operand_kinds::Operand_kind::Doc::to_json() const
79 {
80 return json::ast::Value(make_empty_location(), value);
81 }
82
83 json::ast::Value Operand_kinds::Operand_kind::Bases::to_json() const
84 {
85 json::ast::Array retval;
86 retval.values.reserve(values.size());
87 for(auto &value : values)
88 retval.values.push_back(json::ast::Value(make_empty_location(), value));
89 return json::ast::Value(make_empty_location(), std::move(retval));
90 }
91
92 json::ast::Value Operand_kinds::Operand_kind::to_json() const
93 {
94 json::ast::Object retval;
95 retval.values["category"] =
96 json::ast::Value(make_empty_location(), get_json_name_from_category(category));
97 retval.values["kind"] = json::ast::Value(make_empty_location(), kind);
98 retval.values[get_value_json_key_name_from_category(category)] = util::visit(
99 [&](auto &v) -> json::ast::Value
100 {
101 return v.to_json();
102 },
103 value);
104 return json::ast::Value(make_empty_location(), std::move(retval));
105 }
106
107 json::ast::Value Operand_kinds::to_json() const
108 {
109 json::ast::Array retval;
110 retval.values.reserve(operand_kinds.size());
111 for(auto &operand_kind : operand_kinds)
112 retval.values.push_back(operand_kind.to_json());
113 return json::ast::Value(make_empty_location(), std::move(retval));
114 }
115
116 json::ast::Value Top_level::to_json() const
117 {
118 json::ast::Object retval;
119 retval.values["copyright"] = copyright.to_json();
120 retval.values["magic_number"] =
121 json::ast::Value(make_empty_location(), to_hex_string(magic_number));
122 retval.values["major_version"] = json::ast::Value(
123 make_empty_location(), json::ast::Number_value::unsigned_integer_to_string(major_version));
124 retval.values["minor_version"] = json::ast::Value(
125 make_empty_location(), json::ast::Number_value::unsigned_integer_to_string(minor_version));
126 retval.values["revision"] = json::ast::Value(
127 make_empty_location(), json::ast::Number_value::unsigned_integer_to_string(revision));
128 retval.values["instructions"] = instructions.to_json();
129 retval.values["operand_kinds"] = operand_kinds.to_json();
130 return json::ast::Value(make_empty_location(), std::move(retval));
131 }
132 }
133 }
134 }