Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 1 | //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the subclesses of Expr class declared in ExprCXX.h |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 14 | #include "clang/Basic/IdentifierTable.h" |
| 15 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
| 17 | using namespace clang; |
| 18 | |
Argyrios Kyrtzidis | 4189a76 | 2008-09-10 02:14:49 +0000 | [diff] [blame] | 19 | void CXXConditionDeclExpr::Destroy(ASTContext& C) { |
Sebastian Redl | df2d3cf | 2009-02-05 15:12:41 +0000 | [diff] [blame] | 20 | // FIXME: Cannot destroy the decl here, because it is linked into the |
| 21 | // DeclContext's chain. |
| 22 | //getVarDecl()->Destroy(C); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 23 | this->~CXXConditionDeclExpr(); |
| 24 | C.Deallocate(this); |
Argyrios Kyrtzidis | 4189a76 | 2008-09-10 02:14:49 +0000 | [diff] [blame] | 25 | } |
Argyrios Kyrtzidis | 9e922b1 | 2008-09-09 23:47:53 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // Child Iterators for iterating over subexpressions/substatements |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 31 | // CXXTypeidExpr - has child iterators if the operand is an expression |
| 32 | Stmt::child_iterator CXXTypeidExpr::child_begin() { |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 33 | return isTypeOperand() ? child_iterator() : &Operand.Ex; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 34 | } |
| 35 | Stmt::child_iterator CXXTypeidExpr::child_end() { |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 36 | return isTypeOperand() ? child_iterator() : &Operand.Ex+1; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 37 | } |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | a758d09 | 2007-08-24 20:21:10 +0000 | [diff] [blame] | 39 | // CXXBoolLiteralExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 40 | Stmt::child_iterator CXXBoolLiteralExpr::child_begin() { |
| 41 | return child_iterator(); |
| 42 | } |
| 43 | Stmt::child_iterator CXXBoolLiteralExpr::child_end() { |
| 44 | return child_iterator(); |
| 45 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 46 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 47 | // CXXThisExpr |
| 48 | Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); } |
| 49 | Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); } |
| 50 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 51 | // CXXThrowExpr |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 52 | Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 53 | Stmt::child_iterator CXXThrowExpr::child_end() { |
| 54 | // If Op is 0, we are processing throw; which has no children. |
Ted Kremenek | 1060aff | 2008-06-17 03:11:08 +0000 | [diff] [blame] | 55 | return Op ? &Op+1 : &Op; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 57 | |
| 58 | // CXXDefaultArgExpr |
| 59 | Stmt::child_iterator CXXDefaultArgExpr::child_begin() { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 60 | return child_iterator(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 61 | } |
| 62 | Stmt::child_iterator CXXDefaultArgExpr::child_end() { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 63 | return child_iterator(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 64 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 65 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 66 | // CXXTemporaryObjectExpr |
| 67 | Stmt::child_iterator CXXTemporaryObjectExpr::child_begin() { |
| 68 | return child_iterator(Args); |
| 69 | } |
| 70 | Stmt::child_iterator CXXTemporaryObjectExpr::child_end() { |
| 71 | return child_iterator(Args + NumArgs); |
| 72 | } |
| 73 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 74 | // CXXZeroInitValueExpr |
| 75 | Stmt::child_iterator CXXZeroInitValueExpr::child_begin() { |
| 76 | return child_iterator(); |
| 77 | } |
| 78 | Stmt::child_iterator CXXZeroInitValueExpr::child_end() { |
| 79 | return child_iterator(); |
| 80 | } |
Argyrios Kyrtzidis | 9e922b1 | 2008-09-09 23:47:53 +0000 | [diff] [blame] | 81 | |
| 82 | // CXXConditionDeclExpr |
| 83 | Stmt::child_iterator CXXConditionDeclExpr::child_begin() { |
| 84 | return getVarDecl(); |
| 85 | } |
| 86 | Stmt::child_iterator CXXConditionDeclExpr::child_end() { |
| 87 | return child_iterator(); |
| 88 | } |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 89 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 90 | // CXXNewExpr |
| 91 | CXXNewExpr::CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, |
| 92 | Expr **placementArgs, unsigned numPlaceArgs, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 93 | bool parenTypeId, Expr *arraySize, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 94 | CXXConstructorDecl *constructor, bool initializer, |
| 95 | Expr **constructorArgs, unsigned numConsArgs, |
| 96 | FunctionDecl *operatorDelete, QualType ty, |
| 97 | SourceLocation startLoc, SourceLocation endLoc) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 98 | : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()), |
| 99 | GlobalNew(globalNew), ParenTypeId(parenTypeId), |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 100 | Initializer(initializer), Array(arraySize), NumPlacementArgs(numPlaceArgs), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 101 | NumConstructorArgs(numConsArgs), OperatorNew(operatorNew), |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 102 | OperatorDelete(operatorDelete), Constructor(constructor), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 103 | StartLoc(startLoc), EndLoc(endLoc) |
| 104 | { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 105 | unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 106 | SubExprs = new Stmt*[TotalSize]; |
| 107 | unsigned i = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 108 | if (Array) |
| 109 | SubExprs[i++] = arraySize; |
| 110 | for (unsigned j = 0; j < NumPlacementArgs; ++j) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 111 | SubExprs[i++] = placementArgs[j]; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 112 | for (unsigned j = 0; j < NumConstructorArgs; ++j) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 113 | SubExprs[i++] = constructorArgs[j]; |
| 114 | assert(i == TotalSize); |
| 115 | } |
| 116 | |
| 117 | Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; } |
| 118 | Stmt::child_iterator CXXNewExpr::child_end() { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 119 | return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // CXXDeleteExpr |
| 123 | Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; } |
| 124 | Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; } |
| 125 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 126 | // UnresolvedFunctionNameExpr |
| 127 | Stmt::child_iterator UnresolvedFunctionNameExpr::child_begin() { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 128 | return child_iterator(); |
| 129 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 130 | Stmt::child_iterator UnresolvedFunctionNameExpr::child_end() { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 131 | return child_iterator(); |
| 132 | } |
| 133 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 134 | // UnaryTypeTraitExpr |
| 135 | Stmt::child_iterator UnaryTypeTraitExpr::child_begin() { |
| 136 | return child_iterator(); |
| 137 | } |
| 138 | Stmt::child_iterator UnaryTypeTraitExpr::child_end() { |
| 139 | return child_iterator(); |
| 140 | } |
| 141 | |
Daniel Dunbar | c87a282 | 2009-02-17 23:20:26 +0000 | [diff] [blame] | 142 | bool UnaryTypeTraitExpr::EvaluateTrait() const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 143 | 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 Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 163 | OverloadedOperatorKind 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 Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 176 | return FDecl->getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 177 | else if (const OverloadedFunctionDecl *Ovl |
| 178 | = dyn_cast<OverloadedFunctionDecl>(DRE->getDecl())) |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 179 | return Ovl->getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 180 | else |
| 181 | return OO_None; |
| 182 | } |
| 183 | |
| 184 | SourceRange 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 Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 209 | Expr *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 Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 217 | //===----------------------------------------------------------------------===// |
| 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. |
| 224 | const 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 Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 233 | |
| 234 | CXXTemporaryObjectExpr::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 | |
| 250 | CXXTemporaryObjectExpr::~CXXTemporaryObjectExpr() { |
| 251 | delete [] Args; |
| 252 | } |