Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- Support.cpp - Support routines for interpreter --------------------===// |
| 2 | // |
| 3 | // This file contains support routines for the interpreter core. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "Interpreter.h" |
| 8 | #include "llvm/SymbolTable.h" |
| 9 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 10 | #include <iostream> |
| 11 | using std::cout; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 12 | |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | // |
| 15 | // LookupMatchingNames helper - Search a symbol table for values matching Name. |
| 16 | // |
Chris Lattner | 2312b1c | 2002-04-28 04:55:14 +0000 | [diff] [blame] | 17 | static inline void LookupMatchingNames(const std::string &Name, |
| 18 | SymbolTable *SymTab, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 19 | std::vector<Value*> &Results) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 20 | if (SymTab == 0) return; // No symbolic values :( |
| 21 | |
| 22 | // Loop over all of the type planes in the symbol table... |
| 23 | for (SymbolTable::iterator I = SymTab->begin(), E = SymTab->end(); |
| 24 | I != E; ++I) { |
| 25 | SymbolTable::VarMap &Plane = I->second; |
| 26 | |
| 27 | // Search the symbol table plane for this name... |
| 28 | SymbolTable::VarMap::iterator Val = Plane.find(Name); |
| 29 | if (Val != Plane.end()) |
| 30 | Results.push_back(Val->second); // Found a name match! |
| 31 | } |
| 32 | } |
| 33 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 34 | // LookupMatchingNames - Search the current function namespace, then the global |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 35 | // namespace looking for values that match the specified name. Return ALL |
| 36 | // matches to that name. This is obviously slow, and should only be used for |
| 37 | // user interaction. |
| 38 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 39 | std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) { |
| 40 | std::vector<Value*> Results; |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 41 | Function *CurMeth = getCurrentMethod(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 2312b1c | 2002-04-28 04:55:14 +0000 | [diff] [blame] | 43 | if (CurMeth) ::LookupMatchingNames(Name, CurMeth->getSymbolTable(), Results); |
| 44 | if (CurMod ) ::LookupMatchingNames(Name, CurMod ->getSymbolTable(), Results); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 45 | return Results; |
| 46 | } |
| 47 | |
| 48 | // ChooseOneOption - Prompt the user to choose among the specified options to |
| 49 | // pick one value. If no options are provided, emit an error. If a single |
| 50 | // option is provided, just return that option. |
| 51 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 52 | Value *Interpreter::ChooseOneOption(const std::string &Name, |
| 53 | const std::vector<Value*> &Opts) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 54 | switch (Opts.size()) { |
| 55 | case 1: return Opts[0]; |
| 56 | case 0: |
| 57 | cout << "Error: no entities named '" << Name << "' found!\n"; |
| 58 | return 0; |
| 59 | default: break; // Must prompt user... |
| 60 | } |
| 61 | |
| 62 | cout << "Multiple entities named '" << Name << "' found! Please choose:\n"; |
| 63 | cout << " 0. Cancel operation\n"; |
| 64 | for (unsigned i = 0; i < Opts.size(); ++i) { |
| 65 | cout << " " << (i+1) << "."; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 66 | WriteAsOperand(cout, Opts[i]) << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | unsigned Option; |
| 70 | do { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 71 | cout << "lli> " << std::flush; |
| 72 | std::cin >> Option; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 73 | if (Option > Opts.size()) |
| 74 | cout << "Invalid selection: Please choose from 0 to " << Opts.size() |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 75 | << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 76 | } while (Option > Opts.size()); |
| 77 | |
| 78 | if (Option == 0) return 0; |
| 79 | return Opts[Option-1]; |
| 80 | } |