|
Qore Programming Language
0.8.3
|
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */ 00002 /* 00003 QoreProgram.h 00004 00005 Program Object Definition 00006 00007 Qore Programming Language 00008 00009 Copyright 2003 - 2011 David Nichols 00010 00011 This library is free software; you can redistribute it and/or 00012 modify it under the terms of the GNU Lesser General Public 00013 License as published by the Free Software Foundation; either 00014 version 2.1 of the License, or (at your option) any later version. 00015 00016 This library is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 Lesser General Public License for more details. 00020 00021 You should have received a copy of the GNU Lesser General Public 00022 License along with this library; if not, write to the Free Software 00023 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 */ 00025 00026 #ifndef _QORE_QOREPROGRAM_H 00027 00028 #define _QORE_QOREPROGRAM_H 00029 00030 #include <qore/AbstractPrivateData.h> 00031 #include <qore/Restrictions.h> 00032 00033 // warnings - must correspond with the string order in QoreProgram.cpp 00034 // new warnings must also be added as constants 00035 #define QP_WARN_NONE 0 00036 #define QP_WARN_WARNING_MASK_UNCHANGED (1 << 0) //!< when the warning mask is attempted to be changed but it's locked 00037 #define QP_WARN_DUPLICATE_LOCAL_VARS (1 << 1) //!< duplicate local variable name reachable in the same scope 00038 #define QP_WARN_UNKNOWN_WARNING (1 << 2) //!< when an unknown warning is encountered 00039 #define QP_WARN_UNDECLARED_VAR (1 << 3) //!< when a variable is not declared 00040 #define QP_WARN_DUPLICATE_GLOBAL_VARS (1 << 4) //!< when a global variable is declared more than once 00041 #define QP_WARN_UNREACHABLE_CODE (1 << 5) //!< when unreachable code is encountered 00042 #define QP_WARN_NONEXISTENT_METHOD_CALL (1 << 6) //!< when a non-existent method call is encountered 00043 #define QP_WARN_INVALID_OPERATION (1 << 7) //!< when an expression always returns NOTHING, for example 00044 #define QP_WARN_CALL_WITH_TYPE_ERRORS (1 << 8) //!< when a function or method call always returns a fixed value due to type errors 00045 #define QP_WARN_RETURN_VALUE_IGNORED (1 << 9) //!< when a function or method call has no side effects and the return value is ignored 00046 #define QP_WARN_DEPRECATED (1 << 10) //!< when depcrecated functionality is accessed 00047 #define QP_WARN_EXCESS_ARGS (1 << 11) //!< when excess arguments are given to a function that does not access them 00048 #define QP_WARN_DUPLICATE_HASH_KEY (1 << 12) //!< when a hash key has been defined more than once in a literal hash 00049 #define QP_WARN_UNREFERENCED_VARIABLE (1 << 13) //!< when a variable is declared but not referenced 00050 #define QP_WARN_DUPLICATE_BLOCK_VARS (1 << 14) //!< when a variable is declared more than once in the same block; this would normally be an error, but for backwards-compatibility it's just a warning unless parse option 'assume-local' is set, in which case it's an error 00051 #define QP_WARN_ALL -1 //!< for all possible warnings 00052 00053 #define QP_WARN_DEFAULT (QP_WARN_UNKNOWN_WARNING|QP_WARN_UNREACHABLE_CODE|QP_WARN_NONEXISTENT_METHOD_CALL|QP_WARN_INVALID_OPERATION|QP_WARN_CALL_WITH_TYPE_ERRORS|QP_WARN_RETURN_VALUE_IGNORED|QP_WARN_DEPRECATED|QP_WARN_DUPLICATE_HASH_KEY|QP_WARN_DUPLICATE_BLOCK_VARS) 00054 00056 DLLEXPORT extern const char **qore_warnings; 00057 00059 DLLEXPORT extern unsigned qore_num_warnings; 00060 00062 DLLEXPORT int get_warning_code(const char *str); 00063 00064 class AbstractCallReferenceNode; 00065 class LocalVar; 00066 class ExceptionSink; 00067 class QoreListNode; 00068 class QoreNamespace; 00069 class RootQoreNamespace; 00070 class QoreStringNode; 00071 class QoreHashNode; 00072 class FunctionCallNode; 00073 class AbstractStatement; 00074 class UnresolvedProgramCallReferenceNode; 00075 class Var; 00076 class LVList; 00077 class UserFunction; 00078 class UserFunctionVariant; 00079 class QoreTypeInfo; 00080 class QoreParseTypeInfo; 00081 class ParamList; 00082 class AbstractQoreFunction; 00083 class AbstractQoreZoneInfo; 00084 class qore_program_private; 00085 00087 00094 class QoreProgram : public AbstractPrivateData { 00095 friend class qore_program_private_base; 00096 friend class qore_program_private; 00097 private: 00099 qore_program_private *priv; 00100 00102 DLLLOCAL QoreProgram(const QoreProgram&); 00103 00105 DLLLOCAL QoreProgram& operator=(const QoreProgram&); 00106 00107 protected: 00109 00111 DLLLOCAL virtual ~QoreProgram(); 00112 00113 public: 00115 DLLEXPORT QoreProgram(); 00116 00118 00121 DLLEXPORT QoreProgram(int64 parse_options); 00122 00124 00129 DLLEXPORT AbstractQoreNode *callFunction(const char *name, const QoreListNode *args, ExceptionSink *xsink); 00130 00132 00140 DLLEXPORT AbstractQoreNode *run(ExceptionSink *xsink); 00141 00143 00150 DLLEXPORT AbstractQoreNode *runTopLevel(ExceptionSink *xsink); 00151 00153 00163 DLLEXPORT void parseFileAndRun(const char *filename); 00164 00166 00175 DLLEXPORT void parseAndRun(FILE *fp, const char *name); 00176 00178 00187 DLLEXPORT void parseAndRun(const char *str, const char *name); 00188 00190 00197 DLLEXPORT void runClass(const char *classname, ExceptionSink *xsink); 00198 00200 00211 DLLEXPORT void parseFileAndRunClass(const char *filename, const char *classname); 00212 00214 00224 DLLEXPORT void parseAndRunClass(FILE *fp, const char *name, const char *classname); 00225 00227 00237 DLLEXPORT void parseAndRunClass(const char *str, const char *name, const char *classname); 00238 00240 00248 DLLEXPORT void parse(FILE *fp, const char *name, ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL); 00249 00251 00259 DLLEXPORT void parse(const QoreString *str, const QoreString *lstr, ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL); 00260 00262 00270 DLLEXPORT void parse(const char *str, const char *lstr, ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL); 00271 00273 00281 DLLEXPORT void parseFile(const char *filename, ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL, bool only_first_except = false); 00282 00284 00293 DLLEXPORT void parsePending(const char *code, const char *label, ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL); 00294 00296 00305 DLLEXPORT void parsePending(const QoreString *str, const QoreString *lstr, ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL); 00306 00308 00312 DLLEXPORT void parseCommit(ExceptionSink *xsink, ExceptionSink *warn_sink = 0, int warn_mask = QP_WARN_ALL); 00313 00315 00319 DLLEXPORT void parseRollback(); 00320 00322 DLLEXPORT bool existsFunction(const char *name); 00323 00325 00329 using AbstractPrivateData::deref; 00330 DLLEXPORT virtual void deref(ExceptionSink *xsink); 00331 00333 DLLEXPORT void lockOptions(); 00334 00336 00341 DLLEXPORT void setExecClass(const char *ecn = 0); 00342 00344 DLLEXPORT void parseSetParseOptions(int po); 00345 00347 DLLEXPORT void parseSetParseOptions(int64 po); 00348 00350 DLLEXPORT void waitForTermination(); 00351 00353 00356 DLLEXPORT void waitForTerminationAndDeref(ExceptionSink *xsink); 00357 00359 DLLEXPORT QoreNamespace *getQoreNS() const; 00360 00362 DLLEXPORT RootQoreNamespace *getRootNS() const; 00363 00365 00369 DLLEXPORT int setWarningMask(int wm); 00370 00372 00376 DLLEXPORT int enableWarning(int code); 00377 00379 00383 DLLEXPORT int disableWarning(int code); 00384 00386 DLLEXPORT int getParseOptions() const; 00387 00389 DLLEXPORT int64 getParseOptions64() const; 00390 00392 00396 DLLEXPORT void setParseOptions(int po, ExceptionSink *xsink); 00397 00399 00403 DLLEXPORT void setParseOptions(int64 po, ExceptionSink *xsink); 00404 00406 00410 DLLEXPORT void disableParseOptions(int po, ExceptionSink *xsink); 00411 00413 00417 DLLEXPORT void disableParseOptions(int64 po, ExceptionSink *xsink); 00418 00420 00424 DLLEXPORT void replaceParseOptions(int64 po, ExceptionSink *xsink); 00425 00427 00430 DLLEXPORT QoreListNode *getUserFunctionList(); 00431 00433 DLLEXPORT bool checkWarning(int code) const; 00434 00436 DLLEXPORT int getWarningMask() const; 00437 00439 DLLEXPORT bool checkFeature(const char *f) const; 00440 00442 DLLEXPORT QoreListNode *getFeatureList() const; 00443 00445 00447 DLLEXPORT QoreStringNode *getScriptName() const; 00448 00450 00452 DLLEXPORT QoreStringNode *getScriptPath() const; 00453 00455 00457 DLLEXPORT QoreStringNode *getScriptDir() const; 00458 00460 00462 DLLEXPORT void setScriptPath(const char *path); 00463 00465 00469 DLLEXPORT AbstractQoreNode *getGlobalVariableValue(const char *var, bool &found) const; 00470 00471 // retrieves the time zone setting for the program 00472 DLLEXPORT const AbstractQoreZoneInfo *currentTZ() const; 00473 00474 // sets the program's time zone 00475 DLLEXPORT void setTZ(const AbstractQoreZoneInfo *n_TZ); 00476 00478 00481 DLLEXPORT void addFeature(const char *name); 00482 00484 00486 DLLEXPORT void parseSetTimeZone(const char *zone); 00487 00489 00496 DLLEXPORT void parseDefine(const char *str, AbstractQoreNode *val); 00497 00499 00506 DLLEXPORT void parseDefine(const char *str, const char *val); 00507 00509 DLLLOCAL UserFunction *findUserFunction(const char *name); 00510 00511 DLLLOCAL QoreProgram(QoreProgram *pgm, int64 po, bool ec = false, const char *ecn = 0); 00512 00513 DLLLOCAL LocalVar *createLocalVar(const char *name, const QoreTypeInfo *typeInfo); 00514 00515 DLLLOCAL void registerUserFunctionVariant(char *name, UserFunctionVariant *variant); 00516 00517 // raises a parse exception if the function cannot be found 00518 DLLLOCAL const AbstractQoreFunction *resolveFunction(const char *name, QoreProgram *&pgm); 00519 00520 DLLLOCAL AbstractCallReferenceNode *resolveCallReference(UnresolvedProgramCallReferenceNode *fr); 00521 DLLLOCAL Var *addGlobalVarDef(const char *name, QoreParseTypeInfo *typeInfo); 00522 DLLLOCAL Var *addResolvedGlobalVarDef(const char *name, const QoreTypeInfo *typeInfo); 00523 DLLLOCAL void addStatement(AbstractStatement *s); 00524 DLLLOCAL Var *findGlobalVar(const char *name); 00525 DLLLOCAL const Var *findGlobalVar(const char *name) const; 00526 DLLLOCAL Var *checkGlobalVar(const char *name, const QoreTypeInfo *typeInfo); 00527 DLLLOCAL void importGlobalVariable(Var *var, ExceptionSink *xsink, bool readonly); 00528 00529 // takes over ownership of the xsink arg (and deletes it) 00530 DLLLOCAL void addParseException(ExceptionSink *xsink); 00531 // clears xsink arg 00532 DLLLOCAL void addParseException(ExceptionSink &xsink); 00533 // returns 0 if a "requires" exception has already occurred 00534 DLLLOCAL ExceptionSink *getParseExceptionSink(); 00535 DLLLOCAL void makeParseWarning(int sline, int eline, const char *file, int code, const char *warn, const char *fmt, ...); 00536 DLLLOCAL void makeParseWarning(int code, const char *warn, const char *fmt, ...); 00537 // takes over ownership of "desc" 00538 DLLLOCAL void makeParseWarning(int code, const char *warn, QoreStringNode *desc); 00539 DLLLOCAL void addParseWarning(int code, ExceptionSink *xsink); 00540 00541 DLLLOCAL void cannotProvideFeature(QoreStringNode *desc); 00542 DLLLOCAL void exportUserFunction(const char *name, QoreProgram *p, ExceptionSink *xsink); 00543 DLLLOCAL void exportUserFunction(const char *name, const char *new_name, QoreProgram *p, ExceptionSink *xsink); 00544 DLLLOCAL void startThread(); 00545 DLLLOCAL QoreHashNode *getThreadData(); 00546 DLLLOCAL void depRef(); 00547 DLLLOCAL void depDeref(ExceptionSink *xsink); 00548 DLLLOCAL void addFile(char *f); 00549 DLLLOCAL QoreListNode *getVarList(); 00550 DLLLOCAL void parseSetIncludePath(const char *path); 00551 DLLLOCAL const char *parseGetIncludePath() const; 00552 00553 // increment atomic thread counter 00554 DLLLOCAL void tc_inc(); 00555 00556 // decrement atomic thread counter 00557 DLLLOCAL void tc_dec(); 00558 00559 /* for run-time module loading; the parse lock must be grabbed 00560 before loading new modules - note this should only be assigned 00561 to a AutoLock or SafeLocker object! 00562 */ 00563 DLLLOCAL QoreThreadLock *getParseLock(); 00564 DLLLOCAL QoreHashNode *clearThreadData(ExceptionSink *xsink); 00565 DLLLOCAL const LVList *getTopLevelLVList() const; 00566 00568 00570 DLLLOCAL const char *parseGetScriptDir() const; 00571 00572 // can only be called while parsing from the same thread doing the parsing 00573 DLLLOCAL bool parseExceptionRaised() const; 00574 }; 00575 00577 class QoreProgramHelper { 00578 private: 00579 QoreProgram *pgm; 00580 ExceptionSink &xsink; 00581 00582 public: 00584 DLLLOCAL QoreProgramHelper(ExceptionSink &xs) : pgm(new QoreProgram), xsink(xs) { 00585 } 00586 00588 DLLLOCAL QoreProgramHelper(int64 parse_options, ExceptionSink &xs) : pgm(new QoreProgram(parse_options)), xsink(xs) { 00589 } 00590 00592 00594 DLLLOCAL ~QoreProgramHelper() { 00595 pgm->waitForTerminationAndDeref(&xsink); 00596 } 00597 00599 DLLLOCAL QoreProgram* operator->() { return pgm; } 00600 00602 DLLLOCAL QoreProgram* operator*() { return pgm; } 00603 }; 00604 00605 #endif // _QORE_QOREPROGRAM_H