Qore Programming Language  1.12.0
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 protected:
102  DLLLOCAL virtual int parseInitImpl(QoreValue& val, QoreParseContext& parse_context) {
103  return 0;
104  }
105 };
106 
107 class QoreClassCastOperatorNode : public QoreCastOperatorNode {
108 public:
109  DLLLOCAL QoreClassCastOperatorNode(const QoreProgramLocation* loc, const QoreClass* qc, QoreValue exp,
110  bool or_nothing)
111  : QoreCastOperatorNode(loc, exp), qc(qc), or_nothing(or_nothing) {
112  }
113 
114  DLLLOCAL virtual ~QoreClassCastOperatorNode() = default;
115 
116  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
117  return qc ? qc->getTypeInfo() : objectTypeInfo;
118  }
119 
120  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
121  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
122  if (*xsink)
123  return nullptr;
124  return new QoreClassCastOperatorNode(loc, qc, n_exp.release(), or_nothing);
125  }
126 
127 protected:
128  const QoreClass* qc;
129  bool or_nothing;
130 
131  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
132 };
133 
134 class QoreHashDeclCastOperatorNode : public QoreCastOperatorNode {
135 public:
136  DLLLOCAL QoreHashDeclCastOperatorNode(const QoreProgramLocation* loc, const TypedHashDecl* hd, QoreValue exp,
137  bool or_nothing)
138  : QoreCastOperatorNode(loc, exp), hd(hd), or_nothing(or_nothing) {
139  }
140 
141  DLLLOCAL virtual ~QoreHashDeclCastOperatorNode() = default;
142 
143  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
144  return hd ? hd->getTypeInfo() : hashTypeInfo;
145  }
146 
147  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
148  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
149  if (*xsink)
150  return nullptr;
151  return new QoreHashDeclCastOperatorNode(loc, hd, n_exp.release(), or_nothing);
152  }
153 
154 protected:
155  const TypedHashDecl* hd;
156  bool or_nothing;
157 
158  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
159 };
160 
161 class QoreComplexHashCastOperatorNode : public QoreCastOperatorNode {
162 public:
163  DLLLOCAL QoreComplexHashCastOperatorNode(const QoreProgramLocation* loc, const QoreTypeInfo* typeInfo,
164  QoreValue exp, bool or_nothing)
165  : QoreCastOperatorNode(loc, exp), typeInfo(typeInfo), or_nothing(or_nothing) {
166  }
167 
168  DLLLOCAL virtual ~QoreComplexHashCastOperatorNode() = default;
169 
170  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
171  return typeInfo;
172  }
173 
174  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
175  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
176  if (*xsink)
177  return nullptr;
178  assert(typeInfo);
179  return new QoreComplexHashCastOperatorNode(loc, typeInfo, n_exp.release(), or_nothing);
180  }
181 
182 protected:
183  const QoreTypeInfo* typeInfo;
184  bool or_nothing;
185 
186  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
187 };
188 
189 class QoreComplexListCastOperatorNode : public QoreCastOperatorNode {
190 public:
191  DLLLOCAL QoreComplexListCastOperatorNode(const QoreProgramLocation* loc, const QoreTypeInfo* typeInfo,
192  QoreValue exp, bool or_nothing)
193  : QoreCastOperatorNode(loc, exp), typeInfo(typeInfo), or_nothing(or_nothing) {
194  }
195 
196  DLLLOCAL virtual ~QoreComplexListCastOperatorNode() = default;
197 
198  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
199  return typeInfo;
200  }
201 
202  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink* xsink) const {
203  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
204  if (*xsink)
205  return nullptr;
206  return new QoreComplexListCastOperatorNode(loc, typeInfo, n_exp.release(), or_nothing);
207  }
208 
209 protected:
210  const QoreTypeInfo* typeInfo;
211  bool or_nothing;
212 
213  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
214 };
215 
216 #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