Commit graph

98 commits

Author SHA1 Message Date
Andreas Kling
70d7fb6a03 LibCpp: Make C++ AST (mostly) const-correct
I cheated and used const_cast to avoid dealing with the strange pattern
where we sometimes do a delayed reparenting of an AST node.
2023-02-21 00:54:04 +01:00
Poseydon42
3cd04f40e1 LibCpp: Parse 'using namespace' declarations
This gets us a little bit closer to a fully capable C++ parser.
2023-01-27 12:40:40 +01:00
Linus Groh
57dc179b1f Everywhere: Rename to_{string => deprecated_string}() where applicable
This will make it easier to support both string types at the same time
while we convert code, and tracking down remaining uses.

One big exception is Value::to_string() in LibJS, where the name is
dictated by the ToString AO.
2022-12-06 08:54:33 +01:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
Ferhat Geçdoğan
1fa976722a LibCpp: Support "FIXME" for comments 2022-06-14 19:07:00 +01:00
Itamar
b35293d945 LibCodeComprehension: Re-organize code comprehension related code
This moves all code comprehension-related code to a new library,
LibCodeComprehension.

This also moves some types related to code comprehension tasks (such as
autocomplete, find declaration) out of LibGUI and into
LibCodeComprehension.
2022-05-21 18:15:58 +02:00
Itamar
f4cca20972 LibCpp: Parse inheritance 2022-04-17 10:09:23 +04:30
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Itamar
abc420b15a LibCpp: Set end position for the return type node of FunctionType nodes
Previously we didn't set the end position for the return type node of
function FunctionType nodes.

This caused a VERIFY failure crash when dumping an AST that contains
such nodes.
2022-02-27 21:42:05 +01:00
Itamar
7b42abccf2 LibCpp: Allow qualified names in AST Declaration nodes
Previously, the names of declarations where stored as a simple
StringView.

Because of that, we couldn't parse out-of-line function definitions,
which have qualified names.
For example, we couldn't parse the following snippet:

```
void MyClass::foo(){}
```

To fix this, we now store the name of a declaration with a
ASTNode::Name node, which represents a qualified named.
2022-02-23 00:48:44 +00:00
Itamar
4f1c77a059 LibCpp: Fix end position calculation for various AST node types
Previously, the end position of the Type and Name nodes was incorrect.
It was incorrectly set to the start position of the next unconsumed
token.

This commit adds a previous_token_end() method and uses it to correctly
get the end position for these node types.
2022-02-09 00:51:31 +01:00
Itamar
ae68355a56 LibCpp: Fix parent of parameter type node
Previously, the parent of a parameter's Type node was incorrectly set
to the parent of the Parameter node.

We now set the parent of the parameter's Type node to the Parameter
node itself.
2022-02-09 00:51:31 +01:00
Sam Atkins
081d9bd010 LibCpp: Cast unused smart-pointer return values to void 2021-12-05 15:31:03 +01:00
Brian Gianforcaro
a225b26d88 LibCpp: Fix copy paste typo in Parser::match_secondary_expression
This was caught by SonarCloud.
2021-11-29 14:19:31 -08:00
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Brian Gianforcaro
998234f9e9 LibCpp: Remove redundant comparison to Token::Type::PipePipe
SonarCloud flagged this 'Identical sub-expressions on both sides of
operator "||"'. When looking at the git history it looks like it was
just a copy / paste mistake that happened when Token::Type::Arrow
support was added.
2021-09-28 10:58:09 +02:00
Itamar
9da9398bf0 LibCpp: Do macro substitution in the preprocessor instead of the parser
After this change, the parser is completely separated from preprocessor
concepts.
2021-08-07 21:24:11 +02:00
Itamar
0c4dc00f01 LibCpp: Import definitions from headers while processing
When the preprocessor encounters an #include statement it now adds
the preprocessor definitions that exist in the included header to its
own set of definitions.

We previously only aggregated the definitions from headers after
processing the source, which was less correct. (For example, there
could be an #ifdef that depends on a definition from another header).
2021-08-07 21:24:11 +02:00
Itamar
4673a517f6 LibCpp: Do lexing in the Preprocessor
We now call Preprocessor::process_and_lex() and pass the result to the
parser.

Doing the lexing in the preprocessor will allow us to maintain the
original position information of tokens after substituting definitions.
2021-08-07 21:24:11 +02:00
Ali Mohammad Pur
f16011e4d1 LibCpp: Allow 'final' in a class declaration with inheritance 2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
010be01694 LibCpp: Add support for east const
Now LibCpp can understand the eastest of consts too :^)
2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
e27ec04cdd LibCpp: Allow 'override' as a function target qualifier
This is just ignored right now.
2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
5f66874ea0 LibCpp: Add support for parsing function types
This makes it work with types like `Function<T(U, V)>`.
2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
b3cbe14569 LibCpp: Allow 'const' after a function's signature
This is too lax for functions that aren't class members, but let's
allow that anyway.
2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
3319114127 LibCpp: Add support for parsing reference types 2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
3c1422d774 LibCpp: Allow virtual destructors 2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
c866a56f07 LibCpp: Match and ignore struct/class inheritance 2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
8fefbfd5ac LibCpp: Parse enum members with explicit values 2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
67a19eaecb LibCpp: Parse "extern" declarations
Note that this is not the `extern "C"` declarations, just extern decl
qualifiers.
2021-08-02 01:03:59 +02:00
Ali Mohammad Pur
5d27740387 LibCpp: Accept scoped variable declarations
For instance, `Type Scope::Class::variable = value;` is a valid
declaration.
2021-08-02 01:03:59 +02:00
Itamar
42eb06f045 LibCpp: Don't store entire ASTNode vector in each parser state
We previously stored the entire ASTNode vector in each parser state,
and this vector was copied whenever a state was loaded or saved.

We don't actually need to store the whole nodes list in each state
because a new state can only add new nodes to this list, and won't
mutate existing nodes.

It would suffice to only hold a vector of the nodes that were created
while parsing in the current state to keep a reference to them.

This reduces the time it takes on my machine for the c++ language
server to handle a file that #includes <LibGUI/Widget.h> from ~4sec to
~0.7sec.
2021-07-13 23:20:09 +02:00
Itamar
eb6a15d52b LibCpp: Only store error messages for the main parser state
There's no need to store parser error messages for states with
depth > 0, as they will eventually be popped from the states stack and
their error messages will never be displayed to the user.

Profiling shows that this change reduces the % of backtraces that
contain the store_state & load_state functions from ~95% to ~70%.

Empirically this change reduces the time it takes on my machine for the
c++ language server to handle a file that #includes <LibGUI/Widget.h>
from ~14sec to ~4sec.
2021-07-13 23:20:09 +02:00
Itamar
34fc6c7e1c LibCpp: Make the fields of AST node types private
Previously almost all fields were public and were directly accessed by
the Parser and CppComprehensionEngine.

This commit makes all fields of AST node types private. They are now
accessed via getters & setters.
2021-07-10 21:58:28 +02:00
Itamar
232013c05b LibCpp: Add Parser::tokens_in_range(start, end)
This function returns the tokens that exist in the specified range.
2021-07-04 17:50:33 +02:00
Itamar
9a31fb6673 LibCpp: Fix positional information of Pointer types 2021-07-04 17:50:33 +02:00
Itamar
1dfdfcf820 LibCpp: Fix parsing of ellipsis
Previously the positional information for the node of an ellipsis was
incorrect.
2021-07-04 17:50:33 +02:00
Itamar
d7aa831a43 LibCpp: Differentiate between Type and NamedType
This adds a new ASTNode type called 'NamedType' which inherits from
the Type node.

Previously every Type node had a name field, but it was not logically
accurate. For example, pointer types do not have a name
(the pointed-to type may have one).
2021-06-29 00:07:19 +04:30
Itamar
10cad8a874 LibCpp: Add LOG_SCOPE() macro for debugging the parser's flow
LOG_SCOPE() uses ScopeLogger and additionally shows the current token
in the parser's state.
2021-06-29 00:07:19 +04:30
Itamar
c1ee0c1685 LibCpp: Support parsing enum classes 2021-06-29 00:07:19 +04:30
Federico Guerinoni
e0f1c237d2 HackStudio: Make TODO entries clickable
Now you can click a TODO entry to set focus on that position of that
file.
2021-06-23 19:00:11 +01:00
Federico Guerinoni
c397e030f4 LibCpp: Add function for retrieving TODO comments from the parser
Now `get_todo_entries` collects all TODO found within a comment
statement.
2021-06-23 19:00:11 +01:00
Itamar
7de6c1489b LibCpp: Parse basic constructors and destructors 2021-06-09 22:26:46 +02:00
Itamar
fd851ec5c9 LibCpp: Handle class access-specifiers in the Parser
We can now handle access-specifier tags (for example 'private:') when
parsing class declarations.

We currently only consume these tags on move on. We'll need to add some
logic that accounts for the access level of symbols down the road.
2021-06-09 22:26:46 +02:00
Itamar
dcdb0c7035 LibCpp: Support non-field class members
Previously, we had a special ASTNode for class members,
"MemberDeclaration", which only represented fields.

This commit removes MemberDeclaration and instead uses regular
Declaration nodes for representing the members of a class.

This means that we can now also parse methods, inner-classes, and other
declarations that appear inside of a class.
2021-06-09 22:26:46 +02:00
Itamar
0c9db38e8f LibCpp: Modify Token::to_string() to include more information
Token::to_string() now includes not only the token's type, but also its
text and span in the document.
2021-05-22 15:52:11 +02:00
Itamar
ef95ddcbfa LibCpp: Fix match_expression()
match_expression() will now return true if there's a match for a Name
node.
2021-05-19 23:19:07 +02:00
Itamar
f28d944122 LibCpp: Support 'auto' Type 2021-05-19 23:19:07 +02:00
Itamar
0e51aea781 LibCpp: Modify parsing of a Name's scope
A Name node can now have a non-empty scope and a null name.

For example, "AK::" has a non-empty scope and a null name component.
2021-05-15 23:28:50 +02:00
Itamar
84e41c4565 LibCpp: Modify logic of Parser::index_of_node_at
After this commit, Parser::index_of_node_at will prefer to return nodes
with greater indices.

Since the parsing logic ensures that child nodes come after parent
nodes, this change makes this function return child nodes when possible.
2021-05-15 23:28:50 +02:00