Qore Programming Language  1.12.0
QoreClosureNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreClosureNode.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_QORECLOSURENODE_H
33 
34 #define _QORE_QORECLOSURENODE_H
35 
36 #include "qore/intern/QoreObjectIntern.h"
37 
38 #include <map>
39 
40 class CVecInstantiator {
41 protected:
42  cvv_vec_t* cvec;
43  ExceptionSink* xsink;
44 
45 public:
46  DLLLOCAL CVecInstantiator(cvv_vec_t* cv, ExceptionSink* xs) : cvec(cv), xsink(xs) {
47  if (!cvec)
48  return;
49  for (cvv_vec_t::iterator i = cvec->begin(), e = cvec->end(); i != e; ++i)
50  thread_instantiate_closure_var((*i)->refSelf());
51  }
52 
53  DLLLOCAL ~CVecInstantiator() {
54  if (!cvec)
55  return;
56  // elements are dereferenced when uninstantiated
57  for (cvv_vec_t::iterator i = cvec->begin(), e = cvec->end(); i != e; ++i)
58  thread_uninstantiate_closure_var(xsink);
59  }
60 };
61 
62 class QoreClosureBase : public ResolvedCallReferenceNode {
63 protected:
64  const QoreClosureParseNode* closure;
65  mutable ThreadSafeLocalVarRuntimeEnvironment closure_env;
66  cvv_vec_t* cvec;
67  const qore_class_private* class_ctx;
68 
69  DLLLOCAL void del(ExceptionSink* xsink) {
70  closure_env.del(xsink);
71  if (cvec) {
72  for (cvv_vec_t::iterator i = cvec->begin(), e = cvec->end(); i != e; ++i)
73  (*i)->deref(xsink);
74  delete cvec;
75 #ifdef DEBUG
76  cvec = nullptr;
77 #endif
78  }
79  }
80 
81 public:
83  DLLLOCAL QoreClosureBase(const QoreClosureParseNode* n_closure, cvv_vec_t* cv, const qore_class_private* class_ctx)
84  : ResolvedCallReferenceNode(false, NT_RUNTIME_CLOSURE), closure(n_closure),
85  closure_env(n_closure->getVList()), cvec(cv), class_ctx(class_ctx) {
86  //printd(5, "QoreClosureBase::QoreClosureBase() this: %p closure: %p\n", this, closure);
87  closure->ref();
88  }
89 
90  DLLLOCAL ~QoreClosureBase() {
91  //printd(5, "QoreClosureBase::~QoreClosureBase() this: %p closure: %p\n", this, closure);
92  const_cast<QoreClosureParseNode*>(closure)->deref();
93  assert(!cvec);
94  }
95 
96  DLLLOCAL ClosureVarValue* find(const LocalVar* id) const {
97  return closure_env.find(id);
98  }
99 
100  DLLLOCAL bool hasVar(ClosureVarValue* cvv) const {
101  return closure_env.hasVar(cvv);
102  }
103 
104  DLLLOCAL const cvar_map_t& getMap() const {
105  return closure_env.getMap();
106  }
107 
108  // returns true if at least one variable in the set of closure-bound local variables could contain an object or a closure (also through a container)
109  DLLLOCAL bool needsScan() const {
110  return closure->needsScan();
111  }
112 
113  DLLLOCAL static const char* getStaticTypeName() {
114  return "closure";
115  }
116 
117  DLLLOCAL virtual QoreFunction* getFunction() {
118  return closure->getFunction();
119  }
120 
121  DLLLOCAL virtual QoreObject* getObject() const {
122  return nullptr;
123  }
124 };
125 
126 class QoreClosureNode : public QoreClosureBase {
127 private:
128  QoreProgram* pgm;
129 
130  DLLLOCAL QoreClosureNode(const QoreClosureNode&); // not implemented
131  DLLLOCAL QoreClosureNode& operator=(const QoreClosureNode&); // not implemented
132 
133 protected:
134  DLLLOCAL virtual bool derefImpl(ExceptionSink* xsink);
135 
136 public:
137  DLLLOCAL QoreClosureNode(const QoreClosureParseNode* n_closure, cvv_vec_t* cv = nullptr,
138  const qore_class_private* class_ctx = nullptr) : QoreClosureBase(n_closure, cv, class_ctx), pgm(::getProgram()) {
139  pgm->depRef();
140  }
141 
142  DLLLOCAL virtual ~QoreClosureNode() {
143  }
144 
145  DLLLOCAL virtual QoreValue execValue(const QoreListNode* args, ExceptionSink* xsink) const;
146 
147  DLLLOCAL virtual QoreProgram* getProgram() const {
148  return pgm;
149  }
150 
152 
154  DLLEXPORT virtual bool getAsBoolImpl() const;
155 
156  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const {
157  str.sprintf("function closure (%slambda, %p)", closure->isLambda() ? "" : "non-", this);
158  return 0;
159  }
160 
161  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const {
162  del = true;
163  QoreString* rv = new QoreString;
164  getAsString(*rv, foff, xsink);
165  return rv;
166  }
167 
168  DLLLOCAL virtual const char* getTypeName() const {
169  return getStaticTypeName();
170  }
171 
172  DLLLOCAL bool isLambda() const { return closure->isLambda(); }
173 
174  DLLLOCAL virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const {
175  return QoreClosureNode::is_equal_hard(v, xsink);
176  }
177 
178  DLLLOCAL virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const {
179  return v == this;
180  }
181 };
182 
183 class QoreObjectClosureNode : public QoreClosureBase {
184 private:
185  QoreObject* obj;
186 
187  DLLLOCAL QoreObjectClosureNode(const QoreObjectClosureNode&); // not implemented
188  DLLLOCAL QoreObjectClosureNode& operator=(const QoreObjectClosureNode&); // not implemented
189 
190 protected:
191  DLLLOCAL virtual bool derefImpl(ExceptionSink* xsink);
192 
193 public:
194  DLLLOCAL QoreObjectClosureNode(QoreObject* n_obj, const qore_class_private* c_ctx,
195  const QoreClosureParseNode* n_closure, cvv_vec_t* cv = nullptr)
196  : QoreClosureBase(n_closure, cv, c_ctx),
197  obj(n_obj) {
198  obj->tRef();
199  }
200 
201  DLLLOCAL ~QoreObjectClosureNode() {
202  assert(!obj);
203  }
204 
205  DLLLOCAL virtual QoreValue execValue(const QoreListNode* args, ExceptionSink* xsink) const;
206 
207  DLLLOCAL virtual QoreProgram* getProgram() const {
208  return obj->getProgram();
209  }
210 
211  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const {
212  str.sprintf("function closure (%slambda, in object of class '%s', %p)", closure->isLambda() ? "" : "non-", obj->getClassName(), this);
213  return 0;
214  }
215 
216  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const {
217  del = true;
218  QoreString* rv = new QoreString;
219  getAsString(*rv, foff, xsink);
220  return rv;
221  }
222 
223  DLLLOCAL virtual const char* getTypeName() const {
224  return getStaticTypeName();
225  }
226 
227  DLLLOCAL bool isLambda() const { return closure->isLambda(); }
228 
229  DLLLOCAL virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const {
230  return QoreObjectClosureNode::is_equal_hard(v, xsink);
231  }
232 
233  DLLLOCAL virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const {
234  return v == this;
235  }
236 
237  DLLLOCAL virtual QoreObject* getObject() const {
238  return obj;
239  }
240 };
241 
242 #endif
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
DLLEXPORT void ref() const
increments the reference count
DLLLOCAL AbstractQoreNode & operator=(const AbstractQoreNode &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and calls derefImpl() if there_can_be_only_one is false,...
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
DLLEXPORT void tRef() const
increments the existence reference count
DLLEXPORT const char * getClassName() const
returns the name of the class
DLLEXPORT QoreProgram * getProgram() const
returns the QoreProgram object associated with this object
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only
Definition: QoreProgram.h:127
DLLEXPORT void depRef()
incremements the weak reference count for the program object
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
base class for resolved call references
Definition: CallReferenceNode.h:109
virtual DLLLOCAL QoreFunction * getFunction()=0
Returns the internal function object, if any; can return nullptr.
const qore_type_t NT_RUNTIME_CLOSURE
type value for ResolvedCallReferenceNode (QoreClosureNode, QoreObjectClosureNode)
Definition: node_types.h:71
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275