Qore Programming Language  1.12.1
QoreCastOperatorNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreCastOperatorNode.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_QORECASTOPERATORNODE_H
33 
34 #define _QORE_QORECASTOPERATORNODE_H
35 
36 class QoreParseCastOperatorNode : public QoreSingleExpressionOperatorNode<> {
37 friend class QoreCastOperatorNode;
38 public:
39  DLLLOCAL QoreParseCastOperatorNode(const QoreProgramLocation* loc, QoreParseTypeInfo* pti, QoreValue exp)
40  : QoreSingleExpressionOperatorNode<>(loc, exp), pti(pti) {
41  }
42 
43  // type is unknown before resolution
44  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
45  return anyTypeInfo;
46  }
47 
48  DLLLOCAL virtual ~QoreParseCastOperatorNode() {
49  delete pti;
50  }
51 
52  DLLLOCAL virtual QoreString* getAsString(bool &del, int foff, ExceptionSink *xsink) const;
53 
54  DLLLOCAL virtual int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const;
55 
56  // returns the type name as a c string
57  DLLLOCAL virtual const char* getTypeName() const {
58  return cast_str.getBuffer();
59  }
60 
61  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
62  assert(false);
63  return nullptr;
64  }
65 
66 protected:
67  DLLLOCAL static QoreString cast_str;
68  QoreParseTypeInfo* pti;
69 
70  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const {
71  assert(false);
72  return QoreValue();
73  }
74 
75  DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context);
76 };
77 
78 class QoreCastOperatorNode : public QoreSingleExpressionOperatorNode<> {
79 public:
80  DLLLOCAL QoreCastOperatorNode(const QoreProgramLocation* loc, QoreValue exp)
81  : QoreSingleExpressionOperatorNode<>(loc, exp) {
82  }
83 
84  DLLLOCAL virtual ~QoreCastOperatorNode() = default;
85 
86  DLLLOCAL QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const {
87  del = false;
88  return &QoreParseCastOperatorNode::cast_str;
89  }
90 
91  DLLLOCAL virtual int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const {
92  str.concat(&QoreParseCastOperatorNode::cast_str);
93  return 0;
94  }
95 
96  // returns the type name as a c string
97  DLLLOCAL virtual const char* getTypeName() const {
98  return QoreParseCastOperatorNode::cast_str.getBuffer();
99  }
100 
101  // checks if the value matches the expected type
102  DLLLOCAL virtual int checkValue(ExceptionSink* xsink, const QoreValue& val, bool lvalue) const = 0;
103 
104 protected:
105  DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context) {
106  return 0;
107  }
108 };
109 
110 class QoreClassCastOperatorNode : public QoreCastOperatorNode {
111 public:
112  DLLLOCAL QoreClassCastOperatorNode(const QoreProgramLocation* loc, const QoreClass* qc, QoreValue exp,
113  bool or_nothing)
114  : QoreCastOperatorNode(loc, exp), qc(qc), or_nothing(or_nothing) {
115  }
116 
117  DLLLOCAL virtual ~QoreClassCastOperatorNode() = default;
118 
119  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
120  return qc ? qc->getTypeInfo() : objectTypeInfo;
121  }
122 
123  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
124  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
125  if (*xsink)
126  return nullptr;
127  return new QoreClassCastOperatorNode(loc, qc, n_exp.release(), or_nothing);
128  }
129 
130  // checks if the value matches the expected type
131  DLLLOCAL virtual int checkValue(ExceptionSink* xsink, const QoreValue& val, bool lvalue) const;
132 
133 protected:
134  const QoreClass* qc;
135  bool or_nothing;
136 
137  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
138 };
139 
140 class QoreHashDeclCastOperatorNode : public QoreCastOperatorNode {
141 public:
142  DLLLOCAL QoreHashDeclCastOperatorNode(const QoreProgramLocation* loc, const TypedHashDecl* hd, QoreValue exp,
143  bool or_nothing)
144  : QoreCastOperatorNode(loc, exp), hd(hd), or_nothing(or_nothing) {
145  }
146 
147  DLLLOCAL virtual ~QoreHashDeclCastOperatorNode() = default;
148 
149  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
150  return hd ? hd->getTypeInfo() : hashTypeInfo;
151  }
152 
153  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
154  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
155  if (*xsink)
156  return nullptr;
157  return new QoreHashDeclCastOperatorNode(loc, hd, n_exp.release(), or_nothing);
158  }
159 
160  // checks if the value matches the expected type
161  DLLLOCAL virtual int checkValue(ExceptionSink* xsink, const QoreValue& val, bool lvalue) const;
162 
163 protected:
164  const TypedHashDecl* hd;
165  bool or_nothing;
166 
167  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
168 };
169 
170 class QoreComplexHashCastOperatorNode : public QoreCastOperatorNode {
171 public:
172  DLLLOCAL QoreComplexHashCastOperatorNode(const QoreProgramLocation* loc, const QoreTypeInfo* typeInfo,
173  QoreValue exp, bool or_nothing)
174  : QoreCastOperatorNode(loc, exp), typeInfo(typeInfo), or_nothing(or_nothing) {
175  }
176 
177  DLLLOCAL virtual ~QoreComplexHashCastOperatorNode() = default;
178 
179  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
180  return typeInfo;
181  }
182 
183  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
184  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
185  if (*xsink)
186  return nullptr;
187  assert(typeInfo);
188  return new QoreComplexHashCastOperatorNode(loc, typeInfo, n_exp.release(), or_nothing);
189  }
190 
191  // checks if the value matches the expected type
192  DLLLOCAL virtual int checkValue(ExceptionSink* xsink, const QoreValue& val, bool lvalue) const;
193 
194 protected:
195  const QoreTypeInfo* typeInfo;
196  bool or_nothing;
197 
198  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
199 };
200 
201 class QoreComplexListCastOperatorNode : public QoreCastOperatorNode {
202 public:
203  DLLLOCAL QoreComplexListCastOperatorNode(const QoreProgramLocation* loc, const QoreTypeInfo* typeInfo,
204  QoreValue exp, bool or_nothing)
205  : QoreCastOperatorNode(loc, exp), typeInfo(typeInfo), or_nothing(or_nothing) {
206  }
207 
208  DLLLOCAL virtual ~QoreComplexListCastOperatorNode() = default;
209 
210  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
211  return typeInfo;
212  }
213 
214  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink* xsink) const {
215  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
216  if (*xsink)
217  return nullptr;
218  return new QoreComplexListCastOperatorNode(loc, typeInfo, n_exp.release(), or_nothing);
219  }
220 
221  // checks if the value matches the expected type
222  DLLLOCAL virtual int checkValue(ExceptionSink* xsink, const QoreValue& val, bool lvalue) const;
223 
224 protected:
225  const QoreTypeInfo* typeInfo;
226  bool or_nothing;
227 
228  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
229 };
230 
231 #endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
defines a Qore-language class
Definition: QoreClass.h:249
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
DLLEXPORT void concat(const QoreString *str, ExceptionSink *xsink)
concatenates a string and converts encodings if necessary
typed hash declaration
Definition: TypedHashDecl.h:44
holds an object and dereferences it in the destructor
Definition: QoreValue.h:476
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275