1 //===-- Support.cpp - Support routines for interpreter --------------------===//
3 // This file contains support routines for the interpreter core.
5 //===----------------------------------------------------------------------===//
7 #include "Interpreter.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/Assembly/Writer.h"
13 //===----------------------------------------------------------------------===//
15 // LookupMatchingNames helper - Search a symbol table for values matching Name.
17 static inline void LookupMatchingNames(const std::string &Name,SymTabValue &STV,
18 std::vector<Value*> &Results) {
19 SymbolTable *SymTab = STV.getSymbolTable();
20 if (SymTab == 0) return; // No symbolic values :(
22 // Loop over all of the type planes in the symbol table...
23 for (SymbolTable::iterator I = SymTab->begin(), E = SymTab->end();
25 SymbolTable::VarMap &Plane = I->second;
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!
34 // LookupMatchingNames - Search the current function namespace, then the global
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
39 std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
40 std::vector<Value*> Results;
41 Function *CurMeth = getCurrentMethod();
43 if (CurMeth) ::LookupMatchingNames(Name, *CurMeth, Results);
44 if (CurMod ) ::LookupMatchingNames(Name, *CurMod , Results);
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.
52 Value *Interpreter::ChooseOneOption(const std::string &Name,
53 const std::vector<Value*> &Opts) {
54 switch (Opts.size()) {
55 case 1: return Opts[0];
57 cout << "Error: no entities named '" << Name << "' found!\n";
59 default: break; // Must prompt user...
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) << ".";
66 WriteAsOperand(cout, Opts[i]) << "\n";
71 cout << "lli> " << std::flush;
73 if (Option > Opts.size())
74 cout << "Invalid selection: Please choose from 0 to " << Opts.size()
76 } while (Option > Opts.size());
78 if (Option == 0) return 0;
79 return Opts[Option-1];