Blender V5.3
symbol_variable.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "symbol_variable.hh"
10#include "symbol_table.hh"
11
12#include <cmath>
13#include <iomanip>
14#include <limits>
15#include <sstream>
16
17namespace bsl {
18
19using namespace blender::gpu::shader::parser::ast;
20
24 const SymbolTable &table)
25 : Symbol(
26 string(decl.identifier().str()), string(decl.identifier().str()), decl.front(), parent),
27 type(type),
28 array_dimensions(decl.array().dimensions()),
29 is_bitfield(decl.bitfield().is_valid()),
31 is_compilation_const(AttrList(decl.type().prev()).contains_attr("compilation_constant"))
32{
33 if (ast::Const constant = decl.type().constant(); constant.is_valid()) {
34 is_constexpr = constant.front() == Constexpr;
35 is_const = true;
36 }
37 if (ArrayDecl array = decl.array(); array.is_valid()) {
38 Subscript sub = array.sub();
39 array_elements = 1;
40 for (int i = 0; i < array_dimensions; ++i, sub = sub.next()) {
41 Node node = sub.expr().child_first();
42 if (!node.is_valid()) {
43 array_elements = -1;
44 break;
45 }
46 auto [result, _] = table.expr_type_analysis(*parent, node);
47 if (!result.is_constexpr()) {
48 array_elements = -1;
49 break;
50 }
51 array_elements *= result.value.comp_as<int>(0);
52 }
53 }
54}
55
56void SymbolVariable::set_offset(bool is_union, int &offset)
57{
58 if (is_union) {
59 this->offset = 0;
60 }
61 else {
62 this->offset = pad(offset, this->type->align);
63 offset = this->offset;
64 int elements = (this->array_dimensions == 0) ? 1 : this->array_elements;
65 if (elements != -1) {
66 offset += this->type->size * elements;
67 }
68 else {
69 /* TODO(fclem): Maybe assert here. There is no reason to allow non-static array size. */
70 offset += this->type->size;
71 }
72 }
73}
74
75/* Can be null if in global space. */
80
81static std::string to_string_exact(float val)
82{
83 std::stringstream ss;
84 ss << std::setprecision(std::numeric_limits<float>::max_digits10) << val;
85 string str = ss.str();
86 /* Add trailing zero if there is no decimal. */
87 if (str.find_first_of(".eE") == std::string::npos) {
88 str += ".0";
89 }
90 return str + "f";
91}
92
94{
95 const int size = v.values.size();
96 const ConstexprValue::CompType comp_type = v.comp_type();
97 std::string s;
98
99 if (size > 1) {
100 switch (comp_type) {
102 s += "bool";
103 break;
105 s += "uint";
106 break;
108 s += "int";
109 break;
111 s += "float";
112 break;
113 }
114 s += std::to_string(size) + "(";
115 }
116
117 for (int i = 0; i < size; ++i) {
118 switch (comp_type) {
120 s += v.values[i] ? "true" : "false";
121 break;
123 s += std::to_string(v.values[i]) + "u";
124 break;
126 s += std::to_string(int32_t(v.values[i]));
127 break;
129 if (std::isnan(std::bit_cast<float>(v.values[i]))) {
130 s += "uintBitsToFloat(0x7FC00000u)";
131 }
132 else {
133 s += to_string_exact(std::bit_cast<float>(v.values[i]));
134 }
135 break;
136 }
137 if (size > 1 && i < size - 1) {
138 s += ", ";
139 }
140 }
141
142 if (size > 1) {
143 s += ")";
144 }
145
146 return s;
147}
148
150{
153}
154
155} // namespace bsl
int pad[32 - sizeof(int)]
#define str(s)
if(state< num_states)
ATTR_WARN_UNUSED_RESULT const BMVert * v
Definition codegen.cc:18
static std::string to_string_exact(float val)
std::string constexpr_to_string(const ConstexprValue &v)
SymbolClass * as_class()
string value_str() const
void set_offset(bool is_union, int &offset)
SymbolClass * parent_class()
SymbolVariable(SymbolScope *parent, SymbolClass *type, ast::Declarator decl, const SymbolTable &table)
SymbolScope * parent