blob: 35fc97b2d3ce06231e3ae9248e59f50fbe136a5b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner84e66db2007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the name/Value symbol table for LLVM.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_VALUE_SYMBOL_TABLE_H
15#define LLVM_VALUE_SYMBOL_TABLE_H
16
17#include "llvm/Value.h"
18#include "llvm/ADT/StringMap.h"
Chandler Carruth1ec7df12009-10-26 01:35:46 +000019#include "llvm/System/DataTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020
21namespace llvm {
22 template<typename ValueSubClass, typename ItemParentClass>
23 class SymbolTableListTraits;
24 class BasicBlock;
25 class Function;
Devang Patelb32440d2009-07-29 17:16:17 +000026 class NamedMDNode;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027 class Module;
Daniel Dunbare2db8232009-07-23 18:52:12 +000028 class StringRef;
Devang Patel833d4422010-01-07 19:39:36 +000029
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030/// This class provides a symbol table of name/value pairs. It is essentially
31/// a std::map<std::string,Value*> but has a controlled interface provided by
32/// LLVM as well as ensuring uniqueness of names.
33///
34class ValueSymbolTable {
35 friend class Value;
36 friend class SymbolTableListTraits<Argument, Function>;
37 friend class SymbolTableListTraits<BasicBlock, Function>;
38 friend class SymbolTableListTraits<Instruction, BasicBlock>;
39 friend class SymbolTableListTraits<Function, Module>;
40 friend class SymbolTableListTraits<GlobalVariable, Module>;
41 friend class SymbolTableListTraits<GlobalAlias, Module>;
42/// @name Types
43/// @{
44public:
45 /// @brief A mapping of names to values.
Chris Lattnerd89380f2009-08-04 23:07:12 +000046 typedef StringMap<Value*> ValueMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 /// @brief An iterator over a ValueMap.
49 typedef ValueMap::iterator iterator;
50
51 /// @brief A const_iterator over a ValueMap.
52 typedef ValueMap::const_iterator const_iterator;
53
54/// @}
55/// @name Constructors
56/// @{
57public:
58
59 ValueSymbolTable() : vmap(0), LastUnique(0) {}
60 ~ValueSymbolTable();
61
62/// @}
63/// @name Accessors
64/// @{
65public:
66
Daniel Dunbare2db8232009-07-23 18:52:12 +000067 /// This method finds the value with the given \p Name in the
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 /// the symbol table.
Daniel Dunbare2db8232009-07-23 18:52:12 +000069 /// @returns the value associated with the \p Name
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 /// @brief Lookup a named Value.
Daniel Dunbarde46a5b2009-11-06 10:58:06 +000071 Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
73 /// @returns true iff the symbol table is empty
74 /// @brief Determine if the symbol table is empty
75 inline bool empty() const { return vmap.empty(); }
76
77 /// @brief The number of name/type pairs is returned.
78 inline unsigned size() const { return unsigned(vmap.size()); }
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 /// This function can be used from the debugger to display the
81 /// content of the symbol table while debugging.
82 /// @brief Print out symbol table on stderr
83 void dump() const;
84
85/// @}
86/// @name Iteration
87/// @{
88public:
89 /// @brief Get an iterator that from the beginning of the symbol table.
90 inline iterator begin() { return vmap.begin(); }
91
92 /// @brief Get a const_iterator that from the beginning of the symbol table.
93 inline const_iterator begin() const { return vmap.begin(); }
94
95 /// @brief Get an iterator to the end of the symbol table.
96 inline iterator end() { return vmap.end(); }
97
98 /// @brief Get a const_iterator to the end of the symbol table.
99 inline const_iterator end() const { return vmap.end(); }
100
101/// @}
102/// @name Mutators
103/// @{
104private:
105 /// This method adds the provided value \p N to the symbol table. The Value
106 /// must have a name which is used to place the value in the symbol table.
107 /// If the inserted name conflicts, this renames the value.
108 /// @brief Add a named value to the symbol table
109 void reinsertValue(Value *V);
110
111 /// createValueName - This method attempts to create a value name and insert
112 /// it into the symbol table with the specified name. If it conflicts, it
113 /// auto-renames the name and returns that instead.
Daniel Dunbarde46a5b2009-11-06 10:58:06 +0000114 ValueName *createValueName(StringRef Name, Value *V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115
116 /// This method removes a value from the symbol table. It leaves the
117 /// ValueName attached to the value, but it is no longer inserted in the
118 /// symtab.
119 void removeValueName(ValueName *V);
120
121/// @}
122/// @name Internal Data
123/// @{
124private:
125 ValueMap vmap; ///< The map that holds the symbol table.
126 mutable uint32_t LastUnique; ///< Counter for tracking unique names
127
128/// @}
129};
130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131} // End llvm namespace
132
133#endif