Blender V5.3
ast.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 "ast.hh"
10#include "token.hh"
11
13
14std::string NodeData::location(const ParserBase &parser) const
15{
16 std::string str = parser[front].filename();
17 str += ":" + std::to_string(parser[front].line_number());
18 str += ':' + std::to_string(parser[front].char_number() + 1);
19 return str;
20}
21
23{
24 return is_valid() ? Node{p, p->ast_nodes[id].prev} : Node{};
25}
26
28{
29 Node node = *this;
30 do {
31 node = node.prev();
32 } while (node.is_valid() && node.type() != type);
33 return node;
34}
35
37{
38 return is_valid() ? Node{p, p->ast_nodes[id].next} : Node{};
39}
40
42{
43 Node node = *this;
44 do {
45 node = node.next();
46 } while (node.is_valid() && node.type() != type);
47 return node;
48}
49
51{
52 return is_valid() ? Node{p, p->ast_nodes[id].parent} : Node{};
53}
54
56{
57 Node node = *this;
58 do {
59 node = node.parent();
60 } while (node.is_valid() && node.type() != type);
61 return node;
62}
63
65{
66 return p->ast_nodes[id].child_first == p->ast_nodes[id].child_last;
67}
68
70{
71 int count = 0;
72 for ([[maybe_unused]] Node node : this->children_range()) {
73 count++;
74 }
75 return count;
76}
77
79{
80 return is_valid() ? Node{p, p->ast_nodes[id].child_first} : Node{};
81}
82
84{
85 return is_valid() ? Node{p, p->ast_nodes[id].child_last} : Node{};
86}
87
88const NodeData *Node::data() const
89{
90 return is_valid() ? &p->ast_nodes[id] : nullptr;
91}
92
94{
95 return is_valid() ? p->ast_nodes[id].type : NodeType::Invalid;
96}
97
98bool Node::is_empty() const
99{
100 return p->ast_nodes[id].child_first == -1;
101}
102
104{
105 return is_valid() ? (*p)[p->ast_nodes[id].front] : Token::invalid(p);
106}
107
109{
110 return is_valid() ? (*p)[p->ast_nodes[id].back] : Token::invalid(p);
111}
112
113std::string_view Node::str() const
114{
115 return is_valid() ? p->substr(front(), back()) : "";
116}
117
118std::string_view Node::str_full() const
119{
120 return is_valid() ? p->substr(front(), back(), true) : "";
121}
122
124{
125 return back().index_ - front().index_ == 1;
126}
127
128std::string StructuredBinding::tmp_id() const
129{
130 return "_" + std::to_string(front().index_ - parent().front().index_);
131}
132
134{
135 return front() == Enum;
136}
137
139{
140 return front().next() == Class;
141}
142
144{
145 return front() == Union;
146}
147
149{
151 return IdQualified(node.front().prev() == Colon ? Node{} : node);
152}
153
155{
156 Node node = body().prev();
157 return IdQualified(node.front().prev() == Colon ? node : Node{});
158}
159
161{
162 return front().next() == TokenType::Namespace;
163}
164
166{
167 return is_const() && constant().front() == Constexpr;
168}
169
170static std::string to_string(ast::NodeType type);
171static bool display_type(ast::NodeType type);
172
173void Node::print_ast() const
174{
175 if (!is_valid()) {
176 return;
177 }
178
179 const ParserBase &parser = *p;
180 const Nodes &nodes = parser.ast_nodes;
181
182 /* Recursive lambda to handle tree traversal and indentation. */
183 auto print_node =
184 [&](auto &self, NodeID id, int depth, const std::vector<bool> &is_last) -> void {
185 if (id == -1 || id >= int(nodes.size())) {
186 return;
187 }
188
189 const auto &node = nodes[id];
190
191 std::string loc = node.location(parser);
192 /* Create indentation based on tree depth. */
193 int padding_size = std::max(0, 55 - int(loc.size()));
194 std::string padding(padding_size, ' ');
195
196 std::cout << loc << padding;
197
198 /* Print the tree branch lines based on depth and ancestor sibling status. */
199 for (int i = 0; i < depth - 1; ++i) {
200 if (is_last[i]) {
201 std::cout << " "; /* Ancestor was the last sibling, leave space. */
202 }
203 else {
204 std::cout << "│ "; /* Ancestor has more siblings, draw vertical line. */
205 }
206 }
207 /* Print the branch for the current node. */
208 if (depth > 0) {
209 if (is_last.back()) {
210 std::cout << "└─"; /* Last sibling. */
211 }
212 else {
213 std::cout << "├─"; /* Not the last sibling. */
214 }
215 }
216
217 /* Print the current node. */
218 std::cout << "o " << to_string(node.type);
219 if (display_type(node.type)) {
220 std::cout << " " << parser[node.front].str();
221 }
222 std::cout << "\n";
223
224 /* Traverse all children of this node. */
225 /* We start at child_first, then follow the 'next' sibling chain. */
226 NodeID child_id = node.child_first;
227 while (child_id != -1) {
228 /* Determine if this child is the last one in the sibling chain. */
229 bool child_is_last = (nodes[child_id].next == -1);
230
231 /* Pass the updated sibling history to the next depth. */
232 std::vector<bool> next_is_last = is_last;
233 next_is_last.push_back(child_is_last);
234
235 self(self, child_id, depth + 1, next_is_last);
236 child_id = nodes[child_id].next;
237 }
238 };
239
240 if (nodes[0].parent == -1) {
241 print_node(print_node, this->id, 0, {});
242 }
243}
244
245static std::string to_string(ast::NodeType type)
246{
247 switch (type) {
249 return "Invalid";
251 return "OpDeref";
253 return "AccessSpecifier";
255 return "StaticStmt";
257 return "TranslationUnit";
259 return "Preprocessor";
261 return "Namespace";
263 return "NamespaceSeparator";
265 return "ClassDecl";
267 return "IdType";
269 return "Const";
271 return "Reference";
273 return "EnumValue";
275 return "Id";
277 return "IdQualified";
279 return "AssignStmt";
281 return "VarDecl";
283 return "FuncDecl";
285 return "FuncArgList";
287 return "FuncArg";
289 return "FuncParamList";
291 return "LocalScope";
293 return "Subscript";
295 return "AttrList";
297 return "Attr";
299 return "Expr";
301 return "TemplateDecl";
303 return "TemplateArgList";
305 return "TemplateParamList";
307 return "TemplateSpec";
309 return "TemplateInst";
311 return "TemplateArg";
313 return "ReturnStmt";
315 return "ContinueStmt";
317 return "BreakStmt";
319 return "UsingStmt";
321 return "BitField";
323 return "FuncForwardDecl";
325 return "LocalStmt";
327 return "FuncCall";
329 return "LocalVar";
331 return "SwitchStmt";
333 return "SwitchCase";
335 return "ForLoop";
337 return "WhileLoop";
339 return "DoWhileLoop";
341 return "Condition";
343 return "IfStmt";
345 return "ElseIfStmt";
347 return "ElseStmt";
349 return "InitializerList";
351 return "Initializer";
353 return "DesignatedInitializer";
355 return "StringConst";
357 return "NumConst";
359 return "Op";
361 return "PipelineDecl";
363 return "Constructor";
365 return "TemplateExplicit";
367 return "Declarator";
369 return "ExprSub";
371 return "StructuredBinding";
373 return "ArrayDecl";
374 }
375 return "Error";
376}
377
379{
380 switch (type) {
382 return false;
384 return true;
386 return true;
388 return false;
390 return false;
392 return false;
394 return false;
396 return false;
398 return true;
400 return false;
402 return true;
404 return true;
406 return false;
408 return true;
410 return true;
412 return false;
414 return false;
416 return false;
418 return false;
420 return false;
422 return false;
424 return false;
426 return false;
428 return false;
430 return true;
432 return false;
434 return false;
436 return false;
438 return false;
440 return false;
442 return false;
444 return false;
446 return false;
448 return false;
450 return false;
452 return false;
454 return false;
456 return false;
458 return false;
460 return false;
462 return false;
464 return false;
466 return false;
468 return false;
470 return false;
472 return false;
474 return false;
476 return false;
478 return false;
480 return false;
482 return false;
484 return false;
486 return false;
488 return false;
490 return true;
492 return true;
494 return true;
496 return false;
498 return false;
500 return false;
502 return false;
504 return false;
506 return false;
507 }
508 return false;
509}
510
511} // namespace blender::gpu::shader::parser::ast
#define str(s)
static bool display_type(ast::NodeType type)
Definition ast.cc:378
std::vector< NodeData > Nodes
Definition ast.hh:94
static std::string to_string(ast::NodeType type)
Definition ast.cc:245
size_t line_number(const std::string_view &str, size_t pos)
size_t char_number(const std::string_view &str, size_t pos)
uint padding(uint offset, uint alignment)
@ Enum
Definition types.hh:106
@ Union
Definition types.hh:109
@ Colon
Definition types.hh:65
@ Constexpr
Definition types.hh:86
@ Class
Definition types.hh:118
std::vector< ast::NodeData > ast_nodes
std::string location(const ParserBase &parser) const
Definition ast.cc:14
ChildRange children_range() const
Definition ast.hh:334
std::string_view str_full() const
Definition ast.cc:118
std::string_view str() const
Definition ast.cc:113
const NodeData * data() const
Definition ast.cc:88
std::string_view substr(const Token &start, const Token &end, const bool with_trailing_whitespaces=false) const
Return the substring between the start and end tokens (included).
Definition lexit.hh:224
int32_t index_
Definition lexit.hh:51
static Token invalid(const TokenBuffer *buf)
Definition lexit.hh:349
Token next(int i=1) const
Definition lexit.hh:372