#ifndef TOOLS_GN_LABEL_H_
#define TOOLS_GN_LABEL_H_
#include <string_view>
#include <tuple>
#include <stddef.h>
#include "gn/source_dir.h"
#include "gn/string_atom.h"
class Err;
class Value;
class Label {
public:
Label();
Label(const SourceDir& dir,
std::string_view name,
const SourceDir& toolchain_dir,
std::string_view toolchain_name);
Label(const SourceDir& dir, std::string_view name);
static Label Resolve(const SourceDir& current_dir,
std::string_view source_root,
const Label& current_toolchain,
const Value& input,
Err* err);
bool is_null() const { return dir_.is_null(); }
const SourceDir& dir() const { return dir_; }
const std::string& name() const { return name_.str(); }
StringAtom name_atom() const { return name_; }
const SourceDir& toolchain_dir() const { return toolchain_dir_; }
const std::string& toolchain_name() const { return toolchain_name_.str(); }
StringAtom toolchain_name_atom() const { return toolchain_name_; }
Label GetToolchainLabel() const;
Label GetWithNoToolchain() const;
std::string GetUserVisibleName(bool include_toolchain) const;
std::string GetUserVisibleName(const Label& default_toolchain) const;
bool operator==(const Label& other) const {
return hash_ == other.hash_ && name_.SameAs(other.name_) &&
dir_ == other.dir_ && toolchain_dir_ == other.toolchain_dir_ &&
toolchain_name_.SameAs(other.toolchain_name_);
}
bool operator!=(const Label& other) const { return !operator==(other); }
bool operator<(const Label& other) const {
if (dir_ != other.dir_)
return dir_ < other.dir_;
if (!name_.SameAs(other.name_))
return name_ < other.name_;
if (toolchain_dir_ != other.toolchain_dir_)
return toolchain_dir_ < other.toolchain_dir_;
return toolchain_name_ < other.toolchain_name_;
}
bool ToolchainsEqual(const Label& other) const {
return toolchain_dir_ == other.toolchain_dir_ &&
toolchain_name_.SameAs(other.toolchain_name_);
}
size_t hash() const { return hash_; }
private:
Label(SourceDir dir, StringAtom name)
: dir_(dir), name_(name), hash_(ComputeHash()) {}
Label(SourceDir dir,
StringAtom name,
SourceDir toolchain_dir,
StringAtom toolchain_name)
: dir_(dir),
name_(name),
toolchain_dir_(toolchain_dir),
toolchain_name_(toolchain_name),
hash_(ComputeHash()) {}
size_t ComputeHash() const {
size_t h0 = dir_.hash();
size_t h1 = name_.ptr_hash();
size_t h2 = toolchain_dir_.hash();
size_t h3 = toolchain_name_.ptr_hash();
return ((h3 * 131 + h2) * 131 + h1) * 131 + h0;
}
SourceDir dir_;
StringAtom name_;
SourceDir toolchain_dir_;
StringAtom toolchain_name_;
size_t hash_;
};
namespace std {
template <>
struct hash<Label> {
std::size_t operator()(const Label& v) const { return v.hash(); }
};
}
extern const char kLabels_Help[];
#endif