Skip to content

Commit

Permalink
Modernize iterate.
Browse files Browse the repository at this point in the history
  • Loading branch information
egorpugin committed Apr 19, 2019
1 parent b6aac13 commit 71512b3
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 146 deletions.
2 changes: 1 addition & 1 deletion src/sw/builder/file_storage.cpp
Expand Up @@ -46,7 +46,7 @@ FileStorage::FileStorage(const SwContext &swctx, const String &config)
{
//if (config.empty())
//throw SW_RUNTIME_ERROR("Empty config");
//load();
load();
}

FileStorage::file_holder *FileStorage::getFileLog()
Expand Down
58 changes: 28 additions & 30 deletions src/sw/driver/compiler.cpp
Expand Up @@ -1139,7 +1139,7 @@ void VisualStudioCompiler::prepareCommand1(const TargetBase &t)
//cmd->out.capture = true;

getCommandLineOptions<VisualStudioCompilerOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);

if (PreprocessToFile)
{
Expand Down Expand Up @@ -1189,7 +1189,7 @@ void VisualStudioASMCompiler::prepareCommand1(const TargetBase &t)
//cmd->base = clone();

// defs and idirs for asm must go before file
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);
getCommandLineOptions<VisualStudioAssemblerOptions>(cmd.get(), *this);
}

Expand Down Expand Up @@ -1236,7 +1236,7 @@ void ClangCompiler::prepareCommand1(const TargetBase &t)
CPPStandard.skip = true;

getCommandLineOptions<ClangOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*this->cmd); });
addEverything(*this->cmd);
getCommandLineOptions<ClangOptions>(cmd.get(), *this, "", true);
}

Expand Down Expand Up @@ -1297,7 +1297,7 @@ void ClangClCompiler::prepareCommand1(const TargetBase &t)

getCommandLineOptions<VisualStudioCompilerOptions>(cmd.get(), *this);
getCommandLineOptions<ClangClOptions>(cmd.get(), *this/*, "-Xclang"*/);
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);
}

void ClangClCompiler::setOutputFile(const path &output_file)
Expand Down Expand Up @@ -1341,7 +1341,7 @@ void GNUASMCompiler::prepareCommand1(const TargetBase &t)
getCommandLineOptions<GNUAssemblerOptions>(cmd.get(), *this);

if (!InputFile && !assembly)
iterate([this](auto & v, auto & gs) { v.addEverything(*cmd); });
addEverything(*cmd);
}

SW_DEFINE_PROGRAM_CLONE(GNUASMCompiler)
Expand Down Expand Up @@ -1394,7 +1394,7 @@ void GNUCompiler::prepareCommand1(const TargetBase &t)
CPPStandard.skip = true;

getCommandLineOptions<GNUOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*this->cmd); });
addEverything(*this->cmd);
getCommandLineOptions<GNUOptions>(cmd.get(), *this, "", true);
}

Expand All @@ -1419,36 +1419,34 @@ void GNUCompiler::setSourceFile(const path &input_file, path &output_file)
FilesOrdered NativeLinker::gatherLinkDirectories() const
{
FilesOrdered dirs;
iterate([&dirs](auto &v, auto &gs)

auto get_ldir = [&dirs](const auto &a)
{
auto get_ldir = [&dirs](const auto &a)
{
for (auto &d : a)
dirs.push_back(d);
};
for (auto &d : a)
dirs.push_back(d);
};

get_ldir(NativeLinkerOptions::gatherLinkDirectories());
get_ldir(NativeLinkerOptions::System.gatherLinkDirectories());

get_ldir(v.gatherLinkDirectories());
get_ldir(v.System.gatherLinkDirectories());
});
return dirs;
}

FilesOrdered NativeLinker::gatherLinkLibraries(bool system) const
{
FilesOrdered dirs;
iterate([&dirs, &system](auto &v, auto &gs)

auto get_ldir = [&dirs](const auto &a)
{
auto get_ldir = [&dirs](const auto &a)
{
for (auto &d : a)
dirs.push_back(d);
};
for (auto &d : a)
dirs.push_back(d);
};

if (system)
get_ldir(NativeLinkerOptions::System.gatherLinkLibraries());
else
get_ldir(NativeLinkerOptions::gatherLinkLibraries());

if (system)
get_ldir(v.System.gatherLinkLibraries());
else
get_ldir(v.gatherLinkLibraries());
});
return dirs;
}

Expand Down Expand Up @@ -1511,7 +1509,7 @@ void VisualStudioLibraryTool::prepareCommand1(const TargetBase &t)
((VisualStudioLibraryTool*)this)->VisualStudioLibraryToolOptions::LinkDirectories() = gatherLinkDirectories();

getCommandLineOptions<VisualStudioLibraryToolOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);
getAdditionalOptions(cmd.get());
}

Expand Down Expand Up @@ -1559,7 +1557,7 @@ void VisualStudioLinker::prepareCommand1(const TargetBase &t)
((VisualStudioLibraryTool*)this)->VisualStudioLibraryToolOptions::LinkDirectories() = gatherLinkDirectories();

getCommandLineOptions<VisualStudioLibraryToolOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);
getAdditionalOptions(cmd.get());
}

Expand Down Expand Up @@ -1722,7 +1720,7 @@ void GNULinker::prepareCommand1(const TargetBase &t)
//((GNULibraryTool*)this)->GNULibraryToolOptions::LinkDirectories() = gatherLinkDirectories();

getCommandLineOptions<GNULinkerOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);
//getAdditionalOptions(cmd.get());
}

Expand Down Expand Up @@ -1796,7 +1794,7 @@ void GNULibrarian::prepareCommand1(const TargetBase &t)
//((GNULibraryTool*)this)->GNULibraryToolOptions::LinkDirectories() = gatherLinkDirectories();

getCommandLineOptions<GNULibrarianOptions>(cmd.get(), *this);
iterate([this](auto &v, auto &gs) { v.addEverything(*cmd); });
addEverything(*cmd);
//getAdditionalOptions(cmd.get());
}

Expand Down
4 changes: 3 additions & 1 deletion src/sw/driver/compiler.h
Expand Up @@ -117,8 +117,10 @@ struct SW_DRIVER_CPP_API NativeToolchain
};

struct SDK SDK;
// also OS SDK (win sdk, macos sdk, linux headers etc.)
//std::optional<UnresolvedPackage> os sdk
std::optional<UnresolvedPackage> libc;
//std::optional<UnresolvedPackage> libcpp; // ?
//std::optional<UnresolvedPackage> libcpp;

std::shared_ptr<NativeLinker> Librarian;
std::shared_ptr<NativeLinker> Linker;
Expand Down
48 changes: 11 additions & 37 deletions src/sw/driver/options.h
Expand Up @@ -249,8 +249,7 @@ struct SW_DRIVER_CPP_API NativeLinkerOptionsData
void remove(const LinkLibrary &l);
};

struct SW_DRIVER_CPP_API NativeCompilerOptions : IterableOptions<NativeCompilerOptions>,
NativeCompilerOptionsData
struct SW_DRIVER_CPP_API NativeCompilerOptions : NativeCompilerOptionsData
{
NativeCompilerOptionsData System;

Expand All @@ -265,8 +264,7 @@ struct SW_DRIVER_CPP_API NativeCompilerOptions : IterableOptions<NativeCompilerO
PathOptionsType gatherIncludeDirectories() const;
};

struct SW_DRIVER_CPP_API NativeLinkerOptions : IterableOptions<NativeLinkerOptions>,
NativeLinkerOptionsData
struct SW_DRIVER_CPP_API NativeLinkerOptions : NativeLinkerOptionsData
{
// 1. remove dups somewhere: implement unique_vector
// 2. move Dependencies out - ???
Expand Down Expand Up @@ -308,16 +306,13 @@ struct SW_DRIVER_CPP_API NativeLinkerOptions : IterableOptions<NativeLinkerOptio
using UnresolvedDependenciesType = std::unordered_map<UnresolvedPackage, DependencyPtr>;

struct SW_DRIVER_CPP_API NativeOptions : NativeCompilerOptions,
NativeLinkerOptions,
IterableOptions<NativeOptions>
NativeLinkerOptions
{
using NativeCompilerOptions::add;
using NativeCompilerOptions::remove;
using NativeLinkerOptions::add;
using NativeLinkerOptions::remove;

using IterableOptions<NativeOptions>::iterate;

void merge(const NativeOptions &o, const GroupSettings &s = GroupSettings());
};

Expand Down Expand Up @@ -470,36 +465,15 @@ struct InheritanceGroup : T
//Public.merge(g.Interface);
}

template <class F, class ... Args>
void iterate(F &&f, const GroupSettings &s = GroupSettings())
{
auto s2 = s;

s2.Inheritance = InheritanceType::Private;
//T::template iterate<F, Args...>(std::forward<F>(f), s);
Private.template iterate<F, Args...>(std::forward<F>(f), s);
s2.Inheritance = InheritanceType::Protected;
Protected.template iterate<F, Args...>(std::forward<F>(f), s2);
s2.Inheritance = InheritanceType::Public;
Public.template iterate<F, Args...>(std::forward<F>(f), s2);
s2.Inheritance = InheritanceType::Interface;
Interface.template iterate<F, Args...>(std::forward<F>(f), s2);
}

template <class F, class ... Args>
void iterate(F &&f, const GroupSettings &s = GroupSettings()) const
template <class F>
void iterate(F &&f) const
{
auto s2 = s;

s2.Inheritance = InheritanceType::Private;
//T::template iterate<F, Args...>(std::forward<F>(f), s);
Private.template iterate<F, Args...>(std::forward<F>(f), s);
s2.Inheritance = InheritanceType::Protected;
Protected.template iterate<F, Args...>(std::forward<F>(f), s2);
s2.Inheritance = InheritanceType::Public;
Public.template iterate<F, Args...>(std::forward<F>(f), s2);
s2.Inheritance = InheritanceType::Interface;
Interface.template iterate<F, Args...>(std::forward<F>(f), s2);
for (int i = toIndex(InheritanceType::Min); i < toIndex(InheritanceType::Max); i++)
{
auto s = getInheritanceStorage().raw()[i];
if (s)
f(*s, (InheritanceType)i);
}
}

// merge to T, always w/o interface
Expand Down
3 changes: 1 addition & 2 deletions src/sw/driver/source_file.h
Expand Up @@ -41,8 +41,7 @@ using SourceFileMap = std::unordered_map<path, std::shared_ptr<T>>;
*
*/
//template <class T>
struct SW_DRIVER_CPP_API SourceFileStorage : protected SourceFileMap<SourceFile>,
IterableOptions<SourceFileStorage>
struct SW_DRIVER_CPP_API SourceFileStorage : protected SourceFileMap<SourceFile>
{
public:
using SourceFileMapThis = SourceFileMap<SourceFile>;
Expand Down
5 changes: 0 additions & 5 deletions src/sw/driver/sw.h
Expand Up @@ -109,11 +109,6 @@ using sw::NativeLanguage;
using sw::NativeSourceFile;
using sw::NativeLinkerOptions;

// TODO: remove
using sw::WithSourceFileStorage;
using sw::WithoutSourceFileStorage;
using sw::WithNativeOptions;
using sw::WithoutNativeOptions;
//

using sw::CommandLineOption;
Expand Down
27 changes: 0 additions & 27 deletions src/sw/driver/target/base.h
Expand Up @@ -469,11 +469,6 @@ struct SW_DRIVER_CPP_API ProjectTarget : ProjDirBase
TargetType getType() const override { return TargetType::Project; }
};

struct WithSourceFileStorage {};
struct WithoutSourceFileStorage {};
struct WithNativeOptions {};
struct WithoutNativeOptions {};

//template <class ... Args>
//struct SW_DRIVER_CPP_API TargetOptions : Args...
struct SW_DRIVER_CPP_API TargetOptions : SourceFileStorage, NativeOptions
Expand All @@ -489,22 +484,6 @@ struct SW_DRIVER_CPP_API TargetOptions : SourceFileStorage, NativeOptions
void add(const IncludeDirectory &i);
void remove(const IncludeDirectory &i);

template <class F>
void iterate(F &&f, const GroupSettings &s = GroupSettings())
{
SourceFileStorage::iterate(std::forward<F>(f), s);
NativeOptions::iterate(std::forward<F>(f), s);
}

template <class F, class SFS, class NO, class ... Args>
void iterate(F &&f, const GroupSettings &s = GroupSettings())
{
if constexpr (std::is_same_v<SFS, WithSourceFileStorage>)
SourceFileStorage::iterate(std::forward<F>(f), s);
if constexpr (std::is_same_v<NO, WithNativeOptions>)
NativeOptions::iterate(std::forward<F>(f), s);
}

void merge(const TargetOptions &g, const GroupSettings &s = GroupSettings())
{
SourceFileStorage::merge(g, s);
Expand Down Expand Up @@ -560,12 +539,6 @@ struct SW_DRIVER_CPP_API TargetOptionsGroup :
InheritanceGroup<TargetOptions>::inheritance(g, s);
}

template <class SFS, class NO, class F>
void iterate(F &&f, const GroupSettings &s = GroupSettings())
{
InheritanceGroup<TargetOptions>::iterate<F, SFS, NO>(std::forward<F>(f), s);
}

// self merge
void merge(const GroupSettings &s = GroupSettings())
{
Expand Down
25 changes: 12 additions & 13 deletions src/sw/driver/target/native.cpp
Expand Up @@ -82,11 +82,10 @@ bool NativeExecutedTarget::init()
Target::init();

// propagate this pointer to all
TargetOptionsGroup::iterate<WithSourceFileStorage, WithoutNativeOptions>([this](auto &v, auto &gs)
TargetOptionsGroup::iterate([this](auto &v, auto i)
{
v.target = this;
});
//LanguageStorage::target = this;

Librarian = std::dynamic_pointer_cast<NativeLinker>(getSolution()->Settings.Native.Librarian->clone());
Linker = std::dynamic_pointer_cast<NativeLinker>(getSolution()->Settings.Native.Linker->clone());
Expand Down Expand Up @@ -342,8 +341,8 @@ std::unordered_set<NativeSourceFile*> NativeExecutedTarget::gatherSourceFiles()
Files NativeExecutedTarget::gatherIncludeDirectories() const
{
Files idirs;
((NativeExecutedTarget*)this)->TargetOptionsGroup::iterate<WithoutSourceFileStorage, WithNativeOptions>(
[this, &idirs](auto &v, auto &s)
TargetOptionsGroup::iterate(
[this, &idirs](auto &v, auto i)
{
auto idirs2 = v.gatherIncludeDirectories();
for (auto &i2 : idirs2)
Expand Down Expand Up @@ -1418,8 +1417,8 @@ bool NativeExecutedTarget::prepare()
std::vector<DependencyPtr> deps_ordered;

// set our initial deps
TargetOptionsGroup::iterate<WithoutSourceFileStorage, WithNativeOptions>(
[this, &deps, &deps_ordered](auto &v, auto &s)
TargetOptionsGroup::iterate(
[this, &deps, &deps_ordered](auto &v, auto i)
{
//DEBUG_BREAK_IF_STRING_HAS(getPackage().ppath.toString(), "sw.server.protos");

Expand All @@ -1430,7 +1429,7 @@ bool NativeExecutedTarget::prepare()
if (d->isDummy())
continue;

deps.emplace(d, s.Inheritance);
deps.emplace(d, i);
deps_ordered.push_back(d);
}
});
Expand All @@ -1450,11 +1449,11 @@ bool NativeExecutedTarget::prepare()
}

// iterate over child deps
(*(NativeExecutedTarget*)d->target).TargetOptionsGroup::iterate<WithoutSourceFileStorage, WithNativeOptions>(
[this, &new_dependency, &deps, d = d.get(), &deps_ordered](auto &v, auto &s)
(*(NativeExecutedTarget*)d->target).TargetOptionsGroup::iterate(
[this, &new_dependency, &deps, d = d.get(), &deps_ordered](auto &v, auto Inheritance)
{
// nothing to do with private inheritance
if (s.Inheritance == InheritanceType::Private)
if (Inheritance == InheritanceType::Private)
return;

for (auto &d2 : v.Dependencies)
Expand All @@ -1464,13 +1463,13 @@ bool NativeExecutedTarget::prepare()
if (d2->isDummy())
continue;

if (s.Inheritance == InheritanceType::Protected && !hasSameParent(d2->target))
if (Inheritance == InheritanceType::Protected && !hasSameParent(d2->target))
continue;

auto copy = std::make_shared<Dependency>(*d2);
auto[i, inserted] = deps.emplace(copy,
s.Inheritance == InheritanceType::Interface ?
InheritanceType::Public : s.Inheritance
Inheritance == InheritanceType::Interface ?
InheritanceType::Public : Inheritance
);
if (inserted)
deps_ordered.push_back(copy);
Expand Down

0 comments on commit 71512b3

Please sign in to comment.