Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame^] | 1 | //===-- SourceLanguage-Unknown.cpp - Implement itf for unknown languages --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // If the LLVM debugger does not have a module for a particular language, it |
| 11 | // falls back on using this one to perform the source-language interface. This |
| 12 | // interface is not wonderful, but it gets the job done. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Debugger/SourceLanguage.h" |
| 17 | #include "llvm/Debugger/ProgramInfo.h" |
| 18 | #include <iostream> |
| 19 | using namespace llvm; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Implement the SourceLanguage cache for the Unknown language. |
| 23 | // |
| 24 | |
| 25 | namespace { |
| 26 | /// SLUCache - This cache allows for efficient lookup of source functions by |
| 27 | /// name. |
| 28 | /// |
| 29 | struct SLUCache : public SourceLanguageCache { |
| 30 | ProgramInfo &PI; |
| 31 | std::multimap<std::string, SourceFunctionInfo*> FunctionMap; |
| 32 | public: |
| 33 | SLUCache(ProgramInfo &pi); |
| 34 | |
| 35 | typedef std::multimap<std::string, SourceFunctionInfo*>::const_iterator |
| 36 | fm_iterator; |
| 37 | |
| 38 | std::pair<fm_iterator, fm_iterator> |
| 39 | getFunction(const std::string &Name) const { |
| 40 | return FunctionMap.equal_range(Name); |
| 41 | } |
| 42 | |
| 43 | SourceFunctionInfo *addSourceFunction(SourceFunctionInfo *SF) { |
| 44 | FunctionMap.insert(std::make_pair(SF->getSymbolicName(), SF)); |
| 45 | return SF; |
| 46 | } |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | SLUCache::SLUCache(ProgramInfo &pi) : PI(pi) { |
| 51 | } |
| 52 | |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | // Implement SourceLanguageUnknown class, which is used to handle unrecognized |
| 56 | // languages. |
| 57 | // |
| 58 | |
| 59 | namespace { |
| 60 | struct SLU : public SourceLanguage { |
| 61 | //===------------------------------------------------------------------===// |
| 62 | // Implement the miscellaneous methods... |
| 63 | // |
| 64 | virtual const char *getSourceLanguageName() const { |
| 65 | return "unknown"; |
| 66 | } |
| 67 | |
| 68 | /// lookupFunction - Given a textual function name, return the |
| 69 | /// SourceFunctionInfo descriptor for that function, or null if it cannot be |
| 70 | /// found. If the program is currently running, the RuntimeInfo object |
| 71 | /// provides information about the current evaluation context, otherwise it |
| 72 | /// will be null. |
| 73 | /// |
| 74 | virtual SourceFunctionInfo *lookupFunction(const std::string &FunctionName, |
| 75 | ProgramInfo &PI, |
| 76 | RuntimeInfo *RI = 0) const; |
| 77 | |
| 78 | //===------------------------------------------------------------------===// |
| 79 | // We do use a cache for information... |
| 80 | // |
| 81 | typedef SLUCache CacheType; |
| 82 | SLUCache *createSourceLanguageCache(ProgramInfo &PI) const { |
| 83 | return new SLUCache(PI); |
| 84 | } |
| 85 | |
| 86 | /// createSourceFunctionInfo - Create the new object and inform the cache of |
| 87 | /// the new function. |
| 88 | virtual SourceFunctionInfo * |
| 89 | createSourceFunctionInfo(const GlobalVariable *Desc, ProgramInfo &PI) const; |
| 90 | |
| 91 | } TheUnknownSourceLanguageInstance; |
| 92 | } |
| 93 | |
| 94 | const SourceLanguage &SourceLanguage::getUnknownLanguageInstance() { |
| 95 | return TheUnknownSourceLanguageInstance; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | SourceFunctionInfo * |
| 100 | SLU::createSourceFunctionInfo(const GlobalVariable *Desc, |
| 101 | ProgramInfo &PI) const { |
| 102 | SourceFunctionInfo *Result = new SourceFunctionInfo(PI, Desc); |
| 103 | return PI.getLanguageCache(this).addSourceFunction(Result); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /// lookupFunction - Given a textual function name, return the |
| 108 | /// SourceFunctionInfo descriptor for that function, or null if it cannot be |
| 109 | /// found. If the program is currently running, the RuntimeInfo object |
| 110 | /// provides information about the current evaluation context, otherwise it will |
| 111 | /// be null. |
| 112 | /// |
| 113 | SourceFunctionInfo *SLU::lookupFunction(const std::string &FunctionName, |
| 114 | ProgramInfo &PI, RuntimeInfo *RI) const{ |
| 115 | SLUCache &Cache = PI.getLanguageCache(this); |
| 116 | std::pair<SLUCache::fm_iterator, SLUCache::fm_iterator> IP |
| 117 | = Cache.getFunction(FunctionName); |
| 118 | |
| 119 | if (IP.first == IP.second) { |
| 120 | if (PI.allSourceFunctionsRead()) |
| 121 | return 0; // Nothing found |
| 122 | |
| 123 | // Otherwise, we might be able to find the function if we read all of them |
| 124 | // in. Do so now. |
| 125 | PI.getSourceFunctions(); |
| 126 | assert(PI.allSourceFunctionsRead() && "Didn't read in all functions?"); |
| 127 | return lookupFunction(FunctionName, PI, RI); |
| 128 | } |
| 129 | |
| 130 | SourceFunctionInfo *Found = IP.first->second; |
| 131 | ++IP.first; |
| 132 | if (IP.first != IP.second) |
| 133 | std::cout << "Whoa, found multiple functions with the same name. I should" |
| 134 | << " ask the user which one to use: FIXME!\n"; |
| 135 | return Found; |
| 136 | } |