Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLMangler.h" |
| 9 | #include "src/sksl/ir/SkSLSymbolTable.h" |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 10 | |
| 11 | namespace SkSL { |
| 12 | |
John Stiles | 769146f | 2021-09-15 21:47:00 -0400 | [diff] [blame] | 13 | String Mangler::uniqueName(skstd::string_view baseName, SymbolTable* symbolTable) { |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 14 | SkASSERT(symbolTable); |
| 15 | // The inliner runs more than once, so the base name might already have been mangled and have a |
| 16 | // prefix like "_123_x". Let's strip that prefix off to make the generated code easier to read. |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 17 | if (baseName.starts_with("_")) { |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 18 | // Determine if we have a string of digits. |
| 19 | int offset = 1; |
| 20 | while (isdigit(baseName[offset])) { |
| 21 | ++offset; |
| 22 | } |
| 23 | // If we found digits, another underscore, and anything else, that's the mangler prefix. |
| 24 | // Strip it off. |
| 25 | if (offset > 1 && baseName[offset] == '_' && baseName[offset + 1] != '\0') { |
John Stiles | 769146f | 2021-09-15 21:47:00 -0400 | [diff] [blame] | 26 | baseName.remove_prefix(offset + 1); |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 27 | } else { |
| 28 | // This name doesn't contain a mangler prefix, but it does start with an underscore. |
| 29 | // OpenGL disallows two consecutive underscores anywhere in the string, and we'll be |
| 30 | // adding one as part of the mangler prefix, so strip the leading underscore. |
John Stiles | 769146f | 2021-09-15 21:47:00 -0400 | [diff] [blame] | 31 | baseName.remove_prefix(1); |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | // Append a unique numeric prefix to avoid name overlap. Check the symbol table to make sure |
| 36 | // we're not reusing an existing name. (Note that within a single compilation pass, this check |
| 37 | // isn't fully comprehensive, as code isn't always generated in top-to-bottom order.) |
| 38 | String uniqueName; |
| 39 | for (;;) { |
John Stiles | 769146f | 2021-09-15 21:47:00 -0400 | [diff] [blame] | 40 | uniqueName = String::printf("_%d_%.*s", fCounter++, (int)baseName.size(), baseName.data()); |
| 41 | if ((*symbolTable)[uniqueName] == nullptr) { |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 42 | break; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return uniqueName; |
| 47 | } |
| 48 | |
| 49 | } // namespace SkSL |