Qore Programming Language  1.12.0
QoreClosureParseNode.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_QORECLOSUREPARSENODE_H
33 
34 #define _QORE_QORECLOSUREPARSENODE_H
35 
36 #include "qore/intern/ParseNode.h"
37 
38 #include <vector>
39 
40 class LocalVar;
41 class ThreadSafeLocalVarRuntimeEnvironment;
42 
43 class ClosureParseEnvironment {
44 private:
45  LVarSet* vlist;
46  VNode* high_water_mark;
47  ClosureParseEnvironment* prev;
48 
49 public:
50  DLLLOCAL ClosureParseEnvironment(LVarSet* n_vlist) : vlist(n_vlist), high_water_mark(getVStack()) {
51  prev = thread_get_closure_parse_env();
52  thread_set_closure_parse_env(this);
53  }
54 
55  DLLLOCAL ~ClosureParseEnvironment() {
56  thread_set_closure_parse_env(prev);
57  }
58 
59  DLLLOCAL VNode* getHighWaterMark() {
60  return high_water_mark;
61  }
62 
63  DLLLOCAL void add(LocalVar* var) {
64  // add var to the set
65  vlist->add(var);
66  }
67 };
68 
69 class QoreClosureNode;
70 class QoreObjectClosureNode;
71 
72 class QoreClosureParseNode : public ParseNode {
73  friend class QoreClosureParseNodeBackground;
74 private:
75  UserClosureFunction* uf;
76  bool lambda, in_method;
77 
78  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
79 
80  DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
81  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
82  return runTimeClosureTypeInfo;
83  }
84 
85  DLLLOCAL QoreClosureNode* evalClosure() const;
86  DLLLOCAL QoreObjectClosureNode* evalObjectClosure() const;
87 
88 public:
89  DLLLOCAL QoreClosureParseNode(const QoreProgramLocation* loc, UserClosureFunction* n_uf, bool n_lambda = false);
90 
91  DLLLOCAL ~QoreClosureParseNode() {
92  delete uf;
93  }
94 
95  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
96  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
97  DLLLOCAL virtual const char* getTypeName() const;
98  DLLLOCAL static const char* getStaticTypeName() {
99  return "function closure";
100  }
101 
102  DLLLOCAL bool isLambda() const { return lambda; }
103 
104  DLLLOCAL QoreValue exec(const QoreClosureBase& closure_base, QoreProgram* pgm, const QoreListNode* args,
105  QoreObject* self, const qore_class_private* class_ctx, ExceptionSink* xsink) const;
106 
107  DLLLOCAL QoreClosureBase* evalBackground(ExceptionSink* xsink) const;
108 
109  DLLLOCAL const LVarSet* getVList() const {
110  return uf->getVList();
111  }
112 
113  // 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)
114  DLLLOCAL bool needsScan() const {
115  return uf->needsScan();
116  }
117 
118  DLLLOCAL UserClosureFunction* getFunction() const {
119  return uf;
120  }
121 
122  DLLLOCAL QoreClosureParseNode* refSelf() const {
123  ref();
124  return const_cast<QoreClosureParseNode*>(this);
125  }
126 };
127 
128 #endif
DLLEXPORT void ref() const
increments the reference count
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
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only
Definition: QoreProgram.h:127
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275