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) {
168 DLLLOCAL Var(Var* ref,
bool ro =
false,
bool is_thread_local =
false,
const char* import_as =
nullptr);
170 DLLLOCAL
const char* getName()
const;
172 DLLLOCAL
const std::string& getNameStr()
const {
176 DLLLOCAL
bool isBuiltin()
const {
180 DLLLOCAL
bool isThreadLocal()
const {
181 return is_thread_local;
184 DLLLOCAL
bool isGlobal()
const {
185 return !is_thread_local;
188 DLLLOCAL
int getLValue(LValueHelper& lvh,
bool for_remove)
const;
189 DLLLOCAL
void remove(LValueRemoveHelper& lvrh);
192 QoreLValue<qore_gvar_ref_u>& val = getVal();
195 QoreAutoVarRWWriteLocker al(rwl);
198 printd(5,
"Var::clearLocal() clearing '%s' %p\n", name.c_str(),
this);
203 h = val.assign(QoreTypeInfo::getDefaultQoreValue(typeInfo));
205 h = val.removeValue(
true);
211 printd(5,
"Var::clearLocal() skipping imported var '%s' %p\n", name.c_str(),
this);
216 QoreLValue<qore_gvar_ref_u>& val = getVal();
220 discard(val.assignInitial(v),
nullptr);
223 DLLLOCAL
const Var* parseGetVar()
const;
225 DLLLOCAL
bool isImported()
const;
231 DLLLOCAL
void doDoubleDeclarationError(
const QoreProgramLocation* loc) {
234 parse_error(*loc,
"global variable '%s' previously declared with type '%s'", name.c_str(),
235 QoreParseTypeInfo::getName(parseTypeInfo));
239 parse_error(*loc,
"global variable '%s' previously declared with type '%s'", name.c_str(),
240 QoreTypeInfo::getName(typeInfo));
241 assert(!parseTypeInfo);
245 DLLLOCAL
void checkAssignType(
const QoreProgramLocation* loc,
const QoreTypeInfo *n_typeInfo);
247 DLLLOCAL
int parseInit() {
248 QoreLValue<qore_gvar_ref_u>& val = getVal();
257 typeInfo = QoreParseTypeInfo::resolveAndDelete(parseTypeInfo, loc, err);
258 refTypeInfo = QoreTypeInfo::getReferenceTarget(typeInfo);
259 parseTypeInfo =
nullptr;
265 discard(val.assignInitial(QoreTypeInfo::getDefaultQoreValue(typeInfo)),
nullptr);
271 DLLLOCAL QoreParseTypeInfo* copyParseTypeInfo()
const {
272 return parseTypeInfo ? parseTypeInfo->copy() :
nullptr;
275 DLLLOCAL
const QoreTypeInfo* parseGetTypeInfoForInitialAssignment();
277 DLLLOCAL
const QoreTypeInfo* parseGetTypeInfo();
279 DLLLOCAL
const QoreTypeInfo* getTypeInfo()
const;
281 DLLLOCAL
bool hasTypeInfo()
const;
283 DLLLOCAL
bool isRef()
const;
286 DLLLOCAL
const char* getClassName()
const;
288 DLLLOCAL
bool isPublic()
const {
292 DLLLOCAL
void setPublic() {
297 DLLLOCAL
const QoreProgramLocation* getParseLocation()
const {
302DLLLOCAL
void delete_global_variables();
307typedef vector_set_t<const void*> lvid_set_t;
310hashdecl ObjCountRec {
319 DLLLOCAL
int getDifference();
322typedef std::vector<ObjCountRec> ocvec_t;
327 friend class LValueRemoveHelper;
328 friend class LValueLockHandoffHelper;
332 DLLLOCAL LValueHelper(
const LValueHelper&) =
delete;
333 DLLLOCAL LValueHelper& operator=(
const LValueHelper&) =
delete;
347 DLLLOCAL
int doListLValue(
const QoreSquareBracketsOperatorNode* op,
bool for_remove);
348 DLLLOCAL
int doHashLValue(
qore_type_t t,
const char* mem,
bool for_remove);
349 DLLLOCAL
int doObjLValue(
QoreObject* o,
const char* mem,
bool for_remove);
350 DLLLOCAL
int doHashObjLValue(
const QoreHashObjectDereferenceOperatorNode* op,
bool for_remove);
352 DLLLOCAL
int makeIntQv(
const char* desc);
353 DLLLOCAL
int makeIntVal(
const char* desc);
355 DLLLOCAL
int makeFloat(
const char* desc);
356 DLLLOCAL
int makeNumber(
const char* desc);
358 DLLLOCAL
int doRecursiveException() {
359 vl.xsink->raiseException(
"REFERENCE-ERROR",
"recursive reference detected in assignment");
367 typedef std::vector<AbstractQoreNode*> nvec_t;
369 lvid_set_t* lvid_set =
nullptr;
379 RObject* robj =
nullptr;
382 QoreLValueGeneric* val =
nullptr;
384 const QoreTypeInfo* typeInfo =
nullptr;
391 DLLLOCAL LValueHelper(LValueHelper&& o);
396 DLLLOCAL ~LValueHelper();
398 DLLLOCAL
int set(
const ReferenceNode& ref,
bool for_remove =
false);
400 DLLLOCAL
void setClosure(RObject* c) {
408 DLLLOCAL
int doLValue(
const QoreValue& exp,
bool for_remove);
410 DLLLOCAL
int doLValue(
const ReferenceNode* ref,
bool for_remove);
412 DLLLOCAL
void setAndLock(QoreVarRWLock& rwl);
413 DLLLOCAL
void set(QoreVarRWLock& rwl);
419 DLLLOCAL
void setTypeInfo(
const QoreTypeInfo* ti) {
425 DLLLOCAL
void setValue(QoreLValueGeneric& nv,
const QoreTypeInfo* ti =
nullptr) {
432 before = nv.assigned && nv.type ==
QV_Node ? needs_scan(nv.v.n) : false;
437 DLLLOCAL
void setValue(
QoreValue& nqv,
const QoreTypeInfo* ti =
nullptr) {
445 before = needs_scan(nqv);
450 DLLLOCAL
void resetValue(QoreLValueGeneric& nv,
const QoreTypeInfo* ti =
nullptr) {
460 before = nv.assigned && nv.type ==
QV_Node ? needs_scan(nv.v.n) : false;
465 DLLLOCAL
void resetValue(
QoreValue& nqv,
const QoreTypeInfo* ti =
nullptr) {
475 before = needs_scan(nqv);
480 DLLLOCAL
void clearPtr() {
490 DLLLOCAL
operator bool()
const {
494 DLLLOCAL
bool isOptimized()
const {
495 return val && val->optimized();
498 DLLLOCAL
const QoreTypeInfo* getTypeInfo()
const {
503 return val ? val->getType() : qv->
getType();
508 return val->getReference();
514 DLLLOCAL
const QoreValue getValue()
const {
515 return val ? val->getValue() : *qv;
519 return val ? val->getValue() : *qv;
523 assert((val && val->getInternalNode()) || (qv && qv->
getInternalNode()));
527 DLLLOCAL
const char* getTypeName()
const {
528 return val ? val->getTypeName() : qv->
getTypeName();
531 DLLLOCAL
bool checkType(
const qore_type_t t)
const {
532 return getType() == t;
535 DLLLOCAL
bool isNothing()
const {
539 DLLLOCAL
void setObjectContext(qore_object_private* obj);
541 DLLLOCAL
QoreValue getReferencedValue()
const;
545 DLLLOCAL
void ensureUnique() {
552 assignNodeIntern(current_value->
realCopy());
557 DLLLOCAL
int64 getAsBigInt()
const;
558 DLLLOCAL
bool getAsBool()
const;
559 DLLLOCAL
double getAsFloat()
const;
561 DLLLOCAL
int64 plusEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
562 DLLLOCAL
int64 minusEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
563 DLLLOCAL
int64 multiplyEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
564 DLLLOCAL
int64 divideEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
565 DLLLOCAL
int64 orEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
566 DLLLOCAL
int64 andEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
567 DLLLOCAL
int64 xorEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
568 DLLLOCAL
int64 modulaEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
569 DLLLOCAL
int64 shiftLeftEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
570 DLLLOCAL
int64 shiftRightEqualsBigInt(
int64 v,
const char* desc =
"<lvalue>");
571 DLLLOCAL
int64 preIncrementBigInt(
const char* desc =
"<lvalue>");
572 DLLLOCAL
int64 preDecrementBigInt(
const char* desc =
"<lvalue>");
573 DLLLOCAL
int64 postIncrementBigInt(
const char* desc =
"<lvalue>");
574 DLLLOCAL
int64 postDecrementBigInt(
const char* desc =
"<lvalue>");
576 DLLLOCAL
double plusEqualsFloat(
double v,
const char* desc =
"<lvalue>");
577 DLLLOCAL
double minusEqualsFloat(
double v,
const char* desc =
"<lvalue>");
578 DLLLOCAL
double multiplyEqualsFloat(
double v,
const char* desc =
"<lvalue>");
579 DLLLOCAL
double divideEqualsFloat(
double v,
const char* desc =
"<lvalue>");
580 DLLLOCAL
double preIncrementFloat(
const char* desc =
"<lvalue>");
581 DLLLOCAL
double preDecrementFloat(
const char* desc =
"<lvalue>");
582 DLLLOCAL
double postIncrementFloat(
const char* desc =
"<lvalue>");
583 DLLLOCAL
double postDecrementFloat(
const char* desc =
"<lvalue>");
585 DLLLOCAL
void plusEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
586 DLLLOCAL
void minusEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
587 DLLLOCAL
void multiplyEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
588 DLLLOCAL
void divideEqualsNumber(
QoreValue r,
const char* desc =
"<lvalue>");
589 DLLLOCAL
void preIncrementNumber(
const char* desc =
"<lvalue>");
590 DLLLOCAL
void preDecrementNumber(
const char* desc =
"<lvalue>");
591 DLLLOCAL
QoreNumberNode* postIncrementNumber(
bool ref_rv,
const char* desc =
"<lvalue>");
592 DLLLOCAL
QoreNumberNode* postDecrementNumber(
bool ref_rv,
const char* desc =
"<lvalue>");
594 DLLLOCAL
QoreNumberNode* ensureUniqueNumber(
const char* desc =
"<lvalue>") {
597 if (makeNumber(desc))
603 if (makeNumber(desc))
609 if (!(*p)->is_unique()) {
617 DLLLOCAL
int assign(
QoreValue val,
const char* desc =
"<lvalue>",
bool check_types =
true,
bool weak_assignment =
false);
619 DLLLOCAL
QoreValue removeValue(
bool for_del);
620 DLLLOCAL
QoreValue remove(
bool& static_assignment);
622 DLLLOCAL
void setDelta(
int dt) {
628class LValueRemoveHelper {
631 LValueRemoveHelper(
const LValueRemoveHelper&) =
delete;
632 LValueRemoveHelper& operator=(
const LValueRemoveHelper&) =
delete;
633 void*
operator new(size_t) =
delete;
635 DLLLOCAL
void doRemove(
const QoreSquareBracketsOperatorNode* op);
636 DLLLOCAL
void doRemove(
const QoreSquareBracketsOperatorNode* op,
const QoreParseListNode* l);
637 DLLLOCAL
void doRemove(
const QoreSquareBracketsRangeOperatorNode* op);
641 QoreLValueGeneric rv;
651 DLLLOCAL
operator bool()
const {
659 DLLLOCAL
bool forDel()
const {
663 DLLLOCAL
void doRemove(QoreLValueGeneric& qv,
const QoreTypeInfo* ti) {
667 rv.assignSetTakeInitial(qv, QoreTypeInfo::getDefaultQoreValue(ti));
669 rv.assignSetTakeInitial(qv);
674 DLLLOCAL
QoreValue remove(
bool& static_assignment);
676 DLLLOCAL
void deleteLValue();
680 QoreLValueGeneric* val;
681 const QoreTypeInfo* typeInfo;
683 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