LLVM API Documentation

TypeSymbolTable.cpp

Go to the documentation of this file.
00001 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the TypeSymbolTable class for the VMCore library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/TypeSymbolTable.h"
00015 #include "llvm/DerivedTypes.h"
00016 #include "llvm/ADT/StringExtras.h"
00017 #include "llvm/Support/Streams.h"
00018 #include <algorithm>
00019 using namespace llvm;
00020 
00021 #define DEBUG_SYMBOL_TABLE 0
00022 #define DEBUG_ABSTYPE 0
00023 
00024 TypeSymbolTable::~TypeSymbolTable() {
00025   // Drop all abstract type references in the type plane...
00026   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
00027     if (TI->second->isAbstract())   // If abstract, drop the reference...
00028       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
00029   }
00030 }
00031 
00032 std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
00033   std::string TryName = BaseName;
00034   const_iterator End = tmap.end();
00035 
00036   // See if the name exists
00037   while (tmap.find(TryName) != End)            // Loop until we find a free
00038     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
00039   return TryName;
00040 }
00041 
00042 // lookup a type by name - returns null on failure
00043 Type* TypeSymbolTable::lookup(const std::string& Name) const {
00044   const_iterator TI = tmap.find(Name);
00045   if (TI != tmap.end())
00046     return const_cast<Type*>(TI->second);
00047   return 0;
00048 }
00049 
00050 // remove - Remove a type from the symbol table...
00051 Type* TypeSymbolTable::remove(iterator Entry) {
00052   assert(Entry != tmap.end() && "Invalid entry to remove!");
00053 
00054   const Type* Result = Entry->second;
00055 
00056 #if DEBUG_SYMBOL_TABLE
00057   dump();
00058   cerr << " Removing Value: " << Result->getName() << "\n";
00059 #endif
00060 
00061   tmap.erase(Entry);
00062 
00063   // If we are removing an abstract type, remove the symbol table from it's use
00064   // list...
00065   if (Result->isAbstract()) {
00066 #if DEBUG_ABSTYPE
00067     cerr << "Removing abstract type from symtab"
00068          << Result->getDescription()
00069          << "\n";
00070 #endif
00071     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
00072   }
00073 
00074   return const_cast<Type*>(Result);
00075 }
00076 
00077 
00078 // insert - Insert a type into the symbol table with the specified name...
00079 void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
00080   assert(T && "Can't insert null type into symbol table!");
00081 
00082   if (tmap.insert(make_pair(Name, T)).second) {
00083     // Type inserted fine with no conflict.
00084     
00085 #if DEBUG_SYMBOL_TABLE
00086     dump();
00087     cerr << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
00088 #endif
00089   } else {
00090     // If there is a name conflict...
00091     
00092     // Check to see if there is a naming conflict.  If so, rename this type!
00093     std::string UniqueName = Name;
00094     if (lookup(Name))
00095       UniqueName = getUniqueName(Name);
00096     
00097 #if DEBUG_SYMBOL_TABLE
00098     dump();
00099     cerr << " Inserting type: " << UniqueName << ": "
00100         << T->getDescription() << "\n";
00101 #endif
00102 
00103     // Insert the tmap entry
00104     tmap.insert(make_pair(UniqueName, T));
00105   }
00106 
00107   // If we are adding an abstract type, add the symbol table to it's use list.
00108   if (T->isAbstract()) {
00109     cast<DerivedType>(T)->addAbstractTypeUser(this);
00110 #if DEBUG_ABSTYPE
00111     cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
00112 #endif
00113   }
00114 }
00115 
00116 // This function is called when one of the types in the type plane are refined
00117 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
00118                                          const Type *NewType) {
00119 
00120   // Loop over all of the types in the symbol table, replacing any references
00121   // to OldType with references to NewType.  Note that there may be multiple
00122   // occurrences, and although we only need to remove one at a time, it's
00123   // faster to remove them all in one pass.
00124   //
00125   for (iterator I = begin(), E = end(); I != E; ++I) {
00126     if (I->second == (Type*)OldType) {  // FIXME when Types aren't const.
00127 #if DEBUG_ABSTYPE
00128       cerr << "Removing type " << OldType->getDescription() << "\n";
00129 #endif
00130       OldType->removeAbstractTypeUser(this);
00131 
00132       I->second = (Type*)NewType;  // TODO FIXME when types aren't const
00133       if (NewType->isAbstract()) {
00134 #if DEBUG_ABSTYPE
00135         cerr << "Added type " << NewType->getDescription() << "\n";
00136 #endif
00137         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
00138       }
00139     }
00140   }
00141 }
00142 
00143 
00144 // Handle situation where type becomes Concreate from Abstract
00145 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
00146   // Loop over all of the types in the symbol table, dropping any abstract
00147   // type user entries for AbsTy which occur because there are names for the
00148   // type.
00149   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
00150     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
00151       AbsTy->removeAbstractTypeUser(this);
00152 }
00153 
00154 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
00155   cerr << "  '" << T.first << "' = ";
00156   T.second->dump();
00157   cerr << "\n";
00158 }
00159 
00160 void TypeSymbolTable::dump() const {
00161   cerr << "TypeSymbolPlane: ";
00162   for_each(tmap.begin(), tmap.end(), DumpTypes);
00163 }
00164 
00165 // vim: sw=2 ai



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.