2 * Copyright (c) 1996-2007 Barton P. Miller
4 * We provide the Paradyn Parallel Performance Tools (below
5 * described as "Paradyn") on an AS IS basis, and do not warrant its
6 * validity or performance. We reserve the right to update, modify,
7 * or discontinue this software at any time. We shall have no
8 * obligation to supply such updates or modifications or any other
9 * form of support to you.
11 * By your use of Paradyn, you understand and agree that we (or any
12 * other person or entity with proprietary rights in Paradyn) are
13 * under no obligation to provide either maintenance services,
14 * update services, notices of latent defects, or correction of
15 * defects for Paradyn.
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "Collections.h"
41 #include "common/h/headers.h"
44 using namespace Dyninst;
45 using namespace Dyninst::SymtabAPI;
47 /**************************************************************************
49 *************************************************************************/
52 * localVarCollection::~localVarCollection
54 * Destructor for localVarCollection. Deletes all type objects that
55 * have been inserted into the collection.
57 localVarCollection::~localVarCollection()
59 dyn_hash_map<std::string, localVar *>::iterator li = localVariablesByName.begin();
61 // delete localVariablesByName collection
62 for(;li!=localVariablesByName.end();li++)
69 * localVarCollection::addLocalVar()
70 * This function adds local variables to the set of local variables
74 void localVarCollection::addLocalVar(localVar * var)
76 localVariablesByName[var->getName()] = var;
77 localVars.push_back(var);
81 * localVarCollection::findLocalVar()
82 * This function finds a local variable by name and returns a pointer to
83 * it or NULL if the local variable does not exist in the set of function
86 localVar *localVarCollection::findLocalVar(std::string &name){
88 if(localVariablesByName.find(name) != localVariablesByName.end())
89 return localVariablesByName[name];
91 return (localVar *)NULL;
95 * localVarCollection::getAllVars()
96 * this function returns all the local variables in the collection.
98 std::vector<localVar *> *localVarCollection::getAllVars() {
102 void localVarCollection::serialize(SerializerBase *, const char *)
104 fprintf(stderr, "%s[%d]: IMPLEMENT ME\n", FILE__, __LINE__);
107 // Could be somewhere else... for DWARF-work.
108 dyn_hash_map<std::string, typeCollection *> typeCollection::fileToTypesMap;
114 typeCollection *typeCollection::getGlobalTypeCollection() {
115 typeCollection *tc = new typeCollection();
120 typeCollection *typeCollection::getModTypeCollection(Module *mod) {
123 //#if defined(USES_DWARF_DEBUG)
124 // TODO: can we use this on other platforms as well?
125 if( fileToTypesMap.find( mod->exec()->file()) != fileToTypesMap.end()) {
126 // /* DEBUG */ fprintf( stderr, "%s[%d]: found cache for file '%s' (module '%s')\n", __FILE__, __LINE__, fileName, moduleFileName );
127 typeCollection *cachedTC = fileToTypesMap [mod->exec()->file()];
128 cachedTC->refcount++;
133 typeCollection *newTC = new typeCollection();
134 fileToTypesMap[mod->exec()->file()] = newTC;
139 void typeCollection::freeTypeCollection(typeCollection *tc) {
142 if (tc->refcount == 0) {
143 dyn_hash_map<std::string, typeCollection *>::iterator iter = fileToTypesMap.begin();
144 for (; iter!= fileToTypesMap.end(); iter++) {
145 if (iter->second == tc) {
146 fileToTypesMap.erase(iter->first);
155 * typeCollection::typeCollection
157 * Constructor for typeCollection. Creates the two dictionaries
158 * for the type, by Name and ID.
160 typeCollection::typeCollection():
164 /* Initialize hash tables: typesByName, typesByID */
168 * typeCollection::~typeCollection
170 * Destructor for typeCollection. Deletes all type objects that have
171 * been inserted into the collection.
173 typeCollection::~typeCollection()
175 // We sometimes directly delete (refcount == 1) or go through the
176 // decRefCount (which will delete when refcount == 0)
177 assert(refcount == 0 ||
179 // delete all of the types
180 // This doesn't seem to work - jkh 1/31/00
182 dictionary_hash_iter<std::string, type *> ti(typesByName);
183 dictionary_hash_iter<int, type *> tid(typesByID);
184 dictionary_hash_iter<std::string, type *> gi(globalVarsByName);
190 while (tid.next(id, type))
194 // Underlying types deleted already just need to get rid of pointers
195 while (ti.next(name, type))
198 // delete globalVarsByName collection
199 while (gi.next(name, type))
202 for (dictionary_hash_iter<int, type *> it = typesByID.begin();
203 it != typesByID.end();
205 (*it)->decrRefCount();
207 for (dictionary_hash_iter<std::string, type *> it2 = typesByName.begin();
208 it2 != typesByName.end();
210 (*it2)->decrRefCount();
216 * typeCollection::findType
218 * Retrieve a pointer to a type object representing the named type from
219 * the collection. If no such type exists and no such type can be derived
220 * from existing types, then the function returns NULL.
222 * name The name of the type to look up.
223 * id The unique type ID of the type tp look up.
225 Type *typeCollection::findType(std::string name)
227 if (typesByName.find(name) != typesByName.end())
228 return typesByName[name];
229 else if (Symtab::builtInTypes)
230 return Symtab::builtInTypes->findBuiltInType(name);
235 Type *typeCollection::findTypeLocal(std::string name)
237 if (typesByName.find(name) != typesByName.end())
238 return typesByName[name];
243 Type *typeCollection::findTypeLocal(const int ID)
245 if (typesByID.find(ID) != typesByID.end())
246 return typesByID[ID];
252 Type * typeCollection::findOrCreateType( const int ID ) {
253 if( typesByID.find(ID) != typesByID.end()) { return typesByID[ID]; }
255 Type * returnType = NULL;
256 if( Symtab::builtInTypes ) {
257 returnType = Symtab::builtInTypes->findBuiltInType(ID);
262 /* Create a placeholder type. */
263 returnType = Type::createPlaceholder(ID);
264 assert( returnType != NULL );
266 /* Having created the type, add it. */
267 addType( returnType );
270 } /* end findOrCreateType() */
272 Type *typeCollection::findType(const int ID)
274 if (typesByID.find(ID) != typesByID.end())
275 return typesByID[ID];
279 if (Symtab::builtInTypes)
280 ret = Symtab::builtInTypes->findBuiltInType(ID);
287 * typeCollection::findVariableType
289 * Retrieve a pointer to a type object representing the named type from
290 * the collection. If no such type exists and no such type can be derived
291 * from existing types, then the function returns NULL.
293 * name The name of the type to look up.
295 Type *typeCollection::findVariableType(std::string &name)
297 if (globalVarsByName.find(name) != globalVarsByName.end())
298 return globalVarsByName[name];
300 return (Type *) NULL;
304 * typeCollection::addType
306 * Add a new type to the type collection. Note that when a type is added to
307 * the collection, it becomes the collection's responsibility to delete it
308 * when it is no longer needed. For one thing, this means that a type
309 * allocated on the stack should *NEVER* be put into a typeCollection.
311 void typeCollection::addType(Type *type)
313 if(type->getName() != "") { //Type could have no name.
314 typesByName[type->getName()] = type;
315 type->incrRefCount();
318 //Types can share the same ID for typedef, thus not adding types with
319 //same ID to the collection
321 // XXX - Fortran seems to restart type numbers for each subroutine
322 // if(!(this->findType(type->getID())))
323 typesByID[type->getID()] = type;
324 type->incrRefCount();
327 void typeCollection::addGlobalVariable(std::string &name, Type *type)
329 globalVarsByName[name] = type;
332 void typeCollection::clearNumberedTypes()
334 for (dyn_hash_map<int, Type *>::iterator it = typesByID.begin();
335 it != typesByID.end();
339 it->second->decrRefCount();
341 fprintf(stderr, "%s[%d]: FIXME\n", FILE__, __LINE__);
348 * localVarCollection::getAllVars()
349 * this function returns all the local variables in the collection.
351 std::vector<Type *> *typeCollection::getAllTypes() {
352 std::vector<Type *> *typesVec = new std::vector<Type *>;
353 //for (dyn_hash_map<int, Type *>::iterator it = typesByID.begin();
354 // it != typesByID.end();
356 for (dyn_hash_map<string, Type *>::iterator it = typesByName.begin();
357 it != typesByName.end();
359 typesVec->push_back(it->second);
361 if(!typesVec->size()){
368 vector<pair<string, Type *> > *typeCollection::getAllGlobalVariables() {
369 vector<pair<string, Type *> > *varsVec = new vector<pair<string, Type *> >;
370 for(dyn_hash_map<string, Type *>::iterator it = globalVarsByName.begin();
371 it != globalVarsByName.end(); it++) {
372 varsVec->push_back(pair<string, Type *>(it->first, it->second));
374 if(!varsVec->size()){
381 void typeCollection::serialize(SerializerBase *, const char *)
383 fprintf(stderr, "%s[%d]: IMPLEMENT ME\n", FILE__, __LINE__);
387 * builtInTypeCollection::builtInTypeCollection
389 * Constructor for builtInTypeCollection. Creates adictionary
390 * for the builtInType, by Name and ID.
391 * XXX- Don't know if a collection is needed for types by name, but
392 * it is created just in case. jdd 4/21/99
394 builtInTypeCollection::builtInTypeCollection()
396 /* Initialize hash tables: builtInTypesByName, builtInTypesByID */
400 * builtInTypeCollection::~builtInTypeCollection
402 * Destructor for builtInTypeCollection. Deletes all builtInType objects that have
403 * been inserted into the collection.
405 builtInTypeCollection::~builtInTypeCollection()
407 dyn_hash_map<std::string, Type *>::iterator bit = builtInTypesByName.begin();
408 dyn_hash_map<int, Type *>::iterator bitid = builtInTypesByID.begin();
410 // delete builtInTypesByName collection
411 for(;bit!=builtInTypesByName.end();bit++)
412 bit->second->decrRefCount();
413 // delete builtInTypesByID collection
414 for(;bitid!=builtInTypesByID.end();bit++)
415 bitid->second->decrRefCount();
420 * builtInTypeCollection::findBuiltInType
422 * Retrieve a pointer to a type object representing the named type from
423 * the collection. If no such type exists and no such type can be derived
424 * from existing types, then the function returns NULL.
426 * name The name of the type to look up.
427 * id The unique type ID of the type tp look up.
429 Type *builtInTypeCollection::findBuiltInType(std::string &name)
431 if (builtInTypesByName.find(name) != builtInTypesByName.end())
432 return builtInTypesByName[name];
437 Type *builtInTypeCollection::findBuiltInType(const int ID)
439 if (builtInTypesByID.find(ID) != builtInTypesByID.end())
440 return builtInTypesByID[ID];
445 void builtInTypeCollection::addBuiltInType(Type *type)
447 if(type->getName() != "") { //Type could have no name.
448 builtInTypesByName[type->getName()] = type;
449 type->incrRefCount();
451 //All built-in types have unique IDs so far jdd 4/21/99
452 builtInTypesByID[type->getID()] = type;
453 type->incrRefCount();
456 std::vector<Type *> *builtInTypeCollection::getAllBuiltInTypes() {
457 std::vector<Type *> *typesVec = new std::vector<Type *>;
458 for (dyn_hash_map<int, Type *>::iterator it = builtInTypesByID.begin();
459 it != builtInTypesByID.end();
461 typesVec->push_back(it->second);
463 if(!typesVec->size()){