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