blob: 3634c203566eaefcaa96a61bead33db8cbfefa32 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneka758d092007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000016#include "clang/AST/ExprCXX.h"
17using namespace clang;
18
Argyrios Kyrtzidis4189a762008-09-10 02:14:49 +000019void CXXConditionDeclExpr::Destroy(ASTContext& C) {
Sebastian Redldf2d3cf2009-02-05 15:12:41 +000020 // FIXME: Cannot destroy the decl here, because it is linked into the
21 // DeclContext's chain.
22 //getVarDecl()->Destroy(C);
Ted Kremenek8189cde2009-02-07 01:47:29 +000023 this->~CXXConditionDeclExpr();
24 C.Deallocate(this);
Argyrios Kyrtzidis4189a762008-09-10 02:14:49 +000025}
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +000026
Ted Kremeneka758d092007-08-24 20:21:10 +000027//===----------------------------------------------------------------------===//
28// Child Iterators for iterating over subexpressions/substatements
29//===----------------------------------------------------------------------===//
30
Sebastian Redlc42e1182008-11-11 11:37:55 +000031// CXXTypeidExpr - has child iterators if the operand is an expression
32Stmt::child_iterator CXXTypeidExpr::child_begin() {
Sebastian Redld4575892008-12-03 23:17:54 +000033 return isTypeOperand() ? child_iterator() : &Operand.Ex;
Sebastian Redlc42e1182008-11-11 11:37:55 +000034}
35Stmt::child_iterator CXXTypeidExpr::child_end() {
Sebastian Redld4575892008-12-03 23:17:54 +000036 return isTypeOperand() ? child_iterator() : &Operand.Ex+1;
Sebastian Redlc42e1182008-11-11 11:37:55 +000037}
Ted Kremeneka758d092007-08-24 20:21:10 +000038
Ted Kremeneka758d092007-08-24 20:21:10 +000039// CXXBoolLiteralExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +000040Stmt::child_iterator CXXBoolLiteralExpr::child_begin() {
41 return child_iterator();
42}
43Stmt::child_iterator CXXBoolLiteralExpr::child_end() {
44 return child_iterator();
45}
Chris Lattner50dd2892008-02-26 00:51:44 +000046
Douglas Gregor796da182008-11-04 14:32:21 +000047// CXXThisExpr
48Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); }
49Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); }
50
Chris Lattner50dd2892008-02-26 00:51:44 +000051// CXXThrowExpr
Ted Kremenek1060aff2008-06-17 03:11:08 +000052Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; }
Chris Lattner50dd2892008-02-26 00:51:44 +000053Stmt::child_iterator CXXThrowExpr::child_end() {
54 // If Op is 0, we are processing throw; which has no children.
Ted Kremenek1060aff2008-06-17 03:11:08 +000055 return Op ? &Op+1 : &Op;
Chris Lattner50dd2892008-02-26 00:51:44 +000056}
Chris Lattner04421082008-04-08 04:40:51 +000057
58// CXXDefaultArgExpr
59Stmt::child_iterator CXXDefaultArgExpr::child_begin() {
Chris Lattner8123a952008-04-10 02:22:51 +000060 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000061}
62Stmt::child_iterator CXXDefaultArgExpr::child_end() {
Chris Lattner8123a952008-04-10 02:22:51 +000063 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000064}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000065
Douglas Gregor506ae412009-01-16 18:33:17 +000066// CXXTemporaryObjectExpr
67Stmt::child_iterator CXXTemporaryObjectExpr::child_begin() {
68 return child_iterator(Args);
69}
70Stmt::child_iterator CXXTemporaryObjectExpr::child_end() {
71 return child_iterator(Args + NumArgs);
72}
73
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000074// CXXZeroInitValueExpr
75Stmt::child_iterator CXXZeroInitValueExpr::child_begin() {
76 return child_iterator();
77}
78Stmt::child_iterator CXXZeroInitValueExpr::child_end() {
79 return child_iterator();
80}
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +000081
82// CXXConditionDeclExpr
83Stmt::child_iterator CXXConditionDeclExpr::child_begin() {
84 return getVarDecl();
85}
86Stmt::child_iterator CXXConditionDeclExpr::child_end() {
87 return child_iterator();
88}
Douglas Gregor49badde2008-10-27 19:41:14 +000089
Sebastian Redl4c5d3202008-11-21 19:14:01 +000090// CXXNewExpr
91CXXNewExpr::CXXNewExpr(bool globalNew, FunctionDecl *operatorNew,
92 Expr **placementArgs, unsigned numPlaceArgs,
Sebastian Redlcee63fb2008-12-02 14:43:59 +000093 bool parenTypeId, Expr *arraySize,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000094 CXXConstructorDecl *constructor, bool initializer,
95 Expr **constructorArgs, unsigned numConsArgs,
96 FunctionDecl *operatorDelete, QualType ty,
97 SourceLocation startLoc, SourceLocation endLoc)
Sebastian Redl28507842009-02-26 14:39:58 +000098 : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()),
99 GlobalNew(globalNew), ParenTypeId(parenTypeId),
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000100 Initializer(initializer), Array(arraySize), NumPlacementArgs(numPlaceArgs),
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000101 NumConstructorArgs(numConsArgs), OperatorNew(operatorNew),
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000102 OperatorDelete(operatorDelete), Constructor(constructor),
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000103 StartLoc(startLoc), EndLoc(endLoc)
104{
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000105 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000106 SubExprs = new Stmt*[TotalSize];
107 unsigned i = 0;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000108 if (Array)
109 SubExprs[i++] = arraySize;
110 for (unsigned j = 0; j < NumPlacementArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000111 SubExprs[i++] = placementArgs[j];
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000112 for (unsigned j = 0; j < NumConstructorArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000113 SubExprs[i++] = constructorArgs[j];
114 assert(i == TotalSize);
115}
116
117Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; }
118Stmt::child_iterator CXXNewExpr::child_end() {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000119 return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000120}
121
122// CXXDeleteExpr
123Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; }
124Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; }
125
Douglas Gregor17330012009-02-04 15:01:18 +0000126// UnresolvedFunctionNameExpr
127Stmt::child_iterator UnresolvedFunctionNameExpr::child_begin() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000128 return child_iterator();
129}
Douglas Gregor17330012009-02-04 15:01:18 +0000130Stmt::child_iterator UnresolvedFunctionNameExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000131 return child_iterator();
132}
133
Sebastian Redl64b45f72009-01-05 20:52:13 +0000134// UnaryTypeTraitExpr
135Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
136 return child_iterator();
137}
138Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
139 return child_iterator();
140}
141
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000142// UnresolvedDeclRefExpr
143StmtIterator UnresolvedDeclRefExpr::child_begin() {
144 return child_iterator();
145}
146
147StmtIterator UnresolvedDeclRefExpr::child_end() {
148 return child_iterator();
149}
150
Daniel Dunbarc87a2822009-02-17 23:20:26 +0000151bool UnaryTypeTraitExpr::EvaluateTrait() const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000152 switch(UTT) {
153 default: assert(false && "Unknown type trait or not implemented");
154 case UTT_IsPOD: return QueriedType->isPODType();
155 case UTT_IsClass: // Fallthrough
156 case UTT_IsUnion:
157 if (const RecordType *Record = QueriedType->getAsRecordType()) {
158 bool Union = Record->getDecl()->isUnion();
159 return UTT == UTT_IsUnion ? Union : !Union;
160 }
161 return false;
162 case UTT_IsEnum: return QueriedType->isEnumeralType();
163 case UTT_IsPolymorphic:
164 if (const RecordType *Record = QueriedType->getAsRecordType()) {
165 // Type traits are only parsed in C++, so we've got CXXRecords.
166 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
167 }
168 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000169 case UTT_IsAbstract:
170 if (const RecordType *RT = QueriedType->getAsRecordType())
171 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
172 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000173 case UTT_HasTrivialConstructor:
174 if (const RecordType *RT = QueriedType->getAsRecordType())
175 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
176 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000177 }
178}
179
Douglas Gregorb4609802008-11-14 16:09:21 +0000180SourceRange CXXOperatorCallExpr::getSourceRange() const {
181 OverloadedOperatorKind Kind = getOperator();
182 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
183 if (getNumArgs() == 1)
184 // Prefix operator
185 return SourceRange(getOperatorLoc(),
186 getArg(0)->getSourceRange().getEnd());
187 else
188 // Postfix operator
189 return SourceRange(getArg(0)->getSourceRange().getEnd(),
190 getOperatorLoc());
191 } else if (Kind == OO_Call) {
192 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
193 } else if (Kind == OO_Subscript) {
194 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
195 } else if (getNumArgs() == 1) {
196 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
197 } else if (getNumArgs() == 2) {
198 return SourceRange(getArg(0)->getSourceRange().getBegin(),
199 getArg(1)->getSourceRange().getEnd());
200 } else {
201 return SourceRange();
202 }
203}
204
Douglas Gregor88a35142008-12-22 05:46:06 +0000205Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
206 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
207 return MemExpr->getBase();
208
209 // FIXME: Will eventually need to cope with member pointers.
210 return 0;
211}
212
Douglas Gregor49badde2008-10-27 19:41:14 +0000213//===----------------------------------------------------------------------===//
214// Named casts
215//===----------------------------------------------------------------------===//
216
217/// getCastName - Get the name of the C++ cast being used, e.g.,
218/// "static_cast", "dynamic_cast", "reinterpret_cast", or
219/// "const_cast". The returned pointer must not be freed.
220const char *CXXNamedCastExpr::getCastName() const {
221 switch (getStmtClass()) {
222 case CXXStaticCastExprClass: return "static_cast";
223 case CXXDynamicCastExprClass: return "dynamic_cast";
224 case CXXReinterpretCastExprClass: return "reinterpret_cast";
225 case CXXConstCastExprClass: return "const_cast";
226 default: return "<invalid cast>";
227 }
228}
Douglas Gregor506ae412009-01-16 18:33:17 +0000229
230CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(CXXConstructorDecl *Cons,
231 QualType writtenTy,
232 SourceLocation tyBeginLoc,
233 Expr **Args,
234 unsigned NumArgs,
235 SourceLocation rParenLoc)
Douglas Gregorba498172009-03-13 21:01:28 +0000236 : Expr(CXXTemporaryObjectExprClass, writtenTy,
237 writtenTy->isDependentType(),
238 (writtenTy->isDependentType() ||
239 CallExpr::hasAnyValueDependentArguments(Args, NumArgs))),
Douglas Gregor506ae412009-01-16 18:33:17 +0000240 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc),
241 Constructor(Cons), Args(0), NumArgs(NumArgs) {
242 if (NumArgs > 0) {
243 this->Args = new Stmt*[NumArgs];
244 for (unsigned i = 0; i < NumArgs; ++i)
245 this->Args[i] = Args[i];
246 }
247}
248
249CXXTemporaryObjectExpr::~CXXTemporaryObjectExpr() {
250 delete [] Args;
251}