diff options
| author | Patrick Schönberger | 2021-08-11 18:02:56 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2021-08-11 18:02:56 +0200 |
| commit | 17860defa84c6d8bc0e8bc088a7e09361f17db07 (patch) | |
| tree | 87d602a2d4419307e73928bc1993a6295ba2cb39 /src/find.h | |
| parent | 3715a3f575b615f66e8ea7e57f83849e8bae4deb (diff) | |
| download | toc-17860defa84c6d8bc0e8bc088a7e09361f17db07.tar.gz toc-17860defa84c6d8bc0e8bc088a7e09361f17db07.zip | |
structs and functions in ctx
Diffstat (limited to 'src/find.h')
| -rw-r--r-- | src/find.h | 213 |
1 files changed, 69 insertions, 144 deletions
@@ -27,202 +27,127 @@ opt<T *> findPtr(const std::vector<T> & ts, std::function<bool(T)> f) return nullopt;
}
-opt<Function> findFunction(
- const Program & p,
- const std::string & name,
- const std::vector<std::string> & namespacePrefixes)
+bool checkNamespace(std::shared_ptr<Context> ctx, const std::vector<std::string> & namespacePrefix)
{
- if (namespacePrefixes.empty())
- {
- return find<Function>(p.functions, [&](Function f) { return f.name == name; });
- }
- auto n = find<Namespace>(p.namespaces, [&](Namespace n) { return n.name == namespacePrefixes[0]; });
-
- if (!n.has_value())
- return nullopt;
-
- std::vector<Namespace> namespaces = { n.value() };
+ bool prefixMatches = true;
+
+ auto nIt = ctx;
+ for (int i = namespacePrefix.size() - 1; i >= 0; i--)
+ {
+ const std::string & prefix = namespacePrefix[i];
+ if (nIt == nullptr || ! nIt->name.has_value() || nIt->name.value() != prefix)
+ {
+ prefixMatches = false;
+ break;
+ }
+ nIt = nIt->parent;
+ }
+
+ return prefixMatches;
+}
- for (int i = 1; i < namespacePrefixes.size(); i++)
- {
- n = find<Namespace>(n.value().namespaces, [&](Namespace n) { return n.name == namespacePrefixes[i]; });
-
- if (!n.has_value())
- return nullopt;
- namespaces.push_back(n.value());
- }
- for (int i = namespaces.size()-1; i >= 0; i--)
+opt<Function> findFunction(
+ const std::string & name,
+ const std::vector<std::string> & namespacePrefix,
+ std::shared_ptr<Context> ctx)
+{
+ for (auto it = ctx; it != nullptr; it = it->parent)
{
- auto f = find<Function>(namespaces[i].functions, [&](Function f) { return f.name == name; });
- if (f.has_value())
- return f.value();
+ auto f = find<Function>(it->functions, [&](Function f) { return f.name == name; });
+ if (f.has_value() && checkNamespace(it, namespacePrefix))
+ return f;
}
-
- return find<Function>(p.functions, [&](Function f) { return f.name == name; });
+ return nullopt;
}
opt<Function *> findFunctionPtr(
- const Program & p,
const std::string & name,
- const std::vector<std::string> & namespacePrefixes)
+ const std::vector<std::string> & namespacePrefix,
+ std::shared_ptr<Context> ctx)
{
- if (namespacePrefixes.empty())
+ for (auto it = ctx; it != nullptr; it = it->parent)
{
- return findPtr<Function>(p.functions, [&](Function f) { return f.name == name; });
- }
-
- auto n = find<Namespace>(p.namespaces, [&](Namespace n) { return n.name == namespacePrefixes[0]; });
-
- if (!n.has_value())
- return nullopt;
-
- std::vector<Namespace> namespaces = { n.value() };
-
- for (int i = 1; i < namespacePrefixes.size(); i++)
- {
- n = find<Namespace>(n.value().namespaces, [&](Namespace n) { return n.name == namespacePrefixes[i]; });
-
- if (!n.has_value())
- return nullopt;
-
- namespaces.push_back(n.value());
+ auto f = findPtr<Function>(it->functions, [&](Function f) { return f.name == name; });
+ if (f.has_value() && checkNamespace(it, namespacePrefix))
+ return f;
}
+ return nullopt;
+}
- for (int i = namespaces.size()-1; i >= 0; i--)
- {
- auto f = findPtr<Function>(namespaces[i].functions, [&](Function f) { return f.name == name; });
- if (f.has_value())
- return f.value();
- }
- return findPtr<Function>(p.functions, [&](Function f) { return f.name == name; });
-}
opt<Struct> findStruct(
- const Program & p,
const std::string & name,
- const std::vector<std::string> & namespacePrefixes)
+ const std::vector<std::string> & namespacePrefix,
+ std::shared_ptr<Context> ctx)
{
- if (namespacePrefixes.empty())
+ for (auto it = ctx; it != nullptr; it = it->parent)
{
- return find<Struct>(p.structs, [&](Struct s) { return s.name == name; });
+ auto s = find<Struct>(it->structs, [&](Struct s) { return s.name == name; });
+ if (s.has_value() && checkNamespace(it, namespacePrefix))
+ return s;
}
-
- auto n = find<Namespace>(p.namespaces, [&](Namespace n) { return n.name == namespacePrefixes[0]; });
-
- if (!n.has_value())
- return nullopt;
-
- std::vector<Namespace> namespaces = { n.value() };
-
- for (int i = 1; i < namespacePrefixes.size(); i++)
- {
- n = find<Namespace>(n.value().namespaces, [&](Namespace n) { return n.name == namespacePrefixes[i]; });
-
- if (!n.has_value())
- return nullopt;
-
- namespaces.push_back(n.value());
- }
-
- for (int i = namespaces.size()-1; i >= 0; i--)
- {
- auto f = find<Struct>(namespaces[i].structs, [&](Struct f) { return f.name == name; });
- if (f.has_value())
- return f.value();
- }
-
- return find<Struct>(n.value().structs, [&](Struct s) { return s.name == name; });
+ return nullopt;
}
opt<Struct *> findStructPtr(
- const Program & p,
const std::string & name,
- const std::vector<std::string> & namespacePrefixes)
+ const std::vector<std::string> & namespacePrefix,
+ std::shared_ptr<Context> ctx)
{
- if (namespacePrefixes.empty())
- {
- return findPtr<Struct>(p.structs, [&](Struct s) { return s.name == name; });
- }
-
- auto n = find<Namespace>(p.namespaces, [&](Namespace n) { return n.name == namespacePrefixes[0]; });
-
- if (!n.has_value())
- return nullopt;
-
- std::vector<Namespace> namespaces = { n.value() };
-
- for (int i = 1; i < namespacePrefixes.size(); i++)
+ for (auto it = ctx; it != nullptr; it = it->parent)
{
- n = find<Namespace>(n.value().namespaces, [&](Namespace n) { return n.name == namespacePrefixes[i]; });
-
- if (!n.has_value())
- return nullopt;
-
- namespaces.push_back(n.value());
+ auto s = findPtr<Struct>(it->structs, [&](Struct s) { return s.name == name; });
+ if (s.has_value() && checkNamespace(it, namespacePrefix))
+ return s;
}
+ return nullopt;
+}
- for (int i = namespaces.size()-1; i >= 0; i--)
- {
- auto f = findPtr<Struct>(namespaces[i].structs, [&](Struct f) { return f.name == name; });
- if (f.has_value())
- return f.value();
- }
- return findPtr<Struct>(n.value().structs, [&](Struct s) { return s.name == name; });
-}
opt<Variable> findVariable(
- const Program & p,
const std::string & name,
+ const std::vector<std::string> & namespacePrefix,
std::shared_ptr<Context> ctx)
{
- auto it = ctx;
- while (it != nullptr)
+ for (auto it = ctx; it != nullptr; it = it->parent)
{
auto v = find<Variable>(it->variables, [&](Variable v) { return v.name == name; });
- if (v.has_value())
+ if (v.has_value() && checkNamespace(it, namespacePrefix))
return v;
- it = it->parent;
}
return nullopt;
}
+
+
opt<StructMember<Function>> findStructMethod(
- const Program & p,
const std::string & name,
- TypeInfo ti)
+ const Struct & s)
{
- if (!ti.isStruct)
- return nullopt;
- auto s = findStruct(p, ti.type.name, ti.type.namespacePrefixes);
- if (!s.has_value())
- return nullopt;
- return find<StructMember<Function>>(s.value().methods, [&](Function f) { return f.name == name; });
+ return find<StructMember<Function>>(s.methods, [&](Function f) { return f.name == name; });
}
opt<StructMember<Function> *> findStructMethodPtr(
- const Program & p,
const std::string & name,
- TypeInfo ti)
+ const Struct & s)
{
- if (!ti.isStruct)
- return nullopt;
- auto s = findStruct(p, ti.type.name, ti.type.namespacePrefixes);
- if (!s.has_value())
- return nullopt;
- return findPtr<StructMember<Function>>(s.value().methods, [&](Function f) { return f.name == name; });
+ return findPtr<StructMember<Function>>(s.methods, [&](Function f) { return f.name == name; });
}
opt<StructMember<Variable>> findStructMember(
- const Program & p,
- TypeInfo ti,
- const std::string & name)
+ const std::string & name,
+ const Struct & s)
+{
+ return find<StructMember<Variable>>(s.members, [&](Variable v) { return v.name == name; });
+}
+
+opt<StructMember<Variable> *> findStructMemberPtr(
+ const std::string & name,
+ const Struct & s)
{
- auto s = findStruct(p, ti.type.name, ti.type.namespacePrefixes);
- if (!s.has_value())
- return nullopt;
- return find<StructMember<Variable>>(s.value().members, [&](Variable v) { return v.name == name; });
+ return findPtr<StructMember<Variable>>(s.members, [&](Variable v) { return v.name == name; });
}
\ No newline at end of file |
