Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
scopedtypevariant.h
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2020 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
#ifndef SCOPEDTYPEVARIANT_H
17
#define SCOPEDTYPEVARIANT_H
18
19
#include <utility>
20
#include <vector>
21
#include <variant>
22
23
#include "
qcstring.h
"
24
#include "
definition.h
"
25
26
//! Class representing a local class definition found while
27
//! generating syntax highlighted code.
28
class
LocalDef
29
{
30
public
:
31
void
insertBaseClass
(
const
QCString
&name) {
m_baseClasses
.push_back(name); }
32
std::vector<QCString>
baseClasses
()
const
{
return
m_baseClasses
; }
33
private
:
34
std::vector<QCString>
m_baseClasses
;
35
};
36
37
//-----------------------------------------------------------------------------
38
39
/*! Variant class for a scoped type.
40
*
41
* Variants:
42
* - DummyDef: a type used for hiding a global type.
43
* - LocalDef: a locally defined type (e.g. found inside a function)
44
* - GlobalDef: a globally defined type (processed by doxygen in an earlier pass).
45
*/
46
class
ScopedTypeVariant
47
{
48
public
:
49
struct
DummyDef
{};
50
using
GlobalDef
=
const
Definition
*;
51
using
Variant
= std::variant<DummyDef,LocalDef,GlobalDef>;
52
explicit
ScopedTypeVariant
(
GlobalDef
d =
nullptr
)
53
:
m_name
(d ? d->
name
() :
QCString
()),
m_variant
(d ?
Variant
(d) :
Variant
(
DummyDef
())) {}
54
explicit
ScopedTypeVariant
(
const
QCString
&
name
)
55
:
m_name
(
name
),
m_variant
(
LocalDef
()) {}
56
QCString
name
()
const
{
return
m_name
; }
57
LocalDef
*
localDef
() {
return
std::get_if<LocalDef>(&
m_variant
); }
58
const
LocalDef
*
localDef
()
const
{
return
std::get_if<LocalDef>(&
m_variant
); }
59
const
Definition
*
globalDef
()
const
{
auto
pp = std::get_if<GlobalDef>(&
m_variant
);
return
pp ? *pp :
nullptr
; }
60
bool
isDummy
()
const
{
return
std::holds_alternative<DummyDef>(
m_variant
); }
61
62
private
:
63
QCString
m_name
;
64
Variant
m_variant
;
65
};
66
67
//-----------------------------------------------------------------------------
68
69
/*! Represents a stack of variable to class mappings as found in the
70
* code. Each scope is enclosed in pushScope() and popScope() calls.
71
* Variables are added by calling addVariables() and one can search
72
* for variable using findVariable().
73
*/
74
class
VariableContext
75
{
76
public
:
77
using
Scope
= std::unordered_map<std::string,ScopedTypeVariant>;
78
79
void
pushScope
()
80
{
81
m_scopes
.emplace_back();
82
}
83
void
popScope
()
84
{
85
if
(!
m_scopes
.empty())
86
{
87
m_scopes
.pop_back();
88
}
89
}
90
void
clear
()
91
{
92
m_scopes
.clear();
93
m_globalScope
.clear();
94
}
95
void
clearExceptGlobal
()
96
{
97
m_scopes
.clear();
98
}
99
void
addVariable
(
const
QCString
&name,
ScopedTypeVariant
stv)
100
{
101
Scope
*scope =
m_scopes
.empty() ? &
m_globalScope
: &
m_scopes
.back();
102
scope->emplace(name.
str
(),std::move(stv));
// add it to a list
103
}
104
const
ScopedTypeVariant
*
findVariable
(
const
QCString
&name)
105
{
106
const
ScopedTypeVariant
*result =
nullptr
;
107
if
(name.
isEmpty
())
return
result;
108
109
// search from inner to outer scope
110
auto
it = std::rbegin(
m_scopes
);
111
while
(it != std::rend(
m_scopes
))
112
{
113
auto
it2 = it->find(name.
str
());
114
if
(it2 != std::end(*it))
115
{
116
result = &it2->second;
117
return
result;
118
}
119
++it;
120
}
121
// nothing found -> also try the global scope
122
auto
it2 =
m_globalScope
.find(name.
str
());
123
if
(it2 !=
m_globalScope
.end())
124
{
125
result = &it2->second;
126
}
127
return
result;
128
}
129
bool
atGlobalScope
()
const
{
return
m_scopes
.empty(); }
130
131
private
:
132
Scope
m_globalScope
;
133
std::vector<Scope>
m_scopes
;
134
};
135
136
//-----------------------------------------------------------------------------
137
138
/** Represents the call context */
139
class
CallContext
140
{
141
public
:
142
struct
Ctx
143
{
144
Ctx
(
const
QCString
&name_,
const
QCString
&type_,
int
bracketCount_) :
name
(name_),
type
(type_),
bracketCount
(bracketCount_) {}
145
QCString
name
;
146
QCString
type
;
147
int
bracketCount
;
148
ScopedTypeVariant
stv
;
149
};
150
151
CallContext
()
152
{
153
clear
();
154
}
155
void
setScope
(
const
ScopedTypeVariant
&stv)
156
{
157
Ctx
&ctx =
m_stvList
.back();
158
ctx.
stv
=stv;
159
}
160
void
pushScope
(
const
QCString
&name_,
const
QCString
&type_,
int
bracketCount_)
161
{
162
m_stvList
.emplace_back(name_,type_,bracketCount_);
163
}
164
void
popScope
(
QCString
&name_,
QCString
&type_,
int
&bracketCount_)
165
{
166
if
(
m_stvList
.size()>1)
167
{
168
const
Ctx
&ctx =
m_stvList
.back();
169
name_ = ctx.
name
;
170
type_ = ctx.
type
;
171
bracketCount_ = ctx.
bracketCount
;
172
m_stvList
.pop_back();
173
}
174
}
175
void
clear
()
176
{
177
m_stvList
.clear();
178
m_stvList
.emplace_back(
QCString
(),
QCString
(),0);
179
}
180
const
ScopedTypeVariant
getScope
()
const
181
{
182
return
m_stvList
.back().stv;
183
}
184
185
private
:
186
std::vector<Ctx>
m_stvList
;
187
};
188
189
190
#endif
CallContext::getScope
const ScopedTypeVariant getScope() const
Definition
scopedtypevariant.h:180
CallContext::clear
void clear()
Definition
scopedtypevariant.h:175
CallContext::popScope
void popScope(QCString &name_, QCString &type_, int &bracketCount_)
Definition
scopedtypevariant.h:164
CallContext::setScope
void setScope(const ScopedTypeVariant &stv)
Definition
scopedtypevariant.h:155
CallContext::pushScope
void pushScope(const QCString &name_, const QCString &type_, int bracketCount_)
Definition
scopedtypevariant.h:160
CallContext::m_stvList
std::vector< Ctx > m_stvList
Definition
scopedtypevariant.h:186
CallContext::CallContext
CallContext()
Definition
scopedtypevariant.h:151
Definition
The common base class of all entity definitions found in the sources.
Definition
definition.h:77
LocalDef
Definition
scopedtypevariant.h:29
LocalDef::baseClasses
std::vector< QCString > baseClasses() const
Definition
scopedtypevariant.h:32
LocalDef::m_baseClasses
std::vector< QCString > m_baseClasses
Definition
scopedtypevariant.h:34
LocalDef::insertBaseClass
void insertBaseClass(const QCString &name)
Definition
scopedtypevariant.h:31
QCString
This is an alternative implementation of QCString.
Definition
qcstring.h:101
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition
qcstring.h:163
QCString::str
const std::string & str() const
Definition
qcstring.h:552
ScopedTypeVariant
Definition
scopedtypevariant.h:47
ScopedTypeVariant::m_name
QCString m_name
Definition
scopedtypevariant.h:63
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(const QCString &name)
Definition
scopedtypevariant.h:54
ScopedTypeVariant::isDummy
bool isDummy() const
Definition
scopedtypevariant.h:60
ScopedTypeVariant::localDef
const LocalDef * localDef() const
Definition
scopedtypevariant.h:58
ScopedTypeVariant::GlobalDef
const Definition * GlobalDef
Definition
scopedtypevariant.h:50
ScopedTypeVariant::localDef
LocalDef * localDef()
Definition
scopedtypevariant.h:57
ScopedTypeVariant::Variant
std::variant< DummyDef, LocalDef, GlobalDef > Variant
Definition
scopedtypevariant.h:51
ScopedTypeVariant::globalDef
const Definition * globalDef() const
Definition
scopedtypevariant.h:59
ScopedTypeVariant::m_variant
Variant m_variant
Definition
scopedtypevariant.h:64
ScopedTypeVariant::name
QCString name() const
Definition
scopedtypevariant.h:56
ScopedTypeVariant::ScopedTypeVariant
ScopedTypeVariant(GlobalDef d=nullptr)
Definition
scopedtypevariant.h:52
VariableContext
Definition
scopedtypevariant.h:75
VariableContext::m_globalScope
Scope m_globalScope
Definition
scopedtypevariant.h:132
VariableContext::clear
void clear()
Definition
scopedtypevariant.h:90
VariableContext::atGlobalScope
bool atGlobalScope() const
Definition
scopedtypevariant.h:129
VariableContext::m_scopes
std::vector< Scope > m_scopes
Definition
scopedtypevariant.h:133
VariableContext::popScope
void popScope()
Definition
scopedtypevariant.h:83
VariableContext::addVariable
void addVariable(const QCString &name, ScopedTypeVariant stv)
Definition
scopedtypevariant.h:99
VariableContext::findVariable
const ScopedTypeVariant * findVariable(const QCString &name)
Definition
scopedtypevariant.h:104
VariableContext::Scope
std::unordered_map< std::string, ScopedTypeVariant > Scope
Definition
scopedtypevariant.h:77
VariableContext::pushScope
void pushScope()
Definition
scopedtypevariant.h:79
VariableContext::clearExceptGlobal
void clearExceptGlobal()
Definition
scopedtypevariant.h:95
definition.h
qcstring.h
CallContext::Ctx
Definition
scopedtypevariant.h:143
CallContext::Ctx::name
QCString name
Definition
scopedtypevariant.h:145
CallContext::Ctx::type
QCString type
Definition
scopedtypevariant.h:146
CallContext::Ctx::Ctx
Ctx(const QCString &name_, const QCString &type_, int bracketCount_)
Definition
scopedtypevariant.h:144
CallContext::Ctx::stv
ScopedTypeVariant stv
Definition
scopedtypevariant.h:148
CallContext::Ctx::bracketCount
int bracketCount
Definition
scopedtypevariant.h:147
ScopedTypeVariant::DummyDef
Definition
scopedtypevariant.h:49
src
scopedtypevariant.h
Generated by
1.17.0