#ifndef LLVM_OPTION_ARGLIST_H
#define LLVM_OPTION_ARGLIST_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/OptSpecifier.h"
#include "llvm/Option/Option.h"
#include <algorithm>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <list>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace llvm {
class raw_ostream;
namespace opt {
template<typename BaseIter, unsigned NumOptSpecifiers = 0>
class arg_iterator {
BaseIter Current, End;
OptSpecifier Ids[NumOptSpecifiers ? NumOptSpecifiers : 1];
void SkipToNextArg() {
for (; Current != End; ++Current) {
if (!*Current)
continue;
if (!NumOptSpecifiers)
return;
const Option &O = (*Current)->getOption();
for (auto Id : Ids) {
if (!Id.isValid())
break;
if (O.matches(Id))
return;
}
}
}
using Traits = std::iterator_traits<BaseIter>;
public:
using value_type = typename Traits::value_type;
using reference = typename Traits::reference;
using pointer = typename Traits::pointer;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
arg_iterator(
BaseIter Current, BaseIter End,
const OptSpecifier (&Ids)[NumOptSpecifiers ? NumOptSpecifiers : 1] = {})
: Current(Current), End(End) {
for (unsigned I = 0; I != NumOptSpecifiers; ++I)
this->Ids[I] = Ids[I];
SkipToNextArg();
}
reference operator*() const { return *Current; }
pointer operator->() const { return Current; }
arg_iterator &operator++() {
++Current;
SkipToNextArg();
return *this;
}
arg_iterator operator++(int) {
arg_iterator tmp(*this);
++(*this);
return tmp;
}
friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
return LHS.Current == RHS.Current;
}
friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
return !(LHS == RHS);
}
};
class ArgList {
public:
using arglist_type = SmallVector<Arg *, 16>;
using iterator = arg_iterator<arglist_type::iterator>;
using const_iterator = arg_iterator<arglist_type::const_iterator>;
using reverse_iterator = arg_iterator<arglist_type::reverse_iterator>;
using const_reverse_iterator =
arg_iterator<arglist_type::const_reverse_iterator>;
template<unsigned N> using filtered_iterator =
arg_iterator<arglist_type::const_iterator, N>;
template<unsigned N> using filtered_reverse_iterator =
arg_iterator<arglist_type::const_reverse_iterator, N>;
private:
arglist_type Args;
using OptRange = std::pair<unsigned, unsigned>;
static OptRange emptyRange() { return {-1u, 0u}; }
DenseMap<unsigned, OptRange> OptRanges;
OptRange getRange(std::initializer_list<OptSpecifier> Ids) const;
protected:
ArgList() = default;
ArgList(ArgList &&RHS)
: Args(std::move(RHS.Args)), OptRanges(std::move(RHS.OptRanges)) {
RHS.Args.clear();
RHS.OptRanges.clear();
}
ArgList &operator=(ArgList &&RHS) {
Args = std::move(RHS.Args);
RHS.Args.clear();
OptRanges = std::move(RHS.OptRanges);
RHS.OptRanges.clear();
return *this;
}
~ArgList() = default;
static OptSpecifier toOptSpecifier(OptSpecifier S) { return S; }
public:
void append(Arg *A);
const arglist_type &getArgs() const { return Args; }
unsigned size() const { return Args.size(); }
iterator begin() { return {Args.begin(), Args.end()}; }
iterator end() { return {Args.end(), Args.end()}; }
reverse_iterator rbegin() { return {Args.rbegin(), Args.rend()}; }
reverse_iterator rend() { return {Args.rend(), Args.rend()}; }
const_iterator begin() const { return {Args.begin(), Args.end()}; }
const_iterator end() const { return {Args.end(), Args.end()}; }
const_reverse_iterator rbegin() const { return {Args.rbegin(), Args.rend()}; }
const_reverse_iterator rend() const { return {Args.rend(), Args.rend()}; }
template<typename ...OptSpecifiers>
iterator_range<filtered_iterator<sizeof...(OptSpecifiers)>>
filtered(OptSpecifiers ...Ids) const {
OptRange Range = getRange({toOptSpecifier(Ids)...});
auto B = Args.begin() + Range.first;
auto E = Args.begin() + Range.second;
using Iterator = filtered_iterator<sizeof...(OptSpecifiers)>;
return make_range(Iterator(B, E, {toOptSpecifier(Ids)...}),
Iterator(E, E, {toOptSpecifier(Ids)...}));
}
template<typename ...OptSpecifiers>
iterator_range<filtered_reverse_iterator<sizeof...(OptSpecifiers)>>
filtered_reverse(OptSpecifiers ...Ids) const {
OptRange Range = getRange({toOptSpecifier(Ids)...});
auto B = Args.rend() - Range.second;
auto E = Args.rend() - Range.first;
using Iterator = filtered_reverse_iterator<sizeof...(OptSpecifiers)>;
return make_range(Iterator(B, E, {toOptSpecifier(Ids)...}),
Iterator(E, E, {toOptSpecifier(Ids)...}));
}
void eraseArg(OptSpecifier Id);
template<typename ...OptSpecifiers>
bool hasArgNoClaim(OptSpecifiers ...Ids) const {
return getLastArgNoClaim(Ids...) != nullptr;
}
template<typename ...OptSpecifiers>
bool hasArg(OptSpecifiers ...Ids) const {
return getLastArg(Ids...) != nullptr;
}
bool hasMultipleArgs(OptSpecifier Id) const {
auto Args = filtered(Id);
return (Args.begin() != Args.end()) && (++Args.begin()) != Args.end();
}
template<typename ...OptSpecifiers>
Arg *getLastArg(OptSpecifiers ...Ids) const {
Arg *Res = nullptr;
for (Arg *A : filtered(Ids...)) {
Res = A;
Res->claim();
}
return Res;
}
template<typename ...OptSpecifiers>
Arg *getLastArgNoClaim(OptSpecifiers ...Ids) const {
for (Arg *A : filtered_reverse(Ids...))
return A;
return nullptr;
}
virtual const char *getArgString(unsigned Index) const = 0;
virtual unsigned getNumInputArgStrings() const = 0;
StringRef getLastArgValue(OptSpecifier Id, StringRef Default = "") const;
std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const;
bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
bool Default) const;
void addOptInFlag(ArgStringList &Output, OptSpecifier Pos,
OptSpecifier Neg) const;
void addOptOutFlag(ArgStringList &Output, OptSpecifier Pos,
OptSpecifier Neg) const {
addOptInFlag(Output, Neg, Pos);
}
template<typename ...OptSpecifiers>
void AddLastArg(ArgStringList &Output, OptSpecifiers ...Ids) const {
if (Arg *A = getLastArg(Ids...)) A->render(*this, Output);
}
void AddAllArgsExcept(ArgStringList &Output, ArrayRef<OptSpecifier> Ids,
ArrayRef<OptSpecifier> ExcludeIds) const;
void AddAllArgs(ArgStringList &Output, ArrayRef<OptSpecifier> Ids) const;
void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
const char *Translation,
bool Joined = false) const;
void ClaimAllArgs(OptSpecifier Id0) const;
void ClaimAllArgs() const;
virtual const char *MakeArgStringRef(StringRef Str) const = 0;
const char *MakeArgString(const Twine &Str) const {
SmallString<256> Buf;
return MakeArgStringRef(Str.toStringRef(Buf));
}
const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
StringRef RHS) const;
void print(raw_ostream &O) const;
void dump() const;
};
class InputArgList final : public ArgList {
private:
mutable ArgStringList ArgStrings;
mutable std::list<std::string> SynthesizedStrings;
unsigned NumInputArgStrings;
void releaseMemory();
public:
InputArgList() : NumInputArgStrings(0) {}
InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
InputArgList(InputArgList &&RHS)
: ArgList(std::move(RHS)), ArgStrings(std::move(RHS.ArgStrings)),
SynthesizedStrings(std::move(RHS.SynthesizedStrings)),
NumInputArgStrings(RHS.NumInputArgStrings) {}
InputArgList &operator=(InputArgList &&RHS) {
releaseMemory();
ArgList::operator=(std::move(RHS));
ArgStrings = std::move(RHS.ArgStrings);
SynthesizedStrings = std::move(RHS.SynthesizedStrings);
NumInputArgStrings = RHS.NumInputArgStrings;
return *this;
}
~InputArgList() { releaseMemory(); }
const char *getArgString(unsigned Index) const override {
return ArgStrings[Index];
}
void replaceArgString(unsigned Index, const Twine &S) {
ArgStrings[Index] = MakeArgString(S);
}
unsigned getNumInputArgStrings() const override {
return NumInputArgStrings;
}
public:
unsigned MakeIndex(StringRef String0) const;
unsigned MakeIndex(StringRef String0, StringRef String1) const;
using ArgList::MakeArgString;
const char *MakeArgStringRef(StringRef Str) const override;
};
class DerivedArgList final : public ArgList {
const InputArgList &BaseArgs;
mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
public:
DerivedArgList(const InputArgList &BaseArgs);
const char *getArgString(unsigned Index) const override {
return BaseArgs.getArgString(Index);
}
unsigned getNumInputArgStrings() const override {
return BaseArgs.getNumInputArgStrings();
}
const InputArgList &getBaseArgs() const {
return BaseArgs;
}
void AddSynthesizedArg(Arg *A);
using ArgList::MakeArgString;
const char *MakeArgStringRef(StringRef Str) const override;
void AddFlagArg(const Arg *BaseArg, const Option Opt) {
append(MakeFlagArg(BaseArg, Opt));
}
void AddPositionalArg(const Arg *BaseArg, const Option Opt,
StringRef Value) {
append(MakePositionalArg(BaseArg, Opt, Value));
}
void AddSeparateArg(const Arg *BaseArg, const Option Opt,
StringRef Value) {
append(MakeSeparateArg(BaseArg, Opt, Value));
}
void AddJoinedArg(const Arg *BaseArg, const Option Opt,
StringRef Value) {
append(MakeJoinedArg(BaseArg, Opt, Value));
}
Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const;
Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const;
Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const;
};
}
}
#endif