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