blob: d857b664f9f338834385e5677c7346b22438736c [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- 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 Lattnerfe11a972002-12-23 23:59:41 +000010#include "llvm/Module.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000011
12//===----------------------------------------------------------------------===//
13//
14// LookupMatchingNames helper - Search a symbol table for values matching Name.
15//
Chris Lattner2312b1c2002-04-28 04:55:14 +000016static inline void LookupMatchingNames(const std::string &Name,
Chris Lattner6e6026b2002-11-20 18:36:02 +000017 SymbolTable &SymTab,
Chris Lattner697954c2002-01-20 22:54:45 +000018 std::vector<Value*> &Results) {
Chris Lattner92101ac2001-08-23 17:05:04 +000019 // Loop over all of the type planes in the symbol table...
Chris Lattner6e6026b2002-11-20 18:36:02 +000020 for (SymbolTable::iterator I = SymTab.begin(), E = SymTab.end(); I != E; ++I){
Chris Lattner92101ac2001-08-23 17:05:04 +000021 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000030// LookupMatchingNames - Search the current function namespace, then the global
Chris Lattner92101ac2001-08-23 17:05:04 +000031// 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//
Chris Lattner697954c2002-01-20 22:54:45 +000035std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
36 std::vector<Value*> Results;
Chris Lattnerda82ed52003-05-08 16:18:31 +000037 Function *CurFunc = getCurrentFunction();
Chris Lattner92101ac2001-08-23 17:05:04 +000038
Chris Lattnerda82ed52003-05-08 16:18:31 +000039 if (CurFunc) ::LookupMatchingNames(Name, CurFunc->getSymbolTable(), Results);
Chris Lattnerfe11a972002-12-23 23:59:41 +000040 ::LookupMatchingNames(Name, getModule().getSymbolTable(), Results);
Chris Lattner92101ac2001-08-23 17:05:04 +000041 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//
Chris Lattner697954c2002-01-20 22:54:45 +000048Value *Interpreter::ChooseOneOption(const std::string &Name,
49 const std::vector<Value*> &Opts) {
Chris Lattner92101ac2001-08-23 17:05:04 +000050 switch (Opts.size()) {
51 case 1: return Opts[0];
52 case 0:
Chris Lattnerda82ed52003-05-08 16:18:31 +000053 std::cout << "Error: no entities named '" << Name << "' found!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000054 return 0;
55 default: break; // Must prompt user...
56 }
57
Chris Lattnerda82ed52003-05-08 16:18:31 +000058 std::cout << "Multiple entities named '" << Name
59 << "' found! Please choose:\n";
60 std::cout << " 0. Cancel operation\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000061 for (unsigned i = 0; i < Opts.size(); ++i) {
Chris Lattnerda82ed52003-05-08 16:18:31 +000062 std::cout << " " << (i+1) << ".";
63 WriteAsOperand(std::cout, Opts[i]) << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000064 }
65
66 unsigned Option;
67 do {
Chris Lattnerda82ed52003-05-08 16:18:31 +000068 std::cout << "lli> " << std::flush;
Chris Lattner697954c2002-01-20 22:54:45 +000069 std::cin >> Option;
Chris Lattner92101ac2001-08-23 17:05:04 +000070 if (Option > Opts.size())
Chris Lattnerda82ed52003-05-08 16:18:31 +000071 std::cout << "Invalid selection: Please choose from 0 to " << Opts.size()
72 << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000073 } while (Option > Opts.size());
74
75 if (Option == 0) return 0;
76 return Opts[Option-1];
77}