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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/*
* 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 __FunctionDef_hpp__
#define __FunctionDef_hpp__
#include "Array.hpp"
#include "Tree.hpp"
#include "DynLib.hpp"
#include "mex.h"
#include <QSharedData>
#include <QDateTime>
#include "Scope.hpp"
#include "JIT.hpp"
using namespace std;
typedef enum {
FM_M_FUNCTION,
FM_BUILT_IN_FUNCTION,
FM_SPECIAL_FUNCTION,
FM_IMPORTED_FUNCTION,
FM_MEX_FUNCTION
} FunctionType;
class Interpreter;
typedef ArrayVector (*BuiltInFuncPtr) (int,const ArrayVector&);
typedef ArrayVector (*SpecialFuncPtr) (int,const ArrayVector&,Interpreter*);
void VariableReferencesList(const Tree & t, StringVector& idents);
StringVector IdentifierList(const Tree & t);
/** Base class for the function types
* A FunctionDef class is a base class for the different types
* of function pointers used. There are three types of functions
* available:
* - M-functions - these are functions or scripts written by the
* user in the interpreted language.
* - Built-in functions - these are functions coded in C++ that
* implement functionality too difficult/impossible to do in
* the language itself.
* - Special functions - these are functions coded in C++ that
* require access to the internals of the interpreter object.
* All of these functions have in common a name, a script classification
* (is it a script or not), a well defined number of input arguments,
* a well defined number of output arguments, and some means of
* being evaluated.
*/
class FunctionDef {
public:
/**
* The name of the function - must follow identifier rules.
*/
QString name;
/**
* The reference count for this functiondef
*/
int refcount;
/**
* Is this a script?
*/
bool scriptFlag;
/**
* Is this a temporary function?
*/
bool temporaryFlag;
/**
* If a function requires access to the graphics subsystem,
* it must declare itself as such. Such functions are executed
* through the main thread.
*/
bool graphicsFunction;
/**
* The names of the arguments to the fuction (analogous to returnVals).
* Should have "varargin" as the last entry for variable argument
* functions.
*/
StringVector arguments;
/**
* The constructor.
*/
FunctionDef();
/**
* The virtual destructor
*/
virtual ~FunctionDef();
/**
* Increment the reference count
*/
void lock();
/**
* Decrement the reference count
*/
void unlock();
/**
* Returns true if the current function has references
*/
bool referenced();
/**
* The type of the function (FM_M_FUNCTION, FM_BUILT_IN_FUNCTION,
* FM_SPECIAL_FUNCTION, FM_IMPORTED_FUNCTION).
*/
virtual const FunctionType type() = 0;
/**
* Returns the name of the function
*/
virtual QString functionName() = 0;
/**
* Returns the "detailed" name of the function -- a full path for example
*/
virtual QString detailedName() = 0;
/**
* Print a description of the function
*/
virtual void printMe(Interpreter* io) = 0;
/**
* The number of inputs required by this function (-1 if variable).
*/
virtual int inputArgCount() = 0;
/**
* The number of outputs returned by this function (-1 if variable).
*/
virtual int outputArgCount() = 0;
/**
* Evaluate the function and return its output.
*/
virtual ArrayVector evaluateFunc(Interpreter *, ArrayVector& , int,
VariableTable * Workspace = 0) = 0;
/**
* Compile the function (if it is required). We guarantee that this
* function will be called at least once before evaluateFunction is called.
* Return true if the updateCode call did anything.
*/
virtual bool updateCode(Interpreter *) {return false;}
};
/**
* An MFunctionDef is a FunctionDef for an interpreted function. The
* function pointer stores the name of the file where the function is
* located, and a time stamp as to when the function was last modified.
* When the updateCode() member function is called, the contents of
* the file are parsed (if necessary), and the resulting AST code tree
* is stored. The number of input and output arguments are computed
* based on the contents of the returnVals and arguments StringVectors.
*/
class MFunctionDef : public FunctionDef {
public:
/**
* The names of the return values - this is a vector of strings with
* one entry for each return value in the declared function. Thus,
* if the function is declared as "function [a,b] = foo(x)", then
* returnVals contains two entries: "a", and "b". For variable
* return functions, the last entry should be "varargout".
*/
StringVector returnVals;
/**
* The AST for the code that defines the function (only the body of the
* function is contained in this AST, not the function declaration itself).
*/
Tree code;
/**
* Flag to indicate if the function has been compiled.
*/
bool functionCompiled;
/**
* Location of the function's defining file in the current filesystem.
*/
QString fileName;
/**
* Time function was last modified.
*/
QDateTime timeStamp;
/**
* Set to true for all of the localFunctions.
*/
bool localFunction;
bool pcodeFunction;
bool nestedFunction;
/**
* The help text.
*/
StringVector helpText;
/**
* The variable access list - used as a hinting mechanism to try and
* resolve scope rules for nested functions.
*/
StringVector variablesAccessed;
/**
* The constructor.
*/
MFunctionDef();
/**
* The destructor
*/
~MFunctionDef();
/** The type of the function
*/
virtual const FunctionType type() {return FM_M_FUNCTION;}
virtual QString functionName() {return fileName;}
virtual QString detailedName() {return name;}
/** Print a description of the function
*/
virtual void printMe(Interpreter* io);
/**
* The number of inputs required by this function, which is the number of
* elements in arguments unless the last element is the keyword "varargin"
* in which case the answer is -1.
*/
virtual int inputArgCount();
/**
* The number of outputs returned by this function, which is the number of
* elements in returnVals unless the last element is the keyword "varargout"
* in which case the answer is -1.
*/
virtual int outputArgCount();
/**
* Evaluate the function and return the outputs.
* Throws an Exception if
* - the special variable 'varargout' is not defined in the body of the
* of the function as promised by the function declaration.
* - the variable 'varargout' contains too few elements to satisfy the
* number of return values in the call
* - the variable 'varargout' is the wrong type.
*/
virtual ArrayVector evaluateFunc(Interpreter *, ArrayVector &, int, VariableTable *);
/**
* Check the timestamp on the file, and if necessary, recompile the
* function. Throws an exception if a syntax error occurs in the
* parsing of the file (i.e., it cannot be classified as either a
* script or a function definition).
*/
virtual bool updateCode(Interpreter *m_eval);
/**
* Find the line number closest to the requested one
*/
unsigned ClosestLine(unsigned line);
};
class BuiltInFunctionDef : public FunctionDef {
public:
/** The number of return args for this function (-1 for variable).
*/
int retCount;
/** The number of input args for this function (-1 for variable).
*/
int argCount;
/**
* The pointer to (address of) the function.
*/
BuiltInFuncPtr fptr;
/**
* Default constructor.
*/
BuiltInFunctionDef();
/**
* Default destructor.
*/
~BuiltInFunctionDef();
/**
* The type of the function is FM_BUILT_IN_FUNCTION.
*/
virtual const FunctionType type() {return FM_BUILT_IN_FUNCTION;}
virtual QString functionName() {return name;}
virtual QString detailedName() {return "builtin";}
/** Print a description of the function
*/
virtual void printMe(Interpreter *io);
/**
* The number of inputs required by this function.
*/
virtual int inputArgCount();
/**
* The number of outputs returned by this function.
*/
virtual int outputArgCount();
/**
* Evaluate the function and return the values.
*/
virtual ArrayVector evaluateFunc(Interpreter *, ArrayVector &, int, VariableTable *);
};
class SpecialFunctionDef : public FunctionDef {
public:
/**
* The number of return args for this function (-1 for variable).
*/
int retCount;
/** The number of input args for this function (-1 for variable).
*/
int argCount;
/**
* The pointer to (address of) the function.
*/
SpecialFuncPtr fptr;
/**
* Default constructor.
*/
SpecialFunctionDef();
/**
* Default destructor.
*/
~SpecialFunctionDef();
/**
* The type of the function is FM_SPECIAL_FUNCTION.
*/
virtual const FunctionType type() {return FM_SPECIAL_FUNCTION;}
virtual QString functionName() {return name;}
virtual QString detailedName() {return "builtin";}
/** Print a description of the function
*/
virtual void printMe(Interpreter *);
/**
* The number of inputs required by this function.
*/
virtual int inputArgCount() {return argCount;}
/**
* The number of outputs returned by this function.
*/
virtual int outputArgCount() {return retCount;}
/**
* Evaluate the function and return the values.
*/
virtual ArrayVector evaluateFunc(Interpreter *, ArrayVector& , int, VariableTable *);
};
typedef void (*GenericFuncPointer)();
class ImportedFunctionDef : public FunctionDef {
public:
/**
* The number of return args for this function (either 0 or 1).
*/
int retCount;
/**
* The number of input args for this function (cannot be variable).
*/
int argCount;
/**
* The pointer to the function to be called.
*/
GenericFuncPointer address;
/**
* The types of each argument
*/
StringVector types;
/**
* The guard expressions associated with each argument
*/
TreeList sizeCheckExpressions;
/**
* The return type of the function
*/
QString retType;
#ifdef HAVE_LLVM
/**
* The stub function that does the foreign function call work.
*/
JITFunction fcnStub;
#endif
/**
* Default constructor
*/
ImportedFunctionDef(GenericFuncPointer address_arg,
StringVector types_arg,
StringVector arguments_arg,
TreeList sizeChecks,
QString retType_arg,
QString name);
/**
* Default destructor
*/
~ImportedFunctionDef();
/**
* The type of the function is FM_IMPORTED_FUNCTION.
*/
virtual const FunctionType type() {return FM_IMPORTED_FUNCTION;}
virtual QString functionName() {return name;}
virtual QString detailedName() {return "imported";}
/** Print a description of the function
*/
virtual void printMe(Interpreter *);
/**
* The number of inputs required by this function.
*/
virtual int inputArgCount() {return argCount;}
/**
* The number of outputs returned by this function.
*/
virtual int outputArgCount() {return retCount;}
/**
* Evaluate the function and return the values.
*/
virtual ArrayVector evaluateFunc(Interpreter *, ArrayVector& , int, VariableTable *);
/**
* Helper function -- is the ith argument passed as a pointer
*/
bool isPassedAsPointer(int arg);
};
typedef void (*mexFuncPtr)(int, mxArray**, int, const mxArray**);
class MexFunctionDef : public FunctionDef {
public:
/**
* The full name of the library to link to
*/
QString fullname;
/**
* The dynamic library object
*/
DynLib *lib;
/**
* The following flag is set to true if the library is
* successfully imported
*/
bool importSuccess;
/**
* The pointer to the function to be called.
*/
mexFuncPtr address;
/**
* Default constructor
*/
MexFunctionDef(QString fullpathname);
/**
* Default destructor
*/
~MexFunctionDef();
bool LoadSuccessful();
/**
* The type of the function is FM_MEX_FUNCTION.
*/
virtual const FunctionType type() {return FM_MEX_FUNCTION;}
virtual QString functionName() {return name;}
virtual QString detailedName() {return "mex";}
/** Print a description of the function
*/
virtual void printMe(Interpreter *);
/**
* The number of inputs required by this function.
*/
virtual int inputArgCount() {return -1;}
/**
* The number of outputs returned by this function.
*/
virtual int outputArgCount() {return -1;}
/**
* Evaluate the function and return the values.
*/
virtual ArrayVector evaluateFunc(Interpreter *, ArrayVector& , int, VariableTable*);
};
// This used to be a simple typedef to a pointer of a functiondef
// Now, it is a reference counted class.
class FuncPtr {
private:
FunctionDef* d;
public:
FuncPtr() : d(NULL) {}
~FuncPtr() {
if (d) {
d->unlock();
if (!d->referenced()) delete d;
}
}
FuncPtr(SpecialFunctionDef* ptr) {
d = ptr;
if (d)
d->lock();
}
FuncPtr(BuiltInFunctionDef* ptr) {
d = ptr;
if (d)
d->lock();
}
FuncPtr(FunctionDef* ptr) {
d = ptr;
if (d)
d->lock();
}
FuncPtr(const FuncPtr ©) {
d = copy.d;
if (d)
d->lock();
}
FuncPtr& operator=(const FuncPtr ©) {
if (copy.d == d)
return *this;
if (d) {
d->unlock();
if (!d->referenced()) delete d;
}
d = copy.d;
if (d)
d->lock();
return *this;
}
FunctionDef* operator->() const {
return d;
}
FunctionDef& operator*() const {
return *d;
}
bool operator!() const {
return (d == NULL);
}
operator FunctionDef* () const {return d;}
operator MFunctionDef* () const {return ((MFunctionDef*)d);}
};
#endif
|