Qore Programming Language  1.12.0
QoreNamespaceList.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreNamespaceList.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
8 
9  namespaces are children of a program object. there is a parse
10  lock per program object to ensure that objects are added (or backed out)
11  atomically per program object. All the objects referenced here should
12  be safe to read & copied at all times. They will only be deleted when the
13  program object is deleted (except the pending structures, which will be
14  deleted any time there is a parse error, together with all other
15  pending structures)
16 
17  Permission is hereby granted, free of charge, to any person obtaining a
18  copy of this software and associated documentation files (the "Software"),
19  to deal in the Software without restriction, including without limitation
20  the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  and/or sell copies of the Software, and to permit persons to whom the
22  Software is furnished to do so, subject to the following conditions:
23 
24  The above copyright notice and this permission notice shall be included in
25  all copies or substantial portions of the Software.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33  DEALINGS IN THE SOFTWARE.
34 
35  Note that the Qore library is released under a choice of three open-source
36  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
37  information.
38 */
39 
40 #ifndef _QORE_NAMESPACELIST_H
41 
42 #define _QORE_NAMESPACELIST_H
43 
44 // issue #3461: cannot use a vector map because we erase in the map while iterating
45 
46 #ifdef HAVE_QORE_HASH_MAP
47 //#warning compiling with hash_map
48 #include <qore/hash_map_include.h>
49 #include "qore/intern/xxhash.h"
50 
51 typedef HASH_MAP<std::string, QoreNamespace*> nsmap_t;
52 #else
53 #include <map>
54 typedef std::map<std::string, QoreNamespace*> nsmap_t;
55 #endif
56 
57 class qore_ns_private;
58 class qore_root_ns_private;
59 
60 hashdecl GVEntryBase {
61  NamedScope* name;
62  Var* var;
63 
64  DLLLOCAL GVEntryBase(const NamedScope& n, Var* v) : name(new NamedScope(n)), var(v) {
65  }
66 
67  DLLLOCAL GVEntryBase(const QoreProgramLocation* loc, char* n, const QoreTypeInfo* typeInfo,
68  QoreParseTypeInfo* parseTypeInfo, qore_var_t type);
69 
70  DLLLOCAL GVEntryBase(const GVEntryBase& old) : name(old.name), var(old.var) {
71  }
72 
73  DLLLOCAL void clear();
74 
75  DLLLOCAL Var* takeVar() {
76  Var* rv = var;
77  var = nullptr;
78  return rv;
79  }
80 };
81 
82 hashdecl GVEntry : public GVEntryBase {
83  qore_ns_private* ns;
84 
85  DLLLOCAL GVEntry(qore_ns_private* n_ns, const NamedScope& n, Var* v) : GVEntryBase(n, v), ns(n_ns) {
86  }
87 
88  DLLLOCAL GVEntry(const GVEntry& old) : GVEntryBase(old), ns(old.ns) {
89  }
90 
91  DLLLOCAL GVEntry(const GVEntryBase& old, qore_ns_private* n_ns) : GVEntryBase(old), ns(n_ns) {
92  }
93 };
94 
95 template <class T>
96 hashdecl GVList : public std::vector<T> {
97  DLLLOCAL ~GVList() {
98  clear();
99  }
100 
101  DLLLOCAL void clear() {
102  for (typename GVList<T>::iterator i = std::vector<T>::begin(), e = std::vector<T>::end(); i != e; ++i)
103  (*i).clear();
104  std::vector<T>::clear();
105  }
106 
107  DLLLOCAL void zero() {
108  std::vector<T>::clear();
109  }
110 };
111 
112 typedef GVList<GVEntryBase> gvblist_t;
113 typedef GVList<GVEntry> gvlist_t;
114 
115 class QoreNamespaceList {
116 private:
117  DLLLOCAL void deleteAll();
118 
119  // not implemented
120  DLLLOCAL QoreNamespaceList(const QoreNamespaceList& old);
121  // not implemented
122  DLLLOCAL QoreNamespaceList& operator=(const QoreNamespaceList& nsl);
123 
124 public:
125  nsmap_t nsmap;
126 
127  DLLLOCAL QoreNamespaceList() {
128  }
129 
130  DLLLOCAL QoreNamespaceList(const QoreNamespaceList& old, int64 po, const qore_ns_private& parent);
131 
132  DLLLOCAL ~QoreNamespaceList() {
133  deleteAll();
134  }
135 
136  DLLLOCAL QoreNamespace *find(const char* name) {
137  nsmap_t::iterator i = nsmap.find(name);
138  return i == nsmap.end() ? 0 : i->second;
139  }
140 
141  DLLLOCAL QoreNamespace *find(const std::string &name) {
142  nsmap_t::iterator i = nsmap.find(name);
143  return i == nsmap.end() ? 0 : i->second;
144  }
145  DLLLOCAL const QoreNamespace* find(const std::string& name) const {
146  nsmap_t::const_iterator i = nsmap.find(name);
147  return i == nsmap.end() ? 0 : i->second;
148  }
149 
150  // do not delete the pointer returned from this function
151  DLLLOCAL qore_ns_private* parseAdd(QoreNamespace* ot, qore_ns_private* parent);
152 
153  DLLLOCAL qore_ns_private* runtimeAdd(QoreNamespace* ot, qore_ns_private* parent);
154 
155  DLLLOCAL void resolveCopy();
156  DLLLOCAL int parseInitConstants();
157 
158  DLLLOCAL int parseInitGlobalVars();
159  DLLLOCAL void clearConstants(QoreListNode& l);
160  DLLLOCAL void clearData(ExceptionSink* sink);
161  DLLLOCAL void deleteGlobalVars(ExceptionSink* sink);
162 
163  DLLLOCAL void parseResolveHierarchy();
164  DLLLOCAL void parseResolveClassMembers();
165  DLLLOCAL int parseInit();
166  DLLLOCAL void parseResolveAbstract();
167  DLLLOCAL void parseCommit();
168  DLLLOCAL void parseCommitRuntimeInit(ExceptionSink* sink);
169  DLLLOCAL void parseRollback(ExceptionSink* sink);
170  DLLLOCAL void deleteAllConstants(ExceptionSink *xsink);
171  DLLLOCAL void reset();
172 
173  DLLLOCAL void parseAssimilate(QoreNamespaceList& n, qore_ns_private* parent);
174  DLLLOCAL void runtimeAssimilate(QoreNamespaceList& n, qore_ns_private* parent);
175 
176  DLLLOCAL bool addGlobalVars(qore_root_ns_private& rns);
177 
178  DLLLOCAL void deleteData(bool deref_vars, ExceptionSink *xsink);
179 
180  DLLLOCAL bool empty() const {
181  return nsmap.empty();
182  }
183 
184  DLLLOCAL size_t size() const {
185  return nsmap.size();
186  }
187 
188  DLLLOCAL void getGlobalVars(QoreHashNode& h) const;
189 };
190 
191 #endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
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:260