diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-20 11:44:00 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-20 11:44:14 +0100 |
| commit | 55ecd2ff710377c48cbc620be02db32cfcdc29c6 (patch) | |
| tree | 74475402303b8890f8af58b0c1b46e419d048651 /src/main/java/org/shedlang/compiler/backends | |
| parent | a43868cec3986d17da9987caf89ccc137e3a48d4 (diff) | |
Add explicit locals
Diffstat (limited to 'src/main/java/org/shedlang/compiler/backends')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java b/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java index d8ca3fc2..7a1aba01 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java @@ -21,12 +21,7 @@ public record NameAllocator<TPrefix>(PSet<String> existingNames) { } public Result<TPrefix> allocateFixedName(TPrefix prefix, String name) { - var fullName = prefix + "__" + name; - - if (existingNames.contains(fullName)) { - throw new RuntimeException("name is already allocated: " + fullName); - } - + var fullName = allocateFixedName(existingNames::contains, prefix, name); var newAllocator = new NameAllocator<TPrefix>(existingNames.plus(fullName)); return new Result<>(fullName, newAllocator); } @@ -52,6 +47,16 @@ public record NameAllocator<TPrefix>(PSet<String> existingNames) { } } + public static <TPrefix> String allocateFixedName(Predicate<String> isUsed, TPrefix prefix, String name) { + var fullName = prefix + "__" + name; + + if (isUsed.test(fullName)) { + throw new RuntimeException("name is already allocated: " + fullName); + } + + return fullName; + } + public record Result<TPrefix>(String name, NameAllocator<TPrefix> allocator) { } } |
