Doxygen
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.
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 */
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())) {}
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:
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 */
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 }
96 {
97 m_scopes.clear();
98 }
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 }
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:
133 std::vector<Scope> m_scopes;
134};
135
136//-----------------------------------------------------------------------------
137
138/** Represents the call context */
140{
141 public:
142 struct Ctx
143 {
144 Ctx(const QCString &name_,const QCString &type_, int bracketCount_) : name(name_), type(type_), bracketCount(bracketCount_) {}
149 };
150
152 {
153 clear();
154 }
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 }
181 {
182 return m_stvList.back().stv;
183 }
184
185 private:
186 std::vector<Ctx> m_stvList;
187};
188
189
190#endif
const ScopedTypeVariant getScope() const
void popScope(QCString &name_, QCString &type_, int &bracketCount_)
void setScope(const ScopedTypeVariant &stv)
void pushScope(const QCString &name_, const QCString &type_, int bracketCount_)
std::vector< Ctx > m_stvList
The common base class of all entity definitions found in the sources.
Definition definition.h:76
Class representing a local class definition found while generating syntax highlighted code.
std::vector< QCString > baseClasses() const
std::vector< QCString > m_baseClasses
void insertBaseClass(const QCString &name)
This is an alternative implementation of QCString.
Definition qcstring.h:101
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
const std::string & str() const
Definition qcstring.h:537
ScopedTypeVariant(const QCString &name)
const LocalDef * localDef() const
const Definition * GlobalDef
std::variant< DummyDef, LocalDef, GlobalDef > Variant
const Definition * globalDef() const
QCString name() const
ScopedTypeVariant(GlobalDef d=nullptr)
bool atGlobalScope() const
std::vector< Scope > m_scopes
void addVariable(const QCString &name, ScopedTypeVariant stv)
const ScopedTypeVariant * findVariable(const QCString &name)
std::unordered_map< std::string, ScopedTypeVariant > Scope
Ctx(const QCString &name_, const QCString &type_, int bracketCount_)
ScopedTypeVariant stv