blob: d60ef39070b822487b4a4c6fa402dac2599e8cde [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
Daniel Dunbarc87a2822009-02-17 23:20:26 +0000142bool UnaryTypeTraitExpr::EvaluateTrait() const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000143 switch(UTT) {
144 default: assert(false && "Unknown type trait or not implemented");
145 case UTT_IsPOD: return QueriedType->isPODType();
146 case UTT_IsClass: // Fallthrough
147 case UTT_IsUnion:
148 if (const RecordType *Record = QueriedType->getAsRecordType()) {
149 bool Union = Record->getDecl()->isUnion();
150 return UTT == UTT_IsUnion ? Union : !Union;
151 }
152 return false;
153 case UTT_IsEnum: return QueriedType->isEnumeralType();
154 case UTT_IsPolymorphic:
155 if (const RecordType *Record = QueriedType->getAsRecordType()) {
156 // Type traits are only parsed in C++, so we've got CXXRecords.
157 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
158 }
159 return false;
160 }
161}
162
Douglas Gregorb4609802008-11-14 16:09:21 +0000163OverloadedOperatorKind CXXOperatorCallExpr::getOperator() const {
164 // All simple function calls (e.g. func()) are implicitly cast to pointer to
165 // function. As a result, we try and obtain the DeclRefExpr from the
166 // ImplicitCastExpr.
167 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
168 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
169 return OO_None;
170
171 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
172 if (!DRE)
173 return OO_None;
174
175 if (const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()))
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000176 return FDecl->getDeclName().getCXXOverloadedOperator();
Douglas Gregorb4609802008-11-14 16:09:21 +0000177 else if (const OverloadedFunctionDecl *Ovl
178 = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl()))
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000179 return Ovl->getDeclName().getCXXOverloadedOperator();
Douglas Gregorb4609802008-11-14 16:09:21 +0000180 else
181 return OO_None;
182}
183
184SourceRange CXXOperatorCallExpr::getSourceRange() const {
185 OverloadedOperatorKind Kind = getOperator();
186 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
187 if (getNumArgs() == 1)
188 // Prefix operator
189 return SourceRange(getOperatorLoc(),
190 getArg(0)->getSourceRange().getEnd());
191 else
192 // Postfix operator
193 return SourceRange(getArg(0)->getSourceRange().getEnd(),
194 getOperatorLoc());
195 } else if (Kind == OO_Call) {
196 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
197 } else if (Kind == OO_Subscript) {
198 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
199 } else if (getNumArgs() == 1) {
200 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
201 } else if (getNumArgs() == 2) {
202 return SourceRange(getArg(0)->getSourceRange().getBegin(),
203 getArg(1)->getSourceRange().getEnd());
204 } else {
205 return SourceRange();
206 }
207}
208
Douglas Gregor88a35142008-12-22 05:46:06 +0000209Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
210 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
211 return MemExpr->getBase();
212
213 // FIXME: Will eventually need to cope with member pointers.
214 return 0;
215}
216
Douglas Gregor49badde2008-10-27 19:41:14 +0000217//===----------------------------------------------------------------------===//
218// Named casts
219//===----------------------------------------------------------------------===//
220
221/// getCastName - Get the name of the C++ cast being used, e.g.,
222/// "static_cast", "dynamic_cast", "reinterpret_cast", or
223/// "const_cast". The returned pointer must not be freed.
224const char *CXXNamedCastExpr::getCastName() const {
225 switch (getStmtClass()) {
226 case CXXStaticCastExprClass: return "static_cast";
227 case CXXDynamicCastExprClass: return "dynamic_cast";
228 case CXXReinterpretCastExprClass: return "reinterpret_cast";
229 case CXXConstCastExprClass: return "const_cast";
230 default: return "<invalid cast>";
231 }
232}
Douglas Gregor506ae412009-01-16 18:33:17 +0000233
234CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(CXXConstructorDecl *Cons,
235 QualType writtenTy,
236 SourceLocation tyBeginLoc,
237 Expr **Args,
238 unsigned NumArgs,
239 SourceLocation rParenLoc)
240 : Expr(CXXTemporaryObjectExprClass, writtenTy),
241 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc),
242 Constructor(Cons), Args(0), NumArgs(NumArgs) {
243 if (NumArgs > 0) {
244 this->Args = new Stmt*[NumArgs];
245 for (unsigned i = 0; i < NumArgs; ++i)
246 this->Args[i] = Args[i];
247 }
248}
249
250CXXTemporaryObjectExpr::~CXXTemporaryObjectExpr() {
251 delete [] Args;
252}