LLVM API Documentation

SourceLanguage-Unknown.cpp

Go to the documentation of this file.
00001 //===-- SourceLanguage-Unknown.cpp - Implement itf for unknown languages --===//
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 // If the LLVM debugger does not have a module for a particular language, it
00011 // falls back on using this one to perform the source-language interface.  This
00012 // interface is not wonderful, but it gets the job done.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/Debugger/SourceLanguage.h"
00017 #include "llvm/Debugger/ProgramInfo.h"
00018 #include "llvm/Support/Streams.h"
00019 #include <cassert>
00020 #include <ostream>
00021 using namespace llvm;
00022 
00023 //===----------------------------------------------------------------------===//
00024 // Implement the SourceLanguage cache for the Unknown language.
00025 //
00026 
00027 namespace {
00028   /// SLUCache - This cache allows for efficient lookup of source functions by
00029   /// name.
00030   ///
00031   struct SLUCache : public SourceLanguageCache {
00032     ProgramInfo &PI;
00033     std::multimap<std::string, SourceFunctionInfo*> FunctionMap;
00034   public:
00035     SLUCache(ProgramInfo &pi);
00036 
00037     typedef std::multimap<std::string, SourceFunctionInfo*>::const_iterator
00038        fm_iterator;
00039 
00040     std::pair<fm_iterator, fm_iterator>
00041     getFunction(const std::string &Name) const {
00042       return FunctionMap.equal_range(Name);
00043     }
00044 
00045     SourceFunctionInfo *addSourceFunction(SourceFunctionInfo *SF) {
00046       FunctionMap.insert(std::make_pair(SF->getSymbolicName(), SF));
00047       return SF;
00048     }
00049   };
00050 }
00051 
00052 SLUCache::SLUCache(ProgramInfo &pi) : PI(pi) {
00053 }
00054 
00055 
00056 //===----------------------------------------------------------------------===//
00057 // Implement SourceLanguageUnknown class, which is used to handle unrecognized
00058 // languages.
00059 //
00060 
00061 namespace {
00062   static struct SLU : public SourceLanguage {
00063     //===------------------------------------------------------------------===//
00064     // Implement the miscellaneous methods...
00065     //
00066     virtual const char *getSourceLanguageName() const {
00067       return "unknown";
00068     }
00069 
00070     /// lookupFunction - Given a textual function name, return the
00071     /// SourceFunctionInfo descriptor for that function, or null if it cannot be
00072     /// found.  If the program is currently running, the RuntimeInfo object
00073     /// provides information about the current evaluation context, otherwise it
00074     /// will be null.
00075     ///
00076     virtual SourceFunctionInfo *lookupFunction(const std::string &FunctionName,
00077                                                ProgramInfo &PI,
00078                                                RuntimeInfo *RI = 0) const;
00079 
00080     //===------------------------------------------------------------------===//
00081     // We do use a cache for information...
00082     //
00083     typedef SLUCache CacheType;
00084     SLUCache *createSourceLanguageCache(ProgramInfo &PI) const {
00085       return new SLUCache(PI);
00086     }
00087 
00088     /// createSourceFunctionInfo - Create the new object and inform the cache of
00089     /// the new function.
00090     virtual SourceFunctionInfo *
00091     createSourceFunctionInfo(const GlobalVariable *Desc, ProgramInfo &PI) const;
00092 
00093   } TheUnknownSourceLanguageInstance;
00094 }
00095 
00096 const SourceLanguage &SourceLanguage::getUnknownLanguageInstance() {
00097   return TheUnknownSourceLanguageInstance;
00098 }
00099 
00100 
00101 SourceFunctionInfo *
00102 SLU::createSourceFunctionInfo(const GlobalVariable *Desc,
00103                               ProgramInfo &PI) const {
00104   SourceFunctionInfo *Result = new SourceFunctionInfo(PI, Desc);
00105   return PI.getLanguageCache(this).addSourceFunction(Result);
00106 }
00107 
00108 
00109 /// lookupFunction - Given a textual function name, return the
00110 /// SourceFunctionInfo descriptor for that function, or null if it cannot be
00111 /// found.  If the program is currently running, the RuntimeInfo object
00112 /// provides information about the current evaluation context, otherwise it will
00113 /// be null.
00114 ///
00115 SourceFunctionInfo *SLU::lookupFunction(const std::string &FunctionName,
00116                                         ProgramInfo &PI, RuntimeInfo *RI) const{
00117   SLUCache &Cache = PI.getLanguageCache(this);
00118   std::pair<SLUCache::fm_iterator, SLUCache::fm_iterator> IP
00119     = Cache.getFunction(FunctionName);
00120 
00121   if (IP.first == IP.second) {
00122     if (PI.allSourceFunctionsRead())
00123       return 0;  // Nothing found
00124 
00125     // Otherwise, we might be able to find the function if we read all of them
00126     // in.  Do so now.
00127     PI.getSourceFunctions();
00128     assert(PI.allSourceFunctionsRead() && "Didn't read in all functions?");
00129     return lookupFunction(FunctionName, PI, RI);
00130   }
00131 
00132   SourceFunctionInfo *Found = IP.first->second;
00133   ++IP.first;
00134   if (IP.first != IP.second)
00135     cout << "Whoa, found multiple functions with the same name.  I should"
00136          << " ask the user which one to use: FIXME!\n";
00137   return Found;
00138 }



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