Qore Programming Language  1.7.0
QoreParseListNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreParseListNode.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_QOREPARSELISTNODE_H
33 
34 #define _QORE_QOREPARSELISTNODE_H
35 
36 #include <qore/Qore.h>
37 
38 #include <vector>
39 #include <string>
40 
41 DLLLOCAL QoreValue copy_value_and_resolve_lvar_refs(const QoreValue& n, ExceptionSink* xsink);
42 
43 class QoreParseListNode : public ParseNode {
44 public:
45  typedef std::vector<QoreValue> nvec_t;
46 
47  DLLLOCAL QoreParseListNode(const QoreProgramLocation* loc) : ParseNode(loc, NT_PARSE_LIST, true) {
48  }
49 
50  DLLLOCAL QoreParseListNode(const QoreParseListNode& old, ExceptionSink* xsink) : ParseNode(old),
51  vtype(old.vtype), typeInfo(old.typeInfo), finalized(old.finalized), vlist(old.vlist) {
52  values.reserve(old.values.size());
53  lvec.reserve(old.lvec.size());
54 
55  for (unsigned i = 0; i < old.values.size(); ++i) {
56  add(copy_value_and_resolve_lvar_refs(old.values[i], xsink), old.lvec[i]);
57  if (*xsink)
58  return;
59  }
60  }
61 
62  DLLLOCAL ~QoreParseListNode() {
63  for (auto& i : values)
64  i.discard(nullptr);
65  }
66 
67  DLLLOCAL void add(QoreValue v, const QoreProgramLocation* loc) {
68  values.push_back(v);
69  lvec.push_back(loc);
70 
71  if (!size()) {
72  if (v.hasNode() && v.hasEffect())
73  set_effect(true);
74  } else if (has_effect() && !v.hasEffect()) {
75  set_effect(false);
76  }
77  }
78 
79  DLLLOCAL QoreValue shift() {
80  if (!values.size())
81  return QoreValue();
82  assert(vtypes.empty());
83  QoreValue rv = values[0];
84  values.erase(values.begin());
85  lvec.erase(lvec.begin());
86  return rv;
87  }
88 
89  DLLLOCAL QoreValue get(size_t i) {
90  assert(i < values.size());
91  return values[i];
92  }
93 
94  DLLLOCAL const QoreValue get(size_t i) const {
95  assert(i < values.size());
96  return values[i];
97  }
98 
99  DLLLOCAL QoreValue& getReference(size_t i) {
100  assert(i < values.size());
101  return values[i];
102  }
103 
104  DLLLOCAL const QoreProgramLocation* getLocation(size_t i) const {
105  assert(i < lvec.size());
106  return lvec[i];
107  }
108 
109  DLLLOCAL QoreValue swap(size_t i, QoreValue v) {
110  QoreValue rv = values[i];
111  values[i] = v;
112  return rv;
113  }
114 
115  DLLLOCAL size_t size() const {
116  return values.size();
117  }
118 
119  DLLLOCAL bool empty() const {
120  return values.empty();
121  }
122 
123  DLLLOCAL void setFinalized() {
124  if (!finalized)
125  finalized = true;
126  }
127 
128  DLLLOCAL bool isFinalized() const {
129  return finalized;
130  }
131 
132  DLLLOCAL bool isVariableList() const {
133  return vlist;
134  }
135 
136  DLLLOCAL void setVariableList() {
137  assert(!vlist);
138  vlist = true;
139  }
140 
141  DLLLOCAL void finalizeBlock(int start_line, int end_line);
142 
143  DLLLOCAL nvec_t& getValues() {
144  return values;
145  }
146 
147  DLLLOCAL const nvec_t& getValues() const {
148  return values;
149  }
150 
151  DLLLOCAL const type_vec_t& getValueTypes() const {
152  return vtypes;
153  }
154 
155  DLLLOCAL QoreParseListNode* listRefSelf() const {
156  ref();
157  return const_cast<QoreParseListNode*>(this);
158  }
159 
160  DLLLOCAL int initArgs(QoreParseContext& parse_context, type_vec_t& arg_types, QoreListNode*& args);
161 
162  DLLLOCAL virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
163 
164  DLLLOCAL virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
165 
166  DLLLOCAL virtual const char* getTypeName() const {
167  return "list";
168  }
169 
170  DLLLOCAL QoreParseListNode* copyBlank() const {
171  QoreParseListNode* n = new QoreParseListNode(loc);
172  n->vtype = vtype;
173 
174  n->finalized = finalized;
175  n->vlist = vlist;
176  return n;
177  }
178 
179  DLLLOCAL int parseInitIntern(bool& needs_eval, QoreParseContext& parse_context);
180 
181 protected:
182  typedef std::vector<const QoreProgramLocation*> lvec_t;
183  nvec_t values;
184  type_vec_t vtypes;
185  lvec_t lvec;
186  // common value type, if any
187  const QoreTypeInfo* vtype = nullptr;
188  // node type info (derivative of list)
189  const QoreTypeInfo* typeInfo;
190  // flag for a list expression in curly brackets for the list version of the map operator
191  bool finalized = false;
192  // is this a variable list declaration?
193  bool vlist = false;
194 
195  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
196  return typeInfo;
197  }
198 
199  DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
200 
201  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
202 };
203 
204 #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
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
std::vector< const QoreTypeInfo * > type_vec_t
vector of type information for parameter lists
Definition: common.h:251
const qore_type_t NT_PARSE_LIST
type value for QoreParseListNode
Definition: node_types.h:84
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275
DLLEXPORT bool hasNode() const
returns true if the object contains a non-null AbstractQoreNode pointer (ie type == QV_Node && v....