blob: fbbee20172005bbb027b1c3c5a6cc24a477ac229 [file] [log] [blame]
Googler68f21152021-11-30 11:45:33 +00001// 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 Gribenkoe4e77d02022-03-17 14:09:39 +00005#ifndef CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_SYMBOL_TABLE_H_
6#define CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_SYMBOL_TABLE_H_
Googler68f21152021-11-30 11:45:33 +00007
8#include <optional>
Googler70557292021-11-30 11:46:02 +00009#include <string>
Googler68f21152021-11-30 11:45:33 +000010
11#include "lifetime_annotations/lifetime.h"
Lukasz Anforowiczcec7a8a2022-04-27 10:24:51 -070012#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
Googler68f21152021-11-30 11:45:33 +000015
Martin Brænne1a207c52022-04-19 00:05:38 -070016namespace clang {
17namespace tidy {
18namespace lifetimes {
Googler68f21152021-11-30 11:45:33 +000019
20// One-to-one mapping between lifetime names and the corresponding lifetimes.
21class 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
Googler70557292021-11-30 11:46:02 +000034 // 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
Googler352ec042021-11-30 11:46:23 +000040 // 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 Versarid101a292022-01-27 17:28:42 +000048 // 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 Versari95ce68f2022-03-24 14:44:19 +000054 // 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 Versari7f1783d2022-01-31 14:40:39 +000058 // Accessor for hashing/debugging purposes.
59 const llvm::StringMap<Lifetime>& GetMapping() const {
60 return name_to_lifetime_;
61 }
62
Googler68f21152021-11-30 11:45:33 +000063 private:
64 llvm::StringMap<Lifetime> name_to_lifetime_;
Googler70557292021-11-30 11:46:02 +000065 llvm::DenseMap<Lifetime, std::string> lifetime_to_name_;
Googler352ec042021-11-30 11:46:23 +000066 int next_name_index_ = 0;
Googler68f21152021-11-30 11:45:33 +000067};
68
Martin Brænne1a207c52022-04-19 00:05:38 -070069} // namespace lifetimes
70} // namespace tidy
71} // namespace clang
Googler68f21152021-11-30 11:45:33 +000072
Dmitri Gribenkoe4e77d02022-03-17 14:09:39 +000073#endif // CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_SYMBOL_TABLE_H_