16using namespace blender::gpu::shader::parser::ast;
18template<
typename T,
typename AstNodeT>
25 param.
is_valid() && sym && sym->template_data)
27 auto [inst, _, err] = sym->template_data->lookup_inst(param, scope, table);
30 return {sym, std::nullopt};
34# define LOOKUP_LOG(a) cerr << a;
41 optional<SourceLocation>
loc)
const
44 SymbolFunction *func = lookup_generic<SymbolFunction>(table,
id,
loc.value_or(
id.front()));
48template<
typename ArgOrParamList>
51 ArgOrParamList params,
53 optional<SourceLocation>
loc)
const
58 if (
auto [cls, err_cls] =
lookup_class(table,
id); !cls->is_error && cls && cls->is_builtin())
68 if (func->template_data) {
72 func = func->template_data->lookup_inst(template_params, arguments_scope, table)
84 func = func->lookup_overload(
id, table, arg_types).unwrap(err);
92 optional<SourceLocation> loc)
const;
97 optional<SourceLocation> loc)
const;
101 optional<SourceLocation>
loc)
const
104 SymbolClass *cls = lookup_generic<SymbolClass>(table,
id,
loc.value_or(
id.front()));
111 optional<SourceLocation>
loc)
const
114 SymbolClass *cls = lookup_generic_nested<SymbolClass>(table,
id, last,
loc.value_or(
id.front()));
121 return lookup_generic<SymbolFunction>(table,
id,
id.front());
127 return lookup_generic<SymbolClass>(table,
id,
id.front());
133 return lookup_generic<SymbolVariable>(table,
id,
id.front());
143 if (fwd.first.identifier().str() !=
decl.identifier().str()) {
147 if (fwd.first.return_type().str() !=
decl.return_type().str()) {
161 fwd_arg = fwd_arg.
next();
162 decl_par = decl_par.
next();
195 if constexpr (std::is_same_v<T, SymbolFunction>) {
198 else if constexpr (std::is_same_v<T, SymbolClass>) {
201 else if constexpr (std::is_same_v<T, SymbolVariable>) {
210T *SymbolScope::lookup_generic(
const SymbolTable &table,
212 const SourceLocation &loc)
const
227 if (auto
v = lookup_generic_nested<
T>(
228 table,
id.namespace_start(),
id.child_last(
NodeType::
Id),
loc))
236 LOOKUP_LOG(
"[Lookup] Member function scope ('this_' found). Checking class scope...\n")
237 const SymbolVariable *this_var = it->second.second;
239 const SymbolClass *cls = this_var->
type;
241 if (auto
v = this_scope->lookup_generic<
T>(table,
id,
loc);
v && !
v->is_error) {
242 LOOKUP_LOG(
"[Lookup] Symbol found in class scope '" << cls->identifier <<
"'.\n")
249 LOOKUP_LOG(
"[Lookup] Not found locally. Delegating to parent scope\n")
254 LOOKUP_LOG(
"[Lookup] FAILED at root scope.\n")
256 if constexpr (is_same_v<
T, SymbolFunction>) {
258 return it->second.second;
261 else if constexpr (is_same_v<T, SymbolClass>) {
263 return it->second.second;
266 else if constexpr (is_same_v<T, SymbolVariable>) {
268 return it->second.second;
272 static_assert(
false);
280T *SymbolScope::lookup_generic_nested(
const SymbolTable &table,
283 const SourceLocation &
loc)
const
286 <<
id.front().buf_->substr(
id.front(), last.
front())
291 LOOKUP_LOG(
"[Nested] Looking up anonymous namespace scope\n")
292 Id next_search_id =
id.
id != last.
id ?
id.next_id() :
id;
293 if (auto ret = it->second->lookup_generic_nested<
T>(table, next_search_id, last,
loc)) {
299 if (
id.
id != last.
id) {
301 if (TemplateParamList list = id.template_params(); list.is_valid()) {
302 LOOKUP_LOG(
"[Nested] Segment '" << id.str() <<
"' has template params. Checking classes.\n")
303 if (auto it = classes.find(string(id.str())); it != classes.end()) {
304 if (SymbolClass *cls = it->second.second->as_class(); cls && cls->template_data) {
305 if (auto [tmp, _, err] = cls->template_data->lookup_inst(list, *this, table);
308 LOOKUP_LOG(
"[Nested] Found template instance scope '" << tmp->identifier <<
"'.\n")
309 return tmp->lookup_generic_nested<T>(table, id.next_id(), last, loc);
313 LOOKUP_LOG(
"[Nested] Template instance lookup failed for '" << id.str() <<
"'.\n")
317 if (
auto it =
scopes.find(
string(
id.str())); it !=
scopes.end()) {
318 LOOKUP_LOG(
"[Nested] Match sub-scope/namespace '"
319 << id.str() <<
"' at " << it->second->identifier <<
". Descending...\n")
320 return it->second->lookup_generic_nested<T>(table, id.next_id(), last, loc);
322 LOOKUP_LOG(
"[Nested] Mid-segment '" <<
id.
str() <<
"' could not be resolved as a scope.\n")
326 if constexpr (is_same_v<T, SymbolFunction>) {
328 if (it->second.first <=
loc) {
329 LOOKUP_LOG(
"[Nested] Found function match: " << it->second.second->identifier <<
"\n")
330 return it->second.second;
333 "[Nested] Found function, but declared AFTER current source location "
334 "(shadowed/invisible).\n")
337 else if constexpr (is_same_v<
T, SymbolClass>) {
338 if (
auto it =
classes.find(
string(
id.str())); it !=
classes.end()) {
339 if (it->second.first <= loc) {
340 LOOKUP_LOG(
"[Nested] Found class match: " << it->second.second->identifier <<
"\n")
341 return it->second.second;
344 "[Nested] Found class, but declared AFTER current source location "
345 "(shadowed/invisible).\n")
348 else if constexpr (is_same_v<T, SymbolVariable>) {
350 if (it->second.first <=
loc) {
351 if (TemplateParamList list =
id.template_params(); list.is_valid()) {
353 "[Nested] Found variable match, but rejected: Variables cannot be templated.\n")
357 return it->second.second;
360 "[Nested] Found variable, but declared AFTER current source location "
361 "(shadowed/invisible).\n")
365 static_assert(
false);
373 return it !=
classes.end() ? it->second.second :
nullptr;
379 return it !=
functions.end() ? it->second.second :
nullptr;
385 return it !=
variables.end() ? it->second.second :
nullptr;
408 return fn ?
fn->as_function() :
nullptr;
416 return fn ?
fn->as_function() :
nullptr;
426 if (it.first->is_builtin && !
builtin) {
431 it.first->add_overload(
fn);
454 return !
variables.emplace(
id, var).second;
465 vector<SymbolVariable *> members;
467 members.emplace_back(
v.second);
472 return a->loc < b->loc;
488 enum class SymbolType { Variable, FunctionTemplate, ClassTemplate,
Function,
Class,
Scope };
494 const string &identifier_mangled,
496 const vector<bool> &is_last) {
497 string loc = src_loc;
503 int padding_size =
max(0, 55 -
int(
loc.size()));
504 string padding(padding_size,
' ');
509 for (
int i = 0; i < depth - 1; ++i) {
520 if (is_last.back()) {
531 case SymbolType::Variable:
534 case SymbolType::FunctionTemplate:
537 case SymbolType::ClassTemplate:
540 case SymbolType::Function:
543 case SymbolType::Class:
546 case SymbolType::Scope:
554 cout <<
" : " << decl_type->identifier;
556 if (!identifier_mangled.empty()) {
557 cout <<
" -> " << identifier_mangled;
565 this->identifier.empty() ?
"<root>" : this->identifier,
572 auto print_node = [&](
auto &self,
auto *
node,
int depth,
const vector<bool> &is_last) ->
void {
580 vector<Child> children;
582 for (
const auto &[name, var] :
node->variables) {
583 children.emplace_back(
584 SymbolType::Variable, var.second->identifier, var.second, var.second->type);
586 for (
const auto &[name, func] :
node->functions) {
587 children.emplace_back(SymbolType::Function, func.second->identifier, func.second,
nullptr);
589 for (
const auto &[name, cls] :
node->classes) {
590 children.emplace_back(SymbolType::Class, name, cls.second,
nullptr);
592 for (
const auto &[name, scope_ptr] :
node->scopes) {
593 children.emplace_back(SymbolType::Scope, scope_ptr->identifier,
nullptr,
nullptr);
596 sort(children.begin(), children.end(), [](
const Child &a,
const Child &
b) {
597 return a.resolved->loc < b.resolved->loc;
601 for (
size_t i = 0; i < children.size(); ++i) {
602 bool child_is_last = (i == children.size() - 1);
604 vector<bool> next_is_last = is_last;
605 next_is_last.push_back(child_is_last);
607 print_line(children[i].resolved->loc,
611 children[i].resolved ? children[i].resolved->identifier :
"",
615 if (children[i].
type == SymbolType::Scope) {
616 const auto *next_node =
static_cast<const SymbolScope *
>(children[i].resolved);
618 self(self, next_node, depth + 1, next_is_last);
623 print_node(print_node,
this, 0, {});
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
#define assert(assertion)
uint padding(uint offset, uint alignment)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static Result< T * > resolve_template_instantiation(const SymbolTable &table, T *sym, AstNodeT id, const SymbolScope &scope)
const char * get_symbol_type_name()
string to_string(const char *str)
bool is_reference() const
Declarator declarator() const
std::string_view str() const
Represents a specific position within a source file or included sub-file.
static Result< vector< SymbolClass * > > to_arg_types(const SymbolTable &table, const SymbolScope &scope, ast::FuncParamList list)
SymbolScope * child_scope(int n)
unordered_map< string, SymbolScope * > scopes
vector< SymbolVariable * > non_static_variables_in_declaration_order() const
SymbolVariable * lookup_variable(const SymbolTable &table, ast::IdQualified id) const
enum bsl::SymbolScope::Type type
SymbolScope(ast::LocalScope decl)
vector< std::pair< ast::FuncForwardDecl, SymbolFunction * > > function_prototypes
bool function_emplace(SymbolFunction *fn, bool builtin=false)
SymbolMap< SymbolFunction > functions
optional< SourceLocation > poi
SymbolFunction * as_function()
bool variable_emplace(string id, SymbolVariable *var)
SymbolFunction * parent_function()
SymbolMap< SymbolVariable > variables
SymbolFunction * lookup_function_base(const SymbolTable &table, ast::IdQualified id) const
SymbolMap< SymbolClass > classes
const SymbolScope * root_scope() const
Result< SymbolFunction * > lookup_function(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
SymbolClass * lookup_class_base(const SymbolTable &table, ast::IdQualified id) const
Result< SymbolClass * > lookup_class(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
std::pair< ast::FuncForwardDecl, SymbolFunction * > lookup_function_forward_decl(ast::FuncDecl decl) const
static constexpr const char * err_symbol