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