Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 1 | // 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 Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 18 | #include <string> |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 21 | #include "src/ast/node.h" |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 22 | |
| 23 | namespace tint { |
| 24 | namespace ast { |
| 25 | |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 26 | /// The base class for all decorations |
Ben Clayton | e319d7f | 2020-11-30 23:30:58 +0000 | [diff] [blame] | 27 | class Decoration : public Castable<Decoration, Node> { |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 28 | public: |
Ben Clayton | 6f58546 | 2020-11-13 21:43:58 +0000 | [diff] [blame] | 29 | ~Decoration() override; |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 30 | |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 31 | /// @returns the WGSL name for the decoration |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 32 | virtual std::string Name() const = 0; |
Ben Clayton | 241c16d | 2021-06-09 18:53:57 +0000 | [diff] [blame] | 33 | |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 34 | protected: |
| 35 | /// Constructor |
Ben Clayton | 4f3ff57 | 2021-10-15 17:33:10 +0000 | [diff] [blame] | 36 | /// @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 Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 41 | /// A list of decorations |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 42 | using DecorationList = std::vector<const Decoration*>; |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 43 | |
James Price | a12ccb2 | 2021-04-08 21:53:27 +0000 | [diff] [blame] | 44 | /// @param decorations the list of decorations to search |
| 45 | /// @returns true if `decorations` includes a decoration of type `T` |
| 46 | template <typename T> |
| 47 | bool 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. |
| 58 | template <typename T> |
Ben Clayton | 8648120 | 2021-10-19 18:38:54 +0000 | [diff] [blame] | 59 | const T* GetDecoration(const DecorationList& decorations) { |
James Price | a12ccb2 | 2021-04-08 21:53:27 +0000 | [diff] [blame] | 60 | for (auto* deco : decorations) { |
| 61 | if (deco->Is<T>()) { |
| 62 | return deco->As<T>(); |
| 63 | } |
| 64 | } |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
Ben Clayton | 3529880 | 2020-11-03 21:40:20 +0000 | [diff] [blame] | 68 | } // namespace ast |
| 69 | } // namespace tint |
| 70 | |
| 71 | #endif // SRC_AST_DECORATION_H_ |