| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// | 
|  | 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. | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  This file implements semantic analysis for C++ expressions. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 14 | #include "SemaInherit.h" | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "Sema.h" | 
|  | 16 | #include "clang/AST/ExprCXX.h" | 
| Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" | 
| Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; | 
|  | 23 |  | 
| Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 24 | /// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function | 
| Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 25 | /// name (e.g., operator void const *) as an expression. This is | 
|  | 26 | /// very similar to ActOnIdentifierExpr, except that instead of | 
|  | 27 | /// providing an identifier the parser provides the type of the | 
|  | 28 | /// conversion function. | 
| Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 29 | Sema::OwningExprResult | 
| Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 30 | Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc, | 
|  | 31 | TypeTy *Ty, bool HasTrailingLParen, | 
| Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 32 | const CXXScopeSpec &SS, | 
|  | 33 | bool isAddressOfOperand) { | 
| Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 34 | QualType ConvType = QualType::getFromOpaquePtr(Ty); | 
|  | 35 | QualType ConvTypeCanon = Context.getCanonicalType(ConvType); | 
|  | 36 | DeclarationName ConvName | 
|  | 37 | = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon); | 
| Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 38 | return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen, | 
| Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 39 | &SS, isAddressOfOperand); | 
| Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 40 | } | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 41 |  | 
| Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 42 | /// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator | 
| Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 43 | /// name (e.g., @c operator+ ) as an expression. This is very | 
|  | 44 | /// similar to ActOnIdentifierExpr, except that instead of providing | 
|  | 45 | /// an identifier the parser provides the kind of overloaded | 
|  | 46 | /// operator that was parsed. | 
| Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 47 | Sema::OwningExprResult | 
| Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 48 | Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc, | 
|  | 49 | OverloadedOperatorKind Op, | 
|  | 50 | bool HasTrailingLParen, | 
| Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 51 | const CXXScopeSpec &SS, | 
|  | 52 | bool isAddressOfOperand) { | 
| Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 53 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op); | 
| Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 54 | return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS, | 
| Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 55 | isAddressOfOperand); | 
| Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 58 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 59 | Action::OwningExprResult | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 60 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, | 
|  | 61 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { | 
| Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 62 | NamespaceDecl *StdNs = GetStdNamespace(); | 
| Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 63 | if (!StdNs) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 64 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); | 
| Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); | 
| Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 67 | Decl *TypeInfoDecl = LookupQualifiedName(StdNs, TypeInfoII, LookupTagName); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 68 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); | 
| Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 69 | if (!TypeInfoRecordDecl) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 70 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); | 
|  | 73 |  | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 74 | if (!isType) { | 
|  | 75 | // C++0x [expr.typeid]p3: | 
|  | 76 | //   When typeid is applied to an expression other than an lvalue of a | 
|  | 77 | //   polymorphic class type [...] [the] expression is an unevaluated | 
|  | 78 | //   operand. | 
|  | 79 |  | 
|  | 80 | // FIXME: if the type of the expression is a class type, the class | 
|  | 81 | // shall be completely defined. | 
|  | 82 | bool isUnevaluatedOperand = true; | 
|  | 83 | Expr *E = static_cast<Expr *>(TyOrExpr); | 
|  | 84 | if (E && !E->isTypeDependent() && E->isLvalue(Context) == Expr::LV_Valid) { | 
|  | 85 | QualType T = E->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 86 | if (const RecordType *RecordT = T->getAs<RecordType>()) { | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 87 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); | 
|  | 88 | if (RecordD->isPolymorphic()) | 
|  | 89 | isUnevaluatedOperand = false; | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | // If this is an unevaluated operand, clear out the set of declaration | 
|  | 94 | // references we have been computing. | 
|  | 95 | if (isUnevaluatedOperand) | 
|  | 96 | PotentiallyReferencedDeclStack.back().clear(); | 
|  | 97 | } | 
|  | 98 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 99 | return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, | 
|  | 100 | TypeInfoType.withConst(), | 
|  | 101 | SourceRange(OpLoc, RParenLoc))); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 104 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 105 | Action::OwningExprResult | 
| Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 106 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { | 
| Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 107 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | "Unknown C++ Boolean value!"); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 109 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, | 
|  | 110 | Context.BoolTy, OpLoc)); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | } | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 112 |  | 
| Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 113 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. | 
|  | 114 | Action::OwningExprResult | 
|  | 115 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { | 
|  | 116 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); | 
|  | 117 | } | 
|  | 118 |  | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 119 | /// ActOnCXXThrow - Parse throw expressions. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 120 | Action::OwningExprResult | 
|  | 121 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 122 | Expr *Ex = E.takeAs<Expr>(); | 
|  | 123 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) | 
|  | 124 | return ExprError(); | 
|  | 125 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | /// CheckCXXThrowOperand - Validate the operand of a throw. | 
|  | 129 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { | 
|  | 130 | // C++ [except.throw]p3: | 
|  | 131 | //   [...] adjusting the type from "array of T" or "function returning T" | 
|  | 132 | //   to "pointer to T" or "pointer to function returning T", [...] | 
|  | 133 | DefaultFunctionArrayConversion(E); | 
|  | 134 |  | 
|  | 135 | //   If the type of the exception would be an incomplete type or a pointer | 
|  | 136 | //   to an incomplete type other than (cv) void the program is ill-formed. | 
|  | 137 | QualType Ty = E->getType(); | 
|  | 138 | int isPointer = 0; | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 139 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 140 | Ty = Ptr->getPointeeType(); | 
|  | 141 | isPointer = 1; | 
|  | 142 | } | 
|  | 143 | if (!isPointer || !Ty->isVoidType()) { | 
|  | 144 | if (RequireCompleteType(ThrowLoc, Ty, | 
|  | 145 | isPointer ? diag::err_throw_incomplete_ptr | 
|  | 146 | : diag::err_throw_incomplete, | 
|  | 147 | E->getSourceRange(), SourceRange(), QualType())) | 
|  | 148 | return true; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | // FIXME: Construct a temporary here. | 
|  | 152 | return false; | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 153 | } | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 154 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 155 | Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 156 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this | 
|  | 157 | /// is a non-lvalue expression whose value is the address of the object for | 
|  | 158 | /// which the function is called. | 
|  | 159 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 160 | if (!isa<FunctionDecl>(CurContext)) | 
|  | 161 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 162 |  | 
|  | 163 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) | 
|  | 164 | if (MD->isInstance()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 165 | return Owned(new (Context) CXXThisExpr(ThisLoc, | 
|  | 166 | MD->getThisType(Context))); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 167 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 168 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 169 | } | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 170 |  | 
|  | 171 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. | 
|  | 172 | /// Can be interpreted either as function-style casting ("int(x)") | 
|  | 173 | /// or class type construction ("ClassType(x,y,z)") | 
|  | 174 | /// or creation of a value-initialized type ("int()"). | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 175 | Action::OwningExprResult | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 176 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, | 
|  | 177 | SourceLocation LParenLoc, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 178 | MultiExprArg exprs, | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 179 | SourceLocation *CommaLocs, | 
|  | 180 | SourceLocation RParenLoc) { | 
|  | 181 | assert(TypeRep && "Missing type!"); | 
|  | 182 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 183 | unsigned NumExprs = exprs.size(); | 
|  | 184 | Expr **Exprs = (Expr**)exprs.get(); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 185 | SourceLocation TyBeginLoc = TypeRange.getBegin(); | 
|  | 186 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); | 
|  | 187 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 188 | if (Ty->isDependentType() || | 
| Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 189 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 190 | exprs.release(); | 
| Anders Carlsson | 26de549 | 2009-04-24 05:23:13 +0000 | [diff] [blame] | 191 |  | 
| Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 192 | return Owned(CXXUnresolvedConstructExpr::Create(Context, | 
|  | 193 | TypeRange.getBegin(), Ty, | 
|  | 194 | LParenLoc, | 
|  | 195 | Exprs, NumExprs, | 
|  | 196 | RParenLoc)); | 
| Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
|  | 199 |  | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 200 | // C++ [expr.type.conv]p1: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 201 | // If the expression list is a single expression, the type conversion | 
|  | 202 | // expression is equivalent (in definedness, and if defined in meaning) to the | 
|  | 203 | // corresponding cast expression. | 
|  | 204 | // | 
|  | 205 | if (NumExprs == 1) { | 
| Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 206 | if (CheckCastTypes(TypeRange, Ty, Exprs[0], /*functional-style*/true)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 207 | return ExprError(); | 
|  | 208 | exprs.release(); | 
|  | 209 | return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), | 
|  | 210 | Ty, TyBeginLoc, Exprs[0], | 
|  | 211 | RParenLoc)); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 214 | if (const RecordType *RT = Ty->getAs<RecordType>()) { | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 215 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 216 |  | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 217 | // FIXME: We should always create a CXXTemporaryObjectExpr here unless | 
|  | 218 | // both the ctor and dtor are trivial. | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 219 | if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) { | 
|  | 220 | CXXConstructorDecl *Constructor | 
|  | 221 | = PerformInitializationByConstructor(Ty, Exprs, NumExprs, | 
|  | 222 | TypeRange.getBegin(), | 
|  | 223 | SourceRange(TypeRange.getBegin(), | 
|  | 224 | RParenLoc), | 
|  | 225 | DeclarationName(), | 
|  | 226 | IK_Direct); | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 227 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 228 | if (!Constructor) | 
|  | 229 | return ExprError(); | 
|  | 230 |  | 
|  | 231 | exprs.release(); | 
| Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 232 | Expr *E = new (Context) CXXTemporaryObjectExpr(Context, Constructor, | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 233 | Ty, TyBeginLoc, Exprs, | 
|  | 234 | NumExprs, RParenLoc); | 
|  | 235 | return MaybeBindToTemporary(E); | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
|  | 238 | // Fall through to value-initialize an object of class type that | 
|  | 239 | // doesn't have a user-declared default constructor. | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | // C++ [expr.type.conv]p1: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 243 | // If the expression list specifies more than a single value, the type shall | 
|  | 244 | // be a class with a suitably declared constructor. | 
|  | 245 | // | 
|  | 246 | if (NumExprs > 1) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 247 | return ExprError(Diag(CommaLocs[0], | 
|  | 248 | diag::err_builtin_func_cast_more_than_one_arg) | 
|  | 249 | << FullRange); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 250 |  | 
|  | 251 | assert(NumExprs == 0 && "Expected 0 expressions"); | 
|  | 252 |  | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 253 | // C++ [expr.type.conv]p2: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 254 | // The expression T(), where T is a simple-type-specifier for a non-array | 
|  | 255 | // complete object type or the (possibly cv-qualified) void type, creates an | 
|  | 256 | // rvalue of the specified type, which is value-initialized. | 
|  | 257 | // | 
|  | 258 | if (Ty->isArrayType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 259 | return ExprError(Diag(TyBeginLoc, | 
|  | 260 | diag::err_value_init_for_array_type) << FullRange); | 
| Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 261 | if (!Ty->isDependentType() && !Ty->isVoidType() && | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 262 | RequireCompleteType(TyBeginLoc, Ty, | 
|  | 263 | diag::err_invalid_incomplete_type_use, FullRange)) | 
|  | 264 | return ExprError(); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 265 |  | 
| Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 266 | if (RequireNonAbstractType(TyBeginLoc, Ty, | 
|  | 267 | diag::err_allocation_of_abstract_type)) | 
| Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 268 | return ExprError(); | 
|  | 269 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 270 | exprs.release(); | 
|  | 271 | return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 272 | } | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 273 |  | 
|  | 274 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 275 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: | 
|  | 276 | /// @code new (memory) int[size][4] @endcode | 
|  | 277 | /// or | 
|  | 278 | /// @code ::new Foo(23, "hello") @endcode | 
|  | 279 | /// For the interpretation of this heap of arguments, consult the base version. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 280 | Action::OwningExprResult | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 281 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 282 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 283 | SourceLocation PlacementRParen, bool ParenTypeId, | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 284 | Declarator &D, SourceLocation ConstructorLParen, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 285 | MultiExprArg ConstructorArgs, | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 286 | SourceLocation ConstructorRParen) | 
|  | 287 | { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 288 | Expr *ArraySize = 0; | 
|  | 289 | unsigned Skip = 0; | 
|  | 290 | // If the specified type is an array, unwrap it and save the expression. | 
|  | 291 | if (D.getNumTypeObjects() > 0 && | 
|  | 292 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { | 
|  | 293 | DeclaratorChunk &Chunk = D.getTypeObject(0); | 
|  | 294 | if (Chunk.Arr.hasStatic) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 295 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) | 
|  | 296 | << D.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 297 | if (!Chunk.Arr.NumElts) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 298 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) | 
|  | 299 | << D.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 300 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); | 
|  | 301 | Skip = 1; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip); | 
| Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 305 | if (D.isInvalidType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 306 | return ExprError(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 307 |  | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 308 | // Every dimension shall be of constant size. | 
|  | 309 | unsigned i = 1; | 
|  | 310 | QualType ElementType = AllocType; | 
|  | 311 | while (const ArrayType *Array = Context.getAsArrayType(ElementType)) { | 
|  | 312 | if (!Array->isConstantArrayType()) { | 
|  | 313 | Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst) | 
|  | 314 | << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange(); | 
|  | 315 | return ExprError(); | 
|  | 316 | } | 
|  | 317 | ElementType = Array->getElementType(); | 
|  | 318 | ++i; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | return BuildCXXNew(StartLoc, UseGlobal, | 
|  | 322 | PlacementLParen, | 
|  | 323 | move(PlacementArgs), | 
|  | 324 | PlacementRParen, | 
|  | 325 | ParenTypeId, | 
|  | 326 | AllocType, | 
|  | 327 | D.getSourceRange().getBegin(), | 
|  | 328 | D.getSourceRange(), | 
|  | 329 | Owned(ArraySize), | 
|  | 330 | ConstructorLParen, | 
|  | 331 | move(ConstructorArgs), | 
|  | 332 | ConstructorRParen); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | Sema::OwningExprResult | 
|  | 336 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, | 
|  | 337 | SourceLocation PlacementLParen, | 
|  | 338 | MultiExprArg PlacementArgs, | 
|  | 339 | SourceLocation PlacementRParen, | 
|  | 340 | bool ParenTypeId, | 
|  | 341 | QualType AllocType, | 
|  | 342 | SourceLocation TypeLoc, | 
|  | 343 | SourceRange TypeRange, | 
|  | 344 | ExprArg ArraySizeE, | 
|  | 345 | SourceLocation ConstructorLParen, | 
|  | 346 | MultiExprArg ConstructorArgs, | 
|  | 347 | SourceLocation ConstructorRParen) { | 
|  | 348 | if (CheckAllocatedType(AllocType, TypeLoc, TypeRange)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 349 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 350 |  | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 351 | QualType ResultType = Context.getPointerType(AllocType); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 352 |  | 
|  | 353 | // That every array dimension except the first is constant was already | 
|  | 354 | // checked by the type check above. | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 355 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 356 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral | 
|  | 357 | //   or enumeration type with a non-negative value." | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 358 | Expr *ArraySize = (Expr *)ArraySizeE.get(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 359 | if (ArraySize && !ArraySize->isTypeDependent()) { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 360 | QualType SizeType = ArraySize->getType(); | 
|  | 361 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 362 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), | 
|  | 363 | diag::err_array_size_not_integral) | 
|  | 364 | << SizeType << ArraySize->getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 365 | // Let's see if this is a constant < 0. If so, we reject it out of hand. | 
|  | 366 | // We don't care about special rules, so we tell the machinery it's not | 
|  | 367 | // evaluated - it gives us a result in more cases. | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 368 | if (!ArraySize->isValueDependent()) { | 
|  | 369 | llvm::APSInt Value; | 
|  | 370 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { | 
|  | 371 | if (Value < llvm::APSInt( | 
|  | 372 | llvm::APInt::getNullValue(Value.getBitWidth()), false)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 373 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), | 
|  | 374 | diag::err_typecheck_negative_array_size) | 
|  | 375 | << ArraySize->getSourceRange()); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 376 | } | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 377 | } | 
|  | 378 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 379 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 380 | FunctionDecl *OperatorNew = 0; | 
|  | 381 | FunctionDecl *OperatorDelete = 0; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 382 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); | 
|  | 383 | unsigned NumPlaceArgs = PlacementArgs.size(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 384 | if (!AllocType->isDependentType() && | 
|  | 385 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && | 
|  | 386 | FindAllocationFunctions(StartLoc, | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 387 | SourceRange(PlacementLParen, PlacementRParen), | 
|  | 388 | UseGlobal, AllocType, ArraySize, PlaceArgs, | 
|  | 389 | NumPlaceArgs, OperatorNew, OperatorDelete)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 390 | return ExprError(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 391 |  | 
|  | 392 | bool Init = ConstructorLParen.isValid(); | 
|  | 393 | // --- Choosing a constructor --- | 
|  | 394 | // C++ 5.3.4p15 | 
|  | 395 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) | 
|  | 396 | //   the object is not initialized. If the object, or any part of it, is | 
|  | 397 | //   const-qualified, it's an error. | 
|  | 398 | // 2) If T is a POD and there's an empty initializer, the object is value- | 
|  | 399 | //   initialized. | 
|  | 400 | // 3) If T is a POD and there's one initializer argument, the object is copy- | 
|  | 401 | //   constructed. | 
|  | 402 | // 4) If T is a POD and there's more initializer arguments, it's an error. | 
|  | 403 | // 5) If T is not a POD, the initializer arguments are used as constructor | 
|  | 404 | //   arguments. | 
|  | 405 | // | 
|  | 406 | // Or by the C++0x formulation: | 
|  | 407 | // 1) If there's no initializer, the object is default-initialized according | 
|  | 408 | //    to C++0x rules. | 
|  | 409 | // 2) Otherwise, the object is direct-initialized. | 
|  | 410 | CXXConstructorDecl *Constructor = 0; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 411 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); | 
| Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 412 | const RecordType *RT; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 413 | unsigned NumConsArgs = ConstructorArgs.size(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 414 | if (AllocType->isDependentType()) { | 
|  | 415 | // Skip all the checks. | 
|  | 416 | } | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 417 | else if ((RT = AllocType->getAs<RecordType>()) && | 
| Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 418 | !AllocType->isAggregateType()) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 419 | Constructor = PerformInitializationByConstructor( | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 420 | AllocType, ConsArgs, NumConsArgs, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 421 | TypeLoc, | 
|  | 422 | SourceRange(TypeLoc, ConstructorRParen), | 
| Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 423 | RT->getDecl()->getDeclName(), | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 424 | NumConsArgs != 0 ? IK_Direct : IK_Default); | 
|  | 425 | if (!Constructor) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 426 | return ExprError(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 427 | } else { | 
|  | 428 | if (!Init) { | 
|  | 429 | // FIXME: Check that no subpart is const. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 430 | if (AllocType.isConstQualified()) | 
|  | 431 | return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 432 | << TypeRange); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 433 | } else if (NumConsArgs == 0) { | 
|  | 434 | // Object is value-initialized. Do nothing. | 
|  | 435 | } else if (NumConsArgs == 1) { | 
|  | 436 | // Object is direct-initialized. | 
| Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 437 | // FIXME: What DeclarationName do we pass in here? | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 438 | if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 439 | DeclarationName() /*AllocType.getAsString()*/, | 
|  | 440 | /*DirectInit=*/true)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 441 | return ExprError(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 442 | } else { | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 443 | return ExprError(Diag(StartLoc, | 
|  | 444 | diag::err_builtin_direct_init_more_than_one_arg) | 
|  | 445 | << SourceRange(ConstructorLParen, ConstructorRParen)); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 446 | } | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) | 
|  | 450 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 451 | PlacementArgs.release(); | 
|  | 452 | ConstructorArgs.release(); | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 453 | ArraySizeE.release(); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 454 | return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, | 
| Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 455 | NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init, | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 456 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 457 | StartLoc, Init ? ConstructorRParen : SourceLocation())); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
|  | 460 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type | 
|  | 461 | /// in a new-expression. | 
|  | 462 | /// dimension off and stores the size expression in ArraySize. | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 463 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, | 
|  | 464 | SourceRange R) | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 465 | { | 
|  | 466 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an | 
|  | 467 | //   abstract class type or array thereof. | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 468 | if (AllocType->isFunctionType()) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 469 | return Diag(Loc, diag::err_bad_new_type) | 
|  | 470 | << AllocType << 0 << R; | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 471 | else if (AllocType->isReferenceType()) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 472 | return Diag(Loc, diag::err_bad_new_type) | 
|  | 473 | << AllocType << 1 << R; | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 474 | else if (!AllocType->isDependentType() && | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 475 | RequireCompleteType(Loc, AllocType, | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 476 | diag::err_new_incomplete_type, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 477 | R)) | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 478 | return true; | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 479 | else if (RequireNonAbstractType(Loc, AllocType, | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 480 | diag::err_allocation_of_abstract_type)) | 
|  | 481 | return true; | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 482 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 483 | return false; | 
|  | 484 | } | 
|  | 485 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 486 | /// FindAllocationFunctions - Finds the overloads of operator new and delete | 
|  | 487 | /// that are appropriate for the allocation. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 488 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, | 
|  | 489 | bool UseGlobal, QualType AllocType, | 
|  | 490 | bool IsArray, Expr **PlaceArgs, | 
|  | 491 | unsigned NumPlaceArgs, | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 492 | FunctionDecl *&OperatorNew, | 
|  | 493 | FunctionDecl *&OperatorDelete) | 
|  | 494 | { | 
|  | 495 | // --- Choosing an allocation function --- | 
|  | 496 | // C++ 5.3.4p8 - 14 & 18 | 
|  | 497 | // 1) If UseGlobal is true, only look in the global scope. Else, also look | 
|  | 498 | //   in the scope of the allocated class. | 
|  | 499 | // 2) If an array size is given, look for operator new[], else look for | 
|  | 500 | //   operator new. | 
|  | 501 | // 3) The first argument is always size_t. Append the arguments from the | 
|  | 502 | //   placement form. | 
|  | 503 | // FIXME: Also find the appropriate delete operator. | 
|  | 504 |  | 
|  | 505 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); | 
|  | 506 | // We don't care about the actual value of this argument. | 
|  | 507 | // FIXME: Should the Sema create the expression and embed it in the syntax | 
|  | 508 | // tree? Or should the consumer just recalculate the value? | 
| Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 509 | AllocArgs[0] = new (Context) IntegerLiteral(llvm::APInt::getNullValue( | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 510 | Context.Target.getPointerWidth(0)), | 
|  | 511 | Context.getSizeType(), | 
|  | 512 | SourceLocation()); | 
|  | 513 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); | 
|  | 514 |  | 
|  | 515 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 516 | IsArray ? OO_Array_New : OO_New); | 
|  | 517 | if (AllocType->isRecordType() && !UseGlobal) { | 
| Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 518 | CXXRecordDecl *Record | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 519 | = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 520 | // FIXME: We fail to find inherited overloads. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 521 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 522 | AllocArgs.size(), Record, /*AllowMissing=*/true, | 
|  | 523 | OperatorNew)) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 524 | return true; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 525 | } | 
|  | 526 | if (!OperatorNew) { | 
|  | 527 | // Didn't find a member overload. Look for a global one. | 
|  | 528 | DeclareGlobalNewDelete(); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 529 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 530 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 531 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, | 
|  | 532 | OperatorNew)) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 533 | return true; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
| Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 536 | // FindAllocationOverload can change the passed in arguments, so we need to | 
|  | 537 | // copy them back. | 
|  | 538 | if (NumPlaceArgs > 0) | 
|  | 539 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); | 
|  | 540 |  | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 541 | // FIXME: This is leaked on error. But so much is currently in Sema that it's | 
|  | 542 | // easier to clean it in one go. | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 543 | AllocArgs[0]->Destroy(Context); | 
|  | 544 | return false; | 
|  | 545 | } | 
|  | 546 |  | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 547 | /// FindAllocationOverload - Find an fitting overload for the allocation | 
|  | 548 | /// function in the specified scope. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 549 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, | 
|  | 550 | DeclarationName Name, Expr** Args, | 
|  | 551 | unsigned NumArgs, DeclContext *Ctx, | 
|  | 552 | bool AllowMissing, FunctionDecl *&Operator) | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 553 | { | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 554 | DeclContext::lookup_iterator Alloc, AllocEnd; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 555 | llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name); | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 556 | if (Alloc == AllocEnd) { | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 557 | if (AllowMissing) | 
|  | 558 | return false; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 559 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) | 
| Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 560 | << Name << Range; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 561 | } | 
|  | 562 |  | 
|  | 563 | OverloadCandidateSet Candidates; | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 564 | for (; Alloc != AllocEnd; ++Alloc) { | 
|  | 565 | // Even member operator new/delete are implicitly treated as | 
|  | 566 | // static, so don't use AddMemberCandidate. | 
|  | 567 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) | 
|  | 568 | AddOverloadCandidate(Fn, Args, NumArgs, Candidates, | 
|  | 569 | /*SuppressUserConversions=*/false); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 570 | } | 
|  | 571 |  | 
|  | 572 | // Do the resolution. | 
|  | 573 | OverloadCandidateSet::iterator Best; | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 574 | switch(BestViableFunction(Candidates, StartLoc, Best)) { | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 575 | case OR_Success: { | 
|  | 576 | // Got one! | 
|  | 577 | FunctionDecl *FnDecl = Best->Function; | 
|  | 578 | // The first argument is size_t, and the first parameter must be size_t, | 
|  | 579 | // too. This is checked on declaration and can be assumed. (It can't be | 
|  | 580 | // asserted on, though, since invalid decls are left in there.) | 
|  | 581 | for (unsigned i = 1; i < NumArgs; ++i) { | 
|  | 582 | // FIXME: Passing word to diagnostic. | 
| Anders Carlsson | fc27d26 | 2009-05-31 19:49:47 +0000 | [diff] [blame] | 583 | if (PerformCopyInitialization(Args[i], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 584 | FnDecl->getParamDecl(i)->getType(), | 
|  | 585 | "passing")) | 
|  | 586 | return true; | 
|  | 587 | } | 
|  | 588 | Operator = FnDecl; | 
|  | 589 | return false; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | case OR_No_Viable_Function: | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 593 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) | 
| Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 594 | << Name << Range; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 595 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/false); | 
|  | 596 | return true; | 
|  | 597 |  | 
|  | 598 | case OR_Ambiguous: | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 599 | Diag(StartLoc, diag::err_ovl_ambiguous_call) | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 600 | << Name << Range; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 601 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); | 
|  | 602 | return true; | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 603 |  | 
|  | 604 | case OR_Deleted: | 
|  | 605 | Diag(StartLoc, diag::err_ovl_deleted_call) | 
|  | 606 | << Best->Function->isDeleted() | 
|  | 607 | << Name << Range; | 
|  | 608 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); | 
|  | 609 | return true; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 610 | } | 
|  | 611 | assert(false && "Unreachable, bad result from BestViableFunction"); | 
|  | 612 | return true; | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 616 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and | 
|  | 617 | /// delete. These are: | 
|  | 618 | /// @code | 
|  | 619 | ///   void* operator new(std::size_t) throw(std::bad_alloc); | 
|  | 620 | ///   void* operator new[](std::size_t) throw(std::bad_alloc); | 
|  | 621 | ///   void operator delete(void *) throw(); | 
|  | 622 | ///   void operator delete[](void *) throw(); | 
|  | 623 | /// @endcode | 
|  | 624 | /// Note that the placement and nothrow forms of new are *not* implicitly | 
|  | 625 | /// declared. Their use requires including \<new\>. | 
|  | 626 | void Sema::DeclareGlobalNewDelete() | 
|  | 627 | { | 
|  | 628 | if (GlobalNewDeleteDeclared) | 
|  | 629 | return; | 
|  | 630 | GlobalNewDeleteDeclared = true; | 
|  | 631 |  | 
|  | 632 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); | 
|  | 633 | QualType SizeT = Context.getSizeType(); | 
|  | 634 |  | 
|  | 635 | // FIXME: Exception specifications are not added. | 
|  | 636 | DeclareGlobalAllocationFunction( | 
|  | 637 | Context.DeclarationNames.getCXXOperatorName(OO_New), | 
|  | 638 | VoidPtr, SizeT); | 
|  | 639 | DeclareGlobalAllocationFunction( | 
|  | 640 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), | 
|  | 641 | VoidPtr, SizeT); | 
|  | 642 | DeclareGlobalAllocationFunction( | 
|  | 643 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), | 
|  | 644 | Context.VoidTy, VoidPtr); | 
|  | 645 | DeclareGlobalAllocationFunction( | 
|  | 646 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), | 
|  | 647 | Context.VoidTy, VoidPtr); | 
|  | 648 | } | 
|  | 649 |  | 
|  | 650 | /// DeclareGlobalAllocationFunction - Declares a single implicit global | 
|  | 651 | /// allocation function if it doesn't already exist. | 
|  | 652 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, | 
|  | 653 | QualType Return, QualType Argument) | 
|  | 654 | { | 
|  | 655 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); | 
|  | 656 |  | 
|  | 657 | // Check if this function is already declared. | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 658 | { | 
| Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 659 | DeclContext::lookup_iterator Alloc, AllocEnd; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 660 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 661 | Alloc != AllocEnd; ++Alloc) { | 
|  | 662 | // FIXME: Do we need to check for default arguments here? | 
|  | 663 | FunctionDecl *Func = cast<FunctionDecl>(*Alloc); | 
|  | 664 | if (Func->getNumParams() == 1 && | 
| Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 665 | Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 666 | return; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 667 | } | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0); | 
|  | 671 | FunctionDecl *Alloc = | 
|  | 672 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, | 
| Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 673 | FnType, FunctionDecl::None, false, true, | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 674 | SourceLocation()); | 
|  | 675 | Alloc->setImplicit(); | 
|  | 676 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), | 
| Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 677 | 0, Argument, VarDecl::None, 0); | 
| Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 678 | Alloc->setParams(Context, &Param, 1); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 679 |  | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 680 | // FIXME: Also add this declaration to the IdentifierResolver, but | 
|  | 681 | // make sure it is at the end of the chain to coincide with the | 
|  | 682 | // global scope. | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 683 | ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 684 | } | 
|  | 685 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 686 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: | 
|  | 687 | /// @code ::delete ptr; @endcode | 
|  | 688 | /// or | 
|  | 689 | /// @code delete [] ptr; @endcode | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 690 | Action::OwningExprResult | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 691 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 692 | bool ArrayForm, ExprArg Operand) | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 693 | { | 
|  | 694 | // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type | 
|  | 695 | //   having a single conversion function to a pointer type. The result has | 
|  | 696 | //   type void." | 
|  | 697 | // DR599 amends "pointer type" to "pointer to object type" in both cases. | 
|  | 698 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 699 | Expr *Ex = (Expr *)Operand.get(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 700 | if (!Ex->isTypeDependent()) { | 
|  | 701 | QualType Type = Ex->getType(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 702 |  | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 703 | if (Type->isRecordType()) { | 
|  | 704 | // FIXME: Find that one conversion function and amend the type. | 
|  | 705 | } | 
|  | 706 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 707 | if (!Type->isPointerType()) | 
|  | 708 | return ExprError(Diag(StartLoc, diag::err_delete_operand) | 
|  | 709 | << Type << Ex->getSourceRange()); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 710 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 711 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); | 
| Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 712 | if (Pointee->isFunctionType() || Pointee->isVoidType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 713 | return ExprError(Diag(StartLoc, diag::err_delete_operand) | 
|  | 714 | << Type << Ex->getSourceRange()); | 
| Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 715 | else if (!Pointee->isDependentType() && | 
|  | 716 | RequireCompleteType(StartLoc, Pointee, | 
|  | 717 | diag::warn_delete_incomplete, | 
|  | 718 | Ex->getSourceRange())) | 
|  | 719 | return ExprError(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 720 |  | 
|  | 721 | // FIXME: Look up the correct operator delete overload and pass a pointer | 
|  | 722 | // along. | 
|  | 723 | // FIXME: Check access and ambiguity of operator delete and destructor. | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 724 | } | 
|  | 725 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 726 | Operand.release(); | 
|  | 727 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, | 
|  | 728 | 0, Ex, StartLoc)); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 729 | } | 
|  | 730 |  | 
|  | 731 |  | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 732 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a | 
|  | 733 | /// C++ if/switch/while/for statement. | 
|  | 734 | /// e.g: "if (int x = f()) {...}" | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 735 | Action::OwningExprResult | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 736 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, | 
|  | 737 | Declarator &D, | 
|  | 738 | SourceLocation EqualLoc, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 739 | ExprArg AssignExprVal) { | 
|  | 740 | assert(AssignExprVal.get() && "Null assignment expression"); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 741 |  | 
|  | 742 | // C++ 6.4p2: | 
|  | 743 | // The declarator shall not specify a function or an array. | 
|  | 744 | // The type-specifier-seq shall not contain typedef and shall not declare a | 
|  | 745 | // new class or enumeration. | 
|  | 746 |  | 
|  | 747 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && | 
|  | 748 | "Parser allowed 'typedef' as storage class of condition decl."); | 
|  | 749 |  | 
|  | 750 | QualType Ty = GetTypeForDeclarator(D, S); | 
|  | 751 |  | 
|  | 752 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... | 
|  | 753 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl | 
|  | 754 | // would be created and CXXConditionDeclExpr wants a VarDecl. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 755 | return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type) | 
|  | 756 | << SourceRange(StartLoc, EqualLoc)); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 757 | } else if (Ty->isArrayType()) { // ...or an array. | 
| Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 758 | Diag(StartLoc, diag::err_invalid_use_of_array_type) | 
|  | 759 | << SourceRange(StartLoc, EqualLoc); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 760 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 761 | RecordDecl *RD = RT->getDecl(); | 
|  | 762 | // The type-specifier-seq shall not declare a new class... | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 763 | if (RD->isDefinition() && | 
|  | 764 | (RD->getIdentifier() == 0 || S->isDeclScope(DeclPtrTy::make(RD)))) | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 765 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); | 
|  | 766 | } else if (const EnumType *ET = Ty->getAsEnumType()) { | 
|  | 767 | EnumDecl *ED = ET->getDecl(); | 
|  | 768 | // ...or enumeration. | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 769 | if (ED->isDefinition() && | 
|  | 770 | (ED->getIdentifier() == 0 || S->isDeclScope(DeclPtrTy::make(ED)))) | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 771 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); | 
|  | 772 | } | 
|  | 773 |  | 
| Douglas Gregor | 2e01cda | 2009-06-23 21:43:56 +0000 | [diff] [blame] | 774 | DeclPtrTy Dcl = ActOnDeclarator(S, D); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 775 | if (!Dcl) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 776 | return ExprError(); | 
| Anders Carlsson | f5dcd38 | 2009-05-30 21:37:25 +0000 | [diff] [blame] | 777 | AddInitializerToDecl(Dcl, move(AssignExprVal), /*DirectInit=*/false); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 778 |  | 
| Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 779 | // Mark this variable as one that is declared within a conditional. | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 780 | // We know that the decl had to be a VarDecl because that is the only type of | 
|  | 781 | // decl that can be assigned and the grammar requires an '='. | 
|  | 782 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); | 
|  | 783 | VD->setDeclaredInCondition(true); | 
|  | 784 | return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD)); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 785 | } | 
|  | 786 |  | 
|  | 787 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. | 
|  | 788 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { | 
|  | 789 | // C++ 6.4p4: | 
|  | 790 | // The value of a condition that is an initialized declaration in a statement | 
|  | 791 | // other than a switch statement is the value of the declared variable | 
|  | 792 | // implicitly converted to type bool. If that conversion is ill-formed, the | 
|  | 793 | // program is ill-formed. | 
|  | 794 | // The value of a condition that is an expression is the value of the | 
|  | 795 | // expression, implicitly converted to bool. | 
|  | 796 | // | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 797 | return PerformContextuallyConvertToBool(CondExpr); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 798 | } | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 799 |  | 
|  | 800 | /// Helper function to determine whether this is the (deprecated) C++ | 
|  | 801 | /// conversion from a string literal to a pointer to non-const char or | 
|  | 802 | /// non-const wchar_t (for narrow and wide string literals, | 
|  | 803 | /// respectively). | 
|  | 804 | bool | 
|  | 805 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { | 
|  | 806 | // Look inside the implicit cast, if it exists. | 
|  | 807 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) | 
|  | 808 | From = Cast->getSubExpr(); | 
|  | 809 |  | 
|  | 810 | // A string literal (2.13.4) that is not a wide string literal can | 
|  | 811 | // be converted to an rvalue of type "pointer to char"; a wide | 
|  | 812 | // string literal can be converted to an rvalue of type "pointer | 
|  | 813 | // to wchar_t" (C++ 4.2p2). | 
|  | 814 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 815 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 816 | if (const BuiltinType *ToPointeeType | 
|  | 817 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { | 
|  | 818 | // This conversion is considered only when there is an | 
|  | 819 | // explicit appropriate pointer target type (C++ 4.2p2). | 
|  | 820 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && | 
|  | 821 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || | 
|  | 822 | (!StrLit->isWide() && | 
|  | 823 | (ToPointeeType->getKind() == BuiltinType::Char_U || | 
|  | 824 | ToPointeeType->getKind() == BuiltinType::Char_S)))) | 
|  | 825 | return true; | 
|  | 826 | } | 
|  | 827 |  | 
|  | 828 | return false; | 
|  | 829 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 830 |  | 
|  | 831 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 832 | /// expression From to the type ToType. Returns true if there was an | 
|  | 833 | /// error, false otherwise. The expression From is replaced with the | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 834 | /// converted expression. Flavor is the kind of conversion we're | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 835 | /// performing, used in the error message. If @p AllowExplicit, | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 836 | /// explicit user-defined conversions are permitted. @p Elidable should be true | 
|  | 837 | /// when called for copies which may be elided (C++ 12.8p15). C++0x overload | 
|  | 838 | /// resolution works differently in that case. | 
|  | 839 | bool | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 840 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 841 | const char *Flavor, bool AllowExplicit, | 
|  | 842 | bool Elidable) | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 843 | { | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 844 | ImplicitConversionSequence ICS; | 
|  | 845 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; | 
|  | 846 | if (Elidable && getLangOptions().CPlusPlus0x) { | 
|  | 847 | ICS = TryImplicitConversion(From, ToType, /*SuppressUserConversions*/false, | 
|  | 848 | AllowExplicit, /*ForceRValue*/true); | 
|  | 849 | } | 
|  | 850 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) { | 
|  | 851 | ICS = TryImplicitConversion(From, ToType, false, AllowExplicit); | 
|  | 852 | } | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 853 | return PerformImplicitConversion(From, ToType, ICS, Flavor); | 
|  | 854 | } | 
|  | 855 |  | 
|  | 856 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 857 | /// expression From to the type ToType using the pre-computed implicit | 
|  | 858 | /// conversion sequence ICS. Returns true if there was an error, false | 
|  | 859 | /// otherwise. The expression From is replaced with the converted | 
|  | 860 | /// expression. Flavor is the kind of conversion we're performing, | 
|  | 861 | /// used in the error message. | 
|  | 862 | bool | 
|  | 863 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
|  | 864 | const ImplicitConversionSequence &ICS, | 
|  | 865 | const char* Flavor) { | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 866 | switch (ICS.ConversionKind) { | 
|  | 867 | case ImplicitConversionSequence::StandardConversion: | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 868 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor)) | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 869 | return true; | 
|  | 870 | break; | 
|  | 871 |  | 
|  | 872 | case ImplicitConversionSequence::UserDefinedConversion: | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 873 | // FIXME: This is, of course, wrong. We'll need to actually call the | 
|  | 874 | // constructor or conversion operator, and then cope with the standard | 
|  | 875 | // conversions. | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 876 | ImpCastExprToType(From, ToType.getNonReferenceType(), | 
| Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 877 | ToType->isLValueReferenceType()); | 
| Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 878 | return false; | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 879 |  | 
|  | 880 | case ImplicitConversionSequence::EllipsisConversion: | 
|  | 881 | assert(false && "Cannot perform an ellipsis conversion"); | 
| Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 882 | return false; | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 883 |  | 
|  | 884 | case ImplicitConversionSequence::BadConversion: | 
|  | 885 | return true; | 
|  | 886 | } | 
|  | 887 |  | 
|  | 888 | // Everything went well. | 
|  | 889 | return false; | 
|  | 890 | } | 
|  | 891 |  | 
|  | 892 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 893 | /// expression From to the type ToType by following the standard | 
|  | 894 | /// conversion sequence SCS. Returns true if there was an error, false | 
|  | 895 | /// otherwise. The expression From is replaced with the converted | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 896 | /// expression. Flavor is the context in which we're performing this | 
|  | 897 | /// conversion, for use in error messages. | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 898 | bool | 
|  | 899 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 900 | const StandardConversionSequence& SCS, | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 901 | const char *Flavor) { | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 902 | // Overall FIXME: we are recomputing too many types here and doing far too | 
|  | 903 | // much extra work. What this means is that we need to keep track of more | 
|  | 904 | // information that is computed when we try the implicit conversion initially, | 
|  | 905 | // so that we don't need to recompute anything here. | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 906 | QualType FromType = From->getType(); | 
|  | 907 |  | 
| Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 908 | if (SCS.CopyConstructor) { | 
| Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 909 | // FIXME: When can ToType be a reference type? | 
|  | 910 | assert(!ToType->isReferenceType()); | 
|  | 911 |  | 
| Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 912 | // FIXME: Keep track of whether the copy constructor is elidable or not. | 
| Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 913 | From = CXXConstructExpr::Create(Context, ToType, | 
| Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 914 | SCS.CopyConstructor, false, &From, 1); | 
| Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 915 | return false; | 
|  | 916 | } | 
|  | 917 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 918 | // Perform the first implicit conversion. | 
|  | 919 | switch (SCS.First) { | 
|  | 920 | case ICK_Identity: | 
|  | 921 | case ICK_Lvalue_To_Rvalue: | 
|  | 922 | // Nothing to do. | 
|  | 923 | break; | 
|  | 924 |  | 
|  | 925 | case ICK_Array_To_Pointer: | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 926 | FromType = Context.getArrayDecayedType(FromType); | 
|  | 927 | ImpCastExprToType(From, FromType); | 
|  | 928 | break; | 
|  | 929 |  | 
|  | 930 | case ICK_Function_To_Pointer: | 
| Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 931 | if (Context.getCanonicalType(FromType) == Context.OverloadTy) { | 
| Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 932 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); | 
|  | 933 | if (!Fn) | 
|  | 934 | return true; | 
|  | 935 |  | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 936 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) | 
|  | 937 | return true; | 
|  | 938 |  | 
| Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 939 | FixOverloadedFunctionReference(From, Fn); | 
|  | 940 | FromType = From->getType(); | 
| Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 941 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 942 | FromType = Context.getPointerType(FromType); | 
|  | 943 | ImpCastExprToType(From, FromType); | 
|  | 944 | break; | 
|  | 945 |  | 
|  | 946 | default: | 
|  | 947 | assert(false && "Improper first standard conversion"); | 
|  | 948 | break; | 
|  | 949 | } | 
|  | 950 |  | 
|  | 951 | // Perform the second implicit conversion | 
|  | 952 | switch (SCS.Second) { | 
|  | 953 | case ICK_Identity: | 
|  | 954 | // Nothing to do. | 
|  | 955 | break; | 
|  | 956 |  | 
|  | 957 | case ICK_Integral_Promotion: | 
|  | 958 | case ICK_Floating_Promotion: | 
| Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 959 | case ICK_Complex_Promotion: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 960 | case ICK_Integral_Conversion: | 
|  | 961 | case ICK_Floating_Conversion: | 
| Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 962 | case ICK_Complex_Conversion: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 963 | case ICK_Floating_Integral: | 
| Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 964 | case ICK_Complex_Real: | 
| Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 965 | case ICK_Compatible_Conversion: | 
|  | 966 | // FIXME: Go deeper to get the unqualified type! | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 967 | FromType = ToType.getUnqualifiedType(); | 
|  | 968 | ImpCastExprToType(From, FromType); | 
|  | 969 | break; | 
|  | 970 |  | 
|  | 971 | case ICK_Pointer_Conversion: | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 972 | if (SCS.IncompatibleObjC) { | 
|  | 973 | // Diagnose incompatible Objective-C conversions | 
|  | 974 | Diag(From->getSourceRange().getBegin(), | 
|  | 975 | diag::ext_typecheck_convert_incompatible_pointer) | 
|  | 976 | << From->getType() << ToType << Flavor | 
|  | 977 | << From->getSourceRange(); | 
|  | 978 | } | 
|  | 979 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 980 | if (CheckPointerConversion(From, ToType)) | 
|  | 981 | return true; | 
|  | 982 | ImpCastExprToType(From, ToType); | 
|  | 983 | break; | 
|  | 984 |  | 
|  | 985 | case ICK_Pointer_Member: | 
| Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 986 | if (CheckMemberPointerConversion(From, ToType)) | 
|  | 987 | return true; | 
|  | 988 | ImpCastExprToType(From, ToType); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 989 | break; | 
|  | 990 |  | 
|  | 991 | case ICK_Boolean_Conversion: | 
|  | 992 | FromType = Context.BoolTy; | 
|  | 993 | ImpCastExprToType(From, FromType); | 
|  | 994 | break; | 
|  | 995 |  | 
|  | 996 | default: | 
|  | 997 | assert(false && "Improper second standard conversion"); | 
|  | 998 | break; | 
|  | 999 | } | 
|  | 1000 |  | 
|  | 1001 | switch (SCS.Third) { | 
|  | 1002 | case ICK_Identity: | 
|  | 1003 | // Nothing to do. | 
|  | 1004 | break; | 
|  | 1005 |  | 
|  | 1006 | case ICK_Qualification: | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1007 | // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue | 
|  | 1008 | // references. | 
| Douglas Gregor | 66b947f | 2009-01-16 19:38:23 +0000 | [diff] [blame] | 1009 | ImpCastExprToType(From, ToType.getNonReferenceType(), | 
| Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1010 | ToType->isLValueReferenceType()); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1011 | break; | 
|  | 1012 |  | 
|  | 1013 | default: | 
|  | 1014 | assert(false && "Improper second standard conversion"); | 
|  | 1015 | break; | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | return false; | 
|  | 1019 | } | 
|  | 1020 |  | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1021 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, | 
|  | 1022 | SourceLocation KWLoc, | 
|  | 1023 | SourceLocation LParen, | 
|  | 1024 | TypeTy *Ty, | 
|  | 1025 | SourceLocation RParen) { | 
| Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1026 | QualType T = QualType::getFromOpaquePtr(Ty); | 
|  | 1027 |  | 
|  | 1028 | // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html | 
|  | 1029 | // all traits except __is_class, __is_enum and __is_union require a the type | 
|  | 1030 | // to be complete. | 
|  | 1031 | if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) { | 
|  | 1032 | if (RequireCompleteType(KWLoc, T, | 
|  | 1033 | diag::err_incomplete_type_used_in_type_trait_expr, | 
|  | 1034 | SourceRange(), SourceRange(), T)) | 
|  | 1035 | return ExprError(); | 
|  | 1036 | } | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1037 |  | 
|  | 1038 | // There is no point in eagerly computing the value. The traits are designed | 
|  | 1039 | // to be used from type trait templates, so Ty will be a template parameter | 
|  | 1040 | // 99% of the time. | 
| Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1041 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T, | 
|  | 1042 | RParen, Context.BoolTy)); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1043 | } | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1044 |  | 
|  | 1045 | QualType Sema::CheckPointerToMemberOperands( | 
|  | 1046 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) | 
|  | 1047 | { | 
|  | 1048 | const char *OpSpelling = isIndirect ? "->*" : ".*"; | 
|  | 1049 | // C++ 5.5p2 | 
|  | 1050 | //   The binary operator .* [p3: ->*] binds its second operand, which shall | 
|  | 1051 | //   be of type "pointer to member of T" (where T is a completely-defined | 
|  | 1052 | //   class type) [...] | 
|  | 1053 | QualType RType = rex->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1054 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1055 | if (!MemPtr) { | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1056 | Diag(Loc, diag::err_bad_memptr_rhs) | 
|  | 1057 | << OpSpelling << RType << rex->getSourceRange(); | 
|  | 1058 | return QualType(); | 
| Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1059 | } | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1060 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1061 | QualType Class(MemPtr->getClass(), 0); | 
|  | 1062 |  | 
|  | 1063 | // C++ 5.5p2 | 
|  | 1064 | //   [...] to its first operand, which shall be of class T or of a class of | 
|  | 1065 | //   which T is an unambiguous and accessible base class. [p3: a pointer to | 
|  | 1066 | //   such a class] | 
|  | 1067 | QualType LType = lex->getType(); | 
|  | 1068 | if (isIndirect) { | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1069 | if (const PointerType *Ptr = LType->getAs<PointerType>()) | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1070 | LType = Ptr->getPointeeType().getNonReferenceType(); | 
|  | 1071 | else { | 
|  | 1072 | Diag(Loc, diag::err_bad_memptr_lhs) | 
|  | 1073 | << OpSpelling << 1 << LType << lex->getSourceRange(); | 
|  | 1074 | return QualType(); | 
|  | 1075 | } | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | if (Context.getCanonicalType(Class).getUnqualifiedType() != | 
|  | 1079 | Context.getCanonicalType(LType).getUnqualifiedType()) { | 
|  | 1080 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, | 
|  | 1081 | /*DetectVirtual=*/false); | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1082 | // FIXME: Would it be useful to print full ambiguity paths, or is that | 
|  | 1083 | // overkill? | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1084 | if (!IsDerivedFrom(LType, Class, Paths) || | 
|  | 1085 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { | 
|  | 1086 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling | 
|  | 1087 | << (int)isIndirect << lex->getType() << lex->getSourceRange(); | 
|  | 1088 | return QualType(); | 
|  | 1089 | } | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 | // C++ 5.5p2 | 
|  | 1093 | //   The result is an object or a function of the type specified by the | 
|  | 1094 | //   second operand. | 
|  | 1095 | // The cv qualifiers are the union of those in the pointer and the left side, | 
|  | 1096 | // in accordance with 5.5p5 and 5.2.5. | 
|  | 1097 | // FIXME: This returns a dereferenced member function pointer as a normal | 
|  | 1098 | // function type. However, the only operation valid on such functions is | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1099 | // calling them. There's also a GCC extension to get a function pointer to the | 
|  | 1100 | // thing, which is another complication, because this type - unlike the type | 
|  | 1101 | // that is the result of this expression - takes the class as the first | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1102 | // argument. | 
|  | 1103 | // We probably need a "MemberFunctionClosureType" or something like that. | 
|  | 1104 | QualType Result = MemPtr->getPointeeType(); | 
|  | 1105 | if (LType.isConstQualified()) | 
|  | 1106 | Result.addConst(); | 
|  | 1107 | if (LType.isVolatileQualified()) | 
|  | 1108 | Result.addVolatile(); | 
|  | 1109 | return Result; | 
|  | 1110 | } | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1111 |  | 
|  | 1112 | /// \brief Get the target type of a standard or user-defined conversion. | 
|  | 1113 | static QualType TargetType(const ImplicitConversionSequence &ICS) { | 
|  | 1114 | assert((ICS.ConversionKind == | 
|  | 1115 | ImplicitConversionSequence::StandardConversion || | 
|  | 1116 | ICS.ConversionKind == | 
|  | 1117 | ImplicitConversionSequence::UserDefinedConversion) && | 
|  | 1118 | "function only valid for standard or user-defined conversions"); | 
|  | 1119 | if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion) | 
|  | 1120 | return QualType::getFromOpaquePtr(ICS.Standard.ToTypePtr); | 
|  | 1121 | return QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr); | 
|  | 1122 | } | 
|  | 1123 |  | 
|  | 1124 | /// \brief Try to convert a type to another according to C++0x 5.16p3. | 
|  | 1125 | /// | 
|  | 1126 | /// This is part of the parameter validation for the ? operator. If either | 
|  | 1127 | /// value operand is a class type, the two operands are attempted to be | 
|  | 1128 | /// converted to each other. This function does the conversion in one direction. | 
|  | 1129 | /// It emits a diagnostic and returns true only if it finds an ambiguous | 
|  | 1130 | /// conversion. | 
|  | 1131 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, | 
|  | 1132 | SourceLocation QuestionLoc, | 
|  | 1133 | ImplicitConversionSequence &ICS) | 
|  | 1134 | { | 
|  | 1135 | // C++0x 5.16p3 | 
|  | 1136 | //   The process for determining whether an operand expression E1 of type T1 | 
|  | 1137 | //   can be converted to match an operand expression E2 of type T2 is defined | 
|  | 1138 | //   as follows: | 
|  | 1139 | //   -- If E2 is an lvalue: | 
|  | 1140 | if (To->isLvalue(Self.Context) == Expr::LV_Valid) { | 
|  | 1141 | //   E1 can be converted to match E2 if E1 can be implicitly converted to | 
|  | 1142 | //   type "lvalue reference to T2", subject to the constraint that in the | 
|  | 1143 | //   conversion the reference must bind directly to E1. | 
|  | 1144 | if (!Self.CheckReferenceInit(From, | 
|  | 1145 | Self.Context.getLValueReferenceType(To->getType()), | 
|  | 1146 | &ICS)) | 
|  | 1147 | { | 
|  | 1148 | assert((ICS.ConversionKind == | 
|  | 1149 | ImplicitConversionSequence::StandardConversion || | 
|  | 1150 | ICS.ConversionKind == | 
|  | 1151 | ImplicitConversionSequence::UserDefinedConversion) && | 
|  | 1152 | "expected a definite conversion"); | 
|  | 1153 | bool DirectBinding = | 
|  | 1154 | ICS.ConversionKind == ImplicitConversionSequence::StandardConversion ? | 
|  | 1155 | ICS.Standard.DirectBinding : ICS.UserDefined.After.DirectBinding; | 
|  | 1156 | if (DirectBinding) | 
|  | 1157 | return false; | 
|  | 1158 | } | 
|  | 1159 | } | 
|  | 1160 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; | 
|  | 1161 | //   -- If E2 is an rvalue, or if the conversion above cannot be done: | 
|  | 1162 | //      -- if E1 and E2 have class type, and the underlying class types are | 
|  | 1163 | //         the same or one is a base class of the other: | 
|  | 1164 | QualType FTy = From->getType(); | 
|  | 1165 | QualType TTy = To->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1166 | const RecordType *FRec = FTy->getAs<RecordType>(); | 
|  | 1167 | const RecordType *TRec = TTy->getAs<RecordType>(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1168 | bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); | 
|  | 1169 | if (FRec && TRec && (FRec == TRec || | 
|  | 1170 | FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { | 
|  | 1171 | //         E1 can be converted to match E2 if the class of T2 is the | 
|  | 1172 | //         same type as, or a base class of, the class of T1, and | 
|  | 1173 | //         [cv2 > cv1]. | 
|  | 1174 | if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) { | 
|  | 1175 | // Could still fail if there's no copy constructor. | 
|  | 1176 | // FIXME: Is this a hard error then, or just a conversion failure? The | 
|  | 1177 | // standard doesn't say. | 
|  | 1178 | ICS = Self.TryCopyInitialization(From, TTy); | 
|  | 1179 | } | 
|  | 1180 | } else { | 
|  | 1181 | //     -- Otherwise: E1 can be converted to match E2 if E1 can be | 
|  | 1182 | //        implicitly converted to the type that expression E2 would have | 
|  | 1183 | //        if E2 were converted to an rvalue. | 
|  | 1184 | // First find the decayed type. | 
|  | 1185 | if (TTy->isFunctionType()) | 
|  | 1186 | TTy = Self.Context.getPointerType(TTy); | 
|  | 1187 | else if(TTy->isArrayType()) | 
|  | 1188 | TTy = Self.Context.getArrayDecayedType(TTy); | 
|  | 1189 |  | 
|  | 1190 | // Now try the implicit conversion. | 
|  | 1191 | // FIXME: This doesn't detect ambiguities. | 
|  | 1192 | ICS = Self.TryImplicitConversion(From, TTy); | 
|  | 1193 | } | 
|  | 1194 | return false; | 
|  | 1195 | } | 
|  | 1196 |  | 
|  | 1197 | /// \brief Try to find a common type for two according to C++0x 5.16p5. | 
|  | 1198 | /// | 
|  | 1199 | /// This is part of the parameter validation for the ? operator. If either | 
|  | 1200 | /// value operand is a class type, overload resolution is used to find a | 
|  | 1201 | /// conversion to a common type. | 
|  | 1202 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, | 
|  | 1203 | SourceLocation Loc) { | 
|  | 1204 | Expr *Args[2] = { LHS, RHS }; | 
|  | 1205 | OverloadCandidateSet CandidateSet; | 
|  | 1206 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Args, 2, CandidateSet); | 
|  | 1207 |  | 
|  | 1208 | OverloadCandidateSet::iterator Best; | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1209 | switch (Self.BestViableFunction(CandidateSet, Loc, Best)) { | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1210 | case Sema::OR_Success: | 
|  | 1211 | // We found a match. Perform the conversions on the arguments and move on. | 
|  | 1212 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], | 
|  | 1213 | Best->Conversions[0], "converting") || | 
|  | 1214 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], | 
|  | 1215 | Best->Conversions[1], "converting")) | 
|  | 1216 | break; | 
|  | 1217 | return false; | 
|  | 1218 |  | 
|  | 1219 | case Sema::OR_No_Viable_Function: | 
|  | 1220 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) | 
|  | 1221 | << LHS->getType() << RHS->getType() | 
|  | 1222 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 1223 | return true; | 
|  | 1224 |  | 
|  | 1225 | case Sema::OR_Ambiguous: | 
|  | 1226 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) | 
|  | 1227 | << LHS->getType() << RHS->getType() | 
|  | 1228 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1229 | // FIXME: Print the possible common types by printing the return types of | 
|  | 1230 | // the viable candidates. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1231 | break; | 
|  | 1232 |  | 
|  | 1233 | case Sema::OR_Deleted: | 
|  | 1234 | assert(false && "Conditional operator has only built-in overloads"); | 
|  | 1235 | break; | 
|  | 1236 | } | 
|  | 1237 | return true; | 
|  | 1238 | } | 
|  | 1239 |  | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1240 | /// \brief Perform an "extended" implicit conversion as returned by | 
|  | 1241 | /// TryClassUnification. | 
|  | 1242 | /// | 
|  | 1243 | /// TryClassUnification generates ICSs that include reference bindings. | 
|  | 1244 | /// PerformImplicitConversion is not suitable for this; it chokes if the | 
|  | 1245 | /// second part of a standard conversion is ICK_DerivedToBase. This function | 
|  | 1246 | /// handles the reference binding specially. | 
|  | 1247 | static bool ConvertForConditional(Sema &Self, Expr *&E, | 
|  | 1248 | const ImplicitConversionSequence &ICS) | 
|  | 1249 | { | 
|  | 1250 | if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion && | 
|  | 1251 | ICS.Standard.ReferenceBinding) { | 
|  | 1252 | assert(ICS.Standard.DirectBinding && | 
|  | 1253 | "TryClassUnification should never generate indirect ref bindings"); | 
| Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1254 | // FIXME: CheckReferenceInit should be able to reuse the ICS instead of | 
|  | 1255 | // redoing all the work. | 
|  | 1256 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( | 
|  | 1257 | TargetType(ICS))); | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1258 | } | 
|  | 1259 | if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion && | 
|  | 1260 | ICS.UserDefined.After.ReferenceBinding) { | 
|  | 1261 | assert(ICS.UserDefined.After.DirectBinding && | 
|  | 1262 | "TryClassUnification should never generate indirect ref bindings"); | 
| Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1263 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( | 
|  | 1264 | TargetType(ICS))); | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1265 | } | 
|  | 1266 | if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, "converting")) | 
|  | 1267 | return true; | 
|  | 1268 | return false; | 
|  | 1269 | } | 
|  | 1270 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1271 | /// \brief Check the operands of ?: under C++ semantics. | 
|  | 1272 | /// | 
|  | 1273 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y | 
|  | 1274 | /// extension. In this case, LHS == Cond. (But they're not aliases.) | 
|  | 1275 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, | 
|  | 1276 | SourceLocation QuestionLoc) { | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1277 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ | 
|  | 1278 | // interface pointers. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1279 |  | 
|  | 1280 | // C++0x 5.16p1 | 
|  | 1281 | //   The first expression is contextually converted to bool. | 
|  | 1282 | if (!Cond->isTypeDependent()) { | 
|  | 1283 | if (CheckCXXBooleanCondition(Cond)) | 
|  | 1284 | return QualType(); | 
|  | 1285 | } | 
|  | 1286 |  | 
|  | 1287 | // Either of the arguments dependent? | 
|  | 1288 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) | 
|  | 1289 | return Context.DependentTy; | 
|  | 1290 |  | 
|  | 1291 | // C++0x 5.16p2 | 
|  | 1292 | //   If either the second or the third operand has type (cv) void, ... | 
|  | 1293 | QualType LTy = LHS->getType(); | 
|  | 1294 | QualType RTy = RHS->getType(); | 
|  | 1295 | bool LVoid = LTy->isVoidType(); | 
|  | 1296 | bool RVoid = RTy->isVoidType(); | 
|  | 1297 | if (LVoid || RVoid) { | 
|  | 1298 | //   ... then the [l2r] conversions are performed on the second and third | 
|  | 1299 | //   operands ... | 
|  | 1300 | DefaultFunctionArrayConversion(LHS); | 
|  | 1301 | DefaultFunctionArrayConversion(RHS); | 
|  | 1302 | LTy = LHS->getType(); | 
|  | 1303 | RTy = RHS->getType(); | 
|  | 1304 |  | 
|  | 1305 | //   ... and one of the following shall hold: | 
|  | 1306 | //   -- The second or the third operand (but not both) is a throw- | 
|  | 1307 | //      expression; the result is of the type of the other and is an rvalue. | 
|  | 1308 | bool LThrow = isa<CXXThrowExpr>(LHS); | 
|  | 1309 | bool RThrow = isa<CXXThrowExpr>(RHS); | 
|  | 1310 | if (LThrow && !RThrow) | 
|  | 1311 | return RTy; | 
|  | 1312 | if (RThrow && !LThrow) | 
|  | 1313 | return LTy; | 
|  | 1314 |  | 
|  | 1315 | //   -- Both the second and third operands have type void; the result is of | 
|  | 1316 | //      type void and is an rvalue. | 
|  | 1317 | if (LVoid && RVoid) | 
|  | 1318 | return Context.VoidTy; | 
|  | 1319 |  | 
|  | 1320 | // Neither holds, error. | 
|  | 1321 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) | 
|  | 1322 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) | 
|  | 1323 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 1324 | return QualType(); | 
|  | 1325 | } | 
|  | 1326 |  | 
|  | 1327 | // Neither is void. | 
|  | 1328 |  | 
|  | 1329 | // C++0x 5.16p3 | 
|  | 1330 | //   Otherwise, if the second and third operand have different types, and | 
|  | 1331 | //   either has (cv) class type, and attempt is made to convert each of those | 
|  | 1332 | //   operands to the other. | 
|  | 1333 | if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && | 
|  | 1334 | (LTy->isRecordType() || RTy->isRecordType())) { | 
|  | 1335 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; | 
|  | 1336 | // These return true if a single direction is already ambiguous. | 
|  | 1337 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) | 
|  | 1338 | return QualType(); | 
|  | 1339 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) | 
|  | 1340 | return QualType(); | 
|  | 1341 |  | 
|  | 1342 | bool HaveL2R = ICSLeftToRight.ConversionKind != | 
|  | 1343 | ImplicitConversionSequence::BadConversion; | 
|  | 1344 | bool HaveR2L = ICSRightToLeft.ConversionKind != | 
|  | 1345 | ImplicitConversionSequence::BadConversion; | 
|  | 1346 | //   If both can be converted, [...] the program is ill-formed. | 
|  | 1347 | if (HaveL2R && HaveR2L) { | 
|  | 1348 | Diag(QuestionLoc, diag::err_conditional_ambiguous) | 
|  | 1349 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 1350 | return QualType(); | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | //   If exactly one conversion is possible, that conversion is applied to | 
|  | 1354 | //   the chosen operand and the converted operands are used in place of the | 
|  | 1355 | //   original operands for the remainder of this section. | 
|  | 1356 | if (HaveL2R) { | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1357 | if (ConvertForConditional(*this, LHS, ICSLeftToRight)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1358 | return QualType(); | 
|  | 1359 | LTy = LHS->getType(); | 
|  | 1360 | } else if (HaveR2L) { | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1361 | if (ConvertForConditional(*this, RHS, ICSRightToLeft)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1362 | return QualType(); | 
|  | 1363 | RTy = RHS->getType(); | 
|  | 1364 | } | 
|  | 1365 | } | 
|  | 1366 |  | 
|  | 1367 | // C++0x 5.16p4 | 
|  | 1368 | //   If the second and third operands are lvalues and have the same type, | 
|  | 1369 | //   the result is of that type [...] | 
|  | 1370 | bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); | 
|  | 1371 | if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && | 
|  | 1372 | RHS->isLvalue(Context) == Expr::LV_Valid) | 
|  | 1373 | return LTy; | 
|  | 1374 |  | 
|  | 1375 | // C++0x 5.16p5 | 
|  | 1376 | //   Otherwise, the result is an rvalue. If the second and third operands | 
|  | 1377 | //   do not have the same type, and either has (cv) class type, ... | 
|  | 1378 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { | 
|  | 1379 | //   ... overload resolution is used to determine the conversions (if any) | 
|  | 1380 | //   to be applied to the operands. If the overload resolution fails, the | 
|  | 1381 | //   program is ill-formed. | 
|  | 1382 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) | 
|  | 1383 | return QualType(); | 
|  | 1384 | } | 
|  | 1385 |  | 
|  | 1386 | // C++0x 5.16p6 | 
|  | 1387 | //   LValue-to-rvalue, array-to-pointer, and function-to-pointer standard | 
|  | 1388 | //   conversions are performed on the second and third operands. | 
|  | 1389 | DefaultFunctionArrayConversion(LHS); | 
|  | 1390 | DefaultFunctionArrayConversion(RHS); | 
|  | 1391 | LTy = LHS->getType(); | 
|  | 1392 | RTy = RHS->getType(); | 
|  | 1393 |  | 
|  | 1394 | //   After those conversions, one of the following shall hold: | 
|  | 1395 | //   -- The second and third operands have the same type; the result | 
|  | 1396 | //      is of that type. | 
|  | 1397 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) | 
|  | 1398 | return LTy; | 
|  | 1399 |  | 
|  | 1400 | //   -- The second and third operands have arithmetic or enumeration type; | 
|  | 1401 | //      the usual arithmetic conversions are performed to bring them to a | 
|  | 1402 | //      common type, and the result is of that type. | 
|  | 1403 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { | 
|  | 1404 | UsualArithmeticConversions(LHS, RHS); | 
|  | 1405 | return LHS->getType(); | 
|  | 1406 | } | 
|  | 1407 |  | 
|  | 1408 | //   -- The second and third operands have pointer type, or one has pointer | 
|  | 1409 | //      type and the other is a null pointer constant; pointer conversions | 
|  | 1410 | //      and qualification conversions are performed to bring them to their | 
|  | 1411 | //      composite pointer type. The result is of the composite pointer type. | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1412 | QualType Composite = FindCompositePointerType(LHS, RHS); | 
|  | 1413 | if (!Composite.isNull()) | 
|  | 1414 | return Composite; | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1415 |  | 
| Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1416 | // Fourth bullet is same for pointers-to-member. However, the possible | 
|  | 1417 | // conversions are far more limited: we have null-to-pointer, upcast of | 
|  | 1418 | // containing class, and second-level cv-ness. | 
|  | 1419 | // cv-ness is not a union, but must match one of the two operands. (Which, | 
|  | 1420 | // frankly, is stupid.) | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1421 | const MemberPointerType *LMemPtr = LTy->getAs<MemberPointerType>(); | 
|  | 1422 | const MemberPointerType *RMemPtr = RTy->getAs<MemberPointerType>(); | 
| Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1423 | if (LMemPtr && RHS->isNullPointerConstant(Context)) { | 
|  | 1424 | ImpCastExprToType(RHS, LTy); | 
|  | 1425 | return LTy; | 
|  | 1426 | } | 
|  | 1427 | if (RMemPtr && LHS->isNullPointerConstant(Context)) { | 
|  | 1428 | ImpCastExprToType(LHS, RTy); | 
|  | 1429 | return RTy; | 
|  | 1430 | } | 
|  | 1431 | if (LMemPtr && RMemPtr) { | 
|  | 1432 | QualType LPointee = LMemPtr->getPointeeType(); | 
|  | 1433 | QualType RPointee = RMemPtr->getPointeeType(); | 
|  | 1434 | // First, we check that the unqualified pointee type is the same. If it's | 
|  | 1435 | // not, there's no conversion that will unify the two pointers. | 
|  | 1436 | if (Context.getCanonicalType(LPointee).getUnqualifiedType() == | 
|  | 1437 | Context.getCanonicalType(RPointee).getUnqualifiedType()) { | 
|  | 1438 | // Second, we take the greater of the two cv qualifications. If neither | 
|  | 1439 | // is greater than the other, the conversion is not possible. | 
|  | 1440 | unsigned Q = LPointee.getCVRQualifiers() | RPointee.getCVRQualifiers(); | 
|  | 1441 | if (Q == LPointee.getCVRQualifiers() || Q == RPointee.getCVRQualifiers()){ | 
|  | 1442 | // Third, we check if either of the container classes is derived from | 
|  | 1443 | // the other. | 
|  | 1444 | QualType LContainer(LMemPtr->getClass(), 0); | 
|  | 1445 | QualType RContainer(RMemPtr->getClass(), 0); | 
|  | 1446 | QualType MoreDerived; | 
|  | 1447 | if (Context.getCanonicalType(LContainer) == | 
|  | 1448 | Context.getCanonicalType(RContainer)) | 
|  | 1449 | MoreDerived = LContainer; | 
|  | 1450 | else if (IsDerivedFrom(LContainer, RContainer)) | 
|  | 1451 | MoreDerived = LContainer; | 
|  | 1452 | else if (IsDerivedFrom(RContainer, LContainer)) | 
|  | 1453 | MoreDerived = RContainer; | 
|  | 1454 |  | 
|  | 1455 | if (!MoreDerived.isNull()) { | 
|  | 1456 | // The type 'Q Pointee (MoreDerived::*)' is the common type. | 
|  | 1457 | // We don't use ImpCastExprToType here because this could still fail | 
|  | 1458 | // for ambiguous or inaccessible conversions. | 
|  | 1459 | QualType Common = Context.getMemberPointerType( | 
|  | 1460 | LPointee.getQualifiedType(Q), MoreDerived.getTypePtr()); | 
|  | 1461 | if (PerformImplicitConversion(LHS, Common, "converting")) | 
|  | 1462 | return QualType(); | 
|  | 1463 | if (PerformImplicitConversion(RHS, Common, "converting")) | 
|  | 1464 | return QualType(); | 
|  | 1465 | return Common; | 
|  | 1466 | } | 
|  | 1467 | } | 
|  | 1468 | } | 
|  | 1469 | } | 
|  | 1470 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1471 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) | 
|  | 1472 | << LHS->getType() << RHS->getType() | 
|  | 1473 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 1474 | return QualType(); | 
|  | 1475 | } | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1476 |  | 
|  | 1477 | /// \brief Find a merged pointer type and convert the two expressions to it. | 
|  | 1478 | /// | 
|  | 1479 | /// This finds the composite pointer type for @p E1 and @p E2 according to | 
|  | 1480 | /// C++0x 5.9p2. It converts both expressions to this type and returns it. | 
|  | 1481 | /// It does not emit diagnostics. | 
|  | 1482 | QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { | 
|  | 1483 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); | 
|  | 1484 | QualType T1 = E1->getType(), T2 = E2->getType(); | 
| Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1485 | if(!T1->isAnyPointerType() && !T2->isAnyPointerType()) | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1486 | return QualType(); | 
|  | 1487 |  | 
|  | 1488 | // C++0x 5.9p2 | 
|  | 1489 | //   Pointer conversions and qualification conversions are performed on | 
|  | 1490 | //   pointer operands to bring them to their composite pointer type. If | 
|  | 1491 | //   one operand is a null pointer constant, the composite pointer type is | 
|  | 1492 | //   the type of the other operand. | 
|  | 1493 | if (E1->isNullPointerConstant(Context)) { | 
|  | 1494 | ImpCastExprToType(E1, T2); | 
|  | 1495 | return T2; | 
|  | 1496 | } | 
|  | 1497 | if (E2->isNullPointerConstant(Context)) { | 
|  | 1498 | ImpCastExprToType(E2, T1); | 
|  | 1499 | return T1; | 
|  | 1500 | } | 
|  | 1501 | // Now both have to be pointers. | 
|  | 1502 | if(!T1->isPointerType() || !T2->isPointerType()) | 
|  | 1503 | return QualType(); | 
|  | 1504 |  | 
|  | 1505 | //   Otherwise, of one of the operands has type "pointer to cv1 void," then | 
|  | 1506 | //   the other has type "pointer to cv2 T" and the composite pointer type is | 
|  | 1507 | //   "pointer to cv12 void," where cv12 is the union of cv1 and cv2. | 
|  | 1508 | //   Otherwise, the composite pointer type is a pointer type similar to the | 
|  | 1509 | //   type of one of the operands, with a cv-qualification signature that is | 
|  | 1510 | //   the union of the cv-qualification signatures of the operand types. | 
|  | 1511 | // In practice, the first part here is redundant; it's subsumed by the second. | 
|  | 1512 | // What we do here is, we build the two possible composite types, and try the | 
|  | 1513 | // conversions in both directions. If only one works, or if the two composite | 
|  | 1514 | // types are the same, we have succeeded. | 
|  | 1515 | llvm::SmallVector<unsigned, 4> QualifierUnion; | 
|  | 1516 | QualType Composite1 = T1, Composite2 = T2; | 
|  | 1517 | const PointerType *Ptr1, *Ptr2; | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1518 | while ((Ptr1 = Composite1->getAs<PointerType>()) && | 
|  | 1519 | (Ptr2 = Composite2->getAs<PointerType>())) { | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1520 | Composite1 = Ptr1->getPointeeType(); | 
|  | 1521 | Composite2 = Ptr2->getPointeeType(); | 
|  | 1522 | QualifierUnion.push_back( | 
|  | 1523 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); | 
|  | 1524 | } | 
|  | 1525 | // Rewrap the composites as pointers with the union CVRs. | 
|  | 1526 | for (llvm::SmallVector<unsigned, 4>::iterator I = QualifierUnion.begin(), | 
|  | 1527 | E = QualifierUnion.end(); I != E; ++I) { | 
|  | 1528 | Composite1 = Context.getPointerType(Composite1.getQualifiedType(*I)); | 
|  | 1529 | Composite2 = Context.getPointerType(Composite2.getQualifiedType(*I)); | 
|  | 1530 | } | 
|  | 1531 |  | 
|  | 1532 | ImplicitConversionSequence E1ToC1 = TryImplicitConversion(E1, Composite1); | 
|  | 1533 | ImplicitConversionSequence E2ToC1 = TryImplicitConversion(E2, Composite1); | 
|  | 1534 | ImplicitConversionSequence E1ToC2, E2ToC2; | 
|  | 1535 | E1ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; | 
|  | 1536 | E2ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; | 
|  | 1537 | if (Context.getCanonicalType(Composite1) != | 
|  | 1538 | Context.getCanonicalType(Composite2)) { | 
|  | 1539 | E1ToC2 = TryImplicitConversion(E1, Composite2); | 
|  | 1540 | E2ToC2 = TryImplicitConversion(E2, Composite2); | 
|  | 1541 | } | 
|  | 1542 |  | 
|  | 1543 | bool ToC1Viable = E1ToC1.ConversionKind != | 
|  | 1544 | ImplicitConversionSequence::BadConversion | 
|  | 1545 | && E2ToC1.ConversionKind != | 
|  | 1546 | ImplicitConversionSequence::BadConversion; | 
|  | 1547 | bool ToC2Viable = E1ToC2.ConversionKind != | 
|  | 1548 | ImplicitConversionSequence::BadConversion | 
|  | 1549 | && E2ToC2.ConversionKind != | 
|  | 1550 | ImplicitConversionSequence::BadConversion; | 
|  | 1551 | if (ToC1Viable && !ToC2Viable) { | 
|  | 1552 | if (!PerformImplicitConversion(E1, Composite1, E1ToC1, "converting") && | 
|  | 1553 | !PerformImplicitConversion(E2, Composite1, E2ToC1, "converting")) | 
|  | 1554 | return Composite1; | 
|  | 1555 | } | 
|  | 1556 | if (ToC2Viable && !ToC1Viable) { | 
|  | 1557 | if (!PerformImplicitConversion(E1, Composite2, E1ToC2, "converting") && | 
|  | 1558 | !PerformImplicitConversion(E2, Composite2, E2ToC2, "converting")) | 
|  | 1559 | return Composite2; | 
|  | 1560 | } | 
|  | 1561 | return QualType(); | 
|  | 1562 | } | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 1563 |  | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 1564 | Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1565 | const RecordType *RT = E->getType()->getAs<RecordType>(); | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 1566 | if (!RT) | 
|  | 1567 | return Owned(E); | 
|  | 1568 |  | 
|  | 1569 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 1570 | if (RD->hasTrivialDestructor()) | 
|  | 1571 | return Owned(E); | 
|  | 1572 |  | 
|  | 1573 | CXXTemporary *Temp = CXXTemporary::Create(Context, | 
|  | 1574 | RD->getDestructor(Context)); | 
| Anders Carlsson | 860306e | 2009-05-30 21:21:49 +0000 | [diff] [blame] | 1575 | ExprTemporaries.push_back(Temp); | 
| Fariborz Jahanian | f8d736c | 2009-06-27 15:05:11 +0000 | [diff] [blame] | 1576 | MarkDestructorReferenced(E->getExprLoc(), E->getType()); | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 1577 | // FIXME: Add the temporary to the temporaries vector. | 
|  | 1578 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); | 
|  | 1579 | } | 
|  | 1580 |  | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 1581 | Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr, | 
| Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 1582 | bool ShouldDestroyTemps) { | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 1583 | assert(SubExpr && "sub expression can't be null!"); | 
|  | 1584 |  | 
|  | 1585 | if (ExprTemporaries.empty()) | 
|  | 1586 | return SubExpr; | 
|  | 1587 |  | 
|  | 1588 | Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, | 
|  | 1589 | &ExprTemporaries[0], | 
|  | 1590 | ExprTemporaries.size(), | 
| Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 1591 | ShouldDestroyTemps); | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 1592 | ExprTemporaries.clear(); | 
|  | 1593 |  | 
|  | 1594 | return E; | 
|  | 1595 | } | 
|  | 1596 |  | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 1597 | Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { | 
|  | 1598 | Expr *FullExpr = Arg.takeAs<Expr>(); | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 1599 | if (FullExpr) | 
| Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 1600 | FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr, | 
|  | 1601 | /*ShouldDestroyTemps=*/true); | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 1602 |  | 
|  | 1603 | return Owned(FullExpr); | 
|  | 1604 | } |