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

ArkScript-lang / Ark / 12206460859

06 Dec 2024 09:32PM UTC coverage: 77.864% (+0.05%) from 77.815%
12206460859

push

github

SuperFola
refactor(name resolution): removing dead code and refactoring code when assigning names to symbols in namespaces

7 of 7 new or added lines in 2 files covered. (100.0%)

24 existing lines in 5 files now uncovered.

5438 of 6984 relevant lines covered (77.86%)

9338.42 hits per line

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

92.11
/src/arkreactor/Compiler/NameResolution/StaticScope.cpp
1
#include <Ark/Compiler/NameResolution/StaticScope.hpp>
2

3
#include <utility>
4
#include <fmt/format.h>
5

6
namespace Ark::internal
7
{
8
    std::string StaticScope::add(const std::string& name, bool is_mutable)
1,212✔
9
    {
1,212✔
10
        m_vars.emplace(name, name, is_mutable);
1,212✔
11
        return name;
1,212✔
12
    }
13

14
    std::optional<Declaration> StaticScope::get(const std::string& name, [[maybe_unused]] const bool extensive_lookup)
10,569✔
15
    {
10,569✔
16
        if (const auto it = std::ranges::find(m_vars, name, &Declaration::name); it != m_vars.end())
13,587✔
17
            return *it;
3,018✔
18
        return std::nullopt;
7,551✔
19
    }
10,569✔
20

UNCOV
21
    std::string StaticScope::fullyQualifiedName(const std::string& name) const
×
UNCOV
22
    {
×
UNCOV
23
        return name;
×
24
    }
25

26
    bool StaticScope::saveNamespace([[maybe_unused]] std::unique_ptr<StaticScope>&)
×
UNCOV
27
    {
×
28
        // the scope can not be saved on a static scope
UNCOV
29
        return false;
×
30
    }
31

32
    bool StaticScope::isNamespace() const
182✔
33
    {
182✔
34
        return false;
182✔
35
    }
36

37
    NamespaceScope::NamespaceScope(std::string name, const bool with_prefix, const bool is_glob, const std::vector<std::string>& symbols) :
286✔
38
        StaticScope(),
143✔
39
        m_namespace(std::move(name)),
143✔
40
        m_with_prefix(with_prefix),
143✔
41
        m_is_glob(is_glob),
143✔
42
        m_symbols(symbols)
143✔
43
    {}
286✔
44

45
    std::string NamespaceScope::add(const std::string& name, bool is_mutable)
489✔
46
    {
489✔
47
        // Since we do multiple passes on namespaces, we need to check if the given name is already hidden,
48
        // so that we can save the name as it was on the first pass
49
        if (name.ends_with("#hidden"))
489✔
50
        {
51
            std::string std_name = name.substr(0, name.find_first_of('#'));
128✔
52
            return m_vars.emplace(name, std_name, is_mutable).first->name;
128✔
53
        }
128✔
54

55
        // Otherwise, we also have to check for the presence of a namespace prefix,
56
        // and remove it when checking against the symbols list, to determine if we
57
        // need to hide the name or not
58
        const bool starts_with_prefix = !m_namespace.empty() && name.starts_with(m_namespace + ":");
361✔
59
        std::string fqn = fullyQualifiedName(name);
361✔
60
        std::string unprefixed_name = starts_with_prefix ? name.substr(name.find_first_of(':') + 1) : name;
361✔
61

62
        if (!m_symbols.empty() && !hasSymbol(unprefixed_name))
361✔
63
            return m_vars.emplace(fqn + "#hidden", fqn, is_mutable).first->name;
20✔
64
        return m_vars.emplace(fqn, fqn, is_mutable).first->name;
341✔
65
    }
489✔
66

67
    std::optional<Declaration> NamespaceScope::get(const std::string& name, const bool extensive_lookup)
44,278✔
68
    {
44,278✔
69
        const bool starts_with_prefix = !m_namespace.empty() && name.starts_with(m_namespace + ":");
44,278✔
70
        // If the name starts with the namespace and we imported the namespace with prefix
71
        // search for name in the namespace
72
        if (starts_with_prefix && m_with_prefix)
44,278✔
73
        {
74
            if (const auto it = std::ranges::find(m_vars, name, &Declaration::name); it != m_vars.end())
3,676✔
75
                return *it;
1,823✔
76
        }
30✔
77
        // If the name does not start with the prefix, and we import through either glob or symbol list
78
        // search for the name in the namespace
79
        // If the name does not start with the prefix, in a namespace with a symbol list but can be resolved,
80
        // modify it to hide it to the end user
81
        // If the name wasn't qualified, in a prefixed namespace, look up for it but by qualifying the name
82
        else if (!starts_with_prefix)
42,425✔
83
        {
84
            auto it = std::ranges::find(m_vars, fullyQualifiedName(name), &Declaration::name);
41,857✔
85
            auto it_original = std::ranges::find(m_vars, fullyQualifiedName(name), &Declaration::original_name);
41,857✔
86
            if ((m_is_glob || hasSymbol(name) || m_with_prefix) && it != m_vars.end())
41,857✔
87
                return *it;
362✔
88
            if (!m_symbols.empty() && it_original != m_vars.end())
41,495✔
89
                return *it_original;
13✔
90
        }
41,857✔
91
        // lookup in the additional saved namespaces
92
        if (extensive_lookup)
42,080✔
93
        {
94
            for (const auto& scope : m_additional_namespaces)
70,313✔
95
            {
96
                if (auto maybe_decl = scope->get(name, extensive_lookup); maybe_decl.has_value())
28,616✔
97
                    return maybe_decl;
2,336✔
98
            }
28,616✔
99
        }
39,361✔
100
        // otherwise we didn't find the name in the namespace
101
        return std::nullopt;
39,744✔
102
    }
44,278✔
103

104
    std::string NamespaceScope::fullyQualifiedName(const std::string& name) const
84,075✔
105
    {
84,075✔
106
        const bool starts_with_prefix = !m_namespace.empty() && name.starts_with(m_namespace + ":");
84,075✔
107
        if (!m_namespace.empty() && !starts_with_prefix)
84,075✔
108
            return fmt::format("{}:{}", m_namespace, name);
71,843✔
109
        return name;
12,232✔
110
    }
84,075✔
111

112
    bool NamespaceScope::saveNamespace(std::unique_ptr<StaticScope>& scope)
27✔
113
    {
27✔
114
        m_additional_namespaces.push_back(std::move(scope));
27✔
115
        return true;
27✔
116
    }
117

118
    bool NamespaceScope::isNamespace() const
204✔
119
    {
204✔
120
        return true;
204✔
121
    }
122
}
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