+
+void Value::setName(const std::string &name) {
+ if (Name == name) return; // Name is already set.
+
+ // Get the symbol table to update for this object.
+ SymbolTable *ST = 0;
+ if (Instruction *I = dyn_cast<Instruction>(this)) {
+ if (BasicBlock *P = I->getParent())
+ if (Function *PP = P->getParent())
+ ST = &PP->getSymbolTable();
+ } else if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
+ if (Function *P = BB->getParent()) ST = &P->getSymbolTable();
+ } else if (GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
+ if (Module *P = GV->getParent()) ST = &P->getSymbolTable();
+ } else if (Argument *A = dyn_cast<Argument>(this)) {
+ if (Function *P = A->getParent()) ST = &P->getSymbolTable();
+ } else {
+ assert(isa<Constant>(this) && "Unknown value type!");
+ return; // no name is setable for this.
+ }
+
+ if (!ST) // No symbol table to update? Just do the change.
+ Name = name;
+ else if (hasName()) {
+ if (!name.empty()) { // Replacing name.
+ ST->changeName(this, name);
+ } else { // Transitioning from hasName -> noname.
+ ST->remove(this);
+ Name.clear();
+ }
+ } else { // Transitioning from noname -> hasName.
+ Name = name;
+ ST->insert(this);
+ }