diff options
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) { } } |
