Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.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 expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 29 | /// \brief Determine whether the use of this declaration is valid, and |
| 30 | /// emit any corresponding diagnostics. |
| 31 | /// |
| 32 | /// This routine diagnoses various problems with referencing |
| 33 | /// declarations that can occur when using a declaration. For example, |
| 34 | /// it might warn if a deprecated or unavailable declaration is being |
| 35 | /// used, or produce an error (and return true) if a C++0x deleted |
| 36 | /// function is being used. |
| 37 | /// |
| 38 | /// \returns true if there was an error (this declaration cannot be |
| 39 | /// referenced), false otherwise. |
| 40 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 41 | // See if the decl is deprecated. |
| 42 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 43 | // Implementing deprecated stuff requires referencing deprecated |
| 44 | // stuff. Don't warn if we are implementing a deprecated |
| 45 | // construct. |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 46 | bool isSilenced = false; |
| 47 | |
| 48 | if (NamedDecl *ND = getCurFunctionOrMethodDecl()) { |
| 49 | // If this reference happens *in* a deprecated function or method, don't |
| 50 | // warn. |
| 51 | isSilenced = ND->getAttr<DeprecatedAttr>(); |
| 52 | |
| 53 | // If this is an Objective-C method implementation, check to see if the |
| 54 | // method was deprecated on the declaration, not the definition. |
| 55 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 56 | // The semantic decl context of a ObjCMethodDecl is the |
| 57 | // ObjCImplementationDecl. |
| 58 | if (ObjCImplementationDecl *Impl |
| 59 | = dyn_cast<ObjCImplementationDecl>(MD->getParent())) { |
| 60 | |
| 61 | MD = Impl->getClassInterface()->getMethod(MD->getSelector(), |
| 62 | MD->isInstanceMethod()); |
| 63 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (!isSilenced) |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 69 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 70 | } |
| 71 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 72 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 73 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 74 | if (FD->isDeleted()) { |
| 75 | Diag(Loc, diag::err_deleted_function_use); |
| 76 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 77 | return true; |
| 78 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 79 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 80 | |
| 81 | // See if the decl is unavailable |
| 82 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 83 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 84 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 85 | } |
| 86 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 87 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 90 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 91 | Expr *Ex = (Expr *)E; |
| 92 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 93 | } |
| 94 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | // Standard Promotions and Conversions |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 99 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 100 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 101 | QualType Ty = E->getType(); |
| 102 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 103 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 104 | if (Ty->isFunctionType()) |
| 105 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 106 | else if (Ty->isArrayType()) { |
| 107 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 108 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 109 | // type 'array of type' is converted to an expression that has type 'pointer |
| 110 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 111 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 112 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 113 | // |
| 114 | // C++ 4.2p1: |
| 115 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 116 | // T" can be converted to an rvalue of type "pointer to T". |
| 117 | // |
| 118 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 119 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 120 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 121 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 125 | /// operators (C99 6.3). The conversions of array and function types are |
| 126 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 127 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 128 | /// In these instances, this routine should *not* be called. |
| 129 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 130 | QualType Ty = Expr->getType(); |
| 131 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 132 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 133 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 134 | ImpCastExprToType(Expr, Context.IntTy); |
| 135 | else |
| 136 | DefaultFunctionArrayConversion(Expr); |
| 137 | |
| 138 | return Expr; |
| 139 | } |
| 140 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 141 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 142 | /// do not have a prototype. Arguments that have type float are promoted to |
| 143 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 144 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 145 | QualType Ty = Expr->getType(); |
| 146 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 147 | |
| 148 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 149 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 150 | if (BT->getKind() == BuiltinType::Float) |
| 151 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 152 | |
| 153 | UsualUnaryConversions(Expr); |
| 154 | } |
| 155 | |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 156 | // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 157 | // will warn if the resulting type is not a POD type. |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 158 | void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 159 | DefaultArgumentPromotion(Expr); |
| 160 | |
| 161 | if (!Expr->getType()->isPODType()) { |
| 162 | Diag(Expr->getLocStart(), |
| 163 | diag::warn_cannot_pass_non_pod_arg_to_vararg) << |
| 164 | Expr->getType() << CT; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 169 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 170 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 171 | /// routine returns the first non-arithmetic type found. The client is |
| 172 | /// responsible for emitting appropriate error diagnostics. |
| 173 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 174 | /// GCC. |
| 175 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 176 | bool isCompAssign) { |
| 177 | if (!isCompAssign) { |
| 178 | UsualUnaryConversions(lhsExpr); |
| 179 | UsualUnaryConversions(rhsExpr); |
| 180 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 181 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 182 | // For conversion purposes, we ignore any qualifiers. |
| 183 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 184 | QualType lhs = |
| 185 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 186 | QualType rhs = |
| 187 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 188 | |
| 189 | // If both types are identical, no conversion is needed. |
| 190 | if (lhs == rhs) |
| 191 | return lhs; |
| 192 | |
| 193 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 194 | // The caller can deal with this (e.g. pointer + int). |
| 195 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 196 | return lhs; |
| 197 | |
| 198 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
| 199 | if (!isCompAssign) { |
| 200 | ImpCastExprToType(lhsExpr, destType); |
| 201 | ImpCastExprToType(rhsExpr, destType); |
| 202 | } |
| 203 | return destType; |
| 204 | } |
| 205 | |
| 206 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 207 | // Perform the usual unary conversions. We do this early so that |
| 208 | // integral promotions to "int" can allow us to exit early, in the |
| 209 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 210 | // qualifiers. For example, "const float" and "float" are |
| 211 | // equivalent. |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 212 | if (lhs->isPromotableIntegerType()) |
| 213 | lhs = Context.IntTy; |
| 214 | else |
| 215 | lhs = lhs.getUnqualifiedType(); |
| 216 | if (rhs->isPromotableIntegerType()) |
| 217 | rhs = Context.IntTy; |
| 218 | else |
| 219 | rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 220 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 221 | // If both types are identical, no conversion is needed. |
| 222 | if (lhs == rhs) |
| 223 | return lhs; |
| 224 | |
| 225 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 226 | // The caller can deal with this (e.g. pointer + int). |
| 227 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 228 | return lhs; |
| 229 | |
| 230 | // At this point, we have two different arithmetic types. |
| 231 | |
| 232 | // Handle complex types first (C99 6.3.1.8p1). |
| 233 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 234 | // if we have an integer operand, the result is the complex type. |
| 235 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 236 | // convert the rhs to the lhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 237 | return lhs; |
| 238 | } |
| 239 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 240 | // convert the lhs to the rhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 241 | return rhs; |
| 242 | } |
| 243 | // This handles complex/complex, complex/float, or float/complex. |
| 244 | // When both operands are complex, the shorter operand is converted to the |
| 245 | // type of the longer, and that is the type of the result. This corresponds |
| 246 | // to what is done when combining two real floating-point operands. |
| 247 | // The fun begins when size promotion occur across type domains. |
| 248 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 249 | // floating-point type, the less precise type is converted, within it's |
| 250 | // real or complex domain, to the precision of the other type. For example, |
| 251 | // when combining a "long double" with a "double _Complex", the |
| 252 | // "double _Complex" is promoted to "long double _Complex". |
| 253 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 254 | |
| 255 | if (result > 0) { // The left side is bigger, convert rhs. |
| 256 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 257 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 258 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | } |
| 260 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 261 | // domains match. This is a requirement for our implementation, C99 |
| 262 | // does not require this promotion. |
| 263 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 264 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 265 | return rhs; |
| 266 | } else { // handle "_Complex double, double". |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 267 | return lhs; |
| 268 | } |
| 269 | } |
| 270 | return lhs; // The domain/size match exactly. |
| 271 | } |
| 272 | // Now handle "real" floating types (i.e. float, double, long double). |
| 273 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 274 | // if we have an integer operand, the result is the real floating type. |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 275 | if (rhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 276 | // convert rhs to the lhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 277 | return lhs; |
| 278 | } |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 279 | if (rhs->isComplexIntegerType()) { |
| 280 | // convert rhs to the complex floating point type. |
| 281 | return Context.getComplexType(lhs); |
| 282 | } |
| 283 | if (lhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 284 | // convert lhs to the rhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 285 | return rhs; |
| 286 | } |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 287 | if (lhs->isComplexIntegerType()) { |
| 288 | // convert lhs to the complex floating point type. |
| 289 | return Context.getComplexType(rhs); |
| 290 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 291 | // We have two real floating types, float/complex combos were handled above. |
| 292 | // Convert the smaller operand to the bigger result. |
| 293 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 294 | if (result > 0) // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 295 | return lhs; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 296 | assert(result < 0 && "illegal float comparison"); |
| 297 | return rhs; // convert the lhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 298 | } |
| 299 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 300 | // Handle GCC complex int extension. |
| 301 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 302 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 303 | |
| 304 | if (lhsComplexInt && rhsComplexInt) { |
| 305 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 306 | rhsComplexInt->getElementType()) >= 0) |
| 307 | return lhs; // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 308 | return rhs; |
| 309 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 310 | // convert the rhs to the lhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 311 | return lhs; |
| 312 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 313 | // convert the lhs to the rhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 314 | return rhs; |
| 315 | } |
| 316 | } |
| 317 | // Finally, we have two differing integer types. |
| 318 | // The rules for this case are in C99 6.3.1.8 |
| 319 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 320 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 321 | rhsSigned = rhs->isSignedIntegerType(); |
| 322 | QualType destType; |
| 323 | if (lhsSigned == rhsSigned) { |
| 324 | // Same signedness; use the higher-ranked type |
| 325 | destType = compare >= 0 ? lhs : rhs; |
| 326 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 327 | // The unsigned type has greater than or equal rank to the |
| 328 | // signed type, so use the unsigned type |
| 329 | destType = lhsSigned ? rhs : lhs; |
| 330 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 331 | // The two types are different widths; if we are here, that |
| 332 | // means the signed type is larger than the unsigned type, so |
| 333 | // use the signed type. |
| 334 | destType = lhsSigned ? lhs : rhs; |
| 335 | } else { |
| 336 | // The signed type is higher-ranked than the unsigned type, |
| 337 | // but isn't actually any bigger (like unsigned int and long |
| 338 | // on most 32-bit systems). Use the unsigned type corresponding |
| 339 | // to the signed type. |
| 340 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 341 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 342 | return destType; |
| 343 | } |
| 344 | |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | // Semantic Analysis for various Expression Types |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | |
| 349 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 350 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 352 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 353 | /// multiple tokens. However, the common case is that StringToks points to one |
| 354 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 355 | /// |
| 356 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 357 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | assert(NumStringToks && "Must have at least one string!"); |
| 359 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 360 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 362 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | |
| 364 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 365 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 366 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 367 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 368 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 369 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 370 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 371 | |
| 372 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 373 | if (getLangOptions().CPlusPlus) |
| 374 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 375 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 376 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 377 | // the nul terminator character as well as the string length for pascal |
| 378 | // strings. |
| 379 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 380 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 381 | ArrayType::Normal, 0); |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 382 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 384 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 385 | Literal.GetStringLength(), |
| 386 | Literal.AnyWide, StrTy, |
| 387 | &StringTokLocs[0], |
| 388 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 391 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 392 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 393 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 394 | /// for values inside the block or for globals). |
| 395 | /// |
| 396 | /// FIXME: This will create BlockDeclRefExprs for global variables, |
| 397 | /// function references, etc which is suboptimal :) and breaks |
| 398 | /// things like "integer constant expression" tests. |
| 399 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 400 | ValueDecl *VD) { |
| 401 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 402 | // we wanted to. |
| 403 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 404 | return false; |
| 405 | |
| 406 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 407 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 408 | return false; |
| 409 | |
| 410 | // If this is a reference to an extern, static, or global variable, no need to |
| 411 | // snapshot it. |
| 412 | // FIXME: What about 'const' variables in C++? |
| 413 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 414 | return Var->hasLocalStorage(); |
| 415 | |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 421 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | 0d755ad | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 423 | /// identifier is used in a function call context. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 424 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 425 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 426 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 427 | IdentifierInfo &II, |
| 428 | bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 429 | const CXXScopeSpec *SS, |
| 430 | bool isAddressOfOperand) { |
| 431 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 432 | isAddressOfOperand); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 435 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 436 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 437 | /// nested-name-specifier. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 438 | DeclRefExpr * |
| 439 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 440 | bool TypeDependent, bool ValueDependent, |
| 441 | const CXXScopeSpec *SS) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 442 | if (SS && !SS->isEmpty()) |
| 443 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 444 | ValueDependent, |
| 445 | SS->getRange().getBegin()); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 446 | else |
| 447 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 450 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 451 | /// variable corresponding to the anonymous union or struct whose type |
| 452 | /// is Record. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 453 | static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 454 | assert(Record->isAnonymousStructOrUnion() && |
| 455 | "Record must be an anonymous struct or union!"); |
| 456 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 457 | // FIXME: Once Decls are directly linked together, this will |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 458 | // be an O(1) operation rather than a slow walk through DeclContext's |
| 459 | // vector (which itself will be eliminated). DeclGroups might make |
| 460 | // this even better. |
| 461 | DeclContext *Ctx = Record->getDeclContext(); |
| 462 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 463 | DEnd = Ctx->decls_end(); |
| 464 | D != DEnd; ++D) { |
| 465 | if (*D == Record) { |
| 466 | // The object for the anonymous struct/union directly |
| 467 | // follows its type in the list of declarations. |
| 468 | ++D; |
| 469 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 470 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 471 | return *D; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | assert(false && "Missing object for anonymous record"); |
| 476 | return 0; |
| 477 | } |
| 478 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 479 | Sema::OwningExprResult |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 480 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 481 | FieldDecl *Field, |
| 482 | Expr *BaseObjectExpr, |
| 483 | SourceLocation OpLoc) { |
| 484 | assert(Field->getDeclContext()->isRecord() && |
| 485 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 486 | && "Field must be stored inside an anonymous struct or union"); |
| 487 | |
| 488 | // Construct the sequence of field member references |
| 489 | // we'll have to perform to get to the field in the anonymous |
| 490 | // union/struct. The list of members is built from the field |
| 491 | // outward, so traverse it backwards to go from an object in |
| 492 | // the current context to the field we found. |
| 493 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 494 | AnonFields.push_back(Field); |
| 495 | VarDecl *BaseObject = 0; |
| 496 | DeclContext *Ctx = Field->getDeclContext(); |
| 497 | do { |
| 498 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 499 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 500 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
| 501 | AnonFields.push_back(AnonField); |
| 502 | else { |
| 503 | BaseObject = cast<VarDecl>(AnonObject); |
| 504 | break; |
| 505 | } |
| 506 | Ctx = Ctx->getParent(); |
| 507 | } while (Ctx->isRecord() && |
| 508 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
| 509 | |
| 510 | // Build the expression that refers to the base object, from |
| 511 | // which we will build a sequence of member references to each |
| 512 | // of the anonymous union objects and, eventually, the field we |
| 513 | // found via name lookup. |
| 514 | bool BaseObjectIsPointer = false; |
| 515 | unsigned ExtraQuals = 0; |
| 516 | if (BaseObject) { |
| 517 | // BaseObject is an anonymous struct/union variable (and is, |
| 518 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 519 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 520 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 521 | SourceLocation()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 522 | ExtraQuals |
| 523 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 524 | } else if (BaseObjectExpr) { |
| 525 | // The caller provided the base object expression. Determine |
| 526 | // whether its a pointer and whether it adds any qualifiers to the |
| 527 | // anonymous struct/union fields we're looking into. |
| 528 | QualType ObjectType = BaseObjectExpr->getType(); |
| 529 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 530 | BaseObjectIsPointer = true; |
| 531 | ObjectType = ObjectPtr->getPointeeType(); |
| 532 | } |
| 533 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 534 | } else { |
| 535 | // We've found a member of an anonymous struct/union that is |
| 536 | // inside a non-anonymous struct/union, so in a well-formed |
| 537 | // program our base object expression is "this". |
| 538 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 539 | if (!MD->isStatic()) { |
| 540 | QualType AnonFieldType |
| 541 | = Context.getTagDeclType( |
| 542 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 543 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 544 | if ((Context.getCanonicalType(AnonFieldType) |
| 545 | == Context.getCanonicalType(ThisType)) || |
| 546 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 547 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 548 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 549 | MD->getThisType(Context)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 550 | BaseObjectIsPointer = true; |
| 551 | } |
| 552 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 553 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 554 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 555 | } |
| 556 | ExtraQuals = MD->getTypeQualifiers(); |
| 557 | } |
| 558 | |
| 559 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 560 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 561 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // Build the implicit member references to the field of the |
| 565 | // anonymous struct/union. |
| 566 | Expr *Result = BaseObjectExpr; |
| 567 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 568 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 569 | FI != FIEnd; ++FI) { |
| 570 | QualType MemberType = (*FI)->getType(); |
| 571 | if (!(*FI)->isMutable()) { |
| 572 | unsigned combinedQualifiers |
| 573 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 574 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 575 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 576 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 577 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 578 | BaseObjectIsPointer = false; |
| 579 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 582 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 585 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 586 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 587 | /// performs lookup on that name and returns an expression that refers |
| 588 | /// to that name. This routine isn't directly called from the parser, |
| 589 | /// because the parser doesn't know about DeclarationName. Rather, |
| 590 | /// this routine is called by ActOnIdentifierExpr, |
| 591 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 592 | /// which form the DeclarationName from the corresponding syntactic |
| 593 | /// forms. |
| 594 | /// |
| 595 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 596 | /// function call context. LookupCtx is only used for a C++ |
| 597 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 598 | /// the identifier must be a member of. |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 599 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 600 | /// isAddressOfOperand means that this expression is the direct operand |
| 601 | /// of an address-of operator. This matters because this is the only |
| 602 | /// situation where a qualified name referencing a non-static member may |
| 603 | /// appear outside a member function of this class. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 604 | Sema::OwningExprResult |
| 605 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 606 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 607 | const CXXScopeSpec *SS, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 608 | bool isAddressOfOperand) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 609 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 610 | if (SS && SS->isInvalid()) |
| 611 | return ExprError(); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 612 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 613 | false, true, Loc); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 615 | NamedDecl *D = 0; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 616 | if (Lookup.isAmbiguous()) { |
| 617 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 618 | SS && SS->isSet() ? SS->getRange() |
| 619 | : SourceRange()); |
| 620 | return ExprError(); |
| 621 | } else |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 622 | D = Lookup.getAsDecl(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 624 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 625 | // well. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 626 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 627 | if (II && getCurMethodDecl()) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 628 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 629 | // in which case we should look for an ivar. 2) scoped lookup could have |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 630 | // found a decl, but that decl is outside the current instance method (i.e. |
| 631 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 632 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 633 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 634 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 635 | ObjCInterfaceDecl *ClassDeclared; |
| 636 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 637 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 638 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 639 | return ExprError(); |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 640 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 641 | // If a class method attemps to use a free standing ivar, this is |
| 642 | // an error. |
| 643 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 644 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 645 | << IV->getDeclName()); |
| 646 | // If a class method uses a global variable, even if an ivar with |
| 647 | // same name exists, use the global. |
| 648 | if (!IsClsMethod) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 649 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 650 | ClassDeclared != IFace) |
| 651 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 652 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 653 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 654 | IdentifierInfo &II = Context.Idents.get("self"); |
| 655 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
| 656 | ObjCIvarRefExpr *MRef = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 657 | Loc, static_cast<Expr*>(SelfExpr.release()), |
| 658 | true, true); |
| 659 | Context.setFieldDecl(IFace, IV, MRef); |
| 660 | return Owned(MRef); |
| 661 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 664 | else if (getCurMethodDecl()->isInstanceMethod()) { |
| 665 | // We should warn if a local variable hides an ivar. |
| 666 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 667 | ObjCInterfaceDecl *ClassDeclared; |
| 668 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 669 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 670 | IFace == ClassDeclared) |
| 671 | Diag(Loc, diag::warn_ivar_use_hidden)<<IV->getDeclName(); |
| 672 | } |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 673 | } |
Steve Naroff | 76de9d7 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 674 | // Needed to implement property "super.method" notation. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 675 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 676 | QualType T = Context.getPointerType(Context.getObjCInterfaceType( |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 677 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 678 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 679 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 680 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 682 | // Determine whether this name might be a candidate for |
| 683 | // argument-dependent lookup. |
| 684 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 685 | HasTrailingLParen; |
| 686 | |
| 687 | if (ADL && D == 0) { |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 688 | // We've seen something of the form |
| 689 | // |
| 690 | // identifier( |
| 691 | // |
| 692 | // and we did not find any entity by the name |
| 693 | // "identifier". However, this identifier is still subject to |
| 694 | // argument-dependent lookup, so keep track of the name. |
| 695 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 696 | Context.OverloadTy, |
| 697 | Loc)); |
| 698 | } |
| 699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 700 | if (D == 0) { |
| 701 | // Otherwise, this could be an implicitly declared function reference (legal |
| 702 | // in C90, extension in C99). |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 703 | if (HasTrailingLParen && II && |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 704 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 705 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | else { |
| 707 | // If this name wasn't predeclared and if this is not a function call, |
| 708 | // diagnose the problem. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 709 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 710 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 711 | << Name << SS->getRange()); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 712 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 713 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 714 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 715 | << Name.getAsString()); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 716 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 717 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 720 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 721 | // If this is an expression of the form &Class::member, don't build an |
| 722 | // implicit member ref, because we want a pointer to the member in general, |
| 723 | // not any specific instance's member. |
| 724 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 725 | DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 726 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 727 | QualType DType; |
| 728 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 729 | DType = FD->getType().getNonReferenceType(); |
| 730 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 731 | DType = Method->getType(); |
| 732 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 733 | DType = Context.OverloadTy; |
| 734 | } |
| 735 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 736 | if (!DType.isNull()) { |
| 737 | // The pointer is type- and value-dependent if it points into something |
| 738 | // dependent. |
| 739 | bool Dependent = false; |
| 740 | for (; DC; DC = DC->getParent()) { |
| 741 | // FIXME: could stop early at namespace scope. |
| 742 | if (DC->isRecord()) { |
| 743 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 744 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 745 | Dependent = true; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 750 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 755 | // We may have found a field within an anonymous union or struct |
| 756 | // (C++ [class.union]). |
| 757 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 758 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 759 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 761 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 762 | if (!MD->isStatic()) { |
| 763 | // C++ [class.mfct.nonstatic]p2: |
| 764 | // [...] if name lookup (3.4.1) resolves the name in the |
| 765 | // id-expression to a nonstatic nontype member of class X or of |
| 766 | // a base class of X, the id-expression is transformed into a |
| 767 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 768 | // as the postfix-expression to the left of the '.' operator. |
| 769 | DeclContext *Ctx = 0; |
| 770 | QualType MemberType; |
| 771 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 772 | Ctx = FD->getDeclContext(); |
| 773 | MemberType = FD->getType(); |
| 774 | |
| 775 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 776 | MemberType = RefType->getPointeeType(); |
| 777 | else if (!FD->isMutable()) { |
| 778 | unsigned combinedQualifiers |
| 779 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 780 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 781 | } |
| 782 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 783 | if (!Method->isStatic()) { |
| 784 | Ctx = Method->getParent(); |
| 785 | MemberType = Method->getType(); |
| 786 | } |
| 787 | } else if (OverloadedFunctionDecl *Ovl |
| 788 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 789 | for (OverloadedFunctionDecl::function_iterator |
| 790 | Func = Ovl->function_begin(), |
| 791 | FuncEnd = Ovl->function_end(); |
| 792 | Func != FuncEnd; ++Func) { |
| 793 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 794 | if (!DMethod->isStatic()) { |
| 795 | Ctx = Ovl->getDeclContext(); |
| 796 | MemberType = Context.OverloadTy; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 801 | |
| 802 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 803 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 804 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 805 | if ((Context.getCanonicalType(CtxType) |
| 806 | == Context.getCanonicalType(ThisType)) || |
| 807 | IsDerivedFrom(ThisType, CtxType)) { |
| 808 | // Build the implicit member access expression. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 809 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 810 | MD->getThisType(Context)); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 811 | return Owned(new (Context) MemberExpr(This, true, D, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 812 | SourceLocation(), MemberType)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 818 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 819 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 820 | if (MD->isStatic()) |
| 821 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 822 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 823 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 826 | // Any other ways we could have found the field in a well-formed |
| 827 | // program would have been turned into implicit member expressions |
| 828 | // above. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 829 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 830 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 831 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 832 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 833 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 834 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 835 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 836 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 837 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 838 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 840 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 841 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 842 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 843 | false, false, SS)); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 844 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 845 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 846 | false, false, SS)); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 847 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 849 | // Check whether this declaration can be used. Note that we suppress |
| 850 | // this check when we're going to perform argument-dependent lookup |
| 851 | // on this function name, because this might not be the function |
| 852 | // that overload resolution actually selects. |
| 853 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 854 | return ExprError(); |
| 855 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 856 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 857 | // Warn about constructs like: |
| 858 | // if (void *X = foo()) { ... } else { X }. |
| 859 | // In the else block, the pointer is always false. |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 860 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 861 | Scope *CheckS = S; |
| 862 | while (CheckS) { |
| 863 | if (CheckS->isWithinElse() && |
| 864 | CheckS->getControlParent()->isDeclScope(Var)) { |
| 865 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 866 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 867 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 868 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 869 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 870 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | |
| 874 | // Move up one more control parent to check again. |
| 875 | CheckS = CheckS->getControlParent(); |
| 876 | if (CheckS) |
| 877 | CheckS = CheckS->getParent(); |
| 878 | } |
| 879 | } |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 880 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(VD)) { |
| 881 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 882 | // C99 DR 316 says that, if a function type comes from a |
| 883 | // function definition (without a prototype), that type is only |
| 884 | // used for checking compatibility. Therefore, when referencing |
| 885 | // the function, we pretend that we don't have the full function |
| 886 | // type. |
| 887 | QualType T = Func->getType(); |
| 888 | QualType NoProtoType = T; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 889 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 890 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 891 | return Owned(BuildDeclRefExpr(VD, NoProtoType, Loc, false, false, SS)); |
| 892 | } |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 893 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 894 | |
| 895 | // Only create DeclRefExpr's for valid Decl's. |
| 896 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 897 | return ExprError(); |
| 898 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 899 | // If the identifier reference is inside a block, and it refers to a value |
| 900 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 901 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 902 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 903 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 904 | // We do not do this for things like enum constants, global variables, etc, |
| 905 | // as they do not get snapshotted. |
| 906 | // |
| 907 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 908 | // Blocks that have these can't be constant. |
| 909 | CurBlock->hasBlockDeclRefExprs = true; |
| 910 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 911 | // The BlocksAttr indicates the variable is bound by-reference. |
| 912 | if (VD->getAttr<BlocksAttr>()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 913 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 914 | VD->getType().getNonReferenceType(), Loc, true)); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 915 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 916 | // Variable will be bound by-copy, make it const within the closure. |
| 917 | VD->getType().addConst(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 918 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 919 | VD->getType().getNonReferenceType(), Loc, false)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 920 | } |
| 921 | // If this reference is not in a block or if the referenced variable is |
| 922 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 924 | bool TypeDependent = false; |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 925 | bool ValueDependent = false; |
| 926 | if (getLangOptions().CPlusPlus) { |
| 927 | // C++ [temp.dep.expr]p3: |
| 928 | // An id-expression is type-dependent if it contains: |
| 929 | // - an identifier that was declared with a dependent type, |
| 930 | if (VD->getType()->isDependentType()) |
| 931 | TypeDependent = true; |
| 932 | // - FIXME: a template-id that is dependent, |
| 933 | // - a conversion-function-id that specifies a dependent type, |
| 934 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 935 | Name.getCXXNameType()->isDependentType()) |
| 936 | TypeDependent = true; |
| 937 | // - a nested-name-specifier that contains a class-name that |
| 938 | // names a dependent type. |
| 939 | else if (SS && !SS->isEmpty()) { |
| 940 | for (DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 941 | DC; DC = DC->getParent()) { |
| 942 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 943 | if (DC->isRecord()) { |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 944 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 945 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 946 | TypeDependent = true; |
| 947 | break; |
| 948 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 952 | |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 953 | // C++ [temp.dep.constexpr]p2: |
| 954 | // |
| 955 | // An identifier is value-dependent if it is: |
| 956 | // - a name declared with a dependent type, |
| 957 | if (TypeDependent) |
| 958 | ValueDependent = true; |
| 959 | // - the name of a non-type template parameter, |
| 960 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 961 | ValueDependent = true; |
| 962 | // - a constant with integral or enumeration type and is |
| 963 | // initialized with an expression that is value-dependent |
| 964 | // (FIXME!). |
| 965 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 966 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 967 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 968 | TypeDependent, ValueDependent, SS)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 971 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 972 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 973 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 975 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 976 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 977 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 978 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 979 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 981 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 982 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 983 | // string. |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 984 | unsigned Length; |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 985 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 986 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 987 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 988 | Length = MD->getSynthesizedMethodSize(); |
| 989 | else { |
| 990 | Diag(Loc, diag::ext_predef_outside_function); |
| 991 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 992 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 993 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 994 | |
| 995 | |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 996 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 997 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 998 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 999 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1002 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | llvm::SmallString<16> CharBuffer; |
| 1004 | CharBuffer.resize(Tok.getLength()); |
| 1005 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1006 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1007 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1009 | Tok.getLocation(), PP); |
| 1010 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1011 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1012 | |
| 1013 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1014 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1015 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1016 | Literal.isWide(), |
| 1017 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1020 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1021 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1023 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1024 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1025 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1026 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1027 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1028 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1029 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1030 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1031 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1032 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1034 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1035 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1036 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1037 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1039 | Tok.getLocation(), PP); |
| 1040 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1041 | return ExprError(); |
| 1042 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1043 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1045 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1046 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1047 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1048 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1049 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1050 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1051 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1052 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1053 | |
| 1054 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1055 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1056 | // isExact will be set by GetFloatValue(). |
| 1057 | bool isExact = false; |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1058 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 1059 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1061 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1062 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1063 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1064 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1065 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1066 | // long long is a C99 feature. |
| 1067 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1068 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1069 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1072 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1073 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1075 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1076 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1077 | Ty = Context.UnsignedLongLongTy; |
| 1078 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1079 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1080 | } else { |
| 1081 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1082 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1083 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1084 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1085 | // be an unsigned int. |
| 1086 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1087 | |
| 1088 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1089 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1090 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1091 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1092 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1093 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1094 | // Does it fit in a unsigned int? |
| 1095 | if (ResultVal.isIntN(IntSize)) { |
| 1096 | // Does it fit in a signed int? |
| 1097 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1098 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1100 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1101 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1102 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1103 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1104 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1105 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1106 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1107 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1108 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1109 | // Does it fit in a unsigned long? |
| 1110 | if (ResultVal.isIntN(LongSize)) { |
| 1111 | // Does it fit in a signed long? |
| 1112 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1113 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1115 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1116 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1117 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1120 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1121 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1122 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1123 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1124 | // Does it fit in a unsigned long long? |
| 1125 | if (ResultVal.isIntN(LongLongSize)) { |
| 1126 | // Does it fit in a signed long long? |
| 1127 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1128 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1129 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1130 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1131 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1132 | } |
| 1133 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | // If we still couldn't decide a type, we probably have something that |
| 1136 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1137 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1138 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1139 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1140 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1141 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1143 | if (ResultVal.getBitWidth() != Width) |
| 1144 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1145 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1146 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1147 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1149 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1150 | if (Literal.isImaginary) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1151 | Res = new (Context) ImaginaryLiteral(Res, |
| 1152 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1153 | |
| 1154 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1157 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1158 | SourceLocation R, ExprArg Val) { |
| 1159 | Expr *E = (Expr *)Val.release(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1160 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1161 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1165 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1166 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1167 | SourceLocation OpLoc, |
| 1168 | const SourceRange &ExprRange, |
| 1169 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1170 | if (exprType->isDependentType()) |
| 1171 | return false; |
| 1172 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1173 | // C99 6.5.3.4p1: |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1174 | if (isa<FunctionType>(exprType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1175 | // alignof(function) is allowed. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1176 | if (isSizeof) |
| 1177 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1178 | return false; |
| 1179 | } |
| 1180 | |
| 1181 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1182 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1183 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1184 | return false; |
| 1185 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1187 | return DiagnoseIncompleteType(OpLoc, exprType, |
| 1188 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1189 | diag::err_alignof_incomplete_type, |
| 1190 | ExprRange); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1193 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1194 | const SourceRange &ExprRange) { |
| 1195 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1197 | // alignof decl is always ok. |
| 1198 | if (isa<DeclRefExpr>(E)) |
| 1199 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1200 | |
| 1201 | // Cannot know anything else if the expression is dependent. |
| 1202 | if (E->isTypeDependent()) |
| 1203 | return false; |
| 1204 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1205 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1206 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1207 | if (FD->isBitField()) { |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1208 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1209 | return true; |
| 1210 | } |
| 1211 | // Other fields are ok. |
| 1212 | return false; |
| 1213 | } |
| 1214 | } |
| 1215 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1216 | } |
| 1217 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1218 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1219 | /// the same for @c alignof and @c __alignof |
| 1220 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1221 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1222 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1223 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1224 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1225 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1227 | QualType ArgTy; |
| 1228 | SourceRange Range; |
| 1229 | if (isType) { |
| 1230 | ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1231 | Range = ArgRange; |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Verify that the operand is valid. |
| 1234 | if (CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, isSizeof)) |
| 1235 | return ExprError(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1236 | } else { |
| 1237 | // Get the end location. |
| 1238 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1239 | Range = ArgEx->getSourceRange(); |
| 1240 | ArgTy = ArgEx->getType(); |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Verify that the operand is valid. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1243 | bool isInvalid; |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1244 | if (!isSizeof) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1245 | isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range); |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1246 | } else if (ArgEx->isBitField()) { // C99 6.5.3.4p1. |
| 1247 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1248 | isInvalid = true; |
| 1249 | } else { |
| 1250 | isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true); |
| 1251 | } |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (isInvalid) { |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1254 | DeleteExpr(ArgEx); |
| 1255 | return ExprError(); |
| 1256 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1259 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1260 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeof, isType, TyOrEx, |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1261 | Context.getSizeType(), OpLoc, |
| 1262 | Range.getEnd())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1265 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1266 | if (V->isTypeDependent()) |
| 1267 | return Context.DependentTy; |
| 1268 | |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1269 | DefaultFunctionArrayConversion(V); |
| 1270 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1271 | // These operators return the element type of a complex type. |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1272 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1273 | return CT->getElementType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Otherwise they pass through real integer and floating point types here. |
| 1276 | if (V->getType()->isArithmeticType()) |
| 1277 | return V->getType(); |
| 1278 | |
| 1279 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1280 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1281 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1282 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1286 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1287 | Action::OwningExprResult |
| 1288 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1289 | tok::TokenKind Kind, ExprArg Input) { |
| 1290 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1292 | UnaryOperator::Opcode Opc; |
| 1293 | switch (Kind) { |
| 1294 | default: assert(0 && "Unknown unary op!"); |
| 1295 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1296 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1297 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1299 | if (getLangOptions().CPlusPlus && |
| 1300 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1301 | // Which overloaded operator? |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1302 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1303 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1304 | |
| 1305 | // C++ [over.inc]p1: |
| 1306 | // |
| 1307 | // [...] If the function is a member function with one |
| 1308 | // parameter (which shall be of type int) or a non-member |
| 1309 | // function with two parameters (the second of which shall be |
| 1310 | // of type int), it defines the postfix increment operator ++ |
| 1311 | // for objects of that type. When the postfix increment is |
| 1312 | // called as a result of using the ++ operator, the int |
| 1313 | // argument will have value zero. |
| 1314 | Expr *Args[2] = { |
| 1315 | Arg, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1316 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1317 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1318 | }; |
| 1319 | |
| 1320 | // Build the candidate set for overloading |
| 1321 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1322 | if (AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet)) |
| 1323 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1324 | |
| 1325 | // Perform overload resolution. |
| 1326 | OverloadCandidateSet::iterator Best; |
| 1327 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1328 | case OR_Success: { |
| 1329 | // We found a built-in operator or an overloaded operator. |
| 1330 | FunctionDecl *FnDecl = Best->Function; |
| 1331 | |
| 1332 | if (FnDecl) { |
| 1333 | // We matched an overloaded operator. Build a call to that |
| 1334 | // operator. |
| 1335 | |
| 1336 | // Convert the arguments. |
| 1337 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1338 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1339 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1340 | } else { |
| 1341 | // Convert the arguments. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1342 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1343 | FnDecl->getParamDecl(0)->getType(), |
| 1344 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1345 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1349 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1350 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1351 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1352 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1353 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1354 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 488e25b | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1355 | SourceLocation()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1356 | UsualUnaryConversions(FnExpr); |
| 1357 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1358 | Input.release(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1359 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
| 1360 | ResultTy, OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1361 | } else { |
| 1362 | // We matched a built-in operator. Convert the arguments, then |
| 1363 | // break out so that we will build the appropriate built-in |
| 1364 | // operator node. |
| 1365 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1366 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1367 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1368 | |
| 1369 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1370 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | case OR_No_Viable_Function: |
| 1374 | // No viable function; fall through to handling this as a |
| 1375 | // built-in operator, which will produce an error message for us. |
| 1376 | break; |
| 1377 | |
| 1378 | case OR_Ambiguous: |
| 1379 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1380 | << UnaryOperator::getOpcodeStr(Opc) |
| 1381 | << Arg->getSourceRange(); |
| 1382 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1383 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1384 | |
| 1385 | case OR_Deleted: |
| 1386 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1387 | << Best->Function->isDeleted() |
| 1388 | << UnaryOperator::getOpcodeStr(Opc) |
| 1389 | << Arg->getSourceRange(); |
| 1390 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1391 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // Either we found no viable overloaded operator or we matched a |
| 1395 | // built-in operator. In either case, fall through to trying to |
| 1396 | // build a built-in operation. |
| 1397 | } |
| 1398 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1399 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1400 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | if (result.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1402 | return ExprError(); |
| 1403 | Input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1404 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1407 | Action::OwningExprResult |
| 1408 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1409 | ExprArg Idx, SourceLocation RLoc) { |
| 1410 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1411 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1413 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1414 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1415 | LHSExp->getType()->isEnumeralType() || |
| 1416 | RHSExp->getType()->isRecordType() || |
| 1417 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1418 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1419 | // to the candidate set. |
| 1420 | OverloadCandidateSet CandidateSet; |
| 1421 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1422 | if (AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1423 | SourceRange(LLoc, RLoc))) |
| 1424 | return ExprError(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1425 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1426 | // Perform overload resolution. |
| 1427 | OverloadCandidateSet::iterator Best; |
| 1428 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1429 | case OR_Success: { |
| 1430 | // We found a built-in operator or an overloaded operator. |
| 1431 | FunctionDecl *FnDecl = Best->Function; |
| 1432 | |
| 1433 | if (FnDecl) { |
| 1434 | // We matched an overloaded operator. Build a call to that |
| 1435 | // operator. |
| 1436 | |
| 1437 | // Convert the arguments. |
| 1438 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1439 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1440 | PerformCopyInitialization(RHSExp, |
| 1441 | FnDecl->getParamDecl(0)->getType(), |
| 1442 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1443 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1444 | } else { |
| 1445 | // Convert the arguments. |
| 1446 | if (PerformCopyInitialization(LHSExp, |
| 1447 | FnDecl->getParamDecl(0)->getType(), |
| 1448 | "passing") || |
| 1449 | PerformCopyInitialization(RHSExp, |
| 1450 | FnDecl->getParamDecl(1)->getType(), |
| 1451 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1452 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1456 | QualType ResultTy |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1457 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1458 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1459 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1460 | // Build the actual expression node. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1461 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1462 | SourceLocation()); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1463 | UsualUnaryConversions(FnExpr); |
| 1464 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1465 | Base.release(); |
| 1466 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1467 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1468 | ResultTy, LLoc)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1469 | } else { |
| 1470 | // We matched a built-in operator. Convert the arguments, then |
| 1471 | // break out so that we will build the appropriate built-in |
| 1472 | // operator node. |
| 1473 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1474 | "passing") || |
| 1475 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1476 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1477 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1478 | |
| 1479 | break; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | case OR_No_Viable_Function: |
| 1484 | // No viable function; fall through to handling this as a |
| 1485 | // built-in operator, which will produce an error message for us. |
| 1486 | break; |
| 1487 | |
| 1488 | case OR_Ambiguous: |
| 1489 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1490 | << "[]" |
| 1491 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1492 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1493 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1494 | |
| 1495 | case OR_Deleted: |
| 1496 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1497 | << Best->Function->isDeleted() |
| 1498 | << "[]" |
| 1499 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1500 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1501 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | // Either we found no viable overloaded operator or we matched a |
| 1505 | // built-in operator. In either case, fall through to trying to |
| 1506 | // build a built-in operation. |
| 1507 | } |
| 1508 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1509 | // Perform default conversions. |
| 1510 | DefaultFunctionArrayConversion(LHSExp); |
| 1511 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1513 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1514 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1516 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1517 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1518 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1519 | Expr *BaseExpr, *IndexExpr; |
| 1520 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1521 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1522 | BaseExpr = LHSExp; |
| 1523 | IndexExpr = RHSExp; |
| 1524 | ResultType = Context.DependentTy; |
| 1525 | } else if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1526 | BaseExpr = LHSExp; |
| 1527 | IndexExpr = RHSExp; |
| 1528 | // FIXME: need to deal with const... |
| 1529 | ResultType = PTy->getPointeeType(); |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1530 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1531 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1532 | BaseExpr = RHSExp; |
| 1533 | IndexExpr = LHSExp; |
| 1534 | // FIXME: need to deal with const... |
| 1535 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1536 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1537 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1538 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1540 | // FIXME: need to deal with const... |
| 1541 | ResultType = VTy->getElementType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1543 | return ExprError(Diag(LHSExp->getLocStart(), |
| 1544 | diag::err_typecheck_subscript_value) << RHSExp->getSourceRange()); |
| 1545 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1546 | // C99 6.5.2.1p1 |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1547 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1548 | return ExprError(Diag(IndexExpr->getLocStart(), |
| 1549 | diag::err_typecheck_subscript) << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1550 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1551 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 1552 | // the following check catches trying to index a pointer to a function (e.g. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1553 | // void (*)(int)) and pointers to incomplete types. Functions are not |
| 1554 | // objects in C99. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1555 | if (!ResultType->isObjectType() && !ResultType->isDependentType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1556 | return ExprError(Diag(BaseExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1557 | diag::err_typecheck_subscript_not_object) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1558 | << BaseExpr->getType() << BaseExpr->getSourceRange()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1559 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1560 | Base.release(); |
| 1561 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1562 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1563 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1566 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1567 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1568 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1569 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1570 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1571 | // The vector accessor can't exceed the number of elements. |
| 1572 | const char *compStr = CompName.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1573 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1574 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1575 | // special names that indicate a subset of exactly half the elements are |
| 1576 | // to be selected. |
| 1577 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1578 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1579 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1580 | // indicating that it is a string of hex values to be used as vector indices. |
| 1581 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1582 | |
| 1583 | // Check that we've found one of the special components, or that the component |
| 1584 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1585 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1586 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1587 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1588 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1589 | do |
| 1590 | compStr++; |
| 1591 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1592 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1593 | do |
| 1594 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1595 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1596 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1597 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1598 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1599 | // We didn't get to the end of the string. This means the component names |
| 1600 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1601 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1602 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1603 | return QualType(); |
| 1604 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1605 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1606 | // Ensure no component accessor exceeds the width of the vector type it |
| 1607 | // operates on. |
| 1608 | if (!HalvingSwizzle) { |
| 1609 | compStr = CompName.getName(); |
| 1610 | |
| 1611 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1612 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1613 | |
| 1614 | while (*compStr) { |
| 1615 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1616 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1617 | << baseType << SourceRange(CompLoc); |
| 1618 | return QualType(); |
| 1619 | } |
| 1620 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1621 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1622 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1623 | // If this is a halving swizzle, verify that the base type has an even |
| 1624 | // number of elements. |
| 1625 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1626 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1627 | << baseType << SourceRange(CompLoc); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1628 | return QualType(); |
| 1629 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1630 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1631 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1632 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1633 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1634 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1635 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1636 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1637 | : CompName.getLength(); |
| 1638 | if (HexSwizzle) |
| 1639 | CompSize--; |
| 1640 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1641 | if (CompSize == 1) |
| 1642 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1643 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1644 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1645 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1646 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1647 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1648 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1649 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1650 | } |
| 1651 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1654 | |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1655 | /// constructSetterName - Return the setter name for the given |
| 1656 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 1657 | /// has been capitalized. |
| 1658 | // FIXME: Merge with same routine in Parser. But where should this |
| 1659 | // live? |
| 1660 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 1661 | const IdentifierInfo *Name) { |
| 1662 | llvm::SmallString<100> SelectorName; |
| 1663 | SelectorName = "set"; |
| 1664 | SelectorName.append(Name->getName(), Name->getName()+Name->getLength()); |
| 1665 | SelectorName[3] = toupper(SelectorName[3]); |
| 1666 | return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]); |
| 1667 | } |
| 1668 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1669 | Action::OwningExprResult |
| 1670 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1671 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 1672 | IdentifierInfo &Member) { |
| 1673 | Expr *BaseExpr = static_cast<Expr *>(Base.release()); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1674 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Perform default conversions. |
| 1677 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1678 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1679 | QualType BaseType = BaseExpr->getType(); |
| 1680 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1681 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1682 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1683 | // must have pointer type, and the accessed type is the pointee. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | if (OpKind == tok::arrow) { |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1685 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1686 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 1687 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1688 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 1689 | MemberLoc, Member)); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1690 | else |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1691 | return ExprError(Diag(MemberLoc, |
| 1692 | diag::err_typecheck_member_reference_arrow) |
| 1693 | << BaseType << BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1694 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1695 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1696 | // Handle field access to simple records. This also handles access to fields |
| 1697 | // of the ObjC 'id' struct. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1698 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1699 | RecordDecl *RDecl = RTy->getDecl(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1700 | if (DiagnoseIncompleteType(OpLoc, BaseType, |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1701 | diag::err_typecheck_incomplete_tag, |
| 1702 | BaseExpr->getSourceRange())) |
| 1703 | return ExprError(); |
| 1704 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1705 | // The record definition is complete, now make sure the member is valid. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1706 | // FIXME: Qualified name lookup for C++ is a bit more complicated |
| 1707 | // than this. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1708 | LookupResult Result |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1709 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1710 | LookupMemberName, false); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1711 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1712 | NamedDecl *MemberDecl = 0; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1713 | if (!Result) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1714 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 1715 | << &Member << BaseExpr->getSourceRange()); |
| 1716 | else if (Result.isAmbiguous()) { |
| 1717 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 1718 | MemberLoc, BaseExpr->getSourceRange()); |
| 1719 | return ExprError(); |
| 1720 | } else |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1721 | MemberDecl = Result; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1723 | // If the decl being referenced had an error, return an error for this |
| 1724 | // sub-expr without emitting another error, in order to avoid cascading |
| 1725 | // error cases. |
| 1726 | if (MemberDecl->isInvalidDecl()) |
| 1727 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1728 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1729 | // Check the use of this field |
| 1730 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 1731 | return ExprError(); |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1733 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1734 | // We may have found a field within an anonymous union or struct |
| 1735 | // (C++ [class.union]). |
| 1736 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1737 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1738 | BaseExpr, OpLoc); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1740 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1741 | // FIXME: Handle address space modifiers |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1742 | QualType MemberType = FD->getType(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1743 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 1744 | MemberType = Ref->getPointeeType(); |
| 1745 | else { |
| 1746 | unsigned combinedQualifiers = |
| 1747 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1748 | if (FD->isMutable()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1749 | combinedQualifiers &= ~QualType::Const; |
| 1750 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1751 | } |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1752 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1753 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 1754 | MemberLoc, MemberType)); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1755 | } else if (CXXClassVarDecl *Var = dyn_cast<CXXClassVarDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1756 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1757 | Var, MemberLoc, |
| 1758 | Var->getType().getNonReferenceType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1759 | else if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1760 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1761 | MemberFn, MemberLoc, MemberFn->getType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1762 | else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1763 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1764 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1765 | MemberLoc, Context.OverloadTy)); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1766 | else if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1767 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 1768 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1769 | else if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1770 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 1771 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1772 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1773 | // We found a declaration kind that we didn't expect. This is a |
| 1774 | // generic error message that tells the user that she can't refer |
| 1775 | // to this member with '.' or '->'. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1776 | return ExprError(Diag(MemberLoc, |
| 1777 | diag::err_typecheck_member_reference_unknown) |
| 1778 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1779 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1781 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 1782 | // (*Obj).ivar. |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1783 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1784 | ObjCInterfaceDecl *ClassDeclared; |
| 1785 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member, |
| 1786 | ClassDeclared)) { |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1787 | // If the decl being referenced had an error, return an error for this |
| 1788 | // sub-expr without emitting another error, in order to avoid cascading |
| 1789 | // error cases. |
| 1790 | if (IV->isInvalidDecl()) |
| 1791 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1792 | |
| 1793 | // Check whether we can reference this field. |
| 1794 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 1795 | return ExprError(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1796 | if (IV->getAccessControl() != ObjCIvarDecl::Public) { |
| 1797 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 1798 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1799 | ClassOfMethodDecl = MD->getClassInterface(); |
| 1800 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 1801 | if (ClassDeclared != IFTy->getDecl() || |
| 1802 | ClassOfMethodDecl != ClassDeclared) |
| 1803 | Diag(MemberLoc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1804 | } |
| 1805 | // @protected |
| 1806 | else if (!IFTy->getDecl()->isSuperClassOf(ClassOfMethodDecl)) |
| 1807 | Diag(MemberLoc, diag::error_protected_ivar_access) << IV->getDeclName(); |
| 1808 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1809 | |
| 1810 | ObjCIvarRefExpr *MRef= new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1811 | MemberLoc, BaseExpr, |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1812 | OpKind == tok::arrow); |
| 1813 | Context.setFieldDecl(IFTy->getDecl(), IV, MRef); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1814 | return Owned(MRef); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1815 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1816 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1817 | << IFTy->getDecl()->getDeclName() << &Member |
| 1818 | << BaseExpr->getSourceRange()); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1819 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1820 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1821 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 1822 | // pointer to a (potentially qualified) interface type. |
| 1823 | const PointerType *PTy; |
| 1824 | const ObjCInterfaceType *IFTy; |
| 1825 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 1826 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 1827 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1828 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1829 | // Search for a declared property first. |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1830 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1831 | // Check whether we can reference this property. |
| 1832 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1833 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1834 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1835 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1836 | MemberLoc, BaseExpr)); |
| 1837 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1838 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1839 | // Check protocols on qualified interfaces. |
Chris Lattner | 9baefc2 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 1840 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 1841 | E = IFTy->qual_end(); I != E; ++I) |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1842 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1843 | // Check whether we can reference this property. |
| 1844 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1845 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1846 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1847 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1848 | MemberLoc, BaseExpr)); |
| 1849 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1850 | |
| 1851 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1852 | // selector is implemented. |
| 1853 | |
| 1854 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1855 | // shared with the code in ActOnInstanceMessage. |
| 1856 | |
| 1857 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1858 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1859 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1860 | // If this reference is in an @implementation, check for 'private' methods. |
| 1861 | if (!Getter) |
| 1862 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1863 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1864 | if (ObjCImplementationDecl *ImpDecl = |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1865 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1866 | Getter = ImpDecl->getInstanceMethod(Sel); |
| 1867 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 1868 | // Look through local category implementations associated with the class. |
| 1869 | if (!Getter) { |
| 1870 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 1871 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1872 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 1873 | } |
| 1874 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1875 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1876 | // Check if we can reference this property. |
| 1877 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1878 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1879 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1880 | // If we found a getter then this may be a valid dot-reference, we |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1881 | // will look for the matching setter, in case it is needed. |
| 1882 | IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), |
| 1883 | &Member); |
| 1884 | Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); |
| 1885 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 1886 | if (!Setter) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1887 | // If this reference is in an @implementation, also check for 'private' |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1888 | // methods. |
| 1889 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1890 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1891 | if (ObjCImplementationDecl *ImpDecl = |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1892 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1893 | Setter = ImpDecl->getInstanceMethod(SetterSel); |
| 1894 | } |
| 1895 | // Look through local category implementations associated with the class. |
| 1896 | if (!Setter) { |
| 1897 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 1898 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1899 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(SetterSel); |
| 1900 | } |
| 1901 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1902 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1903 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1904 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1905 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1906 | // FIXME: we must check that the setter has property type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1907 | return Owned(new (Context) ObjCKVCRefExpr(Getter, Getter->getResultType(), |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1908 | Setter, MemberLoc, BaseExpr)); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1909 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1910 | |
| 1911 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1912 | << &Member << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 1913 | } |
Steve Naroff | 18bc164 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 1914 | // Handle properties on qualified "id" protocols. |
| 1915 | const ObjCQualifiedIdType *QIdTy; |
| 1916 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 1917 | // Check protocols on qualified interfaces. |
| 1918 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1919 | E = QIdTy->qual_end(); I != E; ++I) { |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1920 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1921 | // Check the use of this declaration |
| 1922 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1923 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1924 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1925 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1926 | MemberLoc, BaseExpr)); |
| 1927 | } |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1928 | // Also must look for a getter name which uses property syntax. |
| 1929 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1930 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1931 | // Check the use of this method. |
| 1932 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 1933 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1934 | |
| 1935 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1936 | OMD->getResultType(), OMD, OpLoc, MemberLoc, NULL, 0)); |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1939 | |
| 1940 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1941 | << &Member << BaseType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1942 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1943 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 1944 | if (BaseType->isExtVectorType()) { |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1945 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 1946 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1947 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1948 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1949 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1950 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1951 | |
| 1952 | return ExprError(Diag(MemberLoc, |
| 1953 | diag::err_typecheck_member_reference_struct_union) |
| 1954 | << BaseType << BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1957 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 1958 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 1959 | /// function prototype Proto. Call is the call expression itself, and |
| 1960 | /// Fn is the function expression. For a C++ member function, this |
| 1961 | /// routine does not attempt to convert the object argument. Returns |
| 1962 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1963 | bool |
| 1964 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1965 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1966 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1967 | Expr **Args, unsigned NumArgs, |
| 1968 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1969 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1970 | // assignment, to the types of the corresponding parameter, ... |
| 1971 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1972 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1973 | bool Invalid = false; |
| 1974 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1975 | // If too few arguments are available (and we don't have default |
| 1976 | // arguments for the remaining parameters), don't make the call. |
| 1977 | if (NumArgs < NumArgsInProto) { |
| 1978 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 1979 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 1980 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 1981 | // Use default arguments for missing arguments |
| 1982 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1983 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | // If too many are passed and not variadic, error on the extras and drop |
| 1987 | // them. |
| 1988 | if (NumArgs > NumArgsInProto) { |
| 1989 | if (!Proto->isVariadic()) { |
| 1990 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 1991 | diag::err_typecheck_call_too_many_args) |
| 1992 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 1993 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 1994 | Args[NumArgs-1]->getLocEnd()); |
| 1995 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1996 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1997 | Invalid = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1998 | } |
| 1999 | NumArgsToCheck = NumArgsInProto; |
| 2000 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2002 | // Continue to check argument types (even if we have too few/many args). |
| 2003 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2004 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2006 | Expr *Arg; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2007 | if (i < NumArgs) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2008 | Arg = Args[i]; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Pass the argument. |
| 2011 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2012 | return true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2013 | } else |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2014 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2015 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2016 | QualType ArgType = Arg->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2018 | Call->setArg(i, Arg); |
| 2019 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2020 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2021 | // If this is a variadic call, handle args passed through "...". |
| 2022 | if (Proto->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2023 | VariadicCallType CallType = VariadicFunction; |
| 2024 | if (Fn->getType()->isBlockPointerType()) |
| 2025 | CallType = VariadicBlock; // Block |
| 2026 | else if (isa<MemberExpr>(Fn)) |
| 2027 | CallType = VariadicMethod; |
| 2028 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2029 | // Promote the arguments (C99 6.5.2.2p7). |
| 2030 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2031 | Expr *Arg = Args[i]; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2032 | DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2033 | Call->setArg(i, Arg); |
| 2034 | } |
| 2035 | } |
| 2036 | |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2037 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2040 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2041 | /// This provides the location of the left/right parens and a list of comma |
| 2042 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2043 | Action::OwningExprResult |
| 2044 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2045 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2046 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2047 | unsigned NumArgs = args.size(); |
| 2048 | Expr *Fn = static_cast<Expr *>(fn.release()); |
| 2049 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 2050 | assert(Fn && "no function call expression"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2051 | FunctionDecl *FDecl = NULL; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2052 | DeclarationName UnqualifiedName; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2053 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2054 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2055 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2056 | // in which case we won't do any semantic analysis now. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2057 | // FIXME: Will need to cache the results of name lookup (including ADL) in Fn. |
| 2058 | bool Dependent = false; |
| 2059 | if (Fn->isTypeDependent()) |
| 2060 | Dependent = true; |
| 2061 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2062 | Dependent = true; |
| 2063 | |
| 2064 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2065 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2066 | Context.DependentTy, RParenLoc)); |
| 2067 | |
| 2068 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2069 | if (Fn->getType()->isRecordType()) |
| 2070 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2071 | CommaLocs, RParenLoc)); |
| 2072 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2073 | // Determine whether this is a call to a member function. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2074 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 2075 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 2076 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2077 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2078 | CommaLocs, RParenLoc)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2081 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2082 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2083 | Expr *FnExpr = Fn; |
| 2084 | bool ADL = true; |
| 2085 | while (true) { |
| 2086 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2087 | FnExpr = IcExpr->getSubExpr(); |
| 2088 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2089 | // Parentheses around a function disable ADL |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2090 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2091 | ADL = false; |
| 2092 | FnExpr = PExpr->getSubExpr(); |
| 2093 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2094 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2095 | == UnaryOperator::AddrOf) { |
| 2096 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2097 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 2098 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2099 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 2100 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2101 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2102 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2103 | UnqualifiedName = DepName->getName(); |
| 2104 | break; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2105 | } else { |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2106 | // Any kind of name that does not refer to a declaration (or |
| 2107 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2108 | ADL = false; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2111 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2113 | OverloadedFunctionDecl *Ovl = 0; |
| 2114 | if (DRExpr) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2115 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2116 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
| 2117 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2118 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2119 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2120 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2121 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2122 | ADL = false; |
| 2123 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2124 | // We don't perform ADL in C. |
| 2125 | if (!getLangOptions().CPlusPlus) |
| 2126 | ADL = false; |
| 2127 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2128 | if (Ovl || ADL) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2129 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 2130 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2131 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 2132 | if (!FDecl) |
| 2133 | return ExprError(); |
| 2134 | |
| 2135 | // Update Fn to refer to the actual function selected. |
| 2136 | Expr *NewFn = 0; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2137 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2138 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2139 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2140 | QDRExpr->getLocation(), |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2141 | false, false, |
| 2142 | QDRExpr->getSourceRange().getBegin()); |
| 2143 | else |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2144 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2145 | Fn->getSourceRange().getBegin()); |
| 2146 | Fn->Destroy(Context); |
| 2147 | Fn = NewFn; |
| 2148 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2149 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2150 | |
| 2151 | // Promote the function operand. |
| 2152 | UsualUnaryConversions(Fn); |
| 2153 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2154 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2155 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2156 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2157 | Args, NumArgs, |
| 2158 | Context.BoolTy, |
| 2159 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2160 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2161 | const FunctionType *FuncT; |
| 2162 | if (!Fn->getType()->isBlockPointerType()) { |
| 2163 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2164 | // have type pointer to function". |
| 2165 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2166 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2167 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2168 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2169 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2170 | } else { // This is a block call. |
| 2171 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2172 | getAsFunctionType(); |
| 2173 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2174 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2175 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2176 | << Fn->getType() << Fn->getSourceRange()); |
| 2177 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2178 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2179 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2180 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2181 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2182 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2183 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2184 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2185 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2186 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2187 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2188 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2189 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2190 | Expr *Arg = Args[i]; |
| 2191 | DefaultArgumentPromotion(Arg); |
| 2192 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2193 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2194 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2196 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2197 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2198 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2199 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2200 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2201 | // Do special checking on direct calls to functions. |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2202 | if (FDecl) |
| 2203 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2204 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2205 | return Owned(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2208 | Action::OwningExprResult |
| 2209 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2210 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2211 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2212 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 2213 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2214 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2215 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2216 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2217 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2218 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2219 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2220 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2221 | } else if (DiagnoseIncompleteType(LParenLoc, literalType, |
| 2222 | diag::err_typecheck_decl_incomplete_type, |
| 2223 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2224 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2225 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2226 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2227 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2228 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2230 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2231 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2232 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2233 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2234 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2235 | InitExpr.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2236 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2237 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2240 | Action::OwningExprResult |
| 2241 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
| 2242 | InitListDesignations &Designators, |
| 2243 | SourceLocation RBraceLoc) { |
| 2244 | unsigned NumInit = initlist.size(); |
| 2245 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2246 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2247 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2248 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2249 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2250 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2251 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2252 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2253 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2256 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 58d5ebb | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2257 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2258 | UsualUnaryConversions(castExpr); |
| 2259 | |
| 2260 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2261 | // type needs to be scalar. |
| 2262 | if (castType->isVoidType()) { |
| 2263 | // Cast to void allows any expr type. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2264 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2265 | // We can't check any more until template instantiation time. |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2266 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2267 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2268 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2269 | (castType->isStructureType() || castType->isUnionType())) { |
| 2270 | // GCC struct/union extension: allow cast to self. |
| 2271 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2272 | << castType << castExpr->getSourceRange(); |
| 2273 | } else if (castType->isUnionType()) { |
| 2274 | // GCC cast to union extension |
| 2275 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2276 | RecordDecl::field_iterator Field, FieldEnd; |
| 2277 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 2278 | Field != FieldEnd; ++Field) { |
| 2279 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2280 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2281 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2282 | << castExpr->getSourceRange(); |
| 2283 | break; |
| 2284 | } |
| 2285 | } |
| 2286 | if (Field == FieldEnd) |
| 2287 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2288 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2289 | } else { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2290 | // Reject any other conversions to non-scalar types. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2291 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2292 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2293 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2294 | } else if (!castExpr->getType()->isScalarType() && |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2295 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2296 | return Diag(castExpr->getLocStart(), |
| 2297 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2298 | << castExpr->getType() << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2299 | } else if (castExpr->getType()->isVectorType()) { |
| 2300 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2301 | return true; |
| 2302 | } else if (castType->isVectorType()) { |
| 2303 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2304 | return true; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame^] | 2305 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
| 2306 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2307 | } |
| 2308 | return false; |
| 2309 | } |
| 2310 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2311 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2312 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2313 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2314 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2315 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2316 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2317 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2318 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2319 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2320 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2321 | } else |
| 2322 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2323 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2324 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2325 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2326 | return false; |
| 2327 | } |
| 2328 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2329 | Action::OwningExprResult |
| 2330 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2331 | SourceLocation RParenLoc, ExprArg Op) { |
| 2332 | assert((Ty != 0) && (Op.get() != 0) && |
| 2333 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2334 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2335 | Expr *castExpr = static_cast<Expr*>(Op.release()); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2336 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2337 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2338 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2339 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2340 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2341 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2344 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 2345 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2346 | /// C99 6.5.15 |
| 2347 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 2348 | SourceLocation QuestionLoc) { |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2349 | UsualUnaryConversions(Cond); |
| 2350 | UsualUnaryConversions(LHS); |
| 2351 | UsualUnaryConversions(RHS); |
| 2352 | QualType CondTy = Cond->getType(); |
| 2353 | QualType LHSTy = LHS->getType(); |
| 2354 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2355 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2356 | // first, check the condition. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2357 | if (!Cond->isTypeDependent()) { |
| 2358 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 2359 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 2360 | << CondTy; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2361 | return QualType(); |
| 2362 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2363 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2364 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2365 | // Now check the two expressions. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2366 | if ((LHS && LHS->isTypeDependent()) || (RHS && RHS->isTypeDependent())) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2367 | return Context.DependentTy; |
| 2368 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2369 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2370 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2371 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 2372 | UsualArithmeticConversions(LHS, RHS); |
| 2373 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2374 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2375 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2376 | // If both operands are the same structure or union type, the result is that |
| 2377 | // type. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2378 | if (const RecordType *LHSRT = LHSTy->getAsRecordType()) { // C99 6.5.15p3 |
| 2379 | if (const RecordType *RHSRT = RHSTy->getAsRecordType()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2380 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2381 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2382 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2383 | return LHSTy.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2384 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2385 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2386 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2387 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2388 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 2389 | if (!LHSTy->isVoidType()) |
| 2390 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2391 | << RHS->getSourceRange(); |
| 2392 | if (!RHSTy->isVoidType()) |
| 2393 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2394 | << LHS->getSourceRange(); |
| 2395 | ImpCastExprToType(LHS, Context.VoidTy); |
| 2396 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2397 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2398 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2399 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2400 | // the type of the other operand." |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2401 | if ((LHSTy->isPointerType() || LHSTy->isBlockPointerType() || |
| 2402 | Context.isObjCObjectPointerType(LHSTy)) && |
| 2403 | RHS->isNullPointerConstant(Context)) { |
| 2404 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 2405 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2406 | } |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2407 | if ((RHSTy->isPointerType() || RHSTy->isBlockPointerType() || |
| 2408 | Context.isObjCObjectPointerType(RHSTy)) && |
| 2409 | LHS->isNullPointerConstant(Context)) { |
| 2410 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 2411 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2412 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | bd57d36 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2414 | // Handle the case where both operands are pointers before we handle null |
| 2415 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2416 | if (const PointerType *LHSPT = LHSTy->getAsPointerType()) { // C99 6.5.15p3,6 |
| 2417 | if (const PointerType *RHSPT = RHSTy->getAsPointerType()) { |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2418 | // get the "pointed to" types |
| 2419 | QualType lhptee = LHSPT->getPointeeType(); |
| 2420 | QualType rhptee = RHSPT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2422 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2423 | if (lhptee->isVoidType() && |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2424 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2425 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2426 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2427 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2428 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2429 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2430 | return destType; |
| 2431 | } |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2432 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2433 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2434 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2435 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2436 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2437 | return destType; |
| 2438 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2439 | |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2440 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
Chris Lattner | b4650c1 | 2009-02-19 04:44:58 +0000 | [diff] [blame] | 2441 | // Two identical pointer types are always compatible. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2442 | return LHSTy; |
| 2443 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2445 | QualType compositeType = LHSTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2446 | |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2447 | // If either type is an Objective-C object type then check |
| 2448 | // compatibility according to Objective-C. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2449 | if (Context.isObjCObjectPointerType(LHSTy) || |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2450 | Context.isObjCObjectPointerType(RHSTy)) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2451 | // If both operands are interfaces and either operand can be |
| 2452 | // assigned to the other, use that type as the composite |
| 2453 | // type. This allows |
| 2454 | // xxx ? (A*) a : (B*) b |
| 2455 | // where B is a subclass of A. |
| 2456 | // |
| 2457 | // Additionally, as for assignment, if either type is 'id' |
| 2458 | // allow silent coercion. Finally, if the types are |
| 2459 | // incompatible then make sure to use 'id' as the composite |
| 2460 | // type so the result is acceptable for sending messages to. |
| 2461 | |
Steve Naroff | 1fd0361 | 2009-02-12 19:05:07 +0000 | [diff] [blame] | 2462 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2463 | // It could return the composite type. |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2464 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2465 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2466 | if (LHSIface && RHSIface && |
| 2467 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2468 | compositeType = LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2469 | } else if (LHSIface && RHSIface && |
Douglas Gregor | 7ffd0de | 2008-11-26 06:43:45 +0000 | [diff] [blame] | 2470 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2471 | compositeType = RHSTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2472 | } else if (Context.isObjCIdStructType(lhptee) || |
| 2473 | Context.isObjCIdStructType(rhptee)) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2474 | compositeType = Context.getObjCIdType(); |
| 2475 | } else { |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2476 | Diag(QuestionLoc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2477 | << LHSTy << RHSTy |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2478 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2479 | QualType incompatTy = Context.getObjCIdType(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2480 | ImpCastExprToType(LHS, incompatTy); |
| 2481 | ImpCastExprToType(RHS, incompatTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2482 | return incompatTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2483 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2484 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2485 | rhptee.getUnqualifiedType())) { |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2486 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 2487 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2488 | // In this situation, we assume void* type. No especially good |
| 2489 | // reason, but this is what gcc does, and we do have to pick |
| 2490 | // to get a consistent AST. |
| 2491 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2492 | ImpCastExprToType(LHS, incompatTy); |
| 2493 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 2494 | return incompatTy; |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2495 | } |
| 2496 | // The pointer types are compatible. |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2497 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 2498 | // differently qualified versions of compatible types, the result type is |
| 2499 | // a pointer to an appropriately qualified version of the *composite* |
| 2500 | // type. |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2501 | // FIXME: Need to calculate the composite type. |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2502 | // FIXME: Need to add qualifiers |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2503 | ImpCastExprToType(LHS, compositeType); |
| 2504 | ImpCastExprToType(RHS, compositeType); |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2505 | return compositeType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2506 | } |
| 2507 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2508 | |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2509 | // Selection between block pointer types is ok as long as they are the same. |
| 2510 | if (LHSTy->isBlockPointerType() && RHSTy->isBlockPointerType() && |
| 2511 | Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) |
| 2512 | return LHSTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2513 | |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2514 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 2515 | // evaluates to "struct objc_object *" (and is handled above when comparing |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2516 | // id with statically typed objects). |
| 2517 | if (LHSTy->isObjCQualifiedIdType() || RHSTy->isObjCQualifiedIdType()) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2518 | // GCC allows qualified id and any Objective-C type to devolve to |
| 2519 | // id. Currently localizing to here until clear this should be |
| 2520 | // part of ObjCQualifiedIdTypesAreCompatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2521 | if (ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true) || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2522 | (LHSTy->isObjCQualifiedIdType() && |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2523 | Context.isObjCObjectPointerType(RHSTy)) || |
| 2524 | (RHSTy->isObjCQualifiedIdType() && |
| 2525 | Context.isObjCObjectPointerType(LHSTy))) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2526 | // FIXME: This is not the correct composite type. This only |
| 2527 | // happens to work because id can more or less be used anywhere, |
| 2528 | // however this may change the type of method sends. |
| 2529 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 2530 | // (confusing) incompatible comparison warnings in some |
| 2531 | // cases. Investigate. |
| 2532 | QualType compositeType = Context.getObjCIdType(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2533 | ImpCastExprToType(LHS, compositeType); |
| 2534 | ImpCastExprToType(RHS, compositeType); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2535 | return compositeType; |
| 2536 | } |
| 2537 | } |
| 2538 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2539 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2540 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2541 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2542 | return QualType(); |
| 2543 | } |
| 2544 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2545 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2546 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2547 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 2548 | SourceLocation ColonLoc, |
| 2549 | ExprArg Cond, ExprArg LHS, |
| 2550 | ExprArg RHS) { |
| 2551 | Expr *CondExpr = (Expr *) Cond.get(); |
| 2552 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2553 | |
| 2554 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 2555 | // was the condition. |
| 2556 | bool isLHSNull = LHSExpr == 0; |
| 2557 | if (isLHSNull) |
| 2558 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2559 | |
| 2560 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 2561 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2562 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2563 | return ExprError(); |
| 2564 | |
| 2565 | Cond.release(); |
| 2566 | LHS.release(); |
| 2567 | RHS.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2568 | return Owned(new (Context) ConditionalOperator(CondExpr, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2569 | isLHSNull ? 0 : LHSExpr, |
| 2570 | RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2573 | |
| 2574 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2575 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2576 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 2577 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 2578 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2579 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2580 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 2581 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2582 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2583 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2584 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 2585 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2586 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2587 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2588 | lhptee = Context.getCanonicalType(lhptee); |
| 2589 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2590 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2591 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2592 | |
| 2593 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 2594 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 2595 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2596 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2597 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2598 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2599 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2600 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 2601 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2602 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2603 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2604 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2605 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2607 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2608 | assert(rhptee->isFunctionType()); |
| 2609 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2610 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2611 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2612 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2613 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2614 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2615 | |
| 2616 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2617 | assert(lhptee->isFunctionType()); |
| 2618 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2619 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2620 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2621 | // unqualified versions of compatible types, ... |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2622 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2623 | rhptee.getUnqualifiedType())) |
| 2624 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2625 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2628 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 2629 | /// block pointer types are compatible or whether a block and normal pointer |
| 2630 | /// are compatible. It is more restrict than comparing two function pointer |
| 2631 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2632 | Sema::AssignConvertType |
| 2633 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2634 | QualType rhsType) { |
| 2635 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2636 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2637 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 2638 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2639 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 2640 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2641 | // make sure we operate on the canonical type |
| 2642 | lhptee = Context.getCanonicalType(lhptee); |
| 2643 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2644 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2645 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2646 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2647 | // For blocks we enforce that qualifiers are identical. |
| 2648 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 2649 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2650 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2651 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2652 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2653 | return ConvTy; |
| 2654 | } |
| 2655 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2656 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 2657 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2658 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 2659 | /// |
| 2660 | /// int a, *pint; |
| 2661 | /// short *pshort; |
| 2662 | /// struct foo *pfoo; |
| 2663 | /// |
| 2664 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 2665 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 2666 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 2667 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 2668 | /// |
| 2669 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2670 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2671 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2672 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2673 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2674 | // Get canonical types. We're not formatting these types, just comparing |
| 2675 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2676 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 2677 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2678 | |
| 2679 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 2680 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2682 | // If the left-hand side is a reference type, then we are in a |
| 2683 | // (rare!) case where we've allowed the use of references in C, |
| 2684 | // e.g., as a parameter type in a built-in function. In this case, |
| 2685 | // just make sure that the type referenced is compatible with the |
| 2686 | // right-hand side type. The caller is responsible for adjusting |
| 2687 | // lhsType so that the resulting expression does not have reference |
| 2688 | // type. |
| 2689 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 2690 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 2691 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2692 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2693 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2695 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 2696 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2697 | return Compatible; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2698 | // Relax integer conversions like we do for pointers below. |
| 2699 | if (rhsType->isIntegerType()) |
| 2700 | return IntToPointer; |
| 2701 | if (lhsType->isIntegerType()) |
| 2702 | return PointerToInt; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2703 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2704 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2705 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2706 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2707 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2708 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 2709 | if (LV->getElementType() == rhsType) |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2710 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2711 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2712 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2713 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2714 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2715 | if (getLangOptions().LaxVectorConversions && |
| 2716 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2717 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2718 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2719 | } |
| 2720 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2721 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2722 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2723 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2724 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2725 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2726 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2727 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2728 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2730 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2731 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2732 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2733 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2734 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2735 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Treat block pointers as objects. |
| 2738 | if (getLangOptions().ObjC1 && |
| 2739 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2740 | return Compatible; |
| 2741 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2742 | return Incompatible; |
| 2743 | } |
| 2744 | |
| 2745 | if (isa<BlockPointerType>(lhsType)) { |
| 2746 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 2747 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2748 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2749 | // Treat block pointers as objects. |
| 2750 | if (getLangOptions().ObjC1 && |
| 2751 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2752 | return Compatible; |
| 2753 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2754 | if (rhsType->isBlockPointerType()) |
| 2755 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2756 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2757 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 2758 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2759 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2760 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2761 | return Incompatible; |
| 2762 | } |
| 2763 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2764 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2765 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2766 | if (lhsType == Context.BoolTy) |
| 2767 | return Compatible; |
| 2768 | |
| 2769 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2770 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2771 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2772 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2773 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2774 | |
| 2775 | if (isa<BlockPointerType>(lhsType) && |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2776 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2777 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2778 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2779 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2780 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2781 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2782 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2783 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | } |
| 2785 | return Incompatible; |
| 2786 | } |
| 2787 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2788 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2789 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2790 | if (getLangOptions().CPlusPlus) { |
| 2791 | if (!lhsType->isRecordType()) { |
| 2792 | // C++ 5.17p3: If the left operand is not of class type, the |
| 2793 | // expression is implicitly converted (C++ 4) to the |
| 2794 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2795 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 2796 | "assigning")) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2797 | return Incompatible; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2798 | else |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2799 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 2803 | // structures. |
| 2804 | } |
| 2805 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2806 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 2807 | // a null pointer constant. |
Steve Naroff | f7f52e7 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 2808 | if ((lhsType->isPointerType() || |
| 2809 | lhsType->isObjCQualifiedIdType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2810 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | 9d3185e | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 2811 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2812 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2813 | return Compatible; |
| 2814 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2815 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2816 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2817 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2818 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2819 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2820 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2821 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2822 | if (!lhsType->isReferenceType()) |
| 2823 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2825 | Sema::AssignConvertType result = |
| 2826 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2827 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2828 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 2829 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2830 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 2831 | // so that we can use references in built-in functions even in C. |
| 2832 | // The getNonReferenceType() call makes sure that the resulting expression |
| 2833 | // does not have reference type. |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2834 | if (rExpr->getType() != lhsType) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2835 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2836 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2839 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2840 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 2841 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 2842 | } |
| 2843 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2844 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2845 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 2846 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2847 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2848 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2851 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2852 | Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2853 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2854 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2855 | QualType lhsType = |
| 2856 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 2857 | QualType rhsType = |
| 2858 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2859 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2860 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2861 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2862 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2863 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2864 | // Handle the case of a vector & extvector type of the same size and element |
| 2865 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2866 | if (getLangOptions().LaxVectorConversions) { |
| 2867 | // FIXME: Should we warn here? |
| 2868 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2869 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 2870 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2871 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2872 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2876 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2877 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 2878 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2879 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2880 | QualType eltType = V->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2881 | |
| 2882 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2883 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 2884 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2885 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2886 | return lhsType; |
| 2887 | } |
| 2888 | } |
| 2889 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2890 | // If the rhs is an extended vector and the lhs is a scalar of the same type, |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2891 | // promote the lhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2892 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2893 | QualType eltType = V->getElementType(); |
| 2894 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2895 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2896 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 2897 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2898 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2899 | return rhsType; |
| 2900 | } |
| 2901 | } |
| 2902 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2903 | // You cannot convert between vector values of different size. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2904 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2905 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2906 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2907 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2910 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2911 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2912 | { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 2913 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2914 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2915 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2916 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2917 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2918 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2919 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2920 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2924 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2925 | { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 2926 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 2927 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 2928 | return CheckVectorOperands(Loc, lex, rex); |
| 2929 | return InvalidOperands(Loc, lex, rex); |
| 2930 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2931 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2932 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2933 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2934 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2935 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2936 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2940 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2941 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2942 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2943 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2944 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2945 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2946 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2947 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2948 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2949 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2950 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2951 | // Put any potential pointer into PExp |
| 2952 | Expr* PExp = lex, *IExp = rex; |
| 2953 | if (IExp->getType()->isPointerType()) |
| 2954 | std::swap(PExp, IExp); |
| 2955 | |
| 2956 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 2957 | if (IExp->getType()->isIntegerType()) { |
| 2958 | // Check for arithmetic on pointers to incomplete types |
| 2959 | if (!PTy->getPointeeType()->isObjectType()) { |
| 2960 | if (PTy->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2961 | if (getLangOptions().CPlusPlus) { |
| 2962 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 2963 | << lex->getSourceRange() << rex->getSourceRange(); |
| 2964 | return QualType(); |
| 2965 | } |
| 2966 | |
| 2967 | // GNU extension: arithmetic on pointer to void |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2968 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2969 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2970 | } else if (PTy->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2971 | if (getLangOptions().CPlusPlus) { |
| 2972 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2973 | << lex->getType() << lex->getSourceRange(); |
| 2974 | return QualType(); |
| 2975 | } |
| 2976 | |
| 2977 | // GNU extension: arithmetic on pointer to function |
| 2978 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2979 | << lex->getType() << lex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2980 | } else { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2981 | DiagnoseIncompleteType(Loc, PTy->getPointeeType(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2982 | diag::err_typecheck_arithmetic_incomplete_type, |
| 2983 | lex->getSourceRange(), SourceRange(), |
| 2984 | lex->getType()); |
| 2985 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2986 | } |
| 2987 | } |
| 2988 | return PExp->getType(); |
| 2989 | } |
| 2990 | } |
| 2991 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2992 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2995 | // C99 6.5.6 |
| 2996 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2997 | SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2998 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2999 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3000 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3001 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3003 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3004 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3005 | // Handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3006 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3007 | return compType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3008 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3009 | // Either ptr - int or ptr - ptr. |
| 3010 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3011 | QualType lpointee = LHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3012 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3013 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3014 | if (!lpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3015 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3016 | if (lpointee->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3017 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3018 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3019 | } else if (lpointee->isFunctionType()) { |
| 3020 | if (getLangOptions().CPlusPlus) { |
| 3021 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3022 | << lex->getType() << lex->getSourceRange(); |
| 3023 | return QualType(); |
| 3024 | } |
| 3025 | |
| 3026 | // GNU extension: arithmetic on pointer to function |
| 3027 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3028 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3029 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3030 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3031 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3032 | return QualType(); |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | // The result type of a pointer-int computation is the pointer type. |
| 3037 | if (rex->getType()->isIntegerType()) |
| 3038 | return lex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3040 | // Handle pointer-pointer subtractions. |
| 3041 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3042 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3043 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3044 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3045 | if (!rpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3046 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3047 | if (rpointee->isVoidType()) { |
| 3048 | if (!lpointee->isVoidType()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3049 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3050 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 0804888 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 3051 | } else if (rpointee->isFunctionType()) { |
| 3052 | if (getLangOptions().CPlusPlus) { |
| 3053 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3054 | << rex->getType() << rex->getSourceRange(); |
| 3055 | return QualType(); |
| 3056 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3057 | |
Douglas Gregor | 0804888 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 3058 | // GNU extension: arithmetic on pointer to function |
| 3059 | if (!lpointee->isFunctionType()) |
| 3060 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3061 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3062 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3063 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3064 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3065 | return QualType(); |
| 3066 | } |
| 3067 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3069 | // Pointee types must be compatible. |
Eli Friedman | f1c7b48 | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3070 | if (!Context.typesAreCompatible( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3071 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
Eli Friedman | f1c7b48 | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3072 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3073 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3074 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3075 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3076 | return QualType(); |
| 3077 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3078 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3079 | return Context.getPointerDiffType(); |
| 3080 | } |
| 3081 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3082 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3083 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3086 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3087 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3088 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3089 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 3090 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3091 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3092 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3093 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 3094 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 3095 | if (!isCompAssign) |
| 3096 | UsualUnaryConversions(lex); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3097 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3098 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3099 | // "The type of the result is that of the promoted left operand." |
| 3100 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3103 | // C99 6.5.8 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3104 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3105 | bool isRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3106 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3107 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3108 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3109 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 3110 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 3111 | UsualArithmeticConversions(lex, rex); |
| 3112 | else { |
| 3113 | UsualUnaryConversions(lex); |
| 3114 | UsualUnaryConversions(rex); |
| 3115 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3116 | QualType lType = lex->getType(); |
| 3117 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3118 | |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3119 | // For non-floating point types, check for self-comparisons of the form |
| 3120 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3121 | // often indicate logic errors in the program. |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3122 | if (!lType->isFloatingType()) { |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 3123 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3124 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3125 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3126 | Diag(Loc, diag::warn_selfcomparison); |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3127 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3128 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3129 | // The result of comparisons is 'bool' in C++, 'int' in C. |
| 3130 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy : Context.IntTy; |
| 3131 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3132 | if (isRelational) { |
| 3133 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3134 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3135 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3136 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3137 | if (lType->isFloatingType()) { |
| 3138 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3139 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3140 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3142 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3143 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3144 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3145 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3146 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3147 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3148 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3149 | // All of the following pointer related warnings are GCC extensions, except |
| 3150 | // when handling null pointer constants. One day, we can consider making them |
| 3151 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3152 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3153 | QualType LCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3154 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3155 | QualType RCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3156 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3157 | |
Steve Naroff | 66296cb | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3158 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3159 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3160 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3161 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3162 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3163 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3164 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3165 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3166 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3167 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3168 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3169 | // Handle block pointer types. |
| 3170 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 3171 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 3172 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3173 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3174 | if (!LHSIsNull && !RHSIsNull && |
| 3175 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3176 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3177 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3178 | } |
| 3179 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3180 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3181 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3182 | // Allow block pointers to be compared with null pointer constants. |
| 3183 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 3184 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 3185 | if (!LHSIsNull && !RHSIsNull) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3186 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3187 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3188 | } |
| 3189 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3190 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3191 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3192 | |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3193 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3194 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3195 | const PointerType *LPT = lType->getAsPointerType(); |
| 3196 | const PointerType *RPT = rType->getAsPointerType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3197 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3198 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3199 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3200 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3201 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3202 | if (!LPtrToVoid && !RPtrToVoid && |
| 3203 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3204 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3205 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3206 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3207 | return ResultTy; |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3208 | } |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3209 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3210 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 3211 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3212 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 3213 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3214 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3215 | } else { |
| 3216 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3217 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3218 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3219 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3220 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3221 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3222 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 3223 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3224 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3225 | rType->isIntegerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3226 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3227 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3228 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3229 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3230 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3231 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3232 | if (lType->isIntegerType() && |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3233 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3234 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3235 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3236 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3237 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3238 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3239 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3240 | // Handle block pointers. |
| 3241 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 3242 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3243 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3244 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3245 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3246 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3247 | } |
| 3248 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 3249 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3250 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3251 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3252 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3253 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3254 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3255 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3258 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3259 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3260 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 3261 | /// types. |
| 3262 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3263 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3264 | bool isRelational) { |
| 3265 | // Check to make sure we're operating on vectors of the same type and width, |
| 3266 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3267 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3268 | if (vType.isNull()) |
| 3269 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3270 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3271 | QualType lType = lex->getType(); |
| 3272 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3273 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3274 | // For non-floating point types, check for self-comparisons of the form |
| 3275 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3276 | // often indicate logic errors in the program. |
| 3277 | if (!lType->isFloatingType()) { |
| 3278 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3279 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 3280 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3281 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3282 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3283 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3284 | // Check for comparisons of floating point operands using != and ==. |
| 3285 | if (!isRelational && lType->isFloatingType()) { |
| 3286 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3287 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3288 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3289 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3290 | // Return the type for the comparison, which is the same as vector type for |
| 3291 | // integer vectors, or an integer type of identical size and number of |
| 3292 | // elements for floating point vectors. |
| 3293 | if (lType->isIntegerType()) |
| 3294 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3295 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3296 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3297 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3298 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3299 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3300 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 3301 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 3302 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3303 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3304 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3305 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 3306 | } |
| 3307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3308 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3309 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3310 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 3311 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3312 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3313 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3314 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3315 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3316 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3317 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3318 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3322 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3323 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3324 | UsualUnaryConversions(lex); |
| 3325 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3326 | |
Eli Friedman | 5773a6c | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 3327 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3328 | return Context.IntTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3329 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3332 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 3333 | /// is a read-only property; return true if so. A readonly property expression |
| 3334 | /// depends on various declarations and thus must be treated specially. |
| 3335 | /// |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3336 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3337 | { |
| 3338 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 3339 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 3340 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 3341 | QualType BaseType = PropExpr->getBase()->getType(); |
| 3342 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3343 | if (const ObjCInterfaceType *IFTy = |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3344 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 3345 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 3346 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 3347 | return true; |
| 3348 | } |
| 3349 | } |
| 3350 | return false; |
| 3351 | } |
| 3352 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3353 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 3354 | /// emit an error and return true. If so, return false. |
| 3355 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3356 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); |
| 3357 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 3358 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3359 | if (IsLV == Expr::MLV_Valid) |
| 3360 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3361 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3362 | unsigned Diag = 0; |
| 3363 | bool NeedType = false; |
| 3364 | switch (IsLV) { // C99 6.5.16p2 |
| 3365 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 3366 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3367 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3368 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 3369 | NeedType = true; |
| 3370 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3371 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3372 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 3373 | NeedType = true; |
| 3374 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 3375 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3376 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 3377 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3378 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3379 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 3380 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3381 | case Expr::MLV_IncompleteType: |
| 3382 | case Expr::MLV_IncompleteVoidType: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3383 | return S.DiagnoseIncompleteType(Loc, E->getType(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3384 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 3385 | E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3386 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3387 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 3388 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 3389 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3390 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 3391 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 3392 | case Expr::MLV_ReadonlyProperty: |
| 3393 | Diag = diag::error_readonly_property_assignment; |
| 3394 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 3395 | case Expr::MLV_NoSetterProperty: |
| 3396 | Diag = diag::error_nosetter_property_assignment; |
| 3397 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3398 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3399 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3400 | if (NeedType) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3401 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange(); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3402 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3403 | S.Diag(Loc, Diag) << E->getSourceRange(); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3404 | return true; |
| 3405 | } |
| 3406 | |
| 3407 | |
| 3408 | |
| 3409 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3410 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 3411 | SourceLocation Loc, |
| 3412 | QualType CompoundType) { |
| 3413 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 3414 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3415 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3416 | |
| 3417 | QualType LHSType = LHS->getType(); |
| 3418 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3419 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3420 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3421 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3422 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3423 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3424 | // Special case of NSObject attributes on c-style pointer types. |
| 3425 | if (ConvTy == IncompatiblePointer && |
| 3426 | ((Context.isObjCNSObjectType(LHSType) && |
| 3427 | Context.isObjCObjectPointerType(RHSType)) || |
| 3428 | (Context.isObjCNSObjectType(RHSType) && |
| 3429 | Context.isObjCObjectPointerType(LHSType)))) |
| 3430 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3431 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3432 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 3433 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 3434 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3435 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3436 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 3437 | RHSCheck = ICE->getSubExpr(); |
| 3438 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 3439 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 3440 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3441 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3442 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3443 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3444 | Diag(Loc, diag::warn_not_compound_assign) |
| 3445 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 3446 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3447 | } |
| 3448 | } else { |
| 3449 | // Compound assignment "x += y" |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3450 | ConvTy = CheckCompoundAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3451 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3452 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3453 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 3454 | RHS, "assigning")) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3455 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3456 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3457 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 3458 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3459 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3460 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 3461 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3462 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 3463 | // oprdu. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3464 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3467 | // C99 6.5.17 |
| 3468 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
| 3469 | // FIXME: what is required for LHS? |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 3471 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3472 | DefaultFunctionArrayConversion(RHS); |
| 3473 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 3476 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 3477 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3478 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 3479 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3480 | if (Op->isTypeDependent()) |
| 3481 | return Context.DependentTy; |
| 3482 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3483 | QualType ResType = Op->getType(); |
| 3484 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3485 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3486 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 3487 | // Decrement of bool is not allowed. |
| 3488 | if (!isInc) { |
| 3489 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 3490 | return QualType(); |
| 3491 | } |
| 3492 | // Increment of bool sets it to true, but is deprecated. |
| 3493 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 3494 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3495 | // OK! |
| 3496 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 3497 | // C99 6.5.2.4p2, 6.5.6p2 |
| 3498 | if (PT->getPointeeType()->isObjectType()) { |
| 3499 | // Pointer to object is ok! |
| 3500 | } else if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3501 | if (getLangOptions().CPlusPlus) { |
| 3502 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 3503 | << Op->getSourceRange(); |
| 3504 | return QualType(); |
| 3505 | } |
| 3506 | |
| 3507 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3508 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3509 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3510 | if (getLangOptions().CPlusPlus) { |
| 3511 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 3512 | << Op->getType() << Op->getSourceRange(); |
| 3513 | return QualType(); |
| 3514 | } |
| 3515 | |
| 3516 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3517 | << ResType << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3518 | return QualType(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3519 | } else { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3520 | DiagnoseIncompleteType(OpLoc, PT->getPointeeType(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3521 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3522 | Op->getSourceRange(), SourceRange(), |
| 3523 | ResType); |
| 3524 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3525 | } |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3526 | } else if (ResType->isComplexType()) { |
| 3527 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 3528 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3529 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3530 | } else { |
| 3531 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3532 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3533 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3534 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3535 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 3536 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3537 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3538 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3539 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3542 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3543 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3544 | /// where the declaration is needed for type checking. We only need to |
| 3545 | /// handle cases when the expression references a function designator |
| 3546 | /// or is an lvalue. Here are some examples: |
| 3547 | /// - &(x) => x |
| 3548 | /// - &*****f => f for f a function designator. |
| 3549 | /// - &s.xx => s |
| 3550 | /// - &s.zz[1].yy -> s, if zz is an array |
| 3551 | /// - *(x + 1) -> x, if x is an array |
| 3552 | /// - &"123"[2] -> 0 |
| 3553 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3554 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3555 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3556 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 3557 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3558 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3559 | case Stmt::MemberExprClass: |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3560 | // Fields cannot be declared with a 'register' storage class. |
| 3561 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3562 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3563 | return 0; |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3564 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3565 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3566 | // &X[4] and &4[X] refers to X if X is not a pointer. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3568 | NamedDecl *D = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Daniel Dunbar | 48d04ae | 2008-10-21 21:22:32 +0000 | [diff] [blame] | 3569 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Anders Carlsson | f2a4b84 | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 3570 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3571 | return 0; |
| 3572 | else |
| 3573 | return VD; |
| 3574 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3575 | case Stmt::UnaryOperatorClass: { |
| 3576 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3577 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3578 | switch(UO->getOpcode()) { |
| 3579 | case UnaryOperator::Deref: { |
| 3580 | // *(X + 1) refers to X if X is not a pointer. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3581 | if (NamedDecl *D = getPrimaryDecl(UO->getSubExpr())) { |
| 3582 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3583 | if (!VD || VD->getType()->isPointerType()) |
| 3584 | return 0; |
| 3585 | return VD; |
| 3586 | } |
| 3587 | return 0; |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3588 | } |
| 3589 | case UnaryOperator::Real: |
| 3590 | case UnaryOperator::Imag: |
| 3591 | case UnaryOperator::Extension: |
| 3592 | return getPrimaryDecl(UO->getSubExpr()); |
| 3593 | default: |
| 3594 | return 0; |
| 3595 | } |
| 3596 | } |
| 3597 | case Stmt::BinaryOperatorClass: { |
| 3598 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 3599 | |
| 3600 | // Handle cases involving pointer arithmetic. The result of an |
| 3601 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 3602 | |
| 3603 | // (x + n) or (n + x) => x |
| 3604 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 3605 | if (BO->getLHS()->getType()->isPointerType()) { |
| 3606 | return getPrimaryDecl(BO->getLHS()); |
| 3607 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 3608 | return getPrimaryDecl(BO->getRHS()); |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | return 0; |
| 3613 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3614 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3615 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3616 | case Stmt::ImplicitCastExprClass: |
| 3617 | // &X[4] when X is an array, has an implicit cast from array to pointer. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3618 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3619 | default: |
| 3620 | return 0; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3625 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3626 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3627 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3628 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3629 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3630 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3631 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 3632 | if (op->isTypeDependent()) |
| 3633 | return Context.DependentTy; |
| 3634 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3635 | if (getLangOptions().C99) { |
| 3636 | // Implement C99-only parts of addressof rules. |
| 3637 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 3638 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 3639 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 3640 | // (assuming the deref expression is valid). |
| 3641 | return uOp->getSubExpr()->getType(); |
| 3642 | } |
| 3643 | // Technically, there should be a check for array subscript |
| 3644 | // expressions here, but the result of one is always an lvalue anyway. |
| 3645 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3646 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 3647 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 3648 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3649 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3650 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 3651 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3652 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 3653 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3654 | return QualType(); |
| 3655 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3656 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1 |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 3657 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) { |
| 3658 | if (Field->isBitField()) { |
| 3659 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3660 | << "bit-field" << op->getSourceRange(); |
| 3661 | return QualType(); |
| 3662 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3663 | } |
| 3664 | // Check for Apple extension for accessing vector components. |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3665 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 3666 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3667 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3668 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3669 | return QualType(); |
| 3670 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3671 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3672 | // with the register storage-class specifier. |
| 3673 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 3674 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3675 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3676 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3677 | return QualType(); |
| 3678 | } |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3679 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3680 | return Context.OverloadTy; |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3681 | } else if (isa<FieldDecl>(dcl)) { |
| 3682 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 3683 | // Could be a pointer to member, though, if there is an explicit |
| 3684 | // scope qualifier for the class. |
| 3685 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3686 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3687 | if (Ctx && Ctx->isRecord()) |
| 3688 | return Context.getMemberPointerType(op->getType(), |
| 3689 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3690 | } |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3691 | } else if (isa<FunctionDecl>(dcl)) { |
| 3692 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3693 | // As above. |
| 3694 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3695 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3696 | if (Ctx && Ctx->isRecord()) |
| 3697 | return Context.getMemberPointerType(op->getType(), |
| 3698 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3699 | } |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3700 | } |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3701 | else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3702 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3703 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3704 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3705 | // If the operand has type "type", the result has type "pointer to type". |
| 3706 | return Context.getPointerType(op->getType()); |
| 3707 | } |
| 3708 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3709 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3710 | if (Op->isTypeDependent()) |
| 3711 | return Context.DependentTy; |
| 3712 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3713 | UsualUnaryConversions(Op); |
| 3714 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3715 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3716 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 3717 | // incomplete type or void. It would be possible to warn about dereferencing |
| 3718 | // a void pointer, but it's completely well-defined, and such a warning is |
| 3719 | // unlikely to catch any mistakes. |
| 3720 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3721 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3722 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3723 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3724 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3725 | return QualType(); |
| 3726 | } |
| 3727 | |
| 3728 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 3729 | tok::TokenKind Kind) { |
| 3730 | BinaryOperator::Opcode Opc; |
| 3731 | switch (Kind) { |
| 3732 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3733 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 3734 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3735 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 3736 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 3737 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 3738 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 3739 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 3740 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 3741 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 3742 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 3743 | case tok::less: Opc = BinaryOperator::LT; break; |
| 3744 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 3745 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 3746 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 3747 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 3748 | case tok::amp: Opc = BinaryOperator::And; break; |
| 3749 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 3750 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 3751 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 3752 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 3753 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 3754 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 3755 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 3756 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 3757 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 3758 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 3759 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 3760 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 3761 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 3762 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 3763 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 3764 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 3765 | } |
| 3766 | return Opc; |
| 3767 | } |
| 3768 | |
| 3769 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 3770 | tok::TokenKind Kind) { |
| 3771 | UnaryOperator::Opcode Opc; |
| 3772 | switch (Kind) { |
| 3773 | default: assert(0 && "Unknown unary op!"); |
| 3774 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 3775 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 3776 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 3777 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 3778 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 3779 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 3780 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 3781 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3782 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 3783 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 3784 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 3785 | } |
| 3786 | return Opc; |
| 3787 | } |
| 3788 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3789 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 3790 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 3791 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3792 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 3793 | unsigned Op, |
| 3794 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3795 | QualType ResultTy; // Result type of the binary operator. |
| 3796 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 3797 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
| 3798 | |
| 3799 | switch (Opc) { |
| 3800 | default: |
| 3801 | assert(0 && "Unknown binary expr!"); |
| 3802 | case BinaryOperator::Assign: |
| 3803 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 3804 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3805 | case BinaryOperator::PtrMemD: |
| 3806 | case BinaryOperator::PtrMemI: |
| 3807 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 3808 | Opc == BinaryOperator::PtrMemI); |
| 3809 | break; |
| 3810 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3811 | case BinaryOperator::Div: |
| 3812 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 3813 | break; |
| 3814 | case BinaryOperator::Rem: |
| 3815 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 3816 | break; |
| 3817 | case BinaryOperator::Add: |
| 3818 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 3819 | break; |
| 3820 | case BinaryOperator::Sub: |
| 3821 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 3822 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3823 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3824 | case BinaryOperator::Shr: |
| 3825 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 3826 | break; |
| 3827 | case BinaryOperator::LE: |
| 3828 | case BinaryOperator::LT: |
| 3829 | case BinaryOperator::GE: |
| 3830 | case BinaryOperator::GT: |
| 3831 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, true); |
| 3832 | break; |
| 3833 | case BinaryOperator::EQ: |
| 3834 | case BinaryOperator::NE: |
| 3835 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, false); |
| 3836 | break; |
| 3837 | case BinaryOperator::And: |
| 3838 | case BinaryOperator::Xor: |
| 3839 | case BinaryOperator::Or: |
| 3840 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 3841 | break; |
| 3842 | case BinaryOperator::LAnd: |
| 3843 | case BinaryOperator::LOr: |
| 3844 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 3845 | break; |
| 3846 | case BinaryOperator::MulAssign: |
| 3847 | case BinaryOperator::DivAssign: |
| 3848 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 3849 | if (!CompTy.isNull()) |
| 3850 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3851 | break; |
| 3852 | case BinaryOperator::RemAssign: |
| 3853 | CompTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 3854 | if (!CompTy.isNull()) |
| 3855 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3856 | break; |
| 3857 | case BinaryOperator::AddAssign: |
| 3858 | CompTy = CheckAdditionOperands(lhs, rhs, OpLoc, true); |
| 3859 | if (!CompTy.isNull()) |
| 3860 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3861 | break; |
| 3862 | case BinaryOperator::SubAssign: |
| 3863 | CompTy = CheckSubtractionOperands(lhs, rhs, OpLoc, true); |
| 3864 | if (!CompTy.isNull()) |
| 3865 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3866 | break; |
| 3867 | case BinaryOperator::ShlAssign: |
| 3868 | case BinaryOperator::ShrAssign: |
| 3869 | CompTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 3870 | if (!CompTy.isNull()) |
| 3871 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3872 | break; |
| 3873 | case BinaryOperator::AndAssign: |
| 3874 | case BinaryOperator::XorAssign: |
| 3875 | case BinaryOperator::OrAssign: |
| 3876 | CompTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 3877 | if (!CompTy.isNull()) |
| 3878 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3879 | break; |
| 3880 | case BinaryOperator::Comma: |
| 3881 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 3882 | break; |
| 3883 | } |
| 3884 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3885 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3886 | if (CompTy.isNull()) |
| 3887 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 3888 | else |
| 3889 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3890 | CompTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3893 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3894 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 3895 | tok::TokenKind Kind, |
| 3896 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3898 | Expr *lhs = (Expr *)LHS.release(), *rhs = (Expr*)RHS.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3899 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3900 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 3901 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3902 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3903 | // If either expression is type-dependent, just build the AST. |
| 3904 | // FIXME: We'll need to perform some caching of the result of name |
| 3905 | // lookup for operator+. |
| 3906 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3907 | if (Opc > BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) |
| 3908 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3909 | Context.DependentTy, |
| 3910 | Context.DependentTy, TokLoc)); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3911 | else |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3912 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, |
| 3913 | Context.DependentTy, TokLoc)); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3916 | if (getLangOptions().CPlusPlus && Opc != BinaryOperator::PtrMemD && |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3917 | (lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType() || |
| 3918 | rhs->getType()->isRecordType() || rhs->getType()->isEnumeralType())) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3919 | // If this is one of the assignment operators, we only perform |
| 3920 | // overload resolution if the left-hand side is a class or |
| 3921 | // enumeration type (C++ [expr.ass]p3). |
| 3922 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 3923 | !(lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType())) { |
| 3924 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
| 3925 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3926 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3927 | // Determine which overloaded operator we're dealing with. |
| 3928 | static const OverloadedOperatorKind OverOps[] = { |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3929 | // Overloading .* is not possible. |
| 3930 | static_cast<OverloadedOperatorKind>(0), OO_ArrowStar, |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3931 | OO_Star, OO_Slash, OO_Percent, |
| 3932 | OO_Plus, OO_Minus, |
| 3933 | OO_LessLess, OO_GreaterGreater, |
| 3934 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 3935 | OO_EqualEqual, OO_ExclaimEqual, |
| 3936 | OO_Amp, |
| 3937 | OO_Caret, |
| 3938 | OO_Pipe, |
| 3939 | OO_AmpAmp, |
| 3940 | OO_PipePipe, |
| 3941 | OO_Equal, OO_StarEqual, |
| 3942 | OO_SlashEqual, OO_PercentEqual, |
| 3943 | OO_PlusEqual, OO_MinusEqual, |
| 3944 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 3945 | OO_AmpEqual, OO_CaretEqual, |
| 3946 | OO_PipeEqual, |
| 3947 | OO_Comma |
| 3948 | }; |
| 3949 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3950 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3951 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3952 | // to the candidate set. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3953 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3954 | Expr *Args[2] = { lhs, rhs }; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3955 | if (AddOperatorCandidates(OverOp, S, TokLoc, Args, 2, CandidateSet)) |
| 3956 | return ExprError(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3957 | |
| 3958 | // Perform overload resolution. |
| 3959 | OverloadCandidateSet::iterator Best; |
| 3960 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3961 | case OR_Success: { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3962 | // We found a built-in operator or an overloaded operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3963 | FunctionDecl *FnDecl = Best->Function; |
| 3964 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3965 | if (FnDecl) { |
| 3966 | // We matched an overloaded operator. Build a call to that |
| 3967 | // operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3968 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3969 | // Convert the arguments. |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3970 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3971 | if (PerformObjectArgumentInitialization(lhs, Method) || |
| 3972 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(0)->getType(), |
| 3973 | "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3974 | return ExprError(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3975 | } else { |
| 3976 | // Convert the arguments. |
| 3977 | if (PerformCopyInitialization(lhs, FnDecl->getParamDecl(0)->getType(), |
| 3978 | "passing") || |
| 3979 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(1)->getType(), |
| 3980 | "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3981 | return ExprError(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3982 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3984 | // Determine the result type |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3985 | QualType ResultTy |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3986 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3987 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3988 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3989 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3990 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3991 | SourceLocation()); |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 3992 | UsualUnaryConversions(FnExpr); |
| 3993 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3994 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3995 | ResultTy, TokLoc)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3996 | } else { |
| 3997 | // We matched a built-in operator. Convert the arguments, then |
| 3998 | // break out so that we will build the appropriate built-in |
| 3999 | // operator node. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4000 | if (PerformImplicitConversion(lhs, Best->BuiltinTypes.ParamTypes[0], |
| 4001 | Best->Conversions[0], "passing") || |
| 4002 | PerformImplicitConversion(rhs, Best->BuiltinTypes.ParamTypes[1], |
| 4003 | Best->Conversions[1], "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4004 | return ExprError(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4005 | |
| 4006 | break; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4007 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | case OR_No_Viable_Function: |
| 4011 | // No viable function; fall through to handling this as a |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4012 | // built-in operator, which will produce an error message for us. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4013 | break; |
| 4014 | |
| 4015 | case OR_Ambiguous: |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4016 | Diag(TokLoc, diag::err_ovl_ambiguous_oper) |
| 4017 | << BinaryOperator::getOpcodeStr(Opc) |
| 4018 | << lhs->getSourceRange() << rhs->getSourceRange(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4019 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4020 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4021 | |
| 4022 | case OR_Deleted: |
| 4023 | Diag(TokLoc, diag::err_ovl_deleted_oper) |
| 4024 | << Best->Function->isDeleted() |
| 4025 | << BinaryOperator::getOpcodeStr(Opc) |
| 4026 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 4027 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4028 | return ExprError(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4031 | // Either we found no viable overloaded operator or we matched a |
| 4032 | // built-in operator. In either case, fall through to trying to |
| 4033 | // build a built-in operation. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4036 | // Build a built-in binary operation. |
| 4037 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | // Unary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4041 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 4042 | tok::TokenKind Op, ExprArg input) { |
| 4043 | // FIXME: Input is modified later, but smart pointer not reassigned. |
| 4044 | Expr *Input = (Expr*)input.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4045 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4046 | |
| 4047 | if (getLangOptions().CPlusPlus && |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4048 | (Input->getType()->isRecordType() |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4049 | || Input->getType()->isEnumeralType())) { |
| 4050 | // Determine which overloaded operator we're dealing with. |
| 4051 | static const OverloadedOperatorKind OverOps[] = { |
| 4052 | OO_None, OO_None, |
| 4053 | OO_PlusPlus, OO_MinusMinus, |
| 4054 | OO_Amp, OO_Star, |
| 4055 | OO_Plus, OO_Minus, |
| 4056 | OO_Tilde, OO_Exclaim, |
| 4057 | OO_None, OO_None, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4058 | OO_None, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4059 | OO_None |
| 4060 | }; |
| 4061 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 4062 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4063 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4064 | // to the candidate set. |
| 4065 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 4066 | if (OverOp != OO_None && |
| 4067 | AddOperatorCandidates(OverOp, S, OpLoc, &Input, 1, CandidateSet)) |
| 4068 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4069 | |
| 4070 | // Perform overload resolution. |
| 4071 | OverloadCandidateSet::iterator Best; |
| 4072 | switch (BestViableFunction(CandidateSet, Best)) { |
| 4073 | case OR_Success: { |
| 4074 | // We found a built-in operator or an overloaded operator. |
| 4075 | FunctionDecl *FnDecl = Best->Function; |
| 4076 | |
| 4077 | if (FnDecl) { |
| 4078 | // We matched an overloaded operator. Build a call to that |
| 4079 | // operator. |
| 4080 | |
| 4081 | // Convert the arguments. |
| 4082 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4083 | if (PerformObjectArgumentInitialization(Input, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4084 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4085 | } else { |
| 4086 | // Convert the arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4087 | if (PerformCopyInitialization(Input, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4088 | FnDecl->getParamDecl(0)->getType(), |
| 4089 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4090 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4094 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4095 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4096 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4098 | // Build the actual expression node. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4099 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4100 | SourceLocation()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4101 | UsualUnaryConversions(FnExpr); |
| 4102 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4103 | input.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4104 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, &Input, |
| 4105 | 1, ResultTy, OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4106 | } else { |
| 4107 | // We matched a built-in operator. Convert the arguments, then |
| 4108 | // break out so that we will build the appropriate built-in |
| 4109 | // operator node. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4110 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4111 | Best->Conversions[0], "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4112 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4113 | |
| 4114 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4115 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | case OR_No_Viable_Function: |
| 4119 | // No viable function; fall through to handling this as a |
| 4120 | // built-in operator, which will produce an error message for us. |
| 4121 | break; |
| 4122 | |
| 4123 | case OR_Ambiguous: |
| 4124 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4125 | << UnaryOperator::getOpcodeStr(Opc) |
| 4126 | << Input->getSourceRange(); |
| 4127 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4128 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4129 | |
| 4130 | case OR_Deleted: |
| 4131 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4132 | << Best->Function->isDeleted() |
| 4133 | << UnaryOperator::getOpcodeStr(Opc) |
| 4134 | << Input->getSourceRange(); |
| 4135 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4136 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | // Either we found no viable overloaded operator or we matched a |
| 4140 | // built-in operator. In either case, fall through to trying to |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4141 | // build a built-in operation. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4144 | QualType resultType; |
| 4145 | switch (Opc) { |
| 4146 | default: |
| 4147 | assert(0 && "Unimplemented unary expr!"); |
| 4148 | case UnaryOperator::PreInc: |
| 4149 | case UnaryOperator::PreDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4150 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 4151 | Opc == UnaryOperator::PreInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4152 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4153 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4154 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 4155 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4156 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 4157 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4158 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 4159 | break; |
| 4160 | case UnaryOperator::Plus: |
| 4161 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4162 | UsualUnaryConversions(Input); |
| 4163 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4164 | if (resultType->isDependentType()) |
| 4165 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4166 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4167 | break; |
| 4168 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4169 | resultType->isEnumeralType()) |
| 4170 | break; |
| 4171 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4172 | Opc == UnaryOperator::Plus && |
| 4173 | resultType->isPointerType()) |
| 4174 | break; |
| 4175 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4176 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4177 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4178 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4179 | UsualUnaryConversions(Input); |
| 4180 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4181 | if (resultType->isDependentType()) |
| 4182 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4183 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4184 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4185 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4186 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4187 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4188 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4189 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4190 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4191 | break; |
| 4192 | case UnaryOperator::LNot: // logical negation |
| 4193 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4194 | DefaultFunctionArrayConversion(Input); |
| 4195 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4196 | if (resultType->isDependentType()) |
| 4197 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4198 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4199 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4200 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4201 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4202 | // In C++, it's bool. C++ 5.3.1p8 |
| 4203 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4204 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4205 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4206 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 4207 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4208 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4209 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4210 | resultType = Input->getType(); |
| 4211 | break; |
| 4212 | } |
| 4213 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4214 | return ExprError(); |
| 4215 | input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4216 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4219 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4220 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4221 | SourceLocation LabLoc, |
| 4222 | IdentifierInfo *LabelII) { |
| 4223 | // Look up the record for this label identifier. |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 4224 | llvm::DenseMap<IdentifierInfo*, Action::StmtTy*>::iterator I = |
| 4225 | ActiveScope->LabelMap.find(LabelII); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4226 | |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 4227 | LabelStmt *LabelDecl; |
| 4228 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4229 | // If we haven't seen this label yet, create a forward reference. It |
| 4230 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 4231 | if (I == ActiveScope->LabelMap.end()) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4232 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4233 | |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 4234 | ActiveScope->LabelMap.insert(std::make_pair(LabelII, LabelDecl)); |
| 4235 | } else |
| 4236 | LabelDecl = static_cast<LabelStmt *>(I->second); |
| 4237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4238 | // Create the AST node. The address of a label always has type 'void*'. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4239 | return new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4240 | Context.getPointerType(Context.VoidTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4243 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4244 | SourceLocation RPLoc) { // "({..})" |
| 4245 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 4246 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4247 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4248 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4249 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| 4250 | if (isFileScope) { |
| 4251 | return Diag(LPLoc, diag::err_stmtexpr_file_scope); |
| 4252 | } |
| 4253 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4254 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4255 | // example, it is not possible to goto into a stmt expression apparently. |
| 4256 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4257 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4258 | // FIXME: the last statement in the compount stmt has its value used. We |
| 4259 | // should not warn about it being unused. |
| 4260 | |
| 4261 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4262 | // as the type of the stmtexpr. |
| 4263 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4264 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4265 | if (!Compound->body_empty()) { |
| 4266 | Stmt *LastStmt = Compound->body_back(); |
| 4267 | // If LastStmt is a label, skip down through into the body. |
| 4268 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4269 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4270 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4271 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4272 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4273 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4274 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4275 | return new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4276 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4278 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4279 | SourceLocation BuiltinLoc, |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4280 | SourceLocation TypeLoc, |
| 4281 | TypeTy *argty, |
| 4282 | OffsetOfComponent *CompPtr, |
| 4283 | unsigned NumComponents, |
| 4284 | SourceLocation RPLoc) { |
| 4285 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4286 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4287 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4288 | bool Dependent = ArgTy->isDependentType(); |
| 4289 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4290 | // We must have at least one component that refers to the type, and the first |
| 4291 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4292 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4293 | if (!Dependent && !ArgTy->isRecordType()) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4294 | return Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4295 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4296 | // Otherwise, create a null pointer as the base, and iteratively process |
| 4297 | // the offsetof designators. |
| 4298 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 4299 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
| 4300 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
| 4301 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4302 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4303 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4304 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4305 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 4306 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4307 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4308 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4309 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4310 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4311 | if (!Dependent) { |
| 4312 | // FIXME: Dependent case loses a lot of information here. And probably |
| 4313 | // leaks like a sieve. |
| 4314 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4315 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4316 | if (OC.isBrackets) { |
| 4317 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 4318 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 4319 | if (!AT) { |
| 4320 | Res->Destroy(Context); |
| 4321 | return Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 4322 | << Res->getType(); |
| 4323 | } |
| 4324 | |
| 4325 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4326 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4327 | // Promote the array so it looks more like a normal array subscript |
| 4328 | // expression. |
| 4329 | DefaultFunctionArrayConversion(Res); |
| 4330 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4331 | // C99 6.5.2.1p1 |
| 4332 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 4333 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
| 4334 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript) |
| 4335 | << Idx->getSourceRange(); |
| 4336 | |
| 4337 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4338 | OC.LocEnd); |
| 4339 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4340 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4341 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4342 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4343 | if (!RC) { |
| 4344 | Res->Destroy(Context); |
| 4345 | return Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 4346 | << Res->getType(); |
| 4347 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4348 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4349 | // Get the decl corresponding to this. |
| 4350 | RecordDecl *RD = RC->getDecl(); |
| 4351 | FieldDecl *MemberDecl |
| 4352 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4353 | LookupMemberName) |
| 4354 | .getAsDecl()); |
| 4355 | if (!MemberDecl) |
| 4356 | return Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4357 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4358 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4359 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 4360 | // FIXME: Verify that MemberDecl isn't a bitfield. |
| 4361 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 4362 | // matter here. |
| 4363 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4364 | MemberDecl->getType().getNonReferenceType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4365 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4366 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4367 | |
| 4368 | return new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4369 | Context.getSizeType(), BuiltinLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4373 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4374 | TypeTy *arg1, TypeTy *arg2, |
| 4375 | SourceLocation RPLoc) { |
| 4376 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 4377 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4378 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4379 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4380 | |
| 4381 | return new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4382 | argT2, RPLoc); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4385 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4386 | ExprTy *expr1, ExprTy *expr2, |
| 4387 | SourceLocation RPLoc) { |
| 4388 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 4389 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 4390 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4391 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4392 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 4393 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4394 | QualType resType; |
| 4395 | if (CondExpr->isValueDependent()) { |
| 4396 | resType = Context.DependentTy; |
| 4397 | } else { |
| 4398 | // The conditional expression is required to be a constant expression. |
| 4399 | llvm::APSInt condEval(32); |
| 4400 | SourceLocation ExpLoc; |
| 4401 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 4402 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant) |
| 4403 | << CondExpr->getSourceRange(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4404 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4405 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 4406 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 4407 | } |
| 4408 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4409 | return new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4410 | resType, RPLoc); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4413 | //===----------------------------------------------------------------------===// |
| 4414 | // Clang Extensions. |
| 4415 | //===----------------------------------------------------------------------===// |
| 4416 | |
| 4417 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4418 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4419 | // Analyze block parameters. |
| 4420 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4421 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4422 | // Add BSI to CurBlock. |
| 4423 | BSI->PrevBlockInfo = CurBlock; |
| 4424 | CurBlock = BSI; |
Steve Naroff | f3cf897 | 2009-02-28 16:48:43 +0000 | [diff] [blame] | 4425 | ActiveScope = BlockScope; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4426 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4427 | BSI->ReturnType = 0; |
| 4428 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4429 | BSI->hasBlockDeclRefExprs = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4430 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4431 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4432 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4435 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
| 4436 | assert(ParamInfo.getIdentifier() == 0 && "block-id should have no identifier!"); |
| 4437 | |
| 4438 | if (ParamInfo.getNumTypeObjects() == 0 |
| 4439 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
| 4440 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4441 | |
| 4442 | // The type is entirely optional as well, if none, use DependentTy. |
| 4443 | if (T.isNull()) |
| 4444 | T = Context.DependentTy; |
| 4445 | |
| 4446 | // The parameter list is optional, if there was none, assume (). |
| 4447 | if (!T->isFunctionType()) |
| 4448 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 4449 | |
| 4450 | CurBlock->hasPrototype = true; |
| 4451 | CurBlock->isVariadic = false; |
| 4452 | Type *RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4453 | .getTypePtr(); |
| 4454 | |
| 4455 | if (!RetTy->isDependentType()) |
| 4456 | CurBlock->ReturnType = RetTy; |
| 4457 | return; |
| 4458 | } |
| 4459 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4460 | // Analyze arguments to block. |
| 4461 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4462 | "Not a function declarator!"); |
| 4463 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4464 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4465 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 4466 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4467 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4468 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 4469 | // no arguments, not a function that takes a single void argument. |
| 4470 | if (FTI.hasPrototype && |
| 4471 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4472 | (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() && |
| 4473 | ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) { |
| 4474 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4475 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4476 | } else if (FTI.hasPrototype) { |
| 4477 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4478 | CurBlock->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 4479 | CurBlock->isVariadic = FTI.isVariadic; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4480 | QualType T = GetTypeForDeclarator (ParamInfo, CurScope); |
| 4481 | |
| 4482 | Type* RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4483 | .getTypePtr(); |
| 4484 | |
| 4485 | if (!RetTy->isDependentType()) |
| 4486 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4487 | } |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4488 | CurBlock->TheDecl->setArgs(&CurBlock->Params[0], CurBlock->Params.size()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4489 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4490 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 4491 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 4492 | // If this has an identifier, add it to the scope stack. |
| 4493 | if ((*AI)->getIdentifier()) |
| 4494 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 4498 | /// is invoked to pop the information about the block from the action impl. |
| 4499 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 4500 | // Ensure that CurBlock is deleted. |
| 4501 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4502 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4503 | // Pop off CurBlock, handle nested blocks. |
| 4504 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4505 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4506 | // FIXME: Delete the ParmVarDecl objects as well??? |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4507 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 4511 | /// literal was successfully completed. ^(int x){...} |
| 4512 | Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, |
| 4513 | Scope *CurScope) { |
| 4514 | // Ensure that CurBlock is deleted. |
| 4515 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4516 | ExprOwningPtr<CompoundStmt> Body(this, static_cast<CompoundStmt*>(body)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4517 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4518 | PopDeclContext(); |
| 4519 | |
Steve Naroff | b098c14 | 2009-02-28 21:01:15 +0000 | [diff] [blame] | 4520 | // Before poping CurBlock, set ActiveScope to this scopes parent. |
| 4521 | ActiveScope = CurBlock->TheScope->getParent(); |
| 4522 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4523 | // Pop off CurBlock, handle nested blocks. |
| 4524 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4525 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4526 | QualType RetTy = Context.VoidTy; |
| 4527 | if (BSI->ReturnType) |
| 4528 | RetTy = QualType(BSI->ReturnType, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4529 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4530 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4531 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 4532 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4533 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4534 | QualType BlockTy; |
| 4535 | if (!BSI->hasPrototype) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4536 | BlockTy = Context.getFunctionNoProtoType(RetTy); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4537 | else |
| 4538 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4539 | BSI->isVariadic, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4540 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4541 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4542 | |
Steve Naroff | 1c90bfc | 2008-10-08 18:44:00 +0000 | [diff] [blame] | 4543 | BSI->TheDecl->setBody(Body.take()); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4544 | return new (Context) BlockExpr(BSI->TheDecl, BlockTy, BSI->hasBlockDeclRefExprs); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4547 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 4548 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4549 | SourceLocation RPLoc) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4550 | Expr *E = static_cast<Expr*>(expr); |
| 4551 | QualType T = QualType::getFromOpaquePtr(type); |
| 4552 | |
| 4553 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4554 | |
| 4555 | // Get the va_list type |
| 4556 | QualType VaListType = Context.getBuiltinVaListType(); |
| 4557 | // Deal with implicit array decay; for example, on x86-64, |
| 4558 | // va_list is an array, but it's supposed to decay to |
| 4559 | // a pointer for va_arg. |
| 4560 | if (VaListType->isArrayType()) |
| 4561 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | efbe85c | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 4562 | // Make sure the input expression also decays appropriately. |
| 4563 | UsualUnaryConversions(E); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4564 | |
| 4565 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4566 | return Diag(E->getLocStart(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4567 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4568 | << E->getType() << E->getSourceRange(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4569 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4570 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4571 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4572 | return new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), RPLoc); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4575 | Sema::ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
| 4576 | // The type of __null will be int or long, depending on the size of |
| 4577 | // pointers on the target. |
| 4578 | QualType Ty; |
| 4579 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 4580 | Ty = Context.IntTy; |
| 4581 | else |
| 4582 | Ty = Context.LongTy; |
| 4583 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4584 | return new (Context) GNUNullExpr(Ty, TokenLoc); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4587 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 4588 | SourceLocation Loc, |
| 4589 | QualType DstType, QualType SrcType, |
| 4590 | Expr *SrcExpr, const char *Flavor) { |
| 4591 | // Decode the result (notice that AST's are still created for extensions). |
| 4592 | bool isInvalid = false; |
| 4593 | unsigned DiagKind; |
| 4594 | switch (ConvTy) { |
| 4595 | default: assert(0 && "Unknown conversion type"); |
| 4596 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4597 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4598 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 4599 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4600 | case IntToPointer: |
| 4601 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 4602 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4603 | case IncompatiblePointer: |
| 4604 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 4605 | break; |
| 4606 | case FunctionVoidPointer: |
| 4607 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 4608 | break; |
| 4609 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 4610 | // If the qualifiers lost were because we were applying the |
| 4611 | // (deprecated) C++ conversion from a string literal to a char* |
| 4612 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 4613 | // Ideally, this check would be performed in |
| 4614 | // CheckPointerTypesForAssignment. However, that would require a |
| 4615 | // bit of refactoring (so that the second argument is an |
| 4616 | // expression, rather than a type), which should be done as part |
| 4617 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 4618 | // C++ semantics. |
| 4619 | if (getLangOptions().CPlusPlus && |
| 4620 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 4621 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4622 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 4623 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4624 | case IntToBlockPointer: |
| 4625 | DiagKind = diag::err_int_to_block_pointer; |
| 4626 | break; |
| 4627 | case IncompatibleBlockPointer: |
Steve Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 4628 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4629 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4630 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4631 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4632 | // it can give a more specific diagnostic. |
| 4633 | DiagKind = diag::warn_incompatible_qualified_id; |
| 4634 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4635 | case IncompatibleVectors: |
| 4636 | DiagKind = diag::warn_incompatible_vectors; |
| 4637 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4638 | case Incompatible: |
| 4639 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 4640 | isInvalid = true; |
| 4641 | break; |
| 4642 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4643 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4644 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 4645 | << SrcExpr->getSourceRange(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4646 | return isInvalid; |
| 4647 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4648 | |
| 4649 | bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result) |
| 4650 | { |
| 4651 | Expr::EvalResult EvalResult; |
| 4652 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4653 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4654 | EvalResult.HasSideEffects) { |
| 4655 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 4656 | |
| 4657 | if (EvalResult.Diag) { |
| 4658 | // We only show the note if it's not the usual "invalid subexpression" |
| 4659 | // or if it's actually in a subexpression. |
| 4660 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 4661 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 4662 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4663 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4664 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4665 | return true; |
| 4666 | } |
| 4667 | |
| 4668 | if (EvalResult.Diag) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4669 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4670 | E->getSourceRange(); |
| 4671 | |
| 4672 | // Print the reason it's not a constant. |
| 4673 | if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 4674 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4675 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4676 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4677 | if (Result) |
| 4678 | *Result = EvalResult.Val.getInt(); |
| 4679 | return false; |
| 4680 | } |