blob: ca89ae33332cc061126fb58dac0cc746eeea54f7 [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 Lattner697954c2002-01-20 22:54:45 +000010#include <iostream>
11using std::cout;
Chris Lattner92101ac2001-08-23 17:05:04 +000012
13//===----------------------------------------------------------------------===//
14//
15// LookupMatchingNames helper - Search a symbol table for values matching Name.
16//
Chris Lattner697954c2002-01-20 22:54:45 +000017static inline void LookupMatchingNames(const std::string &Name,SymTabValue &STV,
18 std::vector<Value*> &Results) {
Chris Lattner92101ac2001-08-23 17:05:04 +000019 SymbolTable *SymTab = STV.getSymbolTable();
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
34// LookupMatchingNames - Search the current method 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
37// user interaction.
38//
Chris Lattner697954c2002-01-20 22:54:45 +000039std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
40 std::vector<Value*> Results;
Chris Lattner92101ac2001-08-23 17:05:04 +000041 Method *CurMeth = getCurrentMethod();
42
43 if (CurMeth) ::LookupMatchingNames(Name, *CurMeth, Results);
44 if (CurMod ) ::LookupMatchingNames(Name, *CurMod , Results);
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 Lattner697954c2002-01-20 22:54:45 +000052Value *Interpreter::ChooseOneOption(const std::string &Name,
53 const std::vector<Value*> &Opts) {
Chris Lattner92101ac2001-08-23 17:05:04 +000054 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 Lattner697954c2002-01-20 22:54:45 +000066 WriteAsOperand(cout, Opts[i]) << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000067 }
68
69 unsigned Option;
70 do {
Chris Lattner697954c2002-01-20 22:54:45 +000071 cout << "lli> " << std::flush;
72 std::cin >> Option;
Chris Lattner92101ac2001-08-23 17:05:04 +000073 if (Option > Opts.size())
74 cout << "Invalid selection: Please choose from 0 to " << Opts.size()
Chris Lattner697954c2002-01-20 22:54:45 +000075 << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000076 } while (Option > Opts.size());
77
78 if (Option == 0) return 0;
79 return Opts[Option-1];
80}