10#include <unordered_set>
18using namespace shader::parser;
19using namespace shader::parser::ast;
25 while (ns_scope.
type() == ScopeType::Namespace || ns_scope.
type() == ScopeType::Struct) {
26 prefix = ns_scope.
front().
prev().full_symbol_name() +
"::" + prefix;
27 ns_scope = ns_scope.
scope();
36 const std::string &filepath)
38 Token def_start = template_tok;
39 Scope template_args = def_start.
next().scope();
44 Token def_end = body_start.scope().back();
59 Token fn_args = body_start.
prev() == TokenType::Const ? body_start.
prev(2) : body_start.
prev();
60 Token fn_name = fn_args.scope().front().prev();
73 const std::string &filepath)
75 ns.foreach_scope(ScopeType::Namespace, [&](
const Scope &ns) {
76 parse_namespace_symbols(parser, ns, metadata, filepath);
79 auto process_symbol = [&](
Scope ns_scope,
81 string_view identifier,
86 std::vector<std::pair<std::string, std::string>> members = {}) {
87 if (name.scope() != ns_scope) {
90 metadata::Symbol symbol;
92 symbol.identifier = identifier;
93 symbol.definition_line = line;
94 symbol.is_method = is_method;
95 symbol.is_static = is_static;
96 symbol.is_struct = is_struct;
97 symbol.members = members;
98 metadata.symbol_table.emplace_back(symbol);
101 auto process_templates = [&](
Scope ns_scope,
Token t,
bool is_method) {
102 if (t.scope() != ns_scope) {
106 if (t.next() ==
'<') {
107 if (t.next(2) ==
'>') {
111 TemplateDefinition symbol = SourceProcessor::parse_template_definition(
112 parser, t, is_method, ns_scope, filepath);
113 metadata.template_definitions.emplace_back(symbol);
125 string resolved_name = string(name.str()) +
127 process_symbol(ns_scope, name, resolved_name, line,
false,
false,
true, {});
133 Token name = template_args.front().
prev();
134 string resolved_name = string(name.str()) +
136 process_symbol(ns_scope, name, resolved_name, line, is_method,
false,
false);
142 std::vector<std::pair<std::string, std::string>> members;
145 members.emplace_back(type.str(), name.str());
150 struct_name.line_number(),
158 process_symbol(body, name, name.str(), struct_name.line_number(),
true, is_static,
false);
161 body.foreach_token(
Template, [&](
Token t) { process_templates(body, t,
true); });
165 process_symbol(ns, name, name.str(), name.line_number(),
false,
false,
false);
168 ns.foreach_token(
Template, [&](
Token t) { process_templates(ns, t,
false); });
171void SourceProcessor::parse_local_symbols(
Parser &
parser)
173 parse_namespace_symbols(parser, parser(), metadata_, filepath_);
180 const set<metadata::Symbol> &symbols_set)
183 ns_prefix += ns_name +
"::";
185 bool has_nested_scope =
false;
188 has_nested_scope =
true;
191 if (has_nested_scope) {
198 if (token.
prev() ==
'.') {
207 bool is_pipeline_arg =
false;
208 if (token.scope().
type() == ScopeType::FunctionArg) {
209 std::string_view type = token.scope().scope().front().
prev(2).
str();
210 is_pipeline_arg = (type ==
"PipelineGraphic" || type ==
"PipelineCompute");
213 if (is_pipeline_arg) {
217 else if (next !=
'<' &&
228 const bool is_fn = (token.
next() ==
'(') ||
229 (token.
next() ==
'<' && token.
next().scope().back().
next() ==
'(');
231 if (is_fn && token.scope().
type() == ScopeType::Struct) {
239 const Scope struct_scope = token.scope().first_scope_of_type(ScopeType::Struct);
241 struct_name = struct_scope.
front().
prev().full_symbol_name();
245 const int token_line = token.line_number();
247 for (
const auto &symbol : symbols_set) {
248 if (token.
str() != symbol.identifier) {
252 if (!symbol.name_space.starts_with(ns_prefix)) {
257 if (token_line < symbol.definition_line) {
261 string symbol_visible = symbol.name_space.substr(ns_prefix.size()) + symbol.identifier;
263 bool append_struct_ns =
false;
264 string specified_symbol = token.full_symbol_name();
266 if (symbol.is_method && !struct_name.empty()) {
267 if (!symbol.is_static) {
271 bool is_prev_ns_specifier = token.
prev() ==
':' && token.
prev(2) ==
':';
272 if (!is_prev_ns_specifier) {
275 specified_symbol = struct_name +
"::" + specified_symbol;
276 append_struct_ns =
true;
279 if (specified_symbol != symbol_visible) {
286 if (specified_symbol != symbol_visible) {
295 for (
const auto &overload : symbols_set) {
297 if (overload.name_space == symbol.name_space || overload.identifier != symbol.identifier)
303 if (token_line < overload.definition_line) {
307 if (!ns_prefix.starts_with(overload.name_space)) {
310 if (specified_symbol != overload.identifier) {
314 token,
"Call to function is ambiguous. Specify namespace to remove ambiguity.");
320 parser.insert_before(token.namespace_start(), ns_name +
"::");
321 if (append_struct_ns) {
323 parser.insert_before(token.namespace_start(), struct_name +
"::");
333 if (toks[0].scope().type() != ScopeType::Namespace || !toks[0].
str().starts_with(
"Pipeline")) {
340 if (namespace_tok == TokenType::Namespace) {
345 error_handler.
report(namespace_tok,
"Expected namespace token.");
356 parser().foreach_token(TokenType::Namespace, [&](
Token t) {
359 while (name.
next() ==
':') {
365 for (
int i = 0; i < nesting; i++) {
370 parser.apply_mutations();
374 set<metadata::Symbol> symbols_set;
378 unordered_set<string> unique_symbols;
379 for (
const auto &symbol : metadata_.symbol_table) {
380 auto [_, inserted] = unique_symbols.insert(symbol.name_space + symbol.identifier);
382 symbols_set.emplace(symbol);
390 parser().foreach_scope(ScopeType::Namespace, [&](
const Scope &scope) {
393 }
while (parser.apply_mutations());
396void SourceProcessor::lower_scope_resolution_operators(
Parser &
parser)
398 parser().foreach_match<
true>(
"::", [&](
const vector<Token> &tokens) {
399 if (tokens[0].scope().type() == ScopeType::Attribute) {
403 if (prev !=
Word && !(prev ==
'>' &&
prev.followed_by_whitespace() ==
false)) {
405 parser.erase(tokens.front(), tokens.back());
412 parser.apply_mutations();
parser::IntermediateForm< parser::FullLexer, parser::FullParser > Parser
static constexpr const char * namespace_separator
static std::string template_arguments_mangle(const Scope template_args)
static void lower_namespace(string ns_prefix, const Scope &scope, SourceProcessor::Parser &parser, ErrorHandler &error_handler, const set< metadata::Symbol > &symbols_set)
static string get_prefix(Scope ns_scope)
SymEdge< T > * prev(const SymEdge< T > *se)
void report(Token tok, std::string_view message)
void foreach_match(const std::string &pattern, CallbackFn callback) const
void foreach_scope(ScopeType type, CallbackFn callback) const
void foreach_token(const TokenType token_type, Callback callback) const
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).
Token prev(int i=1) const
std::string_view str() const
const TokenType & type() const
Token next(int i=1) const