1 //
2 // Copyright (C) 2016 Google, Inc.
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 //    Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 //
13 //    Redistributions in binary form must reproduce the above
14 //    copyright notice, this list of conditions and the following
15 //    disclaimer in the documentation and/or other materials provided
16 //    with the distribution.
17 //
18 //    Neither the name of Google, Inc., nor the names of its
19 //    contributors may be used to endorse or promote products derived
20 //    from this software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 // POSSIBILITY OF SUCH DAMAGE.
34 //
35 
36 // Map from physical token form (e.g. '-') to logical operator
37 // form (e.g., binary subtract or unary negate).
38 
39 #include "hlslOpMap.h"
40 
41 namespace glslang {
42 
43 // Map parsing tokens that could be assignments into assignment operators.
assignment(EHlslTokenClass op)44 TOperator HlslOpMap::assignment(EHlslTokenClass op)
45 {
46     switch (op) {
47     case EHTokAssign:      return EOpAssign;
48     case EHTokMulAssign:   return EOpMulAssign;
49     case EHTokDivAssign:   return EOpDivAssign;
50     case EHTokAddAssign:   return EOpAddAssign;
51     case EHTokModAssign:   return EOpModAssign;
52     case EHTokLeftAssign:  return EOpLeftShiftAssign;
53     case EHTokRightAssign: return EOpRightShiftAssign;
54     case EHTokAndAssign:   return EOpAndAssign;
55     case EHTokXorAssign:   return EOpExclusiveOrAssign;
56     case EHTokOrAssign:    return EOpInclusiveOrAssign;
57     case EHTokSubAssign:   return EOpSubAssign;
58 
59     default:
60         return EOpNull;
61     }
62 }
63 
64 // Map parsing tokens that could be binary operations into binary operators.
binary(EHlslTokenClass op)65 TOperator HlslOpMap::binary(EHlslTokenClass op)
66 {
67     switch (op) {
68     case EHTokPlus:        return EOpAdd;
69     case EHTokDash:        return EOpSub;
70     case EHTokStar:        return EOpMul;
71     case EHTokSlash:       return EOpDiv;
72     case EHTokPercent:     return EOpMod;
73     case EHTokRightOp:     return EOpRightShift;
74     case EHTokLeftOp:      return EOpLeftShift;
75     case EHTokAmpersand:   return EOpAnd;
76     case EHTokVerticalBar: return EOpInclusiveOr;
77     case EHTokCaret:       return EOpExclusiveOr;
78     case EHTokEqOp:        return EOpEqual;
79     case EHTokNeOp:        return EOpNotEqual;
80     case EHTokLeftAngle:   return EOpLessThan;
81     case EHTokRightAngle:  return EOpGreaterThan;
82     case EHTokLeOp:        return EOpLessThanEqual;
83     case EHTokGeOp:        return EOpGreaterThanEqual;
84     case EHTokOrOp:        return EOpLogicalOr;
85     case EHTokXorOp:       return EOpLogicalXor;
86     case EHTokAndOp:       return EOpLogicalAnd;
87 
88     default:
89         return EOpNull;
90     }
91 }
92 
93 // Map parsing tokens that could be unary operations into unary operators.
94 // These are just the ones that can appear in front of its operand.
preUnary(EHlslTokenClass op)95 TOperator HlslOpMap::preUnary(EHlslTokenClass op)
96 {
97     switch (op) {
98     case EHTokPlus:       return EOpAdd;        // means no-op, but still a unary op was present
99     case EHTokDash:       return EOpNegative;
100     case EHTokBang:       return EOpLogicalNot;
101     case EHTokTilde:      return EOpBitwiseNot;
102 
103     case EHTokIncOp:      return EOpPreIncrement;
104     case EHTokDecOp:      return EOpPreDecrement;
105 
106     default:              return EOpNull;       // means not a pre-unary op
107     }
108 }
109 
110 // Map parsing tokens that could be unary operations into unary operators.
111 // These are just the ones that can appear behind its operand.
postUnary(EHlslTokenClass op)112 TOperator HlslOpMap::postUnary(EHlslTokenClass op)
113 {
114     switch (op) {
115     case EHTokDot:         return EOpIndexDirectStruct;
116     case EHTokLeftBracket: return EOpIndexIndirect;
117 
118     case EHTokIncOp:       return EOpPostIncrement;
119     case EHTokDecOp:       return EOpPostDecrement;
120 
121     case EHTokColonColon:  return EOpScoping;
122 
123     default:               return EOpNull;             // means not a post-unary op
124     }
125 }
126 
127 // Map operators into their level of precedence.
precedenceLevel(TOperator op)128 PrecedenceLevel HlslOpMap::precedenceLevel(TOperator op)
129 {
130     switch (op) {
131     case EOpLogicalOr:
132         return PlLogicalOr;
133     case EOpLogicalXor:
134         return PlLogicalXor;
135     case EOpLogicalAnd:
136         return PlLogicalAnd;
137 
138     case EOpInclusiveOr:
139         return PlBitwiseOr;
140     case EOpExclusiveOr:
141         return PlBitwiseXor;
142     case EOpAnd:
143         return PlBitwiseAnd;
144 
145     case EOpEqual:
146     case EOpNotEqual:
147         return PlEquality;
148 
149     case EOpLessThan:
150     case EOpGreaterThan:
151     case EOpLessThanEqual:
152     case EOpGreaterThanEqual:
153         return PlRelational;
154 
155     case EOpRightShift:
156     case EOpLeftShift:
157         return PlShift;
158 
159     case EOpAdd:
160     case EOpSub:
161         return PlAdd;
162 
163     case EOpMul:
164     case EOpDiv:
165     case EOpMod:
166         return PlMul;
167 
168     default:
169         return PlBad;
170     }
171 }
172 
173 } // end namespace glslang
174