Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 5 | #ifndef CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_SYMBOL_TABLE_H_ |
| 6 | #define CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_SYMBOL_TABLE_H_ |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 7 | |
| 8 | #include <optional> |
Googler | 7055729 | 2021-11-30 11:46:02 +0000 | [diff] [blame] | 9 | #include <string> |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 10 | |
| 11 | #include "lifetime_annotations/lifetime.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 12 | #include "llvm/ADT/DenseMap.h" |
| 13 | #include "llvm/ADT/StringMap.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 15 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace lifetimes { |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 19 | |
| 20 | // One-to-one mapping between lifetime names and the corresponding lifetimes. |
| 21 | class LifetimeSymbolTable { |
| 22 | public: |
| 23 | // Looks up a lifetime name in the symbol table. |
| 24 | // Returns the corresponding lifetime if the name was present in the symbol |
| 25 | // table, or nullopt if the lifetime name wasn't found. |
| 26 | // If `name` is "static", returns `Lifetime::Static()`. |
| 27 | std::optional<Lifetime> LookupName(llvm::StringRef name) const; |
| 28 | |
| 29 | // Looks up a lifetime name in the symbol table and inserts a new variable |
| 30 | // lifetime in the table if the name was not found. |
| 31 | // If `name` is "static", returns `Lifetime::Static()`. |
| 32 | Lifetime LookupNameAndMaybeDeclare(llvm::StringRef name); |
| 33 | |
Googler | 7055729 | 2021-11-30 11:46:02 +0000 | [diff] [blame] | 34 | // Looks up a lifetime in the symbol table. |
| 35 | // Returns the corresponding name if the lifetime was present in the symbol |
| 36 | // table, or nullopt if the lifetime wasn't found. |
| 37 | // If `lifetime` is `Lifetime::Static()`, returns "static". |
| 38 | std::optional<llvm::StringRef> LookupLifetime(Lifetime lifetime) const; |
| 39 | |
Googler | 352ec04 | 2021-11-30 11:46:23 +0000 | [diff] [blame] | 40 | // Looks up a lifetime in the symbol table and inserts a new autogenerated |
| 41 | // name for it if the lifetime was not found. |
| 42 | // If `lifetime` is `Lifetime::Static()`, returns "static". |
| 43 | // The autogenerated name will be the first name from the sequence |
| 44 | // "a", ..., "z", "aa", "ab", "ac", ... that is not yet contained in the |
| 45 | // symbol table. |
| 46 | llvm::StringRef LookupLifetimeAndMaybeDeclare(Lifetime lifetime); |
| 47 | |
Luca Versari | d101a29 | 2022-01-27 17:28:42 +0000 | [diff] [blame] | 48 | // Associates a lifetime to a given name. |
| 49 | // The name must not be already used. |
| 50 | // Calling this function with a lifetime that is already used will cause calls |
| 51 | // to LookupLifetime() to have undefined behaviour. |
| 52 | void Add(llvm::StringRef name, Lifetime lifetime); |
| 53 | |
Luca Versari | 95ce68f | 2022-03-24 14:44:19 +0000 | [diff] [blame] | 54 | // Re-binds a name to a different lifetime. |
| 55 | // The name *must* be already used. |
| 56 | void Rebind(llvm::StringRef name, Lifetime lifetime); |
| 57 | |
Luca Versari | 7f1783d | 2022-01-31 14:40:39 +0000 | [diff] [blame] | 58 | // Accessor for hashing/debugging purposes. |
| 59 | const llvm::StringMap<Lifetime>& GetMapping() const { |
| 60 | return name_to_lifetime_; |
| 61 | } |
| 62 | |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 63 | private: |
| 64 | llvm::StringMap<Lifetime> name_to_lifetime_; |
Googler | 7055729 | 2021-11-30 11:46:02 +0000 | [diff] [blame] | 65 | llvm::DenseMap<Lifetime, std::string> lifetime_to_name_; |
Googler | 352ec04 | 2021-11-30 11:46:23 +0000 | [diff] [blame] | 66 | int next_name_index_ = 0; |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 69 | } // namespace lifetimes |
| 70 | } // namespace tidy |
| 71 | } // namespace clang |
Googler | 68f2115 | 2021-11-30 11:45:33 +0000 | [diff] [blame] | 72 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 73 | #endif // CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_SYMBOL_TABLE_H_ |