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" |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/Support/Debug.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 24 | /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | Action::ExprResult |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 26 | Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, |
| 27 | SourceLocation LAngleBracketLoc, TypeTy *Ty, |
| 28 | SourceLocation RAngleBracketLoc, |
| 29 | SourceLocation LParenLoc, ExprTy *E, |
| 30 | SourceLocation RParenLoc) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 31 | Expr *Ex = (Expr*)E; |
| 32 | QualType DestType = QualType::getFromOpaquePtr(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | |
| 34 | switch (Kind) { |
| 35 | default: assert(0 && "Unknown C++ cast!"); |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 37 | case tok::kw_const_cast: |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 38 | CheckConstCast(OpLoc, Ex, DestType); |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 39 | return new CXXConstCastExpr(DestType.getNonReferenceType(), Ex, |
| 40 | DestType, OpLoc); |
| 41 | |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 42 | case tok::kw_dynamic_cast: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 43 | return new CXXDynamicCastExpr(DestType.getNonReferenceType(), Ex, |
| 44 | DestType, OpLoc); |
| 45 | |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 46 | case tok::kw_reinterpret_cast: |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 47 | CheckReinterpretCast(OpLoc, Ex, DestType); |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 48 | return new CXXReinterpretCastExpr(DestType.getNonReferenceType(), Ex, |
| 49 | DestType, OpLoc); |
| 50 | |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 51 | case tok::kw_static_cast: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 52 | return new CXXStaticCastExpr(DestType.getNonReferenceType(), Ex, |
| 53 | DestType, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 56 | return true; |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid. |
| 60 | /// Refer to C++ 5.2.11 for details. const_cast is typically used in code |
| 61 | /// like this: |
| 62 | /// const char *str = "literal"; |
| 63 | /// legacy_function(const_cast\<char*\>(str)); |
| 64 | void |
| 65 | Sema::CheckConstCast(SourceLocation OpLoc, Expr *&SrcExpr, QualType DestType) |
| 66 | { |
| 67 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); |
| 68 | |
| 69 | DestType = Context.getCanonicalType(DestType); |
| 70 | QualType SrcType = SrcExpr->getType(); |
| 71 | if (const ReferenceType *DestTypeTmp = DestType->getAsReferenceType()) { |
| 72 | if (SrcExpr->isLvalue(Context) != Expr::LV_Valid) { |
| 73 | // Cannot cast non-lvalue to reference type. |
| 74 | Diag(OpLoc, diag::err_bad_cxx_cast_rvalue, |
| 75 | "const_cast", OrigDestType.getAsString()); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2 |
| 80 | // [...] if a pointer to T1 can be [cast] to the type pointer to T2. |
| 81 | DestType = Context.getPointerType(DestTypeTmp->getPointeeType()); |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 82 | SrcType = Context.getPointerType(SrcType); |
| 83 | } else { |
| 84 | // C++ 5.2.11p1: Otherwise, the result is an rvalue and the |
| 85 | // lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 86 | // conversions are performed on the expression. |
| 87 | DefaultFunctionArrayConversion(SrcExpr); |
| 88 | SrcType = SrcExpr->getType(); |
| 89 | } |
| 90 | |
| 91 | if (!DestType->isPointerType()) { |
| 92 | // Cannot cast to non-pointer, non-reference type. Note that, if DestType |
| 93 | // was a reference type, we converted it to a pointer above. |
| 94 | // C++ 5.2.11p3: For two pointer types [...] |
| 95 | Diag(OpLoc, diag::err_bad_const_cast_dest, OrigDestType.getAsString()); |
| 96 | return; |
| 97 | } |
| 98 | if (DestType->isFunctionPointerType()) { |
| 99 | // Cannot cast direct function pointers. |
| 100 | // C++ 5.2.11p2: [...] where T is any object type or the void type [...] |
| 101 | // T is the ultimate pointee of source and target type. |
| 102 | Diag(OpLoc, diag::err_bad_const_cast_dest, OrigDestType.getAsString()); |
| 103 | return; |
| 104 | } |
| 105 | SrcType = Context.getCanonicalType(SrcType); |
| 106 | |
| 107 | // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are |
| 108 | // completely equal. |
| 109 | // FIXME: const_cast should probably not be able to convert between pointers |
| 110 | // to different address spaces. |
| 111 | // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers |
| 112 | // in multi-level pointers may change, but the level count must be the same, |
| 113 | // as must be the final pointee type. |
| 114 | while (SrcType != DestType && UnwrapSimilarPointerTypes(SrcType, DestType)) { |
| 115 | SrcType = SrcType.getUnqualifiedType(); |
| 116 | DestType = DestType.getUnqualifiedType(); |
| 117 | } |
| 118 | |
| 119 | // Doug Gregor said to disallow this until users complain. |
| 120 | #if 0 |
| 121 | // If we end up with constant arrays of equal size, unwrap those too. A cast |
| 122 | // from const int [N] to int (&)[N] is invalid by my reading of the |
| 123 | // standard, but g++ accepts it even with -ansi -pedantic. |
| 124 | // No more than one level, though, so don't embed this in the unwrap loop |
| 125 | // above. |
| 126 | const ConstantArrayType *SrcTypeArr, *DestTypeArr; |
| 127 | if ((SrcTypeArr = Context.getAsConstantArrayType(SrcType)) && |
| 128 | (DestTypeArr = Context.getAsConstantArrayType(DestType))) |
| 129 | { |
| 130 | if (SrcTypeArr->getSize() != DestTypeArr->getSize()) { |
| 131 | // Different array sizes. |
| 132 | Diag(OpLoc, diag::err_bad_cxx_cast_generic, "const_cast", |
| 133 | OrigDestType.getAsString(), OrigSrcType.getAsString()); |
| 134 | return; |
| 135 | } |
| 136 | SrcType = SrcTypeArr->getElementType().getUnqualifiedType(); |
| 137 | DestType = DestTypeArr->getElementType().getUnqualifiedType(); |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | // Since we're dealing in canonical types, the remainder must be the same. |
| 142 | if (SrcType != DestType) { |
| 143 | // Cast between unrelated types. |
| 144 | Diag(OpLoc, diag::err_bad_cxx_cast_generic, "const_cast", |
| 145 | OrigDestType.getAsString(), OrigSrcType.getAsString()); |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is |
| 151 | /// valid. |
| 152 | /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code |
| 153 | /// like this: |
| 154 | /// char *bytes = reinterpret_cast\<char*\>(int_ptr); |
| 155 | void |
| 156 | Sema::CheckReinterpretCast(SourceLocation OpLoc, Expr *&SrcExpr, |
| 157 | QualType DestType) |
| 158 | { |
| 159 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); |
| 160 | |
| 161 | DestType = Context.getCanonicalType(DestType); |
| 162 | QualType SrcType = SrcExpr->getType(); |
| 163 | if (const ReferenceType *DestTypeTmp = DestType->getAsReferenceType()) { |
| 164 | if (SrcExpr->isLvalue(Context) != Expr::LV_Valid) { |
| 165 | // Cannot cast non-lvalue to reference type. |
| 166 | Diag(OpLoc, diag::err_bad_cxx_cast_rvalue, |
| 167 | "reinterpret_cast", OrigDestType.getAsString()); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the |
| 172 | // same effect as the conversion *reinterpret_cast<T*>(&x) with the |
| 173 | // built-in & and * operators. |
| 174 | // This code does this transformation for the checked types. |
| 175 | DestType = Context.getPointerType(DestTypeTmp->getPointeeType()); |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 176 | SrcType = Context.getPointerType(SrcType); |
| 177 | } else { |
| 178 | // C++ 5.2.10p1: [...] the lvalue-to-rvalue, array-to-pointer, and |
| 179 | // function-to-pointer standard conversions are performed on the |
| 180 | // expression v. |
| 181 | DefaultFunctionArrayConversion(SrcExpr); |
| 182 | SrcType = SrcExpr->getType(); |
| 183 | } |
| 184 | |
| 185 | // Canonicalize source for comparison. |
| 186 | SrcType = Context.getCanonicalType(SrcType); |
| 187 | |
| 188 | bool destIsPtr = DestType->isPointerType(); |
| 189 | bool srcIsPtr = SrcType->isPointerType(); |
| 190 | if (!destIsPtr && !srcIsPtr) { |
| 191 | // Except for std::nullptr_t->integer, which is not supported yet, and |
| 192 | // lvalue->reference, which is handled above, at least one of the two |
| 193 | // arguments must be a pointer. |
| 194 | Diag(OpLoc, diag::err_bad_cxx_cast_generic, "reinterpret_cast", |
| 195 | OrigDestType.getAsString(), OrigSrcType.getAsString()); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (SrcType == DestType) { |
| 200 | // C++ 5.2.10p2 has a note that mentions that, subject to all other |
| 201 | // restrictions, a cast to the same type is allowed. The intent is not |
| 202 | // entirely clear here, since all other paragraphs explicitly forbid casts |
| 203 | // to the same type. However, the behavior of compilers is pretty consistent |
| 204 | // on this point: allow same-type conversion if the involved are pointers, |
| 205 | // disallow otherwise. |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Note: Clang treats enumeration types as integral types. If this is ever |
| 210 | // changed for C++, the additional check here will be redundant. |
| 211 | if (DestType->isIntegralType() && !DestType->isEnumeralType()) { |
| 212 | assert(srcIsPtr); |
| 213 | // C++ 5.2.10p4: A pointer can be explicitly converted to any integral |
| 214 | // type large enough to hold it. |
| 215 | if (Context.getTypeSize(SrcType) > Context.getTypeSize(DestType)) { |
| 216 | Diag(OpLoc, diag::err_bad_reinterpret_cast_small_int, |
| 217 | OrigDestType.getAsString()); |
| 218 | } |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | if (SrcType->isIntegralType() || SrcType->isEnumeralType()) { |
| 223 | assert(destIsPtr); |
| 224 | // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly |
| 225 | // converted to a pointer. |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | if (!destIsPtr || !srcIsPtr) { |
| 230 | // With the valid non-pointer conversions out of the way, we can be even |
| 231 | // more stringent. |
| 232 | Diag(OpLoc, diag::err_bad_cxx_cast_generic, "reinterpret_cast", |
| 233 | OrigDestType.getAsString(), OrigSrcType.getAsString()); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness. |
| 238 | if (CastsAwayConstness(SrcType, DestType)) { |
| 239 | Diag(OpLoc, diag::err_bad_cxx_cast_const_away, "reinterpret_cast", |
| 240 | OrigDestType.getAsString(), OrigSrcType.getAsString()); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | // Not casting away constness, so the only remaining check is for compatible |
| 245 | // pointer categories. |
| 246 | |
| 247 | if (SrcType->isFunctionPointerType()) { |
| 248 | if (DestType->isFunctionPointerType()) { |
| 249 | // C++ 5.2.10p6: A pointer to a function can be explicitly converted to |
| 250 | // a pointer to a function of a different type. |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // FIXME: Handle member pointers. |
| 255 | |
| 256 | // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to |
| 257 | // an object type or vice versa is conditionally-supported. |
| 258 | // Compilers support it in C++03 too, though, because it's necessary for |
| 259 | // casting the return value of dlsym() and GetProcAddress(). |
| 260 | // FIXME: Conditionally-supported behavior should be configurable in the |
| 261 | // TargetInfo or similar. |
| 262 | if (!getLangOptions().CPlusPlus0x) { |
| 263 | Diag(OpLoc, diag::ext_reinterpret_cast_fn_obj); |
| 264 | } |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // FIXME: Handle member pointers. |
| 269 | |
| 270 | if (DestType->isFunctionPointerType()) { |
| 271 | // See above. |
| 272 | if (!getLangOptions().CPlusPlus0x) { |
| 273 | Diag(OpLoc, diag::ext_reinterpret_cast_fn_obj); |
| 274 | } |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // C++ 5.2.10p7: A pointer to an object can be explicitly converted to |
| 279 | // a pointer to an object of different type. |
| 280 | // Void pointers are not specified, but supported by every compiler out there. |
| 281 | // So we finish by allowing everything that remains - it's got to be two |
| 282 | // object pointers. |
| 283 | } |
| 284 | |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 285 | /// CastsAwayConstness - Check if the pointer conversion from SrcType |
| 286 | /// to DestType casts away constness as defined in C++ |
| 287 | /// 5.2.11p8ff. This is used by the cast checkers. Both arguments |
| 288 | /// must denote pointer types. |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 289 | bool |
| 290 | Sema::CastsAwayConstness(QualType SrcType, QualType DestType) |
| 291 | { |
| 292 | // Casting away constness is defined in C++ 5.2.11p8 with reference to |
| 293 | // C++ 4.4. |
| 294 | // We piggyback on Sema::IsQualificationConversion for this, since the rules |
| 295 | // are non-trivial. So first we construct Tcv *...cv* as described in |
| 296 | // C++ 5.2.11p8. |
| 297 | SrcType = Context.getCanonicalType(SrcType); |
| 298 | DestType = Context.getCanonicalType(DestType); |
| 299 | |
| 300 | QualType UnwrappedSrcType = SrcType, UnwrappedDestType = DestType; |
| 301 | llvm::SmallVector<unsigned, 8> cv1, cv2; |
| 302 | |
| 303 | // Find the qualifications. |
| 304 | while (UnwrapSimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) { |
| 305 | cv1.push_back(UnwrappedSrcType.getCVRQualifiers()); |
| 306 | cv2.push_back(UnwrappedDestType.getCVRQualifiers()); |
| 307 | } |
| 308 | assert(cv1.size() > 0 && "Must have at least one pointer level."); |
| 309 | |
| 310 | // Construct void pointers with those qualifiers (in reverse order of |
| 311 | // unwrapping, of course). |
| 312 | QualType SrcConstruct = Context.VoidTy; |
| 313 | QualType DestConstruct = Context.VoidTy; |
| 314 | for (llvm::SmallVector<unsigned, 8>::reverse_iterator i1 = cv1.rbegin(), |
| 315 | i2 = cv2.rbegin(); |
| 316 | i1 != cv1.rend(); ++i1, ++i2) |
| 317 | { |
| 318 | SrcConstruct = Context.getPointerType(SrcConstruct.getQualifiedType(*i1)); |
| 319 | DestConstruct = Context.getPointerType(DestConstruct.getQualifiedType(*i2)); |
| 320 | } |
| 321 | |
| 322 | // Test if they're compatible. |
| 323 | return SrcConstruct != DestConstruct && |
| 324 | !IsQualificationConversion(SrcConstruct, DestConstruct); |
| 325 | } |
| 326 | |
| 327 | /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid. |
| 328 | void |
| 329 | Sema::CheckStaticCast(SourceLocation OpLoc, Expr *&SrcExpr, QualType DestType) |
| 330 | { |
| 331 | #if 0 |
| 332 | // 5.2.9/1 sets the ground rule of disallowing casting away constness. |
| 333 | // 5.2.9/2 permits everything allowed for direct-init, deferring to 8.5. |
| 334 | // Note: for class destination, that's overload resolution over dest's |
| 335 | // constructors. Src's conversions are only considered in overload choice. |
| 336 | // For any other destination, that's just the clause 4 standards convs. |
| 337 | // 5.2.9/4 permits static_cast<cv void>(anything), which is a no-op. |
| 338 | // 5.2.9/5 permits explicit non-dynamic downcasts for lvalue-to-reference. |
| 339 | // 5.2.9/6 permits reversing all implicit conversions except lvalue-to-rvalue, |
| 340 | // function-to-pointer, array decay and to-bool, with some further |
| 341 | // restrictions. Defers to 4. |
| 342 | // 5.2.9/7 permits integer-to-enum conversion. Interesting note: if the |
| 343 | // integer does not correspond to an enum value, the result is unspecified - |
| 344 | // but it still has to be some value of the enum. I don't think any compiler |
| 345 | // complies with that. |
| 346 | // 5.2.9/8 is 5.2.9/5 for pointers. |
| 347 | // 5.2.9/9 messes with member pointers. TODO. No need to think about that yet. |
| 348 | // 5.2.9/10 permits void* to T*. |
| 349 | |
| 350 | QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); |
| 351 | DestType = Context.getCanonicalType(DestType); |
| 352 | // Tests are ordered by simplicity and a wild guess at commonness. |
| 353 | |
| 354 | if (const BuiltinType *BuiltinDest = DestType->getAsBuiltinType()) { |
| 355 | // 5.2.9/4 |
| 356 | if (BuiltinDest->getKind() == BuiltinType::Void) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | // Primitive conversions for 5.2.9/2 and 6. |
| 361 | } |
| 362 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 365 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 367 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 368 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 370 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 372 | |
| 373 | /// ActOnCXXThrow - Parse throw expressions. |
| 374 | Action::ExprResult |
| 375 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 376 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 377 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 378 | |
| 379 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 380 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 381 | /// is a non-lvalue expression whose value is the address of the object for |
| 382 | /// which the function is called. |
| 383 | |
| 384 | if (!isa<FunctionDecl>(CurContext)) { |
| 385 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 386 | return ExprResult(true); |
| 387 | } |
| 388 | |
| 389 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 390 | if (MD->isInstance()) |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 391 | return new PredefinedExpr(ThisLoc, MD->getThisType(Context), |
| 392 | PredefinedExpr::CXXThis); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 393 | |
| 394 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 395 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 396 | |
| 397 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 398 | /// Can be interpreted either as function-style casting ("int(x)") |
| 399 | /// or class type construction ("ClassType(x,y,z)") |
| 400 | /// or creation of a value-initialized type ("int()"). |
| 401 | Action::ExprResult |
| 402 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 403 | SourceLocation LParenLoc, |
| 404 | ExprTy **ExprTys, unsigned NumExprs, |
| 405 | SourceLocation *CommaLocs, |
| 406 | SourceLocation RParenLoc) { |
| 407 | assert(TypeRep && "Missing type!"); |
| 408 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
| 409 | Expr **Exprs = (Expr**)ExprTys; |
| 410 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 411 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 412 | |
| 413 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 414 | // C++ 5.2.3p1: |
| 415 | // If the simple-type-specifier specifies a class type, the class type shall |
| 416 | // be complete. |
| 417 | // |
| 418 | if (!RT->getDecl()->isDefinition()) |
| 419 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use, |
| 420 | Ty.getAsString(), FullRange); |
| 421 | |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 422 | unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 423 | "class constructors are not supported yet"); |
| 424 | return Diag(TyBeginLoc, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // C++ 5.2.3p1: |
| 428 | // If the expression list is a single expression, the type conversion |
| 429 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 430 | // corresponding cast expression. |
| 431 | // |
| 432 | if (NumExprs == 1) { |
| 433 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
| 434 | return true; |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 435 | return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc, |
| 436 | Exprs[0], RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // C++ 5.2.3p1: |
| 440 | // If the expression list specifies more than a single value, the type shall |
| 441 | // be a class with a suitably declared constructor. |
| 442 | // |
| 443 | if (NumExprs > 1) |
| 444 | return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg, |
| 445 | FullRange); |
| 446 | |
| 447 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 448 | |
| 449 | // C++ 5.2.3p2: |
| 450 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 451 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 452 | // rvalue of the specified type, which is value-initialized. |
| 453 | // |
| 454 | if (Ty->isArrayType()) |
| 455 | return Diag(TyBeginLoc, diag::err_value_init_for_array_type, FullRange); |
| 456 | if (Ty->isIncompleteType() && !Ty->isVoidType()) |
| 457 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use, |
| 458 | Ty.getAsString(), FullRange); |
| 459 | |
| 460 | return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); |
| 461 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 462 | |
| 463 | |
| 464 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 465 | /// C++ if/switch/while/for statement. |
| 466 | /// e.g: "if (int x = f()) {...}" |
| 467 | Action::ExprResult |
| 468 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 469 | Declarator &D, |
| 470 | SourceLocation EqualLoc, |
| 471 | ExprTy *AssignExprVal) { |
| 472 | assert(AssignExprVal && "Null assignment expression"); |
| 473 | |
| 474 | // C++ 6.4p2: |
| 475 | // The declarator shall not specify a function or an array. |
| 476 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 477 | // new class or enumeration. |
| 478 | |
| 479 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 480 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 481 | |
| 482 | QualType Ty = GetTypeForDeclarator(D, S); |
| 483 | |
| 484 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 485 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 486 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 487 | return Diag(StartLoc, diag::err_invalid_use_of_function_type, |
| 488 | SourceRange(StartLoc, EqualLoc)); |
| 489 | } else if (Ty->isArrayType()) { // ...or an array. |
| 490 | Diag(StartLoc, diag::err_invalid_use_of_array_type, |
| 491 | SourceRange(StartLoc, EqualLoc)); |
| 492 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 493 | RecordDecl *RD = RT->getDecl(); |
| 494 | // The type-specifier-seq shall not declare a new class... |
| 495 | if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD))) |
| 496 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 497 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 498 | EnumDecl *ED = ET->getDecl(); |
| 499 | // ...or enumeration. |
| 500 | if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED))) |
| 501 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 502 | } |
| 503 | |
| 504 | DeclTy *Dcl = ActOnDeclarator(S, D, 0); |
| 505 | if (!Dcl) |
| 506 | return true; |
| 507 | AddInitializerToDecl(Dcl, AssignExprVal); |
| 508 | |
| 509 | return new CXXConditionDeclExpr(StartLoc, EqualLoc, |
| 510 | cast<VarDecl>(static_cast<Decl *>(Dcl))); |
| 511 | } |
| 512 | |
| 513 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 514 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 515 | // C++ 6.4p4: |
| 516 | // The value of a condition that is an initialized declaration in a statement |
| 517 | // other than a switch statement is the value of the declared variable |
| 518 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 519 | // program is ill-formed. |
| 520 | // The value of a condition that is an expression is the value of the |
| 521 | // expression, implicitly converted to bool. |
| 522 | // |
| 523 | QualType Ty = CondExpr->getType(); // Save the type. |
| 524 | AssignConvertType |
| 525 | ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr); |
| 526 | if (ConvTy == Incompatible) |
| 527 | return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition, |
| 528 | Ty.getAsString(), CondExpr->getSourceRange()); |
| 529 | return false; |
| 530 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 531 | |
| 532 | /// Helper function to determine whether this is the (deprecated) C++ |
| 533 | /// conversion from a string literal to a pointer to non-const char or |
| 534 | /// non-const wchar_t (for narrow and wide string literals, |
| 535 | /// respectively). |
| 536 | bool |
| 537 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 538 | // Look inside the implicit cast, if it exists. |
| 539 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 540 | From = Cast->getSubExpr(); |
| 541 | |
| 542 | // A string literal (2.13.4) that is not a wide string literal can |
| 543 | // be converted to an rvalue of type "pointer to char"; a wide |
| 544 | // string literal can be converted to an rvalue of type "pointer |
| 545 | // to wchar_t" (C++ 4.2p2). |
| 546 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 547 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 548 | if (const BuiltinType *ToPointeeType |
| 549 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 550 | // This conversion is considered only when there is an |
| 551 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 552 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 553 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 554 | (!StrLit->isWide() && |
| 555 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 556 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | return false; |
| 561 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 562 | |
| 563 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 564 | /// expression From to the type ToType. Returns true if there was an |
| 565 | /// error, false otherwise. The expression From is replaced with the |
| 566 | /// converted expression. |
| 567 | bool |
| 568 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType) |
| 569 | { |
| 570 | ImplicitConversionSequence ICS = TryCopyInitialization(From, ToType); |
| 571 | switch (ICS.ConversionKind) { |
| 572 | case ImplicitConversionSequence::StandardConversion: |
| 573 | if (PerformImplicitConversion(From, ToType, ICS.Standard)) |
| 574 | return true; |
| 575 | break; |
| 576 | |
| 577 | case ImplicitConversionSequence::UserDefinedConversion: |
| 578 | // FIXME: This is, of course, wrong. We'll need to actually call |
| 579 | // the constructor or conversion operator, and then cope with the |
| 580 | // standard conversions. |
| 581 | ImpCastExprToType(From, ToType); |
| 582 | break; |
| 583 | |
| 584 | case ImplicitConversionSequence::EllipsisConversion: |
| 585 | assert(false && "Cannot perform an ellipsis conversion"); |
| 586 | break; |
| 587 | |
| 588 | case ImplicitConversionSequence::BadConversion: |
| 589 | return true; |
| 590 | } |
| 591 | |
| 592 | // Everything went well. |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 597 | /// expression From to the type ToType by following the standard |
| 598 | /// conversion sequence SCS. Returns true if there was an error, false |
| 599 | /// otherwise. The expression From is replaced with the converted |
| 600 | /// expression. |
| 601 | bool |
| 602 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 603 | const StandardConversionSequence& SCS) |
| 604 | { |
| 605 | // Overall FIXME: we are recomputing too many types here and doing |
| 606 | // far too much extra work. What this means is that we need to keep |
| 607 | // track of more information that is computed when we try the |
| 608 | // implicit conversion initially, so that we don't need to recompute |
| 609 | // anything here. |
| 610 | QualType FromType = From->getType(); |
| 611 | |
| 612 | // Perform the first implicit conversion. |
| 613 | switch (SCS.First) { |
| 614 | case ICK_Identity: |
| 615 | case ICK_Lvalue_To_Rvalue: |
| 616 | // Nothing to do. |
| 617 | break; |
| 618 | |
| 619 | case ICK_Array_To_Pointer: |
| 620 | FromType = Context.getArrayDecayedType(FromType); |
| 621 | ImpCastExprToType(From, FromType); |
| 622 | break; |
| 623 | |
| 624 | case ICK_Function_To_Pointer: |
| 625 | FromType = Context.getPointerType(FromType); |
| 626 | ImpCastExprToType(From, FromType); |
| 627 | break; |
| 628 | |
| 629 | default: |
| 630 | assert(false && "Improper first standard conversion"); |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | // Perform the second implicit conversion |
| 635 | switch (SCS.Second) { |
| 636 | case ICK_Identity: |
| 637 | // Nothing to do. |
| 638 | break; |
| 639 | |
| 640 | case ICK_Integral_Promotion: |
| 641 | case ICK_Floating_Promotion: |
| 642 | case ICK_Integral_Conversion: |
| 643 | case ICK_Floating_Conversion: |
| 644 | case ICK_Floating_Integral: |
| 645 | FromType = ToType.getUnqualifiedType(); |
| 646 | ImpCastExprToType(From, FromType); |
| 647 | break; |
| 648 | |
| 649 | case ICK_Pointer_Conversion: |
| 650 | if (CheckPointerConversion(From, ToType)) |
| 651 | return true; |
| 652 | ImpCastExprToType(From, ToType); |
| 653 | break; |
| 654 | |
| 655 | case ICK_Pointer_Member: |
| 656 | // FIXME: Implement pointer-to-member conversions. |
| 657 | assert(false && "Pointer-to-member conversions are unsupported"); |
| 658 | break; |
| 659 | |
| 660 | case ICK_Boolean_Conversion: |
| 661 | FromType = Context.BoolTy; |
| 662 | ImpCastExprToType(From, FromType); |
| 663 | break; |
| 664 | |
| 665 | default: |
| 666 | assert(false && "Improper second standard conversion"); |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | switch (SCS.Third) { |
| 671 | case ICK_Identity: |
| 672 | // Nothing to do. |
| 673 | break; |
| 674 | |
| 675 | case ICK_Qualification: |
| 676 | ImpCastExprToType(From, ToType); |
| 677 | break; |
| 678 | |
| 679 | default: |
| 680 | assert(false && "Improper second standard conversion"); |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | return false; |
| 685 | } |
| 686 | |