Skip to content

Commit 6cab4b4

Browse files
authored
[Expr.Post.VectorSwizzle] Vector member swizzles (#682)
This PR adds specification language for vector member swizzles to clarify how swizzles are parsed, how the fit in the grammar, what the component accesses mean, and whether the expression produces an lvalue or a prvalue. Rendering of the grammar: <img width="635" height="623" alt="Screenshot 2025-10-14 at 3 14 39 PM" src="https://github.com/user-attachments/assets/3c3feb5b-fd31-47d5-863f-7166a71d6fda" />
1 parent 0187a91 commit 6cab4b4

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

specs/language/expressions.tex

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
simple-type-specifier braced-init-list\br
153153
typename-specifier braced-init-list\br
154154
postfix-expression \terminal{.} \opt{\terminal{template}} id-expression\br
155-
postfix-expression \terminal{->} \opt{\terminal{template}} id-expression\br
155+
postfix-expression \terminal{.} vector-swizzle-component-sequence\br
156156
postfix-expression \terminal{++}\br
157157
postfix-expression \terminal{--}
158158
\end{grammar}
@@ -167,6 +167,69 @@
167167
(\ref{Overload}).\footnote{HLSL does not support the base address of a subscript
168168
operator being the expression inside the braces, which is valid in C and C++.}
169169

170+
\p If the postfix expression \texttt{E1} is of vector type, the expression
171+
\textit{E2} must be a value of integer type. If the value is known at compile
172+
time to be outside the range \texttt{[0, N-1]} where \texttt{N} is the number of
173+
elements in the vector, the program is ill-formed. If the value is outside the
174+
range at runtime, the behavior is undefined.
175+
176+
\Sec{Vector Swizzle}{Expr.Post.VectorSwizzle}
177+
178+
\begin{grammar}
179+
\define{vector-swizzle-component-sequence}\br
180+
swizzle-component-sequence-rgba\br
181+
swizzle-component-sequence-xyzw\br
182+
183+
\define{swizzle-component-sequence-rgba}\br
184+
swizzle-component-rgba\br
185+
swizzle-component-sequence-rgba swizzle-component-rgba\br
186+
187+
\define{swizzle-component-sequence-xyzw}\br
188+
swizzle-component-xyzw\br
189+
swizzle-component-sequence-xyzw swizzle-component-xyzw\br
190+
191+
\define{swizzle-component-rgba} \textnormal{one of}\br
192+
\terminal{r g b a}\br
193+
194+
\define{swizzle-component-xyzw} \textnormal{one of}\br
195+
\terminal{x y z w}
196+
\end{grammar}
197+
198+
\p A \textit{postfix-expression} followed by a dot (\texttt{.}) and a sequence
199+
of one or more \textit{swizzle-components} is a postfix expression. The
200+
postfix expression before the dot is evaluated and must be of vector type. If
201+
the postfix expression before the dot is an lvalue, the swizzle expression may
202+
produce either an lvalue or a prvalue; otherwise it produces a prvalue.
203+
204+
\p A \textit{swizzle-component-sequence} is a sequence of one or more swizzle
205+
components of either the \textit{swizzle-component-rgba} or
206+
\textit{swizzle-component-xyzw} forms; the two forms may not be mixed in the
207+
same \textit{swizzle-component-sequence}. The type of a swizzle expression is a
208+
vector of the same element type as the postfix expression before the dot, with a
209+
number of elements equal to the number of components in the
210+
\textit{swizzle-component-sequence}. If the postfix expression before the dot is
211+
an lvalue and the \textit{swizzle-component-sequence} contains no repeated
212+
components, the swizzle expression is an lvalue; otherwise it is a prvalue.
213+
214+
\p Swizzle components map to elements of the vector in the following way:
215+
216+
\begin{center}
217+
\begin{tabular}{|| c | c | c ||}
218+
\hline
219+
Element Index & RGBA component & XYZW component \\
220+
\hline
221+
0 & r & x \\
222+
1 & g & y \\
223+
2 & b & z \\
224+
3 & a & w \\
225+
\hline
226+
\end{tabular}
227+
\end{center}
228+
229+
\p A program is ill-formed if any component in the \textit{swizzle-component-sequence}
230+
refers to an element index that is out of range of the vector type of the
231+
postfix expression before the dot.
232+
170233
\Sec{Function Calls}{Expr.Post.Call}
171234

172235
\p A function call may be an \textit{ordinary function}, or a \textit{member

0 commit comments

Comments
 (0)