Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
NamedScope.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 NamedScope.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 NamedScopes 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_NAMEDSCOPE_H
41
42#define QORE_NAMEDSCOPE_H
43
44#include <vector>
45#include <string>
46
47// for parsing namespace/class scope resolution
48class NamedScope {
49protected:
50 typedef std::vector<std::string> nslist_t;
51
52 nslist_t* strlist = nullptr;
53 bool del;
54
55 DLLLOCAL void init();
56
57public:
58 char* ostr;
59
60 DLLLOCAL NamedScope(char *str) : del(true), ostr(str) {
61 assert(str);
62 init();
63 }
64
65 DLLLOCAL NamedScope(const char *str) : del(false), ostr((char *)str) {
66 assert(str);
67 init();
68 }
69
70 // takes all values from and deletes the argument
71 DLLLOCAL NamedScope(NamedScope* ns) : strlist(ns->strlist), del(ns->del), ostr(ns->ostr) {
72 ns->strlist = nullptr;
73 ns->ostr = nullptr;
74 delete ns;
75 }
76
77 DLLLOCAL NamedScope(const NamedScope& old) : del(true), ostr(strdup(old.ostr)) {
78 if (old.strlist) {
79 strlist = new nslist_t(*old.strlist);
80 }
81 else {
82 strlist = nullptr;
83 }
84 }
85
86 DLLLOCAL ~NamedScope() {
87 clear();
88 }
89
90 DLLLOCAL void clear() {
91 if (ostr && del)
92 free(ostr);
93 if (strlist) {
94 delete strlist;
95 strlist = nullptr;
96 }
97 ostr = nullptr;
98 del = false;
99 }
100
101 DLLLOCAL const char* getIdentifier() const {
102 return strlist ? (*strlist)[strlist->size() - 1].c_str() : ostr;
103 }
104
105 DLLLOCAL const char* get(unsigned i) const {
106 if (!i && !strlist) {
107 return ostr;
108 }
109 assert(strlist);
110 assert(i < strlist->size());
111 return (*strlist)[i].c_str();
112 }
113
114 DLLLOCAL const char* operator[](unsigned i) const {
115 if (!i && !strlist) {
116 return ostr;
117 }
118 assert(strlist);
119 assert(i < strlist->size());
120 return (*strlist)[i].c_str();
121 }
122
123 DLLLOCAL unsigned size() const {
124 return strlist ? strlist->size() : 1;
125 }
126
127 DLLLOCAL NamedScope* copy() const;
128 DLLLOCAL void fixBCCall();
129
130 DLLLOCAL char* takeName() {
131 char *rv = del ? ostr : strdup(ostr);
132 ostr = nullptr;
133 clear();
134 return rv;
135 }
136
137 DLLLOCAL void optimize() {
138 if (!strlist) {
139 return;
140 }
141
142 while (strlist->size() > 1) {
143 strlist->erase(strlist->begin());
144 }
145 if (del) {
146 free(ostr);
147 del = false;
148 ostr = (char*)strlist->back().c_str();
149 }
150 }
151};
152
153class ltns {
154public:
155 DLLLOCAL bool operator()(const NamedScope& s1, const NamedScope& s2) const {
156 return strcmp(s1.ostr, s2.ostr) < 0;
157 }
158};
159
160#endif // QORE_NAMEDSCOPE_H