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