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