Blender V5.3
scope.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#pragma once
11
12#include "token.hh"
13#include "token_stream.hh"
14
15#include <cassert>
16
18
19struct ScopeParser;
20
21struct Scope {
23
24 private:
25#ifndef NDEBUG
26 /* String view for nicer debugging experience. Isn't actually used. */
27 std::string_view token_view_;
28 std::string_view str_view_;
29#endif
30 /* Parser in which the scope resides. */
31 const ParserBase *parser_;
32 /* Scope index. */
33 int64_t index_;
34
35 public:
36 Scope() = delete;
37
38 Scope(const ParserBase &parser, int64_t index) : parser_(&parser), index_(index)
39 {
40 if (index < 0 || index >= parser.scope_types.size()) {
41 index = parser.scope_types.size();
42 return;
43 }
44#ifndef NDEBUG
45 IndexRange index_range = parser.scope_ranges[index];
46 token_view_ = parser_->token_types_str().substr(index_range.start, index_range.size);
47 str_view_ = parser_->substr((*parser_)[index_range.start], (*parser_)[index_range.last()]);
48#endif
49 }
50
51 /* Create an invalid scope. */
53
54 bool is_valid() const
55 {
56 return index_ < parser_->scope_types.size();
57 }
58 bool is_invalid() const
59 {
60 return index_ >= parser_->scope_types.size();
61 }
62
64 {
65 return is_invalid() ? Token(*parser_) : Token(*parser_, range().start + i);
66 }
67
68 /* Return first token of that scope. */
69 Token front() const
70 {
71 return is_invalid() ? Token(*parser_) : Token(*parser_, range().start);
72 }
73
74 /* Return last token of that scope. */
75 Token back() const
76 {
77 return is_invalid() ? Token(*parser_) : Token(*parser_, range().last());
78 }
79
81 {
82 return is_invalid() ? IndexRange(0, 0) : parser_->scope_ranges[index_];
83 }
84
86 {
87 return Token(*parser_, range().start + index);
88 }
89
90 size_t token_count() const
91 {
92 return is_invalid() ? 0 : range().size;
93 }
94
96 {
97 return is_invalid() ? ScopeType::Invalid : ScopeType(parser_->scope_types[index_]);
98 }
99
100 /* WORKAROUND: Only used for semantic tagging of scopes after parsing pass.
101 * The type is only retained until the next parsing pass. */
103 {
104 const_cast<ParserBase *>(parser_)->scope_types[index_] = type;
105 }
106
107 /* Returns the scope that contains this scope. */
108 Scope scope() const
109 {
110 if (is_invalid()) {
111 return Scope(*parser_);
112 }
113 const size_t scope_start = this->front().str_index_start();
114 Scope scope = *this;
115 while ((scope = scope.prev()).is_valid()) {
116 if (scope.back().str_index_last() > scope_start) {
117 return scope;
118 }
119 }
120 return scope;
121 }
122
123 /* Returns the parent node.
124 * Equivalent to scope(). Should ultimately replace it. */
125 Scope parent() const
126 {
127 if (is_invalid()) {
128 return Scope(*parser_);
129 }
130 return Scope(*parser_, parser_->scope_links[index_].parent_);
131 }
132
134 {
135 if (is_invalid()) {
136 return Scope(*parser_);
137 }
138 return Scope(*parser_, parser_->scope_links[index_].prev_);
139 }
140
142 {
143 if (is_invalid()) {
144 return Scope(*parser_);
145 }
146 return Scope(*parser_, parser_->scope_links[index_].next_);
147 }
148
150 {
151 if (is_invalid()) {
152 return Scope(*parser_);
153 }
154 return Scope(*parser_, parser_->scope_links[index_].child_first_);
155 }
156
157 /* Returns the previous scope before this scope. Can be either the container scope or the
158 * previous scope inside the same container. */
159 Scope prev() const
160 {
161 return is_invalid() ? Scope(*parser_) : front().prev().scope();
162 }
163
164 /* Returns the next scope after this scope. Can be either the container scope or the next scope
165 * inside the same container. */
166 Scope next() const
167 {
168 return is_invalid() ? Scope(*parser_) : back().next().scope();
169 }
170
171 bool contains(const Scope sub) const
172 {
173 Scope parent = sub.scope();
174 while (parent.type() != ScopeType::Global && parent != *this) {
175 parent = parent.scope();
176 }
177 return parent == *this;
178 }
179
180 /* Returns true if scope contains the sub-string. */
181 bool contains(const std::string &str) const
182 {
183 return this->str().find(str) != std::string::npos;
184 }
185
186 std::string_view str_with_whitespace() const
187 {
188 if (this->is_invalid()) {
189 return "";
190 }
191 return parser_->substr(front(), back(), true);
192 }
193
194 std::string_view str() const
195 {
196 if (this->is_invalid()) {
197 return "";
198 }
199 return parser_->substr(front(), back(), false);
200 }
201
202 /* Return the content without the first and last token. */
203 std::string_view str_exclusive() const
204 {
205 if (this->is_invalid() || this->token_count() <= 2) {
206 return "";
207 }
208 return parser_->substr(front().next(), back().prev(), false);
209 }
210
211 /* Return first occurrence of token_type inside this scope. */
212 Token find_token(const char token_type) const
213 {
214 if (this->is_invalid()) {
215 return Token(*parser_);
216 }
217 size_t pos = parser_->token_types_str().substr(range().start, range().size).find(token_type);
218 return (pos != std::string::npos) ? Token(*parser_, range().start + pos) : Token(*parser_);
219 }
220
221 bool contains_token(const char token_type) const
222 {
223 return find_token(token_type).is_valid();
224 }
225
226 /* Return the first container scope that has the given type (including itself).
227 * Returns invalid scope on failure. */
229 {
230 Scope scope = *this;
231 while (scope.type() != ScopeType::Global && scope.type() != type) {
232 scope = scope.scope();
233 }
234 return scope.type() == type ? scope : Scope(*parser_);
235 }
236
254 template<bool include_preprocessor = false, typename CallbackFn>
255 void foreach_match(const std::string &pattern, CallbackFn callback) const
256 {
257 assert(!pattern.empty());
258 if (this->is_invalid()) {
259 return;
260 }
261
262 const std::string_view scope_tokens = parser_->token_types_str().substr(range().start,
263 range().size);
264
265 auto count_match = [](const std::string_view &s, const std::string_view &pattern) {
266 size_t pos = 0, occurrences = 0;
267 while ((pos = s.find(pattern, pos)) != std::string::npos) {
268 occurrences += 1;
269 pos += pattern.length();
270 }
271 return occurrences;
272 };
273 const int control_token_count = count_match(pattern, "?") * 2 + count_match(pattern, "..") * 2;
274
275 if (range().size < pattern.size() - control_token_count) {
276 return;
277 }
278
279 const size_t searchable_range = scope_tokens.size() -
280 (pattern.size() - 1 - control_token_count);
281
282 std::vector<Token> match(pattern.size(), Token(*parser_));
283
284 for (size_t pos = 0; pos < searchable_range; pos++) {
285 size_t cursor = range().start + pos;
286
287 for (int i = 0; i < pattern.size(); i++) {
288 bool is_last_token = i == pattern.size() - 1;
289 TokenType token_type = TokenType(parser_->types_[cursor]);
290 TokenType curr_search_token = TokenType(pattern[i]);
291 TokenType next_search_token = TokenType(is_last_token ? '\0' : pattern[i + 1]);
292
293 /* Scope skipping. */
294 if (!is_last_token && curr_search_token == '.' && next_search_token == '.') {
295 cursor = match[i - 1].scope().back().index_;
296 i++;
297 continue;
298 }
299
300 /* Regular token. */
301 if (curr_search_token == token_type) {
302 match[i] = Token(*parser_, cursor++);
303 }
304 else if (curr_search_token == '?' && next_search_token != '?') {
305 /* We just matched an optional token in previous iteration. Continue scanning. */
306 match[i] = Token(*parser_);
307 }
308 else if (!is_last_token && curr_search_token != '?' && next_search_token == '?') {
309 /* This was an optional token. Continue scanning. */
310 match[i] = Token(*parser_);
311 i++;
312 continue;
313 }
314 else {
315 /* Token mismatch. Test next position. */
316 break;
317 }
318
319 if constexpr (!include_preprocessor) {
320 if (match[i].scope().type() == ScopeType::Preprocessor) {
321 /* Scope mismatch. Test next position. */
322 break;
323 }
324 }
325
326 if (is_last_token) {
327 callback(match);
328 /* Avoid matching the same position if start of pattern is optional tokens. */
329 pos = cursor - range().start - 1;
330 }
331 }
332 }
333 }
334
339 template<typename CallbackFn> void foreach_scope(ScopeType type, CallbackFn callback) const
340 {
341 /* Makes no sense to iterate on global scope since it is the top level. */
343
344 if (this->is_invalid()) {
345 return;
346 }
347 size_t pos = this->index_;
348 while ((pos = parser_->scope_types_str.find(char(type), pos)) != std::string::npos) {
349 Scope scope(*parser_, pos);
350 if (scope.front().index_ > this->back().index_) {
351 /* Found scope starts after this scope. End iteration. */
352 break;
353 }
354 /* Make sure found scope is direct child of this scope. */
355 Scope parent_scope = scope.scope();
356 if (parent_scope.index_ == this->index_) {
357 callback(scope);
358 }
359 pos += 1;
360 }
361 }
362
367 template<typename CallbackFn> void foreach_attribute(CallbackFn callback) const
368 {
370 this->foreach_scope(ScopeType::Attribute, [&](Scope attr) {
371 callback(attr[0], attr[1] == '(' ? attr[1].scope() : Scope(*parser_));
372 });
373 }
374
379 template<typename Callback>
380 void foreach_token(const TokenType token_type, Callback callback) const
381 {
382 IndexRange index_range = parser_->scope_ranges[index_];
383 std::string_view view(parser_->token_types_str());
384
385 size_t offset = index_range.start;
386 for (const char c : view.substr(index_range.start, index_range.size)) {
387 if (token_type == TokenType(c)) {
388 callback(Token(*parser_, offset));
389 }
390 offset++;
391 }
392 }
393
399 template<typename Callback> void foreach_function(Callback callback) const
400 {
401 foreach_match("m?AA(..)c?{..}", [&](const std::vector<Token> matches) {
402 callback(matches[0] == Static,
403 matches[2],
404 matches[3],
405 matches[4].scope(),
406 matches[8] == Const,
407 matches[10].scope());
408 });
409 foreach_match("m?A<..>A(..)c?{..}", [&](const std::vector<Token> matches) {
410 callback(matches[0] == Static,
411 matches[2],
412 matches[7],
413 matches[8].scope(),
414 matches[12] == Const,
415 matches[14].scope());
416 });
417 foreach_match("m?AA::A(..)c?{..}", [&](const std::vector<Token> matches) {
418 callback(matches[0] == Static,
419 matches[2],
420 matches[6],
421 matches[7].scope(),
422 matches[11] == Const,
423 matches[13].scope());
424 });
425 foreach_match("m?AA<..>(..)c?{..}", [&](const std::vector<Token> matches) {
426 callback(matches[0] == Static,
427 matches[2],
428 matches[3],
429 matches[8].scope(),
430 matches[12] == Const,
431 matches[14].scope());
432 });
433 }
434
440 template<typename Callback> void foreach_struct(Callback callback) const
441 {
442 foreach_match("sA{..}", [&](const std::vector<Token> matches) {
443 callback(matches[0], Scope(*parser_), matches[1], matches[2].scope());
444 });
445 foreach_match("sA<..>{..}", [&](const std::vector<Token> matches) {
446 callback(matches[0], Scope(*parser_), matches[1], matches[6].scope());
447 });
448 foreach_match("s[[..]]A{..}", [&](const std::vector<Token> matches) {
449 callback(matches[0], matches[2].scope(), matches[7], matches[8].scope());
450 });
451 foreach_match("s[[..]]A<..>{..}", [&](const std::vector<Token> matches) {
452 callback(matches[0], matches[2].scope(), matches[7], matches[12].scope());
453 });
454 }
455
467 template<typename Callback> void foreach_declaration(Callback callback) const
468 {
469 auto attrs = [&](const std::vector<Token> &tokens) {
470 Token first = tokens[0].is_valid() ? tokens[0] :
471 (tokens[2].is_valid() ? tokens[2] : tokens[4]);
472 Scope attributes = first.prev().prev().scope();
473 attributes = (attributes.type() == ScopeType::Attributes) ? attributes : Scope(*parser_);
474 return attributes;
475 };
476
477 auto cb = [&](Scope attributes,
478 Token const_tok,
479 Token type,
480 Scope template_scope,
481 Token name,
482 Scope array,
483 Token decl_end) {
484 if (type.scope() != *this) {
485 return;
486 }
487 callback(attributes, const_tok, type, template_scope, name, array, decl_end);
488 };
489
490 Scope invalid(*parser_);
491
492 /* TODO(fclem): This is getting out of hand... */
493 foreach_match("m?c?AA;", [&](const std::vector<Token> toks) {
494 cb(attrs(toks), toks[2], toks[4], invalid, toks[5], invalid, toks.back());
495 });
496 foreach_match("m?c?AA[..];", [&](const std::vector<Token> toks) {
497 cb(attrs(toks), toks[2], toks[4], invalid, toks[5], toks[6].scope(), toks.back());
498 });
499 foreach_match("m?c?AA[..][..];", [&](const std::vector<Token> toks) {
500 cb(attrs(toks), toks[2], toks[4], invalid, toks[5], toks[6].scope(), toks.back());
501 });
502 foreach_match("m?c?A<..>A;", [&](const std::vector<Token> toks) {
503 cb(attrs(toks), toks[2], toks[4], toks[5].scope(), toks[9], invalid, toks.back());
504 });
505 foreach_match("m?c?A<..>A[..];", [&](const std::vector<Token> toks) {
506 cb(attrs(toks), toks[2], toks[4], toks[5].scope(), toks[9], toks[10].scope(), toks.back());
507 });
508 foreach_match("m?c?A<..>A[..][..];", [&](const std::vector<Token> toks) {
509 cb(attrs(toks), toks[2], toks[4], toks[5].scope(), toks[9], toks[10].scope(), toks.back());
510 });
511
512 foreach_match("m?c?A&A;", [&](const std::vector<Token> toks) {
513 cb(attrs(toks), toks[2], toks[4], invalid, toks[6], invalid, toks.back());
514 });
515 foreach_match("m?c?A(&A)[..];", [&](const std::vector<Token> toks) {
516 cb(attrs(toks), toks[2], toks[4], invalid, toks[7], toks[9].scope(), toks.back());
517 });
518 foreach_match("m?c?A(&A)[..][..];", [&](const std::vector<Token> toks) {
519 cb(attrs(toks), toks[2], toks[4], invalid, toks[7], toks[9].scope(), toks.back());
520 });
521 foreach_match("m?c?A<..>&A;", [&](const std::vector<Token> toks) {
522 cb(attrs(toks), toks[2], toks[4], toks[5].scope(), toks[10], invalid, toks.back());
523 });
524 foreach_match("m?c?A<..>(&A)[..];", [&](const std::vector<Token> toks) {
525 cb(attrs(toks), toks[2], toks[4], toks[5].scope(), toks[11], toks[13].scope(), toks.back());
526 });
527 foreach_match("m?c?A<..>(&A)[..][..];", [&](const std::vector<Token> toks) {
528 cb(attrs(toks), toks[2], toks[4], toks[5].scope(), toks[11], toks[13].scope(), toks.back());
529 });
530 }
531
532 bool operator==(const Scope &other) const
533 {
534 return this->index_ == other.index_ && this->parser_ == other.parser_;
535 }
536 bool operator!=(const Scope &other) const
537 {
538 return !(*this == other);
539 }
540};
541
542} // namespace blender::gpu::shader::parser
constexpr IndexRange()=default
__int64 int64_t
Definition BKE_text.h:90
TokenType
Definition types.hh:41
@ Static
Definition types.hh:105
@ Const
Definition types.hh:85
bool contains_token(const char token_type) const
Definition scope.hh:221
std::string_view str_with_whitespace() const
Definition scope.hh:186
void foreach_struct(Callback callback) const
Definition scope.hh:440
void foreach_declaration(Callback callback) const
Definition scope.hh:467
Scope(const ParserBase &parser)
Definition scope.hh:52
Token find_token(const char token_type) const
Definition scope.hh:212
Token operator[](const int64_t index) const
Definition scope.hh:85
bool operator!=(const Scope &other) const
Definition scope.hh:536
std::string_view str() const
Definition scope.hh:194
void foreach_match(const std::string &pattern, CallbackFn callback) const
Definition scope.hh:255
Scope first_scope_of_type(const ScopeType type) const
Definition scope.hh:228
std::string_view str_exclusive() const
Definition scope.hh:203
Scope(const ParserBase &parser, int64_t index)
Definition scope.hh:38
void foreach_attribute(CallbackFn callback) const
Definition scope.hh:367
bool contains(const std::string &str) const
Definition scope.hh:181
void foreach_function(Callback callback) const
Definition scope.hh:399
void set_type(ScopeType type)
Definition scope.hh:102
void foreach_scope(ScopeType type, CallbackFn callback) const
Definition scope.hh:339
bool contains(const Scope sub) const
Definition scope.hh:171
void foreach_token(const TokenType token_type, Callback callback) const
Definition scope.hh:380
bool operator==(const Scope &other) const
Definition scope.hh:532
Token prev(int i=1) const
Definition lexit.hh:376
int32_t index_
Definition lexit.hh:51
Token(const TokenBuffer *buf, int32_t index)
Definition lexit.hh:337
bool is_valid() const
Definition lexit.hh:65
Token next(int i=1) const
Definition lexit.hh:372