2 * Copyright 2017 Jacob Lifshay
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
24 #ifndef GENERATE_SPIRV_PARSER_AST_H_
25 #define GENERATE_SPIRV_PARSER_AST_H_
27 #include "json/json.h"
34 namespace generate_spirv_parser
40 std::vector
<std::string
> lines
;
44 explicit Copyright(std::vector
<std::string
> lines
) noexcept
: lines(std::move(lines
))
47 json::ast::Value
to_json() const;
48 template <typename Fn
>
49 void visit(Fn fn
) const
57 std::vector
<std::string
> capabilities
;
58 Capabilities() : capabilities()
61 explicit Capabilities(std::vector
<std::string
> capabilities
) noexcept
62 : capabilities(std::move(capabilities
))
65 bool empty() const noexcept
67 return capabilities
.empty();
69 json::ast::Value
to_json() const;
70 template <typename Fn
>
71 void visit(Fn fn
) const
79 std::vector
<std::string
> extensions
;
80 Extensions() : extensions()
83 explicit Extensions(std::vector
<std::string
> extensions
) noexcept
84 : extensions(std::move(extensions
))
87 bool empty() const noexcept
89 return extensions
.empty();
91 json::ast::Value
to_json() const;
92 template <typename Fn
>
93 void visit(Fn fn
) const
107 enum class Quantifier
113 static constexpr const char *get_quantifier_string(Quantifier quantifier
) noexcept
117 case Quantifier::none
:
119 case Quantifier::optional
:
121 case Quantifier::variable
:
128 Quantifier quantifier
;
129 Operand(std::string kind
, std::string name
, Quantifier quantifier
) noexcept
130 : kind(std::move(kind
)),
131 name(std::move(name
)),
132 quantifier(quantifier
)
135 json::ast::Value
to_json() const;
136 template <typename Fn
>
137 void visit(Fn fn
) const
142 std::vector
<Operand
> operands
;
143 Operands() : operands()
146 explicit Operands(std::vector
<Operand
> operands
) noexcept
147 : operands(std::move(operands
))
150 bool empty() const noexcept
152 return operands
.empty();
154 json::ast::Value
to_json() const;
155 template <typename Fn
>
156 void visit(Fn fn
) const
159 for(auto &operand
: operands
)
164 std::uint32_t opcode
;
166 Capabilities capabilities
;
167 Extensions extensions
;
168 Instruction(std::string opname
,
169 std::uint32_t opcode
,
171 Capabilities capabilities
,
172 Extensions extensions
) noexcept
: opname(std::move(opname
)),
174 operands(std::move(operands
)),
175 capabilities(std::move(capabilities
)),
176 extensions(std::move(extensions
))
179 json::ast::Value
to_json() const;
180 template <typename Fn
>
181 void visit(Fn fn
) const
185 capabilities
.visit(fn
);
186 extensions
.visit(fn
);
189 std::vector
<Instruction
> instructions
;
190 explicit Instructions(std::vector
<Instruction
> instructions
) noexcept
191 : instructions(std::move(instructions
))
194 json::ast::Value
to_json() const;
195 template <typename Fn
>
196 void visit(Fn fn
) const
199 for(auto &instruction
: instructions
)
200 instruction
.visit(fn
);
217 static constexpr const char *get_json_name_from_category(Category category
) noexcept
221 case Category::bit_enum
:
223 case Category::value_enum
:
227 case Category::literal
:
229 case Category::composite
:
237 static constexpr const char *get_json_key_name() noexcept
243 std::string enumerant
;
245 Capabilities capabilities
;
252 explicit Parameter(std::string kind
, std::string name
) noexcept
253 : kind(std::move(kind
)),
254 name(std::move(name
))
257 json::ast::Value
to_json() const;
258 template <typename Fn
>
259 void visit(Fn fn
) const
264 std::vector
<Parameter
> parameters
;
265 Parameters() : parameters()
268 explicit Parameters(std::vector
<Parameter
> parameters
) noexcept
269 : parameters(std::move(parameters
))
272 json::ast::Value
to_json() const;
273 bool empty() const noexcept
275 return parameters
.empty();
277 template <typename Fn
>
278 void visit(Fn fn
) const
281 for(auto ¶meter
: parameters
)
285 Parameters parameters
;
286 Extensions extensions
;
287 Enumerant(std::string enumerant
,
289 Capabilities capabilities
,
290 Parameters parameters
,
291 Extensions extensions
) noexcept
: enumerant(std::move(enumerant
)),
293 capabilities(std::move(capabilities
)),
294 parameters(std::move(parameters
)),
295 extensions(std::move(extensions
))
298 json::ast::Value
to_json(bool is_bit_enumerant
) const;
299 template <typename Fn
>
300 void visit(Fn fn
) const
303 capabilities
.visit(fn
);
304 parameters
.visit(fn
);
305 extensions
.visit(fn
);
308 std::vector
<Enumerant
> enumerants
;
309 explicit Enumerants(std::vector
<Enumerant
> enumerants
) noexcept
: enumerants(enumerants
)
312 json::ast::Value
to_json(bool is_bit_enumerant
) const;
313 json::ast::Value
to_json(Category category
) const
315 return to_json(category
== Category::bit_enum
);
317 template <typename Fn
>
318 void visit(Fn fn
) const
321 for(auto &enumerant
: enumerants
)
327 static constexpr const char *get_json_key_name() noexcept
333 explicit Doc(std::string value
) noexcept
: value(std::move(value
))
336 json::ast::Value
to_json() const;
337 json::ast::Value
to_json(Category category
) const
341 template <typename Fn
>
342 void visit(Fn fn
) const
349 static constexpr const char *get_json_key_name() noexcept
353 std::vector
<std::string
> values
;
355 explicit Bases(std::vector
<std::string
> values
) noexcept
: values(std::move(values
))
358 json::ast::Value
to_json() const;
359 json::ast::Value
to_json(Category category
) const
363 template <typename Fn
>
364 void visit(Fn fn
) const
369 typedef util::variant
<Enumerants
, Doc
, Bases
> Value
;
371 static bool does_category_match_value(Category category
, const Value
&value
) noexcept
375 case Category::bit_enum
:
376 case Category::value_enum
:
377 return util::holds_alternative
<Enumerants
>(value
);
379 case Category::literal
:
380 return util::holds_alternative
<Doc
>(value
);
381 case Category::composite
:
382 return util::holds_alternative
<Bases
>(value
);
386 static constexpr const char *get_value_json_key_name_from_category(
387 Category category
) noexcept
391 case Category::bit_enum
:
392 case Category::value_enum
:
393 return Enumerants::get_json_key_name();
395 case Category::literal
:
396 return Doc::get_json_key_name();
397 case Category::composite
:
398 return Bases::get_json_key_name();
402 Operand_kind(Category category
, std::string kind
, Value value
) noexcept
403 : category(category
),
405 value(std::move(value
))
408 json::ast::Value
to_json() const;
409 template <typename Fn
>
410 void visit(Fn fn
) const
421 std::vector
<Operand_kind
> operand_kinds
;
422 explicit Operand_kinds(std::vector
<Operand_kind
> operand_kinds
) noexcept
423 : operand_kinds(std::move(operand_kinds
))
426 json::ast::Value
to_json() const;
427 template <typename Fn
>
428 void visit(Fn fn
) const
431 for(auto &operand_kind
: operand_kinds
)
432 operand_kind
.visit(fn
);
439 std::uint32_t magic_number
;
440 std::size_t major_version
;
441 std::size_t minor_version
;
442 std::size_t revision
;
443 Instructions instructions
;
444 Operand_kinds operand_kinds
;
445 Top_level(Copyright copyright
,
446 std::uint32_t magic_number
,
447 std::size_t major_version
,
448 std::size_t minor_version
,
449 std::size_t revision
,
450 Instructions instructions
,
451 Operand_kinds operand_kinds
)
452 : copyright(std::move(copyright
)),
453 magic_number(magic_number
),
454 major_version(major_version
),
455 minor_version(minor_version
),
457 instructions(std::move(instructions
)),
458 operand_kinds(std::move(operand_kinds
))
461 json::ast::Value
to_json() const;
462 template <typename Fn
>
463 void visit(Fn fn
) const
467 instructions
.visit(fn
);
468 operand_kinds
.visit(fn
);
475 #endif /* GENERATE_SPIRV_PARSER_AST_H_ */