summaryrefslogtreecommitdiff
path: root/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp
blob: 4abfdc78612955012e03afc3850cfa41b537a247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/*
 * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "CppComprehensionEngine.h"
#include <AK/Assertions.h>
#include <AK/HashTable.h>
#include <AK/OwnPtr.h>
#include <AK/ScopeGuard.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCpp/AST.h>
#include <LibCpp/Lexer.h>
#include <LibCpp/Parser.h>
#include <LibCpp/Preprocessor.h>
#include <LibRegex/Regex.h>
#include <Userland/DevTools/HackStudio/LanguageServers/ConnectionFromClient.h>

namespace LanguageServers::Cpp {

CppComprehensionEngine::CppComprehensionEngine(FileDB const& filedb)
    : CodeComprehensionEngine(filedb, true)
{
}

CppComprehensionEngine::DocumentData const* CppComprehensionEngine::get_or_create_document_data(String const& file)
{
    auto absolute_path = filedb().to_absolute_path(file);
    if (!m_documents.contains(absolute_path)) {
        set_document_data(absolute_path, create_document_data_for(absolute_path));
    }
    return get_document_data(absolute_path);
}

CppComprehensionEngine::DocumentData const* CppComprehensionEngine::get_document_data(String const& file) const
{
    auto absolute_path = filedb().to_absolute_path(file);
    auto document_data = m_documents.get(absolute_path);
    if (!document_data.has_value())
        return nullptr;
    return document_data.value();
}

OwnPtr<CppComprehensionEngine::DocumentData> CppComprehensionEngine::create_document_data_for(String const& file)
{
    if (m_unfinished_documents.contains(file)) {
        return {};
    }
    m_unfinished_documents.set(file);
    ScopeGuard mark_finished([&file, this]() { m_unfinished_documents.remove(file); });
    auto document = filedb().get_or_create_from_filesystem(file);
    if (!document)
        return {};
    return create_document_data(document->text(), file);
}

void CppComprehensionEngine::set_document_data(String const& file, OwnPtr<DocumentData>&& data)
{
    m_documents.set(filedb().to_absolute_path(file), move(data));
}

Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::get_suggestions(String const& file, const GUI::TextPosition& autocomplete_position)
{
    Cpp::Position position { autocomplete_position.line(), autocomplete_position.column() > 0 ? autocomplete_position.column() - 1 : 0 };

    dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "CppComprehensionEngine position {}:{}", position.line, position.column);

    auto const* document_ptr = get_or_create_document_data(file);
    if (!document_ptr)
        return {};

    auto const& document = *document_ptr;
    auto containing_token = document.parser().token_at(position);

    if (containing_token.has_value() && containing_token->type() == Token::Type::IncludePath) {
        auto results = try_autocomplete_include(document, containing_token.value(), position);
        if (results.has_value())
            return results.value();
    }

    auto node = document.parser().node_at(position);
    if (!node) {
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", position.line, position.column);
        return {};
    }

    if (node->parent() && node->parent()->parent())
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node: {}, parent: {}, grandparent: {}", node->class_name(), node->parent()->class_name(), node->parent()->parent()->class_name());

    if (!node->parent())
        return {};

    auto results = try_autocomplete_property(document, *node, containing_token);
    if (results.has_value())
        return results.value();

    results = try_autocomplete_name(document, *node, containing_token);
    if (results.has_value())
        return results.value();
    return {};
}

Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_name(DocumentData const& document, ASTNode const& node, Optional<Token> containing_token) const
{
    auto partial_text = String::empty();
    if (containing_token.has_value() && containing_token.value().type() != Token::Type::ColonColon) {
        partial_text = containing_token.value().text();
    }
    return autocomplete_name(document, node, partial_text);
}

Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_property(DocumentData const& document, ASTNode const& node, Optional<Token> containing_token) const
{
    if (!containing_token.has_value())
        return {};

    if (!node.parent()->is_member_expression())
        return {};

    auto const& parent = static_cast<MemberExpression const&>(*node.parent());

    auto partial_text = String::empty();
    if (containing_token.value().type() != Token::Type::Dot) {
        if (&node != parent.property())
            return {};
        partial_text = containing_token.value().text();
    }

    return autocomplete_property(document, parent, partial_text);
}

Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::autocomplete_name(DocumentData const& document, ASTNode const& node, String const& partial_text) const
{
    auto reference_scope = scope_of_reference_to_symbol(node);
    auto current_scope = scope_of_node(node);

    auto symbol_matches = [&](Symbol const& symbol) {
        if (!is_symbol_available(symbol, current_scope, reference_scope)) {
            return false;
        }

        if (!symbol.name.name.starts_with(partial_text))
            return false;

        if (symbol.is_local) {
            // If this symbol was declared below us in a function, it's not available to us.
            bool is_unavailable = symbol.is_local && symbol.declaration->start().line > node.start().line;
            if (is_unavailable)
                return false;
        }

        return true;
    };

    Vector<Symbol> matches;

    for_each_available_symbol(document, [&](Symbol const& symbol) {
        if (symbol_matches(symbol)) {
            matches.append(symbol);
        }
        return IterationDecision::Continue;
    });

    Vector<GUI::AutocompleteProvider::Entry> suggestions;
    for (auto& symbol : matches) {
        suggestions.append({ symbol.name.name, partial_text.length() });
    }

    if (reference_scope.is_empty()) {
        for (auto& preprocessor_name : document.preprocessor().definitions().keys()) {
            if (preprocessor_name.starts_with(partial_text)) {
                suggestions.append({ preprocessor_name, partial_text.length() });
            }
        }
    }

    return suggestions;
}

Vector<StringView> CppComprehensionEngine::scope_of_reference_to_symbol(ASTNode const& node) const
{
    Name const* name = nullptr;
    if (node.is_name()) {
        // FIXME It looks like this code path is never taken
        name = reinterpret_cast<Name const*>(&node);
    } else if (node.is_identifier()) {
        auto* parent = node.parent();
        if (!(parent && parent->is_name()))
            return {};
        name = reinterpret_cast<Name const*>(parent);
    } else {
        return {};
    }

    VERIFY(name->is_name());

    Vector<StringView> scope_parts;
    for (auto& scope_part : name->scope()) {
        // If the target node is part of a scope reference, we want to end the scope chain before it.
        if (&scope_part == &node)
            break;
        scope_parts.append(scope_part.name());
    }
    return scope_parts;
}

Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::autocomplete_property(DocumentData const& document, MemberExpression const& parent, const String partial_text) const
{
    VERIFY(parent.object());
    auto type = type_of(document, *parent.object());
    if (type.is_null()) {
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "Could not infer type of object");
        return {};
    }

    Vector<GUI::AutocompleteProvider::Entry> suggestions;
    for (auto& prop : properties_of_type(document, type)) {
        if (prop.name.name.starts_with(partial_text)) {
            suggestions.append({ prop.name.name, partial_text.length() });
        }
    }
    return suggestions;
}

bool CppComprehensionEngine::is_property(ASTNode const& node) const
{
    if (!node.parent()->is_member_expression())
        return false;

    auto& parent = verify_cast<MemberExpression>(*node.parent());
    return parent.property() == &node;
}

String CppComprehensionEngine::type_of_property(DocumentData const& document, Identifier const& identifier) const
{
    auto& parent = verify_cast<MemberExpression>(*identifier.parent());
    VERIFY(parent.object());
    auto properties = properties_of_type(document, type_of(document, *parent.object()));
    for (auto& prop : properties) {
        if (prop.name.name != identifier.name())
            continue;
        Type const* type { nullptr };
        if (prop.declaration->is_variable_declaration()) {
            type = verify_cast<VariableDeclaration>(*prop.declaration).type();
        }
        if (!type)
            continue;
        if (!type->is_named_type())
            continue;

        VERIFY(verify_cast<NamedType>(*type).name());
        if (verify_cast<NamedType>(*type).name())
            return verify_cast<NamedType>(*type).name()->full_name();
        return String::empty();
    }
    return {};
}

String CppComprehensionEngine::type_of_variable(Identifier const& identifier) const
{
    ASTNode const* current = &identifier;
    while (current) {
        for (auto& decl : current->declarations()) {
            if (decl.is_variable_or_parameter_declaration()) {
                auto& var_or_param = verify_cast<VariableOrParameterDeclaration>(decl);
                if (var_or_param.full_name() == identifier.name() && var_or_param.type()->is_named_type()) {
                    VERIFY(verify_cast<NamedType>(*var_or_param.type()).name());
                    if (verify_cast<NamedType>(*var_or_param.type()).name())
                        return verify_cast<NamedType>(*var_or_param.type()).name()->full_name();
                    return String::empty();
                }
            }
        }
        current = current->parent();
    }
    return {};
}

String CppComprehensionEngine::type_of(DocumentData const& document, Expression const& expression) const
{
    if (expression.is_member_expression()) {
        auto& member_expression = verify_cast<MemberExpression>(expression);
        VERIFY(member_expression.property());
        if (member_expression.property()->is_identifier())
            return type_of_property(document, static_cast<Identifier const&>(*member_expression.property()));
        return {};
    }

    Identifier const* identifier { nullptr };
    if (expression.is_name()) {
        identifier = static_cast<Name const&>(expression).name();
    } else if (expression.is_identifier()) {
        identifier = &static_cast<Identifier const&>(expression);
    } else {
        dbgln("expected identifier or name, got: {}", expression.class_name());
        VERIFY_NOT_REACHED(); // TODO
    }
    VERIFY(identifier);
    if (is_property(*identifier))
        return type_of_property(document, *identifier);

    return type_of_variable(*identifier);
}

Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::properties_of_type(DocumentData const& document, String const& type) const
{
    auto type_symbol = SymbolName::create(type);
    auto decl = find_declaration_of(document, type_symbol);
    if (!decl) {
        dbgln("Couldn't find declaration of type: {}", type);
        return {};
    }

    if (!decl->is_struct_or_class()) {
        dbgln("Expected declaration of type: {} to be struct or class", type);
        return {};
    }

    auto& struct_or_class = verify_cast<StructOrClassDeclaration>(*decl);
    VERIFY(struct_or_class.full_name() == type_symbol.name);

    Vector<Symbol> properties;
    for (auto& member : struct_or_class.members()) {
        Vector<StringView> scope(type_symbol.scope);
        scope.append(type_symbol.name);
        // FIXME: We don't have to create the Symbol here, it should already exist in the 'm_symbol' table of some DocumentData we already parsed.
        properties.append(Symbol::create(member.full_name(), scope, member, Symbol::IsLocal::No));
    }
    return properties;
}

CppComprehensionEngine::Symbol CppComprehensionEngine::Symbol::create(StringView name, Vector<StringView> const& scope, NonnullRefPtr<Declaration> declaration, IsLocal is_local)
{
    return { { name, scope }, move(declaration), is_local == IsLocal::Yes };
}

Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::get_child_symbols(ASTNode const& node) const
{
    return get_child_symbols(node, {}, Symbol::IsLocal::No);
}

Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::get_child_symbols(ASTNode const& node, Vector<StringView> const& scope, Symbol::IsLocal is_local) const
{
    Vector<Symbol> symbols;

    for (auto& decl : node.declarations()) {
        symbols.append(Symbol::create(decl.full_name(), scope, decl, is_local));

        bool should_recurse = decl.is_namespace() || decl.is_struct_or_class() || decl.is_function();
        bool are_child_symbols_local = decl.is_function();

        if (!should_recurse)
            continue;

        auto new_scope = scope;
        new_scope.append(decl.full_name());
        symbols.extend(get_child_symbols(decl, new_scope, are_child_symbols_local ? Symbol::IsLocal::Yes : is_local));
    }

    return symbols;
}

String CppComprehensionEngine::document_path_from_include_path(StringView include_path) const
{
    static Regex<PosixExtended> library_include("<(.+)>");
    static Regex<PosixExtended> user_defined_include("\"(.+)\"");

    auto document_path_for_library_include = [&](StringView include_path) -> String {
        RegexResult result;
        if (!library_include.search(include_path, result))
            return {};

        auto path = result.capture_group_matches.at(0).at(0).view.string_view();
        return String::formatted("/usr/include/{}", path);
    };

    auto document_path_for_user_defined_include = [&](StringView include_path) -> String {
        RegexResult result;
        if (!user_defined_include.search(include_path, result))
            return {};

        return result.capture_group_matches.at(0).at(0).view.string_view();
    };

    auto result = document_path_for_library_include(include_path);
    if (result.is_null())
        result = document_path_for_user_defined_include(include_path);

    return result;
}

void CppComprehensionEngine::on_edit(String const& file)
{
    set_document_data(file, create_document_data_for(file));
}

void CppComprehensionEngine::file_opened([[maybe_unused]] String const& file)
{
    get_or_create_document_data(file);
}

Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_declaration_of(String const& filename, const GUI::TextPosition& identifier_position)
{
    auto const* document_ptr = get_or_create_document_data(filename);
    if (!document_ptr)
        return {};

    auto const& document = *document_ptr;
    auto decl = find_declaration_of(document, identifier_position);
    if (decl) {
        return GUI::AutocompleteProvider::ProjectLocation { decl->filename(), decl->start().line, decl->start().column };
    }

    return find_preprocessor_definition(document, identifier_position);
}

RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(DocumentData const& document, const GUI::TextPosition& identifier_position)
{
    auto node = document.parser().node_at(Cpp::Position { identifier_position.line(), identifier_position.column() });
    if (!node) {
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
        return {};
    }
    return find_declaration_of(document, *node);
}

Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_preprocessor_definition(DocumentData const& document, const GUI::TextPosition& text_position)
{
    Position cpp_position { text_position.line(), text_position.column() };
    auto substitution = find_preprocessor_substitution(document, cpp_position);
    if (!substitution.has_value())
        return {};
    return GUI::AutocompleteProvider::ProjectLocation { substitution->defined_value.filename, substitution->defined_value.line, substitution->defined_value.column };
}

Optional<Cpp::Preprocessor::Substitution> CppComprehensionEngine::find_preprocessor_substitution(DocumentData const& document, Cpp::Position const& cpp_position)
{
    // Search for a replaced preprocessor token that intersects with text_position
    for (auto& substitution : document.preprocessor().substitutions()) {
        if (substitution.original_tokens.first().start() > cpp_position)
            continue;
        if (substitution.original_tokens.first().end() < cpp_position)
            continue;
        return substitution;
    }
    return {};
}

struct TargetDeclaration {
    enum Type {
        Variable,
        Type,
        Function,
        Property,
        Scope
    } type;
    String name;
};

static Optional<TargetDeclaration> get_target_declaration(ASTNode const& node, String name);
static Optional<TargetDeclaration> get_target_declaration(ASTNode const& node)
{
    if (node.is_identifier()) {
        return get_target_declaration(node, static_cast<Identifier const&>(node).name());
    }

    if (node.is_declaration()) {
        return get_target_declaration(node, verify_cast<Declaration>(node).full_name());
    }

    if (node.is_type() && node.parent() && node.parent()->is_declaration()) {
        return get_target_declaration(*node.parent(), verify_cast<Declaration>(node.parent())->full_name());
    }

    dbgln("get_target_declaration: Invalid argument node of type: {}", node.class_name());
    return {};
}

static Optional<TargetDeclaration> get_target_declaration(ASTNode const& node, String name)
{
    if (node.parent() && node.parent()->is_name()) {
        auto& name_node = *verify_cast<Name>(node.parent());
        if (&node != name_node.name()) {
            // Node is part of scope reference chain
            return TargetDeclaration { TargetDeclaration::Type::Scope, name };
        }
        if (name_node.parent() && name_node.parent()->is_declaration()) {
            auto declaration = verify_cast<Declaration>(name_node.parent());
            if (declaration->is_struct_or_class() || declaration->is_enum()) {
                return TargetDeclaration { TargetDeclaration::Type::Type, name };
            }
            if (declaration->is_function()) {
                return TargetDeclaration { TargetDeclaration::Type::Function, name };
            }
        }
    }

    if ((node.parent() && node.parent()->is_function_call()) || (node.parent()->is_name() && node.parent()->parent() && node.parent()->parent()->is_function_call())) {
        return TargetDeclaration { TargetDeclaration::Type::Function, name };
    }

    if ((node.parent() && node.parent()->is_type()) || (node.parent()->is_name() && node.parent()->parent() && node.parent()->parent()->is_type()))
        return TargetDeclaration { TargetDeclaration::Type::Type, name };

    if ((node.parent() && node.parent()->is_member_expression()))
        return TargetDeclaration { TargetDeclaration::Type::Property, name };

    return TargetDeclaration { TargetDeclaration::Type::Variable, name };
}
RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(DocumentData const& document_data, ASTNode const& node) const
{
    dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "find_declaration_of: {} ({})", document_data.parser().text_of_node(node), node.class_name());

    auto target_decl = get_target_declaration(node);
    if (!target_decl.has_value())
        return {};

    auto reference_scope = scope_of_reference_to_symbol(node);
    auto current_scope = scope_of_node(node);

    auto symbol_matches = [&](Symbol const& symbol) {
        bool match_function = target_decl.value().type == TargetDeclaration::Function && symbol.declaration->is_function();
        bool match_variable = target_decl.value().type == TargetDeclaration::Variable && symbol.declaration->is_variable_declaration();
        bool match_type = target_decl.value().type == TargetDeclaration::Type && (symbol.declaration->is_struct_or_class() || symbol.declaration->is_enum());
        bool match_property = target_decl.value().type == TargetDeclaration::Property && symbol.declaration->parent()->is_declaration() && verify_cast<Declaration>(symbol.declaration->parent())->is_struct_or_class();
        bool match_parameter = target_decl.value().type == TargetDeclaration::Variable && symbol.declaration->is_parameter();
        bool match_scope = target_decl.value().type == TargetDeclaration::Scope && (symbol.declaration->is_namespace() || symbol.declaration->is_struct_or_class());

        if (match_property) {
            // FIXME: This is not really correct, we also need to check that the type of the struct/class matches (not just the property name)
            if (symbol.name.name == target_decl.value().name) {
                return true;
            }
        }

        if (!is_symbol_available(symbol, current_scope, reference_scope)) {
            return false;
        }

        if (match_function || match_type || match_scope) {
            if (symbol.name.name == target_decl->name)
                return true;
        }

        if (match_variable || match_parameter) {
            // If this symbol was declared below us in a function, it's not available to us.
            bool is_unavailable = symbol.is_local && symbol.declaration->start().line > node.start().line;

            if (!is_unavailable && (symbol.name.name == target_decl->name)) {
                return true;
            }
        }

        return false;
    };

    Optional<Symbol> match;

    for_each_available_symbol(document_data, [&](Symbol const& symbol) {
        if (symbol_matches(symbol)) {
            match = symbol;
            return IterationDecision::Break;
        }
        return IterationDecision::Continue;
    });

    if (!match.has_value())
        return {};

    return match->declaration;
}

void CppComprehensionEngine::update_declared_symbols(DocumentData& document)
{
    for (auto& symbol : get_child_symbols(*document.parser().root_node())) {
        document.m_symbols.set(symbol.name, move(symbol));
    }

    Vector<GUI::AutocompleteProvider::Declaration> declarations;
    for (auto& symbol_entry : document.m_symbols) {
        auto& symbol = symbol_entry.value;
        declarations.append({ symbol.name.name, { document.filename(), symbol.declaration->start().line, symbol.declaration->start().column }, type_of_declaration(symbol.declaration), symbol.name.scope_as_string() });
    }

    for (auto& definition : document.preprocessor().definitions()) {
        declarations.append({ definition.key, { document.filename(), definition.value.line, definition.value.column }, GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition, {} });
    }
    set_declarations_of_document(document.filename(), move(declarations));
}

void CppComprehensionEngine::update_todo_entries(DocumentData& document)
{
    set_todo_entries_of_document(document.filename(), document.parser().get_todo_entries());
}

GUI::AutocompleteProvider::DeclarationType CppComprehensionEngine::type_of_declaration(Declaration const& decl)
{
    if (decl.is_struct())
        return GUI::AutocompleteProvider::DeclarationType::Struct;
    if (decl.is_class())
        return GUI::AutocompleteProvider::DeclarationType::Class;
    if (decl.is_function())
        return GUI::AutocompleteProvider::DeclarationType::Function;
    if (decl.is_variable_declaration())
        return GUI::AutocompleteProvider::DeclarationType::Variable;
    if (decl.is_namespace())
        return GUI::AutocompleteProvider::DeclarationType::Namespace;
    if (decl.is_member())
        return GUI::AutocompleteProvider::DeclarationType::Member;
    return GUI::AutocompleteProvider::DeclarationType::Variable;
}

OwnPtr<CppComprehensionEngine::DocumentData> CppComprehensionEngine::create_document_data(String&& text, String const& filename)
{
    auto document_data = make<DocumentData>();
    document_data->m_filename = filename;
    document_data->m_text = move(text);
    document_data->m_preprocessor = make<Preprocessor>(document_data->m_filename, document_data->text());
    document_data->preprocessor().set_ignore_unsupported_keywords(true);
    document_data->preprocessor().set_ignore_invalid_statements(true);
    document_data->preprocessor().set_keep_include_statements(true);

    document_data->preprocessor().definitions_in_header_callback = [this](StringView include_path) -> Preprocessor::Definitions {
        auto included_document = get_or_create_document_data(document_path_from_include_path(include_path));
        if (!included_document)
            return {};

        return included_document->preprocessor().definitions();
    };

    auto tokens = document_data->preprocessor().process_and_lex();

    for (auto include_path : document_data->preprocessor().included_paths()) {
        auto include_fullpath = document_path_from_include_path(include_path);
        auto included_document = get_or_create_document_data(include_fullpath);
        if (!included_document)
            continue;

        document_data->m_available_headers.set(include_fullpath);

        for (auto& header : included_document->m_available_headers)
            document_data->m_available_headers.set(header);
    }

    document_data->m_parser = make<Parser>(move(tokens), filename);

    auto root = document_data->parser().parse();

    if constexpr (CPP_LANGUAGE_SERVER_DEBUG)
        root->dump();

    update_declared_symbols(*document_data);
    update_todo_entries(*document_data);

    return document_data;
}

Vector<StringView> CppComprehensionEngine::scope_of_node(ASTNode const& node) const
{

    auto parent = node.parent();
    if (!parent)
        return {};

    auto parent_scope = scope_of_node(*parent);

    if (!parent->is_declaration())
        return parent_scope;

    auto& parent_decl = static_cast<Declaration&>(*parent);

    StringView containing_scope;
    if (parent_decl.is_namespace())
        containing_scope = static_cast<NamespaceDeclaration&>(parent_decl).full_name();
    if (parent_decl.is_struct_or_class())
        containing_scope = static_cast<StructOrClassDeclaration&>(parent_decl).full_name();
    if (parent_decl.is_function())
        containing_scope = static_cast<FunctionDeclaration&>(parent_decl).full_name();

    parent_scope.append(containing_scope);
    return parent_scope;
}

Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_include(DocumentData const&, Token include_path_token, Cpp::Position const& cursor_position) const
{
    VERIFY(include_path_token.type() == Token::Type::IncludePath);
    auto partial_include = include_path_token.text().trim_whitespace();

    enum IncludeType {
        Project,
        System,
    } include_type { Project };

    String include_root;
    bool already_has_suffix = false;
    if (partial_include.starts_with("<")) {
        include_root = "/usr/include/";
        include_type = System;
        if (partial_include.ends_with(">")) {
            already_has_suffix = true;
            partial_include = partial_include.substring_view(0, partial_include.length() - 1).trim_whitespace();
        }
    } else if (partial_include.starts_with("\"")) {
        include_root = filedb().project_root();
        if (partial_include.length() > 1 && partial_include.ends_with("\"")) {
            already_has_suffix = true;
            partial_include = partial_include.substring_view(0, partial_include.length() - 1).trim_whitespace();
        }
    } else
        return {};

    // The cursor is past the end of the <> or "", and so should not trigger autocomplete.
    if (already_has_suffix && include_path_token.end() <= cursor_position)
        return {};

    auto last_slash = partial_include.find_last('/');
    auto include_dir = String::empty();
    auto partial_basename = partial_include.substring_view((last_slash.has_value() ? last_slash.value() : 0) + 1);
    if (last_slash.has_value()) {
        include_dir = partial_include.substring_view(1, last_slash.value());
    }

    auto full_dir = LexicalPath::join(include_root, include_dir).string();
    dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "searching path: {}, partial_basename: {}", full_dir, partial_basename);

    Core::DirIterator it(full_dir, Core::DirIterator::Flags::SkipDots);
    Vector<GUI::AutocompleteProvider::Entry> options;

    auto prefix = include_type == System ? "<" : "\"";
    auto suffix = include_type == System ? ">" : "\"";
    while (it.has_next()) {
        auto path = it.next_path();

        if (!path.starts_with(partial_basename))
            continue;

        if (Core::File::is_directory(LexicalPath::join(full_dir, path).string())) {
            // FIXME: Don't dismiss the autocomplete when filling these suggestions.
            auto completion = String::formatted("{}{}{}/", prefix, include_dir, path);
            options.empend(completion, include_dir.length() + partial_basename.length() + 1, GUI::AutocompleteProvider::Language::Cpp, path, GUI::AutocompleteProvider::Entry::HideAutocompleteAfterApplying::No);
        } else if (path.ends_with(".h")) {
            // FIXME: Place the cursor after the trailing > or ", even if it was
            //        already typed.
            auto completion = String::formatted("{}{}{}{}", prefix, include_dir, path, already_has_suffix ? "" : suffix);
            options.empend(completion, include_dir.length() + partial_basename.length() + 1, GUI::AutocompleteProvider::Language::Cpp, path);
        }
    }

    return options;
}

RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(CppComprehensionEngine::DocumentData const& document, CppComprehensionEngine::SymbolName const& target_symbol_name) const
{
    RefPtr<Declaration> target_declaration;
    for_each_available_symbol(document, [&](Symbol const& symbol) {
        if (symbol.name == target_symbol_name) {
            target_declaration = symbol.declaration;
            return IterationDecision::Break;
        }
        return IterationDecision::Continue;
    });
    return target_declaration;
}

String CppComprehensionEngine::SymbolName::scope_as_string() const
{
    if (scope.is_empty())
        return String::empty();

    StringBuilder builder;
    for (size_t i = 0; i < scope.size() - 1; ++i) {
        builder.appendff("{}::", scope[i]);
    }
    builder.append(scope.last());
    return builder.to_string();
}

CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView name, Vector<StringView>&& scope)
{
    return { name, move(scope) };
}

CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView qualified_name)
{
    auto parts = qualified_name.split_view("::");
    VERIFY(!parts.is_empty());
    auto name = parts.take_last();
    return SymbolName::create(name, move(parts));
}

String CppComprehensionEngine::SymbolName::to_string() const
{
    if (scope.is_empty())
        return name;
    return String::formatted("{}::{}", scope_as_string(), name);
}

bool CppComprehensionEngine::is_symbol_available(Symbol const& symbol, Vector<StringView> const& current_scope, Vector<StringView> const& reference_scope)
{

    if (!reference_scope.is_empty()) {
        return reference_scope == symbol.name.scope;
    }

    // FIXME: Take "using namespace ..." into consideration

    // Check if current_scope starts with symbol's scope
    if (symbol.name.scope.size() > current_scope.size())
        return false;

    for (size_t i = 0; i < symbol.name.scope.size(); ++i) {
        if (current_scope[i] != symbol.name.scope[i])
            return false;
    }

    return true;
}

Optional<CodeComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get_function_params_hint(String const& filename, const GUI::TextPosition& identifier_position)
{
    auto const* document_ptr = get_or_create_document_data(filename);
    if (!document_ptr)
        return {};

    auto const& document = *document_ptr;
    Cpp::Position cpp_position { identifier_position.line(), identifier_position.column() };
    auto node = document.parser().node_at(cpp_position);
    if (!node) {
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
        return {};
    }

    dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node type: {}", node->class_name());

    FunctionCall* call_node { nullptr };

    if (node->is_function_call()) {
        call_node = verify_cast<FunctionCall>(node.ptr());

        auto token = document.parser().token_at(cpp_position);

        // If we're in a function call with 0 arguments
        if (token.has_value() && (token->type() == Token::Type::LeftParen || token->type() == Token::Type::RightParen)) {
            return get_function_params_hint(document, *call_node, call_node->arguments().is_empty() ? 0 : call_node->arguments().size() - 1);
        }
    }

    // Walk upwards in the AST to find a FunctionCall node
    while (!call_node && node) {
        auto parent_is_call = node->parent() && node->parent()->is_function_call();
        if (parent_is_call) {
            call_node = verify_cast<FunctionCall>(node->parent());
            break;
        }
        node = node->parent();
    }

    if (!call_node) {
        dbgln("did not find function call");
        return {};
    }

    Optional<size_t> invoked_arg_index;
    for (size_t arg_index = 0; arg_index < call_node->arguments().size(); ++arg_index) {
        if (&call_node->arguments()[arg_index] == node.ptr()) {
            invoked_arg_index = arg_index;
            break;
        }
    }
    if (!invoked_arg_index.has_value()) {
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "could not find argument index, defaulting to the last argument");
        invoked_arg_index = call_node->arguments().is_empty() ? 0 : call_node->arguments().size() - 1;
    }

    dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "arg index: {}", invoked_arg_index.value());
    return get_function_params_hint(document, *call_node, invoked_arg_index.value());
}

Optional<CppComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get_function_params_hint(
    DocumentData const& document,
    FunctionCall& call_node,
    size_t argument_index)
{
    Identifier const* callee = nullptr;
    VERIFY(call_node.callee());
    if (call_node.callee()->is_identifier()) {
        callee = verify_cast<Identifier>(call_node.callee());
    } else if (call_node.callee()->is_name()) {
        callee = verify_cast<Name>(*call_node.callee()).name();
    } else if (call_node.callee()->is_member_expression()) {
        auto& member_exp = verify_cast<MemberExpression>(*call_node.callee());
        VERIFY(member_exp.property());
        if (member_exp.property()->is_identifier()) {
            callee = verify_cast<Identifier>(member_exp.property());
        }
    }

    if (!callee) {
        dbgln("unexpected node type for function call: {}", call_node.callee()->class_name());
        return {};
    }
    VERIFY(callee);

    auto decl = find_declaration_of(document, *callee);
    if (!decl) {
        dbgln("func decl not found");
        return {};
    }
    if (!decl->is_function()) {
        dbgln("declaration is not a function");
        return {};
    }

    auto& func_decl = verify_cast<FunctionDeclaration>(*decl);
    auto document_of_declaration = get_document_data(func_decl.filename());

    FunctionParamsHint hint {};
    hint.current_index = argument_index;
    for (auto& arg : func_decl.parameters()) {
        Vector<StringView> tokens_text;
        for (auto token : document_of_declaration->parser().tokens_in_range(arg.start(), arg.end())) {
            tokens_text.append(token.text());
        }
        hint.params.append(String::join(" ", tokens_text));
    }

    return hint;
}

Vector<GUI::AutocompleteProvider::TokenInfo> CppComprehensionEngine::get_tokens_info(String const& filename)
{
    dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "CppComprehensionEngine::get_tokens_info: {}", filename);

    auto const* document_ptr = get_or_create_document_data(filename);
    if (!document_ptr)
        return {};

    auto const& document = *document_ptr;

    Vector<GUI::AutocompleteProvider::TokenInfo> tokens_info;
    size_t i = 0;
    for (auto const& token : document.preprocessor().unprocessed_tokens()) {

        tokens_info.append({ get_token_semantic_type(document, token),
            token.start().line, token.start().column, token.end().line, token.end().column });
        ++i;
        dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "{}: {}", token.text(), GUI::AutocompleteProvider::TokenInfo::type_to_string(tokens_info.last().type));
    }
    return tokens_info;
}

GUI::AutocompleteProvider::TokenInfo::SemanticType CppComprehensionEngine::get_token_semantic_type(DocumentData const& document, Token const& token)
{
    using GUI::AutocompleteProvider;
    switch (token.type()) {
    case Cpp::Token::Type::Identifier:
        return get_semantic_type_for_identifier(document, token.start());
    case Cpp::Token::Type::Keyword:
        return AutocompleteProvider::TokenInfo::SemanticType::Keyword;
    case Cpp::Token::Type::KnownType:
        return AutocompleteProvider::TokenInfo::SemanticType::Type;
    case Cpp::Token::Type::DoubleQuotedString:
    case Cpp::Token::Type::SingleQuotedString:
    case Cpp::Token::Type::RawString:
        return AutocompleteProvider::TokenInfo::SemanticType::String;
    case Cpp::Token::Type::Integer:
    case Cpp::Token::Type::Float:
        return AutocompleteProvider::TokenInfo::SemanticType::Number;
    case Cpp::Token::Type::IncludePath:
        return AutocompleteProvider::TokenInfo::SemanticType::IncludePath;
    case Cpp::Token::Type::EscapeSequence:
        return AutocompleteProvider::TokenInfo::SemanticType::Keyword;
    case Cpp::Token::Type::PreprocessorStatement:
    case Cpp::Token::Type::IncludeStatement:
        return AutocompleteProvider::TokenInfo::SemanticType::PreprocessorStatement;
    case Cpp::Token::Type::Comment:
        return AutocompleteProvider::TokenInfo::SemanticType::Comment;
    default:
        return AutocompleteProvider::TokenInfo::SemanticType::Unknown;
    }
}

GUI::AutocompleteProvider::TokenInfo::SemanticType CppComprehensionEngine::get_semantic_type_for_identifier(DocumentData const& document, Position position)
{
    if (find_preprocessor_substitution(document, position).has_value())
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::PreprocessorMacro;

    auto decl = find_declaration_of(document, GUI::TextPosition { position.line, position.column });
    if (!decl)
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::Identifier;

    if (decl->is_function())
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::Function;
    if (decl->is_parameter())
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::Parameter;
    if (decl->is_variable_declaration()) {
        if (decl->is_member())
            return GUI::AutocompleteProvider::TokenInfo::SemanticType::Member;
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::Variable;
    }
    if (decl->is_struct_or_class() || decl->is_enum())
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::CustomType;
    if (decl->is_namespace())
        return GUI::AutocompleteProvider::TokenInfo::SemanticType::Namespace;

    return GUI::AutocompleteProvider::TokenInfo::SemanticType::Identifier;
}

}