1 // 2 // Copyright 2020 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 #ifndef COMPILER_TRANSLATOR_TREEUTIL_INTERMASNODE_H_ 8 #define COMPILER_TRANSLATOR_TREEUTIL_INTERMASNODE_H_ 9 10 #include "common/angleutils.h" 11 #include "compiler/translator/IntermNode.h" 12 13 #include <utility> 14 15 namespace sh 16 { 17 18 namespace priv 19 { 20 21 template <typename T> 22 struct AsNode 23 {}; 24 25 template <> 26 struct AsNode<TIntermNode> 27 { 28 static ANGLE_INLINE TIntermNode *exec(TIntermNode *node) { return node; } 29 }; 30 31 template <> 32 struct AsNode<TIntermTyped> 33 { 34 static ANGLE_INLINE TIntermTyped *exec(TIntermNode *node) 35 { 36 return node ? node->getAsTyped() : nullptr; 37 } 38 }; 39 40 template <> 41 struct AsNode<TIntermSymbol> 42 { 43 static ANGLE_INLINE TIntermSymbol *exec(TIntermNode *node) 44 { 45 return node ? node->getAsSymbolNode() : nullptr; 46 } 47 }; 48 49 template <> 50 struct AsNode<TIntermConstantUnion> 51 { 52 static ANGLE_INLINE TIntermConstantUnion *exec(TIntermNode *node) 53 { 54 return node ? node->getAsConstantUnion() : nullptr; 55 } 56 }; 57 58 template <> 59 struct AsNode<TIntermFunctionPrototype> 60 { 61 static ANGLE_INLINE TIntermFunctionPrototype *exec(TIntermNode *node) 62 { 63 return node ? node->getAsFunctionPrototypeNode() : nullptr; 64 } 65 }; 66 67 template <> 68 struct AsNode<TIntermPreprocessorDirective> 69 { 70 static ANGLE_INLINE TIntermPreprocessorDirective *exec(TIntermNode *node) 71 { 72 return node ? node->getAsPreprocessorDirective() : nullptr; 73 } 74 }; 75 76 template <> 77 struct AsNode<TIntermSwizzle> 78 { 79 static ANGLE_INLINE TIntermSwizzle *exec(TIntermNode *node) 80 { 81 return node ? node->getAsSwizzleNode() : nullptr; 82 } 83 }; 84 85 template <> 86 struct AsNode<TIntermBinary> 87 { 88 static ANGLE_INLINE TIntermBinary *exec(TIntermNode *node) 89 { 90 return node ? node->getAsBinaryNode() : nullptr; 91 } 92 }; 93 94 template <> 95 struct AsNode<TIntermUnary> 96 { 97 static ANGLE_INLINE TIntermUnary *exec(TIntermNode *node) 98 { 99 return node ? node->getAsUnaryNode() : nullptr; 100 } 101 }; 102 103 template <> 104 struct AsNode<TIntermTernary> 105 { 106 static ANGLE_INLINE TIntermTernary *exec(TIntermNode *node) 107 { 108 return node ? node->getAsTernaryNode() : nullptr; 109 } 110 }; 111 112 template <> 113 struct AsNode<TIntermIfElse> 114 { 115 static ANGLE_INLINE TIntermIfElse *exec(TIntermNode *node) 116 { 117 return node ? node->getAsIfElseNode() : nullptr; 118 } 119 }; 120 121 template <> 122 struct AsNode<TIntermSwitch> 123 { 124 static ANGLE_INLINE TIntermSwitch *exec(TIntermNode *node) 125 { 126 return node ? node->getAsSwitchNode() : nullptr; 127 } 128 }; 129 130 template <> 131 struct AsNode<TIntermCase> 132 { 133 static ANGLE_INLINE TIntermCase *exec(TIntermNode *node) 134 { 135 return node ? node->getAsCaseNode() : nullptr; 136 } 137 }; 138 139 template <> 140 struct AsNode<TIntermFunctionDefinition> 141 { 142 static ANGLE_INLINE TIntermFunctionDefinition *exec(TIntermNode *node) 143 { 144 return node ? node->getAsFunctionDefinition() : nullptr; 145 } 146 }; 147 148 template <> 149 struct AsNode<TIntermAggregate> 150 { 151 static ANGLE_INLINE TIntermAggregate *exec(TIntermNode *node) 152 { 153 return node ? node->getAsAggregate() : nullptr; 154 } 155 }; 156 157 template <> 158 struct AsNode<TIntermBlock> 159 { 160 static ANGLE_INLINE TIntermBlock *exec(TIntermNode *node) 161 { 162 return node ? node->getAsBlock() : nullptr; 163 } 164 }; 165 166 template <> 167 struct AsNode<TIntermGlobalQualifierDeclaration> 168 { 169 static ANGLE_INLINE TIntermGlobalQualifierDeclaration *exec(TIntermNode *node) 170 { 171 return node ? node->getAsGlobalQualifierDeclarationNode() : nullptr; 172 } 173 }; 174 175 template <> 176 struct AsNode<TIntermDeclaration> 177 { 178 static ANGLE_INLINE TIntermDeclaration *exec(TIntermNode *node) 179 { 180 return node ? node->getAsDeclarationNode() : nullptr; 181 } 182 }; 183 184 template <> 185 struct AsNode<TIntermLoop> 186 { 187 static ANGLE_INLINE TIntermLoop *exec(TIntermNode *node) 188 { 189 return node ? node->getAsLoopNode() : nullptr; 190 } 191 }; 192 193 template <> 194 struct AsNode<TIntermBranch> 195 { 196 static ANGLE_INLINE TIntermBranch *exec(TIntermNode *node) 197 { 198 return node ? node->getAsBranchNode() : nullptr; 199 } 200 }; 201 202 } // namespace priv 203 204 template <typename T> 205 ANGLE_INLINE T *asNode(TIntermNode *node) 206 { 207 return priv::AsNode<T>::exec(node); 208 } 209 210 } // namespace sh 211 212 #endif // COMPILER_TRANSLATOR_TREEUTIL_INTERMASNODE_H_ 213