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 | |
| 14 | #include "Sema.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
| 18 | #include "clang/Basic/PartialDiagnostic.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 25 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 26 | Action::OwningExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 27 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 28 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 29 | if (!StdNamespace) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 30 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 31 | |
| 32 | if (isType) |
| 33 | // FIXME: Preserve type source info. |
| 34 | TyOrExpr = GetTypeFromParser(TyOrExpr).getAsOpaquePtr(); |
| 35 | |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 36 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 37 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 38 | LookupQualifiedName(R, StdNamespace); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 39 | Decl *TypeInfoDecl = R.getAsSingleDecl(Context); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 40 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 41 | if (!TypeInfoRecordDecl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 42 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 43 | |
| 44 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 45 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 46 | if (!isType) { |
| 47 | // C++0x [expr.typeid]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | // When typeid is applied to an expression other than an lvalue of a |
| 49 | // polymorphic class type [...] [the] expression is an unevaluated |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 50 | // operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 52 | // FIXME: if the type of the expression is a class type, the class |
| 53 | // shall be completely defined. |
| 54 | bool isUnevaluatedOperand = true; |
| 55 | Expr *E = static_cast<Expr *>(TyOrExpr); |
| 56 | if (E && !E->isTypeDependent() && E->isLvalue(Context) == Expr::LV_Valid) { |
| 57 | QualType T = E->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 58 | if (const RecordType *RecordT = T->getAs<RecordType>()) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 59 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); |
| 60 | if (RecordD->isPolymorphic()) |
| 61 | isUnevaluatedOperand = false; |
| 62 | } |
| 63 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 65 | // If this is an unevaluated operand, clear out the set of declaration |
| 66 | // references we have been computing. |
| 67 | if (isUnevaluatedOperand) |
| 68 | PotentiallyReferencedDeclStack.back().clear(); |
| 69 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 71 | return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, |
| 72 | TypeInfoType.withConst(), |
| 73 | SourceRange(OpLoc, RParenLoc))); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 76 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 77 | Action::OwningExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 78 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 79 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 81 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 82 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 84 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 85 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
| 86 | Action::OwningExprResult |
| 87 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 88 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 89 | } |
| 90 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 91 | /// ActOnCXXThrow - Parse throw expressions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 92 | Action::OwningExprResult |
| 93 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 94 | Expr *Ex = E.takeAs<Expr>(); |
| 95 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) |
| 96 | return ExprError(); |
| 97 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); |
| 98 | } |
| 99 | |
| 100 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
| 101 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { |
| 102 | // C++ [except.throw]p3: |
| 103 | // [...] adjusting the type from "array of T" or "function returning T" |
| 104 | // to "pointer to T" or "pointer to function returning T", [...] |
| 105 | DefaultFunctionArrayConversion(E); |
| 106 | |
| 107 | // If the type of the exception would be an incomplete type or a pointer |
| 108 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 109 | QualType Ty = E->getType(); |
| 110 | int isPointer = 0; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 111 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 112 | Ty = Ptr->getPointeeType(); |
| 113 | isPointer = 1; |
| 114 | } |
| 115 | if (!isPointer || !Ty->isVoidType()) { |
| 116 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 117 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 118 | : diag::err_throw_incomplete) |
| 119 | << E->getSourceRange())) |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // FIXME: Construct a temporary here. |
| 124 | return false; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 125 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 126 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 127 | Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 128 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 129 | /// is a non-lvalue expression whose value is the address of the object for |
| 130 | /// which the function is called. |
| 131 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 132 | if (!isa<FunctionDecl>(CurContext)) |
| 133 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 134 | |
| 135 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 136 | if (MD->isInstance()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 137 | return Owned(new (Context) CXXThisExpr(ThisLoc, |
| 138 | MD->getThisType(Context))); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 139 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 140 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 141 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 142 | |
| 143 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 144 | /// Can be interpreted either as function-style casting ("int(x)") |
| 145 | /// or class type construction ("ClassType(x,y,z)") |
| 146 | /// or creation of a value-initialized type ("int()"). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 147 | Action::OwningExprResult |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 148 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 149 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 150 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 151 | SourceLocation *CommaLocs, |
| 152 | SourceLocation RParenLoc) { |
| 153 | assert(TypeRep && "Missing type!"); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 154 | // FIXME: Preserve type source info. |
| 155 | QualType Ty = GetTypeFromParser(TypeRep); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 156 | unsigned NumExprs = exprs.size(); |
| 157 | Expr **Exprs = (Expr**)exprs.get(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 158 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 159 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 160 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 161 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 162 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 163 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
| 165 | return Owned(CXXUnresolvedConstructExpr::Create(Context, |
| 166 | TypeRange.getBegin(), Ty, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 167 | LParenLoc, |
| 168 | Exprs, NumExprs, |
| 169 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 172 | if (Ty->isArrayType()) |
| 173 | return ExprError(Diag(TyBeginLoc, |
| 174 | diag::err_value_init_for_array_type) << FullRange); |
| 175 | if (!Ty->isVoidType() && |
| 176 | RequireCompleteType(TyBeginLoc, Ty, |
| 177 | PDiag(diag::err_invalid_incomplete_type_use) |
| 178 | << FullRange)) |
| 179 | return ExprError(); |
Fariborz Jahanian | f071e9b | 2009-10-23 21:01:39 +0000 | [diff] [blame] | 180 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 181 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 182 | diag::err_allocation_of_abstract_type)) |
| 183 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
| 185 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 186 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 187 | // If the expression list is a single expression, the type conversion |
| 188 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 189 | // corresponding cast expression. |
| 190 | // |
| 191 | if (NumExprs == 1) { |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 192 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 193 | CXXMethodDecl *Method = 0; |
| 194 | if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method, |
| 195 | /*FunctionalStyle=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 196 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 197 | |
| 198 | exprs.release(); |
| 199 | if (Method) { |
| 200 | OwningExprResult CastArg |
| 201 | = BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(), |
| 202 | Kind, Method, Owned(Exprs[0])); |
| 203 | if (CastArg.isInvalid()) |
| 204 | return ExprError(); |
| 205 | |
| 206 | Exprs[0] = CastArg.takeAs<Expr>(); |
Fariborz Jahanian | 4fc7ab3 | 2009-08-28 15:11:24 +0000 | [diff] [blame] | 207 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 208 | |
| 209 | return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), |
| 210 | Ty, TyBeginLoc, Kind, |
| 211 | Exprs[0], RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 214 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 215 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 216 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | if (NumExprs > 1 || !Record->hasTrivialConstructor() || |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 218 | !Record->hasTrivialDestructor()) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 219 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 220 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 221 | CXXConstructorDecl *Constructor |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 222 | = PerformInitializationByConstructor(Ty, move(exprs), |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 223 | TypeRange.getBegin(), |
| 224 | SourceRange(TypeRange.getBegin(), |
| 225 | RParenLoc), |
| 226 | DeclarationName(), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 227 | IK_Direct, |
| 228 | ConstructorArgs); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 229 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 230 | if (!Constructor) |
| 231 | return ExprError(); |
| 232 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | OwningExprResult Result = |
| 234 | BuildCXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 235 | move_arg(ConstructorArgs), RParenLoc); |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 236 | if (Result.isInvalid()) |
| 237 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 239 | return MaybeBindToTemporary(Result.takeAs<Expr>()); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Fall through to value-initialize an object of class type that |
| 243 | // doesn't have a user-declared default constructor. |
| 244 | } |
| 245 | |
| 246 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 247 | // If the expression list specifies more than a single value, the type shall |
| 248 | // be a class with a suitably declared constructor. |
| 249 | // |
| 250 | if (NumExprs > 1) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 251 | return ExprError(Diag(CommaLocs[0], |
| 252 | diag::err_builtin_func_cast_more_than_one_arg) |
| 253 | << FullRange); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 254 | |
| 255 | assert(NumExprs == 0 && "Expected 0 expressions"); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 256 | // C++ [expr.type.conv]p2: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 257 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 258 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 259 | // rvalue of the specified type, which is value-initialized. |
| 260 | // |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 261 | exprs.release(); |
| 262 | return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 263 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 264 | |
| 265 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 266 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 267 | /// @code new (memory) int[size][4] @endcode |
| 268 | /// or |
| 269 | /// @code ::new Foo(23, "hello") @endcode |
| 270 | /// For the interpretation of this heap of arguments, consult the base version. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 271 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 272 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 273 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 274 | SourceLocation PlacementRParen, bool ParenTypeId, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 275 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 276 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | SourceLocation ConstructorRParen) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 278 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 279 | // If the specified type is an array, unwrap it and save the expression. |
| 280 | if (D.getNumTypeObjects() > 0 && |
| 281 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 282 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 283 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 284 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 285 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 286 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 287 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 288 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 289 | |
| 290 | if (ParenTypeId) { |
| 291 | // Can't have dynamic array size when the type-id is in parentheses. |
| 292 | Expr *NumElts = (Expr *)Chunk.Arr.NumElts; |
| 293 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 294 | !NumElts->isIntegerConstantExpr(Context)) { |
| 295 | Diag(D.getTypeObject(0).Loc, diag::err_new_paren_array_nonconst) |
| 296 | << NumElts->getSourceRange(); |
| 297 | return ExprError(); |
| 298 | } |
| 299 | } |
| 300 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 301 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 302 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 305 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 306 | if (ArraySize) { |
| 307 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 308 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 309 | break; |
| 310 | |
| 311 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 312 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 313 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 314 | !NumElts->isIntegerConstantExpr(Context)) { |
| 315 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 316 | << NumElts->getSourceRange(); |
| 317 | return ExprError(); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 322 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 323 | //FIXME: Store DeclaratorInfo in CXXNew expression. |
| 324 | DeclaratorInfo *DInfo = 0; |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 325 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &DInfo); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 326 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 327 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 328 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 330 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 332 | PlacementRParen, |
| 333 | ParenTypeId, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | AllocType, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 335 | D.getSourceRange().getBegin(), |
| 336 | D.getSourceRange(), |
| 337 | Owned(ArraySize), |
| 338 | ConstructorLParen, |
| 339 | move(ConstructorArgs), |
| 340 | ConstructorRParen); |
| 341 | } |
| 342 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | Sema::OwningExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 344 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 345 | SourceLocation PlacementLParen, |
| 346 | MultiExprArg PlacementArgs, |
| 347 | SourceLocation PlacementRParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | bool ParenTypeId, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 349 | QualType AllocType, |
| 350 | SourceLocation TypeLoc, |
| 351 | SourceRange TypeRange, |
| 352 | ExprArg ArraySizeE, |
| 353 | SourceLocation ConstructorLParen, |
| 354 | MultiExprArg ConstructorArgs, |
| 355 | SourceLocation ConstructorRParen) { |
| 356 | if (CheckAllocatedType(AllocType, TypeLoc, TypeRange)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 357 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 359 | QualType ResultType = Context.getPointerType(AllocType); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 360 | |
| 361 | // That every array dimension except the first is constant was already |
| 362 | // checked by the type check above. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 363 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 364 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 365 | // or enumeration type with a non-negative value." |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 366 | Expr *ArraySize = (Expr *)ArraySizeE.get(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 367 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 368 | QualType SizeType = ArraySize->getType(); |
| 369 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 370 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 371 | diag::err_array_size_not_integral) |
| 372 | << SizeType << ArraySize->getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 373 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 374 | // We don't care about special rules, so we tell the machinery it's not |
| 375 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 376 | if (!ArraySize->isValueDependent()) { |
| 377 | llvm::APSInt Value; |
| 378 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 379 | if (Value < llvm::APSInt( |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 380 | llvm::APInt::getNullValue(Value.getBitWidth()), |
| 381 | Value.isUnsigned())) |
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 | } |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 387 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 388 | ImpCastExprToType(ArraySize, Context.getSizeType(), |
| 389 | CastExpr::CK_IntegralCast); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 390 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 391 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 392 | FunctionDecl *OperatorNew = 0; |
| 393 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 394 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 395 | unsigned NumPlaceArgs = PlacementArgs.size(); |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 396 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 397 | if (!AllocType->isDependentType() && |
| 398 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 399 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 400 | SourceRange(PlacementLParen, PlacementRParen), |
| 401 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 402 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 403 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 404 | |
| 405 | bool Init = ConstructorLParen.isValid(); |
| 406 | // --- Choosing a constructor --- |
| 407 | // C++ 5.3.4p15 |
| 408 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) |
| 409 | // the object is not initialized. If the object, or any part of it, is |
| 410 | // const-qualified, it's an error. |
| 411 | // 2) If T is a POD and there's an empty initializer, the object is value- |
| 412 | // initialized. |
| 413 | // 3) If T is a POD and there's one initializer argument, the object is copy- |
| 414 | // constructed. |
| 415 | // 4) If T is a POD and there's more initializer arguments, it's an error. |
| 416 | // 5) If T is not a POD, the initializer arguments are used as constructor |
| 417 | // arguments. |
| 418 | // |
| 419 | // Or by the C++0x formulation: |
| 420 | // 1) If there's no initializer, the object is default-initialized according |
| 421 | // to C++0x rules. |
| 422 | // 2) Otherwise, the object is direct-initialized. |
| 423 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 424 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 425 | const RecordType *RT; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 426 | unsigned NumConsArgs = ConstructorArgs.size(); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 427 | ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this); |
| 428 | |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 429 | if (AllocType->isDependentType() || |
| 430 | Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 431 | // Skip all the checks. |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 432 | } else if ((RT = AllocType->getAs<RecordType>()) && |
| 433 | !AllocType->isAggregateType()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 434 | Constructor = PerformInitializationByConstructor( |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 435 | AllocType, move(ConstructorArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 436 | TypeLoc, |
| 437 | SourceRange(TypeLoc, ConstructorRParen), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 438 | RT->getDecl()->getDeclName(), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 439 | NumConsArgs != 0 ? IK_Direct : IK_Default, |
| 440 | ConvertedConstructorArgs); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 441 | if (!Constructor) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 442 | return ExprError(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 443 | |
| 444 | // Take the converted constructor arguments and use them for the new |
| 445 | // expression. |
| 446 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 447 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 448 | } else { |
| 449 | if (!Init) { |
| 450 | // FIXME: Check that no subpart is const. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 451 | if (AllocType.isConstQualified()) |
| 452 | return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 453 | << TypeRange); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 454 | } else if (NumConsArgs == 0) { |
Fariborz Jahanian | 6f26920 | 2009-11-03 20:38:53 +0000 | [diff] [blame] | 455 | // Object is value-initialized. Do nothing. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 456 | } else if (NumConsArgs == 1) { |
| 457 | // Object is direct-initialized. |
Sebastian Redl | 4f14963 | 2009-05-07 16:14:23 +0000 | [diff] [blame] | 458 | // FIXME: What DeclarationName do we pass in here? |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 459 | if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 460 | DeclarationName() /*AllocType.getAsString()*/, |
| 461 | /*DirectInit=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 462 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 463 | } else { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 464 | return ExprError(Diag(StartLoc, |
| 465 | diag::err_builtin_direct_init_more_than_one_arg) |
| 466 | << SourceRange(ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 471 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 472 | PlacementArgs.release(); |
| 473 | ConstructorArgs.release(); |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 474 | ArraySizeE.release(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 475 | return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 476 | NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 477 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | StartLoc, Init ? ConstructorRParen : SourceLocation())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 482 | /// in a new-expression. |
| 483 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 484 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 486 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 487 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 488 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 489 | return Diag(Loc, diag::err_bad_new_type) |
| 490 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 491 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 492 | return Diag(Loc, diag::err_bad_new_type) |
| 493 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 494 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 495 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 496 | PDiag(diag::err_new_incomplete_type) |
| 497 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 498 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 499 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 500 | diag::err_allocation_of_abstract_type)) |
| 501 | return true; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 502 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 506 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 507 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 508 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 509 | bool UseGlobal, QualType AllocType, |
| 510 | bool IsArray, Expr **PlaceArgs, |
| 511 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 512 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 514 | // --- Choosing an allocation function --- |
| 515 | // C++ 5.3.4p8 - 14 & 18 |
| 516 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 517 | // in the scope of the allocated class. |
| 518 | // 2) If an array size is given, look for operator new[], else look for |
| 519 | // operator new. |
| 520 | // 3) The first argument is always size_t. Append the arguments from the |
| 521 | // placement form. |
| 522 | // FIXME: Also find the appropriate delete operator. |
| 523 | |
| 524 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 525 | // We don't care about the actual value of this argument. |
| 526 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 527 | // tree? Or should the consumer just recalculate the value? |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 528 | IntegerLiteral Size(llvm::APInt::getNullValue( |
| 529 | Context.Target.getPointerWidth(0)), |
| 530 | Context.getSizeType(), |
| 531 | SourceLocation()); |
| 532 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 533 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 534 | |
| 535 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 536 | IsArray ? OO_Array_New : OO_New); |
| 537 | if (AllocType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | CXXRecordDecl *Record |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 539 | = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 540 | // FIXME: We fail to find inherited overloads. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 541 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 542 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 543 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 544 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 545 | } |
| 546 | if (!OperatorNew) { |
| 547 | // Didn't find a member overload. Look for a global one. |
| 548 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 549 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 550 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 551 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 552 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 553 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 556 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 557 | // copy them back. |
| 558 | if (NumPlaceArgs > 0) |
| 559 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 561 | return false; |
| 562 | } |
| 563 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 564 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 565 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 566 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 567 | DeclarationName Name, Expr** Args, |
| 568 | unsigned NumArgs, DeclContext *Ctx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | bool AllowMissing, FunctionDecl *&Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 570 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 571 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 572 | if (R.empty()) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 573 | if (AllowMissing) |
| 574 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 575 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 576 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 577 | } |
| 578 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 579 | // FIXME: handle ambiguity |
| 580 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 581 | OverloadCandidateSet Candidates; |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 582 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 583 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 584 | // Even member operator new/delete are implicitly treated as |
| 585 | // static, so don't use AddMemberCandidate. |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 586 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 587 | AddOverloadCandidate(Fn, Args, NumArgs, Candidates, |
| 588 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 589 | continue; |
| 590 | } |
| 591 | |
| 592 | // FIXME: Handle function templates |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // Do the resolution. |
| 596 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 597 | switch(BestViableFunction(Candidates, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 598 | case OR_Success: { |
| 599 | // Got one! |
| 600 | FunctionDecl *FnDecl = Best->Function; |
| 601 | // The first argument is size_t, and the first parameter must be size_t, |
| 602 | // too. This is checked on declaration and can be assumed. (It can't be |
| 603 | // asserted on, though, since invalid decls are left in there.) |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 604 | for (unsigned i = 0; i < NumArgs; ++i) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 605 | // FIXME: Passing word to diagnostic. |
Anders Carlsson | fc27d26 | 2009-05-31 19:49:47 +0000 | [diff] [blame] | 606 | if (PerformCopyInitialization(Args[i], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 607 | FnDecl->getParamDecl(i)->getType(), |
| 608 | "passing")) |
| 609 | return true; |
| 610 | } |
| 611 | Operator = FnDecl; |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | case OR_No_Viable_Function: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 616 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 617 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 618 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/false); |
| 619 | return true; |
| 620 | |
| 621 | case OR_Ambiguous: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 622 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 623 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 624 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 625 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 626 | |
| 627 | case OR_Deleted: |
| 628 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 629 | << Best->Function->isDeleted() |
| 630 | << Name << Range; |
| 631 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 632 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 633 | } |
| 634 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 639 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 640 | /// delete. These are: |
| 641 | /// @code |
| 642 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 643 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 644 | /// void operator delete(void *) throw(); |
| 645 | /// void operator delete[](void *) throw(); |
| 646 | /// @endcode |
| 647 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 648 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 650 | if (GlobalNewDeleteDeclared) |
| 651 | return; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 652 | |
| 653 | // C++ [basic.std.dynamic]p2: |
| 654 | // [...] The following allocation and deallocation functions (18.4) are |
| 655 | // implicitly declared in global scope in each translation unit of a |
| 656 | // program |
| 657 | // |
| 658 | // void* operator new(std::size_t) throw(std::bad_alloc); |
| 659 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 660 | // void operator delete(void*) throw(); |
| 661 | // void operator delete[](void*) throw(); |
| 662 | // |
| 663 | // These implicit declarations introduce only the function names operator |
| 664 | // new, operator new[], operator delete, operator delete[]. |
| 665 | // |
| 666 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 667 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 668 | // However, we do not make these implicit declarations visible to name |
| 669 | // lookup. |
| 670 | if (!StdNamespace) { |
| 671 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 672 | StdNamespace = NamespaceDecl::Create(Context, |
| 673 | Context.getTranslationUnitDecl(), |
| 674 | SourceLocation(), |
| 675 | &PP.getIdentifierTable().get("std")); |
| 676 | StdNamespace->setImplicit(true); |
| 677 | } |
| 678 | |
| 679 | if (!StdBadAlloc) { |
| 680 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 681 | // implicitly. |
| 682 | StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class, |
| 683 | StdNamespace, |
| 684 | SourceLocation(), |
| 685 | &PP.getIdentifierTable().get("bad_alloc"), |
| 686 | SourceLocation(), 0); |
| 687 | StdBadAlloc->setImplicit(true); |
| 688 | } |
| 689 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 690 | GlobalNewDeleteDeclared = true; |
| 691 | |
| 692 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 693 | QualType SizeT = Context.getSizeType(); |
| 694 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 695 | DeclareGlobalAllocationFunction( |
| 696 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
| 697 | VoidPtr, SizeT); |
| 698 | DeclareGlobalAllocationFunction( |
| 699 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
| 700 | VoidPtr, SizeT); |
| 701 | DeclareGlobalAllocationFunction( |
| 702 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 703 | Context.VoidTy, VoidPtr); |
| 704 | DeclareGlobalAllocationFunction( |
| 705 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 706 | Context.VoidTy, VoidPtr); |
| 707 | } |
| 708 | |
| 709 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 710 | /// allocation function if it doesn't already exist. |
| 711 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | QualType Return, QualType Argument) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 713 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 714 | |
| 715 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 716 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 717 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 718 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 719 | Alloc != AllocEnd; ++Alloc) { |
| 720 | // FIXME: Do we need to check for default arguments here? |
| 721 | FunctionDecl *Func = cast<FunctionDecl>(*Alloc); |
| 722 | if (Func->getNumParams() == 1 && |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 723 | Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 724 | return; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 728 | QualType BadAllocType; |
| 729 | bool HasBadAllocExceptionSpec |
| 730 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 731 | Name.getCXXOverloadedOperator() == OO_Array_New); |
| 732 | if (HasBadAllocExceptionSpec) { |
| 733 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
| 734 | BadAllocType = Context.getTypeDeclType(StdBadAlloc); |
| 735 | } |
| 736 | |
| 737 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0, |
| 738 | true, false, |
| 739 | HasBadAllocExceptionSpec? 1 : 0, |
| 740 | &BadAllocType); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 741 | FunctionDecl *Alloc = |
| 742 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 743 | FnType, /*DInfo=*/0, FunctionDecl::None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 744 | Alloc->setImplicit(); |
| 745 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 746 | 0, Argument, /*DInfo=*/0, |
| 747 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 748 | Alloc->setParams(Context, &Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 750 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 751 | // make sure it is at the end of the chain to coincide with the |
| 752 | // global scope. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 753 | ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 756 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 757 | DeclarationName Name, |
| 758 | FunctionDecl* &Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 759 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 760 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 761 | LookupQualifiedName(Found, RD); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 762 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 763 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 764 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 765 | |
| 766 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 767 | F != FEnd; ++F) { |
| 768 | if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F)) |
| 769 | if (Delete->isUsualDeallocationFunction()) { |
| 770 | Operator = Delete; |
| 771 | return false; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | // We did find operator delete/operator delete[] declarations, but |
| 776 | // none of them were suitable. |
| 777 | if (!Found.empty()) { |
| 778 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 779 | << Name << RD; |
| 780 | |
| 781 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 782 | F != FEnd; ++F) { |
| 783 | Diag((*F)->getLocation(), |
| 784 | diag::note_delete_member_function_declared_here) |
| 785 | << Name; |
| 786 | } |
| 787 | |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | // Look for a global declaration. |
| 792 | DeclareGlobalNewDelete(); |
| 793 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
| 794 | |
| 795 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 796 | Expr* DeallocArgs[1]; |
| 797 | DeallocArgs[0] = &Null; |
| 798 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
| 799 | DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, |
| 800 | Operator)) |
| 801 | return true; |
| 802 | |
| 803 | assert(Operator && "Did not find a deallocation function!"); |
| 804 | return false; |
| 805 | } |
| 806 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 807 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 808 | /// @code ::delete ptr; @endcode |
| 809 | /// or |
| 810 | /// @code delete [] ptr; @endcode |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 811 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 812 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | bool ArrayForm, ExprArg Operand) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 814 | // C++ [expr.delete]p1: |
| 815 | // The operand shall have a pointer type, or a class type having a single |
| 816 | // conversion function to a pointer type. The result has type void. |
| 817 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 818 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 819 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 820 | FunctionDecl *OperatorDelete = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 822 | Expr *Ex = (Expr *)Operand.get(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 823 | if (!Ex->isTypeDependent()) { |
| 824 | QualType Type = Ex->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 826 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 827 | llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 828 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 829 | OverloadedFunctionDecl *Conversions = |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 830 | RD->getVisibleConversionFunctions(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 832 | for (OverloadedFunctionDecl::function_iterator |
| 833 | Func = Conversions->function_begin(), |
| 834 | FuncEnd = Conversions->function_end(); |
| 835 | Func != FuncEnd; ++Func) { |
| 836 | // Skip over templated conversion functions; they aren't considered. |
| 837 | if (isa<FunctionTemplateDecl>(*Func)) |
| 838 | continue; |
| 839 | |
| 840 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 841 | |
| 842 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 843 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 844 | if (ConvPtrType->getPointeeType()->isObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 845 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 846 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 847 | if (ObjectPtrConversions.size() == 1) { |
| 848 | // We have a single conversion to a pointer-to-object type. Perform |
| 849 | // that conversion. |
| 850 | Operand.release(); |
| 851 | if (!PerformImplicitConversion(Ex, |
| 852 | ObjectPtrConversions.front()->getConversionType(), |
| 853 | "converting")) { |
| 854 | Operand = Owned(Ex); |
| 855 | Type = Ex->getType(); |
| 856 | } |
| 857 | } |
| 858 | else if (ObjectPtrConversions.size() > 1) { |
| 859 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
| 860 | << Type << Ex->getSourceRange(); |
| 861 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) { |
| 862 | CXXConversionDecl *Conv = ObjectPtrConversions[i]; |
| 863 | Diag(Conv->getLocation(), diag::err_ovl_candidate); |
| 864 | } |
| 865 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 866 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 869 | if (!Type->isPointerType()) |
| 870 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 871 | << Type << Ex->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 872 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 873 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 874 | if (Pointee->isFunctionType() || Pointee->isVoidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 875 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 876 | << Type << Ex->getSourceRange()); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 877 | else if (!Pointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | RequireCompleteType(StartLoc, Pointee, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 879 | PDiag(diag::warn_delete_incomplete) |
| 880 | << Ex->getSourceRange())) |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 881 | return ExprError(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 882 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 883 | // C++ [expr.delete]p2: |
| 884 | // [Note: a pointer to a const type can be the operand of a |
| 885 | // delete-expression; it is not necessary to cast away the constness |
| 886 | // (5.2.11) of the pointer expression before it is used as the operand |
| 887 | // of the delete-expression. ] |
| 888 | ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), |
| 889 | CastExpr::CK_NoOp); |
| 890 | |
| 891 | // Update the operand. |
| 892 | Operand.take(); |
| 893 | Operand = ExprArg(*this, Ex); |
| 894 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 895 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 896 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 897 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 898 | if (const RecordType *RT = Pointee->getAs<RecordType>()) { |
| 899 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 900 | |
| 901 | if (!UseGlobal && |
| 902 | FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 903 | return ExprError(); |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 904 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 905 | if (!RD->hasTrivialDestructor()) |
| 906 | if (const CXXDestructorDecl *Dtor = RD->getDestructor(Context)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 908 | const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 909 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 910 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 911 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 912 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 913 | DeclareGlobalNewDelete(); |
| 914 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 916 | &Ex, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 917 | OperatorDelete)) |
| 918 | return ExprError(); |
| 919 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 921 | // FIXME: Check access and ambiguity of operator delete and destructor. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 924 | Operand.release(); |
| 925 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 926 | OperatorDelete, Ex, StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 930 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 931 | /// C++ if/switch/while/for statement. |
| 932 | /// e.g: "if (int x = f()) {...}" |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 933 | Action::OwningExprResult |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 934 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 935 | Declarator &D, |
| 936 | SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 937 | ExprArg AssignExprVal) { |
| 938 | assert(AssignExprVal.get() && "Null assignment expression"); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 939 | |
| 940 | // C++ 6.4p2: |
| 941 | // The declarator shall not specify a function or an array. |
| 942 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 943 | // new class or enumeration. |
| 944 | |
| 945 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 946 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 947 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 948 | // FIXME: Store DeclaratorInfo in the expression. |
| 949 | DeclaratorInfo *DInfo = 0; |
Argyrios Kyrtzidis | e955e72 | 2009-08-11 05:20:41 +0000 | [diff] [blame] | 950 | TagDecl *OwnedTag = 0; |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 951 | QualType Ty = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 953 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 954 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 955 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 956 | return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type) |
| 957 | << SourceRange(StartLoc, EqualLoc)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 958 | } else if (Ty->isArrayType()) { // ...or an array. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 959 | Diag(StartLoc, diag::err_invalid_use_of_array_type) |
| 960 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | e955e72 | 2009-08-11 05:20:41 +0000 | [diff] [blame] | 961 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 962 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 963 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Douglas Gregor | 2e01cda | 2009-06-23 21:43:56 +0000 | [diff] [blame] | 966 | DeclPtrTy Dcl = ActOnDeclarator(S, D); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 967 | if (!Dcl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 968 | return ExprError(); |
Anders Carlsson | f5dcd38 | 2009-05-30 21:37:25 +0000 | [diff] [blame] | 969 | AddInitializerToDecl(Dcl, move(AssignExprVal), /*DirectInit=*/false); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 970 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 971 | // Mark this variable as one that is declared within a conditional. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 972 | // We know that the decl had to be a VarDecl because that is the only type of |
| 973 | // decl that can be assigned and the grammar requires an '='. |
| 974 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 975 | VD->setDeclaredInCondition(true); |
| 976 | return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 980 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 981 | // C++ 6.4p4: |
| 982 | // The value of a condition that is an initialized declaration in a statement |
| 983 | // other than a switch statement is the value of the declared variable |
| 984 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 985 | // program is ill-formed. |
| 986 | // The value of a condition that is an expression is the value of the |
| 987 | // expression, implicitly converted to bool. |
| 988 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 989 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 990 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 991 | |
| 992 | /// Helper function to determine whether this is the (deprecated) C++ |
| 993 | /// conversion from a string literal to a pointer to non-const char or |
| 994 | /// non-const wchar_t (for narrow and wide string literals, |
| 995 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 997 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 998 | // Look inside the implicit cast, if it exists. |
| 999 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 1000 | From = Cast->getSubExpr(); |
| 1001 | |
| 1002 | // A string literal (2.13.4) that is not a wide string literal can |
| 1003 | // be converted to an rvalue of type "pointer to char"; a wide |
| 1004 | // string literal can be converted to an rvalue of type "pointer |
| 1005 | // to wchar_t" (C++ 4.2p2). |
| 1006 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1007 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1009 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1010 | // This conversion is considered only when there is an |
| 1011 | // explicit appropriate pointer target type (C++ 4.2p2). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1012 | if (!ToPtrType->getPointeeType().hasQualifiers() && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1013 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 1014 | (!StrLit->isWide() && |
| 1015 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 1016 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | return false; |
| 1021 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1022 | |
| 1023 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1024 | /// expression From to the type ToType. Returns true if there was an |
| 1025 | /// error, false otherwise. The expression From is replaced with the |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1026 | /// converted expression. Flavor is the kind of conversion we're |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1027 | /// performing, used in the error message. If @p AllowExplicit, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1028 | /// explicit user-defined conversions are permitted. @p Elidable should be true |
| 1029 | /// when called for copies which may be elided (C++ 12.8p15). C++0x overload |
| 1030 | /// resolution works differently in that case. |
| 1031 | bool |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1032 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1033 | const char *Flavor, bool AllowExplicit, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | bool Elidable) { |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1035 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 1036 | return PerformImplicitConversion(From, ToType, Flavor, AllowExplicit, |
| 1037 | Elidable, ICS); |
| 1038 | } |
| 1039 | |
| 1040 | bool |
| 1041 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 1042 | const char *Flavor, bool AllowExplicit, |
| 1043 | bool Elidable, |
| 1044 | ImplicitConversionSequence& ICS) { |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1045 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1046 | if (Elidable && getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | ICS = TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1048 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | AllowExplicit, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1050 | /*ForceRValue=*/true, |
| 1051 | /*InOverloadResolution=*/false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1052 | } |
| 1053 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | ICS = TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1055 | /*SuppressUserConversions=*/false, |
| 1056 | AllowExplicit, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1057 | /*ForceRValue=*/false, |
| 1058 | /*InOverloadResolution=*/false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1059 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1060 | return PerformImplicitConversion(From, ToType, ICS, Flavor); |
| 1061 | } |
| 1062 | |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1063 | /// BuildCXXDerivedToBaseExpr - This routine generates the suitable AST |
| 1064 | /// for the derived to base conversion of the expression 'From'. All |
| 1065 | /// necessary information is passed in ICS. |
| 1066 | bool |
| 1067 | Sema::BuildCXXDerivedToBaseExpr(Expr *&From, CastExpr::CastKind CastKind, |
| 1068 | const ImplicitConversionSequence& ICS, |
| 1069 | const char *Flavor) { |
| 1070 | QualType BaseType = |
| 1071 | QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr); |
| 1072 | // Must do additional defined to base conversion. |
| 1073 | QualType DerivedType = |
| 1074 | QualType::getFromOpaquePtr(ICS.UserDefined.After.FromTypePtr); |
| 1075 | |
| 1076 | From = new (Context) ImplicitCastExpr( |
| 1077 | DerivedType.getNonReferenceType(), |
| 1078 | CastKind, |
| 1079 | From, |
| 1080 | DerivedType->isLValueReferenceType()); |
| 1081 | From = new (Context) ImplicitCastExpr(BaseType.getNonReferenceType(), |
| 1082 | CastExpr::CK_DerivedToBase, From, |
| 1083 | BaseType->isLValueReferenceType()); |
| 1084 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1085 | OwningExprResult FromResult = |
| 1086 | BuildCXXConstructExpr( |
| 1087 | ICS.UserDefined.After.CopyConstructor->getLocation(), |
| 1088 | BaseType, |
| 1089 | ICS.UserDefined.After.CopyConstructor, |
| 1090 | MultiExprArg(*this, (void **)&From, 1)); |
| 1091 | if (FromResult.isInvalid()) |
| 1092 | return true; |
| 1093 | From = FromResult.takeAs<Expr>(); |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1097 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1098 | /// expression From to the type ToType using the pre-computed implicit |
| 1099 | /// conversion sequence ICS. Returns true if there was an error, false |
| 1100 | /// otherwise. The expression From is replaced with the converted |
| 1101 | /// expression. Flavor is the kind of conversion we're performing, |
| 1102 | /// used in the error message. |
| 1103 | bool |
| 1104 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 1105 | const ImplicitConversionSequence &ICS, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1106 | const char* Flavor, bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1107 | switch (ICS.ConversionKind) { |
| 1108 | case ImplicitConversionSequence::StandardConversion: |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1109 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor, |
| 1110 | IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1111 | return true; |
| 1112 | break; |
| 1113 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1114 | case ImplicitConversionSequence::UserDefinedConversion: { |
| 1115 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1116 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
| 1117 | CastExpr::CastKind CastKind = CastExpr::CK_Unknown; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1118 | QualType BeforeToType; |
| 1119 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1120 | CastKind = CastExpr::CK_UserDefinedConversion; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1121 | |
| 1122 | // If the user-defined conversion is specified by a conversion function, |
| 1123 | // the initial standard conversion sequence converts the source type to |
| 1124 | // the implicit object parameter of the conversion function. |
| 1125 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
| 1126 | } else if (const CXXConstructorDecl *Ctor = |
| 1127 | dyn_cast<CXXConstructorDecl>(FD)) { |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1128 | CastKind = CastExpr::CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1129 | // Do no conversion if dealing with ... for the first conversion. |
| 1130 | if (!ICS.UserDefined.EllipsisConversion) |
| 1131 | // If the user-defined conversion is specified by a constructor, the |
| 1132 | // initial standard conversion sequence converts the source type to the |
| 1133 | // type required by the argument of the constructor |
| 1134 | BeforeToType = Ctor->getParamDecl(0)->getType(); |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1135 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1136 | else |
| 1137 | assert(0 && "Unknown conversion function kind!"); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1138 | // Whatch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 1139 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1140 | if (PerformImplicitConversion(From, BeforeToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1141 | ICS.UserDefined.Before, "converting", |
| 1142 | IgnoreBaseAccess)) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1143 | return true; |
| 1144 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1145 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1146 | OwningExprResult CastArg |
| 1147 | = BuildCXXCastArgument(From->getLocStart(), |
| 1148 | ToType.getNonReferenceType(), |
| 1149 | CastKind, cast<CXXMethodDecl>(FD), |
| 1150 | Owned(From)); |
| 1151 | |
| 1152 | if (CastArg.isInvalid()) |
| 1153 | return true; |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1154 | |
| 1155 | if (ICS.UserDefined.After.Second == ICK_Derived_To_Base && |
| 1156 | ICS.UserDefined.After.CopyConstructor) { |
| 1157 | From = CastArg.takeAs<Expr>(); |
| 1158 | return BuildCXXDerivedToBaseExpr(From, CastKind, ICS, Flavor); |
| 1159 | } |
Fariborz Jahanian | 7a1f4cc | 2009-10-23 18:08:22 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (ICS.UserDefined.After.Second == ICK_Pointer_Member && |
| 1162 | ToType.getNonReferenceType()->isMemberFunctionPointerType()) |
| 1163 | CastKind = CastExpr::CK_BaseToDerivedMemberPointer; |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1164 | |
Anders Carlsson | 626c2d6 | 2009-09-15 05:49:31 +0000 | [diff] [blame] | 1165 | From = new (Context) ImplicitCastExpr(ToType.getNonReferenceType(), |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1166 | CastKind, CastArg.takeAs<Expr>(), |
Anders Carlsson | 626c2d6 | 2009-09-15 05:49:31 +0000 | [diff] [blame] | 1167 | ToType->isLValueReferenceType()); |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1168 | return false; |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1171 | case ImplicitConversionSequence::EllipsisConversion: |
| 1172 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1173 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1174 | |
| 1175 | case ImplicitConversionSequence::BadConversion: |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
| 1179 | // Everything went well. |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1184 | /// expression From to the type ToType by following the standard |
| 1185 | /// conversion sequence SCS. Returns true if there was an error, false |
| 1186 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1187 | /// expression. Flavor is the context in which we're performing this |
| 1188 | /// conversion, for use in error messages. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | bool |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1190 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1191 | const StandardConversionSequence& SCS, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1192 | const char *Flavor, bool IgnoreBaseAccess) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1193 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 1194 | // much extra work. What this means is that we need to keep track of more |
| 1195 | // information that is computed when we try the implicit conversion initially, |
| 1196 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1197 | QualType FromType = From->getType(); |
| 1198 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1199 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 1200 | // FIXME: When can ToType be a reference type? |
| 1201 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1202 | if (SCS.Second == ICK_Derived_To_Base) { |
| 1203 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1204 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
| 1205 | MultiExprArg(*this, (void **)&From, 1), |
| 1206 | /*FIXME:ConstructLoc*/SourceLocation(), |
| 1207 | ConstructorArgs)) |
| 1208 | return true; |
| 1209 | OwningExprResult FromResult = |
| 1210 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1211 | ToType, SCS.CopyConstructor, |
| 1212 | move_arg(ConstructorArgs)); |
| 1213 | if (FromResult.isInvalid()) |
| 1214 | return true; |
| 1215 | From = FromResult.takeAs<Expr>(); |
| 1216 | return false; |
| 1217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | OwningExprResult FromResult = |
| 1219 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1220 | ToType, SCS.CopyConstructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 1221 | MultiExprArg(*this, (void**)&From, 1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1223 | if (FromResult.isInvalid()) |
| 1224 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1226 | From = FromResult.takeAs<Expr>(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1227 | return false; |
| 1228 | } |
| 1229 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1230 | // Perform the first implicit conversion. |
| 1231 | switch (SCS.First) { |
| 1232 | case ICK_Identity: |
| 1233 | case ICK_Lvalue_To_Rvalue: |
| 1234 | // Nothing to do. |
| 1235 | break; |
| 1236 | |
| 1237 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1238 | FromType = Context.getArrayDecayedType(FromType); |
Anders Carlsson | 8249576 | 2009-08-08 21:04:35 +0000 | [diff] [blame] | 1239 | ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1240 | break; |
| 1241 | |
| 1242 | case ICK_Function_To_Pointer: |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1243 | if (Context.getCanonicalType(FromType) == Context.OverloadTy) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1244 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 1245 | if (!Fn) |
| 1246 | return true; |
| 1247 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1248 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
| 1249 | return true; |
| 1250 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1251 | From = FixOverloadedFunctionReference(From, Fn); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1252 | FromType = From->getType(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1253 | |
Sebastian Redl | 759986e | 2009-10-17 20:50:27 +0000 | [diff] [blame] | 1254 | // If there's already an address-of operator in the expression, we have |
| 1255 | // the right type already, and the code below would just introduce an |
| 1256 | // invalid additional pointer level. |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1257 | if (FromType->isPointerType() || FromType->isMemberFunctionPointerType()) |
Sebastian Redl | 759986e | 2009-10-17 20:50:27 +0000 | [diff] [blame] | 1258 | break; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1259 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1260 | FromType = Context.getPointerType(FromType); |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 1261 | ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | |
| 1264 | default: |
| 1265 | assert(false && "Improper first standard conversion"); |
| 1266 | break; |
| 1267 | } |
| 1268 | |
| 1269 | // Perform the second implicit conversion |
| 1270 | switch (SCS.Second) { |
| 1271 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1272 | // If both sides are functions (or pointers/references to them), there could |
| 1273 | // be incompatible exception declarations. |
| 1274 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1275 | return true; |
| 1276 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1277 | break; |
| 1278 | |
| 1279 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1280 | case ICK_Integral_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1281 | ImpCastExprToType(From, ToType, CastExpr::CK_IntegralCast); |
| 1282 | break; |
| 1283 | |
| 1284 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1285 | case ICK_Floating_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1286 | ImpCastExprToType(From, ToType, CastExpr::CK_FloatingCast); |
| 1287 | break; |
| 1288 | |
| 1289 | case ICK_Complex_Promotion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1290 | case ICK_Complex_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1291 | ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); |
| 1292 | break; |
| 1293 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1294 | case ICK_Floating_Integral: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1295 | if (ToType->isFloatingType()) |
| 1296 | ImpCastExprToType(From, ToType, CastExpr::CK_IntegralToFloating); |
| 1297 | else |
| 1298 | ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral); |
| 1299 | break; |
| 1300 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1301 | case ICK_Complex_Real: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1302 | ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); |
| 1303 | break; |
| 1304 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1305 | case ICK_Compatible_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1306 | ImpCastExprToType(From, ToType, CastExpr::CK_NoOp); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1307 | break; |
| 1308 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1309 | case ICK_Pointer_Conversion: { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1310 | if (SCS.IncompatibleObjC) { |
| 1311 | // Diagnose incompatible Objective-C conversions |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | Diag(From->getSourceRange().getBegin(), |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1313 | diag::ext_typecheck_convert_incompatible_pointer) |
| 1314 | << From->getType() << ToType << Flavor |
| 1315 | << From->getSourceRange(); |
| 1316 | } |
| 1317 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1318 | |
| 1319 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1320 | if (CheckPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1321 | return true; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1322 | ImpCastExprToType(From, ToType, Kind); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1323 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | case ICK_Pointer_Member: { |
| 1327 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1328 | if (CheckMemberPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1329 | return true; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1330 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1331 | return true; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1332 | ImpCastExprToType(From, ToType, Kind); |
| 1333 | break; |
| 1334 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1335 | case ICK_Boolean_Conversion: |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1336 | ImpCastExprToType(From, Context.BoolTy, CastExpr::CK_Unknown); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1339 | case ICK_Derived_To_Base: |
| 1340 | if (CheckDerivedToBaseConversion(From->getType(), |
| 1341 | ToType.getNonReferenceType(), |
| 1342 | From->getLocStart(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1343 | From->getSourceRange(), |
| 1344 | IgnoreBaseAccess)) |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1345 | return true; |
| 1346 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 1347 | CastExpr::CK_DerivedToBase); |
| 1348 | break; |
| 1349 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1350 | default: |
| 1351 | assert(false && "Improper second standard conversion"); |
| 1352 | break; |
| 1353 | } |
| 1354 | |
| 1355 | switch (SCS.Third) { |
| 1356 | case ICK_Identity: |
| 1357 | // Nothing to do. |
| 1358 | break; |
| 1359 | |
| 1360 | case ICK_Qualification: |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1361 | // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue |
| 1362 | // references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1364 | CastExpr::CK_NoOp, |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1365 | ToType->isLValueReferenceType()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1366 | break; |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1367 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1368 | default: |
| 1369 | assert(false && "Improper second standard conversion"); |
| 1370 | break; |
| 1371 | } |
| 1372 | |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1376 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, |
| 1377 | SourceLocation KWLoc, |
| 1378 | SourceLocation LParen, |
| 1379 | TypeTy *Ty, |
| 1380 | SourceLocation RParen) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1381 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1383 | // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
| 1384 | // all traits except __is_class, __is_enum and __is_union require a the type |
| 1385 | // to be complete. |
| 1386 | if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | if (RequireCompleteType(KWLoc, T, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 1388 | diag::err_incomplete_type_used_in_type_trait_expr)) |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1389 | return ExprError(); |
| 1390 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1391 | |
| 1392 | // There is no point in eagerly computing the value. The traits are designed |
| 1393 | // to be used from type trait templates, so Ty will be a template parameter |
| 1394 | // 99% of the time. |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1395 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T, |
| 1396 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1397 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1398 | |
| 1399 | QualType Sema::CheckPointerToMemberOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1401 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 1402 | // C++ 5.5p2 |
| 1403 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 1404 | // be of type "pointer to member of T" (where T is a completely-defined |
| 1405 | // class type) [...] |
| 1406 | QualType RType = rex->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1407 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1408 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1409 | Diag(Loc, diag::err_bad_memptr_rhs) |
| 1410 | << OpSpelling << RType << rex->getSourceRange(); |
| 1411 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1413 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1414 | QualType Class(MemPtr->getClass(), 0); |
| 1415 | |
| 1416 | // C++ 5.5p2 |
| 1417 | // [...] to its first operand, which shall be of class T or of a class of |
| 1418 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 1419 | // such a class] |
| 1420 | QualType LType = lex->getType(); |
| 1421 | if (isIndirect) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1422 | if (const PointerType *Ptr = LType->getAs<PointerType>()) |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1423 | LType = Ptr->getPointeeType().getNonReferenceType(); |
| 1424 | else { |
| 1425 | Diag(Loc, diag::err_bad_memptr_lhs) |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 1426 | << OpSpelling << 1 << LType |
| 1427 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1428 | return QualType(); |
| 1429 | } |
| 1430 | } |
| 1431 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1432 | if (!Context.hasSameUnqualifiedType(Class, LType)) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1433 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1434 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1435 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 1436 | // overkill? |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1437 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 1438 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 1439 | const char *ReplaceStr = isIndirect ? ".*" : "->*"; |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1440 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 1441 | << (int)isIndirect << lex->getType() << |
| 1442 | CodeModificationHint::CreateReplacement(SourceRange(Loc), ReplaceStr); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1443 | return QualType(); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | // C++ 5.5p2 |
| 1448 | // The result is an object or a function of the type specified by the |
| 1449 | // second operand. |
| 1450 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 1451 | // in accordance with 5.5p5 and 5.2.5. |
| 1452 | // FIXME: This returns a dereferenced member function pointer as a normal |
| 1453 | // function type. However, the only operation valid on such functions is |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1454 | // calling them. There's also a GCC extension to get a function pointer to the |
| 1455 | // thing, which is another complication, because this type - unlike the type |
| 1456 | // 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] | 1457 | // argument. |
| 1458 | // We probably need a "MemberFunctionClosureType" or something like that. |
| 1459 | QualType Result = MemPtr->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1460 | Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1461 | return Result; |
| 1462 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1463 | |
| 1464 | /// \brief Get the target type of a standard or user-defined conversion. |
| 1465 | static QualType TargetType(const ImplicitConversionSequence &ICS) { |
| 1466 | assert((ICS.ConversionKind == |
| 1467 | ImplicitConversionSequence::StandardConversion || |
| 1468 | ICS.ConversionKind == |
| 1469 | ImplicitConversionSequence::UserDefinedConversion) && |
| 1470 | "function only valid for standard or user-defined conversions"); |
| 1471 | if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1472 | return QualType::getFromOpaquePtr(ICS.Standard.ToTypePtr); |
| 1473 | return QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr); |
| 1474 | } |
| 1475 | |
| 1476 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 1477 | /// |
| 1478 | /// This is part of the parameter validation for the ? operator. If either |
| 1479 | /// value operand is a class type, the two operands are attempted to be |
| 1480 | /// converted to each other. This function does the conversion in one direction. |
| 1481 | /// It emits a diagnostic and returns true only if it finds an ambiguous |
| 1482 | /// conversion. |
| 1483 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 1484 | SourceLocation QuestionLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | ImplicitConversionSequence &ICS) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1486 | // C++0x 5.16p3 |
| 1487 | // The process for determining whether an operand expression E1 of type T1 |
| 1488 | // can be converted to match an operand expression E2 of type T2 is defined |
| 1489 | // as follows: |
| 1490 | // -- If E2 is an lvalue: |
| 1491 | if (To->isLvalue(Self.Context) == Expr::LV_Valid) { |
| 1492 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 1493 | // type "lvalue reference to T2", subject to the constraint that in the |
| 1494 | // conversion the reference must bind directly to E1. |
| 1495 | if (!Self.CheckReferenceInit(From, |
| 1496 | Self.Context.getLValueReferenceType(To->getType()), |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1497 | To->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1498 | /*SuppressUserConversions=*/false, |
| 1499 | /*AllowExplicit=*/false, |
| 1500 | /*ForceRValue=*/false, |
| 1501 | &ICS)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1502 | { |
| 1503 | assert((ICS.ConversionKind == |
| 1504 | ImplicitConversionSequence::StandardConversion || |
| 1505 | ICS.ConversionKind == |
| 1506 | ImplicitConversionSequence::UserDefinedConversion) && |
| 1507 | "expected a definite conversion"); |
| 1508 | bool DirectBinding = |
| 1509 | ICS.ConversionKind == ImplicitConversionSequence::StandardConversion ? |
| 1510 | ICS.Standard.DirectBinding : ICS.UserDefined.After.DirectBinding; |
| 1511 | if (DirectBinding) |
| 1512 | return false; |
| 1513 | } |
| 1514 | } |
| 1515 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1516 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 1517 | // -- if E1 and E2 have class type, and the underlying class types are |
| 1518 | // the same or one is a base class of the other: |
| 1519 | QualType FTy = From->getType(); |
| 1520 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1521 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 1522 | const RecordType *TRec = TTy->getAs<RecordType>(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1523 | bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); |
| 1524 | if (FRec && TRec && (FRec == TRec || |
| 1525 | FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
| 1526 | // E1 can be converted to match E2 if the class of T2 is the |
| 1527 | // same type as, or a base class of, the class of T1, and |
| 1528 | // [cv2 > cv1]. |
| 1529 | if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) { |
| 1530 | // Could still fail if there's no copy constructor. |
| 1531 | // FIXME: Is this a hard error then, or just a conversion failure? The |
| 1532 | // standard doesn't say. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | ICS = Self.TryCopyInitialization(From, TTy, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 1534 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1535 | /*ForceRValue=*/false, |
| 1536 | /*InOverloadResolution=*/false); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1537 | } |
| 1538 | } else { |
| 1539 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 1540 | // implicitly converted to the type that expression E2 would have |
| 1541 | // if E2 were converted to an rvalue. |
| 1542 | // First find the decayed type. |
| 1543 | if (TTy->isFunctionType()) |
| 1544 | TTy = Self.Context.getPointerType(TTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | else if (TTy->isArrayType()) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1546 | TTy = Self.Context.getArrayDecayedType(TTy); |
| 1547 | |
| 1548 | // Now try the implicit conversion. |
| 1549 | // FIXME: This doesn't detect ambiguities. |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1550 | ICS = Self.TryImplicitConversion(From, TTy, |
| 1551 | /*SuppressUserConversions=*/false, |
| 1552 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1553 | /*ForceRValue=*/false, |
| 1554 | /*InOverloadResolution=*/false); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1555 | } |
| 1556 | return false; |
| 1557 | } |
| 1558 | |
| 1559 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 1560 | /// |
| 1561 | /// This is part of the parameter validation for the ? operator. If either |
| 1562 | /// value operand is a class type, overload resolution is used to find a |
| 1563 | /// conversion to a common type. |
| 1564 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, |
| 1565 | SourceLocation Loc) { |
| 1566 | Expr *Args[2] = { LHS, RHS }; |
| 1567 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 1568 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1569 | |
| 1570 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1571 | switch (Self.BestViableFunction(CandidateSet, Loc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1572 | case Sema::OR_Success: |
| 1573 | // We found a match. Perform the conversions on the arguments and move on. |
| 1574 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
| 1575 | Best->Conversions[0], "converting") || |
| 1576 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
| 1577 | Best->Conversions[1], "converting")) |
| 1578 | break; |
| 1579 | return false; |
| 1580 | |
| 1581 | case Sema::OR_No_Viable_Function: |
| 1582 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 1583 | << LHS->getType() << RHS->getType() |
| 1584 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1585 | return true; |
| 1586 | |
| 1587 | case Sema::OR_Ambiguous: |
| 1588 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) |
| 1589 | << LHS->getType() << RHS->getType() |
| 1590 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1591 | // FIXME: Print the possible common types by printing the return types of |
| 1592 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1593 | break; |
| 1594 | |
| 1595 | case Sema::OR_Deleted: |
| 1596 | assert(false && "Conditional operator has only built-in overloads"); |
| 1597 | break; |
| 1598 | } |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1602 | /// \brief Perform an "extended" implicit conversion as returned by |
| 1603 | /// TryClassUnification. |
| 1604 | /// |
| 1605 | /// TryClassUnification generates ICSs that include reference bindings. |
| 1606 | /// PerformImplicitConversion is not suitable for this; it chokes if the |
| 1607 | /// second part of a standard conversion is ICK_DerivedToBase. This function |
| 1608 | /// handles the reference binding specially. |
| 1609 | static bool ConvertForConditional(Sema &Self, Expr *&E, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | const ImplicitConversionSequence &ICS) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1611 | if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion && |
| 1612 | ICS.Standard.ReferenceBinding) { |
| 1613 | assert(ICS.Standard.DirectBinding && |
| 1614 | "TryClassUnification should never generate indirect ref bindings"); |
Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1615 | // FIXME: CheckReferenceInit should be able to reuse the ICS instead of |
| 1616 | // redoing all the work. |
| 1617 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1618 | TargetType(ICS)), |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1619 | /*FIXME:*/E->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1620 | /*SuppressUserConversions=*/false, |
| 1621 | /*AllowExplicit=*/false, |
| 1622 | /*ForceRValue=*/false); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1623 | } |
| 1624 | if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion && |
| 1625 | ICS.UserDefined.After.ReferenceBinding) { |
| 1626 | assert(ICS.UserDefined.After.DirectBinding && |
| 1627 | "TryClassUnification should never generate indirect ref bindings"); |
Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 1628 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1629 | TargetType(ICS)), |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1630 | /*FIXME:*/E->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1631 | /*SuppressUserConversions=*/false, |
| 1632 | /*AllowExplicit=*/false, |
| 1633 | /*ForceRValue=*/false); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1634 | } |
| 1635 | if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, "converting")) |
| 1636 | return true; |
| 1637 | return false; |
| 1638 | } |
| 1639 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1640 | /// \brief Check the operands of ?: under C++ semantics. |
| 1641 | /// |
| 1642 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 1643 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
| 1644 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 1645 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1646 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 1647 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1648 | |
| 1649 | // C++0x 5.16p1 |
| 1650 | // The first expression is contextually converted to bool. |
| 1651 | if (!Cond->isTypeDependent()) { |
| 1652 | if (CheckCXXBooleanCondition(Cond)) |
| 1653 | return QualType(); |
| 1654 | } |
| 1655 | |
| 1656 | // Either of the arguments dependent? |
| 1657 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) |
| 1658 | return Context.DependentTy; |
| 1659 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 1660 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 1661 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1662 | // C++0x 5.16p2 |
| 1663 | // If either the second or the third operand has type (cv) void, ... |
| 1664 | QualType LTy = LHS->getType(); |
| 1665 | QualType RTy = RHS->getType(); |
| 1666 | bool LVoid = LTy->isVoidType(); |
| 1667 | bool RVoid = RTy->isVoidType(); |
| 1668 | if (LVoid || RVoid) { |
| 1669 | // ... then the [l2r] conversions are performed on the second and third |
| 1670 | // operands ... |
| 1671 | DefaultFunctionArrayConversion(LHS); |
| 1672 | DefaultFunctionArrayConversion(RHS); |
| 1673 | LTy = LHS->getType(); |
| 1674 | RTy = RHS->getType(); |
| 1675 | |
| 1676 | // ... and one of the following shall hold: |
| 1677 | // -- The second or the third operand (but not both) is a throw- |
| 1678 | // expression; the result is of the type of the other and is an rvalue. |
| 1679 | bool LThrow = isa<CXXThrowExpr>(LHS); |
| 1680 | bool RThrow = isa<CXXThrowExpr>(RHS); |
| 1681 | if (LThrow && !RThrow) |
| 1682 | return RTy; |
| 1683 | if (RThrow && !LThrow) |
| 1684 | return LTy; |
| 1685 | |
| 1686 | // -- Both the second and third operands have type void; the result is of |
| 1687 | // type void and is an rvalue. |
| 1688 | if (LVoid && RVoid) |
| 1689 | return Context.VoidTy; |
| 1690 | |
| 1691 | // Neither holds, error. |
| 1692 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 1693 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
| 1694 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1695 | return QualType(); |
| 1696 | } |
| 1697 | |
| 1698 | // Neither is void. |
| 1699 | |
| 1700 | // C++0x 5.16p3 |
| 1701 | // Otherwise, if the second and third operand have different types, and |
| 1702 | // either has (cv) class type, and attempt is made to convert each of those |
| 1703 | // operands to the other. |
| 1704 | if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && |
| 1705 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 1706 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 1707 | // These return true if a single direction is already ambiguous. |
| 1708 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) |
| 1709 | return QualType(); |
| 1710 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) |
| 1711 | return QualType(); |
| 1712 | |
| 1713 | bool HaveL2R = ICSLeftToRight.ConversionKind != |
| 1714 | ImplicitConversionSequence::BadConversion; |
| 1715 | bool HaveR2L = ICSRightToLeft.ConversionKind != |
| 1716 | ImplicitConversionSequence::BadConversion; |
| 1717 | // If both can be converted, [...] the program is ill-formed. |
| 1718 | if (HaveL2R && HaveR2L) { |
| 1719 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
| 1720 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1721 | return QualType(); |
| 1722 | } |
| 1723 | |
| 1724 | // If exactly one conversion is possible, that conversion is applied to |
| 1725 | // the chosen operand and the converted operands are used in place of the |
| 1726 | // original operands for the remainder of this section. |
| 1727 | if (HaveL2R) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1728 | if (ConvertForConditional(*this, LHS, ICSLeftToRight)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1729 | return QualType(); |
| 1730 | LTy = LHS->getType(); |
| 1731 | } else if (HaveR2L) { |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1732 | if (ConvertForConditional(*this, RHS, ICSRightToLeft)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1733 | return QualType(); |
| 1734 | RTy = RHS->getType(); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | // C++0x 5.16p4 |
| 1739 | // If the second and third operands are lvalues and have the same type, |
| 1740 | // the result is of that type [...] |
| 1741 | bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); |
| 1742 | if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && |
| 1743 | RHS->isLvalue(Context) == Expr::LV_Valid) |
| 1744 | return LTy; |
| 1745 | |
| 1746 | // C++0x 5.16p5 |
| 1747 | // Otherwise, the result is an rvalue. If the second and third operands |
| 1748 | // do not have the same type, and either has (cv) class type, ... |
| 1749 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 1750 | // ... overload resolution is used to determine the conversions (if any) |
| 1751 | // to be applied to the operands. If the overload resolution fails, the |
| 1752 | // program is ill-formed. |
| 1753 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 1754 | return QualType(); |
| 1755 | } |
| 1756 | |
| 1757 | // C++0x 5.16p6 |
| 1758 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 1759 | // conversions are performed on the second and third operands. |
| 1760 | DefaultFunctionArrayConversion(LHS); |
| 1761 | DefaultFunctionArrayConversion(RHS); |
| 1762 | LTy = LHS->getType(); |
| 1763 | RTy = RHS->getType(); |
| 1764 | |
| 1765 | // After those conversions, one of the following shall hold: |
| 1766 | // -- The second and third operands have the same type; the result |
| 1767 | // is of that type. |
| 1768 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) |
| 1769 | return LTy; |
| 1770 | |
| 1771 | // -- The second and third operands have arithmetic or enumeration type; |
| 1772 | // the usual arithmetic conversions are performed to bring them to a |
| 1773 | // common type, and the result is of that type. |
| 1774 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 1775 | UsualArithmeticConversions(LHS, RHS); |
| 1776 | return LHS->getType(); |
| 1777 | } |
| 1778 | |
| 1779 | // -- The second and third operands have pointer type, or one has pointer |
| 1780 | // type and the other is a null pointer constant; pointer conversions |
| 1781 | // and qualification conversions are performed to bring them to their |
| 1782 | // composite pointer type. The result is of the composite pointer type. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1783 | QualType Composite = FindCompositePointerType(LHS, RHS); |
| 1784 | if (!Composite.isNull()) |
| 1785 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1786 | |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1787 | // Fourth bullet is same for pointers-to-member. However, the possible |
| 1788 | // conversions are far more limited: we have null-to-pointer, upcast of |
| 1789 | // containing class, and second-level cv-ness. |
| 1790 | // cv-ness is not a union, but must match one of the two operands. (Which, |
| 1791 | // frankly, is stupid.) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1792 | const MemberPointerType *LMemPtr = LTy->getAs<MemberPointerType>(); |
| 1793 | const MemberPointerType *RMemPtr = RTy->getAs<MemberPointerType>(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1794 | if (LMemPtr && |
| 1795 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1796 | ImpCastExprToType(RHS, LTy, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1797 | return LTy; |
| 1798 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1799 | if (RMemPtr && |
| 1800 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1801 | ImpCastExprToType(LHS, RTy, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1802 | return RTy; |
| 1803 | } |
| 1804 | if (LMemPtr && RMemPtr) { |
| 1805 | QualType LPointee = LMemPtr->getPointeeType(); |
| 1806 | QualType RPointee = RMemPtr->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1807 | |
| 1808 | QualifierCollector LPQuals, RPQuals; |
| 1809 | const Type *LPCan = LPQuals.strip(Context.getCanonicalType(LPointee)); |
| 1810 | const Type *RPCan = RPQuals.strip(Context.getCanonicalType(RPointee)); |
| 1811 | |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1812 | // First, we check that the unqualified pointee type is the same. If it's |
| 1813 | // not, there's no conversion that will unify the two pointers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1814 | if (LPCan == RPCan) { |
| 1815 | |
| 1816 | // Second, we take the greater of the two qualifications. If neither |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1817 | // is greater than the other, the conversion is not possible. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1818 | |
| 1819 | Qualifiers MergedQuals = LPQuals + RPQuals; |
| 1820 | |
| 1821 | bool CompatibleQuals = true; |
| 1822 | if (MergedQuals.getCVRQualifiers() != LPQuals.getCVRQualifiers() && |
| 1823 | MergedQuals.getCVRQualifiers() != RPQuals.getCVRQualifiers()) |
| 1824 | CompatibleQuals = false; |
| 1825 | else if (LPQuals.getAddressSpace() != RPQuals.getAddressSpace()) |
| 1826 | // FIXME: |
| 1827 | // C99 6.5.15 as modified by TR 18037: |
| 1828 | // If the second and third operands are pointers into different |
| 1829 | // address spaces, the address spaces must overlap. |
| 1830 | CompatibleQuals = false; |
| 1831 | // FIXME: GC qualifiers? |
| 1832 | |
| 1833 | if (CompatibleQuals) { |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1834 | // Third, we check if either of the container classes is derived from |
| 1835 | // the other. |
| 1836 | QualType LContainer(LMemPtr->getClass(), 0); |
| 1837 | QualType RContainer(RMemPtr->getClass(), 0); |
| 1838 | QualType MoreDerived; |
| 1839 | if (Context.getCanonicalType(LContainer) == |
| 1840 | Context.getCanonicalType(RContainer)) |
| 1841 | MoreDerived = LContainer; |
| 1842 | else if (IsDerivedFrom(LContainer, RContainer)) |
| 1843 | MoreDerived = LContainer; |
| 1844 | else if (IsDerivedFrom(RContainer, LContainer)) |
| 1845 | MoreDerived = RContainer; |
| 1846 | |
| 1847 | if (!MoreDerived.isNull()) { |
| 1848 | // The type 'Q Pointee (MoreDerived::*)' is the common type. |
| 1849 | // We don't use ImpCastExprToType here because this could still fail |
| 1850 | // for ambiguous or inaccessible conversions. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1851 | LPointee = Context.getQualifiedType(LPointee, MergedQuals); |
| 1852 | QualType Common |
| 1853 | = Context.getMemberPointerType(LPointee, MoreDerived.getTypePtr()); |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 1854 | if (PerformImplicitConversion(LHS, Common, "converting")) |
| 1855 | return QualType(); |
| 1856 | if (PerformImplicitConversion(RHS, Common, "converting")) |
| 1857 | return QualType(); |
| 1858 | return Common; |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1864 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 1865 | << LHS->getType() << RHS->getType() |
| 1866 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 1867 | return QualType(); |
| 1868 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1869 | |
| 1870 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 1871 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1872 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 1873 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 1874 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1875 | /// It does not emit diagnostics. |
| 1876 | QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { |
| 1877 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 1878 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1880 | if (!T1->isPointerType() && !T1->isMemberPointerType() && |
| 1881 | !T2->isPointerType() && !T2->isMemberPointerType()) |
| 1882 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1883 | |
| 1884 | // C++0x 5.9p2 |
| 1885 | // Pointer conversions and qualification conversions are performed on |
| 1886 | // pointer operands to bring them to their composite pointer type. If |
| 1887 | // one operand is a null pointer constant, the composite pointer type is |
| 1888 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1889 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1890 | if (T2->isMemberPointerType()) |
| 1891 | ImpCastExprToType(E1, T2, CastExpr::CK_NullToMemberPointer); |
| 1892 | else |
| 1893 | ImpCastExprToType(E1, T2, CastExpr::CK_IntegralToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1894 | return T2; |
| 1895 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1896 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1897 | if (T1->isMemberPointerType()) |
| 1898 | ImpCastExprToType(E2, T1, CastExpr::CK_NullToMemberPointer); |
| 1899 | else |
| 1900 | ImpCastExprToType(E2, T1, CastExpr::CK_IntegralToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1901 | return T1; |
| 1902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1904 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 1905 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 1906 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1907 | return QualType(); |
| 1908 | |
| 1909 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 1910 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 1911 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 1912 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 1913 | // type of one of the operands, with a cv-qualification signature that is |
| 1914 | // the union of the cv-qualification signatures of the operand types. |
| 1915 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 1916 | // What we do here is, we build the two possible composite types, and try the |
| 1917 | // conversions in both directions. If only one works, or if the two composite |
| 1918 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1919 | // FIXME: extended qualifiers? |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 1920 | typedef llvm::SmallVector<unsigned, 4> QualifierVector; |
| 1921 | QualifierVector QualifierUnion; |
| 1922 | typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> |
| 1923 | ContainingClassVector; |
| 1924 | ContainingClassVector MemberOfClass; |
| 1925 | QualType Composite1 = Context.getCanonicalType(T1), |
| 1926 | Composite2 = Context.getCanonicalType(T2); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1927 | do { |
| 1928 | const PointerType *Ptr1, *Ptr2; |
| 1929 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 1930 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 1931 | Composite1 = Ptr1->getPointeeType(); |
| 1932 | Composite2 = Ptr2->getPointeeType(); |
| 1933 | QualifierUnion.push_back( |
| 1934 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 1935 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 1936 | continue; |
| 1937 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1939 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 1940 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 1941 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 1942 | Composite1 = MemPtr1->getPointeeType(); |
| 1943 | Composite2 = MemPtr2->getPointeeType(); |
| 1944 | QualifierUnion.push_back( |
| 1945 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 1946 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 1947 | MemPtr2->getClass())); |
| 1948 | continue; |
| 1949 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1951 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1953 | // Cannot unwrap any more types. |
| 1954 | break; |
| 1955 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1957 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 1958 | ContainingClassVector::reverse_iterator MOC |
| 1959 | = MemberOfClass.rbegin(); |
| 1960 | for (QualifierVector::reverse_iterator |
| 1961 | I = QualifierUnion.rbegin(), |
| 1962 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1963 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1964 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1965 | if (MOC->first && MOC->second) { |
| 1966 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1967 | Composite1 = Context.getMemberPointerType( |
| 1968 | Context.getQualifiedType(Composite1, Quals), |
| 1969 | MOC->first); |
| 1970 | Composite2 = Context.getMemberPointerType( |
| 1971 | Context.getQualifiedType(Composite2, Quals), |
| 1972 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1973 | } else { |
| 1974 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1975 | Composite1 |
| 1976 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 1977 | Composite2 |
| 1978 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 1979 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | ImplicitConversionSequence E1ToC1 = |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1983 | TryImplicitConversion(E1, Composite1, |
| 1984 | /*SuppressUserConversions=*/false, |
| 1985 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1986 | /*ForceRValue=*/false, |
| 1987 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | ImplicitConversionSequence E2ToC1 = |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1989 | TryImplicitConversion(E2, Composite1, |
| 1990 | /*SuppressUserConversions=*/false, |
| 1991 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1992 | /*ForceRValue=*/false, |
| 1993 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 1995 | ImplicitConversionSequence E1ToC2, E2ToC2; |
| 1996 | E1ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1997 | E2ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1998 | if (Context.getCanonicalType(Composite1) != |
| 1999 | Context.getCanonicalType(Composite2)) { |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2000 | E1ToC2 = TryImplicitConversion(E1, Composite2, |
| 2001 | /*SuppressUserConversions=*/false, |
| 2002 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2003 | /*ForceRValue=*/false, |
| 2004 | /*InOverloadResolution=*/false); |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2005 | E2ToC2 = TryImplicitConversion(E2, Composite2, |
| 2006 | /*SuppressUserConversions=*/false, |
| 2007 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2008 | /*ForceRValue=*/false, |
| 2009 | /*InOverloadResolution=*/false); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | bool ToC1Viable = E1ToC1.ConversionKind != |
| 2013 | ImplicitConversionSequence::BadConversion |
| 2014 | && E2ToC1.ConversionKind != |
| 2015 | ImplicitConversionSequence::BadConversion; |
| 2016 | bool ToC2Viable = E1ToC2.ConversionKind != |
| 2017 | ImplicitConversionSequence::BadConversion |
| 2018 | && E2ToC2.ConversionKind != |
| 2019 | ImplicitConversionSequence::BadConversion; |
| 2020 | if (ToC1Viable && !ToC2Viable) { |
| 2021 | if (!PerformImplicitConversion(E1, Composite1, E1ToC1, "converting") && |
| 2022 | !PerformImplicitConversion(E2, Composite1, E2ToC1, "converting")) |
| 2023 | return Composite1; |
| 2024 | } |
| 2025 | if (ToC2Viable && !ToC1Viable) { |
| 2026 | if (!PerformImplicitConversion(E1, Composite2, E1ToC2, "converting") && |
| 2027 | !PerformImplicitConversion(E2, Composite2, E2ToC2, "converting")) |
| 2028 | return Composite2; |
| 2029 | } |
| 2030 | return QualType(); |
| 2031 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2032 | |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2033 | Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 2034 | if (!Context.getLangOptions().CPlusPlus) |
| 2035 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2037 | const RecordType *RT = E->getType()->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2038 | if (!RT) |
| 2039 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2041 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2042 | if (RD->hasTrivialDestructor()) |
| 2043 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2045 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 2046 | QualType Ty = CE->getCallee()->getType(); |
| 2047 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 2048 | Ty = PT->getPointeeType(); |
| 2049 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2050 | const FunctionType *FTy = Ty->getAs<FunctionType>(); |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2051 | if (FTy->getResultType()->isReferenceType()) |
| 2052 | return Owned(E); |
| 2053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | CXXTemporary *Temp = CXXTemporary::Create(Context, |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2055 | RD->getDestructor(Context)); |
Anders Carlsson | 860306e | 2009-05-30 21:21:49 +0000 | [diff] [blame] | 2056 | ExprTemporaries.push_back(Temp); |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2057 | if (CXXDestructorDecl *Destructor = |
| 2058 | const_cast<CXXDestructorDecl*>(RD->getDestructor(Context))) |
| 2059 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2060 | // FIXME: Add the temporary to the temporaries vector. |
| 2061 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 2062 | } |
| 2063 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 2065 | bool ShouldDestroyTemps) { |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2066 | assert(SubExpr && "sub expression can't be null!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2068 | if (ExprTemporaries.empty()) |
| 2069 | return SubExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2071 | Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | &ExprTemporaries[0], |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2073 | ExprTemporaries.size(), |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 2074 | ShouldDestroyTemps); |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2075 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2076 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2077 | return E; |
| 2078 | } |
| 2079 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | Sema::OwningExprResult |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2081 | Sema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 2082 | tok::TokenKind OpKind, TypeTy *&ObjectType) { |
| 2083 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2084 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2086 | Expr *BaseExpr = (Expr*)Base.get(); |
| 2087 | assert(BaseExpr && "no record expansion"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2089 | QualType BaseType = BaseExpr->getType(); |
| 2090 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 2091 | // If we have a pointer to a dependent type and are using the -> operator, |
| 2092 | // the object type is the type that the pointer points to. We might still |
| 2093 | // have enough information about that type to do something useful. |
| 2094 | if (OpKind == tok::arrow) |
| 2095 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 2096 | BaseType = Ptr->getPointeeType(); |
| 2097 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2098 | ObjectType = BaseType.getAsOpaquePtr(); |
| 2099 | return move(Base); |
| 2100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2102 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2104 | // returned, with the original second operand. |
| 2105 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2106 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2107 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2108 | llvm::SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2109 | CTypes.insert(Context.getCanonicalType(BaseType)); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2110 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2111 | while (BaseType->isRecordType()) { |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 2112 | Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2113 | BaseExpr = (Expr*)Base.get(); |
| 2114 | if (BaseExpr == NULL) |
| 2115 | return ExprError(); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2116 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 2117 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2118 | BaseType = BaseExpr->getType(); |
| 2119 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2120 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 2121 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2122 | for (unsigned i = 0; i < Locations.size(); i++) |
| 2123 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 2124 | return ExprError(); |
| 2125 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2129 | if (BaseType->isPointerType()) |
| 2130 | BaseType = BaseType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | |
| 2132 | // We could end up with various non-record types here, such as extended |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2133 | // vector types or Objective-C interfaces. Just return early and let |
| 2134 | // ActOnMemberReferenceExpr do the work. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2135 | if (!BaseType->isRecordType()) { |
| 2136 | // C++ [basic.lookup.classref]p2: |
| 2137 | // [...] If the type of the object expression is of pointer to scalar |
| 2138 | // type, the unqualified-id is looked up in the context of the complete |
| 2139 | // postfix-expression. |
| 2140 | ObjectType = 0; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2141 | return move(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2144 | // The object type must be complete (or dependent). |
| 2145 | if (!BaseType->isDependentType() && |
| 2146 | RequireCompleteType(OpLoc, BaseType, |
| 2147 | PDiag(diag::err_incomplete_member_access))) |
| 2148 | return ExprError(); |
| 2149 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2150 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | // If the id-expression in a class member access (5.2.5) is an |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2152 | // unqualified-id, and the type of the object expression is of a class |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2153 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 2154 | // up in the scope of class C. [...] |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2155 | ObjectType = BaseType.getAsOpaquePtr(); |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2160 | CXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp, |
| 2161 | CXXMethodDecl *Method) { |
| 2162 | MemberExpr *ME = |
| 2163 | new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, |
| 2164 | SourceLocation(), Method->getType()); |
| 2165 | QualType ResultType; |
| 2166 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(Method)) |
| 2167 | ResultType = Conv->getConversionType().getNonReferenceType(); |
| 2168 | else |
| 2169 | ResultType = Method->getResultType().getNonReferenceType(); |
| 2170 | |
| 2171 | CXXMemberCallExpr *CE = |
| 2172 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, |
| 2173 | ResultType, |
Douglas Gregor | 00b98c2 | 2009-11-12 15:31:47 +0000 | [diff] [blame] | 2174 | Exp->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2175 | return CE; |
| 2176 | } |
| 2177 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2178 | Sema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc, |
| 2179 | QualType Ty, |
| 2180 | CastExpr::CastKind Kind, |
| 2181 | CXXMethodDecl *Method, |
| 2182 | ExprArg Arg) { |
| 2183 | Expr *From = Arg.takeAs<Expr>(); |
| 2184 | |
| 2185 | switch (Kind) { |
| 2186 | default: assert(0 && "Unhandled cast kind!"); |
| 2187 | case CastExpr::CK_ConstructorConversion: { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 2188 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 2189 | |
| 2190 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method), |
| 2191 | MultiExprArg(*this, (void **)&From, 1), |
| 2192 | CastLoc, ConstructorArgs)) |
| 2193 | return ExprError(); |
Anders Carlsson | 4fa2684 | 2009-10-18 21:20:14 +0000 | [diff] [blame] | 2194 | |
| 2195 | OwningExprResult Result = |
| 2196 | BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
| 2197 | move_arg(ConstructorArgs)); |
| 2198 | if (Result.isInvalid()) |
| 2199 | return ExprError(); |
| 2200 | |
| 2201 | return MaybeBindToTemporary(Result.takeAs<Expr>()); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | case CastExpr::CK_UserDefinedConversion: { |
Anders Carlsson | aac6e3a | 2009-09-15 07:42:44 +0000 | [diff] [blame] | 2205 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
| 2206 | |
| 2207 | // Cast to base if needed. |
| 2208 | if (PerformObjectArgumentInitialization(From, Method)) |
| 2209 | return ExprError(); |
| 2210 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2211 | // Create an implicit call expr that calls it. |
| 2212 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method); |
Anders Carlsson | 4fa2684 | 2009-10-18 21:20:14 +0000 | [diff] [blame] | 2213 | return MaybeBindToTemporary(CE); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2214 | } |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2218 | Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { |
| 2219 | Expr *FullExpr = Arg.takeAs<Expr>(); |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2220 | if (FullExpr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr, |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 2222 | /*ShouldDestroyTemps=*/true); |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2223 | |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 2224 | |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2225 | return Owned(FullExpr); |
| 2226 | } |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 2227 | |
| 2228 | /// \brief Determine whether a reference to the given declaration in the |
| 2229 | /// current context is an implicit member access |
| 2230 | /// (C++ [class.mfct.non-static]p2). |
| 2231 | /// |
| 2232 | /// FIXME: Should Objective-C also use this approach? |
| 2233 | /// |
| 2234 | /// \param SS if non-NULL, the C++ nested-name-specifier that precedes the |
| 2235 | /// name of the declaration referenced. |
| 2236 | /// |
| 2237 | /// \param D the declaration being referenced from the current scope. |
| 2238 | /// |
| 2239 | /// \param NameLoc the location of the name in the source. |
| 2240 | /// |
| 2241 | /// \param ThisType if the reference to this declaration is an implicit member |
| 2242 | /// access, will be set to the type of the "this" pointer to be used when |
| 2243 | /// building that implicit member access. |
| 2244 | /// |
| 2245 | /// \param MemberType if the reference to this declaration is an implicit |
| 2246 | /// member access, will be set to the type of the member being referenced |
| 2247 | /// (for use at the type of the resulting member access expression). |
| 2248 | /// |
| 2249 | /// \returns true if this is an implicit member reference (in which case |
| 2250 | /// \p ThisType and \p MemberType will be set), or false if it is not an |
| 2251 | /// implicit member reference. |
| 2252 | bool Sema::isImplicitMemberReference(const CXXScopeSpec *SS, NamedDecl *D, |
| 2253 | SourceLocation NameLoc, QualType &ThisType, |
| 2254 | QualType &MemberType) { |
| 2255 | // If this isn't a C++ method, then it isn't an implicit member reference. |
| 2256 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext); |
| 2257 | if (!MD || MD->isStatic()) |
| 2258 | return false; |
| 2259 | |
| 2260 | // C++ [class.mfct.nonstatic]p2: |
| 2261 | // [...] if name lookup (3.4.1) resolves the name in the |
| 2262 | // id-expression to a nonstatic nontype member of class X or of |
| 2263 | // a base class of X, the id-expression is transformed into a |
| 2264 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 2265 | // as the postfix-expression to the left of the '.' operator. |
| 2266 | DeclContext *Ctx = 0; |
| 2267 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2268 | Ctx = FD->getDeclContext(); |
| 2269 | MemberType = FD->getType(); |
| 2270 | |
| 2271 | if (const ReferenceType *RefType = MemberType->getAs<ReferenceType>()) |
| 2272 | MemberType = RefType->getPointeeType(); |
| 2273 | else if (!FD->isMutable()) |
| 2274 | MemberType |
| 2275 | = Context.getQualifiedType(MemberType, |
| 2276 | Qualifiers::fromCVRMask(MD->getTypeQualifiers())); |
| 2277 | } else { |
| 2278 | for (OverloadIterator Ovl(D), OvlEnd; Ovl != OvlEnd; ++Ovl) { |
| 2279 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Ovl); |
| 2280 | FunctionTemplateDecl *FunTmpl = 0; |
| 2281 | if (!Method && (FunTmpl = dyn_cast<FunctionTemplateDecl>(*Ovl))) |
| 2282 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 2283 | |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 2284 | // FIXME: Do we have to know if there are explicit template arguments? |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 2285 | if (Method && !Method->isStatic()) { |
| 2286 | Ctx = Method->getParent(); |
| 2287 | if (isa<CXXMethodDecl>(D) && !FunTmpl) |
| 2288 | MemberType = Method->getType(); |
| 2289 | else |
| 2290 | MemberType = Context.OverloadTy; |
| 2291 | break; |
| 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | if (!Ctx || !Ctx->isRecord()) |
| 2297 | return false; |
| 2298 | |
| 2299 | // Determine whether the declaration(s) we found are actually in a base |
| 2300 | // class. If not, this isn't an implicit member reference. |
| 2301 | ThisType = MD->getThisType(Context); |
Douglas Gregor | 7a34314 | 2009-11-01 17:08:18 +0000 | [diff] [blame] | 2302 | |
| 2303 | // If the type of "this" is dependent, we can't tell if the member is in a |
| 2304 | // base class or not, so treat this as a dependent implicit member reference. |
| 2305 | if (ThisType->isDependentType()) |
| 2306 | return true; |
| 2307 | |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 2308 | QualType CtxType = Context.getTypeDeclType(cast<CXXRecordDecl>(Ctx)); |
| 2309 | QualType ClassType |
| 2310 | = Context.getTypeDeclType(cast<CXXRecordDecl>(MD->getParent())); |
| 2311 | return Context.hasSameType(CtxType, ClassType) || |
| 2312 | IsDerivedFrom(ClassType, CtxType); |
| 2313 | } |
| 2314 | |