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