32#ifndef _QORE_VARIABLE_H
33#define _QORE_VARIABLE_H
45#include "qore/intern/RSet.h"
46#include "qore/intern/VRMutex.h"
47#include "qore/intern/QoreLValue.h"
48#include "qore/intern/qore_var_rwlock_priv.h"
49#include "qore/vector_set"
59class ScopedObjectCallNode;
60class QoreSquareBracketsOperatorNode;
61class QoreSquareBracketsRangeOperatorNode;
62class QoreHashObjectDereferenceOperatorNode;
64union qore_gvar_ref_u {
72 DLLLOCAL
void setPtr(Var* refptr,
bool readonly =
false) {
73 _refptr = (size_t)refptr;
78 DLLLOCAL Var* getPtr()
const {
79#ifndef HAVE_LLVM_BUG_22050
82 return (Var*)((_refptr & 1L) ? (_refptr ^ 1L) : _refptr);
84 return (Var*)(_refptr & (~1L));
88 DLLLOCAL
bool isReadOnly()
const {
96DLLLOCAL
void get_thread_local_lvalue(
void* ptr, QoreLValue<qore_gvar_ref_u>*& lvar,
bool& is_new,
bool& finalized);
99class LValueRemoveHelper;
105 const QoreProgramLocation* loc;
106 QoreLValue<qore_gvar_ref_u> val;
108 mutable QoreVarRWLock rwl;
109 QoreParseTypeInfo* parseTypeInfo =
nullptr;
110 const QoreTypeInfo* typeInfo =
nullptr;
111 const QoreTypeInfo* refTypeInfo =
nullptr;
113 mutable bool finalized;
114 bool is_thread_local;
118 Var(
const Var&) =
delete;
121 bool builtin =
false;
123 DLLLOCAL ~Var() {
delete parseTypeInfo; }
127 xsink->
raiseException(
"DESTRUCTOR-ERROR",
"illegal variable assignment after second phase of variable " \
134 QoreLValue<qore_gvar_ref_u>& getVal()
const {
135 if (is_thread_local) {
136 QoreLValue<qore_gvar_ref_u>* lvar;
138 get_thread_local_lvalue((
void*)
this, lvar, is_new, finalized);
142 lvar->v.setPtr(val.v.getPtr(), val.v.isReadOnly());
143 }
else if (typeInfo) {
149 return const_cast<QoreLValue<qore_gvar_ref_u>&
>(val);
153 DLLLOCAL Var(
const QoreProgramLocation* loc,
const char* n_name,
bool is_thread_local =
false)
154 : loc(loc), name(n_name), pub(false), finalized(false), is_thread_local(is_thread_local) {
157 DLLLOCAL Var(
const QoreProgramLocation* loc,
const char* n_name, QoreParseTypeInfo* n_parseTypeInfo,
158 bool is_thread_local =
false) : loc(loc), name(n_name), parseTypeInfo(n_parseTypeInfo),
159 pub(false), finalized(false), is_thread_local(is_thread_local) {
162 DLLLOCAL Var(
const QoreProgramLocation* loc,
const char* n_name,
const QoreTypeInfo* n_typeInfo,
163 bool builtin =
false,
bool is_thread_local =
false) : loc(loc), val(n_typeInfo), name(n_name),
164 typeInfo(n_typeInfo), pub(false), finalized(false), is_thread_local(is_thread_local), builtin(builtin) {
167 DLLLOCAL Var(Var* ref,
bool ro =
false,
bool is_thread_local =
false);
169 DLLLOCAL
const char* getName()
const;
171 DLLLOCAL
const std::string& getNameStr()
const {
175 DLLLOCAL
bool isBuiltin()
const {
179 DLLLOCAL
bool isThreadLocal()
const {
180 return is_thread_local;
183 DLLLOCAL
bool isGlobal()
const {
184 return !is_thread_local;
187 DLLLOCAL
int getLValue(LValueHelper& lvh,
bool for_remove)
const;
188 DLLLOCAL
void remove(LValueRemoveHelper& lvrh);
191 QoreLValue<qore_gvar_ref_u>& val = getVal();
194 QoreAutoVarRWWriteLocker al(rwl);
197 printd(5,
"Var::clearLocal() clearing '%s' %p\n", name.c_str(),
this);
202 h = val.assign(QoreTypeInfo::getDefaultQoreValue(typeInfo));
204 h = val.removeValue(
true);
210 printd(5,
"Var::clearLocal() skipping imported var '%s' %p\n", name.c_str(),
this);
215 QoreLValue<qore_gvar_ref_u>& val = getVal();
219 discard(val.assignInitial(v),
nullptr);
222 DLLLOCAL
const Var* parseGetVar()
const;
224 DLLLOCAL
bool isImported()
const;
230 DLLLOCAL
void doDoubleDeclarationError(
const QoreProgramLocation* loc) {
233 parse_error(*loc,
"global variable '%s' previously declared with type '%s'", name.c_str(), QoreParseTypeInfo::getName(parseTypeInfo));
237 parse_error(*loc,
"global variable '%s' previously declared with type '%s'", name.c_str(), QoreTypeInfo::getName(typeInfo));
238 assert(!parseTypeInfo);
242 DLLLOCAL
void checkAssignType(
const QoreProgramLocation* loc,
const QoreTypeInfo *n_typeInfo);
244 DLLLOCAL
int parseInit() {
245 QoreLValue<qore_gvar_ref_u>& val = getVal();
254 typeInfo = QoreParseTypeInfo::resolveAndDelete(parseTypeInfo, loc, err);
255 refTypeInfo = QoreTypeInfo::getReferenceTarget(typeInfo);
256 parseTypeInfo =
nullptr;
262 discard(val.assignInitial(QoreTypeInfo::getDefaultQoreValue(typeInfo)),
nullptr);
268 DLLLOCAL QoreParseTypeInfo* copyParseTypeInfo()
const {
269 return parseTypeInfo ? parseTypeInfo->copy() :
nullptr;
272 DLLLOCAL
const QoreTypeInfo* parseGetTypeInfoForInitialAssignment();
274 DLLLOCAL
const QoreTypeInfo* parseGetTypeInfo();
276 DLLLOCAL
const QoreTypeInfo* getTypeInfo()
const;
278 DLLLOCAL
bool hasTypeInfo()
const;
280 DLLLOCAL
bool isRef()
const;
283 DLLLOCAL
const char* getClassName()
const;
285 DLLLOCAL
bool isPublic()
const {
289 DLLLOCAL
void setPublic() {
294 DLLLOCAL
const QoreProgramLocation* getParseLocation()
const {
299DLLLOCAL
void delete_global_variables();
304typedef vector_set_t<const void*> lvid_set_t;
307hashdecl ObjCountRec {
316 DLLLOCAL
int getDifference();
319typedef std::vector<ObjCountRec> ocvec_t;
324 friend class LValueRemoveHelper;
325 friend class LValueLockHandoffHelper;
329 DLLLOCAL LValueHelper(
const LValueHelper&) =
delete;
330 DLLLOCAL LValueHelper& operator=(
const LValueHelper&) =
delete;
344 DLLLOCAL
int doListLValue(
const QoreSquareBracketsOperatorNode* op,
bool for_remove);
345 DLLLOCAL
int doHashLValue(
qore_type_t t,
const char* mem,
bool for_remove);
346 DLLLOCAL
int doObjLValue(
QoreObject* o,
const char* mem,
bool for_remove);
347 DLLLOCAL
int doHashObjLValue(
const QoreHashObjectDereferenceOperatorNode* op,
bool for_remove);
349 DLLLOCAL
int makeIntQv(
const char* desc);
350 DLLLOCAL
int makeIntVal(
const char* desc);
352 DLLLOCAL
int makeFloat(
const char* desc);
353 DLLLOCAL
int makeNumber(
const char* desc);
355 DLLLOCAL
int doRecursiveException() {
356 vl.xsink->raiseException(
"REFERENCE-ERROR",
"recursive reference detected in assignment");
364 typedef std::vector<AbstractQoreNode*> nvec_t;
366 lvid_set_t* lvid_set =
nullptr;
376 RObject* robj =
nullptr;
379 QoreLValueGeneric* val =
nullptr;
381 const QoreTypeInfo* typeInfo =
nullptr;
388 DLLLOCAL LValueHelper(LValueHelper&& o);
393 DLLLOCAL ~LValueHelper();
395 DLLLOCAL
int set(
const ReferenceNode& ref,
bool for_remove =
false);
397 DLLLOCAL
void setClosure(RObject* c) {
405 DLLLOCAL
int doLValue(
const QoreValue& exp,
bool for_remove);
407 DLLLOCAL
int doLValue(
const ReferenceNode* ref,
bool for_remove);
409 DLLLOCAL
void setAndLock(QoreVarRWLock& rwl);
410 DLLLOCAL
void set(QoreVarRWLock& rwl);
416 DLLLOCAL
void setTypeInfo(
const QoreTypeInfo* ti) {
422 DLLLOCAL
void setValue(QoreLValueGeneric& nv,
const QoreTypeInfo* ti =
nullptr) {
429 before = nv.assigned && nv.type ==
QV_Node ? needs_scan(nv.v.n) : false;
434 DLLLOCAL
void setValue(
QoreValue& nqv,
const QoreTypeInfo* ti =
nullptr) {
442 before = needs_scan(nqv);
447 DLLLOCAL
void resetValue(QoreLValueGeneric& nv,
const QoreTypeInfo* ti =
nullptr) {
457 before = nv.assigned && nv.type ==
QV_Node ? needs_scan(nv.v.n) : false;
462 DLLLOCAL
void resetValue(
QoreValue& nqv,
const QoreTypeInfo* ti =
nullptr) {
472 before = needs_scan(nqv);
477 DLLLOCAL
void clearPtr() {
487 DLLLOCAL
operator bool()
const {
491 DLLLOCAL
bool isOptimized()
const {
492 return val && val->optimized();
495 DLLLOCAL
const QoreTypeInfo* getTypeInfo()
const {
500 return val ? val->getType() : qv->
getType();
505 return val->getReference();
511 DLLLOCAL
const QoreValue getValue()
const {
512 return val ? val->getValue() : *qv;
516 return val ? val->getValue() : *qv;
520 assert((val && val->getInternalNode()) || (qv && qv->
getInternalNode()));
524 DLLLOCAL
const char* getTypeName()
const {
525 return val ? val->getTypeName() : qv->
getTypeName();
528 DLLLOCAL
bool checkType(
const qore_type_t t)
const {
529 return getType() == t;
532 DLLLOCAL
bool isNothing()
const {
536 DLLLOCAL
void setObjectContext(qore_object_private* obj);
538 DLLLOCAL
QoreValue getReferencedValue()
const;
542 DLLLOCAL
void ensureUnique() {
549 assignNodeIntern(current_value->
realCopy());
554 DLLLOCAL
int64 getAsBigInt()
const;
555 DLLLOCAL
bool getAsBool()
const;
556 DLLLOCAL
double getAsFloat()
const;
558 DLLLOCAL
int64 plusEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
559 DLLLOCAL
int64 minusEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
560 DLLLOCAL
int64 multiplyEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
561 DLLLOCAL
int64 divideEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
562 DLLLOCAL
int64 orEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
563 DLLLOCAL
int64 andEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
564 DLLLOCAL
int64 xorEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
565 DLLLOCAL
int64 modulaEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
566 DLLLOCAL
int64 shiftLeftEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
567 DLLLOCAL
int64 shiftRightEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
568 DLLLOCAL
int64 preIncrementBigInt(
const char* desc =
"<lvalue>");
569 DLLLOCAL
int64 preDecrementBigInt(
const char* desc =
"<lvalue>");
570 DLLLOCAL
int64 postIncrementBigInt(
const char* desc =
"<lvalue>");
571 DLLLOCAL
int64 postDecrementBigInt(
const char* desc =
"<lvalue>");
573 DLLLOCAL
double plusEqualsFloat(
double v,
const char* desc =
"<lvalue>");
574 DLLLOCAL
double minusEqualsFloat(
double v,
const char* desc =
"<lvalue>");
575 DLLLOCAL
double multiplyEqualsFloat(
double v,
const char* desc =
"<lvalue>");
576 DLLLOCAL
double divideEqualsFloat(
double v,
const char* desc =
"<lvalue>");
577 DLLLOCAL
double preIncrementFloat(
const char* desc =
"<lvalue>");
578 DLLLOCAL
double preDecrementFloat(
const char* desc =
"<lvalue>");
579 DLLLOCAL
double postIncrementFloat(
const char* desc =
"<lvalue>");
580 DLLLOCAL
double postDecrementFloat(
const char* desc =
"<lvalue>");
582 DLLLOCAL
void plusEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
583 DLLLOCAL
void minusEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
584 DLLLOCAL
void multiplyEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
585 DLLLOCAL
void divideEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
586 DLLLOCAL
void preIncrementNumber(
const char* desc =
"<lvalue>");
587 DLLLOCAL
void preDecrementNumber(
const char* desc =
"<lvalue>");
588 DLLLOCAL
QoreNumberNode* postIncrementNumber(
bool ref_rv,
const char* desc =
"<lvalue>");
589 DLLLOCAL
QoreNumberNode* postDecrementNumber(
bool ref_rv,
const char* desc =
"<lvalue>");
591 DLLLOCAL
QoreNumberNode* ensureUniqueNumber(
const char* desc =
"<lvalue>") {
594 if (makeNumber(desc))
600 if (makeNumber(desc))
606 if (!(*p)->is_unique()) {
614 DLLLOCAL
int assign(
QoreValue val,
const char* desc =
"<lvalue>",
bool check_types =
true,
bool weak_assignment =
false);
616 DLLLOCAL
QoreValue removeValue(
bool for_del);
617 DLLLOCAL
QoreValue remove(
bool& static_assignment);
619 DLLLOCAL
void setDelta(
int dt) {
625class LValueRemoveHelper {
628 LValueRemoveHelper(
const LValueRemoveHelper&) =
delete;
629 LValueRemoveHelper& operator=(
const LValueRemoveHelper&) =
delete;
630 void*
operator new(size_t) =
delete;
632 DLLLOCAL
void doRemove(
const QoreSquareBracketsOperatorNode* op);
633 DLLLOCAL
void doRemove(
const QoreSquareBracketsOperatorNode* op,
const QoreParseListNode* l);
634 DLLLOCAL
void doRemove(
const QoreSquareBracketsRangeOperatorNode* op);
638 QoreLValueGeneric rv;
648 DLLLOCAL
operator bool()
const {
656 DLLLOCAL
bool forDel()
const {
660 DLLLOCAL
void doRemove(QoreLValueGeneric& qv,
const QoreTypeInfo* ti) {
664 rv.assignSetTakeInitial(qv, QoreTypeInfo::getDefaultQoreValue(ti));
666 rv.assignSetTakeInitial(qv);
671 DLLLOCAL
QoreValue remove(
bool& static_assignment);
673 DLLLOCAL
void deleteLValue();
677 QoreLValueGeneric* val;
678 const QoreTypeInfo* typeInfo;
680 DLLLOCAL lvinfo(QoreLValueGeneric* val,
const QoreTypeInfo* typeInfo) : val(val), typeInfo(typeInfo) {
static void discard(AbstractQoreNode *n, ExceptionSink *xsink)
to deref an AbstractQoreNode (when the pointer may be 0)
Definition QoreLib.h:324
#define PO_STRICT_TYPES
enforce strict type checking and setting default values
Definition Restrictions.h:98
The base class for all value and parse types in Qore expression trees.
Definition AbstractQoreNode.h:57
virtual DLLEXPORT AbstractQoreNode * realCopy() const =0
returns a copy of the object; the caller owns the reference count
DLLLOCAL qore_type_t getType() const
returns the data type
Definition AbstractQoreNode.h:175
AutoVLock is a container for safely managing global variable and object lock handovers,...
Definition AutoVLock.h:80
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition ExceptionSink.h:50
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition QoreHashNode.h:51
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition QoreListNode.h:52
Qore's arbitrary-precision number value type, dynamically-allocated only, reference counted.
Definition QoreNumberNode.h:51
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition QoreObject.h:61
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only
Definition QoreProgram.h:128
DLLEXPORT int64 getParseOptions64() const
returns the parse options currently set for this program
Provides atomic reference counting to Qore objects.
Definition QoreReferenceCounter.h:44
DLLEXPORT bool is_unique() const
Returns true if the reference count is 1.
qore_value_u v
the actual value is stored here
Definition QoreValue.h:116
DLLLOCAL detail::QoreValueCastHelper< T >::Result get()
returns the value as the given type
Definition QoreValue.h:214
DLLEXPORT AbstractQoreNode * getInternalNode()
returns any AbstractQoreNode value held; if type != QV_Node, returns nullptr
DLLEXPORT qore_type_t getType() const
returns the type of value contained
DLLEXPORT const char * getTypeName() const
returns a string type description of the value contained (ex: "nothing" for a null AbstractQoreNode p...
parse type: reference to a lvalue expression
Definition ReferenceNode.h:45
holds an object and dereferences it in the destructor
Definition QoreValue.h:487
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode)
Definition common.h:76
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition common.h:266
#define QV_Node
for heap-allocated values
Definition QoreValue.h:46
#define QV_Ref
for references (when used with lvalues)
Definition QoreValue.h:47
const qore_type_t NT_NUMBER
type value for QoreNumberNode
Definition node_types.h:53
const qore_type_t NT_NOTHING
type value for QoreNothingNode
Definition node_types.h:42
const qore_type_t NT_OBJECT
type value for QoreObject
Definition node_types.h:52
const qore_type_t NT_REFERENCE
type value for ReferenceNode
Definition node_types.h:64
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
The main value class in Qore, designed to be passed by value.
Definition QoreValue.h:279
AbstractQoreNode * n
for all heap-allocated values
Definition QoreValue.h:60