invoc_data.set_expander (this);
// lookup the rules
- AST::MacroRulesDefinition *rules_def = nullptr;
- bool ok = mappings.lookup_macro_invocation (invoc, &rules_def);
+ auto rules_def = mappings.lookup_macro_invocation (invoc);
// If there's no rule associated with the invocation, we can simply return
// early. The early name resolver will have already emitted an error.
- if (!ok)
+ if (!rules_def)
return;
+ auto rdef = rules_def.value ();
+
// We store the last expanded invocation and macro definition for error
// reporting in case the recursion limit is reached
last_invoc = *invoc.clone_macro_invocation_impl ();
- last_def = *rules_def;
+ last_def = *rdef;
- if (rules_def->is_builtin ())
- fragment
- = rules_def->get_builtin_transcriber () (invoc.get_locus (), invoc_data)
- .value_or (AST::Fragment::create_empty ());
+ if (rdef->is_builtin ())
+ fragment = rdef->get_builtin_transcriber () (invoc.get_locus (), invoc_data)
+ .value_or (AST::Fragment::create_empty ());
else
- fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def,
+ fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rdef,
has_semicolon);
set_expanded_fragment (std::move (fragment));
// TODO: Should we use `ctx.mark_resolved()`?
auto definition = ctx.mappings.lookup_macro_def (resolved);
- AST::MacroRulesDefinition *existing;
- auto exists = ctx.mappings.lookup_macro_invocation (invocation, &existing);
-
- if (!exists)
+ if (!ctx.mappings.lookup_macro_invocation (invocation))
ctx.mappings.insert_macro_invocation (invocation, definition.value ());
}
if (!rules_def)
return;
- AST::MacroRulesDefinition *tmp_def = nullptr;
- if (mappings.lookup_macro_invocation (invoc, &tmp_def))
+ if (mappings.lookup_macro_invocation (invoc))
return;
mappings.insert_macro_invocation (invoc, rules_def.value ());
* we could be inserting the same macro def over and over again until we
* implement some optimizations */
// FIXME: ARTHUR: Remove that lookup and add proper optimizations instead
- AST::MacroRulesDefinition *tmp_def = nullptr;
- if (mappings.lookup_macro_invocation (invoc, &tmp_def))
+ if (mappings.lookup_macro_invocation (invoc))
return;
mappings.insert_macro_invocation (invoc, *rules_def);
macroInvocations[invoc.get_macro_node_id ()] = def;
}
-bool
-Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc,
- AST::MacroRulesDefinition **def)
+tl::optional<AST::MacroRulesDefinition *>
+Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc)
{
auto it = macroInvocations.find (invoc.get_macro_node_id ());
if (it == macroInvocations.end ())
- return false;
+ return tl::nullopt;
- *def = it->second;
- return true;
+ return it->second;
}
void
void insert_macro_invocation (AST::MacroInvocation &invoc,
AST::MacroRulesDefinition *def);
- bool lookup_macro_invocation (AST::MacroInvocation &invoc,
- AST::MacroRulesDefinition **def);
+ tl::optional<AST::MacroRulesDefinition *>
+ lookup_macro_invocation (AST::MacroInvocation &invoc);
void insert_exported_macro (AST::MacroRulesDefinition &def);
std::vector<NodeId> &get_exported_macros ();