Qore Programming Language  0.8.11.1
QoreProgram.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreProgram.h
4 
5  Program Object Definition
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2014 David Nichols
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_QOREPROGRAM_H
35 
36 #define _QORE_QOREPROGRAM_H
37 
38 #include <qore/AbstractPrivateData.h>
39 #include <qore/Restrictions.h>
40 
41 // warnings - must correspond with the string order in QoreProgram.cpp
42 // new warnings must also be added as constants
43 #define QP_WARN_NONE 0
44 #define QP_WARN_WARNING_MASK_UNCHANGED (1 << 0)
45 #define QP_WARN_DUPLICATE_LOCAL_VARS (1 << 1)
46 #define QP_WARN_UNKNOWN_WARNING (1 << 2)
47 #define QP_WARN_UNDECLARED_VAR (1 << 3)
48 #define QP_WARN_DUPLICATE_GLOBAL_VARS (1 << 4)
49 #define QP_WARN_UNREACHABLE_CODE (1 << 5)
50 #define QP_WARN_NONEXISTENT_METHOD_CALL (1 << 6)
51 #define QP_WARN_INVALID_OPERATION (1 << 7)
52 #define QP_WARN_CALL_WITH_TYPE_ERRORS (1 << 8)
53 #define QP_WARN_RETURN_VALUE_IGNORED (1 << 9)
54 #define QP_WARN_DEPRECATED (1 << 10)
55 #define QP_WARN_EXCESS_ARGS (1 << 11)
56 #define QP_WARN_DUPLICATE_HASH_KEY (1 << 12)
57 #define QP_WARN_UNREFERENCED_VARIABLE (1 << 13)
58 #define QP_WARN_DUPLICATE_BLOCK_VARS (1 << 14)
59 #define QP_WARN_MODULE_ONLY (1 << 15)
60 #define QP_WARN_ALL -1
61 
62 #define QP_WARN_MODULES (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_DUPLICATE_HASH_KEY|QP_WARN_DUPLICATE_BLOCK_VARS)
63 
64 #define QP_WARN_DEFAULT (QP_WARN_UNKNOWN_WARNING|QP_WARN_MODULES|QP_WARN_DEPRECATED)
65 
67 DLLEXPORT extern const char** qore_warnings;
68 
70 DLLEXPORT extern unsigned qore_num_warnings;
71 
73 DLLEXPORT int get_warning_code(const char* str);
74 
76 class LocalVar;
77 class ExceptionSink;
78 class QoreListNode;
79 class QoreNamespace;
80 class RootQoreNamespace;
81 class QoreStringNode;
82 class QoreHashNode;
83 class FunctionCallNode;
84 class AbstractStatement;
85 class UnresolvedProgramCallReferenceNode;
86 class Var;
87 class LVList;
88 class UserFunctionVariant;
89 class QoreTypeInfo;
90 class QoreParseTypeInfo;
91 class ParamList;
92 class AbstractQoreZoneInfo;
93 class qore_program_private;
94 
96 
104  friend class qore_program_private_base;
105  friend class qore_program_private;
106 private:
108  qore_program_private* priv;
109 
111  DLLLOCAL QoreProgram(const QoreProgram&);
112 
114  DLLLOCAL QoreProgram& operator=(const QoreProgram&);
115 
116 protected:
118 
120  DLLLOCAL virtual ~QoreProgram();
121 
122 public:
124  DLLEXPORT QoreProgram();
125 
127 
130  DLLEXPORT QoreProgram(int64 parse_options);
131 
133 
138  DLLEXPORT AbstractQoreNode* callFunction(const char* name, const QoreListNode* args, ExceptionSink* xsink);
139 
141 
149  DLLEXPORT AbstractQoreNode* run(ExceptionSink* xsink);
150 
152 
159  DLLEXPORT AbstractQoreNode* runTopLevel(ExceptionSink* xsink);
160 
162 
172  DLLEXPORT void parseFileAndRun(const char* filename);
173 
175 
184  DLLEXPORT void parseAndRun(FILE *fp, const char* name);
185 
187 
196  DLLEXPORT void parseAndRun(const char* str, const char* name);
197 
199 
206  DLLEXPORT void runClass(const char* classname, ExceptionSink* xsink);
207 
209 
220  DLLEXPORT void parseFileAndRunClass(const char* filename, const char* classname);
221 
223 
233  DLLEXPORT void parseAndRunClass(FILE *fp, const char* name, const char* classname);
234 
236 
246  DLLEXPORT void parseAndRunClass(const char* str, const char* name, const char* classname);
247 
249 
257  DLLEXPORT void parse(FILE *fp, const char* name, ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL);
258 
260 
268  DLLEXPORT void parse(const QoreString* str, const QoreString* lstr, ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL);
269 
271 
281  DLLEXPORT void parse(const QoreString* str, const QoreString* lstr, ExceptionSink* xsink, ExceptionSink* warn_sink, int warn_mask, const QoreString* source, int offset);
282 
284 
292  DLLEXPORT void parse(const char* str, const char* lstr, ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL);
293 
295 
305  DLLEXPORT void parse(const char* str, const char* lstr, ExceptionSink* xsink, ExceptionSink* warn_sink, int warn_mask, const char* source, int offset);
306 
308 
316  DLLEXPORT void parseFile(const char* filename, ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL, bool only_first_except = false);
317 
319 
328  DLLEXPORT void parsePending(const char* code, const char* label, ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL);
329 
331 
343  DLLEXPORT void parsePending(const char* code, const char* label, ExceptionSink* xsink, ExceptionSink* warn_sink, int warn_mask, const char* source, int offset);
344 
346 
356  DLLEXPORT void parsePending(const QoreString* str, const QoreString* lstr, ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL);
357 
359 
371  DLLEXPORT void parsePending(const QoreString* str, const QoreString* lstr, ExceptionSink* xsink, ExceptionSink* warn_sink, int warn_mask, const QoreString* source, int offset);
372 
374 
378  DLLEXPORT void parseCommit(ExceptionSink* xsink, ExceptionSink* warn_sink = 0, int warn_mask = QP_WARN_ALL);
379 
381 
385  DLLEXPORT void parseRollback();
386 
388  DLLEXPORT bool existsFunction(const char* name);
389 
391 
396  DLLEXPORT virtual void deref(ExceptionSink* xsink);
397 
399  DLLEXPORT void lockOptions();
400 
402 
407  DLLEXPORT void setExecClass(const char* ecn = 0);
408 
410  DLLEXPORT void parseSetParseOptions(int po);
411 
413  DLLEXPORT void parseSetParseOptions(int64 po);
414 
416  DLLEXPORT void parseDisableParseOptions(int64 po);
417 
419  DLLEXPORT void waitForTermination();
420 
422 
425  DLLEXPORT void waitForTerminationAndDeref(ExceptionSink* xsink);
426 
428  DLLEXPORT QoreNamespace* getQoreNS() const;
429 
431  DLLEXPORT RootQoreNamespace* getRootNS() const;
432 
434 
438  DLLEXPORT int setWarningMask(int wm);
439 
441 
445  DLLEXPORT int enableWarning(int code);
446 
448 
452  DLLEXPORT int disableWarning(int code);
453 
455  DLLEXPORT int getParseOptions() const;
456 
458  DLLEXPORT int64 getParseOptions64() const;
459 
461 
465  DLLEXPORT void setParseOptions(int po, ExceptionSink* xsink);
466 
468 
472  DLLEXPORT void setParseOptions(int64 po, ExceptionSink* xsink);
473 
475 
479  DLLEXPORT void disableParseOptions(int po, ExceptionSink* xsink);
480 
482 
486  DLLEXPORT void disableParseOptions(int64 po, ExceptionSink* xsink);
487 
489 
493  DLLEXPORT void replaceParseOptions(int64 po, ExceptionSink* xsink);
494 
496 
499  DLLEXPORT QoreListNode* getUserFunctionList();
500 
502  DLLEXPORT bool checkWarning(int code) const;
503 
505  DLLEXPORT int getWarningMask() const;
506 
508  DLLEXPORT bool checkFeature(const char* f) const;
509 
511  DLLEXPORT QoreListNode* getFeatureList() const;
512 
514 
516  DLLEXPORT QoreStringNode* getScriptName() const;
517 
519 
521  DLLEXPORT QoreStringNode* getScriptPath() const;
522 
524 
526  DLLEXPORT QoreStringNode* getScriptDir() const;
527 
529 
531  DLLEXPORT void setScriptPath(const char* path);
532 
534 
538  DLLEXPORT AbstractQoreNode* getGlobalVariableValue(const char* var, bool &found) const;
539 
540  // retrieves the time zone setting for the program
541  DLLEXPORT const AbstractQoreZoneInfo *currentTZ() const;
542 
543  // sets the program's time zone
544  DLLEXPORT void setTZ(const AbstractQoreZoneInfo *n_TZ);
545 
547 
550  DLLEXPORT void addFeature(const char* name);
551 
553 
555  DLLEXPORT void parseSetTimeZone(const char* zone);
556 
558 
565  DLLEXPORT void parseDefine(const char* str, AbstractQoreNode* val);
566 
568 
575  DLLEXPORT void parseDefine(const char* str, const char* val);
576 
577  DLLLOCAL QoreProgram(QoreProgram* pgm, int64 po, bool ec = false, const char* ecn = 0);
578 
579  DLLLOCAL LocalVar *createLocalVar(const char* name, const QoreTypeInfo *typeInfo);
580 
581  // returns 0 if a "requires" exception has already occurred
582  DLLLOCAL ExceptionSink* getParseExceptionSink();
583 
584  DLLLOCAL QoreHashNode* getThreadData();
585  DLLLOCAL void depRef();
586  DLLLOCAL void depDeref(ExceptionSink* xsink);
587  DLLLOCAL void addFile(char* f);
588  DLLLOCAL QoreListNode* getVarList();
589  DLLLOCAL void parseSetIncludePath(const char* path);
590  DLLLOCAL const char* parseGetIncludePath() const;
591 
592  /* for run-time module loading; the parse lock must be grabbed
593  before loading new modules - note this should only be assigned
594  to an AutoLock or SafeLocker object!
595  */
596  DLLLOCAL QoreThreadLock *getParseLock();
597  DLLLOCAL const LVList* getTopLevelLVList() const;
598 
600 
602  DLLLOCAL const char* parseGetScriptDir() const;
603 
604  // can only be called while parsing from the same thread doing the parsing
605  DLLLOCAL bool parseExceptionRaised() const;
606 };
607 
609 
612 private:
613  QoreProgram* pgm;
614  ExceptionSink& xsink;
615 
616 public:
618  DLLLOCAL QoreProgramHelper(ExceptionSink& xs) : pgm(new QoreProgram), xsink(xs) {
619  }
620 
622  DLLLOCAL QoreProgramHelper(int64 parse_options, ExceptionSink& xs) : pgm(new QoreProgram(parse_options)), xsink(xs) {
623  }
624 
626 
628  DLLLOCAL ~QoreProgramHelper() {
629  // waits for all background threads to execute
630  thread_counter.waitForZero(&xsink);
631  // waits for the current Program to terminate
632  pgm->waitForTerminationAndDeref(&xsink);
633  }
634 
636  DLLLOCAL QoreProgram* operator->() { return pgm; }
637 
639  DLLLOCAL QoreProgram* operator*() { return pgm; }
640 };
641 
642 #endif // _QORE_QOREPROGRAM_H
DLLEXPORT void waitForTermination()
this call blocks until the program's last thread terminates
DLLEXPORT QoreNamespace * getQoreNS() const
returns a pointer to the "Qore" namespace
DLLEXPORT void parseDefine(const char *str, AbstractQoreNode *val)
defines a parse-time variable; call only at parse time (or before parsing)
DLLEXPORT void lockOptions()
locks parse options so they may not be changed
DLLEXPORT RootQoreNamespace * getRootNS() const
returns a pointer to the root namespace
DLLEXPORT void parsePending(const char *code, const char *label, ExceptionSink *xsink, ExceptionSink *warn_sink=0, int warn_mask=QP_WARN_ALL)
parses code from the given string but does not commit changes to the QoreProgram
DLLEXPORT void addFeature(const char *name)
manually add the feature to the program
DLLEXPORT int getParseOptions() const
returns the parse options currently set for this program; DEPRECATED; use getParseOptions64() instead...
DLLLOCAL ~QoreProgramHelper()
waits until all background threads in the Qore library have terminated and until the QoreProgram obje...
Definition: QoreProgram.h:628
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:49
virtual DLLLOCAL ~QoreProgram()
the destructor is private in order to prohibit the object from being allocated on the stack ...
DLLLOCAL QoreProgram * operator*()
returns the QoreProgram object being managed
Definition: QoreProgram.h:639
DLLEXPORT void waitForTerminationAndDeref(ExceptionSink *xsink)
this call blocks until the program's last thread terminates, and then calls QoreProgram::deref() ...
DLLEXPORT void disableParseOptions(int po, ExceptionSink *xsink)
turns off the parse options given in the passed mask and adds Qore-language exception information if ...
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
DLLEXPORT AbstractQoreNode * getGlobalVariableValue(const char *var, bool &found) const
returns the value of the global variable given (do not include the "$" symbol), the caller owns the r...
DLLEXPORT void setExecClass(const char *ecn=0)
sets the name of the application class to be executed (instantiated) instead of top-level code ...
DLLEXPORT AbstractQoreNode * runTopLevel(ExceptionSink *xsink)
tuns the top level code and returns any return value
DLLEXPORT bool checkWarning(int code) const
returns true if the warning code is set
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:58
DLLEXPORT QoreListNode * getFeatureList() const
returns a list of features in the program object
DLLEXPORT void parseFileAndRun(const char *filename)
parses the given filename and runs the file
DLLEXPORT QoreCounter thread_counter
global background thread counter (for threads started explicitly by Qore)
DLLLOCAL QoreProgramHelper(ExceptionSink &xs)
creates the QoreProgram object: DEPRECATED: use QoreProgramHelper(int64, ExceptionSink&) instead ...
Definition: QoreProgram.h:618
base class for call references, reference-counted, dynamically allocated only
Definition: CallReferenceNode.h:39
DLLEXPORT AbstractQoreNode * callFunction(const char *name, const QoreListNode *args, ExceptionSink *xsink)
calls a function from the function name and returns the return value
safely manages QoreProgram objects; note the the destructor will block until all background threads i...
Definition: QoreProgram.h:611
DLLEXPORT void parseSetTimeZone(const char *zone)
sets the time zone during parsing
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:50
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:48
DLLEXPORT void parseAndRun(FILE *fp, const char *name)
parses the given file and runs the file
DLLEXPORT QoreListNode * getUserFunctionList()
returns a list of all user functions in this program
the root namespace of a QoreProgram object
Definition: QoreNamespace.h:219
DLLEXPORT bool existsFunction(const char *name)
returns true if the given function exists as a user function, false if not
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT int enableWarning(int code)
enables a warning by its code
DLLLOCAL const char * parseGetScriptDir() const
returns the script directory, if known (0 if not), does not grab the parse lock, only to be called wh...
DLLLOCAL QoreProgramHelper(int64 parse_options, ExceptionSink &xs)
creates the QoreProgram object and sets the parse options
Definition: QoreProgram.h:622
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:65
DLLEXPORT void parseCommit(ExceptionSink *xsink, ExceptionSink *warn_sink=0, int warn_mask=QP_WARN_ALL)
commits pending changes to the program
DLLEXPORT int setWarningMask(int wm)
sets the warning mask
DLLEXPORT void setScriptPath(const char *path)
sets the script path
DLLEXPORT QoreStringNode * getScriptPath() const
returns the script path (directory and name), if known (0 if not)
DLLEXPORT int64 getParseOptions64() const
returns the parse options currently set for this program
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only ...
Definition: QoreProgram.h:103
DLLEXPORT void parseRollback()
rolls back changes to the program object that were added with QoreProgram::parsePending() ...
DLLEXPORT void parse(FILE *fp, const char *name, ExceptionSink *xsink, ExceptionSink *warn_sink=0, int warn_mask=QP_WARN_ALL)
parses code from the file given and commits changes to the QoreProgram
DLLEXPORT void parseAndRunClass(FILE *fp, const char *name, const char *classname)
parses the given file and runs the code by instantiating the class given
DLLEXPORT void setParseOptions(int po, ExceptionSink *xsink)
sets the parse options and adds Qore-language exception information if an error occurs ...
DLLLOCAL QoreProgram * operator->()
returns the QoreProgram object being managed
Definition: QoreProgram.h:636
DLLEXPORT QoreStringNode * getScriptDir() const
returns the script directory, if known (0 if not)
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
DLLEXPORT void runClass(const char *classname, ExceptionSink *xsink)
instantiates the class given and runs its constructor
DLLEXPORT QoreProgram()
creates the object
DLLEXPORT int disableWarning(int code)
disables a warning by its code
DLLEXPORT void parseSetParseOptions(int po)
adds the parse options given to the parse option mask; DEPRECATED: use parseSetParseOptions(int64) in...
DLLEXPORT void parseDisableParseOptions(int64 po)
disables the parse options given to the parse option mask
DLLEXPORT AbstractQoreNode * run(ExceptionSink *xsink)
runs the program (instantiates the program class if a program class has been set) and returns the ret...
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
DLLEXPORT bool checkFeature(const char *f) const
returns true if the given feature is present in the program object
DLLEXPORT void replaceParseOptions(int64 po, ExceptionSink *xsink)
replaces the parse options in the program with those given by the argument; adds Qore-language except...
DLLEXPORT int waitForZero(ExceptionSink *xsink, int timeout_ms=0)
blocks the calling thread until the counter reaches 0
DLLEXPORT int getWarningMask() const
returns the warning mask
DLLEXPORT void parseFileAndRunClass(const char *filename, const char *classname)
parses the given filename and runs the program by instantiating the class given
DLLEXPORT void parseFile(const char *filename, ExceptionSink *xsink, ExceptionSink *warn_sink=0, int warn_mask=QP_WARN_ALL, bool only_first_except=false)
parses code from the file given and commits changes to the QoreProgram
DLLEXPORT QoreStringNode * getScriptName() const
returns the script file name, if known (0 if not)
DLLEXPORT const AbstractQoreZoneInfo * currentTZ()
returns the current local time zone, note that if 0 = UTC