1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/*
* Copyright (c) 2002-2006 Samit Basu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __Scope_hpp__
#define __Scope_hpp__
/**
* A Scope is a combination of a variable hashtable and a function hashtable.
*/
#include <string>
#include <algorithm>
#include <QMutex>
#include <QHash>
#include <algorithm>
#include "Array.hpp"
#include "Types.hpp"
#include "SymbolTable.hpp"
typedef FM::SymbolTable<Array> VariableTable;
/**
* A Scope is a collection of functions and variables all visible
* at some point in the execution. The scope also keeps track of
* the loop level, and a list of the global and persistent variables
* relevant to the current scope.
*/
class Scope {
/**
* This is a mutex to protect the scope when multiple threads
* have access to the scope. For all scopes (except the global
* one, this mutex is unused.
*/
QMutex *mutex;
/**
* The reference count for this Scope
*/
int refcount;
/**
* This is the hash-table of Array pointers that forms the
* symbol table. Each variable has a name associated with
* it that must be unique to the Scope. The Scope owns the
* variables in the symbol table, and is responsible for
* destructing them when destroyed.
*/
VariableTable symTab;
/**
* The name of the scope.
*/
QString name;
/**
* Detailed information that indicates where the scope is from.
*/
QString detail;
/**
* The context information (currently executed token ID)
*/
int tokid;
/**
* The line number for the current step trap (from dbstep)
*/
int steptrap;
/**
* The current line number from which we are stepping
*/
int stepcurrentline;
/**
* Flag that indicates if this scope is active (if it is actually
* used to store local variables -- for scripts, the corresponding
* scope is not active).
*/
bool active;
/**
* The loop level. This is used to track the depth of nested
* loops.
*/
int loopLevel;
/**
* These are the global variables as defined in the current
* scope. Global variables are not stored in this Scope, but
* are deferred to the top scope in the Context.
*/
StringVector globalVars;
/**
* Persistent variables are similar to global variables in that
* they are deferred to the top scope in the Context. However,
* unlike global variables, persistent variables are mangled
* with the name of the scope before being indexed into the global
* scope.
*/
StringVector persistentVars;
/**
* This string vector contains the names of variables accessed (potentially)
* in this scope.
*/
StringVector variablesAccessed;
/**
* This string vector contains the names of variables that must be local to this
* scope.
*/
StringVector localVariables;
/**
* On every call to modify the scope, we have to check the global/persistent
* variable table. This is generally expensive, so we cache information
* about these tables being empty (the usual case).
*/
bool anyPersistents;
bool anyGlobals;
bool isNested;
/**
* The number of input/output arguments - used by M-functions that need to know
*/
int numargin;
int numargout;
public:
/**
* Construct a scope with the given name.
*/
Scope(QString scopeName, bool nested) : mutex(NULL), refcount(0),
name(scopeName), tokid(0), steptrap(0),
stepcurrentline(0), active(true),
loopLevel(0), anyPersistents(false), anyGlobals(false),
isNested(nested)
{}
inline bool isActive() const {
return active;
}
inline void setActive(bool x) {
active = x;
}
inline int stepTrap() const {
return steptrap;
}
inline void setStepTrap(int x) {
steptrap = x;
}
inline int stepCurrentLine() const {
return stepcurrentline;
}
inline void setStepCurrentLine(int x) {
stepcurrentline = x;
}
inline int tokenID() const {
return tokid;
}
inline void setTokenID(int x) {
tokid = x;
}
inline int nargin() const {
return numargin;
}
inline void setNargin(int x) {
numargin = x;
}
inline int nargout() const {
return numargout;
}
inline void setNargout(int x) {
numargout = x;
}
inline QString detailString() {
return detail;
}
inline void setDetailString(QString x) {
detail = x;
}
inline void setVariablesAccessed(StringVector varList) {
variablesAccessed = varList;
}
inline bool variableAccessed(QString varName) {
for (int i=0;i<variablesAccessed.size();i++)
if (variablesAccessed[i] == varName) return true;
return false;
}
inline StringVector getVariablesAccessedList() {
return variablesAccessed;
}
inline void setLocalVariables(StringVector varList) {
localVariables = varList;
}
inline StringVector getLocalVariablesList() {
return localVariables;
}
inline bool variableLocal(QString varName) {
for (int i=0;i<localVariables.size();i++)
if (localVariables[i] == varName) return true;
return false;
}
inline bool isnested() {
return isNested;
}
inline void setNestingFlag(bool x) {
isNested = x;
}
inline bool nests(QString peerName) {
// Nesting requires that our peer have a strictly more
// qualified (longer) name, and that our name is a prefix
// of that name.
return ((name.size() < peerName.size()) &&
(peerName.left(name.size()) == name));
}
static bool nests(QString name, QString peerName) {
// Nesting requires that our peer have a strictly more
// qualified (longer) name, and that our name is a prefix
// of that name.
return ((name.size() < peerName.size()) &&
(peerName.left(name.size()) == name));
}
/**
* Lock the scope's mutex
*/
inline void lock() {
if (mutex) mutex->lock();
}
/**
* Unlock the scope's mutex
*/
inline void unlock() {
if (mutex) mutex->unlock();
}
/**
* Initialize the scope's mutex - only needed in Global
* scope.
*/
inline void mutexSetup() {
if (mutex)
delete mutex;
mutex = new QMutex(QMutex::Recursive);
}
/**
* Insert a variable with the given name. If the variable
* already exists in the Scope, then the previous definition
* is replaced with the given one.
*/
inline void insertVariable(const QString& varName, const Array& val) {
symTab.insertSymbol(varName,val);
}
/**
* Lookup a variable. Return a pointer to the variable in the symbol
* table if found and NULL otherwise. Different than lookupFunction
* because in write-back assignments (e.g., A(:,346) = b) it is critical
* to manage the number of copies.
*/
inline Array* lookupVariable(const QString& varName) {
return symTab.findSymbol(varName);
}
/**
* Add a variable name to the global variables list.
*/
inline void addGlobalVariablePointer(QString varName) {
if (!isVariableGlobal(varName)) {
globalVars.push_back(varName);
anyGlobals = true;
}
}
inline StringVector getGlobalVariablesList() {
return globalVars;
}
/**
* Delete a variable name from the global variables list.
*/
inline void deleteGlobalVariablePointer(QString varName) {
StringVector::iterator i = std::find(globalVars.begin(),
globalVars.end(),
varName);
if (*i == varName)
globalVars.erase(i);
if (globalVars.empty()) anyGlobals = false;
}
/**
* Check to see if a variable is globally defined.
*/
inline bool isVariableGlobal(const QString& varName) {
if (!anyGlobals) return false;
bool foundName = false;
int i = 0;
if (globalVars.empty()) return false;
while (i<globalVars.size() && !foundName) {
foundName = (globalVars[i] == varName);
if (!foundName) i++;
}
return foundName;
}
/**
* Add a variable name to the persistent variables list.
*/
inline void addPersistentVariablePointer(QString varName) {
if (!isVariablePersistent(varName)) {
persistentVars.push_back(varName);
anyPersistents = true;
}
}
inline StringVector getPersistentVariablesList() {
return persistentVars;
}
/**
* Delete a variable name from the persistent variables list.
*/
inline void deletePersistentVariablePointer(QString varName) {
StringVector::iterator i = std::find(persistentVars.begin(),
persistentVars.end(),
varName);
if (*i == varName)
persistentVars.erase(i);
if (persistentVars.empty()) anyPersistents = false;
}
/**
* Check to see if a variable is defined in the persistent
* list.
*/
inline bool isVariablePersistent(const QString& varName) {
if (!anyPersistents) return false;
bool foundName = false;
int i = 0;
if (persistentVars.empty()) return false;
while (i<persistentVars.size() && !foundName) {
foundName = (persistentVars[i] == varName);
if (!foundName) i++;
}
return foundName;
}
/**
* Mangle the name of a variable by prepending
* a "_scopename_" to the name of the variable.
*/
inline QString getMangledName(QString varName) {
return (QString("_") + name + QString("_") + varName);
}
/**
* Get the name of the scope.
*/
inline QString getName() {
return name;
}
/**
* Increment the loop counter.
*/
inline void enterLoop() {
loopLevel++;
}
/**
* Decrement the loop counter.
*/
inline void exitLoop() {
loopLevel--;
}
/**
* Test the loop counter.
*/
inline bool inLoop() {
return (loopLevel>0);
}
/**
* Get a list of all possible completions of the given
* string.
*/
inline StringVector getCompletions(const QString& prefix) {
return symTab.getCompletions(prefix);
}
/**
* Returns a list of all currently defined variables
* in the active scope.
*/
inline StringVector listAllVariables() {
StringVector names(symTab.getCompletions(""));
for (int i=0;i<globalVars.size();i++)
names.push_back(globalVars[i]);
for (int i=0;i<persistentVars.size();i++)
names.push_back(persistentVars[i]);
return names;
}
/**
* Delete a variable in this scope. It does not simply
* replace the variable with an empty variable, but deletes
* the variable from the symbol table completely.
*/
inline void deleteVariable(QString var) {
symTab.deleteSymbol(var);
}
/**
* Clear the list of global variable names
*/
inline void clearGlobalVariableList() {
globalVars.clear();
anyGlobals = false;
}
/**
* Clear the list of persistent variable names
*/
inline void clearPersistentVariableList() {
persistentVars.clear();
anyPersistents = false;
}
};
typedef Scope* ScopePtr;
#endif
|