diff options
author | Tom Tromey <tromey@adacore.com> | 2022-02-22 09:48:25 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2022-04-04 12:46:09 -0600 |
commit | 1e237aba2216f89b9a4b3235ad8d09d1b1b8f039 (patch) | |
tree | 088adb7b45abf2f494d139e2a4865355d8cef3bf /gdb/parser-defs.h | |
parent | 28c4b1ffaa41b17af11984c0383a8a37ea00eef4 (diff) | |
download | binutils-1e237aba2216f89b9a4b3235ad8d09d1b1b8f039.zip binutils-1e237aba2216f89b9a4b3235ad8d09d1b1b8f039.tar.gz binutils-1e237aba2216f89b9a4b3235ad8d09d1b1b8f039.tar.bz2 |
Refactor expression completion
This refactors the gdb expression completion code to make it easier to
add more types of completers.
In the old approach, just two kinds of completers were supported:
field names for some sub-expression, or tag names (like "enum
something"). The data for each kind was combined in single structure,
"expr_completion_state", and handled explicitly by
complete_expression.
In the new approach, the parser state just holds an object that is
responsible for implementing completion. This way, new completion
types can be added by subclassing this base object.
The structop completer is moved into structop_base_operation, and new
objects are defined for use by the completion code. This moves much
of the logic of expression completion out of completer.c as well.
Diffstat (limited to 'gdb/parser-defs.h')
-rw-r--r-- | gdb/parser-defs.h | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h index 6de5140..71381b1 100644 --- a/gdb/parser-defs.h +++ b/gdb/parser-defs.h @@ -82,20 +82,55 @@ struct expr_builder expression_up expout; }; -/* This is used for expression completion. */ +/* Complete an expression that references a field, like "x->y". */ -struct expr_completion_state +struct expr_complete_structop : public expr_completion_base { + explicit expr_complete_structop (expr::structop_base_operation *op) + : m_op (op) + { + } + + bool complete (struct expression *exp, + completion_tracker &tracker) override + { + return m_op->complete (exp, tracker); + } + +private: + /* The last struct expression directly before a '.' or '->'. This is set when parsing and is only used when completing a field name. It is nullptr if no dereference operation was found. */ - expr::structop_base_operation *expout_last_op = nullptr; + expr::structop_base_operation *m_op = nullptr; +}; + +/* Complete a tag name in an expression. This is used for something + like "enum abc<TAB>". */ + +struct expr_complete_tag : public expr_completion_base +{ + expr_complete_tag (enum type_code code, + gdb::unique_xmalloc_ptr<char> name) + : m_code (code), + m_name (std::move (name)) + { + /* Parsers should enforce this statically. */ + gdb_assert (code == TYPE_CODE_ENUM + || code == TYPE_CODE_UNION + || code == TYPE_CODE_STRUCT); + } + + bool complete (struct expression *exp, + completion_tracker &tracker) override; + +private: - /* If we are completing a tagged type name, this will be nonzero. */ - enum type_code expout_tag_completion_type = TYPE_CODE_UNDEF; + /* The kind of tag to complete. */ + enum type_code m_code; /* The token for tagged type name completion. */ - gdb::unique_xmalloc_ptr<char> expout_completion_name; + gdb::unique_xmalloc_ptr<char> m_name; }; /* An instance of this type is instantiated during expression parsing, @@ -254,7 +289,7 @@ struct parser_state : public expr_builder bool parse_completion; /* Completion state is updated here. */ - expr_completion_state m_completion_state; + std::unique_ptr<expr_completion_base> m_completion_state; /* The innermost block tracker. */ innermost_block_tracker *block_tracker; |