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