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