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