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" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 22 | /// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 23 | /// name (e.g., operator void const *) as an expression. This is |
| 24 | /// very similar to ActOnIdentifierExpr, except that instead of |
| 25 | /// providing an identifier the parser provides the type of the |
| 26 | /// conversion function. |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 27 | Sema::ExprResult |
| 28 | Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc, |
| 29 | TypeTy *Ty, bool HasTrailingLParen, |
| 30 | const CXXScopeSpec &SS) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 31 | QualType ConvType = QualType::getFromOpaquePtr(Ty); |
| 32 | QualType ConvTypeCanon = Context.getCanonicalType(ConvType); |
| 33 | DeclarationName ConvName |
| 34 | = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 35 | return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen, |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 36 | &SS); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 37 | } |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 39 | /// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 40 | /// name (e.g., @c operator+ ) as an expression. This is very |
| 41 | /// similar to ActOnIdentifierExpr, except that instead of providing |
| 42 | /// an identifier the parser provides the kind of overloaded |
| 43 | /// operator that was parsed. |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 44 | Sema::ExprResult |
| 45 | Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc, |
| 46 | OverloadedOperatorKind Op, |
| 47 | bool HasTrailingLParen, |
| 48 | const CXXScopeSpec &SS) { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 49 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 50 | return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS); |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 53 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
| 54 | Action::ExprResult |
| 55 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 56 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
| 57 | const NamespaceDecl *StdNs = GetStdNamespace(); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 58 | if (!StdNs) |
| 59 | return Diag(OpLoc, diag::err_need_header_before_typeid); |
| 60 | |
| 61 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 62 | Decl *TypeInfoDecl = LookupDecl(TypeInfoII, |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 63 | Decl::IDNS_Tag | Decl::IDNS_Ordinary, |
| 64 | 0, StdNs, /*createBuiltins=*/false); |
| 65 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 66 | if (!TypeInfoRecordDecl) |
| 67 | return Diag(OpLoc, diag::err_need_header_before_typeid); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 68 | |
| 69 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 70 | |
| 71 | return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(), |
| 72 | SourceRange(OpLoc, RParenLoc)); |
| 73 | } |
| 74 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 75 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 77 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 78 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 80 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 82 | |
| 83 | /// ActOnCXXThrow - Parse throw expressions. |
| 84 | Action::ExprResult |
| 85 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 86 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 87 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 88 | |
| 89 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 90 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 91 | /// is a non-lvalue expression whose value is the address of the object for |
| 92 | /// which the function is called. |
| 93 | |
| 94 | if (!isa<FunctionDecl>(CurContext)) { |
| 95 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 96 | return ExprResult(true); |
| 97 | } |
| 98 | |
| 99 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 100 | if (MD->isInstance()) |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 101 | return new CXXThisExpr(ThisLoc, MD->getThisType(Context)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 102 | |
| 103 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 104 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 105 | |
| 106 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 107 | /// Can be interpreted either as function-style casting ("int(x)") |
| 108 | /// or class type construction ("ClassType(x,y,z)") |
| 109 | /// or creation of a value-initialized type ("int()"). |
| 110 | Action::ExprResult |
| 111 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 112 | SourceLocation LParenLoc, |
| 113 | ExprTy **ExprTys, unsigned NumExprs, |
| 114 | SourceLocation *CommaLocs, |
| 115 | SourceLocation RParenLoc) { |
| 116 | assert(TypeRep && "Missing type!"); |
| 117 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
| 118 | Expr **Exprs = (Expr**)ExprTys; |
| 119 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 120 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 121 | |
| 122 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 123 | // C++ 5.2.3p1: |
| 124 | // If the simple-type-specifier specifies a class type, the class type shall |
| 125 | // be complete. |
| 126 | // |
| 127 | if (!RT->getDecl()->isDefinition()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 128 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use) |
| 129 | << Ty.getAsString() << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 130 | |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 131 | unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 132 | "class constructors are not supported yet"); |
| 133 | return Diag(TyBeginLoc, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // C++ 5.2.3p1: |
| 137 | // If the expression list is a single expression, the type conversion |
| 138 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 139 | // corresponding cast expression. |
| 140 | // |
| 141 | if (NumExprs == 1) { |
| 142 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
| 143 | return true; |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 144 | return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc, |
| 145 | Exprs[0], RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // C++ 5.2.3p1: |
| 149 | // If the expression list specifies more than a single value, the type shall |
| 150 | // be a class with a suitably declared constructor. |
| 151 | // |
| 152 | if (NumExprs > 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 153 | return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg) |
| 154 | << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 155 | |
| 156 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 157 | |
| 158 | // C++ 5.2.3p2: |
| 159 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 160 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 161 | // rvalue of the specified type, which is value-initialized. |
| 162 | // |
| 163 | if (Ty->isArrayType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 164 | return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 165 | if (Ty->isIncompleteType() && !Ty->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 166 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use) |
| 167 | << Ty.getAsString() << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 168 | |
| 169 | return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); |
| 170 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 171 | |
| 172 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame^] | 173 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 174 | /// @code new (memory) int[size][4] @endcode |
| 175 | /// or |
| 176 | /// @code ::new Foo(23, "hello") @endcode |
| 177 | /// For the interpretation of this heap of arguments, consult the base version. |
| 178 | Action::ExprResult |
| 179 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 180 | SourceLocation PlacementLParen, |
| 181 | ExprTy **PlacementArgs, unsigned NumPlaceArgs, |
| 182 | SourceLocation PlacementRParen, bool ParenTypeId, |
| 183 | SourceLocation TyStart, TypeTy *Ty, SourceLocation TyEnd, |
| 184 | SourceLocation ConstructorLParen, |
| 185 | ExprTy **ConstructorArgs, unsigned NumConsArgs, |
| 186 | SourceLocation ConstructorRParen) |
| 187 | { |
| 188 | QualType AllocType = QualType::getFromOpaquePtr(Ty); |
| 189 | QualType CheckType = AllocType; |
| 190 | // To leverage the existing parser as much as possible, array types are |
| 191 | // parsed as VLAs. Unwrap for checking. |
| 192 | if (const VariableArrayType *VLA = Context.getAsVariableArrayType(AllocType)){ |
| 193 | CheckType = VLA->getElementType(); |
| 194 | } |
| 195 | |
| 196 | // Validate the type, and unwrap an array if any. |
| 197 | if (CheckAllocatedType(CheckType, StartLoc, SourceRange(TyStart, TyEnd))) |
| 198 | return true; |
| 199 | |
| 200 | QualType ResultType = Context.getPointerType(CheckType); |
| 201 | |
| 202 | // That every array dimension except the first is constant was already |
| 203 | // checked by the type check above. |
| 204 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 205 | // or enumeration type with a non-negative value." |
| 206 | // This was checked by ActOnTypeName, since C99 has the same restriction on |
| 207 | // VLA expressions. |
| 208 | |
| 209 | // --- Choosing an allocation function --- |
| 210 | // C++ 5.3.4p8 - 14 & 18 |
| 211 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 212 | // in the scope of the allocated class. |
| 213 | // 2) If an array size is given, look for operator new[], else look for |
| 214 | // operator new. |
| 215 | // 3) The first argument is always size_t. Append the arguments from the |
| 216 | // placement form. |
| 217 | // FIXME: Find the correct overload of operator new. |
| 218 | // FIXME: Also find the corresponding overload of operator delete. |
| 219 | FunctionDecl *OperatorNew = 0; |
| 220 | FunctionDecl *OperatorDelete = 0; |
| 221 | Expr **PlaceArgs = (Expr**)PlacementArgs; |
| 222 | |
| 223 | bool Init = ConstructorLParen.isValid(); |
| 224 | // --- Choosing a constructor --- |
| 225 | // C++ 5.3.4p15 |
| 226 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) |
| 227 | // the object is not initialized. If the object, or any part of it, is |
| 228 | // const-qualified, it's an error. |
| 229 | // 2) If T is a POD and there's an empty initializer, the object is value- |
| 230 | // initialized. |
| 231 | // 3) If T is a POD and there's one initializer argument, the object is copy- |
| 232 | // constructed. |
| 233 | // 4) If T is a POD and there's more initializer arguments, it's an error. |
| 234 | // 5) If T is not a POD, the initializer arguments are used as constructor |
| 235 | // arguments. |
| 236 | // |
| 237 | // Or by the C++0x formulation: |
| 238 | // 1) If there's no initializer, the object is default-initialized according |
| 239 | // to C++0x rules. |
| 240 | // 2) Otherwise, the object is direct-initialized. |
| 241 | CXXConstructorDecl *Constructor = 0; |
| 242 | Expr **ConsArgs = (Expr**)ConstructorArgs; |
| 243 | if (CheckType->isRecordType()) { |
| 244 | // FIXME: This is incorrect for when there is an empty initializer and |
| 245 | // no user-defined constructor. Must zero-initialize, not default-construct. |
| 246 | Constructor = PerformInitializationByConstructor( |
| 247 | CheckType, ConsArgs, NumConsArgs, |
| 248 | TyStart, SourceRange(TyStart, ConstructorRParen), |
| 249 | CheckType.getAsString(), |
| 250 | NumConsArgs != 0 ? IK_Direct : IK_Default); |
| 251 | if (!Constructor) |
| 252 | return true; |
| 253 | } else { |
| 254 | if (!Init) { |
| 255 | // FIXME: Check that no subpart is const. |
| 256 | if (CheckType.isConstQualified()) { |
| 257 | Diag(StartLoc, diag::err_new_uninitialized_const) |
| 258 | << SourceRange(StartLoc, TyEnd); |
| 259 | return true; |
| 260 | } |
| 261 | } else if (NumConsArgs == 0) { |
| 262 | // Object is value-initialized. Do nothing. |
| 263 | } else if (NumConsArgs == 1) { |
| 264 | // Object is direct-initialized. |
| 265 | if (CheckInitializerTypes(ConsArgs[0], CheckType, StartLoc, |
| 266 | CheckType.getAsString())) |
| 267 | return true; |
| 268 | } else { |
| 269 | Diag(StartLoc, diag::err_builtin_direct_init_more_than_one_arg) |
| 270 | << SourceRange(ConstructorLParen, ConstructorRParen); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
| 275 | |
| 276 | return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs, |
| 277 | ParenTypeId, AllocType, Constructor, Init, |
| 278 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, |
| 279 | StartLoc, Init ? ConstructorRParen : TyEnd); |
| 280 | } |
| 281 | |
| 282 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 283 | /// in a new-expression. |
| 284 | /// dimension off and stores the size expression in ArraySize. |
| 285 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation StartLoc, |
| 286 | const SourceRange &TyR) |
| 287 | { |
| 288 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 289 | // abstract class type or array thereof. |
| 290 | // FIXME: We don't have abstract types yet. |
| 291 | // FIXME: Under C++ semantics, an incomplete object type is still an object |
| 292 | // type. This code assumes the C semantics, where it's not. |
| 293 | if (!AllocType->isObjectType()) { |
| 294 | diag::kind msg; |
| 295 | if (AllocType->isFunctionType()) { |
| 296 | msg = diag::err_new_function; |
| 297 | } else if(AllocType->isIncompleteType()) { |
| 298 | msg = diag::err_new_incomplete; |
| 299 | } else if(AllocType->isReferenceType()) { |
| 300 | msg = diag::err_new_reference; |
| 301 | } else { |
| 302 | assert(false && "Unexpected type class"); |
| 303 | return true; |
| 304 | } |
| 305 | Diag(StartLoc, msg) << AllocType.getAsString() << TyR; |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | // Every dimension beyond the first shall be of constant size. |
| 310 | while (const ArrayType *Array = Context.getAsArrayType(AllocType)) { |
| 311 | if (!Array->isConstantArrayType()) { |
| 312 | // FIXME: Might be nice to get a better source range from somewhere. |
| 313 | Diag(StartLoc, diag::err_new_array_nonconst) << TyR; |
| 314 | return true; |
| 315 | } |
| 316 | AllocType = Array->getElementType(); |
| 317 | } |
| 318 | |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 323 | /// @code ::delete ptr; @endcode |
| 324 | /// or |
| 325 | /// @code delete [] ptr; @endcode |
| 326 | Action::ExprResult |
| 327 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
| 328 | bool ArrayForm, ExprTy *Operand) |
| 329 | { |
| 330 | // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type |
| 331 | // having a single conversion function to a pointer type. The result has |
| 332 | // type void." |
| 333 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 334 | |
| 335 | Expr *Ex = (Expr *)Operand; |
| 336 | QualType Type = Ex->getType(); |
| 337 | |
| 338 | if (Type->isRecordType()) { |
| 339 | // FIXME: Find that one conversion function and amend the type. |
| 340 | } |
| 341 | |
| 342 | if (!Type->isPointerType()) { |
| 343 | Diag(StartLoc, diag::err_delete_operand) |
| 344 | << Type.getAsString() << Ex->getSourceRange(); |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | QualType Pointee = Type->getAsPointerType()->getPointeeType(); |
| 349 | if (Pointee->isIncompleteType() && !Pointee->isVoidType()) |
| 350 | Diag(StartLoc, diag::warn_delete_incomplete) |
| 351 | << Pointee.getAsString() << Ex->getSourceRange(); |
| 352 | else if (!Pointee->isObjectType()) { |
| 353 | Diag(StartLoc, diag::err_delete_operand) |
| 354 | << Type.getAsString() << Ex->getSourceRange(); |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | // FIXME: Look up the correct operator delete overload and pass a pointer |
| 359 | // along. |
| 360 | // FIXME: Check access and ambiguity of operator delete and destructor. |
| 361 | |
| 362 | return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex, |
| 363 | StartLoc); |
| 364 | } |
| 365 | |
| 366 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 367 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 368 | /// C++ if/switch/while/for statement. |
| 369 | /// e.g: "if (int x = f()) {...}" |
| 370 | Action::ExprResult |
| 371 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 372 | Declarator &D, |
| 373 | SourceLocation EqualLoc, |
| 374 | ExprTy *AssignExprVal) { |
| 375 | assert(AssignExprVal && "Null assignment expression"); |
| 376 | |
| 377 | // C++ 6.4p2: |
| 378 | // The declarator shall not specify a function or an array. |
| 379 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 380 | // new class or enumeration. |
| 381 | |
| 382 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 383 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 384 | |
| 385 | QualType Ty = GetTypeForDeclarator(D, S); |
| 386 | |
| 387 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 388 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 389 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 390 | return Diag(StartLoc, diag::err_invalid_use_of_function_type) |
| 391 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 392 | } else if (Ty->isArrayType()) { // ...or an array. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 393 | Diag(StartLoc, diag::err_invalid_use_of_array_type) |
| 394 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 395 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 396 | RecordDecl *RD = RT->getDecl(); |
| 397 | // The type-specifier-seq shall not declare a new class... |
| 398 | if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD))) |
| 399 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 400 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 401 | EnumDecl *ED = ET->getDecl(); |
| 402 | // ...or enumeration. |
| 403 | if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED))) |
| 404 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 405 | } |
| 406 | |
| 407 | DeclTy *Dcl = ActOnDeclarator(S, D, 0); |
| 408 | if (!Dcl) |
| 409 | return true; |
| 410 | AddInitializerToDecl(Dcl, AssignExprVal); |
| 411 | |
| 412 | return new CXXConditionDeclExpr(StartLoc, EqualLoc, |
| 413 | cast<VarDecl>(static_cast<Decl *>(Dcl))); |
| 414 | } |
| 415 | |
| 416 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 417 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 418 | // C++ 6.4p4: |
| 419 | // The value of a condition that is an initialized declaration in a statement |
| 420 | // other than a switch statement is the value of the declared variable |
| 421 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 422 | // program is ill-formed. |
| 423 | // The value of a condition that is an expression is the value of the |
| 424 | // expression, implicitly converted to bool. |
| 425 | // |
| 426 | QualType Ty = CondExpr->getType(); // Save the type. |
| 427 | AssignConvertType |
| 428 | ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr); |
| 429 | if (ConvTy == Incompatible) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 430 | return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition) |
| 431 | << Ty.getAsString() << CondExpr->getSourceRange(); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 432 | return false; |
| 433 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 434 | |
| 435 | /// Helper function to determine whether this is the (deprecated) C++ |
| 436 | /// conversion from a string literal to a pointer to non-const char or |
| 437 | /// non-const wchar_t (for narrow and wide string literals, |
| 438 | /// respectively). |
| 439 | bool |
| 440 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 441 | // Look inside the implicit cast, if it exists. |
| 442 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 443 | From = Cast->getSubExpr(); |
| 444 | |
| 445 | // A string literal (2.13.4) that is not a wide string literal can |
| 446 | // be converted to an rvalue of type "pointer to char"; a wide |
| 447 | // string literal can be converted to an rvalue of type "pointer |
| 448 | // to wchar_t" (C++ 4.2p2). |
| 449 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 450 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 451 | if (const BuiltinType *ToPointeeType |
| 452 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 453 | // This conversion is considered only when there is an |
| 454 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 455 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 456 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 457 | (!StrLit->isWide() && |
| 458 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 459 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | return false; |
| 464 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 465 | |
| 466 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 467 | /// expression From to the type ToType. Returns true if there was an |
| 468 | /// error, false otherwise. The expression From is replaced with the |
| 469 | /// converted expression. |
| 470 | bool |
| 471 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType) |
| 472 | { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 473 | ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 474 | switch (ICS.ConversionKind) { |
| 475 | case ImplicitConversionSequence::StandardConversion: |
| 476 | if (PerformImplicitConversion(From, ToType, ICS.Standard)) |
| 477 | return true; |
| 478 | break; |
| 479 | |
| 480 | case ImplicitConversionSequence::UserDefinedConversion: |
| 481 | // FIXME: This is, of course, wrong. We'll need to actually call |
| 482 | // the constructor or conversion operator, and then cope with the |
| 483 | // standard conversions. |
| 484 | ImpCastExprToType(From, ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 485 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 486 | |
| 487 | case ImplicitConversionSequence::EllipsisConversion: |
| 488 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 489 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 490 | |
| 491 | case ImplicitConversionSequence::BadConversion: |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | // Everything went well. |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 500 | /// expression From to the type ToType by following the standard |
| 501 | /// conversion sequence SCS. Returns true if there was an error, false |
| 502 | /// otherwise. The expression From is replaced with the converted |
| 503 | /// expression. |
| 504 | bool |
| 505 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 506 | const StandardConversionSequence& SCS) |
| 507 | { |
| 508 | // Overall FIXME: we are recomputing too many types here and doing |
| 509 | // far too much extra work. What this means is that we need to keep |
| 510 | // track of more information that is computed when we try the |
| 511 | // implicit conversion initially, so that we don't need to recompute |
| 512 | // anything here. |
| 513 | QualType FromType = From->getType(); |
| 514 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 515 | if (SCS.CopyConstructor) { |
| 516 | // FIXME: Create a temporary object by calling the copy |
| 517 | // constructor. |
| 518 | ImpCastExprToType(From, ToType); |
| 519 | return false; |
| 520 | } |
| 521 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 522 | // Perform the first implicit conversion. |
| 523 | switch (SCS.First) { |
| 524 | case ICK_Identity: |
| 525 | case ICK_Lvalue_To_Rvalue: |
| 526 | // Nothing to do. |
| 527 | break; |
| 528 | |
| 529 | case ICK_Array_To_Pointer: |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 530 | if (FromType->isOverloadType()) { |
| 531 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 532 | if (!Fn) |
| 533 | return true; |
| 534 | |
| 535 | FixOverloadedFunctionReference(From, Fn); |
| 536 | FromType = From->getType(); |
| 537 | } else { |
| 538 | FromType = Context.getArrayDecayedType(FromType); |
| 539 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 540 | ImpCastExprToType(From, FromType); |
| 541 | break; |
| 542 | |
| 543 | case ICK_Function_To_Pointer: |
| 544 | FromType = Context.getPointerType(FromType); |
| 545 | ImpCastExprToType(From, FromType); |
| 546 | break; |
| 547 | |
| 548 | default: |
| 549 | assert(false && "Improper first standard conversion"); |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | // Perform the second implicit conversion |
| 554 | switch (SCS.Second) { |
| 555 | case ICK_Identity: |
| 556 | // Nothing to do. |
| 557 | break; |
| 558 | |
| 559 | case ICK_Integral_Promotion: |
| 560 | case ICK_Floating_Promotion: |
| 561 | case ICK_Integral_Conversion: |
| 562 | case ICK_Floating_Conversion: |
| 563 | case ICK_Floating_Integral: |
| 564 | FromType = ToType.getUnqualifiedType(); |
| 565 | ImpCastExprToType(From, FromType); |
| 566 | break; |
| 567 | |
| 568 | case ICK_Pointer_Conversion: |
| 569 | if (CheckPointerConversion(From, ToType)) |
| 570 | return true; |
| 571 | ImpCastExprToType(From, ToType); |
| 572 | break; |
| 573 | |
| 574 | case ICK_Pointer_Member: |
| 575 | // FIXME: Implement pointer-to-member conversions. |
| 576 | assert(false && "Pointer-to-member conversions are unsupported"); |
| 577 | break; |
| 578 | |
| 579 | case ICK_Boolean_Conversion: |
| 580 | FromType = Context.BoolTy; |
| 581 | ImpCastExprToType(From, FromType); |
| 582 | break; |
| 583 | |
| 584 | default: |
| 585 | assert(false && "Improper second standard conversion"); |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | switch (SCS.Third) { |
| 590 | case ICK_Identity: |
| 591 | // Nothing to do. |
| 592 | break; |
| 593 | |
| 594 | case ICK_Qualification: |
| 595 | ImpCastExprToType(From, ToType); |
| 596 | break; |
| 597 | |
| 598 | default: |
| 599 | assert(false && "Improper second standard conversion"); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | return false; |
| 604 | } |
| 605 | |