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