Deconstify parameter to getPointerToFunction().
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Support.cpp
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"
10 #include "llvm/Module.h"
11
12 //===----------------------------------------------------------------------===//
13 //
14 // LookupMatchingNames helper - Search a symbol table for values matching Name.
15 //
16 static inline void LookupMatchingNames(const std::string &Name,
17                                        SymbolTable &SymTab,
18                                        std::vector<Value*> &Results) {
19   // Loop over all of the type planes in the symbol table...
20   for (SymbolTable::iterator I = SymTab.begin(), E = SymTab.end(); I != E; ++I){
21     SymbolTable::VarMap &Plane = I->second;
22     
23     // Search the symbol table plane for this name...
24     SymbolTable::VarMap::iterator Val = Plane.find(Name);
25     if (Val != Plane.end())
26       Results.push_back(Val->second);                    // Found a name match!
27   }
28 }
29
30 // LookupMatchingNames - Search the current function namespace, then the global
31 // namespace looking for values that match the specified name.  Return ALL
32 // matches to that name.  This is obviously slow, and should only be used for
33 // user interaction.
34 //
35 std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
36   std::vector<Value*> Results;
37   Function *CurFunc = getCurrentFunction();
38   
39   if (CurFunc) ::LookupMatchingNames(Name, CurFunc->getSymbolTable(), Results);
40   ::LookupMatchingNames(Name, getModule().getSymbolTable(), Results);
41   return Results;
42 }
43
44 // ChooseOneOption - Prompt the user to choose among the specified options to
45 // pick one value.  If no options are provided, emit an error.  If a single 
46 // option is provided, just return that option.
47 //
48 Value *Interpreter::ChooseOneOption(const std::string &Name,
49                                     const std::vector<Value*> &Opts) {
50   switch (Opts.size()) {
51   case 1: return Opts[0];
52   case 0: 
53     std::cout << "Error: no entities named '" << Name << "' found!\n";
54     return 0;
55   default: break;  // Must prompt user...
56   }
57
58   std::cout << "Multiple entities named '" << Name
59             << "' found!  Please choose:\n";
60   std::cout << "  0. Cancel operation\n";
61   for (unsigned i = 0; i < Opts.size(); ++i) {
62     std::cout << "  " << (i+1) << ".";
63     WriteAsOperand(std::cout, Opts[i]) << "\n";
64   }
65
66   unsigned Option;
67   do {
68     std::cout << "lli> " << std::flush;
69     std::cin >> Option;
70     if (Option > Opts.size())
71       std::cout << "Invalid selection: Please choose from 0 to " << Opts.size()
72                 << "\n";
73   } while (Option > Opts.size());
74
75   if (Option == 0) return 0;
76   return Opts[Option-1];
77 }