37 #ifndef _QORE_CONSTANTLIST_H
39 #define _QORE_CONSTANTLIST_H
42 #include "qore/intern/ParseNode.h"
46 class qore_ns_private;
47 class qore_class_private;
55 DLLLOCAL ClassNs(qore_class_private* qc) : ptr((size_t)qc) {
58 DLLLOCAL ClassNs(qore_ns_private* ns) {
59 ptr = ((size_t)ns) | (size_t)1;
62 DLLLOCAL ClassNs(
const ClassNs& old) : ptr(old.ptr) {
65 DLLLOCAL qore_class_private* getClass()
const {
66 return (!(ptr & (
size_t)1)) ? (qore_class_private*)ptr :
nullptr;
69 DLLLOCAL qore_ns_private* getNs()
const {
70 return (ptr & (
size_t)1) ? (qore_ns_private*)(ptr & ~(
size_t)1) :
nullptr;
73 DLLLOCAL
bool isNs()
const {
74 return (
bool)(ptr & (size_t)1);
78 DLLLOCAL
const char* getType()
const {
79 return isNs() ?
"namespace" :
"class";
82 DLLLOCAL
const char* getName()
const;
86 class RuntimeConstantRefNode;
89 friend class ConstantEntryInitHelper;
90 friend class RuntimeConstantRefNode;
91 friend class ConstantList;
94 const QoreProgramLocation* loc;
97 const QoreTypeInfo* typeInfo;
105 DLLLOCAL ConstantEntry(
const QoreProgramLocation* loc,
const char* n,
QoreValue v,
106 const QoreTypeInfo* ti =
nullptr,
bool n_pub =
false,
bool n_init =
false,
bool n_builtin =
false,
107 ClassAccess n_access = Public);
109 DLLLOCAL ConstantEntry(
const ConstantEntry& old);
125 DLLLOCAL
void ref() {
129 DLLLOCAL ConstantEntry* refSelf() {
134 DLLLOCAL
int parseInit(ClassNs ptr);
136 DLLLOCAL
QoreValue get(
const QoreProgramLocation* loc,
const QoreTypeInfo*& constantTypeInfo, ClassNs ptr) {
138 parse_error(*loc,
"recursive constant reference found to constant '%s'", name.c_str());
139 constantTypeInfo = nothingTypeInfo;
143 if (!init && parseInit(ptr)) {
144 constantTypeInfo = nothingTypeInfo;
148 constantTypeInfo = typeInfo;
152 DLLLOCAL
const char* getName()
const {
156 DLLLOCAL
const std::string& getNameStr()
const {
160 DLLLOCAL
bool isPublic()
const {
164 DLLLOCAL
bool isUserPublic()
const {
165 return pub && !builtin;
168 DLLLOCAL
bool isSystem()
const {
172 DLLLOCAL
bool isUser()
const {
176 DLLLOCAL ClassAccess getAccess()
const {
180 DLLLOCAL
const char* getModuleName()
const {
181 return from_module.empty() ? nullptr : from_module.c_str();
187 std::string from_module;
189 DLLLOCAL ~ConstantEntry() {
191 assert(val.isNothing());
194 DLLLOCAL
int scanValue(
const QoreValue& n)
const;
199 DLLLOCAL
void setModuleName() {
200 assert(from_module.empty());
201 const char* mod_name = get_module_context_name();
203 from_module = mod_name;
209 class ConstantEntryInitHelper {
214 DLLLOCAL ConstantEntryInitHelper(ConstantEntry& n_ce) : ce(n_ce) {
221 DLLLOCAL ~ConstantEntryInitHelper() {
230 #include <qore/vector_map>
231 typedef vector_map_t<const char*, ConstantEntry*> cnemap_t;
246 friend class ConstantListIterator;
247 friend class ConstConstantListIterator;
251 DLLLOCAL ConstantList& operator=(
const ConstantList&);
260 vector_map_t<std::string, ConstantEntry*> new_cnemap;
263 DLLLOCAL ~ConstantList();
265 DLLLOCAL ConstantList(ClassNs p) : ptr(p) {
269 DLLLOCAL ConstantList(
const ConstantList& old,
int64 po, ClassNs p);
272 DLLLOCAL cnemap_t::iterator add(
const char* name,
QoreValue val,
const QoreTypeInfo* typeInfo =
nullptr, ClassAccess access = Public);
274 DLLLOCAL cnemap_t::iterator parseAdd(
const QoreProgramLocation* loc,
const char* name,
QoreValue val,
const QoreTypeInfo* typeInfo =
nullptr,
bool pub =
false, ClassAccess access = Public);
276 DLLLOCAL ConstantEntry* findEntry(
const char* name);
278 DLLLOCAL
const ConstantEntry* findEntry(
const char* name)
const;
280 DLLLOCAL
QoreValue find(
const char* name,
const QoreTypeInfo*& constantTypeInfo, ClassAccess& access,
bool& found);
282 DLLLOCAL
QoreValue find(
const char* name,
const QoreTypeInfo*& constantTypeInfo,
bool& found) {
284 return find(name, constantTypeInfo, access, found);
287 DLLLOCAL
bool inList(
const char* name)
const;
288 DLLLOCAL
bool inList(
const std::string& name)
const;
292 DLLLOCAL
void assimilate(ConstantList& n);
295 DLLLOCAL
void assimilate(ConstantList& n,
const char* type,
const char* name);
298 DLLLOCAL
void mergeUserPublic(
const ConstantList& src);
300 DLLLOCAL
int importSystemConstants(
const ConstantList& src,
ExceptionSink* xsink);
303 DLLLOCAL
void parseAdd(
const QoreProgramLocation* loc,
const std::string& name,
QoreValue val, ClassAccess access,
const char* cname);
305 DLLLOCAL
void parseInit();
307 DLLLOCAL
void parseDeleteAll();
310 DLLLOCAL
void reset();
312 DLLLOCAL
bool empty()
const {
313 return cnemap.empty();
316 DLLLOCAL cnemap_t::iterator end() {
320 DLLLOCAL cnemap_t::const_iterator end()
const {
324 DLLLOCAL
void setAccess(ClassAccess access) {
325 for (
auto& i : cnemap)
326 i.second->access = access;
330 class ConstantListIterator {
333 cnemap_t::iterator i;
336 DLLLOCAL ConstantListIterator(ConstantList& n_cl) : cl(n_cl.cnemap), i(cl.end()) {
339 DLLLOCAL
bool next() {
344 return i != cl.end();
347 DLLLOCAL
const std::string& getName()
const {
348 return i->second->getNameStr();
352 return i->second->val;
355 DLLLOCAL ConstantEntry* getEntry()
const {
359 DLLLOCAL ClassAccess getAccess()
const {
360 return i->second->getAccess();
363 DLLLOCAL
bool isPublic()
const {
364 return i->second->isPublic();
367 DLLLOCAL
bool isUserPublic()
const {
368 return i->second->isUserPublic();
372 class ConstConstantListIterator {
375 cnemap_t::const_iterator i;
378 DLLLOCAL ConstConstantListIterator(
const ConstantList& n_cl) : cl(n_cl.cnemap), i(cl.end()) {
381 DLLLOCAL
bool next() {
386 return i != cl.end();
389 DLLLOCAL
const std::string& getName()
const {
390 return i->second->getNameStr();
393 DLLLOCAL
const QoreValue getValue()
const {
394 return i->second->val;
397 DLLLOCAL
const ConstantEntry* getEntry()
const {
401 DLLLOCAL
bool isPublic()
const {
402 return i->second->isPublic();
405 DLLLOCAL
bool isUserPublic()
const {
406 return i->second->isUserPublic();
410 class RuntimeConstantRefNode :
public ParseNode {
414 DLLLOCAL
virtual void parseInitImpl(
QoreValue& val, LocalVar* oflag,
int pflag,
int& lvids,
const QoreTypeInfo*& typeInfo) {
417 DLLLOCAL
virtual const QoreTypeInfo* getTypeInfo()
const {
418 assert(ce->saved_node);
419 return getTypeInfoForValue(ce->saved_node);
423 assert(ce->saved_node);
424 return ce->saved_node->eval(needs_deref, xsink);
427 DLLLOCAL ~RuntimeConstantRefNode() {
431 DLLLOCAL RuntimeConstantRefNode(
const QoreProgramLocation* loc, ConstantEntry* n_ce) : ParseNode(loc,
NT_RTCONSTREF, true, false), ce(n_ce) {
432 assert(ce->saved_node);
436 assert(ce->saved_node);
437 return ce->saved_node->getAsString(str, foff, xsink);
441 assert(ce->saved_node);
442 return ce->saved_node->getAsString(del, foff, xsink);
445 DLLLOCAL
virtual const char* getTypeName()
const {
446 return ce->saved_node ? ce->saved_node->getTypeName() :
"nothing";
450 #endif // _QORE_CONSTANTLIST_H