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