Files
jak-project/decompiler/analysis/static_refs.cpp
T
Tyler Wilding abf61a94fb docs: add support for :override-doc in method declarations as well as documenting state handlers (#2139)
Adding support for better child-type method docstrings. This is a
problem unique to methods.

Usually, a child-type will have the same signature and a common name
will apply, but the implementation is different. This means, you
probably want a different docstring to describe what is happening.

Currently this is possible to do via `:replace`. The problem with
replace is two fold:
- a replaced method ends up in the generated `deftype`...because you
usually change the signature!
- we don't put docstrings in the `deftype` in normal GOAL, this is just
something we do for the `all-types` file (they go in the `defmethod`
instead)
- more importantly, this means anytime you now want to change the
parent's name/args/return type -- you have to apply that change
everywhere.

So this is a better design you can now just declare the method like so:
```clj
(:override-doc "my new docstring" <method_id>)
```

And internally a pseudo-replaced method will be added, but it will
inherit everything from the parent (except the docstring of course)

Unrelated - I also made all the keyword args for declaring methods not
depend on ordering

This also adds support for documenting virtual and non-virtual state
handlers. For example:

```clj
  (:states
    (part-tester-idle (:event "test") symbol))
```

or

```clj
(idle () _type_ :state (:event "test") 20)
```

I will probably add the ability to give some sort of over-view docstring
at a later date.

Co-authored-by: water <awaterford111445@gmail.com>
2023-01-21 20:45:45 -05:00

98 lines
3.0 KiB
C++

#include "static_refs.h"
#include "common/goos/PrettyPrinter.h"
#include "common/log/log.h"
#include "decompiler/Function/Function.h"
#include "decompiler/ObjectFile/LinkedObjectFile.h"
#include "decompiler/analysis/final_output.h"
namespace decompiler {
namespace {
bool kind_for_lambda(FunctionName::FunctionKind k) {
if (k == FunctionName::FunctionKind::UNIDENTIFIED || k == FunctionName::FunctionKind::NV_STATE ||
k == FunctionName::FunctionKind::V_STATE) {
return true;
}
return false;
}
bool try_convert_lambda(const Function& parent_function,
FormPool& pool,
Form* f,
bool defstate_behavior,
const DecompilerTypeSystem& dts) {
auto atom = form_as_atom(f);
if (atom && atom->is_static_addr()) {
auto& lab = parent_function.ir2.env.file->labels.at(atom->label());
auto& env = parent_function.ir2.env;
const auto& info = parent_function.ir2.env.file->label_db->lookup(lab.name);
auto& file = env.file;
auto other_func = file->try_get_function_at_label(atom->label());
if (other_func && kind_for_lambda(other_func->guessed_name.kind)) {
if (info.from_user) {
lg::error(
"Label {} had an entry in config, but it is a function. This will be "
"ignored and is no longer required.",
lab.name);
}
if (!other_func->ir2.env.has_local_vars() || !other_func->ir2.top_form ||
!other_func->ir2.expressions_succeeded) {
// don't bother if we don't even have vars.
return false;
}
goos::Object result;
if (defstate_behavior) {
result = final_output_defstate_anonymous_behavior(*other_func, dts);
} else {
result = final_output_lambda(*other_func);
}
f->clear();
f->push_back(pool.alloc_element<LambdaDefinitionElement>(result));
return true;
}
}
return false;
}
} // namespace
// TODO - important entry point!
int insert_static_refs(Form* top_level_form,
FormPool& pool,
const Function& function,
const DecompilerTypeSystem& dts) {
int replaced = 0;
// first, look for defstates and lambdas to behaviors.
top_level_form->apply([&](FormElement* fe) {
auto as_defstate = dynamic_cast<DefstateElement*>(fe);
if (as_defstate) {
for (auto& e : as_defstate->entries()) {
if (e.is_behavior) {
if (try_convert_lambda(function, pool, e.val, true, dts)) {
replaced++;
}
}
}
}
});
// next, all the rest.
top_level_form->apply_form([&](Form* f) {
if (try_convert_lambda(function, pool, f, false, dts)) {
replaced++;
}
});
top_level_form->apply([&](FormElement* fe) {
auto as_static_data = dynamic_cast<DecompiledDataElement*>(fe);
if (as_static_data) {
as_static_data->do_decomp(function.ir2.env, function.ir2.env.file);
}
});
return replaced;
}
} // namespace decompiler