• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 13241966727

10 Feb 2025 01:27PM UTC coverage: 78.821% (+0.006%) from 78.815%
13241966727

push

github

SuperFola
fix(ci): use valgrind 3.24 from ftp.us.debian.org instead of 3.20 as it disappeared

5828 of 7394 relevant lines covered (78.82%)

22751.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.47
/src/arkreactor/Compiler/NameResolution/ScopeResolver.cpp
1
#include <Ark/Compiler/NameResolution/ScopeResolver.hpp>
2

3
#include <ranges>
4

5
namespace Ark::internal
6
{
7
    ScopeResolver::ScopeResolver()
156✔
8
    {
156✔
9
        createNewNamespace("", /* with_prefix= */ false, /* is_glob= */ true, /* symbols= */ {});
156✔
10
    }
156✔
11

12
    void ScopeResolver::createNew()
1,720✔
13
    {
1,720✔
14
        m_scopes.emplace_back(std::make_unique<StaticScope>());
1,720✔
15
    }
1,720✔
16

17
    void ScopeResolver::removeLastScope()
1,718✔
18
    {
1,718✔
19
        m_scopes.pop_back();
1,718✔
20
    }
1,718✔
21

22
    void ScopeResolver::createNewNamespace(const std::string& name, bool with_prefix, bool is_glob, const std::vector<std::string>& symbols)
232✔
23
    {
232✔
24
        m_scopes.emplace_back(std::make_unique<NamespaceScope>(name, with_prefix, is_glob, symbols));
232✔
25
    }
232✔
26

27
    std::string ScopeResolver::registerInCurrent(const std::string& name, const bool is_mutable)
4,784✔
28
    {
4,784✔
29
        return m_scopes.back()->add(name, is_mutable);
4,784✔
30
    }
31

32
    void ScopeResolver::saveNamespaceAndRemove()
75✔
33
    {
75✔
34
        for (auto& m_scope : std::ranges::reverse_view(m_scopes) | std::ranges::views::drop(1))
150✔
35
        {
36
            if (m_scope->saveNamespace(m_scopes.back()))
75✔
37
                break;
75✔
38
        }
75✔
39

40
        m_scopes.pop_back();
75✔
41
    }
75✔
42

43
    std::optional<bool> ScopeResolver::isImmutable(const std::string& name) const
802✔
44
    {
802✔
45
        for (const auto& m_scope : std::ranges::reverse_view(m_scopes))
2,336✔
46
        {
47
            if (auto maybe = m_scope->get(name, true); maybe.has_value())
2,336✔
48
                return !maybe.value().is_mutable;
802✔
49
        }
1,534✔
50
        return std::nullopt;
×
51
    }
802✔
52

53
    bool ScopeResolver::isRegistered(const std::string& name) const
1,004✔
54
    {
1,004✔
55
        for (const auto& m_scope : std::ranges::reverse_view(m_scopes))
2,967✔
56
        {
57
            if (m_scope->get(name, true).has_value())
1,963✔
58
                return true;
999✔
59
        }
1,963✔
60
        return false;
5✔
61
    }
1,004✔
62

63
    bool ScopeResolver::isInScope(const std::string& name) const
3,232✔
64
    {
3,232✔
65
        return m_scopes.back()->get(name, false).has_value();
3,232✔
66
    }
67

68
    std::string ScopeResolver::getFullyQualifiedNameInNearestScope(const std::string& name) const
21,913✔
69
    {
21,913✔
70
        std::optional<std::string> maybe_name;
21,913✔
71
        for (const auto& scope : std::ranges::reverse_view(m_scopes))
115,822✔
72
        {
73
            if (auto maybe_fqn = scope->get(name, true); maybe_fqn.has_value())
105,912✔
74
            {
75
                // priorize non-hidden symbols
76
                if ((maybe_name.has_value() && maybe_name.value().ends_with("#hidden")) || !maybe_name.has_value())
12,003✔
77
                    maybe_name = maybe_fqn.value().name;
11,292✔
78
            }
12,003✔
79
        }
93,909✔
80
        return maybe_name.value_or(name);
21,913✔
81
    }
21,913✔
82

83
    std::pair<bool, std::string> ScopeResolver::canFullyQualifyName(const std::string& name)
19,018✔
84
    {
19,018✔
85
        // a given name can be fully qualified if
86
        // old == new
87
        // old != new and new has prefix
88
        //     if the prefix namespace is glob
89
        //     if the prefix namespace has name in its symbols
90
        //     if the prefix namespace is with_prefix && it is the top most scope
91
        const std::string maybe_fqn = getFullyQualifiedNameInNearestScope(name);
19,018✔
92

93
        if (maybe_fqn == name)
19,018✔
94
            return std::make_pair(true, maybe_fqn);
18,663✔
95

96
        const std::string prefix = maybe_fqn.substr(0, maybe_fqn.find_first_of(':'));
355✔
97
        const std::string unprefixed_name = name.substr(name.find_first_of(':') + 1);
355✔
98
        auto namespaces =
355✔
99
            std::ranges::reverse_view(m_scopes) | std::ranges::views::filter([](const auto& e) {
1,026✔
100
                return e->isNamespace();
671✔
101
            });
102
        bool top = true;
355✔
103
        for (auto& scope : namespaces)
710✔
104
        {
105
            if (top && prefix == scope->prefix())
355✔
106
                return std::make_pair(true, maybe_fqn);
330✔
107
            if (!top && prefix == scope->prefix() && (scope->isGlob() || scope->hasSymbol(name)))
25✔
108
                return std::make_pair(true, maybe_fqn);
×
109

110
            // check for the presence of the symbol in symbol imports and glob imports
111
            if (scope->recursiveHasSymbol(unprefixed_name))
25✔
112
                return std::make_pair(true, maybe_fqn);
23✔
113

114
            top = false;
2✔
115
        }
355✔
116

117
        return std::make_pair(false, maybe_fqn);
2✔
118
    }
19,018✔
119

120
    StaticScope* ScopeResolver::currentScope() const
76✔
121
    {
76✔
122
        if (!m_scopes.empty()) [[likely]]
76✔
123
            return m_scopes.back().get();
76✔
124
        return nullptr;
×
125
    }
76✔
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc