Switch more of toolchain to enumerate/seq (#3137)

These were already used in a few spots, just trying to switch more loops
for consistency and ease of reading.
This commit is contained in:
Jon Ross-Perkins
2023-08-23 00:47:03 +00:00
committed by GitHub
parent 5c2338dbd0
commit 7330e6698c
10 changed files with 39 additions and 44 deletions
+12 -10
View File
@@ -5,6 +5,8 @@
#include "toolchain/lowering/lowering_context.h"
#include "common/vlog.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "toolchain/lowering/lowering_function_context.h"
#include "toolchain/semantics/semantics_ir.h"
#include "toolchain/semantics/semantics_node.h"
@@ -31,20 +33,20 @@ auto LoweringContext::Run() -> std::unique_ptr<llvm::Module> {
// Lower types.
auto types = semantics_ir_->types();
types_.resize_for_overwrite(types.size());
for (int i = 0; i < static_cast<int>(types.size()); ++i) {
types_[i] = BuildType(types[i]);
for (auto [i, type] : llvm::enumerate(types)) {
types_[i] = BuildType(type);
}
// Lower function declarations.
functions_.resize_for_overwrite(semantics_ir_->functions_size());
for (int i = 0; i < semantics_ir_->functions_size(); ++i) {
for (auto i : llvm::seq(semantics_ir_->functions_size())) {
functions_[i] = BuildFunctionDeclaration(SemanticsFunctionId(i));
}
// TODO: Lower global variable declarations.
// Lower function definitions.
for (int i = 0; i < semantics_ir_->functions_size(); ++i) {
for (auto i : llvm::seq(semantics_ir_->functions_size())) {
BuildFunctionDefinition(SemanticsFunctionId(i));
}
@@ -61,8 +63,8 @@ auto LoweringContext::BuildFunctionDeclaration(SemanticsFunctionId function_id)
auto param_refs = semantics_ir().GetNodeBlock(function.param_refs_id);
llvm::SmallVector<llvm::Type*> args;
args.resize_for_overwrite(param_refs.size());
for (int i = 0; i < static_cast<int>(param_refs.size()); ++i) {
args[i] = GetType(semantics_ir().GetNode(param_refs[i]).type_id());
for (auto [i, param_ref] : llvm::enumerate(param_refs)) {
args[i] = GetType(semantics_ir().GetNode(param_ref).type_id());
}
// If return type is not valid, the function does not have a return type.
@@ -77,8 +79,8 @@ auto LoweringContext::BuildFunctionDeclaration(SemanticsFunctionId function_id)
semantics_ir().GetString(function.name_id), llvm_module());
// Set parameter names.
for (int i = 0; i < static_cast<int>(param_refs.size()); ++i) {
auto name_id = semantics_ir().GetNode(param_refs[i]).GetAsVarStorage();
for (auto [i, param_ref] : llvm::enumerate(param_refs)) {
auto name_id = semantics_ir().GetNode(param_ref).GetAsVarStorage();
llvm_function->getArg(i)->setName(semantics_ir().GetString(name_id));
}
@@ -99,8 +101,8 @@ auto LoweringContext::BuildFunctionDefinition(SemanticsFunctionId function_id)
// Add parameters to locals.
auto param_refs = semantics_ir().GetNodeBlock(function.param_refs_id);
for (int i = 0; i < static_cast<int>(param_refs.size()); ++i) {
function_lowering.SetLocal(param_refs[i], llvm_function->getArg(i));
for (auto [i, param_ref] : llvm::enumerate(param_refs)) {
function_lowering.SetLocal(param_ref, llvm_function->getArg(i));
}
// Lower all blocks.
+8 -11
View File
@@ -2,6 +2,8 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "toolchain/lowering/lowering_function_context.h"
#include "toolchain/semantics/semantics_node_kind.h"
@@ -63,7 +65,7 @@ auto LoweringHandleArrayValue(LoweringFunctionContext& context,
auto* tuple_type =
context.GetType(context.semantics_ir().GetNode(tuple_node_id).type_id());
for (int i = 0; i < static_cast<int>(llvm_type->getArrayNumElements()); ++i) {
for (auto i : llvm::seq(llvm_type->getArrayNumElements())) {
llvm::Value* array_element_value = context.GetIndexFromStructOrArray(
tuple_type, tuple_value, i, "array.element");
if (tuple_value->getType()->isPointerTy()) {
@@ -306,14 +308,9 @@ auto LoweringHandleTupleValue(LoweringFunctionContext& context,
context.builder().CreateAlloca(llvm_type, /*ArraySize=*/nullptr, "tuple");
context.SetLocal(node_id, alloca);
auto refs = context.semantics_ir().GetNodeBlock(node.GetAsTupleValue());
auto type_refs = context.semantics_ir().GetTypeBlock(
context.semantics_ir()
.GetNode(context.semantics_ir().GetType(node.type_id()))
.GetAsTupleType());
for (int i = 0; i < static_cast<int>(type_refs.size()); ++i) {
for (auto [i, ref] : llvm::enumerate(refs)) {
auto* gep = context.builder().CreateStructGEP(llvm_type, alloca, i);
context.builder().CreateStore(context.GetLocal(refs[i]), gep);
context.builder().CreateStore(context.GetLocal(ref), gep);
}
}
@@ -337,13 +334,13 @@ auto LoweringHandleStructValue(LoweringFunctionContext& context,
context.semantics_ir()
.GetNode(context.semantics_ir().GetType(node.type_id()))
.GetAsStructType());
for (int i = 0; i < static_cast<int>(refs.size()); ++i) {
for (auto [i, ref, type_ref] : llvm::enumerate(refs, type_refs)) {
auto [field_name_id, field_type_id] =
context.semantics_ir().GetNode(type_refs[i]).GetAsStructTypeField();
context.semantics_ir().GetNode(type_ref).GetAsStructTypeField();
auto member_name = context.semantics_ir().GetString(field_name_id);
auto* gep =
context.builder().CreateStructGEP(llvm_type, alloca, i, member_name);
context.builder().CreateStore(context.GetLocal(refs[i]), gep);
context.builder().CreateStore(context.GetLocal(ref), gep);
}
}
+1 -4
View File
@@ -4,9 +4,6 @@
#include "toolchain/parser/parse_tree.h"
#include <cstdlib>
#include <optional>
#include "common/check.h"
#include "common/error.h"
#include "llvm/ADT/Sequence.h"
@@ -244,7 +241,7 @@ auto ParseTree::Verify() const -> ErrorOr<Success> {
}
}
} else {
for (int i = 0; i < n_impl.kind.child_count(); ++i) {
for (int i : llvm::seq(n_impl.kind.child_count())) {
if (nodes.empty()) {
return Error(llvm::formatv(
"Node #{0} is a {1} with child_count {2}, but only had {3} "
+2 -4
View File
@@ -4,11 +4,10 @@
#include "toolchain/parser/parser_context.h"
#include <cstdlib>
#include <memory>
#include <optional>
#include "common/check.h"
#include "llvm/ADT/STLExtras.h"
#include "toolchain/lexer/token_kind.h"
#include "toolchain/lexer/tokenized_buffer.h"
#include "toolchain/parser/parse_node_kind.h"
@@ -448,8 +447,7 @@ auto ParserContext::EmitExpectedDeclarationSemiOrDefinition(
auto ParserContext::PrintForStackDump(llvm::raw_ostream& output) const -> void {
output << "Parser stack:\n";
for (int i = 0; i < static_cast<int>(state_stack_.size()); ++i) {
const auto& entry = state_stack_[i];
for (auto [i, entry] : llvm::enumerate(state_stack_)) {
output << "\t" << i << ".\t" << entry.state;
PrintTokenForStackDump(output, entry.token);
}
+3 -3
View File
@@ -8,6 +8,7 @@
#include "common/check.h"
#include "common/vlog.h"
#include "llvm/ADT/STLExtras.h"
#include "toolchain/diagnostics/diagnostic_kind.h"
#include "toolchain/lexer/tokenized_buffer.h"
#include "toolchain/parser/parse_node_kind.h"
@@ -298,9 +299,8 @@ auto SemanticsContext::ImplicitAsForArgs(
// Check type conversions per-element.
// TODO: arg_ir_id is passed so that implicit conversions can be inserted.
// It's currently not supported, but will be needed.
for (size_t i = 0; i < arg_refs.size(); ++i) {
auto value_id = arg_refs[i];
auto as_type_id = semantics_ir_->GetNode(param_refs[i]).type_id();
for (auto [i, value_id, param_ref] : llvm::enumerate(arg_refs, param_refs)) {
auto as_type_id = semantics_ir_->GetNode(param_ref).type_id();
if (ImplicitAsImpl(value_id, as_type_id,
diagnostic == nullptr ? &value_id : nullptr) ==
ImplicitAsKind::Incompatible) {
@@ -2,6 +2,7 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "llvm/ADT/STLExtras.h"
#include "toolchain/semantics/semantics_context.h"
#include "toolchain/semantics/semantics_node.h"
@@ -30,8 +31,8 @@ auto SemanticsHandleMemberAccessExpression(SemanticsContext& context,
auto refs =
context.semantics_ir().GetNodeBlock(base_type.GetAsStructType());
// TODO: Do we need to optimize this with a lookup table for O(1)?
for (int i = 0; i < static_cast<int>(refs.size()); ++i) {
auto ref = context.semantics_ir().GetNode(refs[i]);
for (auto [i, ref_id] : llvm::enumerate(refs)) {
auto ref = context.semantics_ir().GetNode(ref_id);
if (auto [field_name_id, field_type_id] = ref.GetAsStructTypeField();
name_id == field_name_id) {
context.AddNodeAndPush(
+2 -3
View File
@@ -5,9 +5,8 @@
#include "toolchain/semantics/semantics_ir.h"
#include "common/check.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/SaveAndRestore.h"
#include "toolchain/base/pretty_stack_trace_function.h"
#include "toolchain/parser/parse_tree_node_location_translator.h"
#include "toolchain/semantics/semantics_builtin_kind.h"
@@ -52,7 +51,7 @@ auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
// Copy builtins over.
semantics_ir.nodes_.resize_for_overwrite(SemanticsBuiltinKind::ValidCount);
static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
for (int i : llvm::seq(SemanticsBuiltinKind::ValidCount)) {
// We can reuse the type node ID because the offsets of cross-references
// will be the same in this IR.
auto type = builtin_ir.nodes_[i].type_id();
@@ -4,8 +4,8 @@
#include "toolchain/semantics/semantics_ir_formatter.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/SaveAndRestore.h"
#include "toolchain/lexer/tokenized_buffer.h"
#include "toolchain/parser/parse_tree.h"
@@ -45,7 +45,7 @@ class NodeNamer {
CollectNamesInBlock(ScopeIndex::Package, semantics_ir.top_node_block_id());
// Build each function scope.
for (int i = 0; i != semantics_ir.functions_size(); ++i) {
for (int i : llvm::seq(semantics_ir.functions_size())) {
auto fn_id = SemanticsFunctionId(i);
auto fn_scope = GetScopeFor(fn_id);
const auto& fn = semantics_ir.GetFunction(fn_id);
@@ -405,7 +405,7 @@ class SemanticsIRFormatter {
}
out_ << "}\n";
for (int i = 0; i != semantics_ir_.functions_size(); ++i) {
for (int i : llvm::seq(semantics_ir_.functions_size())) {
FormatFunction(SemanticsFunctionId(i));
}
}
@@ -5,6 +5,7 @@
#include "toolchain/semantics/semantics_node_block_stack.h"
#include "common/vlog.h"
#include "llvm/ADT/STLExtras.h"
#include "toolchain/semantics/semantics_node.h"
namespace Carbon {
@@ -40,8 +41,8 @@ auto SemanticsNodeBlockStack::Pop() -> SemanticsNodeBlockId {
auto SemanticsNodeBlockStack::PrintForStackDump(llvm::raw_ostream& output) const
-> void {
output << name_ << ":\n";
for (int i = 0; i < static_cast<int>(stack_.size()); ++i) {
output << "\t" << i << ".\t" << stack_[i] << "\n";
for (auto [i, entry] : llvm::enumerate(stack_)) {
output << "\t" << i << ".\t" << entry << "\n";
}
}
+2 -2
View File
@@ -4,6 +4,7 @@
#include "toolchain/semantics/semantics_node_stack.h"
#include "llvm/ADT/STLExtras.h"
#include "toolchain/semantics/semantics_node.h"
namespace Carbon {
@@ -11,8 +12,7 @@ namespace Carbon {
auto SemanticsNodeStack::PrintForStackDump(llvm::raw_ostream& output) const
-> void {
output << "SemanticsNodeStack:\n";
for (int i = 0; i < static_cast<int>(stack_.size()); ++i) {
const auto& entry = stack_[i];
for (auto [i, entry] : llvm::enumerate(stack_)) {
auto parse_node_kind = parse_tree_->node_kind(entry.parse_node);
output << "\t" << i << ".\t" << parse_node_kind;
if (parse_node_kind == ParseNodeKind::PatternBinding) {