From b3c51fb94d89888ef00170d1d82fab061dce2fae Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Mon, 8 Sep 2025 19:52:29 -0500 Subject: [PATCH 1/7] Array accesses and bounds This adds language documenting that out of bounds accesses for arrays is undefined behavior, and that zero-sized arrays are disallowed. This also restricts arrays of unknown size to strictly global resource declarations but allows omitting the array bound if it can be inferred from an initiailzer. Resolves #141 --- specs/language/declarations.tex | 62 +++++++++++++++++++++++++++++++-- specs/language/expressions.tex | 5 +++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index 342b6adc9..9bc778585 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -19,14 +19,14 @@ \textit{template-declaration}\br \textit{type-alias-declaration}\br ... - + \define{special-declaration}\br \textit{export-declaration-group}\br \textit{cbuffer-declaration-group}\br ... \define{empty-declaration} \terminal{;} - + \end{grammar} \Sec{Specifiers}{Decl.Spec} @@ -56,6 +56,64 @@ \p If a function is declared with an \texttt{export} specifier then all redeclarations of the same function must also use the \texttt{export} specifier or be part of \textit{export-declaration-group} (\ref{Decl.Export}). \Sec{Declarators}{Decl.Decl} + +\begin{grammar} + \define{init-declarator-list}\br + \textit{init-declarator}\br + \textit{init-declarator-list} \terminal{,} \textit{init-declarator}\br + + \define{init-declarator}\br + \textit{declarator} \opt{initializer}\br + + \define{declarator}\br + \textit{declarator-id} \opt{attribute-specifier-seq}\br + \textit{declarator} \textit{parameters-and-qualifiers}\br + \textit{declarator} \terminal{[} \opt{constant-expression} \terminal{]} \opt{attribute-specifier-seq}\br + + \define{parameters-and-qualifiers}\br + \terminal{(} \textit{parameter-declaration-clause} \terminal{)} \opt{cv-qualifier-seq} \opt{attribute-specifier-seq}\br + + \define{cv-qualifier-seq}\br + \textit{cv-qualifier} \opt{cv-qualifier-seq}\br + + \define{cv-qualifier}\br + \terminal{const}\br + + \define{declarator-id}\br + \textit{id-expression}\br + +\end{grammar} + +\Sub{Arrays}{Decl.arrays} +% This might need to nest deeper as we flesh out the sections. + +\p A declaration \texttt{T D} where \texttt{D} is of the form + +\begin{grammar} +\textit{D} \terminal{[} \textit{N} \terminal{]} \opt{attribute-specifier-seq}\br +\end{grammar} + +declares an array of \texttt{N} \texttt{T}, where \texttt{N} is the \textit{array +bound}, and \texttt{T} is the contained type of the array. \texttt{N} shall be a +constant expression converted to unsigned integer type and shall have a value +greater than zero. + +\p A declaration \texttt{T D} where \texttt{D} is of the form + +\begin{grammar} +\textit{D} \terminal{[} \terminal{]} \opt{attribute-specifier-seq}\br +\end{grammar} + +declares an array of unknown bound, except when the bound is implicitly derived +as described below. A declaration may only declare an array of unknown bound for +global declarations of resource objects (\ref{Resources}). + +\p A declarator with an omitted array bound is valid if the when declaring an +object of array type which is not a non-static data member and is initialized +with an initializer that follows the declarator. In this case the array bound is +implied by the number of elements provided by the initializer list following the +rules described in \ref{Decl.Init}. + \Sec{Initializers}{Decl.Init} \p The process of initialization described in this section applies to all diff --git a/specs/language/expressions.tex b/specs/language/expressions.tex index 3f94dc1fa..082b7d10c 100644 --- a/specs/language/expressions.tex +++ b/specs/language/expressions.tex @@ -167,6 +167,11 @@ (\ref{Overload}).\footnote{HLSL does not support the base address of a subscript operator being the expression inside the braces, which is valid in C and C++.} +\p If the base object is of array, vector or matrix type, the index expression +must be of integer type or of a type that implicitly converts to integer type. +If the index expression evaluates to a value that is out-of-range for the base +object, the behavior is undefined. + \Sec{Function Calls}{Expr.Post.Call} \p A function call may be an \textit{ordinary function}, or a \textit{member From 62999a9b83e2c248a7e8fd56f7d5ccc58926f6c4 Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 16 Sep 2025 12:57:20 -0500 Subject: [PATCH 2/7] Clarify spec around contained types and array declarator nesting --- specs/language/declarations.tex | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index 9bc778585..cc6f3e40b 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -87,27 +87,37 @@ \Sub{Arrays}{Decl.arrays} % This might need to nest deeper as we flesh out the sections. -\p A declaration \texttt{T D} where \texttt{D} is of the form +\p A declaration \textit{T D} where \textit{D} is of the form \begin{grammar} -\textit{D} \terminal{[} \textit{N} \terminal{]} \opt{attribute-specifier-seq}\br +\textit{D`} \terminal{[} \textit{N} \terminal{]} \opt{attribute-specifier-seq}\br \end{grammar} -declares an array of \texttt{N} \texttt{T}, where \texttt{N} is the \textit{array -bound}, and \texttt{T} is the contained type of the array. \texttt{N} shall be a -constant expression converted to unsigned integer type and shall have a value -greater than zero. +declares an array of \textit{N} \textit{T`}, where \textit{N} is the +\textit{array bound}, and \textit{T`} is the derived type of the declarator +\textit{D`} and is called the \textit{contained type} of the array. \textit{N} +shall be a constant expression converted to unsigned integer type and shall have +a value greater than zero. -\p A declaration \texttt{T D} where \texttt{D} is of the form +\p A declaration \textit{T D} where \textit{D} is of the form \begin{grammar} -\textit{D} \terminal{[} \terminal{]} \opt{attribute-specifier-seq}\br +\textit{D`} \terminal{[} \terminal{]} \opt{attribute-specifier-seq}\br \end{grammar} declares an array of unknown bound, except when the bound is implicitly derived as described below. A declaration may only declare an array of unknown bound for global declarations of resource objects (\ref{Resources}). +\p The contained type of an array of either form shall be a complete type. + +\begin{note} + An array of arrays may be declared with infinite nesting of array declarators + as long as either all array bounds are known, or only the outermost array + bound is unknown. This is codified in the spec above with the requirement that + the contained type of an array be a complete type. +\end{note} + \p A declarator with an omitted array bound is valid if the when declaring an object of array type which is not a non-static data member and is initialized with an initializer that follows the declarator. In this case the array bound is From fdb5021123713ed330ac40c17696e4066773746d Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Mon, 10 Nov 2025 19:31:55 -0600 Subject: [PATCH 3/7] Fix grammar --- specs/language/declarations.tex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index cc6f3e40b..fe8c7b712 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -118,11 +118,11 @@ the contained type of an array be a complete type. \end{note} -\p A declarator with an omitted array bound is valid if the when declaring an -object of array type which is not a non-static data member and is initialized -with an initializer that follows the declarator. In this case the array bound is -implied by the number of elements provided by the initializer list following the -rules described in \ref{Decl.Init}. +\p A declarator with an omitted array bound is valid when declaring an object of +array type which is not a non-static data member and is initialized with an +initializer that follows the declarator. In this case the array bound is implied +by the number of elements provided by the initializer list following the rules +described in \ref{Decl.Init}. \Sec{Initializers}{Decl.Init} From 9f43a90127e4fa64aca0f026b27e5553585f5ca5 Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Mon, 10 Nov 2025 20:07:16 -0600 Subject: [PATCH 4/7] Trying to clarify wording around array nesting. --- specs/language/declarations.tex | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index fe8c7b712..315db52a4 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -109,15 +109,21 @@ as described below. A declaration may only declare an array of unknown bound for global declarations of resource objects (\ref{Resources}). -\p The contained type of an array of either form shall be a complete type. +\p Array declarations may be nested to form multidimensional arrays. When +declaring a multidimensional array, only the first (outermost) dimension may +have an unknown bound; all inner dimensions must have known bounds. \begin{note} - An array of arrays may be declared with infinite nesting of array declarators - as long as either all array bounds are known, or only the outermost array - bound is unknown. This is codified in the spec above with the requirement that - the contained type of an array be a complete type. + For example the declaration \texttt{int A[3][4];} declares \texttt{A} as an + array of 3 elements, each of which is an array of 4 integers. + + Applying this example to the above grammar, \texttt{T} is \texttt{int}, + \texttt{D} is \texttt{A[3][4]}, \texttt{D'} is \texttt{A[3]}, \texttt{N} is 3, + and \texttt{T'} is \texttt{int[4]}. \end{note} +\p The contained type of an array of any form shall be a complete type. + \p A declarator with an omitted array bound is valid when declaring an object of array type which is not a non-static data member and is initialized with an initializer that follows the declarator. In this case the array bound is implied From d4df15eff8e1519477946427095b198bf32ed2b3 Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 20 Jan 2026 10:30:40 -0600 Subject: [PATCH 5/7] Fixing formatting of grammar --- specs/language/declarations.tex | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index b5d47a0ab..5e28b9f0f 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -68,28 +68,28 @@ \begin{grammar} \define{init-declarator-list}\br - \textit{init-declarator}\br - \textit{init-declarator-list} \terminal{,} \textit{init-declarator}\br + init-declarator\br + init-declarator-list \terminal{,} init-declarator\br \define{init-declarator}\br - \textit{declarator} \opt{initializer}\br + declarator \opt{initializer}\br \define{declarator}\br - \textit{declarator-id} \opt{attribute-specifier-seq}\br - \textit{declarator} \textit{parameters-and-qualifiers}\br - \textit{declarator} \terminal{[} \opt{constant-expression} \terminal{]} \opt{attribute-specifier-seq}\br + declarator-id \opt{attribute-specifier-seq}\br + declarator parameters-and-qualifiers\br + declarator \terminal{[} \opt{constant-expression} \terminal{]} \opt{attribute-specifier-seq}\br \define{parameters-and-qualifiers}\br - \terminal{(} \textit{parameter-declaration-clause} \terminal{)} \opt{cv-qualifier-seq} \opt{attribute-specifier-seq}\br + \terminal{(} parameter-declaration-clause \terminal{)} \opt{cv-qualifier-seq} \opt{attribute-specifier-seq}\br \define{cv-qualifier-seq}\br - \textit{cv-qualifier} \opt{cv-qualifier-seq}\br + cv-qualifier \opt{cv-qualifier-seq}\br \define{cv-qualifier}\br \terminal{const}\br \define{declarator-id}\br - \textit{id-expression}\br + id-expression\br \end{grammar} From 42ddf788894a99b91d69326f855bfd4c21ba465a Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 20 Jan 2026 11:23:20 -0600 Subject: [PATCH 6/7] Pandoc doesn't like literal `[` and `]` characters --- specs/language/declarations.tex | 51 ++------------------------------- 1 file changed, 3 insertions(+), 48 deletions(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index 5e28b9f0f..0523a90fc 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -1,4 +1,3 @@ - \Ch{Declarations}{Decl} \Sec{Preamble}{Decl.Pre} \p Declarations generally specify how names are to be interpreted. Declarations have the form @@ -53,7 +52,7 @@ \begin{grammar} \define{function-specifier}\br - \texttt{export}\br + export \end{grammar} \p The \texttt{export} specifier denotes that the function has program linkage (\ref{Basic.Linkage.Program}). @@ -77,7 +76,7 @@ \define{declarator}\br declarator-id \opt{attribute-specifier-seq}\br declarator parameters-and-qualifiers\br - declarator \terminal{[} \opt{constant-expression} \terminal{]} \opt{attribute-specifier-seq}\br + declarator \terminal{\lbrack} \opt{constant-expression} \terminal{\rbrack} \opt{attribute-specifier-seq}\br \define{parameters-and-qualifiers}\br \terminal{(} parameter-declaration-clause \terminal{)} \opt{cv-qualifier-seq} \opt{attribute-specifier-seq}\br @@ -89,55 +88,11 @@ \terminal{const}\br \define{declarator-id}\br - id-expression\br - -\end{grammar} - -\Sub{Arrays}{Decl.arrays} -% This might need to nest deeper as we flesh out the sections. + id-expression -\p A declaration \textit{T D} where \textit{D} is of the form - -\begin{grammar} -\textit{D`} \terminal{[} \textit{N} \terminal{]} \opt{attribute-specifier-seq}\br \end{grammar} -declares an array of \textit{N} \textit{T`}, where \textit{N} is the -\textit{array bound}, and \textit{T`} is the derived type of the declarator -\textit{D`} and is called the \textit{contained type} of the array. \textit{N} -shall be a constant expression converted to unsigned integer type and shall have -a value greater than zero. - -\p A declaration \textit{T D} where \textit{D} is of the form - -\begin{grammar} -\textit{D`} \terminal{[} \terminal{]} \opt{attribute-specifier-seq}\br -\end{grammar} - -declares an array of unknown bound, except when the bound is implicitly derived -as described below. A declaration may only declare an array of unknown bound for -global declarations of resource objects (\ref{Resources}). - -\p Array declarations may be nested to form multidimensional arrays. When -declaring a multidimensional array, only the first (outermost) dimension may -have an unknown bound; all inner dimensions must have known bounds. - -\begin{note} - For example the declaration \texttt{int A[3][4];} declares \texttt{A} as an - array of 3 elements, each of which is an array of 4 integers. - - Applying this example to the above grammar, \texttt{T} is \texttt{int}, - \texttt{D} is \texttt{A[3][4]}, \texttt{D'} is \texttt{A[3]}, \texttt{N} is 3, - and \texttt{T'} is \texttt{int[4]}. -\end{note} - -\p The contained type of an array of any form shall be a complete type. -\p A declarator with an omitted array bound is valid when declaring an object of -array type which is not a non-static data member and is initialized with an -initializer that follows the declarator. In this case the array bound is implied -by the number of elements provided by the initializer list following the rules -described in \ref{Decl.Init}. \Sec{Initializers}{Decl.Init} From 44a8faca0dd421d10766bf973aabc5cb51b2404f Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 20 Jan 2026 11:24:28 -0600 Subject: [PATCH 7/7] Use \terminal for `export` --- specs/language/declarations.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/language/declarations.tex b/specs/language/declarations.tex index 0523a90fc..46e8c5983 100644 --- a/specs/language/declarations.tex +++ b/specs/language/declarations.tex @@ -52,7 +52,7 @@ \begin{grammar} \define{function-specifier}\br - export + \terminal{export} \end{grammar} \p The \texttt{export} specifier denotes that the function has program linkage (\ref{Basic.Linkage.Program}).