blob: c3c925889758050c41975f6e2d3ef45e4ae7a89b [file] [log] [blame]
Ben Clayton35298802020-11-03 21:40:20 +00001// Copyright 2020 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#ifndef SRC_AST_DECORATION_H_
16#define SRC_AST_DECORATION_H_
17
Ben Clayton241c16d2021-06-09 18:53:57 +000018#include <string>
Ben Clayton35298802020-11-03 21:40:20 +000019#include <vector>
20
Ben Clayton6f585462020-11-13 21:43:58 +000021#include "src/ast/node.h"
Ben Clayton35298802020-11-03 21:40:20 +000022
23namespace tint {
24namespace ast {
25
Ben Clayton35298802020-11-03 21:40:20 +000026/// The base class for all decorations
Ben Claytone319d7f2020-11-30 23:30:58 +000027class Decoration : public Castable<Decoration, Node> {
Ben Clayton35298802020-11-03 21:40:20 +000028 public:
Ben Clayton6f585462020-11-13 21:43:58 +000029 ~Decoration() override;
Ben Clayton35298802020-11-03 21:40:20 +000030
Ben Clayton241c16d2021-06-09 18:53:57 +000031 /// @returns the WGSL name for the decoration
Ben Clayton4f3ff572021-10-15 17:33:10 +000032 virtual std::string Name() const = 0;
Ben Clayton241c16d2021-06-09 18:53:57 +000033
Ben Clayton35298802020-11-03 21:40:20 +000034 protected:
35 /// Constructor
Ben Clayton4f3ff572021-10-15 17:33:10 +000036 /// @param pid the identifier of the program that owns this node
37 /// @param src the source of this node
38 Decoration(ProgramID pid, const Source& src) : Base(pid, src) {}
Ben Clayton35298802020-11-03 21:40:20 +000039};
40
Ben Claytonb053acf2020-11-16 16:31:07 +000041/// A list of decorations
Ben Clayton86481202021-10-19 18:38:54 +000042using DecorationList = std::vector<const Decoration*>;
Ben Clayton35298802020-11-03 21:40:20 +000043
James Pricea12ccb22021-04-08 21:53:27 +000044/// @param decorations the list of decorations to search
45/// @returns true if `decorations` includes a decoration of type `T`
46template <typename T>
47bool HasDecoration(const DecorationList& decorations) {
48 for (auto* deco : decorations) {
49 if (deco->Is<T>()) {
50 return true;
51 }
52 }
53 return false;
54}
55
56/// @param decorations the list of decorations to search
57/// @returns a pointer to `T` from `decorations` if found, otherwise nullptr.
58template <typename T>
Ben Clayton86481202021-10-19 18:38:54 +000059const T* GetDecoration(const DecorationList& decorations) {
James Pricea12ccb22021-04-08 21:53:27 +000060 for (auto* deco : decorations) {
61 if (deco->Is<T>()) {
62 return deco->As<T>();
63 }
64 }
65 return nullptr;
66}
67
Ben Clayton35298802020-11-03 21:40:20 +000068} // namespace ast
69} // namespace tint
70
71#endif // SRC_AST_DECORATION_H_