Skip to content

Commit

Permalink
link: simplify control flow
Browse files Browse the repository at this point in the history
This refactor was left out of the previous commit to make the diff less
noisy and easier to review. There should be no change in behavior.
  • Loading branch information
ifreund committed Jan 29, 2025
1 parent d883770 commit 8c90833
Showing 1 changed file with 110 additions and 109 deletions.
219 changes: 110 additions & 109 deletions src/link.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1890,8 +1890,116 @@ pub fn resolveInputs(
mem.reverse(UnresolvedInput, unresolved_inputs.items);

syslib: while (unresolved_inputs.popOrNull()) |unresolved_input| {
const name_query: UnresolvedInput.NameQuery = switch (unresolved_input) {
.name_query => |nq| nq,
switch (unresolved_input) {
.name_query => |name_query| {
const query = name_query.query;

// Checked in the first pass above while looking for libc libraries.
assert(!fs.path.isAbsolute(name_query.name));

checked_paths.clearRetainingCapacity();

switch (query.search_strategy) {
.mode_first, .no_fallback => {
// check for preferred mode
for (lib_directories) |lib_directory| switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.preferred_mode,
color,
)) {
.ok => continue :syslib,
.no_match => {},
};
// check for fallback mode
if (query.search_strategy == .no_fallback) {
try failed_libs.append(arena, .{
.name = name_query.name,
.strategy = query.search_strategy,
.checked_paths = try arena.dupe(u8, checked_paths.items),
.preferred_mode = query.preferred_mode,
});
continue :syslib;
}
for (lib_directories) |lib_directory| switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.fallbackMode(),
color,
)) {
.ok => continue :syslib,
.no_match => {},
};
try failed_libs.append(arena, .{
.name = name_query.name,
.strategy = query.search_strategy,
.checked_paths = try arena.dupe(u8, checked_paths.items),
.preferred_mode = query.preferred_mode,
});
continue :syslib;
},
.paths_first => {
for (lib_directories) |lib_directory| {
// check for preferred mode
switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.preferred_mode,
color,
)) {
.ok => continue :syslib,
.no_match => {},
}

// check for fallback mode
switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.fallbackMode(),
color,
)) {
.ok => continue :syslib,
.no_match => {},
}
}
try failed_libs.append(arena, .{
.name = name_query.name,
.strategy = query.search_strategy,
.checked_paths = try arena.dupe(u8, checked_paths.items),
.preferred_mode = query.preferred_mode,
});
continue :syslib;
},
}
},
.ambiguous_name => |an| {
// First check the path relative to the current working directory.
// If the file is a library and is not found there, check the library search paths as well.
Expand Down Expand Up @@ -1961,113 +2069,6 @@ pub fn resolveInputs(
try resolved_inputs.append(gpa, .{ .dso_exact = dso_exact });
continue;
},
};
const query = name_query.query;

// Checked in the first pass above while looking for libc libraries.
assert(!fs.path.isAbsolute(name_query.name));

checked_paths.clearRetainingCapacity();

switch (query.search_strategy) {
.mode_first, .no_fallback => {
// check for preferred mode
for (lib_directories) |lib_directory| switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.preferred_mode,
color,
)) {
.ok => continue :syslib,
.no_match => {},
};
// check for fallback mode
if (query.search_strategy == .no_fallback) {
try failed_libs.append(arena, .{
.name = name_query.name,
.strategy = query.search_strategy,
.checked_paths = try arena.dupe(u8, checked_paths.items),
.preferred_mode = query.preferred_mode,
});
continue :syslib;
}
for (lib_directories) |lib_directory| switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.fallbackMode(),
color,
)) {
.ok => continue :syslib,
.no_match => {},
};
try failed_libs.append(arena, .{
.name = name_query.name,
.strategy = query.search_strategy,
.checked_paths = try arena.dupe(u8, checked_paths.items),
.preferred_mode = query.preferred_mode,
});
continue :syslib;
},
.paths_first => {
for (lib_directories) |lib_directory| {
// check for preferred mode
switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.preferred_mode,
color,
)) {
.ok => continue :syslib,
.no_match => {},
}

// check for fallback mode
switch (try resolveLibInput(
gpa,
arena,
unresolved_inputs,
resolved_inputs,
&checked_paths,
&ld_script_bytes,
lib_directory,
name_query,
target,
query.fallbackMode(),
color,
)) {
.ok => continue :syslib,
.no_match => {},
}
}
try failed_libs.append(arena, .{
.name = name_query.name,
.strategy = query.search_strategy,
.checked_paths = try arena.dupe(u8, checked_paths.items),
.preferred_mode = query.preferred_mode,
});
continue :syslib;
},
}
@compileError("unreachable");
}
Expand Down

0 comments on commit 8c90833

Please sign in to comment.