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