Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add underscores to private data members #898

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions common/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ExitingStream {
// Indicates that the program is exiting due to a bug in the program, rather
// than, e.g., invalid input.
ExitingStream& TreatAsBug() {
treat_as_bug = true;
treat_as_bug_ = true;
return *this;
}

Expand All @@ -45,16 +45,16 @@ class ExitingStream {
// Forward output to llvm::errs.
template <typename T>
ExitingStream& operator<<(const T& message) {
if (separator) {
if (separator_) {
llvm::errs() << ": ";
separator = false;
separator_ = false;
}
llvm::errs() << message;
return *this;
}

ExitingStream& operator<<(AddSeparator /*unused*/) {
separator = true;
separator_ = true;
return *this;
}

Expand All @@ -64,7 +64,7 @@ class ExitingStream {
[[noreturn]] friend auto operator|(Helper /*unused*/, ExitingStream& rhs) {
// Finish with a newline.
llvm::errs() << "\n";
if (rhs.treat_as_bug) {
if (rhs.treat_as_bug_) {
std::abort();
} else {
std::exit(-1);
Expand All @@ -73,10 +73,10 @@ class ExitingStream {

private:
// Whether a separator should be printed if << is used again.
bool separator = false;
bool separator_ = false;

// Whether the program is exiting due to a bug.
bool treat_as_bug = false;
bool treat_as_bug_ = false;
};

} // namespace Internal
Expand Down
28 changes: 14 additions & 14 deletions common/indirect_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,43 @@ class IndirectValue {
// std::is_constructible give correct answers.

// Initializes the underlying T object as if by `T()`.
IndirectValue() : value(std::make_unique<T>()) {}
IndirectValue() : value_(std::make_unique<T>()) {}

// Initializes the underlying T object as if by `T(std::move(value))`.
IndirectValue(T value) : value(std::make_unique<T>(std::move(value))) {}
IndirectValue(T value) : value_(std::make_unique<T>(std::move(value))) {}

// TODO(geoffromer): consider defining implicit conversions from
// U and IndirectValue<U>, when U is implicitly convertible to T.

IndirectValue(const IndirectValue& other)
: value(std::make_unique<T>(*other)) {}
: value_(std::make_unique<T>(*other)) {}

IndirectValue(IndirectValue&& other)
: value(std::make_unique<T>(std::move(*other))) {}
: value_(std::make_unique<T>(std::move(*other))) {}

auto operator=(const IndirectValue& other) -> IndirectValue& {
*value = *other.value;
*value_ = *other.value_;
return *this;
}

auto operator=(IndirectValue&& other) -> IndirectValue& {
*value = std::move(*other.value);
*value_ = std::move(*other.value_);
return *this;
}

auto operator*() -> T& { return *value; }
auto operator*() const -> const T& { return *value; }
auto operator*() -> T& { return *value_; }
auto operator*() const -> const T& { return *value_; }

auto operator->() -> T* { return value.get(); }
auto operator->() const -> const T* { return value.get(); }
auto operator->() -> T* { return value_.get(); }
auto operator->() const -> const T* { return value_.get(); }

// Returns the address of the stored value.
//
// TODO(geoffromer): Consider eliminating this method, which is not
// present in comparable types like indirect_value<T> or optional<T>,
// once our APIs are less pointer-centric.
auto GetPointer() -> T* { return value.get(); }
auto GetPointer() const -> const T* { return value.get(); }
auto GetPointer() -> T* { return value_.get(); }
auto GetPointer() const -> const T* { return value_.get(); }

private:
static_assert(std::is_object_v<T>, "T must be an object type");
Expand All @@ -91,9 +91,9 @@ class IndirectValue {
-> IndirectValue<std::decay_t<decltype(callable())>>;

template <typename... Args>
IndirectValue(std::unique_ptr<T> value) : value(std::move(value)) {}
IndirectValue(std::unique_ptr<T> value) : value_(std::move(value)) {}

const std::unique_ptr<T> value;
const std::unique_ptr<T> value_;
};

template <typename Callable>
Expand Down
12 changes: 6 additions & 6 deletions executable_semantics/ast/source_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ class SourceLocation {
public:
// The filename should be eternal or arena-allocated to eliminate copies.
SourceLocation(const char* filename, int line_num)
: filename(filename), line_num(line_num) {}
: filename_(filename), line_num_(line_num) {}
SourceLocation(Nonnull<const std::string*> filename, int line_num)
: filename(filename->c_str()), line_num(line_num) {}
: filename_(filename->c_str()), line_num_(line_num) {}

SourceLocation(const SourceLocation&) = default;
SourceLocation(SourceLocation&&) = default;
auto operator=(const SourceLocation&) -> SourceLocation& = default;
auto operator=(SourceLocation&&) -> SourceLocation& = default;

bool operator==(SourceLocation other) const {
return filename == other.filename && line_num == other.line_num;
return filename_ == other.filename_ && line_num_ == other.line_num_;
}

void Print(llvm::raw_ostream& out) const {
out << filename << ":" << line_num;
out << filename_ << ":" << line_num_;
}
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }

private:
std::string_view filename;
int line_num;
std::string_view filename_;
int line_num_;
};

} // namespace Carbon
Expand Down
10 changes: 5 additions & 5 deletions executable_semantics/common/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Arena {
auto smart_ptr =
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
Nonnull<T*> ptr = smart_ptr->Instance();
arena.push_back(std::move(smart_ptr));
arena_.push_back(std::move(smart_ptr));
return ptr;
}

Expand All @@ -38,16 +38,16 @@ class Arena {
public:
template <typename... Args>
explicit ArenaEntryTyped(Args&&... args)
: instance(std::forward<Args>(args)...) {}
: instance_(std::forward<Args>(args)...) {}

auto Instance() -> Nonnull<T*> { return Nonnull<T*>(&instance); }
auto Instance() -> Nonnull<T*> { return Nonnull<T*>(&instance_); }

private:
T instance;
T instance_;
};

// Manages allocations in an arena for destruction at shutdown.
std::vector<std::unique_ptr<ArenaEntry>> arena;
std::vector<std::unique_ptr<ArenaEntry>> arena_;
};

} // namespace Carbon
Expand Down
12 changes: 6 additions & 6 deletions executable_semantics/interpreter/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Address {

// Returns true if the two addresses refer to the same memory location.
friend auto operator==(const Address& lhs, const Address& rhs) -> bool {
return lhs.index == rhs.index;
return lhs.index_ == rhs.index_;
}

friend auto operator!=(const Address& lhs, const Address& rhs) -> bool {
Expand All @@ -40,7 +40,7 @@ class Address {
// the whole memory allocation, and an optional FieldPath specifying a
// particular field within that allocation.
void Print(llvm::raw_ostream& out) const {
out << "Address(" << index << ")" << field_path;
out << "Address(" << index_ << ")" << field_path_;
}

LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
Expand All @@ -49,7 +49,7 @@ class Address {
// `field_name`, this method returns the address of that field.
auto SubobjectAddress(std::string field_name) const -> Address {
Address result = *this;
result.field_path.Append(std::move(field_name));
result.field_path_.Append(std::move(field_name));
return result;
}

Expand All @@ -59,10 +59,10 @@ class Address {
// details of the Heap.
friend class Heap;

explicit Address(uint64_t index) : index(index) {}
explicit Address(uint64_t index) : index_(index) {}

uint64_t index;
FieldPath field_path;
uint64_t index_;
FieldPath field_path_;
};

} // namespace Carbon
Expand Down
28 changes: 14 additions & 14 deletions executable_semantics/interpreter/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ class Dictionary {
// NOLINTNEXTLINE(readability-identifier-naming)
using iterator_category = std::forward_iterator_tag;

explicit Iterator(std::optional<Nonnull<Node*>> x) : p(x) {}
Iterator(const Iterator& iter) : p(iter.p) {}
explicit Iterator(std::optional<Nonnull<Node*>> x) : p_(x) {}
Iterator(const Iterator& iter) : p_(iter.p_) {}
auto operator++() -> Iterator& {
p = (*p)->next;
p_ = (*p_)->next;
return *this;
}
auto operator++(int) -> Iterator {
Iterator tmp(*this);
operator++();
return tmp;
}
auto operator==(const Iterator& rhs) const -> bool { return p == rhs.p; }
auto operator!=(const Iterator& rhs) const -> bool { return p != rhs.p; }
auto operator*() -> const value_type& { return (*p)->curr; }
auto operator->() -> const value_type* { return &(*p)->curr; }
auto operator==(const Iterator& rhs) const -> bool { return p_ == rhs.p_; }
auto operator!=(const Iterator& rhs) const -> bool { return p_ != rhs.p_; }
auto operator*() -> const value_type& { return (*p_)->curr; }
auto operator->() -> const value_type* { return &(*p_)->curr; }

private:
std::optional<Nonnull<Node*>> p;
std::optional<Nonnull<Node*>> p_;
};

// Create an empty dictionary.
explicit Dictionary(Nonnull<Arena*> arena) : arena(arena) {}
explicit Dictionary(Nonnull<Arena*> arena) : arena_(arena) {}

// Return the value associated with the given key.
// Time complexity: O(n) where n is the number of times
Expand All @@ -82,21 +82,21 @@ class Dictionary {
// Associate the value v with key k in the dictionary.
// Time complexity: O(1).
auto Set(const K& k, const V& v) -> void {
head = arena->New<Node>(std::make_pair(k, v), head);
head_ = arena_->New<Node>(std::make_pair(k, v), head_);
}

auto IsEmpty() -> bool { return !head; }
auto IsEmpty() -> bool { return !head_; }

// The position of the first element of the dictionary
// or `end()` if the dictionary is empty.
auto begin() const -> Iterator { return Iterator(head); }
auto begin() const -> Iterator { return Iterator(head_); }

// The position one past that of the last element.
auto end() const -> Iterator { return Iterator(std::nullopt); }

private:
std::optional<Nonnull<Node*>> head;
Nonnull<Arena*> arena;
std::optional<Nonnull<Node*>> head_;
Nonnull<Arena*> arena_;
};

} // namespace Carbon
Expand Down
10 changes: 5 additions & 5 deletions executable_semantics/interpreter/field_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ class FieldPath {
FieldPath() = default;

// Constructs a FieldPath consisting of a single step.
explicit FieldPath(std::string name) : components({std::move(name)}) {}
explicit FieldPath(std::string name) : components_({std::move(name)}) {}

FieldPath(const FieldPath&) = default;
FieldPath(FieldPath&&) = default;
auto operator=(const FieldPath&) -> FieldPath& = default;
auto operator=(FieldPath&&) -> FieldPath& = default;

// Returns whether *this is empty.
auto IsEmpty() const -> bool { return components.empty(); }
auto IsEmpty() const -> bool { return components_.empty(); }

// Appends `name` to the end of *this.
auto Append(std::string name) -> void {
components.push_back(std::move(name));
components_.push_back(std::move(name));
}

void Print(llvm::raw_ostream& out) const {
for (const std::string& component : components) {
for (const std::string& component : components_) {
out << "." << component;
}
}
Expand All @@ -58,7 +58,7 @@ class FieldPath {
// another Value, so its implementation details are tied to the implementation
// details of Value.
friend class Value;
std::vector<std::string> components;
std::vector<std::string> components_;
};

} // namespace Carbon
Expand Down
28 changes: 14 additions & 14 deletions executable_semantics/interpreter/heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,55 @@ auto Heap::AllocateValue(Nonnull<const Value*> v) -> Address {
// ensures that we don't do anything else in between, which is really bad!
// Consider whether to include a copy of the input v in this function
// or to leave it up to the caller.
Address a(values.size());
values.push_back(v);
alive.push_back(true);
Address a(values_.size());
values_.push_back(v);
alive_.push_back(true);
return a;
}

auto Heap::Read(const Address& a, SourceLocation source_loc)
-> Nonnull<const Value*> {
this->CheckAlive(a, source_loc);
return values[a.index]->GetField(arena, a.field_path, source_loc);
return values_[a.index_]->GetField(arena_, a.field_path_, source_loc);
}

void Heap::Write(const Address& a, Nonnull<const Value*> v,
SourceLocation source_loc) {
this->CheckAlive(a, source_loc);
values[a.index] =
values[a.index]->SetField(arena, a.field_path, v, source_loc);
values_[a.index_] =
values_[a.index_]->SetField(arena_, a.field_path_, v, source_loc);
}

void Heap::CheckAlive(const Address& address, SourceLocation source_loc) {
if (!alive[address.index]) {
if (!alive_[address.index_]) {
FATAL_RUNTIME_ERROR(source_loc)
<< "undefined behavior: access to dead value "
<< *values[address.index];
<< *values_[address.index_];
}
}

void Heap::Deallocate(const Address& address) {
CHECK(address.field_path.IsEmpty());
if (alive[address.index]) {
alive[address.index] = false;
CHECK(address.field_path_.IsEmpty());
if (alive_[address.index_]) {
alive_[address.index_] = false;
} else {
FATAL_RUNTIME_ERROR_NO_LINE() << "deallocating an already dead value";
}
}

void Heap::Print(llvm::raw_ostream& out) const {
llvm::ListSeparator sep;
for (size_t i = 0; i < values.size(); ++i) {
for (size_t i = 0; i < values_.size(); ++i) {
out << sep;
PrintAddress(Address(i), out);
}
}

void Heap::PrintAddress(const Address& a, llvm::raw_ostream& out) const {
if (!alive[a.index]) {
if (!alive_[a.index_]) {
out << "!!";
}
out << *values[a.index];
out << *values_[a.index_];
}

} // namespace Carbon
Loading