blob: 4c31d2692ad5299d8c694a1d8e8c28c21dc631e3 [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 Lattner2312b1c2002-04-28 04:55:14 +000017static inline void LookupMatchingNames(const std::string &Name,
Chris Lattner6e6026b2002-11-20 18:36:02 +000018 SymbolTable &SymTab,
Chris Lattner697954c2002-01-20 22:54:45 +000019 std::vector<Value*> &Results) {
Chris Lattner92101ac2001-08-23 17:05:04 +000020 // Loop over all of the type planes in the symbol table...
Chris Lattner6e6026b2002-11-20 18:36:02 +000021 for (SymbolTable::iterator I = SymTab.begin(), E = SymTab.end(); I != E; ++I){
Chris Lattner92101ac2001-08-23 17:05:04 +000022 SymbolTable::VarMap &Plane = I->second;
23
24 // Search the symbol table plane for this name...
25 SymbolTable::VarMap::iterator Val = Plane.find(Name);
26 if (Val != Plane.end())
27 Results.push_back(Val->second); // Found a name match!
28 }
29}
30
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000031// LookupMatchingNames - Search the current function namespace, then the global
Chris Lattner92101ac2001-08-23 17:05:04 +000032// namespace looking for values that match the specified name. Return ALL
33// matches to that name. This is obviously slow, and should only be used for
34// user interaction.
35//
Chris Lattner697954c2002-01-20 22:54:45 +000036std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
37 std::vector<Value*> Results;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000038 Function *CurMeth = getCurrentMethod();
Chris Lattner92101ac2001-08-23 17:05:04 +000039
Chris Lattner2312b1c2002-04-28 04:55:14 +000040 if (CurMeth) ::LookupMatchingNames(Name, CurMeth->getSymbolTable(), Results);
41 if (CurMod ) ::LookupMatchingNames(Name, CurMod ->getSymbolTable(), Results);
Chris Lattner92101ac2001-08-23 17:05:04 +000042 return Results;
43}
44
45// ChooseOneOption - Prompt the user to choose among the specified options to
46// pick one value. If no options are provided, emit an error. If a single
47// option is provided, just return that option.
48//
Chris Lattner697954c2002-01-20 22:54:45 +000049Value *Interpreter::ChooseOneOption(const std::string &Name,
50 const std::vector<Value*> &Opts) {
Chris Lattner92101ac2001-08-23 17:05:04 +000051 switch (Opts.size()) {
52 case 1: return Opts[0];
53 case 0:
54 cout << "Error: no entities named '" << Name << "' found!\n";
55 return 0;
56 default: break; // Must prompt user...
57 }
58
59 cout << "Multiple entities named '" << Name << "' found! Please choose:\n";
60 cout << " 0. Cancel operation\n";
61 for (unsigned i = 0; i < Opts.size(); ++i) {
62 cout << " " << (i+1) << ".";
Chris Lattner697954c2002-01-20 22:54:45 +000063 WriteAsOperand(cout, Opts[i]) << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +000064 }
65
66 unsigned Option;
67 do {
Chris Lattner697954c2002-01-20 22:54:45 +000068 cout << "lli> " << std::flush;
69 std::cin >> Option;
Chris Lattner92101ac2001-08-23 17:05:04 +000070 if (Option > Opts.size())
71 cout << "Invalid selection: Please choose from 0 to " << Opts.size()
Chris Lattner697954c2002-01-20 22:54:45 +000072 << "\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}