Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 14 | #include "SemaInherit.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "Sema.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 24 | /// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 25 | /// name (e.g., operator void const *) as an expression. This is |
| 26 | /// very similar to ActOnIdentifierExpr, except that instead of |
| 27 | /// providing an identifier the parser provides the type of the |
| 28 | /// conversion function. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 29 | Sema::OwningExprResult |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 30 | Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc, |
| 31 | TypeTy *Ty, bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 32 | const CXXScopeSpec &SS, |
| 33 | bool isAddressOfOperand) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 34 | QualType ConvType = QualType::getFromOpaquePtr(Ty); |
| 35 | QualType ConvTypeCanon = Context.getCanonicalType(ConvType); |
| 36 | DeclarationName ConvName |
| 37 | = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 38 | return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 39 | &SS, isAddressOfOperand); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 40 | } |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 42 | /// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 43 | /// name (e.g., @c operator+ ) as an expression. This is very |
| 44 | /// similar to ActOnIdentifierExpr, except that instead of providing |
| 45 | /// an identifier the parser provides the kind of overloaded |
| 46 | /// operator that was parsed. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 47 | Sema::OwningExprResult |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 48 | Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc, |
| 49 | OverloadedOperatorKind Op, |
| 50 | bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 51 | const CXXScopeSpec &SS, |
| 52 | bool isAddressOfOperand) { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 53 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 54 | return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 55 | isAddressOfOperand); |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 58 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 59 | Action::OwningExprResult |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 60 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 61 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 62 | NamespaceDecl *StdNs = GetStdNamespace(); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 63 | if (!StdNs) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 64 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 65 | |
| 66 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 67 | Decl *TypeInfoDecl = LookupQualifiedName(StdNs, TypeInfoII, LookupTagName); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 68 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 69 | if (!TypeInfoRecordDecl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 70 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 71 | |
| 72 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 73 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 74 | return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, |
| 75 | TypeInfoType.withConst(), |
| 76 | SourceRange(OpLoc, RParenLoc))); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 79 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 80 | Action::OwningExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 81 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 82 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 84 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 85 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 87 | |
| 88 | /// ActOnCXXThrow - Parse throw expressions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 89 | Action::OwningExprResult |
| 90 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { |
| 91 | return Owned(new (Context) CXXThrowExpr((Expr*)E.release(), Context.VoidTy, |
| 92 | OpLoc)); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 93 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 94 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 95 | Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 96 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 97 | /// is a non-lvalue expression whose value is the address of the object for |
| 98 | /// which the function is called. |
| 99 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 100 | if (!isa<FunctionDecl>(CurContext)) |
| 101 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 102 | |
| 103 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 104 | if (MD->isInstance()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 105 | return Owned(new (Context) CXXThisExpr(ThisLoc, |
| 106 | MD->getThisType(Context))); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 107 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 108 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 109 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 110 | |
| 111 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 112 | /// Can be interpreted either as function-style casting ("int(x)") |
| 113 | /// or class type construction ("ClassType(x,y,z)") |
| 114 | /// or creation of a value-initialized type ("int()"). |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 115 | Action::OwningExprResult |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 116 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 117 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 118 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 119 | SourceLocation *CommaLocs, |
| 120 | SourceLocation RParenLoc) { |
| 121 | assert(TypeRep && "Missing type!"); |
| 122 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 123 | unsigned NumExprs = exprs.size(); |
| 124 | Expr **Exprs = (Expr**)exprs.get(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 125 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 126 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 127 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 128 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 129 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 130 | exprs.release(); |
| 131 | return Owned(new (Context) CXXTemporaryObjectExpr(0, Ty, TyBeginLoc, |
| 132 | Exprs, NumExprs, |
| 133 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 137 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 138 | // If the expression list is a single expression, the type conversion |
| 139 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 140 | // corresponding cast expression. |
| 141 | // |
| 142 | if (NumExprs == 1) { |
| 143 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 144 | return ExprError(); |
| 145 | exprs.release(); |
| 146 | return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), |
| 147 | Ty, TyBeginLoc, Exprs[0], |
| 148 | RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 151 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 152 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 154 | if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) { |
| 155 | CXXConstructorDecl *Constructor |
| 156 | = PerformInitializationByConstructor(Ty, Exprs, NumExprs, |
| 157 | TypeRange.getBegin(), |
| 158 | SourceRange(TypeRange.getBegin(), |
| 159 | RParenLoc), |
| 160 | DeclarationName(), |
| 161 | IK_Direct); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 162 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 163 | if (!Constructor) |
| 164 | return ExprError(); |
| 165 | |
| 166 | exprs.release(); |
| 167 | return Owned(new (Context) CXXTemporaryObjectExpr(Constructor, Ty, |
| 168 | TyBeginLoc, Exprs, |
| 169 | NumExprs, RParenLoc)); |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Fall through to value-initialize an object of class type that |
| 173 | // doesn't have a user-declared default constructor. |
| 174 | } |
| 175 | |
| 176 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 177 | // If the expression list specifies more than a single value, the type shall |
| 178 | // be a class with a suitably declared constructor. |
| 179 | // |
| 180 | if (NumExprs > 1) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 181 | return ExprError(Diag(CommaLocs[0], |
| 182 | diag::err_builtin_func_cast_more_than_one_arg) |
| 183 | << FullRange); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 184 | |
| 185 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 186 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 187 | // C++ [expr.type.conv]p2: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 188 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 189 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 190 | // rvalue of the specified type, which is value-initialized. |
| 191 | // |
| 192 | if (Ty->isArrayType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 193 | return ExprError(Diag(TyBeginLoc, |
| 194 | diag::err_value_init_for_array_type) << FullRange); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 195 | if (!Ty->isDependentType() && !Ty->isVoidType() && |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 196 | RequireCompleteType(TyBeginLoc, Ty, |
| 197 | diag::err_invalid_incomplete_type_use, FullRange)) |
| 198 | return ExprError(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 199 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 200 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 201 | diag::err_allocation_of_abstract_type)) |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 202 | return ExprError(); |
| 203 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 204 | exprs.release(); |
| 205 | return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 206 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 207 | |
| 208 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 209 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 210 | /// @code new (memory) int[size][4] @endcode |
| 211 | /// or |
| 212 | /// @code ::new Foo(23, "hello") @endcode |
| 213 | /// For the interpretation of this heap of arguments, consult the base version. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 214 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 215 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 216 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 217 | SourceLocation PlacementRParen, bool ParenTypeId, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 218 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 219 | MultiExprArg ConstructorArgs, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 220 | SourceLocation ConstructorRParen) |
| 221 | { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 222 | Expr *ArraySize = 0; |
| 223 | unsigned Skip = 0; |
| 224 | // If the specified type is an array, unwrap it and save the expression. |
| 225 | if (D.getNumTypeObjects() > 0 && |
| 226 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 227 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 228 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 229 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 230 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 231 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 232 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 233 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 234 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
| 235 | Skip = 1; |
| 236 | } |
| 237 | |
| 238 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip); |
| 239 | if (D.getInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 240 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 241 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 242 | if (CheckAllocatedType(AllocType, D)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 243 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 244 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 245 | QualType ResultType = AllocType->isDependentType() |
| 246 | ? Context.DependentTy |
| 247 | : Context.getPointerType(AllocType); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 248 | |
| 249 | // That every array dimension except the first is constant was already |
| 250 | // checked by the type check above. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 251 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 252 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 253 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 254 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 255 | QualType SizeType = ArraySize->getType(); |
| 256 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 257 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 258 | diag::err_array_size_not_integral) |
| 259 | << SizeType << ArraySize->getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 260 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 261 | // We don't care about special rules, so we tell the machinery it's not |
| 262 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 263 | if (!ArraySize->isValueDependent()) { |
| 264 | llvm::APSInt Value; |
| 265 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 266 | if (Value < llvm::APSInt( |
| 267 | llvm::APInt::getNullValue(Value.getBitWidth()), false)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 268 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
| 269 | diag::err_typecheck_negative_array_size) |
| 270 | << ArraySize->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 271 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 274 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 275 | FunctionDecl *OperatorNew = 0; |
| 276 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 277 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 278 | unsigned NumPlaceArgs = PlacementArgs.size(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 279 | if (!AllocType->isDependentType() && |
| 280 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 281 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 282 | SourceRange(PlacementLParen, PlacementRParen), |
| 283 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 284 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 285 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 286 | |
| 287 | bool Init = ConstructorLParen.isValid(); |
| 288 | // --- Choosing a constructor --- |
| 289 | // C++ 5.3.4p15 |
| 290 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) |
| 291 | // the object is not initialized. If the object, or any part of it, is |
| 292 | // const-qualified, it's an error. |
| 293 | // 2) If T is a POD and there's an empty initializer, the object is value- |
| 294 | // initialized. |
| 295 | // 3) If T is a POD and there's one initializer argument, the object is copy- |
| 296 | // constructed. |
| 297 | // 4) If T is a POD and there's more initializer arguments, it's an error. |
| 298 | // 5) If T is not a POD, the initializer arguments are used as constructor |
| 299 | // arguments. |
| 300 | // |
| 301 | // Or by the C++0x formulation: |
| 302 | // 1) If there's no initializer, the object is default-initialized according |
| 303 | // to C++0x rules. |
| 304 | // 2) Otherwise, the object is direct-initialized. |
| 305 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 306 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 307 | unsigned NumConsArgs = ConstructorArgs.size(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 308 | if (AllocType->isDependentType()) { |
| 309 | // Skip all the checks. |
| 310 | } |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 311 | // FIXME: Should check for primitive/aggregate here, not record. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 312 | else if (const RecordType *RT = AllocType->getAsRecordType()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 313 | // FIXME: This is incorrect for when there is an empty initializer and |
| 314 | // no user-defined constructor. Must zero-initialize, not default-construct. |
| 315 | Constructor = PerformInitializationByConstructor( |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 316 | AllocType, ConsArgs, NumConsArgs, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 317 | D.getSourceRange().getBegin(), |
| 318 | SourceRange(D.getSourceRange().getBegin(), |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 319 | ConstructorRParen), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 320 | RT->getDecl()->getDeclName(), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 321 | NumConsArgs != 0 ? IK_Direct : IK_Default); |
| 322 | if (!Constructor) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 323 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 324 | } else { |
| 325 | if (!Init) { |
| 326 | // FIXME: Check that no subpart is const. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 327 | if (AllocType.isConstQualified()) |
| 328 | return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const) |
| 329 | << D.getSourceRange()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 330 | } else if (NumConsArgs == 0) { |
| 331 | // Object is value-initialized. Do nothing. |
| 332 | } else if (NumConsArgs == 1) { |
| 333 | // Object is direct-initialized. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 334 | // FIXME: WHAT DeclarationName do we pass in here? |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 335 | if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 336 | DeclarationName() /*AllocType.getAsString()*/, |
| 337 | /*DirectInit=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 338 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 339 | } else { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 340 | return ExprError(Diag(StartLoc, |
| 341 | diag::err_builtin_direct_init_more_than_one_arg) |
| 342 | << SourceRange(ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
| 347 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 348 | PlacementArgs.release(); |
| 349 | ConstructorArgs.release(); |
| 350 | return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 351 | NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 352 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 353 | StartLoc, Init ? ConstructorRParen : SourceLocation())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 357 | /// in a new-expression. |
| 358 | /// dimension off and stores the size expression in ArraySize. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 359 | bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 360 | { |
| 361 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 362 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 363 | if (AllocType->isFunctionType()) |
| 364 | return Diag(D.getSourceRange().getBegin(), diag::err_bad_new_type) |
| 365 | << AllocType << 0 << D.getSourceRange(); |
| 366 | else if (AllocType->isReferenceType()) |
| 367 | return Diag(D.getSourceRange().getBegin(), diag::err_bad_new_type) |
| 368 | << AllocType << 1 << D.getSourceRange(); |
| 369 | else if (!AllocType->isDependentType() && |
| 370 | RequireCompleteType(D.getSourceRange().getBegin(), AllocType, |
| 371 | diag::err_new_incomplete_type, |
| 372 | D.getSourceRange())) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 373 | return true; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 374 | else if (RequireNonAbstractType(D.getSourceRange().getBegin(), AllocType, |
| 375 | diag::err_allocation_of_abstract_type)) |
| 376 | return true; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 377 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 378 | // Every dimension shall be of constant size. |
| 379 | unsigned i = 1; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 380 | while (const ArrayType *Array = Context.getAsArrayType(AllocType)) { |
| 381 | if (!Array->isConstantArrayType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 382 | Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst) |
| 383 | << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 384 | return true; |
| 385 | } |
| 386 | AllocType = Array->getElementType(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 387 | ++i; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return false; |
| 391 | } |
| 392 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 393 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 394 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 395 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 396 | bool UseGlobal, QualType AllocType, |
| 397 | bool IsArray, Expr **PlaceArgs, |
| 398 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 399 | FunctionDecl *&OperatorNew, |
| 400 | FunctionDecl *&OperatorDelete) |
| 401 | { |
| 402 | // --- Choosing an allocation function --- |
| 403 | // C++ 5.3.4p8 - 14 & 18 |
| 404 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 405 | // in the scope of the allocated class. |
| 406 | // 2) If an array size is given, look for operator new[], else look for |
| 407 | // operator new. |
| 408 | // 3) The first argument is always size_t. Append the arguments from the |
| 409 | // placement form. |
| 410 | // FIXME: Also find the appropriate delete operator. |
| 411 | |
| 412 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 413 | // We don't care about the actual value of this argument. |
| 414 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 415 | // tree? Or should the consumer just recalculate the value? |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 416 | AllocArgs[0] = new (Context) IntegerLiteral(llvm::APInt::getNullValue( |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 417 | Context.Target.getPointerWidth(0)), |
| 418 | Context.getSizeType(), |
| 419 | SourceLocation()); |
| 420 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 421 | |
| 422 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 423 | IsArray ? OO_Array_New : OO_New); |
| 424 | if (AllocType->isRecordType() && !UseGlobal) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 425 | CXXRecordDecl *Record |
| 426 | = cast<CXXRecordDecl>(AllocType->getAsRecordType()->getDecl()); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 427 | // FIXME: We fail to find inherited overloads. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 428 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 429 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 430 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 431 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 432 | } |
| 433 | if (!OperatorNew) { |
| 434 | // Didn't find a member overload. Look for a global one. |
| 435 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 436 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 437 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 438 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 439 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 440 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 443 | // FIXME: This is leaked on error. But so much is currently in Sema that it's |
| 444 | // easier to clean it in one go. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 445 | AllocArgs[0]->Destroy(Context); |
| 446 | return false; |
| 447 | } |
| 448 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 449 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 450 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 451 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 452 | DeclarationName Name, Expr** Args, |
| 453 | unsigned NumArgs, DeclContext *Ctx, |
| 454 | bool AllowMissing, FunctionDecl *&Operator) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 455 | { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 456 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 457 | llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 458 | if (Alloc == AllocEnd) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 459 | if (AllowMissing) |
| 460 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 461 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 462 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | OverloadCandidateSet Candidates; |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 466 | for (; Alloc != AllocEnd; ++Alloc) { |
| 467 | // Even member operator new/delete are implicitly treated as |
| 468 | // static, so don't use AddMemberCandidate. |
| 469 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) |
| 470 | AddOverloadCandidate(Fn, Args, NumArgs, Candidates, |
| 471 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Do the resolution. |
| 475 | OverloadCandidateSet::iterator Best; |
| 476 | switch(BestViableFunction(Candidates, Best)) { |
| 477 | case OR_Success: { |
| 478 | // Got one! |
| 479 | FunctionDecl *FnDecl = Best->Function; |
| 480 | // The first argument is size_t, and the first parameter must be size_t, |
| 481 | // too. This is checked on declaration and can be assumed. (It can't be |
| 482 | // asserted on, though, since invalid decls are left in there.) |
| 483 | for (unsigned i = 1; i < NumArgs; ++i) { |
| 484 | // FIXME: Passing word to diagnostic. |
| 485 | if (PerformCopyInitialization(Args[i-1], |
| 486 | FnDecl->getParamDecl(i)->getType(), |
| 487 | "passing")) |
| 488 | return true; |
| 489 | } |
| 490 | Operator = FnDecl; |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | case OR_No_Viable_Function: |
| 495 | if (AllowMissing) |
| 496 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 497 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 498 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 499 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/false); |
| 500 | return true; |
| 501 | |
| 502 | case OR_Ambiguous: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 503 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 504 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 505 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 506 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 507 | |
| 508 | case OR_Deleted: |
| 509 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 510 | << Best->Function->isDeleted() |
| 511 | << Name << Range; |
| 512 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 513 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 514 | } |
| 515 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 520 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 521 | /// delete. These are: |
| 522 | /// @code |
| 523 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 524 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 525 | /// void operator delete(void *) throw(); |
| 526 | /// void operator delete[](void *) throw(); |
| 527 | /// @endcode |
| 528 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 529 | /// declared. Their use requires including \<new\>. |
| 530 | void Sema::DeclareGlobalNewDelete() |
| 531 | { |
| 532 | if (GlobalNewDeleteDeclared) |
| 533 | return; |
| 534 | GlobalNewDeleteDeclared = true; |
| 535 | |
| 536 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 537 | QualType SizeT = Context.getSizeType(); |
| 538 | |
| 539 | // FIXME: Exception specifications are not added. |
| 540 | DeclareGlobalAllocationFunction( |
| 541 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
| 542 | VoidPtr, SizeT); |
| 543 | DeclareGlobalAllocationFunction( |
| 544 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
| 545 | VoidPtr, SizeT); |
| 546 | DeclareGlobalAllocationFunction( |
| 547 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 548 | Context.VoidTy, VoidPtr); |
| 549 | DeclareGlobalAllocationFunction( |
| 550 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 551 | Context.VoidTy, VoidPtr); |
| 552 | } |
| 553 | |
| 554 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 555 | /// allocation function if it doesn't already exist. |
| 556 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
| 557 | QualType Return, QualType Argument) |
| 558 | { |
| 559 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 560 | |
| 561 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 562 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 563 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 564 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 565 | Alloc != AllocEnd; ++Alloc) { |
| 566 | // FIXME: Do we need to check for default arguments here? |
| 567 | FunctionDecl *Func = cast<FunctionDecl>(*Alloc); |
| 568 | if (Func->getNumParams() == 1 && |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 569 | Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 570 | return; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0); |
| 575 | FunctionDecl *Alloc = |
| 576 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 577 | FnType, FunctionDecl::None, false, true, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 578 | SourceLocation()); |
| 579 | Alloc->setImplicit(); |
| 580 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 581 | 0, Argument, VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 582 | Alloc->setParams(Context, &Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 584 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 585 | // make sure it is at the end of the chain to coincide with the |
| 586 | // global scope. |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 587 | ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 590 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 591 | /// @code ::delete ptr; @endcode |
| 592 | /// or |
| 593 | /// @code delete [] ptr; @endcode |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 594 | Action::OwningExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 595 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 596 | bool ArrayForm, ExprArg Operand) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 597 | { |
| 598 | // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type |
| 599 | // having a single conversion function to a pointer type. The result has |
| 600 | // type void." |
| 601 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 602 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 603 | Expr *Ex = (Expr *)Operand.get(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 604 | if (!Ex->isTypeDependent()) { |
| 605 | QualType Type = Ex->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 606 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 607 | if (Type->isRecordType()) { |
| 608 | // FIXME: Find that one conversion function and amend the type. |
| 609 | } |
| 610 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 611 | if (!Type->isPointerType()) |
| 612 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 613 | << Type << Ex->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 614 | |
| 615 | QualType Pointee = Type->getAsPointerType()->getPointeeType(); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 616 | if (Pointee->isFunctionType() || Pointee->isVoidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 617 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 618 | << Type << Ex->getSourceRange()); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 619 | else if (!Pointee->isDependentType() && |
| 620 | RequireCompleteType(StartLoc, Pointee, |
| 621 | diag::warn_delete_incomplete, |
| 622 | Ex->getSourceRange())) |
| 623 | return ExprError(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 624 | |
| 625 | // FIXME: Look up the correct operator delete overload and pass a pointer |
| 626 | // along. |
| 627 | // FIXME: Check access and ambiguity of operator delete and destructor. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 630 | Operand.release(); |
| 631 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
| 632 | 0, Ex, StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 636 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 637 | /// C++ if/switch/while/for statement. |
| 638 | /// e.g: "if (int x = f()) {...}" |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 639 | Action::OwningExprResult |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 640 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 641 | Declarator &D, |
| 642 | SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 643 | ExprArg AssignExprVal) { |
| 644 | assert(AssignExprVal.get() && "Null assignment expression"); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 645 | |
| 646 | // C++ 6.4p2: |
| 647 | // The declarator shall not specify a function or an array. |
| 648 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 649 | // new class or enumeration. |
| 650 | |
| 651 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 652 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 653 | |
| 654 | QualType Ty = GetTypeForDeclarator(D, S); |
| 655 | |
| 656 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 657 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 658 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 659 | return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type) |
| 660 | << SourceRange(StartLoc, EqualLoc)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 661 | } else if (Ty->isArrayType()) { // ...or an array. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 662 | Diag(StartLoc, diag::err_invalid_use_of_array_type) |
| 663 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 664 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 665 | RecordDecl *RD = RT->getDecl(); |
| 666 | // The type-specifier-seq shall not declare a new class... |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 667 | if (RD->isDefinition() && |
| 668 | (RD->getIdentifier() == 0 || S->isDeclScope(DeclPtrTy::make(RD)))) |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 669 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 670 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 671 | EnumDecl *ED = ET->getDecl(); |
| 672 | // ...or enumeration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 673 | if (ED->isDefinition() && |
| 674 | (ED->getIdentifier() == 0 || S->isDeclScope(DeclPtrTy::make(ED)))) |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 675 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 676 | } |
| 677 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 678 | DeclPtrTy Dcl = ActOnDeclarator(S, D, DeclPtrTy()); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 679 | if (!Dcl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 680 | return ExprError(); |
| 681 | AddInitializerToDecl(Dcl, move(AssignExprVal)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 683 | // Mark this variable as one that is declared within a conditional. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 684 | // We know that the decl had to be a VarDecl because that is the only type of |
| 685 | // decl that can be assigned and the grammar requires an '='. |
| 686 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 687 | VD->setDeclaredInCondition(true); |
| 688 | return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 692 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 693 | // C++ 6.4p4: |
| 694 | // The value of a condition that is an initialized declaration in a statement |
| 695 | // other than a switch statement is the value of the declared variable |
| 696 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 697 | // program is ill-formed. |
| 698 | // The value of a condition that is an expression is the value of the |
| 699 | // expression, implicitly converted to bool. |
| 700 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 701 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 702 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 703 | |
| 704 | /// Helper function to determine whether this is the (deprecated) C++ |
| 705 | /// conversion from a string literal to a pointer to non-const char or |
| 706 | /// non-const wchar_t (for narrow and wide string literals, |
| 707 | /// respectively). |
| 708 | bool |
| 709 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 710 | // Look inside the implicit cast, if it exists. |
| 711 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 712 | From = Cast->getSubExpr(); |
| 713 | |
| 714 | // A string literal (2.13.4) that is not a wide string literal can |
| 715 | // be converted to an rvalue of type "pointer to char"; a wide |
| 716 | // string literal can be converted to an rvalue of type "pointer |
| 717 | // to wchar_t" (C++ 4.2p2). |
| 718 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 719 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 720 | if (const BuiltinType *ToPointeeType |
| 721 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 722 | // This conversion is considered only when there is an |
| 723 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 724 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 725 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 726 | (!StrLit->isWide() && |
| 727 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 728 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 729 | return true; |
| 730 | } |
| 731 | |
| 732 | return false; |
| 733 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 734 | |
| 735 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 736 | /// expression From to the type ToType. Returns true if there was an |
| 737 | /// error, false otherwise. The expression From is replaced with the |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 738 | /// converted expression. Flavor is the kind of conversion we're |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 739 | /// performing, used in the error message. If @p AllowExplicit, |
| 740 | /// explicit user-defined conversions are permitted. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 741 | bool |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 742 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 743 | const char *Flavor, bool AllowExplicit) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 744 | { |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 745 | ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType, false, |
| 746 | AllowExplicit); |
| 747 | return PerformImplicitConversion(From, ToType, ICS, Flavor); |
| 748 | } |
| 749 | |
| 750 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 751 | /// expression From to the type ToType using the pre-computed implicit |
| 752 | /// conversion sequence ICS. Returns true if there was an error, false |
| 753 | /// otherwise. The expression From is replaced with the converted |
| 754 | /// expression. Flavor is the kind of conversion we're performing, |
| 755 | /// used in the error message. |
| 756 | bool |
| 757 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 758 | const ImplicitConversionSequence &ICS, |
| 759 | const char* Flavor) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 760 | switch (ICS.ConversionKind) { |
| 761 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 762 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 763 | return true; |
| 764 | break; |
| 765 | |
| 766 | case ImplicitConversionSequence::UserDefinedConversion: |
| 767 | // FIXME: This is, of course, wrong. We'll need to actually call |
| 768 | // the constructor or conversion operator, and then cope with the |
| 769 | // standard conversions. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 770 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 771 | ToType->isLValueReferenceType()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 772 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 773 | |
| 774 | case ImplicitConversionSequence::EllipsisConversion: |
| 775 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 776 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 777 | |
| 778 | case ImplicitConversionSequence::BadConversion: |
| 779 | return true; |
| 780 | } |
| 781 | |
| 782 | // Everything went well. |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 787 | /// expression From to the type ToType by following the standard |
| 788 | /// conversion sequence SCS. Returns true if there was an error, false |
| 789 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 790 | /// expression. Flavor is the context in which we're performing this |
| 791 | /// conversion, for use in error messages. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 792 | bool |
| 793 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 794 | const StandardConversionSequence& SCS, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 795 | const char *Flavor) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 796 | // Overall FIXME: we are recomputing too many types here and doing |
| 797 | // far too much extra work. What this means is that we need to keep |
| 798 | // track of more information that is computed when we try the |
| 799 | // implicit conversion initially, so that we don't need to recompute |
| 800 | // anything here. |
| 801 | QualType FromType = From->getType(); |
| 802 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 803 | if (SCS.CopyConstructor) { |
| 804 | // FIXME: Create a temporary object by calling the copy |
| 805 | // constructor. |
Douglas Gregor | 66b947f | 2009-01-16 19:38:23 +0000 | [diff] [blame] | 806 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 807 | ToType->isLValueReferenceType()); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 808 | return false; |
| 809 | } |
| 810 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 811 | // Perform the first implicit conversion. |
| 812 | switch (SCS.First) { |
| 813 | case ICK_Identity: |
| 814 | case ICK_Lvalue_To_Rvalue: |
| 815 | // Nothing to do. |
| 816 | break; |
| 817 | |
| 818 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 819 | FromType = Context.getArrayDecayedType(FromType); |
| 820 | ImpCastExprToType(From, FromType); |
| 821 | break; |
| 822 | |
| 823 | case ICK_Function_To_Pointer: |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 824 | if (Context.getCanonicalType(FromType) == Context.OverloadTy) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 825 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 826 | if (!Fn) |
| 827 | return true; |
| 828 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 829 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
| 830 | return true; |
| 831 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 832 | FixOverloadedFunctionReference(From, Fn); |
| 833 | FromType = From->getType(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 834 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 835 | FromType = Context.getPointerType(FromType); |
| 836 | ImpCastExprToType(From, FromType); |
| 837 | break; |
| 838 | |
| 839 | default: |
| 840 | assert(false && "Improper first standard conversion"); |
| 841 | break; |
| 842 | } |
| 843 | |
| 844 | // Perform the second implicit conversion |
| 845 | switch (SCS.Second) { |
| 846 | case ICK_Identity: |
| 847 | // Nothing to do. |
| 848 | break; |
| 849 | |
| 850 | case ICK_Integral_Promotion: |
| 851 | case ICK_Floating_Promotion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 852 | case ICK_Complex_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 853 | case ICK_Integral_Conversion: |
| 854 | case ICK_Floating_Conversion: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 855 | case ICK_Complex_Conversion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 856 | case ICK_Floating_Integral: |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 857 | case ICK_Complex_Real: |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 858 | case ICK_Compatible_Conversion: |
| 859 | // FIXME: Go deeper to get the unqualified type! |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 860 | FromType = ToType.getUnqualifiedType(); |
| 861 | ImpCastExprToType(From, FromType); |
| 862 | break; |
| 863 | |
| 864 | case ICK_Pointer_Conversion: |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 865 | if (SCS.IncompatibleObjC) { |
| 866 | // Diagnose incompatible Objective-C conversions |
| 867 | Diag(From->getSourceRange().getBegin(), |
| 868 | diag::ext_typecheck_convert_incompatible_pointer) |
| 869 | << From->getType() << ToType << Flavor |
| 870 | << From->getSourceRange(); |
| 871 | } |
| 872 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 873 | if (CheckPointerConversion(From, ToType)) |
| 874 | return true; |
| 875 | ImpCastExprToType(From, ToType); |
| 876 | break; |
| 877 | |
| 878 | case ICK_Pointer_Member: |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 879 | if (CheckMemberPointerConversion(From, ToType)) |
| 880 | return true; |
| 881 | ImpCastExprToType(From, ToType); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 882 | break; |
| 883 | |
| 884 | case ICK_Boolean_Conversion: |
| 885 | FromType = Context.BoolTy; |
| 886 | ImpCastExprToType(From, FromType); |
| 887 | break; |
| 888 | |
| 889 | default: |
| 890 | assert(false && "Improper second standard conversion"); |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | switch (SCS.Third) { |
| 895 | case ICK_Identity: |
| 896 | // Nothing to do. |
| 897 | break; |
| 898 | |
| 899 | case ICK_Qualification: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 900 | // FIXME: Not sure about lvalue vs rvalue here in the presence of |
| 901 | // rvalue references. |
Douglas Gregor | 66b947f | 2009-01-16 19:38:23 +0000 | [diff] [blame] | 902 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 903 | ToType->isLValueReferenceType()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 904 | break; |
| 905 | |
| 906 | default: |
| 907 | assert(false && "Improper second standard conversion"); |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | return false; |
| 912 | } |
| 913 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 914 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, |
| 915 | SourceLocation KWLoc, |
| 916 | SourceLocation LParen, |
| 917 | TypeTy *Ty, |
| 918 | SourceLocation RParen) { |
| 919 | // FIXME: Some of the type traits have requirements. Interestingly, only the |
| 920 | // __is_base_of requirement is explicitly stated to be diagnosed. Indeed, |
| 921 | // G++ accepts __is_pod(Incomplete) without complaints, and claims that the |
| 922 | // type is indeed a POD. |
| 923 | |
| 924 | // There is no point in eagerly computing the value. The traits are designed |
| 925 | // to be used from type trait templates, so Ty will be a template parameter |
| 926 | // 99% of the time. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 927 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 928 | QualType::getFromOpaquePtr(Ty), |
| 929 | RParen, Context.BoolTy)); |
| 930 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 931 | |
| 932 | QualType Sema::CheckPointerToMemberOperands( |
| 933 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) |
| 934 | { |
| 935 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 936 | // C++ 5.5p2 |
| 937 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 938 | // be of type "pointer to member of T" (where T is a completely-defined |
| 939 | // class type) [...] |
| 940 | QualType RType = rex->getType(); |
| 941 | const MemberPointerType *MemPtr = RType->getAsMemberPointerType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 942 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 943 | Diag(Loc, diag::err_bad_memptr_rhs) |
| 944 | << OpSpelling << RType << rex->getSourceRange(); |
| 945 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 946 | } else if (RequireCompleteType(Loc, QualType(MemPtr->getClass(), 0), |
| 947 | diag::err_memptr_rhs_incomplete, |
| 948 | rex->getSourceRange())) |
| 949 | return QualType(); |
| 950 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 951 | QualType Class(MemPtr->getClass(), 0); |
| 952 | |
| 953 | // C++ 5.5p2 |
| 954 | // [...] to its first operand, which shall be of class T or of a class of |
| 955 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 956 | // such a class] |
| 957 | QualType LType = lex->getType(); |
| 958 | if (isIndirect) { |
| 959 | if (const PointerType *Ptr = LType->getAsPointerType()) |
| 960 | LType = Ptr->getPointeeType().getNonReferenceType(); |
| 961 | else { |
| 962 | Diag(Loc, diag::err_bad_memptr_lhs) |
| 963 | << OpSpelling << 1 << LType << lex->getSourceRange(); |
| 964 | return QualType(); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if (Context.getCanonicalType(Class).getUnqualifiedType() != |
| 969 | Context.getCanonicalType(LType).getUnqualifiedType()) { |
| 970 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 971 | /*DetectVirtual=*/false); |
| 972 | // FIXME: Would it be useful to print full ambiguity paths, |
| 973 | // or is that overkill? |
| 974 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 975 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 976 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
| 977 | << (int)isIndirect << lex->getType() << lex->getSourceRange(); |
| 978 | return QualType(); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | // C++ 5.5p2 |
| 983 | // The result is an object or a function of the type specified by the |
| 984 | // second operand. |
| 985 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 986 | // in accordance with 5.5p5 and 5.2.5. |
| 987 | // FIXME: This returns a dereferenced member function pointer as a normal |
| 988 | // function type. However, the only operation valid on such functions is |
| 989 | // calling them. There's also a GCC extension to get a function pointer to |
| 990 | // the thing, which is another complication, because this type - unlike the |
| 991 | // type that is the result of this expression - takes the class as the first |
| 992 | // argument. |
| 993 | // We probably need a "MemberFunctionClosureType" or something like that. |
| 994 | QualType Result = MemPtr->getPointeeType(); |
| 995 | if (LType.isConstQualified()) |
| 996 | Result.addConst(); |
| 997 | if (LType.isVolatileQualified()) |
| 998 | Result.addVolatile(); |
| 999 | return Result; |
| 1000 | } |