CppParser
Loading...
Searching...
No Matches
cppast.cpp
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
11furnished to do so, subject 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
19SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE.
22 */
23
24#include "cppast.h"
25#include "cpputil.h"
26
31
33{
34 if (isCopyConstructor_ != TriStateBool::Unknown)
35 return isCopyConstructor_ == TriStateBool::True;
36
37 isCopyConstructor_ = TriStateBool::False;
38 if (!params_ || (params_->size() != 1))
39 return false;
40 auto& param = params_->front();
41 if (!isVar(param.get()))
42 return false;
43 auto* var = static_cast<CppVar*>(param.get());
44 if (ptrLevel(var->varType()) != 0)
45 return false;
46 if (!isConst(var->varType()) || !isByRef(var->varType()))
47 return false;
48 auto templStartPos = var->varType()->baseType().find('<');
49 if (templStartPos != std::string::npos)
50 {
51 while (isspace(var->varType()->baseType()[--templStartPos]))
52 ;
53 ++templStartPos;
54 if (var->varType()->baseType().substr(0, templStartPos) != name_)
55 return false;
56 }
57 else if (var->varType()->baseType() != name_)
58 {
59 return false;
60 }
61 isCopyConstructor_ = TriStateBool::True;
62
63 return true;
64}
65
67{
68 if (isMoveConstructor_ != TriStateBool::Unknown)
69 return isMoveConstructor_ == TriStateBool::True;
70
71 isMoveConstructor_ = TriStateBool::False;
72 if (!params_ || (params_->size() != 1))
73 return false;
74 auto& param = params_->front();
75 if (!isVar(param.get()))
76 return false;
77 auto* var = static_cast<CppVar*>(param.get());
78 if (ptrLevel(var->varType()) != 0)
79 return false;
80 if (ptrLevel(var->varType()) != 0)
81 return false;
82 if (isConst(var->varType()) || !isByRValueRef(var->varType()))
83 return false;
84 auto templStartPos = var->varType()->baseType().find('<');
85 if (templStartPos != std::string::npos)
86 {
87 while (isspace(var->varType()->baseType()[--templStartPos]))
88 ;
89 ++templStartPos;
90 if (var->varType()->baseType().substr(0, templStartPos) != name_)
91 return false;
92 }
93 else if (var->varType()->baseType() != name_)
94 {
95 return false;
96 }
97 isMoveConstructor_ = TriStateBool::True;
98
99 return true;
100}
101
103{
104 if (!isClassLike(this))
105 return false;
106 if (hasVirtual_ != TriStateBool::Unknown)
107 return hasVirtual_ == TriStateBool::True;
108
109 hasVirtual_ = TriStateBool::False;
110 forEachMember(this, [&](CppObj* mem) {
111 if (isFunction(mem) && isPublic(mem))
112 {
113 auto func = (CppFunction*) mem;
114 if (func->attr() & (kVirtual | kOverride))
115 {
116 hasVirtual_ = TriStateBool::True;
117 return true;
118 }
119 }
120 return false;
121 });
122
123 return hasVirtual_ == TriStateBool::True;
124}
125
127{
128 if (!isClassLike(this))
129 return false;
130 if (hasPureVirtual_ != TriStateBool::Unknown)
131 return hasPureVirtual_ == TriStateBool::True;
132
133 hasPureVirtual_ = TriStateBool::False;
134 forEachMember(this, [&](CppObj* mem) {
135 if (isFunction(mem))
136 {
137 auto func = static_cast<CppFunction*>(mem);
138 if (isPureVirtual(func))
139 {
140 hasPureVirtual_ = TriStateBool::True;
141 return true;
142 }
143 }
144 else if (isDestructor(mem))
145 {
146 auto dtor = static_cast<CppDestructor*>(mem);
147 if (isPureVirtual(dtor))
148 {
149 hasPureVirtual_ = TriStateBool::True;
150 return true;
151 }
152 }
153
154 return false;
155 });
156
157 return hasPureVirtual_ == TriStateBool::True;
158}
159
161{
162 if (ctors_.empty())
163 return true;
164 for (auto* ctor : ctors_)
165 {
166 if (!ctor->hasParams())
167 return true;
168 }
169 return false;
170}
171
173{
174 if (mem->objType_ == CppObjType::kConstructor)
175 {
176 auto* ctor = static_cast<CppConstructor*>(mem);
177 ctors_.push_back(ctor);
178 if (ctor->isCopyConstructor())
179 copyCtor_ = ctor;
180 else if (ctor->isMoveConstructor())
181 moveCtor_ = ctor;
182 }
183 else if (mem->objType_ == CppObjType::kDestructor)
184 {
185 dtor_ = static_cast<CppDestructor*>(mem);
186 }
187}
188
190{
191 return cppObj ? cppObj->objType_ : CppObjType::kUnknown;
192}
193
194bool operator==(const CppExpr& expr1, const CppExpr& expr2)
195{
196 if (expr1.flags_ != expr2.flags_)
197 return false;
198 if (expr1.oper_ != expr2.oper_)
199 return false;
200 if ((expr1.expr1_) != (expr2.expr1_))
201 return false;
202
204 return true;
206 {
207 return ((expr1.expr2_) != (expr2.expr2_));
208 }
209
210 return false;
211}
bool operator==(const CppExpr &expr1, const CppExpr &expr2)
Definition: cppast.cpp:194
CppObjType objType(CppObj *cppObj)
Definition: cppast.cpp:189
bool forEachMember(CppCompoundEPtr compound, std::function< bool(CppObj *)> visitor)
@ kVirtual
Definition: cppconst.h:193
@ kOverride
Definition: cppconst.h:195
CppObjType
Definition: cppconst.h:36
@ kBinaryOperatorEnd
Definition: cppconst.h:161
@ kBinaryOperatorStart
Definition: cppconst.h:127
bool isPureVirtual(CppFunctionBase *func)
bool isConst(CppFunctionBase *func)
bool isClassLike(CppObj *cppObj)
bool isFunction(CppObj *cppObj)
bool isPublic(CppObj *cppObj)
bool isVar(CppObj *cppObj)
bool isDestructor(CppObj *cppObj)
bool isByRef(CppVarType *varType)
std::uint8_t ptrLevel(CppVarType *varType)
bool isByRValueRef(CppVarType *varType)
std::vector< CppConstructor * > ctors_
Definition: cppast.h:1142
TriStateBool hasVirtual_
Definition: cppast.h:1147
bool hasPureVirtual()
Definition: cppast.cpp:126
bool triviallyConstructable()
Definition: cppast.cpp:160
CppConstructor * copyCtor_
Definition: cppast.h:1143
bool hasPublicVirtualMethod()
Definition: cppast.cpp:102
void assignSpecialMember(CppObj *mem)
Definition: cppast.cpp:172
CppDestructor * dtor()
Definition: cppast.h:1084
CppConstructor * moveCtor_
Definition: cppast.h:1144
TriStateBool hasPureVirtual_
Definition: cppast.h:1148
CppDestructor * dtor_
Definition: cppast.h:1145
bool isCopyConstructor()
Definition: cppast.cpp:32
TriStateBool isCopyConstructor_
Definition: cppast.h:1502
bool isMoveConstructor()
Definition: cppast.cpp:66
TriStateBool isMoveConstructor_
Definition: cppast.h:1503
An expression in a C/C++ program.
Definition: cppast.h:1750
CppExprAtom expr1_
Definition: cppast.h:1768
CppOperator oper_
Definition: cppast.h:1771
CppExprAtom expr2_
Definition: cppast.h:1769
short flags_
Definition: cppast.h:1772
CppParamVectorPtr params_
Definition: cppast.h:1280
std::string name_
Definition: cppast.h:1196
An abstract class that is used as base class of all other classes.
Definition: cppast.h:134
CppObjType objType_
Definition: cppast.h:135
std::string & baseType()
Definition: cppast.h:435
Class to represent C++ variable definition.
Definition: cppast.h:565
CppVarType * varType()
Definition: cppast.h:589