CppParser
Loading...
Searching...
No Matches
cppvar-info-accessor.h
Go to the documentation of this file.
1/*
2The MIT License (MIT)
3
4Copyright (c) 2018 Satya Das
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/***************************************************************************************/
25
26#pragma once
27
28#include "cppast.h"
29#include "cppconst.h"
30
31 inline std::uint8_t ptrLevel(CppVarType* varType)
32{
33return varType->typeModifier().ptrLevel_;
34}
35
36inline std::uint8_t ptrLevel(std::unique_ptr<CppVarType>& varType)
37{
38return varType->typeModifier().ptrLevel_;
39}
40
41inline std::uint8_t ptrLevel(CppVarTypeEPtr varType)
42{
43return varType->typeModifier().ptrLevel_;
44}
45
47{
48return varType->typeModifier().refType_;
49}
50
51inline CppRefType refType(std::unique_ptr<CppVarType>& varType)
52{
53return varType->typeModifier().refType_;
54}
55
57{
58return varType->typeModifier().refType_;
59}
60
61inline std::uint8_t effectivePtrLevel(CppVarTypeEPtr& varType)
62{
63return ptrLevel(varType) + [&varType]() {
64 switch (refType(varType))
65 {
68 return 1;
70 break;
71 }
72 return 0;
73}();
74}
75
76inline std::string& baseType(CppVarType* varType)
77{
78return varType->baseType();
79}
80
81inline std::string& baseType(std::unique_ptr<CppVarType>& varType)
82{
83return varType->baseType();
84}
85
86inline std::string& baseType(CppVarTypeEPtr varType)
87{
88return varType->baseType();
89}
90
91inline bool usesTemplateType(std::string& varTypeName)
92{
93return varTypeName.find('<') != varTypeName.npos;
94}
95
96inline bool usesTemplateType(CppVarTypeEPtr varType)
97{
98return usesTemplateType(baseType(varType));
99}
100
101inline bool isVoid(CppVarType* varType)
102{
103if (varType->typeModifier().ptrLevel_ != 0 || varType->typeModifier().refType_ != CppRefType::kNoRef)
104 return false;
105// return (varType->baseType().compare("void") == 0);
106// Above simple check fails to detect cases like usage of GrGLvoid
107if (varType->baseType().length() < 4)
108 return false;
109return (strncmp(varType->baseType().c_str() + varType->baseType().length() - 4, "void", 4) == 0);
110}
111
112inline bool isVoid(std::unique_ptr<CppVarType>& varType)
113{
114return isVoid(varType.get());
115}
116
117inline bool isByRef(CppVarType* varType)
118{
119return varType->typeModifier().refType_ == CppRefType::kByRef;
120}
121
122inline bool isByRef(std::unique_ptr<CppVarType>& varType)
123{
124return isByRef(varType.get());
125}
126
127inline bool isByRef(CppVarTypeEPtr varType)
128{
129return varType->typeModifier().refType_ == CppRefType::kByRef;
130}
131
132inline bool isByRValueRef(CppVarType* varType)
133{
134return varType->typeModifier().refType_ == CppRefType::kRValRef;
135}
136
137inline bool isByRValueRef(std::unique_ptr<CppVarType>& varType)
138{
139return isByRValueRef(varType.get());
140}
141
142inline bool isByRValueRef(CppVarTypeEPtr varType)
143{
144return varType->typeModifier().refType_ == CppRefType::kRValRef;
145}
146
147inline bool isConst(CppVarType* varType)
148{
149return ((varType->typeAttr() & kConst) == kConst) || (varType->typeModifier().constBits_ & 1);
150}
151
152inline bool isConst(std::unique_ptr<CppVarType>& varType)
153{
154return isConst(varType.get());
155}
156
157inline bool isConst(CppVarTypeEPtr varType)
158{
159return ((varType->typeAttr() & kConst) == kConst) || (varType->typeModifier().constBits_ & 1);
160}
161
162inline bool isByValue(CppVarType* varType)
163{
164return !isVoid(varType) && (varType->typeModifier().refType_ == CppRefType::kNoRef)
165 && (varType->typeModifier().ptrLevel_ == 0);
166}
167
168inline bool isByValue(std::unique_ptr<CppVarType>& varType)
169{
170return isByValue(varType.get());
171}
172
173inline bool isByValue(CppVarTypeEPtr varType)
174{
175return !isVoid(varType) && (varType->typeModifier().refType_ == CppRefType::kNoRef)
176 && (varType->typeModifier().ptrLevel_ == 0);
177}
178
179inline std::string& baseType(CppVar* var)
180{
181return baseType(var->varType());
182}
183
184inline std::string& baseType(std::unique_ptr<CppVar>& var)
185{
186return baseType(var->varType());
187}
188
189inline std::string& baseType(CppVarEPtr var)
190{
191return baseType(var->varType());
192}
193
194inline std::uint8_t ptrLevel(CppVar* var)
195{
196return ptrLevel(var->varType());
197}
198
199inline std::uint8_t ptrLevel(std::unique_ptr<CppVar>& var)
200{
201return ptrLevel(var->varType());
202}
203
204inline std::uint8_t ptrLevel(CppVarEPtr var)
205{
206return ptrLevel(var->varType());
207}
208
210{
211return refType(var->varType());
212}
213
214inline CppRefType refType(std::unique_ptr<CppVar>& var)
215{
216return refType(var->varType());
217}
218
220{
221return refType(var->varType());
222}
223
224inline std::string& name(CppVar* var)
225{
226return var->varDecl().name();
227}
228
229inline std::string& name(std::unique_ptr<CppVar>& var)
230{
231return name(var.get());
232}
233
234inline std::string& name(CppVarEPtr var)
235{
236return var->varDecl().name();
237}
238
239inline bool isByRef(CppVar* var)
240{
241return isByRef(var->varType());
242}
243
244inline bool isByRef(std::unique_ptr<CppVar>& var)
245{
246return isByRef(var->varType());
247}
248
249inline bool isByRef(CppVarEPtr var)
250{
251return isByRef(var->varType());
252}
253
254inline bool isByRValueRef(CppVar* var)
255{
256return isByRValueRef(var->varType());
257}
258
259inline bool isByRValueRef(std::unique_ptr<CppVar>& var)
260{
261return isByRValueRef(var->varType());
262}
263
264inline bool isByRValueRef(CppVarEPtr var)
265{
266return isByRValueRef(var->varType());
267}
268
269inline bool isConst(CppVar* var)
270{
271return isConst(var->varType());
272}
273
274inline bool isConst(std::unique_ptr<CppVar>& var)
275{
276return isConst(var->varType());
277}
278
279inline bool isConst(CppVarEPtr var)
280{
281return isConst(var->varType());
282}
283
284inline bool isByValue(CppVar* var)
285{
286return isByValue(var->varType());
287}
288
289inline bool isByValue(std::unique_ptr<CppVar>& var)
290{
291return isByValue(var->varType());
292}
293
294inline bool isByValue(CppVarEPtr var)
295{
296return isByValue(var->varType());
297}
Helps working with raw or unique_ptr of CppObj in a uniform way.
Definition: cppeasyptr.h:44
CppRefType
Type of references a variable can have in a C++ program.
Definition: cppconst.h:215
@ kConst
Definition: cppconst.h:189
bool isByValue(CppVarType *varType)
std::uint8_t effectivePtrLevel(CppVarTypeEPtr &varType)
bool isByRef(CppVarType *varType)
bool isConst(CppVarType *varType)
std::string & baseType(CppVarType *varType)
std::uint8_t ptrLevel(CppVarType *varType)
std::string & name(CppVar *var)
bool isByRValueRef(CppVarType *varType)
CppRefType refType(CppVarType *varType)
bool isVoid(CppVarType *varType)
bool usesTemplateType(std::string &varTypeName)
std::uint8_t ptrLevel_
Definition: typemodifier.h:31
CppRefType refType_
Definition: typemodifier.h:30
std::uint32_t constBits_
Definition: typemodifier.h:47
std::string & name()
Definition: cppast.h:503
CppTypeModifier & typeModifier()
Definition: cppast.h:463
std::string & baseType()
Definition: cppast.h:435
std::uint32_t typeAttr()
Definition: cppast.h:447
Class to represent C++ variable definition.
Definition: cppast.h:565
CppVarType * varType()
Definition: cppast.h:589
CppVarDecl & varDecl()
Definition: cppast.h:612