ShaderCompiler/ConstFold.h file

GLSL expression tokenizer, precedence-climbing parser and literal constant folder for ShaderCompiler.

The folder rewrites multi-token literal-only subexpressions into a single literal with EXACT GLSL semantics: int and float never mix (a mixed expression such as "1 / 255.0" is left untouched), int arithmetic folds only while the result stays in 32-bit signed range (overflow simply skips the fold), int division/modulo truncate toward zero, and floats are computed in double and emitted only when the "%.9g" text round-trips to the exact same value (always containing '.' or 'e', never longer than the original expression). Unsigned ("2u") and suffixed ("1.0f") literals are treated as opaque and never fold. Nothing containing identifiers, function calls, swizzles or indexing folds — but constructor/call ARGUMENTS fold individually ("vec2(1.0 + 2.0, 3.0)" becomes "vec2(3.0, 3.0)") and the parser models the full operator-precedence structure, so a literal run that associates with an opaque operand ("x - 1 + 2") is correctly left alone.

The input is the comment-stripped text of one fold unit (typically a function body) as a list of line ranges. Preprocessor directive lines act as barriers: tokens never combine across them, and a conditional that splits a statement suppresses folding until the next statement boundary in every affected branch, so alternative "#ifdef" branches can never lend each other a bogus parse context. The computed edits never span multiple lines.

The tokenizer/parser is deliberately reusable — it is the seed of the expression AST that future non-GLSL emitters (D3D11/software) will grow from.

Namespaces

namespace ShaderCompiler

Classes

struct ShaderCompiler::GlslToken
One GLSL expression token with its position in the source line it came from.
struct ShaderCompiler::FoldInputLine
One input line range of a fold unit (positions must be valid in the rewritable original text).
struct ShaderCompiler::FoldEdit
One computed rewrite: columns [Begin, End) of line Index collapse to Replacement.
class ShaderCompiler::GlslExprTokenizer
GLSL expression tokenizer (reusable outside the folder).
class ShaderCompiler::ConstFolder
Computes literal constant-folding rewrites for one fold unit.

Enums

enum class GlslTokenType : std::uint8_t { Identifier, IntLiteral, UIntLiteral, FloatLiteral, BoolLiteral, Operator, End }
Classification of one GLSL expression token.