Qore Programming Language  0.9.16
QoreCastOperatorNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreCastOperatorNode.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_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 void parseInitImpl(QoreValue& val, LocalVar* oflag, int pflag, int& lvids, const QoreTypeInfo*& typeInfo);
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 void parseInitImpl(QoreValue& val, LocalVar* oflag, int pflag, int& lvids, const QoreTypeInfo*& typeInfo) {
103  }
104 };
105 
106 class QoreClassCastOperatorNode : public QoreCastOperatorNode {
107 public:
108  DLLLOCAL QoreClassCastOperatorNode(const QoreProgramLocation* loc, const QoreClass* qc, QoreValue exp,
109  bool or_nothing)
110  : QoreCastOperatorNode(loc, exp), qc(qc), or_nothing(or_nothing) {
111  }
112 
113  DLLLOCAL virtual ~QoreClassCastOperatorNode() = default;
114 
115  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
116  return qc ? qc->getTypeInfo() : objectTypeInfo;
117  }
118 
119  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
120  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
121  if (*xsink)
122  return nullptr;
123  return new QoreClassCastOperatorNode(loc, qc, n_exp.release(), or_nothing);
124  }
125 
126 protected:
127  const QoreClass* qc;
128  bool or_nothing;
129 
130  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
131 };
132 
133 class QoreHashDeclCastOperatorNode : public QoreCastOperatorNode {
134 public:
135  DLLLOCAL QoreHashDeclCastOperatorNode(const QoreProgramLocation* loc, const TypedHashDecl* hd, QoreValue exp,
136  bool or_nothing)
137  : QoreCastOperatorNode(loc, exp), hd(hd), or_nothing(or_nothing) {
138  }
139 
140  DLLLOCAL virtual ~QoreHashDeclCastOperatorNode() = default;
141 
142  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
143  return hd ? hd->getTypeInfo() : hashTypeInfo;
144  }
145 
146  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
147  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
148  if (*xsink)
149  return nullptr;
150  return new QoreHashDeclCastOperatorNode(loc, hd, n_exp.release(), or_nothing);
151  }
152 
153 protected:
154  const TypedHashDecl* hd;
155  bool or_nothing;
156 
157  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
158 };
159 
160 class QoreComplexHashCastOperatorNode : public QoreCastOperatorNode {
161 public:
162  DLLLOCAL QoreComplexHashCastOperatorNode(const QoreProgramLocation* loc, const QoreTypeInfo* typeInfo,
163  QoreValue exp, bool or_nothing)
164  : QoreCastOperatorNode(loc, exp), typeInfo(typeInfo), or_nothing(or_nothing) {
165  }
166 
167  DLLLOCAL virtual ~QoreComplexHashCastOperatorNode() = default;
168 
169  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
170  return typeInfo;
171  }
172 
173  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink *xsink) const {
174  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
175  if (*xsink)
176  return nullptr;
177  assert(typeInfo);
178  return new QoreComplexHashCastOperatorNode(loc, typeInfo, n_exp.release(), or_nothing);
179  }
180 
181 protected:
182  const QoreTypeInfo* typeInfo;
183  bool or_nothing;
184 
185  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
186 };
187 
188 class QoreComplexListCastOperatorNode : public QoreCastOperatorNode {
189 public:
190  DLLLOCAL QoreComplexListCastOperatorNode(const QoreProgramLocation* loc, const QoreTypeInfo* typeInfo,
191  QoreValue exp, bool or_nothing)
192  : QoreCastOperatorNode(loc, exp), typeInfo(typeInfo), or_nothing(or_nothing) {
193  }
194 
195  DLLLOCAL virtual ~QoreComplexListCastOperatorNode() = default;
196 
197  DLLLOCAL virtual const QoreTypeInfo* getTypeInfo() const {
198  return typeInfo;
199  }
200 
201  DLLLOCAL virtual QoreOperatorNode* copyBackground(ExceptionSink* xsink) const {
202  ValueHolder n_exp(copy_value_and_resolve_lvar_refs(exp, xsink), xsink);
203  if (*xsink)
204  return nullptr;
205  return new QoreComplexListCastOperatorNode(loc, typeInfo, n_exp.release(), or_nothing);
206  }
207 
208 protected:
209  const QoreTypeInfo* typeInfo;
210  bool or_nothing;
211 
212  DLLLOCAL virtual QoreValue evalImpl(bool& needs_deref, ExceptionSink* xsink) const;
213 };
214 
215 #endif
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
QoreClass
defines a Qore-language class
Definition: QoreClass.h:239
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
TypedHashDecl
typed hash declaration
Definition: TypedHashDecl.h:44
QoreString::concat
DLLEXPORT void concat(const QoreString *str, ExceptionSink *xsink)
concatenates a string and converts encodings if necessary
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
ValueHolder
holds an object and dereferences it in the destructor
Definition: QoreValue.h:452